107 lines
2.7 KiB
C++
107 lines
2.7 KiB
C++
|
|
#include "pagetable.h"
|
|
#include "pageframealloc.h"
|
|
#include "../utils/string.h"
|
|
|
|
void load_cr3( void* cr3_value ) {
|
|
asm volatile("mov %0, %%cr3" :: "r"((uint64_t)cr3_value) : "memory");
|
|
}
|
|
|
|
void write_identity_map(pagetable_t* identity_table, uint64_t size){
|
|
for(uintptr_t i=0; i<size/4096; i++){
|
|
identity_table[i/512].entries[i%512] = {
|
|
.present = 1,
|
|
.write = 1,
|
|
.user = 0,
|
|
.address = i,
|
|
};
|
|
}
|
|
}
|
|
|
|
void create_basic_page_table(four_lvl_paging_t* table, pagetable_t* kernel_identity){
|
|
|
|
assert(table);
|
|
assert(kernel_identity);
|
|
|
|
table->l4 = (pagetable_t*)PageFrameAllocator::alloc(true);
|
|
table->l3 = (pagetable_t*)PageFrameAllocator::alloc(true);
|
|
table->l2 = (pagetable_t*)PageFrameAllocator::alloc(true);
|
|
|
|
//memset(table, 0, sizeof(four_lvl_paging_t));
|
|
table->l4->entries[0] = {
|
|
.present = 1,
|
|
.write = 1,
|
|
.user = 1,
|
|
.address = ((uintptr_t)(table->l3)) >> 12,
|
|
};
|
|
|
|
table->l3->entries[0] = {
|
|
.present = 1,
|
|
.write = 1,
|
|
.user = 1,
|
|
.address = ((uintptr_t)(table->l2)) >> 12,
|
|
};
|
|
|
|
//kernel memory
|
|
for(uint32_t i=0; i<KERNEL_MEMORY_BORDER/4096/512; i++){
|
|
table->l2->entries[i] = {
|
|
.present = 1,
|
|
.write = 1,
|
|
.user = 1,
|
|
.address = (uintptr_t)(&kernel_identity[i]) >> 12,
|
|
};
|
|
}
|
|
|
|
//write_identity_map(table->l1, KERNEL_MEMORY_BORDER);
|
|
//for(uintptr_t i=0; i<KERNEL_MEMORY_BORDER/4096; i++){
|
|
// table->l1[i/512].entries[i%512] = {
|
|
// .present = 1,
|
|
// .write = 1,
|
|
// .user = 0,
|
|
// .address = i,
|
|
// };
|
|
//}
|
|
|
|
//add ioapic address to pagetable
|
|
table->l2_apic = (pagetable_t*)PageFrameAllocator::alloc(true);
|
|
table->ioapic = (pagetable_t*)PageFrameAllocator::alloc(true);
|
|
table->lapic = (pagetable_t*)PageFrameAllocator::alloc(true);
|
|
|
|
table->l3->entries[3] = {
|
|
.present = 1,
|
|
.write = 1,
|
|
.user = 1,
|
|
.address = (uintptr_t)(table->l2_apic) >> 12,
|
|
};
|
|
table->l2_apic->entries[502] = {
|
|
.present = 1,
|
|
.write = 1,
|
|
.user = 1,
|
|
.address = (uintptr_t)(table->ioapic) >> 12,
|
|
};
|
|
table->ioapic->entries[0] = {
|
|
.present = 1,
|
|
.write = 1,
|
|
.user = 0,
|
|
.address = 0xFEC00,
|
|
};
|
|
|
|
//lapic
|
|
table->l2_apic->entries[503] = {
|
|
.present = 1,
|
|
.write = 1,
|
|
.user = 1,
|
|
.address = (uintptr_t)(table->lapic) >> 12,
|
|
};
|
|
table->lapic->entries[0] = {
|
|
.present = 1,
|
|
.write = 1,
|
|
.user = 0,
|
|
.address = 0xFEE00,
|
|
};
|
|
|
|
//unmap 0 page
|
|
kernel_identity[0].entries[0].present = 0;
|
|
}
|
|
|