I. Syscalls


Limited Direct Execution

To make a program run as fast as one might expect, OS developers came up with a technique, which we call limited direct execution.

Direct execution: just run the program directly on the CPU.

Limit: without limits on running programs, the OS wouldn’t be in control of anything and thus would be “just a library”

Restricted Operations

Direct execution is fast, but what if the program wishes to perform some kind of restricted operations, such as issuing an I/O request to a disk, or gaining access to more system resources such as CPU or memory?

The approach we take is to divide the processor modes into user mode and kernel mode

System Calls

When a user program wished to perform privileged operation, it should perform a system call.

Basic Procedure

  1. A program in user mode execute the trap instruction.
  2. The hardware:
    1. Save regs to kernel stack
    2. Move to kernel mode
    3. jump to trap handler (according to trap table)
  3. The OS handles the trap
    1. Do the work of syscall
    2. return-from-trap
  4. The hardware:
    1. Restore regs from kernel stack
    2. Move to user mode
    3. jump to PC after trap
  5. The program continues its execution