2014-06-30 22:52:35 +00:00
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2014-07-22 06:43:01 +00:00
|
|
|
#include <sys/kconfig.h>
|
|
|
|
#include <sys/kassert.h>
|
|
|
|
#include <sys/kdebug.h>
|
|
|
|
#include <sys/kmem.h>
|
2014-06-30 22:52:35 +00:00
|
|
|
|
2014-07-22 06:43:01 +00:00
|
|
|
#include <machine/amd64.h>
|
|
|
|
#include <machine/amd64op.h>
|
|
|
|
#include <machine/mp.h>
|
|
|
|
#include <machine/pmap.h>
|
2014-06-30 22:52:35 +00:00
|
|
|
|
|
|
|
typedef struct AS
|
|
|
|
{
|
|
|
|
PageTable *root;
|
|
|
|
uint64_t tables;
|
|
|
|
uint64_t mappings;
|
|
|
|
} AS;
|
|
|
|
|
|
|
|
AS systemAS;
|
|
|
|
AS *currentAS[MAX_CPUS];
|
|
|
|
|
|
|
|
void
|
|
|
|
PMap_Init()
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
kprintf("Initializing PMAP ... ");
|
|
|
|
|
|
|
|
// Setup global state
|
|
|
|
for (i = 0; i < MAX_CPUS; i++) {
|
|
|
|
currentAS[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate system page table
|
|
|
|
systemAS.root = PAlloc_AllocPage();
|
|
|
|
systemAS.tables = PAGETABLE_ENTRIES / 2 + 1;
|
|
|
|
systemAS.mappings = 0;
|
|
|
|
if (!systemAS.root)
|
|
|
|
PANIC("Cannot allocate system page table");
|
|
|
|
|
2014-07-18 22:07:25 +00:00
|
|
|
for (i = 0; i < PAGETABLE_ENTRIES / 2; i++)
|
|
|
|
systemAS.root->entries[i] = 0;
|
|
|
|
|
2014-06-30 22:52:35 +00:00
|
|
|
for (i = PAGETABLE_ENTRIES / 2; i < PAGETABLE_ENTRIES; i++) {
|
|
|
|
PageTable *pgtbl = PAlloc_AllocPage();
|
2014-07-18 22:07:25 +00:00
|
|
|
PageEntry pte = DMVA2PA((uint64_t)pgtbl) | PTE_W | PTE_P;
|
2014-06-30 22:52:35 +00:00
|
|
|
if (!pgtbl)
|
|
|
|
PANIC("Not enough memory!");
|
|
|
|
|
|
|
|
systemAS.root->entries[i] = pte;
|
|
|
|
|
|
|
|
for (j = 0; j < PAGETABLE_ENTRIES; j++) {
|
|
|
|
pgtbl->entries[j] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup system mappings
|
2014-07-18 22:07:25 +00:00
|
|
|
PMap_SystemLMap(0x0, MEM_DIRECTMAP_BASE + 0x0,
|
|
|
|
3*512, 0); // 3GB RWX
|
|
|
|
PMap_SystemLMap(0xC0000000, MEM_DIRECTMAP_BASE + 0xC0000000,
|
|
|
|
512, PTE_NX|PTE_PCD); // 1GB RW + PCD
|
|
|
|
PMap_SystemLMap(0x100000000, MEM_DIRECTMAP_BASE + 0x100000000,
|
|
|
|
60*512, 0); // 60GB RWX
|
2014-07-01 01:35:20 +00:00
|
|
|
|
2014-07-18 22:07:25 +00:00
|
|
|
write_cr3(DMVA2PA((uint64_t)systemAS.root));
|
2014-06-30 22:52:35 +00:00
|
|
|
|
|
|
|
kprintf("Done!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
AS*
|
2014-07-07 17:57:57 +00:00
|
|
|
PMap_NewAS()
|
2014-06-30 22:52:35 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
AS *as = PAlloc_AllocPage();
|
|
|
|
|
|
|
|
if (!as)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
as->root = PAlloc_AllocPage();
|
|
|
|
as->tables = 1;
|
|
|
|
as->mappings = 0;
|
|
|
|
|
|
|
|
if (!as->root) {
|
|
|
|
PAlloc_FreePage(as);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-24 01:07:07 +00:00
|
|
|
for (i = 0; i < PAGETABLE_ENTRIES / 2; i++)
|
2014-06-30 22:52:35 +00:00
|
|
|
{
|
|
|
|
as->root->entries[i] = 0;
|
|
|
|
}
|
2014-07-24 01:07:07 +00:00
|
|
|
for (i = PAGETABLE_ENTRIES / 2; i < PAGETABLE_ENTRIES; i++) {
|
|
|
|
as->root->entries[i] = systemAS.root->entries[i];
|
|
|
|
}
|
2014-06-30 22:52:35 +00:00
|
|
|
|
|
|
|
return as;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PMap_DestroyAS(AS *space)
|
|
|
|
{
|
2014-07-24 01:07:07 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < PAGETABLE_ENTRIES / 2; i++)
|
|
|
|
{
|
|
|
|
if (space->root->entries[i] != 0) {
|
|
|
|
// Remove subpages
|
|
|
|
PAlloc_FreePage((void *)DMPA2VA(space->root->entries[i]));
|
|
|
|
}
|
|
|
|
}
|
2014-06-30 22:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PMap_LoadAS(AS *space)
|
|
|
|
{
|
2014-07-24 01:07:07 +00:00
|
|
|
write_cr3(DMVA2PA((uint64_t)space->root));
|
2014-06-30 22:52:35 +00:00
|
|
|
currentAS[THISCPU()] = space;
|
|
|
|
}
|
|
|
|
|
|
|
|
PageTable *
|
|
|
|
PMapAllocPageTable()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
PageTable *pgtbl = PAlloc_AllocPage();
|
|
|
|
|
|
|
|
if (!pgtbl)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0; i < PAGETABLE_ENTRIES; i++) {
|
|
|
|
pgtbl->entries[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pgtbl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PMapLookupEntry(AS *space, uint64_t va, PageEntry **entry, int size)
|
|
|
|
{
|
|
|
|
int i,j,k,l;
|
|
|
|
PageTable *table = space->root;
|
|
|
|
PageEntry pte;
|
|
|
|
|
|
|
|
i = (va >> (HUGE_PGSHIFT + PGIDXSHIFT)) & PGIDXMASK;
|
|
|
|
j = (va >> HUGE_PGSHIFT) & PGIDXMASK;
|
|
|
|
k = (va >> LARGE_PGSHIFT) & PGIDXMASK;
|
|
|
|
l = (va >> PGSHIFT) & PGIDXMASK;
|
|
|
|
|
|
|
|
*entry = NULL;
|
|
|
|
|
|
|
|
pte = table->entries[i];
|
|
|
|
if (pte == 0) {
|
|
|
|
PageTable *newtable = PMapAllocPageTable();
|
|
|
|
if (!newtable)
|
|
|
|
return;
|
|
|
|
|
2014-07-28 00:09:31 +00:00
|
|
|
pte = DMVA2PA((uint64_t)newtable) | PTE_P | PTE_W | PTE_U;
|
2014-06-30 22:52:35 +00:00
|
|
|
table->entries[i] = pte;
|
|
|
|
}
|
2014-07-18 22:07:25 +00:00
|
|
|
table = (PageTable *)DMPA2VA(pte & 0xFFFFFFFFFFFFF000);
|
2014-06-30 22:52:35 +00:00
|
|
|
|
|
|
|
pte = table->entries[j];
|
2014-07-17 07:44:13 +00:00
|
|
|
if (size == HUGE_PGSIZE) {
|
|
|
|
// Handle 1GB pages
|
|
|
|
*entry = &table->entries[j];
|
|
|
|
return;
|
|
|
|
}
|
2014-06-30 22:52:35 +00:00
|
|
|
if (pte == 0) {
|
|
|
|
PageTable *newtable = PMapAllocPageTable();
|
|
|
|
if (!newtable)
|
|
|
|
return;
|
|
|
|
|
2014-07-28 00:09:31 +00:00
|
|
|
pte = DMVA2PA((uint64_t)newtable) | PTE_P | PTE_W | PTE_U;
|
2014-06-30 22:52:35 +00:00
|
|
|
table->entries[j] = pte;
|
|
|
|
}
|
2014-07-18 22:07:25 +00:00
|
|
|
table = (PageTable *)DMPA2VA(pte & 0xFFFFFFFFFFFFF000);
|
2014-06-30 22:52:35 +00:00
|
|
|
|
|
|
|
pte = table->entries[k];
|
2014-07-17 07:44:13 +00:00
|
|
|
if (size == LARGE_PGSIZE) {
|
|
|
|
// Handle 2MB pages
|
|
|
|
*entry = &table->entries[k];
|
|
|
|
return;
|
|
|
|
}
|
2014-06-30 22:52:35 +00:00
|
|
|
if (pte == 0) {
|
|
|
|
PageTable *newtable = PMapAllocPageTable();
|
|
|
|
if (!newtable)
|
|
|
|
return;
|
|
|
|
|
2014-07-28 00:09:31 +00:00
|
|
|
pte = DMVA2PA((uint64_t)newtable) | PTE_P | PTE_W | PTE_U;
|
2014-06-30 22:52:35 +00:00
|
|
|
table->entries[k] = pte;
|
|
|
|
}
|
2014-07-18 22:07:25 +00:00
|
|
|
table = (PageTable *)DMPA2VA(pte & 0xFFFFFFFFFFFFF000);
|
2014-06-30 22:52:35 +00:00
|
|
|
|
|
|
|
// Handle 4KB pages
|
|
|
|
ASSERT(size == PGSIZE);
|
|
|
|
*entry = &table->entries[l];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-28 00:09:31 +00:00
|
|
|
bool
|
|
|
|
PMap_Map(AS *as, uint64_t phys, uint64_t virt, uint64_t pages, uint64_t flags)
|
2014-06-30 22:52:35 +00:00
|
|
|
{
|
2014-07-28 00:09:31 +00:00
|
|
|
int i;
|
|
|
|
PageEntry *entry;
|
2014-06-30 22:52:35 +00:00
|
|
|
|
2014-07-28 00:09:31 +00:00
|
|
|
for (i = 0; i < pages; i++) {
|
|
|
|
uint64_t va = virt + PGSIZE * i;
|
|
|
|
PMapLookupEntry(as, va, &entry, PGSIZE);
|
|
|
|
if (!entry) {
|
|
|
|
kprintf("Map failed to allocate memory!\n");
|
|
|
|
return false;
|
2014-06-30 22:52:35 +00:00
|
|
|
}
|
2014-07-28 00:09:31 +00:00
|
|
|
|
|
|
|
*entry = (phys + PGSIZE * i) | PTE_P | PTE_W | PTE_U | flags;
|
2014-06-30 22:52:35 +00:00
|
|
|
}
|
|
|
|
|
2014-07-28 00:09:31 +00:00
|
|
|
return true;
|
2014-06-30 22:52:35 +00:00
|
|
|
}
|
|
|
|
|
2014-08-08 00:58:41 +00:00
|
|
|
bool
|
|
|
|
PMap_Unmap(AS *as, uint64_t va, uint64_t pages)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
PageEntry *entry;
|
|
|
|
|
|
|
|
for (i = 0; i < pages; i++) {
|
2014-09-29 20:46:38 +00:00
|
|
|
uint64_t vai = va + PGSIZE * i;
|
|
|
|
PMapLookupEntry(as, vai, &entry, PGSIZE);
|
2014-08-08 00:58:41 +00:00
|
|
|
if (!entry) {
|
|
|
|
kprintf("Unmap tried to allocate memory!\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*entry = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-13 03:22:52 +00:00
|
|
|
bool
|
|
|
|
PMap_AllocMap(AS *as, uint64_t virt, uint64_t len, uint64_t flags)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint64_t pages = (len + PGSIZE - 1) / PGSIZE;
|
|
|
|
PageEntry *entry;
|
|
|
|
|
|
|
|
ASSERT((virt & PGMASK) == 0);
|
|
|
|
|
|
|
|
for (i = 0; i < pages; i++) {
|
|
|
|
uint64_t va = virt + PGSIZE * i;
|
|
|
|
PMapLookupEntry(as, va, &entry, PGSIZE);
|
|
|
|
if (!entry) {
|
|
|
|
kprintf("Map failed to allocate memory!\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((*entry & PTE_P) != PTE_P) {
|
|
|
|
void *pg = PAlloc_AllocPage();
|
|
|
|
*entry = (uint64_t)DMVA2PA(pg) | PTE_P | PTE_U | flags;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-17 00:21:18 +00:00
|
|
|
void
|
|
|
|
PMap_SystemLookup(uint64_t va, PageEntry **entry, int size)
|
|
|
|
{
|
|
|
|
PMapLookupEntry(&systemAS, va, entry, size);
|
|
|
|
}
|
|
|
|
|
2014-06-30 22:52:35 +00:00
|
|
|
bool
|
2014-07-01 01:35:20 +00:00
|
|
|
PMap_SystemLMap(uint64_t phys, uint64_t virt, uint64_t lpages, uint64_t flags)
|
2014-06-30 22:52:35 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
PageEntry *entry;
|
|
|
|
|
|
|
|
for (i = 0; i < lpages; i++) {
|
|
|
|
uint64_t va = virt + LARGE_PGSIZE * i;
|
|
|
|
PMapLookupEntry(&systemAS, va, &entry, LARGE_PGSIZE);
|
|
|
|
if (!entry) {
|
|
|
|
kprintf("SystemLMap failed to allocate memory!\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-01 01:35:20 +00:00
|
|
|
*entry = (phys + LARGE_PGSIZE * i) | PTE_P | PTE_W | PTE_PS | flags;
|
2014-06-30 22:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-07-28 00:09:31 +00:00
|
|
|
PMap_SystemMap(uint64_t phys, uint64_t virt, uint64_t pages, uint64_t flags)
|
2014-06-30 22:52:35 +00:00
|
|
|
{
|
2014-07-28 00:09:31 +00:00
|
|
|
int i;
|
|
|
|
PageEntry *entry;
|
|
|
|
|
|
|
|
for (i = 0; i < pages; i++) {
|
|
|
|
uint64_t va = virt + PGSIZE * i;
|
|
|
|
PMapLookupEntry(&systemAS, va, &entry, PGSIZE);
|
|
|
|
if (!entry) {
|
|
|
|
kprintf("SystemMap failed to allocate memory!\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*entry = (phys + PGSIZE * i) | PTE_P | PTE_W | flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2014-06-30 22:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PMap_SystemUnmap(uint64_t virt, uint64_t pages)
|
|
|
|
{
|
2014-07-07 17:57:57 +00:00
|
|
|
NOT_IMPLEMENTED();
|
|
|
|
return false;
|
2014-06-30 22:52:35 +00:00
|
|
|
}
|
|
|
|
|
2014-07-28 00:09:31 +00:00
|
|
|
uint64_t
|
|
|
|
AddrFromIJKL(uint64_t i, uint64_t j, uint64_t k, uint64_t l)
|
|
|
|
{
|
|
|
|
return (i << 39) | (j << HUGE_PGSHIFT) | (k << LARGE_PGSHIFT) | (l << PGSHIFT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PMap_Dump(AS *space)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
int j = 0;
|
|
|
|
int k = 0;
|
|
|
|
int l = 0;
|
|
|
|
PageTable *root = space->root;
|
|
|
|
|
|
|
|
kprintf("Root: %016llx\n", (uint64_t)space->root);
|
|
|
|
|
|
|
|
for (i = 0; i < PAGETABLE_ENTRIES; i++) {
|
|
|
|
PageEntry pte = root->entries[i];
|
|
|
|
PageTable *l1 = (PageTable *)DMPA2VA(pte & 0xFFFFFFFFFFFFF000);
|
|
|
|
|
|
|
|
if (!(pte & PTE_P))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
kprintf("Level 1: %016llx\n", (uint64_t)pte);
|
|
|
|
|
|
|
|
for (j = 0; j < PAGETABLE_ENTRIES; j++) {
|
2014-09-29 20:46:38 +00:00
|
|
|
PageEntry pte2 = l1->entries[j];
|
|
|
|
PageTable *l2 = (PageTable *)DMPA2VA(pte2 & 0xFFFFFFFFFFFFF000);
|
2014-07-28 00:09:31 +00:00
|
|
|
|
2014-09-29 20:46:38 +00:00
|
|
|
if (!(pte2 & PTE_P))
|
2014-07-28 00:09:31 +00:00
|
|
|
continue;
|
|
|
|
|
2014-09-29 20:46:38 +00:00
|
|
|
kprintf("Level 2: %016llx\n", (uint64_t)pte2);
|
2014-07-28 00:09:31 +00:00
|
|
|
|
|
|
|
for (k = 0; k < PAGETABLE_ENTRIES; k++) {
|
2014-09-29 20:46:38 +00:00
|
|
|
PageEntry pte3 = l2->entries[k];
|
|
|
|
PageTable *l3 = (PageTable *)DMPA2VA(pte3 & 0xFFFFFFFFFFFFF000);
|
2014-07-28 00:09:31 +00:00
|
|
|
|
2014-09-29 20:46:38 +00:00
|
|
|
if (!(pte3 & PTE_P))
|
2014-07-28 00:09:31 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
kprintf("Level 3: %016llx:%016llx\n",
|
|
|
|
AddrFromIJKL(i, j, k, l),
|
2014-09-29 20:46:38 +00:00
|
|
|
(uint64_t)pte3);
|
2014-07-28 00:09:31 +00:00
|
|
|
|
2014-09-29 20:46:38 +00:00
|
|
|
if ((pte3 & PTE_PS) == 0) {
|
2014-07-28 00:09:31 +00:00
|
|
|
for (l = 0; l < PAGETABLE_ENTRIES; l++) {
|
2014-09-29 20:46:38 +00:00
|
|
|
PageEntry pte4 = l3->entries[l];
|
2014-07-28 00:09:31 +00:00
|
|
|
|
|
|
|
kprintf("Level 4: %016llx:%016llx\n",
|
|
|
|
AddrFromIJKL(i, j, k, l),
|
2014-09-29 20:46:38 +00:00
|
|
|
(uint64_t)pte4);
|
2014-07-28 00:09:31 +00:00
|
|
|
}
|
|
|
|
l = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
k = 0;
|
|
|
|
}
|
|
|
|
j = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-17 00:21:18 +00:00
|
|
|
void
|
|
|
|
Debug_PMapDump(int argc, const char *argv[])
|
|
|
|
{
|
2014-07-28 00:09:31 +00:00
|
|
|
PMap_Dump(currentAS[THISCPU()]);
|
2014-07-17 00:21:18 +00:00
|
|
|
}
|
|
|
|
|
2014-07-20 20:45:57 +00:00
|
|
|
REGISTER_DBGCMD(pmapdump, "Dump memory mappings", Debug_PMapDump);
|
|
|
|
|