Skip to content

Linux Foundations / Understand the system underneath ยท Step 21 of 38

Lesson: Privilege levelsSwitch
โ† Linux Foundations

System administration

Privilege levels

Understand user mode, kernel mode and how the processor moves between them.

If any program could change any part of memory or command any device, one bug could take down the whole machine. Processors prevent this with privilege levels that the kernel uses to protect itself and other processes.

Rings on x86

x86 processors have numbered privilege levels called rings, from 0 to 3. Ring 0 has the most privilege. Linux uses ring 0 for the kernel and ring 3 for user programs. Other architectures use different names. On 64-bit ARM, user programs run at EL0 and the Linux kernel runs at EL1, or at EL2 on systems that use the Virtualization Host Extensions (VHE).

Kernel mode and user mode

In kernel mode the CPU may run any instruction and manage hardware and memory mappings. In user mode privileged instructions are refused, and a process can only reach memory that has been mapped for it.

If a program breaks the rules, the CPU raises an exception and the kernel decides what to do. An invalid memory access usually ends with the kernel sending SIGSEGV:

Segmentation fault (core dumped)

The faulty process stops, while the rest of the system carries on. A serious bug inside the kernel is different: it can crash or destabilise the whole machine.

Root is not kernel mode

The root user passes most permission checks, but a program run by root still executes in user mode. It has to ask the kernel like any other program. The difference is that the kernel says yes more often.

bash
sudo id -u
0

User ID 0 is an identity checked by the kernel. It is not CPU ring 0. See the accounts and access files lab for more on users.

Crossing the boundary

The CPU enters kernel mode in controlled ways, at entry points that the kernel set up:

  • A system call, when a program requests a service.
  • An exception, such as a page fault or division error.
  • A hardware interrupt, for example from a network card.

When the work is done, the kernel returns to user mode. Not every page fault is an error. Many are a normal part of loading memory on demand.

See the time spent on each side

bash
time dd if=/dev/zero of=/dev/null bs=1 count=500000 status=none
real    0m0.310s
user    0m0.090s
sys     0m0.220s

user is CPU time spent in user mode. sys is CPU time the kernel spent working for the process. real is elapsed wall clock time. Copying one byte at a time causes a huge number of system calls, so sys is noticeable. Your numbers will differ.

Try it

  1. Run the time command above, then change to bs=1M count=500 and compare user and sys.
  2. Run id and sudo id and note the user IDs.
  3. Run vmstat 1 5 and look at the us and sy CPU columns.

Check your understanding

Privilege levels

All 5 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.