uvm_device.c revision 1.43 1 /* $NetBSD: uvm_device.c,v 1.43 2005/06/06 12:09:19 yamt Exp $ */
2
3 /*
4 *
5 * Copyright (c) 1997 Charles D. Cranor and Washington University.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Charles D. Cranor and
19 * Washington University.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 * from: Id: uvm_device.c,v 1.1.2.9 1998/02/06 05:11:47 chs Exp
35 */
36
37 /*
38 * uvm_device.c: the device pager.
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: uvm_device.c,v 1.43 2005/06/06 12:09:19 yamt Exp $");
43
44 #include "opt_uvmhist.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/conf.h>
49 #include <sys/proc.h>
50 #include <sys/malloc.h>
51 #include <sys/vnode.h>
52
53 #include <uvm/uvm.h>
54 #include <uvm/uvm_device.h>
55
56 /*
57 * private global data structure
58 *
59 * we keep a list of active device objects in the system.
60 */
61
62 LIST_HEAD(udv_list_struct, uvm_device);
63 static struct udv_list_struct udv_list;
64 static struct simplelock udv_lock;
65
66 /*
67 * functions
68 */
69
70 static void udv_init(void);
71 static void udv_reference(struct uvm_object *);
72 static void udv_detach(struct uvm_object *);
73 static int udv_fault(struct uvm_faultinfo *, vaddr_t,
74 struct vm_page **, int, int, vm_fault_t, vm_prot_t, int);
75
76 /*
77 * master pager structure
78 */
79
80 struct uvm_pagerops uvm_deviceops = {
81 udv_init,
82 udv_reference,
83 udv_detach,
84 udv_fault,
85 };
86
87 /*
88 * the ops!
89 */
90
91 /*
92 * udv_init
93 *
94 * init pager private data structures.
95 */
96
97 static void
98 udv_init(void)
99 {
100 LIST_INIT(&udv_list);
101 simple_lock_init(&udv_lock);
102 }
103
104 /*
105 * udv_attach
106 *
107 * get a VM object that is associated with a device. allocate a new
108 * one if needed.
109 *
110 * => caller must _not_ already be holding the lock on the uvm_object.
111 * => in fact, nothing should be locked so that we can sleep here.
112 */
113
114 struct uvm_object *
115 udv_attach(arg, accessprot, off, size)
116 void *arg;
117 vm_prot_t accessprot;
118 voff_t off; /* used only for access check */
119 vsize_t size; /* used only for access check */
120 {
121 dev_t device = *((dev_t *)arg);
122 struct uvm_device *udv, *lcv;
123 const struct cdevsw *cdev;
124 dev_type_mmap((*mapfn));
125
126 UVMHIST_FUNC("udv_attach"); UVMHIST_CALLED(maphist);
127
128 UVMHIST_LOG(maphist, "(device=0x%x)", device,0,0,0);
129
130 /*
131 * before we do anything, ensure this device supports mmap
132 */
133
134 cdev = cdevsw_lookup(device);
135 if (cdev == NULL)
136 return (NULL);
137 mapfn = cdev->d_mmap;
138 if (mapfn == NULL || mapfn == nommap || mapfn == nullmmap)
139 return(NULL);
140
141 /*
142 * Negative offsets on the object are not allowed.
143 */
144
145 if (off < 0)
146 return(NULL);
147
148 /*
149 * Check that the specified range of the device allows the
150 * desired protection.
151 *
152 * XXX assumes VM_PROT_* == PROT_*
153 * XXX clobbers off and size, but nothing else here needs them.
154 */
155
156 while (size != 0) {
157 if ((*mapfn)(device, off, accessprot) == -1)
158 return (NULL);
159 off += PAGE_SIZE; size -= PAGE_SIZE;
160 }
161
162 /*
163 * keep looping until we get it
164 */
165
166 for (;;) {
167
168 /*
169 * first, attempt to find it on the main list
170 */
171
172 simple_lock(&udv_lock);
173 LIST_FOREACH(lcv, &udv_list, u_list) {
174 if (device == lcv->u_device)
175 break;
176 }
177
178 /*
179 * got it on main list. put a hold on it and unlock udv_lock.
180 */
181
182 if (lcv) {
183
184 /*
185 * if someone else has a hold on it, sleep and start
186 * over again.
187 */
188
189 if (lcv->u_flags & UVM_DEVICE_HOLD) {
190 lcv->u_flags |= UVM_DEVICE_WANTED;
191 UVM_UNLOCK_AND_WAIT(lcv, &udv_lock, FALSE,
192 "udv_attach",0);
193 continue;
194 }
195
196 /* we are now holding it */
197 lcv->u_flags |= UVM_DEVICE_HOLD;
198 simple_unlock(&udv_lock);
199
200 /*
201 * bump reference count, unhold, return.
202 */
203
204 simple_lock(&lcv->u_obj.vmobjlock);
205 lcv->u_obj.uo_refs++;
206 simple_unlock(&lcv->u_obj.vmobjlock);
207
208 simple_lock(&udv_lock);
209 if (lcv->u_flags & UVM_DEVICE_WANTED)
210 wakeup(lcv);
211 lcv->u_flags &= ~(UVM_DEVICE_WANTED|UVM_DEVICE_HOLD);
212 simple_unlock(&udv_lock);
213 return(&lcv->u_obj);
214 }
215
216 /*
217 * did not find it on main list. need to malloc a new one.
218 */
219
220 simple_unlock(&udv_lock);
221 /* NOTE: we could sleep in the following malloc() */
222 MALLOC(udv, struct uvm_device *, sizeof(*udv), M_TEMP,
223 M_WAITOK);
224 simple_lock(&udv_lock);
225
226 /*
227 * now we have to double check to make sure no one added it
228 * to the list while we were sleeping...
229 */
230
231 LIST_FOREACH(lcv, &udv_list, u_list) {
232 if (device == lcv->u_device)
233 break;
234 }
235
236 /*
237 * did we lose a race to someone else?
238 * free our memory and retry.
239 */
240
241 if (lcv) {
242 simple_unlock(&udv_lock);
243 FREE(udv, M_TEMP);
244 continue;
245 }
246
247 /*
248 * we have it! init the data structures, add to list
249 * and return.
250 */
251
252 UVM_OBJ_INIT(&udv->u_obj, &uvm_deviceops, 1);
253 udv->u_flags = 0;
254 udv->u_device = device;
255 LIST_INSERT_HEAD(&udv_list, udv, u_list);
256 simple_unlock(&udv_lock);
257 return(&udv->u_obj);
258 }
259 /*NOTREACHED*/
260 }
261
262 /*
263 * udv_reference
264 *
265 * add a reference to a VM object. Note that the reference count must
266 * already be one (the passed in reference) so there is no chance of the
267 * udv being released or locked out here.
268 *
269 * => caller must call with object unlocked.
270 */
271
272 static void
273 udv_reference(uobj)
274 struct uvm_object *uobj;
275 {
276 UVMHIST_FUNC("udv_reference"); UVMHIST_CALLED(maphist);
277
278 simple_lock(&uobj->vmobjlock);
279 uobj->uo_refs++;
280 UVMHIST_LOG(maphist, "<- done (uobj=0x%x, ref = %d)",
281 uobj, uobj->uo_refs,0,0);
282 simple_unlock(&uobj->vmobjlock);
283 }
284
285 /*
286 * udv_detach
287 *
288 * remove a reference to a VM object.
289 *
290 * => caller must call with object unlocked and map locked.
291 */
292
293 static void
294 udv_detach(uobj)
295 struct uvm_object *uobj;
296 {
297 struct uvm_device *udv = (struct uvm_device *)uobj;
298 UVMHIST_FUNC("udv_detach"); UVMHIST_CALLED(maphist);
299
300 /*
301 * loop until done
302 */
303 again:
304 simple_lock(&uobj->vmobjlock);
305 if (uobj->uo_refs > 1) {
306 uobj->uo_refs--;
307 simple_unlock(&uobj->vmobjlock);
308 UVMHIST_LOG(maphist," <- done, uobj=0x%x, ref=%d",
309 uobj,uobj->uo_refs,0,0);
310 return;
311 }
312
313 /*
314 * is it being held? if so, wait until others are done.
315 */
316
317 simple_lock(&udv_lock);
318 if (udv->u_flags & UVM_DEVICE_HOLD) {
319 udv->u_flags |= UVM_DEVICE_WANTED;
320 simple_unlock(&uobj->vmobjlock);
321 UVM_UNLOCK_AND_WAIT(udv, &udv_lock, FALSE, "udv_detach",0);
322 goto again;
323 }
324
325 /*
326 * got it! nuke it now.
327 */
328
329 LIST_REMOVE(udv, u_list);
330 if (udv->u_flags & UVM_DEVICE_WANTED)
331 wakeup(udv);
332 simple_unlock(&udv_lock);
333 simple_unlock(&uobj->vmobjlock);
334 FREE(udv, M_TEMP);
335 UVMHIST_LOG(maphist," <- done, freed uobj=0x%x", uobj,0,0,0);
336 }
337
338 /*
339 * udv_fault: non-standard fault routine for device "pages"
340 *
341 * => rather than having a "get" function, we have a fault routine
342 * since we don't return vm_pages we need full control over the
343 * pmap_enter map in
344 * => all the usual fault data structured are locked by the caller
345 * (i.e. maps(read), amap (if any), uobj)
346 * => on return, we unlock all fault data structures
347 * => flags: PGO_ALLPAGES: get all of the pages
348 * PGO_LOCKED: fault data structures are locked
349 * XXX: currently PGO_LOCKED is always required ... consider removing
350 * it as a flag
351 * => NOTE: vaddr is the VA of pps[0] in ufi->entry, _NOT_ pps[centeridx]
352 */
353
354 static int
355 udv_fault(ufi, vaddr, pps, npages, centeridx, fault_type, access_type, flags)
356 struct uvm_faultinfo *ufi;
357 vaddr_t vaddr;
358 struct vm_page **pps;
359 int npages, centeridx, flags;
360 vm_fault_t fault_type;
361 vm_prot_t access_type;
362 {
363 struct vm_map_entry *entry = ufi->entry;
364 struct uvm_object *uobj = entry->object.uvm_obj;
365 struct uvm_device *udv = (struct uvm_device *)uobj;
366 const struct cdevsw *cdev;
367 vaddr_t curr_va;
368 off_t curr_offset;
369 paddr_t paddr, mdpgno;
370 int lcv, retval;
371 dev_t device;
372 paddr_t (*mapfn)(dev_t, off_t, int);
373 vm_prot_t mapprot;
374 UVMHIST_FUNC("udv_fault"); UVMHIST_CALLED(maphist);
375 UVMHIST_LOG(maphist," flags=%d", flags,0,0,0);
376
377 /*
378 * we do not allow device mappings to be mapped copy-on-write
379 * so we kill any attempt to do so here.
380 */
381
382 if (UVM_ET_ISCOPYONWRITE(entry)) {
383 UVMHIST_LOG(maphist, "<- failed -- COW entry (etype=0x%x)",
384 entry->etype, 0,0,0);
385 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj, NULL);
386 return(EIO);
387 }
388
389 /*
390 * get device map function.
391 */
392
393 device = udv->u_device;
394 cdev = cdevsw_lookup(device);
395 if (cdev == NULL) {
396 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj, NULL);
397 return (EIO);
398 }
399 mapfn = cdev->d_mmap;
400
401 /*
402 * now we must determine the offset in udv to use and the VA to
403 * use for pmap_enter. note that we always use orig_map's pmap
404 * for pmap_enter (even if we have a submap). since virtual
405 * addresses in a submap must match the main map, this is ok.
406 */
407
408 /* udv offset = (offset from start of entry) + entry's offset */
409 curr_offset = entry->offset + (vaddr - entry->start);
410 /* pmap va = vaddr (virtual address of pps[0]) */
411 curr_va = vaddr;
412
413 /*
414 * loop over the page range entering in as needed
415 */
416
417 retval = 0;
418 for (lcv = 0 ; lcv < npages ; lcv++, curr_offset += PAGE_SIZE,
419 curr_va += PAGE_SIZE) {
420 if ((flags & PGO_ALLPAGES) == 0 && lcv != centeridx)
421 continue;
422
423 if (pps[lcv] == PGO_DONTCARE)
424 continue;
425
426 mdpgno = (*mapfn)(device, curr_offset, access_type);
427 if (mdpgno == -1) {
428 retval = EIO;
429 break;
430 }
431 paddr = pmap_phys_address(mdpgno);
432 mapprot = ufi->entry->protection;
433 UVMHIST_LOG(maphist,
434 " MAPPING: device: pm=0x%x, va=0x%x, pa=0x%lx, at=%d",
435 ufi->orig_map->pmap, curr_va, paddr, mapprot);
436 if (pmap_enter(ufi->orig_map->pmap, curr_va, paddr,
437 mapprot, PMAP_CANFAIL | mapprot) != 0) {
438 /*
439 * pmap_enter() didn't have the resource to
440 * enter this mapping. Unlock everything,
441 * wait for the pagedaemon to free up some
442 * pages, and then tell uvm_fault() to start
443 * the fault again.
444 *
445 * XXX Needs some rethinking for the PGO_ALLPAGES
446 * XXX case.
447 */
448 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap,
449 uobj, NULL);
450 pmap_update(ufi->orig_map->pmap); /* sync what we have so far */
451 uvm_wait("udv_fault");
452 return (ERESTART);
453 }
454 }
455
456 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj, NULL);
457 pmap_update(ufi->orig_map->pmap);
458 return (retval);
459 }
460