WIP: exception handling interfaces.

This commit is contained in:
4lDO2
2025-04-15 16:30:04 +02:00
parent c9d824f406
commit 8d3e6e5b49
5 changed files with 96 additions and 48 deletions
+30
View File
@@ -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::<Exception>(),
)
}
}
}
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::<Exception>(),
)
}
}
}
+30
View File
@@ -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::<Exception>(),
)
}
}
}
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::<Exception>(),
)
}
}
}
+5 -39
View File
@@ -75,14 +75,7 @@ pub fn fpath(fd: usize, buf: &mut [u8]) -> Result<usize> {
/// Rename a file
pub fn frename<T: AsRef<str>>(fd: usize, path: T) -> Result<usize> {
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<usize> {
/// Open a file
pub fn open<T: AsRef<str>>(path: T, flags: usize) -> Result<usize> {
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<T: AsRef<str>>(fd: usize, path: T, flags: usize) -> Result<usize> {
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<usize> {
/// Remove a directory
pub fn rmdir<T: AsRef<str>>(path: T) -> Result<usize> {
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<T: AsRef<str>>(path: T) -> Result<usize> {
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
+27 -7
View File
@@ -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::<Self>(),
)
}
unsafe { slice::from_raw_parts(self as *const Self as *const u8, mem::size_of::<Self>()) }
}
}
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::<ProcSchemeAttrs>())
slice::from_raw_parts_mut(
self as *mut ProcSchemeAttrs as *mut u8,
mem::size_of::<ProcSchemeAttrs>(),
)
}
}
}
#[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::<Self>()) }
}
}
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::<CtxtStsBuf>(),
)
}
}
}
+4 -2
View File
@@ -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;