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
| Signal | Width | Role |
|---|---|---|
trial | 10 | input of each check |
Full specification
| Thing | Value |
|---|---|
| The level | machine |
| The PC | 0: your first row |
| x10 | the address of the first instruction of the application |
| mstatus, mtvec, mepc, mscratch | 0000_1800, 0, 0, 0 |
| mcause | x17 | What the handler does |
|---|---|---|
| 8 | 1 | x10 takes x10 plus x11. Return behind the ecall. |
| 8 | 2 | x10 takes the number of system calls so far, this one included. Calls of service 0 do not count. Return behind the ecall. |
| 8 | 0 | The application is finished. Write x10 to the word at 8000_0000, then stop. |
| 8 | anything else | x10 takes minus 1. The call counts. Return behind the ecall. |
| anything else | any | End 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