1139825Simp/*-
21541Srgrimes * Copyright (c) 1991, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * This code is derived from software contributed to Berkeley by
61541Srgrimes * The Mach Operating System project at Carnegie-Mellon University.
71541Srgrimes *
81541Srgrimes * Redistribution and use in source and binary forms, with or without
91541Srgrimes * modification, are permitted provided that the following conditions
101541Srgrimes * are met:
111541Srgrimes * 1. Redistributions of source code must retain the above copyright
121541Srgrimes *    notice, this list of conditions and the following disclaimer.
131541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer in the
151541Srgrimes *    documentation and/or other materials provided with the distribution.
161541Srgrimes * 4. Neither the name of the University nor the names of its contributors
171541Srgrimes *    may be used to endorse or promote products derived from this software
181541Srgrimes *    without specific prior written permission.
191541Srgrimes *
201541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301541Srgrimes * SUCH DAMAGE.
311541Srgrimes *
321817Sdg *	from: @(#)vm_page.h	8.2 (Berkeley) 12/13/93
331541Srgrimes *
341541Srgrimes *
351541Srgrimes * Copyright (c) 1987, 1990 Carnegie-Mellon University.
361541Srgrimes * All rights reserved.
371541Srgrimes *
381541Srgrimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young
395455Sdg *
401541Srgrimes * Permission to use, copy, modify and distribute this software and
411541Srgrimes * its documentation is hereby granted, provided that both the copyright
421541Srgrimes * notice and this permission notice appear in all copies of the
431541Srgrimes * software, derivative works or modified versions, and any portions
441541Srgrimes * thereof, and that both notices appear in supporting documentation.
455455Sdg *
465455Sdg * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
475455Sdg * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
481541Srgrimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
495455Sdg *
501541Srgrimes * Carnegie Mellon requests users of this software to return to
511541Srgrimes *
521541Srgrimes *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
531541Srgrimes *  School of Computer Science
541541Srgrimes *  Carnegie Mellon University
551541Srgrimes *  Pittsburgh PA 15213-3890
561541Srgrimes *
571541Srgrimes * any improvements or extensions that they make and grant Carnegie the
581541Srgrimes * rights to redistribute these changes.
591817Sdg *
6050477Speter * $FreeBSD: stable/10/sys/vm/vm_page.h 307672 2016-10-20 13:12:19Z kib $
611541Srgrimes */
621541Srgrimes
631541Srgrimes/*
641541Srgrimes *	Resident memory system definitions.
651541Srgrimes */
661541Srgrimes
671541Srgrimes#ifndef	_VM_PAGE_
681541Srgrimes#define	_VM_PAGE_
691541Srgrimes
706816Sdg#include <vm/pmap.h>
7138517Sdfr
721541Srgrimes/*
731541Srgrimes *	Management of resident (logical) pages.
741541Srgrimes *
751541Srgrimes *	A small structure is kept for each resident
761541Srgrimes *	page, indexed by page number.  Each structure
77251367Salc *	is an element of several collections:
781541Srgrimes *
79251367Salc *		A radix tree used to quickly
801541Srgrimes *		perform object/offset lookups
811541Srgrimes *
821541Srgrimes *		A list of all pages for a given object,
831541Srgrimes *		so they can be quickly deactivated at
841541Srgrimes *		time of deallocation.
851541Srgrimes *
861541Srgrimes *		An ordered list of pages due for pageout.
871541Srgrimes *
881541Srgrimes *	In addition, the structure contains the object
891541Srgrimes *	and offset to which this page belongs (for pageout),
901541Srgrimes *	and sundry status bits.
911541Srgrimes *
92223307Salc *	In general, operations on this structure's mutable fields are
93223307Salc *	synchronized using either one of or a combination of the lock on the
94223307Salc *	object that the page belongs to (O), the pool lock for the page (P),
95242941Salc *	or the lock for either the free or paging queue (Q).  If a field is
96223307Salc *	annotated below with two of these locks, then holding either lock is
97225840Skib *	sufficient for read access, but both locks are required for write
98223307Salc *	access.
99223307Salc *
100225840Skib *	In contrast, the synchronization of accesses to the page's
101225840Skib *	dirty field is machine dependent (M).  In the
102225840Skib *	machine-independent layer, the lock on the object that the
103225840Skib *	page belongs to must be held in order to operate on the field.
104225840Skib *	However, the pmap layer is permitted to set all bits within
105225840Skib *	the field without holding that lock.  If the underlying
106225840Skib *	architecture does not support atomic read-modify-write
107225840Skib *	operations on the field's type, then the machine-independent
108225843Skib *	layer uses a 32-bit atomic on the aligned 32-bit word that
109225840Skib *	contains the dirty field.  In the machine-independent layer,
110225840Skib *	the implementation of read-modify-write operations on the
111225840Skib *	field is encapsulated in vm_page_clear_dirty_mask().
1121541Srgrimes */
1131541Srgrimes
114227102Skib#if PAGE_SIZE == 4096
115227102Skib#define VM_PAGE_BITS_ALL 0xffu
116227102Skibtypedef uint8_t vm_page_bits_t;
117227102Skib#elif PAGE_SIZE == 8192
118227102Skib#define VM_PAGE_BITS_ALL 0xffffu
119227102Skibtypedef uint16_t vm_page_bits_t;
120227102Skib#elif PAGE_SIZE == 16384
121227102Skib#define VM_PAGE_BITS_ALL 0xffffffffu
122227102Skibtypedef uint32_t vm_page_bits_t;
123227102Skib#elif PAGE_SIZE == 32768
124227102Skib#define VM_PAGE_BITS_ALL 0xfffffffffffffffflu
125227102Skibtypedef uint64_t vm_page_bits_t;
126227102Skib#endif
127227102Skib
1281541Srgrimesstruct vm_page {
129254182Skib	union {
130254182Skib		TAILQ_ENTRY(vm_page) q; /* page queue or free list (Q) */
131254182Skib		struct {
132254182Skib			SLIST_ENTRY(vm_page) ss; /* private slists */
133254182Skib			void *pv;
134254182Skib		} s;
135254182Skib		struct {
136254182Skib			u_long p;
137254182Skib			u_long v;
138254182Skib		} memguard;
139254182Skib	} plinks;
140254182Skib	TAILQ_ENTRY(vm_page) listq;	/* pages in same object (O) */
141254182Skib	vm_object_t object;		/* which object am I in (O,P) */
142217479Salc	vm_pindex_t pindex;		/* offset into object (O,P) */
143121511Salc	vm_paddr_t phys_addr;		/* physical address of page */
14460755Speter	struct md_page md;		/* machine dependant stuff */
145255608Skib	u_int wire_count;		/* wired down maps refs (P) */
146255608Skib	volatile u_int busy_lock;	/* busy owners lock */
147255608Skib	uint16_t hold_count;		/* page hold count (P) */
148255608Skib	uint16_t flags;			/* page PG_* flags (P) */
149255608Skib	uint8_t aflags;			/* access is atomic */
150255608Skib	uint8_t oflags;			/* page VPO_* flags (O) */
151207702Salc	uint8_t	queue;			/* page queue index (P,Q) */
152186719Skib	int8_t segind;
153170816Salc	uint8_t	order;			/* index of the buddy queue */
154170816Salc	uint8_t pool;
155249278Sattilio	u_char	act_count;		/* page usage count (P) */
156254182Skib	/* NOTE that these must support one bit per DEV_BSIZE in a page */
15713490Sdyson	/* so, on normal X86 kernels, they must be at least 8 bits wide */
158227102Skib	vm_page_bits_t valid;		/* map of valid DEV_BSIZE chunks (O) */
159227102Skib	vm_page_bits_t dirty;		/* map of dirty DEV_BSIZE chunks (M) */
160269072Skib	int8_t psind;			/* pagesizes[] index (O) */
1611541Srgrimes};
1621541Srgrimes
163161125Salc/*
164161125Salc * Page flags stored in oflags:
165161125Salc *
166161125Salc * Access to these page flags is synchronized by the lock on the object
167161125Salc * containing the page (O).
168224746Skib *
169224746Skib * Note: VPO_UNMANAGED (used by OBJT_DEVICE, OBJT_PHYS and OBJT_SG)
170224746Skib * 	 indicates that the page is not under PV management but
171224746Skib * 	 otherwise should be treated as a normal page.  Pages not
172224746Skib * 	 under PV management cannot be paged out via the
173224746Skib * 	 object/vm_page_t because there is no knowledge of their pte
174224746Skib * 	 mappings, and such pages are also not on any PQ queue.
175224746Skib *
176161125Salc */
177254138Sattilio#define	VPO_UNUSED01	0x01		/* --available-- */
178254138Sattilio#define	VPO_SWAPSLEEP	0x02		/* waiting for swap to finish */
179242300Salc#define	VPO_UNMANAGED	0x04		/* no PV management for page */
180242300Salc#define	VPO_SWAPINPROG	0x08		/* swap I/O in progress on page */
181242300Salc#define	VPO_NOSYNC	0x10		/* do not collect for syncer */
182161125Salc
183254138Sattilio/*
184254138Sattilio * Busy page implementation details.
185254138Sattilio * The algorithm is taken mostly by rwlock(9) and sx(9) locks implementation,
186254138Sattilio * even if the support for owner identity is removed because of size
187254138Sattilio * constraints.  Checks on lock recursion are then not possible, while the
188254138Sattilio * lock assertions effectiveness is someway reduced.
189254138Sattilio */
190254138Sattilio#define	VPB_BIT_SHARED		0x01
191254138Sattilio#define	VPB_BIT_EXCLUSIVE	0x02
192254138Sattilio#define	VPB_BIT_WAITERS		0x04
193254138Sattilio#define	VPB_BIT_FLAGMASK						\
194254138Sattilio	(VPB_BIT_SHARED | VPB_BIT_EXCLUSIVE | VPB_BIT_WAITERS)
195254138Sattilio
196254138Sattilio#define	VPB_SHARERS_SHIFT	3
197254138Sattilio#define	VPB_SHARERS(x)							\
198254138Sattilio	(((x) & ~VPB_BIT_FLAGMASK) >> VPB_SHARERS_SHIFT)
199254138Sattilio#define	VPB_SHARERS_WORD(x)	((x) << VPB_SHARERS_SHIFT | VPB_BIT_SHARED)
200254138Sattilio#define	VPB_ONE_SHARER		(1 << VPB_SHARERS_SHIFT)
201254138Sattilio
202254138Sattilio#define	VPB_SINGLE_EXCLUSIVER	VPB_BIT_EXCLUSIVE
203254138Sattilio
204254138Sattilio#define	VPB_UNBUSIED		VPB_SHARERS_WORD(0)
205254138Sattilio
206217508Salc#define	PQ_NONE		255
207217508Salc#define	PQ_INACTIVE	0
208217508Salc#define	PQ_ACTIVE	1
209242300Salc#define	PQ_COUNT	2
21018169Sdyson
211242941SalcTAILQ_HEAD(pglist, vm_page);
212254182SkibSLIST_HEAD(spglist, vm_page);
21318169Sdyson
214242941Salcstruct vm_pagequeue {
215242941Salc	struct mtx	pq_mutex;
216242941Salc	struct pglist	pq_pl;
217254065Skib	int		pq_cnt;
218301833Sngie	u_int		* const pq_vcnt;
219254065Skib	const char	* const pq_name;
220242941Salc} __aligned(CACHE_LINE_SIZE);
22152647Salc
222242941Salc
223254065Skibstruct vm_domain {
224254065Skib	struct vm_pagequeue vmd_pagequeues[PQ_COUNT];
225254065Skib	u_int vmd_page_count;
226254065Skib	u_int vmd_free_count;
227254065Skib	long vmd_segs;	/* bitmask of the segments */
228254065Skib	boolean_t vmd_oom;
229254065Skib	int vmd_pass;	/* local pagedaemon pass */
230291935Skib	int vmd_oom_seq;
231288294Salc	int vmd_last_active_scan;
232254065Skib	struct vm_page vmd_marker; /* marker for pagedaemon private use */
233254065Skib};
234254065Skib
235254065Skibextern struct vm_domain vm_dom[MAXMEMDOM];
236254065Skib
237242941Salc#define	vm_pagequeue_assert_locked(pq)	mtx_assert(&(pq)->pq_mutex, MA_OWNED)
238242941Salc#define	vm_pagequeue_lock(pq)		mtx_lock(&(pq)->pq_mutex)
239242941Salc#define	vm_pagequeue_unlock(pq)		mtx_unlock(&(pq)->pq_mutex)
240242941Salc
241254065Skib#ifdef _KERNEL
242254065Skibstatic __inline void
243254065Skibvm_pagequeue_cnt_add(struct vm_pagequeue *pq, int addend)
244254065Skib{
245254065Skib
246254065Skib#ifdef notyet
247254065Skib	vm_pagequeue_assert_locked(pq);
248254065Skib#endif
249254065Skib	pq->pq_cnt += addend;
250254065Skib	atomic_add_int(pq->pq_vcnt, addend);
251254065Skib}
252254065Skib#define	vm_pagequeue_cnt_inc(pq)	vm_pagequeue_cnt_add((pq), 1)
253254065Skib#define	vm_pagequeue_cnt_dec(pq)	vm_pagequeue_cnt_add((pq), -1)
254254065Skib#endif	/* _KERNEL */
255254065Skib
256242402Sattilioextern struct mtx_padalign vm_page_queue_free_mtx;
257242402Sattilioextern struct mtx_padalign pa_lock[];
258197750Salc
259207410Skmacy#if defined(__arm__)
260207410Skmacy#define	PDRSHIFT	PDR_SHIFT
261207410Skmacy#elif !defined(PDRSHIFT)
262207410Skmacy#define PDRSHIFT	21
263207410Skmacy#endif
264207410Skmacy
265207410Skmacy#define	pa_index(pa)	((pa) >> PDRSHIFT)
266242402Sattilio#define	PA_LOCKPTR(pa)	((struct mtx *)(&pa_lock[pa_index(pa) % PA_LOCK_COUNT]))
267207410Skmacy#define	PA_LOCKOBJPTR(pa)	((struct lock_object *)PA_LOCKPTR((pa)))
268207410Skmacy#define	PA_LOCK(pa)	mtx_lock(PA_LOCKPTR(pa))
269207410Skmacy#define	PA_TRYLOCK(pa)	mtx_trylock(PA_LOCKPTR(pa))
270207410Skmacy#define	PA_UNLOCK(pa)	mtx_unlock(PA_LOCKPTR(pa))
271207410Skmacy#define	PA_UNLOCK_COND(pa) 			\
272207410Skmacy	do {		   			\
273208504Salc		if ((pa) != 0) {		\
274208504Salc			PA_UNLOCK((pa));	\
275208504Salc			(pa) = 0;		\
276208504Salc		}				\
277207410Skmacy	} while (0)
278207410Skmacy
279207410Skmacy#define	PA_LOCK_ASSERT(pa, a)	mtx_assert(PA_LOCKPTR(pa), (a))
280207410Skmacy
281228133Skib#ifdef KLD_MODULE
282228133Skib#define	vm_page_lock(m)		vm_page_lock_KBI((m), LOCK_FILE, LOCK_LINE)
283228133Skib#define	vm_page_unlock(m)	vm_page_unlock_KBI((m), LOCK_FILE, LOCK_LINE)
284228133Skib#define	vm_page_trylock(m)	vm_page_trylock_KBI((m), LOCK_FILE, LOCK_LINE)
285251183Salc#else	/* !KLD_MODULE */
286251183Salc#define	vm_page_lockptr(m)	(PA_LOCKPTR(VM_PAGE_TO_PHYS((m))))
287251183Salc#define	vm_page_lock(m)		mtx_lock(vm_page_lockptr((m)))
288251183Salc#define	vm_page_unlock(m)	mtx_unlock(vm_page_lockptr((m)))
289251183Salc#define	vm_page_trylock(m)	mtx_trylock(vm_page_lockptr((m)))
290251183Salc#endif
291228133Skib#if defined(INVARIANTS)
292251280Salc#define	vm_page_assert_locked(m)		\
293251280Salc    vm_page_assert_locked_KBI((m), __FILE__, __LINE__)
294228133Skib#define	vm_page_lock_assert(m, a)		\
295228133Skib    vm_page_lock_assert_KBI((m), (a), __FILE__, __LINE__)
296228133Skib#else
297251280Salc#define	vm_page_assert_locked(m)
298228133Skib#define	vm_page_lock_assert(m, a)
299228133Skib#endif
300207410Skmacy
3011541Srgrimes/*
302238998Salc * The vm_page's aflags are updated using atomic operations.  To set or clear
303238998Salc * these flags, the functions vm_page_aflag_set() and vm_page_aflag_clear()
304238998Salc * must be used.  Neither these flags nor these functions are part of the KBI.
3051541Srgrimes *
306251280Salc * PGA_REFERENCED may be cleared only if the page is locked.  It is set by
307251280Salc * both the MI and MD VM layers.  However, kernel loadable modules should not
308251280Salc * directly set this flag.  They should call vm_page_reference() instead.
309208990Salc *
310270440Skib * PGA_WRITEABLE is set exclusively on managed pages by pmap_enter().
311270440Skib * When it does so, the object must be locked, or the page must be
312270440Skib * exclusive busied.  The MI VM layer must never access this flag
313270440Skib * directly.  Instead, it should call pmap_page_is_write_mapped().
314233949Snwhitehorn *
315233949Snwhitehorn * PGA_EXECUTABLE may be set by pmap routines, and indicates that a page has
316237168Salc * at least one executable mapping.  It is not consumed by the MI VM layer.
3171541Srgrimes */
318225418Skib#define	PGA_WRITEABLE	0x01		/* page may be mapped writeable */
319225418Skib#define	PGA_REFERENCED	0x02		/* page has been referenced */
320233949Snwhitehorn#define	PGA_EXECUTABLE	0x04		/* page may be mapped executable */
3211541Srgrimes
3229507Sdg/*
323225418Skib * Page flags.  If changed at any other time than page allocation or
324225418Skib * freeing, the modification must be protected by the vm_page lock.
325225418Skib */
326242300Salc#define	PG_CACHED	0x0001		/* page is cached */
327242300Salc#define	PG_FREE		0x0002		/* page is free */
328242300Salc#define	PG_FICTITIOUS	0x0004		/* physical page doesn't exist */
329242300Salc#define	PG_ZERO		0x0008		/* page is zeroed */
330242300Salc#define	PG_MARKER	0x0010		/* special queue marker page */
331242300Salc#define	PG_WINATCFLS	0x0040		/* flush dirty page on inactive q */
332242300Salc#define	PG_NODUMP	0x0080		/* don't include this page in a dump */
333242300Salc#define	PG_UNHOLDFREE	0x0100		/* delayed free of a held page */
334225418Skib
335225418Skib/*
3369507Sdg * Misc constants.
3379507Sdg */
3389507Sdg#define ACT_DECLINE		1
3399507Sdg#define ACT_ADVANCE		3
34016750Sdyson#define ACT_INIT		5
34118169Sdyson#define ACT_MAX			64
3429507Sdg
34355206Speter#ifdef _KERNEL
344169291Salc
345238998Salc#include <sys/systm.h>
346238998Salc
347238998Salc#include <machine/atomic.h>
348238998Salc
3491541Srgrimes/*
350243176Salc * Each pageable resident page falls into one of four lists:
3511541Srgrimes *
3525455Sdg *	free
3531541Srgrimes *		Available for allocation now.
3549507Sdg *
3559507Sdg *	cache
356172317Salc *		Almost available for allocation. Still associated with
357172317Salc *		an object, but clean and immediately freeable.
3589507Sdg *
359171420Salc * The following lists are LRU sorted:
360171420Salc *
3611541Srgrimes *	inactive
36213765Smpp *		Low activity, candidates for reclamation.
3631541Srgrimes *		This is the list of pages that should be
3641541Srgrimes *		paged out next.
3659507Sdg *
3661541Srgrimes *	active
3679507Sdg *		Pages that are "active" i.e. they have been
3689507Sdg *		recently referenced.
36910544Sdyson *
3701541Srgrimes */
3711541Srgrimes
37212767Sdysonextern int vm_page_zero_count;
37312767Sdyson
3745455Sdgextern vm_page_t vm_page_array;		/* First resident page in table */
375235359Skibextern long vm_page_array_size;		/* number of vm_page_t's */
3765455Sdgextern long first_page;			/* first physical page number */
3771541Srgrimes
378170816Salc#define	VM_PAGE_IS_FREE(m)	(((m)->flags & PG_FREE) != 0)
379170816Salc
3801541Srgrimes#define VM_PAGE_TO_PHYS(entry)	((entry)->phys_addr)
3811541Srgrimes
382235372Skibvm_page_t PHYS_TO_VM_PAGE(vm_paddr_t pa);
3831541Srgrimes
384100276Salc/* page allocation classes: */
38533109Sdyson#define VM_ALLOC_NORMAL		0
38633109Sdyson#define VM_ALLOC_INTERRUPT	1
38733109Sdyson#define VM_ALLOC_SYSTEM		2
388100276Salc#define	VM_ALLOC_CLASS_MASK	3
389100276Salc/* page allocation flags: */
390106276Sjeff#define	VM_ALLOC_WIRED		0x0020	/* non pageable */
391106276Sjeff#define	VM_ALLOC_ZERO		0x0040	/* Try to obtain a zeroed page */
392106276Sjeff#define	VM_ALLOC_NOOBJ		0x0100	/* No associated object */
393136850Salc#define	VM_ALLOC_NOBUSY		0x0200	/* Do not busy the page */
394172317Salc#define	VM_ALLOC_IFCACHED	0x0400	/* Fail if the page is not cached */
395172317Salc#define	VM_ALLOC_IFNOTCACHED	0x0800	/* Fail if the page is cached */
396209713Skib#define	VM_ALLOC_IGN_SBUSY	0x1000	/* vm_page_grab() only */
397230623Skmacy#define	VM_ALLOC_NODUMP		0x2000	/* don't include in dump */
398254138Sattilio#define	VM_ALLOC_SBUSY		0x4000	/* Shared busy the page */
3995455Sdg
400209713Skib#define	VM_ALLOC_COUNT_SHIFT	16
401209713Skib#define	VM_ALLOC_COUNT(count)	((count) << VM_ALLOC_COUNT_SHIFT)
402209713Skib
403243040Skib#ifdef M_NOWAIT
404243040Skibstatic inline int
405243040Skibmalloc2vm_flags(int malloc_flags)
406243040Skib{
407243040Skib	int pflags;
408243040Skib
409243131Skib	KASSERT((malloc_flags & M_USE_RESERVE) == 0 ||
410243131Skib	    (malloc_flags & M_NOWAIT) != 0,
411243131Skib	    ("M_USE_RESERVE requires M_NOWAIT"));
412243040Skib	pflags = (malloc_flags & M_USE_RESERVE) != 0 ? VM_ALLOC_INTERRUPT :
413243040Skib	    VM_ALLOC_SYSTEM;
414243040Skib	if ((malloc_flags & M_ZERO) != 0)
415243040Skib		pflags |= VM_ALLOC_ZERO;
416243040Skib	if ((malloc_flags & M_NODUMP) != 0)
417243040Skib		pflags |= VM_ALLOC_NODUMP;
418243040Skib	return (pflags);
419243040Skib}
420243040Skib#endif
421243040Skib
422254138Sattiliovoid vm_page_busy_downgrade(vm_page_t m);
423307672Skibvoid vm_page_busy_sleep(vm_page_t m, const char *msg, bool nonshared);
42479248Sdillonvoid vm_page_flash(vm_page_t m);
42579248Sdillonvoid vm_page_hold(vm_page_t mem);
42679248Sdillonvoid vm_page_unhold(vm_page_t mem);
42779248Sdillonvoid vm_page_free(vm_page_t m);
42879248Sdillonvoid vm_page_free_zero(vm_page_t m);
42979248Sdillon
43079248Sdillonvoid vm_page_activate (vm_page_t);
431251591Salcvoid vm_page_advise(vm_page_t m, int advice);
43279248Sdillonvm_page_t vm_page_alloc (vm_object_t, vm_pindex_t, int);
433227568Salcvm_page_t vm_page_alloc_contig(vm_object_t object, vm_pindex_t pindex, int req,
434227568Salc    u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
435227568Salc    vm_paddr_t boundary, vm_memattr_t memattr);
436215973Sjchandravm_page_t vm_page_alloc_freelist(int, int);
43779248Sdillonvm_page_t vm_page_grab (vm_object_t, vm_pindex_t, int);
438193126Salcvoid vm_page_cache(vm_page_t);
439172341Salcvoid vm_page_cache_free(vm_object_t, vm_pindex_t, vm_pindex_t);
440172317Salcvoid vm_page_cache_transfer(vm_object_t, vm_pindex_t, vm_object_t);
44179248Sdillonint vm_page_try_to_cache (vm_page_t);
44279248Sdillonint vm_page_try_to_free (vm_page_t);
44379248Sdillonvoid vm_page_deactivate (vm_page_t);
444242941Salcvoid vm_page_dequeue(vm_page_t m);
445242941Salcvoid vm_page_dequeue_locked(vm_page_t m);
446209685Skibvm_page_t vm_page_find_least(vm_object_t, vm_pindex_t);
447219476Salcvm_page_t vm_page_getfake(vm_paddr_t paddr, vm_memattr_t memattr);
448235366Skibvoid vm_page_initfake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr);
449254141Sattilioint vm_page_insert (vm_page_t, vm_object_t, vm_pindex_t);
450234039Salcboolean_t vm_page_is_cached(vm_object_t object, vm_pindex_t pindex);
45179248Sdillonvm_page_t vm_page_lookup (vm_object_t, vm_pindex_t);
452209407Salcvm_page_t vm_page_next(vm_page_t m);
453207410Skmacyint vm_page_pa_tryrelock(pmap_t, vm_paddr_t, vm_paddr_t *);
454254065Skibstruct vm_pagequeue *vm_page_pagequeue(vm_page_t m);
455209407Salcvm_page_t vm_page_prev(vm_page_t m);
456269072Skibboolean_t vm_page_ps_is_valid(vm_page_t m);
457219476Salcvoid vm_page_putfake(vm_page_t m);
458239246Skibvoid vm_page_readahead_finish(vm_page_t m);
459225418Skibvoid vm_page_reference(vm_page_t m);
46079248Sdillonvoid vm_page_remove (vm_page_t);
461254141Sattilioint vm_page_rename (vm_page_t, vm_object_t, vm_pindex_t);
462254141Sattiliovm_page_t vm_page_replace(vm_page_t mnew, vm_object_t object,
463254141Sattilio    vm_pindex_t pindex);
464242941Salcvoid vm_page_requeue(vm_page_t m);
465242941Salcvoid vm_page_requeue_locked(vm_page_t m);
466254138Sattilioint vm_page_sbusied(vm_page_t m);
467228156Skibvoid vm_page_set_valid_range(vm_page_t m, int base, int size);
468254138Sattilioint vm_page_sleep_if_busy(vm_page_t m, const char *msg);
469127868Salcvm_offset_t vm_page_startup(vm_offset_t vaddr);
470254138Sattiliovoid vm_page_sunbusy(vm_page_t m);
471254138Sattilioint vm_page_trysbusy(vm_page_t m);
472216511Salcvoid vm_page_unhold_pages(vm_page_t *ma, int count);
47379248Sdillonvoid vm_page_unwire (vm_page_t, int);
474219476Salcvoid vm_page_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr);
47579248Sdillonvoid vm_page_wire (vm_page_t);
476254138Sattiliovoid vm_page_xunbusy_hard(vm_page_t m);
47779248Sdillonvoid vm_page_set_validclean (vm_page_t, int, int);
47879248Sdillonvoid vm_page_clear_dirty (vm_page_t, int, int);
47979248Sdillonvoid vm_page_set_invalid (vm_page_t, int, int);
48079248Sdillonint vm_page_is_valid (vm_page_t, int, int);
48179248Sdillonvoid vm_page_test_dirty (vm_page_t);
482227102Skibvm_page_bits_t vm_page_bits(int base, int size);
48345347Sjulianvoid vm_page_zero_invalid(vm_page_t m, boolean_t setvalid);
48443752Sdillonvoid vm_page_free_toq(vm_page_t m);
48582314Spetervoid vm_page_zero_idle_wakeup(void);
48698849Sken
487237346Salcvoid vm_page_dirty_KBI(vm_page_t m);
488228133Skibvoid vm_page_lock_KBI(vm_page_t m, const char *file, int line);
489228133Skibvoid vm_page_unlock_KBI(vm_page_t m, const char *file, int line);
490228133Skibint vm_page_trylock_KBI(vm_page_t m, const char *file, int line);
491228133Skib#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
492251280Salcvoid vm_page_assert_locked_KBI(vm_page_t m, const char *file, int line);
493228133Skibvoid vm_page_lock_assert_KBI(vm_page_t m, int a, const char *file, int line);
494228133Skib#endif
495228133Skib
496254138Sattilio#define	vm_page_assert_sbusied(m)					\
497254138Sattilio	KASSERT(vm_page_sbusied(m),					\
498254138Sattilio	    ("vm_page_assert_sbusied: page %p not shared busy @ %s:%d", \
499254138Sattilio	    (void *)m, __FILE__, __LINE__));
500254138Sattilio
501254138Sattilio#define	vm_page_assert_unbusied(m)					\
502254138Sattilio	KASSERT(!vm_page_busied(m),					\
503254138Sattilio	    ("vm_page_assert_unbusied: page %p busy @ %s:%d",		\
504254138Sattilio	    (void *)m, __FILE__, __LINE__));
505254138Sattilio
506254138Sattilio#define	vm_page_assert_xbusied(m)					\
507254138Sattilio	KASSERT(vm_page_xbusied(m),					\
508254138Sattilio	    ("vm_page_assert_xbusied: page %p not exclusive busy @ %s:%d", \
509254138Sattilio	    (void *)m, __FILE__, __LINE__));
510254138Sattilio
511254138Sattilio#define	vm_page_busied(m)						\
512254138Sattilio	((m)->busy_lock != VPB_UNBUSIED)
513254138Sattilio
514254138Sattilio#define	vm_page_sbusy(m) do {						\
515254138Sattilio	if (!vm_page_trysbusy(m))					\
516254138Sattilio		panic("%s: page %p failed shared busing", __func__, m);	\
517254138Sattilio} while (0)
518254138Sattilio
519254138Sattilio#define	vm_page_tryxbusy(m)						\
520254138Sattilio	(atomic_cmpset_acq_int(&m->busy_lock, VPB_UNBUSIED,		\
521254138Sattilio	    VPB_SINGLE_EXCLUSIVER))
522254138Sattilio
523254138Sattilio#define	vm_page_xbusied(m)						\
524254138Sattilio	((m->busy_lock & VPB_SINGLE_EXCLUSIVER) != 0)
525254138Sattilio
526254138Sattilio#define	vm_page_xbusy(m) do {						\
527254138Sattilio	if (!vm_page_tryxbusy(m))					\
528254138Sattilio		panic("%s: page %p failed exclusive busing", __func__,	\
529254138Sattilio		    m);							\
530254138Sattilio} while (0)
531254138Sattilio
532254138Sattilio#define	vm_page_xunbusy(m) do {						\
533254138Sattilio	if (!atomic_cmpset_rel_int(&(m)->busy_lock,			\
534254138Sattilio	    VPB_SINGLE_EXCLUSIVER, VPB_UNBUSIED))			\
535254138Sattilio		vm_page_xunbusy_hard(m);				\
536254138Sattilio} while (0)
537254138Sattilio
538222992Skib#ifdef INVARIANTS
539222992Skibvoid vm_page_object_lock_assert(vm_page_t m);
540222992Skib#define	VM_PAGE_OBJECT_LOCK_ASSERT(m)	vm_page_object_lock_assert(m)
541270440Skibvoid vm_page_assert_pga_writeable(vm_page_t m, uint8_t bits);
542270440Skib#define	VM_PAGE_ASSERT_PGA_WRITEABLE(m, bits)				\
543270440Skib	vm_page_assert_pga_writeable(m, bits)
544222992Skib#else
545222992Skib#define	VM_PAGE_OBJECT_LOCK_ASSERT(m)	(void)0
546270440Skib#define	VM_PAGE_ASSERT_PGA_WRITEABLE(m, bits)	(void)0
547222992Skib#endif
548222992Skib
549105549Salc/*
550238998Salc * We want to use atomic updates for the aflags field, which is 8 bits wide.
551238998Salc * However, not all architectures support atomic operations on 8-bit
552238998Salc * destinations.  In order that we can easily use a 32-bit operation, we
553238998Salc * require that the aflags field be 32-bit aligned.
554238998Salc */
555238998SalcCTASSERT(offsetof(struct vm_page, aflags) % sizeof(uint32_t) == 0);
556238998Salc
557238998Salc/*
558238998Salc *	Clear the given bits in the specified page.
559238998Salc */
560238998Salcstatic inline void
561238998Salcvm_page_aflag_clear(vm_page_t m, uint8_t bits)
562238998Salc{
563238998Salc	uint32_t *addr, val;
564238998Salc
565238998Salc	/*
566251280Salc	 * The PGA_REFERENCED flag can only be cleared if the page is locked.
567238998Salc	 */
568238998Salc	if ((bits & PGA_REFERENCED) != 0)
569251280Salc		vm_page_assert_locked(m);
570238998Salc
571238998Salc	/*
572238998Salc	 * Access the whole 32-bit word containing the aflags field with an
573238998Salc	 * atomic update.  Parallel non-atomic updates to the other fields
574238998Salc	 * within this word are handled properly by the atomic update.
575238998Salc	 */
576238998Salc	addr = (void *)&m->aflags;
577238998Salc	KASSERT(((uintptr_t)addr & (sizeof(uint32_t) - 1)) == 0,
578238998Salc	    ("vm_page_aflag_clear: aflags is misaligned"));
579238998Salc	val = bits;
580238998Salc#if BYTE_ORDER == BIG_ENDIAN
581238998Salc	val <<= 24;
582238998Salc#endif
583238998Salc	atomic_clear_32(addr, val);
584238998Salc}
585238998Salc
586238998Salc/*
587238998Salc *	Set the given bits in the specified page.
588238998Salc */
589238998Salcstatic inline void
590238998Salcvm_page_aflag_set(vm_page_t m, uint8_t bits)
591238998Salc{
592238998Salc	uint32_t *addr, val;
593238998Salc
594270440Skib	VM_PAGE_ASSERT_PGA_WRITEABLE(m, bits);
595238998Salc
596238998Salc	/*
597238998Salc	 * Access the whole 32-bit word containing the aflags field with an
598238998Salc	 * atomic update.  Parallel non-atomic updates to the other fields
599238998Salc	 * within this word are handled properly by the atomic update.
600238998Salc	 */
601238998Salc	addr = (void *)&m->aflags;
602238998Salc	KASSERT(((uintptr_t)addr & (sizeof(uint32_t) - 1)) == 0,
603238998Salc	    ("vm_page_aflag_set: aflags is misaligned"));
604238998Salc	val = bits;
605238998Salc#if BYTE_ORDER == BIG_ENDIAN
606238998Salc	val <<= 24;
607238998Salc#endif
608238998Salc	atomic_set_32(addr, val);
609238998Salc}
610238998Salc
611238998Salc/*
612237346Salc *	vm_page_dirty:
613237346Salc *
614237346Salc *	Set all bits in the page's dirty field.
615237346Salc *
616237346Salc *	The object containing the specified page must be locked if the
617237346Salc *	call is made from the machine-independent layer.
618237346Salc *
619237346Salc *	See vm_page_clear_dirty_mask().
620237346Salc */
621237346Salcstatic __inline void
622237346Salcvm_page_dirty(vm_page_t m)
623237346Salc{
624237346Salc
625237346Salc	/* Use vm_page_dirty_KBI() under INVARIANTS to save memory. */
626237346Salc#if defined(KLD_MODULE) || defined(INVARIANTS)
627237346Salc	vm_page_dirty_KBI(m);
628237346Salc#else
629237346Salc	m->dirty = VM_PAGE_BITS_ALL;
630237346Salc#endif
631237346Salc}
632237346Salc
633237346Salc/*
634242941Salc *	vm_page_remque:
635242941Salc *
636242941Salc *	If the given page is in a page queue, then remove it from that page
637242941Salc *	queue.
638242941Salc *
639242941Salc *	The page must be locked.
640242941Salc */
641242941Salcstatic inline void
642242941Salcvm_page_remque(vm_page_t m)
643242941Salc{
644242941Salc
645242941Salc	if (m->queue != PQ_NONE)
646242941Salc		vm_page_dequeue(m);
647242941Salc}
648242941Salc
649242941Salc/*
650105549Salc *	vm_page_undirty:
651105549Salc *
652105549Salc *	Set page to not be dirty.  Note: does not clear pmap modify bits
653105549Salc */
654105549Salcstatic __inline void
655105549Salcvm_page_undirty(vm_page_t m)
656105549Salc{
657222992Skib
658222992Skib	VM_PAGE_OBJECT_LOCK_ASSERT(m);
659105549Salc	m->dirty = 0;
660105549Salc}
661105549Salc
66255206Speter#endif				/* _KERNEL */
6635455Sdg#endif				/* !_VM_PAGE_ */
664