The Pedigree Project 0.1
include/pedigree/kernel/processor/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_PHYSICALMEMORYMANAGER_H
21#define KERNEL_PROCESSOR_PHYSICALMEMORYMANAGER_H
22
23#include "pedigree/kernel/TargetInfo.h"
24#include "pedigree/kernel/compiler.h"
25#include "pedigree/kernel/processor/types.h"
26#include "pedigree/kernel/utilities/Vector.h"
27
31class MemoryRegion;
32
35class EXPORTED_PUBLIC PhysicalMemoryManager {
36 friend class CacheManager;
37 friend class Cache;
38
39 public:
41 friend class MemoryRegion;
42
44 static const size_t continuous = 1 << 0;
46 static const size_t nonRamMemory = 1 << 1;
50 static const size_t force = 1 << 2;
52 static const size_t virtualOnly = 1 << 3;
53
55 static const size_t below1MB = 1 << 4;
57 static const size_t below16MB = 1 << 5;
59 static const size_t below4GB = 1 << 6;
61 static const size_t below64GB = 1 << 7;
62
64 static const size_t addressConstraints = below1MB | below16MB | below4GB | below64GB;
65
67 static const size_t anonymous = 1 << 8;
68
71 static PhysicalMemoryManager& instance();
72
75 inline static constexpr size_t getPageSize() PURE {
77 }
80 virtual physical_uintptr_t allocatePage(size_t pageConstraints = 0) = 0;
81 // This path never invokes reclaim and reports exhaustion without panicking.
82 virtual physical_uintptr_t tryAllocatePage() {
83 return 0;
84 }
85 // The caller owns the entire aligned physical page throughout the copy.
86 virtual bool copyPhysicalPageToBuffer(physical_uintptr_t page, void* buffer) {
87 return false;
88 }
89 virtual bool copyPhysicalPageFromBuffer(physical_uintptr_t page, const void* buffer) {
90 return false;
91 }
93 uint64_t totalPages = 0;
94 uint64_t freePages = 0;
95 bool available = false;
96 };
97 virtual MemorySnapshot memorySnapshot() const {
98 return {};
99 }
102 virtual void freePage(physical_uintptr_t page) = 0;
103
113 virtual void pin(physical_uintptr_t page) = 0;
114
115#if HOSTED && PEDIGREE_HOSTED_SMOKE_TESTS
116 static EXPORTED_PUBLIC void setTryAllocationFailureForTest(ssize_t after);
118 static EXPORTED_PUBLIC size_t pageReferenceCountForTest(physical_uintptr_t page);
119#endif
120
129 virtual bool allocateRegion(MemoryRegion& Region, size_t cPages, size_t pageConstraints,
130 size_t Flags, physical_uintptr_t start = -1) = 0;
131
140 inline MemoryRegionInfo(void* VirtualAddress, physical_uintptr_t PhysicalAddress, size_t size,
141 const char* name)
142 : pVirtualAddress(VirtualAddress),
143 physicalAddress(PhysicalAddress),
144 sVirtualAddress(size),
145 pName(name) {}
146
150 physical_uintptr_t physicalAddress;
154 const char* pName;
155 };
156
160 void allocateMemoryRegionList(Vector<MemoryRegionInfo*>& MemoryRegions);
164 void freeMemoryRegionList(Vector<MemoryRegionInfo*>& MemoryRegions);
165
167 virtual size_t freePageCount() const;
168
169 protected:
173 virtual ~PhysicalMemoryManager();
174
177
178 private:
185
188 virtual void freePageUnlocked(physical_uintptr_t page) = 0;
189
191 virtual void unmapRegion(MemoryRegion* pRegion) = 0;
192};
193
196#endif
Definition Cache.h:207
Special memory entity in the kernel's virtual address space.
virtual physical_uintptr_t allocatePage(size_t pageConstraints=0)=0
PhysicalMemoryManager(const PhysicalMemoryManager &)
virtual void freePageUnlocked(physical_uintptr_t page)=0
virtual void freePage(physical_uintptr_t page)=0
virtual void pin(physical_uintptr_t page)=0
virtual bool allocateRegion(MemoryRegion &Region, size_t cPages, size_t pageConstraints, size_t Flags, physical_uintptr_t start=-1)=0
PhysicalMemoryManager & operator=(const PhysicalMemoryManager &)
virtual void unmapRegion(MemoryRegion *pRegion)=0
static constexpr size_t getPageSize() noexcept
Definition TargetInfo.h:40
A vector / dynamic array.
Definition Vector.h:33
uintptr_t physicalAddress(physical_uintptr_t address) PURE
Definition utils.h:39
MemoryRegionInfo(void *VirtualAddress, physical_uintptr_t PhysicalAddress, size_t size, const char *name)