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 
23 #include "pedigree/kernel/Atomic.h"
24 #include "pedigree/kernel/Spinlock.h"
25 #include "pedigree/kernel/compiler.h"
26 #include "pedigree/kernel/processor/PhysicalMemoryManager.h"
27 #include "pedigree/kernel/processor/types.h"
28 #include "pedigree/kernel/utilities/HashTable.h"
29 #include "pedigree/kernel/utilities/RangeList.h"
30 #include "pedigree/kernel/utilities/utility.h"
31 
32 class BootstrapStruct_t;
33 class MemoryRegion;
34 
38 extern size_t g_AllocedPages;
39 extern size_t g_FreePages;
40 
44 {
45  friend class CacheManager;
46  friend class Cache;
47 
48  public:
52  {
53  return m_Instance;
54  }
55 
56  //
57  // PhysicalMemoryManager Interface
58  //
59  virtual physical_uintptr_t allocatePage(size_t pageConstraints = 0);
60  virtual void freePage(physical_uintptr_t page);
61  virtual bool allocateRegion(
62  MemoryRegion &Region, size_t cPages, size_t pageConstraints,
63  size_t Flags, physical_uintptr_t start = -1);
64 
65  virtual void pin(physical_uintptr_t page);
66 
70 
74 #ifdef X64
76 #endif
77 
79  void initialisationDone();
80 
82  void shutdown();
83 
84 #if defined(ACPI)
85  inline const RangeList<uint64_t> &getAcpiRanges() const
86  {
87  return m_AcpiRanges;
88  }
89 #endif
90 
92  virtual size_t freePageCount() const;
93 
94  protected:
99 
100  private:
108 
109  void unmapRegion(MemoryRegion *pRegion);
110 
113  virtual void freePageUnlocked(physical_uintptr_t page);
114 
119  class PageStack
120  {
121  friend class X86CommonPhysicalMemoryManager;
122 
123  public:
129  physical_uintptr_t allocate(size_t constraints);
132  void free(uint64_t physicalAddress, size_t length);
134  inline ~PageStack()
135  {
136  }
137 
138  inline size_t freePages() const
139  {
140  return m_FreePages;
141  }
142 
143  void setCapacity(size_t newCapacity)
144  {
145  m_DesiredCapacity = newCapacity;
146  }
147 
148  void increaseCapacity(size_t by)
149  {
150  m_DesiredCapacity += by;
151  }
152 
153  protected:
155  void markAbove4GReady();
156 
158  void markBelow4GReady();
159 
160  private:
163  PageStack(const PageStack &);
166  PageStack &operator=(const PageStack &);
167 
173  bool maybeMap(size_t index, uint64_t physicalAddress);
174 
176 #if defined(X86)
177  static const size_t StackCount = 1;
178 #elif defined(X64)
179  static const size_t StackCount = 3;
180 #endif
181 
190  size_t m_FreePages;
192  size_t m_Capacity;
195 
198  };
199 
202 
207 
210 
211 #if defined(ACPI)
212 
213  RangeList<uint64_t> m_AcpiRanges;
214 #endif
215 
220 
223 
225  Spinlock m_Lock, m_RegionLock;
226 
229  {
230  public:
231  PageHashable()
232  {
233  m_Hash = m_Page = 0;
234  }
235 
236  PageHashable(physical_uintptr_t p)
237  {
238  m_Hash = p / getPageSize();
239  m_Page = p;
240  }
241 
242  size_t hash() const
243  {
244  return m_Hash;
245  }
246 
247  bool operator==(const PageHashable &p) const
248  {
249  return p.m_Page == m_Page;
250  }
251 
252  private:
253  size_t m_Hash;
254  physical_uintptr_t m_Page;
255  };
256 
258  struct page
259  {
260  page() : active(false), refcount(0)
261  {
262  }
263 
264  bool active;
265  size_t refcount;
266  };
267 
269 
272 };
273 
276 #endif
Bootstrap structure passed to the kernel entry point.
bool maybeMap(size_t index, uint64_t physicalAddress)
virtual void freePageUnlocked(physical_uintptr_t page)
X86CommonPhysicalMemoryManager & operator=(const X86CommonPhysicalMemoryManager &)
The Stack of pages (below4GB, below64GB, no constraint).
void initialise64(const BootstrapStruct_t &Info) INITIALISATION_ONLY
void free(uint64_t physicalAddress, size_t length)
Definition: Cache.h:118
virtual bool allocateRegion(MemoryRegion &Region, size_t cPages, size_t pageConstraints, size_t Flags, physical_uintptr_t start=-1)
PageStack & operator=(const PageStack &)
uintptr_t physicalAddress(physical_uintptr_t address) PURE
Definition: utils.h:38
virtual void pin(physical_uintptr_t page)
Special memory entity in the kernel&#39;s virtual address space.
Definition: MemoryRegion.h:35
virtual void freePage(physical_uintptr_t page)
virtual physical_uintptr_t allocatePage(size_t pageConstraints=0)
void initialise(const BootstrapStruct_t &Info) INITIALISATION_ONLY
bool operator==(const Iterator< originalT, Struct, FunctionPrev, FunctionNext, T1 > &x1, const Iterator< originalT, Struct, FunctionPrev, FunctionNext, T2 > &x2)
Definition: Iterator.h:326
Implementation of the PhysicalMemoryManager for common x86.