Serenity Operating System
at hosted 124 lines 5.0 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/Memory.h> 28#include <Kernel/Process.h> 29#include <Kernel/Random.h> 30#include <Kernel/Thread.h> 31#include <Kernel/VM/MemoryManager.h> 32#include <Kernel/VM/PageDirectory.h> 33 34namespace Kernel { 35 36static const FlatPtr userspace_range_base = 0x00800000; 37static const FlatPtr userspace_range_ceiling = 0xbe000000; 38static const FlatPtr kernelspace_range_base = 0xc0800000; 39 40static HashMap<u32, PageDirectory*>& cr3_map() 41{ 42 ASSERT_INTERRUPTS_DISABLED(); 43 static HashMap<u32, PageDirectory*>* map; 44 if (!map) 45 map = new HashMap<u32, PageDirectory*>; 46 return *map; 47} 48 49RefPtr<PageDirectory> PageDirectory::find_by_cr3(u32 cr3) 50{ 51 InterruptDisabler disabler; 52 return cr3_map().get(cr3).value_or({}); 53} 54 55extern "C" PageDirectoryEntry* boot_pdpt[4]; 56extern "C" PageDirectoryEntry boot_pd0[1024]; 57extern "C" PageDirectoryEntry boot_pd3[1024]; 58 59PageDirectory::PageDirectory() 60{ 61 m_range_allocator.initialize_with_range(VirtualAddress(0xc0800000), 0x3f000000); 62 63 // Adopt the page tables already set up by boot.S 64 PhysicalAddress boot_pdpt_paddr(virtual_to_low_physical((FlatPtr)boot_pdpt)); 65 PhysicalAddress boot_pd0_paddr(virtual_to_low_physical((FlatPtr)boot_pd0)); 66 PhysicalAddress boot_pd3_paddr(virtual_to_low_physical((FlatPtr)boot_pd3)); 67 klog() << "MM: boot_pdpt @ " << boot_pdpt_paddr; 68 klog() << "MM: boot_pd0 @ " << boot_pd0_paddr; 69 klog() << "MM: boot_pd3 @ " << boot_pd3_paddr; 70 m_directory_table = PhysicalPage::create(boot_pdpt_paddr, true, false); 71 m_directory_pages[0] = PhysicalPage::create(boot_pd0_paddr, true, false); 72 m_directory_pages[3] = PhysicalPage::create(boot_pd3_paddr, true, false); 73} 74 75PageDirectory::PageDirectory(Process& process, const RangeAllocator* parent_range_allocator) 76 : m_process(&process) 77{ 78 if (parent_range_allocator) { 79 m_range_allocator.initialize_from_parent(*parent_range_allocator); 80 } else { 81 size_t random_offset = (get_good_random<u32>() % 32 * MB) & PAGE_MASK; 82 u32 base = userspace_range_base + random_offset; 83 m_range_allocator.initialize_with_range(VirtualAddress(base), userspace_range_ceiling - base); 84 } 85 86 // Set up a userspace page directory 87 m_directory_table = MM.allocate_user_physical_page(); 88 m_directory_pages[0] = MM.allocate_user_physical_page(); 89 m_directory_pages[1] = MM.allocate_user_physical_page(); 90 m_directory_pages[2] = MM.allocate_user_physical_page(); 91 // Share the top 1 GB of kernel-only mappings (>=3GB or >=0xc0000000) 92 m_directory_pages[3] = MM.kernel_page_directory().m_directory_pages[3]; 93 94 { 95 InterruptDisabler disabler; 96 auto& table = *(PageDirectoryPointerTable*)MM.quickmap_page(*m_directory_table); 97 table.raw[0] = (u64)m_directory_pages[0]->paddr().as_ptr() | 1; 98 table.raw[1] = (u64)m_directory_pages[1]->paddr().as_ptr() | 1; 99 table.raw[2] = (u64)m_directory_pages[2]->paddr().as_ptr() | 1; 100 table.raw[3] = (u64)m_directory_pages[3]->paddr().as_ptr() | 1; 101 MM.unquickmap_page(); 102 } 103 104 // Clone bottom 2 MB of mappings from kernel_page_directory 105 PageDirectoryEntry buffer; 106 auto* kernel_pd = MM.quickmap_pd(MM.kernel_page_directory(), 0); 107 memcpy(&buffer, kernel_pd, sizeof(PageDirectoryEntry)); 108 auto* new_pd = MM.quickmap_pd(*this, 0); 109 memcpy(new_pd, &buffer, sizeof(PageDirectoryEntry)); 110 111 InterruptDisabler disabler; 112 cr3_map().set(cr3(), this); 113} 114 115PageDirectory::~PageDirectory() 116{ 117#ifdef MM_DEBUG 118 dbg() << "MM: ~PageDirectory K" << this; 119#endif 120 InterruptDisabler disabler; 121 cr3_map().remove(cr3()); 122} 123 124}