xref: /onnv-gate/usr/src/uts/common/vm/page.h (revision 13076:e0735b974926)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51841Spraks  * Common Development and Distribution License (the "License").
61841Spraks  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
2212117SStan.Studzinski@Sun.COM  * Copyright (c) 1986, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
260Sstevel@tonic-gate /*	  All Rights Reserved  	*/
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
300Sstevel@tonic-gate  * The Regents of the University of California
310Sstevel@tonic-gate  * All Rights Reserved
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
340Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
350Sstevel@tonic-gate  * contributors.
360Sstevel@tonic-gate  */
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #ifndef	_VM_PAGE_H
390Sstevel@tonic-gate #define	_VM_PAGE_H
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <vm/seg.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #ifdef	__cplusplus
440Sstevel@tonic-gate extern "C" {
450Sstevel@tonic-gate #endif
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #if defined(_KERNEL) || defined(_KMEMUSER)
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate  * Shared/Exclusive lock.
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate 
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate  * Types of page locking supported by page_lock & friends.
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate typedef enum {
570Sstevel@tonic-gate 	SE_SHARED,
580Sstevel@tonic-gate 	SE_EXCL			/* exclusive lock (value == -1) */
590Sstevel@tonic-gate } se_t;
600Sstevel@tonic-gate 
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate  * For requesting that page_lock reclaim the page from the free list.
630Sstevel@tonic-gate  */
640Sstevel@tonic-gate typedef enum {
650Sstevel@tonic-gate 	P_RECLAIM,		/* reclaim page from free list */
660Sstevel@tonic-gate 	P_NO_RECLAIM		/* DON`T reclaim the page	*/
670Sstevel@tonic-gate } reclaim_t;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate  * Callers of page_try_reclaim_lock and page_lock_es can use this flag
710Sstevel@tonic-gate  * to get SE_EXCL access before reader/writers are given access.
720Sstevel@tonic-gate  */
730Sstevel@tonic-gate #define	SE_EXCL_WANTED	0x02
740Sstevel@tonic-gate 
75917Selowe /*
76917Selowe  * All page_*lock() requests will be denied unless this flag is set in
77917Selowe  * the 'es' parameter.
78917Selowe  */
79917Selowe #define	SE_RETIRED	0x04
80917Selowe 
810Sstevel@tonic-gate #endif	/* _KERNEL | _KMEMUSER */
820Sstevel@tonic-gate 
830Sstevel@tonic-gate typedef int	selock_t;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate  * Define VM_STATS to turn on all sorts of statistic gathering about
870Sstevel@tonic-gate  * the VM layer.  By default, it is only turned on when DEBUG is
880Sstevel@tonic-gate  * also defined.
890Sstevel@tonic-gate  */
900Sstevel@tonic-gate #ifdef DEBUG
910Sstevel@tonic-gate #define	VM_STATS
920Sstevel@tonic-gate #endif	/* DEBUG */
930Sstevel@tonic-gate 
940Sstevel@tonic-gate #ifdef VM_STATS
950Sstevel@tonic-gate #define	VM_STAT_ADD(stat)			(stat)++
960Sstevel@tonic-gate #define	VM_STAT_COND_ADD(cond, stat)		((void) (!(cond) || (stat)++))
970Sstevel@tonic-gate #else
980Sstevel@tonic-gate #define	VM_STAT_ADD(stat)
990Sstevel@tonic-gate #define	VM_STAT_COND_ADD(cond, stat)
1000Sstevel@tonic-gate #endif	/* VM_STATS */
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate #ifdef _KERNEL
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /*
10512230SFrank.Rival@oracle.com  * PAGE_LLOCK_SIZE is 2 * NCPU, but no smaller than 128.
10612230SFrank.Rival@oracle.com  * PAGE_LLOCK_SHIFT is log2(PAGE_LLOCK_SIZE).
107*13076SJonathan.Adams@Sun.COM  *
108*13076SJonathan.Adams@Sun.COM  * We use ? : instead of #if because <vm/page.h> is included everywhere;
109*13076SJonathan.Adams@Sun.COM  * NCPU_P2 is only a constant in the "unix" module.
110*13076SJonathan.Adams@Sun.COM  *
11112230SFrank.Rival@oracle.com  */
112*13076SJonathan.Adams@Sun.COM #define	PAGE_LLOCK_SHIFT \
113*13076SJonathan.Adams@Sun.COM 	    ((unsigned)(((2*NCPU_P2) > 128) ? NCPU_LOG2 + 1 : 7))
114*13076SJonathan.Adams@Sun.COM 
115*13076SJonathan.Adams@Sun.COM #define	PAGE_LLOCK_SIZE (1ul << PAGE_LLOCK_SHIFT)
11612230SFrank.Rival@oracle.com 
11712230SFrank.Rival@oracle.com /*
11812268SFrank.Rival@oracle.com  * The number of low order 0 (or less variable) bits in the page_t address.
1190Sstevel@tonic-gate  */
12012268SFrank.Rival@oracle.com #if defined(__sparc)
12112230SFrank.Rival@oracle.com #define	PP_SHIFT		7
12212268SFrank.Rival@oracle.com #else
12312268SFrank.Rival@oracle.com #define	PP_SHIFT		6
12412268SFrank.Rival@oracle.com #endif
12512230SFrank.Rival@oracle.com 
12612230SFrank.Rival@oracle.com /*
12712230SFrank.Rival@oracle.com  * pp may be the root of a large page, and many low order bits will be 0.
12812230SFrank.Rival@oracle.com  * Shift and XOR multiple times to capture the good bits across the range of
12912230SFrank.Rival@oracle.com  * possible page sizes.
13012230SFrank.Rival@oracle.com  */
13112230SFrank.Rival@oracle.com #define	PAGE_LLOCK_HASH(pp)	\
13212230SFrank.Rival@oracle.com 	(((((uintptr_t)(pp) >> PP_SHIFT) ^ \
13312230SFrank.Rival@oracle.com 	((uintptr_t)(pp) >> (PAGE_LLOCK_SHIFT + PP_SHIFT))) ^ \
13412230SFrank.Rival@oracle.com 	((uintptr_t)(pp) >> ((PAGE_LLOCK_SHIFT * 2) + PP_SHIFT)) ^ \
13512230SFrank.Rival@oracle.com 	((uintptr_t)(pp) >> ((PAGE_LLOCK_SHIFT * 3) + PP_SHIFT))) & \
13612230SFrank.Rival@oracle.com 	(PAGE_LLOCK_SIZE - 1))
13712230SFrank.Rival@oracle.com 
13812230SFrank.Rival@oracle.com #define	page_struct_lock(pp)	\
13912230SFrank.Rival@oracle.com 	mutex_enter(&page_llocks[PAGE_LLOCK_HASH(PP_PAGEROOT(pp))].pad_mutex)
14012230SFrank.Rival@oracle.com #define	page_struct_unlock(pp)	\
14112230SFrank.Rival@oracle.com 	mutex_exit(&page_llocks[PAGE_LLOCK_HASH(PP_PAGEROOT(pp))].pad_mutex)
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate #endif	/* _KERNEL */
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate #include <sys/t_lock.h>
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate struct as;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate /*
1500Sstevel@tonic-gate  * Each physical page has a page structure, which is used to maintain
1510Sstevel@tonic-gate  * these pages as a cache.  A page can be found via a hashed lookup
1520Sstevel@tonic-gate  * based on the [vp, offset].  If a page has an [vp, offset] identity,
1530Sstevel@tonic-gate  * then it is entered on a doubly linked circular list off the
1540Sstevel@tonic-gate  * vnode using the vpnext/vpprev pointers.   If the p_free bit
1550Sstevel@tonic-gate  * is on, then the page is also on a doubly linked circular free
1560Sstevel@tonic-gate  * list using next/prev pointers.  If the "p_selock" and "p_iolock"
1570Sstevel@tonic-gate  * are held, then the page is currently being read in (exclusive p_selock)
1580Sstevel@tonic-gate  * or written back (shared p_selock).  In this case, the next/prev pointers
1590Sstevel@tonic-gate  * are used to link the pages together for a consecutive i/o request.  If
1600Sstevel@tonic-gate  * the page is being brought in from its backing store, then other processes
1610Sstevel@tonic-gate  * will wait for the i/o to complete before attaching to the page since it
1620Sstevel@tonic-gate  * will have an "exclusive" lock.
1630Sstevel@tonic-gate  *
1640Sstevel@tonic-gate  * Each page structure has the locks described below along with
1650Sstevel@tonic-gate  * the fields they protect:
1660Sstevel@tonic-gate  *
1670Sstevel@tonic-gate  *	p_selock	This is a per-page shared/exclusive lock that is
1680Sstevel@tonic-gate  *			used to implement the logical shared/exclusive
1690Sstevel@tonic-gate  *			lock for each page.  The "shared" lock is normally
1700Sstevel@tonic-gate  *			used in most cases while the "exclusive" lock is
1710Sstevel@tonic-gate  *			required to destroy or retain exclusive access to
1720Sstevel@tonic-gate  *			a page (e.g., while reading in pages).  The appropriate
1730Sstevel@tonic-gate  *			lock is always held whenever there is any reference
1740Sstevel@tonic-gate  *			to a page structure (e.g., during i/o).
1750Sstevel@tonic-gate  *			(Note that with the addition of the "writer-lock-wanted"
1760Sstevel@tonic-gate  *			semantics (via SE_EWANTED), threads must not acquire
1770Sstevel@tonic-gate  *			multiple reader locks or else a deadly embrace will
1780Sstevel@tonic-gate  *			occur in the following situation: thread 1 obtains a
1790Sstevel@tonic-gate  *			reader lock; next thread 2 fails to get a writer lock
1800Sstevel@tonic-gate  *			but specified SE_EWANTED so it will wait by either
1810Sstevel@tonic-gate  *			blocking (when using page_lock_es) or spinning while
1820Sstevel@tonic-gate  *			retrying (when using page_try_reclaim_lock) until the
1830Sstevel@tonic-gate  *			reader lock is released; then thread 1 attempts to
1840Sstevel@tonic-gate  *			get another reader lock but is denied due to
1850Sstevel@tonic-gate  *			SE_EWANTED being set, and now both threads are in a
1860Sstevel@tonic-gate  *			deadly embrace.)
1870Sstevel@tonic-gate  *
1880Sstevel@tonic-gate  *				p_hash
1890Sstevel@tonic-gate  *				p_vnode
1900Sstevel@tonic-gate  *				p_offset
1910Sstevel@tonic-gate  *
1920Sstevel@tonic-gate  *				p_free
1930Sstevel@tonic-gate  *				p_age
1940Sstevel@tonic-gate  *
1950Sstevel@tonic-gate  *	p_iolock	This is a binary semaphore lock that provides
1960Sstevel@tonic-gate  *			exclusive access to the i/o list links in each
1970Sstevel@tonic-gate  *			page structure.  It is always held while the page
1980Sstevel@tonic-gate  *			is on an i/o list (i.e., involved in i/o).  That is,
1990Sstevel@tonic-gate  *			even though a page may be only `shared' locked
2000Sstevel@tonic-gate  *			while it is doing a write, the following fields may
2010Sstevel@tonic-gate  *			change anyway.  Normally, the page must be
2020Sstevel@tonic-gate  *			`exclusively' locked to change anything in it.
2030Sstevel@tonic-gate  *
2040Sstevel@tonic-gate  *				p_next
2050Sstevel@tonic-gate  *				p_prev
2060Sstevel@tonic-gate  *
20712230SFrank.Rival@oracle.com  * The following fields are protected by the global page_llocks[]:
2080Sstevel@tonic-gate  *
2090Sstevel@tonic-gate  *				p_lckcnt
2100Sstevel@tonic-gate  *				p_cowcnt
2110Sstevel@tonic-gate  *
2120Sstevel@tonic-gate  * The following lists are protected by the global page_freelock:
2130Sstevel@tonic-gate  *
2140Sstevel@tonic-gate  *				page_cachelist
2150Sstevel@tonic-gate  *				page_freelist
2160Sstevel@tonic-gate  *
2170Sstevel@tonic-gate  * The following, for our purposes, are protected by
2180Sstevel@tonic-gate  * the global freemem_lock:
2190Sstevel@tonic-gate  *
2200Sstevel@tonic-gate  *				freemem
2210Sstevel@tonic-gate  *				freemem_wait
2220Sstevel@tonic-gate  *				freemem_cv
2230Sstevel@tonic-gate  *
2240Sstevel@tonic-gate  * The following fields are protected by hat layer lock(s).  When a page
2250Sstevel@tonic-gate  * structure is not mapped and is not associated with a vnode (after a call
2260Sstevel@tonic-gate  * to page_hashout() for example) the p_nrm field may be modified with out
2270Sstevel@tonic-gate  * holding the hat layer lock:
2280Sstevel@tonic-gate  *
2290Sstevel@tonic-gate  *				p_nrm
2300Sstevel@tonic-gate  *				p_mapping
2310Sstevel@tonic-gate  *				p_share
2320Sstevel@tonic-gate  *
2330Sstevel@tonic-gate  * The following field is file system dependent.  How it is used and
2340Sstevel@tonic-gate  * the locking strategies applied are up to the individual file system
2350Sstevel@tonic-gate  * implementation.
2360Sstevel@tonic-gate  *
2370Sstevel@tonic-gate  *				p_fsdata
2380Sstevel@tonic-gate  *
2390Sstevel@tonic-gate  * The page structure is used to represent and control the system's
2400Sstevel@tonic-gate  * physical pages.  There is one instance of the structure for each
2410Sstevel@tonic-gate  * page that is not permenately allocated.  For example, the pages that
2420Sstevel@tonic-gate  * hold the page structures are permanently held by the kernel
2430Sstevel@tonic-gate  * and hence do not need page structures to track them.  The array
2440Sstevel@tonic-gate  * of page structures is allocated early on in the kernel's life and
2450Sstevel@tonic-gate  * is based on the amount of available physical memory.
2460Sstevel@tonic-gate  *
2470Sstevel@tonic-gate  * Each page structure may simultaneously appear on several linked lists.
2480Sstevel@tonic-gate  * The lists are:  hash list, free or in i/o list, and a vnode's page list.
2490Sstevel@tonic-gate  * Each type of list is protected by a different group of mutexes as described
2500Sstevel@tonic-gate  * below:
2510Sstevel@tonic-gate  *
2520Sstevel@tonic-gate  * The hash list is used to quickly find a page when the page's vnode and
2530Sstevel@tonic-gate  * offset within the vnode are known.  Each page that is hashed is
2540Sstevel@tonic-gate  * connected via the `p_hash' field.  The anchor for each hash is in the
2550Sstevel@tonic-gate  * array `page_hash'.  An array of mutexes, `ph_mutex', protects the
2560Sstevel@tonic-gate  * lists anchored by page_hash[].  To either search or modify a given hash
2570Sstevel@tonic-gate  * list, the appropriate mutex in the ph_mutex array must be held.
2580Sstevel@tonic-gate  *
2590Sstevel@tonic-gate  * The free list contains pages that are `free to be given away'.  For
2600Sstevel@tonic-gate  * efficiency reasons, pages on this list are placed in two catagories:
2610Sstevel@tonic-gate  * pages that are still associated with a vnode, and pages that are not
2620Sstevel@tonic-gate  * associated with a vnode.  Free pages always have their `p_free' bit set,
2630Sstevel@tonic-gate  * free pages that are still associated with a vnode also have their
2640Sstevel@tonic-gate  * `p_age' bit set.  Pages on the free list are connected via their
2650Sstevel@tonic-gate  * `p_next' and `p_prev' fields.  When a page is involved in some sort
2660Sstevel@tonic-gate  * of i/o, it is not free and these fields may be used to link associated
2670Sstevel@tonic-gate  * pages together.  At the moment, the free list is protected by a
2680Sstevel@tonic-gate  * single mutex `page_freelock'.  The list of free pages still associated
2690Sstevel@tonic-gate  * with a vnode is anchored by `page_cachelist' while other free pages
2700Sstevel@tonic-gate  * are anchored in architecture dependent ways (to handle page coloring etc.).
2710Sstevel@tonic-gate  *
2720Sstevel@tonic-gate  * Pages associated with a given vnode appear on a list anchored in the
2730Sstevel@tonic-gate  * vnode by the `v_pages' field.  They are linked together with
2740Sstevel@tonic-gate  * `p_vpnext' and `p_vpprev'.  The field `p_offset' contains a page's
2750Sstevel@tonic-gate  * offset within the vnode.  The pages on this list are not kept in
2760Sstevel@tonic-gate  * offset order.  These lists, in a manner similar to the hash lists,
2770Sstevel@tonic-gate  * are protected by an array of mutexes called `vph_hash'.  Before
2780Sstevel@tonic-gate  * searching or modifying this chain the appropriate mutex in the
2790Sstevel@tonic-gate  * vph_hash[] array must be held.
2800Sstevel@tonic-gate  *
2810Sstevel@tonic-gate  * Again, each of the lists that a page can appear on is protected by a
2820Sstevel@tonic-gate  * mutex.  Before reading or writing any of the fields comprising the
2830Sstevel@tonic-gate  * list, the appropriate lock must be held.  These list locks should only
2840Sstevel@tonic-gate  * be held for very short intervals.
2850Sstevel@tonic-gate  *
2860Sstevel@tonic-gate  * In addition to the list locks, each page structure contains a
2870Sstevel@tonic-gate  * shared/exclusive lock that protects various fields within it.
2880Sstevel@tonic-gate  * To modify one of these fields, the `p_selock' must be exclusively held.
2890Sstevel@tonic-gate  * To read a field with a degree of certainty, the lock must be at least
2900Sstevel@tonic-gate  * held shared.
2910Sstevel@tonic-gate  *
2920Sstevel@tonic-gate  * Removing a page structure from one of the lists requires holding
2930Sstevel@tonic-gate  * the appropriate list lock and the page's p_selock.  A page may be
2940Sstevel@tonic-gate  * prevented from changing identity, being freed, or otherwise modified
2950Sstevel@tonic-gate  * by acquiring p_selock shared.
2960Sstevel@tonic-gate  *
2970Sstevel@tonic-gate  * To avoid deadlocks, a strict locking protocol must be followed.  Basically
2980Sstevel@tonic-gate  * there are two cases:  In the first case, the page structure in question
2990Sstevel@tonic-gate  * is known ahead of time (e.g., when the page is to be added or removed
3000Sstevel@tonic-gate  * from a list).  In the second case, the page structure is not known and
3010Sstevel@tonic-gate  * must be found by searching one of the lists.
3020Sstevel@tonic-gate  *
3030Sstevel@tonic-gate  * When adding or removing a known page to one of the lists, first the
3040Sstevel@tonic-gate  * page must be exclusively locked (since at least one of its fields
3050Sstevel@tonic-gate  * will be modified), second the lock protecting the list must be acquired,
3060Sstevel@tonic-gate  * third the page inserted or deleted, and finally the list lock dropped.
3070Sstevel@tonic-gate  *
3080Sstevel@tonic-gate  * The more interesting case occures when the particular page structure
3090Sstevel@tonic-gate  * is not known ahead of time.  For example, when a call is made to
3100Sstevel@tonic-gate  * page_lookup(), it is not known if a page with the desired (vnode and
3110Sstevel@tonic-gate  * offset pair) identity exists.  So the appropriate mutex in ph_mutex is
3120Sstevel@tonic-gate  * acquired, the hash list searched, and if the desired page is found
3130Sstevel@tonic-gate  * an attempt is made to lock it.  The attempt to acquire p_selock must
3140Sstevel@tonic-gate  * not block while the hash list lock is held.  A deadlock could occure
3150Sstevel@tonic-gate  * if some other process was trying to remove the page from the list.
3160Sstevel@tonic-gate  * The removing process (following the above protocol) would have exclusively
3170Sstevel@tonic-gate  * locked the page, and be spinning waiting to acquire the lock protecting
3180Sstevel@tonic-gate  * the hash list.  Since the searching process holds the hash list lock
3190Sstevel@tonic-gate  * and is waiting to acquire the page lock, a deadlock occurs.
3200Sstevel@tonic-gate  *
3210Sstevel@tonic-gate  * The proper scheme to follow is: first, lock the appropriate list,
3220Sstevel@tonic-gate  * search the list, and if the desired page is found either use
3230Sstevel@tonic-gate  * page_trylock() (which will not block) or pass the address of the
3240Sstevel@tonic-gate  * list lock to page_lock().  If page_lock() can not acquire the page's
3250Sstevel@tonic-gate  * lock, it will drop the list lock before going to sleep.  page_lock()
3260Sstevel@tonic-gate  * returns a value to indicate if the list lock was dropped allowing the
3270Sstevel@tonic-gate  * calling program to react appropriately (i.e., retry the operation).
3280Sstevel@tonic-gate  *
3290Sstevel@tonic-gate  * If the list lock was dropped before the attempt at locking the page
3300Sstevel@tonic-gate  * was made, checks would have to be made to ensure that the page had
3310Sstevel@tonic-gate  * not changed identity before its lock was obtained.  This is because
3320Sstevel@tonic-gate  * the interval between dropping the list lock and acquiring the page
3330Sstevel@tonic-gate  * lock is indeterminate.
3340Sstevel@tonic-gate  *
3350Sstevel@tonic-gate  * In addition, when both a hash list lock (ph_mutex[]) and a vnode list
3360Sstevel@tonic-gate  * lock (vph_mutex[]) are needed, the hash list lock must be acquired first.
3370Sstevel@tonic-gate  * The routine page_hashin() is a good example of this sequence.
3380Sstevel@tonic-gate  * This sequence is ASSERTed by checking that the vph_mutex[] is not held
3390Sstevel@tonic-gate  * just before each acquisition of one of the mutexs in ph_mutex[].
3400Sstevel@tonic-gate  *
3410Sstevel@tonic-gate  * So, as a quick summary:
3420Sstevel@tonic-gate  *
3430Sstevel@tonic-gate  * 	pse_mutex[]'s protect the p_selock and p_cv fields.
3440Sstevel@tonic-gate  *
3450Sstevel@tonic-gate  * 	p_selock protects the p_free, p_age, p_vnode, p_offset and p_hash,
3460Sstevel@tonic-gate  *
3470Sstevel@tonic-gate  * 	ph_mutex[]'s protect the page_hash[] array and its chains.
3480Sstevel@tonic-gate  *
3490Sstevel@tonic-gate  * 	vph_mutex[]'s protect the v_pages field and the vp page chains.
3500Sstevel@tonic-gate  *
3510Sstevel@tonic-gate  *	First lock the page, then the hash chain, then the vnode chain.  When
3520Sstevel@tonic-gate  *	this is not possible `trylocks' must be used.  Sleeping while holding
3530Sstevel@tonic-gate  *	any of these mutexes (p_selock is not a mutex) is not allowed.
3540Sstevel@tonic-gate  *
3550Sstevel@tonic-gate  *
3560Sstevel@tonic-gate  *	field		reading		writing		    ordering
3570Sstevel@tonic-gate  *	======================================================================
3580Sstevel@tonic-gate  *	p_vnode		p_selock(E,S)	p_selock(E)
3590Sstevel@tonic-gate  *	p_offset
3600Sstevel@tonic-gate  *	p_free
3610Sstevel@tonic-gate  *	p_age
3620Sstevel@tonic-gate  *	=====================================================================
3630Sstevel@tonic-gate  *	p_hash		p_selock(E,S)	p_selock(E) &&	    p_selock, ph_mutex
3640Sstevel@tonic-gate  *					ph_mutex[]
3650Sstevel@tonic-gate  *	=====================================================================
3660Sstevel@tonic-gate  *	p_vpnext	p_selock(E,S)	p_selock(E) &&	    p_selock, vph_mutex
3670Sstevel@tonic-gate  *	p_vpprev			vph_mutex[]
3680Sstevel@tonic-gate  *	=====================================================================
3690Sstevel@tonic-gate  *	When the p_free bit is set:
3700Sstevel@tonic-gate  *
3710Sstevel@tonic-gate  *	p_next		p_selock(E,S)	p_selock(E) &&	    p_selock,
3720Sstevel@tonic-gate  *	p_prev				page_freelock	    page_freelock
3730Sstevel@tonic-gate  *
3740Sstevel@tonic-gate  *	When the p_free bit is not set:
3750Sstevel@tonic-gate  *
3760Sstevel@tonic-gate  *	p_next		p_selock(E,S)	p_selock(E) &&	    p_selock, p_iolock
3770Sstevel@tonic-gate  *	p_prev				p_iolock
3780Sstevel@tonic-gate  *	=====================================================================
3790Sstevel@tonic-gate  *	p_selock	pse_mutex[]	pse_mutex[]	    can`t acquire any
3800Sstevel@tonic-gate  *	p_cv						    other mutexes or
3810Sstevel@tonic-gate  *							    sleep while holding
3820Sstevel@tonic-gate  *							    this lock.
3830Sstevel@tonic-gate  *	=====================================================================
38412230SFrank.Rival@oracle.com  *	p_lckcnt	p_selock(E,S)	p_selock(E)
38512230SFrank.Rival@oracle.com  *					    OR
38612230SFrank.Rival@oracle.com  *					p_selock(S) &&
38712230SFrank.Rival@oracle.com  *					page_llocks[]
38812230SFrank.Rival@oracle.com  *	p_cowcnt
3890Sstevel@tonic-gate  *	=====================================================================
3900Sstevel@tonic-gate  *	p_nrm		hat layer lock	hat layer lock
3910Sstevel@tonic-gate  *	p_mapping
3920Sstevel@tonic-gate  *	p_pagenum
3930Sstevel@tonic-gate  *	=====================================================================
3940Sstevel@tonic-gate  *
3950Sstevel@tonic-gate  *	where:
3960Sstevel@tonic-gate  *		E----> exclusive version of p_selock.
3970Sstevel@tonic-gate  *		S----> shared version of p_selock.
3980Sstevel@tonic-gate  *
3990Sstevel@tonic-gate  *
4000Sstevel@tonic-gate  *	Global data structures and variable:
4010Sstevel@tonic-gate  *
4020Sstevel@tonic-gate  *	field		reading		writing		    ordering
4030Sstevel@tonic-gate  *	=====================================================================
4040Sstevel@tonic-gate  *	page_hash[]	ph_mutex[]	ph_mutex[]	    can hold this lock
4050Sstevel@tonic-gate  *							    before acquiring
4060Sstevel@tonic-gate  *							    a vph_mutex or
4070Sstevel@tonic-gate  *							    pse_mutex.
4080Sstevel@tonic-gate  *	=====================================================================
4090Sstevel@tonic-gate  *	vp->v_pages	vph_mutex[]	vph_mutex[]	    can only acquire
4100Sstevel@tonic-gate  *							    a pse_mutex while
4110Sstevel@tonic-gate  *							    holding this lock.
4120Sstevel@tonic-gate  *	=====================================================================
4130Sstevel@tonic-gate  *	page_cachelist	page_freelock	page_freelock	    can't acquire any
4140Sstevel@tonic-gate  *	page_freelist	page_freelock	page_freelock
4150Sstevel@tonic-gate  *	=====================================================================
4160Sstevel@tonic-gate  *	freemem		freemem_lock	freemem_lock	    can't acquire any
4170Sstevel@tonic-gate  *	freemem_wait					    other mutexes while
4180Sstevel@tonic-gate  *	freemem_cv					    holding this mutex.
4190Sstevel@tonic-gate  *	=====================================================================
4200Sstevel@tonic-gate  *
4210Sstevel@tonic-gate  * Page relocation, PG_NORELOC and P_NORELOC.
4220Sstevel@tonic-gate  *
4230Sstevel@tonic-gate  * Pages may be relocated using the page_relocate() interface. Relocation
4240Sstevel@tonic-gate  * involves moving the contents and identity of a page to another, free page.
4250Sstevel@tonic-gate  * To relocate a page, the SE_EXCL lock must be obtained. The way to prevent
4260Sstevel@tonic-gate  * a page from being relocated is to hold the SE_SHARED lock (the SE_EXCL
4270Sstevel@tonic-gate  * lock must not be held indefinitely). If the page is going to be held
4280Sstevel@tonic-gate  * SE_SHARED indefinitely, then the PG_NORELOC hint should be passed
4290Sstevel@tonic-gate  * to page_create_va so that pages that are prevented from being relocated
4300Sstevel@tonic-gate  * can be managed differently by the platform specific layer.
4310Sstevel@tonic-gate  *
4320Sstevel@tonic-gate  * Pages locked in memory using page_pp_lock (p_lckcnt/p_cowcnt != 0)
4330Sstevel@tonic-gate  * are guaranteed to be held in memory, but can still be relocated
4340Sstevel@tonic-gate  * providing the SE_EXCL lock can be obtained.
4350Sstevel@tonic-gate  *
4360Sstevel@tonic-gate  * The P_NORELOC bit in the page_t.p_state field is provided for use by
4370Sstevel@tonic-gate  * the platform specific code in managing pages when the PG_NORELOC
4380Sstevel@tonic-gate  * hint is used.
4390Sstevel@tonic-gate  *
4400Sstevel@tonic-gate  * Memory delete and page locking.
4410Sstevel@tonic-gate  *
4420Sstevel@tonic-gate  * The set of all usable pages is managed using the global page list as
4430Sstevel@tonic-gate  * implemented by the memseg structure defined below. When memory is added
4440Sstevel@tonic-gate  * or deleted this list changes. Additions to this list guarantee that the
4450Sstevel@tonic-gate  * list is never corrupt.  In order to avoid the necessity of an additional
4460Sstevel@tonic-gate  * lock to protect against failed accesses to the memseg being deleted and,
4470Sstevel@tonic-gate  * more importantly, the page_ts, the memseg structure is never freed and the
4480Sstevel@tonic-gate  * page_t virtual address space is remapped to a page (or pages) of
4490Sstevel@tonic-gate  * zeros.  If a page_t is manipulated while it is p_selock'd, or if it is
4500Sstevel@tonic-gate  * locked indirectly via a hash or freelist lock, it is not possible for
4510Sstevel@tonic-gate  * memory delete to collect the page and so that part of the page list is
4520Sstevel@tonic-gate  * prevented from being deleted. If the page is referenced outside of one
4530Sstevel@tonic-gate  * of these locks, it is possible for the page_t being referenced to be
4540Sstevel@tonic-gate  * deleted.  Examples of this are page_t pointers returned by
4550Sstevel@tonic-gate  * page_numtopp_nolock, page_first and page_next.  Providing the page_t
4560Sstevel@tonic-gate  * is re-checked after taking the p_selock (for p_vnode != NULL), the
4570Sstevel@tonic-gate  * remapping to the zero pages will be detected.
4580Sstevel@tonic-gate  *
4590Sstevel@tonic-gate  *
4600Sstevel@tonic-gate  * Page size (p_szc field) and page locking.
4610Sstevel@tonic-gate  *
4620Sstevel@tonic-gate  * p_szc field of free pages is changed by free list manager under freelist
4630Sstevel@tonic-gate  * locks and is of no concern to the rest of VM subsystem.
4640Sstevel@tonic-gate  *
4650Sstevel@tonic-gate  * p_szc changes of allocated anonymous (swapfs) can only be done only after
4660Sstevel@tonic-gate  * exclusively locking all constituent pages and calling hat_pageunload() on
4670Sstevel@tonic-gate  * each of them. To prevent p_szc changes of non free anonymous (swapfs) large
4680Sstevel@tonic-gate  * pages it's enough to either lock SHARED any of constituent pages or prevent
4690Sstevel@tonic-gate  * hat_pageunload() by holding hat level lock that protects mapping lists (this
4700Sstevel@tonic-gate  * method is for hat code only)
4710Sstevel@tonic-gate  *
4720Sstevel@tonic-gate  * To increase (promote) p_szc of allocated non anonymous file system pages
4730Sstevel@tonic-gate  * one has to first lock exclusively all involved constituent pages and call
4740Sstevel@tonic-gate  * hat_pageunload() on each of them. To prevent p_szc promote it's enough to
4750Sstevel@tonic-gate  * either lock SHARED any of constituent pages that will be needed to make a
4760Sstevel@tonic-gate  * large page or prevent hat_pageunload() by holding hat level lock that
4770Sstevel@tonic-gate  * protects mapping lists (this method is for hat code only).
4780Sstevel@tonic-gate  *
4790Sstevel@tonic-gate  * To decrease (demote) p_szc of an allocated non anonymous file system large
4800Sstevel@tonic-gate  * page one can either use the same method as used for changeing p_szc of
4810Sstevel@tonic-gate  * anonymous large pages or if it's not possible to lock all constituent pages
4820Sstevel@tonic-gate  * exclusively a different method can be used. In the second method one only
4830Sstevel@tonic-gate  * has to exclusively lock one of constituent pages but then one has to
4840Sstevel@tonic-gate  * acquire further locks by calling page_szc_lock() and
4850Sstevel@tonic-gate  * hat_page_demote(). hat_page_demote() acquires hat level locks and then
4860Sstevel@tonic-gate  * demotes the page. This mechanism relies on the fact that any code that
4870Sstevel@tonic-gate  * needs to prevent p_szc of a file system large page from changeing either
4880Sstevel@tonic-gate  * locks all constituent large pages at least SHARED or locks some pages at
4890Sstevel@tonic-gate  * least SHARED and calls page_szc_lock() or uses hat level page locks.
4900Sstevel@tonic-gate  * Demotion using this method is implemented by page_demote_vp_pages().
4910Sstevel@tonic-gate  * Please see comments in front of page_demote_vp_pages(), hat_page_demote()
4920Sstevel@tonic-gate  * and page_szc_lock() for more details.
4930Sstevel@tonic-gate  *
4940Sstevel@tonic-gate  * Lock order: p_selock, page_szc_lock, ph_mutex/vph_mutex/freelist,
4950Sstevel@tonic-gate  * hat level locks.
4960Sstevel@tonic-gate  */
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate typedef struct page {
4990Sstevel@tonic-gate 	u_offset_t	p_offset;	/* offset into vnode for this page */
5000Sstevel@tonic-gate 	struct vnode	*p_vnode;	/* vnode that this page is named by */
5010Sstevel@tonic-gate 	selock_t	p_selock;	/* shared/exclusive lock on the page */
5020Sstevel@tonic-gate #if defined(_LP64)
5031841Spraks 	uint_t		p_vpmref;	/* vpm ref - index of the vpmap_t */
5040Sstevel@tonic-gate #endif
5050Sstevel@tonic-gate 	struct page	*p_hash;	/* hash by [vnode, offset] */
5060Sstevel@tonic-gate 	struct page	*p_vpnext;	/* next page in vnode list */
5070Sstevel@tonic-gate 	struct page	*p_vpprev;	/* prev page in vnode list */
5080Sstevel@tonic-gate 	struct page	*p_next;	/* next page in free/intrans lists */
5090Sstevel@tonic-gate 	struct page	*p_prev;	/* prev page in free/intrans lists */
5100Sstevel@tonic-gate 	ushort_t	p_lckcnt;	/* number of locks on page data */
5110Sstevel@tonic-gate 	ushort_t	p_cowcnt;	/* number of copy on write lock */
5120Sstevel@tonic-gate 	kcondvar_t	p_cv;		/* page struct's condition var */
5130Sstevel@tonic-gate 	kcondvar_t	p_io_cv;	/* for iolock */
5140Sstevel@tonic-gate 	uchar_t		p_iolock_state;	/* replaces p_iolock */
5150Sstevel@tonic-gate 	volatile uchar_t p_szc;		/* page size code */
5160Sstevel@tonic-gate 	uchar_t		p_fsdata;	/* file system dependent byte */
5170Sstevel@tonic-gate 	uchar_t		p_state;	/* p_free, p_noreloc */
5180Sstevel@tonic-gate 	uchar_t		p_nrm;		/* non-cache, ref, mod readonly bits */
5190Sstevel@tonic-gate #if defined(__sparc)
5200Sstevel@tonic-gate 	uchar_t		p_vcolor;	/* virtual color */
5210Sstevel@tonic-gate #else
5220Sstevel@tonic-gate 	uchar_t		p_embed;	/* x86 - changes p_mapping & p_index */
5230Sstevel@tonic-gate #endif
5240Sstevel@tonic-gate 	uchar_t		p_index;	/* MPSS mapping info. Not used on x86 */
5250Sstevel@tonic-gate 	uchar_t		p_toxic;	/* page has an unrecoverable error */
5260Sstevel@tonic-gate 	void		*p_mapping;	/* hat specific translation info */
5270Sstevel@tonic-gate 	pfn_t		p_pagenum;	/* physical page number */
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	uint_t		p_share;	/* number of translations */
5300Sstevel@tonic-gate #if defined(_LP64)
5310Sstevel@tonic-gate 	uint_t		p_sharepad;	/* pad for growing p_share */
5320Sstevel@tonic-gate #endif
5332414Saguzovsk 	uint_t		p_slckcnt;	/* number of softlocks */
5340Sstevel@tonic-gate #if defined(__sparc)
5350Sstevel@tonic-gate 	uint_t		p_kpmref;	/* number of kpm mapping sharers */
5360Sstevel@tonic-gate 	struct kpme	*p_kpmelist;	/* kpm specific mapping info */
5370Sstevel@tonic-gate #else
5380Sstevel@tonic-gate 	/* index of entry in p_map when p_embed is set */
5390Sstevel@tonic-gate 	uint_t		p_mlentry;
5400Sstevel@tonic-gate #endif
5411841Spraks #if defined(_LP64)
5421841Spraks 	kmutex_t	p_ilock;	/* protects p_vpmref */
5431841Spraks #else
5440Sstevel@tonic-gate 	uint64_t	p_msresv_2;	/* page allocation debugging */
5451841Spraks #endif
5460Sstevel@tonic-gate } page_t;
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate typedef	page_t	devpage_t;
5500Sstevel@tonic-gate #define	devpage	page
5510Sstevel@tonic-gate 
5522414Saguzovsk #define	PAGE_LOCK_MAXIMUM \
5532414Saguzovsk 	((1 << (sizeof (((page_t *)0)->p_lckcnt) * NBBY)) - 1)
5542414Saguzovsk 
5552414Saguzovsk #define	PAGE_SLOCK_MAXIMUM UINT_MAX
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate /*
5580Sstevel@tonic-gate  * Page hash table is a power-of-two in size, externally chained
5590Sstevel@tonic-gate  * through the hash field.  PAGE_HASHAVELEN is the average length
5600Sstevel@tonic-gate  * desired for this chain, from which the size of the page_hash
5610Sstevel@tonic-gate  * table is derived at boot time and stored in the kernel variable
5620Sstevel@tonic-gate  * page_hashsz.  In the hash function it is given by PAGE_HASHSZ.
5630Sstevel@tonic-gate  *
5640Sstevel@tonic-gate  * PAGE_HASH_FUNC returns an index into the page_hash[] array.  This
5650Sstevel@tonic-gate  * index is also used to derive the mutex that protects the chain.
5660Sstevel@tonic-gate  *
5670Sstevel@tonic-gate  * In constructing the hash function, first we dispose of unimportant bits
5680Sstevel@tonic-gate  * (page offset from "off" and the low 3 bits of "vp" which are zero for
5690Sstevel@tonic-gate  * struct alignment). Then shift and sum the remaining bits a couple times
5700Sstevel@tonic-gate  * in order to get as many source bits from the two source values into the
5710Sstevel@tonic-gate  * resulting hashed value.  Note that this will perform quickly, since the
5720Sstevel@tonic-gate  * shifting/summing are fast register to register operations with no additional
5730Sstevel@tonic-gate  * memory references).
57412230SFrank.Rival@oracle.com  *
57512230SFrank.Rival@oracle.com  * PH_SHIFT_SIZE is the amount to use for the successive shifts in the hash
57612230SFrank.Rival@oracle.com  * function below.  The actual value is LOG2(PH_TABLE_SIZE), so that as many
57712230SFrank.Rival@oracle.com  * bits as possible will filter thru PAGE_HASH_FUNC() and PAGE_HASH_MUTEX().
578*13076SJonathan.Adams@Sun.COM  *
579*13076SJonathan.Adams@Sun.COM  * We use ? : instead of #if because <vm/page.h> is included everywhere;
580*13076SJonathan.Adams@Sun.COM  * NCPU maps to a global variable outside of the "unix" module.
5810Sstevel@tonic-gate  */
5824325Sqiao #if defined(_LP64)
583*13076SJonathan.Adams@Sun.COM #define	PH_SHIFT_SIZE	((NCPU < 4) ? 7		: (NCPU_LOG2 + 1))
5844325Sqiao #else	/* 32 bits */
585*13076SJonathan.Adams@Sun.COM #define	PH_SHIFT_SIZE	((NCPU < 4) ? 4		: 7)
586*13076SJonathan.Adams@Sun.COM #endif	/* _LP64 */
5874325Sqiao 
588*13076SJonathan.Adams@Sun.COM #define	PH_TABLE_SIZE	(1ul << PH_SHIFT_SIZE)
5894325Sqiao 
5900Sstevel@tonic-gate /*
59112230SFrank.Rival@oracle.com  *
59212230SFrank.Rival@oracle.com  * We take care to get as much randomness as possible from both the vp and
59312230SFrank.Rival@oracle.com  * the offset.  Workloads can have few vnodes with many offsets, many vnodes
59412230SFrank.Rival@oracle.com  * with few offsets or a moderate mix of both.  This hash should perform
59512230SFrank.Rival@oracle.com  * equally well for each of these possibilities and for all types of memory
59612230SFrank.Rival@oracle.com  * allocations.
59712230SFrank.Rival@oracle.com  *
59812230SFrank.Rival@oracle.com  * vnodes representing files are created over a long period of time and
59912230SFrank.Rival@oracle.com  * have good variation in the upper vp bits, and the right shifts below
60012230SFrank.Rival@oracle.com  * capture these bits.  However, swap vnodes are created quickly in a
60112230SFrank.Rival@oracle.com  * narrow vp* range.  Refer to comments at swap_alloc: vnum has exactly
60212230SFrank.Rival@oracle.com  * AN_VPSHIFT bits, so the kmem_alloc'd vnode addresses have approximately
60312230SFrank.Rival@oracle.com  * AN_VPSHIFT bits of variation above their VNODE_ALIGN low order 0 bits.
60412230SFrank.Rival@oracle.com  * Spread swap vnodes widely in the hash table by XOR'ing a term with the
60512230SFrank.Rival@oracle.com  * vp bits of variation left shifted to the top of the range.
6060Sstevel@tonic-gate  */
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate #define	PAGE_HASHSZ	page_hashsz
6090Sstevel@tonic-gate #define	PAGE_HASHAVELEN		4
6100Sstevel@tonic-gate #define	PAGE_HASH_FUNC(vp, off) \
61112230SFrank.Rival@oracle.com 	(((((uintptr_t)(off) >> PAGESHIFT) ^ \
612*13076SJonathan.Adams@Sun.COM 	    ((uintptr_t)(off) >> (PAGESHIFT + PH_SHIFT_SIZE))) ^ \
613*13076SJonathan.Adams@Sun.COM 	    (((uintptr_t)(vp) >> 3) ^ \
614*13076SJonathan.Adams@Sun.COM 	    ((uintptr_t)(vp) >> (3 + PH_SHIFT_SIZE)) ^ \
615*13076SJonathan.Adams@Sun.COM 	    ((uintptr_t)(vp) >> (3 + 2 * PH_SHIFT_SIZE)) ^ \
616*13076SJonathan.Adams@Sun.COM 	    ((uintptr_t)(vp) << \
617*13076SJonathan.Adams@Sun.COM 	    (page_hashsz_shift - AN_VPSHIFT - VNODE_ALIGN_LOG2)))) & \
618*13076SJonathan.Adams@Sun.COM 	    (PAGE_HASHSZ - 1))
619*13076SJonathan.Adams@Sun.COM 
6200Sstevel@tonic-gate #ifdef _KERNEL
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate /*
6230Sstevel@tonic-gate  * The page hash value is re-hashed to an index for the ph_mutex array.
6240Sstevel@tonic-gate  *
6250Sstevel@tonic-gate  * For 64 bit kernels, the mutex array is padded out to prevent false
6260Sstevel@tonic-gate  * sharing of cache sub-blocks (64 bytes) of adjacent mutexes.
6270Sstevel@tonic-gate  *
6280Sstevel@tonic-gate  * For 32 bit kernels, we don't want to waste kernel address space with
6290Sstevel@tonic-gate  * padding, so instead we rely on the hash function to introduce skew of
6300Sstevel@tonic-gate  * adjacent vnode/offset indexes (the left shift part of the hash function).
6310Sstevel@tonic-gate  * Since sizeof (kmutex_t) is 8, we shift an additional 3 to skew to a different
6320Sstevel@tonic-gate  * 64 byte sub-block.
6330Sstevel@tonic-gate  */
6340Sstevel@tonic-gate extern pad_mutex_t ph_mutex[];
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate #define	PAGE_HASH_MUTEX(x) \
63712230SFrank.Rival@oracle.com 	&(ph_mutex[((x) ^ ((x) >> PH_SHIFT_SIZE) + ((x) << 3)) & \
6380Sstevel@tonic-gate 		(PH_TABLE_SIZE - 1)].pad_mutex)
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate /*
6410Sstevel@tonic-gate  * Flags used while creating pages.
6420Sstevel@tonic-gate  */
6430Sstevel@tonic-gate #define	PG_EXCL		0x0001
64412156SStan.Studzinski@Sun.COM #define	PG_WAIT		0x0002		/* Blocking memory allocations */
6450Sstevel@tonic-gate #define	PG_PHYSCONTIG	0x0004		/* NOT SUPPORTED */
6460Sstevel@tonic-gate #define	PG_MATCH_COLOR	0x0008		/* SUPPORTED by free list routines */
6470Sstevel@tonic-gate #define	PG_NORELOC	0x0010		/* Non-relocatable alloc hint. */
6480Sstevel@tonic-gate 					/* Page must be PP_ISNORELOC */
6490Sstevel@tonic-gate #define	PG_PANIC	0x0020		/* system will panic if alloc fails */
6500Sstevel@tonic-gate #define	PG_PUSHPAGE	0x0040		/* alloc may use reserve */
6514426Saguzovsk #define	PG_LOCAL	0x0080		/* alloc from given lgrp only */
65212156SStan.Studzinski@Sun.COM #define	PG_NORMALPRI	0x0100		/* PG_WAIT like priority, but */
65312156SStan.Studzinski@Sun.COM 					/* non-blocking */
6540Sstevel@tonic-gate /*
6550Sstevel@tonic-gate  * When p_selock has the SE_EWANTED bit set, threads waiting for SE_EXCL
6560Sstevel@tonic-gate  * access are given priority over all other waiting threads.
6570Sstevel@tonic-gate  */
6580Sstevel@tonic-gate #define	SE_EWANTED	0x40000000
6590Sstevel@tonic-gate #define	PAGE_LOCKED(pp)		(((pp)->p_selock & ~SE_EWANTED) != 0)
6600Sstevel@tonic-gate #define	PAGE_SHARED(pp)		(((pp)->p_selock & ~SE_EWANTED) > 0)
6610Sstevel@tonic-gate #define	PAGE_EXCL(pp)		((pp)->p_selock < 0)
6620Sstevel@tonic-gate #define	PAGE_LOCKED_SE(pp, se)	\
6630Sstevel@tonic-gate 	((se) == SE_EXCL ? PAGE_EXCL(pp) : PAGE_SHARED(pp))
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate extern	long page_hashsz;
66612230SFrank.Rival@oracle.com extern	unsigned int page_hashsz_shift;
6670Sstevel@tonic-gate extern	page_t **page_hash;
6680Sstevel@tonic-gate 
66912230SFrank.Rival@oracle.com extern	pad_mutex_t page_llocks[];	/* page logical lock mutex */
6700Sstevel@tonic-gate extern	kmutex_t freemem_lock;		/* freemem lock */
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate extern	pgcnt_t	total_pages;		/* total pages in the system */
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate /*
6750Sstevel@tonic-gate  * Variables controlling locking of physical memory.
6760Sstevel@tonic-gate  */
6770Sstevel@tonic-gate extern	pgcnt_t	pages_pp_maximum;	/* tuning: lock + claim <= max */
6780Sstevel@tonic-gate extern	void init_pages_pp_maximum(void);
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate struct lgrp;
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate /* page_list_{add,sub} flags */
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate /* which list */
6850Sstevel@tonic-gate #define	PG_FREE_LIST	0x0001
6860Sstevel@tonic-gate #define	PG_CACHE_LIST	0x0002
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate /* where on list */
6890Sstevel@tonic-gate #define	PG_LIST_TAIL	0x0010
6900Sstevel@tonic-gate #define	PG_LIST_HEAD	0x0020
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate /* called from */
6930Sstevel@tonic-gate #define	PG_LIST_ISINIT	0x1000
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate /*
6960Sstevel@tonic-gate  * Page frame operations.
6970Sstevel@tonic-gate  */
6980Sstevel@tonic-gate page_t	*page_lookup(struct vnode *, u_offset_t, se_t);
6990Sstevel@tonic-gate page_t	*page_lookup_create(struct vnode *, u_offset_t, se_t, page_t *,
7000Sstevel@tonic-gate 	spgcnt_t *, int);
7010Sstevel@tonic-gate page_t	*page_lookup_nowait(struct vnode *, u_offset_t, se_t);
7020Sstevel@tonic-gate page_t	*page_find(struct vnode *, u_offset_t);
7030Sstevel@tonic-gate page_t	*page_exists(struct vnode *, u_offset_t);
7040Sstevel@tonic-gate int	page_exists_physcontig(vnode_t *, u_offset_t, uint_t, page_t *[]);
7050Sstevel@tonic-gate int	page_exists_forreal(struct vnode *, u_offset_t, uint_t *);
7060Sstevel@tonic-gate void	page_needfree(spgcnt_t);
7070Sstevel@tonic-gate page_t	*page_create(struct vnode *, u_offset_t, size_t, uint_t);
708749Ssusans int	page_alloc_pages(struct vnode *, struct seg *, caddr_t, page_t **,
7094426Saguzovsk 	page_t **, uint_t, int, int);
7100Sstevel@tonic-gate page_t  *page_create_va_large(vnode_t *vp, u_offset_t off, size_t bytes,
7110Sstevel@tonic-gate 	uint_t flags, struct seg *seg, caddr_t vaddr, void *arg);
7120Sstevel@tonic-gate page_t	*page_create_va(struct vnode *, u_offset_t, size_t, uint_t,
7130Sstevel@tonic-gate 	struct seg *, caddr_t);
7146880Sdv142724 int	page_create_wait(pgcnt_t npages, uint_t flags);
7156880Sdv142724 void    page_create_putback(spgcnt_t npages);
7160Sstevel@tonic-gate void	page_free(page_t *, int);
7170Sstevel@tonic-gate void	page_free_at_startup(page_t *);
7180Sstevel@tonic-gate void	page_free_pages(page_t *);
7190Sstevel@tonic-gate void	free_vp_pages(struct vnode *, u_offset_t, size_t);
7200Sstevel@tonic-gate int	page_reclaim(page_t *, kmutex_t *);
7213253Smec int	page_reclaim_pages(page_t *, kmutex_t *, uint_t);
7220Sstevel@tonic-gate void	page_destroy(page_t *, int);
7230Sstevel@tonic-gate void	page_destroy_pages(page_t *);
7240Sstevel@tonic-gate void	page_destroy_free(page_t *);
7250Sstevel@tonic-gate void	page_rename(page_t *, struct vnode *, u_offset_t);
7260Sstevel@tonic-gate int	page_hashin(page_t *, struct vnode *, u_offset_t, kmutex_t *);
7270Sstevel@tonic-gate void	page_hashout(page_t *, kmutex_t *);
7280Sstevel@tonic-gate int	page_num_hashin(pfn_t, struct vnode *, u_offset_t);
7290Sstevel@tonic-gate void	page_add(page_t **, page_t *);
7300Sstevel@tonic-gate void	page_add_common(page_t **, page_t *);
7310Sstevel@tonic-gate void	page_sub(page_t **, page_t *);
7320Sstevel@tonic-gate void	page_sub_common(page_t **, page_t *);
7330Sstevel@tonic-gate page_t	*page_get_freelist(struct vnode *, u_offset_t, struct seg *,
7340Sstevel@tonic-gate 		caddr_t, size_t, uint_t, struct lgrp *);
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate page_t	*page_get_cachelist(struct vnode *, u_offset_t, struct seg *,
7370Sstevel@tonic-gate 		caddr_t, uint_t, struct lgrp *);
7385466Skchow #if defined(__i386) || defined(__amd64)
7395466Skchow int	page_chk_freelist(uint_t);
7405466Skchow #endif
7410Sstevel@tonic-gate void	page_list_add(page_t *, int);
7420Sstevel@tonic-gate void	page_boot_demote(page_t *);
7430Sstevel@tonic-gate void	page_promote_size(page_t *, uint_t);
7440Sstevel@tonic-gate void	page_list_add_pages(page_t *, int);
7450Sstevel@tonic-gate void	page_list_sub(page_t *, int);
746917Selowe void	page_list_sub_pages(page_t *, uint_t);
747414Skchow void	page_list_xfer(page_t *, int, int);
7480Sstevel@tonic-gate void	page_list_break(page_t **, page_t **, size_t);
7490Sstevel@tonic-gate void	page_list_concat(page_t **, page_t **);
7500Sstevel@tonic-gate void	page_vpadd(page_t **, page_t *);
7510Sstevel@tonic-gate void	page_vpsub(page_t **, page_t *);
7520Sstevel@tonic-gate int	page_lock(page_t *, se_t, kmutex_t *, reclaim_t);
7530Sstevel@tonic-gate int	page_lock_es(page_t *, se_t, kmutex_t *, reclaim_t, int);
7540Sstevel@tonic-gate void page_lock_clr_exclwanted(page_t *);
7550Sstevel@tonic-gate int	page_trylock(page_t *, se_t);
7560Sstevel@tonic-gate int	page_try_reclaim_lock(page_t *, se_t, int);
7570Sstevel@tonic-gate int	page_tryupgrade(page_t *);
7580Sstevel@tonic-gate void	page_downgrade(page_t *);
7590Sstevel@tonic-gate void	page_unlock(page_t *);
7603253Smec void	page_unlock_nocapture(page_t *);
7610Sstevel@tonic-gate void	page_lock_delete(page_t *);
7623253Smec int	page_deleted(page_t *);
7630Sstevel@tonic-gate int	page_pp_lock(page_t *, int, int);
7640Sstevel@tonic-gate void	page_pp_unlock(page_t *, int, int);
7650Sstevel@tonic-gate int	page_resv(pgcnt_t, uint_t);
7660Sstevel@tonic-gate void	page_unresv(pgcnt_t);
7670Sstevel@tonic-gate void	page_pp_useclaim(page_t *, page_t *, uint_t);
7680Sstevel@tonic-gate int	page_addclaim(page_t *);
7690Sstevel@tonic-gate int	page_subclaim(page_t *);
7700Sstevel@tonic-gate int	page_addclaim_pages(page_t **);
7710Sstevel@tonic-gate int	page_subclaim_pages(page_t **);
7720Sstevel@tonic-gate pfn_t	page_pptonum(page_t *);
7730Sstevel@tonic-gate page_t	*page_numtopp(pfn_t, se_t);
7740Sstevel@tonic-gate page_t	*page_numtopp_noreclaim(pfn_t, se_t);
7750Sstevel@tonic-gate page_t	*page_numtopp_nolock(pfn_t);
7760Sstevel@tonic-gate page_t	*page_numtopp_nowait(pfn_t, se_t);
7770Sstevel@tonic-gate page_t  *page_first();
7780Sstevel@tonic-gate page_t  *page_next(page_t *);
7790Sstevel@tonic-gate page_t  *page_list_next(page_t *);
7800Sstevel@tonic-gate page_t	*page_nextn(page_t *, ulong_t);
7810Sstevel@tonic-gate page_t	*page_next_scan_init(void **);
7820Sstevel@tonic-gate page_t	*page_next_scan_large(page_t *, ulong_t *, void **);
7830Sstevel@tonic-gate void    prefetch_page_r(void *);
7843253Smec int	ppcopy(page_t *, page_t *);
7850Sstevel@tonic-gate void	page_relocate_hash(page_t *, page_t *);
7860Sstevel@tonic-gate void	pagezero(page_t *, uint_t, uint_t);
7870Sstevel@tonic-gate void	pagescrub(page_t *, uint_t, uint_t);
7880Sstevel@tonic-gate void	page_io_lock(page_t *);
7890Sstevel@tonic-gate void	page_io_unlock(page_t *);
7900Sstevel@tonic-gate int	page_io_trylock(page_t *);
7910Sstevel@tonic-gate int	page_iolock_assert(page_t *);
7920Sstevel@tonic-gate void	page_iolock_init(page_t *);
7932999Sstans void	page_io_wait(page_t *);
7942999Sstans int	page_io_locked(page_t *);
7950Sstevel@tonic-gate pgcnt_t	page_busy(int);
7960Sstevel@tonic-gate void	page_lock_init(void);
7970Sstevel@tonic-gate ulong_t	page_share_cnt(page_t *);
7980Sstevel@tonic-gate int	page_isshared(page_t *);
7990Sstevel@tonic-gate int	page_isfree(page_t *);
8000Sstevel@tonic-gate int	page_isref(page_t *);
8010Sstevel@tonic-gate int	page_ismod(page_t *);
8020Sstevel@tonic-gate int	page_release(page_t *, int);
803917Selowe void	page_retire_init(void);
804917Selowe int	page_retire(uint64_t, uchar_t);
805917Selowe int	page_retire_check(uint64_t, uint64_t *);
806917Selowe int	page_unretire(uint64_t);
807917Selowe int	page_unretire_pp(page_t *, int);
808917Selowe void	page_tryretire(page_t *);
8093253Smec void	page_retire_mdboot();
8103480Sjfrank uint64_t	page_retire_pend_count(void);
8119544SChristopher.Baumbauer@Sun.COM uint64_t	page_retire_pend_kas_count(void);
8129544SChristopher.Baumbauer@Sun.COM void	page_retire_incr_pend_count(void *);
8139544SChristopher.Baumbauer@Sun.COM void	page_retire_decr_pend_count(void *);
814917Selowe void	page_clrtoxic(page_t *, uchar_t);
8150Sstevel@tonic-gate void	page_settoxic(page_t *, uchar_t);
816917Selowe 
8170Sstevel@tonic-gate int	page_mem_avail(pgcnt_t);
81813035SOndrej.Kubecka@Sun.COM int	page_reclaim_mem(pgcnt_t, pgcnt_t, int);
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate void page_set_props(page_t *, uint_t);
82110271SJason.Beloro@Sun.COM void page_clr_all_props(page_t *);
822917Selowe int page_clear_lck_cow(page_t *, int);
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate kmutex_t	*page_vnode_mutex(struct vnode *);
8250Sstevel@tonic-gate kmutex_t	*page_se_mutex(struct page *);
8260Sstevel@tonic-gate kmutex_t	*page_szc_lock(struct page *);
8270Sstevel@tonic-gate int		page_szc_lock_assert(struct page *pp);
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate /*
8300Sstevel@tonic-gate  * Page relocation interfaces. page_relocate() is generic.
8310Sstevel@tonic-gate  * page_get_replacement_page() is provided by the PSM.
8320Sstevel@tonic-gate  * page_free_replacement_page() is generic.
8330Sstevel@tonic-gate  */
8340Sstevel@tonic-gate int group_page_trylock(page_t *, se_t);
8350Sstevel@tonic-gate void group_page_unlock(page_t *);
8360Sstevel@tonic-gate int page_relocate(page_t **, page_t **, int, int, spgcnt_t *, struct lgrp *);
8370Sstevel@tonic-gate int do_page_relocate(page_t **, page_t **, int, spgcnt_t *, struct lgrp *);
8380Sstevel@tonic-gate page_t *page_get_replacement_page(page_t *, struct lgrp *, uint_t);
8390Sstevel@tonic-gate void page_free_replacement_page(page_t *);
8400Sstevel@tonic-gate int page_relocate_cage(page_t **, page_t **);
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate int page_try_demote_pages(page_t *);
843917Selowe int page_try_demote_free_pages(page_t *);
8440Sstevel@tonic-gate void page_demote_free_pages(page_t *);
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate struct anon_map;
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate void page_mark_migrate(struct seg *, caddr_t, size_t, struct anon_map *,
8490Sstevel@tonic-gate     ulong_t, vnode_t *, u_offset_t, int);
8500Sstevel@tonic-gate void page_migrate(struct seg *, caddr_t, page_t **, pgcnt_t);
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate /*
8530Sstevel@tonic-gate  * Tell the PIM we are adding physical memory
8540Sstevel@tonic-gate  */
8550Sstevel@tonic-gate void add_physmem(page_t *, size_t, pfn_t);
8560Sstevel@tonic-gate void add_physmem_cb(page_t *, pfn_t);	/* callback for page_t part */
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate /*
8590Sstevel@tonic-gate  * hw_page_array[] is configured with hardware supported page sizes by
8600Sstevel@tonic-gate  * platform specific code.
8610Sstevel@tonic-gate  */
8620Sstevel@tonic-gate typedef struct {
8630Sstevel@tonic-gate 	size_t	hp_size;
8640Sstevel@tonic-gate 	uint_t	hp_shift;
8652961Sdp78419 	uint_t  hp_colors;
8660Sstevel@tonic-gate 	pgcnt_t	hp_pgcnt;	/* base pagesize cnt */
8670Sstevel@tonic-gate } hw_pagesize_t;
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate extern hw_pagesize_t	hw_page_array[];
8700Sstevel@tonic-gate extern uint_t		page_coloring_shift;
8712961Sdp78419 extern uint_t		page_colors_mask;
8720Sstevel@tonic-gate extern int		cpu_page_colors;
8732961Sdp78419 extern uint_t		colorequiv;
8742961Sdp78419 extern uchar_t		colorequivszc[];
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate uint_t	page_num_pagesizes(void);
8775349Skchow uint_t	page_num_user_pagesizes(int);
8780Sstevel@tonic-gate size_t	page_get_pagesize(uint_t);
8790Sstevel@tonic-gate size_t	page_get_user_pagesize(uint_t n);
8800Sstevel@tonic-gate pgcnt_t	page_get_pagecnt(uint_t);
8810Sstevel@tonic-gate uint_t	page_get_shift(uint_t);
8820Sstevel@tonic-gate int	page_szc(size_t);
88373Smec int	page_szc_user_filtered(size_t);
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate /* page_get_replacement page flags */
8860Sstevel@tonic-gate #define	PGR_SAMESZC	0x1	/* only look for page size same as orig */
8870Sstevel@tonic-gate #define	PGR_NORELOC	0x2	/* allocate a P_NORELOC page */
8880Sstevel@tonic-gate 
8892961Sdp78419 /*
8902961Sdp78419  * macros for "masked arithmetic"
8912961Sdp78419  * The purpose is to step through all combinations of a set of bits while
8922961Sdp78419  * keeping some other bits fixed. Fixed bits need not be contiguous. The
8932961Sdp78419  * variable bits need not be contiguous either, or even right aligned. The
8942961Sdp78419  * trick is to set all fixed bits to 1, then increment, then restore the
8952961Sdp78419  * fixed bits. If incrementing causes a carry from a low bit position, the
8962961Sdp78419  * carry propagates thru the fixed bits, because they are temporarily set to 1.
8972961Sdp78419  *	v is the value
8982961Sdp78419  *	i is the increment
8992961Sdp78419  *	eq_mask defines the fixed bits
9002961Sdp78419  *	mask limits the size of the result
9012961Sdp78419  */
9022961Sdp78419 #define	ADD_MASKED(v, i, eq_mask, mask) \
9032961Sdp78419 	(((((v) | (eq_mask)) + (i)) & (mask) & ~(eq_mask)) | ((v) & (eq_mask)))
9042961Sdp78419 
9052961Sdp78419 /*
9062961Sdp78419  * convenience macro which increments by 1
9072961Sdp78419  */
9082961Sdp78419 #define	INC_MASKED(v, eq_mask, mask) ADD_MASKED(v, 1, eq_mask, mask)
9092961Sdp78419 
9100Sstevel@tonic-gate #endif	/* _KERNEL */
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate /*
9130Sstevel@tonic-gate  * Constants used for the p_iolock_state
9140Sstevel@tonic-gate  */
9150Sstevel@tonic-gate #define	PAGE_IO_INUSE	0x1
9160Sstevel@tonic-gate #define	PAGE_IO_WANTED	0x2
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate /*
9190Sstevel@tonic-gate  * Constants used for page_release status
9200Sstevel@tonic-gate  */
9210Sstevel@tonic-gate #define	PGREL_NOTREL    0x1
9220Sstevel@tonic-gate #define	PGREL_CLEAN	0x2
9230Sstevel@tonic-gate #define	PGREL_MOD	0x3
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate /*
9260Sstevel@tonic-gate  * The p_state field holds what used to be the p_age and p_free
9270Sstevel@tonic-gate  * bits.  These fields are protected by p_selock (see above).
9280Sstevel@tonic-gate  */
9290Sstevel@tonic-gate #define	P_FREE		0x80		/* Page on free list */
9300Sstevel@tonic-gate #define	P_NORELOC	0x40		/* Page is non-relocatable */
9310Sstevel@tonic-gate #define	P_MIGRATE	0x20		/* Migrate page on next touch */
9320Sstevel@tonic-gate #define	P_SWAP		0x10		/* belongs to vnode that is V_ISSWAP */
9333446Smrj #define	P_BOOTPAGES	0x08		/* member of bootpages list */
93412117SStan.Studzinski@Sun.COM #define	P_RAF		0x04		/* page retired at free */
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate #define	PP_ISFREE(pp)		((pp)->p_state & P_FREE)
9370Sstevel@tonic-gate #define	PP_ISAGED(pp)		(((pp)->p_state & P_FREE) && \
9380Sstevel@tonic-gate 					((pp)->p_vnode == NULL))
9390Sstevel@tonic-gate #define	PP_ISNORELOC(pp)	((pp)->p_state & P_NORELOC)
94011185SSean.McEnroe@Sun.COM #define	PP_ISKAS(pp)		(VN_ISKAS((pp)->p_vnode))
9413290Sjohansen #define	PP_ISNORELOCKERNEL(pp)	(PP_ISNORELOC(pp) && PP_ISKAS(pp))
9420Sstevel@tonic-gate #define	PP_ISMIGRATE(pp)	((pp)->p_state & P_MIGRATE)
9430Sstevel@tonic-gate #define	PP_ISSWAP(pp)		((pp)->p_state & P_SWAP)
9443446Smrj #define	PP_ISBOOTPAGES(pp)	((pp)->p_state & P_BOOTPAGES)
94512117SStan.Studzinski@Sun.COM #define	PP_ISRAF(pp)		((pp)->p_state & P_RAF)
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate #define	PP_SETFREE(pp)		((pp)->p_state = ((pp)->p_state & ~P_MIGRATE) \
9480Sstevel@tonic-gate 				| P_FREE)
9490Sstevel@tonic-gate #define	PP_SETAGED(pp)		ASSERT(PP_ISAGED(pp))
9500Sstevel@tonic-gate #define	PP_SETNORELOC(pp)	((pp)->p_state |= P_NORELOC)
9510Sstevel@tonic-gate #define	PP_SETMIGRATE(pp)	((pp)->p_state |= P_MIGRATE)
9520Sstevel@tonic-gate #define	PP_SETSWAP(pp)		((pp)->p_state |= P_SWAP)
9533446Smrj #define	PP_SETBOOTPAGES(pp)	((pp)->p_state |= P_BOOTPAGES)
95412117SStan.Studzinski@Sun.COM #define	PP_SETRAF(pp)		((pp)->p_state |= P_RAF)
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate #define	PP_CLRFREE(pp)		((pp)->p_state &= ~P_FREE)
9570Sstevel@tonic-gate #define	PP_CLRAGED(pp)		ASSERT(!PP_ISAGED(pp))
9580Sstevel@tonic-gate #define	PP_CLRNORELOC(pp)	((pp)->p_state &= ~P_NORELOC)
9590Sstevel@tonic-gate #define	PP_CLRMIGRATE(pp)	((pp)->p_state &= ~P_MIGRATE)
9600Sstevel@tonic-gate #define	PP_CLRSWAP(pp)		((pp)->p_state &= ~P_SWAP)
9613446Smrj #define	PP_CLRBOOTPAGES(pp)	((pp)->p_state &= ~P_BOOTPAGES)
96212117SStan.Studzinski@Sun.COM #define	PP_CLRRAF(pp)		((pp)->p_state &= ~P_RAF)
9630Sstevel@tonic-gate 
964917Selowe /*
965917Selowe  * Flags for page_t p_toxic, for tracking memory hardware errors.
966917Selowe  *
967917Selowe  * These flags are OR'ed into p_toxic with page_settoxic() to track which
968917Selowe  * error(s) have occurred on a given page. The flags are cleared with
969917Selowe  * page_clrtoxic(). Both page_settoxic() and page_cleartoxic use atomic
970917Selowe  * primitives to manipulate the p_toxic field so no other locking is needed.
971917Selowe  *
972917Selowe  * When an error occurs on a page, p_toxic is set to record the error. The
973917Selowe  * error could be a memory error or something else (i.e. a datapath). The Page
974917Selowe  * Retire mechanism does not try to determine the exact cause of the error;
975917Selowe  * Page Retire rightly leaves that sort of determination to FMA's Diagnostic
976917Selowe  * Engine (DE).
977917Selowe  *
978917Selowe  * Note that, while p_toxic bits can be set without holding any locks, they
979917Selowe  * should only be cleared while holding the page exclusively locked.
9803253Smec  * There is one exception to this, the PR_CAPTURE bit is protected by a mutex
9813253Smec  * within the page capture logic and thus to set or clear the bit, that mutex
9823253Smec  * needs to be held.  The page does not need to be locked but the page_clrtoxic
9833253Smec  * function must be used as we need an atomic operation.
9843253Smec  * Also note that there is what amounts to a hack to prevent recursion with
9853253Smec  * large pages such that if we are unlocking a page and the PR_CAPTURE bit is
9863253Smec  * set, we will only try to capture the page if the current threads T_CAPTURING
9873253Smec  * flag is not set.  If the flag is set, the unlock will not try to capture
9883253Smec  * the page even though the PR_CAPTURE bit is set.
989917Selowe  *
990917Selowe  * Pages with PR_UE or PR_FMA flags are retired unconditionally, while pages
991917Selowe  * with PR_MCE are retired if the system has not retired too many of them.
992917Selowe  *
993917Selowe  * A page must be exclusively locked to be retired. Pages can be retired if
994917Selowe  * they are mapped, modified, or both, as long as they are not marked PR_UE,
995917Selowe  * since pages with uncorrectable errors cannot be relocated in memory.
996917Selowe  * Once a page has been successfully retired it is zeroed, attached to the
997917Selowe  * retired_pages vnode and, finally, PR_RETIRED is set in p_toxic. The other
998917Selowe  * p_toxic bits are NOT cleared. Pages are not left locked after retiring them
999917Selowe  * to avoid special case code throughout the kernel; rather, page_*lock() will
1000917Selowe  * fail to lock the page, unless SE_RETIRED is passed as an argument.
1001917Selowe  *
1002917Selowe  * While we have your attention, go take a look at the comments at the
1003917Selowe  * beginning of page_retire.c too.
1004917Selowe  */
1005917Selowe #define	PR_OK		0x00	/* no problem */
1006917Selowe #define	PR_MCE		0x01	/* page has seen two or more CEs */
1007917Selowe #define	PR_UE		0x02	/* page has an unhandled UE */
1008917Selowe #define	PR_UE_SCRUBBED	0x04	/* page has seen a UE but was cleaned */
1009917Selowe #define	PR_FMA		0x08	/* A DE wants this page retired */
101012156SStan.Studzinski@Sun.COM #define	PR_CAPTURE	0x10	/* page is hashed on page_capture_hash[] */
10113253Smec #define	PR_RESV		0x20	/* Reserved for future use */
1012917Selowe #define	PR_MSG		0x40	/* message(s) already printed for this page */
1013917Selowe #define	PR_RETIRED	0x80	/* This page has been retired */
10140Sstevel@tonic-gate 
1015917Selowe #define	PR_REASONS	(PR_UE | PR_MCE | PR_FMA)
1016917Selowe #define	PR_TOXIC	(PR_UE)
1017917Selowe #define	PR_ERRMASK	(PR_UE | PR_UE_SCRUBBED | PR_MCE | PR_FMA)
10183253Smec #define	PR_TOXICFLAGS	(0xCF)
1019917Selowe 
1020917Selowe #define	PP_RETIRED(pp)	((pp)->p_toxic & PR_RETIRED)
1021917Selowe #define	PP_TOXIC(pp)	((pp)->p_toxic & PR_TOXIC)
1022917Selowe #define	PP_PR_REQ(pp)	(((pp)->p_toxic & PR_REASONS) && !PP_RETIRED(pp))
1023973Selowe #define	PP_PR_NOSHARE(pp)						\
1024973Selowe 	((((pp)->p_toxic & (PR_RETIRED | PR_FMA | PR_UE)) == PR_FMA) &&	\
10253290Sjohansen 	!PP_ISKAS(pp))
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate /*
10283253Smec  * Flags for page_unretire_pp
10293253Smec  */
10303253Smec #define	PR_UNR_FREE	0x1
10313253Smec #define	PR_UNR_CLEAN	0x2
10323253Smec #define	PR_UNR_TEMP	0x4
10333253Smec 
10343253Smec /*
10350Sstevel@tonic-gate  * kpm large page description.
10360Sstevel@tonic-gate  * The virtual address range of segkpm is divided into chunks of
10370Sstevel@tonic-gate  * kpm_pgsz. Each chunk is controlled by a kpm_page_t. The ushort
10380Sstevel@tonic-gate  * is sufficient for 2^^15 * PAGESIZE, so e.g. the maximum kpm_pgsz
10390Sstevel@tonic-gate  * for 8K is 256M and 2G for 64K pages. It it kept as small as
10400Sstevel@tonic-gate  * possible to save physical memory space.
10410Sstevel@tonic-gate  *
10420Sstevel@tonic-gate  * There are 2 segkpm mapping windows within in the virtual address
10430Sstevel@tonic-gate  * space when we have to prevent VAC alias conflicts. The so called
10440Sstevel@tonic-gate  * Alias window (mappings are always by PAGESIZE) is controlled by
10450Sstevel@tonic-gate  * kp_refcnta. The regular window is controlled by kp_refcnt for the
10460Sstevel@tonic-gate  * normal operation, which is to use the largest available pagesize.
10470Sstevel@tonic-gate  * When VAC alias conflicts are present within a chunk in the regular
10480Sstevel@tonic-gate  * window the large page mapping is broken up into smaller PAGESIZE
10490Sstevel@tonic-gate  * mappings. kp_refcntc is used to control the pages that are invoked
10500Sstevel@tonic-gate  * in the conflict and kp_refcnts holds the active mappings done
10510Sstevel@tonic-gate  * with the small page size. In non vac conflict mode kp_refcntc is
10520Sstevel@tonic-gate  * also used as "go" indication (-1) for the trap level tsbmiss
10530Sstevel@tonic-gate  * handler.
10540Sstevel@tonic-gate  */
10550Sstevel@tonic-gate typedef struct kpm_page {
10560Sstevel@tonic-gate 	short kp_refcnt;	/* pages mapped large */
10570Sstevel@tonic-gate 	short kp_refcnta;	/* pages mapped in Alias window */
10580Sstevel@tonic-gate 	short kp_refcntc;	/* TL-tsbmiss flag; #vac alias conflict pages */
10590Sstevel@tonic-gate 	short kp_refcnts;	/* vac alias: pages mapped small */
10600Sstevel@tonic-gate } kpm_page_t;
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate /*
10630Sstevel@tonic-gate  * Note: khl_lock offset changes must be reflected in sfmmu_asm.s
10640Sstevel@tonic-gate  */
10650Sstevel@tonic-gate typedef struct kpm_hlk {
10660Sstevel@tonic-gate 	kmutex_t khl_mutex;	/* kpm_page mutex */
10670Sstevel@tonic-gate 	uint_t   khl_lock;	/* trap level tsbmiss handling */
10680Sstevel@tonic-gate } kpm_hlk_t;
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate /*
10710Sstevel@tonic-gate  * kpm small page description.
10720Sstevel@tonic-gate  * When kpm_pgsz is equal to PAGESIZE a smaller representation is used
10730Sstevel@tonic-gate  * to save memory space. Alias range mappings and regular segkpm
10740Sstevel@tonic-gate  * mappings are done in units of PAGESIZE and can share the mapping
10750Sstevel@tonic-gate  * information and the mappings are always distinguishable by their
10767393SDonghai.Qiao@Sun.COM  * virtual address. Other information needed for VAC conflict prevention
10777393SDonghai.Qiao@Sun.COM  * is already available on a per page basis.
10787393SDonghai.Qiao@Sun.COM  *
10797393SDonghai.Qiao@Sun.COM  * The state about how a kpm page is mapped and whether it is ready to go
10807393SDonghai.Qiao@Sun.COM  * is indicated by the following 1 byte kpm_spage structure. This byte is
10817393SDonghai.Qiao@Sun.COM  * split into two 4-bit parts - kp_mapped and kp_mapped_go.
10827393SDonghai.Qiao@Sun.COM  * 	- kp_mapped == 1	the page is mapped cacheable
10837393SDonghai.Qiao@Sun.COM  *	- kp_mapped == 2	the page is mapped non-cacheable
10847393SDonghai.Qiao@Sun.COM  *	- kp_mapped_go == 1	the mapping is ready to be dropped in
10857393SDonghai.Qiao@Sun.COM  *	- kp_mapped_go == 0	the mapping is not ready to be dropped in.
10867393SDonghai.Qiao@Sun.COM  * When kp_mapped_go == 0, we will have C handler resolve the VAC conflict.
10877393SDonghai.Qiao@Sun.COM  * Otherwise, the assembly tsb miss handler can simply drop in the mapping
10887393SDonghai.Qiao@Sun.COM  * when a tsb miss occurs.
10890Sstevel@tonic-gate  */
10907393SDonghai.Qiao@Sun.COM typedef union kpm_spage {
10917393SDonghai.Qiao@Sun.COM 	struct {
10927393SDonghai.Qiao@Sun.COM #ifdef  _BIG_ENDIAN
10937393SDonghai.Qiao@Sun.COM 		uchar_t mapped_go: 4;	/* go or nogo flag */
10947393SDonghai.Qiao@Sun.COM 		uchar_t mapped: 4;	/* page mapped small */
10957393SDonghai.Qiao@Sun.COM #else
10967393SDonghai.Qiao@Sun.COM 		uchar_t mapped: 4;	/* page mapped small */
10977393SDonghai.Qiao@Sun.COM 		uchar_t mapped_go: 4;	/* go or nogo flag */
10987393SDonghai.Qiao@Sun.COM #endif
10997393SDonghai.Qiao@Sun.COM 	} kpm_spage_un;
11007393SDonghai.Qiao@Sun.COM 	uchar_t kp_mapped_flag;
11010Sstevel@tonic-gate } kpm_spage_t;
11020Sstevel@tonic-gate 
11037393SDonghai.Qiao@Sun.COM #define	kp_mapped	kpm_spage_un.mapped
11047393SDonghai.Qiao@Sun.COM #define	kp_mapped_go	kpm_spage_un.mapped_go
11057393SDonghai.Qiao@Sun.COM 
11060Sstevel@tonic-gate /*
11070Sstevel@tonic-gate  * Note: kshl_lock offset changes must be reflected in sfmmu_asm.s
11080Sstevel@tonic-gate  */
11090Sstevel@tonic-gate typedef struct kpm_shlk {
11100Sstevel@tonic-gate 	uint_t   kshl_lock;	/* trap level tsbmiss handling */
11110Sstevel@tonic-gate } kpm_shlk_t;
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate /*
11140Sstevel@tonic-gate  * Each segment of physical memory is described by a memseg struct.
11150Sstevel@tonic-gate  * Within a segment, memory is considered contiguous. The members
11160Sstevel@tonic-gate  * can be categorized as follows:
11170Sstevel@tonic-gate  * . Platform independent:
11180Sstevel@tonic-gate  *         pages, epages, pages_base, pages_end, next, lnext.
11190Sstevel@tonic-gate  * . 64bit only but platform independent:
11200Sstevel@tonic-gate  *         kpm_pbase, kpm_nkpmpgs, kpm_pages, kpm_spages.
11210Sstevel@tonic-gate  * . Really platform or mmu specific:
11220Sstevel@tonic-gate  *         pagespa, epagespa, nextpa, kpm_pagespa.
11230Sstevel@tonic-gate  * . Mixed:
11240Sstevel@tonic-gate  *         msegflags.
11250Sstevel@tonic-gate  */
11260Sstevel@tonic-gate struct memseg {
11270Sstevel@tonic-gate 	page_t *pages, *epages;		/* [from, to] in page array */
11280Sstevel@tonic-gate 	pfn_t pages_base, pages_end;	/* [from, to] in page numbers */
11290Sstevel@tonic-gate 	struct memseg *next;		/* next segment in list */
113012004Sjiang.liu@intel.com 	struct memseg *lnext;		/* next segment in deleted list */
11310Sstevel@tonic-gate #if defined(__sparc)
11320Sstevel@tonic-gate 	uint64_t pagespa, epagespa;	/* [from, to] page array physical */
11330Sstevel@tonic-gate 	uint64_t nextpa;		/* physical next pointer */
11340Sstevel@tonic-gate 	pfn_t	kpm_pbase;		/* start of kpm range */
11350Sstevel@tonic-gate 	pgcnt_t kpm_nkpmpgs;		/* # of kpm_pgsz pages */
11360Sstevel@tonic-gate 	union _mseg_un {
11370Sstevel@tonic-gate 		kpm_page_t  *kpm_lpgs;	/* ptr to kpm_page array */
11380Sstevel@tonic-gate 		kpm_spage_t *kpm_spgs;	/* ptr to kpm_spage array */
11390Sstevel@tonic-gate 	} mseg_un;
11400Sstevel@tonic-gate 	uint64_t kpm_pagespa;		/* physical ptr to kpm (s)pages array */
114112004Sjiang.liu@intel.com #endif /* __sparc */
11420Sstevel@tonic-gate 	uint_t msegflags;		/* memseg flags */
11430Sstevel@tonic-gate };
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate /* memseg union aliases */
11460Sstevel@tonic-gate #define	kpm_pages	mseg_un.kpm_lpgs
11470Sstevel@tonic-gate #define	kpm_spages	mseg_un.kpm_spgs
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate /* msegflags */
11500Sstevel@tonic-gate #define	MEMSEG_DYNAMIC		0x1	/* DR: memory was added dynamically */
115110106SJason.Beloro@Sun.COM #define	MEMSEG_META_INCL	0x2	/* DR: memseg includes it's metadata */
115210106SJason.Beloro@Sun.COM #define	MEMSEG_META_ALLOC	0x4	/* DR: memseg allocated it's metadata */
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate /* memseg support macros */
11550Sstevel@tonic-gate #define	MSEG_NPAGES(SEG)	((SEG)->pages_end - (SEG)->pages_base)
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate /* memseg hash */
11580Sstevel@tonic-gate #define	MEM_HASH_SHIFT		0x9
11590Sstevel@tonic-gate #define	N_MEM_SLOTS		0x200		/* must be a power of 2 */
11600Sstevel@tonic-gate #define	MEMSEG_PFN_HASH(pfn)	(((pfn)/mhash_per_slot) & (N_MEM_SLOTS - 1))
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate /* memseg  externals */
11630Sstevel@tonic-gate extern struct memseg *memsegs;		/* list of memory segments */
11640Sstevel@tonic-gate extern ulong_t mhash_per_slot;
11650Sstevel@tonic-gate extern uint64_t memsegspa;		/* memsegs as physical address */
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate void build_pfn_hash();
11680Sstevel@tonic-gate extern struct memseg *page_numtomemseg_nolock(pfn_t pfnum);
11690Sstevel@tonic-gate 
11703253Smec /*
11713253Smec  * page capture related info:
11723253Smec  * The page capture routines allow us to asynchronously capture given pages
11733253Smec  * for the explicit use of the requestor.  New requestors can be added by
11743253Smec  * explicitly adding themselves to the PC_* flags below and incrementing
11753253Smec  * PC_NUM_CALLBACKS as necessary.
11763253Smec  *
11773253Smec  * Subsystems using page capture must register a callback before attempting
11783253Smec  * to capture a page.  A duration of -1 will indicate that we will never give
11793253Smec  * up while trying to capture a page and will only stop trying to capture the
11803253Smec  * given page once we have successfully captured it.  Thus the user needs to be
11813253Smec  * aware of the behavior of all callers who have a duration of -1.
11823253Smec  *
11833253Smec  * For now, only /dev/physmem and page retire use the page capture interface
11843253Smec  * and only a single request can be outstanding for a given page.  Thus, if
11853253Smec  * /dev/phsymem wants a page and page retire also wants the same page, only
11863253Smec  * the page retire request will be honored until the point in time that the
11873253Smec  * page is actually retired, at which point in time, subsequent requests by
11883253Smec  * /dev/physmem will succeed if the CAPTURE_GET_RETIRED flag was set.
11893253Smec  */
11903253Smec 
11913253Smec #define	PC_RETIRE		(0)
11923253Smec #define	PC_PHYSMEM		(1)
11933253Smec #define	PC_NUM_CALLBACKS	(2)
11943253Smec #define	PC_MASK			((1 << PC_NUM_CALLBACKS) - 1)
11953253Smec 
11963253Smec #define	CAPTURE_RETIRE		(1 << PC_RETIRE)
11973253Smec #define	CAPTURE_PHYSMEM		(1 << PC_PHYSMEM)
11983253Smec 
11993253Smec #define	CAPTURE_ASYNC		(0x0200)
12003253Smec 
12013253Smec #define	CAPTURE_GET_RETIRED	(0x1000)
12023253Smec #define	CAPTURE_GET_CAGE	(0x2000)
12033253Smec 
12043253Smec struct page_capture_callback {
12053253Smec 	int cb_active;		/* 1 means active, 0 means inactive */
12063253Smec 	clock_t duration;	/* the length in time that we'll attempt to */
12073253Smec 				/* capture this page asynchronously. (in HZ) */
12083253Smec 	krwlock_t cb_rwlock;
12093253Smec 	int (*cb_func)(page_t *, void *, uint_t); /* callback function */
12103253Smec };
12113253Smec 
12123253Smec extern kcondvar_t pc_cv;
12133253Smec 
12143253Smec void page_capture_register_callback(uint_t index, clock_t duration,
12153253Smec     int (*cb_func)(page_t *, void *, uint_t));
12163253Smec void page_capture_unregister_callback(uint_t index);
12173253Smec int page_trycapture(page_t *pp, uint_t szc, uint_t flags, void *datap);
12183253Smec void page_unlock_capture(page_t *pp);
12193253Smec int page_capture_unretire_pp(page_t *);
12203253Smec 
122111185SSean.McEnroe@Sun.COM extern int memsegs_trylock(int);
12223446Smrj extern void memsegs_lock(int);
12233446Smrj extern void memsegs_unlock(int);
12243446Smrj extern int memsegs_lock_held(void);
12253446Smrj extern void memlist_read_lock(void);
12263446Smrj extern void memlist_read_unlock(void);
12273446Smrj extern void memlist_write_lock(void);
12283446Smrj extern void memlist_write_unlock(void);
12293446Smrj 
12300Sstevel@tonic-gate #ifdef	__cplusplus
12310Sstevel@tonic-gate }
12320Sstevel@tonic-gate #endif
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate #endif	/* _VM_PAGE_H */
1235