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