Advertisement
Guest User

Untitled

a guest
Sep 9th, 2022
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. void map_addr(uint64_t virt, uint64_t phys, uint32_t flags)
  2. {
  3. virt -= virt % 0x00200000;
  4. phys -= phys % 0x00200000;
  5.  
  6. // i is in range [0, 511]
  7. size_t pt_lvl4_i = (virt >> 39) % 0x200; // 512gb entry
  8. size_t pt_lvl3_i = (virt >> 30) % 0x200; // 1gb entry
  9. size_t pt_lvl2_i = (virt >> 21) % 0x200; // 2mb entry
  10. // size_t pt_lvl1_i = (virt >> 12) % 0x200; // 4kb entry
  11.  
  12. uint64_t* pt_lvl3 = (uint64_t*)(page_table_lvl4[pt_lvl4_i] + KERNEL_VMA);
  13. if (!((uint64_t)pt_lvl3 & FLAG_PRESENT)) {
  14. pt_lvl3 = (uint64_t*)kalloc(4096);
  15. page_table_lvl4[pt_lvl4_i] = ((uint64_t)pt_lvl3 - KERNEL_VMA) | flags;
  16. } else {
  17. pt_lvl3 = (uint64_t*)((uint64_t)pt_lvl3 - (uint64_t)pt_lvl3 % 4096);
  18. }
  19.  
  20. uint64_t* pt_lvl2 = (uint64_t*)(pt_lvl3[pt_lvl3_i] + KERNEL_VMA);
  21. if (!((uint64_t)pt_lvl2 & FLAG_PRESENT)) {
  22. pt_lvl2 = (uint64_t*)kalloc(4096);
  23. pt_lvl3[pt_lvl3_i] = ((uint64_t)pt_lvl2 - KERNEL_VMA) | flags;
  24. } else {
  25. pt_lvl2 = (uint64_t*)((uint64_t)pt_lvl2 - (uint64_t)pt_lvl2 % 4096);
  26. }
  27.  
  28. __asm__ volatile("mov %%cr3, %%rax;mov %%rax, %%cr3;" ::: "rax", "memory");
  29.  
  30. // working
  31. // __asm__ volatile("9: jmp 9b;");
  32. pt_lvl2[pt_lvl2_i] = phys | flags | FLAG_HUGE;
  33. // not working
  34. // __asm__ volatile("9: jmp 9b;");
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement