在没有堆栈的 Linux 上进行系统调用

发布于 2024-10-16 15:23:48 字数 182 浏览 4 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绮烟 2024-10-23 15:23:48

一般来说:不知道。即使在 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,对我来说效果很好:

$ cat syscall_sig.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>

#define __NR_nanosleep 35

static sig_atomic_t alrm = 0;

void handler(int sig)
{
    if (sig == SIGALRM)
        alrm = 1;
}

int main(void)
{
    stack_t ss;
    struct sigaction sa;
    struct timespec req, rem;
    long ret;

    ss.ss_flags = 0;
    ss.ss_size = SIGSTKSZ;
    ss.ss_sp = malloc(ss.ss_size);
    sigaltstack(&ss, NULL);

    memset(&sa, 0, sizeof(sa));
    sa.sa_handler = handler;
    sa.sa_flags = SA_ONSTACK;
    sigaction(SIGALRM, &sa, NULL);

    alarm(1);

    req.tv_sec = 5;
    req.tv_nsec = 0;
    asm("xorq $0x12345678, %%rsp ; syscall ; xorq $0x12345678, %%rsp"
        : "=a" (ret)
        : "0" (__NR_nanosleep), "D" (&req), "S" (&rem)
        : "rcx", "r11", "memory");

    printf("syscall return code %ld, alarm flag %d\n", ret, alrm);

    return 0;
}

$ gcc -Wall -o syscall_sig syscall_sig.c
$ ./syscall_sig
syscall return code -4, alarm flag 1
$ 

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 the syscall 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 the syscall, works fine for me:

$ cat syscall_sig.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>

#define __NR_nanosleep 35

static sig_atomic_t alrm = 0;

void handler(int sig)
{
    if (sig == SIGALRM)
        alrm = 1;
}

int main(void)
{
    stack_t ss;
    struct sigaction sa;
    struct timespec req, rem;
    long ret;

    ss.ss_flags = 0;
    ss.ss_size = SIGSTKSZ;
    ss.ss_sp = malloc(ss.ss_size);
    sigaltstack(&ss, NULL);

    memset(&sa, 0, sizeof(sa));
    sa.sa_handler = handler;
    sa.sa_flags = SA_ONSTACK;
    sigaction(SIGALRM, &sa, NULL);

    alarm(1);

    req.tv_sec = 5;
    req.tv_nsec = 0;
    asm("xorq $0x12345678, %%rsp ; syscall ; xorq $0x12345678, %%rsp"
        : "=a" (ret)
        : "0" (__NR_nanosleep), "D" (&req), "S" (&rem)
        : "rcx", "r11", "memory");

    printf("syscall return code %ld, alarm flag %d\n", ret, alrm);

    return 0;
}

$ gcc -Wall -o syscall_sig syscall_sig.c
$ ./syscall_sig
syscall return code -4, alarm flag 1
$ 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文