From d492fff0fe62fae8655436d569f8ff3a41817ae7 Mon Sep 17 00:00:00 2001 From: Himbeer Date: Sat, 27 Jul 2024 17:47:11 +0200 Subject: syscall: Add errorName syscall to convert error code to string --- src/lib/syscall.zig | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) (limited to 'src/lib/syscall.zig') diff --git a/src/lib/syscall.zig b/src/lib/syscall.zig index db2136f..ab36dc0 100644 --- a/src/lib/syscall.zig +++ b/src/lib/syscall.zig @@ -20,16 +20,44 @@ pub const HandleError = error{ pub fn handler(proc: *process.Info, trap_frame: *trap.Frame) !void { switch (trap_frame.general_purpose_registers[17]) { - 100000 => trap_frame.setReturnValue(uprint(trap_frame)), - 100001 => trap_frame.setReturnValue(launch(trap_frame)), - 100002 => trap_frame.setReturnValue(end(proc)), - 100003 => trap_frame.setReturnValue(terminate(proc, trap_frame)), - 100004 => trap_frame.setReturnValue(processId(proc)), - 100005 => trap_frame.setReturnValue(threadId(proc)), + 100000 => trap_frame.setReturnValue(errorName(trap_frame)), + 100001 => trap_frame.setReturnValue(uprint(trap_frame)), + 100002 => trap_frame.setReturnValue(launch(trap_frame)), + 100003 => trap_frame.setReturnValue(end(proc)), + 100004 => trap_frame.setReturnValue(terminate(proc, trap_frame)), + 100005 => trap_frame.setReturnValue(processId(proc)), + 100006 => trap_frame.setReturnValue(threadId(proc)), else => return HandleError.UnknownSyscall, } } +pub const ErrorNameError = error{ErrorOutOfRange}; + +// errorName(value: usize, buffer: [*]u8, len: usize) !usize +fn errorName(trap_frame: *const trap.Frame) !usize { + const value_wide = trap_frame.general_purpose_registers[10]; + // fixme: Kernel panic if null pointer + const buffer_ptr: [*]u8 = @ptrFromInt(trap_frame.general_purpose_registers[11]); + const len = trap_frame.general_purpose_registers[12]; + + const value = std.math.cast(u16, value_wide) orelse { + return ErrorNameError.ErrorOutOfRange; + }; + const buffer = buffer_ptr[0..len]; + + if (value == 0) return 0; + + const error_name = @errorName(@errorFromInt(value)); + + const n = @min(buffer.len, error_name.len); + + paging.setUserMemoryAccess(true); + defer paging.setUserMemoryAccess(false); + + @memcpy(buffer[0..n], error_name[0..n]); + return n; +} + // uprint(str_addr: usize, len: usize) void fn uprint(trap_frame: *const trap.Frame) void { const procmem: *paging.Table = @ptrFromInt(instructions.satp.read().ppn << 12); -- cgit v1.2.3