160727d8bSWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 6df8bae1dSRodney W. Grimes * The Mach Operating System project at Carnegie-Mellon University. 7df8bae1dSRodney W. Grimes * 8df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 9df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 10df8bae1dSRodney W. Grimes * are met: 11df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 12df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 13df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 15df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 16fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 17df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 18df8bae1dSRodney W. Grimes * without specific prior written permission. 19df8bae1dSRodney W. Grimes * 20df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30df8bae1dSRodney W. Grimes * SUCH DAMAGE. 31df8bae1dSRodney W. Grimes * 323c4dd356SDavid Greenman * from: @(#)vm_page.h 8.2 (Berkeley) 12/13/93 33df8bae1dSRodney W. Grimes * 34df8bae1dSRodney W. Grimes * 35df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 36df8bae1dSRodney W. Grimes * All rights reserved. 37df8bae1dSRodney W. Grimes * 38df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 39df8bae1dSRodney W. Grimes * 40df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 41df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 42df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 43df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 44df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 45df8bae1dSRodney W. Grimes * 46df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 47df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 48df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 49df8bae1dSRodney W. Grimes * 50df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 51df8bae1dSRodney W. Grimes * 52df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 53df8bae1dSRodney W. Grimes * School of Computer Science 54df8bae1dSRodney W. Grimes * Carnegie Mellon University 55df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 56df8bae1dSRodney W. Grimes * 57df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 58df8bae1dSRodney W. Grimes * rights to redistribute these changes. 593c4dd356SDavid Greenman * 60c3aac50fSPeter Wemm * $FreeBSD$ 61df8bae1dSRodney W. Grimes */ 62df8bae1dSRodney W. Grimes 63df8bae1dSRodney W. Grimes /* 64df8bae1dSRodney W. Grimes * Resident memory system definitions. 65df8bae1dSRodney W. Grimes */ 66df8bae1dSRodney W. Grimes 67df8bae1dSRodney W. Grimes #ifndef _VM_PAGE_ 68df8bae1dSRodney W. Grimes #define _VM_PAGE_ 69df8bae1dSRodney W. Grimes 70f919ebdeSDavid Greenman #include <vm/pmap.h> 71069e9bc1SDoug Rabson 72df8bae1dSRodney W. Grimes /* 73df8bae1dSRodney W. Grimes * Management of resident (logical) pages. 74df8bae1dSRodney W. Grimes * 75df8bae1dSRodney W. Grimes * A small structure is kept for each resident 76df8bae1dSRodney W. Grimes * page, indexed by page number. Each structure 77da384208SAlan Cox * is an element of several collections: 78df8bae1dSRodney W. Grimes * 79da384208SAlan Cox * A radix tree used to quickly 80df8bae1dSRodney W. Grimes * perform object/offset lookups 81df8bae1dSRodney W. Grimes * 82df8bae1dSRodney W. Grimes * A list of all pages for a given object, 83df8bae1dSRodney W. Grimes * so they can be quickly deactivated at 84df8bae1dSRodney W. Grimes * time of deallocation. 85df8bae1dSRodney W. Grimes * 86df8bae1dSRodney W. Grimes * An ordered list of pages due for pageout. 87df8bae1dSRodney W. Grimes * 88df8bae1dSRodney W. Grimes * In addition, the structure contains the object 89df8bae1dSRodney W. Grimes * and offset to which this page belongs (for pageout), 90df8bae1dSRodney W. Grimes * and sundry status bits. 91df8bae1dSRodney W. Grimes * 923c76db4cSAlan Cox * In general, operations on this structure's mutable fields are 933c76db4cSAlan Cox * synchronized using either one of or a combination of the lock on the 943c76db4cSAlan Cox * object that the page belongs to (O), the pool lock for the page (P), 958d220203SAlan Cox * or the lock for either the free or paging queue (Q). If a field is 963c76db4cSAlan Cox * annotated below with two of these locks, then holding either lock is 973c76db4cSAlan Cox * sufficient for read access, but both locks are required for write 983c76db4cSAlan Cox * access. 990ce3ba8cSKip Macy * 100abb9b935SKonstantin Belousov * In contrast, the synchronization of accesses to the page's 101abb9b935SKonstantin Belousov * dirty field is machine dependent (M). In the 102abb9b935SKonstantin Belousov * machine-independent layer, the lock on the object that the 103abb9b935SKonstantin Belousov * page belongs to must be held in order to operate on the field. 104abb9b935SKonstantin Belousov * However, the pmap layer is permitted to set all bits within 105abb9b935SKonstantin Belousov * the field without holding that lock. If the underlying 106abb9b935SKonstantin Belousov * architecture does not support atomic read-modify-write 107abb9b935SKonstantin Belousov * operations on the field's type, then the machine-independent 1082042bb37SKonstantin Belousov * layer uses a 32-bit atomic on the aligned 32-bit word that 109abb9b935SKonstantin Belousov * contains the dirty field. In the machine-independent layer, 110abb9b935SKonstantin Belousov * the implementation of read-modify-write operations on the 111abb9b935SKonstantin Belousov * field is encapsulated in vm_page_clear_dirty_mask(). 112df8bae1dSRodney W. Grimes */ 113df8bae1dSRodney W. Grimes 114561cc9fcSKonstantin Belousov #if PAGE_SIZE == 4096 115561cc9fcSKonstantin Belousov #define VM_PAGE_BITS_ALL 0xffu 116561cc9fcSKonstantin Belousov typedef uint8_t vm_page_bits_t; 117561cc9fcSKonstantin Belousov #elif PAGE_SIZE == 8192 118561cc9fcSKonstantin Belousov #define VM_PAGE_BITS_ALL 0xffffu 119561cc9fcSKonstantin Belousov typedef uint16_t vm_page_bits_t; 120561cc9fcSKonstantin Belousov #elif PAGE_SIZE == 16384 121561cc9fcSKonstantin Belousov #define VM_PAGE_BITS_ALL 0xffffffffu 122561cc9fcSKonstantin Belousov typedef uint32_t vm_page_bits_t; 123561cc9fcSKonstantin Belousov #elif PAGE_SIZE == 32768 124561cc9fcSKonstantin Belousov #define VM_PAGE_BITS_ALL 0xfffffffffffffffflu 125561cc9fcSKonstantin Belousov typedef uint64_t vm_page_bits_t; 126561cc9fcSKonstantin Belousov #endif 127561cc9fcSKonstantin Belousov 128df8bae1dSRodney W. Grimes struct vm_page { 129c325e866SKonstantin Belousov union { 130c325e866SKonstantin Belousov TAILQ_ENTRY(vm_page) q; /* page queue or free list (Q) */ 131c325e866SKonstantin Belousov struct { 132c325e866SKonstantin Belousov SLIST_ENTRY(vm_page) ss; /* private slists */ 133c325e866SKonstantin Belousov void *pv; 134c325e866SKonstantin Belousov } s; 135c325e866SKonstantin Belousov struct { 136c325e866SKonstantin Belousov u_long p; 137c325e866SKonstantin Belousov u_long v; 138c325e866SKonstantin Belousov } memguard; 139c325e866SKonstantin Belousov } plinks; 140e3975643SJake Burkholder TAILQ_ENTRY(vm_page) listq; /* pages in same object (O) */ 1415ac59343SAlan Cox vm_object_t object; /* which object am I in (O,P) */ 14243319c11SAlan Cox vm_pindex_t pindex; /* offset into object (O,P) */ 143227f9a1cSJake Burkholder vm_paddr_t phys_addr; /* physical address of page */ 144763df3ecSPedro F. Giffuni struct md_page md; /* machine dependent stuff */ 145fd8c28bfSAlan Cox u_int wire_count; /* wired down maps refs (P) */ 1463846a822SKonstantin Belousov volatile u_int busy_lock; /* busy owners lock */ 1473846a822SKonstantin Belousov uint16_t hold_count; /* page hold count (P) */ 1483846a822SKonstantin Belousov uint16_t flags; /* page PG_* flags (P) */ 1493407fefeSKonstantin Belousov uint8_t aflags; /* access is atomic */ 150081a4881SAlan Cox uint8_t oflags; /* page VPO_* flags (O) */ 1513846a822SKonstantin Belousov uint8_t queue; /* page queue index (P,Q) */ 152dd05fa19SAlan Cox int8_t psind; /* pagesizes[] index (O) */ 1533846a822SKonstantin Belousov int8_t segind; 1543846a822SKonstantin Belousov uint8_t order; /* index of the buddy queue */ 1553846a822SKonstantin Belousov uint8_t pool; 156a15f7df5SAttilio Rao u_char act_count; /* page usage count (P) */ 157c325e866SKonstantin Belousov /* NOTE that these must support one bit per DEV_BSIZE in a page */ 158bd7e5f99SJohn Dyson /* so, on normal X86 kernels, they must be at least 8 bits wide */ 159561cc9fcSKonstantin Belousov vm_page_bits_t valid; /* map of valid DEV_BSIZE chunks (O) */ 160561cc9fcSKonstantin Belousov vm_page_bits_t dirty; /* map of dirty DEV_BSIZE chunks (M) */ 161df8bae1dSRodney W. Grimes }; 162df8bae1dSRodney W. Grimes 1635786be7cSAlan Cox /* 1645786be7cSAlan Cox * Page flags stored in oflags: 1655786be7cSAlan Cox * 1665786be7cSAlan Cox * Access to these page flags is synchronized by the lock on the object 1675786be7cSAlan Cox * containing the page (O). 168d98d0ce2SKonstantin Belousov * 169d98d0ce2SKonstantin Belousov * Note: VPO_UNMANAGED (used by OBJT_DEVICE, OBJT_PHYS and OBJT_SG) 170d98d0ce2SKonstantin Belousov * indicates that the page is not under PV management but 171d98d0ce2SKonstantin Belousov * otherwise should be treated as a normal page. Pages not 172d98d0ce2SKonstantin Belousov * under PV management cannot be paged out via the 173d98d0ce2SKonstantin Belousov * object/vm_page_t because there is no knowledge of their pte 174d98d0ce2SKonstantin Belousov * mappings, and such pages are also not on any PQ queue. 175d98d0ce2SKonstantin Belousov * 1765786be7cSAlan Cox */ 177c7aebda8SAttilio Rao #define VPO_UNUSED01 0x01 /* --available-- */ 178c7aebda8SAttilio Rao #define VPO_SWAPSLEEP 0x02 /* waiting for swap to finish */ 179081a4881SAlan Cox #define VPO_UNMANAGED 0x04 /* no PV management for page */ 180081a4881SAlan Cox #define VPO_SWAPINPROG 0x08 /* swap I/O in progress on page */ 181081a4881SAlan Cox #define VPO_NOSYNC 0x10 /* do not collect for syncer */ 1825786be7cSAlan Cox 183c7aebda8SAttilio Rao /* 184c7aebda8SAttilio Rao * Busy page implementation details. 185c7aebda8SAttilio Rao * The algorithm is taken mostly by rwlock(9) and sx(9) locks implementation, 186c7aebda8SAttilio Rao * even if the support for owner identity is removed because of size 187c7aebda8SAttilio Rao * constraints. Checks on lock recursion are then not possible, while the 188c7aebda8SAttilio Rao * lock assertions effectiveness is someway reduced. 189c7aebda8SAttilio Rao */ 190c7aebda8SAttilio Rao #define VPB_BIT_SHARED 0x01 191c7aebda8SAttilio Rao #define VPB_BIT_EXCLUSIVE 0x02 192c7aebda8SAttilio Rao #define VPB_BIT_WAITERS 0x04 193c7aebda8SAttilio Rao #define VPB_BIT_FLAGMASK \ 194c7aebda8SAttilio Rao (VPB_BIT_SHARED | VPB_BIT_EXCLUSIVE | VPB_BIT_WAITERS) 195c7aebda8SAttilio Rao 196c7aebda8SAttilio Rao #define VPB_SHARERS_SHIFT 3 197c7aebda8SAttilio Rao #define VPB_SHARERS(x) \ 198c7aebda8SAttilio Rao (((x) & ~VPB_BIT_FLAGMASK) >> VPB_SHARERS_SHIFT) 199c7aebda8SAttilio Rao #define VPB_SHARERS_WORD(x) ((x) << VPB_SHARERS_SHIFT | VPB_BIT_SHARED) 200c7aebda8SAttilio Rao #define VPB_ONE_SHARER (1 << VPB_SHARERS_SHIFT) 201c7aebda8SAttilio Rao 202c7aebda8SAttilio Rao #define VPB_SINGLE_EXCLUSIVER VPB_BIT_EXCLUSIVE 203c7aebda8SAttilio Rao 204c7aebda8SAttilio Rao #define VPB_UNBUSIED VPB_SHARERS_WORD(0) 205c7aebda8SAttilio Rao 20644e46b9eSAlan Cox #define PQ_NONE 255 20744e46b9eSAlan Cox #define PQ_INACTIVE 0 20844e46b9eSAlan Cox #define PQ_ACTIVE 1 209ebcddc72SAlan Cox #define PQ_LAUNDRY 2 210b1fd102eSMark Johnston #define PQ_UNSWAPPABLE 3 211b1fd102eSMark Johnston #define PQ_COUNT 4 212ef39c05bSAlexander Leidinger 2138d220203SAlan Cox TAILQ_HEAD(pglist, vm_page); 214c325e866SKonstantin Belousov SLIST_HEAD(spglist, vm_page); 21570c17636SAlan Cox 2168d220203SAlan Cox struct vm_pagequeue { 2178d220203SAlan Cox struct mtx pq_mutex; 2188d220203SAlan Cox struct pglist pq_pl; 219449c2e92SKonstantin Belousov int pq_cnt; 2205a2e650aSConrad Meyer u_int * const pq_vcnt; 2218d220203SAlan Cox const char * const pq_name; 2228d220203SAlan Cox } __aligned(CACHE_LINE_SIZE); 2238d220203SAlan Cox 224449c2e92SKonstantin Belousov 225449c2e92SKonstantin Belousov struct vm_domain { 226449c2e92SKonstantin Belousov struct vm_pagequeue vmd_pagequeues[PQ_COUNT]; 227449c2e92SKonstantin Belousov u_int vmd_page_count; 228449c2e92SKonstantin Belousov u_int vmd_free_count; 229449c2e92SKonstantin Belousov long vmd_segs; /* bitmask of the segments */ 230449c2e92SKonstantin Belousov boolean_t vmd_oom; 23176386c7eSKonstantin Belousov int vmd_oom_seq; 23222cf98d1SAlan Cox int vmd_last_active_scan; 233ebcddc72SAlan Cox struct vm_page vmd_laundry_marker; 234449c2e92SKonstantin Belousov struct vm_page vmd_marker; /* marker for pagedaemon private use */ 2357e78597fSMark Johnston struct vm_page vmd_inacthead; /* marker for LRU-defeating insertions */ 236449c2e92SKonstantin Belousov }; 237449c2e92SKonstantin Belousov 238449c2e92SKonstantin Belousov extern struct vm_domain vm_dom[MAXMEMDOM]; 2398d220203SAlan Cox 2408d220203SAlan Cox #define vm_pagequeue_assert_locked(pq) mtx_assert(&(pq)->pq_mutex, MA_OWNED) 2418d220203SAlan Cox #define vm_pagequeue_lock(pq) mtx_lock(&(pq)->pq_mutex) 242ebcddc72SAlan Cox #define vm_pagequeue_lockptr(pq) (&(pq)->pq_mutex) 2438d220203SAlan Cox #define vm_pagequeue_unlock(pq) mtx_unlock(&(pq)->pq_mutex) 244e67e0775SAlan Cox 245449c2e92SKonstantin Belousov #ifdef _KERNEL 246bfc8c24cSGleb Smirnoff extern vm_page_t bogus_page; 247bfc8c24cSGleb Smirnoff 248449c2e92SKonstantin Belousov static __inline void 249449c2e92SKonstantin Belousov vm_pagequeue_cnt_add(struct vm_pagequeue *pq, int addend) 250449c2e92SKonstantin Belousov { 251449c2e92SKonstantin Belousov 252449c2e92SKonstantin Belousov #ifdef notyet 253449c2e92SKonstantin Belousov vm_pagequeue_assert_locked(pq); 254449c2e92SKonstantin Belousov #endif 255449c2e92SKonstantin Belousov pq->pq_cnt += addend; 256449c2e92SKonstantin Belousov atomic_add_int(pq->pq_vcnt, addend); 257449c2e92SKonstantin Belousov } 258449c2e92SKonstantin Belousov #define vm_pagequeue_cnt_inc(pq) vm_pagequeue_cnt_add((pq), 1) 259449c2e92SKonstantin Belousov #define vm_pagequeue_cnt_dec(pq) vm_pagequeue_cnt_add((pq), -1) 260449c2e92SKonstantin Belousov #endif /* _KERNEL */ 261449c2e92SKonstantin Belousov 2624ceaf45dSAttilio Rao extern struct mtx_padalign vm_page_queue_free_mtx; 2634ceaf45dSAttilio Rao extern struct mtx_padalign pa_lock[]; 2642965a453SKip Macy 2652965a453SKip Macy #if defined(__arm__) 2662965a453SKip Macy #define PDRSHIFT PDR_SHIFT 2672965a453SKip Macy #elif !defined(PDRSHIFT) 2682965a453SKip Macy #define PDRSHIFT 21 2692965a453SKip Macy #endif 2702965a453SKip Macy 2712965a453SKip Macy #define pa_index(pa) ((pa) >> PDRSHIFT) 2724ceaf45dSAttilio Rao #define PA_LOCKPTR(pa) ((struct mtx *)(&pa_lock[pa_index(pa) % PA_LOCK_COUNT])) 2732965a453SKip Macy #define PA_LOCKOBJPTR(pa) ((struct lock_object *)PA_LOCKPTR((pa))) 2742965a453SKip Macy #define PA_LOCK(pa) mtx_lock(PA_LOCKPTR(pa)) 2752965a453SKip Macy #define PA_TRYLOCK(pa) mtx_trylock(PA_LOCKPTR(pa)) 2762965a453SKip Macy #define PA_UNLOCK(pa) mtx_unlock(PA_LOCKPTR(pa)) 2772965a453SKip Macy #define PA_UNLOCK_COND(pa) \ 2782965a453SKip Macy do { \ 279567e51e1SAlan Cox if ((pa) != 0) { \ 280567e51e1SAlan Cox PA_UNLOCK((pa)); \ 281567e51e1SAlan Cox (pa) = 0; \ 282567e51e1SAlan Cox } \ 2832965a453SKip Macy } while (0) 2842965a453SKip Macy 2852965a453SKip Macy #define PA_LOCK_ASSERT(pa, a) mtx_assert(PA_LOCKPTR(pa), (a)) 2862965a453SKip Macy 287cf1911a9SKonstantin Belousov #ifdef KLD_MODULE 288cf1911a9SKonstantin Belousov #define vm_page_lock(m) vm_page_lock_KBI((m), LOCK_FILE, LOCK_LINE) 289cf1911a9SKonstantin Belousov #define vm_page_unlock(m) vm_page_unlock_KBI((m), LOCK_FILE, LOCK_LINE) 290cf1911a9SKonstantin Belousov #define vm_page_trylock(m) vm_page_trylock_KBI((m), LOCK_FILE, LOCK_LINE) 291cf1911a9SKonstantin Belousov #else /* !KLD_MODULE */ 2922965a453SKip Macy #define vm_page_lockptr(m) (PA_LOCKPTR(VM_PAGE_TO_PHYS((m)))) 2932965a453SKip Macy #define vm_page_lock(m) mtx_lock(vm_page_lockptr((m))) 2942965a453SKip Macy #define vm_page_unlock(m) mtx_unlock(vm_page_lockptr((m))) 2952965a453SKip Macy #define vm_page_trylock(m) mtx_trylock(vm_page_lockptr((m))) 296ef5ba5a3SAlan Cox #endif 297ef5ba5a3SAlan Cox #if defined(INVARIANTS) 298b4171812SAlan Cox #define vm_page_assert_locked(m) \ 299b4171812SAlan Cox vm_page_assert_locked_KBI((m), __FILE__, __LINE__) 300ef5ba5a3SAlan Cox #define vm_page_lock_assert(m, a) \ 301ef5ba5a3SAlan Cox vm_page_lock_assert_KBI((m), (a), __FILE__, __LINE__) 302ef5ba5a3SAlan Cox #else 303b4171812SAlan Cox #define vm_page_assert_locked(m) 304ef5ba5a3SAlan Cox #define vm_page_lock_assert(m, a) 305cf1911a9SKonstantin Belousov #endif 306e67e0775SAlan Cox 307df8bae1dSRodney W. Grimes /* 308369763e3SAlan Cox * The vm_page's aflags are updated using atomic operations. To set or clear 309369763e3SAlan Cox * these flags, the functions vm_page_aflag_set() and vm_page_aflag_clear() 310369763e3SAlan Cox * must be used. Neither these flags nor these functions are part of the KBI. 3113407fefeSKonstantin Belousov * 312b4171812SAlan Cox * PGA_REFERENCED may be cleared only if the page is locked. It is set by 313b4171812SAlan Cox * both the MI and MD VM layers. However, kernel loadable modules should not 314b4171812SAlan Cox * directly set this flag. They should call vm_page_reference() instead. 315ce186587SAlan Cox * 316afb69e6bSKonstantin Belousov * PGA_WRITEABLE is set exclusively on managed pages by pmap_enter(). 317afb69e6bSKonstantin Belousov * When it does so, the object must be locked, or the page must be 318afb69e6bSKonstantin Belousov * exclusive busied. The MI VM layer must never access this flag 319afb69e6bSKonstantin Belousov * directly. Instead, it should call pmap_page_is_write_mapped(). 32057bd5cceSNathan Whitehorn * 32157bd5cceSNathan Whitehorn * PGA_EXECUTABLE may be set by pmap routines, and indicates that a page has 3226031c68dSAlan Cox * at least one executable mapping. It is not consumed by the MI VM layer. 323df8bae1dSRodney W. Grimes */ 3243407fefeSKonstantin Belousov #define PGA_WRITEABLE 0x01 /* page may be mapped writeable */ 3253407fefeSKonstantin Belousov #define PGA_REFERENCED 0x02 /* page has been referenced */ 32657bd5cceSNathan Whitehorn #define PGA_EXECUTABLE 0x04 /* page may be mapped executable */ 3273407fefeSKonstantin Belousov 3283407fefeSKonstantin Belousov /* 3293407fefeSKonstantin Belousov * Page flags. If changed at any other time than page allocation or 3303407fefeSKonstantin Belousov * freeing, the modification must be protected by the vm_page lock. 3313407fefeSKonstantin Belousov */ 332081a4881SAlan Cox #define PG_FICTITIOUS 0x0004 /* physical page doesn't exist */ 333081a4881SAlan Cox #define PG_ZERO 0x0008 /* page is zeroed */ 334081a4881SAlan Cox #define PG_MARKER 0x0010 /* special queue marker page */ 335081a4881SAlan Cox #define PG_NODUMP 0x0080 /* don't include this page in a dump */ 336081a4881SAlan Cox #define PG_UNHOLDFREE 0x0100 /* delayed free of a held page */ 337df8bae1dSRodney W. Grimes 33824a1cce3SDavid Greenman /* 33924a1cce3SDavid Greenman * Misc constants. 34024a1cce3SDavid Greenman */ 34124a1cce3SDavid Greenman #define ACT_DECLINE 1 34224a1cce3SDavid Greenman #define ACT_ADVANCE 3 34338efa82bSJohn Dyson #define ACT_INIT 5 3445070c7f8SJohn Dyson #define ACT_MAX 64 345df8bae1dSRodney W. Grimes 346c4473420SPeter Wemm #ifdef _KERNEL 34704a18977SAlan Cox 348369763e3SAlan Cox #include <sys/systm.h> 349369763e3SAlan Cox 350369763e3SAlan Cox #include <machine/atomic.h> 351369763e3SAlan Cox 352df8bae1dSRodney W. Grimes /* 353b1fd102eSMark Johnston * Each pageable resident page falls into one of five lists: 354df8bae1dSRodney W. Grimes * 355df8bae1dSRodney W. Grimes * free 356df8bae1dSRodney W. Grimes * Available for allocation now. 35724a1cce3SDavid Greenman * 358df8bae1dSRodney W. Grimes * inactive 3596c5e9bbdSMike Pritchard * Low activity, candidates for reclamation. 3603453bca8SAlan Cox * This list is approximately LRU ordered. 3613453bca8SAlan Cox * 3623453bca8SAlan Cox * laundry 363df8bae1dSRodney W. Grimes * This is the list of pages that should be 364df8bae1dSRodney W. Grimes * paged out next. 36524a1cce3SDavid Greenman * 366b1fd102eSMark Johnston * unswappable 367b1fd102eSMark Johnston * Dirty anonymous pages that cannot be paged 368b1fd102eSMark Johnston * out because no swap device is configured. 369b1fd102eSMark Johnston * 370df8bae1dSRodney W. Grimes * active 3713453bca8SAlan Cox * Pages that are "active", i.e., they have been 37224a1cce3SDavid Greenman * recently referenced. 37310ad4d48SJohn Dyson * 374df8bae1dSRodney W. Grimes */ 375df8bae1dSRodney W. Grimes 376a316d390SJohn Dyson extern int vm_page_zero_count; 377a316d390SJohn Dyson 3780d94caffSDavid Greenman extern vm_page_t vm_page_array; /* First resident page in table */ 37913a0b7bcSKonstantin Belousov extern long vm_page_array_size; /* number of vm_page_t's */ 3800d94caffSDavid Greenman extern long first_page; /* first physical page number */ 3810d94caffSDavid Greenman 382df8bae1dSRodney W. Grimes #define VM_PAGE_TO_PHYS(entry) ((entry)->phys_addr) 383df8bae1dSRodney W. Grimes 384a5073058SJason A. Harmening /* 3857c989c15SJason A. Harmening * PHYS_TO_VM_PAGE() returns the vm_page_t object that represents a memory 386a5073058SJason A. Harmening * page to which the given physical address belongs. The correct vm_page_t 387a5073058SJason A. Harmening * object is returned for addresses that are not page-aligned. 388a5073058SJason A. Harmening */ 389b6de32bdSKonstantin Belousov vm_page_t PHYS_TO_VM_PAGE(vm_paddr_t pa); 390df8bae1dSRodney W. Grimes 39189fc8bdbSGleb Smirnoff /* 39289fc8bdbSGleb Smirnoff * Page allocation parameters for vm_page for the functions 39389fc8bdbSGleb Smirnoff * vm_page_alloc(), vm_page_grab(), vm_page_alloc_contig() and 39489fc8bdbSGleb Smirnoff * vm_page_alloc_freelist(). Some functions support only a subset 39589fc8bdbSGleb Smirnoff * of the flags, and ignore others, see the flags legend. 39689fc8bdbSGleb Smirnoff * 397*5471caf6SAlan Cox * The meaning of VM_ALLOC_ZERO differs slightly between the vm_page_alloc*() 398*5471caf6SAlan Cox * and the vm_page_grab*() functions. See these functions for details. 399*5471caf6SAlan Cox * 40089fc8bdbSGleb Smirnoff * Bits 0 - 1 define class. 40189fc8bdbSGleb Smirnoff * Bits 2 - 15 dedicated for flags. 40289fc8bdbSGleb Smirnoff * Legend: 40389fc8bdbSGleb Smirnoff * (a) - vm_page_alloc() supports the flag. 40489fc8bdbSGleb Smirnoff * (c) - vm_page_alloc_contig() supports the flag. 40589fc8bdbSGleb Smirnoff * (f) - vm_page_alloc_freelist() supports the flag. 40689fc8bdbSGleb Smirnoff * (g) - vm_page_grab() supports the flag. 407*5471caf6SAlan Cox * (p) - vm_page_grab_pages() supports the flag. 40889fc8bdbSGleb Smirnoff * Bits above 15 define the count of additional pages that the caller 40989fc8bdbSGleb Smirnoff * intends to allocate. 41089fc8bdbSGleb Smirnoff */ 4116d40c3d3SDavid Greenman #define VM_ALLOC_NORMAL 0 4126d40c3d3SDavid Greenman #define VM_ALLOC_INTERRUPT 1 4136d40c3d3SDavid Greenman #define VM_ALLOC_SYSTEM 2 414827b2fa0SAlan Cox #define VM_ALLOC_CLASS_MASK 3 415*5471caf6SAlan Cox #define VM_ALLOC_WIRED 0x0020 /* (acfgp) Allocate a wired page */ 416*5471caf6SAlan Cox #define VM_ALLOC_ZERO 0x0040 /* (acfgp) Allocate a prezeroed page */ 41789fc8bdbSGleb Smirnoff #define VM_ALLOC_NOOBJ 0x0100 /* (acg) No associated object */ 418*5471caf6SAlan Cox #define VM_ALLOC_NOBUSY 0x0200 /* (acgp) Do not excl busy the page */ 419*5471caf6SAlan Cox #define VM_ALLOC_IGN_SBUSY 0x1000 /* (gp) Ignore shared busy flag */ 42089fc8bdbSGleb Smirnoff #define VM_ALLOC_NODUMP 0x2000 /* (ag) don't include in dump */ 421*5471caf6SAlan Cox #define VM_ALLOC_SBUSY 0x4000 /* (acgp) Shared busy the page */ 422e3ed82bcSGleb Smirnoff #define VM_ALLOC_NOWAIT 0x8000 /* (g) Do not sleep, return NULL */ 4235f195aa3SKonstantin Belousov #define VM_ALLOC_COUNT_SHIFT 16 4245f195aa3SKonstantin Belousov #define VM_ALLOC_COUNT(count) ((count) << VM_ALLOC_COUNT_SHIFT) 4250d94caffSDavid Greenman 426b32ecf44SKonstantin Belousov #ifdef M_NOWAIT 427b32ecf44SKonstantin Belousov static inline int 428b32ecf44SKonstantin Belousov malloc2vm_flags(int malloc_flags) 429b32ecf44SKonstantin Belousov { 430b32ecf44SKonstantin Belousov int pflags; 431b32ecf44SKonstantin Belousov 432962b064aSKonstantin Belousov KASSERT((malloc_flags & M_USE_RESERVE) == 0 || 433962b064aSKonstantin Belousov (malloc_flags & M_NOWAIT) != 0, 434962b064aSKonstantin Belousov ("M_USE_RESERVE requires M_NOWAIT")); 435b32ecf44SKonstantin Belousov pflags = (malloc_flags & M_USE_RESERVE) != 0 ? VM_ALLOC_INTERRUPT : 436b32ecf44SKonstantin Belousov VM_ALLOC_SYSTEM; 437b32ecf44SKonstantin Belousov if ((malloc_flags & M_ZERO) != 0) 438b32ecf44SKonstantin Belousov pflags |= VM_ALLOC_ZERO; 439b32ecf44SKonstantin Belousov if ((malloc_flags & M_NODUMP) != 0) 440b32ecf44SKonstantin Belousov pflags |= VM_ALLOC_NODUMP; 441b32ecf44SKonstantin Belousov return (pflags); 442b32ecf44SKonstantin Belousov } 443b32ecf44SKonstantin Belousov #endif 444b32ecf44SKonstantin Belousov 44588302601SAlan Cox /* 44688302601SAlan Cox * Predicates supported by vm_page_ps_test(): 44788302601SAlan Cox * 44888302601SAlan Cox * PS_ALL_DIRTY is true only if the entire (super)page is dirty. 44988302601SAlan Cox * However, it can be spuriously false when the (super)page has become 45088302601SAlan Cox * dirty in the pmap but that information has not been propagated to the 45188302601SAlan Cox * machine-independent layer. 45288302601SAlan Cox */ 45388302601SAlan Cox #define PS_ALL_DIRTY 0x1 45488302601SAlan Cox #define PS_ALL_VALID 0x2 45588302601SAlan Cox #define PS_NONE_BUSY 0x4 45688302601SAlan Cox 457c7aebda8SAttilio Rao void vm_page_busy_downgrade(vm_page_t m); 4585975e53dSKonstantin Belousov void vm_page_busy_sleep(vm_page_t m, const char *msg, bool nonshared); 4591b40f8c0SMatthew Dillon void vm_page_flash(vm_page_t m); 4601b40f8c0SMatthew Dillon void vm_page_hold(vm_page_t mem); 4611b40f8c0SMatthew Dillon void vm_page_unhold(vm_page_t mem); 4621b40f8c0SMatthew Dillon void vm_page_free(vm_page_t m); 4631b40f8c0SMatthew Dillon void vm_page_free_zero(vm_page_t m); 4641b40f8c0SMatthew Dillon 4651b40f8c0SMatthew Dillon void vm_page_activate (vm_page_t); 4662051980fSAlan Cox void vm_page_advise(vm_page_t m, int advice); 4671b40f8c0SMatthew Dillon vm_page_t vm_page_alloc (vm_object_t, vm_pindex_t, int); 468fbd80bd0SAlan Cox vm_page_t vm_page_alloc_contig(vm_object_t object, vm_pindex_t pindex, int req, 469fbd80bd0SAlan Cox u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment, 470fbd80bd0SAlan Cox vm_paddr_t boundary, vm_memattr_t memattr); 471aa546366SJayachandran C. vm_page_t vm_page_alloc_freelist(int, int); 4721b40f8c0SMatthew Dillon vm_page_t vm_page_grab (vm_object_t, vm_pindex_t, int); 473*5471caf6SAlan Cox void vm_page_grab_pages(vm_object_t object, vm_pindex_t pindex, int allocflags, 474*5471caf6SAlan Cox vm_page_t *ma, int count); 4751b40f8c0SMatthew Dillon int vm_page_try_to_free (vm_page_t); 4761b40f8c0SMatthew Dillon void vm_page_deactivate (vm_page_t); 4773138cd36SMark Johnston void vm_page_deactivate_noreuse(vm_page_t); 4788d220203SAlan Cox void vm_page_dequeue(vm_page_t m); 4798d220203SAlan Cox void vm_page_dequeue_locked(vm_page_t m); 480b382c10aSKonstantin Belousov vm_page_t vm_page_find_least(vm_object_t, vm_pindex_t); 48110cf2560SAlan Cox vm_page_t vm_page_getfake(vm_paddr_t paddr, vm_memattr_t memattr); 482e461aae7SKonstantin Belousov void vm_page_initfake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr); 483e946b949SAttilio Rao int vm_page_insert (vm_page_t, vm_object_t, vm_pindex_t); 484ebcddc72SAlan Cox void vm_page_launder(vm_page_t m); 4851b40f8c0SMatthew Dillon vm_page_t vm_page_lookup (vm_object_t, vm_pindex_t); 48691b4f427SAlan Cox vm_page_t vm_page_next(vm_page_t m); 4872965a453SKip Macy int vm_page_pa_tryrelock(pmap_t, vm_paddr_t, vm_paddr_t *); 488449c2e92SKonstantin Belousov struct vm_pagequeue *vm_page_pagequeue(vm_page_t m); 48991b4f427SAlan Cox vm_page_t vm_page_prev(vm_page_t m); 49088302601SAlan Cox bool vm_page_ps_test(vm_page_t m, int flags, vm_page_t skip_m); 49110cf2560SAlan Cox void vm_page_putfake(vm_page_t m); 492b6c00483SKonstantin Belousov void vm_page_readahead_finish(vm_page_t m); 493c869e672SAlan Cox bool vm_page_reclaim_contig(int req, u_long npages, vm_paddr_t low, 494c869e672SAlan Cox vm_paddr_t high, u_long alignment, vm_paddr_t boundary); 4953407fefeSKonstantin Belousov void vm_page_reference(vm_page_t m); 4961b40f8c0SMatthew Dillon void vm_page_remove (vm_page_t); 497e946b949SAttilio Rao int vm_page_rename (vm_page_t, vm_object_t, vm_pindex_t); 498e946b949SAttilio Rao vm_page_t vm_page_replace(vm_page_t mnew, vm_object_t object, 499e946b949SAttilio Rao vm_pindex_t pindex); 5008d220203SAlan Cox void vm_page_requeue(vm_page_t m); 5018d220203SAlan Cox void vm_page_requeue_locked(vm_page_t m); 502c7aebda8SAttilio Rao int vm_page_sbusied(vm_page_t m); 503c869e672SAlan Cox vm_page_t vm_page_scan_contig(u_long npages, vm_page_t m_start, 504c869e672SAlan Cox vm_page_t m_end, u_long alignment, vm_paddr_t boundary, int options); 505dc874f98SKonstantin Belousov void vm_page_set_valid_range(vm_page_t m, int base, int size); 506c7aebda8SAttilio Rao int vm_page_sleep_if_busy(vm_page_t m, const char *msg); 507889eb0fcSAlan Cox vm_offset_t vm_page_startup(vm_offset_t vaddr); 508c7aebda8SAttilio Rao void vm_page_sunbusy(vm_page_t m); 509c7aebda8SAttilio Rao int vm_page_trysbusy(vm_page_t m); 5108c22654dSAlan Cox void vm_page_unhold_pages(vm_page_t *ma, int count); 511b1fd102eSMark Johnston void vm_page_unswappable(vm_page_t m); 51215aaea78SAlan Cox boolean_t vm_page_unwire(vm_page_t m, uint8_t queue); 51310cf2560SAlan Cox void vm_page_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr); 5141b40f8c0SMatthew Dillon void vm_page_wire (vm_page_t); 515c7aebda8SAttilio Rao void vm_page_xunbusy_hard(vm_page_t m); 516bd9546a2SKonstantin Belousov void vm_page_xunbusy_maybelocked(vm_page_t m); 5171b40f8c0SMatthew Dillon void vm_page_set_validclean (vm_page_t, int, int); 5181b40f8c0SMatthew Dillon void vm_page_clear_dirty (vm_page_t, int, int); 5191b40f8c0SMatthew Dillon void vm_page_set_invalid (vm_page_t, int, int); 5201b40f8c0SMatthew Dillon int vm_page_is_valid (vm_page_t, int, int); 5211b40f8c0SMatthew Dillon void vm_page_test_dirty (vm_page_t); 522561cc9fcSKonstantin Belousov vm_page_bits_t vm_page_bits(int base, int size); 5238d17e694SJulian Elischer void vm_page_zero_invalid(vm_page_t m, boolean_t setvalid); 524faa273d5SMatthew Dillon void vm_page_free_toq(vm_page_t m); 52598cb733cSKenneth D. Merry 526eddc9291SAlan Cox void vm_page_dirty_KBI(vm_page_t m); 527cf1911a9SKonstantin Belousov void vm_page_lock_KBI(vm_page_t m, const char *file, int line); 528cf1911a9SKonstantin Belousov void vm_page_unlock_KBI(vm_page_t m, const char *file, int line); 529cf1911a9SKonstantin Belousov int vm_page_trylock_KBI(vm_page_t m, const char *file, int line); 530cf1911a9SKonstantin Belousov #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT) 531b4171812SAlan Cox void vm_page_assert_locked_KBI(vm_page_t m, const char *file, int line); 532cf1911a9SKonstantin Belousov void vm_page_lock_assert_KBI(vm_page_t m, int a, const char *file, int line); 533cf1911a9SKonstantin Belousov #endif 534cf1911a9SKonstantin Belousov 535c7aebda8SAttilio Rao #define vm_page_assert_sbusied(m) \ 536c7aebda8SAttilio Rao KASSERT(vm_page_sbusied(m), \ 537c7aebda8SAttilio Rao ("vm_page_assert_sbusied: page %p not shared busy @ %s:%d", \ 538dc62d559SConrad Meyer (m), __FILE__, __LINE__)) 539c7aebda8SAttilio Rao 540c7aebda8SAttilio Rao #define vm_page_assert_unbusied(m) \ 541c7aebda8SAttilio Rao KASSERT(!vm_page_busied(m), \ 542c7aebda8SAttilio Rao ("vm_page_assert_unbusied: page %p busy @ %s:%d", \ 543dc62d559SConrad Meyer (m), __FILE__, __LINE__)) 544c7aebda8SAttilio Rao 545c7aebda8SAttilio Rao #define vm_page_assert_xbusied(m) \ 546c7aebda8SAttilio Rao KASSERT(vm_page_xbusied(m), \ 547c7aebda8SAttilio Rao ("vm_page_assert_xbusied: page %p not exclusive busy @ %s:%d", \ 548dc62d559SConrad Meyer (m), __FILE__, __LINE__)) 549c7aebda8SAttilio Rao 550c7aebda8SAttilio Rao #define vm_page_busied(m) \ 551c7aebda8SAttilio Rao ((m)->busy_lock != VPB_UNBUSIED) 552c7aebda8SAttilio Rao 553c7aebda8SAttilio Rao #define vm_page_sbusy(m) do { \ 554c7aebda8SAttilio Rao if (!vm_page_trysbusy(m)) \ 555dc62d559SConrad Meyer panic("%s: page %p failed shared busying", __func__, \ 556dc62d559SConrad Meyer (m)); \ 557c7aebda8SAttilio Rao } while (0) 558c7aebda8SAttilio Rao 559c7aebda8SAttilio Rao #define vm_page_tryxbusy(m) \ 560dc62d559SConrad Meyer (atomic_cmpset_acq_int(&(m)->busy_lock, VPB_UNBUSIED, \ 561c7aebda8SAttilio Rao VPB_SINGLE_EXCLUSIVER)) 562c7aebda8SAttilio Rao 563c7aebda8SAttilio Rao #define vm_page_xbusied(m) \ 564dc62d559SConrad Meyer (((m)->busy_lock & VPB_SINGLE_EXCLUSIVER) != 0) 565c7aebda8SAttilio Rao 566c7aebda8SAttilio Rao #define vm_page_xbusy(m) do { \ 567c7aebda8SAttilio Rao if (!vm_page_tryxbusy(m)) \ 568dc62d559SConrad Meyer panic("%s: page %p failed exclusive busying", __func__, \ 569dc62d559SConrad Meyer (m)); \ 570c7aebda8SAttilio Rao } while (0) 571c7aebda8SAttilio Rao 572505cd5d1SKonstantin Belousov /* Note: page m's lock must not be owned by the caller. */ 573c7aebda8SAttilio Rao #define vm_page_xunbusy(m) do { \ 574c7aebda8SAttilio Rao if (!atomic_cmpset_rel_int(&(m)->busy_lock, \ 575c7aebda8SAttilio Rao VPB_SINGLE_EXCLUSIVER, VPB_UNBUSIED)) \ 576c7aebda8SAttilio Rao vm_page_xunbusy_hard(m); \ 577c7aebda8SAttilio Rao } while (0) 578c7aebda8SAttilio Rao 5793b1025d2SKonstantin Belousov #ifdef INVARIANTS 5803b1025d2SKonstantin Belousov void vm_page_object_lock_assert(vm_page_t m); 5813b1025d2SKonstantin Belousov #define VM_PAGE_OBJECT_LOCK_ASSERT(m) vm_page_object_lock_assert(m) 582afb69e6bSKonstantin Belousov void vm_page_assert_pga_writeable(vm_page_t m, uint8_t bits); 583afb69e6bSKonstantin Belousov #define VM_PAGE_ASSERT_PGA_WRITEABLE(m, bits) \ 584afb69e6bSKonstantin Belousov vm_page_assert_pga_writeable(m, bits) 5853b1025d2SKonstantin Belousov #else 5863b1025d2SKonstantin Belousov #define VM_PAGE_OBJECT_LOCK_ASSERT(m) (void)0 587afb69e6bSKonstantin Belousov #define VM_PAGE_ASSERT_PGA_WRITEABLE(m, bits) (void)0 5883b1025d2SKonstantin Belousov #endif 5893b1025d2SKonstantin Belousov 590f3b676f0SAlan Cox /* 591369763e3SAlan Cox * We want to use atomic updates for the aflags field, which is 8 bits wide. 592369763e3SAlan Cox * However, not all architectures support atomic operations on 8-bit 593369763e3SAlan Cox * destinations. In order that we can easily use a 32-bit operation, we 594369763e3SAlan Cox * require that the aflags field be 32-bit aligned. 595369763e3SAlan Cox */ 596369763e3SAlan Cox CTASSERT(offsetof(struct vm_page, aflags) % sizeof(uint32_t) == 0); 597369763e3SAlan Cox 598369763e3SAlan Cox /* 599369763e3SAlan Cox * Clear the given bits in the specified page. 600369763e3SAlan Cox */ 601369763e3SAlan Cox static inline void 602369763e3SAlan Cox vm_page_aflag_clear(vm_page_t m, uint8_t bits) 603369763e3SAlan Cox { 604369763e3SAlan Cox uint32_t *addr, val; 605369763e3SAlan Cox 606369763e3SAlan Cox /* 607b4171812SAlan Cox * The PGA_REFERENCED flag can only be cleared if the page is locked. 608369763e3SAlan Cox */ 609369763e3SAlan Cox if ((bits & PGA_REFERENCED) != 0) 610b4171812SAlan Cox vm_page_assert_locked(m); 611369763e3SAlan Cox 612369763e3SAlan Cox /* 613369763e3SAlan Cox * Access the whole 32-bit word containing the aflags field with an 614369763e3SAlan Cox * atomic update. Parallel non-atomic updates to the other fields 615369763e3SAlan Cox * within this word are handled properly by the atomic update. 616369763e3SAlan Cox */ 617369763e3SAlan Cox addr = (void *)&m->aflags; 618369763e3SAlan Cox KASSERT(((uintptr_t)addr & (sizeof(uint32_t) - 1)) == 0, 619369763e3SAlan Cox ("vm_page_aflag_clear: aflags is misaligned")); 620369763e3SAlan Cox val = bits; 621369763e3SAlan Cox #if BYTE_ORDER == BIG_ENDIAN 622369763e3SAlan Cox val <<= 24; 623369763e3SAlan Cox #endif 624369763e3SAlan Cox atomic_clear_32(addr, val); 625369763e3SAlan Cox } 626369763e3SAlan Cox 627369763e3SAlan Cox /* 628369763e3SAlan Cox * Set the given bits in the specified page. 629369763e3SAlan Cox */ 630369763e3SAlan Cox static inline void 631369763e3SAlan Cox vm_page_aflag_set(vm_page_t m, uint8_t bits) 632369763e3SAlan Cox { 633369763e3SAlan Cox uint32_t *addr, val; 634369763e3SAlan Cox 635afb69e6bSKonstantin Belousov VM_PAGE_ASSERT_PGA_WRITEABLE(m, bits); 636369763e3SAlan Cox 637369763e3SAlan Cox /* 638369763e3SAlan Cox * Access the whole 32-bit word containing the aflags field with an 639369763e3SAlan Cox * atomic update. Parallel non-atomic updates to the other fields 640369763e3SAlan Cox * within this word are handled properly by the atomic update. 641369763e3SAlan Cox */ 642369763e3SAlan Cox addr = (void *)&m->aflags; 643369763e3SAlan Cox KASSERT(((uintptr_t)addr & (sizeof(uint32_t) - 1)) == 0, 644369763e3SAlan Cox ("vm_page_aflag_set: aflags is misaligned")); 645369763e3SAlan Cox val = bits; 646369763e3SAlan Cox #if BYTE_ORDER == BIG_ENDIAN 647369763e3SAlan Cox val <<= 24; 648369763e3SAlan Cox #endif 649369763e3SAlan Cox atomic_set_32(addr, val); 650369763e3SAlan Cox } 651369763e3SAlan Cox 652369763e3SAlan Cox /* 653eddc9291SAlan Cox * vm_page_dirty: 654eddc9291SAlan Cox * 655eddc9291SAlan Cox * Set all bits in the page's dirty field. 656eddc9291SAlan Cox * 657eddc9291SAlan Cox * The object containing the specified page must be locked if the 658eddc9291SAlan Cox * call is made from the machine-independent layer. 659eddc9291SAlan Cox * 660eddc9291SAlan Cox * See vm_page_clear_dirty_mask(). 661eddc9291SAlan Cox */ 662eddc9291SAlan Cox static __inline void 663eddc9291SAlan Cox vm_page_dirty(vm_page_t m) 664eddc9291SAlan Cox { 665eddc9291SAlan Cox 666eddc9291SAlan Cox /* Use vm_page_dirty_KBI() under INVARIANTS to save memory. */ 667eddc9291SAlan Cox #if defined(KLD_MODULE) || defined(INVARIANTS) 668eddc9291SAlan Cox vm_page_dirty_KBI(m); 669eddc9291SAlan Cox #else 670eddc9291SAlan Cox m->dirty = VM_PAGE_BITS_ALL; 671eddc9291SAlan Cox #endif 672eddc9291SAlan Cox } 673eddc9291SAlan Cox 674eddc9291SAlan Cox /* 6758d220203SAlan Cox * vm_page_remque: 6768d220203SAlan Cox * 6778d220203SAlan Cox * If the given page is in a page queue, then remove it from that page 6788d220203SAlan Cox * queue. 6798d220203SAlan Cox * 6808d220203SAlan Cox * The page must be locked. 6818d220203SAlan Cox */ 6828d220203SAlan Cox static inline void 6838d220203SAlan Cox vm_page_remque(vm_page_t m) 6848d220203SAlan Cox { 6858d220203SAlan Cox 6868d220203SAlan Cox if (m->queue != PQ_NONE) 6878d220203SAlan Cox vm_page_dequeue(m); 6888d220203SAlan Cox } 6898d220203SAlan Cox 6908d220203SAlan Cox /* 691f3b676f0SAlan Cox * vm_page_undirty: 692f3b676f0SAlan Cox * 693f3b676f0SAlan Cox * Set page to not be dirty. Note: does not clear pmap modify bits 694f3b676f0SAlan Cox */ 695f3b676f0SAlan Cox static __inline void 696f3b676f0SAlan Cox vm_page_undirty(vm_page_t m) 697f3b676f0SAlan Cox { 6983b1025d2SKonstantin Belousov 6993b1025d2SKonstantin Belousov VM_PAGE_OBJECT_LOCK_ASSERT(m); 700f3b676f0SAlan Cox m->dirty = 0; 701f3b676f0SAlan Cox } 702f3b676f0SAlan Cox 7038170d6e5SConrad Meyer static inline void 7048170d6e5SConrad Meyer vm_page_replace_checked(vm_page_t mnew, vm_object_t object, vm_pindex_t pindex, 7058170d6e5SConrad Meyer vm_page_t mold) 7068170d6e5SConrad Meyer { 7078170d6e5SConrad Meyer vm_page_t mret; 7088170d6e5SConrad Meyer 7098170d6e5SConrad Meyer mret = vm_page_replace(mnew, object, pindex); 7108170d6e5SConrad Meyer KASSERT(mret == mold, 7118170d6e5SConrad Meyer ("invalid page replacement, mold=%p, mret=%p", mold, mret)); 7128170d6e5SConrad Meyer 7138170d6e5SConrad Meyer /* Unused if !INVARIANTS. */ 7148170d6e5SConrad Meyer (void)mold; 7158170d6e5SConrad Meyer (void)mret; 7168170d6e5SConrad Meyer } 7178170d6e5SConrad Meyer 718ebcddc72SAlan Cox static inline bool 719ebcddc72SAlan Cox vm_page_active(vm_page_t m) 720ebcddc72SAlan Cox { 721ebcddc72SAlan Cox 722ebcddc72SAlan Cox return (m->queue == PQ_ACTIVE); 723ebcddc72SAlan Cox } 724ebcddc72SAlan Cox 725ebcddc72SAlan Cox static inline bool 726ebcddc72SAlan Cox vm_page_inactive(vm_page_t m) 727ebcddc72SAlan Cox { 728ebcddc72SAlan Cox 729ebcddc72SAlan Cox return (m->queue == PQ_INACTIVE); 730ebcddc72SAlan Cox } 731ebcddc72SAlan Cox 732ebcddc72SAlan Cox static inline bool 733ebcddc72SAlan Cox vm_page_in_laundry(vm_page_t m) 734ebcddc72SAlan Cox { 735ebcddc72SAlan Cox 736b1fd102eSMark Johnston return (m->queue == PQ_LAUNDRY || m->queue == PQ_UNSWAPPABLE); 737ebcddc72SAlan Cox } 738ebcddc72SAlan Cox 739c4473420SPeter Wemm #endif /* _KERNEL */ 740df8bae1dSRodney W. Grimes #endif /* !_VM_PAGE_ */ 741