Exceptions
ARM DDI 0337G
Copyright © 2005-2008 ARM Limited. All rights reserved.
5-25
Unrestricted Access
Non-Confidential
5.11
Setting up multiple stacks
To implement multiple stacks, the application must take these actions:
•
use the MSR instruction to set up the Process_SP register
•
if using an MPU, protect the stacks appropriately
•
initialize the stack and privilege of the Thread mode.
If the privilege of Thread mode is changed from privileged to user, only another ISR,
such as SVCall, can change the privilege back from user to privileged.
The stack in Thread mode can be changed from main to process or from process to
main, but doing so affects its access to the local variables of the thread. It is better to
have another ISR change the stack used in Thread mode. The following shows an
example boot sequence:
1.
Call setup routine to:
a.
Set up other stacks using MSR.
b.
Enable the MPU to support base regions, if any.
c.
Invoke all boot routines.
d.
Return from setup routine.
2.
Change Thread mode to unprivileged.
3.
Use SVC to invoke the kernel. Then the kernel:
a.
Starts threads.
b.
Uses MRS to read the SP for the current user thread and save it in its TCB.
c.
Uses MSR to set the SP for the next thread. This is usually SP_process.
d.
Sets up the MPU for the newly current thread, if necessary.
e.
Returns into the newly current thread.
Example 5-4 shows a modification to the EXC_RETURN value in the ISR to return
using PSP.
Example 5-4 Modification to the EXC_RETURN value in the ISR
; First time use of PSP, run from a Handler with RETTOBASE == 1
LDR r0, PSPValue ; acquire value for new Process stack
MSR PSP, r0 ; set Process stack value
ORR lr, lr, #4 ; change EXC_RETURN for return on PSP
BX lr ; return from Handler to Thread
Example 5-5 on page 5-26 shows how to implement a simple context switcher after the
switch to Thread on PSP.