From fc02e9f961d9b786449244bd8020c89d569af8a9 Mon Sep 17 00:00:00 2001 From: zhangxinyu <840317537@qq.com> Date: Wed, 17 May 2023 13:10:56 +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=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ch2/user/src/lib.rs | 1 + ch2/user/src/syscall.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 ch2/user/src/syscall.rs diff --git a/ch2/user/src/lib.rs b/ch2/user/src/lib.rs index 081d219..e859d12 100644 --- a/ch2/user/src/lib.rs +++ b/ch2/user/src/lib.rs @@ -2,6 +2,7 @@ #![feature(linkage)] // 开启弱链接特性 #![feature(panic_info_message)] +pub mod syscall; pub mod user_lang_items; pub use user_lang_items::*; diff --git a/ch2/user/src/syscall.rs b/ch2/user/src/syscall.rs new file mode 100644 index 0000000..1604d1b --- /dev/null +++ b/ch2/user/src/syscall.rs @@ -0,0 +1,30 @@ +use core::arch::asm; + +const SYSCALL_WRITE: usize = 64; +const SYSCALL_EXIT: usize = 93; + + +fn syscall(id: usize, args: [usize; 3]) -> isize { + let mut ret: isize; + unsafe { + // a0寄存器同时作为输入参数和输出参数, {in_var} => {out_var} + asm!( + "ecall", + inlateout("x10") args[0] => ret, + in("x11") args[1], + in("x12") args[2], + in("x17") id + ); + } + ret +} + + +pub fn sys_write(fd: usize, buffer: &[u8]) -> isize { + syscall(SYSCALL_WRITE, [fd, buffer.as_ptr() as usize, buffer.len()]) +} + +pub fn sys_exit(exit_code: i32) -> isize { + syscall(SYSCALL_EXIT, [exit_code as usize, 0, 0]) +} +