2023-01-12 13:20:38 +02:00
|
|
|
#include <BAN/Errors.h>
|
2023-01-25 21:39:03 +02:00
|
|
|
#include <kernel/Debug.h>
|
2023-01-12 13:20:38 +02:00
|
|
|
#include <kernel/MMU.h>
|
|
|
|
#include <kernel/kmalloc.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2023-01-13 00:04:50 +02:00
|
|
|
#define MMU_DEBUG_PRINT 0
|
2023-01-12 13:46:02 +02:00
|
|
|
|
2023-01-12 13:20:38 +02:00
|
|
|
// bits 31-12 set
|
|
|
|
#define PAGE_MASK 0xfffff000
|
|
|
|
#define PAGE_SIZE 0x00001000
|
|
|
|
|
|
|
|
static MMU* s_instance = nullptr;
|
|
|
|
|
2023-02-01 21:05:44 +02:00
|
|
|
void MMU::intialize()
|
2023-01-12 13:20:38 +02:00
|
|
|
{
|
|
|
|
ASSERT(s_instance == nullptr);
|
|
|
|
s_instance = new MMU();
|
|
|
|
}
|
|
|
|
|
2023-02-01 21:05:44 +02:00
|
|
|
MMU& MMU::get()
|
2023-01-12 13:20:38 +02:00
|
|
|
{
|
|
|
|
ASSERT(s_instance);
|
|
|
|
return *s_instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t* allocate_page_aligned_page()
|
|
|
|
{
|
2023-01-22 20:16:44 +02:00
|
|
|
uint64_t* page = (uint64_t*)kmalloc(PAGE_SIZE, PAGE_SIZE);
|
|
|
|
ASSERT(page);
|
|
|
|
ASSERT(((uintptr_t)page % PAGE_SIZE) == 0);
|
|
|
|
memset(page, 0, PAGE_SIZE);
|
|
|
|
return page;
|
2023-01-12 13:20:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MMU::MMU()
|
|
|
|
{
|
2023-01-22 03:03:01 +02:00
|
|
|
m_highest_paging_struct = (uint64_t*)kmalloc(sizeof(uint64_t) * 4, 32);
|
|
|
|
ASSERT(m_highest_paging_struct);
|
|
|
|
ASSERT(((uintptr_t)m_highest_paging_struct % 32) == 0);
|
2023-01-12 13:20:38 +02:00
|
|
|
|
2023-01-12 13:46:02 +02:00
|
|
|
// allocate all page directories
|
2023-01-12 13:20:38 +02:00
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
uint64_t* page_directory = allocate_page_aligned_page();
|
2023-03-01 20:15:58 +02:00
|
|
|
m_highest_paging_struct[i] = (uint64_t)page_directory | Flags::Present;
|
2023-01-12 13:20:38 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 23:38:05 +02:00
|
|
|
// create and identity map first 6 MiB
|
2023-01-22 03:03:01 +02:00
|
|
|
uint64_t* page_directory1 = (uint64_t*)(m_highest_paging_struct[0] & PAGE_MASK);
|
2023-03-06 23:38:05 +02:00
|
|
|
for (uint64_t i = 0; i < 3; i++)
|
2023-01-12 13:20:38 +02:00
|
|
|
{
|
|
|
|
uint64_t* page_table = allocate_page_aligned_page();
|
|
|
|
for (uint64_t j = 0; j < 512; j++)
|
2023-03-01 20:15:58 +02:00
|
|
|
page_table[j] = (i << 21) | (j << 12) | Flags::ReadWrite | Flags::Present;
|
2023-01-12 13:20:38 +02:00
|
|
|
|
2023-03-01 20:15:58 +02:00
|
|
|
page_directory1[i] = (uint64_t)page_table | Flags::ReadWrite | Flags::Present;
|
2023-01-12 13:20:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// dont map first page (0 -> 4 KiB) so that nullptr dereference
|
|
|
|
// causes page fault :)
|
2023-01-13 14:45:45 +02:00
|
|
|
uint64_t* page_table1 = (uint64_t*)(page_directory1[0] & PAGE_MASK);
|
2023-01-12 13:20:38 +02:00
|
|
|
page_table1[0] = 0;
|
|
|
|
|
|
|
|
// reload this new pdpt
|
2023-01-22 03:03:01 +02:00
|
|
|
asm volatile("movl %0, %%cr3" :: "r"(m_highest_paging_struct));
|
2023-01-12 13:20:38 +02:00
|
|
|
}
|
|
|
|
|
2023-03-01 20:15:58 +02:00
|
|
|
void MMU::allocate_page(uintptr_t address, uint8_t flags)
|
2023-01-12 13:20:38 +02:00
|
|
|
{
|
2023-01-12 13:46:02 +02:00
|
|
|
#if MMU_DEBUG_PRINT
|
2023-03-02 00:47:29 +02:00
|
|
|
dprintln("AllocatePage(0x{8H})", address);
|
2023-01-12 13:46:02 +02:00
|
|
|
#endif
|
2023-03-01 20:15:58 +02:00
|
|
|
ASSERT(flags & Flags::Present);
|
2023-01-12 13:46:02 +02:00
|
|
|
|
2023-03-02 00:47:29 +02:00
|
|
|
address &= PAGE_MASK;
|
|
|
|
|
2023-01-12 13:20:38 +02:00
|
|
|
uint32_t pdpte = (address & 0xC0000000) >> 30;
|
|
|
|
uint32_t pde = (address & 0x3FE00000) >> 21;
|
|
|
|
uint32_t pte = (address & 0x001FF000) >> 12;
|
|
|
|
|
2023-01-22 03:03:01 +02:00
|
|
|
uint64_t* page_directory = (uint64_t*)(m_highest_paging_struct[pdpte] & PAGE_MASK);
|
2023-03-01 20:15:58 +02:00
|
|
|
if (!(page_directory[pde] & Flags::Present))
|
2023-01-12 13:20:38 +02:00
|
|
|
{
|
|
|
|
uint64_t* page_table = allocate_page_aligned_page();
|
2023-03-01 20:15:58 +02:00
|
|
|
page_directory[pde] = (uint64_t)page_table;
|
2023-01-12 13:20:38 +02:00
|
|
|
}
|
2023-03-01 20:15:58 +02:00
|
|
|
page_directory[pde] |= flags;
|
2023-01-12 13:20:38 +02:00
|
|
|
|
|
|
|
uint64_t* page_table = (uint64_t*)(page_directory[pde] & PAGE_MASK);
|
2023-03-02 00:47:29 +02:00
|
|
|
page_table[pte] = address | flags;
|
2023-01-12 13:20:38 +02:00
|
|
|
|
2023-03-02 00:47:29 +02:00
|
|
|
asm volatile("invlpg (%0)" :: "r"(address) : "memory");
|
2023-01-12 13:20:38 +02:00
|
|
|
}
|
|
|
|
|
2023-03-01 20:15:58 +02:00
|
|
|
void MMU::allocate_range(uintptr_t address, ptrdiff_t size, uint8_t flags)
|
2023-01-12 13:20:38 +02:00
|
|
|
{
|
|
|
|
uintptr_t s_page = address & PAGE_MASK;
|
|
|
|
uintptr_t e_page = (address + size - 1) & PAGE_MASK;
|
|
|
|
for (uintptr_t page = s_page; page <= e_page; page += PAGE_SIZE)
|
2023-03-01 20:15:58 +02:00
|
|
|
allocate_page(page, flags);
|
2023-01-12 13:20:38 +02:00
|
|
|
}
|
2023-01-12 13:45:01 +02:00
|
|
|
|
2023-02-01 21:05:44 +02:00
|
|
|
void MMU::unallocate_page(uintptr_t address)
|
2023-01-12 13:45:01 +02:00
|
|
|
{
|
2023-01-12 13:46:02 +02:00
|
|
|
#if MMU_DEBUG_PRINT
|
|
|
|
dprintln("UnAllocatePage(0x{8H})", address & PAGE_MASK);
|
|
|
|
#endif
|
|
|
|
|
2023-01-12 13:45:01 +02:00
|
|
|
uint32_t pdpte = (address & 0xC0000000) >> 30;
|
|
|
|
uint32_t pde = (address & 0x3FE00000) >> 21;
|
|
|
|
uint32_t pte = (address & 0x001FF000) >> 12;
|
|
|
|
|
2023-01-22 03:03:01 +02:00
|
|
|
uint64_t* page_directory = (uint64_t*)(m_highest_paging_struct[pdpte] & PAGE_MASK);
|
2023-03-01 20:15:58 +02:00
|
|
|
if (!(page_directory[pde] & Flags::Present))
|
2023-01-26 02:35:11 +02:00
|
|
|
return;
|
2023-01-12 13:45:01 +02:00
|
|
|
|
|
|
|
uint64_t* page_table = (uint64_t*)(page_directory[pde] & PAGE_MASK);
|
2023-03-01 20:15:58 +02:00
|
|
|
if (!(page_table[pte] & Flags::Present))
|
2023-01-26 02:35:11 +02:00
|
|
|
return;
|
2023-01-12 13:45:01 +02:00
|
|
|
|
|
|
|
page_table[pte] = 0;
|
|
|
|
|
|
|
|
// TODO: Unallocate the page table if this was the only allocated page
|
|
|
|
|
|
|
|
asm volatile("invlpg (%0)" :: "r"(address & PAGE_MASK) : "memory");
|
|
|
|
}
|
|
|
|
|
2023-02-01 21:05:44 +02:00
|
|
|
void MMU::unallocate_range(uintptr_t address, ptrdiff_t size)
|
2023-01-12 13:45:01 +02:00
|
|
|
{
|
|
|
|
uintptr_t s_page = address & PAGE_MASK;
|
|
|
|
uintptr_t e_page = (address + size - 1) & PAGE_MASK;
|
|
|
|
for (uintptr_t page = s_page; page <= e_page; page += PAGE_SIZE)
|
2023-02-01 21:05:44 +02:00
|
|
|
unallocate_page(page);
|
2023-01-12 13:45:01 +02:00
|
|
|
}
|