Kernel: Minimal set of functions necessary to manage system resources safely and efficiently.
The OS Hierarchy:
Applications and System Software
General Libraries
System Call Libraries
OS Kernel
Remember: Applications and System Software can use abstractions like library functions or just invoke kernel functions directly via supervisor calls.
User v.s. Kernel Mode
We divide the CPU instruction set into two sets:
Privileged
Non-Privileged
The CPU can operate in two modes (mode bit):
Kernel Mode: Privileged and non-privilege instructions allowed.
User Mode: Only non-privileged instructions allowed.
Why?: Accessing I/O devices, control registers, etc. is risky for security, so we only let the kernel do it.
Example: Transitioning from user mode to kernel
mode
User Process:
User Process Executing
Call System Call
trap mode bit = 0
Kernel:
Execute System Call
return mode bit = 1
User Process:
Return from system call
Remember: User mode processes and kernel mode processes are separate!
System Calls and Supervisor Calls
System Call: A higher-level library function.
Eventually makes a supervisor call.
Supervisor Call: Lower-level privileged instruction that automatically transfers execution control to a well-defined location in the kernel.
Privileged: The call switches the CPU to kernel mode via the mode bit, and
Well-Defined Location: The function isn’t specified by an address, but indirectly via and index into a branch vector.
This prevents a call from branching to arbitrary locations within the kernel.
Remember: Changing the mode bit of the CPU from user \to kernel mode can only be done by a privileged instruction.
Going the other way is not restricted.
More on Branch Vector:
Branch Vector: A data structure that contains a list of function addresses pointing to specific kernel routines.
e.g., index 0 might point to sys_read(), index 1 to sys_write()
User programs can only make system calls with the index, which controls entry points.
Analogy: Restaurant Menu
The system call can only order item #1, #2, #2, etc. off the menu, and cannot reach into the kitchen and give dangerous orders like “give me whatever is in pot #7”.
System Call Implementation
System calls can be:
Defined in the main body of the kernel, or
Loaded in main memory
Typically a number is associated with each system call.
The indexing in maintained by the system-call interface.
System-Call Interface:
Invokes intended system call in kernel and returns status and any return values.
Caller know nothing about implementation. Details are hidden.
Caller only needs to obey API.
System Call Parameter Passing
Three methods to pass parameters to system calls:
Registers: Put data right into registers.
Simplest.
Block/Table in Memory: Put address of table/array/list in register.
Most-common.
Stack in Memory: Push params to stack, let kernel pop them off.
Why?: Storing in memory (block or stack) allows us to avoid limits like having limited registers.
Types of System Calls
Six major categories of system calls:
Process Control
File Manipulation
Device Manipulation
Information Maintenance
Communication
Protection
Interrupts and Traps
Interrupt: Event that diverts current execution of a program to respond to an event.
Triggered by a hardware signal sent to the CPU from an external device.
Trap: An interrupt triggered by the currently-executing instruction.
aka: Internal interrupt
Remember: Interrupts (internal and external) stop the execution of the current program, save the state of computation, and transfer control to the kernel.
Example: Interrupts and Traps Overview
Two Most-Common Uses of Interrupts:
An I/O device signals to the OS the completion of an I/O operation.
Implementation of time-sharing CPU among multiple concurrent computations (interrupt generated by countdown timer).
Examples of Traps:
Dividing by zero, executing an invalid opcode, causing arithmetic overflow.
Executing a supervisor call, not because it’s an error, but in order to transfer control to the kernel.
Example: System Call with Multiprogramming without
Timesharing
Suppose two applications, Application 1 and Application 2
Application 1 wants to call system call S(), which triggers an I/O operation.
Application 2 only want to do some simple arithmetic.
The first trap occurs when switching the CPU from user mode to kernel mode.
Functions can invoke multiple services (e.g., reading a file invokes I/O layer, then memory layer; but writing a file would invoke memory layer, then I/O layer; so ordering layers optimally is challenging)
Microkernel (Small)
Microkernel: Implements only the essential functions (e.g., communication, memory managemenet, CPU scheduling) rather than monolithically.
Other services (e.g., application programs, file system, device drivers) are implemented in user mode — dipping into the kernel often for the essential functions).
Examples: macOS mach
Problems: Communication between user mode and microkernel (e.g., flipping mode bit constantly) introduces efficiency issues.
Modules
Modules: Many operating systems implement loadable kernel modules.