2012-04-08 8 views
3

linuxカーネルのアセンブリコードから呼び出されるC関数を記述する必要があります。アセンブリコードから呼び出されたC関数の記述方法

特別な問題を考慮する必要がありますか?

私は心の中でいくつかを持っていますが、誰もがより多くの詳細を提供することができます

(1)Cでのアセンブリ内の呼び出し元と呼び出し先がよく手を振っていることを確認してください大会

を呼び出します。しかし、どの呼び出し規約を使うべきですか?どのようにしてc関数を宣言し、アセンブリコードで宣言できますか?

(2)を保護レジスタ

いくつかのレジスタが起動する前アセンブリに保存されるべきです。私は大まかに彼らがeax、ecx、edxであることを覚えています。しかし、C関数への呼び出しの前に古い値を参照する必要がなければ、それらを保存する必要はありません。

その他の問題は何ですか?

+1

"Linuxカーネルのアセンブリコードから呼び出されるC関数を記述する必要があります。" - 何を止めているの? –

+0

@MitchWheat具体的には、c関数を宣言するときの "asmlinkage"の正確な使用法はわかりません。私はそれを使用する必要がありますか?他の選択肢はありますか?何が意味ですか? – Infinite

+1

まずはしないようにしてください。 Linuxはいくつかの場所でアセンブリを使用していますが、混乱するのは難しいことです。私はその問題がどのアセンブリコードであるかに依存していると思います。割り込みハンドラの初期段階、または初期起動段階(Cが使用される場所の2つの例)では、2つの異なる動作が実行されます。 – ugoren

答えて

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

+0

まあ、私はちょうど見るべき場所を知っています:)本については、ここで同様の質問を検索することができます。それ以外にも、常にLinuxカーネルソース自体を見ることができます。また、質問は大歓迎です。 –

+0

x86_64呼び出し規約の主な参照先はhttp://www.x86-64.org/documentation/abi.pdf( "the ABI" - _Application Binary Interface_)です。これは、呼び出し規約と複合データ( 'struct')と同様に 'プリミティブ'型レイアウト/サイズ/配置制約をリストします。 Linuxカーネルで64ビットx86で使用されている規約は、これに非常に近いものです。しかし、32ビットでは、カーネルソースが便利な場所であるgcc -mregparmを使用しているため、ユーザー空間ABIとはかなり異なっています(好奇心が強い場合は_gabi.pdf_を検索してください)。 –

関連する問題