Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cortex-M: initial stack alignment #2976

Merged

Conversation

jnohlgard
Copy link
Member

The initial stack passed to the thread entry point was always anti-aligned to a 64 bit boundary (meaning the address is aligned to 32 bit, and never to 64 bits)

The cause is bit 9 in xPSR being set in the stacked xPSR when initializing the thread stack in thread_arch_stack_init.

I have changed the initialization for Cortex-M3 and Cortex-M4 to clear the faulty bit and also cleaned up the comments a bit and added extra handling for aligning the stack on a 64-bit boundary.

Procedure Call Standard for the ARM® Architecture requires that the stack pointer is aligned on a 8 byte boundary upon function entry. Clang relies on this behaviour which caused problems in auto_init_net_if on mulle, but I am guessing this is not the only location where this caused problems. GCC does not seem to assume 64 bit alignment, at least not with -Os and lower.

Joakim Gebart added 2 commits May 13, 2015 09:15
AAPCS requires stack pointers to be aligned on a double word boundary.

In addition, Clang-3.6 assumes the stack pointer is always aligned to
a 8 byte boundary upon function entry, at least in armv7-m, causing
hard-to-find errors in the compiled code.
AAPCS requires stack pointers to be aligned on a double word boundary.

In addition, Clang-3.6 assumes the stack pointer is always aligned to
a 8 byte boundary upon function entry, at least in armv7-m, causing
hard-to-find errors in the compiled code.

This is the same implementation as for the Cortex-M4
@jnohlgard jnohlgard added Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) Platform: ARM Platform: This PR/issue effects ARM-based platforms labels May 13, 2015
@jnohlgard
Copy link
Member Author

I have tested on CM4, but not CM3. Could someone test this on a CM3 board?

To verify:

  • run with debugger
  • set breakpoint at main_trampoline, idle_thread, (thread entry points)
  • continue until breakpoint, print $psp or info reg and check that the value of the sp ends with a 0 or an 8.

@OlegHahm
Copy link
Member

Verified on Cortex-M3 for STM32F103.

@OlegHahm
Copy link
Member

ACK, but I would like to have some of the hardware guys to take another look.

@OlegHahm
Copy link
Member

@kaspar030, ping!

@kaspar030
Copy link
Contributor

Well, looks OK, doesn't break anything as far as I can see. ACK.

kaspar030 added a commit that referenced this pull request May 18, 2015
@kaspar030 kaspar030 merged commit df7decc into RIOT-OS:master May 18, 2015
@OlegHahm OlegHahm added this to the Release 2015.06 milestone May 18, 2015
@jnohlgard jnohlgard deleted the pr/cortex-m-initial-stack-alignment branch June 25, 2015 08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Platform: ARM Platform: This PR/issue effects ARM-based platforms Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants