1 /* $OpenBSD: uvm_device.c,v 1.11 2001/08/11 10:57:22 art Exp $ */ 2 /* $NetBSD: uvm_device.c,v 1.22 2000/05/28 10:21:55 drochner Exp $ */ 3 4 /* 5 * 6 * Copyright (c) 1997 Charles D. Cranor and Washington University. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by Charles D. Cranor and 20 * Washington University. 21 * 4. The name of the author may not be used to endorse or promote products 22 * derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 * 35 * from: Id: uvm_device.c,v 1.1.2.9 1998/02/06 05:11:47 chs Exp 36 */ 37 38 /* 39 * uvm_device.c: the device pager. 40 */ 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/conf.h> 45 #include <sys/proc.h> 46 #include <sys/malloc.h> 47 #include <sys/vnode.h> 48 49 #include <vm/vm.h> 50 #include <vm/vm_page.h> 51 #include <vm/vm_kern.h> 52 53 #include <uvm/uvm.h> 54 #include <uvm/uvm_device.h> 55 56 /* 57 * private global data structure 58 * 59 * we keep a list of active device objects in the system. 60 */ 61 62 LIST_HEAD(udv_list_struct, uvm_device); 63 static struct udv_list_struct udv_list; 64 static simple_lock_data_t udv_lock; 65 66 /* 67 * functions 68 */ 69 70 static void udv_init __P((void)); 71 static void udv_reference __P((struct uvm_object *)); 72 static void udv_detach __P((struct uvm_object *)); 73 static int udv_fault __P((struct uvm_faultinfo *, vaddr_t, 74 vm_page_t *, int, int, vm_fault_t, 75 vm_prot_t, int)); 76 static int udv_asyncget __P((struct uvm_object *, voff_t, 77 int)); 78 static boolean_t udv_flush __P((struct uvm_object *, voff_t, voff_t, 79 int)); 80 static int udv_put __P((struct uvm_object *, vm_page_t *, 81 int, boolean_t)); 82 83 /* 84 * master pager structure 85 */ 86 87 struct uvm_pagerops uvm_deviceops = { 88 udv_init, 89 udv_reference, 90 udv_detach, 91 udv_fault, 92 udv_flush, 93 NULL, /* no get function since we have udv_fault */ 94 udv_asyncget, 95 udv_put, 96 NULL, /* no cluster function */ 97 NULL, /* no put cluster function */ 98 NULL, /* no AIO-DONE function since no async i/o */ 99 NULL, /* no releasepg function since no normal pages */ 100 }; 101 102 /* 103 * the ops! 104 */ 105 106 /* 107 * udv_init 108 * 109 * init pager private data structures. 110 */ 111 112 void 113 udv_init() 114 { 115 116 LIST_INIT(&udv_list); 117 simple_lock_init(&udv_lock); 118 } 119 120 /* 121 * udv_attach 122 * 123 * get a VM object that is associated with a device. allocate a new 124 * one if needed. 125 * 126 * => caller must _not_ already be holding the lock on the uvm_object. 127 * => in fact, nothing should be locked so that we can sleep here. 128 */ 129 struct uvm_object * 130 udv_attach(arg, accessprot, off, size) 131 void *arg; 132 vm_prot_t accessprot; 133 voff_t off; /* used only for access check */ 134 vsize_t size; /* used only for access check */ 135 { 136 dev_t device = *((dev_t *) arg); 137 struct uvm_device *udv, *lcv; 138 int (*mapfn) __P((dev_t, int, int)); 139 UVMHIST_FUNC("udv_attach"); UVMHIST_CALLED(maphist); 140 141 UVMHIST_LOG(maphist, "(device=0x%x)", device,0,0,0); 142 143 /* 144 * before we do anything, ensure this device supports mmap 145 */ 146 147 mapfn = cdevsw[major(device)].d_mmap; 148 if (mapfn == NULL || 149 mapfn == (int (*) __P((dev_t, int, int))) enodev || 150 mapfn == (int (*) __P((dev_t, int, int))) nullop) 151 return(NULL); 152 153 /* 154 * As long as the device d_mmap interface gets an "int" 155 * offset, we have to watch out not to overflow its 156 * numeric range. (assuming it will be interpreted as 157 * "unsigned") 158 */ 159 if (((off + size - 1) & (u_int)-1) != off + size - 1) 160 return (0); 161 162 /* 163 * Check that the specified range of the device allows the 164 * desired protection. 165 * 166 * XXX assumes VM_PROT_* == PROT_* 167 * XXX clobbers off and size, but nothing else here needs them. 168 */ 169 170 while (size != 0) { 171 if ((*mapfn)(device, off, accessprot) == -1) 172 return (NULL); 173 off += PAGE_SIZE; size -= PAGE_SIZE; 174 } 175 176 /* 177 * keep looping until we get it 178 */ 179 180 while (1) { 181 182 /* 183 * first, attempt to find it on the main list 184 */ 185 186 simple_lock(&udv_lock); 187 for (lcv = udv_list.lh_first ; lcv != NULL ; lcv = lcv->u_list.le_next) { 188 if (device == lcv->u_device) 189 break; 190 } 191 192 /* 193 * got it on main list. put a hold on it and unlock udv_lock. 194 */ 195 196 if (lcv) { 197 198 /* 199 * if someone else has a hold on it, sleep and start 200 * over again. 201 */ 202 203 if (lcv->u_flags & UVM_DEVICE_HOLD) { 204 lcv->u_flags |= UVM_DEVICE_WANTED; 205 UVM_UNLOCK_AND_WAIT(lcv, &udv_lock, FALSE, 206 "udv_attach",0); 207 continue; 208 } 209 210 /* we are now holding it */ 211 lcv->u_flags |= UVM_DEVICE_HOLD; 212 simple_unlock(&udv_lock); 213 214 /* 215 * bump reference count, unhold, return. 216 */ 217 218 simple_lock(&lcv->u_obj.vmobjlock); 219 lcv->u_obj.uo_refs++; 220 simple_unlock(&lcv->u_obj.vmobjlock); 221 222 simple_lock(&udv_lock); 223 if (lcv->u_flags & UVM_DEVICE_WANTED) 224 wakeup(lcv); 225 lcv->u_flags &= ~(UVM_DEVICE_WANTED|UVM_DEVICE_HOLD); 226 simple_unlock(&udv_lock); 227 return(&lcv->u_obj); 228 } 229 230 /* 231 * did not find it on main list. need to malloc a new one. 232 */ 233 234 simple_unlock(&udv_lock); 235 /* NOTE: we could sleep in the following malloc() */ 236 MALLOC(udv, struct uvm_device *, sizeof(*udv), M_TEMP, M_WAITOK); 237 simple_lock(&udv_lock); 238 239 /* 240 * now we have to double check to make sure no one added it 241 * to the list while we were sleeping... 242 */ 243 244 for (lcv = udv_list.lh_first ; lcv != NULL ; 245 lcv = lcv->u_list.le_next) { 246 if (device == lcv->u_device) 247 break; 248 } 249 250 /* 251 * did we lose a race to someone else? free our memory and retry. 252 */ 253 254 if (lcv) { 255 simple_unlock(&udv_lock); 256 FREE(udv, M_TEMP); 257 continue; 258 } 259 260 /* 261 * we have it! init the data structures, add to list 262 * and return. 263 */ 264 265 simple_lock_init(&udv->u_obj.vmobjlock); 266 udv->u_obj.pgops = &uvm_deviceops; 267 TAILQ_INIT(&udv->u_obj.memq); /* not used, but be safe */ 268 udv->u_obj.uo_npages = 0; 269 udv->u_obj.uo_refs = 1; 270 udv->u_flags = 0; 271 udv->u_device = device; 272 LIST_INSERT_HEAD(&udv_list, udv, u_list); 273 simple_unlock(&udv_lock); 274 275 return(&udv->u_obj); 276 277 } /* while(1) loop */ 278 279 /*NOTREACHED*/ 280 } 281 282 /* 283 * udv_reference 284 * 285 * add a reference to a VM object. Note that the reference count must 286 * already be one (the passed in reference) so there is no chance of the 287 * udv being released or locked out here. 288 * 289 * => caller must call with object unlocked. 290 */ 291 292 static void 293 udv_reference(uobj) 294 struct uvm_object *uobj; 295 { 296 UVMHIST_FUNC("udv_reference"); UVMHIST_CALLED(maphist); 297 298 simple_lock(&uobj->vmobjlock); 299 uobj->uo_refs++; 300 UVMHIST_LOG(maphist, "<- done (uobj=0x%x, ref = %d)", 301 uobj, uobj->uo_refs,0,0); 302 simple_unlock(&uobj->vmobjlock); 303 } 304 305 /* 306 * udv_detach 307 * 308 * remove a reference to a VM object. 309 * 310 * => caller must call with object unlocked and map locked. 311 */ 312 313 static void 314 udv_detach(uobj) 315 struct uvm_object *uobj; 316 { 317 struct uvm_device *udv = (struct uvm_device *) uobj; 318 UVMHIST_FUNC("udv_detach"); UVMHIST_CALLED(maphist); 319 320 /* 321 * loop until done 322 */ 323 324 while (1) { 325 simple_lock(&uobj->vmobjlock); 326 327 if (uobj->uo_refs > 1) { 328 uobj->uo_refs--; /* drop ref! */ 329 simple_unlock(&uobj->vmobjlock); 330 UVMHIST_LOG(maphist," <- done, uobj=0x%x, ref=%d", 331 uobj,uobj->uo_refs,0,0); 332 return; 333 } 334 335 #ifdef DIAGNOSTIC 336 if (uobj->uo_npages || uobj->memq.tqh_first) 337 panic("udv_detach: pages in a device object?"); 338 #endif 339 340 /* 341 * now lock udv_lock 342 */ 343 simple_lock(&udv_lock); 344 345 /* 346 * is it being held? if so, wait until others are done. 347 */ 348 if (udv->u_flags & UVM_DEVICE_HOLD) { 349 350 /* 351 * want it 352 */ 353 udv->u_flags |= UVM_DEVICE_WANTED; 354 simple_unlock(&uobj->vmobjlock); 355 UVM_UNLOCK_AND_WAIT(udv, &udv_lock, FALSE, "udv_detach",0); 356 continue; 357 } 358 359 /* 360 * got it! nuke it now. 361 */ 362 363 LIST_REMOVE(udv, u_list); 364 if (udv->u_flags & UVM_DEVICE_WANTED) 365 wakeup(udv); 366 FREE(udv, M_TEMP); 367 break; /* DONE! */ 368 369 } /* while (1) loop */ 370 371 UVMHIST_LOG(maphist," <- done, freed uobj=0x%x", uobj,0,0,0); 372 return; 373 } 374 375 376 /* 377 * udv_flush 378 * 379 * flush pages out of a uvm object. a no-op for devices. 380 */ 381 382 static boolean_t udv_flush(uobj, start, stop, flags) 383 struct uvm_object *uobj; 384 voff_t start, stop; 385 int flags; 386 { 387 388 return(TRUE); 389 } 390 391 /* 392 * udv_fault: non-standard fault routine for device "pages" 393 * 394 * => rather than having a "get" function, we have a fault routine 395 * since we don't return vm_pages we need full control over the 396 * pmap_enter map in 397 * => all the usual fault data structured are locked by the caller 398 * (i.e. maps(read), amap (if any), uobj) 399 * => on return, we unlock all fault data structures 400 * => flags: PGO_ALLPAGES: get all of the pages 401 * PGO_LOCKED: fault data structures are locked 402 * XXX: currently PGO_LOCKED is always required ... consider removing 403 * it as a flag 404 * => NOTE: vaddr is the VA of pps[0] in ufi->entry, _NOT_ pps[centeridx] 405 */ 406 407 static int 408 udv_fault(ufi, vaddr, pps, npages, centeridx, fault_type, access_type, flags) 409 struct uvm_faultinfo *ufi; 410 vaddr_t vaddr; 411 vm_page_t *pps; 412 int npages, centeridx, flags; 413 vm_fault_t fault_type; 414 vm_prot_t access_type; 415 { 416 struct vm_map_entry *entry = ufi->entry; 417 struct uvm_object *uobj = entry->object.uvm_obj; 418 struct uvm_device *udv = (struct uvm_device *)uobj; 419 vaddr_t curr_va; 420 int curr_offset; 421 paddr_t paddr; 422 int lcv, retval, mdpgno; 423 dev_t device; 424 int (*mapfn) __P((dev_t, int, int)); 425 vm_prot_t mapprot; 426 UVMHIST_FUNC("udv_fault"); UVMHIST_CALLED(maphist); 427 UVMHIST_LOG(maphist," flags=%d", flags,0,0,0); 428 429 /* 430 * XXX: !PGO_LOCKED calls are currently not allowed (or used) 431 */ 432 433 if ((flags & PGO_LOCKED) == 0) 434 panic("udv_fault: !PGO_LOCKED fault"); 435 436 /* 437 * we do not allow device mappings to be mapped copy-on-write 438 * so we kill any attempt to do so here. 439 */ 440 441 if (UVM_ET_ISCOPYONWRITE(entry)) { 442 UVMHIST_LOG(maphist, "<- failed -- COW entry (etype=0x%x)", 443 entry->etype, 0,0,0); 444 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj, NULL); 445 return(VM_PAGER_ERROR); 446 } 447 448 /* 449 * get device map function. 450 */ 451 device = udv->u_device; 452 mapfn = cdevsw[major(device)].d_mmap; 453 454 /* 455 * now we must determine the offset in udv to use and the VA to 456 * use for pmap_enter. note that we always use orig_map's pmap 457 * for pmap_enter (even if we have a submap). since virtual 458 * addresses in a submap must match the main map, this is ok. 459 */ 460 /* udv offset = (offset from start of entry) + entry's offset */ 461 curr_offset = (int)((vaddr - entry->start) + entry->offset); 462 /* pmap va = vaddr (virtual address of pps[0]) */ 463 curr_va = vaddr; 464 465 /* 466 * loop over the page range entering in as needed 467 */ 468 469 retval = VM_PAGER_OK; 470 for (lcv = 0 ; lcv < npages ; lcv++, curr_offset += PAGE_SIZE, 471 curr_va += PAGE_SIZE) { 472 if ((flags & PGO_ALLPAGES) == 0 && lcv != centeridx) 473 continue; 474 475 if (pps[lcv] == PGO_DONTCARE) 476 continue; 477 478 mdpgno = (*mapfn)(device, curr_offset, access_type); 479 if (mdpgno == -1) { 480 retval = VM_PAGER_ERROR; 481 break; 482 } 483 paddr = pmap_phys_address(mdpgno); 484 mapprot = ufi->entry->protection; 485 UVMHIST_LOG(maphist, 486 " MAPPING: device: pm=0x%x, va=0x%x, pa=0x%x, at=%d", 487 ufi->orig_map->pmap, curr_va, (int)paddr, mapprot); 488 if (pmap_enter(ufi->orig_map->pmap, curr_va, paddr, 489 mapprot, PMAP_CANFAIL | mapprot) != KERN_SUCCESS) { 490 /* 491 * pmap_enter() didn't have the resource to 492 * enter this mapping. Unlock everything, 493 * wait for the pagedaemon to free up some 494 * pages, and then tell uvm_fault() to start 495 * the fault again. 496 * 497 * XXX Needs some rethinking for the PGO_ALLPAGES 498 * XXX case. 499 */ 500 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, 501 uobj, NULL); 502 uvm_wait("udv_fault"); 503 return (VM_PAGER_REFAULT); 504 } 505 } 506 507 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj, NULL); 508 return (retval); 509 } 510 511 /* 512 * udv_asyncget: start async I/O to bring pages into ram 513 * 514 * => caller must lock object(???XXX: see if this is best) 515 * => a no-op for devices 516 */ 517 518 static int 519 udv_asyncget(uobj, offset, npages) 520 struct uvm_object *uobj; 521 voff_t offset; 522 int npages; 523 { 524 525 return(KERN_SUCCESS); 526 } 527 528 /* 529 * udv_put: flush page data to backing store. 530 * 531 * => this function should never be called (since we never have any 532 * page structures to "put") 533 */ 534 535 static int 536 udv_put(uobj, pps, npages, flags) 537 struct uvm_object *uobj; 538 struct vm_page **pps; 539 int npages, flags; 540 { 541 542 panic("udv_put: trying to page out to a device!"); 543 } 544