xref: /illumos-gate/usr/src/uts/i86pc/vm/vm_dep.h (revision 584b574a)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5a985e578Skchow  * Common Development and Distribution License (the "License").
6a985e578Skchow  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22b52a336eSPavel Tatashin  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
23d3b5f563SJohn Levon  * Copyright 2019 Joyent, Inc.
247c478bd9Sstevel@tonic-gate  */
25a3114836SGerry Liu /*
26a3114836SGerry Liu  * Copyright (c) 2010, Intel Corporation.
27a3114836SGerry Liu  * All rights reserved.
28a3114836SGerry Liu  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * UNIX machine dependent virtual memory support.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #ifndef	_VM_DEP_H
357c478bd9Sstevel@tonic-gate #define	_VM_DEP_H
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
397c478bd9Sstevel@tonic-gate extern "C" {
407c478bd9Sstevel@tonic-gate #endif
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <sys/clock.h>
437c478bd9Sstevel@tonic-gate #include <vm/hat_pte.h>
44ae115bc7Smrj #include <sys/param.h>
45ce8eb11aSdp #include <sys/memnode.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
48843e1988Sjohnlev  * WARNING: vm_dep.h is included by files in common.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #define	GETTICK()	tsc_read()
5206fb6a36Sdv /*
5306fb6a36Sdv  * Do not use this function for obtaining clock tick.  This
5406fb6a36Sdv  * is called by callers who do not need to have a guarenteed
5506fb6a36Sdv  * correct tick value.  The proper routine to use is tsc_read().
5606fb6a36Sdv  */
577c478bd9Sstevel@tonic-gate 
58b52a336eSPavel Tatashin extern u_longlong_t	randtick();
59843e1988Sjohnlev extern uint_t page_create_update_flags_x86(uint_t);
607c478bd9Sstevel@tonic-gate 
61843e1988Sjohnlev extern size_t plcnt_sz(size_t);
62843e1988Sjohnlev #define	PLCNT_SZ(ctrs_sz) (ctrs_sz = plcnt_sz(ctrs_sz))
637c478bd9Sstevel@tonic-gate 
64843e1988Sjohnlev extern caddr_t plcnt_init(caddr_t);
65843e1988Sjohnlev #define	PLCNT_INIT(addr) (addr = plcnt_init(addr))
6607ad560dSkchow 
67843e1988Sjohnlev extern void plcnt_inc_dec(page_t *, int, int, long, int);
68843e1988Sjohnlev #define	PLCNT_INCR(pp, mnode, mtype, szc, flags)			\
69843e1988Sjohnlev 	plcnt_inc_dec(pp, mtype, szc, 1l << PAGE_BSZS_SHIFT(szc), flags)
70843e1988Sjohnlev #define	PLCNT_DECR(pp, mnode, mtype, szc, flags)			\
71567fb7baSToomas Soome 	plcnt_inc_dec(pp, mtype, szc, \
72567fb7baSToomas Soome 	(long)(ULONG_MAX << PAGE_BSZS_SHIFT(szc)), flags)
737c478bd9Sstevel@tonic-gate 
74affbd3ccSkchow /*
75843e1988Sjohnlev  * macro to update page list max counts.  no-op on x86.
76affbd3ccSkchow  */
77e21bae1bSkchow #define	PLCNT_XFER_NORELOC(pp)
78e21bae1bSkchow 
79a3114836SGerry Liu /*
80a3114836SGerry Liu  * macro to modify the page list max counts when memory is added to
81a3114836SGerry Liu  * the page lists during startup (add_physmem) or during a DR operation
82a3114836SGerry Liu  * when memory is added (kphysm_add_memory_dynamic) or deleted
83a3114836SGerry Liu  * (kphysm_del_cleanup).
84a3114836SGerry Liu  */
85a3114836SGerry Liu #define	PLCNT_MODIFY_MAX(pfn, cnt)	mtype_modify_max(pfn, cnt)
86a3114836SGerry Liu 
87843e1988Sjohnlev extern int memrange_num(pfn_t);
88843e1988Sjohnlev extern int pfn_2_mtype(pfn_t);
89843e1988Sjohnlev extern int mtype_func(int, int, uint_t);
90843e1988Sjohnlev extern void mtype_modify_max(pfn_t, long);
91843e1988Sjohnlev extern int mnode_pgcnt(int);
92843e1988Sjohnlev extern int mnode_range_cnt(int);
937c478bd9Sstevel@tonic-gate 
9407ad560dSkchow /*
95843e1988Sjohnlev  * candidate counters in vm_pagelist.c are indexed by color and range
9607ad560dSkchow  */
97a3114836SGerry Liu #define	NUM_MEM_RANGES		4		/* memory range types */
98843e1988Sjohnlev #define	MAX_MNODE_MRANGES	NUM_MEM_RANGES
99843e1988Sjohnlev #define	MNODE_RANGE_CNT(mnode)	mnode_range_cnt(mnode)
100843e1988Sjohnlev #define	MNODE_MAX_MRANGE(mnode)	memrange_num(mem_node_config[mnode].physbase)
1017c478bd9Sstevel@tonic-gate 
1025d07b933Sdp /*
103843e1988Sjohnlev  * This was really badly defined, it implicitly uses mnode_maxmrange[]
104843e1988Sjohnlev  * which is a static in vm_pagelist.c
1055d07b933Sdp  */
106843e1988Sjohnlev extern int mtype_2_mrange(int);
1075d07b933Sdp #define	MTYPE_2_MRANGE(mnode, mtype)	\
108843e1988Sjohnlev 	(mnode_maxmrange[mnode] - mtype_2_mrange(mtype))
1095d07b933Sdp 
1107c478bd9Sstevel@tonic-gate /*
111d94ffb28Sjmcp  * Per page size free lists. Allocated dynamically.
1127c478bd9Sstevel@tonic-gate  * dimensions [mtype][mmu_page_sizes][colors]
1137c478bd9Sstevel@tonic-gate  *
1147c478bd9Sstevel@tonic-gate  * mtype specifies a physical memory range with a unique mnode.
1157c478bd9Sstevel@tonic-gate  */
1167c478bd9Sstevel@tonic-gate 
117d94ffb28Sjmcp extern page_t ****page_freelists;
1187c478bd9Sstevel@tonic-gate 
119d94ffb28Sjmcp #define	PAGE_FREELISTS(mnode, szc, color, mtype)		\
120d94ffb28Sjmcp 	(*(page_freelists[mtype][szc] + (color)))
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate  * For now there is only a single size cache list. Allocated dynamically.
1247c478bd9Sstevel@tonic-gate  * dimensions [mtype][colors]
1257c478bd9Sstevel@tonic-gate  *
1267c478bd9Sstevel@tonic-gate  * mtype specifies a physical memory range with a unique mnode.
1277c478bd9Sstevel@tonic-gate  */
1287c478bd9Sstevel@tonic-gate extern page_t ***page_cachelists;
1297c478bd9Sstevel@tonic-gate 
130*584b574aSToomas Soome #define	PAGE_CACHELISTS(mnode, color, mtype)		\
1317c478bd9Sstevel@tonic-gate 	(*(page_cachelists[mtype] + (color)))
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /*
134d94ffb28Sjmcp  * There are mutexes for both the page freelist
1357c478bd9Sstevel@tonic-gate  * and the page cachelist.  We want enough locks to make contention
1367c478bd9Sstevel@tonic-gate  * reasonable, but not too many -- otherwise page_freelist_lock() gets
1377c478bd9Sstevel@tonic-gate  * so expensive that it becomes the bottleneck!
1387c478bd9Sstevel@tonic-gate  */
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate #define	NPC_MUTEX	16
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate extern kmutex_t	*fpc_mutex[NPC_MUTEX];
1437c478bd9Sstevel@tonic-gate extern kmutex_t	*cpc_mutex[NPC_MUTEX];
1447c478bd9Sstevel@tonic-gate 
145d94ffb28Sjmcp extern page_t *page_get_mnode_freelist(int, uint_t, int, uchar_t, uint_t);
1467c478bd9Sstevel@tonic-gate extern page_t *page_get_mnode_cachelist(uint_t, uint_t, int, int);
1477c478bd9Sstevel@tonic-gate 
148ce8eb11aSdp /* mem node iterator is not used on x86 */
149ce8eb11aSdp #define	MEM_NODE_ITERATOR_DECL(it)
150b779d3e0Sdp #define	MEM_NODE_ITERATOR_INIT(pfn, mnode, szc, it)
151ce8eb11aSdp 
152ce8eb11aSdp /*
153ce8eb11aSdp  * interleaved_mnodes mode is never set on x86, therefore,
154ce8eb11aSdp  * simply return the limits of the given mnode, which then
155ce8eb11aSdp  * determines the length of hpm_counters array for the mnode.
156ce8eb11aSdp  */
157*584b574aSToomas Soome #define	HPM_COUNTERS_LIMITS(mnode, physbase, physmax, first)	\
158ce8eb11aSdp 	{							\
159ce8eb11aSdp 		(physbase) = mem_node_config[(mnode)].physbase;	\
160ce8eb11aSdp 		(physmax) = mem_node_config[(mnode)].physmax;	\
161ce8eb11aSdp 		(first) = (mnode);				\
162ce8eb11aSdp 	}
163ce8eb11aSdp 
164ce8eb11aSdp #define	PAGE_CTRS_WRITE_LOCK(mnode)				\
165ce8eb11aSdp 	{							\
166ce8eb11aSdp 		rw_enter(&page_ctrs_rwlock[(mnode)], RW_WRITER);\
167ce8eb11aSdp 		page_freelist_lock(mnode);			\
168ce8eb11aSdp 	}
169ce8eb11aSdp 
170ce8eb11aSdp #define	PAGE_CTRS_WRITE_UNLOCK(mnode)				\
171ce8eb11aSdp 	{							\
172ce8eb11aSdp 		page_freelist_unlock(mnode);			\
173ce8eb11aSdp 		rw_exit(&page_ctrs_rwlock[(mnode)]);		\
174ce8eb11aSdp 	}
175ce8eb11aSdp 
176a3114836SGerry Liu /*
177a3114836SGerry Liu  * macro to call page_ctrs_adjust() when memory is added
178a3114836SGerry Liu  * during a DR operation.
179a3114836SGerry Liu  */
180a3114836SGerry Liu #define	PAGE_CTRS_ADJUST(pfn, cnt, rv) {				       \
181a3114836SGerry Liu 	spgcnt_t _cnt = (spgcnt_t)(cnt);				       \
182a3114836SGerry Liu 	int _mn;							       \
183a3114836SGerry Liu 	pgcnt_t _np;							       \
184a3114836SGerry Liu 	pfn_t _pfn = (pfn);						       \
185a3114836SGerry Liu 	pfn_t _endpfn = _pfn + _cnt;					       \
186*584b574aSToomas Soome 	rv = 0;								       \
187a3114836SGerry Liu 	while (_pfn < _endpfn) {					       \
188a3114836SGerry Liu 		_mn = PFN_2_MEM_NODE(_pfn);				       \
189a3114836SGerry Liu 		_np = MIN(_endpfn, mem_node_config[_mn].physmax + 1) - _pfn;   \
190a3114836SGerry Liu 		_pfn += _np;						       \
191a3114836SGerry Liu 		if ((rv = page_ctrs_adjust(_mn)) != 0)			       \
192a3114836SGerry Liu 			break;						       \
193a3114836SGerry Liu 	}								       \
194a3114836SGerry Liu }
195a3114836SGerry Liu 
196ce8eb11aSdp #define	PAGE_GET_COLOR_SHIFT(szc, nszc)				\
1975d07b933Sdp 	    (hw_page_array[(nszc)].hp_shift - hw_page_array[(szc)].hp_shift)
1985d07b933Sdp 
199ce8eb11aSdp #define	PAGE_CONVERT_COLOR(ncolor, szc, nszc)			\
200ce8eb11aSdp 	    ((ncolor) << PAGE_GET_COLOR_SHIFT((szc), (nszc)))
201ce8eb11aSdp 
202ce8eb11aSdp #define	PFN_2_COLOR(pfn, szc, it)					\
2035d07b933Sdp 	(((pfn) & page_colors_mask) >>			                \
2047c478bd9Sstevel@tonic-gate 	(hw_page_array[szc].hp_shift - hw_page_array[0].hp_shift))
2057c478bd9Sstevel@tonic-gate 
2065d07b933Sdp #define	PNUM_SIZE(szc)							\
2075d07b933Sdp 	(hw_page_array[(szc)].hp_pgcnt)
2085d07b933Sdp #define	PNUM_SHIFT(szc)							\
2095d07b933Sdp 	(hw_page_array[(szc)].hp_shift - hw_page_array[0].hp_shift)
2105d07b933Sdp #define	PAGE_GET_SHIFT(szc)						\
2115d07b933Sdp 	(hw_page_array[(szc)].hp_shift)
2125d07b933Sdp #define	PAGE_GET_PAGECOLORS(szc)					\
2135d07b933Sdp 	(hw_page_array[(szc)].hp_colors)
2145d07b933Sdp 
2155d07b933Sdp /*
2165d07b933Sdp  * This macro calculates the next sequential pfn with the specified
2175d07b933Sdp  * color using color equivalency mask
2185d07b933Sdp  */
219ce8eb11aSdp #define	PAGE_NEXT_PFN_FOR_COLOR(pfn, szc, color, ceq_mask, color_mask, it)    \
2205d07b933Sdp 	{								      \
2215d07b933Sdp 		uint_t	pfn_shift = PAGE_BSZS_SHIFT(szc);                     \
2225d07b933Sdp 		pfn_t	spfn = pfn >> pfn_shift;                              \
2235d07b933Sdp 		pfn_t	stride = (ceq_mask) + 1;                              \
224a7c3ca36Sdp 		ASSERT(((color) & ~(ceq_mask)) == 0);                         \
2255d07b933Sdp 		ASSERT((((ceq_mask) + 1) & (ceq_mask)) == 0);                 \
2265d07b933Sdp 		if (((spfn ^ (color)) & (ceq_mask)) == 0) {                   \
2275d07b933Sdp 			pfn += stride << pfn_shift;                           \
2285d07b933Sdp 		} else {                                                      \
2295d07b933Sdp 			pfn = (spfn & ~(pfn_t)(ceq_mask)) | (color);          \
2305d07b933Sdp 			pfn = (pfn > spfn ? pfn : pfn + stride) << pfn_shift; \
2315d07b933Sdp 		}                                                             \
2325d07b933Sdp 	}
2335d07b933Sdp 
2345d07b933Sdp /* get the color equivalency mask for the next szc */
2355d07b933Sdp #define	PAGE_GET_NSZ_MASK(szc, mask)                                         \
2365d07b933Sdp 	((mask) >> (PAGE_GET_SHIFT((szc) + 1) - PAGE_GET_SHIFT(szc)))
2375d07b933Sdp 
2385d07b933Sdp /* get the color of the next szc */
2395d07b933Sdp #define	PAGE_GET_NSZ_COLOR(szc, color)                                       \
2405d07b933Sdp 	((color) >> (PAGE_GET_SHIFT((szc) + 1) - PAGE_GET_SHIFT(szc)))
2415d07b933Sdp 
2425d07b933Sdp /* Find the bin for the given page if it was of size szc */
243ce8eb11aSdp #define	PP_2_BIN_SZC(pp, szc)	(PFN_2_COLOR(pp->p_pagenum, szc, NULL))
2445d07b933Sdp 
245d94ffb28Sjmcp #define	PP_2_BIN(pp)		(PP_2_BIN_SZC(pp, pp->p_szc))
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate #define	PP_2_MEM_NODE(pp)	(PFN_2_MEM_NODE(pp->p_pagenum))
248843e1988Sjohnlev #define	PP_2_MTYPE(pp)		(pfn_2_mtype(pp->p_pagenum))
2497c478bd9Sstevel@tonic-gate #define	PP_2_SZC(pp)		(pp->p_szc)
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate #define	SZCPAGES(szc)		(1 << PAGE_BSZS_SHIFT(szc))
2527c478bd9Sstevel@tonic-gate #define	PFN_BASE(pfnum, szc)	(pfnum & ~(SZCPAGES(szc) - 1))
2537c478bd9Sstevel@tonic-gate 
254d94ffb28Sjmcp /*
255d94ffb28Sjmcp  * this structure is used for walking free page lists
256d94ffb28Sjmcp  * controls when to split large pages into smaller pages,
257d94ffb28Sjmcp  * and when to coalesce smaller pages into larger pages
258d94ffb28Sjmcp  */
259d94ffb28Sjmcp typedef struct page_list_walker {
260d94ffb28Sjmcp 	uint_t	plw_colors;		/* num of colors for szc */
261d94ffb28Sjmcp 	uint_t  plw_color_mask;		/* colors-1 */
262d94ffb28Sjmcp 	uint_t	plw_bin_step;		/* next bin: 1 or 2 */
263d94ffb28Sjmcp 	uint_t  plw_count;		/* loop count */
264d94ffb28Sjmcp 	uint_t	plw_bin0;		/* starting bin */
265d94ffb28Sjmcp 	uint_t  plw_bin_marker;		/* bin after initial jump */
266d94ffb28Sjmcp 	uint_t  plw_bin_split_prev;	/* last bin we tried to split */
267d94ffb28Sjmcp 	uint_t  plw_do_split;		/* set if OK to split */
268d94ffb28Sjmcp 	uint_t  plw_split_next;		/* next bin to split */
269d94ffb28Sjmcp 	uint_t	plw_ceq_dif;		/* number of different color groups */
270d94ffb28Sjmcp 					/* to check */
271d94ffb28Sjmcp 	uint_t	plw_ceq_mask[MMU_PAGE_SIZES + 1]; /* color equiv mask */
272d94ffb28Sjmcp 	uint_t	plw_bins[MMU_PAGE_SIZES + 1];	/* num of bins */
273d94ffb28Sjmcp } page_list_walker_t;
274d94ffb28Sjmcp 
2755d07b933Sdp void	page_list_walk_init(uchar_t szc, uint_t flags, uint_t bin,
2765d07b933Sdp     int can_split, int use_ceq, page_list_walker_t *plw);
2775d07b933Sdp 
2785d07b933Sdp uint_t	page_list_walk_next_bin(uchar_t szc, uint_t bin,
2795d07b933Sdp     page_list_walker_t *plw);
2805d07b933Sdp 
281affbd3ccSkchow extern struct cpu	cpus[];
282affbd3ccSkchow #define	CPU0		cpus
283affbd3ccSkchow 
284843e1988Sjohnlev extern int mtype_init(vnode_t *, caddr_t, uint_t *, size_t);
285843e1988Sjohnlev #define	MTYPE_INIT(mtype, vp, vaddr, flags, pgsz)		\
286843e1988Sjohnlev 	(mtype = mtype_init(vp, vaddr, &(flags), pgsz))
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate /*
2897c478bd9Sstevel@tonic-gate  * macros to loop through the mtype range (page_get_mnode_{free,cache,any}list,
2907c478bd9Sstevel@tonic-gate  * and page_get_contig_pages)
2917c478bd9Sstevel@tonic-gate  *
2927c478bd9Sstevel@tonic-gate  * MTYPE_START sets the initial mtype. -1 if the mtype range specified does
2937c478bd9Sstevel@tonic-gate  * not contain mnode.
2947c478bd9Sstevel@tonic-gate  *
2957c478bd9Sstevel@tonic-gate  * MTYPE_NEXT sets the next mtype. -1 if there are no more valid
2967c478bd9Sstevel@tonic-gate  * mtype in the range.
2977c478bd9Sstevel@tonic-gate  */
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate #define	MTYPE_START(mnode, mtype, flags)				\
3007c478bd9Sstevel@tonic-gate 	(mtype = mtype_func(mnode, mtype, flags))
3017c478bd9Sstevel@tonic-gate 
302affbd3ccSkchow #define	MTYPE_NEXT(mnode, mtype, flags) {				\
303affbd3ccSkchow 	if (flags & PGI_MT_RANGE) {					\
304affbd3ccSkchow 		mtype = mtype_func(mnode, mtype, flags | PGI_MT_NEXT);	\
305affbd3ccSkchow 	} else {							\
306affbd3ccSkchow 		mtype = -1;						\
307affbd3ccSkchow 	}								\
308affbd3ccSkchow }
3097c478bd9Sstevel@tonic-gate 
310d3b5f563SJohn Levon extern int mtype_pgr_init(int *, page_t *, pgcnt_t);
311d3b5f563SJohn Levon #define	MTYPE_PGR_INIT(mtype, flags, pp, pgcnt)				\
312d3b5f563SJohn Levon 	(mtype = mtype_pgr_init(&flags, pp, pgcnt))
3137c478bd9Sstevel@tonic-gate 
314affbd3ccSkchow #define	MNODE_PGCNT(mnode)		mnode_pgcnt(mnode)
315affbd3ccSkchow 
316843e1988Sjohnlev extern void mnodetype_2_pfn(int, int, pfn_t *, pfn_t *);
3177c478bd9Sstevel@tonic-gate #define	MNODETYPE_2_PFN(mnode, mtype, pfnlo, pfnhi)			\
318843e1988Sjohnlev 	mnodetype_2_pfn(mnode, mtype, &pfnlo, &pfnhi)
3197c478bd9Sstevel@tonic-gate 
320d94ffb28Sjmcp #define	PC_BIN_MUTEX(mnode, bin, flags) ((flags & PG_FREE_LIST) ?	\
321d94ffb28Sjmcp 	&fpc_mutex[(bin) & (NPC_MUTEX - 1)][mnode] :			\
3227c478bd9Sstevel@tonic-gate 	&cpc_mutex[(bin) & (NPC_MUTEX - 1)][mnode])
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate #define	FPC_MUTEX(mnode, i)	(&fpc_mutex[i][mnode])
3257c478bd9Sstevel@tonic-gate #define	CPC_MUTEX(mnode, i)	(&cpc_mutex[i][mnode])
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate #ifdef DEBUG
3287c478bd9Sstevel@tonic-gate #define	CHK_LPG(pp, szc)	chk_lpg(pp, szc)
3297c478bd9Sstevel@tonic-gate extern void	chk_lpg(page_t *, uchar_t);
3307c478bd9Sstevel@tonic-gate #else
3317c478bd9Sstevel@tonic-gate #define	CHK_LPG(pp, szc)
3327c478bd9Sstevel@tonic-gate #endif
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate #define	FULL_REGION_CNT(rg_szc)	\
3357c478bd9Sstevel@tonic-gate 	(LEVEL_SIZE(rg_szc) >> LEVEL_SHIFT(rg_szc - 1))
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate /* Return the leader for this mapping size */
3387c478bd9Sstevel@tonic-gate #define	PP_GROUPLEADER(pp, szc) \
3397c478bd9Sstevel@tonic-gate 	(&(pp)[-(int)((pp)->p_pagenum & (SZCPAGES(szc)-1))])
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate /* Return the root page for this page based on p_szc */
3427c478bd9Sstevel@tonic-gate #define	PP_PAGEROOT(pp) ((pp)->p_szc == 0 ? (pp) : \
3437c478bd9Sstevel@tonic-gate 	PP_GROUPLEADER((pp), (pp)->p_szc))
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate /*
3467c478bd9Sstevel@tonic-gate  * The counter base must be per page_counter element to prevent
3477c478bd9Sstevel@tonic-gate  * races when re-indexing, and the base page size element should
3487c478bd9Sstevel@tonic-gate  * be aligned on a boundary of the given region size.
3497c478bd9Sstevel@tonic-gate  *
3507c478bd9Sstevel@tonic-gate  * We also round up the number of pages spanned by the counters
3517c478bd9Sstevel@tonic-gate  * for a given region to PC_BASE_ALIGN in certain situations to simplify
3527c478bd9Sstevel@tonic-gate  * the coding for some non-performance critical routines.
3537c478bd9Sstevel@tonic-gate  */
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate #define	PC_BASE_ALIGN		((pfn_t)1 << PAGE_BSZS_SHIFT(MMU_PAGE_SIZES-1))
3567c478bd9Sstevel@tonic-gate #define	PC_BASE_ALIGN_MASK	(PC_BASE_ALIGN - 1)
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate /*
3597c478bd9Sstevel@tonic-gate  * cpu/mmu-dependent vm variables
3607c478bd9Sstevel@tonic-gate  */
3617c478bd9Sstevel@tonic-gate extern uint_t mmu_page_sizes;
3627c478bd9Sstevel@tonic-gate extern uint_t mmu_exported_page_sizes;
36302bc52beSkchow /*
36402bc52beSkchow  * page sizes that legacy applications can see via getpagesizes(3c).
36502bc52beSkchow  * Used to prevent legacy applications from inadvertantly using the
36602bc52beSkchow  * 'new' large pagesizes (1g and above).
36702bc52beSkchow  */
36802bc52beSkchow extern uint_t mmu_legacy_page_sizes;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate /* For x86, userszc is the same as the kernel's szc */
3717c478bd9Sstevel@tonic-gate #define	USERSZC_2_SZC(userszc)	(userszc)
3727c478bd9Sstevel@tonic-gate #define	SZC_2_USERSZC(szc)	(szc)
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate /*
3757c478bd9Sstevel@tonic-gate  * for hw_page_map_t, sized to hold the ratio of large page to base
3767c478bd9Sstevel@tonic-gate  * pagesize (1024 max)
3777c478bd9Sstevel@tonic-gate  */
3787c478bd9Sstevel@tonic-gate typedef	short	hpmctr_t;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate /*
3817c478bd9Sstevel@tonic-gate  * get the setsize of the current cpu - assume homogenous for x86
3827c478bd9Sstevel@tonic-gate  */
3837c478bd9Sstevel@tonic-gate extern int	l2cache_sz, l2cache_linesz, l2cache_assoc;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate #define	L2CACHE_ALIGN		l2cache_linesz
386affbd3ccSkchow #define	L2CACHE_ALIGN_MAX	64
3877c478bd9Sstevel@tonic-gate #define	CPUSETSIZE()		\
3887c478bd9Sstevel@tonic-gate 	(l2cache_assoc ? (l2cache_sz / l2cache_assoc) : MMU_PAGESIZE)
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate /*
3917c478bd9Sstevel@tonic-gate  * Return the log2(pagesize(szc) / MMU_PAGESIZE) --- or the shift count
3927c478bd9Sstevel@tonic-gate  * for the number of base pages in this pagesize
3937c478bd9Sstevel@tonic-gate  */
3947c478bd9Sstevel@tonic-gate #define	PAGE_BSZS_SHIFT(szc) (LEVEL_SHIFT(szc) - MMU_PAGESHIFT)
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate /*
3977c478bd9Sstevel@tonic-gate  * Internal PG_ flags.
3987c478bd9Sstevel@tonic-gate  */
3997c478bd9Sstevel@tonic-gate #define	PGI_RELOCONLY	0x010000	/* opposite of PG_NORELOC */
4007c478bd9Sstevel@tonic-gate #define	PGI_NOCAGE	0x020000	/* cage is disabled */
4017c478bd9Sstevel@tonic-gate #define	PGI_PGCPHIPRI	0x040000	/* page_get_contig_page pri alloc */
4027c478bd9Sstevel@tonic-gate #define	PGI_PGCPSZC0	0x080000	/* relocate base pagesize page */
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate /*
4057c478bd9Sstevel@tonic-gate  * PGI range flags - should not overlap PGI flags
4067c478bd9Sstevel@tonic-gate  */
4077c478bd9Sstevel@tonic-gate #define	PGI_MT_RANGE0	0x1000000	/* mtype range to 0 */
408843e1988Sjohnlev #define	PGI_MT_RANGE16M 0x2000000	/* mtype range to 16m */
40907ad560dSkchow #define	PGI_MT_RANGE4G	0x4000000	/* mtype range to 4g */
41007ad560dSkchow #define	PGI_MT_NEXT	0x8000000	/* get next mtype */
41107ad560dSkchow #define	PGI_MT_RANGE	(PGI_MT_RANGE0 | PGI_MT_RANGE16M | PGI_MT_RANGE4G)
4127c478bd9Sstevel@tonic-gate 
413843e1988Sjohnlev 
414ec25b48fSsusans /*
415ec25b48fSsusans  * Maximum and default values for user heap, stack, private and shared
416ec25b48fSsusans  * anonymous memory, and user text and initialized data.
417ec25b48fSsusans  * Used by map_pgsz*() routines.
418ec25b48fSsusans  */
419ec25b48fSsusans extern size_t max_uheap_lpsize;
420ec25b48fSsusans extern size_t default_uheap_lpsize;
421ec25b48fSsusans extern size_t max_ustack_lpsize;
422ec25b48fSsusans extern size_t default_ustack_lpsize;
423ec25b48fSsusans extern size_t max_privmap_lpsize;
424ec25b48fSsusans extern size_t max_uidata_lpsize;
425ec25b48fSsusans extern size_t max_utext_lpsize;
426ec25b48fSsusans extern size_t max_shm_lpsize;
427ec25b48fSsusans extern size_t mcntl0_lpsize;
428ec25b48fSsusans 
429ec25b48fSsusans /*
430ec25b48fSsusans  * Sanity control. Don't use large pages regardless of user
431ec25b48fSsusans  * settings if there's less than priv or shm_lpg_min_physmem memory installed.
432ec25b48fSsusans  * The units for this variable are 8K pages.
433ec25b48fSsusans  */
434ec25b48fSsusans extern pgcnt_t privm_lpg_min_physmem;
435ec25b48fSsusans extern pgcnt_t shm_lpg_min_physmem;
436ec25b48fSsusans 
4377c478bd9Sstevel@tonic-gate /*
4387c478bd9Sstevel@tonic-gate  * hash as and addr to get a bin.
4397c478bd9Sstevel@tonic-gate  */
4407c478bd9Sstevel@tonic-gate 
441d94ffb28Sjmcp #define	AS_2_BIN(as, seg, vp, addr, bin, szc)				    \
4425d07b933Sdp 	bin = (((((uintptr_t)(addr) >> PAGESHIFT) + ((uintptr_t)(as) >> 4)) \
4435d07b933Sdp 	    & page_colors_mask) >>					    \
4445d07b933Sdp 	    (hw_page_array[szc].hp_shift - hw_page_array[0].hp_shift))
4457c478bd9Sstevel@tonic-gate 
446affbd3ccSkchow /*
447affbd3ccSkchow  * cpu private vm data - accessed thru CPU->cpu_vm_data
448affbd3ccSkchow  *	vc_pnum_memseg: tracks last memseg visited in page_numtopp_nolock()
449affbd3ccSkchow  *	vc_pnext_memseg: tracks last memseg visited in page_nextn()
450affbd3ccSkchow  *	vc_kmptr: orignal unaligned kmem pointer for this vm_cpu_data_t
4516061ce8aSkchow  *	vc_kmsize: orignal kmem size for this vm_cpu_data_t
452affbd3ccSkchow  */
453affbd3ccSkchow 
454affbd3ccSkchow typedef struct {
455affbd3ccSkchow 	struct memseg	*vc_pnum_memseg;
456affbd3ccSkchow 	struct memseg	*vc_pnext_memseg;
457affbd3ccSkchow 	void		*vc_kmptr;
4586061ce8aSkchow 	size_t		vc_kmsize;
459affbd3ccSkchow } vm_cpu_data_t;
460affbd3ccSkchow 
461affbd3ccSkchow /* allocation size to ensure vm_cpu_data_t resides in its own cache line */
462affbd3ccSkchow #define	VM_CPU_DATA_PADSIZE						\
463affbd3ccSkchow 	(P2ROUNDUP(sizeof (vm_cpu_data_t), L2CACHE_ALIGN_MAX))
464affbd3ccSkchow 
4657c478bd9Sstevel@tonic-gate /*
4667c478bd9Sstevel@tonic-gate  * When a bin is empty, and we can't satisfy a color request correctly,
4677c478bd9Sstevel@tonic-gate  * we scan.  If we assume that the programs have reasonable spatial
4687c478bd9Sstevel@tonic-gate  * behavior, then it will not be a good idea to use the adjacent color.
4697c478bd9Sstevel@tonic-gate  * Using the adjacent color would result in virtually adjacent addresses
4707c478bd9Sstevel@tonic-gate  * mapping into the same spot in the cache.  So, if we stumble across
4717c478bd9Sstevel@tonic-gate  * an empty bin, skip a bunch before looking.  After the first skip,
4727c478bd9Sstevel@tonic-gate  * then just look one bin at a time so we don't miss our cache on
4737c478bd9Sstevel@tonic-gate  * every look. Be sure to check every bin.  Page_create() will panic
4747c478bd9Sstevel@tonic-gate  * if we miss a page.
4757c478bd9Sstevel@tonic-gate  *
4767c478bd9Sstevel@tonic-gate  * This also explains the `<=' in the for loops in both page_get_freelist()
4777c478bd9Sstevel@tonic-gate  * and page_get_cachelist().  Since we checked the target bin, skipped
4787c478bd9Sstevel@tonic-gate  * a bunch, then continued one a time, we wind up checking the target bin
4797c478bd9Sstevel@tonic-gate  * twice to make sure we get all of them bins.
4807c478bd9Sstevel@tonic-gate  */
4817c478bd9Sstevel@tonic-gate #define	BIN_STEP	19
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate #ifdef VM_STATS
4847c478bd9Sstevel@tonic-gate struct vmm_vmstats_str {
485d94ffb28Sjmcp 	ulong_t pgf_alloc[MMU_PAGE_SIZES];	/* page_get_freelist */
486d94ffb28Sjmcp 	ulong_t pgf_allocok[MMU_PAGE_SIZES];
487d94ffb28Sjmcp 	ulong_t pgf_allocokrem[MMU_PAGE_SIZES];
488d94ffb28Sjmcp 	ulong_t pgf_allocfailed[MMU_PAGE_SIZES];
4897c478bd9Sstevel@tonic-gate 	ulong_t	pgf_allocdeferred;
490d94ffb28Sjmcp 	ulong_t	pgf_allocretry[MMU_PAGE_SIZES];
491affbd3ccSkchow 	ulong_t pgc_alloc;			/* page_get_cachelist */
4927c478bd9Sstevel@tonic-gate 	ulong_t pgc_allocok;
4937c478bd9Sstevel@tonic-gate 	ulong_t pgc_allocokrem;
4947c478bd9Sstevel@tonic-gate 	ulong_t pgc_allocokdeferred;
4957c478bd9Sstevel@tonic-gate 	ulong_t pgc_allocfailed;
496affbd3ccSkchow 	ulong_t	pgcp_alloc[MMU_PAGE_SIZES];	/* page_get_contig_pages */
4977c478bd9Sstevel@tonic-gate 	ulong_t	pgcp_allocfailed[MMU_PAGE_SIZES];
4987c478bd9Sstevel@tonic-gate 	ulong_t	pgcp_allocempty[MMU_PAGE_SIZES];
4997c478bd9Sstevel@tonic-gate 	ulong_t	pgcp_allocok[MMU_PAGE_SIZES];
500affbd3ccSkchow 	ulong_t	ptcp[MMU_PAGE_SIZES];		/* page_trylock_contig_pages */
5017c478bd9Sstevel@tonic-gate 	ulong_t	ptcpfreethresh[MMU_PAGE_SIZES];
5027c478bd9Sstevel@tonic-gate 	ulong_t	ptcpfailexcl[MMU_PAGE_SIZES];
5037c478bd9Sstevel@tonic-gate 	ulong_t	ptcpfailszc[MMU_PAGE_SIZES];
5047c478bd9Sstevel@tonic-gate 	ulong_t	ptcpfailcage[MMU_PAGE_SIZES];
5057c478bd9Sstevel@tonic-gate 	ulong_t	ptcpok[MMU_PAGE_SIZES];
506affbd3ccSkchow 	ulong_t	pgmf_alloc[MMU_PAGE_SIZES];	/* page_get_mnode_freelist */
5077c478bd9Sstevel@tonic-gate 	ulong_t	pgmf_allocfailed[MMU_PAGE_SIZES];
5087c478bd9Sstevel@tonic-gate 	ulong_t	pgmf_allocempty[MMU_PAGE_SIZES];
5097c478bd9Sstevel@tonic-gate 	ulong_t	pgmf_allocok[MMU_PAGE_SIZES];
510affbd3ccSkchow 	ulong_t	pgmc_alloc;			/* page_get_mnode_cachelist */
5117c478bd9Sstevel@tonic-gate 	ulong_t	pgmc_allocfailed;
5127c478bd9Sstevel@tonic-gate 	ulong_t	pgmc_allocempty;
5137c478bd9Sstevel@tonic-gate 	ulong_t	pgmc_allocok;
514affbd3ccSkchow 	ulong_t	pladd_free[MMU_PAGE_SIZES];	/* page_list_add/sub */
515affbd3ccSkchow 	ulong_t	plsub_free[MMU_PAGE_SIZES];
516affbd3ccSkchow 	ulong_t	pladd_cache;
517affbd3ccSkchow 	ulong_t	plsub_cache;
518affbd3ccSkchow 	ulong_t	plsubpages_szcbig;
519affbd3ccSkchow 	ulong_t	plsubpages_szc0;
5205d07b933Sdp 	ulong_t	pfs_req[MMU_PAGE_SIZES];	/* page_freelist_split */
5215d07b933Sdp 	ulong_t	pfs_demote[MMU_PAGE_SIZES];
5225d07b933Sdp 	ulong_t	pfc_coalok[MMU_PAGE_SIZES][MAX_MNODE_MRANGES];
523affbd3ccSkchow 	ulong_t	ppr_reloc[MMU_PAGE_SIZES];	/* page_relocate */
5247c478bd9Sstevel@tonic-gate 	ulong_t ppr_relocnoroot[MMU_PAGE_SIZES];
5257c478bd9Sstevel@tonic-gate 	ulong_t ppr_reloc_replnoroot[MMU_PAGE_SIZES];
5267c478bd9Sstevel@tonic-gate 	ulong_t ppr_relocnolock[MMU_PAGE_SIZES];
5277c478bd9Sstevel@tonic-gate 	ulong_t ppr_relocnomem[MMU_PAGE_SIZES];
5287c478bd9Sstevel@tonic-gate 	ulong_t ppr_relocok[MMU_PAGE_SIZES];
5298b464eb8Smec 	ulong_t ppr_copyfail;
5305d07b933Sdp 	/* page coalesce counter */
5315d07b933Sdp 	ulong_t page_ctrs_coalesce[MMU_PAGE_SIZES][MAX_MNODE_MRANGES];
5325d07b933Sdp 	/* candidates useful */
5335d07b933Sdp 	ulong_t page_ctrs_cands_skip[MMU_PAGE_SIZES][MAX_MNODE_MRANGES];
5345d07b933Sdp 	/* ctrs changed after locking */
5355d07b933Sdp 	ulong_t page_ctrs_changed[MMU_PAGE_SIZES][MAX_MNODE_MRANGES];
5365d07b933Sdp 	/* page_freelist_coalesce failed */
5375d07b933Sdp 	ulong_t page_ctrs_failed[MMU_PAGE_SIZES][MAX_MNODE_MRANGES];
5387c478bd9Sstevel@tonic-gate 	ulong_t page_ctrs_coalesce_all;	/* page coalesce all counter */
5397c478bd9Sstevel@tonic-gate 	ulong_t page_ctrs_cands_skip_all; /* candidates useful for all func */
5407c478bd9Sstevel@tonic-gate 	ulong_t	restrict4gcnt;
54107ad560dSkchow 	ulong_t	unrestrict16mcnt;	/* non-DMA 16m allocs allowed */
542a985e578Skchow 	ulong_t	pgpanicalloc;		/* PG_PANIC allocation */
54378b03d3aSkchow 	ulong_t	pcf_deny[MMU_PAGE_SIZES];	/* page_chk_freelist */
54478b03d3aSkchow 	ulong_t	pcf_allow[MMU_PAGE_SIZES];
5457c478bd9Sstevel@tonic-gate };
5467c478bd9Sstevel@tonic-gate extern struct vmm_vmstats_str vmm_vmstats;
5477c478bd9Sstevel@tonic-gate #endif	/* VM_STATS */
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate extern size_t page_ctrs_sz(void);
5507c478bd9Sstevel@tonic-gate extern caddr_t page_ctrs_alloc(caddr_t);
551affbd3ccSkchow extern void page_ctr_sub(int, int, page_t *, int);
5525d07b933Sdp extern page_t *page_freelist_split(uchar_t,
55319397407SSherry Moore     uint_t, int, int, pfn_t, pfn_t, page_list_walker_t *);
5545d07b933Sdp extern page_t *page_freelist_coalesce(int, uchar_t, uint_t, uint_t, int,
5555d07b933Sdp     pfn_t);
556a3114836SGerry Liu extern void page_freelist_coalesce_all(int);
5577c478bd9Sstevel@tonic-gate extern uint_t page_get_pagecolors(uint_t);
558d2b85481Srscott extern void pfnzero(pfn_t, uint_t, uint_t);
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate #endif
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate #endif	/* _VM_DEP_H */
565