diff options
author | Himbeer <himbeer@disroot.org> | 2024-07-27 22:22:31 +0200 |
---|---|---|
committer | Himbeer <himbeer@disroot.org> | 2024-07-27 22:22:31 +0200 |
commit | 5e4f463c81e88202f623b0f07bf8de90b1462fdd (patch) | |
tree | 85bcfb34796911192693b80c5681ecde27a9782c | |
parent | b6e6b4d4251da9a39e2504868f96b681d45be4e2 (diff) |
trap: Make Frame the root struct
-rw-r--r-- | src/lib/TrapFrame.zig | 38 | ||||
-rw-r--r-- | src/lib/interrupts.zig | 6 | ||||
-rw-r--r-- | src/lib/process.zig | 8 | ||||
-rw-r--r-- | src/lib/syscall.zig | 12 | ||||
-rw-r--r-- | src/lib/trap.zig | 38 |
5 files changed, 51 insertions, 51 deletions
diff --git a/src/lib/TrapFrame.zig b/src/lib/TrapFrame.zig new file mode 100644 index 0000000..71556ed --- /dev/null +++ b/src/lib/TrapFrame.zig @@ -0,0 +1,38 @@ +// SPDX-FileCopyrightText: 2024 Himbeer <himbeer@disroot.org> +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +general_purpose_registers: [32]usize, // Offset: 0 +floating_point_registers: [32]usize, // Offset: 256 +satp: usize, // Offset: 512 +stack_pointer: *allowzero u8, // Offset: 520 +hart_id: usize, // Offset: 528 + +const Self = @This(); + +pub fn setReturnValue(self: *Self, value: anytype) void { + switch (@typeInfo(@TypeOf(value))) { + .ErrorUnion => self.returnErrorUnion(value), + .ErrorSet => self.returnError(value), + else => self.returnValue(value), + } +} + +fn returnErrorUnion(self: *Self, error_union: anytype) void { + if (error_union) |value| { + self.returnValue(value); + } else |err| { + self.returnError(err); + } +} + +fn returnError(self: *Self, err: anyerror) void { + self.general_purpose_registers[11] = @intFromError(err); +} + +fn returnValue(self: *Self, value: anytype) void { + self.general_purpose_registers[11] = 0; + if (@typeInfo(@TypeOf(value)) != .Void) { + self.general_purpose_registers[10] = @bitCast(value); + } +} diff --git a/src/lib/interrupts.zig b/src/lib/interrupts.zig index 7010295..f68a5f1 100644 --- a/src/lib/interrupts.zig +++ b/src/lib/interrupts.zig @@ -5,15 +5,15 @@ const std = @import("std"); const Console = @import("Console.zig"); +const TrapFrame = @import("TrapFrame.zig"); const instructions = @import("instructions.zig"); const paging = @import("paging.zig"); const plic = @import("plic.zig"); const process = @import("process.zig"); const syscall = @import("syscall.zig"); const time = @import("sbi/time.zig"); -const trap = @import("trap.zig"); -pub var trap_frame: trap.Frame = undefined; +pub var trap_frame: TrapFrame = undefined; pub const SupervisorTrapVector = packed struct(usize) { pub const Mode = enum(u2) { @@ -88,7 +88,7 @@ pub const SyncCause = enum(u63) { _, }; -export fn handleTrap(epc: usize, cause_bits: usize, frame: *trap.Frame) usize { +export fn handleTrap(epc: usize, cause_bits: usize, frame: *TrapFrame) usize { const console = Console.autoChoose().?; const w = console.writer(); diff --git a/src/lib/process.zig b/src/lib/process.zig index 780ef1d..6893136 100644 --- a/src/lib/process.zig +++ b/src/lib/process.zig @@ -5,11 +5,11 @@ const builtin = @import("builtin"); const std = @import("std"); +const TrapFrame = @import("TrapFrame.zig"); const instructions = @import("instructions.zig"); const paging = @import("paging.zig"); const rethooks = @import("rethooks.zig"); const time = @import("sbi/time.zig"); -const trap = @import("trap.zig"); const Allocator = std.mem.Allocator; const elf = std.elf; @@ -56,7 +56,7 @@ pub const Info = struct { allocator: Allocator, id: u16, thread_id: usize, - trap_frame: trap.Frame, + trap_frame: TrapFrame, pages: []align(paging.page_size) u8, stack: []align(paging.page_size) u8, pc: usize, @@ -70,7 +70,7 @@ pub const Info = struct { pub fn createThread(self: *const Info, allocator: ?Allocator, entry: usize) !*Info { const alloc = allocator orelse self.allocator; - var trap_frame = std.mem.zeroInit(trap.Frame, .{}); + var trap_frame = std.mem.zeroInit(TrapFrame, .{}); const stack = try paging.zeroedAlloc(num_stack_pages); errdefer paging.free(stack); @@ -295,7 +295,7 @@ pub fn create(allocator: Allocator, elf_buf: []align(@alignOf(elf.Elf64_Ehdr)) c .allocator = allocator, .id = next_pid, .thread_id = 0, - .trap_frame = std.mem.zeroInit(trap.Frame, .{}), + .trap_frame = std.mem.zeroInit(TrapFrame, .{}), .pages = pages, .stack = @ptrCast(stack), .pc = hdr.entry, diff --git a/src/lib/syscall.zig b/src/lib/syscall.zig index 19e4f38..0edd367 100644 --- a/src/lib/syscall.zig +++ b/src/lib/syscall.zig @@ -5,11 +5,11 @@ const std = @import("std"); const Console = @import("Console.zig"); +const TrapFrame = @import("TrapFrame.zig"); const instructions = @import("instructions.zig"); const mem = @import("mem.zig"); const paging = @import("paging.zig"); const process = @import("process.zig"); -const trap = @import("trap.zig"); pub const Error = error{ Unimplemented, @@ -20,7 +20,7 @@ pub const HandleError = error{ UnknownSyscall, }; -pub fn handler(proc: *process.Info, trap_frame: *trap.Frame) !void { +pub fn handler(proc: *process.Info, trap_frame: *TrapFrame) !void { switch (trap_frame.general_purpose_registers[17]) { 100000 => trap_frame.setReturnValue(errorName(trap_frame)), 100001 => trap_frame.setReturnValue(consoleWrite(trap_frame)), @@ -36,7 +36,7 @@ pub fn handler(proc: *process.Info, trap_frame: *trap.Frame) !void { pub const ErrorNameError = error{ErrorOutOfRange}; // errorName(value: usize, buffer: [*]u8, len: usize) !usize -fn errorName(trap_frame: *const trap.Frame) !usize { +fn errorName(trap_frame: *const TrapFrame) !usize { const value_wide = trap_frame.general_purpose_registers[10]; const buffer_opt: ?[*]u8 = @ptrFromInt(trap_frame.general_purpose_registers[11]); const buffer_ptr = buffer_opt orelse return Error.ZeroAddressSupplied; @@ -61,7 +61,7 @@ fn errorName(trap_frame: *const trap.Frame) !usize { } // consoleWrite(bytes_addr: usize, len: usize) !usize -fn consoleWrite(trap_frame: *const trap.Frame) !usize { +fn consoleWrite(trap_frame: *const TrapFrame) !usize { const vaddr = trap_frame.general_purpose_registers[10]; const len = trap_frame.general_purpose_registers[11]; @@ -82,7 +82,7 @@ fn consoleWrite(trap_frame: *const trap.Frame) !usize { } // launch(bytes: [*]align(@alignOf(std.elf.Elf64_Ehdr)) const u8, len: usize) !usize -fn launch(trap_frame: *const trap.Frame) !usize { +fn launch(trap_frame: *const TrapFrame) !usize { const alignment = @alignOf(std.elf.Elf64_Ehdr); const bytes_addr = trap_frame.general_purpose_registers[10]; const bytes_opt: ?[*]align(alignment) const u8 = @ptrFromInt(bytes_addr); @@ -112,7 +112,7 @@ pub const TerminateError = error{ }; // terminate(pid: u16, tid: usize) !void -fn terminate(proc: *const process.Info, trap_frame: *const trap.Frame) !void { +fn terminate(proc: *const process.Info, trap_frame: *const TrapFrame) !void { const pid_wide = trap_frame.general_purpose_registers[10]; const pid = std.math.cast(u16, pid_wide) orelse { return TerminateError.PidOutOfRange; diff --git a/src/lib/trap.zig b/src/lib/trap.zig deleted file mode 100644 index 99db8ba..0000000 --- a/src/lib/trap.zig +++ /dev/null @@ -1,38 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Himbeer <himbeer@disroot.org> -// -// SPDX-License-Identifier: AGPL-3.0-or-later - -pub const Frame = extern struct { - general_purpose_registers: [32]usize, // Offset: 0 - floating_point_registers: [32]usize, // Offset: 256 - satp: usize, // Offset: 512 - stack_pointer: *allowzero u8, // Offset: 520 - hart_id: usize, // Offset: 528 - - pub fn setReturnValue(self: *Frame, value: anytype) void { - switch (@typeInfo(@TypeOf(value))) { - .ErrorUnion => self.returnErrorUnion(value), - .ErrorSet => self.returnError(value), - else => self.returnValue(value), - } - } - - fn returnErrorUnion(self: *Frame, error_union: anytype) void { - if (error_union) |value| { - self.returnValue(value); - } else |err| { - self.returnError(err); - } - } - - fn returnError(self: *Frame, err: anyerror) void { - self.general_purpose_registers[11] = @intFromError(err); - } - - fn returnValue(self: *Frame, value: anytype) void { - self.general_purpose_registers[11] = 0; - if (@typeInfo(@TypeOf(value)) != .Void) { - self.general_purpose_registers[10] = @bitCast(value); - } - } -}; |