[Solved] What is the ‘asmlinkage’ modifier meant for?

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?

Enquirer: gjain


Solution #1:

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.

Respondent: dirkgently

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.