IS" asmlinkage"对于C函数需要从装配叫什么名字? [英] Is "asmlinkage" required for a c function to be called from assembly?

查看:253
本文介绍了IS" asmlinkage"对于C函数需要从装配叫什么名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写将从组装code调用C函数。

I am writing a C function that will be invoked from assembly code.

(具体来说,我想要做的系统调用Linux内核处理的路径一些检查工作,所以我会打电话给C函数的系统调用在entry_32.S派遣前)

(Specifically, I want to do some checking job in the path of system call handling in linux kernel, so I will call the c function before a system call is dispatched in entry_32.S)

我定义我的C函数时感到困惑的asmlinkage修饰符。

I am confused with the "asmlinkage" modifier when defining my c function.

我知道asmlinkage是告诉这些参数将通过堆栈传递的编译器。

I know asmlinkage is to tell the compiler that the parameters will be passed through stack.

的#define asmlinkage CPP_ASMLINKAGE __attribute __((regparm(0)))

问题:

(1)定义将从组装code调用这样的功能时,需要asmlinkage?

(1) Is asmlinkage required when defining such a function that will be invoked from assembly code?

(2)什么是默认调用gcc的约定。如果我定义一个C函数时省略了asmlinkage,这是否意味着_cdecl或FASTCALL?

(2) what is the default calling convention in gcc. If I omit "asmlinkage" when defining a c function, does it imply _cdecl or fastcall?

(3)如果默认调用的CDECL,为什么asmlinkage需要约定,综合考虑CDECL等于asmlinkage修饰符? (我是正确的吗?)

(3) if the default calling convention is cdecl, why is asmlinkage needed, considering cdecl is equal to asmlinkage modifier? (am I correct here?)

(4)为什么这些系统调用函数全部用asmlinkage声明。我们可以将参数复制到寄存器,然后再调用这些系统调用的功能呢?从我的角度来看,在86,发出一个系统调用时,参数易于保存在寄存器中;又何苦然后保存在栈通过堆栈惯例来执行这样的传递参数​​?

(4) why are those system call functions all declared with asmlinkage. Can we copy the parameters to registers first, then call those system call functions? From my point of view, in x86, when issuing a system call, the parameters are readily saved in registers; then why bother to save then in stack to enforce such passing parameters via stack convention?

最后,有谁能够推荐一些资源/书籍,可以参考我对这种混合组装/ C编程?

Finally, can anybody recommend some resources/books that I can refer to for such mix assembly/c programming?

推荐答案

几个小时的研究之后,我得到了以下经验几点:

After several hours' research, I got the following empirical points:

(1)defning将从组装code调用这样的功能时,需要asmlinkage?

没有。实际上是经常使用的快速调用。

No. Actually the fastcall is frequently used.

例如,在entry_32.S,如果你搜索呼,就可以获取所有从这个汇编文件调用的C函数。然后,你可以看到,许多用快速调用,而不是asmlinkage的调用约定。例如,

For example, in entry_32.S, if you search "call", you can obtain all the c functions invoked from this assembly file. Then you can see, many use fastcall instead of asmlinkage as the calling convention. For example,

    /*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的约定。如果我定义一个C函数时省略了asmlinkage,这是否意味着_cdecl或FASTCALL?

(3)如果默认调用的CDECL,为什么asmlinkage需要约定,综合考虑CDECL等于asmlinkage修饰符? (我是正确的吗?)

有关不能从装配code调用C函数,我们可以放心地假设默认调用约定CDECL(或快速调用;也没关系,因为gcc会照顾的参数传递的主叫方和被叫方。默认调用约定可以在编译时指定)。然而,从组装code调用C函数,我们应该明确声明函数的调用约定,因为在组件侧的参数传递code已定。例如,如果patch_espfix_desc被声明为asmlinkage,然后将GCC编译功能检索栈参数。这是与组件侧,里面放的参数到寄存器不一致。

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

但我还不清楚何时使用asmlinkage以及何时使用FASTCALL。我真的需要一些指导和资源来参考一下。

这篇关于IS" asmlinkage"对于C函数需要从装配叫什么名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆