Skip to content
systemcoding.dev
Kernels

How CPU Privilege Rings and Exception Levels Protect the Kernel

Privilege rings on x86 and exception levels on ARM split code into trust levels the hardware enforces, so a user program cannot touch kernel memory or devices.

6 min read
Summarize in AI
Flat illustration of user space above the system call boundary, the kernel and the hardware, with the Linux, Intel and Arm logos.

The CPU enforces the line, not the operating system

A kernel cannot protect itself with rules alone. If a user program could execute any instruction, it could disable interrupts, rewrite page tables and read every other process's memory. The protection has to be built into the processor, so that certain instructions fail unless the processor is in a privileged mode. x86 calls these modes privilege rings. ARM calls them exception levels. The idea is the same: a small number of ordered levels, and rules about which can do what.

This post explains both, shows what a violation looks like, and describes how a processor moves between levels. It uses x86-64 and AArch64 as documented in the Intel Software Developer's Manual and the Arm Architecture Reference Manual, running Linux 6.12 LTS.

x86 privilege rings

x86 defines four rings, numbered 0 to 3, where lower is more privileged. The current privilege level (CPL) lives in the low two bits of the code segment selector. In practice, Linux and nearly every modern operating system use only two of them:

RingLinux useWhat it can do
0The kernelEvery instruction, all memory, all I/O
1, 2UnusedHistorical drivers, never adopted
3User programsNormal instructions; no privileged ones

The reason rings 1 and 2 went unused is that paging, which every OS needs, only distinguishes two levels: the user/supervisor bit in a page table entry is one bit wide. Segmentation could have supported four levels, but 64-bit mode flattened segments, so the extra rings carry no practical meaning on x86-64.

What ring 3 cannot do

Ring 3 cannot execute instructions that would break isolation. A few examples:

  • hlt stops the core until the next interrupt.

  • cli and sti change the interrupt flag.

  • mov cr3, rax loads a new page table root.

  • in and out access I/O ports (unless the kernel grants permission through the I/O bitmap).

  • wrmsr writes model-specific registers.

The following program tries one of them. It will never reach the return.

ring3.cc
#include <stdio.h>

int main(void)
{
    puts("about to execute hlt in user mode");
    fflush(stdout);

    __asm__ volatile ("hlt");

    puts("never printed");
    return 0;
}
Shell
gcc -o ring3 ring3.c
./ring3
echo "exit status: $?"
Text
about to execute hlt in user mode
Segmentation fault (core dumped)
exit status: 139

The processor raised a general protection fault (#GP, vector 13) because hlt is privileged. The kernel's fault handler saw that the fault came from user mode and delivered SIGSEGV. Exit status 139 is 128 plus signal 11. The shell message may say "Segmentation fault" without "(core dumped)" depending on your core file settings.

ARM exception levels

ARMv8-A organizes privilege differently. It defines four exception levels, EL0 to EL3, and a higher number means more privilege:

LevelTypical software
EL0User applications
EL1The operating system kernel
EL2A hypervisor
EL3Secure monitor (firmware such as Arm Trusted Firmware)

The two differences from x86 are worth pausing on. First, ARM puts virtualization into the base model: EL2 exists so a hypervisor can sit beneath one or more EL1 kernels. On x86, virtualization was added later as a separate mode (VMX root and non-root), orthogonal to the rings. Second, the secure world: EL3 and the TrustZone extension partition the machine into a normal world and a secure world, so a trusted OS can run next to Linux and keep secrets that Linux cannot read.

You can read the current level from AArch64 code only at EL1 or above, since the CurrentEL register is privileged. A user program learns its level indirectly. It is always EL0 when it runs.

How a processor changes level

The levels would be worthless if user code could jump to ring 0 freely. Every transition goes through a gate the kernel controls.

On x86-64 the paths are:

  1. syscall, which loads the kernel's entry address from the IA32_LSTAR MSR and sets CPL to 0.

  2. Interrupts and exceptions, which look up the handler in the interrupt descriptor table the kernel loaded with lidt.

  3. sysret or iretq, which return to ring 3.

On AArch64 the paths are:

  1. svc (supervisor call), which causes a synchronous exception into EL1 and jumps to the vector table the kernel registered in VBAR_EL1.

  2. Faults and interrupts, which go to the same vector table at a different offset.

  3. eret, which returns to the level and address saved in SPSR_EL1 and ELR_EL1.

The common property is that the target address is chosen by the privileged side at boot. User code can trigger the transition but cannot pick where it lands.

A system call at the instruction level

Here is the smallest x86-64 Linux program that uses a raw syscall, to show the transition without the C library in the way. It writes a line and exits.

hello.Sasm
.globl _start
.text
_start:
    mov $1, %eax            ## SYS_write
    mov $1, %edi            ## fd 1
    lea msg(%rip), %rsi
    mov $len, %edx
    syscall                 ## ring 3 -> ring 0 -> ring 3

    mov $60, %eax           ## SYS_exit
    xor %edi, %edi
    syscall

.data
msg: .ascii "ring 0 did this write\n"
.set len, . - msg
Shell
gcc -nostdlib -static -o hello hello.S
./hello
Text
ring 0 did this write

The write itself executed in ring 0, in the kernel's ksys_write path. User code never touched the terminal.

Rings and the rest of the protection

Privilege level alone does not protect memory. It works together with the page tables: the user/supervisor bit decides which pages ring 3 may access, and the NX bit decides which pages may be executed. Newer features add to it, such as SMEP and SMAP on x86, and PAN (Privileged Access Never) and PXN on ARM, which stop the kernel from executing or accidentally using user memory.

The rings also do not protect against everything. Speculative execution attacks such as Meltdown and Spectre showed that a user program could infer kernel memory through side effects even without a permitted access. The fixes, such as page-table isolation, are described in the kernel's hardware vulnerabilities documentation.

Virtualization and the "negative rings"

Hypervisors need more privilege than a guest kernel. Intel and AMD added hardware virtualization that gives a guest its own ring 0 while the hypervisor runs in a root mode beneath it. Informally this is sometimes called ring -1. System Management Mode and firmware engines are sometimes called ring -2 or -3. These are community labels, not architectural terms, so check the vendor manual before using them.

What goes wrong without the hardware

Early personal computer systems ran everything in one mode, and the consequences are the reason the design exists. A single faulty program could overwrite the operating system and halt the machine, and a malicious one could read anything. Moving the enforcement into the processor changed the problem from "trust every program" to "trust the small piece of code that controls the gates". That is also why bugs in that small piece matter so much: if user code finds a way to run at ring 0 or EL1, every other protection is gone. Kernel hardening work, from stack protectors to control-flow integrity, is largely about keeping the gates narrow.

Also inHardware
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.