From 920d0b01e1b6623cb3a1b48f6cd4f9a0f3c8a06a Mon Sep 17 00:00:00 2001 From: Nathan Singer Date: Mon, 9 Jun 2025 21:01:17 -0400 Subject: [PATCH] paging is now enabled with __shaky__ code --- TODO.md | 3 ++ kernel/arch/io/io.s | 2 +- kernel/arch/paging.c | 6 ---- kernel/arch/paging/paging.c | 55 +++++++++++++++++++++++++++++++++ kernel/arch/paging/paging_src.s | 20 ++++++++++++ kernel/include/kernel/paging.h | 17 ++++++++-- kernel/kmain.c | 4 ++- 7 files changed, 97 insertions(+), 10 deletions(-) delete mode 100644 kernel/arch/paging.c create mode 100644 kernel/arch/paging/paging.c create mode 100644 kernel/arch/paging/paging_src.s diff --git a/TODO.md b/TODO.md index a262528..a658665 100644 --- a/TODO.md +++ b/TODO.md @@ -5,6 +5,9 @@ (https://wiki.osdev.org/Troubleshooting#Showing_the_stack_content) lets implmeent some better testing functions aswell - Paging & virtual memory + - Get GRUB to show memory map - DONE + - Write a page allocator - WIP + - Setup paging - Setup higher half kernel - Heap allocator (malloc) - after this, implment some data structures? cleanup queue? diff --git a/kernel/arch/io/io.s b/kernel/arch/io/io.s index 7e2ae7b..c94152e 100644 --- a/kernel/arch/io/io.s +++ b/kernel/arch/io/io.s @@ -10,7 +10,7 @@ outb: ret ; return to the calling function -global outb_16 +global outb_16 ; TODO these are probably wrong, eh, the stack might need more bcs the variables are bigger? im not sure outb_16: mov ax, [esp + 8] mov dx, [esp + 4] diff --git a/kernel/arch/paging.c b/kernel/arch/paging.c deleted file mode 100644 index 6ba6414..0000000 --- a/kernel/arch/paging.c +++ /dev/null @@ -1,6 +0,0 @@ -#include - -extern uint32_t endkernel; // found in link.ld - - - diff --git a/kernel/arch/paging/paging.c b/kernel/arch/paging/paging.c new file mode 100644 index 0000000..4ceafb1 --- /dev/null +++ b/kernel/arch/paging/paging.c @@ -0,0 +1,55 @@ +#include + +#include + +#include + +//extern uint32_t endkernel; // found in link.ld + +/** + * The page table must be page aligned (aligned at 4KiB) + * + * + * This is a temporary solution, as we want a page frame allocator, to properly get page frames.. but this works for now + */ +uint32_t page_directory[PAGE_DIRECTORY_ENTRIES] __attribute__((aligned(4096))); + +uint32_t first_page_table[PAGE_TABLE_ENTRIES] __attribute__((aligned(4096))); + +void setup_page_table(void) +{ + uint32_t i; + for (i = 0; i < PAGE_TABLE_ENTRIES; i++) { + first_page_table[i] = (i * 0x1000) | 3; // supervisor, r/w, present + } + puts("test"); +} + +void setup_page_dir(void) +{ + setup_page_table(); + /** + * Now that we have a page directory, we need to blank it. + * + * The page directory should have exactly 1024 entries. We will set each entry to not present, so that the if the + * MMU looks for that page table, it will see that it is not there yet. + */ + int i; + for (i = 0; i < PAGE_DIRECTORY_ENTRIES; i++) { + // This sets the following flags to the pages: + // Supervisor: Only kernel-mode can access them + // Write Enabled: It can be both read from and written to + // Not Present: The page table is not present + page_directory[i] = 0x00000002; + } + + page_directory[0] = ((uint32_t) first_page_table) | 3; +} + +void setup_paging(void) +{ + setup_page_dir(); + + load_page_directory(page_directory); + enable_paging(); +} diff --git a/kernel/arch/paging/paging_src.s b/kernel/arch/paging/paging_src.s new file mode 100644 index 0000000..36fc876 --- /dev/null +++ b/kernel/arch/paging/paging_src.s @@ -0,0 +1,20 @@ +global load_page_directory +load_page_directory: + push ebp + mov esp, ebp + mov eax, [esp + 4] + mov cr3, eax + mov esp, ebp + pop ebp + ret + +global enable_paging +enable_paging: + push ebp + mov ebp, esp + mov eax, cr0 + or eax, 0x80000000 + mov cr0, eax + mov esp, ebp + pop ebp + ret diff --git a/kernel/include/kernel/paging.h b/kernel/include/kernel/paging.h index 75a5e30..d41c280 100644 --- a/kernel/include/kernel/paging.h +++ b/kernel/include/kernel/paging.h @@ -1,3 +1,16 @@ -typedef struct { +#include -} pageframe_t; +#ifndef ARCH_PAGING_H +#define ARCH_PAGING_H + +#define PAGE_TABLE_ENTRIES 1024 +#define PAGE_DIRECTORY_ENTRIES 1024 + +void load_page_directory(uint32_t*); + +void enable_paging(); + + +void setup_paging(void); + +#endif diff --git a/kernel/kmain.c b/kernel/kmain.c index 45ad6a1..820ecb4 100644 --- a/kernel/kmain.c +++ b/kernel/kmain.c @@ -5,12 +5,12 @@ #include #include #include +#include #include #include #include #include #include - #include #include "multiboot.h" @@ -76,6 +76,8 @@ void _main(multiboot_info_t* mbd, uint32_t magic) serial_initialize(); #endif + setup_paging(); + init_kb(); init_pit(0x36, PIT_CHANNEL_0, 0);