1) Use of SWI commands
Area addreg, CODE, READONLY
ENTRY
MAIN
ADR r0, ThunbProg 1; (Why add 1? When the BX command jumps to the specified address to execute the program, if the (BX {cond} Rm) Rm bit [0] is 1, then, the flag T in CPSR is automatically set during the jump to interpret the target code as the Thunb code)
BX r0
CODE16
ThunbProg
Mov r2, #2
Mov r3, #3
Add r2, r2, r3
ADR r0, ARMProg
BX ro
CODE32
ARMProg
Mov r4, #4
Mov r5, #5
Add r4, r4, r5
Stop mov r0, #0x18
LDR r1, = 0x20026
SWI 0x123456
END
SWI -- Soft Interrupt command:
SWI commands are used to generate soft interruptions. From the support mode to the management mode, CPSR is saved to the SPSR in the management mode.
SWI {cond} immed_24; immed_24 is the Soft Interrupt number (service type)
When SWI commands are used, the following two methods are usually used to pass parameters. SWI exception interrupt handlers can provide related services, both of which are user software protocols. the SWI exception interrupt handler reads the SWI command that causes the Soft Interrupt to obtain the 24-bit instant count.
(1) The 24-bit immediate number in the command specifies the service type requested by the user, and the parameters are passed through the General Register.
Mov r0, #34; Set sub-function number 34
SWI 12; call Soft Interrupt on the 12th
(2) The 24-bit immediate number in the instruction is ignored. The service type requested by the user is determined by the value of register R0, and the parameter is passed through other General registers.
Mov r0, #12; Call no. 12 Soft Interrupt
Mov r1, #34; Set sub-function number 34
SWI 0
In the SWI exception Interrupt Processing Program, the steps for getting the SWI immediate number are: first, determine whether the SWI command that causes the Soft Interrupt is an ARM command or a Thunb command, which can be obtained through SPSR access; then obtain the address of the SWI command, which can be obtained by accessing the LR register; then read the command and break down the immediate number. for example:
T_bit EQU 0X20
SWI_Handler
Stmfd sp !, {R0-R3, R12, LR}; Field Protection
MRS R0, SPSR; read SPSR
Stmfd sp !, {R0}: Save the SPSR.
TST R0, # T_bit
LDRNEH R0, [LR, #-2]; if it is a Thunb command, read the instruction code (16 bits)
BICNE R0, #0XFF00: obtains the number of 8-bit instant messages of the Thunb command.
LDREQ R0, [LR, #-4]; if it is an ARM instruction, read the instruction code (32-bit)
BICEQ R0, #0XFF000000; 24-bit immediate Number of ARM commands obtained
....
Ldmfd sp !, {R0-R3, R12, PC} ^; SWI exception interrupt return
2)
;/*
; **************************************** **************************************** ***********
; ** Module name: SWI. s
; ** Function Description: SWI Exception Handling Function
; ** Creation date: 04-16-2007 by: CANOPEN
; ** Modification date::
; **************************************** **************************************** ***********
;*/
Area swi_function, code, readonly
Export swi_handler_ex
Import swi_exception_function
; T_bit equ 0x20
Swi_handler_ex
Stmfd SP !, {R0-r3, R12, LR}; // protected site
MoV R1, SP; // If the SWI call has a parameter, point r1 to the first parameter.
; // According to the atpcs standard, the first parameter is stored in R0
Mrs r0, spsr
Stmfd SP !, {R0}; // spsr inbound Stack
LDR r0, [LR, #-4]; // obtain the SWI instruction code
Bic r0, R0, #0xff000000; // obtain the SWI number
; // R0 now contains SWI number
; // R1 now contains pointer to stacked registers
BL SWI_Exception_Function; // call the SWI processing function compiled by C.
LDMFD sp !, {R0}
MSR spsr_cf, r0; // spsr output Stack
LDMFD sp !, {R0-r3, r12, pc} ^; // restore site
END
; /*************************************** **************************************** ***********
; ** End Of File
; **************************************** **************************************** ***********
Note:
1. When the SWI Soft Interrupt is reached, the ATPCS process call should not be mentioned, and subsequent logs should be recorded;
2. SWI. s is located in the ARM Executable Image for LPC2294 Engineering Template. Therefore, the SWI trigger is not considered as the Thumb state. Once a SWI exception is triggered, the kernel hardware is completed:
♂Enter the Supervisor mode;
♂Copy CPSR to SPSR_svc
♂Returns an error when copying data to LR_svc.♂Therefore, when the kernel is in Supervisor mode before the SWI Soft Interrupt is triggered, the values in SPSR_svc and LR_svc will be damaged;
3. The SWI instruction code uses 24-bit data as the Soft Interrupt number (swi_num). Therefore, you can obtain the Soft Interrupt number by using the SWI instruction code. This is the case for LDR r0, [lr, #-4;
4. SWI_Exception_Function generally uses C encoding (or compilation). switch can be directly applied using C encoding to switch based on swi_nun Soft Interrupt number. SWI_Exception_Function function is flexible, for example, it can be a function with or without parameters;
5. a swi call allows 1 ~ Four font parameters and 1 ~ Four font return values. The four parameters are stored in R0 ~ In R3, the returned values are also stored in R0 ~ In R3, which is consistent with the ATPCS function call;
6. In C, declare a typical SWI call without parameters and no return value: "_ swi (0x00) void IRQEnable (); "In this way, you can use" IRQEnable (); "to trigger a Soft Interrupt (Interrupt No. 0). The function that allows IRQ interruption must be implemented in the SWI_Exception_Function soft interrupt handler;
7. The following are SWI calls with parameters. SWI calls follow the ATPCS standard like normal function calls, and Handle parameters are stored in R0:
_ Swi (0x01) void SwiHandle (int Handle );
# Define IRQDisable () SwiHandle (0)
# Define IRQEnable () SwiHandle (1)
# Define FIQDisable () SwiHandle (2)
# Define FIQEnable () SwiHandle (3)
8. Note that the SP is indirectly addressable when the best Stack pointer is used. The "MOV r1, sp" command will also program a pointer for R1, the content is the address of R0 in RAM rather than the R0 value;
9. Another point: SWI calls the parameters with parameters and the SWI_Exception_Function function. They are two different things. I have been confused by these two parameters for a long time.
My conclusion: I tried it in RealView. Just use the immediate number to identify which service it is.