vm_pager.c revision 41059
1/* 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * The Mach Operating System project at Carnegie-Mellon University. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * from: @(#)vm_pager.c 8.6 (Berkeley) 1/12/94 37 * 38 * 39 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 40 * All rights reserved. 41 * 42 * Authors: Avadis Tevanian, Jr., Michael Wayne Young 43 * 44 * Permission to use, copy, modify and distribute this software and 45 * its documentation is hereby granted, provided that both the copyright 46 * notice and this permission notice appear in all copies of the 47 * software, derivative works or modified versions, and any portions 48 * thereof, and that both notices appear in supporting documentation. 49 * 50 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 51 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 52 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 53 * 54 * Carnegie Mellon requests users of this software to return to 55 * 56 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 57 * School of Computer Science 58 * Carnegie Mellon University 59 * Pittsburgh PA 15213-3890 60 * 61 * any improvements or extensions that they make and grant Carnegie the 62 * rights to redistribute these changes. 63 * 64 * $Id: vm_pager.c,v 1.39 1998/10/31 15:31:29 peter Exp $ 65 */ 66 67/* 68 * Paging space routine stubs. Emulates a matchmaker-like interface 69 * for builtin pagers. 70 */ 71 72#include <sys/param.h> 73#include <sys/systm.h> 74#include <sys/kernel.h> 75#include <sys/buf.h> 76#include <sys/ucred.h> 77#include <sys/malloc.h> 78 79#include <vm/vm.h> 80#include <vm/vm_param.h> 81#include <vm/vm_prot.h> 82#include <vm/vm_object.h> 83#include <vm/vm_page.h> 84#include <vm/vm_pager.h> 85#include <vm/vm_extern.h> 86 87MALLOC_DEFINE(M_VMPGDATA, "VM pgdata", "XXX: VM pager private data"); 88 89extern struct pagerops defaultpagerops; 90extern struct pagerops swappagerops; 91extern struct pagerops vnodepagerops; 92extern struct pagerops devicepagerops; 93 94static int dead_pager_getpages __P((vm_object_t, vm_page_t *, int, int)); 95static vm_object_t dead_pager_alloc __P((void *, vm_ooffset_t, vm_prot_t, 96 vm_ooffset_t)); 97static int dead_pager_putpages __P((vm_object_t, vm_page_t *, int, int, int *)); 98static boolean_t dead_pager_haspage __P((vm_object_t, vm_pindex_t, int *, int *)); 99static void dead_pager_dealloc __P((vm_object_t)); 100 101int 102dead_pager_getpages(obj, ma, count, req) 103 vm_object_t obj; 104 vm_page_t *ma; 105 int count; 106 int req; 107{ 108 return VM_PAGER_FAIL; 109} 110 111vm_object_t 112dead_pager_alloc(handle, size, prot, off) 113 void *handle; 114 vm_ooffset_t size; 115 vm_prot_t prot; 116 vm_ooffset_t off; 117{ 118 return NULL; 119} 120 121int 122dead_pager_putpages(object, m, count, flags, rtvals) 123 vm_object_t object; 124 vm_page_t *m; 125 int count; 126 int flags; 127 int *rtvals; 128{ 129 int i; 130 for (i = 0; i < count; i++) { 131 rtvals[i] = VM_PAGER_AGAIN; 132 } 133 return VM_PAGER_AGAIN; 134} 135 136int 137dead_pager_haspage(object, pindex, prev, next) 138 vm_object_t object; 139 vm_pindex_t pindex; 140 int *prev; 141 int *next; 142{ 143 if (prev) 144 *prev = 0; 145 if (next) 146 *next = 0; 147 return FALSE; 148} 149 150void 151dead_pager_dealloc(object) 152 vm_object_t object; 153{ 154 return; 155} 156 157struct pagerops deadpagerops = { 158 NULL, 159 dead_pager_alloc, 160 dead_pager_dealloc, 161 dead_pager_getpages, 162 dead_pager_putpages, 163 dead_pager_haspage, 164 NULL 165}; 166 167static struct pagerops *pagertab[] = { 168 &defaultpagerops, /* OBJT_DEFAULT */ 169 &swappagerops, /* OBJT_SWAP */ 170 &vnodepagerops, /* OBJT_VNODE */ 171 &devicepagerops, /* OBJT_DEVICE */ 172 &deadpagerops /* OBJT_DEAD */ 173}; 174static int npagers = sizeof(pagertab) / sizeof(pagertab[0]); 175 176/* 177 * Kernel address space for mapping pages. 178 * Used by pagers where KVAs are needed for IO. 179 * 180 * XXX needs to be large enough to support the number of pending async 181 * cleaning requests (NPENDINGIO == 64) * the maximum swap cluster size 182 * (MAXPHYS == 64k) if you want to get the most efficiency. 183 */ 184#define PAGER_MAP_SIZE (8 * 1024 * 1024) 185 186int pager_map_size = PAGER_MAP_SIZE; 187vm_map_t pager_map; 188static int bswneeded; 189static vm_offset_t swapbkva; /* swap buffers kva */ 190 191void 192vm_pager_init() 193{ 194 struct pagerops **pgops; 195 196 /* 197 * Initialize known pagers 198 */ 199 for (pgops = pagertab; pgops < &pagertab[npagers]; pgops++) 200 if (pgops && ((*pgops)->pgo_init != NULL)) 201 (*(*pgops)->pgo_init) (); 202} 203 204void 205vm_pager_bufferinit() 206{ 207 struct buf *bp; 208 int i; 209 210 bp = swbuf; 211 /* 212 * Now set up swap and physical I/O buffer headers. 213 */ 214 for (i = 0; i < nswbuf; i++, bp++) { 215 TAILQ_INSERT_HEAD(&bswlist, bp, b_freelist); 216 bp->b_rcred = bp->b_wcred = NOCRED; 217 bp->b_xflags = 0; 218 } 219 220 swapbkva = kmem_alloc_pageable(pager_map, nswbuf * MAXPHYS); 221 if (!swapbkva) 222 panic("Not enough pager_map VM space for physical buffers"); 223} 224 225/* 226 * Allocate an instance of a pager of the given type. 227 * Size, protection and offset parameters are passed in for pagers that 228 * need to perform page-level validation (e.g. the device pager). 229 */ 230vm_object_t 231vm_pager_allocate(objtype_t type, void *handle, vm_ooffset_t size, vm_prot_t prot, 232 vm_ooffset_t off) 233{ 234 struct pagerops *ops; 235 236 ops = pagertab[type]; 237 if (ops) 238 return ((*ops->pgo_alloc) (handle, size, prot, off)); 239 return (NULL); 240} 241 242void 243vm_pager_deallocate(object) 244 vm_object_t object; 245{ 246 (*pagertab[object->type]->pgo_dealloc) (object); 247} 248 249 250int 251vm_pager_get_pages(object, m, count, reqpage) 252 vm_object_t object; 253 vm_page_t *m; 254 int count; 255 int reqpage; 256{ 257 return ((*pagertab[object->type]->pgo_getpages)(object, m, count, reqpage)); 258} 259 260int 261vm_pager_put_pages(object, m, count, flags, rtvals) 262 vm_object_t object; 263 vm_page_t *m; 264 int count; 265 int flags; 266 int *rtvals; 267{ 268 return ((*pagertab[object->type]->pgo_putpages)(object, m, count, flags, rtvals)); 269} 270 271boolean_t 272vm_pager_has_page(object, offset, before, after) 273 vm_object_t object; 274 vm_pindex_t offset; 275 int *before; 276 int *after; 277{ 278 return ((*pagertab[object->type]->pgo_haspage) (object, offset, before, after)); 279} 280 281/* 282 * Called by pageout daemon before going back to sleep. 283 * Gives pagers a chance to clean up any completed async pageing operations. 284 */ 285void 286vm_pager_sync() 287{ 288 struct pagerops **pgops; 289 290 for (pgops = pagertab; pgops < &pagertab[npagers]; pgops++) 291 if (pgops && ((*pgops)->pgo_sync != NULL)) 292 (*(*pgops)->pgo_sync) (); 293} 294 295vm_offset_t 296vm_pager_map_page(m) 297 vm_page_t m; 298{ 299 vm_offset_t kva; 300 301 kva = kmem_alloc_wait(pager_map, PAGE_SIZE); 302 pmap_kenter(kva, VM_PAGE_TO_PHYS(m)); 303 return (kva); 304} 305 306void 307vm_pager_unmap_page(kva) 308 vm_offset_t kva; 309{ 310 pmap_kremove(kva); 311 kmem_free_wakeup(pager_map, kva, PAGE_SIZE); 312} 313 314vm_object_t 315vm_pager_object_lookup(pg_list, handle) 316 register struct pagerlst *pg_list; 317 void *handle; 318{ 319 register vm_object_t object; 320 321 for (object = TAILQ_FIRST(pg_list); object != NULL; object = TAILQ_NEXT(object,pager_object_list)) 322 if (object->handle == handle) 323 return (object); 324 return (NULL); 325} 326 327/* 328 * initialize a physical buffer 329 */ 330 331static void 332initpbuf(struct buf *bp) { 333 bzero(bp, sizeof *bp); 334 bp->b_rcred = NOCRED; 335 bp->b_wcred = NOCRED; 336 bp->b_qindex = QUEUE_NONE; 337 bp->b_data = (caddr_t) (MAXPHYS * (bp - swbuf)) + swapbkva; 338 bp->b_kvabase = bp->b_data; 339 bp->b_kvasize = MAXPHYS; 340 bp->b_xflags = 0; 341} 342 343/* 344 * allocate a physical buffer 345 */ 346struct buf * 347getpbuf() 348{ 349 int s; 350 struct buf *bp; 351 352 s = splvm(); 353 /* get a bp from the swap buffer header pool */ 354 while ((bp = TAILQ_FIRST(&bswlist)) == NULL) { 355 bswneeded = 1; 356 tsleep(&bswneeded, PVM, "wswbuf", 0); 357 } 358 TAILQ_REMOVE(&bswlist, bp, b_freelist); 359 splx(s); 360 361 initpbuf(bp); 362 return bp; 363} 364 365/* 366 * allocate a physical buffer, if one is available 367 */ 368struct buf * 369trypbuf() 370{ 371 int s; 372 struct buf *bp; 373 374 s = splvm(); 375 if ((bp = TAILQ_FIRST(&bswlist)) == NULL) { 376 splx(s); 377 return NULL; 378 } 379 TAILQ_REMOVE(&bswlist, bp, b_freelist); 380 splx(s); 381 382 initpbuf(bp); 383 384 return bp; 385} 386 387/* 388 * release a physical buffer 389 */ 390void 391relpbuf(bp) 392 struct buf *bp; 393{ 394 int s; 395 396 s = splvm(); 397 398 if (bp->b_rcred != NOCRED) { 399 crfree(bp->b_rcred); 400 bp->b_rcred = NOCRED; 401 } 402 if (bp->b_wcred != NOCRED) { 403 crfree(bp->b_wcred); 404 bp->b_wcred = NOCRED; 405 } 406 if (bp->b_vp) 407 pbrelvp(bp); 408 409 if (bp->b_flags & B_WANTED) 410 wakeup(bp); 411 412 TAILQ_INSERT_HEAD(&bswlist, bp, b_freelist); 413 414 if (bswneeded) { 415 bswneeded = 0; 416 wakeup(&bswneeded); 417 } 418 splx(s); 419} 420