Implemented and fixed paging

This commit is contained in:
Simon 2024-01-27 03:26:21 -05:00
parent 1a3c3cacc3
commit 79bd0a643e
2 changed files with 18 additions and 34 deletions

View file

@ -22,49 +22,44 @@
// PAGE MAP LEVEL 4 ENTRY // PAGE MAP LEVEL 4 ENTRY
struct pml4e { struct pml4e {
uint64_t flags : 6; uint64_t flags : 6;
uint64_t _reserved : 6; uint64_t : 6;
uint64_t address : 40; uint64_t address : 40;
uint64_t _reserved_2 : 11; uint64_t : 11;
uint64_t execute_disable : 1; uint64_t execute_disable : 1;
}; };
// PAGE DIRECTORY POINTER TABLE ENTRY // PAGE DIRECTORY POINTER TABLE ENTRY
struct pdpte { struct pdpte {
uint64_t flags : 6; uint64_t flags : 6;
uint64_t _reserved : 1; uint64_t : 1;
uint64_t page_size : 1; uint64_t page_size : 1;
uint64_t _reserved_2 : 2; uint64_t : 4;
uint64_t address : 40; uint64_t address : 40;
uint64_t _reserved_3 : 11; uint64_t : 11;
uint64_t execute_disable : 1; uint64_t execute_disable : 1;
}; };
// PAGE DIRECTORY ENTRY // PAGE DIRECTORY ENTRY
struct pde { struct pde {
uint64_t flags : 6; uint64_t flags : 6;
uint64_t _reserved : 1; uint64_t : 1;
uint64_t page_size : 1; uint64_t page_size : 1;
uint64_t _reserved_2 : 2; uint64_t : 4;
uint64_t address : 40; uint64_t address : 40;
uint64_t _reserved_3 : 11; uint64_t : 11;
uint64_t execute_disable : 1; uint64_t execute_disable : 1;
}; };
// PAGE TABLE ENTRY // PAGE TABLE ENTRY
struct pte { struct pte {
uint64_t flags : 9; uint64_t flags : 9;
uint64_t _reserved : 3; uint64_t : 3;
uint64_t address : 40; uint64_t address : 40;
uint64_t _reserved_2 : 7; uint64_t : 7;
uint64_t protection_key : 4; uint64_t protection_key : 4;
uint64_t execute_disable : 1; uint64_t execute_disable : 1;
}; };
struct pml4e pml4[512];
struct pdpte pdpt[512];
struct pde pd[512];
struct pte pt[512];
static int get_maxphysaddr() { static int get_maxphysaddr() {
uint32_t eax, ebx, ecx, edx; uint32_t eax, ebx, ecx, edx;
__cpuid(0x80000008, eax, ebx, ecx, edx); __cpuid(0x80000008, eax, ebx, ecx, edx);
@ -73,23 +68,11 @@ static int get_maxphysaddr() {
// entry point for amd64 // entry point for amd64
void* amd64_shim(void *boot_info) { void* amd64_shim(void *boot_info) {
for (int i = 0; i < 512; i++) { struct pml4e *pml4 = (struct pml4e *)0x1000;
pml4[i].flags = 0; struct pdpte *pdpt = (struct pdpte *)0x2000;
} struct pde *pd = (struct pd *)0x3000;
for (int i = 0; i < 512; i++) { struct pte *pt = (struct pt *)0x4000;
pdpt[i].flags = 0; pd[1].flags = F_PRESENT | F_WRITEABLE;
} pd[1].address = ((uint64_t)pt) >> 12;
for (int i = 0; i < 512; i++) { __asm("invlpg 0x200000");
pd[i].flags = 0;
}
pml4[0].flags = F_PRESENT | F_WRITEABLE;
pml4[0].address = (uint64_t)&pdpt;
pdpt[0].flags = F_PRESENT | F_WRITEABLE;
pdpt[0].address = (uint64_t)&pd;
pd[0].flags = F_PRESENT | F_WRITEABLE;
pd[0].address = (uint64_t)&pt;
for (int i = 0; i < 512; i++) {
pt[i].flags = F_PRESENT | F_WRITEABLE;
pt[i].address = i * 4096;
}
} }

View file

@ -1,5 +1,6 @@
void kmain() { void kmain() {
*(char*)0xB8000 = 'c'; *(char*)0xB8000 = 'c';
*(char*)(0xB8002 + 0x20'0000) = 'd';
while(1) { while(1) {
__asm("cli; hlt"); __asm("cli; hlt");
} }