Quick fixes, just changing order and makes a reference to the newly mapped higher-half page-directory

This commit is contained in:
vanten-s 2025-11-13 21:57:20 +01:00
parent 0cd287d4bd
commit e41b764ac6
Signed by: vanten-s
GPG key ID: DE3060396884D3F2
2 changed files with 30 additions and 28 deletions

View file

@ -34,12 +34,9 @@ extern const size_t _kernel_end;
struct PageDirectoryEntry _page_directory[1024] __attribute__((aligned(4096))); // Create a page directory with 1024 entries and make sure it's 4 KiB aligned struct PageDirectoryEntry _page_directory[1024] __attribute__((aligned(4096))); // Create a page directory with 1024 entries and make sure it's 4 KiB aligned
// Identity page table for lowest 4 MiB, including this // Identity page table for lowest 4 MiB, including this. This will be the table where the (current) kernel will be stored
struct PageTableEntry _identity_page_table[1024] __attribute__((aligned(4096))); struct PageTableEntry _identity_page_table[1024] __attribute__((aligned(4096)));
// Assumes kernel is smaller than 4 MiB, should change later
struct PageTableEntry _kernel_page_table[1024] __attribute__((aligned(4096)));
void initialize_pages() { void initialize_pages() {
struct PageDirectoryEntry identity_pd_entry = { struct PageDirectoryEntry identity_pd_entry = {
.page_table_address = (size_t)(_identity_page_table) >> 12, .page_table_address = (size_t)(_identity_page_table) >> 12,
@ -50,6 +47,7 @@ void initialize_pages() {
}; };
_page_directory[0] = identity_pd_entry; _page_directory[0] = identity_pd_entry;
_page_directory[768] = identity_pd_entry;
// Identity page lowest 4 MiB to not crash instantly // Identity page lowest 4 MiB to not crash instantly
for (size_t i = 0; i < mib(4) / kib(4); i++) { for (size_t i = 0; i < mib(4) / kib(4); i++) {
@ -62,8 +60,6 @@ void initialize_pages() {
_identity_page_table[i] = table_entry; _identity_page_table[i] = table_entry;
} }
_page_directory[768] = identity_pd_entry;
asm("mov %0, %%cr3" :: "r" (_page_directory)); asm("mov %0, %%cr3" :: "r" (_page_directory));
uint32_t control_register_0; uint32_t control_register_0;
asm("mov %%cr0, %0" : "=r" (control_register_0)); asm("mov %%cr0, %0" : "=r" (control_register_0));

View file

@ -27,32 +27,38 @@
Page pages[NUM_PAGES]; Page pages[NUM_PAGES];
struct PageDirectoryEntry { struct PageDirectoryEntry {
uint32_t page_table_address : 20;
uint32_t available_1 : 4;
bool size : 1;
bool available_2 : 1;
bool accessed : 1;
bool cache_disabled : 1;
bool write_through : 1;
bool user : 1;
bool write : 1;
bool present : 1; bool present : 1;
bool write : 1;
bool user : 1;
bool write_through : 1;
bool cache_disabled : 1;
bool accessed : 1;
bool available_2 : 1;
bool size : 1;
size_t available_1 : 4;
size_t page_table_address : 20;
} __attribute__((packed)); // Force the compiler to keep our structure intact. } __attribute__((packed)); // Force the compiler to keep our structure intact.
struct PageTableEntry { struct PageTableEntry {
uint32_t physical_address : 20;
uint32_t available : 3;
bool global : 1;
bool pat : 1;
bool dirty : 1;
bool accessed : 1;
bool cache_disabled : 1;
bool write_through : 1;
bool user : 1;
bool write : 1;
bool present : 1; bool present : 1;
bool write : 1;
bool user : 1;
bool write_through : 1;
bool cache_disabled : 1;
bool accessed : 1;
bool dirty : 1;
bool pat : 1;
bool global : 1;
size_t available : 3;
size_t physical_address : 20;
} __attribute__((packed)); } __attribute__((packed));
extern "C" PageDirectoryEntry _page_directory[1024];
extern "C" PageTableEntry _identity_page_table[1024];
PageDirectoryEntry* page_directory;
PageTableEntry* identity_page_table;
PageTableEntry* kernel_page_table;
PageDirectoryEntry dynamic_page_table[1024] __attribute__((aligned(4096))); // Create a page directory with 1024 entries and make sure it's 4 KiB aligned PageDirectoryEntry dynamic_page_table[1024] __attribute__((aligned(4096))); // Create a page directory with 1024 entries and make sure it's 4 KiB aligned
Page::Page() { } Page::Page() { }
@ -112,9 +118,9 @@ size_t Page::pt_entry() {
void Page::initialize() { void Page::initialize() {
// Allocate the lowest 1 MiB of both virtual and physical memory to BIOS // Allocate the lowest 1 MiB of both virtual and physical memory to BIOS
for (size_t i = 0; i < mib(1); i += kib(4)) {
Page::alloc_fixed((void*)i, (void*)i); page_directory = (struct PageDirectoryEntry*)((size_t)(_page_directory) + 0xC0000000);
} identity_page_table = (struct PageTableEntry*)((size_t)(_identity_page_table) + 0xC0000000);
/* for (size_t index = _kernel_start; index + kib(4) < _kernel_end; index += kib(4)) { /* for (size_t index = _kernel_start; index + kib(4) < _kernel_end; index += kib(4)) {
Page::alloc_fixed(void *physical_address, void *virtual_addr) Page::alloc_fixed(void *physical_address, void *virtual_addr)