Sub topic 1
Kernel Mode, User Mode, and Interrupts
Prerequisites
This chapter assumes you already know the following:
- Basic C programming: functions, pointers, headers.
- Basic computer organization: what the CPU, registers, program counter (PC), and memory do.
- What an interrupt is at the hardware level: a signal that makes the CPU pause its current work.
Later chapters build on this one. System calls, context switches, and preemptive scheduling all depend on the mode switch and the interrupt mechanism described here.
Why the CPU Has Two Modes
An operating system must stop one program from damaging another program or the OS itself. Software alone cannot guarantee this, because a buggy or hostile program could simply overwrite the OS. So the hardware enforces a split.
The CPU keeps a mode bit in a status register. Textbooks (Silberschatz, which GATE questions often follow) use these values:
- Kernel mode (also called supervisor, system, or privileged mode): the mode bit is 0. The CPU can execute every instruction and touch all memory and devices.
- User mode: the mode bit is 1. The CPU can execute only a safe subset of instructions and can touch only the memory the OS has assigned to the process.
Real processors differ in the encoding. x86 uses four privilege rings, and in practice operating systems use only ring 0 (kernel) and ring 3 (user). The ideas are the same.
What User Mode Forbids
Instructions that could break isolation are called privileged instructions. If a process runs one in user mode, the CPU does not execute it. It raises an exception, and the OS usually terminates the process.
Typical privileged operations:
- Direct I/O instructions, such as talking to a disk controller.
- Loading the memory-management registers, such as the base and limit registers or the page-table base register.
- Enabling or disabling interrupts.
- Setting the timer.
- Changing the mode bit to kernel mode.
- Halting the CPU.
Ordinary work such as arithmetic, loads and stores inside the process's own memory, and branches is not privileged. A user program can do all of that without the kernel's help.
Memory protection works together with the mode bit. With a base and limit register pair, every address a user process generates is checked against the range assigned to it. An address outside the range causes a trap to the kernel. Only kernel mode can change those registers.
Moving Between Modes
The CPU does not let user code simply set the mode bit to 0, because that would defeat the protection. The switch to kernel mode happens only through controlled entry points that the kernel set up in advance.
- User to kernel: a system call, an exception, or an interrupt. In each case the hardware sets the mode bit to kernel mode and jumps to a kernel-chosen address.
- Kernel to user: a special return instruction, such as
ireton x86 orsysret. The kernel executes it after finishing its work. It restores the saved program counter and the user mode bit.
Note the asymmetry. Many events enter the kernel, but there is one controlled way out.
Interrupts, Traps, and Exceptions
All the entries above are forms of transferring control to the kernel. Terminology varies between books, so learn the distinctions that matter for GATE.
| Kind | Cause | Synchronous with the instruction? | Example |
|---|---|---|---|
| Hardware interrupt | A device or timer signals the CPU | No (asynchronous) | Keyboard key press, disk finished, timer tick |
| Exception (fault) | The running instruction fails | Yes | Divide by zero, illegal memory access, page fault |
| Trap (software interrupt) | The program deliberately requests the kernel | Yes | System call instruction such as `int 0x80` or `syscall` |
Two points follow from this table.
- An interrupt is asynchronous: it can arrive between any two instructions and has nothing to do with what the program is doing. Exceptions and traps are synchronous: they are caused by the instruction currently executing.
- A page fault is an exception, but it is not an error. The kernel loads the missing page and re-executes the same instruction. A divide by zero, by contrast, usually ends the process.
What Happens When an Interrupt Arrives
The CPU checks for pending interrupts at the end of each instruction cycle. If one is pending and not masked, the following steps occur:
- The hardware finishes the current instruction.
- The hardware saves the minimum state, usually the program counter and flags, on the kernel stack.
- The mode bit is set to kernel mode.
- The CPU uses the interrupt number as an index into the interrupt vector table (on x86, the interrupt descriptor table). That entry holds the address of the matching interrupt service routine (ISR).
- The ISR saves any other registers it will use, handles the event, and restores those registers.
- A return-from-interrupt instruction restores the saved state and the user mode bit, and the interrupted program continues as though nothing happened.
The vector table lives in kernel memory, so a user program cannot redirect an interrupt to its own code.
Masking and priorities
The kernel can temporarily disable interrupts, which is called masking, during short critical sections. Disabling interrupts is itself a privileged operation. Most systems also have non-maskable interrupts for events that cannot wait, such as a hardware failure. Interrupt controllers assign priorities, so a higher-priority interrupt can preempt the handler of a lower-priority one.
The Timer Interrupt
The timer deserves special attention because CPU scheduling depends on it. Suppose a user program enters an infinite loop. Without help, it would keep the CPU forever, since the OS never regains control.
The fix is a hardware timer. Before handing the CPU to a user process, the kernel programs the timer with a time limit. When the time expires, the timer raises an interrupt. The ISR runs in kernel mode, so the OS regains control and can pick another process.
This is the mechanism behind preemption, and it makes time-sharing possible. Loading the timer is a privileged instruction, so the user program cannot turn it off. Later chapters on context switching and Round Robin scheduling rely on this timer tick.
Interrupts Versus Polling
A CPU can learn that a device has finished in two ways.
- Polling: the CPU repeatedly reads the device's status register in a loop. This is simple, but it wastes CPU cycles when the device is slow.
- Interrupt-driven I/O: the CPU starts the operation and does other work. The device interrupts when it finishes. This costs some overhead per interrupt but frees the CPU.
Polling can win when the device is very fast or the event is almost always ready, because the interrupt overhead exceeds the waiting time.
Common Errors and Gotchas
- Confusing the mode with the process. The same process runs in both modes at different moments. A system call runs kernel code on behalf of the process, still within that process's context.
- Calling a library function a system call.
printfis a library function that runs in user mode. It eventually calls thewritesystem call, and only that part enters the kernel. - Treating all interrupts as asynchronous. Traps and exceptions are synchronous. GATE questions often ask which events are caused by the running program.
- Saying user code can switch modes by itself. It cannot. It can only request entry through a trap, and the kernel decides what happens.
- Thinking an interrupt is always an error. Timer ticks and I/O completions are routine events.
Exercise
Which of these events must be executed in kernel mode, or will cause a switch into it? (a) Adding two registers (b) Reading the clock through a system call (c) Disabling interrupts (d) A divide-by-zero in user code. Name the ones that involve kernel mode and say what happens in each case.
Show answer
(a) Adding two registers is an ordinary instruction and stays in user mode. (b) A system call is a trap, so the CPU switches to kernel mode, runs the kernel routine, and returns to user mode. (c) Disabling interrupts is a privileged instruction. In user mode it raises an exception and the OS typically terminates the process. It works only if already in kernel mode. (d) Divide-by-zero is a synchronous exception, so the CPU switches to kernel mode and runs the exception handler, which usually terminates the process.
Exercise
Why is loading the timer a privileged instruction, and what would go wrong if it were not?
Show answer
If a user process could set or disable the timer, it could stop the timer interrupt from firing. The OS would then never regain control from that process, so a looping program could monopolize the CPU and preemptive scheduling would fail.