System Call
As we talked in 0 OS isolate user programs from each other, but sometimes we need them communicate with each other. OS provides service to user programs through system call. System call is a function call that invokes the OS. Whenever an application wants to use a resource that the OS manages, it asks permission from the OS through a system call.
- system call are OS specific
- system call enforces separation of user to kernel mode
When we call system call, kernel must verify arguments that it is passed to protect kernel from misbehaving processes
- prevent kernel creash or malfunction from invalid argument
- prevent security issues
When we dispatch system call:
- kernel assigns each system call a system call number
- kernel initializes system call table, mapping each system call number to a function implementing that system call
- User processs sets up system call number and arguments
- User process runs system call instruction
- Hardware switches to kernel mode and invokes system call handler
- Kernel looks up system call table using system call number
- Kernel invokes the corresponding function
- kernel returns to user process by interrupt return
When pass parameters to system call, the first parameter (%rax
(syscall number)) is always the syscall number, and can pass up to 6 parameters, the registers are:
%rbx
,%rcx
,%rdx
,%rsi
,%rdi
,%rbp
- if more than 6 parameters are needed, all rest should be packaged and pass through it as the 6th parameter