Compare commits

...

6 Commits

Author SHA1 Message Date
81f56083c5 Almost there! 2022-10-14 17:38:29 +02:00
cdb73836b0 Some more multiple address space stuff
This page-faults. This is because the memory where the ELF should be is all zeroes, which the CPU tries to interpret.
2022-10-14 17:37:51 +02:00
bb7887a29d Add basic address space infrastructure 2022-10-14 17:34:29 +02:00
177282d79c Use the more appropriate size_t 2022-10-14 17:33:06 +02:00
5abd8814e3 Kernel: Continue moving utilities to a separate subdirectory 2022-10-14 17:33:06 +02:00
e21b608af4 Utilities: Start moving utilities into specific headers in a utils/ subdirectory 2022-10-14 17:31:47 +02:00
9 changed files with 106 additions and 49 deletions

View File

@ -1,37 +1,10 @@
#pragma once
#include <stdint.h>
#ifndef PAGE_SIZE
#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)
{
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);
}
inline uint64_t round_down_to_nearest_page(uint64_t addr)
{
return addr - (addr % PAGE_SIZE);
}
inline uint64_t round_up_to_nearest_page(uint64_t addr)
{
if (addr % PAGE_SIZE) return addr + (PAGE_SIZE - (addr % PAGE_SIZE));
return addr;
}
}

View File

@ -0,0 +1,24 @@
#pragma once
#include <stddef.h>
#include <stdint.h>
#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
#endif
inline uintptr_t get_top_of_stack(uintptr_t bottom, size_t stack_pages)
{
return bottom + (stack_pages * PAGE_SIZE) - sizeof(uintptr_t);
}
inline uintptr_t round_down_to_nearest_page(uintptr_t addr)
{
return addr - (addr % PAGE_SIZE);
}
inline uintptr_t round_up_to_nearest_page(uintptr_t addr)
{
if (addr % PAGE_SIZE) return addr + (PAGE_SIZE - (addr % PAGE_SIZE));
return addr;
}

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

View File

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

View File

@ -87,10 +87,6 @@ extern "C" void _start()
PIC::enable_master(0b11111100); // enable keyboard and PIT
PIC::enable_slave(0b11111111);
kinfoln("Prepared PIC");
Interrupts::enable(); // Task switching commences here
kinfoln("Interrupts enabled");
PCI::scan([](PCI::Device& dev) {

View File

@ -6,6 +6,8 @@
#include "memory/PMM.h"
#include "misc/utils.h"
#include "std/string.h"
#include "utils/Addresses.h"
#include "utils/Registers.h"
static PageTable* kernel_pml4;
static PageTable* current_pml4;
@ -55,13 +57,13 @@ 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();
current_pml4 = kernel_pml4;
}
void VMM::unmap(uint64_t vaddr)
{
vaddr = Utilities::round_down_to_nearest_page(vaddr);
vaddr = round_down_to_nearest_page(vaddr);
PageDirectoryEntry* pde = find_pde(current_pml4, vaddr);
if (!pde) return; // Already unmapped
@ -88,7 +90,7 @@ void VMM::remap(uint64_t vaddr, int flags)
uint64_t VMM::get_physical(uint64_t vaddr)
{
PageDirectoryEntry* pde = find_pde(current_pml4, Utilities::round_down_to_nearest_page(vaddr));
PageDirectoryEntry* pde = find_pde(current_pml4, round_down_to_nearest_page(vaddr));
if (!pde) return UINT64_MAX; // Not mapped
return pde->get_address() | (vaddr % PAGE_SIZE);
@ -96,7 +98,7 @@ uint64_t VMM::get_physical(uint64_t vaddr)
uint64_t VMM::get_flags(uint64_t vaddr)
{
PageDirectoryEntry* pde = find_pde(current_pml4, Utilities::round_down_to_nearest_page(vaddr));
PageDirectoryEntry* pde = find_pde(current_pml4, round_down_to_nearest_page(vaddr));
if (!pde) return 0; // Not mapped
uint64_t flags = 0;
@ -107,7 +109,7 @@ uint64_t VMM::get_flags(uint64_t vaddr)
void VMM::map(uint64_t vaddr, uint64_t paddr, int flags)
{
vaddr = Utilities::round_down_to_nearest_page(vaddr);
vaddr = round_down_to_nearest_page(vaddr);
PageDirectoryEntry* pde = find_pde(current_pml4, vaddr);
bool will_flush_tlb = true;
if (!pde)
@ -121,7 +123,7 @@ void VMM::map(uint64_t vaddr, uint64_t paddr, int flags)
pde = create_pde_if_not_exists(current_pml4, vaddr);
}
pde->set_address(Utilities::round_down_to_nearest_page(paddr));
pde->set_address(round_down_to_nearest_page(paddr));
if (flags & User) propagate_user(current_pml4, vaddr);
else
pde->user = false;

View File

@ -13,6 +13,7 @@
#include "std/stdlib.h"
#include "std/string.h"
#include "sys/elf/ELF.h"
#include "utils/Addresses.h"
static const char* format_permissions(uint32_t flags)
{
@ -90,8 +91,8 @@ ELFImage* ELFLoader::load_elf_from_vfs(VFS::Node* node)
ASSERT(phdr.p_vaddr);
uint64_t pages = Utilities::get_blocks_from_size(PAGE_SIZE, (phdr.p_vaddr % PAGE_SIZE) + phdr.p_memsz);
void* buffer = (void*)((uint64_t)MemoryManager::get_pages_at(
Utilities::round_down_to_nearest_page(phdr.p_vaddr), pages, MAP_READ_WRITE) +
void* buffer = (void*)((uint64_t)MemoryManager::get_pages_at(round_down_to_nearest_page(phdr.p_vaddr),
pages, MAP_READ_WRITE) +
(phdr.p_vaddr % PAGE_SIZE));
VMM::apply_address_space();
@ -193,7 +194,7 @@ void ELFLoader::release_elf_image(ELFImage* image)
{
ELFSection& section = image->sections[i];
kdbgln("Freeing up section %lx, was using %ld pages", section.base, section.pages);
MemoryManager::release_pages((void*)Utilities::round_down_to_nearest_page(section.base), section.pages);
MemoryManager::release_pages((void*)round_down_to_nearest_page(section.base), section.pages);
}
kfree(image);
}

View File

@ -14,6 +14,8 @@
#include "sys/elf/ELFLoader.h"
#include "thread/PIT.h"
#include "thread/Task.h"
#include "utils/Addresses.h"
#include "utils/Registers.h"
static uint64_t task_num = 0;
@ -34,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 = Utilities::get_top_of_stack((uint64_t)MemoryManager::get_page(), 1);
idle_task.regs.rsp = 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);
@ -55,6 +57,7 @@ void Scheduler::init()
// the other registers will be saved next task switch
frequency = 1000 / PIT::frequency();
kdbgln("frequency: %ld", frequency);
}
void Scheduler::add_kernel_task(void (*task)(void))
@ -66,11 +69,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 = Utilities::get_top_of_stack(new_task->allocated_stack, TASK_PAGES_IN_STACK);
new_task->regs.rsp = 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;
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;
@ -95,7 +98,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 = Utilities::get_top_of_stack(new_task->allocated_stack, TASK_PAGES_IN_STACK);
new_task->regs.rsp = 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;
@ -139,7 +142,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 = Utilities::get_top_of_stack(new_task->allocated_stack, TASK_PAGES_IN_STACK);
new_task->regs.rsp = 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;
@ -166,7 +169,7 @@ void Scheduler::reset_task(Task* task, ELFImage* new_image)
task->state = task->Running;
task->regs.rip = new_image->entry;
task->image = new_image;
task->regs.rsp = Utilities::get_top_of_stack(task->allocated_stack, TASK_PAGES_IN_STACK);
task->regs.rsp = get_top_of_stack(task->allocated_stack, TASK_PAGES_IN_STACK);
task->regs.cs = 0x18 | 0x03;
task->regs.ss = 0x20 | 0x03;
task->regs.ds = 0x20 | 0x03;