Abstract
- A way for program in User Space to request privileged services like hardware access from the Kernel
- A form of Software Interrupt
- Can be traced by
strace
User program isn't communicating with syscall directly!
The System call is placed behind an Abstraction Barrier created with the The GNU C Library. So user programs like OS System Program is triggering system call that requests privileged services from the kernel via the GNU C Library.
Best Practice
Program should always check the results of System Call (系统调用) to see if an error has occurred
Better Security
Allow User Space program to use computer hardware to complete its job with kernel’s security implementation to prevent programs from doing malicious stuff
How is a system call triggered?
- Step 1-3: Calling program pushes the arguments for the parameters of the system call to its Stack Segment
- Step 4(where the actual Library Call is happening): An Instruction is triggered to trigger the corresponding Library Call, the same instruction is used to trigger other library calls
- Step 6: Execute Trap Interrupt (陷入) to enter the Kernel Mode
- Step 7: Kernel code examines Syscall Interrupt Number, dispatch the correct Interrupt Handler via Interrupt Vector Table
- Step 8: The desired Interrupt Handler starts running
- Step 9: After Interrupt Handler finishes, control maybe returned to the User Space at the Instruction following the Trap Interrupt (陷入)
Control MAYBE returned to user-space
- The System Call (系统调用) may block the caller (in this case Library Call), preventing it from continuing
- For example, the system call for keyboard input. When system call tries to read but nothing has been typed yet, the caller has to be blocked
- Step 10: Then, library call returns, and we are back to the user program
- Step 11: To finish the job, the user program has to clean up the Stack Segment by incrementing the Stack Pointer exactly enough to remove the arguments we pushed to the stack segment
Highly CPU dependent
- System call is implemented with Assembly language which is differently across different Instruction Set Architecture (ISA)
Parameters of System Call
For example, some ISA may expect the parameters be stored in Stack Segment of the Kernel
Some ISA may expect the parameters be stored in Register
Abstraction comes to rescue
- Kernel provides an Abstraction Barrier on top of these Interrupts (中断) and Interrupt Handler. The abstraction barrier has a standardised Library Call interface like POSIX that wraps the Assembly Instruction of different Instruction Set Architecture (ISA) for applications inside User Space to call
- Specific Instruction of Instruction Set Architecture (ISA) is generated automatically during Compilation
- Unix-like systems use libc and Windows uses ntdll.dll
Examples
Linux System Calls
- Above is some POSIX system calls, basically a standardised set of Library Call that wraps System Call (系统调用), almost 1-to-1
- Full List
Windows System Calls
- The list above shows the Library Call which is powered by Windows’ System Call (系统调用)