160727d8bSWarner Losh /*- 2796df753SPedro F. Giffuni * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU) 351369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1991, 1993 5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 6df8bae1dSRodney W. Grimes * 7df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 8df8bae1dSRodney W. Grimes * The Mach Operating System project at Carnegie-Mellon University. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 20df8bae1dSRodney W. Grimes * without specific prior written permission. 21df8bae1dSRodney W. Grimes * 22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32df8bae1dSRodney W. Grimes * SUCH DAMAGE. 33df8bae1dSRodney W. Grimes * 343c4dd356SDavid Greenman * from: @(#)vm_page.h 8.2 (Berkeley) 12/13/93 35df8bae1dSRodney W. Grimes * 36df8bae1dSRodney W. Grimes * 37df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 38df8bae1dSRodney W. Grimes * All rights reserved. 39df8bae1dSRodney W. Grimes * 40df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 41df8bae1dSRodney W. Grimes * 42df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 43df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 44df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 45df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 46df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 47df8bae1dSRodney W. Grimes * 48df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 49df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 50df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 51df8bae1dSRodney W. Grimes * 52df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 53df8bae1dSRodney W. Grimes * 54df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 55df8bae1dSRodney W. Grimes * School of Computer Science 56df8bae1dSRodney W. Grimes * Carnegie Mellon University 57df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 58df8bae1dSRodney W. Grimes * 59df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 60df8bae1dSRodney W. Grimes * rights to redistribute these changes. 613c4dd356SDavid Greenman * 62c3aac50fSPeter Wemm * $FreeBSD$ 63df8bae1dSRodney W. Grimes */ 64df8bae1dSRodney W. Grimes 65df8bae1dSRodney W. Grimes /* 66df8bae1dSRodney W. Grimes * Resident memory system definitions. 67df8bae1dSRodney W. Grimes */ 68df8bae1dSRodney W. Grimes 69df8bae1dSRodney W. Grimes #ifndef _VM_PAGE_ 70df8bae1dSRodney W. Grimes #define _VM_PAGE_ 71df8bae1dSRodney W. Grimes 72f919ebdeSDavid Greenman #include <vm/pmap.h> 73069e9bc1SDoug Rabson 74df8bae1dSRodney W. Grimes /* 75df8bae1dSRodney W. Grimes * Management of resident (logical) pages. 76df8bae1dSRodney W. Grimes * 77df8bae1dSRodney W. Grimes * A small structure is kept for each resident 78df8bae1dSRodney W. Grimes * page, indexed by page number. Each structure 79da384208SAlan Cox * is an element of several collections: 80df8bae1dSRodney W. Grimes * 81da384208SAlan Cox * A radix tree used to quickly 82df8bae1dSRodney W. Grimes * perform object/offset lookups 83df8bae1dSRodney W. Grimes * 84df8bae1dSRodney W. Grimes * A list of all pages for a given object, 85df8bae1dSRodney W. Grimes * so they can be quickly deactivated at 86df8bae1dSRodney W. Grimes * time of deallocation. 87df8bae1dSRodney W. Grimes * 88df8bae1dSRodney W. Grimes * An ordered list of pages due for pageout. 89df8bae1dSRodney W. Grimes * 90df8bae1dSRodney W. Grimes * In addition, the structure contains the object 91df8bae1dSRodney W. Grimes * and offset to which this page belongs (for pageout), 92df8bae1dSRodney W. Grimes * and sundry status bits. 93df8bae1dSRodney W. Grimes * 943c76db4cSAlan Cox * In general, operations on this structure's mutable fields are 953c76db4cSAlan Cox * synchronized using either one of or a combination of the lock on the 965cd29d0fSMark Johnston * object that the page belongs to (O), the page lock (P), 975cd29d0fSMark Johnston * the per-domain lock for the free queues (F), or the page's queue 985cd29d0fSMark Johnston * lock (Q). The physical address of a page is used to select its page 995cd29d0fSMark Johnston * lock from a pool. The queue lock for a page depends on the value of 1005cd29d0fSMark Johnston * its queue field and described in detail below. If a field is 1013c76db4cSAlan Cox * annotated below with two of these locks, then holding either lock is 1023c76db4cSAlan Cox * sufficient for read access, but both locks are required for write 1031d3a1bcfSMark Johnston * access. An annotation of (C) indicates that the field is immutable. 1040ce3ba8cSKip Macy * 105abb9b935SKonstantin Belousov * In contrast, the synchronization of accesses to the page's 106abb9b935SKonstantin Belousov * dirty field is machine dependent (M). In the 107abb9b935SKonstantin Belousov * machine-independent layer, the lock on the object that the 108abb9b935SKonstantin Belousov * page belongs to must be held in order to operate on the field. 109abb9b935SKonstantin Belousov * However, the pmap layer is permitted to set all bits within 110abb9b935SKonstantin Belousov * the field without holding that lock. If the underlying 111abb9b935SKonstantin Belousov * architecture does not support atomic read-modify-write 112abb9b935SKonstantin Belousov * operations on the field's type, then the machine-independent 1132042bb37SKonstantin Belousov * layer uses a 32-bit atomic on the aligned 32-bit word that 114abb9b935SKonstantin Belousov * contains the dirty field. In the machine-independent layer, 115abb9b935SKonstantin Belousov * the implementation of read-modify-write operations on the 116abb9b935SKonstantin Belousov * field is encapsulated in vm_page_clear_dirty_mask(). 1171d3a1bcfSMark Johnston * 1181d3a1bcfSMark Johnston * The page structure contains two counters which prevent page reuse. 1191d3a1bcfSMark Johnston * Both counters are protected by the page lock (P). The hold 1201d3a1bcfSMark Johnston * counter counts transient references obtained via a pmap lookup, and 1211d3a1bcfSMark Johnston * is also used to prevent page reclamation in situations where it is 1221d3a1bcfSMark Johnston * undesirable to block other accesses to the page. The wire counter 1231d3a1bcfSMark Johnston * is used to implement mlock(2) and is non-zero for pages containing 1241d3a1bcfSMark Johnston * kernel memory. Pages that are wired or held will not be reclaimed 1251d3a1bcfSMark Johnston * or laundered by the page daemon, but are treated differently during 1261d3a1bcfSMark Johnston * a page queue scan: held pages remain at their position in the queue, 1271d3a1bcfSMark Johnston * while wired pages are removed from the queue and must later be 1281d3a1bcfSMark Johnston * re-enqueued appropriately by the unwiring thread. It is legal to 1291d3a1bcfSMark Johnston * call vm_page_free() on a held page; doing so causes it to be removed 1301d3a1bcfSMark Johnston * from its object and page queue, and the page is released to the 1311d3a1bcfSMark Johnston * allocator once the last hold reference is dropped. In contrast, 1321d3a1bcfSMark Johnston * wired pages may not be freed. 1331d3a1bcfSMark Johnston * 1341d3a1bcfSMark Johnston * In some pmap implementations, the wire count of a page table page is 1351d3a1bcfSMark Johnston * used to track the number of populated entries. 1361d3a1bcfSMark Johnston * 1371d3a1bcfSMark Johnston * The busy lock is an embedded reader-writer lock which protects the 1381d3a1bcfSMark Johnston * page's contents and identity (i.e., its <object, pindex> tuple) and 1391d3a1bcfSMark Johnston * interlocks with the object lock (O). In particular, a page may be 1401d3a1bcfSMark Johnston * busied or unbusied only with the object write lock held. To avoid 1411d3a1bcfSMark Johnston * bloating the page structure, the busy lock lacks some of the 1421d3a1bcfSMark Johnston * features available to the kernel's general-purpose synchronization 1431d3a1bcfSMark Johnston * primitives. As a result, busy lock ordering rules are not verified, 1441d3a1bcfSMark Johnston * lock recursion is not detected, and an attempt to xbusy a busy page 1451d3a1bcfSMark Johnston * or sbusy an xbusy page results will trigger a panic rather than 1461d3a1bcfSMark Johnston * causing the thread to block. vm_page_sleep_if_busy() can be used to 1471d3a1bcfSMark Johnston * sleep until the page's busy state changes, after which the caller 1481d3a1bcfSMark Johnston * must re-lookup the page and re-evaluate its state. 1495cd29d0fSMark Johnston * 1505cd29d0fSMark Johnston * The queue field is the index of the page queue containing the 1515cd29d0fSMark Johnston * page, or PQ_NONE if the page is not enqueued. The queue lock of a 1525cd29d0fSMark Johnston * page is the page queue lock corresponding to the page queue index, 1535cd29d0fSMark Johnston * or the page lock (P) for the page if it is not enqueued. To modify 1545cd29d0fSMark Johnston * the queue field, the queue lock for the old value of the field must 1555cd29d0fSMark Johnston * be held. It is invalid for a page's queue field to transition 1565cd29d0fSMark Johnston * between two distinct page queue indices. That is, when updating 1575cd29d0fSMark Johnston * the queue field, either the new value or the old value must be 1585cd29d0fSMark Johnston * PQ_NONE. 1595cd29d0fSMark Johnston * 1605cd29d0fSMark Johnston * To avoid contention on page queue locks, page queue operations 1615cd29d0fSMark Johnston * (enqueue, dequeue, requeue) are batched using per-CPU queues. 1625cd29d0fSMark Johnston * A deferred operation is requested by inserting an entry into a 1635cd29d0fSMark Johnston * batch queue; the entry is simply a pointer to the page, and the 1645cd29d0fSMark Johnston * request type is encoded in the page's aflags field using the values 1655cd29d0fSMark Johnston * in PGA_QUEUE_STATE_MASK. The type-stability of struct vm_pages is 1665cd29d0fSMark Johnston * crucial to this scheme since the processing of entries in a given 1675cd29d0fSMark Johnston * batch queue may be deferred indefinitely. In particular, a page 1685cd29d0fSMark Johnston * may be freed before its pending batch queue entries have been 1695cd29d0fSMark Johnston * processed. The page lock (P) must be held to schedule a batched 1705cd29d0fSMark Johnston * queue operation, and the page queue lock must be held in order to 1715cd29d0fSMark Johnston * process batch queue entries for the page queue. 172df8bae1dSRodney W. Grimes */ 173df8bae1dSRodney W. Grimes 174561cc9fcSKonstantin Belousov #if PAGE_SIZE == 4096 175561cc9fcSKonstantin Belousov #define VM_PAGE_BITS_ALL 0xffu 176561cc9fcSKonstantin Belousov typedef uint8_t vm_page_bits_t; 177561cc9fcSKonstantin Belousov #elif PAGE_SIZE == 8192 178561cc9fcSKonstantin Belousov #define VM_PAGE_BITS_ALL 0xffffu 179561cc9fcSKonstantin Belousov typedef uint16_t vm_page_bits_t; 180561cc9fcSKonstantin Belousov #elif PAGE_SIZE == 16384 181561cc9fcSKonstantin Belousov #define VM_PAGE_BITS_ALL 0xffffffffu 182561cc9fcSKonstantin Belousov typedef uint32_t vm_page_bits_t; 183561cc9fcSKonstantin Belousov #elif PAGE_SIZE == 32768 184561cc9fcSKonstantin Belousov #define VM_PAGE_BITS_ALL 0xfffffffffffffffflu 185561cc9fcSKonstantin Belousov typedef uint64_t vm_page_bits_t; 186561cc9fcSKonstantin Belousov #endif 187561cc9fcSKonstantin Belousov 188df8bae1dSRodney W. Grimes struct vm_page { 189c325e866SKonstantin Belousov union { 190c325e866SKonstantin Belousov TAILQ_ENTRY(vm_page) q; /* page queue or free list (Q) */ 191c325e866SKonstantin Belousov struct { 192c325e866SKonstantin Belousov SLIST_ENTRY(vm_page) ss; /* private slists */ 193c325e866SKonstantin Belousov void *pv; 194c325e866SKonstantin Belousov } s; 195c325e866SKonstantin Belousov struct { 196c325e866SKonstantin Belousov u_long p; 197c325e866SKonstantin Belousov u_long v; 198c325e866SKonstantin Belousov } memguard; 199c325e866SKonstantin Belousov } plinks; 200e3975643SJake Burkholder TAILQ_ENTRY(vm_page) listq; /* pages in same object (O) */ 2015ac59343SAlan Cox vm_object_t object; /* which object am I in (O,P) */ 20243319c11SAlan Cox vm_pindex_t pindex; /* offset into object (O,P) */ 2035cd29d0fSMark Johnston vm_paddr_t phys_addr; /* physical address of page (C) */ 204763df3ecSPedro F. Giffuni struct md_page md; /* machine dependent stuff */ 205fd8c28bfSAlan Cox u_int wire_count; /* wired down maps refs (P) */ 2063846a822SKonstantin Belousov volatile u_int busy_lock; /* busy owners lock */ 2073846a822SKonstantin Belousov uint16_t flags; /* page PG_* flags (P) */ 208eeacb3b0SMark Johnston uint8_t order; /* index of the buddy queue (F) */ 209eeacb3b0SMark Johnston uint8_t pool; /* vm_phys freepool index (F) */ 2103407fefeSKonstantin Belousov uint8_t aflags; /* access is atomic */ 211081a4881SAlan Cox uint8_t oflags; /* page VPO_* flags (O) */ 21299d92d73SMark Johnston uint8_t queue; /* page queue index (Q) */ 213dd05fa19SAlan Cox int8_t psind; /* pagesizes[] index (O) */ 2141d3a1bcfSMark Johnston int8_t segind; /* vm_phys segment index (C) */ 215a15f7df5SAttilio Rao u_char act_count; /* page usage count (P) */ 216c325e866SKonstantin Belousov /* NOTE that these must support one bit per DEV_BSIZE in a page */ 217bd7e5f99SJohn Dyson /* so, on normal X86 kernels, they must be at least 8 bits wide */ 218561cc9fcSKonstantin Belousov vm_page_bits_t valid; /* map of valid DEV_BSIZE chunks (O) */ 219561cc9fcSKonstantin Belousov vm_page_bits_t dirty; /* map of dirty DEV_BSIZE chunks (M) */ 220df8bae1dSRodney W. Grimes }; 221df8bae1dSRodney W. Grimes 2225786be7cSAlan Cox /* 2235786be7cSAlan Cox * Page flags stored in oflags: 2245786be7cSAlan Cox * 2255786be7cSAlan Cox * Access to these page flags is synchronized by the lock on the object 2265786be7cSAlan Cox * containing the page (O). 227d98d0ce2SKonstantin Belousov * 228d98d0ce2SKonstantin Belousov * Note: VPO_UNMANAGED (used by OBJT_DEVICE, OBJT_PHYS and OBJT_SG) 229d98d0ce2SKonstantin Belousov * indicates that the page is not under PV management but 230d98d0ce2SKonstantin Belousov * otherwise should be treated as a normal page. Pages not 231d98d0ce2SKonstantin Belousov * under PV management cannot be paged out via the 232d98d0ce2SKonstantin Belousov * object/vm_page_t because there is no knowledge of their pte 233d98d0ce2SKonstantin Belousov * mappings, and such pages are also not on any PQ queue. 234d98d0ce2SKonstantin Belousov * 2355786be7cSAlan Cox */ 23649bfa624SAlan Cox #define VPO_KMEM_EXEC 0x01 /* kmem mapping allows execution */ 237c7aebda8SAttilio Rao #define VPO_SWAPSLEEP 0x02 /* waiting for swap to finish */ 238081a4881SAlan Cox #define VPO_UNMANAGED 0x04 /* no PV management for page */ 239081a4881SAlan Cox #define VPO_SWAPINPROG 0x08 /* swap I/O in progress on page */ 240081a4881SAlan Cox #define VPO_NOSYNC 0x10 /* do not collect for syncer */ 2415786be7cSAlan Cox 242c7aebda8SAttilio Rao /* 243c7aebda8SAttilio Rao * Busy page implementation details. 244c7aebda8SAttilio Rao * The algorithm is taken mostly by rwlock(9) and sx(9) locks implementation, 245c7aebda8SAttilio Rao * even if the support for owner identity is removed because of size 246c7aebda8SAttilio Rao * constraints. Checks on lock recursion are then not possible, while the 247c7aebda8SAttilio Rao * lock assertions effectiveness is someway reduced. 248c7aebda8SAttilio Rao */ 249c7aebda8SAttilio Rao #define VPB_BIT_SHARED 0x01 250c7aebda8SAttilio Rao #define VPB_BIT_EXCLUSIVE 0x02 251c7aebda8SAttilio Rao #define VPB_BIT_WAITERS 0x04 252c7aebda8SAttilio Rao #define VPB_BIT_FLAGMASK \ 253c7aebda8SAttilio Rao (VPB_BIT_SHARED | VPB_BIT_EXCLUSIVE | VPB_BIT_WAITERS) 254c7aebda8SAttilio Rao 255c7aebda8SAttilio Rao #define VPB_SHARERS_SHIFT 3 256c7aebda8SAttilio Rao #define VPB_SHARERS(x) \ 257c7aebda8SAttilio Rao (((x) & ~VPB_BIT_FLAGMASK) >> VPB_SHARERS_SHIFT) 258c7aebda8SAttilio Rao #define VPB_SHARERS_WORD(x) ((x) << VPB_SHARERS_SHIFT | VPB_BIT_SHARED) 259c7aebda8SAttilio Rao #define VPB_ONE_SHARER (1 << VPB_SHARERS_SHIFT) 260c7aebda8SAttilio Rao 261c7aebda8SAttilio Rao #define VPB_SINGLE_EXCLUSIVER VPB_BIT_EXCLUSIVE 262c7aebda8SAttilio Rao 263c7aebda8SAttilio Rao #define VPB_UNBUSIED VPB_SHARERS_WORD(0) 264c7aebda8SAttilio Rao 26544e46b9eSAlan Cox #define PQ_NONE 255 26644e46b9eSAlan Cox #define PQ_INACTIVE 0 26744e46b9eSAlan Cox #define PQ_ACTIVE 1 268ebcddc72SAlan Cox #define PQ_LAUNDRY 2 269b1fd102eSMark Johnston #define PQ_UNSWAPPABLE 3 270b1fd102eSMark Johnston #define PQ_COUNT 4 271ef39c05bSAlexander Leidinger 272b9e8fb64SKonstantin Belousov #ifndef VM_PAGE_HAVE_PGLIST 2738d220203SAlan Cox TAILQ_HEAD(pglist, vm_page); 274b9e8fb64SKonstantin Belousov #define VM_PAGE_HAVE_PGLIST 275b9e8fb64SKonstantin Belousov #endif 276c325e866SKonstantin Belousov SLIST_HEAD(spglist, vm_page); 27770c17636SAlan Cox 278449c2e92SKonstantin Belousov #ifdef _KERNEL 279bfc8c24cSGleb Smirnoff extern vm_page_t bogus_page; 280449c2e92SKonstantin Belousov #endif /* _KERNEL */ 281449c2e92SKonstantin Belousov 2824ceaf45dSAttilio Rao extern struct mtx_padalign pa_lock[]; 2832965a453SKip Macy 2842965a453SKip Macy #if defined(__arm__) 2852965a453SKip Macy #define PDRSHIFT PDR_SHIFT 2862965a453SKip Macy #elif !defined(PDRSHIFT) 2872965a453SKip Macy #define PDRSHIFT 21 2882965a453SKip Macy #endif 2892965a453SKip Macy 2902965a453SKip Macy #define pa_index(pa) ((pa) >> PDRSHIFT) 2914ceaf45dSAttilio Rao #define PA_LOCKPTR(pa) ((struct mtx *)(&pa_lock[pa_index(pa) % PA_LOCK_COUNT])) 2922965a453SKip Macy #define PA_LOCKOBJPTR(pa) ((struct lock_object *)PA_LOCKPTR((pa))) 2932965a453SKip Macy #define PA_LOCK(pa) mtx_lock(PA_LOCKPTR(pa)) 2942965a453SKip Macy #define PA_TRYLOCK(pa) mtx_trylock(PA_LOCKPTR(pa)) 2952965a453SKip Macy #define PA_UNLOCK(pa) mtx_unlock(PA_LOCKPTR(pa)) 2962965a453SKip Macy #define PA_UNLOCK_COND(pa) \ 2972965a453SKip Macy do { \ 298567e51e1SAlan Cox if ((pa) != 0) { \ 299567e51e1SAlan Cox PA_UNLOCK((pa)); \ 300567e51e1SAlan Cox (pa) = 0; \ 301567e51e1SAlan Cox } \ 3022965a453SKip Macy } while (0) 3032965a453SKip Macy 3042965a453SKip Macy #define PA_LOCK_ASSERT(pa, a) mtx_assert(PA_LOCKPTR(pa), (a)) 3052965a453SKip Macy 306f4b36404SMatt Macy #if defined(KLD_MODULE) && !defined(KLD_TIED) 307cf1911a9SKonstantin Belousov #define vm_page_lock(m) vm_page_lock_KBI((m), LOCK_FILE, LOCK_LINE) 308cf1911a9SKonstantin Belousov #define vm_page_unlock(m) vm_page_unlock_KBI((m), LOCK_FILE, LOCK_LINE) 309cf1911a9SKonstantin Belousov #define vm_page_trylock(m) vm_page_trylock_KBI((m), LOCK_FILE, LOCK_LINE) 310cf1911a9SKonstantin Belousov #else /* !KLD_MODULE */ 3112965a453SKip Macy #define vm_page_lockptr(m) (PA_LOCKPTR(VM_PAGE_TO_PHYS((m)))) 3122965a453SKip Macy #define vm_page_lock(m) mtx_lock(vm_page_lockptr((m))) 3132965a453SKip Macy #define vm_page_unlock(m) mtx_unlock(vm_page_lockptr((m))) 3142965a453SKip Macy #define vm_page_trylock(m) mtx_trylock(vm_page_lockptr((m))) 315ef5ba5a3SAlan Cox #endif 316ef5ba5a3SAlan Cox #if defined(INVARIANTS) 317b4171812SAlan Cox #define vm_page_assert_locked(m) \ 318b4171812SAlan Cox vm_page_assert_locked_KBI((m), __FILE__, __LINE__) 319ef5ba5a3SAlan Cox #define vm_page_lock_assert(m, a) \ 320ef5ba5a3SAlan Cox vm_page_lock_assert_KBI((m), (a), __FILE__, __LINE__) 321ef5ba5a3SAlan Cox #else 322b4171812SAlan Cox #define vm_page_assert_locked(m) 323ef5ba5a3SAlan Cox #define vm_page_lock_assert(m, a) 324cf1911a9SKonstantin Belousov #endif 325e67e0775SAlan Cox 326df8bae1dSRodney W. Grimes /* 327369763e3SAlan Cox * The vm_page's aflags are updated using atomic operations. To set or clear 328369763e3SAlan Cox * these flags, the functions vm_page_aflag_set() and vm_page_aflag_clear() 329369763e3SAlan Cox * must be used. Neither these flags nor these functions are part of the KBI. 3303407fefeSKonstantin Belousov * 331b4171812SAlan Cox * PGA_REFERENCED may be cleared only if the page is locked. It is set by 332b4171812SAlan Cox * both the MI and MD VM layers. However, kernel loadable modules should not 333b4171812SAlan Cox * directly set this flag. They should call vm_page_reference() instead. 334ce186587SAlan Cox * 335afb69e6bSKonstantin Belousov * PGA_WRITEABLE is set exclusively on managed pages by pmap_enter(). 336afb69e6bSKonstantin Belousov * When it does so, the object must be locked, or the page must be 337afb69e6bSKonstantin Belousov * exclusive busied. The MI VM layer must never access this flag 338afb69e6bSKonstantin Belousov * directly. Instead, it should call pmap_page_is_write_mapped(). 33957bd5cceSNathan Whitehorn * 34057bd5cceSNathan Whitehorn * PGA_EXECUTABLE may be set by pmap routines, and indicates that a page has 3416031c68dSAlan Cox * at least one executable mapping. It is not consumed by the MI VM layer. 3425cd29d0fSMark Johnston * 3435cd29d0fSMark Johnston * PGA_ENQUEUED is set and cleared when a page is inserted into or removed 3445cd29d0fSMark Johnston * from a page queue, respectively. It determines whether the plinks.q field 3455cd29d0fSMark Johnston * of the page is valid. To set or clear this flag, the queue lock for the 3465cd29d0fSMark Johnston * page must be held: the page queue lock corresponding to the page's "queue" 3475cd29d0fSMark Johnston * field if its value is not PQ_NONE, and the page lock otherwise. 3485cd29d0fSMark Johnston * 3495cd29d0fSMark Johnston * PGA_DEQUEUE is set when the page is scheduled to be dequeued from a page 3505cd29d0fSMark Johnston * queue, and cleared when the dequeue request is processed. A page may 3515cd29d0fSMark Johnston * have PGA_DEQUEUE set and PGA_ENQUEUED cleared, for instance if a dequeue 3525cd29d0fSMark Johnston * is requested after the page is scheduled to be enqueued but before it is 3531e2b3e6fSMark Johnston * actually inserted into the page queue. For allocated pages, the page lock 3541e2b3e6fSMark Johnston * must be held to set this flag, but it may be set by vm_page_free_prep() 3551e2b3e6fSMark Johnston * without the page lock held. The page queue lock must be held to clear the 3561e2b3e6fSMark Johnston * PGA_DEQUEUE flag. 3575cd29d0fSMark Johnston * 3585cd29d0fSMark Johnston * PGA_REQUEUE is set when the page is scheduled to be enqueued or requeued 3595cd29d0fSMark Johnston * in its page queue. The page lock must be held to set this flag, and the 3605cd29d0fSMark Johnston * queue lock for the page must be held to clear it. 3615cd29d0fSMark Johnston * 3625cd29d0fSMark Johnston * PGA_REQUEUE_HEAD is a special flag for enqueuing pages near the head of 3635cd29d0fSMark Johnston * the inactive queue, thus bypassing LRU. The page lock must be held to 3645cd29d0fSMark Johnston * set this flag, and the queue lock for the page must be held to clear it. 365df8bae1dSRodney W. Grimes */ 3663407fefeSKonstantin Belousov #define PGA_WRITEABLE 0x01 /* page may be mapped writeable */ 3673407fefeSKonstantin Belousov #define PGA_REFERENCED 0x02 /* page has been referenced */ 36857bd5cceSNathan Whitehorn #define PGA_EXECUTABLE 0x04 /* page may be mapped executable */ 3695cd29d0fSMark Johnston #define PGA_ENQUEUED 0x08 /* page is enqueued in a page queue */ 3705cd29d0fSMark Johnston #define PGA_DEQUEUE 0x10 /* page is due to be dequeued */ 3715cd29d0fSMark Johnston #define PGA_REQUEUE 0x20 /* page is due to be requeued */ 3725cd29d0fSMark Johnston #define PGA_REQUEUE_HEAD 0x40 /* page requeue should bypass LRU */ 3735cd29d0fSMark Johnston 3745cd29d0fSMark Johnston #define PGA_QUEUE_STATE_MASK (PGA_ENQUEUED | PGA_DEQUEUE | PGA_REQUEUE | \ 3755cd29d0fSMark Johnston PGA_REQUEUE_HEAD) 3763407fefeSKonstantin Belousov 3773407fefeSKonstantin Belousov /* 3783407fefeSKonstantin Belousov * Page flags. If changed at any other time than page allocation or 3793407fefeSKonstantin Belousov * freeing, the modification must be protected by the vm_page lock. 380d9a73522SMark Johnston * 381d9a73522SMark Johnston * The PG_PCPU_CACHE flag is set at allocation time if the page was 382d9a73522SMark Johnston * allocated from a per-CPU cache. It is cleared the next time that the 383d9a73522SMark Johnston * page is allocated from the physical memory allocator. 3843407fefeSKonstantin Belousov */ 3859f74cdbfSMark Johnston #define PG_PCPU_CACHE 0x0001 /* was allocated from per-CPU caches */ 386081a4881SAlan Cox #define PG_FICTITIOUS 0x0004 /* physical page doesn't exist */ 387081a4881SAlan Cox #define PG_ZERO 0x0008 /* page is zeroed */ 388081a4881SAlan Cox #define PG_MARKER 0x0010 /* special queue marker page */ 389081a4881SAlan Cox #define PG_NODUMP 0x0080 /* don't include this page in a dump */ 390df8bae1dSRodney W. Grimes 39124a1cce3SDavid Greenman /* 39224a1cce3SDavid Greenman * Misc constants. 39324a1cce3SDavid Greenman */ 39424a1cce3SDavid Greenman #define ACT_DECLINE 1 39524a1cce3SDavid Greenman #define ACT_ADVANCE 3 39638efa82bSJohn Dyson #define ACT_INIT 5 3975070c7f8SJohn Dyson #define ACT_MAX 64 398df8bae1dSRodney W. Grimes 399c4473420SPeter Wemm #ifdef _KERNEL 40004a18977SAlan Cox 401369763e3SAlan Cox #include <sys/systm.h> 402369763e3SAlan Cox 403369763e3SAlan Cox #include <machine/atomic.h> 404369763e3SAlan Cox 405df8bae1dSRodney W. Grimes /* 406b1fd102eSMark Johnston * Each pageable resident page falls into one of five lists: 407df8bae1dSRodney W. Grimes * 408df8bae1dSRodney W. Grimes * free 409df8bae1dSRodney W. Grimes * Available for allocation now. 41024a1cce3SDavid Greenman * 411df8bae1dSRodney W. Grimes * inactive 4126c5e9bbdSMike Pritchard * Low activity, candidates for reclamation. 4133453bca8SAlan Cox * This list is approximately LRU ordered. 4143453bca8SAlan Cox * 4153453bca8SAlan Cox * laundry 416df8bae1dSRodney W. Grimes * This is the list of pages that should be 417df8bae1dSRodney W. Grimes * paged out next. 41824a1cce3SDavid Greenman * 419b1fd102eSMark Johnston * unswappable 420b1fd102eSMark Johnston * Dirty anonymous pages that cannot be paged 421b1fd102eSMark Johnston * out because no swap device is configured. 422b1fd102eSMark Johnston * 423df8bae1dSRodney W. Grimes * active 4243453bca8SAlan Cox * Pages that are "active", i.e., they have been 42524a1cce3SDavid Greenman * recently referenced. 42610ad4d48SJohn Dyson * 427df8bae1dSRodney W. Grimes */ 428df8bae1dSRodney W. Grimes 4290d94caffSDavid Greenman extern vm_page_t vm_page_array; /* First resident page in table */ 43013a0b7bcSKonstantin Belousov extern long vm_page_array_size; /* number of vm_page_t's */ 4310d94caffSDavid Greenman extern long first_page; /* first physical page number */ 4320d94caffSDavid Greenman 433df8bae1dSRodney W. Grimes #define VM_PAGE_TO_PHYS(entry) ((entry)->phys_addr) 434df8bae1dSRodney W. Grimes 435a5073058SJason A. Harmening /* 4367c989c15SJason A. Harmening * PHYS_TO_VM_PAGE() returns the vm_page_t object that represents a memory 437a5073058SJason A. Harmening * page to which the given physical address belongs. The correct vm_page_t 438a5073058SJason A. Harmening * object is returned for addresses that are not page-aligned. 439a5073058SJason A. Harmening */ 440b6de32bdSKonstantin Belousov vm_page_t PHYS_TO_VM_PAGE(vm_paddr_t pa); 441df8bae1dSRodney W. Grimes 44289fc8bdbSGleb Smirnoff /* 44389fc8bdbSGleb Smirnoff * Page allocation parameters for vm_page for the functions 44489fc8bdbSGleb Smirnoff * vm_page_alloc(), vm_page_grab(), vm_page_alloc_contig() and 44589fc8bdbSGleb Smirnoff * vm_page_alloc_freelist(). Some functions support only a subset 44689fc8bdbSGleb Smirnoff * of the flags, and ignore others, see the flags legend. 44789fc8bdbSGleb Smirnoff * 4485471caf6SAlan Cox * The meaning of VM_ALLOC_ZERO differs slightly between the vm_page_alloc*() 4495471caf6SAlan Cox * and the vm_page_grab*() functions. See these functions for details. 4505471caf6SAlan Cox * 45189fc8bdbSGleb Smirnoff * Bits 0 - 1 define class. 45289fc8bdbSGleb Smirnoff * Bits 2 - 15 dedicated for flags. 45389fc8bdbSGleb Smirnoff * Legend: 45489fc8bdbSGleb Smirnoff * (a) - vm_page_alloc() supports the flag. 45589fc8bdbSGleb Smirnoff * (c) - vm_page_alloc_contig() supports the flag. 45689fc8bdbSGleb Smirnoff * (f) - vm_page_alloc_freelist() supports the flag. 45789fc8bdbSGleb Smirnoff * (g) - vm_page_grab() supports the flag. 4585471caf6SAlan Cox * (p) - vm_page_grab_pages() supports the flag. 45989fc8bdbSGleb Smirnoff * Bits above 15 define the count of additional pages that the caller 46089fc8bdbSGleb Smirnoff * intends to allocate. 46189fc8bdbSGleb Smirnoff */ 4626d40c3d3SDavid Greenman #define VM_ALLOC_NORMAL 0 4636d40c3d3SDavid Greenman #define VM_ALLOC_INTERRUPT 1 4646d40c3d3SDavid Greenman #define VM_ALLOC_SYSTEM 2 465827b2fa0SAlan Cox #define VM_ALLOC_CLASS_MASK 3 4668d6fbbb8SJeff Roberson #define VM_ALLOC_WAITOK 0x0008 /* (acf) Sleep and retry */ 4678d6fbbb8SJeff Roberson #define VM_ALLOC_WAITFAIL 0x0010 /* (acf) Sleep and return error */ 4685471caf6SAlan Cox #define VM_ALLOC_WIRED 0x0020 /* (acfgp) Allocate a wired page */ 4695471caf6SAlan Cox #define VM_ALLOC_ZERO 0x0040 /* (acfgp) Allocate a prezeroed page */ 47089fc8bdbSGleb Smirnoff #define VM_ALLOC_NOOBJ 0x0100 /* (acg) No associated object */ 4715471caf6SAlan Cox #define VM_ALLOC_NOBUSY 0x0200 /* (acgp) Do not excl busy the page */ 4725471caf6SAlan Cox #define VM_ALLOC_IGN_SBUSY 0x1000 /* (gp) Ignore shared busy flag */ 47389fc8bdbSGleb Smirnoff #define VM_ALLOC_NODUMP 0x2000 /* (ag) don't include in dump */ 4745471caf6SAlan Cox #define VM_ALLOC_SBUSY 0x4000 /* (acgp) Shared busy the page */ 4758d6fbbb8SJeff Roberson #define VM_ALLOC_NOWAIT 0x8000 /* (acfgp) Do not sleep */ 4765f195aa3SKonstantin Belousov #define VM_ALLOC_COUNT_SHIFT 16 4775f195aa3SKonstantin Belousov #define VM_ALLOC_COUNT(count) ((count) << VM_ALLOC_COUNT_SHIFT) 4780d94caffSDavid Greenman 479b32ecf44SKonstantin Belousov #ifdef M_NOWAIT 480b32ecf44SKonstantin Belousov static inline int 481b32ecf44SKonstantin Belousov malloc2vm_flags(int malloc_flags) 482b32ecf44SKonstantin Belousov { 483b32ecf44SKonstantin Belousov int pflags; 484b32ecf44SKonstantin Belousov 485962b064aSKonstantin Belousov KASSERT((malloc_flags & M_USE_RESERVE) == 0 || 486962b064aSKonstantin Belousov (malloc_flags & M_NOWAIT) != 0, 487962b064aSKonstantin Belousov ("M_USE_RESERVE requires M_NOWAIT")); 488b32ecf44SKonstantin Belousov pflags = (malloc_flags & M_USE_RESERVE) != 0 ? VM_ALLOC_INTERRUPT : 489b32ecf44SKonstantin Belousov VM_ALLOC_SYSTEM; 490b32ecf44SKonstantin Belousov if ((malloc_flags & M_ZERO) != 0) 491b32ecf44SKonstantin Belousov pflags |= VM_ALLOC_ZERO; 492b32ecf44SKonstantin Belousov if ((malloc_flags & M_NODUMP) != 0) 493b32ecf44SKonstantin Belousov pflags |= VM_ALLOC_NODUMP; 4948d6fbbb8SJeff Roberson if ((malloc_flags & M_NOWAIT)) 4958d6fbbb8SJeff Roberson pflags |= VM_ALLOC_NOWAIT; 4968d6fbbb8SJeff Roberson if ((malloc_flags & M_WAITOK)) 4978d6fbbb8SJeff Roberson pflags |= VM_ALLOC_WAITOK; 498b32ecf44SKonstantin Belousov return (pflags); 499b32ecf44SKonstantin Belousov } 500b32ecf44SKonstantin Belousov #endif 501b32ecf44SKonstantin Belousov 50288302601SAlan Cox /* 50388302601SAlan Cox * Predicates supported by vm_page_ps_test(): 50488302601SAlan Cox * 50588302601SAlan Cox * PS_ALL_DIRTY is true only if the entire (super)page is dirty. 50688302601SAlan Cox * However, it can be spuriously false when the (super)page has become 50788302601SAlan Cox * dirty in the pmap but that information has not been propagated to the 50888302601SAlan Cox * machine-independent layer. 50988302601SAlan Cox */ 51088302601SAlan Cox #define PS_ALL_DIRTY 0x1 51188302601SAlan Cox #define PS_ALL_VALID 0x2 51288302601SAlan Cox #define PS_NONE_BUSY 0x4 51388302601SAlan Cox 514c7aebda8SAttilio Rao void vm_page_busy_downgrade(vm_page_t m); 5155975e53dSKonstantin Belousov void vm_page_busy_sleep(vm_page_t m, const char *msg, bool nonshared); 5161b40f8c0SMatthew Dillon void vm_page_flash(vm_page_t m); 5171b40f8c0SMatthew Dillon void vm_page_free(vm_page_t m); 5181b40f8c0SMatthew Dillon void vm_page_free_zero(vm_page_t m); 5191b40f8c0SMatthew Dillon 5201b40f8c0SMatthew Dillon void vm_page_activate (vm_page_t); 5212051980fSAlan Cox void vm_page_advise(vm_page_t m, int advice); 5221b40f8c0SMatthew Dillon vm_page_t vm_page_alloc(vm_object_t, vm_pindex_t, int); 523ef435ae7SJeff Roberson vm_page_t vm_page_alloc_domain(vm_object_t, vm_pindex_t, int, int); 52433fff5d5SMark Johnston vm_page_t vm_page_alloc_after(vm_object_t, vm_pindex_t, int, vm_page_t); 525ef435ae7SJeff Roberson vm_page_t vm_page_alloc_domain_after(vm_object_t, vm_pindex_t, int, int, 526ef435ae7SJeff Roberson vm_page_t); 527fbd80bd0SAlan Cox vm_page_t vm_page_alloc_contig(vm_object_t object, vm_pindex_t pindex, int req, 528fbd80bd0SAlan Cox u_long npages, vm_paddr_t low, vm_paddr_t high, u_long alignment, 529fbd80bd0SAlan Cox vm_paddr_t boundary, vm_memattr_t memattr); 530ef435ae7SJeff Roberson vm_page_t vm_page_alloc_contig_domain(vm_object_t object, 531ef435ae7SJeff Roberson vm_pindex_t pindex, int domain, int req, u_long npages, vm_paddr_t low, 532ef435ae7SJeff Roberson vm_paddr_t high, u_long alignment, vm_paddr_t boundary, 533ef435ae7SJeff Roberson vm_memattr_t memattr); 534aa546366SJayachandran C. vm_page_t vm_page_alloc_freelist(int, int); 535ef435ae7SJeff Roberson vm_page_t vm_page_alloc_freelist_domain(int, int, int); 536e55d32b7SKonstantin Belousov bool vm_page_blacklist_add(vm_paddr_t pa, bool verbose); 53793c5d3a4SKonstantin Belousov void vm_page_change_lock(vm_page_t m, struct mtx **mtx); 5381b40f8c0SMatthew Dillon vm_page_t vm_page_grab (vm_object_t, vm_pindex_t, int); 5399df950b3SMark Johnston int vm_page_grab_pages(vm_object_t object, vm_pindex_t pindex, int allocflags, 5405471caf6SAlan Cox vm_page_t *ma, int count); 5411b40f8c0SMatthew Dillon void vm_page_deactivate(vm_page_t); 5423138cd36SMark Johnston void vm_page_deactivate_noreuse(vm_page_t); 5438d220203SAlan Cox void vm_page_dequeue(vm_page_t m); 5445cd29d0fSMark Johnston void vm_page_dequeue_deferred(vm_page_t m); 5455cd29d0fSMark Johnston void vm_page_drain_pqbatch(void); 546b382c10aSKonstantin Belousov vm_page_t vm_page_find_least(vm_object_t, vm_pindex_t); 5475cd29d0fSMark Johnston bool vm_page_free_prep(vm_page_t m); 54810cf2560SAlan Cox vm_page_t vm_page_getfake(vm_paddr_t paddr, vm_memattr_t memattr); 549e461aae7SKonstantin Belousov void vm_page_initfake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr); 550e946b949SAttilio Rao int vm_page_insert (vm_page_t, vm_object_t, vm_pindex_t); 551ebcddc72SAlan Cox void vm_page_launder(vm_page_t m); 5521b40f8c0SMatthew Dillon vm_page_t vm_page_lookup (vm_object_t, vm_pindex_t); 55391b4f427SAlan Cox vm_page_t vm_page_next(vm_page_t m); 5542965a453SKip Macy int vm_page_pa_tryrelock(pmap_t, vm_paddr_t, vm_paddr_t *); 555449c2e92SKonstantin Belousov struct vm_pagequeue *vm_page_pagequeue(vm_page_t m); 55691b4f427SAlan Cox vm_page_t vm_page_prev(vm_page_t m); 55788302601SAlan Cox bool vm_page_ps_test(vm_page_t m, int flags, vm_page_t skip_m); 55810cf2560SAlan Cox void vm_page_putfake(vm_page_t m); 559b6c00483SKonstantin Belousov void vm_page_readahead_finish(vm_page_t m); 560c869e672SAlan Cox bool vm_page_reclaim_contig(int req, u_long npages, vm_paddr_t low, 561c869e672SAlan Cox vm_paddr_t high, u_long alignment, vm_paddr_t boundary); 5623f289c3fSJeff Roberson bool vm_page_reclaim_contig_domain(int domain, int req, u_long npages, 563ef435ae7SJeff Roberson vm_paddr_t low, vm_paddr_t high, u_long alignment, vm_paddr_t boundary); 5643407fefeSKonstantin Belousov void vm_page_reference(vm_page_t m); 565*98549e2dSMark Johnston #define VPR_TRYFREE 0x01 566*98549e2dSMark Johnston #define VPR_NOREUSE 0x02 567*98549e2dSMark Johnston void vm_page_release(vm_page_t m, int flags); 568*98549e2dSMark Johnston void vm_page_release_locked(vm_page_t m, int flags); 5690fd977b3SMark Johnston bool vm_page_remove(vm_page_t); 570e946b949SAttilio Rao int vm_page_rename(vm_page_t, vm_object_t, vm_pindex_t); 571e946b949SAttilio Rao vm_page_t vm_page_replace(vm_page_t mnew, vm_object_t object, 572e946b949SAttilio Rao vm_pindex_t pindex); 5738d220203SAlan Cox void vm_page_requeue(vm_page_t m); 574c7aebda8SAttilio Rao int vm_page_sbusied(vm_page_t m); 575c869e672SAlan Cox vm_page_t vm_page_scan_contig(u_long npages, vm_page_t m_start, 576c869e672SAlan Cox vm_page_t m_end, u_long alignment, vm_paddr_t boundary, int options); 577dc874f98SKonstantin Belousov void vm_page_set_valid_range(vm_page_t m, int base, int size); 578c7aebda8SAttilio Rao int vm_page_sleep_if_busy(vm_page_t m, const char *msg); 579889eb0fcSAlan Cox vm_offset_t vm_page_startup(vm_offset_t vaddr); 580c7aebda8SAttilio Rao void vm_page_sunbusy(vm_page_t m); 581c7aebda8SAttilio Rao int vm_page_trysbusy(vm_page_t m); 5828c22654dSAlan Cox void vm_page_unhold_pages(vm_page_t *ma, int count); 583b1fd102eSMark Johnston void vm_page_unswappable(vm_page_t m); 5841d3a1bcfSMark Johnston bool vm_page_unwire(vm_page_t m, uint8_t queue); 5851d3a1bcfSMark Johnston bool vm_page_unwire_noq(vm_page_t m); 58610cf2560SAlan Cox void vm_page_updatefake(vm_page_t m, vm_paddr_t paddr, vm_memattr_t memattr); 5871b40f8c0SMatthew Dillon void vm_page_wire (vm_page_t); 588c7aebda8SAttilio Rao void vm_page_xunbusy_hard(vm_page_t m); 589bd9546a2SKonstantin Belousov void vm_page_xunbusy_maybelocked(vm_page_t m); 5901b40f8c0SMatthew Dillon void vm_page_set_validclean (vm_page_t, int, int); 5911b40f8c0SMatthew Dillon void vm_page_clear_dirty (vm_page_t, int, int); 5921b40f8c0SMatthew Dillon void vm_page_set_invalid (vm_page_t, int, int); 5931b40f8c0SMatthew Dillon int vm_page_is_valid (vm_page_t, int, int); 5941b40f8c0SMatthew Dillon void vm_page_test_dirty (vm_page_t); 595561cc9fcSKonstantin Belousov vm_page_bits_t vm_page_bits(int base, int size); 5968d17e694SJulian Elischer void vm_page_zero_invalid(vm_page_t m, boolean_t setvalid); 597faa273d5SMatthew Dillon void vm_page_free_toq(vm_page_t m); 5988c8ee2eeSKonstantin Belousov void vm_page_free_pages_toq(struct spglist *free, bool update_wire_count); 59998cb733cSKenneth D. Merry 600eddc9291SAlan Cox void vm_page_dirty_KBI(vm_page_t m); 601cf1911a9SKonstantin Belousov void vm_page_lock_KBI(vm_page_t m, const char *file, int line); 602cf1911a9SKonstantin Belousov void vm_page_unlock_KBI(vm_page_t m, const char *file, int line); 603cf1911a9SKonstantin Belousov int vm_page_trylock_KBI(vm_page_t m, const char *file, int line); 604cf1911a9SKonstantin Belousov #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT) 605b4171812SAlan Cox void vm_page_assert_locked_KBI(vm_page_t m, const char *file, int line); 606cf1911a9SKonstantin Belousov void vm_page_lock_assert_KBI(vm_page_t m, int a, const char *file, int line); 607cf1911a9SKonstantin Belousov #endif 608cf1911a9SKonstantin Belousov 609c7aebda8SAttilio Rao #define vm_page_assert_sbusied(m) \ 610c7aebda8SAttilio Rao KASSERT(vm_page_sbusied(m), \ 611c7aebda8SAttilio Rao ("vm_page_assert_sbusied: page %p not shared busy @ %s:%d", \ 612dc62d559SConrad Meyer (m), __FILE__, __LINE__)) 613c7aebda8SAttilio Rao 614c7aebda8SAttilio Rao #define vm_page_assert_unbusied(m) \ 615c7aebda8SAttilio Rao KASSERT(!vm_page_busied(m), \ 616c7aebda8SAttilio Rao ("vm_page_assert_unbusied: page %p busy @ %s:%d", \ 617dc62d559SConrad Meyer (m), __FILE__, __LINE__)) 618c7aebda8SAttilio Rao 619c7aebda8SAttilio Rao #define vm_page_assert_xbusied(m) \ 620c7aebda8SAttilio Rao KASSERT(vm_page_xbusied(m), \ 621c7aebda8SAttilio Rao ("vm_page_assert_xbusied: page %p not exclusive busy @ %s:%d", \ 622dc62d559SConrad Meyer (m), __FILE__, __LINE__)) 623c7aebda8SAttilio Rao 624c7aebda8SAttilio Rao #define vm_page_busied(m) \ 625c7aebda8SAttilio Rao ((m)->busy_lock != VPB_UNBUSIED) 626c7aebda8SAttilio Rao 627c7aebda8SAttilio Rao #define vm_page_sbusy(m) do { \ 628c7aebda8SAttilio Rao if (!vm_page_trysbusy(m)) \ 629dc62d559SConrad Meyer panic("%s: page %p failed shared busying", __func__, \ 630dc62d559SConrad Meyer (m)); \ 631c7aebda8SAttilio Rao } while (0) 632c7aebda8SAttilio Rao 633c7aebda8SAttilio Rao #define vm_page_tryxbusy(m) \ 634dc62d559SConrad Meyer (atomic_cmpset_acq_int(&(m)->busy_lock, VPB_UNBUSIED, \ 635c7aebda8SAttilio Rao VPB_SINGLE_EXCLUSIVER)) 636c7aebda8SAttilio Rao 637c7aebda8SAttilio Rao #define vm_page_xbusied(m) \ 638dc62d559SConrad Meyer (((m)->busy_lock & VPB_SINGLE_EXCLUSIVER) != 0) 639c7aebda8SAttilio Rao 640c7aebda8SAttilio Rao #define vm_page_xbusy(m) do { \ 641c7aebda8SAttilio Rao if (!vm_page_tryxbusy(m)) \ 642dc62d559SConrad Meyer panic("%s: page %p failed exclusive busying", __func__, \ 643dc62d559SConrad Meyer (m)); \ 644c7aebda8SAttilio Rao } while (0) 645c7aebda8SAttilio Rao 646505cd5d1SKonstantin Belousov /* Note: page m's lock must not be owned by the caller. */ 647c7aebda8SAttilio Rao #define vm_page_xunbusy(m) do { \ 648c7aebda8SAttilio Rao if (!atomic_cmpset_rel_int(&(m)->busy_lock, \ 649c7aebda8SAttilio Rao VPB_SINGLE_EXCLUSIVER, VPB_UNBUSIED)) \ 650c7aebda8SAttilio Rao vm_page_xunbusy_hard(m); \ 651c7aebda8SAttilio Rao } while (0) 652c7aebda8SAttilio Rao 6533b1025d2SKonstantin Belousov #ifdef INVARIANTS 6543b1025d2SKonstantin Belousov void vm_page_object_lock_assert(vm_page_t m); 6553b1025d2SKonstantin Belousov #define VM_PAGE_OBJECT_LOCK_ASSERT(m) vm_page_object_lock_assert(m) 656afb69e6bSKonstantin Belousov void vm_page_assert_pga_writeable(vm_page_t m, uint8_t bits); 657afb69e6bSKonstantin Belousov #define VM_PAGE_ASSERT_PGA_WRITEABLE(m, bits) \ 658afb69e6bSKonstantin Belousov vm_page_assert_pga_writeable(m, bits) 6593b1025d2SKonstantin Belousov #else 6603b1025d2SKonstantin Belousov #define VM_PAGE_OBJECT_LOCK_ASSERT(m) (void)0 661afb69e6bSKonstantin Belousov #define VM_PAGE_ASSERT_PGA_WRITEABLE(m, bits) (void)0 6623b1025d2SKonstantin Belousov #endif 6633b1025d2SKonstantin Belousov 664f3b676f0SAlan Cox /* 665369763e3SAlan Cox * We want to use atomic updates for the aflags field, which is 8 bits wide. 666369763e3SAlan Cox * However, not all architectures support atomic operations on 8-bit 667369763e3SAlan Cox * destinations. In order that we can easily use a 32-bit operation, we 668369763e3SAlan Cox * require that the aflags field be 32-bit aligned. 669369763e3SAlan Cox */ 670369763e3SAlan Cox CTASSERT(offsetof(struct vm_page, aflags) % sizeof(uint32_t) == 0); 671369763e3SAlan Cox 672369763e3SAlan Cox /* 673369763e3SAlan Cox * Clear the given bits in the specified page. 674369763e3SAlan Cox */ 675369763e3SAlan Cox static inline void 676369763e3SAlan Cox vm_page_aflag_clear(vm_page_t m, uint8_t bits) 677369763e3SAlan Cox { 678369763e3SAlan Cox uint32_t *addr, val; 679369763e3SAlan Cox 680369763e3SAlan Cox /* 681b4171812SAlan Cox * The PGA_REFERENCED flag can only be cleared if the page is locked. 682369763e3SAlan Cox */ 683369763e3SAlan Cox if ((bits & PGA_REFERENCED) != 0) 684b4171812SAlan Cox vm_page_assert_locked(m); 685369763e3SAlan Cox 686369763e3SAlan Cox /* 687369763e3SAlan Cox * Access the whole 32-bit word containing the aflags field with an 688369763e3SAlan Cox * atomic update. Parallel non-atomic updates to the other fields 689369763e3SAlan Cox * within this word are handled properly by the atomic update. 690369763e3SAlan Cox */ 691369763e3SAlan Cox addr = (void *)&m->aflags; 692369763e3SAlan Cox KASSERT(((uintptr_t)addr & (sizeof(uint32_t) - 1)) == 0, 693369763e3SAlan Cox ("vm_page_aflag_clear: aflags is misaligned")); 694369763e3SAlan Cox val = bits; 695369763e3SAlan Cox #if BYTE_ORDER == BIG_ENDIAN 696369763e3SAlan Cox val <<= 24; 697369763e3SAlan Cox #endif 698369763e3SAlan Cox atomic_clear_32(addr, val); 699369763e3SAlan Cox } 700369763e3SAlan Cox 701369763e3SAlan Cox /* 702369763e3SAlan Cox * Set the given bits in the specified page. 703369763e3SAlan Cox */ 704369763e3SAlan Cox static inline void 705369763e3SAlan Cox vm_page_aflag_set(vm_page_t m, uint8_t bits) 706369763e3SAlan Cox { 707369763e3SAlan Cox uint32_t *addr, val; 708369763e3SAlan Cox 709afb69e6bSKonstantin Belousov VM_PAGE_ASSERT_PGA_WRITEABLE(m, bits); 710369763e3SAlan Cox 711369763e3SAlan Cox /* 712369763e3SAlan Cox * Access the whole 32-bit word containing the aflags field with an 713369763e3SAlan Cox * atomic update. Parallel non-atomic updates to the other fields 714369763e3SAlan Cox * within this word are handled properly by the atomic update. 715369763e3SAlan Cox */ 716369763e3SAlan Cox addr = (void *)&m->aflags; 717369763e3SAlan Cox KASSERT(((uintptr_t)addr & (sizeof(uint32_t) - 1)) == 0, 718369763e3SAlan Cox ("vm_page_aflag_set: aflags is misaligned")); 719369763e3SAlan Cox val = bits; 720369763e3SAlan Cox #if BYTE_ORDER == BIG_ENDIAN 721369763e3SAlan Cox val <<= 24; 722369763e3SAlan Cox #endif 723369763e3SAlan Cox atomic_set_32(addr, val); 724369763e3SAlan Cox } 725369763e3SAlan Cox 726369763e3SAlan Cox /* 727eddc9291SAlan Cox * vm_page_dirty: 728eddc9291SAlan Cox * 729eddc9291SAlan Cox * Set all bits in the page's dirty field. 730eddc9291SAlan Cox * 731eddc9291SAlan Cox * The object containing the specified page must be locked if the 732eddc9291SAlan Cox * call is made from the machine-independent layer. 733eddc9291SAlan Cox * 734eddc9291SAlan Cox * See vm_page_clear_dirty_mask(). 735eddc9291SAlan Cox */ 736eddc9291SAlan Cox static __inline void 737eddc9291SAlan Cox vm_page_dirty(vm_page_t m) 738eddc9291SAlan Cox { 739eddc9291SAlan Cox 740eddc9291SAlan Cox /* Use vm_page_dirty_KBI() under INVARIANTS to save memory. */ 741f4b36404SMatt Macy #if (defined(KLD_MODULE) && !defined(KLD_TIED)) || defined(INVARIANTS) 742eddc9291SAlan Cox vm_page_dirty_KBI(m); 743eddc9291SAlan Cox #else 744eddc9291SAlan Cox m->dirty = VM_PAGE_BITS_ALL; 745eddc9291SAlan Cox #endif 746eddc9291SAlan Cox } 747eddc9291SAlan Cox 748eddc9291SAlan Cox /* 749f3b676f0SAlan Cox * vm_page_undirty: 750f3b676f0SAlan Cox * 751f3b676f0SAlan Cox * Set page to not be dirty. Note: does not clear pmap modify bits 752f3b676f0SAlan Cox */ 753f3b676f0SAlan Cox static __inline void 754f3b676f0SAlan Cox vm_page_undirty(vm_page_t m) 755f3b676f0SAlan Cox { 7563b1025d2SKonstantin Belousov 7573b1025d2SKonstantin Belousov VM_PAGE_OBJECT_LOCK_ASSERT(m); 758f3b676f0SAlan Cox m->dirty = 0; 759f3b676f0SAlan Cox } 760f3b676f0SAlan Cox 7618170d6e5SConrad Meyer static inline void 7628170d6e5SConrad Meyer vm_page_replace_checked(vm_page_t mnew, vm_object_t object, vm_pindex_t pindex, 7638170d6e5SConrad Meyer vm_page_t mold) 7648170d6e5SConrad Meyer { 7658170d6e5SConrad Meyer vm_page_t mret; 7668170d6e5SConrad Meyer 7678170d6e5SConrad Meyer mret = vm_page_replace(mnew, object, pindex); 7688170d6e5SConrad Meyer KASSERT(mret == mold, 7698170d6e5SConrad Meyer ("invalid page replacement, mold=%p, mret=%p", mold, mret)); 7708170d6e5SConrad Meyer 7718170d6e5SConrad Meyer /* Unused if !INVARIANTS. */ 7728170d6e5SConrad Meyer (void)mold; 7738170d6e5SConrad Meyer (void)mret; 7748170d6e5SConrad Meyer } 7758170d6e5SConrad Meyer 7761b5c869dSMark Johnston /* 7771b5c869dSMark Johnston * vm_page_queue: 7781b5c869dSMark Johnston * 7791b5c869dSMark Johnston * Return the index of the queue containing m. This index is guaranteed 7801b5c869dSMark Johnston * not to change while the page lock is held. 7811b5c869dSMark Johnston */ 7821b5c869dSMark Johnston static inline uint8_t 7831b5c869dSMark Johnston vm_page_queue(vm_page_t m) 7841b5c869dSMark Johnston { 7851b5c869dSMark Johnston 7861b5c869dSMark Johnston vm_page_assert_locked(m); 7871b5c869dSMark Johnston 7881b5c869dSMark Johnston if ((m->aflags & PGA_DEQUEUE) != 0) 7891b5c869dSMark Johnston return (PQ_NONE); 7901b5c869dSMark Johnston atomic_thread_fence_acq(); 7911b5c869dSMark Johnston return (m->queue); 7921b5c869dSMark Johnston } 7931b5c869dSMark Johnston 794ebcddc72SAlan Cox static inline bool 795ebcddc72SAlan Cox vm_page_active(vm_page_t m) 796ebcddc72SAlan Cox { 797ebcddc72SAlan Cox 7981b5c869dSMark Johnston return (vm_page_queue(m) == PQ_ACTIVE); 799ebcddc72SAlan Cox } 800ebcddc72SAlan Cox 801ebcddc72SAlan Cox static inline bool 802ebcddc72SAlan Cox vm_page_inactive(vm_page_t m) 803ebcddc72SAlan Cox { 804ebcddc72SAlan Cox 8051b5c869dSMark Johnston return (vm_page_queue(m) == PQ_INACTIVE); 806ebcddc72SAlan Cox } 807ebcddc72SAlan Cox 808ebcddc72SAlan Cox static inline bool 809ebcddc72SAlan Cox vm_page_in_laundry(vm_page_t m) 810ebcddc72SAlan Cox { 8111b5c869dSMark Johnston uint8_t queue; 812ebcddc72SAlan Cox 8131b5c869dSMark Johnston queue = vm_page_queue(m); 8141b5c869dSMark Johnston return (queue == PQ_LAUNDRY || queue == PQ_UNSWAPPABLE); 8155cd29d0fSMark Johnston } 8165cd29d0fSMark Johnston 8175cd29d0fSMark Johnston /* 818eeacb3b0SMark Johnston * vm_page_wired: 8191d3a1bcfSMark Johnston * 8201d3a1bcfSMark Johnston * Return true if a reference prevents the page from being reclaimable. 8211d3a1bcfSMark Johnston */ 8221d3a1bcfSMark Johnston static inline bool 823d842aa51SMark Johnston vm_page_wired(vm_page_t m) 824d842aa51SMark Johnston { 825d842aa51SMark Johnston 826d842aa51SMark Johnston return (m->wire_count > 0); 827d842aa51SMark Johnston } 828d842aa51SMark Johnston 829c4473420SPeter Wemm #endif /* _KERNEL */ 830df8bae1dSRodney W. Grimes #endif /* !_VM_PAGE_ */ 831