From 9172da075af71d4c03cc60267e5acb0ec7d88f31 Mon Sep 17 00:00:00 2001 From: Nigel Date: Mon, 15 Aug 2022 19:51:22 +0200 Subject: [PATCH] Added identity paging basics Followed wiki.osdev.org/Setting_Up_Paging --- src/kernel/kernel.cpp | 11 +++-- src/kernel/memory/paging.cpp | 79 ++++++++++++++++++++++++++++++++---- src/kernel/memory/paging.h | 3 -- 3 files changed, 77 insertions(+), 16 deletions(-) diff --git a/src/kernel/kernel.cpp b/src/kernel/kernel.cpp index 7d15fcf..12d1c79 100644 --- a/src/kernel/kernel.cpp +++ b/src/kernel/kernel.cpp @@ -4,7 +4,7 @@ extern "C" void kernel_main (BootInfo* bootinfo) { init_serial(); pit_initialise(); - InitializePaging(); + //InitializePaging(); //Enable(); startSuperVisorTerminal(bootinfo); @@ -90,8 +90,7 @@ extern "C" void early_main(unsigned long magic, unsigned long addr){ memAlloc.allocate_region(kernel_end, kernel_end - kernel_begin); // test alloc_block - - + /* uint8_t* memory = (uint8_t*) memAlloc.allocate_block(); printf("Got a new pointer: 0x%x\n", memory); @@ -102,13 +101,13 @@ extern "C" void early_main(unsigned long magic, unsigned long addr){ memAlloc.free_block((void*) memory); uint8_t* newBlockPlse = (uint8_t*) memAlloc.allocate_block(); - + */ // memAlloc.free_block((void*) memory); - - + InitializePaging(); + Enable(); } initGDT(); diff --git a/src/kernel/memory/paging.cpp b/src/kernel/memory/paging.cpp index 6f50e3c..9ea4859 100644 --- a/src/kernel/memory/paging.cpp +++ b/src/kernel/memory/paging.cpp @@ -1,16 +1,79 @@ #include "paging.h" -PageDirectoryEntry kernel_directory[MAX_DIRECTORY_ENTRIES]; +PageDirectoryEntry kernel_directory[MAX_DIRECTORY_ENTRIES]__attribute__((aligned(4096))); +PageTableEntry first_page_table[1024]__attribute__((aligned(4096))); + +#define KERNEL_VRT_MEMORY_BEGIN 0xC0000000 +#define KERNEL_VRT_MEMORY_END 0xCFFFFFFF +#define PAGE_SIZE 0xFA0; + void InitializePaging() { + + + printf("\nInit paging\n"); + // The basics as explained by wiki.osdev.org + + // Set all page_directories to not present + int i = 0; + while ( i < 1024) + { + kernel_directory[i] = 0x00000002; + i++; + } + + + // map 4 megabytes + unsigned int j ; + for( j = 0; j < 1024; j++ ) + { + first_page_table[j] = (j * 0x1000) | 3 ; + /* + Attributes: + Supervisor Level , + read/write, + present, + */ + + } + + // Put the page table in the page directory + // attributes: supervisor level, read/write, present; + kernel_directory[0] = ((unsigned int)first_page_table) | 3; + + printf("Init paging DONE\n"); + // NOTE: Adjust this as needed + + // BIOS Address Identity mapping // Identity map the first 8MB ... Physical addresses 0x00000000 to 0x007A1200 + /* + + PHYSICAL_ADDRESS BIOSAddr = 0x00000000; + PHYSICAL_ADDRESS BIOSAddr_Max = 0x007A1200; + + do + { + Map( BIOSAddr, BIOSAddr, *kernel_directory); + BIOSAddr += PAGE_SIZE + } while(BIOSAddr <= BIOSAddr_Max); + + // Identity map the kernel space + VIRTUAL_ADDRESS Vaddr = KERNEL_VRT_MEMORY_BEGIN; + PHYSICAL_ADDRESS KernelAddr = kernel_begin; + + do + { + Map(KernelAddr, Vaddr , *kernel_directory); + + Vaddr += PAGE_SIZE; + KernelAddr += PAGE_SIZE + + } + while(KernelAddr < kernel_end); - // Identity map the kernel space || our map to 3GB space - - - // Identity map VGA memory - + //TODO: Identity map VGA memory + */ } @@ -30,9 +93,11 @@ void FreePage(VIRTUAL_ADDRESS vaddr , PageDirectoryEntry& page_directory) void Map ( PHYSICAL_ADDRESS paddr, VIRTUAL_ADDRESS vaddr, PageDirectoryEntry& page_directory) { + + } -void Unmap(VIRTUAL_ADDRESS, PageDirectoryEntry& page_directory) +void Unmap(VIRTUAL_ADDRESS vaddr, PageDirectoryEntry& page_directory) { // NOTE: I will implement lazy unmapping for now diff --git a/src/kernel/memory/paging.h b/src/kernel/memory/paging.h index 04aa2fb..789574e 100644 --- a/src/kernel/memory/paging.h +++ b/src/kernel/memory/paging.h @@ -12,10 +12,7 @@ void InitializePaging(); void Enable(); void AllocatePage(VIRTUAL_ADDRESS, PageDirectoryEntry&); - - void FreePage(VIRTUAL_ADDRESS, PageDirectoryEntry&); - void Map(PHYSICAL_ADDRESS, VIRTUAL_ADDRESS, PageDirectoryEntry&); void Unmap (VIRTUAL_ADDRESS, PageDirectoryEntry&);