Exercise: Trap pretty printing
Easy
An exception is typically an unusual condition that occurs at runtime, interrupting the program and transferring control to a supervisor-mode exception handler. In RISC-V terminology, this transfer of control is called a trap, and the exception handler is referred to as a trap handler. Not only exceptions, but also other events, such as system calls (the ecall instruction in RISC-V) or interrupts (external asynchronous events like keyboard presses), result in a trap. The trap handler’s role is to identify the source of the trap and handle it appropriately.
Some exceptions lead to program termination. Many of you have likely triggered a program-terminating exception while testing your C programs, for instance because you forgot to initialize a pointer. In C, an uninitialized pointer contains a random, typically invalid value and dereferencing such a pointer triggers an exception. When this occurs, the operating system terminates the faulty process and displays some diagnostic information, such as the dreadful Segmentation fault (core dumped) error message. xv6 reports the trap cause along with other relevant details for program-terminating exceptions.
The table below provides an overview of the possible trap causes that can be handled in supervisor mode, as specified via the scause register defined in the privileged RISC-V ISA specification.

This exercise focuses on making the exception handler messages of xv6 easier to understand.
To test each step of this exercise, you can use the following userspace program handlertest.c:
#include "user/user.h"
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Needs to be invoked with a command line argument (1-3)\n");
return 1;
}
char arg = argv[1][0];
int *ptr = 0;
switch (arg) {
case '1':
printf("Testing illegal instruction:\n");
asm(".word 0x0");
break;
case '2':
printf("Testing null pointer dereference (store):\n");
*ptr = 42;
printf("Value: %d\n", *ptr);
break;
case '3':
printf("Testing instruction page fault:\n");
int (*fptr)(void) = (void*)0x31337;
fptr();
break;
default:
printf("Unknown test case\n");
break;
}
return 0;
}
- Look at the
usertrapfunction and understand when the exception handling code is invoked and what information it contains. - Modify this function to present a more informative message when an illegal instruction is encountered, communicating the address of the faulting instruction and the type of the exception. You can test this by invoking the first test case of the
handlertestprogram:$ handlertest 1 Testing illegal instruction: usertrap(): illegal instruction in process pid=3 at instruction pointer = 0x38 - Now add a check that presents a different error message when a store to a null pointer is encountered (which triggers a store page fault). This is a common mistake when writing C code and having a clear error message for this type of mistake is useful when debugging code. Test your solution with the second test case.
$ handlertest 2 Testing null pointer dereference (store): usertrap(): store to nullptr in process pid=4 at PC = 0x66 - Finally, make sure that other types of exceptions are still handled with the original exception message with the third test case:
$ handlertest 3 Testing instruction page fault: usertrap(): unexpected scause 0xc pid=5 sepc=0x31336 stval=0x31336 - Try adding another test case to
handlertestthat loads the value from a null pointer. Does this raise an exception? Why (not)?