A trap handler serves an ecall correctly. With a word that is no instruction, the program never ends. Find the fault, and one more that hides behind it.
Hardware context: A small model of a RISC-V processor with traps, as test code: seven machine CSRs, the entry of a trap and mret; every program also runs on the real core ap_rv32. Writing start-up code and trap handlers, and reading a trap in a trace
The handler below serves two kinds of trap. It was tested with a program that made one ecall, and it passed.
Then a program with a faulty word was run. It never reached its end. Repair the handler. You write it as rows, one for each instruction, as in the lab of the lesson.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
trial | 10 | input of each check |
Full specification
| mcause | The trap | The work | Where to return |
|---|---|---|---|
| 11 | An ecall | x10 takes x10 plus x11 | Behind the ecall |
| 2 | A word that is no instruction | Add 1 to the word at 8000_0000 | Behind the word |
| Thing | Rule for the handler |
|---|---|
| x10 | Changed only by the service of an ecall |
| Every other register, x2 included | Must hold after mret what it held at the trap |
| Memory at the sp of the program and above | Belongs to the program: do not change it |
| Memory below that sp | Free for the handler, up to 64 bytes |
| mscratch | Free for the handler |
How to read a result: reached_the_end is 1 when the program of the bench came to its last row. other_registers_kept is 1 when every register but x10 holds what the program expects. frame_of_the_program_kept is 1 when the four words at its sp and above are unchanged by the handler. count_low_byte is the low byte of the word at 8000_0000, and x10 is in decimal.
Examples
Example 1
One Service Call- Input
one ecall, with x10 = 5 and x11 = 7
- Output
reached_the_end = 1, other_registers_kept = 1, frame_of_the_program_kept = 1, count_low_byte = 236, x10 = 12
The service adds x11 to x10: 12. The handler returns behind the ecall, and every other register is as it was.
Example 2
A Word That Is No Instruction- Input
one word that is no instruction, then an ecall
- Output
reached_the_end = 1, other_registers_kept = 1, frame_of_the_program_kept = 1, count_low_byte = 157, x10 = 2
The count goes up by one. The handler steps over the word, and the ecall behind it is served.
Example 3
Registers of the Program- Input
the program uses x5 and x6 around an ecall
- Output
reached_the_end = 1, other_registers_kept = 1, frame_of_the_program_kept = 1, count_low_byte = 76, x10 = 132
x5 and x6 belong to the program. They hold 100 and 23 after the ecall, so x10 ends at 9 plus 123.
Constraints and Contract
- The handler starts at address 0 and has at most 64 rows. mtvec holds 0: do not write it.
- The program of the bench starts at 100 in hex. It may use every register, and it has values of its own in the words at its sp.
- The count at 8000_0000 does not start at 0. Add to what is there.
- A run that executes more than 3000 instructions and traps is stopped and refused.
Hints
Hint 1
Follow the rows for mcause 2 by hand, from the branch to the mret. What does mepc hold at the mret?
Hint 2
Count the words that the handler stores, and the bytes it takes below sp.
Hint 3
After your repair, run the third public case again. It tells you whether the program got its registers back.
What the grader checks: One ecall. An illegal word, and an ecall behind it. A program that uses x5 and x6 around an ecall. Two illegal words in a row. A program that reads its own frame after a trap. An ecall as the last instruction. x11 negative and zero. A trap right after a store to the frame. Four mixed programs of 20 to 44 instructions. Every register, the four words of the frame, the count and the end address are checked.
12 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 3 problems on this path