MIT 6.S081 Lab traps
Compulsory exercises
Preparation
- reading
- To start the lab, switch to the trap branch
1 | git fetch |
RISC-V assembly (easy)
- Which registers contain arguments to functions? For example, which register holds 13 in main’s call to printf?
- a0-a1 a2-a7; a2
- Where is the call to function f in the assembly code for main? Where is the call to g? (Hint: the compiler may inline functions.)
- f in printf 0x34 , g in f 0x14
- At what address is the function printf located?
- 0x630
- What value is in the register ra just after the jalr to printf in main?
- 0x38
- Run the following code.
1 | unsigned int i = 0x00646c72; |
- What is the output?
- 注意字节序是小端序
- He110 World
- The output depends on that fact that the RISC-V is little-endian. If the RISC-V were instead big-endian what would you set i to in order to yield the same output? Would you need to change 57616 to a different value?
- i = 0x72646c00 -> “ord\0”
- no
- In the following code, what is going to be printed after ‘y=’? (note: the answer is not a specific value.) Why does this happen?
1 | printf("x=%d y=%d", 3); |
- x=3 y=寄存器a2的十进制值,因为在printf没有传入第三个参数,a2没有被更新,因此直接把原来a2的值传入
Backtrace (moderate)
-
该任务是打印出错堆栈前的所有返回地址ra
-
Implement a backtrace() function in kernel/printf.c
-
添加定义和内联函数(内核中)
1 | void backtrace(void); |
1 | // store frame pointer |
-
注意ra在fp-8处,上一个fp在fp-16处
-
使用riscv中的宏定义PGROUNDDOWN(fp)和PGROUNDUP(fp)确定循环终止条件
-
实现backtrace()
1 | void backtrace(void) |
-
添加至sys_sleep()和panic()
1 | backtrace(); |
1 | backtrace(); |
Alarm (hard)
-
添加系统调用在计时器中断时打印alarm
-
test0: invoke handler
-
修改proc.h
1 | int ticks; |
-
需要完成sigalarm系统调用,并且添加sigreturn系统调用(空)
1 | uint64 |
-
修改usertrap()
1 | //test0 |
-
test1/test2(): resume interrupted code
-
test0尚未完成返回,因此程序在alarm后崩溃,需要补全sigreturn
-
修改proc.h用于保存alarm时的寄存器信息,inhandler防止函数被多次调用
-
需要初始化的参数可以通过allocproc()初始化
1 | int ticks; |
-
完成sigreturn系统调用
1 | uint64 |
-
修改usertrap()
1 | //test12 |
Optional challenge exercises
-
Print the names of the functions and line numbers in backtrace() instead of numerical addresses (hard).
MIT 6.S081 Lab traps