操作系统 - 系统调用

系统调用概述

系统调用:运行在用户空间的程序向操作系统内核请求需要更高权限运行的服务。系统调用提供用户程序与操作系统之间的接口。

系统调用需要了解的点:

  • 系统调用将处理器从用户态切换到核心态,以便 CPU 访问受到保护的内核内存。

  • 每个系统调用都由一个唯一的数字来标识。

  • 系统调用可以有一套参数,用于用户空间与内核空间之间相互传递信息。(x86-64中最多使用寄存器传递6个参数,参考资料如下:

    x86-32 [Free|Open|Net|DragonFly]BSD UNIX System Call convention:

    Parameters are passed on the stack. Push the parameters (last parameter pushed first) on to the stack. Then push an additional 32-bit of dummy data (Its not actually dummy data. refer to following link for more info) and then give a system call instruction int $0x80

    x86-64 Linux System Call convention:

    1. User-level applications use as integer registers for passing the sequence %rdi, %rsi, %rdx, %rcx, %r8 and %r9. The kernel interface uses %rdi, %rsi, %rdx, %r10, %r8 and %r9.
    2. A system-call is done via the syscall instruction. This clobbers %rcx and %r11 as well as the %rax return value, but other registers are preserved.
    3. The number of the syscall has to be passed in register %rax.
    4. System-calls are limited to six arguments, no argument is passed directly on the stack.
    5. Returning from the syscall, register %rax contains the result of the system-call. A value in the range between -4095 and -1 indicates an error, it is -errno.
    6. Only values of class INTEGER or class MEMORY are passed to the kernel.

系统调用的详细过程

以x86-32为例,说明系统调用过程:

  1. 通过调用外壳(wrapper)函数发起系统调用(glibc)。

  2. 参数通过堆栈传入外壳函数,外壳函数会将上述参数复制到寄存器(系统调用最多使用6个)。

  3. 外壳函数将系统调用编号复制到eax寄存器中。

  4. 外壳函数执行中断机器指令(int 0x80),引发处理器从用户态切换到核心态,并执行系统中断0x80(十进制数128)的中断向量所指向的代码。

  5. 为响应中断0x80,内核会调用system_call()例程(位于汇编文件arch/i386/entry.S中)来处理中断,具体如下:

    5.1 在内核栈中保存寄存器值。

    5.2 审核系统调用编号是否有效。

    5.3 如果调用号有效,就去存放所有调用服务例程的列表中进行索引,发现并调用相应的系统调用服务例程。

    5.4 从内核栈中恢复各寄存器值,并将系统调用返回值置于栈中。

    5.5 返回至外壳函数,同时将处理器切换回用户态。

  6. 若系统调用服务例程的返回值表明调用有误,外壳函数会使用该值来设置全局变量 errno。然后,外壳函数会返回到调用程序,并同时返回一个整型值,以表明系统调用是否成功。

image-20210918223613321

其他

Linux常见的系统调用

跟踪系统调用

Linux可以在命令行中使用 strace追踪系统调用 ,ltrace追踪库函数的调用。

Mac OS可以使用dtruss追踪系统调用。

reference

[1] UNIX 系统编程手册

[2] 现代操作系统

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值