rust-embedded / cortex-m

Low level access to Cortex-M processors
Apache License 2.0
835 stars 152 forks source link

`cortex-m-rt` linker script symbols are hard to use with `thumbv8` MSPLIM #564

Closed korken89 closed 3 weeks ago

korken89 commented 1 month ago

Hi all,

I'm adding flip-link-like functionality to a project using a Cortex-M33, so I plan to set MSPLIM to the end of statically allocated RAM. I checked the cortex-m-rt crate for available public symbols to set MSPLIM, and I want to set it to __euninit, which is where the stack and statically allocated memory collide. However, there isn't a public symbol for this. Moreover, double-underscore symbols are private as stated here:

https://github.com/rust-embedded/cortex-m/blob/6a324a830fd3040181d18ce59ae05bd44e4759a6/cortex-m-rt/link.x.in#L3-L7

What would be the best course of action here?

To me it seems that either one adds a feature like set-msplim to cortex-m to extend cortex_m::register::msplim with a method that uses the internal symbol from cortex-m-rt, or add a public symbol in cortex-m-rt to indicate "end of statically allocated memory" so users can setup MSPLIM themselves in pre_init.

Right now I'll do the following, but it is technically allowed to break in any patch release (even if we don't do that in practice):

#[cortex_m_rt::pre_init]
unsafe fn pre_init() {
    extern "C" {
        static __euninit: u32;
    }

    msplim::write(addr_of!(__euninit) as u32);
}
korken89 commented 1 month ago

Maybe one also wants to add an offset, e.g. 64 bytes so UsageFault gets a little bit of stack and does not cause a Lockup due to infinite faults.