forked from CHatingPython/towos
126 lines
3.3 KiB
C++
126 lines
3.3 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 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 {
|
|
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;
|
|
|
|
static constexpr virtaddr_t HHDM_BASE = 0xFFFF800000000000ULL;
|
|
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();
|
|
|
|
virtaddr_t reserve(u64 size);
|
|
bool commit(virtaddr_t addr, u64 size);
|
|
bool decommit(virtaddr_t addr, u64 size);
|
|
void release(virtaddr_t addr, u64 size);
|
|
private:
|
|
struct range {
|
|
range* prev;
|
|
range* next;
|
|
|
|
u64 begin;
|
|
u64 size;
|
|
u64 committedPages;
|
|
};
|
|
private:
|
|
range* m_freeRanges;
|
|
};*/
|
|
|
|
} // namespace x86_64
|
|
|
|
#endif // ARCH_X86_64_MEMORY_HPP
|