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$
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) */
1601541Srgrimes};
1611541Srgrimes
162161125Salc/*
163161125Salc * Page flags stored in oflags:
164161125Salc *
165161125Salc * Access to these page flags is synchronized by the lock on the object
166161125Salc * containing the page (O).
167224746Skib *
168224746Skib * Note: VPO_UNMANAGED (used by OBJT_DEVICE, OBJT_PHYS and OBJT_SG)
169224746Skib * 	 indicates that the page is not under PV management but
170224746Skib * 	 otherwise should be treated as a normal page.  Pages not
171224746Skib * 	 under PV management cannot be paged out via the
172224746Skib * 	 object/vm_page_t because there is no knowledge of their pte
173224746Skib * 	 mappings, and such pages are also not on any PQ queue.
174224746Skib *
175161125Salc */
176254138Sattilio#define	VPO_UNUSED01	0x01		/* --available-- */
177254138Sattilio#define	VPO_SWAPSLEEP	0x02		/* waiting for swap to finish */
178242300Salc#define	VPO_UNMANAGED	0x04		/* no PV management for page */
179242300Salc#define	VPO_SWAPINPROG	0x08		/* swap I/O in progress on page */
180242300Salc#define	VPO_NOSYNC	0x10		/* do not collect for syncer */
181161125Salc
182254138Sattilio/*
183254138Sattilio * Busy page implementation details.
184254138Sattilio * The algorithm is taken mostly by rwlock(9) and sx(9) locks implementation,
185254138Sattilio * even if the support for owner identity is removed because of size
186254138Sattilio * constraints.  Checks on lock recursion are then not possible, while the
187254138Sattilio * lock assertions effectiveness is someway reduced.
188254138Sattilio */
189254138Sattilio#define	VPB_BIT_SHARED		0x01
190254138Sattilio#define	VPB_BIT_EXCLUSIVE	0x02
191254138Sattilio#define	VPB_BIT_WAITERS		0x04
192254138Sattilio#define	VPB_BIT_FLAGMASK						\
193254138Sattilio	(VPB_BIT_SHARED | VPB_BIT_EXCLUSIVE | VPB_BIT_WAITERS)
194254138Sattilio
195254138Sattilio#define	VPB_SHARERS_SHIFT	3
196254138Sattilio#define	VPB_SHARERS(x)							\
197254138Sattilio	(((x) & ~VPB_BIT_FLAGMASK) >> VPB_SHARERS_SHIFT)
198254138Sattilio#define	VPB_SHARERS_WORD(x)	((x) << VPB_SHARERS_SHIFT | VPB_BIT_SHARED)
199254138Sattilio#define	VPB_ONE_SHARER		(1 << VPB_SHARERS_SHIFT)
200254138Sattilio
201254138Sattilio#define	VPB_SINGLE_EXCLUSIVER	VPB_BIT_EXCLUSIVE
202254138Sattilio
203254138Sattilio#define	VPB_UNBUSIED		VPB_SHARERS_WORD(0)
204254138Sattilio
205217508Salc#define	PQ_NONE		255
206217508Salc#define	PQ_INACTIVE	0
207217508Salc#define	PQ_ACTIVE	1
208242300Salc#define	PQ_COUNT	2
20918169Sdyson
210242941SalcTAILQ_HEAD(pglist, vm_page);
211254182SkibSLIST_HEAD(spglist, vm_page);
21218169Sdyson
213242941Salcstruct vm_pagequeue {
214242941Salc	struct mtx	pq_mutex;
215242941Salc	struct pglist	pq_pl;
216254065Skib	int		pq_cnt;
217254065Skib	int		* const pq_vcnt;
218254065Skib	const char	* const pq_name;
219242941Salc} __aligned(CACHE_LINE_SIZE);
22052647Salc
221242941Salc
222254065Skibstruct vm_domain {
223254065Skib	struct vm_pagequeue vmd_pagequeues[PQ_COUNT];
224254065Skib	u_int vmd_page_count;
225254065Skib	u_int vmd_free_count;
226254065Skib	long vmd_segs;	/* bitmask of the segments */
227254065Skib	boolean_t vmd_oom;
228254065Skib	int vmd_pass;	/* local pagedaemon pass */
229254065Skib	struct vm_page vmd_marker; /* marker for pagedaemon private use */
230254065Skib};
231254065Skib
232254065Skibextern struct vm_domain vm_dom[MAXMEMDOM];
233254065Skib
234242941Salc#define	vm_pagequeue_assert_locked(pq)	mtx_assert(&(pq)->pq_mutex, MA_OWNED)
235242941Salc#define	vm_pagequeue_lock(pq)		mtx_lock(&(pq)->pq_mutex)
236242941Salc#define	vm_pagequeue_unlock(pq)		mtx_unlock(&(pq)->pq_mutex)
237242941Salc
238254065Skib#ifdef _KERNEL
239254065Skibstatic __inline void
240254065Skibvm_pagequeue_cnt_add(struct vm_pagequeue *pq, int addend)
241254065Skib{
242254065Skib
243254065Skib#ifdef notyet
244254065Skib	vm_pagequeue_assert_locked(pq);
245254065Skib#endif
246254065Skib	pq->pq_cnt += addend;
247254065Skib	atomic_add_int(pq->pq_vcnt, addend);
248254065Skib}
249254065Skib#define	vm_pagequeue_cnt_inc(pq)	vm_pagequeue_cnt_add((pq), 1)
250254065Skib#define	vm_pagequeue_cnt_dec(pq)	vm_pagequeue_cnt_add((pq), -1)
251254065Skib#endif	/* _KERNEL */
252254065Skib
253242402Sattilioextern struct mtx_padalign vm_page_queue_free_mtx;
254242402Sattilioextern struct mtx_padalign pa_lock[];
255197750Salc
256207410Skmacy#if defined(__arm__)
257207410Skmacy#define	PDRSHIFT	PDR_SHIFT
258207410Skmacy#elif !defined(PDRSHIFT)
259207410Skmacy#define PDRSHIFT	21
260207410Skmacy#endif
261207410Skmacy
262207410Skmacy#define	pa_index(pa)	((pa) >> PDRSHIFT)
263242402Sattilio#define	PA_LOCKPTR(pa)	((struct mtx *)(&pa_lock[pa_index(pa) % PA_LOCK_COUNT]))
264207410Skmacy#define	PA_LOCKOBJPTR(pa)	((struct lock_object *)PA_LOCKPTR((pa)))
265207410Skmacy#define	PA_LOCK(pa)	mtx_lock(PA_LOCKPTR(pa))
266207410Skmacy#define	PA_TRYLOCK(pa)	mtx_trylock(PA_LOCKPTR(pa))
267207410Skmacy#define	PA_UNLOCK(pa)	mtx_unlock(PA_LOCKPTR(pa))
268207410Skmacy#define	PA_UNLOCK_COND(pa) 			\
269207410Skmacy	do {		   			\
270208504Salc		if ((pa) != 0) {		\
271208504Salc			PA_UNLOCK((pa));	\
272208504Salc			(pa) = 0;		\
273208504Salc		}				\
274207410Skmacy	} while (0)
275207410Skmacy
276207410Skmacy#define	PA_LOCK_ASSERT(pa, a)	mtx_assert(PA_LOCKPTR(pa), (a))
277207410Skmacy
278228133Skib#ifdef KLD_MODULE
279228133Skib#define	vm_page_lock(m)		vm_page_lock_KBI((m), LOCK_FILE, LOCK_LINE)
280228133Skib#define	vm_page_unlock(m)	vm_page_unlock_KBI((m), LOCK_FILE, LOCK_LINE)
281228133Skib#define	vm_page_trylock(m)	vm_page_trylock_KBI((m), LOCK_FILE, LOCK_LINE)
282251183Salc#else	/* !KLD_MODULE */
283251183Salc#define	vm_page_lockptr(m)	(PA_LOCKPTR(VM_PAGE_TO_PHYS((m))))
284251183Salc#define	vm_page_lock(m)		mtx_lock(vm_page_lockptr((m)))
285251183Salc#define	vm_page_unlock(m)	mtx_unlock(vm_page_lockptr((m)))
286251183Salc#define	vm_page_trylock(m)	mtx_trylock(vm_page_lockptr((m)))
287251183Salc#endif
288228133Skib#if defined(INVARIANTS)
289251280Salc#define	vm_page_assert_locked(m)		\
290251280Salc    vm_page_assert_locked_KBI((m), __FILE__, __LINE__)
291228133Skib#define	vm_page_lock_assert(m, a)		\
292228133Skib    vm_page_lock_assert_KBI((m), (a), __FILE__, __LINE__)
293228133Skib#else
294251280Salc#define	vm_page_assert_locked(m)
295228133Skib#define	vm_page_lock_assert(m, a)
296228133Skib#endif
297207410Skmacy
2981541Srgrimes/*
299238998Salc * The vm_page's aflags are updated using atomic operations.  To set or clear
300238998Salc * these flags, the functions vm_page_aflag_set() and vm_page_aflag_clear()
301238998Salc * must be used.  Neither these flags nor these functions are part of the KBI.
3021541Srgrimes *
303251280Salc * PGA_REFERENCED may be cleared only if the page is locked.  It is set by
304251280Salc * both the MI and MD VM layers.  However, kernel loadable modules should not
305251280Salc * directly set this flag.  They should call vm_page_reference() instead.
306208990Salc *
307225418Skib * PGA_WRITEABLE is set exclusively on managed pages by pmap_enter().  When it
308254138Sattilio * does so, the page must be exclusive busied.  The MI VM layer must never
309254138Sattilio * access this flag directly.  Instead, it should call
310254138Sattilio * pmap_page_is_write_mapped().
311233949Snwhitehorn *
312233949Snwhitehorn * PGA_EXECUTABLE may be set by pmap routines, and indicates that a page has
313237168Salc * at least one executable mapping.  It is not consumed by the MI VM layer.
3141541Srgrimes */
315225418Skib#define	PGA_WRITEABLE	0x01		/* page may be mapped writeable */
316225418Skib#define	PGA_REFERENCED	0x02		/* page has been referenced */
317233949Snwhitehorn#define	PGA_EXECUTABLE	0x04		/* page may be mapped executable */
3181541Srgrimes
3199507Sdg/*
320225418Skib * Page flags.  If changed at any other time than page allocation or
321225418Skib * freeing, the modification must be protected by the vm_page lock.
322225418Skib */
323242300Salc#define	PG_CACHED	0x0001		/* page is cached */
324242300Salc#define	PG_FREE		0x0002		/* page is free */
325242300Salc#define	PG_FICTITIOUS	0x0004		/* physical page doesn't exist */
326242300Salc#define	PG_ZERO		0x0008		/* page is zeroed */
327242300Salc#define	PG_MARKER	0x0010		/* special queue marker page */
328242300Salc#define	PG_WINATCFLS	0x0040		/* flush dirty page on inactive q */
329242300Salc#define	PG_NODUMP	0x0080		/* don't include this page in a dump */
330242300Salc#define	PG_UNHOLDFREE	0x0100		/* delayed free of a held page */
331225418Skib
332225418Skib/*
3339507Sdg * Misc constants.
3349507Sdg */
3359507Sdg#define ACT_DECLINE		1
3369507Sdg#define ACT_ADVANCE		3
33716750Sdyson#define ACT_INIT		5
33818169Sdyson#define ACT_MAX			64
3399507Sdg
34055206Speter#ifdef _KERNEL
341169291Salc
342238998Salc#include <sys/systm.h>
343238998Salc
344238998Salc#include <machine/atomic.h>
345238998Salc
3461541Srgrimes/*
347243176Salc * Each pageable resident page falls into one of four lists:
3481541Srgrimes *
3495455Sdg *	free
3501541Srgrimes *		Available for allocation now.
3519507Sdg *
3529507Sdg *	cache
353172317Salc *		Almost available for allocation. Still associated with
354172317Salc *		an object, but clean and immediately freeable.
3559507Sdg *
356171420Salc * The following lists are LRU sorted:
357171420Salc *
3581541Srgrimes *	inactive
35913765Smpp *		Low activity, candidates for reclamation.
3601541Srgrimes *		This is the list of pages that should be
3611541Srgrimes *		paged out next.
3629507Sdg *
3631541Srgrimes *	active
3649507Sdg *		Pages that are "active" i.e. they have been
3659507Sdg *		recently referenced.
36610544Sdyson *
3671541Srgrimes */
3681541Srgrimes
36912767Sdysonextern int vm_page_zero_count;
37012767Sdyson
3715455Sdgextern vm_page_t vm_page_array;		/* First resident page in table */
372235359Skibextern long vm_page_array_size;		/* number of vm_page_t's */
3735455Sdgextern long first_page;			/* first physical page number */
3741541Srgrimes
375170816Salc#define	VM_PAGE_IS_FREE(m)	(((m)->flags & PG_FREE) != 0)
376170816Salc
3771541Srgrimes#define VM_PAGE_TO_PHYS(entry)	((entry)->phys_addr)
3781541Srgrimes
379235372Skibvm_page_t PHYS_TO_VM_PAGE(vm_paddr_t pa);
3801541Srgrimes
381100276Salc/* page allocation classes: */
38233109Sdyson#define VM_ALLOC_NORMAL		0
38333109Sdyson#define VM_ALLOC_INTERRUPT	1
38433109Sdyson#define VM_ALLOC_SYSTEM		2
385100276Salc#define	VM_ALLOC_CLASS_MASK	3
386100276Salc/* page allocation flags: */
387106276Sjeff#define	VM_ALLOC_WIRED		0x0020	/* non pageable */
388106276Sjeff#define	VM_ALLOC_ZERO		0x0040	/* Try to obtain a zeroed page */
389106276Sjeff#define	VM_ALLOC_NOOBJ		0x0100	/* No associated object */
390136850Salc#define	VM_ALLOC_NOBUSY		0x0200	/* Do not busy the page */
391172317Salc#define	VM_ALLOC_IFCACHED	0x0400	/* Fail if the page is not cached */
392172317Salc#define	VM_ALLOC_IFNOTCACHED	0x0800	/* Fail if the page is cached */
393209713Skib#define	VM_ALLOC_IGN_SBUSY	0x1000	/* vm_page_grab() only */
394230623Skmacy#define	VM_ALLOC_NODUMP		0x2000	/* don't include in dump */
395254138Sattilio#define	VM_ALLOC_SBUSY		0x4000	/* Shared busy the page */
3965455Sdg
397209713Skib#define	VM_ALLOC_COUNT_SHIFT	16
398209713Skib#define	VM_ALLOC_COUNT(count)	((count) << VM_ALLOC_COUNT_SHIFT)
399209713Skib
400243040Skib#ifdef M_NOWAIT
401243040Skibstatic inline int
402243040Skibmalloc2vm_flags(int malloc_flags)
403243040Skib{
404243040Skib	int pflags;
405243040Skib
406243131Skib	KASSERT((malloc_flags & M_USE_RESERVE) == 0 ||
407243131Skib	    (malloc_flags & M_NOWAIT) != 0,
408243131Skib	    ("M_USE_RESERVE requires M_NOWAIT"));
409243040Skib	pflags = (malloc_flags & M_USE_RESERVE) != 0 ? VM_ALLOC_INTERRUPT :
410243040Skib	    VM_ALLOC_SYSTEM;
411243040Skib	if ((malloc_flags & M_ZERO) != 0)
412243040Skib		pflags |= VM_ALLOC_ZERO;
413243040Skib	if ((malloc_flags & M_NODUMP) != 0)
414243040Skib		pflags |= VM_ALLOC_NODUMP;
415243040Skib	return (pflags);
416243040Skib}
417243040Skib#endif
418243040Skib
419254138Sattiliovoid vm_page_busy_downgrade(vm_page_t m);
420254138Sattiliovoid vm_page_busy_sleep(vm_page_t m, const char *msg);
42179248Sdillonvoid vm_page_flash(vm_page_t m);
42279248Sdillonvoid vm_page_hold(vm_page_t mem);
42379248Sdillonvoid vm_page_unhold(vm_page_t mem);
42479248Sdillonvoid vm_page_free(vm_page_t m);
42579248Sdillonvoid vm_page_free_zero(vm_page_t m);
42679248Sdillon
42779248Sdillonvoid vm_page_activate (vm_page_t);
428251591Salcvoid vm_page_advise(vm_page_t m, int advice);
42979248Sdillonvm_page_t vm_page_alloc (vm_object_t, vm_pindex_t, int);
430227568Salcvm_page_t vm_page_alloc_contig(vm_object_t object, vm_pindex_t pindex, int req,
431227568Salc    u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment,
432227568Salc    vm_paddr_t boundary, vm_memattr_t memattr);
433215973Sjchandravm_page_t vm_page_alloc_freelist(int, int);
43479248Sdillonvm_page_t vm_page_grab (vm_object_t, vm_pindex_t, int);
435193126Salcvoid vm_page_cache(vm_page_t);
436172341Salcvoid vm_page_cache_free(vm_object_t, vm_pindex_t, vm_pindex_t);
437172317Salcvoid vm_page_cache_transfer(vm_object_t, vm_pindex_t, vm_object_t);
43879248Sdillonint vm_page_try_to_cache (vm_page_t);
43979248Sdillonint vm_page_try_to_free (vm_page_t);
44079248Sdillonvoid vm_page_deactivate (vm_page_t);
441242941Salcvoid vm_page_dequeue(vm_page_t m);
442242941Salcvoid vm_page_dequeue_locked(vm_page_t m);
443209685Skibvm_page_t vm_page_find_least(vm_object_t, vm_pindex_t);
444219476Salcvm_page_t vm_page_getfake(vm_paddr_t paddr, vm_memattr_t memattr);
445235366Skibvoid vm_page_initfake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr);
446254141Sattilioint vm_page_insert (vm_page_t, vm_object_t, vm_pindex_t);
447234039Salcboolean_t vm_page_is_cached(vm_object_t object, vm_pindex_t pindex);
44879248Sdillonvm_page_t vm_page_lookup (vm_object_t, vm_pindex_t);
449209407Salcvm_page_t vm_page_next(vm_page_t m);
450207410Skmacyint vm_page_pa_tryrelock(pmap_t, vm_paddr_t, vm_paddr_t *);
451254065Skibstruct vm_pagequeue *vm_page_pagequeue(vm_page_t m);
452209407Salcvm_page_t vm_page_prev(vm_page_t m);
453219476Salcvoid vm_page_putfake(vm_page_t m);
454239246Skibvoid vm_page_readahead_finish(vm_page_t m);
455225418Skibvoid vm_page_reference(vm_page_t m);
45679248Sdillonvoid vm_page_remove (vm_page_t);
457254141Sattilioint vm_page_rename (vm_page_t, vm_object_t, vm_pindex_t);
458254141Sattiliovm_page_t vm_page_replace(vm_page_t mnew, vm_object_t object,
459254141Sattilio    vm_pindex_t pindex);
460242941Salcvoid vm_page_requeue(vm_page_t m);
461242941Salcvoid vm_page_requeue_locked(vm_page_t m);
462254138Sattilioint vm_page_sbusied(vm_page_t m);
463228156Skibvoid vm_page_set_valid_range(vm_page_t m, int base, int size);
464254138Sattilioint vm_page_sleep_if_busy(vm_page_t m, const char *msg);
465127868Salcvm_offset_t vm_page_startup(vm_offset_t vaddr);
466254138Sattiliovoid vm_page_sunbusy(vm_page_t m);
467254138Sattilioint vm_page_trysbusy(vm_page_t m);
468216511Salcvoid vm_page_unhold_pages(vm_page_t *ma, int count);
46979248Sdillonvoid vm_page_unwire (vm_page_t, int);
470219476Salcvoid vm_page_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr);
47179248Sdillonvoid vm_page_wire (vm_page_t);
472254138Sattiliovoid vm_page_xunbusy_hard(vm_page_t m);
47379248Sdillonvoid vm_page_set_validclean (vm_page_t, int, int);
47479248Sdillonvoid vm_page_clear_dirty (vm_page_t, int, int);
47579248Sdillonvoid vm_page_set_invalid (vm_page_t, int, int);
47679248Sdillonint vm_page_is_valid (vm_page_t, int, int);
47779248Sdillonvoid vm_page_test_dirty (vm_page_t);
478227102Skibvm_page_bits_t vm_page_bits(int base, int size);
47945347Sjulianvoid vm_page_zero_invalid(vm_page_t m, boolean_t setvalid);
48043752Sdillonvoid vm_page_free_toq(vm_page_t m);
48182314Spetervoid vm_page_zero_idle_wakeup(void);
48298849Sken
483237346Salcvoid vm_page_dirty_KBI(vm_page_t m);
484228133Skibvoid vm_page_lock_KBI(vm_page_t m, const char *file, int line);
485228133Skibvoid vm_page_unlock_KBI(vm_page_t m, const char *file, int line);
486228133Skibint vm_page_trylock_KBI(vm_page_t m, const char *file, int line);
487228133Skib#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
488251280Salcvoid vm_page_assert_locked_KBI(vm_page_t m, const char *file, int line);
489228133Skibvoid vm_page_lock_assert_KBI(vm_page_t m, int a, const char *file, int line);
490228133Skib#endif
491228133Skib
492254138Sattilio#define	vm_page_assert_sbusied(m)					\
493254138Sattilio	KASSERT(vm_page_sbusied(m),					\
494254138Sattilio	    ("vm_page_assert_sbusied: page %p not shared busy @ %s:%d", \
495254138Sattilio	    (void *)m, __FILE__, __LINE__));
496254138Sattilio
497254138Sattilio#define	vm_page_assert_unbusied(m)					\
498254138Sattilio	KASSERT(!vm_page_busied(m),					\
499254138Sattilio	    ("vm_page_assert_unbusied: page %p busy @ %s:%d",		\
500254138Sattilio	    (void *)m, __FILE__, __LINE__));
501254138Sattilio
502254138Sattilio#define	vm_page_assert_xbusied(m)					\
503254138Sattilio	KASSERT(vm_page_xbusied(m),					\
504254138Sattilio	    ("vm_page_assert_xbusied: page %p not exclusive busy @ %s:%d", \
505254138Sattilio	    (void *)m, __FILE__, __LINE__));
506254138Sattilio
507254138Sattilio#define	vm_page_busied(m)						\
508254138Sattilio	((m)->busy_lock != VPB_UNBUSIED)
509254138Sattilio
510254138Sattilio#define	vm_page_sbusy(m) do {						\
511254138Sattilio	if (!vm_page_trysbusy(m))					\
512254138Sattilio		panic("%s: page %p failed shared busing", __func__, m);	\
513254138Sattilio} while (0)
514254138Sattilio
515254138Sattilio#define	vm_page_tryxbusy(m)						\
516254138Sattilio	(atomic_cmpset_acq_int(&m->busy_lock, VPB_UNBUSIED,		\
517254138Sattilio	    VPB_SINGLE_EXCLUSIVER))
518254138Sattilio
519254138Sattilio#define	vm_page_xbusied(m)						\
520254138Sattilio	((m->busy_lock & VPB_SINGLE_EXCLUSIVER) != 0)
521254138Sattilio
522254138Sattilio#define	vm_page_xbusy(m) do {						\
523254138Sattilio	if (!vm_page_tryxbusy(m))					\
524254138Sattilio		panic("%s: page %p failed exclusive busing", __func__,	\
525254138Sattilio		    m);							\
526254138Sattilio} while (0)
527254138Sattilio
528254138Sattilio#define	vm_page_xunbusy(m) do {						\
529254138Sattilio	if (!atomic_cmpset_rel_int(&(m)->busy_lock,			\
530254138Sattilio	    VPB_SINGLE_EXCLUSIVER, VPB_UNBUSIED))			\
531254138Sattilio		vm_page_xunbusy_hard(m);				\
532254138Sattilio} while (0)
533254138Sattilio
534222992Skib#ifdef INVARIANTS
535222992Skibvoid vm_page_object_lock_assert(vm_page_t m);
536222992Skib#define	VM_PAGE_OBJECT_LOCK_ASSERT(m)	vm_page_object_lock_assert(m)
537222992Skib#else
538222992Skib#define	VM_PAGE_OBJECT_LOCK_ASSERT(m)	(void)0
539222992Skib#endif
540222992Skib
541105549Salc/*
542238998Salc * We want to use atomic updates for the aflags field, which is 8 bits wide.
543238998Salc * However, not all architectures support atomic operations on 8-bit
544238998Salc * destinations.  In order that we can easily use a 32-bit operation, we
545238998Salc * require that the aflags field be 32-bit aligned.
546238998Salc */
547238998SalcCTASSERT(offsetof(struct vm_page, aflags) % sizeof(uint32_t) == 0);
548238998Salc
549238998Salc/*
550238998Salc *	Clear the given bits in the specified page.
551238998Salc */
552238998Salcstatic inline void
553238998Salcvm_page_aflag_clear(vm_page_t m, uint8_t bits)
554238998Salc{
555238998Salc	uint32_t *addr, val;
556238998Salc
557238998Salc	/*
558251280Salc	 * The PGA_REFERENCED flag can only be cleared if the page is locked.
559238998Salc	 */
560238998Salc	if ((bits & PGA_REFERENCED) != 0)
561251280Salc		vm_page_assert_locked(m);
562238998Salc
563238998Salc	/*
564238998Salc	 * Access the whole 32-bit word containing the aflags field with an
565238998Salc	 * atomic update.  Parallel non-atomic updates to the other fields
566238998Salc	 * within this word are handled properly by the atomic update.
567238998Salc	 */
568238998Salc	addr = (void *)&m->aflags;
569238998Salc	KASSERT(((uintptr_t)addr & (sizeof(uint32_t) - 1)) == 0,
570238998Salc	    ("vm_page_aflag_clear: aflags is misaligned"));
571238998Salc	val = bits;
572238998Salc#if BYTE_ORDER == BIG_ENDIAN
573238998Salc	val <<= 24;
574238998Salc#endif
575238998Salc	atomic_clear_32(addr, val);
576238998Salc}
577238998Salc
578238998Salc/*
579238998Salc *	Set the given bits in the specified page.
580238998Salc */
581238998Salcstatic inline void
582238998Salcvm_page_aflag_set(vm_page_t m, uint8_t bits)
583238998Salc{
584238998Salc	uint32_t *addr, val;
585238998Salc
586238998Salc	/*
587238998Salc	 * The PGA_WRITEABLE flag can only be set if the page is managed and
588254138Sattilio	 * exclusive busied.  Currently, this flag is only set by pmap_enter().
589238998Salc	 */
590238998Salc	KASSERT((bits & PGA_WRITEABLE) == 0 ||
591254138Sattilio	    ((m->oflags & VPO_UNMANAGED) == 0 && vm_page_xbusied(m)),
592254138Sattilio	    ("vm_page_aflag_set: PGA_WRITEABLE and not exclusive busy"));
593238998Salc
594238998Salc	/*
595238998Salc	 * Access the whole 32-bit word containing the aflags field with an
596238998Salc	 * atomic update.  Parallel non-atomic updates to the other fields
597238998Salc	 * within this word are handled properly by the atomic update.
598238998Salc	 */
599238998Salc	addr = (void *)&m->aflags;
600238998Salc	KASSERT(((uintptr_t)addr & (sizeof(uint32_t) - 1)) == 0,
601238998Salc	    ("vm_page_aflag_set: aflags is misaligned"));
602238998Salc	val = bits;
603238998Salc#if BYTE_ORDER == BIG_ENDIAN
604238998Salc	val <<= 24;
605238998Salc#endif
606238998Salc	atomic_set_32(addr, val);
607238998Salc}
608238998Salc
609238998Salc/*
610237346Salc *	vm_page_dirty:
611237346Salc *
612237346Salc *	Set all bits in the page's dirty field.
613237346Salc *
614237346Salc *	The object containing the specified page must be locked if the
615237346Salc *	call is made from the machine-independent layer.
616237346Salc *
617237346Salc *	See vm_page_clear_dirty_mask().
618237346Salc */
619237346Salcstatic __inline void
620237346Salcvm_page_dirty(vm_page_t m)
621237346Salc{
622237346Salc
623237346Salc	/* Use vm_page_dirty_KBI() under INVARIANTS to save memory. */
624237346Salc#if defined(KLD_MODULE) || defined(INVARIANTS)
625237346Salc	vm_page_dirty_KBI(m);
626237346Salc#else
627237346Salc	m->dirty = VM_PAGE_BITS_ALL;
628237346Salc#endif
629237346Salc}
630237346Salc
631237346Salc/*
632242941Salc *	vm_page_remque:
633242941Salc *
634242941Salc *	If the given page is in a page queue, then remove it from that page
635242941Salc *	queue.
636242941Salc *
637242941Salc *	The page must be locked.
638242941Salc */
639242941Salcstatic inline void
640242941Salcvm_page_remque(vm_page_t m)
641242941Salc{
642242941Salc
643242941Salc	if (m->queue != PQ_NONE)
644242941Salc		vm_page_dequeue(m);
645242941Salc}
646242941Salc
647242941Salc/*
648105549Salc *	vm_page_undirty:
649105549Salc *
650105549Salc *	Set page to not be dirty.  Note: does not clear pmap modify bits
651105549Salc */
652105549Salcstatic __inline void
653105549Salcvm_page_undirty(vm_page_t m)
654105549Salc{
655222992Skib
656222992Skib	VM_PAGE_OBJECT_LOCK_ASSERT(m);
657105549Salc	m->dirty = 0;
658105549Salc}
659105549Salc
66055206Speter#endif				/* _KERNEL */
6615455Sdg#endif				/* !_VM_PAGE_ */
662