我正在尝试在 Linux (RedHat Enterprise 8) 中实现一个系统调用,但我对它的工作方式有点困惑。据我所知,我在用户模式下实现了一个包装器,它将系统调用号放在 eax 中,将参数放在 ebx、ecx、edx 等中,然后调用调用适当系统调用的 int 0x80。我的问题是,由于系统调用的编写方式与常规 C 函数类似,它如何知道哪些寄存器包含哪些参数?它是一个约定,还是有一个机制,如果是的话,它在哪里以及如何做?
编辑:这是一项家庭作业。我知道有系统调用宏可以为我做这些事情。
最佳答案
来自Linux Journal文章,第 2 页底部
Since the system call interface is exclusively register-parametered, six parameters at most can be used with a single system call. %eax is the syscall number; %ebx, %ecx, %edx, %esi, %edi and %ebp are the six generic registers used as param0-5; and %esp cannot be used because it's overwritten by the kernel when it enters ring 0 (i.e., kernel mode).
您的 c 代码可能看起来像是在进行系统调用,但实际上它调用了 libc 中的函数。该函数确保所有参数都在正确的寄存器中,然后进行中断。
关于linux - 系统调用如何知道包装函数将其参数放入何处?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2495945/