当前位置:网站首页>Understanding of asmlinkage
Understanding of asmlinkage
2022-07-28 16:52:00 【be faithful to one 's husband unto death】
Original address :
One 、 In large scale C Language project engineering or linux We often see two in the kernel FASTCALL and armlinkage
Two identifiers ( Modifier ), So what are the differences between them ? Today I will share my experience with you .
Everyone knows that in the standard C The formal parameters of functions in the system will involve the problem of parameter storage when actually passing in parameters , Where are these parameters stored ? Friends with a certain theoretical basis will definitely answer : These function parameters and local variables inside the function are allocated to the local stack of the function , Is that really the case ? In fact, there are exceptions :
First as linux operating system , It doesn't necessarily only run on X86 Under the platform , There are other platforms such as ARM,PPC, Da Vinci, wait , Therefore, different processor architectures cannot be guaranteed to pass The local stack passes parameters , Maybe a friend will ask at this time : Where can I put it if I don't put it on the stack ? be familiar with ARM My friend must know ARM It defines a set of rules for parameters passing in the process of function call , It's called ATPCS( The mainland is called AAPCS), The rules clearly state ARM in R0-R4 Are used as general registers , When the function is called, the processor starts from R0-R4 Get parameters in , When the function returns Save the parameters to be returned to R0-R4 in , In other words, function parameters can be stored directly in registers , So in order to strictly distinguish the storage location of function parameters , Two tags are introduced , namely asmlinkage and FASTCALL, The former means that the function parameters are stored in the local stack , The latter tells the compiler to save the function parameters in registers
We are searching for some additional clues ,ARM in R0-R4 Used to store incoming parameters , Vaguely tell us , As a high-level embedded system developer , Or high level C Language programmers , The parameters of a function should not This is greater than 5 individual , Then someone will ask in turn : exceed 5 Where will those parameters of go ? My answer is : If the incoming parameter exceeds 5 individual , Redundant parameters are still stored in the local stack , At this time, someone may May ask : What's wrong with passing function parameters into the local stack ? My answer is : There is nothing bad on the surface , But if you are a person with linux Kernel trained programmers , You will vaguely remember linux in , Whether it's a system call , Or system traps will cause user space to fall into kernel space , We know , The permission level of system space is 0, The permission level of user space is 3, The system call level is 3 User space trap The permission level is 0 The kernel space of , It will inevitably cause stack switching ,linux The system will start from the global task status stack TSS Find a suitable kernel stack information to save and overwrite the current SP,SS The inside of two registers Rong , To complete stack switching , At this time, the stack seen in kernel space is not the stack in user space , So the data pushed into the user stack at the time of call is at the moment when it gets into the kernel , Stranded in the user space stack , The kernel doesn't even know it exists , Therefore, as a security consideration or as a personal cultivation of high-level programmers , You should not pass too many parameters to system call level functions .
Two : It is GCC Yes C An extension of the program , #define asmlinkage __attribute__((regparm(0)))
To express with 0 Register transfer function parameters , such , All function parameters are forced to be extracted from the stack .
This asmlinkage Mostly used in system calls , The system call needs to be in entry.s In the file, assembly language is used to call , So we must ensure that it conforms to C Parameter passing rules of language , It can be called correctly in assembly language .
That's why asmlinkage Why? ! This is my understanding .
Take a closer look, there is asmlinkage The place where is usually the function called by the system , Because in the system call , After the register is transferred from user space SAVE_ALL Push into the stack , Then call the corresponding system call function , In this way, the system call function must ensure that the parameters are passed through the stack
————————————————
Copyright notice : This paper is about CSDN Blogger 「 Daddy programmer 」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/qq84395064/article/details/86593469
边栏推荐
- ANSA二次开发 - 抽中面的两种方法
- asp.net大文件分块上传断点续传demo
- Applet: get element node information
- 排序3-选择排序与归并排序(递归实现+非递归实现)
- Curl returns blank or null without output. Solve the problem
- 优化Hypermesh脚本性能的几点建议
- 一小时内学会Abaqus脚本编程秘籍
- Hdu1847 problem solving ideas
- Interesting kotlin 0x06:list minus list
- Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
猜你喜欢

What does it remote operation and maintenance mean? Which is the best remote operation and maintenance software?

Introduction and implementation of stack (detailed explanation)

Sort 4-heap sort and massive TOPK problem

Ansa secondary development - two methods of drawing the middle surface

ANSA二次开发 - 在PyCharm上搭建ANSA/META二次开发环境

Leetcode learn to insert and sort unordered linked lists (detailed explanation)

Interesting kotlin 0x06:list minus list

结构化设计的概要与原理--模块化

有趣的 Kotlin 0x08:What am I

Nowcode- learn to delete duplicate elements in the linked list (detailed explanation)
随机推荐
Leetcode learn complex questions with random pointer linked lists (detailed explanation)
Sort 5-count sort
UNP前六章 回射服务模型 解析
CRC16数据校验支持ModelBus和XMODEM校验模式(C语言)
Installation of QT learning
Reset grafana login password to default password
HM secondary development - data names and its use
Some suggestions on optimizing HyperMesh script performance
13 differences between MySQL and Oracle
PHP gets the applet code, and the applet jumps with parameters
LeetCode-学会对无序链表进行插入排序(详解)
Li Hongyi, machine learning 5. Tips for neural network design
微软100题-天天做-第16题
Abaqus GUI界面解决中文乱码问题(插件中文乱码也适用)
队列的介绍与实现(详解)
About the web docking pin printer, lodop uses
mysql cdc 如果binlog日志文件不全,全量阶段能读到所有数据吗
Some suggestions on Oracle SQL tuning
LeetCode-学会复杂带随机指针链表的题(详解)
parseJson