Skip to content

Commit 082e200

Browse files
committed
update ch03
1 parent a92bcdd commit 082e200

File tree

4 files changed

+94
-13
lines changed

4 files changed

+94
-13
lines changed

os/src/syscall/mod.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,28 @@
1111
//! submodules, and you should also implement syscalls this way.
1212
1313
/// write syscall
14-
const SYSCALL_WRITE: usize = 64;
14+
pub const SYSCALL_WRITE: usize = 64;
1515
/// exit syscall
16-
const SYSCALL_EXIT: usize = 93;
16+
pub const SYSCALL_EXIT: usize = 93;
1717
/// yield syscall
18-
const SYSCALL_YIELD: usize = 124;
18+
pub const SYSCALL_YIELD: usize = 124;
1919
/// gettime syscall
20-
const SYSCALL_GET_TIME: usize = 169;
20+
pub const SYSCALL_GET_TIME: usize = 169;
2121
/// trace syscall
22-
const SYSCALL_TRACE: usize = 410;
22+
pub const SYSCALL_TRACE: usize = 410;
2323

2424
mod fs;
2525
mod process;
2626

27+
use crate::task::inc_current_syscall_times;
2728
use fs::*;
2829
use process::*;
2930

3031
/// handle syscall exception with `syscall_id` and other arguments
3132
pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
33+
// inc the syscall_time of current app (valid only)
34+
inc_current_syscall_times(syscall_id);
35+
3236
match syscall_id {
3337
SYSCALL_WRITE => sys_write(args[0], args[1] as *const u8, args[2]),
3438
SYSCALL_EXIT => sys_exit(args[0] as i32),

os/src/syscall/process.rs

Lines changed: 24 additions & 4 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::{exit_current_and_run_next, query_current_syscall_times, suspend_current_and_run_next},
44
timer::get_time_us,
55
};
66

@@ -39,7 +39,27 @@ pub fn sys_get_time(ts: *mut TimeVal, _tz: usize) -> isize {
3939
}
4040

4141
// TODO: implement the syscall
42-
pub fn sys_trace(_trace_request: usize, _id: usize, _data: usize) -> isize {
43-
trace!("kernel: sys_trace");
44-
-1
42+
43+
const READ_DATA: usize = 0;
44+
const WRITE_DATA: usize = 1;
45+
const QUERY_SYSCALL_TIME: usize = 2;
46+
47+
pub fn sys_trace(trace_request: usize, id: usize, data: usize) -> isize {
48+
match trace_request {
49+
READ_DATA => {
50+
let ptr = id as *const u8;
51+
52+
unsafe { *ptr as isize }
53+
}
54+
WRITE_DATA => {
55+
let ptr = id as *mut u8;
56+
57+
unsafe { *ptr = data as u8 }
58+
59+
0
60+
}
61+
QUERY_SYSCALL_TIME => query_current_syscall_times(id) as isize,
62+
_ => -1,
63+
// _ => panic!("Unsupport trace_request for sys_trace"),
64+
}
4565
}

os/src/task/mod.rs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ mod task;
1717
use crate::config::MAX_APP_NUM;
1818
use crate::loader::{get_num_app, init_app_cx};
1919
use crate::sync::UPSafeCell;
20+
use crate::syscall::{SYSCALL_EXIT, SYSCALL_GET_TIME, SYSCALL_TRACE, SYSCALL_WRITE, SYSCALL_YIELD};
2021
use lazy_static::*;
2122
use switch::__switch;
2223
pub use task::{TaskControlBlock, TaskStatus};
@@ -51,10 +52,7 @@ lazy_static! {
5152
/// Global variable: TASK_MANAGER
5253
pub static ref TASK_MANAGER: TaskManager = {
5354
let num_app = get_num_app();
54-
let mut tasks = [TaskControlBlock {
55-
task_cx: TaskContext::zero_init(),
56-
task_status: TaskStatus::UnInit,
57-
}; MAX_APP_NUM];
55+
let mut tasks = [TaskControlBlock::new(); MAX_APP_NUM];
5856
for (i, task) in tasks.iter_mut().enumerate() {
5957
task.task_cx = TaskContext::goto_restore(init_app_cx(i));
6058
task.task_status = TaskStatus::Ready;
@@ -169,3 +167,37 @@ pub fn exit_current_and_run_next() {
169167
mark_current_exited();
170168
run_next_task();
171169
}
170+
171+
/// Modified: to inc syscall_time in the TCB of current task
172+
173+
pub fn inc_current_syscall_times(syscall_id: usize) {
174+
let mut inner = TASK_MANAGER.inner.exclusive_access();
175+
let current_app_id = inner.current_task;
176+
let current_tcb = inner.tasks.get_mut(current_app_id).unwrap();
177+
178+
match syscall_id {
179+
SYSCALL_WRITE => current_tcb.sys_write_times += 1,
180+
SYSCALL_EXIT => current_tcb.sys_exit_times += 1,
181+
SYSCALL_YIELD => current_tcb.sys_yield_times += 1,
182+
SYSCALL_GET_TIME => current_tcb.sys_get_time_times += 1,
183+
SYSCALL_TRACE => current_tcb.sys_trace_times += 1,
184+
_ => panic!("Unsupported syscall_id: {}", syscall_id),
185+
}
186+
}
187+
188+
/// Modified: to query syscall_time in the TCB of current task
189+
190+
pub fn query_current_syscall_times(syscall_id: usize) -> usize {
191+
let inner = TASK_MANAGER.inner.exclusive_access();
192+
let current_app_id = inner.current_task;
193+
let current_tcb = inner.tasks.get(current_app_id).unwrap();
194+
195+
match syscall_id {
196+
SYSCALL_WRITE => current_tcb.sys_write_times,
197+
SYSCALL_EXIT => current_tcb.sys_exit_times,
198+
SYSCALL_YIELD => current_tcb.sys_yield_times,
199+
SYSCALL_GET_TIME => current_tcb.sys_get_time_times,
200+
SYSCALL_TRACE => current_tcb.sys_trace_times,
201+
_ => panic!("Unsupported syscall_id: {}", syscall_id),
202+
}
203+
}

os/src/task/task.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,31 @@ pub struct TaskControlBlock {
99
pub task_status: TaskStatus,
1010
/// The task context
1111
pub task_cx: TaskContext,
12+
/// sys_write times
13+
pub sys_write_times: usize,
14+
/// sys_exit times
15+
pub sys_exit_times: usize,
16+
/// sys_yield times
17+
pub sys_yield_times: usize,
18+
/// sys get_time times
19+
pub sys_get_time_times: usize,
20+
/// sys trace times
21+
pub sys_trace_times: usize,
22+
}
23+
24+
impl TaskControlBlock {
25+
/// quick init of TCB
26+
pub fn new() -> TaskControlBlock {
27+
TaskControlBlock {
28+
task_cx: TaskContext::zero_init(),
29+
task_status: TaskStatus::UnInit,
30+
sys_write_times: 0,
31+
sys_exit_times: 0,
32+
sys_yield_times: 0,
33+
sys_get_time_times: 0,
34+
sys_trace_times: 0,
35+
}
36+
}
1237
}
1338

1439
/// The status of a task

0 commit comments

Comments
 (0)