xref: /illumos-gate/usr/src/uts/i86pc/vm/hat_i86.c (revision a31148363f598def767ac48c5d82e1572e44b935)
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 /*
2225540de2SJakub Jermar  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
25*a3114836SGerry Liu /*
26*a3114836SGerry Liu  * Copyright (c) 2010, Intel Corporation.
27*a3114836SGerry Liu  * All rights reserved.
28*a3114836SGerry Liu  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * VM - Hardware Address Translation management for i386 and amd64
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * Implementation of the interfaces described in <common/vm/hat.h>
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * Nearly all the details of how the hardware is managed should not be
377c478bd9Sstevel@tonic-gate  * visible outside this layer except for misc. machine specific functions
387c478bd9Sstevel@tonic-gate  * that work in conjunction with this code.
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * Routines used only inside of i86pc/vm start with hati_ for HAT Internal.
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include <sys/machparam.h>
447c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
457c478bd9Sstevel@tonic-gate #include <sys/mman.h>
467c478bd9Sstevel@tonic-gate #include <sys/types.h>
477c478bd9Sstevel@tonic-gate #include <sys/systm.h>
487c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
497c478bd9Sstevel@tonic-gate #include <sys/thread.h>
507c478bd9Sstevel@tonic-gate #include <sys/proc.h>
517c478bd9Sstevel@tonic-gate #include <sys/cpu.h>
527c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
537c478bd9Sstevel@tonic-gate #include <sys/disp.h>
547c478bd9Sstevel@tonic-gate #include <sys/shm.h>
557c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
567c478bd9Sstevel@tonic-gate #include <sys/machparam.h>
577c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
587c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
597c478bd9Sstevel@tonic-gate #include <sys/promif.h>
607c478bd9Sstevel@tonic-gate #include <sys/var.h>
617c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
627c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
637c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
64ae115bc7Smrj #include <sys/controlregs.h>
65ae115bc7Smrj #include <sys/bootconf.h>
66ae115bc7Smrj #include <sys/bootsvcs.h>
67ae115bc7Smrj #include <sys/bootinfo.h>
6895c0a3c8Sjosephb #include <sys/archsystm.h>
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
717c478bd9Sstevel@tonic-gate #include <vm/hat_i86.h>
727c478bd9Sstevel@tonic-gate #include <vm/as.h>
737c478bd9Sstevel@tonic-gate #include <vm/seg.h>
747c478bd9Sstevel@tonic-gate #include <vm/page.h>
757c478bd9Sstevel@tonic-gate #include <vm/seg_kp.h>
767c478bd9Sstevel@tonic-gate #include <vm/seg_kpm.h>
777c478bd9Sstevel@tonic-gate #include <vm/vm_dep.h>
78843e1988Sjohnlev #ifdef __xpv
79843e1988Sjohnlev #include <sys/hypervisor.h>
80843e1988Sjohnlev #endif
81ae115bc7Smrj #include <vm/kboot_mmu.h>
82250b7ff9Sjosephb #include <vm/seg_spt.h>
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  * Basic parameters for hat operation.
887c478bd9Sstevel@tonic-gate  */
897c478bd9Sstevel@tonic-gate struct hat_mmu_info mmu;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate  * The page that is the kernel's top level pagetable.
937c478bd9Sstevel@tonic-gate  *
94843e1988Sjohnlev  * For 32 bit PAE support on i86pc, the kernel hat will use the 1st 4 entries
957c478bd9Sstevel@tonic-gate  * on this 4K page for its top level page table. The remaining groups of
967c478bd9Sstevel@tonic-gate  * 4 entries are used for per processor copies of user VLP pagetables for
977c478bd9Sstevel@tonic-gate  * running threads.  See hat_switch() and reload_pae32() for details.
987c478bd9Sstevel@tonic-gate  *
99843e1988Sjohnlev  * vlp_page[0..3] - level==2 PTEs for kernel HAT
100843e1988Sjohnlev  * vlp_page[4..7] - level==2 PTEs for user thread on cpu 0
101843e1988Sjohnlev  * vlp_page[8..11]  - level==2 PTE for user thread on cpu 1
102843e1988Sjohnlev  * etc...
1037c478bd9Sstevel@tonic-gate  */
1047c478bd9Sstevel@tonic-gate static x86pte_t *vlp_page;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  * forward declaration of internal utility routines
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate static x86pte_t hati_update_pte(htable_t *ht, uint_t entry, x86pte_t expected,
1107c478bd9Sstevel@tonic-gate 	x86pte_t new);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate /*
1137c478bd9Sstevel@tonic-gate  * The kernel address space exists in all HATs. To implement this the
114843e1988Sjohnlev  * kernel reserves a fixed number of entries in the topmost level(s) of page
115843e1988Sjohnlev  * tables. The values are setup during startup and then copied to every user
116843e1988Sjohnlev  * hat created by hat_alloc(). This means that kernelbase must be:
1177c478bd9Sstevel@tonic-gate  *
1187c478bd9Sstevel@tonic-gate  *	  4Meg aligned for 32 bit kernels
1197c478bd9Sstevel@tonic-gate  *	512Gig aligned for x86_64 64 bit kernel
1207c478bd9Sstevel@tonic-gate  *
121843e1988Sjohnlev  * The hat_kernel_range_ts describe what needs to be copied from kernel hat
122843e1988Sjohnlev  * to each user hat.
1237c478bd9Sstevel@tonic-gate  */
124843e1988Sjohnlev typedef struct hat_kernel_range {
125843e1988Sjohnlev 	level_t		hkr_level;
126843e1988Sjohnlev 	uintptr_t	hkr_start_va;
127843e1988Sjohnlev 	uintptr_t	hkr_end_va;	/* zero means to end of memory */
128843e1988Sjohnlev } hat_kernel_range_t;
129843e1988Sjohnlev #define	NUM_KERNEL_RANGE 2
130843e1988Sjohnlev static hat_kernel_range_t kernel_ranges[NUM_KERNEL_RANGE];
131843e1988Sjohnlev static int num_kernel_ranges;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate uint_t use_boot_reserve = 1;	/* cleared after early boot process */
1347c478bd9Sstevel@tonic-gate uint_t can_steal_post_boot = 0;	/* set late in boot to enable stealing */
1357c478bd9Sstevel@tonic-gate 
136512cf780Skchow /*
137512cf780Skchow  * enable_1gpg: controls 1g page support for user applications.
138512cf780Skchow  * By default, 1g pages are exported to user applications. enable_1gpg can
139512cf780Skchow  * be set to 0 to not export.
140512cf780Skchow  */
14178b03d3aSkchow int	enable_1gpg = 1;
14202bc52beSkchow 
143512cf780Skchow /*
144512cf780Skchow  * AMD shanghai processors provide better management of 1gb ptes in its tlb.
14521584dbcSPavel Tatashin  * By default, 1g page support will be disabled for pre-shanghai AMD
146512cf780Skchow  * processors that don't have optimal tlb support for the 1g page size.
147512cf780Skchow  * chk_optimal_1gtlb can be set to 0 to force 1g page support on sub-optimal
148512cf780Skchow  * processors.
149512cf780Skchow  */
150512cf780Skchow int	chk_optimal_1gtlb = 1;
151512cf780Skchow 
152512cf780Skchow 
15302bc52beSkchow #ifdef DEBUG
15402bc52beSkchow uint_t	map1gcnt;
15502bc52beSkchow #endif
15602bc52beSkchow 
15702bc52beSkchow 
1587c478bd9Sstevel@tonic-gate /*
1597c478bd9Sstevel@tonic-gate  * A cpuset for all cpus. This is used for kernel address cross calls, since
1607c478bd9Sstevel@tonic-gate  * the kernel addresses apply to all cpus.
1617c478bd9Sstevel@tonic-gate  */
1627c478bd9Sstevel@tonic-gate cpuset_t khat_cpuset;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate /*
1657c478bd9Sstevel@tonic-gate  * management stuff for hat structures
1667c478bd9Sstevel@tonic-gate  */
1677c478bd9Sstevel@tonic-gate kmutex_t	hat_list_lock;
1687c478bd9Sstevel@tonic-gate kcondvar_t	hat_list_cv;
1697c478bd9Sstevel@tonic-gate kmem_cache_t	*hat_cache;
1707c478bd9Sstevel@tonic-gate kmem_cache_t	*hat_hash_cache;
1717c478bd9Sstevel@tonic-gate kmem_cache_t	*vlp_hash_cache;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate /*
1747c478bd9Sstevel@tonic-gate  * Simple statistics
1757c478bd9Sstevel@tonic-gate  */
1767c478bd9Sstevel@tonic-gate struct hatstats hatstat;
1777c478bd9Sstevel@tonic-gate 
178ab4a9bebSjohnlev /*
179ab4a9bebSjohnlev  * Some earlier hypervisor versions do not emulate cmpxchg of PTEs
180ab4a9bebSjohnlev  * correctly.  For such hypervisors we must set PT_USER for kernel
181ab4a9bebSjohnlev  * entries ourselves (normally the emulation would set PT_USER for
182ab4a9bebSjohnlev  * kernel entries and PT_USER|PT_GLOBAL for user entries).  pt_kern is
183ab4a9bebSjohnlev  * thus set appropriately.  Note that dboot/kbm is OK, as only the full
184ab4a9bebSjohnlev  * HAT uses cmpxchg() and the other paths (hypercall etc.) were never
185ab4a9bebSjohnlev  * incorrect.
186ab4a9bebSjohnlev  */
187ab4a9bebSjohnlev int pt_kern;
188ab4a9bebSjohnlev 
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate  * useful stuff for atomic access/clearing/setting REF/MOD/RO bits in page_t's.
1917c478bd9Sstevel@tonic-gate  */
1927c478bd9Sstevel@tonic-gate extern void atomic_orb(uchar_t *addr, uchar_t val);
1937c478bd9Sstevel@tonic-gate extern void atomic_andb(uchar_t *addr, uchar_t val);
1947c478bd9Sstevel@tonic-gate 
195*a3114836SGerry Liu #ifndef __xpv
196*a3114836SGerry Liu extern pfn_t memseg_get_start(struct memseg *);
197*a3114836SGerry Liu #endif
198*a3114836SGerry Liu 
1997c478bd9Sstevel@tonic-gate #define	PP_GETRM(pp, rmmask)    (pp->p_nrm & rmmask)
2007c478bd9Sstevel@tonic-gate #define	PP_ISMOD(pp)		PP_GETRM(pp, P_MOD)
2017c478bd9Sstevel@tonic-gate #define	PP_ISREF(pp)		PP_GETRM(pp, P_REF)
2027c478bd9Sstevel@tonic-gate #define	PP_ISRO(pp)		PP_GETRM(pp, P_RO)
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate #define	PP_SETRM(pp, rm)	atomic_orb(&(pp->p_nrm), rm)
2057c478bd9Sstevel@tonic-gate #define	PP_SETMOD(pp)		PP_SETRM(pp, P_MOD)
2067c478bd9Sstevel@tonic-gate #define	PP_SETREF(pp)		PP_SETRM(pp, P_REF)
2077c478bd9Sstevel@tonic-gate #define	PP_SETRO(pp)		PP_SETRM(pp, P_RO)
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate #define	PP_CLRRM(pp, rm)	atomic_andb(&(pp->p_nrm), ~(rm))
2107c478bd9Sstevel@tonic-gate #define	PP_CLRMOD(pp)   	PP_CLRRM(pp, P_MOD)
2117c478bd9Sstevel@tonic-gate #define	PP_CLRREF(pp)   	PP_CLRRM(pp, P_REF)
2127c478bd9Sstevel@tonic-gate #define	PP_CLRRO(pp)    	PP_CLRRM(pp, P_RO)
2137c478bd9Sstevel@tonic-gate #define	PP_CLRALL(pp)		PP_CLRRM(pp, P_MOD | P_REF | P_RO)
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate  * kmem cache constructor for struct hat
2177c478bd9Sstevel@tonic-gate  */
2187c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2197c478bd9Sstevel@tonic-gate static int
2207c478bd9Sstevel@tonic-gate hati_constructor(void *buf, void *handle, int kmflags)
2217c478bd9Sstevel@tonic-gate {
2227c478bd9Sstevel@tonic-gate 	hat_t	*hat = buf;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	mutex_init(&hat->hat_mutex, NULL, MUTEX_DEFAULT, NULL);
2257c478bd9Sstevel@tonic-gate 	bzero(hat->hat_pages_mapped,
2267c478bd9Sstevel@tonic-gate 	    sizeof (pgcnt_t) * (mmu.max_page_level + 1));
227250b7ff9Sjosephb 	hat->hat_ism_pgcnt = 0;
2287c478bd9Sstevel@tonic-gate 	hat->hat_stats = 0;
2297c478bd9Sstevel@tonic-gate 	hat->hat_flags = 0;
2307c478bd9Sstevel@tonic-gate 	CPUSET_ZERO(hat->hat_cpus);
2317c478bd9Sstevel@tonic-gate 	hat->hat_htable = NULL;
2327c478bd9Sstevel@tonic-gate 	hat->hat_ht_hash = NULL;
2337c478bd9Sstevel@tonic-gate 	return (0);
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate /*
2377c478bd9Sstevel@tonic-gate  * Allocate a hat structure for as. We also create the top level
2387c478bd9Sstevel@tonic-gate  * htable and initialize it to contain the kernel hat entries.
2397c478bd9Sstevel@tonic-gate  */
2407c478bd9Sstevel@tonic-gate hat_t *
2417c478bd9Sstevel@tonic-gate hat_alloc(struct as *as)
2427c478bd9Sstevel@tonic-gate {
243843e1988Sjohnlev 	hat_t			*hat;
244843e1988Sjohnlev 	htable_t		*ht;	/* top level htable */
245843e1988Sjohnlev 	uint_t			use_vlp;
246843e1988Sjohnlev 	uint_t			r;
247843e1988Sjohnlev 	hat_kernel_range_t	*rp;
248843e1988Sjohnlev 	uintptr_t		va;
249843e1988Sjohnlev 	uintptr_t		eva;
250843e1988Sjohnlev 	uint_t			start;
251843e1988Sjohnlev 	uint_t			cnt;
252843e1988Sjohnlev 	htable_t		*src;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	/*
2557c478bd9Sstevel@tonic-gate 	 * Once we start creating user process HATs we can enable
2567c478bd9Sstevel@tonic-gate 	 * the htable_steal() code.
2577c478bd9Sstevel@tonic-gate 	 */
2587c478bd9Sstevel@tonic-gate 	if (can_steal_post_boot == 0)
2597c478bd9Sstevel@tonic-gate 		can_steal_post_boot = 1;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
2627c478bd9Sstevel@tonic-gate 	hat = kmem_cache_alloc(hat_cache, KM_SLEEP);
2637c478bd9Sstevel@tonic-gate 	hat->hat_as = as;
2647c478bd9Sstevel@tonic-gate 	mutex_init(&hat->hat_mutex, NULL, MUTEX_DEFAULT, NULL);
2657c478bd9Sstevel@tonic-gate 	ASSERT(hat->hat_flags == 0);
2667c478bd9Sstevel@tonic-gate 
267843e1988Sjohnlev #if defined(__xpv)
2687c478bd9Sstevel@tonic-gate 	/*
269843e1988Sjohnlev 	 * No VLP stuff on the hypervisor due to the 64-bit split top level
270843e1988Sjohnlev 	 * page tables.  On 32-bit it's not needed as the hypervisor takes
271843e1988Sjohnlev 	 * care of copying the top level PTEs to a below 4Gig page.
2727c478bd9Sstevel@tonic-gate 	 */
273843e1988Sjohnlev 	use_vlp = 0;
274843e1988Sjohnlev #else	/* __xpv */
275843e1988Sjohnlev 	/* 32 bit processes uses a VLP style hat when running with PAE */
2767c478bd9Sstevel@tonic-gate #if defined(__amd64)
2777c478bd9Sstevel@tonic-gate 	use_vlp = (ttoproc(curthread)->p_model == DATAMODEL_ILP32);
2787c478bd9Sstevel@tonic-gate #elif defined(__i386)
2797c478bd9Sstevel@tonic-gate 	use_vlp = mmu.pae_hat;
2807c478bd9Sstevel@tonic-gate #endif
281843e1988Sjohnlev #endif	/* __xpv */
2827c478bd9Sstevel@tonic-gate 	if (use_vlp) {
2837c478bd9Sstevel@tonic-gate 		hat->hat_flags = HAT_VLP;
2847c478bd9Sstevel@tonic-gate 		bzero(hat->hat_vlp_ptes, VLP_SIZE);
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	/*
2887c478bd9Sstevel@tonic-gate 	 * Allocate the htable hash
2897c478bd9Sstevel@tonic-gate 	 */
2907c478bd9Sstevel@tonic-gate 	if ((hat->hat_flags & HAT_VLP)) {
2917c478bd9Sstevel@tonic-gate 		hat->hat_num_hash = mmu.vlp_hash_cnt;
2927c478bd9Sstevel@tonic-gate 		hat->hat_ht_hash = kmem_cache_alloc(vlp_hash_cache, KM_SLEEP);
2937c478bd9Sstevel@tonic-gate 	} else {
2947c478bd9Sstevel@tonic-gate 		hat->hat_num_hash = mmu.hash_cnt;
2957c478bd9Sstevel@tonic-gate 		hat->hat_ht_hash = kmem_cache_alloc(hat_hash_cache, KM_SLEEP);
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 	bzero(hat->hat_ht_hash, hat->hat_num_hash * sizeof (htable_t *));
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	/*
3007c478bd9Sstevel@tonic-gate 	 * Initialize Kernel HAT entries at the top of the top level page
301843e1988Sjohnlev 	 * tables for the new hat.
3027c478bd9Sstevel@tonic-gate 	 */
3037c478bd9Sstevel@tonic-gate 	hat->hat_htable = NULL;
3047c478bd9Sstevel@tonic-gate 	hat->hat_ht_cached = NULL;
305843e1988Sjohnlev 	XPV_DISALLOW_MIGRATE();
3067c478bd9Sstevel@tonic-gate 	ht = htable_create(hat, (uintptr_t)0, TOP_LEVEL(hat), NULL);
307843e1988Sjohnlev 	hat->hat_htable = ht;
308ae115bc7Smrj 
309843e1988Sjohnlev #if defined(__amd64)
310843e1988Sjohnlev 	if (hat->hat_flags & HAT_VLP)
311843e1988Sjohnlev 		goto init_done;
3127c478bd9Sstevel@tonic-gate #endif
3137c478bd9Sstevel@tonic-gate 
314843e1988Sjohnlev 	for (r = 0; r < num_kernel_ranges; ++r) {
315843e1988Sjohnlev 		rp = &kernel_ranges[r];
316843e1988Sjohnlev 		for (va = rp->hkr_start_va; va != rp->hkr_end_va;
317843e1988Sjohnlev 		    va += cnt * LEVEL_SIZE(rp->hkr_level)) {
318843e1988Sjohnlev 
319843e1988Sjohnlev 			if (rp->hkr_level == TOP_LEVEL(hat))
320843e1988Sjohnlev 				ht = hat->hat_htable;
321843e1988Sjohnlev 			else
322843e1988Sjohnlev 				ht = htable_create(hat, va, rp->hkr_level,
323843e1988Sjohnlev 				    NULL);
324843e1988Sjohnlev 
325843e1988Sjohnlev 			start = htable_va2entry(va, ht);
326843e1988Sjohnlev 			cnt = HTABLE_NUM_PTES(ht) - start;
327843e1988Sjohnlev 			eva = va +
328843e1988Sjohnlev 			    ((uintptr_t)cnt << LEVEL_SHIFT(rp->hkr_level));
329843e1988Sjohnlev 			if (rp->hkr_end_va != 0 &&
330843e1988Sjohnlev 			    (eva > rp->hkr_end_va || eva == 0))
331843e1988Sjohnlev 				cnt = htable_va2entry(rp->hkr_end_va, ht) -
332843e1988Sjohnlev 				    start;
333843e1988Sjohnlev 
334843e1988Sjohnlev #if defined(__i386) && !defined(__xpv)
335843e1988Sjohnlev 			if (ht->ht_flags & HTABLE_VLP) {
336843e1988Sjohnlev 				bcopy(&vlp_page[start],
337843e1988Sjohnlev 				    &hat->hat_vlp_ptes[start],
338843e1988Sjohnlev 				    cnt * sizeof (x86pte_t));
339843e1988Sjohnlev 				continue;
340843e1988Sjohnlev 			}
341843e1988Sjohnlev #endif
342843e1988Sjohnlev 			src = htable_lookup(kas.a_hat, va, rp->hkr_level);
343843e1988Sjohnlev 			ASSERT(src != NULL);
344843e1988Sjohnlev 			x86pte_copy(src, ht, start, cnt);
345843e1988Sjohnlev 			htable_release(src);
346843e1988Sjohnlev 		}
347843e1988Sjohnlev 	}
348843e1988Sjohnlev 
349843e1988Sjohnlev init_done:
350843e1988Sjohnlev 
351843e1988Sjohnlev #if defined(__xpv)
3527c478bd9Sstevel@tonic-gate 	/*
353843e1988Sjohnlev 	 * Pin top level page tables after initializing them
3547c478bd9Sstevel@tonic-gate 	 */
355843e1988Sjohnlev 	xen_pin(hat->hat_htable->ht_pfn, mmu.max_level);
356843e1988Sjohnlev #if defined(__amd64)
357843e1988Sjohnlev 	xen_pin(hat->hat_user_ptable, mmu.max_level);
358843e1988Sjohnlev #endif
3597c478bd9Sstevel@tonic-gate #endif
360551bc2a6Smrj 	XPV_ALLOW_MIGRATE();
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	/*
363a85a6733Sjosephb 	 * Put it at the start of the global list of all hats (used by stealing)
364a85a6733Sjosephb 	 *
365a85a6733Sjosephb 	 * kas.a_hat is not in the list but is instead used to find the
366a85a6733Sjosephb 	 * first and last items in the list.
367a85a6733Sjosephb 	 *
368a85a6733Sjosephb 	 * - kas.a_hat->hat_next points to the start of the user hats.
369a85a6733Sjosephb 	 *   The list ends where hat->hat_next == NULL
370a85a6733Sjosephb 	 *
371a85a6733Sjosephb 	 * - kas.a_hat->hat_prev points to the last of the user hats.
372a85a6733Sjosephb 	 *   The list begins where hat->hat_prev == NULL
3737c478bd9Sstevel@tonic-gate 	 */
3747c478bd9Sstevel@tonic-gate 	mutex_enter(&hat_list_lock);
375a85a6733Sjosephb 	hat->hat_prev = NULL;
376a85a6733Sjosephb 	hat->hat_next = kas.a_hat->hat_next;
377a85a6733Sjosephb 	if (hat->hat_next)
378a85a6733Sjosephb 		hat->hat_next->hat_prev = hat;
379a85a6733Sjosephb 	else
380a85a6733Sjosephb 		kas.a_hat->hat_prev = hat;
3817c478bd9Sstevel@tonic-gate 	kas.a_hat->hat_next = hat;
3827c478bd9Sstevel@tonic-gate 	mutex_exit(&hat_list_lock);
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	return (hat);
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate /*
3887c478bd9Sstevel@tonic-gate  * process has finished executing but as has not been cleaned up yet.
3897c478bd9Sstevel@tonic-gate  */
3907c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3917c478bd9Sstevel@tonic-gate void
3927c478bd9Sstevel@tonic-gate hat_free_start(hat_t *hat)
3937c478bd9Sstevel@tonic-gate {
3947c478bd9Sstevel@tonic-gate 	ASSERT(AS_WRITE_HELD(hat->hat_as, &hat->hat_as->a_lock));
395a85a6733Sjosephb 
396a85a6733Sjosephb 	/*
397a85a6733Sjosephb 	 * If the hat is currently a stealing victim, wait for the stealing
398a85a6733Sjosephb 	 * to finish.  Once we mark it as HAT_FREEING, htable_steal()
399a85a6733Sjosephb 	 * won't look at its pagetables anymore.
400a85a6733Sjosephb 	 */
4017c478bd9Sstevel@tonic-gate 	mutex_enter(&hat_list_lock);
402a85a6733Sjosephb 	while (hat->hat_flags & HAT_VICTIM)
403a85a6733Sjosephb 		cv_wait(&hat_list_cv, &hat_list_lock);
4047c478bd9Sstevel@tonic-gate 	hat->hat_flags |= HAT_FREEING;
4057c478bd9Sstevel@tonic-gate 	mutex_exit(&hat_list_lock);
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate /*
4097c478bd9Sstevel@tonic-gate  * An address space is being destroyed, so we destroy the associated hat.
4107c478bd9Sstevel@tonic-gate  */
4117c478bd9Sstevel@tonic-gate void
4127c478bd9Sstevel@tonic-gate hat_free_end(hat_t *hat)
4137c478bd9Sstevel@tonic-gate {
4147c478bd9Sstevel@tonic-gate 	kmem_cache_t *cache;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	ASSERT(hat->hat_flags & HAT_FREEING);
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	/*
4197c478bd9Sstevel@tonic-gate 	 * must not be running on the given hat
4207c478bd9Sstevel@tonic-gate 	 */
4217c478bd9Sstevel@tonic-gate 	ASSERT(CPU->cpu_current_hat != hat);
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	/*
424a85a6733Sjosephb 	 * Remove it from the list of HATs
4257c478bd9Sstevel@tonic-gate 	 */
4267c478bd9Sstevel@tonic-gate 	mutex_enter(&hat_list_lock);
427a85a6733Sjosephb 	if (hat->hat_prev)
428a85a6733Sjosephb 		hat->hat_prev->hat_next = hat->hat_next;
429a85a6733Sjosephb 	else
4307c478bd9Sstevel@tonic-gate 		kas.a_hat->hat_next = hat->hat_next;
431a85a6733Sjosephb 	if (hat->hat_next)
432a85a6733Sjosephb 		hat->hat_next->hat_prev = hat->hat_prev;
433a85a6733Sjosephb 	else
434a85a6733Sjosephb 		kas.a_hat->hat_prev = hat->hat_prev;
4357c478bd9Sstevel@tonic-gate 	mutex_exit(&hat_list_lock);
436a85a6733Sjosephb 	hat->hat_next = hat->hat_prev = NULL;
4377c478bd9Sstevel@tonic-gate 
438843e1988Sjohnlev #if defined(__xpv)
439843e1988Sjohnlev 	/*
440843e1988Sjohnlev 	 * On the hypervisor, unpin top level page table(s)
441843e1988Sjohnlev 	 */
442843e1988Sjohnlev 	xen_unpin(hat->hat_htable->ht_pfn);
443843e1988Sjohnlev #if defined(__amd64)
444843e1988Sjohnlev 	xen_unpin(hat->hat_user_ptable);
445843e1988Sjohnlev #endif
446843e1988Sjohnlev #endif
447843e1988Sjohnlev 
4487c478bd9Sstevel@tonic-gate 	/*
4497c478bd9Sstevel@tonic-gate 	 * Make a pass through the htables freeing them all up.
4507c478bd9Sstevel@tonic-gate 	 */
4517c478bd9Sstevel@tonic-gate 	htable_purge_hat(hat);
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	/*
4547c478bd9Sstevel@tonic-gate 	 * Decide which kmem cache the hash table came from, then free it.
4557c478bd9Sstevel@tonic-gate 	 */
4567c478bd9Sstevel@tonic-gate 	if (hat->hat_flags & HAT_VLP)
4577c478bd9Sstevel@tonic-gate 		cache = vlp_hash_cache;
4587c478bd9Sstevel@tonic-gate 	else
4597c478bd9Sstevel@tonic-gate 		cache = hat_hash_cache;
4607c478bd9Sstevel@tonic-gate 	kmem_cache_free(cache, hat->hat_ht_hash);
4617c478bd9Sstevel@tonic-gate 	hat->hat_ht_hash = NULL;
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	hat->hat_flags = 0;
4647c478bd9Sstevel@tonic-gate 	kmem_cache_free(hat_cache, hat);
4657c478bd9Sstevel@tonic-gate }
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate /*
4687c478bd9Sstevel@tonic-gate  * round kernelbase down to a supported value to use for _userlimit
4697c478bd9Sstevel@tonic-gate  *
4707c478bd9Sstevel@tonic-gate  * userlimit must be aligned down to an entry in the top level htable.
4717c478bd9Sstevel@tonic-gate  * The one exception is for 32 bit HAT's running PAE.
4727c478bd9Sstevel@tonic-gate  */
4737c478bd9Sstevel@tonic-gate uintptr_t
4747c478bd9Sstevel@tonic-gate hat_kernelbase(uintptr_t va)
4757c478bd9Sstevel@tonic-gate {
4767c478bd9Sstevel@tonic-gate #if defined(__i386)
4777c478bd9Sstevel@tonic-gate 	va &= LEVEL_MASK(1);
4787c478bd9Sstevel@tonic-gate #endif
4797c478bd9Sstevel@tonic-gate 	if (IN_VA_HOLE(va))
4807c478bd9Sstevel@tonic-gate 		panic("_userlimit %p will fall in VA hole\n", (void *)va);
4817c478bd9Sstevel@tonic-gate 	return (va);
4827c478bd9Sstevel@tonic-gate }
4837c478bd9Sstevel@tonic-gate 
484512cf780Skchow /*
485512cf780Skchow  *
486512cf780Skchow  */
487512cf780Skchow static void
488512cf780Skchow set_max_page_level()
489512cf780Skchow {
490512cf780Skchow 	level_t lvl;
491512cf780Skchow 
492512cf780Skchow 	if (!kbm_largepage_support) {
493512cf780Skchow 		lvl = 0;
4946f0cf5c5Skchow 	} else {
4956f0cf5c5Skchow 		if (x86_feature & X86_1GPG) {
4966f0cf5c5Skchow 			lvl = 2;
4976f0cf5c5Skchow 			if (chk_optimal_1gtlb &&
4986f0cf5c5Skchow 			    cpuid_opteron_erratum(CPU, 6671130)) {
4996f0cf5c5Skchow 				lvl = 1;
5006f0cf5c5Skchow 			}
5016f0cf5c5Skchow 			if (plat_mnode_xcheck(LEVEL_SIZE(2) >>
5026f0cf5c5Skchow 			    LEVEL_SHIFT(0))) {
5036f0cf5c5Skchow 				lvl = 1;
5046f0cf5c5Skchow 			}
5056f0cf5c5Skchow 		} else {
506512cf780Skchow 			lvl = 1;
507512cf780Skchow 		}
508512cf780Skchow 	}
509512cf780Skchow 	mmu.max_page_level = lvl;
510512cf780Skchow 
511512cf780Skchow 	if ((lvl == 2) && (enable_1gpg == 0))
512512cf780Skchow 		mmu.umax_page_level = 1;
513512cf780Skchow 	else
514512cf780Skchow 		mmu.umax_page_level = lvl;
515512cf780Skchow }
516512cf780Skchow 
5177c478bd9Sstevel@tonic-gate /*
5187c478bd9Sstevel@tonic-gate  * Initialize hat data structures based on processor MMU information.
5197c478bd9Sstevel@tonic-gate  */
5207c478bd9Sstevel@tonic-gate void
5217c478bd9Sstevel@tonic-gate mmu_init(void)
5227c478bd9Sstevel@tonic-gate {
5237c478bd9Sstevel@tonic-gate 	uint_t max_htables;
5247c478bd9Sstevel@tonic-gate 	uint_t pa_bits;
5257c478bd9Sstevel@tonic-gate 	uint_t va_bits;
5267c478bd9Sstevel@tonic-gate 	int i;
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	/*
529ae115bc7Smrj 	 * If CPU enabled the page table global bit, use it for the kernel
530ae115bc7Smrj 	 * This is bit 7 in CR4 (PGE - Page Global Enable).
5317c478bd9Sstevel@tonic-gate 	 */
532ae115bc7Smrj 	if ((x86_feature & X86_PGE) != 0 && (getcr4() & CR4_PGE) != 0)
5337c478bd9Sstevel@tonic-gate 		mmu.pt_global = PT_GLOBAL;
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	/*
536ae115bc7Smrj 	 * Detect NX and PAE usage.
5377c478bd9Sstevel@tonic-gate 	 */
538ae115bc7Smrj 	mmu.pae_hat = kbm_pae_support;
539ae115bc7Smrj 	if (kbm_nx_support)
5407c478bd9Sstevel@tonic-gate 		mmu.pt_nx = PT_NX;
541ae115bc7Smrj 	else
5427c478bd9Sstevel@tonic-gate 		mmu.pt_nx = 0;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	/*
5457c478bd9Sstevel@tonic-gate 	 * Use CPU info to set various MMU parameters
5467c478bd9Sstevel@tonic-gate 	 */
5477c478bd9Sstevel@tonic-gate 	cpuid_get_addrsize(CPU, &pa_bits, &va_bits);
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	if (va_bits < sizeof (void *) * NBBY) {
5507c478bd9Sstevel@tonic-gate 		mmu.hole_start = (1ul << (va_bits - 1));
5517c478bd9Sstevel@tonic-gate 		mmu.hole_end = 0ul - mmu.hole_start - 1;
5527c478bd9Sstevel@tonic-gate 	} else {
5537c478bd9Sstevel@tonic-gate 		mmu.hole_end = 0;
5547c478bd9Sstevel@tonic-gate 		mmu.hole_start = mmu.hole_end - 1;
5557c478bd9Sstevel@tonic-gate 	}
5567c478bd9Sstevel@tonic-gate #if defined(OPTERON_ERRATUM_121)
5577c478bd9Sstevel@tonic-gate 	/*
5587c478bd9Sstevel@tonic-gate 	 * If erratum 121 has already been detected at this time, hole_start
5597c478bd9Sstevel@tonic-gate 	 * contains the value to be subtracted from mmu.hole_start.
5607c478bd9Sstevel@tonic-gate 	 */
5617c478bd9Sstevel@tonic-gate 	ASSERT(hole_start == 0 || opteron_erratum_121 != 0);
5627c478bd9Sstevel@tonic-gate 	hole_start = mmu.hole_start - hole_start;
5637c478bd9Sstevel@tonic-gate #else
5647c478bd9Sstevel@tonic-gate 	hole_start = mmu.hole_start;
5657c478bd9Sstevel@tonic-gate #endif
5667c478bd9Sstevel@tonic-gate 	hole_end = mmu.hole_end;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	mmu.highest_pfn = mmu_btop((1ull << pa_bits) - 1);
5697c478bd9Sstevel@tonic-gate 	if (mmu.pae_hat == 0 && pa_bits > 32)
5707c478bd9Sstevel@tonic-gate 		mmu.highest_pfn = PFN_4G - 1;
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	if (mmu.pae_hat) {
5737c478bd9Sstevel@tonic-gate 		mmu.pte_size = 8;	/* 8 byte PTEs */
5747c478bd9Sstevel@tonic-gate 		mmu.pte_size_shift = 3;
5757c478bd9Sstevel@tonic-gate 	} else {
5767c478bd9Sstevel@tonic-gate 		mmu.pte_size = 4;	/* 4 byte PTEs */
5777c478bd9Sstevel@tonic-gate 		mmu.pte_size_shift = 2;
5787c478bd9Sstevel@tonic-gate 	}
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	if (mmu.pae_hat && (x86_feature & X86_PAE) == 0)
5817c478bd9Sstevel@tonic-gate 		panic("Processor does not support PAE");
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	if ((x86_feature & X86_CX8) == 0)
5847c478bd9Sstevel@tonic-gate 		panic("Processor does not support cmpxchg8b instruction");
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate #if defined(__amd64)
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	mmu.num_level = 4;
5897c478bd9Sstevel@tonic-gate 	mmu.max_level = 3;
5907c478bd9Sstevel@tonic-gate 	mmu.ptes_per_table = 512;
5917c478bd9Sstevel@tonic-gate 	mmu.top_level_count = 512;
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	mmu.level_shift[0] = 12;
5947c478bd9Sstevel@tonic-gate 	mmu.level_shift[1] = 21;
5957c478bd9Sstevel@tonic-gate 	mmu.level_shift[2] = 30;
5967c478bd9Sstevel@tonic-gate 	mmu.level_shift[3] = 39;
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate #elif defined(__i386)
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	if (mmu.pae_hat) {
6017c478bd9Sstevel@tonic-gate 		mmu.num_level = 3;
6027c478bd9Sstevel@tonic-gate 		mmu.max_level = 2;
6037c478bd9Sstevel@tonic-gate 		mmu.ptes_per_table = 512;
6047c478bd9Sstevel@tonic-gate 		mmu.top_level_count = 4;
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 		mmu.level_shift[0] = 12;
6077c478bd9Sstevel@tonic-gate 		mmu.level_shift[1] = 21;
6087c478bd9Sstevel@tonic-gate 		mmu.level_shift[2] = 30;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	} else {
6117c478bd9Sstevel@tonic-gate 		mmu.num_level = 2;
6127c478bd9Sstevel@tonic-gate 		mmu.max_level = 1;
6137c478bd9Sstevel@tonic-gate 		mmu.ptes_per_table = 1024;
6147c478bd9Sstevel@tonic-gate 		mmu.top_level_count = 1024;
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 		mmu.level_shift[0] = 12;
6177c478bd9Sstevel@tonic-gate 		mmu.level_shift[1] = 22;
6187c478bd9Sstevel@tonic-gate 	}
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate #endif	/* __i386 */
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	for (i = 0; i < mmu.num_level; ++i) {
6237c478bd9Sstevel@tonic-gate 		mmu.level_size[i] = 1UL << mmu.level_shift[i];
6247c478bd9Sstevel@tonic-gate 		mmu.level_offset[i] = mmu.level_size[i] - 1;
6257c478bd9Sstevel@tonic-gate 		mmu.level_mask[i] = ~mmu.level_offset[i];
6267c478bd9Sstevel@tonic-gate 	}
6277c478bd9Sstevel@tonic-gate 
628512cf780Skchow 	set_max_page_level();
629512cf780Skchow 
630472714d6Skchow 	mmu_page_sizes = mmu.max_page_level + 1;
631472714d6Skchow 	mmu_exported_page_sizes = mmu.umax_page_level + 1;
632472714d6Skchow 
633472714d6Skchow 	/* restrict legacy applications from using pagesizes 1g and above */
634472714d6Skchow 	mmu_legacy_page_sizes =
635472714d6Skchow 	    (mmu_exported_page_sizes > 2) ? 2 : mmu_exported_page_sizes;
636472714d6Skchow 
637472714d6Skchow 
638ae115bc7Smrj 	for (i = 0; i <= mmu.max_page_level; ++i) {
639ab4a9bebSjohnlev 		mmu.pte_bits[i] = PT_VALID | pt_kern;
640ae115bc7Smrj 		if (i > 0)
641ae115bc7Smrj 			mmu.pte_bits[i] |= PT_PAGESIZE;
642ae115bc7Smrj 	}
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	/*
6457c478bd9Sstevel@tonic-gate 	 * NOTE Legacy 32 bit PAE mode only has the P_VALID bit at top level.
6467c478bd9Sstevel@tonic-gate 	 */
6477c478bd9Sstevel@tonic-gate 	for (i = 1; i < mmu.num_level; ++i)
6487c478bd9Sstevel@tonic-gate 		mmu.ptp_bits[i] = PT_PTPBITS;
649ae115bc7Smrj 
6507c478bd9Sstevel@tonic-gate #if defined(__i386)
6517c478bd9Sstevel@tonic-gate 	mmu.ptp_bits[2] = PT_VALID;
6527c478bd9Sstevel@tonic-gate #endif
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	/*
6557c478bd9Sstevel@tonic-gate 	 * Compute how many hash table entries to have per process for htables.
6567c478bd9Sstevel@tonic-gate 	 * We start with 1 page's worth of entries.
6577c478bd9Sstevel@tonic-gate 	 *
6587c478bd9Sstevel@tonic-gate 	 * If physical memory is small, reduce the amount need to cover it.
6597c478bd9Sstevel@tonic-gate 	 */
6607c478bd9Sstevel@tonic-gate 	max_htables = physmax / mmu.ptes_per_table;
6617c478bd9Sstevel@tonic-gate 	mmu.hash_cnt = MMU_PAGESIZE / sizeof (htable_t *);
6627c478bd9Sstevel@tonic-gate 	while (mmu.hash_cnt > 16 && mmu.hash_cnt >= max_htables)
6637c478bd9Sstevel@tonic-gate 		mmu.hash_cnt >>= 1;
6647c478bd9Sstevel@tonic-gate 	mmu.vlp_hash_cnt = mmu.hash_cnt;
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate #if defined(__amd64)
6677c478bd9Sstevel@tonic-gate 	/*
6687c478bd9Sstevel@tonic-gate 	 * If running in 64 bits and physical memory is large,
6697c478bd9Sstevel@tonic-gate 	 * increase the size of the cache to cover all of memory for
6707c478bd9Sstevel@tonic-gate 	 * a 64 bit process.
6717c478bd9Sstevel@tonic-gate 	 */
6727c478bd9Sstevel@tonic-gate #define	HASH_MAX_LENGTH 4
6737c478bd9Sstevel@tonic-gate 	while (mmu.hash_cnt * HASH_MAX_LENGTH < max_htables)
6747c478bd9Sstevel@tonic-gate 		mmu.hash_cnt <<= 1;
6757c478bd9Sstevel@tonic-gate #endif
6767c478bd9Sstevel@tonic-gate }
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate /*
6807c478bd9Sstevel@tonic-gate  * initialize hat data structures
6817c478bd9Sstevel@tonic-gate  */
6827c478bd9Sstevel@tonic-gate void
6837c478bd9Sstevel@tonic-gate hat_init()
6847c478bd9Sstevel@tonic-gate {
6857c478bd9Sstevel@tonic-gate #if defined(__i386)
6867c478bd9Sstevel@tonic-gate 	/*
6877c478bd9Sstevel@tonic-gate 	 * _userlimit must be aligned correctly
6887c478bd9Sstevel@tonic-gate 	 */
6897c478bd9Sstevel@tonic-gate 	if ((_userlimit & LEVEL_MASK(1)) != _userlimit) {
6907c478bd9Sstevel@tonic-gate 		prom_printf("hat_init(): _userlimit=%p, not aligned at %p\n",
6917c478bd9Sstevel@tonic-gate 		    (void *)_userlimit, (void *)LEVEL_SIZE(1));
6927c478bd9Sstevel@tonic-gate 		halt("hat_init(): Unable to continue");
6937c478bd9Sstevel@tonic-gate 	}
6947c478bd9Sstevel@tonic-gate #endif
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	cv_init(&hat_list_cv, NULL, CV_DEFAULT, NULL);
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	/*
6997c478bd9Sstevel@tonic-gate 	 * initialize kmem caches
7007c478bd9Sstevel@tonic-gate 	 */
7017c478bd9Sstevel@tonic-gate 	htable_init();
7027c478bd9Sstevel@tonic-gate 	hment_init();
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	hat_cache = kmem_cache_create("hat_t",
7057c478bd9Sstevel@tonic-gate 	    sizeof (hat_t), 0, hati_constructor, NULL, NULL,
7067c478bd9Sstevel@tonic-gate 	    NULL, 0, 0);
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	hat_hash_cache = kmem_cache_create("HatHash",
7097c478bd9Sstevel@tonic-gate 	    mmu.hash_cnt * sizeof (htable_t *), 0, NULL, NULL, NULL,
7107c478bd9Sstevel@tonic-gate 	    NULL, 0, 0);
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	/*
7137c478bd9Sstevel@tonic-gate 	 * VLP hats can use a smaller hash table size on large memroy machines
7147c478bd9Sstevel@tonic-gate 	 */
7157c478bd9Sstevel@tonic-gate 	if (mmu.hash_cnt == mmu.vlp_hash_cnt) {
7167c478bd9Sstevel@tonic-gate 		vlp_hash_cache = hat_hash_cache;
7177c478bd9Sstevel@tonic-gate 	} else {
7187c478bd9Sstevel@tonic-gate 		vlp_hash_cache = kmem_cache_create("HatVlpHash",
7197c478bd9Sstevel@tonic-gate 		    mmu.vlp_hash_cnt * sizeof (htable_t *), 0, NULL, NULL, NULL,
7207c478bd9Sstevel@tonic-gate 		    NULL, 0, 0);
7217c478bd9Sstevel@tonic-gate 	}
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	/*
7247c478bd9Sstevel@tonic-gate 	 * Set up the kernel's hat
7257c478bd9Sstevel@tonic-gate 	 */
7267c478bd9Sstevel@tonic-gate 	AS_LOCK_ENTER(&kas, &kas.a_lock, RW_WRITER);
7277c478bd9Sstevel@tonic-gate 	kas.a_hat = kmem_cache_alloc(hat_cache, KM_NOSLEEP);
7287c478bd9Sstevel@tonic-gate 	mutex_init(&kas.a_hat->hat_mutex, NULL, MUTEX_DEFAULT, NULL);
7297c478bd9Sstevel@tonic-gate 	kas.a_hat->hat_as = &kas;
7307c478bd9Sstevel@tonic-gate 	kas.a_hat->hat_flags = 0;
7317c478bd9Sstevel@tonic-gate 	AS_LOCK_EXIT(&kas, &kas.a_lock);
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	CPUSET_ZERO(khat_cpuset);
7347c478bd9Sstevel@tonic-gate 	CPUSET_ADD(khat_cpuset, CPU->cpu_id);
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	/*
7377c478bd9Sstevel@tonic-gate 	 * The kernel hat's next pointer serves as the head of the hat list .
738a85a6733Sjosephb 	 * The kernel hat's prev pointer tracks the last hat on the list for
739a85a6733Sjosephb 	 * htable_steal() to use.
7407c478bd9Sstevel@tonic-gate 	 */
7417c478bd9Sstevel@tonic-gate 	kas.a_hat->hat_next = NULL;
742a85a6733Sjosephb 	kas.a_hat->hat_prev = NULL;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	/*
7457c478bd9Sstevel@tonic-gate 	 * Allocate an htable hash bucket for the kernel
7467c478bd9Sstevel@tonic-gate 	 * XX64 - tune for 64 bit procs
7477c478bd9Sstevel@tonic-gate 	 */
7487c478bd9Sstevel@tonic-gate 	kas.a_hat->hat_num_hash = mmu.hash_cnt;
7497c478bd9Sstevel@tonic-gate 	kas.a_hat->hat_ht_hash = kmem_cache_alloc(hat_hash_cache, KM_NOSLEEP);
7507c478bd9Sstevel@tonic-gate 	bzero(kas.a_hat->hat_ht_hash, mmu.hash_cnt * sizeof (htable_t *));
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	/*
7537c478bd9Sstevel@tonic-gate 	 * zero out the top level and cached htable pointers
7547c478bd9Sstevel@tonic-gate 	 */
7557c478bd9Sstevel@tonic-gate 	kas.a_hat->hat_ht_cached = NULL;
7567c478bd9Sstevel@tonic-gate 	kas.a_hat->hat_htable = NULL;
7579d9461f9Strevtom 
7589d9461f9Strevtom 	/*
7599d9461f9Strevtom 	 * Pre-allocate hrm_hashtab before enabling the collection of
7609d9461f9Strevtom 	 * refmod statistics.  Allocating on the fly would mean us
7619d9461f9Strevtom 	 * running the risk of suffering recursive mutex enters or
7629d9461f9Strevtom 	 * deadlocks.
7639d9461f9Strevtom 	 */
7649d9461f9Strevtom 	hrm_hashtab = kmem_zalloc(HRM_HASHSIZE * sizeof (struct hrmstat *),
7659d9461f9Strevtom 	    KM_SLEEP);
7667c478bd9Sstevel@tonic-gate }
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate /*
7697c478bd9Sstevel@tonic-gate  * Prepare CPU specific pagetables for VLP processes on 64 bit kernels.
7707c478bd9Sstevel@tonic-gate  *
7717c478bd9Sstevel@tonic-gate  * Each CPU has a set of 2 pagetables that are reused for any 32 bit
7727c478bd9Sstevel@tonic-gate  * process it runs. They are the top level pagetable, hci_vlp_l3ptes, and
7737c478bd9Sstevel@tonic-gate  * the next to top level table for the bottom 512 Gig, hci_vlp_l2ptes.
7747c478bd9Sstevel@tonic-gate  */
7757c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7767c478bd9Sstevel@tonic-gate static void
7777c478bd9Sstevel@tonic-gate hat_vlp_setup(struct cpu *cpu)
7787c478bd9Sstevel@tonic-gate {
779843e1988Sjohnlev #if defined(__amd64) && !defined(__xpv)
7807c478bd9Sstevel@tonic-gate 	struct hat_cpu_info *hci = cpu->cpu_hat_info;
7817c478bd9Sstevel@tonic-gate 	pfn_t pfn;
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	/*
7847c478bd9Sstevel@tonic-gate 	 * allocate the level==2 page table for the bottom most
7857c478bd9Sstevel@tonic-gate 	 * 512Gig of address space (this is where 32 bit apps live)
7867c478bd9Sstevel@tonic-gate 	 */
7877c478bd9Sstevel@tonic-gate 	ASSERT(hci != NULL);
7887c478bd9Sstevel@tonic-gate 	hci->hci_vlp_l2ptes = kmem_zalloc(MMU_PAGESIZE, KM_SLEEP);
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	/*
7917c478bd9Sstevel@tonic-gate 	 * Allocate a top level pagetable and copy the kernel's
7927c478bd9Sstevel@tonic-gate 	 * entries into it. Then link in hci_vlp_l2ptes in the 1st entry.
7937c478bd9Sstevel@tonic-gate 	 */
7947c478bd9Sstevel@tonic-gate 	hci->hci_vlp_l3ptes = kmem_zalloc(MMU_PAGESIZE, KM_SLEEP);
7957c478bd9Sstevel@tonic-gate 	hci->hci_vlp_pfn =
7967c478bd9Sstevel@tonic-gate 	    hat_getpfnum(kas.a_hat, (caddr_t)hci->hci_vlp_l3ptes);
7977c478bd9Sstevel@tonic-gate 	ASSERT(hci->hci_vlp_pfn != PFN_INVALID);
798843e1988Sjohnlev 	bcopy(vlp_page, hci->hci_vlp_l3ptes, MMU_PAGESIZE);
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	pfn = hat_getpfnum(kas.a_hat, (caddr_t)hci->hci_vlp_l2ptes);
8017c478bd9Sstevel@tonic-gate 	ASSERT(pfn != PFN_INVALID);
8027c478bd9Sstevel@tonic-gate 	hci->hci_vlp_l3ptes[0] = MAKEPTP(pfn, 2);
803843e1988Sjohnlev #endif /* __amd64 && !__xpv */
8047c478bd9Sstevel@tonic-gate }
8057c478bd9Sstevel@tonic-gate 
806ae115bc7Smrj /*ARGSUSED*/
807ae115bc7Smrj static void
808ae115bc7Smrj hat_vlp_teardown(cpu_t *cpu)
809ae115bc7Smrj {
810843e1988Sjohnlev #if defined(__amd64) && !defined(__xpv)
811ae115bc7Smrj 	struct hat_cpu_info *hci;
812ae115bc7Smrj 
813ae115bc7Smrj 	if ((hci = cpu->cpu_hat_info) == NULL)
814ae115bc7Smrj 		return;
815ae115bc7Smrj 	if (hci->hci_vlp_l2ptes)
816ae115bc7Smrj 		kmem_free(hci->hci_vlp_l2ptes, MMU_PAGESIZE);
817ae115bc7Smrj 	if (hci->hci_vlp_l3ptes)
818ae115bc7Smrj 		kmem_free(hci->hci_vlp_l3ptes, MMU_PAGESIZE);
819843e1988Sjohnlev #endif
820843e1988Sjohnlev }
821843e1988Sjohnlev 
822843e1988Sjohnlev #define	NEXT_HKR(r, l, s, e) {			\
823843e1988Sjohnlev 	kernel_ranges[r].hkr_level = l;		\
824843e1988Sjohnlev 	kernel_ranges[r].hkr_start_va = s;	\
825843e1988Sjohnlev 	kernel_ranges[r].hkr_end_va = e;	\
826843e1988Sjohnlev 	++r;					\
827ae115bc7Smrj }
828ae115bc7Smrj 
8297c478bd9Sstevel@tonic-gate /*
8307c478bd9Sstevel@tonic-gate  * Finish filling in the kernel hat.
8317c478bd9Sstevel@tonic-gate  * Pre fill in all top level kernel page table entries for the kernel's
8327c478bd9Sstevel@tonic-gate  * part of the address range.  From this point on we can't use any new
8337c478bd9Sstevel@tonic-gate  * kernel large pages if they need PTE's at max_level
834ae115bc7Smrj  *
835ae115bc7Smrj  * create the kmap mappings.
8367c478bd9Sstevel@tonic-gate  */
8377c478bd9Sstevel@tonic-gate void
8387c478bd9Sstevel@tonic-gate hat_init_finish(void)
8397c478bd9Sstevel@tonic-gate {
840ae115bc7Smrj 	size_t		size;
841843e1988Sjohnlev 	uint_t		r = 0;
842843e1988Sjohnlev 	uintptr_t	va;
843843e1988Sjohnlev 	hat_kernel_range_t *rp;
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	/*
847843e1988Sjohnlev 	 * We are now effectively running on the kernel hat.
848843e1988Sjohnlev 	 * Clearing use_boot_reserve shuts off using the pre-allocated boot
849843e1988Sjohnlev 	 * reserve for all HAT allocations.  From here on, the reserves are
850843e1988Sjohnlev 	 * only used when avoiding recursion in kmem_alloc().
8517c478bd9Sstevel@tonic-gate 	 */
852843e1988Sjohnlev 	use_boot_reserve = 0;
853843e1988Sjohnlev 	htable_adjust_reserve();
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	/*
856843e1988Sjohnlev 	 * User HATs are initialized with copies of all kernel mappings in
857843e1988Sjohnlev 	 * higher level page tables. Ensure that those entries exist.
8587c478bd9Sstevel@tonic-gate 	 */
859843e1988Sjohnlev #if defined(__amd64)
860843e1988Sjohnlev 
861843e1988Sjohnlev 	NEXT_HKR(r, 3, kernelbase, 0);
862843e1988Sjohnlev #if defined(__xpv)
863843e1988Sjohnlev 	NEXT_HKR(r, 3, HYPERVISOR_VIRT_START, HYPERVISOR_VIRT_END);
864843e1988Sjohnlev #endif
865843e1988Sjohnlev 
866843e1988Sjohnlev #elif defined(__i386)
867843e1988Sjohnlev 
868843e1988Sjohnlev #if !defined(__xpv)
869843e1988Sjohnlev 	if (mmu.pae_hat) {
870843e1988Sjohnlev 		va = kernelbase;
871843e1988Sjohnlev 		if ((va & LEVEL_MASK(2)) != va) {
872843e1988Sjohnlev 			va = P2ROUNDUP(va, LEVEL_SIZE(2));
873843e1988Sjohnlev 			NEXT_HKR(r, 1, kernelbase, va);
874843e1988Sjohnlev 		}
875843e1988Sjohnlev 		if (va != 0)
876843e1988Sjohnlev 			NEXT_HKR(r, 2, va, 0);
877843e1988Sjohnlev 	} else
878843e1988Sjohnlev #endif /* __xpv */
879843e1988Sjohnlev 		NEXT_HKR(r, 1, kernelbase, 0);
880843e1988Sjohnlev 
881843e1988Sjohnlev #endif /* __i386 */
882843e1988Sjohnlev 
883843e1988Sjohnlev 	num_kernel_ranges = r;
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 	/*
886843e1988Sjohnlev 	 * Create all the kernel pagetables that will have entries
887843e1988Sjohnlev 	 * shared to user HATs.
8887c478bd9Sstevel@tonic-gate 	 */
889843e1988Sjohnlev 	for (r = 0; r < num_kernel_ranges; ++r) {
890843e1988Sjohnlev 		rp = &kernel_ranges[r];
891843e1988Sjohnlev 		for (va = rp->hkr_start_va; va != rp->hkr_end_va;
892843e1988Sjohnlev 		    va += LEVEL_SIZE(rp->hkr_level)) {
893843e1988Sjohnlev 			htable_t *ht;
894843e1988Sjohnlev 
895843e1988Sjohnlev 			if (IN_HYPERVISOR_VA(va))
896843e1988Sjohnlev 				continue;
897843e1988Sjohnlev 
898843e1988Sjohnlev 			/* can/must skip if a page mapping already exists */
899843e1988Sjohnlev 			if (rp->hkr_level <= mmu.max_page_level &&
900843e1988Sjohnlev 			    (ht = htable_getpage(kas.a_hat, va, NULL)) !=
901843e1988Sjohnlev 			    NULL) {
902843e1988Sjohnlev 				htable_release(ht);
903843e1988Sjohnlev 				continue;
904843e1988Sjohnlev 			}
905843e1988Sjohnlev 
906843e1988Sjohnlev 			(void) htable_create(kas.a_hat, va, rp->hkr_level - 1,
907843e1988Sjohnlev 			    NULL);
908843e1988Sjohnlev 		}
909843e1988Sjohnlev 	}
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	/*
912843e1988Sjohnlev 	 * 32 bit PAE metal kernels use only 4 of the 512 entries in the
913843e1988Sjohnlev 	 * page holding the top level pagetable. We use the remainder for
914843e1988Sjohnlev 	 * the "per CPU" page tables for VLP processes.
915843e1988Sjohnlev 	 * Map the top level kernel pagetable into the kernel to make
916843e1988Sjohnlev 	 * it easy to use bcopy access these tables.
9177c478bd9Sstevel@tonic-gate 	 */
9187c478bd9Sstevel@tonic-gate 	if (mmu.pae_hat) {
9197c478bd9Sstevel@tonic-gate 		vlp_page = vmem_alloc(heap_arena, MMU_PAGESIZE, VM_SLEEP);
9207c478bd9Sstevel@tonic-gate 		hat_devload(kas.a_hat, (caddr_t)vlp_page, MMU_PAGESIZE,
9217c478bd9Sstevel@tonic-gate 		    kas.a_hat->hat_htable->ht_pfn,
922843e1988Sjohnlev #if !defined(__xpv)
923ae115bc7Smrj 		    PROT_WRITE |
924843e1988Sjohnlev #endif
925ae115bc7Smrj 		    PROT_READ | HAT_NOSYNC | HAT_UNORDERED_OK,
9267c478bd9Sstevel@tonic-gate 		    HAT_LOAD | HAT_LOAD_NOCONSIST);
9277c478bd9Sstevel@tonic-gate 	}
9287c478bd9Sstevel@tonic-gate 	hat_vlp_setup(CPU);
929ae115bc7Smrj 
930ae115bc7Smrj 	/*
931ae115bc7Smrj 	 * Create kmap (cached mappings of kernel PTEs)
932ae115bc7Smrj 	 * for 32 bit we map from segmap_start .. ekernelheap
933ae115bc7Smrj 	 * for 64 bit we map from segmap_start .. segmap_start + segmapsize;
934ae115bc7Smrj 	 */
935ae115bc7Smrj #if defined(__i386)
936ae115bc7Smrj 	size = (uintptr_t)ekernelheap - segmap_start;
937ae115bc7Smrj #elif defined(__amd64)
938ae115bc7Smrj 	size = segmapsize;
939ae115bc7Smrj #endif
940ae115bc7Smrj 	hat_kmap_init((uintptr_t)segmap_start, size);
9417c478bd9Sstevel@tonic-gate }
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate /*
9447c478bd9Sstevel@tonic-gate  * On 32 bit PAE mode, PTE's are 64 bits, but ordinary atomic memory references
9457c478bd9Sstevel@tonic-gate  * are 32 bit, so for safety we must use cas64() to install these.
9467c478bd9Sstevel@tonic-gate  */
9477c478bd9Sstevel@tonic-gate #ifdef __i386
9487c478bd9Sstevel@tonic-gate static void
9497c478bd9Sstevel@tonic-gate reload_pae32(hat_t *hat, cpu_t *cpu)
9507c478bd9Sstevel@tonic-gate {
9517c478bd9Sstevel@tonic-gate 	x86pte_t *src;
9527c478bd9Sstevel@tonic-gate 	x86pte_t *dest;
9537c478bd9Sstevel@tonic-gate 	x86pte_t pte;
9547c478bd9Sstevel@tonic-gate 	int i;
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	/*
9577c478bd9Sstevel@tonic-gate 	 * Load the 4 entries of the level 2 page table into this
9587c478bd9Sstevel@tonic-gate 	 * cpu's range of the vlp_page and point cr3 at them.
9597c478bd9Sstevel@tonic-gate 	 */
9607c478bd9Sstevel@tonic-gate 	ASSERT(mmu.pae_hat);
9617c478bd9Sstevel@tonic-gate 	src = hat->hat_vlp_ptes;
9627c478bd9Sstevel@tonic-gate 	dest = vlp_page + (cpu->cpu_id + 1) * VLP_NUM_PTES;
9637c478bd9Sstevel@tonic-gate 	for (i = 0; i < VLP_NUM_PTES; ++i) {
9647c478bd9Sstevel@tonic-gate 		for (;;) {
9657c478bd9Sstevel@tonic-gate 			pte = dest[i];
9667c478bd9Sstevel@tonic-gate 			if (pte == src[i])
9677c478bd9Sstevel@tonic-gate 				break;
9687c478bd9Sstevel@tonic-gate 			if (cas64(dest + i, pte, src[i]) != src[i])
9697c478bd9Sstevel@tonic-gate 				break;
9707c478bd9Sstevel@tonic-gate 		}
9717c478bd9Sstevel@tonic-gate 	}
9727c478bd9Sstevel@tonic-gate }
9737c478bd9Sstevel@tonic-gate #endif
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate /*
9767c478bd9Sstevel@tonic-gate  * Switch to a new active hat, maintaining bit masks to track active CPUs.
977843e1988Sjohnlev  *
978843e1988Sjohnlev  * On the 32-bit PAE hypervisor, %cr3 is a 64-bit value, on metal it
979843e1988Sjohnlev  * remains a 32-bit value.
9807c478bd9Sstevel@tonic-gate  */
9817c478bd9Sstevel@tonic-gate void
9827c478bd9Sstevel@tonic-gate hat_switch(hat_t *hat)
9837c478bd9Sstevel@tonic-gate {
984843e1988Sjohnlev 	uint64_t	newcr3;
9857c478bd9Sstevel@tonic-gate 	cpu_t		*cpu = CPU;
9867c478bd9Sstevel@tonic-gate 	hat_t		*old = cpu->cpu_current_hat;
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 	/*
9897c478bd9Sstevel@tonic-gate 	 * set up this information first, so we don't miss any cross calls
9907c478bd9Sstevel@tonic-gate 	 */
9917c478bd9Sstevel@tonic-gate 	if (old != NULL) {
9927c478bd9Sstevel@tonic-gate 		if (old == hat)
9937c478bd9Sstevel@tonic-gate 			return;
9947c478bd9Sstevel@tonic-gate 		if (old != kas.a_hat)
9957c478bd9Sstevel@tonic-gate 			CPUSET_ATOMIC_DEL(old->hat_cpus, cpu->cpu_id);
9967c478bd9Sstevel@tonic-gate 	}
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	/*
99995c0a3c8Sjosephb 	 * Add this CPU to the active set for this HAT.
10007c478bd9Sstevel@tonic-gate 	 */
10017c478bd9Sstevel@tonic-gate 	if (hat != kas.a_hat) {
10027c478bd9Sstevel@tonic-gate 		CPUSET_ATOMIC_ADD(hat->hat_cpus, cpu->cpu_id);
10037c478bd9Sstevel@tonic-gate 	}
10047c478bd9Sstevel@tonic-gate 	cpu->cpu_current_hat = hat;
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 	/*
10077c478bd9Sstevel@tonic-gate 	 * now go ahead and load cr3
10087c478bd9Sstevel@tonic-gate 	 */
10097c478bd9Sstevel@tonic-gate 	if (hat->hat_flags & HAT_VLP) {
10107c478bd9Sstevel@tonic-gate #if defined(__amd64)
10117c478bd9Sstevel@tonic-gate 		x86pte_t *vlpptep = cpu->cpu_hat_info->hci_vlp_l2ptes;
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 		VLP_COPY(hat->hat_vlp_ptes, vlpptep);
10147c478bd9Sstevel@tonic-gate 		newcr3 = MAKECR3(cpu->cpu_hat_info->hci_vlp_pfn);
10157c478bd9Sstevel@tonic-gate #elif defined(__i386)
10167c478bd9Sstevel@tonic-gate 		reload_pae32(hat, cpu);
10177c478bd9Sstevel@tonic-gate 		newcr3 = MAKECR3(kas.a_hat->hat_htable->ht_pfn) +
10187c478bd9Sstevel@tonic-gate 		    (cpu->cpu_id + 1) * VLP_SIZE;
10197c478bd9Sstevel@tonic-gate #endif
10207c478bd9Sstevel@tonic-gate 	} else {
1021843e1988Sjohnlev 		newcr3 = MAKECR3((uint64_t)hat->hat_htable->ht_pfn);
10227c478bd9Sstevel@tonic-gate 	}
1023843e1988Sjohnlev #ifdef __xpv
1024843e1988Sjohnlev 	{
1025843e1988Sjohnlev 		struct mmuext_op t[2];
1026843e1988Sjohnlev 		uint_t retcnt;
1027843e1988Sjohnlev 		uint_t opcnt = 1;
1028843e1988Sjohnlev 
1029843e1988Sjohnlev 		t[0].cmd = MMUEXT_NEW_BASEPTR;
1030843e1988Sjohnlev 		t[0].arg1.mfn = mmu_btop(pa_to_ma(newcr3));
1031843e1988Sjohnlev #if defined(__amd64)
1032843e1988Sjohnlev 		/*
1033843e1988Sjohnlev 		 * There's an interesting problem here, as to what to
1034843e1988Sjohnlev 		 * actually specify when switching to the kernel hat.
1035843e1988Sjohnlev 		 * For now we'll reuse the kernel hat again.
1036843e1988Sjohnlev 		 */
1037843e1988Sjohnlev 		t[1].cmd = MMUEXT_NEW_USER_BASEPTR;
1038843e1988Sjohnlev 		if (hat == kas.a_hat)
1039843e1988Sjohnlev 			t[1].arg1.mfn = mmu_btop(pa_to_ma(newcr3));
1040843e1988Sjohnlev 		else
1041843e1988Sjohnlev 			t[1].arg1.mfn = pfn_to_mfn(hat->hat_user_ptable);
1042843e1988Sjohnlev 		++opcnt;
1043843e1988Sjohnlev #endif	/* __amd64 */
1044843e1988Sjohnlev 		if (HYPERVISOR_mmuext_op(t, opcnt, &retcnt, DOMID_SELF) < 0)
1045843e1988Sjohnlev 			panic("HYPERVISOR_mmu_update() failed");
1046843e1988Sjohnlev 		ASSERT(retcnt == opcnt);
1047843e1988Sjohnlev 
1048843e1988Sjohnlev 	}
1049843e1988Sjohnlev #else
10507c478bd9Sstevel@tonic-gate 	setcr3(newcr3);
1051843e1988Sjohnlev #endif
10527c478bd9Sstevel@tonic-gate 	ASSERT(cpu == CPU);
10537c478bd9Sstevel@tonic-gate }
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate /*
10567c478bd9Sstevel@tonic-gate  * Utility to return a valid x86pte_t from protections, pfn, and level number
10577c478bd9Sstevel@tonic-gate  */
10587c478bd9Sstevel@tonic-gate static x86pte_t
10597c478bd9Sstevel@tonic-gate hati_mkpte(pfn_t pfn, uint_t attr, level_t level, uint_t flags)
10607c478bd9Sstevel@tonic-gate {
10617c478bd9Sstevel@tonic-gate 	x86pte_t	pte;
10627c478bd9Sstevel@tonic-gate 	uint_t		cache_attr = attr & HAT_ORDER_MASK;
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	pte = MAKEPTE(pfn, level);
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 	if (attr & PROT_WRITE)
10677c478bd9Sstevel@tonic-gate 		PTE_SET(pte, PT_WRITABLE);
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 	if (attr & PROT_USER)
10707c478bd9Sstevel@tonic-gate 		PTE_SET(pte, PT_USER);
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 	if (!(attr & PROT_EXEC))
10737c478bd9Sstevel@tonic-gate 		PTE_SET(pte, mmu.pt_nx);
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	/*
1076ae115bc7Smrj 	 * Set the software bits used track ref/mod sync's and hments.
1077ae115bc7Smrj 	 * If not using REF/MOD, set them to avoid h/w rewriting PTEs.
10787c478bd9Sstevel@tonic-gate 	 */
10797c478bd9Sstevel@tonic-gate 	if (flags & HAT_LOAD_NOCONSIST)
1080ae115bc7Smrj 		PTE_SET(pte, PT_NOCONSIST | PT_REF | PT_MOD);
1081ae115bc7Smrj 	else if (attr & HAT_NOSYNC)
1082ae115bc7Smrj 		PTE_SET(pte, PT_NOSYNC | PT_REF | PT_MOD);
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 	/*
10857c478bd9Sstevel@tonic-gate 	 * Set the caching attributes in the PTE. The combination
10867c478bd9Sstevel@tonic-gate 	 * of attributes are poorly defined, so we pay attention
10877c478bd9Sstevel@tonic-gate 	 * to them in the given order.
10887c478bd9Sstevel@tonic-gate 	 *
10897c478bd9Sstevel@tonic-gate 	 * The test for HAT_STRICTORDER is different because it's defined
10907c478bd9Sstevel@tonic-gate 	 * as "0" - which was a stupid thing to do, but is too late to change!
10917c478bd9Sstevel@tonic-gate 	 */
10927c478bd9Sstevel@tonic-gate 	if (cache_attr == HAT_STRICTORDER) {
10937c478bd9Sstevel@tonic-gate 		PTE_SET(pte, PT_NOCACHE);
10947c478bd9Sstevel@tonic-gate 	/*LINTED [Lint hates empty ifs, but it's the obvious way to do this] */
10957c478bd9Sstevel@tonic-gate 	} else if (cache_attr & (HAT_UNORDERED_OK | HAT_STORECACHING_OK)) {
10967c478bd9Sstevel@tonic-gate 		/* nothing to set */;
10977c478bd9Sstevel@tonic-gate 	} else if (cache_attr & (HAT_MERGING_OK | HAT_LOADCACHING_OK)) {
10987c478bd9Sstevel@tonic-gate 		PTE_SET(pte, PT_NOCACHE);
10997c478bd9Sstevel@tonic-gate 		if (x86_feature & X86_PAT)
11007c478bd9Sstevel@tonic-gate 			PTE_SET(pte, (level == 0) ? PT_PAT_4K : PT_PAT_LARGE);
11017c478bd9Sstevel@tonic-gate 		else
11027c478bd9Sstevel@tonic-gate 			PTE_SET(pte, PT_WRITETHRU);
11037c478bd9Sstevel@tonic-gate 	} else {
11047c478bd9Sstevel@tonic-gate 		panic("hati_mkpte(): bad caching attributes: %x\n", cache_attr);
11057c478bd9Sstevel@tonic-gate 	}
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	return (pte);
11087c478bd9Sstevel@tonic-gate }
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate /*
11117c478bd9Sstevel@tonic-gate  * Duplicate address translations of the parent to the child.
11127c478bd9Sstevel@tonic-gate  * This function really isn't used anymore.
11137c478bd9Sstevel@tonic-gate  */
11147c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11157c478bd9Sstevel@tonic-gate int
11167c478bd9Sstevel@tonic-gate hat_dup(hat_t *old, hat_t *new, caddr_t addr, size_t len, uint_t flag)
11177c478bd9Sstevel@tonic-gate {
11187c478bd9Sstevel@tonic-gate 	ASSERT((uintptr_t)addr < kernelbase);
11197c478bd9Sstevel@tonic-gate 	ASSERT(new != kas.a_hat);
11207c478bd9Sstevel@tonic-gate 	ASSERT(old != kas.a_hat);
11217c478bd9Sstevel@tonic-gate 	return (0);
11227c478bd9Sstevel@tonic-gate }
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate /*
11257c478bd9Sstevel@tonic-gate  * Allocate any hat resources required for a process being swapped in.
11267c478bd9Sstevel@tonic-gate  */
11277c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11287c478bd9Sstevel@tonic-gate void
11297c478bd9Sstevel@tonic-gate hat_swapin(hat_t *hat)
11307c478bd9Sstevel@tonic-gate {
11317c478bd9Sstevel@tonic-gate 	/* do nothing - we let everything fault back in */
11327c478bd9Sstevel@tonic-gate }
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate /*
11357c478bd9Sstevel@tonic-gate  * Unload all translations associated with an address space of a process
11367c478bd9Sstevel@tonic-gate  * that is being swapped out.
11377c478bd9Sstevel@tonic-gate  */
11387c478bd9Sstevel@tonic-gate void
11397c478bd9Sstevel@tonic-gate hat_swapout(hat_t *hat)
11407c478bd9Sstevel@tonic-gate {
11417c478bd9Sstevel@tonic-gate 	uintptr_t	vaddr = (uintptr_t)0;
11427c478bd9Sstevel@tonic-gate 	uintptr_t	eaddr = _userlimit;
11437c478bd9Sstevel@tonic-gate 	htable_t	*ht = NULL;
11447c478bd9Sstevel@tonic-gate 	level_t		l;
11457c478bd9Sstevel@tonic-gate 
1146843e1988Sjohnlev 	XPV_DISALLOW_MIGRATE();
11477c478bd9Sstevel@tonic-gate 	/*
11487c478bd9Sstevel@tonic-gate 	 * We can't just call hat_unload(hat, 0, _userlimit...)  here, because
11497c478bd9Sstevel@tonic-gate 	 * seg_spt and shared pagetables can't be swapped out.
11507c478bd9Sstevel@tonic-gate 	 * Take a look at segspt_shmswapout() - it's a big no-op.
11517c478bd9Sstevel@tonic-gate 	 *
11527c478bd9Sstevel@tonic-gate 	 * Instead we'll walk through all the address space and unload
11537c478bd9Sstevel@tonic-gate 	 * any mappings which we are sure are not shared, not locked.
11547c478bd9Sstevel@tonic-gate 	 */
11557c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(vaddr));
11567c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(eaddr));
11577c478bd9Sstevel@tonic-gate 	ASSERT(AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock));
11587c478bd9Sstevel@tonic-gate 	if ((uintptr_t)hat->hat_as->a_userlimit < eaddr)
11597c478bd9Sstevel@tonic-gate 		eaddr = (uintptr_t)hat->hat_as->a_userlimit;
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 	while (vaddr < eaddr) {
11627c478bd9Sstevel@tonic-gate 		(void) htable_walk(hat, &ht, &vaddr, eaddr);
11637c478bd9Sstevel@tonic-gate 		if (ht == NULL)
11647c478bd9Sstevel@tonic-gate 			break;
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 		ASSERT(!IN_VA_HOLE(vaddr));
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 		/*
11697c478bd9Sstevel@tonic-gate 		 * If the page table is shared skip its entire range.
11707c478bd9Sstevel@tonic-gate 		 */
11717c478bd9Sstevel@tonic-gate 		l = ht->ht_level;
11727c478bd9Sstevel@tonic-gate 		if (ht->ht_flags & HTABLE_SHARED_PFN) {
1173ae320ee6Speterte 			vaddr = ht->ht_vaddr + LEVEL_SIZE(l + 1);
11747c478bd9Sstevel@tonic-gate 			htable_release(ht);
11757c478bd9Sstevel@tonic-gate 			ht = NULL;
11767c478bd9Sstevel@tonic-gate 			continue;
11777c478bd9Sstevel@tonic-gate 		}
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 		/*
11807c478bd9Sstevel@tonic-gate 		 * If the page table has no locked entries, unload this one.
11817c478bd9Sstevel@tonic-gate 		 */
11827c478bd9Sstevel@tonic-gate 		if (ht->ht_lock_cnt == 0)
11837c478bd9Sstevel@tonic-gate 			hat_unload(hat, (caddr_t)vaddr, LEVEL_SIZE(l),
11847c478bd9Sstevel@tonic-gate 			    HAT_UNLOAD_UNMAP);
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 		/*
11877c478bd9Sstevel@tonic-gate 		 * If we have a level 0 page table with locked entries,
11887c478bd9Sstevel@tonic-gate 		 * skip the entire page table, otherwise skip just one entry.
11897c478bd9Sstevel@tonic-gate 		 */
11907c478bd9Sstevel@tonic-gate 		if (ht->ht_lock_cnt > 0 && l == 0)
11917c478bd9Sstevel@tonic-gate 			vaddr = ht->ht_vaddr + LEVEL_SIZE(1);
11927c478bd9Sstevel@tonic-gate 		else
11937c478bd9Sstevel@tonic-gate 			vaddr += LEVEL_SIZE(l);
11947c478bd9Sstevel@tonic-gate 	}
11957c478bd9Sstevel@tonic-gate 	if (ht)
11967c478bd9Sstevel@tonic-gate 		htable_release(ht);
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 	/*
11997c478bd9Sstevel@tonic-gate 	 * We're in swapout because the system is low on memory, so
12007c478bd9Sstevel@tonic-gate 	 * go back and flush all the htables off the cached list.
12017c478bd9Sstevel@tonic-gate 	 */
12027c478bd9Sstevel@tonic-gate 	htable_purge_hat(hat);
1203843e1988Sjohnlev 	XPV_ALLOW_MIGRATE();
12047c478bd9Sstevel@tonic-gate }
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate /*
12077c478bd9Sstevel@tonic-gate  * returns number of bytes that have valid mappings in hat.
12087c478bd9Sstevel@tonic-gate  */
12097c478bd9Sstevel@tonic-gate size_t
12107c478bd9Sstevel@tonic-gate hat_get_mapped_size(hat_t *hat)
12117c478bd9Sstevel@tonic-gate {
12127c478bd9Sstevel@tonic-gate 	size_t total = 0;
12137c478bd9Sstevel@tonic-gate 	int l;
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 	for (l = 0; l <= mmu.max_page_level; l++)
12167c478bd9Sstevel@tonic-gate 		total += (hat->hat_pages_mapped[l] << LEVEL_SHIFT(l));
1217250b7ff9Sjosephb 	total += hat->hat_ism_pgcnt;
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 	return (total);
12207c478bd9Sstevel@tonic-gate }
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate /*
12237c478bd9Sstevel@tonic-gate  * enable/disable collection of stats for hat.
12247c478bd9Sstevel@tonic-gate  */
12257c478bd9Sstevel@tonic-gate int
12267c478bd9Sstevel@tonic-gate hat_stats_enable(hat_t *hat)
12277c478bd9Sstevel@tonic-gate {
12287c478bd9Sstevel@tonic-gate 	atomic_add_32(&hat->hat_stats, 1);
12297c478bd9Sstevel@tonic-gate 	return (1);
12307c478bd9Sstevel@tonic-gate }
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate void
12337c478bd9Sstevel@tonic-gate hat_stats_disable(hat_t *hat)
12347c478bd9Sstevel@tonic-gate {
12357c478bd9Sstevel@tonic-gate 	atomic_add_32(&hat->hat_stats, -1);
12367c478bd9Sstevel@tonic-gate }
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate /*
12397c478bd9Sstevel@tonic-gate  * Utility to sync the ref/mod bits from a page table entry to the page_t
12407c478bd9Sstevel@tonic-gate  * We must be holding the mapping list lock when this is called.
12417c478bd9Sstevel@tonic-gate  */
12427c478bd9Sstevel@tonic-gate static void
12437c478bd9Sstevel@tonic-gate hati_sync_pte_to_page(page_t *pp, x86pte_t pte, level_t level)
12447c478bd9Sstevel@tonic-gate {
12457c478bd9Sstevel@tonic-gate 	uint_t	rm = 0;
12467c478bd9Sstevel@tonic-gate 	pgcnt_t	pgcnt;
12477c478bd9Sstevel@tonic-gate 
1248ae115bc7Smrj 	if (PTE_GET(pte, PT_SOFTWARE) >= PT_NOSYNC)
12497c478bd9Sstevel@tonic-gate 		return;
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate 	if (PTE_GET(pte, PT_REF))
12527c478bd9Sstevel@tonic-gate 		rm |= P_REF;
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 	if (PTE_GET(pte, PT_MOD))
12557c478bd9Sstevel@tonic-gate 		rm |= P_MOD;
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 	if (rm == 0)
12587c478bd9Sstevel@tonic-gate 		return;
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 	/*
12617c478bd9Sstevel@tonic-gate 	 * sync to all constituent pages of a large page
12627c478bd9Sstevel@tonic-gate 	 */
12637c478bd9Sstevel@tonic-gate 	ASSERT(x86_hm_held(pp));
12647c478bd9Sstevel@tonic-gate 	pgcnt = page_get_pagecnt(level);
12657c478bd9Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(pp->p_pagenum, pgcnt));
12667c478bd9Sstevel@tonic-gate 	for (; pgcnt > 0; --pgcnt) {
12677c478bd9Sstevel@tonic-gate 		/*
12687c478bd9Sstevel@tonic-gate 		 * hat_page_demote() can't decrease
12697c478bd9Sstevel@tonic-gate 		 * pszc below this mapping size
12707c478bd9Sstevel@tonic-gate 		 * since this large mapping existed after we
12717c478bd9Sstevel@tonic-gate 		 * took mlist lock.
12727c478bd9Sstevel@tonic-gate 		 */
12737c478bd9Sstevel@tonic-gate 		ASSERT(pp->p_szc >= level);
12747c478bd9Sstevel@tonic-gate 		hat_page_setattr(pp, rm);
12757c478bd9Sstevel@tonic-gate 		++pp;
12767c478bd9Sstevel@tonic-gate 	}
12777c478bd9Sstevel@tonic-gate }
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate /*
12807c478bd9Sstevel@tonic-gate  * This the set of PTE bits for PFN, permissions and caching
1281843e1988Sjohnlev  * that are allowed to change on a HAT_LOAD_REMAP
12827c478bd9Sstevel@tonic-gate  */
12837c478bd9Sstevel@tonic-gate #define	PT_REMAP_BITS							\
12847c478bd9Sstevel@tonic-gate 	(PT_PADDR | PT_NX | PT_WRITABLE | PT_WRITETHRU |		\
1285843e1988Sjohnlev 	PT_NOCACHE | PT_PAT_4K | PT_PAT_LARGE | PT_IGNORE | PT_REF | PT_MOD)
12867c478bd9Sstevel@tonic-gate 
1287b193e412Skchow #define	REMAPASSERT(EX)	if (!(EX)) panic("hati_pte_map: " #EX)
12887c478bd9Sstevel@tonic-gate /*
12897c478bd9Sstevel@tonic-gate  * Do the low-level work to get a mapping entered into a HAT's pagetables
12907c478bd9Sstevel@tonic-gate  * and in the mapping list of the associated page_t.
12917c478bd9Sstevel@tonic-gate  */
1292ae115bc7Smrj static int
12937c478bd9Sstevel@tonic-gate hati_pte_map(
12947c478bd9Sstevel@tonic-gate 	htable_t	*ht,
12957c478bd9Sstevel@tonic-gate 	uint_t		entry,
12967c478bd9Sstevel@tonic-gate 	page_t		*pp,
12977c478bd9Sstevel@tonic-gate 	x86pte_t	pte,
12987c478bd9Sstevel@tonic-gate 	int		flags,
12997c478bd9Sstevel@tonic-gate 	void		*pte_ptr)
13007c478bd9Sstevel@tonic-gate {
13017c478bd9Sstevel@tonic-gate 	hat_t		*hat = ht->ht_hat;
13027c478bd9Sstevel@tonic-gate 	x86pte_t	old_pte;
13037c478bd9Sstevel@tonic-gate 	level_t		l = ht->ht_level;
13047c478bd9Sstevel@tonic-gate 	hment_t		*hm;
13057c478bd9Sstevel@tonic-gate 	uint_t		is_consist;
130625540de2SJakub Jermar 	uint_t		is_locked;
1307ae115bc7Smrj 	int		rv = 0;
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate 	/*
131021584dbcSPavel Tatashin 	 * Is this a consistent (ie. need mapping list lock) mapping?
13117c478bd9Sstevel@tonic-gate 	 */
13127c478bd9Sstevel@tonic-gate 	is_consist = (pp != NULL && (flags & HAT_LOAD_NOCONSIST) == 0);
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 	/*
13157c478bd9Sstevel@tonic-gate 	 * Track locked mapping count in the htable.  Do this first,
13167c478bd9Sstevel@tonic-gate 	 * as we track locking even if there already is a mapping present.
13177c478bd9Sstevel@tonic-gate 	 */
131825540de2SJakub Jermar 	is_locked = (flags & HAT_LOAD_LOCK) != 0 && hat != kas.a_hat;
131925540de2SJakub Jermar 	if (is_locked)
13207c478bd9Sstevel@tonic-gate 		HTABLE_LOCK_INC(ht);
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 	/*
13237c478bd9Sstevel@tonic-gate 	 * Acquire the page's mapping list lock and get an hment to use.
13247c478bd9Sstevel@tonic-gate 	 * Note that hment_prepare() might return NULL.
13257c478bd9Sstevel@tonic-gate 	 */
13267c478bd9Sstevel@tonic-gate 	if (is_consist) {
13277c478bd9Sstevel@tonic-gate 		x86_hm_enter(pp);
13287c478bd9Sstevel@tonic-gate 		hm = hment_prepare(ht, entry, pp);
13297c478bd9Sstevel@tonic-gate 	}
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 	/*
13327c478bd9Sstevel@tonic-gate 	 * Set the new pte, retrieving the old one at the same time.
13337c478bd9Sstevel@tonic-gate 	 */
13347c478bd9Sstevel@tonic-gate 	old_pte = x86pte_set(ht, entry, pte, pte_ptr);
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 	/*
133725540de2SJakub Jermar 	 * Did we get a large page / page table collision?
13387c478bd9Sstevel@tonic-gate 	 */
1339ae115bc7Smrj 	if (old_pte == LPAGE_ERROR) {
134025540de2SJakub Jermar 		if (is_locked)
134125540de2SJakub Jermar 			HTABLE_LOCK_DEC(ht);
1342ae115bc7Smrj 		rv = -1;
1343ae115bc7Smrj 		goto done;
13447c478bd9Sstevel@tonic-gate 	}
13457c478bd9Sstevel@tonic-gate 
1346ae115bc7Smrj 	/*
1347ae115bc7Smrj 	 * If the mapping didn't change there is nothing more to do.
1348ae115bc7Smrj 	 */
1349ae115bc7Smrj 	if (PTE_EQUIV(pte, old_pte))
1350ae115bc7Smrj 		goto done;
1351ae115bc7Smrj 
13527c478bd9Sstevel@tonic-gate 	/*
13537c478bd9Sstevel@tonic-gate 	 * Install a new mapping in the page's mapping list
13547c478bd9Sstevel@tonic-gate 	 */
13557c478bd9Sstevel@tonic-gate 	if (!PTE_ISVALID(old_pte)) {
13567c478bd9Sstevel@tonic-gate 		if (is_consist) {
13577c478bd9Sstevel@tonic-gate 			hment_assign(ht, entry, pp, hm);
13587c478bd9Sstevel@tonic-gate 			x86_hm_exit(pp);
13597c478bd9Sstevel@tonic-gate 		} else {
13607c478bd9Sstevel@tonic-gate 			ASSERT(flags & HAT_LOAD_NOCONSIST);
13617c478bd9Sstevel@tonic-gate 		}
136202bc52beSkchow #if defined(__amd64)
136302bc52beSkchow 		if (ht->ht_flags & HTABLE_VLP) {
136402bc52beSkchow 			cpu_t *cpu = CPU;
136502bc52beSkchow 			x86pte_t *vlpptep = cpu->cpu_hat_info->hci_vlp_l2ptes;
136602bc52beSkchow 			VLP_COPY(hat->hat_vlp_ptes, vlpptep);
136702bc52beSkchow 		}
136802bc52beSkchow #endif
13697c478bd9Sstevel@tonic-gate 		HTABLE_INC(ht->ht_valid_cnt);
13707c478bd9Sstevel@tonic-gate 		PGCNT_INC(hat, l);
1371ae115bc7Smrj 		return (rv);
13727c478bd9Sstevel@tonic-gate 	}
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 	/*
13757c478bd9Sstevel@tonic-gate 	 * Remap's are more complicated:
13767c478bd9Sstevel@tonic-gate 	 *  - HAT_LOAD_REMAP must be specified if changing the pfn.
13777c478bd9Sstevel@tonic-gate 	 *    We also require that NOCONSIST be specified.
13787c478bd9Sstevel@tonic-gate 	 *  - Otherwise only permission or caching bits may change.
13797c478bd9Sstevel@tonic-gate 	 */
13807c478bd9Sstevel@tonic-gate 	if (!PTE_ISPAGE(old_pte, l))
13817c478bd9Sstevel@tonic-gate 		panic("non-null/page mapping pte=" FMT_PTE, old_pte);
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate 	if (PTE2PFN(old_pte, l) != PTE2PFN(pte, l)) {
1384b193e412Skchow 		REMAPASSERT(flags & HAT_LOAD_REMAP);
1385b193e412Skchow 		REMAPASSERT(flags & HAT_LOAD_NOCONSIST);
1386ae115bc7Smrj 		REMAPASSERT(PTE_GET(old_pte, PT_SOFTWARE) >= PT_NOCONSIST);
1387b193e412Skchow 		REMAPASSERT(pf_is_memory(PTE2PFN(old_pte, l)) ==
13887c478bd9Sstevel@tonic-gate 		    pf_is_memory(PTE2PFN(pte, l)));
1389b193e412Skchow 		REMAPASSERT(!is_consist);
13907c478bd9Sstevel@tonic-gate 	}
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 	/*
1393843e1988Sjohnlev 	 * We only let remaps change the certain bits in the PTE.
13947c478bd9Sstevel@tonic-gate 	 */
1395843e1988Sjohnlev 	if (PTE_GET(old_pte, ~PT_REMAP_BITS) != PTE_GET(pte, ~PT_REMAP_BITS))
1396843e1988Sjohnlev 		panic("remap bits changed: old_pte="FMT_PTE", pte="FMT_PTE"\n",
1397843e1988Sjohnlev 		    old_pte, pte);
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 	/*
14007c478bd9Sstevel@tonic-gate 	 * We don't create any mapping list entries on a remap, so release
14017c478bd9Sstevel@tonic-gate 	 * any allocated hment after we drop the mapping list lock.
14027c478bd9Sstevel@tonic-gate 	 */
1403ae115bc7Smrj done:
14047c478bd9Sstevel@tonic-gate 	if (is_consist) {
14057c478bd9Sstevel@tonic-gate 		x86_hm_exit(pp);
14067c478bd9Sstevel@tonic-gate 		if (hm != NULL)
14077c478bd9Sstevel@tonic-gate 			hment_free(hm);
14087c478bd9Sstevel@tonic-gate 	}
1409ae115bc7Smrj 	return (rv);
14107c478bd9Sstevel@tonic-gate }
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate /*
1413ae115bc7Smrj  * Internal routine to load a single page table entry. This only fails if
1414ae115bc7Smrj  * we attempt to overwrite a page table link with a large page.
14157c478bd9Sstevel@tonic-gate  */
1416ae115bc7Smrj static int
14177c478bd9Sstevel@tonic-gate hati_load_common(
14187c478bd9Sstevel@tonic-gate 	hat_t		*hat,
14197c478bd9Sstevel@tonic-gate 	uintptr_t	va,
14207c478bd9Sstevel@tonic-gate 	page_t		*pp,
14217c478bd9Sstevel@tonic-gate 	uint_t		attr,
14227c478bd9Sstevel@tonic-gate 	uint_t		flags,
14237c478bd9Sstevel@tonic-gate 	level_t		level,
14247c478bd9Sstevel@tonic-gate 	pfn_t		pfn)
14257c478bd9Sstevel@tonic-gate {
14267c478bd9Sstevel@tonic-gate 	htable_t	*ht;
14277c478bd9Sstevel@tonic-gate 	uint_t		entry;
14287c478bd9Sstevel@tonic-gate 	x86pte_t	pte;
1429ae115bc7Smrj 	int		rv = 0;
14307c478bd9Sstevel@tonic-gate 
1431aac11643Sjosephb 	/*
1432aac11643Sjosephb 	 * The number 16 is arbitrary and here to catch a recursion problem
1433aac11643Sjosephb 	 * early before we blow out the kernel stack.
1434aac11643Sjosephb 	 */
1435aac11643Sjosephb 	++curthread->t_hatdepth;
1436aac11643Sjosephb 	ASSERT(curthread->t_hatdepth < 16);
1437aac11643Sjosephb 
14387c478bd9Sstevel@tonic-gate 	ASSERT(hat == kas.a_hat ||
14397c478bd9Sstevel@tonic-gate 	    AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock));
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate 	if (flags & HAT_LOAD_SHARE)
14427c478bd9Sstevel@tonic-gate 		hat->hat_flags |= HAT_SHARED;
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate 	/*
14457c478bd9Sstevel@tonic-gate 	 * Find the page table that maps this page if it already exists.
14467c478bd9Sstevel@tonic-gate 	 */
14477c478bd9Sstevel@tonic-gate 	ht = htable_lookup(hat, va, level);
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	/*
1450aac11643Sjosephb 	 * We must have HAT_LOAD_NOCONSIST if page_t is NULL.
14517c478bd9Sstevel@tonic-gate 	 */
1452aac11643Sjosephb 	if (pp == NULL)
14537c478bd9Sstevel@tonic-gate 		flags |= HAT_LOAD_NOCONSIST;
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 	if (ht == NULL) {
14567c478bd9Sstevel@tonic-gate 		ht = htable_create(hat, va, level, NULL);
14577c478bd9Sstevel@tonic-gate 		ASSERT(ht != NULL);
14587c478bd9Sstevel@tonic-gate 	}
14597c478bd9Sstevel@tonic-gate 	entry = htable_va2entry(va, ht);
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	/*
14627c478bd9Sstevel@tonic-gate 	 * a bunch of paranoid error checking
14637c478bd9Sstevel@tonic-gate 	 */
14647c478bd9Sstevel@tonic-gate 	ASSERT(ht->ht_busy > 0);
14657c478bd9Sstevel@tonic-gate 	if (ht->ht_vaddr > va || va > HTABLE_LAST_PAGE(ht))
1466903a11ebSrh 		panic("hati_load_common: bad htable %p, va %p",
1467903a11ebSrh 		    (void *)ht, (void *)va);
14687c478bd9Sstevel@tonic-gate 	ASSERT(ht->ht_level == level);
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate 	/*
14717c478bd9Sstevel@tonic-gate 	 * construct the new PTE
14727c478bd9Sstevel@tonic-gate 	 */
14737c478bd9Sstevel@tonic-gate 	if (hat == kas.a_hat)
14747c478bd9Sstevel@tonic-gate 		attr &= ~PROT_USER;
14757c478bd9Sstevel@tonic-gate 	pte = hati_mkpte(pfn, attr, level, flags);
14767c478bd9Sstevel@tonic-gate 	if (hat == kas.a_hat && va >= kernelbase)
14777c478bd9Sstevel@tonic-gate 		PTE_SET(pte, mmu.pt_global);
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate 	/*
14807c478bd9Sstevel@tonic-gate 	 * establish the mapping
14817c478bd9Sstevel@tonic-gate 	 */
1482ae115bc7Smrj 	rv = hati_pte_map(ht, entry, pp, pte, flags, NULL);
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 	/*
14857c478bd9Sstevel@tonic-gate 	 * release the htable and any reserves
14867c478bd9Sstevel@tonic-gate 	 */
14877c478bd9Sstevel@tonic-gate 	htable_release(ht);
1488aac11643Sjosephb 	--curthread->t_hatdepth;
1489ae115bc7Smrj 	return (rv);
14907c478bd9Sstevel@tonic-gate }
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate /*
14937c478bd9Sstevel@tonic-gate  * special case of hat_memload to deal with some kernel addrs for performance
14947c478bd9Sstevel@tonic-gate  */
14957c478bd9Sstevel@tonic-gate static void
14967c478bd9Sstevel@tonic-gate hat_kmap_load(
14977c478bd9Sstevel@tonic-gate 	caddr_t		addr,
14987c478bd9Sstevel@tonic-gate 	page_t		*pp,
14997c478bd9Sstevel@tonic-gate 	uint_t		attr,
15007c478bd9Sstevel@tonic-gate 	uint_t		flags)
15017c478bd9Sstevel@tonic-gate {
15027c478bd9Sstevel@tonic-gate 	uintptr_t	va = (uintptr_t)addr;
15037c478bd9Sstevel@tonic-gate 	x86pte_t	pte;
15047c478bd9Sstevel@tonic-gate 	pfn_t		pfn = page_pptonum(pp);
15057c478bd9Sstevel@tonic-gate 	pgcnt_t		pg_off = mmu_btop(va - mmu.kmap_addr);
15067c478bd9Sstevel@tonic-gate 	htable_t	*ht;
15077c478bd9Sstevel@tonic-gate 	uint_t		entry;
15087c478bd9Sstevel@tonic-gate 	void		*pte_ptr;
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate 	/*
15117c478bd9Sstevel@tonic-gate 	 * construct the requested PTE
15127c478bd9Sstevel@tonic-gate 	 */
15137c478bd9Sstevel@tonic-gate 	attr &= ~PROT_USER;
15147c478bd9Sstevel@tonic-gate 	attr |= HAT_STORECACHING_OK;
15157c478bd9Sstevel@tonic-gate 	pte = hati_mkpte(pfn, attr, 0, flags);
15167c478bd9Sstevel@tonic-gate 	PTE_SET(pte, mmu.pt_global);
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate 	/*
15197c478bd9Sstevel@tonic-gate 	 * Figure out the pte_ptr and htable and use common code to finish up
15207c478bd9Sstevel@tonic-gate 	 */
15217c478bd9Sstevel@tonic-gate 	if (mmu.pae_hat)
15227c478bd9Sstevel@tonic-gate 		pte_ptr = mmu.kmap_ptes + pg_off;
15237c478bd9Sstevel@tonic-gate 	else
15247c478bd9Sstevel@tonic-gate 		pte_ptr = (x86pte32_t *)mmu.kmap_ptes + pg_off;
15257c478bd9Sstevel@tonic-gate 	ht = mmu.kmap_htables[(va - mmu.kmap_htables[0]->ht_vaddr) >>
15267c478bd9Sstevel@tonic-gate 	    LEVEL_SHIFT(1)];
15277c478bd9Sstevel@tonic-gate 	entry = htable_va2entry(va, ht);
1528aac11643Sjosephb 	++curthread->t_hatdepth;
1529aac11643Sjosephb 	ASSERT(curthread->t_hatdepth < 16);
1530ae115bc7Smrj 	(void) hati_pte_map(ht, entry, pp, pte, flags, pte_ptr);
1531aac11643Sjosephb 	--curthread->t_hatdepth;
15327c478bd9Sstevel@tonic-gate }
15337c478bd9Sstevel@tonic-gate 
15347c478bd9Sstevel@tonic-gate /*
15357c478bd9Sstevel@tonic-gate  * hat_memload() - load a translation to the given page struct
15367c478bd9Sstevel@tonic-gate  *
15377c478bd9Sstevel@tonic-gate  * Flags for hat_memload/hat_devload/hat_*attr.
15387c478bd9Sstevel@tonic-gate  *
15397c478bd9Sstevel@tonic-gate  * 	HAT_LOAD	Default flags to load a translation to the page.
15407c478bd9Sstevel@tonic-gate  *
15417c478bd9Sstevel@tonic-gate  * 	HAT_LOAD_LOCK	Lock down mapping resources; hat_map(), hat_memload(),
15427c478bd9Sstevel@tonic-gate  *			and hat_devload().
15437c478bd9Sstevel@tonic-gate  *
15447c478bd9Sstevel@tonic-gate  *	HAT_LOAD_NOCONSIST Do not add mapping to page_t mapping list.
1545ae115bc7Smrj  *			sets PT_NOCONSIST
15467c478bd9Sstevel@tonic-gate  *
15477c478bd9Sstevel@tonic-gate  *	HAT_LOAD_SHARE	A flag to hat_memload() to indicate h/w page tables
15487c478bd9Sstevel@tonic-gate  *			that map some user pages (not kas) is shared by more
15497c478bd9Sstevel@tonic-gate  *			than one process (eg. ISM).
15507c478bd9Sstevel@tonic-gate  *
15517c478bd9Sstevel@tonic-gate  *	HAT_LOAD_REMAP	Reload a valid pte with a different page frame.
15527c478bd9Sstevel@tonic-gate  *
15537c478bd9Sstevel@tonic-gate  *	HAT_NO_KALLOC	Do not kmem_alloc while creating the mapping; at this
15547c478bd9Sstevel@tonic-gate  *			point, it's setting up mapping to allocate internal
15557c478bd9Sstevel@tonic-gate  *			hat layer data structures.  This flag forces hat layer
15567c478bd9Sstevel@tonic-gate  *			to tap its reserves in order to prevent infinite
15577c478bd9Sstevel@tonic-gate  *			recursion.
15587c478bd9Sstevel@tonic-gate  *
15597c478bd9Sstevel@tonic-gate  * The following is a protection attribute (like PROT_READ, etc.)
15607c478bd9Sstevel@tonic-gate  *
1561ae115bc7Smrj  *	HAT_NOSYNC	set PT_NOSYNC - this mapping's ref/mod bits
15627c478bd9Sstevel@tonic-gate  *			are never cleared.
15637c478bd9Sstevel@tonic-gate  *
15647c478bd9Sstevel@tonic-gate  * Installing new valid PTE's and creation of the mapping list
15657c478bd9Sstevel@tonic-gate  * entry are controlled under the same lock. It's derived from the
15667c478bd9Sstevel@tonic-gate  * page_t being mapped.
15677c478bd9Sstevel@tonic-gate  */
15687c478bd9Sstevel@tonic-gate static uint_t supported_memload_flags =
15697c478bd9Sstevel@tonic-gate 	HAT_LOAD | HAT_LOAD_LOCK | HAT_LOAD_ADV | HAT_LOAD_NOCONSIST |
15707c478bd9Sstevel@tonic-gate 	HAT_LOAD_SHARE | HAT_NO_KALLOC | HAT_LOAD_REMAP | HAT_LOAD_TEXT;
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate void
15737c478bd9Sstevel@tonic-gate hat_memload(
15747c478bd9Sstevel@tonic-gate 	hat_t		*hat,
15757c478bd9Sstevel@tonic-gate 	caddr_t		addr,
15767c478bd9Sstevel@tonic-gate 	page_t		*pp,
15777c478bd9Sstevel@tonic-gate 	uint_t		attr,
15787c478bd9Sstevel@tonic-gate 	uint_t		flags)
15797c478bd9Sstevel@tonic-gate {
15807c478bd9Sstevel@tonic-gate 	uintptr_t	va = (uintptr_t)addr;
15817c478bd9Sstevel@tonic-gate 	level_t		level = 0;
15827c478bd9Sstevel@tonic-gate 	pfn_t		pfn = page_pptonum(pp);
15837c478bd9Sstevel@tonic-gate 
1584843e1988Sjohnlev 	XPV_DISALLOW_MIGRATE();
15857c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(va));
1586ae115bc7Smrj 	ASSERT(hat == kas.a_hat || va < _userlimit);
15877c478bd9Sstevel@tonic-gate 	ASSERT(hat == kas.a_hat ||
15887c478bd9Sstevel@tonic-gate 	    AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock));
15897c478bd9Sstevel@tonic-gate 	ASSERT((flags & supported_memload_flags) == flags);
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate 	ASSERT(!IN_VA_HOLE(va));
15927c478bd9Sstevel@tonic-gate 	ASSERT(!PP_ISFREE(pp));
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 	/*
15957c478bd9Sstevel@tonic-gate 	 * kernel address special case for performance.
15967c478bd9Sstevel@tonic-gate 	 */
15977c478bd9Sstevel@tonic-gate 	if (mmu.kmap_addr <= va && va < mmu.kmap_eaddr) {
15987c478bd9Sstevel@tonic-gate 		ASSERT(hat == kas.a_hat);
15997c478bd9Sstevel@tonic-gate 		hat_kmap_load(addr, pp, attr, flags);
1600843e1988Sjohnlev 		XPV_ALLOW_MIGRATE();
16017c478bd9Sstevel@tonic-gate 		return;
16027c478bd9Sstevel@tonic-gate 	}
16037c478bd9Sstevel@tonic-gate 
16047c478bd9Sstevel@tonic-gate 	/*
16057c478bd9Sstevel@tonic-gate 	 * This is used for memory with normal caching enabled, so
16067c478bd9Sstevel@tonic-gate 	 * always set HAT_STORECACHING_OK.
16077c478bd9Sstevel@tonic-gate 	 */
16087c478bd9Sstevel@tonic-gate 	attr |= HAT_STORECACHING_OK;
1609ae115bc7Smrj 	if (hati_load_common(hat, va, pp, attr, flags, level, pfn) != 0)
1610ae115bc7Smrj 		panic("unexpected hati_load_common() failure");
1611843e1988Sjohnlev 	XPV_ALLOW_MIGRATE();
16127c478bd9Sstevel@tonic-gate }
16137c478bd9Sstevel@tonic-gate 
161405d3dc4bSpaulsan /* ARGSUSED */
161505d3dc4bSpaulsan void
161605d3dc4bSpaulsan hat_memload_region(struct hat *hat, caddr_t addr, struct page *pp,
161705d3dc4bSpaulsan     uint_t attr, uint_t flags, hat_region_cookie_t rcookie)
161805d3dc4bSpaulsan {
161905d3dc4bSpaulsan 	hat_memload(hat, addr, pp, attr, flags);
162005d3dc4bSpaulsan }
162105d3dc4bSpaulsan 
16227c478bd9Sstevel@tonic-gate /*
16237c478bd9Sstevel@tonic-gate  * Load the given array of page structs using large pages when possible
16247c478bd9Sstevel@tonic-gate  */
16257c478bd9Sstevel@tonic-gate void
16267c478bd9Sstevel@tonic-gate hat_memload_array(
16277c478bd9Sstevel@tonic-gate 	hat_t		*hat,
16287c478bd9Sstevel@tonic-gate 	caddr_t		addr,
16297c478bd9Sstevel@tonic-gate 	size_t		len,
16307c478bd9Sstevel@tonic-gate 	page_t		**pages,
16317c478bd9Sstevel@tonic-gate 	uint_t		attr,
16327c478bd9Sstevel@tonic-gate 	uint_t		flags)
16337c478bd9Sstevel@tonic-gate {
16347c478bd9Sstevel@tonic-gate 	uintptr_t	va = (uintptr_t)addr;
16357c478bd9Sstevel@tonic-gate 	uintptr_t	eaddr = va + len;
16367c478bd9Sstevel@tonic-gate 	level_t		level;
16377c478bd9Sstevel@tonic-gate 	size_t		pgsize;
16387c478bd9Sstevel@tonic-gate 	pgcnt_t		pgindx = 0;
16397c478bd9Sstevel@tonic-gate 	pfn_t		pfn;
16407c478bd9Sstevel@tonic-gate 	pgcnt_t		i;
16417c478bd9Sstevel@tonic-gate 
1642843e1988Sjohnlev 	XPV_DISALLOW_MIGRATE();
16437c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(va));
1644ae115bc7Smrj 	ASSERT(hat == kas.a_hat || va + len <= _userlimit);
16457c478bd9Sstevel@tonic-gate 	ASSERT(hat == kas.a_hat ||
16467c478bd9Sstevel@tonic-gate 	    AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock));
16477c478bd9Sstevel@tonic-gate 	ASSERT((flags & supported_memload_flags) == flags);
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 	/*
16507c478bd9Sstevel@tonic-gate 	 * memload is used for memory with full caching enabled, so
16517c478bd9Sstevel@tonic-gate 	 * set HAT_STORECACHING_OK.
16527c478bd9Sstevel@tonic-gate 	 */
16537c478bd9Sstevel@tonic-gate 	attr |= HAT_STORECACHING_OK;
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 	/*
16567c478bd9Sstevel@tonic-gate 	 * handle all pages using largest possible pagesize
16577c478bd9Sstevel@tonic-gate 	 */
16587c478bd9Sstevel@tonic-gate 	while (va < eaddr) {
16597c478bd9Sstevel@tonic-gate 		/*
16607c478bd9Sstevel@tonic-gate 		 * decide what level mapping to use (ie. pagesize)
16617c478bd9Sstevel@tonic-gate 		 */
16627c478bd9Sstevel@tonic-gate 		pfn = page_pptonum(pages[pgindx]);
16637c478bd9Sstevel@tonic-gate 		for (level = mmu.max_page_level; ; --level) {
16647c478bd9Sstevel@tonic-gate 			pgsize = LEVEL_SIZE(level);
16657c478bd9Sstevel@tonic-gate 			if (level == 0)
16667c478bd9Sstevel@tonic-gate 				break;
1667ae115bc7Smrj 
16687c478bd9Sstevel@tonic-gate 			if (!IS_P2ALIGNED(va, pgsize) ||
16697c478bd9Sstevel@tonic-gate 			    (eaddr - va) < pgsize ||
1670ae115bc7Smrj 			    !IS_P2ALIGNED(pfn_to_pa(pfn), pgsize))
16717c478bd9Sstevel@tonic-gate 				continue;
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 			/*
16747c478bd9Sstevel@tonic-gate 			 * To use a large mapping of this size, all the
16757c478bd9Sstevel@tonic-gate 			 * pages we are passed must be sequential subpages
16767c478bd9Sstevel@tonic-gate 			 * of the large page.
16777c478bd9Sstevel@tonic-gate 			 * hat_page_demote() can't change p_szc because
16787c478bd9Sstevel@tonic-gate 			 * all pages are locked.
16797c478bd9Sstevel@tonic-gate 			 */
16807c478bd9Sstevel@tonic-gate 			if (pages[pgindx]->p_szc >= level) {
16817c478bd9Sstevel@tonic-gate 				for (i = 0; i < mmu_btop(pgsize); ++i) {
16827c478bd9Sstevel@tonic-gate 					if (pfn + i !=
16837c478bd9Sstevel@tonic-gate 					    page_pptonum(pages[pgindx + i]))
16847c478bd9Sstevel@tonic-gate 						break;
16857c478bd9Sstevel@tonic-gate 					ASSERT(pages[pgindx + i]->p_szc >=
16867c478bd9Sstevel@tonic-gate 					    level);
16877c478bd9Sstevel@tonic-gate 					ASSERT(pages[pgindx] + i ==
16887c478bd9Sstevel@tonic-gate 					    pages[pgindx + i]);
16897c478bd9Sstevel@tonic-gate 				}
169002bc52beSkchow 				if (i == mmu_btop(pgsize)) {
169102bc52beSkchow #ifdef DEBUG
169202bc52beSkchow 					if (level == 2)
169302bc52beSkchow 						map1gcnt++;
169402bc52beSkchow #endif
16957c478bd9Sstevel@tonic-gate 					break;
169602bc52beSkchow 				}
16977c478bd9Sstevel@tonic-gate 			}
16987c478bd9Sstevel@tonic-gate 		}
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 		/*
1701ae115bc7Smrj 		 * Load this page mapping. If the load fails, try a smaller
1702ae115bc7Smrj 		 * pagesize.
17037c478bd9Sstevel@tonic-gate 		 */
17047c478bd9Sstevel@tonic-gate 		ASSERT(!IN_VA_HOLE(va));
1705ae115bc7Smrj 		while (hati_load_common(hat, va, pages[pgindx], attr,
1706250b7ff9Sjosephb 		    flags, level, pfn) != 0) {
1707ae115bc7Smrj 			if (level == 0)
1708ae115bc7Smrj 				panic("unexpected hati_load_common() failure");
1709ae115bc7Smrj 			--level;
1710ae115bc7Smrj 			pgsize = LEVEL_SIZE(level);
1711ae115bc7Smrj 		}
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate 		/*
17147c478bd9Sstevel@tonic-gate 		 * move to next page
17157c478bd9Sstevel@tonic-gate 		 */
17167c478bd9Sstevel@tonic-gate 		va += pgsize;
17177c478bd9Sstevel@tonic-gate 		pgindx += mmu_btop(pgsize);
17187c478bd9Sstevel@tonic-gate 	}
1719843e1988Sjohnlev 	XPV_ALLOW_MIGRATE();
17207c478bd9Sstevel@tonic-gate }
17217c478bd9Sstevel@tonic-gate 
172205d3dc4bSpaulsan /* ARGSUSED */
172305d3dc4bSpaulsan void
172405d3dc4bSpaulsan hat_memload_array_region(struct hat *hat, caddr_t addr, size_t len,
172505d3dc4bSpaulsan     struct page **pps, uint_t attr, uint_t flags,
172605d3dc4bSpaulsan     hat_region_cookie_t rcookie)
172705d3dc4bSpaulsan {
172805d3dc4bSpaulsan 	hat_memload_array(hat, addr, len, pps, attr, flags);
172905d3dc4bSpaulsan }
173005d3dc4bSpaulsan 
17317c478bd9Sstevel@tonic-gate /*
17327c478bd9Sstevel@tonic-gate  * void hat_devload(hat, addr, len, pf, attr, flags)
17337c478bd9Sstevel@tonic-gate  *	load/lock the given page frame number
17347c478bd9Sstevel@tonic-gate  *
17357c478bd9Sstevel@tonic-gate  * Advisory ordering attributes. Apply only to device mappings.
17367c478bd9Sstevel@tonic-gate  *
17377c478bd9Sstevel@tonic-gate  * HAT_STRICTORDER: the CPU must issue the references in order, as the
17387c478bd9Sstevel@tonic-gate  *	programmer specified.  This is the default.
17397c478bd9Sstevel@tonic-gate  * HAT_UNORDERED_OK: the CPU may reorder the references (this is all kinds
17407c478bd9Sstevel@tonic-gate  *	of reordering; store or load with store or load).
17417c478bd9Sstevel@tonic-gate  * HAT_MERGING_OK: merging and batching: the CPU may merge individual stores
17427c478bd9Sstevel@tonic-gate  *	to consecutive locations (for example, turn two consecutive byte
17437c478bd9Sstevel@tonic-gate  *	stores into one halfword store), and it may batch individual loads
17447c478bd9Sstevel@tonic-gate  *	(for example, turn two consecutive byte loads into one halfword load).
17457c478bd9Sstevel@tonic-gate  *	This also implies re-ordering.
17467c478bd9Sstevel@tonic-gate  * HAT_LOADCACHING_OK: the CPU may cache the data it fetches and reuse it
17477c478bd9Sstevel@tonic-gate  *	until another store occurs.  The default is to fetch new data
17487c478bd9Sstevel@tonic-gate  *	on every load.  This also implies merging.
17497c478bd9Sstevel@tonic-gate  * HAT_STORECACHING_OK: the CPU may keep the data in the cache and push it to
17507c478bd9Sstevel@tonic-gate  *	the device (perhaps with other data) at a later time.  The default is
17517c478bd9Sstevel@tonic-gate  *	to push the data right away.  This also implies load caching.
17527c478bd9Sstevel@tonic-gate  *
17537c478bd9Sstevel@tonic-gate  * Equivalent of hat_memload(), but can be used for device memory where
17547c478bd9Sstevel@tonic-gate  * there are no page_t's and we support additional flags (write merging, etc).
17557c478bd9Sstevel@tonic-gate  * Note that we can have large page mappings with this interface.
17567c478bd9Sstevel@tonic-gate  */
17577c478bd9Sstevel@tonic-gate int supported_devload_flags = HAT_LOAD | HAT_LOAD_LOCK |
17587c478bd9Sstevel@tonic-gate 	HAT_LOAD_NOCONSIST | HAT_STRICTORDER | HAT_UNORDERED_OK |
17597c478bd9Sstevel@tonic-gate 	HAT_MERGING_OK | HAT_LOADCACHING_OK | HAT_STORECACHING_OK;
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate void
17627c478bd9Sstevel@tonic-gate hat_devload(
17637c478bd9Sstevel@tonic-gate 	hat_t		*hat,
17647c478bd9Sstevel@tonic-gate 	caddr_t		addr,
17657c478bd9Sstevel@tonic-gate 	size_t		len,
17667c478bd9Sstevel@tonic-gate 	pfn_t		pfn,
17677c478bd9Sstevel@tonic-gate 	uint_t		attr,
17687c478bd9Sstevel@tonic-gate 	int		flags)
17697c478bd9Sstevel@tonic-gate {
17707c478bd9Sstevel@tonic-gate 	uintptr_t	va = ALIGN2PAGE(addr);
17717c478bd9Sstevel@tonic-gate 	uintptr_t	eva = va + len;
17727c478bd9Sstevel@tonic-gate 	level_t		level;
17737c478bd9Sstevel@tonic-gate 	size_t		pgsize;
17747c478bd9Sstevel@tonic-gate 	page_t		*pp;
17757c478bd9Sstevel@tonic-gate 	int		f;	/* per PTE copy of flags  - maybe modified */
17767c478bd9Sstevel@tonic-gate 	uint_t		a;	/* per PTE copy of attr */
17777c478bd9Sstevel@tonic-gate 
1778843e1988Sjohnlev 	XPV_DISALLOW_MIGRATE();
17797c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(va));
1780ae115bc7Smrj 	ASSERT(hat == kas.a_hat || eva <= _userlimit);
17817c478bd9Sstevel@tonic-gate 	ASSERT(hat == kas.a_hat ||
17827c478bd9Sstevel@tonic-gate 	    AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock));
17837c478bd9Sstevel@tonic-gate 	ASSERT((flags & supported_devload_flags) == flags);
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 	/*
17867c478bd9Sstevel@tonic-gate 	 * handle all pages
17877c478bd9Sstevel@tonic-gate 	 */
17887c478bd9Sstevel@tonic-gate 	while (va < eva) {
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate 		/*
17917c478bd9Sstevel@tonic-gate 		 * decide what level mapping to use (ie. pagesize)
17927c478bd9Sstevel@tonic-gate 		 */
17937c478bd9Sstevel@tonic-gate 		for (level = mmu.max_page_level; ; --level) {
17947c478bd9Sstevel@tonic-gate 			pgsize = LEVEL_SIZE(level);
17957c478bd9Sstevel@tonic-gate 			if (level == 0)
17967c478bd9Sstevel@tonic-gate 				break;
17977c478bd9Sstevel@tonic-gate 			if (IS_P2ALIGNED(va, pgsize) &&
17987c478bd9Sstevel@tonic-gate 			    (eva - va) >= pgsize &&
179902bc52beSkchow 			    IS_P2ALIGNED(pfn, mmu_btop(pgsize))) {
180002bc52beSkchow #ifdef DEBUG
180102bc52beSkchow 				if (level == 2)
180202bc52beSkchow 					map1gcnt++;
180302bc52beSkchow #endif
18047c478bd9Sstevel@tonic-gate 				break;
180502bc52beSkchow 			}
18067c478bd9Sstevel@tonic-gate 		}
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate 		/*
1809ae115bc7Smrj 		 * If this is just memory then allow caching (this happens
18107c478bd9Sstevel@tonic-gate 		 * for the nucleus pages) - though HAT_PLAT_NOCACHE can be used
1811ae115bc7Smrj 		 * to override that. If we don't have a page_t then make sure
18127c478bd9Sstevel@tonic-gate 		 * NOCONSIST is set.
18137c478bd9Sstevel@tonic-gate 		 */
18147c478bd9Sstevel@tonic-gate 		a = attr;
18157c478bd9Sstevel@tonic-gate 		f = flags;
1816843e1988Sjohnlev 		if (!pf_is_memory(pfn))
1817843e1988Sjohnlev 			f |= HAT_LOAD_NOCONSIST;
1818843e1988Sjohnlev 		else if (!(a & HAT_PLAT_NOCACHE))
1819843e1988Sjohnlev 			a |= HAT_STORECACHING_OK;
18207c478bd9Sstevel@tonic-gate 
1821843e1988Sjohnlev 		if (f & HAT_LOAD_NOCONSIST)
18227c478bd9Sstevel@tonic-gate 			pp = NULL;
1823843e1988Sjohnlev 		else
1824843e1988Sjohnlev 			pp = page_numtopp_nolock(pfn);
18257c478bd9Sstevel@tonic-gate 
1826e803c1e6SPrakash Sangappa 		/*
1827e803c1e6SPrakash Sangappa 		 * Check to make sure we are really trying to map a valid
1828e803c1e6SPrakash Sangappa 		 * memory page. The caller wishing to intentionally map
1829e803c1e6SPrakash Sangappa 		 * free memory pages will have passed the HAT_LOAD_NOCONSIST
1830e803c1e6SPrakash Sangappa 		 * flag, then pp will be NULL.
1831e803c1e6SPrakash Sangappa 		 */
1832e803c1e6SPrakash Sangappa 		if (pp != NULL) {
1833e803c1e6SPrakash Sangappa 			if (PP_ISFREE(pp)) {
1834e803c1e6SPrakash Sangappa 				panic("hat_devload: loading "
1835e803c1e6SPrakash Sangappa 				    "a mapping to free page %p", (void *)pp);
1836e803c1e6SPrakash Sangappa 			}
1837e803c1e6SPrakash Sangappa 
1838e803c1e6SPrakash Sangappa 			if (!PAGE_LOCKED(pp) && !PP_ISNORELOC(pp)) {
1839e803c1e6SPrakash Sangappa 				panic("hat_devload: loading a mapping "
1840e803c1e6SPrakash Sangappa 				    "to an unlocked page %p",
1841e803c1e6SPrakash Sangappa 				    (void *)pp);
1842e803c1e6SPrakash Sangappa 			}
1843e803c1e6SPrakash Sangappa 		}
1844e803c1e6SPrakash Sangappa 
18457c478bd9Sstevel@tonic-gate 		/*
18467c478bd9Sstevel@tonic-gate 		 * load this page mapping
18477c478bd9Sstevel@tonic-gate 		 */
18487c478bd9Sstevel@tonic-gate 		ASSERT(!IN_VA_HOLE(va));
1849ae115bc7Smrj 		while (hati_load_common(hat, va, pp, a, f, level, pfn) != 0) {
1850ae115bc7Smrj 			if (level == 0)
1851ae115bc7Smrj 				panic("unexpected hati_load_common() failure");
1852ae115bc7Smrj 			--level;
1853ae115bc7Smrj 			pgsize = LEVEL_SIZE(level);
1854ae115bc7Smrj 		}
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate 		/*
18577c478bd9Sstevel@tonic-gate 		 * move to next page
18587c478bd9Sstevel@tonic-gate 		 */
18597c478bd9Sstevel@tonic-gate 		va += pgsize;
18607c478bd9Sstevel@tonic-gate 		pfn += mmu_btop(pgsize);
18617c478bd9Sstevel@tonic-gate 	}
1862843e1988Sjohnlev 	XPV_ALLOW_MIGRATE();
18637c478bd9Sstevel@tonic-gate }
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate /*
18667c478bd9Sstevel@tonic-gate  * void hat_unlock(hat, addr, len)
18677c478bd9Sstevel@tonic-gate  *	unlock the mappings to a given range of addresses
18687c478bd9Sstevel@tonic-gate  *
18697c478bd9Sstevel@tonic-gate  * Locks are tracked by ht_lock_cnt in the htable.
18707c478bd9Sstevel@tonic-gate  */
18717c478bd9Sstevel@tonic-gate void
18727c478bd9Sstevel@tonic-gate hat_unlock(hat_t *hat, caddr_t addr, size_t len)
18737c478bd9Sstevel@tonic-gate {
18747c478bd9Sstevel@tonic-gate 	uintptr_t	vaddr = (uintptr_t)addr;
18757c478bd9Sstevel@tonic-gate 	uintptr_t	eaddr = vaddr + len;
18767c478bd9Sstevel@tonic-gate 	htable_t	*ht = NULL;
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 	/*
18797c478bd9Sstevel@tonic-gate 	 * kernel entries are always locked, we don't track lock counts
18807c478bd9Sstevel@tonic-gate 	 */
1881ae115bc7Smrj 	ASSERT(hat == kas.a_hat || eaddr <= _userlimit);
18827c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(vaddr));
18837c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(eaddr));
18847c478bd9Sstevel@tonic-gate 	if (hat == kas.a_hat)
18857c478bd9Sstevel@tonic-gate 		return;
18867c478bd9Sstevel@tonic-gate 	if (eaddr > _userlimit)
18877c478bd9Sstevel@tonic-gate 		panic("hat_unlock() address out of range - above _userlimit");
18887c478bd9Sstevel@tonic-gate 
1889843e1988Sjohnlev 	XPV_DISALLOW_MIGRATE();
18907c478bd9Sstevel@tonic-gate 	ASSERT(AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock));
18917c478bd9Sstevel@tonic-gate 	while (vaddr < eaddr) {
18927c478bd9Sstevel@tonic-gate 		(void) htable_walk(hat, &ht, &vaddr, eaddr);
18937c478bd9Sstevel@tonic-gate 		if (ht == NULL)
18947c478bd9Sstevel@tonic-gate 			break;
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate 		ASSERT(!IN_VA_HOLE(vaddr));
18977c478bd9Sstevel@tonic-gate 
18987c478bd9Sstevel@tonic-gate 		if (ht->ht_lock_cnt < 1)
18997c478bd9Sstevel@tonic-gate 			panic("hat_unlock(): lock_cnt < 1, "
1900903a11ebSrh 			    "htable=%p, vaddr=%p\n", (void *)ht, (void *)vaddr);
19017c478bd9Sstevel@tonic-gate 		HTABLE_LOCK_DEC(ht);
19027c478bd9Sstevel@tonic-gate 
19037c478bd9Sstevel@tonic-gate 		vaddr += LEVEL_SIZE(ht->ht_level);
19047c478bd9Sstevel@tonic-gate 	}
19057c478bd9Sstevel@tonic-gate 	if (ht)
19067c478bd9Sstevel@tonic-gate 		htable_release(ht);
1907843e1988Sjohnlev 	XPV_ALLOW_MIGRATE();
19087c478bd9Sstevel@tonic-gate }
19097c478bd9Sstevel@tonic-gate 
191005d3dc4bSpaulsan /* ARGSUSED */
191105d3dc4bSpaulsan void
19127dacfc44Spaulsan hat_unlock_region(struct hat *hat, caddr_t addr, size_t len,
191305d3dc4bSpaulsan     hat_region_cookie_t rcookie)
191405d3dc4bSpaulsan {
191505d3dc4bSpaulsan 	panic("No shared region support on x86");
191605d3dc4bSpaulsan }
191705d3dc4bSpaulsan 
1918843e1988Sjohnlev #if !defined(__xpv)
19197c478bd9Sstevel@tonic-gate /*
19207c478bd9Sstevel@tonic-gate  * Cross call service routine to demap a virtual page on
19217c478bd9Sstevel@tonic-gate  * the current CPU or flush all mappings in TLB.
19227c478bd9Sstevel@tonic-gate  */
19237c478bd9Sstevel@tonic-gate /*ARGSUSED*/
19247c478bd9Sstevel@tonic-gate static int
19257c478bd9Sstevel@tonic-gate hati_demap_func(xc_arg_t a1, xc_arg_t a2, xc_arg_t a3)
19267c478bd9Sstevel@tonic-gate {
19277c478bd9Sstevel@tonic-gate 	hat_t	*hat = (hat_t *)a1;
19287c478bd9Sstevel@tonic-gate 	caddr_t	addr = (caddr_t)a2;
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate 	/*
19317c478bd9Sstevel@tonic-gate 	 * If the target hat isn't the kernel and this CPU isn't operating
19327c478bd9Sstevel@tonic-gate 	 * in the target hat, we can ignore the cross call.
19337c478bd9Sstevel@tonic-gate 	 */
19347c478bd9Sstevel@tonic-gate 	if (hat != kas.a_hat && hat != CPU->cpu_current_hat)
19357c478bd9Sstevel@tonic-gate 		return (0);
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate 	/*
19387c478bd9Sstevel@tonic-gate 	 * For a normal address, we just flush one page mapping
19397c478bd9Sstevel@tonic-gate 	 */
19407c478bd9Sstevel@tonic-gate 	if ((uintptr_t)addr != DEMAP_ALL_ADDR) {
1941ae115bc7Smrj 		mmu_tlbflush_entry(addr);
19427c478bd9Sstevel@tonic-gate 		return (0);
19437c478bd9Sstevel@tonic-gate 	}
19447c478bd9Sstevel@tonic-gate 
19457c478bd9Sstevel@tonic-gate 	/*
19467c478bd9Sstevel@tonic-gate 	 * Otherwise we reload cr3 to effect a complete TLB flush.
19477c478bd9Sstevel@tonic-gate 	 *
19487c478bd9Sstevel@tonic-gate 	 * A reload of cr3 on a VLP process also means we must also recopy in
19497c478bd9Sstevel@tonic-gate 	 * the pte values from the struct hat
19507c478bd9Sstevel@tonic-gate 	 */
19517c478bd9Sstevel@tonic-gate 	if (hat->hat_flags & HAT_VLP) {
19527c478bd9Sstevel@tonic-gate #if defined(__amd64)
19537c478bd9Sstevel@tonic-gate 		x86pte_t *vlpptep = CPU->cpu_hat_info->hci_vlp_l2ptes;
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 		VLP_COPY(hat->hat_vlp_ptes, vlpptep);
19567c478bd9Sstevel@tonic-gate #elif defined(__i386)
19577c478bd9Sstevel@tonic-gate 		reload_pae32(hat, CPU);
19587c478bd9Sstevel@tonic-gate #endif
19597c478bd9Sstevel@tonic-gate 	}
19607c478bd9Sstevel@tonic-gate 	reload_cr3();
19617c478bd9Sstevel@tonic-gate 	return (0);
19627c478bd9Sstevel@tonic-gate }
19637c478bd9Sstevel@tonic-gate 
196495c0a3c8Sjosephb /*
196595c0a3c8Sjosephb  * Flush all TLB entries, including global (ie. kernel) ones.
196695c0a3c8Sjosephb  */
196795c0a3c8Sjosephb static void
196895c0a3c8Sjosephb flush_all_tlb_entries(void)
196995c0a3c8Sjosephb {
197095c0a3c8Sjosephb 	ulong_t cr4 = getcr4();
197195c0a3c8Sjosephb 
197295c0a3c8Sjosephb 	if (cr4 & CR4_PGE) {
197395c0a3c8Sjosephb 		setcr4(cr4 & ~(ulong_t)CR4_PGE);
197495c0a3c8Sjosephb 		setcr4(cr4);
197595c0a3c8Sjosephb 
197695c0a3c8Sjosephb 		/*
197795c0a3c8Sjosephb 		 * 32 bit PAE also needs to always reload_cr3()
197895c0a3c8Sjosephb 		 */
197995c0a3c8Sjosephb 		if (mmu.max_level == 2)
198095c0a3c8Sjosephb 			reload_cr3();
198195c0a3c8Sjosephb 	} else {
198295c0a3c8Sjosephb 		reload_cr3();
198395c0a3c8Sjosephb 	}
198495c0a3c8Sjosephb }
198595c0a3c8Sjosephb 
198695c0a3c8Sjosephb #define	TLB_CPU_HALTED	(01ul)
198795c0a3c8Sjosephb #define	TLB_INVAL_ALL	(02ul)
198895c0a3c8Sjosephb #define	CAS_TLB_INFO(cpu, old, new)	\
198995c0a3c8Sjosephb 	caslong((ulong_t *)&(cpu)->cpu_m.mcpu_tlb_info, (old), (new))
199095c0a3c8Sjosephb 
199195c0a3c8Sjosephb /*
199295c0a3c8Sjosephb  * Record that a CPU is going idle
199395c0a3c8Sjosephb  */
199495c0a3c8Sjosephb void
199595c0a3c8Sjosephb tlb_going_idle(void)
199695c0a3c8Sjosephb {
199795c0a3c8Sjosephb 	atomic_or_long((ulong_t *)&CPU->cpu_m.mcpu_tlb_info, TLB_CPU_HALTED);
199895c0a3c8Sjosephb }
199995c0a3c8Sjosephb 
200095c0a3c8Sjosephb /*
200195c0a3c8Sjosephb  * Service a delayed TLB flush if coming out of being idle.
200221584dbcSPavel Tatashin  * It will be called from cpu idle notification with interrupt disabled.
200395c0a3c8Sjosephb  */
200495c0a3c8Sjosephb void
200595c0a3c8Sjosephb tlb_service(void)
200695c0a3c8Sjosephb {
200795c0a3c8Sjosephb 	ulong_t tlb_info;
200895c0a3c8Sjosephb 	ulong_t found;
200995c0a3c8Sjosephb 
201095c0a3c8Sjosephb 	/*
201195c0a3c8Sjosephb 	 * We only have to do something if coming out of being idle.
201295c0a3c8Sjosephb 	 */
201395c0a3c8Sjosephb 	tlb_info = CPU->cpu_m.mcpu_tlb_info;
201495c0a3c8Sjosephb 	if (tlb_info & TLB_CPU_HALTED) {
201595c0a3c8Sjosephb 		ASSERT(CPU->cpu_current_hat == kas.a_hat);
201695c0a3c8Sjosephb 
201795c0a3c8Sjosephb 		/*
201895c0a3c8Sjosephb 		 * Atomic clear and fetch of old state.
201995c0a3c8Sjosephb 		 */
202095c0a3c8Sjosephb 		while ((found = CAS_TLB_INFO(CPU, tlb_info, 0)) != tlb_info) {
202195c0a3c8Sjosephb 			ASSERT(found & TLB_CPU_HALTED);
202295c0a3c8Sjosephb 			tlb_info = found;
202395c0a3c8Sjosephb 			SMT_PAUSE();
202495c0a3c8Sjosephb 		}
202595c0a3c8Sjosephb 		if (tlb_info & TLB_INVAL_ALL)
202695c0a3c8Sjosephb 			flush_all_tlb_entries();
202795c0a3c8Sjosephb 	}
202895c0a3c8Sjosephb }
2029843e1988Sjohnlev #endif /* !__xpv */
203095c0a3c8Sjosephb 
20317c478bd9Sstevel@tonic-gate /*
20327c478bd9Sstevel@tonic-gate  * Internal routine to do cross calls to invalidate a range of pages on
20337c478bd9Sstevel@tonic-gate  * all CPUs using a given hat.
20347c478bd9Sstevel@tonic-gate  */
20357c478bd9Sstevel@tonic-gate void
2036ae115bc7Smrj hat_tlb_inval(hat_t *hat, uintptr_t va)
20377c478bd9Sstevel@tonic-gate {
20387c478bd9Sstevel@tonic-gate 	extern int	flushes_require_xcalls;	/* from mp_startup.c */
20397c478bd9Sstevel@tonic-gate 	cpuset_t	justme;
2040ae115bc7Smrj 	cpuset_t	cpus_to_shootdown;
2041843e1988Sjohnlev #ifndef __xpv
2042843e1988Sjohnlev 	cpuset_t	check_cpus;
204395c0a3c8Sjosephb 	cpu_t		*cpup;
204495c0a3c8Sjosephb 	int		c;
2045843e1988Sjohnlev #endif
20467c478bd9Sstevel@tonic-gate 
20477c478bd9Sstevel@tonic-gate 	/*
20487c478bd9Sstevel@tonic-gate 	 * If the hat is being destroyed, there are no more users, so
20497c478bd9Sstevel@tonic-gate 	 * demap need not do anything.
20507c478bd9Sstevel@tonic-gate 	 */
20517c478bd9Sstevel@tonic-gate 	if (hat->hat_flags & HAT_FREEING)
20527c478bd9Sstevel@tonic-gate 		return;
20537c478bd9Sstevel@tonic-gate 
20547c478bd9Sstevel@tonic-gate 	/*
20557c478bd9Sstevel@tonic-gate 	 * If demapping from a shared pagetable, we best demap the
20567c478bd9Sstevel@tonic-gate 	 * entire set of user TLBs, since we don't know what addresses
20577c478bd9Sstevel@tonic-gate 	 * these were shared at.
20587c478bd9Sstevel@tonic-gate 	 */
20597c478bd9Sstevel@tonic-gate 	if (hat->hat_flags & HAT_SHARED) {
20607c478bd9Sstevel@tonic-gate 		hat = kas.a_hat;
20617c478bd9Sstevel@tonic-gate 		va = DEMAP_ALL_ADDR;
20627c478bd9Sstevel@tonic-gate 	}
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate 	/*
20657c478bd9Sstevel@tonic-gate 	 * if not running with multiple CPUs, don't use cross calls
20667c478bd9Sstevel@tonic-gate 	 */
20677c478bd9Sstevel@tonic-gate 	if (panicstr || !flushes_require_xcalls) {
2068843e1988Sjohnlev #ifdef __xpv
2069843e1988Sjohnlev 		if (va == DEMAP_ALL_ADDR)
2070843e1988Sjohnlev 			xen_flush_tlb();
2071843e1988Sjohnlev 		else
2072843e1988Sjohnlev 			xen_flush_va((caddr_t)va);
2073843e1988Sjohnlev #else
20747c478bd9Sstevel@tonic-gate 		(void) hati_demap_func((xc_arg_t)hat, (xc_arg_t)va, NULL);
2075843e1988Sjohnlev #endif
20767c478bd9Sstevel@tonic-gate 		return;
20777c478bd9Sstevel@tonic-gate 	}
20787c478bd9Sstevel@tonic-gate 
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate 	/*
2081ae115bc7Smrj 	 * Determine CPUs to shootdown. Kernel changes always do all CPUs.
2082ae115bc7Smrj 	 * Otherwise it's just CPUs currently executing in this hat.
20837c478bd9Sstevel@tonic-gate 	 */
20847c478bd9Sstevel@tonic-gate 	kpreempt_disable();
20857c478bd9Sstevel@tonic-gate 	CPUSET_ONLY(justme, CPU->cpu_id);
2086ae115bc7Smrj 	if (hat == kas.a_hat)
2087ae115bc7Smrj 		cpus_to_shootdown = khat_cpuset;
20887c478bd9Sstevel@tonic-gate 	else
2089ae115bc7Smrj 		cpus_to_shootdown = hat->hat_cpus;
2090ae115bc7Smrj 
2091843e1988Sjohnlev #ifndef __xpv
209295c0a3c8Sjosephb 	/*
209395c0a3c8Sjosephb 	 * If any CPUs in the set are idle, just request a delayed flush
209495c0a3c8Sjosephb 	 * and avoid waking them up.
209595c0a3c8Sjosephb 	 */
209695c0a3c8Sjosephb 	check_cpus = cpus_to_shootdown;
209795c0a3c8Sjosephb 	for (c = 0; c < NCPU && !CPUSET_ISNULL(check_cpus); ++c) {
209895c0a3c8Sjosephb 		ulong_t tlb_info;
209995c0a3c8Sjosephb 
210095c0a3c8Sjosephb 		if (!CPU_IN_SET(check_cpus, c))
210195c0a3c8Sjosephb 			continue;
210295c0a3c8Sjosephb 		CPUSET_DEL(check_cpus, c);
210395c0a3c8Sjosephb 		cpup = cpu[c];
210495c0a3c8Sjosephb 		if (cpup == NULL)
210595c0a3c8Sjosephb 			continue;
210695c0a3c8Sjosephb 
210795c0a3c8Sjosephb 		tlb_info = cpup->cpu_m.mcpu_tlb_info;
210895c0a3c8Sjosephb 		while (tlb_info == TLB_CPU_HALTED) {
210995c0a3c8Sjosephb 			(void) CAS_TLB_INFO(cpup, TLB_CPU_HALTED,
2110250b7ff9Sjosephb 			    TLB_CPU_HALTED | TLB_INVAL_ALL);
211195c0a3c8Sjosephb 			SMT_PAUSE();
211295c0a3c8Sjosephb 			tlb_info = cpup->cpu_m.mcpu_tlb_info;
211395c0a3c8Sjosephb 		}
211495c0a3c8Sjosephb 		if (tlb_info == (TLB_CPU_HALTED | TLB_INVAL_ALL)) {
211595c0a3c8Sjosephb 			HATSTAT_INC(hs_tlb_inval_delayed);
211695c0a3c8Sjosephb 			CPUSET_DEL(cpus_to_shootdown, c);
211795c0a3c8Sjosephb 		}
211895c0a3c8Sjosephb 	}
2119843e1988Sjohnlev #endif
212095c0a3c8Sjosephb 
2121ae115bc7Smrj 	if (CPUSET_ISNULL(cpus_to_shootdown) ||
2122ae115bc7Smrj 	    CPUSET_ISEQUAL(cpus_to_shootdown, justme)) {
2123ae115bc7Smrj 
2124843e1988Sjohnlev #ifdef __xpv
2125843e1988Sjohnlev 		if (va == DEMAP_ALL_ADDR)
2126843e1988Sjohnlev 			xen_flush_tlb();
2127843e1988Sjohnlev 		else
2128843e1988Sjohnlev 			xen_flush_va((caddr_t)va);
2129843e1988Sjohnlev #else
2130ae115bc7Smrj 		(void) hati_demap_func((xc_arg_t)hat, (xc_arg_t)va, NULL);
2131843e1988Sjohnlev #endif
2132ae115bc7Smrj 
2133ae115bc7Smrj 	} else {
2134ae115bc7Smrj 
2135ae115bc7Smrj 		CPUSET_ADD(cpus_to_shootdown, CPU->cpu_id);
2136843e1988Sjohnlev #ifdef __xpv
2137843e1988Sjohnlev 		if (va == DEMAP_ALL_ADDR)
2138843e1988Sjohnlev 			xen_gflush_tlb(cpus_to_shootdown);
2139843e1988Sjohnlev 		else
2140843e1988Sjohnlev 			xen_gflush_va((caddr_t)va, cpus_to_shootdown);
2141843e1988Sjohnlev #else
2142f34a7178SJoe Bonasera 		xc_call((xc_arg_t)hat, (xc_arg_t)va, NULL,
2143f34a7178SJoe Bonasera 		    CPUSET2BV(cpus_to_shootdown), hati_demap_func);
2144843e1988Sjohnlev #endif
2145ae115bc7Smrj 
2146ae115bc7Smrj 	}
21477c478bd9Sstevel@tonic-gate 	kpreempt_enable();
21487c478bd9Sstevel@tonic-gate }
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate /*
21517c478bd9Sstevel@tonic-gate  * Interior routine for HAT_UNLOADs from hat_unload_callback(),
21527c478bd9Sstevel@tonic-gate  * hat_kmap_unload() OR from hat_steal() code.  This routine doesn't
21537c478bd9Sstevel@tonic-gate  * handle releasing of the htables.
21547c478bd9Sstevel@tonic-gate  */
21557c478bd9Sstevel@tonic-gate void
21567c478bd9Sstevel@tonic-gate hat_pte_unmap(
21577c478bd9Sstevel@tonic-gate 	htable_t	*ht,
21587c478bd9Sstevel@tonic-gate 	uint_t		entry,
21597c478bd9Sstevel@tonic-gate 	uint_t		flags,
21607c478bd9Sstevel@tonic-gate 	x86pte_t	old_pte,
21617c478bd9Sstevel@tonic-gate 	void		*pte_ptr)
21627c478bd9Sstevel@tonic-gate {
21637c478bd9Sstevel@tonic-gate 	hat_t		*hat = ht->ht_hat;
21647c478bd9Sstevel@tonic-gate 	hment_t		*hm = NULL;
21657c478bd9Sstevel@tonic-gate 	page_t		*pp = NULL;
21667c478bd9Sstevel@tonic-gate 	level_t		l = ht->ht_level;
21677c478bd9Sstevel@tonic-gate 	pfn_t		pfn;
21687c478bd9Sstevel@tonic-gate 
21697c478bd9Sstevel@tonic-gate 	/*
21707c478bd9Sstevel@tonic-gate 	 * We always track the locking counts, even if nothing is unmapped
21717c478bd9Sstevel@tonic-gate 	 */
21727c478bd9Sstevel@tonic-gate 	if ((flags & HAT_UNLOAD_UNLOCK) != 0 && hat != kas.a_hat) {
21737c478bd9Sstevel@tonic-gate 		ASSERT(ht->ht_lock_cnt > 0);
21747c478bd9Sstevel@tonic-gate 		HTABLE_LOCK_DEC(ht);
21757c478bd9Sstevel@tonic-gate 	}
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate 	/*
21787c478bd9Sstevel@tonic-gate 	 * Figure out which page's mapping list lock to acquire using the PFN
21797c478bd9Sstevel@tonic-gate 	 * passed in "old" PTE. We then attempt to invalidate the PTE.
21807c478bd9Sstevel@tonic-gate 	 * If another thread, probably a hat_pageunload, has asynchronously
21817c478bd9Sstevel@tonic-gate 	 * unmapped/remapped this address we'll loop here.
21827c478bd9Sstevel@tonic-gate 	 */
21837c478bd9Sstevel@tonic-gate 	ASSERT(ht->ht_busy > 0);
21847c478bd9Sstevel@tonic-gate 	while (PTE_ISVALID(old_pte)) {
21857c478bd9Sstevel@tonic-gate 		pfn = PTE2PFN(old_pte, l);
2186ae115bc7Smrj 		if (PTE_GET(old_pte, PT_SOFTWARE) >= PT_NOCONSIST) {
21877c478bd9Sstevel@tonic-gate 			pp = NULL;
21887c478bd9Sstevel@tonic-gate 		} else {
2189843e1988Sjohnlev #ifdef __xpv
2190843e1988Sjohnlev 			if (pfn == PFN_INVALID)
2191843e1988Sjohnlev 				panic("Invalid PFN, but not PT_NOCONSIST");
2192843e1988Sjohnlev #endif
21937c478bd9Sstevel@tonic-gate 			pp = page_numtopp_nolock(pfn);
2194aa2ed9e5Sjosephb 			if (pp == NULL) {
2195aa2ed9e5Sjosephb 				panic("no page_t, not NOCONSIST: old_pte="
2196aa2ed9e5Sjosephb 				    FMT_PTE " ht=%lx entry=0x%x pte_ptr=%lx",
2197aa2ed9e5Sjosephb 				    old_pte, (uintptr_t)ht, entry,
2198aa2ed9e5Sjosephb 				    (uintptr_t)pte_ptr);
2199aa2ed9e5Sjosephb 			}
22007c478bd9Sstevel@tonic-gate 			x86_hm_enter(pp);
22017c478bd9Sstevel@tonic-gate 		}
2202aa2ed9e5Sjosephb 
2203aa2ed9e5Sjosephb 		/*
2204aa2ed9e5Sjosephb 		 * If freeing the address space, check that the PTE
2205aa2ed9e5Sjosephb 		 * hasn't changed, as the mappings are no longer in use by
2206aa2ed9e5Sjosephb 		 * any thread, invalidation is unnecessary.
2207aa2ed9e5Sjosephb 		 * If not freeing, do a full invalidate.
2208843e1988Sjohnlev 		 *
2209843e1988Sjohnlev 		 * On the hypervisor we must always remove mappings, as a
2210843e1988Sjohnlev 		 * writable mapping left behind could cause a page table
2211843e1988Sjohnlev 		 * allocation to fail.
2212aa2ed9e5Sjosephb 		 */
2213843e1988Sjohnlev #if !defined(__xpv)
2214aa2ed9e5Sjosephb 		if (hat->hat_flags & HAT_FREEING)
2215aa2ed9e5Sjosephb 			old_pte = x86pte_get(ht, entry);
2216aa2ed9e5Sjosephb 		else
2217843e1988Sjohnlev #endif
2218ae115bc7Smrj 			old_pte = x86pte_inval(ht, entry, old_pte, pte_ptr);
22197c478bd9Sstevel@tonic-gate 
22207c478bd9Sstevel@tonic-gate 		/*
22217c478bd9Sstevel@tonic-gate 		 * If the page hadn't changed we've unmapped it and can proceed
22227c478bd9Sstevel@tonic-gate 		 */
22237c478bd9Sstevel@tonic-gate 		if (PTE_ISVALID(old_pte) && PTE2PFN(old_pte, l) == pfn)
22247c478bd9Sstevel@tonic-gate 			break;
22257c478bd9Sstevel@tonic-gate 
22267c478bd9Sstevel@tonic-gate 		/*
22277c478bd9Sstevel@tonic-gate 		 * Otherwise, we'll have to retry with the current old_pte.
22287c478bd9Sstevel@tonic-gate 		 * Drop the hment lock, since the pfn may have changed.
22297c478bd9Sstevel@tonic-gate 		 */
22307c478bd9Sstevel@tonic-gate 		if (pp != NULL) {
22317c478bd9Sstevel@tonic-gate 			x86_hm_exit(pp);
22327c478bd9Sstevel@tonic-gate 			pp = NULL;
22337c478bd9Sstevel@tonic-gate 		} else {
2234ae115bc7Smrj 			ASSERT(PTE_GET(old_pte, PT_SOFTWARE) >= PT_NOCONSIST);
22357c478bd9Sstevel@tonic-gate 		}
22367c478bd9Sstevel@tonic-gate 	}
22377c478bd9Sstevel@tonic-gate 
22387c478bd9Sstevel@tonic-gate 	/*
22397c478bd9Sstevel@tonic-gate 	 * If the old mapping wasn't valid, there's nothing more to do
22407c478bd9Sstevel@tonic-gate 	 */
22417c478bd9Sstevel@tonic-gate 	if (!PTE_ISVALID(old_pte)) {
22427c478bd9Sstevel@tonic-gate 		if (pp != NULL)
22437c478bd9Sstevel@tonic-gate 			x86_hm_exit(pp);
22447c478bd9Sstevel@tonic-gate 		return;
22457c478bd9Sstevel@tonic-gate 	}
22467c478bd9Sstevel@tonic-gate 
22477c478bd9Sstevel@tonic-gate 	/*
22487c478bd9Sstevel@tonic-gate 	 * Take care of syncing any MOD/REF bits and removing the hment.
22497c478bd9Sstevel@tonic-gate 	 */
22507c478bd9Sstevel@tonic-gate 	if (pp != NULL) {
22517c478bd9Sstevel@tonic-gate 		if (!(flags & HAT_UNLOAD_NOSYNC))
22527c478bd9Sstevel@tonic-gate 			hati_sync_pte_to_page(pp, old_pte, l);
22537c478bd9Sstevel@tonic-gate 		hm = hment_remove(pp, ht, entry);
22547c478bd9Sstevel@tonic-gate 		x86_hm_exit(pp);
22557c478bd9Sstevel@tonic-gate 		if (hm != NULL)
22567c478bd9Sstevel@tonic-gate 			hment_free(hm);
22577c478bd9Sstevel@tonic-gate 	}
22587c478bd9Sstevel@tonic-gate 
22597c478bd9Sstevel@tonic-gate 	/*
22607c478bd9Sstevel@tonic-gate 	 * Handle book keeping in the htable and hat
22617c478bd9Sstevel@tonic-gate 	 */
22627c478bd9Sstevel@tonic-gate 	ASSERT(ht->ht_valid_cnt > 0);
22637c478bd9Sstevel@tonic-gate 	HTABLE_DEC(ht->ht_valid_cnt);
22647c478bd9Sstevel@tonic-gate 	PGCNT_DEC(hat, l);
22657c478bd9Sstevel@tonic-gate }
22667c478bd9Sstevel@tonic-gate 
22677c478bd9Sstevel@tonic-gate /*
22687c478bd9Sstevel@tonic-gate  * very cheap unload implementation to special case some kernel addresses
22697c478bd9Sstevel@tonic-gate  */
22707c478bd9Sstevel@tonic-gate static void
22717c478bd9Sstevel@tonic-gate hat_kmap_unload(caddr_t addr, size_t len, uint_t flags)
22727c478bd9Sstevel@tonic-gate {
22737c478bd9Sstevel@tonic-gate 	uintptr_t	va = (uintptr_t)addr;
22747c478bd9Sstevel@tonic-gate 	uintptr_t	eva = va + len;
2275ae115bc7Smrj 	pgcnt_t		pg_index;
22767c478bd9Sstevel@tonic-gate 	htable_t	*ht;
22777c478bd9Sstevel@tonic-gate 	uint_t		entry;
2278ae115bc7Smrj 	x86pte_t	*pte_ptr;
22797c478bd9Sstevel@tonic-gate 	x86pte_t	old_pte;
22807c478bd9Sstevel@tonic-gate 
22817c478bd9Sstevel@tonic-gate 	for (; va < eva; va += MMU_PAGESIZE) {
22827c478bd9Sstevel@tonic-gate 		/*
22837c478bd9Sstevel@tonic-gate 		 * Get the PTE
22847c478bd9Sstevel@tonic-gate 		 */
2285ae115bc7Smrj 		pg_index = mmu_btop(va - mmu.kmap_addr);
2286ae115bc7Smrj 		pte_ptr = PT_INDEX_PTR(mmu.kmap_ptes, pg_index);
2287ae115bc7Smrj 		old_pte = GET_PTE(pte_ptr);
22887c478bd9Sstevel@tonic-gate 
22897c478bd9Sstevel@tonic-gate 		/*
22907c478bd9Sstevel@tonic-gate 		 * get the htable / entry
22917c478bd9Sstevel@tonic-gate 		 */
22927c478bd9Sstevel@tonic-gate 		ht = mmu.kmap_htables[(va - mmu.kmap_htables[0]->ht_vaddr)
22937c478bd9Sstevel@tonic-gate 		    >> LEVEL_SHIFT(1)];
22947c478bd9Sstevel@tonic-gate 		entry = htable_va2entry(va, ht);
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate 		/*
22977c478bd9Sstevel@tonic-gate 		 * use mostly common code to unmap it.
22987c478bd9Sstevel@tonic-gate 		 */
22997c478bd9Sstevel@tonic-gate 		hat_pte_unmap(ht, entry, flags, old_pte, pte_ptr);
23007c478bd9Sstevel@tonic-gate 	}
23017c478bd9Sstevel@tonic-gate }
23027c478bd9Sstevel@tonic-gate 
23037c478bd9Sstevel@tonic-gate 
23047c478bd9Sstevel@tonic-gate /*
23057c478bd9Sstevel@tonic-gate  * unload a range of virtual address space (no callback)
23067c478bd9Sstevel@tonic-gate  */
23077c478bd9Sstevel@tonic-gate void
23087c478bd9Sstevel@tonic-gate hat_unload(hat_t *hat, caddr_t addr, size_t len, uint_t flags)
23097c478bd9Sstevel@tonic-gate {
23107c478bd9Sstevel@tonic-gate 	uintptr_t va = (uintptr_t)addr;
2311ae115bc7Smrj 
2312843e1988Sjohnlev 	XPV_DISALLOW_MIGRATE();
2313ae115bc7Smrj 	ASSERT(hat == kas.a_hat || va + len <= _userlimit);
23147c478bd9Sstevel@tonic-gate 
23157c478bd9Sstevel@tonic-gate 	/*
23167c478bd9Sstevel@tonic-gate 	 * special case for performance.
23177c478bd9Sstevel@tonic-gate 	 */
23187c478bd9Sstevel@tonic-gate 	if (mmu.kmap_addr <= va && va < mmu.kmap_eaddr) {
23197c478bd9Sstevel@tonic-gate 		ASSERT(hat == kas.a_hat);
23207c478bd9Sstevel@tonic-gate 		hat_kmap_unload(addr, len, flags);
2321ae115bc7Smrj 	} else {
2322ae115bc7Smrj 		hat_unload_callback(hat, addr, len, flags, NULL);
23237c478bd9Sstevel@tonic-gate 	}
2324843e1988Sjohnlev 	XPV_ALLOW_MIGRATE();
23257c478bd9Sstevel@tonic-gate }
23267c478bd9Sstevel@tonic-gate 
23277c478bd9Sstevel@tonic-gate /*
23287c478bd9Sstevel@tonic-gate  * Do the callbacks for ranges being unloaded.
23297c478bd9Sstevel@tonic-gate  */
23307c478bd9Sstevel@tonic-gate typedef struct range_info {
23317c478bd9Sstevel@tonic-gate 	uintptr_t	rng_va;
23327c478bd9Sstevel@tonic-gate 	ulong_t		rng_cnt;
23337c478bd9Sstevel@tonic-gate 	level_t		rng_level;
23347c478bd9Sstevel@tonic-gate } range_info_t;
23357c478bd9Sstevel@tonic-gate 
23367c478bd9Sstevel@tonic-gate static void
23377c478bd9Sstevel@tonic-gate handle_ranges(hat_callback_t *cb, uint_t cnt, range_info_t *range)
23387c478bd9Sstevel@tonic-gate {
23397c478bd9Sstevel@tonic-gate 	/*
23407c478bd9Sstevel@tonic-gate 	 * do callbacks to upper level VM system
23417c478bd9Sstevel@tonic-gate 	 */
23427c478bd9Sstevel@tonic-gate 	while (cb != NULL && cnt > 0) {
23437c478bd9Sstevel@tonic-gate 		--cnt;
23447c478bd9Sstevel@tonic-gate 		cb->hcb_start_addr = (caddr_t)range[cnt].rng_va;
23457c478bd9Sstevel@tonic-gate 		cb->hcb_end_addr = cb->hcb_start_addr;
23467c478bd9Sstevel@tonic-gate 		cb->hcb_end_addr +=
23477c478bd9Sstevel@tonic-gate 		    range[cnt].rng_cnt << LEVEL_SIZE(range[cnt].rng_level);
23487c478bd9Sstevel@tonic-gate 		cb->hcb_function(cb);
23497c478bd9Sstevel@tonic-gate 	}
23507c478bd9Sstevel@tonic-gate }
23517c478bd9Sstevel@tonic-gate 
23527c478bd9Sstevel@tonic-gate /*
23537c478bd9Sstevel@tonic-gate  * Unload a given range of addresses (has optional callback)
23547c478bd9Sstevel@tonic-gate  *
23557c478bd9Sstevel@tonic-gate  * Flags:
23567c478bd9Sstevel@tonic-gate  * define	HAT_UNLOAD		0x00
23577c478bd9Sstevel@tonic-gate  * define	HAT_UNLOAD_NOSYNC	0x02
23587c478bd9Sstevel@tonic-gate  * define	HAT_UNLOAD_UNLOCK	0x04
23597c478bd9Sstevel@tonic-gate  * define	HAT_UNLOAD_OTHER	0x08 - not used
23607c478bd9Sstevel@tonic-gate  * define	HAT_UNLOAD_UNMAP	0x10 - same as HAT_UNLOAD
23617c478bd9Sstevel@tonic-gate  */
23627c478bd9Sstevel@tonic-gate #define	MAX_UNLOAD_CNT (8)
23637c478bd9Sstevel@tonic-gate void
23647c478bd9Sstevel@tonic-gate hat_unload_callback(
23657c478bd9Sstevel@tonic-gate 	hat_t		*hat,
23667c478bd9Sstevel@tonic-gate 	caddr_t		addr,
23677c478bd9Sstevel@tonic-gate 	size_t		len,
23687c478bd9Sstevel@tonic-gate 	uint_t		flags,
23697c478bd9Sstevel@tonic-gate 	hat_callback_t	*cb)
23707c478bd9Sstevel@tonic-gate {
23717c478bd9Sstevel@tonic-gate 	uintptr_t	vaddr = (uintptr_t)addr;
23727c478bd9Sstevel@tonic-gate 	uintptr_t	eaddr = vaddr + len;
23737c478bd9Sstevel@tonic-gate 	htable_t	*ht = NULL;
23747c478bd9Sstevel@tonic-gate 	uint_t		entry;
2375aa2ed9e5Sjosephb 	uintptr_t	contig_va = (uintptr_t)-1L;
23767c478bd9Sstevel@tonic-gate 	range_info_t	r[MAX_UNLOAD_CNT];
23777c478bd9Sstevel@tonic-gate 	uint_t		r_cnt = 0;
23787c478bd9Sstevel@tonic-gate 	x86pte_t	old_pte;
23797c478bd9Sstevel@tonic-gate 
2380843e1988Sjohnlev 	XPV_DISALLOW_MIGRATE();
2381ae115bc7Smrj 	ASSERT(hat == kas.a_hat || eaddr <= _userlimit);
23827c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(vaddr));
23837c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(eaddr));
23847c478bd9Sstevel@tonic-gate 
2385ae115bc7Smrj 	/*
2386ae115bc7Smrj 	 * Special case a single page being unloaded for speed. This happens
2387ae115bc7Smrj 	 * quite frequently, COW faults after a fork() for example.
2388ae115bc7Smrj 	 */
2389ae115bc7Smrj 	if (cb == NULL && len == MMU_PAGESIZE) {
2390ae115bc7Smrj 		ht = htable_getpte(hat, vaddr, &entry, &old_pte, 0);
2391ae115bc7Smrj 		if (ht != NULL) {
2392ae115bc7Smrj 			if (PTE_ISVALID(old_pte))
2393ae115bc7Smrj 				hat_pte_unmap(ht, entry, flags, old_pte, NULL);
2394ae115bc7Smrj 			htable_release(ht);
2395ae115bc7Smrj 		}
2396843e1988Sjohnlev 		XPV_ALLOW_MIGRATE();
2397ae115bc7Smrj 		return;
2398ae115bc7Smrj 	}
2399ae115bc7Smrj 
24007c478bd9Sstevel@tonic-gate 	while (vaddr < eaddr) {
24017c478bd9Sstevel@tonic-gate 		old_pte = htable_walk(hat, &ht, &vaddr, eaddr);
24027c478bd9Sstevel@tonic-gate 		if (ht == NULL)
24037c478bd9Sstevel@tonic-gate 			break;
24047c478bd9Sstevel@tonic-gate 
24057c478bd9Sstevel@tonic-gate 		ASSERT(!IN_VA_HOLE(vaddr));
24067c478bd9Sstevel@tonic-gate 
24077c478bd9Sstevel@tonic-gate 		if (vaddr < (uintptr_t)addr)
24087c478bd9Sstevel@tonic-gate 			panic("hat_unload_callback(): unmap inside large page");
24097c478bd9Sstevel@tonic-gate 
24107c478bd9Sstevel@tonic-gate 		/*
24117c478bd9Sstevel@tonic-gate 		 * We'll do the call backs for contiguous ranges
24127c478bd9Sstevel@tonic-gate 		 */
2413aa2ed9e5Sjosephb 		if (vaddr != contig_va ||
24147c478bd9Sstevel@tonic-gate 		    (r_cnt > 0 && r[r_cnt - 1].rng_level != ht->ht_level)) {
24157c478bd9Sstevel@tonic-gate 			if (r_cnt == MAX_UNLOAD_CNT) {
24167c478bd9Sstevel@tonic-gate 				handle_ranges(cb, r_cnt, r);
24177c478bd9Sstevel@tonic-gate 				r_cnt = 0;
24187c478bd9Sstevel@tonic-gate 			}
24197c478bd9Sstevel@tonic-gate 			r[r_cnt].rng_va = vaddr;
24207c478bd9Sstevel@tonic-gate 			r[r_cnt].rng_cnt = 0;
24217c478bd9Sstevel@tonic-gate 			r[r_cnt].rng_level = ht->ht_level;
24227c478bd9Sstevel@tonic-gate 			++r_cnt;
24237c478bd9Sstevel@tonic-gate 		}
24247c478bd9Sstevel@tonic-gate 
24257c478bd9Sstevel@tonic-gate 		/*
24267c478bd9Sstevel@tonic-gate 		 * Unload one mapping from the page tables.
24277c478bd9Sstevel@tonic-gate 		 */
24287c478bd9Sstevel@tonic-gate 		entry = htable_va2entry(vaddr, ht);
24297c478bd9Sstevel@tonic-gate 		hat_pte_unmap(ht, entry, flags, old_pte, NULL);
24307c478bd9Sstevel@tonic-gate 		ASSERT(ht->ht_level <= mmu.max_page_level);
24317c478bd9Sstevel@tonic-gate 		vaddr += LEVEL_SIZE(ht->ht_level);
2432aa2ed9e5Sjosephb 		contig_va = vaddr;
24337c478bd9Sstevel@tonic-gate 		++r[r_cnt - 1].rng_cnt;
24347c478bd9Sstevel@tonic-gate 	}
24357c478bd9Sstevel@tonic-gate 	if (ht)
24367c478bd9Sstevel@tonic-gate 		htable_release(ht);
24377c478bd9Sstevel@tonic-gate 
24387c478bd9Sstevel@tonic-gate 	/*
24397c478bd9Sstevel@tonic-gate 	 * handle last range for callbacks
24407c478bd9Sstevel@tonic-gate 	 */
24417c478bd9Sstevel@tonic-gate 	if (r_cnt > 0)
24427c478bd9Sstevel@tonic-gate 		handle_ranges(cb, r_cnt, r);
2443843e1988Sjohnlev 	XPV_ALLOW_MIGRATE();
24447c478bd9Sstevel@tonic-gate }
24457c478bd9Sstevel@tonic-gate 
2446ca3e8d88SDave Plauger /*
24479e0b7c70SDave Plauger  * Invalidate a virtual address translation on a slave CPU during
24489e0b7c70SDave Plauger  * panic() dumps.
2449ca3e8d88SDave Plauger  */
2450ca3e8d88SDave Plauger void
2451ca3e8d88SDave Plauger hat_flush_range(hat_t *hat, caddr_t va, size_t size)
2452ca3e8d88SDave Plauger {
2453ca3e8d88SDave Plauger 	ssize_t sz;
2454ca3e8d88SDave Plauger 	caddr_t endva = va + size;
2455ca3e8d88SDave Plauger 
2456ca3e8d88SDave Plauger 	while (va < endva) {
2457ca3e8d88SDave Plauger 		sz = hat_getpagesize(hat, va);
24589e0b7c70SDave Plauger 		if (sz < 0) {
2459ca3e8d88SDave Plauger #ifdef __xpv
2460ca3e8d88SDave Plauger 			xen_flush_tlb();
2461ca3e8d88SDave Plauger #else
24629e0b7c70SDave Plauger 			flush_all_tlb_entries();
2463ca3e8d88SDave Plauger #endif
2464ca3e8d88SDave Plauger 			break;
24659e0b7c70SDave Plauger 		}
24669e0b7c70SDave Plauger #ifdef __xpv
24679e0b7c70SDave Plauger 		xen_flush_va(va);
24689e0b7c70SDave Plauger #else
24699e0b7c70SDave Plauger 		mmu_tlbflush_entry(va);
24709e0b7c70SDave Plauger #endif
2471ca3e8d88SDave Plauger 		va += sz;
2472ca3e8d88SDave Plauger 	}
2473ca3e8d88SDave Plauger }
2474ca3e8d88SDave Plauger 
24757c478bd9Sstevel@tonic-gate /*
24767c478bd9Sstevel@tonic-gate  * synchronize mapping with software data structures
24777c478bd9Sstevel@tonic-gate  *
24787c478bd9Sstevel@tonic-gate  * This interface is currently only used by the working set monitor
24797c478bd9Sstevel@tonic-gate  * driver.
24807c478bd9Sstevel@tonic-gate  */
24817c478bd9Sstevel@tonic-gate /*ARGSUSED*/
24827c478bd9Sstevel@tonic-gate void
24837c478bd9Sstevel@tonic-gate hat_sync(hat_t *hat, caddr_t addr, size_t len, uint_t flags)
24847c478bd9Sstevel@tonic-gate {
24857c478bd9Sstevel@tonic-gate 	uintptr_t	vaddr = (uintptr_t)addr;
24867c478bd9Sstevel@tonic-gate 	uintptr_t	eaddr = vaddr + len;
24877c478bd9Sstevel@tonic-gate 	htable_t	*ht = NULL;
24887c478bd9Sstevel@tonic-gate 	uint_t		entry;
24897c478bd9Sstevel@tonic-gate 	x86pte_t	pte;
24907c478bd9Sstevel@tonic-gate 	x86pte_t	save_pte;
24917c478bd9Sstevel@tonic-gate 	x86pte_t	new;
24927c478bd9Sstevel@tonic-gate 	page_t		*pp;
24937c478bd9Sstevel@tonic-gate 
24947c478bd9Sstevel@tonic-gate 	ASSERT(!IN_VA_HOLE(vaddr));
24957c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(vaddr));
24967c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(eaddr));
2497ae115bc7Smrj 	ASSERT(hat == kas.a_hat || eaddr <= _userlimit);
24987c478bd9Sstevel@tonic-gate 
2499843e1988Sjohnlev 	XPV_DISALLOW_MIGRATE();
25007c478bd9Sstevel@tonic-gate 	for (; vaddr < eaddr; vaddr += LEVEL_SIZE(ht->ht_level)) {
25017c478bd9Sstevel@tonic-gate try_again:
25027c478bd9Sstevel@tonic-gate 		pte = htable_walk(hat, &ht, &vaddr, eaddr);
25037c478bd9Sstevel@tonic-gate 		if (ht == NULL)
25047c478bd9Sstevel@tonic-gate 			break;
25057c478bd9Sstevel@tonic-gate 		entry = htable_va2entry(vaddr, ht);
25067c478bd9Sstevel@tonic-gate 
2507ae115bc7Smrj 		if (PTE_GET(pte, PT_SOFTWARE) >= PT_NOSYNC ||
25087c478bd9Sstevel@tonic-gate 		    PTE_GET(pte, PT_REF | PT_MOD) == 0)
25097c478bd9Sstevel@tonic-gate 			continue;
25107c478bd9Sstevel@tonic-gate 
25117c478bd9Sstevel@tonic-gate 		/*
25127c478bd9Sstevel@tonic-gate 		 * We need to acquire the mapping list lock to protect
25137c478bd9Sstevel@tonic-gate 		 * against hat_pageunload(), hat_unload(), etc.
25147c478bd9Sstevel@tonic-gate 		 */
25157c478bd9Sstevel@tonic-gate 		pp = page_numtopp_nolock(PTE2PFN(pte, ht->ht_level));
25167c478bd9Sstevel@tonic-gate 		if (pp == NULL)
25177c478bd9Sstevel@tonic-gate 			break;
25187c478bd9Sstevel@tonic-gate 		x86_hm_enter(pp);
25197c478bd9Sstevel@tonic-gate 		save_pte = pte;
25207c478bd9Sstevel@tonic-gate 		pte = x86pte_get(ht, entry);
25217c478bd9Sstevel@tonic-gate 		if (pte != save_pte) {
25227c478bd9Sstevel@tonic-gate 			x86_hm_exit(pp);
25237c478bd9Sstevel@tonic-gate 			goto try_again;
25247c478bd9Sstevel@tonic-gate 		}
2525ae115bc7Smrj 		if (PTE_GET(pte, PT_SOFTWARE) >= PT_NOSYNC ||
25267c478bd9Sstevel@tonic-gate 		    PTE_GET(pte, PT_REF | PT_MOD) == 0) {
25277c478bd9Sstevel@tonic-gate 			x86_hm_exit(pp);
25287c478bd9Sstevel@tonic-gate 			continue;
25297c478bd9Sstevel@tonic-gate 		}
25307c478bd9Sstevel@tonic-gate 
25317c478bd9Sstevel@tonic-gate 		/*
25327c478bd9Sstevel@tonic-gate 		 * Need to clear ref or mod bits. We may compete with
25337c478bd9Sstevel@tonic-gate 		 * hardware updating the R/M bits and have to try again.
25347c478bd9Sstevel@tonic-gate 		 */
25357c478bd9Sstevel@tonic-gate 		if (flags == HAT_SYNC_ZERORM) {
25367c478bd9Sstevel@tonic-gate 			new = pte;
25377c478bd9Sstevel@tonic-gate 			PTE_CLR(new, PT_REF | PT_MOD);
25387c478bd9Sstevel@tonic-gate 			pte = hati_update_pte(ht, entry, pte, new);
25397c478bd9Sstevel@tonic-gate 			if (pte != 0) {
25407c478bd9Sstevel@tonic-gate 				x86_hm_exit(pp);
25417c478bd9Sstevel@tonic-gate 				goto try_again;
25427c478bd9Sstevel@tonic-gate 			}
25437c478bd9Sstevel@tonic-gate 		} else {
25447c478bd9Sstevel@tonic-gate 			/*
25457c478bd9Sstevel@tonic-gate 			 * sync the PTE to the page_t
25467c478bd9Sstevel@tonic-gate 			 */
25477c478bd9Sstevel@tonic-gate 			hati_sync_pte_to_page(pp, save_pte, ht->ht_level);
25487c478bd9Sstevel@tonic-gate 		}
25497c478bd9Sstevel@tonic-gate 		x86_hm_exit(pp);
25507c478bd9Sstevel@tonic-gate 	}
25517c478bd9Sstevel@tonic-gate 	if (ht)
25527c478bd9Sstevel@tonic-gate 		htable_release(ht);
2553843e1988Sjohnlev 	XPV_ALLOW_MIGRATE();
25547c478bd9Sstevel@tonic-gate }
25557c478bd9Sstevel@tonic-gate 
25567c478bd9Sstevel@tonic-gate /*
25577c478bd9Sstevel@tonic-gate  * void	hat_map(hat, addr, len, flags)
25587c478bd9Sstevel@tonic-gate  */
25597c478bd9Sstevel@tonic-gate /*ARGSUSED*/
25607c478bd9Sstevel@tonic-gate void
25617c478bd9Sstevel@tonic-gate hat_map(hat_t *hat, caddr_t addr, size_t len, uint_t flags)
25627c478bd9Sstevel@tonic-gate {
25637c478bd9Sstevel@tonic-gate 	/* does nothing */
25647c478bd9Sstevel@tonic-gate }
25657c478bd9Sstevel@tonic-gate 
25667c478bd9Sstevel@tonic-gate /*
25677c478bd9Sstevel@tonic-gate  * uint_t hat_getattr(hat, addr, *attr)
25687c478bd9Sstevel@tonic-gate  *	returns attr for <hat,addr> in *attr.  returns 0 if there was a
25697c478bd9Sstevel@tonic-gate  *	mapping and *attr is valid, nonzero if there was no mapping and
25707c478bd9Sstevel@tonic-gate  *	*attr is not valid.
25717c478bd9Sstevel@tonic-gate  */
25727c478bd9Sstevel@tonic-gate uint_t
25737c478bd9Sstevel@tonic-gate hat_getattr(hat_t *hat, caddr_t addr, uint_t *attr)
25747c478bd9Sstevel@tonic-gate {
25757c478bd9Sstevel@tonic-gate 	uintptr_t	vaddr = ALIGN2PAGE(addr);
25767c478bd9Sstevel@tonic-gate 	htable_t	*ht = NULL;
25777c478bd9Sstevel@tonic-gate 	x86pte_t	pte;
25787c478bd9Sstevel@tonic-gate 
2579ae115bc7Smrj 	ASSERT(hat == kas.a_hat || vaddr <= _userlimit);
25807c478bd9Sstevel@tonic-gate 
25817c478bd9Sstevel@tonic-gate 	if (IN_VA_HOLE(vaddr))
25827c478bd9Sstevel@tonic-gate 		return ((uint_t)-1);
25837c478bd9Sstevel@tonic-gate 
2584ae115bc7Smrj 	ht = htable_getpte(hat, vaddr, NULL, &pte, mmu.max_page_level);
25857c478bd9Sstevel@tonic-gate 	if (ht == NULL)
25867c478bd9Sstevel@tonic-gate 		return ((uint_t)-1);
25877c478bd9Sstevel@tonic-gate 
25887c478bd9Sstevel@tonic-gate 	if (!PTE_ISVALID(pte) || !PTE_ISPAGE(pte, ht->ht_level)) {
25897c478bd9Sstevel@tonic-gate 		htable_release(ht);
25907c478bd9Sstevel@tonic-gate 		return ((uint_t)-1);
25917c478bd9Sstevel@tonic-gate 	}
25927c478bd9Sstevel@tonic-gate 
25937c478bd9Sstevel@tonic-gate 	*attr = PROT_READ;
25947c478bd9Sstevel@tonic-gate 	if (PTE_GET(pte, PT_WRITABLE))
25957c478bd9Sstevel@tonic-gate 		*attr |= PROT_WRITE;
25967c478bd9Sstevel@tonic-gate 	if (PTE_GET(pte, PT_USER))
25977c478bd9Sstevel@tonic-gate 		*attr |= PROT_USER;
25987c478bd9Sstevel@tonic-gate 	if (!PTE_GET(pte, mmu.pt_nx))
25997c478bd9Sstevel@tonic-gate 		*attr |= PROT_EXEC;
2600ae115bc7Smrj 	if (PTE_GET(pte, PT_SOFTWARE) >= PT_NOSYNC)
26017c478bd9Sstevel@tonic-gate 		*attr |= HAT_NOSYNC;
26027c478bd9Sstevel@tonic-gate 	htable_release(ht);
26037c478bd9Sstevel@tonic-gate 	return (0);
26047c478bd9Sstevel@tonic-gate }
26057c478bd9Sstevel@tonic-gate 
26067c478bd9Sstevel@tonic-gate /*
26077c478bd9Sstevel@tonic-gate  * hat_updateattr() applies the given attribute change to an existing mapping
26087c478bd9Sstevel@tonic-gate  */
26097c478bd9Sstevel@tonic-gate #define	HAT_LOAD_ATTR		1
26107c478bd9Sstevel@tonic-gate #define	HAT_SET_ATTR		2
26117c478bd9Sstevel@tonic-gate #define	HAT_CLR_ATTR		3
26127c478bd9Sstevel@tonic-gate 
26137c478bd9Sstevel@tonic-gate static void
26147c478bd9Sstevel@tonic-gate hat_updateattr(hat_t *hat, caddr_t addr, size_t len, uint_t attr, int what)
26157c478bd9Sstevel@tonic-gate {
26167c478bd9Sstevel@tonic-gate 	uintptr_t	vaddr = (uintptr_t)addr;
26177c478bd9Sstevel@tonic-gate 	uintptr_t	eaddr = (uintptr_t)addr + len;
26187c478bd9Sstevel@tonic-gate 	htable_t	*ht = NULL;
26197c478bd9Sstevel@tonic-gate 	uint_t		entry;
26207c478bd9Sstevel@tonic-gate 	x86pte_t	oldpte, newpte;
26217c478bd9Sstevel@tonic-gate 	page_t		*pp;
26227c478bd9Sstevel@tonic-gate 
2623843e1988Sjohnlev 	XPV_DISALLOW_MIGRATE();
26247c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(vaddr));
26257c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(eaddr));
26267c478bd9Sstevel@tonic-gate 	ASSERT(hat == kas.a_hat ||
26277c478bd9Sstevel@tonic-gate 	    AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock));
26287c478bd9Sstevel@tonic-gate 	for (; vaddr < eaddr; vaddr += LEVEL_SIZE(ht->ht_level)) {
26297c478bd9Sstevel@tonic-gate try_again:
26307c478bd9Sstevel@tonic-gate 		oldpte = htable_walk(hat, &ht, &vaddr, eaddr);
26317c478bd9Sstevel@tonic-gate 		if (ht == NULL)
26327c478bd9Sstevel@tonic-gate 			break;
2633ae115bc7Smrj 		if (PTE_GET(oldpte, PT_SOFTWARE) >= PT_NOCONSIST)
26347c478bd9Sstevel@tonic-gate 			continue;
26357c478bd9Sstevel@tonic-gate 
26367c478bd9Sstevel@tonic-gate 		pp = page_numtopp_nolock(PTE2PFN(oldpte, ht->ht_level));
26377c478bd9Sstevel@tonic-gate 		if (pp == NULL)
26387c478bd9Sstevel@tonic-gate 			continue;
26397c478bd9Sstevel@tonic-gate 		x86_hm_enter(pp);
26407c478bd9Sstevel@tonic-gate 
26417c478bd9Sstevel@tonic-gate 		newpte = oldpte;
26427c478bd9Sstevel@tonic-gate 		/*
26437c478bd9Sstevel@tonic-gate 		 * We found a page table entry in the desired range,
26447c478bd9Sstevel@tonic-gate 		 * figure out the new attributes.
26457c478bd9Sstevel@tonic-gate 		 */
26467c478bd9Sstevel@tonic-gate 		if (what == HAT_SET_ATTR || what == HAT_LOAD_ATTR) {
26477c478bd9Sstevel@tonic-gate 			if ((attr & PROT_WRITE) &&
26487c478bd9Sstevel@tonic-gate 			    !PTE_GET(oldpte, PT_WRITABLE))
26497c478bd9Sstevel@tonic-gate 				newpte |= PT_WRITABLE;
26507c478bd9Sstevel@tonic-gate 
2651ae115bc7Smrj 			if ((attr & HAT_NOSYNC) &&
2652ae115bc7Smrj 			    PTE_GET(oldpte, PT_SOFTWARE) < PT_NOSYNC)
26537c478bd9Sstevel@tonic-gate 				newpte |= PT_NOSYNC;
26547c478bd9Sstevel@tonic-gate 
26557c478bd9Sstevel@tonic-gate 			if ((attr & PROT_EXEC) && PTE_GET(oldpte, mmu.pt_nx))
26567c478bd9Sstevel@tonic-gate 				newpte &= ~mmu.pt_nx;
26577c478bd9Sstevel@tonic-gate 		}
26587c478bd9Sstevel@tonic-gate 
26597c478bd9Sstevel@tonic-gate 		if (what == HAT_LOAD_ATTR) {
26607c478bd9Sstevel@tonic-gate 			if (!(attr & PROT_WRITE) &&
26617c478bd9Sstevel@tonic-gate 			    PTE_GET(oldpte, PT_WRITABLE))
26627c478bd9Sstevel@tonic-gate 				newpte &= ~PT_WRITABLE;
26637c478bd9Sstevel@tonic-gate 
2664ae115bc7Smrj 			if (!(attr & HAT_NOSYNC) &&
2665ae115bc7Smrj 			    PTE_GET(oldpte, PT_SOFTWARE) >= PT_NOSYNC)
2666ae115bc7Smrj 				newpte &= ~PT_SOFTWARE;
26677c478bd9Sstevel@tonic-gate 
26687c478bd9Sstevel@tonic-gate 			if (!(attr & PROT_EXEC) && !PTE_GET(oldpte, mmu.pt_nx))
26697c478bd9Sstevel@tonic-gate 				newpte |= mmu.pt_nx;
26707c478bd9Sstevel@tonic-gate 		}
26717c478bd9Sstevel@tonic-gate 
26727c478bd9Sstevel@tonic-gate 		if (what == HAT_CLR_ATTR) {
26737c478bd9Sstevel@tonic-gate 			if ((attr & PROT_WRITE) && PTE_GET(oldpte, PT_WRITABLE))
26747c478bd9Sstevel@tonic-gate 				newpte &= ~PT_WRITABLE;
26757c478bd9Sstevel@tonic-gate 
2676ae115bc7Smrj 			if ((attr & HAT_NOSYNC) &&
2677ae115bc7Smrj 			    PTE_GET(oldpte, PT_SOFTWARE) >= PT_NOSYNC)
2678ae115bc7Smrj 				newpte &= ~PT_SOFTWARE;
26797c478bd9Sstevel@tonic-gate 
26807c478bd9Sstevel@tonic-gate 			if ((attr & PROT_EXEC) && !PTE_GET(oldpte, mmu.pt_nx))
26817c478bd9Sstevel@tonic-gate 				newpte |= mmu.pt_nx;
26827c478bd9Sstevel@tonic-gate 		}
26837c478bd9Sstevel@tonic-gate 
2684ae115bc7Smrj 		/*
2685ae115bc7Smrj 		 * Ensure NOSYNC/NOCONSIST mappings have REF and MOD set.
2686ae115bc7Smrj 		 * x86pte_set() depends on this.
2687ae115bc7Smrj 		 */
2688ae115bc7Smrj 		if (PTE_GET(newpte, PT_SOFTWARE) >= PT_NOSYNC)
2689ae115bc7Smrj 			newpte |= PT_REF | PT_MOD;
2690ae115bc7Smrj 
26917c478bd9Sstevel@tonic-gate 		/*
26927c478bd9Sstevel@tonic-gate 		 * what about PROT_READ or others? this code only handles:
26937c478bd9Sstevel@tonic-gate 		 * EXEC, WRITE, NOSYNC
26947c478bd9Sstevel@tonic-gate 		 */
26957c478bd9Sstevel@tonic-gate 
26967c478bd9Sstevel@tonic-gate 		/*
26977c478bd9Sstevel@tonic-gate 		 * If new PTE really changed, update the table.
26987c478bd9Sstevel@tonic-gate 		 */
26997c478bd9Sstevel@tonic-gate 		if (newpte != oldpte) {
27007c478bd9Sstevel@tonic-gate 			entry = htable_va2entry(vaddr, ht);
27017c478bd9Sstevel@tonic-gate 			oldpte = hati_update_pte(ht, entry, oldpte, newpte);
27027c478bd9Sstevel@tonic-gate 			if (oldpte != 0) {
27037c478bd9Sstevel@tonic-gate 				x86_hm_exit(pp);
27047c478bd9Sstevel@tonic-gate 				goto try_again;
27057c478bd9Sstevel@tonic-gate 			}
27067c478bd9Sstevel@tonic-gate 		}
27077c478bd9Sstevel@tonic-gate 		x86_hm_exit(pp);
27087c478bd9Sstevel@tonic-gate 	}
27097c478bd9Sstevel@tonic-gate 	if (ht)
27107c478bd9Sstevel@tonic-gate 		htable_release(ht);
2711843e1988Sjohnlev 	XPV_ALLOW_MIGRATE();
27127c478bd9Sstevel@tonic-gate }
27137c478bd9Sstevel@tonic-gate 
27147c478bd9Sstevel@tonic-gate /*
27157c478bd9Sstevel@tonic-gate  * Various wrappers for hat_updateattr()
27167c478bd9Sstevel@tonic-gate  */
27177c478bd9Sstevel@tonic-gate void
27187c478bd9Sstevel@tonic-gate hat_setattr(hat_t *hat, caddr_t addr, size_t len, uint_t attr)
27197c478bd9Sstevel@tonic-gate {
2720ae115bc7Smrj 	ASSERT(hat == kas.a_hat || (uintptr_t)addr + len <= _userlimit);
27217c478bd9Sstevel@tonic-gate 	hat_updateattr(hat, addr, len, attr, HAT_SET_ATTR);
27227c478bd9Sstevel@tonic-gate }
27237c478bd9Sstevel@tonic-gate 
27247c478bd9Sstevel@tonic-gate void
27257c478bd9Sstevel@tonic-gate hat_clrattr(hat_t *hat, caddr_t addr, size_t len, uint_t attr)
27267c478bd9Sstevel@tonic-gate {
2727ae115bc7Smrj 	ASSERT(hat == kas.a_hat || (uintptr_t)addr + len <= _userlimit);
27287c478bd9Sstevel@tonic-gate 	hat_updateattr(hat, addr, len, attr, HAT_CLR_ATTR);
27297c478bd9Sstevel@tonic-gate }
27307c478bd9Sstevel@tonic-gate 
27317c478bd9Sstevel@tonic-gate void
27327c478bd9Sstevel@tonic-gate hat_chgattr(hat_t *hat, caddr_t addr, size_t len, uint_t attr)
27337c478bd9Sstevel@tonic-gate {
2734ae115bc7Smrj 	ASSERT(hat == kas.a_hat || (uintptr_t)addr + len <= _userlimit);
27357c478bd9Sstevel@tonic-gate 	hat_updateattr(hat, addr, len, attr, HAT_LOAD_ATTR);
27367c478bd9Sstevel@tonic-gate }
27377c478bd9Sstevel@tonic-gate 
27387c478bd9Sstevel@tonic-gate void
27397c478bd9Sstevel@tonic-gate hat_chgprot(hat_t *hat, caddr_t addr, size_t len, uint_t vprot)
27407c478bd9Sstevel@tonic-gate {
2741ae115bc7Smrj 	ASSERT(hat == kas.a_hat || (uintptr_t)addr + len <= _userlimit);
27427c478bd9Sstevel@tonic-gate 	hat_updateattr(hat, addr, len, vprot & HAT_PROT_MASK, HAT_LOAD_ATTR);
27437c478bd9Sstevel@tonic-gate }
27447c478bd9Sstevel@tonic-gate 
27457c478bd9Sstevel@tonic-gate /*
27467c478bd9Sstevel@tonic-gate  * size_t hat_getpagesize(hat, addr)
27477c478bd9Sstevel@tonic-gate  *	returns pagesize in bytes for <hat, addr>. returns -1 of there is
27487c478bd9Sstevel@tonic-gate  *	no mapping. This is an advisory call.
27497c478bd9Sstevel@tonic-gate  */
27507c478bd9Sstevel@tonic-gate ssize_t
27517c478bd9Sstevel@tonic-gate hat_getpagesize(hat_t *hat, caddr_t addr)
27527c478bd9Sstevel@tonic-gate {
27537c478bd9Sstevel@tonic-gate 	uintptr_t	vaddr = ALIGN2PAGE(addr);
27547c478bd9Sstevel@tonic-gate 	htable_t	*ht;
27557c478bd9Sstevel@tonic-gate 	size_t		pagesize;
27567c478bd9Sstevel@tonic-gate 
2757ae115bc7Smrj 	ASSERT(hat == kas.a_hat || vaddr <= _userlimit);
27587c478bd9Sstevel@tonic-gate 	if (IN_VA_HOLE(vaddr))
27597c478bd9Sstevel@tonic-gate 		return (-1);
27607c478bd9Sstevel@tonic-gate 	ht = htable_getpage(hat, vaddr, NULL);
27617c478bd9Sstevel@tonic-gate 	if (ht == NULL)
27627c478bd9Sstevel@tonic-gate 		return (-1);
27637c478bd9Sstevel@tonic-gate 	pagesize = LEVEL_SIZE(ht->ht_level);
27647c478bd9Sstevel@tonic-gate 	htable_release(ht);
27657c478bd9Sstevel@tonic-gate 	return (pagesize);
27667c478bd9Sstevel@tonic-gate }
27677c478bd9Sstevel@tonic-gate 
27687c478bd9Sstevel@tonic-gate 
27697c478bd9Sstevel@tonic-gate 
27707c478bd9Sstevel@tonic-gate /*
27717c478bd9Sstevel@tonic-gate  * pfn_t hat_getpfnum(hat, addr)
27727c478bd9Sstevel@tonic-gate  *	returns pfn for <hat, addr> or PFN_INVALID if mapping is invalid.
27737c478bd9Sstevel@tonic-gate  */
27747c478bd9Sstevel@tonic-gate pfn_t
27757c478bd9Sstevel@tonic-gate hat_getpfnum(hat_t *hat, caddr_t addr)
27767c478bd9Sstevel@tonic-gate {
27777c478bd9Sstevel@tonic-gate 	uintptr_t	vaddr = ALIGN2PAGE(addr);
27787c478bd9Sstevel@tonic-gate 	htable_t	*ht;
27797c478bd9Sstevel@tonic-gate 	uint_t		entry;
27807c478bd9Sstevel@tonic-gate 	pfn_t		pfn = PFN_INVALID;
27817c478bd9Sstevel@tonic-gate 
2782ae115bc7Smrj 	ASSERT(hat == kas.a_hat || vaddr <= _userlimit);
27837c478bd9Sstevel@tonic-gate 	if (khat_running == 0)
2784ae115bc7Smrj 		return (PFN_INVALID);
27857c478bd9Sstevel@tonic-gate 
27867c478bd9Sstevel@tonic-gate 	if (IN_VA_HOLE(vaddr))
27877c478bd9Sstevel@tonic-gate 		return (PFN_INVALID);
27887c478bd9Sstevel@tonic-gate 
2789843e1988Sjohnlev 	XPV_DISALLOW_MIGRATE();
27907c478bd9Sstevel@tonic-gate 	/*
27917c478bd9Sstevel@tonic-gate 	 * A very common use of hat_getpfnum() is from the DDI for kernel pages.
27927c478bd9Sstevel@tonic-gate 	 * Use the kmap_ptes (which also covers the 32 bit heap) to speed
27937c478bd9Sstevel@tonic-gate 	 * this up.
27947c478bd9Sstevel@tonic-gate 	 */
27957c478bd9Sstevel@tonic-gate 	if (mmu.kmap_addr <= vaddr && vaddr < mmu.kmap_eaddr) {
27967c478bd9Sstevel@tonic-gate 		x86pte_t pte;
2797ae115bc7Smrj 		pgcnt_t pg_index;
27987c478bd9Sstevel@tonic-gate 
2799ae115bc7Smrj 		pg_index = mmu_btop(vaddr - mmu.kmap_addr);
2800ae115bc7Smrj 		pte = GET_PTE(PT_INDEX_PTR(mmu.kmap_ptes, pg_index));
2801843e1988Sjohnlev 		if (PTE_ISVALID(pte))
2802843e1988Sjohnlev 			/*LINTED [use of constant 0 causes a lint warning] */
2803843e1988Sjohnlev 			pfn = PTE2PFN(pte, 0);
2804843e1988Sjohnlev 		XPV_ALLOW_MIGRATE();
2805843e1988Sjohnlev 		return (pfn);
28067c478bd9Sstevel@tonic-gate 	}
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate 	ht = htable_getpage(hat, vaddr, &entry);
2809843e1988Sjohnlev 	if (ht == NULL) {
2810843e1988Sjohnlev 		XPV_ALLOW_MIGRATE();
28117c478bd9Sstevel@tonic-gate 		return (PFN_INVALID);
2812843e1988Sjohnlev 	}
28137c478bd9Sstevel@tonic-gate 	ASSERT(vaddr >= ht->ht_vaddr);
28147c478bd9Sstevel@tonic-gate 	ASSERT(vaddr <= HTABLE_LAST_PAGE(ht));
28157c478bd9Sstevel@tonic-gate 	pfn = PTE2PFN(x86pte_get(ht, entry), ht->ht_level);
28167c478bd9Sstevel@tonic-gate 	if (ht->ht_level > 0)
28177c478bd9Sstevel@tonic-gate 		pfn += mmu_btop(vaddr & LEVEL_OFFSET(ht->ht_level));
28187c478bd9Sstevel@tonic-gate 	htable_release(ht);
2819843e1988Sjohnlev 	XPV_ALLOW_MIGRATE();
28207c478bd9Sstevel@tonic-gate 	return (pfn);
28217c478bd9Sstevel@tonic-gate }
28227c478bd9Sstevel@tonic-gate 
28237c478bd9Sstevel@tonic-gate /*
28247c478bd9Sstevel@tonic-gate  * hat_getkpfnum() is an obsolete DDI routine, and its use is discouraged.
28257c478bd9Sstevel@tonic-gate  * Use hat_getpfnum(kas.a_hat, ...) instead.
28267c478bd9Sstevel@tonic-gate  *
28277c478bd9Sstevel@tonic-gate  * We'd like to return PFN_INVALID if the mappings have underlying page_t's
28287c478bd9Sstevel@tonic-gate  * but can't right now due to the fact that some software has grown to use
28297c478bd9Sstevel@tonic-gate  * this interface incorrectly. So for now when the interface is misused,
28307c478bd9Sstevel@tonic-gate  * return a warning to the user that in the future it won't work in the
28317c478bd9Sstevel@tonic-gate  * way they're abusing it, and carry on.
28327c478bd9Sstevel@tonic-gate  *
28337c478bd9Sstevel@tonic-gate  * Note that hat_getkpfnum() is never supported on amd64.
28347c478bd9Sstevel@tonic-gate  */
28357c478bd9Sstevel@tonic-gate #if !defined(__amd64)
28367c478bd9Sstevel@tonic-gate pfn_t
28377c478bd9Sstevel@tonic-gate hat_getkpfnum(caddr_t addr)
28387c478bd9Sstevel@tonic-gate {
28397c478bd9Sstevel@tonic-gate 	pfn_t	pfn;
28407c478bd9Sstevel@tonic-gate 	int badcaller = 0;
28417c478bd9Sstevel@tonic-gate 
28427c478bd9Sstevel@tonic-gate 	if (khat_running == 0)
28437c478bd9Sstevel@tonic-gate 		panic("hat_getkpfnum(): called too early\n");
28447c478bd9Sstevel@tonic-gate 	if ((uintptr_t)addr < kernelbase)
28457c478bd9Sstevel@tonic-gate 		return (PFN_INVALID);
28467c478bd9Sstevel@tonic-gate 
2847843e1988Sjohnlev 	XPV_DISALLOW_MIGRATE();
28487c478bd9Sstevel@tonic-gate 	if (segkpm && IS_KPM_ADDR(addr)) {
28497c478bd9Sstevel@tonic-gate 		badcaller = 1;
28507c478bd9Sstevel@tonic-gate 		pfn = hat_kpm_va2pfn(addr);
28517c478bd9Sstevel@tonic-gate 	} else {
28527c478bd9Sstevel@tonic-gate 		pfn = hat_getpfnum(kas.a_hat, addr);
28537c478bd9Sstevel@tonic-gate 		badcaller = pf_is_memory(pfn);
28547c478bd9Sstevel@tonic-gate 	}
28557c478bd9Sstevel@tonic-gate 
28567c478bd9Sstevel@tonic-gate 	if (badcaller)
28577c478bd9Sstevel@tonic-gate 		hat_getkpfnum_badcall(caller());
2858843e1988Sjohnlev 	XPV_ALLOW_MIGRATE();
28597c478bd9Sstevel@tonic-gate 	return (pfn);
28607c478bd9Sstevel@tonic-gate }
28617c478bd9Sstevel@tonic-gate #endif /* __amd64 */
28627c478bd9Sstevel@tonic-gate 
28637c478bd9Sstevel@tonic-gate /*
28647c478bd9Sstevel@tonic-gate  * int hat_probe(hat, addr)
28657c478bd9Sstevel@tonic-gate  *	return 0 if no valid mapping is present.  Faster version
28667c478bd9Sstevel@tonic-gate  *	of hat_getattr in certain architectures.
28677c478bd9Sstevel@tonic-gate  */
28687c478bd9Sstevel@tonic-gate int
28697c478bd9Sstevel@tonic-gate hat_probe(hat_t *hat, caddr_t addr)
28707c478bd9Sstevel@tonic-gate {
28717c478bd9Sstevel@tonic-gate 	uintptr_t	vaddr = ALIGN2PAGE(addr);
28727c478bd9Sstevel@tonic-gate 	uint_t		entry;
28737c478bd9Sstevel@tonic-gate 	htable_t	*ht;
28747c478bd9Sstevel@tonic-gate 	pgcnt_t		pg_off;
28757c478bd9Sstevel@tonic-gate 
2876ae115bc7Smrj 	ASSERT(hat == kas.a_hat || vaddr <= _userlimit);
28777c478bd9Sstevel@tonic-gate 	ASSERT(hat == kas.a_hat ||
28787c478bd9Sstevel@tonic-gate 	    AS_LOCK_HELD(hat->hat_as, &hat->hat_as->a_lock));
28797c478bd9Sstevel@tonic-gate 	if (IN_VA_HOLE(vaddr))
28807c478bd9Sstevel@tonic-gate 		return (0);
28817c478bd9Sstevel@tonic-gate 
28827c478bd9Sstevel@tonic-gate 	/*
28837c478bd9Sstevel@tonic-gate 	 * Most common use of hat_probe is from segmap. We special case it
28847c478bd9Sstevel@tonic-gate 	 * for performance.
28857c478bd9Sstevel@tonic-gate 	 */
28867c478bd9Sstevel@tonic-gate 	if (mmu.kmap_addr <= vaddr && vaddr < mmu.kmap_eaddr) {
28877c478bd9Sstevel@tonic-gate 		pg_off = mmu_btop(vaddr - mmu.kmap_addr);
28887c478bd9Sstevel@tonic-gate 		if (mmu.pae_hat)
28897c478bd9Sstevel@tonic-gate 			return (PTE_ISVALID(mmu.kmap_ptes[pg_off]));
28907c478bd9Sstevel@tonic-gate 		else
28917c478bd9Sstevel@tonic-gate 			return (PTE_ISVALID(
28927c478bd9Sstevel@tonic-gate 			    ((x86pte32_t *)mmu.kmap_ptes)[pg_off]));
28937c478bd9Sstevel@tonic-gate 	}
28947c478bd9Sstevel@tonic-gate 
28957c478bd9Sstevel@tonic-gate 	ht = htable_getpage(hat, vaddr, &entry);
28967c478bd9Sstevel@tonic-gate 	htable_release(ht);
2897843e1988Sjohnlev 	return (ht != NULL);
28987c478bd9Sstevel@tonic-gate }
28997c478bd9Sstevel@tonic-gate 
29007c478bd9Sstevel@tonic-gate /*
2901250b7ff9Sjosephb  * Find out if the segment for hat_share()/hat_unshare() is DISM or locked ISM.
2902250b7ff9Sjosephb  */
2903250b7ff9Sjosephb static int
2904250b7ff9Sjosephb is_it_dism(hat_t *hat, caddr_t va)
2905250b7ff9Sjosephb {
2906250b7ff9Sjosephb 	struct seg *seg;
2907250b7ff9Sjosephb 	struct shm_data *shmd;
2908250b7ff9Sjosephb 	struct spt_data *sptd;
2909250b7ff9Sjosephb 
2910250b7ff9Sjosephb 	seg = as_findseg(hat->hat_as, va, 0);
2911250b7ff9Sjosephb 	ASSERT(seg != NULL);
2912250b7ff9Sjosephb 	ASSERT(seg->s_base <= va);
2913250b7ff9Sjosephb 	shmd = (struct shm_data *)seg->s_data;
2914250b7ff9Sjosephb 	ASSERT(shmd != NULL);
2915250b7ff9Sjosephb 	sptd = (struct spt_data *)shmd->shm_sptseg->s_data;
2916250b7ff9Sjosephb 	ASSERT(sptd != NULL);
2917250b7ff9Sjosephb 	if (sptd->spt_flags & SHM_PAGEABLE)
2918250b7ff9Sjosephb 		return (1);
2919250b7ff9Sjosephb 	return (0);
2920250b7ff9Sjosephb }
2921250b7ff9Sjosephb 
2922250b7ff9Sjosephb /*
2923250b7ff9Sjosephb  * Simple implementation of ISM. hat_share() is similar to hat_memload_array(),
29247c478bd9Sstevel@tonic-gate  * except that we use the ism_hat's existing mappings to determine the pages
2925250b7ff9Sjosephb  * and protections to use for this hat. If we find a full properly aligned
2926250b7ff9Sjosephb  * and sized pagetable, we will attempt to share the pagetable itself.
29277c478bd9Sstevel@tonic-gate  */
29287c478bd9Sstevel@tonic-gate /*ARGSUSED*/
29297c478bd9Sstevel@tonic-gate int
29307c478bd9Sstevel@tonic-gate hat_share(
29317c478bd9Sstevel@tonic-gate 	hat_t		*hat,
29327c478bd9Sstevel@tonic-gate 	caddr_t		addr,
29337c478bd9Sstevel@tonic-gate 	hat_t		*ism_hat,
29347c478bd9Sstevel@tonic-gate 	caddr_t		src_addr,
29357c478bd9Sstevel@tonic-gate 	size_t		len,	/* almost useless value, see below.. */
29367c478bd9Sstevel@tonic-gate 	uint_t		ismszc)
29377c478bd9Sstevel@tonic-gate {
29387c478bd9Sstevel@tonic-gate 	uintptr_t	vaddr_start = (uintptr_t)addr;
29397c478bd9Sstevel@tonic-gate 	uintptr_t	vaddr;
29407c478bd9Sstevel@tonic-gate 	uintptr_t	eaddr = vaddr_start + len;
29417c478bd9Sstevel@tonic-gate 	uintptr_t	ism_addr_start = (uintptr_t)src_addr;
29427c478bd9Sstevel@tonic-gate 	uintptr_t	ism_addr = ism_addr_start;
29437c478bd9Sstevel@tonic-gate 	uintptr_t	e_ism_addr = ism_addr + len;
29447c478bd9Sstevel@tonic-gate 	htable_t	*ism_ht = NULL;
29457c478bd9Sstevel@tonic-gate 	htable_t	*ht;
29467c478bd9Sstevel@tonic-gate 	x86pte_t	pte;
29477c478bd9Sstevel@tonic-gate 	page_t		*pp;
29487c478bd9Sstevel@tonic-gate 	pfn_t		pfn;
29497c478bd9Sstevel@tonic-gate 	level_t		l;
29507c478bd9Sstevel@tonic-gate 	pgcnt_t		pgcnt;
29517c478bd9Sstevel@tonic-gate 	uint_t		prot;
2952250b7ff9Sjosephb 	int		is_dism;
2953250b7ff9Sjosephb 	int		flags;
29547c478bd9Sstevel@tonic-gate 
29557c478bd9Sstevel@tonic-gate 	/*
29567c478bd9Sstevel@tonic-gate 	 * We might be asked to share an empty DISM hat by as_dup()
29577c478bd9Sstevel@tonic-gate 	 */
29587c478bd9Sstevel@tonic-gate 	ASSERT(hat != kas.a_hat);
2959ae115bc7Smrj 	ASSERT(eaddr <= _userlimit);
29607c478bd9Sstevel@tonic-gate 	if (!(ism_hat->hat_flags & HAT_SHARED)) {
29617c478bd9Sstevel@tonic-gate 		ASSERT(hat_get_mapped_size(ism_hat) == 0);
29627c478bd9Sstevel@tonic-gate 		return (0);
29637c478bd9Sstevel@tonic-gate 	}
2964843e1988Sjohnlev 	XPV_DISALLOW_MIGRATE();
29657c478bd9Sstevel@tonic-gate 
29667c478bd9Sstevel@tonic-gate 	/*
29677c478bd9Sstevel@tonic-gate 	 * The SPT segment driver often passes us a size larger than there are
29687c478bd9Sstevel@tonic-gate 	 * valid mappings. That's because it rounds the segment size up to a
29697c478bd9Sstevel@tonic-gate 	 * large pagesize, even if the actual memory mapped by ism_hat is less.
29707c478bd9Sstevel@tonic-gate 	 */
29717c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(vaddr_start));
29727c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(ism_addr_start));
29737c478bd9Sstevel@tonic-gate 	ASSERT(ism_hat->hat_flags & HAT_SHARED);
2974250b7ff9Sjosephb 	is_dism = is_it_dism(hat, addr);
29757c478bd9Sstevel@tonic-gate 	while (ism_addr < e_ism_addr) {
29767c478bd9Sstevel@tonic-gate 		/*
29777c478bd9Sstevel@tonic-gate 		 * use htable_walk to get the next valid ISM mapping
29787c478bd9Sstevel@tonic-gate 		 */
29797c478bd9Sstevel@tonic-gate 		pte = htable_walk(ism_hat, &ism_ht, &ism_addr, e_ism_addr);
29807c478bd9Sstevel@tonic-gate 		if (ism_ht == NULL)
29817c478bd9Sstevel@tonic-gate 			break;
29827c478bd9Sstevel@tonic-gate 
29837c478bd9Sstevel@tonic-gate 		/*
2984250b7ff9Sjosephb 		 * First check to see if we already share the page table.
29857c478bd9Sstevel@tonic-gate 		 */
2986250b7ff9Sjosephb 		l = ism_ht->ht_level;
29877c478bd9Sstevel@tonic-gate 		vaddr = vaddr_start + (ism_addr - ism_addr_start);
2988250b7ff9Sjosephb 		ht = htable_lookup(hat, vaddr, l);
2989250b7ff9Sjosephb 		if (ht != NULL) {
2990250b7ff9Sjosephb 			if (ht->ht_flags & HTABLE_SHARED_PFN)
2991250b7ff9Sjosephb 				goto shared;
2992250b7ff9Sjosephb 			htable_release(ht);
2993250b7ff9Sjosephb 			goto not_shared;
29947c478bd9Sstevel@tonic-gate 		}
29957c478bd9Sstevel@tonic-gate 
29967c478bd9Sstevel@tonic-gate 		/*
2997250b7ff9Sjosephb 		 * Can't ever share top table.
29987c478bd9Sstevel@tonic-gate 		 */
2999250b7ff9Sjosephb 		if (l == mmu.max_level)
3000250b7ff9Sjosephb 			goto not_shared;
30017c478bd9Sstevel@tonic-gate 
3002250b7ff9Sjosephb 		/*
3003250b7ff9Sjosephb 		 * Avoid level mismatches later due to DISM faults.
3004250b7ff9Sjosephb 		 */
3005250b7ff9Sjosephb 		if (is_dism && l > 0)
3006250b7ff9Sjosephb 			goto not_shared;
30077c478bd9Sstevel@tonic-gate 
3008250b7ff9Sjosephb 		/*
3009250b7ff9Sjosephb 		 * addresses and lengths must align
3010250b7ff9Sjosephb 		 * table must be fully populated
3011250b7ff9Sjosephb 		 * no lower level page tables
3012250b7ff9Sjosephb 		 */
3013250b7ff9Sjosephb 		if (ism_addr != ism_ht->ht_vaddr ||
3014250b7ff9Sjosephb 		    (vaddr & LEVEL_OFFSET(l + 1)) != 0)
3015250b7ff9Sjosephb 			goto not_shared;
30167c478bd9Sstevel@tonic-gate 
3017250b7ff9Sjosephb 		/*
3018250b7ff9Sjosephb 		 * The range of address space must cover a full table.
3019250b7ff9Sjosephb 		 */
30201d03c31eSjohnlev 		if (e_ism_addr - ism_addr < LEVEL_SIZE(l + 1))
3021250b7ff9Sjosephb 			goto not_shared;
30227c478bd9Sstevel@tonic-gate 
3023250b7ff9Sjosephb 		/*
3024250b7ff9Sjosephb 		 * All entries in the ISM page table must be leaf PTEs.
3025250b7ff9Sjosephb 		 */
3026250b7ff9Sjosephb 		if (l > 0) {
3027250b7ff9Sjosephb 			int e;
30287c478bd9Sstevel@tonic-gate 
3029250b7ff9Sjosephb 			/*
3030250b7ff9Sjosephb 			 * We know the 0th is from htable_walk() above.
3031250b7ff9Sjosephb 			 */
3032250b7ff9Sjosephb 			for (e = 1; e < HTABLE_NUM_PTES(ism_ht); ++e) {
3033250b7ff9Sjosephb 				x86pte_t pte;
3034250b7ff9Sjosephb 				pte = x86pte_get(ism_ht, e);
3035250b7ff9Sjosephb 				if (!PTE_ISPAGE(pte, l))
3036250b7ff9Sjosephb 					goto not_shared;
30377c478bd9Sstevel@tonic-gate 			}
30387c478bd9Sstevel@tonic-gate 		}
30397c478bd9Sstevel@tonic-gate 
3040250b7ff9Sjosephb 		/*
3041250b7ff9Sjosephb 		 * share the page table
3042250b7ff9Sjosephb 		 */
3043250b7ff9Sjosephb 		ht = htable_create(hat, vaddr, l, ism_ht);
3044250b7ff9Sjosephb shared:
3045250b7ff9Sjosephb 		ASSERT(ht->ht_flags & HTABLE_SHARED_PFN);
3046250b7ff9Sjosephb 		ASSERT(ht->ht_shares == ism_ht);
3047250b7ff9Sjosephb 		hat->hat_ism_pgcnt +=
3048250b7ff9Sjosephb 		    (ism_ht->ht_valid_cnt - ht->ht_valid_cnt) <<
3049250b7ff9Sjosephb 		    (LEVEL_SHIFT(ht->ht_level) - MMU_PAGESHIFT);
3050250b7ff9Sjosephb 		ht->ht_valid_cnt = ism_ht->ht_valid_cnt;
3051250b7ff9Sjosephb 		htable_release(ht);
3052250b7ff9Sjosephb 		ism_addr = ism_ht->ht_vaddr + LEVEL_SIZE(l + 1);
3053250b7ff9Sjosephb 		htable_release(ism_ht);
3054250b7ff9Sjosephb 		ism_ht = NULL;
3055250b7ff9Sjosephb 		continue;
3056250b7ff9Sjosephb 
3057250b7ff9Sjosephb not_shared:
30587c478bd9Sstevel@tonic-gate 		/*
30597c478bd9Sstevel@tonic-gate 		 * Unable to share the page table. Instead we will
30607c478bd9Sstevel@tonic-gate 		 * create new mappings from the values in the ISM mappings.
3061250b7ff9Sjosephb 		 * Figure out what level size mappings to use;
3062250b7ff9Sjosephb 		 */
3063250b7ff9Sjosephb 		for (l = ism_ht->ht_level; l > 0; --l) {
3064250b7ff9Sjosephb 			if (LEVEL_SIZE(l) <= eaddr - vaddr &&
3065250b7ff9Sjosephb 			    (vaddr & LEVEL_OFFSET(l)) == 0)
3066250b7ff9Sjosephb 				break;
3067250b7ff9Sjosephb 		}
3068250b7ff9Sjosephb 
3069250b7ff9Sjosephb 		/*
30707c478bd9Sstevel@tonic-gate 		 * The ISM mapping might be larger than the share area,
3071250b7ff9Sjosephb 		 * be careful to truncate it if needed.
30727c478bd9Sstevel@tonic-gate 		 */
30737c478bd9Sstevel@tonic-gate 		if (eaddr - vaddr >= LEVEL_SIZE(ism_ht->ht_level)) {
30747c478bd9Sstevel@tonic-gate 			pgcnt = mmu_btop(LEVEL_SIZE(ism_ht->ht_level));
30757c478bd9Sstevel@tonic-gate 		} else {
30767c478bd9Sstevel@tonic-gate 			pgcnt = mmu_btop(eaddr - vaddr);
30777c478bd9Sstevel@tonic-gate 			l = 0;
30787c478bd9Sstevel@tonic-gate 		}
30797c478bd9Sstevel@tonic-gate 
30807c478bd9Sstevel@tonic-gate 		pfn = PTE2PFN(pte, ism_ht->ht_level);
30817c478bd9Sstevel@tonic-gate 		ASSERT(pfn != PFN_INVALID);
30827c478bd9Sstevel@tonic-gate 		while (pgcnt > 0) {
30837c478bd9Sstevel@tonic-gate 			/*
30847c478bd9Sstevel@tonic-gate 			 * Make a new pte for the PFN for this level.
30857c478bd9Sstevel@tonic-gate 			 * Copy protections for the pte from the ISM pte.
30867c478bd9Sstevel@tonic-gate 			 */
30877c478bd9Sstevel@tonic-gate 			pp = page_numtopp_nolock(pfn);
30887c478bd9Sstevel@tonic-gate 			ASSERT(pp != NULL);
30897c478bd9Sstevel@tonic-gate 
30907c478bd9Sstevel@tonic-gate 			prot = PROT_USER | PROT_READ | HAT_UNORDERED_OK;
30917c478bd9Sstevel@tonic-gate 			if (PTE_GET(pte, PT_WRITABLE))
30927c478bd9Sstevel@tonic-gate 				prot |= PROT_WRITE;
30937c478bd9Sstevel@tonic-gate 			if (!PTE_GET(pte, PT_NX))
30947c478bd9Sstevel@tonic-gate 				prot |= PROT_EXEC;
30957c478bd9Sstevel@tonic-gate 
3096250b7ff9Sjosephb 			flags = HAT_LOAD;
3097250b7ff9Sjosephb 			if (!is_dism)
3098250b7ff9Sjosephb 				flags |= HAT_LOAD_LOCK | HAT_LOAD_NOCONSIST;
3099250b7ff9Sjosephb 			while (hati_load_common(hat, vaddr, pp, prot, flags,
3100ae115bc7Smrj 			    l, pfn) != 0) {
3101ae115bc7Smrj 				if (l == 0)
3102ae115bc7Smrj 					panic("hati_load_common() failure");
3103ae115bc7Smrj 				--l;
3104ae115bc7Smrj 			}
31057c478bd9Sstevel@tonic-gate 
31067c478bd9Sstevel@tonic-gate 			vaddr += LEVEL_SIZE(l);
31077c478bd9Sstevel@tonic-gate 			ism_addr += LEVEL_SIZE(l);
31087c478bd9Sstevel@tonic-gate 			pfn += mmu_btop(LEVEL_SIZE(l));
31097c478bd9Sstevel@tonic-gate 			pgcnt -= mmu_btop(LEVEL_SIZE(l));
31107c478bd9Sstevel@tonic-gate 		}
31117c478bd9Sstevel@tonic-gate 	}
31127c478bd9Sstevel@tonic-gate 	if (ism_ht != NULL)
31137c478bd9Sstevel@tonic-gate 		htable_release(ism_ht);
3114843e1988Sjohnlev 	XPV_ALLOW_MIGRATE();
31157c478bd9Sstevel@tonic-gate 	return (0);
31167c478bd9Sstevel@tonic-gate }
31177c478bd9Sstevel@tonic-gate 
31187c478bd9Sstevel@tonic-gate 
31197c478bd9Sstevel@tonic-gate /*
31207c478bd9Sstevel@tonic-gate  * hat_unshare() is similar to hat_unload_callback(), but
31217c478bd9Sstevel@tonic-gate  * we have to look for empty shared pagetables. Note that
31227c478bd9Sstevel@tonic-gate  * hat_unshare() is always invoked against an entire segment.
31237c478bd9Sstevel@tonic-gate  */
31247c478bd9Sstevel@tonic-gate /*ARGSUSED*/
31257c478bd9Sstevel@tonic-gate void
31267c478bd9Sstevel@tonic-gate hat_unshare(hat_t *hat, caddr_t addr, size_t len, uint_t ismszc)
31277c478bd9Sstevel@tonic-gate {
31287173d045Sjosephb 	uint64_t	vaddr = (uintptr_t)addr;
31297c478bd9Sstevel@tonic-gate 	uintptr_t	eaddr = vaddr + len;
31307c478bd9Sstevel@tonic-gate 	htable_t	*ht = NULL;
31317c478bd9Sstevel@tonic-gate 	uint_t		need_demaps = 0;
3132250b7ff9Sjosephb 	int		flags = HAT_UNLOAD_UNMAP;
3133250b7ff9Sjosephb 	level_t		l;
31347c478bd9Sstevel@tonic-gate 
31357c478bd9Sstevel@tonic-gate 	ASSERT(hat != kas.a_hat);
3136ae115bc7Smrj 	ASSERT(eaddr <= _userlimit);
31377c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(vaddr));
31387c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(eaddr));
3139843e1988Sjohnlev 	XPV_DISALLOW_MIGRATE();
31407c478bd9Sstevel@tonic-gate 
31417c478bd9Sstevel@tonic-gate 	/*
31427c478bd9Sstevel@tonic-gate 	 * First go through and remove any shared pagetables.
31437c478bd9Sstevel@tonic-gate 	 *
3144ae115bc7Smrj 	 * Note that it's ok to delay the TLB shootdown till the entire range is
31457c478bd9Sstevel@tonic-gate 	 * finished, because if hat_pageunload() were to unload a shared
3146ae115bc7Smrj 	 * pagetable page, its hat_tlb_inval() will do a global TLB invalidate.
31477c478bd9Sstevel@tonic-gate 	 */
3148250b7ff9Sjosephb 	l = mmu.max_page_level;
3149250b7ff9Sjosephb 	if (l == mmu.max_level)
3150250b7ff9Sjosephb 		--l;
3151250b7ff9Sjosephb 	for (; l >= 0; --l) {
3152250b7ff9Sjosephb 		for (vaddr = (uintptr_t)addr; vaddr < eaddr;
3153250b7ff9Sjosephb 		    vaddr = (vaddr & LEVEL_MASK(l + 1)) + LEVEL_SIZE(l + 1)) {
3154250b7ff9Sjosephb 			ASSERT(!IN_VA_HOLE(vaddr));
3155250b7ff9Sjosephb 			/*
3156250b7ff9Sjosephb 			 * find a pagetable that maps the current address
3157250b7ff9Sjosephb 			 */
3158250b7ff9Sjosephb 			ht = htable_lookup(hat, vaddr, l);
3159250b7ff9Sjosephb 			if (ht == NULL)
3160250b7ff9Sjosephb 				continue;
31617c478bd9Sstevel@tonic-gate 			if (ht->ht_flags & HTABLE_SHARED_PFN) {
31627c478bd9Sstevel@tonic-gate 				/*
3163250b7ff9Sjosephb 				 * clear page count, set valid_cnt to 0,
3164250b7ff9Sjosephb 				 * let htable_release() finish the job
31657c478bd9Sstevel@tonic-gate 				 */
3166250b7ff9Sjosephb 				hat->hat_ism_pgcnt -= ht->ht_valid_cnt <<
3167250b7ff9Sjosephb 				    (LEVEL_SHIFT(ht->ht_level) - MMU_PAGESHIFT);
31687c478bd9Sstevel@tonic-gate 				ht->ht_valid_cnt = 0;
31697c478bd9Sstevel@tonic-gate 				need_demaps = 1;
31707c478bd9Sstevel@tonic-gate 			}
31717c478bd9Sstevel@tonic-gate 			htable_release(ht);
31727c478bd9Sstevel@tonic-gate 		}
31737c478bd9Sstevel@tonic-gate 	}
31747c478bd9Sstevel@tonic-gate 
31757c478bd9Sstevel@tonic-gate 	/*
31767c478bd9Sstevel@tonic-gate 	 * flush the TLBs - since we're probably dealing with MANY mappings
31777c478bd9Sstevel@tonic-gate 	 * we do just one CR3 reload.
31787c478bd9Sstevel@tonic-gate 	 */
31797c478bd9Sstevel@tonic-gate 	if (!(hat->hat_flags & HAT_FREEING) && need_demaps)
3180ae115bc7Smrj 		hat_tlb_inval(hat, DEMAP_ALL_ADDR);
31817c478bd9Sstevel@tonic-gate 
31827c478bd9Sstevel@tonic-gate 	/*
31837c478bd9Sstevel@tonic-gate 	 * Now go back and clean up any unaligned mappings that
31847c478bd9Sstevel@tonic-gate 	 * couldn't share pagetables.
31857c478bd9Sstevel@tonic-gate 	 */
3186250b7ff9Sjosephb 	if (!is_it_dism(hat, addr))
3187250b7ff9Sjosephb 		flags |= HAT_UNLOAD_UNLOCK;
3188250b7ff9Sjosephb 	hat_unload(hat, addr, len, flags);
3189843e1988Sjohnlev 	XPV_ALLOW_MIGRATE();
31907c478bd9Sstevel@tonic-gate }
31917c478bd9Sstevel@tonic-gate 
31927c478bd9Sstevel@tonic-gate 
31937c478bd9Sstevel@tonic-gate /*
31947c478bd9Sstevel@tonic-gate  * hat_reserve() does nothing
31957c478bd9Sstevel@tonic-gate  */
31967c478bd9Sstevel@tonic-gate /*ARGSUSED*/
31977c478bd9Sstevel@tonic-gate void
31987c478bd9Sstevel@tonic-gate hat_reserve(struct as *as, caddr_t addr, size_t len)
31997c478bd9Sstevel@tonic-gate {
32007c478bd9Sstevel@tonic-gate }
32017c478bd9Sstevel@tonic-gate 
32027c478bd9Sstevel@tonic-gate 
32037c478bd9Sstevel@tonic-gate /*
32047c478bd9Sstevel@tonic-gate  * Called when all mappings to a page should have write permission removed.
320521584dbcSPavel Tatashin  * Mostly stolen from hat_pagesync()
32067c478bd9Sstevel@tonic-gate  */
32077c478bd9Sstevel@tonic-gate static void
32087c478bd9Sstevel@tonic-gate hati_page_clrwrt(struct page *pp)
32097c478bd9Sstevel@tonic-gate {
32107c478bd9Sstevel@tonic-gate 	hment_t		*hm = NULL;
32117c478bd9Sstevel@tonic-gate 	htable_t	*ht;
32127c478bd9Sstevel@tonic-gate 	uint_t		entry;
32137c478bd9Sstevel@tonic-gate 	x86pte_t	old;
32147c478bd9Sstevel@tonic-gate 	x86pte_t	new;
32157c478bd9Sstevel@tonic-gate 	uint_t		pszc = 0;
32167c478bd9Sstevel@tonic-gate 
3217843e1988Sjohnlev 	XPV_DISALLOW_MIGRATE();
32187c478bd9Sstevel@tonic-gate next_size:
32197c478bd9Sstevel@tonic-gate 	/*
32207c478bd9Sstevel@tonic-gate 	 * walk thru the mapping list clearing write permission
32217c478bd9Sstevel@tonic-gate 	 */
32227c478bd9Sstevel@tonic-gate 	x86_hm_enter(pp);
32237c478bd9Sstevel@tonic-gate 	while ((hm = hment_walk(pp, &ht, &entry, hm)) != NULL) {
32247c478bd9Sstevel@tonic-gate 		if (ht->ht_level < pszc)
32257c478bd9Sstevel@tonic-gate 			continue;
32267c478bd9Sstevel@tonic-gate 		old = x86pte_get(ht, entry);
32277c478bd9Sstevel@tonic-gate 
32287c478bd9Sstevel@tonic-gate 		for (;;) {
32297c478bd9Sstevel@tonic-gate 			/*
32307c478bd9Sstevel@tonic-gate 			 * Is this mapping of interest?
32317c478bd9Sstevel@tonic-gate 			 */
32327c478bd9Sstevel@tonic-gate 			if (PTE2PFN(old, ht->ht_level) != pp->p_pagenum ||
32337c478bd9Sstevel@tonic-gate 			    PTE_GET(old, PT_WRITABLE) == 0)
32347c478bd9Sstevel@tonic-gate 				break;
32357c478bd9Sstevel@tonic-gate 
32367c478bd9Sstevel@tonic-gate 			/*
32377c478bd9Sstevel@tonic-gate 			 * Clear ref/mod writable bits. This requires cross
32387c478bd9Sstevel@tonic-gate 			 * calls to ensure any executing TLBs see cleared bits.
32397c478bd9Sstevel@tonic-gate 			 */
32407c478bd9Sstevel@tonic-gate 			new = old;
32417c478bd9Sstevel@tonic-gate 			PTE_CLR(new, PT_REF | PT_MOD | PT_WRITABLE);
32427c478bd9Sstevel@tonic-gate 			old = hati_update_pte(ht, entry, old, new);
32437c478bd9Sstevel@tonic-gate 			if (old != 0)
32447c478bd9Sstevel@tonic-gate 				continue;
32457c478bd9Sstevel@tonic-gate 
32467c478bd9Sstevel@tonic-gate 			break;
32477c478bd9Sstevel@tonic-gate 		}
32487c478bd9Sstevel@tonic-gate 	}
32497c478bd9Sstevel@tonic-gate 	x86_hm_exit(pp);
32507c478bd9Sstevel@tonic-gate 	while (pszc < pp->p_szc) {
32517c478bd9Sstevel@tonic-gate 		page_t *tpp;
32527c478bd9Sstevel@tonic-gate 		pszc++;
32537c478bd9Sstevel@tonic-gate 		tpp = PP_GROUPLEADER(pp, pszc);
32547c478bd9Sstevel@tonic-gate 		if (pp != tpp) {
32557c478bd9Sstevel@tonic-gate 			pp = tpp;
32567c478bd9Sstevel@tonic-gate 			goto next_size;
32577c478bd9Sstevel@tonic-gate 		}
32587c478bd9Sstevel@tonic-gate 	}
3259843e1988Sjohnlev 	XPV_ALLOW_MIGRATE();
32607c478bd9Sstevel@tonic-gate }
32617c478bd9Sstevel@tonic-gate 
32627c478bd9Sstevel@tonic-gate /*
32637c478bd9Sstevel@tonic-gate  * void hat_page_setattr(pp, flag)
32647c478bd9Sstevel@tonic-gate  * void hat_page_clrattr(pp, flag)
32657c478bd9Sstevel@tonic-gate  *	used to set/clr ref/mod bits.
32667c478bd9Sstevel@tonic-gate  */
32677c478bd9Sstevel@tonic-gate void
32687c478bd9Sstevel@tonic-gate hat_page_setattr(struct page *pp, uint_t flag)
32697c478bd9Sstevel@tonic-gate {
32707c478bd9Sstevel@tonic-gate 	vnode_t		*vp = pp->p_vnode;
32717c478bd9Sstevel@tonic-gate 	kmutex_t	*vphm = NULL;
32727c478bd9Sstevel@tonic-gate 	page_t		**listp;
3273d9615970Sqiao 	int		noshuffle;
3274d9615970Sqiao 
3275d9615970Sqiao 	noshuffle = flag & P_NSH;
3276d9615970Sqiao 	flag &= ~P_NSH;
32777c478bd9Sstevel@tonic-gate 
32787c478bd9Sstevel@tonic-gate 	if (PP_GETRM(pp, flag) == flag)
32797c478bd9Sstevel@tonic-gate 		return;
32807c478bd9Sstevel@tonic-gate 
3281d9615970Sqiao 	if ((flag & P_MOD) != 0 && vp != NULL && IS_VMODSORT(vp) &&
3282d9615970Sqiao 	    !noshuffle) {
32837c478bd9Sstevel@tonic-gate 		vphm = page_vnode_mutex(vp);
32847c478bd9Sstevel@tonic-gate 		mutex_enter(vphm);
32857c478bd9Sstevel@tonic-gate 	}
32867c478bd9Sstevel@tonic-gate 
32877c478bd9Sstevel@tonic-gate 	PP_SETRM(pp, flag);
32887c478bd9Sstevel@tonic-gate 
32897c478bd9Sstevel@tonic-gate 	if (vphm != NULL) {
32907c478bd9Sstevel@tonic-gate 
32917c478bd9Sstevel@tonic-gate 		/*
32927c478bd9Sstevel@tonic-gate 		 * Some File Systems examine v_pages for NULL w/o
32937c478bd9Sstevel@tonic-gate 		 * grabbing the vphm mutex. Must not let it become NULL when
32947c478bd9Sstevel@tonic-gate 		 * pp is the only page on the list.
32957c478bd9Sstevel@tonic-gate 		 */
32967c478bd9Sstevel@tonic-gate 		if (pp->p_vpnext != pp) {
32977c478bd9Sstevel@tonic-gate 			page_vpsub(&vp->v_pages, pp);
32987c478bd9Sstevel@tonic-gate 			if (vp->v_pages != NULL)
32997c478bd9Sstevel@tonic-gate 				listp = &vp->v_pages->p_vpprev->p_vpnext;
33007c478bd9Sstevel@tonic-gate 			else
33017c478bd9Sstevel@tonic-gate 				listp = &vp->v_pages;
33027c478bd9Sstevel@tonic-gate 			page_vpadd(listp, pp);
33037c478bd9Sstevel@tonic-gate 		}
33047c478bd9Sstevel@tonic-gate 		mutex_exit(vphm);
33057c478bd9Sstevel@tonic-gate 	}
33067c478bd9Sstevel@tonic-gate }
33077c478bd9Sstevel@tonic-gate 
33087c478bd9Sstevel@tonic-gate void
33097c478bd9Sstevel@tonic-gate hat_page_clrattr(struct page *pp, uint_t flag)
33107c478bd9Sstevel@tonic-gate {
33117c478bd9Sstevel@tonic-gate 	vnode_t		*vp = pp->p_vnode;
33127c478bd9Sstevel@tonic-gate 	ASSERT(!(flag & ~(P_MOD | P_REF | P_RO)));
33137c478bd9Sstevel@tonic-gate 
33147c478bd9Sstevel@tonic-gate 	/*
3315a71e32b6Sstans 	 * Caller is expected to hold page's io lock for VMODSORT to work
3316a71e32b6Sstans 	 * correctly with pvn_vplist_dirty() and pvn_getdirty() when mod
3317a71e32b6Sstans 	 * bit is cleared.
3318a71e32b6Sstans 	 * We don't have assert to avoid tripping some existing third party
3319a71e32b6Sstans 	 * code. The dirty page is moved back to top of the v_page list
3320a71e32b6Sstans 	 * after IO is done in pvn_write_done().
33217c478bd9Sstevel@tonic-gate 	 */
33227c478bd9Sstevel@tonic-gate 	PP_CLRRM(pp, flag);
33237c478bd9Sstevel@tonic-gate 
3324a71e32b6Sstans 	if ((flag & P_MOD) != 0 && vp != NULL && IS_VMODSORT(vp)) {
33257c478bd9Sstevel@tonic-gate 
33267c478bd9Sstevel@tonic-gate 		/*
33277c478bd9Sstevel@tonic-gate 		 * VMODSORT works by removing write permissions and getting
33287c478bd9Sstevel@tonic-gate 		 * a fault when a page is made dirty. At this point
33297c478bd9Sstevel@tonic-gate 		 * we need to remove write permission from all mappings
33307c478bd9Sstevel@tonic-gate 		 * to this page.
33317c478bd9Sstevel@tonic-gate 		 */
33327c478bd9Sstevel@tonic-gate 		hati_page_clrwrt(pp);
33337c478bd9Sstevel@tonic-gate 	}
33347c478bd9Sstevel@tonic-gate }
33357c478bd9Sstevel@tonic-gate 
33367c478bd9Sstevel@tonic-gate /*
33377c478bd9Sstevel@tonic-gate  *	If flag is specified, returns 0 if attribute is disabled
333821584dbcSPavel Tatashin  *	and non zero if enabled.  If flag specifes multiple attributes
333921584dbcSPavel Tatashin  *	then returns 0 if ALL attributes are disabled.  This is an advisory
33407c478bd9Sstevel@tonic-gate  *	call.
33417c478bd9Sstevel@tonic-gate  */
33427c478bd9Sstevel@tonic-gate uint_t
33437c478bd9Sstevel@tonic-gate hat_page_getattr(struct page *pp, uint_t flag)
33447c478bd9Sstevel@tonic-gate {
33457c478bd9Sstevel@tonic-gate 	return (PP_GETRM(pp, flag));
33467c478bd9Sstevel@tonic-gate }
33477c478bd9Sstevel@tonic-gate 
33487c478bd9Sstevel@tonic-gate 
33497c478bd9Sstevel@tonic-gate /*
33507c478bd9Sstevel@tonic-gate  * common code used by hat_pageunload() and hment_steal()
33517c478bd9Sstevel@tonic-gate  */
33527c478bd9Sstevel@tonic-gate hment_t *
33537c478bd9Sstevel@tonic-gate hati_page_unmap(page_t *pp, htable_t *ht, uint_t entry)
33547c478bd9Sstevel@tonic-gate {
33557c478bd9Sstevel@tonic-gate 	x86pte_t old_pte;
33567c478bd9Sstevel@tonic-gate 	pfn_t pfn = pp->p_pagenum;
33577c478bd9Sstevel@tonic-gate 	hment_t *hm;
33587c478bd9Sstevel@tonic-gate 
33597c478bd9Sstevel@tonic-gate 	/*
33607c478bd9Sstevel@tonic-gate 	 * We need to acquire a hold on the htable in order to
33617c478bd9Sstevel@tonic-gate 	 * do the invalidate. We know the htable must exist, since
33627c478bd9Sstevel@tonic-gate 	 * unmap's don't release the htable until after removing any
33637c478bd9Sstevel@tonic-gate 	 * hment. Having x86_hm_enter() keeps that from proceeding.
33647c478bd9Sstevel@tonic-gate 	 */
33657c478bd9Sstevel@tonic-gate 	htable_acquire(ht);
33667c478bd9Sstevel@tonic-gate 
33677c478bd9Sstevel@tonic-gate 	/*
33687c478bd9Sstevel@tonic-gate 	 * Invalidate the PTE and remove the hment.
33697c478bd9Sstevel@tonic-gate 	 */
3370ae115bc7Smrj 	old_pte = x86pte_inval(ht, entry, 0, NULL);
3371aa2ed9e5Sjosephb 	if (PTE2PFN(old_pte, ht->ht_level) != pfn) {
3372ae115bc7Smrj 		panic("x86pte_inval() failure found PTE = " FMT_PTE
3373aa2ed9e5Sjosephb 		    " pfn being unmapped is %lx ht=0x%lx entry=0x%x",
3374aa2ed9e5Sjosephb 		    old_pte, pfn, (uintptr_t)ht, entry);
3375aa2ed9e5Sjosephb 	}
33767c478bd9Sstevel@tonic-gate 
33777c478bd9Sstevel@tonic-gate 	/*
33787c478bd9Sstevel@tonic-gate 	 * Clean up all the htable information for this mapping
33797c478bd9Sstevel@tonic-gate 	 */
33807c478bd9Sstevel@tonic-gate 	ASSERT(ht->ht_valid_cnt > 0);
33817c478bd9Sstevel@tonic-gate 	HTABLE_DEC(ht->ht_valid_cnt);
33827c478bd9Sstevel@tonic-gate 	PGCNT_DEC(ht->ht_hat, ht->ht_level);
33837c478bd9Sstevel@tonic-gate 
33847c478bd9Sstevel@tonic-gate 	/*
33857c478bd9Sstevel@tonic-gate 	 * sync ref/mod bits to the page_t
33867c478bd9Sstevel@tonic-gate 	 */
3387ae115bc7Smrj 	if (PTE_GET(old_pte, PT_SOFTWARE) < PT_NOSYNC)
33887c478bd9Sstevel@tonic-gate 		hati_sync_pte_to_page(pp, old_pte, ht->ht_level);
33897c478bd9Sstevel@tonic-gate 
33907c478bd9Sstevel@tonic-gate 	/*
33917c478bd9Sstevel@tonic-gate 	 * Remove the mapping list entry for this page.
33927c478bd9Sstevel@tonic-gate 	 */
33937c478bd9Sstevel@tonic-gate 	hm = hment_remove(pp, ht, entry);
33947c478bd9Sstevel@tonic-gate 
33957c478bd9Sstevel@tonic-gate 	/*
33967c478bd9Sstevel@tonic-gate 	 * drop the mapping list lock so that we might free the
33977c478bd9Sstevel@tonic-gate 	 * hment and htable.
33987c478bd9Sstevel@tonic-gate 	 */
33997c478bd9Sstevel@tonic-gate 	x86_hm_exit(pp);
34007c478bd9Sstevel@tonic-gate 	htable_release(ht);
34017c478bd9Sstevel@tonic-gate 	return (hm);
34027c478bd9Sstevel@tonic-gate }
34037c478bd9Sstevel@tonic-gate 
3404a5652762Spraks extern int	vpm_enable;
34057c478bd9Sstevel@tonic-gate /*
34067c478bd9Sstevel@tonic-gate  * Unload all translations to a page. If the page is a subpage of a large
34077c478bd9Sstevel@tonic-gate  * page, the large page mappings are also removed.
34087c478bd9Sstevel@tonic-gate  *
34097c478bd9Sstevel@tonic-gate  * The forceflags are unused.
34107c478bd9Sstevel@tonic-gate  */
34117c478bd9Sstevel@tonic-gate 
34127c478bd9Sstevel@tonic-gate /*ARGSUSED*/
34137c478bd9Sstevel@tonic-gate static int
34147c478bd9Sstevel@tonic-gate hati_pageunload(struct page *pp, uint_t pg_szcd, uint_t forceflag)
34157c478bd9Sstevel@tonic-gate {
34167c478bd9Sstevel@tonic-gate 	page_t		*cur_pp = pp;
34177c478bd9Sstevel@tonic-gate 	hment_t		*hm;
34187c478bd9Sstevel@tonic-gate 	hment_t		*prev;
34197c478bd9Sstevel@tonic-gate 	htable_t	*ht;
34207c478bd9Sstevel@tonic-gate 	uint_t		entry;
34217c478bd9Sstevel@tonic-gate 	level_t		level;
34227c478bd9Sstevel@tonic-gate 
3423843e1988Sjohnlev 	XPV_DISALLOW_MIGRATE();
3424a5652762Spraks #if defined(__amd64)
3425a5652762Spraks 	/*
3426a5652762Spraks 	 * clear the vpm ref.
3427a5652762Spraks 	 */
3428a5652762Spraks 	if (vpm_enable) {
3429a5652762Spraks 		pp->p_vpmref = 0;
3430a5652762Spraks 	}
3431a5652762Spraks #endif
34327c478bd9Sstevel@tonic-gate 	/*
34337c478bd9Sstevel@tonic-gate 	 * The loop with next_size handles pages with multiple pagesize mappings
34347c478bd9Sstevel@tonic-gate 	 */
34357c478bd9Sstevel@tonic-gate next_size:
34367c478bd9Sstevel@tonic-gate 	for (;;) {
34377c478bd9Sstevel@tonic-gate 
34387c478bd9Sstevel@tonic-gate 		/*
34397c478bd9Sstevel@tonic-gate 		 * Get a mapping list entry
34407c478bd9Sstevel@tonic-gate 		 */
34417c478bd9Sstevel@tonic-gate 		x86_hm_enter(cur_pp);
34427c478bd9Sstevel@tonic-gate 		for (prev = NULL; ; prev = hm) {
34437c478bd9Sstevel@tonic-gate 			hm = hment_walk(cur_pp, &ht, &entry, prev);
34447c478bd9Sstevel@tonic-gate 			if (hm == NULL) {
34457c478bd9Sstevel@tonic-gate 				x86_hm_exit(cur_pp);
34467c478bd9Sstevel@tonic-gate 
34477c478bd9Sstevel@tonic-gate 				/*
34487c478bd9Sstevel@tonic-gate 				 * If not part of a larger page, we're done.
34497c478bd9Sstevel@tonic-gate 				 */
3450ae115bc7Smrj 				if (cur_pp->p_szc <= pg_szcd) {
3451843e1988Sjohnlev 					XPV_ALLOW_MIGRATE();
34527c478bd9Sstevel@tonic-gate 					return (0);
3453ae115bc7Smrj 				}
34547c478bd9Sstevel@tonic-gate 
34557c478bd9Sstevel@tonic-gate 				/*
34567c478bd9Sstevel@tonic-gate 				 * Else check the next larger page size.
34577c478bd9Sstevel@tonic-gate 				 * hat_page_demote() may decrease p_szc
34587c478bd9Sstevel@tonic-gate 				 * but that's ok we'll just take an extra
34597c478bd9Sstevel@tonic-gate 				 * trip discover there're no larger mappings
34607c478bd9Sstevel@tonic-gate 				 * and return.
34617c478bd9Sstevel@tonic-gate 				 */
34627c478bd9Sstevel@tonic-gate 				++pg_szcd;
34637c478bd9Sstevel@tonic-gate 				cur_pp = PP_GROUPLEADER(cur_pp, pg_szcd);
34647c478bd9Sstevel@tonic-gate 				goto next_size;
34657c478bd9Sstevel@tonic-gate 			}
34667c478bd9Sstevel@tonic-gate 
34677c478bd9Sstevel@tonic-gate 			/*
34687c478bd9Sstevel@tonic-gate 			 * If this mapping size matches, remove it.
34697c478bd9Sstevel@tonic-gate 			 */
34707c478bd9Sstevel@tonic-gate 			level = ht->ht_level;
34717c478bd9Sstevel@tonic-gate 			if (level == pg_szcd)
34727c478bd9Sstevel@tonic-gate 				break;
34737c478bd9Sstevel@tonic-gate 		}
34747c478bd9Sstevel@tonic-gate 
34757c478bd9Sstevel@tonic-gate 		/*
34767c478bd9Sstevel@tonic-gate 		 * Remove the mapping list entry for this page.
34777c478bd9Sstevel@tonic-gate 		 * Note this does the x86_hm_exit() for us.
34787c478bd9Sstevel@tonic-gate 		 */
34797c478bd9Sstevel@tonic-gate 		hm = hati_page_unmap(cur_pp, ht, entry);
34807c478bd9Sstevel@tonic-gate 		if (hm != NULL)
34817c478bd9Sstevel@tonic-gate 			hment_free(hm);
34827c478bd9Sstevel@tonic-gate 	}
34837c478bd9Sstevel@tonic-gate }
34847c478bd9Sstevel@tonic-gate 
34857c478bd9Sstevel@tonic-gate int
34867c478bd9Sstevel@tonic-gate hat_pageunload(struct page *pp, uint_t forceflag)
34877c478bd9Sstevel@tonic-gate {
34887c478bd9Sstevel@tonic-gate 	ASSERT(PAGE_EXCL(pp));
34897c478bd9Sstevel@tonic-gate 	return (hati_pageunload(pp, 0, forceflag));
34907c478bd9Sstevel@tonic-gate }
34917c478bd9Sstevel@tonic-gate 
34927c478bd9Sstevel@tonic-gate /*
34937c478bd9Sstevel@tonic-gate  * Unload all large mappings to pp and reduce by 1 p_szc field of every large
34947c478bd9Sstevel@tonic-gate  * page level that included pp.
34957c478bd9Sstevel@tonic-gate  *
34967c478bd9Sstevel@tonic-gate  * pp must be locked EXCL. Even though no other constituent pages are locked
34977c478bd9Sstevel@tonic-gate  * it's legal to unload large mappings to pp because all constituent pages of
34987c478bd9Sstevel@tonic-gate  * large locked mappings have to be locked SHARED.  therefore if we have EXCL
34997c478bd9Sstevel@tonic-gate  * lock on one of constituent pages none of the large mappings to pp are
35007c478bd9Sstevel@tonic-gate  * locked.
35017c478bd9Sstevel@tonic-gate  *
35027c478bd9Sstevel@tonic-gate  * Change (always decrease) p_szc field starting from the last constituent
35037c478bd9Sstevel@tonic-gate  * page and ending with root constituent page so that root's pszc always shows
35047c478bd9Sstevel@tonic-gate  * the area where hat_page_demote() may be active.
35057c478bd9Sstevel@tonic-gate  *
35067c478bd9Sstevel@tonic-gate  * This mechanism is only used for file system pages where it's not always
35077c478bd9Sstevel@tonic-gate  * possible to get EXCL locks on all constituent pages to demote the size code
35087c478bd9Sstevel@tonic-gate  * (as is done for anonymous or kernel large pages).
35097c478bd9Sstevel@tonic-gate  */
35107c478bd9Sstevel@tonic-gate void
35117c478bd9Sstevel@tonic-gate hat_page_demote(page_t *pp)
35127c478bd9Sstevel@tonic-gate {
35137c478bd9Sstevel@tonic-gate 	uint_t		pszc;
35147c478bd9Sstevel@tonic-gate 	uint_t		rszc;
35157c478bd9Sstevel@tonic-gate 	uint_t		szc;
35167c478bd9Sstevel@tonic-gate 	page_t		*rootpp;
35177c478bd9Sstevel@tonic-gate 	page_t		*firstpp;
35187c478bd9Sstevel@tonic-gate 	page_t		*lastpp;
35197c478bd9Sstevel@tonic-gate 	pgcnt_t		pgcnt;
35207c478bd9Sstevel@tonic-gate 
35217c478bd9Sstevel@tonic-gate 	ASSERT(PAGE_EXCL(pp));
35227c478bd9Sstevel@tonic-gate 	ASSERT(!PP_ISFREE(pp));
35237c478bd9Sstevel@tonic-gate 	ASSERT(page_szc_lock_assert(pp));
35247c478bd9Sstevel@tonic-gate 
35257c478bd9Sstevel@tonic-gate 	if (pp->p_szc == 0)
35267c478bd9Sstevel@tonic-gate 		return;
35277c478bd9Sstevel@tonic-gate 
35287c478bd9Sstevel@tonic-gate 	rootpp = PP_GROUPLEADER(pp, 1);
35297c478bd9Sstevel@tonic-gate 	(void) hati_pageunload(rootpp, 1, HAT_FORCE_PGUNLOAD);
35307c478bd9Sstevel@tonic-gate 
35317c478bd9Sstevel@tonic-gate 	/*
35327c478bd9Sstevel@tonic-gate 	 * all large mappings to pp are gone
35337c478bd9Sstevel@tonic-gate 	 * and no new can be setup since pp is locked exclusively.
35347c478bd9Sstevel@tonic-gate 	 *
35357c478bd9Sstevel@tonic-gate 	 * Lock the root to make sure there's only one hat_page_demote()
35367c478bd9Sstevel@tonic-gate 	 * outstanding within the area of this root's pszc.
35377c478bd9Sstevel@tonic-gate 	 *
35387c478bd9Sstevel@tonic-gate 	 * Second potential hat_page_demote() is already eliminated by upper
35397c478bd9Sstevel@tonic-gate 	 * VM layer via page_szc_lock() but we don't rely on it and use our
35407c478bd9Sstevel@tonic-gate 	 * own locking (so that upper layer locking can be changed without
35417c478bd9Sstevel@tonic-gate 	 * assumptions that hat depends on upper layer VM to prevent multiple
35427c478bd9Sstevel@tonic-gate 	 * hat_page_demote() to be issued simultaneously to the same large
35437c478bd9Sstevel@tonic-gate 	 * page).
35447c478bd9Sstevel@tonic-gate 	 */
35457c478bd9Sstevel@tonic-gate again:
35467c478bd9Sstevel@tonic-gate 	pszc = pp->p_szc;
35477c478bd9Sstevel@tonic-gate 	if (pszc == 0)
35487c478bd9Sstevel@tonic-gate 		return;
35497c478bd9Sstevel@tonic-gate 	rootpp = PP_GROUPLEADER(pp, pszc);
35507c478bd9Sstevel@tonic-gate 	x86_hm_enter(rootpp);
35517c478bd9Sstevel@tonic-gate 	/*
35527c478bd9Sstevel@tonic-gate 	 * If root's p_szc is different from pszc we raced with another
35537c478bd9Sstevel@tonic-gate 	 * hat_page_demote().  Drop the lock and try to find the root again.
35547c478bd9Sstevel@tonic-gate 	 * If root's p_szc is greater than pszc previous hat_page_demote() is
35557c478bd9Sstevel@tonic-gate 	 * not done yet.  Take and release mlist lock of root's root to wait
35567c478bd9Sstevel@tonic-gate 	 * for previous hat_page_demote() to complete.
35577c478bd9Sstevel@tonic-gate 	 */
35587c478bd9Sstevel@tonic-gate 	if ((rszc = rootpp->p_szc) != pszc) {
35597c478bd9Sstevel@tonic-gate 		x86_hm_exit(rootpp);
35607c478bd9Sstevel@tonic-gate 		if (rszc > pszc) {
35617c478bd9Sstevel@tonic-gate 			/* p_szc of a locked non free page can't increase */
35627c478bd9Sstevel@tonic-gate 			ASSERT(pp != rootpp);
35637c478bd9Sstevel@tonic-gate 
35647c478bd9Sstevel@tonic-gate 			rootpp = PP_GROUPLEADER(rootpp, rszc);
35657c478bd9Sstevel@tonic-gate 			x86_hm_enter(rootpp);
35667c478bd9Sstevel@tonic-gate 			x86_hm_exit(rootpp);
35677c478bd9Sstevel@tonic-gate 		}
35687c478bd9Sstevel@tonic-gate 		goto again;
35697c478bd9Sstevel@tonic-gate 	}
35707c478bd9Sstevel@tonic-gate 	ASSERT(pp->p_szc == pszc);
35717c478bd9Sstevel@tonic-gate 
35727c478bd9Sstevel@tonic-gate 	/*
35737c478bd9Sstevel@tonic-gate 	 * Decrement by 1 p_szc of every constituent page of a region that
35747c478bd9Sstevel@tonic-gate 	 * covered pp. For example if original szc is 3 it gets changed to 2
35757c478bd9Sstevel@tonic-gate 	 * everywhere except in region 2 that covered pp. Region 2 that
35767c478bd9Sstevel@tonic-gate 	 * covered pp gets demoted to 1 everywhere except in region 1 that
35777c478bd9Sstevel@tonic-gate 	 * covered pp. The region 1 that covered pp is demoted to region
35787c478bd9Sstevel@tonic-gate 	 * 0. It's done this way because from region 3 we removed level 3
35797c478bd9Sstevel@tonic-gate 	 * mappings, from region 2 that covered pp we removed level 2 mappings
35807c478bd9Sstevel@tonic-gate 	 * and from region 1 that covered pp we removed level 1 mappings.  All
35817c478bd9Sstevel@tonic-gate 	 * changes are done from from high pfn's to low pfn's so that roots
35827c478bd9Sstevel@tonic-gate 	 * are changed last allowing one to know the largest region where
35837c478bd9Sstevel@tonic-gate 	 * hat_page_demote() is stil active by only looking at the root page.
35847c478bd9Sstevel@tonic-gate 	 *
35857c478bd9Sstevel@tonic-gate 	 * This algorithm is implemented in 2 while loops. First loop changes
35867c478bd9Sstevel@tonic-gate 	 * p_szc of pages to the right of pp's level 1 region and second
35877c478bd9Sstevel@tonic-gate 	 * loop changes p_szc of pages of level 1 region that covers pp
35887c478bd9Sstevel@tonic-gate 	 * and all pages to the left of level 1 region that covers pp.
35897c478bd9Sstevel@tonic-gate 	 * In the first loop p_szc keeps dropping with every iteration
35907c478bd9Sstevel@tonic-gate 	 * and in the second loop it keeps increasing with every iteration.
35917c478bd9Sstevel@tonic-gate 	 *
35927c478bd9Sstevel@tonic-gate 	 * First loop description: Demote pages to the right of pp outside of
35937c478bd9Sstevel@tonic-gate 	 * level 1 region that covers pp.  In every iteration of the while
35947c478bd9Sstevel@tonic-gate 	 * loop below find the last page of szc region and the first page of
35957c478bd9Sstevel@tonic-gate 	 * (szc - 1) region that is immediately to the right of (szc - 1)
35967c478bd9Sstevel@tonic-gate 	 * region that covers pp.  From last such page to first such page
35977c478bd9Sstevel@tonic-gate 	 * change every page's szc to szc - 1. Decrement szc and continue
35987c478bd9Sstevel@tonic-gate 	 * looping until szc is 1. If pp belongs to the last (szc - 1) region
35997c478bd9Sstevel@tonic-gate 	 * of szc region skip to the next iteration.
36007c478bd9Sstevel@tonic-gate 	 */
36017c478bd9Sstevel@tonic-gate 	szc = pszc;
36027c478bd9Sstevel@tonic-gate 	while (szc > 1) {
36037c478bd9Sstevel@tonic-gate 		lastpp = PP_GROUPLEADER(pp, szc);
36047c478bd9Sstevel@tonic-gate 		pgcnt = page_get_pagecnt(szc);
36057c478bd9Sstevel@tonic-gate 		lastpp += pgcnt - 1;
36067c478bd9Sstevel@tonic-gate 		firstpp = PP_GROUPLEADER(pp, (szc - 1));
36077c478bd9Sstevel@tonic-gate 		pgcnt = page_get_pagecnt(szc - 1);
36087c478bd9Sstevel@tonic-gate 		if (lastpp - firstpp < pgcnt) {
36097c478bd9Sstevel@tonic-gate 			szc--;
36107c478bd9Sstevel@tonic-gate 			continue;
36117c478bd9Sstevel@tonic-gate 		}
36127c478bd9Sstevel@tonic-gate 		firstpp += pgcnt;
36137c478bd9Sstevel@tonic-gate 		while (lastpp != firstpp) {
36147c478bd9Sstevel@tonic-gate 			ASSERT(lastpp->p_szc == pszc);
36157c478bd9Sstevel@tonic-gate 			lastpp->p_szc = szc - 1;
36167c478bd9Sstevel@tonic-gate 			lastpp--;
36177c478bd9Sstevel@tonic-gate 		}
36187c478bd9Sstevel@tonic-gate 		firstpp->p_szc = szc - 1;
36197c478bd9Sstevel@tonic-gate 		szc--;
36207c478bd9Sstevel@tonic-gate 	}
36217c478bd9Sstevel@tonic-gate 
36227c478bd9Sstevel@tonic-gate 	/*
36237c478bd9Sstevel@tonic-gate 	 * Second loop description:
36247c478bd9Sstevel@tonic-gate 	 * First iteration changes p_szc to 0 of every
36257c478bd9Sstevel@tonic-gate 	 * page of level 1 region that covers pp.
36267c478bd9Sstevel@tonic-gate 	 * Subsequent iterations find last page of szc region
36277c478bd9Sstevel@tonic-gate 	 * immediately to the left of szc region that covered pp
36287c478bd9Sstevel@tonic-gate 	 * and first page of (szc + 1) region that covers pp.
36297c478bd9Sstevel@tonic-gate 	 * From last to first page change p_szc of every page to szc.
36307c478bd9Sstevel@tonic-gate 	 * Increment szc and continue looping until szc is pszc.
36317c478bd9Sstevel@tonic-gate 	 * If pp belongs to the fist szc region of (szc + 1) region
36327c478bd9Sstevel@tonic-gate 	 * skip to the next iteration.
36337c478bd9Sstevel@tonic-gate 	 *
36347c478bd9Sstevel@tonic-gate 	 */
36357c478bd9Sstevel@tonic-gate 	szc = 0;
36367c478bd9Sstevel@tonic-gate 	while (szc < pszc) {
36377c478bd9Sstevel@tonic-gate 		firstpp = PP_GROUPLEADER(pp, (szc + 1));
36387c478bd9Sstevel@tonic-gate 		if (szc == 0) {
36397c478bd9Sstevel@tonic-gate 			pgcnt = page_get_pagecnt(1);
36407c478bd9Sstevel@tonic-gate 			lastpp = firstpp + (pgcnt - 1);
36417c478bd9Sstevel@tonic-gate 		} else {
36427c478bd9Sstevel@tonic-gate 			lastpp = PP_GROUPLEADER(pp, szc);
36437c478bd9Sstevel@tonic-gate 			if (firstpp == lastpp) {
36447c478bd9Sstevel@tonic-gate 				szc++;
36457c478bd9Sstevel@tonic-gate 				continue;
36467c478bd9Sstevel@tonic-gate 			}
36477c478bd9Sstevel@tonic-gate 			lastpp--;
36487c478bd9Sstevel@tonic-gate 			pgcnt = page_get_pagecnt(szc);
36497c478bd9Sstevel@tonic-gate 		}
36507c478bd9Sstevel@tonic-gate 		while (lastpp != firstpp) {
36517c478bd9Sstevel@tonic-gate 			ASSERT(lastpp->p_szc == pszc);
36527c478bd9Sstevel@tonic-gate 			lastpp->p_szc = szc;
36537c478bd9Sstevel@tonic-gate 			lastpp--;
36547c478bd9Sstevel@tonic-gate 		}
36557c478bd9Sstevel@tonic-gate 		firstpp->p_szc = szc;
36567c478bd9Sstevel@tonic-gate 		if (firstpp == rootpp)
36577c478bd9Sstevel@tonic-gate 			break;
36587c478bd9Sstevel@tonic-gate 		szc++;
36597c478bd9Sstevel@tonic-gate 	}
36607c478bd9Sstevel@tonic-gate 	x86_hm_exit(rootpp);
36617c478bd9Sstevel@tonic-gate }
36627c478bd9Sstevel@tonic-gate 
36637c478bd9Sstevel@tonic-gate /*
36647c478bd9Sstevel@tonic-gate  * get hw stats from hardware into page struct and reset hw stats
36657c478bd9Sstevel@tonic-gate  * returns attributes of page
36667c478bd9Sstevel@tonic-gate  * Flags for hat_pagesync, hat_getstat, hat_sync
36677c478bd9Sstevel@tonic-gate  *
36687c478bd9Sstevel@tonic-gate  * define	HAT_SYNC_ZERORM		0x01
36697c478bd9Sstevel@tonic-gate  *
36707c478bd9Sstevel@tonic-gate  * Additional flags for hat_pagesync
36717c478bd9Sstevel@tonic-gate  *
36727c478bd9Sstevel@tonic-gate  * define	HAT_SYNC_STOPON_REF	0x02
36737c478bd9Sstevel@tonic-gate  * define	HAT_SYNC_STOPON_MOD	0x04
36747c478bd9Sstevel@tonic-gate  * define	HAT_SYNC_STOPON_RM	0x06
36757c478bd9Sstevel@tonic-gate  * define	HAT_SYNC_STOPON_SHARED	0x08
36767c478bd9Sstevel@tonic-gate  */
36777c478bd9Sstevel@tonic-gate uint_t
36787c478bd9Sstevel@tonic-gate hat_pagesync(struct page *pp, uint_t flags)
36797c478bd9Sstevel@tonic-gate {
36807c478bd9Sstevel@tonic-gate 	hment_t		*hm = NULL;
36817c478bd9Sstevel@tonic-gate 	htable_t	*ht;
36827c478bd9Sstevel@tonic-gate 	uint_t		entry;
36837c478bd9Sstevel@tonic-gate 	x86pte_t	old, save_old;
36847c478bd9Sstevel@tonic-gate 	x86pte_t	new;
36857c478bd9Sstevel@tonic-gate 	uchar_t		nrmbits = P_REF|P_MOD|P_RO;
36867c478bd9Sstevel@tonic-gate 	extern ulong_t	po_share;
36877c478bd9Sstevel@tonic-gate 	page_t		*save_pp = pp;
36887c478bd9Sstevel@tonic-gate 	uint_t		pszc = 0;
36897c478bd9Sstevel@tonic-gate 
36907c478bd9Sstevel@tonic-gate 	ASSERT(PAGE_LOCKED(pp) || panicstr);
36917c478bd9Sstevel@tonic-gate 
36927c478bd9Sstevel@tonic-gate 	if (PP_ISRO(pp) && (flags & HAT_SYNC_STOPON_MOD))
36937c478bd9Sstevel@tonic-gate 		return (pp->p_nrm & nrmbits);
36947c478bd9Sstevel@tonic-gate 
36957c478bd9Sstevel@tonic-gate 	if ((flags & HAT_SYNC_ZERORM) == 0) {
36967c478bd9Sstevel@tonic-gate 
36977c478bd9Sstevel@tonic-gate 		if ((flags & HAT_SYNC_STOPON_REF) != 0 && PP_ISREF(pp))
36987c478bd9Sstevel@tonic-gate 			return (pp->p_nrm & nrmbits);
36997c478bd9Sstevel@tonic-gate 
37007c478bd9Sstevel@tonic-gate 		if ((flags & HAT_SYNC_STOPON_MOD) != 0 && PP_ISMOD(pp))
37017c478bd9Sstevel@tonic-gate 			return (pp->p_nrm & nrmbits);
37027c478bd9Sstevel@tonic-gate 
37037c478bd9Sstevel@tonic-gate 		if ((flags & HAT_SYNC_STOPON_SHARED) != 0 &&
37047c478bd9Sstevel@tonic-gate 		    hat_page_getshare(pp) > po_share) {
37057c478bd9Sstevel@tonic-gate 			if (PP_ISRO(pp))
37067c478bd9Sstevel@tonic-gate 				PP_SETREF(pp);
37077c478bd9Sstevel@tonic-gate 			return (pp->p_nrm & nrmbits);
37087c478bd9Sstevel@tonic-gate 		}
37097c478bd9Sstevel@tonic-gate 	}
37107c478bd9Sstevel@tonic-gate 
3711843e1988Sjohnlev 	XPV_DISALLOW_MIGRATE();
37127c478bd9Sstevel@tonic-gate next_size:
37137c478bd9Sstevel@tonic-gate 	/*
37147c478bd9Sstevel@tonic-gate 	 * walk thru the mapping list syncing (and clearing) ref/mod bits.
37157c478bd9Sstevel@tonic-gate 	 */
37167c478bd9Sstevel@tonic-gate 	x86_hm_enter(pp);
37177c478bd9Sstevel@tonic-gate 	while ((hm = hment_walk(pp, &ht, &entry, hm)) != NULL) {
37187c478bd9Sstevel@tonic-gate 		if (ht->ht_level < pszc)
37197c478bd9Sstevel@tonic-gate 			continue;
37207c478bd9Sstevel@tonic-gate 		old = x86pte_get(ht, entry);
37217c478bd9Sstevel@tonic-gate try_again:
37227c478bd9Sstevel@tonic-gate 
37237c478bd9Sstevel@tonic-gate 		ASSERT(PTE2PFN(old, ht->ht_level) == pp->p_pagenum);
37247c478bd9Sstevel@tonic-gate 
37257c478bd9Sstevel@tonic-gate 		if (PTE_GET(old, PT_REF | PT_MOD) == 0)
37267c478bd9Sstevel@tonic-gate 			continue;
37277c478bd9Sstevel@tonic-gate 
37287c478bd9Sstevel@tonic-gate 		save_old = old;
37297c478bd9Sstevel@tonic-gate 		if ((flags & HAT_SYNC_ZERORM) != 0) {
37307c478bd9Sstevel@tonic-gate 
37317c478bd9Sstevel@tonic-gate 			/*
37327c478bd9Sstevel@tonic-gate 			 * Need to clear ref or mod bits. Need to demap
37337c478bd9Sstevel@tonic-gate 			 * to make sure any executing TLBs see cleared bits.
37347c478bd9Sstevel@tonic-gate 			 */
37357c478bd9Sstevel@tonic-gate 			new = old;
37367c478bd9Sstevel@tonic-gate 			PTE_CLR(new, PT_REF | PT_MOD);
37377c478bd9Sstevel@tonic-gate 			old = hati_update_pte(ht, entry, old, new);
37387c478bd9Sstevel@tonic-gate 			if (old != 0)
37397c478bd9Sstevel@tonic-gate 				goto try_again;
37407c478bd9Sstevel@tonic-gate 
37417c478bd9Sstevel@tonic-gate 			old = save_old;
37427c478bd9Sstevel@tonic-gate 		}
37437c478bd9Sstevel@tonic-gate 
37447c478bd9Sstevel@tonic-gate 		/*
37457c478bd9Sstevel@tonic-gate 		 * Sync the PTE
37467c478bd9Sstevel@tonic-gate 		 */
3747ae115bc7Smrj 		if (!(flags & HAT_SYNC_ZERORM) &&
3748ae115bc7Smrj 		    PTE_GET(old, PT_SOFTWARE) <= PT_NOSYNC)
37497c478bd9Sstevel@tonic-gate 			hati_sync_pte_to_page(pp, old, ht->ht_level);
37507c478bd9Sstevel@tonic-gate 
37517c478bd9Sstevel@tonic-gate 		/*
37527c478bd9Sstevel@tonic-gate 		 * can stop short if we found a ref'd or mod'd page
37537c478bd9Sstevel@tonic-gate 		 */
37547c478bd9Sstevel@tonic-gate 		if ((flags & HAT_SYNC_STOPON_MOD) && PP_ISMOD(save_pp) ||
37557c478bd9Sstevel@tonic-gate 		    (flags & HAT_SYNC_STOPON_REF) && PP_ISREF(save_pp)) {
37567c478bd9Sstevel@tonic-gate 			x86_hm_exit(pp);
3757ae115bc7Smrj 			goto done;
37587c478bd9Sstevel@tonic-gate 		}
37597c478bd9Sstevel@tonic-gate 	}
37607c478bd9Sstevel@tonic-gate 	x86_hm_exit(pp);
37617c478bd9Sstevel@tonic-gate 	while (pszc < pp->p_szc) {
37627c478bd9Sstevel@tonic-gate 		page_t *tpp;
37637c478bd9Sstevel@tonic-gate 		pszc++;
37647c478bd9Sstevel@tonic-gate 		tpp = PP_GROUPLEADER(pp, pszc);
37657c478bd9Sstevel@tonic-gate 		if (pp != tpp) {
37667c478bd9Sstevel@tonic-gate 			pp = tpp;
37677c478bd9Sstevel@tonic-gate 			goto next_size;
37687c478bd9Sstevel@tonic-gate 		}
37697c478bd9Sstevel@tonic-gate 	}
3770ae115bc7Smrj done:
3771843e1988Sjohnlev 	XPV_ALLOW_MIGRATE();
37727c478bd9Sstevel@tonic-gate 	return (save_pp->p_nrm & nrmbits);
37737c478bd9Sstevel@tonic-gate }
37747c478bd9Sstevel@tonic-gate 
37757c478bd9Sstevel@tonic-gate /*
37767c478bd9Sstevel@tonic-gate  * returns approx number of mappings to this pp.  A return of 0 implies
37777c478bd9Sstevel@tonic-gate  * there are no mappings to the page.
37787c478bd9Sstevel@tonic-gate  */
37797c478bd9Sstevel@tonic-gate ulong_t
37807c478bd9Sstevel@tonic-gate hat_page_getshare(page_t *pp)
37817c478bd9Sstevel@tonic-gate {
37827c478bd9Sstevel@tonic-gate 	uint_t cnt;
37837c478bd9Sstevel@tonic-gate 	cnt = hment_mapcnt(pp);
3784a5652762Spraks #if defined(__amd64)
3785a5652762Spraks 	if (vpm_enable && pp->p_vpmref) {
3786a5652762Spraks 		cnt += 1;
3787a5652762Spraks 	}
3788a5652762Spraks #endif
37897c478bd9Sstevel@tonic-gate 	return (cnt);
37907c478bd9Sstevel@tonic-gate }
37917c478bd9Sstevel@tonic-gate 
379205d3dc4bSpaulsan /*
379305d3dc4bSpaulsan  * Return 1 the number of mappings exceeds sh_thresh. Return 0
379405d3dc4bSpaulsan  * otherwise.
379505d3dc4bSpaulsan  */
379605d3dc4bSpaulsan int
379705d3dc4bSpaulsan hat_page_checkshare(page_t *pp, ulong_t sh_thresh)
379805d3dc4bSpaulsan {
379905d3dc4bSpaulsan 	return (hat_page_getshare(pp) > sh_thresh);
380005d3dc4bSpaulsan }
380105d3dc4bSpaulsan 
38027c478bd9Sstevel@tonic-gate /*
38037c478bd9Sstevel@tonic-gate  * hat_softlock isn't supported anymore
38047c478bd9Sstevel@tonic-gate  */
38057c478bd9Sstevel@tonic-gate /*ARGSUSED*/
38067c478bd9Sstevel@tonic-gate faultcode_t
38077c478bd9Sstevel@tonic-gate hat_softlock(
38087c478bd9Sstevel@tonic-gate 	hat_t *hat,
38097c478bd9Sstevel@tonic-gate 	caddr_t addr,
38107c478bd9Sstevel@tonic-gate 	size_t *len,
38117c478bd9Sstevel@tonic-gate 	struct page **page_array,
38127c478bd9Sstevel@tonic-gate 	uint_t flags)
38137c478bd9Sstevel@tonic-gate {
38147c478bd9Sstevel@tonic-gate 	return (FC_NOSUPPORT);
38157c478bd9Sstevel@tonic-gate }
38167c478bd9Sstevel@tonic-gate 
38177c478bd9Sstevel@tonic-gate 
38187c478bd9Sstevel@tonic-gate 
38197c478bd9Sstevel@tonic-gate /*
38207c478bd9Sstevel@tonic-gate  * Routine to expose supported HAT features to platform independent code.
38217c478bd9Sstevel@tonic-gate  */
38227c478bd9Sstevel@tonic-gate /*ARGSUSED*/
38237c478bd9Sstevel@tonic-gate int
38247c478bd9Sstevel@tonic-gate hat_supported(enum hat_features feature, void *arg)
38257c478bd9Sstevel@tonic-gate {
38267c478bd9Sstevel@tonic-gate 	switch (feature) {
38277c478bd9Sstevel@tonic-gate 
38287c478bd9Sstevel@tonic-gate 	case HAT_SHARED_PT:	/* this is really ISM */
38297c478bd9Sstevel@tonic-gate 		return (1);
38307c478bd9Sstevel@tonic-gate 
38317c478bd9Sstevel@tonic-gate 	case HAT_DYNAMIC_ISM_UNMAP:
38327c478bd9Sstevel@tonic-gate 		return (0);
38337c478bd9Sstevel@tonic-gate 
38347c478bd9Sstevel@tonic-gate 	case HAT_VMODSORT:
38357c478bd9Sstevel@tonic-gate 		return (1);
38367c478bd9Sstevel@tonic-gate 
383705d3dc4bSpaulsan 	case HAT_SHARED_REGIONS:
383805d3dc4bSpaulsan 		return (0);
383905d3dc4bSpaulsan 
38407c478bd9Sstevel@tonic-gate 	default:
38417c478bd9Sstevel@tonic-gate 		panic("hat_supported() - unknown feature");
38427c478bd9Sstevel@tonic-gate 	}
38437c478bd9Sstevel@tonic-gate 	return (0);
38447c478bd9Sstevel@tonic-gate }
38457c478bd9Sstevel@tonic-gate 
38467c478bd9Sstevel@tonic-gate /*
38477c478bd9Sstevel@tonic-gate  * Called when a thread is exiting and has been switched to the kernel AS
38487c478bd9Sstevel@tonic-gate  */
38497c478bd9Sstevel@tonic-gate void
38507c478bd9Sstevel@tonic-gate hat_thread_exit(kthread_t *thd)
38517c478bd9Sstevel@tonic-gate {
38527c478bd9Sstevel@tonic-gate 	ASSERT(thd->t_procp->p_as == &kas);
3853843e1988Sjohnlev 	XPV_DISALLOW_MIGRATE();
38547c478bd9Sstevel@tonic-gate 	hat_switch(thd->t_procp->p_as->a_hat);
3855843e1988Sjohnlev 	XPV_ALLOW_MIGRATE();
38567c478bd9Sstevel@tonic-gate }
38577c478bd9Sstevel@tonic-gate 
38587c478bd9Sstevel@tonic-gate /*
38597c478bd9Sstevel@tonic-gate  * Setup the given brand new hat structure as the new HAT on this cpu's mmu.
38607c478bd9Sstevel@tonic-gate  */
38617c478bd9Sstevel@tonic-gate /*ARGSUSED*/
38627c478bd9Sstevel@tonic-gate void
38637c478bd9Sstevel@tonic-gate hat_setup(hat_t *hat, int flags)
38647c478bd9Sstevel@tonic-gate {
3865843e1988Sjohnlev 	XPV_DISALLOW_MIGRATE();
38667c478bd9Sstevel@tonic-gate 	kpreempt_disable();
38677c478bd9Sstevel@tonic-gate 
38687c478bd9Sstevel@tonic-gate 	hat_switch(hat);
38697c478bd9Sstevel@tonic-gate 
38707c478bd9Sstevel@tonic-gate 	kpreempt_enable();
3871843e1988Sjohnlev 	XPV_ALLOW_MIGRATE();
38727c478bd9Sstevel@tonic-gate }
38737c478bd9Sstevel@tonic-gate 
38747c478bd9Sstevel@tonic-gate /*
38757c478bd9Sstevel@tonic-gate  * Prepare for a CPU private mapping for the given address.
38767c478bd9Sstevel@tonic-gate  *
38777c478bd9Sstevel@tonic-gate  * The address can only be used from a single CPU and can be remapped
38787c478bd9Sstevel@tonic-gate  * using hat_mempte_remap().  Return the address of the PTE.
38797c478bd9Sstevel@tonic-gate  *
38807c478bd9Sstevel@tonic-gate  * We do the htable_create() if necessary and increment the valid count so
38817c478bd9Sstevel@tonic-gate  * the htable can't disappear.  We also hat_devload() the page table into
38827c478bd9Sstevel@tonic-gate  * kernel so that the PTE is quickly accessed.
38837c478bd9Sstevel@tonic-gate  */
3884ae115bc7Smrj hat_mempte_t
3885ae115bc7Smrj hat_mempte_setup(caddr_t addr)
38867c478bd9Sstevel@tonic-gate {
38877c478bd9Sstevel@tonic-gate 	uintptr_t	va = (uintptr_t)addr;
38887c478bd9Sstevel@tonic-gate 	htable_t	*ht;
38897c478bd9Sstevel@tonic-gate 	uint_t		entry;
38907c478bd9Sstevel@tonic-gate 	x86pte_t	oldpte;
3891ae115bc7Smrj 	hat_mempte_t	p;
38927c478bd9Sstevel@tonic-gate 
38937c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(va));
38947c478bd9Sstevel@tonic-gate 	ASSERT(!IN_VA_HOLE(va));
3895aac11643Sjosephb 	++curthread->t_hatdepth;
3896551bc2a6Smrj 	XPV_DISALLOW_MIGRATE();
38977c478bd9Sstevel@tonic-gate 	ht = htable_getpte(kas.a_hat, va, &entry, &oldpte, 0);
38987c478bd9Sstevel@tonic-gate 	if (ht == NULL) {
38997c478bd9Sstevel@tonic-gate 		ht = htable_create(kas.a_hat, va, 0, NULL);
39007c478bd9Sstevel@tonic-gate 		entry = htable_va2entry(va, ht);
39017c478bd9Sstevel@tonic-gate 		ASSERT(ht->ht_level == 0);
39027c478bd9Sstevel@tonic-gate 		oldpte = x86pte_get(ht, entry);
39037c478bd9Sstevel@tonic-gate 	}
39047c478bd9Sstevel@tonic-gate 	if (PTE_ISVALID(oldpte))
39057c478bd9Sstevel@tonic-gate 		panic("hat_mempte_setup(): address already mapped"
3906903a11ebSrh 		    "ht=%p, entry=%d, pte=" FMT_PTE, (void *)ht, entry, oldpte);
39077c478bd9Sstevel@tonic-gate 
39087c478bd9Sstevel@tonic-gate 	/*
39097c478bd9Sstevel@tonic-gate 	 * increment ht_valid_cnt so that the pagetable can't disappear
39107c478bd9Sstevel@tonic-gate 	 */
39117c478bd9Sstevel@tonic-gate 	HTABLE_INC(ht->ht_valid_cnt);
39127c478bd9Sstevel@tonic-gate 
39137c478bd9Sstevel@tonic-gate 	/*
3914ae115bc7Smrj 	 * return the PTE physical address to the caller.
39157c478bd9Sstevel@tonic-gate 	 */
39167c478bd9Sstevel@tonic-gate 	htable_release(ht);
3917551bc2a6Smrj 	XPV_ALLOW_MIGRATE();
3918ae115bc7Smrj 	p = PT_INDEX_PHYSADDR(pfn_to_pa(ht->ht_pfn), entry);
3919aac11643Sjosephb 	--curthread->t_hatdepth;
3920ae115bc7Smrj 	return (p);
39217c478bd9Sstevel@tonic-gate }
39227c478bd9Sstevel@tonic-gate 
39237c478bd9Sstevel@tonic-gate /*
39247c478bd9Sstevel@tonic-gate  * Release a CPU private mapping for the given address.
39257c478bd9Sstevel@tonic-gate  * We decrement the htable valid count so it might be destroyed.
39267c478bd9Sstevel@tonic-gate  */
3927ae115bc7Smrj /*ARGSUSED1*/
39287c478bd9Sstevel@tonic-gate void
3929ae115bc7Smrj hat_mempte_release(caddr_t addr, hat_mempte_t pte_pa)
39307c478bd9Sstevel@tonic-gate {
39317c478bd9Sstevel@tonic-gate 	htable_t	*ht;
39327c478bd9Sstevel@tonic-gate 
3933551bc2a6Smrj 	XPV_DISALLOW_MIGRATE();
39347c478bd9Sstevel@tonic-gate 	/*
3935ae115bc7Smrj 	 * invalidate any left over mapping and decrement the htable valid count
39367c478bd9Sstevel@tonic-gate 	 */
3937843e1988Sjohnlev #ifdef __xpv
3938843e1988Sjohnlev 	if (HYPERVISOR_update_va_mapping((uintptr_t)addr, 0,
3939843e1988Sjohnlev 	    UVMF_INVLPG | UVMF_LOCAL))
3940843e1988Sjohnlev 		panic("HYPERVISOR_update_va_mapping() failed");
3941843e1988Sjohnlev #else
3942ae115bc7Smrj 	{
3943ae115bc7Smrj 		x86pte_t *pteptr;
3944ae115bc7Smrj 
3945ae115bc7Smrj 		pteptr = x86pte_mapin(mmu_btop(pte_pa),
3946ae115bc7Smrj 		    (pte_pa & MMU_PAGEOFFSET) >> mmu.pte_size_shift, NULL);
3947ae115bc7Smrj 		if (mmu.pae_hat)
3948ae115bc7Smrj 			*pteptr = 0;
3949ae115bc7Smrj 		else
3950ae115bc7Smrj 			*(x86pte32_t *)pteptr = 0;
3951ae115bc7Smrj 		mmu_tlbflush_entry(addr);
3952ae115bc7Smrj 		x86pte_mapout();
3953ae115bc7Smrj 	}
3954843e1988Sjohnlev #endif
3955ae115bc7Smrj 
39567c478bd9Sstevel@tonic-gate 	ht = htable_getpte(kas.a_hat, ALIGN2PAGE(addr), NULL, NULL, 0);
39577c478bd9Sstevel@tonic-gate 	if (ht == NULL)
39587c478bd9Sstevel@tonic-gate 		panic("hat_mempte_release(): invalid address");
39597c478bd9Sstevel@tonic-gate 	ASSERT(ht->ht_level == 0);
39607c478bd9Sstevel@tonic-gate 	HTABLE_DEC(ht->ht_valid_cnt);
39617c478bd9Sstevel@tonic-gate 	htable_release(ht);
3962551bc2a6Smrj 	XPV_ALLOW_MIGRATE();
39637c478bd9Sstevel@tonic-gate }
39647c478bd9Sstevel@tonic-gate 
39657c478bd9Sstevel@tonic-gate /*
39667c478bd9Sstevel@tonic-gate  * Apply a temporary CPU private mapping to a page. We flush the TLB only
39677c478bd9Sstevel@tonic-gate  * on this CPU, so this ought to have been called with preemption disabled.
39687c478bd9Sstevel@tonic-gate  */
39697c478bd9Sstevel@tonic-gate void
39707c478bd9Sstevel@tonic-gate hat_mempte_remap(
3971ae115bc7Smrj 	pfn_t		pfn,
3972ae115bc7Smrj 	caddr_t		addr,
3973ae115bc7Smrj 	hat_mempte_t	pte_pa,
3974ae115bc7Smrj 	uint_t		attr,
3975ae115bc7Smrj 	uint_t		flags)
39767c478bd9Sstevel@tonic-gate {
39777c478bd9Sstevel@tonic-gate 	uintptr_t	va = (uintptr_t)addr;
39787c478bd9Sstevel@tonic-gate 	x86pte_t	pte;
39797c478bd9Sstevel@tonic-gate 
39807c478bd9Sstevel@tonic-gate 	/*
39817c478bd9Sstevel@tonic-gate 	 * Remap the given PTE to the new page's PFN. Invalidate only
39827c478bd9Sstevel@tonic-gate 	 * on this CPU.
39837c478bd9Sstevel@tonic-gate 	 */
39847c478bd9Sstevel@tonic-gate #ifdef DEBUG
39857c478bd9Sstevel@tonic-gate 	htable_t	*ht;
39867c478bd9Sstevel@tonic-gate 	uint_t		entry;
39877c478bd9Sstevel@tonic-gate 
39887c478bd9Sstevel@tonic-gate 	ASSERT(IS_PAGEALIGNED(va));
39897c478bd9Sstevel@tonic-gate 	ASSERT(!IN_VA_HOLE(va));
39907c478bd9Sstevel@tonic-gate 	ht = htable_getpte(kas.a_hat, va, &entry, NULL, 0);
39917c478bd9Sstevel@tonic-gate 	ASSERT(ht != NULL);
39927c478bd9Sstevel@tonic-gate 	ASSERT(ht->ht_level == 0);
39937c478bd9Sstevel@tonic-gate 	ASSERT(ht->ht_valid_cnt > 0);
3994ae115bc7Smrj 	ASSERT(ht->ht_pfn == mmu_btop(pte_pa));
39957c478bd9Sstevel@tonic-gate 	htable_release(ht);
39967c478bd9Sstevel@tonic-gate #endif
3997843e1988Sjohnlev 	XPV_DISALLOW_MIGRATE();
39987c478bd9Sstevel@tonic-gate 	pte = hati_mkpte(pfn, attr, 0, flags);
3999843e1988Sjohnlev #ifdef __xpv
4000843e1988Sjohnlev 	if (HYPERVISOR_update_va_mapping(va, pte, UVMF_INVLPG | UVMF_LOCAL))
4001843e1988Sjohnlev 		panic("HYPERVISOR_update_va_mapping() failed");
4002843e1988Sjohnlev #else
4003ae115bc7Smrj 	{
4004ae115bc7Smrj 		x86pte_t *pteptr;
4005ae115bc7Smrj 
4006ae115bc7Smrj 		pteptr = x86pte_mapin(mmu_btop(pte_pa),
4007ae115bc7Smrj 		    (pte_pa & MMU_PAGEOFFSET) >> mmu.pte_size_shift, NULL);
4008ae115bc7Smrj 		if (mmu.pae_hat)
4009ae115bc7Smrj 			*(x86pte_t *)pteptr = pte;
4010ae115bc7Smrj 		else
4011ae115bc7Smrj 			*(x86pte32_t *)pteptr = (x86pte32_t)pte;
4012ae115bc7Smrj 		mmu_tlbflush_entry(addr);
4013ae115bc7Smrj 		x86pte_mapout();
4014ae115bc7Smrj 	}
4015843e1988Sjohnlev #endif
4016843e1988Sjohnlev 	XPV_ALLOW_MIGRATE();
40177c478bd9Sstevel@tonic-gate }
40187c478bd9Sstevel@tonic-gate 
40197c478bd9Sstevel@tonic-gate 
40207c478bd9Sstevel@tonic-gate 
40217c478bd9Sstevel@tonic-gate /*
40227c478bd9Sstevel@tonic-gate  * Hat locking functions
40237c478bd9Sstevel@tonic-gate  * XXX - these two functions are currently being used by hatstats
40247c478bd9Sstevel@tonic-gate  * 	they can be removed by using a per-as mutex for hatstats.
40257c478bd9Sstevel@tonic-gate  */
40267c478bd9Sstevel@tonic-gate void
40277c478bd9Sstevel@tonic-gate hat_enter(hat_t *hat)
40287c478bd9Sstevel@tonic-gate {
40297c478bd9Sstevel@tonic-gate 	mutex_enter(&hat->hat_mutex);
40307c478bd9Sstevel@tonic-gate }
40317c478bd9Sstevel@tonic-gate 
40327c478bd9Sstevel@tonic-gate void
40337c478bd9Sstevel@tonic-gate hat_exit(hat_t *hat)
40347c478bd9Sstevel@tonic-gate {
40357c478bd9Sstevel@tonic-gate 	mutex_exit(&hat->hat_mutex);
40367c478bd9Sstevel@tonic-gate }
40377c478bd9Sstevel@tonic-gate 
40387c478bd9Sstevel@tonic-gate /*
4039ae115bc7Smrj  * HAT part of cpu initialization.
40407c478bd9Sstevel@tonic-gate  */
40417c478bd9Sstevel@tonic-gate void
4042ae115bc7Smrj hat_cpu_online(struct cpu *cpup)
40437c478bd9Sstevel@tonic-gate {
4044ae115bc7Smrj 	if (cpup != CPU) {
4045ae115bc7Smrj 		x86pte_cpu_init(cpup);
4046ae115bc7Smrj 		hat_vlp_setup(cpup);
40477c478bd9Sstevel@tonic-gate 	}
4048ae115bc7Smrj 	CPUSET_ATOMIC_ADD(khat_cpuset, cpup->cpu_id);
40497c478bd9Sstevel@tonic-gate }
40507c478bd9Sstevel@tonic-gate 
40517c478bd9Sstevel@tonic-gate /*
4052ae115bc7Smrj  * HAT part of cpu deletion.
4053ae115bc7Smrj  * (currently, we only call this after the cpu is safely passivated.)
40547c478bd9Sstevel@tonic-gate  */
40557c478bd9Sstevel@tonic-gate void
4056ae115bc7Smrj hat_cpu_offline(struct cpu *cpup)
40577c478bd9Sstevel@tonic-gate {
4058ae115bc7Smrj 	ASSERT(cpup != CPU);
4059ae115bc7Smrj 
4060ae115bc7Smrj 	CPUSET_ATOMIC_DEL(khat_cpuset, cpup->cpu_id);
4061ae115bc7Smrj 	hat_vlp_teardown(cpup);
4062*a3114836SGerry Liu 	x86pte_cpu_fini(cpup);
40637c478bd9Sstevel@tonic-gate }
40647c478bd9Sstevel@tonic-gate 
40657c478bd9Sstevel@tonic-gate /*
40667c478bd9Sstevel@tonic-gate  * Function called after all CPUs are brought online.
40677c478bd9Sstevel@tonic-gate  * Used to remove low address boot mappings.
40687c478bd9Sstevel@tonic-gate  */
40697c478bd9Sstevel@tonic-gate void
40707c478bd9Sstevel@tonic-gate clear_boot_mappings(uintptr_t low, uintptr_t high)
40717c478bd9Sstevel@tonic-gate {
40727c478bd9Sstevel@tonic-gate 	uintptr_t vaddr = low;
40737c478bd9Sstevel@tonic-gate 	htable_t *ht = NULL;
40747c478bd9Sstevel@tonic-gate 	level_t level;
40757c478bd9Sstevel@tonic-gate 	uint_t entry;
40767c478bd9Sstevel@tonic-gate 	x86pte_t pte;
40777c478bd9Sstevel@tonic-gate 
40787c478bd9Sstevel@tonic-gate 	/*
40797c478bd9Sstevel@tonic-gate 	 * On 1st CPU we can unload the prom mappings, basically we blow away
4080ae115bc7Smrj 	 * all virtual mappings under _userlimit.
40817c478bd9Sstevel@tonic-gate 	 */
40827c478bd9Sstevel@tonic-gate 	while (vaddr < high) {
40837c478bd9Sstevel@tonic-gate 		pte = htable_walk(kas.a_hat, &ht, &vaddr, high);
40847c478bd9Sstevel@tonic-gate 		if (ht == NULL)
40857c478bd9Sstevel@tonic-gate 			break;
40867c478bd9Sstevel@tonic-gate 
40877c478bd9Sstevel@tonic-gate 		level = ht->ht_level;
40887c478bd9Sstevel@tonic-gate 		entry = htable_va2entry(vaddr, ht);
40897c478bd9Sstevel@tonic-gate 		ASSERT(level <= mmu.max_page_level);
40907c478bd9Sstevel@tonic-gate 		ASSERT(PTE_ISPAGE(pte, level));
40917c478bd9Sstevel@tonic-gate 
40927c478bd9Sstevel@tonic-gate 		/*
40937c478bd9Sstevel@tonic-gate 		 * Unload the mapping from the page tables.
40947c478bd9Sstevel@tonic-gate 		 */
4095ae115bc7Smrj 		(void) x86pte_inval(ht, entry, 0, NULL);
40967c478bd9Sstevel@tonic-gate 		ASSERT(ht->ht_valid_cnt > 0);
40977c478bd9Sstevel@tonic-gate 		HTABLE_DEC(ht->ht_valid_cnt);
40987c478bd9Sstevel@tonic-gate 		PGCNT_DEC(ht->ht_hat, ht->ht_level);
40997c478bd9Sstevel@tonic-gate 
41007c478bd9Sstevel@tonic-gate 		vaddr += LEVEL_SIZE(ht->ht_level);
41017c478bd9Sstevel@tonic-gate 	}
41027c478bd9Sstevel@tonic-gate 	if (ht)
41037c478bd9Sstevel@tonic-gate 		htable_release(ht);
41047c478bd9Sstevel@tonic-gate }
41057c478bd9Sstevel@tonic-gate 
41067c478bd9Sstevel@tonic-gate /*
41077c478bd9Sstevel@tonic-gate  * Atomically update a new translation for a single page.  If the
41087c478bd9Sstevel@tonic-gate  * currently installed PTE doesn't match the value we expect to find,
41097c478bd9Sstevel@tonic-gate  * it's not updated and we return the PTE we found.
41107c478bd9Sstevel@tonic-gate  *
41117c478bd9Sstevel@tonic-gate  * If activating nosync or NOWRITE and the page was modified we need to sync
41127c478bd9Sstevel@tonic-gate  * with the page_t. Also sync with page_t if clearing ref/mod bits.
41137c478bd9Sstevel@tonic-gate  */
41147c478bd9Sstevel@tonic-gate static x86pte_t
41157c478bd9Sstevel@tonic-gate hati_update_pte(htable_t *ht, uint_t entry, x86pte_t expected, x86pte_t new)
41167c478bd9Sstevel@tonic-gate {
41177c478bd9Sstevel@tonic-gate 	page_t		*pp;
41187c478bd9Sstevel@tonic-gate 	uint_t		rm = 0;
41197c478bd9Sstevel@tonic-gate 	x86pte_t	replaced;
41207c478bd9Sstevel@tonic-gate 
4121ae115bc7Smrj 	if (PTE_GET(expected, PT_SOFTWARE) < PT_NOSYNC &&
41227c478bd9Sstevel@tonic-gate 	    PTE_GET(expected, PT_MOD | PT_REF) &&
41237c478bd9Sstevel@tonic-gate 	    (PTE_GET(new, PT_NOSYNC) || !PTE_GET(new, PT_WRITABLE) ||
4124250b7ff9Sjosephb 	    !PTE_GET(new, PT_MOD | PT_REF))) {
41257c478bd9Sstevel@tonic-gate 
4126ae115bc7Smrj 		ASSERT(!pfn_is_foreign(PTE2PFN(expected, ht->ht_level)));
41277c478bd9Sstevel@tonic-gate 		pp = page_numtopp_nolock(PTE2PFN(expected, ht->ht_level));
41287c478bd9Sstevel@tonic-gate 		ASSERT(pp != NULL);
41297c478bd9Sstevel@tonic-gate 		if (PTE_GET(expected, PT_MOD))
41307c478bd9Sstevel@tonic-gate 			rm |= P_MOD;
41317c478bd9Sstevel@tonic-gate 		if (PTE_GET(expected, PT_REF))
41327c478bd9Sstevel@tonic-gate 			rm |= P_REF;
41337c478bd9Sstevel@tonic-gate 		PTE_CLR(new, PT_MOD | PT_REF);
41347c478bd9Sstevel@tonic-gate 	}
41357c478bd9Sstevel@tonic-gate 
41367c478bd9Sstevel@tonic-gate 	replaced = x86pte_update(ht, entry, expected, new);
41377c478bd9Sstevel@tonic-gate 	if (replaced != expected)
41387c478bd9Sstevel@tonic-gate 		return (replaced);
41397c478bd9Sstevel@tonic-gate 
41407c478bd9Sstevel@tonic-gate 	if (rm) {
41417c478bd9Sstevel@tonic-gate 		/*
41427c478bd9Sstevel@tonic-gate 		 * sync to all constituent pages of a large page
41437c478bd9Sstevel@tonic-gate 		 */
41447c478bd9Sstevel@tonic-gate 		pgcnt_t pgcnt = page_get_pagecnt(ht->ht_level);
41457c478bd9Sstevel@tonic-gate 		ASSERT(IS_P2ALIGNED(pp->p_pagenum, pgcnt));
41467c478bd9Sstevel@tonic-gate 		while (pgcnt-- > 0) {
41477c478bd9Sstevel@tonic-gate 			/*
41487c478bd9Sstevel@tonic-gate 			 * hat_page_demote() can't decrease
41497c478bd9Sstevel@tonic-gate 			 * pszc below this mapping size
41507c478bd9Sstevel@tonic-gate 			 * since large mapping existed after we
41517c478bd9Sstevel@tonic-gate 			 * took mlist lock.
41527c478bd9Sstevel@tonic-gate 			 */
41537c478bd9Sstevel@tonic-gate 			ASSERT(pp->p_szc >= ht->ht_level);
41547c478bd9Sstevel@tonic-gate 			hat_page_setattr(pp, rm);
41557c478bd9Sstevel@tonic-gate 			++pp;
41567c478bd9Sstevel@tonic-gate 		}
41577c478bd9Sstevel@tonic-gate 	}
41587c478bd9Sstevel@tonic-gate 
41597c478bd9Sstevel@tonic-gate 	return (0);
41607c478bd9Sstevel@tonic-gate }
41617c478bd9Sstevel@tonic-gate 
416205d3dc4bSpaulsan /* ARGSUSED */
416305d3dc4bSpaulsan void
41647dacfc44Spaulsan hat_join_srd(struct hat *hat, vnode_t *evp)
416505d3dc4bSpaulsan {
416605d3dc4bSpaulsan }
416705d3dc4bSpaulsan 
416805d3dc4bSpaulsan /* ARGSUSED */
416905d3dc4bSpaulsan hat_region_cookie_t
41707dacfc44Spaulsan hat_join_region(struct hat *hat,
417105d3dc4bSpaulsan     caddr_t r_saddr,
417205d3dc4bSpaulsan     size_t r_size,
417305d3dc4bSpaulsan     void *r_obj,
417405d3dc4bSpaulsan     u_offset_t r_objoff,
417505d3dc4bSpaulsan     uchar_t r_perm,
417605d3dc4bSpaulsan     uchar_t r_pgszc,
417705d3dc4bSpaulsan     hat_rgn_cb_func_t r_cb_function,
417805d3dc4bSpaulsan     uint_t flags)
417905d3dc4bSpaulsan {
418005d3dc4bSpaulsan 	panic("No shared region support on x86");
418105d3dc4bSpaulsan 	return (HAT_INVALID_REGION_COOKIE);
418205d3dc4bSpaulsan }
418305d3dc4bSpaulsan 
418405d3dc4bSpaulsan /* ARGSUSED */
418505d3dc4bSpaulsan void
41867dacfc44Spaulsan hat_leave_region(struct hat *hat, hat_region_cookie_t rcookie, uint_t flags)
418705d3dc4bSpaulsan {
418805d3dc4bSpaulsan 	panic("No shared region support on x86");
418905d3dc4bSpaulsan }
419005d3dc4bSpaulsan 
419105d3dc4bSpaulsan /* ARGSUSED */
419205d3dc4bSpaulsan void
41937dacfc44Spaulsan hat_dup_region(struct hat *hat, hat_region_cookie_t rcookie)
419405d3dc4bSpaulsan {
419505d3dc4bSpaulsan 	panic("No shared region support on x86");
419605d3dc4bSpaulsan }
419705d3dc4bSpaulsan 
419805d3dc4bSpaulsan 
41997c478bd9Sstevel@tonic-gate /*
42007c478bd9Sstevel@tonic-gate  * Kernel Physical Mapping (kpm) facility
42017c478bd9Sstevel@tonic-gate  *
42027c478bd9Sstevel@tonic-gate  * Most of the routines needed to support segkpm are almost no-ops on the
42037c478bd9Sstevel@tonic-gate  * x86 platform.  We map in the entire segment when it is created and leave
42047c478bd9Sstevel@tonic-gate  * it mapped in, so there is no additional work required to set up and tear
42057c478bd9Sstevel@tonic-gate  * down individual mappings.  All of these routines were created to support
42067c478bd9Sstevel@tonic-gate  * SPARC platforms that have to avoid aliasing in their virtually indexed
42077c478bd9Sstevel@tonic-gate  * caches.
42087c478bd9Sstevel@tonic-gate  *
42097c478bd9Sstevel@tonic-gate  * Most of the routines have sanity checks in them (e.g. verifying that the
42107c478bd9Sstevel@tonic-gate  * passed-in page is locked).  We don't actually care about most of these
42117c478bd9Sstevel@tonic-gate  * checks on x86, but we leave them in place to identify problems in the
42127c478bd9Sstevel@tonic-gate  * upper levels.
42137c478bd9Sstevel@tonic-gate  */
42147c478bd9Sstevel@tonic-gate 
42157c478bd9Sstevel@tonic-gate /*
42167c478bd9Sstevel@tonic-gate  * Map in a locked page and return the vaddr.
42177c478bd9Sstevel@tonic-gate  */
42187c478bd9Sstevel@tonic-gate /*ARGSUSED*/
42197c478bd9Sstevel@tonic-gate caddr_t
42207c478bd9Sstevel@tonic-gate hat_kpm_mapin(struct page *pp, struct kpme *kpme)
42217c478bd9Sstevel@tonic-gate {
42227c478bd9Sstevel@tonic-gate 	caddr_t		vaddr;
42237c478bd9Sstevel@tonic-gate 
42247c478bd9Sstevel@tonic-gate #ifdef DEBUG
42257c478bd9Sstevel@tonic-gate 	if (kpm_enable == 0) {
42267c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "hat_kpm_mapin: kpm_enable not set\n");
42277c478bd9Sstevel@tonic-gate 		return ((caddr_t)NULL);
42287c478bd9Sstevel@tonic-gate 	}
42297c478bd9Sstevel@tonic-gate 
42307c478bd9Sstevel@tonic-gate 	if (pp == NULL || PAGE_LOCKED(pp) == 0) {
42317c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "hat_kpm_mapin: pp zero or not locked\n");
42327c478bd9Sstevel@tonic-gate 		return ((caddr_t)NULL);
42337c478bd9Sstevel@tonic-gate 	}
42347c478bd9Sstevel@tonic-gate #endif
42357c478bd9Sstevel@tonic-gate 
42367c478bd9Sstevel@tonic-gate 	vaddr = hat_kpm_page2va(pp, 1);
42377c478bd9Sstevel@tonic-gate 
42387c478bd9Sstevel@tonic-gate 	return (vaddr);
42397c478bd9Sstevel@tonic-gate }
42407c478bd9Sstevel@tonic-gate 
42417c478bd9Sstevel@tonic-gate /*
42427c478bd9Sstevel@tonic-gate  * Mapout a locked page.
42437c478bd9Sstevel@tonic-gate  */
42447c478bd9Sstevel@tonic-gate /*ARGSUSED*/
42457c478bd9Sstevel@tonic-gate void
42467c478bd9Sstevel@tonic-gate hat_kpm_mapout(struct page *pp, struct kpme *kpme, caddr_t vaddr)
42477c478bd9Sstevel@tonic-gate {
42487c478bd9Sstevel@tonic-gate #ifdef DEBUG
42497c478bd9Sstevel@tonic-gate 	if (kpm_enable == 0) {
42507c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "hat_kpm_mapout: kpm_enable not set\n");
42517c478bd9Sstevel@tonic-gate 		return;
42527c478bd9Sstevel@tonic-gate 	}
42537c478bd9Sstevel@tonic-gate 
42547c478bd9Sstevel@tonic-gate 	if (IS_KPM_ADDR(vaddr) == 0) {
42557c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "hat_kpm_mapout: no kpm address\n");
42567c478bd9Sstevel@tonic-gate 		return;
42577c478bd9Sstevel@tonic-gate 	}
42587c478bd9Sstevel@tonic-gate 
42597c478bd9Sstevel@tonic-gate 	if (pp == NULL || PAGE_LOCKED(pp) == 0) {
42607c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "hat_kpm_mapout: page zero or not locked\n");
42617c478bd9Sstevel@tonic-gate 		return;
42627c478bd9Sstevel@tonic-gate 	}
42637c478bd9Sstevel@tonic-gate #endif
42647c478bd9Sstevel@tonic-gate }
42657c478bd9Sstevel@tonic-gate 
4266d20abfaaSPavel Tatashin /*
4267d20abfaaSPavel Tatashin  * hat_kpm_mapin_pfn is used to obtain a kpm mapping for physical
4268d20abfaaSPavel Tatashin  * memory addresses that are not described by a page_t.  It can
4269d20abfaaSPavel Tatashin  * also be used for normal pages that are not locked, but beware
4270d20abfaaSPavel Tatashin  * this is dangerous - no locking is performed, so the identity of
4271d20abfaaSPavel Tatashin  * the page could change.  hat_kpm_mapin_pfn is not supported when
4272d20abfaaSPavel Tatashin  * vac_colors > 1, because the chosen va depends on the page identity,
4273d20abfaaSPavel Tatashin  * which could change.
4274d20abfaaSPavel Tatashin  * The caller must only pass pfn's for valid physical addresses; violation
4275d20abfaaSPavel Tatashin  * of this rule will cause panic.
4276d20abfaaSPavel Tatashin  */
4277d20abfaaSPavel Tatashin caddr_t
4278d20abfaaSPavel Tatashin hat_kpm_mapin_pfn(pfn_t pfn)
4279d20abfaaSPavel Tatashin {
4280d20abfaaSPavel Tatashin 	caddr_t paddr, vaddr;
4281d20abfaaSPavel Tatashin 
4282d20abfaaSPavel Tatashin 	if (kpm_enable == 0)
4283d20abfaaSPavel Tatashin 		return ((caddr_t)NULL);
4284d20abfaaSPavel Tatashin 
4285d20abfaaSPavel Tatashin 	paddr = (caddr_t)ptob(pfn);
4286d20abfaaSPavel Tatashin 	vaddr = (uintptr_t)kpm_vbase + paddr;
4287d20abfaaSPavel Tatashin 
4288d20abfaaSPavel Tatashin 	return ((caddr_t)vaddr);
4289d20abfaaSPavel Tatashin }
4290d20abfaaSPavel Tatashin 
4291d20abfaaSPavel Tatashin /*ARGSUSED*/
4292d20abfaaSPavel Tatashin void
4293d20abfaaSPavel Tatashin hat_kpm_mapout_pfn(pfn_t pfn)
4294d20abfaaSPavel Tatashin {
4295d20abfaaSPavel Tatashin 	/* empty */
4296d20abfaaSPavel Tatashin }
4297d20abfaaSPavel Tatashin 
42987c478bd9Sstevel@tonic-gate /*
42997c478bd9Sstevel@tonic-gate  * Return the kpm virtual address for a specific pfn
43007c478bd9Sstevel@tonic-gate  */
43017c478bd9Sstevel@tonic-gate caddr_t
43027c478bd9Sstevel@tonic-gate hat_kpm_pfn2va(pfn_t pfn)
43037c478bd9Sstevel@tonic-gate {
4304ae115bc7Smrj 	uintptr_t vaddr = (uintptr_t)kpm_vbase + mmu_ptob(pfn);
43057c478bd9Sstevel@tonic-gate 
4306d2b85481Srscott 	ASSERT(!pfn_is_foreign(pfn));
43077c478bd9Sstevel@tonic-gate 	return ((caddr_t)vaddr);
43087c478bd9Sstevel@tonic-gate }
43097c478bd9Sstevel@tonic-gate 
43107c478bd9Sstevel@tonic-gate /*
43117c478bd9Sstevel@tonic-gate  * Return the kpm virtual address for the page at pp.
43127c478bd9Sstevel@tonic-gate  */
43137c478bd9Sstevel@tonic-gate /*ARGSUSED*/
43147c478bd9Sstevel@tonic-gate caddr_t
43157c478bd9Sstevel@tonic-gate hat_kpm_page2va(struct page *pp, int checkswap)
43167c478bd9Sstevel@tonic-gate {
43177c478bd9Sstevel@tonic-gate 	return (hat_kpm_pfn2va(pp->p_pagenum));
43187c478bd9Sstevel@tonic-gate }
43197c478bd9Sstevel@tonic-gate 
43207c478bd9Sstevel@tonic-gate /*
43217c478bd9Sstevel@tonic-gate  * Return the page frame number for the kpm virtual address vaddr.
43227c478bd9Sstevel@tonic-gate  */
43237c478bd9Sstevel@tonic-gate pfn_t
43247c478bd9Sstevel@tonic-gate hat_kpm_va2pfn(caddr_t vaddr)
43257c478bd9Sstevel@tonic-gate {
43267c478bd9Sstevel@tonic-gate 	pfn_t		pfn;
43277c478bd9Sstevel@tonic-gate 
43287c478bd9Sstevel@tonic-gate 	ASSERT(IS_KPM_ADDR(vaddr));
43297c478bd9Sstevel@tonic-gate 
43307c478bd9Sstevel@tonic-gate 	pfn = (pfn_t)btop(vaddr - kpm_vbase);
43317c478bd9Sstevel@tonic-gate 
43327c478bd9Sstevel@tonic-gate 	return (pfn);
43337c478bd9Sstevel@tonic-gate }
43347c478bd9Sstevel@tonic-gate 
43357c478bd9Sstevel@tonic-gate 
43367c478bd9Sstevel@tonic-gate /*
43377c478bd9Sstevel@tonic-gate  * Return the page for the kpm virtual address vaddr.
43387c478bd9Sstevel@tonic-gate  */
43397c478bd9Sstevel@tonic-gate page_t *
43407c478bd9Sstevel@tonic-gate hat_kpm_vaddr2page(caddr_t vaddr)
43417c478bd9Sstevel@tonic-gate {
43427c478bd9Sstevel@tonic-gate 	pfn_t		pfn;
43437c478bd9Sstevel@tonic-gate 
43447c478bd9Sstevel@tonic-gate 	ASSERT(IS_KPM_ADDR(vaddr));
43457c478bd9Sstevel@tonic-gate 
43467c478bd9Sstevel@tonic-gate 	pfn = hat_kpm_va2pfn(vaddr);
43477c478bd9Sstevel@tonic-gate 
43487c478bd9Sstevel@tonic-gate 	return (page_numtopp_nolock(pfn));
43497c478bd9Sstevel@tonic-gate }
43507c478bd9Sstevel@tonic-gate 
43517c478bd9Sstevel@tonic-gate /*
43527c478bd9Sstevel@tonic-gate  * hat_kpm_fault is called from segkpm_fault when we take a page fault on a
43537c478bd9Sstevel@tonic-gate  * KPM page.  This should never happen on x86
43547c478bd9Sstevel@tonic-gate  */
43557c478bd9Sstevel@tonic-gate int
43567c478bd9Sstevel@tonic-gate hat_kpm_fault(hat_t *hat, caddr_t vaddr)
43577c478bd9Sstevel@tonic-gate {
4358903a11ebSrh 	panic("pagefault in seg_kpm.  hat: 0x%p  vaddr: 0x%p",
4359903a11ebSrh 	    (void *)hat, (void *)vaddr);
43607c478bd9Sstevel@tonic-gate 
43617c478bd9Sstevel@tonic-gate 	return (0);
43627c478bd9Sstevel@tonic-gate }
43637c478bd9Sstevel@tonic-gate 
43647c478bd9Sstevel@tonic-gate /*ARGSUSED*/
43657c478bd9Sstevel@tonic-gate void
43667c478bd9Sstevel@tonic-gate hat_kpm_mseghash_clear(int nentries)
43677c478bd9Sstevel@tonic-gate {}
43687c478bd9Sstevel@tonic-gate 
43697c478bd9Sstevel@tonic-gate /*ARGSUSED*/
43707c478bd9Sstevel@tonic-gate void
43717c478bd9Sstevel@tonic-gate hat_kpm_mseghash_update(pgcnt_t inx, struct memseg *msp)
43727c478bd9Sstevel@tonic-gate {}
4373843e1988Sjohnlev 
4374*a3114836SGerry Liu #ifndef	__xpv
4375*a3114836SGerry Liu void
4376*a3114836SGerry Liu hat_kpm_addmem_mseg_update(struct memseg *msp, pgcnt_t nkpmpgs,
4377*a3114836SGerry Liu 	offset_t kpm_pages_off)
4378*a3114836SGerry Liu {
4379*a3114836SGerry Liu 	_NOTE(ARGUNUSED(nkpmpgs, kpm_pages_off));
4380*a3114836SGerry Liu 	pfn_t base, end;
4381*a3114836SGerry Liu 
4382*a3114836SGerry Liu 	/*
4383*a3114836SGerry Liu 	 * kphysm_add_memory_dynamic() does not set nkpmpgs
4384*a3114836SGerry Liu 	 * when page_t memory is externally allocated.  That
4385*a3114836SGerry Liu 	 * code must properly calculate nkpmpgs in all cases
4386*a3114836SGerry Liu 	 * if nkpmpgs needs to be used at some point.
4387*a3114836SGerry Liu 	 */
4388*a3114836SGerry Liu 
4389*a3114836SGerry Liu 	/*
4390*a3114836SGerry Liu 	 * The meta (page_t) pages for dynamically added memory are allocated
4391*a3114836SGerry Liu 	 * either from the incoming memory itself or from existing memory.
4392*a3114836SGerry Liu 	 * In the former case the base of the incoming pages will be different
4393*a3114836SGerry Liu 	 * than the base of the dynamic segment so call memseg_get_start() to
4394*a3114836SGerry Liu 	 * get the actual base of the incoming memory for each case.
4395*a3114836SGerry Liu 	 */
4396*a3114836SGerry Liu 
4397*a3114836SGerry Liu 	base = memseg_get_start(msp);
4398*a3114836SGerry Liu 	end = msp->pages_end;
4399*a3114836SGerry Liu 
4400*a3114836SGerry Liu 	hat_devload(kas.a_hat, kpm_vbase + mmu_ptob(base),
4401*a3114836SGerry Liu 	    mmu_ptob(end - base), base, PROT_READ | PROT_WRITE,
4402*a3114836SGerry Liu 	    HAT_LOAD | HAT_LOAD_LOCK | HAT_LOAD_NOCONSIST);
4403*a3114836SGerry Liu }
4404*a3114836SGerry Liu 
4405*a3114836SGerry Liu void
4406*a3114836SGerry Liu hat_kpm_addmem_mseg_insert(struct memseg *msp)
4407*a3114836SGerry Liu {
4408*a3114836SGerry Liu 	_NOTE(ARGUNUSED(msp));
4409*a3114836SGerry Liu }
4410*a3114836SGerry Liu 
4411*a3114836SGerry Liu void
4412*a3114836SGerry Liu hat_kpm_addmem_memsegs_update(struct memseg *msp)
4413*a3114836SGerry Liu {
4414*a3114836SGerry Liu 	_NOTE(ARGUNUSED(msp));
4415*a3114836SGerry Liu }
4416*a3114836SGerry Liu 
4417*a3114836SGerry Liu /*
4418*a3114836SGerry Liu  * Return end of metadata for an already setup memseg.
4419*a3114836SGerry Liu  * X86 platforms don't need per-page meta data to support kpm.
4420*a3114836SGerry Liu  */
4421*a3114836SGerry Liu caddr_t
4422*a3114836SGerry Liu hat_kpm_mseg_reuse(struct memseg *msp)
4423*a3114836SGerry Liu {
4424*a3114836SGerry Liu 	return ((caddr_t)msp->epages);
4425*a3114836SGerry Liu }
4426*a3114836SGerry Liu 
4427*a3114836SGerry Liu void
4428*a3114836SGerry Liu hat_kpm_delmem_mseg_update(struct memseg *msp, struct memseg **mspp)
4429*a3114836SGerry Liu {
4430*a3114836SGerry Liu 	_NOTE(ARGUNUSED(msp, mspp));
4431*a3114836SGerry Liu 	ASSERT(0);
4432*a3114836SGerry Liu }
4433*a3114836SGerry Liu 
4434*a3114836SGerry Liu void
4435*a3114836SGerry Liu hat_kpm_split_mseg_update(struct memseg *msp, struct memseg **mspp,
4436*a3114836SGerry Liu 	struct memseg *lo, struct memseg *mid, struct memseg *hi)
4437*a3114836SGerry Liu {
4438*a3114836SGerry Liu 	_NOTE(ARGUNUSED(msp, mspp, lo, mid, hi));
4439*a3114836SGerry Liu 	ASSERT(0);
4440*a3114836SGerry Liu }
4441*a3114836SGerry Liu 
4442*a3114836SGerry Liu /*
4443*a3114836SGerry Liu  * Walk the memsegs chain, applying func to each memseg span.
4444*a3114836SGerry Liu  */
4445*a3114836SGerry Liu void
4446*a3114836SGerry Liu hat_kpm_walk(void (*func)(void *, void *, size_t), void *arg)
4447*a3114836SGerry Liu {
4448*a3114836SGerry Liu 	pfn_t	pbase, pend;
4449*a3114836SGerry Liu 	void	*base;
4450*a3114836SGerry Liu 	size_t	size;
4451*a3114836SGerry Liu 	struct memseg *msp;
4452*a3114836SGerry Liu 
4453*a3114836SGerry Liu 	for (msp = memsegs; msp; msp = msp->next) {
4454*a3114836SGerry Liu 		pbase = msp->pages_base;
4455*a3114836SGerry Liu 		pend = msp->pages_end;
4456*a3114836SGerry Liu 		base = ptob(pbase) + kpm_vbase;
4457*a3114836SGerry Liu 		size = ptob(pend - pbase);
4458*a3114836SGerry Liu 		func(arg, base, size);
4459*a3114836SGerry Liu 	}
4460*a3114836SGerry Liu }
4461*a3114836SGerry Liu 
4462*a3114836SGerry Liu #else	/* __xpv */
4463*a3114836SGerry Liu 
4464843e1988Sjohnlev /*
4465843e1988Sjohnlev  * There are specific Hypervisor calls to establish and remove mappings
4466843e1988Sjohnlev  * to grant table references and the privcmd driver. We have to ensure
4467843e1988Sjohnlev  * that a page table actually exists.
4468843e1988Sjohnlev  */
4469843e1988Sjohnlev void
44707eea693dSMark Johnson hat_prepare_mapping(hat_t *hat, caddr_t addr, uint64_t *pte_ma)
4471843e1988Sjohnlev {
44727eea693dSMark Johnson 	maddr_t base_ma;
44737eea693dSMark Johnson 	htable_t *ht;
44747eea693dSMark Johnson 	uint_t entry;
44757eea693dSMark Johnson 
4476843e1988Sjohnlev 	ASSERT(IS_P2ALIGNED((uintptr_t)addr, MMU_PAGESIZE));
4477551bc2a6Smrj 	XPV_DISALLOW_MIGRATE();
44787eea693dSMark Johnson 	ht = htable_create(hat, (uintptr_t)addr, 0, NULL);
44797eea693dSMark Johnson 
44807eea693dSMark Johnson 	/*
44817eea693dSMark Johnson 	 * if an address for pte_ma is passed in, return the MA of the pte
44827eea693dSMark Johnson 	 * for this specific address.  This address is only valid as long
44837eea693dSMark Johnson 	 * as the htable stays locked.
44847eea693dSMark Johnson 	 */
44857eea693dSMark Johnson 	if (pte_ma != NULL) {
44867eea693dSMark Johnson 		entry = htable_va2entry((uintptr_t)addr, ht);
44877eea693dSMark Johnson 		base_ma = pa_to_ma(ptob(ht->ht_pfn));
44887eea693dSMark Johnson 		*pte_ma = base_ma + (entry << mmu.pte_size_shift);
44897eea693dSMark Johnson 	}
4490551bc2a6Smrj 	XPV_ALLOW_MIGRATE();
4491843e1988Sjohnlev }
4492843e1988Sjohnlev 
4493843e1988Sjohnlev void
4494843e1988Sjohnlev hat_release_mapping(hat_t *hat, caddr_t addr)
4495843e1988Sjohnlev {
4496843e1988Sjohnlev 	htable_t *ht;
4497843e1988Sjohnlev 
4498843e1988Sjohnlev 	ASSERT(IS_P2ALIGNED((uintptr_t)addr, MMU_PAGESIZE));
4499551bc2a6Smrj 	XPV_DISALLOW_MIGRATE();
4500843e1988Sjohnlev 	ht = htable_lookup(hat, (uintptr_t)addr, 0);
4501843e1988Sjohnlev 	ASSERT(ht != NULL);
4502843e1988Sjohnlev 	ASSERT(ht->ht_busy >= 2);
4503843e1988Sjohnlev 	htable_release(ht);
4504843e1988Sjohnlev 	htable_release(ht);
4505551bc2a6Smrj 	XPV_ALLOW_MIGRATE();
4506*a3114836SGerry Liu }
4507*a3114836SGerry Liu #endif	/* __xpv */
4508