xref: /freebsd/sys/vm/device_pager.c (revision e9d37c9f8d5dd0072a00e3c7db33b0cc36c93ca4)
160727d8bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1990 University of Utah.
3df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
4df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
5df8bae1dSRodney W. Grimes  *
6df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
7df8bae1dSRodney W. Grimes  * the Systems Programming Group of the University of Utah Computer
8df8bae1dSRodney W. Grimes  * Science Department.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
3426f9a767SRodney W. Grimes  *	@(#)device_pager.c	8.1 (Berkeley) 6/11/93
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37874651b1SDavid E. O'Brien #include <sys/cdefs.h>
38874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
39874651b1SDavid E. O'Brien 
40df8bae1dSRodney W. Grimes #include <sys/param.h>
41df8bae1dSRodney W. Grimes #include <sys/systm.h>
42df8bae1dSRodney W. Grimes #include <sys/conf.h>
43fb919e4dSMark Murray #include <sys/lock.h>
440cddd8f0SMatthew Dillon #include <sys/proc.h>
45fb919e4dSMark Murray #include <sys/mutex.h>
46df8bae1dSRodney W. Grimes #include <sys/mman.h>
4789f6b863SAttilio Rao #include <sys/rwlock.h>
48a9fa2c05SAlfred Perlstein #include <sys/sx.h>
49df8bae1dSRodney W. Grimes 
50df8bae1dSRodney W. Grimes #include <vm/vm.h>
511c771f92SKonstantin Belousov #include <vm/vm_param.h>
52efeaf95aSDavid Greenman #include <vm/vm_object.h>
53df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
5424a1cce3SDavid Greenman #include <vm/vm_pager.h>
5543f48b65SKonstantin Belousov #include <vm/vm_phys.h>
56670d17b5SJeff Roberson #include <vm/uma.h>
57df8bae1dSRodney W. Grimes 
5811caded3SAlfred Perlstein static void dev_pager_init(void);
5911caded3SAlfred Perlstein static vm_object_t dev_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
603364c323SKonstantin Belousov     vm_ooffset_t, struct ucred *);
6111caded3SAlfred Perlstein static void dev_pager_dealloc(vm_object_t);
62b0cd2017SGleb Smirnoff static int dev_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
63b89def80SKonstantin Belousov static void dev_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
64b89def80SKonstantin Belousov static boolean_t dev_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
65b7ac5a85SKonstantin Belousov static void dev_pager_free_page(vm_object_t object, vm_page_t m);
66f708ef1bSPoul-Henning Kamp 
67f708ef1bSPoul-Henning Kamp /* list of device pager objects */
68f708ef1bSPoul-Henning Kamp static struct pagerlst dev_pager_object_list;
69a9fa2c05SAlfred Perlstein /* protect list manipulation */
70a9fa2c05SAlfred Perlstein static struct mtx dev_pager_mtx;
71a9fa2c05SAlfred Perlstein 
72df8bae1dSRodney W. Grimes struct pagerops devicepagerops = {
734e658600SPoul-Henning Kamp 	.pgo_init =	dev_pager_init,
744e658600SPoul-Henning Kamp 	.pgo_alloc =	dev_pager_alloc,
754e658600SPoul-Henning Kamp 	.pgo_dealloc =	dev_pager_dealloc,
764e658600SPoul-Henning Kamp 	.pgo_getpages =	dev_pager_getpages,
774e658600SPoul-Henning Kamp 	.pgo_putpages =	dev_pager_putpages,
784e658600SPoul-Henning Kamp 	.pgo_haspage =	dev_pager_haspage,
79df8bae1dSRodney W. Grimes };
80df8bae1dSRodney W. Grimes 
81b7ac5a85SKonstantin Belousov struct pagerops mgtdevicepagerops = {
82b7ac5a85SKonstantin Belousov 	.pgo_alloc =	dev_pager_alloc,
83b7ac5a85SKonstantin Belousov 	.pgo_dealloc =	dev_pager_dealloc,
84b7ac5a85SKonstantin Belousov 	.pgo_getpages =	dev_pager_getpages,
85b7ac5a85SKonstantin Belousov 	.pgo_putpages =	dev_pager_putpages,
86b7ac5a85SKonstantin Belousov 	.pgo_haspage =	dev_pager_haspage,
87b7ac5a85SKonstantin Belousov };
88b7ac5a85SKonstantin Belousov 
89286790a7SKonstantin Belousov static int old_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
90286790a7SKonstantin Belousov     vm_ooffset_t foff, struct ucred *cred, u_short *color);
91286790a7SKonstantin Belousov static void old_dev_pager_dtor(void *handle);
92286790a7SKonstantin Belousov static int old_dev_pager_fault(vm_object_t object, vm_ooffset_t offset,
93286790a7SKonstantin Belousov     int prot, vm_page_t *mres);
94286790a7SKonstantin Belousov 
95286790a7SKonstantin Belousov static struct cdev_pager_ops old_dev_pager_ops = {
96286790a7SKonstantin Belousov 	.cdev_pg_ctor =	old_dev_pager_ctor,
97286790a7SKonstantin Belousov 	.cdev_pg_dtor =	old_dev_pager_dtor,
98286790a7SKonstantin Belousov 	.cdev_pg_fault = old_dev_pager_fault
99286790a7SKonstantin Belousov };
100286790a7SKonstantin Belousov 
101f708ef1bSPoul-Henning Kamp static void
102b89def80SKonstantin Belousov dev_pager_init(void)
103df8bae1dSRodney W. Grimes {
104b89def80SKonstantin Belousov 
10524a1cce3SDavid Greenman 	TAILQ_INIT(&dev_pager_object_list);
1066008862bSJohn Baldwin 	mtx_init(&dev_pager_mtx, "dev_pager list", NULL, MTX_DEF);
107df8bae1dSRodney W. Grimes }
108df8bae1dSRodney W. Grimes 
109286790a7SKonstantin Belousov vm_object_t
110286790a7SKonstantin Belousov cdev_pager_lookup(void *handle)
111df8bae1dSRodney W. Grimes {
112286790a7SKonstantin Belousov 	vm_object_t object;
113286790a7SKonstantin Belousov 
114286790a7SKonstantin Belousov 	mtx_lock(&dev_pager_mtx);
115286790a7SKonstantin Belousov 	object = vm_pager_object_lookup(&dev_pager_object_list, handle);
116286790a7SKonstantin Belousov 	mtx_unlock(&dev_pager_mtx);
117286790a7SKonstantin Belousov 	return (object);
118286790a7SKonstantin Belousov }
119286790a7SKonstantin Belousov 
120286790a7SKonstantin Belousov vm_object_t
121286790a7SKonstantin Belousov cdev_pager_allocate(void *handle, enum obj_type tp, struct cdev_pager_ops *ops,
122286790a7SKonstantin Belousov     vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff, struct ucred *cred)
123286790a7SKonstantin Belousov {
124deea654eSKonstantin Belousov 	vm_object_t object, object1;
1252f7af3dbSAlan Cox 	vm_pindex_t pindex;
126286790a7SKonstantin Belousov 	u_short color;
127286790a7SKonstantin Belousov 
128b7ac5a85SKonstantin Belousov 	if (tp != OBJT_DEVICE && tp != OBJT_MGTDEVICE)
129286790a7SKonstantin Belousov 		return (NULL);
130df8bae1dSRodney W. Grimes 
131df8bae1dSRodney W. Grimes 	/*
132df8bae1dSRodney W. Grimes 	 * Offset should be page aligned.
133df8bae1dSRodney W. Grimes 	 */
134aa8de40aSPoul-Henning Kamp 	if (foff & PAGE_MASK)
135df8bae1dSRodney W. Grimes 		return (NULL);
136df8bae1dSRodney W. Grimes 
1376cde7a16SDavid Greenman 	size = round_page(size);
1382f7af3dbSAlan Cox 	pindex = OFF_TO_IDX(foff + size);
1396cde7a16SDavid Greenman 
140286790a7SKonstantin Belousov 	if (ops->cdev_pg_ctor(handle, size, prot, foff, cred, &color) != 0)
141c8664f82SAlan Cox 		return (NULL);
142deea654eSKonstantin Belousov 	mtx_lock(&dev_pager_mtx);
14324a1cce3SDavid Greenman 
14424a1cce3SDavid Greenman 	/*
145df8bae1dSRodney W. Grimes 	 * Look up pager, creating as necessary.
146df8bae1dSRodney W. Grimes 	 */
147deea654eSKonstantin Belousov 	object1 = NULL;
14824a1cce3SDavid Greenman 	object = vm_pager_object_lookup(&dev_pager_object_list, handle);
14924a1cce3SDavid Greenman 	if (object == NULL) {
150df8bae1dSRodney W. Grimes 		/*
151e46cd413SAlan Cox 		 * Allocate object and associate it with the pager.  Initialize
152e46cd413SAlan Cox 		 * the object's pg_color based upon the physical address of the
153e46cd413SAlan Cox 		 * device's memory.
154df8bae1dSRodney W. Grimes 		 */
155deea654eSKonstantin Belousov 		mtx_unlock(&dev_pager_mtx);
156286790a7SKonstantin Belousov 		object1 = vm_object_allocate(tp, pindex);
157e46cd413SAlan Cox 		object1->flags |= OBJ_COLORED;
158286790a7SKonstantin Belousov 		object1->pg_color = color;
159286790a7SKonstantin Belousov 		object1->handle = handle;
160286790a7SKonstantin Belousov 		object1->un_pager.devp.ops = ops;
16143ab9660SKenneth D. Merry 		object1->un_pager.devp.dev = handle;
162339772b0SKonstantin Belousov 		TAILQ_INIT(&object1->un_pager.devp.devp_pglist);
163deea654eSKonstantin Belousov 		mtx_lock(&dev_pager_mtx);
164deea654eSKonstantin Belousov 		object = vm_pager_object_lookup(&dev_pager_object_list, handle);
165deea654eSKonstantin Belousov 		if (object != NULL) {
166deea654eSKonstantin Belousov 			/*
167deea654eSKonstantin Belousov 			 * We raced with other thread while allocating object.
168deea654eSKonstantin Belousov 			 */
169deea654eSKonstantin Belousov 			if (pindex > object->size)
170deea654eSKonstantin Belousov 				object->size = pindex;
1718add0cedSJohn Baldwin 			KASSERT(object->type == tp,
1728add0cedSJohn Baldwin 		    ("Inconsistent device pager type %p %d", object, tp));
173deea654eSKonstantin Belousov 		} else {
174deea654eSKonstantin Belousov 			object = object1;
175deea654eSKonstantin Belousov 			object1 = NULL;
17624a1cce3SDavid Greenman 			object->handle = handle;
177deea654eSKonstantin Belousov 			TAILQ_INSERT_TAIL(&dev_pager_object_list, object,
178deea654eSKonstantin Belousov 			    pager_object_list);
179deea654eSKonstantin Belousov 		}
180df8bae1dSRodney W. Grimes 	} else {
1812f7af3dbSAlan Cox 		if (pindex > object->size)
1822f7af3dbSAlan Cox 			object->size = pindex;
1838add0cedSJohn Baldwin 		KASSERT(object->type == tp,
1848add0cedSJohn Baldwin 		    ("Inconsistent device pager type %p %d", object, tp));
185df8bae1dSRodney W. Grimes 	}
186deea654eSKonstantin Belousov 	mtx_unlock(&dev_pager_mtx);
187339772b0SKonstantin Belousov 	if (object1 != NULL) {
188339772b0SKonstantin Belousov 		object1->handle = object1;
189339772b0SKonstantin Belousov 		mtx_lock(&dev_pager_mtx);
190339772b0SKonstantin Belousov 		TAILQ_INSERT_TAIL(&dev_pager_object_list, object1,
191339772b0SKonstantin Belousov 		    pager_object_list);
192339772b0SKonstantin Belousov 		mtx_unlock(&dev_pager_mtx);
193deea654eSKonstantin Belousov 		vm_object_deallocate(object1);
194339772b0SKonstantin Belousov 	}
19524a1cce3SDavid Greenman 	return (object);
19624a1cce3SDavid Greenman }
19724a1cce3SDavid Greenman 
198286790a7SKonstantin Belousov static vm_object_t
199286790a7SKonstantin Belousov dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
200286790a7SKonstantin Belousov     vm_ooffset_t foff, struct ucred *cred)
201286790a7SKonstantin Belousov {
202286790a7SKonstantin Belousov 
203286790a7SKonstantin Belousov 	return (cdev_pager_allocate(handle, OBJT_DEVICE, &old_dev_pager_ops,
204286790a7SKonstantin Belousov 	    size, prot, foff, cred));
205286790a7SKonstantin Belousov }
206286790a7SKonstantin Belousov 
207286790a7SKonstantin Belousov void
208286790a7SKonstantin Belousov cdev_pager_free_page(vm_object_t object, vm_page_t m)
209286790a7SKonstantin Belousov {
210286790a7SKonstantin Belousov 
21189f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
212b7ac5a85SKonstantin Belousov 	if (object->type == OBJT_MGTDEVICE) {
213b7ac5a85SKonstantin Belousov 		KASSERT((m->oflags & VPO_UNMANAGED) == 0, ("unmanaged %p", m));
214b7ac5a85SKonstantin Belousov 		pmap_remove_all(m);
215b7ac5a85SKonstantin Belousov 		vm_page_lock(m);
216b7ac5a85SKonstantin Belousov 		vm_page_remove(m);
217b7ac5a85SKonstantin Belousov 		vm_page_unlock(m);
218b7ac5a85SKonstantin Belousov 	} else if (object->type == OBJT_DEVICE)
219b7ac5a85SKonstantin Belousov 		dev_pager_free_page(object, m);
220b7ac5a85SKonstantin Belousov }
221b7ac5a85SKonstantin Belousov 
222b7ac5a85SKonstantin Belousov static void
223b7ac5a85SKonstantin Belousov dev_pager_free_page(vm_object_t object, vm_page_t m)
224b7ac5a85SKonstantin Belousov {
225b7ac5a85SKonstantin Belousov 
22689f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
227b7ac5a85SKonstantin Belousov 	KASSERT((object->type == OBJT_DEVICE &&
228b7ac5a85SKonstantin Belousov 	    (m->oflags & VPO_UNMANAGED) != 0),
229b7ac5a85SKonstantin Belousov 	    ("Managed device or page obj %p m %p", object, m));
230c325e866SKonstantin Belousov 	TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, plinks.q);
231286790a7SKonstantin Belousov 	vm_page_putfake(m);
232286790a7SKonstantin Belousov }
233286790a7SKonstantin Belousov 
234f708ef1bSPoul-Henning Kamp static void
235b89def80SKonstantin Belousov dev_pager_dealloc(vm_object_t object)
23624a1cce3SDavid Greenman {
237df8bae1dSRodney W. Grimes 	vm_page_t m;
238df8bae1dSRodney W. Grimes 
23989f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
24043ab9660SKenneth D. Merry 	object->un_pager.devp.ops->cdev_pg_dtor(object->un_pager.devp.dev);
241286790a7SKonstantin Belousov 
242a9fa2c05SAlfred Perlstein 	mtx_lock(&dev_pager_mtx);
24324a1cce3SDavid Greenman 	TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list);
244a9fa2c05SAlfred Perlstein 	mtx_unlock(&dev_pager_mtx);
24589f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
246b7ac5a85SKonstantin Belousov 
247b7ac5a85SKonstantin Belousov 	if (object->type == OBJT_DEVICE) {
248df8bae1dSRodney W. Grimes 		/*
249df8bae1dSRodney W. Grimes 		 * Free up our fake pages.
250df8bae1dSRodney W. Grimes 		 */
251b7ac5a85SKonstantin Belousov 		while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist))
252b7ac5a85SKonstantin Belousov 		    != NULL)
253b7ac5a85SKonstantin Belousov 			dev_pager_free_page(object, m);
254b7ac5a85SKonstantin Belousov 	}
255e735691bSJohn Baldwin 	object->handle = NULL;
256e735691bSJohn Baldwin 	object->type = OBJT_DEAD;
257df8bae1dSRodney W. Grimes }
258df8bae1dSRodney W. Grimes 
259f708ef1bSPoul-Henning Kamp static int
260b0cd2017SGleb Smirnoff dev_pager_getpages(vm_object_t object, vm_page_t *ma, int count, int *rbehind,
261b0cd2017SGleb Smirnoff     int *rahead)
262df8bae1dSRodney W. Grimes {
263b89def80SKonstantin Belousov 	int error;
264df8bae1dSRodney W. Grimes 
265b0cd2017SGleb Smirnoff 	/* Since our haspage reports zero after/before, the count is 1. */
266b0cd2017SGleb Smirnoff 	KASSERT(count == 1, ("%s: count %d", __func__, count));
26789f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
268286790a7SKonstantin Belousov 	error = object->un_pager.devp.ops->cdev_pg_fault(object,
269b0cd2017SGleb Smirnoff 	    IDX_TO_OFF(ma[0]->pindex), PROT_READ, &ma[0]);
270286790a7SKonstantin Belousov 
27189f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
272286790a7SKonstantin Belousov 
273286790a7SKonstantin Belousov 	if (error == VM_PAGER_OK) {
274b7ac5a85SKonstantin Belousov 		KASSERT((object->type == OBJT_DEVICE &&
275b0cd2017SGleb Smirnoff 		     (ma[0]->oflags & VPO_UNMANAGED) != 0) ||
276b7ac5a85SKonstantin Belousov 		    (object->type == OBJT_MGTDEVICE &&
277b0cd2017SGleb Smirnoff 		     (ma[0]->oflags & VPO_UNMANAGED) == 0),
278b0cd2017SGleb Smirnoff 		    ("Wrong page type %p %p", ma[0], object));
279b7ac5a85SKonstantin Belousov 		if (object->type == OBJT_DEVICE) {
280286790a7SKonstantin Belousov 			TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist,
281b0cd2017SGleb Smirnoff 			    ma[0], plinks.q);
282286790a7SKonstantin Belousov 		}
283b0cd2017SGleb Smirnoff 		if (rbehind)
284b0cd2017SGleb Smirnoff 			*rbehind = 0;
285b0cd2017SGleb Smirnoff 		if (rahead)
286b0cd2017SGleb Smirnoff 			*rahead = 0;
287b7ac5a85SKonstantin Belousov 	}
288286790a7SKonstantin Belousov 
289286790a7SKonstantin Belousov 	return (error);
290286790a7SKonstantin Belousov }
291286790a7SKonstantin Belousov 
292286790a7SKonstantin Belousov static int
293286790a7SKonstantin Belousov old_dev_pager_fault(vm_object_t object, vm_ooffset_t offset, int prot,
294286790a7SKonstantin Belousov     vm_page_t *mres)
295286790a7SKonstantin Belousov {
296286790a7SKonstantin Belousov 	vm_paddr_t paddr;
297286790a7SKonstantin Belousov 	vm_page_t m_paddr, page;
298286790a7SKonstantin Belousov 	struct cdev *dev;
299286790a7SKonstantin Belousov 	struct cdevsw *csw;
300286790a7SKonstantin Belousov 	struct file *fpop;
301286790a7SKonstantin Belousov 	struct thread *td;
302*e9d37c9fSKonstantin Belousov 	vm_memattr_t memattr, memattr1;
303286790a7SKonstantin Belousov 	int ref, ret;
304286790a7SKonstantin Belousov 
3053153e878SAlan Cox 	memattr = object->memattr;
306286790a7SKonstantin Belousov 
30789f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
308286790a7SKonstantin Belousov 
309286790a7SKonstantin Belousov 	dev = object->handle;
3103979450bSKonstantin Belousov 	csw = dev_refthread(dev, &ref);
31191a1929fSAttilio Rao 	if (csw == NULL) {
31289f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
31391a1929fSAttilio Rao 		return (VM_PAGER_FAIL);
31491a1929fSAttilio Rao 	}
3157818e0a5SKonstantin Belousov 	td = curthread;
3167818e0a5SKonstantin Belousov 	fpop = td->td_fpop;
3177818e0a5SKonstantin Belousov 	td->td_fpop = NULL;
318286790a7SKonstantin Belousov 	ret = csw->d_mmap(dev, offset, &paddr, prot, &memattr);
3197818e0a5SKonstantin Belousov 	td->td_fpop = fpop;
3203979450bSKonstantin Belousov 	dev_relthread(dev, ref);
321286790a7SKonstantin Belousov 	if (ret != 0) {
322286790a7SKonstantin Belousov 		printf(
323286790a7SKonstantin Belousov 	    "WARNING: dev_pager_getpage: map function returns error %d", ret);
32489f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
325286790a7SKonstantin Belousov 		return (VM_PAGER_FAIL);
326286790a7SKonstantin Belousov 	}
327286790a7SKonstantin Belousov 
3283153e878SAlan Cox 	/* If "paddr" is a real page, perform a sanity check on "memattr". */
3293153e878SAlan Cox 	if ((m_paddr = vm_phys_paddr_to_vm_page(paddr)) != NULL &&
330*e9d37c9fSKonstantin Belousov 	    (memattr1 = pmap_page_get_memattr(m_paddr)) != memattr) {
331*e9d37c9fSKonstantin Belousov 		/*
332*e9d37c9fSKonstantin Belousov 		 * For the /dev/mem d_mmap routine to return the
333*e9d37c9fSKonstantin Belousov 		 * correct memattr, pmap_page_get_memattr() needs to
334*e9d37c9fSKonstantin Belousov 		 * be called, which we do there.
335*e9d37c9fSKonstantin Belousov 		 */
336*e9d37c9fSKonstantin Belousov 		if ((csw->d_flags & D_MEM) == 0) {
337*e9d37c9fSKonstantin Belousov 			printf("WARNING: Device driver %s has set "
338*e9d37c9fSKonstantin Belousov 			    "\"memattr\" inconsistently (drv %u pmap %u).\n",
339*e9d37c9fSKonstantin Belousov 			    csw->d_name, memattr, memattr1);
340*e9d37c9fSKonstantin Belousov 		}
341*e9d37c9fSKonstantin Belousov 		memattr = memattr1;
3423153e878SAlan Cox 	}
343286790a7SKonstantin Belousov 	if (((*mres)->flags & PG_FICTITIOUS) != 0) {
34492bab635SDoug Rabson 		/*
345286790a7SKonstantin Belousov 		 * If the passed in result page is a fake page, update it with
34692bab635SDoug Rabson 		 * the new physical address.
34792bab635SDoug Rabson 		 */
348286790a7SKonstantin Belousov 		page = *mres;
34989f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
35010cf2560SAlan Cox 		vm_page_updatefake(page, paddr, memattr);
35192bab635SDoug Rabson 	} else {
352df8bae1dSRodney W. Grimes 		/*
35307159f9cSMaxime Henrion 		 * Replace the passed in reqpage page with our own fake page and
35407159f9cSMaxime Henrion 		 * free up the all of the original pages.
355df8bae1dSRodney W. Grimes 		 */
35610cf2560SAlan Cox 		page = vm_page_getfake(paddr, memattr);
35789f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
3588170d6e5SConrad Meyer 		vm_page_replace_checked(page, object, (*mres)->pindex, *mres);
359286790a7SKonstantin Belousov 		vm_page_lock(*mres);
360286790a7SKonstantin Belousov 		vm_page_free(*mres);
361286790a7SKonstantin Belousov 		vm_page_unlock(*mres);
362286790a7SKonstantin Belousov 		*mres = page;
36392bab635SDoug Rabson 	}
364ce1b857fSAlan Cox 	page->valid = VM_PAGE_BITS_ALL;
365df8bae1dSRodney W. Grimes 	return (VM_PAGER_OK);
366df8bae1dSRodney W. Grimes }
367df8bae1dSRodney W. Grimes 
368e4542174SMatthew Dillon static void
369b89def80SKonstantin Belousov dev_pager_putpages(vm_object_t object, vm_page_t *m, int count, int flags,
370b89def80SKonstantin Belousov     int *rtvals)
371df8bae1dSRodney W. Grimes {
372286790a7SKonstantin Belousov 
373df8bae1dSRodney W. Grimes 	panic("dev_pager_putpage called");
374df8bae1dSRodney W. Grimes }
375df8bae1dSRodney W. Grimes 
376f708ef1bSPoul-Henning Kamp static boolean_t
377b89def80SKonstantin Belousov dev_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
378b89def80SKonstantin Belousov     int *after)
379df8bae1dSRodney W. Grimes {
380b89def80SKonstantin Belousov 
38124a1cce3SDavid Greenman 	if (before != NULL)
38224a1cce3SDavid Greenman 		*before = 0;
38324a1cce3SDavid Greenman 	if (after != NULL)
38424a1cce3SDavid Greenman 		*after = 0;
385df8bae1dSRodney W. Grimes 	return (TRUE);
386df8bae1dSRodney W. Grimes }
387286790a7SKonstantin Belousov 
388286790a7SKonstantin Belousov static int
389286790a7SKonstantin Belousov old_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
390286790a7SKonstantin Belousov     vm_ooffset_t foff, struct ucred *cred, u_short *color)
391286790a7SKonstantin Belousov {
392286790a7SKonstantin Belousov 	struct cdev *dev;
393286790a7SKonstantin Belousov 	struct cdevsw *csw;
394286790a7SKonstantin Belousov 	vm_memattr_t dummy;
395286790a7SKonstantin Belousov 	vm_ooffset_t off;
396286790a7SKonstantin Belousov 	vm_paddr_t paddr;
397286790a7SKonstantin Belousov 	unsigned int npages;
398286790a7SKonstantin Belousov 	int ref;
399286790a7SKonstantin Belousov 
400286790a7SKonstantin Belousov 	/*
401286790a7SKonstantin Belousov 	 * Make sure this device can be mapped.
402286790a7SKonstantin Belousov 	 */
403286790a7SKonstantin Belousov 	dev = handle;
404286790a7SKonstantin Belousov 	csw = dev_refthread(dev, &ref);
405286790a7SKonstantin Belousov 	if (csw == NULL)
406286790a7SKonstantin Belousov 		return (ENXIO);
407286790a7SKonstantin Belousov 
408286790a7SKonstantin Belousov 	/*
409286790a7SKonstantin Belousov 	 * Check that the specified range of the device allows the desired
410286790a7SKonstantin Belousov 	 * protection.
411286790a7SKonstantin Belousov 	 *
412286790a7SKonstantin Belousov 	 * XXX assumes VM_PROT_* == PROT_*
413286790a7SKonstantin Belousov 	 */
414286790a7SKonstantin Belousov 	npages = OFF_TO_IDX(size);
4157253a5ecSKonstantin Belousov 	paddr = 0; /* Make paddr initialized for the case of size == 0. */
416286790a7SKonstantin Belousov 	for (off = foff; npages--; off += PAGE_SIZE) {
417286790a7SKonstantin Belousov 		if (csw->d_mmap(dev, off, &paddr, (int)prot, &dummy) != 0) {
418286790a7SKonstantin Belousov 			dev_relthread(dev, ref);
419286790a7SKonstantin Belousov 			return (EINVAL);
420286790a7SKonstantin Belousov 		}
421286790a7SKonstantin Belousov 	}
422286790a7SKonstantin Belousov 
423286790a7SKonstantin Belousov 	dev_ref(dev);
424286790a7SKonstantin Belousov 	dev_relthread(dev, ref);
425286790a7SKonstantin Belousov 	*color = atop(paddr) - OFF_TO_IDX(off - PAGE_SIZE);
426286790a7SKonstantin Belousov 	return (0);
427286790a7SKonstantin Belousov }
428286790a7SKonstantin Belousov 
429286790a7SKonstantin Belousov static void
430286790a7SKonstantin Belousov old_dev_pager_dtor(void *handle)
431286790a7SKonstantin Belousov {
432286790a7SKonstantin Belousov 
433286790a7SKonstantin Belousov 	dev_rel(handle);
434286790a7SKonstantin Belousov }
435