The Pedigree Project  0.1
kernel/core/processor/hosted/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_HOSTED_VIRTUALADDRESSSPACE_H
21 #define KERNEL_PROCESSOR_HOSTED_VIRTUALADDRESSSPACE_H
22 
23 #include "pedigree/kernel/Spinlock.h"
24 #include "pedigree/kernel/processor/VirtualAddressSpace.h"
25 #include "pedigree/kernel/processor/types.h"
26 #include "pedigree/kernel/utilities/Vector.h"
27 
28 //
29 // Virtual address space layout
30 //
31 #define KERNEL_SPACE_START reinterpret_cast<void *>(0x0000700000000000)
32 
33 #define USERSPACE_DYNAMIC_LINKER_LOCATION reinterpret_cast<void *>(0x4FA00000)
34 
35 #define USERSPACE_VIRTUAL_START reinterpret_cast<void *>(0x20000000)
36 #define USERSPACE_VIRTUAL_HEAP reinterpret_cast<void *>(0x50000000)
37 #define USERSPACE_RESERVED_START USERSPACE_DYNAMIC_LINKER_LOCATION
38 #define USERSPACE_VIRTUAL_STACK_SIZE 0x100000
39 #define USERSPACE_VIRTUAL_MAX_STACK_SIZE 0x100000
40 #define USERSPACE_DYNAMIC_START reinterpret_cast<void *>(0x0000400000000000)
41 #define USERSPACE_DYNAMIC_END reinterpret_cast<void *>(0x0000500000000000)
42 #define USERSPACE_VIRTUAL_LOWEST_STACK \
43  reinterpret_cast<void *>( \
44  USERSPACE_DYNAMIC_END + USERSPACE_VIRTUAL_MAX_STACK_SIZE)
45 #define USERSPACE_VIRTUAL_STACK reinterpret_cast<void *>(0x00006FFFFFFFF000)
46 #define KERNEL_VIRTUAL_MODULE_BASE reinterpret_cast<void *>(0x70000000)
47 #define KERNEL_VIRTUAL_MODULE_SIZE 0x10000000
48 #define KERNEL_VIRTUAL_EVENT_BASE reinterpret_cast<void *>(0x300000000000)
49 #define KERNEL_VIRTUAL_HEAP reinterpret_cast<void *>(0x0000700000000000)
50 #define KERNEL_VIRTUAL_HEAP_SIZE 0x40000000
51 #define KERNEL_VIRTUAL_ADDRESS reinterpret_cast<void *>(0x400000)
52 #define KERNEL_VIRTUAL_CACHE reinterpret_cast<void *>(0x200000000000)
53 #define KERNEL_VIRTUAL_CACHE_SIZE 0x10000000
54 #define KERNEL_VIRTUAL_MEMORYREGION_ADDRESS \
55  reinterpret_cast<void *>(0x0000700040000000)
56 #define KERNEL_VIRTUAL_MEMORYREGION_SIZE 0x40000000
57 #define KERNEL_VIRTUAL_PAGESTACK_4GB \
58  reinterpret_cast<void *>(0x0000700080000000)
59 #define KERNEL_VIRTUAL_STACK reinterpret_cast<void *>(0x0000700FFFFFF000)
60 #define KERNEL_STACK_SIZE 0x10000
61 
69 {
71  friend class Processor;
73  friend class Multiprocessor;
79 
80  public:
81  //
82  // VirtualAddressSpace Interface
83  //
84  virtual bool isAddressValid(void *virtualAddress);
85  virtual bool isMapped(void *virtualAddress);
86 
87  virtual bool
88  map(physical_uintptr_t physAddress, void *virtualAddress, size_t flags);
89  virtual void getMapping(
90  void *virtualAddress, physical_uintptr_t &physAddress, size_t &flags);
91  virtual void setFlags(void *virtualAddress, size_t newFlags);
92  virtual void unmap(void *virtualAddress);
93  virtual Stack *allocateStack();
94  virtual Stack *allocateStack(size_t stackSz);
95  virtual void freeStack(Stack *pStack);
96 
97  virtual bool memIsInHeap(void *pMem);
98  virtual void *getEndOfHeap();
99 
100  virtual VirtualAddressSpace *clone(bool copyOnWrite = true);
101  virtual void revertToKernelAddressSpace();
102 
104  virtual ~HostedVirtualAddressSpace();
105 
107  virtual uintptr_t getKernelStart() const
108  {
109  return reinterpret_cast<uintptr_t>(KERNEL_SPACE_START);
110  }
111 
113  virtual uintptr_t getUserStart() const
114  {
115  return reinterpret_cast<uintptr_t>(USERSPACE_VIRTUAL_START);
116  }
117 
119  virtual uintptr_t getUserReservedStart() const
120  {
121  return reinterpret_cast<uintptr_t>(USERSPACE_RESERVED_START);
122  }
123 
125  virtual uintptr_t getDynamicLinkerAddress() const
126  {
127  return reinterpret_cast<uintptr_t>(USERSPACE_DYNAMIC_LINKER_LOCATION);
128  }
129 
131  virtual uintptr_t getKernelHeapStart() const
132  {
133  return reinterpret_cast<uintptr_t>(KERNEL_VIRTUAL_HEAP);
134  }
135 
137  virtual uintptr_t getKernelHeapEnd() const
138  {
139  return reinterpret_cast<uintptr_t>(KERNEL_VIRTUAL_HEAP) +
140  KERNEL_VIRTUAL_HEAP_SIZE;
141  }
142 
144  virtual uintptr_t getDynamicStart() const
145  {
146  return reinterpret_cast<uintptr_t>(USERSPACE_DYNAMIC_START);
147  }
148 
150  virtual uintptr_t getDynamicEnd() const
151  {
152  return reinterpret_cast<uintptr_t>(USERSPACE_DYNAMIC_END);
153  }
154 
156  virtual uintptr_t getKernelCacheStart() const
157  {
158  return reinterpret_cast<uintptr_t>(KERNEL_VIRTUAL_CACHE);
159  }
160 
162  virtual uintptr_t getKernelCacheEnd() const
163  {
164  return reinterpret_cast<uintptr_t>(KERNEL_VIRTUAL_CACHE) +
165  KERNEL_VIRTUAL_CACHE_SIZE;
166  }
167 
169  virtual uintptr_t getKernelEventBlockStart() const
170  {
171  return reinterpret_cast<uintptr_t>(KERNEL_VIRTUAL_EVENT_BASE);
172  }
173 
175  virtual uintptr_t getKernelModulesStart() const
176  {
177  return reinterpret_cast<uintptr_t>(KERNEL_VIRTUAL_MODULE_BASE);
178  }
179 
181  virtual uintptr_t getKernelModulesEnd() const
182  {
183  return reinterpret_cast<uintptr_t>(KERNEL_VIRTUAL_MODULE_BASE) +
184  KERNEL_VIRTUAL_MODULE_SIZE;
185  }
186 
187  private:
196  HostedVirtualAddressSpace(void *Heap, void *VirtualStack);
197 
204 
206  static void switchAddressSpace(
207  VirtualAddressSpace &oldSpace, VirtualAddressSpace &newSpace);
208 
214  uint64_t toFlags(size_t flags, bool bFinal = false);
220  size_t fromFlags(uint64_t Flags, bool bFinal = false);
221 
223  Stack *doAllocateStack(size_t sSize);
224 
225  typedef struct
226  {
227  bool active;
228  void *vaddr;
229  physical_uintptr_t paddr;
230  size_t flags; // Real flags, not the mmap-specific ones.
231  } mapping_t;
232 
234  void *m_pStackTop;
248  size_t m_nLastUnmap;
249 
250  static HostedVirtualAddressSpace m_KernelSpace;
251 };
252 
255 #endif
virtual bool isMapped(void *virtualAddress)
virtual bool map(physical_uintptr_t physAddress, void *virtualAddress, size_t flags)
virtual bool isAddressValid(void *virtualAddress)
uint64_t toFlags(size_t flags, bool bFinal=false)
static EXPORTED_PUBLIC VirtualAddressSpace & getKernelAddressSpace()
HostedVirtualAddressSpace & operator=(const HostedVirtualAddressSpace &)
virtual void setFlags(void *virtualAddress, size_t newFlags)
size_t fromFlags(uint64_t Flags, bool bFinal=false)
virtual bool memIsInHeap(void *pMem)
static VirtualAddressSpace * create()
virtual VirtualAddressSpace * clone(bool copyOnWrite=true)
The exception was caused by a hardware task switch.
Definition: Processor.h:80
virtual void getMapping(void *virtualAddress, physical_uintptr_t &physAddress, size_t &flags)
virtual void unmap(void *virtualAddress)
virtual void freeStack(Stack *pStack)
static void switchAddressSpace(VirtualAddressSpace &oldSpace, VirtualAddressSpace &newSpace)