refactor(syscall):将syscall trait 中的handle参数from_user修改为TrapFrame (#1188)

* refactor(syscall):将syscall trait 中的handle参数from_user修改为frame,

* refactor: 将x86_64::interrupt::TrapFrame替换为arch::interrupt::TrapFrame

Signed-off-by: sparkzky <sparkhhhhhhhhhh@outlook.com>

---------

Signed-off-by: sparkzky <sparkhhhhhhhhhh@outlook.com>
Co-authored-by: sparkzky <sparkhhhhhhhhhh@outlook.com>
This commit is contained in:
DoL 2025-06-04 10:18:03 +08:00 committed by GitHub
parent 5db1f9ad54
commit 9f9b37c492
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
28 changed files with 61 additions and 53 deletions

View File

@ -2,13 +2,13 @@
use alloc::string::ToString;
use crate::arch::interrupt::TrapFrame;
use crate::arch::syscall::nr::SYS_CLOSE;
use crate::process::ProcessManager;
use crate::syscall::table::FormattedSyscallParam;
use crate::syscall::table::Syscall;
use alloc::vec::Vec;
use system_error::SystemError;
/// Handler for the `close` system call.
pub struct SysCloseHandle;
@ -19,7 +19,7 @@ impl Syscall for SysCloseHandle {
}
/// Handles the close syscall by extracting arguments and calling `do_close`.
fn handle(&self, args: &[usize], _from_user: bool) -> Result<usize, SystemError> {
fn handle(&self, args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
let fd = Self::fd(args);
do_close(fd)
}

View File

@ -1,5 +1,6 @@
//! System call handler for epoll creation.
use crate::arch::interrupt::TrapFrame;
use crate::arch::syscall::nr::SYS_EPOLL_CREATE;
use crate::filesystem::epoll::event_poll::EventPoll;
use crate::filesystem::vfs::file::FileMode;
@ -15,7 +16,7 @@ impl Syscall for SysEpollCreateHandle {
1
}
fn handle(&self, args: &[usize], _from_user: bool) -> Result<usize, SystemError> {
fn handle(&self, args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
let max_size = Self::max_size(args);
if max_size < 0 {
return Err(SystemError::EINVAL);

View File

@ -1,5 +1,6 @@
//! System call handler for epoll_create1.
use crate::arch::interrupt::TrapFrame;
use crate::arch::syscall::nr::SYS_EPOLL_CREATE1;
use crate::filesystem::epoll::event_poll::EventPoll;
use crate::filesystem::vfs::file::FileMode;
@ -7,7 +8,6 @@ use crate::syscall::table::FormattedSyscallParam;
use crate::syscall::table::Syscall;
use alloc::vec::Vec;
use system_error::SystemError;
pub struct SysEpollCreate1Handle;
impl Syscall for SysEpollCreate1Handle {
@ -15,7 +15,7 @@ impl Syscall for SysEpollCreate1Handle {
1
}
fn handle(&self, args: &[usize], _from_user: bool) -> Result<usize, SystemError> {
fn handle(&self, args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
return EventPoll::create_epoll(Self::flags(args));
}

View File

@ -1,5 +1,6 @@
//! System call handler for epoll_ctl.
use crate::arch::interrupt::TrapFrame;
use crate::arch::syscall::nr::SYS_EPOLL_CTL;
use crate::filesystem::epoll::event_poll::EventPoll;
use crate::filesystem::epoll::EPollCtlOption;
@ -18,7 +19,7 @@ impl Syscall for SysEpollCtlHandle {
4
}
fn handle(&self, args: &[usize], _from_user: bool) -> Result<usize, SystemError> {
fn handle(&self, args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
let op = EPollCtlOption::from_op_num(Self::op(args))?;
let mut epds = EPollEvent::default();
let event = Self::event(args);

View File

@ -1,5 +1,7 @@
//! System call handler for epoll_pwait.
use super::epoll_utils::do_epoll_wait;
use crate::arch::interrupt::TrapFrame;
use crate::arch::ipc::signal::SigSet;
use crate::arch::syscall::nr::SYS_EPOLL_PWAIT;
use crate::ipc::signal::restore_saved_sigmask;
@ -11,8 +13,6 @@ use crate::syscall::user_access::UserBufferReader;
use alloc::vec::Vec;
use system_error::SystemError;
use super::epoll_utils::do_epoll_wait;
pub struct SysEpollPwaitHandle;
impl Syscall for SysEpollPwaitHandle {
@ -20,7 +20,7 @@ impl Syscall for SysEpollPwaitHandle {
5
}
fn handle(&self, args: &[usize], _from_user: bool) -> Result<usize, SystemError> {
fn handle(&self, args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
let epfd = Self::epfd(args);
let epoll_event = Self::epoll_event(args);
let max_events = Self::max_events(args);

View File

@ -1,6 +1,7 @@
//! System call handler for epoll_wait.
use super::epoll_utils::do_epoll_wait;
use crate::arch::interrupt::TrapFrame;
use crate::arch::syscall::nr::SYS_EPOLL_WAIT;
use crate::mm::VirtAddr;
use crate::syscall::table::FormattedSyscallParam;
@ -15,7 +16,7 @@ impl Syscall for SysEpollWaitHandle {
4
}
fn handle(&self, args: &[usize], _from_user: bool) -> Result<usize, SystemError> {
fn handle(&self, args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
let epfd = Self::epfd(args);
let max_events = Self::max_events(args);
let timeout = Self::timeout(args);

View File

@ -6,9 +6,9 @@ use crate::arch::syscall::nr::SYS_FSTAT;
use crate::syscall::table::FormattedSyscallParam;
use crate::syscall::table::Syscall;
use crate::arch::interrupt::TrapFrame;
use alloc::string::ToString;
use alloc::vec::Vec;
pub struct SysFstatHandle;
impl Syscall for SysFstatHandle {
@ -17,7 +17,7 @@ impl Syscall for SysFstatHandle {
2
}
fn handle(&self, args: &[usize], _from_user: bool) -> Result<usize, SystemError> {
fn handle(&self, args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
let fd = Self::fd(args);
let usr_kstat = Self::usr_kstat(args);
crate::syscall::Syscall::newfstat(fd, usr_kstat)

View File

@ -1,5 +1,6 @@
//! System call handler for ioctls.
use crate::arch::interrupt::TrapFrame;
use crate::arch::syscall::nr::SYS_IOCTL;
use crate::process::ProcessManager;
use crate::syscall::table::FormattedSyscallParam;
@ -29,7 +30,7 @@ impl Syscall for SysIoctlHandle {
///
/// * `Ok(usize)` - On success, returns 0
/// * `Err(SystemError)` - On failure, returns a POSIX error code
fn handle(&self, args: &[usize], _from_user: bool) -> Result<usize, SystemError> {
fn handle(&self, args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
let fd = Self::fd(args);
let cmd = Self::cmd(args);
let data = Self::data(args);

View File

@ -4,13 +4,13 @@ use system_error::SystemError;
use defer::defer;
use crate::arch::interrupt::TrapFrame;
use crate::arch::syscall::nr::SYS_LSTAT;
use crate::filesystem::vfs::file::FileMode;
use crate::filesystem::vfs::syscall::sys_close::do_close;
use crate::filesystem::vfs::ModeType;
use crate::syscall::table::FormattedSyscallParam;
use crate::syscall::table::Syscall;
use alloc::vec::Vec;
pub struct SysLstatHandle;
@ -21,7 +21,7 @@ impl Syscall for SysLstatHandle {
2
}
fn handle(&self, args: &[usize], _from_user: bool) -> Result<usize, SystemError> {
fn handle(&self, args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
let path = Self::path(args);
let usr_kstat = Self::usr_kstat(args);

View File

@ -2,6 +2,7 @@
use system_error::SystemError;
use crate::arch::interrupt::TrapFrame;
use crate::arch::syscall::nr::SYS_OPEN;
use crate::syscall::table::FormattedSyscallParam;
use crate::syscall::table::Syscall;
@ -19,7 +20,7 @@ impl Syscall for SysOpenHandle {
}
/// Handles the open syscall by extracting arguments and calling `do_open`.
fn handle(&self, args: &[usize], _from_user: bool) -> Result<usize, SystemError> {
fn handle(&self, args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
let path = Self::path(args);
let flags = Self::flags(args);
let mode = Self::mode(args);

View File

@ -1,11 +1,11 @@
use system_error::SystemError;
use crate::arch::interrupt::TrapFrame;
use crate::arch::syscall::nr::SYS_READ;
use crate::process::ProcessManager;
use crate::syscall::table::FormattedSyscallParam;
use crate::syscall::table::Syscall;
use crate::syscall::user_access::UserBufferWriter;
use alloc::string::ToString;
use alloc::vec::Vec;
@ -34,12 +34,12 @@ impl Syscall for SysReadHandle {
/// # Returns
/// * `Ok(usize)` - Number of bytes successfully read
/// * `Err(SystemError)` - Error code if operation fails
fn handle(&self, args: &[usize], from_user: bool) -> Result<usize, SystemError> {
fn handle(&self, args: &[usize], frame: &mut TrapFrame) -> Result<usize, SystemError> {
let fd = Self::fd(args);
let buf_vaddr = Self::buf(args);
let len = Self::len(args);
let mut user_buffer_writer = UserBufferWriter::new(buf_vaddr, len, from_user)?;
let mut user_buffer_writer = UserBufferWriter::new(buf_vaddr, len, frame.is_from_user())?;
let user_buf = user_buffer_writer.buffer(0)?;
do_read(fd, user_buf)

View File

@ -1,11 +1,11 @@
use system_error::SystemError;
use crate::arch::interrupt::TrapFrame;
use crate::arch::syscall::nr::SYS_READV;
use crate::filesystem::vfs::iov::IoVec;
use crate::filesystem::vfs::iov::IoVecs;
use crate::syscall::table::FormattedSyscallParam;
use crate::syscall::table::Syscall;
use alloc::string::ToString;
use alloc::vec::Vec;
@ -22,7 +22,7 @@ impl Syscall for SysReadVHandle {
3
}
fn handle(&self, args: &[usize], _from_user: bool) -> Result<usize, SystemError> {
fn handle(&self, args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
let fd = Self::fd(args);
let iov = Self::iov(args);
let count = Self::count(args);

View File

@ -2,14 +2,14 @@
use system_error::SystemError;
use defer::defer;
use crate::arch::interrupt::TrapFrame;
use crate::arch::syscall::nr::SYS_STAT;
use crate::filesystem::vfs::file::FileMode;
use crate::filesystem::vfs::syscall::sys_close::do_close;
use crate::filesystem::vfs::ModeType;
use crate::syscall::table::FormattedSyscallParam;
use crate::syscall::table::Syscall;
use defer::defer;
use alloc::vec::Vec;
@ -21,7 +21,7 @@ impl Syscall for SysStatHandle {
2
}
fn handle(&self, args: &[usize], _from_user: bool) -> Result<usize, SystemError> {
fn handle(&self, args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
let path = Self::path(args);
let usr_kstat = Self::usr_kstat(args);

View File

@ -1,11 +1,11 @@
use system_error::SystemError;
use crate::arch::interrupt::TrapFrame;
use crate::arch::syscall::nr::SYS_WRITE;
use crate::process::ProcessManager;
use crate::syscall::table::FormattedSyscallParam;
use crate::syscall::table::Syscall;
use crate::syscall::user_access::UserBufferReader;
use alloc::string::ToString;
use alloc::vec::Vec;
@ -34,12 +34,12 @@ impl Syscall for SysWriteHandle {
/// # Returns
/// * `Ok(usize)` - Number of bytes successfully written
/// * `Err(SystemError)` - Error code if operation fails
fn handle(&self, args: &[usize], from_user: bool) -> Result<usize, SystemError> {
fn handle(&self, args: &[usize], frame: &mut TrapFrame) -> Result<usize, SystemError> {
let fd = Self::fd(args);
let buf_vaddr = Self::buf(args);
let len = Self::len(args);
let user_buffer_reader = UserBufferReader::new(buf_vaddr, len, from_user)?;
let user_buffer_reader = UserBufferReader::new(buf_vaddr, len, frame.is_from_user())?;
let user_buf = user_buffer_reader.read_from_user(0)?;

View File

@ -10,7 +10,7 @@ use alloc::string::ToString;
use alloc::vec::Vec;
use super::sys_write::do_write;
use crate::arch::interrupt::TrapFrame;
/// System call handler for `writev` operation
///
/// The `writev` system call writes data from multiple buffers to a file descriptor.
@ -38,7 +38,7 @@ impl Syscall for SysWriteVHandle {
///
/// # Safety
/// The caller must ensure the `iov` pointer is valid and points to properly initialized memory.
fn handle(&self, args: &[usize], _from_user: bool) -> Result<usize, SystemError> {
fn handle(&self, args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
let fd = Self::fd(args);
let iov = Self::iov(args);
let count = Self::count(args);

View File

@ -2,6 +2,7 @@ use alloc::string::ToString;
use alloc::vec::Vec;
use core::ffi::c_int;
use crate::arch::interrupt::TrapFrame;
use crate::syscall::table::FormattedSyscallParam;
use crate::syscall::table::Syscall;
use crate::{
@ -60,7 +61,7 @@ impl Syscall for SysKillHandle {
fn num_args(&self) -> usize {
2
}
fn handle(&self, args: &[usize], _from_user: bool) -> Result<usize, SystemError> {
fn handle(&self, args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
let id = Self::pid(args);
let sig_c_int = Self::sig(args);

View File

@ -1,4 +1,5 @@
use super::sys_pipe2::do_kernel_pipe2;
use crate::arch::interrupt::TrapFrame;
use crate::arch::syscall::nr::SYS_PIPE;
use crate::{
filesystem::vfs::file::FileMode,
@ -23,7 +24,7 @@ impl Syscall for SysPipeHandle {
1 // pipefd
}
fn handle(&self, args: &[usize], _from_user: bool) -> Result<usize, SystemError> {
fn handle(&self, args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
let pipefd = Self::pipefd(args);
if pipefd.is_null() {
return Err(SystemError::EFAULT);

View File

@ -1,3 +1,4 @@
use crate::arch::interrupt::TrapFrame;
use crate::{
arch::syscall::nr::SYS_PIPE2,
filesystem::vfs::{
@ -15,7 +16,6 @@ use crate::{
use alloc::vec::Vec;
use core::ffi::c_int;
use system_error::SystemError;
pub struct SysPipe2Handle;
// Extracted core logic for pipe2
@ -80,7 +80,7 @@ impl Syscall for SysPipe2Handle {
2 // fd_ptr, flags
}
fn handle(&self, args: &[usize], _from_user: bool) -> Result<usize, SystemError> {
fn handle(&self, args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
let fd_ptr = Self::pipefd(args);
if fd_ptr.is_null() {
return Err(SystemError::EFAULT);

View File

@ -1,4 +1,5 @@
use super::super::signal_types::{SigInfo, SigType};
use crate::arch::interrupt::TrapFrame;
use crate::{
alloc::vec::Vec,
arch::ipc::signal::{SigCode, Signal},
@ -8,7 +9,6 @@ use crate::{
};
use syscall_table_macros::declare_syscall;
use system_error::SystemError;
pub struct SysRestartHandle;
/// # SYS_RESTART_SYSCALL 系统调用函数,用于重启被信号中断的系统调用
@ -41,7 +41,7 @@ impl Syscall for SysRestartHandle {
Vec::new() // 没有参数返回空Vec
}
fn handle(&self, _args: &[usize], _from_user: bool) -> Result<usize, SystemError> {
fn handle(&self, _args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
do_kernel_restart_syscall()
}
}

View File

@ -1,4 +1,5 @@
use crate::alloc::vec::Vec;
use crate::arch::interrupt::TrapFrame;
use crate::{
arch::ipc::signal::{SigSet, Signal},
arch::syscall::nr::SYS_RT_SIGPROCMASK,
@ -12,7 +13,6 @@ use crate::{
use core::mem::size_of;
use syscall_table_macros::declare_syscall;
use system_error::SystemError; // 添加 Vec
pub struct SysRtSigprocmaskHandle;
/// # SYS_SIGPROCMASK系统调用函数用于设置或查询当前进程的信号屏蔽字
@ -122,7 +122,7 @@ impl Syscall for SysRtSigprocmaskHandle {
]
}
fn handle(&self, args: &[usize], _from_user: bool) -> Result<usize, SystemError> {
fn handle(&self, args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
let how = Self::how(args);
let nset = Self::nset(args);
let oset = Self::oset(args);

View File

@ -1,4 +1,5 @@
use crate::alloc::vec::Vec;
use crate::arch::interrupt::TrapFrame;
use crate::syscall::table::FormattedSyscallParam;
use crate::{
arch::syscall::nr::SYS_SHMAT,
@ -16,7 +17,6 @@ use crate::{
};
use syscall_table_macros::declare_syscall;
use system_error::SystemError;
pub struct SysShmatHandle;
/// # SYS_SHMAT系统调用函数用于连接共享内存段
@ -168,7 +168,7 @@ impl Syscall for SysShmatHandle {
FormattedSyscallParam::new("shmflg", format!("{}", Self::shmflg(args).bits())),
]
}
fn handle(&self, args: &[usize], _from_user: bool) -> Result<usize, SystemError> {
fn handle(&self, args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
let id = Self::id(args);
let vaddr = Self::vaddr(args);
let shmflg = Self::shmflg(args);

View File

@ -1,4 +1,5 @@
use crate::alloc::vec::Vec;
use crate::arch::interrupt::TrapFrame;
use crate::{
arch::syscall::nr::SYS_SHMCTL,
ipc::shm::{shm_manager_lock, ShmCtlCmd, ShmId},
@ -6,7 +7,6 @@ use crate::{
};
use syscall_table_macros::declare_syscall;
use system_error::SystemError;
pub struct SysShmctlHandle;
/// # SYS_SHMCTL系统调用函数用于管理共享内存段
@ -81,11 +81,11 @@ impl Syscall for SysShmctlHandle {
]
}
fn handle(&self, args: &[usize], from_user: bool) -> Result<usize, SystemError> {
fn handle(&self, args: &[usize], frame: &mut TrapFrame) -> Result<usize, SystemError> {
let id = Self::id(args);
let cmd = Self::cmd(args);
let user_buf = Self::user_buf(args);
do_kernel_shmctl(id, cmd, user_buf, from_user)
do_kernel_shmctl(id, cmd, user_buf, frame.is_from_user())
}
}

View File

@ -1,3 +1,4 @@
use crate::arch::interrupt::TrapFrame;
use crate::mm::page::PageFlushAll;
use crate::syscall::table::FormattedSyscallParam;
use crate::{
@ -9,7 +10,6 @@ use crate::{
use alloc::vec::Vec;
use syscall_table_macros::declare_syscall;
use system_error::SystemError;
pub struct SysShmdtHandle;
impl SysShmdtHandle {
@ -40,7 +40,7 @@ impl Syscall for SysShmdtHandle {
///
/// 成功0
/// 失败:错误码
fn handle(&self, args: &[usize], _from_user: bool) -> Result<usize, SystemError> {
fn handle(&self, args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
let vaddr = Self::vaddr(args);
let current_address_space = AddressSpace::current()?;
let mut address_write_guard = current_address_space.write();

View File

@ -1,4 +1,5 @@
use crate::alloc::vec::Vec;
use crate::arch::interrupt::TrapFrame;
use crate::syscall::table::FormattedSyscallParam;
use crate::{
arch::syscall::nr::SYS_SHMGET,
@ -8,7 +9,6 @@ use crate::{
use log::error;
use syscall_table_macros::declare_syscall;
use system_error::SystemError;
pub struct SysShmgetHandle;
/// # SYS_SHMGET系统调用函数用于获取共享内存
@ -90,7 +90,7 @@ impl Syscall for SysShmgetHandle {
3 // key, size, shmflg
}
fn handle(&self, args: &[usize], _from_user: bool) -> Result<usize, SystemError> {
fn handle(&self, args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
let key = Self::key(args);
let size = Self::size(args);
let shmflg = Self::shmflg(args);

View File

@ -2,6 +2,7 @@ use super::super::signal_types::{
SaHandlerType, Sigaction, SigactionType, UserSigaction, USER_SIG_DFL, USER_SIG_ERR,
USER_SIG_IGN,
};
use crate::arch::interrupt::TrapFrame;
use crate::arch::syscall::nr::SYS_RT_SIGACTION;
use crate::syscall::table::{FormattedSyscallParam, Syscall};
use crate::{
@ -14,7 +15,6 @@ use alloc::vec::Vec;
use core::ffi::{c_int, c_void};
use log::error;
use system_error::SystemError;
pub struct SysSigactionHandle;
/// 通用信号注册函数
@ -172,12 +172,12 @@ impl Syscall for SysSigactionHandle {
}
#[no_mangle]
fn handle(&self, args: &[usize], from_user: bool) -> Result<usize, SystemError> {
fn handle(&self, args: &[usize], frame: &mut TrapFrame) -> Result<usize, SystemError> {
let sig = Self::sig(args);
let act = Self::act(args);
let old_act = Self::old_act(args);
do_kernel_sigaction(sig, act, old_act, from_user)
do_kernel_sigaction(sig, act, old_act, frame.is_from_user())
}
fn entry_format(&self, args: &[usize]) -> Vec<FormattedSyscallParam> {
vec![

View File

@ -1,3 +1,4 @@
use crate::arch::interrupt::TrapFrame;
use crate::{
alloc::vec::Vec,
arch::ipc::signal::SigSet,
@ -11,7 +12,6 @@ use crate::{
use core::mem::size_of;
use syscall_table_macros::declare_syscall;
use system_error::SystemError;
pub struct SysSigpendingHandle;
#[inline(never)]
@ -51,7 +51,7 @@ impl Syscall for SysSigpendingHandle {
FormattedSyscallParam::new("sigsetsize", format!("{}", args[1])),
]
}
fn handle(&self, args: &[usize], _from_user: bool) -> Result<usize, SystemError> {
fn handle(&self, args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
let user_sigset = SysSigpendingHandle::user_sigset_ptr(args);
let size = SysSigpendingHandle::sigsetsize(args);

View File

@ -106,7 +106,7 @@ impl Syscall {
// handler.name,
// handler.args_string(args)
// );
return handler.inner_handle.handle(args, frame.is_from_user());
return handler.inner_handle.handle(args, frame);
}
// 如果找不到fallback到原有逻辑

View File

@ -6,6 +6,7 @@ use alloc::vec::Vec;
use core::cell::OnceCell;
use core::fmt::Display;
use crate::arch::interrupt::TrapFrame;
use crate::libs::once::Once;
use crate::syscall::SystemError;
@ -13,7 +14,7 @@ use crate::syscall::SystemError;
pub trait Syscall: Send + Sync + 'static {
/// 系统调用参数数量
fn num_args(&self) -> usize;
fn handle(&self, args: &[usize], from_user: bool) -> Result<usize, SystemError>;
fn handle(&self, args: &[usize], frame: &mut TrapFrame) -> Result<usize, SystemError>;
/// Formats the system call parameters for display/debug purposes
///