xref: /illumos-gate/usr/src/uts/i86pc/vm/i86_mmu.c (revision cdf9f8c9206117516a14f3c70f86510326c3d5fa)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/t_lock.h>
29 #include <sys/memlist.h>
30 #include <sys/cpuvar.h>
31 #include <sys/vmem.h>
32 #include <sys/mman.h>
33 #include <sys/vm.h>
34 #include <sys/kmem.h>
35 #include <sys/cmn_err.h>
36 #include <sys/debug.h>
37 #include <sys/vm_machparam.h>
38 #include <sys/tss.h>
39 #include <sys/vnode.h>
40 #include <vm/hat.h>
41 #include <vm/anon.h>
42 #include <vm/as.h>
43 #include <vm/page.h>
44 #include <vm/seg.h>
45 #include <vm/seg_kmem.h>
46 #include <vm/seg_map.h>
47 #include <vm/hat_i86.h>
48 #include <sys/promif.h>
49 #include <sys/x86_archext.h>
50 #include <sys/systm.h>
51 #include <sys/archsystm.h>
52 #include <sys/sunddi.h>
53 #include <sys/ddidmareq.h>
54 #include <sys/controlregs.h>
55 #include <sys/reboot.h>
56 #include <sys/kdi.h>
57 #include <sys/bootconf.h>
58 #include <sys/bootsvcs.h>
59 #include <sys/bootinfo.h>
60 #include <vm/kboot_mmu.h>
61 
62 #ifdef __xpv
63 #include <sys/hypervisor.h>
64 #endif
65 
66 caddr_t
67 i86devmap(pfn_t pf, pgcnt_t pgcnt, uint_t prot)
68 {
69 	caddr_t addr;
70 	caddr_t addr1;
71 	page_t *pp;
72 
73 	addr1 = addr = vmem_alloc(heap_arena, mmu_ptob(pgcnt), VM_SLEEP);
74 
75 	for (; pgcnt != 0; addr += MMU_PAGESIZE, ++pf, --pgcnt) {
76 		pp = page_numtopp_nolock(pf);
77 		if (pp == NULL) {
78 			hat_devload(kas.a_hat, addr, MMU_PAGESIZE, pf,
79 			    prot | HAT_NOSYNC, HAT_LOAD_LOCK);
80 		} else {
81 			hat_memload(kas.a_hat, addr, pp,
82 			    prot | HAT_NOSYNC, HAT_LOAD_LOCK);
83 		}
84 	}
85 
86 	return (addr1);
87 }
88 
89 /*
90  * This routine is like page_numtopp, but accepts only free pages, which
91  * it allocates (unfrees) and returns with the exclusive lock held.
92  * It is used by machdep.c/dma_init() to find contiguous free pages.
93  *
94  * XXX this and some others should probably be in vm_machdep.c
95  */
96 page_t *
97 page_numtopp_alloc(pfn_t pfnum)
98 {
99 	page_t *pp;
100 
101 retry:
102 	pp = page_numtopp_nolock(pfnum);
103 	if (pp == NULL) {
104 		return (NULL);
105 	}
106 
107 	if (!page_trylock(pp, SE_EXCL)) {
108 		return (NULL);
109 	}
110 
111 	if (page_pptonum(pp) != pfnum) {
112 		page_unlock(pp);
113 		goto retry;
114 	}
115 
116 	if (!PP_ISFREE(pp)) {
117 		page_unlock(pp);
118 		return (NULL);
119 	}
120 	if (pp->p_szc) {
121 		page_demote_free_pages(pp);
122 		page_unlock(pp);
123 		goto retry;
124 	}
125 
126 	/* If associated with a vnode, destroy mappings */
127 
128 	if (pp->p_vnode) {
129 
130 		page_destroy_free(pp);
131 
132 		if (!page_lock(pp, SE_EXCL, (kmutex_t *)NULL, P_NO_RECLAIM)) {
133 			return (NULL);
134 		}
135 
136 		if (page_pptonum(pp) != pfnum) {
137 			page_unlock(pp);
138 			goto retry;
139 		}
140 	}
141 
142 	if (!PP_ISFREE(pp)) {
143 		page_unlock(pp);
144 		return (NULL);
145 	}
146 
147 	if (!page_reclaim(pp, (kmutex_t *)NULL))
148 		return (NULL);
149 
150 	return (pp);
151 }
152 
153 /*
154  * Flag is not set early in boot. Once it is set we are no longer
155  * using boot's page tables.
156  */
157 uint_t khat_running = 0;
158 
159 /*
160  * This procedure is callable only while the boot loader is in charge of the
161  * MMU. It assumes that PA == VA for page table pointers.  It doesn't live in
162  * kboot_mmu.c since it's used from common code.
163  */
164 pfn_t
165 va_to_pfn(void *vaddr)
166 {
167 	uintptr_t	des_va = ALIGN2PAGE(vaddr);
168 	uintptr_t	va = des_va;
169 	size_t		len;
170 	uint_t		prot;
171 	pfn_t		pfn;
172 
173 	if (khat_running)
174 		panic("va_to_pfn(): called too late\n");
175 
176 	if (kbm_probe(&va, &len, &pfn, &prot) == 0)
177 		return (PFN_INVALID);
178 	if (va > des_va)
179 		return (PFN_INVALID);
180 	if (va < des_va)
181 		pfn += mmu_btop(des_va - va);
182 	return (pfn);
183 }
184 
185 /*
186  * Initialize a special area in the kernel that always holds some PTEs for
187  * faster performance. This always holds segmap's PTEs.
188  * In the 32 bit kernel this maps the kernel heap too.
189  */
190 void
191 hat_kmap_init(uintptr_t base, size_t len)
192 {
193 	uintptr_t map_addr;	/* base rounded down to large page size */
194 	uintptr_t map_eaddr;	/* base + len rounded up */
195 	size_t map_len;
196 	caddr_t ptes;		/* mapping area in kernel for kmap ptes */
197 	size_t window_size;	/* size of mapping area for ptes */
198 	ulong_t htable_cnt;	/* # of page tables to cover map_len */
199 	ulong_t i;
200 	htable_t *ht;
201 	uintptr_t va;
202 
203 	/*
204 	 * We have to map in an area that matches an entire page table.
205 	 * The PTEs are large page aligned to avoid spurious pagefaults
206 	 * on the hypervisor.
207 	 */
208 	map_addr = base & LEVEL_MASK(1);
209 	map_eaddr = (base + len + LEVEL_SIZE(1) - 1) & LEVEL_MASK(1);
210 	map_len = map_eaddr - map_addr;
211 	window_size = mmu_btop(map_len) * mmu.pte_size;
212 	window_size = (window_size + LEVEL_SIZE(1)) & LEVEL_MASK(1);
213 	htable_cnt = map_len >> LEVEL_SHIFT(1);
214 
215 	/*
216 	 * allocate vmem for the kmap_ptes
217 	 */
218 	ptes = vmem_xalloc(heap_arena, window_size, LEVEL_SIZE(1), 0,
219 	    0, NULL, NULL, VM_SLEEP);
220 	mmu.kmap_htables =
221 	    kmem_alloc(htable_cnt * sizeof (htable_t *), KM_SLEEP);
222 
223 	/*
224 	 * Map the page tables that cover kmap into the allocated range.
225 	 * Note we don't ever htable_release() the kmap page tables - they
226 	 * can't ever be stolen, freed, etc.
227 	 */
228 	for (va = map_addr, i = 0; i < htable_cnt; va += LEVEL_SIZE(1), ++i) {
229 		ht = htable_create(kas.a_hat, va, 0, NULL);
230 		if (ht == NULL)
231 			panic("hat_kmap_init: ht == NULL");
232 		mmu.kmap_htables[i] = ht;
233 
234 		hat_devload(kas.a_hat, ptes + i * MMU_PAGESIZE,
235 		    MMU_PAGESIZE, ht->ht_pfn,
236 #ifdef __xpv
237 		    PROT_READ | HAT_NOSYNC | HAT_UNORDERED_OK,
238 #else
239 		    PROT_READ | PROT_WRITE | HAT_NOSYNC | HAT_UNORDERED_OK,
240 #endif
241 		    HAT_LOAD | HAT_LOAD_NOCONSIST);
242 	}
243 
244 	/*
245 	 * set information in mmu to activate handling of kmap
246 	 */
247 	mmu.kmap_addr = map_addr;
248 	mmu.kmap_eaddr = map_eaddr;
249 	mmu.kmap_ptes = (x86pte_t *)ptes;
250 }
251 
252 extern caddr_t	kpm_vbase;
253 extern size_t	kpm_size;
254 
255 #ifdef __xpv
256 /*
257  * Create the initial segkpm mappings for the hypervisor. To avoid having
258  * to deal with page tables being read only, we make all mappings
259  * read only at first.
260  */
261 static void
262 xen_kpm_create(paddr_t paddr, level_t lvl)
263 {
264 	ulong_t pg_off;
265 
266 	for (pg_off = 0; pg_off < LEVEL_SIZE(lvl); pg_off += MMU_PAGESIZE) {
267 		kbm_map((uintptr_t)kpm_vbase + paddr, (paddr_t)0, 0, 1);
268 		kbm_read_only((uintptr_t)kpm_vbase + paddr + pg_off,
269 		    paddr + pg_off);
270 	}
271 }
272 
273 /*
274  * Try to make all kpm mappings writable. Failures are ok, as those
275  * are just pagetable, GDT, etc. pages.
276  */
277 static void
278 xen_kpm_finish_init(void)
279 {
280 	pfn_t gdtpfn = mmu_btop(CPU->cpu_m.mcpu_gdtpa);
281 	pfn_t pfn;
282 	page_t *pp;
283 
284 	for (pfn = 0; pfn < mfn_count; ++pfn) {
285 		/*
286 		 * skip gdt
287 		 */
288 		if (pfn == gdtpfn)
289 			continue;
290 
291 		/*
292 		 * p_index is a hint that this is a pagetable
293 		 */
294 		pp = page_numtopp_nolock(pfn);
295 		if (pp && pp->p_index) {
296 			pp->p_index = 0;
297 			continue;
298 		}
299 		(void) xen_kpm_page(pfn, PT_VALID | PT_WRITABLE);
300 	}
301 }
302 #endif
303 
304 /*
305  * Routine to pre-allocate data structures for hat_kern_setup(). It computes
306  * how many pagetables it needs by walking the boot loader's page tables.
307  */
308 /*ARGSUSED*/
309 void
310 hat_kern_alloc(
311 	caddr_t	segmap_base,
312 	size_t	segmap_size,
313 	caddr_t	ekernelheap)
314 {
315 	uintptr_t	last_va = (uintptr_t)-1;	/* catch 1st time */
316 	uintptr_t	va = 0;
317 	size_t		size;
318 	pfn_t		pfn;
319 	uint_t		prot;
320 	uint_t		table_cnt = 1;
321 	uint_t		mapping_cnt;
322 	level_t		start_level;
323 	level_t		l;
324 	struct memlist	*pmem;
325 	level_t		lpagel = mmu.max_page_level;
326 	uint64_t	paddr;
327 	int64_t		psize;
328 	int		nwindows;
329 
330 	if (kpm_size > 0) {
331 		/*
332 		 * Create the kpm page tables.  When running on the
333 		 * hypervisor these are made read/only at first.
334 		 * Later we'll add write permission where possible.
335 		 */
336 		for (pmem = phys_install; pmem; pmem = pmem->next) {
337 			paddr = pmem->address;
338 			psize = pmem->size;
339 			while (psize >= MMU_PAGESIZE) {
340 				if ((paddr & LEVEL_OFFSET(lpagel)) == 0 &&
341 				    psize > LEVEL_SIZE(lpagel))
342 					l = lpagel;
343 				else
344 					l = 0;
345 #if defined(__xpv)
346 				/*
347 				 * Create read/only mappings to avoid
348 				 * conflicting with pagetable usage
349 				 */
350 				xen_kpm_create(paddr, l);
351 #else
352 				kbm_map((uintptr_t)kpm_vbase + paddr, paddr,
353 				    l, 1);
354 #endif
355 				paddr += LEVEL_SIZE(l);
356 				psize -= LEVEL_SIZE(l);
357 			}
358 		}
359 	}
360 
361 	/*
362 	 * If this machine doesn't have a kpm segment, we need to allocate
363 	 * a small number of 'windows' which can be used to map pagetables.
364 	 */
365 	nwindows = (kpm_size == 0) ? 2 * NCPU : 0;
366 
367 #if defined(__xpv)
368 	/*
369 	 * On a hypervisor, these windows are also used by the xpv_panic
370 	 * code, where we need one window for each level of the pagetable
371 	 * hierarchy.
372 	 */
373 	nwindows = MAX(nwindows, mmu.max_level);
374 #endif
375 
376 	if (nwindows != 0) {
377 		/*
378 		 * Create the page windows and 1 page of VA in
379 		 * which we map the PTEs of those windows.
380 		 */
381 		mmu.pwin_base = vmem_xalloc(heap_arena, nwindows * MMU_PAGESIZE,
382 		    LEVEL_SIZE(1), 0, 0, NULL, NULL, VM_SLEEP);
383 		ASSERT(nwindows <= MMU_PAGESIZE / mmu.pte_size);
384 		mmu.pwin_pte_va = vmem_xalloc(heap_arena, MMU_PAGESIZE,
385 		    MMU_PAGESIZE, 0, 0, NULL, NULL, VM_SLEEP);
386 
387 		/*
388 		 * Find/Create the page table window mappings.
389 		 */
390 		paddr = 0;
391 		(void) find_pte((uintptr_t)mmu.pwin_base, &paddr, 0, 0);
392 		ASSERT(paddr != 0);
393 		ASSERT((paddr & MMU_PAGEOFFSET) == 0);
394 		mmu.pwin_pte_pa = paddr;
395 #ifdef __xpv
396 		(void) find_pte((uintptr_t)mmu.pwin_pte_va, NULL, 0, 0);
397 		kbm_read_only((uintptr_t)mmu.pwin_pte_va, mmu.pwin_pte_pa);
398 #else
399 		kbm_map((uintptr_t)mmu.pwin_pte_va, mmu.pwin_pte_pa, 0, 1);
400 #endif
401 	}
402 
403 	/*
404 	 * Walk the boot loader's page tables and figure out
405 	 * how many tables and page mappings there will be.
406 	 */
407 	while (kbm_probe(&va, &size, &pfn, &prot) != 0) {
408 		/*
409 		 * At each level, if the last_va falls into a new htable,
410 		 * increment table_cnt. We can stop at the 1st level where
411 		 * they are in the same htable.
412 		 */
413 		if (size == MMU_PAGESIZE)
414 			start_level = 0;
415 		else
416 			start_level = 1;
417 
418 		for (l = start_level; l < mmu.max_level; ++l) {
419 			if (va >> LEVEL_SHIFT(l + 1) ==
420 			    last_va >> LEVEL_SHIFT(l + 1))
421 				break;
422 			++table_cnt;
423 		}
424 		last_va = va;
425 		va = (va & LEVEL_MASK(1)) + LEVEL_SIZE(1);
426 	}
427 
428 	/*
429 	 * Besides the boot loader mappings, we're going to fill in
430 	 * the entire top level page table for the kernel. Make sure there's
431 	 * enough reserve for that too.
432 	 */
433 	table_cnt += mmu.top_level_count - ((kernelbase >>
434 	    LEVEL_SHIFT(mmu.max_level)) & (mmu.top_level_count - 1));
435 
436 #if defined(__i386)
437 	/*
438 	 * The 32 bit PAE hat allocates tables one level below the top when
439 	 * kernelbase isn't 1 Gig aligned. We'll just be sloppy and allocate
440 	 * a bunch more to the reserve. Any unused will be returned later.
441 	 * Note we've already counted these mappings, just not the extra
442 	 * pagetables.
443 	 */
444 	if (mmu.pae_hat != 0 && (kernelbase & LEVEL_OFFSET(mmu.max_level)) != 0)
445 		table_cnt += mmu.ptes_per_table -
446 		    ((kernelbase & LEVEL_OFFSET(mmu.max_level)) >>
447 		    LEVEL_SHIFT(mmu.max_level - 1));
448 #endif
449 
450 	/*
451 	 * Add 1/4 more into table_cnt for extra slop.  The unused
452 	 * slop is freed back when we htable_adjust_reserve() later.
453 	 */
454 	table_cnt += table_cnt >> 2;
455 
456 	/*
457 	 * We only need mapping entries (hments) for shared pages.
458 	 * This should be far, far fewer than the total possible,
459 	 * We'll allocate enough for 1/16 of all possible PTEs.
460 	 */
461 	mapping_cnt = (table_cnt * mmu.ptes_per_table) >> 4;
462 
463 	/*
464 	 * Now create the initial htable/hment reserves
465 	 */
466 	htable_initial_reserve(table_cnt);
467 	hment_reserve(mapping_cnt);
468 	x86pte_cpu_init(CPU);
469 }
470 
471 
472 /*
473  * This routine handles the work of creating the kernel's initial mappings
474  * by deciphering the mappings in the page tables created by the boot program.
475  *
476  * We maintain large page mappings, but only to a level 1 pagesize.
477  * The boot loader can only add new mappings once this function starts.
478  * In particular it can not change the pagesize used for any existing
479  * mappings or this code breaks!
480  */
481 
482 void
483 hat_kern_setup(void)
484 {
485 	/*
486 	 * Attach htables to the existing pagetables
487 	 */
488 	/* BEGIN CSTYLED */
489 	htable_attach(kas.a_hat, 0, mmu.max_level, NULL,
490 #ifdef __xpv
491 	    mmu_btop(xen_info->pt_base - ONE_GIG));
492 #else
493 	    mmu_btop(getcr3()));
494 #endif
495 	/* END CSTYLED */
496 
497 #if defined(__i386) && !defined(__xpv)
498 	CPU->cpu_tss->tss_cr3 = dftss0.tss_cr3 = getcr3();
499 #endif /* __i386 */
500 
501 #if defined(__xpv) && defined(__amd64)
502 	/*
503 	 * Try to make the kpm mappings r/w. Failures here are OK, as
504 	 * it's probably just a pagetable
505 	 */
506 	xen_kpm_finish_init();
507 #endif
508 
509 	/*
510 	 * The kernel HAT is now officially open for business.
511 	 */
512 	khat_running = 1;
513 
514 	CPUSET_ATOMIC_ADD(kas.a_hat->hat_cpus, CPU->cpu_id);
515 	CPU->cpu_current_hat = kas.a_hat;
516 }
517