xref: /haiku/src/system/kernel/arch/x86/arch_vm.cpp (revision 4988ca58a0b6f12e48286fe17ee651be490e0f24)
1393fceb5SAxel Dörfler /*
2dac21d8bSIngo Weinhold  * Copyright 2009-2010, Ingo Weinhold, ingo_weinhold@gmx.de.
3b20d05b4SJérôme Duval  * Copyright 2008, Jérôme Duval.
4bb163c02SIngo Weinhold  * Copyright 2002-2007, Axel Dörfler, axeld@pinc-software.de.
5393fceb5SAxel Dörfler  * Distributed under the terms of the MIT License.
6393fceb5SAxel Dörfler  *
7393fceb5SAxel Dörfler  * Copyright 2001, Travis Geiselbrecht. All rights reserved.
8393fceb5SAxel Dörfler  * Distributed under the terms of the NewOS License.
9393fceb5SAxel Dörfler  */
10393fceb5SAxel Dörfler 
11393fceb5SAxel Dörfler 
1274785e79SIngo Weinhold #include <stdlib.h>
1374785e79SIngo Weinhold #include <string.h>
1474785e79SIngo Weinhold 
15dac21d8bSIngo Weinhold #include <algorithm>
16dac21d8bSIngo Weinhold #include <new>
17dac21d8bSIngo Weinhold 
18393fceb5SAxel Dörfler #include <KernelExport.h>
1974785e79SIngo Weinhold 
2045bd7bb3SIngo Weinhold #include <boot/kernel_args.h>
21393fceb5SAxel Dörfler #include <smp.h>
22393fceb5SAxel Dörfler #include <util/AutoLock.h>
23e50cf876SIngo Weinhold #include <vm/vm.h>
24e50cf876SIngo Weinhold #include <vm/vm_page.h>
25e50cf876SIngo Weinhold #include <vm/vm_priv.h>
26e50cf876SIngo Weinhold #include <vm/VMAddressSpace.h>
27f34a1dd5SIngo Weinhold #include <vm/VMArea.h>
28393fceb5SAxel Dörfler 
29393fceb5SAxel Dörfler #include <arch/vm.h>
30393fceb5SAxel Dörfler #include <arch/int.h>
31393fceb5SAxel Dörfler #include <arch/cpu.h>
32393fceb5SAxel Dörfler 
33393fceb5SAxel Dörfler #include <arch/x86/bios.h>
34393fceb5SAxel Dörfler 
35393fceb5SAxel Dörfler 
36393fceb5SAxel Dörfler //#define TRACE_ARCH_VM
37393fceb5SAxel Dörfler #ifdef TRACE_ARCH_VM
38393fceb5SAxel Dörfler #	define TRACE(x) dprintf x
39393fceb5SAxel Dörfler #else
40393fceb5SAxel Dörfler #	define TRACE(x) ;
41393fceb5SAxel Dörfler #endif
42393fceb5SAxel Dörfler 
43dac21d8bSIngo Weinhold // 0: disabled, 1: some, 2: more
44dac21d8bSIngo Weinhold #define TRACE_MTRR_ARCH_VM 1
45dac21d8bSIngo Weinhold 
46dac21d8bSIngo Weinhold #if TRACE_MTRR_ARCH_VM >= 1
474f893e39SJérôme Duval #	define TRACE_MTRR(x...) dprintf(x)
484f893e39SJérôme Duval #else
494f893e39SJérôme Duval #	define TRACE_MTRR(x...)
504f893e39SJérôme Duval #endif
514f893e39SJérôme Duval 
52dac21d8bSIngo Weinhold #if TRACE_MTRR_ARCH_VM >= 2
53dac21d8bSIngo Weinhold #	define TRACE_MTRR2(x...) dprintf(x)
54dac21d8bSIngo Weinhold #else
55dac21d8bSIngo Weinhold #	define TRACE_MTRR2(x...)
56dac21d8bSIngo Weinhold #endif
57bb163c02SIngo Weinhold 
58bb163c02SIngo Weinhold 
59dac21d8bSIngo Weinhold void *gDmaAddress;
60bb163c02SIngo Weinhold 
61bb163c02SIngo Weinhold 
62dac21d8bSIngo Weinhold struct memory_type_range : DoublyLinkedListLinkImpl<memory_type_range> {
63bb163c02SIngo Weinhold 	uint64						base;
64bb163c02SIngo Weinhold 	uint64						size;
65bb163c02SIngo Weinhold 	uint32						type;
66bb163c02SIngo Weinhold 	area_id						area;
67bb163c02SIngo Weinhold };
68bb163c02SIngo Weinhold 
69393fceb5SAxel Dörfler 
70dac21d8bSIngo Weinhold struct memory_type_range_point
71dac21d8bSIngo Weinhold 		: DoublyLinkedListLinkImpl<memory_type_range_point> {
72dac21d8bSIngo Weinhold 	uint64				address;
73dac21d8bSIngo Weinhold 	memory_type_range*	range;
74393fceb5SAxel Dörfler 
75dac21d8bSIngo Weinhold 	bool IsStart() const	{ return range->base == address; }
76bb163c02SIngo Weinhold 
77dac21d8bSIngo Weinhold 	bool operator<(const memory_type_range_point& other) const
78dac21d8bSIngo Weinhold 	{
79dac21d8bSIngo Weinhold 		return address < other.address;
80dac21d8bSIngo Weinhold 	}
81dac21d8bSIngo Weinhold };
82bb163c02SIngo Weinhold 
83dac21d8bSIngo Weinhold 
84fa0c1e96SIngo Weinhold struct update_mtrr_info {
85fa0c1e96SIngo Weinhold 	uint64	ignoreUncacheableSize;
86fa0c1e96SIngo Weinhold 	uint64	shortestUncacheableSize;
87fa0c1e96SIngo Weinhold };
88fa0c1e96SIngo Weinhold 
89fa0c1e96SIngo Weinhold 
90dac21d8bSIngo Weinhold typedef DoublyLinkedList<memory_type_range> MemoryTypeRangeList;
91dac21d8bSIngo Weinhold 
92dac21d8bSIngo Weinhold static mutex sMemoryTypeLock = MUTEX_INITIALIZER("memory type ranges");
93dac21d8bSIngo Weinhold static MemoryTypeRangeList sMemoryTypeRanges;
94dac21d8bSIngo Weinhold static int32 sMemoryTypeRangeCount = 0;
95dac21d8bSIngo Weinhold 
96dac21d8bSIngo Weinhold static const uint32 kMaxMemoryTypeRegisters	= 32;
97bb163c02SIngo Weinhold static x86_mtrr_info sMemoryTypeRegisters[kMaxMemoryTypeRegisters];
98393fceb5SAxel Dörfler static uint32 sMemoryTypeRegisterCount;
99bb163c02SIngo Weinhold static uint32 sMemoryTypeRegistersUsed;
100bb163c02SIngo Weinhold 
101dac21d8bSIngo Weinhold static memory_type_range* sTemporaryRanges = NULL;
102dac21d8bSIngo Weinhold static memory_type_range_point* sTemporaryRangePoints = NULL;
103dac21d8bSIngo Weinhold static int32 sTemporaryRangeCount = 0;
104dac21d8bSIngo Weinhold static int32 sTemporaryRangePointCount = 0;
105393fceb5SAxel Dörfler 
106393fceb5SAxel Dörfler 
107bb163c02SIngo Weinhold static void
108bb163c02SIngo Weinhold set_mtrrs()
109393fceb5SAxel Dörfler {
110dac21d8bSIngo Weinhold 	x86_set_mtrrs(IA32_MTR_WRITE_BACK, sMemoryTypeRegisters,
111dac21d8bSIngo Weinhold 		sMemoryTypeRegistersUsed);
112393fceb5SAxel Dörfler 
113dac21d8bSIngo Weinhold #if TRACE_MTRR_ARCH_VM
114bb163c02SIngo Weinhold 	TRACE_MTRR("set MTRRs to:\n");
115bb163c02SIngo Weinhold 	for (uint32 i = 0; i < sMemoryTypeRegistersUsed; i++) {
116bb163c02SIngo Weinhold 		const x86_mtrr_info& info = sMemoryTypeRegisters[i];
117dac21d8bSIngo Weinhold 		TRACE_MTRR("  mtrr: %2" B_PRIu32 ": base: %#10" B_PRIx64  ", size: %#10"
118dac21d8bSIngo Weinhold 			B_PRIx64 ", type: %u\n", i, info.base, info.size,
119dac21d8bSIngo Weinhold 			info.type);
120393fceb5SAxel Dörfler 	}
121bb163c02SIngo Weinhold #endif
122393fceb5SAxel Dörfler }
123393fceb5SAxel Dörfler 
124393fceb5SAxel Dörfler 
125dac21d8bSIngo Weinhold static bool
126bb163c02SIngo Weinhold add_used_mtrr(uint64 base, uint64 size, uint32 type)
127393fceb5SAxel Dörfler {
128fa0c1e96SIngo Weinhold 	if (sMemoryTypeRegistersUsed == sMemoryTypeRegisterCount)
129dac21d8bSIngo Weinhold 		return false;
130393fceb5SAxel Dörfler 
131dac21d8bSIngo Weinhold 	x86_mtrr_info& mtrr = sMemoryTypeRegisters[sMemoryTypeRegistersUsed++];
132dac21d8bSIngo Weinhold 	mtrr.base = base;
133dac21d8bSIngo Weinhold 	mtrr.size = size;
134dac21d8bSIngo Weinhold 	mtrr.type = type;
135dac21d8bSIngo Weinhold 
136dac21d8bSIngo Weinhold 	return true;
137dac21d8bSIngo Weinhold }
138dac21d8bSIngo Weinhold 
139dac21d8bSIngo Weinhold 
140dac21d8bSIngo Weinhold static bool
141dac21d8bSIngo Weinhold add_mtrrs_for_range(uint64 base, uint64 size, uint32 type)
142dac21d8bSIngo Weinhold {
143dac21d8bSIngo Weinhold 	for (uint64 interval = B_PAGE_SIZE; size > 0; interval <<= 1) {
144dac21d8bSIngo Weinhold 		if ((base & interval) != 0) {
145dac21d8bSIngo Weinhold 			if (!add_used_mtrr(base, interval, type))
146dac21d8bSIngo Weinhold 				return false;
147dac21d8bSIngo Weinhold 			base += interval;
148affb4716SIngo Weinhold 			size -= interval;
149dac21d8bSIngo Weinhold 		}
150dac21d8bSIngo Weinhold 
151affb4716SIngo Weinhold 		if ((size & interval) != 0) {
152affb4716SIngo Weinhold 			if (!add_used_mtrr(base + size - interval, interval, type))
153affb4716SIngo Weinhold 				return false;
154dac21d8bSIngo Weinhold 			size -= interval;
155dac21d8bSIngo Weinhold 		}
156dac21d8bSIngo Weinhold 	}
157dac21d8bSIngo Weinhold 
158dac21d8bSIngo Weinhold 	return true;
159dac21d8bSIngo Weinhold }
160dac21d8bSIngo Weinhold 
161dac21d8bSIngo Weinhold 
162dac21d8bSIngo Weinhold static memory_type_range*
163dac21d8bSIngo Weinhold find_range(area_id areaID)
164dac21d8bSIngo Weinhold {
165dac21d8bSIngo Weinhold 	for (MemoryTypeRangeList::Iterator it = sMemoryTypeRanges.GetIterator();
166dac21d8bSIngo Weinhold 			memory_type_range* range = it.Next();) {
167dac21d8bSIngo Weinhold 		if (range->area == areaID)
168dac21d8bSIngo Weinhold 			return range;
169dac21d8bSIngo Weinhold 	}
170dac21d8bSIngo Weinhold 
171dac21d8bSIngo Weinhold 	return NULL;
172393fceb5SAxel Dörfler }
173393fceb5SAxel Dörfler 
174393fceb5SAxel Dörfler 
1754f893e39SJérôme Duval static void
176dac21d8bSIngo Weinhold optimize_memory_ranges(MemoryTypeRangeList& ranges, uint32 type,
177dac21d8bSIngo Weinhold 	bool removeRanges)
1784f893e39SJérôme Duval {
179bb163c02SIngo Weinhold 	uint64 previousEnd = 0;
180dac21d8bSIngo Weinhold 	uint64 nextStart = 0;
181dac21d8bSIngo Weinhold 	MemoryTypeRangeList::Iterator it = ranges.GetIterator();
182dac21d8bSIngo Weinhold 	memory_type_range* range = it.Next();
183dac21d8bSIngo Weinhold 	while (range != NULL) {
184dac21d8bSIngo Weinhold 		if (range->type != type) {
185dac21d8bSIngo Weinhold 			previousEnd = range->base + range->size;
186dac21d8bSIngo Weinhold 			nextStart = 0;
187dac21d8bSIngo Weinhold 			range = it.Next();
188dac21d8bSIngo Weinhold 			continue;
189bb163c02SIngo Weinhold 		}
190bb163c02SIngo Weinhold 
191dac21d8bSIngo Weinhold 		// find the start of the next range we cannot join this one with
192dac21d8bSIngo Weinhold 		if (nextStart == 0) {
193dac21d8bSIngo Weinhold 			MemoryTypeRangeList::Iterator nextIt = it;
194dac21d8bSIngo Weinhold 			while (memory_type_range* nextRange = nextIt.Next()) {
195dac21d8bSIngo Weinhold 				if (nextRange->type != range->type) {
196dac21d8bSIngo Weinhold 					nextStart = nextRange->base;
197dac21d8bSIngo Weinhold 					break;
198dac21d8bSIngo Weinhold 				}
199dac21d8bSIngo Weinhold 			}
200bb163c02SIngo Weinhold 
201dac21d8bSIngo Weinhold 			if (nextStart == 0) {
202dac21d8bSIngo Weinhold 				// no upper limit -- set an artificial one, so we don't need to
203dac21d8bSIngo Weinhold 				// special case below
204dac21d8bSIngo Weinhold 				nextStart = (uint64)1 << 32;
205dac21d8bSIngo Weinhold 			}
206dac21d8bSIngo Weinhold 		}
207dac21d8bSIngo Weinhold 
208dac21d8bSIngo Weinhold 		// Align the range's base and end to the greatest power of two possible.
209dac21d8bSIngo Weinhold 		// As long as we can align both without intersecting any differently
210dac21d8bSIngo Weinhold 		// range, we can extend the range without making it more complicated.
211dac21d8bSIngo Weinhold 		// Once one side hit a limit we need to be careful. We can still
212dac21d8bSIngo Weinhold 		// continue aligning the other side, if the range crosses the power of
213dac21d8bSIngo Weinhold 		// two boundary.
214dac21d8bSIngo Weinhold 		uint64 rangeBase = range->base;
215dac21d8bSIngo Weinhold 		uint64 rangeEnd = rangeBase + range->size;
216dac21d8bSIngo Weinhold 		uint64 interval = B_PAGE_SIZE * 2;
217dac21d8bSIngo Weinhold 		while (true) {
218dac21d8bSIngo Weinhold 			uint64 alignedBase = rangeBase & ~(interval - 1);
219dac21d8bSIngo Weinhold 			uint64 alignedEnd = (rangeEnd + interval - 1) & ~(interval - 1);
220dac21d8bSIngo Weinhold 
221dac21d8bSIngo Weinhold 			if (alignedBase < previousEnd)
222dac21d8bSIngo Weinhold 				alignedBase += interval;
223dac21d8bSIngo Weinhold 
224dac21d8bSIngo Weinhold 			if (alignedEnd > nextStart)
225dac21d8bSIngo Weinhold 				alignedEnd -= interval;
226dac21d8bSIngo Weinhold 
227dac21d8bSIngo Weinhold 			if (alignedBase >= alignedEnd)
228dac21d8bSIngo Weinhold 				break;
229dac21d8bSIngo Weinhold 
230dac21d8bSIngo Weinhold 			rangeBase = std::min(rangeBase, alignedBase);
231dac21d8bSIngo Weinhold 			rangeEnd = std::max(rangeEnd, alignedEnd);
232dac21d8bSIngo Weinhold 
233dac21d8bSIngo Weinhold 			interval <<= 1;
234dac21d8bSIngo Weinhold 		}
235dac21d8bSIngo Weinhold 
236dac21d8bSIngo Weinhold 		range->base = rangeBase;
237dac21d8bSIngo Weinhold 		range->size = rangeEnd - rangeBase;
238dac21d8bSIngo Weinhold 
239dac21d8bSIngo Weinhold 		if (removeRanges)
240dac21d8bSIngo Weinhold 			it.Remove();
241dac21d8bSIngo Weinhold 
242dac21d8bSIngo Weinhold 		previousEnd = rangeEnd;
243dac21d8bSIngo Weinhold 
244dac21d8bSIngo Weinhold 		// Skip the subsequent ranges we have swallowed and possible cut one
245dac21d8bSIngo Weinhold 		// we now partially intersect with.
246dac21d8bSIngo Weinhold 		while ((range = it.Next()) != NULL) {
247dac21d8bSIngo Weinhold 			if (range->base >= rangeEnd)
248dac21d8bSIngo Weinhold 				break;
249dac21d8bSIngo Weinhold 
250dac21d8bSIngo Weinhold 			if (range->base + range->size > rangeEnd) {
251dac21d8bSIngo Weinhold 				// we partially intersect -- cut the range
252dac21d8bSIngo Weinhold 				range->size = range->base + range->size - rangeEnd;
253dac21d8bSIngo Weinhold 				range->base = rangeEnd;
254dac21d8bSIngo Weinhold 				break;
255dac21d8bSIngo Weinhold 			}
256dac21d8bSIngo Weinhold 
257dac21d8bSIngo Weinhold 			// we have swallowed this range completely
258dac21d8bSIngo Weinhold 			range->size = 0;
259dac21d8bSIngo Weinhold 			it.Remove();
260dac21d8bSIngo Weinhold 		}
261dac21d8bSIngo Weinhold 	}
262dac21d8bSIngo Weinhold }
263dac21d8bSIngo Weinhold 
264dac21d8bSIngo Weinhold 
265dac21d8bSIngo Weinhold static bool
266dac21d8bSIngo Weinhold ensure_temporary_ranges_space(int32 count)
267dac21d8bSIngo Weinhold {
268dac21d8bSIngo Weinhold 	if (sTemporaryRangeCount >= count && sTemporaryRangePointCount >= count)
269dac21d8bSIngo Weinhold 		return true;
270dac21d8bSIngo Weinhold 
271dac21d8bSIngo Weinhold 	// round count to power of 2
272dac21d8bSIngo Weinhold 	int32 unalignedCount = count;
273dac21d8bSIngo Weinhold 	count = 8;
274dac21d8bSIngo Weinhold 	while (count < unalignedCount)
275dac21d8bSIngo Weinhold 		count <<= 1;
276dac21d8bSIngo Weinhold 
277dac21d8bSIngo Weinhold 	// resize ranges array
278dac21d8bSIngo Weinhold 	if (sTemporaryRangeCount < count) {
279dac21d8bSIngo Weinhold 		memory_type_range* ranges = new(std::nothrow) memory_type_range[count];
280dac21d8bSIngo Weinhold 		if (ranges == NULL)
281dac21d8bSIngo Weinhold 			return false;
282dac21d8bSIngo Weinhold 
283dac21d8bSIngo Weinhold 		delete[] sTemporaryRanges;
284dac21d8bSIngo Weinhold 
285dac21d8bSIngo Weinhold 		sTemporaryRanges = ranges;
286dac21d8bSIngo Weinhold 		sTemporaryRangeCount = count;
287dac21d8bSIngo Weinhold 	}
288dac21d8bSIngo Weinhold 
289dac21d8bSIngo Weinhold 	// resize points array
290dac21d8bSIngo Weinhold 	if (sTemporaryRangePointCount < count) {
291dac21d8bSIngo Weinhold 		memory_type_range_point* points
292dac21d8bSIngo Weinhold 			= new(std::nothrow) memory_type_range_point[count];
293dac21d8bSIngo Weinhold 		if (points == NULL)
294dac21d8bSIngo Weinhold 			return false;
295dac21d8bSIngo Weinhold 
296dac21d8bSIngo Weinhold 		delete[] sTemporaryRangePoints;
297dac21d8bSIngo Weinhold 
298dac21d8bSIngo Weinhold 		sTemporaryRangePoints = points;
299dac21d8bSIngo Weinhold 		sTemporaryRangePointCount = count;
300dac21d8bSIngo Weinhold 	}
301dac21d8bSIngo Weinhold 
302dac21d8bSIngo Weinhold 	return true;
303dac21d8bSIngo Weinhold }
304dac21d8bSIngo Weinhold 
305dac21d8bSIngo Weinhold 
306dac21d8bSIngo Weinhold status_t
307fa0c1e96SIngo Weinhold update_mtrrs(update_mtrr_info& updateInfo)
308dac21d8bSIngo Weinhold {
309dac21d8bSIngo Weinhold 	// resize the temporary points/ranges arrays, if necessary
310dac21d8bSIngo Weinhold 	if (!ensure_temporary_ranges_space(sMemoryTypeRangeCount * 2))
311dac21d8bSIngo Weinhold 		return B_NO_MEMORY;
312dac21d8bSIngo Weinhold 
313dac21d8bSIngo Weinhold 	// get the range points and sort them
314dac21d8bSIngo Weinhold 	memory_type_range_point* rangePoints = sTemporaryRangePoints;
315dac21d8bSIngo Weinhold 	int32 pointCount = 0;
316dac21d8bSIngo Weinhold 	for (MemoryTypeRangeList::Iterator it = sMemoryTypeRanges.GetIterator();
317dac21d8bSIngo Weinhold 			memory_type_range* range = it.Next();) {
318fa0c1e96SIngo Weinhold 		if (range->type == IA32_MTR_UNCACHED) {
319fa0c1e96SIngo Weinhold 			// Ignore uncacheable ranges below a certain size, if requested.
320fa0c1e96SIngo Weinhold 			// Since we always enforce uncacheability via the PTE attributes,
321fa0c1e96SIngo Weinhold 			// this is no problem (though not recommended for performance
322fa0c1e96SIngo Weinhold 			// reasons).
323fa0c1e96SIngo Weinhold 			if (range->size <= updateInfo.ignoreUncacheableSize)
324fa0c1e96SIngo Weinhold 				continue;
325fa0c1e96SIngo Weinhold 			if (range->size < updateInfo.shortestUncacheableSize)
326fa0c1e96SIngo Weinhold 				updateInfo.shortestUncacheableSize = range->size;
327fa0c1e96SIngo Weinhold 		}
328fa0c1e96SIngo Weinhold 
329dac21d8bSIngo Weinhold 		rangePoints[pointCount].address = range->base;
330dac21d8bSIngo Weinhold 		rangePoints[pointCount++].range = range;
331dac21d8bSIngo Weinhold 		rangePoints[pointCount].address = range->base + range->size;
332dac21d8bSIngo Weinhold 		rangePoints[pointCount++].range = range;
333dac21d8bSIngo Weinhold 	}
334dac21d8bSIngo Weinhold 
335dac21d8bSIngo Weinhold 	std::sort(rangePoints, rangePoints + pointCount);
336dac21d8bSIngo Weinhold 
337dac21d8bSIngo Weinhold #if TRACE_MTRR_ARCH_VM >= 2
338dac21d8bSIngo Weinhold 	TRACE_MTRR2("memory type range points:\n");
339dac21d8bSIngo Weinhold 	for (int32 i = 0; i < pointCount; i++) {
340dac21d8bSIngo Weinhold 		TRACE_MTRR2("%12" B_PRIx64 " (%p)\n", rangePoints[i].address,
341dac21d8bSIngo Weinhold 			rangePoints[i].range);
342dac21d8bSIngo Weinhold 	}
343dac21d8bSIngo Weinhold #endif
344dac21d8bSIngo Weinhold 
345dac21d8bSIngo Weinhold 	// Compute the effective ranges. When ranges overlap, we go with the
346dac21d8bSIngo Weinhold 	// stricter requirement. The types are not necessarily totally ordered, so
347dac21d8bSIngo Weinhold 	// the order we use below is not always correct. To keep it simple we
348dac21d8bSIngo Weinhold 	// consider it the reponsibility of the callers not to define overlapping
349dac21d8bSIngo Weinhold 	// memory ranges with uncomparable types.
350dac21d8bSIngo Weinhold 
351dac21d8bSIngo Weinhold 	memory_type_range* ranges = sTemporaryRanges;
352dac21d8bSIngo Weinhold 	typedef DoublyLinkedList<memory_type_range_point> PointList;
353dac21d8bSIngo Weinhold 	PointList pendingPoints;
354dac21d8bSIngo Weinhold 	memory_type_range* activeRange = NULL;
355dac21d8bSIngo Weinhold 	int32 rangeCount = 0;
356dac21d8bSIngo Weinhold 
357dac21d8bSIngo Weinhold 	for (int32 i = 0; i < pointCount; i++) {
358dac21d8bSIngo Weinhold 		memory_type_range_point* point = &rangePoints[i];
359dac21d8bSIngo Weinhold 		bool terminateRange = false;
360dac21d8bSIngo Weinhold 		if (point->IsStart()) {
361dac21d8bSIngo Weinhold 			// a range start point
362dac21d8bSIngo Weinhold 			pendingPoints.Add(point);
363dac21d8bSIngo Weinhold 			if (activeRange != NULL && activeRange->type > point->range->type)
364dac21d8bSIngo Weinhold 				terminateRange = true;
365dac21d8bSIngo Weinhold 		} else {
366dac21d8bSIngo Weinhold 			// a range end point -- remove the pending start point
367dac21d8bSIngo Weinhold 			for (PointList::Iterator it = pendingPoints.GetIterator();
368dac21d8bSIngo Weinhold 					memory_type_range_point* pendingPoint = it.Next();) {
369dac21d8bSIngo Weinhold 				if (pendingPoint->range == point->range) {
370dac21d8bSIngo Weinhold 					it.Remove();
371dac21d8bSIngo Weinhold 					break;
372dac21d8bSIngo Weinhold 				}
373dac21d8bSIngo Weinhold 			}
374dac21d8bSIngo Weinhold 
375dac21d8bSIngo Weinhold 			if (point->range == activeRange)
376dac21d8bSIngo Weinhold 				terminateRange = true;
377dac21d8bSIngo Weinhold 		}
378dac21d8bSIngo Weinhold 
379dac21d8bSIngo Weinhold 		if (terminateRange) {
380dac21d8bSIngo Weinhold 			ranges[rangeCount].size = point->address - ranges[rangeCount].base;
381dac21d8bSIngo Weinhold 			rangeCount++;
382dac21d8bSIngo Weinhold 			activeRange = NULL;
383dac21d8bSIngo Weinhold 		}
384dac21d8bSIngo Weinhold 
385dac21d8bSIngo Weinhold 		if (activeRange != NULL || pendingPoints.IsEmpty())
386dac21d8bSIngo Weinhold 			continue;
387dac21d8bSIngo Weinhold 
388dac21d8bSIngo Weinhold 		// we need to start a new range -- find the strictest pending range
389dac21d8bSIngo Weinhold 		for (PointList::Iterator it = pendingPoints.GetIterator();
390dac21d8bSIngo Weinhold 				memory_type_range_point* pendingPoint = it.Next();) {
391dac21d8bSIngo Weinhold 			memory_type_range* pendingRange = pendingPoint->range;
392dac21d8bSIngo Weinhold 			if (activeRange == NULL || activeRange->type > pendingRange->type)
393dac21d8bSIngo Weinhold 				activeRange = pendingRange;
394dac21d8bSIngo Weinhold 		}
395dac21d8bSIngo Weinhold 
396dac21d8bSIngo Weinhold 		memory_type_range* previousRange = rangeCount > 0
397dac21d8bSIngo Weinhold 			? &ranges[rangeCount - 1] : NULL;
398dac21d8bSIngo Weinhold 		if (previousRange == NULL || previousRange->type != activeRange->type
399dac21d8bSIngo Weinhold 				|| previousRange->base + previousRange->size
400dac21d8bSIngo Weinhold 					< activeRange->base) {
401dac21d8bSIngo Weinhold 			// we can't join with the previous range -- add a new one
402dac21d8bSIngo Weinhold 			ranges[rangeCount].base = point->address;
403dac21d8bSIngo Weinhold 			ranges[rangeCount].type = activeRange->type;
404dac21d8bSIngo Weinhold 		} else
405dac21d8bSIngo Weinhold 			rangeCount--;
406dac21d8bSIngo Weinhold 	}
407dac21d8bSIngo Weinhold 
408dac21d8bSIngo Weinhold #if TRACE_MTRR_ARCH_VM >= 2
409dac21d8bSIngo Weinhold 	TRACE_MTRR2("effective memory type ranges:\n");
410dac21d8bSIngo Weinhold 	for (int32 i = 0; i < rangeCount; i++) {
411dac21d8bSIngo Weinhold 		TRACE_MTRR2("%12" B_PRIx64 " - %12" B_PRIx64 ": %" B_PRIu32 "\n",
412dac21d8bSIngo Weinhold 			ranges[i].base, ranges[i].base + ranges[i].size, ranges[i].type);
413dac21d8bSIngo Weinhold 	}
414dac21d8bSIngo Weinhold #endif
415dac21d8bSIngo Weinhold 
416dac21d8bSIngo Weinhold 	// Extend ranges to be more MTRR-friendly. A range is MTRR friendly, when it
417dac21d8bSIngo Weinhold 	// has a power of two size and a base address aligned to the size. For
418dac21d8bSIngo Weinhold 	// ranges without this property we need more than one MTRR. We improve
419dac21d8bSIngo Weinhold 	// MTRR-friendliness by aligning a range's base and end address to the
420dac21d8bSIngo Weinhold 	// greatest power of two (base rounded down, end up) such that the extended
421dac21d8bSIngo Weinhold 	// range does not intersect with any other differently typed range. We join
422dac21d8bSIngo Weinhold 	// equally typed ranges, if possible. There are two exceptions to the
423dac21d8bSIngo Weinhold 	// intersection requirement: Uncached ranges may intersect with any other
424dac21d8bSIngo Weinhold 	// range; the resulting type will still be uncached. Hence we can ignore
425fa0c1e96SIngo Weinhold 	// uncached ranges when extending the other ranges. Write-through ranges may
426dac21d8bSIngo Weinhold 	// intersect with write-back ranges; the resulting type will be
427dac21d8bSIngo Weinhold 	// write-through. Hence we can ignore write-through ranges when extending
428dac21d8bSIngo Weinhold 	// write-back ranges.
429dac21d8bSIngo Weinhold 
430dac21d8bSIngo Weinhold 	MemoryTypeRangeList rangeList;
431dac21d8bSIngo Weinhold 	for (int32 i = 0; i < rangeCount; i++)
432dac21d8bSIngo Weinhold 		rangeList.Add(&ranges[i]);
433dac21d8bSIngo Weinhold 
434dac21d8bSIngo Weinhold 	static const uint32 kMemoryTypes[] = {
435dac21d8bSIngo Weinhold 		IA32_MTR_UNCACHED,
436dac21d8bSIngo Weinhold 		IA32_MTR_WRITE_COMBINING,
437dac21d8bSIngo Weinhold 		IA32_MTR_WRITE_PROTECTED,
438dac21d8bSIngo Weinhold 		IA32_MTR_WRITE_THROUGH,
439dac21d8bSIngo Weinhold 		IA32_MTR_WRITE_BACK
440dac21d8bSIngo Weinhold 	};
441dac21d8bSIngo Weinhold 	static const int32 kMemoryTypeCount = sizeof(kMemoryTypes)
442dac21d8bSIngo Weinhold 		/ sizeof(*kMemoryTypes);
443dac21d8bSIngo Weinhold 
444dac21d8bSIngo Weinhold 	for (int32 i = 0; i < kMemoryTypeCount; i++) {
445dac21d8bSIngo Weinhold 		uint32 type = kMemoryTypes[i];
446dac21d8bSIngo Weinhold 
447dac21d8bSIngo Weinhold 		// Remove uncached and write-through ranges after processing them. This
448dac21d8bSIngo Weinhold 		// let's us leverage their intersection property with any other
449dac21d8bSIngo Weinhold 		// respectively write-back ranges.
450dac21d8bSIngo Weinhold 		bool removeRanges = type == IA32_MTR_UNCACHED
451dac21d8bSIngo Weinhold 			|| type == IA32_MTR_WRITE_THROUGH;
452dac21d8bSIngo Weinhold 
453dac21d8bSIngo Weinhold 		optimize_memory_ranges(rangeList, type, removeRanges);
454dac21d8bSIngo Weinhold 	}
455dac21d8bSIngo Weinhold 
456dac21d8bSIngo Weinhold #if TRACE_MTRR_ARCH_VM >= 2
457dac21d8bSIngo Weinhold 	TRACE_MTRR2("optimized memory type ranges:\n");
458dac21d8bSIngo Weinhold 	for (int32 i = 0; i < rangeCount; i++) {
459dac21d8bSIngo Weinhold 		if (ranges[i].size > 0) {
460dac21d8bSIngo Weinhold 			TRACE_MTRR2("%12" B_PRIx64 " - %12" B_PRIx64 ": %" B_PRIu32 "\n",
461dac21d8bSIngo Weinhold 				ranges[i].base, ranges[i].base + ranges[i].size,
462dac21d8bSIngo Weinhold 				ranges[i].type);
463dac21d8bSIngo Weinhold 		}
464dac21d8bSIngo Weinhold 	}
465dac21d8bSIngo Weinhold #endif
466dac21d8bSIngo Weinhold 
467dac21d8bSIngo Weinhold 	// compute the mtrrs from the ranges
468bb163c02SIngo Weinhold 	sMemoryTypeRegistersUsed = 0;
469dac21d8bSIngo Weinhold 	for (int32 i = 0; i < kMemoryTypeCount; i++) {
470dac21d8bSIngo Weinhold 		uint32 type = kMemoryTypes[i];
471bb163c02SIngo Weinhold 
472dac21d8bSIngo Weinhold 		// skip write-back ranges -- that'll be the default type anyway
473dac21d8bSIngo Weinhold 		if (type == IA32_MTR_WRITE_BACK)
474dac21d8bSIngo Weinhold 			continue;
475dac21d8bSIngo Weinhold 
476dac21d8bSIngo Weinhold 		for (int32 i = 0; i < rangeCount; i++) {
477dac21d8bSIngo Weinhold 			if (ranges[i].size == 0 || ranges[i].type != type)
478dac21d8bSIngo Weinhold 				continue;
479dac21d8bSIngo Weinhold 
480fa0c1e96SIngo Weinhold 			if (!add_mtrrs_for_range(ranges[i].base, ranges[i].size, type))
481fa0c1e96SIngo Weinhold 				return B_BUSY;
482dac21d8bSIngo Weinhold 		}
483bb163c02SIngo Weinhold 	}
484bb163c02SIngo Weinhold 
485bb163c02SIngo Weinhold 	set_mtrrs();
486bb163c02SIngo Weinhold 
487bb163c02SIngo Weinhold 	return B_OK;
488bb163c02SIngo Weinhold }
489bb163c02SIngo Weinhold 
490bb163c02SIngo Weinhold 
491fa0c1e96SIngo Weinhold status_t
492fa0c1e96SIngo Weinhold update_mtrrs()
493fa0c1e96SIngo Weinhold {
494fa0c1e96SIngo Weinhold 	// Until we know how many MTRRs we have, pretend everything is OK.
495fa0c1e96SIngo Weinhold 	if (sMemoryTypeRegisterCount == 0)
496fa0c1e96SIngo Weinhold 		return B_OK;
497fa0c1e96SIngo Weinhold 
498fa0c1e96SIngo Weinhold 	update_mtrr_info updateInfo;
499fa0c1e96SIngo Weinhold 	updateInfo.ignoreUncacheableSize = 0;
500fa0c1e96SIngo Weinhold 
501fa0c1e96SIngo Weinhold 	while (true) {
502fa0c1e96SIngo Weinhold 		TRACE_MTRR2("update_mtrrs(): Trying with ignoreUncacheableSize %#"
503fa0c1e96SIngo Weinhold 			B_PRIx64 ".\n", updateInfo.ignoreUncacheableSize);
504fa0c1e96SIngo Weinhold 
505fa0c1e96SIngo Weinhold 		updateInfo.shortestUncacheableSize = ~(uint64)0;
506fa0c1e96SIngo Weinhold 		status_t error = update_mtrrs(updateInfo);
507fa0c1e96SIngo Weinhold 		if (error != B_BUSY) {
508fa0c1e96SIngo Weinhold 			if (error == B_OK && updateInfo.ignoreUncacheableSize > 0) {
509fa0c1e96SIngo Weinhold 				TRACE_MTRR("update_mtrrs(): Succeeded setting MTRRs after "
510fa0c1e96SIngo Weinhold 					"ignoring uncacheable ranges up to size %#" B_PRIx64 ".\n",
511fa0c1e96SIngo Weinhold 					updateInfo.ignoreUncacheableSize);
512fa0c1e96SIngo Weinhold 			}
513fa0c1e96SIngo Weinhold 			return error;
514fa0c1e96SIngo Weinhold 		}
515fa0c1e96SIngo Weinhold 
516fa0c1e96SIngo Weinhold 		// Not enough MTRRs. Retry with less uncacheable ranges.
517fa0c1e96SIngo Weinhold 		if (updateInfo.shortestUncacheableSize == ~(uint64)0) {
518fa0c1e96SIngo Weinhold 			// Ugh, even without any uncacheable ranges the available MTRRs do
519fa0c1e96SIngo Weinhold 			// not suffice.
520fa0c1e96SIngo Weinhold 			panic("update_mtrrs(): Out of MTRRs!");
521fa0c1e96SIngo Weinhold 			return B_BUSY;
522fa0c1e96SIngo Weinhold 		}
523fa0c1e96SIngo Weinhold 
524fa0c1e96SIngo Weinhold 		ASSERT(updateInfo.ignoreUncacheableSize
525fa0c1e96SIngo Weinhold 			< updateInfo.shortestUncacheableSize);
526fa0c1e96SIngo Weinhold 
527fa0c1e96SIngo Weinhold 		updateInfo.ignoreUncacheableSize = updateInfo.shortestUncacheableSize;
528fa0c1e96SIngo Weinhold 	}
529fa0c1e96SIngo Weinhold }
530fa0c1e96SIngo Weinhold 
531fa0c1e96SIngo Weinhold 
532bb163c02SIngo Weinhold static status_t
533bb163c02SIngo Weinhold add_memory_type_range(area_id areaID, uint64 base, uint64 size, uint32 type)
534bb163c02SIngo Weinhold {
535bb163c02SIngo Weinhold 	// translate the type
536393fceb5SAxel Dörfler 	if (type == 0)
537393fceb5SAxel Dörfler 		return B_OK;
538393fceb5SAxel Dörfler 
539393fceb5SAxel Dörfler 	switch (type) {
540393fceb5SAxel Dörfler 		case B_MTR_UC:
5414f893e39SJérôme Duval 			type = IA32_MTR_UNCACHED;
542393fceb5SAxel Dörfler 			break;
543393fceb5SAxel Dörfler 		case B_MTR_WC:
5444f893e39SJérôme Duval 			type = IA32_MTR_WRITE_COMBINING;
545393fceb5SAxel Dörfler 			break;
546393fceb5SAxel Dörfler 		case B_MTR_WT:
5474f893e39SJérôme Duval 			type = IA32_MTR_WRITE_THROUGH;
548393fceb5SAxel Dörfler 			break;
549393fceb5SAxel Dörfler 		case B_MTR_WP:
5504f893e39SJérôme Duval 			type = IA32_MTR_WRITE_PROTECTED;
551393fceb5SAxel Dörfler 			break;
552393fceb5SAxel Dörfler 		case B_MTR_WB:
5534f893e39SJérôme Duval 			type = IA32_MTR_WRITE_BACK;
554393fceb5SAxel Dörfler 			break;
555393fceb5SAxel Dörfler 		default:
556393fceb5SAxel Dörfler 			return B_BAD_VALUE;
557393fceb5SAxel Dörfler 	}
558393fceb5SAxel Dörfler 
559dac21d8bSIngo Weinhold 	TRACE_MTRR("add_memory_type_range(%" B_PRId32 ", %#" B_PRIx64 ", %#"
560dac21d8bSIngo Weinhold 		B_PRIx64 ", %" B_PRIu32 ")\n", areaID, base, size, type);
561393fceb5SAxel Dörfler 
562bb163c02SIngo Weinhold 	MutexLocker locker(sMemoryTypeLock);
563bb163c02SIngo Weinhold 
564dac21d8bSIngo Weinhold 	memory_type_range* range = areaID >= 0 ? find_range(areaID) : NULL;
565dac21d8bSIngo Weinhold 	int32 oldRangeType = -1;
566dac21d8bSIngo Weinhold 	if (range != NULL) {
567dac21d8bSIngo Weinhold 		if (range->base != base || range->size != size)
568393fceb5SAxel Dörfler 			return B_BAD_VALUE;
569dac21d8bSIngo Weinhold 		if (range->type == type)
570393fceb5SAxel Dörfler 			return B_OK;
571dac21d8bSIngo Weinhold 
572dac21d8bSIngo Weinhold 		oldRangeType = range->type;
573dac21d8bSIngo Weinhold 		range->type = type;
574dac21d8bSIngo Weinhold 	} else {
575dac21d8bSIngo Weinhold 		range = new(std::nothrow) memory_type_range;
576dac21d8bSIngo Weinhold 		if (range == NULL)
577dac21d8bSIngo Weinhold 			return B_NO_MEMORY;
578dac21d8bSIngo Weinhold 
579dac21d8bSIngo Weinhold 		range->area = areaID;
580dac21d8bSIngo Weinhold 		range->base = base;
581dac21d8bSIngo Weinhold 		range->size = size;
582dac21d8bSIngo Weinhold 		range->type = type;
583dac21d8bSIngo Weinhold 		sMemoryTypeRanges.Add(range);
584dac21d8bSIngo Weinhold 		sMemoryTypeRangeCount++;
585393fceb5SAxel Dörfler 	}
586393fceb5SAxel Dörfler 
587dac21d8bSIngo Weinhold 	status_t error = update_mtrrs();
588dac21d8bSIngo Weinhold 	if (error != B_OK) {
589dac21d8bSIngo Weinhold 		// revert the addition of the range/change of its type
590dac21d8bSIngo Weinhold 		if (oldRangeType < 0) {
591dac21d8bSIngo Weinhold 			sMemoryTypeRanges.Remove(range);
592dac21d8bSIngo Weinhold 			sMemoryTypeRangeCount--;
593dac21d8bSIngo Weinhold 			delete range;
594dac21d8bSIngo Weinhold 		} else
595dac21d8bSIngo Weinhold 			range->type = oldRangeType;
596393fceb5SAxel Dörfler 
597dac21d8bSIngo Weinhold 		update_mtrrs();
598bb163c02SIngo Weinhold 		return error;
599bb163c02SIngo Weinhold 	}
6004f893e39SJérôme Duval 
601dac21d8bSIngo Weinhold 	return B_OK;
602dac21d8bSIngo Weinhold }
603dac21d8bSIngo Weinhold 
6044f893e39SJérôme Duval 
6054f893e39SJérôme Duval static void
606bb163c02SIngo Weinhold remove_memory_type_range(area_id areaID)
6074f893e39SJérôme Duval {
608bb163c02SIngo Weinhold 	MutexLocker locker(sMemoryTypeLock);
609bb163c02SIngo Weinhold 
610dac21d8bSIngo Weinhold 	memory_type_range* range = find_range(areaID);
611dac21d8bSIngo Weinhold 	if (range != NULL) {
612dac21d8bSIngo Weinhold 		TRACE_MTRR("remove_memory_type_range(%" B_PRId32 ", %#" B_PRIx64 ", %#"
613dac21d8bSIngo Weinhold 			B_PRIx64 ", %" B_PRIu32 ")\n", range->area, range->base,
614dac21d8bSIngo Weinhold 			range->size, range->type);
615dac21d8bSIngo Weinhold 
616dac21d8bSIngo Weinhold 		sMemoryTypeRanges.Remove(range);
617dac21d8bSIngo Weinhold 		sMemoryTypeRangeCount--;
618dac21d8bSIngo Weinhold 		delete range;
619dac21d8bSIngo Weinhold 
620dac21d8bSIngo Weinhold 		update_mtrrs();
621dac21d8bSIngo Weinhold 	} else {
622dac21d8bSIngo Weinhold 		dprintf("remove_memory_type_range(): no range known for area %" B_PRId32
623dac21d8bSIngo Weinhold 			"\n", areaID);
6244f893e39SJérôme Duval 	}
6254f893e39SJérôme Duval }
6264f893e39SJérôme Duval 
6274f893e39SJérôme Duval 
628393fceb5SAxel Dörfler //	#pragma mark -
629393fceb5SAxel Dörfler 
630393fceb5SAxel Dörfler 
631393fceb5SAxel Dörfler status_t
632393fceb5SAxel Dörfler arch_vm_init(kernel_args *args)
633393fceb5SAxel Dörfler {
634393fceb5SAxel Dörfler 	TRACE(("arch_vm_init: entry\n"));
635393fceb5SAxel Dörfler 	return 0;
636393fceb5SAxel Dörfler }
637393fceb5SAxel Dörfler 
638393fceb5SAxel Dörfler 
639393fceb5SAxel Dörfler /*!	Marks DMA region as in-use, and maps it into the kernel space */
640393fceb5SAxel Dörfler status_t
641393fceb5SAxel Dörfler arch_vm_init_post_area(kernel_args *args)
642393fceb5SAxel Dörfler {
643393fceb5SAxel Dörfler 	area_id id;
644393fceb5SAxel Dörfler 
645393fceb5SAxel Dörfler 	TRACE(("arch_vm_init_post_area: entry\n"));
646393fceb5SAxel Dörfler 
647393fceb5SAxel Dörfler 	// account for DMA area and mark the pages unusable
648393fceb5SAxel Dörfler 	vm_mark_page_range_inuse(0x0, 0xa0000 / B_PAGE_SIZE);
649393fceb5SAxel Dörfler 
650393fceb5SAxel Dörfler 	// map 0 - 0xa0000 directly
65164d79effSIngo Weinhold 	id = map_physical_memory("dma_region", 0x0, 0xa0000,
652dac21d8bSIngo Weinhold 		B_ANY_KERNEL_ADDRESS | B_MTR_WB,
653dac21d8bSIngo Weinhold 		B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA, &gDmaAddress);
654393fceb5SAxel Dörfler 	if (id < 0) {
655393fceb5SAxel Dörfler 		panic("arch_vm_init_post_area: unable to map dma region\n");
656393fceb5SAxel Dörfler 		return B_NO_MEMORY;
657393fceb5SAxel Dörfler 	}
658393fceb5SAxel Dörfler 
659*4988ca58SAlex Smith 	// TODO x86_64
660*4988ca58SAlex Smith #ifndef __x86_64__
661393fceb5SAxel Dörfler 	return bios_init();
662*4988ca58SAlex Smith #else
663*4988ca58SAlex Smith 	return B_OK;
664*4988ca58SAlex Smith #endif
665393fceb5SAxel Dörfler }
666393fceb5SAxel Dörfler 
667393fceb5SAxel Dörfler 
668393fceb5SAxel Dörfler /*!	Gets rid of all yet unmapped (and therefore now unused) page tables */
669393fceb5SAxel Dörfler status_t
670393fceb5SAxel Dörfler arch_vm_init_end(kernel_args *args)
671393fceb5SAxel Dörfler {
672393fceb5SAxel Dörfler 	TRACE(("arch_vm_init_endvm: entry\n"));
673393fceb5SAxel Dörfler 
674393fceb5SAxel Dörfler 	// throw away anything in the kernel_args.pgtable[] that's not yet mapped
675393fceb5SAxel Dörfler 	vm_free_unused_boot_loader_range(KERNEL_BASE,
676d40a9355SIngo Weinhold 		args->arch_args.virtual_end - KERNEL_BASE);
677393fceb5SAxel Dörfler 
678393fceb5SAxel Dörfler 	return B_OK;
679393fceb5SAxel Dörfler }
680393fceb5SAxel Dörfler 
681393fceb5SAxel Dörfler 
682393fceb5SAxel Dörfler status_t
683393fceb5SAxel Dörfler arch_vm_init_post_modules(kernel_args *args)
684393fceb5SAxel Dörfler {
685393fceb5SAxel Dörfler 	// the x86 CPU modules are now accessible
686393fceb5SAxel Dörfler 
687393fceb5SAxel Dörfler 	sMemoryTypeRegisterCount = x86_count_mtrrs();
688393fceb5SAxel Dörfler 	if (sMemoryTypeRegisterCount == 0)
689393fceb5SAxel Dörfler 		return B_OK;
690393fceb5SAxel Dörfler 
691393fceb5SAxel Dörfler 	// not very likely, but play safe here
692393fceb5SAxel Dörfler 	if (sMemoryTypeRegisterCount > kMaxMemoryTypeRegisters)
693393fceb5SAxel Dörfler 		sMemoryTypeRegisterCount = kMaxMemoryTypeRegisters;
694393fceb5SAxel Dörfler 
695393fceb5SAxel Dörfler 	// set the physical memory ranges to write-back mode
696393fceb5SAxel Dörfler 	for (uint32 i = 0; i < args->num_physical_memory_ranges; i++) {
697bb163c02SIngo Weinhold 		add_memory_type_range(-1, args->physical_memory_range[i].start,
698bb163c02SIngo Weinhold 			args->physical_memory_range[i].size, B_MTR_WB);
699393fceb5SAxel Dörfler 	}
700393fceb5SAxel Dörfler 
701393fceb5SAxel Dörfler 	return B_OK;
702393fceb5SAxel Dörfler }
703393fceb5SAxel Dörfler 
704393fceb5SAxel Dörfler 
705393fceb5SAxel Dörfler void
706b0db552cSIngo Weinhold arch_vm_aspace_swap(struct VMAddressSpace *from, struct VMAddressSpace *to)
707393fceb5SAxel Dörfler {
7089a42ad7aSIngo Weinhold 	// This functions is only invoked when a userland thread is in the process
7099a42ad7aSIngo Weinhold 	// of dying. It switches to the kernel team and does whatever cleanup is
7109a42ad7aSIngo Weinhold 	// necessary (in case it is the team's main thread, it will delete the
7119a42ad7aSIngo Weinhold 	// team).
7129a42ad7aSIngo Weinhold 	// It is however not necessary to change the page directory. Userland team's
7139a42ad7aSIngo Weinhold 	// page directories include all kernel mappings as well. Furthermore our
7149a42ad7aSIngo Weinhold 	// arch specific translation map data objects are ref-counted, so they won't
7159a42ad7aSIngo Weinhold 	// go away as long as they are still used on any CPU.
716393fceb5SAxel Dörfler }
717393fceb5SAxel Dörfler 
718393fceb5SAxel Dörfler 
719393fceb5SAxel Dörfler bool
720393fceb5SAxel Dörfler arch_vm_supports_protection(uint32 protection)
721393fceb5SAxel Dörfler {
722393fceb5SAxel Dörfler 	// x86 always has the same read/write properties for userland and the
723393fceb5SAxel Dörfler 	// kernel.
724393fceb5SAxel Dörfler 	// That's why we do not support user-read/kernel-write access. While the
725393fceb5SAxel Dörfler 	// other way around is not supported either, we don't care in this case
726393fceb5SAxel Dörfler 	// and give the kernel full access.
727393fceb5SAxel Dörfler 	if ((protection & (B_READ_AREA | B_WRITE_AREA)) == B_READ_AREA
72840f1dd84SIngo Weinhold 		&& (protection & B_KERNEL_WRITE_AREA) != 0) {
729393fceb5SAxel Dörfler 		return false;
73040f1dd84SIngo Weinhold 	}
731393fceb5SAxel Dörfler 
732393fceb5SAxel Dörfler 	return true;
733393fceb5SAxel Dörfler }
734393fceb5SAxel Dörfler 
735393fceb5SAxel Dörfler 
736393fceb5SAxel Dörfler void
737a99eb6b5SIngo Weinhold arch_vm_unset_memory_type(struct VMArea *area)
738393fceb5SAxel Dörfler {
7393b0c1b52SIngo Weinhold 	if (area->MemoryType() == 0)
740393fceb5SAxel Dörfler 		return;
741393fceb5SAxel Dörfler 
742bb163c02SIngo Weinhold 	remove_memory_type_range(area->id);
743393fceb5SAxel Dörfler }
744393fceb5SAxel Dörfler 
745393fceb5SAxel Dörfler 
746393fceb5SAxel Dörfler status_t
747147133b7SIngo Weinhold arch_vm_set_memory_type(struct VMArea *area, phys_addr_t physicalBase,
748147133b7SIngo Weinhold 	uint32 type)
749393fceb5SAxel Dörfler {
750bbd97b4bSIngo Weinhold 	return add_memory_type_range(area->id, physicalBase, area->Size(), type);
751393fceb5SAxel Dörfler }
752