4

I have a need to be able to call a custom function defined in the linux kernel somewhere. What exactly needs to be done to create some function:

void custom_function(int arg);

where arg is the system call number and then somewhere in entry_32.S be able to do:

call custom_function

AShelly
  • 34,686
  • 15
  • 91
  • 152
Erica
  • 41
  • 1

1 Answers1

1

As the stack handling of assembly function and C function are different. The assembly code "callq" can not directly call C routines, need a piece of code to put assembly parameter into C stack before calling. The macro "asmlinkage", defined in Linux kernel source, is used to tell compiler the function is prepared to called from assembly and the compiler will add some parameter placement code at head of this C function.

So, things you have to do is:

At caller side

movq <arg6>,%r9     /* 6th arg */
movq <arg5>,%r8     /* 5th arg */
movq <arg4>,%rcx    /* 4th arg */
movq <arg3>,%rdx    /* 3rd arg */
movq <arg2>,%rsi    /* 2nd arg */
movq <arg1>,%rdi    /* 1st arg*/
callq <your-function-name>
movq %rax, <buf-to-return-result>  /* return value */

At callee side:

asmlinkage int my-function(int arg1, int arg2, int arg3, ...) {
   <your code>;
   return 0;
}
Houcheng
  • 2,674
  • 25
  • 32