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