[boot] Fix a bug with address-index translation

When `page_entry_iterator` became a template and changed its static shifts
translating virtual address to table indices into a for loop, that loop
was getting the indices backwards (ie, PML4E index was really the PTE
index, and so on).

Tags: paging
This commit is contained in:
Justin C. Miller
2020-05-22 00:32:04 -07:00
parent b491a09686
commit e1d148a34d
2 changed files with 13 additions and 4 deletions

View File

@@ -59,14 +59,14 @@ load(
void *data_start = offset_ptr<void>(data, pheader->offset); void *data_start = offset_ptr<void>(data, pheader->offset);
bs->copy_mem(pages, data_start, pheader->mem_size); bs->copy_mem(pages, data_start, pheader->mem_size);
console::print(L" Kernel section %d physical: 0x%lx\r\n", i, pages); console::print(L" section %d phys: 0x%lx\r\n", i, pages);
console::print(L" Kernel section %d virtual: 0x%lx\r\n", i, pheader->vaddr); console::print(L" section %d virt: 0x%lx\r\n", i, pheader->vaddr);
// TODO: set appropriate RWX permissions // TODO: set appropriate RWX permissions
paging::map_pages(pml4, args, reinterpret_cast<uintptr_t>(pages), pheader->vaddr, pheader->mem_size); paging::map_pages(pml4, args, reinterpret_cast<uintptr_t>(pages), pheader->vaddr, pheader->mem_size);
} }
console::print(L" Kernel entrypoint: 0x%lx\r\n", header->entrypoint); console::print(L" entrypoint: 0x%lx\r\n", header->entrypoint);
return reinterpret_cast<kernel::entrypoint>(header->entrypoint); return reinterpret_cast<kernel::entrypoint>(header->entrypoint);
} }

View File

@@ -71,11 +71,20 @@ public:
{ {
m_table[0] = pml4; m_table[0] = pml4;
for (unsigned i = 0; i < D; ++i) { for (unsigned i = 0; i < D; ++i) {
m_index[i] = static_cast<uint16_t>((virt >> (12 + 9*i)) & 0x1ff); m_index[i] = static_cast<uint16_t>((virt >> (12 + 9*(3-i))) & 0x1ff);
ensure_table(i); ensure_table(i);
} }
} }
uintptr_t vaddress() const {
uintptr_t address = 0;
for (unsigned i = 0; i < D; ++i)
address |= static_cast<uintptr_t>(m_index[i]) << (12 + 9*(3-i));
if (address & (1ull<<47)) // canonicalize the address
address |= (0xffffull<<48);
return address;
}
void increment() void increment()
{ {
for (unsigned i = D - 1; i >= 0; --i) { for (unsigned i = D - 1; i >= 0; --i) {