xref: /titanic_50/usr/src/uts/common/vm/page.h (revision affbd3ccca8e26191a210ec9f9ffae170f919afd)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate  * The Regents of the University of California
337c478bd9Sstevel@tonic-gate  * All Rights Reserved
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate  * contributors.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #ifndef	_VM_PAGE_H
417c478bd9Sstevel@tonic-gate #define	_VM_PAGE_H
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include <vm/seg.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
487c478bd9Sstevel@tonic-gate extern "C" {
497c478bd9Sstevel@tonic-gate #endif
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #if defined(_KERNEL) || defined(_KMEMUSER)
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * Shared/Exclusive lock.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate  * Types of page locking supported by page_lock & friends.
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate typedef enum {
617c478bd9Sstevel@tonic-gate 	SE_SHARED,
627c478bd9Sstevel@tonic-gate 	SE_EXCL			/* exclusive lock (value == -1) */
637c478bd9Sstevel@tonic-gate } se_t;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * For requesting that page_lock reclaim the page from the free list.
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate typedef enum {
697c478bd9Sstevel@tonic-gate 	P_RECLAIM,		/* reclaim page from free list */
707c478bd9Sstevel@tonic-gate 	P_NO_RECLAIM		/* DON`T reclaim the page	*/
717c478bd9Sstevel@tonic-gate } reclaim_t;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * Callers of page_try_reclaim_lock and page_lock_es can use this flag
757c478bd9Sstevel@tonic-gate  * to get SE_EXCL access before reader/writers are given access.
767c478bd9Sstevel@tonic-gate  */
777c478bd9Sstevel@tonic-gate #define	SE_EXCL_WANTED	0x02
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #endif	/* _KERNEL | _KMEMUSER */
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate typedef int	selock_t;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate  * Define VM_STATS to turn on all sorts of statistic gathering about
857c478bd9Sstevel@tonic-gate  * the VM layer.  By default, it is only turned on when DEBUG is
867c478bd9Sstevel@tonic-gate  * also defined.
877c478bd9Sstevel@tonic-gate  */
887c478bd9Sstevel@tonic-gate #ifdef DEBUG
897c478bd9Sstevel@tonic-gate #define	VM_STATS
907c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate #ifdef VM_STATS
937c478bd9Sstevel@tonic-gate #define	VM_STAT_ADD(stat)			(stat)++
947c478bd9Sstevel@tonic-gate #define	VM_STAT_COND_ADD(cond, stat)		((void) (!(cond) || (stat)++))
957c478bd9Sstevel@tonic-gate #else
967c478bd9Sstevel@tonic-gate #define	VM_STAT_ADD(stat)
977c478bd9Sstevel@tonic-gate #define	VM_STAT_COND_ADD(cond, stat)
987c478bd9Sstevel@tonic-gate #endif	/* VM_STATS */
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate #ifdef _KERNEL
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate  * Macros to acquire and release the page logical lock.
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate #define	page_struct_lock(pp)	mutex_enter(&page_llock)
1067c478bd9Sstevel@tonic-gate #define	page_struct_unlock(pp)	mutex_exit(&page_llock)
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate struct as;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate  * Each physical page has a page structure, which is used to maintain
1167c478bd9Sstevel@tonic-gate  * these pages as a cache.  A page can be found via a hashed lookup
1177c478bd9Sstevel@tonic-gate  * based on the [vp, offset].  If a page has an [vp, offset] identity,
1187c478bd9Sstevel@tonic-gate  * then it is entered on a doubly linked circular list off the
1197c478bd9Sstevel@tonic-gate  * vnode using the vpnext/vpprev pointers.   If the p_free bit
1207c478bd9Sstevel@tonic-gate  * is on, then the page is also on a doubly linked circular free
1217c478bd9Sstevel@tonic-gate  * list using next/prev pointers.  If the "p_selock" and "p_iolock"
1227c478bd9Sstevel@tonic-gate  * are held, then the page is currently being read in (exclusive p_selock)
1237c478bd9Sstevel@tonic-gate  * or written back (shared p_selock).  In this case, the next/prev pointers
1247c478bd9Sstevel@tonic-gate  * are used to link the pages together for a consecutive i/o request.  If
1257c478bd9Sstevel@tonic-gate  * the page is being brought in from its backing store, then other processes
1267c478bd9Sstevel@tonic-gate  * will wait for the i/o to complete before attaching to the page since it
1277c478bd9Sstevel@tonic-gate  * will have an "exclusive" lock.
1287c478bd9Sstevel@tonic-gate  *
1297c478bd9Sstevel@tonic-gate  * Each page structure has the locks described below along with
1307c478bd9Sstevel@tonic-gate  * the fields they protect:
1317c478bd9Sstevel@tonic-gate  *
1327c478bd9Sstevel@tonic-gate  *	p_selock	This is a per-page shared/exclusive lock that is
1337c478bd9Sstevel@tonic-gate  *			used to implement the logical shared/exclusive
1347c478bd9Sstevel@tonic-gate  *			lock for each page.  The "shared" lock is normally
1357c478bd9Sstevel@tonic-gate  *			used in most cases while the "exclusive" lock is
1367c478bd9Sstevel@tonic-gate  *			required to destroy or retain exclusive access to
1377c478bd9Sstevel@tonic-gate  *			a page (e.g., while reading in pages).  The appropriate
1387c478bd9Sstevel@tonic-gate  *			lock is always held whenever there is any reference
1397c478bd9Sstevel@tonic-gate  *			to a page structure (e.g., during i/o).
1407c478bd9Sstevel@tonic-gate  *			(Note that with the addition of the "writer-lock-wanted"
1417c478bd9Sstevel@tonic-gate  *			semantics (via SE_EWANTED), threads must not acquire
1427c478bd9Sstevel@tonic-gate  *			multiple reader locks or else a deadly embrace will
1437c478bd9Sstevel@tonic-gate  *			occur in the following situation: thread 1 obtains a
1447c478bd9Sstevel@tonic-gate  *			reader lock; next thread 2 fails to get a writer lock
1457c478bd9Sstevel@tonic-gate  *			but specified SE_EWANTED so it will wait by either
1467c478bd9Sstevel@tonic-gate  *			blocking (when using page_lock_es) or spinning while
1477c478bd9Sstevel@tonic-gate  *			retrying (when using page_try_reclaim_lock) until the
1487c478bd9Sstevel@tonic-gate  *			reader lock is released; then thread 1 attempts to
1497c478bd9Sstevel@tonic-gate  *			get another reader lock but is denied due to
1507c478bd9Sstevel@tonic-gate  *			SE_EWANTED being set, and now both threads are in a
1517c478bd9Sstevel@tonic-gate  *			deadly embrace.)
1527c478bd9Sstevel@tonic-gate  *
1537c478bd9Sstevel@tonic-gate  *				p_hash
1547c478bd9Sstevel@tonic-gate  *				p_vnode
1557c478bd9Sstevel@tonic-gate  *				p_offset
1567c478bd9Sstevel@tonic-gate  *
1577c478bd9Sstevel@tonic-gate  *				p_free
1587c478bd9Sstevel@tonic-gate  *				p_age
1597c478bd9Sstevel@tonic-gate  *
1607c478bd9Sstevel@tonic-gate  *	p_iolock	This is a binary semaphore lock that provides
1617c478bd9Sstevel@tonic-gate  *			exclusive access to the i/o list links in each
1627c478bd9Sstevel@tonic-gate  *			page structure.  It is always held while the page
1637c478bd9Sstevel@tonic-gate  *			is on an i/o list (i.e., involved in i/o).  That is,
1647c478bd9Sstevel@tonic-gate  *			even though a page may be only `shared' locked
1657c478bd9Sstevel@tonic-gate  *			while it is doing a write, the following fields may
1667c478bd9Sstevel@tonic-gate  *			change anyway.  Normally, the page must be
1677c478bd9Sstevel@tonic-gate  *			`exclusively' locked to change anything in it.
1687c478bd9Sstevel@tonic-gate  *
1697c478bd9Sstevel@tonic-gate  *				p_next
1707c478bd9Sstevel@tonic-gate  *				p_prev
1717c478bd9Sstevel@tonic-gate  *
1727c478bd9Sstevel@tonic-gate  * The following fields are protected by the global page_llock:
1737c478bd9Sstevel@tonic-gate  *
1747c478bd9Sstevel@tonic-gate  *				p_lckcnt
1757c478bd9Sstevel@tonic-gate  *				p_cowcnt
1767c478bd9Sstevel@tonic-gate  *
1777c478bd9Sstevel@tonic-gate  * The following lists are protected by the global page_freelock:
1787c478bd9Sstevel@tonic-gate  *
1797c478bd9Sstevel@tonic-gate  *				page_cachelist
1807c478bd9Sstevel@tonic-gate  *				page_freelist
1817c478bd9Sstevel@tonic-gate  *
1827c478bd9Sstevel@tonic-gate  * The following, for our purposes, are protected by
1837c478bd9Sstevel@tonic-gate  * the global freemem_lock:
1847c478bd9Sstevel@tonic-gate  *
1857c478bd9Sstevel@tonic-gate  *				freemem
1867c478bd9Sstevel@tonic-gate  *				freemem_wait
1877c478bd9Sstevel@tonic-gate  *				freemem_cv
1887c478bd9Sstevel@tonic-gate  *
1897c478bd9Sstevel@tonic-gate  * The following fields are protected by hat layer lock(s).  When a page
1907c478bd9Sstevel@tonic-gate  * structure is not mapped and is not associated with a vnode (after a call
1917c478bd9Sstevel@tonic-gate  * to page_hashout() for example) the p_nrm field may be modified with out
1927c478bd9Sstevel@tonic-gate  * holding the hat layer lock:
1937c478bd9Sstevel@tonic-gate  *
1947c478bd9Sstevel@tonic-gate  *				p_nrm
1957c478bd9Sstevel@tonic-gate  *				p_mapping
1967c478bd9Sstevel@tonic-gate  *				p_share
1977c478bd9Sstevel@tonic-gate  *
1987c478bd9Sstevel@tonic-gate  * The following field is file system dependent.  How it is used and
1997c478bd9Sstevel@tonic-gate  * the locking strategies applied are up to the individual file system
2007c478bd9Sstevel@tonic-gate  * implementation.
2017c478bd9Sstevel@tonic-gate  *
2027c478bd9Sstevel@tonic-gate  *				p_fsdata
2037c478bd9Sstevel@tonic-gate  *
2047c478bd9Sstevel@tonic-gate  * The page structure is used to represent and control the system's
2057c478bd9Sstevel@tonic-gate  * physical pages.  There is one instance of the structure for each
2067c478bd9Sstevel@tonic-gate  * page that is not permenately allocated.  For example, the pages that
2077c478bd9Sstevel@tonic-gate  * hold the page structures are permanently held by the kernel
2087c478bd9Sstevel@tonic-gate  * and hence do not need page structures to track them.  The array
2097c478bd9Sstevel@tonic-gate  * of page structures is allocated early on in the kernel's life and
2107c478bd9Sstevel@tonic-gate  * is based on the amount of available physical memory.
2117c478bd9Sstevel@tonic-gate  *
2127c478bd9Sstevel@tonic-gate  * Each page structure may simultaneously appear on several linked lists.
2137c478bd9Sstevel@tonic-gate  * The lists are:  hash list, free or in i/o list, and a vnode's page list.
2147c478bd9Sstevel@tonic-gate  * Each type of list is protected by a different group of mutexes as described
2157c478bd9Sstevel@tonic-gate  * below:
2167c478bd9Sstevel@tonic-gate  *
2177c478bd9Sstevel@tonic-gate  * The hash list is used to quickly find a page when the page's vnode and
2187c478bd9Sstevel@tonic-gate  * offset within the vnode are known.  Each page that is hashed is
2197c478bd9Sstevel@tonic-gate  * connected via the `p_hash' field.  The anchor for each hash is in the
2207c478bd9Sstevel@tonic-gate  * array `page_hash'.  An array of mutexes, `ph_mutex', protects the
2217c478bd9Sstevel@tonic-gate  * lists anchored by page_hash[].  To either search or modify a given hash
2227c478bd9Sstevel@tonic-gate  * list, the appropriate mutex in the ph_mutex array must be held.
2237c478bd9Sstevel@tonic-gate  *
2247c478bd9Sstevel@tonic-gate  * The free list contains pages that are `free to be given away'.  For
2257c478bd9Sstevel@tonic-gate  * efficiency reasons, pages on this list are placed in two catagories:
2267c478bd9Sstevel@tonic-gate  * pages that are still associated with a vnode, and pages that are not
2277c478bd9Sstevel@tonic-gate  * associated with a vnode.  Free pages always have their `p_free' bit set,
2287c478bd9Sstevel@tonic-gate  * free pages that are still associated with a vnode also have their
2297c478bd9Sstevel@tonic-gate  * `p_age' bit set.  Pages on the free list are connected via their
2307c478bd9Sstevel@tonic-gate  * `p_next' and `p_prev' fields.  When a page is involved in some sort
2317c478bd9Sstevel@tonic-gate  * of i/o, it is not free and these fields may be used to link associated
2327c478bd9Sstevel@tonic-gate  * pages together.  At the moment, the free list is protected by a
2337c478bd9Sstevel@tonic-gate  * single mutex `page_freelock'.  The list of free pages still associated
2347c478bd9Sstevel@tonic-gate  * with a vnode is anchored by `page_cachelist' while other free pages
2357c478bd9Sstevel@tonic-gate  * are anchored in architecture dependent ways (to handle page coloring etc.).
2367c478bd9Sstevel@tonic-gate  *
2377c478bd9Sstevel@tonic-gate  * Pages associated with a given vnode appear on a list anchored in the
2387c478bd9Sstevel@tonic-gate  * vnode by the `v_pages' field.  They are linked together with
2397c478bd9Sstevel@tonic-gate  * `p_vpnext' and `p_vpprev'.  The field `p_offset' contains a page's
2407c478bd9Sstevel@tonic-gate  * offset within the vnode.  The pages on this list are not kept in
2417c478bd9Sstevel@tonic-gate  * offset order.  These lists, in a manner similar to the hash lists,
2427c478bd9Sstevel@tonic-gate  * are protected by an array of mutexes called `vph_hash'.  Before
2437c478bd9Sstevel@tonic-gate  * searching or modifying this chain the appropriate mutex in the
2447c478bd9Sstevel@tonic-gate  * vph_hash[] array must be held.
2457c478bd9Sstevel@tonic-gate  *
2467c478bd9Sstevel@tonic-gate  * Again, each of the lists that a page can appear on is protected by a
2477c478bd9Sstevel@tonic-gate  * mutex.  Before reading or writing any of the fields comprising the
2487c478bd9Sstevel@tonic-gate  * list, the appropriate lock must be held.  These list locks should only
2497c478bd9Sstevel@tonic-gate  * be held for very short intervals.
2507c478bd9Sstevel@tonic-gate  *
2517c478bd9Sstevel@tonic-gate  * In addition to the list locks, each page structure contains a
2527c478bd9Sstevel@tonic-gate  * shared/exclusive lock that protects various fields within it.
2537c478bd9Sstevel@tonic-gate  * To modify one of these fields, the `p_selock' must be exclusively held.
2547c478bd9Sstevel@tonic-gate  * To read a field with a degree of certainty, the lock must be at least
2557c478bd9Sstevel@tonic-gate  * held shared.
2567c478bd9Sstevel@tonic-gate  *
2577c478bd9Sstevel@tonic-gate  * Removing a page structure from one of the lists requires holding
2587c478bd9Sstevel@tonic-gate  * the appropriate list lock and the page's p_selock.  A page may be
2597c478bd9Sstevel@tonic-gate  * prevented from changing identity, being freed, or otherwise modified
2607c478bd9Sstevel@tonic-gate  * by acquiring p_selock shared.
2617c478bd9Sstevel@tonic-gate  *
2627c478bd9Sstevel@tonic-gate  * To avoid deadlocks, a strict locking protocol must be followed.  Basically
2637c478bd9Sstevel@tonic-gate  * there are two cases:  In the first case, the page structure in question
2647c478bd9Sstevel@tonic-gate  * is known ahead of time (e.g., when the page is to be added or removed
2657c478bd9Sstevel@tonic-gate  * from a list).  In the second case, the page structure is not known and
2667c478bd9Sstevel@tonic-gate  * must be found by searching one of the lists.
2677c478bd9Sstevel@tonic-gate  *
2687c478bd9Sstevel@tonic-gate  * When adding or removing a known page to one of the lists, first the
2697c478bd9Sstevel@tonic-gate  * page must be exclusively locked (since at least one of its fields
2707c478bd9Sstevel@tonic-gate  * will be modified), second the lock protecting the list must be acquired,
2717c478bd9Sstevel@tonic-gate  * third the page inserted or deleted, and finally the list lock dropped.
2727c478bd9Sstevel@tonic-gate  *
2737c478bd9Sstevel@tonic-gate  * The more interesting case occures when the particular page structure
2747c478bd9Sstevel@tonic-gate  * is not known ahead of time.  For example, when a call is made to
2757c478bd9Sstevel@tonic-gate  * page_lookup(), it is not known if a page with the desired (vnode and
2767c478bd9Sstevel@tonic-gate  * offset pair) identity exists.  So the appropriate mutex in ph_mutex is
2777c478bd9Sstevel@tonic-gate  * acquired, the hash list searched, and if the desired page is found
2787c478bd9Sstevel@tonic-gate  * an attempt is made to lock it.  The attempt to acquire p_selock must
2797c478bd9Sstevel@tonic-gate  * not block while the hash list lock is held.  A deadlock could occure
2807c478bd9Sstevel@tonic-gate  * if some other process was trying to remove the page from the list.
2817c478bd9Sstevel@tonic-gate  * The removing process (following the above protocol) would have exclusively
2827c478bd9Sstevel@tonic-gate  * locked the page, and be spinning waiting to acquire the lock protecting
2837c478bd9Sstevel@tonic-gate  * the hash list.  Since the searching process holds the hash list lock
2847c478bd9Sstevel@tonic-gate  * and is waiting to acquire the page lock, a deadlock occurs.
2857c478bd9Sstevel@tonic-gate  *
2867c478bd9Sstevel@tonic-gate  * The proper scheme to follow is: first, lock the appropriate list,
2877c478bd9Sstevel@tonic-gate  * search the list, and if the desired page is found either use
2887c478bd9Sstevel@tonic-gate  * page_trylock() (which will not block) or pass the address of the
2897c478bd9Sstevel@tonic-gate  * list lock to page_lock().  If page_lock() can not acquire the page's
2907c478bd9Sstevel@tonic-gate  * lock, it will drop the list lock before going to sleep.  page_lock()
2917c478bd9Sstevel@tonic-gate  * returns a value to indicate if the list lock was dropped allowing the
2927c478bd9Sstevel@tonic-gate  * calling program to react appropriately (i.e., retry the operation).
2937c478bd9Sstevel@tonic-gate  *
2947c478bd9Sstevel@tonic-gate  * If the list lock was dropped before the attempt at locking the page
2957c478bd9Sstevel@tonic-gate  * was made, checks would have to be made to ensure that the page had
2967c478bd9Sstevel@tonic-gate  * not changed identity before its lock was obtained.  This is because
2977c478bd9Sstevel@tonic-gate  * the interval between dropping the list lock and acquiring the page
2987c478bd9Sstevel@tonic-gate  * lock is indeterminate.
2997c478bd9Sstevel@tonic-gate  *
3007c478bd9Sstevel@tonic-gate  * In addition, when both a hash list lock (ph_mutex[]) and a vnode list
3017c478bd9Sstevel@tonic-gate  * lock (vph_mutex[]) are needed, the hash list lock must be acquired first.
3027c478bd9Sstevel@tonic-gate  * The routine page_hashin() is a good example of this sequence.
3037c478bd9Sstevel@tonic-gate  * This sequence is ASSERTed by checking that the vph_mutex[] is not held
3047c478bd9Sstevel@tonic-gate  * just before each acquisition of one of the mutexs in ph_mutex[].
3057c478bd9Sstevel@tonic-gate  *
3067c478bd9Sstevel@tonic-gate  * So, as a quick summary:
3077c478bd9Sstevel@tonic-gate  *
3087c478bd9Sstevel@tonic-gate  * 	pse_mutex[]'s protect the p_selock and p_cv fields.
3097c478bd9Sstevel@tonic-gate  *
3107c478bd9Sstevel@tonic-gate  * 	p_selock protects the p_free, p_age, p_vnode, p_offset and p_hash,
3117c478bd9Sstevel@tonic-gate  *
3127c478bd9Sstevel@tonic-gate  * 	ph_mutex[]'s protect the page_hash[] array and its chains.
3137c478bd9Sstevel@tonic-gate  *
3147c478bd9Sstevel@tonic-gate  * 	vph_mutex[]'s protect the v_pages field and the vp page chains.
3157c478bd9Sstevel@tonic-gate  *
3167c478bd9Sstevel@tonic-gate  *	First lock the page, then the hash chain, then the vnode chain.  When
3177c478bd9Sstevel@tonic-gate  *	this is not possible `trylocks' must be used.  Sleeping while holding
3187c478bd9Sstevel@tonic-gate  *	any of these mutexes (p_selock is not a mutex) is not allowed.
3197c478bd9Sstevel@tonic-gate  *
3207c478bd9Sstevel@tonic-gate  *
3217c478bd9Sstevel@tonic-gate  *	field		reading		writing		    ordering
3227c478bd9Sstevel@tonic-gate  *	======================================================================
3237c478bd9Sstevel@tonic-gate  *	p_vnode		p_selock(E,S)	p_selock(E)
3247c478bd9Sstevel@tonic-gate  *	p_offset
3257c478bd9Sstevel@tonic-gate  *	p_free
3267c478bd9Sstevel@tonic-gate  *	p_age
3277c478bd9Sstevel@tonic-gate  *	=====================================================================
3287c478bd9Sstevel@tonic-gate  *	p_hash		p_selock(E,S)	p_selock(E) &&	    p_selock, ph_mutex
3297c478bd9Sstevel@tonic-gate  *					ph_mutex[]
3307c478bd9Sstevel@tonic-gate  *	=====================================================================
3317c478bd9Sstevel@tonic-gate  *	p_vpnext	p_selock(E,S)	p_selock(E) &&	    p_selock, vph_mutex
3327c478bd9Sstevel@tonic-gate  *	p_vpprev			vph_mutex[]
3337c478bd9Sstevel@tonic-gate  *	=====================================================================
3347c478bd9Sstevel@tonic-gate  *	When the p_free bit is set:
3357c478bd9Sstevel@tonic-gate  *
3367c478bd9Sstevel@tonic-gate  *	p_next		p_selock(E,S)	p_selock(E) &&	    p_selock,
3377c478bd9Sstevel@tonic-gate  *	p_prev				page_freelock	    page_freelock
3387c478bd9Sstevel@tonic-gate  *
3397c478bd9Sstevel@tonic-gate  *	When the p_free bit is not set:
3407c478bd9Sstevel@tonic-gate  *
3417c478bd9Sstevel@tonic-gate  *	p_next		p_selock(E,S)	p_selock(E) &&	    p_selock, p_iolock
3427c478bd9Sstevel@tonic-gate  *	p_prev				p_iolock
3437c478bd9Sstevel@tonic-gate  *	=====================================================================
3447c478bd9Sstevel@tonic-gate  *	p_selock	pse_mutex[]	pse_mutex[]	    can`t acquire any
3457c478bd9Sstevel@tonic-gate  *	p_cv						    other mutexes or
3467c478bd9Sstevel@tonic-gate  *							    sleep while holding
3477c478bd9Sstevel@tonic-gate  *							    this lock.
3487c478bd9Sstevel@tonic-gate  *	=====================================================================
3497c478bd9Sstevel@tonic-gate  *	p_lckcnt	p_selock(E,S)	p_selock(E) &&
3507c478bd9Sstevel@tonic-gate  *	p_cowcnt			page_llock
3517c478bd9Sstevel@tonic-gate  *	=====================================================================
3527c478bd9Sstevel@tonic-gate  *	p_nrm		hat layer lock	hat layer lock
3537c478bd9Sstevel@tonic-gate  *	p_mapping
3547c478bd9Sstevel@tonic-gate  *	p_pagenum
3557c478bd9Sstevel@tonic-gate  *	=====================================================================
3567c478bd9Sstevel@tonic-gate  *
3577c478bd9Sstevel@tonic-gate  *	where:
3587c478bd9Sstevel@tonic-gate  *		E----> exclusive version of p_selock.
3597c478bd9Sstevel@tonic-gate  *		S----> shared version of p_selock.
3607c478bd9Sstevel@tonic-gate  *
3617c478bd9Sstevel@tonic-gate  *
3627c478bd9Sstevel@tonic-gate  *	Global data structures and variable:
3637c478bd9Sstevel@tonic-gate  *
3647c478bd9Sstevel@tonic-gate  *	field		reading		writing		    ordering
3657c478bd9Sstevel@tonic-gate  *	=====================================================================
3667c478bd9Sstevel@tonic-gate  *	page_hash[]	ph_mutex[]	ph_mutex[]	    can hold this lock
3677c478bd9Sstevel@tonic-gate  *							    before acquiring
3687c478bd9Sstevel@tonic-gate  *							    a vph_mutex or
3697c478bd9Sstevel@tonic-gate  *							    pse_mutex.
3707c478bd9Sstevel@tonic-gate  *	=====================================================================
3717c478bd9Sstevel@tonic-gate  *	vp->v_pages	vph_mutex[]	vph_mutex[]	    can only acquire
3727c478bd9Sstevel@tonic-gate  *							    a pse_mutex while
3737c478bd9Sstevel@tonic-gate  *							    holding this lock.
3747c478bd9Sstevel@tonic-gate  *	=====================================================================
3757c478bd9Sstevel@tonic-gate  *	page_cachelist	page_freelock	page_freelock	    can't acquire any
3767c478bd9Sstevel@tonic-gate  *	page_freelist	page_freelock	page_freelock
3777c478bd9Sstevel@tonic-gate  *	=====================================================================
3787c478bd9Sstevel@tonic-gate  *	freemem		freemem_lock	freemem_lock	    can't acquire any
3797c478bd9Sstevel@tonic-gate  *	freemem_wait					    other mutexes while
3807c478bd9Sstevel@tonic-gate  *	freemem_cv					    holding this mutex.
3817c478bd9Sstevel@tonic-gate  *	=====================================================================
3827c478bd9Sstevel@tonic-gate  *
3837c478bd9Sstevel@tonic-gate  * Page relocation, PG_NORELOC and P_NORELOC.
3847c478bd9Sstevel@tonic-gate  *
3857c478bd9Sstevel@tonic-gate  * Pages may be relocated using the page_relocate() interface. Relocation
3867c478bd9Sstevel@tonic-gate  * involves moving the contents and identity of a page to another, free page.
3877c478bd9Sstevel@tonic-gate  * To relocate a page, the SE_EXCL lock must be obtained. The way to prevent
3887c478bd9Sstevel@tonic-gate  * a page from being relocated is to hold the SE_SHARED lock (the SE_EXCL
3897c478bd9Sstevel@tonic-gate  * lock must not be held indefinitely). If the page is going to be held
3907c478bd9Sstevel@tonic-gate  * SE_SHARED indefinitely, then the PG_NORELOC hint should be passed
3917c478bd9Sstevel@tonic-gate  * to page_create_va so that pages that are prevented from being relocated
3927c478bd9Sstevel@tonic-gate  * can be managed differently by the platform specific layer.
3937c478bd9Sstevel@tonic-gate  *
3947c478bd9Sstevel@tonic-gate  * Pages locked in memory using page_pp_lock (p_lckcnt/p_cowcnt != 0)
3957c478bd9Sstevel@tonic-gate  * are guaranteed to be held in memory, but can still be relocated
3967c478bd9Sstevel@tonic-gate  * providing the SE_EXCL lock can be obtained.
3977c478bd9Sstevel@tonic-gate  *
3987c478bd9Sstevel@tonic-gate  * The P_NORELOC bit in the page_t.p_state field is provided for use by
3997c478bd9Sstevel@tonic-gate  * the platform specific code in managing pages when the PG_NORELOC
4007c478bd9Sstevel@tonic-gate  * hint is used.
4017c478bd9Sstevel@tonic-gate  *
4027c478bd9Sstevel@tonic-gate  * Memory delete and page locking.
4037c478bd9Sstevel@tonic-gate  *
4047c478bd9Sstevel@tonic-gate  * The set of all usable pages is managed using the global page list as
4057c478bd9Sstevel@tonic-gate  * implemented by the memseg structure defined below. When memory is added
4067c478bd9Sstevel@tonic-gate  * or deleted this list changes. Additions to this list guarantee that the
4077c478bd9Sstevel@tonic-gate  * list is never corrupt.  In order to avoid the necessity of an additional
4087c478bd9Sstevel@tonic-gate  * lock to protect against failed accesses to the memseg being deleted and,
4097c478bd9Sstevel@tonic-gate  * more importantly, the page_ts, the memseg structure is never freed and the
4107c478bd9Sstevel@tonic-gate  * page_t virtual address space is remapped to a page (or pages) of
4117c478bd9Sstevel@tonic-gate  * zeros.  If a page_t is manipulated while it is p_selock'd, or if it is
4127c478bd9Sstevel@tonic-gate  * locked indirectly via a hash or freelist lock, it is not possible for
4137c478bd9Sstevel@tonic-gate  * memory delete to collect the page and so that part of the page list is
4147c478bd9Sstevel@tonic-gate  * prevented from being deleted. If the page is referenced outside of one
4157c478bd9Sstevel@tonic-gate  * of these locks, it is possible for the page_t being referenced to be
4167c478bd9Sstevel@tonic-gate  * deleted.  Examples of this are page_t pointers returned by
4177c478bd9Sstevel@tonic-gate  * page_numtopp_nolock, page_first and page_next.  Providing the page_t
4187c478bd9Sstevel@tonic-gate  * is re-checked after taking the p_selock (for p_vnode != NULL), the
4197c478bd9Sstevel@tonic-gate  * remapping to the zero pages will be detected.
4207c478bd9Sstevel@tonic-gate  *
4217c478bd9Sstevel@tonic-gate  *
4227c478bd9Sstevel@tonic-gate  * Page size (p_szc field) and page locking.
4237c478bd9Sstevel@tonic-gate  *
4247c478bd9Sstevel@tonic-gate  * p_szc field of free pages is changed by free list manager under freelist
4257c478bd9Sstevel@tonic-gate  * locks and is of no concern to the rest of VM subsystem.
4267c478bd9Sstevel@tonic-gate  *
4277c478bd9Sstevel@tonic-gate  * p_szc changes of allocated anonymous (swapfs) can only be done only after
4287c478bd9Sstevel@tonic-gate  * exclusively locking all constituent pages and calling hat_pageunload() on
4297c478bd9Sstevel@tonic-gate  * each of them. To prevent p_szc changes of non free anonymous (swapfs) large
4307c478bd9Sstevel@tonic-gate  * pages it's enough to either lock SHARED any of constituent pages or prevent
4317c478bd9Sstevel@tonic-gate  * hat_pageunload() by holding hat level lock that protects mapping lists (this
4327c478bd9Sstevel@tonic-gate  * method is for hat code only)
4337c478bd9Sstevel@tonic-gate  *
4347c478bd9Sstevel@tonic-gate  * To increase (promote) p_szc of allocated non anonymous file system pages
4357c478bd9Sstevel@tonic-gate  * one has to first lock exclusively all involved constituent pages and call
4367c478bd9Sstevel@tonic-gate  * hat_pageunload() on each of them. To prevent p_szc promote it's enough to
4377c478bd9Sstevel@tonic-gate  * either lock SHARED any of constituent pages that will be needed to make a
4387c478bd9Sstevel@tonic-gate  * large page or prevent hat_pageunload() by holding hat level lock that
4397c478bd9Sstevel@tonic-gate  * protects mapping lists (this method is for hat code only).
4407c478bd9Sstevel@tonic-gate  *
4417c478bd9Sstevel@tonic-gate  * To decrease (demote) p_szc of an allocated non anonymous file system large
4427c478bd9Sstevel@tonic-gate  * page one can either use the same method as used for changeing p_szc of
4437c478bd9Sstevel@tonic-gate  * anonymous large pages or if it's not possible to lock all constituent pages
4447c478bd9Sstevel@tonic-gate  * exclusively a different method can be used. In the second method one only
4457c478bd9Sstevel@tonic-gate  * has to exclusively lock one of constituent pages but then one has to
4467c478bd9Sstevel@tonic-gate  * acquire further locks by calling page_szc_lock() and
4477c478bd9Sstevel@tonic-gate  * hat_page_demote(). hat_page_demote() acquires hat level locks and then
4487c478bd9Sstevel@tonic-gate  * demotes the page. This mechanism relies on the fact that any code that
4497c478bd9Sstevel@tonic-gate  * needs to prevent p_szc of a file system large page from changeing either
4507c478bd9Sstevel@tonic-gate  * locks all constituent large pages at least SHARED or locks some pages at
4517c478bd9Sstevel@tonic-gate  * least SHARED and calls page_szc_lock() or uses hat level page locks.
4527c478bd9Sstevel@tonic-gate  * Demotion using this method is implemented by page_demote_vp_pages().
4537c478bd9Sstevel@tonic-gate  * Please see comments in front of page_demote_vp_pages(), hat_page_demote()
4547c478bd9Sstevel@tonic-gate  * and page_szc_lock() for more details.
4557c478bd9Sstevel@tonic-gate  *
4567c478bd9Sstevel@tonic-gate  * Lock order: p_selock, page_szc_lock, ph_mutex/vph_mutex/freelist,
4577c478bd9Sstevel@tonic-gate  * hat level locks.
4587c478bd9Sstevel@tonic-gate  */
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate typedef struct page {
4617c478bd9Sstevel@tonic-gate 	u_offset_t	p_offset;	/* offset into vnode for this page */
4627c478bd9Sstevel@tonic-gate 	struct vnode	*p_vnode;	/* vnode that this page is named by */
4637c478bd9Sstevel@tonic-gate 	selock_t	p_selock;	/* shared/exclusive lock on the page */
4647c478bd9Sstevel@tonic-gate #if defined(_LP64)
4657c478bd9Sstevel@tonic-gate 	int		p_selockpad;	/* pad for growing selock */
4667c478bd9Sstevel@tonic-gate #endif
4677c478bd9Sstevel@tonic-gate 	struct page	*p_hash;	/* hash by [vnode, offset] */
4687c478bd9Sstevel@tonic-gate 	struct page	*p_vpnext;	/* next page in vnode list */
4697c478bd9Sstevel@tonic-gate 	struct page	*p_vpprev;	/* prev page in vnode list */
4707c478bd9Sstevel@tonic-gate 	struct page	*p_next;	/* next page in free/intrans lists */
4717c478bd9Sstevel@tonic-gate 	struct page	*p_prev;	/* prev page in free/intrans lists */
4727c478bd9Sstevel@tonic-gate 	ushort_t	p_lckcnt;	/* number of locks on page data */
4737c478bd9Sstevel@tonic-gate 	ushort_t	p_cowcnt;	/* number of copy on write lock */
4747c478bd9Sstevel@tonic-gate 	kcondvar_t	p_cv;		/* page struct's condition var */
4757c478bd9Sstevel@tonic-gate 	kcondvar_t	p_io_cv;	/* for iolock */
4767c478bd9Sstevel@tonic-gate 	uchar_t		p_iolock_state;	/* replaces p_iolock */
4777c478bd9Sstevel@tonic-gate 	volatile uchar_t p_szc;		/* page size code */
4787c478bd9Sstevel@tonic-gate 	uchar_t		p_fsdata;	/* file system dependent byte */
4797c478bd9Sstevel@tonic-gate 	uchar_t		p_state;	/* p_free, p_noreloc */
4807c478bd9Sstevel@tonic-gate 	uchar_t		p_nrm;		/* non-cache, ref, mod readonly bits */
4817c478bd9Sstevel@tonic-gate #if defined(__sparc)
4827c478bd9Sstevel@tonic-gate 	uchar_t		p_vcolor;	/* virtual color */
4837c478bd9Sstevel@tonic-gate #else
4847c478bd9Sstevel@tonic-gate 	uchar_t		p_embed;	/* x86 - changes p_mapping & p_index */
4857c478bd9Sstevel@tonic-gate #endif
4867c478bd9Sstevel@tonic-gate 	uchar_t		p_index;	/* MPSS mapping info. Not used on x86 */
4877c478bd9Sstevel@tonic-gate 	uchar_t		p_toxic;	/* page has an unrecoverable error */
4887c478bd9Sstevel@tonic-gate 	void		*p_mapping;	/* hat specific translation info */
4897c478bd9Sstevel@tonic-gate 	pfn_t		p_pagenum;	/* physical page number */
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	uint_t		p_share;	/* number of translations */
4927c478bd9Sstevel@tonic-gate #if defined(_LP64)
4937c478bd9Sstevel@tonic-gate 	uint_t		p_sharepad;	/* pad for growing p_share */
4947c478bd9Sstevel@tonic-gate #endif
4957c478bd9Sstevel@tonic-gate 	uint_t		p_msresv_1;	/* reserved for future use */
4967c478bd9Sstevel@tonic-gate #if defined(__sparc)
4977c478bd9Sstevel@tonic-gate 	uint_t		p_kpmref;	/* number of kpm mapping sharers */
4987c478bd9Sstevel@tonic-gate 	struct kpme	*p_kpmelist;	/* kpm specific mapping info */
4997c478bd9Sstevel@tonic-gate #else
5007c478bd9Sstevel@tonic-gate 	/* index of entry in p_map when p_embed is set */
5017c478bd9Sstevel@tonic-gate 	uint_t		p_mlentry;
5027c478bd9Sstevel@tonic-gate #endif
5037c478bd9Sstevel@tonic-gate 	uint64_t	p_msresv_2;	/* page allocation debugging */
5047c478bd9Sstevel@tonic-gate } page_t;
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate typedef	page_t	devpage_t;
5087c478bd9Sstevel@tonic-gate #define	devpage	page
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate /*
5127c478bd9Sstevel@tonic-gate  * Page hash table is a power-of-two in size, externally chained
5137c478bd9Sstevel@tonic-gate  * through the hash field.  PAGE_HASHAVELEN is the average length
5147c478bd9Sstevel@tonic-gate  * desired for this chain, from which the size of the page_hash
5157c478bd9Sstevel@tonic-gate  * table is derived at boot time and stored in the kernel variable
5167c478bd9Sstevel@tonic-gate  * page_hashsz.  In the hash function it is given by PAGE_HASHSZ.
5177c478bd9Sstevel@tonic-gate  *
5187c478bd9Sstevel@tonic-gate  * PAGE_HASH_FUNC returns an index into the page_hash[] array.  This
5197c478bd9Sstevel@tonic-gate  * index is also used to derive the mutex that protects the chain.
5207c478bd9Sstevel@tonic-gate  *
5217c478bd9Sstevel@tonic-gate  * In constructing the hash function, first we dispose of unimportant bits
5227c478bd9Sstevel@tonic-gate  * (page offset from "off" and the low 3 bits of "vp" which are zero for
5237c478bd9Sstevel@tonic-gate  * struct alignment). Then shift and sum the remaining bits a couple times
5247c478bd9Sstevel@tonic-gate  * in order to get as many source bits from the two source values into the
5257c478bd9Sstevel@tonic-gate  * resulting hashed value.  Note that this will perform quickly, since the
5267c478bd9Sstevel@tonic-gate  * shifting/summing are fast register to register operations with no additional
5277c478bd9Sstevel@tonic-gate  * memory references).
5287c478bd9Sstevel@tonic-gate  */
5297c478bd9Sstevel@tonic-gate #if NCPU < 4
5307c478bd9Sstevel@tonic-gate #define	PH_TABLE_SIZE	16
5317c478bd9Sstevel@tonic-gate #define	VP_SHIFT	7
5327c478bd9Sstevel@tonic-gate #else
5337c478bd9Sstevel@tonic-gate #define	PH_TABLE_SIZE	128
5347c478bd9Sstevel@tonic-gate #define	VP_SHIFT	9
5357c478bd9Sstevel@tonic-gate #endif
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate /*
5387c478bd9Sstevel@tonic-gate  * The amount to use for the successive shifts in the hash function below.
5397c478bd9Sstevel@tonic-gate  * The actual value is LOG2(PH_TABLE_SIZE), so that as many bits as
5407c478bd9Sstevel@tonic-gate  * possible will filter thru PAGE_HASH_FUNC() and PAGE_HASH_MUTEX().
5417c478bd9Sstevel@tonic-gate  */
5427c478bd9Sstevel@tonic-gate #define	PH_SHIFT_SIZE   (7)
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate #define	PAGE_HASHSZ	page_hashsz
5457c478bd9Sstevel@tonic-gate #define	PAGE_HASHAVELEN		4
5467c478bd9Sstevel@tonic-gate #define	PAGE_HASH_FUNC(vp, off) \
5477c478bd9Sstevel@tonic-gate 	((((uintptr_t)(off) >> PAGESHIFT) + \
5487c478bd9Sstevel@tonic-gate 		((uintptr_t)(off) >> (PAGESHIFT + PH_SHIFT_SIZE)) + \
5497c478bd9Sstevel@tonic-gate 		((uintptr_t)(vp) >> 3) + \
5507c478bd9Sstevel@tonic-gate 		((uintptr_t)(vp) >> (3 + PH_SHIFT_SIZE)) + \
5517c478bd9Sstevel@tonic-gate 		((uintptr_t)(vp) >> (3 + 2 * PH_SHIFT_SIZE))) & \
5527c478bd9Sstevel@tonic-gate 		(PAGE_HASHSZ - 1))
5537c478bd9Sstevel@tonic-gate #ifdef _KERNEL
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate /*
5567c478bd9Sstevel@tonic-gate  * The page hash value is re-hashed to an index for the ph_mutex array.
5577c478bd9Sstevel@tonic-gate  *
5587c478bd9Sstevel@tonic-gate  * For 64 bit kernels, the mutex array is padded out to prevent false
5597c478bd9Sstevel@tonic-gate  * sharing of cache sub-blocks (64 bytes) of adjacent mutexes.
5607c478bd9Sstevel@tonic-gate  *
5617c478bd9Sstevel@tonic-gate  * For 32 bit kernels, we don't want to waste kernel address space with
5627c478bd9Sstevel@tonic-gate  * padding, so instead we rely on the hash function to introduce skew of
5637c478bd9Sstevel@tonic-gate  * adjacent vnode/offset indexes (the left shift part of the hash function).
5647c478bd9Sstevel@tonic-gate  * Since sizeof (kmutex_t) is 8, we shift an additional 3 to skew to a different
5657c478bd9Sstevel@tonic-gate  * 64 byte sub-block.
5667c478bd9Sstevel@tonic-gate  */
5677c478bd9Sstevel@tonic-gate typedef struct pad_mutex {
5687c478bd9Sstevel@tonic-gate 	kmutex_t	pad_mutex;
5697c478bd9Sstevel@tonic-gate #ifdef _LP64
5707c478bd9Sstevel@tonic-gate 	char		pad_pad[64 - sizeof (kmutex_t)];
5717c478bd9Sstevel@tonic-gate #endif
5727c478bd9Sstevel@tonic-gate } pad_mutex_t;
5737c478bd9Sstevel@tonic-gate extern pad_mutex_t ph_mutex[];
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate #define	PAGE_HASH_MUTEX(x) \
5767c478bd9Sstevel@tonic-gate 	&(ph_mutex[((x) + ((x) >> VP_SHIFT) + ((x) << 3)) & \
5777c478bd9Sstevel@tonic-gate 		(PH_TABLE_SIZE - 1)].pad_mutex)
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate /*
5807c478bd9Sstevel@tonic-gate  * Flags used while creating pages.
5817c478bd9Sstevel@tonic-gate  */
5827c478bd9Sstevel@tonic-gate #define	PG_EXCL		0x0001
5837c478bd9Sstevel@tonic-gate #define	PG_WAIT		0x0002
5847c478bd9Sstevel@tonic-gate #define	PG_PHYSCONTIG	0x0004		/* NOT SUPPORTED */
5857c478bd9Sstevel@tonic-gate #define	PG_MATCH_COLOR	0x0008		/* SUPPORTED by free list routines */
5867c478bd9Sstevel@tonic-gate #define	PG_NORELOC	0x0010		/* Non-relocatable alloc hint. */
5877c478bd9Sstevel@tonic-gate 					/* Page must be PP_ISNORELOC */
5887c478bd9Sstevel@tonic-gate #define	PG_PANIC	0x0020		/* system will panic if alloc fails */
5897c478bd9Sstevel@tonic-gate #define	PG_PUSHPAGE	0x0040		/* alloc may use reserve */
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate /*
5927c478bd9Sstevel@tonic-gate  * When p_selock has the SE_EWANTED bit set, threads waiting for SE_EXCL
5937c478bd9Sstevel@tonic-gate  * access are given priority over all other waiting threads.
5947c478bd9Sstevel@tonic-gate  */
5957c478bd9Sstevel@tonic-gate #define	SE_EWANTED	0x40000000
5967c478bd9Sstevel@tonic-gate #define	PAGE_LOCKED(pp)		(((pp)->p_selock & ~SE_EWANTED) != 0)
5977c478bd9Sstevel@tonic-gate #define	PAGE_SHARED(pp)		(((pp)->p_selock & ~SE_EWANTED) > 0)
5987c478bd9Sstevel@tonic-gate #define	PAGE_EXCL(pp)		((pp)->p_selock < 0)
5997c478bd9Sstevel@tonic-gate #define	PAGE_LOCKED_SE(pp, se)	\
6007c478bd9Sstevel@tonic-gate 	((se) == SE_EXCL ? PAGE_EXCL(pp) : PAGE_SHARED(pp))
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate extern	long page_hashsz;
6037c478bd9Sstevel@tonic-gate extern	page_t **page_hash;
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate extern	kmutex_t page_llock;		/* page logical lock mutex */
6067c478bd9Sstevel@tonic-gate extern	kmutex_t freemem_lock;		/* freemem lock */
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate extern	pgcnt_t	total_pages;		/* total pages in the system */
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate /*
6117c478bd9Sstevel@tonic-gate  * Variables controlling locking of physical memory.
6127c478bd9Sstevel@tonic-gate  */
6137c478bd9Sstevel@tonic-gate extern	pgcnt_t	pages_pp_maximum;	/* tuning: lock + claim <= max */
6147c478bd9Sstevel@tonic-gate extern	void init_pages_pp_maximum(void);
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate struct lgrp;
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate /* page_list_{add,sub} flags */
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate /* which list */
6217c478bd9Sstevel@tonic-gate #define	PG_FREE_LIST	0x0001
6227c478bd9Sstevel@tonic-gate #define	PG_CACHE_LIST	0x0002
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate /* where on list */
6257c478bd9Sstevel@tonic-gate #define	PG_LIST_TAIL	0x0010
6267c478bd9Sstevel@tonic-gate #define	PG_LIST_HEAD	0x0020
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate /* called from */
6297c478bd9Sstevel@tonic-gate #define	PG_LIST_ISINIT	0x1000
6307c478bd9Sstevel@tonic-gate #define	PG_LIST_ISCAGE	0x2000
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate /*
6337c478bd9Sstevel@tonic-gate  * Flags for setting the p_toxic flag when a page has errors
6347c478bd9Sstevel@tonic-gate  * These flags may be OR'ed into the p_toxic page flag to
6357c478bd9Sstevel@tonic-gate  * indicate that error(s) have occurred on a page,
6367c478bd9Sstevel@tonic-gate  * (see page_settoxic()). If both PAGE_IS_TOXIC and
6377c478bd9Sstevel@tonic-gate  * PAGE_IS_FAILING are set, PAGE_IS_FAILING takes precedence.
6387c478bd9Sstevel@tonic-gate  *
6397c478bd9Sstevel@tonic-gate  * When an error happens on a page, the trap handler sets
6407c478bd9Sstevel@tonic-gate  * PAGE_IS_FAULTY on the page to indicate that an error has been
6417c478bd9Sstevel@tonic-gate  * seen on the page. The error could be really a memory error or
6427c478bd9Sstevel@tonic-gate  * something else (like a datapath error). When it is determined
6437c478bd9Sstevel@tonic-gate  * that it is a memory error, the page is marked as PAGE_IS_TOXIC
6447c478bd9Sstevel@tonic-gate  * or PAGE_IS_FAILING depending on the type of error and then
6457c478bd9Sstevel@tonic-gate  * retired.
6467c478bd9Sstevel@tonic-gate  *
6477c478bd9Sstevel@tonic-gate  * We use the page's 'toxic' flag to determine whether the page
6487c478bd9Sstevel@tonic-gate  * has just got a single error - PAGE_IS_TOXIC - or is being
6497c478bd9Sstevel@tonic-gate  * retired due to multiple soft errors - PAGE_IS_FAILING. In
6507c478bd9Sstevel@tonic-gate  * page_free(), a page that has been marked PAGE_IS_FAILING will
6517c478bd9Sstevel@tonic-gate  * not be cleaned, it will always be retired. A page marked
6527c478bd9Sstevel@tonic-gate  * PAGE_IS_TOXIC is cleaned and is retired only if this attempt at
6537c478bd9Sstevel@tonic-gate  * cleaning fails.
6547c478bd9Sstevel@tonic-gate  *
6557c478bd9Sstevel@tonic-gate  * When a page has been successfully retired, we set PAGE_IS_RETIRED.
6567c478bd9Sstevel@tonic-gate  */
6577c478bd9Sstevel@tonic-gate #define	PAGE_IS_OK		0x0
6587c478bd9Sstevel@tonic-gate #define	PAGE_IS_TOXIC		0x1
6597c478bd9Sstevel@tonic-gate #define	PAGE_IS_FAILING		0x2
6607c478bd9Sstevel@tonic-gate #define	PAGE_IS_RETIRED		0x4
6617c478bd9Sstevel@tonic-gate #define	PAGE_IS_FAULTY		0x8
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate /*
6647c478bd9Sstevel@tonic-gate  * Page frame operations.
6657c478bd9Sstevel@tonic-gate  */
6667c478bd9Sstevel@tonic-gate page_t	*page_lookup(struct vnode *, u_offset_t, se_t);
6677c478bd9Sstevel@tonic-gate page_t	*page_lookup_create(struct vnode *, u_offset_t, se_t, page_t *,
6687c478bd9Sstevel@tonic-gate 	spgcnt_t *, int);
6697c478bd9Sstevel@tonic-gate page_t	*page_lookup_nowait(struct vnode *, u_offset_t, se_t);
6707c478bd9Sstevel@tonic-gate page_t	*page_find(struct vnode *, u_offset_t);
6717c478bd9Sstevel@tonic-gate page_t	*page_exists(struct vnode *, u_offset_t);
6727c478bd9Sstevel@tonic-gate int	page_exists_physcontig(vnode_t *, u_offset_t, uint_t, page_t *[]);
6737c478bd9Sstevel@tonic-gate int	page_exists_forreal(struct vnode *, u_offset_t, uint_t *);
6747c478bd9Sstevel@tonic-gate void	page_needfree(spgcnt_t);
6757c478bd9Sstevel@tonic-gate page_t	*page_create(struct vnode *, u_offset_t, size_t, uint_t);
6767c478bd9Sstevel@tonic-gate int	page_alloc_pages(struct seg *, caddr_t, page_t **, page_t **,
6777c478bd9Sstevel@tonic-gate 		uint_t, int);
6787c478bd9Sstevel@tonic-gate page_t  *page_create_va_large(vnode_t *vp, u_offset_t off, size_t bytes,
6797c478bd9Sstevel@tonic-gate 	uint_t flags, struct seg *seg, caddr_t vaddr, void *arg);
6807c478bd9Sstevel@tonic-gate page_t	*page_create_va(struct vnode *, u_offset_t, size_t, uint_t,
6817c478bd9Sstevel@tonic-gate 	struct seg *, caddr_t);
6827c478bd9Sstevel@tonic-gate int	page_create_wait(size_t npages, uint_t flags);
6837c478bd9Sstevel@tonic-gate void    page_create_putback(ssize_t npages);
6847c478bd9Sstevel@tonic-gate void	page_free(page_t *, int);
6857c478bd9Sstevel@tonic-gate void	page_free_at_startup(page_t *);
6867c478bd9Sstevel@tonic-gate void	page_free_pages(page_t *);
6877c478bd9Sstevel@tonic-gate void	free_vp_pages(struct vnode *, u_offset_t, size_t);
6887c478bd9Sstevel@tonic-gate int	page_reclaim(page_t *, kmutex_t *);
6897c478bd9Sstevel@tonic-gate void	page_destroy(page_t *, int);
6907c478bd9Sstevel@tonic-gate void	page_destroy_pages(page_t *);
6917c478bd9Sstevel@tonic-gate void	page_destroy_free(page_t *);
6927c478bd9Sstevel@tonic-gate void	page_rename(page_t *, struct vnode *, u_offset_t);
6937c478bd9Sstevel@tonic-gate int	page_hashin(page_t *, struct vnode *, u_offset_t, kmutex_t *);
6947c478bd9Sstevel@tonic-gate void	page_hashout(page_t *, kmutex_t *);
6957c478bd9Sstevel@tonic-gate int	page_num_hashin(pfn_t, struct vnode *, u_offset_t);
6967c478bd9Sstevel@tonic-gate void	page_add(page_t **, page_t *);
6977c478bd9Sstevel@tonic-gate void	page_add_common(page_t **, page_t *);
6987c478bd9Sstevel@tonic-gate void	page_sub(page_t **, page_t *);
6997c478bd9Sstevel@tonic-gate void	page_sub_common(page_t **, page_t *);
7007c478bd9Sstevel@tonic-gate page_t	*page_get_freelist(struct vnode *, u_offset_t, struct seg *,
7017c478bd9Sstevel@tonic-gate 		caddr_t, size_t, uint_t, struct lgrp *);
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate page_t	*page_get_cachelist(struct vnode *, u_offset_t, struct seg *,
7047c478bd9Sstevel@tonic-gate 		caddr_t, uint_t, struct lgrp *);
7057c478bd9Sstevel@tonic-gate void	page_list_add(page_t *, int);
7067c478bd9Sstevel@tonic-gate void	page_boot_demote(page_t *);
7077c478bd9Sstevel@tonic-gate void	page_promote_size(page_t *, uint_t);
7087c478bd9Sstevel@tonic-gate void	page_list_add_pages(page_t *, int);
7097c478bd9Sstevel@tonic-gate void	page_list_sub(page_t *, int);
710*affbd3ccSkchow void	page_list_xfer(page_t *, int, int);
7117c478bd9Sstevel@tonic-gate void	page_list_break(page_t **, page_t **, size_t);
7127c478bd9Sstevel@tonic-gate void	page_list_concat(page_t **, page_t **);
7137c478bd9Sstevel@tonic-gate void	page_vpadd(page_t **, page_t *);
7147c478bd9Sstevel@tonic-gate void	page_vpsub(page_t **, page_t *);
7157c478bd9Sstevel@tonic-gate int	page_lock(page_t *, se_t, kmutex_t *, reclaim_t);
7167c478bd9Sstevel@tonic-gate int	page_lock_es(page_t *, se_t, kmutex_t *, reclaim_t, int);
7177c478bd9Sstevel@tonic-gate void page_lock_clr_exclwanted(page_t *);
7187c478bd9Sstevel@tonic-gate int	page_trylock(page_t *, se_t);
7197c478bd9Sstevel@tonic-gate int	page_try_reclaim_lock(page_t *, se_t, int);
7207c478bd9Sstevel@tonic-gate int	page_tryupgrade(page_t *);
7217c478bd9Sstevel@tonic-gate void	page_downgrade(page_t *);
7227c478bd9Sstevel@tonic-gate void	page_unlock(page_t *);
7237c478bd9Sstevel@tonic-gate void	page_lock_delete(page_t *);
7247c478bd9Sstevel@tonic-gate int	page_pp_lock(page_t *, int, int);
7257c478bd9Sstevel@tonic-gate void	page_pp_unlock(page_t *, int, int);
7267c478bd9Sstevel@tonic-gate int	page_resv(pgcnt_t, uint_t);
7277c478bd9Sstevel@tonic-gate void	page_unresv(pgcnt_t);
7287c478bd9Sstevel@tonic-gate void	page_pp_useclaim(page_t *, page_t *, uint_t);
7297c478bd9Sstevel@tonic-gate int	page_addclaim(page_t *);
7307c478bd9Sstevel@tonic-gate int	page_subclaim(page_t *);
7317c478bd9Sstevel@tonic-gate int	page_addclaim_pages(page_t **);
7327c478bd9Sstevel@tonic-gate int	page_subclaim_pages(page_t **);
7337c478bd9Sstevel@tonic-gate pfn_t	page_pptonum(page_t *);
7347c478bd9Sstevel@tonic-gate page_t	*page_numtopp(pfn_t, se_t);
7357c478bd9Sstevel@tonic-gate page_t	*page_numtopp_noreclaim(pfn_t, se_t);
7367c478bd9Sstevel@tonic-gate page_t	*page_numtopp_nolock(pfn_t);
7377c478bd9Sstevel@tonic-gate page_t	*page_numtopp_nowait(pfn_t, se_t);
7387c478bd9Sstevel@tonic-gate page_t  *page_first();
7397c478bd9Sstevel@tonic-gate page_t  *page_next(page_t *);
7407c478bd9Sstevel@tonic-gate page_t  *page_list_next(page_t *);
7417c478bd9Sstevel@tonic-gate page_t	*page_nextn(page_t *, ulong_t);
7427c478bd9Sstevel@tonic-gate page_t	*page_next_scan_init(void **);
7437c478bd9Sstevel@tonic-gate page_t	*page_next_scan_large(page_t *, ulong_t *, void **);
7447c478bd9Sstevel@tonic-gate void    prefetch_page_r(void *);
7457c478bd9Sstevel@tonic-gate void	ppcopy(page_t *, page_t *);
7467c478bd9Sstevel@tonic-gate void	page_relocate_hash(page_t *, page_t *);
7477c478bd9Sstevel@tonic-gate void	pagezero(page_t *, uint_t, uint_t);
7487c478bd9Sstevel@tonic-gate void	pagescrub(page_t *, uint_t, uint_t);
7497c478bd9Sstevel@tonic-gate void	page_io_lock(page_t *);
7507c478bd9Sstevel@tonic-gate void	page_io_unlock(page_t *);
7517c478bd9Sstevel@tonic-gate int	page_io_trylock(page_t *);
7527c478bd9Sstevel@tonic-gate int	page_iolock_assert(page_t *);
7537c478bd9Sstevel@tonic-gate void	page_iolock_init(page_t *);
7547c478bd9Sstevel@tonic-gate pgcnt_t	page_busy(int);
7557c478bd9Sstevel@tonic-gate void	page_lock_init(void);
7567c478bd9Sstevel@tonic-gate ulong_t	page_share_cnt(page_t *);
7577c478bd9Sstevel@tonic-gate int	page_isshared(page_t *);
7587c478bd9Sstevel@tonic-gate int	page_isfree(page_t *);
7597c478bd9Sstevel@tonic-gate int	page_isref(page_t *);
7607c478bd9Sstevel@tonic-gate int	page_ismod(page_t *);
7617c478bd9Sstevel@tonic-gate int	page_release(page_t *, int);
7627c478bd9Sstevel@tonic-gate int	page_retire(page_t *, uchar_t);
7637c478bd9Sstevel@tonic-gate int	page_istoxic(page_t *);
7647c478bd9Sstevel@tonic-gate int	page_isfailing(page_t *);
7657c478bd9Sstevel@tonic-gate int	page_isretired(page_t *);
7667c478bd9Sstevel@tonic-gate int	page_deteriorating(page_t *);
7677c478bd9Sstevel@tonic-gate void	page_settoxic(page_t *, uchar_t);
7687c478bd9Sstevel@tonic-gate void	page_clrtoxic(page_t *);
7697c478bd9Sstevel@tonic-gate void	page_clrtoxic_flag(page_t *, uchar_t);
7707c478bd9Sstevel@tonic-gate int	page_isfaulty(page_t *);
7717c478bd9Sstevel@tonic-gate int	page_mem_avail(pgcnt_t);
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate void page_set_props(page_t *, uint_t);
7747c478bd9Sstevel@tonic-gate void page_clr_all_props(page_t *);
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate kmutex_t	*page_vnode_mutex(struct vnode *);
7777c478bd9Sstevel@tonic-gate kmutex_t	*page_se_mutex(struct page *);
7787c478bd9Sstevel@tonic-gate kmutex_t	*page_szc_lock(struct page *);
7797c478bd9Sstevel@tonic-gate int		page_szc_lock_assert(struct page *pp);
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate /*
7827c478bd9Sstevel@tonic-gate  * Page relocation interfaces. page_relocate() is generic.
7837c478bd9Sstevel@tonic-gate  * page_get_replacement_page() is provided by the PSM.
7847c478bd9Sstevel@tonic-gate  * page_free_replacement_page() is generic.
7857c478bd9Sstevel@tonic-gate  */
7867c478bd9Sstevel@tonic-gate int group_page_trylock(page_t *, se_t);
7877c478bd9Sstevel@tonic-gate void group_page_unlock(page_t *);
7887c478bd9Sstevel@tonic-gate int page_relocate(page_t **, page_t **, int, int, spgcnt_t *, struct lgrp *);
7897c478bd9Sstevel@tonic-gate int do_page_relocate(page_t **, page_t **, int, spgcnt_t *, struct lgrp *);
7907c478bd9Sstevel@tonic-gate page_t *page_get_replacement_page(page_t *, struct lgrp *, uint_t);
7917c478bd9Sstevel@tonic-gate void page_free_replacement_page(page_t *);
7927c478bd9Sstevel@tonic-gate int page_relocate_cage(page_t **, page_t **);
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate int page_try_demote_pages(page_t *);
7957c478bd9Sstevel@tonic-gate void page_demote_free_pages(page_t *);
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate struct anon_map;
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate void page_mark_migrate(struct seg *, caddr_t, size_t, struct anon_map *,
8007c478bd9Sstevel@tonic-gate     ulong_t, vnode_t *, u_offset_t, int);
8017c478bd9Sstevel@tonic-gate void page_migrate(struct seg *, caddr_t, page_t **, pgcnt_t);
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate /*
8047c478bd9Sstevel@tonic-gate  * Tell the PIM we are adding physical memory
8057c478bd9Sstevel@tonic-gate  */
8067c478bd9Sstevel@tonic-gate void add_physmem(page_t *, size_t, pfn_t);
8077c478bd9Sstevel@tonic-gate void add_physmem_cb(page_t *, pfn_t);	/* callback for page_t part */
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate /*
8107c478bd9Sstevel@tonic-gate  * hw_page_array[] is configured with hardware supported page sizes by
8117c478bd9Sstevel@tonic-gate  * platform specific code.
8127c478bd9Sstevel@tonic-gate  */
8137c478bd9Sstevel@tonic-gate typedef struct {
8147c478bd9Sstevel@tonic-gate 	size_t	hp_size;
8157c478bd9Sstevel@tonic-gate 	uint_t	hp_shift;
8167c478bd9Sstevel@tonic-gate 	pgcnt_t	hp_pgcnt;	/* base pagesize cnt */
8177c478bd9Sstevel@tonic-gate } hw_pagesize_t;
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate extern hw_pagesize_t	hw_page_array[];
8207c478bd9Sstevel@tonic-gate extern uint_t		page_colors, page_colors_mask;
8217c478bd9Sstevel@tonic-gate extern uint_t		page_coloring_shift;
8227c478bd9Sstevel@tonic-gate extern int		cpu_page_colors;
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate uint_t	page_num_pagesizes(void);
8257c478bd9Sstevel@tonic-gate uint_t	page_num_user_pagesizes(void);
8267c478bd9Sstevel@tonic-gate size_t	page_get_pagesize(uint_t);
8277c478bd9Sstevel@tonic-gate size_t	page_get_user_pagesize(uint_t n);
8287c478bd9Sstevel@tonic-gate pgcnt_t	page_get_pagecnt(uint_t);
8297c478bd9Sstevel@tonic-gate uint_t	page_get_shift(uint_t);
8307c478bd9Sstevel@tonic-gate int	page_szc(size_t);
8314abce959Smec int	page_szc_user_filtered(size_t);
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate /* page_get_replacement page flags */
8357c478bd9Sstevel@tonic-gate #define	PGR_SAMESZC	0x1	/* only look for page size same as orig */
8367c478bd9Sstevel@tonic-gate #define	PGR_NORELOC	0x2	/* allocate a P_NORELOC page */
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate /*
8417c478bd9Sstevel@tonic-gate  * Constants used for the p_iolock_state
8427c478bd9Sstevel@tonic-gate  */
8437c478bd9Sstevel@tonic-gate #define	PAGE_IO_INUSE	0x1
8447c478bd9Sstevel@tonic-gate #define	PAGE_IO_WANTED	0x2
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate /*
8477c478bd9Sstevel@tonic-gate  * Constants used for page_release status
8487c478bd9Sstevel@tonic-gate  */
8497c478bd9Sstevel@tonic-gate #define	PGREL_NOTREL    0x1
8507c478bd9Sstevel@tonic-gate #define	PGREL_CLEAN	0x2
8517c478bd9Sstevel@tonic-gate #define	PGREL_MOD	0x3
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate /*
8547c478bd9Sstevel@tonic-gate  * The p_state field holds what used to be the p_age and p_free
8557c478bd9Sstevel@tonic-gate  * bits.  These fields are protected by p_selock (see above).
8567c478bd9Sstevel@tonic-gate  */
8577c478bd9Sstevel@tonic-gate #define	P_FREE		0x80		/* Page on free list */
8587c478bd9Sstevel@tonic-gate #define	P_NORELOC	0x40		/* Page is non-relocatable */
8597c478bd9Sstevel@tonic-gate #define	P_MIGRATE	0x20		/* Migrate page on next touch */
8607c478bd9Sstevel@tonic-gate #define	P_SWAP		0x10		/* belongs to vnode that is V_ISSWAP */
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate #define	PP_ISFREE(pp)		((pp)->p_state & P_FREE)
8637c478bd9Sstevel@tonic-gate #define	PP_ISAGED(pp)		(((pp)->p_state & P_FREE) && \
8647c478bd9Sstevel@tonic-gate 					((pp)->p_vnode == NULL))
8657c478bd9Sstevel@tonic-gate #define	PP_ISNORELOC(pp)	((pp)->p_state & P_NORELOC)
8667c478bd9Sstevel@tonic-gate #define	PP_ISMIGRATE(pp)	((pp)->p_state & P_MIGRATE)
8677c478bd9Sstevel@tonic-gate #define	PP_ISSWAP(pp)		((pp)->p_state & P_SWAP)
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate #define	PP_SETFREE(pp)		((pp)->p_state = ((pp)->p_state & ~P_MIGRATE) \
8707c478bd9Sstevel@tonic-gate 				| P_FREE)
8717c478bd9Sstevel@tonic-gate #define	PP_SETAGED(pp)		ASSERT(PP_ISAGED(pp))
8727c478bd9Sstevel@tonic-gate #define	PP_SETNORELOC(pp)	((pp)->p_state |= P_NORELOC)
8737c478bd9Sstevel@tonic-gate #define	PP_SETMIGRATE(pp)	((pp)->p_state |= P_MIGRATE)
8747c478bd9Sstevel@tonic-gate #define	PP_SETSWAP(pp)		((pp)->p_state |= P_SWAP)
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate #define	PP_CLRFREE(pp)		((pp)->p_state &= ~P_FREE)
8777c478bd9Sstevel@tonic-gate #define	PP_CLRAGED(pp)		ASSERT(!PP_ISAGED(pp))
8787c478bd9Sstevel@tonic-gate #define	PP_CLRNORELOC(pp)	((pp)->p_state &= ~P_NORELOC)
8797c478bd9Sstevel@tonic-gate #define	PP_CLRMIGRATE(pp)	((pp)->p_state &= ~P_MIGRATE)
8807c478bd9Sstevel@tonic-gate #define	PP_CLRSWAP(pp)		((pp)->p_state &= ~P_SWAP)
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate /*
8857c478bd9Sstevel@tonic-gate  * kpm large page description.
8867c478bd9Sstevel@tonic-gate  * The virtual address range of segkpm is divided into chunks of
8877c478bd9Sstevel@tonic-gate  * kpm_pgsz. Each chunk is controlled by a kpm_page_t. The ushort
8887c478bd9Sstevel@tonic-gate  * is sufficient for 2^^15 * PAGESIZE, so e.g. the maximum kpm_pgsz
8897c478bd9Sstevel@tonic-gate  * for 8K is 256M and 2G for 64K pages. It it kept as small as
8907c478bd9Sstevel@tonic-gate  * possible to save physical memory space.
8917c478bd9Sstevel@tonic-gate  *
8927c478bd9Sstevel@tonic-gate  * There are 2 segkpm mapping windows within in the virtual address
8937c478bd9Sstevel@tonic-gate  * space when we have to prevent VAC alias conflicts. The so called
8947c478bd9Sstevel@tonic-gate  * Alias window (mappings are always by PAGESIZE) is controlled by
8957c478bd9Sstevel@tonic-gate  * kp_refcnta. The regular window is controlled by kp_refcnt for the
8967c478bd9Sstevel@tonic-gate  * normal operation, which is to use the largest available pagesize.
8977c478bd9Sstevel@tonic-gate  * When VAC alias conflicts are present within a chunk in the regular
8987c478bd9Sstevel@tonic-gate  * window the large page mapping is broken up into smaller PAGESIZE
8997c478bd9Sstevel@tonic-gate  * mappings. kp_refcntc is used to control the pages that are invoked
9007c478bd9Sstevel@tonic-gate  * in the conflict and kp_refcnts holds the active mappings done
9017c478bd9Sstevel@tonic-gate  * with the small page size. In non vac conflict mode kp_refcntc is
9027c478bd9Sstevel@tonic-gate  * also used as "go" indication (-1) for the trap level tsbmiss
9037c478bd9Sstevel@tonic-gate  * handler.
9047c478bd9Sstevel@tonic-gate  */
9057c478bd9Sstevel@tonic-gate typedef struct kpm_page {
9067c478bd9Sstevel@tonic-gate 	short kp_refcnt;	/* pages mapped large */
9077c478bd9Sstevel@tonic-gate 	short kp_refcnta;	/* pages mapped in Alias window */
9087c478bd9Sstevel@tonic-gate 	short kp_refcntc;	/* TL-tsbmiss flag; #vac alias conflict pages */
9097c478bd9Sstevel@tonic-gate 	short kp_refcnts;	/* vac alias: pages mapped small */
9107c478bd9Sstevel@tonic-gate } kpm_page_t;
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate /*
9137c478bd9Sstevel@tonic-gate  * Note: khl_lock offset changes must be reflected in sfmmu_asm.s
9147c478bd9Sstevel@tonic-gate  */
9157c478bd9Sstevel@tonic-gate typedef struct kpm_hlk {
9167c478bd9Sstevel@tonic-gate 	kmutex_t khl_mutex;	/* kpm_page mutex */
9177c478bd9Sstevel@tonic-gate 	uint_t   khl_lock;	/* trap level tsbmiss handling */
9187c478bd9Sstevel@tonic-gate } kpm_hlk_t;
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate /*
9217c478bd9Sstevel@tonic-gate  * kpm small page description.
9227c478bd9Sstevel@tonic-gate  * When kpm_pgsz is equal to PAGESIZE a smaller representation is used
9237c478bd9Sstevel@tonic-gate  * to save memory space. Alias range mappings and regular segkpm
9247c478bd9Sstevel@tonic-gate  * mappings are done in units of PAGESIZE and can share the mapping
9257c478bd9Sstevel@tonic-gate  * information and the mappings are always distinguishable by their
9267c478bd9Sstevel@tonic-gate  * virtual address. Other information neeeded for VAC conflict prevention
9277c478bd9Sstevel@tonic-gate  * is already available on a per page basis. There are basically 3 states
9287c478bd9Sstevel@tonic-gate  * a kpm_spage can have: not mapped (0), mapped in Alias range or virtually
9297c478bd9Sstevel@tonic-gate  * uncached (1) and mapped in the regular segkpm window (-1). The -1 value
9307c478bd9Sstevel@tonic-gate  * is also used as "go" indication for the segkpm trap level tsbmiss
9317c478bd9Sstevel@tonic-gate  * handler for small pages (value is kept the same as it is used for large
9327c478bd9Sstevel@tonic-gate  * mappings).
9337c478bd9Sstevel@tonic-gate  */
9347c478bd9Sstevel@tonic-gate typedef struct kpm_spage {
9357c478bd9Sstevel@tonic-gate 	char	kp_mapped;	/* page mapped small */
9367c478bd9Sstevel@tonic-gate } kpm_spage_t;
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate /*
9397c478bd9Sstevel@tonic-gate  * Note: kshl_lock offset changes must be reflected in sfmmu_asm.s
9407c478bd9Sstevel@tonic-gate  */
9417c478bd9Sstevel@tonic-gate typedef struct kpm_shlk {
9427c478bd9Sstevel@tonic-gate 	uint_t   kshl_lock;	/* trap level tsbmiss handling */
9437c478bd9Sstevel@tonic-gate } kpm_shlk_t;
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate /*
9467c478bd9Sstevel@tonic-gate  * Each segment of physical memory is described by a memseg struct.
9477c478bd9Sstevel@tonic-gate  * Within a segment, memory is considered contiguous. The members
9487c478bd9Sstevel@tonic-gate  * can be categorized as follows:
9497c478bd9Sstevel@tonic-gate  * . Platform independent:
9507c478bd9Sstevel@tonic-gate  *         pages, epages, pages_base, pages_end, next, lnext.
9517c478bd9Sstevel@tonic-gate  * . 64bit only but platform independent:
9527c478bd9Sstevel@tonic-gate  *         kpm_pbase, kpm_nkpmpgs, kpm_pages, kpm_spages.
9537c478bd9Sstevel@tonic-gate  * . Really platform or mmu specific:
9547c478bd9Sstevel@tonic-gate  *         pagespa, epagespa, nextpa, kpm_pagespa.
9557c478bd9Sstevel@tonic-gate  * . Mixed:
9567c478bd9Sstevel@tonic-gate  *         msegflags.
9577c478bd9Sstevel@tonic-gate  */
9587c478bd9Sstevel@tonic-gate struct memseg {
9597c478bd9Sstevel@tonic-gate 	page_t *pages, *epages;		/* [from, to] in page array */
9607c478bd9Sstevel@tonic-gate 	pfn_t pages_base, pages_end;	/* [from, to] in page numbers */
9617c478bd9Sstevel@tonic-gate 	struct memseg *next;		/* next segment in list */
9627c478bd9Sstevel@tonic-gate #if defined(__sparc)
9637c478bd9Sstevel@tonic-gate 	struct memseg *lnext;		/* next segment in deleted list */
9647c478bd9Sstevel@tonic-gate 	uint64_t pagespa, epagespa;	/* [from, to] page array physical */
9657c478bd9Sstevel@tonic-gate 	uint64_t nextpa;		/* physical next pointer */
9667c478bd9Sstevel@tonic-gate 	pfn_t	kpm_pbase;		/* start of kpm range */
9677c478bd9Sstevel@tonic-gate 	pgcnt_t kpm_nkpmpgs;		/* # of kpm_pgsz pages */
9687c478bd9Sstevel@tonic-gate 	union _mseg_un {
9697c478bd9Sstevel@tonic-gate 		kpm_page_t  *kpm_lpgs;	/* ptr to kpm_page array */
9707c478bd9Sstevel@tonic-gate 		kpm_spage_t *kpm_spgs;	/* ptr to kpm_spage array */
9717c478bd9Sstevel@tonic-gate 	} mseg_un;
9727c478bd9Sstevel@tonic-gate 	uint64_t kpm_pagespa;		/* physical ptr to kpm (s)pages array */
9737c478bd9Sstevel@tonic-gate 	uint_t msegflags;		/* memseg flags */
9747c478bd9Sstevel@tonic-gate #endif /* __sparc */
9757c478bd9Sstevel@tonic-gate };
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate /* memseg union aliases */
9787c478bd9Sstevel@tonic-gate #define	kpm_pages	mseg_un.kpm_lpgs
9797c478bd9Sstevel@tonic-gate #define	kpm_spages	mseg_un.kpm_spgs
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate /* msegflags */
9827c478bd9Sstevel@tonic-gate #define	MEMSEG_DYNAMIC		0x1	/* DR: memory was added dynamically */
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate /* memseg support macros */
9857c478bd9Sstevel@tonic-gate #define	MSEG_NPAGES(SEG)	((SEG)->pages_end - (SEG)->pages_base)
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate /* memseg hash */
9887c478bd9Sstevel@tonic-gate #define	MEM_HASH_SHIFT		0x9
9897c478bd9Sstevel@tonic-gate #define	N_MEM_SLOTS		0x200		/* must be a power of 2 */
9907c478bd9Sstevel@tonic-gate #define	MEMSEG_PFN_HASH(pfn)	(((pfn)/mhash_per_slot) & (N_MEM_SLOTS - 1))
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate /* memseg  externals */
9937c478bd9Sstevel@tonic-gate extern struct memseg *memsegs;		/* list of memory segments */
9947c478bd9Sstevel@tonic-gate extern ulong_t mhash_per_slot;
9957c478bd9Sstevel@tonic-gate extern uint64_t memsegspa;		/* memsegs as physical address */
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate void build_pfn_hash();
9987c478bd9Sstevel@tonic-gate extern struct memseg *page_numtomemseg_nolock(pfn_t pfnum);
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
10027c478bd9Sstevel@tonic-gate }
10037c478bd9Sstevel@tonic-gate #endif
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate #endif	/* _VM_PAGE_H */
1006