|
|
|
@ -44,7 +44,8 @@ impl fmt::Write for SysPutc {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn sys_call(id: usize, arg0: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize, arg5: usize) -> i32 {
|
|
|
|
|
fn sys_call(syscall_id: SyscallId, arg0: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize, arg5: usize) -> i32 {
|
|
|
|
|
let id = syscall_id as usize;
|
|
|
|
|
let ret: i32;
|
|
|
|
|
unsafe {
|
|
|
|
|
#[cfg(target_arch = "riscv32")]
|
|
|
|
@ -70,12 +71,12 @@ fn sys_call(id: usize, arg0: usize, arg1: usize, arg2: usize, arg3: usize, arg4:
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn sys_exit(code: usize) -> ! {
|
|
|
|
|
sys_call(SYS_EXIT, code, 0, 0, 0, 0, 0);
|
|
|
|
|
sys_call(SyscallId::Exit, code, 0, 0, 0, 0, 0);
|
|
|
|
|
unreachable!()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn sys_write(fd: usize, base: *const u8, len: usize) -> i32 {
|
|
|
|
|
sys_call(SYS_WRITE, fd, base as usize, len, 0, 0, 0)
|
|
|
|
|
sys_call(SyscallId::Write, fd, base as usize, len, 0, 0, 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn sys_open(path: &str, flags: usize) -> i32 {
|
|
|
|
@ -83,86 +84,89 @@ pub fn sys_open(path: &str, flags: usize) -> i32 {
|
|
|
|
|
use core::mem::replace;
|
|
|
|
|
let end = unsafe { &mut *(path.as_ptr().offset(path.len() as isize) as *mut u8) };
|
|
|
|
|
let backup = replace(end, 0);
|
|
|
|
|
let ret = sys_call(SYS_OPEN, path.as_ptr() as usize, flags, 0, 0, 0, 0);
|
|
|
|
|
let ret = sys_call(SyscallId::Open, path.as_ptr() as usize, flags, 0, 0, 0, 0);
|
|
|
|
|
*end = backup;
|
|
|
|
|
ret
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn sys_close(fd: usize) -> i32 {
|
|
|
|
|
sys_call(SYS_CLOSE, fd, 0, 0, 0, 0, 0)
|
|
|
|
|
sys_call(SyscallId::Close, fd, 0, 0, 0, 0, 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn sys_dup(fd1: usize, fd2: usize) -> i32 {
|
|
|
|
|
sys_call(SYS_DUP, fd1, fd2, 0, 0, 0, 0)
|
|
|
|
|
sys_call(SyscallId::Dup, fd1, fd2, 0, 0, 0, 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Fork the current process. Return the child's PID.
|
|
|
|
|
pub fn sys_fork() -> i32 {
|
|
|
|
|
sys_call(SYS_FORK, 0, 0, 0, 0, 0, 0)
|
|
|
|
|
sys_call(SyscallId::Fork, 0, 0, 0, 0, 0, 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Wait the process exit.
|
|
|
|
|
/// Return the PID. Store exit code to `code` if it's not null.
|
|
|
|
|
pub fn sys_wait(pid: usize, code: *mut i32) -> i32 {
|
|
|
|
|
sys_call(SYS_WAIT, pid, code as usize, 0, 0, 0, 0)
|
|
|
|
|
sys_call(SyscallId::Wait, pid, code as usize, 0, 0, 0, 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn sys_yield() -> i32 {
|
|
|
|
|
sys_call(SYS_YIELD, 0, 0, 0, 0, 0, 0)
|
|
|
|
|
sys_call(SyscallId::Yield, 0, 0, 0, 0, 0, 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Kill the process
|
|
|
|
|
pub fn sys_kill(pid: usize) -> i32 {
|
|
|
|
|
sys_call(SYS_KILL, pid, 0, 0, 0, 0, 0)
|
|
|
|
|
sys_call(SyscallId::Kill, pid, 0, 0, 0, 0, 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get the current process id
|
|
|
|
|
pub fn sys_getpid() -> i32 {
|
|
|
|
|
sys_call(SYS_GETPID, 0, 0, 0, 0, 0, 0)
|
|
|
|
|
sys_call(SyscallId::GetPid, 0, 0, 0, 0, 0, 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn sys_sleep(time: usize) -> i32 {
|
|
|
|
|
sys_call(SYS_SLEEP, time, 0, 0, 0, 0, 0)
|
|
|
|
|
sys_call(SyscallId::Sleep, time, 0, 0, 0, 0, 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn sys_get_time() -> i32 {
|
|
|
|
|
sys_call(SYS_GETTIME, 0, 0, 0, 0, 0, 0)
|
|
|
|
|
sys_call(SyscallId::GetTime, 0, 0, 0, 0, 0, 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn sys_lab6_set_priority(priority: usize) -> i32 {
|
|
|
|
|
sys_call(SYS_LAB6_SET_PRIORITY, priority, 0, 0, 0, 0, 0)
|
|
|
|
|
sys_call(SyscallId::Lab6SetPriority, priority, 0, 0, 0, 0, 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn sys_putc(c: char) -> i32 {
|
|
|
|
|
sys_call(SYS_PUTC, c as usize, 0, 0, 0, 0, 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const SYS_EXIT: usize = 1;
|
|
|
|
|
const SYS_FORK: usize = 2;
|
|
|
|
|
const SYS_WAIT: usize = 3;
|
|
|
|
|
const SYS_EXEC: usize = 4;
|
|
|
|
|
const SYS_CLONE: usize = 5;
|
|
|
|
|
const SYS_YIELD: usize = 10;
|
|
|
|
|
const SYS_SLEEP: usize = 11;
|
|
|
|
|
const SYS_KILL: usize = 12;
|
|
|
|
|
const SYS_GETTIME: usize = 17;
|
|
|
|
|
const SYS_GETPID: usize = 18;
|
|
|
|
|
const SYS_MMAP: usize = 20;
|
|
|
|
|
const SYS_MUNMAP: usize = 21;
|
|
|
|
|
const SYS_SHMEM: usize = 22;
|
|
|
|
|
const SYS_PUTC: usize = 30;
|
|
|
|
|
const SYS_PGDIR: usize = 31;
|
|
|
|
|
const SYS_OPEN: usize = 100;
|
|
|
|
|
const SYS_CLOSE: usize = 101;
|
|
|
|
|
const SYS_READ: usize = 102;
|
|
|
|
|
const SYS_WRITE: usize = 103;
|
|
|
|
|
const SYS_SEEK: usize = 104;
|
|
|
|
|
const SYS_FSTAT: usize = 110;
|
|
|
|
|
const SYS_FSYNC: usize = 111;
|
|
|
|
|
const SYS_GETCWD: usize = 121;
|
|
|
|
|
const SYS_GETDIRENTRY: usize = 128;
|
|
|
|
|
const SYS_DUP: usize = 130;
|
|
|
|
|
const SYS_LAB6_SET_PRIORITY: usize = 255;
|
|
|
|
|
sys_call(SyscallId::Putc, c as usize, 0, 0, 0, 0, 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
|
enum SyscallId{
|
|
|
|
|
Exit = 1,
|
|
|
|
|
Fork = 2,
|
|
|
|
|
Wait = 3,
|
|
|
|
|
Exec = 4,
|
|
|
|
|
Clone = 5,
|
|
|
|
|
Yield = 10,
|
|
|
|
|
Sleep = 11,
|
|
|
|
|
Kill = 12,
|
|
|
|
|
GetTime = 17,
|
|
|
|
|
GetPid = 18,
|
|
|
|
|
Mmap = 20,
|
|
|
|
|
Munmap = 21,
|
|
|
|
|
Shmem = 22,
|
|
|
|
|
Putc = 30,
|
|
|
|
|
Pgdir = 31,
|
|
|
|
|
Open = 100,
|
|
|
|
|
Close = 101,
|
|
|
|
|
Read = 102,
|
|
|
|
|
Write = 103,
|
|
|
|
|
Seek = 104,
|
|
|
|
|
Fstat = 110,
|
|
|
|
|
Fsync = 111,
|
|
|
|
|
GetCwd = 121,
|
|
|
|
|
GetDirEntry = 128,
|
|
|
|
|
Dup = 130,
|
|
|
|
|
Lab6SetPriority = 255,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* VFS flags */
|
|
|
|
|
// TODO: use bitflags
|
|
|
|
|