Files
towos/kernel/arch/x86_64/memory.hpp

231 lines
6.5 KiB
C++

#ifndef ARCH_X86_64_MEMORY_HPP
#define ARCH_X86_64_MEMORY_HPP
#include "mmap.hpp"
#include "types.hpp"
namespace x86_64 {
using virtaddr_t = u64;
using physaddr_t = u64;
static constexpr virtaddr_t NULLADDR = 0;
static constexpr physaddr_t INVALID_PHYSADDR = 0xFFFFFFFFFFFFFFFFULL;
// Physical Memory Manager
struct pmm {
pmm();
physaddr_t alloc_page();
void free_page(physaddr_t addr);
physaddr_t alloc_page_bootstrap();
const mmap& get_mmap() const { return m_mmap; }
void relocate_bitmap(u64 hhdmBase) { m_bitmap = reinterpret_cast<u64*>(m_bitmapPaddr + hhdmBase); }
private:
static constexpr u64 PAGE_SIZE = 0x1000;
private:
void parse_multiboot();
private:
void mark_used(physaddr_t addr);
void mark_free(physaddr_t addr);
void mark_range_used(physaddr_t addr, u64 length);
void mark_range_free(physaddr_t addr, u64 length);
bool is_free(u64 frame);
void set_used(u64 frame);
void set_free(u64 frame);
void init_bitmap(u64 maxPhys);
physaddr_t find_bitmap_location(u64 bytes);
private:
mmap m_mmap{};
physaddr_t m_bitmapPaddr = INVALID_PHYSADDR;
u64* m_bitmap = nullptr;
u64 m_frameCount = 0;
u64 m_bitmapWords = 0;
u64 m_bootstrapHint = 1;
u64 m_hint = 1;
};
struct paging {
static constexpr virtaddr_t HHDM_BASE = 0xFFFF800000000000ULL;
friend void map_range(paging& paging, const char begin[], const char end[], u64 flags);
enum page_flags {
PAGE_PRESENT = (1 << 0),
PAGE_WRITABLE = (1 << 1),
PAGE_USER = (1 << 2),
PAGE_WRITETHROUGH = (1 << 3),
PAGE_CACHEDISABLED = (1 << 4),
PAGE_ACCESSED = (1 << 5),
PAGE_DIRTY = (1 << 6),
PAGE_LARGE = (1 << 7),
PAGE_GLOBAL = (1 << 8),
PAGE_XD = (1ULL << 63),
PAGE_FLAGS_MASK = PAGE_PRESENT | PAGE_WRITABLE | PAGE_USER | PAGE_WRITETHROUGH | PAGE_CACHEDISABLED |
PAGE_ACCESSED | PAGE_DIRTY | PAGE_LARGE | PAGE_GLOBAL | PAGE_XD,
};
public:
paging(pmm& pmm);
physaddr_t get(virtaddr_t virtualAddr);
void map(physaddr_t physicalAddr, virtaddr_t virtualAddr, u64 flags);
physaddr_t unmap(virtaddr_t virtualAddr);
private:
static constexpr physaddr_t PHYS_MASK = 0x000FFFFFFFFFF000ULL;
static constexpr virtaddr_t ENTRY_INDEX_MASK = 0x1FFULL;
private:
static physaddr_t virt_to_phys(virtaddr_t addr) { return addr & ~HHDM_BASE; }
static void* phys_to_virt(physaddr_t addr) { return reinterpret_cast<void*>(HHDM_BASE + addr); }
private:
void map2m(physaddr_t physicalAddr, virtaddr_t virtualAddr, u64 flags);
void map_hhdm(physaddr_t begin, physaddr_t end);
void allocate_table(u64& entry);
void flush_tlb(u64 addr);
private:
pmm* m_pmm;
physaddr_t m_pml4Phys;
u64* m_pml4;
};
// Virtual Memory
struct vm {
vm(pmm& pmm);
virtaddr_t reserve(u64 size);
bool commit(virtaddr_t addr, u64 size, u64 flags);
bool uncommit(virtaddr_t addr, u64 size);
bool release(virtaddr_t addr, u64 size);
template <typename T>
T* allocate(u64 count = 1) {
virtaddr_t addr = reserve(sizeof(T) * count);
if (addr == 0) return nullptr;
if (!commit(addr, sizeof(T) * count, paging::PAGE_XD | paging::PAGE_WRITABLE)) {
release(addr, sizeof(T) * count);
return nullptr;
}
return reinterpret_cast<T*>(addr);
}
template <typename T>
void free(T* ptr, u64 count) {
virtaddr_t addr = reinterpret_cast<virtaddr_t>(ptr);
if (!uncommit(addr, sizeof(T) * count)) return;
release(addr, sizeof(T) * count);
}
private:
static constexpr u64 INIT_START = 0xFFFF900000000000ULL;
static constexpr u64 INIT_END = 0xFFFFA00000000000ULL;
private:
struct node {
enum color_e {
RED = 0,
BLACK = 1,
};
// NOTE: The parent node is used for the last free node for first free node.
node* parent = nullptr;
node* left = nullptr;
// NOTE: The right node is used for the next free node in free nodes list.
node* right = nullptr;
u64 base = 0;
u64 size = 0;
u64 maxSize = 0;
color_e color = BLACK;
bool is_red() const { return color == RED; }
void make_red() { color = RED; }
void make_black() { color = BLACK; }
static u64 max_size(node* node) { return (node != nullptr) ? node->maxSize : 0; }
void recalculate() {
maxSize = max_size(left);
if (max_size(right) > maxSize) maxSize = max_size(right);
if (size > maxSize) maxSize = size;
}
void recalculate_upwards() {
node* node = this;
while (node != nullptr) {
node->recalculate();
node = node->parent;
}
}
node* min() {
node* node = this;
while (node != nullptr && node->left != nullptr)
node = node->left;
return node;
}
node* max() {
node* node = this;
while (node != nullptr && node->right != nullptr)
node = node->right;
return node;
}
node* pred() {
if (left != nullptr) return left->max();
node* parent = this->parent;
node* node = this;
while (parent != nullptr && node == parent->left) {
node = parent;
parent = parent->parent;
}
return parent;
}
};
struct page {
static constexpr u64 ENTRY_COUNT = 0x1000 / sizeof(node);
node nodes[ENTRY_COUNT];
};
private:
void insert(node* node);
void insert_fixup(node* node);
void transplant(node* first, node* second);
void remove(node* node);
void remove_fixup(node* first, node* second);
node* lower_bound(u64 base);
node* find_first_fit(u64 size);
void rotate_left(node* node);
void rotate_right(node* node);
private:
node* allocate_node();
void free_node(node* node);
// NOTE: This function allocates a page full of free nodes and returns a pointer to the first one (that is not
// tracked in the free nodes linked list).
node* allocate_page();
private:
pmm* m_pmm = nullptr;
node* m_firstFree = nullptr;
node* m_root = nullptr;
};
} // namespace x86_64
#endif // ARCH_X86_64_MEMORY_HPP