2023-01-25 19:05:47 +02:00
|
|
|
#include <BAN/Errors.h>
|
2023-05-28 16:18:18 +03:00
|
|
|
#include <kernel/Arch.h>
|
|
|
|
#include <kernel/LockGuard.h>
|
2023-04-14 13:30:21 +03:00
|
|
|
#include <kernel/Memory/kmalloc.h>
|
2023-05-29 21:06:09 +03:00
|
|
|
#include <kernel/Memory/PageTable.h>
|
2023-01-25 19:05:47 +02:00
|
|
|
|
2023-05-28 16:18:18 +03:00
|
|
|
#define CLEANUP_STRUCTURE(s) \
|
|
|
|
do { \
|
|
|
|
for (uint64_t i = 0; i < 512; i++) \
|
|
|
|
if ((s)[i] & Flags::Present) \
|
|
|
|
return; \
|
2023-05-30 22:21:12 +03:00
|
|
|
kfree((void*)P2V(s)); \
|
2023-05-28 16:18:18 +03:00
|
|
|
} while (false)
|
2023-01-25 19:05:47 +02:00
|
|
|
|
2023-05-30 08:00:17 +03:00
|
|
|
extern uint8_t g_kernel_start[];
|
2023-04-20 00:45:41 +03:00
|
|
|
extern uint8_t g_kernel_end[];
|
|
|
|
|
2023-04-28 14:45:09 +03:00
|
|
|
namespace Kernel
|
2023-01-25 19:05:47 +02:00
|
|
|
{
|
|
|
|
|
2023-05-29 21:06:09 +03:00
|
|
|
static PageTable* s_kernel = nullptr;
|
|
|
|
static PageTable* s_current = nullptr;
|
2023-01-25 19:05:47 +02:00
|
|
|
|
2023-05-30 22:21:12 +03:00
|
|
|
static constexpr inline bool is_canonical(uintptr_t addr)
|
2023-05-30 08:00:17 +03:00
|
|
|
{
|
2023-05-30 22:21:12 +03:00
|
|
|
constexpr uintptr_t mask = 0xFFFF800000000000;
|
2023-05-30 08:00:17 +03:00
|
|
|
addr &= mask;
|
|
|
|
return addr == mask || addr == 0;
|
|
|
|
}
|
|
|
|
|
2023-05-30 22:21:12 +03:00
|
|
|
static constexpr inline uintptr_t uncanonicalize(uintptr_t addr)
|
2023-05-30 08:00:17 +03:00
|
|
|
{
|
2023-05-30 22:21:12 +03:00
|
|
|
if (addr & 0x0000800000000000)
|
|
|
|
return addr & ~0xFFFF000000000000;
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr inline uintptr_t canonicalize(uintptr_t addr)
|
|
|
|
{
|
|
|
|
if (addr & 0x0000800000000000)
|
|
|
|
return addr | 0xFFFF000000000000;
|
|
|
|
return addr;
|
2023-05-30 08:00:17 +03:00
|
|
|
}
|
|
|
|
|
2023-05-29 21:06:09 +03:00
|
|
|
void PageTable::initialize()
|
2023-01-25 19:05:47 +02:00
|
|
|
{
|
2023-05-28 16:18:18 +03:00
|
|
|
ASSERT(s_kernel == nullptr);
|
2023-05-29 21:06:09 +03:00
|
|
|
s_kernel = new PageTable();
|
2023-05-28 16:18:18 +03:00
|
|
|
ASSERT(s_kernel);
|
|
|
|
s_kernel->initialize_kernel();
|
|
|
|
s_kernel->load();
|
2023-01-25 19:05:47 +02:00
|
|
|
}
|
|
|
|
|
2023-05-29 21:06:09 +03:00
|
|
|
PageTable& PageTable::kernel()
|
2023-04-28 14:45:09 +03:00
|
|
|
{
|
2023-05-28 16:18:18 +03:00
|
|
|
ASSERT(s_kernel);
|
|
|
|
return *s_kernel;
|
2023-04-28 14:45:09 +03:00
|
|
|
}
|
2023-04-20 00:45:41 +03:00
|
|
|
|
2023-05-29 21:06:09 +03:00
|
|
|
PageTable& PageTable::current()
|
2023-05-16 00:26:13 +03:00
|
|
|
{
|
|
|
|
ASSERT(s_current);
|
|
|
|
return *s_current;
|
|
|
|
}
|
|
|
|
|
2023-04-28 14:45:09 +03:00
|
|
|
static uint64_t* allocate_page_aligned_page()
|
|
|
|
{
|
|
|
|
void* page = kmalloc(PAGE_SIZE, PAGE_SIZE);
|
|
|
|
ASSERT(page);
|
|
|
|
memset(page, 0, PAGE_SIZE);
|
|
|
|
return (uint64_t*)page;
|
|
|
|
}
|
2023-04-20 00:45:41 +03:00
|
|
|
|
2023-05-29 21:06:09 +03:00
|
|
|
void PageTable::initialize_kernel()
|
2023-04-20 00:45:41 +03:00
|
|
|
{
|
2023-05-30 22:21:12 +03:00
|
|
|
m_highest_paging_struct = V2P(allocate_page_aligned_page());
|
|
|
|
memset((void*)P2V(m_highest_paging_struct), 0, PAGE_SIZE);
|
2023-04-28 14:45:09 +03:00
|
|
|
|
2023-05-05 14:18:18 +03:00
|
|
|
// Identity map 4 KiB -> kernel end. We don't map the first page since nullptr derefs should
|
|
|
|
// page fault. Also there isn't anything useful in that memory.
|
2023-05-30 22:21:12 +03:00
|
|
|
map_range_at(0, KERNEL_OFFSET, (uintptr_t)g_kernel_end - KERNEL_OFFSET, Flags::ReadWrite | Flags::Present);
|
2023-04-28 14:45:09 +03:00
|
|
|
}
|
|
|
|
|
2023-05-29 21:06:09 +03:00
|
|
|
BAN::ErrorOr<PageTable*> PageTable::create_userspace()
|
2023-04-28 14:45:09 +03:00
|
|
|
{
|
2023-05-28 16:18:18 +03:00
|
|
|
// Here we copy the s_kernel paging structs since they are
|
2023-04-28 14:45:09 +03:00
|
|
|
// global for every process
|
2023-04-20 00:45:41 +03:00
|
|
|
|
2023-05-30 22:21:12 +03:00
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
|
2023-05-28 16:18:18 +03:00
|
|
|
LockGuard _(s_kernel->m_lock);
|
|
|
|
|
2023-05-30 22:21:12 +03:00
|
|
|
uint64_t* global_pml4 = (uint64_t*)P2V(s_kernel->m_highest_paging_struct);
|
2023-04-28 14:45:09 +03:00
|
|
|
|
|
|
|
uint64_t* pml4 = allocate_page_aligned_page();
|
|
|
|
for (uint32_t pml4e = 0; pml4e < 512; pml4e++)
|
2023-04-20 00:45:41 +03:00
|
|
|
{
|
2023-04-28 14:45:09 +03:00
|
|
|
if (!(global_pml4[pml4e] & Flags::Present))
|
2023-04-20 00:45:41 +03:00
|
|
|
continue;
|
|
|
|
|
2023-05-28 21:03:08 +03:00
|
|
|
uint64_t* global_pdpt = (uint64_t*)(global_pml4[pml4e] & PAGE_ADDR_MASK);
|
2023-04-20 00:45:41 +03:00
|
|
|
|
2023-04-28 14:45:09 +03:00
|
|
|
uint64_t* pdpt = allocate_page_aligned_page();
|
2023-05-28 21:03:08 +03:00
|
|
|
pml4[pml4e] = (uint64_t)pdpt | (global_pml4[pml4e] & PAGE_FLAG_MASK);
|
2023-04-20 00:45:41 +03:00
|
|
|
|
2023-04-28 14:45:09 +03:00
|
|
|
for (uint32_t pdpte = 0; pdpte < 512; pdpte++)
|
2023-04-20 00:45:41 +03:00
|
|
|
{
|
2023-04-28 14:45:09 +03:00
|
|
|
if (!(global_pdpt[pdpte] & Flags::Present))
|
2023-04-20 00:45:41 +03:00
|
|
|
continue;
|
|
|
|
|
2023-05-28 21:03:08 +03:00
|
|
|
uint64_t* global_pd = (uint64_t*)(global_pdpt[pdpte] & PAGE_ADDR_MASK);
|
2023-04-28 14:45:09 +03:00
|
|
|
|
|
|
|
uint64_t* pd = allocate_page_aligned_page();
|
2023-05-28 21:03:08 +03:00
|
|
|
pdpt[pdpte] = (uint64_t)pd | (global_pdpt[pdpte] & PAGE_FLAG_MASK);
|
2023-04-28 14:45:09 +03:00
|
|
|
|
|
|
|
for (uint32_t pde = 0; pde < 512; pde++)
|
|
|
|
{
|
|
|
|
if (!(global_pd[pde] & Flags::Present))
|
|
|
|
continue;
|
2023-04-20 00:45:41 +03:00
|
|
|
|
2023-05-28 21:03:08 +03:00
|
|
|
uint64_t* global_pt = (uint64_t*)(global_pd[pde] & PAGE_ADDR_MASK);
|
2023-04-20 00:45:41 +03:00
|
|
|
|
2023-04-28 14:45:09 +03:00
|
|
|
uint64_t* pt = allocate_page_aligned_page();
|
2023-05-28 21:03:08 +03:00
|
|
|
pd[pde] = (uint64_t)pt | (global_pd[pde] & PAGE_FLAG_MASK);
|
2023-04-28 14:45:09 +03:00
|
|
|
|
|
|
|
memcpy(pt, global_pt, PAGE_SIZE);
|
|
|
|
}
|
2023-04-20 00:45:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-29 21:06:09 +03:00
|
|
|
PageTable* result = new PageTable;
|
|
|
|
if (result == nullptr)
|
|
|
|
return BAN::Error::from_errno(ENOMEM);
|
2023-05-30 22:21:12 +03:00
|
|
|
result->m_highest_paging_struct = (paddr_t)pml4;
|
2023-05-29 21:06:09 +03:00
|
|
|
return result;
|
2023-04-28 14:45:09 +03:00
|
|
|
}
|
2023-01-25 19:05:47 +02:00
|
|
|
|
2023-05-29 21:06:09 +03:00
|
|
|
PageTable::~PageTable()
|
2023-01-30 18:54:04 +02:00
|
|
|
{
|
2023-05-30 22:21:12 +03:00
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
|
|
|
|
uint64_t* pml4 = (uint64_t*)P2V(m_highest_paging_struct);
|
2023-04-28 14:45:09 +03:00
|
|
|
for (uint32_t pml4e = 0; pml4e < 512; pml4e++)
|
2023-01-30 18:54:04 +02:00
|
|
|
{
|
2023-04-28 14:45:09 +03:00
|
|
|
if (!(pml4[pml4e] & Flags::Present))
|
2023-01-30 18:54:04 +02:00
|
|
|
continue;
|
2023-05-28 21:03:08 +03:00
|
|
|
uint64_t* pdpt = (uint64_t*)(pml4[pml4e] & PAGE_ADDR_MASK);
|
2023-04-28 14:45:09 +03:00
|
|
|
for (uint32_t pdpte = 0; pdpte < 512; pdpte++)
|
2023-01-30 18:54:04 +02:00
|
|
|
{
|
2023-04-28 14:45:09 +03:00
|
|
|
if (!(pdpt[pdpte] & Flags::Present))
|
2023-01-30 18:54:04 +02:00
|
|
|
continue;
|
2023-05-28 21:03:08 +03:00
|
|
|
uint64_t* pd = (uint64_t*)(pdpt[pdpte] & PAGE_ADDR_MASK);
|
2023-04-28 14:45:09 +03:00
|
|
|
for (uint32_t pde = 0; pde < 512; pde++)
|
|
|
|
{
|
|
|
|
if (!(pd[pde] & Flags::Present))
|
|
|
|
continue;
|
2023-05-28 21:03:08 +03:00
|
|
|
kfree((void*)(pd[pde] & PAGE_ADDR_MASK));
|
2023-04-28 14:45:09 +03:00
|
|
|
}
|
|
|
|
kfree(pd);
|
2023-01-30 18:54:04 +02:00
|
|
|
}
|
2023-04-28 14:45:09 +03:00
|
|
|
kfree(pdpt);
|
2023-01-30 18:54:04 +02:00
|
|
|
}
|
2023-04-28 14:45:09 +03:00
|
|
|
kfree(pml4);
|
2023-01-30 18:54:04 +02:00
|
|
|
}
|
|
|
|
|
2023-05-29 21:06:09 +03:00
|
|
|
void PageTable::load()
|
2023-04-28 14:45:09 +03:00
|
|
|
{
|
|
|
|
asm volatile("movq %0, %%cr3" :: "r"(m_highest_paging_struct));
|
2023-05-16 00:26:13 +03:00
|
|
|
s_current = this;
|
2023-04-28 14:45:09 +03:00
|
|
|
}
|
2023-04-20 00:45:41 +03:00
|
|
|
|
2023-05-29 21:06:09 +03:00
|
|
|
void PageTable::invalidate(vaddr_t vaddr)
|
2023-05-28 18:05:30 +03:00
|
|
|
{
|
|
|
|
ASSERT(this == s_current);
|
|
|
|
asm volatile("invlpg (%0)" :: "r"(vaddr) : "memory");
|
|
|
|
}
|
|
|
|
|
2023-05-29 21:06:09 +03:00
|
|
|
void PageTable::identity_map_page(paddr_t address, flags_t flags)
|
2023-04-28 14:45:09 +03:00
|
|
|
{
|
2023-05-28 21:03:08 +03:00
|
|
|
address &= PAGE_ADDR_MASK;
|
2023-04-28 14:45:09 +03:00
|
|
|
map_page_at(address, address, flags);
|
|
|
|
}
|
2023-01-25 19:05:47 +02:00
|
|
|
|
2023-05-29 21:06:09 +03:00
|
|
|
void PageTable::identity_map_range(paddr_t address, size_t size, flags_t flags)
|
2023-04-28 14:45:09 +03:00
|
|
|
{
|
2023-05-28 16:18:18 +03:00
|
|
|
LockGuard _(m_lock);
|
|
|
|
|
|
|
|
paddr_t s_page = address / PAGE_SIZE;
|
|
|
|
paddr_t e_page = (address + size - 1) / PAGE_SIZE;
|
|
|
|
for (paddr_t page = s_page; page <= e_page; page++)
|
|
|
|
identity_map_page(page * PAGE_SIZE, flags);
|
2023-04-28 14:45:09 +03:00
|
|
|
}
|
2023-01-25 19:05:47 +02:00
|
|
|
|
2023-05-30 22:21:12 +03:00
|
|
|
void PageTable::unmap_page(vaddr_t vaddr)
|
2023-04-28 14:45:09 +03:00
|
|
|
{
|
2023-05-28 16:18:18 +03:00
|
|
|
LockGuard _(m_lock);
|
|
|
|
|
2023-05-30 22:21:12 +03:00
|
|
|
ASSERT(is_canonical(vaddr));
|
|
|
|
vaddr = uncanonicalize(vaddr);
|
2023-01-25 19:05:47 +02:00
|
|
|
|
2023-05-30 22:21:12 +03:00
|
|
|
vaddr &= PAGE_ADDR_MASK;
|
2023-01-25 19:05:47 +02:00
|
|
|
|
2023-05-30 22:21:12 +03:00
|
|
|
if (is_page_free(vaddr))
|
2023-05-28 16:18:18 +03:00
|
|
|
{
|
2023-05-30 22:21:12 +03:00
|
|
|
dwarnln("unmapping unmapped page {8H}", vaddr);
|
2023-05-28 16:18:18 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-30 22:21:12 +03:00
|
|
|
uint64_t pml4e = (vaddr >> 39) & 0x1FF;
|
|
|
|
uint64_t pdpte = (vaddr >> 30) & 0x1FF;
|
|
|
|
uint64_t pde = (vaddr >> 21) & 0x1FF;
|
|
|
|
uint64_t pte = (vaddr >> 12) & 0x1FF;
|
2023-04-28 14:45:09 +03:00
|
|
|
|
2023-05-30 22:21:12 +03:00
|
|
|
uint64_t* pml4 = (uint64_t*)P2V(m_highest_paging_struct);
|
|
|
|
uint64_t* pdpt = (uint64_t*)P2V(pml4[pml4e] & PAGE_ADDR_MASK);
|
|
|
|
uint64_t* pd = (uint64_t*)P2V(pdpt[pdpte] & PAGE_ADDR_MASK);
|
|
|
|
uint64_t* pt = (uint64_t*)P2V(pd[pde] & PAGE_ADDR_MASK);
|
2023-04-19 23:51:36 +03:00
|
|
|
|
2023-04-28 14:45:09 +03:00
|
|
|
pt[pte] = 0;
|
|
|
|
CLEANUP_STRUCTURE(pt);
|
|
|
|
pd[pde] = 0;
|
|
|
|
CLEANUP_STRUCTURE(pd);
|
|
|
|
pdpt[pdpte] = 0;
|
|
|
|
CLEANUP_STRUCTURE(pdpt);
|
|
|
|
pml4[pml4e] = 0;
|
|
|
|
}
|
2023-04-19 23:51:36 +03:00
|
|
|
|
2023-05-30 22:21:12 +03:00
|
|
|
void PageTable::unmap_range(vaddr_t vaddr, size_t size)
|
2023-04-19 23:51:36 +03:00
|
|
|
{
|
2023-05-28 16:18:18 +03:00
|
|
|
LockGuard _(m_lock);
|
|
|
|
|
2023-05-30 22:21:12 +03:00
|
|
|
vaddr_t s_page = vaddr / PAGE_SIZE;
|
|
|
|
vaddr_t e_page = (vaddr + size - 1) / PAGE_SIZE;
|
2023-05-28 16:18:18 +03:00
|
|
|
for (vaddr_t page = s_page; page <= e_page; page++)
|
|
|
|
unmap_page(page * PAGE_SIZE);
|
2023-04-19 23:51:36 +03:00
|
|
|
}
|
|
|
|
|
2023-05-29 21:06:09 +03:00
|
|
|
void PageTable::map_page_at(paddr_t paddr, vaddr_t vaddr, flags_t flags)
|
2023-04-19 23:51:36 +03:00
|
|
|
{
|
2023-05-28 16:18:18 +03:00
|
|
|
LockGuard _(m_lock);
|
|
|
|
|
2023-05-30 22:21:12 +03:00
|
|
|
ASSERT(is_canonical(vaddr));
|
|
|
|
vaddr = uncanonicalize(vaddr);
|
2023-04-28 14:45:09 +03:00
|
|
|
|
|
|
|
ASSERT(paddr % PAGE_SIZE == 0);
|
|
|
|
ASSERT(vaddr % PAGE_SIZE == 0);;
|
|
|
|
|
|
|
|
ASSERT(flags & Flags::Present);
|
|
|
|
|
|
|
|
uint64_t pml4e = (vaddr >> 39) & 0x1FF;
|
|
|
|
uint64_t pdpte = (vaddr >> 30) & 0x1FF;
|
|
|
|
uint64_t pde = (vaddr >> 21) & 0x1FF;
|
|
|
|
uint64_t pte = (vaddr >> 12) & 0x1FF;
|
|
|
|
|
2023-05-30 22:21:12 +03:00
|
|
|
uint64_t* pml4 = (uint64_t*)P2V(m_highest_paging_struct);
|
2023-04-28 14:45:09 +03:00
|
|
|
if ((pml4[pml4e] & flags) != flags)
|
|
|
|
{
|
|
|
|
if (!(pml4[pml4e] & Flags::Present))
|
2023-05-30 22:21:12 +03:00
|
|
|
pml4[pml4e] = V2P(allocate_page_aligned_page());
|
2023-05-28 21:03:08 +03:00
|
|
|
pml4[pml4e] = (pml4[pml4e] & PAGE_ADDR_MASK) | flags;
|
2023-04-28 14:45:09 +03:00
|
|
|
}
|
|
|
|
|
2023-05-30 22:21:12 +03:00
|
|
|
uint64_t* pdpt = (uint64_t*)P2V(pml4[pml4e] & PAGE_ADDR_MASK);
|
2023-04-28 14:45:09 +03:00
|
|
|
if ((pdpt[pdpte] & flags) != flags)
|
|
|
|
{
|
|
|
|
if (!(pdpt[pdpte] & Flags::Present))
|
2023-05-30 22:21:12 +03:00
|
|
|
pdpt[pdpte] = V2P(allocate_page_aligned_page());
|
2023-05-28 21:03:08 +03:00
|
|
|
pdpt[pdpte] = (pdpt[pdpte] & PAGE_ADDR_MASK) | flags;
|
2023-04-28 14:45:09 +03:00
|
|
|
}
|
|
|
|
|
2023-05-30 22:21:12 +03:00
|
|
|
uint64_t* pd = (uint64_t*)P2V(pdpt[pdpte] & PAGE_ADDR_MASK);
|
2023-04-28 14:45:09 +03:00
|
|
|
if ((pd[pde] & flags) != flags)
|
|
|
|
{
|
|
|
|
if (!(pd[pde] & Flags::Present))
|
2023-05-30 22:21:12 +03:00
|
|
|
pd[pde] = V2P(allocate_page_aligned_page());
|
2023-05-28 21:03:08 +03:00
|
|
|
pd[pde] = (pd[pde] & PAGE_ADDR_MASK) | flags;
|
2023-04-28 14:45:09 +03:00
|
|
|
}
|
|
|
|
|
2023-05-30 22:21:12 +03:00
|
|
|
uint64_t* pt = (uint64_t*)P2V(pd[pde] & PAGE_ADDR_MASK);
|
2023-05-28 17:57:05 +03:00
|
|
|
pt[pte] = paddr | flags;
|
2023-04-19 23:51:36 +03:00
|
|
|
}
|
|
|
|
|
2023-05-30 23:56:07 +03:00
|
|
|
void PageTable::map_range_at(paddr_t paddr, vaddr_t vaddr, size_t size, flags_t flags)
|
2023-04-19 23:51:36 +03:00
|
|
|
{
|
2023-05-28 16:18:18 +03:00
|
|
|
LockGuard _(m_lock);
|
|
|
|
|
2023-05-30 22:21:12 +03:00
|
|
|
ASSERT(is_canonical(vaddr));
|
2023-04-28 14:45:09 +03:00
|
|
|
|
2023-05-30 22:21:12 +03:00
|
|
|
ASSERT(paddr % PAGE_SIZE == 0);
|
|
|
|
ASSERT(vaddr % PAGE_SIZE == 0);
|
|
|
|
|
2023-05-30 23:56:07 +03:00
|
|
|
size_t first_page = vaddr / PAGE_SIZE;
|
|
|
|
size_t last_page = (vaddr + size - 1) / PAGE_SIZE;
|
|
|
|
size_t page_count = last_page - first_page + 1;
|
|
|
|
for (size_t page = 0; page < page_count; page++)
|
|
|
|
map_page_at(paddr + page * PAGE_SIZE, vaddr + page * PAGE_SIZE, flags);
|
2023-05-30 22:21:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t PageTable::get_page_data(vaddr_t vaddr) const
|
|
|
|
{
|
|
|
|
LockGuard _(m_lock);
|
|
|
|
|
|
|
|
ASSERT(is_canonical(vaddr));
|
|
|
|
vaddr = uncanonicalize(vaddr);
|
|
|
|
|
|
|
|
ASSERT(vaddr % PAGE_SIZE == 0);
|
|
|
|
|
|
|
|
uint64_t pml4e = (vaddr >> 39) & 0x1FF;
|
|
|
|
uint64_t pdpte = (vaddr >> 30) & 0x1FF;
|
|
|
|
uint64_t pde = (vaddr >> 21) & 0x1FF;
|
|
|
|
uint64_t pte = (vaddr >> 12) & 0x1FF;
|
2023-04-28 14:45:09 +03:00
|
|
|
|
2023-05-30 22:21:12 +03:00
|
|
|
uint64_t* pml4 = (uint64_t*)P2V(m_highest_paging_struct);
|
2023-04-28 14:45:09 +03:00
|
|
|
if (!(pml4[pml4e] & Flags::Present))
|
|
|
|
return 0;
|
|
|
|
|
2023-05-30 22:21:12 +03:00
|
|
|
uint64_t* pdpt = (uint64_t*)P2V(pml4[pml4e] & PAGE_ADDR_MASK);
|
2023-04-28 14:45:09 +03:00
|
|
|
if (!(pdpt[pdpte] & Flags::Present))
|
|
|
|
return 0;
|
|
|
|
|
2023-05-30 22:21:12 +03:00
|
|
|
uint64_t* pd = (uint64_t*)P2V(pdpt[pdpte] & PAGE_ADDR_MASK);
|
2023-04-19 23:51:36 +03:00
|
|
|
if (!(pd[pde] & Flags::Present))
|
2023-04-28 14:45:09 +03:00
|
|
|
return 0;
|
|
|
|
|
2023-05-30 22:21:12 +03:00
|
|
|
uint64_t* pt = (uint64_t*)P2V(pd[pde] & PAGE_ADDR_MASK);
|
2023-04-28 14:45:09 +03:00
|
|
|
if (!(pt[pte] & Flags::Present))
|
|
|
|
return 0;
|
|
|
|
|
2023-05-06 00:14:54 +03:00
|
|
|
return pt[pte];
|
|
|
|
}
|
|
|
|
|
2023-05-29 21:06:09 +03:00
|
|
|
PageTable::flags_t PageTable::get_page_flags(vaddr_t addr) const
|
2023-05-06 00:14:54 +03:00
|
|
|
{
|
2023-05-28 21:03:08 +03:00
|
|
|
return get_page_data(addr) & PAGE_FLAG_MASK;
|
2023-05-06 00:14:54 +03:00
|
|
|
}
|
|
|
|
|
2023-05-29 21:06:09 +03:00
|
|
|
paddr_t PageTable::physical_address_of(vaddr_t addr) const
|
2023-05-06 00:14:54 +03:00
|
|
|
{
|
2023-05-28 21:03:08 +03:00
|
|
|
return get_page_data(addr) & PAGE_ADDR_MASK;
|
2023-05-06 00:14:54 +03:00
|
|
|
}
|
|
|
|
|
2023-05-29 21:06:09 +03:00
|
|
|
vaddr_t PageTable::get_free_page() const
|
2023-05-06 00:14:54 +03:00
|
|
|
{
|
2023-05-28 16:18:18 +03:00
|
|
|
LockGuard _(m_lock);
|
|
|
|
|
2023-05-06 00:14:54 +03:00
|
|
|
// Try to find free page that can be mapped without
|
|
|
|
// allocations (page table with unused entries)
|
2023-05-30 22:21:12 +03:00
|
|
|
uint64_t* pml4 = (uint64_t*)P2V(m_highest_paging_struct);
|
2023-05-06 00:14:54 +03:00
|
|
|
for (uint64_t pml4e = 0; pml4e < 512; pml4e++)
|
|
|
|
{
|
|
|
|
if (!(pml4[pml4e] & Flags::Present))
|
|
|
|
continue;
|
2023-05-30 22:21:12 +03:00
|
|
|
uint64_t* pdpt = (uint64_t*)P2V(pml4[pml4e] & PAGE_ADDR_MASK);
|
2023-05-06 00:14:54 +03:00
|
|
|
for (uint64_t pdpte = 0; pdpte < 512; pdpte++)
|
|
|
|
{
|
|
|
|
if (!(pdpt[pdpte] & Flags::Present))
|
|
|
|
continue;
|
2023-05-30 22:21:12 +03:00
|
|
|
uint64_t* pd = (uint64_t*)P2V(pdpt[pdpte] & PAGE_ADDR_MASK);
|
2023-05-06 00:14:54 +03:00
|
|
|
for (uint64_t pde = 0; pde < 512; pde++)
|
|
|
|
{
|
|
|
|
if (!(pd[pde] & Flags::Present))
|
|
|
|
continue;
|
2023-05-30 22:21:12 +03:00
|
|
|
uint64_t* pt = (uint64_t*)P2V(pd[pde] & PAGE_ADDR_MASK);
|
2023-05-06 00:14:54 +03:00
|
|
|
for (uint64_t pte = !(pml4e + pdpte + pde); pte < 512; pte++)
|
|
|
|
{
|
|
|
|
if (!(pt[pte] & Flags::Present))
|
|
|
|
{
|
|
|
|
vaddr_t vaddr = 0;
|
|
|
|
vaddr |= pml4e << 39;
|
|
|
|
vaddr |= pdpte << 30;
|
|
|
|
vaddr |= pde << 21;
|
|
|
|
vaddr |= pte << 12;
|
2023-05-30 22:21:12 +03:00
|
|
|
return canonicalize(vaddr);
|
2023-05-06 00:14:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find any free page page (except for page 0)
|
2023-05-30 22:21:12 +03:00
|
|
|
vaddr_t vaddr = PAGE_SIZE;
|
|
|
|
while ((vaddr >> 48) == 0)
|
2023-05-06 00:14:54 +03:00
|
|
|
{
|
2023-05-30 22:21:12 +03:00
|
|
|
if (!(get_page_flags(vaddr) & Flags::Present))
|
|
|
|
return vaddr;
|
|
|
|
vaddr += PAGE_SIZE;
|
2023-05-06 00:14:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2023-05-29 21:06:09 +03:00
|
|
|
vaddr_t PageTable::get_free_contiguous_pages(size_t page_count) const
|
2023-05-08 22:10:49 +03:00
|
|
|
{
|
2023-05-28 16:18:18 +03:00
|
|
|
LockGuard _(m_lock);
|
|
|
|
|
2023-05-30 22:21:12 +03:00
|
|
|
for (vaddr_t vaddr = PAGE_SIZE; !(vaddr >> 48); vaddr += PAGE_SIZE)
|
2023-05-08 22:10:49 +03:00
|
|
|
{
|
|
|
|
bool valid { true };
|
|
|
|
for (size_t page = 0; page < page_count; page++)
|
|
|
|
{
|
2023-05-30 22:21:12 +03:00
|
|
|
if (get_page_flags(vaddr + page * PAGE_SIZE) & Flags::Present)
|
2023-05-08 22:10:49 +03:00
|
|
|
{
|
2023-05-30 22:21:12 +03:00
|
|
|
vaddr += page * PAGE_SIZE;
|
2023-05-08 22:10:49 +03:00
|
|
|
valid = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (valid)
|
2023-05-30 22:21:12 +03:00
|
|
|
return vaddr;
|
2023-05-08 22:10:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2023-05-29 21:06:09 +03:00
|
|
|
bool PageTable::is_page_free(vaddr_t page) const
|
2023-05-06 00:14:54 +03:00
|
|
|
{
|
|
|
|
ASSERT(page % PAGE_SIZE == 0);
|
|
|
|
return !(get_page_flags(page) & Flags::Present);
|
|
|
|
}
|
|
|
|
|
2023-05-29 21:06:09 +03:00
|
|
|
bool PageTable::is_range_free(vaddr_t start, size_t size) const
|
2023-05-06 00:14:54 +03:00
|
|
|
{
|
2023-05-28 16:18:18 +03:00
|
|
|
LockGuard _(m_lock);
|
2023-05-30 22:21:12 +03:00
|
|
|
|
2023-05-06 00:14:54 +03:00
|
|
|
vaddr_t first_page = start / PAGE_SIZE;
|
2023-05-28 16:18:18 +03:00
|
|
|
vaddr_t last_page = (start + size - 1) / PAGE_SIZE;
|
2023-05-06 00:14:54 +03:00
|
|
|
for (vaddr_t page = first_page; page <= last_page; page++)
|
|
|
|
if (!is_page_free(page * PAGE_SIZE))
|
|
|
|
return false;
|
|
|
|
return true;
|
2023-04-19 23:51:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|