xref: /illumos-gate/usr/src/uts/i86pc/vm/hat_i86.c (revision 250b7ff955bc8ffaf1e3b9aae014cbf82bff0589)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5a85a6733Sjosephb  * Common Development and Distribution License (the "License").
6a85a6733Sjosephb  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22ae115bc7Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * VM - Hardware Address Translation management for i386 and amd64
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * Implementation of the interfaces described in <common/vm/hat.h>
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * Nearly all the details of how the hardware is managed should not be
347c478bd9Sstevel@tonic-gate  * visible outside this layer except for misc. machine specific functions
357c478bd9Sstevel@tonic-gate  * that work in conjunction with this code.
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * Routines used only inside of i86pc/vm start with hati_ for HAT Internal.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <sys/machparam.h>
417c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
427c478bd9Sstevel@tonic-gate #include <sys/mman.h>
437c478bd9Sstevel@tonic-gate #include <sys/types.h>
447c478bd9Sstevel@tonic-gate #include <sys/systm.h>
457c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
467c478bd9Sstevel@tonic-gate #include <sys/thread.h>
477c478bd9Sstevel@tonic-gate #include <sys/proc.h>
487c478bd9Sstevel@tonic-gate #include <sys/cpu.h>
497c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
507c478bd9Sstevel@tonic-gate #include <sys/disp.h>
517c478bd9Sstevel@tonic-gate #include <sys/shm.h>
527c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
537c478bd9Sstevel@tonic-gate #include <sys/machparam.h>
547c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
557c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
567c478bd9Sstevel@tonic-gate #include <sys/promif.h>
577c478bd9Sstevel@tonic-gate #include <sys/var.h>
587c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
597c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
607c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
61ae115bc7Smrj #include <sys/controlregs.h>
62ae115bc7Smrj #include <sys/bootconf.h>
63ae115bc7Smrj #include <sys/bootsvcs.h>
64ae115bc7Smrj #include <sys/bootinfo.h>
6595c0a3c8Sjosephb #include <sys/archsystm.h>
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
687c478bd9Sstevel@tonic-gate #include <vm/hat_i86.h>
697c478bd9Sstevel@tonic-gate #include <vm/as.h>
707c478bd9Sstevel@tonic-gate #include <vm/seg.h>
717c478bd9Sstevel@tonic-gate #include <vm/page.h>
727c478bd9Sstevel@tonic-gate #include <vm/seg_kp.h>
737c478bd9Sstevel@tonic-gate #include <vm/seg_kpm.h>
747c478bd9Sstevel@tonic-gate #include <vm/vm_dep.h>
75ae115bc7Smrj #include <vm/kboot_mmu.h>
76*250b7ff9Sjosephb #include <vm/seg_spt.h>
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * Basic parameters for hat operation.
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate struct hat_mmu_info mmu;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /*
867c478bd9Sstevel@tonic-gate  * The page that is the kernel's top level pagetable.
877c478bd9Sstevel@tonic-gate  *
887c478bd9Sstevel@tonic-gate  * For 32 bit VLP support, the kernel hat will use the 1st 4 entries
897c478bd9Sstevel@tonic-gate  * on this 4K page for its top level page table. The remaining groups of
907c478bd9Sstevel@tonic-gate  * 4 entries are used for per processor copies of user VLP pagetables for
917c478bd9Sstevel@tonic-gate  * running threads.  See hat_switch() and reload_pae32() for details.
927c478bd9Sstevel@tonic-gate  *
937c478bd9Sstevel@tonic-gate  * vlp_page[0] - 0th level==2 PTE for kernel HAT (will be zero)
947c478bd9Sstevel@tonic-gate  * vlp_page[1] - 1st level==2 PTE for kernel HAT (will be zero)
957c478bd9Sstevel@tonic-gate  * vlp_page[2] - 2nd level==2 PTE for kernel HAT (zero for small memory)
967c478bd9Sstevel@tonic-gate  * vlp_page[3] - 3rd level==2 PTE for kernel
977c478bd9Sstevel@tonic-gate  *
987c478bd9Sstevel@tonic-gate  * vlp_page[4] - 0th level==2 PTE for user thread on cpu 0
997c478bd9Sstevel@tonic-gate  * vlp_page[5] - 1st level==2 PTE for user thread on cpu 0
1007c478bd9Sstevel@tonic-gate  * vlp_page[6] - 2nd level==2 PTE for user thread on cpu 0
1017c478bd9Sstevel@tonic-gate  * vlp_page[7] - probably copy of kernel PTE
1027c478bd9Sstevel@tonic-gate  *
1037c478bd9Sstevel@tonic-gate  * vlp_page[8]  - 0th level==2 PTE for user thread on cpu 1
1047c478bd9Sstevel@tonic-gate  * vlp_page[9]  - 1st level==2 PTE for user thread on cpu 1
1057c478bd9Sstevel@tonic-gate  * vlp_page[10] - 2nd level==2 PTE for user thread on cpu 1
1067c478bd9Sstevel@tonic-gate  * vlp_page[11] - probably copy of kernel PTE
1077c478bd9Sstevel@tonic-gate  * ...
1087c478bd9Sstevel@tonic-gate  *
1097c478bd9Sstevel@tonic-gate  * when / where the kernel PTE's are (entry 2 or 3 or none) depends
1107c478bd9Sstevel@tonic-gate  * on kernelbase.
1117c478bd9Sstevel@tonic-gate  */
1127c478bd9Sstevel@tonic-gate static x86pte_t *vlp_page;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate  * forward declaration of internal utility routines
1167c478bd9Sstevel@tonic-gate  */
1177c478bd9Sstevel@tonic-gate static x86pte_t hati_update_pte(htable_t *ht, uint_t entry, x86pte_t expected,
1187c478bd9Sstevel@tonic-gate 	x86pte_t new);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate  * The kernel address space exists in all HATs. To implement this the
1227c478bd9Sstevel@tonic-gate  * kernel reserves a fixed number of entries in every topmost level page
1237c478bd9Sstevel@tonic-gate  * table. The values are setup in hat_init() and then copied to every hat
1247c478bd9Sstevel@tonic-gate  * created by hat_alloc(). This means that kernelbase must be:
1257c478bd9Sstevel@tonic-gate  *
1267c478bd9Sstevel@tonic-gate  *	  4Meg aligned for 32 bit kernels
1277c478bd9Sstevel@tonic-gate  *	512Gig aligned for x86_64 64 bit kernel
1287c478bd9Sstevel@tonic-gate  *
1297c478bd9Sstevel@tonic-gate  * The PAE 32 bit hat is handled as a special case. Otherwise requiring 1Gig
1307c478bd9Sstevel@tonic-gate  * alignment would use too much VA for the kernel.
1317c478bd9Sstevel@tonic-gate  *
1327c478bd9Sstevel@tonic-gate  */
1337c478bd9Sstevel@tonic-gate static uint_t	khat_start;	/* index of 1st entry in kernel's top ptable */
1347c478bd9Sstevel@tonic-gate static uint_t	khat_entries;	/* number of entries in kernel's top ptable */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate #if defined(__i386)
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate static htable_t	*khat_pae32_htable = NULL;
1397c478bd9Sstevel@tonic-gate static uint_t	khat_pae32_start;
1407c478bd9Sstevel@tonic-gate static uint_t	khat_pae32_entries;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate #endif
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate uint_t use_boot_reserve = 1;	/* cleared after early boot process */
1457c478bd9Sstevel@tonic-gate uint_t can_steal_post_boot = 0;	/* set late in boot to enable stealing */
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate  * A cpuset for all cpus. This is used for kernel address cross calls, since
1497c478bd9Sstevel@tonic-gate  * the kernel addresses apply to all cpus.
1507c478bd9Sstevel@tonic-gate  */
1517c478bd9Sstevel@tonic-gate cpuset_t khat_cpuset;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate  * management stuff for hat structures
1557c478bd9Sstevel@tonic-gate  */
1567c478bd9Sstevel@tonic-gate kmutex_t	hat_list_lock;
1577c478bd9Sstevel@tonic-gate kcondvar_t	hat_list_cv;
1587c478bd9Sstevel@tonic-gate kmem_cache_t	*hat_cache;
1597c478bd9Sstevel@tonic-gate kmem_cache_t	*hat_hash_cache;
1607c478bd9Sstevel@tonic-gate kmem_cache_t	*vlp_hash_cache;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate  * Simple statistics
1647c478bd9Sstevel@tonic-gate  */
1657c478bd9Sstevel@tonic-gate struct hatstats hatstat;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate  * useful stuff for atomic access/clearing/setting REF/MOD/RO bits in page_t's.
1697c478bd9Sstevel@tonic-gate  */
1707c478bd9Sstevel@tonic-gate extern void atomic_orb(uchar_t *addr, uchar_t val);
1717c478bd9Sstevel@tonic-gate extern void atomic_andb(uchar_t *addr, uchar_t val);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate #define	PP_GETRM(pp, rmmask)    (pp->p_nrm & rmmask)
1747c478bd9Sstevel@tonic-gate #define	PP_ISMOD(pp)		PP_GETRM(pp, P_MOD)
1757c478bd9Sstevel@tonic-gate #define	PP_ISREF(pp)		PP_GETRM(pp, P_REF)
1767c478bd9Sstevel@tonic-gate #define	PP_ISRO(pp)		PP_GETRM(pp, P_RO)
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate #define	PP_SETRM(pp, rm)	atomic_orb(&(pp->p_nrm), rm)
1797c478bd9Sstevel@tonic-gate #define	PP_SETMOD(pp)		PP_SETRM(pp, P_MOD)
1807c478bd9Sstevel@tonic-gate #define	PP_SETREF(pp)		PP_SETRM(pp, P_REF)
1817c478bd9Sstevel@tonic-gate #define	PP_SETRO(pp)		PP_SETRM(pp, P_RO)
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate #define	PP_CLRRM(pp, rm)	atomic_andb(&(pp->p_nrm), ~(rm))
1847c478bd9Sstevel@tonic-gate #define	PP_CLRMOD(pp)   	PP_CLRRM(pp, P_MOD)
1857c478bd9Sstevel@tonic-gate #define	PP_CLRREF(pp)   	PP_CLRRM(pp, P_REF)
1867c478bd9Sstevel@tonic-gate #define	PP_CLRRO(pp)    	PP_CLRRM(pp, P_RO)
1877c478bd9Sstevel@tonic-gate #define	PP_CLRALL(pp)		PP_CLRRM(pp, P_MOD | P_REF | P_RO)
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate  * kmem cache constructor for struct hat
1917c478bd9Sstevel@tonic-gate  */
1927c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1937c478bd9Sstevel@tonic-gate static int
1947c478bd9Sstevel@tonic-gate hati_constructor(void *buf, void *handle, int kmflags)
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	hat_t	*hat = buf;
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	mutex_init(&hat->hat_mutex, NULL, MUTEX_DEFAULT, NULL);
1997c478bd9Sstevel@tonic-gate 	bzero(hat->hat_pages_mapped,
2007c478bd9Sstevel@tonic-gate 	    sizeof (pgcnt_t) * (mmu.max_page_level + 1));
201*250b7ff9Sjosephb 	hat->hat_ism_pgcnt = 0;
2027c478bd9Sstevel@tonic-gate 	hat->hat_stats = 0;
2037c478bd9Sstevel@tonic-gate 	hat->hat_flags = 0;
2047c478bd9Sstevel@tonic-gate 	CPUSET_ZERO(hat->hat_cpus);
2057c478bd9Sstevel@tonic-gate 	hat->hat_htable = NULL;
2067c478bd9Sstevel@tonic-gate 	hat->hat_ht_hash = NULL;
2077c478bd9Sstevel@tonic-gate 	return (0);
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate /*
2117c478bd9Sstevel@tonic-gate  * Allocate a hat structure for as. We also create the top level
2127c478bd9Sstevel@tonic-gate  * htable and initialize it to contain the kernel hat entries.
2137c478bd9Sstevel@tonic-gate  */
2147c478bd9Sstevel@tonic-gate hat_t *
2157c478bd9Sstevel@tonic-gate hat_alloc(struct as *as)
2167c478bd9Sstevel@tonic-gate {
2177c478bd9Sstevel@tonic-gate 	hat_t		*hat;
2187c478bd9Sstevel@tonic-gate 	htable_t	*ht;	/* top level htable */
2197c478bd9Sstevel@tonic-gate 	uint_t		use_vlp;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	/*
2227c478bd9Sstevel@tonic-gate 	 * Once we start creating user process HATs we can enable
2237c478bd9Sstevel@tonic-gate 	 * the htable_steal() code.
2247c478bd9Sstevel@tonic-gate 	 */
2257c478bd9Sstevel@tonic-gate 	if (can_steal_post_boot == 0)
2267c478bd9Sstevel@tonic-gate 		can_steal_post_boot = 1;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
2297c478bd9Sstevel@tonic-gate 	hat = kmem_cache_alloc(hat_cache, KM_SLEEP);
2307c478bd9Sstevel@tonic-gate 	hat->hat_as = as;
2317c478bd9Sstevel@tonic-gate 	mutex_init(&hat->hat_mutex, NULL, MUTEX_DEFAULT, NULL);
2327c478bd9Sstevel@tonic-gate 	ASSERT(hat->hat_flags == 0);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	/*
2357c478bd9Sstevel@tonic-gate 	 * a 32 bit process uses a VLP style hat when using PAE
2367c478bd9Sstevel@tonic-gate 	 */
2377c478bd9Sstevel@tonic-gate #if defined(__amd64)
2387c478bd9Sstevel@tonic-gate 	use_vlp = (ttoproc(curthread)->p_model == DATAMODEL_ILP32);
2397c478bd9Sstevel@tonic-gate #elif defined(__i386)
2407c478bd9Sstevel@tonic-gate 	use_vlp = mmu.pae_hat;
2417c478bd9Sstevel@tonic-gate #endif
2427c478bd9Sstevel@tonic-gate 	if (use_vlp) {
2437c478bd9Sstevel@tonic-gate 		hat->hat_flags = HAT_VLP;
2447c478bd9Sstevel@tonic-gate 		bzero(hat->hat_vlp_ptes, VLP_SIZE);
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	/*
2487c478bd9Sstevel@tonic-gate 	 * Allocate the htable hash
2497c478bd9Sstevel@tonic-gate 	 */
2507c478bd9Sstevel@tonic-gate 	if ((hat->hat_flags & HAT_VLP)) {
2517c478bd9Sstevel@tonic-gate 		hat->hat_num_hash = mmu.vlp_hash_cnt;
2527c478bd9Sstevel@tonic-gate 		hat->hat_ht_hash = kmem_cache_alloc(vlp_hash_cache, KM_SLEEP);
2537c478bd9Sstevel@tonic-gate 	} else {
2547c478bd9Sstevel@tonic-gate 		hat->hat_num_hash = mmu.hash_cnt;
2557c478bd9Sstevel@tonic-gate 		hat->hat_ht_hash = kmem_cache_alloc(hat_hash_cache, KM_SLEEP);
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 	bzero(hat->hat_ht_hash, hat->hat_num_hash * sizeof (htable_t *));
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	/*
2607c478bd9Sstevel@tonic-gate 	 * Initialize Kernel HAT entries at the top of the top level page
2617c478bd9Sstevel@tonic-gate 	 * table for the new hat.
2627c478bd9Sstevel@tonic-gate 	 *
2637c478bd9Sstevel@tonic-gate 	 * Note that we don't call htable_release() for the top level, that
2647c478bd9Sstevel@tonic-gate 	 * happens when the hat is destroyed in hat_free_end()
2657c478bd9Sstevel@tonic-gate 	 */
2667c478bd9Sstevel@tonic-gate 	hat->hat_htable = NULL;
2677c478bd9Sstevel@tonic-gate 	hat->hat_ht_cached = NULL;
2687c478bd9Sstevel@tonic-gate 	ht = htable_create(hat, (uintptr_t)0, TOP_LEVEL(hat), NULL);
269ae115bc7Smrj 
2707c478bd9Sstevel@tonic-gate 	if (!(hat->hat_flags & HAT_VLP))
2717c478bd9Sstevel@tonic-gate 		x86pte_copy(kas.a_hat->hat_htable, ht, khat_start,
2727c478bd9Sstevel@tonic-gate 		    khat_entries);
2737c478bd9Sstevel@tonic-gate #if defined(__i386)
2747c478bd9Sstevel@tonic-gate 	else if (khat_entries > 0)
2757c478bd9Sstevel@tonic-gate 		bcopy(vlp_page + khat_start, hat->hat_vlp_ptes + khat_start,
2767c478bd9Sstevel@tonic-gate 		    khat_entries * sizeof (x86pte_t));
2777c478bd9Sstevel@tonic-gate #endif
2787c478bd9Sstevel@tonic-gate 	hat->hat_htable = ht;
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate #if defined(__i386)
2817c478bd9Sstevel@tonic-gate 	/*
2827c478bd9Sstevel@tonic-gate 	 * PAE32 HAT alignment is less restrictive than the others to keep
2837c478bd9Sstevel@tonic-gate 	 * the kernel from using too much VA. Because of this we may need
2847c478bd9Sstevel@tonic-gate 	 * one layer further down when kernelbase isn't 1Gig aligned.
2857c478bd9Sstevel@tonic-gate 	 * See hat_free_end() for the htable_release() that goes with this
2867c478bd9Sstevel@tonic-gate 	 * htable_create()
2877c478bd9Sstevel@tonic-gate 	 */
2887c478bd9Sstevel@tonic-gate 	if (khat_pae32_htable != NULL) {
2897c478bd9Sstevel@tonic-gate 		ht = htable_create(hat, kernelbase,
2907c478bd9Sstevel@tonic-gate 		    khat_pae32_htable->ht_level, NULL);
2917c478bd9Sstevel@tonic-gate 		x86pte_copy(khat_pae32_htable, ht, khat_pae32_start,
2927c478bd9Sstevel@tonic-gate 		    khat_pae32_entries);
2937c478bd9Sstevel@tonic-gate 		ht->ht_valid_cnt = khat_pae32_entries;
2947c478bd9Sstevel@tonic-gate 	}
2957c478bd9Sstevel@tonic-gate #endif
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	/*
298a85a6733Sjosephb 	 * Put it at the start of the global list of all hats (used by stealing)
299a85a6733Sjosephb 	 *
300a85a6733Sjosephb 	 * kas.a_hat is not in the list but is instead used to find the
301a85a6733Sjosephb 	 * first and last items in the list.
302a85a6733Sjosephb 	 *
303a85a6733Sjosephb 	 * - kas.a_hat->hat_next points to the start of the user hats.
304a85a6733Sjosephb 	 *   The list ends where hat->hat_next == NULL
305a85a6733Sjosephb 	 *
306a85a6733Sjosephb 	 * - kas.a_hat->hat_prev points to the last of the user hats.
307a85a6733Sjosephb 	 *   The list begins where hat->hat_prev == NULL
3087c478bd9Sstevel@tonic-gate 	 */
3097c478bd9Sstevel@tonic-gate 	mutex_enter(&hat_list_lock);
310a85a6733Sjosephb 	hat->hat_prev = NULL;
311a85a6733Sjosephb 	hat->hat_next = kas.a_hat->hat_next;
312a85a6733Sjosephb 	if (hat->hat_next)
313a85a6733Sjosephb 		hat->hat_next->hat_prev = hat;
314a85a6733Sjosephb 	else
315a85a6733Sjosephb 		kas.a_hat->hat_prev = hat;
3167c478bd9Sstevel@tonic-gate 	kas.a_hat->hat_next = hat;
3177c478bd9Sstevel@tonic-gate 	mutex_exit(&hat_list_lock);
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	return (hat);
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate /*
3237c478bd9Sstevel@tonic-gate  * process has finished executing but as has not been cleaned up yet.
3247c478bd9Sstevel@tonic-gate  */
3257c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3267c478bd9Sstevel@tonic-gate void
3277c478bd9Sstevel@tonic-gate hat_free_start(hat_t *hat)
3287c478bd9Sstevel@tonic-gate {
3297c478bd9Sstevel@tonic-gate 	ASSERT(AS_WRITE_HELD(hat->hat_as, &hat->hat_as->a_lock));
330a85a6733Sjosephb 
331a85a6733Sjosephb 	/*
332a85a6733Sjosephb 	 * If the hat is currently a stealing victim, wait for the stealing
333a85a6733Sjosephb 	 * to finish.  Once we mark it as HAT_FREEING, htable_steal()
334a85a6733Sjosephb 	 * won't look at its pagetables anymore.
335a85a6733Sjosephb 	 */
3367c478bd9Sstevel@tonic-gate 	mutex_enter(&hat_list_lock);
337a85a6733Sjosephb 	while (hat->hat_flags & HAT_VICTIM)
338a85a6733Sjosephb 		cv_wait(&hat_list_cv, &hat_list_lock);
3397c478bd9Sstevel@tonic-gate 	hat->hat_flags |= HAT_FREEING;
3407c478bd9Sstevel@tonic-gate 	mutex_exit(&hat_list_lock);
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate /*
3447c478bd9Sstevel@tonic-gate  * An address space is being destroyed, so we destroy the associated hat.
3457c478bd9Sstevel@tonic-gate  */
3467c478bd9Sstevel@tonic-gate void
3477c478bd9Sstevel@tonic-gate hat_free_end(hat_t *hat)
3487c478bd9Sstevel@tonic-gate {
3497c478bd9Sstevel@tonic-gate 	int i;
3507c478bd9Sstevel@tonic-gate 	kmem_cache_t *cache;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate #ifdef DEBUG
3537c478bd9Sstevel@tonic-gate 	for (i = 0; i <= mmu.max_page_level; i++)
3547c478bd9Sstevel@tonic-gate 		ASSERT(hat->hat_pages_mapped[i] == 0);
3557c478bd9Sstevel@tonic-gate #endif
3567c478bd9Sstevel@tonic-gate 	ASSERT(hat->hat_flags & HAT_FREEING);
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	/*
3597c478bd9Sstevel@tonic-gate 	 * must not be running on the given hat
3607c478bd9Sstevel@tonic-gate 	 */
3617c478bd9Sstevel@tonic-gate 	ASSERT(CPU->cpu_current_hat != hat);
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	/*
364a85a6733Sjosephb 	 * Remove it from the list of HATs
3657c478bd9Sstevel@tonic-gate 	 */
3667c478bd9Sstevel@tonic-gate 	mutex_enter(&hat_list_lock);
367a85a6733Sjosephb 	if (hat->hat_prev)
368a85a6733Sjosephb 		hat->hat_prev->hat_next = hat->hat_next;
369a85a6733Sjosephb 	else
3707c478bd9Sstevel@tonic-gate 		kas.a_hat->hat_next = hat->hat_next;
371a85a6733Sjosephb 	if (hat->hat_next)
372a85a6733Sjosephb 		hat->hat_next->hat_prev = hat->hat_prev;
373a85a6733Sjosephb 	else
374a85a6733Sjosephb 		kas.a_hat->hat_prev = hat->hat_prev;
3757c478bd9Sstevel@tonic-gate 	mutex_exit(&hat_list_lock);
376a85a6733Sjosephb 	hat->hat_next = hat->hat_prev = NULL;
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	/*
3797c478bd9Sstevel@tonic-gate 	 * Make a pass through the htables freeing them all up.
3807c478bd9Sstevel@tonic-gate 	 */
3817c478bd9Sstevel@tonic-gate 	htable_purge_hat(hat);
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	/*
3847c478bd9Sstevel@tonic-gate 	 * Decide which kmem cache the hash table came from, then free it.
3857c478bd9Sstevel@tonic-gate 	 */
3867c478bd9Sstevel@tonic-gate 	if (hat->hat_flags & HAT_VLP)
3877c478bd9Sstevel@tonic-gate 		cache = vlp_hash_cache;
3887c478bd9Sstevel@tonic-gate 	else
3897c478bd9Sstevel@tonic-gate 		cache = hat_hash_cache;
3907c478bd9Sstevel@tonic-gate 	kmem_cache_free(cache, hat->hat_ht_hash);
3917c478bd9Sstevel@tonic-gate 	hat->hat_ht_hash = NULL;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	hat->hat_flags = 0;
3947c478bd9Sstevel@tonic-gate 	kmem_cache_free(hat_cache, hat);
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate /*
3987c478bd9Sstevel@tonic-gate  * round kernelbase down to a supported value to use for _userlimit
3997c478bd9Sstevel@tonic-gate  *
4007c478bd9Sstevel@tonic-gate  * userlimit must be aligned down to an entry in the top level htable.
4017c478bd9Sstevel@tonic-gate  * The one exception is for 32 bit HAT's running PAE.
4027c478bd9Sstevel@tonic-gate  */
4037c478bd9Sstevel@tonic-gate uintptr_t
4047c478bd9Sstevel@tonic-gate hat_kernelbase(uintptr_t va)
4057c478bd9Sstevel@tonic-gate {
4067c478bd9Sstevel@tonic-gate #if defined(__i386)
4077c478bd9Sstevel@tonic-gate 	va &= LEVEL_MASK(1);
4087c478bd9Sstevel@tonic-gate #endif
4097c478bd9Sstevel@tonic-gate 	if (IN_VA_HOLE(va))
4107c478bd9Sstevel@tonic-gate 		panic("_userlimit %p will fall in VA hole\n", (void *)va);
4117c478bd9Sstevel@tonic-gate 	return (va);
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate /*
4157c478bd9Sstevel@tonic-gate  * Initialize hat data structures based on processor MMU information.
4167c478bd9Sstevel@tonic-gate  */
4177c478bd9Sstevel@tonic-gate void
4187c478bd9Sstevel@tonic-gate mmu_init(void)
4197c478bd9Sstevel@tonic-gate {
4207c478bd9Sstevel@tonic-gate 	uint_t max_htables;
4217c478bd9Sstevel@tonic-gate 	uint_t pa_bits;
4227c478bd9Sstevel@tonic-gate 	uint_t va_bits;
4237c478bd9Sstevel@tonic-gate 	int i;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	/*
426ae115bc7Smrj 	 * If CPU enabled the page table global bit, use it for the kernel
427ae115bc7Smrj 	 * This is bit 7 in CR4 (PGE - Page Global Enable).
4287c478bd9Sstevel@tonic-gate 	 */
429ae115bc7Smrj 	if ((x86_feature & X86_PGE) != 0 && (getcr4() & CR4_PGE) != 0)
4307c478bd9Sstevel@tonic-gate 		mmu.pt_global = PT_GLOBAL;
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	/*
433ae115bc7Smrj 	 * Detect NX and PAE usage.
4347c478bd9Sstevel@tonic-gate 	 */
435ae115bc7Smrj 	mmu.pae_hat = kbm_pae_support;
436ae115bc7Smrj 	if (kbm_nx_support)
4377c478bd9Sstevel@tonic-gate 		mmu.pt_nx = PT_NX;
438ae115bc7Smrj 	else
4397c478bd9Sstevel@tonic-gate 		mmu.pt_nx = 0;
4407c478bd9Sstevel@tonic-gate 
441935f8dd0Sjosephb 	/*
442935f8dd0Sjosephb 	 * Intel CPUs allow speculative caching (in TLB-like h/w) of
443935f8dd0Sjosephb 	 * entries in upper page tables even though there may not be
444935f8dd0Sjosephb 	 * any valid entries in lower tables. This implies we have to
445935f8dd0Sjosephb 	 * re-INVLPG at every upper page table entry invalidation.
446935f8dd0Sjosephb 	 */
447935f8dd0Sjosephb 	if (cpuid_getvendor(CPU) == X86_VENDOR_Intel)
448935f8dd0Sjosephb 		mmu.inval_nonleaf = 1;
449935f8dd0Sjosephb 	else
450935f8dd0Sjosephb 		mmu.inval_nonleaf = 0;
4517c478bd9Sstevel@tonic-gate 	/*
4527c478bd9Sstevel@tonic-gate 	 * Use CPU info to set various MMU parameters
4537c478bd9Sstevel@tonic-gate 	 */
4547c478bd9Sstevel@tonic-gate 	cpuid_get_addrsize(CPU, &pa_bits, &va_bits);
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	if (va_bits < sizeof (void *) * NBBY) {
4577c478bd9Sstevel@tonic-gate 		mmu.hole_start = (1ul << (va_bits - 1));
4587c478bd9Sstevel@tonic-gate 		mmu.hole_end = 0ul - mmu.hole_start - 1;
4597c478bd9Sstevel@tonic-gate 	} else {
4607c478bd9Sstevel@tonic-gate 		mmu.hole_end = 0;
4617c478bd9Sstevel@tonic-gate 		mmu.hole_start = mmu.hole_end - 1;
4627c478bd9Sstevel@tonic-gate 	}
4637c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_121)
4647c478bd9Sstevel@tonic-gate 	/*
4657c478bd9Sstevel@tonic-gate 	 * If erratum 121 has already been detected at this time, hole_start
4667c478bd9Sstevel@tonic-gate 	 * contains the value to be subtracted from mmu.hole_start.
4677c478bd9Sstevel@tonic-gate 	 */
4687c478bd9Sstevel@tonic-gate 	ASSERT(hole_start == 0 || opteron_erratum_121 != 0);
4697c478bd9Sstevel@tonic-gate 	hole_start = mmu.hole_start - hole_start;
4707c478bd9Sstevel@tonic-gate #else
4717c478bd9Sstevel@tonic-gate 	hole_start = mmu.hole_start;
4727c478bd9Sstevel@tonic-gate #endif
4737c478bd9Sstevel@tonic-gate 	hole_end = mmu.hole_end;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	mmu.highest_pfn = mmu_btop((1ull << pa_bits) - 1);
4767c478bd9Sstevel@tonic-gate 	if (mmu.pae_hat == 0 && pa_bits > 32)
4777c478bd9Sstevel@tonic-gate 		mmu.highest_pfn = PFN_4G - 1;
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	if (mmu.pae_hat) {
4807c478bd9Sstevel@tonic-gate 		mmu.pte_size = 8;	/* 8 byte PTEs */
4817c478bd9Sstevel@tonic-gate 		mmu.pte_size_shift = 3;
4827c478bd9Sstevel@tonic-gate 	} else {
4837c478bd9Sstevel@tonic-gate 		mmu.pte_size = 4;	/* 4 byte PTEs */
4847c478bd9Sstevel@tonic-gate 		mmu.pte_size_shift = 2;
4857c478bd9Sstevel@tonic-gate 	}
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	if (mmu.pae_hat && (x86_feature & X86_PAE) == 0)
4887c478bd9Sstevel@tonic-gate 		panic("Processor does not support PAE");
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	if ((x86_feature & X86_CX8) == 0)
4917c478bd9Sstevel@tonic-gate 		panic("Processor does not support cmpxchg8b instruction");
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	/*
4947c478bd9Sstevel@tonic-gate 	 * Initialize parameters based on the 64 or 32 bit kernels and
4957c478bd9Sstevel@tonic-gate 	 * for the 32 bit kernel decide if we should use PAE.
4967c478bd9Sstevel@tonic-gate 	 */
497ae115bc7Smrj 	if (kbm_largepage_support)
4987c478bd9Sstevel@tonic-gate 		mmu.max_page_level = 1;
4997c478bd9Sstevel@tonic-gate 	else
5007c478bd9Sstevel@tonic-gate 		mmu.max_page_level = 0;
5017c478bd9Sstevel@tonic-gate 	mmu_page_sizes = mmu.max_page_level + 1;
5027c478bd9Sstevel@tonic-gate 	mmu_exported_page_sizes = mmu_page_sizes;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate #if defined(__amd64)
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	mmu.num_level = 4;
5077c478bd9Sstevel@tonic-gate 	mmu.max_level = 3;
5087c478bd9Sstevel@tonic-gate 	mmu.ptes_per_table = 512;
5097c478bd9Sstevel@tonic-gate 	mmu.top_level_count = 512;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	mmu.level_shift[0] = 12;
5127c478bd9Sstevel@tonic-gate 	mmu.level_shift[1] = 21;
5137c478bd9Sstevel@tonic-gate 	mmu.level_shift[2] = 30;
5147c478bd9Sstevel@tonic-gate 	mmu.level_shift[3] = 39;
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate #elif defined(__i386)
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	if (mmu.pae_hat) {
5197c478bd9Sstevel@tonic-gate 		mmu.num_level = 3;
5207c478bd9Sstevel@tonic-gate 		mmu.max_level = 2;
5217c478bd9Sstevel@tonic-gate 		mmu.ptes_per_table = 512;
5227c478bd9Sstevel@tonic-gate 		mmu.top_level_count = 4;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 		mmu.level_shift[0] = 12;
5257c478bd9Sstevel@tonic-gate 		mmu.level_shift[1] = 21;
5267c478bd9Sstevel@tonic-gate 		mmu.level_shift[2] = 30;
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	} else {
5297c478bd9Sstevel@tonic-gate 		mmu.num_level = 2;
5307c478bd9Sstevel@tonic-gate 		mmu.max_level = 1;
5317c478bd9Sstevel@tonic-gate 		mmu.ptes_per_table = 1024;
5327c478bd9Sstevel@tonic-gate 		mmu.top_level_count = 1024;
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 		mmu.level_shift[0] = 12;
5357c478bd9Sstevel@tonic-gate 		mmu.level_shift[1] = 22;
5367c478bd9Sstevel@tonic-gate 	}
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate #endif	/* __i386 */
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	for (i = 0; i < mmu.num_level; ++i) {
5417c478bd9Sstevel@tonic-gate 		mmu.level_size[i] = 1UL << mmu.level_shift[i];
5427c478bd9Sstevel@tonic-gate 		mmu.level_offset[i] = mmu.level_size[i] - 1;
5437c478bd9Sstevel@tonic-gate 		mmu.level_mask[i] = ~mmu.level_offset[i];
5447c478bd9Sstevel@tonic-gate 	}
5457c478bd9Sstevel@tonic-gate 
546ae115bc7Smrj 	for (i = 0; i <= mmu.max_page_level; ++i) {
547ae115bc7Smrj 		mmu.pte_bits[i] = PT_VALID;
548ae115bc7Smrj 		if (i > 0)
549ae115bc7Smrj 			mmu.pte_bits[i] |= PT_PAGESIZE;
550ae115bc7Smrj 	}
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	/*
5537c478bd9Sstevel@tonic-gate 	 * NOTE Legacy 32 bit PAE mode only has the P_VALID bit at top level.
5547c478bd9Sstevel@tonic-gate 	 */
5557c478bd9Sstevel@tonic-gate 	for (i = 1; i < mmu.num_level; ++i)
5567c478bd9Sstevel@tonic-gate 		mmu.ptp_bits[i] = PT_PTPBITS;
557ae115bc7Smrj 
5587c478bd9Sstevel@tonic-gate #if defined(__i386)
5597c478bd9Sstevel@tonic-gate 	mmu.ptp_bits[2] = PT_VALID;
5607c478bd9Sstevel@tonic-gate #endif
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	/*
5637c478bd9Sstevel@tonic-gate 	 * Compute how many hash table entries to have per process for htables.
5647c478bd9Sstevel@tonic-gate 	 * We start with 1 page's worth of entries.
5657c478bd9Sstevel@tonic-gate 	 *
5667c478bd9Sstevel@tonic-gate 	 * If physical memory is small, reduce the amount need to cover it.
5677c478bd9Sstevel@tonic-gate 	 */
5687c478bd9Sstevel@tonic-gate 	max_htables = physmax / mmu.ptes_per_table;
5697c478bd9Sstevel@tonic-gate 	mmu.hash_cnt = MMU_PAGESIZE / sizeof (htable_t *);
5707c478bd9Sstevel@tonic-gate 	while (mmu.hash_cnt > 16 && mmu.hash_cnt >= max_htables)
5717c478bd9Sstevel@tonic-gate 		mmu.hash_cnt >>= 1;
5727c478bd9Sstevel@tonic-gate 	mmu.vlp_hash_cnt = mmu.hash_cnt;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate #if defined(__amd64)
5757c478bd9Sstevel@tonic-gate 	/*
5767c478bd9Sstevel@tonic-gate 	 * If running in 64 bits and physical memory is large,
5777c478bd9Sstevel@tonic-gate 	 * increase the size of the cache to cover all of memory for
5787c478bd9Sstevel@tonic-gate 	 * a 64 bit process.
5797c478bd9Sstevel@tonic-gate 	 */
5807c478bd9Sstevel@tonic-gate #define	HASH_MAX_LENGTH 4
5817c478bd9Sstevel@tonic-gate 	while (mmu.hash_cnt * HASH_MAX_LENGTH < max_htables)
5827c478bd9Sstevel@tonic-gate 		mmu.hash_cnt <<= 1;
5837c478bd9Sstevel@tonic-gate #endif
5847c478bd9Sstevel@tonic-gate }
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate /*
5887c478bd9Sstevel@tonic-gate  * initialize hat data structures
5897c478bd9Sstevel@tonic-gate  */
5907c478bd9Sstevel@tonic-gate void
5917c478bd9Sstevel@tonic-gate hat_init()
5927c478bd9Sstevel@tonic-gate {
5937c478bd9Sstevel@tonic-gate #if defined(__i386)
5947c478bd9Sstevel@tonic-gate 	/*
5957c478bd9Sstevel@tonic-gate 	 * _userlimit must be aligned correctly
5967c478bd9Sstevel@tonic-gate 	 */
5977c478bd9Sstevel@tonic-gate 	if ((_userlimit & LEVEL_MASK(1)) != _userlimit) {
5987c478bd9Sstevel@tonic-gate 		prom_printf("hat_init(): _userlimit=%p, not aligned at %p\n",
5997c478bd9Sstevel@tonic-gate 		    (void *)_userlimit, (void *)LEVEL_SIZE(1));
6007c478bd9Sstevel@tonic-gate 		halt("hat_init(): Unable to continue");
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate #endif
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	cv_init(&hat_list_cv, NULL, CV_DEFAULT, NULL);
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	/*
6077c478bd9Sstevel@tonic-gate 	 * initialize kmem caches
6087c478bd9Sstevel@tonic-gate 	 */
6097c478bd9Sstevel@tonic-gate 	htable_init();
6107c478bd9Sstevel@tonic-gate 	hment_init();
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	hat_cache = kmem_cache_create("hat_t",
6137c478bd9Sstevel@tonic-gate 	    sizeof (hat_t), 0, hati_constructor, NULL, NULL,
6147c478bd9Sstevel@tonic-gate 	    NULL, 0, 0);
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	hat_hash_cache = kmem_cache_create("HatHash",
6177c478bd9Sstevel@tonic-gate 	    mmu.hash_cnt * sizeof (htable_t *), 0, NULL, NULL, NULL,
6187c478bd9Sstevel@tonic-gate 	    NULL, 0, 0);
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	/*
6217c478bd9Sstevel@tonic-gate 	 * VLP hats can use a smaller hash table size on large memroy machines
6227c478bd9Sstevel@tonic-gate 	 */
6237c478bd9Sstevel@tonic-gate 	if (mmu.hash_cnt == mmu.vlp_hash_cnt) {
6247c478bd9Sstevel@tonic-gate 		vlp_hash_cache = hat_hash_cache;
6257c478bd9Sstevel@tonic-gate 	} else {
6267c478bd9Sstevel@tonic-gate 		vlp_hash_cache = kmem_cache_create("HatVlpHash",
6277c478bd9Sstevel@tonic-gate 		    mmu.vlp_hash_cnt * sizeof (htable_t *), 0, NULL, NULL, NULL,
6287c478bd9Sstevel@tonic-gate 		    NULL, 0, 0);
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	/*
6327c478bd9Sstevel@tonic-gate 	 * Set up the kernel's hat
6337c478bd9Sstevel@tonic-gate 	 */
6347c478bd9Sstevel@tonic-gate 	AS_LOCK_ENTER(&kas, &kas.a_lock, RW_WRITER);
6357c478bd9Sstevel@tonic-gate 	kas.a_hat = kmem_cache_alloc(hat_cache, KM_NOSLEEP);
6367c478bd9Sstevel@tonic-gate 	mutex_init(&kas.a_hat->hat_mutex, NULL, MUTEX_DEFAULT, NULL);
6377c478bd9Sstevel@tonic-gate 	kas.a_hat->hat_as = &kas;
6387c478bd9Sstevel@tonic-gate 	kas.a_hat->hat_flags = 0;
6397c478bd9Sstevel@tonic-gate 	AS_LOCK_EXIT(&kas, &kas.a_lock);
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	CPUSET_ZERO(khat_cpuset);
6427c478bd9Sstevel@tonic-gate 	CPUSET_ADD(khat_cpuset, CPU->cpu_id);
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	/*
6457c478bd9Sstevel@tonic-gate 	 * The kernel hat's next pointer serves as the head of the hat list .
646a85a6733Sjosephb 	 * The kernel hat's prev pointer tracks the last hat on the list for
647a85a6733Sjosephb 	 * htable_steal() to use.
6487c478bd9Sstevel@tonic-gate 	 */
6497c478bd9Sstevel@tonic-gate 	kas.a_hat->hat_next = NULL;
650a85a6733Sjosephb 	kas.a_hat->hat_prev = NULL;
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	/*
6537c478bd9Sstevel@tonic-gate 	 * Allocate an htable hash bucket for the kernel
6547c478bd9Sstevel@tonic-gate 	 * XX64 - tune for 64 bit procs
6557c478bd9Sstevel@tonic-gate 	 */
6567c478bd9Sstevel@tonic-gate 	kas.a_hat->hat_num_hash = mmu.hash_cnt;
6577c478bd9Sstevel@tonic-gate 	kas.a_hat->hat_ht_hash = kmem_cache_alloc(hat_hash_cache, KM_NOSLEEP);
6587c478bd9Sstevel@tonic-gate 	bzero(kas.a_hat->hat_ht_hash, mmu.hash_cnt * sizeof (htable_t *));
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	/*
6617c478bd9Sstevel@tonic-gate 	 * zero out the top level and cached htable pointers
6627c478bd9Sstevel@tonic-gate 	 */
6637c478bd9Sstevel@tonic-gate 	kas.a_hat->hat_ht_cached = NULL;
6647c478bd9Sstevel@tonic-gate 	kas.a_hat->hat_htable = NULL;
6659d9461f9Strevtom 
6669d9461f9Strevtom 	/*
6679d9461f9Strevtom 	 * Pre-allocate hrm_hashtab before enabling the collection of
6689d9461f9Strevtom 	 * refmod statistics.  Allocating on the fly would mean us
6699d9461f9Strevtom 	 * running the risk of suffering recursive mutex enters or
6709d9461f9Strevtom 	 * deadlocks.
6719d9461f9Strevtom 	 */
6729d9461f9Strevtom 	hrm_hashtab = kmem_zalloc(HRM_HASHSIZE * sizeof (struct hrmstat *),
6739d9461f9Strevtom 	    KM_SLEEP);
6747c478bd9Sstevel@tonic-gate }
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate /*
6777c478bd9Sstevel@tonic-gate  * Prepare CPU specific pagetables for VLP processes on 64 bit kernels.
6787c478bd9Sstevel@tonic-gate  *
6797c478bd9Sstevel@tonic-gate  * Each CPU has a set of 2 pagetables that are reused for any 32 bit
6807c478bd9Sstevel@tonic-gate  * process it runs. They are the top level pagetable, hci_vlp_l3ptes, and
6817c478bd9Sstevel@tonic-gate  * the next to top level table for the bottom 512 Gig, hci_vlp_l2ptes.
6827c478bd9Sstevel@tonic-gate  */
6837c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6847c478bd9Sstevel@tonic-gate static void
6857c478bd9Sstevel@tonic-gate hat_vlp_setup(struct cpu *cpu)
6867c478bd9Sstevel@tonic-gate {
6877c478bd9Sstevel@tonic-gate #if defined(__amd64)
6887c478bd9Sstevel@tonic-gate 	struct hat_cpu_info *hci = cpu->cpu_hat_info;
6897c478bd9Sstevel@tonic-gate 	pfn_t pfn;
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	/*
6927c478bd9Sstevel@tonic-gate 	 * allocate the level==2 page table for the bottom most
6937c478bd9Sstevel@tonic-gate 	 * 512Gig of address space (this is where 32 bit apps live)
6947c478bd9Sstevel@tonic-gate 	 */
6957c478bd9Sstevel@tonic-gate 	ASSERT(hci != NULL);
6967c478bd9Sstevel@tonic-gate 	hci->hci_vlp_l2ptes = kmem_zalloc(MMU_PAGESIZE, KM_SLEEP);
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	/*
6997c478bd9Sstevel@tonic-gate 	 * Allocate a top level pagetable and copy the kernel's
7007c478bd9Sstevel@tonic-gate 	 * entries into it. Then link in hci_vlp_l2ptes in the 1st entry.
7017c478bd9Sstevel@tonic-gate 	 */
7027c478bd9Sstevel@tonic-gate 	hci->hci_vlp_l3ptes = kmem_zalloc(MMU_PAGESIZE, KM_SLEEP);
7037c478bd9Sstevel@tonic-gate 	hci->hci_vlp_pfn =
7047c478bd9Sstevel@tonic-gate 	    hat_getpfnum(kas.a_hat, (caddr_t)hci->hci_vlp_l3ptes);
7057c478bd9Sstevel@tonic-gate 	ASSERT(hci->hci_vlp_pfn != PFN_INVALID);
7067c478bd9Sstevel@tonic-gate 	bcopy(vlp_page + khat_start, hci->hci_vlp_l3ptes + khat_start,
7077c478bd9Sstevel@tonic-gate 	    khat_entries * sizeof (x86pte_t));
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	pfn = hat_getpfnum(kas.a_hat, (caddr_t)hci->hci_vlp_l2ptes);
7107c478bd9Sstevel@tonic-gate 	ASSERT(pfn != PFN_INVALID);
7117c478bd9Sstevel@tonic-gate 	hci->hci_vlp_l3ptes[0] = MAKEPTP(pfn, 2);
7127c478bd9Sstevel@tonic-gate #endif /* __amd64 */
7137c478bd9Sstevel@tonic-gate }
7147c478bd9Sstevel@tonic-gate 
715ae115bc7Smrj /*ARGSUSED*/
716ae115bc7Smrj static void
717ae115bc7Smrj hat_vlp_teardown(cpu_t *cpu)
718ae115bc7Smrj {
719ae115bc7Smrj #if defined(__amd64)
720ae115bc7Smrj 	struct hat_cpu_info *hci;
721ae115bc7Smrj 
722ae115bc7Smrj 	if ((hci = cpu->cpu_hat_info) == NULL)
723ae115bc7Smrj 		return;
724ae115bc7Smrj 	if (hci->hci_vlp_l2ptes)
725ae115bc7Smrj 		kmem_free(hci->hci_vlp_l2ptes, MMU_PAGESIZE);
726ae115bc7Smrj 	if (hci->hci_vlp_l3ptes)
727ae115bc7Smrj 		kmem_free(hci->hci_vlp_l3ptes, MMU_PAGESIZE);
728ae115bc7Smrj #endif	/* __amd64 */
729ae115bc7Smrj }
730ae115bc7Smrj 
7317c478bd9Sstevel@tonic-gate /*
7327c478bd9Sstevel@tonic-gate  * Finish filling in the kernel hat.
7337c478bd9Sstevel@tonic-gate  * Pre fill in all top level kernel page table entries for the kernel's
7347c478bd9Sstevel@tonic-gate  * part of the address range.  From this point on we can't use any new
7357c478bd9Sstevel@tonic-gate  * kernel large pages if they need PTE's at max_level
736ae115bc7Smrj  *
737ae115bc7Smrj  * create the kmap mappings.
7387c478bd9Sstevel@tonic-gate  */
7397c478bd9Sstevel@tonic-gate void
7407c478bd9Sstevel@tonic-gate hat_init_finish(void)
7417c478bd9Sstevel@tonic-gate {
7427c478bd9Sstevel@tonic-gate 	htable_t	*top = kas.a_hat->hat_htable;
7437c478bd9Sstevel@tonic-gate 	htable_t	*ht;
7447c478bd9Sstevel@tonic-gate 	uint_t		e;
7457c478bd9Sstevel@tonic-gate 	x86pte_t	pte;
7467c478bd9Sstevel@tonic-gate 	uintptr_t	va = kernelbase;
747ae115bc7Smrj 	size_t		size;
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate #if defined(__i386)
7517c478bd9Sstevel@tonic-gate 	ASSERT((va & LEVEL_MASK(1)) == va);
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	/*
7547c478bd9Sstevel@tonic-gate 	 * Deal with kernelbase not 1Gig aligned for 32 bit PAE hats.
7557c478bd9Sstevel@tonic-gate 	 */
7567c478bd9Sstevel@tonic-gate 	if (!mmu.pae_hat || (va & LEVEL_OFFSET(mmu.max_level)) == 0) {
7577c478bd9Sstevel@tonic-gate 		khat_pae32_htable = NULL;
7587c478bd9Sstevel@tonic-gate 	} else {
7597c478bd9Sstevel@tonic-gate 		ASSERT(mmu.max_level == 2);
7607c478bd9Sstevel@tonic-gate 		ASSERT((va & LEVEL_OFFSET(mmu.max_level - 1)) == 0);
7617c478bd9Sstevel@tonic-gate 		khat_pae32_htable =
7627c478bd9Sstevel@tonic-gate 		    htable_create(kas.a_hat, va, mmu.max_level - 1, NULL);
7637c478bd9Sstevel@tonic-gate 		khat_pae32_start = htable_va2entry(va, khat_pae32_htable);
7647c478bd9Sstevel@tonic-gate 		khat_pae32_entries = mmu.ptes_per_table - khat_pae32_start;
7657c478bd9Sstevel@tonic-gate 		for (e = khat_pae32_start; e < mmu.ptes_per_table;
7667c478bd9Sstevel@tonic-gate 		    ++e, va += LEVEL_SIZE(mmu.max_level - 1)) {
7677c478bd9Sstevel@tonic-gate 			pte = x86pte_get(khat_pae32_htable, e);
7687c478bd9Sstevel@tonic-gate 			if (PTE_ISVALID(pte))
7697c478bd9Sstevel@tonic-gate 				continue;
7707c478bd9Sstevel@tonic-gate 			ht = htable_create(kas.a_hat, va, mmu.max_level - 2,
7717c478bd9Sstevel@tonic-gate 			    NULL);
7727c478bd9Sstevel@tonic-gate 			ASSERT(ht != NULL);
7737c478bd9Sstevel@tonic-gate 		}
7747c478bd9Sstevel@tonic-gate 	}
7757c478bd9Sstevel@tonic-gate #endif
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 	/*
7787c478bd9Sstevel@tonic-gate 	 * The kernel hat will need fixed values in the highest level
7797c478bd9Sstevel@tonic-gate 	 * ptable for copying to all other hat's. This implies
7807c478bd9Sstevel@tonic-gate 	 * alignment restrictions on _userlimit.
7817c478bd9Sstevel@tonic-gate 	 *
7827c478bd9Sstevel@tonic-gate 	 * Note we don't htable_release() these htables. This keeps them
7837c478bd9Sstevel@tonic-gate 	 * from ever being stolen or free'd.
7847c478bd9Sstevel@tonic-gate 	 *
7857c478bd9Sstevel@tonic-gate 	 * top_level_count is used instead of ptes_per_table, since
7867c478bd9Sstevel@tonic-gate 	 * on 32-bit PAE we only have 4 usable entries at the top level ptable.
7877c478bd9Sstevel@tonic-gate 	 */
7887c478bd9Sstevel@tonic-gate 	if (va == 0)
7897c478bd9Sstevel@tonic-gate 		khat_start = mmu.top_level_count;
7907c478bd9Sstevel@tonic-gate 	else
7917c478bd9Sstevel@tonic-gate 		khat_start = htable_va2entry(va, kas.a_hat->hat_htable);
7927c478bd9Sstevel@tonic-gate 	khat_entries = mmu.top_level_count - khat_start;
7937c478bd9Sstevel@tonic-gate 	for (e = khat_start; e < mmu.top_level_count;
7947c478bd9Sstevel@tonic-gate 	    ++e, va += LEVEL_SIZE(mmu.max_level)) {
795ae115bc7Smrj 		if (IN_HYPERVISOR_VA(va))
796ae115bc7Smrj 			continue;
7977c478bd9Sstevel@tonic-gate 		pte = x86pte_get(top, e);
7987c478bd9Sstevel@tonic-gate 		if (PTE_ISVALID(pte))
7997c478bd9Sstevel@tonic-gate 			continue;
8007c478bd9Sstevel@tonic-gate 		ht = htable_create(kas.a_hat, va, mmu.max_level - 1, NULL);
8017c478bd9Sstevel@tonic-gate 		ASSERT(ht != NULL);
8027c478bd9Sstevel@tonic-gate 	}
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	/*
8057c478bd9Sstevel@tonic-gate 	 * We are now effectively running on the kernel hat.
8067c478bd9Sstevel@tonic-gate 	 * Clearing use_boot_reserve shuts off using the pre-allocated boot
8077c478bd9Sstevel@tonic-gate 	 * reserve for all HAT allocations.  From here on, the reserves are
8087c478bd9Sstevel@tonic-gate 	 * only used when mapping in memory for the hat's own allocations.
8097c478bd9Sstevel@tonic-gate 	 */
8107c478bd9Sstevel@tonic-gate 	use_boot_reserve = 0;
8117c478bd9Sstevel@tonic-gate 	htable_adjust_reserve();
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	/*
8147c478bd9Sstevel@tonic-gate 	 * 32 bit kernels use only 4 of the 512 entries in its top level
8157c478bd9Sstevel@tonic-gate 	 * pagetable. We'll use the remainder for the "per CPU" page tables
8167c478bd9Sstevel@tonic-gate 	 * for VLP processes.
8177c478bd9Sstevel@tonic-gate 	 *
818ae115bc7Smrj 	 * We also map the top level kernel pagetable into the kernel to make
819ae115bc7Smrj 	 * it easy to use bcopy to initialize new address spaces.
8207c478bd9Sstevel@tonic-gate 	 */
8217c478bd9Sstevel@tonic-gate 	if (mmu.pae_hat) {
8227c478bd9Sstevel@tonic-gate 		vlp_page = vmem_alloc(heap_arena, MMU_PAGESIZE, VM_SLEEP);
8237c478bd9Sstevel@tonic-gate 		hat_devload(kas.a_hat, (caddr_t)vlp_page, MMU_PAGESIZE,
8247c478bd9Sstevel@tonic-gate 		    kas.a_hat->hat_htable->ht_pfn,
825ae115bc7Smrj 		    PROT_WRITE |
826ae115bc7Smrj 		    PROT_READ | HAT_NOSYNC | HAT_UNORDERED_OK,
8277c478bd9Sstevel@tonic-gate 		    HAT_LOAD | HAT_LOAD_NOCONSIST);
8287c478bd9Sstevel@tonic-gate 	}
8297c478bd9Sstevel@tonic-gate 	hat_vlp_setup(CPU);
830ae115bc7Smrj 
831ae115bc7Smrj 	/*
832ae115bc7Smrj 	 * Create kmap (cached mappings of kernel PTEs)
833ae115bc7Smrj 	 * for 32 bit we map from segmap_start .. ekernelheap
834ae115bc7Smrj 	 * for 64 bit we map from segmap_start .. segmap_start + segmapsize;
835ae115bc7Smrj 	 */
836ae115bc7Smrj #if defined(__i386)
837ae115bc7Smrj 	size = (uintptr_t)ekernelheap - segmap_start;
838ae115bc7Smrj #elif defined(__amd64)
839ae115bc7Smrj 	size = segmapsize;
840ae115bc7Smrj #endif
841ae115bc7Smrj 	hat_kmap_init((uintptr_t)segmap_start, size);
8427c478bd9Sstevel@tonic-gate }
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate /*
8457c478bd9Sstevel@tonic-gate  * On 32 bit PAE mode, PTE's are 64 bits, but ordinary atomic memory references
8467c478bd9Sstevel@tonic-gate  * are 32 bit, so for safety we must use cas64() to install these.
8477c478bd9Sstevel@tonic-gate  */
8487c478bd9Sstevel@tonic-gate #ifdef __i386
8497c478bd9Sstevel@tonic-gate static void
8507c478bd9Sstevel@tonic-gate reload_pae32(hat_t *hat, cpu_t *cpu)
8517c478bd9Sstevel@tonic-gate {
8527c478bd9Sstevel@tonic-gate 	x86pte_t *src;
8537c478bd9Sstevel@tonic-gate 	x86pte_t *dest;
8547c478bd9Sstevel@tonic-gate 	x86pte_t pte;
8557c478bd9Sstevel@tonic-gate 	int i;
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	/*
8587c478bd9Sstevel@tonic-gate 	 * Load the 4 entries of the level 2 page table into this
8597c478bd9Sstevel@tonic-gate 	 * cpu's range of the vlp_page and point cr3 at them.
8607c478bd9Sstevel@tonic-gate 	 */
8617c478bd9Sstevel@tonic-gate 	ASSERT(mmu.pae_hat);
8627c478bd9Sstevel@tonic-gate 	src = hat->hat_vlp_ptes;
8637c478bd9Sstevel@tonic-gate 	dest = vlp_page + (cpu->cpu_id + 1) * VLP_NUM_PTES;
8647c478bd9Sstevel@tonic-gate 	for (i = 0; i < VLP_NUM_PTES; ++i) {
8657c478bd9Sstevel@tonic-gate 		for (;;) {
8667c478bd9Sstevel@tonic-gate 			pte = dest[i];
8677c478bd9Sstevel@tonic-gate 			if (pte == src[i])
8687c478bd9Sstevel@tonic-gate 				break;
8697c478bd9Sstevel@tonic-gate 			if (cas64(dest + i, pte, src[i]) != src[i])
8707c478bd9Sstevel@tonic-gate 				break;
8717c478bd9Sstevel@tonic-gate 		}
8727c478bd9Sstevel@tonic-gate 	}
8737c478bd9Sstevel@tonic-gate }
8747c478bd9Sstevel@tonic-gate #endif
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate /*
8777c478bd9Sstevel@tonic-gate  * Switch to a new active hat, maintaining bit masks to track active CPUs.
8787c478bd9Sstevel@tonic-gate  */
8797c478bd9Sstevel@tonic-gate void
8807c478bd9Sstevel@tonic-gate hat_switch(hat_t *hat)
8817c478bd9Sstevel@tonic-gate {
8827c478bd9Sstevel@tonic-gate 	uintptr_t	newcr3;
8837c478bd9Sstevel@tonic-gate 	cpu_t		*cpu = CPU;
8847c478bd9Sstevel@tonic-gate 	hat_t		*old = cpu->cpu_current_hat;
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	/*
8877c478bd9Sstevel@tonic-gate 	 * set up this information first, so we don't miss any cross calls
8887c478bd9Sstevel@tonic-gate 	 */
8897c478bd9Sstevel@tonic-gate 	if (old != NULL) {
8907c478bd9Sstevel@tonic-gate 		if (old == hat)
8917c478bd9Sstevel@tonic-gate 			return;
8927c478bd9Sstevel@tonic-gate 		if (old != kas.a_hat)
8937c478bd9Sstevel@tonic-gate 			CPUSET_ATOMIC_DEL(old->hat_cpus, cpu->cpu_id);
8947c478bd9Sstevel@tonic-gate 	}
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	/*
89795c0a3c8Sjosephb 	 * Add this CPU to the active set for this HAT.
8987c478bd9Sstevel@tonic-gate 	 */
8997c478bd9Sstevel@tonic-gate 	if (hat != kas.a_hat) {
9007c478bd9Sstevel@tonic-gate 		CPUSET_ATOMIC_ADD(hat->hat_cpus, cpu->cpu_id);
9017c478bd9Sstevel@tonic-gate 	}
9027c478bd9Sstevel@tonic-gate 	cpu->cpu_current_hat = hat;
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	/*
9057c478bd9Sstevel@tonic-gate 	 * now go ahead and load cr3
9067c478bd9Sstevel@tonic-gate 	 */
9077c478bd9Sstevel@tonic-gate 	if (hat->hat_flags & HAT_VLP) {
9087c478bd9Sstevel@tonic-gate #if defined(__amd64)
9097c478bd9Sstevel@tonic-gate 		x86pte_t *vlpptep = cpu->cpu_hat_info->hci_vlp_l2ptes;
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 		VLP_COPY(hat->hat_vlp_ptes, vlpptep);
9127c478bd9Sstevel@tonic-gate 		newcr3 = MAKECR3(cpu->cpu_hat_info->hci_vlp_pfn);
9137c478bd9Sstevel@tonic-gate #elif defined(__i386)
9147c478bd9Sstevel@tonic-gate 		reload_pae32(hat, cpu);
9157c478bd9Sstevel@tonic-gate 		newcr3 = MAKECR3(kas.a_hat->hat_htable->ht_pfn) +
9167c478bd9Sstevel@tonic-gate 		    (cpu->cpu_id + 1) * VLP_SIZE;
9177c478bd9Sstevel@tonic-gate #endif
9187c478bd9Sstevel@tonic-gate 	} else {
9197c478bd9Sstevel@tonic-gate 		newcr3 = MAKECR3(hat->hat_htable->ht_pfn);
9207c478bd9Sstevel@tonic-gate 	}
9217c478bd9Sstevel@tonic-gate 	setcr3(newcr3);
9227c478bd9Sstevel@tonic-gate 	ASSERT(cpu == CPU);
9237c478bd9Sstevel@tonic-gate }
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate /*
9267c478bd9Sstevel@tonic-gate  * Utility to return a valid x86pte_t from protections, pfn, and level number
9277c478bd9Sstevel@tonic-gate  */
9287c478bd9Sstevel@tonic-gate static x86pte_t
9297c478bd9Sstevel@tonic-gate hati_mkpte(pfn_t pfn, uint_t attr, level_t level, uint_t flags)
9307c478bd9Sstevel@tonic-gate {
9317c478bd9Sstevel@tonic-gate 	x86pte_t	pte;
9327c478bd9Sstevel@tonic-gate 	uint_t		cache_attr = attr & HAT_ORDER_MASK;
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	pte = MAKEPTE(pfn, level);
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	if (attr & PROT_WRITE)
9377c478bd9Sstevel@tonic-gate 		PTE_SET(pte, PT_WRITABLE);
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 	if (attr & PROT_USER)
9407c478bd9Sstevel@tonic-gate 		PTE_SET(pte, PT_USER);
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	if (!(attr & PROT_EXEC))
9437c478bd9Sstevel@tonic-gate 		PTE_SET(pte, mmu.pt_nx);
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	/*
946ae115bc7Smrj 	 * Set the software bits used track ref/mod sync's and hments.
947ae115bc7Smrj 	 * If not using REF/MOD, set them to avoid h/w rewriting PTEs.
9487c478bd9Sstevel@tonic-gate 	 */
9497c478bd9Sstevel@tonic-gate 	if (flags & HAT_LOAD_NOCONSIST)
950ae115bc7Smrj 		PTE_SET(pte, PT_NOCONSIST | PT_REF | PT_MOD);
951ae115bc7Smrj 	else if (attr & HAT_NOSYNC)
952ae115bc7Smrj 		PTE_SET(pte, PT_NOSYNC | PT_REF | PT_MOD);
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 	/*
9557c478bd9Sstevel@tonic-gate 	 * Set the caching attributes in the PTE. The combination
9567c478bd9Sstevel@tonic-gate 	 * of attributes are poorly defined, so we pay attention
9577c478bd9Sstevel@tonic-gate 	 * to them in the given order.
9587c478bd9Sstevel@tonic-gate 	 *
9597c478bd9Sstevel@tonic-gate 	 * The test for HAT_STRICTORDER is different because it's defined
9607c478bd9Sstevel@tonic-gate 	 * as "0" - which was a stupid thing to do, but is too late to change!
9617c478bd9Sstevel@tonic-gate 	 */
9627c478bd9Sstevel@tonic-gate 	if (cache_attr == HAT_STRICTORDER) {
9637c478bd9Sstevel@tonic-gate 		PTE_SET(pte, PT_NOCACHE);
9647c478bd9Sstevel@tonic-gate 	/*LINTED [Lint hates empty ifs, but it's the obvious way to do this] */
9657c478bd9Sstevel@tonic-gate 	} else if (cache_attr & (HAT_UNORDERED_OK | HAT_STORECACHING_OK)) {
9667c478bd9Sstevel@tonic-gate 		/* nothing to set */;
9677c478bd9Sstevel@tonic-gate 	} else if (cache_attr & (HAT_MERGING_OK | HAT_LOADCACHING_OK)) {
9687c478bd9Sstevel@tonic-gate 		PTE_SET(pte, PT_NOCACHE);
9697c478bd9Sstevel@tonic-gate 		if (x86_feature & X86_PAT)
9707c478bd9Sstevel@tonic-gate 			PTE_SET(pte, (level == 0) ? PT_PAT_4K : PT_PAT_LARGE);
9717c478bd9Sstevel@tonic-gate 		else
9727c478bd9Sstevel@tonic-gate 			PTE_SET(pte, PT_WRITETHRU);
9737c478bd9Sstevel@tonic-gate 	} else {
9747c478bd9Sstevel@tonic-gate 		panic("hati_mkpte(): bad caching attributes: %x\n", cache_attr);
9757c478bd9Sstevel@tonic-gate 	}
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 	return (pte);
9787c478bd9Sstevel@tonic-gate }
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate /*
9817c478bd9Sstevel@tonic-gate  * Duplicate address translations of the parent to the child.
9827c478bd9Sstevel@tonic-gate  * This function really isn't used anymore.
9837c478bd9Sstevel@tonic-gate  */
9847c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9857c478bd9Sstevel@tonic-gate int
9867c478bd9Sstevel@tonic-gate hat_dup(hat_t *old, hat_t *new, caddr_t addr, size_t len, uint_t flag)
9877c478bd9Sstevel@tonic-gate {
9887c478bd9Sstevel@tonic-gate 	ASSERT((uintptr_t)addr < kernelbase);
9897c478bd9Sstevel@tonic-gate 	ASSERT(new != kas.a_hat);
9907c478bd9Sstevel@tonic-gate 	ASSERT(old != kas.a_hat);
9917c478bd9Sstevel@tonic-gate 	return (0);
9927c478bd9Sstevel@tonic-gate }
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate /*
9957c478bd9Sstevel@tonic-gate  * Allocate any hat resources required for a process being swapped in.
9967c478bd9Sstevel@tonic-gate  */
9977c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9987c478bd9Sstevel@tonic-gate void
9997c478bd9Sstevel@tonic-gate hat_swapin(hat_t *hat)
10007c478bd9Sstevel@tonic-gate {
10017c478bd9Sstevel@tonic-gate 	/* do nothing - we let everything fault back in */
10027c478bd9Sstevel@tonic-gate }
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate /*
10057c478bd9Sstevel@tonic-gate  * Unload all translations associated with an address space of a process
10067c478bd9Sstevel@tonic-gate  * that is being swapped out.
10077c478bd9Sstevel@tonic-gate  */
10087c478bd9Sstevel@tonic-gate void
10097c478bd9Sstevel@tonic-gate hat_swapout(hat_t *hat)
10107c478bd9Sstevel@tonic-gate {
10117c478bd9Sstevel@tonic-gate 	uintptr_t	vaddr = (uintptr_t)0;
10127c478bd9Sstevel@tonic-gate 	uintptr_t	eaddr = _userlimit;
10137c478bd9Sstevel@tonic-gate 	htable_t	*ht = NULL;
10147c478bd9Sstevel@tonic-gate 	level_t		l;
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	/*
10177c478bd9Sstevel@tonic-gate 	 * We can't just call hat_unload(hat, 0, _userlimit...)  here, because
10187c478bd9Sstevel@tonic-gate 	 * seg_spt and shared pagetables can't be swapped out.
10197c478bd9Sstevel@tonic-gate 	 * Take a look at segspt_shmswapout() - it's a big no-op.
10207c478bd9Sstevel@tonic-gate 	 *
10217c478bd9Sstevel@tonic-gate 	 * Instead we'll walk through all the address space and unload
10227c478bd9Sstevel@tonic-gate 	 * any mappings which we are sure are not shared, not locked.
10237c478bd9Sstevel@tonic-gate 	 */
10247c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(vaddr));
10257c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(eaddr));
10267c478bd9Sstevel@tonic-gate 	ASSERT(AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock));
10277c478bd9Sstevel@tonic-gate 	if ((uintptr_t)hat->hat_as->a_userlimit < eaddr)
10287c478bd9Sstevel@tonic-gate 		eaddr = (uintptr_t)hat->hat_as->a_userlimit;
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	while (vaddr < eaddr) {
10317c478bd9Sstevel@tonic-gate 		(void) htable_walk(hat, &ht, &vaddr, eaddr);
10327c478bd9Sstevel@tonic-gate 		if (ht == NULL)
10337c478bd9Sstevel@tonic-gate 			break;
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 		ASSERT(!IN_VA_HOLE(vaddr));
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 		/*
10387c478bd9Sstevel@tonic-gate 		 * If the page table is shared skip its entire range.
10397c478bd9Sstevel@tonic-gate 		 * This code knows that only level 0 page tables are shared
10407c478bd9Sstevel@tonic-gate 		 */
10417c478bd9Sstevel@tonic-gate 		l = ht->ht_level;
10427c478bd9Sstevel@tonic-gate 		if (ht->ht_flags & HTABLE_SHARED_PFN) {
10437c478bd9Sstevel@tonic-gate 			ASSERT(l == 0);
10447c478bd9Sstevel@tonic-gate 			vaddr = ht->ht_vaddr + LEVEL_SIZE(1);
10457c478bd9Sstevel@tonic-gate 			htable_release(ht);
10467c478bd9Sstevel@tonic-gate 			ht = NULL;
10477c478bd9Sstevel@tonic-gate 			continue;
10487c478bd9Sstevel@tonic-gate 		}
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 		/*
10517c478bd9Sstevel@tonic-gate 		 * If the page table has no locked entries, unload this one.
10527c478bd9Sstevel@tonic-gate 		 */
10537c478bd9Sstevel@tonic-gate 		if (ht->ht_lock_cnt == 0)
10547c478bd9Sstevel@tonic-gate 			hat_unload(hat, (caddr_t)vaddr, LEVEL_SIZE(l),
10557c478bd9Sstevel@tonic-gate 			    HAT_UNLOAD_UNMAP);
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 		/*
10587c478bd9Sstevel@tonic-gate 		 * If we have a level 0 page table with locked entries,
10597c478bd9Sstevel@tonic-gate 		 * skip the entire page table, otherwise skip just one entry.
10607c478bd9Sstevel@tonic-gate 		 */
10617c478bd9Sstevel@tonic-gate 		if (ht->ht_lock_cnt > 0 && l == 0)
10627c478bd9Sstevel@tonic-gate 			vaddr = ht->ht_vaddr + LEVEL_SIZE(1);
10637c478bd9Sstevel@tonic-gate 		else
10647c478bd9Sstevel@tonic-gate 			vaddr += LEVEL_SIZE(l);
10657c478bd9Sstevel@tonic-gate 	}
10667c478bd9Sstevel@tonic-gate 	if (ht)
10677c478bd9Sstevel@tonic-gate 		htable_release(ht);
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 	/*
10707c478bd9Sstevel@tonic-gate 	 * We're in swapout because the system is low on memory, so
10717c478bd9Sstevel@tonic-gate 	 * go back and flush all the htables off the cached list.
10727c478bd9Sstevel@tonic-gate 	 */
10737c478bd9Sstevel@tonic-gate 	htable_purge_hat(hat);
10747c478bd9Sstevel@tonic-gate }
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate /*
10777c478bd9Sstevel@tonic-gate  * returns number of bytes that have valid mappings in hat.
10787c478bd9Sstevel@tonic-gate  */
10797c478bd9Sstevel@tonic-gate size_t
10807c478bd9Sstevel@tonic-gate hat_get_mapped_size(hat_t *hat)
10817c478bd9Sstevel@tonic-gate {
10827c478bd9Sstevel@tonic-gate 	size_t total = 0;
10837c478bd9Sstevel@tonic-gate 	int l;
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	for (l = 0; l <= mmu.max_page_level; l++)
10867c478bd9Sstevel@tonic-gate 		total += (hat->hat_pages_mapped[l] << LEVEL_SHIFT(l));
1087*250b7ff9Sjosephb 	total += hat->hat_ism_pgcnt;
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 	return (total);
10907c478bd9Sstevel@tonic-gate }
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate /*
10937c478bd9Sstevel@tonic-gate  * enable/disable collection of stats for hat.
10947c478bd9Sstevel@tonic-gate  */
10957c478bd9Sstevel@tonic-gate int
10967c478bd9Sstevel@tonic-gate hat_stats_enable(hat_t *hat)
10977c478bd9Sstevel@tonic-gate {
10987c478bd9Sstevel@tonic-gate 	atomic_add_32(&hat->hat_stats, 1);
10997c478bd9Sstevel@tonic-gate 	return (1);
11007c478bd9Sstevel@tonic-gate }
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate void
11037c478bd9Sstevel@tonic-gate hat_stats_disable(hat_t *hat)
11047c478bd9Sstevel@tonic-gate {
11057c478bd9Sstevel@tonic-gate 	atomic_add_32(&hat->hat_stats, -1);
11067c478bd9Sstevel@tonic-gate }
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate /*
11097c478bd9Sstevel@tonic-gate  * Utility to sync the ref/mod bits from a page table entry to the page_t
11107c478bd9Sstevel@tonic-gate  * We must be holding the mapping list lock when this is called.
11117c478bd9Sstevel@tonic-gate  */
11127c478bd9Sstevel@tonic-gate static void
11137c478bd9Sstevel@tonic-gate hati_sync_pte_to_page(page_t *pp, x86pte_t pte, level_t level)
11147c478bd9Sstevel@tonic-gate {
11157c478bd9Sstevel@tonic-gate 	uint_t	rm = 0;
11167c478bd9Sstevel@tonic-gate 	pgcnt_t	pgcnt;
11177c478bd9Sstevel@tonic-gate 
1118ae115bc7Smrj 	if (PTE_GET(pte, PT_SOFTWARE) >= PT_NOSYNC)
11197c478bd9Sstevel@tonic-gate 		return;
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	if (PTE_GET(pte, PT_REF))
11227c478bd9Sstevel@tonic-gate 		rm |= P_REF;
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	if (PTE_GET(pte, PT_MOD))
11257c478bd9Sstevel@tonic-gate 		rm |= P_MOD;
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	if (rm == 0)
11287c478bd9Sstevel@tonic-gate 		return;
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	/*
11317c478bd9Sstevel@tonic-gate 	 * sync to all constituent pages of a large page
11327c478bd9Sstevel@tonic-gate 	 */
11337c478bd9Sstevel@tonic-gate 	ASSERT(x86_hm_held(pp));
11347c478bd9Sstevel@tonic-gate 	pgcnt = page_get_pagecnt(level);
11357c478bd9Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(pp->p_pagenum, pgcnt));
11367c478bd9Sstevel@tonic-gate 	for (; pgcnt > 0; --pgcnt) {
11377c478bd9Sstevel@tonic-gate 		/*
11387c478bd9Sstevel@tonic-gate 		 * hat_page_demote() can't decrease
11397c478bd9Sstevel@tonic-gate 		 * pszc below this mapping size
11407c478bd9Sstevel@tonic-gate 		 * since this large mapping existed after we
11417c478bd9Sstevel@tonic-gate 		 * took mlist lock.
11427c478bd9Sstevel@tonic-gate 		 */
11437c478bd9Sstevel@tonic-gate 		ASSERT(pp->p_szc >= level);
11447c478bd9Sstevel@tonic-gate 		hat_page_setattr(pp, rm);
11457c478bd9Sstevel@tonic-gate 		++pp;
11467c478bd9Sstevel@tonic-gate 	}
11477c478bd9Sstevel@tonic-gate }
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate /*
11507c478bd9Sstevel@tonic-gate  * This the set of PTE bits for PFN, permissions and caching
1151ae115bc7Smrj  * that require a TLB flush (hat_tlb_inval) if changed on a HAT_LOAD_REMAP
11527c478bd9Sstevel@tonic-gate  */
11537c478bd9Sstevel@tonic-gate #define	PT_REMAP_BITS							\
11547c478bd9Sstevel@tonic-gate 	(PT_PADDR | PT_NX | PT_WRITABLE | PT_WRITETHRU |		\
11557c478bd9Sstevel@tonic-gate 	PT_NOCACHE | PT_PAT_4K | PT_PAT_LARGE)
11567c478bd9Sstevel@tonic-gate 
1157b193e412Skchow #define	REMAPASSERT(EX)	if (!(EX)) panic("hati_pte_map: " #EX)
11587c478bd9Sstevel@tonic-gate /*
11597c478bd9Sstevel@tonic-gate  * Do the low-level work to get a mapping entered into a HAT's pagetables
11607c478bd9Sstevel@tonic-gate  * and in the mapping list of the associated page_t.
11617c478bd9Sstevel@tonic-gate  */
1162ae115bc7Smrj static int
11637c478bd9Sstevel@tonic-gate hati_pte_map(
11647c478bd9Sstevel@tonic-gate 	htable_t	*ht,
11657c478bd9Sstevel@tonic-gate 	uint_t		entry,
11667c478bd9Sstevel@tonic-gate 	page_t		*pp,
11677c478bd9Sstevel@tonic-gate 	x86pte_t	pte,
11687c478bd9Sstevel@tonic-gate 	int		flags,
11697c478bd9Sstevel@tonic-gate 	void		*pte_ptr)
11707c478bd9Sstevel@tonic-gate {
11717c478bd9Sstevel@tonic-gate 	hat_t		*hat = ht->ht_hat;
11727c478bd9Sstevel@tonic-gate 	x86pte_t	old_pte;
11737c478bd9Sstevel@tonic-gate 	level_t		l = ht->ht_level;
11747c478bd9Sstevel@tonic-gate 	hment_t		*hm;
11757c478bd9Sstevel@tonic-gate 	uint_t		is_consist;
1176ae115bc7Smrj 	int		rv = 0;
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 	/*
11797c478bd9Sstevel@tonic-gate 	 * Is this a consistant (ie. need mapping list lock) mapping?
11807c478bd9Sstevel@tonic-gate 	 */
11817c478bd9Sstevel@tonic-gate 	is_consist = (pp != NULL && (flags & HAT_LOAD_NOCONSIST) == 0);
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 	/*
11847c478bd9Sstevel@tonic-gate 	 * Track locked mapping count in the htable.  Do this first,
11857c478bd9Sstevel@tonic-gate 	 * as we track locking even if there already is a mapping present.
11867c478bd9Sstevel@tonic-gate 	 */
11877c478bd9Sstevel@tonic-gate 	if ((flags & HAT_LOAD_LOCK) != 0 && hat != kas.a_hat)
11887c478bd9Sstevel@tonic-gate 		HTABLE_LOCK_INC(ht);
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 	/*
11917c478bd9Sstevel@tonic-gate 	 * Acquire the page's mapping list lock and get an hment to use.
11927c478bd9Sstevel@tonic-gate 	 * Note that hment_prepare() might return NULL.
11937c478bd9Sstevel@tonic-gate 	 */
11947c478bd9Sstevel@tonic-gate 	if (is_consist) {
11957c478bd9Sstevel@tonic-gate 		x86_hm_enter(pp);
11967c478bd9Sstevel@tonic-gate 		hm = hment_prepare(ht, entry, pp);
11977c478bd9Sstevel@tonic-gate 	}
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 	/*
12007c478bd9Sstevel@tonic-gate 	 * Set the new pte, retrieving the old one at the same time.
12017c478bd9Sstevel@tonic-gate 	 */
12027c478bd9Sstevel@tonic-gate 	old_pte = x86pte_set(ht, entry, pte, pte_ptr);
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 	/*
1205ae115bc7Smrj 	 * did we get a large page / page table collision?
12067c478bd9Sstevel@tonic-gate 	 */
1207ae115bc7Smrj 	if (old_pte == LPAGE_ERROR) {
1208ae115bc7Smrj 		rv = -1;
1209ae115bc7Smrj 		goto done;
12107c478bd9Sstevel@tonic-gate 	}
12117c478bd9Sstevel@tonic-gate 
1212ae115bc7Smrj 	/*
1213ae115bc7Smrj 	 * If the mapping didn't change there is nothing more to do.
1214ae115bc7Smrj 	 */
1215ae115bc7Smrj 	if (PTE_EQUIV(pte, old_pte))
1216ae115bc7Smrj 		goto done;
1217ae115bc7Smrj 
12187c478bd9Sstevel@tonic-gate 	/*
12197c478bd9Sstevel@tonic-gate 	 * Install a new mapping in the page's mapping list
12207c478bd9Sstevel@tonic-gate 	 */
12217c478bd9Sstevel@tonic-gate 	if (!PTE_ISVALID(old_pte)) {
12227c478bd9Sstevel@tonic-gate 		if (is_consist) {
12237c478bd9Sstevel@tonic-gate 			hment_assign(ht, entry, pp, hm);
12247c478bd9Sstevel@tonic-gate 			x86_hm_exit(pp);
12257c478bd9Sstevel@tonic-gate 		} else {
12267c478bd9Sstevel@tonic-gate 			ASSERT(flags & HAT_LOAD_NOCONSIST);
12277c478bd9Sstevel@tonic-gate 		}
12287c478bd9Sstevel@tonic-gate 		HTABLE_INC(ht->ht_valid_cnt);
12297c478bd9Sstevel@tonic-gate 		PGCNT_INC(hat, l);
1230ae115bc7Smrj 		return (rv);
12317c478bd9Sstevel@tonic-gate 	}
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 	/*
12347c478bd9Sstevel@tonic-gate 	 * Remap's are more complicated:
12357c478bd9Sstevel@tonic-gate 	 *  - HAT_LOAD_REMAP must be specified if changing the pfn.
12367c478bd9Sstevel@tonic-gate 	 *    We also require that NOCONSIST be specified.
12377c478bd9Sstevel@tonic-gate 	 *  - Otherwise only permission or caching bits may change.
12387c478bd9Sstevel@tonic-gate 	 */
12397c478bd9Sstevel@tonic-gate 	if (!PTE_ISPAGE(old_pte, l))
12407c478bd9Sstevel@tonic-gate 		panic("non-null/page mapping pte=" FMT_PTE, old_pte);
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate 	if (PTE2PFN(old_pte, l) != PTE2PFN(pte, l)) {
1243b193e412Skchow 		REMAPASSERT(flags & HAT_LOAD_REMAP);
1244b193e412Skchow 		REMAPASSERT(flags & HAT_LOAD_NOCONSIST);
1245ae115bc7Smrj 		REMAPASSERT(PTE_GET(old_pte, PT_SOFTWARE) >= PT_NOCONSIST);
1246b193e412Skchow 		REMAPASSERT(pf_is_memory(PTE2PFN(old_pte, l)) ==
12477c478bd9Sstevel@tonic-gate 		    pf_is_memory(PTE2PFN(pte, l)));
1248b193e412Skchow 		REMAPASSERT(!is_consist);
12497c478bd9Sstevel@tonic-gate 	}
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate 	/*
12527c478bd9Sstevel@tonic-gate 	 * We only let remaps change the bits for PFNs, permissions
12537c478bd9Sstevel@tonic-gate 	 * or caching type.
12547c478bd9Sstevel@tonic-gate 	 */
12557c478bd9Sstevel@tonic-gate 	ASSERT(PTE_GET(old_pte, ~(PT_REMAP_BITS | PT_REF | PT_MOD)) ==
12567c478bd9Sstevel@tonic-gate 	    PTE_GET(pte, ~PT_REMAP_BITS));
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 	/*
12597c478bd9Sstevel@tonic-gate 	 * We don't create any mapping list entries on a remap, so release
12607c478bd9Sstevel@tonic-gate 	 * any allocated hment after we drop the mapping list lock.
12617c478bd9Sstevel@tonic-gate 	 */
1262ae115bc7Smrj done:
12637c478bd9Sstevel@tonic-gate 	if (is_consist) {
12647c478bd9Sstevel@tonic-gate 		x86_hm_exit(pp);
12657c478bd9Sstevel@tonic-gate 		if (hm != NULL)
12667c478bd9Sstevel@tonic-gate 			hment_free(hm);
12677c478bd9Sstevel@tonic-gate 	}
1268ae115bc7Smrj 	return (rv);
12697c478bd9Sstevel@tonic-gate }
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate /*
1272ae115bc7Smrj  * Internal routine to load a single page table entry. This only fails if
1273ae115bc7Smrj  * we attempt to overwrite a page table link with a large page.
12747c478bd9Sstevel@tonic-gate  */
1275ae115bc7Smrj static int
12767c478bd9Sstevel@tonic-gate hati_load_common(
12777c478bd9Sstevel@tonic-gate 	hat_t		*hat,
12787c478bd9Sstevel@tonic-gate 	uintptr_t	va,
12797c478bd9Sstevel@tonic-gate 	page_t		*pp,
12807c478bd9Sstevel@tonic-gate 	uint_t		attr,
12817c478bd9Sstevel@tonic-gate 	uint_t		flags,
12827c478bd9Sstevel@tonic-gate 	level_t		level,
12837c478bd9Sstevel@tonic-gate 	pfn_t		pfn)
12847c478bd9Sstevel@tonic-gate {
12857c478bd9Sstevel@tonic-gate 	htable_t	*ht;
12867c478bd9Sstevel@tonic-gate 	uint_t		entry;
12877c478bd9Sstevel@tonic-gate 	x86pte_t	pte;
1288ae115bc7Smrj 	int		rv = 0;
12897c478bd9Sstevel@tonic-gate 
1290aac11643Sjosephb 	/*
1291aac11643Sjosephb 	 * The number 16 is arbitrary and here to catch a recursion problem
1292aac11643Sjosephb 	 * early before we blow out the kernel stack.
1293aac11643Sjosephb 	 */
1294aac11643Sjosephb 	++curthread->t_hatdepth;
1295aac11643Sjosephb 	ASSERT(curthread->t_hatdepth < 16);
1296aac11643Sjosephb 
12977c478bd9Sstevel@tonic-gate 	ASSERT(hat == kas.a_hat ||
12987c478bd9Sstevel@tonic-gate 	    AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock));
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 	if (flags & HAT_LOAD_SHARE)
13017c478bd9Sstevel@tonic-gate 		hat->hat_flags |= HAT_SHARED;
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 	/*
13047c478bd9Sstevel@tonic-gate 	 * Find the page table that maps this page if it already exists.
13057c478bd9Sstevel@tonic-gate 	 */
13067c478bd9Sstevel@tonic-gate 	ht = htable_lookup(hat, va, level);
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	/*
1309aac11643Sjosephb 	 * We must have HAT_LOAD_NOCONSIST if page_t is NULL.
13107c478bd9Sstevel@tonic-gate 	 */
1311aac11643Sjosephb 	if (pp == NULL)
13127c478bd9Sstevel@tonic-gate 		flags |= HAT_LOAD_NOCONSIST;
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 	if (ht == NULL) {
13157c478bd9Sstevel@tonic-gate 		ht = htable_create(hat, va, level, NULL);
13167c478bd9Sstevel@tonic-gate 		ASSERT(ht != NULL);
13177c478bd9Sstevel@tonic-gate 	}
13187c478bd9Sstevel@tonic-gate 	entry = htable_va2entry(va, ht);
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	/*
13217c478bd9Sstevel@tonic-gate 	 * a bunch of paranoid error checking
13227c478bd9Sstevel@tonic-gate 	 */
13237c478bd9Sstevel@tonic-gate 	ASSERT(ht->ht_busy > 0);
13247c478bd9Sstevel@tonic-gate 	if (ht->ht_vaddr > va || va > HTABLE_LAST_PAGE(ht))
13257c478bd9Sstevel@tonic-gate 		panic("hati_load_common: bad htable %p, va %p", ht, (void *)va);
13267c478bd9Sstevel@tonic-gate 	ASSERT(ht->ht_level == level);
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate 	/*
13297c478bd9Sstevel@tonic-gate 	 * construct the new PTE
13307c478bd9Sstevel@tonic-gate 	 */
13317c478bd9Sstevel@tonic-gate 	if (hat == kas.a_hat)
13327c478bd9Sstevel@tonic-gate 		attr &= ~PROT_USER;
13337c478bd9Sstevel@tonic-gate 	pte = hati_mkpte(pfn, attr, level, flags);
13347c478bd9Sstevel@tonic-gate 	if (hat == kas.a_hat && va >= kernelbase)
13357c478bd9Sstevel@tonic-gate 		PTE_SET(pte, mmu.pt_global);
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 	/*
13387c478bd9Sstevel@tonic-gate 	 * establish the mapping
13397c478bd9Sstevel@tonic-gate 	 */
1340ae115bc7Smrj 	rv = hati_pte_map(ht, entry, pp, pte, flags, NULL);
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 	/*
13437c478bd9Sstevel@tonic-gate 	 * release the htable and any reserves
13447c478bd9Sstevel@tonic-gate 	 */
13457c478bd9Sstevel@tonic-gate 	htable_release(ht);
1346aac11643Sjosephb 	--curthread->t_hatdepth;
1347ae115bc7Smrj 	return (rv);
13487c478bd9Sstevel@tonic-gate }
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate /*
13517c478bd9Sstevel@tonic-gate  * special case of hat_memload to deal with some kernel addrs for performance
13527c478bd9Sstevel@tonic-gate  */
13537c478bd9Sstevel@tonic-gate static void
13547c478bd9Sstevel@tonic-gate hat_kmap_load(
13557c478bd9Sstevel@tonic-gate 	caddr_t		addr,
13567c478bd9Sstevel@tonic-gate 	page_t		*pp,
13577c478bd9Sstevel@tonic-gate 	uint_t		attr,
13587c478bd9Sstevel@tonic-gate 	uint_t		flags)
13597c478bd9Sstevel@tonic-gate {
13607c478bd9Sstevel@tonic-gate 	uintptr_t	va = (uintptr_t)addr;
13617c478bd9Sstevel@tonic-gate 	x86pte_t	pte;
13627c478bd9Sstevel@tonic-gate 	pfn_t		pfn = page_pptonum(pp);
13637c478bd9Sstevel@tonic-gate 	pgcnt_t		pg_off = mmu_btop(va - mmu.kmap_addr);
13647c478bd9Sstevel@tonic-gate 	htable_t	*ht;
13657c478bd9Sstevel@tonic-gate 	uint_t		entry;
13667c478bd9Sstevel@tonic-gate 	void		*pte_ptr;
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 	/*
13697c478bd9Sstevel@tonic-gate 	 * construct the requested PTE
13707c478bd9Sstevel@tonic-gate 	 */
13717c478bd9Sstevel@tonic-gate 	attr &= ~PROT_USER;
13727c478bd9Sstevel@tonic-gate 	attr |= HAT_STORECACHING_OK;
13737c478bd9Sstevel@tonic-gate 	pte = hati_mkpte(pfn, attr, 0, flags);
13747c478bd9Sstevel@tonic-gate 	PTE_SET(pte, mmu.pt_global);
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 	/*
13777c478bd9Sstevel@tonic-gate 	 * Figure out the pte_ptr and htable and use common code to finish up
13787c478bd9Sstevel@tonic-gate 	 */
13797c478bd9Sstevel@tonic-gate 	if (mmu.pae_hat)
13807c478bd9Sstevel@tonic-gate 		pte_ptr = mmu.kmap_ptes + pg_off;
13817c478bd9Sstevel@tonic-gate 	else
13827c478bd9Sstevel@tonic-gate 		pte_ptr = (x86pte32_t *)mmu.kmap_ptes + pg_off;
13837c478bd9Sstevel@tonic-gate 	ht = mmu.kmap_htables[(va - mmu.kmap_htables[0]->ht_vaddr) >>
13847c478bd9Sstevel@tonic-gate 	    LEVEL_SHIFT(1)];
13857c478bd9Sstevel@tonic-gate 	entry = htable_va2entry(va, ht);
1386aac11643Sjosephb 	++curthread->t_hatdepth;
1387aac11643Sjosephb 	ASSERT(curthread->t_hatdepth < 16);
1388ae115bc7Smrj 	(void) hati_pte_map(ht, entry, pp, pte, flags, pte_ptr);
1389aac11643Sjosephb 	--curthread->t_hatdepth;
13907c478bd9Sstevel@tonic-gate }
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate /*
13937c478bd9Sstevel@tonic-gate  * hat_memload() - load a translation to the given page struct
13947c478bd9Sstevel@tonic-gate  *
13957c478bd9Sstevel@tonic-gate  * Flags for hat_memload/hat_devload/hat_*attr.
13967c478bd9Sstevel@tonic-gate  *
13977c478bd9Sstevel@tonic-gate  * 	HAT_LOAD	Default flags to load a translation to the page.
13987c478bd9Sstevel@tonic-gate  *
13997c478bd9Sstevel@tonic-gate  * 	HAT_LOAD_LOCK	Lock down mapping resources; hat_map(), hat_memload(),
14007c478bd9Sstevel@tonic-gate  *			and hat_devload().
14017c478bd9Sstevel@tonic-gate  *
14027c478bd9Sstevel@tonic-gate  *	HAT_LOAD_NOCONSIST Do not add mapping to page_t mapping list.
1403ae115bc7Smrj  *			sets PT_NOCONSIST
14047c478bd9Sstevel@tonic-gate  *
14057c478bd9Sstevel@tonic-gate  *	HAT_LOAD_SHARE	A flag to hat_memload() to indicate h/w page tables
14067c478bd9Sstevel@tonic-gate  *			that map some user pages (not kas) is shared by more
14077c478bd9Sstevel@tonic-gate  *			than one process (eg. ISM).
14087c478bd9Sstevel@tonic-gate  *
14097c478bd9Sstevel@tonic-gate  *	HAT_LOAD_REMAP	Reload a valid pte with a different page frame.
14107c478bd9Sstevel@tonic-gate  *
14117c478bd9Sstevel@tonic-gate  *	HAT_NO_KALLOC	Do not kmem_alloc while creating the mapping; at this
14127c478bd9Sstevel@tonic-gate  *			point, it's setting up mapping to allocate internal
14137c478bd9Sstevel@tonic-gate  *			hat layer data structures.  This flag forces hat layer
14147c478bd9Sstevel@tonic-gate  *			to tap its reserves in order to prevent infinite
14157c478bd9Sstevel@tonic-gate  *			recursion.
14167c478bd9Sstevel@tonic-gate  *
14177c478bd9Sstevel@tonic-gate  * The following is a protection attribute (like PROT_READ, etc.)
14187c478bd9Sstevel@tonic-gate  *
1419ae115bc7Smrj  *	HAT_NOSYNC	set PT_NOSYNC - this mapping's ref/mod bits
14207c478bd9Sstevel@tonic-gate  *			are never cleared.
14217c478bd9Sstevel@tonic-gate  *
14227c478bd9Sstevel@tonic-gate  * Installing new valid PTE's and creation of the mapping list
14237c478bd9Sstevel@tonic-gate  * entry are controlled under the same lock. It's derived from the
14247c478bd9Sstevel@tonic-gate  * page_t being mapped.
14257c478bd9Sstevel@tonic-gate  */
14267c478bd9Sstevel@tonic-gate static uint_t supported_memload_flags =
14277c478bd9Sstevel@tonic-gate 	HAT_LOAD | HAT_LOAD_LOCK | HAT_LOAD_ADV | HAT_LOAD_NOCONSIST |
14287c478bd9Sstevel@tonic-gate 	HAT_LOAD_SHARE | HAT_NO_KALLOC | HAT_LOAD_REMAP | HAT_LOAD_TEXT;
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate void
14317c478bd9Sstevel@tonic-gate hat_memload(
14327c478bd9Sstevel@tonic-gate 	hat_t		*hat,
14337c478bd9Sstevel@tonic-gate 	caddr_t		addr,
14347c478bd9Sstevel@tonic-gate 	page_t		*pp,
14357c478bd9Sstevel@tonic-gate 	uint_t		attr,
14367c478bd9Sstevel@tonic-gate 	uint_t		flags)
14377c478bd9Sstevel@tonic-gate {
14387c478bd9Sstevel@tonic-gate 	uintptr_t	va = (uintptr_t)addr;
14397c478bd9Sstevel@tonic-gate 	level_t		level = 0;
14407c478bd9Sstevel@tonic-gate 	pfn_t		pfn = page_pptonum(pp);
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(va));
1443ae115bc7Smrj 	ASSERT(hat == kas.a_hat || va < _userlimit);
14447c478bd9Sstevel@tonic-gate 	ASSERT(hat == kas.a_hat ||
14457c478bd9Sstevel@tonic-gate 	    AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock));
14467c478bd9Sstevel@tonic-gate 	ASSERT((flags & supported_memload_flags) == flags);
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 	ASSERT(!IN_VA_HOLE(va));
14497c478bd9Sstevel@tonic-gate 	ASSERT(!PP_ISFREE(pp));
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 	/*
14527c478bd9Sstevel@tonic-gate 	 * kernel address special case for performance.
14537c478bd9Sstevel@tonic-gate 	 */
14547c478bd9Sstevel@tonic-gate 	if (mmu.kmap_addr <= va && va < mmu.kmap_eaddr) {
14557c478bd9Sstevel@tonic-gate 		ASSERT(hat == kas.a_hat);
14567c478bd9Sstevel@tonic-gate 		hat_kmap_load(addr, pp, attr, flags);
14577c478bd9Sstevel@tonic-gate 		return;
14587c478bd9Sstevel@tonic-gate 	}
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 	/*
14617c478bd9Sstevel@tonic-gate 	 * This is used for memory with normal caching enabled, so
14627c478bd9Sstevel@tonic-gate 	 * always set HAT_STORECACHING_OK.
14637c478bd9Sstevel@tonic-gate 	 */
14647c478bd9Sstevel@tonic-gate 	attr |= HAT_STORECACHING_OK;
1465ae115bc7Smrj 	if (hati_load_common(hat, va, pp, attr, flags, level, pfn) != 0)
1466ae115bc7Smrj 		panic("unexpected hati_load_common() failure");
14677c478bd9Sstevel@tonic-gate }
14687c478bd9Sstevel@tonic-gate 
14697c478bd9Sstevel@tonic-gate /*
14707c478bd9Sstevel@tonic-gate  * Load the given array of page structs using large pages when possible
14717c478bd9Sstevel@tonic-gate  */
14727c478bd9Sstevel@tonic-gate void
14737c478bd9Sstevel@tonic-gate hat_memload_array(
14747c478bd9Sstevel@tonic-gate 	hat_t		*hat,
14757c478bd9Sstevel@tonic-gate 	caddr_t		addr,
14767c478bd9Sstevel@tonic-gate 	size_t		len,
14777c478bd9Sstevel@tonic-gate 	page_t		**pages,
14787c478bd9Sstevel@tonic-gate 	uint_t		attr,
14797c478bd9Sstevel@tonic-gate 	uint_t		flags)
14807c478bd9Sstevel@tonic-gate {
14817c478bd9Sstevel@tonic-gate 	uintptr_t	va = (uintptr_t)addr;
14827c478bd9Sstevel@tonic-gate 	uintptr_t	eaddr = va + len;
14837c478bd9Sstevel@tonic-gate 	level_t		level;
14847c478bd9Sstevel@tonic-gate 	size_t		pgsize;
14857c478bd9Sstevel@tonic-gate 	pgcnt_t		pgindx = 0;
14867c478bd9Sstevel@tonic-gate 	pfn_t		pfn;
14877c478bd9Sstevel@tonic-gate 	pgcnt_t		i;
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(va));
1490ae115bc7Smrj 	ASSERT(hat == kas.a_hat || va + len <= _userlimit);
14917c478bd9Sstevel@tonic-gate 	ASSERT(hat == kas.a_hat ||
14927c478bd9Sstevel@tonic-gate 	    AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock));
14937c478bd9Sstevel@tonic-gate 	ASSERT((flags & supported_memload_flags) == flags);
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 	/*
14967c478bd9Sstevel@tonic-gate 	 * memload is used for memory with full caching enabled, so
14977c478bd9Sstevel@tonic-gate 	 * set HAT_STORECACHING_OK.
14987c478bd9Sstevel@tonic-gate 	 */
14997c478bd9Sstevel@tonic-gate 	attr |= HAT_STORECACHING_OK;
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 	/*
15027c478bd9Sstevel@tonic-gate 	 * handle all pages using largest possible pagesize
15037c478bd9Sstevel@tonic-gate 	 */
15047c478bd9Sstevel@tonic-gate 	while (va < eaddr) {
15057c478bd9Sstevel@tonic-gate 		/*
15067c478bd9Sstevel@tonic-gate 		 * decide what level mapping to use (ie. pagesize)
15077c478bd9Sstevel@tonic-gate 		 */
15087c478bd9Sstevel@tonic-gate 		pfn = page_pptonum(pages[pgindx]);
15097c478bd9Sstevel@tonic-gate 		for (level = mmu.max_page_level; ; --level) {
15107c478bd9Sstevel@tonic-gate 			pgsize = LEVEL_SIZE(level);
15117c478bd9Sstevel@tonic-gate 			if (level == 0)
15127c478bd9Sstevel@tonic-gate 				break;
1513ae115bc7Smrj 
15147c478bd9Sstevel@tonic-gate 			if (!IS_P2ALIGNED(va, pgsize) ||
15157c478bd9Sstevel@tonic-gate 			    (eaddr - va) < pgsize ||
1516ae115bc7Smrj 			    !IS_P2ALIGNED(pfn_to_pa(pfn), pgsize))
15177c478bd9Sstevel@tonic-gate 				continue;
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate 			/*
15207c478bd9Sstevel@tonic-gate 			 * To use a large mapping of this size, all the
15217c478bd9Sstevel@tonic-gate 			 * pages we are passed must be sequential subpages
15227c478bd9Sstevel@tonic-gate 			 * of the large page.
15237c478bd9Sstevel@tonic-gate 			 * hat_page_demote() can't change p_szc because
15247c478bd9Sstevel@tonic-gate 			 * all pages are locked.
15257c478bd9Sstevel@tonic-gate 			 */
15267c478bd9Sstevel@tonic-gate 			if (pages[pgindx]->p_szc >= level) {
15277c478bd9Sstevel@tonic-gate 				for (i = 0; i < mmu_btop(pgsize); ++i) {
15287c478bd9Sstevel@tonic-gate 					if (pfn + i !=
15297c478bd9Sstevel@tonic-gate 					    page_pptonum(pages[pgindx + i]))
15307c478bd9Sstevel@tonic-gate 						break;
15317c478bd9Sstevel@tonic-gate 					ASSERT(pages[pgindx + i]->p_szc >=
15327c478bd9Sstevel@tonic-gate 					    level);
15337c478bd9Sstevel@tonic-gate 					ASSERT(pages[pgindx] + i ==
15347c478bd9Sstevel@tonic-gate 					    pages[pgindx + i]);
15357c478bd9Sstevel@tonic-gate 				}
15367c478bd9Sstevel@tonic-gate 				if (i == mmu_btop(pgsize))
15377c478bd9Sstevel@tonic-gate 					break;
15387c478bd9Sstevel@tonic-gate 			}
15397c478bd9Sstevel@tonic-gate 		}
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 		/*
1542ae115bc7Smrj 		 * Load this page mapping. If the load fails, try a smaller
1543ae115bc7Smrj 		 * pagesize.
15447c478bd9Sstevel@tonic-gate 		 */
15457c478bd9Sstevel@tonic-gate 		ASSERT(!IN_VA_HOLE(va));
1546ae115bc7Smrj 		while (hati_load_common(hat, va, pages[pgindx], attr,
1547*250b7ff9Sjosephb 		    flags, level, pfn) != 0) {
1548ae115bc7Smrj 			if (level == 0)
1549ae115bc7Smrj 				panic("unexpected hati_load_common() failure");
1550ae115bc7Smrj 			--level;
1551ae115bc7Smrj 			pgsize = LEVEL_SIZE(level);
1552ae115bc7Smrj 		}
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 		/*
15557c478bd9Sstevel@tonic-gate 		 * move to next page
15567c478bd9Sstevel@tonic-gate 		 */
15577c478bd9Sstevel@tonic-gate 		va += pgsize;
15587c478bd9Sstevel@tonic-gate 		pgindx += mmu_btop(pgsize);
15597c478bd9Sstevel@tonic-gate 	}
15607c478bd9Sstevel@tonic-gate }
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate /*
15637c478bd9Sstevel@tonic-gate  * void hat_devload(hat, addr, len, pf, attr, flags)
15647c478bd9Sstevel@tonic-gate  *	load/lock the given page frame number
15657c478bd9Sstevel@tonic-gate  *
15667c478bd9Sstevel@tonic-gate  * Advisory ordering attributes. Apply only to device mappings.
15677c478bd9Sstevel@tonic-gate  *
15687c478bd9Sstevel@tonic-gate  * HAT_STRICTORDER: the CPU must issue the references in order, as the
15697c478bd9Sstevel@tonic-gate  *	programmer specified.  This is the default.
15707c478bd9Sstevel@tonic-gate  * HAT_UNORDERED_OK: the CPU may reorder the references (this is all kinds
15717c478bd9Sstevel@tonic-gate  *	of reordering; store or load with store or load).
15727c478bd9Sstevel@tonic-gate  * HAT_MERGING_OK: merging and batching: the CPU may merge individual stores
15737c478bd9Sstevel@tonic-gate  *	to consecutive locations (for example, turn two consecutive byte
15747c478bd9Sstevel@tonic-gate  *	stores into one halfword store), and it may batch individual loads
15757c478bd9Sstevel@tonic-gate  *	(for example, turn two consecutive byte loads into one halfword load).
15767c478bd9Sstevel@tonic-gate  *	This also implies re-ordering.
15777c478bd9Sstevel@tonic-gate  * HAT_LOADCACHING_OK: the CPU may cache the data it fetches and reuse it
15787c478bd9Sstevel@tonic-gate  *	until another store occurs.  The default is to fetch new data
15797c478bd9Sstevel@tonic-gate  *	on every load.  This also implies merging.
15807c478bd9Sstevel@tonic-gate  * HAT_STORECACHING_OK: the CPU may keep the data in the cache and push it to
15817c478bd9Sstevel@tonic-gate  *	the device (perhaps with other data) at a later time.  The default is
15827c478bd9Sstevel@tonic-gate  *	to push the data right away.  This also implies load caching.
15837c478bd9Sstevel@tonic-gate  *
15847c478bd9Sstevel@tonic-gate  * Equivalent of hat_memload(), but can be used for device memory where
15857c478bd9Sstevel@tonic-gate  * there are no page_t's and we support additional flags (write merging, etc).
15867c478bd9Sstevel@tonic-gate  * Note that we can have large page mappings with this interface.
15877c478bd9Sstevel@tonic-gate  */
15887c478bd9Sstevel@tonic-gate int supported_devload_flags = HAT_LOAD | HAT_LOAD_LOCK |
15897c478bd9Sstevel@tonic-gate 	HAT_LOAD_NOCONSIST | HAT_STRICTORDER | HAT_UNORDERED_OK |
15907c478bd9Sstevel@tonic-gate 	HAT_MERGING_OK | HAT_LOADCACHING_OK | HAT_STORECACHING_OK;
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate void
15937c478bd9Sstevel@tonic-gate hat_devload(
15947c478bd9Sstevel@tonic-gate 	hat_t		*hat,
15957c478bd9Sstevel@tonic-gate 	caddr_t		addr,
15967c478bd9Sstevel@tonic-gate 	size_t		len,
15977c478bd9Sstevel@tonic-gate 	pfn_t		pfn,
15987c478bd9Sstevel@tonic-gate 	uint_t		attr,
15997c478bd9Sstevel@tonic-gate 	int		flags)
16007c478bd9Sstevel@tonic-gate {
16017c478bd9Sstevel@tonic-gate 	uintptr_t	va = ALIGN2PAGE(addr);
16027c478bd9Sstevel@tonic-gate 	uintptr_t	eva = va + len;
16037c478bd9Sstevel@tonic-gate 	level_t		level;
16047c478bd9Sstevel@tonic-gate 	size_t		pgsize;
16057c478bd9Sstevel@tonic-gate 	page_t		*pp;
16067c478bd9Sstevel@tonic-gate 	int		f;	/* per PTE copy of flags  - maybe modified */
16077c478bd9Sstevel@tonic-gate 	uint_t		a;	/* per PTE copy of attr */
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(va));
1610ae115bc7Smrj 	ASSERT(hat == kas.a_hat || eva <= _userlimit);
16117c478bd9Sstevel@tonic-gate 	ASSERT(hat == kas.a_hat ||
16127c478bd9Sstevel@tonic-gate 	    AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock));
16137c478bd9Sstevel@tonic-gate 	ASSERT((flags & supported_devload_flags) == flags);
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 	/*
16167c478bd9Sstevel@tonic-gate 	 * handle all pages
16177c478bd9Sstevel@tonic-gate 	 */
16187c478bd9Sstevel@tonic-gate 	while (va < eva) {
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 		/*
16217c478bd9Sstevel@tonic-gate 		 * decide what level mapping to use (ie. pagesize)
16227c478bd9Sstevel@tonic-gate 		 */
16237c478bd9Sstevel@tonic-gate 		for (level = mmu.max_page_level; ; --level) {
16247c478bd9Sstevel@tonic-gate 			pgsize = LEVEL_SIZE(level);
16257c478bd9Sstevel@tonic-gate 			if (level == 0)
16267c478bd9Sstevel@tonic-gate 				break;
16277c478bd9Sstevel@tonic-gate 			if (IS_P2ALIGNED(va, pgsize) &&
16287c478bd9Sstevel@tonic-gate 			    (eva - va) >= pgsize &&
16297c478bd9Sstevel@tonic-gate 			    IS_P2ALIGNED(pfn, mmu_btop(pgsize)))
16307c478bd9Sstevel@tonic-gate 				break;
16317c478bd9Sstevel@tonic-gate 		}
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate 		/*
1634ae115bc7Smrj 		 * If this is just memory then allow caching (this happens
16357c478bd9Sstevel@tonic-gate 		 * for the nucleus pages) - though HAT_PLAT_NOCACHE can be used
1636ae115bc7Smrj 		 * to override that. If we don't have a page_t then make sure
16377c478bd9Sstevel@tonic-gate 		 * NOCONSIST is set.
16387c478bd9Sstevel@tonic-gate 		 */
16397c478bd9Sstevel@tonic-gate 		a = attr;
16407c478bd9Sstevel@tonic-gate 		f = flags;
16417c478bd9Sstevel@tonic-gate 		if (pf_is_memory(pfn)) {
16427c478bd9Sstevel@tonic-gate 			if (!(a & HAT_PLAT_NOCACHE))
16437c478bd9Sstevel@tonic-gate 				a |= HAT_STORECACHING_OK;
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 			if (f & HAT_LOAD_NOCONSIST)
16467c478bd9Sstevel@tonic-gate 				pp = NULL;
16477c478bd9Sstevel@tonic-gate 			else
16487c478bd9Sstevel@tonic-gate 				pp = page_numtopp_nolock(pfn);
16497c478bd9Sstevel@tonic-gate 		} else {
16507c478bd9Sstevel@tonic-gate 			pp = NULL;
16517c478bd9Sstevel@tonic-gate 			f |= HAT_LOAD_NOCONSIST;
16527c478bd9Sstevel@tonic-gate 		}
16537c478bd9Sstevel@tonic-gate 
16547c478bd9Sstevel@tonic-gate 		/*
16557c478bd9Sstevel@tonic-gate 		 * load this page mapping
16567c478bd9Sstevel@tonic-gate 		 */
16577c478bd9Sstevel@tonic-gate 		ASSERT(!IN_VA_HOLE(va));
1658ae115bc7Smrj 		while (hati_load_common(hat, va, pp, a, f, level, pfn) != 0) {
1659ae115bc7Smrj 			if (level == 0)
1660ae115bc7Smrj 				panic("unexpected hati_load_common() failure");
1661ae115bc7Smrj 			--level;
1662ae115bc7Smrj 			pgsize = LEVEL_SIZE(level);
1663ae115bc7Smrj 		}
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate 		/*
16667c478bd9Sstevel@tonic-gate 		 * move to next page
16677c478bd9Sstevel@tonic-gate 		 */
16687c478bd9Sstevel@tonic-gate 		va += pgsize;
16697c478bd9Sstevel@tonic-gate 		pfn += mmu_btop(pgsize);
16707c478bd9Sstevel@tonic-gate 	}
16717c478bd9Sstevel@tonic-gate }
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate /*
16747c478bd9Sstevel@tonic-gate  * void hat_unlock(hat, addr, len)
16757c478bd9Sstevel@tonic-gate  *	unlock the mappings to a given range of addresses
16767c478bd9Sstevel@tonic-gate  *
16777c478bd9Sstevel@tonic-gate  * Locks are tracked by ht_lock_cnt in the htable.
16787c478bd9Sstevel@tonic-gate  */
16797c478bd9Sstevel@tonic-gate void
16807c478bd9Sstevel@tonic-gate hat_unlock(hat_t *hat, caddr_t addr, size_t len)
16817c478bd9Sstevel@tonic-gate {
16827c478bd9Sstevel@tonic-gate 	uintptr_t	vaddr = (uintptr_t)addr;
16837c478bd9Sstevel@tonic-gate 	uintptr_t	eaddr = vaddr + len;
16847c478bd9Sstevel@tonic-gate 	htable_t	*ht = NULL;
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 	/*
16877c478bd9Sstevel@tonic-gate 	 * kernel entries are always locked, we don't track lock counts
16887c478bd9Sstevel@tonic-gate 	 */
1689ae115bc7Smrj 	ASSERT(hat == kas.a_hat || eaddr <= _userlimit);
16907c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(vaddr));
16917c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(eaddr));
16927c478bd9Sstevel@tonic-gate 	if (hat == kas.a_hat)
16937c478bd9Sstevel@tonic-gate 		return;
16947c478bd9Sstevel@tonic-gate 	if (eaddr > _userlimit)
16957c478bd9Sstevel@tonic-gate 		panic("hat_unlock() address out of range - above _userlimit");
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 	ASSERT(AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock));
16987c478bd9Sstevel@tonic-gate 	while (vaddr < eaddr) {
16997c478bd9Sstevel@tonic-gate 		(void) htable_walk(hat, &ht, &vaddr, eaddr);
17007c478bd9Sstevel@tonic-gate 		if (ht == NULL)
17017c478bd9Sstevel@tonic-gate 			break;
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate 		ASSERT(!IN_VA_HOLE(vaddr));
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate 		if (ht->ht_lock_cnt < 1)
17067c478bd9Sstevel@tonic-gate 			panic("hat_unlock(): lock_cnt < 1, "
17077c478bd9Sstevel@tonic-gate 			    "htable=%p, vaddr=%p\n", ht, (caddr_t)vaddr);
17087c478bd9Sstevel@tonic-gate 		HTABLE_LOCK_DEC(ht);
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate 		vaddr += LEVEL_SIZE(ht->ht_level);
17117c478bd9Sstevel@tonic-gate 	}
17127c478bd9Sstevel@tonic-gate 	if (ht)
17137c478bd9Sstevel@tonic-gate 		htable_release(ht);
17147c478bd9Sstevel@tonic-gate }
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate /*
17177c478bd9Sstevel@tonic-gate  * Cross call service routine to demap a virtual page on
17187c478bd9Sstevel@tonic-gate  * the current CPU or flush all mappings in TLB.
17197c478bd9Sstevel@tonic-gate  */
17207c478bd9Sstevel@tonic-gate /*ARGSUSED*/
17217c478bd9Sstevel@tonic-gate static int
17227c478bd9Sstevel@tonic-gate hati_demap_func(xc_arg_t a1, xc_arg_t a2, xc_arg_t a3)
17237c478bd9Sstevel@tonic-gate {
17247c478bd9Sstevel@tonic-gate 	hat_t	*hat = (hat_t *)a1;
17257c478bd9Sstevel@tonic-gate 	caddr_t	addr = (caddr_t)a2;
17267c478bd9Sstevel@tonic-gate 
17277c478bd9Sstevel@tonic-gate 	/*
17287c478bd9Sstevel@tonic-gate 	 * If the target hat isn't the kernel and this CPU isn't operating
17297c478bd9Sstevel@tonic-gate 	 * in the target hat, we can ignore the cross call.
17307c478bd9Sstevel@tonic-gate 	 */
17317c478bd9Sstevel@tonic-gate 	if (hat != kas.a_hat && hat != CPU->cpu_current_hat)
17327c478bd9Sstevel@tonic-gate 		return (0);
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate 	/*
17357c478bd9Sstevel@tonic-gate 	 * For a normal address, we just flush one page mapping
17367c478bd9Sstevel@tonic-gate 	 */
17377c478bd9Sstevel@tonic-gate 	if ((uintptr_t)addr != DEMAP_ALL_ADDR) {
1738ae115bc7Smrj 		mmu_tlbflush_entry(addr);
17397c478bd9Sstevel@tonic-gate 		return (0);
17407c478bd9Sstevel@tonic-gate 	}
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate 	/*
17437c478bd9Sstevel@tonic-gate 	 * Otherwise we reload cr3 to effect a complete TLB flush.
17447c478bd9Sstevel@tonic-gate 	 *
17457c478bd9Sstevel@tonic-gate 	 * A reload of cr3 on a VLP process also means we must also recopy in
17467c478bd9Sstevel@tonic-gate 	 * the pte values from the struct hat
17477c478bd9Sstevel@tonic-gate 	 */
17487c478bd9Sstevel@tonic-gate 	if (hat->hat_flags & HAT_VLP) {
17497c478bd9Sstevel@tonic-gate #if defined(__amd64)
17507c478bd9Sstevel@tonic-gate 		x86pte_t *vlpptep = CPU->cpu_hat_info->hci_vlp_l2ptes;
17517c478bd9Sstevel@tonic-gate 
17527c478bd9Sstevel@tonic-gate 		VLP_COPY(hat->hat_vlp_ptes, vlpptep);
17537c478bd9Sstevel@tonic-gate #elif defined(__i386)
17547c478bd9Sstevel@tonic-gate 		reload_pae32(hat, CPU);
17557c478bd9Sstevel@tonic-gate #endif
17567c478bd9Sstevel@tonic-gate 	}
17577c478bd9Sstevel@tonic-gate 	reload_cr3();
17587c478bd9Sstevel@tonic-gate 	return (0);
17597c478bd9Sstevel@tonic-gate }
17607c478bd9Sstevel@tonic-gate 
176195c0a3c8Sjosephb /*
176295c0a3c8Sjosephb  * Flush all TLB entries, including global (ie. kernel) ones.
176395c0a3c8Sjosephb  */
176495c0a3c8Sjosephb static void
176595c0a3c8Sjosephb flush_all_tlb_entries(void)
176695c0a3c8Sjosephb {
176795c0a3c8Sjosephb 	ulong_t cr4 = getcr4();
176895c0a3c8Sjosephb 
176995c0a3c8Sjosephb 	if (cr4 & CR4_PGE) {
177095c0a3c8Sjosephb 		setcr4(cr4 & ~(ulong_t)CR4_PGE);
177195c0a3c8Sjosephb 		setcr4(cr4);
177295c0a3c8Sjosephb 
177395c0a3c8Sjosephb 		/*
177495c0a3c8Sjosephb 		 * 32 bit PAE also needs to always reload_cr3()
177595c0a3c8Sjosephb 		 */
177695c0a3c8Sjosephb 		if (mmu.max_level == 2)
177795c0a3c8Sjosephb 			reload_cr3();
177895c0a3c8Sjosephb 	} else {
177995c0a3c8Sjosephb 		reload_cr3();
178095c0a3c8Sjosephb 	}
178195c0a3c8Sjosephb }
178295c0a3c8Sjosephb 
178395c0a3c8Sjosephb #define	TLB_CPU_HALTED	(01ul)
178495c0a3c8Sjosephb #define	TLB_INVAL_ALL	(02ul)
178595c0a3c8Sjosephb #define	CAS_TLB_INFO(cpu, old, new)	\
178695c0a3c8Sjosephb 	caslong((ulong_t *)&(cpu)->cpu_m.mcpu_tlb_info, (old), (new))
178795c0a3c8Sjosephb 
178895c0a3c8Sjosephb /*
178995c0a3c8Sjosephb  * Record that a CPU is going idle
179095c0a3c8Sjosephb  */
179195c0a3c8Sjosephb void
179295c0a3c8Sjosephb tlb_going_idle(void)
179395c0a3c8Sjosephb {
179495c0a3c8Sjosephb 	atomic_or_long((ulong_t *)&CPU->cpu_m.mcpu_tlb_info, TLB_CPU_HALTED);
179595c0a3c8Sjosephb }
179695c0a3c8Sjosephb 
179795c0a3c8Sjosephb /*
179895c0a3c8Sjosephb  * Service a delayed TLB flush if coming out of being idle.
179995c0a3c8Sjosephb  */
180095c0a3c8Sjosephb void
180195c0a3c8Sjosephb tlb_service(void)
180295c0a3c8Sjosephb {
180395c0a3c8Sjosephb 	ulong_t flags = getflags();
180495c0a3c8Sjosephb 	ulong_t tlb_info;
180595c0a3c8Sjosephb 	ulong_t found;
180695c0a3c8Sjosephb 
180795c0a3c8Sjosephb 	/*
180895c0a3c8Sjosephb 	 * Be sure interrupts are off while doing this so that
180995c0a3c8Sjosephb 	 * higher level interrupts correctly wait for flushes to finish.
181095c0a3c8Sjosephb 	 */
181195c0a3c8Sjosephb 	if (flags & PS_IE)
181295c0a3c8Sjosephb 		flags = intr_clear();
181395c0a3c8Sjosephb 
181495c0a3c8Sjosephb 	/*
181595c0a3c8Sjosephb 	 * We only have to do something if coming out of being idle.
181695c0a3c8Sjosephb 	 */
181795c0a3c8Sjosephb 	tlb_info = CPU->cpu_m.mcpu_tlb_info;
181895c0a3c8Sjosephb 	if (tlb_info & TLB_CPU_HALTED) {
181995c0a3c8Sjosephb 		ASSERT(CPU->cpu_current_hat == kas.a_hat);
182095c0a3c8Sjosephb 
182195c0a3c8Sjosephb 		/*
182295c0a3c8Sjosephb 		 * Atomic clear and fetch of old state.
182395c0a3c8Sjosephb 		 */
182495c0a3c8Sjosephb 		while ((found = CAS_TLB_INFO(CPU, tlb_info, 0)) != tlb_info) {
182595c0a3c8Sjosephb 			ASSERT(found & TLB_CPU_HALTED);
182695c0a3c8Sjosephb 			tlb_info = found;
182795c0a3c8Sjosephb 			SMT_PAUSE();
182895c0a3c8Sjosephb 		}
182995c0a3c8Sjosephb 		if (tlb_info & TLB_INVAL_ALL)
183095c0a3c8Sjosephb 			flush_all_tlb_entries();
183195c0a3c8Sjosephb 	}
183295c0a3c8Sjosephb 
183395c0a3c8Sjosephb 	/*
183495c0a3c8Sjosephb 	 * Restore interrupt enable control bit.
183595c0a3c8Sjosephb 	 */
183695c0a3c8Sjosephb 	if (flags & PS_IE)
183795c0a3c8Sjosephb 		sti();
183895c0a3c8Sjosephb }
183995c0a3c8Sjosephb 
18407c478bd9Sstevel@tonic-gate /*
18417c478bd9Sstevel@tonic-gate  * Internal routine to do cross calls to invalidate a range of pages on
18427c478bd9Sstevel@tonic-gate  * all CPUs using a given hat.
18437c478bd9Sstevel@tonic-gate  */
18447c478bd9Sstevel@tonic-gate void
1845ae115bc7Smrj hat_tlb_inval(hat_t *hat, uintptr_t va)
18467c478bd9Sstevel@tonic-gate {
18477c478bd9Sstevel@tonic-gate 	extern int	flushes_require_xcalls;	/* from mp_startup.c */
18487c478bd9Sstevel@tonic-gate 	cpuset_t	justme;
184995c0a3c8Sjosephb 	cpuset_t	check_cpus;
1850ae115bc7Smrj 	cpuset_t	cpus_to_shootdown;
185195c0a3c8Sjosephb 	cpu_t		*cpup;
185295c0a3c8Sjosephb 	int		c;
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate 	/*
18557c478bd9Sstevel@tonic-gate 	 * If the hat is being destroyed, there are no more users, so
18567c478bd9Sstevel@tonic-gate 	 * demap need not do anything.
18577c478bd9Sstevel@tonic-gate 	 */
18587c478bd9Sstevel@tonic-gate 	if (hat->hat_flags & HAT_FREEING)
18597c478bd9Sstevel@tonic-gate 		return;
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate 	/*
18627c478bd9Sstevel@tonic-gate 	 * If demapping from a shared pagetable, we best demap the
18637c478bd9Sstevel@tonic-gate 	 * entire set of user TLBs, since we don't know what addresses
18647c478bd9Sstevel@tonic-gate 	 * these were shared at.
18657c478bd9Sstevel@tonic-gate 	 */
18667c478bd9Sstevel@tonic-gate 	if (hat->hat_flags & HAT_SHARED) {
18677c478bd9Sstevel@tonic-gate 		hat = kas.a_hat;
18687c478bd9Sstevel@tonic-gate 		va = DEMAP_ALL_ADDR;
18697c478bd9Sstevel@tonic-gate 	}
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate 	/*
18727c478bd9Sstevel@tonic-gate 	 * if not running with multiple CPUs, don't use cross calls
18737c478bd9Sstevel@tonic-gate 	 */
18747c478bd9Sstevel@tonic-gate 	if (panicstr || !flushes_require_xcalls) {
18757c478bd9Sstevel@tonic-gate 		(void) hati_demap_func((xc_arg_t)hat, (xc_arg_t)va, NULL);
18767c478bd9Sstevel@tonic-gate 		return;
18777c478bd9Sstevel@tonic-gate 	}
18787c478bd9Sstevel@tonic-gate 
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate 	/*
1881ae115bc7Smrj 	 * Determine CPUs to shootdown. Kernel changes always do all CPUs.
1882ae115bc7Smrj 	 * Otherwise it's just CPUs currently executing in this hat.
18837c478bd9Sstevel@tonic-gate 	 */
18847c478bd9Sstevel@tonic-gate 	kpreempt_disable();
18857c478bd9Sstevel@tonic-gate 	CPUSET_ONLY(justme, CPU->cpu_id);
1886ae115bc7Smrj 	if (hat == kas.a_hat)
1887ae115bc7Smrj 		cpus_to_shootdown = khat_cpuset;
18887c478bd9Sstevel@tonic-gate 	else
1889ae115bc7Smrj 		cpus_to_shootdown = hat->hat_cpus;
1890ae115bc7Smrj 
189195c0a3c8Sjosephb 	/*
189295c0a3c8Sjosephb 	 * If any CPUs in the set are idle, just request a delayed flush
189395c0a3c8Sjosephb 	 * and avoid waking them up.
189495c0a3c8Sjosephb 	 */
189595c0a3c8Sjosephb 	check_cpus = cpus_to_shootdown;
189695c0a3c8Sjosephb 	for (c = 0; c < NCPU && !CPUSET_ISNULL(check_cpus); ++c) {
189795c0a3c8Sjosephb 		ulong_t tlb_info;
189895c0a3c8Sjosephb 
189995c0a3c8Sjosephb 		if (!CPU_IN_SET(check_cpus, c))
190095c0a3c8Sjosephb 			continue;
190195c0a3c8Sjosephb 		CPUSET_DEL(check_cpus, c);
190295c0a3c8Sjosephb 		cpup = cpu[c];
190395c0a3c8Sjosephb 		if (cpup == NULL)
190495c0a3c8Sjosephb 			continue;
190595c0a3c8Sjosephb 
190695c0a3c8Sjosephb 		tlb_info = cpup->cpu_m.mcpu_tlb_info;
190795c0a3c8Sjosephb 		while (tlb_info == TLB_CPU_HALTED) {
190895c0a3c8Sjosephb 			(void) CAS_TLB_INFO(cpup, TLB_CPU_HALTED,
1909*250b7ff9Sjosephb 			    TLB_CPU_HALTED | TLB_INVAL_ALL);
191095c0a3c8Sjosephb 			SMT_PAUSE();
191195c0a3c8Sjosephb 			tlb_info = cpup->cpu_m.mcpu_tlb_info;
191295c0a3c8Sjosephb 		}
191395c0a3c8Sjosephb 		if (tlb_info == (TLB_CPU_HALTED | TLB_INVAL_ALL)) {
191495c0a3c8Sjosephb 			HATSTAT_INC(hs_tlb_inval_delayed);
191595c0a3c8Sjosephb 			CPUSET_DEL(cpus_to_shootdown, c);
191695c0a3c8Sjosephb 		}
191795c0a3c8Sjosephb 	}
191895c0a3c8Sjosephb 
1919ae115bc7Smrj 	if (CPUSET_ISNULL(cpus_to_shootdown) ||
1920ae115bc7Smrj 	    CPUSET_ISEQUAL(cpus_to_shootdown, justme)) {
1921ae115bc7Smrj 
1922ae115bc7Smrj 		(void) hati_demap_func((xc_arg_t)hat, (xc_arg_t)va, NULL);
1923ae115bc7Smrj 
1924ae115bc7Smrj 	} else {
1925ae115bc7Smrj 
1926ae115bc7Smrj 		CPUSET_ADD(cpus_to_shootdown, CPU->cpu_id);
1927ae115bc7Smrj 		xc_call((xc_arg_t)hat, (xc_arg_t)va, NULL, X_CALL_HIPRI,
1928ae115bc7Smrj 		    cpus_to_shootdown, hati_demap_func);
1929ae115bc7Smrj 
1930ae115bc7Smrj 	}
19317c478bd9Sstevel@tonic-gate 	kpreempt_enable();
19327c478bd9Sstevel@tonic-gate }
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate /*
19357c478bd9Sstevel@tonic-gate  * Interior routine for HAT_UNLOADs from hat_unload_callback(),
19367c478bd9Sstevel@tonic-gate  * hat_kmap_unload() OR from hat_steal() code.  This routine doesn't
19377c478bd9Sstevel@tonic-gate  * handle releasing of the htables.
19387c478bd9Sstevel@tonic-gate  */
19397c478bd9Sstevel@tonic-gate void
19407c478bd9Sstevel@tonic-gate hat_pte_unmap(
19417c478bd9Sstevel@tonic-gate 	htable_t	*ht,
19427c478bd9Sstevel@tonic-gate 	uint_t		entry,
19437c478bd9Sstevel@tonic-gate 	uint_t		flags,
19447c478bd9Sstevel@tonic-gate 	x86pte_t	old_pte,
19457c478bd9Sstevel@tonic-gate 	void		*pte_ptr)
19467c478bd9Sstevel@tonic-gate {
19477c478bd9Sstevel@tonic-gate 	hat_t		*hat = ht->ht_hat;
19487c478bd9Sstevel@tonic-gate 	hment_t		*hm = NULL;
19497c478bd9Sstevel@tonic-gate 	page_t		*pp = NULL;
19507c478bd9Sstevel@tonic-gate 	level_t		l = ht->ht_level;
19517c478bd9Sstevel@tonic-gate 	pfn_t		pfn;
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 	/*
19547c478bd9Sstevel@tonic-gate 	 * We always track the locking counts, even if nothing is unmapped
19557c478bd9Sstevel@tonic-gate 	 */
19567c478bd9Sstevel@tonic-gate 	if ((flags & HAT_UNLOAD_UNLOCK) != 0 && hat != kas.a_hat) {
19577c478bd9Sstevel@tonic-gate 		ASSERT(ht->ht_lock_cnt > 0);
19587c478bd9Sstevel@tonic-gate 		HTABLE_LOCK_DEC(ht);
19597c478bd9Sstevel@tonic-gate 	}
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	/*
19627c478bd9Sstevel@tonic-gate 	 * Figure out which page's mapping list lock to acquire using the PFN
19637c478bd9Sstevel@tonic-gate 	 * passed in "old" PTE. We then attempt to invalidate the PTE.
19647c478bd9Sstevel@tonic-gate 	 * If another thread, probably a hat_pageunload, has asynchronously
19657c478bd9Sstevel@tonic-gate 	 * unmapped/remapped this address we'll loop here.
19667c478bd9Sstevel@tonic-gate 	 */
19677c478bd9Sstevel@tonic-gate 	ASSERT(ht->ht_busy > 0);
19687c478bd9Sstevel@tonic-gate 	while (PTE_ISVALID(old_pte)) {
19697c478bd9Sstevel@tonic-gate 		pfn = PTE2PFN(old_pte, l);
1970ae115bc7Smrj 		if (PTE_GET(old_pte, PT_SOFTWARE) >= PT_NOCONSIST) {
19717c478bd9Sstevel@tonic-gate 			pp = NULL;
19727c478bd9Sstevel@tonic-gate 		} else {
19737c478bd9Sstevel@tonic-gate 			pp = page_numtopp_nolock(pfn);
1974aa2ed9e5Sjosephb 			if (pp == NULL) {
1975aa2ed9e5Sjosephb 				panic("no page_t, not NOCONSIST: old_pte="
1976aa2ed9e5Sjosephb 				    FMT_PTE " ht=%lx entry=0x%x pte_ptr=%lx",
1977aa2ed9e5Sjosephb 				    old_pte, (uintptr_t)ht, entry,
1978aa2ed9e5Sjosephb 				    (uintptr_t)pte_ptr);
1979aa2ed9e5Sjosephb 			}
19807c478bd9Sstevel@tonic-gate 			x86_hm_enter(pp);
19817c478bd9Sstevel@tonic-gate 		}
1982aa2ed9e5Sjosephb 
1983aa2ed9e5Sjosephb 		/*
1984aa2ed9e5Sjosephb 		 * If freeing the address space, check that the PTE
1985aa2ed9e5Sjosephb 		 * hasn't changed, as the mappings are no longer in use by
1986aa2ed9e5Sjosephb 		 * any thread, invalidation is unnecessary.
1987aa2ed9e5Sjosephb 		 * If not freeing, do a full invalidate.
1988aa2ed9e5Sjosephb 		 */
1989aa2ed9e5Sjosephb 		if (hat->hat_flags & HAT_FREEING)
1990aa2ed9e5Sjosephb 			old_pte = x86pte_get(ht, entry);
1991aa2ed9e5Sjosephb 		else
1992ae115bc7Smrj 			old_pte = x86pte_inval(ht, entry, old_pte, pte_ptr);
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate 		/*
19957c478bd9Sstevel@tonic-gate 		 * If the page hadn't changed we've unmapped it and can proceed
19967c478bd9Sstevel@tonic-gate 		 */
19977c478bd9Sstevel@tonic-gate 		if (PTE_ISVALID(old_pte) && PTE2PFN(old_pte, l) == pfn)
19987c478bd9Sstevel@tonic-gate 			break;
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate 		/*
20017c478bd9Sstevel@tonic-gate 		 * Otherwise, we'll have to retry with the current old_pte.
20027c478bd9Sstevel@tonic-gate 		 * Drop the hment lock, since the pfn may have changed.
20037c478bd9Sstevel@tonic-gate 		 */
20047c478bd9Sstevel@tonic-gate 		if (pp != NULL) {
20057c478bd9Sstevel@tonic-gate 			x86_hm_exit(pp);
20067c478bd9Sstevel@tonic-gate 			pp = NULL;
20077c478bd9Sstevel@tonic-gate 		} else {
2008ae115bc7Smrj 			ASSERT(PTE_GET(old_pte, PT_SOFTWARE) >= PT_NOCONSIST);
20097c478bd9Sstevel@tonic-gate 		}
20107c478bd9Sstevel@tonic-gate 	}
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate 	/*
20137c478bd9Sstevel@tonic-gate 	 * If the old mapping wasn't valid, there's nothing more to do
20147c478bd9Sstevel@tonic-gate 	 */
20157c478bd9Sstevel@tonic-gate 	if (!PTE_ISVALID(old_pte)) {
20167c478bd9Sstevel@tonic-gate 		if (pp != NULL)
20177c478bd9Sstevel@tonic-gate 			x86_hm_exit(pp);
20187c478bd9Sstevel@tonic-gate 		return;
20197c478bd9Sstevel@tonic-gate 	}
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate 	/*
20227c478bd9Sstevel@tonic-gate 	 * Take care of syncing any MOD/REF bits and removing the hment.
20237c478bd9Sstevel@tonic-gate 	 */
20247c478bd9Sstevel@tonic-gate 	if (pp != NULL) {
20257c478bd9Sstevel@tonic-gate 		if (!(flags & HAT_UNLOAD_NOSYNC))
20267c478bd9Sstevel@tonic-gate 			hati_sync_pte_to_page(pp, old_pte, l);
20277c478bd9Sstevel@tonic-gate 		hm = hment_remove(pp, ht, entry);
20287c478bd9Sstevel@tonic-gate 		x86_hm_exit(pp);
20297c478bd9Sstevel@tonic-gate 		if (hm != NULL)
20307c478bd9Sstevel@tonic-gate 			hment_free(hm);
20317c478bd9Sstevel@tonic-gate 	}
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 	/*
20347c478bd9Sstevel@tonic-gate 	 * Handle book keeping in the htable and hat
20357c478bd9Sstevel@tonic-gate 	 */
20367c478bd9Sstevel@tonic-gate 	ASSERT(ht->ht_valid_cnt > 0);
20377c478bd9Sstevel@tonic-gate 	HTABLE_DEC(ht->ht_valid_cnt);
20387c478bd9Sstevel@tonic-gate 	PGCNT_DEC(hat, l);
20397c478bd9Sstevel@tonic-gate }
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate /*
20427c478bd9Sstevel@tonic-gate  * very cheap unload implementation to special case some kernel addresses
20437c478bd9Sstevel@tonic-gate  */
20447c478bd9Sstevel@tonic-gate static void
20457c478bd9Sstevel@tonic-gate hat_kmap_unload(caddr_t addr, size_t len, uint_t flags)
20467c478bd9Sstevel@tonic-gate {
20477c478bd9Sstevel@tonic-gate 	uintptr_t	va = (uintptr_t)addr;
20487c478bd9Sstevel@tonic-gate 	uintptr_t	eva = va + len;
2049ae115bc7Smrj 	pgcnt_t		pg_index;
20507c478bd9Sstevel@tonic-gate 	htable_t	*ht;
20517c478bd9Sstevel@tonic-gate 	uint_t		entry;
2052ae115bc7Smrj 	x86pte_t	*pte_ptr;
20537c478bd9Sstevel@tonic-gate 	x86pte_t	old_pte;
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate 	for (; va < eva; va += MMU_PAGESIZE) {
20567c478bd9Sstevel@tonic-gate 		/*
20577c478bd9Sstevel@tonic-gate 		 * Get the PTE
20587c478bd9Sstevel@tonic-gate 		 */
2059ae115bc7Smrj 		pg_index = mmu_btop(va - mmu.kmap_addr);
2060ae115bc7Smrj 		pte_ptr = PT_INDEX_PTR(mmu.kmap_ptes, pg_index);
2061ae115bc7Smrj 		old_pte = GET_PTE(pte_ptr);
20627c478bd9Sstevel@tonic-gate 
20637c478bd9Sstevel@tonic-gate 		/*
20647c478bd9Sstevel@tonic-gate 		 * get the htable / entry
20657c478bd9Sstevel@tonic-gate 		 */
20667c478bd9Sstevel@tonic-gate 		ht = mmu.kmap_htables[(va - mmu.kmap_htables[0]->ht_vaddr)
20677c478bd9Sstevel@tonic-gate 		    >> LEVEL_SHIFT(1)];
20687c478bd9Sstevel@tonic-gate 		entry = htable_va2entry(va, ht);
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate 		/*
20717c478bd9Sstevel@tonic-gate 		 * use mostly common code to unmap it.
20727c478bd9Sstevel@tonic-gate 		 */
20737c478bd9Sstevel@tonic-gate 		hat_pte_unmap(ht, entry, flags, old_pte, pte_ptr);
20747c478bd9Sstevel@tonic-gate 	}
20757c478bd9Sstevel@tonic-gate }
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate 
20787c478bd9Sstevel@tonic-gate /*
20797c478bd9Sstevel@tonic-gate  * unload a range of virtual address space (no callback)
20807c478bd9Sstevel@tonic-gate  */
20817c478bd9Sstevel@tonic-gate void
20827c478bd9Sstevel@tonic-gate hat_unload(hat_t *hat, caddr_t addr, size_t len, uint_t flags)
20837c478bd9Sstevel@tonic-gate {
20847c478bd9Sstevel@tonic-gate 	uintptr_t va = (uintptr_t)addr;
2085ae115bc7Smrj 
2086ae115bc7Smrj 	ASSERT(hat == kas.a_hat || va + len <= _userlimit);
20877c478bd9Sstevel@tonic-gate 
20887c478bd9Sstevel@tonic-gate 	/*
20897c478bd9Sstevel@tonic-gate 	 * special case for performance.
20907c478bd9Sstevel@tonic-gate 	 */
20917c478bd9Sstevel@tonic-gate 	if (mmu.kmap_addr <= va && va < mmu.kmap_eaddr) {
20927c478bd9Sstevel@tonic-gate 		ASSERT(hat == kas.a_hat);
20937c478bd9Sstevel@tonic-gate 		hat_kmap_unload(addr, len, flags);
2094ae115bc7Smrj 	} else {
2095ae115bc7Smrj 		hat_unload_callback(hat, addr, len, flags, NULL);
20967c478bd9Sstevel@tonic-gate 	}
20977c478bd9Sstevel@tonic-gate }
20987c478bd9Sstevel@tonic-gate 
20997c478bd9Sstevel@tonic-gate /*
21007c478bd9Sstevel@tonic-gate  * Do the callbacks for ranges being unloaded.
21017c478bd9Sstevel@tonic-gate  */
21027c478bd9Sstevel@tonic-gate typedef struct range_info {
21037c478bd9Sstevel@tonic-gate 	uintptr_t	rng_va;
21047c478bd9Sstevel@tonic-gate 	ulong_t		rng_cnt;
21057c478bd9Sstevel@tonic-gate 	level_t		rng_level;
21067c478bd9Sstevel@tonic-gate } range_info_t;
21077c478bd9Sstevel@tonic-gate 
21087c478bd9Sstevel@tonic-gate static void
21097c478bd9Sstevel@tonic-gate handle_ranges(hat_callback_t *cb, uint_t cnt, range_info_t *range)
21107c478bd9Sstevel@tonic-gate {
21117c478bd9Sstevel@tonic-gate 	/*
21127c478bd9Sstevel@tonic-gate 	 * do callbacks to upper level VM system
21137c478bd9Sstevel@tonic-gate 	 */
21147c478bd9Sstevel@tonic-gate 	while (cb != NULL && cnt > 0) {
21157c478bd9Sstevel@tonic-gate 		--cnt;
21167c478bd9Sstevel@tonic-gate 		cb->hcb_start_addr = (caddr_t)range[cnt].rng_va;
21177c478bd9Sstevel@tonic-gate 		cb->hcb_end_addr = cb->hcb_start_addr;
21187c478bd9Sstevel@tonic-gate 		cb->hcb_end_addr +=
21197c478bd9Sstevel@tonic-gate 		    range[cnt].rng_cnt << LEVEL_SIZE(range[cnt].rng_level);
21207c478bd9Sstevel@tonic-gate 		cb->hcb_function(cb);
21217c478bd9Sstevel@tonic-gate 	}
21227c478bd9Sstevel@tonic-gate }
21237c478bd9Sstevel@tonic-gate 
21247c478bd9Sstevel@tonic-gate /*
21257c478bd9Sstevel@tonic-gate  * Unload a given range of addresses (has optional callback)
21267c478bd9Sstevel@tonic-gate  *
21277c478bd9Sstevel@tonic-gate  * Flags:
21287c478bd9Sstevel@tonic-gate  * define	HAT_UNLOAD		0x00
21297c478bd9Sstevel@tonic-gate  * define	HAT_UNLOAD_NOSYNC	0x02
21307c478bd9Sstevel@tonic-gate  * define	HAT_UNLOAD_UNLOCK	0x04
21317c478bd9Sstevel@tonic-gate  * define	HAT_UNLOAD_OTHER	0x08 - not used
21327c478bd9Sstevel@tonic-gate  * define	HAT_UNLOAD_UNMAP	0x10 - same as HAT_UNLOAD
21337c478bd9Sstevel@tonic-gate  */
21347c478bd9Sstevel@tonic-gate #define	MAX_UNLOAD_CNT (8)
21357c478bd9Sstevel@tonic-gate void
21367c478bd9Sstevel@tonic-gate hat_unload_callback(
21377c478bd9Sstevel@tonic-gate 	hat_t		*hat,
21387c478bd9Sstevel@tonic-gate 	caddr_t		addr,
21397c478bd9Sstevel@tonic-gate 	size_t		len,
21407c478bd9Sstevel@tonic-gate 	uint_t		flags,
21417c478bd9Sstevel@tonic-gate 	hat_callback_t	*cb)
21427c478bd9Sstevel@tonic-gate {
21437c478bd9Sstevel@tonic-gate 	uintptr_t	vaddr = (uintptr_t)addr;
21447c478bd9Sstevel@tonic-gate 	uintptr_t	eaddr = vaddr + len;
21457c478bd9Sstevel@tonic-gate 	htable_t	*ht = NULL;
21467c478bd9Sstevel@tonic-gate 	uint_t		entry;
2147aa2ed9e5Sjosephb 	uintptr_t	contig_va = (uintptr_t)-1L;
21487c478bd9Sstevel@tonic-gate 	range_info_t	r[MAX_UNLOAD_CNT];
21497c478bd9Sstevel@tonic-gate 	uint_t		r_cnt = 0;
21507c478bd9Sstevel@tonic-gate 	x86pte_t	old_pte;
21517c478bd9Sstevel@tonic-gate 
2152ae115bc7Smrj 	ASSERT(hat == kas.a_hat || eaddr <= _userlimit);
21537c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(vaddr));
21547c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(eaddr));
21557c478bd9Sstevel@tonic-gate 
2156ae115bc7Smrj 	/*
2157ae115bc7Smrj 	 * Special case a single page being unloaded for speed. This happens
2158ae115bc7Smrj 	 * quite frequently, COW faults after a fork() for example.
2159ae115bc7Smrj 	 */
2160ae115bc7Smrj 	if (cb == NULL && len == MMU_PAGESIZE) {
2161ae115bc7Smrj 		ht = htable_getpte(hat, vaddr, &entry, &old_pte, 0);
2162ae115bc7Smrj 		if (ht != NULL) {
2163ae115bc7Smrj 			if (PTE_ISVALID(old_pte))
2164ae115bc7Smrj 				hat_pte_unmap(ht, entry, flags, old_pte, NULL);
2165ae115bc7Smrj 			htable_release(ht);
2166ae115bc7Smrj 		}
2167ae115bc7Smrj 		return;
2168ae115bc7Smrj 	}
2169ae115bc7Smrj 
21707c478bd9Sstevel@tonic-gate 	while (vaddr < eaddr) {
21717c478bd9Sstevel@tonic-gate 		old_pte = htable_walk(hat, &ht, &vaddr, eaddr);
21727c478bd9Sstevel@tonic-gate 		if (ht == NULL)
21737c478bd9Sstevel@tonic-gate 			break;
21747c478bd9Sstevel@tonic-gate 
21757c478bd9Sstevel@tonic-gate 		ASSERT(!IN_VA_HOLE(vaddr));
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate 		if (vaddr < (uintptr_t)addr)
21787c478bd9Sstevel@tonic-gate 			panic("hat_unload_callback(): unmap inside large page");
21797c478bd9Sstevel@tonic-gate 
21807c478bd9Sstevel@tonic-gate 		/*
21817c478bd9Sstevel@tonic-gate 		 * We'll do the call backs for contiguous ranges
21827c478bd9Sstevel@tonic-gate 		 */
2183aa2ed9e5Sjosephb 		if (vaddr != contig_va ||
21847c478bd9Sstevel@tonic-gate 		    (r_cnt > 0 && r[r_cnt - 1].rng_level != ht->ht_level)) {
21857c478bd9Sstevel@tonic-gate 			if (r_cnt == MAX_UNLOAD_CNT) {
21867c478bd9Sstevel@tonic-gate 				handle_ranges(cb, r_cnt, r);
21877c478bd9Sstevel@tonic-gate 				r_cnt = 0;
21887c478bd9Sstevel@tonic-gate 			}
21897c478bd9Sstevel@tonic-gate 			r[r_cnt].rng_va = vaddr;
21907c478bd9Sstevel@tonic-gate 			r[r_cnt].rng_cnt = 0;
21917c478bd9Sstevel@tonic-gate 			r[r_cnt].rng_level = ht->ht_level;
21927c478bd9Sstevel@tonic-gate 			++r_cnt;
21937c478bd9Sstevel@tonic-gate 		}
21947c478bd9Sstevel@tonic-gate 
21957c478bd9Sstevel@tonic-gate 		/*
21967c478bd9Sstevel@tonic-gate 		 * Unload one mapping from the page tables.
21977c478bd9Sstevel@tonic-gate 		 */
21987c478bd9Sstevel@tonic-gate 		entry = htable_va2entry(vaddr, ht);
21997c478bd9Sstevel@tonic-gate 		hat_pte_unmap(ht, entry, flags, old_pte, NULL);
22007c478bd9Sstevel@tonic-gate 		ASSERT(ht->ht_level <= mmu.max_page_level);
22017c478bd9Sstevel@tonic-gate 		vaddr += LEVEL_SIZE(ht->ht_level);
2202aa2ed9e5Sjosephb 		contig_va = vaddr;
22037c478bd9Sstevel@tonic-gate 		++r[r_cnt - 1].rng_cnt;
22047c478bd9Sstevel@tonic-gate 	}
22057c478bd9Sstevel@tonic-gate 	if (ht)
22067c478bd9Sstevel@tonic-gate 		htable_release(ht);
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 	/*
22097c478bd9Sstevel@tonic-gate 	 * handle last range for callbacks
22107c478bd9Sstevel@tonic-gate 	 */
22117c478bd9Sstevel@tonic-gate 	if (r_cnt > 0)
22127c478bd9Sstevel@tonic-gate 		handle_ranges(cb, r_cnt, r);
22137c478bd9Sstevel@tonic-gate }
22147c478bd9Sstevel@tonic-gate 
22157c478bd9Sstevel@tonic-gate /*
22167c478bd9Sstevel@tonic-gate  * synchronize mapping with software data structures
22177c478bd9Sstevel@tonic-gate  *
22187c478bd9Sstevel@tonic-gate  * This interface is currently only used by the working set monitor
22197c478bd9Sstevel@tonic-gate  * driver.
22207c478bd9Sstevel@tonic-gate  */
22217c478bd9Sstevel@tonic-gate /*ARGSUSED*/
22227c478bd9Sstevel@tonic-gate void
22237c478bd9Sstevel@tonic-gate hat_sync(hat_t *hat, caddr_t addr, size_t len, uint_t flags)
22247c478bd9Sstevel@tonic-gate {
22257c478bd9Sstevel@tonic-gate 	uintptr_t	vaddr = (uintptr_t)addr;
22267c478bd9Sstevel@tonic-gate 	uintptr_t	eaddr = vaddr + len;
22277c478bd9Sstevel@tonic-gate 	htable_t	*ht = NULL;
22287c478bd9Sstevel@tonic-gate 	uint_t		entry;
22297c478bd9Sstevel@tonic-gate 	x86pte_t	pte;
22307c478bd9Sstevel@tonic-gate 	x86pte_t	save_pte;
22317c478bd9Sstevel@tonic-gate 	x86pte_t	new;
22327c478bd9Sstevel@tonic-gate 	page_t		*pp;
22337c478bd9Sstevel@tonic-gate 
22347c478bd9Sstevel@tonic-gate 	ASSERT(!IN_VA_HOLE(vaddr));
22357c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(vaddr));
22367c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(eaddr));
2237ae115bc7Smrj 	ASSERT(hat == kas.a_hat || eaddr <= _userlimit);
22387c478bd9Sstevel@tonic-gate 
22397c478bd9Sstevel@tonic-gate 	for (; vaddr < eaddr; vaddr += LEVEL_SIZE(ht->ht_level)) {
22407c478bd9Sstevel@tonic-gate try_again:
22417c478bd9Sstevel@tonic-gate 		pte = htable_walk(hat, &ht, &vaddr, eaddr);
22427c478bd9Sstevel@tonic-gate 		if (ht == NULL)
22437c478bd9Sstevel@tonic-gate 			break;
22447c478bd9Sstevel@tonic-gate 		entry = htable_va2entry(vaddr, ht);
22457c478bd9Sstevel@tonic-gate 
2246ae115bc7Smrj 		if (PTE_GET(pte, PT_SOFTWARE) >= PT_NOSYNC ||
22477c478bd9Sstevel@tonic-gate 		    PTE_GET(pte, PT_REF | PT_MOD) == 0)
22487c478bd9Sstevel@tonic-gate 			continue;
22497c478bd9Sstevel@tonic-gate 
22507c478bd9Sstevel@tonic-gate 		/*
22517c478bd9Sstevel@tonic-gate 		 * We need to acquire the mapping list lock to protect
22527c478bd9Sstevel@tonic-gate 		 * against hat_pageunload(), hat_unload(), etc.
22537c478bd9Sstevel@tonic-gate 		 */
22547c478bd9Sstevel@tonic-gate 		pp = page_numtopp_nolock(PTE2PFN(pte, ht->ht_level));
22557c478bd9Sstevel@tonic-gate 		if (pp == NULL)
22567c478bd9Sstevel@tonic-gate 			break;
22577c478bd9Sstevel@tonic-gate 		x86_hm_enter(pp);
22587c478bd9Sstevel@tonic-gate 		save_pte = pte;
22597c478bd9Sstevel@tonic-gate 		pte = x86pte_get(ht, entry);
22607c478bd9Sstevel@tonic-gate 		if (pte != save_pte) {
22617c478bd9Sstevel@tonic-gate 			x86_hm_exit(pp);
22627c478bd9Sstevel@tonic-gate 			goto try_again;
22637c478bd9Sstevel@tonic-gate 		}
2264ae115bc7Smrj 		if (PTE_GET(pte, PT_SOFTWARE) >= PT_NOSYNC ||
22657c478bd9Sstevel@tonic-gate 		    PTE_GET(pte, PT_REF | PT_MOD) == 0) {
22667c478bd9Sstevel@tonic-gate 			x86_hm_exit(pp);
22677c478bd9Sstevel@tonic-gate 			continue;
22687c478bd9Sstevel@tonic-gate 		}
22697c478bd9Sstevel@tonic-gate 
22707c478bd9Sstevel@tonic-gate 		/*
22717c478bd9Sstevel@tonic-gate 		 * Need to clear ref or mod bits. We may compete with
22727c478bd9Sstevel@tonic-gate 		 * hardware updating the R/M bits and have to try again.
22737c478bd9Sstevel@tonic-gate 		 */
22747c478bd9Sstevel@tonic-gate 		if (flags == HAT_SYNC_ZERORM) {
22757c478bd9Sstevel@tonic-gate 			new = pte;
22767c478bd9Sstevel@tonic-gate 			PTE_CLR(new, PT_REF | PT_MOD);
22777c478bd9Sstevel@tonic-gate 			pte = hati_update_pte(ht, entry, pte, new);
22787c478bd9Sstevel@tonic-gate 			if (pte != 0) {
22797c478bd9Sstevel@tonic-gate 				x86_hm_exit(pp);
22807c478bd9Sstevel@tonic-gate 				goto try_again;
22817c478bd9Sstevel@tonic-gate 			}
22827c478bd9Sstevel@tonic-gate 		} else {
22837c478bd9Sstevel@tonic-gate 			/*
22847c478bd9Sstevel@tonic-gate 			 * sync the PTE to the page_t
22857c478bd9Sstevel@tonic-gate 			 */
22867c478bd9Sstevel@tonic-gate 			hati_sync_pte_to_page(pp, save_pte, ht->ht_level);
22877c478bd9Sstevel@tonic-gate 		}
22887c478bd9Sstevel@tonic-gate 		x86_hm_exit(pp);
22897c478bd9Sstevel@tonic-gate 	}
22907c478bd9Sstevel@tonic-gate 	if (ht)
22917c478bd9Sstevel@tonic-gate 		htable_release(ht);
22927c478bd9Sstevel@tonic-gate }
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate /*
22957c478bd9Sstevel@tonic-gate  * void	hat_map(hat, addr, len, flags)
22967c478bd9Sstevel@tonic-gate  */
22977c478bd9Sstevel@tonic-gate /*ARGSUSED*/
22987c478bd9Sstevel@tonic-gate void
22997c478bd9Sstevel@tonic-gate hat_map(hat_t *hat, caddr_t addr, size_t len, uint_t flags)
23007c478bd9Sstevel@tonic-gate {
23017c478bd9Sstevel@tonic-gate 	/* does nothing */
23027c478bd9Sstevel@tonic-gate }
23037c478bd9Sstevel@tonic-gate 
23047c478bd9Sstevel@tonic-gate /*
23057c478bd9Sstevel@tonic-gate  * uint_t hat_getattr(hat, addr, *attr)
23067c478bd9Sstevel@tonic-gate  *	returns attr for <hat,addr> in *attr.  returns 0 if there was a
23077c478bd9Sstevel@tonic-gate  *	mapping and *attr is valid, nonzero if there was no mapping and
23087c478bd9Sstevel@tonic-gate  *	*attr is not valid.
23097c478bd9Sstevel@tonic-gate  */
23107c478bd9Sstevel@tonic-gate uint_t
23117c478bd9Sstevel@tonic-gate hat_getattr(hat_t *hat, caddr_t addr, uint_t *attr)
23127c478bd9Sstevel@tonic-gate {
23137c478bd9Sstevel@tonic-gate 	uintptr_t	vaddr = ALIGN2PAGE(addr);
23147c478bd9Sstevel@tonic-gate 	htable_t	*ht = NULL;
23157c478bd9Sstevel@tonic-gate 	x86pte_t	pte;
23167c478bd9Sstevel@tonic-gate 
2317ae115bc7Smrj 	ASSERT(hat == kas.a_hat || vaddr <= _userlimit);
23187c478bd9Sstevel@tonic-gate 
23197c478bd9Sstevel@tonic-gate 	if (IN_VA_HOLE(vaddr))
23207c478bd9Sstevel@tonic-gate 		return ((uint_t)-1);
23217c478bd9Sstevel@tonic-gate 
2322ae115bc7Smrj 	ht = htable_getpte(hat, vaddr, NULL, &pte, mmu.max_page_level);
23237c478bd9Sstevel@tonic-gate 	if (ht == NULL)
23247c478bd9Sstevel@tonic-gate 		return ((uint_t)-1);
23257c478bd9Sstevel@tonic-gate 
23267c478bd9Sstevel@tonic-gate 	if (!PTE_ISVALID(pte) || !PTE_ISPAGE(pte, ht->ht_level)) {
23277c478bd9Sstevel@tonic-gate 		htable_release(ht);
23287c478bd9Sstevel@tonic-gate 		return ((uint_t)-1);
23297c478bd9Sstevel@tonic-gate 	}
23307c478bd9Sstevel@tonic-gate 
23317c478bd9Sstevel@tonic-gate 	*attr = PROT_READ;
23327c478bd9Sstevel@tonic-gate 	if (PTE_GET(pte, PT_WRITABLE))
23337c478bd9Sstevel@tonic-gate 		*attr |= PROT_WRITE;
23347c478bd9Sstevel@tonic-gate 	if (PTE_GET(pte, PT_USER))
23357c478bd9Sstevel@tonic-gate 		*attr |= PROT_USER;
23367c478bd9Sstevel@tonic-gate 	if (!PTE_GET(pte, mmu.pt_nx))
23377c478bd9Sstevel@tonic-gate 		*attr |= PROT_EXEC;
2338ae115bc7Smrj 	if (PTE_GET(pte, PT_SOFTWARE) >= PT_NOSYNC)
23397c478bd9Sstevel@tonic-gate 		*attr |= HAT_NOSYNC;
23407c478bd9Sstevel@tonic-gate 	htable_release(ht);
23417c478bd9Sstevel@tonic-gate 	return (0);
23427c478bd9Sstevel@tonic-gate }
23437c478bd9Sstevel@tonic-gate 
23447c478bd9Sstevel@tonic-gate /*
23457c478bd9Sstevel@tonic-gate  * hat_updateattr() applies the given attribute change to an existing mapping
23467c478bd9Sstevel@tonic-gate  */
23477c478bd9Sstevel@tonic-gate #define	HAT_LOAD_ATTR		1
23487c478bd9Sstevel@tonic-gate #define	HAT_SET_ATTR		2
23497c478bd9Sstevel@tonic-gate #define	HAT_CLR_ATTR		3
23507c478bd9Sstevel@tonic-gate 
23517c478bd9Sstevel@tonic-gate static void
23527c478bd9Sstevel@tonic-gate hat_updateattr(hat_t *hat, caddr_t addr, size_t len, uint_t attr, int what)
23537c478bd9Sstevel@tonic-gate {
23547c478bd9Sstevel@tonic-gate 	uintptr_t	vaddr = (uintptr_t)addr;
23557c478bd9Sstevel@tonic-gate 	uintptr_t	eaddr = (uintptr_t)addr + len;
23567c478bd9Sstevel@tonic-gate 	htable_t	*ht = NULL;
23577c478bd9Sstevel@tonic-gate 	uint_t		entry;
23587c478bd9Sstevel@tonic-gate 	x86pte_t	oldpte, newpte;
23597c478bd9Sstevel@tonic-gate 	page_t		*pp;
23607c478bd9Sstevel@tonic-gate 
23617c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(vaddr));
23627c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(eaddr));
23637c478bd9Sstevel@tonic-gate 	ASSERT(hat == kas.a_hat ||
23647c478bd9Sstevel@tonic-gate 	    AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock));
23657c478bd9Sstevel@tonic-gate 	for (; vaddr < eaddr; vaddr += LEVEL_SIZE(ht->ht_level)) {
23667c478bd9Sstevel@tonic-gate try_again:
23677c478bd9Sstevel@tonic-gate 		oldpte = htable_walk(hat, &ht, &vaddr, eaddr);
23687c478bd9Sstevel@tonic-gate 		if (ht == NULL)
23697c478bd9Sstevel@tonic-gate 			break;
2370ae115bc7Smrj 		if (PTE_GET(oldpte, PT_SOFTWARE) >= PT_NOCONSIST)
23717c478bd9Sstevel@tonic-gate 			continue;
23727c478bd9Sstevel@tonic-gate 
23737c478bd9Sstevel@tonic-gate 		pp = page_numtopp_nolock(PTE2PFN(oldpte, ht->ht_level));
23747c478bd9Sstevel@tonic-gate 		if (pp == NULL)
23757c478bd9Sstevel@tonic-gate 			continue;
23767c478bd9Sstevel@tonic-gate 		x86_hm_enter(pp);
23777c478bd9Sstevel@tonic-gate 
23787c478bd9Sstevel@tonic-gate 		newpte = oldpte;
23797c478bd9Sstevel@tonic-gate 		/*
23807c478bd9Sstevel@tonic-gate 		 * We found a page table entry in the desired range,
23817c478bd9Sstevel@tonic-gate 		 * figure out the new attributes.
23827c478bd9Sstevel@tonic-gate 		 */
23837c478bd9Sstevel@tonic-gate 		if (what == HAT_SET_ATTR || what == HAT_LOAD_ATTR) {
23847c478bd9Sstevel@tonic-gate 			if ((attr & PROT_WRITE) &&
23857c478bd9Sstevel@tonic-gate 			    !PTE_GET(oldpte, PT_WRITABLE))
23867c478bd9Sstevel@tonic-gate 				newpte |= PT_WRITABLE;
23877c478bd9Sstevel@tonic-gate 
2388ae115bc7Smrj 			if ((attr & HAT_NOSYNC) &&
2389ae115bc7Smrj 			    PTE_GET(oldpte, PT_SOFTWARE) < PT_NOSYNC)
23907c478bd9Sstevel@tonic-gate 				newpte |= PT_NOSYNC;
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate 			if ((attr & PROT_EXEC) && PTE_GET(oldpte, mmu.pt_nx))
23937c478bd9Sstevel@tonic-gate 				newpte &= ~mmu.pt_nx;
23947c478bd9Sstevel@tonic-gate 		}
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate 		if (what == HAT_LOAD_ATTR) {
23977c478bd9Sstevel@tonic-gate 			if (!(attr & PROT_WRITE) &&
23987c478bd9Sstevel@tonic-gate 			    PTE_GET(oldpte, PT_WRITABLE))
23997c478bd9Sstevel@tonic-gate 				newpte &= ~PT_WRITABLE;
24007c478bd9Sstevel@tonic-gate 
2401ae115bc7Smrj 			if (!(attr & HAT_NOSYNC) &&
2402ae115bc7Smrj 			    PTE_GET(oldpte, PT_SOFTWARE) >= PT_NOSYNC)
2403ae115bc7Smrj 				newpte &= ~PT_SOFTWARE;
24047c478bd9Sstevel@tonic-gate 
24057c478bd9Sstevel@tonic-gate 			if (!(attr & PROT_EXEC) && !PTE_GET(oldpte, mmu.pt_nx))
24067c478bd9Sstevel@tonic-gate 				newpte |= mmu.pt_nx;
24077c478bd9Sstevel@tonic-gate 		}
24087c478bd9Sstevel@tonic-gate 
24097c478bd9Sstevel@tonic-gate 		if (what == HAT_CLR_ATTR) {
24107c478bd9Sstevel@tonic-gate 			if ((attr & PROT_WRITE) && PTE_GET(oldpte, PT_WRITABLE))
24117c478bd9Sstevel@tonic-gate 				newpte &= ~PT_WRITABLE;
24127c478bd9Sstevel@tonic-gate 
2413ae115bc7Smrj 			if ((attr & HAT_NOSYNC) &&
2414ae115bc7Smrj 			    PTE_GET(oldpte, PT_SOFTWARE) >= PT_NOSYNC)
2415ae115bc7Smrj 				newpte &= ~PT_SOFTWARE;
24167c478bd9Sstevel@tonic-gate 
24177c478bd9Sstevel@tonic-gate 			if ((attr & PROT_EXEC) && !PTE_GET(oldpte, mmu.pt_nx))
24187c478bd9Sstevel@tonic-gate 				newpte |= mmu.pt_nx;
24197c478bd9Sstevel@tonic-gate 		}
24207c478bd9Sstevel@tonic-gate 
2421ae115bc7Smrj 		/*
2422ae115bc7Smrj 		 * Ensure NOSYNC/NOCONSIST mappings have REF and MOD set.
2423ae115bc7Smrj 		 * x86pte_set() depends on this.
2424ae115bc7Smrj 		 */
2425ae115bc7Smrj 		if (PTE_GET(newpte, PT_SOFTWARE) >= PT_NOSYNC)
2426ae115bc7Smrj 			newpte |= PT_REF | PT_MOD;
2427ae115bc7Smrj 
24287c478bd9Sstevel@tonic-gate 		/*
24297c478bd9Sstevel@tonic-gate 		 * what about PROT_READ or others? this code only handles:
24307c478bd9Sstevel@tonic-gate 		 * EXEC, WRITE, NOSYNC
24317c478bd9Sstevel@tonic-gate 		 */
24327c478bd9Sstevel@tonic-gate 
24337c478bd9Sstevel@tonic-gate 		/*
24347c478bd9Sstevel@tonic-gate 		 * If new PTE really changed, update the table.
24357c478bd9Sstevel@tonic-gate 		 */
24367c478bd9Sstevel@tonic-gate 		if (newpte != oldpte) {
24377c478bd9Sstevel@tonic-gate 			entry = htable_va2entry(vaddr, ht);
24387c478bd9Sstevel@tonic-gate 			oldpte = hati_update_pte(ht, entry, oldpte, newpte);
24397c478bd9Sstevel@tonic-gate 			if (oldpte != 0) {
24407c478bd9Sstevel@tonic-gate 				x86_hm_exit(pp);
24417c478bd9Sstevel@tonic-gate 				goto try_again;
24427c478bd9Sstevel@tonic-gate 			}
24437c478bd9Sstevel@tonic-gate 		}
24447c478bd9Sstevel@tonic-gate 		x86_hm_exit(pp);
24457c478bd9Sstevel@tonic-gate 	}
24467c478bd9Sstevel@tonic-gate 	if (ht)
24477c478bd9Sstevel@tonic-gate 		htable_release(ht);
24487c478bd9Sstevel@tonic-gate }
24497c478bd9Sstevel@tonic-gate 
24507c478bd9Sstevel@tonic-gate /*
24517c478bd9Sstevel@tonic-gate  * Various wrappers for hat_updateattr()
24527c478bd9Sstevel@tonic-gate  */
24537c478bd9Sstevel@tonic-gate void
24547c478bd9Sstevel@tonic-gate hat_setattr(hat_t *hat, caddr_t addr, size_t len, uint_t attr)
24557c478bd9Sstevel@tonic-gate {
2456ae115bc7Smrj 	ASSERT(hat == kas.a_hat || (uintptr_t)addr + len <= _userlimit);
24577c478bd9Sstevel@tonic-gate 	hat_updateattr(hat, addr, len, attr, HAT_SET_ATTR);
24587c478bd9Sstevel@tonic-gate }
24597c478bd9Sstevel@tonic-gate 
24607c478bd9Sstevel@tonic-gate void
24617c478bd9Sstevel@tonic-gate hat_clrattr(hat_t *hat, caddr_t addr, size_t len, uint_t attr)
24627c478bd9Sstevel@tonic-gate {
2463ae115bc7Smrj 	ASSERT(hat == kas.a_hat || (uintptr_t)addr + len <= _userlimit);
24647c478bd9Sstevel@tonic-gate 	hat_updateattr(hat, addr, len, attr, HAT_CLR_ATTR);
24657c478bd9Sstevel@tonic-gate }
24667c478bd9Sstevel@tonic-gate 
24677c478bd9Sstevel@tonic-gate void
24687c478bd9Sstevel@tonic-gate hat_chgattr(hat_t *hat, caddr_t addr, size_t len, uint_t attr)
24697c478bd9Sstevel@tonic-gate {
2470ae115bc7Smrj 	ASSERT(hat == kas.a_hat || (uintptr_t)addr + len <= _userlimit);
24717c478bd9Sstevel@tonic-gate 	hat_updateattr(hat, addr, len, attr, HAT_LOAD_ATTR);
24727c478bd9Sstevel@tonic-gate }
24737c478bd9Sstevel@tonic-gate 
24747c478bd9Sstevel@tonic-gate void
24757c478bd9Sstevel@tonic-gate hat_chgprot(hat_t *hat, caddr_t addr, size_t len, uint_t vprot)
24767c478bd9Sstevel@tonic-gate {
2477ae115bc7Smrj 	ASSERT(hat == kas.a_hat || (uintptr_t)addr + len <= _userlimit);
24787c478bd9Sstevel@tonic-gate 	hat_updateattr(hat, addr, len, vprot & HAT_PROT_MASK, HAT_LOAD_ATTR);
24797c478bd9Sstevel@tonic-gate }
24807c478bd9Sstevel@tonic-gate 
24817c478bd9Sstevel@tonic-gate /*
24827c478bd9Sstevel@tonic-gate  * size_t hat_getpagesize(hat, addr)
24837c478bd9Sstevel@tonic-gate  *	returns pagesize in bytes for <hat, addr>. returns -1 of there is
24847c478bd9Sstevel@tonic-gate  *	no mapping. This is an advisory call.
24857c478bd9Sstevel@tonic-gate  */
24867c478bd9Sstevel@tonic-gate ssize_t
24877c478bd9Sstevel@tonic-gate hat_getpagesize(hat_t *hat, caddr_t addr)
24887c478bd9Sstevel@tonic-gate {
24897c478bd9Sstevel@tonic-gate 	uintptr_t	vaddr = ALIGN2PAGE(addr);
24907c478bd9Sstevel@tonic-gate 	htable_t	*ht;
24917c478bd9Sstevel@tonic-gate 	size_t		pagesize;
24927c478bd9Sstevel@tonic-gate 
2493ae115bc7Smrj 	ASSERT(hat == kas.a_hat || vaddr <= _userlimit);
24947c478bd9Sstevel@tonic-gate 	if (IN_VA_HOLE(vaddr))
24957c478bd9Sstevel@tonic-gate 		return (-1);
24967c478bd9Sstevel@tonic-gate 	ht = htable_getpage(hat, vaddr, NULL);
24977c478bd9Sstevel@tonic-gate 	if (ht == NULL)
24987c478bd9Sstevel@tonic-gate 		return (-1);
24997c478bd9Sstevel@tonic-gate 	pagesize = LEVEL_SIZE(ht->ht_level);
25007c478bd9Sstevel@tonic-gate 	htable_release(ht);
25017c478bd9Sstevel@tonic-gate 	return (pagesize);
25027c478bd9Sstevel@tonic-gate }
25037c478bd9Sstevel@tonic-gate 
25047c478bd9Sstevel@tonic-gate 
25057c478bd9Sstevel@tonic-gate 
25067c478bd9Sstevel@tonic-gate /*
25077c478bd9Sstevel@tonic-gate  * pfn_t hat_getpfnum(hat, addr)
25087c478bd9Sstevel@tonic-gate  *	returns pfn for <hat, addr> or PFN_INVALID if mapping is invalid.
25097c478bd9Sstevel@tonic-gate  */
25107c478bd9Sstevel@tonic-gate pfn_t
25117c478bd9Sstevel@tonic-gate hat_getpfnum(hat_t *hat, caddr_t addr)
25127c478bd9Sstevel@tonic-gate {
25137c478bd9Sstevel@tonic-gate 	uintptr_t	vaddr = ALIGN2PAGE(addr);
25147c478bd9Sstevel@tonic-gate 	htable_t	*ht;
25157c478bd9Sstevel@tonic-gate 	uint_t		entry;
25167c478bd9Sstevel@tonic-gate 	pfn_t		pfn = PFN_INVALID;
25177c478bd9Sstevel@tonic-gate 
2518ae115bc7Smrj 	ASSERT(hat == kas.a_hat || vaddr <= _userlimit);
25197c478bd9Sstevel@tonic-gate 	if (khat_running == 0)
2520ae115bc7Smrj 		return (PFN_INVALID);
25217c478bd9Sstevel@tonic-gate 
25227c478bd9Sstevel@tonic-gate 	if (IN_VA_HOLE(vaddr))
25237c478bd9Sstevel@tonic-gate 		return (PFN_INVALID);
25247c478bd9Sstevel@tonic-gate 
25257c478bd9Sstevel@tonic-gate 	/*
25267c478bd9Sstevel@tonic-gate 	 * A very common use of hat_getpfnum() is from the DDI for kernel pages.
25277c478bd9Sstevel@tonic-gate 	 * Use the kmap_ptes (which also covers the 32 bit heap) to speed
25287c478bd9Sstevel@tonic-gate 	 * this up.
25297c478bd9Sstevel@tonic-gate 	 */
25307c478bd9Sstevel@tonic-gate 	if (mmu.kmap_addr <= vaddr && vaddr < mmu.kmap_eaddr) {
25317c478bd9Sstevel@tonic-gate 		x86pte_t pte;
2532ae115bc7Smrj 		pgcnt_t pg_index;
25337c478bd9Sstevel@tonic-gate 
2534ae115bc7Smrj 		pg_index = mmu_btop(vaddr - mmu.kmap_addr);
2535ae115bc7Smrj 		pte = GET_PTE(PT_INDEX_PTR(mmu.kmap_ptes, pg_index));
25367c478bd9Sstevel@tonic-gate 		if (!PTE_ISVALID(pte))
25377c478bd9Sstevel@tonic-gate 			return (PFN_INVALID);
25387c478bd9Sstevel@tonic-gate 		/*LINTED [use of constant 0 causes a silly lint warning] */
25397c478bd9Sstevel@tonic-gate 		return (PTE2PFN(pte, 0));
25407c478bd9Sstevel@tonic-gate 	}
25417c478bd9Sstevel@tonic-gate 
25427c478bd9Sstevel@tonic-gate 	ht = htable_getpage(hat, vaddr, &entry);
25437c478bd9Sstevel@tonic-gate 	if (ht == NULL)
25447c478bd9Sstevel@tonic-gate 		return (PFN_INVALID);
25457c478bd9Sstevel@tonic-gate 	ASSERT(vaddr >= ht->ht_vaddr);
25467c478bd9Sstevel@tonic-gate 	ASSERT(vaddr <= HTABLE_LAST_PAGE(ht));
25477c478bd9Sstevel@tonic-gate 	pfn = PTE2PFN(x86pte_get(ht, entry), ht->ht_level);
25487c478bd9Sstevel@tonic-gate 	if (ht->ht_level > 0)
25497c478bd9Sstevel@tonic-gate 		pfn += mmu_btop(vaddr & LEVEL_OFFSET(ht->ht_level));
25507c478bd9Sstevel@tonic-gate 	htable_release(ht);
25517c478bd9Sstevel@tonic-gate 	return (pfn);
25527c478bd9Sstevel@tonic-gate }
25537c478bd9Sstevel@tonic-gate 
25547c478bd9Sstevel@tonic-gate /*
25557c478bd9Sstevel@tonic-gate  * hat_getkpfnum() is an obsolete DDI routine, and its use is discouraged.
25567c478bd9Sstevel@tonic-gate  * Use hat_getpfnum(kas.a_hat, ...) instead.
25577c478bd9Sstevel@tonic-gate  *
25587c478bd9Sstevel@tonic-gate  * We'd like to return PFN_INVALID if the mappings have underlying page_t's
25597c478bd9Sstevel@tonic-gate  * but can't right now due to the fact that some software has grown to use
25607c478bd9Sstevel@tonic-gate  * this interface incorrectly. So for now when the interface is misused,
25617c478bd9Sstevel@tonic-gate  * return a warning to the user that in the future it won't work in the
25627c478bd9Sstevel@tonic-gate  * way they're abusing it, and carry on.
25637c478bd9Sstevel@tonic-gate  *
25647c478bd9Sstevel@tonic-gate  * Note that hat_getkpfnum() is never supported on amd64.
25657c478bd9Sstevel@tonic-gate  */
25667c478bd9Sstevel@tonic-gate #if !defined(__amd64)
25677c478bd9Sstevel@tonic-gate pfn_t
25687c478bd9Sstevel@tonic-gate hat_getkpfnum(caddr_t addr)
25697c478bd9Sstevel@tonic-gate {
25707c478bd9Sstevel@tonic-gate 	pfn_t	pfn;
25717c478bd9Sstevel@tonic-gate 	int badcaller = 0;
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate 	if (khat_running == 0)
25747c478bd9Sstevel@tonic-gate 		panic("hat_getkpfnum(): called too early\n");
25757c478bd9Sstevel@tonic-gate 	if ((uintptr_t)addr < kernelbase)
25767c478bd9Sstevel@tonic-gate 		return (PFN_INVALID);
25777c478bd9Sstevel@tonic-gate 
25787c478bd9Sstevel@tonic-gate 
25797c478bd9Sstevel@tonic-gate 	if (segkpm && IS_KPM_ADDR(addr)) {
25807c478bd9Sstevel@tonic-gate 		badcaller = 1;
25817c478bd9Sstevel@tonic-gate 		pfn = hat_kpm_va2pfn(addr);
25827c478bd9Sstevel@tonic-gate 	} else {
25837c478bd9Sstevel@tonic-gate 		pfn = hat_getpfnum(kas.a_hat, addr);
25847c478bd9Sstevel@tonic-gate 		badcaller = pf_is_memory(pfn);
25857c478bd9Sstevel@tonic-gate 	}
25867c478bd9Sstevel@tonic-gate 
25877c478bd9Sstevel@tonic-gate 	if (badcaller)
25887c478bd9Sstevel@tonic-gate 		hat_getkpfnum_badcall(caller());
25897c478bd9Sstevel@tonic-gate 	return (pfn);
25907c478bd9Sstevel@tonic-gate }
25917c478bd9Sstevel@tonic-gate #endif /* __amd64 */
25927c478bd9Sstevel@tonic-gate 
25937c478bd9Sstevel@tonic-gate /*
25947c478bd9Sstevel@tonic-gate  * int hat_probe(hat, addr)
25957c478bd9Sstevel@tonic-gate  *	return 0 if no valid mapping is present.  Faster version
25967c478bd9Sstevel@tonic-gate  *	of hat_getattr in certain architectures.
25977c478bd9Sstevel@tonic-gate  */
25987c478bd9Sstevel@tonic-gate int
25997c478bd9Sstevel@tonic-gate hat_probe(hat_t *hat, caddr_t addr)
26007c478bd9Sstevel@tonic-gate {
26017c478bd9Sstevel@tonic-gate 	uintptr_t	vaddr = ALIGN2PAGE(addr);
26027c478bd9Sstevel@tonic-gate 	uint_t		entry;
26037c478bd9Sstevel@tonic-gate 	htable_t	*ht;
26047c478bd9Sstevel@tonic-gate 	pgcnt_t		pg_off;
26057c478bd9Sstevel@tonic-gate 
2606ae115bc7Smrj 	ASSERT(hat == kas.a_hat || vaddr <= _userlimit);
26077c478bd9Sstevel@tonic-gate 	ASSERT(hat == kas.a_hat ||
26087c478bd9Sstevel@tonic-gate 	    AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock));
26097c478bd9Sstevel@tonic-gate 	if (IN_VA_HOLE(vaddr))
26107c478bd9Sstevel@tonic-gate 		return (0);
26117c478bd9Sstevel@tonic-gate 
26127c478bd9Sstevel@tonic-gate 	/*
26137c478bd9Sstevel@tonic-gate 	 * Most common use of hat_probe is from segmap. We special case it
26147c478bd9Sstevel@tonic-gate 	 * for performance.
26157c478bd9Sstevel@tonic-gate 	 */
26167c478bd9Sstevel@tonic-gate 	if (mmu.kmap_addr <= vaddr && vaddr < mmu.kmap_eaddr) {
26177c478bd9Sstevel@tonic-gate 		pg_off = mmu_btop(vaddr - mmu.kmap_addr);
26187c478bd9Sstevel@tonic-gate 		if (mmu.pae_hat)
26197c478bd9Sstevel@tonic-gate 			return (PTE_ISVALID(mmu.kmap_ptes[pg_off]));
26207c478bd9Sstevel@tonic-gate 		else
26217c478bd9Sstevel@tonic-gate 			return (PTE_ISVALID(
26227c478bd9Sstevel@tonic-gate 			    ((x86pte32_t *)mmu.kmap_ptes)[pg_off]));
26237c478bd9Sstevel@tonic-gate 	}
26247c478bd9Sstevel@tonic-gate 
26257c478bd9Sstevel@tonic-gate 	ht = htable_getpage(hat, vaddr, &entry);
26267c478bd9Sstevel@tonic-gate 	if (ht == NULL)
26277c478bd9Sstevel@tonic-gate 		return (0);
26287c478bd9Sstevel@tonic-gate 	htable_release(ht);
26297c478bd9Sstevel@tonic-gate 	return (1);
26307c478bd9Sstevel@tonic-gate }
26317c478bd9Sstevel@tonic-gate 
26327c478bd9Sstevel@tonic-gate /*
2633*250b7ff9Sjosephb  * Find out if the segment for hat_share()/hat_unshare() is DISM or locked ISM.
2634*250b7ff9Sjosephb  */
2635*250b7ff9Sjosephb static int
2636*250b7ff9Sjosephb is_it_dism(hat_t *hat, caddr_t va)
2637*250b7ff9Sjosephb {
2638*250b7ff9Sjosephb 	struct seg *seg;
2639*250b7ff9Sjosephb 	struct shm_data *shmd;
2640*250b7ff9Sjosephb 	struct spt_data *sptd;
2641*250b7ff9Sjosephb 
2642*250b7ff9Sjosephb 	seg = as_findseg(hat->hat_as, va, 0);
2643*250b7ff9Sjosephb 	ASSERT(seg != NULL);
2644*250b7ff9Sjosephb 	ASSERT(seg->s_base <= va);
2645*250b7ff9Sjosephb 	shmd = (struct shm_data *)seg->s_data;
2646*250b7ff9Sjosephb 	ASSERT(shmd != NULL);
2647*250b7ff9Sjosephb 	sptd = (struct spt_data *)shmd->shm_sptseg->s_data;
2648*250b7ff9Sjosephb 	ASSERT(sptd != NULL);
2649*250b7ff9Sjosephb 	if (sptd->spt_flags & SHM_PAGEABLE)
2650*250b7ff9Sjosephb 		return (1);
2651*250b7ff9Sjosephb 	return (0);
2652*250b7ff9Sjosephb }
2653*250b7ff9Sjosephb 
2654*250b7ff9Sjosephb /*
2655*250b7ff9Sjosephb  * Simple implementation of ISM. hat_share() is similar to hat_memload_array(),
26567c478bd9Sstevel@tonic-gate  * except that we use the ism_hat's existing mappings to determine the pages
2657*250b7ff9Sjosephb  * and protections to use for this hat. If we find a full properly aligned
2658*250b7ff9Sjosephb  * and sized pagetable, we will attempt to share the pagetable itself.
26597c478bd9Sstevel@tonic-gate  */
26607c478bd9Sstevel@tonic-gate /*ARGSUSED*/
26617c478bd9Sstevel@tonic-gate int
26627c478bd9Sstevel@tonic-gate hat_share(
26637c478bd9Sstevel@tonic-gate 	hat_t		*hat,
26647c478bd9Sstevel@tonic-gate 	caddr_t		addr,
26657c478bd9Sstevel@tonic-gate 	hat_t		*ism_hat,
26667c478bd9Sstevel@tonic-gate 	caddr_t		src_addr,
26677c478bd9Sstevel@tonic-gate 	size_t		len,	/* almost useless value, see below.. */
26687c478bd9Sstevel@tonic-gate 	uint_t		ismszc)
26697c478bd9Sstevel@tonic-gate {
26707c478bd9Sstevel@tonic-gate 	uintptr_t	vaddr_start = (uintptr_t)addr;
26717c478bd9Sstevel@tonic-gate 	uintptr_t	vaddr;
26727c478bd9Sstevel@tonic-gate 	uintptr_t	eaddr = vaddr_start + len;
26737c478bd9Sstevel@tonic-gate 	uintptr_t	ism_addr_start = (uintptr_t)src_addr;
26747c478bd9Sstevel@tonic-gate 	uintptr_t	ism_addr = ism_addr_start;
26757c478bd9Sstevel@tonic-gate 	uintptr_t	e_ism_addr = ism_addr + len;
26767c478bd9Sstevel@tonic-gate 	htable_t	*ism_ht = NULL;
26777c478bd9Sstevel@tonic-gate 	htable_t	*ht;
26787c478bd9Sstevel@tonic-gate 	x86pte_t	pte;
26797c478bd9Sstevel@tonic-gate 	page_t		*pp;
26807c478bd9Sstevel@tonic-gate 	pfn_t		pfn;
26817c478bd9Sstevel@tonic-gate 	level_t		l;
26827c478bd9Sstevel@tonic-gate 	pgcnt_t		pgcnt;
26837c478bd9Sstevel@tonic-gate 	uint_t		prot;
26847c478bd9Sstevel@tonic-gate 	uint_t		valid_cnt;
2685*250b7ff9Sjosephb 	int		is_dism;
2686*250b7ff9Sjosephb 	int		flags;
26877c478bd9Sstevel@tonic-gate 
26887c478bd9Sstevel@tonic-gate 	/*
26897c478bd9Sstevel@tonic-gate 	 * We might be asked to share an empty DISM hat by as_dup()
26907c478bd9Sstevel@tonic-gate 	 */
26917c478bd9Sstevel@tonic-gate 	ASSERT(hat != kas.a_hat);
2692ae115bc7Smrj 	ASSERT(eaddr <= _userlimit);
26937c478bd9Sstevel@tonic-gate 	if (!(ism_hat->hat_flags & HAT_SHARED)) {
26947c478bd9Sstevel@tonic-gate 		ASSERT(hat_get_mapped_size(ism_hat) == 0);
26957c478bd9Sstevel@tonic-gate 		return (0);
26967c478bd9Sstevel@tonic-gate 	}
26977c478bd9Sstevel@tonic-gate 
26987c478bd9Sstevel@tonic-gate 	/*
26997c478bd9Sstevel@tonic-gate 	 * The SPT segment driver often passes us a size larger than there are
27007c478bd9Sstevel@tonic-gate 	 * valid mappings. That's because it rounds the segment size up to a
27017c478bd9Sstevel@tonic-gate 	 * large pagesize, even if the actual memory mapped by ism_hat is less.
27027c478bd9Sstevel@tonic-gate 	 */
27037c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(vaddr_start));
27047c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(ism_addr_start));
27057c478bd9Sstevel@tonic-gate 	ASSERT(ism_hat->hat_flags & HAT_SHARED);
2706*250b7ff9Sjosephb 	is_dism = is_it_dism(hat, addr);
27077c478bd9Sstevel@tonic-gate 	while (ism_addr < e_ism_addr) {
27087c478bd9Sstevel@tonic-gate 		/*
27097c478bd9Sstevel@tonic-gate 		 * use htable_walk to get the next valid ISM mapping
27107c478bd9Sstevel@tonic-gate 		 */
27117c478bd9Sstevel@tonic-gate 		pte = htable_walk(ism_hat, &ism_ht, &ism_addr, e_ism_addr);
27127c478bd9Sstevel@tonic-gate 		if (ism_ht == NULL)
27137c478bd9Sstevel@tonic-gate 			break;
27147c478bd9Sstevel@tonic-gate 
27157c478bd9Sstevel@tonic-gate 		/*
2716*250b7ff9Sjosephb 		 * First check to see if we already share the page table.
27177c478bd9Sstevel@tonic-gate 		 */
2718*250b7ff9Sjosephb 		l = ism_ht->ht_level;
27197c478bd9Sstevel@tonic-gate 		vaddr = vaddr_start + (ism_addr - ism_addr_start);
2720*250b7ff9Sjosephb 		ht = htable_lookup(hat, vaddr, l);
2721*250b7ff9Sjosephb 		if (ht != NULL) {
2722*250b7ff9Sjosephb 			if (ht->ht_flags & HTABLE_SHARED_PFN)
2723*250b7ff9Sjosephb 				goto shared;
2724*250b7ff9Sjosephb 			htable_release(ht);
2725*250b7ff9Sjosephb 			goto not_shared;
27267c478bd9Sstevel@tonic-gate 		}
27277c478bd9Sstevel@tonic-gate 
27287c478bd9Sstevel@tonic-gate 		/*
2729*250b7ff9Sjosephb 		 * Can't ever share top table.
27307c478bd9Sstevel@tonic-gate 		 */
2731*250b7ff9Sjosephb 		if (l == mmu.max_level)
2732*250b7ff9Sjosephb 			goto not_shared;
27337c478bd9Sstevel@tonic-gate 
2734*250b7ff9Sjosephb 		/*
2735*250b7ff9Sjosephb 		 * Avoid level mismatches later due to DISM faults.
2736*250b7ff9Sjosephb 		 */
2737*250b7ff9Sjosephb 		if (is_dism && l > 0)
2738*250b7ff9Sjosephb 			goto not_shared;
27397c478bd9Sstevel@tonic-gate 
2740*250b7ff9Sjosephb 		/*
2741*250b7ff9Sjosephb 		 * addresses and lengths must align
2742*250b7ff9Sjosephb 		 * table must be fully populated
2743*250b7ff9Sjosephb 		 * no lower level page tables
2744*250b7ff9Sjosephb 		 */
2745*250b7ff9Sjosephb 		if (ism_addr != ism_ht->ht_vaddr ||
2746*250b7ff9Sjosephb 		    (vaddr & LEVEL_OFFSET(l + 1)) != 0)
2747*250b7ff9Sjosephb 			goto not_shared;
27487c478bd9Sstevel@tonic-gate 
2749*250b7ff9Sjosephb 		/*
2750*250b7ff9Sjosephb 		 * The range of address space must cover a full table.
2751*250b7ff9Sjosephb 		 */
2752*250b7ff9Sjosephb 		if (e_ism_addr - ism_addr < LEVEL_SIZE(1 + 1))
2753*250b7ff9Sjosephb 			goto not_shared;
27547c478bd9Sstevel@tonic-gate 
2755*250b7ff9Sjosephb 		/*
2756*250b7ff9Sjosephb 		 * All entries in the ISM page table must be leaf PTEs.
2757*250b7ff9Sjosephb 		 */
2758*250b7ff9Sjosephb 		if (l > 0) {
2759*250b7ff9Sjosephb 			int e;
27607c478bd9Sstevel@tonic-gate 
2761*250b7ff9Sjosephb 			/*
2762*250b7ff9Sjosephb 			 * We know the 0th is from htable_walk() above.
2763*250b7ff9Sjosephb 			 */
2764*250b7ff9Sjosephb 			for (e = 1; e < HTABLE_NUM_PTES(ism_ht); ++e) {
2765*250b7ff9Sjosephb 				x86pte_t pte;
2766*250b7ff9Sjosephb 				pte = x86pte_get(ism_ht, e);
2767*250b7ff9Sjosephb 				if (!PTE_ISPAGE(pte, l))
2768*250b7ff9Sjosephb 					goto not_shared;
27697c478bd9Sstevel@tonic-gate 			}
27707c478bd9Sstevel@tonic-gate 		}
27717c478bd9Sstevel@tonic-gate 
2772*250b7ff9Sjosephb 		/*
2773*250b7ff9Sjosephb 		 * share the page table
2774*250b7ff9Sjosephb 		 */
2775*250b7ff9Sjosephb 		ht = htable_create(hat, vaddr, l, ism_ht);
2776*250b7ff9Sjosephb shared:
2777*250b7ff9Sjosephb 		ASSERT(ht->ht_flags & HTABLE_SHARED_PFN);
2778*250b7ff9Sjosephb 		ASSERT(ht->ht_shares == ism_ht);
2779*250b7ff9Sjosephb 		hat->hat_ism_pgcnt +=
2780*250b7ff9Sjosephb 		    (ism_ht->ht_valid_cnt - ht->ht_valid_cnt) <<
2781*250b7ff9Sjosephb 		    (LEVEL_SHIFT(ht->ht_level) - MMU_PAGESHIFT);
2782*250b7ff9Sjosephb 		ht->ht_valid_cnt = ism_ht->ht_valid_cnt;
2783*250b7ff9Sjosephb 		htable_release(ht);
2784*250b7ff9Sjosephb 		ism_addr = ism_ht->ht_vaddr + LEVEL_SIZE(l + 1);
2785*250b7ff9Sjosephb 		htable_release(ism_ht);
2786*250b7ff9Sjosephb 		ism_ht = NULL;
2787*250b7ff9Sjosephb 		continue;
2788*250b7ff9Sjosephb 
2789*250b7ff9Sjosephb not_shared:
27907c478bd9Sstevel@tonic-gate 		/*
27917c478bd9Sstevel@tonic-gate 		 * Unable to share the page table. Instead we will
27927c478bd9Sstevel@tonic-gate 		 * create new mappings from the values in the ISM mappings.
2793*250b7ff9Sjosephb 		 * Figure out what level size mappings to use;
2794*250b7ff9Sjosephb 		 */
2795*250b7ff9Sjosephb 		for (l = ism_ht->ht_level; l > 0; --l) {
2796*250b7ff9Sjosephb 			if (LEVEL_SIZE(l) <= eaddr - vaddr &&
2797*250b7ff9Sjosephb 			    (vaddr & LEVEL_OFFSET(l)) == 0)
2798*250b7ff9Sjosephb 				break;
2799*250b7ff9Sjosephb 		}
2800*250b7ff9Sjosephb 
2801*250b7ff9Sjosephb 		/*
28027c478bd9Sstevel@tonic-gate 		 * The ISM mapping might be larger than the share area,
2803*250b7ff9Sjosephb 		 * be careful to truncate it if needed.
28047c478bd9Sstevel@tonic-gate 		 */
28057c478bd9Sstevel@tonic-gate 		if (eaddr - vaddr >= LEVEL_SIZE(ism_ht->ht_level)) {
28067c478bd9Sstevel@tonic-gate 			pgcnt = mmu_btop(LEVEL_SIZE(ism_ht->ht_level));
28077c478bd9Sstevel@tonic-gate 		} else {
28087c478bd9Sstevel@tonic-gate 			pgcnt = mmu_btop(eaddr - vaddr);
28097c478bd9Sstevel@tonic-gate 			l = 0;
28107c478bd9Sstevel@tonic-gate 		}
28117c478bd9Sstevel@tonic-gate 
28127c478bd9Sstevel@tonic-gate 		pfn = PTE2PFN(pte, ism_ht->ht_level);
28137c478bd9Sstevel@tonic-gate 		ASSERT(pfn != PFN_INVALID);
28147c478bd9Sstevel@tonic-gate 		while (pgcnt > 0) {
28157c478bd9Sstevel@tonic-gate 			/*
28167c478bd9Sstevel@tonic-gate 			 * Make a new pte for the PFN for this level.
28177c478bd9Sstevel@tonic-gate 			 * Copy protections for the pte from the ISM pte.
28187c478bd9Sstevel@tonic-gate 			 */
28197c478bd9Sstevel@tonic-gate 			pp = page_numtopp_nolock(pfn);
28207c478bd9Sstevel@tonic-gate 			ASSERT(pp != NULL);
28217c478bd9Sstevel@tonic-gate 
28227c478bd9Sstevel@tonic-gate 			prot = PROT_USER | PROT_READ | HAT_UNORDERED_OK;
28237c478bd9Sstevel@tonic-gate 			if (PTE_GET(pte, PT_WRITABLE))
28247c478bd9Sstevel@tonic-gate 				prot |= PROT_WRITE;
28257c478bd9Sstevel@tonic-gate 			if (!PTE_GET(pte, PT_NX))
28267c478bd9Sstevel@tonic-gate 				prot |= PROT_EXEC;
28277c478bd9Sstevel@tonic-gate 
2828*250b7ff9Sjosephb 			flags = HAT_LOAD;
2829*250b7ff9Sjosephb 			if (!is_dism)
2830*250b7ff9Sjosephb 				flags |= HAT_LOAD_LOCK | HAT_LOAD_NOCONSIST;
2831*250b7ff9Sjosephb 			while (hati_load_common(hat, vaddr, pp, prot, flags,
2832ae115bc7Smrj 			    l, pfn) != 0) {
2833ae115bc7Smrj 				if (l == 0)
2834ae115bc7Smrj 					panic("hati_load_common() failure");
2835ae115bc7Smrj 				--l;
2836ae115bc7Smrj 			}
28377c478bd9Sstevel@tonic-gate 
28387c478bd9Sstevel@tonic-gate 			vaddr += LEVEL_SIZE(l);
28397c478bd9Sstevel@tonic-gate 			ism_addr += LEVEL_SIZE(l);
28407c478bd9Sstevel@tonic-gate 			pfn += mmu_btop(LEVEL_SIZE(l));
28417c478bd9Sstevel@tonic-gate 			pgcnt -= mmu_btop(LEVEL_SIZE(l));
28427c478bd9Sstevel@tonic-gate 		}
28437c478bd9Sstevel@tonic-gate 	}
28447c478bd9Sstevel@tonic-gate 	if (ism_ht != NULL)
28457c478bd9Sstevel@tonic-gate 		htable_release(ism_ht);
28467c478bd9Sstevel@tonic-gate 	return (0);
28477c478bd9Sstevel@tonic-gate }
28487c478bd9Sstevel@tonic-gate 
28497c478bd9Sstevel@tonic-gate 
28507c478bd9Sstevel@tonic-gate /*
28517c478bd9Sstevel@tonic-gate  * hat_unshare() is similar to hat_unload_callback(), but
28527c478bd9Sstevel@tonic-gate  * we have to look for empty shared pagetables. Note that
28537c478bd9Sstevel@tonic-gate  * hat_unshare() is always invoked against an entire segment.
28547c478bd9Sstevel@tonic-gate  */
28557c478bd9Sstevel@tonic-gate /*ARGSUSED*/
28567c478bd9Sstevel@tonic-gate void
28577c478bd9Sstevel@tonic-gate hat_unshare(hat_t *hat, caddr_t addr, size_t len, uint_t ismszc)
28587c478bd9Sstevel@tonic-gate {
28597c478bd9Sstevel@tonic-gate 	uintptr_t	vaddr = (uintptr_t)addr;
28607c478bd9Sstevel@tonic-gate 	uintptr_t	eaddr = vaddr + len;
28617c478bd9Sstevel@tonic-gate 	htable_t	*ht = NULL;
28627c478bd9Sstevel@tonic-gate 	uint_t		need_demaps = 0;
2863*250b7ff9Sjosephb 	int		flags = HAT_UNLOAD_UNMAP;
2864*250b7ff9Sjosephb 	level_t		l;
28657c478bd9Sstevel@tonic-gate 
28667c478bd9Sstevel@tonic-gate 	ASSERT(hat != kas.a_hat);
2867ae115bc7Smrj 	ASSERT(eaddr <= _userlimit);
28687c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(vaddr));
28697c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(eaddr));
28707c478bd9Sstevel@tonic-gate 
28717c478bd9Sstevel@tonic-gate 	/*
28727c478bd9Sstevel@tonic-gate 	 * First go through and remove any shared pagetables.
28737c478bd9Sstevel@tonic-gate 	 *
2874ae115bc7Smrj 	 * Note that it's ok to delay the TLB shootdown till the entire range is
28757c478bd9Sstevel@tonic-gate 	 * finished, because if hat_pageunload() were to unload a shared
2876ae115bc7Smrj 	 * pagetable page, its hat_tlb_inval() will do a global TLB invalidate.
28777c478bd9Sstevel@tonic-gate 	 */
2878*250b7ff9Sjosephb 	l = mmu.max_page_level;
2879*250b7ff9Sjosephb 	if (l == mmu.max_level)
2880*250b7ff9Sjosephb 		--l;
2881*250b7ff9Sjosephb 	for (; l >= 0; --l) {
2882*250b7ff9Sjosephb 		for (vaddr = (uintptr_t)addr; vaddr < eaddr;
2883*250b7ff9Sjosephb 		    vaddr = (vaddr & LEVEL_MASK(l + 1)) + LEVEL_SIZE(l + 1)) {
2884*250b7ff9Sjosephb 			ASSERT(!IN_VA_HOLE(vaddr));
2885*250b7ff9Sjosephb 			/*
2886*250b7ff9Sjosephb 			 * find a pagetable that maps the current address
2887*250b7ff9Sjosephb 			 */
2888*250b7ff9Sjosephb 			ht = htable_lookup(hat, vaddr, l);
2889*250b7ff9Sjosephb 			if (ht == NULL)
2890*250b7ff9Sjosephb 				continue;
28917c478bd9Sstevel@tonic-gate 			if (ht->ht_flags & HTABLE_SHARED_PFN) {
28927c478bd9Sstevel@tonic-gate 				/*
2893*250b7ff9Sjosephb 				 * clear page count, set valid_cnt to 0,
2894*250b7ff9Sjosephb 				 * let htable_release() finish the job
28957c478bd9Sstevel@tonic-gate 				 */
2896*250b7ff9Sjosephb 				hat->hat_ism_pgcnt -= ht->ht_valid_cnt <<
2897*250b7ff9Sjosephb 				    (LEVEL_SHIFT(ht->ht_level) - MMU_PAGESHIFT);
28987c478bd9Sstevel@tonic-gate 				ht->ht_valid_cnt = 0;
28997c478bd9Sstevel@tonic-gate 				need_demaps = 1;
29007c478bd9Sstevel@tonic-gate 			}
29017c478bd9Sstevel@tonic-gate 			htable_release(ht);
29027c478bd9Sstevel@tonic-gate 		}
29037c478bd9Sstevel@tonic-gate 	}
29047c478bd9Sstevel@tonic-gate 
29057c478bd9Sstevel@tonic-gate 	/*
29067c478bd9Sstevel@tonic-gate 	 * flush the TLBs - since we're probably dealing with MANY mappings
29077c478bd9Sstevel@tonic-gate 	 * we do just one CR3 reload.
29087c478bd9Sstevel@tonic-gate 	 */
29097c478bd9Sstevel@tonic-gate 	if (!(hat->hat_flags & HAT_FREEING) && need_demaps)
2910ae115bc7Smrj 		hat_tlb_inval(hat, DEMAP_ALL_ADDR);
29117c478bd9Sstevel@tonic-gate 
29127c478bd9Sstevel@tonic-gate 	/*
29137c478bd9Sstevel@tonic-gate 	 * Now go back and clean up any unaligned mappings that
29147c478bd9Sstevel@tonic-gate 	 * couldn't share pagetables.
29157c478bd9Sstevel@tonic-gate 	 */
2916*250b7ff9Sjosephb 	if (!is_it_dism(hat, addr))
2917*250b7ff9Sjosephb 		flags |= HAT_UNLOAD_UNLOCK;
2918*250b7ff9Sjosephb 	hat_unload(hat, addr, len, flags);
29197c478bd9Sstevel@tonic-gate }
29207c478bd9Sstevel@tonic-gate 
29217c478bd9Sstevel@tonic-gate 
29227c478bd9Sstevel@tonic-gate /*
29237c478bd9Sstevel@tonic-gate  * hat_reserve() does nothing
29247c478bd9Sstevel@tonic-gate  */
29257c478bd9Sstevel@tonic-gate /*ARGSUSED*/
29267c478bd9Sstevel@tonic-gate void
29277c478bd9Sstevel@tonic-gate hat_reserve(struct as *as, caddr_t addr, size_t len)
29287c478bd9Sstevel@tonic-gate {
29297c478bd9Sstevel@tonic-gate }
29307c478bd9Sstevel@tonic-gate 
29317c478bd9Sstevel@tonic-gate 
29327c478bd9Sstevel@tonic-gate /*
29337c478bd9Sstevel@tonic-gate  * Called when all mappings to a page should have write permission removed.
29347c478bd9Sstevel@tonic-gate  * Mostly stolem from hat_pagesync()
29357c478bd9Sstevel@tonic-gate  */
29367c478bd9Sstevel@tonic-gate static void
29377c478bd9Sstevel@tonic-gate hati_page_clrwrt(struct page *pp)
29387c478bd9Sstevel@tonic-gate {
29397c478bd9Sstevel@tonic-gate 	hment_t		*hm = NULL;
29407c478bd9Sstevel@tonic-gate 	htable_t	*ht;
29417c478bd9Sstevel@tonic-gate 	uint_t		entry;
29427c478bd9Sstevel@tonic-gate 	x86pte_t	old;
29437c478bd9Sstevel@tonic-gate 	x86pte_t	new;
29447c478bd9Sstevel@tonic-gate 	uint_t		pszc = 0;
29457c478bd9Sstevel@tonic-gate 
29467c478bd9Sstevel@tonic-gate next_size:
29477c478bd9Sstevel@tonic-gate 	/*
29487c478bd9Sstevel@tonic-gate 	 * walk thru the mapping list clearing write permission
29497c478bd9Sstevel@tonic-gate 	 */
29507c478bd9Sstevel@tonic-gate 	x86_hm_enter(pp);
29517c478bd9Sstevel@tonic-gate 	while ((hm = hment_walk(pp, &ht, &entry, hm)) != NULL) {
29527c478bd9Sstevel@tonic-gate 		if (ht->ht_level < pszc)
29537c478bd9Sstevel@tonic-gate 			continue;
29547c478bd9Sstevel@tonic-gate 		old = x86pte_get(ht, entry);
29557c478bd9Sstevel@tonic-gate 
29567c478bd9Sstevel@tonic-gate 		for (;;) {
29577c478bd9Sstevel@tonic-gate 			/*
29587c478bd9Sstevel@tonic-gate 			 * Is this mapping of interest?
29597c478bd9Sstevel@tonic-gate 			 */
29607c478bd9Sstevel@tonic-gate 			if (PTE2PFN(old, ht->ht_level) != pp->p_pagenum ||
29617c478bd9Sstevel@tonic-gate 			    PTE_GET(old, PT_WRITABLE) == 0)
29627c478bd9Sstevel@tonic-gate 				break;
29637c478bd9Sstevel@tonic-gate 
29647c478bd9Sstevel@tonic-gate 			/*
29657c478bd9Sstevel@tonic-gate 			 * Clear ref/mod writable bits. This requires cross
29667c478bd9Sstevel@tonic-gate 			 * calls to ensure any executing TLBs see cleared bits.
29677c478bd9Sstevel@tonic-gate 			 */
29687c478bd9Sstevel@tonic-gate 			new = old;
29697c478bd9Sstevel@tonic-gate 			PTE_CLR(new, PT_REF | PT_MOD | PT_WRITABLE);
29707c478bd9Sstevel@tonic-gate 			old = hati_update_pte(ht, entry, old, new);
29717c478bd9Sstevel@tonic-gate 			if (old != 0)
29727c478bd9Sstevel@tonic-gate 				continue;
29737c478bd9Sstevel@tonic-gate 
29747c478bd9Sstevel@tonic-gate 			break;
29757c478bd9Sstevel@tonic-gate 		}
29767c478bd9Sstevel@tonic-gate 	}
29777c478bd9Sstevel@tonic-gate 	x86_hm_exit(pp);
29787c478bd9Sstevel@tonic-gate 	while (pszc < pp->p_szc) {
29797c478bd9Sstevel@tonic-gate 		page_t *tpp;
29807c478bd9Sstevel@tonic-gate 		pszc++;
29817c478bd9Sstevel@tonic-gate 		tpp = PP_GROUPLEADER(pp, pszc);
29827c478bd9Sstevel@tonic-gate 		if (pp != tpp) {
29837c478bd9Sstevel@tonic-gate 			pp = tpp;
29847c478bd9Sstevel@tonic-gate 			goto next_size;
29857c478bd9Sstevel@tonic-gate 		}
29867c478bd9Sstevel@tonic-gate 	}
29877c478bd9Sstevel@tonic-gate }
29887c478bd9Sstevel@tonic-gate 
29897c478bd9Sstevel@tonic-gate /*
29907c478bd9Sstevel@tonic-gate  * void hat_page_setattr(pp, flag)
29917c478bd9Sstevel@tonic-gate  * void hat_page_clrattr(pp, flag)
29927c478bd9Sstevel@tonic-gate  *	used to set/clr ref/mod bits.
29937c478bd9Sstevel@tonic-gate  */
29947c478bd9Sstevel@tonic-gate void
29957c478bd9Sstevel@tonic-gate hat_page_setattr(struct page *pp, uint_t flag)
29967c478bd9Sstevel@tonic-gate {
29977c478bd9Sstevel@tonic-gate 	vnode_t		*vp = pp->p_vnode;
29987c478bd9Sstevel@tonic-gate 	kmutex_t	*vphm = NULL;
29997c478bd9Sstevel@tonic-gate 	page_t		**listp;
3000d9615970Sqiao 	int		noshuffle;
3001d9615970Sqiao 
3002d9615970Sqiao 	noshuffle = flag & P_NSH;
3003d9615970Sqiao 	flag &= ~P_NSH;
30047c478bd9Sstevel@tonic-gate 
30057c478bd9Sstevel@tonic-gate 	if (PP_GETRM(pp, flag) == flag)
30067c478bd9Sstevel@tonic-gate 		return;
30077c478bd9Sstevel@tonic-gate 
3008d9615970Sqiao 	if ((flag & P_MOD) != 0 && vp != NULL && IS_VMODSORT(vp) &&
3009d9615970Sqiao 	    !noshuffle) {
30107c478bd9Sstevel@tonic-gate 		vphm = page_vnode_mutex(vp);
30117c478bd9Sstevel@tonic-gate 		mutex_enter(vphm);
30127c478bd9Sstevel@tonic-gate 	}
30137c478bd9Sstevel@tonic-gate 
30147c478bd9Sstevel@tonic-gate 	PP_SETRM(pp, flag);
30157c478bd9Sstevel@tonic-gate 
30167c478bd9Sstevel@tonic-gate 	if (vphm != NULL) {
30177c478bd9Sstevel@tonic-gate 
30187c478bd9Sstevel@tonic-gate 		/*
30197c478bd9Sstevel@tonic-gate 		 * Some File Systems examine v_pages for NULL w/o
30207c478bd9Sstevel@tonic-gate 		 * grabbing the vphm mutex. Must not let it become NULL when
30217c478bd9Sstevel@tonic-gate 		 * pp is the only page on the list.
30227c478bd9Sstevel@tonic-gate 		 */
30237c478bd9Sstevel@tonic-gate 		if (pp->p_vpnext != pp) {
30247c478bd9Sstevel@tonic-gate 			page_vpsub(&vp->v_pages, pp);
30257c478bd9Sstevel@tonic-gate 			if (vp->v_pages != NULL)
30267c478bd9Sstevel@tonic-gate 				listp = &vp->v_pages->p_vpprev->p_vpnext;
30277c478bd9Sstevel@tonic-gate 			else
30287c478bd9Sstevel@tonic-gate 				listp = &vp->v_pages;
30297c478bd9Sstevel@tonic-gate 			page_vpadd(listp, pp);
30307c478bd9Sstevel@tonic-gate 		}
30317c478bd9Sstevel@tonic-gate 		mutex_exit(vphm);
30327c478bd9Sstevel@tonic-gate 	}
30337c478bd9Sstevel@tonic-gate }
30347c478bd9Sstevel@tonic-gate 
30357c478bd9Sstevel@tonic-gate void
30367c478bd9Sstevel@tonic-gate hat_page_clrattr(struct page *pp, uint_t flag)
30377c478bd9Sstevel@tonic-gate {
30387c478bd9Sstevel@tonic-gate 	vnode_t		*vp = pp->p_vnode;
30397c478bd9Sstevel@tonic-gate 	ASSERT(!(flag & ~(P_MOD | P_REF | P_RO)));
30407c478bd9Sstevel@tonic-gate 
30417c478bd9Sstevel@tonic-gate 	/*
3042a71e32b6Sstans 	 * Caller is expected to hold page's io lock for VMODSORT to work
3043a71e32b6Sstans 	 * correctly with pvn_vplist_dirty() and pvn_getdirty() when mod
3044a71e32b6Sstans 	 * bit is cleared.
3045a71e32b6Sstans 	 * We don't have assert to avoid tripping some existing third party
3046a71e32b6Sstans 	 * code. The dirty page is moved back to top of the v_page list
3047a71e32b6Sstans 	 * after IO is done in pvn_write_done().
30487c478bd9Sstevel@tonic-gate 	 */
30497c478bd9Sstevel@tonic-gate 	PP_CLRRM(pp, flag);
30507c478bd9Sstevel@tonic-gate 
3051a71e32b6Sstans 	if ((flag & P_MOD) != 0 && vp != NULL && IS_VMODSORT(vp)) {
30527c478bd9Sstevel@tonic-gate 
30537c478bd9Sstevel@tonic-gate 		/*
30547c478bd9Sstevel@tonic-gate 		 * VMODSORT works by removing write permissions and getting
30557c478bd9Sstevel@tonic-gate 		 * a fault when a page is made dirty. At this point
30567c478bd9Sstevel@tonic-gate 		 * we need to remove write permission from all mappings
30577c478bd9Sstevel@tonic-gate 		 * to this page.
30587c478bd9Sstevel@tonic-gate 		 */
30597c478bd9Sstevel@tonic-gate 		hati_page_clrwrt(pp);
30607c478bd9Sstevel@tonic-gate 	}
30617c478bd9Sstevel@tonic-gate }
30627c478bd9Sstevel@tonic-gate 
30637c478bd9Sstevel@tonic-gate /*
30647c478bd9Sstevel@tonic-gate  *	If flag is specified, returns 0 if attribute is disabled
30657c478bd9Sstevel@tonic-gate  *	and non zero if enabled.  If flag specifes multiple attributs
30667c478bd9Sstevel@tonic-gate  *	then returns 0 if ALL atriibutes are disabled.  This is an advisory
30677c478bd9Sstevel@tonic-gate  *	call.
30687c478bd9Sstevel@tonic-gate  */
30697c478bd9Sstevel@tonic-gate uint_t
30707c478bd9Sstevel@tonic-gate hat_page_getattr(struct page *pp, uint_t flag)
30717c478bd9Sstevel@tonic-gate {
30727c478bd9Sstevel@tonic-gate 	return (PP_GETRM(pp, flag));
30737c478bd9Sstevel@tonic-gate }
30747c478bd9Sstevel@tonic-gate 
30757c478bd9Sstevel@tonic-gate 
30767c478bd9Sstevel@tonic-gate /*
30777c478bd9Sstevel@tonic-gate  * common code used by hat_pageunload() and hment_steal()
30787c478bd9Sstevel@tonic-gate  */
30797c478bd9Sstevel@tonic-gate hment_t *
30807c478bd9Sstevel@tonic-gate hati_page_unmap(page_t *pp, htable_t *ht, uint_t entry)
30817c478bd9Sstevel@tonic-gate {
30827c478bd9Sstevel@tonic-gate 	x86pte_t old_pte;
30837c478bd9Sstevel@tonic-gate 	pfn_t pfn = pp->p_pagenum;
30847c478bd9Sstevel@tonic-gate 	hment_t *hm;
30857c478bd9Sstevel@tonic-gate 
30867c478bd9Sstevel@tonic-gate 	/*
30877c478bd9Sstevel@tonic-gate 	 * We need to acquire a hold on the htable in order to
30887c478bd9Sstevel@tonic-gate 	 * do the invalidate. We know the htable must exist, since
30897c478bd9Sstevel@tonic-gate 	 * unmap's don't release the htable until after removing any
30907c478bd9Sstevel@tonic-gate 	 * hment. Having x86_hm_enter() keeps that from proceeding.
30917c478bd9Sstevel@tonic-gate 	 */
30927c478bd9Sstevel@tonic-gate 	htable_acquire(ht);
30937c478bd9Sstevel@tonic-gate 
30947c478bd9Sstevel@tonic-gate 	/*
30957c478bd9Sstevel@tonic-gate 	 * Invalidate the PTE and remove the hment.
30967c478bd9Sstevel@tonic-gate 	 */
3097ae115bc7Smrj 	old_pte = x86pte_inval(ht, entry, 0, NULL);
3098aa2ed9e5Sjosephb 	if (PTE2PFN(old_pte, ht->ht_level) != pfn) {
3099ae115bc7Smrj 		panic("x86pte_inval() failure found PTE = " FMT_PTE
3100aa2ed9e5Sjosephb 		    " pfn being unmapped is %lx ht=0x%lx entry=0x%x",
3101aa2ed9e5Sjosephb 		    old_pte, pfn, (uintptr_t)ht, entry);
3102aa2ed9e5Sjosephb 	}
31037c478bd9Sstevel@tonic-gate 
31047c478bd9Sstevel@tonic-gate 	/*
31057c478bd9Sstevel@tonic-gate 	 * Clean up all the htable information for this mapping
31067c478bd9Sstevel@tonic-gate 	 */
31077c478bd9Sstevel@tonic-gate 	ASSERT(ht->ht_valid_cnt > 0);
31087c478bd9Sstevel@tonic-gate 	HTABLE_DEC(ht->ht_valid_cnt);
31097c478bd9Sstevel@tonic-gate 	PGCNT_DEC(ht->ht_hat, ht->ht_level);
31107c478bd9Sstevel@tonic-gate 
31117c478bd9Sstevel@tonic-gate 	/*
31127c478bd9Sstevel@tonic-gate 	 * sync ref/mod bits to the page_t
31137c478bd9Sstevel@tonic-gate 	 */
3114ae115bc7Smrj 	if (PTE_GET(old_pte, PT_SOFTWARE) < PT_NOSYNC)
31157c478bd9Sstevel@tonic-gate 		hati_sync_pte_to_page(pp, old_pte, ht->ht_level);
31167c478bd9Sstevel@tonic-gate 
31177c478bd9Sstevel@tonic-gate 	/*
31187c478bd9Sstevel@tonic-gate 	 * Remove the mapping list entry for this page.
31197c478bd9Sstevel@tonic-gate 	 */
31207c478bd9Sstevel@tonic-gate 	hm = hment_remove(pp, ht, entry);
31217c478bd9Sstevel@tonic-gate 
31227c478bd9Sstevel@tonic-gate 	/*
31237c478bd9Sstevel@tonic-gate 	 * drop the mapping list lock so that we might free the
31247c478bd9Sstevel@tonic-gate 	 * hment and htable.
31257c478bd9Sstevel@tonic-gate 	 */
31267c478bd9Sstevel@tonic-gate 	x86_hm_exit(pp);
31277c478bd9Sstevel@tonic-gate 	htable_release(ht);
31287c478bd9Sstevel@tonic-gate 	return (hm);
31297c478bd9Sstevel@tonic-gate }
31307c478bd9Sstevel@tonic-gate 
3131a5652762Spraks extern int	vpm_enable;
31327c478bd9Sstevel@tonic-gate /*
31337c478bd9Sstevel@tonic-gate  * Unload all translations to a page. If the page is a subpage of a large
31347c478bd9Sstevel@tonic-gate  * page, the large page mappings are also removed.
31357c478bd9Sstevel@tonic-gate  *
31367c478bd9Sstevel@tonic-gate  * The forceflags are unused.
31377c478bd9Sstevel@tonic-gate  */
31387c478bd9Sstevel@tonic-gate 
31397c478bd9Sstevel@tonic-gate /*ARGSUSED*/
31407c478bd9Sstevel@tonic-gate static int
31417c478bd9Sstevel@tonic-gate hati_pageunload(struct page *pp, uint_t pg_szcd, uint_t forceflag)
31427c478bd9Sstevel@tonic-gate {
31437c478bd9Sstevel@tonic-gate 	page_t		*cur_pp = pp;
31447c478bd9Sstevel@tonic-gate 	hment_t		*hm;
31457c478bd9Sstevel@tonic-gate 	hment_t		*prev;
31467c478bd9Sstevel@tonic-gate 	htable_t	*ht;
31477c478bd9Sstevel@tonic-gate 	uint_t		entry;
31487c478bd9Sstevel@tonic-gate 	level_t		level;
31497c478bd9Sstevel@tonic-gate 
3150a5652762Spraks #if defined(__amd64)
3151a5652762Spraks 	/*
3152a5652762Spraks 	 * clear the vpm ref.
3153a5652762Spraks 	 */
3154a5652762Spraks 	if (vpm_enable) {
3155a5652762Spraks 		pp->p_vpmref = 0;
3156a5652762Spraks 	}
3157a5652762Spraks #endif
31587c478bd9Sstevel@tonic-gate 	/*
31597c478bd9Sstevel@tonic-gate 	 * The loop with next_size handles pages with multiple pagesize mappings
31607c478bd9Sstevel@tonic-gate 	 */
31617c478bd9Sstevel@tonic-gate next_size:
31627c478bd9Sstevel@tonic-gate 	for (;;) {
31637c478bd9Sstevel@tonic-gate 
31647c478bd9Sstevel@tonic-gate 		/*
31657c478bd9Sstevel@tonic-gate 		 * Get a mapping list entry
31667c478bd9Sstevel@tonic-gate 		 */
31677c478bd9Sstevel@tonic-gate 		x86_hm_enter(cur_pp);
31687c478bd9Sstevel@tonic-gate 		for (prev = NULL; ; prev = hm) {
31697c478bd9Sstevel@tonic-gate 			hm = hment_walk(cur_pp, &ht, &entry, prev);
31707c478bd9Sstevel@tonic-gate 			if (hm == NULL) {
31717c478bd9Sstevel@tonic-gate 				x86_hm_exit(cur_pp);
31727c478bd9Sstevel@tonic-gate 
31737c478bd9Sstevel@tonic-gate 				/*
31747c478bd9Sstevel@tonic-gate 				 * If not part of a larger page, we're done.
31757c478bd9Sstevel@tonic-gate 				 */
3176ae115bc7Smrj 				if (cur_pp->p_szc <= pg_szcd) {
31777c478bd9Sstevel@tonic-gate 					return (0);
3178ae115bc7Smrj 				}
31797c478bd9Sstevel@tonic-gate 
31807c478bd9Sstevel@tonic-gate 				/*
31817c478bd9Sstevel@tonic-gate 				 * Else check the next larger page size.
31827c478bd9Sstevel@tonic-gate 				 * hat_page_demote() may decrease p_szc
31837c478bd9Sstevel@tonic-gate 				 * but that's ok we'll just take an extra
31847c478bd9Sstevel@tonic-gate 				 * trip discover there're no larger mappings
31857c478bd9Sstevel@tonic-gate 				 * and return.
31867c478bd9Sstevel@tonic-gate 				 */
31877c478bd9Sstevel@tonic-gate 				++pg_szcd;
31887c478bd9Sstevel@tonic-gate 				cur_pp = PP_GROUPLEADER(cur_pp, pg_szcd);
31897c478bd9Sstevel@tonic-gate 				goto next_size;
31907c478bd9Sstevel@tonic-gate 			}
31917c478bd9Sstevel@tonic-gate 
31927c478bd9Sstevel@tonic-gate 			/*
31937c478bd9Sstevel@tonic-gate 			 * If this mapping size matches, remove it.
31947c478bd9Sstevel@tonic-gate 			 */
31957c478bd9Sstevel@tonic-gate 			level = ht->ht_level;
31967c478bd9Sstevel@tonic-gate 			if (level == pg_szcd)
31977c478bd9Sstevel@tonic-gate 				break;
31987c478bd9Sstevel@tonic-gate 		}
31997c478bd9Sstevel@tonic-gate 
32007c478bd9Sstevel@tonic-gate 		/*
32017c478bd9Sstevel@tonic-gate 		 * Remove the mapping list entry for this page.
32027c478bd9Sstevel@tonic-gate 		 * Note this does the x86_hm_exit() for us.
32037c478bd9Sstevel@tonic-gate 		 */
32047c478bd9Sstevel@tonic-gate 		hm = hati_page_unmap(cur_pp, ht, entry);
32057c478bd9Sstevel@tonic-gate 		if (hm != NULL)
32067c478bd9Sstevel@tonic-gate 			hment_free(hm);
32077c478bd9Sstevel@tonic-gate 	}
32087c478bd9Sstevel@tonic-gate }
32097c478bd9Sstevel@tonic-gate 
32107c478bd9Sstevel@tonic-gate int
32117c478bd9Sstevel@tonic-gate hat_pageunload(struct page *pp, uint_t forceflag)
32127c478bd9Sstevel@tonic-gate {
32137c478bd9Sstevel@tonic-gate 	ASSERT(PAGE_EXCL(pp));
32147c478bd9Sstevel@tonic-gate 	return (hati_pageunload(pp, 0, forceflag));
32157c478bd9Sstevel@tonic-gate }
32167c478bd9Sstevel@tonic-gate 
32177c478bd9Sstevel@tonic-gate /*
32187c478bd9Sstevel@tonic-gate  * Unload all large mappings to pp and reduce by 1 p_szc field of every large
32197c478bd9Sstevel@tonic-gate  * page level that included pp.
32207c478bd9Sstevel@tonic-gate  *
32217c478bd9Sstevel@tonic-gate  * pp must be locked EXCL. Even though no other constituent pages are locked
32227c478bd9Sstevel@tonic-gate  * it's legal to unload large mappings to pp because all constituent pages of
32237c478bd9Sstevel@tonic-gate  * large locked mappings have to be locked SHARED.  therefore if we have EXCL
32247c478bd9Sstevel@tonic-gate  * lock on one of constituent pages none of the large mappings to pp are
32257c478bd9Sstevel@tonic-gate  * locked.
32267c478bd9Sstevel@tonic-gate  *
32277c478bd9Sstevel@tonic-gate  * Change (always decrease) p_szc field starting from the last constituent
32287c478bd9Sstevel@tonic-gate  * page and ending with root constituent page so that root's pszc always shows
32297c478bd9Sstevel@tonic-gate  * the area where hat_page_demote() may be active.
32307c478bd9Sstevel@tonic-gate  *
32317c478bd9Sstevel@tonic-gate  * This mechanism is only used for file system pages where it's not always
32327c478bd9Sstevel@tonic-gate  * possible to get EXCL locks on all constituent pages to demote the size code
32337c478bd9Sstevel@tonic-gate  * (as is done for anonymous or kernel large pages).
32347c478bd9Sstevel@tonic-gate  */
32357c478bd9Sstevel@tonic-gate void
32367c478bd9Sstevel@tonic-gate hat_page_demote(page_t *pp)
32377c478bd9Sstevel@tonic-gate {
32387c478bd9Sstevel@tonic-gate 	uint_t		pszc;
32397c478bd9Sstevel@tonic-gate 	uint_t		rszc;
32407c478bd9Sstevel@tonic-gate 	uint_t		szc;
32417c478bd9Sstevel@tonic-gate 	page_t		*rootpp;
32427c478bd9Sstevel@tonic-gate 	page_t		*firstpp;
32437c478bd9Sstevel@tonic-gate 	page_t		*lastpp;
32447c478bd9Sstevel@tonic-gate 	pgcnt_t		pgcnt;
32457c478bd9Sstevel@tonic-gate 
32467c478bd9Sstevel@tonic-gate 	ASSERT(PAGE_EXCL(pp));
32477c478bd9Sstevel@tonic-gate 	ASSERT(!PP_ISFREE(pp));
32487c478bd9Sstevel@tonic-gate 	ASSERT(page_szc_lock_assert(pp));
32497c478bd9Sstevel@tonic-gate 
32507c478bd9Sstevel@tonic-gate 	if (pp->p_szc == 0)
32517c478bd9Sstevel@tonic-gate 		return;
32527c478bd9Sstevel@tonic-gate 
32537c478bd9Sstevel@tonic-gate 	rootpp = PP_GROUPLEADER(pp, 1);
32547c478bd9Sstevel@tonic-gate 	(void) hati_pageunload(rootpp, 1, HAT_FORCE_PGUNLOAD);
32557c478bd9Sstevel@tonic-gate 
32567c478bd9Sstevel@tonic-gate 	/*
32577c478bd9Sstevel@tonic-gate 	 * all large mappings to pp are gone
32587c478bd9Sstevel@tonic-gate 	 * and no new can be setup since pp is locked exclusively.
32597c478bd9Sstevel@tonic-gate 	 *
32607c478bd9Sstevel@tonic-gate 	 * Lock the root to make sure there's only one hat_page_demote()
32617c478bd9Sstevel@tonic-gate 	 * outstanding within the area of this root's pszc.
32627c478bd9Sstevel@tonic-gate 	 *
32637c478bd9Sstevel@tonic-gate 	 * Second potential hat_page_demote() is already eliminated by upper
32647c478bd9Sstevel@tonic-gate 	 * VM layer via page_szc_lock() but we don't rely on it and use our
32657c478bd9Sstevel@tonic-gate 	 * own locking (so that upper layer locking can be changed without
32667c478bd9Sstevel@tonic-gate 	 * assumptions that hat depends on upper layer VM to prevent multiple
32677c478bd9Sstevel@tonic-gate 	 * hat_page_demote() to be issued simultaneously to the same large
32687c478bd9Sstevel@tonic-gate 	 * page).
32697c478bd9Sstevel@tonic-gate 	 */
32707c478bd9Sstevel@tonic-gate again:
32717c478bd9Sstevel@tonic-gate 	pszc = pp->p_szc;
32727c478bd9Sstevel@tonic-gate 	if (pszc == 0)
32737c478bd9Sstevel@tonic-gate 		return;
32747c478bd9Sstevel@tonic-gate 	rootpp = PP_GROUPLEADER(pp, pszc);
32757c478bd9Sstevel@tonic-gate 	x86_hm_enter(rootpp);
32767c478bd9Sstevel@tonic-gate 	/*
32777c478bd9Sstevel@tonic-gate 	 * If root's p_szc is different from pszc we raced with another
32787c478bd9Sstevel@tonic-gate 	 * hat_page_demote().  Drop the lock and try to find the root again.
32797c478bd9Sstevel@tonic-gate 	 * If root's p_szc is greater than pszc previous hat_page_demote() is
32807c478bd9Sstevel@tonic-gate 	 * not done yet.  Take and release mlist lock of root's root to wait
32817c478bd9Sstevel@tonic-gate 	 * for previous hat_page_demote() to complete.
32827c478bd9Sstevel@tonic-gate 	 */
32837c478bd9Sstevel@tonic-gate 	if ((rszc = rootpp->p_szc) != pszc) {
32847c478bd9Sstevel@tonic-gate 		x86_hm_exit(rootpp);
32857c478bd9Sstevel@tonic-gate 		if (rszc > pszc) {
32867c478bd9Sstevel@tonic-gate 			/* p_szc of a locked non free page can't increase */
32877c478bd9Sstevel@tonic-gate 			ASSERT(pp != rootpp);
32887c478bd9Sstevel@tonic-gate 
32897c478bd9Sstevel@tonic-gate 			rootpp = PP_GROUPLEADER(rootpp, rszc);
32907c478bd9Sstevel@tonic-gate 			x86_hm_enter(rootpp);
32917c478bd9Sstevel@tonic-gate 			x86_hm_exit(rootpp);
32927c478bd9Sstevel@tonic-gate 		}
32937c478bd9Sstevel@tonic-gate 		goto again;
32947c478bd9Sstevel@tonic-gate 	}
32957c478bd9Sstevel@tonic-gate 	ASSERT(pp->p_szc == pszc);
32967c478bd9Sstevel@tonic-gate 
32977c478bd9Sstevel@tonic-gate 	/*
32987c478bd9Sstevel@tonic-gate 	 * Decrement by 1 p_szc of every constituent page of a region that
32997c478bd9Sstevel@tonic-gate 	 * covered pp. For example if original szc is 3 it gets changed to 2
33007c478bd9Sstevel@tonic-gate 	 * everywhere except in region 2 that covered pp. Region 2 that
33017c478bd9Sstevel@tonic-gate 	 * covered pp gets demoted to 1 everywhere except in region 1 that
33027c478bd9Sstevel@tonic-gate 	 * covered pp. The region 1 that covered pp is demoted to region
33037c478bd9Sstevel@tonic-gate 	 * 0. It's done this way because from region 3 we removed level 3
33047c478bd9Sstevel@tonic-gate 	 * mappings, from region 2 that covered pp we removed level 2 mappings
33057c478bd9Sstevel@tonic-gate 	 * and from region 1 that covered pp we removed level 1 mappings.  All
33067c478bd9Sstevel@tonic-gate 	 * changes are done from from high pfn's to low pfn's so that roots
33077c478bd9Sstevel@tonic-gate 	 * are changed last allowing one to know the largest region where
33087c478bd9Sstevel@tonic-gate 	 * hat_page_demote() is stil active by only looking at the root page.
33097c478bd9Sstevel@tonic-gate 	 *
33107c478bd9Sstevel@tonic-gate 	 * This algorithm is implemented in 2 while loops. First loop changes
33117c478bd9Sstevel@tonic-gate 	 * p_szc of pages to the right of pp's level 1 region and second
33127c478bd9Sstevel@tonic-gate 	 * loop changes p_szc of pages of level 1 region that covers pp
33137c478bd9Sstevel@tonic-gate 	 * and all pages to the left of level 1 region that covers pp.
33147c478bd9Sstevel@tonic-gate 	 * In the first loop p_szc keeps dropping with every iteration
33157c478bd9Sstevel@tonic-gate 	 * and in the second loop it keeps increasing with every iteration.
33167c478bd9Sstevel@tonic-gate 	 *
33177c478bd9Sstevel@tonic-gate 	 * First loop description: Demote pages to the right of pp outside of
33187c478bd9Sstevel@tonic-gate 	 * level 1 region that covers pp.  In every iteration of the while
33197c478bd9Sstevel@tonic-gate 	 * loop below find the last page of szc region and the first page of
33207c478bd9Sstevel@tonic-gate 	 * (szc - 1) region that is immediately to the right of (szc - 1)
33217c478bd9Sstevel@tonic-gate 	 * region that covers pp.  From last such page to first such page
33227c478bd9Sstevel@tonic-gate 	 * change every page's szc to szc - 1. Decrement szc and continue
33237c478bd9Sstevel@tonic-gate 	 * looping until szc is 1. If pp belongs to the last (szc - 1) region
33247c478bd9Sstevel@tonic-gate 	 * of szc region skip to the next iteration.
33257c478bd9Sstevel@tonic-gate 	 */
33267c478bd9Sstevel@tonic-gate 	szc = pszc;
33277c478bd9Sstevel@tonic-gate 	while (szc > 1) {
33287c478bd9Sstevel@tonic-gate 		lastpp = PP_GROUPLEADER(pp, szc);
33297c478bd9Sstevel@tonic-gate 		pgcnt = page_get_pagecnt(szc);
33307c478bd9Sstevel@tonic-gate 		lastpp += pgcnt - 1;
33317c478bd9Sstevel@tonic-gate 		firstpp = PP_GROUPLEADER(pp, (szc - 1));
33327c478bd9Sstevel@tonic-gate 		pgcnt = page_get_pagecnt(szc - 1);
33337c478bd9Sstevel@tonic-gate 		if (lastpp - firstpp < pgcnt) {
33347c478bd9Sstevel@tonic-gate 			szc--;
33357c478bd9Sstevel@tonic-gate 			continue;
33367c478bd9Sstevel@tonic-gate 		}
33377c478bd9Sstevel@tonic-gate 		firstpp += pgcnt;
33387c478bd9Sstevel@tonic-gate 		while (lastpp != firstpp) {
33397c478bd9Sstevel@tonic-gate 			ASSERT(lastpp->p_szc == pszc);
33407c478bd9Sstevel@tonic-gate 			lastpp->p_szc = szc - 1;
33417c478bd9Sstevel@tonic-gate 			lastpp--;
33427c478bd9Sstevel@tonic-gate 		}
33437c478bd9Sstevel@tonic-gate 		firstpp->p_szc = szc - 1;
33447c478bd9Sstevel@tonic-gate 		szc--;
33457c478bd9Sstevel@tonic-gate 	}
33467c478bd9Sstevel@tonic-gate 
33477c478bd9Sstevel@tonic-gate 	/*
33487c478bd9Sstevel@tonic-gate 	 * Second loop description:
33497c478bd9Sstevel@tonic-gate 	 * First iteration changes p_szc to 0 of every
33507c478bd9Sstevel@tonic-gate 	 * page of level 1 region that covers pp.
33517c478bd9Sstevel@tonic-gate 	 * Subsequent iterations find last page of szc region
33527c478bd9Sstevel@tonic-gate 	 * immediately to the left of szc region that covered pp
33537c478bd9Sstevel@tonic-gate 	 * and first page of (szc + 1) region that covers pp.
33547c478bd9Sstevel@tonic-gate 	 * From last to first page change p_szc of every page to szc.
33557c478bd9Sstevel@tonic-gate 	 * Increment szc and continue looping until szc is pszc.
33567c478bd9Sstevel@tonic-gate 	 * If pp belongs to the fist szc region of (szc + 1) region
33577c478bd9Sstevel@tonic-gate 	 * skip to the next iteration.
33587c478bd9Sstevel@tonic-gate 	 *
33597c478bd9Sstevel@tonic-gate 	 */
33607c478bd9Sstevel@tonic-gate 	szc = 0;
33617c478bd9Sstevel@tonic-gate 	while (szc < pszc) {
33627c478bd9Sstevel@tonic-gate 		firstpp = PP_GROUPLEADER(pp, (szc + 1));
33637c478bd9Sstevel@tonic-gate 		if (szc == 0) {
33647c478bd9Sstevel@tonic-gate 			pgcnt = page_get_pagecnt(1);
33657c478bd9Sstevel@tonic-gate 			lastpp = firstpp + (pgcnt - 1);
33667c478bd9Sstevel@tonic-gate 		} else {
33677c478bd9Sstevel@tonic-gate 			lastpp = PP_GROUPLEADER(pp, szc);
33687c478bd9Sstevel@tonic-gate 			if (firstpp == lastpp) {
33697c478bd9Sstevel@tonic-gate 				szc++;
33707c478bd9Sstevel@tonic-gate 				continue;
33717c478bd9Sstevel@tonic-gate 			}
33727c478bd9Sstevel@tonic-gate 			lastpp--;
33737c478bd9Sstevel@tonic-gate 			pgcnt = page_get_pagecnt(szc);
33747c478bd9Sstevel@tonic-gate 		}
33757c478bd9Sstevel@tonic-gate 		while (lastpp != firstpp) {
33767c478bd9Sstevel@tonic-gate 			ASSERT(lastpp->p_szc == pszc);
33777c478bd9Sstevel@tonic-gate 			lastpp->p_szc = szc;
33787c478bd9Sstevel@tonic-gate 			lastpp--;
33797c478bd9Sstevel@tonic-gate 		}
33807c478bd9Sstevel@tonic-gate 		firstpp->p_szc = szc;
33817c478bd9Sstevel@tonic-gate 		if (firstpp == rootpp)
33827c478bd9Sstevel@tonic-gate 			break;
33837c478bd9Sstevel@tonic-gate 		szc++;
33847c478bd9Sstevel@tonic-gate 	}
33857c478bd9Sstevel@tonic-gate 	x86_hm_exit(rootpp);
33867c478bd9Sstevel@tonic-gate }
33877c478bd9Sstevel@tonic-gate 
33887c478bd9Sstevel@tonic-gate /*
33897c478bd9Sstevel@tonic-gate  * get hw stats from hardware into page struct and reset hw stats
33907c478bd9Sstevel@tonic-gate  * returns attributes of page
33917c478bd9Sstevel@tonic-gate  * Flags for hat_pagesync, hat_getstat, hat_sync
33927c478bd9Sstevel@tonic-gate  *
33937c478bd9Sstevel@tonic-gate  * define	HAT_SYNC_ZERORM		0x01
33947c478bd9Sstevel@tonic-gate  *
33957c478bd9Sstevel@tonic-gate  * Additional flags for hat_pagesync
33967c478bd9Sstevel@tonic-gate  *
33977c478bd9Sstevel@tonic-gate  * define	HAT_SYNC_STOPON_REF	0x02
33987c478bd9Sstevel@tonic-gate  * define	HAT_SYNC_STOPON_MOD	0x04
33997c478bd9Sstevel@tonic-gate  * define	HAT_SYNC_STOPON_RM	0x06
34007c478bd9Sstevel@tonic-gate  * define	HAT_SYNC_STOPON_SHARED	0x08
34017c478bd9Sstevel@tonic-gate  */
34027c478bd9Sstevel@tonic-gate uint_t
34037c478bd9Sstevel@tonic-gate hat_pagesync(struct page *pp, uint_t flags)
34047c478bd9Sstevel@tonic-gate {
34057c478bd9Sstevel@tonic-gate 	hment_t		*hm = NULL;
34067c478bd9Sstevel@tonic-gate 	htable_t	*ht;
34077c478bd9Sstevel@tonic-gate 	uint_t		entry;
34087c478bd9Sstevel@tonic-gate 	x86pte_t	old, save_old;
34097c478bd9Sstevel@tonic-gate 	x86pte_t	new;
34107c478bd9Sstevel@tonic-gate 	uchar_t		nrmbits = P_REF|P_MOD|P_RO;
34117c478bd9Sstevel@tonic-gate 	extern ulong_t	po_share;
34127c478bd9Sstevel@tonic-gate 	page_t		*save_pp = pp;
34137c478bd9Sstevel@tonic-gate 	uint_t		pszc = 0;
34147c478bd9Sstevel@tonic-gate 
34157c478bd9Sstevel@tonic-gate 	ASSERT(PAGE_LOCKED(pp) || panicstr);
34167c478bd9Sstevel@tonic-gate 
34177c478bd9Sstevel@tonic-gate 	if (PP_ISRO(pp) && (flags & HAT_SYNC_STOPON_MOD))
34187c478bd9Sstevel@tonic-gate 		return (pp->p_nrm & nrmbits);
34197c478bd9Sstevel@tonic-gate 
34207c478bd9Sstevel@tonic-gate 	if ((flags & HAT_SYNC_ZERORM) == 0) {
34217c478bd9Sstevel@tonic-gate 
34227c478bd9Sstevel@tonic-gate 		if ((flags & HAT_SYNC_STOPON_REF) != 0 && PP_ISREF(pp))
34237c478bd9Sstevel@tonic-gate 			return (pp->p_nrm & nrmbits);
34247c478bd9Sstevel@tonic-gate 
34257c478bd9Sstevel@tonic-gate 		if ((flags & HAT_SYNC_STOPON_MOD) != 0 && PP_ISMOD(pp))
34267c478bd9Sstevel@tonic-gate 			return (pp->p_nrm & nrmbits);
34277c478bd9Sstevel@tonic-gate 
34287c478bd9Sstevel@tonic-gate 		if ((flags & HAT_SYNC_STOPON_SHARED) != 0 &&
34297c478bd9Sstevel@tonic-gate 		    hat_page_getshare(pp) > po_share) {
34307c478bd9Sstevel@tonic-gate 			if (PP_ISRO(pp))
34317c478bd9Sstevel@tonic-gate 				PP_SETREF(pp);
34327c478bd9Sstevel@tonic-gate 			return (pp->p_nrm & nrmbits);
34337c478bd9Sstevel@tonic-gate 		}
34347c478bd9Sstevel@tonic-gate 	}
34357c478bd9Sstevel@tonic-gate 
34367c478bd9Sstevel@tonic-gate next_size:
34377c478bd9Sstevel@tonic-gate 	/*
34387c478bd9Sstevel@tonic-gate 	 * walk thru the mapping list syncing (and clearing) ref/mod bits.
34397c478bd9Sstevel@tonic-gate 	 */
34407c478bd9Sstevel@tonic-gate 	x86_hm_enter(pp);
34417c478bd9Sstevel@tonic-gate 	while ((hm = hment_walk(pp, &ht, &entry, hm)) != NULL) {
34427c478bd9Sstevel@tonic-gate 		if (ht->ht_level < pszc)
34437c478bd9Sstevel@tonic-gate 			continue;
34447c478bd9Sstevel@tonic-gate 		old = x86pte_get(ht, entry);
34457c478bd9Sstevel@tonic-gate try_again:
34467c478bd9Sstevel@tonic-gate 
34477c478bd9Sstevel@tonic-gate 		ASSERT(PTE2PFN(old, ht->ht_level) == pp->p_pagenum);
34487c478bd9Sstevel@tonic-gate 
34497c478bd9Sstevel@tonic-gate 		if (PTE_GET(old, PT_REF | PT_MOD) == 0)
34507c478bd9Sstevel@tonic-gate 			continue;
34517c478bd9Sstevel@tonic-gate 
34527c478bd9Sstevel@tonic-gate 		save_old = old;
34537c478bd9Sstevel@tonic-gate 		if ((flags & HAT_SYNC_ZERORM) != 0) {
34547c478bd9Sstevel@tonic-gate 
34557c478bd9Sstevel@tonic-gate 			/*
34567c478bd9Sstevel@tonic-gate 			 * Need to clear ref or mod bits. Need to demap
34577c478bd9Sstevel@tonic-gate 			 * to make sure any executing TLBs see cleared bits.
34587c478bd9Sstevel@tonic-gate 			 */
34597c478bd9Sstevel@tonic-gate 			new = old;
34607c478bd9Sstevel@tonic-gate 			PTE_CLR(new, PT_REF | PT_MOD);
34617c478bd9Sstevel@tonic-gate 			old = hati_update_pte(ht, entry, old, new);
34627c478bd9Sstevel@tonic-gate 			if (old != 0)
34637c478bd9Sstevel@tonic-gate 				goto try_again;
34647c478bd9Sstevel@tonic-gate 
34657c478bd9Sstevel@tonic-gate 			old = save_old;
34667c478bd9Sstevel@tonic-gate 		}
34677c478bd9Sstevel@tonic-gate 
34687c478bd9Sstevel@tonic-gate 		/*
34697c478bd9Sstevel@tonic-gate 		 * Sync the PTE
34707c478bd9Sstevel@tonic-gate 		 */
3471ae115bc7Smrj 		if (!(flags & HAT_SYNC_ZERORM) &&
3472ae115bc7Smrj 		    PTE_GET(old, PT_SOFTWARE) <= PT_NOSYNC)
34737c478bd9Sstevel@tonic-gate 			hati_sync_pte_to_page(pp, old, ht->ht_level);
34747c478bd9Sstevel@tonic-gate 
34757c478bd9Sstevel@tonic-gate 		/*
34767c478bd9Sstevel@tonic-gate 		 * can stop short if we found a ref'd or mod'd page
34777c478bd9Sstevel@tonic-gate 		 */
34787c478bd9Sstevel@tonic-gate 		if ((flags & HAT_SYNC_STOPON_MOD) && PP_ISMOD(save_pp) ||
34797c478bd9Sstevel@tonic-gate 		    (flags & HAT_SYNC_STOPON_REF) && PP_ISREF(save_pp)) {
34807c478bd9Sstevel@tonic-gate 			x86_hm_exit(pp);
3481ae115bc7Smrj 			goto done;
34827c478bd9Sstevel@tonic-gate 		}
34837c478bd9Sstevel@tonic-gate 	}
34847c478bd9Sstevel@tonic-gate 	x86_hm_exit(pp);
34857c478bd9Sstevel@tonic-gate 	while (pszc < pp->p_szc) {
34867c478bd9Sstevel@tonic-gate 		page_t *tpp;
34877c478bd9Sstevel@tonic-gate 		pszc++;
34887c478bd9Sstevel@tonic-gate 		tpp = PP_GROUPLEADER(pp, pszc);
34897c478bd9Sstevel@tonic-gate 		if (pp != tpp) {
34907c478bd9Sstevel@tonic-gate 			pp = tpp;
34917c478bd9Sstevel@tonic-gate 			goto next_size;
34927c478bd9Sstevel@tonic-gate 		}
34937c478bd9Sstevel@tonic-gate 	}
3494ae115bc7Smrj done:
34957c478bd9Sstevel@tonic-gate 	return (save_pp->p_nrm & nrmbits);
34967c478bd9Sstevel@tonic-gate }
34977c478bd9Sstevel@tonic-gate 
34987c478bd9Sstevel@tonic-gate /*
34997c478bd9Sstevel@tonic-gate  * returns approx number of mappings to this pp.  A return of 0 implies
35007c478bd9Sstevel@tonic-gate  * there are no mappings to the page.
35017c478bd9Sstevel@tonic-gate  */
35027c478bd9Sstevel@tonic-gate ulong_t
35037c478bd9Sstevel@tonic-gate hat_page_getshare(page_t *pp)
35047c478bd9Sstevel@tonic-gate {
35057c478bd9Sstevel@tonic-gate 	uint_t cnt;
35067c478bd9Sstevel@tonic-gate 	cnt = hment_mapcnt(pp);
3507a5652762Spraks #if defined(__amd64)
3508a5652762Spraks 	if (vpm_enable && pp->p_vpmref) {
3509a5652762Spraks 		cnt += 1;
3510a5652762Spraks 	}
3511a5652762Spraks #endif
35127c478bd9Sstevel@tonic-gate 	return (cnt);
35137c478bd9Sstevel@tonic-gate }
35147c478bd9Sstevel@tonic-gate 
35157c478bd9Sstevel@tonic-gate /*
35167c478bd9Sstevel@tonic-gate  * hat_softlock isn't supported anymore
35177c478bd9Sstevel@tonic-gate  */
35187c478bd9Sstevel@tonic-gate /*ARGSUSED*/
35197c478bd9Sstevel@tonic-gate faultcode_t
35207c478bd9Sstevel@tonic-gate hat_softlock(
35217c478bd9Sstevel@tonic-gate 	hat_t *hat,
35227c478bd9Sstevel@tonic-gate 	caddr_t addr,
35237c478bd9Sstevel@tonic-gate 	size_t *len,
35247c478bd9Sstevel@tonic-gate 	struct page **page_array,
35257c478bd9Sstevel@tonic-gate 	uint_t flags)
35267c478bd9Sstevel@tonic-gate {
35277c478bd9Sstevel@tonic-gate 	return (FC_NOSUPPORT);
35287c478bd9Sstevel@tonic-gate }
35297c478bd9Sstevel@tonic-gate 
35307c478bd9Sstevel@tonic-gate 
35317c478bd9Sstevel@tonic-gate 
35327c478bd9Sstevel@tonic-gate /*
35337c478bd9Sstevel@tonic-gate  * Routine to expose supported HAT features to platform independent code.
35347c478bd9Sstevel@tonic-gate  */
35357c478bd9Sstevel@tonic-gate /*ARGSUSED*/
35367c478bd9Sstevel@tonic-gate int
35377c478bd9Sstevel@tonic-gate hat_supported(enum hat_features feature, void *arg)
35387c478bd9Sstevel@tonic-gate {
35397c478bd9Sstevel@tonic-gate 	switch (feature) {
35407c478bd9Sstevel@tonic-gate 
35417c478bd9Sstevel@tonic-gate 	case HAT_SHARED_PT:	/* this is really ISM */
35427c478bd9Sstevel@tonic-gate 		return (1);
35437c478bd9Sstevel@tonic-gate 
35447c478bd9Sstevel@tonic-gate 	case HAT_DYNAMIC_ISM_UNMAP:
35457c478bd9Sstevel@tonic-gate 		return (0);
35467c478bd9Sstevel@tonic-gate 
35477c478bd9Sstevel@tonic-gate 	case HAT_VMODSORT:
35487c478bd9Sstevel@tonic-gate 		return (1);
35497c478bd9Sstevel@tonic-gate 
35507c478bd9Sstevel@tonic-gate 	default:
35517c478bd9Sstevel@tonic-gate 		panic("hat_supported() - unknown feature");
35527c478bd9Sstevel@tonic-gate 	}
35537c478bd9Sstevel@tonic-gate 	return (0);
35547c478bd9Sstevel@tonic-gate }
35557c478bd9Sstevel@tonic-gate 
35567c478bd9Sstevel@tonic-gate /*
35577c478bd9Sstevel@tonic-gate  * Called when a thread is exiting and has been switched to the kernel AS
35587c478bd9Sstevel@tonic-gate  */
35597c478bd9Sstevel@tonic-gate void
35607c478bd9Sstevel@tonic-gate hat_thread_exit(kthread_t *thd)
35617c478bd9Sstevel@tonic-gate {
35627c478bd9Sstevel@tonic-gate 	ASSERT(thd->t_procp->p_as == &kas);
35637c478bd9Sstevel@tonic-gate 	hat_switch(thd->t_procp->p_as->a_hat);
35647c478bd9Sstevel@tonic-gate }
35657c478bd9Sstevel@tonic-gate 
35667c478bd9Sstevel@tonic-gate /*
35677c478bd9Sstevel@tonic-gate  * Setup the given brand new hat structure as the new HAT on this cpu's mmu.
35687c478bd9Sstevel@tonic-gate  */
35697c478bd9Sstevel@tonic-gate /*ARGSUSED*/
35707c478bd9Sstevel@tonic-gate void
35717c478bd9Sstevel@tonic-gate hat_setup(hat_t *hat, int flags)
35727c478bd9Sstevel@tonic-gate {
35737c478bd9Sstevel@tonic-gate 	kpreempt_disable();
35747c478bd9Sstevel@tonic-gate 
35757c478bd9Sstevel@tonic-gate 	hat_switch(hat);
35767c478bd9Sstevel@tonic-gate 
35777c478bd9Sstevel@tonic-gate 	kpreempt_enable();
35787c478bd9Sstevel@tonic-gate }
35797c478bd9Sstevel@tonic-gate 
35807c478bd9Sstevel@tonic-gate /*
35817c478bd9Sstevel@tonic-gate  * Prepare for a CPU private mapping for the given address.
35827c478bd9Sstevel@tonic-gate  *
35837c478bd9Sstevel@tonic-gate  * The address can only be used from a single CPU and can be remapped
35847c478bd9Sstevel@tonic-gate  * using hat_mempte_remap().  Return the address of the PTE.
35857c478bd9Sstevel@tonic-gate  *
35867c478bd9Sstevel@tonic-gate  * We do the htable_create() if necessary and increment the valid count so
35877c478bd9Sstevel@tonic-gate  * the htable can't disappear.  We also hat_devload() the page table into
35887c478bd9Sstevel@tonic-gate  * kernel so that the PTE is quickly accessed.
35897c478bd9Sstevel@tonic-gate  */
3590ae115bc7Smrj hat_mempte_t
3591ae115bc7Smrj hat_mempte_setup(caddr_t addr)
35927c478bd9Sstevel@tonic-gate {
35937c478bd9Sstevel@tonic-gate 	uintptr_t	va = (uintptr_t)addr;
35947c478bd9Sstevel@tonic-gate 	htable_t	*ht;
35957c478bd9Sstevel@tonic-gate 	uint_t		entry;
35967c478bd9Sstevel@tonic-gate 	x86pte_t	oldpte;
3597ae115bc7Smrj 	hat_mempte_t	p;
35987c478bd9Sstevel@tonic-gate 
35997c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(va));
36007c478bd9Sstevel@tonic-gate 	ASSERT(!IN_VA_HOLE(va));
3601aac11643Sjosephb 	++curthread->t_hatdepth;
36027c478bd9Sstevel@tonic-gate 	ht = htable_getpte(kas.a_hat, va, &entry, &oldpte, 0);
36037c478bd9Sstevel@tonic-gate 	if (ht == NULL) {
36047c478bd9Sstevel@tonic-gate 		ht = htable_create(kas.a_hat, va, 0, NULL);
36057c478bd9Sstevel@tonic-gate 		entry = htable_va2entry(va, ht);
36067c478bd9Sstevel@tonic-gate 		ASSERT(ht->ht_level == 0);
36077c478bd9Sstevel@tonic-gate 		oldpte = x86pte_get(ht, entry);
36087c478bd9Sstevel@tonic-gate 	}
36097c478bd9Sstevel@tonic-gate 	if (PTE_ISVALID(oldpte))
36107c478bd9Sstevel@tonic-gate 		panic("hat_mempte_setup(): address already mapped"
36117c478bd9Sstevel@tonic-gate 		    "ht=%p, entry=%d, pte=" FMT_PTE, ht, entry, oldpte);
36127c478bd9Sstevel@tonic-gate 
36137c478bd9Sstevel@tonic-gate 	/*
36147c478bd9Sstevel@tonic-gate 	 * increment ht_valid_cnt so that the pagetable can't disappear
36157c478bd9Sstevel@tonic-gate 	 */
36167c478bd9Sstevel@tonic-gate 	HTABLE_INC(ht->ht_valid_cnt);
36177c478bd9Sstevel@tonic-gate 
36187c478bd9Sstevel@tonic-gate 	/*
3619ae115bc7Smrj 	 * return the PTE physical address to the caller.
36207c478bd9Sstevel@tonic-gate 	 */
36217c478bd9Sstevel@tonic-gate 	htable_release(ht);
3622ae115bc7Smrj 	p = PT_INDEX_PHYSADDR(pfn_to_pa(ht->ht_pfn), entry);
3623aac11643Sjosephb 	--curthread->t_hatdepth;
3624ae115bc7Smrj 	return (p);
36257c478bd9Sstevel@tonic-gate }
36267c478bd9Sstevel@tonic-gate 
36277c478bd9Sstevel@tonic-gate /*
36287c478bd9Sstevel@tonic-gate  * Release a CPU private mapping for the given address.
36297c478bd9Sstevel@tonic-gate  * We decrement the htable valid count so it might be destroyed.
36307c478bd9Sstevel@tonic-gate  */
3631ae115bc7Smrj /*ARGSUSED1*/
36327c478bd9Sstevel@tonic-gate void
3633ae115bc7Smrj hat_mempte_release(caddr_t addr, hat_mempte_t pte_pa)
36347c478bd9Sstevel@tonic-gate {
36357c478bd9Sstevel@tonic-gate 	htable_t	*ht;
36367c478bd9Sstevel@tonic-gate 
36377c478bd9Sstevel@tonic-gate 	/*
3638ae115bc7Smrj 	 * invalidate any left over mapping and decrement the htable valid count
36397c478bd9Sstevel@tonic-gate 	 */
3640ae115bc7Smrj 	{
3641ae115bc7Smrj 		x86pte_t *pteptr;
3642ae115bc7Smrj 
3643ae115bc7Smrj 		pteptr = x86pte_mapin(mmu_btop(pte_pa),
3644ae115bc7Smrj 		    (pte_pa & MMU_PAGEOFFSET) >> mmu.pte_size_shift, NULL);
3645ae115bc7Smrj 		if (mmu.pae_hat)
3646ae115bc7Smrj 			*pteptr = 0;
3647ae115bc7Smrj 		else
3648ae115bc7Smrj 			*(x86pte32_t *)pteptr = 0;
3649ae115bc7Smrj 		mmu_tlbflush_entry(addr);
3650ae115bc7Smrj 		x86pte_mapout();
3651ae115bc7Smrj 	}
3652ae115bc7Smrj 
36537c478bd9Sstevel@tonic-gate 	ht = htable_getpte(kas.a_hat, ALIGN2PAGE(addr), NULL, NULL, 0);
36547c478bd9Sstevel@tonic-gate 	if (ht == NULL)
36557c478bd9Sstevel@tonic-gate 		panic("hat_mempte_release(): invalid address");
36567c478bd9Sstevel@tonic-gate 	ASSERT(ht->ht_level == 0);
36577c478bd9Sstevel@tonic-gate 	HTABLE_DEC(ht->ht_valid_cnt);
36587c478bd9Sstevel@tonic-gate 	htable_release(ht);
36597c478bd9Sstevel@tonic-gate }
36607c478bd9Sstevel@tonic-gate 
36617c478bd9Sstevel@tonic-gate /*
36627c478bd9Sstevel@tonic-gate  * Apply a temporary CPU private mapping to a page. We flush the TLB only
36637c478bd9Sstevel@tonic-gate  * on this CPU, so this ought to have been called with preemption disabled.
36647c478bd9Sstevel@tonic-gate  */
36657c478bd9Sstevel@tonic-gate void
36667c478bd9Sstevel@tonic-gate hat_mempte_remap(
3667ae115bc7Smrj 	pfn_t		pfn,
3668ae115bc7Smrj 	caddr_t		addr,
3669ae115bc7Smrj 	hat_mempte_t	pte_pa,
3670ae115bc7Smrj 	uint_t		attr,
3671ae115bc7Smrj 	uint_t		flags)
36727c478bd9Sstevel@tonic-gate {
36737c478bd9Sstevel@tonic-gate 	uintptr_t	va = (uintptr_t)addr;
36747c478bd9Sstevel@tonic-gate 	x86pte_t	pte;
36757c478bd9Sstevel@tonic-gate 
36767c478bd9Sstevel@tonic-gate 	/*
36777c478bd9Sstevel@tonic-gate 	 * Remap the given PTE to the new page's PFN. Invalidate only
36787c478bd9Sstevel@tonic-gate 	 * on this CPU.
36797c478bd9Sstevel@tonic-gate 	 */
36807c478bd9Sstevel@tonic-gate #ifdef DEBUG
36817c478bd9Sstevel@tonic-gate 	htable_t	*ht;
36827c478bd9Sstevel@tonic-gate 	uint_t		entry;
36837c478bd9Sstevel@tonic-gate 
36847c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(va));
36857c478bd9Sstevel@tonic-gate 	ASSERT(!IN_VA_HOLE(va));
36867c478bd9Sstevel@tonic-gate 	ht = htable_getpte(kas.a_hat, va, &entry, NULL, 0);
36877c478bd9Sstevel@tonic-gate 	ASSERT(ht != NULL);
36887c478bd9Sstevel@tonic-gate 	ASSERT(ht->ht_level == 0);
36897c478bd9Sstevel@tonic-gate 	ASSERT(ht->ht_valid_cnt > 0);
3690ae115bc7Smrj 	ASSERT(ht->ht_pfn == mmu_btop(pte_pa));
36917c478bd9Sstevel@tonic-gate 	htable_release(ht);
36927c478bd9Sstevel@tonic-gate #endif
36937c478bd9Sstevel@tonic-gate 	pte = hati_mkpte(pfn, attr, 0, flags);
3694ae115bc7Smrj 	{
3695ae115bc7Smrj 		x86pte_t *pteptr;
3696ae115bc7Smrj 
3697ae115bc7Smrj 		pteptr = x86pte_mapin(mmu_btop(pte_pa),
3698ae115bc7Smrj 		    (pte_pa & MMU_PAGEOFFSET) >> mmu.pte_size_shift, NULL);
3699ae115bc7Smrj 		if (mmu.pae_hat)
3700ae115bc7Smrj 			*(x86pte_t *)pteptr = pte;
3701ae115bc7Smrj 		else
3702ae115bc7Smrj 			*(x86pte32_t *)pteptr = (x86pte32_t)pte;
3703ae115bc7Smrj 		mmu_tlbflush_entry(addr);
3704ae115bc7Smrj 		x86pte_mapout();
3705ae115bc7Smrj 	}
37067c478bd9Sstevel@tonic-gate }
37077c478bd9Sstevel@tonic-gate 
37087c478bd9Sstevel@tonic-gate 
37097c478bd9Sstevel@tonic-gate 
37107c478bd9Sstevel@tonic-gate /*
37117c478bd9Sstevel@tonic-gate  * Hat locking functions
37127c478bd9Sstevel@tonic-gate  * XXX - these two functions are currently being used by hatstats
37137c478bd9Sstevel@tonic-gate  * 	they can be removed by using a per-as mutex for hatstats.
37147c478bd9Sstevel@tonic-gate  */
37157c478bd9Sstevel@tonic-gate void
37167c478bd9Sstevel@tonic-gate hat_enter(hat_t *hat)
37177c478bd9Sstevel@tonic-gate {
37187c478bd9Sstevel@tonic-gate 	mutex_enter(&hat->hat_mutex);
37197c478bd9Sstevel@tonic-gate }
37207c478bd9Sstevel@tonic-gate 
37217c478bd9Sstevel@tonic-gate void
37227c478bd9Sstevel@tonic-gate hat_exit(hat_t *hat)
37237c478bd9Sstevel@tonic-gate {
37247c478bd9Sstevel@tonic-gate 	mutex_exit(&hat->hat_mutex);
37257c478bd9Sstevel@tonic-gate }
37267c478bd9Sstevel@tonic-gate 
37277c478bd9Sstevel@tonic-gate /*
3728ae115bc7Smrj  * HAT part of cpu initialization.
37297c478bd9Sstevel@tonic-gate  */
37307c478bd9Sstevel@tonic-gate void
3731ae115bc7Smrj hat_cpu_online(struct cpu *cpup)
37327c478bd9Sstevel@tonic-gate {
3733ae115bc7Smrj 	if (cpup != CPU) {
3734ae115bc7Smrj 		x86pte_cpu_init(cpup);
3735ae115bc7Smrj 		hat_vlp_setup(cpup);
37367c478bd9Sstevel@tonic-gate 	}
3737ae115bc7Smrj 	CPUSET_ATOMIC_ADD(khat_cpuset, cpup->cpu_id);
37387c478bd9Sstevel@tonic-gate }
37397c478bd9Sstevel@tonic-gate 
37407c478bd9Sstevel@tonic-gate /*
3741ae115bc7Smrj  * HAT part of cpu deletion.
3742ae115bc7Smrj  * (currently, we only call this after the cpu is safely passivated.)
37437c478bd9Sstevel@tonic-gate  */
37447c478bd9Sstevel@tonic-gate void
3745ae115bc7Smrj hat_cpu_offline(struct cpu *cpup)
37467c478bd9Sstevel@tonic-gate {
3747ae115bc7Smrj 	ASSERT(cpup != CPU);
3748ae115bc7Smrj 
3749ae115bc7Smrj 	CPUSET_ATOMIC_DEL(khat_cpuset, cpup->cpu_id);
3750ae115bc7Smrj 	x86pte_cpu_fini(cpup);
3751ae115bc7Smrj 	hat_vlp_teardown(cpup);
37527c478bd9Sstevel@tonic-gate }
37537c478bd9Sstevel@tonic-gate 
37547c478bd9Sstevel@tonic-gate /*
37557c478bd9Sstevel@tonic-gate  * Function called after all CPUs are brought online.
37567c478bd9Sstevel@tonic-gate  * Used to remove low address boot mappings.
37577c478bd9Sstevel@tonic-gate  */
37587c478bd9Sstevel@tonic-gate void
37597c478bd9Sstevel@tonic-gate clear_boot_mappings(uintptr_t low, uintptr_t high)
37607c478bd9Sstevel@tonic-gate {
37617c478bd9Sstevel@tonic-gate 	uintptr_t vaddr = low;
37627c478bd9Sstevel@tonic-gate 	htable_t *ht = NULL;
37637c478bd9Sstevel@tonic-gate 	level_t level;
37647c478bd9Sstevel@tonic-gate 	uint_t entry;
37657c478bd9Sstevel@tonic-gate 	x86pte_t pte;
37667c478bd9Sstevel@tonic-gate 
37677c478bd9Sstevel@tonic-gate 	/*
37687c478bd9Sstevel@tonic-gate 	 * On 1st CPU we can unload the prom mappings, basically we blow away
3769ae115bc7Smrj 	 * all virtual mappings under _userlimit.
37707c478bd9Sstevel@tonic-gate 	 */
37717c478bd9Sstevel@tonic-gate 	while (vaddr < high) {
37727c478bd9Sstevel@tonic-gate 		pte = htable_walk(kas.a_hat, &ht, &vaddr, high);
37737c478bd9Sstevel@tonic-gate 		if (ht == NULL)
37747c478bd9Sstevel@tonic-gate 			break;
37757c478bd9Sstevel@tonic-gate 
37767c478bd9Sstevel@tonic-gate 		level = ht->ht_level;
37777c478bd9Sstevel@tonic-gate 		entry = htable_va2entry(vaddr, ht);
37787c478bd9Sstevel@tonic-gate 		ASSERT(level <= mmu.max_page_level);
37797c478bd9Sstevel@tonic-gate 		ASSERT(PTE_ISPAGE(pte, level));
37807c478bd9Sstevel@tonic-gate 
37817c478bd9Sstevel@tonic-gate 		/*
37827c478bd9Sstevel@tonic-gate 		 * Unload the mapping from the page tables.
37837c478bd9Sstevel@tonic-gate 		 */
3784ae115bc7Smrj 		(void) x86pte_inval(ht, entry, 0, NULL);
37857c478bd9Sstevel@tonic-gate 		ASSERT(ht->ht_valid_cnt > 0);
37867c478bd9Sstevel@tonic-gate 		HTABLE_DEC(ht->ht_valid_cnt);
37877c478bd9Sstevel@tonic-gate 		PGCNT_DEC(ht->ht_hat, ht->ht_level);
37887c478bd9Sstevel@tonic-gate 
37897c478bd9Sstevel@tonic-gate 		vaddr += LEVEL_SIZE(ht->ht_level);
37907c478bd9Sstevel@tonic-gate 	}
37917c478bd9Sstevel@tonic-gate 	if (ht)
37927c478bd9Sstevel@tonic-gate 		htable_release(ht);
37937c478bd9Sstevel@tonic-gate }
37947c478bd9Sstevel@tonic-gate 
37957c478bd9Sstevel@tonic-gate /*
37967c478bd9Sstevel@tonic-gate  * Atomically update a new translation for a single page.  If the
37977c478bd9Sstevel@tonic-gate  * currently installed PTE doesn't match the value we expect to find,
37987c478bd9Sstevel@tonic-gate  * it's not updated and we return the PTE we found.
37997c478bd9Sstevel@tonic-gate  *
38007c478bd9Sstevel@tonic-gate  * If activating nosync or NOWRITE and the page was modified we need to sync
38017c478bd9Sstevel@tonic-gate  * with the page_t. Also sync with page_t if clearing ref/mod bits.
38027c478bd9Sstevel@tonic-gate  */
38037c478bd9Sstevel@tonic-gate static x86pte_t
38047c478bd9Sstevel@tonic-gate hati_update_pte(htable_t *ht, uint_t entry, x86pte_t expected, x86pte_t new)
38057c478bd9Sstevel@tonic-gate {
38067c478bd9Sstevel@tonic-gate 	page_t		*pp;
38077c478bd9Sstevel@tonic-gate 	uint_t		rm = 0;
38087c478bd9Sstevel@tonic-gate 	x86pte_t	replaced;
38097c478bd9Sstevel@tonic-gate 
3810ae115bc7Smrj 	if (PTE_GET(expected, PT_SOFTWARE) < PT_NOSYNC &&
38117c478bd9Sstevel@tonic-gate 	    PTE_GET(expected, PT_MOD | PT_REF) &&
38127c478bd9Sstevel@tonic-gate 	    (PTE_GET(new, PT_NOSYNC) || !PTE_GET(new, PT_WRITABLE) ||
3813*250b7ff9Sjosephb 	    !PTE_GET(new, PT_MOD | PT_REF))) {
38147c478bd9Sstevel@tonic-gate 
3815ae115bc7Smrj 		ASSERT(!pfn_is_foreign(PTE2PFN(expected, ht->ht_level)));
38167c478bd9Sstevel@tonic-gate 		pp = page_numtopp_nolock(PTE2PFN(expected, ht->ht_level));
38177c478bd9Sstevel@tonic-gate 		ASSERT(pp != NULL);
38187c478bd9Sstevel@tonic-gate 		if (PTE_GET(expected, PT_MOD))
38197c478bd9Sstevel@tonic-gate 			rm |= P_MOD;
38207c478bd9Sstevel@tonic-gate 		if (PTE_GET(expected, PT_REF))
38217c478bd9Sstevel@tonic-gate 			rm |= P_REF;
38227c478bd9Sstevel@tonic-gate 		PTE_CLR(new, PT_MOD | PT_REF);
38237c478bd9Sstevel@tonic-gate 	}
38247c478bd9Sstevel@tonic-gate 
38257c478bd9Sstevel@tonic-gate 	replaced = x86pte_update(ht, entry, expected, new);
38267c478bd9Sstevel@tonic-gate 	if (replaced != expected)
38277c478bd9Sstevel@tonic-gate 		return (replaced);
38287c478bd9Sstevel@tonic-gate 
38297c478bd9Sstevel@tonic-gate 	if (rm) {
38307c478bd9Sstevel@tonic-gate 		/*
38317c478bd9Sstevel@tonic-gate 		 * sync to all constituent pages of a large page
38327c478bd9Sstevel@tonic-gate 		 */
38337c478bd9Sstevel@tonic-gate 		pgcnt_t pgcnt = page_get_pagecnt(ht->ht_level);
38347c478bd9Sstevel@tonic-gate 		ASSERT(IS_P2ALIGNED(pp->p_pagenum, pgcnt));
38357c478bd9Sstevel@tonic-gate 		while (pgcnt-- > 0) {
38367c478bd9Sstevel@tonic-gate 			/*
38377c478bd9Sstevel@tonic-gate 			 * hat_page_demote() can't decrease
38387c478bd9Sstevel@tonic-gate 			 * pszc below this mapping size
38397c478bd9Sstevel@tonic-gate 			 * since large mapping existed after we
38407c478bd9Sstevel@tonic-gate 			 * took mlist lock.
38417c478bd9Sstevel@tonic-gate 			 */
38427c478bd9Sstevel@tonic-gate 			ASSERT(pp->p_szc >= ht->ht_level);
38437c478bd9Sstevel@tonic-gate 			hat_page_setattr(pp, rm);
38447c478bd9Sstevel@tonic-gate 			++pp;
38457c478bd9Sstevel@tonic-gate 		}
38467c478bd9Sstevel@tonic-gate 	}
38477c478bd9Sstevel@tonic-gate 
38487c478bd9Sstevel@tonic-gate 	return (0);
38497c478bd9Sstevel@tonic-gate }
38507c478bd9Sstevel@tonic-gate 
38517c478bd9Sstevel@tonic-gate /*
38527c478bd9Sstevel@tonic-gate  * Kernel Physical Mapping (kpm) facility
38537c478bd9Sstevel@tonic-gate  *
38547c478bd9Sstevel@tonic-gate  * Most of the routines needed to support segkpm are almost no-ops on the
38557c478bd9Sstevel@tonic-gate  * x86 platform.  We map in the entire segment when it is created and leave
38567c478bd9Sstevel@tonic-gate  * it mapped in, so there is no additional work required to set up and tear
38577c478bd9Sstevel@tonic-gate  * down individual mappings.  All of these routines were created to support
38587c478bd9Sstevel@tonic-gate  * SPARC platforms that have to avoid aliasing in their virtually indexed
38597c478bd9Sstevel@tonic-gate  * caches.
38607c478bd9Sstevel@tonic-gate  *
38617c478bd9Sstevel@tonic-gate  * Most of the routines have sanity checks in them (e.g. verifying that the
38627c478bd9Sstevel@tonic-gate  * passed-in page is locked).  We don't actually care about most of these
38637c478bd9Sstevel@tonic-gate  * checks on x86, but we leave them in place to identify problems in the
38647c478bd9Sstevel@tonic-gate  * upper levels.
38657c478bd9Sstevel@tonic-gate  */
38667c478bd9Sstevel@tonic-gate 
38677c478bd9Sstevel@tonic-gate /*
38687c478bd9Sstevel@tonic-gate  * Map in a locked page and return the vaddr.
38697c478bd9Sstevel@tonic-gate  */
38707c478bd9Sstevel@tonic-gate /*ARGSUSED*/
38717c478bd9Sstevel@tonic-gate caddr_t
38727c478bd9Sstevel@tonic-gate hat_kpm_mapin(struct page *pp, struct kpme *kpme)
38737c478bd9Sstevel@tonic-gate {
38747c478bd9Sstevel@tonic-gate 	caddr_t		vaddr;
38757c478bd9Sstevel@tonic-gate 
38767c478bd9Sstevel@tonic-gate #ifdef DEBUG
38777c478bd9Sstevel@tonic-gate 	if (kpm_enable == 0) {
38787c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "hat_kpm_mapin: kpm_enable not set\n");
38797c478bd9Sstevel@tonic-gate 		return ((caddr_t)NULL);
38807c478bd9Sstevel@tonic-gate 	}
38817c478bd9Sstevel@tonic-gate 
38827c478bd9Sstevel@tonic-gate 	if (pp == NULL || PAGE_LOCKED(pp) == 0) {
38837c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "hat_kpm_mapin: pp zero or not locked\n");
38847c478bd9Sstevel@tonic-gate 		return ((caddr_t)NULL);
38857c478bd9Sstevel@tonic-gate 	}
38867c478bd9Sstevel@tonic-gate #endif
38877c478bd9Sstevel@tonic-gate 
38887c478bd9Sstevel@tonic-gate 	vaddr = hat_kpm_page2va(pp, 1);
38897c478bd9Sstevel@tonic-gate 
38907c478bd9Sstevel@tonic-gate 	return (vaddr);
38917c478bd9Sstevel@tonic-gate }
38927c478bd9Sstevel@tonic-gate 
38937c478bd9Sstevel@tonic-gate /*
38947c478bd9Sstevel@tonic-gate  * Mapout a locked page.
38957c478bd9Sstevel@tonic-gate  */
38967c478bd9Sstevel@tonic-gate /*ARGSUSED*/
38977c478bd9Sstevel@tonic-gate void
38987c478bd9Sstevel@tonic-gate hat_kpm_mapout(struct page *pp, struct kpme *kpme, caddr_t vaddr)
38997c478bd9Sstevel@tonic-gate {
39007c478bd9Sstevel@tonic-gate #ifdef DEBUG
39017c478bd9Sstevel@tonic-gate 	if (kpm_enable == 0) {
39027c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "hat_kpm_mapout: kpm_enable not set\n");
39037c478bd9Sstevel@tonic-gate 		return;
39047c478bd9Sstevel@tonic-gate 	}
39057c478bd9Sstevel@tonic-gate 
39067c478bd9Sstevel@tonic-gate 	if (IS_KPM_ADDR(vaddr) == 0) {
39077c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "hat_kpm_mapout: no kpm address\n");
39087c478bd9Sstevel@tonic-gate 		return;
39097c478bd9Sstevel@tonic-gate 	}
39107c478bd9Sstevel@tonic-gate 
39117c478bd9Sstevel@tonic-gate 	if (pp == NULL || PAGE_LOCKED(pp) == 0) {
39127c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "hat_kpm_mapout: page zero or not locked\n");
39137c478bd9Sstevel@tonic-gate 		return;
39147c478bd9Sstevel@tonic-gate 	}
39157c478bd9Sstevel@tonic-gate #endif
39167c478bd9Sstevel@tonic-gate }
39177c478bd9Sstevel@tonic-gate 
39187c478bd9Sstevel@tonic-gate /*
39197c478bd9Sstevel@tonic-gate  * Return the kpm virtual address for a specific pfn
39207c478bd9Sstevel@tonic-gate  */
39217c478bd9Sstevel@tonic-gate caddr_t
39227c478bd9Sstevel@tonic-gate hat_kpm_pfn2va(pfn_t pfn)
39237c478bd9Sstevel@tonic-gate {
3924ae115bc7Smrj 	uintptr_t vaddr = (uintptr_t)kpm_vbase + mmu_ptob(pfn);
39257c478bd9Sstevel@tonic-gate 
39267c478bd9Sstevel@tonic-gate 	return ((caddr_t)vaddr);
39277c478bd9Sstevel@tonic-gate }
39287c478bd9Sstevel@tonic-gate 
39297c478bd9Sstevel@tonic-gate /*
39307c478bd9Sstevel@tonic-gate  * Return the kpm virtual address for the page at pp.
39317c478bd9Sstevel@tonic-gate  */
39327c478bd9Sstevel@tonic-gate /*ARGSUSED*/
39337c478bd9Sstevel@tonic-gate caddr_t
39347c478bd9Sstevel@tonic-gate hat_kpm_page2va(struct page *pp, int checkswap)
39357c478bd9Sstevel@tonic-gate {
39367c478bd9Sstevel@tonic-gate 	return (hat_kpm_pfn2va(pp->p_pagenum));
39377c478bd9Sstevel@tonic-gate }
39387c478bd9Sstevel@tonic-gate 
39397c478bd9Sstevel@tonic-gate /*
39407c478bd9Sstevel@tonic-gate  * Return the page frame number for the kpm virtual address vaddr.
39417c478bd9Sstevel@tonic-gate  */
39427c478bd9Sstevel@tonic-gate pfn_t
39437c478bd9Sstevel@tonic-gate hat_kpm_va2pfn(caddr_t vaddr)
39447c478bd9Sstevel@tonic-gate {
39457c478bd9Sstevel@tonic-gate 	pfn_t		pfn;
39467c478bd9Sstevel@tonic-gate 
39477c478bd9Sstevel@tonic-gate 	ASSERT(IS_KPM_ADDR(vaddr));
39487c478bd9Sstevel@tonic-gate 
39497c478bd9Sstevel@tonic-gate 	pfn = (pfn_t)btop(vaddr - kpm_vbase);
39507c478bd9Sstevel@tonic-gate 
39517c478bd9Sstevel@tonic-gate 	return (pfn);
39527c478bd9Sstevel@tonic-gate }
39537c478bd9Sstevel@tonic-gate 
39547c478bd9Sstevel@tonic-gate 
39557c478bd9Sstevel@tonic-gate /*
39567c478bd9Sstevel@tonic-gate  * Return the page for the kpm virtual address vaddr.
39577c478bd9Sstevel@tonic-gate  */
39587c478bd9Sstevel@tonic-gate page_t *
39597c478bd9Sstevel@tonic-gate hat_kpm_vaddr2page(caddr_t vaddr)
39607c478bd9Sstevel@tonic-gate {
39617c478bd9Sstevel@tonic-gate 	pfn_t		pfn;
39627c478bd9Sstevel@tonic-gate 
39637c478bd9Sstevel@tonic-gate 	ASSERT(IS_KPM_ADDR(vaddr));
39647c478bd9Sstevel@tonic-gate 
39657c478bd9Sstevel@tonic-gate 	pfn = hat_kpm_va2pfn(vaddr);
39667c478bd9Sstevel@tonic-gate 
39677c478bd9Sstevel@tonic-gate 	return (page_numtopp_nolock(pfn));
39687c478bd9Sstevel@tonic-gate }
39697c478bd9Sstevel@tonic-gate 
39707c478bd9Sstevel@tonic-gate /*
39717c478bd9Sstevel@tonic-gate  * hat_kpm_fault is called from segkpm_fault when we take a page fault on a
39727c478bd9Sstevel@tonic-gate  * KPM page.  This should never happen on x86
39737c478bd9Sstevel@tonic-gate  */
39747c478bd9Sstevel@tonic-gate int
39757c478bd9Sstevel@tonic-gate hat_kpm_fault(hat_t *hat, caddr_t vaddr)
39767c478bd9Sstevel@tonic-gate {
39777c478bd9Sstevel@tonic-gate 	panic("pagefault in seg_kpm.  hat: 0x%p  vaddr: 0x%p", hat, vaddr);
39787c478bd9Sstevel@tonic-gate 
39797c478bd9Sstevel@tonic-gate 	return (0);
39807c478bd9Sstevel@tonic-gate }
39817c478bd9Sstevel@tonic-gate 
39827c478bd9Sstevel@tonic-gate /*ARGSUSED*/
39837c478bd9Sstevel@tonic-gate void
39847c478bd9Sstevel@tonic-gate hat_kpm_mseghash_clear(int nentries)
39857c478bd9Sstevel@tonic-gate {}
39867c478bd9Sstevel@tonic-gate 
39877c478bd9Sstevel@tonic-gate /*ARGSUSED*/
39887c478bd9Sstevel@tonic-gate void
39897c478bd9Sstevel@tonic-gate hat_kpm_mseghash_update(pgcnt_t inx, struct memseg *msp)
39907c478bd9Sstevel@tonic-gate {}
3991