Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void init_pages()
- {
- // Identity map the kernel
- uint64_t memory_to_map = memory_map_end; // We want the memory map to be indentity mapped as well as the kernel.
- uint64_t p2entries = (memory_to_map - 1) / (0x100000 * 2) + 1; // Getting the ammount of P2 entries (2MB pages).
- uint64_t p3entries = (p2entries - 1) / 512 + 1; // Getting the ammount of P2 tables / P3 entries (512 P2 entries per P3 entry).
- uint64_t p4entries = (p3entries - 1) / 512 + 1; // Getting the ammount of P3 tables / P4 entries (512 P3 entries per P4 entry).
- // Create P2 tables.
- uint8_t* p2entries_address = allocate_frame(p3entries);
- page_entry_t* p2entries_array = p2entries_address;
- for (int i = 0; i < p2entries; i++)
- {
- clear_entry(&p2entries_array[i]);
- p2entries_array[i].present = 1;
- p2entries_array[i].read_write = 1;
- p2entries_array[i].page_size = 1; // 2MB page
- p2entries_array[i].available = 1;
- p2entries_array[i].physical_addr = i * (2 * 0x100000);
- }
- for (int i = p2entries; i < p3entries * 512; i++)
- {
- clear_entry(&p2entries_array[i]);
- }
- // Create P3 tables
- uint8_t* p3entries_address = allocate_frame(p4entries);
- page_entry_t* p3entries_array = p2entries_address;
- for (int i = 0; i < p3entries; i++)
- {
- clear_entry(&p3entries_array[i]);
- p3entries_array[i].present = 1;
- p3entries_array[i].read_write = 1;
- p3entries_array[i].available = 1;
- p3entries_array[i].physical_addr = &p2entries_array[i * 512]; // Point this entry to the corresponding P2 table.
- }
- for (int i = p3entries; i < p4entries * 512; i++)
- {
- clear_entry(&p3entries_array[i]);
- }
- // Create the P4 table
- uint8_t* p4entries_address = allocate_frame(1);
- page_entry_t* p4entries_array = p4entries_address;
- for (int i = 0; i < p4entries; i++)
- {
- clear_entry(&p4entries_array[i]);
- p4entries_array[i].present = 1;
- p4entries_array[i].read_write = 1;
- p4entries_array[i].available = 1;
- p4entries_array[i].physical_addr = &p3entries_array[i * 512]; // Point this entry to the corresponding P2 table.
- }
- for (int i = p4entries; i < 512; i++)
- {
- clear_entry(&p3entries_array[i]);
- }
- set_p4(p4entries_address);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement