1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1990 University of Utah. 5 * Copyright (c) 1991, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * the Systems Programming Group of the University of Utah Computer 10 * Science Department. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)device_pager.c 8.1 (Berkeley) 6/11/93 37 */ 38 39 #include <sys/cdefs.h> 40 __FBSDID("$FreeBSD$"); 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/conf.h> 45 #include <sys/lock.h> 46 #include <sys/proc.h> 47 #include <sys/mutex.h> 48 #include <sys/mman.h> 49 #include <sys/rwlock.h> 50 #include <sys/sx.h> 51 #include <sys/vmmeter.h> 52 53 #include <vm/vm.h> 54 #include <vm/vm_param.h> 55 #include <vm/vm_object.h> 56 #include <vm/vm_page.h> 57 #include <vm/vm_pager.h> 58 #include <vm/vm_phys.h> 59 #include <vm/uma.h> 60 61 static void dev_pager_init(void); 62 static vm_object_t dev_pager_alloc(void *, vm_ooffset_t, vm_prot_t, 63 vm_ooffset_t, struct ucred *); 64 static void dev_pager_dealloc(vm_object_t); 65 static int dev_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *); 66 static void dev_pager_putpages(vm_object_t, vm_page_t *, int, int, int *); 67 static boolean_t dev_pager_haspage(vm_object_t, vm_pindex_t, int *, int *); 68 static void dev_pager_free_page(vm_object_t object, vm_page_t m); 69 static int dev_pager_populate(vm_object_t object, vm_pindex_t pidx, 70 int fault_type, vm_prot_t, vm_pindex_t *first, vm_pindex_t *last); 71 72 /* list of device pager objects */ 73 static struct pagerlst dev_pager_object_list; 74 /* protect list manipulation */ 75 static struct mtx dev_pager_mtx; 76 77 struct pagerops devicepagerops = { 78 .pgo_init = dev_pager_init, 79 .pgo_alloc = dev_pager_alloc, 80 .pgo_dealloc = dev_pager_dealloc, 81 .pgo_getpages = dev_pager_getpages, 82 .pgo_putpages = dev_pager_putpages, 83 .pgo_haspage = dev_pager_haspage, 84 }; 85 86 struct pagerops mgtdevicepagerops = { 87 .pgo_alloc = dev_pager_alloc, 88 .pgo_dealloc = dev_pager_dealloc, 89 .pgo_getpages = dev_pager_getpages, 90 .pgo_putpages = dev_pager_putpages, 91 .pgo_haspage = dev_pager_haspage, 92 .pgo_populate = dev_pager_populate, 93 }; 94 95 static int old_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot, 96 vm_ooffset_t foff, struct ucred *cred, u_short *color); 97 static void old_dev_pager_dtor(void *handle); 98 static int old_dev_pager_fault(vm_object_t object, vm_ooffset_t offset, 99 int prot, vm_page_t *mres); 100 101 static struct cdev_pager_ops old_dev_pager_ops = { 102 .cdev_pg_ctor = old_dev_pager_ctor, 103 .cdev_pg_dtor = old_dev_pager_dtor, 104 .cdev_pg_fault = old_dev_pager_fault 105 }; 106 107 static void 108 dev_pager_init(void) 109 { 110 111 TAILQ_INIT(&dev_pager_object_list); 112 mtx_init(&dev_pager_mtx, "dev_pager list", NULL, MTX_DEF); 113 } 114 115 vm_object_t 116 cdev_pager_lookup(void *handle) 117 { 118 vm_object_t object; 119 120 mtx_lock(&dev_pager_mtx); 121 object = vm_pager_object_lookup(&dev_pager_object_list, handle); 122 mtx_unlock(&dev_pager_mtx); 123 return (object); 124 } 125 126 vm_object_t 127 cdev_pager_allocate(void *handle, enum obj_type tp, struct cdev_pager_ops *ops, 128 vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff, struct ucred *cred) 129 { 130 vm_object_t object, object1; 131 vm_pindex_t pindex; 132 u_short color; 133 134 if (tp != OBJT_DEVICE && tp != OBJT_MGTDEVICE) 135 return (NULL); 136 KASSERT(tp == OBJT_MGTDEVICE || ops->cdev_pg_populate == NULL, 137 ("populate on unmanaged device pager")); 138 139 /* 140 * Offset should be page aligned. 141 */ 142 if (foff & PAGE_MASK) 143 return (NULL); 144 145 /* 146 * Treat the mmap(2) file offset as an unsigned value for a 147 * device mapping. This, in effect, allows a user to pass all 148 * possible off_t values as the mapping cookie to the driver. At 149 * this point, we know that both foff and size are a multiple 150 * of the page size. Do a check to avoid wrap. 151 */ 152 size = round_page(size); 153 pindex = OFF_TO_IDX(foff) + OFF_TO_IDX(size); 154 if (pindex > OBJ_MAX_SIZE || pindex < OFF_TO_IDX(foff) || 155 pindex < OFF_TO_IDX(size)) 156 return (NULL); 157 158 if (ops->cdev_pg_ctor(handle, size, prot, foff, cred, &color) != 0) 159 return (NULL); 160 mtx_lock(&dev_pager_mtx); 161 162 /* 163 * Look up pager, creating as necessary. 164 */ 165 object1 = NULL; 166 object = vm_pager_object_lookup(&dev_pager_object_list, handle); 167 if (object == NULL) { 168 /* 169 * Allocate object and associate it with the pager. Initialize 170 * the object's pg_color based upon the physical address of the 171 * device's memory. 172 */ 173 mtx_unlock(&dev_pager_mtx); 174 object1 = vm_object_allocate(tp, pindex); 175 object1->flags |= OBJ_COLORED; 176 object1->pg_color = color; 177 object1->handle = handle; 178 object1->un_pager.devp.ops = ops; 179 object1->un_pager.devp.dev = handle; 180 TAILQ_INIT(&object1->un_pager.devp.devp_pglist); 181 mtx_lock(&dev_pager_mtx); 182 object = vm_pager_object_lookup(&dev_pager_object_list, handle); 183 if (object != NULL) { 184 /* 185 * We raced with other thread while allocating object. 186 */ 187 if (pindex > object->size) 188 object->size = pindex; 189 KASSERT(object->type == tp, 190 ("Inconsistent device pager type %p %d", 191 object, tp)); 192 KASSERT(object->un_pager.devp.ops == ops, 193 ("Inconsistent devops %p %p", object, ops)); 194 } else { 195 object = object1; 196 object1 = NULL; 197 object->handle = handle; 198 TAILQ_INSERT_TAIL(&dev_pager_object_list, object, 199 pager_object_list); 200 if (ops->cdev_pg_populate != NULL) 201 vm_object_set_flag(object, OBJ_POPULATE); 202 } 203 } else { 204 if (pindex > object->size) 205 object->size = pindex; 206 KASSERT(object->type == tp, 207 ("Inconsistent device pager type %p %d", object, tp)); 208 } 209 mtx_unlock(&dev_pager_mtx); 210 if (object1 != NULL) { 211 object1->handle = object1; 212 mtx_lock(&dev_pager_mtx); 213 TAILQ_INSERT_TAIL(&dev_pager_object_list, object1, 214 pager_object_list); 215 mtx_unlock(&dev_pager_mtx); 216 vm_object_deallocate(object1); 217 } 218 return (object); 219 } 220 221 static vm_object_t 222 dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, 223 vm_ooffset_t foff, struct ucred *cred) 224 { 225 226 return (cdev_pager_allocate(handle, OBJT_DEVICE, &old_dev_pager_ops, 227 size, prot, foff, cred)); 228 } 229 230 void 231 cdev_pager_free_page(vm_object_t object, vm_page_t m) 232 { 233 234 VM_OBJECT_ASSERT_WLOCKED(object); 235 if (object->type == OBJT_MGTDEVICE) { 236 KASSERT((m->oflags & VPO_UNMANAGED) == 0, ("unmanaged %p", m)); 237 pmap_remove_all(m); 238 (void)vm_page_remove(m); 239 vm_page_xunbusy(m); 240 } else if (object->type == OBJT_DEVICE) 241 dev_pager_free_page(object, m); 242 } 243 244 static void 245 dev_pager_free_page(vm_object_t object, vm_page_t m) 246 { 247 248 VM_OBJECT_ASSERT_WLOCKED(object); 249 KASSERT((object->type == OBJT_DEVICE && 250 (m->oflags & VPO_UNMANAGED) != 0), 251 ("Managed device or page obj %p m %p", object, m)); 252 TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, plinks.q); 253 vm_page_putfake(m); 254 } 255 256 static void 257 dev_pager_dealloc(vm_object_t object) 258 { 259 vm_page_t m; 260 261 VM_OBJECT_WUNLOCK(object); 262 object->un_pager.devp.ops->cdev_pg_dtor(object->un_pager.devp.dev); 263 264 mtx_lock(&dev_pager_mtx); 265 TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list); 266 mtx_unlock(&dev_pager_mtx); 267 VM_OBJECT_WLOCK(object); 268 269 if (object->type == OBJT_DEVICE) { 270 /* 271 * Free up our fake pages. 272 */ 273 while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist)) 274 != NULL) 275 dev_pager_free_page(object, m); 276 } 277 object->handle = NULL; 278 object->type = OBJT_DEAD; 279 } 280 281 static int 282 dev_pager_getpages(vm_object_t object, vm_page_t *ma, int count, int *rbehind, 283 int *rahead) 284 { 285 int error; 286 287 /* Since our haspage reports zero after/before, the count is 1. */ 288 KASSERT(count == 1, ("%s: count %d", __func__, count)); 289 VM_OBJECT_ASSERT_WLOCKED(object); 290 if (object->un_pager.devp.ops->cdev_pg_fault == NULL) 291 return (VM_PAGER_FAIL); 292 error = object->un_pager.devp.ops->cdev_pg_fault(object, 293 IDX_TO_OFF(ma[0]->pindex), PROT_READ, &ma[0]); 294 295 VM_OBJECT_ASSERT_WLOCKED(object); 296 297 if (error == VM_PAGER_OK) { 298 KASSERT((object->type == OBJT_DEVICE && 299 (ma[0]->oflags & VPO_UNMANAGED) != 0) || 300 (object->type == OBJT_MGTDEVICE && 301 (ma[0]->oflags & VPO_UNMANAGED) == 0), 302 ("Wrong page type %p %p", ma[0], object)); 303 if (object->type == OBJT_DEVICE) { 304 TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist, 305 ma[0], plinks.q); 306 } 307 if (rbehind) 308 *rbehind = 0; 309 if (rahead) 310 *rahead = 0; 311 } 312 313 return (error); 314 } 315 316 static int 317 dev_pager_populate(vm_object_t object, vm_pindex_t pidx, int fault_type, 318 vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last) 319 { 320 321 VM_OBJECT_ASSERT_WLOCKED(object); 322 if (object->un_pager.devp.ops->cdev_pg_populate == NULL) 323 return (VM_PAGER_FAIL); 324 return (object->un_pager.devp.ops->cdev_pg_populate(object, pidx, 325 fault_type, max_prot, first, last)); 326 } 327 328 static int 329 old_dev_pager_fault(vm_object_t object, vm_ooffset_t offset, int prot, 330 vm_page_t *mres) 331 { 332 vm_paddr_t paddr; 333 vm_page_t m_paddr, page; 334 struct cdev *dev; 335 struct cdevsw *csw; 336 struct file *fpop; 337 struct thread *td; 338 vm_memattr_t memattr, memattr1; 339 int ref, ret; 340 341 memattr = object->memattr; 342 343 VM_OBJECT_WUNLOCK(object); 344 345 dev = object->handle; 346 csw = dev_refthread(dev, &ref); 347 if (csw == NULL) { 348 VM_OBJECT_WLOCK(object); 349 return (VM_PAGER_FAIL); 350 } 351 td = curthread; 352 fpop = td->td_fpop; 353 td->td_fpop = NULL; 354 ret = csw->d_mmap(dev, offset, &paddr, prot, &memattr); 355 td->td_fpop = fpop; 356 dev_relthread(dev, ref); 357 if (ret != 0) { 358 printf( 359 "WARNING: dev_pager_getpage: map function returns error %d", ret); 360 VM_OBJECT_WLOCK(object); 361 return (VM_PAGER_FAIL); 362 } 363 364 /* If "paddr" is a real page, perform a sanity check on "memattr". */ 365 if ((m_paddr = vm_phys_paddr_to_vm_page(paddr)) != NULL && 366 (memattr1 = pmap_page_get_memattr(m_paddr)) != memattr) { 367 /* 368 * For the /dev/mem d_mmap routine to return the 369 * correct memattr, pmap_page_get_memattr() needs to 370 * be called, which we do there. 371 */ 372 if ((csw->d_flags & D_MEM) == 0) { 373 printf("WARNING: Device driver %s has set " 374 "\"memattr\" inconsistently (drv %u pmap %u).\n", 375 csw->d_name, memattr, memattr1); 376 } 377 memattr = memattr1; 378 } 379 if (((*mres)->flags & PG_FICTITIOUS) != 0) { 380 /* 381 * If the passed in result page is a fake page, update it with 382 * the new physical address. 383 */ 384 page = *mres; 385 VM_OBJECT_WLOCK(object); 386 vm_page_updatefake(page, paddr, memattr); 387 } else { 388 /* 389 * Replace the passed in reqpage page with our own fake page and 390 * free up the all of the original pages. 391 */ 392 page = vm_page_getfake(paddr, memattr); 393 VM_OBJECT_WLOCK(object); 394 vm_page_replace_checked(page, object, (*mres)->pindex, *mres); 395 vm_page_free(*mres); 396 *mres = page; 397 } 398 vm_page_valid(page); 399 return (VM_PAGER_OK); 400 } 401 402 static void 403 dev_pager_putpages(vm_object_t object, vm_page_t *m, int count, int flags, 404 int *rtvals) 405 { 406 407 panic("dev_pager_putpage called"); 408 } 409 410 static boolean_t 411 dev_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before, 412 int *after) 413 { 414 415 if (before != NULL) 416 *before = 0; 417 if (after != NULL) 418 *after = 0; 419 return (TRUE); 420 } 421 422 static int 423 old_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot, 424 vm_ooffset_t foff, struct ucred *cred, u_short *color) 425 { 426 struct cdev *dev; 427 struct cdevsw *csw; 428 vm_memattr_t dummy; 429 vm_ooffset_t off; 430 vm_paddr_t paddr; 431 unsigned int npages; 432 int ref; 433 434 /* 435 * Make sure this device can be mapped. 436 */ 437 dev = handle; 438 csw = dev_refthread(dev, &ref); 439 if (csw == NULL) 440 return (ENXIO); 441 442 /* 443 * Check that the specified range of the device allows the desired 444 * protection. 445 * 446 * XXX assumes VM_PROT_* == PROT_* 447 */ 448 npages = OFF_TO_IDX(size); 449 paddr = 0; /* Make paddr initialized for the case of size == 0. */ 450 for (off = foff; npages--; off += PAGE_SIZE) { 451 if (csw->d_mmap(dev, off, &paddr, (int)prot, &dummy) != 0) { 452 dev_relthread(dev, ref); 453 return (EINVAL); 454 } 455 } 456 457 dev_ref(dev); 458 dev_relthread(dev, ref); 459 *color = atop(paddr) - OFF_TO_IDX(off - PAGE_SIZE); 460 return (0); 461 } 462 463 static void 464 old_dev_pager_dtor(void *handle) 465 { 466 467 dev_rel(handle); 468 } 469