xref: /haiku/headers/private/kernel/vm/VMAddressSpace.h (revision fa66a805cce4fd4e4fc501ed6e22c0ed684fab9a)
1 /*
2  * Copyright 2009, 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.
5  *
6  * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
7  * Distributed under the terms of the NewOS License.
8  */
9 #ifndef _KERNEL_VM_VM_ADDRESS_SPACE_H
10 #define _KERNEL_VM_VM_ADDRESS_SPACE_H
11 
12 
13 #include <OS.h>
14 
15 #include <vm/vm_priv.h>
16 #include <vm/vm_translation_map.h>
17 #include <vm/VMArea.h>
18 
19 
20 struct VMAddressSpace {
21 public:
22 			class AreaIterator;
23 
24 public:
25 								VMAddressSpace(team_id id, addr_t base,
26 									size_t size, const char* name);
27 	virtual						~VMAddressSpace();
28 
29 	static	status_t			Init();
30 	static	status_t			InitPostSem();
31 
32 			team_id				ID() const				{ return fID; }
33 			addr_t				Base() const			{ return fBase; }
34 			addr_t				EndAddress() const		{ return fEndAddress; }
35 			size_t				FreeSpace() const		{ return fFreeSpace; }
36 			bool				IsBeingDeleted() const	{ return fDeleting; }
37 
38 			vm_translation_map&	TranslationMap()	{ return fTranslationMap; }
39 
40 			status_t			ReadLock()
41 									{ return rw_lock_read_lock(&fLock); }
42 			void				ReadUnlock()
43 									{ rw_lock_read_unlock(&fLock); }
44 			status_t			WriteLock()
45 									{ return rw_lock_write_lock(&fLock); }
46 			void				WriteUnlock()
47 									{ rw_lock_write_unlock(&fLock); }
48 
49 			int32				RefCount() const
50 									{ return fRefCount; }
51 
52 			void				Get()	{ atomic_add(&fRefCount, 1); }
53 			void 				Put();
54 			void				RemoveAndPut();
55 
56 			void				IncrementFaultCount()
57 									{ atomic_add(&fFaultCount, 1); }
58 			void				IncrementChangeCount()
59 									{ fChangeCount++; }
60 
61 	inline	AreaIterator		GetAreaIterator();
62 
63 			VMAddressSpace*&	HashTableLink()	{ return fHashTableLink; }
64 
65 	virtual	VMArea*				FirstArea() const = 0;
66 	virtual	VMArea*				NextArea(VMArea* area) const = 0;
67 
68 	virtual	VMArea*				LookupArea(addr_t address) const = 0;
69 	virtual	VMArea*				CreateArea(const char* name, uint32 wiring,
70 									uint32 protection) = 0;
71 	virtual	void				DeleteArea(VMArea* area) = 0;
72 	virtual	status_t			InsertArea(void** _address, uint32 addressSpec,
73 									addr_t size, VMArea* area) = 0;
74 	virtual	void				RemoveArea(VMArea* area) = 0;
75 
76 	virtual	bool				CanResizeArea(VMArea* area, size_t newSize) = 0;
77 	virtual	status_t			ResizeArea(VMArea* area, size_t newSize) = 0;
78 	virtual	status_t			ResizeAreaHead(VMArea* area, size_t size) = 0;
79 	virtual	status_t			ResizeAreaTail(VMArea* area, size_t size) = 0;
80 
81 	virtual	status_t			ReserveAddressRange(void** _address,
82 									uint32 addressSpec, size_t size,
83 									uint32 flags) = 0;
84 	virtual	status_t			UnreserveAddressRange(addr_t address,
85 									size_t size) = 0;
86 	virtual	void				UnreserveAllAddressRanges() = 0;
87 
88 	virtual	void				Dump() const;
89 
90 	static	status_t			Create(team_id teamID, addr_t base, size_t size,
91 									bool kernel,
92 									VMAddressSpace** _addressSpace);
93 
94 	static	team_id				KernelID()
95 									{ return sKernelAddressSpace->ID(); }
96 	static	VMAddressSpace*		Kernel()
97 									{ return sKernelAddressSpace; }
98 	static	VMAddressSpace*		GetKernel();
99 
100 	static	team_id				CurrentID();
101 	static	VMAddressSpace*		GetCurrent();
102 
103 	static	VMAddressSpace*		Get(team_id teamID);
104 
105 protected:
106 	static	int					_DumpCommand(int argc, char** argv);
107 	static	int					_DumpListCommand(int argc, char** argv);
108 
109 protected:
110 			struct HashDefinition;
111 
112 protected:
113 			VMAddressSpace*		fHashTableLink;
114 			addr_t				fBase;
115 			addr_t				fEndAddress;		// base + (size - 1)
116 			size_t				fFreeSpace;
117 			rw_lock				fLock;
118 			team_id				fID;
119 			int32				fRefCount;
120 			int32				fFaultCount;
121 			int32				fChangeCount;
122 			vm_translation_map	fTranslationMap;
123 			bool				fDeleting;
124 	static	VMAddressSpace*		sKernelAddressSpace;
125 };
126 
127 
128 class VMAddressSpace::AreaIterator {
129 public:
130 	AreaIterator()
131 	{
132 	}
133 
134 	AreaIterator(VMAddressSpace* addressSpace)
135 		:
136 		fAddressSpace(addressSpace),
137 		fNext(addressSpace->FirstArea())
138 	{
139 	}
140 
141 	bool HasNext() const
142 	{
143 		return fNext != NULL;
144 	}
145 
146 	VMArea* Next()
147 	{
148 		VMArea* result = fNext;
149 		fNext = fAddressSpace->NextArea(fNext);
150 		return result;
151 	}
152 
153 	void Rewind()
154 	{
155 		fNext = fAddressSpace->FirstArea();
156 	}
157 
158 private:
159 	VMAddressSpace*	fAddressSpace;
160 	VMArea*			fNext;
161 };
162 
163 
164 inline VMAddressSpace::AreaIterator
165 VMAddressSpace::GetAreaIterator()
166 {
167 	return AreaIterator(this);
168 }
169 
170 
171 #ifdef __cplusplus
172 extern "C" {
173 #endif
174 
175 status_t vm_delete_areas(struct VMAddressSpace *aspace);
176 #define vm_swap_address_space(from, to) arch_vm_aspace_swap(from, to)
177 
178 #ifdef __cplusplus
179 }
180 #endif
181 
182 
183 #endif	/* _KERNEL_VM_VM_ADDRESS_SPACE_H */
184