User guide

Figure 5-2 Accessing the supervisor stack
5.4.3 Using SWIs in Supervisor mode
When a SWI instruction is executed:
1. The processor enters Supervisor mode.
2. The CPSR is stored into spsr_SVC.
3. The return address is stored in lr_SVC (see The processor response to an exception).
If the processor is already in Supervisor mode, lr_SVC and spsr_SVC are corrupted.
If you call a SWI while in Supervisor mode you must store lr_SVC and spsr_SVC to ensure that the original values
of the link register and the SPSR are not lost. For example, if the handler routine for a particular SWI number calls
another SWI, you must ensure that the handler routine stores both lr_SVC and spsr_SVC on the stack. This
ensures that each invocation of the handler saves the information needed to return to the instruction following the
SWI that invoked it. Example 5-8 shows how to do this.
Example 5-8 SWI Handler
STMFD sp!,{r0-r3,r12,lr} ; Store registers.
MOV r1, sp ; Set pointer to parameters.
MRS r0, spsr ; Get spsr.
STMFD sp!, {r0} ; Store spsr onto stack. This is only really needed in case o
f
; nested SWIs.
; the next two instructions only work for SWI calls from ARM state.
; See Example 5-17 for a version that works for calls from either ARM or Thumb.
LDR r0,[lr,#-4] ; Calculate address of SWI instruction and load it into r0.
BIC r0,r0,#0xFF000000 ; Mask off top 8 bits of instruction to give SWI number.
; r0 now contains SWI number
; r1 now contains pointer to stacked registers
BL C_SWI_Handler ; Call C routine to handle the SWI.
LDMFD sp!, {r0} ; Get spsr from stack.
MSR spsr_cf, r0 ; Restore spsr.
LDMFD sp!, {r0-r3,r12,pc}^ ; Restore registers and return.
Nested SWIs in C and C++
You can write nested SWIs in C or C++. Code generated by the ARM compilers stores and reloads lr_SVC as
necessary.
5.4.4 Calling SWIs from an application
You can call a SWI from assembly language or C/C++.
In assembly language, set up any required register values and issue the relevant SWI. For example:
MOV r0, #65 ; load r0 with the value 65
SWI 0x0 ; Call SWI 0x0 with parameter value in r0
The SWI instruction can be conditionally executed, as can almost all ARM instructions.
Handling Processor Exceptions
Copyright ?1999 2001 ARM Limited 5-10