From 52990e8404bd1a953076c436fe91f1decd5fbf07 Mon Sep 17 00:00:00 2001
From: Luis Gerhorst <privat@luisgerhorst.de>
Date: Fri, 19 Mar 2021 14:27:00 +0100
Subject: [PATCH] fix: backport "arm: core: aarch64: save switch_handle"

https://github.com/zephyrproject-rtos/zephyr/commit/6182330fc33d713fbea30ea428e283f795882b0e
---
 arch/arm/core/aarch64/switch.S | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

diff --git a/arch/arm/core/aarch64/switch.S b/arch/arm/core/aarch64/switch.S
index d61c37d63dc..c9497f0f385 100644
--- a/arch/arm/core/aarch64/switch.S
+++ b/arch/arm/core/aarch64/switch.S
@@ -32,18 +32,25 @@ GDATA(_kernel)
 GTEXT(z_arm64_context_switch)
 SECTION_FUNC(TEXT, z_arm64_context_switch)
 	/* addr of callee-saved regs in thread in x2 */
-	ldr	x2, =_thread_offset_to_callee_saved
-	add	x2, x2, x1
+	ldr	x3, =_thread_offset_to_callee_saved
+	add	x2, x1, x3
 
 	/* Save the current SP */
-	mov	x1, sp
+	mov	x4, sp
 
 	stp	x19, x20, [x2, ___callee_saved_t_x19_x20_OFFSET]
 	stp	x21, x22, [x2, ___callee_saved_t_x21_x22_OFFSET]
 	stp	x23, x24, [x2, ___callee_saved_t_x23_x24_OFFSET]
 	stp	x25, x26, [x2, ___callee_saved_t_x25_x26_OFFSET]
 	stp	x27, x28, [x2, ___callee_saved_t_x27_x28_OFFSET]
-	stp	x29, x1, [x2, ___callee_saved_t_x29_sp_OFFSET]
+	stp	x29, x4, [x2, ___callee_saved_t_x29_sp_OFFSET]
+
+#ifdef CONFIG_SMP
+	/* save old thread into switch handle which is required by
+	 * wait_for_switch
+	 */
+	 str	x1, [x1, #___thread_t_switch_handle_OFFSET]
+#endif
 
 #ifdef CONFIG_THREAD_LOCAL_STORAGE
 	/* Grab the TLS pointer */
@@ -139,12 +146,6 @@ context_switch:
 	/* Get old thread from x1 */
 	sub	x1, x1, ___thread_t_switch_handle_OFFSET
 
-#ifdef CONFIG_SMP
-	/* save old thread into switch handle which is required by
-	 * wait_for_switch
-	 */
-	 str	x1, [x1, #___thread_t_switch_handle_OFFSET]
-#endif
 	/* Switch thread */
 	bl	z_arm64_context_switch
 
-- 
GitLab