linux核心 asmlinkage宏

來源:互聯網
上載者:User
asmlinkage是個宏,使用它是為了保持參數在stack中。

看一下/usr/include/asm/linkage.h裡面的定義:
#define asmlinkage CPP_ASMLINKAGE __attribute__((regparm(0)))

其中 __attribute__是關鍵字,是gcc的C語言擴充。

__attribute__機制是GNU C的一大特色,它可以設定函數屬性、變數屬性和類型屬性等。可以通過它們向編譯器提供更多資料,協助編譯器執行最佳化等。
__attribute__((regparm(0))):告訴gcc編譯器該函數不需要通過任何寄存器來傳遞參數,參數只是通過堆棧來傳遞。
__attribute__((regparm(3))):告訴gcc編譯器這個函數可以通過寄存器傳遞多達3個的參數,這3個寄存器依次為EAX、EDX 和 ECX。更多的參數才通過堆棧傳遞。這樣可以減少一些入棧出棧操作,因此調用比較快。

asmlinkage大都用在系統調用中。有一些情況下是需要明確的告訴編譯器,我們是使用stack來傳遞參數的,比如X86中的系統調用,是先將參數壓入stack以後調用sys_*函數的,所以所有的sys_*函數都有asmlinkage來告訴編譯器不要使用寄存器來編譯,

The asmlinkage tag is one other thing that we should observe about this simple function. This is a #define for some gcc magic that tells the compiler that the function should not expect to find any of its arguments in registers (a common optimization), but only on the CPU's stack. Recall our earlier assertion that system_call consumes its first argument, the system call number, and allows up to four more arguments that are passed along to the real system call. system_call achieves this feat simply by leaving its other arguments (which were passed to it in registers) on the stack. All system calls are marked with the asmlinkage tag, so they all look to the stack for arguments. Of course, in sys_ni_syscall's case, this doesn't make any difference, because sys_ni_syscall doesn't take any arguments, but it's an issue for most other system calls. And, because you'll be seeing asmlinkage in front of many other functions, I thought you should know what it was about.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.