From 56c3a949af87a6179eee655c19c89144604d17a2 Mon Sep 17 00:00:00 2001
From: Dario Nieuwenhuis <dirbaio@dirbaio.net>
Date: Tue, 16 May 2023 01:42:35 +0200
Subject: [PATCH] rp/multicore: ensure stack is 8-byte aligned.

---
 embassy-rp/src/multicore.rs | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/embassy-rp/src/multicore.rs b/embassy-rp/src/multicore.rs
index 9a445c26e..bbc775105 100644
--- a/embassy-rp/src/multicore.rs
+++ b/embassy-rp/src/multicore.rs
@@ -153,7 +153,12 @@ where
         psm.frce_off().modify(|w| w.set_proc1(false));
     }
 
-    let mem = unsafe { core::slice::from_raw_parts_mut(stack.mem.as_mut_ptr() as *mut usize, stack.mem.len() / 4) };
+    // The ARM AAPCS ABI requires 8-byte stack alignment.
+    // #[align] on `struct Stack` ensures the bottom is aligned, but the top could still be
+    // unaligned if the user chooses a stack size that's not multiple of 8.
+    // So, we round down to the next multiple of 8.
+    let stack_words = stack.mem.len() / 8 * 2;
+    let mem = unsafe { core::slice::from_raw_parts_mut(stack.mem.as_mut_ptr() as *mut usize, stack_words) };
 
     // Set up the stack
     let mut stack_ptr = unsafe { mem.as_mut_ptr().add(mem.len()) };