feat: start adding physical memory allocator

This commit is contained in:
Katharina 2026-06-28 16:19:21 +02:00
parent 8dc53f662e
commit e2ea22ad53
15 changed files with 440 additions and 38 deletions

View file

@ -0,0 +1,37 @@
#pragma once
#include "memory/pointer.h"
#include "process/process.h"
#include "util/optional.h"
#include "init/multiboot2.h"
constexpr const size_t page_size = 4096;
struct PhysicalAllocator {
public:
struct allocation_t {
paddr_t ptr;
size_t page_count;
};
struct entry_t {
uint64_t page_count;
pid_t owner;
};
static void init(const multiboot::tag_mmap* multiboot_info);
static PhysicalAllocator& getInstance();
allocation_t allocPages(size_t page_count, pid_t owner);
void free(allocation_t ptr);
void force(allocation_t ptr, pid_t owner);
entry_t getOwner(paddr_t from);
private:
struct Page {
optional<Page*> next;
entry_t entries[255];
void forceEntry(entry_t entry);
void cleanup();
void compact();
};
static_assert(sizeof(Page) == page_size);
Page* storage;
};

View file

@ -7,13 +7,12 @@ constexpr uint64_t high_base = 0xFFFF800000000000;
#define read_symbol(symbol) [](){uint64_t res; asm volatile("movabsq $" symbol ", %0": "=r"(res)); return res;}()
struct paddr_t {
uint64_t address;
template<typename T>
T* access() {
return reinterpret_cast<T*>(address+high_base);
}
uint64_t address;
template<typename T>
T* access() {
return reinterpret_cast<T*>(address+high_base);
}
};
inline void* operator new(size_t, void* p) {

View file

@ -17,3 +17,14 @@ void for_each_reserved_section(Fn&& fn) {
for (auto* s = __reserved_ranges_start__; s < __reserved_ranges_end__; ++s)
fn(*s);
}
inline bool is_in_reserved_section(paddr_t ptr, size_t size = 1) {
bool res = false;
for_each_reserved_section([&](const KernelSection& section) {
if (ptr.address < section.phys_end().address &&
section.phys_start().address < ptr.address + size) {
res = true;
}
});
return res;
}