1 /* $OpenBSD: uvm_device.c,v 1.30 2009/03/23 19:23:43 oga Exp $ */ 2 /* $NetBSD: uvm_device.c,v 1.30 2000/11/25 06:27:59 chs 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 <uvm/uvm.h> 50 #include <uvm/uvm_device.h> 51 52 /* 53 * private global data structure 54 * 55 * we keep a list of active device objects in the system. 56 */ 57 58 LIST_HEAD(udv_list_struct, uvm_device); 59 static struct udv_list_struct udv_list; 60 static simple_lock_data_t udv_lock; 61 62 /* 63 * functions 64 */ 65 66 static void udv_init(void); 67 static void udv_reference(struct uvm_object *); 68 static void udv_detach(struct uvm_object *); 69 static int udv_fault(struct uvm_faultinfo *, vaddr_t, 70 vm_page_t *, int, int, vm_fault_t, 71 vm_prot_t, int); 72 static boolean_t udv_flush(struct uvm_object *, voff_t, voff_t, 73 int); 74 75 /* 76 * master pager structure 77 */ 78 79 struct uvm_pagerops uvm_deviceops = { 80 udv_init, 81 udv_reference, 82 udv_detach, 83 udv_fault, 84 udv_flush, 85 }; 86 87 /* 88 * the ops! 89 */ 90 91 /* 92 * udv_init 93 * 94 * init pager private data structures. 95 */ 96 97 void 98 udv_init(void) 99 { 100 101 LIST_INIT(&udv_list); 102 simple_lock_init(&udv_lock); 103 } 104 105 /* 106 * udv_attach 107 * 108 * get a VM object that is associated with a device. allocate a new 109 * one if needed. 110 * 111 * => caller must _not_ already be holding the lock on the uvm_object. 112 * => in fact, nothing should be locked so that we can sleep here. 113 * 114 * The last two arguments (off and size) are only used for access checking. 115 */ 116 struct uvm_object * 117 udv_attach(void *arg, vm_prot_t accessprot, voff_t off, vsize_t size) 118 { 119 dev_t device = *((dev_t *)arg); 120 struct uvm_device *udv, *lcv; 121 paddr_t (*mapfn)(dev_t, off_t, int); 122 UVMHIST_FUNC("udv_attach"); UVMHIST_CALLED(maphist); 123 124 UVMHIST_LOG(maphist, "(device=0x%lx)", device,0,0,0); 125 126 /* 127 * before we do anything, ensure this device supports mmap 128 */ 129 130 mapfn = cdevsw[major(device)].d_mmap; 131 if (mapfn == NULL || 132 mapfn == (paddr_t (*)(dev_t, off_t, int)) enodev || 133 mapfn == (paddr_t (*)(dev_t, off_t, int)) nullop) 134 return(NULL); 135 136 /* 137 * Negative offsets on the object are not allowed. 138 */ 139 140 if (off < 0) 141 return(NULL); 142 143 /* 144 * Check that the specified range of the device allows the 145 * desired protection. 146 * 147 * XXX assumes VM_PROT_* == PROT_* 148 * XXX clobbers off and size, but nothing else here needs them. 149 */ 150 151 while (size != 0) { 152 if ((*mapfn)(device, off, accessprot) == -1) 153 return (NULL); 154 off += PAGE_SIZE; size -= PAGE_SIZE; 155 } 156 157 /* 158 * keep looping until we get it 159 */ 160 161 for (;;) { 162 163 /* 164 * first, attempt to find it on the main list 165 */ 166 167 simple_lock(&udv_lock); 168 LIST_FOREACH(lcv, &udv_list, u_list) { 169 if (device == lcv->u_device) 170 break; 171 } 172 173 /* 174 * got it on main list. put a hold on it and unlock udv_lock. 175 */ 176 177 if (lcv) { 178 179 /* 180 * if someone else has a hold on it, sleep and start 181 * over again. 182 */ 183 184 if (lcv->u_flags & UVM_DEVICE_HOLD) { 185 lcv->u_flags |= UVM_DEVICE_WANTED; 186 UVM_UNLOCK_AND_WAIT(lcv, &udv_lock, FALSE, 187 "udv_attach",0); 188 continue; 189 } 190 191 /* we are now holding it */ 192 lcv->u_flags |= UVM_DEVICE_HOLD; 193 simple_unlock(&udv_lock); 194 195 /* 196 * bump reference count, unhold, return. 197 */ 198 199 simple_lock(&lcv->u_obj.vmobjlock); 200 lcv->u_obj.uo_refs++; 201 simple_unlock(&lcv->u_obj.vmobjlock); 202 203 simple_lock(&udv_lock); 204 if (lcv->u_flags & UVM_DEVICE_WANTED) 205 wakeup(lcv); 206 lcv->u_flags &= ~(UVM_DEVICE_WANTED|UVM_DEVICE_HOLD); 207 simple_unlock(&udv_lock); 208 return(&lcv->u_obj); 209 } 210 211 /* 212 * did not find it on main list. need to malloc a new one. 213 */ 214 215 simple_unlock(&udv_lock); 216 /* NOTE: we could sleep in the following malloc() */ 217 udv = malloc(sizeof(*udv), M_TEMP, M_WAITOK); 218 simple_lock(&udv_lock); 219 220 /* 221 * now we have to double check to make sure no one added it 222 * to the list while we were sleeping... 223 */ 224 225 LIST_FOREACH(lcv, &udv_list, u_list) { 226 if (device == lcv->u_device) 227 break; 228 } 229 230 /* 231 * did we lose a race to someone else? 232 * free our memory and retry. 233 */ 234 235 if (lcv) { 236 simple_unlock(&udv_lock); 237 free(udv, M_TEMP); 238 continue; 239 } 240 241 /* 242 * we have it! init the data structures, add to list 243 * and return. 244 */ 245 246 simple_lock_init(&udv->u_obj.vmobjlock); 247 udv->u_obj.pgops = &uvm_deviceops; 248 TAILQ_INIT(&udv->u_obj.memq); 249 udv->u_obj.uo_npages = 0; 250 udv->u_obj.uo_refs = 1; 251 udv->u_flags = 0; 252 udv->u_device = device; 253 LIST_INSERT_HEAD(&udv_list, udv, u_list); 254 simple_unlock(&udv_lock); 255 return(&udv->u_obj); 256 } 257 /*NOTREACHED*/ 258 } 259 260 /* 261 * udv_reference 262 * 263 * add a reference to a VM object. Note that the reference count must 264 * already be one (the passed in reference) so there is no chance of the 265 * udv being released or locked out here. 266 * 267 * => caller must call with object unlocked. 268 */ 269 270 static void 271 udv_reference(struct uvm_object *uobj) 272 { 273 UVMHIST_FUNC("udv_reference"); UVMHIST_CALLED(maphist); 274 275 simple_lock(&uobj->vmobjlock); 276 uobj->uo_refs++; 277 UVMHIST_LOG(maphist, "<- done (uobj=%p, ref = %ld)", 278 uobj, uobj->uo_refs,0,0); 279 simple_unlock(&uobj->vmobjlock); 280 } 281 282 /* 283 * udv_detach 284 * 285 * remove a reference to a VM object. 286 * 287 * => caller must call with object unlocked and map locked. 288 */ 289 290 static void 291 udv_detach(struct uvm_object *uobj) 292 { 293 struct uvm_device *udv = (struct uvm_device *)uobj; 294 UVMHIST_FUNC("udv_detach"); UVMHIST_CALLED(maphist); 295 296 /* 297 * loop until done 298 */ 299 again: 300 simple_lock(&uobj->vmobjlock); 301 if (uobj->uo_refs > 1) { 302 uobj->uo_refs--; 303 simple_unlock(&uobj->vmobjlock); 304 UVMHIST_LOG(maphist," <- done, uobj=%p, ref=%ld", 305 uobj,uobj->uo_refs,0,0); 306 return; 307 } 308 KASSERT(uobj->uo_npages == 0 && TAILQ_EMPTY(&uobj->memq)); 309 310 /* 311 * is it being held? if so, wait until others are done. 312 */ 313 314 simple_lock(&udv_lock); 315 if (udv->u_flags & UVM_DEVICE_HOLD) { 316 udv->u_flags |= UVM_DEVICE_WANTED; 317 simple_unlock(&uobj->vmobjlock); 318 UVM_UNLOCK_AND_WAIT(udv, &udv_lock, FALSE, "udv_detach",0); 319 goto again; 320 } 321 322 /* 323 * got it! nuke it now. 324 */ 325 326 LIST_REMOVE(udv, u_list); 327 if (udv->u_flags & UVM_DEVICE_WANTED) 328 wakeup(udv); 329 simple_unlock(&udv_lock); 330 simple_unlock(&uobj->vmobjlock); 331 free(udv, M_TEMP); 332 UVMHIST_LOG(maphist," <- done, freed uobj=%p", uobj,0,0,0); 333 } 334 335 336 /* 337 * udv_flush 338 * 339 * flush pages out of a uvm object. a no-op for devices. 340 */ 341 342 static boolean_t 343 udv_flush(struct uvm_object *uobj, voff_t start, voff_t stop, int flags) 344 { 345 346 return(TRUE); 347 } 348 349 /* 350 * udv_fault: non-standard fault routine for device "pages" 351 * 352 * => rather than having a "get" function, we have a fault routine 353 * since we don't return vm_pages we need full control over the 354 * pmap_enter map in 355 * => all the usual fault data structured are locked by the caller 356 * (i.e. maps(read), amap (if any), uobj) 357 * => on return, we unlock all fault data structures 358 * => flags: PGO_ALLPAGES: get all of the pages 359 * PGO_LOCKED: fault data structures are locked 360 * XXX: currently PGO_LOCKED is always required ... consider removing 361 * it as a flag 362 * => NOTE: vaddr is the VA of pps[0] in ufi->entry, _NOT_ pps[centeridx] 363 */ 364 365 static int 366 udv_fault(struct uvm_faultinfo *ufi, vaddr_t vaddr, vm_page_t *pps, int npages, 367 int centeridx, vm_fault_t fault_type, vm_prot_t access_type, int flags) 368 { 369 struct vm_map_entry *entry = ufi->entry; 370 struct uvm_object *uobj = entry->object.uvm_obj; 371 struct uvm_device *udv = (struct uvm_device *)uobj; 372 vaddr_t curr_va; 373 off_t curr_offset; 374 paddr_t paddr, mdpgno; 375 int lcv, retval; 376 dev_t device; 377 paddr_t (*mapfn)(dev_t, off_t, int); 378 vm_prot_t mapprot; 379 UVMHIST_FUNC("udv_fault"); UVMHIST_CALLED(maphist); 380 UVMHIST_LOG(maphist," flags=%ld", flags,0,0,0); 381 382 /* 383 * we do not allow device mappings to be mapped copy-on-write 384 * so we kill any attempt to do so here. 385 */ 386 387 if (UVM_ET_ISCOPYONWRITE(entry)) { 388 UVMHIST_LOG(maphist, "<- failed -- COW entry (etype=0x%lx)", 389 entry->etype, 0,0,0); 390 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj, NULL); 391 return(VM_PAGER_ERROR); 392 } 393 394 /* 395 * get device map function. 396 */ 397 398 device = udv->u_device; 399 mapfn = cdevsw[major(device)].d_mmap; 400 401 /* 402 * now we must determine the offset in udv to use and the VA to 403 * use for pmap_enter. note that we always use orig_map's pmap 404 * for pmap_enter (even if we have a submap). since virtual 405 * addresses in a submap must match the main map, this is ok. 406 */ 407 408 /* udv offset = (offset from start of entry) + entry's offset */ 409 curr_offset = entry->offset + (vaddr - entry->start); 410 /* pmap va = vaddr (virtual address of pps[0]) */ 411 curr_va = vaddr; 412 413 /* 414 * loop over the page range entering in as needed 415 */ 416 417 retval = VM_PAGER_OK; 418 for (lcv = 0 ; lcv < npages ; lcv++, curr_offset += PAGE_SIZE, 419 curr_va += PAGE_SIZE) { 420 if ((flags & PGO_ALLPAGES) == 0 && lcv != centeridx) 421 continue; 422 423 if (pps[lcv] == PGO_DONTCARE) 424 continue; 425 426 mdpgno = (*mapfn)(device, curr_offset, access_type); 427 if (mdpgno == -1) { 428 retval = VM_PAGER_ERROR; 429 break; 430 } 431 paddr = pmap_phys_address(mdpgno); 432 mapprot = ufi->entry->protection; 433 UVMHIST_LOG(maphist, 434 " MAPPING: device: pm=%p, va=0x%lx, pa=0x%lx, at=%ld", 435 ufi->orig_map->pmap, curr_va, (u_long)paddr, mapprot); 436 if (pmap_enter(ufi->orig_map->pmap, curr_va, paddr, 437 mapprot, PMAP_CANFAIL | mapprot) != 0) { 438 /* 439 * pmap_enter() didn't have the resource to 440 * enter this mapping. Unlock everything, 441 * wait for the pagedaemon to free up some 442 * pages, and then tell uvm_fault() to start 443 * the fault again. 444 * 445 * XXX Needs some rethinking for the PGO_ALLPAGES 446 * XXX case. 447 */ 448 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, 449 uobj, NULL); 450 451 /* sync what we have so far */ 452 pmap_update(ufi->orig_map->pmap); 453 uvm_wait("udv_fault"); 454 return (VM_PAGER_REFAULT); 455 } 456 } 457 458 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj, NULL); 459 pmap_update(ufi->orig_map->pmap); 460 return (retval); 461 } 462