| 1 |
/* |
| 2 |
* Copyright 2022-2025 André Zwing |
| 3 |
* Copyright 2023 Alexandre Julliard |
| 4 |
*/ |
| 5 |
#include <stddef.h> |
| 6 |
#include <stdio.h> |
| 7 |
#include <windows.h> |
| 8 |
#include <ntstatus.h> |
| 9 |
#include <winternl.h> |
| 10 |
#include <winnt.h> |
| 11 |
|
| 12 |
#include "debug.h" |
| 13 |
#include "os.h" |
| 14 |
#include "custommem.h" |
| 15 |
#include "dynablock.h" |
| 16 |
#include "env.h" |
| 17 |
#include "emu/x64emu_private.h" |
| 18 |
#include "emu/x87emu_private.h" |
| 19 |
#include "x64trace.h" |
| 20 |
#include "box64context.h" |
| 21 |
#include "box64cpu.h" |
| 22 |
#include "box64cpu_util.h" |
| 23 |
#include "build_info.h" |
| 24 |
#include "rbtree.h" |
| 25 |
#include "wine/compiler.h" |
| 26 |
#include "wine/debug.h" |
| 27 |
#include "hostext.h" |
| 28 |
#include "sysinfo.h" |
| 29 |
|
| 30 |
uintptr_t box64_pagesize = 4096; |
| 31 |
|
| 32 |
uint32_t default_gs = 0x2b; |
| 33 |
uint32_t default_fs = 0; |
| 34 |
|
| 35 |
int box64_rdtsc = 0; |
| 36 |
uint8_t box64_rdtsc_shift = 0; |
| 37 |
int box64_is32bits = 0; |
| 38 |
int box64_unittest_mode = 0; |
| 39 |
int box64_wine = 0; // this is for the emulated x86 Wine. |
| 40 |
sysinfo_t box64_sysinfo = {0}; |
| 41 |
|
| 42 |
static uint32_t x86emu_parity_tab[8] = { |
| 43 |
0x96696996, |
| 44 |
0x69969669, |
| 45 |
0x69969669, |
| 46 |
0x96696996, |
| 47 |
0x69969669, |
| 48 |
0x96696996, |
| 49 |
0x96696996, |
| 50 |
0x69969669, |
| 51 |
}; |
| 52 |
|
| 53 |
static UINT16 DECLSPEC_ALIGN(4096) bopcode[4096 / sizeof(UINT16)]; |
| 54 |
static UINT16 DECLSPEC_ALIGN(4096) unxcode[4096 / sizeof(UINT16)]; |
| 55 |
|
| 56 |
typedef UINT64 unixlib_handle_t; |
| 57 |
NTSTATUS(WINAPI* __wine_unix_call_dispatcher)(unixlib_handle_t, unsigned int, void*); |
| 58 |
|
| 59 |
#define ROUND_ADDR(addr, mask) ((void*)((UINT_PTR)(addr) & ~(UINT_PTR)(mask))) |
| 60 |
#define ROUND_SIZE(addr, size) (((SIZE_T)(size) + ((UINT_PTR)(addr) & page_mask) + page_mask) & ~page_mask) |
| 61 |
static const UINT_PTR page_mask = 0xfff; |
| 62 |
|
| 63 |
/* reserved TEB64 TLS slots for Wow64 |
| 64 |
#define WOW64_TLS_CPURESERVED 1 |
| 65 |
#define WOW64_TLS_TEMPLIST 3 |
| 66 |
#define WOW64_TLS_USERCALLBACKDATA 5 |
| 67 |
#define WOW64_TLS_APCLIST 7 |
| 68 |
#define WOW64_TLS_FILESYSREDIR 8 |
| 69 |
#define WOW64_TLS_WOW64INFO 10 |
| 70 |
#define WOW64_TLS_MAX_NUMBER 19 |
| 71 |
*/ |
| 72 |
#define WOW64_TLS_ENTRY_CONTEXT (WOW64_TLS_MAX_NUMBER - 1) |
| 73 |
#define WOW64_TLS_EMU (WOW64_TLS_MAX_NUMBER - 2) |
| 74 |
|
| 75 |
int is_addr_unaligned(uintptr_t addr) |
| 76 |
{ |
| 77 |
// FIXME |
| 78 |
return 0; |
| 79 |
} |
| 80 |
|
| 81 |
int is_addr_autosmc(uintptr_t addr) |
| 82 |
{ |
| 83 |
// FIXME |
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
#ifdef DYNAREC |
| 88 |
int nUnalignedRange(uintptr_t start, size_t size) |
| 89 |
{ |
| 90 |
// FIXME |
| 91 |
return 0; |
| 92 |
} |
| 93 |
void getUnalignedRange(uintptr_t start, size_t size, uintptr_t addrs[]) |
| 94 |
{ |
| 95 |
//FIXME |
| 96 |
} |
| 97 |
#endif |
| 98 |
|
| 99 |
typedef void (*wrapper_t)(x64emu_t* emu, uintptr_t fnc); |
| 100 |
int isSimpleWrapper(wrapper_t fun) |
| 101 |
{ |
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
int isRetX87Wrapper(wrapper_t fun) |
| 106 |
{ |
| 107 |
return 0; |
| 108 |
} |
| 109 |
|
| 110 |
cpu_ext_t cpuext = {0}; |
| 111 |
|
| 112 |
static box64context_t box64_context; |
| 113 |
box64context_t* my_context = &box64_context; |
| 114 |
|
| 115 |
|
| 116 |
void fpu_to_box(WOW64_CONTEXT* ctx, x64emu_t* emu) |
| 117 |
{ |
| 118 |
XMM_SAVE_AREA32* fpu = (XMM_SAVE_AREA32*)ctx->ExtendedRegisters; |
| 119 |
|
| 120 |
emu->mxcsr.x32 = fpu->MxCsr; |
| 121 |
emu->cw.x16 = fpu->ControlWord; |
| 122 |
emu->sw.x16 = fpu->StatusWord; |
| 123 |
|
| 124 |
LD2D(&ctx->FloatSave.RegisterArea[0], &emu->x87[0]); |
| 125 |
LD2D(&ctx->FloatSave.RegisterArea[10], &emu->x87[1]); |
| 126 |
LD2D(&ctx->FloatSave.RegisterArea[20], &emu->x87[2]); |
| 127 |
LD2D(&ctx->FloatSave.RegisterArea[30], &emu->x87[3]); |
| 128 |
LD2D(&ctx->FloatSave.RegisterArea[40], &emu->x87[4]); |
| 129 |
LD2D(&ctx->FloatSave.RegisterArea[50], &emu->x87[5]); |
| 130 |
LD2D(&ctx->FloatSave.RegisterArea[60], &emu->x87[6]); |
| 131 |
LD2D(&ctx->FloatSave.RegisterArea[70], &emu->x87[7]); |
| 132 |
memcpy(emu->xmm, fpu->XmmRegisters, sizeof(emu->xmm)); |
| 133 |
} |
| 134 |
|
| 135 |
void box_to_fpu(WOW64_CONTEXT* ctx, x64emu_t* emu) |
| 136 |
{ |
| 137 |
XMM_SAVE_AREA32* fpu = (XMM_SAVE_AREA32*)ctx->ExtendedRegisters; |
| 138 |
|
| 139 |
fpu->MxCsr = emu->mxcsr.x32; |
| 140 |
fpu->ControlWord = emu->cw.x16; |
| 141 |
fpu->StatusWord = emu->sw.x16; |
| 142 |
|
| 143 |
D2LD(&emu->x87[0], &ctx->FloatSave.RegisterArea[0]); |
| 144 |
D2LD(&emu->x87[1], &ctx->FloatSave.RegisterArea[10]); |
| 145 |
D2LD(&emu->x87[2], &ctx->FloatSave.RegisterArea[20]); |
| 146 |
D2LD(&emu->x87[3], &ctx->FloatSave.RegisterArea[30]); |
| 147 |
D2LD(&emu->x87[4], &ctx->FloatSave.RegisterArea[40]); |
| 148 |
D2LD(&emu->x87[5], &ctx->FloatSave.RegisterArea[50]); |
| 149 |
D2LD(&emu->x87[6], &ctx->FloatSave.RegisterArea[60]); |
| 150 |
D2LD(&emu->x87[7], &ctx->FloatSave.RegisterArea[70]); |
| 151 |
memcpy(fpu->XmmRegisters, emu->xmm, sizeof(emu->xmm)); |
| 152 |
} |
| 153 |
|
| 154 |
static NTSTATUS invalidate_mapped_section(PVOID addr) |
| 155 |
{ |
| 156 |
MEMORY_BASIC_INFORMATION mem_info; |
| 157 |
SIZE_T size; |
| 158 |
void* base; |
| 159 |
|
| 160 |
NTSTATUS ret = NtQueryVirtualMemory(NtCurrentProcess(), addr, MemoryBasicInformation, &mem_info, sizeof(mem_info), NULL); |
| 161 |
|
| 162 |
if (!NT_SUCCESS(ret)) |
| 163 |
return ret; |
| 164 |
|
| 165 |
base = mem_info.AllocationBase; |
| 166 |
size = (char*)mem_info.BaseAddress + mem_info.RegionSize - (char*)base; |
| 167 |
|
| 168 |
while (!NtQueryVirtualMemory(NtCurrentProcess(), (char*)base + size, MemoryBasicInformation, &mem_info, sizeof(mem_info), NULL) && mem_info.AllocationBase == base) { |
| 169 |
size += mem_info.RegionSize; |
| 170 |
} |
| 171 |
|
| 172 |
unprotectDB((uintptr_t)base, (DWORD64)size, 1); |
| 173 |
return STATUS_SUCCESS; |
| 174 |
} |
| 175 |
|
| 176 |
void WINAPI BTCpuFlushInstructionCache2(LPCVOID addr, SIZE_T size) |
| 177 |
{ |
| 178 |
printf_log(LOG_DEBUG, "BTCpuFlushInstructionCache2(%p, %zu)\n", addr, size); |
| 179 |
unprotectDB((uintptr_t)addr, (size_t)size, 1); |
| 180 |
} |
| 181 |
|
| 182 |
void WINAPI BTCpuFlushInstructionCacheHeavy(LPCVOID addr, SIZE_T size) |
| 183 |
{ |
| 184 |
printf_log(LOG_DEBUG, "BTCpuFlushInstructionCacheHeavy(%p, %zu)\n", addr, size); |
| 185 |
unprotectDB((uintptr_t)addr, (size_t)size, 1); |
| 186 |
} |
| 187 |
|
| 188 |
void* WINAPI BTCpuGetBopCode(void) |
| 189 |
{ |
| 190 |
printf_log(LOG_DEBUG, "BTCpuGetBopCode()\n"); |
| 191 |
return (UINT32*)&bopcode; |
| 192 |
} |
| 193 |
|
| 194 |
void* WINAPI __wine_get_unix_opcode(void) |
| 195 |
{ |
| 196 |
printf_log(LOG_DEBUG, "__wine_get_unix_opcode()\n"); |
| 197 |
return (UINT32*)&unxcode; |
| 198 |
} |
| 199 |
|
| 200 |
NTSTATUS WINAPI BTCpuGetContext(HANDLE thread, HANDLE process, void* unknown, WOW64_CONTEXT* ctx) |
| 201 |
{ |
| 202 |
printf_log(LOG_DEBUG, "BTCpuGetContext(%p, %p, %p, %p)\n", thread, process, unknown, ctx); |
| 203 |
return NtQueryInformationThread(thread, ThreadWow64Context, ctx, sizeof(*ctx), NULL); |
| 204 |
} |
| 205 |
|
| 206 |
void WINAPI BTCpuNotifyMemoryDirty(PVOID addr, SIZE_T size) |
| 207 |
{ |
| 208 |
printf_log(LOG_DEBUG, "BTCpuNotifyMemoryDirty(%p, %zu)\n", addr, size); |
| 209 |
unprotectDB((uintptr_t)addr, (size_t)size, 1); |
| 210 |
} |
| 211 |
|
| 212 |
void WINAPI BTCpuNotifyMemoryFree(PVOID addr, SIZE_T size, ULONG free_type) |
| 213 |
{ |
| 214 |
printf_log(LOG_DEBUG, "BTCpuNotifyMemoryFree(%p, %zu, %u)\n", addr, size, free_type); |
| 215 |
if (!size) |
| 216 |
invalidate_mapped_section(addr); |
| 217 |
else if (free_type & MEM_DECOMMIT) |
| 218 |
unprotectDB((uintptr_t)ROUND_ADDR(addr, page_mask), (DWORD64)ROUND_SIZE(addr, size), 1); |
| 219 |
} |
| 220 |
|
| 221 |
void WINAPI BTCpuNotifyMemoryProtect(PVOID addr, SIZE_T size, DWORD new_protect) |
| 222 |
{ |
| 223 |
printf_log(LOG_DEBUG, "BTCpuNotifyMemoryProtect(%p, %zu, %08x)\n", addr, size, new_protect); |
| 224 |
if (!(new_protect & (PAGE_EXECUTE | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE))) |
| 225 |
return; |
| 226 |
unprotectDB((uintptr_t)addr, size, 1); |
| 227 |
} |
| 228 |
|
| 229 |
void WINAPI BTCpuNotifyUnmapViewOfSection(PVOID addr, ULONG flags) |
| 230 |
{ |
| 231 |
printf_log(LOG_DEBUG, "BTCpuNotifyUnmapViewOfSection(%p, %u)\n", addr, flags); |
| 232 |
invalidate_mapped_section(addr); |
| 233 |
} |
| 234 |
|
| 235 |
NTSTATUS WINAPI BTCpuProcessInit(void) |
| 236 |
{ |
| 237 |
printf_log(LOG_DEBUG, "BTCpuProcessInit()\n"); |
| 238 |
HMODULE module; |
| 239 |
UNICODE_STRING str; |
| 240 |
void** p__wine_unix_call_dispatcher; |
| 241 |
|
| 242 |
#define STATIC_ASSERT(COND, MSG) typedef char static_assertion_##MSG[(!!(COND)) * 2 - 1] |
| 243 |
/* otherwise adjust arm64_epilog.S and arm64_next.S */ |
| 244 |
STATIC_ASSERT(offsetof(x64emu_t, win64_teb) == 3104, offset_of_b_must_be_4); |
| 245 |
#undef STATIC_ASSERT |
| 246 |
|
| 247 |
LoadEnvVariables(); |
| 248 |
InitializeEnvFiles(); |
| 249 |
InitializeSystemInfo(); |
| 250 |
|
| 251 |
if (!BOX64ENV(nobanner)) PrintBox64Version(1); |
| 252 |
if (DetectHostCpuFeatures()) |
| 253 |
PrintHostCpuFeatures(); |
| 254 |
else { |
| 255 |
printf_log(LOG_INFO, "Minimum CPU requirements not met, disabling DynaRec\n"); |
| 256 |
SET_BOX64ENV(dynarec, 0); |
| 257 |
} |
| 258 |
|
| 259 |
TCHAR filename[MAX_PATH]; |
| 260 |
if (GetModuleFileNameA(NULL, filename, MAX_PATH)) { |
| 261 |
char* shortname = strrchr(filename, '\\'); |
| 262 |
if (shortname) { |
| 263 |
shortname++; |
| 264 |
ApplyEnvFileEntry(shortname); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
PrintEnvVariables(&box64env, LOG_INFO); |
| 269 |
|
| 270 |
memset(bopcode, 0xc3, sizeof(bopcode)); |
| 271 |
memset(unxcode, 0xc3, sizeof(unxcode)); |
| 272 |
bopcode[0] = 0x2ecd; |
| 273 |
unxcode[0] = 0x2ecd; |
| 274 |
|
| 275 |
init_custommem_helper(&box64_context); |
| 276 |
box64_context.db_sizes = rbtree_init("db_sizes"); |
| 277 |
|
| 278 |
if ((ULONG_PTR)bopcode >> 32 || (ULONG_PTR)unxcode >> 32) { |
| 279 |
printf_log(LOG_NONE, "box64cpu loaded above 4G, disabling\n"); |
| 280 |
return STATUS_INVALID_ADDRESS; |
| 281 |
} |
| 282 |
|
| 283 |
RtlInitUnicodeString(&str, L"ntdll.dll"); |
| 284 |
LdrGetDllHandle(NULL, 0, &str, &module); |
| 285 |
p__wine_unix_call_dispatcher = RtlFindExportedRoutineByName(module, "__wine_unix_call_dispatcher"); |
| 286 |
__wine_unix_call_dispatcher = *p__wine_unix_call_dispatcher; |
| 287 |
|
| 288 |
RtlInitializeCriticalSection(&box64_context.mutex_dyndump); |
| 289 |
RtlInitializeCriticalSection(&box64_context.mutex_trace); |
| 290 |
RtlInitializeCriticalSection(&box64_context.mutex_tls); |
| 291 |
RtlInitializeCriticalSection(&box64_context.mutex_thread); |
| 292 |
RtlInitializeCriticalSection(&box64_context.mutex_bridge); |
| 293 |
RtlInitializeCriticalSection(&box64_context.mutex_lock); |
| 294 |
|
| 295 |
InitX64Trace(&box64_context); |
| 296 |
|
| 297 |
return STATUS_SUCCESS; |
| 298 |
} |
| 299 |
|
| 300 |
static uint8_t box64_is_addr_in_jit(void* addr) |
| 301 |
{ |
| 302 |
if (!addr) |
| 303 |
return FALSE; |
| 304 |
return !!FindDynablockFromNativeAddress(addr); |
| 305 |
} |
| 306 |
|
| 307 |
NTSTATUS WINAPI BTCpuResetToConsistentState(EXCEPTION_POINTERS* ptrs) |
| 308 |
{ |
| 309 |
printf_log(LOG_DEBUG, "BTCpuResetToConsistentState(%p)\n", ptrs); |
| 310 |
x64emu_t* emu = NtCurrentTeb()->TlsSlots[WOW64_TLS_EMU]; |
| 311 |
EXCEPTION_RECORD* rec = ptrs->ExceptionRecord; |
| 312 |
CONTEXT* ctx = ptrs->ContextRecord; |
| 313 |
|
| 314 |
if (rec->ExceptionCode == EXCEPTION_ACCESS_VIOLATION) { |
| 315 |
dynablock_t* db = NULL; |
| 316 |
void* addr = NULL; |
| 317 |
uint32_t prot; |
| 318 |
|
| 319 |
if (rec->NumberParameters == 2 && rec->ExceptionInformation[0] == 1) |
| 320 |
addr = ULongToPtr(rec->ExceptionInformation[1]); |
| 321 |
|
| 322 |
if (addr) { |
| 323 |
unprotectDB((uintptr_t)addr, 1, 1); // unprotect 1 byte... But then, the whole page will be unprotected |
| 324 |
NtContinue(ctx, FALSE); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
if (!box64_is_addr_in_jit(ULongToPtr(ctx->Pc))) |
| 329 |
return STATUS_SUCCESS; |
| 330 |
|
| 331 |
/* Replace the host context with one captured before JIT entry so host code can unwind */ |
| 332 |
memcpy(ctx, NtCurrentTeb()->TlsSlots[WOW64_TLS_ENTRY_CONTEXT], sizeof(*ctx)); |
| 333 |
return STATUS_SUCCESS; |
| 334 |
} |
| 335 |
|
| 336 |
NTSTATUS WINAPI BTCpuSetContext(HANDLE thread, HANDLE process, void* unknown, WOW64_CONTEXT* ctx) |
| 337 |
{ |
| 338 |
printf_log(LOG_DEBUG, "BTCpuSetContext(%p, %p, %p, %p)\n", thread, process, unknown, ctx); |
| 339 |
return NtSetInformationThread(thread, ThreadWow64Context, ctx, sizeof(*ctx)); |
| 340 |
} |
| 341 |
|
| 342 |
void WINAPI BTCpuSimulate(void) |
| 343 |
{ |
| 344 |
printf_log(LOG_DEBUG, "BTCpuSimulate()\n"); |
| 345 |
WOW64_CPURESERVED* cpu = NtCurrentTeb()->TlsSlots[WOW64_TLS_CPURESERVED]; |
| 346 |
x64emu_t* emu = NtCurrentTeb()->TlsSlots[WOW64_TLS_EMU]; |
| 347 |
WOW64_CONTEXT* ctx = (WOW64_CONTEXT*)(cpu + 1); |
| 348 |
CONTEXT entry_context; |
| 349 |
|
| 350 |
RtlCaptureContext(&entry_context); |
| 351 |
NtCurrentTeb()->TlsSlots[WOW64_TLS_ENTRY_CONTEXT] = &entry_context; |
| 352 |
|
| 353 |
R_EAX = ctx->Eax; |
| 354 |
R_EBX = ctx->Ebx; |
| 355 |
R_ECX = ctx->Ecx; |
| 356 |
R_EDX = ctx->Edx; |
| 357 |
R_ESI = ctx->Esi; |
| 358 |
R_EDI = ctx->Edi; |
| 359 |
R_EBP = ctx->Ebp; |
| 360 |
R_RIP = ctx->Eip; |
| 361 |
R_ESP = ctx->Esp; |
| 362 |
R_CS = ctx->SegCs & 0xffff; |
| 363 |
R_DS = ctx->SegDs & 0xffff; |
| 364 |
R_ES = ctx->SegEs & 0xffff; |
| 365 |
R_FS = ctx->SegFs & 0xffff; |
| 366 |
R_GS = ctx->SegGs & 0xffff; |
| 367 |
R_SS = ctx->SegSs & 0xffff; |
| 368 |
emu->eflags.x64 = ctx->EFlags; |
| 369 |
emu->segs_offs[_FS] = calculate_fs(); |
| 370 |
emu->win64_teb = (uint64_t)NtCurrentTeb(); |
| 371 |
|
| 372 |
fpu_to_box(ctx, emu); |
| 373 |
|
| 374 |
if (box64env.dynarec) |
| 375 |
DynaRun(emu); |
| 376 |
else |
| 377 |
Run(emu, 0); |
| 378 |
} |
| 379 |
|
| 380 |
NTSTATUS WINAPI BTCpuThreadInit(void) |
| 381 |
{ |
| 382 |
printf_log(LOG_DEBUG, "BTCpuThreadInit()\n"); |
| 383 |
WOW64_CONTEXT* ctx; |
| 384 |
x64emu_t* emu = RtlAllocateHeap(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*emu)); |
| 385 |
|
| 386 |
RtlWow64GetCurrentCpuArea(NULL, (void**)&ctx, NULL); |
| 387 |
emu->context = &box64_context; |
| 388 |
|
| 389 |
// setup cpu helpers |
| 390 |
for (int i = 0; i < 16; ++i) |
| 391 |
emu->sbiidx[i] = &emu->regs[i]; |
| 392 |
emu->sbiidx[4] = &emu->zero; |
| 393 |
emu->x64emu_parity_tab = x86emu_parity_tab; |
| 394 |
|
| 395 |
reset_fpu(emu); |
| 396 |
|
| 397 |
NtCurrentTeb()->TlsSlots[WOW64_TLS_EMU] = emu; |
| 398 |
return STATUS_SUCCESS; |
| 399 |
} |
| 400 |
|
| 401 |
void EmitInterruptionImpl(x64emu_t* emu, int code) |
| 402 |
{ |
| 403 |
if (code == 0x2e /* NT syscall */) { |
| 404 |
WOW64_CPURESERVED* cpu = NtCurrentTeb()->TlsSlots[WOW64_TLS_CPURESERVED]; |
| 405 |
WOW64_CONTEXT* ctx = (WOW64_CONTEXT*)(cpu + 1); |
| 406 |
int id = R_EAX; |
| 407 |
BOOL is_unix_call = FALSE; |
| 408 |
|
| 409 |
if (ULongToPtr(R_RIP) == &unxcode) |
| 410 |
is_unix_call = TRUE; |
| 411 |
else if (ULongToPtr(R_RIP) != &bopcode) |
| 412 |
return; |
| 413 |
|
| 414 |
R_RIP = Pop32(emu); |
| 415 |
ctx->Eip = R_RIP; |
| 416 |
ctx->Esp = R_ESP; |
| 417 |
ctx->Ebx = R_EBX; |
| 418 |
ctx->Esi = R_ESI; |
| 419 |
ctx->Edi = R_EDI; |
| 420 |
ctx->Ebp = R_EBP; |
| 421 |
ctx->EFlags = emu->eflags.x64; |
| 422 |
cpu->Flags = 0; |
| 423 |
|
| 424 |
box_to_fpu(ctx, emu); |
| 425 |
|
| 426 |
if (is_unix_call) { |
| 427 |
uintptr_t handle_low = Pop32(emu); |
| 428 |
uintptr_t handle_high = Pop32(emu); |
| 429 |
unsigned int code = Pop32(emu); |
| 430 |
uintptr_t args = Pop32(emu); |
| 431 |
|
| 432 |
ctx->Esp = R_ESP; |
| 433 |
R_EAX = __wine_unix_call_dispatcher(handle_low | (handle_high << 32), code, (void*)args); |
| 434 |
} else { |
| 435 |
R_EAX = Wow64SystemServiceEx(id, ULongToPtr(ctx->Esp + 4)); |
| 436 |
} |
| 437 |
|
| 438 |
fpu_to_box(ctx, emu); |
| 439 |
|
| 440 |
R_EBX = ctx->Ebx; |
| 441 |
R_ESI = ctx->Esi; |
| 442 |
R_EDI = ctx->Edi; |
| 443 |
R_EBP = ctx->Ebp; |
| 444 |
R_ESP = ctx->Esp; |
| 445 |
R_RIP = ctx->Eip; |
| 446 |
if (cpu->Flags & WOW64_CPURESERVED_FLAG_RESET_STATE) { |
| 447 |
cpu->Flags &= ~WOW64_CPURESERVED_FLAG_RESET_STATE; |
| 448 |
//R_EAX = ctx->Eax; // do not clobber return value |
| 449 |
R_ECX = ctx->Ecx; |
| 450 |
R_EDX = ctx->Edx; |
| 451 |
R_FS = ctx->SegFs & 0xffff; |
| 452 |
emu->segs_offs[_FS] = calculate_fs(); |
| 453 |
emu->eflags.x64 = ctx->EFlags; |
| 454 |
} |
| 455 |
} else { |
| 456 |
RtlRaiseStatus(STATUS_ACCESS_VIOLATION); |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
/* Calls a 2-argument function `Func` setting the parent unwind frame information to the given SP and PC */ |
| 461 |
static void __attribute__((naked)) SEHFrameTrampoline2Args(void* Arg0, int Arg1, void* Func, uint64_t Sp, uint64_t Pc) |
| 462 |
{ |
| 463 |
asm(".seh_proc SEHFrameTrampoline2Args\n\t" |
| 464 |
"stp x3, x4, [sp, #-0x10]!\n\t" |
| 465 |
".seh_pushframe\n\t" |
| 466 |
"stp x29, x30, [sp, #-0x10]!\n\t" |
| 467 |
".seh_save_fplr_x 16\n\t" |
| 468 |
".seh_endprologue\n\t" |
| 469 |
"blr x2\n\t" |
| 470 |
"ldp x29, x30, [sp], 0x20\n\t" |
| 471 |
"ret\n\t" |
| 472 |
".seh_endproc"); |
| 473 |
} |
| 474 |
|
| 475 |
void EmitInterruption(x64emu_t* emu, int num, void* addr) |
| 476 |
{ |
| 477 |
CONTEXT* entry_context = NtCurrentTeb()->TlsSlots[WOW64_TLS_ENTRY_CONTEXT]; |
| 478 |
SEHFrameTrampoline2Args(emu, num, (void*)EmitInterruptionImpl, entry_context->Sp, entry_context->Pc); |
| 479 |
NtCurrentTeb()->TlsSlots[WOW64_TLS_ENTRY_CONTEXT] = entry_context; |
| 480 |
} |
| 481 |
|
| 482 |
NTSTATUS WINAPI LdrDisableThreadCalloutsForDll(HMODULE); |
| 483 |
|
| 484 |
BOOL WINAPI DllMainCRTStartup(HINSTANCE inst, DWORD reason, void* reserved) |
| 485 |
{ |
| 486 |
if (reason == DLL_PROCESS_ATTACH) LdrDisableThreadCalloutsForDll(inst); |
| 487 |
return TRUE; |
| 488 |
} |