The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/vm/device_pager.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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                 vm_page_lock(m);
  239                 (void)vm_page_remove(m);
  240                 vm_page_unlock(m);
  241         } else if (object->type == OBJT_DEVICE)
  242                 dev_pager_free_page(object, m);
  243 }
  244 
  245 static void
  246 dev_pager_free_page(vm_object_t object, vm_page_t m)
  247 {
  248 
  249         VM_OBJECT_ASSERT_WLOCKED(object);
  250         KASSERT((object->type == OBJT_DEVICE &&
  251             (m->oflags & VPO_UNMANAGED) != 0),
  252             ("Managed device or page obj %p m %p", object, m));
  253         TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, plinks.q);
  254         vm_page_putfake(m);
  255 }
  256 
  257 static void
  258 dev_pager_dealloc(vm_object_t object)
  259 {
  260         vm_page_t m;
  261 
  262         VM_OBJECT_WUNLOCK(object);
  263         object->un_pager.devp.ops->cdev_pg_dtor(object->un_pager.devp.dev);
  264 
  265         mtx_lock(&dev_pager_mtx);
  266         TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list);
  267         mtx_unlock(&dev_pager_mtx);
  268         VM_OBJECT_WLOCK(object);
  269 
  270         if (object->type == OBJT_DEVICE) {
  271                 /*
  272                  * Free up our fake pages.
  273                  */
  274                 while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist))
  275                     != NULL)
  276                         dev_pager_free_page(object, m);
  277         }
  278         object->handle = NULL;
  279         object->type = OBJT_DEAD;
  280 }
  281 
  282 static int
  283 dev_pager_getpages(vm_object_t object, vm_page_t *ma, int count, int *rbehind,
  284     int *rahead)
  285 {
  286         int error;
  287 
  288         /* Since our haspage reports zero after/before, the count is 1. */
  289         KASSERT(count == 1, ("%s: count %d", __func__, count));
  290         VM_OBJECT_ASSERT_WLOCKED(object);
  291         if (object->un_pager.devp.ops->cdev_pg_fault == NULL)
  292                 return (VM_PAGER_FAIL);
  293         error = object->un_pager.devp.ops->cdev_pg_fault(object,
  294             IDX_TO_OFF(ma[0]->pindex), PROT_READ, &ma[0]);
  295 
  296         VM_OBJECT_ASSERT_WLOCKED(object);
  297 
  298         if (error == VM_PAGER_OK) {
  299                 KASSERT((object->type == OBJT_DEVICE &&
  300                      (ma[0]->oflags & VPO_UNMANAGED) != 0) ||
  301                     (object->type == OBJT_MGTDEVICE &&
  302                      (ma[0]->oflags & VPO_UNMANAGED) == 0),
  303                     ("Wrong page type %p %p", ma[0], object));
  304                 if (object->type == OBJT_DEVICE) {
  305                         TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist,
  306                             ma[0], plinks.q);
  307                 }
  308                 if (rbehind)
  309                         *rbehind = 0;
  310                 if (rahead)
  311                         *rahead = 0;
  312         }
  313 
  314         return (error);
  315 }
  316 
  317 static int
  318 dev_pager_populate(vm_object_t object, vm_pindex_t pidx, int fault_type,
  319     vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last)
  320 {
  321 
  322         VM_OBJECT_ASSERT_WLOCKED(object);
  323         if (object->un_pager.devp.ops->cdev_pg_populate == NULL)
  324                 return (VM_PAGER_FAIL);
  325         return (object->un_pager.devp.ops->cdev_pg_populate(object, pidx,
  326             fault_type, max_prot, first, last));
  327 }
  328 
  329 static int
  330 old_dev_pager_fault(vm_object_t object, vm_ooffset_t offset, int prot,
  331     vm_page_t *mres)
  332 {
  333         vm_paddr_t paddr;
  334         vm_page_t m_paddr, page;
  335         struct cdev *dev;
  336         struct cdevsw *csw;
  337         struct file *fpop;
  338         struct thread *td;
  339         vm_memattr_t memattr, memattr1;
  340         int ref, ret;
  341 
  342         memattr = object->memattr;
  343 
  344         VM_OBJECT_WUNLOCK(object);
  345 
  346         dev = object->handle;
  347         csw = dev_refthread(dev, &ref);
  348         if (csw == NULL) {
  349                 VM_OBJECT_WLOCK(object);
  350                 return (VM_PAGER_FAIL);
  351         }
  352         td = curthread;
  353         fpop = td->td_fpop;
  354         td->td_fpop = NULL;
  355         ret = csw->d_mmap(dev, offset, &paddr, prot, &memattr);
  356         td->td_fpop = fpop;
  357         dev_relthread(dev, ref);
  358         if (ret != 0) {
  359                 printf(
  360             "WARNING: dev_pager_getpage: map function returns error %d", ret);
  361                 VM_OBJECT_WLOCK(object);
  362                 return (VM_PAGER_FAIL);
  363         }
  364 
  365         /* If "paddr" is a real page, perform a sanity check on "memattr". */
  366         if ((m_paddr = vm_phys_paddr_to_vm_page(paddr)) != NULL &&
  367             (memattr1 = pmap_page_get_memattr(m_paddr)) != memattr) {
  368                 /*
  369                  * For the /dev/mem d_mmap routine to return the
  370                  * correct memattr, pmap_page_get_memattr() needs to
  371                  * be called, which we do there.
  372                  */
  373                 if ((csw->d_flags & D_MEM) == 0) {
  374                         printf("WARNING: Device driver %s has set "
  375                             "\"memattr\" inconsistently (drv %u pmap %u).\n",
  376                             csw->d_name, memattr, memattr1);
  377                 }
  378                 memattr = memattr1;
  379         }
  380         if (((*mres)->flags & PG_FICTITIOUS) != 0) {
  381                 /*
  382                  * If the passed in result page is a fake page, update it with
  383                  * the new physical address.
  384                  */
  385                 page = *mres;
  386                 VM_OBJECT_WLOCK(object);
  387                 vm_page_updatefake(page, paddr, memattr);
  388         } else {
  389                 /*
  390                  * Replace the passed in reqpage page with our own fake page and
  391                  * free up the all of the original pages.
  392                  */
  393                 page = vm_page_getfake(paddr, memattr);
  394                 VM_OBJECT_WLOCK(object);
  395                 vm_page_replace_checked(page, object, (*mres)->pindex, *mres);
  396                 vm_page_lock(*mres);
  397                 vm_page_free(*mres);
  398                 vm_page_unlock(*mres);
  399                 *mres = page;
  400         }
  401         page->valid = VM_PAGE_BITS_ALL;
  402         return (VM_PAGER_OK);
  403 }
  404 
  405 static void
  406 dev_pager_putpages(vm_object_t object, vm_page_t *m, int count, int flags,
  407     int *rtvals)
  408 {
  409 
  410         panic("dev_pager_putpage called");
  411 }
  412 
  413 static boolean_t
  414 dev_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
  415     int *after)
  416 {
  417 
  418         if (before != NULL)
  419                 *before = 0;
  420         if (after != NULL)
  421                 *after = 0;
  422         return (TRUE);
  423 }
  424 
  425 static int
  426 old_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
  427     vm_ooffset_t foff, struct ucred *cred, u_short *color)
  428 {
  429         struct cdev *dev;
  430         struct cdevsw *csw;
  431         vm_memattr_t dummy;
  432         vm_ooffset_t off;
  433         vm_paddr_t paddr;
  434         unsigned int npages;
  435         int ref;
  436 
  437         /*
  438          * Make sure this device can be mapped.
  439          */
  440         dev = handle;
  441         csw = dev_refthread(dev, &ref);
  442         if (csw == NULL)
  443                 return (ENXIO);
  444 
  445         /*
  446          * Check that the specified range of the device allows the desired
  447          * protection.
  448          *
  449          * XXX assumes VM_PROT_* == PROT_*
  450          */
  451         npages = OFF_TO_IDX(size);
  452         paddr = 0; /* Make paddr initialized for the case of size == 0. */
  453         for (off = foff; npages--; off += PAGE_SIZE) {
  454                 if (csw->d_mmap(dev, off, &paddr, (int)prot, &dummy) != 0) {
  455                         dev_relthread(dev, ref);
  456                         return (EINVAL);
  457                 }
  458         }
  459 
  460         dev_ref(dev);
  461         dev_relthread(dev, ref);
  462         *color = atop(paddr) - OFF_TO_IDX(off - PAGE_SIZE);
  463         return (0);
  464 }
  465 
  466 static void
  467 old_dev_pager_dtor(void *handle)
  468 {
  469 
  470         dev_rel(handle);
  471 }

Cache object: b006998f35b2e81b9097984a13503f13


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.