xref: /titanic_41/usr/src/uts/i86pc/vm/htable.c (revision 70025d765b044c6d8594bb965a2247a61e991a99)
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 			ATOMIC_LOAD64((x86pte_t *)pte_ptr, found_pte);
1308 		} else {
1309 			found_pte = *(x86pte32_t *)pte_ptr;
1310 		}
1311 	}
1312 	x86pte_release_pagetable(ht);
1313 
1314 #if defined(__amd64)
1315 	/*
1316 	 * deal with VA hole on amd64
1317 	 */
1318 	if (l == mmu.max_level && va >= mmu.hole_start && va <= mmu.hole_end)
1319 		va = mmu.hole_end + va - mmu.hole_start;
1320 #endif /* __amd64 */
1321 
1322 	*vap = va;
1323 	return (found_pte);
1324 }
1325 
1326 /*
1327  * Find the address and htable for the first populated translation at or
1328  * above the given virtual address.  The caller may also specify an upper
1329  * limit to the address range to search.  Uses level information to quickly
1330  * skip unpopulated sections of virtual address spaces.
1331  *
1332  * If not found returns NULL. When found, returns the htable and virt addr
1333  * and has a hold on the htable.
1334  */
1335 x86pte_t
1336 htable_walk(
1337 	struct hat *hat,
1338 	htable_t **htp,
1339 	uintptr_t *vaddr,
1340 	uintptr_t eaddr)
1341 {
1342 	uintptr_t va = *vaddr;
1343 	htable_t *ht;
1344 	htable_t *prev = *htp;
1345 	level_t l;
1346 	level_t max_mapped_level;
1347 	x86pte_t pte;
1348 
1349 	ASSERT(eaddr > va);
1350 
1351 	/*
1352 	 * If this is a user address, then we know we need not look beyond
1353 	 * kernelbase.
1354 	 */
1355 	ASSERT(hat == kas.a_hat || eaddr <= kernelbase ||
1356 	    eaddr == HTABLE_WALK_TO_END);
1357 	if (hat != kas.a_hat && eaddr == HTABLE_WALK_TO_END)
1358 		eaddr = kernelbase;
1359 
1360 	/*
1361 	 * If we're coming in with a previous page table, search it first
1362 	 * without doing an htable_lookup(), this should be frequent.
1363 	 */
1364 	if (prev) {
1365 		ASSERT(prev->ht_busy > 0);
1366 		ASSERT(prev->ht_vaddr <= va);
1367 		l = prev->ht_level;
1368 		if (va <= HTABLE_LAST_PAGE(prev)) {
1369 			pte = htable_scan(prev, &va, eaddr);
1370 
1371 			if (PTE_ISPAGE(pte, l)) {
1372 				*vaddr = va;
1373 				*htp = prev;
1374 				return (pte);
1375 			}
1376 		}
1377 
1378 		/*
1379 		 * We found nothing in the htable provided by the caller,
1380 		 * so fall through and do the full search
1381 		 */
1382 		htable_release(prev);
1383 	}
1384 
1385 	/*
1386 	 * Find the level of the largest pagesize used by this HAT.
1387 	 */
1388 	max_mapped_level = 0;
1389 	for (l = 1; l <= mmu.max_page_level; ++l)
1390 		if (hat->hat_pages_mapped[l] != 0)
1391 			max_mapped_level = l;
1392 
1393 	while (va < eaddr && va >= *vaddr) {
1394 		ASSERT(!IN_VA_HOLE(va));
1395 
1396 		/*
1397 		 *  Find lowest table with any entry for given address.
1398 		 */
1399 		for (l = 0; l <= TOP_LEVEL(hat); ++l) {
1400 			ht = htable_lookup(hat, va, l);
1401 			if (ht != NULL) {
1402 				pte = htable_scan(ht, &va, eaddr);
1403 				if (PTE_ISPAGE(pte, l)) {
1404 					*vaddr = va;
1405 					*htp = ht;
1406 					return (pte);
1407 				}
1408 				htable_release(ht);
1409 				break;
1410 			}
1411 
1412 			/*
1413 			 * The ht is never NULL at the top level since
1414 			 * the top level htable is created in hat_alloc().
1415 			 */
1416 			ASSERT(l < TOP_LEVEL(hat));
1417 
1418 			/*
1419 			 * No htable covers the address. If there is no
1420 			 * larger page size that could cover it, we
1421 			 * skip to the start of the next page table.
1422 			 */
1423 			if (l >= max_mapped_level) {
1424 				va = NEXT_ENTRY_VA(va, l + 1);
1425 				break;
1426 			}
1427 		}
1428 	}
1429 
1430 	*vaddr = 0;
1431 	*htp = NULL;
1432 	return (0);
1433 }
1434 
1435 /*
1436  * Find the htable and page table entry index of the given virtual address
1437  * with pagesize at or below given level.
1438  * If not found returns NULL. When found, returns the htable, sets
1439  * entry, and has a hold on the htable.
1440  */
1441 htable_t *
1442 htable_getpte(
1443 	struct hat *hat,
1444 	uintptr_t vaddr,
1445 	uint_t *entry,
1446 	x86pte_t *pte,
1447 	level_t level)
1448 {
1449 	htable_t	*ht;
1450 	level_t		l;
1451 	uint_t		e;
1452 
1453 	ASSERT(level <= mmu.max_page_level);
1454 
1455 	for (l = 0; l <= level; ++l) {
1456 		ht = htable_lookup(hat, vaddr, l);
1457 		if (ht == NULL)
1458 			continue;
1459 		e = htable_va2entry(vaddr, ht);
1460 		if (entry != NULL)
1461 			*entry = e;
1462 		if (pte != NULL)
1463 			*pte = x86pte_get(ht, e);
1464 		return (ht);
1465 	}
1466 	return (NULL);
1467 }
1468 
1469 /*
1470  * Find the htable and page table entry index of the given virtual address.
1471  * There must be a valid page mapped at the given address.
1472  * If not found returns NULL. When found, returns the htable, sets
1473  * entry, and has a hold on the htable.
1474  */
1475 htable_t *
1476 htable_getpage(struct hat *hat, uintptr_t vaddr, uint_t *entry)
1477 {
1478 	htable_t	*ht;
1479 	uint_t		e;
1480 	x86pte_t	pte;
1481 
1482 	ht = htable_getpte(hat, vaddr, &e, &pte, mmu.max_page_level);
1483 	if (ht == NULL)
1484 		return (NULL);
1485 
1486 	if (entry)
1487 		*entry = e;
1488 
1489 	if (PTE_ISPAGE(pte, ht->ht_level))
1490 		return (ht);
1491 	htable_release(ht);
1492 	return (NULL);
1493 }
1494 
1495 
1496 void
1497 htable_init()
1498 {
1499 	/*
1500 	 * To save on kernel VA usage, we avoid debug information in 32 bit
1501 	 * kernels.
1502 	 */
1503 #if defined(__amd64)
1504 	int	kmem_flags = KMC_NOHASH;
1505 #elif defined(__i386)
1506 	int	kmem_flags = KMC_NOHASH | KMC_NODEBUG;
1507 #endif
1508 
1509 	/*
1510 	 * initialize kmem caches
1511 	 */
1512 	htable_cache = kmem_cache_create("htable_t",
1513 	    sizeof (htable_t), 0, NULL, NULL,
1514 	    htable_reap, NULL, hat_memload_arena, kmem_flags);
1515 }
1516 
1517 /*
1518  * get the pte index for the virtual address in the given htable's pagetable
1519  */
1520 uint_t
1521 htable_va2entry(uintptr_t va, htable_t *ht)
1522 {
1523 	level_t	l = ht->ht_level;
1524 
1525 	ASSERT(va >= ht->ht_vaddr);
1526 	ASSERT(va <= HTABLE_LAST_PAGE(ht));
1527 	return ((va >> LEVEL_SHIFT(l)) & (ht->ht_num_ptes - 1));
1528 }
1529 
1530 /*
1531  * Given an htable and the index of a pte in it, return the virtual address
1532  * of the page.
1533  */
1534 uintptr_t
1535 htable_e2va(htable_t *ht, uint_t entry)
1536 {
1537 	level_t	l = ht->ht_level;
1538 	uintptr_t va;
1539 
1540 	ASSERT(entry < ht->ht_num_ptes);
1541 	va = ht->ht_vaddr + ((uintptr_t)entry << LEVEL_SHIFT(l));
1542 
1543 	/*
1544 	 * Need to skip over any VA hole in top level table
1545 	 */
1546 #if defined(__amd64)
1547 	if (ht->ht_level == mmu.max_level && va >= mmu.hole_start)
1548 		va += ((mmu.hole_end - mmu.hole_start) + 1);
1549 #endif
1550 
1551 	return (va);
1552 }
1553 
1554 /*
1555  * The code uses compare and swap instructions to read/write PTE's to
1556  * avoid atomicity problems, since PTEs can be 8 bytes on 32 bit systems.
1557  * Again this can be optimized on 64 bit systems, since aligned load/store
1558  * will naturally be atomic.
1559  *
1560  * The combination of using kpreempt_disable()/_enable() and the hci_mutex
1561  * are used to ensure that an interrupt won't overwrite a temporary mapping
1562  * while it's in use. If an interrupt thread tries to access a PTE, it will
1563  * yield briefly back to the pinned thread which holds the cpu's hci_mutex.
1564  */
1565 
1566 static struct hat_cpu_info init_hci;	/* used for cpu 0 */
1567 
1568 /*
1569  * Initialize a CPU private window for mapping page tables.
1570  * There will be 3 total pages of addressing needed:
1571  *
1572  *	1 for r/w access to pagetables
1573  *	1 for r access when copying pagetables (hat_alloc)
1574  *	1 that will map the PTEs for the 1st 2, so we can access them quickly
1575  *
1576  * We use vmem_xalloc() to get a correct alignment so that only one
1577  * hat_mempte_setup() is needed.
1578  */
1579 void
1580 x86pte_cpu_init(cpu_t *cpu, void *pages)
1581 {
1582 	struct hat_cpu_info *hci;
1583 	caddr_t va;
1584 
1585 	/*
1586 	 * We can't use kmem_alloc/vmem_alloc for the 1st CPU, as this is
1587 	 * called before we've activated our own HAT
1588 	 */
1589 	if (pages != NULL) {
1590 		hci = &init_hci;
1591 		va = pages;
1592 	} else {
1593 		hci = kmem_alloc(sizeof (struct hat_cpu_info), KM_SLEEP);
1594 		va = vmem_xalloc(heap_arena, 3 * MMU_PAGESIZE, MMU_PAGESIZE, 0,
1595 		    LEVEL_SIZE(1), NULL, NULL, VM_SLEEP);
1596 	}
1597 	mutex_init(&hci->hci_mutex, NULL, MUTEX_DEFAULT, NULL);
1598 
1599 	/*
1600 	 * If we are using segkpm, then there is no need for any of the
1601 	 * mempte support.  We can access the desired memory through a kpm
1602 	 * mapping rather than setting up a temporary mempte mapping.
1603 	 */
1604 	if (kpm_enable == 0) {
1605 		hci->hci_mapped_pfn = PFN_INVALID;
1606 
1607 		hci->hci_kernel_pte =
1608 		    hat_mempte_kern_setup(va, va + (2 * MMU_PAGESIZE));
1609 		hci->hci_pagetable_va = (void *)va;
1610 	}
1611 
1612 	cpu->cpu_hat_info = hci;
1613 }
1614 
1615 /*
1616  * Macro to establish temporary mappings for x86pte_XXX routines.
1617  */
1618 #define	X86PTE_REMAP(addr, pte, index, perm, pfn)	{		\
1619 		x86pte_t t;						\
1620 									\
1621 		t = MAKEPTE((pfn), 0) | (perm) | mmu.pt_global | mmu.pt_nx;\
1622 		if (mmu.pae_hat)					\
1623 			pte[index] = t;					\
1624 		else							\
1625 			((x86pte32_t *)(pte))[index] = t;		\
1626 		mmu_tlbflush_entry((caddr_t)(addr));			\
1627 }
1628 
1629 /*
1630  * Disable preemption and establish a mapping to the pagetable with the
1631  * given pfn. This is optimized for there case where it's the same
1632  * pfn as we last used referenced from this CPU.
1633  */
1634 static x86pte_t *
1635 x86pte_access_pagetable(htable_t *ht)
1636 {
1637 	pfn_t pfn;
1638 	struct hat_cpu_info *hci;
1639 
1640 	/*
1641 	 * VLP pagetables are contained in the hat_t
1642 	 */
1643 	if (ht->ht_flags & HTABLE_VLP)
1644 		return (ht->ht_hat->hat_vlp_ptes);
1645 
1646 	/*
1647 	 * During early boot, use hat_boot_remap() of a page table adddress.
1648 	 */
1649 	pfn = ht->ht_pfn;
1650 	ASSERT(pfn != PFN_INVALID);
1651 	if (kpm_enable)
1652 		return ((x86pte_t *)hat_kpm_pfn2va(pfn));
1653 
1654 	if (!khat_running) {
1655 		(void) hat_boot_remap(ptable_va, pfn);
1656 		return ((x86pte_t *)ptable_va);
1657 	}
1658 
1659 	/*
1660 	 * Normally, disable preemption and grab the CPU's hci_mutex
1661 	 */
1662 	kpreempt_disable();
1663 	hci = CPU->cpu_hat_info;
1664 	ASSERT(hci != NULL);
1665 	mutex_enter(&hci->hci_mutex);
1666 	if (hci->hci_mapped_pfn != pfn) {
1667 		/*
1668 		 * The current mapping doesn't already point to this page.
1669 		 * Update the CPU specific pagetable mapping to map the pfn.
1670 		 */
1671 		X86PTE_REMAP(hci->hci_pagetable_va, hci->hci_kernel_pte, 0,
1672 		    PT_WRITABLE, pfn);
1673 		hci->hci_mapped_pfn = pfn;
1674 	}
1675 	return (hci->hci_pagetable_va);
1676 }
1677 
1678 /*
1679  * Release access to a page table.
1680  */
1681 static void
1682 x86pte_release_pagetable(htable_t *ht)
1683 {
1684 	struct hat_cpu_info *hci;
1685 
1686 	if (kpm_enable)
1687 		return;
1688 
1689 	/*
1690 	 * nothing to do for VLP htables
1691 	 */
1692 	if (ht->ht_flags & HTABLE_VLP)
1693 		return;
1694 
1695 	/*
1696 	 * During boot-up hat_kern_setup(), erase the boot loader remapping.
1697 	 */
1698 	if (!khat_running) {
1699 		hat_boot_demap(ptable_va);
1700 		return;
1701 	}
1702 
1703 	/*
1704 	 * Normal Operation: drop the CPU's hci_mutex and restore preemption
1705 	 */
1706 	hci = CPU->cpu_hat_info;
1707 	ASSERT(hci != NULL);
1708 	mutex_exit(&hci->hci_mutex);
1709 	kpreempt_enable();
1710 }
1711 
1712 /*
1713  * Atomic retrieval of a pagetable entry
1714  */
1715 x86pte_t
1716 x86pte_get(htable_t *ht, uint_t entry)
1717 {
1718 	x86pte_t	pte;
1719 	x86pte32_t	*pte32p;
1720 	x86pte_t	*ptep;
1721 
1722 	/*
1723 	 * Be careful that loading PAE entries in 32 bit kernel is atomic.
1724 	 */
1725 	ptep = x86pte_access_pagetable(ht);
1726 	if (mmu.pae_hat) {
1727 		ATOMIC_LOAD64(ptep + entry, pte);
1728 	} else {
1729 		pte32p = (x86pte32_t *)ptep;
1730 		pte = pte32p[entry];
1731 	}
1732 	x86pte_release_pagetable(ht);
1733 	return (pte);
1734 }
1735 
1736 /*
1737  * Atomic unconditional set of a page table entry, it returns the previous
1738  * value.
1739  */
1740 x86pte_t
1741 x86pte_set(htable_t *ht, uint_t entry, x86pte_t new, void *ptr)
1742 {
1743 	x86pte_t	old;
1744 	x86pte_t	prev, n;
1745 	x86pte_t	*ptep;
1746 	x86pte32_t	*pte32p;
1747 	x86pte32_t	n32, p32;
1748 
1749 	ASSERT(!(ht->ht_flags & HTABLE_SHARED_PFN));
1750 	if (ptr == NULL) {
1751 		ptep = x86pte_access_pagetable(ht);
1752 		ptep = (void *)((caddr_t)ptep + (entry << mmu.pte_size_shift));
1753 	} else {
1754 		ptep = ptr;
1755 	}
1756 
1757 	if (mmu.pae_hat) {
1758 		for (;;) {
1759 			prev = *ptep;
1760 			n = new;
1761 			/*
1762 			 * prevent potential data loss by preserving the MOD
1763 			 * bit if set in the current PTE and the pfns are the
1764 			 * same. For example, segmap can reissue a read-only
1765 			 * hat_memload on top of a dirty page.
1766 			 */
1767 			if (PTE_ISVALID(prev) && PTE2PFN(prev, ht->ht_level) ==
1768 			    PTE2PFN(n, ht->ht_level)) {
1769 				n |= prev & (PT_REF | PT_MOD);
1770 			}
1771 			if (prev == n) {
1772 				old = new;
1773 				break;
1774 			}
1775 			old = cas64(ptep, prev, n);
1776 			if (old == prev)
1777 				break;
1778 		}
1779 	} else {
1780 		pte32p = (x86pte32_t *)ptep;
1781 		for (;;) {
1782 			p32 = *pte32p;
1783 			n32 = new;
1784 			if (PTE_ISVALID(p32) && PTE2PFN(p32, ht->ht_level) ==
1785 			    PTE2PFN(n32, ht->ht_level)) {
1786 				n32 |= p32 & (PT_REF | PT_MOD);
1787 			}
1788 			if (p32 == n32) {
1789 				old = new;
1790 				break;
1791 			}
1792 			old = cas32(pte32p, p32, n32);
1793 			if (old == p32)
1794 				break;
1795 		}
1796 	}
1797 	if (ptr == NULL)
1798 		x86pte_release_pagetable(ht);
1799 	return (old);
1800 }
1801 
1802 /*
1803  * Atomic compare and swap of a page table entry.
1804  */
1805 static x86pte_t
1806 x86pte_cas(htable_t *ht, uint_t entry, x86pte_t old, x86pte_t new)
1807 {
1808 	x86pte_t	pte;
1809 	x86pte_t	*ptep;
1810 	x86pte32_t	pte32, o32, n32;
1811 	x86pte32_t	*pte32p;
1812 
1813 	ASSERT(!(ht->ht_flags & HTABLE_SHARED_PFN));
1814 	ptep = x86pte_access_pagetable(ht);
1815 	if (mmu.pae_hat) {
1816 		pte = cas64(&ptep[entry], old, new);
1817 	} else {
1818 		o32 = old;
1819 		n32 = new;
1820 		pte32p = (x86pte32_t *)ptep;
1821 		pte32 = cas32(&pte32p[entry], o32, n32);
1822 		pte = pte32;
1823 	}
1824 	x86pte_release_pagetable(ht);
1825 
1826 	return (pte);
1827 }
1828 
1829 /*
1830  * data structure for cross call information
1831  */
1832 typedef struct xcall_info {
1833 	x86pte_t	xi_pte;
1834 	x86pte_t	xi_old;
1835 	x86pte_t	*xi_pteptr;
1836 	pfn_t		xi_pfn;
1837 	processorid_t	xi_cpuid;
1838 	level_t		xi_level;
1839 	xc_func_t	xi_func;
1840 } xcall_info_t;
1841 
1842 /*
1843  * Cross call service function to atomically invalidate a PTE and flush TLBs
1844  */
1845 /*ARGSUSED*/
1846 static int
1847 x86pte_inval_func(xc_arg_t a1, xc_arg_t a2, xc_arg_t a3)
1848 {
1849 	xcall_info_t	*xi = (xcall_info_t *)a1;
1850 	caddr_t		addr = (caddr_t)a2;
1851 
1852 	/*
1853 	 * Only the initiating cpu invalidates the page table entry.
1854 	 * It returns the previous PTE value to the caller.
1855 	 */
1856 	if (CPU->cpu_id == xi->xi_cpuid) {
1857 		x86pte_t	*ptep = xi->xi_pteptr;
1858 		pfn_t		pfn = xi->xi_pfn;
1859 		level_t		level = xi->xi_level;
1860 		x86pte_t	old;
1861 		x86pte_t	prev;
1862 		x86pte32_t	*pte32p;
1863 		x86pte32_t	p32;
1864 
1865 		if (mmu.pae_hat) {
1866 			for (;;) {
1867 				prev = *ptep;
1868 				if (PTE2PFN(prev, level) != pfn)
1869 					break;
1870 				old = cas64(ptep, prev, 0);
1871 				if (old == prev)
1872 					break;
1873 			}
1874 		} else {
1875 			pte32p = (x86pte32_t *)ptep;
1876 			for (;;) {
1877 				p32 = *pte32p;
1878 				if (PTE2PFN(p32, level) != pfn)
1879 					break;
1880 				old = cas32(pte32p, p32, 0);
1881 				if (old == p32)
1882 					break;
1883 			}
1884 			prev = p32;
1885 		}
1886 		xi->xi_pte = prev;
1887 	}
1888 
1889 	/*
1890 	 * For a normal address, we just flush one page mapping
1891 	 * Otherwise reload cr3 to effect a complete TLB flush.
1892 	 *
1893 	 * Note we don't reload VLP pte's -- this assume we never have a
1894 	 * large page size at VLP_LEVEL for VLP processes.
1895 	 */
1896 	if ((uintptr_t)addr != DEMAP_ALL_ADDR) {
1897 		mmu_tlbflush_entry(addr);
1898 	} else {
1899 		reload_cr3();
1900 	}
1901 	return (0);
1902 }
1903 
1904 /*
1905  * Cross call service function to atomically change a PTE and flush TLBs
1906  */
1907 /*ARGSUSED*/
1908 static int
1909 x86pte_update_func(xc_arg_t a1, xc_arg_t a2, xc_arg_t a3)
1910 {
1911 	xcall_info_t	*xi = (xcall_info_t *)a1;
1912 	caddr_t		addr = (caddr_t)a2;
1913 
1914 	/*
1915 	 * Only the initiating cpu changes the page table entry.
1916 	 * It returns the previous PTE value to the caller.
1917 	 */
1918 	if (CPU->cpu_id == xi->xi_cpuid) {
1919 		x86pte_t	*ptep = xi->xi_pteptr;
1920 		x86pte_t	new = xi->xi_pte;
1921 		x86pte_t	old = xi->xi_old;
1922 		x86pte_t	prev;
1923 
1924 		if (mmu.pae_hat) {
1925 			prev = cas64(ptep, old, new);
1926 		} else {
1927 			x86pte32_t o32 = old;
1928 			x86pte32_t n32 = new;
1929 			x86pte32_t *pte32p = (x86pte32_t *)ptep;
1930 			prev = cas32(pte32p, o32, n32);
1931 		}
1932 
1933 		xi->xi_pte = prev;
1934 	}
1935 
1936 	/*
1937 	 * Flush the TLB entry
1938 	 */
1939 	if ((uintptr_t)addr != DEMAP_ALL_ADDR)
1940 		mmu_tlbflush_entry(addr);
1941 	else
1942 		reload_cr3();
1943 	return (0);
1944 }
1945 
1946 /*
1947  * Use cross calls to change a page table entry and invalidate TLBs.
1948  */
1949 void
1950 x86pte_xcall(hat_t *hat, xcall_info_t *xi, uintptr_t addr)
1951 {
1952 	cpuset_t	cpus;
1953 
1954 	/*
1955 	 * Given the current implementation of hat_share(), doing a
1956 	 * hat_pageunload() on a shared page table requries invalidating
1957 	 * all user TLB entries on all CPUs.
1958 	 */
1959 	if (hat->hat_flags & HAT_SHARED) {
1960 		hat = kas.a_hat;
1961 		addr = DEMAP_ALL_ADDR;
1962 	}
1963 
1964 	/*
1965 	 * Use a cross call to do the invalidations.
1966 	 * Note the current CPU always has to be in the cross call CPU set.
1967 	 */
1968 	kpreempt_disable();
1969 	xi->xi_cpuid = CPU->cpu_id;
1970 	CPUSET_ZERO(cpus);
1971 	if (hat == kas.a_hat) {
1972 		CPUSET_OR(cpus, khat_cpuset);
1973 	} else {
1974 		mutex_enter(&hat->hat_switch_mutex);
1975 		CPUSET_OR(cpus, hat->hat_cpus);
1976 		CPUSET_ADD(cpus, CPU->cpu_id);
1977 	}
1978 
1979 	/*
1980 	 * Use a cross call to modify the page table entry and invalidate TLBs.
1981 	 * If we're panic'ing, don't bother with the cross call.
1982 	 * Note the panicstr check isn't bullet proof and the panic system
1983 	 * ought to be made tighter.
1984 	 */
1985 	if (panicstr == NULL)
1986 		xc_wait_sync((xc_arg_t)xi, addr, NULL, X_CALL_HIPRI,
1987 			    cpus, xi->xi_func);
1988 	else
1989 		(void) xi->xi_func((xc_arg_t)xi, (xc_arg_t)addr, NULL);
1990 	if (hat != kas.a_hat)
1991 		mutex_exit(&hat->hat_switch_mutex);
1992 	kpreempt_enable();
1993 }
1994 
1995 /*
1996  * Invalidate a page table entry if it currently maps the given pfn.
1997  * This returns the previous value of the PTE.
1998  */
1999 x86pte_t
2000 x86pte_invalidate_pfn(htable_t *ht, uint_t entry, pfn_t pfn, void *pte_ptr)
2001 {
2002 	xcall_info_t	xi;
2003 	x86pte_t	*ptep;
2004 	hat_t		*hat;
2005 	uintptr_t	addr;
2006 
2007 	ASSERT(!(ht->ht_flags & HTABLE_SHARED_PFN));
2008 	if (pte_ptr != NULL) {
2009 		ptep = pte_ptr;
2010 	} else {
2011 		ptep = x86pte_access_pagetable(ht);
2012 		ptep = (void *)((caddr_t)ptep + (entry << mmu.pte_size_shift));
2013 	}
2014 
2015 	/*
2016 	 * Fill in the structure used by the cross call function to do the
2017 	 * invalidation.
2018 	 */
2019 	xi.xi_pte = 0;
2020 	xi.xi_pteptr = ptep;
2021 	xi.xi_pfn = pfn;
2022 	xi.xi_level = ht->ht_level;
2023 	xi.xi_func = x86pte_inval_func;
2024 	ASSERT(xi.xi_level != VLP_LEVEL);
2025 
2026 	hat = ht->ht_hat;
2027 	addr = htable_e2va(ht, entry);
2028 
2029 	x86pte_xcall(hat, &xi, addr);
2030 
2031 	if (pte_ptr == NULL)
2032 		x86pte_release_pagetable(ht);
2033 	return (xi.xi_pte);
2034 }
2035 
2036 /*
2037  * update a PTE and invalidate any stale TLB entries.
2038  */
2039 x86pte_t
2040 x86pte_update(htable_t *ht, uint_t entry, x86pte_t expected, x86pte_t new)
2041 {
2042 	xcall_info_t	xi;
2043 	x86pte_t	*ptep;
2044 	hat_t		*hat;
2045 	uintptr_t	addr;
2046 
2047 	ASSERT(!(ht->ht_flags & HTABLE_SHARED_PFN));
2048 	ptep = x86pte_access_pagetable(ht);
2049 	ptep = (void *)((caddr_t)ptep + (entry << mmu.pte_size_shift));
2050 
2051 	/*
2052 	 * Fill in the structure used by the cross call function to do the
2053 	 * invalidation.
2054 	 */
2055 	xi.xi_pte = new;
2056 	xi.xi_old = expected;
2057 	xi.xi_pteptr = ptep;
2058 	xi.xi_func = x86pte_update_func;
2059 
2060 	hat = ht->ht_hat;
2061 	addr = htable_e2va(ht, entry);
2062 
2063 	x86pte_xcall(hat, &xi, addr);
2064 
2065 	x86pte_release_pagetable(ht);
2066 	return (xi.xi_pte);
2067 }
2068 
2069 /*
2070  * Copy page tables - this is just a little more complicated than the
2071  * previous routines. Note that it's also not atomic! It also is never
2072  * used for VLP pagetables.
2073  */
2074 void
2075 x86pte_copy(htable_t *src, htable_t *dest, uint_t entry, uint_t count)
2076 {
2077 	struct hat_cpu_info *hci;
2078 	caddr_t	src_va;
2079 	caddr_t dst_va;
2080 	size_t size;
2081 
2082 	ASSERT(khat_running);
2083 	ASSERT(!(dest->ht_flags & HTABLE_VLP));
2084 	ASSERT(!(src->ht_flags & HTABLE_VLP));
2085 	ASSERT(!(src->ht_flags & HTABLE_SHARED_PFN));
2086 	ASSERT(!(dest->ht_flags & HTABLE_SHARED_PFN));
2087 
2088 	/*
2089 	 * Acquire access to the CPU pagetable window for the destination.
2090 	 */
2091 	dst_va = (caddr_t)x86pte_access_pagetable(dest);
2092 	if (kpm_enable) {
2093 		src_va = (caddr_t)x86pte_access_pagetable(src);
2094 	} else {
2095 		hci = CPU->cpu_hat_info;
2096 
2097 		/*
2098 		 * Finish defining the src pagetable mapping
2099 		 */
2100 		src_va = dst_va + MMU_PAGESIZE;
2101 		X86PTE_REMAP(src_va, hci->hci_kernel_pte, 1, 0, src->ht_pfn);
2102 	}
2103 
2104 	/*
2105 	 * now do the copy
2106 	 */
2107 
2108 	dst_va += entry << mmu.pte_size_shift;
2109 	src_va += entry << mmu.pte_size_shift;
2110 	size = count << mmu.pte_size_shift;
2111 	bcopy(src_va, dst_va, size);
2112 
2113 	x86pte_release_pagetable(dest);
2114 }
2115 
2116 /*
2117  * Zero page table entries - Note this doesn't use atomic stores!
2118  */
2119 void
2120 x86pte_zero(htable_t *dest, uint_t entry, uint_t count)
2121 {
2122 	caddr_t dst_va;
2123 	x86pte_t *p;
2124 	x86pte32_t *p32;
2125 	size_t size;
2126 	extern void hat_pte_zero(void *, size_t);
2127 
2128 	/*
2129 	 * Map in the page table to be zeroed.
2130 	 */
2131 	ASSERT(!(dest->ht_flags & HTABLE_SHARED_PFN));
2132 	ASSERT(!(dest->ht_flags & HTABLE_VLP));
2133 	dst_va = (caddr_t)x86pte_access_pagetable(dest);
2134 	dst_va += entry << mmu.pte_size_shift;
2135 	size = count << mmu.pte_size_shift;
2136 	if (x86_feature & X86_SSE2) {
2137 		hat_pte_zero(dst_va, size);
2138 	} else if (khat_running) {
2139 		bzero(dst_va, size);
2140 	} else {
2141 		/*
2142 		 * Can't just use bzero during boot because it checks the
2143 		 * address against kernelbase. Instead just use a zero loop.
2144 		 */
2145 		if (mmu.pae_hat) {
2146 			p = (x86pte_t *)dst_va;
2147 			while (count-- > 0)
2148 				*p++ = 0;
2149 		} else {
2150 			p32 = (x86pte32_t *)dst_va;
2151 			while (count-- > 0)
2152 				*p32++ = 0;
2153 		}
2154 	}
2155 	x86pte_release_pagetable(dest);
2156 }
2157 
2158 /*
2159  * Called to ensure that all pagetables are in the system dump
2160  */
2161 void
2162 hat_dump(void)
2163 {
2164 	hat_t *hat;
2165 	uint_t h;
2166 	htable_t *ht;
2167 	int count;
2168 
2169 	/*
2170 	 * kas.a_hat is the head of the circular list, but not an element of
2171 	 * the list. Once we pass kas.a_hat->hat_next a second time, we
2172 	 * know we've iterated through every hat structure.
2173 	 */
2174 	for (hat = kas.a_hat, count = 0; hat != kas.a_hat->hat_next ||
2175 	    count++ == 0; hat = hat->hat_next) {
2176 		for (h = 0; h < hat->hat_num_hash; ++h) {
2177 			for (ht = hat->hat_ht_hash[h]; ht; ht = ht->ht_next) {
2178 				if ((ht->ht_flags & HTABLE_VLP) == 0) {
2179 					dump_page(ht->ht_pfn);
2180 				}
2181 			}
2182 		}
2183 	}
2184 }
2185