2018-11-07 09:34:31 +04:00
|
|
|
//! System call
|
2018-05-21 12:30:30 +04:00
|
|
|
|
2018-06-03 19:05:43 +04:00
|
|
|
#![allow(unused)]
|
|
|
|
|
2018-05-12 23:41:41 +04:00
|
|
|
use arch::interrupt::TrapFrame;
|
2018-05-21 12:30:30 +04:00
|
|
|
use process::*;
|
2018-05-31 16:26:25 +04:00
|
|
|
use thread;
|
2018-05-21 12:30:30 +04:00
|
|
|
use util;
|
2018-11-07 09:34:31 +04:00
|
|
|
use simple_filesystem::{INode, file::File};
|
|
|
|
use core::{slice, str};
|
|
|
|
use alloc::boxed::Box;
|
2018-05-12 23:41:41 +04:00
|
|
|
|
2018-11-07 09:34:31 +04:00
|
|
|
/// System call dispatcher
|
2018-07-14 08:28:55 +04:00
|
|
|
pub fn syscall(id: usize, args: [usize; 6], tf: &TrapFrame) -> i32 {
|
2018-05-12 23:41:41 +04:00
|
|
|
match id {
|
2018-11-07 09:34:31 +04:00
|
|
|
// file
|
|
|
|
100 => sys_open(args[0] as *const u8, args[1]),
|
|
|
|
101 => sys_close(args[0]),
|
|
|
|
102 => sys_read(args[0], args[1] as *mut u8, args[2]),
|
|
|
|
103 => sys_write(args[0], args[1] as *const u8, args[2]),
|
|
|
|
030 => sys_putc(args[0] as u8 as char),
|
|
|
|
// 104 => sys_seek(),
|
|
|
|
// 110 => sys_fstat(),
|
|
|
|
// 111 => sys_fsync(),
|
|
|
|
// 121 => sys_getcwd(),
|
|
|
|
// 128 => sys_getdirentry(),
|
|
|
|
// 128 => sys_dup(),
|
|
|
|
|
|
|
|
// process
|
|
|
|
001 => sys_exit(args[0]),
|
|
|
|
002 => sys_fork(tf),
|
|
|
|
003 => sys_wait(args[0], args[1] as *mut i32),
|
|
|
|
// 004 => sys_exec(),
|
|
|
|
// 005 => sys_clone(),
|
|
|
|
010 => sys_yield(),
|
|
|
|
011 => sys_sleep(args[0]),
|
|
|
|
012 => sys_kill(args[0]),
|
|
|
|
017 => sys_get_time(),
|
|
|
|
018 => sys_getpid(),
|
|
|
|
255 => sys_lab6_set_priority(args[0]),
|
|
|
|
|
|
|
|
// memory
|
|
|
|
// 020 => sys_mmap(),
|
|
|
|
// 021 => sys_munmap(),
|
|
|
|
// 022 => sys_shmem(),
|
|
|
|
// 031 => sys_pgdir(),
|
|
|
|
|
2018-05-12 23:41:41 +04:00
|
|
|
_ => {
|
2018-07-14 14:42:58 +04:00
|
|
|
error!("unknown syscall id: {:#x?}, args: {:x?}", id, args);
|
2018-07-17 08:07:21 +04:00
|
|
|
::trap::error(tf);
|
2018-05-19 20:22:52 +04:00
|
|
|
}
|
2018-05-12 23:41:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 09:34:31 +04:00
|
|
|
fn sys_read(fd: usize, base: *mut u8, len: usize) -> i32 {
|
|
|
|
info!("read: fd: {}, base: {:?}, len: {:#x}", fd, base, len);
|
|
|
|
let slice = unsafe { slice::from_raw_parts_mut(base, len) };
|
|
|
|
match fd {
|
|
|
|
0 => unimplemented!(),
|
|
|
|
1 | 2 => return -1,
|
|
|
|
_ => {
|
|
|
|
let mut file = process().files.get_mut(&fd);
|
|
|
|
if file.is_none() {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
let file = file.as_mut().unwrap();
|
|
|
|
file.read(slice).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
0
|
|
|
|
}
|
|
|
|
|
2018-05-21 12:30:30 +04:00
|
|
|
fn sys_write(fd: usize, base: *const u8, len: usize) -> i32 {
|
|
|
|
info!("write: fd: {}, base: {:?}, len: {:#x}", fd, base, len);
|
|
|
|
let slice = unsafe { slice::from_raw_parts(base, len) };
|
2018-11-07 09:34:31 +04:00
|
|
|
match fd {
|
|
|
|
0 => return -1,
|
|
|
|
1 | 2 => print!("{}", str::from_utf8(slice).unwrap()),
|
|
|
|
_ => {
|
|
|
|
let mut file = process().files.get_mut(&fd);
|
|
|
|
if file.is_none() {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
let file = file.as_mut().unwrap();
|
|
|
|
file.write(slice).unwrap();
|
|
|
|
}
|
|
|
|
}
|
2018-05-21 12:30:30 +04:00
|
|
|
0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn sys_open(path: *const u8, flags: usize) -> i32 {
|
|
|
|
let path = unsafe { util::from_cstr(path) };
|
2018-11-07 09:34:31 +04:00
|
|
|
let flags = VfsFlags::from_ucore_flags(flags);
|
2018-05-21 12:30:30 +04:00
|
|
|
info!("open: path: {:?}, flags: {:?}", path, flags);
|
|
|
|
match path {
|
2018-11-07 09:34:31 +04:00
|
|
|
"stdin:" => return 0,
|
|
|
|
"stdout:" => return 1,
|
|
|
|
"stderr:" => return 2,
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
let inode = ::fs::ROOT_INODE.lookup(path);
|
|
|
|
if inode.is_err() {
|
|
|
|
return -1;
|
2018-05-21 12:30:30 +04:00
|
|
|
}
|
2018-11-07 09:34:31 +04:00
|
|
|
let inode = inode.unwrap();
|
|
|
|
let files = &mut process().files;
|
|
|
|
let fd = (3..).find(|i| !files.contains_key(i)).unwrap();
|
|
|
|
let file = File::new(inode, flags.contains(VfsFlags::READABLE), flags.contains(VfsFlags::WRITABLE));
|
|
|
|
files.insert(fd, Box::new(file));
|
|
|
|
fd as i32
|
2018-05-21 12:30:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn sys_close(fd: usize) -> i32 {
|
|
|
|
info!("close: fd: {:?}", fd);
|
2018-11-07 09:34:31 +04:00
|
|
|
if fd < 3 {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
match process().files.remove(&fd) {
|
|
|
|
Some(_) => 0,
|
|
|
|
None => -1,
|
|
|
|
}
|
2018-05-21 12:30:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Fork the current process. Return the child's PID.
|
|
|
|
fn sys_fork(tf: &TrapFrame) -> i32 {
|
2018-10-31 20:45:02 +04:00
|
|
|
let mut context = process().fork(tf);
|
|
|
|
let pid = processor().manager().add(context);
|
|
|
|
Process::new_fork(pid, thread::current().id());
|
2018-10-24 17:29:31 +04:00
|
|
|
info!("fork: {} -> {}", thread::current().id(), pid);
|
|
|
|
pid as i32
|
2018-05-21 12:30:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Wait the process exit.
|
|
|
|
/// Return the PID. Store exit code to `code` if it's not null.
|
2018-05-21 16:07:20 +04:00
|
|
|
fn sys_wait(pid: usize, code: *mut i32) -> i32 {
|
2018-10-25 20:49:19 +04:00
|
|
|
loop {
|
2018-10-31 20:45:02 +04:00
|
|
|
let wait_procs = match pid {
|
|
|
|
0 => Process::get_children(),
|
|
|
|
_ => vec![pid],
|
|
|
|
};
|
|
|
|
if wait_procs.is_empty() {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for pid in wait_procs {
|
|
|
|
match processor().manager().get_status(pid) {
|
|
|
|
Some(Status::Exited(exit_code)) => {
|
|
|
|
if !code.is_null() {
|
|
|
|
unsafe { code.write(exit_code as i32); }
|
|
|
|
}
|
|
|
|
processor().manager().remove(pid);
|
|
|
|
Process::do_wait(pid);
|
|
|
|
info!("wait: {} -> {}", thread::current().id(), pid);
|
|
|
|
return 0;
|
2018-10-25 20:49:19 +04:00
|
|
|
}
|
2018-10-31 20:45:02 +04:00
|
|
|
None => return -1,
|
|
|
|
_ => {}
|
2018-10-25 20:49:19 +04:00
|
|
|
}
|
|
|
|
}
|
2018-11-01 17:10:19 +04:00
|
|
|
info!("wait: {} -> {}, sleep", thread::current().id(), pid);
|
2018-10-31 20:45:02 +04:00
|
|
|
if pid == 0 {
|
|
|
|
Process::wait_child();
|
|
|
|
} else {
|
|
|
|
processor().manager().wait(thread::current().id(), pid);
|
|
|
|
processor().yield_now();
|
|
|
|
}
|
2018-10-25 20:49:19 +04:00
|
|
|
}
|
2018-05-21 12:30:30 +04:00
|
|
|
}
|
|
|
|
|
2018-05-21 16:07:20 +04:00
|
|
|
fn sys_yield() -> i32 {
|
2018-05-31 16:26:25 +04:00
|
|
|
thread::yield_now();
|
2018-05-21 12:30:30 +04:00
|
|
|
0
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Kill the process
|
|
|
|
fn sys_kill(pid: usize) -> i32 {
|
2018-11-01 17:10:19 +04:00
|
|
|
info!("kill: {}", pid);
|
2018-10-25 20:49:19 +04:00
|
|
|
processor().manager().exit(pid, 0x100);
|
2018-10-31 20:45:02 +04:00
|
|
|
if pid == thread::current().id() {
|
|
|
|
processor().yield_now();
|
|
|
|
}
|
2018-05-21 12:30:30 +04:00
|
|
|
0
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the current process id
|
|
|
|
fn sys_getpid() -> i32 {
|
2018-05-31 16:26:25 +04:00
|
|
|
thread::current().id() as i32
|
2018-05-21 12:30:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Exit the current process
|
2018-10-23 20:27:25 +04:00
|
|
|
fn sys_exit(exit_code: usize) -> i32 {
|
2018-10-24 17:29:31 +04:00
|
|
|
let pid = thread::current().id();
|
2018-11-01 17:10:19 +04:00
|
|
|
info!("exit: {}", pid);
|
2018-10-25 20:49:19 +04:00
|
|
|
processor().manager().exit(pid, exit_code);
|
2018-10-23 20:27:25 +04:00
|
|
|
processor().yield_now();
|
2018-10-25 20:49:19 +04:00
|
|
|
unreachable!();
|
2018-05-21 12:30:30 +04:00
|
|
|
}
|
|
|
|
|
2018-05-21 16:07:20 +04:00
|
|
|
fn sys_sleep(time: usize) -> i32 {
|
2018-06-01 07:47:58 +04:00
|
|
|
use core::time::Duration;
|
|
|
|
thread::sleep(Duration::from_millis(time as u64 * 10));
|
2018-05-21 12:30:30 +04:00
|
|
|
0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn sys_get_time() -> i32 {
|
2018-10-25 20:49:19 +04:00
|
|
|
unsafe { ::trap::TICK as i32 }
|
2018-05-21 12:30:30 +04:00
|
|
|
}
|
|
|
|
|
2018-05-23 08:41:13 +04:00
|
|
|
fn sys_lab6_set_priority(priority: usize) -> i32 {
|
2018-10-24 17:29:31 +04:00
|
|
|
let pid = thread::current().id();
|
|
|
|
processor().manager().set_priority(pid, priority as u8);
|
2018-05-23 08:41:13 +04:00
|
|
|
0
|
|
|
|
}
|
|
|
|
|
2018-07-14 08:28:55 +04:00
|
|
|
fn sys_putc(c: char) -> i32 {
|
|
|
|
print!("{}", c);
|
|
|
|
0
|
2018-05-17 18:15:26 +04:00
|
|
|
}
|
|
|
|
|
2018-11-07 09:34:31 +04:00
|
|
|
bitflags! {
|
|
|
|
struct VfsFlags: usize {
|
|
|
|
// WARNING: different from origin uCore
|
|
|
|
const READABLE = 1 << 0;
|
|
|
|
const WRITABLE = 1 << 1;
|
|
|
|
/// create file if it does not exist
|
|
|
|
const CREATE = 1 << 2;
|
|
|
|
/// error if O_CREAT and the file exists
|
|
|
|
const EXCLUSIVE = 1 << 3;
|
|
|
|
/// truncate file upon open
|
|
|
|
const TRUNCATE = 1 << 4;
|
|
|
|
/// append on each write
|
|
|
|
const APPEND = 1 << 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VfsFlags {
|
|
|
|
fn from_ucore_flags(f: usize) -> Self {
|
|
|
|
assert_ne!(f & 0b11, 0b11);
|
|
|
|
Self::from_bits_truncate(f + 1)
|
|
|
|
}
|
|
|
|
}
|