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

Cache object: f64174545779db4e388b0c3bba8c521f


[ 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.