xref: /freebsd/sys/vm/device_pager.c (revision 7c1b51d6dc2e165ae7333373513b080f17cf79bd)
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  * 3. 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 #include <sys/vmmeter.h>
50 
51 #include <vm/vm.h>
52 #include <vm/vm_param.h>
53 #include <vm/vm_object.h>
54 #include <vm/vm_page.h>
55 #include <vm/vm_pager.h>
56 #include <vm/vm_phys.h>
57 #include <vm/uma.h>
58 
59 static void dev_pager_init(void);
60 static vm_object_t dev_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
61     vm_ooffset_t, struct ucred *);
62 static void dev_pager_dealloc(vm_object_t);
63 static int dev_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
64 static void dev_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
65 static boolean_t dev_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
66 static void dev_pager_free_page(vm_object_t object, vm_page_t m);
67 static int dev_pager_populate(vm_object_t object, vm_pindex_t pidx,
68     int fault_type, vm_prot_t, vm_pindex_t *first, vm_pindex_t *last);
69 
70 /* list of device pager objects */
71 static struct pagerlst dev_pager_object_list;
72 /* protect list manipulation */
73 static struct mtx dev_pager_mtx;
74 
75 struct pagerops devicepagerops = {
76 	.pgo_init =	dev_pager_init,
77 	.pgo_alloc =	dev_pager_alloc,
78 	.pgo_dealloc =	dev_pager_dealloc,
79 	.pgo_getpages =	dev_pager_getpages,
80 	.pgo_putpages =	dev_pager_putpages,
81 	.pgo_haspage =	dev_pager_haspage,
82 };
83 
84 struct pagerops mgtdevicepagerops = {
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 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, struct cdev_pager_ops *ops,
126     vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff, struct ucred *cred)
127 {
128 	vm_object_t object, object1;
129 	vm_pindex_t pindex;
130 	u_short color;
131 
132 	if (tp != OBJT_DEVICE && tp != OBJT_MGTDEVICE)
133 		return (NULL);
134 	KASSERT(tp == OBJT_MGTDEVICE || ops->cdev_pg_populate == NULL,
135 	    ("populate on unmanaged device pager"));
136 
137 	/*
138 	 * Offset should be page aligned.
139 	 */
140 	if (foff & PAGE_MASK)
141 		return (NULL);
142 
143 	/*
144 	 * Treat the mmap(2) file offset as an unsigned value for a
145 	 * device mapping.  This, in effect, allows a user to pass all
146 	 * possible off_t values as the mapping cookie to the driver.  At
147 	 * this point, we know that both foff and size are a multiple
148 	 * of the page size.  Do a check to avoid wrap.
149 	 */
150 	size = round_page(size);
151 	pindex = UOFF_TO_IDX(foff) + UOFF_TO_IDX(size);
152 	if (pindex > OBJ_MAX_SIZE || pindex < UOFF_TO_IDX(foff) ||
153 	    pindex < UOFF_TO_IDX(size))
154 		return (NULL);
155 
156 	if (ops->cdev_pg_ctor(handle, size, prot, foff, cred, &color) != 0)
157 		return (NULL);
158 	mtx_lock(&dev_pager_mtx);
159 
160 	/*
161 	 * Look up pager, creating as necessary.
162 	 */
163 	object1 = NULL;
164 	object = vm_pager_object_lookup(&dev_pager_object_list, handle);
165 	if (object == NULL) {
166 		/*
167 		 * Allocate object and associate it with the pager.  Initialize
168 		 * the object's pg_color based upon the physical address of the
169 		 * device's memory.
170 		 */
171 		mtx_unlock(&dev_pager_mtx);
172 		object1 = vm_object_allocate(tp, pindex);
173 		object1->flags |= OBJ_COLORED;
174 		object1->pg_color = color;
175 		object1->handle = handle;
176 		object1->un_pager.devp.ops = ops;
177 		object1->un_pager.devp.dev = handle;
178 		TAILQ_INIT(&object1->un_pager.devp.devp_pglist);
179 		mtx_lock(&dev_pager_mtx);
180 		object = vm_pager_object_lookup(&dev_pager_object_list, handle);
181 		if (object != NULL) {
182 			/*
183 			 * We raced with other thread while allocating object.
184 			 */
185 			if (pindex > object->size)
186 				object->size = pindex;
187 			KASSERT(object->type == tp,
188 			    ("Inconsistent device pager type %p %d",
189 			    object, tp));
190 			KASSERT(object->un_pager.devp.ops == ops,
191 			    ("Inconsistent devops %p %p", object, ops));
192 		} else {
193 			object = object1;
194 			object1 = NULL;
195 			object->handle = handle;
196 			TAILQ_INSERT_TAIL(&dev_pager_object_list, object,
197 			    pager_object_list);
198 			if (ops->cdev_pg_populate != NULL)
199 				vm_object_set_flag(object, OBJ_POPULATE);
200 		}
201 	} else {
202 		if (pindex > object->size)
203 			object->size = pindex;
204 		KASSERT(object->type == tp,
205 		    ("Inconsistent device pager type %p %d", object, tp));
206 	}
207 	mtx_unlock(&dev_pager_mtx);
208 	if (object1 != NULL) {
209 		object1->handle = object1;
210 		mtx_lock(&dev_pager_mtx);
211 		TAILQ_INSERT_TAIL(&dev_pager_object_list, object1,
212 		    pager_object_list);
213 		mtx_unlock(&dev_pager_mtx);
214 		vm_object_deallocate(object1);
215 	}
216 	return (object);
217 }
218 
219 static vm_object_t
220 dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
221     vm_ooffset_t foff, struct ucred *cred)
222 {
223 
224 	return (cdev_pager_allocate(handle, OBJT_DEVICE, &old_dev_pager_ops,
225 	    size, prot, foff, cred));
226 }
227 
228 void
229 cdev_pager_free_page(vm_object_t object, vm_page_t m)
230 {
231 
232 	VM_OBJECT_ASSERT_WLOCKED(object);
233 	if (object->type == OBJT_MGTDEVICE) {
234 		KASSERT((m->oflags & VPO_UNMANAGED) == 0, ("unmanaged %p", m));
235 		pmap_remove_all(m);
236 		vm_page_lock(m);
237 		vm_page_remove(m);
238 		vm_page_unlock(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_lock(*mres);
395 		vm_page_free(*mres);
396 		vm_page_unlock(*mres);
397 		*mres = page;
398 	}
399 	page->valid = VM_PAGE_BITS_ALL;
400 	return (VM_PAGER_OK);
401 }
402 
403 static void
404 dev_pager_putpages(vm_object_t object, vm_page_t *m, int count, int flags,
405     int *rtvals)
406 {
407 
408 	panic("dev_pager_putpage called");
409 }
410 
411 static boolean_t
412 dev_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
413     int *after)
414 {
415 
416 	if (before != NULL)
417 		*before = 0;
418 	if (after != NULL)
419 		*after = 0;
420 	return (TRUE);
421 }
422 
423 static int
424 old_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
425     vm_ooffset_t foff, struct ucred *cred, u_short *color)
426 {
427 	struct cdev *dev;
428 	struct cdevsw *csw;
429 	vm_memattr_t dummy;
430 	vm_ooffset_t off;
431 	vm_paddr_t paddr;
432 	unsigned int npages;
433 	int ref;
434 
435 	/*
436 	 * Make sure this device can be mapped.
437 	 */
438 	dev = handle;
439 	csw = dev_refthread(dev, &ref);
440 	if (csw == NULL)
441 		return (ENXIO);
442 
443 	/*
444 	 * Check that the specified range of the device allows the desired
445 	 * protection.
446 	 *
447 	 * XXX assumes VM_PROT_* == PROT_*
448 	 */
449 	npages = OFF_TO_IDX(size);
450 	paddr = 0; /* Make paddr initialized for the case of size == 0. */
451 	for (off = foff; npages--; off += PAGE_SIZE) {
452 		if (csw->d_mmap(dev, off, &paddr, (int)prot, &dummy) != 0) {
453 			dev_relthread(dev, ref);
454 			return (EINVAL);
455 		}
456 	}
457 
458 	dev_ref(dev);
459 	dev_relthread(dev, ref);
460 	*color = atop(paddr) - OFF_TO_IDX(off - PAGE_SIZE);
461 	return (0);
462 }
463 
464 static void
465 old_dev_pager_dtor(void *handle)
466 {
467 
468 	dev_rel(handle);
469 }
470