Skip to content

Commit

Permalink
Start TSS
Browse files Browse the repository at this point in the history
  • Loading branch information
Ratakor committed Sep 25, 2023
1 parent 31db461 commit a9beac5
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions kernel/gdt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -26,21 +26,35 @@ 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 {
limit: u16 align(1),
base: u64 align(1),
};

var gdtr: GDTDescriptor = undefined;
var gdtr: GDTDescriptor = .{
.limit = @sizeOf(GDT) - 1,
.base = undefined,
};

var gdt: GDT = .{
.null_entry = .{},
.kernel_code = .{
Expand Down Expand Up @@ -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();
Expand All @@ -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");
// }

0 comments on commit a9beac5

Please sign in to comment.