Untitled diff

建立於 差異永不過期
4 刪除
195 行
67 新增
227 行
LEAF_ENTRY JIT_ByRefWriteBarrier, _TEXT
LEAF_ENTRY JIT_ByRefWriteBarrierBatch, _TEXT
LOCAL_LABEL(NextByref_ByRefWriteBarrierBatch):
mov rcx, [rsi]
mov rcx, [rsi]


// If !WRITE_BARRIER_CHECK do the write first, otherwise we might have to do some ShadowGC stuff
// If !WRITE_BARRIER_CHECK do the write first, otherwise we might have to do some ShadowGC stuff
#ifndef WRITE_BARRIER_CHECK
#ifndef WRITE_BARRIER_CHECK
// rcx is [rsi]
// rcx is [rsi]
mov [rdi], rcx
mov [rdi], rcx
#endif
#endif


// When WRITE_BARRIER_CHECK is defined _NotInHeap will write the reference
// When WRITE_BARRIER_CHECK is defined _NotInHeap will write the reference
// but if it isn't then it will just return.
// but if it isn't then it will just return.
//
//
// See if this is in GCHeap
// See if this is in GCHeap
PREPARE_EXTERNAL_VAR g_lowest_address, rax
PREPARE_EXTERNAL_VAR g_lowest_address, rax
cmp rdi, [rax]
cmp rdi, [rax]
jb LOCAL_LABEL(NotInHeap_ByRefWriteBarrier)
jb LOCAL_LABEL(NotInHeap_ByRefWriteBarrierBatch)
PREPARE_EXTERNAL_VAR g_highest_address, rax
PREPARE_EXTERNAL_VAR g_highest_address, rax
cmp rdi, [rax]
cmp rdi, [rax]
jnb LOCAL_LABEL(NotInHeap_ByRefWriteBarrier)
jnb LOCAL_LABEL(NotInHeap_ByRefWriteBarrierBatch)


#ifdef WRITE_BARRIER_CHECK
#ifdef WRITE_BARRIER_CHECK
// **ALSO update the shadow GC heap if that is enabled**
// **ALSO update the shadow GC heap if that is enabled**
// Do not perform the work if g_GCShadow is 0
// Do not perform the work if g_GCShadow is 0
PREPARE_EXTERNAL_VAR g_GCShadow, rax
PREPARE_EXTERNAL_VAR g_GCShadow, rax
cmp qword ptr [rax], 0
cmp qword ptr [rax], 0
je LOCAL_LABEL(NoShadow_ByRefWriteBarrier)
je LOCAL_LABEL(NoShadow_ByRefWriteBarrierBatch)


// If we end up outside of the heap don't corrupt random memory
// If we end up outside of the heap don't corrupt random memory
mov r10, rdi
mov r10, rdi
PREPARE_EXTERNAL_VAR g_lowest_address, rax
PREPARE_EXTERNAL_VAR g_lowest_address, rax
sub r10, [rax]
sub r10, [rax]
jb LOCAL_LABEL(NoShadow_ByRefWriteBarrier)
jb LOCAL_LABEL(NoShadow_ByRefWriteBarrierBatch)


// Check that our adjusted destination is somewhere in the shadow gc
// Check that our adjusted destination is somewhere in the shadow gc
PREPARE_EXTERNAL_VAR g_GCShadow, rax
PREPARE_EXTERNAL_VAR g_GCShadow, rax
add r10, [rax]
add r10, [rax]
PREPARE_EXTERNAL_VAR g_GCShadowEnd, rax
PREPARE_EXTERNAL_VAR g_GCShadowEnd, rax
cmp r10, [rax]
cmp r10, [rax]
jnb LOCAL_LABEL(NoShadow_ByRefWriteBarrier)
jnb LOCAL_LABEL(NoShadow_ByRefWriteBarrierBatch)


// Write ref into real GC
// Write ref into real GC
mov [rdi], rcx
mov [rdi], rcx
// Write ref into shadow GC
// Write ref into shadow GC
mov [r10], rcx
mov [r10], rcx


// Ensure that the write to the shadow heap occurs before the read from
// Ensure that the write to the shadow heap occurs before the read from
// the GC heap so that race conditions are caught by INVALIDGCVALUE
// the GC heap so that race conditions are caught by INVALIDGCVALUE
mfence
mfence


// Check that GC/ShadowGC values match
// Check that GC/ShadowGC values match
mov r11, [rdi]
mov r11, [rdi]
mov rax, [r10]
mov rax, [r10]
cmp rax, r11
cmp rax, r11
je LOCAL_LABEL(DoneShadow_ByRefWriteBarrier)
je LOCAL_LABEL(DoneShadow_ByRefWriteBarrierBatch)
movabs r11, INVALIDGCVALUE
movabs r11, INVALIDGCVALUE
mov [r10], r11
mov [r10], r11


jmp LOCAL_LABEL(DoneShadow_ByRefWriteBarrier)
jmp LOCAL_LABEL(DoneShadow_ByRefWriteBarrierBatch)


// If we don't have a shadow GC we won't have done the write yet
// If we don't have a shadow GC we won't have done the write yet
LOCAL_LABEL(NoShadow_ByRefWriteBarrier):
LOCAL_LABEL(NoShadow_ByRefWriteBarrierBatch):
mov [rdi], rcx
mov [rdi], rcx


// If we had a shadow GC then we already wrote to the real GC at the same time
// If we had a shadow GC then we already wrote to the real GC at the same time
// as the shadow GC so we want to jump over the real write immediately above.
// as the shadow GC so we want to jump over the real write immediately above.
// Additionally we know for sure that we are inside the heap and therefore don't
// Additionally we know for sure that we are inside the heap and therefore don't
// need to replicate the above checks.
// need to replicate the above checks.
LOCAL_LABEL(DoneShadow_ByRefWriteBarrier):
LOCAL_LABEL(DoneShadow_ByRefWriteBarrierBatch):
#endif
#endif


#ifdef FEATURE_USE_SOFTWARE_WRITE_WATCH_FOR_GC_HEAP
#ifdef FEATURE_USE_SOFTWARE_WRITE_WATCH_FOR_GC_HEAP
// Update the write watch table if necessary
// Update the write watch table if necessary
PREPARE_EXTERNAL_VAR g_sw_ww_enabled_for_gc_heap, rax
PREPARE_EXTERNAL_VAR g_sw_ww_enabled_for_gc_heap, rax
cmp byte ptr [rax], 0x0
cmp byte ptr [rax], 0x0
je LOCAL_LABEL(CheckCardTable_ByRefWriteBarrier)
je LOCAL_LABEL(CheckCardTable_ByRefWriteBarrierBatch)
mov rax, rdi
mov rax, rdi
shr rax, 0xC // SoftwareWriteWatch::AddressToTableByteIndexShift
shr rax, 0xC // SoftwareWriteWatch::AddressToTableByteIndexShift
PREPARE_EXTERNAL_VAR g_sw_ww_table, r10
PREPARE_EXTERNAL_VAR g_sw_ww_table, r10
add rax, qword ptr [r10]
add rax, qword ptr [r10]
cmp byte ptr [rax], 0x0
cmp byte ptr [rax], 0x0
jne LOCAL_LABEL(CheckCardTable_ByRefWriteBarrier)
jne LOCAL_LABEL(CheckCardTable_ByRefWriteBarrierBatch)
mov byte ptr [rax], 0xFF
mov byte ptr [rax], 0xFF
#endif
#endif


LOCAL_LABEL(CheckCardTable_ByRefWriteBarrier):
LOCAL_LABEL(CheckCardTable_ByRefWriteBarrierBatch):
// See if we can just quick out
// See if we can just quick out
PREPARE_EXTERNAL_VAR g_ephemeral_low, rax
PREPARE_EXTERNAL_VAR g_ephemeral_low, rax
cmp rcx, [rax]
cmp rcx, [rax]
jb LOCAL_LABEL(Exit_ByRefWriteBarrier)
jb LOCAL_LABEL(Exit_ByRefWriteBarrierBatch)
PREPARE_EXTERNAL_VAR g_ephemeral_high, rax
PREPARE_EXTERNAL_VAR g_ephemeral_high, rax
cmp rcx, [rax]
cmp rcx, [rax]
jnb LOCAL_LABEL(Exit_ByRefWriteBarrier)
jnb LOCAL_LABEL(Exit_ByRefWriteBarrierBatch)


mov rax, rcx
mov rax, rcx


PREPARE_EXTERNAL_VAR g_region_shr, rcx
PREPARE_EXTERNAL_VAR g_region_shr, rcx
mov cl, [rcx]
mov cl, [rcx]
test cl, cl
test cl, cl
je LOCAL_LABEL(SkipCheck_ByRefWriteBarrier)
je LOCAL_LABEL(SkipCheck_ByRefWriteBarrierBatch)