经过几个小时的研究,我得到以下经验要点:
(1) 在定义这样一个将从汇编代码中调用的函数时,是否需要 asmlinkage?
不,实际上fastcall是经常使用的。
例如在entry_32.S中,如果搜索“call”,则可以得到从这个汇编文件中调用的所有c函数。然后你可以看到,很多使用fastcall而不是asmlinkage作为调用约定。例如,
/*in entry_32.S*/
movl PT_OLDESP(%esp), %eax
movl %esp, %edx
call patch_espfix_desc
/*in traps_32.c*/
fastcall unsigned long patch_espfix_desc(unsigned long uesp,
unsigned long kesp)
(2) gcc 中的默认调用约定是什么。如果我在定义 ac 函数时省略了“asmlinkage”,它是否意味着 _cdecl 或 fastcall?
(3) 如果默认调用约定是 cdecl,为什么需要 asmlinkage,考虑到 cdecl 等于 asmlinkage 修饰符?(我在这里正确吗?)
For C functions not invoked from assembly code, we can safely assume the default calling convention is cdecl (or fast call; it doesn't matter, because gcc will take care of the caller and callee for parameter passing. The default calling convention can be specified when compiling). However, for C functions invoked from assembly code, we should explicitly declare the function's calling convention, because the parameter passing code in assembly side has been fixed. For example, if patch_espfix_desc is declared as asmlinkage, then gcc will compile the function to retrieve parameters from stack. This is inconsistent with the assembly side, which put the parameters into registers.
But I am still not clear when to use asmlinkage and when to use fastcall. I really need some guideline and resources to refer to.