2012-04-08 177 views
3

我需要编写一个C函数,该函数将从Linux内核中的汇编代码调用。如何编写从汇编代码调用的C函数

应该考虑哪些特殊问题?

我有一些想法,但有谁能够提供更多的细节:

(1)调用约定

确保在装配呼叫者和在C握手言和被叫方好。但是我应该使用哪种调用约定?我如何声明c函数并在汇编代码中声明它?

(2)保护的寄存器

某些寄存器应的调用之前被保存在组件。我大致记得他们是eax,ecx和edx。但我不必保存它们,除非我需要在调用C函数之前引用旧值,对吧?

还有什么其他问题?

+1

“我需要编写一个C函数,它将从Linux内核的汇编代码中调用。” - 什么东西阻止你? – 2012-04-08 08:00:24

+0

@MitchWheat具体来说,在声明c函数时我不知道“asmlinkage”的确切用法。我必须使用它吗?还有其他的选择吗?含义是什么? – Infinite 2012-04-08 08:02:52

+1

首先 - 尽量不要这样做。 Linux在几个地方使用汇编,而且它们都很棘手。我认为这些问题取决于它是什么汇编代码。在中断处理程序的早期阶段或早期启动阶段(使用C的两个地方)运行是两个不同的事情。 – ugoren 2012-04-08 09:34:16

回答

2

由于你的任务标记为linux-kernel请看下面的内核头文件:arch/x86/include/asm/calling.h

3 x86 function call convention, 64-bit: 
    4 ------------------------------------- 
    5 arguments   | callee-saved  | extra caller-saved | return 
    6 [callee-clobbered] |     | [callee-clobbered] | 
    7 --------------------------------------------------------------------------- 
    8 rdi rsi rdx rcx r8-9 | rbx rbp [*] r12-15 | r10-11    | rax, rdx [**] 
    9 
    10 (rsp is obviously invariant across normal function calls. (gcc can 'merge' 
    11 functions when it sees tail-call optimization possibilities) rflags is 
    12 clobbered. Leftover arguments are passed over the stack frame.) 
    13 
    14 [*] In the frame-pointers case rbp is fixed to the stack frame. 
    15 
    16 [**] for struct return values wider than 64 bits the return convention is a 
    17  bit more complex: up to 128 bits width we return small structures 
    18  straight in rax, rdx. For structures larger than that (3 words or 
    19  larger) the caller puts a pointer to an on-stack return struct 
    20  [allocated in the caller's stack frame] into the first argument - i.e. 
    21  into rdi. All other arguments shift up by one in this case. 
    22  Fortunately this case is rare in the kernel. 
    23 
    24 For 32-bit we have the following conventions - kernel is built with 
    25 -mregparm=3 and -freg-struct-return: 
    26 
    27 x86 function calling convention, 32-bit: 
    28 ---------------------------------------- 
    29 arguments   | callee-saved  | extra caller-saved | return 
    30 [callee-clobbered] |      | [callee-clobbered] | 
    31 ------------------------------------------------------------------------- 
    32 eax edx ecx  | ebx edi esi ebp [*] | <none>    | eax, edx [**] 
    33 
    34 (here too esp is obviously invariant across normal function calls. eflags 
    35 is clobbered. Leftover arguments are passed over the stack frame.) 
    36 
    37 [*] In the frame-pointers case ebp is fixed to the stack frame. 
    38 
    39 [**] We build with -freg-struct-return, which on 32-bit means similar 
    40  semantics as on 64-bit: edx can be used for a second return value 
    41  (i.e. covering integer and structure sizes up to 64 bits) - after that 
    42  it gets more complex and more expensive: 3-word or larger struct returns 
    43  get done in the caller's frame and the pointer to the return struct goes 
    44  into regparm0, i.e. eax - the other arguments shift up and the 
    45  function's register parameters degenerate to regparm=2 in essence. 
+0

太棒了!这让我很困惑。你能告诉我你是如何找到这么好的文件的吗?一个愚蠢的问题。但严重的是,我查了几个小时,但没有找到这样的文件。顺便说一句:你能建议一些可以指导内核编程的书籍/链接,尤其是低级时尚吗?非常感谢! – Infinite 2012-04-08 15:33:34

+0

嗯,我只知道在哪里看:)说到这些书你可以在这里搜索类似的问题。除此之外,你总是可以看看Linux内核源码本身。另外,欢迎提出问题。 – 2012-04-08 16:15:58

+0

x86_64调用约定的主要参考是http://www.x86-64.org/documentation/abi.pdf(“ABI” - _Application Binary Interface_)。它列出了调用约定和组合数据('struct')以及'原始'类型布局/大小/对齐约束。 Linux内核在64位x86中使用的约定与此非常接近。然而,在32位版本中,它们与用户空间ABI(如果您好奇的话,在网上搜索_gabi.pdf_)完全不同,因为使用了'gcc -mregparm' - 这就是内核源代码派上用场的地方。 – 2012-05-31 17:13:27