Syscall Details
Syscall Details
July 27, 2025
什么是操作系统?
- 两个世界:用户态(user space)和内核态(kernel space)。用户态是应用程序运行的地方,内核态是操作系统核心代码运行的地方。用户态程序不能直接访问硬件资源或执行特权操作,而内核态程序可以。
- cpu privilege levels:CPU 有不同的特权级别(ring),通常用户态是 ring 3,内核态是 ring 0。特权级别决定了程序可以执行哪些指令和访问哪些资源。
- 两个世界之间的通信:用户态程序通过系统调用(syscall)请求内核提供服务。系统调用是用户态和内核态之间的接口,允许用户程序安全地访问操作系统功能。
os 初始状态 + syscalls
When a user program issues a system call, the operating system (OS) and hardware collaborate to transition from user mode to kernel mode, execute privileged operations, and safely return control to the user program.
1. Issuing the System Call
- User Program Action: The program invokes a system call (e.g.,
read()orwrite()) via a library function (e.g.,libc). - Setup: The library function prepares:
- System Call Number: Identifies the requested service (e.g., stored in
RAXon x86-64). - Arguments: Passed via registers (e.g.,
RDI,RSI,RDXon x86-64).
- System Call Number: Identifies the requested service (e.g., stored in
- Trigger: Executes a hardware instruction (e.g.,
syscallon x86-64,svcon ARM, orint 0x80for legacy x86) to switch to kernel mode.
2. Hardware Transition to Kernel Mode
- Privilege Elevation: The CPU switches from user mode (limited privileges) to kernel mode (full privileges).
- Interrupt Handling:
- The CPU consults the Interrupt Descriptor Table (IDT) or system call entry point to locate the kernel’s handler.
- The Memory Management Unit (MMU) enforces memory protection, ensuring user programs cannot access kernel memory.
- Context Save:
- The CPU saves the user program’s state (registers, program counter, stack pointer, flags) to the kernel stack (not the user stack).
- The kernel stack is per-process and isolated for security.
3. Kernel Executes the System Call
- Handler Invocation: The kernel’s system call dispatcher uses the system call number to jump to the appropriate handler (e.g.,
sys_read()orsys_write()). - Privileged Operations:
- The kernel performs tasks like I/O, file access, or memory allocation, interacting directly with hardware if needed.
- If the operation blocks (e.g., waiting for disk data), the process may be paused, and the scheduler switches to another task.
- Result Preparation: The return value (or error code) is stored in a register (e.g.,
RAXon x86-64).
4. Returning to User Mode
- Context Restoration: The kernel restores the saved user state (registers, program counter) from the kernel stack.
- Mode Switch: The CPU executes a return instruction (e.g.,
sysretorireton x86), switching back to user mode. - Resumption: The user program continues execution at the next instruction, typically checking the return value for success/error.
Hardware and OS Roles
- CPU: Manages privilege levels, mode transitions, and instruction execution.
- MMU: Enforces memory isolation between user and kernel spaces.
- Kernel: Provides system call handlers, manages resources, and ensures security.
Summary
- User-to-Kernel Transition: Hardware elevates privileges, saves state, and jumps to the kernel.
- Kernel Execution: Performs the requested service using privileged operations.
- Kernel-to-User Transition: Restores user state, drops privileges, and resumes the user program.
This controlled interaction ensures user programs can safely access OS services without compromising system stability or security.
References
Last updated on