xref: /freebsd/sys/vm/device_pager.c (revision 00a3fe968b840ee197c32dfe4107dab730bd9915)
160727d8bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1990 University of Utah.
5df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
6df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
9df8bae1dSRodney W. Grimes  * the Systems Programming Group of the University of Utah Computer
10df8bae1dSRodney W. Grimes  * Science Department.
11df8bae1dSRodney W. Grimes  *
12df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
13df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
14df8bae1dSRodney W. Grimes  * are met:
15df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
17df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
19df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
20fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
21df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
22df8bae1dSRodney W. Grimes  *    without specific prior written permission.
23df8bae1dSRodney W. Grimes  *
24df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
35df8bae1dSRodney W. Grimes  *
3626f9a767SRodney W. Grimes  *	@(#)device_pager.c	8.1 (Berkeley) 6/11/93
37df8bae1dSRodney W. Grimes  */
38df8bae1dSRodney W. Grimes 
39874651b1SDavid E. O'Brien #include <sys/cdefs.h>
40874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
41874651b1SDavid E. O'Brien 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
43df8bae1dSRodney W. Grimes #include <sys/systm.h>
44df8bae1dSRodney W. Grimes #include <sys/conf.h>
45fb919e4dSMark Murray #include <sys/lock.h>
460cddd8f0SMatthew Dillon #include <sys/proc.h>
47fb919e4dSMark Murray #include <sys/mutex.h>
48df8bae1dSRodney W. Grimes #include <sys/mman.h>
4989f6b863SAttilio Rao #include <sys/rwlock.h>
50a9fa2c05SAlfred Perlstein #include <sys/sx.h>
51*00a3fe96SKonstantin Belousov #include <sys/user.h>
529ed01c32SGleb Smirnoff #include <sys/vmmeter.h>
53df8bae1dSRodney W. Grimes 
54df8bae1dSRodney W. Grimes #include <vm/vm.h>
551c771f92SKonstantin Belousov #include <vm/vm_param.h>
56efeaf95aSDavid Greenman #include <vm/vm_object.h>
57df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
5824a1cce3SDavid Greenman #include <vm/vm_pager.h>
5943f48b65SKonstantin Belousov #include <vm/vm_phys.h>
60670d17b5SJeff Roberson #include <vm/uma.h>
61df8bae1dSRodney W. Grimes 
6211caded3SAlfred Perlstein static void dev_pager_init(void);
6311caded3SAlfred Perlstein static vm_object_t dev_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
643364c323SKonstantin Belousov     vm_ooffset_t, struct ucred *);
6511caded3SAlfred Perlstein static void dev_pager_dealloc(vm_object_t);
66b0cd2017SGleb Smirnoff static int dev_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
67b89def80SKonstantin Belousov static void dev_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
68b89def80SKonstantin Belousov static boolean_t dev_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
69b7ac5a85SKonstantin Belousov static void dev_pager_free_page(vm_object_t object, vm_page_t m);
70c42b43a0SKonstantin Belousov static int dev_pager_populate(vm_object_t object, vm_pindex_t pidx,
71c42b43a0SKonstantin Belousov     int fault_type, vm_prot_t, vm_pindex_t *first, vm_pindex_t *last);
72f708ef1bSPoul-Henning Kamp 
73f708ef1bSPoul-Henning Kamp /* list of device pager objects */
74f708ef1bSPoul-Henning Kamp static struct pagerlst dev_pager_object_list;
75a9fa2c05SAlfred Perlstein /* protect list manipulation */
76a9fa2c05SAlfred Perlstein static struct mtx dev_pager_mtx;
77a9fa2c05SAlfred Perlstein 
78d474440aSKonstantin Belousov const struct pagerops devicepagerops = {
79*00a3fe96SKonstantin Belousov 	.pgo_kvme_type = KVME_TYPE_DEVICE,
804e658600SPoul-Henning Kamp 	.pgo_init =	dev_pager_init,
814e658600SPoul-Henning Kamp 	.pgo_alloc =	dev_pager_alloc,
824e658600SPoul-Henning Kamp 	.pgo_dealloc =	dev_pager_dealloc,
834e658600SPoul-Henning Kamp 	.pgo_getpages =	dev_pager_getpages,
844e658600SPoul-Henning Kamp 	.pgo_putpages =	dev_pager_putpages,
854e658600SPoul-Henning Kamp 	.pgo_haspage =	dev_pager_haspage,
86df8bae1dSRodney W. Grimes };
87df8bae1dSRodney W. Grimes 
88d474440aSKonstantin Belousov const struct pagerops mgtdevicepagerops = {
89*00a3fe96SKonstantin Belousov 	.pgo_kvme_type = KVME_TYPE_MGTDEVICE,
90b7ac5a85SKonstantin Belousov 	.pgo_alloc =	dev_pager_alloc,
91b7ac5a85SKonstantin Belousov 	.pgo_dealloc =	dev_pager_dealloc,
92b7ac5a85SKonstantin Belousov 	.pgo_getpages =	dev_pager_getpages,
93b7ac5a85SKonstantin Belousov 	.pgo_putpages =	dev_pager_putpages,
94b7ac5a85SKonstantin Belousov 	.pgo_haspage =	dev_pager_haspage,
95c42b43a0SKonstantin Belousov 	.pgo_populate =	dev_pager_populate,
96b7ac5a85SKonstantin Belousov };
97b7ac5a85SKonstantin Belousov 
98286790a7SKonstantin Belousov static int old_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
99286790a7SKonstantin Belousov     vm_ooffset_t foff, struct ucred *cred, u_short *color);
100286790a7SKonstantin Belousov static void old_dev_pager_dtor(void *handle);
101286790a7SKonstantin Belousov static int old_dev_pager_fault(vm_object_t object, vm_ooffset_t offset,
102286790a7SKonstantin Belousov     int prot, vm_page_t *mres);
103286790a7SKonstantin Belousov 
104d474440aSKonstantin Belousov static const struct cdev_pager_ops old_dev_pager_ops = {
105286790a7SKonstantin Belousov 	.cdev_pg_ctor =	old_dev_pager_ctor,
106286790a7SKonstantin Belousov 	.cdev_pg_dtor =	old_dev_pager_dtor,
107286790a7SKonstantin Belousov 	.cdev_pg_fault = old_dev_pager_fault
108286790a7SKonstantin Belousov };
109286790a7SKonstantin Belousov 
110f708ef1bSPoul-Henning Kamp static void
111b89def80SKonstantin Belousov dev_pager_init(void)
112df8bae1dSRodney W. Grimes {
113b89def80SKonstantin Belousov 
11424a1cce3SDavid Greenman 	TAILQ_INIT(&dev_pager_object_list);
1156008862bSJohn Baldwin 	mtx_init(&dev_pager_mtx, "dev_pager list", NULL, MTX_DEF);
116df8bae1dSRodney W. Grimes }
117df8bae1dSRodney W. Grimes 
118286790a7SKonstantin Belousov vm_object_t
119286790a7SKonstantin Belousov cdev_pager_lookup(void *handle)
120df8bae1dSRodney W. Grimes {
121286790a7SKonstantin Belousov 	vm_object_t object;
122286790a7SKonstantin Belousov 
123286790a7SKonstantin Belousov 	mtx_lock(&dev_pager_mtx);
124286790a7SKonstantin Belousov 	object = vm_pager_object_lookup(&dev_pager_object_list, handle);
125286790a7SKonstantin Belousov 	mtx_unlock(&dev_pager_mtx);
126286790a7SKonstantin Belousov 	return (object);
127286790a7SKonstantin Belousov }
128286790a7SKonstantin Belousov 
129286790a7SKonstantin Belousov vm_object_t
130d474440aSKonstantin Belousov cdev_pager_allocate(void *handle, enum obj_type tp,
131d474440aSKonstantin Belousov     const struct cdev_pager_ops *ops, vm_ooffset_t size, vm_prot_t prot,
132d474440aSKonstantin Belousov     vm_ooffset_t foff, struct ucred *cred)
133286790a7SKonstantin Belousov {
134deea654eSKonstantin Belousov 	vm_object_t object, object1;
1352f7af3dbSAlan Cox 	vm_pindex_t pindex;
136286790a7SKonstantin Belousov 	u_short color;
137286790a7SKonstantin Belousov 
138b7ac5a85SKonstantin Belousov 	if (tp != OBJT_DEVICE && tp != OBJT_MGTDEVICE)
139286790a7SKonstantin Belousov 		return (NULL);
140c42b43a0SKonstantin Belousov 	KASSERT(tp == OBJT_MGTDEVICE || ops->cdev_pg_populate == NULL,
141c42b43a0SKonstantin Belousov 	    ("populate on unmanaged device pager"));
142df8bae1dSRodney W. Grimes 
143df8bae1dSRodney W. Grimes 	/*
144df8bae1dSRodney W. Grimes 	 * Offset should be page aligned.
145df8bae1dSRodney W. Grimes 	 */
146aa8de40aSPoul-Henning Kamp 	if (foff & PAGE_MASK)
147df8bae1dSRodney W. Grimes 		return (NULL);
148df8bae1dSRodney W. Grimes 
149987ff181SKonstantin Belousov 	/*
150987ff181SKonstantin Belousov 	 * Treat the mmap(2) file offset as an unsigned value for a
151987ff181SKonstantin Belousov 	 * device mapping.  This, in effect, allows a user to pass all
152987ff181SKonstantin Belousov 	 * possible off_t values as the mapping cookie to the driver.  At
153987ff181SKonstantin Belousov 	 * this point, we know that both foff and size are a multiple
154987ff181SKonstantin Belousov 	 * of the page size.  Do a check to avoid wrap.
155987ff181SKonstantin Belousov 	 */
1566cde7a16SDavid Greenman 	size = round_page(size);
15710d9120cSKonstantin Belousov 	pindex = OFF_TO_IDX(foff) + OFF_TO_IDX(size);
15810d9120cSKonstantin Belousov 	if (pindex > OBJ_MAX_SIZE || pindex < OFF_TO_IDX(foff) ||
15910d9120cSKonstantin Belousov 	    pindex < OFF_TO_IDX(size))
160987ff181SKonstantin Belousov 		return (NULL);
1616cde7a16SDavid Greenman 
162286790a7SKonstantin Belousov 	if (ops->cdev_pg_ctor(handle, size, prot, foff, cred, &color) != 0)
163c8664f82SAlan Cox 		return (NULL);
164deea654eSKonstantin Belousov 	mtx_lock(&dev_pager_mtx);
16524a1cce3SDavid Greenman 
16624a1cce3SDavid Greenman 	/*
167df8bae1dSRodney W. Grimes 	 * Look up pager, creating as necessary.
168df8bae1dSRodney W. Grimes 	 */
169deea654eSKonstantin Belousov 	object1 = NULL;
17024a1cce3SDavid Greenman 	object = vm_pager_object_lookup(&dev_pager_object_list, handle);
17124a1cce3SDavid Greenman 	if (object == NULL) {
172df8bae1dSRodney W. Grimes 		/*
173e46cd413SAlan Cox 		 * Allocate object and associate it with the pager.  Initialize
174e46cd413SAlan Cox 		 * the object's pg_color based upon the physical address of the
175e46cd413SAlan Cox 		 * device's memory.
176df8bae1dSRodney W. Grimes 		 */
177deea654eSKonstantin Belousov 		mtx_unlock(&dev_pager_mtx);
178286790a7SKonstantin Belousov 		object1 = vm_object_allocate(tp, pindex);
179e46cd413SAlan Cox 		object1->flags |= OBJ_COLORED;
180286790a7SKonstantin Belousov 		object1->pg_color = color;
181286790a7SKonstantin Belousov 		object1->handle = handle;
182286790a7SKonstantin Belousov 		object1->un_pager.devp.ops = ops;
18343ab9660SKenneth D. Merry 		object1->un_pager.devp.dev = handle;
184339772b0SKonstantin Belousov 		TAILQ_INIT(&object1->un_pager.devp.devp_pglist);
185deea654eSKonstantin Belousov 		mtx_lock(&dev_pager_mtx);
186deea654eSKonstantin Belousov 		object = vm_pager_object_lookup(&dev_pager_object_list, handle);
187deea654eSKonstantin Belousov 		if (object != NULL) {
188deea654eSKonstantin Belousov 			/*
189deea654eSKonstantin Belousov 			 * We raced with other thread while allocating object.
190deea654eSKonstantin Belousov 			 */
191deea654eSKonstantin Belousov 			if (pindex > object->size)
192deea654eSKonstantin Belousov 				object->size = pindex;
1938add0cedSJohn Baldwin 			KASSERT(object->type == tp,
1941be02479SKonstantin Belousov 			    ("Inconsistent device pager type %p %d",
1951be02479SKonstantin Belousov 			    object, tp));
1961be02479SKonstantin Belousov 			KASSERT(object->un_pager.devp.ops == ops,
1971be02479SKonstantin Belousov 			    ("Inconsistent devops %p %p", object, ops));
198deea654eSKonstantin Belousov 		} else {
199deea654eSKonstantin Belousov 			object = object1;
200deea654eSKonstantin Belousov 			object1 = NULL;
20124a1cce3SDavid Greenman 			object->handle = handle;
202deea654eSKonstantin Belousov 			TAILQ_INSERT_TAIL(&dev_pager_object_list, object,
203deea654eSKonstantin Belousov 			    pager_object_list);
204c42b43a0SKonstantin Belousov 			if (ops->cdev_pg_populate != NULL)
205c42b43a0SKonstantin Belousov 				vm_object_set_flag(object, OBJ_POPULATE);
206deea654eSKonstantin Belousov 		}
207df8bae1dSRodney W. Grimes 	} else {
2082f7af3dbSAlan Cox 		if (pindex > object->size)
2092f7af3dbSAlan Cox 			object->size = pindex;
2108add0cedSJohn Baldwin 		KASSERT(object->type == tp,
2118add0cedSJohn Baldwin 		    ("Inconsistent device pager type %p %d", object, tp));
212df8bae1dSRodney W. Grimes 	}
213deea654eSKonstantin Belousov 	mtx_unlock(&dev_pager_mtx);
214339772b0SKonstantin Belousov 	if (object1 != NULL) {
215339772b0SKonstantin Belousov 		object1->handle = object1;
216339772b0SKonstantin Belousov 		mtx_lock(&dev_pager_mtx);
217339772b0SKonstantin Belousov 		TAILQ_INSERT_TAIL(&dev_pager_object_list, object1,
218339772b0SKonstantin Belousov 		    pager_object_list);
219339772b0SKonstantin Belousov 		mtx_unlock(&dev_pager_mtx);
220deea654eSKonstantin Belousov 		vm_object_deallocate(object1);
221339772b0SKonstantin Belousov 	}
22224a1cce3SDavid Greenman 	return (object);
22324a1cce3SDavid Greenman }
22424a1cce3SDavid Greenman 
225286790a7SKonstantin Belousov static vm_object_t
226286790a7SKonstantin Belousov dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
227286790a7SKonstantin Belousov     vm_ooffset_t foff, struct ucred *cred)
228286790a7SKonstantin Belousov {
229286790a7SKonstantin Belousov 
230286790a7SKonstantin Belousov 	return (cdev_pager_allocate(handle, OBJT_DEVICE, &old_dev_pager_ops,
231286790a7SKonstantin Belousov 	    size, prot, foff, cred));
232286790a7SKonstantin Belousov }
233286790a7SKonstantin Belousov 
234286790a7SKonstantin Belousov void
235286790a7SKonstantin Belousov cdev_pager_free_page(vm_object_t object, vm_page_t m)
236286790a7SKonstantin Belousov {
237286790a7SKonstantin Belousov 
23889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
239b7ac5a85SKonstantin Belousov 	if (object->type == OBJT_MGTDEVICE) {
240b7ac5a85SKonstantin Belousov 		KASSERT((m->oflags & VPO_UNMANAGED) == 0, ("unmanaged %p", m));
241b7ac5a85SKonstantin Belousov 		pmap_remove_all(m);
2420fd977b3SMark Johnston 		(void)vm_page_remove(m);
243b7ac5a85SKonstantin Belousov 	} else if (object->type == OBJT_DEVICE)
244b7ac5a85SKonstantin Belousov 		dev_pager_free_page(object, m);
245b7ac5a85SKonstantin Belousov }
246b7ac5a85SKonstantin Belousov 
247b7ac5a85SKonstantin Belousov static void
248b7ac5a85SKonstantin Belousov dev_pager_free_page(vm_object_t object, vm_page_t m)
249b7ac5a85SKonstantin Belousov {
250b7ac5a85SKonstantin Belousov 
25189f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
252b7ac5a85SKonstantin Belousov 	KASSERT((object->type == OBJT_DEVICE &&
253b7ac5a85SKonstantin Belousov 	    (m->oflags & VPO_UNMANAGED) != 0),
254b7ac5a85SKonstantin Belousov 	    ("Managed device or page obj %p m %p", object, m));
255c325e866SKonstantin Belousov 	TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, plinks.q);
256286790a7SKonstantin Belousov 	vm_page_putfake(m);
257286790a7SKonstantin Belousov }
258286790a7SKonstantin Belousov 
259f708ef1bSPoul-Henning Kamp static void
260b89def80SKonstantin Belousov dev_pager_dealloc(vm_object_t object)
26124a1cce3SDavid Greenman {
262df8bae1dSRodney W. Grimes 	vm_page_t m;
263df8bae1dSRodney W. Grimes 
26489f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
26543ab9660SKenneth D. Merry 	object->un_pager.devp.ops->cdev_pg_dtor(object->un_pager.devp.dev);
266286790a7SKonstantin Belousov 
267a9fa2c05SAlfred Perlstein 	mtx_lock(&dev_pager_mtx);
26824a1cce3SDavid Greenman 	TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list);
269a9fa2c05SAlfred Perlstein 	mtx_unlock(&dev_pager_mtx);
27089f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
271b7ac5a85SKonstantin Belousov 
272b7ac5a85SKonstantin Belousov 	if (object->type == OBJT_DEVICE) {
273df8bae1dSRodney W. Grimes 		/*
274df8bae1dSRodney W. Grimes 		 * Free up our fake pages.
275df8bae1dSRodney W. Grimes 		 */
276b7ac5a85SKonstantin Belousov 		while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist))
2773cf3b4e6SJeff Roberson 		    != NULL) {
2783cf3b4e6SJeff Roberson 			if (vm_page_busy_acquire(m, VM_ALLOC_WAITFAIL) == 0)
2793cf3b4e6SJeff Roberson 				continue;
2803cf3b4e6SJeff Roberson 
281b7ac5a85SKonstantin Belousov 			dev_pager_free_page(object, m);
282b7ac5a85SKonstantin Belousov 		}
2833cf3b4e6SJeff Roberson 	}
284e735691bSJohn Baldwin 	object->handle = NULL;
285e735691bSJohn Baldwin 	object->type = OBJT_DEAD;
286df8bae1dSRodney W. Grimes }
287df8bae1dSRodney W. Grimes 
288f708ef1bSPoul-Henning Kamp static int
289b0cd2017SGleb Smirnoff dev_pager_getpages(vm_object_t object, vm_page_t *ma, int count, int *rbehind,
290b0cd2017SGleb Smirnoff     int *rahead)
291df8bae1dSRodney W. Grimes {
292b89def80SKonstantin Belousov 	int error;
293df8bae1dSRodney W. Grimes 
294b0cd2017SGleb Smirnoff 	/* Since our haspage reports zero after/before, the count is 1. */
295b0cd2017SGleb Smirnoff 	KASSERT(count == 1, ("%s: count %d", __func__, count));
296c42b43a0SKonstantin Belousov 	if (object->un_pager.devp.ops->cdev_pg_fault == NULL)
297c42b43a0SKonstantin Belousov 		return (VM_PAGER_FAIL);
298d6e13f3bSJeff Roberson 	VM_OBJECT_WLOCK(object);
299286790a7SKonstantin Belousov 	error = object->un_pager.devp.ops->cdev_pg_fault(object,
300b0cd2017SGleb Smirnoff 	    IDX_TO_OFF(ma[0]->pindex), PROT_READ, &ma[0]);
301286790a7SKonstantin Belousov 
30289f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
303286790a7SKonstantin Belousov 
304286790a7SKonstantin Belousov 	if (error == VM_PAGER_OK) {
305b7ac5a85SKonstantin Belousov 		KASSERT((object->type == OBJT_DEVICE &&
306b0cd2017SGleb Smirnoff 		     (ma[0]->oflags & VPO_UNMANAGED) != 0) ||
307b7ac5a85SKonstantin Belousov 		    (object->type == OBJT_MGTDEVICE &&
308b0cd2017SGleb Smirnoff 		     (ma[0]->oflags & VPO_UNMANAGED) == 0),
309b0cd2017SGleb Smirnoff 		    ("Wrong page type %p %p", ma[0], object));
310b7ac5a85SKonstantin Belousov 		if (object->type == OBJT_DEVICE) {
311286790a7SKonstantin Belousov 			TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist,
312b0cd2017SGleb Smirnoff 			    ma[0], plinks.q);
313286790a7SKonstantin Belousov 		}
314b0cd2017SGleb Smirnoff 		if (rbehind)
315b0cd2017SGleb Smirnoff 			*rbehind = 0;
316b0cd2017SGleb Smirnoff 		if (rahead)
317b0cd2017SGleb Smirnoff 			*rahead = 0;
318b7ac5a85SKonstantin Belousov 	}
319d6e13f3bSJeff Roberson 	VM_OBJECT_WUNLOCK(object);
320286790a7SKonstantin Belousov 
321286790a7SKonstantin Belousov 	return (error);
322286790a7SKonstantin Belousov }
323286790a7SKonstantin Belousov 
324286790a7SKonstantin Belousov static int
325c42b43a0SKonstantin Belousov dev_pager_populate(vm_object_t object, vm_pindex_t pidx, int fault_type,
326c42b43a0SKonstantin Belousov     vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last)
327c42b43a0SKonstantin Belousov {
328c42b43a0SKonstantin Belousov 
329c42b43a0SKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED(object);
330c42b43a0SKonstantin Belousov 	if (object->un_pager.devp.ops->cdev_pg_populate == NULL)
331c42b43a0SKonstantin Belousov 		return (VM_PAGER_FAIL);
332c42b43a0SKonstantin Belousov 	return (object->un_pager.devp.ops->cdev_pg_populate(object, pidx,
333c42b43a0SKonstantin Belousov 	    fault_type, max_prot, first, last));
334c42b43a0SKonstantin Belousov }
335c42b43a0SKonstantin Belousov 
336c42b43a0SKonstantin Belousov static int
337286790a7SKonstantin Belousov old_dev_pager_fault(vm_object_t object, vm_ooffset_t offset, int prot,
338286790a7SKonstantin Belousov     vm_page_t *mres)
339286790a7SKonstantin Belousov {
340286790a7SKonstantin Belousov 	vm_paddr_t paddr;
341286790a7SKonstantin Belousov 	vm_page_t m_paddr, page;
342286790a7SKonstantin Belousov 	struct cdev *dev;
343286790a7SKonstantin Belousov 	struct cdevsw *csw;
344286790a7SKonstantin Belousov 	struct file *fpop;
345286790a7SKonstantin Belousov 	struct thread *td;
346e9d37c9fSKonstantin Belousov 	vm_memattr_t memattr, memattr1;
347286790a7SKonstantin Belousov 	int ref, ret;
348286790a7SKonstantin Belousov 
3493153e878SAlan Cox 	memattr = object->memattr;
350286790a7SKonstantin Belousov 
35189f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
352286790a7SKonstantin Belousov 
353286790a7SKonstantin Belousov 	dev = object->handle;
3543979450bSKonstantin Belousov 	csw = dev_refthread(dev, &ref);
35591a1929fSAttilio Rao 	if (csw == NULL) {
35689f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
35791a1929fSAttilio Rao 		return (VM_PAGER_FAIL);
35891a1929fSAttilio Rao 	}
3597818e0a5SKonstantin Belousov 	td = curthread;
3607818e0a5SKonstantin Belousov 	fpop = td->td_fpop;
3617818e0a5SKonstantin Belousov 	td->td_fpop = NULL;
362286790a7SKonstantin Belousov 	ret = csw->d_mmap(dev, offset, &paddr, prot, &memattr);
3637818e0a5SKonstantin Belousov 	td->td_fpop = fpop;
3643979450bSKonstantin Belousov 	dev_relthread(dev, ref);
365286790a7SKonstantin Belousov 	if (ret != 0) {
366286790a7SKonstantin Belousov 		printf(
367286790a7SKonstantin Belousov 	    "WARNING: dev_pager_getpage: map function returns error %d", ret);
36889f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
369286790a7SKonstantin Belousov 		return (VM_PAGER_FAIL);
370286790a7SKonstantin Belousov 	}
371286790a7SKonstantin Belousov 
3723153e878SAlan Cox 	/* If "paddr" is a real page, perform a sanity check on "memattr". */
3733153e878SAlan Cox 	if ((m_paddr = vm_phys_paddr_to_vm_page(paddr)) != NULL &&
374e9d37c9fSKonstantin Belousov 	    (memattr1 = pmap_page_get_memattr(m_paddr)) != memattr) {
375e9d37c9fSKonstantin Belousov 		/*
376e9d37c9fSKonstantin Belousov 		 * For the /dev/mem d_mmap routine to return the
377e9d37c9fSKonstantin Belousov 		 * correct memattr, pmap_page_get_memattr() needs to
378e9d37c9fSKonstantin Belousov 		 * be called, which we do there.
379e9d37c9fSKonstantin Belousov 		 */
380e9d37c9fSKonstantin Belousov 		if ((csw->d_flags & D_MEM) == 0) {
381e9d37c9fSKonstantin Belousov 			printf("WARNING: Device driver %s has set "
382e9d37c9fSKonstantin Belousov 			    "\"memattr\" inconsistently (drv %u pmap %u).\n",
383e9d37c9fSKonstantin Belousov 			    csw->d_name, memattr, memattr1);
384e9d37c9fSKonstantin Belousov 		}
385e9d37c9fSKonstantin Belousov 		memattr = memattr1;
3863153e878SAlan Cox 	}
387286790a7SKonstantin Belousov 	if (((*mres)->flags & PG_FICTITIOUS) != 0) {
38892bab635SDoug Rabson 		/*
389286790a7SKonstantin Belousov 		 * If the passed in result page is a fake page, update it with
39092bab635SDoug Rabson 		 * the new physical address.
39192bab635SDoug Rabson 		 */
392286790a7SKonstantin Belousov 		page = *mres;
39389f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
39410cf2560SAlan Cox 		vm_page_updatefake(page, paddr, memattr);
39592bab635SDoug Rabson 	} else {
396df8bae1dSRodney W. Grimes 		/*
39707159f9cSMaxime Henrion 		 * Replace the passed in reqpage page with our own fake page and
39807159f9cSMaxime Henrion 		 * free up the all of the original pages.
399df8bae1dSRodney W. Grimes 		 */
40010cf2560SAlan Cox 		page = vm_page_getfake(paddr, memattr);
40189f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
4023cf3b4e6SJeff Roberson 		vm_page_replace(page, object, (*mres)->pindex, *mres);
403286790a7SKonstantin Belousov 		*mres = page;
40492bab635SDoug Rabson 	}
4050012f373SJeff Roberson 	vm_page_valid(page);
406df8bae1dSRodney W. Grimes 	return (VM_PAGER_OK);
407df8bae1dSRodney W. Grimes }
408df8bae1dSRodney W. Grimes 
409e4542174SMatthew Dillon static void
410b89def80SKonstantin Belousov dev_pager_putpages(vm_object_t object, vm_page_t *m, int count, int flags,
411b89def80SKonstantin Belousov     int *rtvals)
412df8bae1dSRodney W. Grimes {
413286790a7SKonstantin Belousov 
414df8bae1dSRodney W. Grimes 	panic("dev_pager_putpage called");
415df8bae1dSRodney W. Grimes }
416df8bae1dSRodney W. Grimes 
417f708ef1bSPoul-Henning Kamp static boolean_t
418b89def80SKonstantin Belousov dev_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
419b89def80SKonstantin Belousov     int *after)
420df8bae1dSRodney W. Grimes {
421b89def80SKonstantin Belousov 
42224a1cce3SDavid Greenman 	if (before != NULL)
42324a1cce3SDavid Greenman 		*before = 0;
42424a1cce3SDavid Greenman 	if (after != NULL)
42524a1cce3SDavid Greenman 		*after = 0;
426df8bae1dSRodney W. Grimes 	return (TRUE);
427df8bae1dSRodney W. Grimes }
428286790a7SKonstantin Belousov 
429286790a7SKonstantin Belousov static int
430286790a7SKonstantin Belousov old_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
431286790a7SKonstantin Belousov     vm_ooffset_t foff, struct ucred *cred, u_short *color)
432286790a7SKonstantin Belousov {
433286790a7SKonstantin Belousov 	struct cdev *dev;
434286790a7SKonstantin Belousov 	struct cdevsw *csw;
435286790a7SKonstantin Belousov 	vm_memattr_t dummy;
436286790a7SKonstantin Belousov 	vm_ooffset_t off;
437286790a7SKonstantin Belousov 	vm_paddr_t paddr;
438286790a7SKonstantin Belousov 	unsigned int npages;
439286790a7SKonstantin Belousov 	int ref;
440286790a7SKonstantin Belousov 
441286790a7SKonstantin Belousov 	/*
442286790a7SKonstantin Belousov 	 * Make sure this device can be mapped.
443286790a7SKonstantin Belousov 	 */
444286790a7SKonstantin Belousov 	dev = handle;
445286790a7SKonstantin Belousov 	csw = dev_refthread(dev, &ref);
446286790a7SKonstantin Belousov 	if (csw == NULL)
447286790a7SKonstantin Belousov 		return (ENXIO);
448286790a7SKonstantin Belousov 
449286790a7SKonstantin Belousov 	/*
450286790a7SKonstantin Belousov 	 * Check that the specified range of the device allows the desired
451286790a7SKonstantin Belousov 	 * protection.
452286790a7SKonstantin Belousov 	 *
453286790a7SKonstantin Belousov 	 * XXX assumes VM_PROT_* == PROT_*
454286790a7SKonstantin Belousov 	 */
455286790a7SKonstantin Belousov 	npages = OFF_TO_IDX(size);
4567253a5ecSKonstantin Belousov 	paddr = 0; /* Make paddr initialized for the case of size == 0. */
457286790a7SKonstantin Belousov 	for (off = foff; npages--; off += PAGE_SIZE) {
458286790a7SKonstantin Belousov 		if (csw->d_mmap(dev, off, &paddr, (int)prot, &dummy) != 0) {
459286790a7SKonstantin Belousov 			dev_relthread(dev, ref);
460286790a7SKonstantin Belousov 			return (EINVAL);
461286790a7SKonstantin Belousov 		}
462286790a7SKonstantin Belousov 	}
463286790a7SKonstantin Belousov 
464286790a7SKonstantin Belousov 	dev_ref(dev);
465286790a7SKonstantin Belousov 	dev_relthread(dev, ref);
466286790a7SKonstantin Belousov 	*color = atop(paddr) - OFF_TO_IDX(off - PAGE_SIZE);
467286790a7SKonstantin Belousov 	return (0);
468286790a7SKonstantin Belousov }
469286790a7SKonstantin Belousov 
470286790a7SKonstantin Belousov static void
471286790a7SKonstantin Belousov old_dev_pager_dtor(void *handle)
472286790a7SKonstantin Belousov {
473286790a7SKonstantin Belousov 
474286790a7SKonstantin Belousov 	dev_rel(handle);
475286790a7SKonstantin Belousov }
476