有网友來信問到,kernel 里的 system call 實作函數中(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 的語法。
#define asmlinkage CPP_ASMLINKAGE __attribute__((regparm(0)))
__attribute__是关键字,是gcc的C语言扩展,regparm(0)表示不从寄存器传递参数