当前位置: 技术问答>linux和unix
Linux系统调用的参数传递方式
来源: 互联网 发布时间:2016-11-19
本文导语: 先引用一段: “仔细看一下有asmlinkage的地方通常是系统调用的函数,因为在系统调用中,寄存器从用户空间传过来后SAVE_ALL压入堆栈,接着调用相应的系统调用函数,这样系统调用函数一定要保证是通过堆栈传递参...
先引用一段:
“仔细看一下有asmlinkage的地方通常是系统调用的函数,因为在系统调用中,寄存器从用户空间传过来后SAVE_ALL压入堆栈,接着调用相应的系统调用函数,这样系统调用函数一定要保证是通过堆栈传递参数的
转贴一段:
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.”
我的问题是,系统调用的参数传递方式是不是把参数(不一定满4个)放进寄存器里,然后把对应的寄存器压栈,INT X,实际上又把X压栈,下陷后CPU取出了X后找到了对应服务程序,那个服务程序才取出栈中的参数?
也就是说是把参数放到寄存器里,再放到栈中来传递的?
是的话它是把四个寄存器一下全压栈还是按需要压栈?
为什么不直接放到栈中?为什么必须经过寄存器?为什么不能分批把参数放进一个寄存器一次一次压栈?
“仔细看一下有asmlinkage的地方通常是系统调用的函数,因为在系统调用中,寄存器从用户空间传过来后SAVE_ALL压入堆栈,接着调用相应的系统调用函数,这样系统调用函数一定要保证是通过堆栈传递参数的
转贴一段:
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.”
我的问题是,系统调用的参数传递方式是不是把参数(不一定满4个)放进寄存器里,然后把对应的寄存器压栈,INT X,实际上又把X压栈,下陷后CPU取出了X后找到了对应服务程序,那个服务程序才取出栈中的参数?
也就是说是把参数放到寄存器里,再放到栈中来传递的?
是的话它是把四个寄存器一下全压栈还是按需要压栈?
为什么不直接放到栈中?为什么必须经过寄存器?为什么不能分批把参数放进一个寄存器一次一次压栈?
|
系统调用是在内核中执行的,参数只能压入内核栈
在进入内核之前压栈只是压入用户态栈,所以需要寄存器传递
int x是用户态和内核态的分界线
在进入内核之前压栈只是压入用户态栈,所以需要寄存器传递
int x是用户态和内核态的分界线