5.1.13 · D1Instruction Set Architecture (ISA)

Foundations — System vs user mode and privilege levels

1,894 words9 min readBack to topic

This page assumes nothing. Before you read the parent topic you need a small toolkit of words and pictures. We build each one from zero, in an order where every new idea leans only on the ones before it.


0. What even is a "CPU instruction"?

Look at the figure below. A program is just a long list of these numbered commands sitting in memory. The CPU walks through them one at a time.

Figure — System vs user mode and privilege levels

1. The Program Counter (PC)

Picture a finger pointing at one line of a to-do list. Normally the finger slides down one line at a time. To "jump" is to yank the finger to a completely different line.


2. Registers

Picture a handful of labelled cups on the desk right next to you, versus a warehouse (RAM) across town.


3. The Status Register (PSW)

Figure — System vs user mode and privilege levels

In the figure, one register is split into little labelled slots called fields. One of those fields is the mode bit we build next.


4. A bit, and the "mode bit"


5. Privileged vs unprivileged instructions

Picture two shelves of tools. The bottom shelf (add, copy) anyone may use. The top shelf (halt, remap) has a lock, and only the kernel holds the key — the key being "mode bit = kernel".


6. Decode — the moment of the check

Figure — System vs user mode and privilege levels

The figure shows the pipeline: fetch → decode → execute. The privilege check happens at decode, because enforcement must come before the dangerous effect. Checking after would be like checking a driver's licence after the crash.


7. Trap / Exception

Picture a trapdoor in the floor. Step on the wrong tile (an illegal instruction) and you fall straight into the principal's office (the kernel handler), whether you wanted to or not.


8. The controlled doorway — system call


9. Atomic

Picture flipping a light switch: it's either off or on, never "half flipped" that someone could grab mid-way.


10. Rings / Exception Levels — generalising the one bit

Figure — System vs user mode and privilege levels

The figure: nested circles, innermost = most trusted. See Protection rings (x86) / Exception Levels (ARM).


Prerequisite map

instruction and opcode

decode

program counter PC

system call doorway

registers

status register PSW

mode bit

privileged vs unprivileged

trap exception

atomic

rings and levels

System vs user mode


Equipment checklist

Test yourself — you're ready when each reveal matches what you'd say.

What is an opcode?
The small number at the start of an instruction that names which command it is — the "verb."
What does the Program Counter hold?
The memory address of the next instruction to run; a jump writes a new value into it.
Why store the mode bit in a register instead of RAM?
Registers are instant to read (the check runs on every instruction) and the register is protected — user code can't write it.
What is a privileged instruction?
One the CPU only executes when the mode bit says kernel mode.
At which pipeline stage is the privilege check made, and why?
At decode — before execute — so a forbidden instruction's effect never happens.
What is a trap/exception?
The CPU's built-in reaction that switches to kernel mode and jumps to a fixed OS handler instead of running an illegal instruction.
Why must the user→kernel transition be atomic?
So no attacker can interleave code and run their own instructions in kernel mode between the mode switch and the jump.
In ring numbering, which is most privileged?
The lowest number (Ring 0) — inner ring, most trusted.