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
- User Mode: code that runs in user mode is restricted in what it can do. For example, when running in user mode, a process can’t issue I/O requests; doing so would result in the processor raising an exception; the OS would then likely kill the process.
- Kernel Mode: The OS runs in this mode. In this mode, code that runs can do what it likes, including privileged operations such as issuing I/O requests and executing all types of restricted instructions.
System Calls
When a user program wished to perform privileged operation, it should perform a system call.
Basic Procedure
- A program in user mode execute the trap instruction.
- The hardware:
- Save regs to kernel stack
- Move to kernel mode
- jump to trap handler (according to trap table)
- The OS handles the trap
- Do the work of syscall
- return-from-trap
- The hardware:
- Restore regs from kernel stack
- Move to user mode
- jump to PC after trap
- The program continues its execution