xref: /titanic_50/usr/src/uts/sfmmu/vm/hat_sfmmu.h (revision 275c9da86e89f8abf71135cf63d9fc23671b2e60)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * VM - Hardware Address Translation management.
28  *
29  * This file describes the contents of the sun-reference-mmu(sfmmu)-
30  * specific hat data structures and the sfmmu-specific hat procedures.
31  * The machine-independent interface is described in <vm/hat.h>.
32  */
33 
34 #ifndef	_VM_HAT_SFMMU_H
35 #define	_VM_HAT_SFMMU_H
36 
37 #pragma ident	"%Z%%M%	%I%	%E% SMI"
38 
39 #ifdef	__cplusplus
40 extern "C" {
41 #endif
42 
43 #ifndef _ASM
44 
45 #include <sys/types.h>
46 
47 #endif /* _ASM */
48 
49 #ifdef	_KERNEL
50 
51 #include <sys/pte.h>
52 #include <vm/mach_sfmmu.h>
53 #include <sys/mmu.h>
54 
55 /*
56  * Don't alter these without considering changes to ism_map_t.
57  */
58 #define	DEFAULT_ISM_PAGESIZE		MMU_PAGESIZE4M
59 #define	DEFAULT_ISM_PAGESZC		TTE4M
60 #define	ISM_PG_SIZE(ism_vbshift)	(1 << ism_vbshift)
61 #define	ISM_SZ_MASK(ism_vbshift)	(ISM_PG_SIZE(ism_vbshift) - 1)
62 #define	ISM_MAP_SLOTS	8	/* Change this carefully. */
63 
64 #ifndef _ASM
65 
66 #include <sys/t_lock.h>
67 #include <vm/hat.h>
68 #include <vm/seg.h>
69 #include <sys/machparam.h>
70 #include <sys/systm.h>
71 #include <sys/x_call.h>
72 #include <vm/page.h>
73 #include <sys/ksynch.h>
74 
75 typedef struct hat sfmmu_t;
76 typedef struct sf_scd sf_scd_t;
77 
78 /*
79  * SFMMU attributes for hat_memload/hat_devload
80  */
81 #define	SFMMU_UNCACHEPTTE	0x01000000	/* unencache in physical $ */
82 #define	SFMMU_UNCACHEVTTE	0x02000000	/* unencache in virtual $ */
83 #define	SFMMU_SIDEFFECT		0x04000000	/* set side effect bit */
84 #define	SFMMU_LOAD_ALLATTR	(HAT_PROT_MASK | HAT_ORDER_MASK |	\
85 		HAT_ENDIAN_MASK | HAT_NOFAULT | HAT_NOSYNC |		\
86 		SFMMU_UNCACHEPTTE | SFMMU_UNCACHEVTTE | SFMMU_SIDEFFECT)
87 
88 
89 /*
90  * sfmmu flags for hat_memload/hat_devload
91  */
92 #define	SFMMU_NO_TSBLOAD	0x08000000	/* do not preload tsb */
93 #define	SFMMU_LOAD_ALLFLAG	(HAT_LOAD | HAT_LOAD_LOCK |		\
94 		HAT_LOAD_ADV | HAT_LOAD_CONTIG | HAT_LOAD_NOCONSIST |	\
95 		HAT_LOAD_SHARE | HAT_LOAD_REMAP | SFMMU_NO_TSBLOAD |	\
96 		HAT_RELOAD_SHARE | HAT_NO_KALLOC | HAT_LOAD_TEXT)
97 
98 /*
99  * sfmmu internal flag to hat_pageunload that spares locked mappings
100  */
101 #define	SFMMU_KERNEL_RELOC	0x8000
102 
103 /*
104  * mode for sfmmu_chgattr
105  */
106 #define	SFMMU_SETATTR	0x0
107 #define	SFMMU_CLRATTR	0x1
108 #define	SFMMU_CHGATTR	0x2
109 
110 /*
111  * sfmmu specific flags for page_t
112  */
113 #define	P_PNC	0x8		/* non-caching is permanent bit */
114 #define	P_TNC	0x10		/* non-caching is temporary bit */
115 #define	P_KPMS	0x20		/* kpm mapped small (vac alias prevention) */
116 #define	P_KPMC	0x40		/* kpm conflict page (vac alias prevention) */
117 
118 #define	PP_GENERIC_ATTR(pp)	((pp)->p_nrm & (P_MOD | P_REF | P_RO))
119 #define	PP_ISMOD(pp)		((pp)->p_nrm & P_MOD)
120 #define	PP_ISREF(pp)		((pp)->p_nrm & P_REF)
121 #define	PP_ISRO(pp)		((pp)->p_nrm & P_RO)
122 #define	PP_ISNC(pp)		((pp)->p_nrm & (P_PNC|P_TNC))
123 #define	PP_ISPNC(pp)		((pp)->p_nrm & P_PNC)
124 #ifdef VAC
125 #define	PP_ISTNC(pp)		((pp)->p_nrm & P_TNC)
126 #endif
127 #define	PP_ISKPMS(pp)		((pp)->p_nrm & P_KPMS)
128 #define	PP_ISKPMC(pp)		((pp)->p_nrm & P_KPMC)
129 
130 #define	PP_SETMOD(pp)		((pp)->p_nrm |= P_MOD)
131 #define	PP_SETREF(pp)		((pp)->p_nrm |= P_REF)
132 #define	PP_SETREFMOD(pp)	((pp)->p_nrm |= (P_REF|P_MOD))
133 #define	PP_SETRO(pp)		((pp)->p_nrm |= P_RO)
134 #define	PP_SETREFRO(pp)		((pp)->p_nrm |= (P_REF|P_RO))
135 #define	PP_SETPNC(pp)		((pp)->p_nrm |= P_PNC)
136 #ifdef VAC
137 #define	PP_SETTNC(pp)		((pp)->p_nrm |= P_TNC)
138 #endif
139 #define	PP_SETKPMS(pp)		((pp)->p_nrm |= P_KPMS)
140 #define	PP_SETKPMC(pp)		((pp)->p_nrm |= P_KPMC)
141 
142 #define	PP_CLRMOD(pp)		((pp)->p_nrm &= ~P_MOD)
143 #define	PP_CLRREF(pp)		((pp)->p_nrm &= ~P_REF)
144 #define	PP_CLRREFMOD(pp)	((pp)->p_nrm &= ~(P_REF|P_MOD))
145 #define	PP_CLRRO(pp)		((pp)->p_nrm &= ~P_RO)
146 #define	PP_CLRPNC(pp)		((pp)->p_nrm &= ~P_PNC)
147 #ifdef VAC
148 #define	PP_CLRTNC(pp)		((pp)->p_nrm &= ~P_TNC)
149 #endif
150 #define	PP_CLRKPMS(pp)		((pp)->p_nrm &= ~P_KPMS)
151 #define	PP_CLRKPMC(pp)		((pp)->p_nrm &= ~P_KPMC)
152 
153 /*
154  * All shared memory segments attached with the SHM_SHARE_MMU flag (ISM)
155  * will be constrained to a 4M, 32M or 256M alignment. Also since every newly-
156  * created ISM segment is created out of a new address space at base va
157  * of 0 we don't need to store it.
158  */
159 #define	ISM_ALIGN(shift)	(1 << shift)	/* base va aligned to <n>M  */
160 #define	ISM_ALIGNED(shift, va)	(((uintptr_t)va & (ISM_ALIGN(shift) - 1)) == 0)
161 #define	ISM_SHIFT(shift, x)	((uintptr_t)x >> (shift))
162 
163 /*
164  * Pad locks out to cache sub-block boundaries to prevent
165  * false sharing, so several processes don't contend for
166  * the same line if they aren't using the same lock.  Since
167  * this is a typedef we also have a bit of freedom in
168  * changing lock implementations later if we decide it
169  * is necessary.
170  */
171 typedef struct hat_lock {
172 	kmutex_t hl_mutex;
173 	uchar_t hl_pad[64 - sizeof (kmutex_t)];
174 } hatlock_t;
175 
176 #define	HATLOCK_MUTEXP(hatlockp)	(&((hatlockp)->hl_mutex))
177 
178 /*
179  * All segments mapped with ISM are guaranteed to be 4M, 32M or 256M aligned.
180  * Also size is guaranteed to be in 4M, 32M or 256M chunks.
181  * ism_seg consists of the following members:
182  * [XX..22] base address of ism segment. XX is 63 or 31 depending whether
183  *	caddr_t is 64 bits or 32 bits.
184  * [21..0] size of segment.
185  *
186  * NOTE: Don't alter this structure without changing defines above and
187  * the tsb_miss and protection handlers.
188  */
189 typedef struct ism_map {
190 	uintptr_t	imap_seg;  	/* base va + sz of ISM segment */
191 	uchar_t		imap_vb_shift;	/* mmu_pageshift for ism page size */
192 	uchar_t		imap_rid;	/* region id for ism */
193 	ushort_t	imap_hatflags;	/* primary ism page size */
194 	uint_t		imap_sz_mask;	/* mmu_pagemask for ism page size */
195 	sfmmu_t		*imap_ismhat; 	/* hat id of dummy ISM as */
196 	struct ism_ment	*imap_ment;	/* pointer to mapping list entry */
197 } ism_map_t;
198 
199 #define	ism_start(map)	((caddr_t)((map).imap_seg & \
200 				~ISM_SZ_MASK((map).imap_vb_shift)))
201 #define	ism_size(map)	((map).imap_seg & ISM_SZ_MASK((map).imap_vb_shift))
202 #define	ism_end(map)	((caddr_t)(ism_start(map) + (ism_size(map) * \
203 				ISM_PG_SIZE((map).imap_vb_shift))))
204 /*
205  * ISM mapping entry. Used to link all hat's sharing a ism_hat.
206  * Same function as the p_mapping list for a page.
207  */
208 typedef struct ism_ment {
209 	sfmmu_t		*iment_hat;	/* back pointer to hat_share() hat */
210 	caddr_t		iment_base_va;	/* hat's va base for this ism seg */
211 	struct ism_ment	*iment_next;	/* next ism map entry */
212 	struct ism_ment	*iment_prev;	/* prev ism map entry */
213 } ism_ment_t;
214 
215 /*
216  * ISM segment block. One will be hung off the sfmmu structure if a
217  * a process uses ISM.  More will be linked using ismblk_next if more
218  * than ISM_MAP_SLOTS segments are attached to this proc.
219  *
220  * All modifications to fields in this structure will be protected
221  * by the hat mutex.  In order to avoid grabbing this lock in low level
222  * routines (tsb miss/protection handlers and vatopfn) while not
223  * introducing any race conditions with hat_unshare, we will set
224  * CTX_ISM_BUSY bit in the ctx struct. Any mmu traps that occur
225  * for this ctx while this bit is set will be handled in sfmmu_tsb_excption
226  * where it will synchronize behind the hat mutex.
227  */
228 typedef struct ism_blk {
229 	ism_map_t		iblk_maps[ISM_MAP_SLOTS];
230 	struct ism_blk		*iblk_next;
231 	uint64_t		iblk_nextpa;
232 } ism_blk_t;
233 
234 /*
235  * TSB access information.  All fields are protected by the process's
236  * hat lock.
237  */
238 
239 struct tsb_info {
240 	caddr_t		tsb_va;		/* tsb base virtual address */
241 	uint64_t	tsb_pa;		/* tsb base physical address */
242 	struct tsb_info	*tsb_next;	/* next tsb used by this process */
243 	uint16_t	tsb_szc;	/* tsb size code */
244 	uint16_t	tsb_flags;	/* flags for this tsb; see below */
245 	uint_t		tsb_ttesz_mask;	/* page size masks; see below */
246 
247 	tte_t		tsb_tte;	/* tte to lock into DTLB */
248 	sfmmu_t		*tsb_sfmmu;	/* sfmmu */
249 	kmem_cache_t	*tsb_cache;	/* cache from which mem allocated */
250 	vmem_t		*tsb_vmp;	/* vmem arena from which mem alloc'd */
251 };
252 
253 /*
254  * Values for "tsb_ttesz_mask" bitmask.
255  */
256 #define	TSB8K	(1 << TTE8K)
257 #define	TSB64K  (1 << TTE64K)
258 #define	TSB512K (1 << TTE512K)
259 #define	TSB4M   (1 << TTE4M)
260 #define	TSB32M  (1 << TTE32M)
261 #define	TSB256M (1 << TTE256M)
262 
263 /*
264  * Values for "tsb_flags" field.
265  */
266 #define	TSB_RELOC_FLAG		0x1
267 #define	TSB_FLUSH_NEEDED	0x2
268 #define	TSB_SWAPPED	0x4
269 #define	TSB_SHAREDCTX		0x8
270 
271 #endif	/* !_ASM */
272 
273 /*
274  * Data structures for shared hmeblk support.
275  */
276 
277 /*
278  * Do not increase the maximum number of ism/hme regions without checking first
279  * the impact on ism_map_t, TSB miss area, hblk tag and region id type in
280  * sf_region structure.
281  * Initially, shared hmes will only be used for the main text segment
282  * therefore this value will be set to 64, it will be increased when shared
283  * libraries are included.
284  */
285 
286 #define	SFMMU_MAX_HME_REGIONS		(64)
287 #define	SFMMU_HMERGNMAP_WORDS		BT_BITOUL(SFMMU_MAX_HME_REGIONS)
288 
289 #define	SFMMU_PRIVATE	0
290 #define	SFMMU_SHARED	1
291 
292 #ifndef _ASM
293 
294 #define	SFMMU_MAX_ISM_REGIONS		(64)
295 #define	SFMMU_ISMRGNMAP_WORDS		BT_BITOUL(SFMMU_MAX_ISM_REGIONS)
296 
297 #define	SFMMU_RGNMAP_WORDS	(SFMMU_HMERGNMAP_WORDS + SFMMU_ISMRGNMAP_WORDS)
298 
299 #define	SFMMU_MAX_REGION_BUCKETS	(128)
300 #define	SFMMU_MAX_SRD_BUCKETS		(2048)
301 
302 typedef struct sf_hmeregion_map {
303 	ulong_t	bitmap[SFMMU_HMERGNMAP_WORDS];
304 } sf_hmeregion_map_t;
305 
306 typedef struct sf_ismregion_map {
307 	ulong_t	bitmap[SFMMU_ISMRGNMAP_WORDS];
308 } sf_ismregion_map_t;
309 
310 typedef union sf_region_map_u {
311 	struct _h_rmap_s {
312 		sf_hmeregion_map_t hmeregion_map;
313 		sf_ismregion_map_t ismregion_map;
314 	} h_rmap_s;
315 	ulong_t	bitmap[SFMMU_RGNMAP_WORDS];
316 } sf_region_map_t;
317 
318 #define	SF_RGNMAP_ZERO(map) {				\
319 	int _i;						\
320 	for (_i = 0; _i < SFMMU_RGNMAP_WORDS; _i++) {	\
321 		(map).bitmap[_i] = 0;			\
322 	}						\
323 }
324 
325 /*
326  * Returns 1 if map1 and map2 are equal.
327  */
328 #define	SF_RGNMAP_EQUAL(map1, map2, rval)	{		\
329 	int _i;							\
330 	for (_i = 0; _i < SFMMU_RGNMAP_WORDS; _i++) {		\
331 		if ((map1)->bitmap[_i] != (map2)->bitmap[_i])	\
332 			break;					\
333 	}							\
334 	if (_i < SFMMU_RGNMAP_WORDS)				\
335 		rval = 0;					\
336 	else							\
337 		rval = 1;					\
338 }
339 
340 #define	SF_RGNMAP_ADD(map, r)		BT_SET((map).bitmap, r)
341 #define	SF_RGNMAP_DEL(map, r)		BT_CLEAR((map).bitmap, r)
342 #define	SF_RGNMAP_TEST(map, r)		BT_TEST((map).bitmap, r)
343 
344 /*
345  * Tests whether map2 is a subset of map1, returns 1 if
346  * this assertion is true.
347  */
348 #define	SF_RGNMAP_IS_SUBSET(map1, map2, rval)	{		\
349 	int _i;							\
350 	for (_i = 0; _i < SFMMU_RGNMAP_WORDS; _i++) {		\
351 		if (((map1)->bitmap[_i]	& (map2)->bitmap[_i])	\
352 		    != (map2)->bitmap[_i])  {	 		\
353 			break;					\
354 		}						\
355 	}							\
356 	if (_i < SFMMU_RGNMAP_WORDS)		 		\
357 		rval = 0;					\
358 	else							\
359 		rval = 1;					\
360 }
361 
362 #define	SF_SCD_INCR_REF(scdp) {						\
363 	atomic_add_32((volatile uint32_t *)&(scdp)->scd_refcnt, 1);	\
364 }
365 
366 #define	SF_SCD_DECR_REF(srdp, scdp) {				\
367 	sf_region_map_t _scd_rmap = (scdp)->scd_region_map;	\
368 	if (!atomic_add_32_nv(					\
369 	    (volatile uint32_t *)&(scdp)->scd_refcnt, -1)) {	\
370 		sfmmu_destroy_scd((srdp), (scdp), &_scd_rmap);	\
371 	}							\
372 }
373 
374 /*
375  * A sfmmup link in the link list of sfmmups that share the same region.
376  */
377 typedef struct sf_rgn_link {
378 	sfmmu_t	*next;
379 	sfmmu_t *prev;
380 } sf_rgn_link_t;
381 
382 /*
383  * rgn_flags values.
384  */
385 #define	SFMMU_REGION_HME	0x1
386 #define	SFMMU_REGION_ISM	0x2
387 #define	SFMMU_REGION_FREE	0x8
388 
389 #define	SFMMU_REGION_TYPE_MASK	(0x3)
390 
391 /*
392  * sf_region defines a text or (D)ISM segment which map
393  * the same underlying physical object.
394  */
395 typedef struct sf_region {
396 	caddr_t			rgn_saddr;   /* base addr of attached seg */
397 	size_t			rgn_size;    /* size of attached seg */
398 	void			*rgn_obj;    /* the underlying object id */
399 	u_offset_t		rgn_objoff;  /* offset in the object mapped */
400 	uchar_t			rgn_perm;    /* PROT_READ/WRITE/EXEC */
401 	uchar_t			rgn_pgszc;   /* page size of the region */
402 	uchar_t			rgn_flags;   /* region type, free flag */
403 	uchar_t			rgn_id;
404 	int			rgn_refcnt;  /* # of hats sharing the region */
405 	/* callback function for hat_unload_callback */
406 	hat_rgn_cb_func_t	rgn_cb_function;
407 	struct sf_region	*rgn_hash;   /* hash chain linking the rgns */
408 	kmutex_t		rgn_mutex;   /* protect region sfmmu list */
409 	/* A link list of processes attached to this region */
410 	sfmmu_t			*rgn_sfmmu_head;
411 	ulong_t			rgn_ttecnt[MMU_PAGE_SIZES];
412 	uint16_t		rgn_hmeflags; /* rgn tte size flags */
413 } sf_region_t;
414 
415 #define	rgn_next	rgn_hash
416 
417 /* srd */
418 typedef struct sf_shared_region_domain {
419 	vnode_t			*srd_evp;	/* executable vnode */
420 	/* hme region table */
421 	sf_region_t		*srd_hmergnp[SFMMU_MAX_HME_REGIONS];
422 	/* ism region table */
423 	sf_region_t		*srd_ismrgnp[SFMMU_MAX_ISM_REGIONS];
424 	/* hash chain linking srds */
425 	struct sf_shared_region_domain *srd_hash;
426 	/* pointer to the next free hme region */
427 	sf_region_t		*srd_hmergnfree;
428 	/* pointer to the next free ism region */
429 	sf_region_t		*srd_ismrgnfree;
430 	/* id of next ism region created */
431 	uint16_t		srd_next_ismrid;
432 	/* id of next hme region created */
433 	uint16_t		srd_next_hmerid;
434 	uint16_t		srd_ismbusyrgns; /* # of ism rgns in use */
435 	uint16_t		srd_hmebusyrgns; /* # of hme rgns in use */
436 	int			srd_refcnt;	 /* # of procs in the srd */
437 	kmutex_t		srd_mutex;	 /* sync add/remove rgns */
438 	kmutex_t		srd_scd_mutex;
439 	sf_scd_t		*srd_scdp;	 /* list of scds in srd */
440 	/* hash of regions associated with the same executable */
441 	sf_region_t		*srd_rgnhash[SFMMU_MAX_REGION_BUCKETS];
442 } sf_srd_t;
443 
444 typedef struct sf_srd_bucket {
445 	kmutex_t	srdb_lock;
446 	sf_srd_t	*srdb_srdp;
447 } sf_srd_bucket_t;
448 
449 /*
450  * The value of SFMMU_L1_HMERLINKS and SFMMU_L2_HMERLINKS will be increased
451  * to 16 when the use of shared hmes for shared libraries is enabled.
452  */
453 
454 #define	SFMMU_L1_HMERLINKS		(8)
455 #define	SFMMU_L2_HMERLINKS		(8)
456 #define	SFMMU_L1_HMERLINKS_SHIFT	(3)
457 #define	SFMMU_L1_HMERLINKS_MASK		(SFMMU_L1_HMERLINKS - 1)
458 #define	SFMMU_L2_HMERLINKS_MASK		(SFMMU_L2_HMERLINKS - 1)
459 #define	SFMMU_L1_HMERLINKS_SIZE		\
460 	(SFMMU_L1_HMERLINKS * sizeof (sf_rgn_link_t *))
461 #define	SFMMU_L2_HMERLINKS_SIZE		\
462 	(SFMMU_L2_HMERLINKS * sizeof (sf_rgn_link_t))
463 
464 #if (SFMMU_L1_HMERLINKS * SFMMU_L2_HMERLINKS < SFMMU_MAX_HME_REGIONS)
465 #error Not Enough HMERLINKS
466 #endif
467 
468 /*
469  * This macro grabs hat lock and allocates level 2 hat chain
470  * associated with a shme rgn. In the majority of cases, the macro
471  * is called with alloc = 0, and lock = 0.
472  * A pointer to the level 2 sf_rgn_link_t structure is returned in the lnkp
473  * parameter.
474  */
475 #define	SFMMU_HMERID2RLINKP(sfmmup, rid, lnkp, alloc, lock)		\
476 {									\
477 	int _l1ix = ((rid) >> SFMMU_L1_HMERLINKS_SHIFT) &		\
478 	    SFMMU_L1_HMERLINKS_MASK;					\
479 	int _l2ix = ((rid) & SFMMU_L2_HMERLINKS_MASK);			\
480 	hatlock_t *_hatlockp;						\
481 	lnkp = (sfmmup)->sfmmu_hmeregion_links[_l1ix];			\
482 	if (lnkp != NULL) {						\
483 		lnkp = &lnkp[_l2ix];					\
484 	} else if (alloc && lock) {					\
485 		lnkp = kmem_zalloc(SFMMU_L2_HMERLINKS_SIZE, KM_SLEEP);	\
486 		_hatlockp = sfmmu_hat_enter(sfmmup);			\
487 		if ((sfmmup)->sfmmu_hmeregion_links[_l1ix] != NULL) {	\
488 			sfmmu_hat_exit(_hatlockp);			\
489 			kmem_free(lnkp, SFMMU_L2_HMERLINKS_SIZE);	\
490 			lnkp = (sfmmup)->sfmmu_hmeregion_links[_l1ix];	\
491 			ASSERT(lnkp != NULL);				\
492 		} else {						\
493 			(sfmmup)->sfmmu_hmeregion_links[_l1ix] = lnkp;	\
494 			sfmmu_hat_exit(_hatlockp);			\
495 		}							\
496 		lnkp = &lnkp[_l2ix];					\
497 	} else if (alloc) {						\
498 		lnkp = kmem_zalloc(SFMMU_L2_HMERLINKS_SIZE, KM_SLEEP);	\
499 		ASSERT((sfmmup)->sfmmu_hmeregion_links[_l1ix] == NULL);	\
500 		(sfmmup)->sfmmu_hmeregion_links[_l1ix] = lnkp;		\
501 		lnkp = &lnkp[_l2ix];					\
502 	}								\
503 }
504 
505 /*
506  * Per-MMU context domain kstats.
507  *
508  * TSB Miss Exceptions
509  *	Number of times a TSB miss exception is handled in an MMU. See
510  *	sfmmu_tsbmiss_exception() for more details.
511  * TSB Raise Exception
512  *	Number of times the CPUs within an MMU are cross-called
513  *	to invalidate either a specific process context (when the process
514  *	switches MMU contexts) or the context of any process that is
515  *	running on those CPUs (as part of the MMU context wrap-around).
516  * Wrap Around
517  *	The number of times a wrap-around of MMU context happens.
518  */
519 typedef enum mmu_ctx_stat_types {
520 	MMU_CTX_TSB_EXCEPTIONS,		/* TSB miss exceptions handled */
521 	MMU_CTX_TSB_RAISE_EXCEPTION,	/* ctx invalidation cross calls */
522 	MMU_CTX_WRAP_AROUND,		/* wraparounds */
523 	MMU_CTX_NUM_STATS
524 } mmu_ctx_stat_t;
525 
526 /*
527  * Per-MMU context domain structure. This is instantiated the first time a CPU
528  * belonging to the MMU context domain is configured into the system, at boot
529  * time or at DR time.
530  *
531  * mmu_gnum
532  *	The current generation number for the context IDs on this MMU context
533  *	domain. It is protected by mmu_lock.
534  * mmu_cnum
535  *	The current cnum to be allocated on this MMU context domain. It
536  *	is protected via CAS.
537  * mmu_nctxs
538  *	The max number of context IDs supported on every CPU in this
539  *	MMU context domain. It is 8K except for Rock where it is 64K.
540  *      This is needed here in case the system supports mixed type of
541  *      processors/MMUs. It also helps to make ctx switch code access
542  *      fewer cache lines i.e. no need to retrieve it from some global nctxs.
543  * mmu_lock
544  *	The mutex spin lock used to serialize context ID wrap around
545  * mmu_idx
546  *	The index for this MMU context domain structure in the global array
547  *	mmu_ctxdoms.
548  * mmu_ncpus
549  *	The actual number of CPUs that have been configured in this
550  *	MMU context domain. This also acts as a reference count for the
551  *	structure. When the last CPU in an MMU context domain is unconfigured,
552  *	the structure is freed. It is protected by mmu_lock.
553  * mmu_cpuset
554  *	The CPU set of configured CPUs for this MMU context domain. Used
555  *	to cross-call all the CPUs in the MMU context domain to invalidate
556  *	context IDs during a wraparound operation. It is protected by mmu_lock.
557  */
558 
559 typedef struct mmu_ctx {
560 	uint64_t	mmu_gnum;
561 	uint_t		mmu_cnum;
562 	uint_t		mmu_nctxs;
563 	kmutex_t	mmu_lock;
564 	uint_t		mmu_idx;
565 	uint_t		mmu_ncpus;
566 	cpuset_t	mmu_cpuset;
567 	kstat_t		*mmu_kstat;
568 	kstat_named_t	mmu_kstat_data[MMU_CTX_NUM_STATS];
569 } mmu_ctx_t;
570 
571 #define	mmu_tsb_exceptions	\
572 		mmu_kstat_data[MMU_CTX_TSB_EXCEPTIONS].value.ui64
573 #define	mmu_tsb_raise_exception	\
574 		mmu_kstat_data[MMU_CTX_TSB_RAISE_EXCEPTION].value.ui64
575 #define	mmu_wrap_around		\
576 		mmu_kstat_data[MMU_CTX_WRAP_AROUND].value.ui64
577 
578 extern uint_t		max_mmu_ctxdoms;
579 extern mmu_ctx_t	**mmu_ctxs_tbl;
580 
581 extern void	sfmmu_cpu_init(cpu_t *);
582 extern void	sfmmu_cpu_cleanup(cpu_t *);
583 
584 /*
585  * The following structure is used to get MMU context domain information for
586  * a CPU from the platform.
587  *
588  * mmu_idx
589  *	The MMU context domain index within the global array mmu_ctxs
590  * mmu_nctxs
591  *	The number of context IDs supported in the MMU context domain
592  *	(64K for Rock)
593  */
594 typedef struct mmu_ctx_info {
595 	uint_t		mmu_idx;
596 	uint_t		mmu_nctxs;
597 } mmu_ctx_info_t;
598 
599 #pragma weak plat_cpuid_to_mmu_ctx_info
600 
601 extern void	plat_cpuid_to_mmu_ctx_info(processorid_t, mmu_ctx_info_t *);
602 
603 /*
604  * Each address space has an array of sfmmu_ctx_t structures, one structure
605  * per MMU context domain.
606  *
607  * cnum
608  *	The context ID allocated for an address space on an MMU context domain
609  * gnum
610  *	The generation number for the context ID in the MMU context domain.
611  *
612  * This structure needs to be a power-of-two in size.
613  */
614 typedef struct sfmmu_ctx {
615 	uint64_t	gnum:48;
616 	uint64_t	cnum:16;
617 } sfmmu_ctx_t;
618 
619 
620 /*
621  * The platform dependent hat structure.
622  * tte counts should be protected by cas.
623  * cpuset is protected by cas.
624  *
625  * ttecnt accounting for mappings which do not use shared hme is carried out
626  * during pagefault handling. In the shared hme case, only the first process
627  * to access a mapping generates a pagefault, subsequent processes simply
628  * find the shared hme entry during trap handling and therefore there is no
629  * corresponding event to initiate ttecnt accounting. Currently, as shared
630  * hmes are only used for text segments, when joining a region we assume the
631  * worst case and add the the number of ttes required to map the entire region
632  * to the ttecnt corresponding to the region pagesize. However, if the region
633  * has a 4M pagesize, and memory is low, the allocation of 4M pages may fail
634  * then 8K pages will be allocated instead and the first TSB which stores 8K
635  * mappings will potentially be undersized. To compensate for the potential
636  * underaccounting in this case we always add 1/4 of the region size to the 8K
637  * ttecnt.
638  *
639  * Note that sfmmu_xhat_provider MUST be the first element.
640  */
641 
642 struct hat {
643 	void		*sfmmu_xhat_provider;	/* NULL for CPU hat */
644 	cpuset_t	sfmmu_cpusran;	/* cpu bit mask for efficient xcalls */
645 	struct	as	*sfmmu_as;	/* as this hat provides mapping for */
646 	/* per pgsz private ttecnt + shme rgns ttecnt for rgns not in SCD */
647 	ulong_t		sfmmu_ttecnt[MMU_PAGE_SIZES];
648 	/* shme rgns ttecnt for rgns in SCD */
649 	ulong_t		sfmmu_scdrttecnt[MMU_PAGE_SIZES];
650 	/* est. ism ttes that are NOT in a SCD */
651 	ulong_t		sfmmu_ismttecnt[MMU_PAGE_SIZES];
652 	/* ttecnt for isms that are in a SCD */
653 	ulong_t		sfmmu_scdismttecnt[MMU_PAGE_SIZES];
654 	/* inflate tsb0 to allow for large page alloc failure in region */
655 	ulong_t		sfmmu_tsb0_4minflcnt;
656 	union _h_un {
657 		ism_blk_t	*sfmmu_iblkp;  /* maps to ismhat(s) */
658 		ism_ment_t	*sfmmu_imentp; /* ism hat's mapping list */
659 	} h_un;
660 	uint_t		sfmmu_free:1;	/* hat to be freed - set on as_free */
661 	uint_t		sfmmu_ismhat:1;	/* hat is dummy ism hatid */
662 	uint_t		sfmmu_scdhat:1;	/* hat is dummy scd hatid */
663 	uchar_t		sfmmu_rmstat;	/* refmod stats refcnt */
664 	ushort_t	sfmmu_clrstart;	/* start color bin for page coloring */
665 	ushort_t	sfmmu_clrbin;	/* per as phys page coloring bin */
666 	ushort_t	sfmmu_flags;	/* flags */
667 	uchar_t		sfmmu_tteflags;	/* pgsz flags */
668 	uchar_t		sfmmu_rtteflags; /* pgsz flags for SRD hmes */
669 	struct tsb_info	*sfmmu_tsb;	/* list of per as tsbs */
670 	uint64_t	sfmmu_ismblkpa; /* pa of sfmmu_iblkp, or -1 */
671 	lock_t		sfmmu_ctx_lock;	/* sync ctx alloc and invalidation */
672 	kcondvar_t	sfmmu_tsb_cv;	/* signals TSB swapin or relocation */
673 	uchar_t		sfmmu_cext;	/* context page size encoding */
674 	uint8_t		sfmmu_pgsz[MMU_PAGE_SIZES];  /* ranking for MMU */
675 	sf_srd_t	*sfmmu_srdp;
676 	sf_scd_t	*sfmmu_scdp;	/* scd this address space belongs to */
677 	sf_region_map_t	sfmmu_region_map;
678 	sf_rgn_link_t	*sfmmu_hmeregion_links[SFMMU_L1_HMERLINKS];
679 	sf_rgn_link_t	sfmmu_scd_link;	/* link to scd or pending queue */
680 #ifdef sun4v
681 	struct hv_tsb_block sfmmu_hvblock;
682 #endif
683 	/*
684 	 * sfmmu_ctxs is a variable length array of max_mmu_ctxdoms # of
685 	 * elements. max_mmu_ctxdoms is determined at run-time.
686 	 * sfmmu_ctxs[1] is just the fist element of an array, it always
687 	 * has to be the last field to ensure that the memory allocated
688 	 * for sfmmu_ctxs is consecutive with the memory of the rest of
689 	 * the hat data structure.
690 	 */
691 	sfmmu_ctx_t	sfmmu_ctxs[1];
692 
693 };
694 
695 #define	sfmmu_iblk	h_un.sfmmu_iblkp
696 #define	sfmmu_iment	h_un.sfmmu_imentp
697 
698 #define	sfmmu_hmeregion_map	sfmmu_region_map.h_rmap_s.hmeregion_map
699 #define	sfmmu_ismregion_map	sfmmu_region_map.h_rmap_s.ismregion_map
700 
701 #define	SF_RGNMAP_ISNULL(sfmmup)	\
702 	(sfrgnmap_isnull(&(sfmmup)->sfmmu_region_map))
703 #define	SF_HMERGNMAP_ISNULL(sfmmup)	\
704 	(sfhmergnmap_isnull(&(sfmmup)->sfmmu_hmeregion_map))
705 
706 struct sf_scd {
707 	sfmmu_t		*scd_sfmmup;	/* shared context hat */
708 	/* per pgsz ttecnt for shme rgns in SCD */
709 	ulong_t		scd_rttecnt[MMU_PAGE_SIZES];
710 	uint_t		scd_refcnt;	/* address spaces attached to scd */
711 	sf_region_map_t scd_region_map; /* bit mask of attached segments */
712 	sf_scd_t	*scd_next;	/* link pointers for srd_scd list */
713 	sf_scd_t	*scd_prev;
714 	sfmmu_t 	*scd_sf_list;	/* list of doubly linked hat structs */
715 	kmutex_t 	scd_mutex;
716 	/*
717 	 * Link used to add an scd to the sfmmu_iment list.
718 	 */
719 	ism_ment_t	scd_ism_links[SFMMU_MAX_ISM_REGIONS];
720 };
721 
722 #define	scd_hmeregion_map	scd_region_map.h_rmap_s.hmeregion_map
723 #define	scd_ismregion_map	scd_region_map.h_rmap_s.ismregion_map
724 
725 extern int disable_shctx;
726 extern int shctx_on;
727 
728 /*
729  * bit mask for managing vac conflicts on large pages.
730  * bit 1 is for uncache flag.
731  * bits 2 through min(num of cache colors + 1,31) are
732  * for cache colors that have already been flushed.
733  */
734 #ifdef VAC
735 #define	CACHE_NUM_COLOR		(shm_alignment >> MMU_PAGESHIFT)
736 #else
737 #define	CACHE_NUM_COLOR		1
738 #endif
739 
740 #define	CACHE_VCOLOR_MASK(vcolor)	(2 << (vcolor & (CACHE_NUM_COLOR - 1)))
741 
742 #define	CacheColor_IsFlushed(flag, vcolor) \
743 					((flag) & CACHE_VCOLOR_MASK(vcolor))
744 
745 #define	CacheColor_SetFlushed(flag, vcolor) \
746 					((flag) |= CACHE_VCOLOR_MASK(vcolor))
747 /*
748  * Flags passed to sfmmu_page_cache to flush page from vac or not.
749  */
750 #define	CACHE_FLUSH	0
751 #define	CACHE_NO_FLUSH	1
752 
753 /*
754  * Flags passed to sfmmu_tlbcache_demap
755  */
756 #define	FLUSH_NECESSARY_CPUS	0
757 #define	FLUSH_ALL_CPUS		1
758 
759 #ifdef	DEBUG
760 /*
761  * For debugging purpose only. Maybe removed later.
762  */
763 struct ctx_trace {
764 	sfmmu_t		*sc_sfmmu_stolen;
765 	sfmmu_t		*sc_sfmmu_stealing;
766 	clock_t		sc_time;
767 	ushort_t	sc_type;
768 	ushort_t	sc_cnum;
769 };
770 #define	CTX_TRC_STEAL	0x1
771 #define	CTX_TRC_FREE	0x0
772 #define	TRSIZE	0x400
773 #define	NEXT_CTXTR(ptr)	(((ptr) >= ctx_trace_last) ? \
774 		ctx_trace_first : ((ptr) + 1))
775 #define	TRACE_CTXS(mutex, ptr, cnum, stolen_sfmmu, stealing_sfmmu, type) \
776 	mutex_enter(mutex);						\
777 	(ptr)->sc_sfmmu_stolen = (stolen_sfmmu);			\
778 	(ptr)->sc_sfmmu_stealing = (stealing_sfmmu);			\
779 	(ptr)->sc_cnum = (cnum);					\
780 	(ptr)->sc_type = (type);					\
781 	(ptr)->sc_time = lbolt;						\
782 	(ptr) = NEXT_CTXTR(ptr);					\
783 	num_ctx_stolen += (type);					\
784 	mutex_exit(mutex);
785 #else
786 
787 #define	TRACE_CTXS(mutex, ptr, cnum, stolen_sfmmu, stealing_sfmmu, type)
788 
789 #endif	/* DEBUG */
790 
791 #endif	/* !_ASM */
792 
793 /*
794  * Macros for sfmmup->sfmmu_flags access.  The macros that change the flags
795  * ASSERT() that we're holding the HAT lock before changing the flags;
796  * however callers that read the flags may do so without acquiring the lock
797  * in a fast path, and then recheck the flag after acquiring the lock in
798  * a slow path.
799  */
800 #define	SFMMU_FLAGS_ISSET(sfmmup, flags) \
801 	(((sfmmup)->sfmmu_flags & (flags)) == (flags))
802 
803 #define	SFMMU_FLAGS_CLEAR(sfmmup, flags) \
804 	(ASSERT(sfmmu_hat_lock_held((sfmmup))), \
805 	(sfmmup)->sfmmu_flags &= ~(flags))
806 
807 #define	SFMMU_FLAGS_SET(sfmmup, flags) \
808 	(ASSERT(sfmmu_hat_lock_held((sfmmup))), \
809 	(sfmmup)->sfmmu_flags |= (flags))
810 
811 #define	SFMMU_TTEFLAGS_ISSET(sfmmup, flags) \
812 	((((sfmmup)->sfmmu_tteflags | (sfmmup)->sfmmu_rtteflags) & (flags)) == \
813 	    (flags))
814 
815 
816 /*
817  * sfmmu tte HAT flags, must fit in 8 bits
818  */
819 #define	HAT_CHKCTX1_FLAG 0x1
820 #define	HAT_64K_FLAG	(0x1 << TTE64K)
821 #define	HAT_512K_FLAG	(0x1 << TTE512K)
822 #define	HAT_4M_FLAG	(0x1 << TTE4M)
823 #define	HAT_32M_FLAG	(0x1 << TTE32M)
824 #define	HAT_256M_FLAG	(0x1 << TTE256M)
825 
826 /*
827  * sfmmu HAT flags, 16 bits at the moment.
828  */
829 #define	HAT_4MTEXT_FLAG		0x01
830 #define	HAT_32M_ISM		0x02
831 #define	HAT_256M_ISM		0x04
832 #define	HAT_SWAPPED		0x08 /* swapped out */
833 #define	HAT_SWAPIN		0x10 /* swapping in */
834 #define	HAT_BUSY		0x20 /* replacing TSB(s) */
835 #define	HAT_ISMBUSY		0x40 /* adding/removing/traversing ISM maps */
836 
837 #define	HAT_CTX1_FLAG   	0x100 /* ISM imap hatflag for ctx1 */
838 #define	HAT_JOIN_SCD		0x200 /* region is joining scd */
839 #define	HAT_ALLCTX_INVALID	0x400 /* all per-MMU ctxs are invalidated */
840 
841 #define	SFMMU_LGPGS_INUSE(sfmmup)					\
842 	(((sfmmup)->sfmmu_tteflags | (sfmmup)->sfmmu_rtteflags) ||	\
843 	    ((sfmmup)->sfmmu_iblk != NULL))
844 
845 /*
846  * Starting with context 0, the first NUM_LOCKED_CTXS contexts
847  * are locked so that sfmmu_getctx can't steal any of these
848  * contexts.  At the time this software was being developed, the
849  * only context that needs to be locked is context 0 (the kernel
850  * context), and context 1 (reserved for stolen context). So this constant
851  * was originally defined to be 2.
852  *
853  * For sun4v only, USER_CONTEXT_TYPE represents any user context.  Many
854  * routines only care whether the context is kernel, invalid or user.
855  */
856 
857 #define	NUM_LOCKED_CTXS 2
858 #define	INVALID_CONTEXT	1
859 
860 #ifdef sun4v
861 #define	USER_CONTEXT_TYPE	NUM_LOCKED_CTXS
862 #endif
863 #if defined(sun4v) || defined(UTSB_PHYS)
864 /*
865  * Get the location in the 4MB base TSB of the tsbe for this fault.
866  * Assumes that the second TSB only contains 4M mappings.
867  *
868  * In:
869  *   tagacc = tag access register (not clobbered)
870  *   tsbe = 2nd TSB base register
871  *   tmp1, tmp2 = scratch registers
872  * Out:
873  *   tsbe = pointer to the tsbe in the 2nd TSB
874  */
875 
876 #define	GET_4MBASE_TSBE_PTR(tagacc, tsbe, tmp1, tmp2)			\
877 	and	tsbe, TSB_SOFTSZ_MASK, tmp2;	/* tmp2=szc */		\
878 	andn	tsbe, TSB_SOFTSZ_MASK, tsbe;	/* tsbbase */		\
879 	mov	TSB_ENTRIES(0), tmp1;	/* nentries in TSB size 0 */	\
880 	sllx	tmp1, tmp2, tmp1;	/* tmp1 = nentries in TSB */	\
881 	sub	tmp1, 1, tmp1;		/* mask = nentries - 1 */	\
882 	srlx	tagacc, MMU_PAGESHIFT4M, tmp2; 				\
883 	and	tmp2, tmp1, tmp1;	/* tsbent = virtpage & mask */	\
884 	sllx	tmp1, TSB_ENTRY_SHIFT, tmp1;	/* entry num --> ptr */	\
885 	add	tsbe, tmp1, tsbe	/* add entry offset to TSB base */
886 
887 #define	GET_2ND_TSBE_PTR(tagacc, tsbe, tmp1, tmp2)			\
888 	GET_4MBASE_TSBE_PTR(tagacc, tsbe, tmp1, tmp2)
889 
890 /*
891  * Get the location in the 3rd TSB of the tsbe for this fault.
892  * The 3rd TSB corresponds to the shared context, and is used
893  * for 8K - 512k pages.
894  *
895  * In:
896  *   tagacc = tag access register (not clobbered)
897  *   tsbe, tmp1, tmp2 = scratch registers
898  * Out:
899  *   tsbe = pointer to the tsbe in the 3rd TSB
900  */
901 
902 #define	GET_3RD_TSBE_PTR(tagacc, tsbe, tmp1, tmp2)			\
903 	and	tsbe, TSB_SOFTSZ_MASK, tmp2;    /* tmp2=szc */		\
904 	andn	tsbe, TSB_SOFTSZ_MASK, tsbe;    /* tsbbase */		\
905 	mov	TSB_ENTRIES(0), tmp1;	/* nentries in TSB size 0 */	\
906 	sllx	tmp1, tmp2, tmp1;	/* tmp1 = nentries in TSB */	\
907 	sub	tmp1, 1, tmp1;		/* mask = nentries - 1 */	\
908 	srlx	tagacc, MMU_PAGESHIFT, tmp2;				\
909 	and	tmp2, tmp1, tmp1;	/* tsbent = virtpage & mask */	\
910 	sllx	tmp1, TSB_ENTRY_SHIFT, tmp1;    /* entry num --> ptr */	\
911 	add	tsbe, tmp1, tsbe	/* add entry offset to TSB base */
912 
913 #define	GET_4TH_TSBE_PTR(tagacc, tsbe, tmp1, tmp2)                      \
914 	GET_4MBASE_TSBE_PTR(tagacc, tsbe, tmp1, tmp2)
915 /*
916  * Copy the sfmmu_region_map or scd_region_map to the tsbmiss
917  * shmermap or scd_shmermap, from sfmmu_load_mmustate.
918  */
919 #define	SET_REGION_MAP(rgn_map, tsbmiss_map, cnt, tmp, label)		\
920 	/* BEGIN CSTYLED */						\
921 label:									;\
922         ldx     [rgn_map], tmp						;\
923         dec     cnt							;\
924         add     rgn_map, CLONGSIZE, rgn_map                             ;\
925         stx     tmp, [tsbmiss_map]                                      ;\
926         brnz,pt cnt, label                                              ;\
927 	    add   tsbmiss_map, CLONGSIZE, tsbmiss_map                    \
928 	/* END CSTYLED */
929 
930 /*
931  * If there is no scd, then zero the tsbmiss scd_shmermap,
932  * from sfmmu_load_mmustate.
933  */
934 #define	ZERO_REGION_MAP(tsbmiss_map, cnt, label)                        \
935 	/* BEGIN CSTYLED */                                             \
936 label:                                                                  ;\
937         dec     cnt                                                     ;\
938         stx     %g0, [tsbmiss_map]                                      ;\
939         brnz,pt cnt, label                                              ;\
940 	    add   tsbmiss_map, CLONGSIZE, tsbmiss_map
941 	/* END CSTYLED */
942 
943 /*
944  * Set hmemisc to 1 if the shared hme is also part of an scd.
945  * In:
946  *   tsbarea = tsbmiss area (not clobbered)
947  *   hmeblkpa  = hmeblkpa +  hmentoff + SFHME_TTE (not clobbered)
948  *   hmentoff = hmentoff + SFHME_TTE = tte offset(clobbered)
949  * Out:
950  *   use_shctx = 1 if shme is in scd and 0 otherwise
951  */
952 #define	GET_SCDSHMERMAP(tsbarea, hmeblkpa, hmentoff, use_shctx)               \
953 	/* BEGIN CSTYLED */   	                                              \
954         sub     hmeblkpa, hmentoff, hmentoff    /* hmentofff = hmeblkpa */   ;\
955         add     hmentoff, HMEBLK_TAG, hmentoff                               ;\
956         ldxa    [hmentoff]ASI_MEM, hmentoff     /* read 1st part of tag */   ;\
957         and     hmentoff, HTAG_RID_MASK, hmentoff       /* mask off rid */   ;\
958         and     hmentoff, BT_ULMASK, use_shctx  /* mask bit index */         ;\
959         srlx    hmentoff, BT_ULSHIFT, hmentoff  /* extract word */           ;\
960         sllx    hmentoff, CLONGSHIFT, hmentoff  /* index */                  ;\
961         add     tsbarea, hmentoff, hmentoff             /* add to tsbarea */ ;\
962         ldx     [hmentoff + TSBMISS_SCDSHMERMAP], hmentoff      /* scdrgn */ ;\
963         srlx    hmentoff, use_shctx, use_shctx                               ;\
964         and     use_shctx, 0x1, use_shctx                                     \
965 	/* END CSTYLED */
966 
967 /*
968  * Synthesize a TSB base register contents for a process.
969  *
970  * In:
971  *   tsbinfo = TSB info pointer (ro)
972  *   tsbreg, tmp1 = scratch registers
973  * Out:
974  *   tsbreg = value to program into TSB base register
975  */
976 
977 #define	MAKE_UTSBREG(tsbinfo, tsbreg, tmp1)			\
978 	ldx	[tsbinfo + TSBINFO_PADDR], tsbreg;		\
979 	lduh	[tsbinfo + TSBINFO_SZCODE], tmp1;		\
980 	and	tmp1, TSB_SOFTSZ_MASK, tmp1;			\
981 	or	tsbreg, tmp1, tsbreg;
982 
983 
984 /*
985  * Load TSB base register to TSBMISS area for privte contexts.
986  * This register contains utsb_pabase in bits 63:13, and TSB size
987  * code in bits 2:0.
988  *
989  * For private context
990  * In:
991  *   tsbreg = value to load (ro)
992  *   regnum = constant or register
993  *   tmp1 = scratch register
994  * Out:
995  *   Specified scratchpad register updated
996  *
997  */
998 #define	SET_UTSBREG(regnum, tsbreg, tmp1)				\
999 	mov	regnum, tmp1;						\
1000 	stxa	tsbreg, [tmp1]ASI_SCRATCHPAD	/* save tsbreg */
1001 /*
1002  * Get TSB base register from the scratchpad for private contexts
1003  *
1004  * In:
1005  *   regnum = constant or register
1006  *   tsbreg = scratch
1007  * Out:
1008  *   tsbreg = tsbreg from the specified scratchpad register
1009  */
1010 #define	GET_UTSBREG(regnum, tsbreg)					\
1011 	mov	regnum, tsbreg;						\
1012 	ldxa	[tsbreg]ASI_SCRATCHPAD, tsbreg
1013 
1014 /*
1015  * Load TSB base register to TSBMISS area for shared contexts.
1016  * This register contains utsb_pabase in bits 63:13, and TSB size
1017  * code in bits 2:0.
1018  *
1019  * In:
1020  *   tsbmiss = pointer to tsbmiss area
1021  *   tsbmissoffset = offset to right tsb pointer
1022  *   tsbreg = value to load (ro)
1023  * Out:
1024  *   Specified tsbmiss area updated
1025  *
1026  */
1027 #define	SET_UTSBREG_SHCTX(tsbmiss, tsbmissoffset, tsbreg)		\
1028 	stx	tsbreg, [tsbmiss + tsbmissoffset]	/* save tsbreg */
1029 
1030 /*
1031  * Get TSB base register from the scratchpad for
1032  * shared contexts
1033  *
1034  * In:
1035  *   tsbmiss = pointer to tsbmiss area
1036  *   tsbmissoffset = offset to right tsb pointer
1037  *   tsbreg = scratch
1038  * Out:
1039  *   tsbreg = tsbreg from the specified scratchpad register
1040  */
1041 #define	GET_UTSBREG_SHCTX(tsbmiss, tsbmissoffset, tsbreg)		\
1042 	ldx	[tsbmiss + tsbmissoffset], tsbreg
1043 
1044 #endif /* defined(sun4v) || defined(UTSB_PHYS) */
1045 
1046 #ifndef	_ASM
1047 
1048 /*
1049  * Kernel page relocation stuff.
1050  */
1051 struct sfmmu_callback {
1052 	int key;
1053 	int (*prehandler)(caddr_t, uint_t, uint_t, void *);
1054 	int (*posthandler)(caddr_t, uint_t, uint_t, void *, pfn_t);
1055 	int (*errhandler)(caddr_t, uint_t, uint_t, void *);
1056 	int capture_cpus;
1057 };
1058 
1059 extern int sfmmu_max_cb_id;
1060 extern struct sfmmu_callback *sfmmu_cb_table;
1061 
1062 extern int hat_kpr_enabled;
1063 
1064 struct pa_hment;
1065 
1066 /*
1067  * RFE: With multihat gone we gain back an int.  We could use this to
1068  * keep ref bits on a per cpu basis to eliminate xcalls.
1069  */
1070 struct sf_hment {
1071 	tte_t hme_tte;			/* tte for this hment */
1072 
1073 	union {
1074 		struct page *page;	/* what page this maps */
1075 		struct pa_hment *data;	/* pa_hment */
1076 	} sf_hment_un;
1077 
1078 	struct	sf_hment *hme_next;	/* next hment */
1079 	struct	sf_hment *hme_prev;	/* prev hment */
1080 };
1081 
1082 struct pa_hment {
1083 	caddr_t		addr;		/* va */
1084 	uint_t		len;		/* bytes */
1085 	ushort_t	flags;		/* internal flags */
1086 	ushort_t	refcnt;		/* reference count */
1087 	id_t		cb_id;		/* callback id, table index */
1088 	void		*pvt;		/* handler's private data */
1089 	struct sf_hment	sfment;		/* corresponding dummy sf_hment */
1090 };
1091 
1092 #define	hme_page		sf_hment_un.page
1093 #define	hme_data		sf_hment_un.data
1094 #define	hme_size(sfhmep)	((int)(TTE_CSZ(&(sfhmep)->hme_tte)))
1095 #define	PAHME_SZ		(sizeof (struct pa_hment))
1096 #define	SFHME_SZ		(sizeof (struct sf_hment))
1097 
1098 #define	IS_PAHME(hme)	((hme)->hme_tte.ll == 0)
1099 
1100 /*
1101  * hmeblk_tag structure
1102  * structure used to obtain a match on a hme_blk.  Currently consists of
1103  * the address of the sfmmu struct (or hatid), the base page address of the
1104  * hme_blk, and the rehash count.  The rehash count is actually only 2 bits
1105  * and has the following meaning:
1106  * 1 = 8k or 64k hash sequence.
1107  * 2 = 512k hash sequence.
1108  * 3 = 4M hash sequence.
1109  * We require this count because we don't want to get a false hit on a 512K or
1110  * 4M rehash with a base address corresponding to a 8k or 64k hmeblk.
1111  * Note:  The ordering and size of the hmeblk_tag members are implictly known
1112  * by the tsb miss handlers written in assembly.  Do not change this structure
1113  * without checking those routines.  See HTAG_SFMMUPSZ define.
1114  */
1115 
1116 /*
1117  * In private hmeblks hblk_rid field must be SFMMU_INVALID_RID.
1118  */
1119 typedef union {
1120 	struct {
1121 		uint64_t	hblk_basepg: 51,	/* hme_blk base pg # */
1122 				hblk_rehash: 3,		/* rehash number */
1123 				hblk_rid: 10;		/* hme_blk region id */
1124 		void		*hblk_id;
1125 	} hblk_tag_un;
1126 	uint64_t		htag_tag[2];
1127 } hmeblk_tag;
1128 
1129 #define	htag_id		hblk_tag_un.hblk_id
1130 #define	htag_bspage	hblk_tag_un.hblk_basepg
1131 #define	htag_rehash	hblk_tag_un.hblk_rehash
1132 #define	htag_rid	hblk_tag_un.hblk_rid
1133 
1134 #endif /* !_ASM */
1135 
1136 #define	HTAG_REHASH_SHIFT	10
1137 #define	HTAG_MAX_RID	(((0x1 << HTAG_REHASH_SHIFT) - 1))
1138 #define	HTAG_RID_MASK	HTAG_MAX_RID
1139 
1140 /* used for tagging all per sfmmu (i.e. non SRD) private hmeblks */
1141 #define	SFMMU_INVALID_SHMERID	HTAG_MAX_RID
1142 
1143 #if SFMMU_INVALID_SHMERID < SFMMU_MAX_HME_REGIONS
1144 #error SFMMU_INVALID_SHMERID < SFMMU_MAX_HME_REGIONS
1145 #endif
1146 
1147 #define	SFMMU_IS_SHMERID_VALID(rid)	((rid) != SFMMU_INVALID_SHMERID)
1148 
1149 /* ISM regions */
1150 #define	SFMMU_INVALID_ISMRID	0xff
1151 
1152 #if SFMMU_INVALID_ISMRID < SFMMU_MAX_ISM_REGIONS
1153 #error SFMMU_INVALID_ISMRID < SFMMU_MAX_ISM_REGIONS
1154 #endif
1155 
1156 #define	SFMMU_IS_ISMRID_VALID(rid)	((rid) != SFMMU_INVALID_ISMRID)
1157 
1158 
1159 #define	HTAGS_EQ(tag1, tag2)	(((tag1.htag_tag[0] ^ tag2.htag_tag[0]) | \
1160 				(tag1.htag_tag[1] ^ tag2.htag_tag[1])) == 0)
1161 
1162 /*
1163  * this macro must only be used for comparing tags in shared hmeblks.
1164  */
1165 #define	HTAGS_EQ_SHME(hmetag, tag, hrmap)				\
1166 	(((hmetag).htag_rid != SFMMU_INVALID_SHMERID) &&	        \
1167 	(((((hmetag).htag_tag[0] ^ (tag).htag_tag[0]) &			\
1168 		~HTAG_RID_MASK) |	        			\
1169 	    ((hmetag).htag_tag[1] ^ (tag).htag_tag[1])) == 0) &&	\
1170 	SF_RGNMAP_TEST(hrmap, hmetag.htag_rid))
1171 
1172 #define	HME_REHASH(sfmmup)						\
1173 	((sfmmup)->sfmmu_ttecnt[TTE512K] != 0 ||			\
1174 	(sfmmup)->sfmmu_ttecnt[TTE4M] != 0 ||				\
1175 	(sfmmup)->sfmmu_ttecnt[TTE32M] != 0 ||				\
1176 	(sfmmup)->sfmmu_ttecnt[TTE256M] != 0)
1177 
1178 #define	NHMENTS		8		/* # of hments in an 8k hme_blk */
1179 					/* needs to be multiple of 2 */
1180 
1181 #ifndef	_ASM
1182 
1183 #ifdef	HBLK_TRACE
1184 
1185 #define	HBLK_LOCK		1
1186 #define	HBLK_UNLOCK		0
1187 #define	HBLK_STACK_DEPTH	6
1188 #define	HBLK_AUDIT_CACHE_SIZE	16
1189 #define	HBLK_LOCK_PATTERN	0xaaaaaaaa
1190 #define	HBLK_UNLOCK_PATTERN	0xbbbbbbbb
1191 
1192 struct hblk_lockcnt_audit {
1193 	int		flag;		/* lock or unlock */
1194 	kthread_id_t	thread;
1195 	int		depth;
1196 	pc_t		stack[HBLK_STACK_DEPTH];
1197 };
1198 
1199 #endif	/* HBLK_TRACE */
1200 
1201 
1202 /*
1203  * Hment block structure.
1204  * The hme_blk is the node data structure which the hash structure
1205  * mantains. An hme_blk can have 2 different sizes depending on the
1206  * number of hments it implicitly contains.  When dealing with 64K, 512K,
1207  * or 4M hments there is one hment per hme_blk.  When dealing with
1208  * 8k hments we allocate an hme_blk plus an additional 7 hments to
1209  * give us a total of 8 (NHMENTS) hments that can be referenced through a
1210  * hme_blk.
1211  *
1212  * The hmeblk structure contains 2 tte reference counters used to determine if
1213  * it is ok to free up the hmeblk.  Both counters have to be zero in order
1214  * to be able to free up hmeblk.  They are protected by cas.
1215  * hblk_hmecnt is the number of hments present on pp mapping lists.
1216  * hblk_vcnt reflects number of valid ttes in hmeblk.
1217  *
1218  * The hmeblk now also has per tte lock cnts.  This is required because
1219  * the counts can be high and there are not enough bits in the tte. When
1220  * physio is fixed to not lock the translations we should be able to move
1221  * the lock cnt back to the tte.  See bug id 1198554.
1222  *
1223  * Note that xhat_hme_blk's layout follows this structure: hme_blk_misc
1224  * and sf_hment are at the same offsets in both structures. Whenever
1225  * hme_blk is changed, xhat_hme_blk may need to be updated as well.
1226  */
1227 
1228 struct hme_blk_misc {
1229 	uint_t	notused:25;
1230 	uint_t	shared_bit:1;	/* set for SRD shared hmeblk */
1231 	uint_t	xhat_bit:1;	/* set for an xhat hme_blk */
1232 	uint_t	shadow_bit:1;	/* set for a shadow hme_blk */
1233 	uint_t	nucleus_bit:1;	/* set for a nucleus hme_blk */
1234 	uint_t	ttesize:3;	/* contains ttesz of hmeblk */
1235 };
1236 
1237 struct hme_blk {
1238 	uint64_t	hblk_nextpa;	/* physical address for hash list */
1239 
1240 	hmeblk_tag	hblk_tag;	/* tag used to obtain an hmeblk match */
1241 
1242 	struct hme_blk	*hblk_next;	/* on free list or on hash list */
1243 					/* protected by hash lock */
1244 
1245 	struct hme_blk	*hblk_shadow;	/* pts to shadow hblk */
1246 					/* protected by hash lock */
1247 	uint_t		hblk_span;	/* span of memory hmeblk maps */
1248 
1249 	struct hme_blk_misc	hblk_misc;
1250 
1251 	union {
1252 		struct {
1253 			ushort_t hblk_hmecount;	/* hment on mlists counter */
1254 			ushort_t hblk_validcnt;	/* valid tte reference count */
1255 		} hblk_counts;
1256 		uint_t		hblk_shadow_mask;
1257 	} hblk_un;
1258 
1259 	uint_t		hblk_lckcnt;
1260 
1261 #ifdef	HBLK_TRACE
1262 	kmutex_t	hblk_audit_lock;	/* lock to protect index */
1263 	uint_t		hblk_audit_index;	/* index into audit_cache */
1264 	struct	hblk_lockcnt_audit hblk_audit_cache[HBLK_AUDIT_CACHE_SIZE];
1265 #endif	/* HBLK_AUDIT */
1266 
1267 	struct sf_hment hblk_hme[1];	/* hment array */
1268 };
1269 
1270 #define	hblk_shared	hblk_misc.shared_bit
1271 #define	hblk_xhat_bit   hblk_misc.xhat_bit
1272 #define	hblk_shw_bit	hblk_misc.shadow_bit
1273 #define	hblk_nuc_bit	hblk_misc.nucleus_bit
1274 #define	hblk_ttesz	hblk_misc.ttesize
1275 #define	hblk_hmecnt	hblk_un.hblk_counts.hblk_hmecount
1276 #define	hblk_vcnt	hblk_un.hblk_counts.hblk_validcnt
1277 #define	hblk_shw_mask	hblk_un.hblk_shadow_mask
1278 
1279 #define	MAX_HBLK_LCKCNT	0xFFFFFFFF
1280 #define	HMEBLK_ALIGN	0x8		/* hmeblk has to be double aligned */
1281 
1282 #ifdef	HBLK_TRACE
1283 
1284 #define	HBLK_STACK_TRACE(hmeblkp, lock)					\
1285 {									\
1286 	int flag = lock;	/* to pacify lint */			\
1287 	int audit_index;						\
1288 									\
1289 	mutex_enter(&hmeblkp->hblk_audit_lock);				\
1290 	audit_index = hmeblkp->hblk_audit_index;			\
1291 	hmeblkp->hblk_audit_index = ((hmeblkp->hblk_audit_index + 1) &	\
1292 	    (HBLK_AUDIT_CACHE_SIZE - 1));				\
1293 	mutex_exit(&hmeblkp->hblk_audit_lock);				\
1294 									\
1295 	if (flag)							\
1296 		hmeblkp->hblk_audit_cache[audit_index].flag =		\
1297 		    HBLK_LOCK_PATTERN;					\
1298 	else								\
1299 		hmeblkp->hblk_audit_cache[audit_index].flag =		\
1300 		    HBLK_UNLOCK_PATTERN;				\
1301 									\
1302 	hmeblkp->hblk_audit_cache[audit_index].thread = curthread;	\
1303 	hmeblkp->hblk_audit_cache[audit_index].depth =			\
1304 	    getpcstack(hmeblkp->hblk_audit_cache[audit_index].stack,	\
1305 	    HBLK_STACK_DEPTH);						\
1306 }
1307 
1308 #else
1309 
1310 #define	HBLK_STACK_TRACE(hmeblkp, lock)
1311 
1312 #endif	/* HBLK_TRACE */
1313 
1314 #define	HMEHASH_FACTOR	16	/* used to calc # of buckets in hme hash */
1315 
1316 /*
1317  * A maximum number of user hmeblks is defined in order to place an upper
1318  * limit on how much nucleus memory is required and to avoid overflowing the
1319  * tsbmiss uhashsz and khashsz data areas. The number below corresponds to
1320  * the number of buckets required, for an average hash chain length of 4 on
1321  * a 16TB machine.
1322  */
1323 
1324 #define	MAX_UHME_BUCKETS	(0x1 << 30)
1325 #define	MAX_KHME_BUCKETS	(0x1 << 30)
1326 
1327 /*
1328  * The minimum number of kernel hash buckets.
1329  */
1330 #define	MIN_KHME_BUCKETS	0x800
1331 
1332 /*
1333  * The number of hash buckets must be a power of 2. If the initial calculated
1334  * value is less than USER_BUCKETS_THRESHOLD we round up to the next greater
1335  * power of 2, otherwise we round down to avoid huge over allocations.
1336  */
1337 #define	USER_BUCKETS_THRESHOLD	(1<<22)
1338 
1339 #define	MAX_NUCUHME_BUCKETS	0x4000
1340 #define	MAX_NUCKHME_BUCKETS	0x2000
1341 
1342 /*
1343  * There are 2 locks in the hmehash bucket.  The hmehash_mutex is
1344  * a regular mutex used to make sure operations on a hash link are only
1345  * done by one thread.  Any operation which comes into the hat with
1346  * a <vaddr, as> will grab the hmehash_mutex.  Normally one would expect
1347  * the tsb miss handlers to grab the hash lock to make sure the hash list
1348  * is consistent while we traverse it.  Unfortunately this can lead to
1349  * deadlocks or recursive mutex enters since it is possible for
1350  * someone holding the lock to take a tlb/tsb miss.
1351  * To solve this problem we have added the hmehash_listlock.  This lock
1352  * is only grabbed by the tsb miss handlers, vatopfn, and while
1353  * adding/removing a hmeblk from the hash list. The code is written to
1354  * guarantee we won't take a tlb miss while holding this lock.
1355  */
1356 struct hmehash_bucket {
1357 	kmutex_t	hmehash_mutex;
1358 	uint64_t	hmeh_nextpa;	/* physical address for hash list */
1359 	struct hme_blk *hmeblkp;
1360 	uint_t		hmeh_listlock;
1361 };
1362 
1363 #endif /* !_ASM */
1364 
1365 #define	SFMMU_PGCNT_MASK	0x3f
1366 #define	SFMMU_PGCNT_SHIFT	6
1367 #define	INVALID_MMU_ID		-1
1368 #define	SFMMU_MMU_GNUM_RSHIFT	16
1369 #define	SFMMU_MMU_CNUM_LSHIFT	(64 - SFMMU_MMU_GNUM_RSHIFT)
1370 #define	MAX_SFMMU_CTX_VAL	((1 << 16) - 1) /* for sanity check */
1371 #define	MAX_SFMMU_GNUM_VAL	((0x1UL << 48) - 1)
1372 
1373 /*
1374  * The tsb miss handlers written in assembly know that sfmmup
1375  * is a 64 bit ptr.
1376  *
1377  * The bspage and re-hash part is 64 bits, with the sfmmup being another 64
1378  * bits.
1379  */
1380 #define	HTAG_SFMMUPSZ		0	/* Not really used for LP64 */
1381 #define	HTAG_BSPAGE_SHIFT	13
1382 
1383 /*
1384  * Assembly routines need to be able to get to ttesz
1385  */
1386 #define	HBLK_SZMASK		0x7
1387 
1388 #ifndef _ASM
1389 
1390 /*
1391  * Returns the number of bytes that an hmeblk spans given its tte size
1392  */
1393 #define	get_hblk_span(hmeblkp) ((hmeblkp)->hblk_span)
1394 #define	get_hblk_ttesz(hmeblkp)	((hmeblkp)->hblk_ttesz)
1395 #define	get_hblk_cache(hmeblkp)	(((hmeblkp)->hblk_ttesz == TTE8K) ? \
1396 	sfmmu8_cache : sfmmu1_cache)
1397 #define	HMEBLK_SPAN(ttesz)						\
1398 	((ttesz == TTE8K)? (TTEBYTES(ttesz) * NHMENTS) : TTEBYTES(ttesz))
1399 
1400 #define	set_hblk_sz(hmeblkp, ttesz)				\
1401 	(hmeblkp)->hblk_ttesz = (ttesz);			\
1402 	(hmeblkp)->hblk_span = HMEBLK_SPAN(ttesz)
1403 
1404 #define	get_hblk_base(hmeblkp)					\
1405 	((uintptr_t)(hmeblkp)->hblk_tag.htag_bspage << MMU_PAGESHIFT)
1406 
1407 #define	get_hblk_endaddr(hmeblkp)				\
1408 	((caddr_t)(get_hblk_base(hmeblkp) + get_hblk_span(hmeblkp)))
1409 
1410 #define	in_hblk_range(hmeblkp, vaddr)					\
1411 	(((uintptr_t)(vaddr) >= get_hblk_base(hmeblkp)) &&		\
1412 	((uintptr_t)(vaddr) < (get_hblk_base(hmeblkp) +			\
1413 	get_hblk_span(hmeblkp))))
1414 
1415 #define	tte_to_vaddr(hmeblkp, tte)	((caddr_t)(get_hblk_base(hmeblkp) \
1416 	+ (TTEBYTES(TTE_CSZ(&tte)) * (tte).tte_hmenum)))
1417 
1418 #define	tte_to_evaddr(hmeblkp, ttep)	((caddr_t)(get_hblk_base(hmeblkp) \
1419 	+ (TTEBYTES(TTE_CSZ(ttep)) * ((ttep)->tte_hmenum + 1))))
1420 
1421 #define	vaddr_to_vshift(hblktag, vaddr, shwsz)				\
1422 	((((uintptr_t)(vaddr) >> MMU_PAGESHIFT) - (hblktag.htag_bspage)) >>\
1423 	TTE_BSZS_SHIFT((shwsz) - 1))
1424 
1425 #define	HME8BLK_SZ	(sizeof (struct hme_blk) + \
1426 			(NHMENTS - 1) * sizeof (struct sf_hment))
1427 #define	HME1BLK_SZ	(sizeof (struct hme_blk))
1428 #define	H1MIN		(2 + MAX_BIGKTSB_TTES)	/* nucleus text+data, ktsb */
1429 
1430 /*
1431  * Hme_blk hash structure
1432  * Active mappings are kept in a hash structure of hme_blks.  The hash
1433  * function is based on (ctx, vaddr) The size of the hash table size is a
1434  * power of 2 such that the average hash chain lenth is HMENT_HASHAVELEN.
1435  * The hash actually consists of 2 separate hashes.  One hash is for the user
1436  * address space and the other hash is for the kernel address space.
1437  * The number of buckets are calculated at boot time and stored in the global
1438  * variables "uhmehash_num" and "khmehash_num".  By making the hash table size
1439  * a power of 2 we can use a simply & function to derive an index instead of
1440  * a divide.
1441  *
1442  * HME_HASH_FUNCTION(hatid, vaddr, shift) returns a pointer to a hme_hash
1443  * bucket.
1444  * An hme hash bucket contains a pointer to an hme_blk and the mutex that
1445  * protects the link list.
1446  * Spitfire supports 4 page sizes.  8k and 64K pages only need one hash.
1447  * 512K pages need 2 hashes and 4M pages need 3 hashes.
1448  * The 'shift' parameter controls how many bits the vaddr will be shifted in
1449  * the hash function. It is calculated in the HME_HASH_SHIFT(ttesz) function
1450  * and it varies depending on the page size as follows:
1451  *	8k pages:  	HBLK_RANGE_SHIFT
1452  *	64k pages:	MMU_PAGESHIFT64K
1453  *	512K pages:	MMU_PAGESHIFT512K
1454  *	4M pages:	MMU_PAGESHIFT4M
1455  * An assembly version of the hash function exists in sfmmu_ktsb_miss(). All
1456  * changes should be reflected in both versions.  This function and the TSB
1457  * miss handlers are the only places which know about the two hashes.
1458  *
1459  * HBLK_RANGE_SHIFT controls range of virtual addresses that will fall
1460  * into the same bucket for a particular process.  It is currently set to
1461  * be equivalent to 64K range or one hme_blk.
1462  *
1463  * The hme_blks in the hash are protected by a per hash bucket mutex
1464  * known as SFMMU_HASH_LOCK.
1465  * You need to acquire this lock before traversing the hash bucket link
1466  * list, while adding/removing a hme_blk to the list, and while
1467  * modifying an hme_blk.  A possible optimization is to replace these
1468  * mutexes by readers/writer lock but right now it is not clear whether
1469  * this is a win or not.
1470  *
1471  * The HME_HASH_TABLE_SEARCH will search the hash table for the
1472  * hme_blk that contains the hment that corresponds to the passed
1473  * ctx and vaddr.  It assumed the SFMMU_HASH_LOCK is held.
1474  */
1475 
1476 #endif /* ! _ASM */
1477 
1478 #define	KHATID			ksfmmup
1479 #define	UHMEHASH_SZ		uhmehash_num
1480 #define	KHMEHASH_SZ		khmehash_num
1481 #define	HMENT_HASHAVELEN	4
1482 #define	HBLK_RANGE_SHIFT	MMU_PAGESHIFT64K /* shift for HBLK_BS_MASK */
1483 #define	HBLK_MIN_TTESZ		1
1484 #define	HBLK_MIN_BYTES		MMU_PAGESIZE64K
1485 #define	HBLK_MIN_SHIFT		MMU_PAGESHIFT64K
1486 #define	MAX_HASHCNT		5
1487 #define	DEFAULT_MAX_HASHCNT	3
1488 
1489 #ifndef _ASM
1490 
1491 #define	HASHADDR_MASK(hashno)	TTE_PAGEMASK(hashno)
1492 
1493 #define	HME_HASH_SHIFT(ttesz)						\
1494 	((ttesz == TTE8K)? HBLK_RANGE_SHIFT : TTE_PAGE_SHIFT(ttesz))
1495 
1496 #define	HME_HASH_ADDR(vaddr, hmeshift)					\
1497 	((caddr_t)(((uintptr_t)(vaddr) >> (hmeshift)) << (hmeshift)))
1498 
1499 #define	HME_HASH_BSPAGE(vaddr, hmeshift)				\
1500 	(((uintptr_t)(vaddr) >> (hmeshift)) << ((hmeshift) - MMU_PAGESHIFT))
1501 
1502 #define	HME_HASH_REHASH(ttesz)						\
1503 	(((ttesz) < TTE512K)? 1 : (ttesz))
1504 
1505 #define	HME_HASH_FUNCTION(hatid, vaddr, shift)				     \
1506 	((((void *)hatid) != ((void *)KHATID)) ?			     \
1507 	(&uhme_hash[ (((uintptr_t)(hatid) ^ ((uintptr_t)vaddr >> (shift))) & \
1508 	    UHMEHASH_SZ) ]):						     \
1509 	(&khme_hash[ (((uintptr_t)(hatid) ^ ((uintptr_t)vaddr >> (shift))) & \
1510 	    KHMEHASH_SZ) ]))
1511 
1512 /*
1513  * This macro will traverse a hmeblk hash link list looking for an hme_blk
1514  * that owns the specified vaddr and hatid.  If if doesn't find one , hmeblkp
1515  * will be set to NULL, otherwise it will point to the correct hme_blk.
1516  * This macro also cleans empty hblks.
1517  */
1518 #define	HME_HASH_SEARCH_PREV(hmebp, hblktag, hblkp, hblkpa,		\
1519 	pr_hblk, prevpa, listp)						\
1520 {									\
1521 	struct hme_blk *nx_hblk;					\
1522 	uint64_t 	nx_pa;						\
1523 									\
1524 	ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp));				\
1525 	hblkp = hmebp->hmeblkp;						\
1526 	hblkpa = hmebp->hmeh_nextpa;					\
1527 	prevpa = 0;							\
1528 	pr_hblk = NULL;							\
1529 	while (hblkp) {							\
1530 		if (HTAGS_EQ(hblkp->hblk_tag, hblktag)) {		\
1531 			/* found hme_blk */				\
1532 			break;						\
1533 		}							\
1534 		nx_hblk = hblkp->hblk_next;				\
1535 		nx_pa = hblkp->hblk_nextpa;				\
1536 		if (!hblkp->hblk_vcnt && !hblkp->hblk_hmecnt) {		\
1537 			sfmmu_hblk_hash_rm(hmebp, hblkp, prevpa, pr_hblk); \
1538 			sfmmu_hblk_free(hmebp, hblkp, hblkpa, listp);   \
1539 		} else {						\
1540 			pr_hblk = hblkp;				\
1541 			prevpa = hblkpa;				\
1542 		}							\
1543 		hblkp = nx_hblk;					\
1544 		hblkpa = nx_pa;						\
1545 	}								\
1546 }
1547 
1548 #define	HME_HASH_SEARCH(hmebp, hblktag, hblkp, listp)			\
1549 {									\
1550 	struct hme_blk *pr_hblk;					\
1551 	uint64_t hblkpa, prevpa;					\
1552 									\
1553 	HME_HASH_SEARCH_PREV(hmebp, hblktag, hblkp, hblkpa, pr_hblk,	\
1554 		prevpa, listp);						\
1555 }
1556 
1557 /*
1558  * This macro will traverse a hmeblk hash link list looking for an hme_blk
1559  * that owns the specified vaddr and hatid.  If if doesn't find one , hmeblkp
1560  * will be set to NULL, otherwise it will point to the correct hme_blk.
1561  * It doesn't remove empty hblks.
1562  */
1563 #define	HME_HASH_FAST_SEARCH(hmebp, hblktag, hblkp)			\
1564 	ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp));				\
1565 	for (hblkp = hmebp->hmeblkp; hblkp;				\
1566 	    hblkp = hblkp->hblk_next) {					\
1567 		if (HTAGS_EQ(hblkp->hblk_tag, hblktag)) {		\
1568 			/* found hme_blk */				\
1569 			break;						\
1570 		}							\
1571 	}
1572 
1573 #define	SFMMU_HASH_LOCK(hmebp)						\
1574 		(mutex_enter(&hmebp->hmehash_mutex))
1575 
1576 #define	SFMMU_HASH_UNLOCK(hmebp)					\
1577 		(mutex_exit(&hmebp->hmehash_mutex))
1578 
1579 #define	SFMMU_HASH_LOCK_TRYENTER(hmebp)					\
1580 		(mutex_tryenter(&hmebp->hmehash_mutex))
1581 
1582 #define	SFMMU_HASH_LOCK_ISHELD(hmebp)					\
1583 		(mutex_owned(&hmebp->hmehash_mutex))
1584 
1585 #define	SFMMU_XCALL_STATS(sfmmup)					\
1586 {									\
1587 	if (sfmmup == ksfmmup) {					\
1588 		SFMMU_STAT(sf_kernel_xcalls);				\
1589 	} else {							\
1590 		SFMMU_STAT(sf_user_xcalls);				\
1591 	}								\
1592 }
1593 
1594 #define	astosfmmu(as)		((as)->a_hat)
1595 #define	hblktosfmmu(hmeblkp)	((sfmmu_t *)(hmeblkp)->hblk_tag.htag_id)
1596 #define	hblktosrd(hmeblkp)	((sf_srd_t *)(hmeblkp)->hblk_tag.htag_id)
1597 #define	sfmmutoas(sfmmup)	((sfmmup)->sfmmu_as)
1598 
1599 #define	sfmmutohtagid(sfmmup, rid)			   \
1600 	(((rid) == SFMMU_INVALID_SHMERID) ? (void *)(sfmmup) : \
1601 	(void *)((sfmmup)->sfmmu_srdp))
1602 
1603 /*
1604  * We use the sfmmu data structure to keep the per as page coloring info.
1605  */
1606 #define	as_color_bin(as)	(astosfmmu(as)->sfmmu_clrbin)
1607 #define	as_color_start(as)	(astosfmmu(as)->sfmmu_clrstart)
1608 
1609 typedef struct {
1610 	char	h8[HME8BLK_SZ];
1611 } hblk8_t;
1612 
1613 typedef struct {
1614 	char	h1[HME1BLK_SZ];
1615 } hblk1_t;
1616 
1617 typedef struct {
1618 	ulong_t  	index;
1619 	ulong_t  	len;
1620 	hblk8_t		*list;
1621 } nucleus_hblk8_info_t;
1622 
1623 typedef struct {
1624 	ulong_t		index;
1625 	ulong_t		len;
1626 	hblk1_t		*list;
1627 } nucleus_hblk1_info_t;
1628 
1629 /*
1630  * This struct is used for accumlating information about a range
1631  * of pages that are unloading so that a single xcall can flush
1632  * the entire range from remote tlbs. A function that must demap
1633  * a range of virtual addresses declares one of these structures
1634  * and initializes using DEMP_RANGE_INIT(). It then passes a pointer to this
1635  * struct to the appropriate sfmmu_hblk_* level function which does
1636  * all the bookkeeping using the other macros. When the function has
1637  * finished the virtual address range, it needs to call DEMAP_RANGE_FLUSH()
1638  * macro to take care of any remaining unflushed mappings.
1639  *
1640  * The maximum range this struct can represent is the number of bits
1641  * in the dmr_bitvec field times the pagesize in dmr_pgsz. Currently, only
1642  * MMU_PAGESIZE pages are supported.
1643  *
1644  * Since there are now cases where it's no longer necessary to do
1645  * flushes (e.g. when the process isn't runnable because it's swapping
1646  * out or exiting) we allow these macros to take a NULL dmr input and do
1647  * nothing in that case.
1648  */
1649 typedef struct {
1650 	sfmmu_t		*dmr_sfmmup;	/* relevant hat */
1651 	caddr_t		dmr_addr;	/* beginning address */
1652 	caddr_t		dmr_endaddr;	/* ending  address */
1653 	ulong_t		dmr_bitvec;	/* valid pages found */
1654 	ulong_t		dmr_bit;	/* next page to examine */
1655 	ulong_t		dmr_maxbit;	/* highest page in range */
1656 	ulong_t		dmr_pgsz;	/* page size in range */
1657 } demap_range_t;
1658 
1659 #define	DMR_MAXBIT ((ulong_t)1<<63) /* dmr_bit high bit */
1660 
1661 #define	DEMAP_RANGE_INIT(sfmmup, dmrp) \
1662 	if ((dmrp) != NULL) { \
1663 	(dmrp)->dmr_sfmmup = (sfmmup); \
1664 	(dmrp)->dmr_bitvec = 0; \
1665 	(dmrp)->dmr_maxbit = sfmmu_dmr_maxbit; \
1666 	(dmrp)->dmr_pgsz = MMU_PAGESIZE; \
1667 	}
1668 
1669 #define	DEMAP_RANGE_PGSZ(dmrp) ((dmrp)? (dmrp)->dmr_pgsz : MMU_PAGESIZE)
1670 
1671 #define	DEMAP_RANGE_CONTINUE(dmrp, addr, endaddr) \
1672 	if ((dmrp) != NULL) { \
1673 	if ((dmrp)->dmr_bitvec != 0 && (dmrp)->dmr_endaddr != (addr)) \
1674 		sfmmu_tlb_range_demap(dmrp); \
1675 	(dmrp)->dmr_endaddr = (endaddr); \
1676 	}
1677 
1678 #define	DEMAP_RANGE_FLUSH(dmrp) \
1679 	if ((dmrp) != NULL) { \
1680 		if ((dmrp)->dmr_bitvec != 0) \
1681 			sfmmu_tlb_range_demap(dmrp); \
1682 	}
1683 
1684 #define	DEMAP_RANGE_MARKPG(dmrp, addr) \
1685 	if ((dmrp) != NULL) { \
1686 		if ((dmrp)->dmr_bitvec == 0) { \
1687 			(dmrp)->dmr_addr = (addr); \
1688 			(dmrp)->dmr_bit = 1; \
1689 		} \
1690 		(dmrp)->dmr_bitvec |= (dmrp)->dmr_bit; \
1691 	}
1692 
1693 #define	DEMAP_RANGE_NEXTPG(dmrp) \
1694 	if ((dmrp) != NULL && (dmrp)->dmr_bitvec != 0) { \
1695 		if ((dmrp)->dmr_bit & (dmrp)->dmr_maxbit) { \
1696 			sfmmu_tlb_range_demap(dmrp); \
1697 		} else { \
1698 			(dmrp)->dmr_bit <<= 1; \
1699 		} \
1700 	}
1701 
1702 /*
1703  * TSB related structures
1704  *
1705  * The TSB is made up of tte entries.  Both the tag and data are present
1706  * in the TSB.  The TSB locking is managed as follows:
1707  * A software bit in the tsb tag is used to indicate that entry is locked.
1708  * If a cpu servicing a tsb miss reads a locked entry the tag compare will
1709  * fail forcing the cpu to go to the hat hash for the translation.
1710  * The cpu who holds the lock can then modify the data side, and the tag side.
1711  * The last write should be to the word containing the lock bit which will
1712  * clear the lock and allow the tsb entry to be read.  It is assumed that all
1713  * cpus reading the tsb will do so with atomic 128-bit loads.  An atomic 128
1714  * bit load is required to prevent the following from happening:
1715  *
1716  * cpu 0			cpu 1			comments
1717  *
1718  * ldx tag						tag unlocked
1719  *				ldstub lock		set lock
1720  *				stx data
1721  *				stx tag			unlock
1722  * ldx tag						incorrect tte!!!
1723  *
1724  * The software also maintains a bit in the tag to indicate an invalid
1725  * tsb entry.  The purpose of this bit is to allow the tsb invalidate code
1726  * to invalidate a tsb entry with a single cas.  See code for details.
1727  */
1728 
1729 union tsb_tag {
1730 	struct {
1731 		uint32_t	tag_res0:16;	/* reserved - context area */
1732 		uint32_t	tag_inv:1;	/* sw - invalid tsb entry */
1733 		uint32_t	tag_lock:1;	/* sw - locked tsb entry */
1734 		uint32_t	tag_res1:4;	/* reserved */
1735 		uint32_t	tag_va_hi:10;	/* va[63:54] */
1736 		uint32_t	tag_va_lo;	/* va[53:22] */
1737 	} tagbits;
1738 	struct tsb_tagints {
1739 		uint32_t	inthi;
1740 		uint32_t	intlo;
1741 	} tagints;
1742 };
1743 #define	tag_invalid		tagbits.tag_inv
1744 #define	tag_locked		tagbits.tag_lock
1745 #define	tag_vahi		tagbits.tag_va_hi
1746 #define	tag_valo		tagbits.tag_va_lo
1747 #define	tag_inthi		tagints.inthi
1748 #define	tag_intlo		tagints.intlo
1749 
1750 struct tsbe {
1751 	union tsb_tag	tte_tag;
1752 	tte_t		tte_data;
1753 };
1754 
1755 /*
1756  * A per cpu struct is kept that duplicates some info
1757  * used by the tl>0 tsb miss handlers plus it provides
1758  * a scratch area.  Its purpose is to minimize cache misses
1759  * in the tsb miss handler and is 128 bytes (2 e$ lines).
1760  *
1761  * There should be one allocated per cpu in nucleus memory
1762  * and should be aligned on an ecache line boundary.
1763  */
1764 struct tsbmiss {
1765 	sfmmu_t			*ksfmmup;	/* kernel hat id */
1766 	sfmmu_t			*usfmmup;	/* user hat id */
1767 	sf_srd_t		*usrdp;		/* user's SRD hat id */
1768 	struct tsbe		*tsbptr;	/* hardware computed ptr */
1769 	struct tsbe		*tsbptr4m;	/* hardware computed ptr */
1770 	struct tsbe		*tsbscdptr;	/* hardware computed ptr */
1771 	struct tsbe		*tsbscdptr4m;	/* hardware computed ptr */
1772 	uint64_t		ismblkpa;
1773 	struct hmehash_bucket	*khashstart;
1774 	struct hmehash_bucket	*uhashstart;
1775 	uint_t			khashsz;
1776 	uint_t			uhashsz;
1777 	uint16_t 		dcache_line_mask; /* used to flush dcache */
1778 	uchar_t			uhat_tteflags;	/* private page sizes */
1779 	uchar_t			uhat_rtteflags;	/* SHME pagesizes */
1780 	uint32_t		utsb_misses;
1781 	uint32_t		ktsb_misses;
1782 	uint16_t		uprot_traps;
1783 	uint16_t		kprot_traps;
1784 	/*
1785 	 * scratch[0] -> TSB_TAGACC
1786 	 * scratch[1] -> TSBMISS_HMEBP
1787 	 * scratch[2] -> TSBMISS_HATID
1788 	 */
1789 	uintptr_t		scratch[3];
1790 	ulong_t		shmermap[SFMMU_HMERGNMAP_WORDS];	/* 8 bytes */
1791 	ulong_t		scd_shmermap[SFMMU_HMERGNMAP_WORDS];	/* 8 bytes */
1792 	uint8_t		pad[48];			/* pad to 64 bytes */
1793 };
1794 
1795 /*
1796  * A per cpu struct is kept for the use within the tl>0 kpm tsb
1797  * miss handler. Some members are duplicates of common data or
1798  * the physical addresses of common data. A few members are also
1799  * written by the tl>0 kpm tsb miss handler. Its purpose is to
1800  * minimize cache misses in the kpm tsb miss handler and occupies
1801  * one ecache line. There should be one allocated per cpu in
1802  * nucleus memory and it should be aligned on an ecache line
1803  * boundary. It is not merged w/ struct tsbmiss since there is
1804  * not much to share and the tsbmiss pathes are different, so
1805  * a kpm tlbmiss/tsbmiss only touches one cacheline, except for
1806  * (DEBUG || SFMMU_STAT_GATHER) where the dtlb_misses counter
1807  * of struct tsbmiss is used on every dtlb miss.
1808  */
1809 struct kpmtsbm {
1810 	caddr_t		vbase;		/* start of address kpm range */
1811 	caddr_t		vend;		/* end of address kpm range */
1812 	uchar_t		flags;		/* flags needed in TL tsbmiss handler */
1813 	uchar_t		sz_shift;	/* for single kpm window */
1814 	uchar_t		kpmp_shift;	/* hash lock shift */
1815 	uchar_t		kpmp2pshft;	/* kpm page to page shift */
1816 	uint_t		kpmp_table_sz;	/* size of kpmp_table or kpmp_stable */
1817 	uint64_t	kpmp_tablepa;	/* paddr of kpmp_table or kpmp_stable */
1818 	uint64_t	msegphashpa;	/* paddr of memseg_phash */
1819 	struct tsbe	*tsbptr;	/* saved ktsb pointer */
1820 	uint_t		kpm_dtlb_misses; /* kpm tlbmiss counter */
1821 	uint_t		kpm_tsb_misses;	/* kpm tsbmiss counter */
1822 	uintptr_t	pad[1];
1823 };
1824 
1825 extern size_t	tsb_slab_size;
1826 extern uint_t	tsb_slab_shift;
1827 extern size_t	tsb_slab_mask;
1828 
1829 #endif /* !_ASM */
1830 
1831 /*
1832  * Flags for TL kpm tsbmiss handler
1833  */
1834 #define	KPMTSBM_ENABLE_FLAG	0x01	/* bit copy of kpm_enable */
1835 #define	KPMTSBM_TLTSBM_FLAG	0x02	/* use TL tsbmiss handler */
1836 #define	KPMTSBM_TSBPHYS_FLAG	0x04	/* use ASI_MEM for TSB update */
1837 
1838 /*
1839  * The TSB
1840  * All TSB sizes supported by the hardware are now supported (8K - 1M).
1841  * For kernel TSBs we may go beyond the hardware supported sizes and support
1842  * larger TSBs via software.
1843  * All TTE sizes are supported in the TSB; the manner in which this is
1844  * done is cpu dependent.
1845  */
1846 #define	TSB_MIN_SZCODE		TSB_8K_SZCODE	/* min. supported TSB size */
1847 #define	TSB_MIN_OFFSET_MASK	(TSB_OFFSET_MASK(TSB_MIN_SZCODE))
1848 
1849 #ifdef sun4v
1850 #define	UTSB_MAX_SZCODE		TSB_256M_SZCODE /* max. supported TSB size */
1851 #else /* sun4u */
1852 #define	UTSB_MAX_SZCODE		TSB_1M_SZCODE	/* max. supported TSB size */
1853 #endif /* sun4v */
1854 
1855 #define	UTSB_MAX_OFFSET_MASK	(TSB_OFFSET_MASK(UTSB_MAX_SZCODE))
1856 
1857 #define	TSB_FREEMEM_MIN		0x1000		/* 32 mb */
1858 #define	TSB_FREEMEM_LARGE	0x10000		/* 512 mb */
1859 #define	TSB_8K_SZCODE		0		/* 512 entries */
1860 #define	TSB_16K_SZCODE		1		/* 1k entries */
1861 #define	TSB_32K_SZCODE		2		/* 2k entries */
1862 #define	TSB_64K_SZCODE		3		/* 4k entries */
1863 #define	TSB_128K_SZCODE		4		/* 8k entries */
1864 #define	TSB_256K_SZCODE		5		/* 16k entries */
1865 #define	TSB_512K_SZCODE		6		/* 32k entries */
1866 #define	TSB_1M_SZCODE		7		/* 64k entries */
1867 #define	TSB_2M_SZCODE		8		/* 128k entries */
1868 #define	TSB_4M_SZCODE		9		/* 256k entries */
1869 #define	TSB_8M_SZCODE		10		/* 512k entries */
1870 #define	TSB_16M_SZCODE		11		/* 1M entries */
1871 #define	TSB_32M_SZCODE		12		/* 2M entries */
1872 #define	TSB_64M_SZCODE		13		/* 4M entries */
1873 #define	TSB_128M_SZCODE		14		/* 8M entries */
1874 #define	TSB_256M_SZCODE		15		/* 16M entries */
1875 #define	TSB_ENTRY_SHIFT		4	/* each entry = 128 bits = 16 bytes */
1876 #define	TSB_ENTRY_SIZE		(1 << 4)
1877 #define	TSB_START_SIZE		9
1878 #define	TSB_ENTRIES(tsbsz)	(1 << (TSB_START_SIZE + tsbsz))
1879 #define	TSB_BYTES(tsbsz)	(TSB_ENTRIES(tsbsz) << TSB_ENTRY_SHIFT)
1880 #define	TSB_OFFSET_MASK(tsbsz)	(TSB_ENTRIES(tsbsz) - 1)
1881 #define	TSB_BASEADDR_MASK	((1 << 12) - 1)
1882 
1883 /*
1884  * sun4u platforms
1885  * ---------------
1886  * We now support two user TSBs with one TSB base register.
1887  * Hence the TSB base register is split up as follows:
1888  *
1889  * When only one TSB present:
1890  *   [63  62..42  41..13  12..4  3..0]
1891  *     ^   ^       ^       ^     ^
1892  *     |   |       |       |     |
1893  *     |   |       |       |     |_ TSB size code
1894  *     |   |       |       |
1895  *     |   |       |       |_ Reserved 0
1896  *     |   |       |
1897  *     |   |       |_ TSB VA[41..13]
1898  *     |   |
1899  *     |   |_ VA hole (Spitfire), zeros (Cheetah and beyond)
1900  *     |
1901  *     |_ 0
1902  *
1903  * When second TSB present:
1904  *   [63  62..42  41..33  32..29  28..22  21..13  12..4  3..0]
1905  *     ^   ^       ^       ^       ^       ^       ^     ^
1906  *     |   |       |       |       |       |       |     |
1907  *     |   |       |       |       |       |       |     |_ First TSB size code
1908  *     |   |       |       |       |       |       |
1909  *     |   |       |       |       |       |       |_ Reserved 0
1910  *     |   |       |       |       |       |
1911  *     |   |       |       |       |       |_ First TSB's VA[21..13]
1912  *     |   |       |       |       |
1913  *     |   |       |       |       |_ Reserved for future use
1914  *     |   |       |       |
1915  *     |   |       |       |_ Second TSB's size code
1916  *     |   |       |
1917  *     |   |       |_ Second TSB's VA[21..13]
1918  *     |   |
1919  *     |   |_ VA hole (Spitfire) / ones (Cheetah and beyond)
1920  *     |
1921  *     |_ 1
1922  *
1923  * Note that since we store 21..13 of each TSB's VA, TSBs and their slabs
1924  * may be up to 4M in size.  For now, only hardware supported TSB sizes
1925  * are supported, though the slabs are usually 4M in size.
1926  *
1927  * sun4u platforms that define UTSB_PHYS use physical addressing to access
1928  * the user TSBs at TL>0.  The first user TSB base is in the MMU I/D TSB Base
1929  * registers.  The second TSB base uses a dedicated scratchpad register which
1930  * requires a definition of SCRATCHPAD_UTSBREG2 in mach_sfmmu.h.  The layout for
1931  * both registers is equivalent to sun4v below, except the TSB PA range is
1932  * [46..13] for sun4u.
1933  *
1934  * sun4v platforms
1935  * ---------------
1936  * On sun4v platforms, we use two dedicated scratchpad registers as pseudo
1937  * hardware TSB base registers to hold up to two different user TSBs.
1938  *
1939  * Each register contains TSB's physical base and size code information
1940  * as follows:
1941  *
1942  *   [63..56  55..13  12..4  3..0]
1943  *      ^       ^       ^     ^
1944  *      |       |       |     |
1945  *      |       |       |     |_ TSB size code
1946  *      |       |       |
1947  *      |       |       |_ Reserved 0
1948  *      |       |
1949  *      |       |_ TSB PA[55..13]
1950  *      |
1951  *      |
1952  *      |
1953  *      |_ 0 for valid TSB
1954  *
1955  * Absence of a user TSB (primarily the second user TSB) is indicated by
1956  * storing a negative value in the TSB base register. This allows us to
1957  * check for presence of a user TSB by simply checking bit# 63.
1958  */
1959 #define	TSBREG_MSB_SHIFT	32		/* set upper bits */
1960 #define	TSBREG_MSB_CONST	0xfffff800	/* set bits 63..43 */
1961 #define	TSBREG_FIRTSB_SHIFT	42		/* to clear bits 63:22 */
1962 #define	TSBREG_SECTSB_MKSHIFT	20		/* 21:13 --> 41:33 */
1963 #define	TSBREG_SECTSB_LSHIFT	22		/* to clear bits 63:42 */
1964 #define	TSBREG_SECTSB_RSHIFT	(TSBREG_SECTSB_MKSHIFT + TSBREG_SECTSB_LSHIFT)
1965 						/* sectsb va -> bits 21:13 */
1966 						/* after clearing upper bits */
1967 #define	TSBREG_SECSZ_SHIFT	29		/* to get sectsb szc to 3:0 */
1968 #define	TSBREG_VAMASK_SHIFT	13		/* set up VA mask */
1969 
1970 #define	BIGKTSB_SZ_MASK		0xf
1971 #define	TSB_SOFTSZ_MASK		BIGKTSB_SZ_MASK
1972 #define	MIN_BIGKTSB_SZCODE	9	/* 256k entries */
1973 #define	MAX_BIGKTSB_SZCODE	11	/* 1024k entries */
1974 #define	MAX_BIGKTSB_TTES	(TSB_BYTES(MAX_BIGKTSB_SZCODE) / MMU_PAGESIZE4M)
1975 
1976 #define	TAG_VALO_SHIFT		22		/* tag's va are bits 63-22 */
1977 /*
1978  * sw bits used on tsb_tag - bit masks used only in assembly
1979  * use only a sethi for these fields.
1980  */
1981 #define	TSBTAG_INVALID	0x00008000		/* tsb_tag.tag_invalid */
1982 #define	TSBTAG_LOCKED	0x00004000		/* tsb_tag.tag_locked */
1983 
1984 #ifdef	_ASM
1985 
1986 /*
1987  * Marker to indicate that this instruction will be hot patched at runtime
1988  * to some other value.
1989  * This value must be zero since it fills in the imm bits of the target
1990  * instructions to be patched
1991  */
1992 #define	RUNTIME_PATCH	(0)
1993 
1994 /*
1995  * V9 defines nop instruction as the following, which we use
1996  * at runtime to nullify some instructions we don't want to
1997  * execute in the trap handlers on certain platforms.
1998  */
1999 #define	MAKE_NOP_INSTR(reg)	\
2000 	sethi	%hi(0x1000000), reg
2001 
2002 /*
2003  * This macro constructs a SPARC V9 "jmpl <source reg>, %g0"
2004  * instruction, with the source register specified by the jump_reg_number.
2005  * The jmp opcode [24:19] = 11 1000 and source register is bits [18:14].
2006  * The instruction is returned in reg. The macro is used to patch in a jmpl
2007  * instruction at runtime.
2008  */
2009 #define	MAKE_JMP_INSTR(jump_reg_number, reg, tmp)	\
2010 	sethi	%hi(0x81c00000), reg;			\
2011 	mov	jump_reg_number, tmp;			\
2012 	sll	tmp, 14, tmp;				\
2013 	or	reg, tmp, reg
2014 
2015 /*
2016  * Macro to get hat per-MMU cnum on this CPU.
2017  * sfmmu - In, pass in "sfmmup" from the caller.
2018  * cnum	- Out, return 'cnum' to the caller
2019  * scr	- scratch
2020  */
2021 #define	SFMMU_CPU_CNUM(sfmmu, cnum, scr)				      \
2022 	CPU_ADDR(scr, cnum);	/* scr = load CPU struct addr */	      \
2023 	ld	[scr + CPU_MMU_IDX], cnum;	/* cnum = mmuid */	      \
2024 	add	sfmmu, SFMMU_CTXS, scr;	/* scr = sfmmup->sfmmu_ctxs[] */      \
2025 	sllx    cnum, SFMMU_MMU_CTX_SHIFT, cnum;			      \
2026 	add	scr, cnum, scr;		/* scr = sfmmup->sfmmu_ctxs[id] */    \
2027 	ldx	[scr + SFMMU_MMU_GC_NUM], scr;	/* sfmmu_ctxs[id].gcnum */    \
2028 	sllx    scr, SFMMU_MMU_CNUM_LSHIFT, scr;			      \
2029 	srlx    scr, SFMMU_MMU_CNUM_LSHIFT, cnum;	/* cnum = sfmmu cnum */
2030 
2031 /*
2032  * Macro to get hat gnum & cnum assocaited with sfmmu_ctx[mmuid] entry
2033  * entry - In,  pass in (&sfmmu_ctxs[mmuid] - SFMMU_CTXS) from the caller.
2034  * gnum - Out, return sfmmu gnum
2035  * cnum - Out, return sfmmu cnum
2036  * reg	- scratch
2037  */
2038 #define	SFMMU_MMUID_GNUM_CNUM(entry, gnum, cnum, reg)			     \
2039 	ldx	[entry + SFMMU_CTXS], reg;  /* reg = sfmmu (gnum | cnum) */  \
2040 	srlx	reg, SFMMU_MMU_GNUM_RSHIFT, gnum;    /* gnum = sfmmu gnum */ \
2041 	sllx	reg, SFMMU_MMU_CNUM_LSHIFT, cnum;			     \
2042 	srlx	cnum, SFMMU_MMU_CNUM_LSHIFT, cnum;   /* cnum = sfmmu cnum */
2043 
2044 /*
2045  * Macro to get this CPU's tsbmiss area.
2046  */
2047 #define	CPU_TSBMISS_AREA(tsbmiss, tmp1)					\
2048 	CPU_INDEX(tmp1, tsbmiss);		/* tmp1 = cpu idx */	\
2049 	sethi	%hi(tsbmiss_area), tsbmiss;	/* tsbmiss base ptr */	\
2050 	mulx    tmp1, TSBMISS_SIZE, tmp1;	/* byte offset */	\
2051 	or	tsbmiss, %lo(tsbmiss_area), tsbmiss;			\
2052 	add	tsbmiss, tmp1, tsbmiss		/* tsbmiss area of CPU */
2053 
2054 
2055 /*
2056  * Macro to set kernel context + page size codes in DMMU primary context
2057  * register. It is only necessary for sun4u because sun4v does not need
2058  * page size codes
2059  */
2060 #ifdef sun4v
2061 
2062 #define	SET_KCONTEXTREG(reg0, reg1, reg2, reg3, reg4, label1, label2, label3)
2063 
2064 #else
2065 
2066 #define	SET_KCONTEXTREG(reg0, reg1, reg2, reg3, reg4, label1, label2, label3) \
2067 	sethi	%hi(kcontextreg), reg0;					\
2068 	ldx	[reg0 + %lo(kcontextreg)], reg0;			\
2069 	mov	MMU_PCONTEXT, reg1;					\
2070 	ldxa	[reg1]ASI_MMU_CTX, reg2;				\
2071 	xor	reg0, reg2, reg2;					\
2072 	brz	reg2, label3;						\
2073 	srlx	reg2, CTXREG_NEXT_SHIFT, reg2;				\
2074 	rdpr	%pstate, reg3;		/* disable interrupts */	\
2075 	btst	PSTATE_IE, reg3;					\
2076 /*CSTYLED*/								\
2077 	bnz,a,pt %icc, label1;						\
2078 	wrpr	reg3, PSTATE_IE, %pstate;				\
2079 /*CSTYLED*/								\
2080 label1:;								\
2081 	brz	reg2, label2;	   /* need demap if N_pgsz0/1 change */	\
2082 	sethi	%hi(FLUSH_ADDR), reg4;					\
2083 	mov	DEMAP_ALL_TYPE, reg2;					\
2084 	stxa	%g0, [reg2]ASI_DTLB_DEMAP;				\
2085 	stxa	%g0, [reg2]ASI_ITLB_DEMAP;				\
2086 /*CSTYLED*/								\
2087 label2:;								\
2088 	stxa	reg0, [reg1]ASI_MMU_CTX;				\
2089 	flush	reg4;							\
2090 	btst	PSTATE_IE, reg3;					\
2091 /*CSTYLED*/								\
2092 	bnz,a,pt %icc, label3;						\
2093 	wrpr	%g0, reg3, %pstate;	/* restore interrupt state */	\
2094 label3:;
2095 
2096 #endif
2097 
2098 /*
2099  * Macro to setup arguments with kernel sfmmup context + page size before
2100  * calling sfmmu_setctx_sec()
2101  */
2102 #ifdef sun4v
2103 #define	SET_KAS_CTXSEC_ARGS(sfmmup, arg0, arg1)			\
2104 	set	KCONTEXT, arg0;					\
2105 	set	0, arg1;
2106 #else
2107 #define	SET_KAS_CTXSEC_ARGS(sfmmup, arg0, arg1)			\
2108 	ldub	[sfmmup + SFMMU_CEXT], arg1;			\
2109 	set	KCONTEXT, arg0;					\
2110 	sll	arg1, CTXREG_EXT_SHIFT, arg1;
2111 #endif
2112 
2113 #define	PANIC_IF_INTR_DISABLED_PSTR(pstatereg, label, scr)	       	\
2114 	andcc	pstatereg, PSTATE_IE, %g0;	/* panic if intrs */	\
2115 /*CSTYLED*/								\
2116 	bnz,pt	%icc, label;			/* already disabled */	\
2117 	nop;								\
2118 									\
2119 	sethi	%hi(panicstr), scr;					\
2120 	ldx	[scr + %lo(panicstr)], scr;				\
2121 	tst	scr;							\
2122 /*CSTYLED*/								\
2123 	bnz,pt	%xcc, label;						\
2124 	nop;								\
2125 									\
2126 	save	%sp, -SA(MINFRAME), %sp;				\
2127 	sethi	%hi(sfmmu_panic1), %o0;					\
2128 	call	panic;							\
2129 	or	%o0, %lo(sfmmu_panic1), %o0;				\
2130 /*CSTYLED*/								\
2131 label:
2132 
2133 #define	PANIC_IF_INTR_ENABLED_PSTR(label, scr)				\
2134 	/*								\
2135 	 * The caller must have disabled interrupts.			\
2136 	 * If interrupts are not disabled, panic			\
2137 	 */								\
2138 	rdpr	%pstate, scr;						\
2139 	andcc	scr, PSTATE_IE, %g0;					\
2140 /*CSTYLED*/								\
2141 	bz,pt	%icc, label;						\
2142 	nop;								\
2143 									\
2144 	sethi	%hi(panicstr), scr;					\
2145 	ldx	[scr + %lo(panicstr)], scr;				\
2146 	tst	scr;							\
2147 /*CSTYLED*/								\
2148 	bnz,pt	%xcc, label;						\
2149 	nop;								\
2150 									\
2151 	sethi	%hi(sfmmu_panic6), %o0;					\
2152 	call	panic;							\
2153 	or	%o0, %lo(sfmmu_panic6), %o0;				\
2154 /*CSTYLED*/								\
2155 label:
2156 
2157 #endif	/* _ASM */
2158 
2159 #ifndef _ASM
2160 
2161 #ifdef VAC
2162 /*
2163  * Page coloring
2164  * The p_vcolor field of the page struct (1 byte) is used to store the
2165  * virtual page color.  This provides for 255 colors.  The value zero is
2166  * used to mean the page has no color - never been mapped or somehow
2167  * purified.
2168  */
2169 
2170 #define	PP_GET_VCOLOR(pp)	(((pp)->p_vcolor) - 1)
2171 #define	PP_NEWPAGE(pp)		(!(pp)->p_vcolor)
2172 #define	PP_SET_VCOLOR(pp, color)                                          \
2173 	((pp)->p_vcolor = ((color) + 1))
2174 
2175 /*
2176  * As mentioned p_vcolor == 0 means there is no color for this page.
2177  * But PP_SET_VCOLOR(pp, color) expects 'color' to be real color minus
2178  * one so we define this constant.
2179  */
2180 #define	NO_VCOLOR	(-1)
2181 
2182 #define	addr_to_vcolor(addr) \
2183 	(((uint_t)(uintptr_t)(addr) >> MMU_PAGESHIFT) & vac_colors_mask)
2184 #else	/* VAC */
2185 #define	addr_to_vcolor(addr)	(0)
2186 #endif	/* VAC */
2187 
2188 /*
2189  * The field p_index in the psm page structure is for large pages support.
2190  * P_index is a bit-vector of the different mapping sizes that a given page
2191  * is part of. An hme structure for a large mapping is only added in the
2192  * group leader page (first page). All pages covered by a given large mapping
2193  * have the corrosponding mapping bit set in their p_index field. This allows
2194  * us to only store an explicit hme structure in the leading page which
2195  * simplifies the mapping link list management. Furthermore, it provides us
2196  * a fast mechanism for determining the largest mapping a page is part of. For
2197  * exmaple, a page with a 64K and a 4M mappings has a p_index value of 0x0A.
2198  *
2199  * Implementation note: even though the first bit in p_index is reserved
2200  * for 8K mappings, it is NOT USED by the code and SHOULD NOT be set.
2201  * In addition, the upper four bits of the p_index field are used by the
2202  * code as temporaries
2203  */
2204 
2205 /*
2206  * Defines for psm page struct fields and large page support
2207  */
2208 #define	SFMMU_INDEX_SHIFT		6
2209 #define	SFMMU_INDEX_MASK		((1 << SFMMU_INDEX_SHIFT) - 1)
2210 
2211 /* Return the mapping index */
2212 #define	PP_MAPINDEX(pp)	((pp)->p_index & SFMMU_INDEX_MASK)
2213 
2214 /*
2215  * These macros rely on the following property:
2216  * All pages constituting a large page are covered by a virtually
2217  * contiguous set of page_t's.
2218  */
2219 
2220 /* Return the leader for this mapping size */
2221 #define	PP_GROUPLEADER(pp, sz) \
2222 	(&(pp)[-(int)(pp->p_pagenum & (TTEPAGES(sz)-1))])
2223 
2224 /* Return the root page for this page based on p_szc */
2225 #define	PP_PAGEROOT(pp)	((pp)->p_szc == 0 ? (pp) : \
2226 	PP_GROUPLEADER((pp), (pp)->p_szc))
2227 
2228 #define	PP_PAGENEXT_N(pp, n)	((pp) + (n))
2229 #define	PP_PAGENEXT(pp)		PP_PAGENEXT_N((pp), 1)
2230 
2231 #define	PP_PAGEPREV_N(pp, n)	((pp) - (n))
2232 #define	PP_PAGEPREV(pp)		PP_PAGEPREV_N((pp), 1)
2233 
2234 #define	PP_ISMAPPED_LARGE(pp)	(PP_MAPINDEX(pp) != 0)
2235 
2236 /* Need function to test the page mappping which takes p_index into account */
2237 #define	PP_ISMAPPED(pp)	((pp)->p_mapping || PP_ISMAPPED_LARGE(pp))
2238 
2239 /*
2240  * Don't call this macro with sz equal to zero. 8K mappings SHOULD NOT
2241  * set p_index field.
2242  */
2243 #define	PAGESZ_TO_INDEX(sz)	(1 << (sz))
2244 
2245 
2246 /*
2247  * prototypes for hat assembly routines.  Some of these are
2248  * known to machine dependent VM code.
2249  */
2250 extern uint64_t sfmmu_make_tsbtag(caddr_t);
2251 extern struct tsbe *
2252 		sfmmu_get_tsbe(uint64_t, caddr_t, int, int);
2253 extern void	sfmmu_load_tsbe(struct tsbe *, uint64_t, tte_t *, int);
2254 extern void	sfmmu_unload_tsbe(struct tsbe *, uint64_t, int);
2255 extern void	sfmmu_load_mmustate(sfmmu_t *);
2256 extern void	sfmmu_raise_tsb_exception(uint64_t, uint64_t);
2257 #ifndef sun4v
2258 extern void	sfmmu_itlb_ld_kva(caddr_t, tte_t *);
2259 extern void	sfmmu_dtlb_ld_kva(caddr_t, tte_t *);
2260 #endif /* sun4v */
2261 extern void	sfmmu_copytte(tte_t *, tte_t *);
2262 extern int	sfmmu_modifytte(tte_t *, tte_t *, tte_t *);
2263 extern int	sfmmu_modifytte_try(tte_t *, tte_t *, tte_t *);
2264 extern pfn_t	sfmmu_ttetopfn(tte_t *, caddr_t);
2265 extern void	sfmmu_hblk_hash_rm(struct hmehash_bucket *,
2266 			struct hme_blk *, uint64_t, struct hme_blk *);
2267 extern void	sfmmu_hblk_hash_add(struct hmehash_bucket *, struct hme_blk *,
2268 			uint64_t);
2269 extern uint_t	sfmmu_disable_intrs(void);
2270 extern void	sfmmu_enable_intrs(uint_t);
2271 /*
2272  * functions exported to machine dependent VM code
2273  */
2274 extern void	sfmmu_patch_ktsb(void);
2275 #ifndef UTSB_PHYS
2276 extern void	sfmmu_patch_utsb(void);
2277 #endif /* UTSB_PHYS */
2278 extern pfn_t	sfmmu_vatopfn(caddr_t, sfmmu_t *, tte_t *);
2279 extern void	sfmmu_vatopfn_suspended(caddr_t, sfmmu_t *, tte_t *);
2280 extern pfn_t	sfmmu_kvaszc2pfn(caddr_t, int);
2281 #ifdef	DEBUG
2282 extern void	sfmmu_check_kpfn(pfn_t);
2283 #else
2284 #define		sfmmu_check_kpfn(pfn)	/* disabled */
2285 #endif	/* DEBUG */
2286 extern void	sfmmu_memtte(tte_t *, pfn_t, uint_t, int);
2287 extern void	sfmmu_tteload(struct hat *, tte_t *, caddr_t, page_t *,	uint_t);
2288 extern void	sfmmu_tsbmiss_exception(struct regs *, uintptr_t, uint_t);
2289 extern void	sfmmu_init_tsbs(void);
2290 extern caddr_t  sfmmu_ktsb_alloc(caddr_t);
2291 extern int	sfmmu_getctx_pri(void);
2292 extern int	sfmmu_getctx_sec(void);
2293 extern void	sfmmu_setctx_sec(uint_t);
2294 extern void	sfmmu_inv_tsb(caddr_t, uint_t);
2295 extern void	sfmmu_init_ktsbinfo(void);
2296 extern int	sfmmu_setup_4lp(void);
2297 extern void	sfmmu_patch_mmu_asi(int);
2298 extern void	sfmmu_init_nucleus_hblks(caddr_t, size_t, int, int);
2299 extern void	sfmmu_cache_flushall(void);
2300 extern pgcnt_t  sfmmu_tte_cnt(sfmmu_t *, uint_t);
2301 extern void	*sfmmu_tsb_segkmem_alloc(vmem_t *, size_t, int);
2302 extern void	sfmmu_tsb_segkmem_free(vmem_t *, void *, size_t);
2303 extern void	sfmmu_reprog_pgsz_arr(sfmmu_t *, uint8_t *);
2304 
2305 extern void	hat_kern_setup(void);
2306 extern int	hat_page_relocate(page_t **, page_t **, spgcnt_t *);
2307 extern int	sfmmu_get_ppvcolor(struct page *);
2308 extern int	sfmmu_get_addrvcolor(caddr_t);
2309 extern int	sfmmu_hat_lock_held(sfmmu_t *);
2310 extern int	sfmmu_alloc_ctx(sfmmu_t *, int, struct cpu *, int);
2311 
2312 /*
2313  * Functions exported to xhat_sfmmu.c
2314  */
2315 extern kmutex_t *sfmmu_mlist_enter(page_t *);
2316 extern void	sfmmu_mlist_exit(kmutex_t *);
2317 extern int	sfmmu_mlist_held(struct page *);
2318 extern struct hme_blk *sfmmu_hmetohblk(struct sf_hment *);
2319 
2320 /*
2321  * MMU-specific functions optionally imported from the CPU module
2322  */
2323 #pragma weak mmu_large_pages_disabled
2324 #pragma weak mmu_set_ctx_page_sizes
2325 #pragma weak mmu_check_page_sizes
2326 
2327 extern uint_t mmu_large_pages_disabled(uint_t);
2328 extern void mmu_set_ctx_page_sizes(sfmmu_t *);
2329 extern void mmu_check_page_sizes(sfmmu_t *, uint64_t *);
2330 
2331 extern sfmmu_t 		*ksfmmup;
2332 extern caddr_t		ktsb_base;
2333 extern uint64_t		ktsb_pbase;
2334 extern int		ktsb_sz;
2335 extern int		ktsb_szcode;
2336 extern caddr_t		ktsb4m_base;
2337 extern uint64_t		ktsb4m_pbase;
2338 extern int		ktsb4m_sz;
2339 extern int		ktsb4m_szcode;
2340 extern uint64_t		kpm_tsbbase;
2341 extern int		kpm_tsbsz;
2342 extern int		ktsb_phys;
2343 extern int		enable_bigktsb;
2344 #ifndef sun4v
2345 extern int		utsb_dtlb_ttenum;
2346 extern int		utsb4m_dtlb_ttenum;
2347 #endif /* sun4v */
2348 extern int		uhmehash_num;
2349 extern int		khmehash_num;
2350 extern struct hmehash_bucket *uhme_hash;
2351 extern struct hmehash_bucket *khme_hash;
2352 extern kmutex_t		*mml_table;
2353 extern uint_t		mml_table_sz;
2354 extern uint_t		mml_shift;
2355 extern uint_t		hblk_alloc_dynamic;
2356 extern struct tsbmiss	tsbmiss_area[NCPU];
2357 extern struct kpmtsbm	kpmtsbm_area[NCPU];
2358 
2359 #ifndef sun4v
2360 extern int		dtlb_resv_ttenum;
2361 extern caddr_t		utsb_vabase;
2362 extern caddr_t		utsb4m_vabase;
2363 #endif /* sun4v */
2364 extern vmem_t		*kmem_tsb_default_arena[];
2365 extern int		tsb_lgrp_affinity;
2366 
2367 extern uint_t		disable_large_pages;
2368 extern uint_t		disable_ism_large_pages;
2369 extern uint_t		disable_auto_data_large_pages;
2370 extern uint_t		disable_auto_text_large_pages;
2371 
2372 /* kpm externals */
2373 extern pfn_t		sfmmu_kpm_vatopfn(caddr_t);
2374 extern void		sfmmu_kpm_patch_tlbm(void);
2375 extern void		sfmmu_kpm_patch_tsbm(void);
2376 extern void		sfmmu_patch_shctx(void);
2377 extern void		sfmmu_kpm_load_tsb(caddr_t, tte_t *, int);
2378 extern void		sfmmu_kpm_unload_tsb(caddr_t, int);
2379 extern void		sfmmu_kpm_tsbmtl(short *, uint_t *, int);
2380 extern int		sfmmu_kpm_stsbmtl(char *, uint_t *, int);
2381 extern caddr_t		kpm_vbase;
2382 extern size_t		kpm_size;
2383 extern struct memseg	*memseg_hash[];
2384 extern uint64_t		memseg_phash[];
2385 extern kpm_hlk_t	*kpmp_table;
2386 extern kpm_shlk_t	*kpmp_stable;
2387 extern uint_t		kpmp_table_sz;
2388 extern uint_t		kpmp_stable_sz;
2389 extern uchar_t		kpmp_shift;
2390 
2391 #define	PP_ISMAPPED_KPM(pp)	((pp)->p_kpmref > 0)
2392 
2393 #define	IS_KPM_ALIAS_RANGE(vaddr)					\
2394 	(((vaddr) - kpm_vbase) >> (uintptr_t)kpm_size_shift > 0)
2395 
2396 #endif /* !_ASM */
2397 
2398 /* sfmmu_kpm_tsbmtl flags */
2399 #define	KPMTSBM_STOP		0
2400 #define	KPMTSBM_START		1
2401 
2402 /* kpm_smallpages kp_mapped values */
2403 #define	KPM_MAPPEDS		-1	/* small mapping valid, no conflict */
2404 #define	KPM_MAPPEDSC		1	/* small mapping valid, conflict */
2405 
2406 /* Physical memseg address NULL marker */
2407 #define	MSEG_NULLPTR_PA		-1
2408 
2409 /*
2410  * Memseg hash defines for kpm trap level tsbmiss handler.
2411  * Must be in sync w/ page.h .
2412  */
2413 #define	SFMMU_MEM_HASH_SHIFT		0x9
2414 #define	SFMMU_N_MEM_SLOTS		0x200
2415 #define	SFMMU_MEM_HASH_ENTRY_SHIFT	3
2416 
2417 #ifndef	_ASM
2418 #if (SFMMU_MEM_HASH_SHIFT != MEM_HASH_SHIFT)
2419 #error SFMMU_MEM_HASH_SHIFT != MEM_HASH_SHIFT
2420 #endif
2421 #if (SFMMU_N_MEM_SLOTS != N_MEM_SLOTS)
2422 #error SFMMU_N_MEM_SLOTS != N_MEM_SLOTS
2423 #endif
2424 
2425 /* Physical memseg address NULL marker */
2426 #define	SFMMU_MEMSEG_NULLPTR_PA		-1
2427 
2428 /*
2429  * Check KCONTEXT to be zero, asm parts depend on that assumption.
2430  */
2431 #if (KCONTEXT != 0)
2432 #error KCONTEXT != 0
2433 #endif
2434 #endif	/* !_ASM */
2435 
2436 
2437 #endif /* _KERNEL */
2438 
2439 #ifndef _ASM
2440 /*
2441  * ctx, hmeblk, mlistlock and other stats for sfmmu
2442  */
2443 struct sfmmu_global_stat {
2444 	int		sf_tsb_exceptions;	/* # of tsb exceptions */
2445 	int		sf_tsb_raise_exception;	/* # tsb exc. w/o TLB flush */
2446 
2447 	int		sf_pagefaults;		/* # of pagefaults */
2448 
2449 	int		sf_uhash_searches;	/* # of user hash searches */
2450 	int		sf_uhash_links;		/* # of user hash links */
2451 	int		sf_khash_searches;	/* # of kernel hash searches */
2452 	int		sf_khash_links;		/* # of kernel hash links */
2453 
2454 	int		sf_swapout;		/* # times hat swapped out */
2455 
2456 	int		sf_tsb_alloc;		/* # TSB allocations */
2457 	int		sf_tsb_allocfail;	/* # times TSB alloc fail */
2458 	int		sf_tsb_sectsb_create;	/* # times second TSB added */
2459 
2460 	int		sf_scd_1sttsb_alloc;	/* # SCD 1st TSB allocations */
2461 	int		sf_scd_2ndtsb_alloc;	/* # SCD 2nd TSB allocations */
2462 	int		sf_scd_1sttsb_allocfail; /* # SCD 1st TSB alloc fail */
2463 	int		sf_scd_2ndtsb_allocfail; /* # SCD 2nd TSB alloc fail */
2464 
2465 
2466 	int		sf_tteload8k;		/* calls to sfmmu_tteload */
2467 	int		sf_tteload64k;		/* calls to sfmmu_tteload */
2468 	int		sf_tteload512k;		/* calls to sfmmu_tteload */
2469 	int		sf_tteload4m;		/* calls to sfmmu_tteload */
2470 	int		sf_tteload32m;		/* calls to sfmmu_tteload */
2471 	int		sf_tteload256m;		/* calls to sfmmu_tteload */
2472 
2473 	int		sf_tsb_load8k;		/* # times loaded 8K tsbent */
2474 	int		sf_tsb_load4m;		/* # times loaded 4M tsbent */
2475 
2476 	int		sf_hblk_hit;		/* found hblk during tteload */
2477 	int		sf_hblk8_ncreate;	/* static hblk8's created */
2478 	int		sf_hblk8_nalloc;	/* static hblk8's allocated */
2479 	int		sf_hblk1_ncreate;	/* static hblk1's created */
2480 	int		sf_hblk1_nalloc;	/* static hblk1's allocated */
2481 	int		sf_hblk_slab_cnt;	/* sfmmu8_cache slab creates */
2482 	int		sf_hblk_reserve_cnt;	/* hblk_reserve usage */
2483 	int		sf_hblk_recurse_cnt;	/* hblk_reserve	owner reqs */
2484 	int		sf_hblk_reserve_hit;	/* hblk_reserve hash hits */
2485 	int		sf_get_free_success;	/* reserve list allocs */
2486 	int		sf_get_free_throttle;	/* fails due to throttling */
2487 	int		sf_get_free_fail;	/* fails due to empty list */
2488 	int		sf_put_free_success;	/* reserve list frees */
2489 	int		sf_put_free_fail;	/* fails due to full list */
2490 
2491 	int		sf_pgcolor_conflict;	/* VAC conflict resolution */
2492 	int		sf_uncache_conflict;	/* VAC conflict resolution */
2493 	int		sf_unload_conflict;	/* VAC unload resolution */
2494 	int		sf_ism_uncache;		/* VAC conflict resolution */
2495 	int		sf_ism_recache;		/* VAC conflict resolution */
2496 	int		sf_recache;		/* VAC conflict resolution */
2497 
2498 	int		sf_steal_count;		/* # of hblks stolen */
2499 
2500 	int		sf_pagesync;		/* # of pagesyncs */
2501 	int		sf_clrwrt;		/* # of clear write perms */
2502 	int		sf_pagesync_invalid;	/* pagesync with inv tte */
2503 
2504 	int		sf_kernel_xcalls;	/* # of kernel cross calls */
2505 	int		sf_user_xcalls;		/* # of user cross calls */
2506 
2507 	int		sf_tsb_grow;		/* # of user tsb grows */
2508 	int		sf_tsb_shrink;		/* # of user tsb shrinks */
2509 	int		sf_tsb_resize_failures;	/* # of user tsb resize */
2510 	int		sf_tsb_reloc;		/* # of user tsb relocations */
2511 
2512 	int		sf_user_vtop;		/* # of user vatopfn calls */
2513 
2514 	int		sf_ctx_inv;		/* #times invalidate MMU ctx */
2515 
2516 	int		sf_tlb_reprog_pgsz;	/* # times switch TLB pgsz */
2517 
2518 	int		sf_region_remap_demap;	/* # times shme remap demap */
2519 
2520 	int		sf_create_scd;		/* # times SCD is created */
2521 	int		sf_join_scd;		/* # process joined scd */
2522 	int		sf_leave_scd;		/* # process left scd */
2523 	int		sf_destroy_scd;		/* # times SCD is destroyed */
2524 };
2525 
2526 struct sfmmu_tsbsize_stat {
2527 	int		sf_tsbsz_8k;
2528 	int		sf_tsbsz_16k;
2529 	int		sf_tsbsz_32k;
2530 	int		sf_tsbsz_64k;
2531 	int		sf_tsbsz_128k;
2532 	int		sf_tsbsz_256k;
2533 	int		sf_tsbsz_512k;
2534 	int		sf_tsbsz_1m;
2535 	int		sf_tsbsz_2m;
2536 	int		sf_tsbsz_4m;
2537 	int		sf_tsbsz_8m;
2538 	int		sf_tsbsz_16m;
2539 	int		sf_tsbsz_32m;
2540 	int		sf_tsbsz_64m;
2541 	int		sf_tsbsz_128m;
2542 	int		sf_tsbsz_256m;
2543 };
2544 
2545 struct sfmmu_percpu_stat {
2546 	int	sf_itlb_misses;		/* # of itlb misses */
2547 	int	sf_dtlb_misses;		/* # of dtlb misses */
2548 	int	sf_utsb_misses;		/* # of user tsb misses */
2549 	int	sf_ktsb_misses;		/* # of kernel tsb misses */
2550 	int	sf_tsb_hits;		/* # of tsb hits */
2551 	int	sf_umod_faults;		/* # of mod (prot viol) flts */
2552 	int	sf_kmod_faults;		/* # of mod (prot viol) flts */
2553 };
2554 
2555 #define	SFMMU_STAT(stat)		sfmmu_global_stat.stat++
2556 #define	SFMMU_STAT_ADD(stat, amount)	sfmmu_global_stat.stat += (amount)
2557 #define	SFMMU_STAT_SET(stat, count)	sfmmu_global_stat.stat = (count)
2558 
2559 #define	SFMMU_MMU_STAT(stat)		CPU->cpu_m.cpu_mmu_ctxp->stat++
2560 
2561 #endif /* !_ASM */
2562 
2563 #ifdef	__cplusplus
2564 }
2565 #endif
2566 
2567 #endif	/* _VM_HAT_SFMMU_H */
2568