Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NativeAOT] Some cleanup of assembly helpers in hijack area. #72542

Merged
merged 17 commits into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/coreclr/nativeaot/Runtime/ICodeManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ C_ASSERT(PTFF_X1_IS_BYREF == ((uint64_t)GCRK_Scalar_Byref << 32));

inline uint64_t ReturnKindToTransitionFrameFlags(GCRefKind returnKind)
{
if (returnKind == GCRK_Scalar)
return 0;

return PTFF_SAVE_X0 | PTFF_SAVE_X1 | ((uint64_t)returnKind << 32);
// just need to report gc ref bits here.
// appropriate PTFF_SAVE_ bits will be added by the frame building routine.
return ((uint64_t)returnKind << 32);
}

inline GCRefKind TransitionFrameFlagsToReturnKind(uint64_t transFrameFlags)
Expand All @@ -76,10 +75,9 @@ C_ASSERT(PTFF_RDX_IS_BYREF == ((uint64_t)GCRK_Scalar_Byref << 16));

inline uint64_t ReturnKindToTransitionFrameFlags(GCRefKind returnKind)
{
if (returnKind == GCRK_Scalar)
return 0;

return PTFF_SAVE_RAX | PTFF_SAVE_RDX | ((uint64_t)returnKind << 16);
// just need to report gc ref bits here.
// appropriate PTFF_SAVE_ bits will be added by the frame building routine.
return ((uint64_t)returnKind << 16);
}

inline GCRefKind TransitionFrameFlagsToReturnKind(uint64_t transFrameFlags)
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/nativeaot/Runtime/amd64/AsmMacros.inc
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,6 @@ EXTERN RhpGcAlloc : PROC
EXTERN RhpValidateExInfoPop : PROC
EXTERN RhDebugBreak : PROC
EXTERN RhpWaitForGC2 : PROC
EXTERN RhpReversePInvokeAttachOrTrapThread2 : PROC
EXTERN RhExceptionHandling_FailedAllocation : PROC
EXTERN RhThrowHwEx : PROC
EXTERN RhThrowEx : PROC
Expand Down
100 changes: 18 additions & 82 deletions src/coreclr/nativeaot/Runtime/amd64/GcProbe.S
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,6 @@
pop rdx
.endm

//
// Macro to clear the hijack state. This is safe to do because the suspension code will not Unhijack this
// thread if it finds it at an IP that isn`t managed code.
//
// Register state on entry:
// R11: thread pointer
//
// Register state on exit:
// R9: trashed
//
.macro ClearHijackState
xor r9, r9
mov [r11 + OFFSETOF__Thread__m_ppvHijackedReturnAddressLocation], r9
mov [r11 + OFFSETOF__Thread__m_pvHijackedReturnAddress], r9
mov [r11 + OFFSETOF__Thread__m_uHijackedReturnValueFlags], r9
.endm


//
// The prolog for all GC suspension hijacks (normal and stress). Fixes up the hijacked return address, and
// clears the hijack state.
Expand All @@ -98,7 +80,7 @@
// RAX, RDX preserved, other volatile regs trashed
//
.macro FixupHijackedCallstack
// preserve RAX, RDX as they may contain retuvalues
// preserve RAX, RDX as they may contain return values
push rax
push rdx

Expand All @@ -109,88 +91,43 @@
pop rdx
pop rax

//
// Fix the stack by pushing the original return address
//
mov rcx, [r11 + OFFSETOF__Thread__m_pvHijackedReturnAddress]
push rcx

// Fetch the return address flags
mov rcx, [r11 + OFFSETOF__Thread__m_uHijackedReturnValueFlags]

ClearHijackState
.endm

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// RhpWaitForGCNoAbort -- rare path for WaitForGCCompletion
//
//
// INPUT: RDI: transition frame
//
// TRASHES: RCX, RDI, R8, R9, R10, R11
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
NESTED_ENTRY RhpWaitForGCNoAbort, _TEXT, NoHandler
END_PROLOGUE

mov rdx, [rdi + OFFSETOF__PInvokeTransitionFrame__m_pThread]

test dword ptr [rdx + OFFSETOF__Thread__m_ThreadStateFlags], TSF_DoNotTriggerGc
jnz Done

// passing transition frame pointer in rdi
call C_FUNC(RhpWaitForGC2)

Done:
ret

NESTED_END RhpWaitForGCNoAbort, _TEXT

//
// Set the Thread state and wait for a GC to complete.
//
// Register state on entry:
// RBX: thread pointer
//
// Register state on exit:
// RBX: thread pointer
// All other registers trashed
//

.macro WaitForGCCompletion
test dword ptr [rbx + OFFSETOF__Thread__m_ThreadStateFlags], TSF_SuppressGcStress + TSF_DoNotTriggerGc
jnz LOCAL_LABEL(NoWait)

mov rdi, [rbx + OFFSETOF__Thread__m_pDeferredTransitionFrame]
call C_FUNC(RhpWaitForGCNoAbort)
LOCAL_LABEL(NoWait):

// Clear hijack state
xor r9, r9
mov [r11 + OFFSETOF__Thread__m_ppvHijackedReturnAddressLocation], r9
mov [r11 + OFFSETOF__Thread__m_pvHijackedReturnAddress], r9
mov [r11 + OFFSETOF__Thread__m_uHijackedReturnValueFlags], r9
.endm

//
//
//
// GC Probe Hijack target
//
//

NESTED_ENTRY RhpGcProbeHijack, _TEXT, NoHandler
END_PROLOGUE
FixupHijackedCallstack
or ecx, DEFAULT_FRAME_SAVE_FLAGS + PTFF_SAVE_RAX + PTFF_SAVE_RDX
jmp C_FUNC(RhpGcProbe)
NESTED_END RhpGcProbeHijack, _TEXT

NESTED_ENTRY RhpGcProbe, _TEXT, NoHandler
test dword ptr [C_VAR(RhpTrapThreads)], TrapThreadsFlags_TrapThreads
jnz LOCAL_LABEL(RhpGcProbe_Trap)
jnz LOCAL_LABEL(WaitForGC)
ret
LOCAL_LABEL(RhpGcProbe_Trap):

LOCAL_LABEL(WaitForGC):
or ecx, DEFAULT_FRAME_SAVE_FLAGS + PTFF_SAVE_RAX + PTFF_SAVE_RDX
jmp C_FUNC(RhpWaitForGC)
NESTED_END RhpGcProbeHijack, _TEXT

NESTED_ENTRY RhpWaitForGC, _TEXT, NoHandler
PUSH_PROBE_FRAME r11, rax, rcx
END_PROLOGUE

mov rbx, r11
WaitForGCCompletion
mov rdi, [rbx + OFFSETOF__Thread__m_pDeferredTransitionFrame]
call C_FUNC(RhpWaitForGC2)

mov rax, [rbx + OFFSETOF__Thread__m_pDeferredTransitionFrame]
test dword ptr [rax + OFFSETOF__PInvokeTransitionFrame__m_Flags], PTFF_THREAD_ABORT
Expand All @@ -203,7 +140,7 @@ LOCAL_LABEL(Abort):
pop rdx // return address as exception RIP
jmp C_FUNC(RhpThrowHwEx) // Throw the ThreadAbortException as a special kind of hardware exception

NESTED_END RhpGcProbe, _TEXT
NESTED_END RhpWaitForGC, _TEXT


LEAF_ENTRY RhpGcPoll, _TEXT
Expand All @@ -212,7 +149,6 @@ LEAF_ENTRY RhpGcPoll, _TEXT
ret
LOCAL_LABEL(RhpGcPoll_RarePath):
jmp C_FUNC(RhpGcPollRare)

LEAF_END RhpGcPoll, _TEXT

NESTED_ENTRY RhpGcPollRare, _TEXT, NoHandler
Expand Down
Loading