S2.14.1 Drop to User Mode

MediumImplement50 minIcarus Verilog · serverReview the topic

Write the start-up code that hands the processor to an application at user level, and the handler that serves its system calls and ends it when it misbehaves.

Hardware context: A small model of a RISC-V processor with a machine level and a user level, as test code; start-up code lowers the level and an application tries four things that only the machine level may do. The real core ap_rv32 v1.0 has machine level only. Planning which code runs at which level, and testing that protection really refuses

The lab of the lesson started one application at a fixed address and served one service. A real start-up layer is given the address, serves several services, and must survive an application that breaks the rules.

Write that layer: start-up code and a trap handler, as rows for the model with a user level. The application belongs to the bench. This model is not the real core: ap_rv32 v1.0 has machine level only.

Signals in the Timeline

SignalWidthRole
trial10input of each check
Full specification
At reset
The application must run at user level, from its first instruction on. Every register except x5, x6 and x7 must reach it as it is at reset.
ThingValue
The levelmachine
The PC0: your first row
x10the address of the first instruction of the application
mstatus, mtvec, mepc, mscratch0000_1800, 0, 0, 0
A trap from the application
To stop: stay at machine level and jump to the same address for ever.
mcausex17What the handler does
81x10 takes x10 plus x11. Return behind the ecall.
82x10 takes the number of system calls so far, this one included. Calls of service 0 do not count. Return behind the ecall.
80The application is finished. Write x10 to the word at 8000_0000, then stop.
8anything elsex10 takes minus 1. The call counts. Return behind the ecall.
anything elseanyEnd the application. Write 100 in hex plus mcause to the word at 8000_0000, then stop.

How to read a result: ended_at_machine_level is 1 when the run stopped as the table says. application_ran_only_at_user_level is 1 when every instruction of the application ran at user level, and at least one did. other_registers_kept is 1 when the handler changed no register but x5, x6, x7 and x10. exit_word is the word at 8000_0000, in decimal.

Examples

Example 1

One Service, Then the End
Input
the application adds 30 and 12 with service 1, then ends with service 0
Output
ended_at_machine_level = 1, application_ran_only_at_user_level = 1, other_registers_kept = 1, exit_word = 42

The exit word holds 42. The application ran at user level, and the run ended at machine level.

Example 2

A Forbidden Write
Input
the application writes mtvec
Output
ended_at_machine_level = 1, application_ran_only_at_user_level = 1, other_registers_kept = 1, exit_word = 258

The write is refused with cause 2. The handler ends the application: the exit word is 100 in hex plus 2, which is 258.

Example 3

The Count of Calls
Input
three calls of service 2, then the end
Output
ended_at_machine_level = 1, application_ran_only_at_user_level = 1, other_registers_kept = 1, exit_word = 6

Service 2 answers 1, 2 and 3: the calls so far. Their sum, 6, is the exit word.

Constraints and Contract

  • Your rows start at address 0, and there are at most 64. The application is somewhere from 100 in hex up.
  • The handler may change x5, x6 and x7. It may change x10 only as the table says. Every other register belongs to the application.
  • The application may store to any word of data memory. Keep nothing there that the handler depends on.
  • A run that executes more than 3000 instructions and traps is stopped and refused.

Hints

Hint 1

A jump never changes the level. Which instruction does, and which two CSRs does it read?

Hint 2

The handler has two questions to ask before it serves anything: why did the trap come, and which service is wanted?

Hint 3

The count must live where the application cannot write it. Look at the CSRs of the model.

What the grader checks: One service and the end. A forbidden write to mtvec. The count of calls. An unknown service number. An mret, an ebreak and a misaligned load in the application. A read of mscratch by the application. An application that ends at once. Stores to data memory between two calls. Negative values. Three mixed applications at several entry addresses, one ending in a forbidden CSR write. The level of every instruction of the application, the registers and the exit word are checked.

14 checks on Icarus Verilog · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain your source only; samples require the simulation service. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 2 problems on this path

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

One Service, Then the End

Input
the application adds 30 and 12 with service 1, then ends with service 0
Expected output
ended_at_machine_level = 1, application_ran_only_at_user_level = 1, other_registers_kept = 1, exit_word = 42

The exit word holds 42. The application ran at user level, and the run ended at machine level.

Run samples executes all 3 public checks. Check all tests runs 14 checks on the server.

Icarus Verilog · server