Skip to content

Commit dd901c4

Browse files
committed
finish ch3
1 parent 03e9c2d commit dd901c4

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

os/src/syscall/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ mod process;
2727
use fs::*;
2828
use process::*;
2929

30+
use crate::task::TASK_MANAGER;
31+
3032
/// handle syscall exception with `syscall_id` and other arguments
3133
pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
34+
TASK_MANAGER.syscall_count(syscall_id);
3235
match syscall_id {
3336
SYSCALL_WRITE => sys_write(args[0], args[1] as *const u8, args[2]),
3437
SYSCALL_EXIT => sys_exit(args[0] as i32),

os/src/syscall/process.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Process management syscalls
22
use crate::{
3-
task::{exit_current_and_run_next, suspend_current_and_run_next},
3+
task::{TASK_MANAGER, exit_current_and_run_next, suspend_current_and_run_next},
44
timer::get_time_us,
55
};
66

@@ -41,5 +41,21 @@ pub fn sys_get_time(ts: *mut TimeVal, _tz: usize) -> isize {
4141
// TODO: implement the syscall
4242
pub fn sys_trace(_trace_request: usize, _id: usize, _data: usize) -> isize {
4343
trace!("kernel: sys_trace");
44-
-1
44+
match _trace_request {
45+
0 => {
46+
let addr = _id as *const u8;
47+
unsafe { core::ptr::read_volatile(addr) as isize }
48+
},
49+
1 => {
50+
let addr = _id as *mut u8;
51+
unsafe { core::ptr::write_volatile(addr, _data as u8) };
52+
0
53+
},
54+
2 => {
55+
TASK_MANAGER.get_syscall_count(_id) as isize
56+
},
57+
_ => {
58+
-1
59+
}
60+
}
4561
}

os/src/task/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ pub struct TaskManagerInner {
4545
tasks: [TaskControlBlock; MAX_APP_NUM],
4646
/// id of current `Running` task
4747
current_task: usize,
48+
/// syscall count for each syscall id
49+
syscall_count: [[usize; 512]; MAX_APP_NUM],
4850
}
4951

5052
lazy_static! {
@@ -65,6 +67,7 @@ lazy_static! {
6567
UPSafeCell::new(TaskManagerInner {
6668
tasks,
6769
current_task: 0,
70+
syscall_count: [[0; 512]; MAX_APP_NUM],
6871
})
6972
},
7073
}
@@ -135,6 +138,19 @@ impl TaskManager {
135138
panic!("All applications completed!");
136139
}
137140
}
141+
142+
/// Get syscall count for a syscall id.
143+
pub fn get_syscall_count(&self, syscall_id: usize) -> usize {
144+
let inner = self.inner.exclusive_access();
145+
inner.syscall_count[inner.current_task][syscall_id]
146+
}
147+
148+
/// Increment syscall count for a syscall id.
149+
pub fn syscall_count(&self, syscall_id: usize) {
150+
let mut inner = self.inner.exclusive_access();
151+
let current = inner.current_task;
152+
inner.syscall_count[current][syscall_id] += 1;
153+
}
138154
}
139155

140156
/// Run the first task in task list.

0 commit comments

Comments
 (0)