I want to be a complete engineer - technical genius and sensitive humanist all in one!

Monday, October 27, 2008

关于kernel里面的asmlinkage

kernelnewibes上的解释是:

What is asmlinkage?
The asmlinkage tag is one other thing that we should observe aboutthis simple function.
 This is a #define for some gcc magic that tellsthe compiler that the function should not
 expect to find any of itsarguments in registers (a common optimization), but only on 
theCPU’s stack. Recall our earlier assertion that system_call consumesits first argument, 
the system call number, and allows up to four morearguments that are passed along to the 
real system call. system_callachieves this feat simply by leaving its other arguments (which 
werepassed to it in registers) on the stack. All system calls are markedwith 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 issuefor most 
other system calls. And, because you’ll be seeingasmlinkage in front of many other functions, 
I thought you shouldknow what it was about.

在kernel2.6.26
include/linux/linkage.h里面定义如下:

#ifndef asmlinkage_protect
# define asmlinkage_protect(n, ret, args...) do { } while (0)
#endif

CPP_ASMLINKAGE的定义为:
#ifdef __cplusplus
#define CPP_ASMLINKAGE extern “C”
#else
#define CPP_ASMLINKAGE
#endif

而在include/asm-i386/linkage.h里则有更明确的定义:

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

__attribute__是GCC的C语言扩展语法。regparm(0)表示不从寄存器中传递参数。

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

通俗而言,asmlinkage是声明在函数之前,表示该函数可以被汇编代码调用。 加上"asmlinkage" 后,C function 就会由 stack 取参数,而不是从 register 取参数。asmlinkage大都用在系统调用中,系统调用需要在entry.s文件中用汇编语言调用,所以必须要保证它符合C语言的参数传递规则,才能用汇编语言正确调用它。

No comments:

Labels

Followers