当前位置:网站首页>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]
边栏推荐
- General view, DRF view review
- Arm undefined instruction exception assembly
- All in one 1353 -- expression bracket matching (stack)
- 众昂矿业:新能源行业快速发展,氟化工产品势头强劲
- Realization of specification management and specification option management functions
- Element display mode: block level, inline, inline block, nesting specification, display mode conversion
- How to uninstall -- Qianxin secure terminal management system
- redis的string类型及bitmap
- 3428. 放苹果
- Is online account opening safe? Want to know how securities companies get preferential accounts?
猜你喜欢

The shelf life you filled in has been less than 10 days until now, and it is not allowed to publish. If the actual shelf life is more than 10 days, please truthfully fill in the production date and pu

我用字符画出了一个谷爱凌!

Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering

好吃难吃饱七分为宜;好喝难喝醉三分为佳

微信安装包从0.5M暴涨到260M,为什么我们的程序越来越大?

How to upload qiniu cloud

Login to homepage function implementation

UVM入门实验1

4276. 擅长C

说透缓存一致性与内存屏障
随机推荐
QPushButton 按钮的创建与简单应用
General view, DRF view review
One book 1201 Fibonacci sequence
说透缓存一致性与内存屏障
How to permanently set source
[geek challenge 2019] finalsql 1
ROS2安装时出现Connection failed [IP: 91.189.91.39 80]
691. 立方体IV
情人节,我用字符画出了一个对象!
Flask's operations on model classes
4275. Dijkstra sequence
帮忙发几个招聘,有兴趣可以看看
说透缓存一致性与内存屏障
Realization of backstage brand management function
I drew a Gu ailing with characters!
All in one 1353 -- expression bracket matching (stack)
Use of flask
The shelf life you filled in has been less than 10 days until now, and it is not allowed to publish. If the actual shelf life is more than 10 days, please truthfully fill in the production date and pu
Eval and assert execute one sentence Trojan horse
It's better to be full than delicious; It's better to be drunk than drunk