Substituted to the original ch7 code.
This commit is contained in:
parent
4fdd55e2e8
commit
57f7debbc6
50 changed files with 2786 additions and 128 deletions
|
@ -1,6 +1,12 @@
|
|||
use crate::mm::{UserBuffer, translated_byte_buffer, translated_refmut};
|
||||
use crate::mm::{
|
||||
UserBuffer,
|
||||
translated_byte_buffer,
|
||||
translated_refmut,
|
||||
translated_str,
|
||||
};
|
||||
use crate::task::{current_user_token, current_task};
|
||||
use crate::fs::make_pipe;
|
||||
use crate::fs::{make_pipe, OpenFlags, open_file};
|
||||
use alloc::sync::Arc;
|
||||
|
||||
pub fn sys_write(fd: usize, buf: *const u8, len: usize) -> isize {
|
||||
let token = current_user_token();
|
||||
|
@ -10,6 +16,9 @@ pub fn sys_write(fd: usize, buf: *const u8, len: usize) -> isize {
|
|||
return -1;
|
||||
}
|
||||
if let Some(file) = &inner.fd_table[fd] {
|
||||
if !file.writable() {
|
||||
return -1;
|
||||
}
|
||||
let file = file.clone();
|
||||
// release current task TCB manually to avoid multi-borrow
|
||||
drop(inner);
|
||||
|
@ -30,6 +39,9 @@ pub fn sys_read(fd: usize, buf: *const u8, len: usize) -> isize {
|
|||
}
|
||||
if let Some(file) = &inner.fd_table[fd] {
|
||||
let file = file.clone();
|
||||
if !file.readable() {
|
||||
return -1;
|
||||
}
|
||||
// release current task TCB manually to avoid multi-borrow
|
||||
drop(inner);
|
||||
file.read(
|
||||
|
@ -40,6 +52,23 @@ pub fn sys_read(fd: usize, buf: *const u8, len: usize) -> isize {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn sys_open(path: *const u8, flags: u32) -> isize {
|
||||
let task = current_task().unwrap();
|
||||
let token = current_user_token();
|
||||
let path = translated_str(token, path);
|
||||
if let Some(inode) = open_file(
|
||||
path.as_str(),
|
||||
OpenFlags::from_bits(flags).unwrap()
|
||||
) {
|
||||
let mut inner = task.inner_exclusive_access();
|
||||
let fd = inner.alloc_fd();
|
||||
inner.fd_table[fd] = Some(inode);
|
||||
fd as isize
|
||||
} else {
|
||||
-1
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sys_close(fd: usize) -> isize {
|
||||
let task = current_task().unwrap();
|
||||
let mut inner = task.inner_exclusive_access();
|
||||
|
@ -65,4 +94,18 @@ pub fn sys_pipe(pipe: *mut usize) -> isize {
|
|||
*translated_refmut(token, pipe) = read_fd;
|
||||
*translated_refmut(token, unsafe { pipe.add(1) }) = write_fd;
|
||||
0
|
||||
}
|
||||
|
||||
pub fn sys_dup(fd: usize) -> isize {
|
||||
let task = current_task().unwrap();
|
||||
let mut inner = task.inner_exclusive_access();
|
||||
if fd >= inner.fd_table.len() {
|
||||
return -1;
|
||||
}
|
||||
if inner.fd_table[fd].is_none() {
|
||||
return -1;
|
||||
}
|
||||
let new_fd = inner.alloc_fd();
|
||||
inner.fd_table[new_fd] = Some(Arc::clone(inner.fd_table[fd].as_ref().unwrap()));
|
||||
new_fd as isize
|
||||
}
|
|
@ -1,3 +1,5 @@
|
|||
const SYSCALL_DUP: usize = 24;
|
||||
const SYSCALL_OPEN: usize = 56;
|
||||
const SYSCALL_CLOSE: usize = 57;
|
||||
const SYSCALL_PIPE: usize = 59;
|
||||
const SYSCALL_READ: usize = 63;
|
||||
|
@ -18,6 +20,8 @@ use process::*;
|
|||
|
||||
pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
|
||||
match syscall_id {
|
||||
SYSCALL_DUP=> sys_dup(args[0]),
|
||||
SYSCALL_OPEN => sys_open(args[0] as *const u8, args[1] as u32),
|
||||
SYSCALL_CLOSE => sys_close(args[0]),
|
||||
SYSCALL_PIPE => sys_pipe(args[0] as *mut usize),
|
||||
SYSCALL_READ => sys_read(args[0], args[1] as *const u8, args[2]),
|
||||
|
@ -27,7 +31,7 @@ pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
|
|||
SYSCALL_GET_TIME => sys_get_time(),
|
||||
SYSCALL_GETPID => sys_getpid(),
|
||||
SYSCALL_FORK => sys_fork(),
|
||||
SYSCALL_EXEC => sys_exec(args[0] as *const u8),
|
||||
SYSCALL_EXEC => sys_exec(args[0] as *const u8, args[1] as *const usize),
|
||||
SYSCALL_WAITPID => sys_waitpid(args[0] as isize, args[1] as *mut i32),
|
||||
_ => panic!("Unsupported syscall_id: {}", syscall_id),
|
||||
}
|
||||
|
|
|
@ -9,9 +9,15 @@ use crate::timer::get_time_ms;
|
|||
use crate::mm::{
|
||||
translated_str,
|
||||
translated_refmut,
|
||||
translated_ref,
|
||||
};
|
||||
use crate::fs::{
|
||||
open_file,
|
||||
OpenFlags,
|
||||
};
|
||||
use crate::loader::get_app_data_by_name;
|
||||
use alloc::sync::Arc;
|
||||
use alloc::vec::Vec;
|
||||
use alloc::string::String;
|
||||
|
||||
pub fn sys_exit(exit_code: i32) -> ! {
|
||||
exit_current_and_run_next(exit_code);
|
||||
|
@ -45,13 +51,25 @@ pub fn sys_fork() -> isize {
|
|||
new_pid as isize
|
||||
}
|
||||
|
||||
pub fn sys_exec(path: *const u8) -> isize {
|
||||
pub fn sys_exec(path: *const u8, mut args: *const usize) -> isize {
|
||||
let token = current_user_token();
|
||||
let path = translated_str(token, path);
|
||||
if let Some(data) = get_app_data_by_name(path.as_str()) {
|
||||
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 task = current_task().unwrap();
|
||||
task.exec(data);
|
||||
0
|
||||
let argc = args_vec.len();
|
||||
task.exec(all_data.as_slice(), args_vec);
|
||||
// return argc because cx.x[10] will be covered with it later
|
||||
argc as isize
|
||||
} else {
|
||||
-1
|
||||
}
|
||||
|
@ -63,7 +81,7 @@ pub fn sys_waitpid(pid: isize, exit_code_ptr: *mut i32) -> isize {
|
|||
let task = current_task().unwrap();
|
||||
// find a child process
|
||||
|
||||
// ---- access current TCB exclusively
|
||||
// ---- access current PCB exclusively
|
||||
let mut inner = task.inner_exclusive_access();
|
||||
if inner.children
|
||||
.iter()
|
||||
|
@ -76,16 +94,16 @@ pub fn sys_waitpid(pid: isize, exit_code_ptr: *mut i32) -> isize {
|
|||
.iter()
|
||||
.enumerate()
|
||||
.find(|(_, p)| {
|
||||
// ++++ temporarily access child PCB lock exclusively
|
||||
// ++++ temporarily access child PCB exclusively
|
||||
p.inner_exclusive_access().is_zombie() && (pid == -1 || pid as usize == p.getpid())
|
||||
// ++++ release child PCB
|
||||
});
|
||||
if let Some((idx, _)) = pair {
|
||||
let child = inner.children.remove(idx);
|
||||
// confirm that child will be deallocated after removing from children list
|
||||
// confirm that child will be deallocated after being removed from children list
|
||||
assert_eq!(Arc::strong_count(&child), 1);
|
||||
let found_pid = child.getpid();
|
||||
// ++++ temporarily access child TCB exclusively
|
||||
// ++++ temporarily access child PCB exclusively
|
||||
let exit_code = child.inner_exclusive_access().exit_code;
|
||||
// ++++ release child PCB
|
||||
*translated_refmut(inner.memory_set.token(), exit_code_ptr) = exit_code;
|
||||
|
@ -93,5 +111,5 @@ pub fn sys_waitpid(pid: isize, exit_code_ptr: *mut i32) -> isize {
|
|||
} else {
|
||||
-2
|
||||
}
|
||||
// ---- release current PCB lock automatically
|
||||
// ---- release current PCB automatically
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue