Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void map_addr(uint64_t virt, uint64_t phys, uint32_t flags)
- {
- virt -= virt % 0x00200000;
- phys -= phys % 0x00200000;
- // i is in range [0, 511]
- size_t pt_lvl4_i = (virt >> 39) % 0x200; // 512gb entry
- size_t pt_lvl3_i = (virt >> 30) % 0x200; // 1gb entry
- size_t pt_lvl2_i = (virt >> 21) % 0x200; // 2mb entry
- // size_t pt_lvl1_i = (virt >> 12) % 0x200; // 4kb entry
- uint64_t* pt_lvl3 = (uint64_t*)(page_table_lvl4[pt_lvl4_i] + KERNEL_VMA);
- if (!((uint64_t)pt_lvl3 & FLAG_PRESENT)) {
- pt_lvl3 = (uint64_t*)kalloc(4096);
- page_table_lvl4[pt_lvl4_i] = ((uint64_t)pt_lvl3 - KERNEL_VMA) | flags;
- } else {
- pt_lvl3 = (uint64_t*)((uint64_t)pt_lvl3 - (uint64_t)pt_lvl3 % 4096);
- }
- uint64_t* pt_lvl2 = (uint64_t*)(pt_lvl3[pt_lvl3_i] + KERNEL_VMA);
- if (!((uint64_t)pt_lvl2 & FLAG_PRESENT)) {
- pt_lvl2 = (uint64_t*)kalloc(4096);
- pt_lvl3[pt_lvl3_i] = ((uint64_t)pt_lvl2 - KERNEL_VMA) | flags;
- } else {
- pt_lvl2 = (uint64_t*)((uint64_t)pt_lvl2 - (uint64_t)pt_lvl2 % 4096);
- }
- __asm__ volatile("mov %%cr3, %%rax;mov %%rax, %%cr3;" ::: "rax", "memory");
- // working
- // __asm__ volatile("9: jmp 9b;");
- pt_lvl2[pt_lvl2_i] = phys | flags | FLAG_HUGE;
- // not working
- // __asm__ volatile("9: jmp 9b;");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement