xref: /titanic_44/usr/src/uts/i86pc/vm/hat_i86.c (revision b8a1bc93abd7f91503ab2661db0af6e85108b50b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * Copyright (c) 2010, Intel Corporation.
27  * All rights reserved.
28  */
29 
30 
31 /*
32  * VM - Hardware Address Translation management for i386 and amd64
33  *
34  * Implementation of the interfaces described in <common/vm/hat.h>
35  *
36  * Nearly all the details of how the hardware is managed should not be
37  * visible outside this layer except for misc. machine specific functions
38  * that work in conjunction with this code.
39  *
40  * Routines used only inside of i86pc/vm start with hati_ for HAT Internal.
41  */
42 
43 #include <sys/machparam.h>
44 #include <sys/machsystm.h>
45 #include <sys/mman.h>
46 #include <sys/types.h>
47 #include <sys/systm.h>
48 #include <sys/cpuvar.h>
49 #include <sys/thread.h>
50 #include <sys/proc.h>
51 #include <sys/cpu.h>
52 #include <sys/kmem.h>
53 #include <sys/disp.h>
54 #include <sys/shm.h>
55 #include <sys/sysmacros.h>
56 #include <sys/machparam.h>
57 #include <sys/vmem.h>
58 #include <sys/vmsystm.h>
59 #include <sys/promif.h>
60 #include <sys/var.h>
61 #include <sys/x86_archext.h>
62 #include <sys/atomic.h>
63 #include <sys/bitmap.h>
64 #include <sys/controlregs.h>
65 #include <sys/bootconf.h>
66 #include <sys/bootsvcs.h>
67 #include <sys/bootinfo.h>
68 #include <sys/archsystm.h>
69 
70 #include <vm/seg_kmem.h>
71 #include <vm/hat_i86.h>
72 #include <vm/as.h>
73 #include <vm/seg.h>
74 #include <vm/page.h>
75 #include <vm/seg_kp.h>
76 #include <vm/seg_kpm.h>
77 #include <vm/vm_dep.h>
78 #ifdef __xpv
79 #include <sys/hypervisor.h>
80 #endif
81 #include <vm/kboot_mmu.h>
82 #include <vm/seg_spt.h>
83 
84 #include <sys/cmn_err.h>
85 
86 /*
87  * Basic parameters for hat operation.
88  */
89 struct hat_mmu_info mmu;
90 
91 /*
92  * The page that is the kernel's top level pagetable.
93  *
94  * For 32 bit PAE support on i86pc, the kernel hat will use the 1st 4 entries
95  * on this 4K page for its top level page table. The remaining groups of
96  * 4 entries are used for per processor copies of user VLP pagetables for
97  * running threads.  See hat_switch() and reload_pae32() for details.
98  *
99  * vlp_page[0..3] - level==2 PTEs for kernel HAT
100  * vlp_page[4..7] - level==2 PTEs for user thread on cpu 0
101  * vlp_page[8..11]  - level==2 PTE for user thread on cpu 1
102  * etc...
103  */
104 static x86pte_t *vlp_page;
105 
106 /*
107  * forward declaration of internal utility routines
108  */
109 static x86pte_t hati_update_pte(htable_t *ht, uint_t entry, x86pte_t expected,
110 	x86pte_t new);
111 
112 /*
113  * The kernel address space exists in all HATs. To implement this the
114  * kernel reserves a fixed number of entries in the topmost level(s) of page
115  * tables. The values are setup during startup and then copied to every user
116  * hat created by hat_alloc(). This means that kernelbase must be:
117  *
118  *	  4Meg aligned for 32 bit kernels
119  *	512Gig aligned for x86_64 64 bit kernel
120  *
121  * The hat_kernel_range_ts describe what needs to be copied from kernel hat
122  * to each user hat.
123  */
124 typedef struct hat_kernel_range {
125 	level_t		hkr_level;
126 	uintptr_t	hkr_start_va;
127 	uintptr_t	hkr_end_va;	/* zero means to end of memory */
128 } hat_kernel_range_t;
129 #define	NUM_KERNEL_RANGE 2
130 static hat_kernel_range_t kernel_ranges[NUM_KERNEL_RANGE];
131 static int num_kernel_ranges;
132 
133 uint_t use_boot_reserve = 1;	/* cleared after early boot process */
134 uint_t can_steal_post_boot = 0;	/* set late in boot to enable stealing */
135 
136 /*
137  * enable_1gpg: controls 1g page support for user applications.
138  * By default, 1g pages are exported to user applications. enable_1gpg can
139  * be set to 0 to not export.
140  */
141 int	enable_1gpg = 1;
142 
143 /*
144  * AMD shanghai processors provide better management of 1gb ptes in its tlb.
145  * By default, 1g page support will be disabled for pre-shanghai AMD
146  * processors that don't have optimal tlb support for the 1g page size.
147  * chk_optimal_1gtlb can be set to 0 to force 1g page support on sub-optimal
148  * processors.
149  */
150 int	chk_optimal_1gtlb = 1;
151 
152 
153 #ifdef DEBUG
154 uint_t	map1gcnt;
155 #endif
156 
157 
158 /*
159  * A cpuset for all cpus. This is used for kernel address cross calls, since
160  * the kernel addresses apply to all cpus.
161  */
162 cpuset_t khat_cpuset;
163 
164 /*
165  * management stuff for hat structures
166  */
167 kmutex_t	hat_list_lock;
168 kcondvar_t	hat_list_cv;
169 kmem_cache_t	*hat_cache;
170 kmem_cache_t	*hat_hash_cache;
171 kmem_cache_t	*vlp_hash_cache;
172 
173 /*
174  * Simple statistics
175  */
176 struct hatstats hatstat;
177 
178 /*
179  * Some earlier hypervisor versions do not emulate cmpxchg of PTEs
180  * correctly.  For such hypervisors we must set PT_USER for kernel
181  * entries ourselves (normally the emulation would set PT_USER for
182  * kernel entries and PT_USER|PT_GLOBAL for user entries).  pt_kern is
183  * thus set appropriately.  Note that dboot/kbm is OK, as only the full
184  * HAT uses cmpxchg() and the other paths (hypercall etc.) were never
185  * incorrect.
186  */
187 int pt_kern;
188 
189 /*
190  * useful stuff for atomic access/clearing/setting REF/MOD/RO bits in page_t's.
191  */
192 extern void atomic_orb(uchar_t *addr, uchar_t val);
193 extern void atomic_andb(uchar_t *addr, uchar_t val);
194 
195 #ifndef __xpv
196 extern pfn_t memseg_get_start(struct memseg *);
197 #endif
198 
199 #define	PP_GETRM(pp, rmmask)    (pp->p_nrm & rmmask)
200 #define	PP_ISMOD(pp)		PP_GETRM(pp, P_MOD)
201 #define	PP_ISREF(pp)		PP_GETRM(pp, P_REF)
202 #define	PP_ISRO(pp)		PP_GETRM(pp, P_RO)
203 
204 #define	PP_SETRM(pp, rm)	atomic_orb(&(pp->p_nrm), rm)
205 #define	PP_SETMOD(pp)		PP_SETRM(pp, P_MOD)
206 #define	PP_SETREF(pp)		PP_SETRM(pp, P_REF)
207 #define	PP_SETRO(pp)		PP_SETRM(pp, P_RO)
208 
209 #define	PP_CLRRM(pp, rm)	atomic_andb(&(pp->p_nrm), ~(rm))
210 #define	PP_CLRMOD(pp)   	PP_CLRRM(pp, P_MOD)
211 #define	PP_CLRREF(pp)   	PP_CLRRM(pp, P_REF)
212 #define	PP_CLRRO(pp)    	PP_CLRRM(pp, P_RO)
213 #define	PP_CLRALL(pp)		PP_CLRRM(pp, P_MOD | P_REF | P_RO)
214 
215 /*
216  * kmem cache constructor for struct hat
217  */
218 /*ARGSUSED*/
219 static int
220 hati_constructor(void *buf, void *handle, int kmflags)
221 {
222 	hat_t	*hat = buf;
223 
224 	mutex_init(&hat->hat_mutex, NULL, MUTEX_DEFAULT, NULL);
225 	bzero(hat->hat_pages_mapped,
226 	    sizeof (pgcnt_t) * (mmu.max_page_level + 1));
227 	hat->hat_ism_pgcnt = 0;
228 	hat->hat_stats = 0;
229 	hat->hat_flags = 0;
230 	CPUSET_ZERO(hat->hat_cpus);
231 	hat->hat_htable = NULL;
232 	hat->hat_ht_hash = NULL;
233 	return (0);
234 }
235 
236 /*
237  * Allocate a hat structure for as. We also create the top level
238  * htable and initialize it to contain the kernel hat entries.
239  */
240 hat_t *
241 hat_alloc(struct as *as)
242 {
243 	hat_t			*hat;
244 	htable_t		*ht;	/* top level htable */
245 	uint_t			use_vlp;
246 	uint_t			r;
247 	hat_kernel_range_t	*rp;
248 	uintptr_t		va;
249 	uintptr_t		eva;
250 	uint_t			start;
251 	uint_t			cnt;
252 	htable_t		*src;
253 
254 	/*
255 	 * Once we start creating user process HATs we can enable
256 	 * the htable_steal() code.
257 	 */
258 	if (can_steal_post_boot == 0)
259 		can_steal_post_boot = 1;
260 
261 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
262 	hat = kmem_cache_alloc(hat_cache, KM_SLEEP);
263 	hat->hat_as = as;
264 	mutex_init(&hat->hat_mutex, NULL, MUTEX_DEFAULT, NULL);
265 	ASSERT(hat->hat_flags == 0);
266 
267 #if defined(__xpv)
268 	/*
269 	 * No VLP stuff on the hypervisor due to the 64-bit split top level
270 	 * page tables.  On 32-bit it's not needed as the hypervisor takes
271 	 * care of copying the top level PTEs to a below 4Gig page.
272 	 */
273 	use_vlp = 0;
274 #else	/* __xpv */
275 	/* 32 bit processes uses a VLP style hat when running with PAE */
276 #if defined(__amd64)
277 	use_vlp = (ttoproc(curthread)->p_model == DATAMODEL_ILP32);
278 #elif defined(__i386)
279 	use_vlp = mmu.pae_hat;
280 #endif
281 #endif	/* __xpv */
282 	if (use_vlp) {
283 		hat->hat_flags = HAT_VLP;
284 		bzero(hat->hat_vlp_ptes, VLP_SIZE);
285 	}
286 
287 	/*
288 	 * Allocate the htable hash
289 	 */
290 	if ((hat->hat_flags & HAT_VLP)) {
291 		hat->hat_num_hash = mmu.vlp_hash_cnt;
292 		hat->hat_ht_hash = kmem_cache_alloc(vlp_hash_cache, KM_SLEEP);
293 	} else {
294 		hat->hat_num_hash = mmu.hash_cnt;
295 		hat->hat_ht_hash = kmem_cache_alloc(hat_hash_cache, KM_SLEEP);
296 	}
297 	bzero(hat->hat_ht_hash, hat->hat_num_hash * sizeof (htable_t *));
298 
299 	/*
300 	 * Initialize Kernel HAT entries at the top of the top level page
301 	 * tables for the new hat.
302 	 */
303 	hat->hat_htable = NULL;
304 	hat->hat_ht_cached = NULL;
305 	XPV_DISALLOW_MIGRATE();
306 	ht = htable_create(hat, (uintptr_t)0, TOP_LEVEL(hat), NULL);
307 	hat->hat_htable = ht;
308 
309 #if defined(__amd64)
310 	if (hat->hat_flags & HAT_VLP)
311 		goto init_done;
312 #endif
313 
314 	for (r = 0; r < num_kernel_ranges; ++r) {
315 		rp = &kernel_ranges[r];
316 		for (va = rp->hkr_start_va; va != rp->hkr_end_va;
317 		    va += cnt * LEVEL_SIZE(rp->hkr_level)) {
318 
319 			if (rp->hkr_level == TOP_LEVEL(hat))
320 				ht = hat->hat_htable;
321 			else
322 				ht = htable_create(hat, va, rp->hkr_level,
323 				    NULL);
324 
325 			start = htable_va2entry(va, ht);
326 			cnt = HTABLE_NUM_PTES(ht) - start;
327 			eva = va +
328 			    ((uintptr_t)cnt << LEVEL_SHIFT(rp->hkr_level));
329 			if (rp->hkr_end_va != 0 &&
330 			    (eva > rp->hkr_end_va || eva == 0))
331 				cnt = htable_va2entry(rp->hkr_end_va, ht) -
332 				    start;
333 
334 #if defined(__i386) && !defined(__xpv)
335 			if (ht->ht_flags & HTABLE_VLP) {
336 				bcopy(&vlp_page[start],
337 				    &hat->hat_vlp_ptes[start],
338 				    cnt * sizeof (x86pte_t));
339 				continue;
340 			}
341 #endif
342 			src = htable_lookup(kas.a_hat, va, rp->hkr_level);
343 			ASSERT(src != NULL);
344 			x86pte_copy(src, ht, start, cnt);
345 			htable_release(src);
346 		}
347 	}
348 
349 init_done:
350 
351 #if defined(__xpv)
352 	/*
353 	 * Pin top level page tables after initializing them
354 	 */
355 	xen_pin(hat->hat_htable->ht_pfn, mmu.max_level);
356 #if defined(__amd64)
357 	xen_pin(hat->hat_user_ptable, mmu.max_level);
358 #endif
359 #endif
360 	XPV_ALLOW_MIGRATE();
361 
362 	/*
363 	 * Put it at the start of the global list of all hats (used by stealing)
364 	 *
365 	 * kas.a_hat is not in the list but is instead used to find the
366 	 * first and last items in the list.
367 	 *
368 	 * - kas.a_hat->hat_next points to the start of the user hats.
369 	 *   The list ends where hat->hat_next == NULL
370 	 *
371 	 * - kas.a_hat->hat_prev points to the last of the user hats.
372 	 *   The list begins where hat->hat_prev == NULL
373 	 */
374 	mutex_enter(&hat_list_lock);
375 	hat->hat_prev = NULL;
376 	hat->hat_next = kas.a_hat->hat_next;
377 	if (hat->hat_next)
378 		hat->hat_next->hat_prev = hat;
379 	else
380 		kas.a_hat->hat_prev = hat;
381 	kas.a_hat->hat_next = hat;
382 	mutex_exit(&hat_list_lock);
383 
384 	return (hat);
385 }
386 
387 /*
388  * process has finished executing but as has not been cleaned up yet.
389  */
390 /*ARGSUSED*/
391 void
392 hat_free_start(hat_t *hat)
393 {
394 	ASSERT(AS_WRITE_HELD(hat->hat_as, &hat->hat_as->a_lock));
395 
396 	/*
397 	 * If the hat is currently a stealing victim, wait for the stealing
398 	 * to finish.  Once we mark it as HAT_FREEING, htable_steal()
399 	 * won't look at its pagetables anymore.
400 	 */
401 	mutex_enter(&hat_list_lock);
402 	while (hat->hat_flags & HAT_VICTIM)
403 		cv_wait(&hat_list_cv, &hat_list_lock);
404 	hat->hat_flags |= HAT_FREEING;
405 	mutex_exit(&hat_list_lock);
406 }
407 
408 /*
409  * An address space is being destroyed, so we destroy the associated hat.
410  */
411 void
412 hat_free_end(hat_t *hat)
413 {
414 	kmem_cache_t *cache;
415 
416 	ASSERT(hat->hat_flags & HAT_FREEING);
417 
418 	/*
419 	 * must not be running on the given hat
420 	 */
421 	ASSERT(CPU->cpu_current_hat != hat);
422 
423 	/*
424 	 * Remove it from the list of HATs
425 	 */
426 	mutex_enter(&hat_list_lock);
427 	if (hat->hat_prev)
428 		hat->hat_prev->hat_next = hat->hat_next;
429 	else
430 		kas.a_hat->hat_next = hat->hat_next;
431 	if (hat->hat_next)
432 		hat->hat_next->hat_prev = hat->hat_prev;
433 	else
434 		kas.a_hat->hat_prev = hat->hat_prev;
435 	mutex_exit(&hat_list_lock);
436 	hat->hat_next = hat->hat_prev = NULL;
437 
438 #if defined(__xpv)
439 	/*
440 	 * On the hypervisor, unpin top level page table(s)
441 	 */
442 	xen_unpin(hat->hat_htable->ht_pfn);
443 #if defined(__amd64)
444 	xen_unpin(hat->hat_user_ptable);
445 #endif
446 #endif
447 
448 	/*
449 	 * Make a pass through the htables freeing them all up.
450 	 */
451 	htable_purge_hat(hat);
452 
453 	/*
454 	 * Decide which kmem cache the hash table came from, then free it.
455 	 */
456 	if (hat->hat_flags & HAT_VLP)
457 		cache = vlp_hash_cache;
458 	else
459 		cache = hat_hash_cache;
460 	kmem_cache_free(cache, hat->hat_ht_hash);
461 	hat->hat_ht_hash = NULL;
462 
463 	hat->hat_flags = 0;
464 	kmem_cache_free(hat_cache, hat);
465 }
466 
467 /*
468  * round kernelbase down to a supported value to use for _userlimit
469  *
470  * userlimit must be aligned down to an entry in the top level htable.
471  * The one exception is for 32 bit HAT's running PAE.
472  */
473 uintptr_t
474 hat_kernelbase(uintptr_t va)
475 {
476 #if defined(__i386)
477 	va &= LEVEL_MASK(1);
478 #endif
479 	if (IN_VA_HOLE(va))
480 		panic("_userlimit %p will fall in VA hole\n", (void *)va);
481 	return (va);
482 }
483 
484 /*
485  *
486  */
487 static void
488 set_max_page_level()
489 {
490 	level_t lvl;
491 
492 	if (!kbm_largepage_support) {
493 		lvl = 0;
494 	} else {
495 		if (x86_feature & X86_1GPG) {
496 			lvl = 2;
497 			if (chk_optimal_1gtlb &&
498 			    cpuid_opteron_erratum(CPU, 6671130)) {
499 				lvl = 1;
500 			}
501 			if (plat_mnode_xcheck(LEVEL_SIZE(2) >>
502 			    LEVEL_SHIFT(0))) {
503 				lvl = 1;
504 			}
505 		} else {
506 			lvl = 1;
507 		}
508 	}
509 	mmu.max_page_level = lvl;
510 
511 	if ((lvl == 2) && (enable_1gpg == 0))
512 		mmu.umax_page_level = 1;
513 	else
514 		mmu.umax_page_level = lvl;
515 }
516 
517 /*
518  * Initialize hat data structures based on processor MMU information.
519  */
520 void
521 mmu_init(void)
522 {
523 	uint_t max_htables;
524 	uint_t pa_bits;
525 	uint_t va_bits;
526 	int i;
527 
528 	/*
529 	 * If CPU enabled the page table global bit, use it for the kernel
530 	 * This is bit 7 in CR4 (PGE - Page Global Enable).
531 	 */
532 	if ((x86_feature & X86_PGE) != 0 && (getcr4() & CR4_PGE) != 0)
533 		mmu.pt_global = PT_GLOBAL;
534 
535 	/*
536 	 * Detect NX and PAE usage.
537 	 */
538 	mmu.pae_hat = kbm_pae_support;
539 	if (kbm_nx_support)
540 		mmu.pt_nx = PT_NX;
541 	else
542 		mmu.pt_nx = 0;
543 
544 	/*
545 	 * Use CPU info to set various MMU parameters
546 	 */
547 	cpuid_get_addrsize(CPU, &pa_bits, &va_bits);
548 
549 	if (va_bits < sizeof (void *) * NBBY) {
550 		mmu.hole_start = (1ul << (va_bits - 1));
551 		mmu.hole_end = 0ul - mmu.hole_start - 1;
552 	} else {
553 		mmu.hole_end = 0;
554 		mmu.hole_start = mmu.hole_end - 1;
555 	}
556 #if defined(OPTERON_ERRATUM_121)
557 	/*
558 	 * If erratum 121 has already been detected at this time, hole_start
559 	 * contains the value to be subtracted from mmu.hole_start.
560 	 */
561 	ASSERT(hole_start == 0 || opteron_erratum_121 != 0);
562 	hole_start = mmu.hole_start - hole_start;
563 #else
564 	hole_start = mmu.hole_start;
565 #endif
566 	hole_end = mmu.hole_end;
567 
568 	mmu.highest_pfn = mmu_btop((1ull << pa_bits) - 1);
569 	if (mmu.pae_hat == 0 && pa_bits > 32)
570 		mmu.highest_pfn = PFN_4G - 1;
571 
572 	if (mmu.pae_hat) {
573 		mmu.pte_size = 8;	/* 8 byte PTEs */
574 		mmu.pte_size_shift = 3;
575 	} else {
576 		mmu.pte_size = 4;	/* 4 byte PTEs */
577 		mmu.pte_size_shift = 2;
578 	}
579 
580 	if (mmu.pae_hat && (x86_feature & X86_PAE) == 0)
581 		panic("Processor does not support PAE");
582 
583 	if ((x86_feature & X86_CX8) == 0)
584 		panic("Processor does not support cmpxchg8b instruction");
585 
586 #if defined(__amd64)
587 
588 	mmu.num_level = 4;
589 	mmu.max_level = 3;
590 	mmu.ptes_per_table = 512;
591 	mmu.top_level_count = 512;
592 
593 	mmu.level_shift[0] = 12;
594 	mmu.level_shift[1] = 21;
595 	mmu.level_shift[2] = 30;
596 	mmu.level_shift[3] = 39;
597 
598 #elif defined(__i386)
599 
600 	if (mmu.pae_hat) {
601 		mmu.num_level = 3;
602 		mmu.max_level = 2;
603 		mmu.ptes_per_table = 512;
604 		mmu.top_level_count = 4;
605 
606 		mmu.level_shift[0] = 12;
607 		mmu.level_shift[1] = 21;
608 		mmu.level_shift[2] = 30;
609 
610 	} else {
611 		mmu.num_level = 2;
612 		mmu.max_level = 1;
613 		mmu.ptes_per_table = 1024;
614 		mmu.top_level_count = 1024;
615 
616 		mmu.level_shift[0] = 12;
617 		mmu.level_shift[1] = 22;
618 	}
619 
620 #endif	/* __i386 */
621 
622 	for (i = 0; i < mmu.num_level; ++i) {
623 		mmu.level_size[i] = 1UL << mmu.level_shift[i];
624 		mmu.level_offset[i] = mmu.level_size[i] - 1;
625 		mmu.level_mask[i] = ~mmu.level_offset[i];
626 	}
627 
628 	set_max_page_level();
629 
630 	mmu_page_sizes = mmu.max_page_level + 1;
631 	mmu_exported_page_sizes = mmu.umax_page_level + 1;
632 
633 	/* restrict legacy applications from using pagesizes 1g and above */
634 	mmu_legacy_page_sizes =
635 	    (mmu_exported_page_sizes > 2) ? 2 : mmu_exported_page_sizes;
636 
637 
638 	for (i = 0; i <= mmu.max_page_level; ++i) {
639 		mmu.pte_bits[i] = PT_VALID | pt_kern;
640 		if (i > 0)
641 			mmu.pte_bits[i] |= PT_PAGESIZE;
642 	}
643 
644 	/*
645 	 * NOTE Legacy 32 bit PAE mode only has the P_VALID bit at top level.
646 	 */
647 	for (i = 1; i < mmu.num_level; ++i)
648 		mmu.ptp_bits[i] = PT_PTPBITS;
649 
650 #if defined(__i386)
651 	mmu.ptp_bits[2] = PT_VALID;
652 #endif
653 
654 	/*
655 	 * Compute how many hash table entries to have per process for htables.
656 	 * We start with 1 page's worth of entries.
657 	 *
658 	 * If physical memory is small, reduce the amount need to cover it.
659 	 */
660 	max_htables = physmax / mmu.ptes_per_table;
661 	mmu.hash_cnt = MMU_PAGESIZE / sizeof (htable_t *);
662 	while (mmu.hash_cnt > 16 && mmu.hash_cnt >= max_htables)
663 		mmu.hash_cnt >>= 1;
664 	mmu.vlp_hash_cnt = mmu.hash_cnt;
665 
666 #if defined(__amd64)
667 	/*
668 	 * If running in 64 bits and physical memory is large,
669 	 * increase the size of the cache to cover all of memory for
670 	 * a 64 bit process.
671 	 */
672 #define	HASH_MAX_LENGTH 4
673 	while (mmu.hash_cnt * HASH_MAX_LENGTH < max_htables)
674 		mmu.hash_cnt <<= 1;
675 #endif
676 }
677 
678 
679 /*
680  * initialize hat data structures
681  */
682 void
683 hat_init()
684 {
685 #if defined(__i386)
686 	/*
687 	 * _userlimit must be aligned correctly
688 	 */
689 	if ((_userlimit & LEVEL_MASK(1)) != _userlimit) {
690 		prom_printf("hat_init(): _userlimit=%p, not aligned at %p\n",
691 		    (void *)_userlimit, (void *)LEVEL_SIZE(1));
692 		halt("hat_init(): Unable to continue");
693 	}
694 #endif
695 
696 	cv_init(&hat_list_cv, NULL, CV_DEFAULT, NULL);
697 
698 	/*
699 	 * initialize kmem caches
700 	 */
701 	htable_init();
702 	hment_init();
703 
704 	hat_cache = kmem_cache_create("hat_t",
705 	    sizeof (hat_t), 0, hati_constructor, NULL, NULL,
706 	    NULL, 0, 0);
707 
708 	hat_hash_cache = kmem_cache_create("HatHash",
709 	    mmu.hash_cnt * sizeof (htable_t *), 0, NULL, NULL, NULL,
710 	    NULL, 0, 0);
711 
712 	/*
713 	 * VLP hats can use a smaller hash table size on large memroy machines
714 	 */
715 	if (mmu.hash_cnt == mmu.vlp_hash_cnt) {
716 		vlp_hash_cache = hat_hash_cache;
717 	} else {
718 		vlp_hash_cache = kmem_cache_create("HatVlpHash",
719 		    mmu.vlp_hash_cnt * sizeof (htable_t *), 0, NULL, NULL, NULL,
720 		    NULL, 0, 0);
721 	}
722 
723 	/*
724 	 * Set up the kernel's hat
725 	 */
726 	AS_LOCK_ENTER(&kas, &kas.a_lock, RW_WRITER);
727 	kas.a_hat = kmem_cache_alloc(hat_cache, KM_NOSLEEP);
728 	mutex_init(&kas.a_hat->hat_mutex, NULL, MUTEX_DEFAULT, NULL);
729 	kas.a_hat->hat_as = &kas;
730 	kas.a_hat->hat_flags = 0;
731 	AS_LOCK_EXIT(&kas, &kas.a_lock);
732 
733 	CPUSET_ZERO(khat_cpuset);
734 	CPUSET_ADD(khat_cpuset, CPU->cpu_id);
735 
736 	/*
737 	 * The kernel hat's next pointer serves as the head of the hat list .
738 	 * The kernel hat's prev pointer tracks the last hat on the list for
739 	 * htable_steal() to use.
740 	 */
741 	kas.a_hat->hat_next = NULL;
742 	kas.a_hat->hat_prev = NULL;
743 
744 	/*
745 	 * Allocate an htable hash bucket for the kernel
746 	 * XX64 - tune for 64 bit procs
747 	 */
748 	kas.a_hat->hat_num_hash = mmu.hash_cnt;
749 	kas.a_hat->hat_ht_hash = kmem_cache_alloc(hat_hash_cache, KM_NOSLEEP);
750 	bzero(kas.a_hat->hat_ht_hash, mmu.hash_cnt * sizeof (htable_t *));
751 
752 	/*
753 	 * zero out the top level and cached htable pointers
754 	 */
755 	kas.a_hat->hat_ht_cached = NULL;
756 	kas.a_hat->hat_htable = NULL;
757 
758 	/*
759 	 * Pre-allocate hrm_hashtab before enabling the collection of
760 	 * refmod statistics.  Allocating on the fly would mean us
761 	 * running the risk of suffering recursive mutex enters or
762 	 * deadlocks.
763 	 */
764 	hrm_hashtab = kmem_zalloc(HRM_HASHSIZE * sizeof (struct hrmstat *),
765 	    KM_SLEEP);
766 }
767 
768 /*
769  * Prepare CPU specific pagetables for VLP processes on 64 bit kernels.
770  *
771  * Each CPU has a set of 2 pagetables that are reused for any 32 bit
772  * process it runs. They are the top level pagetable, hci_vlp_l3ptes, and
773  * the next to top level table for the bottom 512 Gig, hci_vlp_l2ptes.
774  */
775 /*ARGSUSED*/
776 static void
777 hat_vlp_setup(struct cpu *cpu)
778 {
779 #if defined(__amd64) && !defined(__xpv)
780 	struct hat_cpu_info *hci = cpu->cpu_hat_info;
781 	pfn_t pfn;
782 
783 	/*
784 	 * allocate the level==2 page table for the bottom most
785 	 * 512Gig of address space (this is where 32 bit apps live)
786 	 */
787 	ASSERT(hci != NULL);
788 	hci->hci_vlp_l2ptes = kmem_zalloc(MMU_PAGESIZE, KM_SLEEP);
789 
790 	/*
791 	 * Allocate a top level pagetable and copy the kernel's
792 	 * entries into it. Then link in hci_vlp_l2ptes in the 1st entry.
793 	 */
794 	hci->hci_vlp_l3ptes = kmem_zalloc(MMU_PAGESIZE, KM_SLEEP);
795 	hci->hci_vlp_pfn =
796 	    hat_getpfnum(kas.a_hat, (caddr_t)hci->hci_vlp_l3ptes);
797 	ASSERT(hci->hci_vlp_pfn != PFN_INVALID);
798 	bcopy(vlp_page, hci->hci_vlp_l3ptes, MMU_PAGESIZE);
799 
800 	pfn = hat_getpfnum(kas.a_hat, (caddr_t)hci->hci_vlp_l2ptes);
801 	ASSERT(pfn != PFN_INVALID);
802 	hci->hci_vlp_l3ptes[0] = MAKEPTP(pfn, 2);
803 #endif /* __amd64 && !__xpv */
804 }
805 
806 /*ARGSUSED*/
807 static void
808 hat_vlp_teardown(cpu_t *cpu)
809 {
810 #if defined(__amd64) && !defined(__xpv)
811 	struct hat_cpu_info *hci;
812 
813 	if ((hci = cpu->cpu_hat_info) == NULL)
814 		return;
815 	if (hci->hci_vlp_l2ptes)
816 		kmem_free(hci->hci_vlp_l2ptes, MMU_PAGESIZE);
817 	if (hci->hci_vlp_l3ptes)
818 		kmem_free(hci->hci_vlp_l3ptes, MMU_PAGESIZE);
819 #endif
820 }
821 
822 #define	NEXT_HKR(r, l, s, e) {			\
823 	kernel_ranges[r].hkr_level = l;		\
824 	kernel_ranges[r].hkr_start_va = s;	\
825 	kernel_ranges[r].hkr_end_va = e;	\
826 	++r;					\
827 }
828 
829 /*
830  * Finish filling in the kernel hat.
831  * Pre fill in all top level kernel page table entries for the kernel's
832  * part of the address range.  From this point on we can't use any new
833  * kernel large pages if they need PTE's at max_level
834  *
835  * create the kmap mappings.
836  */
837 void
838 hat_init_finish(void)
839 {
840 	size_t		size;
841 	uint_t		r = 0;
842 	uintptr_t	va;
843 	hat_kernel_range_t *rp;
844 
845 
846 	/*
847 	 * We are now effectively running on the kernel hat.
848 	 * Clearing use_boot_reserve shuts off using the pre-allocated boot
849 	 * reserve for all HAT allocations.  From here on, the reserves are
850 	 * only used when avoiding recursion in kmem_alloc().
851 	 */
852 	use_boot_reserve = 0;
853 	htable_adjust_reserve();
854 
855 	/*
856 	 * User HATs are initialized with copies of all kernel mappings in
857 	 * higher level page tables. Ensure that those entries exist.
858 	 */
859 #if defined(__amd64)
860 
861 	NEXT_HKR(r, 3, kernelbase, 0);
862 #if defined(__xpv)
863 	NEXT_HKR(r, 3, HYPERVISOR_VIRT_START, HYPERVISOR_VIRT_END);
864 #endif
865 
866 #elif defined(__i386)
867 
868 #if !defined(__xpv)
869 	if (mmu.pae_hat) {
870 		va = kernelbase;
871 		if ((va & LEVEL_MASK(2)) != va) {
872 			va = P2ROUNDUP(va, LEVEL_SIZE(2));
873 			NEXT_HKR(r, 1, kernelbase, va);
874 		}
875 		if (va != 0)
876 			NEXT_HKR(r, 2, va, 0);
877 	} else
878 #endif /* __xpv */
879 		NEXT_HKR(r, 1, kernelbase, 0);
880 
881 #endif /* __i386 */
882 
883 	num_kernel_ranges = r;
884 
885 	/*
886 	 * Create all the kernel pagetables that will have entries
887 	 * shared to user HATs.
888 	 */
889 	for (r = 0; r < num_kernel_ranges; ++r) {
890 		rp = &kernel_ranges[r];
891 		for (va = rp->hkr_start_va; va != rp->hkr_end_va;
892 		    va += LEVEL_SIZE(rp->hkr_level)) {
893 			htable_t *ht;
894 
895 			if (IN_HYPERVISOR_VA(va))
896 				continue;
897 
898 			/* can/must skip if a page mapping already exists */
899 			if (rp->hkr_level <= mmu.max_page_level &&
900 			    (ht = htable_getpage(kas.a_hat, va, NULL)) !=
901 			    NULL) {
902 				htable_release(ht);
903 				continue;
904 			}
905 
906 			(void) htable_create(kas.a_hat, va, rp->hkr_level - 1,
907 			    NULL);
908 		}
909 	}
910 
911 	/*
912 	 * 32 bit PAE metal kernels use only 4 of the 512 entries in the
913 	 * page holding the top level pagetable. We use the remainder for
914 	 * the "per CPU" page tables for VLP processes.
915 	 * Map the top level kernel pagetable into the kernel to make
916 	 * it easy to use bcopy access these tables.
917 	 */
918 	if (mmu.pae_hat) {
919 		vlp_page = vmem_alloc(heap_arena, MMU_PAGESIZE, VM_SLEEP);
920 		hat_devload(kas.a_hat, (caddr_t)vlp_page, MMU_PAGESIZE,
921 		    kas.a_hat->hat_htable->ht_pfn,
922 #if !defined(__xpv)
923 		    PROT_WRITE |
924 #endif
925 		    PROT_READ | HAT_NOSYNC | HAT_UNORDERED_OK,
926 		    HAT_LOAD | HAT_LOAD_NOCONSIST);
927 	}
928 	hat_vlp_setup(CPU);
929 
930 	/*
931 	 * Create kmap (cached mappings of kernel PTEs)
932 	 * for 32 bit we map from segmap_start .. ekernelheap
933 	 * for 64 bit we map from segmap_start .. segmap_start + segmapsize;
934 	 */
935 #if defined(__i386)
936 	size = (uintptr_t)ekernelheap - segmap_start;
937 #elif defined(__amd64)
938 	size = segmapsize;
939 #endif
940 	hat_kmap_init((uintptr_t)segmap_start, size);
941 }
942 
943 /*
944  * On 32 bit PAE mode, PTE's are 64 bits, but ordinary atomic memory references
945  * are 32 bit, so for safety we must use cas64() to install these.
946  */
947 #ifdef __i386
948 static void
949 reload_pae32(hat_t *hat, cpu_t *cpu)
950 {
951 	x86pte_t *src;
952 	x86pte_t *dest;
953 	x86pte_t pte;
954 	int i;
955 
956 	/*
957 	 * Load the 4 entries of the level 2 page table into this
958 	 * cpu's range of the vlp_page and point cr3 at them.
959 	 */
960 	ASSERT(mmu.pae_hat);
961 	src = hat->hat_vlp_ptes;
962 	dest = vlp_page + (cpu->cpu_id + 1) * VLP_NUM_PTES;
963 	for (i = 0; i < VLP_NUM_PTES; ++i) {
964 		for (;;) {
965 			pte = dest[i];
966 			if (pte == src[i])
967 				break;
968 			if (cas64(dest + i, pte, src[i]) != src[i])
969 				break;
970 		}
971 	}
972 }
973 #endif
974 
975 /*
976  * Switch to a new active hat, maintaining bit masks to track active CPUs.
977  *
978  * On the 32-bit PAE hypervisor, %cr3 is a 64-bit value, on metal it
979  * remains a 32-bit value.
980  */
981 void
982 hat_switch(hat_t *hat)
983 {
984 	uint64_t	newcr3;
985 	cpu_t		*cpu = CPU;
986 	hat_t		*old = cpu->cpu_current_hat;
987 
988 	/*
989 	 * set up this information first, so we don't miss any cross calls
990 	 */
991 	if (old != NULL) {
992 		if (old == hat)
993 			return;
994 		if (old != kas.a_hat)
995 			CPUSET_ATOMIC_DEL(old->hat_cpus, cpu->cpu_id);
996 	}
997 
998 	/*
999 	 * Add this CPU to the active set for this HAT.
1000 	 */
1001 	if (hat != kas.a_hat) {
1002 		CPUSET_ATOMIC_ADD(hat->hat_cpus, cpu->cpu_id);
1003 	}
1004 	cpu->cpu_current_hat = hat;
1005 
1006 	/*
1007 	 * now go ahead and load cr3
1008 	 */
1009 	if (hat->hat_flags & HAT_VLP) {
1010 #if defined(__amd64)
1011 		x86pte_t *vlpptep = cpu->cpu_hat_info->hci_vlp_l2ptes;
1012 
1013 		VLP_COPY(hat->hat_vlp_ptes, vlpptep);
1014 		newcr3 = MAKECR3(cpu->cpu_hat_info->hci_vlp_pfn);
1015 #elif defined(__i386)
1016 		reload_pae32(hat, cpu);
1017 		newcr3 = MAKECR3(kas.a_hat->hat_htable->ht_pfn) +
1018 		    (cpu->cpu_id + 1) * VLP_SIZE;
1019 #endif
1020 	} else {
1021 		newcr3 = MAKECR3((uint64_t)hat->hat_htable->ht_pfn);
1022 	}
1023 #ifdef __xpv
1024 	{
1025 		struct mmuext_op t[2];
1026 		uint_t retcnt;
1027 		uint_t opcnt = 1;
1028 
1029 		t[0].cmd = MMUEXT_NEW_BASEPTR;
1030 		t[0].arg1.mfn = mmu_btop(pa_to_ma(newcr3));
1031 #if defined(__amd64)
1032 		/*
1033 		 * There's an interesting problem here, as to what to
1034 		 * actually specify when switching to the kernel hat.
1035 		 * For now we'll reuse the kernel hat again.
1036 		 */
1037 		t[1].cmd = MMUEXT_NEW_USER_BASEPTR;
1038 		if (hat == kas.a_hat)
1039 			t[1].arg1.mfn = mmu_btop(pa_to_ma(newcr3));
1040 		else
1041 			t[1].arg1.mfn = pfn_to_mfn(hat->hat_user_ptable);
1042 		++opcnt;
1043 #endif	/* __amd64 */
1044 		if (HYPERVISOR_mmuext_op(t, opcnt, &retcnt, DOMID_SELF) < 0)
1045 			panic("HYPERVISOR_mmu_update() failed");
1046 		ASSERT(retcnt == opcnt);
1047 
1048 	}
1049 #else
1050 	setcr3(newcr3);
1051 #endif
1052 	ASSERT(cpu == CPU);
1053 }
1054 
1055 /*
1056  * Utility to return a valid x86pte_t from protections, pfn, and level number
1057  */
1058 static x86pte_t
1059 hati_mkpte(pfn_t pfn, uint_t attr, level_t level, uint_t flags)
1060 {
1061 	x86pte_t	pte;
1062 	uint_t		cache_attr = attr & HAT_ORDER_MASK;
1063 
1064 	pte = MAKEPTE(pfn, level);
1065 
1066 	if (attr & PROT_WRITE)
1067 		PTE_SET(pte, PT_WRITABLE);
1068 
1069 	if (attr & PROT_USER)
1070 		PTE_SET(pte, PT_USER);
1071 
1072 	if (!(attr & PROT_EXEC))
1073 		PTE_SET(pte, mmu.pt_nx);
1074 
1075 	/*
1076 	 * Set the software bits used track ref/mod sync's and hments.
1077 	 * If not using REF/MOD, set them to avoid h/w rewriting PTEs.
1078 	 */
1079 	if (flags & HAT_LOAD_NOCONSIST)
1080 		PTE_SET(pte, PT_NOCONSIST | PT_REF | PT_MOD);
1081 	else if (attr & HAT_NOSYNC)
1082 		PTE_SET(pte, PT_NOSYNC | PT_REF | PT_MOD);
1083 
1084 	/*
1085 	 * Set the caching attributes in the PTE. The combination
1086 	 * of attributes are poorly defined, so we pay attention
1087 	 * to them in the given order.
1088 	 *
1089 	 * The test for HAT_STRICTORDER is different because it's defined
1090 	 * as "0" - which was a stupid thing to do, but is too late to change!
1091 	 */
1092 	if (cache_attr == HAT_STRICTORDER) {
1093 		PTE_SET(pte, PT_NOCACHE);
1094 	/*LINTED [Lint hates empty ifs, but it's the obvious way to do this] */
1095 	} else if (cache_attr & (HAT_UNORDERED_OK | HAT_STORECACHING_OK)) {
1096 		/* nothing to set */;
1097 	} else if (cache_attr & (HAT_MERGING_OK | HAT_LOADCACHING_OK)) {
1098 		PTE_SET(pte, PT_NOCACHE);
1099 		if (x86_feature & X86_PAT)
1100 			PTE_SET(pte, (level == 0) ? PT_PAT_4K : PT_PAT_LARGE);
1101 		else
1102 			PTE_SET(pte, PT_WRITETHRU);
1103 	} else {
1104 		panic("hati_mkpte(): bad caching attributes: %x\n", cache_attr);
1105 	}
1106 
1107 	return (pte);
1108 }
1109 
1110 /*
1111  * Duplicate address translations of the parent to the child.
1112  * This function really isn't used anymore.
1113  */
1114 /*ARGSUSED*/
1115 int
1116 hat_dup(hat_t *old, hat_t *new, caddr_t addr, size_t len, uint_t flag)
1117 {
1118 	ASSERT((uintptr_t)addr < kernelbase);
1119 	ASSERT(new != kas.a_hat);
1120 	ASSERT(old != kas.a_hat);
1121 	return (0);
1122 }
1123 
1124 /*
1125  * Allocate any hat resources required for a process being swapped in.
1126  */
1127 /*ARGSUSED*/
1128 void
1129 hat_swapin(hat_t *hat)
1130 {
1131 	/* do nothing - we let everything fault back in */
1132 }
1133 
1134 /*
1135  * Unload all translations associated with an address space of a process
1136  * that is being swapped out.
1137  */
1138 void
1139 hat_swapout(hat_t *hat)
1140 {
1141 	uintptr_t	vaddr = (uintptr_t)0;
1142 	uintptr_t	eaddr = _userlimit;
1143 	htable_t	*ht = NULL;
1144 	level_t		l;
1145 
1146 	XPV_DISALLOW_MIGRATE();
1147 	/*
1148 	 * We can't just call hat_unload(hat, 0, _userlimit...)  here, because
1149 	 * seg_spt and shared pagetables can't be swapped out.
1150 	 * Take a look at segspt_shmswapout() - it's a big no-op.
1151 	 *
1152 	 * Instead we'll walk through all the address space and unload
1153 	 * any mappings which we are sure are not shared, not locked.
1154 	 */
1155 	ASSERT(IS_PAGEALIGNED(vaddr));
1156 	ASSERT(IS_PAGEALIGNED(eaddr));
1157 	ASSERT(AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock));
1158 	if ((uintptr_t)hat->hat_as->a_userlimit < eaddr)
1159 		eaddr = (uintptr_t)hat->hat_as->a_userlimit;
1160 
1161 	while (vaddr < eaddr) {
1162 		(void) htable_walk(hat, &ht, &vaddr, eaddr);
1163 		if (ht == NULL)
1164 			break;
1165 
1166 		ASSERT(!IN_VA_HOLE(vaddr));
1167 
1168 		/*
1169 		 * If the page table is shared skip its entire range.
1170 		 */
1171 		l = ht->ht_level;
1172 		if (ht->ht_flags & HTABLE_SHARED_PFN) {
1173 			vaddr = ht->ht_vaddr + LEVEL_SIZE(l + 1);
1174 			htable_release(ht);
1175 			ht = NULL;
1176 			continue;
1177 		}
1178 
1179 		/*
1180 		 * If the page table has no locked entries, unload this one.
1181 		 */
1182 		if (ht->ht_lock_cnt == 0)
1183 			hat_unload(hat, (caddr_t)vaddr, LEVEL_SIZE(l),
1184 			    HAT_UNLOAD_UNMAP);
1185 
1186 		/*
1187 		 * If we have a level 0 page table with locked entries,
1188 		 * skip the entire page table, otherwise skip just one entry.
1189 		 */
1190 		if (ht->ht_lock_cnt > 0 && l == 0)
1191 			vaddr = ht->ht_vaddr + LEVEL_SIZE(1);
1192 		else
1193 			vaddr += LEVEL_SIZE(l);
1194 	}
1195 	if (ht)
1196 		htable_release(ht);
1197 
1198 	/*
1199 	 * We're in swapout because the system is low on memory, so
1200 	 * go back and flush all the htables off the cached list.
1201 	 */
1202 	htable_purge_hat(hat);
1203 	XPV_ALLOW_MIGRATE();
1204 }
1205 
1206 /*
1207  * returns number of bytes that have valid mappings in hat.
1208  */
1209 size_t
1210 hat_get_mapped_size(hat_t *hat)
1211 {
1212 	size_t total = 0;
1213 	int l;
1214 
1215 	for (l = 0; l <= mmu.max_page_level; l++)
1216 		total += (hat->hat_pages_mapped[l] << LEVEL_SHIFT(l));
1217 	total += hat->hat_ism_pgcnt;
1218 
1219 	return (total);
1220 }
1221 
1222 /*
1223  * enable/disable collection of stats for hat.
1224  */
1225 int
1226 hat_stats_enable(hat_t *hat)
1227 {
1228 	atomic_add_32(&hat->hat_stats, 1);
1229 	return (1);
1230 }
1231 
1232 void
1233 hat_stats_disable(hat_t *hat)
1234 {
1235 	atomic_add_32(&hat->hat_stats, -1);
1236 }
1237 
1238 /*
1239  * Utility to sync the ref/mod bits from a page table entry to the page_t
1240  * We must be holding the mapping list lock when this is called.
1241  */
1242 static void
1243 hati_sync_pte_to_page(page_t *pp, x86pte_t pte, level_t level)
1244 {
1245 	uint_t	rm = 0;
1246 	pgcnt_t	pgcnt;
1247 
1248 	if (PTE_GET(pte, PT_SOFTWARE) >= PT_NOSYNC)
1249 		return;
1250 
1251 	if (PTE_GET(pte, PT_REF))
1252 		rm |= P_REF;
1253 
1254 	if (PTE_GET(pte, PT_MOD))
1255 		rm |= P_MOD;
1256 
1257 	if (rm == 0)
1258 		return;
1259 
1260 	/*
1261 	 * sync to all constituent pages of a large page
1262 	 */
1263 	ASSERT(x86_hm_held(pp));
1264 	pgcnt = page_get_pagecnt(level);
1265 	ASSERT(IS_P2ALIGNED(pp->p_pagenum, pgcnt));
1266 	for (; pgcnt > 0; --pgcnt) {
1267 		/*
1268 		 * hat_page_demote() can't decrease
1269 		 * pszc below this mapping size
1270 		 * since this large mapping existed after we
1271 		 * took mlist lock.
1272 		 */
1273 		ASSERT(pp->p_szc >= level);
1274 		hat_page_setattr(pp, rm);
1275 		++pp;
1276 	}
1277 }
1278 
1279 /*
1280  * This the set of PTE bits for PFN, permissions and caching
1281  * that are allowed to change on a HAT_LOAD_REMAP
1282  */
1283 #define	PT_REMAP_BITS							\
1284 	(PT_PADDR | PT_NX | PT_WRITABLE | PT_WRITETHRU |		\
1285 	PT_NOCACHE | PT_PAT_4K | PT_PAT_LARGE | PT_IGNORE | PT_REF | PT_MOD)
1286 
1287 #define	REMAPASSERT(EX)	if (!(EX)) panic("hati_pte_map: " #EX)
1288 /*
1289  * Do the low-level work to get a mapping entered into a HAT's pagetables
1290  * and in the mapping list of the associated page_t.
1291  */
1292 static int
1293 hati_pte_map(
1294 	htable_t	*ht,
1295 	uint_t		entry,
1296 	page_t		*pp,
1297 	x86pte_t	pte,
1298 	int		flags,
1299 	void		*pte_ptr)
1300 {
1301 	hat_t		*hat = ht->ht_hat;
1302 	x86pte_t	old_pte;
1303 	level_t		l = ht->ht_level;
1304 	hment_t		*hm;
1305 	uint_t		is_consist;
1306 	uint_t		is_locked;
1307 	int		rv = 0;
1308 
1309 	/*
1310 	 * Is this a consistent (ie. need mapping list lock) mapping?
1311 	 */
1312 	is_consist = (pp != NULL && (flags & HAT_LOAD_NOCONSIST) == 0);
1313 
1314 	/*
1315 	 * Track locked mapping count in the htable.  Do this first,
1316 	 * as we track locking even if there already is a mapping present.
1317 	 */
1318 	is_locked = (flags & HAT_LOAD_LOCK) != 0 && hat != kas.a_hat;
1319 	if (is_locked)
1320 		HTABLE_LOCK_INC(ht);
1321 
1322 	/*
1323 	 * Acquire the page's mapping list lock and get an hment to use.
1324 	 * Note that hment_prepare() might return NULL.
1325 	 */
1326 	if (is_consist) {
1327 		x86_hm_enter(pp);
1328 		hm = hment_prepare(ht, entry, pp);
1329 	}
1330 
1331 	/*
1332 	 * Set the new pte, retrieving the old one at the same time.
1333 	 */
1334 	old_pte = x86pte_set(ht, entry, pte, pte_ptr);
1335 
1336 	/*
1337 	 * Did we get a large page / page table collision?
1338 	 */
1339 	if (old_pte == LPAGE_ERROR) {
1340 		if (is_locked)
1341 			HTABLE_LOCK_DEC(ht);
1342 		rv = -1;
1343 		goto done;
1344 	}
1345 
1346 	/*
1347 	 * If the mapping didn't change there is nothing more to do.
1348 	 */
1349 	if (PTE_EQUIV(pte, old_pte))
1350 		goto done;
1351 
1352 	/*
1353 	 * Install a new mapping in the page's mapping list
1354 	 */
1355 	if (!PTE_ISVALID(old_pte)) {
1356 		if (is_consist) {
1357 			hment_assign(ht, entry, pp, hm);
1358 			x86_hm_exit(pp);
1359 		} else {
1360 			ASSERT(flags & HAT_LOAD_NOCONSIST);
1361 		}
1362 #if defined(__amd64)
1363 		if (ht->ht_flags & HTABLE_VLP) {
1364 			cpu_t *cpu = CPU;
1365 			x86pte_t *vlpptep = cpu->cpu_hat_info->hci_vlp_l2ptes;
1366 			VLP_COPY(hat->hat_vlp_ptes, vlpptep);
1367 		}
1368 #endif
1369 		HTABLE_INC(ht->ht_valid_cnt);
1370 		PGCNT_INC(hat, l);
1371 		return (rv);
1372 	}
1373 
1374 	/*
1375 	 * Remap's are more complicated:
1376 	 *  - HAT_LOAD_REMAP must be specified if changing the pfn.
1377 	 *    We also require that NOCONSIST be specified.
1378 	 *  - Otherwise only permission or caching bits may change.
1379 	 */
1380 	if (!PTE_ISPAGE(old_pte, l))
1381 		panic("non-null/page mapping pte=" FMT_PTE, old_pte);
1382 
1383 	if (PTE2PFN(old_pte, l) != PTE2PFN(pte, l)) {
1384 		REMAPASSERT(flags & HAT_LOAD_REMAP);
1385 		REMAPASSERT(flags & HAT_LOAD_NOCONSIST);
1386 		REMAPASSERT(PTE_GET(old_pte, PT_SOFTWARE) >= PT_NOCONSIST);
1387 		REMAPASSERT(pf_is_memory(PTE2PFN(old_pte, l)) ==
1388 		    pf_is_memory(PTE2PFN(pte, l)));
1389 		REMAPASSERT(!is_consist);
1390 	}
1391 
1392 	/*
1393 	 * We only let remaps change the certain bits in the PTE.
1394 	 */
1395 	if (PTE_GET(old_pte, ~PT_REMAP_BITS) != PTE_GET(pte, ~PT_REMAP_BITS))
1396 		panic("remap bits changed: old_pte="FMT_PTE", pte="FMT_PTE"\n",
1397 		    old_pte, pte);
1398 
1399 	/*
1400 	 * We don't create any mapping list entries on a remap, so release
1401 	 * any allocated hment after we drop the mapping list lock.
1402 	 */
1403 done:
1404 	if (is_consist) {
1405 		x86_hm_exit(pp);
1406 		if (hm != NULL)
1407 			hment_free(hm);
1408 	}
1409 	return (rv);
1410 }
1411 
1412 /*
1413  * Internal routine to load a single page table entry. This only fails if
1414  * we attempt to overwrite a page table link with a large page.
1415  */
1416 static int
1417 hati_load_common(
1418 	hat_t		*hat,
1419 	uintptr_t	va,
1420 	page_t		*pp,
1421 	uint_t		attr,
1422 	uint_t		flags,
1423 	level_t		level,
1424 	pfn_t		pfn)
1425 {
1426 	htable_t	*ht;
1427 	uint_t		entry;
1428 	x86pte_t	pte;
1429 	int		rv = 0;
1430 
1431 	/*
1432 	 * The number 16 is arbitrary and here to catch a recursion problem
1433 	 * early before we blow out the kernel stack.
1434 	 */
1435 	++curthread->t_hatdepth;
1436 	ASSERT(curthread->t_hatdepth < 16);
1437 
1438 	ASSERT(hat == kas.a_hat ||
1439 	    AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock));
1440 
1441 	if (flags & HAT_LOAD_SHARE)
1442 		hat->hat_flags |= HAT_SHARED;
1443 
1444 	/*
1445 	 * Find the page table that maps this page if it already exists.
1446 	 */
1447 	ht = htable_lookup(hat, va, level);
1448 
1449 	/*
1450 	 * We must have HAT_LOAD_NOCONSIST if page_t is NULL.
1451 	 */
1452 	if (pp == NULL)
1453 		flags |= HAT_LOAD_NOCONSIST;
1454 
1455 	if (ht == NULL) {
1456 		ht = htable_create(hat, va, level, NULL);
1457 		ASSERT(ht != NULL);
1458 	}
1459 	entry = htable_va2entry(va, ht);
1460 
1461 	/*
1462 	 * a bunch of paranoid error checking
1463 	 */
1464 	ASSERT(ht->ht_busy > 0);
1465 	if (ht->ht_vaddr > va || va > HTABLE_LAST_PAGE(ht))
1466 		panic("hati_load_common: bad htable %p, va %p",
1467 		    (void *)ht, (void *)va);
1468 	ASSERT(ht->ht_level == level);
1469 
1470 	/*
1471 	 * construct the new PTE
1472 	 */
1473 	if (hat == kas.a_hat)
1474 		attr &= ~PROT_USER;
1475 	pte = hati_mkpte(pfn, attr, level, flags);
1476 	if (hat == kas.a_hat && va >= kernelbase)
1477 		PTE_SET(pte, mmu.pt_global);
1478 
1479 	/*
1480 	 * establish the mapping
1481 	 */
1482 	rv = hati_pte_map(ht, entry, pp, pte, flags, NULL);
1483 
1484 	/*
1485 	 * release the htable and any reserves
1486 	 */
1487 	htable_release(ht);
1488 	--curthread->t_hatdepth;
1489 	return (rv);
1490 }
1491 
1492 /*
1493  * special case of hat_memload to deal with some kernel addrs for performance
1494  */
1495 static void
1496 hat_kmap_load(
1497 	caddr_t		addr,
1498 	page_t		*pp,
1499 	uint_t		attr,
1500 	uint_t		flags)
1501 {
1502 	uintptr_t	va = (uintptr_t)addr;
1503 	x86pte_t	pte;
1504 	pfn_t		pfn = page_pptonum(pp);
1505 	pgcnt_t		pg_off = mmu_btop(va - mmu.kmap_addr);
1506 	htable_t	*ht;
1507 	uint_t		entry;
1508 	void		*pte_ptr;
1509 
1510 	/*
1511 	 * construct the requested PTE
1512 	 */
1513 	attr &= ~PROT_USER;
1514 	attr |= HAT_STORECACHING_OK;
1515 	pte = hati_mkpte(pfn, attr, 0, flags);
1516 	PTE_SET(pte, mmu.pt_global);
1517 
1518 	/*
1519 	 * Figure out the pte_ptr and htable and use common code to finish up
1520 	 */
1521 	if (mmu.pae_hat)
1522 		pte_ptr = mmu.kmap_ptes + pg_off;
1523 	else
1524 		pte_ptr = (x86pte32_t *)mmu.kmap_ptes + pg_off;
1525 	ht = mmu.kmap_htables[(va - mmu.kmap_htables[0]->ht_vaddr) >>
1526 	    LEVEL_SHIFT(1)];
1527 	entry = htable_va2entry(va, ht);
1528 	++curthread->t_hatdepth;
1529 	ASSERT(curthread->t_hatdepth < 16);
1530 	(void) hati_pte_map(ht, entry, pp, pte, flags, pte_ptr);
1531 	--curthread->t_hatdepth;
1532 }
1533 
1534 /*
1535  * hat_memload() - load a translation to the given page struct
1536  *
1537  * Flags for hat_memload/hat_devload/hat_*attr.
1538  *
1539  * 	HAT_LOAD	Default flags to load a translation to the page.
1540  *
1541  * 	HAT_LOAD_LOCK	Lock down mapping resources; hat_map(), hat_memload(),
1542  *			and hat_devload().
1543  *
1544  *	HAT_LOAD_NOCONSIST Do not add mapping to page_t mapping list.
1545  *			sets PT_NOCONSIST
1546  *
1547  *	HAT_LOAD_SHARE	A flag to hat_memload() to indicate h/w page tables
1548  *			that map some user pages (not kas) is shared by more
1549  *			than one process (eg. ISM).
1550  *
1551  *	HAT_LOAD_REMAP	Reload a valid pte with a different page frame.
1552  *
1553  *	HAT_NO_KALLOC	Do not kmem_alloc while creating the mapping; at this
1554  *			point, it's setting up mapping to allocate internal
1555  *			hat layer data structures.  This flag forces hat layer
1556  *			to tap its reserves in order to prevent infinite
1557  *			recursion.
1558  *
1559  * The following is a protection attribute (like PROT_READ, etc.)
1560  *
1561  *	HAT_NOSYNC	set PT_NOSYNC - this mapping's ref/mod bits
1562  *			are never cleared.
1563  *
1564  * Installing new valid PTE's and creation of the mapping list
1565  * entry are controlled under the same lock. It's derived from the
1566  * page_t being mapped.
1567  */
1568 static uint_t supported_memload_flags =
1569 	HAT_LOAD | HAT_LOAD_LOCK | HAT_LOAD_ADV | HAT_LOAD_NOCONSIST |
1570 	HAT_LOAD_SHARE | HAT_NO_KALLOC | HAT_LOAD_REMAP | HAT_LOAD_TEXT;
1571 
1572 void
1573 hat_memload(
1574 	hat_t		*hat,
1575 	caddr_t		addr,
1576 	page_t		*pp,
1577 	uint_t		attr,
1578 	uint_t		flags)
1579 {
1580 	uintptr_t	va = (uintptr_t)addr;
1581 	level_t		level = 0;
1582 	pfn_t		pfn = page_pptonum(pp);
1583 
1584 	XPV_DISALLOW_MIGRATE();
1585 	ASSERT(IS_PAGEALIGNED(va));
1586 	ASSERT(hat == kas.a_hat || va < _userlimit);
1587 	ASSERT(hat == kas.a_hat ||
1588 	    AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock));
1589 	ASSERT((flags & supported_memload_flags) == flags);
1590 
1591 	ASSERT(!IN_VA_HOLE(va));
1592 	ASSERT(!PP_ISFREE(pp));
1593 
1594 	/*
1595 	 * kernel address special case for performance.
1596 	 */
1597 	if (mmu.kmap_addr <= va && va < mmu.kmap_eaddr) {
1598 		ASSERT(hat == kas.a_hat);
1599 		hat_kmap_load(addr, pp, attr, flags);
1600 		XPV_ALLOW_MIGRATE();
1601 		return;
1602 	}
1603 
1604 	/*
1605 	 * This is used for memory with normal caching enabled, so
1606 	 * always set HAT_STORECACHING_OK.
1607 	 */
1608 	attr |= HAT_STORECACHING_OK;
1609 	if (hati_load_common(hat, va, pp, attr, flags, level, pfn) != 0)
1610 		panic("unexpected hati_load_common() failure");
1611 	XPV_ALLOW_MIGRATE();
1612 }
1613 
1614 /* ARGSUSED */
1615 void
1616 hat_memload_region(struct hat *hat, caddr_t addr, struct page *pp,
1617     uint_t attr, uint_t flags, hat_region_cookie_t rcookie)
1618 {
1619 	hat_memload(hat, addr, pp, attr, flags);
1620 }
1621 
1622 /*
1623  * Load the given array of page structs using large pages when possible
1624  */
1625 void
1626 hat_memload_array(
1627 	hat_t		*hat,
1628 	caddr_t		addr,
1629 	size_t		len,
1630 	page_t		**pages,
1631 	uint_t		attr,
1632 	uint_t		flags)
1633 {
1634 	uintptr_t	va = (uintptr_t)addr;
1635 	uintptr_t	eaddr = va + len;
1636 	level_t		level;
1637 	size_t		pgsize;
1638 	pgcnt_t		pgindx = 0;
1639 	pfn_t		pfn;
1640 	pgcnt_t		i;
1641 
1642 	XPV_DISALLOW_MIGRATE();
1643 	ASSERT(IS_PAGEALIGNED(va));
1644 	ASSERT(hat == kas.a_hat || va + len <= _userlimit);
1645 	ASSERT(hat == kas.a_hat ||
1646 	    AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock));
1647 	ASSERT((flags & supported_memload_flags) == flags);
1648 
1649 	/*
1650 	 * memload is used for memory with full caching enabled, so
1651 	 * set HAT_STORECACHING_OK.
1652 	 */
1653 	attr |= HAT_STORECACHING_OK;
1654 
1655 	/*
1656 	 * handle all pages using largest possible pagesize
1657 	 */
1658 	while (va < eaddr) {
1659 		/*
1660 		 * decide what level mapping to use (ie. pagesize)
1661 		 */
1662 		pfn = page_pptonum(pages[pgindx]);
1663 		for (level = mmu.max_page_level; ; --level) {
1664 			pgsize = LEVEL_SIZE(level);
1665 			if (level == 0)
1666 				break;
1667 
1668 			if (!IS_P2ALIGNED(va, pgsize) ||
1669 			    (eaddr - va) < pgsize ||
1670 			    !IS_P2ALIGNED(pfn_to_pa(pfn), pgsize))
1671 				continue;
1672 
1673 			/*
1674 			 * To use a large mapping of this size, all the
1675 			 * pages we are passed must be sequential subpages
1676 			 * of the large page.
1677 			 * hat_page_demote() can't change p_szc because
1678 			 * all pages are locked.
1679 			 */
1680 			if (pages[pgindx]->p_szc >= level) {
1681 				for (i = 0; i < mmu_btop(pgsize); ++i) {
1682 					if (pfn + i !=
1683 					    page_pptonum(pages[pgindx + i]))
1684 						break;
1685 					ASSERT(pages[pgindx + i]->p_szc >=
1686 					    level);
1687 					ASSERT(pages[pgindx] + i ==
1688 					    pages[pgindx + i]);
1689 				}
1690 				if (i == mmu_btop(pgsize)) {
1691 #ifdef DEBUG
1692 					if (level == 2)
1693 						map1gcnt++;
1694 #endif
1695 					break;
1696 				}
1697 			}
1698 		}
1699 
1700 		/*
1701 		 * Load this page mapping. If the load fails, try a smaller
1702 		 * pagesize.
1703 		 */
1704 		ASSERT(!IN_VA_HOLE(va));
1705 		while (hati_load_common(hat, va, pages[pgindx], attr,
1706 		    flags, level, pfn) != 0) {
1707 			if (level == 0)
1708 				panic("unexpected hati_load_common() failure");
1709 			--level;
1710 			pgsize = LEVEL_SIZE(level);
1711 		}
1712 
1713 		/*
1714 		 * move to next page
1715 		 */
1716 		va += pgsize;
1717 		pgindx += mmu_btop(pgsize);
1718 	}
1719 	XPV_ALLOW_MIGRATE();
1720 }
1721 
1722 /* ARGSUSED */
1723 void
1724 hat_memload_array_region(struct hat *hat, caddr_t addr, size_t len,
1725     struct page **pps, uint_t attr, uint_t flags,
1726     hat_region_cookie_t rcookie)
1727 {
1728 	hat_memload_array(hat, addr, len, pps, attr, flags);
1729 }
1730 
1731 /*
1732  * void hat_devload(hat, addr, len, pf, attr, flags)
1733  *	load/lock the given page frame number
1734  *
1735  * Advisory ordering attributes. Apply only to device mappings.
1736  *
1737  * HAT_STRICTORDER: the CPU must issue the references in order, as the
1738  *	programmer specified.  This is the default.
1739  * HAT_UNORDERED_OK: the CPU may reorder the references (this is all kinds
1740  *	of reordering; store or load with store or load).
1741  * HAT_MERGING_OK: merging and batching: the CPU may merge individual stores
1742  *	to consecutive locations (for example, turn two consecutive byte
1743  *	stores into one halfword store), and it may batch individual loads
1744  *	(for example, turn two consecutive byte loads into one halfword load).
1745  *	This also implies re-ordering.
1746  * HAT_LOADCACHING_OK: the CPU may cache the data it fetches and reuse it
1747  *	until another store occurs.  The default is to fetch new data
1748  *	on every load.  This also implies merging.
1749  * HAT_STORECACHING_OK: the CPU may keep the data in the cache and push it to
1750  *	the device (perhaps with other data) at a later time.  The default is
1751  *	to push the data right away.  This also implies load caching.
1752  *
1753  * Equivalent of hat_memload(), but can be used for device memory where
1754  * there are no page_t's and we support additional flags (write merging, etc).
1755  * Note that we can have large page mappings with this interface.
1756  */
1757 int supported_devload_flags = HAT_LOAD | HAT_LOAD_LOCK |
1758 	HAT_LOAD_NOCONSIST | HAT_STRICTORDER | HAT_UNORDERED_OK |
1759 	HAT_MERGING_OK | HAT_LOADCACHING_OK | HAT_STORECACHING_OK;
1760 
1761 void
1762 hat_devload(
1763 	hat_t		*hat,
1764 	caddr_t		addr,
1765 	size_t		len,
1766 	pfn_t		pfn,
1767 	uint_t		attr,
1768 	int		flags)
1769 {
1770 	uintptr_t	va = ALIGN2PAGE(addr);
1771 	uintptr_t	eva = va + len;
1772 	level_t		level;
1773 	size_t		pgsize;
1774 	page_t		*pp;
1775 	int		f;	/* per PTE copy of flags  - maybe modified */
1776 	uint_t		a;	/* per PTE copy of attr */
1777 
1778 	XPV_DISALLOW_MIGRATE();
1779 	ASSERT(IS_PAGEALIGNED(va));
1780 	ASSERT(hat == kas.a_hat || eva <= _userlimit);
1781 	ASSERT(hat == kas.a_hat ||
1782 	    AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock));
1783 	ASSERT((flags & supported_devload_flags) == flags);
1784 
1785 	/*
1786 	 * handle all pages
1787 	 */
1788 	while (va < eva) {
1789 
1790 		/*
1791 		 * decide what level mapping to use (ie. pagesize)
1792 		 */
1793 		for (level = mmu.max_page_level; ; --level) {
1794 			pgsize = LEVEL_SIZE(level);
1795 			if (level == 0)
1796 				break;
1797 			if (IS_P2ALIGNED(va, pgsize) &&
1798 			    (eva - va) >= pgsize &&
1799 			    IS_P2ALIGNED(pfn, mmu_btop(pgsize))) {
1800 #ifdef DEBUG
1801 				if (level == 2)
1802 					map1gcnt++;
1803 #endif
1804 				break;
1805 			}
1806 		}
1807 
1808 		/*
1809 		 * If this is just memory then allow caching (this happens
1810 		 * for the nucleus pages) - though HAT_PLAT_NOCACHE can be used
1811 		 * to override that. If we don't have a page_t then make sure
1812 		 * NOCONSIST is set.
1813 		 */
1814 		a = attr;
1815 		f = flags;
1816 		if (!pf_is_memory(pfn))
1817 			f |= HAT_LOAD_NOCONSIST;
1818 		else if (!(a & HAT_PLAT_NOCACHE))
1819 			a |= HAT_STORECACHING_OK;
1820 
1821 		if (f & HAT_LOAD_NOCONSIST)
1822 			pp = NULL;
1823 		else
1824 			pp = page_numtopp_nolock(pfn);
1825 
1826 		/*
1827 		 * Check to make sure we are really trying to map a valid
1828 		 * memory page. The caller wishing to intentionally map
1829 		 * free memory pages will have passed the HAT_LOAD_NOCONSIST
1830 		 * flag, then pp will be NULL.
1831 		 */
1832 		if (pp != NULL) {
1833 			if (PP_ISFREE(pp)) {
1834 				panic("hat_devload: loading "
1835 				    "a mapping to free page %p", (void *)pp);
1836 			}
1837 
1838 			if (!PAGE_LOCKED(pp) && !PP_ISNORELOC(pp)) {
1839 				panic("hat_devload: loading a mapping "
1840 				    "to an unlocked page %p",
1841 				    (void *)pp);
1842 			}
1843 		}
1844 
1845 		/*
1846 		 * load this page mapping
1847 		 */
1848 		ASSERT(!IN_VA_HOLE(va));
1849 		while (hati_load_common(hat, va, pp, a, f, level, pfn) != 0) {
1850 			if (level == 0)
1851 				panic("unexpected hati_load_common() failure");
1852 			--level;
1853 			pgsize = LEVEL_SIZE(level);
1854 		}
1855 
1856 		/*
1857 		 * move to next page
1858 		 */
1859 		va += pgsize;
1860 		pfn += mmu_btop(pgsize);
1861 	}
1862 	XPV_ALLOW_MIGRATE();
1863 }
1864 
1865 /*
1866  * void hat_unlock(hat, addr, len)
1867  *	unlock the mappings to a given range of addresses
1868  *
1869  * Locks are tracked by ht_lock_cnt in the htable.
1870  */
1871 void
1872 hat_unlock(hat_t *hat, caddr_t addr, size_t len)
1873 {
1874 	uintptr_t	vaddr = (uintptr_t)addr;
1875 	uintptr_t	eaddr = vaddr + len;
1876 	htable_t	*ht = NULL;
1877 
1878 	/*
1879 	 * kernel entries are always locked, we don't track lock counts
1880 	 */
1881 	ASSERT(hat == kas.a_hat || eaddr <= _userlimit);
1882 	ASSERT(IS_PAGEALIGNED(vaddr));
1883 	ASSERT(IS_PAGEALIGNED(eaddr));
1884 	if (hat == kas.a_hat)
1885 		return;
1886 	if (eaddr > _userlimit)
1887 		panic("hat_unlock() address out of range - above _userlimit");
1888 
1889 	XPV_DISALLOW_MIGRATE();
1890 	ASSERT(AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock));
1891 	while (vaddr < eaddr) {
1892 		(void) htable_walk(hat, &ht, &vaddr, eaddr);
1893 		if (ht == NULL)
1894 			break;
1895 
1896 		ASSERT(!IN_VA_HOLE(vaddr));
1897 
1898 		if (ht->ht_lock_cnt < 1)
1899 			panic("hat_unlock(): lock_cnt < 1, "
1900 			    "htable=%p, vaddr=%p\n", (void *)ht, (void *)vaddr);
1901 		HTABLE_LOCK_DEC(ht);
1902 
1903 		vaddr += LEVEL_SIZE(ht->ht_level);
1904 	}
1905 	if (ht)
1906 		htable_release(ht);
1907 	XPV_ALLOW_MIGRATE();
1908 }
1909 
1910 /* ARGSUSED */
1911 void
1912 hat_unlock_region(struct hat *hat, caddr_t addr, size_t len,
1913     hat_region_cookie_t rcookie)
1914 {
1915 	panic("No shared region support on x86");
1916 }
1917 
1918 #if !defined(__xpv)
1919 /*
1920  * Cross call service routine to demap a virtual page on
1921  * the current CPU or flush all mappings in TLB.
1922  */
1923 /*ARGSUSED*/
1924 static int
1925 hati_demap_func(xc_arg_t a1, xc_arg_t a2, xc_arg_t a3)
1926 {
1927 	hat_t	*hat = (hat_t *)a1;
1928 	caddr_t	addr = (caddr_t)a2;
1929 
1930 	/*
1931 	 * If the target hat isn't the kernel and this CPU isn't operating
1932 	 * in the target hat, we can ignore the cross call.
1933 	 */
1934 	if (hat != kas.a_hat && hat != CPU->cpu_current_hat)
1935 		return (0);
1936 
1937 	/*
1938 	 * For a normal address, we just flush one page mapping
1939 	 */
1940 	if ((uintptr_t)addr != DEMAP_ALL_ADDR) {
1941 		mmu_tlbflush_entry(addr);
1942 		return (0);
1943 	}
1944 
1945 	/*
1946 	 * Otherwise we reload cr3 to effect a complete TLB flush.
1947 	 *
1948 	 * A reload of cr3 on a VLP process also means we must also recopy in
1949 	 * the pte values from the struct hat
1950 	 */
1951 	if (hat->hat_flags & HAT_VLP) {
1952 #if defined(__amd64)
1953 		x86pte_t *vlpptep = CPU->cpu_hat_info->hci_vlp_l2ptes;
1954 
1955 		VLP_COPY(hat->hat_vlp_ptes, vlpptep);
1956 #elif defined(__i386)
1957 		reload_pae32(hat, CPU);
1958 #endif
1959 	}
1960 	reload_cr3();
1961 	return (0);
1962 }
1963 
1964 /*
1965  * Flush all TLB entries, including global (ie. kernel) ones.
1966  */
1967 static void
1968 flush_all_tlb_entries(void)
1969 {
1970 	ulong_t cr4 = getcr4();
1971 
1972 	if (cr4 & CR4_PGE) {
1973 		setcr4(cr4 & ~(ulong_t)CR4_PGE);
1974 		setcr4(cr4);
1975 
1976 		/*
1977 		 * 32 bit PAE also needs to always reload_cr3()
1978 		 */
1979 		if (mmu.max_level == 2)
1980 			reload_cr3();
1981 	} else {
1982 		reload_cr3();
1983 	}
1984 }
1985 
1986 #define	TLB_CPU_HALTED	(01ul)
1987 #define	TLB_INVAL_ALL	(02ul)
1988 #define	CAS_TLB_INFO(cpu, old, new)	\
1989 	caslong((ulong_t *)&(cpu)->cpu_m.mcpu_tlb_info, (old), (new))
1990 
1991 /*
1992  * Record that a CPU is going idle
1993  */
1994 void
1995 tlb_going_idle(void)
1996 {
1997 	atomic_or_long((ulong_t *)&CPU->cpu_m.mcpu_tlb_info, TLB_CPU_HALTED);
1998 }
1999 
2000 /*
2001  * Service a delayed TLB flush if coming out of being idle.
2002  * It will be called from cpu idle notification with interrupt disabled.
2003  */
2004 void
2005 tlb_service(void)
2006 {
2007 	ulong_t tlb_info;
2008 	ulong_t found;
2009 
2010 	/*
2011 	 * We only have to do something if coming out of being idle.
2012 	 */
2013 	tlb_info = CPU->cpu_m.mcpu_tlb_info;
2014 	if (tlb_info & TLB_CPU_HALTED) {
2015 		ASSERT(CPU->cpu_current_hat == kas.a_hat);
2016 
2017 		/*
2018 		 * Atomic clear and fetch of old state.
2019 		 */
2020 		while ((found = CAS_TLB_INFO(CPU, tlb_info, 0)) != tlb_info) {
2021 			ASSERT(found & TLB_CPU_HALTED);
2022 			tlb_info = found;
2023 			SMT_PAUSE();
2024 		}
2025 		if (tlb_info & TLB_INVAL_ALL)
2026 			flush_all_tlb_entries();
2027 	}
2028 }
2029 #endif /* !__xpv */
2030 
2031 /*
2032  * Internal routine to do cross calls to invalidate a range of pages on
2033  * all CPUs using a given hat.
2034  */
2035 void
2036 hat_tlb_inval(hat_t *hat, uintptr_t va)
2037 {
2038 	extern int	flushes_require_xcalls;	/* from mp_startup.c */
2039 	cpuset_t	justme;
2040 	cpuset_t	cpus_to_shootdown;
2041 #ifndef __xpv
2042 	cpuset_t	check_cpus;
2043 	cpu_t		*cpup;
2044 	int		c;
2045 #endif
2046 
2047 	/*
2048 	 * If the hat is being destroyed, there are no more users, so
2049 	 * demap need not do anything.
2050 	 */
2051 	if (hat->hat_flags & HAT_FREEING)
2052 		return;
2053 
2054 	/*
2055 	 * If demapping from a shared pagetable, we best demap the
2056 	 * entire set of user TLBs, since we don't know what addresses
2057 	 * these were shared at.
2058 	 */
2059 	if (hat->hat_flags & HAT_SHARED) {
2060 		hat = kas.a_hat;
2061 		va = DEMAP_ALL_ADDR;
2062 	}
2063 
2064 	/*
2065 	 * if not running with multiple CPUs, don't use cross calls
2066 	 */
2067 	if (panicstr || !flushes_require_xcalls) {
2068 #ifdef __xpv
2069 		if (va == DEMAP_ALL_ADDR)
2070 			xen_flush_tlb();
2071 		else
2072 			xen_flush_va((caddr_t)va);
2073 #else
2074 		(void) hati_demap_func((xc_arg_t)hat, (xc_arg_t)va, NULL);
2075 #endif
2076 		return;
2077 	}
2078 
2079 
2080 	/*
2081 	 * Determine CPUs to shootdown. Kernel changes always do all CPUs.
2082 	 * Otherwise it's just CPUs currently executing in this hat.
2083 	 */
2084 	kpreempt_disable();
2085 	CPUSET_ONLY(justme, CPU->cpu_id);
2086 	if (hat == kas.a_hat)
2087 		cpus_to_shootdown = khat_cpuset;
2088 	else
2089 		cpus_to_shootdown = hat->hat_cpus;
2090 
2091 #ifndef __xpv
2092 	/*
2093 	 * If any CPUs in the set are idle, just request a delayed flush
2094 	 * and avoid waking them up.
2095 	 */
2096 	check_cpus = cpus_to_shootdown;
2097 	for (c = 0; c < NCPU && !CPUSET_ISNULL(check_cpus); ++c) {
2098 		ulong_t tlb_info;
2099 
2100 		if (!CPU_IN_SET(check_cpus, c))
2101 			continue;
2102 		CPUSET_DEL(check_cpus, c);
2103 		cpup = cpu[c];
2104 		if (cpup == NULL)
2105 			continue;
2106 
2107 		tlb_info = cpup->cpu_m.mcpu_tlb_info;
2108 		while (tlb_info == TLB_CPU_HALTED) {
2109 			(void) CAS_TLB_INFO(cpup, TLB_CPU_HALTED,
2110 			    TLB_CPU_HALTED | TLB_INVAL_ALL);
2111 			SMT_PAUSE();
2112 			tlb_info = cpup->cpu_m.mcpu_tlb_info;
2113 		}
2114 		if (tlb_info == (TLB_CPU_HALTED | TLB_INVAL_ALL)) {
2115 			HATSTAT_INC(hs_tlb_inval_delayed);
2116 			CPUSET_DEL(cpus_to_shootdown, c);
2117 		}
2118 	}
2119 #endif
2120 
2121 	if (CPUSET_ISNULL(cpus_to_shootdown) ||
2122 	    CPUSET_ISEQUAL(cpus_to_shootdown, justme)) {
2123 
2124 #ifdef __xpv
2125 		if (va == DEMAP_ALL_ADDR)
2126 			xen_flush_tlb();
2127 		else
2128 			xen_flush_va((caddr_t)va);
2129 #else
2130 		(void) hati_demap_func((xc_arg_t)hat, (xc_arg_t)va, NULL);
2131 #endif
2132 
2133 	} else {
2134 
2135 		CPUSET_ADD(cpus_to_shootdown, CPU->cpu_id);
2136 #ifdef __xpv
2137 		if (va == DEMAP_ALL_ADDR)
2138 			xen_gflush_tlb(cpus_to_shootdown);
2139 		else
2140 			xen_gflush_va((caddr_t)va, cpus_to_shootdown);
2141 #else
2142 		xc_call((xc_arg_t)hat, (xc_arg_t)va, NULL,
2143 		    CPUSET2BV(cpus_to_shootdown), hati_demap_func);
2144 #endif
2145 
2146 	}
2147 	kpreempt_enable();
2148 }
2149 
2150 /*
2151  * Interior routine for HAT_UNLOADs from hat_unload_callback(),
2152  * hat_kmap_unload() OR from hat_steal() code.  This routine doesn't
2153  * handle releasing of the htables.
2154  */
2155 void
2156 hat_pte_unmap(
2157 	htable_t	*ht,
2158 	uint_t		entry,
2159 	uint_t		flags,
2160 	x86pte_t	old_pte,
2161 	void		*pte_ptr)
2162 {
2163 	hat_t		*hat = ht->ht_hat;
2164 	hment_t		*hm = NULL;
2165 	page_t		*pp = NULL;
2166 	level_t		l = ht->ht_level;
2167 	pfn_t		pfn;
2168 
2169 	/*
2170 	 * We always track the locking counts, even if nothing is unmapped
2171 	 */
2172 	if ((flags & HAT_UNLOAD_UNLOCK) != 0 && hat != kas.a_hat) {
2173 		ASSERT(ht->ht_lock_cnt > 0);
2174 		HTABLE_LOCK_DEC(ht);
2175 	}
2176 
2177 	/*
2178 	 * Figure out which page's mapping list lock to acquire using the PFN
2179 	 * passed in "old" PTE. We then attempt to invalidate the PTE.
2180 	 * If another thread, probably a hat_pageunload, has asynchronously
2181 	 * unmapped/remapped this address we'll loop here.
2182 	 */
2183 	ASSERT(ht->ht_busy > 0);
2184 	while (PTE_ISVALID(old_pte)) {
2185 		pfn = PTE2PFN(old_pte, l);
2186 		if (PTE_GET(old_pte, PT_SOFTWARE) >= PT_NOCONSIST) {
2187 			pp = NULL;
2188 		} else {
2189 #ifdef __xpv
2190 			if (pfn == PFN_INVALID)
2191 				panic("Invalid PFN, but not PT_NOCONSIST");
2192 #endif
2193 			pp = page_numtopp_nolock(pfn);
2194 			if (pp == NULL) {
2195 				panic("no page_t, not NOCONSIST: old_pte="
2196 				    FMT_PTE " ht=%lx entry=0x%x pte_ptr=%lx",
2197 				    old_pte, (uintptr_t)ht, entry,
2198 				    (uintptr_t)pte_ptr);
2199 			}
2200 			x86_hm_enter(pp);
2201 		}
2202 
2203 		/*
2204 		 * If freeing the address space, check that the PTE
2205 		 * hasn't changed, as the mappings are no longer in use by
2206 		 * any thread, invalidation is unnecessary.
2207 		 * If not freeing, do a full invalidate.
2208 		 *
2209 		 * On the hypervisor we must always remove mappings, as a
2210 		 * writable mapping left behind could cause a page table
2211 		 * allocation to fail.
2212 		 */
2213 #if !defined(__xpv)
2214 		if (hat->hat_flags & HAT_FREEING)
2215 			old_pte = x86pte_get(ht, entry);
2216 		else
2217 #endif
2218 			old_pte = x86pte_inval(ht, entry, old_pte, pte_ptr);
2219 
2220 		/*
2221 		 * If the page hadn't changed we've unmapped it and can proceed
2222 		 */
2223 		if (PTE_ISVALID(old_pte) && PTE2PFN(old_pte, l) == pfn)
2224 			break;
2225 
2226 		/*
2227 		 * Otherwise, we'll have to retry with the current old_pte.
2228 		 * Drop the hment lock, since the pfn may have changed.
2229 		 */
2230 		if (pp != NULL) {
2231 			x86_hm_exit(pp);
2232 			pp = NULL;
2233 		} else {
2234 			ASSERT(PTE_GET(old_pte, PT_SOFTWARE) >= PT_NOCONSIST);
2235 		}
2236 	}
2237 
2238 	/*
2239 	 * If the old mapping wasn't valid, there's nothing more to do
2240 	 */
2241 	if (!PTE_ISVALID(old_pte)) {
2242 		if (pp != NULL)
2243 			x86_hm_exit(pp);
2244 		return;
2245 	}
2246 
2247 	/*
2248 	 * Take care of syncing any MOD/REF bits and removing the hment.
2249 	 */
2250 	if (pp != NULL) {
2251 		if (!(flags & HAT_UNLOAD_NOSYNC))
2252 			hati_sync_pte_to_page(pp, old_pte, l);
2253 		hm = hment_remove(pp, ht, entry);
2254 		x86_hm_exit(pp);
2255 		if (hm != NULL)
2256 			hment_free(hm);
2257 	}
2258 
2259 	/*
2260 	 * Handle book keeping in the htable and hat
2261 	 */
2262 	ASSERT(ht->ht_valid_cnt > 0);
2263 	HTABLE_DEC(ht->ht_valid_cnt);
2264 	PGCNT_DEC(hat, l);
2265 }
2266 
2267 /*
2268  * very cheap unload implementation to special case some kernel addresses
2269  */
2270 static void
2271 hat_kmap_unload(caddr_t addr, size_t len, uint_t flags)
2272 {
2273 	uintptr_t	va = (uintptr_t)addr;
2274 	uintptr_t	eva = va + len;
2275 	pgcnt_t		pg_index;
2276 	htable_t	*ht;
2277 	uint_t		entry;
2278 	x86pte_t	*pte_ptr;
2279 	x86pte_t	old_pte;
2280 
2281 	for (; va < eva; va += MMU_PAGESIZE) {
2282 		/*
2283 		 * Get the PTE
2284 		 */
2285 		pg_index = mmu_btop(va - mmu.kmap_addr);
2286 		pte_ptr = PT_INDEX_PTR(mmu.kmap_ptes, pg_index);
2287 		old_pte = GET_PTE(pte_ptr);
2288 
2289 		/*
2290 		 * get the htable / entry
2291 		 */
2292 		ht = mmu.kmap_htables[(va - mmu.kmap_htables[0]->ht_vaddr)
2293 		    >> LEVEL_SHIFT(1)];
2294 		entry = htable_va2entry(va, ht);
2295 
2296 		/*
2297 		 * use mostly common code to unmap it.
2298 		 */
2299 		hat_pte_unmap(ht, entry, flags, old_pte, pte_ptr);
2300 	}
2301 }
2302 
2303 
2304 /*
2305  * unload a range of virtual address space (no callback)
2306  */
2307 void
2308 hat_unload(hat_t *hat, caddr_t addr, size_t len, uint_t flags)
2309 {
2310 	uintptr_t va = (uintptr_t)addr;
2311 
2312 	XPV_DISALLOW_MIGRATE();
2313 	ASSERT(hat == kas.a_hat || va + len <= _userlimit);
2314 
2315 	/*
2316 	 * special case for performance.
2317 	 */
2318 	if (mmu.kmap_addr <= va && va < mmu.kmap_eaddr) {
2319 		ASSERT(hat == kas.a_hat);
2320 		hat_kmap_unload(addr, len, flags);
2321 	} else {
2322 		hat_unload_callback(hat, addr, len, flags, NULL);
2323 	}
2324 	XPV_ALLOW_MIGRATE();
2325 }
2326 
2327 /*
2328  * Do the callbacks for ranges being unloaded.
2329  */
2330 typedef struct range_info {
2331 	uintptr_t	rng_va;
2332 	ulong_t		rng_cnt;
2333 	level_t		rng_level;
2334 } range_info_t;
2335 
2336 static void
2337 handle_ranges(hat_callback_t *cb, uint_t cnt, range_info_t *range)
2338 {
2339 	/*
2340 	 * do callbacks to upper level VM system
2341 	 */
2342 	while (cb != NULL && cnt > 0) {
2343 		--cnt;
2344 		cb->hcb_start_addr = (caddr_t)range[cnt].rng_va;
2345 		cb->hcb_end_addr = cb->hcb_start_addr;
2346 		cb->hcb_end_addr +=
2347 		    range[cnt].rng_cnt << LEVEL_SIZE(range[cnt].rng_level);
2348 		cb->hcb_function(cb);
2349 	}
2350 }
2351 
2352 /*
2353  * Unload a given range of addresses (has optional callback)
2354  *
2355  * Flags:
2356  * define	HAT_UNLOAD		0x00
2357  * define	HAT_UNLOAD_NOSYNC	0x02
2358  * define	HAT_UNLOAD_UNLOCK	0x04
2359  * define	HAT_UNLOAD_OTHER	0x08 - not used
2360  * define	HAT_UNLOAD_UNMAP	0x10 - same as HAT_UNLOAD
2361  */
2362 #define	MAX_UNLOAD_CNT (8)
2363 void
2364 hat_unload_callback(
2365 	hat_t		*hat,
2366 	caddr_t		addr,
2367 	size_t		len,
2368 	uint_t		flags,
2369 	hat_callback_t	*cb)
2370 {
2371 	uintptr_t	vaddr = (uintptr_t)addr;
2372 	uintptr_t	eaddr = vaddr + len;
2373 	htable_t	*ht = NULL;
2374 	uint_t		entry;
2375 	uintptr_t	contig_va = (uintptr_t)-1L;
2376 	range_info_t	r[MAX_UNLOAD_CNT];
2377 	uint_t		r_cnt = 0;
2378 	x86pte_t	old_pte;
2379 
2380 	XPV_DISALLOW_MIGRATE();
2381 	ASSERT(hat == kas.a_hat || eaddr <= _userlimit);
2382 	ASSERT(IS_PAGEALIGNED(vaddr));
2383 	ASSERT(IS_PAGEALIGNED(eaddr));
2384 
2385 	/*
2386 	 * Special case a single page being unloaded for speed. This happens
2387 	 * quite frequently, COW faults after a fork() for example.
2388 	 */
2389 	if (cb == NULL && len == MMU_PAGESIZE) {
2390 		ht = htable_getpte(hat, vaddr, &entry, &old_pte, 0);
2391 		if (ht != NULL) {
2392 			if (PTE_ISVALID(old_pte))
2393 				hat_pte_unmap(ht, entry, flags, old_pte, NULL);
2394 			htable_release(ht);
2395 		}
2396 		XPV_ALLOW_MIGRATE();
2397 		return;
2398 	}
2399 
2400 	while (vaddr < eaddr) {
2401 		old_pte = htable_walk(hat, &ht, &vaddr, eaddr);
2402 		if (ht == NULL)
2403 			break;
2404 
2405 		ASSERT(!IN_VA_HOLE(vaddr));
2406 
2407 		if (vaddr < (uintptr_t)addr)
2408 			panic("hat_unload_callback(): unmap inside large page");
2409 
2410 		/*
2411 		 * We'll do the call backs for contiguous ranges
2412 		 */
2413 		if (vaddr != contig_va ||
2414 		    (r_cnt > 0 && r[r_cnt - 1].rng_level != ht->ht_level)) {
2415 			if (r_cnt == MAX_UNLOAD_CNT) {
2416 				handle_ranges(cb, r_cnt, r);
2417 				r_cnt = 0;
2418 			}
2419 			r[r_cnt].rng_va = vaddr;
2420 			r[r_cnt].rng_cnt = 0;
2421 			r[r_cnt].rng_level = ht->ht_level;
2422 			++r_cnt;
2423 		}
2424 
2425 		/*
2426 		 * Unload one mapping from the page tables.
2427 		 */
2428 		entry = htable_va2entry(vaddr, ht);
2429 		hat_pte_unmap(ht, entry, flags, old_pte, NULL);
2430 		ASSERT(ht->ht_level <= mmu.max_page_level);
2431 		vaddr += LEVEL_SIZE(ht->ht_level);
2432 		contig_va = vaddr;
2433 		++r[r_cnt - 1].rng_cnt;
2434 	}
2435 	if (ht)
2436 		htable_release(ht);
2437 
2438 	/*
2439 	 * handle last range for callbacks
2440 	 */
2441 	if (r_cnt > 0)
2442 		handle_ranges(cb, r_cnt, r);
2443 	XPV_ALLOW_MIGRATE();
2444 }
2445 
2446 /*
2447  * Invalidate a virtual address translation on a slave CPU during
2448  * panic() dumps.
2449  */
2450 void
2451 hat_flush_range(hat_t *hat, caddr_t va, size_t size)
2452 {
2453 	ssize_t sz;
2454 	caddr_t endva = va + size;
2455 
2456 	while (va < endva) {
2457 		sz = hat_getpagesize(hat, va);
2458 		if (sz < 0) {
2459 #ifdef __xpv
2460 			xen_flush_tlb();
2461 #else
2462 			flush_all_tlb_entries();
2463 #endif
2464 			break;
2465 		}
2466 #ifdef __xpv
2467 		xen_flush_va(va);
2468 #else
2469 		mmu_tlbflush_entry(va);
2470 #endif
2471 		va += sz;
2472 	}
2473 }
2474 
2475 /*
2476  * synchronize mapping with software data structures
2477  *
2478  * This interface is currently only used by the working set monitor
2479  * driver.
2480  */
2481 /*ARGSUSED*/
2482 void
2483 hat_sync(hat_t *hat, caddr_t addr, size_t len, uint_t flags)
2484 {
2485 	uintptr_t	vaddr = (uintptr_t)addr;
2486 	uintptr_t	eaddr = vaddr + len;
2487 	htable_t	*ht = NULL;
2488 	uint_t		entry;
2489 	x86pte_t	pte;
2490 	x86pte_t	save_pte;
2491 	x86pte_t	new;
2492 	page_t		*pp;
2493 
2494 	ASSERT(!IN_VA_HOLE(vaddr));
2495 	ASSERT(IS_PAGEALIGNED(vaddr));
2496 	ASSERT(IS_PAGEALIGNED(eaddr));
2497 	ASSERT(hat == kas.a_hat || eaddr <= _userlimit);
2498 
2499 	XPV_DISALLOW_MIGRATE();
2500 	for (; vaddr < eaddr; vaddr += LEVEL_SIZE(ht->ht_level)) {
2501 try_again:
2502 		pte = htable_walk(hat, &ht, &vaddr, eaddr);
2503 		if (ht == NULL)
2504 			break;
2505 		entry = htable_va2entry(vaddr, ht);
2506 
2507 		if (PTE_GET(pte, PT_SOFTWARE) >= PT_NOSYNC ||
2508 		    PTE_GET(pte, PT_REF | PT_MOD) == 0)
2509 			continue;
2510 
2511 		/*
2512 		 * We need to acquire the mapping list lock to protect
2513 		 * against hat_pageunload(), hat_unload(), etc.
2514 		 */
2515 		pp = page_numtopp_nolock(PTE2PFN(pte, ht->ht_level));
2516 		if (pp == NULL)
2517 			break;
2518 		x86_hm_enter(pp);
2519 		save_pte = pte;
2520 		pte = x86pte_get(ht, entry);
2521 		if (pte != save_pte) {
2522 			x86_hm_exit(pp);
2523 			goto try_again;
2524 		}
2525 		if (PTE_GET(pte, PT_SOFTWARE) >= PT_NOSYNC ||
2526 		    PTE_GET(pte, PT_REF | PT_MOD) == 0) {
2527 			x86_hm_exit(pp);
2528 			continue;
2529 		}
2530 
2531 		/*
2532 		 * Need to clear ref or mod bits. We may compete with
2533 		 * hardware updating the R/M bits and have to try again.
2534 		 */
2535 		if (flags == HAT_SYNC_ZERORM) {
2536 			new = pte;
2537 			PTE_CLR(new, PT_REF | PT_MOD);
2538 			pte = hati_update_pte(ht, entry, pte, new);
2539 			if (pte != 0) {
2540 				x86_hm_exit(pp);
2541 				goto try_again;
2542 			}
2543 		} else {
2544 			/*
2545 			 * sync the PTE to the page_t
2546 			 */
2547 			hati_sync_pte_to_page(pp, save_pte, ht->ht_level);
2548 		}
2549 		x86_hm_exit(pp);
2550 	}
2551 	if (ht)
2552 		htable_release(ht);
2553 	XPV_ALLOW_MIGRATE();
2554 }
2555 
2556 /*
2557  * void	hat_map(hat, addr, len, flags)
2558  */
2559 /*ARGSUSED*/
2560 void
2561 hat_map(hat_t *hat, caddr_t addr, size_t len, uint_t flags)
2562 {
2563 	/* does nothing */
2564 }
2565 
2566 /*
2567  * uint_t hat_getattr(hat, addr, *attr)
2568  *	returns attr for <hat,addr> in *attr.  returns 0 if there was a
2569  *	mapping and *attr is valid, nonzero if there was no mapping and
2570  *	*attr is not valid.
2571  */
2572 uint_t
2573 hat_getattr(hat_t *hat, caddr_t addr, uint_t *attr)
2574 {
2575 	uintptr_t	vaddr = ALIGN2PAGE(addr);
2576 	htable_t	*ht = NULL;
2577 	x86pte_t	pte;
2578 
2579 	ASSERT(hat == kas.a_hat || vaddr <= _userlimit);
2580 
2581 	if (IN_VA_HOLE(vaddr))
2582 		return ((uint_t)-1);
2583 
2584 	ht = htable_getpte(hat, vaddr, NULL, &pte, mmu.max_page_level);
2585 	if (ht == NULL)
2586 		return ((uint_t)-1);
2587 
2588 	if (!PTE_ISVALID(pte) || !PTE_ISPAGE(pte, ht->ht_level)) {
2589 		htable_release(ht);
2590 		return ((uint_t)-1);
2591 	}
2592 
2593 	*attr = PROT_READ;
2594 	if (PTE_GET(pte, PT_WRITABLE))
2595 		*attr |= PROT_WRITE;
2596 	if (PTE_GET(pte, PT_USER))
2597 		*attr |= PROT_USER;
2598 	if (!PTE_GET(pte, mmu.pt_nx))
2599 		*attr |= PROT_EXEC;
2600 	if (PTE_GET(pte, PT_SOFTWARE) >= PT_NOSYNC)
2601 		*attr |= HAT_NOSYNC;
2602 	htable_release(ht);
2603 	return (0);
2604 }
2605 
2606 /*
2607  * hat_updateattr() applies the given attribute change to an existing mapping
2608  */
2609 #define	HAT_LOAD_ATTR		1
2610 #define	HAT_SET_ATTR		2
2611 #define	HAT_CLR_ATTR		3
2612 
2613 static void
2614 hat_updateattr(hat_t *hat, caddr_t addr, size_t len, uint_t attr, int what)
2615 {
2616 	uintptr_t	vaddr = (uintptr_t)addr;
2617 	uintptr_t	eaddr = (uintptr_t)addr + len;
2618 	htable_t	*ht = NULL;
2619 	uint_t		entry;
2620 	x86pte_t	oldpte, newpte;
2621 	page_t		*pp;
2622 
2623 	XPV_DISALLOW_MIGRATE();
2624 	ASSERT(IS_PAGEALIGNED(vaddr));
2625 	ASSERT(IS_PAGEALIGNED(eaddr));
2626 	ASSERT(hat == kas.a_hat ||
2627 	    AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock));
2628 	for (; vaddr < eaddr; vaddr += LEVEL_SIZE(ht->ht_level)) {
2629 try_again:
2630 		oldpte = htable_walk(hat, &ht, &vaddr, eaddr);
2631 		if (ht == NULL)
2632 			break;
2633 		if (PTE_GET(oldpte, PT_SOFTWARE) >= PT_NOCONSIST)
2634 			continue;
2635 
2636 		pp = page_numtopp_nolock(PTE2PFN(oldpte, ht->ht_level));
2637 		if (pp == NULL)
2638 			continue;
2639 		x86_hm_enter(pp);
2640 
2641 		newpte = oldpte;
2642 		/*
2643 		 * We found a page table entry in the desired range,
2644 		 * figure out the new attributes.
2645 		 */
2646 		if (what == HAT_SET_ATTR || what == HAT_LOAD_ATTR) {
2647 			if ((attr & PROT_WRITE) &&
2648 			    !PTE_GET(oldpte, PT_WRITABLE))
2649 				newpte |= PT_WRITABLE;
2650 
2651 			if ((attr & HAT_NOSYNC) &&
2652 			    PTE_GET(oldpte, PT_SOFTWARE) < PT_NOSYNC)
2653 				newpte |= PT_NOSYNC;
2654 
2655 			if ((attr & PROT_EXEC) && PTE_GET(oldpte, mmu.pt_nx))
2656 				newpte &= ~mmu.pt_nx;
2657 		}
2658 
2659 		if (what == HAT_LOAD_ATTR) {
2660 			if (!(attr & PROT_WRITE) &&
2661 			    PTE_GET(oldpte, PT_WRITABLE))
2662 				newpte &= ~PT_WRITABLE;
2663 
2664 			if (!(attr & HAT_NOSYNC) &&
2665 			    PTE_GET(oldpte, PT_SOFTWARE) >= PT_NOSYNC)
2666 				newpte &= ~PT_SOFTWARE;
2667 
2668 			if (!(attr & PROT_EXEC) && !PTE_GET(oldpte, mmu.pt_nx))
2669 				newpte |= mmu.pt_nx;
2670 		}
2671 
2672 		if (what == HAT_CLR_ATTR) {
2673 			if ((attr & PROT_WRITE) && PTE_GET(oldpte, PT_WRITABLE))
2674 				newpte &= ~PT_WRITABLE;
2675 
2676 			if ((attr & HAT_NOSYNC) &&
2677 			    PTE_GET(oldpte, PT_SOFTWARE) >= PT_NOSYNC)
2678 				newpte &= ~PT_SOFTWARE;
2679 
2680 			if ((attr & PROT_EXEC) && !PTE_GET(oldpte, mmu.pt_nx))
2681 				newpte |= mmu.pt_nx;
2682 		}
2683 
2684 		/*
2685 		 * Ensure NOSYNC/NOCONSIST mappings have REF and MOD set.
2686 		 * x86pte_set() depends on this.
2687 		 */
2688 		if (PTE_GET(newpte, PT_SOFTWARE) >= PT_NOSYNC)
2689 			newpte |= PT_REF | PT_MOD;
2690 
2691 		/*
2692 		 * what about PROT_READ or others? this code only handles:
2693 		 * EXEC, WRITE, NOSYNC
2694 		 */
2695 
2696 		/*
2697 		 * If new PTE really changed, update the table.
2698 		 */
2699 		if (newpte != oldpte) {
2700 			entry = htable_va2entry(vaddr, ht);
2701 			oldpte = hati_update_pte(ht, entry, oldpte, newpte);
2702 			if (oldpte != 0) {
2703 				x86_hm_exit(pp);
2704 				goto try_again;
2705 			}
2706 		}
2707 		x86_hm_exit(pp);
2708 	}
2709 	if (ht)
2710 		htable_release(ht);
2711 	XPV_ALLOW_MIGRATE();
2712 }
2713 
2714 /*
2715  * Various wrappers for hat_updateattr()
2716  */
2717 void
2718 hat_setattr(hat_t *hat, caddr_t addr, size_t len, uint_t attr)
2719 {
2720 	ASSERT(hat == kas.a_hat || (uintptr_t)addr + len <= _userlimit);
2721 	hat_updateattr(hat, addr, len, attr, HAT_SET_ATTR);
2722 }
2723 
2724 void
2725 hat_clrattr(hat_t *hat, caddr_t addr, size_t len, uint_t attr)
2726 {
2727 	ASSERT(hat == kas.a_hat || (uintptr_t)addr + len <= _userlimit);
2728 	hat_updateattr(hat, addr, len, attr, HAT_CLR_ATTR);
2729 }
2730 
2731 void
2732 hat_chgattr(hat_t *hat, caddr_t addr, size_t len, uint_t attr)
2733 {
2734 	ASSERT(hat == kas.a_hat || (uintptr_t)addr + len <= _userlimit);
2735 	hat_updateattr(hat, addr, len, attr, HAT_LOAD_ATTR);
2736 }
2737 
2738 void
2739 hat_chgprot(hat_t *hat, caddr_t addr, size_t len, uint_t vprot)
2740 {
2741 	ASSERT(hat == kas.a_hat || (uintptr_t)addr + len <= _userlimit);
2742 	hat_updateattr(hat, addr, len, vprot & HAT_PROT_MASK, HAT_LOAD_ATTR);
2743 }
2744 
2745 /*
2746  * size_t hat_getpagesize(hat, addr)
2747  *	returns pagesize in bytes for <hat, addr>. returns -1 of there is
2748  *	no mapping. This is an advisory call.
2749  */
2750 ssize_t
2751 hat_getpagesize(hat_t *hat, caddr_t addr)
2752 {
2753 	uintptr_t	vaddr = ALIGN2PAGE(addr);
2754 	htable_t	*ht;
2755 	size_t		pagesize;
2756 
2757 	ASSERT(hat == kas.a_hat || vaddr <= _userlimit);
2758 	if (IN_VA_HOLE(vaddr))
2759 		return (-1);
2760 	ht = htable_getpage(hat, vaddr, NULL);
2761 	if (ht == NULL)
2762 		return (-1);
2763 	pagesize = LEVEL_SIZE(ht->ht_level);
2764 	htable_release(ht);
2765 	return (pagesize);
2766 }
2767 
2768 
2769 
2770 /*
2771  * pfn_t hat_getpfnum(hat, addr)
2772  *	returns pfn for <hat, addr> or PFN_INVALID if mapping is invalid.
2773  */
2774 pfn_t
2775 hat_getpfnum(hat_t *hat, caddr_t addr)
2776 {
2777 	uintptr_t	vaddr = ALIGN2PAGE(addr);
2778 	htable_t	*ht;
2779 	uint_t		entry;
2780 	pfn_t		pfn = PFN_INVALID;
2781 
2782 	ASSERT(hat == kas.a_hat || vaddr <= _userlimit);
2783 	if (khat_running == 0)
2784 		return (PFN_INVALID);
2785 
2786 	if (IN_VA_HOLE(vaddr))
2787 		return (PFN_INVALID);
2788 
2789 	XPV_DISALLOW_MIGRATE();
2790 	/*
2791 	 * A very common use of hat_getpfnum() is from the DDI for kernel pages.
2792 	 * Use the kmap_ptes (which also covers the 32 bit heap) to speed
2793 	 * this up.
2794 	 */
2795 	if (mmu.kmap_addr <= vaddr && vaddr < mmu.kmap_eaddr) {
2796 		x86pte_t pte;
2797 		pgcnt_t pg_index;
2798 
2799 		pg_index = mmu_btop(vaddr - mmu.kmap_addr);
2800 		pte = GET_PTE(PT_INDEX_PTR(mmu.kmap_ptes, pg_index));
2801 		if (PTE_ISVALID(pte))
2802 			/*LINTED [use of constant 0 causes a lint warning] */
2803 			pfn = PTE2PFN(pte, 0);
2804 		XPV_ALLOW_MIGRATE();
2805 		return (pfn);
2806 	}
2807 
2808 	ht = htable_getpage(hat, vaddr, &entry);
2809 	if (ht == NULL) {
2810 		XPV_ALLOW_MIGRATE();
2811 		return (PFN_INVALID);
2812 	}
2813 	ASSERT(vaddr >= ht->ht_vaddr);
2814 	ASSERT(vaddr <= HTABLE_LAST_PAGE(ht));
2815 	pfn = PTE2PFN(x86pte_get(ht, entry), ht->ht_level);
2816 	if (ht->ht_level > 0)
2817 		pfn += mmu_btop(vaddr & LEVEL_OFFSET(ht->ht_level));
2818 	htable_release(ht);
2819 	XPV_ALLOW_MIGRATE();
2820 	return (pfn);
2821 }
2822 
2823 /*
2824  * hat_getkpfnum() is an obsolete DDI routine, and its use is discouraged.
2825  * Use hat_getpfnum(kas.a_hat, ...) instead.
2826  *
2827  * We'd like to return PFN_INVALID if the mappings have underlying page_t's
2828  * but can't right now due to the fact that some software has grown to use
2829  * this interface incorrectly. So for now when the interface is misused,
2830  * return a warning to the user that in the future it won't work in the
2831  * way they're abusing it, and carry on.
2832  *
2833  * Note that hat_getkpfnum() is never supported on amd64.
2834  */
2835 #if !defined(__amd64)
2836 pfn_t
2837 hat_getkpfnum(caddr_t addr)
2838 {
2839 	pfn_t	pfn;
2840 	int badcaller = 0;
2841 
2842 	if (khat_running == 0)
2843 		panic("hat_getkpfnum(): called too early\n");
2844 	if ((uintptr_t)addr < kernelbase)
2845 		return (PFN_INVALID);
2846 
2847 	XPV_DISALLOW_MIGRATE();
2848 	if (segkpm && IS_KPM_ADDR(addr)) {
2849 		badcaller = 1;
2850 		pfn = hat_kpm_va2pfn(addr);
2851 	} else {
2852 		pfn = hat_getpfnum(kas.a_hat, addr);
2853 		badcaller = pf_is_memory(pfn);
2854 	}
2855 
2856 	if (badcaller)
2857 		hat_getkpfnum_badcall(caller());
2858 	XPV_ALLOW_MIGRATE();
2859 	return (pfn);
2860 }
2861 #endif /* __amd64 */
2862 
2863 /*
2864  * int hat_probe(hat, addr)
2865  *	return 0 if no valid mapping is present.  Faster version
2866  *	of hat_getattr in certain architectures.
2867  */
2868 int
2869 hat_probe(hat_t *hat, caddr_t addr)
2870 {
2871 	uintptr_t	vaddr = ALIGN2PAGE(addr);
2872 	uint_t		entry;
2873 	htable_t	*ht;
2874 	pgcnt_t		pg_off;
2875 
2876 	ASSERT(hat == kas.a_hat || vaddr <= _userlimit);
2877 	ASSERT(hat == kas.a_hat ||
2878 	    AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock));
2879 	if (IN_VA_HOLE(vaddr))
2880 		return (0);
2881 
2882 	/*
2883 	 * Most common use of hat_probe is from segmap. We special case it
2884 	 * for performance.
2885 	 */
2886 	if (mmu.kmap_addr <= vaddr && vaddr < mmu.kmap_eaddr) {
2887 		pg_off = mmu_btop(vaddr - mmu.kmap_addr);
2888 		if (mmu.pae_hat)
2889 			return (PTE_ISVALID(mmu.kmap_ptes[pg_off]));
2890 		else
2891 			return (PTE_ISVALID(
2892 			    ((x86pte32_t *)mmu.kmap_ptes)[pg_off]));
2893 	}
2894 
2895 	ht = htable_getpage(hat, vaddr, &entry);
2896 	htable_release(ht);
2897 	return (ht != NULL);
2898 }
2899 
2900 /*
2901  * Find out if the segment for hat_share()/hat_unshare() is DISM or locked ISM.
2902  */
2903 static int
2904 is_it_dism(hat_t *hat, caddr_t va)
2905 {
2906 	struct seg *seg;
2907 	struct shm_data *shmd;
2908 	struct spt_data *sptd;
2909 
2910 	seg = as_findseg(hat->hat_as, va, 0);
2911 	ASSERT(seg != NULL);
2912 	ASSERT(seg->s_base <= va);
2913 	shmd = (struct shm_data *)seg->s_data;
2914 	ASSERT(shmd != NULL);
2915 	sptd = (struct spt_data *)shmd->shm_sptseg->s_data;
2916 	ASSERT(sptd != NULL);
2917 	if (sptd->spt_flags & SHM_PAGEABLE)
2918 		return (1);
2919 	return (0);
2920 }
2921 
2922 /*
2923  * Simple implementation of ISM. hat_share() is similar to hat_memload_array(),
2924  * except that we use the ism_hat's existing mappings to determine the pages
2925  * and protections to use for this hat. If we find a full properly aligned
2926  * and sized pagetable, we will attempt to share the pagetable itself.
2927  */
2928 /*ARGSUSED*/
2929 int
2930 hat_share(
2931 	hat_t		*hat,
2932 	caddr_t		addr,
2933 	hat_t		*ism_hat,
2934 	caddr_t		src_addr,
2935 	size_t		len,	/* almost useless value, see below.. */
2936 	uint_t		ismszc)
2937 {
2938 	uintptr_t	vaddr_start = (uintptr_t)addr;
2939 	uintptr_t	vaddr;
2940 	uintptr_t	eaddr = vaddr_start + len;
2941 	uintptr_t	ism_addr_start = (uintptr_t)src_addr;
2942 	uintptr_t	ism_addr = ism_addr_start;
2943 	uintptr_t	e_ism_addr = ism_addr + len;
2944 	htable_t	*ism_ht = NULL;
2945 	htable_t	*ht;
2946 	x86pte_t	pte;
2947 	page_t		*pp;
2948 	pfn_t		pfn;
2949 	level_t		l;
2950 	pgcnt_t		pgcnt;
2951 	uint_t		prot;
2952 	int		is_dism;
2953 	int		flags;
2954 
2955 	/*
2956 	 * We might be asked to share an empty DISM hat by as_dup()
2957 	 */
2958 	ASSERT(hat != kas.a_hat);
2959 	ASSERT(eaddr <= _userlimit);
2960 	if (!(ism_hat->hat_flags & HAT_SHARED)) {
2961 		ASSERT(hat_get_mapped_size(ism_hat) == 0);
2962 		return (0);
2963 	}
2964 	XPV_DISALLOW_MIGRATE();
2965 
2966 	/*
2967 	 * The SPT segment driver often passes us a size larger than there are
2968 	 * valid mappings. That's because it rounds the segment size up to a
2969 	 * large pagesize, even if the actual memory mapped by ism_hat is less.
2970 	 */
2971 	ASSERT(IS_PAGEALIGNED(vaddr_start));
2972 	ASSERT(IS_PAGEALIGNED(ism_addr_start));
2973 	ASSERT(ism_hat->hat_flags & HAT_SHARED);
2974 	is_dism = is_it_dism(hat, addr);
2975 	while (ism_addr < e_ism_addr) {
2976 		/*
2977 		 * use htable_walk to get the next valid ISM mapping
2978 		 */
2979 		pte = htable_walk(ism_hat, &ism_ht, &ism_addr, e_ism_addr);
2980 		if (ism_ht == NULL)
2981 			break;
2982 
2983 		/*
2984 		 * First check to see if we already share the page table.
2985 		 */
2986 		l = ism_ht->ht_level;
2987 		vaddr = vaddr_start + (ism_addr - ism_addr_start);
2988 		ht = htable_lookup(hat, vaddr, l);
2989 		if (ht != NULL) {
2990 			if (ht->ht_flags & HTABLE_SHARED_PFN)
2991 				goto shared;
2992 			htable_release(ht);
2993 			goto not_shared;
2994 		}
2995 
2996 		/*
2997 		 * Can't ever share top table.
2998 		 */
2999 		if (l == mmu.max_level)
3000 			goto not_shared;
3001 
3002 		/*
3003 		 * Avoid level mismatches later due to DISM faults.
3004 		 */
3005 		if (is_dism && l > 0)
3006 			goto not_shared;
3007 
3008 		/*
3009 		 * addresses and lengths must align
3010 		 * table must be fully populated
3011 		 * no lower level page tables
3012 		 */
3013 		if (ism_addr != ism_ht->ht_vaddr ||
3014 		    (vaddr & LEVEL_OFFSET(l + 1)) != 0)
3015 			goto not_shared;
3016 
3017 		/*
3018 		 * The range of address space must cover a full table.
3019 		 */
3020 		if (e_ism_addr - ism_addr < LEVEL_SIZE(l + 1))
3021 			goto not_shared;
3022 
3023 		/*
3024 		 * All entries in the ISM page table must be leaf PTEs.
3025 		 */
3026 		if (l > 0) {
3027 			int e;
3028 
3029 			/*
3030 			 * We know the 0th is from htable_walk() above.
3031 			 */
3032 			for (e = 1; e < HTABLE_NUM_PTES(ism_ht); ++e) {
3033 				x86pte_t pte;
3034 				pte = x86pte_get(ism_ht, e);
3035 				if (!PTE_ISPAGE(pte, l))
3036 					goto not_shared;
3037 			}
3038 		}
3039 
3040 		/*
3041 		 * share the page table
3042 		 */
3043 		ht = htable_create(hat, vaddr, l, ism_ht);
3044 shared:
3045 		ASSERT(ht->ht_flags & HTABLE_SHARED_PFN);
3046 		ASSERT(ht->ht_shares == ism_ht);
3047 		hat->hat_ism_pgcnt +=
3048 		    (ism_ht->ht_valid_cnt - ht->ht_valid_cnt) <<
3049 		    (LEVEL_SHIFT(ht->ht_level) - MMU_PAGESHIFT);
3050 		ht->ht_valid_cnt = ism_ht->ht_valid_cnt;
3051 		htable_release(ht);
3052 		ism_addr = ism_ht->ht_vaddr + LEVEL_SIZE(l + 1);
3053 		htable_release(ism_ht);
3054 		ism_ht = NULL;
3055 		continue;
3056 
3057 not_shared:
3058 		/*
3059 		 * Unable to share the page table. Instead we will
3060 		 * create new mappings from the values in the ISM mappings.
3061 		 * Figure out what level size mappings to use;
3062 		 */
3063 		for (l = ism_ht->ht_level; l > 0; --l) {
3064 			if (LEVEL_SIZE(l) <= eaddr - vaddr &&
3065 			    (vaddr & LEVEL_OFFSET(l)) == 0)
3066 				break;
3067 		}
3068 
3069 		/*
3070 		 * The ISM mapping might be larger than the share area,
3071 		 * be careful to truncate it if needed.
3072 		 */
3073 		if (eaddr - vaddr >= LEVEL_SIZE(ism_ht->ht_level)) {
3074 			pgcnt = mmu_btop(LEVEL_SIZE(ism_ht->ht_level));
3075 		} else {
3076 			pgcnt = mmu_btop(eaddr - vaddr);
3077 			l = 0;
3078 		}
3079 
3080 		pfn = PTE2PFN(pte, ism_ht->ht_level);
3081 		ASSERT(pfn != PFN_INVALID);
3082 		while (pgcnt > 0) {
3083 			/*
3084 			 * Make a new pte for the PFN for this level.
3085 			 * Copy protections for the pte from the ISM pte.
3086 			 */
3087 			pp = page_numtopp_nolock(pfn);
3088 			ASSERT(pp != NULL);
3089 
3090 			prot = PROT_USER | PROT_READ | HAT_UNORDERED_OK;
3091 			if (PTE_GET(pte, PT_WRITABLE))
3092 				prot |= PROT_WRITE;
3093 			if (!PTE_GET(pte, PT_NX))
3094 				prot |= PROT_EXEC;
3095 
3096 			flags = HAT_LOAD;
3097 			if (!is_dism)
3098 				flags |= HAT_LOAD_LOCK | HAT_LOAD_NOCONSIST;
3099 			while (hati_load_common(hat, vaddr, pp, prot, flags,
3100 			    l, pfn) != 0) {
3101 				if (l == 0)
3102 					panic("hati_load_common() failure");
3103 				--l;
3104 			}
3105 
3106 			vaddr += LEVEL_SIZE(l);
3107 			ism_addr += LEVEL_SIZE(l);
3108 			pfn += mmu_btop(LEVEL_SIZE(l));
3109 			pgcnt -= mmu_btop(LEVEL_SIZE(l));
3110 		}
3111 	}
3112 	if (ism_ht != NULL)
3113 		htable_release(ism_ht);
3114 	XPV_ALLOW_MIGRATE();
3115 	return (0);
3116 }
3117 
3118 
3119 /*
3120  * hat_unshare() is similar to hat_unload_callback(), but
3121  * we have to look for empty shared pagetables. Note that
3122  * hat_unshare() is always invoked against an entire segment.
3123  */
3124 /*ARGSUSED*/
3125 void
3126 hat_unshare(hat_t *hat, caddr_t addr, size_t len, uint_t ismszc)
3127 {
3128 	uint64_t	vaddr = (uintptr_t)addr;
3129 	uintptr_t	eaddr = vaddr + len;
3130 	htable_t	*ht = NULL;
3131 	uint_t		need_demaps = 0;
3132 	int		flags = HAT_UNLOAD_UNMAP;
3133 	level_t		l;
3134 
3135 	ASSERT(hat != kas.a_hat);
3136 	ASSERT(eaddr <= _userlimit);
3137 	ASSERT(IS_PAGEALIGNED(vaddr));
3138 	ASSERT(IS_PAGEALIGNED(eaddr));
3139 	XPV_DISALLOW_MIGRATE();
3140 
3141 	/*
3142 	 * First go through and remove any shared pagetables.
3143 	 *
3144 	 * Note that it's ok to delay the TLB shootdown till the entire range is
3145 	 * finished, because if hat_pageunload() were to unload a shared
3146 	 * pagetable page, its hat_tlb_inval() will do a global TLB invalidate.
3147 	 */
3148 	l = mmu.max_page_level;
3149 	if (l == mmu.max_level)
3150 		--l;
3151 	for (; l >= 0; --l) {
3152 		for (vaddr = (uintptr_t)addr; vaddr < eaddr;
3153 		    vaddr = (vaddr & LEVEL_MASK(l + 1)) + LEVEL_SIZE(l + 1)) {
3154 			ASSERT(!IN_VA_HOLE(vaddr));
3155 			/*
3156 			 * find a pagetable that maps the current address
3157 			 */
3158 			ht = htable_lookup(hat, vaddr, l);
3159 			if (ht == NULL)
3160 				continue;
3161 			if (ht->ht_flags & HTABLE_SHARED_PFN) {
3162 				/*
3163 				 * clear page count, set valid_cnt to 0,
3164 				 * let htable_release() finish the job
3165 				 */
3166 				hat->hat_ism_pgcnt -= ht->ht_valid_cnt <<
3167 				    (LEVEL_SHIFT(ht->ht_level) - MMU_PAGESHIFT);
3168 				ht->ht_valid_cnt = 0;
3169 				need_demaps = 1;
3170 			}
3171 			htable_release(ht);
3172 		}
3173 	}
3174 
3175 	/*
3176 	 * flush the TLBs - since we're probably dealing with MANY mappings
3177 	 * we do just one CR3 reload.
3178 	 */
3179 	if (!(hat->hat_flags & HAT_FREEING) && need_demaps)
3180 		hat_tlb_inval(hat, DEMAP_ALL_ADDR);
3181 
3182 	/*
3183 	 * Now go back and clean up any unaligned mappings that
3184 	 * couldn't share pagetables.
3185 	 */
3186 	if (!is_it_dism(hat, addr))
3187 		flags |= HAT_UNLOAD_UNLOCK;
3188 	hat_unload(hat, addr, len, flags);
3189 	XPV_ALLOW_MIGRATE();
3190 }
3191 
3192 
3193 /*
3194  * hat_reserve() does nothing
3195  */
3196 /*ARGSUSED*/
3197 void
3198 hat_reserve(struct as *as, caddr_t addr, size_t len)
3199 {
3200 }
3201 
3202 
3203 /*
3204  * Called when all mappings to a page should have write permission removed.
3205  * Mostly stolen from hat_pagesync()
3206  */
3207 static void
3208 hati_page_clrwrt(struct page *pp)
3209 {
3210 	hment_t		*hm = NULL;
3211 	htable_t	*ht;
3212 	uint_t		entry;
3213 	x86pte_t	old;
3214 	x86pte_t	new;
3215 	uint_t		pszc = 0;
3216 
3217 	XPV_DISALLOW_MIGRATE();
3218 next_size:
3219 	/*
3220 	 * walk thru the mapping list clearing write permission
3221 	 */
3222 	x86_hm_enter(pp);
3223 	while ((hm = hment_walk(pp, &ht, &entry, hm)) != NULL) {
3224 		if (ht->ht_level < pszc)
3225 			continue;
3226 		old = x86pte_get(ht, entry);
3227 
3228 		for (;;) {
3229 			/*
3230 			 * Is this mapping of interest?
3231 			 */
3232 			if (PTE2PFN(old, ht->ht_level) != pp->p_pagenum ||
3233 			    PTE_GET(old, PT_WRITABLE) == 0)
3234 				break;
3235 
3236 			/*
3237 			 * Clear ref/mod writable bits. This requires cross
3238 			 * calls to ensure any executing TLBs see cleared bits.
3239 			 */
3240 			new = old;
3241 			PTE_CLR(new, PT_REF | PT_MOD | PT_WRITABLE);
3242 			old = hati_update_pte(ht, entry, old, new);
3243 			if (old != 0)
3244 				continue;
3245 
3246 			break;
3247 		}
3248 	}
3249 	x86_hm_exit(pp);
3250 	while (pszc < pp->p_szc) {
3251 		page_t *tpp;
3252 		pszc++;
3253 		tpp = PP_GROUPLEADER(pp, pszc);
3254 		if (pp != tpp) {
3255 			pp = tpp;
3256 			goto next_size;
3257 		}
3258 	}
3259 	XPV_ALLOW_MIGRATE();
3260 }
3261 
3262 /*
3263  * void hat_page_setattr(pp, flag)
3264  * void hat_page_clrattr(pp, flag)
3265  *	used to set/clr ref/mod bits.
3266  */
3267 void
3268 hat_page_setattr(struct page *pp, uint_t flag)
3269 {
3270 	vnode_t		*vp = pp->p_vnode;
3271 	kmutex_t	*vphm = NULL;
3272 	page_t		**listp;
3273 	int		noshuffle;
3274 
3275 	noshuffle = flag & P_NSH;
3276 	flag &= ~P_NSH;
3277 
3278 	if (PP_GETRM(pp, flag) == flag)
3279 		return;
3280 
3281 	if ((flag & P_MOD) != 0 && vp != NULL && IS_VMODSORT(vp) &&
3282 	    !noshuffle) {
3283 		vphm = page_vnode_mutex(vp);
3284 		mutex_enter(vphm);
3285 	}
3286 
3287 	PP_SETRM(pp, flag);
3288 
3289 	if (vphm != NULL) {
3290 
3291 		/*
3292 		 * Some File Systems examine v_pages for NULL w/o
3293 		 * grabbing the vphm mutex. Must not let it become NULL when
3294 		 * pp is the only page on the list.
3295 		 */
3296 		if (pp->p_vpnext != pp) {
3297 			page_vpsub(&vp->v_pages, pp);
3298 			if (vp->v_pages != NULL)
3299 				listp = &vp->v_pages->p_vpprev->p_vpnext;
3300 			else
3301 				listp = &vp->v_pages;
3302 			page_vpadd(listp, pp);
3303 		}
3304 		mutex_exit(vphm);
3305 	}
3306 }
3307 
3308 void
3309 hat_page_clrattr(struct page *pp, uint_t flag)
3310 {
3311 	vnode_t		*vp = pp->p_vnode;
3312 	ASSERT(!(flag & ~(P_MOD | P_REF | P_RO)));
3313 
3314 	/*
3315 	 * Caller is expected to hold page's io lock for VMODSORT to work
3316 	 * correctly with pvn_vplist_dirty() and pvn_getdirty() when mod
3317 	 * bit is cleared.
3318 	 * We don't have assert to avoid tripping some existing third party
3319 	 * code. The dirty page is moved back to top of the v_page list
3320 	 * after IO is done in pvn_write_done().
3321 	 */
3322 	PP_CLRRM(pp, flag);
3323 
3324 	if ((flag & P_MOD) != 0 && vp != NULL && IS_VMODSORT(vp)) {
3325 
3326 		/*
3327 		 * VMODSORT works by removing write permissions and getting
3328 		 * a fault when a page is made dirty. At this point
3329 		 * we need to remove write permission from all mappings
3330 		 * to this page.
3331 		 */
3332 		hati_page_clrwrt(pp);
3333 	}
3334 }
3335 
3336 /*
3337  *	If flag is specified, returns 0 if attribute is disabled
3338  *	and non zero if enabled.  If flag specifes multiple attributes
3339  *	then returns 0 if ALL attributes are disabled.  This is an advisory
3340  *	call.
3341  */
3342 uint_t
3343 hat_page_getattr(struct page *pp, uint_t flag)
3344 {
3345 	return (PP_GETRM(pp, flag));
3346 }
3347 
3348 
3349 /*
3350  * common code used by hat_pageunload() and hment_steal()
3351  */
3352 hment_t *
3353 hati_page_unmap(page_t *pp, htable_t *ht, uint_t entry)
3354 {
3355 	x86pte_t old_pte;
3356 	pfn_t pfn = pp->p_pagenum;
3357 	hment_t *hm;
3358 
3359 	/*
3360 	 * We need to acquire a hold on the htable in order to
3361 	 * do the invalidate. We know the htable must exist, since
3362 	 * unmap's don't release the htable until after removing any
3363 	 * hment. Having x86_hm_enter() keeps that from proceeding.
3364 	 */
3365 	htable_acquire(ht);
3366 
3367 	/*
3368 	 * Invalidate the PTE and remove the hment.
3369 	 */
3370 	old_pte = x86pte_inval(ht, entry, 0, NULL);
3371 	if (PTE2PFN(old_pte, ht->ht_level) != pfn) {
3372 		panic("x86pte_inval() failure found PTE = " FMT_PTE
3373 		    " pfn being unmapped is %lx ht=0x%lx entry=0x%x",
3374 		    old_pte, pfn, (uintptr_t)ht, entry);
3375 	}
3376 
3377 	/*
3378 	 * Clean up all the htable information for this mapping
3379 	 */
3380 	ASSERT(ht->ht_valid_cnt > 0);
3381 	HTABLE_DEC(ht->ht_valid_cnt);
3382 	PGCNT_DEC(ht->ht_hat, ht->ht_level);
3383 
3384 	/*
3385 	 * sync ref/mod bits to the page_t
3386 	 */
3387 	if (PTE_GET(old_pte, PT_SOFTWARE) < PT_NOSYNC)
3388 		hati_sync_pte_to_page(pp, old_pte, ht->ht_level);
3389 
3390 	/*
3391 	 * Remove the mapping list entry for this page.
3392 	 */
3393 	hm = hment_remove(pp, ht, entry);
3394 
3395 	/*
3396 	 * drop the mapping list lock so that we might free the
3397 	 * hment and htable.
3398 	 */
3399 	x86_hm_exit(pp);
3400 	htable_release(ht);
3401 	return (hm);
3402 }
3403 
3404 extern int	vpm_enable;
3405 /*
3406  * Unload all translations to a page. If the page is a subpage of a large
3407  * page, the large page mappings are also removed.
3408  *
3409  * The forceflags are unused.
3410  */
3411 
3412 /*ARGSUSED*/
3413 static int
3414 hati_pageunload(struct page *pp, uint_t pg_szcd, uint_t forceflag)
3415 {
3416 	page_t		*cur_pp = pp;
3417 	hment_t		*hm;
3418 	hment_t		*prev;
3419 	htable_t	*ht;
3420 	uint_t		entry;
3421 	level_t		level;
3422 
3423 	XPV_DISALLOW_MIGRATE();
3424 #if defined(__amd64)
3425 	/*
3426 	 * clear the vpm ref.
3427 	 */
3428 	if (vpm_enable) {
3429 		pp->p_vpmref = 0;
3430 	}
3431 #endif
3432 	/*
3433 	 * The loop with next_size handles pages with multiple pagesize mappings
3434 	 */
3435 next_size:
3436 	for (;;) {
3437 
3438 		/*
3439 		 * Get a mapping list entry
3440 		 */
3441 		x86_hm_enter(cur_pp);
3442 		for (prev = NULL; ; prev = hm) {
3443 			hm = hment_walk(cur_pp, &ht, &entry, prev);
3444 			if (hm == NULL) {
3445 				x86_hm_exit(cur_pp);
3446 
3447 				/*
3448 				 * If not part of a larger page, we're done.
3449 				 */
3450 				if (cur_pp->p_szc <= pg_szcd) {
3451 					XPV_ALLOW_MIGRATE();
3452 					return (0);
3453 				}
3454 
3455 				/*
3456 				 * Else check the next larger page size.
3457 				 * hat_page_demote() may decrease p_szc
3458 				 * but that's ok we'll just take an extra
3459 				 * trip discover there're no larger mappings
3460 				 * and return.
3461 				 */
3462 				++pg_szcd;
3463 				cur_pp = PP_GROUPLEADER(cur_pp, pg_szcd);
3464 				goto next_size;
3465 			}
3466 
3467 			/*
3468 			 * If this mapping size matches, remove it.
3469 			 */
3470 			level = ht->ht_level;
3471 			if (level == pg_szcd)
3472 				break;
3473 		}
3474 
3475 		/*
3476 		 * Remove the mapping list entry for this page.
3477 		 * Note this does the x86_hm_exit() for us.
3478 		 */
3479 		hm = hati_page_unmap(cur_pp, ht, entry);
3480 		if (hm != NULL)
3481 			hment_free(hm);
3482 	}
3483 }
3484 
3485 int
3486 hat_pageunload(struct page *pp, uint_t forceflag)
3487 {
3488 	ASSERT(PAGE_EXCL(pp));
3489 	return (hati_pageunload(pp, 0, forceflag));
3490 }
3491 
3492 /*
3493  * Unload all large mappings to pp and reduce by 1 p_szc field of every large
3494  * page level that included pp.
3495  *
3496  * pp must be locked EXCL. Even though no other constituent pages are locked
3497  * it's legal to unload large mappings to pp because all constituent pages of
3498  * large locked mappings have to be locked SHARED.  therefore if we have EXCL
3499  * lock on one of constituent pages none of the large mappings to pp are
3500  * locked.
3501  *
3502  * Change (always decrease) p_szc field starting from the last constituent
3503  * page and ending with root constituent page so that root's pszc always shows
3504  * the area where hat_page_demote() may be active.
3505  *
3506  * This mechanism is only used for file system pages where it's not always
3507  * possible to get EXCL locks on all constituent pages to demote the size code
3508  * (as is done for anonymous or kernel large pages).
3509  */
3510 void
3511 hat_page_demote(page_t *pp)
3512 {
3513 	uint_t		pszc;
3514 	uint_t		rszc;
3515 	uint_t		szc;
3516 	page_t		*rootpp;
3517 	page_t		*firstpp;
3518 	page_t		*lastpp;
3519 	pgcnt_t		pgcnt;
3520 
3521 	ASSERT(PAGE_EXCL(pp));
3522 	ASSERT(!PP_ISFREE(pp));
3523 	ASSERT(page_szc_lock_assert(pp));
3524 
3525 	if (pp->p_szc == 0)
3526 		return;
3527 
3528 	rootpp = PP_GROUPLEADER(pp, 1);
3529 	(void) hati_pageunload(rootpp, 1, HAT_FORCE_PGUNLOAD);
3530 
3531 	/*
3532 	 * all large mappings to pp are gone
3533 	 * and no new can be setup since pp is locked exclusively.
3534 	 *
3535 	 * Lock the root to make sure there's only one hat_page_demote()
3536 	 * outstanding within the area of this root's pszc.
3537 	 *
3538 	 * Second potential hat_page_demote() is already eliminated by upper
3539 	 * VM layer via page_szc_lock() but we don't rely on it and use our
3540 	 * own locking (so that upper layer locking can be changed without
3541 	 * assumptions that hat depends on upper layer VM to prevent multiple
3542 	 * hat_page_demote() to be issued simultaneously to the same large
3543 	 * page).
3544 	 */
3545 again:
3546 	pszc = pp->p_szc;
3547 	if (pszc == 0)
3548 		return;
3549 	rootpp = PP_GROUPLEADER(pp, pszc);
3550 	x86_hm_enter(rootpp);
3551 	/*
3552 	 * If root's p_szc is different from pszc we raced with another
3553 	 * hat_page_demote().  Drop the lock and try to find the root again.
3554 	 * If root's p_szc is greater than pszc previous hat_page_demote() is
3555 	 * not done yet.  Take and release mlist lock of root's root to wait
3556 	 * for previous hat_page_demote() to complete.
3557 	 */
3558 	if ((rszc = rootpp->p_szc) != pszc) {
3559 		x86_hm_exit(rootpp);
3560 		if (rszc > pszc) {
3561 			/* p_szc of a locked non free page can't increase */
3562 			ASSERT(pp != rootpp);
3563 
3564 			rootpp = PP_GROUPLEADER(rootpp, rszc);
3565 			x86_hm_enter(rootpp);
3566 			x86_hm_exit(rootpp);
3567 		}
3568 		goto again;
3569 	}
3570 	ASSERT(pp->p_szc == pszc);
3571 
3572 	/*
3573 	 * Decrement by 1 p_szc of every constituent page of a region that
3574 	 * covered pp. For example if original szc is 3 it gets changed to 2
3575 	 * everywhere except in region 2 that covered pp. Region 2 that
3576 	 * covered pp gets demoted to 1 everywhere except in region 1 that
3577 	 * covered pp. The region 1 that covered pp is demoted to region
3578 	 * 0. It's done this way because from region 3 we removed level 3
3579 	 * mappings, from region 2 that covered pp we removed level 2 mappings
3580 	 * and from region 1 that covered pp we removed level 1 mappings.  All
3581 	 * changes are done from from high pfn's to low pfn's so that roots
3582 	 * are changed last allowing one to know the largest region where
3583 	 * hat_page_demote() is stil active by only looking at the root page.
3584 	 *
3585 	 * This algorithm is implemented in 2 while loops. First loop changes
3586 	 * p_szc of pages to the right of pp's level 1 region and second
3587 	 * loop changes p_szc of pages of level 1 region that covers pp
3588 	 * and all pages to the left of level 1 region that covers pp.
3589 	 * In the first loop p_szc keeps dropping with every iteration
3590 	 * and in the second loop it keeps increasing with every iteration.
3591 	 *
3592 	 * First loop description: Demote pages to the right of pp outside of
3593 	 * level 1 region that covers pp.  In every iteration of the while
3594 	 * loop below find the last page of szc region and the first page of
3595 	 * (szc - 1) region that is immediately to the right of (szc - 1)
3596 	 * region that covers pp.  From last such page to first such page
3597 	 * change every page's szc to szc - 1. Decrement szc and continue
3598 	 * looping until szc is 1. If pp belongs to the last (szc - 1) region
3599 	 * of szc region skip to the next iteration.
3600 	 */
3601 	szc = pszc;
3602 	while (szc > 1) {
3603 		lastpp = PP_GROUPLEADER(pp, szc);
3604 		pgcnt = page_get_pagecnt(szc);
3605 		lastpp += pgcnt - 1;
3606 		firstpp = PP_GROUPLEADER(pp, (szc - 1));
3607 		pgcnt = page_get_pagecnt(szc - 1);
3608 		if (lastpp - firstpp < pgcnt) {
3609 			szc--;
3610 			continue;
3611 		}
3612 		firstpp += pgcnt;
3613 		while (lastpp != firstpp) {
3614 			ASSERT(lastpp->p_szc == pszc);
3615 			lastpp->p_szc = szc - 1;
3616 			lastpp--;
3617 		}
3618 		firstpp->p_szc = szc - 1;
3619 		szc--;
3620 	}
3621 
3622 	/*
3623 	 * Second loop description:
3624 	 * First iteration changes p_szc to 0 of every
3625 	 * page of level 1 region that covers pp.
3626 	 * Subsequent iterations find last page of szc region
3627 	 * immediately to the left of szc region that covered pp
3628 	 * and first page of (szc + 1) region that covers pp.
3629 	 * From last to first page change p_szc of every page to szc.
3630 	 * Increment szc and continue looping until szc is pszc.
3631 	 * If pp belongs to the fist szc region of (szc + 1) region
3632 	 * skip to the next iteration.
3633 	 *
3634 	 */
3635 	szc = 0;
3636 	while (szc < pszc) {
3637 		firstpp = PP_GROUPLEADER(pp, (szc + 1));
3638 		if (szc == 0) {
3639 			pgcnt = page_get_pagecnt(1);
3640 			lastpp = firstpp + (pgcnt - 1);
3641 		} else {
3642 			lastpp = PP_GROUPLEADER(pp, szc);
3643 			if (firstpp == lastpp) {
3644 				szc++;
3645 				continue;
3646 			}
3647 			lastpp--;
3648 			pgcnt = page_get_pagecnt(szc);
3649 		}
3650 		while (lastpp != firstpp) {
3651 			ASSERT(lastpp->p_szc == pszc);
3652 			lastpp->p_szc = szc;
3653 			lastpp--;
3654 		}
3655 		firstpp->p_szc = szc;
3656 		if (firstpp == rootpp)
3657 			break;
3658 		szc++;
3659 	}
3660 	x86_hm_exit(rootpp);
3661 }
3662 
3663 /*
3664  * get hw stats from hardware into page struct and reset hw stats
3665  * returns attributes of page
3666  * Flags for hat_pagesync, hat_getstat, hat_sync
3667  *
3668  * define	HAT_SYNC_ZERORM		0x01
3669  *
3670  * Additional flags for hat_pagesync
3671  *
3672  * define	HAT_SYNC_STOPON_REF	0x02
3673  * define	HAT_SYNC_STOPON_MOD	0x04
3674  * define	HAT_SYNC_STOPON_RM	0x06
3675  * define	HAT_SYNC_STOPON_SHARED	0x08
3676  */
3677 uint_t
3678 hat_pagesync(struct page *pp, uint_t flags)
3679 {
3680 	hment_t		*hm = NULL;
3681 	htable_t	*ht;
3682 	uint_t		entry;
3683 	x86pte_t	old, save_old;
3684 	x86pte_t	new;
3685 	uchar_t		nrmbits = P_REF|P_MOD|P_RO;
3686 	extern ulong_t	po_share;
3687 	page_t		*save_pp = pp;
3688 	uint_t		pszc = 0;
3689 
3690 	ASSERT(PAGE_LOCKED(pp) || panicstr);
3691 
3692 	if (PP_ISRO(pp) && (flags & HAT_SYNC_STOPON_MOD))
3693 		return (pp->p_nrm & nrmbits);
3694 
3695 	if ((flags & HAT_SYNC_ZERORM) == 0) {
3696 
3697 		if ((flags & HAT_SYNC_STOPON_REF) != 0 && PP_ISREF(pp))
3698 			return (pp->p_nrm & nrmbits);
3699 
3700 		if ((flags & HAT_SYNC_STOPON_MOD) != 0 && PP_ISMOD(pp))
3701 			return (pp->p_nrm & nrmbits);
3702 
3703 		if ((flags & HAT_SYNC_STOPON_SHARED) != 0 &&
3704 		    hat_page_getshare(pp) > po_share) {
3705 			if (PP_ISRO(pp))
3706 				PP_SETREF(pp);
3707 			return (pp->p_nrm & nrmbits);
3708 		}
3709 	}
3710 
3711 	XPV_DISALLOW_MIGRATE();
3712 next_size:
3713 	/*
3714 	 * walk thru the mapping list syncing (and clearing) ref/mod bits.
3715 	 */
3716 	x86_hm_enter(pp);
3717 	while ((hm = hment_walk(pp, &ht, &entry, hm)) != NULL) {
3718 		if (ht->ht_level < pszc)
3719 			continue;
3720 		old = x86pte_get(ht, entry);
3721 try_again:
3722 
3723 		ASSERT(PTE2PFN(old, ht->ht_level) == pp->p_pagenum);
3724 
3725 		if (PTE_GET(old, PT_REF | PT_MOD) == 0)
3726 			continue;
3727 
3728 		save_old = old;
3729 		if ((flags & HAT_SYNC_ZERORM) != 0) {
3730 
3731 			/*
3732 			 * Need to clear ref or mod bits. Need to demap
3733 			 * to make sure any executing TLBs see cleared bits.
3734 			 */
3735 			new = old;
3736 			PTE_CLR(new, PT_REF | PT_MOD);
3737 			old = hati_update_pte(ht, entry, old, new);
3738 			if (old != 0)
3739 				goto try_again;
3740 
3741 			old = save_old;
3742 		}
3743 
3744 		/*
3745 		 * Sync the PTE
3746 		 */
3747 		if (!(flags & HAT_SYNC_ZERORM) &&
3748 		    PTE_GET(old, PT_SOFTWARE) <= PT_NOSYNC)
3749 			hati_sync_pte_to_page(pp, old, ht->ht_level);
3750 
3751 		/*
3752 		 * can stop short if we found a ref'd or mod'd page
3753 		 */
3754 		if ((flags & HAT_SYNC_STOPON_MOD) && PP_ISMOD(save_pp) ||
3755 		    (flags & HAT_SYNC_STOPON_REF) && PP_ISREF(save_pp)) {
3756 			x86_hm_exit(pp);
3757 			goto done;
3758 		}
3759 	}
3760 	x86_hm_exit(pp);
3761 	while (pszc < pp->p_szc) {
3762 		page_t *tpp;
3763 		pszc++;
3764 		tpp = PP_GROUPLEADER(pp, pszc);
3765 		if (pp != tpp) {
3766 			pp = tpp;
3767 			goto next_size;
3768 		}
3769 	}
3770 done:
3771 	XPV_ALLOW_MIGRATE();
3772 	return (save_pp->p_nrm & nrmbits);
3773 }
3774 
3775 /*
3776  * returns approx number of mappings to this pp.  A return of 0 implies
3777  * there are no mappings to the page.
3778  */
3779 ulong_t
3780 hat_page_getshare(page_t *pp)
3781 {
3782 	uint_t cnt;
3783 	cnt = hment_mapcnt(pp);
3784 #if defined(__amd64)
3785 	if (vpm_enable && pp->p_vpmref) {
3786 		cnt += 1;
3787 	}
3788 #endif
3789 	return (cnt);
3790 }
3791 
3792 /*
3793  * Return 1 the number of mappings exceeds sh_thresh. Return 0
3794  * otherwise.
3795  */
3796 int
3797 hat_page_checkshare(page_t *pp, ulong_t sh_thresh)
3798 {
3799 	return (hat_page_getshare(pp) > sh_thresh);
3800 }
3801 
3802 /*
3803  * hat_softlock isn't supported anymore
3804  */
3805 /*ARGSUSED*/
3806 faultcode_t
3807 hat_softlock(
3808 	hat_t *hat,
3809 	caddr_t addr,
3810 	size_t *len,
3811 	struct page **page_array,
3812 	uint_t flags)
3813 {
3814 	return (FC_NOSUPPORT);
3815 }
3816 
3817 
3818 
3819 /*
3820  * Routine to expose supported HAT features to platform independent code.
3821  */
3822 /*ARGSUSED*/
3823 int
3824 hat_supported(enum hat_features feature, void *arg)
3825 {
3826 	switch (feature) {
3827 
3828 	case HAT_SHARED_PT:	/* this is really ISM */
3829 		return (1);
3830 
3831 	case HAT_DYNAMIC_ISM_UNMAP:
3832 		return (0);
3833 
3834 	case HAT_VMODSORT:
3835 		return (1);
3836 
3837 	case HAT_SHARED_REGIONS:
3838 		return (0);
3839 
3840 	default:
3841 		panic("hat_supported() - unknown feature");
3842 	}
3843 	return (0);
3844 }
3845 
3846 /*
3847  * Called when a thread is exiting and has been switched to the kernel AS
3848  */
3849 void
3850 hat_thread_exit(kthread_t *thd)
3851 {
3852 	ASSERT(thd->t_procp->p_as == &kas);
3853 	XPV_DISALLOW_MIGRATE();
3854 	hat_switch(thd->t_procp->p_as->a_hat);
3855 	XPV_ALLOW_MIGRATE();
3856 }
3857 
3858 /*
3859  * Setup the given brand new hat structure as the new HAT on this cpu's mmu.
3860  */
3861 /*ARGSUSED*/
3862 void
3863 hat_setup(hat_t *hat, int flags)
3864 {
3865 	XPV_DISALLOW_MIGRATE();
3866 	kpreempt_disable();
3867 
3868 	hat_switch(hat);
3869 
3870 	kpreempt_enable();
3871 	XPV_ALLOW_MIGRATE();
3872 }
3873 
3874 /*
3875  * Prepare for a CPU private mapping for the given address.
3876  *
3877  * The address can only be used from a single CPU and can be remapped
3878  * using hat_mempte_remap().  Return the address of the PTE.
3879  *
3880  * We do the htable_create() if necessary and increment the valid count so
3881  * the htable can't disappear.  We also hat_devload() the page table into
3882  * kernel so that the PTE is quickly accessed.
3883  */
3884 hat_mempte_t
3885 hat_mempte_setup(caddr_t addr)
3886 {
3887 	uintptr_t	va = (uintptr_t)addr;
3888 	htable_t	*ht;
3889 	uint_t		entry;
3890 	x86pte_t	oldpte;
3891 	hat_mempte_t	p;
3892 
3893 	ASSERT(IS_PAGEALIGNED(va));
3894 	ASSERT(!IN_VA_HOLE(va));
3895 	++curthread->t_hatdepth;
3896 	XPV_DISALLOW_MIGRATE();
3897 	ht = htable_getpte(kas.a_hat, va, &entry, &oldpte, 0);
3898 	if (ht == NULL) {
3899 		ht = htable_create(kas.a_hat, va, 0, NULL);
3900 		entry = htable_va2entry(va, ht);
3901 		ASSERT(ht->ht_level == 0);
3902 		oldpte = x86pte_get(ht, entry);
3903 	}
3904 	if (PTE_ISVALID(oldpte))
3905 		panic("hat_mempte_setup(): address already mapped"
3906 		    "ht=%p, entry=%d, pte=" FMT_PTE, (void *)ht, entry, oldpte);
3907 
3908 	/*
3909 	 * increment ht_valid_cnt so that the pagetable can't disappear
3910 	 */
3911 	HTABLE_INC(ht->ht_valid_cnt);
3912 
3913 	/*
3914 	 * return the PTE physical address to the caller.
3915 	 */
3916 	htable_release(ht);
3917 	XPV_ALLOW_MIGRATE();
3918 	p = PT_INDEX_PHYSADDR(pfn_to_pa(ht->ht_pfn), entry);
3919 	--curthread->t_hatdepth;
3920 	return (p);
3921 }
3922 
3923 /*
3924  * Release a CPU private mapping for the given address.
3925  * We decrement the htable valid count so it might be destroyed.
3926  */
3927 /*ARGSUSED1*/
3928 void
3929 hat_mempte_release(caddr_t addr, hat_mempte_t pte_pa)
3930 {
3931 	htable_t	*ht;
3932 
3933 	XPV_DISALLOW_MIGRATE();
3934 	/*
3935 	 * invalidate any left over mapping and decrement the htable valid count
3936 	 */
3937 #ifdef __xpv
3938 	if (HYPERVISOR_update_va_mapping((uintptr_t)addr, 0,
3939 	    UVMF_INVLPG | UVMF_LOCAL))
3940 		panic("HYPERVISOR_update_va_mapping() failed");
3941 #else
3942 	{
3943 		x86pte_t *pteptr;
3944 
3945 		pteptr = x86pte_mapin(mmu_btop(pte_pa),
3946 		    (pte_pa & MMU_PAGEOFFSET) >> mmu.pte_size_shift, NULL);
3947 		if (mmu.pae_hat)
3948 			*pteptr = 0;
3949 		else
3950 			*(x86pte32_t *)pteptr = 0;
3951 		mmu_tlbflush_entry(addr);
3952 		x86pte_mapout();
3953 	}
3954 #endif
3955 
3956 	ht = htable_getpte(kas.a_hat, ALIGN2PAGE(addr), NULL, NULL, 0);
3957 	if (ht == NULL)
3958 		panic("hat_mempte_release(): invalid address");
3959 	ASSERT(ht->ht_level == 0);
3960 	HTABLE_DEC(ht->ht_valid_cnt);
3961 	htable_release(ht);
3962 	XPV_ALLOW_MIGRATE();
3963 }
3964 
3965 /*
3966  * Apply a temporary CPU private mapping to a page. We flush the TLB only
3967  * on this CPU, so this ought to have been called with preemption disabled.
3968  */
3969 void
3970 hat_mempte_remap(
3971 	pfn_t		pfn,
3972 	caddr_t		addr,
3973 	hat_mempte_t	pte_pa,
3974 	uint_t		attr,
3975 	uint_t		flags)
3976 {
3977 	uintptr_t	va = (uintptr_t)addr;
3978 	x86pte_t	pte;
3979 
3980 	/*
3981 	 * Remap the given PTE to the new page's PFN. Invalidate only
3982 	 * on this CPU.
3983 	 */
3984 #ifdef DEBUG
3985 	htable_t	*ht;
3986 	uint_t		entry;
3987 
3988 	ASSERT(IS_PAGEALIGNED(va));
3989 	ASSERT(!IN_VA_HOLE(va));
3990 	ht = htable_getpte(kas.a_hat, va, &entry, NULL, 0);
3991 	ASSERT(ht != NULL);
3992 	ASSERT(ht->ht_level == 0);
3993 	ASSERT(ht->ht_valid_cnt > 0);
3994 	ASSERT(ht->ht_pfn == mmu_btop(pte_pa));
3995 	htable_release(ht);
3996 #endif
3997 	XPV_DISALLOW_MIGRATE();
3998 	pte = hati_mkpte(pfn, attr, 0, flags);
3999 #ifdef __xpv
4000 	if (HYPERVISOR_update_va_mapping(va, pte, UVMF_INVLPG | UVMF_LOCAL))
4001 		panic("HYPERVISOR_update_va_mapping() failed");
4002 #else
4003 	{
4004 		x86pte_t *pteptr;
4005 
4006 		pteptr = x86pte_mapin(mmu_btop(pte_pa),
4007 		    (pte_pa & MMU_PAGEOFFSET) >> mmu.pte_size_shift, NULL);
4008 		if (mmu.pae_hat)
4009 			*(x86pte_t *)pteptr = pte;
4010 		else
4011 			*(x86pte32_t *)pteptr = (x86pte32_t)pte;
4012 		mmu_tlbflush_entry(addr);
4013 		x86pte_mapout();
4014 	}
4015 #endif
4016 	XPV_ALLOW_MIGRATE();
4017 }
4018 
4019 
4020 
4021 /*
4022  * Hat locking functions
4023  * XXX - these two functions are currently being used by hatstats
4024  * 	they can be removed by using a per-as mutex for hatstats.
4025  */
4026 void
4027 hat_enter(hat_t *hat)
4028 {
4029 	mutex_enter(&hat->hat_mutex);
4030 }
4031 
4032 void
4033 hat_exit(hat_t *hat)
4034 {
4035 	mutex_exit(&hat->hat_mutex);
4036 }
4037 
4038 /*
4039  * HAT part of cpu initialization.
4040  */
4041 void
4042 hat_cpu_online(struct cpu *cpup)
4043 {
4044 	if (cpup != CPU) {
4045 		x86pte_cpu_init(cpup);
4046 		hat_vlp_setup(cpup);
4047 	}
4048 	CPUSET_ATOMIC_ADD(khat_cpuset, cpup->cpu_id);
4049 }
4050 
4051 /*
4052  * HAT part of cpu deletion.
4053  * (currently, we only call this after the cpu is safely passivated.)
4054  */
4055 void
4056 hat_cpu_offline(struct cpu *cpup)
4057 {
4058 	ASSERT(cpup != CPU);
4059 
4060 	CPUSET_ATOMIC_DEL(khat_cpuset, cpup->cpu_id);
4061 	hat_vlp_teardown(cpup);
4062 	x86pte_cpu_fini(cpup);
4063 }
4064 
4065 /*
4066  * Function called after all CPUs are brought online.
4067  * Used to remove low address boot mappings.
4068  */
4069 void
4070 clear_boot_mappings(uintptr_t low, uintptr_t high)
4071 {
4072 	uintptr_t vaddr = low;
4073 	htable_t *ht = NULL;
4074 	level_t level;
4075 	uint_t entry;
4076 	x86pte_t pte;
4077 
4078 	/*
4079 	 * On 1st CPU we can unload the prom mappings, basically we blow away
4080 	 * all virtual mappings under _userlimit.
4081 	 */
4082 	while (vaddr < high) {
4083 		pte = htable_walk(kas.a_hat, &ht, &vaddr, high);
4084 		if (ht == NULL)
4085 			break;
4086 
4087 		level = ht->ht_level;
4088 		entry = htable_va2entry(vaddr, ht);
4089 		ASSERT(level <= mmu.max_page_level);
4090 		ASSERT(PTE_ISPAGE(pte, level));
4091 
4092 		/*
4093 		 * Unload the mapping from the page tables.
4094 		 */
4095 		(void) x86pte_inval(ht, entry, 0, NULL);
4096 		ASSERT(ht->ht_valid_cnt > 0);
4097 		HTABLE_DEC(ht->ht_valid_cnt);
4098 		PGCNT_DEC(ht->ht_hat, ht->ht_level);
4099 
4100 		vaddr += LEVEL_SIZE(ht->ht_level);
4101 	}
4102 	if (ht)
4103 		htable_release(ht);
4104 }
4105 
4106 /*
4107  * Atomically update a new translation for a single page.  If the
4108  * currently installed PTE doesn't match the value we expect to find,
4109  * it's not updated and we return the PTE we found.
4110  *
4111  * If activating nosync or NOWRITE and the page was modified we need to sync
4112  * with the page_t. Also sync with page_t if clearing ref/mod bits.
4113  */
4114 static x86pte_t
4115 hati_update_pte(htable_t *ht, uint_t entry, x86pte_t expected, x86pte_t new)
4116 {
4117 	page_t		*pp;
4118 	uint_t		rm = 0;
4119 	x86pte_t	replaced;
4120 
4121 	if (PTE_GET(expected, PT_SOFTWARE) < PT_NOSYNC &&
4122 	    PTE_GET(expected, PT_MOD | PT_REF) &&
4123 	    (PTE_GET(new, PT_NOSYNC) || !PTE_GET(new, PT_WRITABLE) ||
4124 	    !PTE_GET(new, PT_MOD | PT_REF))) {
4125 
4126 		ASSERT(!pfn_is_foreign(PTE2PFN(expected, ht->ht_level)));
4127 		pp = page_numtopp_nolock(PTE2PFN(expected, ht->ht_level));
4128 		ASSERT(pp != NULL);
4129 		if (PTE_GET(expected, PT_MOD))
4130 			rm |= P_MOD;
4131 		if (PTE_GET(expected, PT_REF))
4132 			rm |= P_REF;
4133 		PTE_CLR(new, PT_MOD | PT_REF);
4134 	}
4135 
4136 	replaced = x86pte_update(ht, entry, expected, new);
4137 	if (replaced != expected)
4138 		return (replaced);
4139 
4140 	if (rm) {
4141 		/*
4142 		 * sync to all constituent pages of a large page
4143 		 */
4144 		pgcnt_t pgcnt = page_get_pagecnt(ht->ht_level);
4145 		ASSERT(IS_P2ALIGNED(pp->p_pagenum, pgcnt));
4146 		while (pgcnt-- > 0) {
4147 			/*
4148 			 * hat_page_demote() can't decrease
4149 			 * pszc below this mapping size
4150 			 * since large mapping existed after we
4151 			 * took mlist lock.
4152 			 */
4153 			ASSERT(pp->p_szc >= ht->ht_level);
4154 			hat_page_setattr(pp, rm);
4155 			++pp;
4156 		}
4157 	}
4158 
4159 	return (0);
4160 }
4161 
4162 /* ARGSUSED */
4163 void
4164 hat_join_srd(struct hat *hat, vnode_t *evp)
4165 {
4166 }
4167 
4168 /* ARGSUSED */
4169 hat_region_cookie_t
4170 hat_join_region(struct hat *hat,
4171     caddr_t r_saddr,
4172     size_t r_size,
4173     void *r_obj,
4174     u_offset_t r_objoff,
4175     uchar_t r_perm,
4176     uchar_t r_pgszc,
4177     hat_rgn_cb_func_t r_cb_function,
4178     uint_t flags)
4179 {
4180 	panic("No shared region support on x86");
4181 	return (HAT_INVALID_REGION_COOKIE);
4182 }
4183 
4184 /* ARGSUSED */
4185 void
4186 hat_leave_region(struct hat *hat, hat_region_cookie_t rcookie, uint_t flags)
4187 {
4188 	panic("No shared region support on x86");
4189 }
4190 
4191 /* ARGSUSED */
4192 void
4193 hat_dup_region(struct hat *hat, hat_region_cookie_t rcookie)
4194 {
4195 	panic("No shared region support on x86");
4196 }
4197 
4198 
4199 /*
4200  * Kernel Physical Mapping (kpm) facility
4201  *
4202  * Most of the routines needed to support segkpm are almost no-ops on the
4203  * x86 platform.  We map in the entire segment when it is created and leave
4204  * it mapped in, so there is no additional work required to set up and tear
4205  * down individual mappings.  All of these routines were created to support
4206  * SPARC platforms that have to avoid aliasing in their virtually indexed
4207  * caches.
4208  *
4209  * Most of the routines have sanity checks in them (e.g. verifying that the
4210  * passed-in page is locked).  We don't actually care about most of these
4211  * checks on x86, but we leave them in place to identify problems in the
4212  * upper levels.
4213  */
4214 
4215 /*
4216  * Map in a locked page and return the vaddr.
4217  */
4218 /*ARGSUSED*/
4219 caddr_t
4220 hat_kpm_mapin(struct page *pp, struct kpme *kpme)
4221 {
4222 	caddr_t		vaddr;
4223 
4224 #ifdef DEBUG
4225 	if (kpm_enable == 0) {
4226 		cmn_err(CE_WARN, "hat_kpm_mapin: kpm_enable not set\n");
4227 		return ((caddr_t)NULL);
4228 	}
4229 
4230 	if (pp == NULL || PAGE_LOCKED(pp) == 0) {
4231 		cmn_err(CE_WARN, "hat_kpm_mapin: pp zero or not locked\n");
4232 		return ((caddr_t)NULL);
4233 	}
4234 #endif
4235 
4236 	vaddr = hat_kpm_page2va(pp, 1);
4237 
4238 	return (vaddr);
4239 }
4240 
4241 /*
4242  * Mapout a locked page.
4243  */
4244 /*ARGSUSED*/
4245 void
4246 hat_kpm_mapout(struct page *pp, struct kpme *kpme, caddr_t vaddr)
4247 {
4248 #ifdef DEBUG
4249 	if (kpm_enable == 0) {
4250 		cmn_err(CE_WARN, "hat_kpm_mapout: kpm_enable not set\n");
4251 		return;
4252 	}
4253 
4254 	if (IS_KPM_ADDR(vaddr) == 0) {
4255 		cmn_err(CE_WARN, "hat_kpm_mapout: no kpm address\n");
4256 		return;
4257 	}
4258 
4259 	if (pp == NULL || PAGE_LOCKED(pp) == 0) {
4260 		cmn_err(CE_WARN, "hat_kpm_mapout: page zero or not locked\n");
4261 		return;
4262 	}
4263 #endif
4264 }
4265 
4266 /*
4267  * hat_kpm_mapin_pfn is used to obtain a kpm mapping for physical
4268  * memory addresses that are not described by a page_t.  It can
4269  * also be used for normal pages that are not locked, but beware
4270  * this is dangerous - no locking is performed, so the identity of
4271  * the page could change.  hat_kpm_mapin_pfn is not supported when
4272  * vac_colors > 1, because the chosen va depends on the page identity,
4273  * which could change.
4274  * The caller must only pass pfn's for valid physical addresses; violation
4275  * of this rule will cause panic.
4276  */
4277 caddr_t
4278 hat_kpm_mapin_pfn(pfn_t pfn)
4279 {
4280 	caddr_t paddr, vaddr;
4281 
4282 	if (kpm_enable == 0)
4283 		return ((caddr_t)NULL);
4284 
4285 	paddr = (caddr_t)ptob(pfn);
4286 	vaddr = (uintptr_t)kpm_vbase + paddr;
4287 
4288 	return ((caddr_t)vaddr);
4289 }
4290 
4291 /*ARGSUSED*/
4292 void
4293 hat_kpm_mapout_pfn(pfn_t pfn)
4294 {
4295 	/* empty */
4296 }
4297 
4298 /*
4299  * Return the kpm virtual address for a specific pfn
4300  */
4301 caddr_t
4302 hat_kpm_pfn2va(pfn_t pfn)
4303 {
4304 	uintptr_t vaddr = (uintptr_t)kpm_vbase + mmu_ptob(pfn);
4305 
4306 	ASSERT(!pfn_is_foreign(pfn));
4307 	return ((caddr_t)vaddr);
4308 }
4309 
4310 /*
4311  * Return the kpm virtual address for the page at pp.
4312  */
4313 /*ARGSUSED*/
4314 caddr_t
4315 hat_kpm_page2va(struct page *pp, int checkswap)
4316 {
4317 	return (hat_kpm_pfn2va(pp->p_pagenum));
4318 }
4319 
4320 /*
4321  * Return the page frame number for the kpm virtual address vaddr.
4322  */
4323 pfn_t
4324 hat_kpm_va2pfn(caddr_t vaddr)
4325 {
4326 	pfn_t		pfn;
4327 
4328 	ASSERT(IS_KPM_ADDR(vaddr));
4329 
4330 	pfn = (pfn_t)btop(vaddr - kpm_vbase);
4331 
4332 	return (pfn);
4333 }
4334 
4335 
4336 /*
4337  * Return the page for the kpm virtual address vaddr.
4338  */
4339 page_t *
4340 hat_kpm_vaddr2page(caddr_t vaddr)
4341 {
4342 	pfn_t		pfn;
4343 
4344 	ASSERT(IS_KPM_ADDR(vaddr));
4345 
4346 	pfn = hat_kpm_va2pfn(vaddr);
4347 
4348 	return (page_numtopp_nolock(pfn));
4349 }
4350 
4351 /*
4352  * hat_kpm_fault is called from segkpm_fault when we take a page fault on a
4353  * KPM page.  This should never happen on x86
4354  */
4355 int
4356 hat_kpm_fault(hat_t *hat, caddr_t vaddr)
4357 {
4358 	panic("pagefault in seg_kpm.  hat: 0x%p  vaddr: 0x%p",
4359 	    (void *)hat, (void *)vaddr);
4360 
4361 	return (0);
4362 }
4363 
4364 /*ARGSUSED*/
4365 void
4366 hat_kpm_mseghash_clear(int nentries)
4367 {}
4368 
4369 /*ARGSUSED*/
4370 void
4371 hat_kpm_mseghash_update(pgcnt_t inx, struct memseg *msp)
4372 {}
4373 
4374 #ifndef	__xpv
4375 void
4376 hat_kpm_addmem_mseg_update(struct memseg *msp, pgcnt_t nkpmpgs,
4377 	offset_t kpm_pages_off)
4378 {
4379 	_NOTE(ARGUNUSED(nkpmpgs, kpm_pages_off));
4380 	pfn_t base, end;
4381 
4382 	/*
4383 	 * kphysm_add_memory_dynamic() does not set nkpmpgs
4384 	 * when page_t memory is externally allocated.  That
4385 	 * code must properly calculate nkpmpgs in all cases
4386 	 * if nkpmpgs needs to be used at some point.
4387 	 */
4388 
4389 	/*
4390 	 * The meta (page_t) pages for dynamically added memory are allocated
4391 	 * either from the incoming memory itself or from existing memory.
4392 	 * In the former case the base of the incoming pages will be different
4393 	 * than the base of the dynamic segment so call memseg_get_start() to
4394 	 * get the actual base of the incoming memory for each case.
4395 	 */
4396 
4397 	base = memseg_get_start(msp);
4398 	end = msp->pages_end;
4399 
4400 	hat_devload(kas.a_hat, kpm_vbase + mmu_ptob(base),
4401 	    mmu_ptob(end - base), base, PROT_READ | PROT_WRITE,
4402 	    HAT_LOAD | HAT_LOAD_LOCK | HAT_LOAD_NOCONSIST);
4403 }
4404 
4405 void
4406 hat_kpm_addmem_mseg_insert(struct memseg *msp)
4407 {
4408 	_NOTE(ARGUNUSED(msp));
4409 }
4410 
4411 void
4412 hat_kpm_addmem_memsegs_update(struct memseg *msp)
4413 {
4414 	_NOTE(ARGUNUSED(msp));
4415 }
4416 
4417 /*
4418  * Return end of metadata for an already setup memseg.
4419  * X86 platforms don't need per-page meta data to support kpm.
4420  */
4421 caddr_t
4422 hat_kpm_mseg_reuse(struct memseg *msp)
4423 {
4424 	return ((caddr_t)msp->epages);
4425 }
4426 
4427 void
4428 hat_kpm_delmem_mseg_update(struct memseg *msp, struct memseg **mspp)
4429 {
4430 	_NOTE(ARGUNUSED(msp, mspp));
4431 	ASSERT(0);
4432 }
4433 
4434 void
4435 hat_kpm_split_mseg_update(struct memseg *msp, struct memseg **mspp,
4436 	struct memseg *lo, struct memseg *mid, struct memseg *hi)
4437 {
4438 	_NOTE(ARGUNUSED(msp, mspp, lo, mid, hi));
4439 	ASSERT(0);
4440 }
4441 
4442 /*
4443  * Walk the memsegs chain, applying func to each memseg span.
4444  */
4445 void
4446 hat_kpm_walk(void (*func)(void *, void *, size_t), void *arg)
4447 {
4448 	pfn_t	pbase, pend;
4449 	void	*base;
4450 	size_t	size;
4451 	struct memseg *msp;
4452 
4453 	for (msp = memsegs; msp; msp = msp->next) {
4454 		pbase = msp->pages_base;
4455 		pend = msp->pages_end;
4456 		base = ptob(pbase) + kpm_vbase;
4457 		size = ptob(pend - pbase);
4458 		func(arg, base, size);
4459 	}
4460 }
4461 
4462 #else	/* __xpv */
4463 
4464 /*
4465  * There are specific Hypervisor calls to establish and remove mappings
4466  * to grant table references and the privcmd driver. We have to ensure
4467  * that a page table actually exists.
4468  */
4469 void
4470 hat_prepare_mapping(hat_t *hat, caddr_t addr, uint64_t *pte_ma)
4471 {
4472 	maddr_t base_ma;
4473 	htable_t *ht;
4474 	uint_t entry;
4475 
4476 	ASSERT(IS_P2ALIGNED((uintptr_t)addr, MMU_PAGESIZE));
4477 	XPV_DISALLOW_MIGRATE();
4478 	ht = htable_create(hat, (uintptr_t)addr, 0, NULL);
4479 
4480 	/*
4481 	 * if an address for pte_ma is passed in, return the MA of the pte
4482 	 * for this specific address.  This address is only valid as long
4483 	 * as the htable stays locked.
4484 	 */
4485 	if (pte_ma != NULL) {
4486 		entry = htable_va2entry((uintptr_t)addr, ht);
4487 		base_ma = pa_to_ma(ptob(ht->ht_pfn));
4488 		*pte_ma = base_ma + (entry << mmu.pte_size_shift);
4489 	}
4490 	XPV_ALLOW_MIGRATE();
4491 }
4492 
4493 void
4494 hat_release_mapping(hat_t *hat, caddr_t addr)
4495 {
4496 	htable_t *ht;
4497 
4498 	ASSERT(IS_P2ALIGNED((uintptr_t)addr, MMU_PAGESIZE));
4499 	XPV_DISALLOW_MIGRATE();
4500 	ht = htable_lookup(hat, (uintptr_t)addr, 0);
4501 	ASSERT(ht != NULL);
4502 	ASSERT(ht->ht_busy >= 2);
4503 	htable_release(ht);
4504 	htable_release(ht);
4505 	XPV_ALLOW_MIGRATE();
4506 }
4507 #endif	/* __xpv */
4508