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-10-30 19:02:09 +02:00
|
|
|
#include <kernel/InterruptController.h>
|
2024-02-28 22:39:02 +02:00
|
|
|
#include <kernel/Lock/SpinLock.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-09-09 22:52:03 +03:00
|
|
|
|
2024-05-24 14:12:35 +03:00
|
|
|
SpinLock PageTable::s_fast_page_lock;
|
2024-02-28 22:39:02 +02:00
|
|
|
|
2023-05-29 21:06:09 +03:00
|
|
|
static PageTable* s_kernel = 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-06 23:59:30 +03:00
|
|
|
using Flags = PageTable::Flags;
|
|
|
|
|
|
|
|
PageTable::flags_t result = 0;
|
|
|
|
if (s_has_nxe && !(entry & (1ull << 63)))
|
|
|
|
result |= Flags::Execute;
|
|
|
|
if (entry & Flags::Reserved)
|
|
|
|
result |= Flags::Reserved;
|
|
|
|
if (entry & Flags::CacheDisable)
|
|
|
|
result |= Flags::CacheDisable;
|
|
|
|
if (entry & Flags::UserSupervisor)
|
|
|
|
result |= Flags::UserSupervisor;
|
|
|
|
if (entry & Flags::ReadWrite)
|
|
|
|
result |= Flags::ReadWrite;
|
|
|
|
if (entry & Flags::Present)
|
|
|
|
result |= Flags::Present;
|
|
|
|
return result;
|
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())
|
2024-03-06 02:19:59 +02:00
|
|
|
s_has_nxe = true;
|
|
|
|
|
|
|
|
if (CPUID::has_pge())
|
|
|
|
s_has_pge = true;
|
|
|
|
|
|
|
|
ASSERT(s_kernel == nullptr);
|
|
|
|
s_kernel = new PageTable();
|
|
|
|
ASSERT(s_kernel);
|
|
|
|
|
|
|
|
s_kernel->initialize_kernel();
|
|
|
|
s_kernel->initial_load();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageTable::initial_load()
|
|
|
|
{
|
|
|
|
if (s_has_nxe)
|
2023-07-13 14:28:53 +03:00
|
|
|
{
|
|
|
|
asm volatile(
|
2024-03-06 02:19:59 +02:00
|
|
|
"movl $0xC0000080, %%ecx;"
|
2023-07-13 14:28:53 +03:00
|
|
|
"rdmsr;"
|
2024-03-06 02:19:59 +02:00
|
|
|
"orl $0x800, %%eax;"
|
2023-07-13 14:28:53 +03:00
|
|
|
"wrmsr"
|
2024-03-06 02:19:59 +02:00
|
|
|
::: "eax", "ecx", "edx", "memory"
|
2023-07-13 14:28:53 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-03-06 02:19:59 +02:00
|
|
|
if (s_has_pge)
|
2023-07-13 15:08:54 +03:00
|
|
|
{
|
|
|
|
asm volatile(
|
2024-03-06 02:19:59 +02:00
|
|
|
"movq %%cr4, %%rax;"
|
|
|
|
"orq $0x80, %%rax;"
|
|
|
|
"movq %%rax, %%cr4;"
|
|
|
|
::: "rax"
|
2023-07-13 15:08:54 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-03-06 02:19:59 +02:00
|
|
|
// enable write protect
|
2023-10-30 12:23:22 +02:00
|
|
|
asm volatile(
|
2024-03-06 02:19:59 +02:00
|
|
|
"movq %%cr0, %%rax;"
|
|
|
|
"orq $0x10000, %%rax;"
|
|
|
|
"movq %%rax, %%cr0;"
|
|
|
|
::: "rax"
|
2023-10-30 12:23:22 +02:00
|
|
|
);
|
|
|
|
|
2024-03-06 02:19:59 +02:00
|
|
|
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-09-18 21:37:37 +03:00
|
|
|
bool PageTable::is_valid_pointer(uintptr_t pointer)
|
|
|
|
{
|
|
|
|
if (!is_canonical(pointer))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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());
|
2024-01-24 14:43:46 +02:00
|
|
|
|
2023-07-21 13:29:46 +03:00
|
|
|
uint64_t* pml4 = (uint64_t*)P2V(m_highest_paging_struct);
|
|
|
|
pml4[511] = s_global_pml4e;
|
2023-07-05 23:41:35 +03:00
|
|
|
|
2023-10-30 19:02:09 +02:00
|
|
|
prepare_fast_page();
|
|
|
|
|
2023-11-17 18:54:59 +02:00
|
|
|
// Map main bios area below 1 MiB
|
|
|
|
map_range_at(
|
|
|
|
0x000E0000,
|
|
|
|
P2V(0x000E0000),
|
|
|
|
0x00100000 - 0x000E0000,
|
|
|
|
PageTable::Flags::Present
|
|
|
|
);
|
|
|
|
|
2023-10-30 15:33:18 +02:00
|
|
|
// Map (phys_kernel_start -> phys_kernel_end) to (virt_kernel_start -> virt_kernel_end)
|
|
|
|
ASSERT((vaddr_t)g_kernel_start % PAGE_SIZE == 0);
|
|
|
|
map_range_at(
|
|
|
|
V2P(g_kernel_start),
|
|
|
|
(vaddr_t)g_kernel_start,
|
|
|
|
g_kernel_end - g_kernel_start,
|
|
|
|
Flags::ReadWrite | Flags::Present
|
|
|
|
);
|
2023-10-17 01:06:24 +03:00
|
|
|
|
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-10-30 19:02:09 +02:00
|
|
|
void PageTable::prepare_fast_page()
|
|
|
|
{
|
|
|
|
constexpr vaddr_t uc_vaddr = uncanonicalize(fast_page());
|
|
|
|
constexpr uint64_t pml4e = (uc_vaddr >> 39) & 0x1FF;
|
|
|
|
constexpr uint64_t pdpte = (uc_vaddr >> 30) & 0x1FF;
|
|
|
|
constexpr uint64_t pde = (uc_vaddr >> 21) & 0x1FF;
|
|
|
|
constexpr uint64_t pte = (uc_vaddr >> 12) & 0x1FF;
|
|
|
|
|
|
|
|
uint64_t* pml4 = (uint64_t*)P2V(m_highest_paging_struct);
|
|
|
|
ASSERT(!(pml4[pml4e] & Flags::Present));
|
|
|
|
pml4[pml4e] = V2P(allocate_zeroed_page_aligned_page()) | Flags::ReadWrite | Flags::Present;
|
|
|
|
|
|
|
|
uint64_t* pdpt = (uint64_t*)P2V(pml4[pml4e] & PAGE_ADDR_MASK);
|
|
|
|
ASSERT(!(pdpt[pdpte] & Flags::Present));
|
|
|
|
pdpt[pdpte] = V2P(allocate_zeroed_page_aligned_page()) | Flags::ReadWrite | Flags::Present;
|
|
|
|
|
|
|
|
uint64_t* pd = (uint64_t*)P2V(pdpt[pdpte] & PAGE_ADDR_MASK);
|
|
|
|
ASSERT(!(pd[pde] & Flags::Present));
|
|
|
|
pd[pde] = V2P(allocate_zeroed_page_aligned_page()) | Flags::ReadWrite | Flags::Present;
|
|
|
|
|
|
|
|
uint64_t* pt = (uint64_t*)P2V(pd[pde] & PAGE_ADDR_MASK);
|
|
|
|
ASSERT(!(pt[pte] & Flags::Present));
|
|
|
|
pt[pte] = V2P(allocate_zeroed_page_aligned_page());
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageTable::map_fast_page(paddr_t paddr)
|
|
|
|
{
|
|
|
|
ASSERT(s_kernel);
|
2024-03-04 11:41:54 +02:00
|
|
|
ASSERT(paddr);
|
2024-02-29 11:19:10 +02:00
|
|
|
|
2024-05-24 14:12:35 +03:00
|
|
|
ASSERT(s_fast_page_lock.current_processor_has_lock());
|
2023-10-30 19:02:09 +02:00
|
|
|
|
|
|
|
constexpr vaddr_t uc_vaddr = uncanonicalize(fast_page());
|
|
|
|
constexpr uint64_t pml4e = (uc_vaddr >> 39) & 0x1FF;
|
|
|
|
constexpr uint64_t pdpte = (uc_vaddr >> 30) & 0x1FF;
|
|
|
|
constexpr uint64_t pde = (uc_vaddr >> 21) & 0x1FF;
|
|
|
|
constexpr uint64_t pte = (uc_vaddr >> 12) & 0x1FF;
|
|
|
|
|
|
|
|
uint64_t* pml4 = (uint64_t*)P2V(s_kernel->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);
|
|
|
|
|
|
|
|
ASSERT(!(pt[pte] & Flags::Present));
|
|
|
|
pt[pte] = paddr | Flags::ReadWrite | Flags::Present;
|
|
|
|
|
|
|
|
invalidate(fast_page());
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageTable::unmap_fast_page()
|
|
|
|
{
|
|
|
|
ASSERT(s_kernel);
|
2024-02-29 11:19:10 +02:00
|
|
|
|
2024-05-24 14:12:35 +03:00
|
|
|
ASSERT(s_fast_page_lock.current_processor_has_lock());
|
2023-10-30 19:02:09 +02:00
|
|
|
|
|
|
|
constexpr vaddr_t uc_vaddr = uncanonicalize(fast_page());
|
|
|
|
constexpr uint64_t pml4e = (uc_vaddr >> 39) & 0x1FF;
|
|
|
|
constexpr uint64_t pdpte = (uc_vaddr >> 30) & 0x1FF;
|
|
|
|
constexpr uint64_t pde = (uc_vaddr >> 21) & 0x1FF;
|
|
|
|
constexpr uint64_t pte = (uc_vaddr >> 12) & 0x1FF;
|
|
|
|
|
|
|
|
uint64_t* pml4 = (uint64_t*)P2V(s_kernel->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);
|
|
|
|
|
|
|
|
ASSERT(pt[pte] & Flags::Present);
|
|
|
|
pt[pte] = 0;
|
|
|
|
|
|
|
|
invalidate(fast_page());
|
|
|
|
}
|
|
|
|
|
2023-05-29 21:06:09 +03:00
|
|
|
BAN::ErrorOr<PageTable*> PageTable::create_userspace()
|
2023-04-28 14:45:09 +03:00
|
|
|
{
|
2024-02-28 22:39:02 +02:00
|
|
|
SpinLockGuard _(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
|
|
|
{
|
2024-03-03 02:17:24 +02:00
|
|
|
SpinLockGuard _(m_lock);
|
2023-04-28 14:45:09 +03:00
|
|
|
asm volatile("movq %0, %%cr3" :: "r"(m_highest_paging_struct));
|
2024-03-07 16:05:29 +02:00
|
|
|
Processor::set_current_page_table(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-10-30 19:02:09 +02:00
|
|
|
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-10-30 19:02:09 +02:00
|
|
|
ASSERT(vaddr);
|
|
|
|
ASSERT(vaddr != fast_page());
|
|
|
|
if (vaddr >= KERNEL_OFFSET)
|
2024-03-04 11:41:54 +02:00
|
|
|
ASSERT(vaddr >= (vaddr_t)g_kernel_start);
|
2023-10-30 19:02:09 +02:00
|
|
|
if ((vaddr >= KERNEL_OFFSET) != (this == s_kernel))
|
2023-07-05 23:41:35 +03:00
|
|
|
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;
|
|
|
|
|
2024-02-28 22:39:02 +02:00
|
|
|
SpinLockGuard _(m_lock);
|
2023-08-02 22:09:14 +03: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* 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);
|
|
|
|
|
2024-02-28 22:39:02 +02:00
|
|
|
SpinLockGuard _(m_lock);
|
2023-08-02 22:09:14 +03:00
|
|
|
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-10-30 19:02:09 +02:00
|
|
|
ASSERT(vaddr);
|
|
|
|
ASSERT(vaddr != fast_page());
|
|
|
|
if ((vaddr >= KERNEL_OFFSET) != (this == s_kernel))
|
2023-07-05 23:41:35 +03:00
|
|
|
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;
|
2023-08-06 23:59:30 +03:00
|
|
|
if (s_has_pge && pml4e == 511) // Map kernel memory as global
|
|
|
|
extra_flags |= 1ull << 8;
|
2023-07-21 13:29:46 +03:00
|
|
|
if (s_has_nxe && !(flags & Flags::Execute))
|
|
|
|
extra_flags |= 1ull << 63;
|
2023-08-01 16:32:51 +03:00
|
|
|
if (flags & Flags::Reserved)
|
|
|
|
extra_flags |= Flags::Reserved;
|
2023-08-06 23:59:30 +03:00
|
|
|
if (flags & Flags::CacheDisable)
|
|
|
|
extra_flags |= Flags::CacheDisable;
|
|
|
|
|
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;
|
|
|
|
|
2024-02-28 22:39:02 +02:00
|
|
|
SpinLockGuard _(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-10-30 19:02:09 +02:00
|
|
|
ASSERT(vaddr);
|
2023-05-30 22:21:12 +03:00
|
|
|
ASSERT(paddr % PAGE_SIZE == 0);
|
|
|
|
ASSERT(vaddr % PAGE_SIZE == 0);
|
|
|
|
|
2023-10-30 19:02:09 +02:00
|
|
|
size_t page_count = range_page_count(vaddr, size);
|
2024-01-24 14:43:46 +02:00
|
|
|
|
2024-02-28 22:39:02 +02:00
|
|
|
SpinLockGuard _(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;
|
2024-01-24 14:43:46 +02:00
|
|
|
|
2024-02-28 22:39:02 +02:00
|
|
|
SpinLockGuard _(m_lock);
|
2023-08-02 22:09:14 +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-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
|
|
|
{
|
2024-02-28 22:39:02 +02:00
|
|
|
SpinLockGuard _(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
|
|
|
|
2024-02-28 22:39:02 +02:00
|
|
|
SpinLockGuard _(m_lock);
|
2023-08-02 22:09:14 +03:00
|
|
|
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-10-30 15:33:18 +02:00
|
|
|
if (first_address >= KERNEL_OFFSET && first_address < (vaddr_t)g_kernel_end)
|
|
|
|
first_address = (vaddr_t)g_kernel_end;
|
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;
|
2024-01-24 14:43:46 +02:00
|
|
|
|
2024-02-28 22:39:02 +02:00
|
|
|
SpinLockGuard _(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;
|
2023-10-30 19:20:17 +02:00
|
|
|
vaddr = canonicalize(vaddr);
|
2023-08-02 22:09:14 +03:00
|
|
|
ASSERT(reserve_page(vaddr));
|
2023-10-30 19:20:17 +02:00
|
|
|
return 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-10-30 19:02:09 +02:00
|
|
|
if (first_address >= KERNEL_OFFSET && first_address < (vaddr_t)g_kernel_start)
|
|
|
|
first_address = (vaddr_t)g_kernel_start;
|
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
|
|
|
|
2024-02-28 22:39:02 +02:00
|
|
|
SpinLockGuard _(m_lock);
|
2023-05-28 16:18:18 +03:00
|
|
|
|
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
|
|
|
|
2024-02-28 22:39:02 +02:00
|
|
|
SpinLockGuard _(m_lock);
|
2023-08-02 22:09:14 +03:00
|
|
|
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()
|
|
|
|
{
|
2024-02-28 22:39:02 +02:00
|
|
|
SpinLockGuard _(m_lock);
|
2023-06-03 18:54:48 +03:00
|
|
|
|
|
|
|
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;
|
2024-01-24 14:43:46 +02:00
|
|
|
|
2023-06-03 18:54:48 +03:00
|
|
|
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
|
|
|
}
|