From ea66641de9eaf3e58be07e7d7f15cd9ec51202fd Mon Sep 17 00:00:00 2001 From: Ratakor Date: Thu, 28 Sep 2023 20:35:07 +0200 Subject: [PATCH] Add swapgs to idt, start time --- kernel/debug.zig | 11 +++++------ kernel/idt.zig | 28 +++++++++++----------------- kernel/main.zig | 8 ++++++-- kernel/time.zig | 14 ++++++++++++++ 4 files changed, 36 insertions(+), 25 deletions(-) create mode 100644 kernel/time.zig diff --git a/kernel/debug.zig b/kernel/debug.zig index 266de9c..6d160d5 100644 --- a/kernel/debug.zig +++ b/kernel/debug.zig @@ -61,12 +61,10 @@ pub fn printStackTrace(stack_trace: *std.builtin.StackTrace) void { } } -fn printInfo(address: u64, symbol_name: []const u8, file_name: []const u8, line: usize) void { - tty.print("0x{x:0>16}: {s} at {s}:{d}\n", .{ address, symbol_name, file_name, line }); -} - fn printSymbol(address: u64) void { var symbol_name: []const u8 = ""; + var file_name: []const u8 = "??"; + var line: usize = 0; if (debug_info) |*info| brk: { if (info.getSymbolName(address)) |name| { @@ -76,10 +74,11 @@ fn printSymbol(address: u64) void { const compile_unit = info.findCompileUnit(address) catch break :brk; const line_info = info.getLineNumberInfo(debug_allocator, compile_unit.*, address) catch break :brk; - return printInfo(address, symbol_name, line_info.file_name, line_info.line); + file_name = line_info.file_name; + line = line_info.line; } - printInfo(address, symbol_name, "??", 0); + tty.print("0x{x:0>16}: {s} at {s}:{d}\n", .{ address, symbol_name, file_name, line }); } fn getSectionData(elf: [*]const u8, shdr: []const u8) []const u8 { diff --git a/kernel/idt.zig b/kernel/idt.zig index 216e60e..f3b292c 100644 --- a/kernel/idt.zig +++ b/kernel/idt.zig @@ -186,25 +186,18 @@ fn makeStubHandler(vector: u8) InterruptStub { }.handler; } -// TODO swapgs - export fn interruptHandler(ctx: *cpu.Context) callconv(.C) void { - // if (ctx.cs != 0x08) { - // asm volatile ("swapgs"); - // } const handler = isr[ctx.isr_vector]; handler(ctx); - // if (ctx.cs != 0x08) { - // asm volatile ("swapgs"); - // } } export fn commonStub() callconv(.Naked) void { asm volatile ( - // \\cmpq $0x08, 0x8(%%rsp) - // \\je 1f - // \\swapgs - // \\1: + // if (cs != gdt.kernel_code) -> swapgs + \\cmpq $0x08, 24(%%rsp) + \\je 1f + \\swapgs + \\1: \\push %%r15 \\push %%r14 \\push %%r13 @@ -247,12 +240,13 @@ export fn commonStub() callconv(.Naked) void { \\pop %%r13 \\pop %%r14 \\pop %%r15 + // if (cs != gdt.kernel_code) -> swapgs + \\cmpq $0x08, 24(%%rsp) + \\je 1f + \\swapgs + \\1: + // restore stack \\add $16, %%rsp - \\ - // \\cmpq $0x08, 0x8(%%rsp) - // \\je 1f - // \\swapgs - // \\1: \\iretq ); } diff --git a/kernel/main.zig b/kernel/main.zig index ee7204e..de13e73 100644 --- a/kernel/main.zig +++ b/kernel/main.zig @@ -8,6 +8,7 @@ const idt = @import("idt.zig"); const pmm = @import("pmm.zig"); const vmm = @import("vmm.zig"); const mem = @import("mem.zig"); +const time = @import("time.zig"); pub const std_options = struct { pub const logFn = debug.log; @@ -21,6 +22,7 @@ pub export var kernel_file_request: limine.KernelFileRequest = .{}; // export var module_request: limine.ModuleRequest = .{}; // export var rsdp_request: limine.RsdpRequest = .{}; pub export var kernel_address_request: limine.KernelAddressRequest = .{}; +pub export var boot_time_request: limine.BootTimeRequest = .{}; inline fn halt() noreturn { while (true) asm volatile ("hlt"); @@ -76,7 +78,7 @@ fn main() !void { serial.init(); debug.init() catch |err| { - tty.print("Failed to initialize debug info: {}\n", .{err}); + tty.print("Failed to initialize debug info: {}\n", .{err}); // TODO warning }; gdt.init(); idt.init(); @@ -91,7 +93,7 @@ fn main() !void { // TODO: apic // TODO: acpi // TODO: pci - // TODO: timers (pit ?) + time.init(); // TODO: timers (pit ?) // TODO: proc // TODO: scheduler @@ -104,6 +106,8 @@ fn main() !void { // TODO: start /bin/init <- load elf with std.elf /////////////////////////////////////////////////////////////////////////// + tty.print("{}\n", .{time.realtime}); + const buf = try pmm.alloc(1, false); defer pmm.free(buf); tty.print("{*} {}\n", .{ buf.ptr, buf.len }); diff --git a/kernel/time.zig b/kernel/time.zig new file mode 100644 index 0000000..dc1aba1 --- /dev/null +++ b/kernel/time.zig @@ -0,0 +1,14 @@ +const std = @import("std"); +const root = @import("root"); + +pub const Timespec = struct { + tv_sec: isize = 0, + tv_nsec: isize = 0, +}; + +pub var realtime: Timespec = .{}; + +pub fn init() void { + const boot_time = root.boot_time_request.response.?; + realtime.tv_sec = boot_time.boot_time; +}