xref: /freebsd/sys/vm/device_pager.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
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 #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 #include <sys/user.h>
50 #include <sys/vmmeter.h>
51 
52 #include <vm/vm.h>
53 #include <vm/vm_param.h>
54 #include <vm/vm_object.h>
55 #include <vm/vm_page.h>
56 #include <vm/vm_pager.h>
57 #include <vm/vm_phys.h>
58 #include <vm/uma.h>
59 
60 static void dev_pager_init(void);
61 static vm_object_t dev_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
62     vm_ooffset_t, struct ucred *);
63 static void dev_pager_dealloc(vm_object_t);
64 static int dev_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
65 static void dev_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
66 static boolean_t dev_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
67 static void dev_pager_free_page(vm_object_t object, vm_page_t m);
68 static int dev_pager_populate(vm_object_t object, vm_pindex_t pidx,
69     int fault_type, vm_prot_t, vm_pindex_t *first, vm_pindex_t *last);
70 
71 /* list of device pager objects */
72 static struct pagerlst dev_pager_object_list;
73 /* protect list manipulation */
74 static struct mtx dev_pager_mtx;
75 
76 const struct pagerops devicepagerops = {
77 	.pgo_kvme_type = KVME_TYPE_DEVICE,
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 const struct pagerops mgtdevicepagerops = {
87 	.pgo_kvme_type = KVME_TYPE_MGTDEVICE,
88 	.pgo_alloc =	dev_pager_alloc,
89 	.pgo_dealloc =	dev_pager_dealloc,
90 	.pgo_getpages =	dev_pager_getpages,
91 	.pgo_putpages =	dev_pager_putpages,
92 	.pgo_haspage =	dev_pager_haspage,
93 	.pgo_populate =	dev_pager_populate,
94 };
95 
96 static int old_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
97     vm_ooffset_t foff, struct ucred *cred, u_short *color);
98 static void old_dev_pager_dtor(void *handle);
99 static int old_dev_pager_fault(vm_object_t object, vm_ooffset_t offset,
100     int prot, vm_page_t *mres);
101 
102 static const struct cdev_pager_ops old_dev_pager_ops = {
103 	.cdev_pg_ctor =	old_dev_pager_ctor,
104 	.cdev_pg_dtor =	old_dev_pager_dtor,
105 	.cdev_pg_fault = old_dev_pager_fault
106 };
107 
108 static void
109 dev_pager_init(void)
110 {
111 
112 	TAILQ_INIT(&dev_pager_object_list);
113 	mtx_init(&dev_pager_mtx, "dev_pager list", NULL, MTX_DEF);
114 }
115 
116 vm_object_t
117 cdev_pager_lookup(void *handle)
118 {
119 	vm_object_t object;
120 
121 	mtx_lock(&dev_pager_mtx);
122 	object = vm_pager_object_lookup(&dev_pager_object_list, handle);
123 	mtx_unlock(&dev_pager_mtx);
124 	return (object);
125 }
126 
127 vm_object_t
128 cdev_pager_allocate(void *handle, enum obj_type tp,
129     const struct cdev_pager_ops *ops, vm_ooffset_t size, vm_prot_t prot,
130     vm_ooffset_t foff, struct ucred *cred)
131 {
132 	vm_object_t object, object1;
133 	vm_pindex_t pindex;
134 	u_short color;
135 
136 	if (tp != OBJT_DEVICE && tp != OBJT_MGTDEVICE)
137 		return (NULL);
138 	KASSERT(tp == OBJT_MGTDEVICE || ops->cdev_pg_populate == NULL,
139 	    ("populate on unmanaged device pager"));
140 
141 	/*
142 	 * Offset should be page aligned.
143 	 */
144 	if (foff & PAGE_MASK)
145 		return (NULL);
146 
147 	/*
148 	 * Treat the mmap(2) file offset as an unsigned value for a
149 	 * device mapping.  This, in effect, allows a user to pass all
150 	 * possible off_t values as the mapping cookie to the driver.  At
151 	 * this point, we know that both foff and size are a multiple
152 	 * of the page size.  Do a check to avoid wrap.
153 	 */
154 	size = round_page(size);
155 	pindex = OFF_TO_IDX(foff) + OFF_TO_IDX(size);
156 	if (pindex > OBJ_MAX_SIZE || pindex < OFF_TO_IDX(foff) ||
157 	    pindex < OFF_TO_IDX(size))
158 		return (NULL);
159 
160 	if (ops->cdev_pg_ctor(handle, size, prot, foff, cred, &color) != 0)
161 		return (NULL);
162 	mtx_lock(&dev_pager_mtx);
163 
164 	/*
165 	 * Look up pager, creating as necessary.
166 	 */
167 	object1 = NULL;
168 	object = vm_pager_object_lookup(&dev_pager_object_list, handle);
169 	if (object == NULL) {
170 		/*
171 		 * Allocate object and associate it with the pager.  Initialize
172 		 * the object's pg_color based upon the physical address of the
173 		 * device's memory.
174 		 */
175 		mtx_unlock(&dev_pager_mtx);
176 		object1 = vm_object_allocate(tp, pindex);
177 		object1->flags |= OBJ_COLORED;
178 		object1->pg_color = color;
179 		object1->handle = handle;
180 		object1->un_pager.devp.ops = ops;
181 		object1->un_pager.devp.dev = handle;
182 		TAILQ_INIT(&object1->un_pager.devp.devp_pglist);
183 		mtx_lock(&dev_pager_mtx);
184 		object = vm_pager_object_lookup(&dev_pager_object_list, handle);
185 		if (object != NULL) {
186 			/*
187 			 * We raced with other thread while allocating object.
188 			 */
189 			if (pindex > object->size)
190 				object->size = pindex;
191 			KASSERT(object->type == tp,
192 			    ("Inconsistent device pager type %p %d",
193 			    object, tp));
194 			KASSERT(object->un_pager.devp.ops == ops,
195 			    ("Inconsistent devops %p %p", object, ops));
196 		} else {
197 			object = object1;
198 			object1 = NULL;
199 			object->handle = handle;
200 			TAILQ_INSERT_TAIL(&dev_pager_object_list, object,
201 			    pager_object_list);
202 			if (ops->cdev_pg_populate != NULL)
203 				vm_object_set_flag(object, OBJ_POPULATE);
204 		}
205 	} else {
206 		if (pindex > object->size)
207 			object->size = pindex;
208 		KASSERT(object->type == tp,
209 		    ("Inconsistent device pager type %p %d", object, tp));
210 	}
211 	mtx_unlock(&dev_pager_mtx);
212 	if (object1 != NULL) {
213 		object1->handle = object1;
214 		mtx_lock(&dev_pager_mtx);
215 		TAILQ_INSERT_TAIL(&dev_pager_object_list, object1,
216 		    pager_object_list);
217 		mtx_unlock(&dev_pager_mtx);
218 		vm_object_deallocate(object1);
219 	}
220 	return (object);
221 }
222 
223 static vm_object_t
224 dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
225     vm_ooffset_t foff, struct ucred *cred)
226 {
227 
228 	return (cdev_pager_allocate(handle, OBJT_DEVICE, &old_dev_pager_ops,
229 	    size, prot, foff, cred));
230 }
231 
232 void
233 cdev_pager_free_page(vm_object_t object, vm_page_t m)
234 {
235 
236 	VM_OBJECT_ASSERT_WLOCKED(object);
237 	if (object->type == OBJT_MGTDEVICE) {
238 		KASSERT((m->oflags & VPO_UNMANAGED) == 0, ("unmanaged %p", m));
239 		pmap_remove_all(m);
240 		(void)vm_page_remove(m);
241 	} else if (object->type == OBJT_DEVICE)
242 		dev_pager_free_page(object, m);
243 }
244 
245 static void
246 dev_pager_free_page(vm_object_t object, vm_page_t m)
247 {
248 
249 	VM_OBJECT_ASSERT_WLOCKED(object);
250 	KASSERT((object->type == OBJT_DEVICE &&
251 	    (m->oflags & VPO_UNMANAGED) != 0),
252 	    ("Managed device or page obj %p m %p", object, m));
253 	TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, plinks.q);
254 	vm_page_putfake(m);
255 }
256 
257 static void
258 dev_pager_dealloc(vm_object_t object)
259 {
260 	vm_page_t m;
261 
262 	VM_OBJECT_WUNLOCK(object);
263 	object->un_pager.devp.ops->cdev_pg_dtor(object->un_pager.devp.dev);
264 
265 	mtx_lock(&dev_pager_mtx);
266 	TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list);
267 	mtx_unlock(&dev_pager_mtx);
268 	VM_OBJECT_WLOCK(object);
269 
270 	if (object->type == OBJT_DEVICE) {
271 		/*
272 		 * Free up our fake pages.
273 		 */
274 		while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist))
275 		    != NULL) {
276 			if (vm_page_busy_acquire(m, VM_ALLOC_WAITFAIL) == 0)
277 				continue;
278 
279 			dev_pager_free_page(object, m);
280 		}
281 	}
282 	object->handle = NULL;
283 	object->type = OBJT_DEAD;
284 }
285 
286 static int
287 dev_pager_getpages(vm_object_t object, vm_page_t *ma, int count, int *rbehind,
288     int *rahead)
289 {
290 	int error;
291 
292 	/* Since our haspage reports zero after/before, the count is 1. */
293 	KASSERT(count == 1, ("%s: count %d", __func__, count));
294 	if (object->un_pager.devp.ops->cdev_pg_fault == NULL)
295 		return (VM_PAGER_FAIL);
296 	VM_OBJECT_WLOCK(object);
297 	error = object->un_pager.devp.ops->cdev_pg_fault(object,
298 	    IDX_TO_OFF(ma[0]->pindex), PROT_READ, &ma[0]);
299 
300 	VM_OBJECT_ASSERT_WLOCKED(object);
301 
302 	if (error == VM_PAGER_OK) {
303 		KASSERT((object->type == OBJT_DEVICE &&
304 		     (ma[0]->oflags & VPO_UNMANAGED) != 0) ||
305 		    (object->type == OBJT_MGTDEVICE &&
306 		     (ma[0]->oflags & VPO_UNMANAGED) == 0),
307 		    ("Wrong page type %p %p", ma[0], object));
308 		if (object->type == OBJT_DEVICE) {
309 			TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist,
310 			    ma[0], plinks.q);
311 		}
312 		if (rbehind)
313 			*rbehind = 0;
314 		if (rahead)
315 			*rahead = 0;
316 	}
317 	VM_OBJECT_WUNLOCK(object);
318 
319 	return (error);
320 }
321 
322 static int
323 dev_pager_populate(vm_object_t object, vm_pindex_t pidx, int fault_type,
324     vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last)
325 {
326 
327 	VM_OBJECT_ASSERT_WLOCKED(object);
328 	if (object->un_pager.devp.ops->cdev_pg_populate == NULL)
329 		return (VM_PAGER_FAIL);
330 	return (object->un_pager.devp.ops->cdev_pg_populate(object, pidx,
331 	    fault_type, max_prot, first, last));
332 }
333 
334 static int
335 old_dev_pager_fault(vm_object_t object, vm_ooffset_t offset, int prot,
336     vm_page_t *mres)
337 {
338 	vm_paddr_t paddr;
339 	vm_page_t m_paddr, page;
340 	struct cdev *dev;
341 	struct cdevsw *csw;
342 	struct file *fpop;
343 	struct thread *td;
344 	vm_memattr_t memattr, memattr1;
345 	int ref, ret;
346 
347 	memattr = object->memattr;
348 
349 	VM_OBJECT_WUNLOCK(object);
350 
351 	dev = object->handle;
352 	csw = dev_refthread(dev, &ref);
353 	if (csw == NULL) {
354 		VM_OBJECT_WLOCK(object);
355 		return (VM_PAGER_FAIL);
356 	}
357 	td = curthread;
358 	fpop = td->td_fpop;
359 	td->td_fpop = NULL;
360 	ret = csw->d_mmap(dev, offset, &paddr, prot, &memattr);
361 	td->td_fpop = fpop;
362 	dev_relthread(dev, ref);
363 	if (ret != 0) {
364 		printf(
365 	    "WARNING: dev_pager_getpage: map function returns error %d", ret);
366 		VM_OBJECT_WLOCK(object);
367 		return (VM_PAGER_FAIL);
368 	}
369 
370 	/* If "paddr" is a real page, perform a sanity check on "memattr". */
371 	if ((m_paddr = vm_phys_paddr_to_vm_page(paddr)) != NULL &&
372 	    (memattr1 = pmap_page_get_memattr(m_paddr)) != memattr) {
373 		/*
374 		 * For the /dev/mem d_mmap routine to return the
375 		 * correct memattr, pmap_page_get_memattr() needs to
376 		 * be called, which we do there.
377 		 */
378 		if ((csw->d_flags & D_MEM) == 0) {
379 			printf("WARNING: Device driver %s has set "
380 			    "\"memattr\" inconsistently (drv %u pmap %u).\n",
381 			    csw->d_name, memattr, memattr1);
382 		}
383 		memattr = memattr1;
384 	}
385 	if (((*mres)->flags & PG_FICTITIOUS) != 0) {
386 		/*
387 		 * If the passed in result page is a fake page, update it with
388 		 * the new physical address.
389 		 */
390 		page = *mres;
391 		VM_OBJECT_WLOCK(object);
392 		vm_page_updatefake(page, paddr, memattr);
393 	} else {
394 		/*
395 		 * Replace the passed in reqpage page with our own fake page and
396 		 * free up the all of the original pages.
397 		 */
398 		page = vm_page_getfake(paddr, memattr);
399 		VM_OBJECT_WLOCK(object);
400 		vm_page_replace(page, object, (*mres)->pindex, *mres);
401 		*mres = page;
402 	}
403 	vm_page_valid(page);
404 	return (VM_PAGER_OK);
405 }
406 
407 static void
408 dev_pager_putpages(vm_object_t object, vm_page_t *m, int count, int flags,
409     int *rtvals)
410 {
411 
412 	panic("dev_pager_putpage called");
413 }
414 
415 static boolean_t
416 dev_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
417     int *after)
418 {
419 
420 	if (before != NULL)
421 		*before = 0;
422 	if (after != NULL)
423 		*after = 0;
424 	return (TRUE);
425 }
426 
427 static int
428 old_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
429     vm_ooffset_t foff, struct ucred *cred, u_short *color)
430 {
431 	struct cdev *dev;
432 	struct cdevsw *csw;
433 	vm_memattr_t dummy;
434 	vm_ooffset_t off;
435 	vm_paddr_t paddr;
436 	unsigned int npages;
437 	int ref;
438 
439 	/*
440 	 * Make sure this device can be mapped.
441 	 */
442 	dev = handle;
443 	csw = dev_refthread(dev, &ref);
444 	if (csw == NULL)
445 		return (ENXIO);
446 
447 	/*
448 	 * Check that the specified range of the device allows the desired
449 	 * protection.
450 	 *
451 	 * XXX assumes VM_PROT_* == PROT_*
452 	 */
453 	npages = OFF_TO_IDX(size);
454 	paddr = 0; /* Make paddr initialized for the case of size == 0. */
455 	for (off = foff; npages--; off += PAGE_SIZE) {
456 		if (csw->d_mmap(dev, off, &paddr, (int)prot, &dummy) != 0) {
457 			dev_relthread(dev, ref);
458 			return (EINVAL);
459 		}
460 	}
461 
462 	dev_ref(dev);
463 	dev_relthread(dev, ref);
464 	*color = atop(paddr) - OFF_TO_IDX(off - PAGE_SIZE);
465 	return (0);
466 }
467 
468 static void
469 old_dev_pager_dtor(void *handle)
470 {
471 
472 	dev_rel(handle);
473 }
474