about summary refs log tree commit diff stats
path: root/src/tools
diff options
context:
space:
mode:
authorptitSeb <sebastien.chev@gmail.com>2023-02-12 23:02:39 +0100
committerptitSeb <sebastien.chev@gmail.com>2023-02-12 23:02:39 +0100
commit6d6de880eb9faabbc83ff31b62dd91e5bb4a490b (patch)
treed2c5caa2c1b5cc8b169cce9972d9dae2b8c5cb08 /src/tools
parenta65bf79ad94babab7f132ed6b8674f9f490bc4a0 (diff)
downloadbox64-6d6de880eb9faabbc83ff31b62dd91e5bb4a490b.tar.gz
box64-6d6de880eb9faabbc83ff31b62dd91e5bb4a490b.zip
Also needed a RunFunctionWindows, following the Windows Calling Convention (and now d3datapter9 works)
Diffstat (limited to 'src/tools')
-rwxr-xr-xsrc/tools/callback.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/tools/callback.c b/src/tools/callback.c
index d3d5101e..9aafa6d8 100755
--- a/src/tools/callback.c
+++ b/src/tools/callback.c
@@ -163,3 +163,47 @@ uint64_t RunFunctionWithEmu(x64emu_t *emu, int QuitOnLongJump, uintptr_t fnc, in
 
     return R_RAX;
 }
+
+EXPORTDYN
+uint64_t RunFunctionWindows(box64context_t *context, uintptr_t fnc, int nargs, ...)
+{
+    (void)context;
+
+    x64emu_t *emu = thread_get_emu();
+    int align = (nargs>4)?(((nargs-4)&1)):0;
+    int stackn = align + ((nargs>4)?(nargs-4):0);
+
+    Push64(emu, R_RBP); // push rbp
+    R_RBP = R_RSP;      // mov rbp, rsp
+
+    R_RSP -= stackn*sizeof(void*);   // need to push in reverse order
+
+    uint64_t *p = (uint64_t*)R_RSP;
+
+    va_list va;
+    va_start (va, nargs);
+    for (int i=0; i<nargs; ++i) {
+        if(i<4) {
+            int nn[] = {_CX, _DX, _R8, _R9};
+            emu->regs[nn[i]].q[0] = va_arg(va, uint64_t);
+        } else {
+            *p = va_arg(va, uint64_t);
+            p++;
+        }
+    }
+    va_end (va);
+
+    R_RSP -= 32;    // ShadowArea
+
+    uintptr_t oldip = R_RIP;
+    DynaCall(emu, fnc);
+
+    if(oldip==R_RIP) {
+        R_RSP = R_RBP;          // mov rsp, rbp
+        R_RBP = Pop64(emu);     // pop rbp
+    }
+
+    uint64_t ret = R_RAX;
+
+    return ret;
+}
\ No newline at end of file