xref: /titanic_51/usr/src/uts/i86pc/vm/htable.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/sysmacros.h>
31 #include <sys/kmem.h>
32 #include <sys/atomic.h>
33 #include <sys/bitmap.h>
34 #include <sys/machparam.h>
35 #include <sys/machsystm.h>
36 #include <sys/mman.h>
37 #include <sys/systm.h>
38 #include <sys/cpuvar.h>
39 #include <sys/thread.h>
40 #include <sys/proc.h>
41 #include <sys/cpu.h>
42 #include <sys/kmem.h>
43 #include <sys/disp.h>
44 #include <sys/vmem.h>
45 #include <sys/vmsystm.h>
46 #include <sys/promif.h>
47 #include <sys/var.h>
48 #include <sys/x86_archext.h>
49 #include <sys/bootconf.h>
50 #include <sys/dumphdr.h>
51 #include <vm/seg_kmem.h>
52 #include <vm/seg_kpm.h>
53 #include <vm/hat.h>
54 #include <vm/hat_i86.h>
55 #include <sys/cmn_err.h>
56 
57 kmem_cache_t *htable_cache;
58 extern cpuset_t khat_cpuset;
59 
60 /*
61  * The variable htable_reserve_amount, rather than HTABLE_RESERVE_AMOUNT,
62  * is used in order to facilitate testing of the htable_steal() code.
63  * By resetting htable_reserve_amount to a lower value, we can force
64  * stealing to occur.  The reserve amount is a guess to get us through boot.
65  */
66 #define	HTABLE_RESERVE_AMOUNT	(200)
67 uint_t htable_reserve_amount = HTABLE_RESERVE_AMOUNT;
68 kmutex_t htable_reserve_mutex;
69 uint_t htable_reserve_cnt;
70 htable_t *htable_reserve_pool;
71 
72 /*
73  * This variable is so that we can tune this via /etc/system
74  */
75 uint_t htable_steal_passes = 10;
76 
77 /*
78  * mutex stuff for access to htable hash
79  */
80 #define	NUM_HTABLE_MUTEX 128
81 kmutex_t htable_mutex[NUM_HTABLE_MUTEX];
82 #define	HTABLE_MUTEX_HASH(h) ((h) & (NUM_HTABLE_MUTEX - 1))
83 
84 #define	HTABLE_ENTER(h)	mutex_enter(&htable_mutex[HTABLE_MUTEX_HASH(h)]);
85 #define	HTABLE_EXIT(h)	mutex_exit(&htable_mutex[HTABLE_MUTEX_HASH(h)]);
86 
87 /*
88  * forward declarations
89  */
90 static void link_ptp(htable_t *higher, htable_t *new, uintptr_t vaddr);
91 static void unlink_ptp(htable_t *higher, htable_t *old, uintptr_t vaddr);
92 static void htable_free(htable_t *ht);
93 static x86pte_t *x86pte_access_pagetable(htable_t *ht);
94 static void x86pte_release_pagetable(htable_t *ht);
95 static x86pte_t x86pte_cas(htable_t *ht, uint_t entry, x86pte_t old,
96 	x86pte_t new);
97 
98 /*
99  * Address used for kernel page tables. See ptable_alloc() below.
100  */
101 uintptr_t ptable_va = 0;
102 size_t	ptable_sz = 2 * MMU_PAGESIZE;
103 
104 /*
105  * A counter to track if we are stealing or reaping htables. When non-zero
106  * htable_free() will directly free htables (either to the reserve or kmem)
107  * instead of putting them in a hat's htable cache.
108  */
109 uint32_t htable_dont_cache = 0;
110 
111 /*
112  * Track the number of active pagetables, so we can know how many to reap
113  */
114 static uint32_t active_ptables = 0;
115 
116 /*
117  * Allocate a memory page for a hardware page table.
118  *
119  * The pages allocated for page tables are currently gotten in a hacked up
120  * way. It works for now, but really needs to be fixed up a bit.
121  *
122  * During boot: The boot loader controls physical memory allocation via
123  * boot_alloc(). To avoid conflict with vmem, we just do boot_alloc()s with
124  * addresses less than kernelbase. These addresses are ignored when we take
125  * over mappings from the boot loader.
126  *
127  * Post-boot: we currently use page_create_va() on the kvp with fake offsets,
128  * segments and virt address. This is pretty bogus, but was copied from the
129  * old hat_i86.c code. A better approach would be to have a custom
130  * page_get_physical() interface that can specify either mnode random or
131  * mnode local and takes a page from whatever color has the MOST available -
132  * this would have a minimal impact on page coloring.
133  *
134  * For now the htable pointer in ht is only used to compute a unique vnode
135  * offset for the page.
136  */
137 static void
138 ptable_alloc(htable_t *ht)
139 {
140 	pfn_t pfn;
141 	page_t *pp;
142 	u_offset_t offset;
143 	static struct seg tmpseg;
144 	static int first_time = 1;
145 
146 	/*
147 	 * Allocating the associated hardware page table is very different
148 	 * before boot has finished.  We get a physical page to from boot
149 	 * w/o eating up any kernel address space.
150 	 */
151 	ht->ht_pfn = PFN_INVALID;
152 	HATSTAT_INC(hs_ptable_allocs);
153 	atomic_add_32(&active_ptables, 1);
154 
155 	if (use_boot_reserve) {
156 		ASSERT(ptable_va != 0);
157 
158 		/*
159 		 * Allocate, then demap the ptable_va, so that we're
160 		 * sure there exist page table entries for the addresses
161 		 */
162 		if (first_time) {
163 			first_time = 0;
164 			if ((uintptr_t)BOP_ALLOC(bootops, (caddr_t)ptable_va,
165 			    ptable_sz, BO_NO_ALIGN) != ptable_va)
166 				panic("BOP_ALLOC failed");
167 
168 			hat_boot_demap(ptable_va);
169 			hat_boot_demap(ptable_va + MMU_PAGESIZE);
170 		}
171 
172 		pfn = ((uintptr_t)BOP_EALLOC(bootops, 0, MMU_PAGESIZE,
173 		    BO_NO_ALIGN, BOPF_X86_ALLOC_PHYS)) >> MMU_PAGESHIFT;
174 		if (page_resv(1, KM_NOSLEEP) == 0)
175 			panic("page_resv() failed in ptable alloc");
176 
177 		pp = page_numtopp_nolock(pfn);
178 		ASSERT(pp != NULL);
179 		if (pp->p_szc != 0)
180 			page_boot_demote(pp);
181 		pp = page_numtopp(pfn, SE_EXCL);
182 		ASSERT(pp != NULL);
183 
184 	} else {
185 		/*
186 		 * Post boot get a page for the table.
187 		 *
188 		 * The first check is to see if there is memory in
189 		 * the system. If we drop to throttlefree, then fail
190 		 * the ptable_alloc() and let the stealing code kick in.
191 		 * Note that we have to do this test here, since the test in
192 		 * page_create_throttle() would let the NOSLEEP allocation
193 		 * go through and deplete the page reserves.
194 		 */
195 		if (freemem <= throttlefree + 1)
196 			return;
197 
198 		/*
199 		 * This code is temporary, so don't review too critically.
200 		 * I'm awaiting a new phys page allocator from Kit -- Joe
201 		 *
202 		 * We need assign an offset for the page to call
203 		 * page_create_va. To avoid conflicts with other pages,
204 		 * we get creative with the offset.
205 		 * for 32 bits, we pic an offset > 4Gig
206 		 * for 64 bits, pic an offset somewhere in the VA hole.
207 		 */
208 		offset = (uintptr_t)ht - kernelbase;
209 		offset <<= MMU_PAGESHIFT;
210 #if defined(__amd64)
211 		offset += mmu.hole_start;	/* something in VA hole */
212 #else
213 		offset += 1ULL << 40;		/* something > 4 Gig */
214 #endif
215 
216 		if (page_resv(1, KM_NOSLEEP) == 0)
217 			return;
218 
219 #ifdef DEBUG
220 		pp = page_exists(&kvp, offset);
221 		if (pp != NULL)
222 			panic("ptable already exists %p", pp);
223 #endif
224 		pp = page_create_va(&kvp, offset, MMU_PAGESIZE,
225 		    PG_EXCL | PG_NORELOC, &tmpseg,
226 		    (void *)((uintptr_t)ht << MMU_PAGESHIFT));
227 		if (pp == NULL)
228 			return;
229 		page_io_unlock(pp);
230 		page_hashout(pp, NULL);
231 		pfn = pp->p_pagenum;
232 	}
233 	page_downgrade(pp);
234 	ASSERT(PAGE_SHARED(pp));
235 
236 	if (pfn == PFN_INVALID)
237 		panic("ptable_alloc(): Invalid PFN!!");
238 	ht->ht_pfn = pfn;
239 }
240 
241 /*
242  * Free an htable's associated page table page.  See the comments
243  * for ptable_alloc().
244  */
245 static void
246 ptable_free(htable_t *ht)
247 {
248 	pfn_t pfn = ht->ht_pfn;
249 	page_t *pp;
250 
251 	/*
252 	 * need to destroy the page used for the pagetable
253 	 */
254 	ASSERT(pfn != PFN_INVALID);
255 	HATSTAT_INC(hs_ptable_frees);
256 	atomic_add_32(&active_ptables, -1);
257 	pp = page_numtopp_nolock(pfn);
258 	if (pp == NULL)
259 		panic("ptable_free(): no page for pfn!");
260 	ASSERT(PAGE_SHARED(pp));
261 	ASSERT(pfn == pp->p_pagenum);
262 
263 	/*
264 	 * Get an exclusive lock, might have to wait for a kmem reader.
265 	 */
266 	if (!page_tryupgrade(pp)) {
267 		page_unlock(pp);
268 		/*
269 		 * RFE: we could change this to not loop forever
270 		 * George Cameron had some idea on how to do that.
271 		 * For now looping works - it's just like sfmmu.
272 		 */
273 		while (!page_lock(pp, SE_EXCL, (kmutex_t *)NULL, P_RECLAIM))
274 			continue;
275 	}
276 	page_free(pp, 1);
277 	page_unresv(1);
278 	ht->ht_pfn = PFN_INVALID;
279 }
280 
281 /*
282  * Put one htable on the reserve list.
283  */
284 static void
285 htable_put_reserve(htable_t *ht)
286 {
287 	ht->ht_hat = NULL;		/* no longer tied to a hat */
288 	ASSERT(ht->ht_pfn == PFN_INVALID);
289 	HATSTAT_INC(hs_htable_rputs);
290 	mutex_enter(&htable_reserve_mutex);
291 	ht->ht_next = htable_reserve_pool;
292 	htable_reserve_pool = ht;
293 	++htable_reserve_cnt;
294 	mutex_exit(&htable_reserve_mutex);
295 }
296 
297 /*
298  * Take one htable from the reserve.
299  */
300 static htable_t *
301 htable_get_reserve(void)
302 {
303 	htable_t *ht = NULL;
304 
305 	mutex_enter(&htable_reserve_mutex);
306 	if (htable_reserve_cnt != 0) {
307 		ht = htable_reserve_pool;
308 		ASSERT(ht != NULL);
309 		ASSERT(ht->ht_pfn == PFN_INVALID);
310 		htable_reserve_pool = ht->ht_next;
311 		--htable_reserve_cnt;
312 		HATSTAT_INC(hs_htable_rgets);
313 	}
314 	mutex_exit(&htable_reserve_mutex);
315 	return (ht);
316 }
317 
318 /*
319  * Allocate initial htables with page tables and put them on the kernel hat's
320  * cache list.
321  */
322 void
323 htable_initial_reserve(uint_t count)
324 {
325 	htable_t *ht;
326 	hat_t *hat = kas.a_hat;
327 
328 	count += HTABLE_RESERVE_AMOUNT;
329 	while (count > 0) {
330 		ht = kmem_cache_alloc(htable_cache, KM_NOSLEEP);
331 		ASSERT(ht != NULL);
332 
333 		ASSERT(use_boot_reserve);
334 		ht->ht_hat = kas.a_hat;	/* so htable_free() works */
335 		ht->ht_flags = 0;	/* so x86pte_zero works */
336 		ptable_alloc(ht);
337 		if (ht->ht_pfn == PFN_INVALID)
338 			panic("ptable_alloc() failed");
339 
340 		x86pte_zero(ht, 0, mmu.ptes_per_table);
341 
342 		ht->ht_next = hat->hat_ht_cached;
343 		hat->hat_ht_cached = ht;
344 		--count;
345 	}
346 }
347 
348 /*
349  * Readjust the reserves after a thread finishes using them.
350  *
351  * The first time this is called post boot, we'll also clear out the
352  * extra boot htables that were put in the kernel hat's cache list.
353  */
354 void
355 htable_adjust_reserve()
356 {
357 	static int first_time = 1;
358 	htable_t *ht;
359 
360 	ASSERT(curthread != hat_reserves_thread);
361 
362 	/*
363 	 * The first time this is called after we can steal, we free up the
364 	 * the kernel's cache htable list. It has lots of extra htable/page
365 	 * tables that were allocated for boot up.
366 	 */
367 	if (first_time) {
368 		first_time = 0;
369 		while ((ht = kas.a_hat->hat_ht_cached) != NULL) {
370 			kas.a_hat->hat_ht_cached = ht->ht_next;
371 			ASSERT(ht->ht_hat == kas.a_hat);
372 			ptable_free(ht);
373 			htable_put_reserve(ht);
374 		}
375 		return;
376 	}
377 
378 	/*
379 	 * Free any excess htables in the reserve list
380 	 */
381 	while (htable_reserve_cnt > htable_reserve_amount) {
382 		ht = htable_get_reserve();
383 		if (ht == NULL)
384 			return;
385 		ASSERT(ht->ht_pfn == PFN_INVALID);
386 		kmem_cache_free(htable_cache, ht);
387 	}
388 }
389 
390 
391 /*
392  * This routine steals htables from user processes for htable_alloc() or
393  * for htable_reap().
394  */
395 static htable_t *
396 htable_steal(uint_t cnt)
397 {
398 	hat_t		*hat = kas.a_hat;	/* list starts with khat */
399 	htable_t	*list = NULL;
400 	htable_t	*ht;
401 	htable_t	*higher;
402 	uint_t		h;
403 	uint_t		e;
404 	uintptr_t	va;
405 	x86pte_t	pte;
406 	uint_t		stolen = 0;
407 	uint_t		pass;
408 	uint_t		threshhold;
409 
410 	/*
411 	 * Limit htable_steal_passes to something reasonable
412 	 */
413 	if (htable_steal_passes == 0)
414 		htable_steal_passes = 1;
415 	if (htable_steal_passes > mmu.ptes_per_table)
416 		htable_steal_passes = mmu.ptes_per_table;
417 
418 	/*
419 	 * Loop through all hats. The 1st pass takes cached htables that
420 	 * aren't in use. The later passes steal by removing mappings, too.
421 	 */
422 	atomic_add_32(&htable_dont_cache, 1);
423 	for (pass = 1; pass <= htable_steal_passes && stolen < cnt; ++pass) {
424 		threshhold = pass / htable_steal_passes;
425 		hat = kas.a_hat->hat_next;
426 		for (;;) {
427 
428 			/*
429 			 * move to next hat
430 			 */
431 			mutex_enter(&hat_list_lock);
432 			hat->hat_flags &= ~HAT_VICTIM;
433 			cv_broadcast(&hat_list_cv);
434 			do {
435 				hat = hat->hat_prev;
436 			} while (hat->hat_flags & HAT_VICTIM);
437 			if (stolen == cnt || hat == kas.a_hat->hat_next) {
438 				mutex_exit(&hat_list_lock);
439 				break;
440 			}
441 			hat->hat_flags |= HAT_VICTIM;
442 			mutex_exit(&hat_list_lock);
443 
444 			/*
445 			 * Take any htables from the hat's cached "free" list.
446 			 */
447 			hat_enter(hat);
448 			while ((ht = hat->hat_ht_cached) != NULL &&
449 			    stolen < cnt) {
450 				hat->hat_ht_cached = ht->ht_next;
451 				ht->ht_next = list;
452 				list = ht;
453 				++stolen;
454 			}
455 			hat_exit(hat);
456 
457 			/*
458 			 * Don't steal on first pass.
459 			 */
460 			if (pass == 1 || stolen == cnt)
461 				continue;
462 
463 			/*
464 			 * search the active htables for one to steal
465 			 */
466 			for (h = 0; h < hat->hat_num_hash && stolen < cnt;
467 			    ++h) {
468 				higher = NULL;
469 				HTABLE_ENTER(h);
470 				for (ht = hat->hat_ht_hash[h]; ht;
471 				    ht = ht->ht_next) {
472 
473 					/*
474 					 * Can we rule out reaping?
475 					 */
476 					if (ht->ht_busy != 0 ||
477 					    (ht->ht_flags & HTABLE_SHARED_PFN)||
478 					    ht->ht_level == TOP_LEVEL(hat) ||
479 					    (ht->ht_level >=
480 					    mmu.max_page_level &&
481 					    ht->ht_valid_cnt > 0) ||
482 					    ht->ht_valid_cnt < threshhold ||
483 					    ht->ht_lock_cnt != 0)
484 						continue;
485 
486 					/*
487 					 * Increment busy so the htable can't
488 					 * disappear. We drop the htable mutex
489 					 * to avoid deadlocks with
490 					 * hat_pageunload() and the hment mutex
491 					 * while we call hat_pte_unmap()
492 					 */
493 					++ht->ht_busy;
494 					HTABLE_EXIT(h);
495 
496 					/*
497 					 * Try stealing.
498 					 * - unload and invalidate all PTEs
499 					 */
500 					for (e = 0, va = ht->ht_vaddr;
501 					    e < ht->ht_num_ptes &&
502 					    ht->ht_valid_cnt > 0 &&
503 					    ht->ht_busy == 1 &&
504 					    ht->ht_lock_cnt == 0;
505 					    ++e, va += MMU_PAGESIZE) {
506 						pte = x86pte_get(ht, e);
507 						if (!PTE_ISVALID(pte))
508 							continue;
509 						hat_pte_unmap(ht, e,
510 						    HAT_UNLOAD, pte, NULL);
511 					}
512 
513 					/*
514 					 * Reacquire htable lock. If we didn't
515 					 * remove all mappings in the table,
516 					 * or another thread added a new mapping
517 					 * behind us, give up on this table.
518 					 */
519 					HTABLE_ENTER(h);
520 					if (ht->ht_busy != 1 ||
521 					    ht->ht_valid_cnt != 0 ||
522 					    ht->ht_lock_cnt != 0) {
523 						--ht->ht_busy;
524 						continue;
525 					}
526 
527 					/*
528 					 * Steal it and unlink the page table.
529 					 */
530 					higher = ht->ht_parent;
531 					unlink_ptp(higher, ht, ht->ht_vaddr);
532 
533 					/*
534 					 * remove from the hash list
535 					 */
536 					if (ht->ht_next)
537 						ht->ht_next->ht_prev =
538 						    ht->ht_prev;
539 
540 					if (ht->ht_prev) {
541 						ht->ht_prev->ht_next =
542 						    ht->ht_next;
543 					} else {
544 						ASSERT(hat->hat_ht_hash[h] ==
545 						    ht);
546 						hat->hat_ht_hash[h] =
547 						    ht->ht_next;
548 					}
549 
550 					/*
551 					 * Break to outer loop to release the
552 					 * higher (ht_parent) pagtable. This
553 					 * spreads out the pain caused by
554 					 * pagefaults.
555 					 */
556 					ht->ht_next = list;
557 					list = ht;
558 					++stolen;
559 
560 					/*
561 					 * If this is the last steal, then move
562 					 * the hat list head, so that we start
563 					 * here next time.
564 					 */
565 					if (stolen == cnt) {
566 						mutex_enter(&hat_list_lock);
567 						kas.a_hat->hat_next = hat;
568 						mutex_exit(&hat_list_lock);
569 					}
570 					break;
571 				}
572 				HTABLE_EXIT(h);
573 				if (higher != NULL)
574 					htable_release(higher);
575 			}
576 		}
577 	}
578 	atomic_add_32(&htable_dont_cache, -1);
579 	return (list);
580 }
581 
582 
583 /*
584  * This is invoked from kmem when the system is low on memory.  We try
585  * to free hments, htables, and ptables to improve the memory situation.
586  */
587 /*ARGSUSED*/
588 static void
589 htable_reap(void *handle)
590 {
591 	uint_t		reap_cnt;
592 	htable_t	*list;
593 	htable_t	*ht;
594 
595 	HATSTAT_INC(hs_reap_attempts);
596 	if (!can_steal_post_boot)
597 		return;
598 
599 	/*
600 	 * Try to reap 5% of the page tables bounded by a maximum of
601 	 * 5% of physmem and a minimum of 10.
602 	 */
603 	reap_cnt = MIN(MAX(physmem / 20, active_ptables / 20), 10);
604 
605 	/*
606 	 * Let htable_steal() do the work, we just call htable_free()
607 	 */
608 	list = htable_steal(reap_cnt);
609 	while ((ht = list) != NULL) {
610 		list = ht->ht_next;
611 		HATSTAT_INC(hs_reaped);
612 		htable_free(ht);
613 	}
614 
615 	/*
616 	 * Free up excess reserves
617 	 */
618 	htable_adjust_reserve();
619 	hment_adjust_reserve();
620 }
621 
622 /*
623  * allocate an htable, stealing one or using the reserve if necessary
624  */
625 static htable_t *
626 htable_alloc(
627 	hat_t		*hat,
628 	uintptr_t	vaddr,
629 	level_t		level,
630 	htable_t	*shared)
631 {
632 	htable_t	*ht = NULL;
633 	uint_t		is_vlp;
634 	uint_t		is_bare = 0;
635 	uint_t		need_to_zero = 1;
636 	int		kmflags = (can_steal_post_boot ? KM_NOSLEEP : KM_SLEEP);
637 
638 	if (level < 0 || level > TOP_LEVEL(hat))
639 		panic("htable_alloc(): level %d out of range\n", level);
640 
641 	is_vlp = (hat->hat_flags & HAT_VLP) && level == VLP_LEVEL;
642 	if (is_vlp || shared != NULL)
643 		is_bare = 1;
644 
645 	/*
646 	 * First reuse a cached htable from the hat_ht_cached field, this
647 	 * avoids unnecessary trips through kmem/page allocators. This is also
648 	 * what happens during use_boot_reserve.
649 	 */
650 	if (hat->hat_ht_cached != NULL && !is_bare) {
651 		hat_enter(hat);
652 		ht = hat->hat_ht_cached;
653 		if (ht != NULL) {
654 			hat->hat_ht_cached = ht->ht_next;
655 			need_to_zero = 0;
656 			/* XX64 ASSERT() they're all zero somehow */
657 			ASSERT(ht->ht_pfn != PFN_INVALID);
658 		}
659 		hat_exit(hat);
660 	}
661 
662 	if (ht == NULL) {
663 		ASSERT(!use_boot_reserve);
664 		/*
665 		 * When allocating for hat_memload_arena, we use the reserve.
666 		 * Also use reserves if we are in a panic().
667 		 */
668 		if (curthread == hat_reserves_thread || panicstr != NULL) {
669 			ASSERT(panicstr != NULL || !is_bare);
670 			ASSERT(panicstr != NULL ||
671 			    curthread == hat_reserves_thread);
672 			ht = htable_get_reserve();
673 		} else {
674 			/*
675 			 * Donate successful htable allocations to the reserve.
676 			 */
677 			for (;;) {
678 				ASSERT(curthread != hat_reserves_thread);
679 				ht = kmem_cache_alloc(htable_cache, kmflags);
680 				if (ht == NULL)
681 					break;
682 				ht->ht_pfn = PFN_INVALID;
683 				if (curthread == hat_reserves_thread ||
684 				    panicstr != NULL ||
685 				    htable_reserve_cnt >= htable_reserve_amount)
686 					break;
687 				htable_put_reserve(ht);
688 			}
689 		}
690 
691 		/*
692 		 * allocate a page for the hardware page table if needed
693 		 */
694 		if (ht != NULL && !is_bare) {
695 			ptable_alloc(ht);
696 			if (ht->ht_pfn == PFN_INVALID) {
697 				kmem_cache_free(htable_cache, ht);
698 				ht = NULL;
699 			}
700 		}
701 	}
702 
703 	/*
704 	 * if allocations failed resort to stealing
705 	 */
706 	if (ht == NULL && can_steal_post_boot) {
707 		ht = htable_steal(1);
708 		HATSTAT_INC(hs_steals);
709 
710 		/*
711 		 * if we had to steal for a bare htable, release the
712 		 * page for the pagetable
713 		 */
714 		if (ht != NULL && is_bare)
715 			ptable_free(ht);
716 	}
717 
718 	/*
719 	 * All attempts to allocate or steal failed...
720 	 */
721 	if (ht == NULL)
722 		panic("htable_alloc(): couldn't steal\n");
723 
724 	/*
725 	 * Shared page tables have all entries locked and entries may not
726 	 * be added or deleted.
727 	 */
728 	ht->ht_flags = 0;
729 	if (shared != NULL) {
730 		ASSERT(level == 0);
731 		ASSERT(shared->ht_valid_cnt > 0);
732 		ht->ht_flags |= HTABLE_SHARED_PFN;
733 		ht->ht_pfn = shared->ht_pfn;
734 		ht->ht_lock_cnt = 0;
735 		ht->ht_valid_cnt = 0;		/* updated in hat_share() */
736 		ht->ht_shares = shared;
737 		need_to_zero = 0;
738 	} else {
739 		ht->ht_shares = NULL;
740 		ht->ht_lock_cnt = 0;
741 		ht->ht_valid_cnt = 0;
742 	}
743 
744 	/*
745 	 * setup flags, etc. for VLP htables
746 	 */
747 	if (is_vlp) {
748 		ht->ht_flags |= HTABLE_VLP;
749 		ht->ht_num_ptes = VLP_NUM_PTES;
750 		ASSERT(ht->ht_pfn == PFN_INVALID);
751 		need_to_zero = 0;
752 	} else if (level == mmu.max_level) {
753 		ht->ht_num_ptes = mmu.top_level_count;
754 	} else {
755 		ht->ht_num_ptes = mmu.ptes_per_table;
756 	}
757 
758 	/*
759 	 * fill in the htable
760 	 */
761 	ht->ht_hat = hat;
762 	ht->ht_parent = NULL;
763 	ht->ht_vaddr = vaddr;
764 	ht->ht_level = level;
765 	ht->ht_busy = 1;
766 	ht->ht_next = NULL;
767 	ht->ht_prev = NULL;
768 
769 	/*
770 	 * Zero out any freshly allocated page table
771 	 */
772 	if (need_to_zero)
773 		x86pte_zero(ht, 0, mmu.ptes_per_table);
774 	return (ht);
775 }
776 
777 /*
778  * Free up an htable, either to a hat's cached list, the reserves or
779  * back to kmem.
780  */
781 static void
782 htable_free(htable_t *ht)
783 {
784 	hat_t *hat = ht->ht_hat;
785 
786 	/*
787 	 * If the process isn't exiting, cache the free htable in the hat
788 	 * structure. We always do this for the boot reserve. We don't
789 	 * do this if the hat is exiting or we are stealing/reaping htables.
790 	 */
791 	if (hat != NULL &&
792 	    !(ht->ht_flags & HTABLE_SHARED_PFN) &&
793 	    (use_boot_reserve ||
794 	    (!(hat->hat_flags & HAT_FREEING) && !htable_dont_cache))) {
795 		ASSERT((ht->ht_flags & HTABLE_VLP) == 0);
796 		ASSERT(ht->ht_pfn != PFN_INVALID);
797 		hat_enter(hat);
798 		ht->ht_next = hat->hat_ht_cached;
799 		hat->hat_ht_cached = ht;
800 		hat_exit(hat);
801 		return;
802 	}
803 
804 	/*
805 	 * If we have a hardware page table, free it.
806 	 * We don't free page tables that are accessed by sharing someone else.
807 	 */
808 	if (ht->ht_flags & HTABLE_SHARED_PFN) {
809 		ASSERT(ht->ht_pfn != PFN_INVALID);
810 		ht->ht_pfn = PFN_INVALID;
811 	} else if (!(ht->ht_flags & HTABLE_VLP)) {
812 		ptable_free(ht);
813 	}
814 
815 	/*
816 	 * If we are the thread using the reserves, put free htables
817 	 * into reserves.
818 	 */
819 	if (curthread == hat_reserves_thread ||
820 	    htable_reserve_cnt < htable_reserve_amount)
821 		htable_put_reserve(ht);
822 	else
823 		kmem_cache_free(htable_cache, ht);
824 }
825 
826 
827 /*
828  * This is called when a hat is being destroyed or swapped out. We reap all
829  * the remaining htables in the hat cache. If destroying all left over
830  * htables are also destroyed.
831  *
832  * We also don't need to invalidate any of the PTPs nor do any demapping.
833  */
834 void
835 htable_purge_hat(hat_t *hat)
836 {
837 	htable_t *ht;
838 	int h;
839 
840 	/*
841 	 * Purge the htable cache if just reaping.
842 	 */
843 	if (!(hat->hat_flags & HAT_FREEING)) {
844 		atomic_add_32(&htable_dont_cache, 1);
845 		for (;;) {
846 			hat_enter(hat);
847 			ht = hat->hat_ht_cached;
848 			if (ht == NULL) {
849 				hat_exit(hat);
850 				break;
851 			}
852 			hat->hat_ht_cached = ht->ht_next;
853 			hat_exit(hat);
854 			htable_free(ht);
855 		}
856 		atomic_add_32(&htable_dont_cache, -1);
857 		return;
858 	}
859 
860 	/*
861 	 * if freeing, no locking is needed
862 	 */
863 	while ((ht = hat->hat_ht_cached) != NULL) {
864 		hat->hat_ht_cached = ht->ht_next;
865 		htable_free(ht);
866 	}
867 
868 	/*
869 	 * walk thru the htable hash table and free all the htables in it.
870 	 */
871 	for (h = 0; h < hat->hat_num_hash; ++h) {
872 		while ((ht = hat->hat_ht_hash[h]) != NULL) {
873 			if (ht->ht_next)
874 				ht->ht_next->ht_prev = ht->ht_prev;
875 
876 			if (ht->ht_prev) {
877 				ht->ht_prev->ht_next = ht->ht_next;
878 			} else {
879 				ASSERT(hat->hat_ht_hash[h] == ht);
880 				hat->hat_ht_hash[h] = ht->ht_next;
881 			}
882 			htable_free(ht);
883 		}
884 	}
885 }
886 
887 /*
888  * Unlink an entry for a table at vaddr and level out of the existing table
889  * one level higher. We are always holding the HASH_ENTER() when doing this.
890  */
891 static void
892 unlink_ptp(htable_t *higher, htable_t *old, uintptr_t vaddr)
893 {
894 	uint_t		entry = htable_va2entry(vaddr, higher);
895 	x86pte_t	expect = MAKEPTP(old->ht_pfn, old->ht_level);
896 	x86pte_t	found;
897 
898 	ASSERT(higher->ht_busy > 0);
899 	ASSERT(higher->ht_valid_cnt > 0);
900 	ASSERT(old->ht_valid_cnt == 0);
901 	found = x86pte_cas(higher, entry, expect, 0);
902 	if (found != expect)
903 		panic("Bad PTP found=" FMT_PTE ", expected=" FMT_PTE,
904 		    found, expect);
905 	HTABLE_DEC(higher->ht_valid_cnt);
906 }
907 
908 /*
909  * Link an entry for a new table at vaddr and level into the existing table
910  * one level higher. We are always holding the HASH_ENTER() when doing this.
911  */
912 static void
913 link_ptp(htable_t *higher, htable_t *new, uintptr_t vaddr)
914 {
915 	uint_t		entry = htable_va2entry(vaddr, higher);
916 	x86pte_t	newptp = MAKEPTP(new->ht_pfn, new->ht_level);
917 	x86pte_t	found;
918 
919 	ASSERT(higher->ht_busy > 0);
920 
921 	ASSERT(new->ht_level != mmu.max_level);
922 
923 	HTABLE_INC(higher->ht_valid_cnt);
924 
925 	found = x86pte_cas(higher, entry, 0, newptp);
926 	if (found != 0)
927 		panic("HAT: ptp not 0, found=" FMT_PTE, found);
928 }
929 
930 /*
931  * Release of an htable.
932  *
933  * During process exit, some empty page tables are not unlinked - hat_free_end()
934  * cleans them up. Upper level pagetable (mmu.max_page_level and higher) are
935  * only released during hat_free_end() or by htable_steal(). We always
936  * release SHARED page tables.
937  */
938 void
939 htable_release(htable_t *ht)
940 {
941 	uint_t		hashval;
942 	htable_t	*shared;
943 	htable_t	*higher;
944 	hat_t		*hat;
945 	uintptr_t	va;
946 	level_t		level;
947 
948 	while (ht != NULL) {
949 		shared = NULL;
950 		for (;;) {
951 			hat = ht->ht_hat;
952 			va = ht->ht_vaddr;
953 			level = ht->ht_level;
954 			hashval = HTABLE_HASH(hat, va, level);
955 
956 			/*
957 			 * The common case is that this isn't the last use of
958 			 * an htable so we don't want to free the htable.
959 			 */
960 			HTABLE_ENTER(hashval);
961 			ASSERT(ht->ht_lock_cnt == 0 || ht->ht_valid_cnt > 0);
962 			ASSERT(ht->ht_valid_cnt >= 0);
963 			ASSERT(ht->ht_busy > 0);
964 			if (ht->ht_valid_cnt > 0)
965 				break;
966 			if (ht->ht_busy > 1)
967 				break;
968 
969 			/*
970 			 * we always release empty shared htables
971 			 */
972 			if (!(ht->ht_flags & HTABLE_SHARED_PFN)) {
973 
974 				/*
975 				 * don't release if in address space tear down
976 				 */
977 				if (hat->hat_flags & HAT_FREEING)
978 					break;
979 
980 				/*
981 				 * At and above max_page_level, free if it's for
982 				 * a boot-time kernel mapping below kernelbase.
983 				 */
984 				if (level >= mmu.max_page_level &&
985 				    (hat != kas.a_hat || va >= kernelbase))
986 					break;
987 			}
988 
989 			/*
990 			 * remember if we destroy an htable that shares its PFN
991 			 * from elsewhere
992 			 */
993 			if (ht->ht_flags & HTABLE_SHARED_PFN) {
994 				ASSERT(ht->ht_level == 0);
995 				ASSERT(shared == NULL);
996 				shared = ht->ht_shares;
997 				HATSTAT_INC(hs_htable_unshared);
998 			}
999 
1000 			/*
1001 			 * Handle release of a table and freeing the htable_t.
1002 			 * Unlink it from the table higher (ie. ht_parent).
1003 			 */
1004 			ASSERT(ht->ht_lock_cnt == 0);
1005 			higher = ht->ht_parent;
1006 			ASSERT(higher != NULL);
1007 
1008 			/*
1009 			 * Unlink the pagetable.
1010 			 */
1011 			unlink_ptp(higher, ht, va);
1012 
1013 			/*
1014 			 * When any top level VLP page table entry changes, we
1015 			 * must issue a reload of cr3 on all processors.
1016 			 */
1017 			if ((hat->hat_flags & HAT_VLP) &&
1018 			    level == VLP_LEVEL - 1)
1019 				hat_demap(hat, DEMAP_ALL_ADDR);
1020 
1021 			/*
1022 			 * remove this htable from its hash list
1023 			 */
1024 			if (ht->ht_next)
1025 				ht->ht_next->ht_prev = ht->ht_prev;
1026 
1027 			if (ht->ht_prev) {
1028 				ht->ht_prev->ht_next = ht->ht_next;
1029 			} else {
1030 				ASSERT(hat->hat_ht_hash[hashval] == ht);
1031 				hat->hat_ht_hash[hashval] = ht->ht_next;
1032 			}
1033 			HTABLE_EXIT(hashval);
1034 			htable_free(ht);
1035 			ht = higher;
1036 		}
1037 
1038 		ASSERT(ht->ht_busy >= 1);
1039 		--ht->ht_busy;
1040 		HTABLE_EXIT(hashval);
1041 
1042 		/*
1043 		 * If we released a shared htable, do a release on the htable
1044 		 * from which it shared
1045 		 */
1046 		ht = shared;
1047 	}
1048 }
1049 
1050 /*
1051  * Find the htable for the pagetable at the given level for the given address.
1052  * If found acquires a hold that eventually needs to be htable_release()d
1053  */
1054 htable_t *
1055 htable_lookup(hat_t *hat, uintptr_t vaddr, level_t level)
1056 {
1057 	uintptr_t	base;
1058 	uint_t		hashval;
1059 	htable_t	*ht = NULL;
1060 
1061 	ASSERT(level >= 0);
1062 	ASSERT(level <= TOP_LEVEL(hat));
1063 
1064 	if (level == TOP_LEVEL(hat))
1065 		base = 0;
1066 	else
1067 		base = vaddr & LEVEL_MASK(level + 1);
1068 
1069 	hashval = HTABLE_HASH(hat, base, level);
1070 	HTABLE_ENTER(hashval);
1071 	for (ht = hat->hat_ht_hash[hashval]; ht; ht = ht->ht_next) {
1072 		if (ht->ht_hat == hat &&
1073 		    ht->ht_vaddr == base &&
1074 		    ht->ht_level == level)
1075 			break;
1076 	}
1077 	if (ht)
1078 		++ht->ht_busy;
1079 
1080 	HTABLE_EXIT(hashval);
1081 	return (ht);
1082 }
1083 
1084 /*
1085  * Acquires a hold on a known htable (from a locked hment entry).
1086  */
1087 void
1088 htable_acquire(htable_t *ht)
1089 {
1090 	hat_t		*hat = ht->ht_hat;
1091 	level_t		level = ht->ht_level;
1092 	uintptr_t	base = ht->ht_vaddr;
1093 	uint_t		hashval = HTABLE_HASH(hat, base, level);
1094 
1095 	HTABLE_ENTER(hashval);
1096 #ifdef DEBUG
1097 	/*
1098 	 * make sure the htable is there
1099 	 */
1100 	{
1101 		htable_t	*h;
1102 
1103 		for (h = hat->hat_ht_hash[hashval];
1104 		    h && h != ht;
1105 		    h = h->ht_next)
1106 			;
1107 		ASSERT(h == ht);
1108 	}
1109 #endif /* DEBUG */
1110 	++ht->ht_busy;
1111 	HTABLE_EXIT(hashval);
1112 }
1113 
1114 /*
1115  * Find the htable for the pagetable at the given level for the given address.
1116  * If found acquires a hold that eventually needs to be htable_release()d
1117  * If not found the table is created.
1118  *
1119  * Since we can't hold a hash table mutex during allocation, we have to
1120  * drop it and redo the search on a create. Then we may have to free the newly
1121  * allocated htable if another thread raced in and created it ahead of us.
1122  */
1123 htable_t *
1124 htable_create(
1125 	hat_t		*hat,
1126 	uintptr_t	vaddr,
1127 	level_t		level,
1128 	htable_t	*shared)
1129 {
1130 	uint_t		h;
1131 	level_t		l;
1132 	uintptr_t	base;
1133 	htable_t	*ht;
1134 	htable_t	*higher = NULL;
1135 	htable_t	*new = NULL;
1136 
1137 	if (level < 0 || level > TOP_LEVEL(hat))
1138 		panic("htable_create(): level %d out of range\n", level);
1139 
1140 	/*
1141 	 * Create the page tables in top down order.
1142 	 */
1143 	for (l = TOP_LEVEL(hat); l >= level; --l) {
1144 		new = NULL;
1145 		if (l == TOP_LEVEL(hat))
1146 			base = 0;
1147 		else
1148 			base = vaddr & LEVEL_MASK(l + 1);
1149 
1150 		h = HTABLE_HASH(hat, base, l);
1151 try_again:
1152 		/*
1153 		 * look up the htable at this level
1154 		 */
1155 		HTABLE_ENTER(h);
1156 		if (l == TOP_LEVEL(hat)) {
1157 			ht = hat->hat_htable;
1158 		} else {
1159 			for (ht = hat->hat_ht_hash[h]; ht; ht = ht->ht_next) {
1160 				ASSERT(ht->ht_hat == hat);
1161 				if (ht->ht_vaddr == base &&
1162 				    ht->ht_level == l)
1163 					break;
1164 			}
1165 		}
1166 
1167 		/*
1168 		 * if we found the htable, increment its busy cnt
1169 		 * and if we had allocated a new htable, free it.
1170 		 */
1171 		if (ht != NULL) {
1172 			/*
1173 			 * If we find a pre-existing shared table, it must
1174 			 * share from the same place.
1175 			 */
1176 			if (l == level && shared && ht->ht_shares &&
1177 			    ht->ht_shares != shared) {
1178 				panic("htable shared from wrong place "
1179 				    "found htable=%p shared=%p", ht, shared);
1180 			}
1181 			++ht->ht_busy;
1182 			HTABLE_EXIT(h);
1183 			if (new)
1184 				htable_free(new);
1185 			if (higher != NULL)
1186 				htable_release(higher);
1187 			higher = ht;
1188 
1189 		/*
1190 		 * if we didn't find it on the first search
1191 		 * allocate a new one and search again
1192 		 */
1193 		} else if (new == NULL) {
1194 			HTABLE_EXIT(h);
1195 			new = htable_alloc(hat, base, l,
1196 			    l == level ? shared : NULL);
1197 			goto try_again;
1198 
1199 		/*
1200 		 * 2nd search and still not there, use "new" table
1201 		 * Link new table into higher, when not at top level.
1202 		 */
1203 		} else {
1204 			ht = new;
1205 			if (higher != NULL) {
1206 				link_ptp(higher, ht, base);
1207 				ht->ht_parent = higher;
1208 
1209 				/*
1210 				 * When any top level VLP page table changes,
1211 				 * we must reload cr3 on all processors.
1212 				 */
1213 #ifdef __i386
1214 				if (mmu.pae_hat &&
1215 #else /* !__i386 */
1216 				if ((hat->hat_flags & HAT_VLP) &&
1217 #endif /* __i386 */
1218 				    l == VLP_LEVEL - 1)
1219 					hat_demap(hat, DEMAP_ALL_ADDR);
1220 			}
1221 			ht->ht_next = hat->hat_ht_hash[h];
1222 			ASSERT(ht->ht_prev == NULL);
1223 			if (hat->hat_ht_hash[h])
1224 				hat->hat_ht_hash[h]->ht_prev = ht;
1225 			hat->hat_ht_hash[h] = ht;
1226 			HTABLE_EXIT(h);
1227 
1228 			/*
1229 			 * Note we don't do htable_release(higher).
1230 			 * That happens recursively when "new" is removed by
1231 			 * htable_release() or htable_steal().
1232 			 */
1233 			higher = ht;
1234 
1235 			/*
1236 			 * If we just created a new shared page table we
1237 			 * increment the shared htable's busy count, so that
1238 			 * it can't be the victim of a steal even if it's empty.
1239 			 */
1240 			if (l == level && shared) {
1241 				(void) htable_lookup(shared->ht_hat,
1242 				    shared->ht_vaddr, shared->ht_level);
1243 				HATSTAT_INC(hs_htable_shared);
1244 			}
1245 		}
1246 	}
1247 
1248 	return (ht);
1249 }
1250 
1251 /*
1252  * Walk through a given htable looking for the first valid entry.  This
1253  * routine takes both a starting and ending address.  The starting address
1254  * is required to be within the htable provided by the caller, but there is
1255  * no such restriction on the ending address.
1256  *
1257  * If the routine finds a valid entry in the htable (at or beyond the
1258  * starting address), the PTE (and its address) will be returned.
1259  * This PTE may correspond to either a page or a pagetable - it is the
1260  * caller's responsibility to determine which.  If no valid entry is
1261  * found, 0 (and invalid PTE) and the next unexamined address will be
1262  * returned.
1263  *
1264  * The loop has been carefully coded for optimization.
1265  */
1266 static x86pte_t
1267 htable_scan(htable_t *ht, uintptr_t *vap, uintptr_t eaddr)
1268 {
1269 	uint_t e;
1270 	x86pte_t found_pte = (x86pte_t)0;
1271 	char *pte_ptr;
1272 	char *end_pte_ptr;
1273 	int l = ht->ht_level;
1274 	uintptr_t va = *vap & LEVEL_MASK(l);
1275 	size_t pgsize = LEVEL_SIZE(l);
1276 
1277 	ASSERT(va >= ht->ht_vaddr);
1278 	ASSERT(va <= HTABLE_LAST_PAGE(ht));
1279 
1280 	/*
1281 	 * Compute the starting index and ending virtual address
1282 	 */
1283 	e = htable_va2entry(va, ht);
1284 
1285 	/*
1286 	 * The following page table scan code knows that the valid
1287 	 * bit of a PTE is in the lowest byte AND that x86 is little endian!!
1288 	 */
1289 	pte_ptr = (char *)x86pte_access_pagetable(ht);
1290 	end_pte_ptr = pte_ptr + (ht->ht_num_ptes << mmu.pte_size_shift);
1291 	pte_ptr += e << mmu.pte_size_shift;
1292 	while (*pte_ptr == 0) {
1293 		va += pgsize;
1294 		if (va >= eaddr)
1295 			break;
1296 		pte_ptr += mmu.pte_size;
1297 		ASSERT(pte_ptr <= end_pte_ptr);
1298 		if (pte_ptr == end_pte_ptr)
1299 			break;
1300 	}
1301 
1302 	/*
1303 	 * if we found a valid PTE, load the entire PTE
1304 	 */
1305 	if (va < eaddr && pte_ptr != end_pte_ptr) {
1306 		if (mmu.pae_hat) {
1307 			found_pte = *(x86pte_t *)pte_ptr;
1308 #if defined(__i386)
1309 			/*
1310 			 * 64 bit reads on 32 bit x86 are not atomic
1311 			 */
1312 			while (found_pte != *(volatile x86pte_t *)pte_ptr)
1313 				found_pte = *(volatile x86pte_t *)pte_ptr;
1314 #endif
1315 		} else {
1316 			found_pte = *(x86pte32_t *)pte_ptr;
1317 		}
1318 	}
1319 	x86pte_release_pagetable(ht);
1320 
1321 #if defined(__amd64)
1322 	/*
1323 	 * deal with VA hole on amd64
1324 	 */
1325 	if (l == mmu.max_level && va >= mmu.hole_start && va <= mmu.hole_end)
1326 		va = mmu.hole_end + va - mmu.hole_start;
1327 #endif /* __amd64 */
1328 
1329 	*vap = va;
1330 	return (found_pte);
1331 }
1332 
1333 /*
1334  * Find the address and htable for the first populated translation at or
1335  * above the given virtual address.  The caller may also specify an upper
1336  * limit to the address range to search.  Uses level information to quickly
1337  * skip unpopulated sections of virtual address spaces.
1338  *
1339  * If not found returns NULL. When found, returns the htable and virt addr
1340  * and has a hold on the htable.
1341  */
1342 x86pte_t
1343 htable_walk(
1344 	struct hat *hat,
1345 	htable_t **htp,
1346 	uintptr_t *vaddr,
1347 	uintptr_t eaddr)
1348 {
1349 	uintptr_t va = *vaddr;
1350 	htable_t *ht;
1351 	htable_t *prev = *htp;
1352 	level_t l;
1353 	level_t max_mapped_level;
1354 	x86pte_t pte;
1355 
1356 	ASSERT(eaddr > va);
1357 
1358 	/*
1359 	 * If this is a user address, then we know we need not look beyond
1360 	 * kernelbase.
1361 	 */
1362 	ASSERT(hat == kas.a_hat || eaddr <= kernelbase ||
1363 	    eaddr == HTABLE_WALK_TO_END);
1364 	if (hat != kas.a_hat && eaddr == HTABLE_WALK_TO_END)
1365 		eaddr = kernelbase;
1366 
1367 	/*
1368 	 * If we're coming in with a previous page table, search it first
1369 	 * without doing an htable_lookup(), this should be frequent.
1370 	 */
1371 	if (prev) {
1372 		ASSERT(prev->ht_busy > 0);
1373 		ASSERT(prev->ht_vaddr <= va);
1374 		l = prev->ht_level;
1375 		if (va <= HTABLE_LAST_PAGE(prev)) {
1376 			pte = htable_scan(prev, &va, eaddr);
1377 
1378 			if (PTE_ISPAGE(pte, l)) {
1379 				*vaddr = va;
1380 				*htp = prev;
1381 				return (pte);
1382 			}
1383 		}
1384 
1385 		/*
1386 		 * We found nothing in the htable provided by the caller,
1387 		 * so fall through and do the full search
1388 		 */
1389 		htable_release(prev);
1390 	}
1391 
1392 	/*
1393 	 * Find the level of the largest pagesize used by this HAT.
1394 	 */
1395 	max_mapped_level = 0;
1396 	for (l = 1; l <= mmu.max_page_level; ++l)
1397 		if (hat->hat_pages_mapped[l] != 0)
1398 			max_mapped_level = l;
1399 
1400 	while (va < eaddr && va >= *vaddr) {
1401 		ASSERT(!IN_VA_HOLE(va));
1402 
1403 		/*
1404 		 *  Find lowest table with any entry for given address.
1405 		 */
1406 		for (l = 0; l <= TOP_LEVEL(hat); ++l) {
1407 			ht = htable_lookup(hat, va, l);
1408 			if (ht != NULL) {
1409 				pte = htable_scan(ht, &va, eaddr);
1410 				if (PTE_ISPAGE(pte, l)) {
1411 					*vaddr = va;
1412 					*htp = ht;
1413 					return (pte);
1414 				}
1415 				htable_release(ht);
1416 				break;
1417 			}
1418 
1419 			/*
1420 			 * The ht is never NULL at the top level since
1421 			 * the top level htable is created in hat_alloc().
1422 			 */
1423 			ASSERT(l < TOP_LEVEL(hat));
1424 
1425 			/*
1426 			 * No htable covers the address. If there is no
1427 			 * larger page size that could cover it, we
1428 			 * skip to the start of the next page table.
1429 			 */
1430 			if (l >= max_mapped_level) {
1431 				va = NEXT_ENTRY_VA(va, l + 1);
1432 				break;
1433 			}
1434 		}
1435 	}
1436 
1437 	*vaddr = 0;
1438 	*htp = NULL;
1439 	return (0);
1440 }
1441 
1442 /*
1443  * Find the htable and page table entry index of the given virtual address
1444  * with pagesize at or below given level.
1445  * If not found returns NULL. When found, returns the htable, sets
1446  * entry, and has a hold on the htable.
1447  */
1448 htable_t *
1449 htable_getpte(
1450 	struct hat *hat,
1451 	uintptr_t vaddr,
1452 	uint_t *entry,
1453 	x86pte_t *pte,
1454 	level_t level)
1455 {
1456 	htable_t	*ht;
1457 	level_t		l;
1458 	uint_t		e;
1459 
1460 	ASSERT(level <= mmu.max_page_level);
1461 
1462 	for (l = 0; l <= level; ++l) {
1463 		ht = htable_lookup(hat, vaddr, l);
1464 		if (ht == NULL)
1465 			continue;
1466 		e = htable_va2entry(vaddr, ht);
1467 		if (entry != NULL)
1468 			*entry = e;
1469 		if (pte != NULL)
1470 			*pte = x86pte_get(ht, e);
1471 		return (ht);
1472 	}
1473 	return (NULL);
1474 }
1475 
1476 /*
1477  * Find the htable and page table entry index of the given virtual address.
1478  * There must be a valid page mapped at the given address.
1479  * If not found returns NULL. When found, returns the htable, sets
1480  * entry, and has a hold on the htable.
1481  */
1482 htable_t *
1483 htable_getpage(struct hat *hat, uintptr_t vaddr, uint_t *entry)
1484 {
1485 	htable_t	*ht;
1486 	uint_t		e;
1487 	x86pte_t	pte;
1488 
1489 	ht = htable_getpte(hat, vaddr, &e, &pte, mmu.max_page_level);
1490 	if (ht == NULL)
1491 		return (NULL);
1492 
1493 	if (entry)
1494 		*entry = e;
1495 
1496 	if (PTE_ISPAGE(pte, ht->ht_level))
1497 		return (ht);
1498 	htable_release(ht);
1499 	return (NULL);
1500 }
1501 
1502 
1503 void
1504 htable_init()
1505 {
1506 	/*
1507 	 * To save on kernel VA usage, we avoid debug information in 32 bit
1508 	 * kernels.
1509 	 */
1510 #if defined(__amd64)
1511 	int	kmem_flags = KMC_NOHASH;
1512 #elif defined(__i386)
1513 	int	kmem_flags = KMC_NOHASH | KMC_NODEBUG;
1514 #endif
1515 
1516 	/*
1517 	 * initialize kmem caches
1518 	 */
1519 	htable_cache = kmem_cache_create("htable_t",
1520 	    sizeof (htable_t), 0, NULL, NULL,
1521 	    htable_reap, NULL, hat_memload_arena, kmem_flags);
1522 }
1523 
1524 /*
1525  * get the pte index for the virtual address in the given htable's pagetable
1526  */
1527 uint_t
1528 htable_va2entry(uintptr_t va, htable_t *ht)
1529 {
1530 	level_t	l = ht->ht_level;
1531 
1532 	ASSERT(va >= ht->ht_vaddr);
1533 	ASSERT(va <= HTABLE_LAST_PAGE(ht));
1534 	return ((va >> LEVEL_SHIFT(l)) & (ht->ht_num_ptes - 1));
1535 }
1536 
1537 /*
1538  * Given an htable and the index of a pte in it, return the virtual address
1539  * of the page.
1540  */
1541 uintptr_t
1542 htable_e2va(htable_t *ht, uint_t entry)
1543 {
1544 	level_t	l = ht->ht_level;
1545 	uintptr_t va;
1546 
1547 	ASSERT(entry < ht->ht_num_ptes);
1548 	va = ht->ht_vaddr + ((uintptr_t)entry << LEVEL_SHIFT(l));
1549 
1550 	/*
1551 	 * Need to skip over any VA hole in top level table
1552 	 */
1553 #if defined(__amd64)
1554 	if (ht->ht_level == mmu.max_level && va >= mmu.hole_start)
1555 		va += ((mmu.hole_end - mmu.hole_start) + 1);
1556 #endif
1557 
1558 	return (va);
1559 }
1560 
1561 /*
1562  * The code uses compare and swap instructions to read/write PTE's to
1563  * avoid atomicity problems, since PTEs can be 8 bytes on 32 bit systems.
1564  * Again this can be optimized on 64 bit systems, since aligned load/store
1565  * will naturally be atomic.
1566  *
1567  * The combination of using kpreempt_disable()/_enable() and the hci_mutex
1568  * are used to ensure that an interrupt won't overwrite a temporary mapping
1569  * while it's in use. If an interrupt thread tries to access a PTE, it will
1570  * yield briefly back to the pinned thread which holds the cpu's hci_mutex.
1571  */
1572 
1573 static struct hat_cpu_info init_hci;	/* used for cpu 0 */
1574 
1575 /*
1576  * Initialize a CPU private window for mapping page tables.
1577  * There will be 3 total pages of addressing needed:
1578  *
1579  *	1 for r/w access to pagetables
1580  *	1 for r access when copying pagetables (hat_alloc)
1581  *	1 that will map the PTEs for the 1st 2, so we can access them quickly
1582  *
1583  * We use vmem_xalloc() to get a correct alignment so that only one
1584  * hat_mempte_setup() is needed.
1585  */
1586 void
1587 x86pte_cpu_init(cpu_t *cpu, void *pages)
1588 {
1589 	struct hat_cpu_info *hci;
1590 	caddr_t va;
1591 
1592 	/*
1593 	 * We can't use kmem_alloc/vmem_alloc for the 1st CPU, as this is
1594 	 * called before we've activated our own HAT
1595 	 */
1596 	if (pages != NULL) {
1597 		hci = &init_hci;
1598 		va = pages;
1599 	} else {
1600 		hci = kmem_alloc(sizeof (struct hat_cpu_info), KM_SLEEP);
1601 		va = vmem_xalloc(heap_arena, 3 * MMU_PAGESIZE, MMU_PAGESIZE, 0,
1602 		    LEVEL_SIZE(1), NULL, NULL, VM_SLEEP);
1603 	}
1604 	mutex_init(&hci->hci_mutex, NULL, MUTEX_DEFAULT, NULL);
1605 
1606 	/*
1607 	 * If we are using segkpm, then there is no need for any of the
1608 	 * mempte support.  We can access the desired memory through a kpm
1609 	 * mapping rather than setting up a temporary mempte mapping.
1610 	 */
1611 	if (kpm_enable == 0) {
1612 		hci->hci_mapped_pfn = PFN_INVALID;
1613 
1614 		hci->hci_kernel_pte =
1615 		    hat_mempte_kern_setup(va, va + (2 * MMU_PAGESIZE));
1616 		hci->hci_pagetable_va = (void *)va;
1617 	}
1618 
1619 	cpu->cpu_hat_info = hci;
1620 }
1621 
1622 /*
1623  * Macro to establish temporary mappings for x86pte_XXX routines.
1624  */
1625 #define	X86PTE_REMAP(addr, pte, index, perm, pfn)	{		\
1626 		x86pte_t t;						\
1627 									\
1628 		t = MAKEPTE((pfn), 0) | (perm) | mmu.pt_global | mmu.pt_nx;\
1629 		if (mmu.pae_hat)					\
1630 			pte[index] = t;					\
1631 		else							\
1632 			((x86pte32_t *)(pte))[index] = t;		\
1633 		mmu_tlbflush_entry((caddr_t)(addr));			\
1634 }
1635 
1636 /*
1637  * Disable preemption and establish a mapping to the pagetable with the
1638  * given pfn. This is optimized for there case where it's the same
1639  * pfn as we last used referenced from this CPU.
1640  */
1641 static x86pte_t *
1642 x86pte_access_pagetable(htable_t *ht)
1643 {
1644 	pfn_t pfn;
1645 	struct hat_cpu_info *hci;
1646 
1647 	/*
1648 	 * VLP pagetables are contained in the hat_t
1649 	 */
1650 	if (ht->ht_flags & HTABLE_VLP)
1651 		return (ht->ht_hat->hat_vlp_ptes);
1652 
1653 	/*
1654 	 * During early boot, use hat_boot_remap() of a page table adddress.
1655 	 */
1656 	pfn = ht->ht_pfn;
1657 	ASSERT(pfn != PFN_INVALID);
1658 	if (kpm_enable)
1659 		return ((x86pte_t *)hat_kpm_pfn2va(pfn));
1660 
1661 	if (!khat_running) {
1662 		(void) hat_boot_remap(ptable_va, pfn);
1663 		return ((x86pte_t *)ptable_va);
1664 	}
1665 
1666 	/*
1667 	 * Normally, disable preemption and grab the CPU's hci_mutex
1668 	 */
1669 	kpreempt_disable();
1670 	hci = CPU->cpu_hat_info;
1671 	ASSERT(hci != NULL);
1672 	mutex_enter(&hci->hci_mutex);
1673 	if (hci->hci_mapped_pfn != pfn) {
1674 		/*
1675 		 * The current mapping doesn't already point to this page.
1676 		 * Update the CPU specific pagetable mapping to map the pfn.
1677 		 */
1678 		X86PTE_REMAP(hci->hci_pagetable_va, hci->hci_kernel_pte, 0,
1679 		    PT_WRITABLE, pfn);
1680 		hci->hci_mapped_pfn = pfn;
1681 	}
1682 	return (hci->hci_pagetable_va);
1683 }
1684 
1685 /*
1686  * Release access to a page table.
1687  */
1688 static void
1689 x86pte_release_pagetable(htable_t *ht)
1690 {
1691 	struct hat_cpu_info *hci;
1692 
1693 	if (kpm_enable)
1694 		return;
1695 
1696 	/*
1697 	 * nothing to do for VLP htables
1698 	 */
1699 	if (ht->ht_flags & HTABLE_VLP)
1700 		return;
1701 
1702 	/*
1703 	 * During boot-up hat_kern_setup(), erase the boot loader remapping.
1704 	 */
1705 	if (!khat_running) {
1706 		hat_boot_demap(ptable_va);
1707 		return;
1708 	}
1709 
1710 	/*
1711 	 * Normal Operation: drop the CPU's hci_mutex and restore preemption
1712 	 */
1713 	hci = CPU->cpu_hat_info;
1714 	ASSERT(hci != NULL);
1715 	mutex_exit(&hci->hci_mutex);
1716 	kpreempt_enable();
1717 }
1718 
1719 /*
1720  * Atomic retrieval of a pagetable entry
1721  */
1722 x86pte_t
1723 x86pte_get(htable_t *ht, uint_t entry)
1724 {
1725 	x86pte_t	pte;
1726 	x86pte32_t	*pte32p;
1727 	volatile x86pte_t	*ptep;
1728 
1729 	/*
1730 	 * 32 bit (non-pae) is always atomic.
1731 	 * 64 bit is only atomic on 64 bit mode.
1732 	 */
1733 	ptep = x86pte_access_pagetable(ht);
1734 	if (mmu.pae_hat) {
1735 		pte = ptep[entry];
1736 #if defined(__i386)
1737 		while (pte != ptep[entry])
1738 			pte = ptep[entry];
1739 #endif /* __i386 */
1740 	} else {
1741 		pte32p = (x86pte32_t *)ptep;
1742 		pte = pte32p[entry];
1743 	}
1744 	x86pte_release_pagetable(ht);
1745 	return (pte);
1746 }
1747 
1748 
1749 /*
1750  * Atomic unconditional set of a page table entry, it returns the previous
1751  * value.
1752  */
1753 x86pte_t
1754 x86pte_set(htable_t *ht, uint_t entry, x86pte_t new, void *ptr)
1755 {
1756 	x86pte_t	old;
1757 	x86pte_t	prev;
1758 	x86pte_t	*ptep;
1759 	x86pte32_t	*pte32p;
1760 	x86pte32_t	n32, p32;
1761 
1762 	ASSERT(!(ht->ht_flags & HTABLE_SHARED_PFN));
1763 	if (ptr == NULL) {
1764 		ptep = x86pte_access_pagetable(ht);
1765 		ptep = (void *)((caddr_t)ptep + (entry << mmu.pte_size_shift));
1766 	} else {
1767 		ptep = ptr;
1768 	}
1769 
1770 	if (mmu.pae_hat) {
1771 		for (;;) {
1772 			prev = *ptep;
1773 			if (prev == new) {
1774 				old = new;
1775 				break;
1776 			}
1777 			old = cas64(ptep, prev, new);
1778 			if (old == prev)
1779 				break;
1780 		}
1781 	} else {
1782 		pte32p = (x86pte32_t *)ptep;
1783 		n32 = new;
1784 		for (;;) {
1785 			p32 = *pte32p;
1786 			if (p32 == n32) {
1787 				old = new;
1788 				break;
1789 			}
1790 			old = cas32(pte32p, p32, n32);
1791 			if (old == p32)
1792 				break;
1793 		}
1794 	}
1795 	if (ptr == NULL)
1796 		x86pte_release_pagetable(ht);
1797 	return (old);
1798 }
1799 
1800 /*
1801  * Atomic compare and swap of a page table entry.
1802  */
1803 static x86pte_t
1804 x86pte_cas(htable_t *ht, uint_t entry, x86pte_t old, x86pte_t new)
1805 {
1806 	x86pte_t	pte;
1807 	x86pte_t	*ptep;
1808 	x86pte32_t	pte32, o32, n32;
1809 	x86pte32_t	*pte32p;
1810 
1811 	ASSERT(!(ht->ht_flags & HTABLE_SHARED_PFN));
1812 	ptep = x86pte_access_pagetable(ht);
1813 	if (mmu.pae_hat) {
1814 		pte = cas64(&ptep[entry], old, new);
1815 	} else {
1816 		o32 = old;
1817 		n32 = new;
1818 		pte32p = (x86pte32_t *)ptep;
1819 		pte32 = cas32(&pte32p[entry], o32, n32);
1820 		pte = pte32;
1821 	}
1822 	x86pte_release_pagetable(ht);
1823 
1824 	return (pte);
1825 }
1826 
1827 /*
1828  * data structure for cross call information
1829  */
1830 typedef struct xcall_info {
1831 	x86pte_t	xi_pte;
1832 	x86pte_t	xi_old;
1833 	x86pte_t	*xi_pteptr;
1834 	pfn_t		xi_pfn;
1835 	processorid_t	xi_cpuid;
1836 	level_t		xi_level;
1837 	xc_func_t	xi_func;
1838 } xcall_info_t;
1839 
1840 /*
1841  * Cross call service function to atomically invalidate a PTE and flush TLBs
1842  */
1843 /*ARGSUSED*/
1844 static int
1845 x86pte_inval_func(xc_arg_t a1, xc_arg_t a2, xc_arg_t a3)
1846 {
1847 	xcall_info_t	*xi = (xcall_info_t *)a1;
1848 	caddr_t		addr = (caddr_t)a2;
1849 
1850 	/*
1851 	 * Only the initiating cpu invalidates the page table entry.
1852 	 * It returns the previous PTE value to the caller.
1853 	 */
1854 	if (CPU->cpu_id == xi->xi_cpuid) {
1855 		x86pte_t	*ptep = xi->xi_pteptr;
1856 		pfn_t		pfn = xi->xi_pfn;
1857 		level_t		level = xi->xi_level;
1858 		x86pte_t	old;
1859 		x86pte_t	prev;
1860 		x86pte32_t	*pte32p;
1861 		x86pte32_t	p32;
1862 
1863 		if (mmu.pae_hat) {
1864 			for (;;) {
1865 				prev = *ptep;
1866 				if (PTE2PFN(prev, level) != pfn)
1867 					break;
1868 				old = cas64(ptep, prev, 0);
1869 				if (old == prev)
1870 					break;
1871 			}
1872 		} else {
1873 			pte32p = (x86pte32_t *)ptep;
1874 			for (;;) {
1875 				p32 = *pte32p;
1876 				if (PTE2PFN(p32, level) != pfn)
1877 					break;
1878 				old = cas32(pte32p, p32, 0);
1879 				if (old == p32)
1880 					break;
1881 			}
1882 			prev = p32;
1883 		}
1884 		xi->xi_pte = prev;
1885 	}
1886 
1887 	/*
1888 	 * For a normal address, we just flush one page mapping
1889 	 * Otherwise reload cr3 to effect a complete TLB flush.
1890 	 *
1891 	 * Note we don't reload VLP pte's -- this assume we never have a
1892 	 * large page size at VLP_LEVEL for VLP processes.
1893 	 */
1894 	if ((uintptr_t)addr != DEMAP_ALL_ADDR) {
1895 		mmu_tlbflush_entry(addr);
1896 	} else {
1897 		reload_cr3();
1898 	}
1899 	return (0);
1900 }
1901 
1902 /*
1903  * Cross call service function to atomically change a PTE and flush TLBs
1904  */
1905 /*ARGSUSED*/
1906 static int
1907 x86pte_update_func(xc_arg_t a1, xc_arg_t a2, xc_arg_t a3)
1908 {
1909 	xcall_info_t	*xi = (xcall_info_t *)a1;
1910 	caddr_t		addr = (caddr_t)a2;
1911 
1912 	/*
1913 	 * Only the initiating cpu changes the page table entry.
1914 	 * It returns the previous PTE value to the caller.
1915 	 */
1916 	if (CPU->cpu_id == xi->xi_cpuid) {
1917 		x86pte_t	*ptep = xi->xi_pteptr;
1918 		x86pte_t	new = xi->xi_pte;
1919 		x86pte_t	old = xi->xi_old;
1920 		x86pte_t	prev;
1921 
1922 		if (mmu.pae_hat) {
1923 			prev = cas64(ptep, old, new);
1924 		} else {
1925 			x86pte32_t o32 = old;
1926 			x86pte32_t n32 = new;
1927 			x86pte32_t *pte32p = (x86pte32_t *)ptep;
1928 			prev = cas32(pte32p, o32, n32);
1929 		}
1930 
1931 		xi->xi_pte = prev;
1932 	}
1933 
1934 	/*
1935 	 * Flush the TLB entry
1936 	 */
1937 	if ((uintptr_t)addr != DEMAP_ALL_ADDR)
1938 		mmu_tlbflush_entry(addr);
1939 	else
1940 		reload_cr3();
1941 	return (0);
1942 }
1943 
1944 /*
1945  * Use cross calls to change a page table entry and invalidate TLBs.
1946  */
1947 void
1948 x86pte_xcall(hat_t *hat, xcall_info_t *xi, uintptr_t addr)
1949 {
1950 	cpuset_t	cpus;
1951 
1952 	/*
1953 	 * Given the current implementation of hat_share(), doing a
1954 	 * hat_pageunload() on a shared page table requries invalidating
1955 	 * all user TLB entries on all CPUs.
1956 	 */
1957 	if (hat->hat_flags & HAT_SHARED) {
1958 		hat = kas.a_hat;
1959 		addr = DEMAP_ALL_ADDR;
1960 	}
1961 
1962 	/*
1963 	 * Use a cross call to do the invalidations.
1964 	 * Note the current CPU always has to be in the cross call CPU set.
1965 	 */
1966 	kpreempt_disable();
1967 	xi->xi_cpuid = CPU->cpu_id;
1968 	CPUSET_ZERO(cpus);
1969 	if (hat == kas.a_hat) {
1970 		CPUSET_OR(cpus, khat_cpuset);
1971 	} else {
1972 		mutex_enter(&hat->hat_switch_mutex);
1973 		CPUSET_OR(cpus, hat->hat_cpus);
1974 		CPUSET_ADD(cpus, CPU->cpu_id);
1975 	}
1976 
1977 	/*
1978 	 * Use a cross call to modify the page table entry and invalidate TLBs.
1979 	 * If we're panic'ing, don't bother with the cross call.
1980 	 * Note the panicstr check isn't bullet proof and the panic system
1981 	 * ought to be made tighter.
1982 	 */
1983 	if (panicstr == NULL)
1984 		xc_wait_sync((xc_arg_t)xi, addr, NULL, X_CALL_HIPRI,
1985 			    cpus, xi->xi_func);
1986 	else
1987 		(void) xi->xi_func((xc_arg_t)xi, (xc_arg_t)addr, NULL);
1988 	if (hat != kas.a_hat)
1989 		mutex_exit(&hat->hat_switch_mutex);
1990 	kpreempt_enable();
1991 }
1992 
1993 /*
1994  * Invalidate a page table entry if it currently maps the given pfn.
1995  * This returns the previous value of the PTE.
1996  */
1997 x86pte_t
1998 x86pte_invalidate_pfn(htable_t *ht, uint_t entry, pfn_t pfn, void *pte_ptr)
1999 {
2000 	xcall_info_t	xi;
2001 	x86pte_t	*ptep;
2002 	hat_t		*hat;
2003 	uintptr_t	addr;
2004 
2005 	ASSERT(!(ht->ht_flags & HTABLE_SHARED_PFN));
2006 	if (pte_ptr != NULL) {
2007 		ptep = pte_ptr;
2008 	} else {
2009 		ptep = x86pte_access_pagetable(ht);
2010 		ptep = (void *)((caddr_t)ptep + (entry << mmu.pte_size_shift));
2011 	}
2012 
2013 	/*
2014 	 * Fill in the structure used by the cross call function to do the
2015 	 * invalidation.
2016 	 */
2017 	xi.xi_pte = 0;
2018 	xi.xi_pteptr = ptep;
2019 	xi.xi_pfn = pfn;
2020 	xi.xi_level = ht->ht_level;
2021 	xi.xi_func = x86pte_inval_func;
2022 	ASSERT(xi.xi_level != VLP_LEVEL);
2023 
2024 	hat = ht->ht_hat;
2025 	addr = htable_e2va(ht, entry);
2026 
2027 	x86pte_xcall(hat, &xi, addr);
2028 
2029 	if (pte_ptr == NULL)
2030 		x86pte_release_pagetable(ht);
2031 	return (xi.xi_pte);
2032 }
2033 
2034 /*
2035  * update a PTE and invalidate any stale TLB entries.
2036  */
2037 x86pte_t
2038 x86pte_update(htable_t *ht, uint_t entry, x86pte_t expected, x86pte_t new)
2039 {
2040 	xcall_info_t	xi;
2041 	x86pte_t	*ptep;
2042 	hat_t		*hat;
2043 	uintptr_t	addr;
2044 
2045 	ASSERT(!(ht->ht_flags & HTABLE_SHARED_PFN));
2046 	ptep = x86pte_access_pagetable(ht);
2047 	ptep = (void *)((caddr_t)ptep + (entry << mmu.pte_size_shift));
2048 
2049 	/*
2050 	 * Fill in the structure used by the cross call function to do the
2051 	 * invalidation.
2052 	 */
2053 	xi.xi_pte = new;
2054 	xi.xi_old = expected;
2055 	xi.xi_pteptr = ptep;
2056 	xi.xi_func = x86pte_update_func;
2057 
2058 	hat = ht->ht_hat;
2059 	addr = htable_e2va(ht, entry);
2060 
2061 	x86pte_xcall(hat, &xi, addr);
2062 
2063 	x86pte_release_pagetable(ht);
2064 	return (xi.xi_pte);
2065 }
2066 
2067 /*
2068  * Copy page tables - this is just a little more complicated than the
2069  * previous routines. Note that it's also not atomic! It also is never
2070  * used for VLP pagetables.
2071  */
2072 void
2073 x86pte_copy(htable_t *src, htable_t *dest, uint_t entry, uint_t count)
2074 {
2075 	struct hat_cpu_info *hci;
2076 	caddr_t	src_va;
2077 	caddr_t dst_va;
2078 	size_t size;
2079 
2080 	ASSERT(khat_running);
2081 	ASSERT(!(dest->ht_flags & HTABLE_VLP));
2082 	ASSERT(!(src->ht_flags & HTABLE_VLP));
2083 	ASSERT(!(src->ht_flags & HTABLE_SHARED_PFN));
2084 	ASSERT(!(dest->ht_flags & HTABLE_SHARED_PFN));
2085 
2086 	/*
2087 	 * Acquire access to the CPU pagetable window for the destination.
2088 	 */
2089 	dst_va = (caddr_t)x86pte_access_pagetable(dest);
2090 	if (kpm_enable) {
2091 		src_va = (caddr_t)x86pte_access_pagetable(src);
2092 	} else {
2093 		hci = CPU->cpu_hat_info;
2094 
2095 		/*
2096 		 * Finish defining the src pagetable mapping
2097 		 */
2098 		src_va = dst_va + MMU_PAGESIZE;
2099 		X86PTE_REMAP(src_va, hci->hci_kernel_pte, 1, 0, src->ht_pfn);
2100 	}
2101 
2102 	/*
2103 	 * now do the copy
2104 	 */
2105 
2106 	dst_va += entry << mmu.pte_size_shift;
2107 	src_va += entry << mmu.pte_size_shift;
2108 	size = count << mmu.pte_size_shift;
2109 	bcopy(src_va, dst_va, size);
2110 
2111 	x86pte_release_pagetable(dest);
2112 }
2113 
2114 /*
2115  * Zero page table entries - Note this doesn't use atomic stores!
2116  */
2117 void
2118 x86pte_zero(htable_t *dest, uint_t entry, uint_t count)
2119 {
2120 	caddr_t dst_va;
2121 	x86pte_t *p;
2122 	x86pte32_t *p32;
2123 	size_t size;
2124 	extern void hat_pte_zero(void *, size_t);
2125 
2126 	/*
2127 	 * Map in the page table to be zeroed.
2128 	 */
2129 	ASSERT(!(dest->ht_flags & HTABLE_SHARED_PFN));
2130 	ASSERT(!(dest->ht_flags & HTABLE_VLP));
2131 	dst_va = (caddr_t)x86pte_access_pagetable(dest);
2132 	dst_va += entry << mmu.pte_size_shift;
2133 	size = count << mmu.pte_size_shift;
2134 	if (x86_feature & X86_SSE2) {
2135 		hat_pte_zero(dst_va, size);
2136 	} else if (khat_running) {
2137 		bzero(dst_va, size);
2138 	} else {
2139 		/*
2140 		 * Can't just use bzero during boot because it checks the
2141 		 * address against kernelbase. Instead just use a zero loop.
2142 		 */
2143 		if (mmu.pae_hat) {
2144 			p = (x86pte_t *)dst_va;
2145 			while (count-- > 0)
2146 				*p++ = 0;
2147 		} else {
2148 			p32 = (x86pte32_t *)dst_va;
2149 			while (count-- > 0)
2150 				*p32++ = 0;
2151 		}
2152 	}
2153 	x86pte_release_pagetable(dest);
2154 }
2155 
2156 /*
2157  * Called to ensure that all pagetables are in the system dump
2158  */
2159 void
2160 hat_dump(void)
2161 {
2162 	hat_t *hat;
2163 	uint_t h;
2164 	htable_t *ht;
2165 	int count;
2166 
2167 	/*
2168 	 * kas.a_hat is the head of the circular list, but not an element of
2169 	 * the list. Once we pass kas.a_hat->hat_next a second time, we
2170 	 * know we've iterated through every hat structure.
2171 	 */
2172 	for (hat = kas.a_hat, count = 0; hat != kas.a_hat->hat_next ||
2173 	    count++ == 0; hat = hat->hat_next) {
2174 		for (h = 0; h < hat->hat_num_hash; ++h) {
2175 			for (ht = hat->hat_ht_hash[h]; ht; ht = ht->ht_next) {
2176 				if ((ht->ht_flags & HTABLE_VLP) == 0) {
2177 					dump_page(ht->ht_pfn);
2178 				}
2179 			}
2180 		}
2181 	}
2182 }
2183