From 5094d3044c0c7ac1348fd95f21fd9ef14abd0e65 Mon Sep 17 00:00:00 2001 From: Stefanos Kornilios Mitsis Poiitidis Date: Mon, 2 Oct 2023 17:17:29 +0300 Subject: [PATCH] run_main_entry: Rewrite in asm for stack setup - Fixes OpenOrbis loading that depends on rsp pointing to the argument structure, in addition to rdi --- src/Core/PS4/Linker.cpp | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Core/PS4/Linker.cpp b/src/Core/PS4/Linker.cpp index a11e4638..e3b957f1 100644 --- a/src/Core/PS4/Linker.cpp +++ b/src/Core/PS4/Linker.cpp @@ -640,8 +640,28 @@ static PS4_SYSV_ABI void ProgramExitFunc() { printf("exit function called\n"); } -static PS4_SYSV_ABI void run_main_entry(u64 addr, EntryParams* params, exit_func_t exit_func) { - reinterpret_cast(addr)(params, exit_func); +static void run_main_entry(u64 addr, EntryParams* params, exit_func_t exit_func) { + //reinterpret_cast(addr)(params, exit_func); // can't be used, stack has to have a specific layout + + asm volatile ( + "andq $-16, %%rsp\n"// Align to 16 bytes + "subq $8, %%rsp\n" // videoout_basic expects the stack to be misaligned + + // Kernel also pushes some more things here during process init + // at least: environment, auxv, possibly other things + + "pushq 8(%1)\n" // copy EntryParams to top of stack like the kernel does + "pushq 0(%1)\n" // OpenOrbis expects to find it there + + "movq %1, %%rdi\n" // also pass params and exit func + "movq %2, %%rsi\n" // as before + + "jmp *%0\n" // can't use call here, as that would mangle the prepared stack. + // there's no coming back + : + : "r"(addr), "r"(params), "r"(exit_func) + : "rax", "rsi", "rdi", "rsp", "rbp" + ); } void Linker::Execute()