diff --git a/headers/memory.h b/headers/memory.h new file mode 100644 index 0000000..ab5c9e5 --- /dev/null +++ b/headers/memory.h @@ -0,0 +1,29 @@ +#pragma once + +#include + +#define NOT_PRESENT (1 << 0) +#define RW (1 << 1) +#define SUPERVISOR (0 << 2) +#define INIT_FLAGS (NOT_PRESENT | RW | SUPERVISOR) + +struct page_directory_entry { + uint32_t present : 1; + uint32_t rw : 1; + uint32_t user : 1; + uint32_t page_size : 1; + uint32_t unused : 8; + uint32_t frame : 20; +}; + +struct page_table_entry { + uint32_t present : 1; + uint32_t rw : 1; + uint32_t user : 1; + uint32_t accessed : 1; + uint32_t dirty : 1; + uint32_t unused : 7; + uint32_t frame : 20; +}; + +void init_memory(void); diff --git a/src/kernel.c b/src/kernel.c index 046d342..a94583e 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -1,8 +1,7 @@ #include "gdt.h" +#include "memory.h" #include "shell.h" #include "terminal.h" -/* #include "power.h" */ -#include "gdt.h" #include #include @@ -21,9 +20,8 @@ void kernel_main(void) { - /* Initialize terminal interface */ terminal_initialize(); - init_gdt(); + init_memory(); shell_init(); } diff --git a/src/memory/load_pd.s b/src/memory/load_pd.s new file mode 100644 index 0000000..bffd31d --- /dev/null +++ b/src/memory/load_pd.s @@ -0,0 +1,12 @@ +.intel_syntax noprefix + +.text +.global load_page_directory +load_page_directory: +push ebp +mov ebp, esp +mov eax, [esp + 8] +mov cr3, eax +mov esp, ebp +pop ebp +ret diff --git a/src/memory/memory.c b/src/memory/memory.c new file mode 100644 index 0000000..a0e5c20 --- /dev/null +++ b/src/memory/memory.c @@ -0,0 +1,18 @@ +#include "memory.h" +#include "string.h" + +extern void load_page_directory(uint32_t *); +extern void enable_paging(void); + +uint32_t page_directory_entries[1024] __attribute__((aligned(4096))); +uint32_t page_table_entries[1024] __attribute__((aligned(4096))); + +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(); +} diff --git a/src/memory/paging.s b/src/memory/paging.s new file mode 100644 index 0000000..10d62d3 --- /dev/null +++ b/src/memory/paging.s @@ -0,0 +1,13 @@ +.intel_syntax noprefix + +.text +.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