我需要編寫一個C函數,該函數將從Linux內核中的彙編代碼調用。如何編寫從彙編代碼調用的C函數
應該考慮哪些特殊問題?
我有一些想法,但有誰能夠提供更多的細節:
(1)調用約定
確保在裝配呼叫者和在C握手言和被叫方好。但是我應該使用哪種調用約定?我如何聲明c函數並在彙編代碼中聲明它?
(2)保護的寄存器
某些寄存器應的調用之前被保存在組件。我大致記得他們是eax,ecx和edx。但我不必保存它們,除非我需要在調用C函數之前引用舊值,對吧?
還有什麼其他問題?
我需要編寫一個C函數,該函數將從Linux內核中的彙編代碼調用。如何編寫從彙編代碼調用的C函數
應該考慮哪些特殊問題?
我有一些想法,但有誰能夠提供更多的細節:
(1)調用約定
確保在裝配呼叫者和在C握手言和被叫方好。但是我應該使用哪種調用約定?我如何聲明c函數並在彙編代碼中聲明它?
(2)保護的寄存器
某些寄存器應的調用之前被保存在組件。我大致記得他們是eax,ecx和edx。但我不必保存它們,除非我需要在調用C函數之前引用舊值,對吧?
還有什麼其他問題?
由於你的任務標記爲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.
太棒了!這讓我很困惑。你能告訴我你是如何找到這麼好的文件的嗎?一個愚蠢的問題。但嚴重的是,我查了幾個小時,但沒有找到這樣的文件。順便說一句:你能建議一些可以指導內核編程的書籍/鏈接,尤其是低級時尚嗎?非常感謝! – Infinite 2012-04-08 15:33:34
嗯,我只知道在哪裏看:)說到這些書你可以在這裏搜索類似的問題。除此之外,你總是可以看看Linux內核源碼本身。另外,歡迎提出問題。 – 2012-04-08 16:15:58
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
「我需要編寫一個C函數,它將從Linux內核的彙編代碼中調用。」 - 什麼東西阻止你? – 2012-04-08 08:00:24
@MitchWheat具體來說,在聲明c函數時我不知道「asmlinkage」的確切用法。我必須使用它嗎?還有其他的選擇嗎?含義是什麼? – Infinite 2012-04-08 08:02:52
首先 - 儘量不要這樣做。 Linux在幾個地方使用匯編,而且它們都很棘手。我認爲這些問題取決於它是什麼彙編代碼。在中斷處理程序的早期階段或早期啓動階段(使用C的兩個地方)運行是兩個不同的事情。 – ugoren 2012-04-08 09:34:16