Skip to content

cortex-m-rt: Add optional MSPLIM initialization, fix vector table size on armv8m #580

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

Merged
merged 2 commits into from
Feb 18, 2025

Conversation

decaday
Copy link
Contributor

@decaday decaday commented Feb 12, 2025

This PR adds an optional initialization for the Main Stack Pointer Limit (MSPLIM) register, controlled by the new set-msplim feature and armv8_main cfg.

The Main Stack Pointer Limit (MSPLIM) register is used to define the lowest valid stack address, helping to enforce stack limits and prevent unintended stack overflows. This register is only available on ARMv8-M Mainline (Cortex-M33).

MSPLIM initialization is often handled in the startup assembly file:

https://github.com/ARMmbed/uvisor/blob/97906335ea6892866e1fab7c6ef287004233826f/core/system/src/main.c#L84-L86

Some chip bootloaders also configure MSPLIM in advance. If it is not explicitly initialized in the startup code of user firmware, it will cause a HardFault when the program pushes to the stack in main().

Instead of relying on HALs or applications to define __pre_init for MSPLIM initialization, it seems more appropriate to handle this directly in cortex-m-rt, I think.

@decaday decaday requested a review from a team as a code owner February 12, 2025 07:47
@decaday
Copy link
Contributor Author

decaday commented Feb 12, 2025

Fixed interrupt count for Cortex-M23 (ARMv8-M Baseline, 240) and Cortex-M33 (ARMv8-M Mainline, 480) based on the technical reference manual. The original value of 496 referred to the entire vector table length, with exceptions length already included in the assert check.
Cortex-M23 technical reference manual (ARMv8-M Baseline)
image
Cortex-M33 technical reference manual (ARMv8-M Mainline)
image

@decaday decaday changed the title cortex-m-rt: Add optional MSPLIM initialization and set-msplim feature cortex-m-rt: Add optional MSPLIM initialization, fix vector table size assertion Feb 12, 2025
@decaday decaday marked this pull request as draft February 12, 2025 08:50
Fixed interrupt count for Cortex-M23 (ARMv8-M Baseline, 240) and Cortex-M33 (ARMv8-M Mainline, 480) based on the technical reference manual. The original value of 496 referred to the entire vector table length, with exceptions length already included in the assert check.
@decaday decaday changed the title cortex-m-rt: Add optional MSPLIM initialization, fix vector table size assertion cortex-m-rt: Add optional MSPLIM initialization, fix vector table size on armv8m Feb 12, 2025
@decaday decaday marked this pull request as ready for review February 12, 2025 09:08
Copy link
Member

@adamgreig adamgreig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks good!

@adamgreig adamgreig added this pull request to the merge queue Feb 18, 2025
Merged via the queue into rust-embedded:master with commit f2fc7d3 Feb 18, 2025
11 checks passed
@jannic
Copy link
Member

jannic commented Feb 18, 2025

Works as expected on an rp2350.
Tested using rp235x-hal, with a trivial example using unbounded recursion.
I get a HardFault in with and without msplim, but with msplim it happens right when the stack limit is reached:

(gdb) print $sp
$2 = (*mut ()) 0x20000450

While without msplim, it happens only when running out of RAM (after overwriting all the heap):

(gdb) print $sp
$1 = (*mut ()) 0x1fffffd0

After enabling SHCSR.USGFAULTENA I also see the UsageFault being called, and the STKOF bit in UFSR being set. (*0xe000ed28 == 0x100000)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants