I'm trying to create the kernel a new pagemap but when switching to it, it 0xe's when it switches to the new pagemap and the error code is 0x10
Code: Select all
#define EXECUTEDISABLE (1ul << 63)
#define PRESENT (1ul)
#define RWALLOWED (1ul << 1)
#define USERMODE (1ul << 2)
#define CACHEDISABLE (1ul << 4)
#define PATBIT4096 (1ul << 7)
#define PATBIT2MB (1ul << 12)
#define PAGE2MB (1ul << 7)
#define WRITETHROUGH (1ul << 3)
uint64_t *find_pte_and_allocate(uint64_t *pt, uint64_t virt) {
uint64_t shift = 48;
for (int i = 0; i < 4; i++) {
shift -= 9;
uint64_t idx = (virt >> shift) & 0x1ff;
uint64_t *page_table =
(uint64_t *)((uint64_t)pt + hhdm_request.response->offset);
if (i == 3) {
return (uint64_t *)((uint64_t)page_table + idx);
}
if (page_table[idx] & ~PRESENT) {
uint64_t *guy =
(uint64_t *)((uint64_t)pmm_alloc() - hhdm_request.response->offset);
page_table[idx] = (uint64_t)guy | PRESENT | RWALLOWED;
pt = guy;
} else if (page_table[idx] & PAGE2MB) {
uint64_t *guy =
(uint64_t *)((uint64_t)pmm_alloc() - hhdm_request.response->offset);
uint64_t old_phys = page_table[idx] & 0x000ffffffffff000;
uint64_t old_flags = page_table[idx] & ~0x000ffffffffff000;
for (int j = 0; j < 512; j++) {
guy[j] = (old_phys + j * 4096) | (old_flags & ~PAGE2MB);
}
} else {
pt = (uint64_t *)(page_table[idx] & 0x000ffffffffff000);
}
}
return 0;
}
uint64_t *find_pte_and_allocate2mb(uint64_t *pt, uint64_t virt) {
uint64_t shift = 48;
for (int i = 0; i < 4; i++) {
shift -= 9;
uint64_t idx = (virt >> shift) & 0x1ff;
uint64_t *page_table =
(uint64_t *)((uint64_t)pt + hhdm_request.response->offset);
if (i == 2) {
return (uint64_t *)((uint64_t)page_table + idx);
}
if (page_table[idx] & ~PRESENT) {
uint64_t *guy =
(uint64_t *)((uint64_t)pmm_alloc() - hhdm_request.response->offset);
page_table[idx] = (uint64_t)guy | PRESENT | RWALLOWED;
pt = guy;
} else {
pt = (uint64_t *)(page_table[idx] & 0x000ffffffffff000);
}
}
return 0;
}
uint64_t *find_pte(uint64_t *pt, uint64_t virt) {
uint64_t shift = 48;
for (int i = 0; i < 4; i++) {
shift -= 9;
uint64_t idx = (virt >> shift) & 0x1ff;
uint64_t *page_table =
(uint64_t *)((uint64_t)pt + hhdm_request.response->offset);
if (i == 2) {
return (uint64_t *)((uint64_t)page_table + idx);
}
if (page_table[idx] & ~PRESENT) {
return (uint64_t *)((uint64_t)page_table + idx);
} else {
pt = (uint64_t *)(page_table[idx] & 0x000ffffffffff000);
}
}
return 0;
}
void map(uint64_t *pt, uint64_t phys, uint64_t virt, uint64_t flags) {
uint64_t *f = find_pte_and_allocate(pt, virt);
*f = phys | flags;
}
void map2mb(uint64_t *pt, uint64_t phys, uint64_t virt, uint64_t flags) {
uint64_t *f = find_pte_and_allocate2mb(pt, virt);
*f = (phys & ~0x1ffffful) | flags | PAGE2MB;
}
https://github.com/rayanmargham/NyauxKC ... /mem/vmm.c
more context and code here