Compare commits

..

No commits in common. "8ff9cb4b96c682650212c2bfe664e1f89b4525cc" and "eaf8a8aafe6c537f41f7effda1e172b25244fb13" have entirely different histories.

7 changed files with 9 additions and 143 deletions

View File

@ -1,5 +1,5 @@
# 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)
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)
## Features
- x86_64-compatible [kernel](kernel/).

View File

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

View File

@ -293,15 +293,7 @@ static void setup_idt()
kerrorln("FIXME(interrupt): %s", name); \
CPU::efficient_halt();
[[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)
extern "C" void handle_x86_exception([[maybe_unused]] Registers* regs)
{
switch (regs->isr)
{
@ -317,7 +309,7 @@ extern "C" void handle_x86_exception(Registers* regs)
case 11: FIXME_UNHANDLED_INTERRUPT("Segment not present");
case 12: FIXME_UNHANDLED_INTERRUPT("Stack-segment fault");
case 13: FIXME_UNHANDLED_INTERRUPT("General protection fault");
case 14: handle_page_fault(regs);
case 14: FIXME_UNHANDLED_INTERRUPT("Page fault");
case 16: FIXME_UNHANDLED_INTERRUPT("x87 floating-point exception");
case 17: FIXME_UNHANDLED_INTERRUPT("Alignment check");
case 19: FIXME_UNHANDLED_INTERRUPT("SIMD floating-point exception");

View File

@ -2,7 +2,6 @@
#include "Log.h"
#include "arch/MMU.h"
#include "arch/Serial.h"
#include "memory/KernelVM.h"
#include "memory/MemoryManager.h"
#include <luna/Alignment.h>
#include <luna/String.h>
@ -32,19 +31,20 @@ static_assert(sizeof(HeapBlock) == 48UL);
static HeapBlock* heap_start = nullptr;
static HeapBlock* heap_end = nullptr;
static usize start_addr = 0xffffffff80000000;
static Result<HeapBlock*> allocate_pages(
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
// we'll never run into anything, but this will probably bite us in the future.
{
u64 virt = TRY(KernelVM::alloc_several_pages(count));
void* const ptr = (void*)TRY(MemoryManager::alloc_at(virt, count, MMU::ReadWrite | MMU::NoExecute));
void* const ptr = (void*)TRY(MemoryManager::alloc_at(start_addr, count, MMU::ReadWrite | MMU::NoExecute));
if (ptr) start_addr += (count * ARCH_PAGE_SIZE);
return (HeapBlock*)ptr;
}
static Result<void> release_pages(void* ptr, usize count)
{
TRY(KernelVM::free_several_pages((u64)ptr, count));
return MemoryManager::unmap_owned((u64)ptr, count);
}
@ -176,7 +176,7 @@ Result<void*> kmalloc(usize size)
if (!heap_start)
{
const usize pages = get_pages_for_allocation(size + sizeof(HeapBlock));
const usize pages = get_pages_for_allocation(size);
HeapBlock* const block = TRY(allocate_pages(pages));
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
{
usize pages = get_pages_for_allocation(size + sizeof(HeapBlock));
usize pages = get_pages_for_allocation(size);
block = TRY(allocate_pages(pages));
block->full_size = (pages * ARCH_PAGE_SIZE) - sizeof(HeapBlock);
@ -222,7 +222,6 @@ Result<void*> kmalloc(usize size)
block->status = BLOCK_START_MEM | BLOCK_END_MEM;
block->next = nullptr;
block->last = heap_end;
heap_end->next = block;
heap_end = block;
}

View File

@ -1,104 +0,0 @@
#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

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