pub mod syscall_impl;
pub mod user_ptr;
use crate::{errno, serial_println};
#[repr(usize)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum SyscallId {
Mkdir = 34, Mount = 40, Umount = 39, Open = 56, Close = 57, Read = 63, Write = 64,
Unlink = 35, Lseek = 62, Getdents = 78, Stat = 1038, Dup2 = 1041, Pipe = 59,
Exit = 93, GetPid = 172, Fork = 220, Exec = 221, Wait = 260, Unknown = 9999,
}
impl From<usize> for SyscallId {
fn from(id: usize) -> Self {
match id {
34 => SyscallId::Mkdir,
39 => SyscallId::Umount,
40 => SyscallId::Mount,
56 => SyscallId::Open,
57 => SyscallId::Close,
63 => SyscallId::Read,
64 => SyscallId::Write,
35 => SyscallId::Unlink,
62 => SyscallId::Lseek,
78 => SyscallId::Getdents,
1038 => SyscallId::Stat,
1041 => SyscallId::Dup2,
59 => SyscallId::Pipe,
93 => SyscallId::Exit,
172 => SyscallId::GetPid,
220 => SyscallId::Fork,
221 => SyscallId::Exec,
260 => SyscallId::Wait,
_ => SyscallId::Unknown,
}
}
}
impl SyscallId {
pub const fn requires_live_trap_frame(self) -> bool {
match self {
SyscallId::Exit | SyscallId::Fork | SyscallId::Exec | SyscallId::Wait => true,
SyscallId::Mkdir
| SyscallId::Mount
| SyscallId::Umount
| SyscallId::Open
| SyscallId::Close
| SyscallId::Read
| SyscallId::Write
| SyscallId::Unlink
| SyscallId::Lseek
| SyscallId::Getdents
| SyscallId::Stat
| SyscallId::Dup2
| SyscallId::Pipe
| SyscallId::GetPid
| SyscallId::Unknown => false,
}
}
pub const fn process_name(self) -> Option<&'static str> {
match self {
SyscallId::Exit => Some("exit"),
SyscallId::Fork => Some("fork"),
SyscallId::Exec => Some("exec"),
SyscallId::Wait => Some("wait"),
_ => None,
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct SyscallContext {
pub syscall_id: usize,
pub arg0: usize,
pub arg1: usize,
pub arg2: usize,
pub arg3: usize,
pub arg4: usize,
pub arg5: usize,
pub sepc: usize,
}
impl SyscallContext {
pub fn from_trap_frame(tf: &crate::interrupts::TrapFrame) -> Self {
Self {
syscall_id: tf.x[17], arg0: tf.x[10], arg1: tf.x[11], arg2: tf.x[12], arg3: tf.x[13], arg4: tf.x[14], arg5: tf.x[15], sepc: tf.sepc,
}
}
pub unsafe fn from_registers() -> Self {
let syscall_id: usize;
let arg0: usize;
let arg1: usize;
let arg2: usize;
let arg3: usize;
let arg4: usize;
let arg5: usize;
core::arch::asm!(
"mv {0}, a7", "mv {1}, a0", "mv {2}, a1",
"mv {3}, a2",
"mv {4}, a3",
"mv {5}, a4",
"mv {6}, a5",
out(reg) syscall_id,
out(reg) arg0,
out(reg) arg1,
out(reg) arg2,
out(reg) arg3,
out(reg) arg4,
out(reg) arg5,
);
let sepc = riscv::register::sepc::read();
Self {
syscall_id,
arg0,
arg1,
arg2,
arg3,
arg4,
arg5,
sepc,
}
}
pub unsafe fn set_return_value(&self, ret: isize) {
core::arch::asm!(
"mv a0, {0}",
in(reg) ret,
);
}
}
pub fn syscall_dispatcher(context: &SyscallContext) -> isize {
let syscall_id = SyscallId::from(context.syscall_id);
#[cfg(feature = "verbose_syscall")]
print_syscall_entry(context, syscall_id);
let result = match syscall_id {
SyscallId::Read => syscall_impl::sys_read(context.arg0, context.arg1, context.arg2),
SyscallId::Write => {
syscall_impl::sys_write(context.arg0, context.arg1 as *const u8, context.arg2)
}
SyscallId::Open => syscall_impl::sys_open(context.arg0, context.arg1),
SyscallId::Close => syscall_impl::sys_close(context.arg0),
SyscallId::Mkdir => syscall_impl::sys_mkdir(context.arg0),
SyscallId::Mount => syscall_impl::sys_mount(context.arg0),
SyscallId::Umount => syscall_impl::sys_umount(context.arg0),
SyscallId::Lseek => {
syscall_impl::sys_lseek(context.arg0, context.arg1 as isize, context.arg2)
}
SyscallId::Unlink => syscall_impl::sys_unlink(context.arg0),
SyscallId::Stat => syscall_impl::sys_stat(context.arg0, context.arg1),
SyscallId::Getdents => syscall_impl::sys_getdents(context.arg0, context.arg1, context.arg2),
SyscallId::Dup2 => syscall_impl::sys_dup2(context.arg0, context.arg1),
SyscallId::Pipe => syscall_impl::sys_pipe(context.arg0),
SyscallId::Exit => syscall_impl::sys_exit(context.arg0 as i32),
SyscallId::GetPid => syscall_impl::sys_getpid(),
SyscallId::Fork => syscall_impl::sys_process_requires_trap_frame("fork"),
SyscallId::Exec => syscall_impl::sys_process_requires_trap_frame("exec"),
SyscallId::Wait => syscall_impl::sys_process_requires_trap_frame("wait"),
SyscallId::Unknown => {
serial_println!(
"[SYSCALL] Unknown syscall: {} (syscall_id={})",
context.syscall_id,
context.syscall_id
);
errno::ENOSYS
}
};
#[cfg(feature = "verbose_syscall")]
print_syscall_exit(syscall_id, result);
result
}
pub fn syscall_trap_dispatcher(tf: &mut crate::interrupts::TrapFrame) {
let syscall_id_raw = tf.x[17]; let sid = SyscallId::from(syscall_id_raw);
match sid {
SyscallId::Exit => {
let exit_code = tf.x[10] as i32; crate::process::exit_current_and_switch(tf, exit_code);
}
SyscallId::Fork => {
let child_pid = crate::process::fork_current(tf);
tf.x[10] = child_pid as usize;
tf.sepc = tf.sepc.wrapping_add(4);
}
SyscallId::Wait => {
let exit_code_ptr = tf.x[10]; let child_pid = crate::process::wait_current(tf, exit_code_ptr);
tf.x[10] = child_pid as usize;
tf.sepc = tf.sepc.wrapping_add(4);
}
SyscallId::Exec => {
let path_ptr = tf.x[10]; let path = syscall_impl::read_c_string(path_ptr);
let ret = match path {
Some(ref s) => crate::process::exec_current_into_trap_frame(tf, s),
None => -1isize,
};
if ret != 0 {
tf.x[10] = ret as usize;
tf.sepc = tf.sepc.wrapping_add(4);
}
}
_ => {
let context = SyscallContext::from_trap_frame(tf);
let result = syscall_dispatcher(&context);
tf.x[10] = result as usize;
tf.sepc = tf.sepc.wrapping_add(4);
}
}
}
#[allow(dead_code)]
fn print_syscall_entry(context: &SyscallContext, syscall_id: SyscallId) {
serial_println!("\n╔════════════════════════════════════════╗");
serial_println!("║ 系统调用追踪 ║");
serial_println!("╠════════════════════════════════════════╣");
serial_println!("║ 调用号: {:?} ({})", syscall_id, context.syscall_id);
serial_println!("║ PC: {:#x}", context.sepc);
serial_println!("╠════════════════════════════════════════╣");
serial_println!("║ 参数: ║");
serial_println!("║ a0 (arg0) = {:#x}", context.arg0);
serial_println!("║ a1 (arg1) = {:#x}", context.arg1);
serial_println!("║ a2 (arg2) = {:#x}", context.arg2);
serial_println!("╠════════════════════════════════════════╣");
}
#[allow(dead_code)]
fn print_syscall_exit(syscall_id: SyscallId, result: isize) {
serial_println!("║ 调用号: {:?}", syscall_id);
serial_println!("║ 返回值: {} ({:#x})", result, result as usize);
serial_println!("╚════════════════════════════════════════╝\n");
}
pub fn test_syscall(syscall_id: usize, arg0: usize, arg1: usize, arg2: usize) -> isize {
let context = SyscallContext {
syscall_id,
arg0,
arg1,
arg2,
arg3: 0,
arg4: 0,
arg5: 0,
sepc: 0,
};
syscall_dispatcher(&context)
}