]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/vm/device_pager.c
Merge compiler-rt trunk r291476.
[FreeBSD/FreeBSD.git] / sys / vm / device_pager.c
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/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         size = round_page(size);
143         pindex = OFF_TO_IDX(foff + size);
144
145         if (ops->cdev_pg_ctor(handle, size, prot, foff, cred, &color) != 0)
146                 return (NULL);
147         mtx_lock(&dev_pager_mtx);
148
149         /*
150          * Look up pager, creating as necessary.
151          */
152         object1 = NULL;
153         object = vm_pager_object_lookup(&dev_pager_object_list, handle);
154         if (object == NULL) {
155                 /*
156                  * Allocate object and associate it with the pager.  Initialize
157                  * the object's pg_color based upon the physical address of the
158                  * device's memory.
159                  */
160                 mtx_unlock(&dev_pager_mtx);
161                 object1 = vm_object_allocate(tp, pindex);
162                 object1->flags |= OBJ_COLORED;
163                 object1->pg_color = color;
164                 object1->handle = handle;
165                 object1->un_pager.devp.ops = ops;
166                 object1->un_pager.devp.dev = handle;
167                 TAILQ_INIT(&object1->un_pager.devp.devp_pglist);
168                 mtx_lock(&dev_pager_mtx);
169                 object = vm_pager_object_lookup(&dev_pager_object_list, handle);
170                 if (object != NULL) {
171                         /*
172                          * We raced with other thread while allocating object.
173                          */
174                         if (pindex > object->size)
175                                 object->size = pindex;
176                         KASSERT(object->type == tp,
177                             ("Inconsistent device pager type %p %d",
178                             object, tp));
179                         KASSERT(object->un_pager.devp.ops == ops,
180                             ("Inconsistent devops %p %p", object, ops));
181                 } else {
182                         object = object1;
183                         object1 = NULL;
184                         object->handle = handle;
185                         TAILQ_INSERT_TAIL(&dev_pager_object_list, object,
186                             pager_object_list);
187                         if (ops->cdev_pg_populate != NULL)
188                                 vm_object_set_flag(object, OBJ_POPULATE);
189                 }
190         } else {
191                 if (pindex > object->size)
192                         object->size = pindex;
193                 KASSERT(object->type == tp,
194                     ("Inconsistent device pager type %p %d", object, tp));
195         }
196         mtx_unlock(&dev_pager_mtx);
197         if (object1 != NULL) {
198                 object1->handle = object1;
199                 mtx_lock(&dev_pager_mtx);
200                 TAILQ_INSERT_TAIL(&dev_pager_object_list, object1,
201                     pager_object_list);
202                 mtx_unlock(&dev_pager_mtx);
203                 vm_object_deallocate(object1);
204         }
205         return (object);
206 }
207
208 static vm_object_t
209 dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
210     vm_ooffset_t foff, struct ucred *cred)
211 {
212
213         return (cdev_pager_allocate(handle, OBJT_DEVICE, &old_dev_pager_ops,
214             size, prot, foff, cred));
215 }
216
217 void
218 cdev_pager_free_page(vm_object_t object, vm_page_t m)
219 {
220
221         VM_OBJECT_ASSERT_WLOCKED(object);
222         if (object->type == OBJT_MGTDEVICE) {
223                 KASSERT((m->oflags & VPO_UNMANAGED) == 0, ("unmanaged %p", m));
224                 pmap_remove_all(m);
225                 vm_page_lock(m);
226                 vm_page_remove(m);
227                 vm_page_unlock(m);
228         } else if (object->type == OBJT_DEVICE)
229                 dev_pager_free_page(object, m);
230 }
231
232 static void
233 dev_pager_free_page(vm_object_t object, vm_page_t m)
234 {
235
236         VM_OBJECT_ASSERT_WLOCKED(object);
237         KASSERT((object->type == OBJT_DEVICE &&
238             (m->oflags & VPO_UNMANAGED) != 0),
239             ("Managed device or page obj %p m %p", object, m));
240         TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, plinks.q);
241         vm_page_putfake(m);
242 }
243
244 static void
245 dev_pager_dealloc(vm_object_t object)
246 {
247         vm_page_t m;
248
249         VM_OBJECT_WUNLOCK(object);
250         object->un_pager.devp.ops->cdev_pg_dtor(object->un_pager.devp.dev);
251
252         mtx_lock(&dev_pager_mtx);
253         TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list);
254         mtx_unlock(&dev_pager_mtx);
255         VM_OBJECT_WLOCK(object);
256
257         if (object->type == OBJT_DEVICE) {
258                 /*
259                  * Free up our fake pages.
260                  */
261                 while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist))
262                     != NULL)
263                         dev_pager_free_page(object, m);
264         }
265         object->handle = NULL;
266         object->type = OBJT_DEAD;
267 }
268
269 static int
270 dev_pager_getpages(vm_object_t object, vm_page_t *ma, int count, int *rbehind,
271     int *rahead)
272 {
273         int error;
274
275         /* Since our haspage reports zero after/before, the count is 1. */
276         KASSERT(count == 1, ("%s: count %d", __func__, count));
277         VM_OBJECT_ASSERT_WLOCKED(object);
278         if (object->un_pager.devp.ops->cdev_pg_fault == NULL)
279                 return (VM_PAGER_FAIL);
280         error = object->un_pager.devp.ops->cdev_pg_fault(object,
281             IDX_TO_OFF(ma[0]->pindex), PROT_READ, &ma[0]);
282
283         VM_OBJECT_ASSERT_WLOCKED(object);
284
285         if (error == VM_PAGER_OK) {
286                 KASSERT((object->type == OBJT_DEVICE &&
287                      (ma[0]->oflags & VPO_UNMANAGED) != 0) ||
288                     (object->type == OBJT_MGTDEVICE &&
289                      (ma[0]->oflags & VPO_UNMANAGED) == 0),
290                     ("Wrong page type %p %p", ma[0], object));
291                 if (object->type == OBJT_DEVICE) {
292                         TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist,
293                             ma[0], plinks.q);
294                 }
295                 if (rbehind)
296                         *rbehind = 0;
297                 if (rahead)
298                         *rahead = 0;
299         }
300
301         return (error);
302 }
303
304 static int
305 dev_pager_populate(vm_object_t object, vm_pindex_t pidx, int fault_type,
306     vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last)
307 {
308
309         VM_OBJECT_ASSERT_WLOCKED(object);
310         if (object->un_pager.devp.ops->cdev_pg_populate == NULL)
311                 return (VM_PAGER_FAIL);
312         return (object->un_pager.devp.ops->cdev_pg_populate(object, pidx,
313             fault_type, max_prot, first, last));
314 }
315
316 static int
317 old_dev_pager_fault(vm_object_t object, vm_ooffset_t offset, int prot,
318     vm_page_t *mres)
319 {
320         vm_paddr_t paddr;
321         vm_page_t m_paddr, page;
322         struct cdev *dev;
323         struct cdevsw *csw;
324         struct file *fpop;
325         struct thread *td;
326         vm_memattr_t memattr, memattr1;
327         int ref, ret;
328
329         memattr = object->memattr;
330
331         VM_OBJECT_WUNLOCK(object);
332
333         dev = object->handle;
334         csw = dev_refthread(dev, &ref);
335         if (csw == NULL) {
336                 VM_OBJECT_WLOCK(object);
337                 return (VM_PAGER_FAIL);
338         }
339         td = curthread;
340         fpop = td->td_fpop;
341         td->td_fpop = NULL;
342         ret = csw->d_mmap(dev, offset, &paddr, prot, &memattr);
343         td->td_fpop = fpop;
344         dev_relthread(dev, ref);
345         if (ret != 0) {
346                 printf(
347             "WARNING: dev_pager_getpage: map function returns error %d", ret);
348                 VM_OBJECT_WLOCK(object);
349                 return (VM_PAGER_FAIL);
350         }
351
352         /* If "paddr" is a real page, perform a sanity check on "memattr". */
353         if ((m_paddr = vm_phys_paddr_to_vm_page(paddr)) != NULL &&
354             (memattr1 = pmap_page_get_memattr(m_paddr)) != memattr) {
355                 /*
356                  * For the /dev/mem d_mmap routine to return the
357                  * correct memattr, pmap_page_get_memattr() needs to
358                  * be called, which we do there.
359                  */
360                 if ((csw->d_flags & D_MEM) == 0) {
361                         printf("WARNING: Device driver %s has set "
362                             "\"memattr\" inconsistently (drv %u pmap %u).\n",
363                             csw->d_name, memattr, memattr1);
364                 }
365                 memattr = memattr1;
366         }
367         if (((*mres)->flags & PG_FICTITIOUS) != 0) {
368                 /*
369                  * If the passed in result page is a fake page, update it with
370                  * the new physical address.
371                  */
372                 page = *mres;
373                 VM_OBJECT_WLOCK(object);
374                 vm_page_updatefake(page, paddr, memattr);
375         } else {
376                 /*
377                  * Replace the passed in reqpage page with our own fake page and
378                  * free up the all of the original pages.
379                  */
380                 page = vm_page_getfake(paddr, memattr);
381                 VM_OBJECT_WLOCK(object);
382                 vm_page_replace_checked(page, object, (*mres)->pindex, *mres);
383                 vm_page_lock(*mres);
384                 vm_page_free(*mres);
385                 vm_page_unlock(*mres);
386                 *mres = page;
387         }
388         page->valid = VM_PAGE_BITS_ALL;
389         return (VM_PAGER_OK);
390 }
391
392 static void
393 dev_pager_putpages(vm_object_t object, vm_page_t *m, int count, int flags,
394     int *rtvals)
395 {
396
397         panic("dev_pager_putpage called");
398 }
399
400 static boolean_t
401 dev_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
402     int *after)
403 {
404
405         if (before != NULL)
406                 *before = 0;
407         if (after != NULL)
408                 *after = 0;
409         return (TRUE);
410 }
411
412 static int
413 old_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
414     vm_ooffset_t foff, struct ucred *cred, u_short *color)
415 {
416         struct cdev *dev;
417         struct cdevsw *csw;
418         vm_memattr_t dummy;
419         vm_ooffset_t off;
420         vm_paddr_t paddr;
421         unsigned int npages;
422         int ref;
423
424         /*
425          * Make sure this device can be mapped.
426          */
427         dev = handle;
428         csw = dev_refthread(dev, &ref);
429         if (csw == NULL)
430                 return (ENXIO);
431
432         /*
433          * Check that the specified range of the device allows the desired
434          * protection.
435          *
436          * XXX assumes VM_PROT_* == PROT_*
437          */
438         npages = OFF_TO_IDX(size);
439         paddr = 0; /* Make paddr initialized for the case of size == 0. */
440         for (off = foff; npages--; off += PAGE_SIZE) {
441                 if (csw->d_mmap(dev, off, &paddr, (int)prot, &dummy) != 0) {
442                         dev_relthread(dev, ref);
443                         return (EINVAL);
444                 }
445         }
446
447         dev_ref(dev);
448         dev_relthread(dev, ref);
449         *color = atop(paddr) - OFF_TO_IDX(off - PAGE_SIZE);
450         return (0);
451 }
452
453 static void
454 old_dev_pager_dtor(void *handle)
455 {
456
457         dev_rel(handle);
458 }