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