Files
towos/kernel/arch/x86_64/paging.cpp
T
2026-10-06 16:24:44 +02:00

253 lines
8.7 KiB
C++

#include "./memory.hpp"
#include "mmap.hpp"
#include "vga.hpp"
namespace x86_64 {
extern "C" char __boot_stack_start[]; // NOLINT
extern "C" char __boot_stack_end[]; // NOLINT
extern "C" char __kernel_text_start[]; // NOLINT
extern "C" char __kernel_text_end[]; // NOLINT
extern "C" char __kernel_rodata_start[]; // NOLINT
extern "C" char __kernel_rodata_end[]; // NOLINT
extern "C" char __kernel_data_start[]; // NOLINT
extern "C" char __kernel_data_end[]; // NOLINT
extern "C" char __kernel_bss_start[]; // NOLINT
extern "C" char __kernel_bss_end[]; // NOLINT
extern "C" char __kernel_phys_text_start[]; // NOLINT
extern "C" char __kernel_phys_rodata_start[]; // NOLINT
extern "C" char __kernel_phys_data_start[]; // NOLINT
extern "C" char __kernel_phys_bss_start[]; // NOLINT
static constexpr u64 PAGE_2M = 0x200000ULL;
static constexpr u64 BOOTSTRAP_HHDM_LIMIT = 0x40000000ULL;
void map_range(paging& paging, const char* virtualBegin, const char* virtualEnd, physaddr_t physicalBegin, u64 flags) {
static constexpr u64 PAGE_MASK = ~0xFFFULL;
virtaddr_t virtualAddr = reinterpret_cast<virtaddr_t>(virtualBegin) & PAGE_MASK;
const virtaddr_t endAddr = (reinterpret_cast<virtaddr_t>(virtualEnd) + 0xFFF) & PAGE_MASK;
physicalBegin &= PAGE_MASK;
for (; virtualAddr < endAddr; virtualAddr += 0x1000, physicalBegin += 0x1000)
paging.map(physicalBegin, virtualAddr, flags);
}
void panic() {
vga::puts("I panicked nya~", 0, 0);
asm volatile("cli; hlt");
}
paging::paging(pmm& pmm)
: m_pmm(&pmm), m_pml4Phys(pmm.alloc_page_bootstrap()), m_pml4(reinterpret_cast<u64*>(m_pml4Phys)) {
for (u64 i = 0; i < 512; ++i)
m_pml4[i] = 0;
for (auto *entry = pmm.get_mmap().entries, *end = entry + pmm.get_mmap().entryCount; entry != end; ++entry) {
if (entry->type != mmap::entry::AVAILABLE) continue;
const physaddr_t begin = entry->base;
const physaddr_t entryEnd = entry->base + entry->size;
if (begin >= BOOTSTRAP_HHDM_LIMIT) continue;
map_hhdm(begin, entryEnd < BOOTSTRAP_HHDM_LIMIT ? entryEnd : BOOTSTRAP_HHDM_LIMIT);
}
for (physaddr_t physical = reinterpret_cast<physaddr_t>(__boot_stack_start),
end = reinterpret_cast<physaddr_t>(__boot_stack_end);
physical < end;
physical += 0x1000) {
map(physical, physical, PAGE_WRITABLE | PAGE_XD);
}
map_range(*this,
__kernel_rodata_start,
__kernel_rodata_end,
reinterpret_cast<physaddr_t>(__kernel_phys_rodata_start),
PAGE_GLOBAL | PAGE_XD);
map_range(*this,
__kernel_data_start,
__kernel_data_end,
reinterpret_cast<physaddr_t>(__kernel_phys_data_start),
PAGE_GLOBAL | PAGE_WRITABLE | PAGE_XD);
map_range(*this,
__kernel_bss_start,
__kernel_bss_end,
reinterpret_cast<physaddr_t>(__kernel_phys_bss_start),
PAGE_GLOBAL | PAGE_WRITABLE | PAGE_XD);
map(0xB8000, 0xB8000, PAGE_WRITABLE | PAGE_XD);
map(m_pml4Phys, m_pml4Phys, PAGE_WRITABLE);
map_range(*this,
__kernel_text_start,
__kernel_text_end,
reinterpret_cast<physaddr_t>(__kernel_phys_text_start),
PAGE_GLOBAL);
asm volatile("mov %0, %%cr3" : : "r"(m_pml4Phys) : "memory");
m_pml4 = reinterpret_cast<u64*>(m_pml4Phys + HHDM_BASE);
pmm.relocate_bitmap(HHDM_BASE);
// TODO: Map the rest of RAM
}
physaddr_t paging::get(virtaddr_t virtualAddr) {
u64 addrInt = reinterpret_cast<u64>(virtualAddr);
u64 pml4Idx = (addrInt >> 39) & ENTRY_INDEX_MASK;
u64 pdptIdx = (addrInt >> 30) & ENTRY_INDEX_MASK;
u64 pdIdx = (addrInt >> 21) & ENTRY_INDEX_MASK;
u64 ptIdx = (addrInt >> 12) & ENTRY_INDEX_MASK;
u64 pml4e = m_pml4[pml4Idx];
if ((pml4e & 0x01) == 0) return INVALID_PHYSADDR;
u64 pdpte = reinterpret_cast<u64*>(pml4e & PHYS_MASK)[pdptIdx];
if ((pdpte & 0x01) == 0) return INVALID_PHYSADDR;
u64 pde = reinterpret_cast<u64*>(pdpte & PHYS_MASK)[pdIdx];
if ((pde & 0x01) == 0) return INVALID_PHYSADDR;
u64 pte = reinterpret_cast<u64*>(pde & PHYS_MASK)[ptIdx];
if ((pte & 0x01) == 0) return INVALID_PHYSADDR;
return (pte & PHYS_MASK) | (addrInt & ((1 << 12) - 1));
}
void paging::map(physaddr_t physicalAddr, virtaddr_t virtualAddr, u64 flags) {
const u64 addrInt = reinterpret_cast<u64>(virtualAddr);
const u64 pml4Idx = (addrInt >> 39) & ENTRY_INDEX_MASK;
const u64 pdptIdx = (addrInt >> 30) & ENTRY_INDEX_MASK;
const u64 pdIdx = (addrInt >> 21) & ENTRY_INDEX_MASK;
const u64 ptIdx = (addrInt >> 12) & ENTRY_INDEX_MASK;
const bool bootstrap = reinterpret_cast<virtaddr_t>(m_pml4) < HHDM_BASE;
auto tableFromPhys = [bootstrap](physaddr_t physical) -> u64* {
return bootstrap ? reinterpret_cast<u64*>(physical) : reinterpret_cast<u64*>(HHDM_BASE + physical);
};
u64& pml4e = m_pml4[pml4Idx];
if ((pml4e & PAGE_PRESENT) == 0) allocate_table(pml4e);
u64* pdpt = tableFromPhys(pml4e & PHYS_MASK);
u64& pdpte = pdpt[pdptIdx];
if ((pdpte & PAGE_PRESENT) == 0) allocate_table(pdpte);
u64* pd = tableFromPhys(pdpte & PHYS_MASK);
u64& pde = pd[pdIdx];
if ((pde & PAGE_PRESENT) == 0) allocate_table(pde);
if ((pde & PAGE_LARGE) == PAGE_LARGE) panic();
u64* pt = tableFromPhys(pde & PHYS_MASK);
u64& pte = pt[ptIdx];
const bool flushNeeded = (pte & PAGE_PRESENT) == PAGE_PRESENT;
pte = (physicalAddr & PHYS_MASK) | (flags & PAGE_FLAGS_MASK) | PAGE_PRESENT;
if (flushNeeded) flush_tlb(addrInt & ~0xFFFULL);
}
physaddr_t paging::unmap(virtaddr_t virtualAddr) {
u64 addrInt = reinterpret_cast<u64>(virtualAddr);
u64 pml4Idx = (addrInt >> 39) & ENTRY_INDEX_MASK;
u64 pdptIdx = (addrInt >> 30) & ENTRY_INDEX_MASK;
u64 pdIdx = (addrInt >> 21) & ENTRY_INDEX_MASK;
u64 ptIdx = (addrInt >> 12) & ENTRY_INDEX_MASK;
u64& pml4e = m_pml4[pml4Idx];
if ((pml4e & 0x01) == 0) return INVALID_PHYSADDR;
u64& pdpte = reinterpret_cast<u64*>(phys_to_virt(pml4e & PHYS_MASK))[pdptIdx];
if ((pdpte & 0x01) == 0) return INVALID_PHYSADDR;
u64& pde = reinterpret_cast<u64*>(phys_to_virt(pdpte & PHYS_MASK))[pdIdx];
if ((pde & 0x01) == 0) return INVALID_PHYSADDR;
u64& pte = reinterpret_cast<u64*>(phys_to_virt(pde & PHYS_MASK))[ptIdx];
if ((pte & 0x01) == 0) return INVALID_PHYSADDR;
physaddr_t oldAddr = pte & PHYS_MASK;
pte = 0;
flush_tlb(addrInt & ~0xFFFULL);
return oldAddr;
}
void paging::map2m(physaddr_t physicalAddr, virtaddr_t virtualAddr, u64 flags) {
const u64 addrInt = reinterpret_cast<u64>(virtualAddr);
if ((physicalAddr & (PAGE_2M - 1)) != 0 || (addrInt & (PAGE_2M - 1)) != 0) {
panic();
}
const u64 pml4Idx = (addrInt >> 39) & ENTRY_INDEX_MASK;
const u64 pdptIdx = (addrInt >> 30) & ENTRY_INDEX_MASK;
const u64 pdIdx = (addrInt >> 21) & ENTRY_INDEX_MASK;
const bool bootstrap = reinterpret_cast<virtaddr_t>(m_pml4) < HHDM_BASE;
auto tableFromPhys = [bootstrap](physaddr_t physical) -> u64* {
return bootstrap ? reinterpret_cast<u64*>(physical) : reinterpret_cast<u64*>(HHDM_BASE + physical);
};
u64& pml4e = m_pml4[pml4Idx];
if ((pml4e & PAGE_PRESENT) == 0) allocate_table(pml4e);
u64* pdpt = tableFromPhys(pml4e & PHYS_MASK);
u64& pdpte = pdpt[pdptIdx];
if ((pdpte & PAGE_PRESENT) == 0) allocate_table(pdpte);
u64* pd = tableFromPhys(pdpte & PHYS_MASK);
u64& pde = pd[pdIdx];
if ((pde & PAGE_PRESENT) == PAGE_PRESENT) panic();
pde = (physicalAddr & PHYS_MASK) | (flags & PAGE_FLAGS_MASK) | PAGE_PRESENT | PAGE_LARGE;
flush_tlb(addrInt & ~0xFFFULL);
}
void paging::map_hhdm(physaddr_t begin, physaddr_t end) {
if (begin >= end) return;
begin &= ~0xFFFULL;
end = (end + 0xFFFULL) & ~0xFFFULL;
while (begin < end && (begin & (PAGE_2M - 1)) != 0) {
map(begin, HHDM_BASE + begin, PAGE_WRITABLE | PAGE_XD);
begin += 0x1000;
}
while (begin + PAGE_2M <= end) {
map2m(begin, HHDM_BASE + begin, PAGE_WRITABLE | PAGE_XD);
begin += PAGE_2M;
}
for (; begin < end; begin += 0x1000)
map(begin, HHDM_BASE + begin, PAGE_WRITABLE | PAGE_XD);
}
void paging::allocate_table(u64& entry) {
const bool bootstrap = (reinterpret_cast<virtaddr_t>(m_pml4) < HHDM_BASE);
physaddr_t paddr = bootstrap ? m_pmm->alloc_page_bootstrap() : m_pmm->alloc_page();
if (paddr == INVALID_PHYSADDR) panic();
auto* table = reinterpret_cast<u64*>(bootstrap ? reinterpret_cast<void*>(paddr) : phys_to_virt(paddr));
for (u64 i = 0; i < 512; ++i)
table[i] = 0;
entry = (paddr & PHYS_MASK) | PAGE_WRITABLE | PAGE_PRESENT;
}
void paging::flush_tlb(u64 addr) {
asm volatile("invlpg (%0)" ::"r"(addr) : "memory");
}
} // namespace x86_64