Privilege Levels: Machine, Supervisor and User
A trap handler is only safe if no other code can move it. So far, any instruction could write mtvec. A processor needs a way to say: this code may, and that code may not.
You Will Be Able To
- Say why a processor has privilege levels, and name the three of RISC-V.
- List what code at user level cannot do, and what happens when it tries.
- Read from the address of a CSR which level may use it, and whether it can be written.
- Lesson
- S2.14
- Time
- 30 min
- Runs on
- Your browser
- Check
- 6 questions
One wrong write to mtvec, and every later trap goes to the wrong place. On a chip that runs code from many authors, that write must be impossible for most of them.
Before You Start
You need the trap from S2.13: mtvec, mepc, mcause, the handler and mret.
You need cause 2, the illegal instruction, from S2.29.
| Question | |
|---|---|
| 1 | Why does a processor have more than one privilege level? |
| 2 | Which three levels does RISC-V name, and which has the most rights? |
| 3 | What happens when user code reads or writes a machine CSR? |
| 4 | How does the address of a CSR say which level may use it? |
| 5 | Which levels does ap_rv32 v1.0 have? |
The Idea: Trusted Code and Other Code
Some code keeps the system alive. It sets up the handler, the timer and the memory. A fault in it stops everything.
Other code is an application. It may be new, or wrong, or written by someone else. A fault in it should stop only itself.
A right that some code has and other code lacks is a privilege.
So the processor keeps a small number beside the PC: the privilege level. It says how much the running code may do.
The highest level is the machine level. Code there may run every instruction and use every CSR. After reset the processor is always at this level.
The lowest is the user level. Code there may run ordinary instructions. It may not touch the registers that control the processor.
Between them RISC-V names a supervisor level, for the core of an operating system. A small chip can leave it out.
What if user code tries a forbidden thing? The processor does not do it. It takes a trap, and the handler at machine level decides.
One thing must be said plainly. The real core of this track, ap_rv32 v1.0, has machine level only. This page uses a model with a user level.
New Words
| Word | Meaning |
|---|---|
| Privilege level | A number the processor keeps, which says what the running code is allowed to do. |
| Machine level | Level 3, the highest, written M. Every instruction and every CSR is allowed. Also called machine mode. |
| Supervisor level | Level 1, written S, for an operating system. Named here, and not in our model. |
| User level | Level 0, the lowest, written U. For applications. Also called user mode. |
| Privileged instruction | An instruction that only a high enough level may execute, such as mret. |
| Operating system | The software that starts applications, serves them, and ends one that fails. |
mhartid | A read-only machine CSR. It holds the number of this processor in the chip. |
The letter m in mtvec, mepc and mstatus says: a CSR of the machine level.
One Picture: Two Floors and Two Doors
- Processor
- Control / FSM
- Memory
- Peripheral
- After reset: upstairs
The processor starts at machine level. The start-up code may run every instruction and use every CSR. It installs the handler.
- The door down
The start-up code runs an mret that lowers the level. The application starts, at user level.
- What the application cannot reach
It adds, loads, stores and branches as before. A machine CSR is closed to it. So is mret.
- The door up
A trap is the only way up. It always lands in the handler, at the address that machine code chose.
- And down again
The handler decides what happens. If the application may go on, an mret takes it back down.
Read the diagram as text
- Start-up code: machine level: runs first after reset
- Trap handler: machine level: the only way in from below
- Machine CSRs: mtvec, mepc, mstatus and the rest
- Application: user level: ordinary instructions only
- Start-up code → Application: door down: mret
- Application → Trap handler: door up: a trap
- Trap handler → Application: door down again: mret
- Trap handler → Machine CSRs: reads and writes
- Application → Machine CSRs: no access: it traps
User code cannot choose where it enters the upper floor. That one fact is the whole protection.
| Level | Number | Who runs there | In ap_rv32 v1.0 | In the model of this page |
|---|---|---|---|---|
| Machine | 3 | Start-up code and the trap handler | yes: the only level | yes |
| Supervisor | 1 | The core of an operating system | no | no |
| User | 0 | Applications | no | yes |
The numbers are those of RISC-V, not checked against its specification. The number 2 is not used.
The Smallest Example, Worked by Hand
How does the processor know that a CSR belongs to the machine level? The answer is in the address of the CSR. It has 12 bits.
| Bits | Meaning | Example: mstatus, 300 in hex | Example: mhartid, f14 in hex |
|---|---|---|---|
| 11 and 10 | 11: the CSR is read-only. Anything else: it can be written | 00: can be written | 11: read-only |
| 9 and 8 | The lowest level that may use the CSR | 11: machine only | 11: machine only |
The address 300 in hex is 0011 0000 0000 in binary. The address f14 is 1111 0001 0100.
| Address, in hex | Instruction | What it is for |
|---|---|---|
| 040 | addi x10, x0, 1 | user code: ordinary work is allowed |
| 044 | sw x10, 256(x20) | so is memory, in this model |
| 048 | csrr x11, mstatus | read a machine CSR |
| 04c | csrw mtvec, x10 | move the handler |
| 050 | mret | an instruction of the machine level |
| 054 | csrr x12, mhartid | even a read-only one |
| 058 | addi x10, x10, 1 | |
| 05c | jal x0, 0x5c | stop |
It starts at 40 in hex, at user level. Four of its rows are not allowed.
Take the row at 48: csrr x11, mstatus. The CSR address is 300. Bits 9 and 8 say 3: machine only. The level is 0.
So the read is refused. The processor takes a trap with cause 2: for user code, this instruction does not exist.
The row at 4c writes mtvec, at 305. The same two bits, the same answer. The handler address stays as it was.
The row at 50 is an mret. It is a privileged instruction of the machine level. At user level it traps too.
The row at 54 only reads mhartid. A read harms nothing, one might think. But bits 9 and 8 say 3 again, and it traps.
The Rules
Rule 1: After reset the level is machine.
The first code that runs is trusted. It sets everything up before it lets other code in.
Rule 2: At machine level, every instruction and every CSR is allowed.
Memory is another matter. A later page, S3.10, adds rules for memory, called PMP entries. PMP is short for physical memory protection. An unlocked entry does not bind machine level. A locked one binds every level until the next reset: see Which PMP Entry Wins.
Rule 3: A CSR may be used only at the level in bits 9 and 8 of its address, or above.
Below that level, a read and a write both trap with cause 2.
Rule 4: A CSR with 11 in bits 11 and 10 of its address is read-only.
A write traps with cause 2 at every level, machine too.
Rule 5: mret is a privileged instruction.
At user level it traps with cause 2.
Rule 6: A refused instruction changes nothing.
No register, no CSR. The trap goes to the handler at machine level, with mepc at the instruction.
Sources for These Rules
The three levels, their numbers, the rule for CSR addresses and the illegal-instruction trap come from the RISC-V privileged specification. That document is not in our library: none of it is checked against it. The real core ap_rv32 v1.0 has machine level only (docs/soc/ap_rv32-blueprint.md, sections 5.1 and 14: user mode is a later stage). So no row on this page comes from the real core. Every row is printed by the lab, which is a model. Checked in the library: Patterson and Hennessy, Computer Organization and Design (ARM edition), section 4.9. It says that an operating system ends a program after an instruction the processor cannot decode. Read-only CSR addresses, bits 11 and 10 at 11, are in the blueprint, section 5.2.
Now Run It
The lab at the end of this page is the model with a user level. Each row starts with M or U: the level the instruction ran at.
M 00000000 | lui x20, 0x80000 | x20 = 80000000
M 00000004 | addi x5, x0, 128 | x5 = 00000080
M 00000008 | csrw mtvec, x5 | mtvec = 00000080
M 0000000c | addi x5, x0, 64 | x5 = 00000040
M 00000010 | csrw mepc, x5 | mepc = 00000040
M 00000014 | lui x5, 0x2 | x5 = 00002000
M 00000018 | addi x5, x5, -2048 | x5 = 00001800
M 0000001c | csrc mstatus, x5 | mstatus = 00000000
M 00000020 | mret | next is 00000040, MIE=0, level U
U 00000040 | addi x10, x0, 1 | x10 = 00000001
U 00000044 | sw x10, 256(x20) | mem[80000100] = 00000001
U 00000048 | csrr x11, mstatus | TRAP mcause=2 mepc=00000048 mtval=300025f3, next is 00000080
M 00000080 | csrr x6, mcause | x6 = 00000002
M 00000084 | sw x6, 0(x20) | mem[80000000] = 00000002
M 00000088 | addi x20, x20, 4 | x20 = 80000004
M 0000008c | csrr x7, mepc | x7 = 00000048
M 00000090 | addi x7, x7, 4 | x7 = 0000004c
M 00000094 | csrw mepc, x7 | mepc = 0000004c
M 00000098 | mret | next is 0000004c, MIE=0, level U
U 0000004c | csrw mtvec, x10 | TRAP mcause=2 mepc=0000004c mtval=30551073, next is 00000080
M 00000080 | csrr x6, mcause | x6 = 00000002
M 00000084 | sw x6, 0(x20) | mem[80000004] = 00000002
M 00000088 | addi x20, x20, 4 | x20 = 80000008
M 0000008c | csrr x7, mepc | x7 = 0000004c
M 00000090 | addi x7, x7, 4 | x7 = 00000050
M 00000094 | csrw mepc, x7 | mepc = 00000050
M 00000098 | mret | next is 00000050, MIE=0, level U
U 00000050 | mret | TRAP mcause=2 mepc=00000050 mtval=30200073, next is 00000080
M 00000080 | csrr x6, mcause | x6 = 00000002
M 00000084 | sw x6, 0(x20) | mem[80000008] = 00000002
M 00000088 | addi x20, x20, 4 | x20 = 8000000c
M 0000008c | csrr x7, mepc | x7 = 00000050
M 00000090 | addi x7, x7, 4 | x7 = 00000054
M 00000094 | csrw mepc, x7 | mepc = 00000054
M 00000098 | mret | next is 00000054, MIE=0, level U
U 00000054 | csrr x12, mhartid | TRAP mcause=2 mepc=00000054 mtval=f1402673, next is 00000080
M 00000080 | csrr x6, mcause | x6 = 00000002
M 00000084 | sw x6, 0(x20) | mem[8000000c] = 00000002
M 00000088 | addi x20, x20, 4 | x20 = 80000010
M 0000008c | csrr x7, mepc | x7 = 00000054
M 00000090 | addi x7, x7, 4 | x7 = 00000058
M 00000094 | csrw mepc, x7 | mepc = 00000058
M 00000098 | mret | next is 00000058, MIE=0, level U
U 00000058 | addi x10, x10, 1 | x10 = 00000002
U 0000005c | jal x0, 0x5c | the program stops here
the list: 2 2 2 2
x10=2 x11=00000000 mtvec=00000080 instructions=40 traps=4The first nine rows start with M. They are the start-up code. Its last row, the mret, says "level U". S2.30 explains how.
Rule 2 and its limit: the rows at 40 and 44 start with U. An addi and a store. Both work as at machine level.
Rule 3 at work: the row at 48 is marked as a trap, with mcause=2. x11 was not written. The last line shows it still at 0.
After each trap come seven rows that start with M: the handler. It notes the cause, steps over the instruction and returns.
Rule 3 again at 4c: the write to mtvec traps. The last line shows mtvec at 80, where the start-up code put it.
Rule 5 at work: the row at 50 is an mret at user level. It traps. Its mtval holds the word of the mret itself.
The last lines: four traps, and the list holds cause 2 four times. The user code changed nothing it should not.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| The level is kept by the processor | Each program promises to behave | A wrong program cannot break the promise. A few bits of state, and checks in the decoder. |
| A forbidden instruction traps | It is silently ignored | Trusted code learns of the attempt and can end the program, or do the work for it. |
| The level is in the CSR address | A table of rights for each CSR | Two bits to compare. The address map of the CSRs is fixed by it. |
| Reads are refused as well as writes | Let user code read everything | An application cannot learn the state of the system. Counters that it may read get addresses of their own. |
| Three levels are defined, and a chip may build fewer | Every chip builds all three | A small controller pays for one level only. Software must know which levels exist. |
CSRs are only half of the protection. User code can still load and store anywhere, in this model. Limits on memory are a second mechanism.
RISC-V has such a mechanism. PMP is short for physical memory protection. It is taught later, on S3.10 and S3.25.
The Trap: Code That Only Looks Like User Code
The level is not a property of the code. It is a state of the processor. The same instructions can run at either level.
Suppose the start-up code jumps to the application and forgets to lower the level first.
M 00000000 | lui x20, 0x80000 | x20 = 80000000
M 00000004 | addi x5, x0, 128 | x5 = 00000080
M 00000008 | csrw mtvec, x5 | mtvec = 00000080
M 0000000c | addi x5, x0, 64 | x5 = 00000040
M 00000010 | csrw mepc, x5 | mepc = 00000040
M 00000014 | mret | next is 00000040, MIE=0, level M
M 00000040 | addi x10, x0, 1 | x10 = 00000001
M 00000044 | sw x10, 256(x20) | mem[80000100] = 00000001
M 00000048 | csrr x11, mstatus | x11 = 00000080
M 0000004c | csrw mtvec, x10 | mtvec = 00000001
M 00000050 | addi x10, x10, 1 | x10 = 00000002
M 00000054 | jal x0, 0x54 | the program stops here
the list:
x10=2 x11=00000080 mtvec=00000001 instructions=11 traps=0Read the rows at 48 and 4c. The read of mstatus works. The write to mtvec works too: the handler address is now 1.
No trap, no message. The application ran to its end. The next fault in this system will jump to a wrong address.
So "it ran without a trap" proves nothing. A test of protection must try the forbidden thing and must see the trap.
At Scale
This section looks ahead. Two floors are the smallest building. Larger systems add floors, and walls inside them.
The supervisor level
A full operating system runs its core at supervisor level and its applications at user level. Machine level keeps a small layer below both. S2.30 says how traps are passed on.
Memory needs its own protection
Levels protect CSRs and instructions. Regions of memory are protected by PMP, on S3.10.
The bus can carry the level
A bus has a signal that says whether an access is privileged. A device can refuse an access from user code.
Security builds on levels
A level is the first of several separations in a chip. S9.5 puts them together.
Try It
Run the lab. Find the four rows that start with U and are marked as a trap.
Change the row at 54 to i(CSRR, 12, 0, 'hc00). The model has no CSR at that address. What does the row print, and why is the cause the same?
Remove the row with CSRC from the start-up code. Which letter do the rows of the application start with now, and what does mtvec hold at the end?
M 00000000 | lui x20, 0x80000 | x20 = 80000000 M 00000004 | addi x5, x0, 128 | x5 = 00000080 M 00000008 | csrw mtvec, x5 | mtvec = 00000080 M 0000000c | addi x5, x0, 64 | x5 = 00000040 M 00000010 | csrw mepc, x5 | mepc = 00000040 M 00000014 | lui x5, 0x2 | x5 = 00002000 M 00000018 | addi x5, x5, -2048 | x5 = 00001800 M 0000001c | csrc mstatus, x5 | mstatus = 00000000 M 00000020 | mret | next is 00000040, MIE=0, level U U 00000040 | addi x10, x0, 1 | x10 = 00000001 U 00000044 | sw x10, 256(x20) | mem[80000100] = 00000001 U 00000048 | csrr x11, mstatus | TRAP mcause=2 mepc=00000048 mtval=300025f3, next is 00000080 M 00000080 | csrr x6, mcause | x6 = 00000002 M 00000084 | sw x6, 0(x20) | mem[80000000] = 00000002 M 00000088 | addi x20, x20, 4 | x20 = 80000004 M 0000008c | csrr x7, mepc | x7 = 00000048 M 00000090 | addi x7, x7, 4 | x7 = 0000004c M 00000094 | csrw mepc, x7 | mepc = 0000004c M 00000098 | mret | next is 0000004c, MIE=0, level U U 0000004c | csrw mtvec, x10 | TRAP mcause=2 mepc=0000004c mtval=30551073, next is 00000080 M 00000080 | csrr x6, mcause | x6 = 00000002 M 00000084 | sw x6, 0(x20) | mem[80000004] = 00000002 M 00000088 | addi x20, x20, 4 | x20 = 80000008 M 0000008c | csrr x7, mepc | x7 = 0000004c M 00000090 | addi x7, x7, 4 | x7 = 00000050 M 00000094 | csrw mepc, x7 | mepc = 00000050 M 00000098 | mret | next is 00000050, MIE=0, level U U 00000050 | mret | TRAP mcause=2 mepc=00000050 mtval=30200073, next is 00000080 M 00000080 | csrr x6, mcause | x6 = 00000002 M 00000084 | sw x6, 0(x20) | mem[80000008] = 00000002 M 00000088 | addi x20, x20, 4 | x20 = 8000000c M 0000008c | csrr x7, mepc | x7 = 00000050 M 00000090 | addi x7, x7, 4 | x7 = 00000054 M 00000094 | csrw mepc, x7 | mepc = 00000054 M 00000098 | mret | next is 00000054, MIE=0, level U U 00000054 | csrr x12, mhartid | TRAP mcause=2 mepc=00000054 mtval=f1402673, next is 00000080 M 00000080 | csrr x6, mcause | x6 = 00000002 M 00000084 | sw x6, 0(x20) | mem[8000000c] = 00000002 M 00000088 | addi x20, x20, 4 | x20 = 80000010 M 0000008c | csrr x7, mepc | x7 = 00000054 M 00000090 | addi x7, x7, 4 | x7 = 00000058 M 00000094 | csrw mepc, x7 | mepc = 00000058 M 00000098 | mret | next is 00000058, MIE=0, level U U 00000058 | addi x10, x10, 1 | x10 = 00000002 U 0000005c | jal x0, 0x5c | the program stops here the list: 2 2 2 2 x10=2 x11=00000000 mtvec=00000080 instructions=40 traps=4Check Yourself
Common Mistakes
Wrong Think of user code as code at certain addresses.
The level is a state of the processor. Any code runs at the level the processor is in.
Fix Lower the level before the application starts, and test that it is lowered.
Wrong Expect that user code may at least read a machine CSR.
Bits 9 and 8 of the address apply to reads and writes alike.
Fix A value that applications need gets a CSR address of its own, or a service of the handler.
Wrong Say that ap_rv32 has a user level.
Version 1.0 has machine level only. The user level here is a model.
Fix Name the level of evidence: the model, or the real core.
Remember
- The processor keeps a privilege level. Machine is 3 and may use every instruction and CSR. User is 0. Supervisor, 1, lies between.
- Bits 9 and 8 of a CSR address give the lowest level that may use it. Bits 11 and 10 at 11 mean read-only.
- A forbidden CSR access or an mret at user level traps with cause 2, and changes nothing.
- ap_rv32 v1.0 has machine level only. The user level on this page is a model.