2 * Copyright 2009-2010, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Copyright 2002-2008, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
4 * Distributed under the terms of the MIT License.
6 * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
7 * Distributed under the terms of the NewOS License.
9 #ifndef _KERNEL_VM_VM_ADDRESS_SPACE_H
10 #define _KERNEL_VM_VM_ADDRESS_SPACE_H
15 #include <vm/vm_priv.h>
16 #include <vm/VMArea.h>
17 #include <vm/VMTranslationMap.h>
20 struct virtual_address_restrictions
;
23 struct VMAddressSpace
{
28 VMAddressSpace(team_id id
, addr_t base
,
29 size_t size
, const char* name
);
30 virtual ~VMAddressSpace();
32 static status_t
Init();
34 team_id
ID() const { return fID
; }
35 addr_t
Base() const { return fBase
; }
36 addr_t
EndAddress() const { return fEndAddress
; }
37 size_t Size() const { return fEndAddress
- fBase
+ 1; }
38 size_t FreeSpace() const { return fFreeSpace
; }
39 bool IsBeingDeleted() const { return fDeleting
; }
41 VMTranslationMap
* TranslationMap() { return fTranslationMap
; }
44 { return rw_lock_read_lock(&fLock
); }
46 { rw_lock_read_unlock(&fLock
); }
48 { return rw_lock_write_lock(&fLock
); }
50 { rw_lock_write_unlock(&fLock
); }
52 int32
RefCount() const
55 inline void Get() { atomic_add(&fRefCount
, 1); }
59 void IncrementFaultCount()
60 { atomic_add(&fFaultCount
, 1); }
61 void IncrementChangeCount()
64 inline bool IsRandomizingEnabled() const
65 { return fRandomizingEnabled
; }
66 inline void SetRandomizingEnabled(bool enabled
)
67 { fRandomizingEnabled
= enabled
; }
69 inline AreaIterator
GetAreaIterator();
71 VMAddressSpace
*& HashTableLink() { return fHashTableLink
; }
73 virtual status_t
InitObject();
75 virtual VMArea
* FirstArea() const = 0;
76 virtual VMArea
* NextArea(VMArea
* area
) const = 0;
78 virtual VMArea
* LookupArea(addr_t address
) const = 0;
79 virtual VMArea
* CreateArea(const char* name
, uint32 wiring
,
81 uint32 allocationFlags
) = 0;
82 virtual void DeleteArea(VMArea
* area
,
83 uint32 allocationFlags
) = 0;
84 virtual status_t
InsertArea(VMArea
* area
, size_t size
,
85 const virtual_address_restrictions
*
87 uint32 allocationFlags
, void** _address
)
89 virtual void RemoveArea(VMArea
* area
,
90 uint32 allocationFlags
) = 0;
92 virtual bool CanResizeArea(VMArea
* area
, size_t newSize
) = 0;
93 virtual status_t
ResizeArea(VMArea
* area
, size_t newSize
,
94 uint32 allocationFlags
) = 0;
95 virtual status_t
ShrinkAreaHead(VMArea
* area
, size_t newSize
,
96 uint32 allocationFlags
) = 0;
97 virtual status_t
ShrinkAreaTail(VMArea
* area
, size_t newSize
,
98 uint32 allocationFlags
) = 0;
100 virtual status_t
ReserveAddressRange(size_t size
,
101 const virtual_address_restrictions
*
103 uint32 flags
, uint32 allocationFlags
,
104 void** _address
) = 0;
105 virtual status_t
UnreserveAddressRange(addr_t address
,
106 size_t size
, uint32 allocationFlags
) = 0;
107 virtual void UnreserveAllAddressRanges(
108 uint32 allocationFlags
) = 0;
110 virtual void Dump() const;
112 static status_t
Create(team_id teamID
, addr_t base
, size_t size
,
114 VMAddressSpace
** _addressSpace
);
116 static team_id
KernelID()
117 { return sKernelAddressSpace
->ID(); }
118 static VMAddressSpace
* Kernel()
119 { return sKernelAddressSpace
; }
120 static VMAddressSpace
* GetKernel();
122 static team_id
CurrentID();
123 static VMAddressSpace
* GetCurrent();
125 static VMAddressSpace
* Get(team_id teamID
);
127 static VMAddressSpace
* DebugFirst();
128 static VMAddressSpace
* DebugNext(VMAddressSpace
* addressSpace
);
129 static VMAddressSpace
* DebugGet(team_id teamID
);
132 static void _DeleteIfUnreferenced(team_id id
);
134 static int _DumpCommand(int argc
, char** argv
);
135 static int _DumpListCommand(int argc
, char** argv
);
138 struct HashDefinition
;
141 VMAddressSpace
* fHashTableLink
;
143 addr_t fEndAddress
; // base + (size - 1)
150 VMTranslationMap
* fTranslationMap
;
151 bool fRandomizingEnabled
;
153 static VMAddressSpace
* sKernelAddressSpace
;
158 VMAddressSpace::Put()
161 if (atomic_add(&fRefCount
, -1) == 1)
162 _DeleteIfUnreferenced(id
);
166 class VMAddressSpace::AreaIterator
{
172 AreaIterator(VMAddressSpace
* addressSpace
)
174 fAddressSpace(addressSpace
),
175 fNext(addressSpace
->FirstArea())
181 return fNext
!= NULL
;
186 VMArea
* result
= fNext
;
188 fNext
= fAddressSpace
->NextArea(fNext
);
194 fNext
= fAddressSpace
->FirstArea();
198 VMAddressSpace
* fAddressSpace
;
203 inline VMAddressSpace::AreaIterator
204 VMAddressSpace::GetAreaIterator()
206 return AreaIterator(this);
214 void vm_delete_areas(struct VMAddressSpace
*aspace
, bool deletingAddressSpace
);
215 #define vm_swap_address_space(from, to) arch_vm_aspace_swap(from, to)
222 #endif /* _KERNEL_VM_VM_ADDRESS_SPACE_H */