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