2023-01-25 19:05:47 +02:00
|
|
|
#include <BAN/Errors.h>
|
2023-05-28 16:18:18 +03:00
|
|
|
#include <kernel/Arch.h>
|
2023-07-13 14:28:53 +03:00
|
|
|
#include <kernel/CPUID.h>
|
2023-05-28 16:18:18 +03:00
|
|
|
#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-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-07-13 14:28:53 +03:00
|
|
|
extern uint8_t g_kernel_execute_start[];
|
|
|
|
extern uint8_t g_kernel_execute_end[];
|
|
|
|
|
2023-07-21 15:45:02 +03:00
|
|
|
extern uint8_t g_userspace_start[];
|
|
|
|
extern uint8_t g_userspace_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-07-13 14:28:53 +03:00
|
|
|
static bool s_has_nxe = false;
|
2023-07-13 15:08:54 +03:00
|
|
|
static bool s_has_pge = false;
|
2023-01-25 19:05:47 +02:00
|
|
|
|
2023-07-21 13:29:46 +03:00
|
|
|
// PML4 entry for kernel memory
|
|
|
|
static paddr_t s_global_pml4e = 0;
|
2023-07-05 23:41:35 +03: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-07-21 12:01:50 +03:00
|
|
|
static inline PageTable::flags_t parse_flags(uint64_t entry)
|
|
|
|
{
|
2023-08-01 16:32:51 +03:00
|
|
|
return (s_has_nxe && !(entry & (1ull << 63)) ? PageTable::Flags::Execute : 0) | (entry & 0b100000111);
|
2023-07-21 12:01:50 +03:00
|
|
|
}
|
|
|
|
|
2023-05-29 21:06:09 +03:00
|
|
|
void PageTable::initialize()
|
2023-01-25 19:05:47 +02:00
|
|
|
{
|
2023-07-13 14:28:53 +03:00
|
|
|
if (CPUID::has_nxe())
|
|
|
|
{
|
|
|
|
asm volatile(
|
|
|
|
"movl $0xC0000080, %ecx;"
|
|
|
|
"rdmsr;"
|
|
|
|
"orl $0x800, %eax;"
|
|
|
|
"wrmsr"
|
|
|
|
);
|
|
|
|
s_has_nxe = true;
|
|
|
|
}
|
|
|
|
|
2023-07-13 15:08:54 +03:00
|
|
|
uint32_t ecx, edx;
|
|
|
|
CPUID::get_features(ecx, edx);
|
|
|
|
if (edx & CPUID::EDX_PGE)
|
|
|
|
{
|
|
|
|
asm volatile(
|
|
|
|
"movq %cr4, %rax;"
|
|
|
|
"orq $0x80, %rax;"
|
|
|
|
"movq %rax, %cr4;"
|
|
|
|
);
|
|
|
|
s_has_pge = true;
|
|
|
|
}
|
|
|
|
|
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-07-05 23:41:35 +03:00
|
|
|
static uint64_t* allocate_zeroed_page_aligned_page()
|
2023-04-28 14:45:09 +03:00
|
|
|
{
|
2023-06-18 23:27:00 +03:00
|
|
|
void* page = kmalloc(PAGE_SIZE, PAGE_SIZE, true);
|
2023-04-28 14:45:09 +03:00
|
|
|
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-07-21 13:29:46 +03:00
|
|
|
ASSERT(s_global_pml4e == 0);
|
|
|
|
s_global_pml4e = V2P(allocate_zeroed_page_aligned_page());
|
|
|
|
|
|
|
|
m_highest_paging_struct = V2P(allocate_zeroed_page_aligned_page());
|
|
|
|
|
|
|
|
uint64_t* pml4 = (uint64_t*)P2V(m_highest_paging_struct);
|
|
|
|
pml4[511] = s_global_pml4e;
|
2023-07-05 23:41:35 +03:00
|
|
|
|
2023-05-31 00:39:06 +03:00
|
|
|
// Map (0 -> phys_kernel_end) to (KERNEL_OFFSET -> virt_kernel_end)
|
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-07-13 14:28:53 +03:00
|
|
|
|
|
|
|
// Map executable kernel memory as executable
|
|
|
|
map_range_at(
|
|
|
|
V2P(g_kernel_execute_start),
|
|
|
|
(vaddr_t)g_kernel_execute_start,
|
|
|
|
g_kernel_execute_end - g_kernel_execute_start,
|
2023-07-21 11:17:39 +03:00
|
|
|
Flags::Execute | Flags::Present
|
2023-07-13 14:28:53 +03:00
|
|
|
);
|
2023-07-21 15:45:02 +03:00
|
|
|
|
|
|
|
// Map userspace memory
|
|
|
|
map_range_at(
|
|
|
|
V2P(g_userspace_start),
|
|
|
|
(vaddr_t)g_userspace_start,
|
|
|
|
g_userspace_end - g_userspace_start,
|
|
|
|
Flags::Execute | Flags::UserSupervisor | 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
|
|
|
LockGuard _(s_kernel->m_lock);
|
2023-07-05 23:41:35 +03:00
|
|
|
PageTable* page_table = new PageTable;
|
|
|
|
if (page_table == nullptr)
|
|
|
|
return BAN::Error::from_errno(ENOMEM);
|
|
|
|
page_table->map_kernel_memory();
|
|
|
|
return page_table;
|
|
|
|
}
|
2023-05-28 16:18:18 +03:00
|
|
|
|
2023-07-05 23:41:35 +03:00
|
|
|
void PageTable::map_kernel_memory()
|
|
|
|
{
|
2023-07-21 13:29:46 +03:00
|
|
|
ASSERT(s_kernel);
|
|
|
|
ASSERT(s_global_pml4e);
|
2023-04-28 14:45:09 +03:00
|
|
|
|
2023-07-05 23:41:35 +03:00
|
|
|
ASSERT(m_highest_paging_struct == 0);
|
|
|
|
m_highest_paging_struct = V2P(allocate_zeroed_page_aligned_page());
|
2023-04-20 00:45:41 +03:00
|
|
|
|
2023-07-21 13:29:46 +03:00
|
|
|
uint64_t* kernel_pml4 = (uint64_t*)P2V(s_kernel->m_highest_paging_struct);
|
2023-04-20 00:45:41 +03:00
|
|
|
|
2023-07-05 23:41:35 +03:00
|
|
|
uint64_t* pml4 = (uint64_t*)P2V(m_highest_paging_struct);
|
2023-07-21 13:29:46 +03:00
|
|
|
pml4[511] = kernel_pml4[511];
|
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
|
|
|
uint64_t* pml4 = (uint64_t*)P2V(m_highest_paging_struct);
|
2023-07-21 13:29:46 +03:00
|
|
|
|
|
|
|
// NOTE: we only loop until 511 since the last one is the kernel memory
|
|
|
|
for (uint64_t pml4e = 0; pml4e < 511; 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-31 00:36:46 +03:00
|
|
|
uint64_t* pdpt = (uint64_t*)P2V(pml4[pml4e] & PAGE_ADDR_MASK);
|
2023-07-05 23:41:35 +03:00
|
|
|
for (uint64_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-31 00:36:46 +03:00
|
|
|
uint64_t* pd = (uint64_t*)P2V(pdpt[pdpte] & PAGE_ADDR_MASK);
|
2023-07-05 23:41:35 +03:00
|
|
|
for (uint64_t pde = 0; pde < 512; pde++)
|
2023-04-28 14:45:09 +03:00
|
|
|
{
|
|
|
|
if (!(pd[pde] & Flags::Present))
|
|
|
|
continue;
|
2023-05-31 00:36:46 +03:00
|
|
|
kfree((void*)P2V(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
|
|
|
{
|
2023-08-02 22:09:14 +03:00
|
|
|
ASSERT(vaddr % PAGE_SIZE == 0);
|
2023-07-05 23:41:35 +03:00
|
|
|
if (this == s_current)
|
|
|
|
asm volatile("invlpg (%0)" :: "r"(vaddr) : "memory");
|
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-07-05 23:41:35 +03:00
|
|
|
if (vaddr && (vaddr >= KERNEL_OFFSET) != (this == s_kernel))
|
|
|
|
Kernel::panic("unmapping {8H}, kernel: {}", vaddr, this == s_kernel);
|
|
|
|
|
2023-08-02 22:09:14 +03:00
|
|
|
ASSERT(is_canonical(vaddr));
|
|
|
|
vaddr_t uc_vaddr = uncanonicalize(vaddr);
|
|
|
|
|
|
|
|
ASSERT(vaddr % PAGE_SIZE == 0);
|
|
|
|
|
|
|
|
uint64_t pml4e = (uc_vaddr >> 39) & 0x1FF;
|
|
|
|
uint64_t pdpte = (uc_vaddr >> 30) & 0x1FF;
|
|
|
|
uint64_t pde = (uc_vaddr >> 21) & 0x1FF;
|
|
|
|
uint64_t pte = (uc_vaddr >> 12) & 0x1FF;
|
|
|
|
|
|
|
|
LockGuard _(m_lock);
|
|
|
|
|
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* 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;
|
2023-08-02 22:09:14 +03:00
|
|
|
invalidate(vaddr);
|
2023-04-28 14:45:09 +03:00
|
|
|
}
|
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-30 22:21:12 +03:00
|
|
|
vaddr_t s_page = vaddr / PAGE_SIZE;
|
2023-08-02 22:09:14 +03:00
|
|
|
vaddr_t e_page = BAN::Math::div_round_up<vaddr_t>(vaddr + size, PAGE_SIZE);
|
|
|
|
|
|
|
|
LockGuard _(m_lock);
|
|
|
|
for (vaddr_t page = s_page; page < e_page; page++)
|
2023-05-28 16:18:18 +03:00
|
|
|
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-07-05 23:41:35 +03:00
|
|
|
if (vaddr && (vaddr >= KERNEL_OFFSET) != (this == s_kernel))
|
|
|
|
Kernel::panic("mapping {8H} to {8H}, kernel: {}", paddr, vaddr, this == s_kernel);
|
|
|
|
|
2023-05-30 22:21:12 +03:00
|
|
|
ASSERT(is_canonical(vaddr));
|
2023-08-02 22:09:14 +03:00
|
|
|
vaddr_t uc_vaddr = uncanonicalize(vaddr);
|
2023-04-28 14:45:09 +03:00
|
|
|
|
|
|
|
ASSERT(paddr % PAGE_SIZE == 0);
|
2023-07-05 23:41:35 +03:00
|
|
|
ASSERT(vaddr % PAGE_SIZE == 0);
|
2023-08-01 16:32:51 +03:00
|
|
|
ASSERT(flags & Flags::Used);
|
2023-04-28 14:45:09 +03:00
|
|
|
|
2023-08-02 22:09:14 +03:00
|
|
|
uint64_t pml4e = (uc_vaddr >> 39) & 0x1FF;
|
|
|
|
uint64_t pdpte = (uc_vaddr >> 30) & 0x1FF;
|
|
|
|
uint64_t pde = (uc_vaddr >> 21) & 0x1FF;
|
|
|
|
uint64_t pte = (uc_vaddr >> 12) & 0x1FF;
|
2023-04-28 14:45:09 +03:00
|
|
|
|
2023-07-21 13:29:46 +03:00
|
|
|
uint64_t extra_flags = 0;
|
|
|
|
if (s_has_nxe && !(flags & Flags::Execute))
|
|
|
|
extra_flags |= 1ull << 63;
|
|
|
|
if (s_has_pge && pml4e == 511)
|
|
|
|
extra_flags |= 1ull << 8;
|
2023-08-01 16:32:51 +03:00
|
|
|
if (flags & Flags::Reserved)
|
|
|
|
extra_flags |= Flags::Reserved;
|
2023-08-02 22:09:14 +03:00
|
|
|
|
|
|
|
// NOTE: we add present here, since it has to be available in higher level structures
|
|
|
|
flags_t uwr_flags = (flags & (Flags::UserSupervisor | Flags::ReadWrite)) | Flags::Present;
|
|
|
|
|
|
|
|
LockGuard _(m_lock);
|
2023-07-21 13:29:46 +03:00
|
|
|
|
2023-05-30 22:21:12 +03:00
|
|
|
uint64_t* pml4 = (uint64_t*)P2V(m_highest_paging_struct);
|
2023-07-21 13:29:46 +03:00
|
|
|
if ((pml4[pml4e] & uwr_flags) != uwr_flags)
|
2023-04-28 14:45:09 +03:00
|
|
|
{
|
|
|
|
if (!(pml4[pml4e] & Flags::Present))
|
2023-07-05 23:41:35 +03:00
|
|
|
pml4[pml4e] = V2P(allocate_zeroed_page_aligned_page());
|
2023-07-21 13:29:46 +03:00
|
|
|
pml4[pml4e] |= uwr_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-07-21 13:29:46 +03:00
|
|
|
if ((pdpt[pdpte] & uwr_flags) != uwr_flags)
|
2023-04-28 14:45:09 +03:00
|
|
|
{
|
|
|
|
if (!(pdpt[pdpte] & Flags::Present))
|
2023-07-05 23:41:35 +03:00
|
|
|
pdpt[pdpte] = V2P(allocate_zeroed_page_aligned_page());
|
2023-07-21 13:29:46 +03:00
|
|
|
pdpt[pdpte] |= uwr_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-07-21 13:29:46 +03:00
|
|
|
if ((pd[pde] & uwr_flags) != uwr_flags)
|
2023-04-28 14:45:09 +03:00
|
|
|
{
|
|
|
|
if (!(pd[pde] & Flags::Present))
|
2023-07-05 23:41:35 +03:00
|
|
|
pd[pde] = V2P(allocate_zeroed_page_aligned_page());
|
2023-07-21 13:29:46 +03:00
|
|
|
pd[pde] |= uwr_flags;
|
2023-04-28 14:45:09 +03:00
|
|
|
}
|
|
|
|
|
2023-08-02 22:09:14 +03:00
|
|
|
if (!(flags & Flags::Present))
|
|
|
|
uwr_flags &= ~Flags::Present;
|
|
|
|
|
2023-05-30 22:21:12 +03:00
|
|
|
uint64_t* pt = (uint64_t*)P2V(pd[pde] & PAGE_ADDR_MASK);
|
2023-07-21 13:29:46 +03:00
|
|
|
pt[pte] = paddr | uwr_flags | extra_flags;
|
2023-07-05 23:41:35 +03:00
|
|
|
|
2023-08-02 22:09:14 +03:00
|
|
|
invalidate(vaddr);
|
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-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;
|
2023-08-02 22:09:14 +03:00
|
|
|
|
|
|
|
LockGuard _(m_lock);
|
2023-05-30 23:56:07 +03:00
|
|
|
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
|
|
|
|
{
|
|
|
|
ASSERT(is_canonical(vaddr));
|
2023-08-02 22:09:14 +03:00
|
|
|
vaddr_t uc_vaddr = uncanonicalize(vaddr);
|
2023-05-30 22:21:12 +03:00
|
|
|
|
|
|
|
ASSERT(vaddr % PAGE_SIZE == 0);
|
|
|
|
|
2023-08-02 22:09:14 +03:00
|
|
|
uint64_t pml4e = (uc_vaddr >> 39) & 0x1FF;
|
|
|
|
uint64_t pdpte = (uc_vaddr >> 30) & 0x1FF;
|
|
|
|
uint64_t pde = (uc_vaddr >> 21) & 0x1FF;
|
|
|
|
uint64_t pte = (uc_vaddr >> 12) & 0x1FF;
|
2023-04-28 14:45:09 +03:00
|
|
|
|
2023-08-02 22:09:14 +03:00
|
|
|
LockGuard _(m_lock);
|
|
|
|
|
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-08-02 22:09:14 +03:00
|
|
|
if (!(pt[pte] & Flags::Used))
|
2023-04-28 14:45:09 +03:00
|
|
|
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-07-21 12:01:50 +03:00
|
|
|
return parse_flags(get_page_data(addr));
|
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-07-13 14:28:53 +03:00
|
|
|
uint64_t page_data = get_page_data(addr);
|
|
|
|
return (page_data & PAGE_ADDR_MASK) & ~(1ull << 63);
|
2023-05-06 00:14:54 +03:00
|
|
|
}
|
|
|
|
|
2023-08-02 22:09:14 +03:00
|
|
|
bool PageTable::reserve_page(vaddr_t vaddr, bool only_free)
|
2023-05-06 00:14:54 +03:00
|
|
|
{
|
2023-05-28 16:18:18 +03:00
|
|
|
LockGuard _(m_lock);
|
2023-08-02 22:09:14 +03:00
|
|
|
ASSERT(vaddr % PAGE_SIZE == 0);
|
|
|
|
if (only_free && !is_page_free(vaddr))
|
|
|
|
return false;
|
|
|
|
map_page_at(0, vaddr, Flags::Reserved);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PageTable::reserve_range(vaddr_t vaddr, size_t bytes, bool only_free)
|
|
|
|
{
|
|
|
|
if (size_t rem = bytes % PAGE_SIZE)
|
|
|
|
bytes += PAGE_SIZE - rem;
|
|
|
|
ASSERT(vaddr % PAGE_SIZE == 0);
|
2023-05-28 16:18:18 +03:00
|
|
|
|
2023-08-02 22:09:14 +03:00
|
|
|
LockGuard _(m_lock);
|
|
|
|
if (only_free && !is_range_free(vaddr, bytes))
|
|
|
|
return false;
|
|
|
|
for (size_t offset = 0; offset < bytes; offset += PAGE_SIZE)
|
|
|
|
reserve_page(vaddr + offset);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
vaddr_t PageTable::reserve_free_page(vaddr_t first_address, vaddr_t last_address)
|
|
|
|
{
|
2023-07-05 23:41:35 +03:00
|
|
|
if (size_t rem = first_address % PAGE_SIZE)
|
|
|
|
first_address += PAGE_SIZE - rem;
|
2023-08-02 22:09:14 +03:00
|
|
|
if (size_t rem = last_address % PAGE_SIZE)
|
|
|
|
last_address -= rem;
|
2023-07-05 23:41:35 +03:00
|
|
|
|
|
|
|
ASSERT(is_canonical(first_address));
|
2023-08-02 22:09:14 +03:00
|
|
|
ASSERT(is_canonical(last_address));
|
|
|
|
const vaddr_t uc_vaddr_start = uncanonicalize(first_address);
|
|
|
|
const vaddr_t uc_vaddr_end = uncanonicalize(last_address);
|
|
|
|
|
|
|
|
uint16_t pml4e = (uc_vaddr_start >> 39) & 0x1FF;
|
|
|
|
uint16_t pdpte = (uc_vaddr_start >> 30) & 0x1FF;
|
|
|
|
uint16_t pde = (uc_vaddr_start >> 21) & 0x1FF;
|
|
|
|
uint16_t pte = (uc_vaddr_start >> 12) & 0x1FF;
|
|
|
|
|
|
|
|
const uint16_t e_pml4e = (uc_vaddr_end >> 39) & 0x1FF;
|
|
|
|
const uint16_t e_pdpte = (uc_vaddr_end >> 30) & 0x1FF;
|
|
|
|
const uint16_t e_pde = (uc_vaddr_end >> 21) & 0x1FF;
|
|
|
|
const uint16_t e_pte = (uc_vaddr_end >> 12) & 0x1FF;
|
|
|
|
|
|
|
|
LockGuard _(m_lock);
|
2023-07-05 23:41:35 +03:00
|
|
|
|
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-07-05 23:41:35 +03:00
|
|
|
for (; pml4e < 512; pml4e++)
|
2023-05-06 00:14:54 +03:00
|
|
|
{
|
2023-08-02 22:09:14 +03:00
|
|
|
if (pml4e > e_pml4e)
|
|
|
|
break;
|
2023-05-06 00:14:54 +03:00
|
|
|
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-07-05 23:41:35 +03:00
|
|
|
for (; pdpte < 512; pdpte++)
|
2023-05-06 00:14:54 +03:00
|
|
|
{
|
2023-08-02 22:09:14 +03:00
|
|
|
if (pml4e == e_pml4e && pdpte > e_pdpte)
|
|
|
|
break;
|
2023-05-06 00:14:54 +03:00
|
|
|
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-07-05 23:41:35 +03:00
|
|
|
for (; pde < 512; pde++)
|
2023-05-06 00:14:54 +03:00
|
|
|
{
|
2023-08-02 22:09:14 +03:00
|
|
|
if (pml4e == e_pml4e && pdpte == e_pdpte && pde > e_pde)
|
|
|
|
break;
|
2023-05-06 00:14:54 +03:00
|
|
|
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-07-05 23:41:35 +03:00
|
|
|
for (; pte < 512; pte++)
|
2023-05-06 00:14:54 +03:00
|
|
|
{
|
2023-08-02 22:09:14 +03:00
|
|
|
if (pml4e == e_pml4e && pdpte == e_pdpte && pde == e_pde && pte >= e_pte)
|
|
|
|
break;
|
2023-08-01 16:32:51 +03:00
|
|
|
if (!(pt[pte] & Flags::Used))
|
2023-05-06 00:14:54 +03:00
|
|
|
{
|
|
|
|
vaddr_t vaddr = 0;
|
2023-08-02 22:09:14 +03:00
|
|
|
vaddr |= (uint64_t)pml4e << 39;
|
|
|
|
vaddr |= (uint64_t)pdpte << 30;
|
|
|
|
vaddr |= (uint64_t)pde << 21;
|
|
|
|
vaddr |= (uint64_t)pte << 12;
|
|
|
|
ASSERT(reserve_page(vaddr));
|
2023-05-30 22:21:12 +03:00
|
|
|
return canonicalize(vaddr);
|
2023-05-06 00:14:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-02 22:09:14 +03:00
|
|
|
// Find any free page
|
|
|
|
vaddr_t uc_vaddr = uc_vaddr_start;
|
|
|
|
while (uc_vaddr < uc_vaddr_end)
|
2023-05-06 00:14:54 +03:00
|
|
|
{
|
2023-08-02 22:09:14 +03:00
|
|
|
if (vaddr_t vaddr = canonicalize(uc_vaddr); is_page_free(vaddr))
|
2023-08-01 16:32:51 +03:00
|
|
|
{
|
2023-08-02 22:09:14 +03:00
|
|
|
ASSERT(reserve_page(vaddr));
|
2023-05-30 22:21:12 +03:00
|
|
|
return vaddr;
|
2023-08-01 16:32:51 +03:00
|
|
|
}
|
2023-08-02 22:09:14 +03:00
|
|
|
uc_vaddr += PAGE_SIZE;
|
2023-05-06 00:14:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2023-08-02 22:09:14 +03:00
|
|
|
vaddr_t PageTable::reserve_free_contiguous_pages(size_t page_count, vaddr_t first_address, vaddr_t last_address)
|
2023-05-08 22:10:49 +03:00
|
|
|
{
|
2023-08-02 22:09:14 +03:00
|
|
|
if (size_t rem = first_address % PAGE_SIZE)
|
|
|
|
first_address += PAGE_SIZE - rem;
|
|
|
|
if (size_t rem = last_address % PAGE_SIZE)
|
|
|
|
last_address -= rem;
|
|
|
|
|
|
|
|
ASSERT(is_canonical(first_address));
|
|
|
|
ASSERT(is_canonical(last_address));
|
2023-06-04 01:15:48 +03:00
|
|
|
|
2023-05-28 16:18:18 +03:00
|
|
|
LockGuard _(m_lock);
|
|
|
|
|
2023-08-02 22:09:14 +03:00
|
|
|
for (vaddr_t vaddr = first_address; vaddr < last_address;)
|
2023-05-08 22:10:49 +03:00
|
|
|
{
|
|
|
|
bool valid { true };
|
|
|
|
for (size_t page = 0; page < page_count; page++)
|
|
|
|
{
|
2023-08-02 22:09:14 +03:00
|
|
|
if (!is_canonical(vaddr + page * PAGE_SIZE))
|
2023-05-08 22:10:49 +03:00
|
|
|
{
|
2023-08-02 22:09:14 +03:00
|
|
|
vaddr = canonicalize(uncanonicalize(vaddr) + page * PAGE_SIZE);
|
|
|
|
valid = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!is_page_free(vaddr + page * PAGE_SIZE))
|
|
|
|
{
|
|
|
|
vaddr += (page + 1) * PAGE_SIZE;
|
2023-05-08 22:10:49 +03:00
|
|
|
valid = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (valid)
|
2023-08-01 16:32:51 +03:00
|
|
|
{
|
2023-08-02 22:09:14 +03:00
|
|
|
ASSERT(reserve_range(vaddr, page_count * PAGE_SIZE));
|
2023-05-30 22:21:12 +03:00
|
|
|
return vaddr;
|
2023-08-01 16:32:51 +03:00
|
|
|
}
|
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);
|
2023-08-01 16:32:51 +03:00
|
|
|
return !(get_page_flags(page) & Flags::Used);
|
2023-05-06 00:14:54 +03:00
|
|
|
}
|
|
|
|
|
2023-08-02 22:09:14 +03:00
|
|
|
bool PageTable::is_range_free(vaddr_t vaddr, size_t size) const
|
2023-05-06 00:14:54 +03:00
|
|
|
{
|
2023-08-02 22:09:14 +03:00
|
|
|
vaddr_t s_page = vaddr / PAGE_SIZE;
|
|
|
|
vaddr_t e_page = BAN::Math::div_round_up<vaddr_t>(vaddr + size, PAGE_SIZE);
|
2023-05-30 22:21:12 +03:00
|
|
|
|
2023-08-02 22:09:14 +03:00
|
|
|
LockGuard _(m_lock);
|
|
|
|
for (vaddr_t page = s_page; page < e_page; page++)
|
2023-05-06 00:14:54 +03:00
|
|
|
if (!is_page_free(page * PAGE_SIZE))
|
|
|
|
return false;
|
|
|
|
return true;
|
2023-04-19 23:51:36 +03:00
|
|
|
}
|
|
|
|
|
2023-06-03 18:54:48 +03:00
|
|
|
static void dump_range(vaddr_t start, vaddr_t end, PageTable::flags_t flags)
|
|
|
|
{
|
|
|
|
if (start == 0)
|
|
|
|
return;
|
2023-07-21 12:01:50 +03:00
|
|
|
dprintln("{}-{}: {}{}{}{}",
|
2023-06-03 18:54:48 +03:00
|
|
|
(void*)canonicalize(start),
|
|
|
|
(void*)canonicalize(end - 1),
|
2023-07-21 12:01:50 +03:00
|
|
|
flags & PageTable::Flags::Execute ? 'x' : '-',
|
2023-06-03 18:54:48 +03:00
|
|
|
flags & PageTable::Flags::UserSupervisor ? 'u' : '-',
|
|
|
|
flags & PageTable::Flags::ReadWrite ? 'w' : '-',
|
|
|
|
flags & PageTable::Flags::Present ? 'r' : '-'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageTable::debug_dump()
|
|
|
|
{
|
|
|
|
LockGuard _(m_lock);
|
|
|
|
|
|
|
|
flags_t flags = 0;
|
|
|
|
vaddr_t start = 0;
|
|
|
|
|
|
|
|
uint64_t* pml4 = (uint64_t*)P2V(m_highest_paging_struct);
|
|
|
|
for (uint64_t pml4e = 0; pml4e < 512; pml4e++)
|
|
|
|
{
|
|
|
|
if (!(pml4[pml4e] & Flags::Present))
|
|
|
|
{
|
|
|
|
dump_range(start, (pml4e << 39), flags);
|
|
|
|
start = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
uint64_t* pdpt = (uint64_t*)P2V(pml4[pml4e] & PAGE_ADDR_MASK);
|
|
|
|
for (uint64_t pdpte = 0; pdpte < 512; pdpte++)
|
|
|
|
{
|
|
|
|
if (!(pdpt[pdpte] & Flags::Present))
|
|
|
|
{
|
|
|
|
dump_range(start, (pml4e << 39) | (pdpte << 30), flags);
|
|
|
|
start = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
uint64_t* pd = (uint64_t*)P2V(pdpt[pdpte] & PAGE_ADDR_MASK);
|
|
|
|
for (uint64_t pde = 0; pde < 512; pde++)
|
|
|
|
{
|
|
|
|
if (!(pd[pde] & Flags::Present))
|
|
|
|
{
|
|
|
|
dump_range(start, (pml4e << 39) | (pdpte << 30) | (pde << 21), flags);
|
|
|
|
start = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
uint64_t* pt = (uint64_t*)P2V(pd[pde] & PAGE_ADDR_MASK);
|
|
|
|
for (uint64_t pte = 0; pte < 512; pte++)
|
|
|
|
{
|
2023-07-21 12:01:50 +03:00
|
|
|
if (parse_flags(pt[pte]) != flags)
|
2023-06-03 18:54:48 +03:00
|
|
|
{
|
|
|
|
dump_range(start, (pml4e << 39) | (pdpte << 30) | (pde << 21) | (pte << 12), flags);
|
|
|
|
start = 0;
|
|
|
|
}
|
|
|
|
|
2023-08-01 16:32:51 +03:00
|
|
|
if (!(pt[pte] & Flags::Used))
|
2023-06-03 18:54:48 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (start == 0)
|
|
|
|
{
|
2023-07-21 12:01:50 +03:00
|
|
|
flags = parse_flags(pt[pte]);
|
2023-06-03 18:54:48 +03:00
|
|
|
start = (pml4e << 39) | (pdpte << 30) | (pde << 21) | (pte << 12);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-19 23:51:36 +03:00
|
|
|
}
|