2024-11-27 21:57:18 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
#include <kernel/paging.h>
|
|
|
|
#include <kernel/tty.h>
|
2025-02-25 18:48:58 +01:00
|
|
|
#include <debugging.h>
|
|
|
|
#include <kernel/heap.h>
|
2024-11-27 21:57:18 +01:00
|
|
|
|
2025-02-23 18:26:02 +01:00
|
|
|
extern struct x86_Page_Directory boot_page_directory[1024] __attribute__((aligned(4096)));
|
|
|
|
|
2025-07-01 12:48:05 +02:00
|
|
|
static struct x86_Page_Table user_code_table[1024] __attribute__((aligned(4096)));
|
2025-02-25 18:48:58 +01:00
|
|
|
static uint8_t user_code[4096] __attribute__((aligned(4096)));
|
|
|
|
|
2025-07-01 12:48:05 +02:00
|
|
|
|
|
|
|
static struct x86_Page_Table user_data_table[1024] __attribute__((aligned(4096)));
|
2025-02-25 18:48:58 +01:00
|
|
|
static uint8_t user_data[4096] __attribute__((aligned(4096)));
|
|
|
|
|
2025-02-23 18:26:02 +01:00
|
|
|
static struct x86_Page_Table* get_table(uint16_t index) {
|
2025-02-25 18:48:58 +01:00
|
|
|
struct x86_Page_Table* table = (struct x86_Page_Table*)((boot_page_directory[index].address << 12) + 0xC0000000);
|
2025-02-23 18:26:02 +01:00
|
|
|
return table;
|
|
|
|
}
|
2024-11-27 21:57:18 +01:00
|
|
|
|
2025-02-25 18:48:58 +01:00
|
|
|
static void reload_pages () {
|
|
|
|
asm("\
|
|
|
|
movl %cr3, %ecx; \
|
|
|
|
movl %ecx, %cr3; \
|
|
|
|
");
|
|
|
|
}
|
|
|
|
|
2025-07-01 12:48:05 +02:00
|
|
|
void set_page_directory_entry(uintptr_t address, struct x86_Page_Table* table)
|
|
|
|
{
|
2025-02-25 18:48:58 +01:00
|
|
|
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;
|
2025-02-26 01:42:35 +01:00
|
|
|
boot_page_directory[address >> 22].user_supervisor = true;
|
2025-02-25 18:48:58 +01:00
|
|
|
}
|
|
|
|
|
2025-07-01 12:48:05 +02:00
|
|
|
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;
|
2025-02-25 18:48:58 +01:00
|
|
|
|
2024-11-27 21:57:18 +01:00
|
|
|
void setup_paging() {
|
2025-02-23 18:26:02 +01:00
|
|
|
struct x86_Page_Table* kernel = get_table(768);
|
2025-02-25 18:48:58 +01:00
|
|
|
uint16_t i = 0;
|
|
|
|
while (kernel[i].present == 0) {
|
|
|
|
i++;
|
2025-02-23 18:26:02 +01:00
|
|
|
}
|
|
|
|
|
2025-02-25 18:48:58 +01:00
|
|
|
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);
|
|
|
|
|
2025-07-01 12:48:05 +02:00
|
|
|
/*
|
2025-02-25 18:48:58 +01:00
|
|
|
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;
|
2025-07-01 12:48:05 +02:00
|
|
|
*/
|
2025-02-25 18:48:58 +01:00
|
|
|
|
|
|
|
set_page_directory_entry(0x00400000, &user_data_table);
|
|
|
|
|
2025-07-01 12:48:05 +02:00
|
|
|
/*
|
2025-02-25 18:48:58 +01:00
|
|
|
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;
|
2025-07-01 12:48:05 +02:00
|
|
|
*/
|
2025-02-26 01:42:35 +01:00
|
|
|
|
2025-02-25 18:48:58 +01:00
|
|
|
reload_pages();
|
|
|
|
}
|