1
0
mirror of https://github.com/rcore-os/rCore.git synced 2024-11-27 02:03:29 +04:00
rCore/kernel/src/syscall.rs

354 lines
9.4 KiB
Rust
Raw Normal View History

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};
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-11-08 13:32:44 +04:00
let ret = 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),
130 => sys_dup(args[0], args[1]),
2018-11-07 09:34:31 +04:00
// process
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
_ => {
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-11-08 13:32:44 +04:00
};
match ret {
Ok(code) => code,
Err(_) => -1,
2018-05-12 23:41:41 +04:00
}
}
2018-11-08 13:32:44 +04:00
fn sys_read(fd: usize, base: *mut u8, len: usize) -> SysResult {
// 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!(),
2018-11-08 13:32:44 +04:00
1 | 2 => return Err(SysError::InvalidFile),
_ => { get_file(fd)?.lock().read(slice)?; }
2018-11-07 09:34:31 +04:00
}
2018-11-08 13:32:44 +04:00
Ok(0)
2018-11-07 09:34:31 +04:00
}
2018-11-08 13:32:44 +04:00
fn sys_write(fd: usize, base: *const u8, len: usize) -> SysResult {
// 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 {
2018-11-08 13:32:44 +04:00
0 => return Err(SysError::InvalidFile),
1 | 2 => {
let s = str::from_utf8(slice).map_err(|_| SysError::InvalidArgument)?;
print!("{}", s);
},
_ => { get_file(fd)?.lock().write(slice)?; }
2018-11-07 09:34:31 +04:00
}
2018-11-08 13:32:44 +04:00
Ok(0)
2018-05-21 12:30:30 +04:00
}
2018-11-08 13:32:44 +04:00
fn sys_open(path: *const u8, flags: usize) -> SysResult {
// 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-08 13:32:44 +04:00
"stdin:" => return Ok(0),
"stdout:" => return Ok(1),
"stderr:" => return Ok(2),
2018-11-07 09:34:31 +04:00
_ => {}
}
2018-11-08 13:32:44 +04:00
let inode = ::fs::ROOT_INODE.lookup(path)?;
2018-11-07 09:34:31 +04:00
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, Arc::new(Mutex::new(file)));
2018-11-08 13:32:44 +04:00
Ok(fd as i32)
2018-05-21 12:30:30 +04:00
}
2018-11-08 13:32:44 +04:00
fn sys_close(fd: usize) -> SysResult {
2018-05-21 12:30:30 +04:00
info!("close: fd: {:?}", fd);
2018-11-07 09:34:31 +04:00
if fd < 3 {
2018-11-08 13:32:44 +04:00
return Ok(0);
2018-11-07 09:34:31 +04:00
}
match process().files.remove(&fd) {
2018-11-08 13:32:44 +04:00
Some(_) => Ok(0),
None => Err(SysError::InvalidFile),
2018-11-07 09:34:31 +04:00
}
2018-05-21 12:30:30 +04:00
}
2018-11-08 13:32:44 +04:00
fn sys_fstat(fd: usize, stat_ptr: *mut Stat) -> SysResult {
// TODO: check ptr
info!("fstat: {}", fd);
2018-11-08 13:32:44 +04:00
let file = get_file(fd)?;
let stat = Stat::from(file.lock().info()?);
2018-11-07 19:32:22 +04:00
unsafe { stat_ptr.write(stat); }
2018-11-08 13:32:44 +04:00
Ok(0)
2018-11-07 19:32:22 +04:00
}
2018-11-08 12:56:01 +04:00
/// entry_id = dentry.offset / 256
/// dentry.name = entry_name
/// dentry.offset += 256
2018-11-08 13:32:44 +04:00
fn sys_getdirentry(fd: usize, dentry_ptr: *mut DirEntry) -> SysResult {
2018-11-08 12:56:01 +04:00
// TODO: check ptr
info!("getdirentry: {}", fd);
2018-11-08 13:32:44 +04:00
let file = get_file(fd)?;
2018-11-08 12:56:01 +04:00
let dentry = unsafe { &mut *dentry_ptr };
2018-11-08 13:32:44 +04:00
if !dentry.check() {
return Err(SysError::InvalidArgument);
}
let info = file.lock().info()?;
if info.type_ != FileType::Dir || info.size <= dentry.entry_id() {
return Err(SysError::InvalidArgument);
}
let name = file.lock().get_entry(dentry.entry_id())?;
2018-11-08 12:56:01 +04:00
dentry.set_name(name.as_str());
2018-11-08 13:32:44 +04:00
Ok(0)
2018-11-08 12:56:01 +04:00
}
2018-11-08 13:32:44 +04:00
fn sys_dup(fd1: usize, fd2: usize) -> SysResult {
info!("dup: {} {}", fd1, fd2);
2018-11-08 13:32:44 +04:00
let file = get_file(fd1)?;
if process().files.contains_key(&fd2) {
2018-11-08 13:32:44 +04:00
return Err(SysError::InvalidFile);
}
process().files.insert(fd2, file.clone());
2018-11-08 13:32:44 +04:00
Ok(0)
}
2018-05-21 12:30:30 +04:00
/// Fork the current process. Return the child's PID.
2018-11-08 13:32:44 +04:00
fn sys_fork(tf: &TrapFrame) -> SysResult {
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);
2018-11-08 13:32:44 +04:00
Ok(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-11-08 13:32:44 +04:00
fn sys_wait(pid: usize, code: *mut i32) -> SysResult {
// 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() {
2018-11-08 13:32:44 +04:00
return Ok(-1);
2018-10-31 20:45:02 +04:00
}
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);
2018-11-08 13:32:44 +04:00
return Ok(0);
2018-10-25 20:49:19 +04:00
}
2018-11-08 13:32:44 +04:00
None => return Ok(-1),
2018-10-31 20:45:02 +04:00
_ => {}
2018-10-25 20:49: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-11-08 13:32:44 +04:00
fn sys_yield() -> SysResult {
2018-05-31 16:26:25 +04:00
thread::yield_now();
2018-11-08 13:32:44 +04:00
Ok(0)
2018-05-21 12:30:30 +04:00
}
/// Kill the process
2018-11-08 13:32:44 +04:00
fn sys_kill(pid: usize) -> SysResult {
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-11-08 13:32:44 +04:00
Ok(0)
2018-05-21 12:30:30 +04:00
}
/// Get the current process id
2018-11-08 13:32:44 +04:00
fn sys_getpid() -> SysResult {
Ok(thread::current().id() as i32)
2018-05-21 12:30:30 +04:00
}
/// Exit the current process
2018-11-08 13:32:44 +04:00
fn sys_exit(exit_code: i32) -> SysResult {
2018-10-24 17:29:31 +04:00
let pid = thread::current().id();
info!("exit: {}, code: {}", pid, exit_code);
processor().manager().exit(pid, exit_code as usize);
processor().yield_now();
2018-10-25 20:49:19 +04:00
unreachable!();
2018-05-21 12:30:30 +04:00
}
2018-11-08 13:32:44 +04:00
fn sys_sleep(time: usize) -> SysResult {
use core::time::Duration;
thread::sleep(Duration::from_millis(time as u64 * 10));
2018-11-08 13:32:44 +04:00
Ok(0)
2018-05-21 12:30:30 +04:00
}
2018-11-08 13:32:44 +04:00
fn sys_get_time() -> SysResult {
unsafe { Ok(::trap::TICK as i32) }
2018-05-21 12:30:30 +04:00
}
2018-11-08 13:32:44 +04:00
fn sys_lab6_set_priority(priority: usize) -> SysResult {
2018-10-24 17:29:31 +04:00
let pid = thread::current().id();
processor().manager().set_priority(pid, priority as u8);
2018-11-08 13:32:44 +04:00
Ok(0)
}
2018-11-08 13:32:44 +04:00
fn sys_putc(c: char) -> SysResult {
2018-07-14 08:28:55 +04:00
print!("{}", c);
2018-11-08 13:32:44 +04:00
Ok(0)
}
fn get_file(fd: usize) -> Result<&'static Arc<Mutex<File>>, SysError> {
process().files.get(&fd).ok_or(SysError::InvalidFile)
}
pub type SysResult = Result<i32, SysError>;
#[repr(i32)]
pub enum SysError {
VfsError,
InvalidFile,
InvalidArgument,
}
impl From<()> for SysError {
fn from(_: ()) -> Self {
SysError::VfsError
}
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,
}
}
}