The Pedigree Project 0.1
kernel/core/processor/x86_common/PhysicalMemoryManager.h
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#ifndef KERNEL_PROCESSOR_X86_COMMON_PHYSICALMEMORYMANAGER_H
21#define KERNEL_PROCESSOR_X86_COMMON_PHYSICALMEMORYMANAGER_H
22#include "pedigree/kernel/Atomic.h"
23#include "pedigree/kernel/Spinlock.h"
24#include "pedigree/kernel/compiler.h"
25#include "pedigree/kernel/processor/PhysicalMemoryManager.h"
26#include "pedigree/kernel/processor/types.h"
27#include "pedigree/kernel/utilities/HashTable.h"
28#include "pedigree/kernel/utilities/RangeList.h"
29#include "pedigree/kernel/utilities/utility.h"
30
31#include <config.h>
32
34class MemoryRegion;
35
39extern size_t g_AllocedPages;
40extern size_t g_FreePages;
41
45 friend class CacheManager;
46 friend class Cache;
47
48#if HOSTED
49 friend class HostedPhysicalMemoryManager;
50#endif
51
52 public:
56 return m_Instance;
57 }
58
59 //
60 // PhysicalMemoryManager Interface
61 //
62 virtual physical_uintptr_t allocatePage(size_t pageConstraints = 0) override;
63 physical_uintptr_t tryAllocatePage() override;
64 bool copyPhysicalPageToBuffer(physical_uintptr_t page, void* buffer) override;
65 bool copyPhysicalPageFromBuffer(physical_uintptr_t page, const void* buffer) override;
66 MemorySnapshot memorySnapshot() const override;
67 virtual void freePage(physical_uintptr_t page) override;
68 virtual bool allocateRegion(MemoryRegion& Region, size_t cPages, size_t pageConstraints,
69 size_t Flags, physical_uintptr_t start = -1) override;
70
71 virtual void pin(physical_uintptr_t page) override;
72
76
81
83 void initialisationDone();
84
86 void shutdown();
87
88 const RangeList<uint64_t>& getAcpiRanges() const {
89 return m_AcpiRanges;
90 }
91
93 virtual size_t freePageCount() const override;
94
95 protected:
99 virtual ~X86CommonPhysicalMemoryManager() override;
100
101 private:
108
109 void unmapRegion(MemoryRegion* pRegion) override;
110
113 virtual void freePageUnlocked(physical_uintptr_t page) override;
114
119 class PageStack {
121#if HOSTED
122 friend class HostedPhysicalMemoryManager;
123#endif
124
125 public:
131 physical_uintptr_t allocate(size_t constraints, bool waitForReady = true);
134 void free(uint64_t physicalAddress, size_t length, bool newMemory = false);
136 inline ~PageStack() {}
137
138 inline size_t freePages() const {
139 return m_FreePages;
140 }
141
142 size_t totalPages() const {
143 return m_TotalPages;
144 }
145
146 protected:
148 void markAbove4GReady();
149
151 void markBelow4GReady();
152
153 private:
160
168 void initialise();
169
175 bool maybeMap(size_t index, uint64_t physicalAddress);
176
178 static const size_t StackCount = 3;
179
182 void* m_Stack[StackCount];
184 size_t m_StackMax[StackCount];
186 size_t m_StackSize[StackCount];
189 size_t m_TotalPages;
191 size_t m_DesiredCapacity[StackCount];
192
194 Atomic<bool> m_StackReady[StackCount];
195 };
196
199
204
207
210
215
218
220 Spinlock m_Lock, m_RegionLock;
221
224 public:
225 PageHashable() {
226 m_Hash = m_Page = 0;
227 }
228
229 PageHashable(physical_uintptr_t p) {
230 m_Hash = p / getPageSize();
231 m_Page = p;
232 }
233
234 size_t hash() const {
235 return m_Hash;
236 }
237
238 bool operator==(const PageHashable& p) const {
239 return p.m_Page == m_Page;
240 }
241
242 private:
243 size_t m_Hash;
244 physical_uintptr_t m_Page;
245 };
246
248 struct page {
249 page() : active(false), refcount(0) {}
250
251 bool active;
252 size_t refcount;
253 };
254
256
259
266 physical_uintptr_t m_BootstrapPinnedPage;
267 size_t m_BootstrapPinnedPageRefcount;
268};
269
272#endif
Definition Cache.h:207
Implementation of the PhysicalMemoryManager for common x86.
Special memory entity in the kernel's virtual address space.
PageStack & operator=(const PageStack &)
Implementation of the PhysicalMemoryManager for common x86.
void initialise64(const BootstrapStruct_t &Info) INITIALISATION_ONLY
void initialise(const BootstrapStruct_t &Info) INITIALISATION_ONLY
virtual size_t freePageCount() const override
virtual void freePageUnlocked(physical_uintptr_t page) override
void unmapRegion(MemoryRegion *pRegion) override
virtual void pin(physical_uintptr_t page) override
virtual void freePage(physical_uintptr_t page) override
virtual bool allocateRegion(MemoryRegion &Region, size_t cPages, size_t pageConstraints, size_t Flags, physical_uintptr_t start=-1) override
virtual physical_uintptr_t allocatePage(size_t pageConstraints=0) override
uintptr_t physicalAddress(physical_uintptr_t address) PURE
Definition utils.h:39