在没有堆栈的 Linux 上进行系统调用
在 Linux i386 上,int $0x80
系统调用 ABI 使得无需有效的用户空间堆栈即可轻松执行系统调用。另一方面,vdso/vsyscall 接口需要访问堆栈。其他 Linux 移植版在这方面表现如何,尤其是 x86_64?他们有办法在没有堆栈的情况下进行系统调用吗?是否有关于每个架构可用的系统调用方法的参考?
On Linux i386, the int $0x80
syscall ABI makes it easy to perform syscalls without having a valid userspace stack. The vdso/vsyscall interface, on the other hand, requires access to a stack. How do other Linux ports fare in this regard, especially x86_64? Do they have ways to make syscalls without a stack? Is there a reference on the available syscall methods for each arch?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说:不知道。即使在 i386 上,如果有第 6 个参数,它也必须在堆栈上传递(例如对于
mmap
)。对于 x86_64 特别是:将系统调用号放入
%rax
(注意:系统调用号的分配方式与 32 位完全不同),%rdi
中最多 6 个参数、%rsi
、%rdx
、%r10
、%r8
和%r9
(这几乎但不完全与寄存器中参数传递的常用 ABI 相同 - 请注意使用%r10
而不是%rcx
),并使用 <代码>系统调用指令。结果在%rax
中返回,并且%rcx
和%r11
被破坏。x86_64 ABI 信息可以在 http://www.x86-64.org/documentation/ 找到abi.pdf; Linux ABI 记录在附录中。 (如果在其他地方查找 x86_64 ABI 信息,请注意 64 位 Windows 使用自己不同的 ABI。)
我不认为
syscall
工作对用户堆栈框架有任何要求适当地。在被信号中断的情况下,处理程序显然需要一个健全的堆栈;但以下实验使用备用信号堆栈并故意在syscall
周围丢弃%rsp
,对我来说效果很好:In general: no idea. Even on i386, if there is a 6th argument, it must be passed on the stack (e.g. for
mmap
).For x86_64 specifically: put the syscall number in
%rax
(beware: the syscall numbers are allocated completely differently to the 32-bit ones), up to 6 arguments in%rdi
,%rsi
,%rdx
,%r10
,%r8
and%r9
(which is almost, but not quite, the same as the usual ABI for parameter passing in registers - note use of%r10
instead of%rcx
), and use thesyscall
instruction. The result is returned in%rax
, and%rcx
and%r11
are clobbered.x86_64 ABI information can be found at http://www.x86-64.org/documentation/abi.pdf; the Linux ABI is documented in the appendix. (And if looking around elsewhere for x86_64 ABI info, be aware that 64-bit Windows uses its own different ABI.)
I don't believe there is any requirement on the user stack frame for
syscall
to work properly. In the case of being interrupted by a signal, a sane stack is obviously required for the handler; but the following experiment, which uses an alternate signal stack and deliberately trashes%rsp
around thesyscall
, works fine for me: