xref: /freebsd/sys/vm/device_pager.c (revision 5ebc7e6281887681c3a348a5a4c902e262ccd656)
1 /*
2  * Copyright (c) 1990 University of Utah.
3  * Copyright (c) 1991, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * the Systems Programming Group of the University of Utah Computer
8  * Science Department.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)device_pager.c	8.1 (Berkeley) 6/11/93
39  * $Id: device_pager.c,v 1.9 1995/05/10 18:56:01 davidg Exp $
40  */
41 
42 /*
43  * Page to/from special files.
44  */
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/conf.h>
49 #include <sys/mman.h>
50 #include <sys/malloc.h>
51 #include <sys/proc.h>
52 
53 #include <vm/vm.h>
54 #include <vm/vm_kern.h>
55 #include <vm/vm_page.h>
56 #include <vm/device_pager.h>
57 
58 struct pagerlst dev_pager_list;	/* list of managed devices */
59 struct pglist dev_pager_fakelist;	/* list of available vm_page_t's */
60 
61 #ifdef DEBUG
62 int dpagerdebug;
63 
64 #define	DDB_FOLLOW	0x01
65 #define DDB_INIT	0x02
66 #define DDB_ALLOC	0x04
67 #define DDB_FAIL	0x08
68 #endif
69 
70 static vm_pager_t dev_pager_alloc __P((void *, vm_size_t, vm_prot_t, vm_offset_t));
71 static void dev_pager_dealloc __P((vm_pager_t));
72 static int dev_pager_getpage __P((vm_pager_t, vm_page_t, boolean_t));
73 static boolean_t dev_pager_haspage __P((vm_pager_t, vm_offset_t));
74 static void dev_pager_init __P((void));
75 static int dev_pager_putpage __P((vm_pager_t, vm_page_t, boolean_t));
76 static vm_page_t dev_pager_getfake __P((vm_offset_t));
77 static void dev_pager_putfake __P((vm_page_t));
78 
79 struct pagerops devicepagerops = {
80 	dev_pager_init,
81 	dev_pager_alloc,
82 	dev_pager_dealloc,
83 	dev_pager_getpage,
84 	0,
85 	dev_pager_putpage,
86 	0,
87 	dev_pager_haspage
88 };
89 
90 static void
91 dev_pager_init()
92 {
93 #ifdef DEBUG
94 	if (dpagerdebug & DDB_FOLLOW)
95 		printf("dev_pager_init()\n");
96 #endif
97 	TAILQ_INIT(&dev_pager_list);
98 	TAILQ_INIT(&dev_pager_fakelist);
99 }
100 
101 static vm_pager_t
102 dev_pager_alloc(handle, size, prot, foff)
103 	void *handle;
104 	vm_size_t size;
105 	vm_prot_t prot;
106 	vm_offset_t foff;
107 {
108 	dev_t dev;
109 	vm_pager_t pager;
110 	int (*mapfunc) ();
111 	vm_object_t object;
112 	dev_pager_t devp;
113 	unsigned int npages, off;
114 
115 #ifdef DEBUG
116 	if (dpagerdebug & DDB_FOLLOW)
117 		printf("dev_pager_alloc(%x, %x, %x, %x)\n",
118 		    handle, size, prot, foff);
119 #endif
120 #ifdef DIAGNOSTIC
121 	/*
122 	 * Pageout to device, should never happen.
123 	 */
124 	if (handle == NULL)
125 		panic("dev_pager_alloc called");
126 #endif
127 
128 	/*
129 	 * Make sure this device can be mapped.
130 	 */
131 	dev = (dev_t) (u_long) handle;
132 	mapfunc = cdevsw[major(dev)].d_mmap;
133 	if (mapfunc == NULL || mapfunc == enodev || mapfunc == nullop)
134 		return (NULL);
135 
136 	/*
137 	 * Offset should be page aligned.
138 	 */
139 	if (foff & (PAGE_SIZE - 1))
140 		return (NULL);
141 
142 	/*
143 	 * Check that the specified range of the device allows the desired
144 	 * protection.
145 	 *
146 	 * XXX assumes VM_PROT_* == PROT_*
147 	 */
148 	npages = atop(round_page(size));
149 	for (off = foff; npages--; off += PAGE_SIZE)
150 		if ((*mapfunc) (dev, off, (int) prot) == -1)
151 			return (NULL);
152 
153 	/*
154 	 * Look up pager, creating as necessary.
155 	 */
156 top:
157 	pager = vm_pager_lookup(&dev_pager_list, handle);
158 	if (pager == NULL) {
159 		/*
160 		 * Allocate and initialize pager structs
161 		 */
162 		pager = (vm_pager_t) malloc(sizeof *pager, M_VMPAGER, M_WAITOK);
163 		if (pager == NULL)
164 			return (NULL);
165 		devp = (dev_pager_t) malloc(sizeof *devp, M_VMPGDATA, M_WAITOK);
166 		if (devp == NULL) {
167 			free((caddr_t) pager, M_VMPAGER);
168 			return (NULL);
169 		}
170 		pager->pg_handle = handle;
171 		pager->pg_ops = &devicepagerops;
172 		pager->pg_type = PG_DEVICE;
173 		pager->pg_data = (caddr_t) devp;
174 		TAILQ_INIT(&devp->devp_pglist);
175 		/*
176 		 * Allocate object and associate it with the pager.
177 		 */
178 		object = devp->devp_object = vm_object_allocate(foff + size);
179 		object->flags &= ~OBJ_INTERNAL;
180 		vm_object_enter(object, pager);
181 		object->pager = pager;
182 		/*
183 		 * Finally, put it on the managed list so other can find it.
184 		 * First we re-lookup in case someone else beat us to this
185 		 * point (due to blocking in the various mallocs).  If so, we
186 		 * free everything and start over.
187 		 */
188 		if (vm_pager_lookup(&dev_pager_list, handle)) {
189 			free((caddr_t) devp, M_VMPGDATA);
190 			free((caddr_t) pager, M_VMPAGER);
191 			goto top;
192 		}
193 		TAILQ_INSERT_TAIL(&dev_pager_list, pager, pg_list);
194 #ifdef DEBUG
195 		if (dpagerdebug & DDB_ALLOC) {
196 			printf("dev_pager_alloc: pager %x devp %x object %x\n",
197 			    pager, devp, object);
198 			vm_object_print(object, FALSE);
199 		}
200 #endif
201 	} else {
202 		/*
203 		 * Gain a reference to the object.
204 		 */
205 		object = vm_object_lookup(pager);
206 		if (foff + size > object->size)
207 			object->size = foff + size;
208 #ifdef DIAGNOSTIC
209 		devp = (dev_pager_t) pager->pg_data;
210 		if (object != devp->devp_object)
211 			panic("dev_pager_setup: bad object");
212 #endif
213 	}
214 	return (pager);
215 }
216 
217 static void
218 dev_pager_dealloc(pager)
219 	vm_pager_t pager;
220 {
221 	dev_pager_t devp;
222 	vm_object_t object;
223 	vm_page_t m;
224 
225 #ifdef DEBUG
226 	if (dpagerdebug & DDB_FOLLOW)
227 		printf("dev_pager_dealloc(%x)\n", pager);
228 #endif
229 	TAILQ_REMOVE(&dev_pager_list, pager, pg_list);
230 	/*
231 	 * Get the object. Note: cannot use vm_object_lookup since object has
232 	 * already been removed from the hash chain.
233 	 */
234 	devp = (dev_pager_t) pager->pg_data;
235 	object = devp->devp_object;
236 #ifdef DEBUG
237 	if (dpagerdebug & DDB_ALLOC)
238 		printf("dev_pager_dealloc: devp %x object %x\n", devp, object);
239 #endif
240 	/*
241 	 * Free up our fake pages.
242 	 */
243 	while ((m = devp->devp_pglist.tqh_first) != 0) {
244 		TAILQ_REMOVE(&devp->devp_pglist, m, pageq);
245 		dev_pager_putfake(m);
246 	}
247 	free((caddr_t) devp, M_VMPGDATA);
248 	free((caddr_t) pager, M_VMPAGER);
249 }
250 
251 static int
252 dev_pager_getpage(pager, m, sync)
253 	vm_pager_t pager;
254 	vm_page_t m;
255 	boolean_t sync;
256 {
257 	register vm_object_t object;
258 	vm_offset_t offset, paddr;
259 	vm_page_t page;
260 	dev_t dev;
261 	int s;
262 	int (*mapfunc) (), prot;
263 
264 #ifdef DEBUG
265 	if (dpagerdebug & DDB_FOLLOW)
266 		printf("dev_pager_getpage(%x, %x)\n", pager, m);
267 #endif
268 
269 	object = m->object;
270 	dev = (dev_t) (u_long) pager->pg_handle;
271 	offset = m->offset + object->paging_offset;
272 	prot = PROT_READ;	/* XXX should pass in? */
273 	mapfunc = cdevsw[major(dev)].d_mmap;
274 
275 	if (mapfunc == NULL || mapfunc == enodev || mapfunc == nullop)
276 		panic("dev_pager_getpage: no map function");
277 
278 	paddr = pmap_phys_address((*mapfunc) ((dev_t) dev, (int) offset, prot));
279 #ifdef DIAGNOSTIC
280 	if (paddr == -1)
281 		panic("dev_pager_getpage: map function returns error");
282 #endif
283 	/*
284 	 * Replace the passed in page with our own fake page and free up the
285 	 * original.
286 	 */
287 	page = dev_pager_getfake(paddr);
288 	TAILQ_INSERT_TAIL(&((dev_pager_t) pager->pg_data)->devp_pglist,
289 	    page, pageq);
290 	vm_object_lock(object);
291 	vm_page_lock_queues();
292 	PAGE_WAKEUP(m);
293 	vm_page_free(m);
294 	vm_page_unlock_queues();
295 	s = splhigh();
296 	vm_page_insert(page, object, offset);
297 	splx(s);
298 	vm_object_unlock(object);
299 
300 	return (VM_PAGER_OK);
301 }
302 
303 static int
304 dev_pager_putpage(pager, m, sync)
305 	vm_pager_t pager;
306 	vm_page_t m;
307 	boolean_t sync;
308 {
309 #ifdef DEBUG
310 	if (dpagerdebug & DDB_FOLLOW)
311 		printf("dev_pager_putpage(%x, %x)\n", pager, m);
312 #endif
313 	if (pager == NULL)
314 		return 0;
315 	panic("dev_pager_putpage called");
316 }
317 
318 static boolean_t
319 dev_pager_haspage(pager, offset)
320 	vm_pager_t pager;
321 	vm_offset_t offset;
322 {
323 #ifdef DEBUG
324 	if (dpagerdebug & DDB_FOLLOW)
325 		printf("dev_pager_haspage(%x, %x)\n", pager, offset);
326 #endif
327 	return (TRUE);
328 }
329 
330 static vm_page_t
331 dev_pager_getfake(paddr)
332 	vm_offset_t paddr;
333 {
334 	vm_page_t m;
335 	int i;
336 
337 	if (dev_pager_fakelist.tqh_first == NULL) {
338 		m = (vm_page_t) malloc(PAGE_SIZE * 2, M_VMPGDATA, M_WAITOK);
339 		for (i = (PAGE_SIZE * 2) / sizeof(*m); i > 0; i--) {
340 			TAILQ_INSERT_TAIL(&dev_pager_fakelist, m, pageq);
341 			m++;
342 		}
343 	}
344 	m = dev_pager_fakelist.tqh_first;
345 	TAILQ_REMOVE(&dev_pager_fakelist, m, pageq);
346 
347 	m->flags = PG_BUSY | PG_FICTITIOUS;
348 	m->dirty = 0;
349 	m->valid = VM_PAGE_BITS_ALL;
350 	m->busy = 0;
351 	m->bmapped = 0;
352 
353 	m->wire_count = 1;
354 	m->phys_addr = paddr;
355 
356 	return (m);
357 }
358 
359 static void
360 dev_pager_putfake(m)
361 	vm_page_t m;
362 {
363 #ifdef DIAGNOSTIC
364 	if (!(m->flags & PG_FICTITIOUS))
365 		panic("dev_pager_putfake: bad page");
366 #endif
367 	TAILQ_INSERT_TAIL(&dev_pager_fakelist, m, pageq);
368 }
369