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