From e21b608af4690aeee2482eb6a23c0709e0df8fa1 Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 14 Oct 2022 17:21:16 +0200 Subject: [PATCH] Utilities: Start moving utilities into specific headers in a utils/ subdirectory --- kernel/include/misc/utils.h | 7 ---- kernel/include/utils/Registers.h | 52 ++++++++++++++++++++++++++++ kernel/src/interrupts/Interrupts.cpp | 4 +-- kernel/src/main.asm | 6 ++++ kernel/src/memory/VMM.cpp | 4 +-- kernel/src/thread/Scheduler.cpp | 3 +- 6 files changed, 64 insertions(+), 12 deletions(-) create mode 100644 kernel/include/utils/Registers.h diff --git a/kernel/include/misc/utils.h b/kernel/include/misc/utils.h index 7bb36f17..c7660111 100644 --- a/kernel/include/misc/utils.h +++ b/kernel/include/misc/utils.h @@ -5,8 +5,6 @@ #define PAGE_SIZE 4096 #endif -extern "C" uint64_t asm_get_rflags(); - namespace Utilities { inline uint64_t get_blocks_from_size(uint64_t blocksize, uint64_t size) @@ -14,11 +12,6 @@ namespace Utilities return (size + (blocksize - 1)) / blocksize; } - inline uint64_t get_rflags() - { - return asm_get_rflags(); - } - inline uint64_t get_top_of_stack(uint64_t bottom, uint64_t stack_pages) { return bottom + (stack_pages * PAGE_SIZE) - sizeof(uintptr_t); diff --git a/kernel/include/utils/Registers.h b/kernel/include/utils/Registers.h new file mode 100644 index 00000000..944a0ea0 --- /dev/null +++ b/kernel/include/utils/Registers.h @@ -0,0 +1,52 @@ +#pragma once + +#include + +extern "C" uintptr_t asm_get_rflags(); +extern "C" void asm_set_rflags(uintptr_t); + +inline uintptr_t read_rflags() +{ + return asm_get_rflags(); +} + +inline void write_rflags(uintptr_t value) +{ + asm_set_rflags(value); +} + +inline uintptr_t read_cr0() +{ + uintptr_t value; + asm volatile("mov %%cr0, %0" : "=r"(value)); + return value; +} + +inline uintptr_t read_cr3() +{ + uintptr_t value; + asm volatile("mov %%cr3, %0" : "=r"(value)); + return value; +} + +inline uintptr_t read_cr4() +{ + uintptr_t value; + asm volatile("mov %%cr4, %0" : "=r"(value)); + return value; +} + +template inline void write_cr0(T value) +{ + asm volatile("mov %0, %%cr0" : : "r"(value)); +} + +template inline void write_cr3(T value) +{ + asm volatile("mov %0, %%cr3" : : "r"(value)); +} + +template inline void write_cr4(T value) +{ + asm volatile("mov %0, %%cr4" : : "r"(value)); +} \ No newline at end of file diff --git a/kernel/src/interrupts/Interrupts.cpp b/kernel/src/interrupts/Interrupts.cpp index 06d40db7..b57420d8 100644 --- a/kernel/src/interrupts/Interrupts.cpp +++ b/kernel/src/interrupts/Interrupts.cpp @@ -1,6 +1,6 @@ #include "interrupts/Interrupts.h" -#include "misc/utils.h" #include "trace/StackTracer.h" +#include "utils/Registers.h" void Interrupts::disable() { @@ -29,7 +29,7 @@ void Interrupts::return_from_handler(Context* context) bool Interrupts::are_enabled() { - return (Utilities::get_rflags() & 0x200) > 0; + return (read_rflags() & 0x200) > 0; } static bool saved_interrupt_state; diff --git a/kernel/src/main.asm b/kernel/src/main.asm index 070821d7..4593a076 100644 --- a/kernel/src/main.asm +++ b/kernel/src/main.asm @@ -31,4 +31,10 @@ global asm_get_rflags asm_get_rflags: pushfq pop rax + ret + +global asm_set_rflags +asm_set_rflags: + push rdi + popfq ret \ No newline at end of file diff --git a/kernel/src/memory/VMM.cpp b/kernel/src/memory/VMM.cpp index 05c3a7ce..e481088d 100644 --- a/kernel/src/memory/VMM.cpp +++ b/kernel/src/memory/VMM.cpp @@ -6,6 +6,7 @@ #include "memory/PMM.h" #include "misc/utils.h" #include "std/string.h" +#include "utils/Registers.h" static PageTable* kernel_pml4; static PageTable* current_pml4; @@ -55,8 +56,7 @@ bool VMM::is_using_kernel_address_space() void VMM::init() { - asm volatile("mov %%cr3, %0" : "=r"(current_pml4)); - kernel_pml4 = current_pml4; + kernel_pml4 = (PageTable*)read_cr3(); } void VMM::unmap(uint64_t vaddr) diff --git a/kernel/src/thread/Scheduler.cpp b/kernel/src/thread/Scheduler.cpp index 20bbf9b9..948c8d1b 100644 --- a/kernel/src/thread/Scheduler.cpp +++ b/kernel/src/thread/Scheduler.cpp @@ -14,6 +14,7 @@ #include "sys/elf/ELFLoader.h" #include "thread/PIT.h" #include "thread/Task.h" +#include "utils/Registers.h" static uint64_t task_num = 0; @@ -70,7 +71,7 @@ void Scheduler::add_kernel_task(void (*task)(void)) new_task->regs.cs = 0x08; new_task->regs.ss = 0x10; new_task->regs.ds = 0x10; - new_task->regs.rflags = Utilities::get_rflags() | 0x200; // enable interrupts + new_task->regs.rflags = read_rflags() | 0x200; // enable interrupts new_task->task_sleep = 0; new_task->task_time = 0; new_task->cpu_time = 0;