S2.13.3 The Handler That Returned to the Same Fault

MediumDebug35 minIcarus Verilog · serverReview the topic

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

SignalWidthRole
trial10input of each check
Full specification
What the handler must do
The bench raises no other trap.
mcauseThe trapThe workWhere to return
11An ecallx10 takes x10 plus x11Behind the ecall
2A word that is no instructionAdd 1 to the word at 8000_0000Behind the word
What belongs to whom
x2 is the stack pointer, sp. At every trap it is a multiple of 4 and points into data memory.
ThingRule for the handler
x10Changed only by the service of an ecall
Every other register, x2 includedMust hold after mret what it held at the trap
Memory at the sp of the program and aboveBelongs to the program: do not change it
Memory below that spFree for the handler, up to 64 bytes
mscratchFree 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

Report a problem with this page

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

One Service Call

Input
one ecall, with x10 = 5 and x11 = 7
Expected 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.

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

Icarus Verilog · server