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