Skip to content

Commit

Permalink
Add swapgs to idt, start time
Browse files Browse the repository at this point in the history
  • Loading branch information
Ratakor committed Sep 28, 2023
1 parent 902cf7d commit ea66641
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 deletions.
11 changes: 5 additions & 6 deletions kernel/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<no symbol info>";
var file_name: []const u8 = "??";
var line: usize = 0;

if (debug_info) |*info| brk: {
if (info.getSymbolName(address)) |name| {
Expand All @@ -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 {
Expand Down
28 changes: 11 additions & 17 deletions kernel/idt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
);
}
8 changes: 6 additions & 2 deletions kernel/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -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 });
Expand Down
14 changes: 14 additions & 0 deletions kernel/time.zig
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit ea66641

Please sign in to comment.