From cf160d12606d5640ed9bafa11d853b48fcbfd3d6 Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 12 Oct 2022 13:18:35 +0200 Subject: [PATCH] Scheduler: Use misc/utils.h instead of doing everything manually. That way the code is cleaner + we have one single point of failure. --- kernel/src/thread/Scheduler.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/kernel/src/thread/Scheduler.cpp b/kernel/src/thread/Scheduler.cpp index 71856ade..0208f03e 100644 --- a/kernel/src/thread/Scheduler.cpp +++ b/kernel/src/thread/Scheduler.cpp @@ -7,6 +7,7 @@ #include "memory/MemoryManager.h" #include "memory/VMM.h" #include "misc/hang.h" +#include "misc/utils.h" #include "panic/Panic.h" #include "std/stdlib.h" #include "std/string.h" @@ -35,7 +36,7 @@ void Scheduler::init() memset(&idle_task, 0, sizeof(Task)); idle_task.id = free_tid++; idle_task.regs.rip = (uint64_t)idle_task_function; - idle_task.regs.rsp = (uint64_t)MemoryManager::get_page(); + idle_task.regs.rsp = Utilities::get_top_of_stack((uint64_t)MemoryManager::get_page(), 1); idle_task.regs.cs = 0x08; idle_task.regs.ss = 0x10; idle_task.regs.rflags = (1 << 21) | (1 << 9); @@ -64,12 +65,11 @@ void Scheduler::add_kernel_task(void (*task)(void)) new_task->regs.rip = (uint64_t)task; new_task->allocated_stack = (uint64_t)MemoryManager::get_pages(TASK_PAGES_IN_STACK); // 16 KB is enough for everyone, right? - new_task->regs.rsp = new_task->allocated_stack + (PAGE_SIZE * TASK_PAGES_IN_STACK) - sizeof(uintptr_t); + new_task->regs.rsp = Utilities::get_top_of_stack(new_task->allocated_stack, TASK_PAGES_IN_STACK); new_task->regs.cs = 0x08; new_task->regs.ss = 0x10; new_task->regs.ds = 0x10; - asm volatile("pushfq; movq (%%rsp), %%rax; movq %%rax, %0; popfq;" : "=m"(new_task->regs.rflags)::"%rax"); - new_task->regs.rflags |= 0x200; // enable interrupts + new_task->regs.rflags = Utilities::get_rflags() | 0x200; // enable interrupts new_task->task_sleep = 0; new_task->task_time = 0; new_task->cpu_time = 0; @@ -92,7 +92,7 @@ void Scheduler::add_user_task(void* task) new_task->regs.rip = (uint64_t)task; new_task->allocated_stack = (uint64_t)MemoryManager::get_pages( TASK_PAGES_IN_STACK, MAP_READ_WRITE | MAP_USER); // 16 KB is enough for everyone, right? - new_task->regs.rsp = new_task->allocated_stack + (PAGE_SIZE * TASK_PAGES_IN_STACK) - sizeof(uintptr_t); + new_task->regs.rsp = Utilities::get_top_of_stack(new_task->allocated_stack, TASK_PAGES_IN_STACK); new_task->regs.cs = 0x18 | 0x03; new_task->regs.ss = 0x20 | 0x03; new_task->regs.ds = 0x20 | 0x03; @@ -128,7 +128,7 @@ void Scheduler::load_user_task(const char* filename) new_task->image = image; new_task->allocated_stack = (uint64_t)MemoryManager::get_pages( TASK_PAGES_IN_STACK, MAP_READ_WRITE | MAP_USER); // 16 KB is enough for everyone, right? - new_task->regs.rsp = new_task->allocated_stack + (PAGE_SIZE * TASK_PAGES_IN_STACK) - sizeof(uintptr_t); + new_task->regs.rsp = Utilities::get_top_of_stack(new_task->allocated_stack, TASK_PAGES_IN_STACK); new_task->regs.cs = 0x18 | 0x03; new_task->regs.ss = 0x20 | 0x03; new_task->regs.ds = 0x20 | 0x03;