User guide

5.4 SWI handlers
When the SWI handler is entered, it must establish which SWI is being called. This information can be stored in bits
0-23 of the instruction itself, as shown in Figure 5-1, or passed in an integer register, usually one of r0-r3.
Figure 5-1 ARM SWI instruction
The top-level SWI handler can load the SWI instruction relative to the link register (LDR swi, [lr, #-4]). Do this
in assembly language, or C/C++ inline assembler.
The handler must first load the SWI instruction that caused the exception into a register. At this point, lr_SVC holds
the address of the instruction that follows the SWI instruction, so the SWI is loaded into the register (in this case r0)
using:
LDR r0, [lr,#-4]
The handler can then examine the comment field bits, to determine the required operation. The SWI number is
extracted by clearing the top eight bits of the opcode:
BIC r0, r0, #0xFF000000
Example 5-5 shows how you can put these instructions together to form a top-level SWI handler.
See Determining the processor state for an example of a handler that deals with both ARM-state and Thumb-state
SWI instructions.
Example 5-5
AREA TopLevelSwi, CODE, READONLY ; Name this block of code.
EXPORT SWI_Handler
SWI_Handler
STMFD sp!,{r0-r12,lr} ; Store registers.
LDR r0,[lr,#-4] ; Calculate address of SWI instruction and load it into r
0.
BIC r0,r0,#0xff000000 ; Mask off top 8 bits of instruction to give SWI number.
;
; Use value in r0 to determine which SWI routine to execute.
;
LDMFD sp!, {r0-r12,pc}^ ; Restore registers and return.
END ; Mark end of this file.
5.4.1 SWI handlers in assembly language
The easiest way to call the handler for the requested SWI number is to use a jump table. If r0 contains the SWI
number, the code in Example 5-6 can be inserted into the top-level handler given in Example 5-5, following on from
the BIC instruction.
Example 5-6 SWI jump table
CMP r0,#MaxSWI ; Range check
LDRLS pc, [pc,r0,LSL #2]
B SWIOutOfRange
SWIJumpTable
DCD SWInum0
DCD SWInum1
; DCD for each of other SWI routines
SWInum0 ; SWI number 0 code
B EndofSWI
SWInum1 ; SWI number 1 code
B EndofSWI
; Rest of SWI handling code
;
EndofSWI
; Return execution to top level
; SWI handler so as to restore
; registers and return to program.
Handling Processor Exceptions
Copyright ?1999 2001 ARM Limited 5-8