asmlinkage如何理解

1.在include\linux\Syscalls.h 中有很多系统调用的函数原型

 形式为:asmlinkage reutrn_type  (*sys_call)(type_argv argv)

2.在include\asm-i386\Linkage.h 中有asmlinkage这个宏的定义

但不是每个体系都支持这个宏好像,很多asm-arch\Linkage.h 没有这个宏的定义。

#define asmlinkage CPP_ASMLINKAGE __attribute__((regparm(0)))

3.在http://bbs.csdn.net/topics/80254626这个帖子里面有人有解答,

此外在http://www.jollen.org/blog/2006/10/_asmlinkage.html里面有一篇博文

已经被到处转载乱大街了。

4.摘抄过来

 

asmlinkage是个宏,使用它是为了保证参数在stack中。因为从汇编语言到C语言代码参数
的传递是通过stack的
,它也可能从stack中得到一些需要的参数。Asmlinkage将要
解析那些参数。

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)))

 

这些解释可能有些文绉绉的,不知道为什么sys_call 的定义要定义为asmlinkpage 的类型?

不过这些都是因为没有搞清楚系统调用的过程。

首先要了解一些概念  :用户进程和内核进程

                               :中断 硬件中断 软件中断   中断向量表   系统调用表

当用户程序中包含了一句系统调用的函数(简单的可以这么理解),在最终被编译为了汇编指令的时候,会调用int 0x80 号中断,

然后系统会切换用户堆栈到内核堆栈上,这个过程中,需要查询中断号与中断处理程序的表,称之为中断向量表

由于系统的中断向量表空间有限,不能每个系统调用都分配一个中断号,所以有一个系统调用表

再这个适配系统调用号的时候,是用汇编语言实现的,为系统调用传递参数的时候,需要把用户空间这些参数压入栈中,SAVE_ALL,所以系统调用

在编译的时候,就不从寄存器中取参数值,而是通过栈中去获取参数值,由于优化,默认是会从寄存器取值的

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值