转自:https://www.cnblogs.com/china_blue/archive/2010/01/15/1648523.html

https://blog.csdn.net/liujiaoyage/article/details/31781289

什么是 "asmlinkage"?


相信大家在看linux的source code的时候,都会注意到asmlinkage这个宏,它是用来做什么的呢?

The asmlinkage tag is one other thing that we should observe about this simple function. This is a #define for some gcc magic that tells the compiler that the function should

not expect to find any of its arguments in registers (a common optimization), but only on the CPU's stack. Recall our earlier assertion that system_call consumes its first

argument, the system call number, and allows up to four more arguments that are passed along to the real system call. system_call achieves this feat simply by leaving its

other arguments (which were passed to it in registers) on the stack. All system calls are marked with the asmlinkage tag, so they all look to the stack for arguments. Of

course, in sys_ni_syscall's case, this doesn't make any difference, because sys_ni_syscall doesn't take any arguments, but it's an issue for most other system calls.

看一下/usr/include/asm/linkage.h里面的定义:
#define asmlinkage CPP_ASMLINKAGE __attribute__((regparm(0)))
__attribute__是关键字,是gcc的C语言扩展,regparm(0)表示不从寄存器传递参数

如果是__attribute__((regparm(3))),那么调用函数的时候参数不是通过栈传递,而是直接放到寄存器里,被调用函数直接从寄存器取参数

还有一种是:

#define fastcall __attribute__((regparm(3))) 
#define asmlinkage __attribute__((regparm(0))) 
函数定义前加宏asmlinkage ,表示这些函数通过堆栈而不是通过寄存器传递参数。 
gcc编译器在汇编过程中调用c语言函数时传递参数有两种方法:一种是通过堆栈,另一种是通过寄存器。缺省时采用寄存器,假如你要在你的汇编过程中调用c语言函数,并且想通过堆栈传递参数,你定义的c函数时要在函数前加上宏asmlinkage

asmlinkage long sys_nice(int increment)
"asmlinkage" 是在 i386 system call 实作中相当重要的一个 gcc 标签(tag)。
当 system call handler 要调用相对应的 system call routine 时,便将一般用途缓存器的值 push 到 stack 里,因此 system call routine 就要由 stack 来读取 system call handler 传递的

参数。这就是 asmlinkage 标签的用意。
system call handler 是 assembly code,system call routine(例如:sys_nice)是 C code,当 assembly code 调用 C function,并且是以 stack 方式传参数(parameter)时,在 C

function 的 prototype 前面就要加上 "asmlinkage"。
加上 "asmlinkage" 后,C function 就会由 stack 取参数,而不是从 register 取参数(可能发生在程序代码最佳化后)。
更进一步的说明...
80x86 的 assembly 有 2 种传递参数的方法:
1. register method
2. stack method
Register method 大多使用一般用途(general-purpose)缓存器来传递参数,这种方法的好处是简单且快速。另外一种传递参数的做法是使用 stack(堆栈),assembly code 的模式如下:
push number1
push number2
push number3
call sum
在 'sum' procedure 里取值的方法,最简单的做法是:
pop ax 
pop ax
pop bx
pop cx
Stack Top 是放 IP,我们传给 sum procedure 的参数由 stack 的后一个 entry 开始读取。
其它有关 asmlinkage
1. asmlinkage 是一个定义
2. "asmlinkage" 被定义在 /usr/include/linux/linkage.h
3. 如果您看了 linkage.h,会发现 "__attribute__" 这个语法,这是 gcc 用来定义 function attribute 的语法

asmlinkage的用法的更多相关文章

  1. Linux内核源码特殊用法

    崇拜并且转载的: http://ilinuxkernel.com/files/5/Linux_Kernel_Source_Code.htm Linux内核源码特殊用法 1 前言 Linux内核源码主要 ...

  2. EditText 基本用法

    title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...

  3. jquery插件的用法之cookie 插件

    一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...

  4. Java中的Socket的用法

                                   Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...

  5. [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法

    一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...

  6. python enumerate 用法

    A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...

  7. [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结

    本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...

  8. 【JavaScript】innerHTML、innerText和outerHTML的用法区别

    用法: <div id="test">   <span style="color:red">test1</span> tes ...

  9. chattr用法

    [root@localhost tmp]# umask 0022 一.chattr用法 1.创建空文件attrtest,然后删除,提示无法删除,因为有隐藏文件 [root@localhost tmp] ...

随机推荐

  1. 通过python的selenium实现网站自动登陆留言

    from selenium import webdriver import time driver = webdriver.Chrome() driver.get('https://wordpress ...

  2. 【GCN】图卷积网络初探——基于图(Graph)的傅里叶变换和卷积

    [GCN]图卷积网络初探——基于图(Graph)的傅里叶变换和卷积 2018年11月29日 11:50:38 夏至夏至520 阅读数 5980更多 分类专栏: # MachineLearning   ...

  3. 轻松搭建CAS 5.x系列(3)-连接数据库,使用数据库表中的帐号做登录

    概要说明 前面的CAS SEVER中的登录帐号名是配置中写死的,实际情况中不太可能用这些方法.通常情况下,数据库的帐号名密码都是在数据库表中的,这样可以对登录帐号进行增删改的处理. 如果您对搭建固定帐 ...

  4. 基于Windows服务的聊天程序

    本文将演示怎么通过C#开发部署一个Windows服务,该服务提供各客户端的信息通讯,适用于局域网.采用TCP协议,单一服务器连接模式为一对多:多台服务器的情况下,当客户端连接数超过预设值时可自动进行负 ...

  5. 维护solr索引库

    一 2)solrcore    一个solr下可以有多个solrcore,每个solrcore就是一个独立的索引库3)solrconfig.xml    lib:配置solr的扩展包的位置,不指定路径 ...

  6. 客户端相关知识学习(十)之app给h5传递数据

    方法一: app可以把参数传到h5的链接里,用类似?xx=xx&xx=xx的形式拼接,js解析参数即可. 方法二: 情况一:app调用h5 原生app都可以对js的function进行触发,前 ...

  7. Appium|Locator Strategy ... is not supported for this session

    appim server log InvalidSelectorError: Locator Strategy 'xpath,//android.widget.TextView[@resource-i ...

  8. 【Day4】1.JsonPath使用案例

    import json python_data = [ { 'username': 'normal', 'vip': True, }, { 'username':None, 'vip':False } ...

  9. yum安装mysql-server

    yum install mysql-server mysql vi /etc/my.cnf [client] default-character-set=utf8 service mysqld sta ...

  10. jquery.table2excel,将HTML的table标签数据导出成excel

    <!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content=&quo ...