xref: /freebsd/sys/vm/device_pager.c (revision e93404065177d6c909cd64bf7d74fe0d8df35edf)
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  */
36df8bae1dSRodney W. Grimes 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38df8bae1dSRodney W. Grimes #include <sys/systm.h>
39df8bae1dSRodney W. Grimes #include <sys/conf.h>
40fb919e4dSMark Murray #include <sys/lock.h>
410cddd8f0SMatthew Dillon #include <sys/proc.h>
42fb919e4dSMark Murray #include <sys/mutex.h>
43df8bae1dSRodney W. Grimes #include <sys/mman.h>
4489f6b863SAttilio Rao #include <sys/rwlock.h>
45a9fa2c05SAlfred Perlstein #include <sys/sx.h>
4600a3fe96SKonstantin Belousov #include <sys/user.h>
479ed01c32SGleb Smirnoff #include <sys/vmmeter.h>
48df8bae1dSRodney W. Grimes 
49df8bae1dSRodney W. Grimes #include <vm/vm.h>
501c771f92SKonstantin Belousov #include <vm/vm_param.h>
51efeaf95aSDavid Greenman #include <vm/vm_object.h>
52df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
5324a1cce3SDavid Greenman #include <vm/vm_pager.h>
5443f48b65SKonstantin Belousov #include <vm/vm_phys.h>
55670d17b5SJeff Roberson #include <vm/uma.h>
56df8bae1dSRodney W. Grimes 
5711caded3SAlfred Perlstein static void dev_pager_init(void);
5811caded3SAlfred Perlstein static vm_object_t dev_pager_alloc(void *, vm_ooffset_t, vm_prot_t,
593364c323SKonstantin Belousov     vm_ooffset_t, struct ucred *);
6011caded3SAlfred Perlstein static void dev_pager_dealloc(vm_object_t);
61b0cd2017SGleb Smirnoff static int dev_pager_getpages(vm_object_t, vm_page_t *, int, int *, int *);
62b89def80SKonstantin Belousov static void dev_pager_putpages(vm_object_t, vm_page_t *, int, int, int *);
63b89def80SKonstantin Belousov static boolean_t dev_pager_haspage(vm_object_t, vm_pindex_t, int *, int *);
64b7ac5a85SKonstantin Belousov static void dev_pager_free_page(vm_object_t object, vm_page_t m);
65c42b43a0SKonstantin Belousov static int dev_pager_populate(vm_object_t object, vm_pindex_t pidx,
66c42b43a0SKonstantin Belousov     int fault_type, vm_prot_t, vm_pindex_t *first, vm_pindex_t *last);
67f708ef1bSPoul-Henning Kamp 
68f708ef1bSPoul-Henning Kamp /* list of device pager objects */
69f708ef1bSPoul-Henning Kamp static struct pagerlst dev_pager_object_list;
70a9fa2c05SAlfred Perlstein /* protect list manipulation */
71a9fa2c05SAlfred Perlstein static struct mtx dev_pager_mtx;
72a9fa2c05SAlfred Perlstein 
73d474440aSKonstantin Belousov const struct pagerops devicepagerops = {
7400a3fe96SKonstantin Belousov 	.pgo_kvme_type = KVME_TYPE_DEVICE,
754e658600SPoul-Henning Kamp 	.pgo_init =	dev_pager_init,
764e658600SPoul-Henning Kamp 	.pgo_alloc =	dev_pager_alloc,
774e658600SPoul-Henning Kamp 	.pgo_dealloc =	dev_pager_dealloc,
784e658600SPoul-Henning Kamp 	.pgo_getpages =	dev_pager_getpages,
794e658600SPoul-Henning Kamp 	.pgo_putpages =	dev_pager_putpages,
804e658600SPoul-Henning Kamp 	.pgo_haspage =	dev_pager_haspage,
81df8bae1dSRodney W. Grimes };
82df8bae1dSRodney W. Grimes 
83d474440aSKonstantin Belousov const struct pagerops mgtdevicepagerops = {
8400a3fe96SKonstantin Belousov 	.pgo_kvme_type = KVME_TYPE_MGTDEVICE,
85b7ac5a85SKonstantin Belousov 	.pgo_alloc =	dev_pager_alloc,
86b7ac5a85SKonstantin Belousov 	.pgo_dealloc =	dev_pager_dealloc,
87b7ac5a85SKonstantin Belousov 	.pgo_getpages =	dev_pager_getpages,
88b7ac5a85SKonstantin Belousov 	.pgo_putpages =	dev_pager_putpages,
89b7ac5a85SKonstantin Belousov 	.pgo_haspage =	dev_pager_haspage,
90c42b43a0SKonstantin Belousov 	.pgo_populate =	dev_pager_populate,
91b7ac5a85SKonstantin Belousov };
92b7ac5a85SKonstantin Belousov 
93286790a7SKonstantin Belousov static int old_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
94286790a7SKonstantin Belousov     vm_ooffset_t foff, struct ucred *cred, u_short *color);
95286790a7SKonstantin Belousov static void old_dev_pager_dtor(void *handle);
96286790a7SKonstantin Belousov static int old_dev_pager_fault(vm_object_t object, vm_ooffset_t offset,
97286790a7SKonstantin Belousov     int prot, vm_page_t *mres);
98286790a7SKonstantin Belousov 
99d474440aSKonstantin Belousov static const struct cdev_pager_ops old_dev_pager_ops = {
100286790a7SKonstantin Belousov 	.cdev_pg_ctor =	old_dev_pager_ctor,
101286790a7SKonstantin Belousov 	.cdev_pg_dtor =	old_dev_pager_dtor,
102286790a7SKonstantin Belousov 	.cdev_pg_fault = old_dev_pager_fault
103286790a7SKonstantin Belousov };
104286790a7SKonstantin Belousov 
105f708ef1bSPoul-Henning Kamp static void
106b89def80SKonstantin Belousov dev_pager_init(void)
107df8bae1dSRodney W. Grimes {
108b89def80SKonstantin Belousov 
10924a1cce3SDavid Greenman 	TAILQ_INIT(&dev_pager_object_list);
1106008862bSJohn Baldwin 	mtx_init(&dev_pager_mtx, "dev_pager list", NULL, MTX_DEF);
111df8bae1dSRodney W. Grimes }
112df8bae1dSRodney W. Grimes 
113286790a7SKonstantin Belousov vm_object_t
114286790a7SKonstantin Belousov cdev_pager_lookup(void *handle)
115df8bae1dSRodney W. Grimes {
116286790a7SKonstantin Belousov 	vm_object_t object;
117286790a7SKonstantin Belousov 
118*e9340406SKonstantin Belousov again:
119286790a7SKonstantin Belousov 	mtx_lock(&dev_pager_mtx);
120286790a7SKonstantin Belousov 	object = vm_pager_object_lookup(&dev_pager_object_list, handle);
121*e9340406SKonstantin Belousov 	if (object != NULL && object->un_pager.devp.dev == NULL) {
122*e9340406SKonstantin Belousov 		msleep(&object->un_pager.devp.dev, &dev_pager_mtx,
123*e9340406SKonstantin Belousov 		    PVM | PDROP, "cdplkp", 0);
124*e9340406SKonstantin Belousov 		vm_object_deallocate(object);
125*e9340406SKonstantin Belousov 		goto again;
126*e9340406SKonstantin Belousov 	}
127286790a7SKonstantin Belousov 	mtx_unlock(&dev_pager_mtx);
128286790a7SKonstantin Belousov 	return (object);
129286790a7SKonstantin Belousov }
130286790a7SKonstantin Belousov 
131286790a7SKonstantin Belousov vm_object_t
132d474440aSKonstantin Belousov cdev_pager_allocate(void *handle, enum obj_type tp,
133d474440aSKonstantin Belousov     const struct cdev_pager_ops *ops, vm_ooffset_t size, vm_prot_t prot,
134d474440aSKonstantin Belousov     vm_ooffset_t foff, struct ucred *cred)
135286790a7SKonstantin Belousov {
136*e9340406SKonstantin Belousov 	vm_object_t object;
1372f7af3dbSAlan Cox 	vm_pindex_t pindex;
138286790a7SKonstantin Belousov 
139b7ac5a85SKonstantin Belousov 	if (tp != OBJT_DEVICE && tp != OBJT_MGTDEVICE)
140286790a7SKonstantin Belousov 		return (NULL);
141c42b43a0SKonstantin Belousov 	KASSERT(tp == OBJT_MGTDEVICE || ops->cdev_pg_populate == NULL,
142c42b43a0SKonstantin Belousov 	    ("populate on unmanaged device pager"));
143df8bae1dSRodney W. Grimes 
144df8bae1dSRodney W. Grimes 	/*
145df8bae1dSRodney W. Grimes 	 * Offset should be page aligned.
146df8bae1dSRodney W. Grimes 	 */
147aa8de40aSPoul-Henning Kamp 	if (foff & PAGE_MASK)
148df8bae1dSRodney W. Grimes 		return (NULL);
149df8bae1dSRodney W. Grimes 
150987ff181SKonstantin Belousov 	/*
151987ff181SKonstantin Belousov 	 * Treat the mmap(2) file offset as an unsigned value for a
152987ff181SKonstantin Belousov 	 * device mapping.  This, in effect, allows a user to pass all
153987ff181SKonstantin Belousov 	 * possible off_t values as the mapping cookie to the driver.  At
154987ff181SKonstantin Belousov 	 * this point, we know that both foff and size are a multiple
155987ff181SKonstantin Belousov 	 * of the page size.  Do a check to avoid wrap.
156987ff181SKonstantin Belousov 	 */
1576cde7a16SDavid Greenman 	size = round_page(size);
15810d9120cSKonstantin Belousov 	pindex = OFF_TO_IDX(foff) + OFF_TO_IDX(size);
15910d9120cSKonstantin Belousov 	if (pindex > OBJ_MAX_SIZE || pindex < OFF_TO_IDX(foff) ||
16010d9120cSKonstantin Belousov 	    pindex < OFF_TO_IDX(size))
161987ff181SKonstantin Belousov 		return (NULL);
1626cde7a16SDavid Greenman 
163*e9340406SKonstantin Belousov again:
164deea654eSKonstantin Belousov 	mtx_lock(&dev_pager_mtx);
16524a1cce3SDavid Greenman 
16624a1cce3SDavid Greenman 	/*
167df8bae1dSRodney W. Grimes 	 * Look up pager, creating as necessary.
168df8bae1dSRodney W. Grimes 	 */
16924a1cce3SDavid Greenman 	object = vm_pager_object_lookup(&dev_pager_object_list, handle);
17024a1cce3SDavid Greenman 	if (object == NULL) {
171*e9340406SKonstantin Belousov 		vm_object_t object1;
172*e9340406SKonstantin Belousov 
173df8bae1dSRodney W. Grimes 		/*
174e46cd413SAlan Cox 		 * Allocate object and associate it with the pager.  Initialize
175e46cd413SAlan Cox 		 * the object's pg_color based upon the physical address of the
176e46cd413SAlan Cox 		 * device's memory.
177df8bae1dSRodney W. Grimes 		 */
178deea654eSKonstantin Belousov 		mtx_unlock(&dev_pager_mtx);
179286790a7SKonstantin Belousov 		object1 = vm_object_allocate(tp, pindex);
180deea654eSKonstantin Belousov 		mtx_lock(&dev_pager_mtx);
181deea654eSKonstantin Belousov 		object = vm_pager_object_lookup(&dev_pager_object_list, handle);
182deea654eSKonstantin Belousov 		if (object != NULL) {
183*e9340406SKonstantin Belousov 			object1->type = OBJT_DEAD;
184*e9340406SKonstantin Belousov 			vm_object_deallocate(object1);
185*e9340406SKonstantin Belousov 			object1 = NULL;
186*e9340406SKonstantin Belousov 			if (object->un_pager.devp.dev == NULL) {
187*e9340406SKonstantin Belousov 				msleep(&object->un_pager.devp.dev,
188*e9340406SKonstantin Belousov 				    &dev_pager_mtx, PVM | PDROP, "cdplkp", 0);
189*e9340406SKonstantin Belousov 				vm_object_deallocate(object);
190*e9340406SKonstantin Belousov 				goto again;
191*e9340406SKonstantin Belousov 			}
192*e9340406SKonstantin Belousov 
193deea654eSKonstantin Belousov 			/*
194deea654eSKonstantin Belousov 			 * We raced with other thread while allocating object.
195deea654eSKonstantin Belousov 			 */
196deea654eSKonstantin Belousov 			if (pindex > object->size)
197deea654eSKonstantin Belousov 				object->size = pindex;
1988add0cedSJohn Baldwin 			KASSERT(object->type == tp,
1991be02479SKonstantin Belousov 			    ("Inconsistent device pager type %p %d",
2001be02479SKonstantin Belousov 			    object, tp));
2011be02479SKonstantin Belousov 			KASSERT(object->un_pager.devp.ops == ops,
2021be02479SKonstantin Belousov 			    ("Inconsistent devops %p %p", object, ops));
203deea654eSKonstantin Belousov 		} else {
204*e9340406SKonstantin Belousov 			u_short color;
205*e9340406SKonstantin Belousov 
206deea654eSKonstantin Belousov 			object = object1;
207deea654eSKonstantin Belousov 			object1 = NULL;
20824a1cce3SDavid Greenman 			object->handle = handle;
209*e9340406SKonstantin Belousov 			object->un_pager.devp.ops = ops;
210*e9340406SKonstantin Belousov 			TAILQ_INIT(&object->un_pager.devp.devp_pglist);
211deea654eSKonstantin Belousov 			TAILQ_INSERT_TAIL(&dev_pager_object_list, object,
212deea654eSKonstantin Belousov 			    pager_object_list);
213*e9340406SKonstantin Belousov 			mtx_unlock(&dev_pager_mtx);
214c42b43a0SKonstantin Belousov 			if (ops->cdev_pg_populate != NULL)
215c42b43a0SKonstantin Belousov 				vm_object_set_flag(object, OBJ_POPULATE);
216*e9340406SKonstantin Belousov 			if (ops->cdev_pg_ctor(handle, size, prot, foff,
217*e9340406SKonstantin Belousov 			    cred, &color) != 0) {
218*e9340406SKonstantin Belousov 				mtx_lock(&dev_pager_mtx);
219*e9340406SKonstantin Belousov 				TAILQ_REMOVE(&dev_pager_object_list, object,
220*e9340406SKonstantin Belousov 				    pager_object_list);
221*e9340406SKonstantin Belousov 				wakeup(&object->un_pager.devp.dev);
222*e9340406SKonstantin Belousov 				mtx_unlock(&dev_pager_mtx);
223*e9340406SKonstantin Belousov 				object->type = OBJT_DEAD;
224*e9340406SKonstantin Belousov 				vm_object_deallocate(object);
225*e9340406SKonstantin Belousov 				object = NULL;
226*e9340406SKonstantin Belousov 				mtx_lock(&dev_pager_mtx);
227df8bae1dSRodney W. Grimes 			} else {
228*e9340406SKonstantin Belousov 				mtx_lock(&dev_pager_mtx);
229*e9340406SKonstantin Belousov 				object->flags |= OBJ_COLORED;
230*e9340406SKonstantin Belousov 				object->pg_color = color;
231*e9340406SKonstantin Belousov 				object->un_pager.devp.dev = handle;
232*e9340406SKonstantin Belousov 				wakeup(&object->un_pager.devp.dev);
233*e9340406SKonstantin Belousov 			}
234*e9340406SKonstantin Belousov 		}
235*e9340406SKonstantin Belousov 		MPASS(object1 == NULL);
236*e9340406SKonstantin Belousov 	} else {
237*e9340406SKonstantin Belousov 		if (object->un_pager.devp.dev == NULL) {
238*e9340406SKonstantin Belousov 			msleep(&object->un_pager.devp.dev,
239*e9340406SKonstantin Belousov 			    &dev_pager_mtx, PVM | PDROP, "cdplkp", 0);
240*e9340406SKonstantin Belousov 			vm_object_deallocate(object);
241*e9340406SKonstantin Belousov 			goto again;
242*e9340406SKonstantin Belousov 		}
2432f7af3dbSAlan Cox 		if (pindex > object->size)
2442f7af3dbSAlan Cox 			object->size = pindex;
2458add0cedSJohn Baldwin 		KASSERT(object->type == tp,
2468add0cedSJohn Baldwin 		    ("Inconsistent device pager type %p %d", object, tp));
247df8bae1dSRodney W. Grimes 	}
248deea654eSKonstantin Belousov 	mtx_unlock(&dev_pager_mtx);
24924a1cce3SDavid Greenman 	return (object);
25024a1cce3SDavid Greenman }
25124a1cce3SDavid Greenman 
252286790a7SKonstantin Belousov static vm_object_t
253286790a7SKonstantin Belousov dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
254286790a7SKonstantin Belousov     vm_ooffset_t foff, struct ucred *cred)
255286790a7SKonstantin Belousov {
256286790a7SKonstantin Belousov 
257286790a7SKonstantin Belousov 	return (cdev_pager_allocate(handle, OBJT_DEVICE, &old_dev_pager_ops,
258286790a7SKonstantin Belousov 	    size, prot, foff, cred));
259286790a7SKonstantin Belousov }
260286790a7SKonstantin Belousov 
261286790a7SKonstantin Belousov void
262286790a7SKonstantin Belousov cdev_pager_free_page(vm_object_t object, vm_page_t m)
263286790a7SKonstantin Belousov {
264286790a7SKonstantin Belousov 
26589f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
266b7ac5a85SKonstantin Belousov 	if (object->type == OBJT_MGTDEVICE) {
267b7ac5a85SKonstantin Belousov 		KASSERT((m->oflags & VPO_UNMANAGED) == 0, ("unmanaged %p", m));
268b7ac5a85SKonstantin Belousov 		pmap_remove_all(m);
2690fd977b3SMark Johnston 		(void)vm_page_remove(m);
270b7ac5a85SKonstantin Belousov 	} else if (object->type == OBJT_DEVICE)
271b7ac5a85SKonstantin Belousov 		dev_pager_free_page(object, m);
272b7ac5a85SKonstantin Belousov }
273b7ac5a85SKonstantin Belousov 
274b7ac5a85SKonstantin Belousov static void
275b7ac5a85SKonstantin Belousov dev_pager_free_page(vm_object_t object, vm_page_t m)
276b7ac5a85SKonstantin Belousov {
277b7ac5a85SKonstantin Belousov 
27889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
279b7ac5a85SKonstantin Belousov 	KASSERT((object->type == OBJT_DEVICE &&
280b7ac5a85SKonstantin Belousov 	    (m->oflags & VPO_UNMANAGED) != 0),
281b7ac5a85SKonstantin Belousov 	    ("Managed device or page obj %p m %p", object, m));
282c325e866SKonstantin Belousov 	TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, plinks.q);
283286790a7SKonstantin Belousov 	vm_page_putfake(m);
284286790a7SKonstantin Belousov }
285286790a7SKonstantin Belousov 
286f708ef1bSPoul-Henning Kamp static void
287b89def80SKonstantin Belousov dev_pager_dealloc(vm_object_t object)
28824a1cce3SDavid Greenman {
289df8bae1dSRodney W. Grimes 	vm_page_t m;
290df8bae1dSRodney W. Grimes 
29189f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
29243ab9660SKenneth D. Merry 	object->un_pager.devp.ops->cdev_pg_dtor(object->un_pager.devp.dev);
293286790a7SKonstantin Belousov 
294a9fa2c05SAlfred Perlstein 	mtx_lock(&dev_pager_mtx);
29524a1cce3SDavid Greenman 	TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list);
296a9fa2c05SAlfred Perlstein 	mtx_unlock(&dev_pager_mtx);
29789f6b863SAttilio Rao 	VM_OBJECT_WLOCK(object);
298b7ac5a85SKonstantin Belousov 
299b7ac5a85SKonstantin Belousov 	if (object->type == OBJT_DEVICE) {
300df8bae1dSRodney W. Grimes 		/*
301df8bae1dSRodney W. Grimes 		 * Free up our fake pages.
302df8bae1dSRodney W. Grimes 		 */
303b7ac5a85SKonstantin Belousov 		while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist))
3043cf3b4e6SJeff Roberson 		    != NULL) {
3053cf3b4e6SJeff Roberson 			if (vm_page_busy_acquire(m, VM_ALLOC_WAITFAIL) == 0)
3063cf3b4e6SJeff Roberson 				continue;
3073cf3b4e6SJeff Roberson 
308b7ac5a85SKonstantin Belousov 			dev_pager_free_page(object, m);
309b7ac5a85SKonstantin Belousov 		}
3103cf3b4e6SJeff Roberson 	}
311e735691bSJohn Baldwin 	object->handle = NULL;
312e735691bSJohn Baldwin 	object->type = OBJT_DEAD;
313df8bae1dSRodney W. Grimes }
314df8bae1dSRodney W. Grimes 
315f708ef1bSPoul-Henning Kamp static int
316b0cd2017SGleb Smirnoff dev_pager_getpages(vm_object_t object, vm_page_t *ma, int count, int *rbehind,
317b0cd2017SGleb Smirnoff     int *rahead)
318df8bae1dSRodney W. Grimes {
319b89def80SKonstantin Belousov 	int error;
320df8bae1dSRodney W. Grimes 
321b0cd2017SGleb Smirnoff 	/* Since our haspage reports zero after/before, the count is 1. */
322b0cd2017SGleb Smirnoff 	KASSERT(count == 1, ("%s: count %d", __func__, count));
323c42b43a0SKonstantin Belousov 	if (object->un_pager.devp.ops->cdev_pg_fault == NULL)
324c42b43a0SKonstantin Belousov 		return (VM_PAGER_FAIL);
325d6e13f3bSJeff Roberson 	VM_OBJECT_WLOCK(object);
326286790a7SKonstantin Belousov 	error = object->un_pager.devp.ops->cdev_pg_fault(object,
327b0cd2017SGleb Smirnoff 	    IDX_TO_OFF(ma[0]->pindex), PROT_READ, &ma[0]);
328286790a7SKonstantin Belousov 
32989f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
330286790a7SKonstantin Belousov 
331286790a7SKonstantin Belousov 	if (error == VM_PAGER_OK) {
332b7ac5a85SKonstantin Belousov 		KASSERT((object->type == OBJT_DEVICE &&
333b0cd2017SGleb Smirnoff 		     (ma[0]->oflags & VPO_UNMANAGED) != 0) ||
334b7ac5a85SKonstantin Belousov 		    (object->type == OBJT_MGTDEVICE &&
335b0cd2017SGleb Smirnoff 		     (ma[0]->oflags & VPO_UNMANAGED) == 0),
336b0cd2017SGleb Smirnoff 		    ("Wrong page type %p %p", ma[0], object));
337b7ac5a85SKonstantin Belousov 		if (object->type == OBJT_DEVICE) {
338286790a7SKonstantin Belousov 			TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist,
339b0cd2017SGleb Smirnoff 			    ma[0], plinks.q);
340286790a7SKonstantin Belousov 		}
341b0cd2017SGleb Smirnoff 		if (rbehind)
342b0cd2017SGleb Smirnoff 			*rbehind = 0;
343b0cd2017SGleb Smirnoff 		if (rahead)
344b0cd2017SGleb Smirnoff 			*rahead = 0;
345b7ac5a85SKonstantin Belousov 	}
346d6e13f3bSJeff Roberson 	VM_OBJECT_WUNLOCK(object);
347286790a7SKonstantin Belousov 
348286790a7SKonstantin Belousov 	return (error);
349286790a7SKonstantin Belousov }
350286790a7SKonstantin Belousov 
351286790a7SKonstantin Belousov static int
352c42b43a0SKonstantin Belousov dev_pager_populate(vm_object_t object, vm_pindex_t pidx, int fault_type,
353c42b43a0SKonstantin Belousov     vm_prot_t max_prot, vm_pindex_t *first, vm_pindex_t *last)
354c42b43a0SKonstantin Belousov {
355c42b43a0SKonstantin Belousov 
356c42b43a0SKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED(object);
357c42b43a0SKonstantin Belousov 	if (object->un_pager.devp.ops->cdev_pg_populate == NULL)
358c42b43a0SKonstantin Belousov 		return (VM_PAGER_FAIL);
359c42b43a0SKonstantin Belousov 	return (object->un_pager.devp.ops->cdev_pg_populate(object, pidx,
360c42b43a0SKonstantin Belousov 	    fault_type, max_prot, first, last));
361c42b43a0SKonstantin Belousov }
362c42b43a0SKonstantin Belousov 
363c42b43a0SKonstantin Belousov static int
364286790a7SKonstantin Belousov old_dev_pager_fault(vm_object_t object, vm_ooffset_t offset, int prot,
365286790a7SKonstantin Belousov     vm_page_t *mres)
366286790a7SKonstantin Belousov {
367286790a7SKonstantin Belousov 	vm_paddr_t paddr;
368286790a7SKonstantin Belousov 	vm_page_t m_paddr, page;
369286790a7SKonstantin Belousov 	struct cdev *dev;
370286790a7SKonstantin Belousov 	struct cdevsw *csw;
371286790a7SKonstantin Belousov 	struct file *fpop;
372286790a7SKonstantin Belousov 	struct thread *td;
373e9d37c9fSKonstantin Belousov 	vm_memattr_t memattr, memattr1;
374286790a7SKonstantin Belousov 	int ref, ret;
375286790a7SKonstantin Belousov 
3763153e878SAlan Cox 	memattr = object->memattr;
377286790a7SKonstantin Belousov 
37889f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(object);
379286790a7SKonstantin Belousov 
380286790a7SKonstantin Belousov 	dev = object->handle;
3813979450bSKonstantin Belousov 	csw = dev_refthread(dev, &ref);
38291a1929fSAttilio Rao 	if (csw == NULL) {
38389f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
38491a1929fSAttilio Rao 		return (VM_PAGER_FAIL);
38591a1929fSAttilio Rao 	}
3867818e0a5SKonstantin Belousov 	td = curthread;
3877818e0a5SKonstantin Belousov 	fpop = td->td_fpop;
3887818e0a5SKonstantin Belousov 	td->td_fpop = NULL;
389286790a7SKonstantin Belousov 	ret = csw->d_mmap(dev, offset, &paddr, prot, &memattr);
3907818e0a5SKonstantin Belousov 	td->td_fpop = fpop;
3913979450bSKonstantin Belousov 	dev_relthread(dev, ref);
392286790a7SKonstantin Belousov 	if (ret != 0) {
393286790a7SKonstantin Belousov 		printf(
394286790a7SKonstantin Belousov 	    "WARNING: dev_pager_getpage: map function returns error %d", ret);
39589f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
396286790a7SKonstantin Belousov 		return (VM_PAGER_FAIL);
397286790a7SKonstantin Belousov 	}
398286790a7SKonstantin Belousov 
3993153e878SAlan Cox 	/* If "paddr" is a real page, perform a sanity check on "memattr". */
4003153e878SAlan Cox 	if ((m_paddr = vm_phys_paddr_to_vm_page(paddr)) != NULL &&
401e9d37c9fSKonstantin Belousov 	    (memattr1 = pmap_page_get_memattr(m_paddr)) != memattr) {
402e9d37c9fSKonstantin Belousov 		/*
403e9d37c9fSKonstantin Belousov 		 * For the /dev/mem d_mmap routine to return the
404e9d37c9fSKonstantin Belousov 		 * correct memattr, pmap_page_get_memattr() needs to
405e9d37c9fSKonstantin Belousov 		 * be called, which we do there.
406e9d37c9fSKonstantin Belousov 		 */
407e9d37c9fSKonstantin Belousov 		if ((csw->d_flags & D_MEM) == 0) {
408e9d37c9fSKonstantin Belousov 			printf("WARNING: Device driver %s has set "
409e9d37c9fSKonstantin Belousov 			    "\"memattr\" inconsistently (drv %u pmap %u).\n",
410e9d37c9fSKonstantin Belousov 			    csw->d_name, memattr, memattr1);
411e9d37c9fSKonstantin Belousov 		}
412e9d37c9fSKonstantin Belousov 		memattr = memattr1;
4133153e878SAlan Cox 	}
414286790a7SKonstantin Belousov 	if (((*mres)->flags & PG_FICTITIOUS) != 0) {
41592bab635SDoug Rabson 		/*
416286790a7SKonstantin Belousov 		 * If the passed in result page is a fake page, update it with
41792bab635SDoug Rabson 		 * the new physical address.
41892bab635SDoug Rabson 		 */
419286790a7SKonstantin Belousov 		page = *mres;
42089f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
42110cf2560SAlan Cox 		vm_page_updatefake(page, paddr, memattr);
42292bab635SDoug Rabson 	} else {
423df8bae1dSRodney W. Grimes 		/*
42407159f9cSMaxime Henrion 		 * Replace the passed in reqpage page with our own fake page and
42507159f9cSMaxime Henrion 		 * free up the all of the original pages.
426df8bae1dSRodney W. Grimes 		 */
42710cf2560SAlan Cox 		page = vm_page_getfake(paddr, memattr);
42889f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
4293cf3b4e6SJeff Roberson 		vm_page_replace(page, object, (*mres)->pindex, *mres);
430286790a7SKonstantin Belousov 		*mres = page;
43192bab635SDoug Rabson 	}
4320012f373SJeff Roberson 	vm_page_valid(page);
433df8bae1dSRodney W. Grimes 	return (VM_PAGER_OK);
434df8bae1dSRodney W. Grimes }
435df8bae1dSRodney W. Grimes 
436e4542174SMatthew Dillon static void
437b89def80SKonstantin Belousov dev_pager_putpages(vm_object_t object, vm_page_t *m, int count, int flags,
438b89def80SKonstantin Belousov     int *rtvals)
439df8bae1dSRodney W. Grimes {
440286790a7SKonstantin Belousov 
441df8bae1dSRodney W. Grimes 	panic("dev_pager_putpage called");
442df8bae1dSRodney W. Grimes }
443df8bae1dSRodney W. Grimes 
444f708ef1bSPoul-Henning Kamp static boolean_t
445b89def80SKonstantin Belousov dev_pager_haspage(vm_object_t object, vm_pindex_t pindex, int *before,
446b89def80SKonstantin Belousov     int *after)
447df8bae1dSRodney W. Grimes {
448b89def80SKonstantin Belousov 
44924a1cce3SDavid Greenman 	if (before != NULL)
45024a1cce3SDavid Greenman 		*before = 0;
45124a1cce3SDavid Greenman 	if (after != NULL)
45224a1cce3SDavid Greenman 		*after = 0;
453df8bae1dSRodney W. Grimes 	return (TRUE);
454df8bae1dSRodney W. Grimes }
455286790a7SKonstantin Belousov 
456286790a7SKonstantin Belousov static int
457286790a7SKonstantin Belousov old_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
458286790a7SKonstantin Belousov     vm_ooffset_t foff, struct ucred *cred, u_short *color)
459286790a7SKonstantin Belousov {
460286790a7SKonstantin Belousov 	struct cdev *dev;
461286790a7SKonstantin Belousov 	struct cdevsw *csw;
462286790a7SKonstantin Belousov 	vm_memattr_t dummy;
463286790a7SKonstantin Belousov 	vm_ooffset_t off;
464286790a7SKonstantin Belousov 	vm_paddr_t paddr;
465286790a7SKonstantin Belousov 	unsigned int npages;
466286790a7SKonstantin Belousov 	int ref;
467286790a7SKonstantin Belousov 
468286790a7SKonstantin Belousov 	/*
469286790a7SKonstantin Belousov 	 * Make sure this device can be mapped.
470286790a7SKonstantin Belousov 	 */
471286790a7SKonstantin Belousov 	dev = handle;
472286790a7SKonstantin Belousov 	csw = dev_refthread(dev, &ref);
473286790a7SKonstantin Belousov 	if (csw == NULL)
474286790a7SKonstantin Belousov 		return (ENXIO);
475286790a7SKonstantin Belousov 
476286790a7SKonstantin Belousov 	/*
477286790a7SKonstantin Belousov 	 * Check that the specified range of the device allows the desired
478286790a7SKonstantin Belousov 	 * protection.
479286790a7SKonstantin Belousov 	 *
480286790a7SKonstantin Belousov 	 * XXX assumes VM_PROT_* == PROT_*
481286790a7SKonstantin Belousov 	 */
482286790a7SKonstantin Belousov 	npages = OFF_TO_IDX(size);
4837253a5ecSKonstantin Belousov 	paddr = 0; /* Make paddr initialized for the case of size == 0. */
484286790a7SKonstantin Belousov 	for (off = foff; npages--; off += PAGE_SIZE) {
485286790a7SKonstantin Belousov 		if (csw->d_mmap(dev, off, &paddr, (int)prot, &dummy) != 0) {
486286790a7SKonstantin Belousov 			dev_relthread(dev, ref);
487286790a7SKonstantin Belousov 			return (EINVAL);
488286790a7SKonstantin Belousov 		}
489286790a7SKonstantin Belousov 	}
490286790a7SKonstantin Belousov 
491286790a7SKonstantin Belousov 	dev_ref(dev);
492286790a7SKonstantin Belousov 	dev_relthread(dev, ref);
493286790a7SKonstantin Belousov 	*color = atop(paddr) - OFF_TO_IDX(off - PAGE_SIZE);
494286790a7SKonstantin Belousov 	return (0);
495286790a7SKonstantin Belousov }
496286790a7SKonstantin Belousov 
497286790a7SKonstantin Belousov static void
498286790a7SKonstantin Belousov old_dev_pager_dtor(void *handle)
499286790a7SKonstantin Belousov {
500286790a7SKonstantin Belousov 
501286790a7SKonstantin Belousov 	dev_rel(handle);
502286790a7SKonstantin Belousov }
503