uvm_device.c revision 1.7 1 /* $NetBSD: uvm_device.c,v 1.7 1998/03/09 00:58:56 mrg Exp $ */
2
3 /*
4 * XXXCDC: "ROUGH DRAFT" QUALITY UVM PRE-RELEASE FILE!
5 * >>>USE AT YOUR OWN RISK, WORK IS NOT FINISHED<<<
6 */
7
8 /*
9 *
10 * Copyright (c) 1997 Charles D. Cranor and Washington University.
11 * All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by Charles D. Cranor and
24 * Washington University.
25 * 4. The name of the author may not be used to endorse or promote products
26 * derived from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *
39 * from: Id: uvm_device.c,v 1.1.2.9 1998/02/06 05:11:47 chs Exp
40 */
41
42 #include "opt_uvmhist.h"
43
44 /*
45 * uvm_device.c: the device pager.
46 */
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/conf.h>
51 #include <sys/mount.h>
52 #include <sys/proc.h>
53 #include <sys/malloc.h>
54 #include <sys/vnode.h>
55
56 #include <vm/vm.h>
57 #include <vm/vm_page.h>
58 #include <vm/vm_kern.h>
59
60 #include <sys/syscallargs.h>
61
62 #include <uvm/uvm.h>
63 #include <uvm/uvm_device.h>
64
65 /*
66 * private global data structure
67 *
68 * we keep a list of active device objects in the system.
69 */
70
71 LIST_HEAD(udv_list_struct, uvm_device);
72 static struct udv_list_struct udv_list;
73 static simple_lock_data_t udv_lock;
74
75 /*
76 * functions
77 */
78
79 static void udv_init __P((void));
80 struct uvm_object *udv_attach __P((void *, vm_prot_t));
81 static void udv_reference __P((struct uvm_object *));
82 static void udv_detach __P((struct uvm_object *));
83 static int udv_fault __P((struct uvm_faultinfo *, vm_offset_t,
84 vm_page_t *, int, int, vm_fault_t,
85 vm_prot_t, int));
86 static boolean_t udv_flush __P((struct uvm_object *, vm_offset_t,
87 vm_offset_t, int));
88 static int udv_asyncget __P((struct uvm_object *, vm_offset_t,
89 int));
90 static int udv_put __P((struct uvm_object *, vm_page_t *,
91 int, boolean_t));
92
93 /*
94 * master pager structure
95 */
96
97 struct uvm_pagerops uvm_deviceops = {
98 udv_init,
99 udv_attach,
100 udv_reference,
101 udv_detach,
102 udv_fault,
103 udv_flush,
104 NULL, /* no get function since we have udv_fault */
105 udv_asyncget,
106 udv_put,
107 NULL, /* no cluster function */
108 NULL, /* no put cluster function */
109 NULL, /* no share protect. no share maps for us */
110 NULL, /* no AIO-DONE function since no async i/o */
111 NULL, /* no releasepg function since no normal pages */
112 };
113
114 /*
115 * the ops!
116 */
117
118 /*
119 * udv_init
120 *
121 * init pager private data structures.
122 */
123
124 void
125 udv_init()
126 {
127
128 LIST_INIT(&udv_list);
129 simple_lock_init(&udv_lock);
130 }
131
132 /*
133 * udv_attach
134 *
135 * get a VM object that is associated with a device. allocate a new
136 * one if needed.
137 *
138 * => caller must _not_ already be holding the lock on the uvm_object.
139 * => in fact, nothing should be locked so that we can sleep here.
140 */
141 struct uvm_object *
142 udv_attach(arg, accessprot)
143 void *arg;
144 vm_prot_t accessprot;
145 {
146 dev_t device = *((dev_t *) arg);
147 struct uvm_device *udv, *lcv;
148 int (*mapfn) __P((dev_t, int, int));
149 UVMHIST_FUNC("udv_attach"); UVMHIST_CALLED(maphist);
150
151 UVMHIST_LOG(maphist, "(device=0x%x)", device,0,0,0);
152
153 /*
154 * before we do anything, ensure this device supports mmap
155 */
156
157 mapfn = cdevsw[major(device)].d_mmap;
158 if (mapfn == NULL ||
159 mapfn == (int (*) __P((dev_t, int, int))) enodev ||
160 mapfn == (int (*) __P((dev_t, int, int))) nullop)
161 return(NULL);
162
163 /*
164 * keep looping until we get it
165 */
166
167 while (1) {
168
169 /*
170 * first, attempt to find it on the main list
171 */
172
173 simple_lock(&udv_lock);
174 for (lcv = udv_list.lh_first ; lcv != NULL ; lcv = lcv->u_list.le_next) {
175 if (device == lcv->u_device)
176 break;
177 }
178
179 /*
180 * got it on main list. put a hold on it and unlock udv_lock.
181 */
182
183 if (lcv) {
184
185 /*
186 * if someone else has a hold on it, sleep and start
187 * over again.
188 */
189
190 if (lcv->u_flags & UVM_DEVICE_HOLD) {
191 lcv->u_flags |= UVM_DEVICE_WANTED;
192 UVM_UNLOCK_AND_WAIT(lcv, &udv_lock, FALSE,
193 "udv_attach",0);
194 continue;
195 }
196
197 /* we are now holding it */
198 lcv->u_flags |= UVM_DEVICE_HOLD;
199 simple_unlock(&udv_lock);
200
201 /*
202 * bump reference count, unhold, return.
203 */
204
205 simple_lock(&lcv->u_obj.vmobjlock);
206 lcv->u_obj.uo_refs++;
207 simple_unlock(&lcv->u_obj.vmobjlock);
208
209 simple_lock(&udv_lock);
210 if (lcv->u_flags & UVM_DEVICE_WANTED)
211 wakeup(lcv);
212 lcv->u_flags &= ~(UVM_DEVICE_WANTED|UVM_DEVICE_HOLD);
213 simple_unlock(&udv_lock);
214 return(&lcv->u_obj);
215 }
216
217 /*
218 * did not find it on main list. need to malloc a new one.
219 */
220
221 simple_unlock(&udv_lock);
222 /* NOTE: we could sleep in the following malloc() */
223 MALLOC(udv, struct uvm_device *, sizeof(*udv), M_TEMP, 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 for (lcv = udv_list.lh_first ; lcv != NULL ;
232 lcv = lcv->u_list.le_next) {
233 if (device == lcv->u_device)
234 break;
235 }
236
237 /*
238 * did we lose a race to someone else? 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 simple_lock_init(&udv->u_obj.vmobjlock);
253 udv->u_obj.pgops = &uvm_deviceops;
254 TAILQ_INIT(&udv->u_obj.memq); /* not used, but be safe */
255 udv->u_obj.uo_npages = 0;
256 udv->u_obj.uo_refs = 1;
257 udv->u_flags = 0;
258 udv->u_device = device;
259 LIST_INSERT_HEAD(&udv_list, udv, u_list);
260 simple_unlock(&udv_lock);
261
262 return(&udv->u_obj);
263
264 } /* while(1) loop */
265
266 /*NOTREACHED*/
267 }
268
269 /*
270 * udv_reference
271 *
272 * add a reference to a VM object. Note that the reference count must
273 * already be one (the passed in reference) so there is no chance of the
274 * udv being released or locked out here.
275 *
276 * => caller must call with object unlocked.
277 */
278
279 static void
280 udv_reference(uobj)
281 struct uvm_object *uobj;
282 {
283 UVMHIST_FUNC("udv_reference"); UVMHIST_CALLED(maphist);
284
285 simple_lock(&uobj->vmobjlock);
286 uobj->uo_refs++;
287 UVMHIST_LOG(maphist, "<- done (uobj=0x%x, ref = %d)",
288 uobj, uobj->uo_refs,0,0);
289 simple_unlock(&uobj->vmobjlock);
290 }
291
292 /*
293 * udv_detach
294 *
295 * remove a reference to a VM object.
296 *
297 * => caller must call with object unlocked and map locked.
298 */
299
300 static void
301 udv_detach(uobj)
302 struct uvm_object *uobj;
303 {
304 struct uvm_device *udv = (struct uvm_device *) uobj;
305 UVMHIST_FUNC("udv_detach"); UVMHIST_CALLED(maphist);
306
307 /*
308 * loop until done
309 */
310
311 while (1) {
312 simple_lock(&uobj->vmobjlock);
313
314 if (uobj->uo_refs > 1) {
315 uobj->uo_refs--; /* drop ref! */
316 simple_unlock(&uobj->vmobjlock);
317 UVMHIST_LOG(maphist," <- done, uobj=0x%x, ref=%d",
318 uobj,uobj->uo_refs,0,0);
319 return;
320 }
321
322 #ifdef DIAGNOSTIC
323 if (uobj->uo_npages || uobj->memq.tqh_first)
324 panic("udv_detach: pages in a device object?");
325 #endif
326
327 /*
328 * now lock udv_lock
329 */
330 simple_lock(&udv_lock);
331
332 /*
333 * is it being held? if so, wait until others are done.
334 */
335 if (udv->u_flags & UVM_DEVICE_HOLD) {
336
337 /*
338 * want it
339 */
340 udv->u_flags |= UVM_DEVICE_WANTED;
341 simple_unlock(&uobj->vmobjlock);
342 UVM_UNLOCK_AND_WAIT(udv, &udv_lock, FALSE, "udv_detach",0);
343 continue;
344 }
345
346 /*
347 * got it! nuke it now.
348 */
349
350 LIST_REMOVE(udv, u_list);
351 if (udv->u_flags & UVM_DEVICE_WANTED)
352 wakeup(udv);
353 FREE(udv, M_TEMP);
354 break; /* DONE! */
355
356 } /* while (1) loop */
357
358 UVMHIST_LOG(maphist," <- done, freed uobj=0x%x", uobj,0,0,0);
359 return;
360 }
361
362
363 /*
364 * udv_flush
365 *
366 * flush pages out of a uvm object. a no-op for devices.
367 */
368
369 static boolean_t udv_flush(uobj, start, stop, flags)
370 struct uvm_object *uobj;
371 vm_offset_t start, stop;
372 int flags;
373 {
374
375 return(TRUE);
376 }
377
378 /*
379 * udv_fault: non-standard fault routine for device "pages"
380 *
381 * => rather than having a "get" function, we have a fault routine
382 * since we don't return vm_pages we need full control over the
383 * pmap_enter map in
384 * => all the usual fault data structured are locked by the caller
385 * (i.e. maps(read), amap (if any), uobj)
386 * => on return, we unlock all fault data structures
387 * => flags: PGO_ALLPAGES: get all of the pages
388 * PGO_LOCKED: fault data structures are locked
389 * XXX: currently PGO_LOCKED is always required ... consider removing
390 * it as a flag
391 * => NOTE: vaddr is the VA of pps[0] in ufi->entry, _NOT_ pps[centeridx]
392 */
393
394 static int
395 udv_fault(ufi, vaddr, pps, npages, centeridx, fault_type, access_type, flags)
396 struct uvm_faultinfo *ufi;
397 vm_offset_t vaddr;
398 vm_page_t *pps;
399 int npages, centeridx, flags;
400 vm_fault_t fault_type;
401 vm_prot_t access_type;
402 {
403 struct vm_map_entry *entry = ufi->entry;
404 struct uvm_object *uobj = entry->object.uvm_obj;
405 struct uvm_device *udv = (struct uvm_device *)uobj;
406 vm_offset_t curr_offset, curr_va, paddr;
407 int lcv, retval;
408 dev_t device;
409 int (*mapfn) __P((dev_t, int, int));
410 UVMHIST_FUNC("udv_fault"); UVMHIST_CALLED(maphist);
411 UVMHIST_LOG(maphist," flags=%d", flags,0,0,0);
412
413 /*
414 * XXX: !PGO_LOCKED calls are currently not allowed (or used)
415 */
416
417 if ((flags & PGO_LOCKED) == 0)
418 panic("udv_fault: !PGO_LOCKED fault");
419
420 /*
421 * we do not allow device mappings to be mapped copy-on-write
422 * so we kill any attempt to do so here.
423 */
424
425 if (UVM_ET_ISCOPYONWRITE(entry)) {
426 UVMHIST_LOG(maphist, "<- failed -- COW entry (etype=0x%x)",
427 entry->etype, 0,0,0);
428 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj, NULL);
429 return(VM_PAGER_ERROR);
430 }
431
432 /*
433 * get device map function.
434 */
435 device = udv->u_device;
436 mapfn = cdevsw[major(device)].d_mmap;
437
438 /*
439 * now we must determine the offset in udv to use and the VA to use
440 * for pmap_enter. note that we always pmap_enter() in the
441 * ufi->orig_map's pmap, but that our ufi->entry may be from some
442 * other map (in the submap/sharemap case). so we must convert the
443 * VA from ufi->map to ufi->orig_map (note that in many cases these
444 * maps are the same). note that ufi->orig_rvaddr and ufi->rvaddr
445 * refer to the same physical page.
446 */
447 /* udv offset = (offset from start of entry) + entry's offset */
448 curr_offset = (vaddr - entry->start) + entry->offset;
449 /* pmap va = orig_va + (offset of vaddr from translated va) */
450 curr_va = ufi->orig_rvaddr + (vaddr - ufi->rvaddr);
451
452 /*
453 * loop over the page range entering in as needed
454 */
455
456 retval = VM_PAGER_OK;
457 for (lcv = 0 ; lcv < npages ; lcv++, curr_offset += PAGE_SIZE,
458 curr_va += PAGE_SIZE) {
459 if ((flags & PGO_ALLPAGES) == 0 && lcv != centeridx)
460 continue;
461
462 if (pps[lcv] == PGO_DONTCARE)
463 continue;
464
465 paddr = pmap_phys_address((*mapfn)(device, (int)curr_offset,
466 access_type));
467
468 if (paddr == -1) {
469 retval = VM_PAGER_ERROR;
470 break;
471 }
472
473 UVMHIST_LOG(maphist,
474 " MAPPING: device: pm=0x%x, va=0x%x, pa=0x%x, at=%d",
475 ufi->orig_map->pmap, curr_va, paddr, access_type);
476 pmap_enter(ufi->orig_map->pmap, curr_va, paddr, access_type, 0);
477
478 }
479
480 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj, NULL);
481 return(retval);
482 }
483
484 /*
485 * udv_asyncget: start async I/O to bring pages into ram
486 *
487 * => caller must lock object(???XXX: see if this is best)
488 * => a no-op for devices
489 */
490
491 static int
492 udv_asyncget(uobj, offset, npages)
493 struct uvm_object *uobj;
494 vm_offset_t offset;
495 int npages;
496 {
497
498 return(KERN_SUCCESS);
499 }
500
501 /*
502 * udv_put: flush page data to backing store.
503 *
504 * => this function should never be called (since we never have any
505 * page structures to "put")
506 */
507
508 static int
509 udv_put(uobj, pps, npages, flags)
510 struct uvm_object *uobj;
511 struct vm_page **pps;
512 int npages, flags;
513 {
514
515 panic("udv_put: trying to page out to a device!");
516 }
517