xref: /titanic_50/usr/src/uts/common/vm/page.h (revision 74b5a35d491a5d9569dd85fa6d4037ba66246430)
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 (c) 1986, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
26 /*	  All Rights Reserved  	*/
27 
28 /*
29  * University Copyright- Copyright (c) 1982, 1986, 1988
30  * The Regents of the University of California
31  * All Rights Reserved
32  *
33  * University Acknowledgment- Portions of this document are derived from
34  * software developed by the University of California, Berkeley, and its
35  * contributors.
36  */
37 
38 #ifndef	_VM_PAGE_H
39 #define	_VM_PAGE_H
40 
41 #include <vm/seg.h>
42 
43 #ifdef	__cplusplus
44 extern "C" {
45 #endif
46 
47 #if defined(_KERNEL) || defined(_KMEMUSER)
48 
49 /*
50  * Shared/Exclusive lock.
51  */
52 
53 /*
54  * Types of page locking supported by page_lock & friends.
55  */
56 typedef enum {
57 	SE_SHARED,
58 	SE_EXCL			/* exclusive lock (value == -1) */
59 } se_t;
60 
61 /*
62  * For requesting that page_lock reclaim the page from the free list.
63  */
64 typedef enum {
65 	P_RECLAIM,		/* reclaim page from free list */
66 	P_NO_RECLAIM		/* DON`T reclaim the page	*/
67 } reclaim_t;
68 
69 /*
70  * Callers of page_try_reclaim_lock and page_lock_es can use this flag
71  * to get SE_EXCL access before reader/writers are given access.
72  */
73 #define	SE_EXCL_WANTED	0x02
74 
75 /*
76  * All page_*lock() requests will be denied unless this flag is set in
77  * the 'es' parameter.
78  */
79 #define	SE_RETIRED	0x04
80 
81 #endif	/* _KERNEL | _KMEMUSER */
82 
83 typedef int	selock_t;
84 
85 /*
86  * Define VM_STATS to turn on all sorts of statistic gathering about
87  * the VM layer.  By default, it is only turned on when DEBUG is
88  * also defined.
89  */
90 #ifdef DEBUG
91 #define	VM_STATS
92 #endif	/* DEBUG */
93 
94 #ifdef VM_STATS
95 #define	VM_STAT_ADD(stat)			(stat)++
96 #define	VM_STAT_COND_ADD(cond, stat)		((void) (!(cond) || (stat)++))
97 #else
98 #define	VM_STAT_ADD(stat)
99 #define	VM_STAT_COND_ADD(cond, stat)
100 #endif	/* VM_STATS */
101 
102 #ifdef _KERNEL
103 
104 /*
105  * PAGE_LLOCK_SIZE is 2 * NCPU, but no smaller than 128.
106  * PAGE_LLOCK_SHIFT is log2(PAGE_LLOCK_SIZE).
107  */
108 #if ((2*NCPU_P2) > 128)
109 #define	PAGE_LLOCK_SHIFT	((unsigned)(NCPU_LOG2 + 1))
110 #else
111 #define	PAGE_LLOCK_SHIFT	7U
112 #endif
113 #define	PAGE_LLOCK_SIZE (1 << PAGE_LLOCK_SHIFT)
114 
115 /*
116  * The number of low order 0 bits in the page_t address.
117  */
118 #define	PP_SHIFT		7
119 
120 /*
121  * pp may be the root of a large page, and many low order bits will be 0.
122  * Shift and XOR multiple times to capture the good bits across the range of
123  * possible page sizes.
124  */
125 #define	PAGE_LLOCK_HASH(pp)	\
126 	(((((uintptr_t)(pp) >> PP_SHIFT) ^ \
127 	((uintptr_t)(pp) >> (PAGE_LLOCK_SHIFT + PP_SHIFT))) ^ \
128 	((uintptr_t)(pp) >> ((PAGE_LLOCK_SHIFT * 2) + PP_SHIFT)) ^ \
129 	((uintptr_t)(pp) >> ((PAGE_LLOCK_SHIFT * 3) + PP_SHIFT))) & \
130 	(PAGE_LLOCK_SIZE - 1))
131 
132 #define	page_struct_lock(pp)	\
133 	mutex_enter(&page_llocks[PAGE_LLOCK_HASH(PP_PAGEROOT(pp))].pad_mutex)
134 #define	page_struct_unlock(pp)	\
135 	mutex_exit(&page_llocks[PAGE_LLOCK_HASH(PP_PAGEROOT(pp))].pad_mutex)
136 
137 #endif	/* _KERNEL */
138 
139 #include <sys/t_lock.h>
140 
141 struct as;
142 
143 /*
144  * Each physical page has a page structure, which is used to maintain
145  * these pages as a cache.  A page can be found via a hashed lookup
146  * based on the [vp, offset].  If a page has an [vp, offset] identity,
147  * then it is entered on a doubly linked circular list off the
148  * vnode using the vpnext/vpprev pointers.   If the p_free bit
149  * is on, then the page is also on a doubly linked circular free
150  * list using next/prev pointers.  If the "p_selock" and "p_iolock"
151  * are held, then the page is currently being read in (exclusive p_selock)
152  * or written back (shared p_selock).  In this case, the next/prev pointers
153  * are used to link the pages together for a consecutive i/o request.  If
154  * the page is being brought in from its backing store, then other processes
155  * will wait for the i/o to complete before attaching to the page since it
156  * will have an "exclusive" lock.
157  *
158  * Each page structure has the locks described below along with
159  * the fields they protect:
160  *
161  *	p_selock	This is a per-page shared/exclusive lock that is
162  *			used to implement the logical shared/exclusive
163  *			lock for each page.  The "shared" lock is normally
164  *			used in most cases while the "exclusive" lock is
165  *			required to destroy or retain exclusive access to
166  *			a page (e.g., while reading in pages).  The appropriate
167  *			lock is always held whenever there is any reference
168  *			to a page structure (e.g., during i/o).
169  *			(Note that with the addition of the "writer-lock-wanted"
170  *			semantics (via SE_EWANTED), threads must not acquire
171  *			multiple reader locks or else a deadly embrace will
172  *			occur in the following situation: thread 1 obtains a
173  *			reader lock; next thread 2 fails to get a writer lock
174  *			but specified SE_EWANTED so it will wait by either
175  *			blocking (when using page_lock_es) or spinning while
176  *			retrying (when using page_try_reclaim_lock) until the
177  *			reader lock is released; then thread 1 attempts to
178  *			get another reader lock but is denied due to
179  *			SE_EWANTED being set, and now both threads are in a
180  *			deadly embrace.)
181  *
182  *				p_hash
183  *				p_vnode
184  *				p_offset
185  *
186  *				p_free
187  *				p_age
188  *
189  *	p_iolock	This is a binary semaphore lock that provides
190  *			exclusive access to the i/o list links in each
191  *			page structure.  It is always held while the page
192  *			is on an i/o list (i.e., involved in i/o).  That is,
193  *			even though a page may be only `shared' locked
194  *			while it is doing a write, the following fields may
195  *			change anyway.  Normally, the page must be
196  *			`exclusively' locked to change anything in it.
197  *
198  *				p_next
199  *				p_prev
200  *
201  * The following fields are protected by the global page_llocks[]:
202  *
203  *				p_lckcnt
204  *				p_cowcnt
205  *
206  * The following lists are protected by the global page_freelock:
207  *
208  *				page_cachelist
209  *				page_freelist
210  *
211  * The following, for our purposes, are protected by
212  * the global freemem_lock:
213  *
214  *				freemem
215  *				freemem_wait
216  *				freemem_cv
217  *
218  * The following fields are protected by hat layer lock(s).  When a page
219  * structure is not mapped and is not associated with a vnode (after a call
220  * to page_hashout() for example) the p_nrm field may be modified with out
221  * holding the hat layer lock:
222  *
223  *				p_nrm
224  *				p_mapping
225  *				p_share
226  *
227  * The following field is file system dependent.  How it is used and
228  * the locking strategies applied are up to the individual file system
229  * implementation.
230  *
231  *				p_fsdata
232  *
233  * The page structure is used to represent and control the system's
234  * physical pages.  There is one instance of the structure for each
235  * page that is not permenately allocated.  For example, the pages that
236  * hold the page structures are permanently held by the kernel
237  * and hence do not need page structures to track them.  The array
238  * of page structures is allocated early on in the kernel's life and
239  * is based on the amount of available physical memory.
240  *
241  * Each page structure may simultaneously appear on several linked lists.
242  * The lists are:  hash list, free or in i/o list, and a vnode's page list.
243  * Each type of list is protected by a different group of mutexes as described
244  * below:
245  *
246  * The hash list is used to quickly find a page when the page's vnode and
247  * offset within the vnode are known.  Each page that is hashed is
248  * connected via the `p_hash' field.  The anchor for each hash is in the
249  * array `page_hash'.  An array of mutexes, `ph_mutex', protects the
250  * lists anchored by page_hash[].  To either search or modify a given hash
251  * list, the appropriate mutex in the ph_mutex array must be held.
252  *
253  * The free list contains pages that are `free to be given away'.  For
254  * efficiency reasons, pages on this list are placed in two catagories:
255  * pages that are still associated with a vnode, and pages that are not
256  * associated with a vnode.  Free pages always have their `p_free' bit set,
257  * free pages that are still associated with a vnode also have their
258  * `p_age' bit set.  Pages on the free list are connected via their
259  * `p_next' and `p_prev' fields.  When a page is involved in some sort
260  * of i/o, it is not free and these fields may be used to link associated
261  * pages together.  At the moment, the free list is protected by a
262  * single mutex `page_freelock'.  The list of free pages still associated
263  * with a vnode is anchored by `page_cachelist' while other free pages
264  * are anchored in architecture dependent ways (to handle page coloring etc.).
265  *
266  * Pages associated with a given vnode appear on a list anchored in the
267  * vnode by the `v_pages' field.  They are linked together with
268  * `p_vpnext' and `p_vpprev'.  The field `p_offset' contains a page's
269  * offset within the vnode.  The pages on this list are not kept in
270  * offset order.  These lists, in a manner similar to the hash lists,
271  * are protected by an array of mutexes called `vph_hash'.  Before
272  * searching or modifying this chain the appropriate mutex in the
273  * vph_hash[] array must be held.
274  *
275  * Again, each of the lists that a page can appear on is protected by a
276  * mutex.  Before reading or writing any of the fields comprising the
277  * list, the appropriate lock must be held.  These list locks should only
278  * be held for very short intervals.
279  *
280  * In addition to the list locks, each page structure contains a
281  * shared/exclusive lock that protects various fields within it.
282  * To modify one of these fields, the `p_selock' must be exclusively held.
283  * To read a field with a degree of certainty, the lock must be at least
284  * held shared.
285  *
286  * Removing a page structure from one of the lists requires holding
287  * the appropriate list lock and the page's p_selock.  A page may be
288  * prevented from changing identity, being freed, or otherwise modified
289  * by acquiring p_selock shared.
290  *
291  * To avoid deadlocks, a strict locking protocol must be followed.  Basically
292  * there are two cases:  In the first case, the page structure in question
293  * is known ahead of time (e.g., when the page is to be added or removed
294  * from a list).  In the second case, the page structure is not known and
295  * must be found by searching one of the lists.
296  *
297  * When adding or removing a known page to one of the lists, first the
298  * page must be exclusively locked (since at least one of its fields
299  * will be modified), second the lock protecting the list must be acquired,
300  * third the page inserted or deleted, and finally the list lock dropped.
301  *
302  * The more interesting case occures when the particular page structure
303  * is not known ahead of time.  For example, when a call is made to
304  * page_lookup(), it is not known if a page with the desired (vnode and
305  * offset pair) identity exists.  So the appropriate mutex in ph_mutex is
306  * acquired, the hash list searched, and if the desired page is found
307  * an attempt is made to lock it.  The attempt to acquire p_selock must
308  * not block while the hash list lock is held.  A deadlock could occure
309  * if some other process was trying to remove the page from the list.
310  * The removing process (following the above protocol) would have exclusively
311  * locked the page, and be spinning waiting to acquire the lock protecting
312  * the hash list.  Since the searching process holds the hash list lock
313  * and is waiting to acquire the page lock, a deadlock occurs.
314  *
315  * The proper scheme to follow is: first, lock the appropriate list,
316  * search the list, and if the desired page is found either use
317  * page_trylock() (which will not block) or pass the address of the
318  * list lock to page_lock().  If page_lock() can not acquire the page's
319  * lock, it will drop the list lock before going to sleep.  page_lock()
320  * returns a value to indicate if the list lock was dropped allowing the
321  * calling program to react appropriately (i.e., retry the operation).
322  *
323  * If the list lock was dropped before the attempt at locking the page
324  * was made, checks would have to be made to ensure that the page had
325  * not changed identity before its lock was obtained.  This is because
326  * the interval between dropping the list lock and acquiring the page
327  * lock is indeterminate.
328  *
329  * In addition, when both a hash list lock (ph_mutex[]) and a vnode list
330  * lock (vph_mutex[]) are needed, the hash list lock must be acquired first.
331  * The routine page_hashin() is a good example of this sequence.
332  * This sequence is ASSERTed by checking that the vph_mutex[] is not held
333  * just before each acquisition of one of the mutexs in ph_mutex[].
334  *
335  * So, as a quick summary:
336  *
337  * 	pse_mutex[]'s protect the p_selock and p_cv fields.
338  *
339  * 	p_selock protects the p_free, p_age, p_vnode, p_offset and p_hash,
340  *
341  * 	ph_mutex[]'s protect the page_hash[] array and its chains.
342  *
343  * 	vph_mutex[]'s protect the v_pages field and the vp page chains.
344  *
345  *	First lock the page, then the hash chain, then the vnode chain.  When
346  *	this is not possible `trylocks' must be used.  Sleeping while holding
347  *	any of these mutexes (p_selock is not a mutex) is not allowed.
348  *
349  *
350  *	field		reading		writing		    ordering
351  *	======================================================================
352  *	p_vnode		p_selock(E,S)	p_selock(E)
353  *	p_offset
354  *	p_free
355  *	p_age
356  *	=====================================================================
357  *	p_hash		p_selock(E,S)	p_selock(E) &&	    p_selock, ph_mutex
358  *					ph_mutex[]
359  *	=====================================================================
360  *	p_vpnext	p_selock(E,S)	p_selock(E) &&	    p_selock, vph_mutex
361  *	p_vpprev			vph_mutex[]
362  *	=====================================================================
363  *	When the p_free bit is set:
364  *
365  *	p_next		p_selock(E,S)	p_selock(E) &&	    p_selock,
366  *	p_prev				page_freelock	    page_freelock
367  *
368  *	When the p_free bit is not set:
369  *
370  *	p_next		p_selock(E,S)	p_selock(E) &&	    p_selock, p_iolock
371  *	p_prev				p_iolock
372  *	=====================================================================
373  *	p_selock	pse_mutex[]	pse_mutex[]	    can`t acquire any
374  *	p_cv						    other mutexes or
375  *							    sleep while holding
376  *							    this lock.
377  *	=====================================================================
378  *	p_lckcnt	p_selock(E,S)	p_selock(E)
379  *					    OR
380  *					p_selock(S) &&
381  *					page_llocks[]
382  *	p_cowcnt
383  *	=====================================================================
384  *	p_nrm		hat layer lock	hat layer lock
385  *	p_mapping
386  *	p_pagenum
387  *	=====================================================================
388  *
389  *	where:
390  *		E----> exclusive version of p_selock.
391  *		S----> shared version of p_selock.
392  *
393  *
394  *	Global data structures and variable:
395  *
396  *	field		reading		writing		    ordering
397  *	=====================================================================
398  *	page_hash[]	ph_mutex[]	ph_mutex[]	    can hold this lock
399  *							    before acquiring
400  *							    a vph_mutex or
401  *							    pse_mutex.
402  *	=====================================================================
403  *	vp->v_pages	vph_mutex[]	vph_mutex[]	    can only acquire
404  *							    a pse_mutex while
405  *							    holding this lock.
406  *	=====================================================================
407  *	page_cachelist	page_freelock	page_freelock	    can't acquire any
408  *	page_freelist	page_freelock	page_freelock
409  *	=====================================================================
410  *	freemem		freemem_lock	freemem_lock	    can't acquire any
411  *	freemem_wait					    other mutexes while
412  *	freemem_cv					    holding this mutex.
413  *	=====================================================================
414  *
415  * Page relocation, PG_NORELOC and P_NORELOC.
416  *
417  * Pages may be relocated using the page_relocate() interface. Relocation
418  * involves moving the contents and identity of a page to another, free page.
419  * To relocate a page, the SE_EXCL lock must be obtained. The way to prevent
420  * a page from being relocated is to hold the SE_SHARED lock (the SE_EXCL
421  * lock must not be held indefinitely). If the page is going to be held
422  * SE_SHARED indefinitely, then the PG_NORELOC hint should be passed
423  * to page_create_va so that pages that are prevented from being relocated
424  * can be managed differently by the platform specific layer.
425  *
426  * Pages locked in memory using page_pp_lock (p_lckcnt/p_cowcnt != 0)
427  * are guaranteed to be held in memory, but can still be relocated
428  * providing the SE_EXCL lock can be obtained.
429  *
430  * The P_NORELOC bit in the page_t.p_state field is provided for use by
431  * the platform specific code in managing pages when the PG_NORELOC
432  * hint is used.
433  *
434  * Memory delete and page locking.
435  *
436  * The set of all usable pages is managed using the global page list as
437  * implemented by the memseg structure defined below. When memory is added
438  * or deleted this list changes. Additions to this list guarantee that the
439  * list is never corrupt.  In order to avoid the necessity of an additional
440  * lock to protect against failed accesses to the memseg being deleted and,
441  * more importantly, the page_ts, the memseg structure is never freed and the
442  * page_t virtual address space is remapped to a page (or pages) of
443  * zeros.  If a page_t is manipulated while it is p_selock'd, or if it is
444  * locked indirectly via a hash or freelist lock, it is not possible for
445  * memory delete to collect the page and so that part of the page list is
446  * prevented from being deleted. If the page is referenced outside of one
447  * of these locks, it is possible for the page_t being referenced to be
448  * deleted.  Examples of this are page_t pointers returned by
449  * page_numtopp_nolock, page_first and page_next.  Providing the page_t
450  * is re-checked after taking the p_selock (for p_vnode != NULL), the
451  * remapping to the zero pages will be detected.
452  *
453  *
454  * Page size (p_szc field) and page locking.
455  *
456  * p_szc field of free pages is changed by free list manager under freelist
457  * locks and is of no concern to the rest of VM subsystem.
458  *
459  * p_szc changes of allocated anonymous (swapfs) can only be done only after
460  * exclusively locking all constituent pages and calling hat_pageunload() on
461  * each of them. To prevent p_szc changes of non free anonymous (swapfs) large
462  * pages it's enough to either lock SHARED any of constituent pages or prevent
463  * hat_pageunload() by holding hat level lock that protects mapping lists (this
464  * method is for hat code only)
465  *
466  * To increase (promote) p_szc of allocated non anonymous file system pages
467  * one has to first lock exclusively all involved constituent pages and call
468  * hat_pageunload() on each of them. To prevent p_szc promote it's enough to
469  * either lock SHARED any of constituent pages that will be needed to make a
470  * large page or prevent hat_pageunload() by holding hat level lock that
471  * protects mapping lists (this method is for hat code only).
472  *
473  * To decrease (demote) p_szc of an allocated non anonymous file system large
474  * page one can either use the same method as used for changeing p_szc of
475  * anonymous large pages or if it's not possible to lock all constituent pages
476  * exclusively a different method can be used. In the second method one only
477  * has to exclusively lock one of constituent pages but then one has to
478  * acquire further locks by calling page_szc_lock() and
479  * hat_page_demote(). hat_page_demote() acquires hat level locks and then
480  * demotes the page. This mechanism relies on the fact that any code that
481  * needs to prevent p_szc of a file system large page from changeing either
482  * locks all constituent large pages at least SHARED or locks some pages at
483  * least SHARED and calls page_szc_lock() or uses hat level page locks.
484  * Demotion using this method is implemented by page_demote_vp_pages().
485  * Please see comments in front of page_demote_vp_pages(), hat_page_demote()
486  * and page_szc_lock() for more details.
487  *
488  * Lock order: p_selock, page_szc_lock, ph_mutex/vph_mutex/freelist,
489  * hat level locks.
490  */
491 
492 typedef struct page {
493 	u_offset_t	p_offset;	/* offset into vnode for this page */
494 	struct vnode	*p_vnode;	/* vnode that this page is named by */
495 	selock_t	p_selock;	/* shared/exclusive lock on the page */
496 #if defined(_LP64)
497 	uint_t		p_vpmref;	/* vpm ref - index of the vpmap_t */
498 #endif
499 	struct page	*p_hash;	/* hash by [vnode, offset] */
500 	struct page	*p_vpnext;	/* next page in vnode list */
501 	struct page	*p_vpprev;	/* prev page in vnode list */
502 	struct page	*p_next;	/* next page in free/intrans lists */
503 	struct page	*p_prev;	/* prev page in free/intrans lists */
504 	ushort_t	p_lckcnt;	/* number of locks on page data */
505 	ushort_t	p_cowcnt;	/* number of copy on write lock */
506 	kcondvar_t	p_cv;		/* page struct's condition var */
507 	kcondvar_t	p_io_cv;	/* for iolock */
508 	uchar_t		p_iolock_state;	/* replaces p_iolock */
509 	volatile uchar_t p_szc;		/* page size code */
510 	uchar_t		p_fsdata;	/* file system dependent byte */
511 	uchar_t		p_state;	/* p_free, p_noreloc */
512 	uchar_t		p_nrm;		/* non-cache, ref, mod readonly bits */
513 #if defined(__sparc)
514 	uchar_t		p_vcolor;	/* virtual color */
515 #else
516 	uchar_t		p_embed;	/* x86 - changes p_mapping & p_index */
517 #endif
518 	uchar_t		p_index;	/* MPSS mapping info. Not used on x86 */
519 	uchar_t		p_toxic;	/* page has an unrecoverable error */
520 	void		*p_mapping;	/* hat specific translation info */
521 	pfn_t		p_pagenum;	/* physical page number */
522 
523 	uint_t		p_share;	/* number of translations */
524 #if defined(_LP64)
525 	uint_t		p_sharepad;	/* pad for growing p_share */
526 #endif
527 	uint_t		p_slckcnt;	/* number of softlocks */
528 #if defined(__sparc)
529 	uint_t		p_kpmref;	/* number of kpm mapping sharers */
530 	struct kpme	*p_kpmelist;	/* kpm specific mapping info */
531 #else
532 	/* index of entry in p_map when p_embed is set */
533 	uint_t		p_mlentry;
534 #endif
535 #if defined(_LP64)
536 	kmutex_t	p_ilock;	/* protects p_vpmref */
537 #else
538 	uint64_t	p_msresv_2;	/* page allocation debugging */
539 #endif
540 } page_t;
541 
542 
543 typedef	page_t	devpage_t;
544 #define	devpage	page
545 
546 #define	PAGE_LOCK_MAXIMUM \
547 	((1 << (sizeof (((page_t *)0)->p_lckcnt) * NBBY)) - 1)
548 
549 #define	PAGE_SLOCK_MAXIMUM UINT_MAX
550 
551 /*
552  * Page hash table is a power-of-two in size, externally chained
553  * through the hash field.  PAGE_HASHAVELEN is the average length
554  * desired for this chain, from which the size of the page_hash
555  * table is derived at boot time and stored in the kernel variable
556  * page_hashsz.  In the hash function it is given by PAGE_HASHSZ.
557  *
558  * PAGE_HASH_FUNC returns an index into the page_hash[] array.  This
559  * index is also used to derive the mutex that protects the chain.
560  *
561  * In constructing the hash function, first we dispose of unimportant bits
562  * (page offset from "off" and the low 3 bits of "vp" which are zero for
563  * struct alignment). Then shift and sum the remaining bits a couple times
564  * in order to get as many source bits from the two source values into the
565  * resulting hashed value.  Note that this will perform quickly, since the
566  * shifting/summing are fast register to register operations with no additional
567  * memory references).
568  *
569  * PH_SHIFT_SIZE is the amount to use for the successive shifts in the hash
570  * function below.  The actual value is LOG2(PH_TABLE_SIZE), so that as many
571  * bits as possible will filter thru PAGE_HASH_FUNC() and PAGE_HASH_MUTEX().
572  */
573 #if defined(_LP64)
574 
575 #if NCPU < 4
576 #define	PH_TABLE_SIZE	128
577 #define	PH_SHIFT_SIZE	7
578 #else
579 #define	PH_TABLE_SIZE	(2 * NCPU_P2)
580 #define	PH_SHIFT_SIZE	(NCPU_LOG2 + 1)
581 #endif
582 
583 #else	/* 32 bits */
584 
585 #if NCPU < 4
586 #define	PH_TABLE_SIZE	16
587 #define	PH_SHIFT_SIZE	4
588 #else
589 #define	PH_TABLE_SIZE	128
590 #define	PH_SHIFT_SIZE	7
591 #endif
592 
593 #endif	/* _LP64 */
594 
595 /*
596  *
597  * We take care to get as much randomness as possible from both the vp and
598  * the offset.  Workloads can have few vnodes with many offsets, many vnodes
599  * with few offsets or a moderate mix of both.  This hash should perform
600  * equally well for each of these possibilities and for all types of memory
601  * allocations.
602  *
603  * vnodes representing files are created over a long period of time and
604  * have good variation in the upper vp bits, and the right shifts below
605  * capture these bits.  However, swap vnodes are created quickly in a
606  * narrow vp* range.  Refer to comments at swap_alloc: vnum has exactly
607  * AN_VPSHIFT bits, so the kmem_alloc'd vnode addresses have approximately
608  * AN_VPSHIFT bits of variation above their VNODE_ALIGN low order 0 bits.
609  * Spread swap vnodes widely in the hash table by XOR'ing a term with the
610  * vp bits of variation left shifted to the top of the range.
611  */
612 
613 #define	PAGE_HASHSZ	page_hashsz
614 #define	PAGE_HASHAVELEN		4
615 #define	PAGE_HASH_FUNC(vp, off) \
616 	(((((uintptr_t)(off) >> PAGESHIFT) ^ \
617 		((uintptr_t)(off) >> (PAGESHIFT + PH_SHIFT_SIZE))) ^ \
618 		(((uintptr_t)(vp) >> 3) ^ \
619 		((uintptr_t)(vp) >> (3 + PH_SHIFT_SIZE)) ^ \
620 		((uintptr_t)(vp) >> (3 + 2 * PH_SHIFT_SIZE)) ^ \
621 		((uintptr_t)(vp) << \
622 		(page_hashsz_shift - AN_VPSHIFT - VNODE_ALIGN_LOG2)))) & \
623 		(PAGE_HASHSZ - 1))
624 #ifdef _KERNEL
625 
626 /*
627  * The page hash value is re-hashed to an index for the ph_mutex array.
628  *
629  * For 64 bit kernels, the mutex array is padded out to prevent false
630  * sharing of cache sub-blocks (64 bytes) of adjacent mutexes.
631  *
632  * For 32 bit kernels, we don't want to waste kernel address space with
633  * padding, so instead we rely on the hash function to introduce skew of
634  * adjacent vnode/offset indexes (the left shift part of the hash function).
635  * Since sizeof (kmutex_t) is 8, we shift an additional 3 to skew to a different
636  * 64 byte sub-block.
637  */
638 extern pad_mutex_t ph_mutex[];
639 
640 #define	PAGE_HASH_MUTEX(x) \
641 	&(ph_mutex[((x) ^ ((x) >> PH_SHIFT_SIZE) + ((x) << 3)) & \
642 		(PH_TABLE_SIZE - 1)].pad_mutex)
643 
644 /*
645  * Flags used while creating pages.
646  */
647 #define	PG_EXCL		0x0001
648 #define	PG_WAIT		0x0002		/* Blocking memory allocations */
649 #define	PG_PHYSCONTIG	0x0004		/* NOT SUPPORTED */
650 #define	PG_MATCH_COLOR	0x0008		/* SUPPORTED by free list routines */
651 #define	PG_NORELOC	0x0010		/* Non-relocatable alloc hint. */
652 					/* Page must be PP_ISNORELOC */
653 #define	PG_PANIC	0x0020		/* system will panic if alloc fails */
654 #define	PG_PUSHPAGE	0x0040		/* alloc may use reserve */
655 #define	PG_LOCAL	0x0080		/* alloc from given lgrp only */
656 #define	PG_NORMALPRI	0x0100		/* PG_WAIT like priority, but */
657 					/* non-blocking */
658 /*
659  * When p_selock has the SE_EWANTED bit set, threads waiting for SE_EXCL
660  * access are given priority over all other waiting threads.
661  */
662 #define	SE_EWANTED	0x40000000
663 #define	PAGE_LOCKED(pp)		(((pp)->p_selock & ~SE_EWANTED) != 0)
664 #define	PAGE_SHARED(pp)		(((pp)->p_selock & ~SE_EWANTED) > 0)
665 #define	PAGE_EXCL(pp)		((pp)->p_selock < 0)
666 #define	PAGE_LOCKED_SE(pp, se)	\
667 	((se) == SE_EXCL ? PAGE_EXCL(pp) : PAGE_SHARED(pp))
668 
669 extern	long page_hashsz;
670 extern	unsigned int page_hashsz_shift;
671 extern	page_t **page_hash;
672 
673 extern	pad_mutex_t page_llocks[];	/* page logical lock mutex */
674 extern	kmutex_t freemem_lock;		/* freemem lock */
675 
676 extern	pgcnt_t	total_pages;		/* total pages in the system */
677 
678 /*
679  * Variables controlling locking of physical memory.
680  */
681 extern	pgcnt_t	pages_pp_maximum;	/* tuning: lock + claim <= max */
682 extern	void init_pages_pp_maximum(void);
683 
684 struct lgrp;
685 
686 /* page_list_{add,sub} flags */
687 
688 /* which list */
689 #define	PG_FREE_LIST	0x0001
690 #define	PG_CACHE_LIST	0x0002
691 
692 /* where on list */
693 #define	PG_LIST_TAIL	0x0010
694 #define	PG_LIST_HEAD	0x0020
695 
696 /* called from */
697 #define	PG_LIST_ISINIT	0x1000
698 
699 /*
700  * Page frame operations.
701  */
702 page_t	*page_lookup(struct vnode *, u_offset_t, se_t);
703 page_t	*page_lookup_create(struct vnode *, u_offset_t, se_t, page_t *,
704 	spgcnt_t *, int);
705 page_t	*page_lookup_nowait(struct vnode *, u_offset_t, se_t);
706 page_t	*page_find(struct vnode *, u_offset_t);
707 page_t	*page_exists(struct vnode *, u_offset_t);
708 int	page_exists_physcontig(vnode_t *, u_offset_t, uint_t, page_t *[]);
709 int	page_exists_forreal(struct vnode *, u_offset_t, uint_t *);
710 void	page_needfree(spgcnt_t);
711 page_t	*page_create(struct vnode *, u_offset_t, size_t, uint_t);
712 int	page_alloc_pages(struct vnode *, struct seg *, caddr_t, page_t **,
713 	page_t **, uint_t, int, int);
714 page_t  *page_create_va_large(vnode_t *vp, u_offset_t off, size_t bytes,
715 	uint_t flags, struct seg *seg, caddr_t vaddr, void *arg);
716 page_t	*page_create_va(struct vnode *, u_offset_t, size_t, uint_t,
717 	struct seg *, caddr_t);
718 int	page_create_wait(pgcnt_t npages, uint_t flags);
719 void    page_create_putback(spgcnt_t npages);
720 void	page_free(page_t *, int);
721 void	page_free_at_startup(page_t *);
722 void	page_free_pages(page_t *);
723 void	free_vp_pages(struct vnode *, u_offset_t, size_t);
724 int	page_reclaim(page_t *, kmutex_t *);
725 int	page_reclaim_pages(page_t *, kmutex_t *, uint_t);
726 void	page_destroy(page_t *, int);
727 void	page_destroy_pages(page_t *);
728 void	page_destroy_free(page_t *);
729 void	page_rename(page_t *, struct vnode *, u_offset_t);
730 int	page_hashin(page_t *, struct vnode *, u_offset_t, kmutex_t *);
731 void	page_hashout(page_t *, kmutex_t *);
732 int	page_num_hashin(pfn_t, struct vnode *, u_offset_t);
733 void	page_add(page_t **, page_t *);
734 void	page_add_common(page_t **, page_t *);
735 void	page_sub(page_t **, page_t *);
736 void	page_sub_common(page_t **, page_t *);
737 page_t	*page_get_freelist(struct vnode *, u_offset_t, struct seg *,
738 		caddr_t, size_t, uint_t, struct lgrp *);
739 
740 page_t	*page_get_cachelist(struct vnode *, u_offset_t, struct seg *,
741 		caddr_t, uint_t, struct lgrp *);
742 #if defined(__i386) || defined(__amd64)
743 int	page_chk_freelist(uint_t);
744 #endif
745 void	page_list_add(page_t *, int);
746 void	page_boot_demote(page_t *);
747 void	page_promote_size(page_t *, uint_t);
748 void	page_list_add_pages(page_t *, int);
749 void	page_list_sub(page_t *, int);
750 void	page_list_sub_pages(page_t *, uint_t);
751 void	page_list_xfer(page_t *, int, int);
752 void	page_list_break(page_t **, page_t **, size_t);
753 void	page_list_concat(page_t **, page_t **);
754 void	page_vpadd(page_t **, page_t *);
755 void	page_vpsub(page_t **, page_t *);
756 int	page_lock(page_t *, se_t, kmutex_t *, reclaim_t);
757 int	page_lock_es(page_t *, se_t, kmutex_t *, reclaim_t, int);
758 void page_lock_clr_exclwanted(page_t *);
759 int	page_trylock(page_t *, se_t);
760 int	page_try_reclaim_lock(page_t *, se_t, int);
761 int	page_tryupgrade(page_t *);
762 void	page_downgrade(page_t *);
763 void	page_unlock(page_t *);
764 void	page_unlock_nocapture(page_t *);
765 void	page_lock_delete(page_t *);
766 int	page_deleted(page_t *);
767 int	page_pp_lock(page_t *, int, int);
768 void	page_pp_unlock(page_t *, int, int);
769 int	page_resv(pgcnt_t, uint_t);
770 void	page_unresv(pgcnt_t);
771 void	page_pp_useclaim(page_t *, page_t *, uint_t);
772 int	page_addclaim(page_t *);
773 int	page_subclaim(page_t *);
774 int	page_addclaim_pages(page_t **);
775 int	page_subclaim_pages(page_t **);
776 pfn_t	page_pptonum(page_t *);
777 page_t	*page_numtopp(pfn_t, se_t);
778 page_t	*page_numtopp_noreclaim(pfn_t, se_t);
779 page_t	*page_numtopp_nolock(pfn_t);
780 page_t	*page_numtopp_nowait(pfn_t, se_t);
781 page_t  *page_first();
782 page_t  *page_next(page_t *);
783 page_t  *page_list_next(page_t *);
784 page_t	*page_nextn(page_t *, ulong_t);
785 page_t	*page_next_scan_init(void **);
786 page_t	*page_next_scan_large(page_t *, ulong_t *, void **);
787 void    prefetch_page_r(void *);
788 int	ppcopy(page_t *, page_t *);
789 void	page_relocate_hash(page_t *, page_t *);
790 void	pagezero(page_t *, uint_t, uint_t);
791 void	pagescrub(page_t *, uint_t, uint_t);
792 void	page_io_lock(page_t *);
793 void	page_io_unlock(page_t *);
794 int	page_io_trylock(page_t *);
795 int	page_iolock_assert(page_t *);
796 void	page_iolock_init(page_t *);
797 void	page_io_wait(page_t *);
798 int	page_io_locked(page_t *);
799 pgcnt_t	page_busy(int);
800 void	page_lock_init(void);
801 ulong_t	page_share_cnt(page_t *);
802 int	page_isshared(page_t *);
803 int	page_isfree(page_t *);
804 int	page_isref(page_t *);
805 int	page_ismod(page_t *);
806 int	page_release(page_t *, int);
807 void	page_retire_init(void);
808 int	page_retire(uint64_t, uchar_t);
809 int	page_retire_check(uint64_t, uint64_t *);
810 int	page_unretire(uint64_t);
811 int	page_unretire_pp(page_t *, int);
812 void	page_tryretire(page_t *);
813 void	page_retire_mdboot();
814 uint64_t	page_retire_pend_count(void);
815 uint64_t	page_retire_pend_kas_count(void);
816 void	page_retire_incr_pend_count(void *);
817 void	page_retire_decr_pend_count(void *);
818 void	page_clrtoxic(page_t *, uchar_t);
819 void	page_settoxic(page_t *, uchar_t);
820 
821 int	page_mem_avail(pgcnt_t);
822 int	page_reclaim_mem(pgcnt_t, pgcnt_t, int);
823 
824 void page_set_props(page_t *, uint_t);
825 void page_clr_all_props(page_t *);
826 int page_clear_lck_cow(page_t *, int);
827 
828 kmutex_t	*page_vnode_mutex(struct vnode *);
829 kmutex_t	*page_se_mutex(struct page *);
830 kmutex_t	*page_szc_lock(struct page *);
831 int		page_szc_lock_assert(struct page *pp);
832 
833 /*
834  * Page relocation interfaces. page_relocate() is generic.
835  * page_get_replacement_page() is provided by the PSM.
836  * page_free_replacement_page() is generic.
837  */
838 int group_page_trylock(page_t *, se_t);
839 void group_page_unlock(page_t *);
840 int page_relocate(page_t **, page_t **, int, int, spgcnt_t *, struct lgrp *);
841 int do_page_relocate(page_t **, page_t **, int, spgcnt_t *, struct lgrp *);
842 page_t *page_get_replacement_page(page_t *, struct lgrp *, uint_t);
843 void page_free_replacement_page(page_t *);
844 int page_relocate_cage(page_t **, page_t **);
845 
846 int page_try_demote_pages(page_t *);
847 int page_try_demote_free_pages(page_t *);
848 void page_demote_free_pages(page_t *);
849 
850 struct anon_map;
851 
852 void page_mark_migrate(struct seg *, caddr_t, size_t, struct anon_map *,
853     ulong_t, vnode_t *, u_offset_t, int);
854 void page_migrate(struct seg *, caddr_t, page_t **, pgcnt_t);
855 
856 /*
857  * Tell the PIM we are adding physical memory
858  */
859 void add_physmem(page_t *, size_t, pfn_t);
860 void add_physmem_cb(page_t *, pfn_t);	/* callback for page_t part */
861 
862 /*
863  * hw_page_array[] is configured with hardware supported page sizes by
864  * platform specific code.
865  */
866 typedef struct {
867 	size_t	hp_size;
868 	uint_t	hp_shift;
869 	uint_t  hp_colors;
870 	pgcnt_t	hp_pgcnt;	/* base pagesize cnt */
871 } hw_pagesize_t;
872 
873 extern hw_pagesize_t	hw_page_array[];
874 extern uint_t		page_coloring_shift;
875 extern uint_t		page_colors_mask;
876 extern int		cpu_page_colors;
877 extern uint_t		colorequiv;
878 extern uchar_t		colorequivszc[];
879 
880 uint_t	page_num_pagesizes(void);
881 uint_t	page_num_user_pagesizes(int);
882 size_t	page_get_pagesize(uint_t);
883 size_t	page_get_user_pagesize(uint_t n);
884 pgcnt_t	page_get_pagecnt(uint_t);
885 uint_t	page_get_shift(uint_t);
886 int	page_szc(size_t);
887 int	page_szc_user_filtered(size_t);
888 
889 /* page_get_replacement page flags */
890 #define	PGR_SAMESZC	0x1	/* only look for page size same as orig */
891 #define	PGR_NORELOC	0x2	/* allocate a P_NORELOC page */
892 
893 /*
894  * macros for "masked arithmetic"
895  * The purpose is to step through all combinations of a set of bits while
896  * keeping some other bits fixed. Fixed bits need not be contiguous. The
897  * variable bits need not be contiguous either, or even right aligned. The
898  * trick is to set all fixed bits to 1, then increment, then restore the
899  * fixed bits. If incrementing causes a carry from a low bit position, the
900  * carry propagates thru the fixed bits, because they are temporarily set to 1.
901  *	v is the value
902  *	i is the increment
903  *	eq_mask defines the fixed bits
904  *	mask limits the size of the result
905  */
906 #define	ADD_MASKED(v, i, eq_mask, mask) \
907 	(((((v) | (eq_mask)) + (i)) & (mask) & ~(eq_mask)) | ((v) & (eq_mask)))
908 
909 /*
910  * convenience macro which increments by 1
911  */
912 #define	INC_MASKED(v, eq_mask, mask) ADD_MASKED(v, 1, eq_mask, mask)
913 
914 #endif	/* _KERNEL */
915 
916 /*
917  * Constants used for the p_iolock_state
918  */
919 #define	PAGE_IO_INUSE	0x1
920 #define	PAGE_IO_WANTED	0x2
921 
922 /*
923  * Constants used for page_release status
924  */
925 #define	PGREL_NOTREL    0x1
926 #define	PGREL_CLEAN	0x2
927 #define	PGREL_MOD	0x3
928 
929 /*
930  * The p_state field holds what used to be the p_age and p_free
931  * bits.  These fields are protected by p_selock (see above).
932  */
933 #define	P_FREE		0x80		/* Page on free list */
934 #define	P_NORELOC	0x40		/* Page is non-relocatable */
935 #define	P_MIGRATE	0x20		/* Migrate page on next touch */
936 #define	P_SWAP		0x10		/* belongs to vnode that is V_ISSWAP */
937 #define	P_BOOTPAGES	0x08		/* member of bootpages list */
938 #define	P_RAF		0x04		/* page retired at free */
939 
940 #define	PP_ISFREE(pp)		((pp)->p_state & P_FREE)
941 #define	PP_ISAGED(pp)		(((pp)->p_state & P_FREE) && \
942 					((pp)->p_vnode == NULL))
943 #define	PP_ISNORELOC(pp)	((pp)->p_state & P_NORELOC)
944 #define	PP_ISKAS(pp)		(VN_ISKAS((pp)->p_vnode))
945 #define	PP_ISNORELOCKERNEL(pp)	(PP_ISNORELOC(pp) && PP_ISKAS(pp))
946 #define	PP_ISMIGRATE(pp)	((pp)->p_state & P_MIGRATE)
947 #define	PP_ISSWAP(pp)		((pp)->p_state & P_SWAP)
948 #define	PP_ISBOOTPAGES(pp)	((pp)->p_state & P_BOOTPAGES)
949 #define	PP_ISRAF(pp)		((pp)->p_state & P_RAF)
950 
951 #define	PP_SETFREE(pp)		((pp)->p_state = ((pp)->p_state & ~P_MIGRATE) \
952 				| P_FREE)
953 #define	PP_SETAGED(pp)		ASSERT(PP_ISAGED(pp))
954 #define	PP_SETNORELOC(pp)	((pp)->p_state |= P_NORELOC)
955 #define	PP_SETMIGRATE(pp)	((pp)->p_state |= P_MIGRATE)
956 #define	PP_SETSWAP(pp)		((pp)->p_state |= P_SWAP)
957 #define	PP_SETBOOTPAGES(pp)	((pp)->p_state |= P_BOOTPAGES)
958 #define	PP_SETRAF(pp)		((pp)->p_state |= P_RAF)
959 
960 #define	PP_CLRFREE(pp)		((pp)->p_state &= ~P_FREE)
961 #define	PP_CLRAGED(pp)		ASSERT(!PP_ISAGED(pp))
962 #define	PP_CLRNORELOC(pp)	((pp)->p_state &= ~P_NORELOC)
963 #define	PP_CLRMIGRATE(pp)	((pp)->p_state &= ~P_MIGRATE)
964 #define	PP_CLRSWAP(pp)		((pp)->p_state &= ~P_SWAP)
965 #define	PP_CLRBOOTPAGES(pp)	((pp)->p_state &= ~P_BOOTPAGES)
966 #define	PP_CLRRAF(pp)		((pp)->p_state &= ~P_RAF)
967 
968 /*
969  * Flags for page_t p_toxic, for tracking memory hardware errors.
970  *
971  * These flags are OR'ed into p_toxic with page_settoxic() to track which
972  * error(s) have occurred on a given page. The flags are cleared with
973  * page_clrtoxic(). Both page_settoxic() and page_cleartoxic use atomic
974  * primitives to manipulate the p_toxic field so no other locking is needed.
975  *
976  * When an error occurs on a page, p_toxic is set to record the error. The
977  * error could be a memory error or something else (i.e. a datapath). The Page
978  * Retire mechanism does not try to determine the exact cause of the error;
979  * Page Retire rightly leaves that sort of determination to FMA's Diagnostic
980  * Engine (DE).
981  *
982  * Note that, while p_toxic bits can be set without holding any locks, they
983  * should only be cleared while holding the page exclusively locked.
984  * There is one exception to this, the PR_CAPTURE bit is protected by a mutex
985  * within the page capture logic and thus to set or clear the bit, that mutex
986  * needs to be held.  The page does not need to be locked but the page_clrtoxic
987  * function must be used as we need an atomic operation.
988  * Also note that there is what amounts to a hack to prevent recursion with
989  * large pages such that if we are unlocking a page and the PR_CAPTURE bit is
990  * set, we will only try to capture the page if the current threads T_CAPTURING
991  * flag is not set.  If the flag is set, the unlock will not try to capture
992  * the page even though the PR_CAPTURE bit is set.
993  *
994  * Pages with PR_UE or PR_FMA flags are retired unconditionally, while pages
995  * with PR_MCE are retired if the system has not retired too many of them.
996  *
997  * A page must be exclusively locked to be retired. Pages can be retired if
998  * they are mapped, modified, or both, as long as they are not marked PR_UE,
999  * since pages with uncorrectable errors cannot be relocated in memory.
1000  * Once a page has been successfully retired it is zeroed, attached to the
1001  * retired_pages vnode and, finally, PR_RETIRED is set in p_toxic. The other
1002  * p_toxic bits are NOT cleared. Pages are not left locked after retiring them
1003  * to avoid special case code throughout the kernel; rather, page_*lock() will
1004  * fail to lock the page, unless SE_RETIRED is passed as an argument.
1005  *
1006  * While we have your attention, go take a look at the comments at the
1007  * beginning of page_retire.c too.
1008  */
1009 #define	PR_OK		0x00	/* no problem */
1010 #define	PR_MCE		0x01	/* page has seen two or more CEs */
1011 #define	PR_UE		0x02	/* page has an unhandled UE */
1012 #define	PR_UE_SCRUBBED	0x04	/* page has seen a UE but was cleaned */
1013 #define	PR_FMA		0x08	/* A DE wants this page retired */
1014 #define	PR_CAPTURE	0x10	/* page is hashed on page_capture_hash[] */
1015 #define	PR_RESV		0x20	/* Reserved for future use */
1016 #define	PR_MSG		0x40	/* message(s) already printed for this page */
1017 #define	PR_RETIRED	0x80	/* This page has been retired */
1018 
1019 #define	PR_REASONS	(PR_UE | PR_MCE | PR_FMA)
1020 #define	PR_TOXIC	(PR_UE)
1021 #define	PR_ERRMASK	(PR_UE | PR_UE_SCRUBBED | PR_MCE | PR_FMA)
1022 #define	PR_TOXICFLAGS	(0xCF)
1023 
1024 #define	PP_RETIRED(pp)	((pp)->p_toxic & PR_RETIRED)
1025 #define	PP_TOXIC(pp)	((pp)->p_toxic & PR_TOXIC)
1026 #define	PP_PR_REQ(pp)	(((pp)->p_toxic & PR_REASONS) && !PP_RETIRED(pp))
1027 #define	PP_PR_NOSHARE(pp)						\
1028 	((((pp)->p_toxic & (PR_RETIRED | PR_FMA | PR_UE)) == PR_FMA) &&	\
1029 	!PP_ISKAS(pp))
1030 
1031 /*
1032  * Flags for page_unretire_pp
1033  */
1034 #define	PR_UNR_FREE	0x1
1035 #define	PR_UNR_CLEAN	0x2
1036 #define	PR_UNR_TEMP	0x4
1037 
1038 /*
1039  * kpm large page description.
1040  * The virtual address range of segkpm is divided into chunks of
1041  * kpm_pgsz. Each chunk is controlled by a kpm_page_t. The ushort
1042  * is sufficient for 2^^15 * PAGESIZE, so e.g. the maximum kpm_pgsz
1043  * for 8K is 256M and 2G for 64K pages. It it kept as small as
1044  * possible to save physical memory space.
1045  *
1046  * There are 2 segkpm mapping windows within in the virtual address
1047  * space when we have to prevent VAC alias conflicts. The so called
1048  * Alias window (mappings are always by PAGESIZE) is controlled by
1049  * kp_refcnta. The regular window is controlled by kp_refcnt for the
1050  * normal operation, which is to use the largest available pagesize.
1051  * When VAC alias conflicts are present within a chunk in the regular
1052  * window the large page mapping is broken up into smaller PAGESIZE
1053  * mappings. kp_refcntc is used to control the pages that are invoked
1054  * in the conflict and kp_refcnts holds the active mappings done
1055  * with the small page size. In non vac conflict mode kp_refcntc is
1056  * also used as "go" indication (-1) for the trap level tsbmiss
1057  * handler.
1058  */
1059 typedef struct kpm_page {
1060 	short kp_refcnt;	/* pages mapped large */
1061 	short kp_refcnta;	/* pages mapped in Alias window */
1062 	short kp_refcntc;	/* TL-tsbmiss flag; #vac alias conflict pages */
1063 	short kp_refcnts;	/* vac alias: pages mapped small */
1064 } kpm_page_t;
1065 
1066 /*
1067  * Note: khl_lock offset changes must be reflected in sfmmu_asm.s
1068  */
1069 typedef struct kpm_hlk {
1070 	kmutex_t khl_mutex;	/* kpm_page mutex */
1071 	uint_t   khl_lock;	/* trap level tsbmiss handling */
1072 } kpm_hlk_t;
1073 
1074 /*
1075  * kpm small page description.
1076  * When kpm_pgsz is equal to PAGESIZE a smaller representation is used
1077  * to save memory space. Alias range mappings and regular segkpm
1078  * mappings are done in units of PAGESIZE and can share the mapping
1079  * information and the mappings are always distinguishable by their
1080  * virtual address. Other information needed for VAC conflict prevention
1081  * is already available on a per page basis.
1082  *
1083  * The state about how a kpm page is mapped and whether it is ready to go
1084  * is indicated by the following 1 byte kpm_spage structure. This byte is
1085  * split into two 4-bit parts - kp_mapped and kp_mapped_go.
1086  * 	- kp_mapped == 1	the page is mapped cacheable
1087  *	- kp_mapped == 2	the page is mapped non-cacheable
1088  *	- kp_mapped_go == 1	the mapping is ready to be dropped in
1089  *	- kp_mapped_go == 0	the mapping is not ready to be dropped in.
1090  * When kp_mapped_go == 0, we will have C handler resolve the VAC conflict.
1091  * Otherwise, the assembly tsb miss handler can simply drop in the mapping
1092  * when a tsb miss occurs.
1093  */
1094 typedef union kpm_spage {
1095 	struct {
1096 #ifdef  _BIG_ENDIAN
1097 		uchar_t mapped_go: 4;	/* go or nogo flag */
1098 		uchar_t mapped: 4;	/* page mapped small */
1099 #else
1100 		uchar_t mapped: 4;	/* page mapped small */
1101 		uchar_t mapped_go: 4;	/* go or nogo flag */
1102 #endif
1103 	} kpm_spage_un;
1104 	uchar_t kp_mapped_flag;
1105 } kpm_spage_t;
1106 
1107 #define	kp_mapped	kpm_spage_un.mapped
1108 #define	kp_mapped_go	kpm_spage_un.mapped_go
1109 
1110 /*
1111  * Note: kshl_lock offset changes must be reflected in sfmmu_asm.s
1112  */
1113 typedef struct kpm_shlk {
1114 	uint_t   kshl_lock;	/* trap level tsbmiss handling */
1115 } kpm_shlk_t;
1116 
1117 /*
1118  * Each segment of physical memory is described by a memseg struct.
1119  * Within a segment, memory is considered contiguous. The members
1120  * can be categorized as follows:
1121  * . Platform independent:
1122  *         pages, epages, pages_base, pages_end, next, lnext.
1123  * . 64bit only but platform independent:
1124  *         kpm_pbase, kpm_nkpmpgs, kpm_pages, kpm_spages.
1125  * . Really platform or mmu specific:
1126  *         pagespa, epagespa, nextpa, kpm_pagespa.
1127  * . Mixed:
1128  *         msegflags.
1129  */
1130 struct memseg {
1131 	page_t *pages, *epages;		/* [from, to] in page array */
1132 	pfn_t pages_base, pages_end;	/* [from, to] in page numbers */
1133 	struct memseg *next;		/* next segment in list */
1134 	struct memseg *lnext;		/* next segment in deleted list */
1135 #if defined(__sparc)
1136 	uint64_t pagespa, epagespa;	/* [from, to] page array physical */
1137 	uint64_t nextpa;		/* physical next pointer */
1138 	pfn_t	kpm_pbase;		/* start of kpm range */
1139 	pgcnt_t kpm_nkpmpgs;		/* # of kpm_pgsz pages */
1140 	union _mseg_un {
1141 		kpm_page_t  *kpm_lpgs;	/* ptr to kpm_page array */
1142 		kpm_spage_t *kpm_spgs;	/* ptr to kpm_spage array */
1143 	} mseg_un;
1144 	uint64_t kpm_pagespa;		/* physical ptr to kpm (s)pages array */
1145 #endif /* __sparc */
1146 	uint_t msegflags;		/* memseg flags */
1147 };
1148 
1149 /* memseg union aliases */
1150 #define	kpm_pages	mseg_un.kpm_lpgs
1151 #define	kpm_spages	mseg_un.kpm_spgs
1152 
1153 /* msegflags */
1154 #define	MEMSEG_DYNAMIC		0x1	/* DR: memory was added dynamically */
1155 #define	MEMSEG_META_INCL	0x2	/* DR: memseg includes it's metadata */
1156 #define	MEMSEG_META_ALLOC	0x4	/* DR: memseg allocated it's metadata */
1157 
1158 /* memseg support macros */
1159 #define	MSEG_NPAGES(SEG)	((SEG)->pages_end - (SEG)->pages_base)
1160 
1161 /* memseg hash */
1162 #define	MEM_HASH_SHIFT		0x9
1163 #define	N_MEM_SLOTS		0x200		/* must be a power of 2 */
1164 #define	MEMSEG_PFN_HASH(pfn)	(((pfn)/mhash_per_slot) & (N_MEM_SLOTS - 1))
1165 
1166 /* memseg  externals */
1167 extern struct memseg *memsegs;		/* list of memory segments */
1168 extern ulong_t mhash_per_slot;
1169 extern uint64_t memsegspa;		/* memsegs as physical address */
1170 
1171 void build_pfn_hash();
1172 extern struct memseg *page_numtomemseg_nolock(pfn_t pfnum);
1173 
1174 /*
1175  * page capture related info:
1176  * The page capture routines allow us to asynchronously capture given pages
1177  * for the explicit use of the requestor.  New requestors can be added by
1178  * explicitly adding themselves to the PC_* flags below and incrementing
1179  * PC_NUM_CALLBACKS as necessary.
1180  *
1181  * Subsystems using page capture must register a callback before attempting
1182  * to capture a page.  A duration of -1 will indicate that we will never give
1183  * up while trying to capture a page and will only stop trying to capture the
1184  * given page once we have successfully captured it.  Thus the user needs to be
1185  * aware of the behavior of all callers who have a duration of -1.
1186  *
1187  * For now, only /dev/physmem and page retire use the page capture interface
1188  * and only a single request can be outstanding for a given page.  Thus, if
1189  * /dev/phsymem wants a page and page retire also wants the same page, only
1190  * the page retire request will be honored until the point in time that the
1191  * page is actually retired, at which point in time, subsequent requests by
1192  * /dev/physmem will succeed if the CAPTURE_GET_RETIRED flag was set.
1193  */
1194 
1195 #define	PC_RETIRE		(0)
1196 #define	PC_PHYSMEM		(1)
1197 #define	PC_NUM_CALLBACKS	(2)
1198 #define	PC_MASK			((1 << PC_NUM_CALLBACKS) - 1)
1199 
1200 #define	CAPTURE_RETIRE		(1 << PC_RETIRE)
1201 #define	CAPTURE_PHYSMEM		(1 << PC_PHYSMEM)
1202 
1203 #define	CAPTURE_ASYNC		(0x0200)
1204 
1205 #define	CAPTURE_GET_RETIRED	(0x1000)
1206 #define	CAPTURE_GET_CAGE	(0x2000)
1207 
1208 struct page_capture_callback {
1209 	int cb_active;		/* 1 means active, 0 means inactive */
1210 	clock_t duration;	/* the length in time that we'll attempt to */
1211 				/* capture this page asynchronously. (in HZ) */
1212 	krwlock_t cb_rwlock;
1213 	int (*cb_func)(page_t *, void *, uint_t); /* callback function */
1214 };
1215 
1216 extern kcondvar_t pc_cv;
1217 
1218 void page_capture_register_callback(uint_t index, clock_t duration,
1219     int (*cb_func)(page_t *, void *, uint_t));
1220 void page_capture_unregister_callback(uint_t index);
1221 int page_trycapture(page_t *pp, uint_t szc, uint_t flags, void *datap);
1222 void page_unlock_capture(page_t *pp);
1223 int page_capture_unretire_pp(page_t *);
1224 
1225 extern int memsegs_trylock(int);
1226 extern void memsegs_lock(int);
1227 extern void memsegs_unlock(int);
1228 extern int memsegs_lock_held(void);
1229 extern void memlist_read_lock(void);
1230 extern void memlist_read_unlock(void);
1231 extern void memlist_write_lock(void);
1232 extern void memlist_write_unlock(void);
1233 
1234 #ifdef	__cplusplus
1235 }
1236 #endif
1237 
1238 #endif	/* _VM_PAGE_H */
1239