diff options
author | Himbeer <himbeer@disroot.org> | 2024-07-27 17:47:11 +0200 |
---|---|---|
committer | Himbeer <himbeer@disroot.org> | 2024-07-27 17:47:11 +0200 |
commit | d492fff0fe62fae8655436d569f8ff3a41817ae7 (patch) | |
tree | 7dd72fe2d2254265768c492e25346d6d7b91eebe /src/lib | |
parent | d332949e66ad0e42b87443279997517c5d5b0610 (diff) |
syscall: Add errorName syscall to convert error code to string
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/syscall.zig | 40 |
1 files changed, 34 insertions, 6 deletions
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); |