详解linux系统调用原理

2019-10-09 17:25:14丽君

编程实践

下面,通过一个简单的程序,看看应用程序如何在 用户态 准备参数并通过 int 指令触发 软中断 以陷入 内核态 执行 系统调用 :

hello_world-int.S

.section .rodata

msg:
 .ascii "Hello, world!n"

.section .text

.global _start

_start:
 # call SYS_WRITE
 movl $4, %eax
 # push arguments
 movl $1, %ebx
 movl $msg, %ecx
 movl $14, %edx
 int $0x80

 # Call SYS_EXIT
 movl $1, %eax
 # push arguments
 movl $0, %ebx
 # initiate
 int $0x80

这是一个汇编语言程序,程序入口在 _start 标签之后。

第 12 行,准备 系统调用号 :将常数 4 放进 寄存器 eax 。 系统调用号 4 代表 系统调用 SYS_write , 我们将通过该系统调用向标准输出写入一个字符串。

第 14-16 行, 准备系统调用参数:第一个参数放进 寄存器 ebx ,第二个参数放进 ecx , 以此类推。

write 系统调用需要 3 个参数:

文件描述符 ,标准输出文件描述符为 1 ; 写入内容(缓冲区)地址; 写入内容长度(字节数);

第 17 行,执行 int 指令触发软中断 0x80 ,程序将陷入内核态并由内核执行系统调用。 系统调用执行完毕后,内核将负责切换回用户态,应用程序继续执行之后的指令( 从 20 行开始 )。

第 20-24 行,调用 exit 系统调用,以便退出程序。

注解
注意到,这里必须显式调用 exit 系统调用退出程序。 否则,程序将继续往下执行,最终遇到段错误( segmentation fault )!

读者可能很好奇——我在写 C 语言或者其他程序时,这个调用并不是必须的!

这是因为 C 库( libc )已经帮你把脏活累活都干了。

接下来,我们编译并执行这个汇编语言程序:

$ ls
hello_world-int.S
$ as -o hello_world-int.o hello_world-int.S
$ ls
hello_world-int.o hello_world-int.S
$ ld -o hello_world-int hello_world-int.o
$ ls
hello_world-int hello_world-int.o hello_world-int.S
$ ./hello_world-int
Hello, world!

其实,将 系统调用号 和 调用参数 放进正确的 寄存器 并触发正确的 软中断 是个重复的麻烦事。 C 库已经把这脏累活给干了——试试 syscall 函数吧!

hello_world-syscall.c

#include <string.h>
#include <sys/syscall.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
 char *msg = "Hello, world!n";
 syscall(SYS_write, 1, msg, strlen(msg));

 return 0;
}