2018-11-07 09:34:31 +04:00
|
|
|
//! System call
|
2018-05-21 12:30:30 +04:00
|
|
|
|
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 19:32:22 +04:00
|
|
|
use simple_filesystem::{INode, file::File, FileInfo, FileType};
|
2018-11-07 09:34:31 +04:00
|
|
|
use core::{slice, str};
|
2018-11-07 20:21:20 +04:00
|
|
|
use alloc::sync::Arc;
|
|
|
|
use spin::Mutex;
|
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(),
|
2018-11-07 19:32:22 +04:00
|
|
|
110 => sys_fstat(args[0], args[1] as *mut Stat),
|
2018-11-07 09:34:31 +04:00
|
|
|
// 111 => sys_fsync(),
|
|
|
|
// 121 => sys_getcwd(),
|
2018-11-08 12:56:01 +04:00
|
|
|
128 => sys_getdirentry(args[0], args[1] as *mut DirEntry),
|
2018-11-07 20:21:20 +04:00
|
|
|
130 => sys_dup(args[0], args[1]),
|
2018-11-07 09:34:31 +04:00
|
|
|
|
|
|
|
// process
|
2018-11-07 20:21:20 +04:00
|
|
|
001 => sys_exit(args[0] as i32),
|
2018-11-07 09:34:31 +04:00
|
|
|
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 {
|
2018-11-07 20:21:20 +04:00
|
|
|
// TODO: check ptr
|
2018-11-07 09:34:31 +04:00
|
|
|
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,
|
|
|
|
_ => {
|
2018-11-07 20:21:20 +04:00
|
|
|
let file = process().files.get_mut(&fd);
|
2018-11-07 09:34:31 +04:00
|
|
|
if file.is_none() {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-11-07 19:32:22 +04:00
|
|
|
let file = file.unwrap();
|
2018-11-07 20:21:20 +04:00
|
|
|
file.lock().read(slice).unwrap();
|
2018-11-07 09:34:31 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
0
|
|
|
|
}
|
|
|
|
|
2018-05-21 12:30:30 +04:00
|
|
|
fn sys_write(fd: usize, base: *const u8, len: usize) -> i32 {
|
2018-11-07 20:21:20 +04:00
|
|
|
// TODO: check ptr
|
2018-05-21 12:30:30 +04:00
|
|
|
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()),
|
|
|
|
_ => {
|
2018-11-07 20:21:20 +04:00
|
|
|
let file = process().files.get_mut(&fd);
|
2018-11-07 09:34:31 +04:00
|
|
|
if file.is_none() {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-11-07 19:32:22 +04:00
|
|
|
let file = file.unwrap();
|
2018-11-07 20:21:20 +04:00
|
|
|
file.lock().write(slice).unwrap();
|
2018-11-07 09:34:31 +04:00
|
|
|
}
|
|
|
|
}
|
2018-05-21 12:30:30 +04:00
|
|
|
0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn sys_open(path: *const u8, flags: usize) -> i32 {
|
2018-11-07 20:21:20 +04:00
|
|
|
// TODO: check ptr
|
2018-05-21 12:30:30 +04:00
|
|
|
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));
|
2018-11-07 20:21:20 +04:00
|
|
|
files.insert(fd, Arc::new(Mutex::new(file)));
|
2018-11-07 09:34:31 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-11-07 19:32:22 +04:00
|
|
|
fn sys_fstat(fd: usize, stat_ptr: *mut Stat) -> i32 {
|
2018-11-07 20:21:20 +04:00
|
|
|
// TODO: check ptr
|
|
|
|
info!("fstat: {}", fd);
|
|
|
|
let file = process().files.get(&fd);
|
2018-11-07 19:32:22 +04:00
|
|
|
if file.is_none() {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
let file = file.unwrap();
|
2018-11-07 20:21:20 +04:00
|
|
|
let stat = Stat::from(file.lock().info().unwrap());
|
2018-11-07 19:32:22 +04:00
|
|
|
unsafe { stat_ptr.write(stat); }
|
|
|
|
0
|
|
|
|
}
|
|
|
|
|
2018-11-08 12:56:01 +04:00
|
|
|
/// entry_id = dentry.offset / 256
|
|
|
|
/// dentry.name = entry_name
|
|
|
|
/// dentry.offset += 256
|
|
|
|
fn sys_getdirentry(fd: usize, dentry_ptr: *mut DirEntry) -> i32 {
|
|
|
|
// TODO: check ptr
|
|
|
|
info!("getdirentry: {}", fd);
|
|
|
|
let file = process().files.get(&fd);
|
|
|
|
if file.is_none() { return -1; }
|
|
|
|
let file = file.unwrap();
|
|
|
|
let dentry = unsafe { &mut *dentry_ptr };
|
|
|
|
if !dentry.check() { return -1; }
|
|
|
|
let info = file.lock().info().unwrap();
|
|
|
|
if info.type_ != FileType::Dir || info.size <= dentry.entry_id() { return -1; }
|
|
|
|
let name = file.lock().get_entry(dentry.entry_id());
|
|
|
|
if name.is_err() { return -1; }
|
|
|
|
let name = name.unwrap();
|
|
|
|
dentry.set_name(name.as_str());
|
|
|
|
0
|
|
|
|
}
|
|
|
|
|
2018-11-07 20:21:20 +04:00
|
|
|
fn sys_dup(fd1: usize, fd2: usize) -> i32 {
|
|
|
|
info!("dup: {} {}", fd1, fd2);
|
|
|
|
let file = process().files.get(&fd1);
|
|
|
|
if file.is_none() {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
let file = file.unwrap();
|
|
|
|
if process().files.contains_key(&fd2) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
process().files.insert(fd2, file.clone());
|
|
|
|
0
|
|
|
|
}
|
|
|
|
|
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-11-07 20:21:20 +04:00
|
|
|
// TODO: check ptr
|
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-11-07 20:21:20 +04:00
|
|
|
fn sys_exit(exit_code: i32) -> i32 {
|
2018-10-24 17:29:31 +04:00
|
|
|
let pid = thread::current().id();
|
2018-11-07 20:21:20 +04:00
|
|
|
info!("exit: {}, code: {}", pid, exit_code);
|
|
|
|
processor().manager().exit(pid, exit_code as usize);
|
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)
|
|
|
|
}
|
|
|
|
}
|
2018-11-07 19:32:22 +04:00
|
|
|
|
2018-11-08 12:56:01 +04:00
|
|
|
#[repr(C)]
|
|
|
|
struct DirEntry {
|
|
|
|
offset: u32,
|
|
|
|
name: [u8; 256],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DirEntry {
|
|
|
|
fn check(&self) -> bool {
|
|
|
|
self.offset % 256 == 0
|
|
|
|
}
|
|
|
|
fn entry_id(&self) -> usize {
|
|
|
|
(self.offset / 256) as usize
|
|
|
|
}
|
|
|
|
fn set_name(&mut self, name: &str) {
|
|
|
|
self.name[..name.len()].copy_from_slice(name.as_bytes());
|
|
|
|
self.name[name.len()] = 0;
|
|
|
|
self.offset += 256;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 19:32:22 +04:00
|
|
|
#[repr(C)]
|
|
|
|
struct Stat {
|
|
|
|
/// protection mode and file type
|
|
|
|
mode: StatMode,
|
|
|
|
/// number of hard links
|
|
|
|
nlinks: u32,
|
|
|
|
/// number of blocks file is using
|
|
|
|
blocks: u32,
|
|
|
|
/// file size (bytes)
|
|
|
|
size: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
bitflags! {
|
|
|
|
struct StatMode: u32 {
|
|
|
|
const NULL = 0;
|
|
|
|
/// ordinary regular file
|
|
|
|
const FILE = 0o10000;
|
|
|
|
/// directory
|
|
|
|
const DIR = 0o20000;
|
|
|
|
/// symbolic link
|
|
|
|
const LINK = 0o30000;
|
|
|
|
/// character device
|
|
|
|
const CHAR = 0o40000;
|
|
|
|
/// block device
|
|
|
|
const BLOCK = 0o50000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<FileInfo> for Stat {
|
|
|
|
fn from(info: FileInfo) -> Self {
|
|
|
|
Stat {
|
|
|
|
mode: match info.type_ {
|
|
|
|
FileType::File => StatMode::FILE,
|
|
|
|
FileType::Dir => StatMode::DIR,
|
|
|
|
_ => StatMode::NULL,
|
|
|
|
},
|
|
|
|
nlinks: info.nlinks as u32,
|
|
|
|
blocks: info.blocks as u32,
|
|
|
|
size: info.size as u32,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|