Entering User Mode and Coming Back: Mret, Ecall and the System Call
User code can do little by itself. It cannot even reach the timer. So it must be able to ask trusted code for help. And trusted code must be able to send it back to work.
You Will Be Able To
- Say what the field MPP holds, and what mret does with it.
- Write the three steps that start an application at user level.
- Follow a system call from the ecall to the return, and say where the answer travels.
- Lesson
- S2.30
- Time
- 35 min
- Runs on
- Your browser
- Check
- 6 questions
S2.14 showed the wall between the levels. A wall with no door is useless. This page is about the two doors.
Before You Start
You need the two levels from S2.14, and what user code cannot do.
You need the entry of a trap and mret from S2.13, and ecall from S2.29.
| Question | |
|---|---|
| 1 | What does the field MPP of mstatus hold? |
| 2 | Which three steps start an application at user level? |
| 3 | Which cause does an ecall at user level have? |
| 4 | How does a system call hand over its value, and get its answer? |
| 5 | What does delegation of a trap mean? |
The Idea: Mret Goes Down, a Trap Comes Up
The processor has no instruction that says "go to user level". It needs none. It has mret.
At a trap the processor notes the level it came from. The note is a field of two bits in mstatus, called MPP.
mret reads the note back. It returns to the address in mepc, and to the level in MPP.
Now the trick. Both notes are CSRs, and machine code can write them. It writes "user" into MPP and an address into mepc.
Then it runs mret. The processor returns to a place that never called: the start of the application, at user level.
The way back up is a trap. The application runs an ecall. It lands in the handler, at machine level.
An ecall that asks trusted code for a service is a system call. The application says which service, by a number in a register.
A full system has a supervisor level too. There, machine code can pass chosen traps on to the supervisor. That is delegation.
New Words
| Word | Meaning |
|---|---|
| MPP | Machine previous privilege: bits 12 and 11 of mstatus. The level the processor was at before the trap. |
| System call | A request from an application to trusted code, made with an ecall. |
| Service number | The number that says which service a system call wants. In this lab it is in x17. |
| Delegation | Machine code lets a kind of trap go straight to the handler of the supervisor level. |
csrc | The instruction that clears bits of a CSR: every bit that is 1 in the register becomes 0 in the CSR. |
The values of MPP are the level numbers: 11 for machine and 00 for user.
One Picture: The Two Doors in Use
- Processor
- Memory
- Control / FSM
- Peripheral
- Prepare the door down
The start-up code writes 00 into MPP: user. It writes the first address of the application into mepc.
- mret
mret loads the level from MPP and the PC from mepc, in one step. The application runs, at user level.
- The application asks
It puts a value into x10 and a service number into x17. Then it runs an ecall.
- The door up
The ecall traps with cause 8. The level becomes machine. MPP takes 00 and mepc the address of the ecall.
- Serve, and return
The handler does the work and leaves the answer in x10. It adds 4 to mepc. Its mret goes back down.
Read the diagram as text
- Start-up code: machine level
- MPP: two bits of mstatus: the level to return to
- mepc: the address to return to
- Handler: machine level: serves the call
- Application: user level: value in x10, service number in x17
- Start-up code → MPP: 1: MPP = 00
- Start-up code → mepc: 2: mepc = 40
- MPP → Application: 3: mret: level and PC
- Application → Handler: 4: ecall: cause 8
- Handler → mepc: 5: mepc plus 4
- mepc → Application: 6: mret, back down
Going down is a return to a place that never called. Coming up is a trap that the application asked for.
| Event | The level | MPP | The PC |
|---|---|---|---|
| A trap, from any level | becomes machine | takes the level before the trap | takes the handler address |
| mret | takes MPP | becomes 00, the lowest level | takes mepc |
These two rows are added to the entry and the return of S2.13. Not checked against the specification.
The Smallest Example, Worked by Hand
| Address, in hex | Instruction | What it is for |
|---|---|---|
| 000 | addi x5, x0, 128 | machine level: the handler address |
| 004 | csrw mtvec, x5 | |
| 008 | addi x5, x0, 64 | where the user code starts |
| 00c | csrw mepc, x5 | |
| 010 | lui x5, 0x2 | bits 12 and 11: the field MPP |
| 014 | addi x5, x5, -2048 | |
| 018 | csrc mstatus, x5 | MPP = 00: user |
| 01c | mret | go down to the user code |
| 040 | addi x10, x0, 21 | user code: the value |
| 044 | addi x17, x0, 1 | the service number |
| 048 | ecall | ask the machine level |
| 04c | addi x11, x10, 0 | the answer came back in x10 |
| 050 | addi x17, x0, 0 | service 0 does not exist |
| 054 | ecall | |
| 058 | addi x11, x0, -1 | never reached |
The start-up code is at 00 to 1c. The application starts at 40. The handler is at 80 and is shown in the lab.
After reset the level is machine, and mstatus holds 0000_1800. Bits 12 and 11 are 11: MPP says machine.
The rows at 10 and 14 build the value 0000_1800 in x5. The csrc at 18 clears those two bits. MPP now says user.
| After the row at | Level | MPP | mepc | What happened |
|---|---|---|---|---|
| 0c | M | 11 | 40 | mepc is written |
| 18 | M | 00 | 40 | MPP is cleared |
| 1c | U | 00 | 40 | mret: the application starts at 40 |
| 48 | M | 00 | 48 | ecall: a trap from user level |
| a4 | U | 00 | 4c | mret: back behind the ecall |
Before the row at a4, the handler has added 4 to mepc.
At 48 the application has 21 in x10 and 1 in x17. The ecall traps. The handler sees cause 8 and service 1.
Service 1 doubles x10. The handler leaves 42 there. It touches no other register that the application uses.
The Rules
Rule 1: At a trap, MPP takes the level before the trap, and the level becomes machine.
So the handler can tell where the trap came from.
Rule 2: mret sets the level from MPP and the PC from mepc.
After it, MPP holds 00. A second mret without a trap would go to user level.
Rule 3: To start an application: write mepc, clear MPP, run mret.
Three steps of machine code. The order of the first two does not matter.
Rule 4: An ecall at user level has cause 8. At machine level it has cause 11.
The handler tells a system call from its own ecalls by the cause.
Rule 5: A system call passes a service number and a value in registers, and gets its answer in a register.
Here: x17 for the number, x10 for the value and the answer. That is a choice of the software.
Rule 6: The handler of a system call returns to mepc plus 4.
MPP still says user, so its mret goes back down by itself.
Sources for These Rules
The field MPP, what mret does with it, cause 8 for an ecall at user level and the delegation of traps 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. Its MPP always reads 11 (docs/soc/ap_rv32-blueprint.md, section 5.1), and its ecall has cause 11 (section 7.1), as S2.13 and S2.29 showed on the real core. Every row on this page is printed by the lab, which is a model. The registers x17 and x10 for a system call are the choice of this lab. They follow common practice for RISC-V. Checked in the library: Patterson and Hennessy, Computer Organization and Design (ARM edition), section 4.9, names the call of the operating system from a user program as one kind of exception.
Now Run It
The lab at the end of this page runs the program above on the model. Each row starts with the level: M or U.
M 00000000 | addi x5, x0, 128 | x5 = 00000080
M 00000004 | csrw mtvec, x5 | mtvec = 00000080
M 00000008 | addi x5, x0, 64 | x5 = 00000040
M 0000000c | csrw mepc, x5 | mepc = 00000040
M 00000010 | lui x5, 0x2 | x5 = 00002000
M 00000014 | addi x5, x5, -2048 | x5 = 00001800
M 00000018 | csrc mstatus, x5 | mstatus = 00000000
M 0000001c | mret | next is 00000040, MIE=0, level U
U 00000040 | addi x10, x0, 21 | x10 = 00000015
U 00000044 | addi x17, x0, 1 | x17 = 00000001
U 00000048 | ecall | TRAP mcause=8 mepc=00000048 mtval=00000000, next is 00000080
M 00000080 | csrr x5, mcause | x5 = 00000008
M 00000084 | addi x6, x0, 8 | x6 = 00000008
M 00000088 | bne x5, x6, 0xa8 | not taken
M 0000008c | addi x6, x0, 1 | x6 = 00000001
M 00000090 | bne x17, x6, 0xa8 | not taken
M 00000094 | add x10, x10, x10 | x10 = 0000002a
M 00000098 | csrr x5, mepc | x5 = 00000048
M 0000009c | addi x5, x5, 4 | x5 = 0000004c
M 000000a0 | csrw mepc, x5 | mepc = 0000004c
M 000000a4 | mret | next is 0000004c, MIE=0, level U
U 0000004c | addi x11, x10, 0 | x11 = 0000002a
U 00000050 | addi x17, x0, 0 | x17 = 00000000
U 00000054 | ecall | TRAP mcause=8 mepc=00000054 mtval=00000000, next is 00000080
M 00000080 | csrr x5, mcause | x5 = 00000008
M 00000084 | addi x6, x0, 8 | x6 = 00000008
M 00000088 | bne x5, x6, 0xa8 | not taken
M 0000008c | addi x6, x0, 1 | x6 = 00000001
M 00000090 | bne x17, x6, 0xa8 | taken: next is 000000a8
M 000000a8 | jal x0, 0xa8 | the program stops here
x10=42 x11=42 mcause=8 level=3 mstatus=00000000 instructions=27 traps=2Rule 3 at work: the rows at 0c, 18 and 1c. mepc takes 40. mstatus goes from 1800 to 0. The mret says "level U".
Rule 4 at work: the row at 48 starts with U and is marked as a trap, with mcause=8. The next row starts with M: the handler.
The handler first checks the cause, at 80 to 88. Then the service number, at 8c and 90. Then it doubles x10, at 94.
Rule 6 at work: the rows at 98 to a4. There mepc goes from 48 to 4c, and the mret says "level U" again.
Rule 5 at work: the row at 4c, back at user level, copies the answer. x11 is 2a in hex: 42.
The second ecall asks for service 0. The handler knows no such service. It jumps to a8 and ends the application there.
The last line: the level is 3, machine. mstatus is 0: MPP holds 00, because the last trap came from user level.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Go down with mret | A second instruction "enter user level" | One mechanism for the first entry and for every return. The first entry looks like a return. |
| Come up only by a trap | A call instruction into trusted code | User code cannot choose where it enters. Every service pays the price of a trap. |
| The service number in a register | A different trap for each service | One cause and one entry. The handler must check the number. |
| MPP is written by the trap | The handler works out the level | The return is right without any work. One field more in mstatus. |
| Delegation is a choice of machine code | Every trap goes to machine level first | A system call reaches the supervisor at once. One more CSR to set up. |
The handler must not trust the application. A service number may be wrong, and a value may be an address the application should not use.
On a chip with a supervisor level, most traps are meant for the operating system. Machine code lists those causes in a CSR.
A listed trap then skips the machine handler. It uses supervisor CSRs with the same roles: an address, a saved PC, a cause.
The Trap: The Level Is Lowered, and the Address Is Forgotten
The start-up code needs three steps. Suppose it clears MPP and runs mret, and never writes mepc.
M 00000000 | addi x5, x0, 128 | x5 = 00000080
M 00000004 | csrw mtvec, x5 | mtvec = 00000080
M 00000008 | lui x5, 0x2 | x5 = 00002000
M 0000000c | addi x5, x5, -2048 | x5 = 00001800
M 00000010 | csrc mstatus, x5 | mstatus = 00000000
M 00000014 | mret | next is 00000000, MIE=0, level U
U 00000000 | addi x5, x0, 128 | x5 = 00000080
U 00000004 | csrw mtvec, x5 | TRAP mcause=2 mepc=00000004 mtval=30529073, next is 00000080
M 00000080 | csrr x5, mcause | x5 = 00000002
M 00000084 | addi x6, x0, 8 | x6 = 00000008
M 00000088 | bne x5, x6, 0xa8 | taken: next is 000000a8
M 000000a8 | jal x0, 0xa8 | the program stops here
x10=0 x11=0 mcause=2 level=3 mstatus=00000000 instructions=10 traps=1After reset mepc holds 0. The mret goes there, at user level. The row after it starts with U and has the address 00000000.
So the start-up code runs a second time, now as user code. Its second row writes mtvec. That is refused: cause 2.
The handler sees a cause that is not 8 and ends the "application". The real application at 40 never ran.
The protection worked, and the system is still dead. Lowering the level and choosing the address belong together.
At Scale
This section looks ahead. One service and one application are the smallest case. An operating system is the large one.
Every file and every message
An application reads a file or sends data with a system call. Each one is a trap up and an mret, or its supervisor twin, down. That is common practice.
The handler saves registers
The handler here uses x5 and x6 without saving them. A real one saves all it uses, on a stack of its own that user code cannot reach.
Arguments are checked
A service that takes an address must check it against the regions of the application. S3.10 has the regions.
On ap_rv32 today
Version 1.0 runs everything at machine level. Its software calls functions directly. User level is planned for a later version.
Try It
Run the lab. Find the two rows with an ecall, and the row where the level changes back to U.
Change the first row of the application to i(ADDI, 10, 0, 50). What does the last line show for x10 and x11?
Remove the row with CSRC. The application now runs at machine level. Which cause does its ecall have, and what does the handler do with it?
M 00000000 | addi x5, x0, 128 | x5 = 00000080 M 00000004 | csrw mtvec, x5 | mtvec = 00000080 M 00000008 | addi x5, x0, 64 | x5 = 00000040 M 0000000c | csrw mepc, x5 | mepc = 00000040 M 00000010 | lui x5, 0x2 | x5 = 00002000 M 00000014 | addi x5, x5, -2048 | x5 = 00001800 M 00000018 | csrc mstatus, x5 | mstatus = 00000000 M 0000001c | mret | next is 00000040, MIE=0, level U U 00000040 | addi x10, x0, 21 | x10 = 00000015 U 00000044 | addi x17, x0, 1 | x17 = 00000001 U 00000048 | ecall | TRAP mcause=8 mepc=00000048 mtval=00000000, next is 00000080 M 00000080 | csrr x5, mcause | x5 = 00000008 M 00000084 | addi x6, x0, 8 | x6 = 00000008 M 00000088 | bne x5, x6, 0xa8 | not taken M 0000008c | addi x6, x0, 1 | x6 = 00000001 M 00000090 | bne x17, x6, 0xa8 | not taken M 00000094 | add x10, x10, x10 | x10 = 0000002a M 00000098 | csrr x5, mepc | x5 = 00000048 M 0000009c | addi x5, x5, 4 | x5 = 0000004c M 000000a0 | csrw mepc, x5 | mepc = 0000004c M 000000a4 | mret | next is 0000004c, MIE=0, level U U 0000004c | addi x11, x10, 0 | x11 = 0000002a U 00000050 | addi x17, x0, 0 | x17 = 00000000 U 00000054 | ecall | TRAP mcause=8 mepc=00000054 mtval=00000000, next is 00000080 M 00000080 | csrr x5, mcause | x5 = 00000008 M 00000084 | addi x6, x0, 8 | x6 = 00000008 M 00000088 | bne x5, x6, 0xa8 | not taken M 0000008c | addi x6, x0, 1 | x6 = 00000001 M 00000090 | bne x17, x6, 0xa8 | taken: next is 000000a8 M 000000a8 | jal x0, 0xa8 | the program stops here x10=42 x11=42 mcause=8 level=3 mstatus=00000000 instructions=27 traps=2Check Yourself
Common Mistakes
Wrong Jump to the application with a jal or a jalr.
A jump changes the PC and never the level. The application runs at machine level.
Fix Write mepc, clear MPP and run mret.
Wrong Serve every trap from user code as a system call.
An illegal instruction also lands in the handler. Its registers are not a request.
Fix Check mcause first: 8 is a system call.
Wrong Return from a system call to mepc as it is.
mepc points at the ecall. The application would call again, for ever.
Fix Add 4 to mepc before the mret.
Remember
- MPP, bits 12 and 11 of mstatus, holds the level before the trap. mret sets the level from it.
- To start an application: write mepc, clear MPP, run mret.
- An ecall at user level traps with cause 8. The handler serves it and returns to mepc plus 4.
- ap_rv32 v1.0 has machine level only: its MPP always reads 11. This page ran on a model.