#include #include #include #include #include #include #include extern struct x86_Page_Directory boot_page_directory[1024] __attribute__((aligned(4096))); static struct x86_Page_Table user_code_table[1024] __attribute__((aligned(4096))); static uint8_t user_code[4096] __attribute__((aligned(4096))); static struct x86_Page_Table user_data_table[1024] __attribute__((aligned(4096))); static uint8_t user_data[4096] __attribute__((aligned(4096))); static struct x86_Page_Table* get_table(uint16_t index) { struct x86_Page_Table* table = (struct x86_Page_Table*)((boot_page_directory[index].address << 12) + 0xC0000000); return table; } static void reload_pages () { asm("\ movl %cr3, %ecx; \ movl %ecx, %cr3; \ "); } void set_page_directory_entry(uintptr_t address, struct x86_Page_Table* table) { boot_page_directory[address >> 22].address = ((uintptr_t)(table) - 0xC0000000) >> 12; boot_page_directory[address >> 22].present = true; boot_page_directory[address >> 22].read_write = true; boot_page_directory[address >> 22].user_supervisor = true; } void set_page_table_entry(size_t address, void* ptr) { struct x86_Page_Table* table = (struct x86_Page_Table*)((boot_page_directory[address >> 22].address << 12) + 0xC0000000); table[(address >> 12) & 0x3FF].address = (size_t)(ptr - 0xC0000000) >> 12; table[(address >> 12) & 0x3FF].present = true; table[(address >> 12) & 0x3FF].read_write = true; table[(address >> 12) & 0x3FF].user_supervisor = true; } struct x86_Page_Table create_table; void setup_paging() { struct x86_Page_Table* kernel = get_table(768); uint16_t i = 0; while (kernel[i].present == 0) { i++; } terminal_writestring("Kernel Start Page: "); print_hex_bytes(&i, 2); while (kernel[i].present != 0) { i++; } terminal_writestring("\nKernel End Page: "); print_hex_bytes(&i, 2); terminal_putchar('\n'); set_page_directory_entry(0, &user_code_table); /* user_code_table.address = ((uintptr_t)user_code - 0xC0000000) >> 12; user_code_table.present = true; user_code_table.read_write = true; user_code_table.user_supervisor = true; */ set_page_directory_entry(0x00400000, &user_data_table); /* user_data_table.address = ((uintptr_t)user_data - 0xC0000000) >> 12; user_data_table.present = true; user_data_table.read_write = true; user_data_table.user_supervisor = true; */ reload_pages(); }