Added functions to enable MMU with paging

main
Nigel Barink 2021-05-10 21:32:28 +01:00
parent 9f2b2a0798
commit 4008fc25e8
3 changed files with 75 additions and 0 deletions

35
src/kernel/MMU.cpp Normal file
View File

@ -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();
}

14
src/kernel/MMU.h Normal file
View File

@ -0,0 +1,14 @@
#pragma once
#include <stdint.h>
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)));
};

View File

@ -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