Sub topic 1
Kernel Mode, User Mode, and Interrupts
Prerequisites
This chapter assumes basic computer organization. You should know that the CPU runs instructions, memory stores data and programs, and interrupts can change the normal flow of execution. You should also be comfortable with C functions and pointers.
Why the CPU Uses Two Modes
A modern operating system must protect itself from faulty or malicious programs. If every program could directly control hardware, one program could overwrite another program's memory or disable the system.
The CPU therefore provides at least two privilege modes:
- User mode runs ordinary application code with restricted privileges.
- Kernel mode runs trusted operating-system code with access to protected instructions, memory, and devices.
A user-mode program cannot directly change page tables, configure device controllers, or disable interrupts. It must ask the kernel to perform such operations.
The CPU records the current mode in a protected control bit or register. Hardware changes this mode during controlled events. A user program cannot simply set the bit to enter kernel mode.
System Calls and Traps
A system call is a controlled request from a user program to the operating system. Examples include opening a file, creating a process, reading input, and allocating memory.
The program places a system-call number and arguments in agreed locations, such as registers or a memory block. It then executes a special trap or syscall instruction. The CPU:
- Saves enough information to resume the user program.
- Switches from user mode to kernel mode.
- Enters a kernel handler.
- Performs permission checks and the requested operation.
- Places a result or error code where the program can read it.
- Returns to user mode at the next instruction.
A trap is a deliberate event caused by the running program. It differs from an exception, which is caused by the current instruction, such as division by zero. Both transfer control to the kernel through protected entry points.
Hardware Interrupts
A hardware interrupt is an asynchronous signal from a device or timer. For example, a disk controller can interrupt the CPU when a read finishes. A timer interrupt lets the kernel regain control from a running process.
The CPU saves the interrupted context and starts an interrupt handler in kernel mode. After handling the event, the kernel can resume the interrupted program or schedule a different one.
Key Distinction
A system call is synchronous: the program requests it and waits for its result. A hardware interrupt is asynchronous: a device or timer causes it while the CPU is doing something else. Both use protected kernel entry mechanisms.
Exercise
A user program needs to read a file, and a timer fires while the read is being processed. Which events are synchronous and which are asynchronous?
Show answer
The file read system call is synchronous because the program requested it. The timer interrupt is asynchronous because the timer caused it independently of the program's instruction stream.