1 /* $OpenBSD: uvm_device.c,v 1.48 2014/07/12 18:44:01 tedu Exp $ */ 2 /* $NetBSD: uvm_device.c,v 1.30 2000/11/25 06:27:59 chs Exp $ */ 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 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * from: Id: uvm_device.c,v 1.1.2.9 1998/02/06 05:11:47 chs Exp 29 */ 30 31 /* 32 * uvm_device.c: the device pager. 33 */ 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/conf.h> 38 #include <sys/proc.h> 39 #include <sys/malloc.h> 40 #include <sys/mutex.h> 41 #include <sys/vnode.h> 42 43 #include <uvm/uvm.h> 44 #include <uvm/uvm_device.h> 45 46 #if defined(__amd64__) || defined(__i386__) || \ 47 defined(__macppc__) || defined(__sparc64__) 48 #include "drm.h" 49 #endif 50 51 /* 52 * private global data structure 53 * 54 * we keep a list of active device objects in the system. 55 */ 56 57 LIST_HEAD(, uvm_device) udv_list = LIST_HEAD_INITIALIZER(udv_list); 58 struct mutex udv_lock = MUTEX_INITIALIZER(IPL_NONE); 59 60 /* 61 * functions 62 */ 63 static void udv_reference(struct uvm_object *); 64 static void udv_detach(struct uvm_object *); 65 static int udv_fault(struct uvm_faultinfo *, vaddr_t, 66 vm_page_t *, int, int, vm_fault_t, 67 vm_prot_t, int); 68 static boolean_t udv_flush(struct uvm_object *, voff_t, voff_t, 69 int); 70 71 /* 72 * master pager structure 73 */ 74 struct uvm_pagerops uvm_deviceops = { 75 NULL, /* inited statically */ 76 udv_reference, 77 udv_detach, 78 udv_fault, 79 udv_flush, 80 }; 81 82 /* 83 * udv_attach 84 * 85 * get a VM object that is associated with a device. allocate a new 86 * one if needed. 87 * 88 * => nothing should be locked so that we can sleep here. 89 * 90 * The last two arguments (off and size) are only used for access checking. 91 */ 92 struct uvm_object * 93 udv_attach(dev_t device, vm_prot_t accessprot, voff_t off, vsize_t size) 94 { 95 struct uvm_device *udv, *lcv; 96 paddr_t (*mapfn)(dev_t, off_t, int); 97 #if NDRM > 0 98 struct uvm_object *obj; 99 #endif 100 101 /* before we do anything, ensure this device supports mmap */ 102 mapfn = cdevsw[major(device)].d_mmap; 103 if (mapfn == NULL || 104 mapfn == (paddr_t (*)(dev_t, off_t, int)) enodev || 105 mapfn == (paddr_t (*)(dev_t, off_t, int)) nullop) 106 return(NULL); 107 108 /* Negative offsets on the object are not allowed. */ 109 if (off < 0) 110 return(NULL); 111 112 #if NDRM > 0 113 obj = udv_attach_drm(device, accessprot, off, size); 114 if (obj) 115 return(obj); 116 #endif 117 118 /* 119 * Check that the specified range of the device allows the 120 * desired protection. 121 * 122 * XXX assumes VM_PROT_* == PROT_* 123 * XXX clobbers off and size, but nothing else here needs them. 124 */ 125 while (size != 0) { 126 if ((*mapfn)(device, off, accessprot) == -1) 127 return (NULL); 128 off += PAGE_SIZE; size -= PAGE_SIZE; 129 } 130 131 /* keep looping until we get it */ 132 for (;;) { 133 /* first, attempt to find it on the main list */ 134 mtx_enter(&udv_lock); 135 LIST_FOREACH(lcv, &udv_list, u_list) { 136 if (device == lcv->u_device) 137 break; 138 } 139 140 /* got it on main list. put a hold on it and unlock udv_lock. */ 141 if (lcv) { 142 /* 143 * if someone else has a hold on it, sleep and start 144 * over again. Else, we need take HOLD flag so we 145 * don't have to re-order locking here. 146 */ 147 if (lcv->u_flags & UVM_DEVICE_HOLD) { 148 lcv->u_flags |= UVM_DEVICE_WANTED; 149 msleep(lcv, &udv_lock, PVM | PNORELOCK, 150 "udv_attach", 0); 151 continue; 152 } 153 154 /* we are now holding it */ 155 lcv->u_flags |= UVM_DEVICE_HOLD; 156 mtx_leave(&udv_lock); 157 158 /* bump reference count, unhold, return. */ 159 lcv->u_obj.uo_refs++; 160 161 mtx_enter(&udv_lock); 162 if (lcv->u_flags & UVM_DEVICE_WANTED) 163 wakeup(lcv); 164 lcv->u_flags &= ~(UVM_DEVICE_WANTED|UVM_DEVICE_HOLD); 165 mtx_leave(&udv_lock); 166 return(&lcv->u_obj); 167 } 168 169 /* did not find it on main list. need to malloc a new one. */ 170 mtx_leave(&udv_lock); 171 /* NOTE: we could sleep in the following malloc() */ 172 udv = malloc(sizeof(*udv), M_TEMP, M_WAITOK); 173 mtx_enter(&udv_lock); 174 175 /* 176 * now we have to double check to make sure no one added it 177 * to the list while we were sleeping... 178 */ 179 LIST_FOREACH(lcv, &udv_list, u_list) { 180 if (device == lcv->u_device) 181 break; 182 } 183 184 /* 185 * did we lose a race to someone else? 186 * free our memory and retry. 187 */ 188 if (lcv) { 189 mtx_leave(&udv_lock); 190 free(udv, M_TEMP, 0); 191 continue; 192 } 193 194 /* 195 * we have it! init the data structures, add to list 196 * and return. 197 */ 198 uvm_objinit(&udv->u_obj, &uvm_deviceops, 1); 199 udv->u_flags = 0; 200 udv->u_device = device; 201 LIST_INSERT_HEAD(&udv_list, udv, u_list); 202 mtx_leave(&udv_lock); 203 return(&udv->u_obj); 204 } 205 /*NOTREACHED*/ 206 } 207 208 /* 209 * udv_reference 210 * 211 * add a reference to a VM object. Note that the reference count must 212 * already be one (the passed in reference) so there is no chance of the 213 * udv being released or locked out here. 214 */ 215 static void 216 udv_reference(struct uvm_object *uobj) 217 { 218 219 uobj->uo_refs++; 220 } 221 222 /* 223 * udv_detach 224 * 225 * remove a reference to a VM object. 226 */ 227 static void 228 udv_detach(struct uvm_object *uobj) 229 { 230 struct uvm_device *udv = (struct uvm_device *)uobj; 231 232 /* loop until done */ 233 again: 234 if (uobj->uo_refs > 1) { 235 uobj->uo_refs--; 236 return; 237 } 238 KASSERT(uobj->uo_npages == 0 && RB_EMPTY(&uobj->memt)); 239 240 /* is it being held? if so, wait until others are done. */ 241 mtx_enter(&udv_lock); 242 if (udv->u_flags & UVM_DEVICE_HOLD) { 243 udv->u_flags |= UVM_DEVICE_WANTED; 244 /* 245 * lock interleaving. -- this is ok in this case since the 246 * locks are both IPL_NONE 247 */ 248 msleep(udv, &udv_lock, PVM | PNORELOCK, "udv_detach", 0); 249 goto again; 250 } 251 252 /* got it! nuke it now. */ 253 LIST_REMOVE(udv, u_list); 254 if (udv->u_flags & UVM_DEVICE_WANTED) 255 wakeup(udv); 256 mtx_leave(&udv_lock); 257 free(udv, M_TEMP, 0); 258 } 259 260 261 /* 262 * udv_flush 263 * 264 * flush pages out of a uvm object. a no-op for devices. 265 */ 266 static boolean_t 267 udv_flush(struct uvm_object *uobj, voff_t start, voff_t stop, int flags) 268 { 269 270 return(TRUE); 271 } 272 273 /* 274 * udv_fault: non-standard fault routine for device "pages" 275 * 276 * => rather than having a "get" function, we have a fault routine 277 * since we don't return vm_pages we need full control over the 278 * pmap_enter map in 279 * => on return, we unlock all fault data structures 280 * => flags: PGO_ALLPAGES: get all of the pages 281 * PGO_LOCKED: fault data structures are locked 282 * XXX: currently PGO_LOCKED is always required ... consider removing 283 * it as a flag 284 * => NOTE: vaddr is the VA of pps[0] in ufi->entry, _NOT_ pps[centeridx] 285 */ 286 static int 287 udv_fault(struct uvm_faultinfo *ufi, vaddr_t vaddr, vm_page_t *pps, int npages, 288 int centeridx, vm_fault_t fault_type, vm_prot_t access_type, int flags) 289 { 290 struct vm_map_entry *entry = ufi->entry; 291 struct uvm_object *uobj = entry->object.uvm_obj; 292 struct uvm_device *udv = (struct uvm_device *)uobj; 293 vaddr_t curr_va; 294 off_t curr_offset; 295 paddr_t paddr; 296 int lcv, retval; 297 dev_t device; 298 paddr_t (*mapfn)(dev_t, off_t, int); 299 vm_prot_t mapprot; 300 301 /* 302 * we do not allow device mappings to be mapped copy-on-write 303 * so we kill any attempt to do so here. 304 */ 305 if (UVM_ET_ISCOPYONWRITE(entry)) { 306 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj, NULL); 307 return(VM_PAGER_ERROR); 308 } 309 310 /* get device map function. */ 311 device = udv->u_device; 312 mapfn = cdevsw[major(device)].d_mmap; 313 314 /* 315 * now we must determine the offset in udv to use and the VA to 316 * use for pmap_enter. note that we always use orig_map's pmap 317 * for pmap_enter (even if we have a submap). since virtual 318 * addresses in a submap must match the main map, this is ok. 319 */ 320 /* udv offset = (offset from start of entry) + entry's offset */ 321 curr_offset = entry->offset + (vaddr - entry->start); 322 /* pmap va = vaddr (virtual address of pps[0]) */ 323 curr_va = vaddr; 324 325 /* loop over the page range entering in as needed */ 326 retval = VM_PAGER_OK; 327 for (lcv = 0 ; lcv < npages ; lcv++, curr_offset += PAGE_SIZE, 328 curr_va += PAGE_SIZE) { 329 if ((flags & PGO_ALLPAGES) == 0 && lcv != centeridx) 330 continue; 331 332 if (pps[lcv] == PGO_DONTCARE) 333 continue; 334 335 paddr = (*mapfn)(device, curr_offset, access_type); 336 if (paddr == -1) { 337 retval = VM_PAGER_ERROR; 338 break; 339 } 340 mapprot = ufi->entry->protection; 341 if (pmap_enter(ufi->orig_map->pmap, curr_va, paddr, 342 mapprot, PMAP_CANFAIL | mapprot) != 0) { 343 /* 344 * pmap_enter() didn't have the resource to 345 * enter this mapping. Unlock everything, 346 * wait for the pagedaemon to free up some 347 * pages, and then tell uvm_fault() to start 348 * the fault again. 349 * 350 * XXX Needs some rethinking for the PGO_ALLPAGES 351 * XXX case. 352 */ 353 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, 354 uobj, NULL); 355 356 /* sync what we have so far */ 357 pmap_update(ufi->orig_map->pmap); 358 uvm_wait("udv_fault"); 359 return (VM_PAGER_REFAULT); 360 } 361 } 362 363 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj, NULL); 364 pmap_update(ufi->orig_map->pmap); 365 return (retval); 366 } 367