From f2d2ab867e30ee530fdeb72c51e7af0302b48e64 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Sun, 15 Dec 2019 12:46:09 +0100 Subject: [PATCH] Step 5: lock/unlock scheduler --- src/arch/x86/constants.zig | 1 - src/arch/x86/index.zig | 2 +- src/console.zig | 1 + src/constants.zig | 1 + src/index.zig | 1 + src/main.zig | 5 ++++- src/task.zig | 34 +++++++++++++++++++++++++++------- src/vga.zig | 2 ++ 8 files changed, 37 insertions(+), 10 deletions(-) delete mode 100644 src/arch/x86/constants.zig create mode 100644 src/constants.zig diff --git a/src/arch/x86/constants.zig b/src/arch/x86/constants.zig deleted file mode 100644 index 326ed83..0000000 --- a/src/arch/x86/constants.zig +++ /dev/null @@ -1 +0,0 @@ -pub const PAGE_SIZE: usize = 4096; diff --git a/src/arch/x86/index.zig b/src/arch/x86/index.zig index 8cd1c39..46de2f3 100644 --- a/src/arch/x86/index.zig +++ b/src/arch/x86/index.zig @@ -6,10 +6,10 @@ pub const assert = std.debug.assert; pub const kernel = @import("../../index.zig"); // x86 namespace +pub const PAGE_SIZE: usize = 4096; pub usingnamespace @import("lib/io.zig"); pub usingnamespace @import("lib/instructions.zig"); pub usingnamespace @import("main.zig"); -pub usingnamespace @import("constants.zig"); pub const pmem = @import("pmem.zig"); pub const paging = @import("paging.zig"); pub const idt = @import("idt.zig"); diff --git a/src/console.zig b/src/console.zig index 13e9e91..4aac2ec 100644 --- a/src/console.zig +++ b/src/console.zig @@ -58,6 +58,7 @@ pub fn loop() void { keypress(input_ring_buffer[input_read_index]); input_read_index +%= 1; } + task.lock_scheduler(); task.schedule(); } } diff --git a/src/constants.zig b/src/constants.zig new file mode 100644 index 0000000..b823fb3 --- /dev/null +++ b/src/constants.zig @@ -0,0 +1 @@ +pub const SMP: bool = false; diff --git a/src/index.zig b/src/index.zig index 5ef7a58..398bdc5 100644 --- a/src/index.zig +++ b/src/index.zig @@ -8,6 +8,7 @@ pub usingnamespace @import("vga.zig"); pub const x86 = @import("arch/x86/index.zig"); ///core +pub const constants = @import("constants.zig"); pub const layout = @import("layout.zig"); pub const multiboot = @import("multiboot.zig"); pub const vmem = @import("vmem.zig"); diff --git a/src/main.zig b/src/main.zig index 0145594..a12d1c2 100644 --- a/src/main.zig +++ b/src/main.zig @@ -28,5 +28,8 @@ export fn kmain(magic: u32, info: *const multiboot.MultibootInfo) noreturn { task.new(@ptrToInt(topbar)) catch unreachable; task.new(@ptrToInt(console.loop)) catch unreachable; - while (true) task.schedule(); + while (true) { + task.lock_scheduler(); + task.schedule(); + } } diff --git a/src/task.zig b/src/task.zig index fcce7de..4989336 100644 --- a/src/task.zig +++ b/src/task.zig @@ -49,10 +49,10 @@ pub const Task = struct { // allocate a new stack t.esp = (try vmem.malloc(STACK_SIZE)) + STACK_SIZE; - // top of stack is the address that ret will pop + // this will be what ret goes to t.esp -= 4; @intToPtr(*usize, t.esp).* = entrypoint; - // top of stack is ebp that we will pop + // this will be popped into ebp t.esp -= 4; @intToPtr(*usize, t.esp).* = t.esp + 8; @@ -74,17 +74,18 @@ pub fn new(entrypoint: usize) !void { pub fn switch_to(new_task: *ListOfTasks.Node) void { assert(new_task.data != current_task.data); + // save old stack + const old_task_esp_addr = ¤t_task.data.esp; + // switch states current_task.data.state = .ReadyToRun; new_task.data.state = .Running; - - // save old stack - const old_task_esp_addr = ¤t_task.data.esp; current_task = new_task; - // x86.cli(); + + unlock_scheduler(); + // don't inline the asm function, it needs to ret @noInlineCall(switch_tasks, new_task.data.esp, @ptrToInt(old_task_esp_addr)); - // x86.sti(); } // circular next @@ -106,6 +107,25 @@ pub fn schedule() void { if (first_ready_to_run()) |t| switch_to(t); } +var IRQ_disable_counter: usize = 0; + +pub fn lock_scheduler() void { + if (constants.SMP == false) { + x86.cli(); + IRQ_disable_counter += 1; + } +} +pub fn unlock_scheduler() void { + if (IRQ_disable_counter == 0) { + println("trying to unlock here"); + while (true) asm volatile ("hlt"); + } + if (constants.SMP == false) { + IRQ_disable_counter -= 1; + if (IRQ_disable_counter == 0) x86.sti(); + } +} + pub fn introspect() void { update_time_used(); diff --git a/src/vga.zig b/src/vga.zig index 5aee204..4f71f29 100644 --- a/src/vga.zig +++ b/src/vga.zig @@ -75,6 +75,8 @@ pub fn topbar() void { vga.cursor_enabled = true; vga.cursor = cursor; vga.background = bg; + + task.lock_scheduler(); task.schedule(); } }