Skip to contentSkip to conversation

target-arm: fix ARMv7M stack alignment on reset

PATCH In review Sebastian Ottlik 2 participants 2 messages Sep 4, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target-arm: fix ARMv7M stack alignment on reset

When the initial SP is loaded from the vector table on ARMv7M systems the two least significant bits are ignored as the stack is always aligned at a four byte boundary (see ARM DDI 0403C, B1.4.1 and B1.5.5). So far QEMU did not ignore these bits leading to a stack alignment inconsitent with real hardware for binaries that rely on this behaviour. This patch fixes this issue by masking the two least significant bits when loading the SP.

---
 target-arm/cpu.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target-arm/cpu.c +1 -1
index b2556c6..e689fde 100644
@@ -108,7 +108,7 @@ static void arm_cpu_reset(CPUState *s)
                modified flash and reset itself.  However images
                loaded via -kernel have not been copied yet, so load the
                values directly from there.  */
-            env->regs[13] = ldl_p(rom);
+            env->regs[13] = ldl_p(rom) & 0xFFFFFFFC;
             pc = ldl_p(rom + 4);
             env->thumb = pc & 1;
             env->regs[15] = pc & ~1;
Signature · 1 line
1.7.9.5
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Sebastian Ottlik

When the initial SP is loaded from the vector table on ARMv7M systems the two least significant bits are ignored as the stack is always aligned at a four byte boundary (see ARM DDI 0403C, B1.4.1 and B1.5.5). So far QEMU did not ignore these bits leading to a stack alignment inconsitent with real hardware for binaries that rely on this behaviour. This patch fixes this issue by masking the two least significant bits when loading the SP.

Yes, the pseudocode in the v7M ARM ARM is clear that the low two bits are masked when loading SP from the vector table. Thanks for the patch, I'll apply it to my ARM tree.

PS: there is a newer version of the v7M ARMARM, DDI0403D. I'd strongly encourage you to get a copy of this rather than continuing to use the rev C as a reference (it's on the ARM infocenter; you'll need to register on the website if you haven't already but that's a simple 'fill in the form' process):
http://infocenter.arm.com/help/topic/com.arm.doc.ddi0403c/index.html
Rev D extensively clarifies and reorganizes the text, as well as documenting the FP extensions.

-- PMM