Advertisement
Guest User

pages.c

a guest
Nov 18th, 2019
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.33 KB | None | 0 0
  1. void init_pages()
  2. {
  3.     // Identity map the kernel
  4.     uint64_t memory_to_map = memory_map_end; // We want the memory map to be indentity mapped as well as the kernel.
  5.     uint64_t p2entries = (memory_to_map - 1) / (0x100000 * 2) + 1; // Getting the ammount of P2 entries (2MB pages).
  6.     uint64_t p3entries = (p2entries - 1) / 512 + 1; // Getting the ammount of P2 tables / P3 entries (512 P2 entries per P3 entry).
  7.     uint64_t p4entries = (p3entries - 1) / 512 + 1; // Getting the ammount of P3 tables / P4 entries (512 P3 entries per P4 entry).
  8.  
  9.     // Create P2 tables.
  10.     uint8_t* p2entries_address = allocate_frame(p3entries);
  11.     page_entry_t* p2entries_array = p2entries_address;
  12.     for (int i = 0; i < p2entries; i++)
  13.     {
  14.         clear_entry(&p2entries_array[i]);
  15.         p2entries_array[i].present = 1;
  16.         p2entries_array[i].read_write = 1;
  17.         p2entries_array[i].page_size = 1; // 2MB page
  18.         p2entries_array[i].available = 1;
  19.         p2entries_array[i].physical_addr = i * (2 * 0x100000);
  20.     }
  21.     for (int i = p2entries; i < p3entries * 512; i++)
  22.     {
  23.         clear_entry(&p2entries_array[i]);
  24.     }
  25.  
  26.     // Create P3 tables
  27.     uint8_t* p3entries_address = allocate_frame(p4entries);
  28.     page_entry_t* p3entries_array = p2entries_address;
  29.     for (int i = 0; i < p3entries; i++)
  30.     {
  31.         clear_entry(&p3entries_array[i]);
  32.         p3entries_array[i].present = 1;
  33.         p3entries_array[i].read_write = 1;
  34.         p3entries_array[i].available = 1;
  35.         p3entries_array[i].physical_addr = &p2entries_array[i * 512]; // Point this entry to the corresponding P2 table.
  36.     }
  37.     for (int i = p3entries; i < p4entries * 512; i++)
  38.     {
  39.         clear_entry(&p3entries_array[i]);
  40.     }
  41.  
  42.     // Create the P4 table
  43.     uint8_t* p4entries_address = allocate_frame(1);
  44.     page_entry_t* p4entries_array = p4entries_address;
  45.     for (int i = 0; i < p4entries; i++)
  46.     {
  47.         clear_entry(&p4entries_array[i]);
  48.         p4entries_array[i].present = 1;
  49.         p4entries_array[i].read_write = 1;
  50.         p4entries_array[i].available = 1;
  51.         p4entries_array[i].physical_addr = &p3entries_array[i * 512]; // Point this entry to the corresponding P2 table.
  52.     }
  53.     for (int i = p4entries; i < 512; i++)
  54.     {
  55.         clear_entry(&p3entries_array[i]);
  56.     }
  57.  
  58.     set_p4(p4entries_address);
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement