client/runtime/native32.h (3795B)
1 /* 2 * native32: a 32-bit host for lifted 32-bit Windows code (recomp32 codegen). 3 * 4 * The host process is 32-bit and maps the guest image 1:1 at its own base, so 5 * a Win32 struct the guest builds is already the struct Windows expects, and 6 * a pointer the guest holds is a pointer the host can pass on. That removes 7 * the import-shim layer a 64-bit host needs: 8 * 9 * guest -> Windows native_bridge(): copy the guest's argument slots to the 10 * host stack, call the real function, and measure how far 11 * the callee moved esp. That distance is the purge, so no 12 * argc table is needed: stdcall, cdecl, thiscall and COM 13 * methods all come out right. An x87 result in st(0) is 14 * moved onto the lifted FPU stack. 15 * Windows -> guest The guest's code sections are mapped non-executable. 16 * When Windows calls a guest WndProc, thread start or 17 * hook, the fetch faults, native32_veh() moves eip to a 18 * trampoline, and the lifted body runs on the guest stack. 19 * threads One machine lock: a thread owns the register globals 20 * while it runs lifted code and hands them back around 21 * every native call. Each guest thread gets its own guest 22 * stack and TIB on first entry. 23 * 24 * Extracted from gunman (Gunman Chronicles), where it runs the whole game; 25 * The Movies is the second user. MSVC x86 only: the bridge is inline asm. 26 * 27 * A host provides: main(), recomp_lookup_manual(), and the generated dispatch 28 * table. It calls native32_init(), guest_map() per module, native32_bind() 29 * per module, then native32_call_guest(entry). 30 */ 31 #ifndef NATIVE32_H 32 #define NATIVE32_H 33 #include <stdint.h> 34 #include "recomp_types.h" 35 #include "guest_memory.h" 36 37 /* Optional hand-written import bodies, matched by name at bind time. A shim 38 * runs inside the lifted model (reads args from g_esp, pops them itself). */ 39 typedef struct { const char* name; recomp_func_t fn; uint32_t va; } native32_shim_t; 40 41 /* Call once, before anything else. */ 42 void native32_init(void); 43 44 /* Fill a mapped module's IAT the way the Windows loader would: shims first 45 * (by name), then LoadLibrary + GetProcAddress. Returns unresolved count. */ 46 int native32_bind(uint32_t base, native32_shim_t* shims, int nshims); 47 48 /* A mapped guest module by file name ("Golf.exe", or a path ending in one; 49 * case-insensitive), and one of its exports by name or ordinal; 0 if none. 50 * native32_bind uses both, so a guest DLL's imports from a guest EXE land on 51 * lifted code. A host uses them to shim LoadLibraryA / GetProcAddress for its 52 * guest DLLs. Map every guest module before binding one that imports from it, 53 * and pass every native32_bind call the same shim array. */ 54 uint32_t native32_module(const char* name); 55 uint32_t native32_export(uint32_t base, const char* name); 56 57 /* Run lifted code at va to completion on this thread's guest stack. */ 58 void native32_call_guest(uint32_t va, int nargs, const uint32_t* args); 59 60 /* The machine lock. native32_call_guest takes it; a host that touches the 61 * register globals from its own threads must too. Nests. */ 62 void mach_enter(void); 63 void mach_leave(void); 64 65 /* Name for a native address ("kernel32.dll!CreateFileA"), or NULL. */ 66 const char* native32_name(uint32_t va); 67 68 69 /* Diagnostics: set before running. */ 70 extern int native32_trace_native; /* one line per native call */ 71 extern int native32_trace_callbacks; /* one line per Windows -> guest call */ 72 73 /* Guest address ranges known to the runtime (for fault reports). */ 74 int native32_in_guest(uint32_t va); 75 76 /* Print the last indirect calls, newest first. */ 77 void native32_dump_icalls(int n); 78 79 #endif