Abstract
- A way for program to request services from the Kernel via Signal (Software Interrupt)
- Can be traced by
strace
- Program should always check the results of System Call (系统调用) to see if an error occurred
Benefits
Better Security
- Allow User Space program to use computer hardware to complete its job with OS’s security implementation to prevent programs from doing malicious stuff
How a system call is triggered
- Calling program pushes parameters of the system call to Stack Segment (Step 1-3)
- Trigger an Instruction to trigger the corresponding Library Call, the same instruction is used to trigger other library calls (Step 4, where the actual library call is happening)
- Execute Trap Interrupt (陷入) (Step 6)
- Kernel code examines Syscall Interrupt Number, dispatch the correct Interrupt Handler via Interrupt Vector Table(Step 7)
- The desired Interrupt Handler starts running (Step 8)
- After Interrupt Handler finishes, control maybe returned to the User Space at the Instruction following the Trap Interrupt (陷入) (Step 9)
Control MAYBE returned to user-space
- The System Call (系统调用) may block the caller (in this case Library Call), preventing it from continuing
- For example, keyboard reads system call. When system call tries to read but nothing has been typed yet, the caller has to be blocked
- Then, library call returns to the user program (Step 10)
- To finish the job, the user program has to clean up the Stack Segment by incrementing the Stack Pointer exactly enough to remove the parameters pushed before the making the System Call (系统调用) (Step 11) (Stack Segment grows downwards, so to remove Stack frame, we increment the Stack Pointer)
Highly CPU dependent
- System call is implemented with Assembly language which is differently across different Instruction Set Architecture (ISA)
Parameters of system call
- Some Instruction Set Architecture (ISA) may expect the parameters be stored in Stack Segment
- Some Instruction Set Architecture (ISA) may expect the parameters be stored in Register
Abstraction comes to rescue
- OS provide an Abstraction Barrier on top of these Interrupts (中断) and Interrupt Handler
- Reusable higher-level library functions that wrap the necessary Assembly Instruction are provided by libc on Unix-like systems and part of a library called ntdll.dll on Window
- Specific Instruction Set Architecture (ISA) Instruction is generated automatically during Compilation
Examples
Linux System Calls
- System Call (系统调用) is tightly-coupled with Library Call, almost 1-to-1
- Full List
Windows System Calls
- System Call (系统调用) is decoupled from Library Call
- We can change the System Call (系统调用), without worrying changing the behaviour of Library Call