From 8d3e6e5b494f752628bcf56601b84e14a92bd65f Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Tue, 15 Apr 2025 16:30:04 +0200 Subject: [PATCH] WIP: exception handling interfaces. --- src/arch/x86.rs | 30 ++++++++++++++++++++++++++++++ src/arch/x86_64.rs | 30 ++++++++++++++++++++++++++++++ src/call.rs | 44 +++++--------------------------------------- src/data.rs | 34 +++++++++++++++++++++++++++------- src/flag.rs | 6 ++++-- 5 files changed, 96 insertions(+), 48 deletions(-) diff --git a/src/arch/x86.rs b/src/arch/x86.rs index bca65c8f00..c6159ac920 100644 --- a/src/arch/x86.rs +++ b/src/arch/x86.rs @@ -211,3 +211,33 @@ impl DerefMut for EnvRegisters { } } } + +#[derive(Clone, Copy, Debug, Default)] +#[repr(C, packed)] +pub struct Exception { + pub kind: usize, + pub code: usize, + pub address: usize, +} +impl Deref for Exception { + type Target = [u8]; + fn deref(&self) -> &[u8] { + unsafe { + slice::from_raw_parts( + self as *const Exception as *const u8, + mem::size_of::(), + ) + } + } +} + +impl DerefMut for Exception { + fn deref_mut(&mut self) -> &mut [u8] { + unsafe { + slice::from_raw_parts_mut( + self as *mut Exception as *mut u8, + mem::size_of::(), + ) + } + } +} diff --git a/src/arch/x86_64.rs b/src/arch/x86_64.rs index bb11ba10db..95f4a6e210 100644 --- a/src/arch/x86_64.rs +++ b/src/arch/x86_64.rs @@ -156,3 +156,33 @@ impl DerefMut for EnvRegisters { } } } + +#[derive(Clone, Copy, Debug, Default)] +#[repr(C, packed)] +pub struct Exception { + pub kind: usize, + pub code: usize, + pub address: usize, +} +impl Deref for Exception { + type Target = [u8]; + fn deref(&self) -> &[u8] { + unsafe { + slice::from_raw_parts( + self as *const Exception as *const u8, + mem::size_of::(), + ) + } + } +} + +impl DerefMut for Exception { + fn deref_mut(&mut self) -> &mut [u8] { + unsafe { + slice::from_raw_parts_mut( + self as *mut Exception as *mut u8, + mem::size_of::(), + ) + } + } +} diff --git a/src/call.rs b/src/call.rs index 3e9e2ab5ec..3ca0e48ef9 100644 --- a/src/call.rs +++ b/src/call.rs @@ -75,14 +75,7 @@ pub fn fpath(fd: usize, buf: &mut [u8]) -> Result { /// Rename a file pub fn frename>(fd: usize, path: T) -> Result { let path = path.as_ref(); - unsafe { - syscall3( - SYS_FRENAME, - fd, - path.as_ptr() as usize, - path.len(), - ) - } + unsafe { syscall3(SYS_FRENAME, fd, path.as_ptr() as usize, path.len()) } } /// Get metadata about a file @@ -183,28 +176,13 @@ pub fn nanosleep(req: &TimeSpec, rem: &mut TimeSpec) -> Result { /// Open a file pub fn open>(path: T, flags: usize) -> Result { let path = path.as_ref(); - unsafe { - syscall3( - SYS_OPEN, - path.as_ptr() as usize, - path.len(), - flags, - ) - } + unsafe { syscall3(SYS_OPEN, path.as_ptr() as usize, path.len(), flags) } } /// Open a file at a specific path pub fn openat>(fd: usize, path: T, flags: usize) -> Result { let path = path.as_ref(); - unsafe { - syscall4( - SYS_OPENAT, - fd, - path.as_ptr() as usize, - path.len(), - flags, - ) - } + unsafe { syscall4(SYS_OPENAT, fd, path.as_ptr() as usize, path.len(), flags) } } /// Read from a file descriptor into a buffer @@ -215,25 +193,13 @@ pub fn read(fd: usize, buf: &mut [u8]) -> Result { /// Remove a directory pub fn rmdir>(path: T) -> Result { let path = path.as_ref(); - unsafe { - syscall2( - SYS_RMDIR, - path.as_ptr() as usize, - path.len(), - ) - } + unsafe { syscall2(SYS_RMDIR, path.as_ptr() as usize, path.len()) } } /// Remove a file pub fn unlink>(path: T) -> Result { let path = path.as_ref(); - unsafe { - syscall2( - SYS_UNLINK, - path.as_ptr() as usize, - path.len(), - ) - } + unsafe { syscall2(SYS_UNLINK, path.as_ptr() as usize, path.len()) } } /// Write a buffer to a file descriptor diff --git a/src/data.rs b/src/data.rs index 68e2dd2f81..1edcdc6e6e 100644 --- a/src/data.rs +++ b/src/data.rs @@ -409,18 +409,38 @@ pub struct ProcSchemeAttrs { impl Deref for ProcSchemeAttrs { type Target = [u8]; fn deref(&self) -> &[u8] { - unsafe { - slice::from_raw_parts( - self as *const Self as *const u8, - mem::size_of::(), - ) - } + unsafe { slice::from_raw_parts(self as *const Self as *const u8, mem::size_of::()) } } } impl DerefMut for ProcSchemeAttrs { fn deref_mut(&mut self) -> &mut [u8] { unsafe { - slice::from_raw_parts_mut(self as *mut ProcSchemeAttrs as *mut u8, mem::size_of::()) + slice::from_raw_parts_mut( + self as *mut ProcSchemeAttrs as *mut u8, + mem::size_of::(), + ) + } + } +} +#[derive(Copy, Clone, Debug, Default)] +#[repr(C)] +pub struct CtxtStsBuf { + pub status: usize, + pub excp: crate::Exception, +} +impl Deref for CtxtStsBuf { + type Target = [u8]; + fn deref(&self) -> &[u8] { + unsafe { slice::from_raw_parts(self as *const Self as *const u8, mem::size_of::()) } + } +} +impl DerefMut for CtxtStsBuf { + fn deref_mut(&mut self) -> &mut [u8] { + unsafe { + slice::from_raw_parts_mut( + self as *mut CtxtStsBuf as *mut u8, + mem::size_of::(), + ) } } } diff --git a/src/flag.rs b/src/flag.rs index b4e2ed2273..7b172c0c94 100644 --- a/src/flag.rs +++ b/src/flag.rs @@ -171,14 +171,17 @@ pub const O_ACCMODE: usize = O_RDONLY | O_WRONLY | O_RDWR; // The top 48 bits of PTRACE_* are reserved, for now // NOT ABI STABLE! -#[derive(Clone, Copy, Debug, Eq, PartialEq)] +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] #[repr(usize)] pub enum ContextStatus { Runnable, Blocked, NotYetStarted, Dead, + ForceKilled, Stopped, + UnhandledExcp, + #[default] Other, // reserved } @@ -306,7 +309,6 @@ pub const ADDRSPACE_OP_MUNMAP: usize = 1; pub const ADDRSPACE_OP_MPROTECT: usize = 2; pub const ADDRSPACE_OP_TRANSFER: usize = 3; - bitflags! { pub struct MremapFlags: usize { const FIXED = 1;