From 69bc5caa81fc6cce7c13c4ea33040b8b3b70ea69 Mon Sep 17 00:00:00 2001 From: WangRunji Date: Sat, 14 Jul 2018 18:42:58 +0800 Subject: [PATCH] Fix syscall return value, args. Pass `hello`. --- kernel/src/arch/riscv32/interrupt.rs | 5 +- kernel/src/arch/x86_64/interrupt/handler.rs | 8 +-- kernel/src/consts.rs | 3 +- kernel/src/syscall.rs | 2 +- user/ucore-ulib/src/syscall.rs | 63 +++++++++++++++++++-- 5 files changed, 68 insertions(+), 13 deletions(-) diff --git a/kernel/src/arch/riscv32/interrupt.rs b/kernel/src/arch/riscv32/interrupt.rs index 63abf48..185d065 100644 --- a/kernel/src/arch/riscv32/interrupt.rs +++ b/kernel/src/arch/riscv32/interrupt.rs @@ -50,8 +50,9 @@ fn timer() { } fn syscall(tf: &mut TrapFrame) { - let ret = ::syscall::syscall(tf.x[17], [tf.x[10], tf.x[11], tf.x[12], tf.x[13], tf.x[14], tf.x[15]], tf); - tf.x[10] = ret as usize; + let ret = ::syscall::syscall(tf.x[10], [tf.x[11], tf.x[12], tf.x[13], tf.x[14], tf.x[15], tf.x[16]], tf); + unsafe { *(&tf.x[10] as *const _ as *mut i32) = ret; } + tf.sepc += 4; } extern { diff --git a/kernel/src/arch/x86_64/interrupt/handler.rs b/kernel/src/arch/x86_64/interrupt/handler.rs index 894f5ee..f845f94 100644 --- a/kernel/src/arch/x86_64/interrupt/handler.rs +++ b/kernel/src/arch/x86_64/interrupt/handler.rs @@ -157,17 +157,17 @@ fn to_kernel(tf: &mut TrapFrame) { } fn syscall(tf: &mut TrapFrame) { - info!("\nInterupt: Syscall {:#x?}", tf.rax); + trace!("\nInterupt: Syscall {:#x?}", tf.rax); use syscall::syscall; let ret = syscall(tf.rax, [tf.rdi, tf.rsi, tf.rdx, tf.rcx, tf.r8, tf.r9], tf); - tf.rax = ret as usize; + unsafe { *(&tf.rax as *const _ as *mut i32) = ret; } } fn syscall32(tf: &mut TrapFrame) { - // info!("\nInterupt: Syscall {:#x?}", tf.rax); + trace!("\nInterupt: Syscall {:#x?}", tf.rax); use syscall::syscall; let ret = syscall(tf.rax, [tf.rdx, tf.rcx, tf.rbx, tf.rdi, tf.rsi, 0], tf); - tf.rax = ret as usize; + unsafe { *(&tf.rax as *const _ as *mut i32) = ret; } } fn error(tf: &TrapFrame) { diff --git a/kernel/src/consts.rs b/kernel/src/consts.rs index 602c760..b4a3661 100644 --- a/kernel/src/consts.rs +++ b/kernel/src/consts.rs @@ -22,8 +22,7 @@ mod riscv { pub const MEMORY_OFFSET: usize = 0x8000_0000; pub const MEMORY_END: usize = 0x8080_0000; pub const USER_STACK_OFFSET: usize = 0x70000000; - pub const USER_STACK_SIZE: usize = 1024 * 1024; - // 1 MB + pub const USER_STACK_SIZE: usize = 0x10000; pub const USER32_STACK_OFFSET: usize = USER_STACK_OFFSET; } diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index e3fbf19..8bac72e 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -26,7 +26,7 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &TrapFrame) -> i32 { SYS_LAB6_SET_PRIORITY => sys_lab6_set_priority(args[0]), SYS_PUTC => sys_putc(args[0] as u8 as char), _ => { - error!("unknown syscall {:#x?}", id); + error!("unknown syscall id: {:#x?}, args: {:x?}", id, args); -1 } } diff --git a/user/ucore-ulib/src/syscall.rs b/user/ucore-ulib/src/syscall.rs index e537e4f..6e9acff 100644 --- a/user/ucore-ulib/src/syscall.rs +++ b/user/ucore-ulib/src/syscall.rs @@ -29,13 +29,13 @@ impl fmt::Write for StdOut { } #[inline(always)] -fn sys_call(id: usize, arg0: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize, arg5: usize) -> isize { - let ret: isize; +fn sys_call(id: usize, arg0: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize, arg5: usize) -> i32 { + let ret: i32; unsafe { #[cfg(target_arch = "riscv")] asm!("ecall" : "={x10}" (ret) - : "{x17}" (id), "{x10}" (arg0), "{x11}" (arg1), "{x12}" (arg2), "{x13}" (arg3), "{x14}" (arg4), "{x15}" (arg5) + : "{x10}" (id), "{x11}" (arg0), "{x12}" (arg1), "{x13}" (arg2), "{x14}" (arg3), "{x15}" (arg4), "{x16}" (arg5) : "memory" : "volatile"); #[cfg(target_arch = "x86_64")] @@ -53,10 +53,65 @@ pub fn sys_exit(code: usize) -> ! { unreachable!() } -pub fn sys_write(fd: usize, base: *const u8, len: usize) -> isize { +pub fn sys_write(fd: usize, base: *const u8, len: usize) -> i32 { sys_call(SYS_WRITE, fd, base as usize, len, 0, 0, 0) } +pub fn sys_open(path: &str, flags: usize) -> i32 { + // UNSAFE: append '\0' to the string + 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); + *end = backup; + ret +} + +pub fn sys_close(fd: usize) -> i32 { + sys_call(SYS_CLOSE, fd, 0 , 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) +} + +/// 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) +} + +pub fn sys_yield() -> i32 { + sys_call(SYS_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) +} + +/// Get the current process id +pub fn sys_getpid() -> i32 { + sys_call(SYS_GETPID, 0, 0, 0, 0, 0, 0) +} + +pub fn sys_sleep(time: usize) -> i32 { + sys_call(SYS_SLEEP, time, 0, 0, 0, 0, 0) +} + +pub fn sys_get_time() -> i32 { + sys_call(SYS_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) +} + +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;