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