Profile photo for Davidlohr Bueso

The short answer to your question is that asmlinkage tells your compiler to look on the CPU stack for the function parameters, instead of registers. In fact, it uses GCC's regparam attribute (http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html), or syscall_linkage for IA64:

  1. #define asmlinkage CPP_ASMLINKAGE __attribute__((regparm(0))) 

The interesting part is why this is necessary. System calls are services that userspace can call to request the kernel to perform something for them (and therefore execute in kernel space). These functions are quite unorthodox in the sense that you cannot expect them to behave like normal functions, where parameters are typically passed by writing to the program stack, but instead they are written to registers. While still in userspace, calling a syscall requires writing certain values to certain registers is translated. The system call number (http://lxr.linux.no/linux+v3.5.4/arch/x86/syscalls/syscall_64.tbl) will always be written in eax, while the the rest of the parameters will go into ebx, ecx, etc. Take for example a call to sethostname(2), declared as int sethostname(char *name, size_t len), it will look something like:

  1. mov ecx, len ; amount of bytes in name 
  2. mov ebx, name ; address of name string 
  3. mov eax, 170 ; syscall number (sys_sethostname) 
  4. int 0x80 ; x86 call the kernel -- sysenter is another entry point 

With the int there is a software interrupt and the CPU switches to kernel mode, to then execute system_call(). Going into detail about this function is out of the scope of this question, so the most important thing you need to keep in mind is that it starts by saving all the registers in the CPU stack (eax, ebx, ecx, etc). After checking other things such as validating parameters, it will call the respective system call if everything is in order. In this example: sys_sethostname, defined as:

  1. SYSCALL_DEFINE2(sethostname, char __user *, name, int, len) 
  2. { 
  3. ... 
  4. } 

So, since all the information about the parameters passed all the way from userland to this point is nicely stored in the stack, the compiler must be instructed about this, hence the asmlinkage.

Note that even though a system call requires not parameters, such as fork(), the eax register will always be populated with the syscall number.

For more details about system calls and the bridge between userspace and kernel space, I recommend Understanding the Linux Kernel, 3rd Ed.
http://www.amazon.com/Understanding-Linux-Kernel-Third-Daniel/dp/0596005652/ref=sr_1_1?s=books&ie=UTF8&qid=1348053154&sr=1-1&keywords=understanding+the+linux+kernel

Source code for the sethostname function: http://lxr.linux.no/linux+v3.5.4/kernel/sys.c#L1365
Source code for x86
system_call: http://lxr.linux.no/linux+v3.5.4/arch/x86/kernel/entry_64.S#L495

View 2 other answers to this question
About · Careers · Privacy · Terms · Contact · Languages · Your Ad Choices · Press ·
© Quora, Inc. 2025