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