use enum instead of consts for syscall id in user lib

master
Ben Pig Chu 6 years ago
parent 70e0025c63
commit ac71a45986

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

Loading…
Cancel
Save