9.4.2. SWI handlers in C and assembly language

Although the top-level header must always be written in ARM assembly language, the routines that handle each SWI can be written in either assembly language or in C. Refer to Using SWIs in supervisor mode for a description of restrictions.

The top-level header 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.

The following line can be added to the SWI_Handler routine in Example 9.5:

	BL	C_SWI_Handler				; Call C routine to handle the SWI

Example 9.7 shows how the C function can be implemented.

Example 9.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 such a 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 9.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;

causing the updated value to be written into the appropriate stack position, and then restored into the register by the top-level handler.

Figure 9.2. Accessing the supervisor stack

Copyright © 1997, 1998 ARM Limited. All rights reserved.ARM DUI 0040D
Non-Confidential