The Pedigree Project  0.1
MemoryMappedFile.h
Go to the documentation of this file.
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 MEMORY_MAPPED_FILE_H
21 #define MEMORY_MAPPED_FILE_H
22 
23 #include "pedigree/kernel/Spinlock.h"
24 #include "pedigree/kernel/compiler.h"
25 #include "pedigree/kernel/process/MemoryPressureManager.h"
26 #include "pedigree/kernel/processor/PageFaultHandler.h"
27 #include "pedigree/kernel/processor/state_forward.h"
28 #include "pedigree/kernel/processor/types.h"
29 #include "pedigree/kernel/utilities/List.h"
30 #include "pedigree/kernel/utilities/String.h"
31 #include "pedigree/kernel/utilities/Tree.h"
32 #include "pedigree/kernel/utilities/new"
33 
34 class File;
35 class Process;
37 
109 {
110  friend class MemoryMapManager;
111 
112  private:
115 
116  public:
118  typedef int Permissions;
119 
120  static const int None = 0x0;
121  static const int Read = 0x1;
122  static const int Write = 0x2;
123  static const int Exec = 0x4;
124 
127  uintptr_t address, bool bCopyOnWrite, size_t length, Permissions perms)
128  : m_bCopyOnWrite(bCopyOnWrite), m_Address(address), m_Length(length),
129  m_Permissions(perms)
130  {
131  }
132 
133  virtual ~MemoryMappedObject();
134 
145  virtual MemoryMappedObject *clone() = 0;
146 
152  virtual MemoryMappedObject *split(uintptr_t at) = 0;
153 
163  virtual bool remove(size_t length) = 0;
164 
170  virtual void setPermissions(Permissions perms) = 0;
171 
175  virtual void sync(uintptr_t at, bool async)
176  {
177  }
178 
183  virtual void invalidate(uintptr_t at)
184  {
185  }
186 
193  virtual void unmap() = 0;
194 
202  virtual bool trap(uintptr_t address, bool bWrite) = 0;
203 
209  virtual bool compact()
210  {
211  return false;
212  }
213 
217  bool matches(uintptr_t address)
218  {
219  return (m_Address <= address) && (address < (m_Address + m_Length));
220  }
221 
225  uintptr_t address() const
226  {
227  return m_Address;
228  }
229 
233  size_t length() const
234  {
235  return m_Length;
236  }
237 
238  protected:
247 
254  uintptr_t m_Address;
255 
262  size_t m_Length;
263 
273  Permissions m_Permissions;
274 };
275 
285 {
286  public:
287  AnonymousMemoryMap(uintptr_t address, size_t length, Permissions perms);
288 
289  virtual ~AnonymousMemoryMap()
290  {
291  }
292 
293  virtual MemoryMappedObject *clone();
294  virtual MemoryMappedObject *split(uintptr_t at);
295  virtual bool remove(size_t length);
296 
298 
299  virtual void unmap();
300 
301  virtual bool trap(uintptr_t address, bool bWrite);
302 
303  private:
304  static physical_uintptr_t m_Zero;
305 
306  void unmapUnlocked();
307 
310 
313 };
314 
324 {
325  public:
327  uintptr_t address, size_t length, size_t offset, File *backing,
328  bool bCopyOnWrite, Permissions perms);
329 
330  virtual ~MemoryMappedFile();
331 
332  virtual MemoryMappedObject *clone();
333  virtual MemoryMappedObject *split(uintptr_t at);
334  virtual bool remove(size_t length);
335 
337 
338  virtual void sync(uintptr_t at, bool async);
339  virtual void invalidate(uintptr_t at);
340 
341  virtual void unmap();
342 
343  virtual bool trap(uintptr_t address, bool bWrite);
344 
360  virtual bool compact();
361 
362  private:
363  void unmapUnlocked();
364 
366  void trackMapping(uintptr_t, physical_uintptr_t);
367 
369  void untrackMapping(uintptr_t);
370 
372  physical_uintptr_t getMapping(uintptr_t);
373 
375  size_t getMappingCount();
376 
378  void clearMappings();
379 
382 
384  size_t m_Offset;
385 
388 
391 };
392 
398  public MemoryPressureHandler
399 {
400  friend class PosixSubsystem;
401 
402  public:
405  {
406  return m_Instance;
407  }
408 
412  MemoryMappedObject *mapFile(
413  File *pFile, uintptr_t &address, size_t length,
414  MemoryMappedObject::Permissions perms, size_t offset = 0,
415  bool bCopyOnWrite = true);
416 
420  MemoryMappedObject *mapAnon(
421  uintptr_t &address, size_t length,
423 
429  void clone(Process *pTarget);
430 
440  size_t remove(uintptr_t base, size_t length);
441 
448  size_t setPermissions(
449  uintptr_t base, size_t length, MemoryMappedObject::Permissions perms);
450 
455  bool contains(uintptr_t base, size_t length);
456 
461  void sync(uintptr_t base, size_t length, bool async);
462 
467  void invalidate(uintptr_t base, size_t length);
468 
472  void unmap(MemoryMappedObject *pObj);
473 
477  void unmapAll();
478 
482  virtual bool trap(InterruptState &state, uintptr_t address, bool bIsWrite);
483 
489  virtual bool compact();
490 
491  virtual const String getMemoryPressureDescription()
492  {
493  return String("Unmap safe pages from memory mapped files.");
494  }
495 
496  protected:
502  void unmapAllUnlocked();
503 
513  bool acquireLock();
514 
518  void releaseLock();
519 
520  private:
523  ~MemoryMapManager();
524 
525  bool sanitiseAddress(uintptr_t &address, size_t length);
526 
527  enum Ops
528  {
529  Sync,
530  Invalidate,
531  };
532 
533  void op(Ops what, uintptr_t base, size_t length, bool async);
534 
537 
539 
542 
545 };
546 
549 #endif
static MemoryMapManager m_Instance
virtual bool trap(uintptr_t address, bool bWrite)=0
size_t length() const
virtual void setPermissions(Permissions perms)=0
Permissions m_Permissions
List< void * > m_Mappings
Tree< VirtualAddressSpace *, MmObjectList * > m_MmObjectLists
virtual void unmap()=0
MemoryMappedObject(uintptr_t address, bool bCopyOnWrite, size_t length, Permissions perms)
Definition: String.h:49
bool matches(uintptr_t address)
virtual void sync(uintptr_t at, bool async)
uintptr_t address() const
Tree< uintptr_t, physical_uintptr_t > m_Mappings
static MemoryMapManager & instance()
virtual MemoryMappedObject * split(uintptr_t at)=0
virtual MemoryMappedObject * clone()=0
virtual void invalidate(uintptr_t at)
Definition: File.h:66
virtual bool compact()