当前位置:网站首页>Arm system call exception assembly
Arm system call exception assembly
2022-07-27 08:39:00 【Life needs depth】
A7 Processor exception swi and M3 Processor exception svc
A special interrupt :SVCall
sketch : An interrupt triggered by a program , Default on
origin :SVC( System service call , Also called system call ) It is mostly used in software development on the operating system .SVC Call requests used to generate system functions . for example , The operating system does not allow the user program to directly access the hardware , But by providing some system service functions , User program use SVC Make a call request to the system service function , Call them in this way to access the hardware indirectly . therefore , When a user program wants to control specific hardware , It will create a SVC abnormal , And then the operating system provides SVC Exception service routine executed , It then calls the relevant operating system functions , The latter completes the service requested by the user program .
purpose : Can be set by , Enable a piece of code to be interrupted by certain interrupts , Instead of being interrupted by other interruptions , For example, it can be used to ensure simulation IIC The time sequence of is not interrupted, resulting in communication failure
Be careful :
- SVC Exceptions must be responded to immediately ( If the priority is not higher than the current one , Or other reasons that make it impossible to immediately Respond to , Will lead to HardFault)
- The concept of master-slave priority is the same as that of ordinary interrupts ( And have the same status , Even the special interrupt is not special )// I wish : By default , except HardFault and NMI, The priority of other interrupts is 0,0( Additional reminders ,group Setting must precede priority Set up ),!!BUT!!, The setting of interrupt priority needs to be called :NVIC_SetPriority(SVCall_IRQn,NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 1, 1));
stay C Use in SVCall
SVC The service function uses the stack to pass parameters , so C Language version of the SVC The service function requires an assembly operation , Used to extract parameters from the stack into registers


__asm void SVC_Handler(void) // The function name is in Keil Chung Tung USART2_IRQHandler wait
{
// Assembly operations , Used to propose the starting position of the stack frame , And on the R0 in , Then jump to the actual SVC In the service routine
IMPORT svc_handler
TST LR, #4
ITE EQ
MRSEQ R0, MSP
MRSNE R0, PSP
B svc_handler
}
// “ real ” Service function of , Accept a pointer parameter (pwdSF): Stack the starting address of the stack .
// pwdSF[0] = R0 , pwdSF[1] = R1
// pwdSF[2] = R2 , pwdSF[3] = R3
// pwdSF[4] = R12, pwdSF[5] = LR
// pwdSF[6] = The return address ( On stack PC)
// pwdSF[7] = xPSR
unsigned long svc_handler(unsigned int* pwdSF)
{
unsigned int svc_number;
unsigned int svc_r0;
unsigned int svc_r1;
unsigned int svc_r2;
unsigned int svc_r3;
int retVal; // Used to store the return value
svc_number = ((char *) pwdSF[6])[-2]; // I didn't think! ,C The array of can be used so much !
svc_r0 = ((unsigned long) pwdSF[0]);
svc_r1 = ((unsigned long) pwdSF[1]);
svc_r2 = ((unsigned long) pwdSF[2]);
svc_r3 = ((unsigned long) pwdSF[3]);
printf (“SVC number = %xn”, svc_number);
printf (“SVC parameter 0 = %x\n”, svc_r0);
printf (“SVC parameter 1 = %x\n”, svc_r1);
printf (“SVC parameter 2 = %x\n”, svc_r2);
printf (“SVC parameter 3 = %x\n”, svc_r3);
// Do some work , And store the return value in retVal in
pwdSF[0]=retVal;
return 0;
}
// Be careful , This function does not return 0! Further , Gray text is only used to make the compiler happy , Specific reference Cortex-M3 Authoritative guide P169
How to trigger an interrupt ?
step1. Declare functions (__svc The corresponding function will be automatically generated )//__svc when keil A macro in
unsigned long __svc(0x03) CallSvc3(unsigned long svc_r0, unsigned long svc_r1, unsigned long svc_r2, unsigned long svc_r3);
step2. Call function
unsigned long svcRet; // The return value of the system service svcRet=CallSvc3(p0, p1, p2, p3); // call 3 System service No , And deliver 4 Parameters , In turn :p1,p2,p3,p4, Then receive the return value to svcRet in ( Don't forget , The origin of this return value is unusual )
flyangchina 2016-08-19 17:25
Pretty good , stay svc_handler There should be code to call the corresponding function in .
You can add a SVC_Table Array to hold function pointers , Such as this :
extern int __SVC_0(int, int);
extern int __SVC_1(int);
void * SVCTable[256] = {
__SVC_0,
__SVC_1
};
Then write like this svc_handler writes :
void svc_handler(unsigned int* pwdSF) {
// Use r0~r3 Call the corresponding function for the parameter ,
// And store the return value in the caller's stack r0 It's about
pwdSF[0] = ((int (*)(int, int, int, int))SVCTable[((char *) pwdSF[6])[-2]])
(
pwdSF[0],
pwdSF[1],
pwdSF[2],
pwdSF[3]
);
}
Add a little more __SVC_0 The definition of , In a random file ,__SVC_1 Forget it :
int __SVC_0 (int i1, int i2) {
return (i1 + i2);
}
Use as follows :
int __svc(0) add (int i1, int i2);
...
void test() {
add(1,1);
}
int main() { ... }
In this way, there is a whole use svc Realize the simple architecture of system call
Serval 2015-02-10 16:10
About “svc_number = ((char *) pwdSF[6])[-2]; // I didn't think! ,C The array of can be used so much ! ” Usage of , Looked at half a day , Post it to see if you understand it :
First, the ultimate goal of this line of code is to get svc #num Medium Num. stay Thumb Code (STM32 Most of them should be Cortex . ) in , An instruction is usually 16 position ,svc #num The instructions of are usually translated as 0xDFxx --> among xx Namely #num. therefore ,Keil The manual has the following explanation :
0 to 224–1 (a 24-bit value) in an ARM instruction.
0-255 (an 8-bit value) in a Thumb instruction.
The code can be translated into something easy to understand :
*((char *)pwdSF[6] - 2)
give an example :
Suppose the system is configured as a small end , Lower the low byte .
Instructions for :svc #1 ---> Binary for 0xDF01, The address is 0x286.
So actually , Pushed into the stack PC, Deposit is 0x288. because PC What is pressed in is the next instruction .
0x286: DF01 ;svc #1
0x288: ......
Stack: 88 02 00 00
That strange line of code is to get this 01. Several steps :
1. Convert to (char*)pwdSF[6], Parallel subtraction 2
This step is to make the pointer point to 88 02 The location of .
(char*)pwdSF[6] - 2
2. Pointer value
*((char *)pwdSF[6] - 2)
3. Pointer access is changed to the following table access form
The pointer value can be written in the form of subscript , for example :*(p + i) Equate to p[i], I suggest that the pointer should be written in the second way
((char *) pwdSF[6])[-2]
边栏推荐
- Day6 --- Sqlalchemy advanced
- Breadth first search
- After downloading URL loader and specifying the size of the image with limit, the image will not be displayed
- Alibaba cloud international receipt message introduction and configuration process
- while Loop
- Map structure
- How to uninstall -- Qianxin secure terminal management system
- What are the differences or similarities between "demand fulfillment to settlement" and "purchase to payment"?
- Binglog backup data
- 面试官:什么是脚手架?为什么需要脚手架?常用的脚手架有哪些?
猜你喜欢

Functions and arrow functions

开怀一笑

Eval and assert execute one sentence Trojan horse

4279. 笛卡尔树

Zhongang Mining: the new energy industry is developing rapidly, and fluorine chemical products have a strong momentum

Breadth first search

Create a project to realize login and registration, generate JWT, and send verification code

Using ecological power, opengauss breaks through the performance bottleneck

Apache SSI remote command execution vulnerability

Flutter 渲染机制——GPU线程渲染
随机推荐
Blueprint class view method
【渗透测试工具分享】【dnslog服务器搭建指导】
海关总署:这类产品暂停进口
Day3 -- flag state holding, exception handling and request hook
redis的string类型及bitmap
Iterators and generators
Background order management
Function realization of order system
JS basic exercises
Luogu super Mary game
All in one 1251 - Fairy Island for medicine (breadth first search)
Apache SSI remote command execution vulnerability
无法获取下列许可SOLIDWORKS Standard,无法找到使用许可文件。(-1,359,2)。
Binglog backup data
691. Cube IV
Explain cache consistency and memory barrier
如何卸载--奇安信安全终端管理系统
Minio 安装与使用
Realization of backstage brand management function
Realize SKU management in the background