diff --git a/kernel/arch/x86_64/paging.cpp b/kernel/arch/x86_64/paging.cpp index caf9df6..db0af53 100644 --- a/kernel/arch/x86_64/paging.cpp +++ b/kernel/arch/x86_64/paging.cpp @@ -95,7 +95,14 @@ paging::paging(pmm& pmm) m_pml4 = reinterpret_cast(m_pml4Phys + HHDM_BASE); pmm.relocate_bitmap(HHDM_BASE); - // TODO: Map the rest of RAM + 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 (entryEnd < BOOTSTRAP_HHDM_LIMIT) continue; + + map_hhdm(begin >= BOOTSTRAP_HHDM_LIMIT ? begin : BOOTSTRAP_HHDM_LIMIT, entryEnd); + } } physaddr_t paging::get(virtaddr_t virtualAddr) { @@ -106,14 +113,26 @@ physaddr_t paging::get(virtaddr_t virtualAddr) { u64 pdIdx = (addrInt >> 21) & ENTRY_INDEX_MASK; u64 ptIdx = (addrInt >> 12) & ENTRY_INDEX_MASK; + const bool bootstrap = reinterpret_cast(m_pml4) < HHDM_BASE; + + auto tableFromPhys = [bootstrap](physaddr_t physical) -> u64* { + return bootstrap ? reinterpret_cast(physical) : reinterpret_cast(HHDM_BASE + physical); + }; + u64 pml4e = m_pml4[pml4Idx]; - if ((pml4e & 0x01) == 0) return INVALID_PHYSADDR; - u64 pdpte = reinterpret_cast(pml4e & PHYS_MASK)[pdptIdx]; - if ((pdpte & 0x01) == 0) return INVALID_PHYSADDR; - u64 pde = reinterpret_cast(pdpte & PHYS_MASK)[pdIdx]; - if ((pde & 0x01) == 0) return INVALID_PHYSADDR; - u64 pte = reinterpret_cast(pde & PHYS_MASK)[ptIdx]; - if ((pte & 0x01) == 0) return INVALID_PHYSADDR; + if ((pml4e & PAGE_PRESENT) == 0) return INVALID_PHYSADDR; + + u64* pdpt = tableFromPhys(pml4e & PHYS_MASK); + u64 pdpte = pdpt[pdptIdx]; + if ((pdpte & PAGE_PRESENT) == 0) return INVALID_PHYSADDR; + + u64* pd = tableFromPhys(pdpte & PHYS_MASK); + u64 pde = pd[pdIdx]; + if ((pde & PAGE_PRESENT) == 0) return INVALID_PHYSADDR; + if ((pde & PAGE_LARGE) == PAGE_LARGE) panic(); + + u64* pt = tableFromPhys(pde & PHYS_MASK); + u64 pte = pt[ptIdx]; return (pte & PHYS_MASK) | (addrInt & ((1 << 12) - 1)); }