diff --git a/kernel/gdt.zig b/kernel/gdt.zig index af5cc60..f1aa73e 100644 --- a/kernel/gdt.zig +++ b/kernel/gdt.zig @@ -15,7 +15,7 @@ const GDTEntry = packed struct { base_high: u8 = 0, }; -const TSS = packed struct { +const TSSDescriptor = packed struct { length: u16, base_low: u16, base_mid: u8, @@ -26,13 +26,23 @@ const TSS = packed struct { reserved: u32, }; +const TSS = extern struct { + reserved0: u32 align(1) = 0, + rsp: [3]u64 align(1), + reserved1: u64 align(1) = 0, + ist: [7]u64 align(1), + reserved2: u80 align(1) = 0, + reserved3: u16 align(1) = 0, + iopb: u16 align(1), +}; + const GDT = extern struct { null_entry: GDTEntry align(8), kernel_code: GDTEntry align(8), kernel_data: GDTEntry align(8), user_code: GDTEntry align(8), user_data: GDTEntry align(8), - // tss: TSS align(8), + // tss: TSSDescriptor align(8), }; const GDTDescriptor = extern struct { @@ -40,7 +50,11 @@ const GDTDescriptor = extern struct { base: u64 align(1), }; -var gdtr: GDTDescriptor = undefined; +var gdtr: GDTDescriptor = .{ + .limit = @sizeOf(GDT) - 1, + .base = undefined, +}; + var gdt: GDT = .{ .null_entry = .{}, .kernel_code = .{ @@ -70,8 +84,13 @@ var gdt: GDT = .{ // .tss = undefined, }; +var tss: TSS = .{ + .rsp = undefined, + .ist = undefined, + .iopb = undefined, +}; + pub fn init() void { - gdtr.limit = @sizeOf(GDT) - 1; gdtr.base = @intFromPtr(&gdt); reloadGDT(); @@ -94,7 +113,22 @@ fn reloadGDT() void { \\mov %%ax, %%gs \\mov %%ax, %%ss : - : [gdtr] "r" (&gdtr), + : [gdtr] "r" (&gdtr) : "rax", "memory" ); } + +// pub fn loadTss(tss: *TSS) void { +// // TODO: create lock +// // TODO: lock defer unlock + +// gdt.tss.base_low = @intFromPtr(tss); +// gdt.tss.base_mid = @truncate(@intFromPtr(tss) >> 16); +// gdt.tss.flags1 = 0b10001001; +// gdt.tss.flags2 = 0; +// gdt.tss.base_high = @truncate(@intFromPtr(tss) >> 24); +// gdt.tss.base_upper = @truncate(@intFromPtr(tss) >> 32); +// gdt.tss.reserved = 0; + +// asm volatile ("ltr %[tss]" : : [tss] "r" (0x28) : "memory"); +// }