From 4008fc25e8d5eda4c460133aba68b5380a37902b Mon Sep 17 00:00:00 2001 From: Nigel Barink Date: Mon, 10 May 2021 21:32:28 +0100 Subject: [PATCH] Added functions to enable MMU with paging --- src/kernel/MMU.cpp | 35 +++++++++++++++++++++++++++++++++++ src/kernel/MMU.h | 14 ++++++++++++++ src/kernel/arch/i386/boot.s | 26 ++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 src/kernel/MMU.cpp create mode 100644 src/kernel/MMU.h diff --git a/src/kernel/MMU.cpp b/src/kernel/MMU.cpp new file mode 100644 index 0000000..96867f4 --- /dev/null +++ b/src/kernel/MMU.cpp @@ -0,0 +1,35 @@ +#include "MMU.h" + + + +void MMU::enable(){ + + //set each entry to not present + int i; + for(i = 0; i < 1024; 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 + this->page_directory[i] = 0x00000002; + } + + // holds the physical address where we want to start mapping these pages to. + // in this case, we want to map these pages to the very beginning of memory. + + //we will fill all 1024 entries in the table, mapping 4 megabytes + for(unsigned int i = 0; i < 1024; i++) + { + // As the address is page aligned, it will always leave 12 bits zeroed. + // Those bits are used by the attributes ;) + first_page_table[i] = (i * 0x1000) | 3; // attributes: supervisor level, read/write, present. + } + + // attributes: supervisor level, read/write, present + this->page_directory[0] = ((unsigned int)first_page_table) | 3; + + + loadPageDirectory(this->page_directory); + enablePaging(); +} \ No newline at end of file diff --git a/src/kernel/MMU.h b/src/kernel/MMU.h new file mode 100644 index 0000000..cb3b7f8 --- /dev/null +++ b/src/kernel/MMU.h @@ -0,0 +1,14 @@ +#pragma once +#include + +extern "C" void loadPageDirectory (long unsigned int* addr ); +extern "C" void enablePaging(); + +class MMU { + public: + void enable (); + + private: + uint32_t page_directory[1024] __attribute__((aligned(4096))); + uint32_t first_page_table[1024] __attribute__((aligned(4096))); +}; \ No newline at end of file diff --git a/src/kernel/arch/i386/boot.s b/src/kernel/arch/i386/boot.s index fa5fce2..99fa270 100644 --- a/src/kernel/arch/i386/boot.s +++ b/src/kernel/arch/i386/boot.s @@ -21,6 +21,32 @@ stack_bottom: stack_top: + + +.text +.globl enablePaging +enablePaging: + push %ebp + mov %esp, %ebp + mov %cr0, %eax + or $0x80000000, %eax + mov %eax, %cr0 + mov %ebp, %esp + pop %ebp + ret + +.text +.globl loadPageDirectory +loadPageDirectory: + push %ebp + mov %esp, %ebp + mov 8(%esp), %eax + mov %eax, %cr3 + mov %ebp, %esp + pop %ebp + ret + + .section .text .global _start .type _start, @function