53

I have read that it is used for functions that implement system calls in Linux. For example:

asmlinkage long sys_getjiffies( void )
{
  return (long)get_jiffies_64();
}

and that it tells the compiler to pass all function arguments on the stack. But isn't that already the case? Function arguments are generally passed by pushing them on stack only. Or is it that we are referring to passing function arguments through registers here?

gjain
  • 4,468
  • 5
  • 39
  • 47
  • 5
    Note that the first parameters of the Linux kernel functions are passed in registers on x86 by default. On x86-32 the first 3 parameters are passed in `%eax`, `%edx` and `%ecx`, in that order, the rest go on stack. The functions with variable argument lists are an exception, they get all their arguments on stack on x86-32. On x86-64, the first 6 parameters are passed in `%rdi`, `%rsi`, `%rdx`, `%rcx`, `%r8`, `%r9` (even for the functions with varargs), in that order, the rest - on the stack. The system calls follow different conventions though. – Eugene May 05 '12 at 08:10
  • (continued) As @dirkgently noted, `asmlinkage` is a way to override thу default conventions on parameter passing. See also a detailed description of various calling conventions in [Agner Fog's manual](http://www.agner.org/optimize/calling_conventions.pdf). – Eugene May 05 '12 at 08:15
  • 1
    There is a good explanation in Quora: [https://www.quora.com/Linux-Kernel/What-does-asmlinkage-mean-in-the-definition-of-system-calls](https://www.quora.com/Linux-Kernel/What-does-asmlinkage-mean-in-the-definition-of-system-calls). – Nan Xiao Oct 30 '15 at 09:24

1 Answers1

49

There's a FAQ:

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.

It is also used to allow calling a function from assembly files.

coanor
  • 3,746
  • 4
  • 50
  • 67
dirkgently
  • 108,024
  • 16
  • 131
  • 187
  • 15
    The reason is that anyhow the kernel needs to save all the registers onto stack (in order to restore the environment before returning to the userspace) when handling the system call requests from userspace, so after that the parameters are available on stack. I.e., it doesn't need extra effort. source: http://stackoverflow.com/questions/10060168/is-asmlinkage-required-for-a-c-function-to-be-called-from-assembly – kumar Jun 20 '12 at 10:30