]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/vm/sg_pager.c
MFC r321985:
[FreeBSD/stable/9.git] / sys / vm / sg_pager.c
1 /*-
2  * Copyright (c) 2009 Hudson River Trading 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$");
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_param.h>
42 #include <vm/vm_object.h>
43 #include <vm/vm_page.h>
44 #include <vm/vm_pager.h>
45 #include <vm/uma.h>
46
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 struct pagerops sgpagerops = {
57         .pgo_alloc =    sg_pager_alloc,
58         .pgo_dealloc =  sg_pager_dealloc,
59         .pgo_getpages = sg_pager_getpages,
60         .pgo_putpages = sg_pager_putpages,
61         .pgo_haspage =  sg_pager_haspage,
62 };
63
64 static vm_object_t
65 sg_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
66     vm_ooffset_t foff, struct ucred *cred)
67 {
68         struct sglist *sg;
69         vm_object_t object;
70         vm_pindex_t npages, pindex;
71         int i;
72
73         /*
74          * Offset should be page aligned.
75          */
76         if (foff & PAGE_MASK)
77                 return (NULL);
78
79         /*
80          * The scatter/gather list must only include page-aligned
81          * ranges.
82          */
83         npages = 0;
84         sg = handle;
85         for (i = 0; i < sg->sg_nseg; i++) {
86                 if ((sg->sg_segs[i].ss_paddr % PAGE_SIZE) != 0 ||
87                     (sg->sg_segs[i].ss_len % PAGE_SIZE) != 0)
88                         return (NULL);
89                 npages += sg->sg_segs[i].ss_len / PAGE_SIZE;
90         }
91
92         /*
93          * The scatter/gather list has a fixed size.  Refuse requests
94          * to map beyond that.
95          */
96         size = round_page(size);
97         pindex = OFF_TO_IDX(foff + size);
98         if (pindex > npages)
99                 return (NULL);
100
101         /*
102          * Allocate a new object and associate it with the
103          * scatter/gather list.  It is ok for our purposes to have
104          * multiple VM objects associated with the same scatter/gather
105          * list because scatter/gather lists are static.  This is also
106          * simpler than ensuring a unique object per scatter/gather
107          * list.
108          */
109         object = vm_object_allocate(OBJT_SG, npages);
110         object->handle = sglist_hold(sg);
111         TAILQ_INIT(&object->un_pager.sgp.sgp_pglist);
112         return (object);
113 }
114
115 static void
116 sg_pager_dealloc(vm_object_t object)
117 {
118         struct sglist *sg;
119         vm_page_t m;
120
121         /*
122          * Free up our fake pages.
123          */
124         while ((m = TAILQ_FIRST(&object->un_pager.sgp.sgp_pglist)) != 0) {
125                 TAILQ_REMOVE(&object->un_pager.sgp.sgp_pglist, m, pageq);
126                 vm_page_putfake(m);
127         }
128         
129         sg = object->handle;
130         sglist_free(sg);
131         object->handle = NULL;
132         object->type = OBJT_DEAD;
133 }
134
135 static int
136 sg_pager_getpages(vm_object_t object, vm_page_t *m, int count, int reqpage)
137 {
138         struct sglist *sg;
139         vm_page_t m_paddr, page;
140         vm_pindex_t offset;
141         vm_paddr_t paddr;
142         vm_memattr_t memattr;
143         size_t space;
144         int i;
145
146         VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
147         sg = object->handle;
148         memattr = object->memattr;
149         VM_OBJECT_UNLOCK(object);
150         offset = m[reqpage]->pindex;
151
152         /*
153          * Lookup the physical address of the requested page.  An initial
154          * value of '1' instead of '0' is used so we can assert that the
155          * page is found since '0' can be a valid page-aligned physical
156          * address.
157          */
158         space = 0;
159         paddr = 1;
160         for (i = 0; i < sg->sg_nseg; i++) {
161                 if (space + sg->sg_segs[i].ss_len <= (offset * PAGE_SIZE)) {
162                         space += sg->sg_segs[i].ss_len;
163                         continue;
164                 }
165                 paddr = sg->sg_segs[i].ss_paddr + offset * PAGE_SIZE - space;
166                 break;
167         }
168         KASSERT(paddr != 1, ("invalid SG page index"));
169
170         /* If "paddr" is a real page, perform a sanity check on "memattr". */
171         if ((m_paddr = vm_phys_paddr_to_vm_page(paddr)) != NULL &&
172             pmap_page_get_memattr(m_paddr) != memattr) {
173                 memattr = pmap_page_get_memattr(m_paddr);
174                 printf(
175             "WARNING: A device driver has set \"memattr\" inconsistently.\n");
176         }
177
178         /* Return a fake page for the requested page. */
179         KASSERT(!(m[reqpage]->flags & PG_FICTITIOUS),
180             ("backing page for SG is fake"));
181
182         /* Construct a new fake page. */
183         page = vm_page_getfake(paddr, memattr);
184         VM_OBJECT_LOCK(object);
185         TAILQ_INSERT_TAIL(&object->un_pager.sgp.sgp_pglist, page, pageq);
186
187         /* Free the original pages and insert this fake page into the object. */
188         for (i = 0; i < count; i++) {
189                 vm_page_lock(m[i]);
190                 vm_page_free(m[i]);
191                 vm_page_unlock(m[i]);
192         }
193         vm_page_insert(page, object, offset);
194         m[reqpage] = page;
195         page->valid = VM_PAGE_BITS_ALL;
196
197         return (VM_PAGER_OK);
198 }
199
200 static void
201 sg_pager_putpages(vm_object_t object, vm_page_t *m, int count,
202     boolean_t sync, int *rtvals)
203 {
204
205         panic("sg_pager_putpage called");
206 }
207
208 static boolean_t
209 sg_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
210     int *after)
211 {
212
213         if (before != NULL)
214                 *before = 0;
215         if (after != NULL)
216                 *after = 0;
217         return (TRUE);
218 }