Utilities: Start moving utilities into specific headers in a utils/ subdirectory

This commit is contained in:
apio 2022-10-14 17:21:16 +02:00
parent 9b3c7816a3
commit e21b608af4
6 changed files with 64 additions and 12 deletions

View File

@ -5,8 +5,6 @@
#define PAGE_SIZE 4096 #define PAGE_SIZE 4096
#endif #endif
extern "C" uint64_t asm_get_rflags();
namespace Utilities namespace Utilities
{ {
inline uint64_t get_blocks_from_size(uint64_t blocksize, uint64_t size) inline uint64_t get_blocks_from_size(uint64_t blocksize, uint64_t size)
@ -14,11 +12,6 @@ namespace Utilities
return (size + (blocksize - 1)) / blocksize; 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) inline uint64_t get_top_of_stack(uint64_t bottom, uint64_t stack_pages)
{ {
return bottom + (stack_pages * PAGE_SIZE) - sizeof(uintptr_t); return bottom + (stack_pages * PAGE_SIZE) - sizeof(uintptr_t);

View File

@ -0,0 +1,52 @@
#pragma once
#include <stdint.h>
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 <typename T> inline void write_cr0(T value)
{
asm volatile("mov %0, %%cr0" : : "r"(value));
}
template <typename T> inline void write_cr3(T value)
{
asm volatile("mov %0, %%cr3" : : "r"(value));
}
template <typename T> inline void write_cr4(T value)
{
asm volatile("mov %0, %%cr4" : : "r"(value));
}

View File

@ -1,6 +1,6 @@
#include "interrupts/Interrupts.h" #include "interrupts/Interrupts.h"
#include "misc/utils.h"
#include "trace/StackTracer.h" #include "trace/StackTracer.h"
#include "utils/Registers.h"
void Interrupts::disable() void Interrupts::disable()
{ {
@ -29,7 +29,7 @@ void Interrupts::return_from_handler(Context* context)
bool Interrupts::are_enabled() bool Interrupts::are_enabled()
{ {
return (Utilities::get_rflags() & 0x200) > 0; return (read_rflags() & 0x200) > 0;
} }
static bool saved_interrupt_state; static bool saved_interrupt_state;

View File

@ -31,4 +31,10 @@ global asm_get_rflags
asm_get_rflags: asm_get_rflags:
pushfq pushfq
pop rax pop rax
ret
global asm_set_rflags
asm_set_rflags:
push rdi
popfq
ret ret

View File

@ -6,6 +6,7 @@
#include "memory/PMM.h" #include "memory/PMM.h"
#include "misc/utils.h" #include "misc/utils.h"
#include "std/string.h" #include "std/string.h"
#include "utils/Registers.h"
static PageTable* kernel_pml4; static PageTable* kernel_pml4;
static PageTable* current_pml4; static PageTable* current_pml4;
@ -55,8 +56,7 @@ bool VMM::is_using_kernel_address_space()
void VMM::init() void VMM::init()
{ {
asm volatile("mov %%cr3, %0" : "=r"(current_pml4)); kernel_pml4 = (PageTable*)read_cr3();
kernel_pml4 = current_pml4;
} }
void VMM::unmap(uint64_t vaddr) void VMM::unmap(uint64_t vaddr)

View File

@ -14,6 +14,7 @@
#include "sys/elf/ELFLoader.h" #include "sys/elf/ELFLoader.h"
#include "thread/PIT.h" #include "thread/PIT.h"
#include "thread/Task.h" #include "thread/Task.h"
#include "utils/Registers.h"
static uint64_t task_num = 0; 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.cs = 0x08;
new_task->regs.ss = 0x10; new_task->regs.ss = 0x10;
new_task->regs.ds = 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_sleep = 0;
new_task->task_time = 0; new_task->task_time = 0;
new_task->cpu_time = 0; new_task->cpu_time = 0;