5.1.13 · D3Instruction Set Architecture (ISA)

Worked examples — System vs user mode and privilege levels

2,214 words10 min readBack to topic

You have met the idea of user mode, kernel mode, and the syscall doorway in the parent note. Here we do the opposite of hand-waving: we list every kind of situation a privilege system can face, then work each one to the end. If you can trace all of these, nothing about mode switching can surprise you in an exam.


The scenario matrix

Before working examples, we lay out the full space of cases. Every real event on a CPU is one of the cells below. Think of the two axes as "who is running?" (current mode) and "what did they just attempt?" (the event).

# Current mode Event attempted Legal? What hardware does
A User ordinary unprivileged instruction (add, load) runs it, mode unchanged
B User privileged instruction (cli, hlt, write page-table base) trap → kernel fault handler
C User syscall / ecall (the legal doorway) atomic promotion → kernel entry vector
D User external interrupt fires (timer, disk done) atomic promotion → interrupt vector
E User memory access outside its mapped region page fault → kernel
F Kernel any instruction, including privileged runs it, mode unchanged
G Kernel sret / iret (return to user) atomic demotion → user, restore user PC
H Kernel writes the mode bit directly ✅ (kernel only) mode changes; a user attempting this = cell B
I degenerate: mode bit = kernel but PC came from user's choice this is the exploit we must prevent vectoring makes it impossible
J limiting: user loop never yields (while(1){}) timer (cell D) forces control back pre-emption
K word problem: read() from a file mix of A, C, F, G full round trip
L exam twist: nested — interrupt during a syscall mix of D inside F privilege already kernel; PC saved twice

The examples below hit every letter. The matrix cell each covers is stated in its title.

Figure — System vs user mode and privilege levels

Worked examples


Example 2 — Cell B: the illegal privileged instruction


Figure — System vs user mode and privilege levels

Example 4 — Cell D + J: the limiting case, a user loop that never yields


Example 5 — Cell E: a memory access out of bounds


Example 6 — Cell H vs I: the fixed point, why you can't promote yourself


Example 7 — Cell K: full word-problem round trip, read() a file


Example 8 — Cell L: exam twist, an interrupt during a syscall


Recall Quick self-test (click to reveal)

Which matrix cell? A user program's hlt instruction ::: Cell B — privileged instruction in user mode → trap. Which matrix cell? The scheduler regaining a spinning CPU ::: Cell D (timer interrupt), enabling the limiting case J. How many mode changes in one complete read()? ::: Exactly 2 — up on syscall, down on sret. Why is cell I (kernel mode, user-chosen PC) impossible? ::: Because promotion only happens via vectored syscall/interrupt, which forces an OS-chosen PC; and writing the mode bit directly is itself privileged.


Active-recall flashcards

#flashcards/hardware

In the scenario matrix, what happens in cell B (user runs a privileged instruction)?
Hardware raises a trap/#GP before the effect; control jumps to the kernel fault handler.
How many mode transitions occur in one full read() syscall?
Two — user→kernel on syscall, kernel→user on sret/iret.
Why can a user's while(1){} loop not freeze the machine?
The kernel armed a hardware timer (privileged); when it fires, an interrupt atomically returns control to the OS.