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.38 1998/10/13 08:24:44 dg 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/buf.h> 75 #include <sys/ucred.h> 76 #include <sys/malloc.h> 77 78 #include <vm/vm.h> 79 #include <vm/vm_param.h> 80 #include <vm/vm_prot.h> 81 #include <vm/vm_object.h> 82 #include <vm/vm_page.h> 83 #include <vm/vm_pager.h> 84 #include <vm/vm_extern.h> 85 86 MALLOC_DEFINE(M_VMPGDATA, "VM pgdata", "XXX: VM pager private data"); 87 88 extern struct pagerops defaultpagerops; 89 extern struct pagerops swappagerops; 90 extern struct pagerops vnodepagerops; 91 extern struct pagerops devicepagerops; 92 93 static int dead_pager_getpages __P((vm_object_t, vm_page_t *, int, int)); 94 static vm_object_t dead_pager_alloc __P((void *, vm_ooffset_t, vm_prot_t, 95 vm_ooffset_t)); 96 static int dead_pager_putpages __P((vm_object_t, vm_page_t *, int, int, int *)); 97 static boolean_t dead_pager_haspage __P((vm_object_t, vm_pindex_t, int *, int *)); 98 static void dead_pager_dealloc __P((vm_object_t)); 99 100 int 101 dead_pager_getpages(obj, ma, count, req) 102 vm_object_t obj; 103 vm_page_t *ma; 104 int count; 105 int req; 106 { 107 return VM_PAGER_FAIL; 108 } 109 110 vm_object_t 111 dead_pager_alloc(handle, size, prot, off) 112 void *handle; 113 vm_ooffset_t size; 114 vm_prot_t prot; 115 vm_ooffset_t off; 116 { 117 return NULL; 118 } 119 120 int 121 dead_pager_putpages(object, m, count, flags, rtvals) 122 vm_object_t object; 123 vm_page_t *m; 124 int count; 125 int flags; 126 int *rtvals; 127 { 128 int i; 129 for (i = 0; i < count; i++) { 130 rtvals[i] = VM_PAGER_AGAIN; 131 } 132 return VM_PAGER_AGAIN; 133 } 134 135 int 136 dead_pager_haspage(object, pindex, prev, next) 137 vm_object_t object; 138 vm_pindex_t pindex; 139 int *prev; 140 int *next; 141 { 142 if (prev) 143 *prev = 0; 144 if (next) 145 *next = 0; 146 return FALSE; 147 } 148 149 void 150 dead_pager_dealloc(object) 151 vm_object_t object; 152 { 153 return; 154 } 155 156 struct pagerops deadpagerops = { 157 NULL, 158 dead_pager_alloc, 159 dead_pager_dealloc, 160 dead_pager_getpages, 161 dead_pager_putpages, 162 dead_pager_haspage, 163 NULL 164 }; 165 166 static struct pagerops *pagertab[] = { 167 &defaultpagerops, /* OBJT_DEFAULT */ 168 &swappagerops, /* OBJT_SWAP */ 169 &vnodepagerops, /* OBJT_VNODE */ 170 &devicepagerops, /* OBJT_DEVICE */ 171 &deadpagerops /* OBJT_DEAD */ 172 }; 173 static int npagers = sizeof(pagertab) / sizeof(pagertab[0]); 174 175 /* 176 * Kernel address space for mapping pages. 177 * Used by pagers where KVAs are needed for IO. 178 * 179 * XXX needs to be large enough to support the number of pending async 180 * cleaning requests (NPENDINGIO == 64) * the maximum swap cluster size 181 * (MAXPHYS == 64k) if you want to get the most efficiency. 182 */ 183 #define PAGER_MAP_SIZE (8 * 1024 * 1024) 184 185 int pager_map_size = PAGER_MAP_SIZE; 186 vm_map_t pager_map; 187 static int bswneeded; 188 static vm_offset_t swapbkva; /* swap buffers kva */ 189 190 void 191 vm_pager_init() 192 { 193 struct pagerops **pgops; 194 195 /* 196 * Initialize known pagers 197 */ 198 for (pgops = pagertab; pgops < &pagertab[npagers]; pgops++) 199 if (pgops && ((*pgops)->pgo_init != NULL)) 200 (*(*pgops)->pgo_init) (); 201 } 202 203 void 204 vm_pager_bufferinit() 205 { 206 struct buf *bp; 207 int i; 208 209 bp = swbuf; 210 /* 211 * Now set up swap and physical I/O buffer headers. 212 */ 213 for (i = 0; i < nswbuf; i++, bp++) { 214 TAILQ_INSERT_HEAD(&bswlist, bp, b_freelist); 215 bp->b_rcred = bp->b_wcred = NOCRED; 216 bp->b_xflags = 0; 217 } 218 219 swapbkva = kmem_alloc_pageable(pager_map, nswbuf * MAXPHYS); 220 if (!swapbkva) 221 panic("Not enough pager_map VM space for physical buffers"); 222 } 223 224 /* 225 * Allocate an instance of a pager of the given type. 226 * Size, protection and offset parameters are passed in for pagers that 227 * need to perform page-level validation (e.g. the device pager). 228 */ 229 vm_object_t 230 vm_pager_allocate(objtype_t type, void *handle, vm_ooffset_t size, vm_prot_t prot, 231 vm_ooffset_t off) 232 { 233 struct pagerops *ops; 234 235 ops = pagertab[type]; 236 if (ops) 237 return ((*ops->pgo_alloc) (handle, size, prot, off)); 238 return (NULL); 239 } 240 241 void 242 vm_pager_deallocate(object) 243 vm_object_t object; 244 { 245 (*pagertab[object->type]->pgo_dealloc) (object); 246 } 247 248 249 int 250 vm_pager_get_pages(object, m, count, reqpage) 251 vm_object_t object; 252 vm_page_t *m; 253 int count; 254 int reqpage; 255 { 256 return ((*pagertab[object->type]->pgo_getpages)(object, m, count, reqpage)); 257 } 258 259 int 260 vm_pager_put_pages(object, m, count, flags, rtvals) 261 vm_object_t object; 262 vm_page_t *m; 263 int count; 264 int flags; 265 int *rtvals; 266 { 267 return ((*pagertab[object->type]->pgo_putpages)(object, m, count, flags, rtvals)); 268 } 269 270 boolean_t 271 vm_pager_has_page(object, offset, before, after) 272 vm_object_t object; 273 vm_pindex_t offset; 274 int *before; 275 int *after; 276 { 277 return ((*pagertab[object->type]->pgo_haspage) (object, offset, before, after)); 278 } 279 280 /* 281 * Called by pageout daemon before going back to sleep. 282 * Gives pagers a chance to clean up any completed async pageing operations. 283 */ 284 void 285 vm_pager_sync() 286 { 287 struct pagerops **pgops; 288 289 for (pgops = pagertab; pgops < &pagertab[npagers]; pgops++) 290 if (pgops && ((*pgops)->pgo_sync != NULL)) 291 (*(*pgops)->pgo_sync) (); 292 } 293 294 vm_offset_t 295 vm_pager_map_page(m) 296 vm_page_t m; 297 { 298 vm_offset_t kva; 299 300 kva = kmem_alloc_wait(pager_map, PAGE_SIZE); 301 pmap_kenter(kva, VM_PAGE_TO_PHYS(m)); 302 return (kva); 303 } 304 305 void 306 vm_pager_unmap_page(kva) 307 vm_offset_t kva; 308 { 309 pmap_kremove(kva); 310 kmem_free_wakeup(pager_map, kva, PAGE_SIZE); 311 } 312 313 vm_object_t 314 vm_pager_object_lookup(pg_list, handle) 315 register struct pagerlst *pg_list; 316 void *handle; 317 { 318 register vm_object_t object; 319 320 for (object = TAILQ_FIRST(pg_list); object != NULL; object = TAILQ_NEXT(object,pager_object_list)) 321 if (object->handle == handle) 322 return (object); 323 return (NULL); 324 } 325 326 /* 327 * initialize a physical buffer 328 */ 329 330 static void 331 initpbuf(struct buf *bp) { 332 bzero(bp, sizeof *bp); 333 bp->b_rcred = NOCRED; 334 bp->b_wcred = NOCRED; 335 bp->b_qindex = QUEUE_NONE; 336 bp->b_data = (caddr_t) (MAXPHYS * (bp - swbuf)) + swapbkva; 337 bp->b_kvabase = bp->b_data; 338 bp->b_kvasize = MAXPHYS; 339 bp->b_xflags = 0; 340 } 341 342 /* 343 * allocate a physical buffer 344 */ 345 struct buf * 346 getpbuf() 347 { 348 int s; 349 struct buf *bp; 350 351 s = splvm(); 352 /* get a bp from the swap buffer header pool */ 353 while ((bp = TAILQ_FIRST(&bswlist)) == NULL) { 354 bswneeded = 1; 355 tsleep(&bswneeded, PVM, "wswbuf", 0); 356 } 357 TAILQ_REMOVE(&bswlist, bp, b_freelist); 358 splx(s); 359 360 initpbuf(bp); 361 return bp; 362 } 363 364 /* 365 * allocate a physical buffer, if one is available 366 */ 367 struct buf * 368 trypbuf() 369 { 370 int s; 371 struct buf *bp; 372 373 s = splvm(); 374 if ((bp = TAILQ_FIRST(&bswlist)) == NULL) { 375 splx(s); 376 return NULL; 377 } 378 TAILQ_REMOVE(&bswlist, bp, b_freelist); 379 splx(s); 380 381 initpbuf(bp); 382 383 return bp; 384 } 385 386 /* 387 * release a physical buffer 388 */ 389 void 390 relpbuf(bp) 391 struct buf *bp; 392 { 393 int s; 394 395 s = splvm(); 396 397 if (bp->b_rcred != NOCRED) { 398 crfree(bp->b_rcred); 399 bp->b_rcred = NOCRED; 400 } 401 if (bp->b_wcred != NOCRED) { 402 crfree(bp->b_wcred); 403 bp->b_wcred = NOCRED; 404 } 405 if (bp->b_vp) 406 pbrelvp(bp); 407 408 if (bp->b_flags & B_WANTED) 409 wakeup(bp); 410 411 TAILQ_INSERT_HEAD(&bswlist, bp, b_freelist); 412 413 if (bswneeded) { 414 bswneeded = 0; 415 wakeup(&bswneeded); 416 } 417 splx(s); 418 } 419