From 335027f00bc2d6fad1d11aa601b248e9c269f94a Mon Sep 17 00:00:00 2001 From: Yifan Wu Date: Tue, 5 Jan 2021 17:37:20 +0800 Subject: [PATCH] Wrap syscalls in user_lib & change xstate to exit_code. --- os/src/syscall/process.rs | 2 +- user/src/console.rs | 6 ++++-- user/src/lib.rs | 9 +++++++-- user/src/syscall.rs | 6 ++---- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/os/src/syscall/process.rs b/os/src/syscall/process.rs index ef56607a..2633cf69 100644 --- a/os/src/syscall/process.rs +++ b/os/src/syscall/process.rs @@ -1,6 +1,6 @@ use crate::batch::run_next_app; -pub fn sys_exit(xstate: i32) -> ! { +pub fn sys_exit(exit_code: i32) -> ! { println!("[kernel] Application exited with code {}", xstate); run_next_app() } \ No newline at end of file diff --git a/user/src/console.rs b/user/src/console.rs index a826b8fe..ac801174 100644 --- a/user/src/console.rs +++ b/user/src/console.rs @@ -1,11 +1,13 @@ use core::fmt::{self, Write}; -use crate::syscall::{STDOUT, sys_write}; +use super::write; struct Stdout; +const STDOUT: usize = 1; + impl Write for Stdout { fn write_str(&mut self, s: &str) -> fmt::Result { - sys_write(STDOUT, s.as_bytes()); + write(STDOUT, s.as_bytes()); Ok(()) } } diff --git a/user/src/lib.rs b/user/src/lib.rs index ed7ba84c..bc68c734 100644 --- a/user/src/lib.rs +++ b/user/src/lib.rs @@ -12,7 +12,7 @@ mod lang_items; #[link_section = ".text.entry"] pub extern "C" fn _start() -> ! { clear_bss(); - syscall::sys_exit(main()); + exit(main()); panic!("unreachable after sys_exit!"); } @@ -30,4 +30,9 @@ fn clear_bss() { (start_bss as usize..end_bss as usize).for_each(|addr| { unsafe { (addr as *mut u8).write_volatile(0); } }); -} \ No newline at end of file +} + +use syscall::*; + +pub fn write(fd: usize, buf: &[u8]) -> isize { sys_write(fd, buf) } +pub fn exit(exit_code: i32) -> isize { sys_exit(exit_code) } \ No newline at end of file diff --git a/user/src/syscall.rs b/user/src/syscall.rs index edb26d77..b10cacf7 100644 --- a/user/src/syscall.rs +++ b/user/src/syscall.rs @@ -1,5 +1,3 @@ -pub const STDOUT: usize = 1; - const SYSCALL_WRITE: usize = 64; const SYSCALL_EXIT: usize = 93; @@ -20,6 +18,6 @@ pub fn sys_write(fd: usize, buffer: &[u8]) -> isize { syscall(SYSCALL_WRITE, [fd, buffer.as_ptr() as usize, buffer.len()]) } -pub fn sys_exit(xstate: i32) -> isize { - syscall(SYSCALL_EXIT, [xstate as usize, 0, 0]) +pub fn sys_exit(exit_code: i32) -> isize { + syscall(SYSCALL_EXIT, [exit_code as usize, 0, 0]) }