diff --git a/emper/Context.cpp b/emper/Context.cpp index 2c8ac30ae653662f68a85a5cca03d87489859010..9b36f9a6b2e11cdb4d05bf95df66b291a2760350 100644 --- a/emper/Context.cpp +++ b/emper/Context.cpp @@ -74,12 +74,11 @@ Context::Context(func_t mainFunction) setEmptyHook(); - // We write the real kickoff function's address at - // TOS - 2 so it's called after this context has been actived. - // This also ensures that the stack is correctly aligned for - // SSE operations, which require the stack to be 8-byte, but - // not 16-byte aligned. - alphaFunctionIpLocation = savedStackpointer = (uintptr_t*)tos - 2; + // TOS is 16-byte aligned, however we need the initial + // savedStackpointer to be 8-byte aligned because will later jmp to + // it. We also save a pointer to very first function this context is + // going to execute a this initial stackpointer value. + alphaFunctionIpLocation = savedStackpointer = (uintptr_t*)tos - 1; void** alphaSavedIp = reinterpret_cast<void**>(savedStackpointer); void (*f)() = &kickoff; diff --git a/emper/ContextAsm.nasm b/emper/ContextAsm.nasm index 2ebc68f9be0a7a9b0c273c5220a48a7c7b55f7b5..e930b2f55ba644dd108f2903931aaafbd2fba177 100644 --- a/emper/ContextAsm.nasm +++ b/emper/ContextAsm.nasm @@ -58,6 +58,5 @@ switch_context: ; which must point to a word which represent the memory address ; where we want to continue. mov rsp, [rdi] - ; Pop the memory address where to continue from the - ; stack. Which means: Continue at [rsp]. - ret + ; Jump to the address rsp currently points at. + jmp [rsp]