diff --git a/core/src/arch/x86_64/pit.zig b/core/src/arch/x86_64/pit.zig index eacb344..c842bd8 100644 --- a/core/src/arch/x86_64/pit.zig +++ b/core/src/arch/x86_64/pit.zig @@ -1,4 +1,5 @@ const io = @import("ioports.zig"); +const platform = @import("platform.zig"); const interrupts = @import("interrupts.zig"); const pic = @import("pic.zig"); const thread = @import("../../thread.zig"); @@ -23,6 +24,6 @@ pub fn initializePIT() void { _ = interrupts.registerIRQ(0, &pitTimerHandler); } -pub fn pitTimerHandler(_: u32, regs: *interrupts.InterruptStackFrame) void { +pub fn pitTimerHandler(_: u32, regs: *platform.Registers) void { thread.preempt(regs); } diff --git a/core/src/arch/x86_64/platform.zig b/core/src/arch/x86_64/platform.zig index b7aa53f..905358c 100644 --- a/core/src/arch/x86_64/platform.zig +++ b/core/src/arch/x86_64/platform.zig @@ -6,6 +6,8 @@ const interrupts = @import("interrupts.zig"); pub const PAGE_SIZE = 4096; +pub const Registers = interrupts.InterruptStackFrame; + // FIXME: Check if it's supported first. fn enableNX() void { asm volatile ( diff --git a/core/src/arch/x86_64/thread.zig b/core/src/arch/x86_64/thread.zig index aadd485..e54c5ab 100644 --- a/core/src/arch/x86_64/thread.zig +++ b/core/src/arch/x86_64/thread.zig @@ -1,7 +1,7 @@ const std = @import("std"); -const interrupts = @import("interrupts.zig"); +const platform = @import("platform.zig"); -pub inline fn enterThread(regs: *interrupts.InterruptStackFrame, base: u64, directory: u64) noreturn { +pub inline fn enterThread(regs: *platform.Registers, base: u64, directory: u64) noreturn { asm volatile ( \\ addq %[base], %rsp \\ push %[ss] @@ -54,28 +54,28 @@ pub inline fn readStackPointer() usize { ); } -pub fn setAddress(regs: *interrupts.InterruptStackFrame, address: u64) void { +pub fn setAddress(regs: *platform.Registers, address: u64) void { regs.rip = address; } -pub fn setArguments(regs: *interrupts.InterruptStackFrame, arg0: u64, arg1: u64) void { +pub fn setArguments(regs: *platform.Registers, arg0: u64, arg1: u64) void { regs.rdi = arg0; regs.rsi = arg1; } -pub fn setStack(regs: *interrupts.InterruptStackFrame, stack: u64) void { +pub fn setStack(regs: *platform.Registers, stack: u64) void { regs.rsp = stack; } -pub fn initKernelRegisters(regs: *interrupts.InterruptStackFrame) void { - regs.* = std.mem.zeroes(interrupts.InterruptStackFrame); +pub fn initKernelRegisters(regs: *platform.Registers) void { + regs.* = std.mem.zeroes(platform.Registers); regs.cs = 0x08; regs.ss = 0x10; regs.rflags = 1 << 9; // IF (Interrupt enable flag) } -pub fn initUserRegisters(regs: *interrupts.InterruptStackFrame) void { - regs.* = std.mem.zeroes(interrupts.InterruptStackFrame); +pub fn initUserRegisters(regs: *platform.Registers) void { + regs.* = std.mem.zeroes(platform.Registers); regs.cs = 0x18 | 3; regs.ss = 0x20 | 3; regs.rflags = 1 << 9; // IF (Interrupt enable flag) diff --git a/core/src/main.zig b/core/src/main.zig index a018bfe..6845e34 100644 --- a/core/src/main.zig +++ b/core/src/main.zig @@ -15,7 +15,7 @@ const MultibootInfo = [*c]u8; const Context = struct { allocator: *pmm.FrameAllocator, space: vmm.AddressSpace, - regs: *interrupts.InterruptStackFrame, + regs: *platform.Registers, }; export fn _start(magic: u32, info: MultibootInfo) callconv(.C) noreturn { diff --git a/core/src/sys/mem.zig b/core/src/sys/mem.zig index 3ce5e91..c75bb28 100644 --- a/core/src/sys/mem.zig +++ b/core/src/sys/mem.zig @@ -1,8 +1,8 @@ -const interrupts = @import("../arch/interrupts.zig").arch; +const platform = @import("../arch/platform.zig").arch; const sys = @import("syscall.zig"); const pmm = @import("../pmm.zig"); -pub fn allocFrame(_: *interrupts.InterruptStackFrame, _: *sys.Arguments, retval: *isize) anyerror!void { +pub fn allocFrame(_: *platform.Registers, _: *sys.Arguments, retval: *isize) anyerror!void { const allocator = pmm.lockGlobalAllocator(); defer pmm.unlockGlobalAllocator(); @@ -11,14 +11,14 @@ pub fn allocFrame(_: *interrupts.InterruptStackFrame, _: *sys.Arguments, retval: retval.* = @bitCast(frame.address); } -pub fn freeFrame(_: *interrupts.InterruptStackFrame, args: *sys.Arguments, _: *isize) anyerror!void { +pub fn freeFrame(_: *platform.Registers, args: *sys.Arguments, _: *isize) anyerror!void { const allocator = pmm.lockGlobalAllocator(); defer pmm.unlockGlobalAllocator(); try pmm.freeFrame(allocator, args.arg0); } -pub fn lockFrame(_: *interrupts.InterruptStackFrame, args: *sys.Arguments, _: *isize) anyerror!void { +pub fn lockFrame(_: *platform.Registers, args: *sys.Arguments, _: *isize) anyerror!void { const allocator = pmm.lockGlobalAllocator(); defer pmm.unlockGlobalAllocator(); diff --git a/core/src/sys/print.zig b/core/src/sys/print.zig index 53a1c42..e0a5dfe 100644 --- a/core/src/sys/print.zig +++ b/core/src/sys/print.zig @@ -1,7 +1,7 @@ -const interrupts = @import("../arch/interrupts.zig").arch; +const platform = @import("../arch/platform.zig").arch; const sys = @import("syscall.zig"); const debug = @import("../arch/debug.zig"); -pub fn print(_: *interrupts.InterruptStackFrame, args: *sys.Arguments, _: *isize) anyerror!void { +pub fn print(_: *platform.Registers, args: *sys.Arguments, _: *isize) anyerror!void { debug.print("The userspace program gave us the number {x}\n", .{args.arg0}); } diff --git a/core/src/sys/sched.zig b/core/src/sys/sched.zig index 2615548..613eb50 100644 --- a/core/src/sys/sched.zig +++ b/core/src/sys/sched.zig @@ -1,25 +1,25 @@ -const interrupts = @import("../arch/interrupts.zig").arch; +const platform = @import("../arch/platform.zig").arch; const sys = @import("syscall.zig"); const thread = @import("../thread.zig"); const cpu = @import("../arch/cpu.zig"); -pub fn yield(regs: *interrupts.InterruptStackFrame, _: *sys.Arguments, _: *isize) anyerror!void { +pub fn yield(regs: *platform.Registers, _: *sys.Arguments, _: *isize) anyerror!void { const core = cpu.thisCore(); const new_thread = thread.fetchNewThread(core, false) orelse return; const current_thread = thread.scheduleNewThread(core, regs, new_thread); thread.addThreadToPriorityQueue(core, current_thread); } -pub fn setPriority(_: *interrupts.InterruptStackFrame, args: *sys.Arguments, _: *isize) anyerror!void { +pub fn setPriority(_: *platform.Registers, args: *sys.Arguments, _: *isize) anyerror!void { const core = cpu.thisCore(); core.current_thread.user_priority = @truncate(args.arg0); } -pub fn getPriority(_: *interrupts.InterruptStackFrame, _: *sys.Arguments, retval: *isize) anyerror!void { +pub fn getPriority(_: *platform.Registers, _: *sys.Arguments, retval: *isize) anyerror!void { const core = cpu.thisCore(); retval.* = core.current_thread.user_priority; } -pub fn sleep(regs: *interrupts.InterruptStackFrame, args: *sys.Arguments, _: *isize) anyerror!void { +pub fn sleep(regs: *platform.Registers, args: *sys.Arguments, _: *isize) anyerror!void { _ = thread.startSleep(regs, args.arg0); } diff --git a/core/src/sys/syscall.zig b/core/src/sys/syscall.zig index 7962ea8..fe628b9 100644 --- a/core/src/sys/syscall.zig +++ b/core/src/sys/syscall.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const interrupts = @import("../arch/interrupts.zig").arch; +const platform = @import("../arch/platform.zig").arch; const print = @import("print.zig").print; const mem = @import("mem.zig"); const sched = @import("sched.zig"); @@ -13,11 +13,11 @@ pub const Arguments = struct { arg5: usize, }; -const SystemCall = *const fn (frame: *interrupts.InterruptStackFrame, args: *Arguments, retval: *isize) anyerror!void; +const SystemCall = *const fn (frame: *platform.Registers, args: *Arguments, retval: *isize) anyerror!void; const syscalls = [_]SystemCall{ print, mem.allocFrame, mem.lockFrame, mem.freeFrame, sched.yield, sched.setPriority, sched.getPriority, sched.sleep }; -pub fn invokeSyscall(number: usize, frame: *interrupts.InterruptStackFrame, args: *Arguments, retval: *isize) void { +pub fn invokeSyscall(number: usize, frame: *platform.Registers, args: *Arguments, retval: *isize) void { if (number >= syscalls.len) { retval.* = -1; return; diff --git a/core/src/thread.zig b/core/src/thread.zig index dc15c86..beff81c 100644 --- a/core/src/thread.zig +++ b/core/src/thread.zig @@ -1,6 +1,6 @@ const std = @import("std"); const vmm = @import("arch/vmm.zig").arch; -const interrupts = @import("arch/interrupts.zig").arch; +const platform = @import("arch/platform.zig").arch; pub const arch = @import("arch/thread.zig").arch; const pmm = @import("pmm.zig"); const cpu = @import("arch/cpu.zig"); @@ -16,7 +16,7 @@ pub const ThreadState = enum { pub const ThreadControlBlock = struct { id: u64, address_space: ?vmm.AddressSpace, - regs: interrupts.InterruptStackFrame, + regs: platform.Registers, state: ThreadState, user_priority: u8, @@ -58,7 +58,7 @@ pub fn enterThread(thread: *ThreadControlBlock) noreturn { } /// Updates the processor state to run a new thread. -fn switchThread(regs: *interrupts.InterruptStackFrame, new_thread: *ThreadControlBlock) void { +fn switchThread(regs: *platform.Registers, new_thread: *ThreadControlBlock) void { const core = cpu.thisCore(); core.current_thread.regs = regs.*; @@ -74,7 +74,7 @@ fn switchThread(regs: *interrupts.InterruptStackFrame, new_thread: *ThreadContro } /// Changes the running thread to a new one and returns the previous one. -pub fn scheduleNewThread(core: *cpu.arch.Core, regs: *interrupts.InterruptStackFrame, new_thread: *ThreadControlBlock) *ThreadControlBlock { +pub fn scheduleNewThread(core: *cpu.arch.Core, regs: *platform.Registers, new_thread: *ThreadControlBlock) *ThreadControlBlock { if (core.active_thread_list.first) |first| { first.data.current_priority +|= 4; } @@ -90,7 +90,7 @@ pub fn scheduleNewThread(core: *cpu.arch.Core, regs: *interrupts.InterruptStackF /// /// Updates the core's sleep queue, checks if the running thread's time /// is up, and if it is, schedules a new one. -pub fn preempt(regs: *interrupts.InterruptStackFrame) void { +pub fn preempt(regs: *platform.Registers) void { const core = cpu.thisCore(); updateSleepQueue(core); @@ -107,7 +107,7 @@ pub fn preempt(regs: *interrupts.InterruptStackFrame) void { } /// Sets the current thread's state to "Blocked" and schedules a new one to replace it. -pub fn block(regs: *interrupts.InterruptStackFrame) *ThreadControlBlock { +pub fn block(regs: *platform.Registers) *ThreadControlBlock { const core = cpu.thisCore(); // fetchNewThread() always returns a thread if should_idle_if_not_found is set to true. @@ -119,7 +119,7 @@ pub fn block(regs: *interrupts.InterruptStackFrame) *ThreadControlBlock { } /// Puts the current thread to sleep, adding it to the sleep queue, and schedules a new one to replace it. -pub fn startSleep(regs: *interrupts.InterruptStackFrame, ticks: u64) *ThreadControlBlock { +pub fn startSleep(regs: *platform.Registers, ticks: u64) *ThreadControlBlock { const core = cpu.thisCore(); // fetchNewThread() always returns a thread if should_idle_if_not_found is set to true.