Step-by-Step learning of SWI interrupt simulation programs under the ARM7 Model

Source: Internet
Author: User

Describes how to use SWI for an interruption.
Privileged mode: A protection mode in which superisor SVC is used by the OS.
It is actually an interruption. For hardware users, the interruption mode is defined by the user.
1. Understand the available registers of normal user status
R0 ~ R7 universal R8 ~ R12 independent registers [some mode register groups are independent] R13-SP [independent] R14-LR [independent] PC [general] CPSR [independent]
R0 ~ R7 General registers are the same as General registers in PC
R8 ~ R12 can be used as the backup register and R0 ~ can be used ~ R7 can all use them
R13 as a stack pointer
R14 is used as the connection register to store the address returned by the current subroutine
The PC arm is a three-level assembly line, so the PC always performs word alignment for the currently executed commands + 8 commands under the arm, so the two low ones are 0
CPSR is the situation where the Current Status Register stores the current execution.

2. Understand what happens when the privileged mode is used for PC processing.
Execute commands in PC + 4 (both in arm State) during function calling !, {Register, LR} invalid fd sp !, {Register, PC}
Register inbound stack and LR connection register function return address inbound Stack
After the operation, register the stack and assign the LR value to the PC.
SWI (FIQ, IRQ) is inconsistent
(A) Save the current CPU status and set the spsr in CPSR-> privileged Mode
(B) Set the corresponding bits of CPRs in current mode, for example, disable FIQ interruption in FIQ mode...
(C) In privileged mode, the independent connection register LR stores the interrupt return address.
(D) switch the PC to the current interrupt vector address to execute the interrupt code

3. interrupted work returned
(A) restore the current processor status spsr-> CPSR in Normal Mode
(B) Recover the PC, LR-> current PC

4. SWI interruptions are generated by executing commands. Therefore, when an interruption occurs, the PC value is not updated to the current command + 8 address.
So the PC-4 is restoring the interrupt address. Note that

5. _ SWI modifier used for SWI interrupt call
Do not return the form void _ SWI (swi_num) swi_name (arguments );
Return the int _ SWI (swi_num) swi_name (arguments );
Up to four results r0-r3 can be returned with a structure
Struct type {int A, B, C, D ;};
Type (return type) _ value_in_regs (modifier that returns multiple results) _ SWI (swi_num) swi_name (arguments );

Then start example
Create the SWI. h file storage structure and SWI function declaration
_ SWI (0) int test1 (INT, INT); // two parameters return r0, and the address offset is 0
Struct mystruct // defines the returned struct
{
Int;
Int B;
Int C;
Int D;
};

_ SWI (1) _ value_in_regs struct mystruct
Test2 (INT, Int, Int, INT); // declare 1 custom interrupt

Create interrupt 1 and process the file handle. s
Import c_handle_swi // introduce II and interrupt handling
Export swi_handle // The outgoing interrupt handler.
Area swihandle, code, readonly
 
Swi_handle

Stmfd SP !, {R0-r3, R12, LR}
; Save spsr_svc
MoV R1, SP
Mrs r0, spsr // save spsr
Stmfd SP !, {R0} // stack entry

LDR r0, [LR, #-4]
Bic r0, R0, #0xff000000 // get the 24-hour number and place it in R0.

BL c_handle_swi // 2. Call
Ldmfd SP !, {R0}
MSR spsr_cf, R0
Ldmfd SP !, {R0-r3, R12, PC} ^

 
End
Create the Lib. c file and provide the interrupt function processing code.
Void c_handle_swi (INT swi_num, int * regs)
{
Switch (swi_num)
{
Case 0: // _ SWI (0)
Regs [0] = regs [0] * regs [1];
Break;
Case 1: // _ SWI (1)
{
Int X, Y, Z, P;
X = regs [0];
Y = regs [1];
Z = regs [2];
P = regs [3];


Regs [0] = x + y + Z + P;
Regs [1] = x + y-z-P;
Regs [2] = x * Y + z * P;
Regs [3] = (x + y) * (Z + p );

}

Default:
Break;
 
 
}
Provides main processing functions

# Include <stdio. h>
# Include "SWI. H"

Unsigned * swi_vec = (unsigned *) 0x08;
Extern void swi_handle (void );

Unsigned install_handler (unsigned routine, unsigned * vector)
{
Unsigned VEC, old_vec;
VEC = (routine-(unsigned) vector-8)> 2;
If (VEC & 0xff000000)
Printf ("handler greater than 32 Mbyte from Vector! ");
 
VEC = 0xea000000 | VEC;
Old_vec = * vector;
* Vector = VEC;
Return old_vec;
 
}

Int main ()
{
Int result1;
Struct mytype res_3;

Install_handler (unsigned) swi_handle, swi_vec); // load information
Result1 = test1 (2, 4); // interrupt 1
Res_3 = Test2 (12, 4, 3, 1); // watch the result
 
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.