From c66551bfbafb8167d44d49a1ba08aef1072f53ad Mon Sep 17 00:00:00 2001
From: Florian Schmaus <flow@cs.fau.de>
Date: Fri, 3 Jun 2022 15:43:06 +0200
Subject: [PATCH] [Context] Use 'jmp' instead of 'ret' to kickoff the context

---
 emper/Context.cpp     | 11 +++++------
 emper/ContextAsm.nasm |  5 ++---
 2 files changed, 7 insertions(+), 9 deletions(-)

diff --git a/emper/Context.cpp b/emper/Context.cpp
index 2c8ac30a..9b36f9a6 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 2ebc68f9..e930b2f5 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]
-- 
GitLab