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