Skip to content

Commit 21f86d9

Browse files
committed
impl sys_trace
1 parent 3a0858b commit 21f86d9

File tree

3 files changed

+112
-8
lines changed

3 files changed

+112
-8
lines changed

lab3.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# 功能简述
2+
3+
# 问答题
4+
5+
1. 正确进入 U 态后,程序的特征还应有:使用 S 态特权指令,访问 S 态寄存器后会报错。 请同学们可以自行测试这些内容(运行 三个 bad 测例 (`ch2b_bad_*.rs`) ), 描述程序出错行为,同时注意注明你使用的 sbi 及其版本。
6+
2. 深入理解 trap.S 中两个函数 `alltraps``restore` 的作用,并回答如下问题:
7+
8+
1. L40:刚进入 `restore` 时,sp 代表了什么值。请指出 `restore` 的两种使用情景。
9+
10+
2. L43-L48:这几行汇编代码特殊处理了哪些寄存器?这些寄存器的的值对于进入用户态有何意义?请分别解释。
11+
12+
```asm
13+
ld t0, 32*8(sp)
14+
ld t1, 33*8(sp)
15+
ld t2, 2\*8(sp)
16+
csrw sstatus, t0
17+
csrw sepc, t1
18+
csrw sscratch, t2
19+
```
20+
21+
3. L50-L56:为何跳过了 x2 和 x4?
22+
23+
```asm
24+
ld x1, 1*8(sp)
25+
ld x3, 3*8(sp)
26+
.set n, 5
27+
.rept 27
28+
LOAD_GP %n
29+
.set n, n+1
30+
.endr
31+
```
32+
33+
4. L60:该指令之后,sp 和 sscratch 中的值分别有什么意义?
34+
35+
```asm
36+
csrrw sp, sscratch, sp
37+
```
38+
39+
5. `__restore`:中发生状态切换在哪一条指令?为何该指令执行之后会进入用户态?
40+
41+
6. L13:该指令之后,sp 和 sscratch 中的值分别有什么意义?
42+
43+
```asm
44+
csrrw sp, sscratch, sp
45+
```
46+
47+
7. 从 U 态进入 S 态是哪一条指令发生的?
48+
49+
# 荣誉准则
50+
51+
1. 在完成本次实验的过程(含此前学习的过程)中,我曾分别与 以下各位 就(与本次实验相关的)以下方面做过交流,还在代码中对应的位置以注释形式记录了具体的交流对象及内容:
52+
53+
54+
55+
2. 此外,我也参考了 以下资料 ,还在代码中对应的位置以注释形式记录了具体的参考来源及内容:
56+
57+
58+
59+
3. 我独立完成了本次实验除以上方面之外的所有工作,包括代码与文档。 我清楚地知道,从以上方面获得的信息在一定程度上降低了实验难度,可能会影响起评分。
60+
61+
4. 我从未使用过他人的代码,不管是原封不动地复制,还是经过了某些等价转换。 我未曾也不会向他人(含此后各届同学)复制或公开我的实验代码,我有义务妥善保管好它们。 我提交至本实验的评测系统的代码,均无意于破坏或妨碍任何计算机系统的正常运转。 我清楚地知道,以上情况均为本课程纪律所禁止,若违反,对应的实验成绩将按“-100”分计。

os/src/syscall/mod.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,39 @@ const SYSCALL_TRACE: usize = 410;
2424
mod fs;
2525
mod process;
2626

27+
use core::sync::atomic::{AtomicIsize, Ordering};
28+
2729
use fs::*;
2830
use process::*;
2931

32+
static SYSCALL_WRITE_COUNT: AtomicIsize = AtomicIsize::new(0);
33+
static SYSCALL_EXIT_COUNT: AtomicIsize = AtomicIsize::new(0);
34+
static SYSCALL_YIELD_COUNT: AtomicIsize = AtomicIsize::new(0);
35+
static SYSCALL_GET_TIME_COUNT: AtomicIsize = AtomicIsize::new(0);
36+
static SYSCALL_TRACE_COUNT: AtomicIsize = AtomicIsize::new(0);
3037
/// handle syscall exception with `syscall_id` and other arguments
3138
pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
3239
match syscall_id {
33-
SYSCALL_WRITE => sys_write(args[0], args[1] as *const u8, args[2]),
34-
SYSCALL_EXIT => sys_exit(args[0] as i32),
35-
SYSCALL_YIELD => sys_yield(),
36-
SYSCALL_GET_TIME => sys_get_time(args[0] as *mut TimeVal, args[1]),
37-
SYSCALL_TRACE => sys_trace(args[0], args[1], args[2]),
40+
SYSCALL_WRITE => {
41+
SYSCALL_WRITE_COUNT.fetch_add(1, Ordering::Relaxed);
42+
sys_write(args[0], args[1] as *const u8, args[2])
43+
}
44+
SYSCALL_EXIT => {
45+
SYSCALL_EXIT_COUNT.fetch_add(1, Ordering::Relaxed);
46+
sys_exit(args[0] as i32)
47+
}
48+
SYSCALL_YIELD => {
49+
SYSCALL_YIELD_COUNT.fetch_add(1, Ordering::Relaxed);
50+
sys_yield()
51+
}
52+
SYSCALL_GET_TIME => {
53+
SYSCALL_GET_TIME_COUNT.fetch_add(1, Ordering::Relaxed);
54+
sys_get_time(args[0] as *mut TimeVal, args[1])
55+
}
56+
SYSCALL_TRACE => {
57+
SYSCALL_TRACE_COUNT.fetch_add(1, Ordering::Relaxed);
58+
sys_trace(args[0], args[1], args[2])
59+
}
3860
_ => panic!("Unsupported syscall_id: {}", syscall_id),
3961
}
4062
}

os/src/syscall/process.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
//! Process management syscalls
2+
use core::sync::atomic::Ordering;
3+
24
use crate::{
5+
syscall::{
6+
SYSCALL_EXIT, SYSCALL_EXIT_COUNT, SYSCALL_GET_TIME, SYSCALL_GET_TIME_COUNT, SYSCALL_TRACE,
7+
SYSCALL_TRACE_COUNT, SYSCALL_WRITE, SYSCALL_WRITE_COUNT, SYSCALL_YIELD,
8+
SYSCALL_YIELD_COUNT,
9+
},
310
task::{exit_current_and_run_next, suspend_current_and_run_next},
411
timer::get_time_us,
512
};
@@ -38,8 +45,22 @@ pub fn sys_get_time(ts: *mut TimeVal, _tz: usize) -> isize {
3845
0
3946
}
4047

41-
// TODO: implement the syscall
42-
pub fn sys_trace(_trace_request: usize, _id: usize, _data: usize) -> isize {
48+
pub fn sys_trace(trace_request: usize, id: usize, data: usize) -> isize {
4349
trace!("kernel: sys_trace");
44-
-1
50+
match trace_request {
51+
0 => return unsafe { (id as *const u8).read_volatile() } as isize,
52+
1 => {
53+
unsafe { (id as *mut u8).write_volatile(data as u8) };
54+
return 0;
55+
}
56+
2 => match id {
57+
SYSCALL_EXIT => SYSCALL_EXIT_COUNT.load(Ordering::Relaxed),
58+
SYSCALL_GET_TIME => SYSCALL_GET_TIME_COUNT.load(Ordering::Relaxed),
59+
SYSCALL_WRITE => SYSCALL_WRITE_COUNT.load(Ordering::Relaxed),
60+
SYSCALL_TRACE => SYSCALL_TRACE_COUNT.load(Ordering::Relaxed),
61+
SYSCALL_YIELD => SYSCALL_YIELD_COUNT.load(Ordering::Relaxed),
62+
_ => return 0,
63+
},
64+
_ => return -1,
65+
}
4566
}

0 commit comments

Comments
 (0)