The Pedigree Project  0.1
kernel/core/processor/x86/VirtualAddressSpace.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_VIRTUALADDRESSSPACE_H
21 #define KERNEL_PROCESSOR_X86_VIRTUALADDRESSSPACE_H
22 
23 #include "pedigree/kernel/Spinlock.h"
24 #include "pedigree/kernel/processor/VirtualAddressSpace.h"
25 #include "pedigree/kernel/processor/types.h"
26 
27 //
28 // Virtual address space layout
29 //
30 #define KERNEL_SPACE_START reinterpret_cast<void *>(0xC0000000)
31 
32 #define USERSPACE_DYNAMIC_LINKER_LOCATION reinterpret_cast<void *>(0x4FA00000)
33 
34 #define USERSPACE_VIRTUAL_START reinterpret_cast<void *>(0x400000)
35 #define USERSPACE_VIRTUAL_HEAP reinterpret_cast<void *>(0x50000000)
36 #define USERSPACE_VIRTUAL_STACK reinterpret_cast<void *>(0xB0000000)
37 #define USERSPACE_RESERVED_START USERSPACE_VIRTUAL_HEAP
38 #define USERSPACE_VIRTUAL_MAX_STACK_SIZE 0x100000
39 #define USERSPACE_VIRTUAL_LOWEST_STACK reinterpret_cast<void *>(0x70000000)
40 #define VIRTUAL_PAGE_DIRECTORY reinterpret_cast<void *>(0xFFBFF000)
41 #define VIRTUAL_PAGE_TABLES reinterpret_cast<void *>(0xFFC00000)
42 #define KERNEL_VIRTUAL_TEMP1 reinterpret_cast<void *>(0xFFBFC000)
43 #define KERNEL_VIRTUAL_TEMP2 reinterpret_cast<void *>(0xFFBFD000)
44 #define KERNEL_VIRTUAL_TEMP3 reinterpret_cast<void *>(0xFFBFE000)
45 #define KERNEL_VIRTUAL_HEAP reinterpret_cast<void *>(0xC0000000)
46 #define KERNEL_VIRTUAL_HEAP_SIZE 0x10000000
47 #define KERNEL_VIRUTAL_PAGE_DIRECTORY reinterpret_cast<void *>(0xFF7FF000)
48 #define KERNEL_VIRTUAL_ADDRESS reinterpret_cast<void *>(0xFF400000 - 0x100000)
49 #define KERNEL_VIRTUAL_MEMORYREGION_ADDRESS reinterpret_cast<void *>(0xD0000000)
50 #define KERNEL_VIRTUAL_PAGESTACK_4GB reinterpret_cast<void *>(0xF0000000)
51 #define KERNEL_VIRTUAL_STACK reinterpret_cast<void *>(0xFF3F6000)
52 #define KERNEL_VIRTUAL_MEMORYREGION_SIZE 0x10000000
53 #define KERNEL_STACK_SIZE 0x8000
54 
62 {
65  friend class Processor;
67  friend class Multiprocessor;
70 
71  public:
72  //
73  // VirtualAddressSpace Interface
74  //
75  virtual bool isAddressValid(void *virtualAddress);
76  virtual bool isMapped(void *virtualAddress);
77  virtual bool
78  map(physical_uintptr_t physicalAddress, void *virtualAddress, size_t flags);
79  virtual void getMapping(
80  void *virtualAddress, physical_uintptr_t &physicalAddress,
81  size_t &flags);
82  virtual void setFlags(void *virtualAddress, size_t newFlags);
83  virtual void unmap(void *virtualAddress);
84  virtual void *allocateStack();
85  virtual void *allocateStack(size_t stackSz);
86  virtual void freeStack(void *pStack);
87 
88  virtual bool memIsInHeap(void *pMem);
89  virtual void *getEndOfHeap();
90 
91  virtual VirtualAddressSpace *clone();
92  virtual void revertToKernelAddressSpace();
93 
94  //
95  // Needed for the PhysicalMemoryManager
96  //
107  bool mapPageStructures(
108  physical_uintptr_t physicalAddress, void *virtualAddress, size_t flags);
109 
111  static void initialise() INITIALISATION_ONLY;
113  virtual ~X86VirtualAddressSpace();
114 
116  virtual uintptr_t getKernelStart() const
117  {
118  return reinterpret_cast<uintptr_t>(KERNEL_SPACE_START);
119  }
120 
122  virtual uintptr_t getUserStart() const
123  {
124  return reinterpret_cast<uintptr_t>(USERSPACE_VIRTUAL_START);
125  }
126 
128  virtual uintptr_t getUserReservedStart() const
129  {
130  return reinterpret_cast<uintptr_t>(USERSPACE_RESERVED_START);
131  }
132 
134  virtual uintptr_t getDynamicLinkerAddress() const
135  {
136  return reinterpret_cast<uintptr_t>(USERSPACE_DYNAMIC_LINKER_LOCATION);
137  }
138 
140  virtual uintptr_t getKernelHeapStart() const
141  {
142  return reinterpret_cast<uintptr_t>(KERNEL_VIRTUAL_HEAP);
143  }
144 
146  virtual uintptr_t getKernelHeapEnd() const
147  {
148  return reinterpret_cast<uintptr_t>(KERNEL_VIRTUAL_HEAP) +
149  KERNEL_VIRTUAL_HEAP_SIZE;
150  }
151 
152  protected:
162  void *Heap, physical_uintptr_t PhysicalPageDirectory,
163  void *VirtualPageDirectory, void *VirtualPageTables,
164  void *VirtualStack) INITIALISATION_ONLY;
165 
166  bool doIsMapped(void *virtualAddress);
167  bool doMap(
168  physical_uintptr_t physicalAddress, void *virtualAddress, size_t flags);
169  void doGetMapping(
170  void *virtualAddress, physical_uintptr_t &physicalAddress,
171  size_t &flags);
172  void doSetFlags(void *virtualAddress, size_t newFlags);
173  void doUnmap(void *virtualAddress);
174  void *doAllocateStack(size_t sSize);
175 
176  private:
179 
186 
192  bool getPageTableEntry(void *virtualAddress, uint32_t *&pageTableEntry);
198  uint32_t toFlags(size_t flags, bool bFinal = false);
204  size_t fromFlags(uint32_t Flags, bool bFinal = false);
205 
214  uintptr_t beginCrossSpace(X86VirtualAddressSpace *pOther);
219  bool mapCrossSpace(
220  uintptr_t &v, physical_uintptr_t physicalAddress, void *virtualAddress,
221  size_t flags);
223  void endCrossSpace();
224 
226  physical_uintptr_t m_PhysicalPageDirectory;
232  void *m_pStackTop;
237 };
238 
241 {
247 
248  public:
249  //
250  // VirtualAddressSpace Interface
251  //
252  virtual bool isMapped(void *virtualAddress);
253  virtual bool
254  map(physical_uintptr_t physicalAddress, void *virtualAddress, size_t flags);
255  virtual void getMapping(
256  void *virtualAddress, physical_uintptr_t &physicalAddress,
257  size_t &flags);
258  virtual void setFlags(void *virtualAddress, size_t newFlags);
259  virtual void unmap(void *virtualAddress);
260  virtual void *allocateStack();
261 
262  private:
274 
277 };
278 
281 #endif
virtual void unmap(void *virtualAddress)
size_t fromFlags(uint32_t Flags, bool bFinal=false)
virtual bool isAddressValid(void *virtualAddress)
virtual bool map(physical_uintptr_t physicalAddress, void *virtualAddress, size_t flags)
static EXPORTED_PUBLIC VirtualAddressSpace & getKernelAddressSpace()
virtual void getMapping(void *virtualAddress, physical_uintptr_t &physicalAddress, size_t &flags)
virtual bool memIsInHeap(void *pMem)
static VirtualAddressSpace * create()
static void initialise() INITIALISATION_ONLY
uintptr_t physicalAddress(physical_uintptr_t address) PURE
Definition: utils.h:38
bool mapPageStructures(physical_uintptr_t physicalAddress, void *virtualAddress, size_t flags)
uintptr_t beginCrossSpace(X86VirtualAddressSpace *pOther)
The exception was caused by a hardware task switch.
Definition: Processor.h:80
virtual void setFlags(void *virtualAddress, size_t newFlags)
bool getPageTableEntry(void *virtualAddress, uint32_t *&pageTableEntry)
X86VirtualAddressSpace & operator=(const X86VirtualAddressSpace &)
virtual bool isMapped(void *virtualAddress)
bool mapCrossSpace(uintptr_t &v, physical_uintptr_t physicalAddress, void *virtualAddress, size_t flags)
uint32_t toFlags(size_t flags, bool bFinal=false)