如何在内联汇编中通过 syscall 或 sysenter 调用系统调用? [英] How to invoke a system call via syscall or sysenter in inline assembly?

查看:42
本文介绍了如何在内联汇编中通过 syscall 或 sysenter 调用系统调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在x86 Linux下如何直接使用sysenter/syscall实现系统调用?有人可以提供帮助吗?如果能把amd64平台的代码也展示一下就更好了.

How can we implement the system call using sysenter/syscall directly in x86 Linux? Can anybody provide help? It would be even better if you can also show the code for amd64 platform.

我知道在 x86 中,我们可以使用

I know in x86, we can use

__asm__(
"               movl $1, %eax  
"
"               movl $0, %ebx 
"
"               call *%gs:0x10 
"
);

间接路由到 sysenter.

to route to sysenter indirectly.

但是我们如何直接使用 sysenter/syscall 进行编码以发出系统调用?

But how can we code using sysenter/syscall directly to issue a system call?

我找到了一些材料 http://damocles.blogbus.com/tag/sysenter/.但还是觉得很难弄明白.

I find some material http://damocles.blogbus.com/tag/sysenter/ . But still find it difficult to figure out.

推荐答案

首先,你不能安全地使用 GNU C Basic asm(""); 语法来解决这个问题(没有输入/输出/clobber 约束).您需要扩展 asm 来告诉编译器您修改的寄存器.请参阅GNU C 手册中的内联汇编inline-assembly tag wiki 链接到其他指南,了解有关 "D" 等内容的详细信息(1) 表示作为 asm() 语句的一部分.

First of all, you can't safely use GNU C Basic asm(""); syntax for this (without input/output/clobber constraints). You need Extended asm to tell the compiler about registers you modify. See the inline asm in the GNU C manual and the inline-assembly tag wiki for links to other guides for details on what things like "D"(1) means as part of an asm() statement.

我将向您展示如何通过编写一个程序来执行系统调用,该程序使用 write() 系统调用将 Hello World! 写入标准输出.这是没有实现实际系统调用的程序源代码:

I'm going to show you how to execute system calls by writing a program that writes Hello World! to standard output by using the write() system call. Here's the source of the program without an implementation of the actual system call :

#include <sys/types.h>

ssize_t my_write(int fd, const void *buf, size_t size);

int main(void)
{
    const char hello[] = "Hello world!
";
    my_write(1, hello, sizeof(hello));
    return 0;
}

您可以看到我将自定义系统调用函数命名为 my_write 以避免与 libc 提供的正常"write 发生名称冲突.此答案的其余部分包含 i386 和 amd64 的 my_write 源.

You can see that I named my custom system call function as my_write in order to avoid name clashes with the "normal" write, provided by libc. The rest of this answer contains the source of my_write for i386 and amd64.

i386 Linux 中的系统调用是使用第 128 个中断向量实现的,例如当然,通过在汇编代码中调用 int 0x80 并相应地设置参数.也可以通过 SYSENTER 来做同样的事情,但实际执行这条指令是通过 VDSO 虚拟映射到每个正在运行的进程来实现的.由于 SYSENTER 从来没有作为 int 0x80 API 的直接替代品,它永远不会由用户级应用程序直接执行 - 相反,当应用程序需要访问某些内核代码时,它调用 VDSO 中的虚拟映射例程(这就是代码中 call *%gs:0x10 的用途),其中包含支持 SYSENTER 指令的所有代码.由于指令的实际运作方式,有相当多的内容.

System calls in i386 Linux are implemented using the 128th interrupt vector, e.g. by calling int 0x80 in your assembly code, having set the parameters accordingly beforehand, of course. It is possible to do the same via SYSENTER, but actually executing this instruction is achieved by the VDSO virtually mapped to each running process. Since SYSENTER was never meant as a direct replacement of the int 0x80 API, it's never directly executed by userland applications - instead, when an application needs to access some kernel code, it calls the virtually mapped routine in the VDSO (that's what the call *%gs:0x10 in your code is for), which contains all the code supporting the SYSENTER instruction. There's quite a lot of it because of how the instruction actually works.

如果您想了解更多相关信息,请查看此链接.它包含对在内核和 VDSO 中应用的技术的相当简要的概述.另见 (x86) Linux 系统调用权威指南 - 一些系统调用如 getpidclock_gettime 非常简单,内核可以导出代码 + 运行的数据在用户空间中,因此 VDSO 永远不需要进入内核,使其比 sysenter 更快.​​

If you want to read more about this, have a look at this link. It contains a fairly brief overview of the techniques applied in the kernel and the VDSO. See also The Definitive Guide to (x86) Linux System Calls - some system calls like getpid and clock_gettime are so simple the kernel can export code + data that runs in user-space so the VDSO never needs to enter the kernel, making it much faster even than sysenter could be.

使用较慢的 int $0x80 调用 32 位 ABI 会容易得多.

It's much easier to use the slower int $0x80 to invoke the 32-bit ABI.

// i386 Linux
#include <asm/unistd.h>      // compile with -m32 for 32 bit call numbers
//#define __NR_write 4
ssize_t my_write(int fd, const void *buf, size_t size)
{
    ssize_t ret;
    asm volatile
    (
        "int $0x80"
        : "=a" (ret)
        : "0"(__NR_write), "b"(fd), "c"(buf), "d"(size)
        : "memory"    // the kernel dereferences pointer args
    );
    return ret;
}

如您所见,使用 int 0x80 API 相对简单.系统调用的编号进入eax寄存器,系统调用所需的所有参数分别进入ebxecxedxesiediebp.系统调用号可以通过读取文件/usr/include/asm/unistd_32.h获得.

As you can see, using the int 0x80 API is relatively simple. The number of the syscall goes to the eax register, while all the parameters needed for the syscall go into respectively ebx, ecx, edx, esi, edi, and ebp. System call numbers can be obtained by reading the file /usr/include/asm/unistd_32.h.

函数的原型和描述可以在手册的第二部分找到,所以在这种情况下write(2).

Prototypes and descriptions of the functions are available in the 2nd section of the manual, so in this case write(2).

内核保存/恢复所有寄存器(EAX 除外),因此我们可以将它们用作内联汇编的仅输入操作数.请参阅什么是 UNIX & 的调用约定Linux 系统调用在 i386 和 x86-64 上

The kernel saves/restores all the registers (except EAX) so we can use them as input-only operands to the inline asm. See What are the calling conventions for UNIX & Linux system calls on i386 and x86-64

请记住,clobber 列表还包含 memory 参数,这意味着指令列表中列出的指令引用内存(通过 buf 参数).(内联 asm 的指针输入并不意味着指向的内存也是输入.参见 如何指示可以使用内联 ASM 参数*指向*的内存?)

Keep in mind that the clobber list also contains the memory parameter, which means that the instruction listed in the instruction list references memory (via the buf parameter). (A pointer input to inline asm does not imply that the pointed-to memory is also an input. See How can I indicate that the memory *pointed* to by an inline ASM argument may be used?)

AMD64 架构的情况看起来有所不同,该架构支持一个名为 SYSCALL 的新指令.它与原始的 SYSENTER 指令非常不同,并且在用户应用程序中使用起来肯定要容易得多 - 实际上它真的很像一个普通的 CALL,并且改编了旧的 int 0x80 到新的 SYSCALL 非常简单.(除了它使用 RCX 和 R11 而不是内核堆栈来保存用户空间的 RIP 和 RFLAGS,以便内核知道从哪里返回).

Things look different on the AMD64 architecture which sports a new instruction called SYSCALL. It is very different from the original SYSENTER instruction, and definitely much easier to use from userland applications - it really resembles a normal CALL, actually, and adapting the old int 0x80 to the new SYSCALL is pretty much trivial. (Except it uses RCX and R11 instead of the kernel stack to save the user-space RIP and RFLAGS so the kernel knows where to return).

在这种情况下,系统调用的编号仍然在寄存器 rax 中传递,但用于保存参数的寄存器现在几乎符合函数调用约定:rdirsirdxr10r8r9.(syscall 本身会破坏 rcx 所以使用 r10 而不是 rcx,让 libc 包装函数只使用 mov r10, rcx/syscall.)

In this case, the number of the system call is still passed in the register rax, but the registers used to hold the arguments now nearly match the function calling convention: rdi, rsi, rdx, r10, r8 and r9 in that order. (syscall itself destroys rcx so r10 is used instead of rcx, letting libc wrapper functions just use mov r10, rcx / syscall.)

// x86-64 Linux
#include <asm/unistd.h>      // compile without -m32 for 64 bit call numbers
// #define __NR_write 1
ssize_t my_write(int fd, const void *buf, size_t size)
{
    ssize_t ret;
    asm volatile
    (
        "syscall"
        : "=a" (ret)
        //                 EDI      RSI       RDX
        : "0"(__NR_write), "D"(fd), "S"(buf), "d"(size)
        : "rcx", "r11", "memory"
    );
    return ret;
}

(在 Godbolt)

请注意实际上唯一需要更改的是寄存器名称和用于进行调用的实际指令.这主要归功于 gcc 的扩展内联汇编语法提供的输入/输出列表,它会自动提供执行指令列表所需的适当移动指令.

Do notice how practically the only thing that needed changing were the register names, and the actual instruction used for making the call. This is mostly thanks to the input/output lists provided by gcc's extended inline assembly syntax, which automagically provides appropriate move instructions needed for executing the instruction list.

"0"(callnum) 匹配约束可以写成 "a" 因为操作数 0 ("=a"(ret)代码>输出)只有一个寄存器可供选择;我们知道它会选择 EAX.使用您认为更清楚的那个.

The "0"(callnum) matching constraint could be written as "a" because operand 0 (the "=a"(ret) output) only has one register to pick from; we know it will pick EAX. Use whichever you find more clear.

请注意,非 Linux 操作系统(如 MacOS)使用不同的编号.甚至还有不同的 32 位 arg 传递约定.

Note that non-Linux OSes, like MacOS, use different call numbers. And even different arg-passing conventions for 32-bit.

这篇关于如何在内联汇编中通过 syscall 或 sysenter 调用系统调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆