User guide

5.4.2 SWI handlers in C and assembly language
Although the top-level handler must always be written in ARM assembly language, the routines that handle each SWI
can be written in either assembly language or in C. See Using SWIs in Supervisor mode for a description of
restrictions.
The top-level handler uses a BL (Branch with Link) instruction to jump to the appropriate C function. Because the
SWI number is loaded into r0 by the assembly routine, this is passed to the C function as the first parameter (in
accordance with the ARM Procedure Call Standard). The function can use this value in, for example, a switch()
statement.
You can add the following line to the SWI_Handler routine in Example 5-5:
BL C_SWI_Handler ; Call C routine to handle the SWI
Example 5-7 shows how the C function can be implemented.
Example 5-7
void C_SWI_handler (unsigned number)
{ switch (number)
{case 0 : /* SWI number 0 code */
break;
case 1 : /* SWI number 1 code */
break;
:
:
default : /* Unknown SWI - report error */
}
}
The supervisor stack space may be limited, so avoid using functions that require a large amount of stack space.
You can pass values in and out of a SWI handler written in C, provided that the top-level handler passes the stack
pointer value into the C function as the second parameter (in r1):
MOV r1, sp ; Second parameter to C routine...
; ...is pointer to register values.
BL C_SWI_Handler ; Call C routine to handle the SWI
and the C function is updated to access it:
void C_SWI_handler(unsigned number, unsigned *reg)
The C function can now access the values contained in the registers at the time the SWI instruction was encountered
in the main application code (see Figure 5-2). It can read from them:
value_in_reg_0 = reg [0];
value_in_reg_1 = reg [1];
value_in_reg_2 = reg [2];
value_in_reg_3 = reg [3];
and also write back to them:
reg [0] = updated_value_0;
reg [1] = updated_value_1;
reg [2] = updated_value_2;
reg [3] = updated_value_3;
This causes the updated value to be written into the appropriate stack position, and then restored into the register by
the top-level handler.
Handling Processor Exceptions
Copyright ?1999 2001 ARM Limited 5-9