Compare commits

...

7 Commits

Author SHA1 Message Date
8ff9cb4b96
x86_64: Add a friendlier handler for page faults
All checks were successful
continuous-integration/drone/push Build is passing
2022-12-05 21:02:21 +01:00
1d0dd8fa93
Use KernelVM in kmalloc() and friends
Much better now!!
2022-12-05 21:02:05 +01:00
ba758bcef8
Initialize the KernelVM 2022-12-05 21:01:18 +01:00
6c3024d4ee
Heap: Count the heap blocks' size in the size required for an allocation 2022-12-05 21:01:06 +01:00
0edabd6d87
Heap: Add a new block to the end of the linked list
*facepalm*

This was causing page faults when having two blocks and the first one (oldest one) being freed first.
2022-12-05 21:00:21 +01:00
d445b29477
Add a virtual memory allocator for the kernel 2022-12-05 20:36:24 +01:00
a8fb4ca739
Move CI badge 2022-12-05 19:57:41 +01:00
7 changed files with 143 additions and 9 deletions

View File

@ -1,5 +1,5 @@
# Luna # Luna
A simple kernel and userspace for the x86_64 platform, written mostly in C++ and C. (rewrite, listed features are currently mostly from the old version) [![Build Status](https://drone.cloudapio.eu/api/badges/apio/Luna/status.svg?ref=refs/heads/restart)](https://drone.cloudapio.eu/apio/Luna) A simple kernel and userspace for the x86_64 platform, written mostly in C++ and C. [![Build Status](https://drone.cloudapio.eu/api/badges/apio/Luna/status.svg?ref=refs/heads/restart)](https://drone.cloudapio.eu/apio/Luna) (rewrite, listed features are currently mostly from the old version)
## Features ## Features
- x86_64-compatible [kernel](kernel/). - x86_64-compatible [kernel](kernel/).

View File

@ -7,6 +7,7 @@ set(SOURCES
src/video/TextConsole.cpp src/video/TextConsole.cpp
src/memory/MemoryManager.cpp src/memory/MemoryManager.cpp
src/memory/Heap.cpp src/memory/Heap.cpp
src/memory/KernelVM.cpp
src/memory/MemoryMap.cpp src/memory/MemoryMap.cpp
src/boot/Init.cpp src/boot/Init.cpp
src/arch/Serial.cpp src/arch/Serial.cpp

View File

@ -293,7 +293,15 @@ static void setup_idt()
kerrorln("FIXME(interrupt): %s", name); \ kerrorln("FIXME(interrupt): %s", name); \
CPU::efficient_halt(); CPU::efficient_halt();
extern "C" void handle_x86_exception([[maybe_unused]] Registers* regs) [[noreturn]] void handle_page_fault(Registers* regs)
{
u64 cr2;
asm volatile("mov %%cr2, %0" : "=r"(cr2));
kerrorln("Page fault at RIP %lx while accessing %lx!", regs->rip, cr2);
CPU::efficient_halt();
}
extern "C" void handle_x86_exception(Registers* regs)
{ {
switch (regs->isr) switch (regs->isr)
{ {
@ -309,7 +317,7 @@ extern "C" void handle_x86_exception([[maybe_unused]] Registers* regs)
case 11: FIXME_UNHANDLED_INTERRUPT("Segment not present"); case 11: FIXME_UNHANDLED_INTERRUPT("Segment not present");
case 12: FIXME_UNHANDLED_INTERRUPT("Stack-segment fault"); case 12: FIXME_UNHANDLED_INTERRUPT("Stack-segment fault");
case 13: FIXME_UNHANDLED_INTERRUPT("General protection fault"); case 13: FIXME_UNHANDLED_INTERRUPT("General protection fault");
case 14: FIXME_UNHANDLED_INTERRUPT("Page fault"); case 14: handle_page_fault(regs);
case 16: FIXME_UNHANDLED_INTERRUPT("x87 floating-point exception"); case 16: FIXME_UNHANDLED_INTERRUPT("x87 floating-point exception");
case 17: FIXME_UNHANDLED_INTERRUPT("Alignment check"); case 17: FIXME_UNHANDLED_INTERRUPT("Alignment check");
case 19: FIXME_UNHANDLED_INTERRUPT("SIMD floating-point exception"); case 19: FIXME_UNHANDLED_INTERRUPT("SIMD floating-point exception");

View File

@ -2,6 +2,7 @@
#include "Log.h" #include "Log.h"
#include "arch/MMU.h" #include "arch/MMU.h"
#include "arch/Serial.h" #include "arch/Serial.h"
#include "memory/KernelVM.h"
#include "memory/MemoryManager.h" #include "memory/MemoryManager.h"
#include <luna/Alignment.h> #include <luna/Alignment.h>
#include <luna/String.h> #include <luna/String.h>
@ -31,20 +32,19 @@ static_assert(sizeof(HeapBlock) == 48UL);
static HeapBlock* heap_start = nullptr; static HeapBlock* heap_start = nullptr;
static HeapBlock* heap_end = nullptr; static HeapBlock* heap_end = nullptr;
static usize start_addr = 0xffffffff80000000;
static Result<HeapBlock*> allocate_pages( static Result<HeapBlock*> allocate_pages(
usize count) // FIXME: Keep track of virtual address space usage. For now, since the address usize count) // FIXME: Keep track of virtual address space usage. For now, since the address
// space is so huge, we can just start at a fairly large address and assume // space is so huge, we can just start at a fairly large address and assume
// we'll never run into anything, but this will probably bite us in the future. // we'll never run into anything, but this will probably bite us in the future.
{ {
void* const ptr = (void*)TRY(MemoryManager::alloc_at(start_addr, count, MMU::ReadWrite | MMU::NoExecute)); u64 virt = TRY(KernelVM::alloc_several_pages(count));
if (ptr) start_addr += (count * ARCH_PAGE_SIZE); void* const ptr = (void*)TRY(MemoryManager::alloc_at(virt, count, MMU::ReadWrite | MMU::NoExecute));
return (HeapBlock*)ptr; return (HeapBlock*)ptr;
} }
static Result<void> release_pages(void* ptr, usize count) static Result<void> release_pages(void* ptr, usize count)
{ {
TRY(KernelVM::free_several_pages((u64)ptr, count));
return MemoryManager::unmap_owned((u64)ptr, count); return MemoryManager::unmap_owned((u64)ptr, count);
} }
@ -176,7 +176,7 @@ Result<void*> kmalloc(usize size)
if (!heap_start) if (!heap_start)
{ {
const usize pages = get_pages_for_allocation(size); const usize pages = get_pages_for_allocation(size + sizeof(HeapBlock));
HeapBlock* const block = TRY(allocate_pages(pages)); HeapBlock* const block = TRY(allocate_pages(pages));
block->full_size = (pages * ARCH_PAGE_SIZE) - sizeof(HeapBlock); block->full_size = (pages * ARCH_PAGE_SIZE) - sizeof(HeapBlock);
@ -214,7 +214,7 @@ Result<void*> kmalloc(usize size)
if (!block) // No free blocks, let's allocate a new one if (!block) // No free blocks, let's allocate a new one
{ {
usize pages = get_pages_for_allocation(size); usize pages = get_pages_for_allocation(size + sizeof(HeapBlock));
block = TRY(allocate_pages(pages)); block = TRY(allocate_pages(pages));
block->full_size = (pages * ARCH_PAGE_SIZE) - sizeof(HeapBlock); block->full_size = (pages * ARCH_PAGE_SIZE) - sizeof(HeapBlock);
@ -222,6 +222,7 @@ Result<void*> kmalloc(usize size)
block->status = BLOCK_START_MEM | BLOCK_END_MEM; block->status = BLOCK_START_MEM | BLOCK_END_MEM;
block->next = nullptr; block->next = nullptr;
block->last = heap_end; block->last = heap_end;
heap_end->next = block;
heap_end = block; heap_end = block;
} }

View File

@ -0,0 +1,104 @@
#include "memory/KernelVM.h"
#include "arch/MMU.h"
#include <luna/Bitmap.h>
static const u64 KERNEL_VM_RANGE_START = 0xfffffffff0000000;
static Bitmap g_kernelvm_bitmap;
static u8 bitmap_memory[4096];
static const usize KERNEL_VM_RANGE_SIZE = (sizeof(bitmap_memory) * 8) * ARCH_PAGE_SIZE;
static const u64 KERNEL_VM_RANGE_END = KERNEL_VM_RANGE_SIZE + KERNEL_VM_RANGE_START;
static_assert(KERNEL_VM_RANGE_END == 0xfffffffff8000000);
static usize g_used_vm;
namespace KernelVM
{
void init()
{
g_kernelvm_bitmap.initialize(bitmap_memory, sizeof(bitmap_memory));
}
Result<u64> alloc_one_page()
{
for (u64 index = 0; index < g_kernelvm_bitmap.size(); index++)
{
if (g_kernelvm_bitmap.get(index)) continue;
g_kernelvm_bitmap.set(index, true);
g_used_vm += ARCH_PAGE_SIZE;
return KERNEL_VM_RANGE_START + (index * ARCH_PAGE_SIZE);
}
return err(ENOMEM);
}
Result<u64> alloc_several_pages(usize count)
{
u64 first_free_index = 0;
u64 free_contiguous_pages = 0;
for (u64 index = 0; index < g_kernelvm_bitmap.size(); index++)
{
if (g_kernelvm_bitmap.get(index))
{
free_contiguous_pages = 0;
continue;
}
// At this point, we have a free page.
if (!free_contiguous_pages) first_free_index = index;
free_contiguous_pages++;
// Found enough contiguous free pages!!
if (free_contiguous_pages == count)
{
g_used_vm += ARCH_PAGE_SIZE * count;
g_kernelvm_bitmap.clear_region(first_free_index, count, true);
return KERNEL_VM_RANGE_START + (first_free_index * ARCH_PAGE_SIZE);
}
}
return err(ENOMEM);
}
Result<void> free_one_page(u64 address)
{
if (address < KERNEL_VM_RANGE_START) return err(EFAULT);
u64 index = (address - KERNEL_VM_RANGE_START) / ARCH_PAGE_SIZE;
if (index >= g_kernelvm_bitmap.size()) return err(EFAULT);
g_kernelvm_bitmap.set(index, false);
g_used_vm -= ARCH_PAGE_SIZE;
return {};
}
Result<void> free_several_pages(u64 address, usize count)
{
if (address < KERNEL_VM_RANGE_START) return err(EFAULT);
if (address + (count * ARCH_PAGE_SIZE) >= KERNEL_VM_RANGE_END) return err(EFAULT);
u64 index = (address - KERNEL_VM_RANGE_START) / ARCH_PAGE_SIZE;
g_kernelvm_bitmap.clear_region(index, count, false);
g_used_vm -= ARCH_PAGE_SIZE * count;
return {};
}
usize used()
{
return g_used_vm;
}
usize free()
{
return KERNEL_VM_RANGE_SIZE - g_used_vm;
}
}

View File

@ -0,0 +1,18 @@
#pragma once
#include <luna/Result.h>
#include <luna/Types.h>
// Simple bitmap allocator which hands out kernel-space virtual addresses for use in kmalloc() and friends.
namespace KernelVM
{
void init();
Result<u64> alloc_one_page();
Result<u64> alloc_several_pages(usize count);
Result<void> free_one_page(u64 address);
Result<void> free_several_pages(u64 address, usize count);
usize free();
usize used();
}

View File

@ -2,6 +2,7 @@
#include "Log.h" #include "Log.h"
#include "arch/CPU.h" #include "arch/CPU.h"
#include "arch/MMU.h" #include "arch/MMU.h"
#include "memory/KernelVM.h"
#include "memory/MemoryMap.h" #include "memory/MemoryMap.h"
#include <luna/Alignment.h> #include <luna/Alignment.h>
#include <luna/Bitmap.h> #include <luna/Bitmap.h>
@ -96,6 +97,7 @@ namespace MemoryManager
void init() void init()
{ {
init_physical_frame_allocator(); init_physical_frame_allocator();
KernelVM::init();
MMU::setup_initial_page_directory(); MMU::setup_initial_page_directory();
} }