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$");
   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/sx.h>
   48 
   49 #include <vm/vm.h>
   50 #include <vm/vm_object.h>
   51 #include <vm/vm_page.h>
   52 #include <vm/vm_pager.h>
   53 #include <vm/uma.h>
   54 
   55 static void dev_pager_init(void);
   56 static vm_object_t dev_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
   57                 vm_ooffset_t);
   58 static void dev_pager_dealloc(vm_object_t);
   59 static int dev_pager_getpages(vm_object_t, vm_page_t *, int, int);
   60 static void dev_pager_putpages(vm_object_t, vm_page_t *, int, 
   61                 boolean_t, int *);
   62 static boolean_t dev_pager_haspage(vm_object_t, vm_pindex_t, int *,
   63                 int *);
   64 
   65 /* list of device pager objects */
   66 static struct pagerlst dev_pager_object_list;
   67 /* protect list manipulation */
   68 static struct mtx dev_pager_mtx;
   69 
   70 
   71 static uma_zone_t fakepg_zone;
   72 
   73 static vm_page_t dev_pager_getfake(vm_paddr_t);
   74 static void dev_pager_putfake(vm_page_t);
   75 static void dev_pager_updatefake(vm_page_t, vm_paddr_t);
   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 static void
   87 dev_pager_init()
   88 {
   89         TAILQ_INIT(&dev_pager_object_list);
   90         mtx_init(&dev_pager_mtx, "dev_pager list", NULL, MTX_DEF);
   91         fakepg_zone = uma_zcreate("DP fakepg", sizeof(struct vm_page),
   92             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
   93             UMA_ZONE_NOFREE|UMA_ZONE_VM); 
   94 }
   95 
   96 /*
   97  * MPSAFE
   98  */
   99 static vm_object_t
  100 dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff)
  101 {
  102         struct cdev *dev;
  103         vm_object_t object, object1;
  104         vm_pindex_t pindex;
  105         unsigned int npages;
  106         vm_paddr_t paddr;
  107         vm_offset_t off;
  108         struct cdevsw *csw;
  109 
  110         /*
  111          * Offset should be page aligned.
  112          */
  113         if (foff & PAGE_MASK)
  114                 return (NULL);
  115 
  116         size = round_page(size);
  117         pindex = OFF_TO_IDX(foff + size);
  118 
  119         /*
  120          * Make sure this device can be mapped.
  121          */
  122         dev = handle;
  123         csw = dev_refthread(dev);
  124         if (csw == NULL)
  125                 return (NULL);
  126 
  127         /*
  128          * Check that the specified range of the device allows the desired
  129          * protection.
  130          *
  131          * XXX assumes VM_PROT_* == PROT_*
  132          */
  133         npages = OFF_TO_IDX(size);
  134         for (off = foff; npages--; off += PAGE_SIZE)
  135                 if ((*csw->d_mmap)(dev, off, &paddr, (int)prot) != 0) {
  136                         dev_relthread(dev);
  137                         return (NULL);
  138                 }
  139 
  140         mtx_lock(&dev_pager_mtx);
  141 
  142         /*
  143          * Look up pager, creating as necessary.
  144          */
  145         object1 = NULL;
  146         object = vm_pager_object_lookup(&dev_pager_object_list, handle);
  147         if (object == NULL) {
  148                 /*
  149                  * Allocate object and associate it with the pager.
  150                  */
  151                 mtx_unlock(&dev_pager_mtx);
  152                 object1 = vm_object_allocate(OBJT_DEVICE, pindex);
  153                 mtx_lock(&dev_pager_mtx);
  154                 object = vm_pager_object_lookup(&dev_pager_object_list, handle);
  155                 if (object != NULL) {
  156                         /*
  157                          * We raced with other thread while allocating object.
  158                          */
  159                         if (pindex > object->size)
  160                                 object->size = pindex;
  161                 } else {
  162                         object = object1;
  163                         object1 = NULL;
  164                         object->handle = handle;
  165                         TAILQ_INIT(&object->un_pager.devp.devp_pglist);
  166                         TAILQ_INSERT_TAIL(&dev_pager_object_list, object,
  167                             pager_object_list);
  168                 }
  169         } else {
  170                 if (pindex > object->size)
  171                         object->size = pindex;
  172         }
  173         mtx_unlock(&dev_pager_mtx);
  174         dev_relthread(dev);
  175         vm_object_deallocate(object1);
  176         return (object);
  177 }
  178 
  179 static void
  180 dev_pager_dealloc(object)
  181         vm_object_t object;
  182 {
  183         vm_page_t m;
  184 
  185         VM_OBJECT_UNLOCK(object);
  186         mtx_lock(&dev_pager_mtx);
  187         TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list);
  188         mtx_unlock(&dev_pager_mtx);
  189         VM_OBJECT_LOCK(object);
  190         /*
  191          * Free up our fake pages.
  192          */
  193         while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist)) != 0) {
  194                 TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, pageq);
  195                 dev_pager_putfake(m);
  196         }
  197 }
  198 
  199 static int
  200 dev_pager_getpages(object, m, count, reqpage)
  201         vm_object_t object;
  202         vm_page_t *m;
  203         int count;
  204         int reqpage;
  205 {
  206         vm_pindex_t offset;
  207         vm_paddr_t paddr;
  208         vm_page_t page;
  209         struct cdev *dev;
  210         int i, ret;
  211         int prot;
  212         struct cdevsw *csw;
  213         struct thread *td;
  214         struct file *fpop;
  215 
  216         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
  217         dev = object->handle;
  218         offset = m[reqpage]->pindex;
  219         VM_OBJECT_UNLOCK(object);
  220         csw = dev_refthread(dev);
  221         if (csw == NULL)
  222                 panic("dev_pager_getpage: no cdevsw");
  223         prot = PROT_READ;       /* XXX should pass in? */
  224 
  225         td = curthread;
  226         fpop = td->td_fpop;
  227         td->td_fpop = NULL;
  228         ret = (*csw->d_mmap)(dev, (vm_offset_t)offset << PAGE_SHIFT, &paddr, prot);
  229         KASSERT(ret == 0, ("dev_pager_getpage: map function returns error"));
  230         td->td_fpop = fpop;
  231         dev_relthread(dev);
  232 
  233         if ((m[reqpage]->flags & PG_FICTITIOUS) != 0) {
  234                 /*
  235                  * If the passed in reqpage page is a fake page, update it with
  236                  * the new physical address.
  237                  */
  238                 VM_OBJECT_LOCK(object);
  239                 dev_pager_updatefake(m[reqpage], paddr);
  240                 if (count > 1) {
  241                         vm_page_lock_queues();
  242                         for (i = 0; i < count; i++) {
  243                                 if (i != reqpage)
  244                                         vm_page_free(m[i]);
  245                         }
  246                         vm_page_unlock_queues();
  247                 }
  248         } else {
  249                 /*
  250                  * Replace the passed in reqpage page with our own fake page and
  251                  * free up the all of the original pages.
  252                  */
  253                 page = dev_pager_getfake(paddr);
  254                 VM_OBJECT_LOCK(object);
  255                 TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist, page, pageq);
  256                 vm_page_lock_queues();
  257                 for (i = 0; i < count; i++)
  258                         vm_page_free(m[i]);
  259                 vm_page_unlock_queues();
  260                 vm_page_insert(page, object, offset);
  261                 m[reqpage] = page;
  262         }
  263 
  264         return (VM_PAGER_OK);
  265 }
  266 
  267 static void
  268 dev_pager_putpages(object, m, count, sync, rtvals)
  269         vm_object_t object;
  270         vm_page_t *m;
  271         int count;
  272         boolean_t sync;
  273         int *rtvals;
  274 {
  275         panic("dev_pager_putpage called");
  276 }
  277 
  278 static boolean_t
  279 dev_pager_haspage(object, pindex, before, after)
  280         vm_object_t object;
  281         vm_pindex_t pindex;
  282         int *before;
  283         int *after;
  284 {
  285         if (before != NULL)
  286                 *before = 0;
  287         if (after != NULL)
  288                 *after = 0;
  289         return (TRUE);
  290 }
  291 
  292 /*
  293  * Instantiate a fictitious page.  Unlike physical memory pages, only
  294  * the machine-independent fields must be initialized.
  295  */
  296 static vm_page_t
  297 dev_pager_getfake(paddr)
  298         vm_paddr_t paddr;
  299 {
  300         vm_page_t m;
  301 
  302         m = uma_zalloc(fakepg_zone, M_WAITOK);
  303 
  304         m->flags = PG_FICTITIOUS;
  305         m->oflags = VPO_BUSY;
  306         m->valid = VM_PAGE_BITS_ALL;
  307         m->dirty = 0;
  308         m->busy = 0;
  309         m->queue = PQ_NONE;
  310         m->object = NULL;
  311 
  312         m->wire_count = 1;
  313         m->hold_count = 0;
  314         m->phys_addr = paddr;
  315 
  316         return (m);
  317 }
  318 
  319 static void
  320 dev_pager_putfake(m)
  321         vm_page_t m;
  322 {
  323         if (!(m->flags & PG_FICTITIOUS))
  324                 panic("dev_pager_putfake: bad page");
  325         uma_zfree(fakepg_zone, m);
  326 }
  327 
  328 static void
  329 dev_pager_updatefake(m, paddr)
  330         vm_page_t m;
  331         vm_paddr_t paddr;
  332 {
  333         if (!(m->flags & PG_FICTITIOUS))
  334                 panic("dev_pager_updatefake: bad page");
  335         m->phys_addr = paddr;
  336         m->valid = VM_PAGE_BITS_ALL;
  337 }

Cache object: 00daf9c47c9b0d42995250d13ae23a14


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