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?

  1. Step 1-3: Calling program pushes the arguments for the parameters of the system call to its Stack Segment
  2. 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 5 Library Call puts Syscall Interrupt Number in a place where Kernel expects it, such as a Register
  3. Step 6: Execute Trap Interrupt (陷入) to enter the Kernel Mode
  4. Step 7: Kernel code examines Syscall Interrupt Number, dispatch the correct Interrupt Handler via Interrupt Vector Table
  5. Step 8: The desired Interrupt Handler starts running
  6. 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
  1. Step 10: Then, library call returns, and we are back to the user program
  2. 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

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


Linux System Calls

Windows System Calls