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/sg_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) 2009 Advanced Computing Technologies LLC
    3  * Written by: John H. Baldwin <jhb@FreeBSD.org>
    4  * All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   25  * SUCH DAMAGE.
   26  */
   27 
   28 #include <sys/cdefs.h>
   29 __FBSDID("$FreeBSD: releng/8.1/sys/vm/sg_pager.c 199583 2009-11-20 15:27:52Z jhb $");
   30 
   31 /*
   32  * This pager manages OBJT_SG objects.  These objects are backed by
   33  * a scatter/gather list of physical address ranges.
   34  */
   35 
   36 #include <sys/param.h>
   37 #include <sys/lock.h>
   38 #include <sys/mutex.h>
   39 #include <sys/sglist.h>
   40 #include <vm/vm.h>
   41 #include <vm/vm_object.h>
   42 #include <vm/vm_page.h>
   43 #include <vm/vm_pager.h>
   44 #include <vm/uma.h>
   45 
   46 static void sg_pager_init(void);
   47 static vm_object_t sg_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
   48     vm_ooffset_t, struct ucred *);
   49 static void sg_pager_dealloc(vm_object_t);
   50 static int sg_pager_getpages(vm_object_t, vm_page_t *, int, int);
   51 static void sg_pager_putpages(vm_object_t, vm_page_t *, int, 
   52                 boolean_t, int *);
   53 static boolean_t sg_pager_haspage(vm_object_t, vm_pindex_t, int *,
   54                 int *);
   55 
   56 static uma_zone_t fakepg_zone;
   57 
   58 static vm_page_t sg_pager_getfake(vm_paddr_t, vm_memattr_t);
   59 static void sg_pager_putfake(vm_page_t);
   60 
   61 struct pagerops sgpagerops = {
   62         .pgo_init =     sg_pager_init,
   63         .pgo_alloc =    sg_pager_alloc,
   64         .pgo_dealloc =  sg_pager_dealloc,
   65         .pgo_getpages = sg_pager_getpages,
   66         .pgo_putpages = sg_pager_putpages,
   67         .pgo_haspage =  sg_pager_haspage,
   68 };
   69 
   70 static void
   71 sg_pager_init(void)
   72 {
   73 
   74         fakepg_zone = uma_zcreate("SG fakepg", sizeof(struct vm_page),
   75             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
   76             UMA_ZONE_NOFREE|UMA_ZONE_VM); 
   77 }
   78 
   79 static vm_object_t
   80 sg_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
   81     vm_ooffset_t foff, struct ucred *cred)
   82 {
   83         struct sglist *sg;
   84         vm_object_t object;
   85         vm_pindex_t npages, pindex;
   86         int i;
   87 
   88         /*
   89          * Offset should be page aligned.
   90          */
   91         if (foff & PAGE_MASK)
   92                 return (NULL);
   93 
   94         /*
   95          * The scatter/gather list must only include page-aligned
   96          * ranges.
   97          */
   98         npages = 0;
   99         sg = handle;
  100         for (i = 0; i < sg->sg_nseg; i++) {
  101                 if ((sg->sg_segs[i].ss_paddr % PAGE_SIZE) != 0 ||
  102                     (sg->sg_segs[i].ss_len % PAGE_SIZE) != 0)
  103                         return (NULL);
  104                 npages += sg->sg_segs[i].ss_len / PAGE_SIZE;
  105         }
  106 
  107         /*
  108          * The scatter/gather list has a fixed size.  Refuse requests
  109          * to map beyond that.
  110          */
  111         size = round_page(size);
  112         pindex = OFF_TO_IDX(foff + size);
  113         if (pindex > npages)
  114                 return (NULL);
  115 
  116         /*
  117          * Allocate a new object and associate it with the
  118          * scatter/gather list.  It is ok for our purposes to have
  119          * multiple VM objects associated with the same scatter/gather
  120          * list because scatter/gather lists are static.  This is also
  121          * simpler than ensuring a unique object per scatter/gather
  122          * list.
  123          */
  124         object = vm_object_allocate(OBJT_SG, npages);
  125         object->handle = sglist_hold(sg);
  126         TAILQ_INIT(&object->un_pager.sgp.sgp_pglist);
  127         return (object);
  128 }
  129 
  130 static void
  131 sg_pager_dealloc(vm_object_t object)
  132 {
  133         struct sglist *sg;
  134         vm_page_t m;
  135 
  136         /*
  137          * Free up our fake pages.
  138          */
  139         while ((m = TAILQ_FIRST(&object->un_pager.sgp.sgp_pglist)) != 0) {
  140                 TAILQ_REMOVE(&object->un_pager.sgp.sgp_pglist, m, pageq);
  141                 sg_pager_putfake(m);
  142         }
  143         
  144         sg = object->handle;
  145         sglist_free(sg);
  146 }
  147 
  148 static int
  149 sg_pager_getpages(vm_object_t object, vm_page_t *m, int count, int reqpage)
  150 {
  151         struct sglist *sg;
  152         vm_page_t m_paddr, page;
  153         vm_pindex_t offset;
  154         vm_paddr_t paddr;
  155         vm_memattr_t memattr;
  156         size_t space;
  157         int i;
  158 
  159         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
  160         sg = object->handle;
  161         memattr = object->memattr;
  162         VM_OBJECT_UNLOCK(object);
  163         offset = m[reqpage]->pindex;
  164 
  165         /*
  166          * Lookup the physical address of the requested page.  An initial
  167          * value of '1' instead of '' is used so we can assert that the
  168          * page is found since '' can be a valid page-aligned physical
  169          * address.
  170          */
  171         space = 0;
  172         paddr = 1;
  173         for (i = 0; i < sg->sg_nseg; i++) {
  174                 if (space + sg->sg_segs[i].ss_len <= (offset * PAGE_SIZE)) {
  175                         space += sg->sg_segs[i].ss_len;
  176                         continue;
  177                 }
  178                 paddr = sg->sg_segs[i].ss_paddr + offset * PAGE_SIZE - space;
  179                 break;
  180         }
  181         KASSERT(paddr != 1, ("invalid SG page index"));
  182 
  183         /* If "paddr" is a real page, perform a sanity check on "memattr". */
  184         if ((m_paddr = vm_phys_paddr_to_vm_page(paddr)) != NULL &&
  185             pmap_page_get_memattr(m_paddr) != memattr) {
  186                 memattr = pmap_page_get_memattr(m_paddr);
  187                 printf(
  188             "WARNING: A device driver has set \"memattr\" inconsistently.\n");
  189         }
  190 
  191         /* Return a fake page for the requested page. */
  192         KASSERT(!(m[reqpage]->flags & PG_FICTITIOUS),
  193             ("backing page for SG is fake"));
  194 
  195         /* Construct a new fake page. */
  196         page = sg_pager_getfake(paddr, memattr);
  197         VM_OBJECT_LOCK(object);
  198         TAILQ_INSERT_TAIL(&object->un_pager.sgp.sgp_pglist, page, pageq);
  199 
  200         /* Free the original pages and insert this fake page into the object. */
  201         vm_page_lock_queues();
  202         for (i = 0; i < count; i++)
  203                 vm_page_free(m[i]);
  204         vm_page_unlock_queues();
  205         vm_page_insert(page, object, offset);
  206         m[reqpage] = page;
  207         page->valid = VM_PAGE_BITS_ALL;
  208 
  209         return (VM_PAGER_OK);
  210 }
  211 
  212 static void
  213 sg_pager_putpages(vm_object_t object, vm_page_t *m, int count,
  214     boolean_t sync, int *rtvals)
  215 {
  216 
  217         panic("sg_pager_putpage called");
  218 }
  219 
  220 static boolean_t
  221 sg_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
  222     int *after)
  223 {
  224 
  225         if (before != NULL)
  226                 *before = 0;
  227         if (after != NULL)
  228                 *after = 0;
  229         return (TRUE);
  230 }
  231 
  232 /*
  233  * Create a fictitious page with the specified physical address and memory
  234  * attribute.  The memory attribute is the only the machine-dependent aspect
  235  * of a fictitious page that must be initialized.
  236  */
  237 static vm_page_t
  238 sg_pager_getfake(vm_paddr_t paddr, vm_memattr_t memattr)
  239 {
  240         vm_page_t m;
  241 
  242         m = uma_zalloc(fakepg_zone, M_WAITOK | M_ZERO);
  243         m->phys_addr = paddr;
  244         /* Fictitious pages don't use "segind". */
  245         m->flags = PG_FICTITIOUS;
  246         /* Fictitious pages don't use "order" or "pool". */
  247         m->oflags = VPO_BUSY;
  248         m->wire_count = 1;
  249         pmap_page_set_memattr(m, memattr);
  250         return (m);
  251 }
  252 
  253 static void
  254 sg_pager_putfake(vm_page_t m)
  255 {
  256 
  257         if (!(m->flags & PG_FICTITIOUS))
  258                 panic("sg_pager_putfake: bad page");
  259         uma_zfree(fakepg_zone, m);
  260 }

Cache object: eb1116a040dda2a442d0dd9e08c6279c


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