Skip to content

Commit

Permalink
Get ThreadContext for AVX registers (dotnet#104514)
Browse files Browse the repository at this point in the history
* some logs

* debugging the adventure

* using the XSTATE_SUPPORTED macro

* use minipal getcpufeatures

* add conditional in MachExceptionInfo constructor

* fix whitespace and remove logs

* whitepace

* remove conditional checks and rely on Fallthrough

* remove uneeded dependencies

---------

Co-authored-by: Diag <[email protected]>
  • Loading branch information
mikelle-rogers and Diag authored Jul 9, 2024
1 parent 1164d2f commit 5b5d791
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
21 changes: 15 additions & 6 deletions src/coreclr/pal/src/exception/machexception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,8 +681,8 @@ HijackFaultingThread(
BuildExceptionRecord(exceptionInfo, &exceptionRecord);

#if defined(HOST_AMD64)
threadContext.ContextFlags = CONTEXT_FLOATING_POINT;
CONTEXT_GetThreadContextFromThreadState(x86_FLOAT_STATE, (thread_state_t)&exceptionInfo.FloatState, &threadContext);
threadContext.ContextFlags = CONTEXT_FLOATING_POINT | CONTEXT_XSTATE;
CONTEXT_GetThreadContextFromThreadState(x86_AVX512_STATE, (thread_state_t)&exceptionInfo.FloatState, &threadContext);

threadContext.ContextFlags |= CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS;
CONTEXT_GetThreadContextFromThreadState(x86_THREAD_STATE, (thread_state_t)&exceptionInfo.ThreadState, &threadContext);
Expand Down Expand Up @@ -1265,10 +1265,19 @@ MachExceptionInfo::MachExceptionInfo(mach_port_t thread, MachMessage& message)
machret = thread_get_state(thread, x86_THREAD_STATE, (thread_state_t)&ThreadState, &count);
CHECK_MACH("thread_get_state", machret);

count = x86_FLOAT_STATE_COUNT;
machret = thread_get_state(thread, x86_FLOAT_STATE, (thread_state_t)&FloatState, &count);
CHECK_MACH("thread_get_state(float)", machret);

count = x86_AVX512_STATE_COUNT;
machret = thread_get_state(thread, x86_AVX512_STATE, (thread_state_t)&FloatState, &count);
if (machret != KERN_SUCCESS)
{
count = x86_AVX_STATE_COUNT;
machret = thread_get_state(thread, x86_AVX_STATE, (thread_state_t)&FloatState, &count);
if (machret != KERN_SUCCESS)
{
count = x86_FLOAT_STATE_COUNT;
machret = thread_get_state(thread, x86_FLOAT_STATE, (thread_state_t)&FloatState, &count);
CHECK_MACH("thread_get_state(float)", machret);
}
}
count = x86_DEBUG_STATE_COUNT;
machret = thread_get_state(thread, x86_DEBUG_STATE, (thread_state_t)&DebugState, &count);
CHECK_MACH("thread_get_state(debug)", machret);
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/pal/src/exception/machmessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ struct MachExceptionInfo
mach_exception_data_type_t Subcodes[2];
#if defined(HOST_AMD64)
x86_thread_state_t ThreadState;
x86_float_state_t FloatState;
x86_avx512_state_t FloatState;
x86_debug_state_t DebugState;
#elif defined(HOST_ARM64)
arm_thread_state64_t ThreadState;
Expand Down
12 changes: 12 additions & 0 deletions src/coreclr/pal/src/thread/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1717,6 +1717,18 @@ CONTEXT_GetThreadContextFromThreadState(
CONTEXT_GetThreadContextFromThreadState((thread_state_flavor_t)pState->fsh.flavor, (thread_state_t)&pState->ufs, lpContext);
}
break;
case x86_AVX_STATE:
{
x86_avx_state_t *pState = (x86_avx_state_t *)threadState;
CONTEXT_GetThreadContextFromThreadState((thread_state_flavor_t)pState->ash.flavor, (thread_state_t)&pState->ufs, lpContext);
}
break;
case x86_AVX512_STATE:
{
x86_avx512_state_t *pState = (x86_avx512_state_t *)threadState;
CONTEXT_GetThreadContextFromThreadState((thread_state_flavor_t)pState->ash.flavor, (thread_state_t)&pState->ufs, lpContext);
}
break;
#elif defined(HOST_ARM64)
case ARM_THREAD_STATE64:
if (lpContext->ContextFlags & (CONTEXT_CONTROL | CONTEXT_INTEGER) & CONTEXT_AREA_MASK)
Expand Down

0 comments on commit 5b5d791

Please sign in to comment.