uvm_page.h revision 1.37
1/* $OpenBSD: uvm_page.h,v 1.37 2009/06/16 00:11:29 oga Exp $ */ 2/* $NetBSD: uvm_page.h,v 1.19 2000/12/28 08:24:55 chs Exp $ */ 3 4/* 5 * Copyright (c) 1997 Charles D. Cranor and Washington University. 6 * Copyright (c) 1991, 1993, The Regents of the University of California. 7 * 8 * All rights reserved. 9 * 10 * This code is derived from software contributed to Berkeley by 11 * The Mach Operating System project at Carnegie-Mellon University. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by Charles D. Cranor, 24 * Washington University, the University of California, Berkeley and 25 * its contributors. 26 * 4. Neither the name of the University nor the names of its contributors 27 * may be used to endorse or promote products derived from this software 28 * without specific prior written permission. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 40 * SUCH DAMAGE. 41 * 42 * @(#)vm_page.h 7.3 (Berkeley) 4/21/91 43 * from: Id: uvm_page.h,v 1.1.2.6 1998/02/04 02:31:42 chuck Exp 44 * 45 * 46 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 47 * All rights reserved. 48 * 49 * Permission to use, copy, modify and distribute this software and 50 * its documentation is hereby granted, provided that both the copyright 51 * notice and this permission notice appear in all copies of the 52 * software, derivative works or modified versions, and any portions 53 * thereof, and that both notices appear in supporting documentation. 54 * 55 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 56 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 57 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 58 * 59 * Carnegie Mellon requests users of this software to return to 60 * 61 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 62 * School of Computer Science 63 * Carnegie Mellon University 64 * Pittsburgh PA 15213-3890 65 * 66 * any improvements or extensions that they make and grant Carnegie the 67 * rights to redistribute these changes. 68 */ 69 70#ifndef _UVM_UVM_PAGE_H_ 71#define _UVM_UVM_PAGE_H_ 72 73/* 74 * uvm_page.h 75 */ 76 77/* 78 * Resident memory system definitions. 79 */ 80 81/* 82 * Management of resident (logical) pages. 83 * 84 * A small structure is kept for each resident 85 * page, indexed by page number. Each structure 86 * is an element of several lists: 87 * 88 * A hash table bucket used to quickly 89 * perform object/offset lookups 90 * 91 * A list of all pages for a given object, 92 * so they can be quickly deactivated at 93 * time of deallocation. 94 * 95 * An ordered list of pages due for pageout. 96 * 97 * In addition, the structure contains the object 98 * and offset to which this page belongs (for pageout), 99 * and sundry status bits. 100 * 101 * Fields in this structure are locked either by the lock on the 102 * object that the page belongs to (O) or by the lock on the page 103 * queues (P) [or both]. 104 */ 105 106#include <uvm/uvm_extern.h> 107#include <uvm/uvm_pglist.h> 108 109union vm_page_fq { 110 struct { 111 TAILQ_ENTRY(vm_page) hashq; /* hash table links (O)*/ 112 TAILQ_ENTRY(vm_page) listq; /* pages in same object (O)*/ 113 } queues; 114 115 struct { 116 RB_ENTRY(vm_page) tree; /* Free chunks, addr/size */ 117 psize_t pages; 118 } free; 119}; 120 121struct vm_page { 122 union vm_page_fq fq; /* free and queue management */ 123 TAILQ_ENTRY(vm_page) pageq; /* queue info for FIFO 124 * queue or free list (P) */ 125 126 struct vm_anon *uanon; /* anon (O,P) */ 127 struct uvm_object *uobject; /* object (O,P) */ 128 voff_t offset; /* offset into object (O,P) */ 129 130 u_int pg_flags; /* object flags [O or P] */ 131 132 u_int pg_version; /* version count [O] */ 133 u_int wire_count; /* wired down map refs [P] */ 134 135 u_int loan_count; /* number of active loans 136 * to read: [O or P] 137 * to modify: [O _and_ P] */ 138 paddr_t phys_addr; /* physical address of page */ 139 140#ifdef __HAVE_VM_PAGE_MD 141 struct vm_page_md mdpage; /* pmap-specific data */ 142#endif 143#if defined(UVM_PAGE_TRKOWN) 144 /* debugging fields to track page ownership */ 145 pid_t owner; /* proc that set PG_BUSY */ 146 char *owner_tag; /* why it was set busy */ 147#endif 148}; 149 150/* 151 * These are the flags defined for vm_page. 152 * 153 * Note: PG_FILLED and PG_DIRTY are added for the filesystems. 154 */ 155 156/* 157 * locking rules: 158 * PG_ ==> locked by object lock 159 * PQ_ ==> lock by page queue lock 160 * PQ_FREE is locked by free queue lock and is mutex with all other PQs 161 * pg_flags may only be changed using the atomic operations. 162 * 163 * PG_ZERO is used to indicate that a page has been pre-zero'd. This flag 164 * is only set when the page is on no queues, and is cleared when the page 165 * is placed on the free list. 166 */ 167 168#define PG_BUSY 0x00000001 /* page is locked */ 169#define PG_WANTED 0x00000002 /* someone is waiting for page */ 170#define PG_TABLED 0x00000004 /* page is in VP table */ 171#define PG_CLEAN 0x00000008 /* page has not been modified */ 172#define PG_CLEANCHK 0x00000010 /* clean bit has been checked */ 173#define PG_RELEASED 0x00000020 /* page released while paging */ 174#define PG_FAKE 0x00000040 /* page is not yet initialized */ 175#define PG_RDONLY 0x00000080 /* page must be mapped read-only */ 176#define PG_ZERO 0x00000100 /* page is pre-zero'd */ 177 178#define PG_PAGER1 0x00001000 /* pager-specific flag */ 179#define PG_MASK 0x0000ffff 180 181#define PQ_FREE 0x00010000 /* page is on free list */ 182#define PQ_INACTIVE 0x00020000 /* page is in inactive list */ 183#define PQ_ACTIVE 0x00040000 /* page is in active list */ 184#define PQ_ANON 0x00100000 /* page is part of an anon, rather 185 than an uvm_object */ 186#define PQ_AOBJ 0x00200000 /* page is part of an anonymous 187 uvm_object */ 188#define PQ_SWAPBACKED (PQ_ANON|PQ_AOBJ) 189#define PQ_ENCRYPT 0x00400000 /* page needs {en,de}cryption */ 190#define PQ_MASK 0x00ff0000 191 192#define PG_PMAP0 0x01000000 /* Used by some pmaps. */ 193#define PG_PMAP1 0x02000000 /* Used by some pmaps. */ 194#define PG_PMAP2 0x04000000 /* Used by some pmaps. */ 195#define PG_PMAP3 0x08000000 /* Used by some pmaps. */ 196 197/* 198 * physical memory layout structure 199 * 200 * MD vmparam.h must #define: 201 * VM_PHYSEG_MAX = max number of physical memory segments we support 202 * (if this is "1" then we revert to a "contig" case) 203 * VM_PHYSSEG_STRAT: memory sort/search options (for VM_PHYSEG_MAX > 1) 204 * - VM_PSTRAT_RANDOM: linear search (random order) 205 * - VM_PSTRAT_BSEARCH: binary search (sorted by address) 206 * - VM_PSTRAT_BIGFIRST: linear search (sorted by largest segment first) 207 * - others? 208 * XXXCDC: eventually we should purge all left-over global variables... 209 */ 210#define VM_PSTRAT_RANDOM 1 211#define VM_PSTRAT_BSEARCH 2 212#define VM_PSTRAT_BIGFIRST 3 213 214/* 215 * vm_physmemseg: describes one segment of physical memory 216 */ 217struct vm_physseg { 218 paddr_t start; /* PF# of first page in segment */ 219 paddr_t end; /* (PF# of last page in segment) + 1 */ 220 paddr_t avail_start; /* PF# of first free page in segment */ 221 paddr_t avail_end; /* (PF# of last free page in segment) +1 */ 222 int free_list; /* which free list they belong on */ 223 struct vm_page *pgs; /* vm_page structures (from start) */ 224 struct vm_page *lastpg; /* vm_page structure for end */ 225#ifdef __HAVE_PMAP_PHYSSEG 226 struct pmap_physseg pmseg; /* pmap specific (MD) data */ 227#endif 228}; 229 230#ifdef _KERNEL 231 232/* 233 * globals 234 */ 235 236extern boolean_t vm_page_zero_enable; 237 238/* 239 * physical memory config is stored in vm_physmem. 240 */ 241 242extern struct vm_physseg vm_physmem[VM_PHYSSEG_MAX]; 243extern int vm_nphysseg; 244 245/* 246 * prototypes: the following prototypes define the interface to pages 247 */ 248 249void uvm_page_init(vaddr_t *, vaddr_t *); 250#if defined(UVM_PAGE_TRKOWN) 251void uvm_page_own(struct vm_page *, char *); 252#endif 253#if !defined(PMAP_STEAL_MEMORY) 254boolean_t uvm_page_physget(paddr_t *); 255#endif 256void uvm_page_rehash(void); 257void uvm_pageidlezero(void); 258 259void uvm_pageactivate(struct vm_page *); 260vaddr_t uvm_pageboot_alloc(vsize_t); 261void uvm_pagecopy(struct vm_page *, struct vm_page *); 262void uvm_pagedeactivate(struct vm_page *); 263void uvm_pagefree(struct vm_page *); 264void uvm_page_unbusy(struct vm_page **, int); 265struct vm_page *uvm_pagelookup(struct uvm_object *, voff_t); 266void uvm_pageunwire(struct vm_page *); 267void uvm_pagewait(struct vm_page *, int); 268void uvm_pagewake(struct vm_page *); 269void uvm_pagewire(struct vm_page *); 270void uvm_pagezero(struct vm_page *); 271 272int uvm_page_lookup_freelist(struct vm_page *); 273 274#if VM_PHYSSEG_MAX == 1 275/* 276 * Inline functions for archs like the vax where function calls are expensive. 277 */ 278/* 279 * vm_physseg_find: find vm_physseg structure that belongs to a PA 280 */ 281static __inline int 282vm_physseg_find(paddr_t pframe, int *offp) 283{ 284 /* 'contig' case */ 285 if (pframe >= vm_physmem[0].start && pframe < vm_physmem[0].end) { 286 if (offp) 287 *offp = pframe - vm_physmem[0].start; 288 return(0); 289 } 290 return(-1); 291} 292 293/* 294 * PHYS_TO_VM_PAGE: find vm_page for a PA. used by MI code to get vm_pages 295 * back from an I/O mapping (ugh!). used in some MD code as well. 296 */ 297static __inline struct vm_page * 298PHYS_TO_VM_PAGE(paddr_t pa) 299{ 300 paddr_t pf = atop(pa); 301 int off; 302 int psi; 303 304 psi = vm_physseg_find(pf, &off); 305 306 return ((psi == -1) ? NULL : &vm_physmem[psi].pgs[off]); 307} 308#else 309/* if VM_PHYSSEG_MAX > 1 they're not inline, they're in uvm_page.c. */ 310struct vm_page *PHYS_TO_VM_PAGE(paddr_t); 311int vm_physseg_find(paddr_t, int *); 312#endif 313 314/* 315 * macros 316 */ 317 318#define uvm_lock_pageq() simple_lock(&uvm.pageqlock) 319#define uvm_unlock_pageq() simple_unlock(&uvm.pageqlock) 320#define uvm_lock_fpageq() mtx_enter(&uvm.fpageqlock); 321#define uvm_unlock_fpageq() mtx_leave(&uvm.fpageqlock); 322 323#define uvm_pagehash(obj,off) \ 324 (((unsigned long)obj+(unsigned long)atop(off)) & uvm.page_hashmask) 325 326#define UVM_PAGEZERO_TARGET (uvmexp.free) 327 328#define VM_PAGE_TO_PHYS(entry) ((entry)->phys_addr) 329 330#define VM_PAGE_IS_FREE(entry) ((entry)->pg_flags & PQ_FREE) 331 332#endif /* _KERNEL */ 333 334#endif /* _UVM_UVM_PAGE_H_ */ 335