In the ARM Cortex M4, If the PSP stack pointer is configured, what address space does it point to? Is it the same address space as the MSP (main stack pointer)? Or do we create a new and separate stack for it? And how do we configure the TOP of stack for the PSP pointer?
Hello,
you can use
MSR PSP,Rn
to set PSP from the register Rn and
MRS Rn,PSP
to get PSP value into the register Rn.
MSR/MRS are also applicable to MSP.
Best regards,
Yasuhiko Koumoto.
Thank you yasuhikokoumoto.
As a follow up questions:
How do you set the size of your PSP stack? And for that matter, how would you set the size of the MSP stack? (or does the pointer decrements until it gets to its 8 byte alignment address?
Can and should the PSP stack overlap the MSP stacK?
One use model is for PSP to be used as the private stack for each thread, while MSP is retained as the stack for the RTOS kernel and all subsequent nested exceptions.
For each thread that executes, there would be a reserved area of memory for it to use as a stack (your toolchain may be able to tell you how much is required for a given program), and at thread initialisation time, the kernel would set the SP for that thread to the top of this area.
Context switching of the thread would then involve preserving the outgoing and restoring the incoming versions of the non-automatically store registers and the PSP.
Another dedicated (potentially larger) memory space would be reserved for the exception handlers (which if nested can consume considerable memory space), and the MSP set to this (likely via the automatic assignment at reset time).
It is very unlikely that having the PSP and MSP set to the same or overlapping values is useful.
If you are using separate PSP and MSP, then you may also want to consider using the memory protection unit, and restricting what regions are accessible for threads vs handlers.
hth
Simon.
You might want to read: Re: Use case of MSP and PSP in Cortex M
That was a great and informative answer Simon. So what you are saying in conclusion is that the PSP pointer does not typically point to the same stack as the MSP pointer.
How do you set the size of your PSP stack, and where would you want to typically locate your PSP stack?