From 5fc90ea24013dc175ef124843e71db819043ed34 Mon Sep 17 00:00:00 2001 From: starnakin Date: Tue, 15 Oct 2024 00:29:46 +0200 Subject: [PATCH] fix: paging with higher half kernel --- src/boot.s | 2 ++ src/kernel.c | 3 +++ src/memory/memory.c | 38 +++++++++++++++++++++++++++++++------- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/boot.s b/src/boot.s index 6295a58..611aa50 100644 --- a/src/boot.s +++ b/src/boot.s @@ -21,6 +21,8 @@ stack_top: # Preallocate pages used for paging. Don't hard-code addresses and assume they # are available, as the bootloader might have loaded its multiboot structures or # modules there. This lets the bootloader know it must avoid the addresses. +.global boot_page_directory +.global boot_page_table1 .section .bss, "aw", @nobits .align 4096 boot_page_directory: diff --git a/src/kernel.c b/src/kernel.c index cceb2dd..1bf9676 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -1,3 +1,4 @@ +#include "alloc.h" #include "drivers.h" #include "gdt.h" #include "idt.h" @@ -33,5 +34,7 @@ void kernel_main(void) "I see no way to confuse an array of 256 seg:off pairs with a " "complex 8*unknown quantity -byte descriptor table. -- Troy " "Martin 03:50, 22 March 2009 (UTC)\n"); + while (!kmalloc(10)) + ; shell_init(); } diff --git a/src/memory/memory.c b/src/memory/memory.c index 4262a3b..df51a0a 100644 --- a/src/memory/memory.c +++ b/src/memory/memory.c @@ -1,19 +1,43 @@ #include "memory.h" +#include "kprintf.h" #include "string.h" + #include -extern void load_page_directory(uint32_t *); extern void enable_paging(void); +extern void load_page_directory(uint32_t *); -uint32_t page_directory_entries[1024] __attribute__((aligned(4096))); +extern uint32_t boot_page_table1; +extern uint32_t boot_page_directory; + +// Pour récupérer une entrée spécifique +uint32_t *page_directory = &boot_page_directory; uint32_t page_table_entries[1024] __attribute__((aligned(4096))); +unsigned long read_cr3() +{ + unsigned long cr3_value; + + // Inline assembly to read CR3 + __asm__ volatile( + "mov %%cr3, %0" // Move the value of CR3 into cr3_value + : "=r"( + cr3_value) // Output operand: cr3_value will receive the value + : // No input operands + : // No clobber list + ); + + return cr3_value; +} + +static inline void load_cr3(uint64_t cr3_value) +{ + asm volatile("mov %0, %%cr3" ::"r"(cr3_value) : "memory"); +} + void init_memory(void) { - memset(page_directory_entries, INIT_FLAGS, 1024); for (int i = 0; i < 1024; i++) page_table_entries[i] = (i << 12) | INIT_FLAGS; - page_directory_entries[0] = (uint32_t)page_table_entries | INIT_FLAGS; - load_page_directory(page_directory_entries); - enable_paging(); -} + page_directory[1] = (uint32_t)page_table_entries | INIT_FLAGS; +} \ No newline at end of file