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