Return result type from syscall and format info on panic.

This commit is contained in:
Drew Galbraith 2024-07-26 16:31:57 -07:00
parent e310eee468
commit 51d40f6db6
4 changed files with 91 additions and 14 deletions

View file

@ -8,22 +8,84 @@ use core::panic::PanicInfo;
include!("bindings.rs");
pub fn syscall<T>(id: u64, req: &T) -> u64 {
unsafe { SysCall1(id, req as *const T as *const c_void) }
pub enum ZError {
UNKNOWN = 0x0,
// First set of error codes generally indicate user errors.
INVALID_ARGUMENT = 0x1,
NOT_FOUND = 0x2,
PERMISSION_DENIED = 0x3,
NULL_PTR = 0x4,
EMPTY = 0x5,
ALREADY_EXISTS = 0x6,
BUFFER_SIZE = 0x7,
FAILED_PRECONDITION = 0x8,
// Second set of error codes generally indicate service errors.
INTERNAL = 0x100,
UNIMPLEMENTED = 0x101,
EXHAUSTED = 0x102,
INVALID_RESPONSE = 0x103,
// Kernel specific error codes (relating to capabilities).
CAP_NOT_FOUND = 0x1000,
CAP_WRONG_TYPE = 0x1001,
CAP_PERMISSION_DENIED = 0x1002,
}
pub fn checked_syscall<T>(id: u64, req: &T) {
if syscall(id, req) != 0 {
debug("Bad syscall response.");
panic!();
impl From<u64> for ZError {
fn from(value: u64) -> Self {
match value {
0x1 => ZError::INVALID_ARGUMENT,
0x2 => ZError::NOT_FOUND,
0x3 => ZError::PERMISSION_DENIED,
0x4 => ZError::NULL_PTR,
0x5 => ZError::EMPTY,
0x6 => ZError::ALREADY_EXISTS,
0x7 => ZError::BUFFER_SIZE,
0x8 => ZError::FAILED_PRECONDITION,
0x100 => ZError::INTERNAL,
0x101 => ZError::UNIMPLEMENTED,
0x102 => ZError::EXHAUSTED,
0x103 => ZError::INVALID_RESPONSE,
0x1000 => ZError::CAP_NOT_FOUND,
0x1001 => ZError::CAP_WRONG_TYPE,
0x1002 => ZError::CAP_PERMISSION_DENIED,
_ => ZError::UNKNOWN,
}
}
}
impl fmt::Debug for ZError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("ZError")
}
}
#[must_use]
pub fn syscall<T>(id: u64, req: &T) -> Result<(), ZError> {
unsafe {
let resp = SysCall1(id, req as *const T as *const c_void);
if resp != 0 {
return Err(ZError::from(resp));
}
}
Ok(())
}
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
fn panic(info: &PanicInfo) -> ! {
unsafe {
if crate::mem::CAN_ALLOC {
crate::debug!("Panic occured: {}", info);
} else {
debug("Panic occured before heap initialized.")
}
}
// Internal error.
let req = ZProcessExitReq { code: 0x100 };
syscall(kZionProcessExit, &req);
let _ = syscall(kZionProcessExit, &req);
unreachable!()
}
@ -32,7 +94,7 @@ pub fn debug(msg: &str) {
message: msg.as_ptr() as *const i8,
size: msg.len() as u64,
};
syscall(kZionDebug, &req);
syscall(kZionDebug, &req).expect("Failed to write");
}
pub struct Writer {
@ -73,7 +135,9 @@ macro_rules! debug {
($fmt:literal, $($val:expr),+) => {{
use core::fmt::Write as _;
use alloc::string::String;
let mut w = mammoth::syscall::Writer::new();
// TODO: Find a way to do this so we don't have to import writer.
// We can't fully qualify this if we want to use it in this crate.
let mut w = Writer::new();
write!(&mut w, $fmt, $($val),*).expect("Failed to format");
let s: String = w.into();
debug(&s);