Deleted Added
full compact
device_pager.c (79224) device_pager.c (92654)
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
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 * $FreeBSD: head/sys/vm/device_pager.c 79224 2001-07-04 16:20:28Z dillon $
39 * $FreeBSD: head/sys/vm/device_pager.c 92654 2002-03-19 09:11:49Z jeff $
40 */
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/conf.h>
45#include <sys/lock.h>
46#include <sys/proc.h>
47#include <sys/mutex.h>
48#include <sys/mman.h>
49#include <sys/sx.h>
50
51#include <vm/vm.h>
52#include <vm/vm_object.h>
53#include <vm/vm_page.h>
54#include <vm/vm_pager.h>
55#include <vm/vm_zone.h>
56
57static void dev_pager_init __P((void));
58static vm_object_t dev_pager_alloc __P((void *, vm_ooffset_t, vm_prot_t,
59 vm_ooffset_t));
60static void dev_pager_dealloc __P((vm_object_t));
61static int dev_pager_getpages __P((vm_object_t, vm_page_t *, int, int));
62static void dev_pager_putpages __P((vm_object_t, vm_page_t *, int,
63 boolean_t, int *));
64static boolean_t dev_pager_haspage __P((vm_object_t, vm_pindex_t, int *,
65 int *));
66
67/* list of device pager objects */
68static struct pagerlst dev_pager_object_list;
69/* protect against object creation */
70static struct sx dev_pager_sx;
71/* protect list manipulation */
72static struct mtx dev_pager_mtx;
73
74
75static vm_zone_t fakepg_zone;
40 */
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/conf.h>
45#include <sys/lock.h>
46#include <sys/proc.h>
47#include <sys/mutex.h>
48#include <sys/mman.h>
49#include <sys/sx.h>
50
51#include <vm/vm.h>
52#include <vm/vm_object.h>
53#include <vm/vm_page.h>
54#include <vm/vm_pager.h>
55#include <vm/vm_zone.h>
56
57static void dev_pager_init __P((void));
58static vm_object_t dev_pager_alloc __P((void *, vm_ooffset_t, vm_prot_t,
59 vm_ooffset_t));
60static void dev_pager_dealloc __P((vm_object_t));
61static int dev_pager_getpages __P((vm_object_t, vm_page_t *, int, int));
62static void dev_pager_putpages __P((vm_object_t, vm_page_t *, int,
63 boolean_t, int *));
64static boolean_t dev_pager_haspage __P((vm_object_t, vm_pindex_t, int *,
65 int *));
66
67/* list of device pager objects */
68static struct pagerlst dev_pager_object_list;
69/* protect against object creation */
70static struct sx dev_pager_sx;
71/* protect list manipulation */
72static struct mtx dev_pager_mtx;
73
74
75static vm_zone_t fakepg_zone;
76#if 0
76static struct vm_zone fakepg_zone_store;
77static struct vm_zone fakepg_zone_store;
78#endif
77
78static vm_page_t dev_pager_getfake __P((vm_offset_t));
79static void dev_pager_putfake __P((vm_page_t));
80
81struct pagerops devicepagerops = {
82 dev_pager_init,
83 dev_pager_alloc,
84 dev_pager_dealloc,
85 dev_pager_getpages,
86 dev_pager_putpages,
87 dev_pager_haspage,
88 NULL
89};
90
91static void
92dev_pager_init()
93{
94 TAILQ_INIT(&dev_pager_object_list);
95 sx_init(&dev_pager_sx, "dev_pager create");
96 mtx_init(&dev_pager_mtx, "dev_pager list", MTX_DEF);
79
80static vm_page_t dev_pager_getfake __P((vm_offset_t));
81static void dev_pager_putfake __P((vm_page_t));
82
83struct pagerops devicepagerops = {
84 dev_pager_init,
85 dev_pager_alloc,
86 dev_pager_dealloc,
87 dev_pager_getpages,
88 dev_pager_putpages,
89 dev_pager_haspage,
90 NULL
91};
92
93static void
94dev_pager_init()
95{
96 TAILQ_INIT(&dev_pager_object_list);
97 sx_init(&dev_pager_sx, "dev_pager create");
98 mtx_init(&dev_pager_mtx, "dev_pager list", MTX_DEF);
99#if 0
97 fakepg_zone = &fakepg_zone_store;
98 zinitna(fakepg_zone, NULL, "DP fakepg", sizeof(struct vm_page), 0, 0, 2);
100 fakepg_zone = &fakepg_zone_store;
101 zinitna(fakepg_zone, NULL, "DP fakepg", sizeof(struct vm_page), 0, 0, 2);
102#endif
103 fakepg_zone = zinit("DP fakepg", sizeof(struct vm_page), 0, 0, 0);
99}
100
101static vm_object_t
102dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff)
103{
104 dev_t dev;
105 d_mmap_t *mapfunc;
106 vm_object_t object;
107 unsigned int npages;
108 vm_offset_t off;
109
110 mtx_assert(&Giant, MA_OWNED);
111 /*
112 * Make sure this device can be mapped.
113 */
114 dev = handle;
115 mapfunc = devsw(dev)->d_mmap;
116 if (mapfunc == NULL || mapfunc == (d_mmap_t *)nullop) {
117 printf("obsolete map function %p\n", (void *)mapfunc);
118 return (NULL);
119 }
120
121 /*
122 * Offset should be page aligned.
123 */
124 if (foff & PAGE_MASK)
125 return (NULL);
126
127 size = round_page(size);
128
129 /*
130 * Check that the specified range of the device allows the desired
131 * protection.
132 *
133 * XXX assumes VM_PROT_* == PROT_*
134 */
135 npages = OFF_TO_IDX(size);
136 for (off = foff; npages--; off += PAGE_SIZE)
137 if ((*mapfunc) (dev, off, (int) prot) == -1)
138 return (NULL);
139
140 /*
141 * Lock to prevent object creation race condition.
142 */
143 sx_xlock(&dev_pager_sx);
144
145 /*
146 * Look up pager, creating as necessary.
147 */
148 object = vm_pager_object_lookup(&dev_pager_object_list, handle);
149 if (object == NULL) {
150 /*
151 * Allocate object and associate it with the pager.
152 */
153 object = vm_object_allocate(OBJT_DEVICE,
154 OFF_TO_IDX(foff + size));
155 object->handle = handle;
156 TAILQ_INIT(&object->un_pager.devp.devp_pglist);
157 mtx_lock(&dev_pager_mtx);
158 TAILQ_INSERT_TAIL(&dev_pager_object_list, object, pager_object_list);
159 mtx_unlock(&dev_pager_mtx);
160 } else {
161 /*
162 * Gain a reference to the object.
163 */
164 vm_object_reference(object);
165 if (OFF_TO_IDX(foff + size) > object->size)
166 object->size = OFF_TO_IDX(foff + size);
167 }
168
169 sx_xunlock(&dev_pager_sx);
170
171 return (object);
172}
173
174static void
175dev_pager_dealloc(object)
176 vm_object_t object;
177{
178 vm_page_t m;
179
180 mtx_lock(&dev_pager_mtx);
181 TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list);
182 mtx_unlock(&dev_pager_mtx);
183 /*
184 * Free up our fake pages.
185 */
186 while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist)) != 0) {
187 TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, pageq);
188 dev_pager_putfake(m);
189 }
190}
191
192static int
193dev_pager_getpages(object, m, count, reqpage)
194 vm_object_t object;
195 vm_page_t *m;
196 int count;
197 int reqpage;
198{
199 vm_offset_t offset;
200 vm_offset_t paddr;
201 vm_page_t page;
202 dev_t dev;
203 int i;
204 d_mmap_t *mapfunc;
205 int prot;
206
207 mtx_assert(&Giant, MA_OWNED);
208 dev = object->handle;
209 offset = m[reqpage]->pindex;
210 prot = PROT_READ; /* XXX should pass in? */
211 mapfunc = devsw(dev)->d_mmap;
212
213 if (mapfunc == NULL || mapfunc == (d_mmap_t *)nullop)
214 panic("dev_pager_getpage: no map function");
215
216 paddr = pmap_phys_address((*mapfunc) (dev, (vm_offset_t) offset << PAGE_SHIFT, prot));
217 KASSERT(paddr != -1,("dev_pager_getpage: map function returns error"));
218 /*
219 * Replace the passed in reqpage page with our own fake page and free up the
220 * all of the original pages.
221 */
222 page = dev_pager_getfake(paddr);
223 TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist, page, pageq);
224 for (i = 0; i < count; i++) {
225 vm_page_free(m[i]);
226 }
227 vm_page_insert(page, object, offset);
228
229 return (VM_PAGER_OK);
230}
231
232static void
233dev_pager_putpages(object, m, count, sync, rtvals)
234 vm_object_t object;
235 vm_page_t *m;
236 int count;
237 boolean_t sync;
238 int *rtvals;
239{
240 panic("dev_pager_putpage called");
241}
242
243static boolean_t
244dev_pager_haspage(object, pindex, before, after)
245 vm_object_t object;
246 vm_pindex_t pindex;
247 int *before;
248 int *after;
249{
250 if (before != NULL)
251 *before = 0;
252 if (after != NULL)
253 *after = 0;
254 return (TRUE);
255}
256
257static vm_page_t
258dev_pager_getfake(paddr)
259 vm_offset_t paddr;
260{
261 vm_page_t m;
262
263 m = zalloc(fakepg_zone);
264
265 m->flags = PG_BUSY | PG_FICTITIOUS;
266 m->valid = VM_PAGE_BITS_ALL;
267 m->dirty = 0;
268 m->busy = 0;
269 m->queue = PQ_NONE;
270 m->object = NULL;
271
272 m->wire_count = 1;
273 m->hold_count = 0;
274 m->phys_addr = paddr;
275
276 return (m);
277}
278
279static void
280dev_pager_putfake(m)
281 vm_page_t m;
282{
283 if (!(m->flags & PG_FICTITIOUS))
284 panic("dev_pager_putfake: bad page");
285 zfree(fakepg_zone, m);
286}
104}
105
106static vm_object_t
107dev_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot, vm_ooffset_t foff)
108{
109 dev_t dev;
110 d_mmap_t *mapfunc;
111 vm_object_t object;
112 unsigned int npages;
113 vm_offset_t off;
114
115 mtx_assert(&Giant, MA_OWNED);
116 /*
117 * Make sure this device can be mapped.
118 */
119 dev = handle;
120 mapfunc = devsw(dev)->d_mmap;
121 if (mapfunc == NULL || mapfunc == (d_mmap_t *)nullop) {
122 printf("obsolete map function %p\n", (void *)mapfunc);
123 return (NULL);
124 }
125
126 /*
127 * Offset should be page aligned.
128 */
129 if (foff & PAGE_MASK)
130 return (NULL);
131
132 size = round_page(size);
133
134 /*
135 * Check that the specified range of the device allows the desired
136 * protection.
137 *
138 * XXX assumes VM_PROT_* == PROT_*
139 */
140 npages = OFF_TO_IDX(size);
141 for (off = foff; npages--; off += PAGE_SIZE)
142 if ((*mapfunc) (dev, off, (int) prot) == -1)
143 return (NULL);
144
145 /*
146 * Lock to prevent object creation race condition.
147 */
148 sx_xlock(&dev_pager_sx);
149
150 /*
151 * Look up pager, creating as necessary.
152 */
153 object = vm_pager_object_lookup(&dev_pager_object_list, handle);
154 if (object == NULL) {
155 /*
156 * Allocate object and associate it with the pager.
157 */
158 object = vm_object_allocate(OBJT_DEVICE,
159 OFF_TO_IDX(foff + size));
160 object->handle = handle;
161 TAILQ_INIT(&object->un_pager.devp.devp_pglist);
162 mtx_lock(&dev_pager_mtx);
163 TAILQ_INSERT_TAIL(&dev_pager_object_list, object, pager_object_list);
164 mtx_unlock(&dev_pager_mtx);
165 } else {
166 /*
167 * Gain a reference to the object.
168 */
169 vm_object_reference(object);
170 if (OFF_TO_IDX(foff + size) > object->size)
171 object->size = OFF_TO_IDX(foff + size);
172 }
173
174 sx_xunlock(&dev_pager_sx);
175
176 return (object);
177}
178
179static void
180dev_pager_dealloc(object)
181 vm_object_t object;
182{
183 vm_page_t m;
184
185 mtx_lock(&dev_pager_mtx);
186 TAILQ_REMOVE(&dev_pager_object_list, object, pager_object_list);
187 mtx_unlock(&dev_pager_mtx);
188 /*
189 * Free up our fake pages.
190 */
191 while ((m = TAILQ_FIRST(&object->un_pager.devp.devp_pglist)) != 0) {
192 TAILQ_REMOVE(&object->un_pager.devp.devp_pglist, m, pageq);
193 dev_pager_putfake(m);
194 }
195}
196
197static int
198dev_pager_getpages(object, m, count, reqpage)
199 vm_object_t object;
200 vm_page_t *m;
201 int count;
202 int reqpage;
203{
204 vm_offset_t offset;
205 vm_offset_t paddr;
206 vm_page_t page;
207 dev_t dev;
208 int i;
209 d_mmap_t *mapfunc;
210 int prot;
211
212 mtx_assert(&Giant, MA_OWNED);
213 dev = object->handle;
214 offset = m[reqpage]->pindex;
215 prot = PROT_READ; /* XXX should pass in? */
216 mapfunc = devsw(dev)->d_mmap;
217
218 if (mapfunc == NULL || mapfunc == (d_mmap_t *)nullop)
219 panic("dev_pager_getpage: no map function");
220
221 paddr = pmap_phys_address((*mapfunc) (dev, (vm_offset_t) offset << PAGE_SHIFT, prot));
222 KASSERT(paddr != -1,("dev_pager_getpage: map function returns error"));
223 /*
224 * Replace the passed in reqpage page with our own fake page and free up the
225 * all of the original pages.
226 */
227 page = dev_pager_getfake(paddr);
228 TAILQ_INSERT_TAIL(&object->un_pager.devp.devp_pglist, page, pageq);
229 for (i = 0; i < count; i++) {
230 vm_page_free(m[i]);
231 }
232 vm_page_insert(page, object, offset);
233
234 return (VM_PAGER_OK);
235}
236
237static void
238dev_pager_putpages(object, m, count, sync, rtvals)
239 vm_object_t object;
240 vm_page_t *m;
241 int count;
242 boolean_t sync;
243 int *rtvals;
244{
245 panic("dev_pager_putpage called");
246}
247
248static boolean_t
249dev_pager_haspage(object, pindex, before, after)
250 vm_object_t object;
251 vm_pindex_t pindex;
252 int *before;
253 int *after;
254{
255 if (before != NULL)
256 *before = 0;
257 if (after != NULL)
258 *after = 0;
259 return (TRUE);
260}
261
262static vm_page_t
263dev_pager_getfake(paddr)
264 vm_offset_t paddr;
265{
266 vm_page_t m;
267
268 m = zalloc(fakepg_zone);
269
270 m->flags = PG_BUSY | PG_FICTITIOUS;
271 m->valid = VM_PAGE_BITS_ALL;
272 m->dirty = 0;
273 m->busy = 0;
274 m->queue = PQ_NONE;
275 m->object = NULL;
276
277 m->wire_count = 1;
278 m->hold_count = 0;
279 m->phys_addr = paddr;
280
281 return (m);
282}
283
284static void
285dev_pager_putfake(m)
286 vm_page_t m;
287{
288 if (!(m->flags & PG_FICTITIOUS))
289 panic("dev_pager_putfake: bad page");
290 zfree(fakepg_zone, m);
291}