1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-10 04:22:20 +02:00

Capture xmm/ymm registers

This commit is contained in:
Brian Fiete 2022-01-03 11:24:45 -05:00
parent f1c38c792d
commit 14e1ffa5ac

View file

@ -2193,32 +2193,165 @@ BFP_EXPORT void BFP_CALLTYPE BfpThread_Resume(BfpThread* thread, BfpThreadResult
OUTRESULT(BfpThreadResult_Ok); OUTRESULT(BfpThreadResult_Ok);
} }
// Windows 7 SP1 is the first version of Windows to support the AVX API.
// The value for CONTEXT_XSTATE has changed between Windows 7 and
// Windows 7 SP1 and greater.
// While the value will be correct for future SDK headers, we need to set
// this value manually when building with a Windows 7 SDK for running on
// Windows 7 SPI OS bits.
#undef CONTEXT_XSTATE
#if defined(_M_X64)
#define CONTEXT_XSTATE (0x00100040)
#else
#define CONTEXT_XSTATE (0x00010040)
#endif
// Since the AVX API is not declared in the Windows 7 SDK headers and
// since we don't have the proper libs to work with, we will declare
// the API as function pointers and get them with GetProcAddress calls
// from kernel32.dll. We also need to set some #defines.
#define XSTATE_AVX (XSTATE_GSSE)
#define XSTATE_MASK_AVX (XSTATE_MASK_GSSE)
typedef DWORD64(WINAPI* PGETENABLEDXSTATEFEATURES)();
static PGETENABLEDXSTATEFEATURES pfnGetEnabledXStateFeatures = NULL;
typedef BOOL(WINAPI* PINITIALIZECONTEXT)(PVOID Buffer, DWORD ContextFlags, PCONTEXT* Context, PDWORD ContextLength);
static PINITIALIZECONTEXT pfnInitializeContext = NULL;
typedef BOOL(WINAPI* PGETXSTATEFEATURESMASK)(PCONTEXT Context, PDWORD64 FeatureMask);
static PGETXSTATEFEATURESMASK pfnGetXStateFeaturesMask = NULL;
typedef PVOID(WINAPI* LOCATEXSTATEFEATURE)(PCONTEXT Context, DWORD FeatureId, PDWORD Length);
static LOCATEXSTATEFEATURE pfnLocateXStateFeature = NULL;
typedef BOOL(WINAPI* SETXSTATEFEATURESMASK)(PCONTEXT Context, DWORD64 FeatureMask);
static SETXSTATEFEATURESMASK pfnSetXStateFeaturesMask = NULL;
static uint8 ContextBuffer[4096];
static CONTEXT* CaptureRegistersEx(HANDLE hThread, intptr*& curPtr)
{
PCONTEXT Context;
DWORD ContextSize;
DWORD64 FeatureMask;
DWORD FeatureLength;
BOOL Success;
PM128A Xmm;
PM128A Ymm;
if (pfnGetEnabledXStateFeatures == (PGETENABLEDXSTATEFEATURES)-1)
return NULL;
if (pfnGetEnabledXStateFeatures == NULL)
{
HMODULE hm = GetModuleHandleA("kernel32.dll");
if (hm == NULL)
{
pfnGetEnabledXStateFeatures = (PGETENABLEDXSTATEFEATURES)-1;
return NULL;
}
pfnGetEnabledXStateFeatures = (PGETENABLEDXSTATEFEATURES)GetProcAddress(hm, "GetEnabledXStateFeatures");
pfnInitializeContext = (PINITIALIZECONTEXT)GetProcAddress(hm, "InitializeContext");
pfnGetXStateFeaturesMask = (PGETXSTATEFEATURESMASK)GetProcAddress(hm, "GetXStateFeaturesMask");
pfnLocateXStateFeature = (LOCATEXSTATEFEATURE)GetProcAddress(hm, "LocateXStateFeature");
pfnSetXStateFeaturesMask = (SETXSTATEFEATURESMASK)GetProcAddress(hm, "SetXStateFeaturesMask");
if (pfnGetEnabledXStateFeatures == NULL
|| pfnInitializeContext == NULL
|| pfnGetXStateFeaturesMask == NULL
|| pfnLocateXStateFeature == NULL
|| pfnSetXStateFeaturesMask == NULL)
{
pfnGetEnabledXStateFeatures = (PGETENABLEDXSTATEFEATURES)-1;
return NULL;
}
}
FeatureMask = pfnGetEnabledXStateFeatures();
if ((FeatureMask & XSTATE_MASK_AVX) == 0)
return NULL;
ContextSize = 0;
Success = pfnInitializeContext(NULL,
CONTEXT_ALL | CONTEXT_XSTATE | CONTEXT_EXCEPTION_REQUEST,
NULL,
&ContextSize);
if (ContextSize > sizeof(ContextBuffer))
return NULL;
Success = pfnInitializeContext(ContextBuffer,
CONTEXT_ALL | CONTEXT_XSTATE | CONTEXT_EXCEPTION_REQUEST,
&Context,
&ContextSize);
if (Success == FALSE)
return NULL;
Success = pfnSetXStateFeaturesMask(Context, XSTATE_MASK_AVX);
if (Success == FALSE)
return Context;
Success = GetThreadContext(hThread, Context);
if (Success == FALSE)
return Context;
Success = pfnGetXStateFeaturesMask(Context, &FeatureMask);
if (Success == FALSE)
return Context;
if ((FeatureMask & XSTATE_MASK_AVX) == 0)
return Context;
Xmm = (PM128A)pfnLocateXStateFeature(Context, XSTATE_LEGACY_SSE, &FeatureLength);
Ymm = (PM128A)pfnLocateXStateFeature(Context, XSTATE_AVX, NULL);
memcpy(curPtr, Ymm, FeatureLength);
curPtr += FeatureLength / sizeof(intptr);
return Context;
}
BFP_EXPORT void BFP_CALLTYPE BfpThread_GetIntRegisters(BfpThread* thread, intptr* outStackPtr, intptr* outIntRegs, int* inOutIntRegCount, BfpThreadResult* outResult) BFP_EXPORT void BFP_CALLTYPE BfpThread_GetIntRegisters(BfpThread* thread, intptr* outStackPtr, intptr* outIntRegs, int* inOutIntRegCount, BfpThreadResult* outResult)
{ {
CONTEXT ctx; CONTEXT ctx;
intptr* curPtr = outIntRegs;
CONTEXT* ctxPtr = NULL;
if (*inOutIntRegCount > 48)
ctxPtr = CaptureRegistersEx((HANDLE)thread, curPtr);
if (ctxPtr == NULL)
{
memset(&ctx, 0, sizeof(CONTEXT)); memset(&ctx, 0, sizeof(CONTEXT));
ctx.ContextFlags = CONTEXT_INTEGER | CONTEXT_CONTROL; ctx.ContextFlags = CONTEXT_ALL;
BOOL success = ::GetThreadContext((HANDLE)thread, &ctx); BOOL success = ::GetThreadContext((HANDLE)thread, (CONTEXT*)&ctx);
if (!success) if (!success)
{ {
int error = GetLastError(); int error = GetLastError();
OUTRESULT(BfpThreadResult_UnknownError); OUTRESULT(BfpThreadResult_UnknownError);
return; return;
} }
ctxPtr = &ctx;
DWORD lastError = GetLastError(); DWORD lastError = GetLastError();
BF_ASSERT(success); BF_ASSERT(success);
}
#ifdef BF32 #ifdef BF32
*outStackPtr = (intptr)ctx.Esp; * outStackPtr = (intptr)ctxPtr->Esp;
if (*inOutIntRegCount < 7) if (*inOutIntRegCount < (int)(curPtr - outIntRegs) + 7)
{ {
OUTRESULT(BfpThreadResult_InsufficientBuffer); OUTRESULT(BfpThreadResult_InsufficientBuffer);
return; return;
} }
#else #else
*outStackPtr = (intptr)ctx.Rsp; * outStackPtr = (intptr)ctxPtr->Rsp;
if (*inOutIntRegCount < 15) if (*inOutIntRegCount < (int)(curPtr - outIntRegs) + 48)
{ {
OUTRESULT(BfpThreadResult_InsufficientBuffer); OUTRESULT(BfpThreadResult_InsufficientBuffer);
return; return;
@ -2230,33 +2363,33 @@ BFP_EXPORT void BFP_CALLTYPE BfpThread_GetIntRegisters(BfpThread* thread, intptr
if (outIntRegs == NULL) if (outIntRegs == NULL)
return; return;
intptr* curPtr = outIntRegs;
#ifdef BF32 #ifdef BF32
*(curPtr++) = (intptr)ctx.Eax; * (curPtr++) = (intptr)ctxPtr->Eax;
*(curPtr++) = (intptr)ctx.Ebx; *(curPtr++) = (intptr)ctxPtr->Ebx;
*(curPtr++) = (intptr)ctx.Ecx; *(curPtr++) = (intptr)ctxPtr->Ecx;
*(curPtr++) = (intptr)ctx.Edx; *(curPtr++) = (intptr)ctxPtr->Edx;
*(curPtr++) = (intptr)ctx.Esi; *(curPtr++) = (intptr)ctxPtr->Esi;
*(curPtr++) = (intptr)ctx.Edi; *(curPtr++) = (intptr)ctxPtr->Edi;
*(curPtr++) = (intptr)ctx.Ebp; *(curPtr++) = (intptr)ctxPtr->Ebp;
#else #else
*(curPtr++) = (intptr)ctx.SegFs; // Testing * (curPtr++) = (intptr)ctxPtr->SegFs; // Testing
*(curPtr++) = (intptr)ctx.Rax; *(curPtr++) = (intptr)ctxPtr->Rax;
*(curPtr++) = (intptr)ctx.Rbx; *(curPtr++) = (intptr)ctxPtr->Rbx;
*(curPtr++) = (intptr)ctx.Rcx; *(curPtr++) = (intptr)ctxPtr->Rcx;
*(curPtr++) = (intptr)ctx.Rdx; *(curPtr++) = (intptr)ctxPtr->Rdx;
*(curPtr++) = (intptr)ctx.Rsi; *(curPtr++) = (intptr)ctxPtr->Rsi;
*(curPtr++) = (intptr)ctx.Rdi; *(curPtr++) = (intptr)ctxPtr->Rdi;
*(curPtr++) = (intptr)ctx.Rbp; *(curPtr++) = (intptr)ctxPtr->Rbp;
*(curPtr++) = (intptr)ctx.R8; *(curPtr++) = (intptr)ctxPtr->R8;
*(curPtr++) = (intptr)ctx.R9; *(curPtr++) = (intptr)ctxPtr->R9;
*(curPtr++) = (intptr)ctx.R10; *(curPtr++) = (intptr)ctxPtr->R10;
*(curPtr++) = (intptr)ctx.R11; *(curPtr++) = (intptr)ctxPtr->R11;
*(curPtr++) = (intptr)ctx.R12; *(curPtr++) = (intptr)ctxPtr->R12;
*(curPtr++) = (intptr)ctx.R13; *(curPtr++) = (intptr)ctxPtr->R13;
*(curPtr++) = (intptr)ctx.R14; *(curPtr++) = (intptr)ctxPtr->R14;
*(curPtr++) = (intptr)ctx.R15; *(curPtr++) = (intptr)ctxPtr->R15;
memcpy(curPtr, &ctxPtr->Xmm0, 16 * 16);
curPtr += (16 * 16) / sizeof(intptr);
#endif #endif
* inOutIntRegCount = (int)(curPtr - outIntRegs); * inOutIntRegCount = (int)(curPtr - outIntRegs);