Compare commits

...

2 Commits

Author SHA1 Message Date
cf160d1260 Scheduler: Use misc/utils.h instead of doing everything manually.
That way the code is cleaner + we have one single point of failure.
2022-10-12 13:18:35 +02:00
66add380cf Kernel/Utilities: add a new get_top_of_stack convenience function 2022-10-12 13:17:58 +02:00
3 changed files with 16 additions and 6 deletions

View File

@ -7,4 +7,5 @@ namespace Utilities
uint64_t size); // Returns how many blocks of size blocksize does size occupy.
uint64_t get_rflags();
uint64_t get_top_of_stack(uint64_t bottom, uint64_t stack_pages);
}

View File

@ -1,5 +1,9 @@
#include "misc/utils.h"
#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
#endif
uint64_t Utilities::get_blocks_from_size(uint64_t blocksize, uint64_t size)
{
return (size + (blocksize - 1)) / blocksize;
@ -10,3 +14,8 @@ uint64_t Utilities::get_rflags()
{
return asm_get_rflags();
}
uint64_t Utilities::get_top_of_stack(uint64_t bottom, uint64_t stack_pages)
{
return bottom + (stack_pages * PAGE_SIZE) - sizeof(uintptr_t);
}

View File

@ -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;