Serenity Operating System
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 <Kernel/Process.h>
28#include <Kernel/Random.h>
29#include <Kernel/Thread.h>
30#include <Kernel/VM/MemoryManager.h>
31#include <Kernel/VM/PageDirectory.h>
32
33namespace Kernel {
34
35static const uintptr_t userspace_range_base = 0x00800000;
36static const uintptr_t userspace_range_ceiling = 0xbe000000;
37static const uintptr_t kernelspace_range_base = 0xc0800000;
38
39static HashMap<u32, PageDirectory*>& cr3_map()
40{
41 ASSERT_INTERRUPTS_DISABLED();
42 static HashMap<u32, PageDirectory*>* map;
43 if (!map)
44 map = new HashMap<u32, PageDirectory*>;
45 return *map;
46}
47
48RefPtr<PageDirectory> PageDirectory::find_by_cr3(u32 cr3)
49{
50 InterruptDisabler disabler;
51 return cr3_map().get(cr3).value_or({});
52}
53
54extern "C" PageDirectoryEntry* boot_pdpt[4];
55extern "C" PageDirectoryEntry boot_pd0[1024];
56extern "C" PageDirectoryEntry boot_pd3[1024];
57
58PageDirectory::PageDirectory()
59{
60 m_range_allocator.initialize_with_range(VirtualAddress(0xc0800000), 0x3f000000);
61
62 // Adopt the page tables already set up by boot.S
63 PhysicalAddress boot_pdpt_paddr(virtual_to_low_physical((uintptr_t)boot_pdpt));
64 PhysicalAddress boot_pd0_paddr(virtual_to_low_physical((uintptr_t)boot_pd0));
65 PhysicalAddress boot_pd3_paddr(virtual_to_low_physical((uintptr_t)boot_pd3));
66 kprintf("MM: boot_pdpt @ P%p\n", boot_pdpt_paddr.get());
67 kprintf("MM: boot_pd0 @ P%p\n", boot_pd0_paddr.get());
68 kprintf("MM: boot_pd3 @ P%p\n", boot_pd3_paddr.get());
69 m_directory_table = PhysicalPage::create(boot_pdpt_paddr, true, false);
70 m_directory_pages[0] = PhysicalPage::create(boot_pd0_paddr, true, false);
71 m_directory_pages[3] = PhysicalPage::create(boot_pd3_paddr, true, false);
72}
73
74PageDirectory::PageDirectory(Process& process, const RangeAllocator* parent_range_allocator)
75 : m_process(&process)
76{
77 if (parent_range_allocator) {
78 m_range_allocator.initialize_from_parent(*parent_range_allocator);
79 } else {
80 size_t random_offset = (get_good_random<u32>() % 32 * MB) & PAGE_MASK;
81 u32 base = userspace_range_base + random_offset;
82 m_range_allocator.initialize_with_range(VirtualAddress(base), userspace_range_ceiling - base);
83 }
84
85 // Set up a userspace page directory
86 m_directory_table = MM.allocate_user_physical_page();
87 m_directory_pages[0] = MM.allocate_user_physical_page();
88 m_directory_pages[1] = MM.allocate_user_physical_page();
89 m_directory_pages[2] = MM.allocate_user_physical_page();
90 // Share the top 1 GB of kernel-only mappings (>=3GB or >=0xc0000000)
91 m_directory_pages[3] = MM.kernel_page_directory().m_directory_pages[3];
92
93 {
94 InterruptDisabler disabler;
95 auto& table = *(PageDirectoryPointerTable*)MM.quickmap_page(*m_directory_table);
96 table.raw[0] = (u64)m_directory_pages[0]->paddr().as_ptr() | 1;
97 table.raw[1] = (u64)m_directory_pages[1]->paddr().as_ptr() | 1;
98 table.raw[2] = (u64)m_directory_pages[2]->paddr().as_ptr() | 1;
99 table.raw[3] = (u64)m_directory_pages[3]->paddr().as_ptr() | 1;
100 MM.unquickmap_page();
101 }
102
103 // Clone bottom 2 MB of mappings from kernel_page_directory
104 PageDirectoryEntry buffer;
105 auto* kernel_pd = MM.quickmap_pd(MM.kernel_page_directory(), 0);
106 memcpy(&buffer, kernel_pd, sizeof(PageDirectoryEntry));
107 auto* new_pd = MM.quickmap_pd(*this, 0);
108 memcpy(new_pd, &buffer, sizeof(PageDirectoryEntry));
109
110 InterruptDisabler disabler;
111 cr3_map().set(cr3(), this);
112}
113
114PageDirectory::~PageDirectory()
115{
116#ifdef MM_DEBUG
117 dbgprintf("MM: ~PageDirectory K%x\n", this);
118#endif
119 InterruptDisabler disabler;
120 cr3_map().remove(cr3());
121}
122
123}