From 00e64296a84d7f726e5473c8a631b1cb4352de1a Mon Sep 17 00:00:00 2001 From: zhangxinyu <840317537@qq.com> Date: Fri, 19 May 2023 14:08:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=B3=BB=E7=BB=9F=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E5=88=86=E5=8F=91=E4=BB=A5=E5=8F=8A=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ch2/os/src/syscall/fs.rs | 20 ++++++++++++++++++++ ch2/os/src/syscall/mod.rs | 17 +++++++++++++++++ ch2/os/src/syscall/process.rs | 9 +++++++++ 3 files changed, 46 insertions(+) create mode 100644 ch2/os/src/syscall/fs.rs create mode 100644 ch2/os/src/syscall/mod.rs create mode 100644 ch2/os/src/syscall/process.rs diff --git a/ch2/os/src/syscall/fs.rs b/ch2/os/src/syscall/fs.rs new file mode 100644 index 0000000..eeb9aeb --- /dev/null +++ b/ch2/os/src/syscall/fs.rs @@ -0,0 +1,20 @@ +//! File and filesystem-related syscalls + +use crate::print; + +const FD_STDOUT: usize = 1; + +/// write buf of length `len` to a file with `fd` +pub fn sys_write(fd: usize, buf: *const u8, len: usize) -> isize { + match fd { + FD_STDOUT => { + let slice = unsafe { core::slice::from_raw_parts(buf, len) }; + let str = core::str::from_utf8(slice).unwrap(); + print!("{}", str); + len as isize + } + _ => { + panic!("Unsupported fd in sys_write!"); + } + } +} diff --git a/ch2/os/src/syscall/mod.rs b/ch2/os/src/syscall/mod.rs new file mode 100644 index 0000000..0f9f63c --- /dev/null +++ b/ch2/os/src/syscall/mod.rs @@ -0,0 +1,17 @@ +const SYSCALL_WRITE: usize = 64; +const SYSCALL_EXIT: usize = 93; + +mod fs; +mod process; + +use fs::*; +use process::*; + +/// 根据syscall_id 进行分发 +pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize { + match syscall_id { + SYSCALL_WRITE => sys_write(args[0], args[1] as *const u8, args[2]), + SYSCALL_EXIT => sys_exit(args[0] as i32), + _ => panic!("Unsupported syscall_id: {}", syscall_id), + } +} \ No newline at end of file diff --git a/ch2/os/src/syscall/process.rs b/ch2/os/src/syscall/process.rs new file mode 100644 index 0000000..6dbed6d --- /dev/null +++ b/ch2/os/src/syscall/process.rs @@ -0,0 +1,9 @@ +//! App management syscalls +use crate::batch::run_next_app; +use crate::println; + +/// 任务退出, 并立即切换任务 +pub fn sys_exit(exit_code: i32) -> ! { + println!("[kernel] Application exited with code {}", exit_code); + run_next_app() +}