From adacb8daa6460190fdaf08404d0965ddd59e4ba5 Mon Sep 17 00:00:00 2001 From: DH Date: Tue, 18 Jul 2023 03:26:33 +0300 Subject: [PATCH] [rpcsx-os] Use xbyak to set context --- .gitmodules | 3 +++ 3rdparty/xbyak | 1 + rpcsx-os/thread.cpp | 33 ++++++++++++++++++++++----------- 3 files changed, 26 insertions(+), 11 deletions(-) create mode 100644 .gitmodules create mode 160000 3rdparty/xbyak diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 000000000..f09d27cf7 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "3rdparty/xbyak"] + path = 3rdparty/xbyak + url = git@github.com:herumi/xbyak.git diff --git a/3rdparty/xbyak b/3rdparty/xbyak new file mode 160000 index 000000000..ce083a0dc --- /dev/null +++ b/3rdparty/xbyak @@ -0,0 +1 @@ +Subproject commit ce083a0dcc306c1717685a81f577a4e050193919 diff --git a/rpcsx-os/thread.cpp b/rpcsx-os/thread.cpp index 9142e85aa..5f85c0ee0 100644 --- a/rpcsx-os/thread.cpp +++ b/rpcsx-os/thread.cpp @@ -10,9 +10,30 @@ #include #include #include +#include thread_local orbis::Thread *rx::thread::g_current = nullptr; +static auto setContext = [] { + struct SetContext : Xbyak::CodeGenerator { + SetContext() { + mov(rbp, rdi); + mov(rax, qword[rbp + REG_RAX * sizeof(unsigned long long)]); + mov(rdi, qword[rbp + REG_RDI * sizeof(unsigned long long)]); + mov(rdx, qword[rbp + REG_RDX * sizeof(unsigned long long)]); + mov(rcx, qword[rbp + REG_RCX * sizeof(unsigned long long)]); + mov(rbx, qword[rbp + REG_RBX * sizeof(unsigned long long)]); + mov(rsi, qword[rbp + REG_RSI * sizeof(unsigned long long)]); + mov(rsp, qword[rbp + REG_RSP * sizeof(unsigned long long)]); + + mov(rbp, qword[rbp + REG_RIP * sizeof(unsigned long long)]); + call(rbp); + } + } static setContextStorage; + + return setContextStorage.getCode(); +}(); + static __attribute__((no_stack_protector)) void handleSigSys(int sig, siginfo_t *info, void *ucontext) { if (auto hostFs = _readgsbase_u64()) { @@ -63,16 +84,6 @@ void rx::thread::invoke(orbis::Thread *thread) { _writefsbase_u64(thread->fsBase); auto context = reinterpret_cast(thread->context); - asm volatile("movq %1, %%rsp\n" - "callq *%0\n" - : - : "rm"(context->uc_mcontext.gregs[REG_RIP]), - "rm"(context->uc_mcontext.gregs[REG_RSP]), - "D"(context->uc_mcontext.gregs[REG_RDI]), - "S"(context->uc_mcontext.gregs[REG_RSI]), - "d"(context->uc_mcontext.gregs[REG_RDX]), - "c"(context->uc_mcontext.gregs[REG_RCX]), - "b"(context->uc_mcontext.gregs[REG_RBX]) - : "memory"); + setContext(context->uc_mcontext); _writefsbase_u64(hostFs); }