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