MIT 6.1810: Traps
MIT 6.1810 中第四个 lab 的 solution
Traps
第一个其实是关于RISC-V汇编的一个问答,不是这个Backtrace,但我简单看了一眼懒得做了(
Backtrace (moderate)
For debugging it is often useful to have a backtrace: a list of the function calls on the stack above the point at which the error occurred. To help with backtraces, the compiler generates machine code that maintains a stack frame on the stack corresponding to each function in the current call chain. Each stack frame consists of the return address and a “frame pointer” to the caller’s stack frame. Register s0 contains a pointer to the current stack frame (it actually points to the the address of the saved return address on the stack plus 8). Your backtrace should use the frame pointers to walk up the stack and print the saved return address in each stack frame.
在kernel/printf.c中添加下面这个函数:
|
|
Alarm (hard)
In this exercise you’ll add a feature to xv6 that periodically alerts a process as it uses CPU time. This might be useful for compute-bound processes that want to limit how much CPU time they chew up, or for processes that want to compute but also want to take some periodic action. More generally, you’ll be implementing a primitive form of user-level interrupt/fault handlers; you could use something similar to handle page faults in the application, for example. Your solution is correct if it passes alarmtest and ‘usertests -q’
添加系统调用的部分就不写了。
在kernel/sysproc.c文件中添加以下函数:
|
|
在kernel/proc.h的proc结构体中添加以下内容:
|
|
在kernel/proc.c中的allocproc()
函数对新加入的变量进行初始化:
|
|
在kernel/trap.c中修改usertrap()
函数,下面是修改后的样子:
|
|
不过这样是无法通过usertests -q
的测试的,不过能通过alarmtest这个测试,我本机测试是这样的。
后来发现了,需要把usertrap()
的判断条件写全才行。
|
|
改成
|
|