From e0c38a12745f2b49b861c7154e41acd64176ade3 Mon Sep 17 00:00:00 2001 From: Baptiste Wicht Date: Sat, 7 Dec 2013 20:10:27 +0100 Subject: [PATCH] Move paging out of memory --- kernel/include/paging.hpp | 22 ++++++++++++++ kernel/src/acpi.cpp | 8 +++++ kernel/src/memory.cpp | 17 ----------- kernel/src/paging.cpp | 62 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 17 deletions(-) create mode 100644 kernel/include/paging.hpp create mode 100644 kernel/src/paging.cpp diff --git a/kernel/include/paging.hpp b/kernel/include/paging.hpp new file mode 100644 index 00000000..88c4c0bd --- /dev/null +++ b/kernel/include/paging.hpp @@ -0,0 +1,22 @@ +//======================================================================= +// Copyright Baptiste Wicht 2013. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +//======================================================================= + +#ifndef PAGING_H +#define PAGING_H + +#include "types.hpp" + +namespace paging { + +const int PAGE_SIZE = 4096; + +bool identity_map(void* physical); +bool identity_map(void* physical, size_t pages); + +} //end of namespace paging + +#endif diff --git a/kernel/src/acpi.cpp b/kernel/src/acpi.cpp index 2caf60d5..e1698641 100644 --- a/kernel/src/acpi.cpp +++ b/kernel/src/acpi.cpp @@ -9,6 +9,7 @@ #include "types.hpp" #include "kernel_utils.hpp" #include "timer.hpp" +#include "paging.hpp" #include "console.hpp" @@ -200,8 +201,15 @@ int init_acpi(){ k_printf("%h\n", reinterpret_cast(ptr)); + if(!paging::identity_map(ptr, 16)){ + k_print_line("Impossible to map the ACPI tables"); + + return -1; + } + // check if address is correct ( if acpi is available on this pc ) if (ptr && check_header(ptr, "RSDT") == 0){ + //k_print_line("2"); // the RSDT contains an unknown number of pointers to acpi tables int entrys = *(ptr + 1); entrys = (entrys-36) /4; diff --git a/kernel/src/memory.cpp b/kernel/src/memory.cpp index a967eb47..5843ecb7 100644 --- a/kernel/src/memory.cpp +++ b/kernel/src/memory.cpp @@ -85,23 +85,6 @@ uint64_t pdpt_index = 0; uint64_t pdt_index = 0; uint64_t pt_index = 256; -void identity_map(void* physical){ - //Find the correct indexes inside the paging table for the physical address - auto table = (reinterpret_cast(physical) >> 12) & 0x1FF; - auto directory = (reinterpret_cast(physical) >> 21) & 0x1FF; - auto directory_ptr = (reinterpret_cast(physical) >> 30) & 0x1FF; - auto pml4 = (reinterpret_cast(physical) >> 39) & 0x1FF; - - //Find the entries - pml4t_t pml4t = reinterpret_cast(0x70000); - auto pdpt = reinterpret_cast(reinterpret_cast(pml4t[pml4]) & ~0xFFF); - auto pdt = reinterpret_cast(reinterpret_cast(pdpt[directory_ptr]) & ~0xFFF); - auto pt = reinterpret_cast(reinterpret_cast(pdt[directory]) & ~0xFFF); - - //Identity map the physical address - pt[table] = reinterpret_cast(reinterpret_cast(physical) | 0x3); -} - uint64_t* allocate_block(uint64_t blocks){ if(!current_mmap_entry){ for(uint64_t i = 0; i < entry_count; ++i){ diff --git a/kernel/src/paging.cpp b/kernel/src/paging.cpp new file mode 100644 index 00000000..f39bdfcf --- /dev/null +++ b/kernel/src/paging.cpp @@ -0,0 +1,62 @@ +//======================================================================= +// Copyright Baptiste Wicht 2013. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +//======================================================================= + +#include "paging.hpp" +#include "types.hpp" + +namespace { + +typedef uint64_t* page_entry; +typedef page_entry* pt_t; +typedef pt_t* pdt_t; +typedef pdt_t* pdpt_t; +typedef pdpt_t* pml4t_t; + +} //end of anonymous namespace + +bool paging::identity_map(void* physical){ + //The address must be page-aligned + if(reinterpret_cast(physical) % PAGE_SIZE != 0){ + return false; + } + + //Find the correct indexes inside the paging table for the physical address + auto table = (reinterpret_cast(physical) >> 12) & 0x1FF; + auto directory = (reinterpret_cast(physical) >> 21) & 0x1FF; + auto directory_ptr = (reinterpret_cast(physical) >> 30) & 0x1FF; + auto pml4 = (reinterpret_cast(physical) >> 39) & 0x1FF; + + //Find the entries + pml4t_t pml4t = reinterpret_cast(0x70000); + auto pdpt = reinterpret_cast(reinterpret_cast(pml4t[pml4]) & ~0xFFF); + auto pdt = reinterpret_cast(reinterpret_cast(pdpt[directory_ptr]) & ~0xFFF); + auto pt = reinterpret_cast(reinterpret_cast(pdt[directory]) & ~0xFFF); + + //Identity map the physical address + pt[table] = reinterpret_cast(reinterpret_cast(physical) | 0x3); + + //TODO Check if pt[table] is already used and if so, return false + + return true; +} + +bool paging::identity_map(void* physical, size_t pages){ + //The address must be page-aligned + if(reinterpret_cast(physical) % PAGE_SIZE != 0){ + return false; + } + + //TODO This should first check each page for the present bit + + for(size_t page = 0; page < pages; ++page){ + if(!identity_map(reinterpret_cast(reinterpret_cast(physical) + page * PAGE_SIZE))){ + return false; + } + } + + return true; +}