From 84998d2879bcf4c211fa1ee2b84262707afc9749 Mon Sep 17 00:00:00 2001 From: Namonay Date: Fri, 19 Jul 2024 15:25:21 +0200 Subject: [PATCH] Added user stack, TSS GDT Entries --- sources/drivers/index.zig | 2 +- sources/kernel/arch/x86/arch.zig | 2 +- sources/kernel/arch/x86/boot.zig | 4 ++-- sources/kernel/arch/x86/gdt.zig | 17 ++++++++++------- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/sources/drivers/index.zig b/sources/drivers/index.zig index bab136f..505e379 100644 --- a/sources/drivers/index.zig +++ b/sources/drivers/index.zig @@ -10,7 +10,7 @@ pub fn initDrivers() void kernel.logs.klogln("[Drivers Manager] loading drivers..."); kernel.logs.beginSection(); kb.init(); - vga.init("RatiOS 0.1 : 42", vga.computeColor(vga.Color.BLACK, vga.Color.LIGHT_GREY), vga.computeColor(vga.Color.WHITE, vga.Color.DARK_GREY), vga.computeColor(vga.Color.RED, vga.Color.DARK_GREY)); + vga.init("RatiOS 0.2", vga.computeColor(vga.Color.BLACK, vga.Color.LIGHT_GREY), vga.computeColor(vga.Color.WHITE, vga.Color.DARK_GREY), vga.computeColor(vga.Color.RED, vga.Color.DARK_GREY)); power.init(); kernel.logs.endSection(); kernel.logs.klogln("[Drivers Manager] loaded drivers"); diff --git a/sources/kernel/arch/x86/arch.zig b/sources/kernel/arch/x86/arch.zig index 2947a9c..c24287e 100644 --- a/sources/kernel/arch/x86/arch.zig +++ b/sources/kernel/arch/x86/arch.zig @@ -19,6 +19,6 @@ pub fn disableInts() void pub fn init() void { + idt.idtInit(); gdt.gdtInit(); - idt.idtInit(); } diff --git a/sources/kernel/arch/x86/boot.zig b/sources/kernel/arch/x86/boot.zig index b082936..1abbff0 100644 --- a/sources/kernel/arch/x86/boot.zig +++ b/sources/kernel/arch/x86/boot.zig @@ -16,8 +16,8 @@ comptime ); } -export var kernel_stack: [32 * 1024]u8 align(16) linksection(".bss") = undefined; -export var user_stack: [64 * 1024]u8 align(16) linksection(".bss") = undefined; +pub export var kernel_stack: [32 * 1024]u8 align(16) linksection(".bss") = undefined; +pub export var user_stack: [64 * 1024]u8 align(16) linksection(".bss") = undefined; extern fn kmain() void; diff --git a/sources/kernel/arch/x86/gdt.zig b/sources/kernel/arch/x86/gdt.zig index 0d1ae9c..82e61bd 100644 --- a/sources/kernel/arch/x86/gdt.zig +++ b/sources/kernel/arch/x86/gdt.zig @@ -1,4 +1,5 @@ pub const console = @import("../../io/out.zig"); +const boot = @import("boot.zig"); const GDTEntry = packed struct { @@ -47,7 +48,7 @@ const GDTPointer = packed struct base: *GDTEntry }; -var gdt_entries: *[6]GDTEntry = @ptrFromInt(0x800); +var gdt_entries: *[8]GDTEntry = @ptrFromInt(0x800); var tss_entry: TSSEntry = .{ .prev_tss = 0, @@ -106,7 +107,7 @@ comptime ( \\ .type tssFlush, @function \\ tssFlush: - \\ mov $0x2B, %ax + \\ mov $0x38, %ax \\ ltr %ax \\ ret ); @@ -135,17 +136,19 @@ fn writeTSS(num: u32, ss0: u16, esp0: u32) void pub fn gdtInit() void { - gdt_pointer.limit = @sizeOf(GDTEntry) * 6 - 1; + gdt_pointer.limit = @sizeOf(GDTEntry) * 8 - 1; gdt_pointer.base = &gdt_entries[0]; gdtSetGate(0, 0, 0, 0, 0); gdtSetGate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF); gdtSetGate(2, 0, 0xFFFFFFFF, 0x92, 0xCF); - gdtSetGate(3, 0, 0xFFFFFFFF, 0xFA, 0xCF); - gdtSetGate(4, 0, 0xFFFFFFFF, 0xF2, 0xCF); - // writeTSS(5, 0x10, 0x8000000); + gdtSetGate(3, @intFromPtr(&boot.kernel_stack), @sizeOf(@TypeOf(boot.kernel_stack)) - 1, 0x92, 0xCF); + gdtSetGate(4, 0, 0xFFFFFFFF, 0xFA, 0xCF); + gdtSetGate(5, 0, 0xFFFFFFFF, 0xF2, 0xCF); + gdtSetGate(6, @intFromPtr(&boot.user_stack), @sizeOf(@TypeOf(boot.user_stack)) - 1, 0xF2, 0xCF); + writeTSS(7, 0x10, @intFromPtr(&boot.kernel_stack)); gdtFlush(&gdt_pointer); - // tssFlush(); + tssFlush(); } pub fn gdtSetGate(num: u32, base: u32, limit: u32, access: u8, flags: u8) void