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