1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use crate::fs::{open_file, OpenFlags};
use crate::mm::{translated_ref, translated_refmut, translated_str};
use crate::task::{
current_process, current_task, current_user_token, exit_current_and_run_next, pid2process,
suspend_current_and_run_next, SignalFlags,
};
use crate::timer::get_time_ms;
use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec::Vec;
pub fn sys_exit(exit_code: i32) -> ! {
kprintln!("[KERN] syscall::process::sys_exit begin");
exit_current_and_run_next(exit_code);
panic!("Unreachable in sys_exit!");
}
pub fn sys_yield() -> isize {
suspend_current_and_run_next();
0
}
pub fn sys_get_time() -> isize {
kprintln!("[KERN] syscall::process::sys_get_time begin");
get_time_ms() as isize
}
pub fn sys_getpid() -> isize {
kprintln!("[KERN] syscall::process::sys_getpid begin");
current_task().unwrap().process.upgrade().unwrap().getpid() as isize
}
pub fn sys_fork() -> isize {
kprintln!("[KERN] syscall::process::sys_fork begin");
let current_process = current_process();
let new_process = current_process.fork();
let new_pid = new_process.getpid();
let new_process_inner = new_process.inner_exclusive_access();
let task = new_process_inner.tasks[0].as_ref().unwrap();
let trap_cx = task.inner_exclusive_access().get_trap_cx();
trap_cx.x[10] = 0;
kprintln!("[KERN] syscall::process::sys_fork end");
new_pid as isize
}
pub fn sys_exec(path: *const u8, mut args: *const usize) -> isize {
kprintln!("[KERN] syscall::process::sys_exec begin");
let token = current_user_token();
let path = translated_str(token, path);
let mut args_vec: Vec<String> = Vec::new();
loop {
let arg_str_ptr = *translated_ref(token, args);
if arg_str_ptr == 0 {
break;
}
args_vec.push(translated_str(token, arg_str_ptr as *const u8));
unsafe {
args = args.add(1);
}
}
if let Some(app_inode) = open_file(path.as_str(), OpenFlags::RDONLY) {
let all_data = app_inode.read_all();
let process = current_process();
let argc = args_vec.len();
process.exec(all_data.as_slice(), args_vec);
kprintln!("[KERN] syscall::process::sys_exec end");
argc as isize
} else {
-1
}
}
pub fn sys_waitpid(pid: isize, exit_code_ptr: *mut i32) -> isize {
let process = current_process();
let mut inner = process.inner_exclusive_access();
if !inner
.children
.iter()
.any(|p| pid == -1 || pid as usize == p.getpid())
{
return -1;
}
let pair = inner.children.iter().enumerate().find(|(_, p)| {
p.inner_exclusive_access().is_zombie && (pid == -1 || pid as usize == p.getpid())
});
if let Some((idx, _)) = pair {
kprintln!("[KERN] syscall::process::sys_waitpid(): remove child from PCB's children Vector");
let child = inner.children.remove(idx);
assert_eq!(Arc::strong_count(&child), 1);
let found_pid = child.getpid();
kprintln!("[KERN] syscall::process::sys_waitpid(): get child's exit_code and return child pid");
let exit_code = child.inner_exclusive_access().exit_code;
*translated_refmut(inner.memory_set.token(), exit_code_ptr) = exit_code;
kprintln!("[KERN] syscall::process::sys_waitpid(): release child PCB");
found_pid as isize
} else {
-2
}
}
pub fn sys_kill(pid: usize, signal: u32) -> isize {
kprintln!("[KERN] syscall::process::sys_kill begin");
if let Some(process) = pid2process(pid) {
if let Some(flag) = SignalFlags::from_bits(signal) {
process.inner_exclusive_access().signals |= flag;
0
} else {
-1
}
} else {
-1
}
}