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