FreeBSD/Linux Kernel Cross Reference
sys/uvm/uvm_device.c
1 /* $NetBSD: uvm_device.c,v 1.42 2004/03/24 07:55:01 junyoung 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.42 2004/03/24 07:55:01 junyoung 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 simple_lock_init(&udv->u_obj.vmobjlock);
253 udv->u_obj.pgops = &uvm_deviceops;
254 TAILQ_INIT(&udv->u_obj.memq);
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 return(&udv->u_obj);
262 }
263 /*NOTREACHED*/
264 }
265
266 /*
267 * udv_reference
268 *
269 * add a reference to a VM object. Note that the reference count must
270 * already be one (the passed in reference) so there is no chance of the
271 * udv being released or locked out here.
272 *
273 * => caller must call with object unlocked.
274 */
275
276 static void
277 udv_reference(uobj)
278 struct uvm_object *uobj;
279 {
280 UVMHIST_FUNC("udv_reference"); UVMHIST_CALLED(maphist);
281
282 simple_lock(&uobj->vmobjlock);
283 uobj->uo_refs++;
284 UVMHIST_LOG(maphist, "<- done (uobj=0x%x, ref = %d)",
285 uobj, uobj->uo_refs,0,0);
286 simple_unlock(&uobj->vmobjlock);
287 }
288
289 /*
290 * udv_detach
291 *
292 * remove a reference to a VM object.
293 *
294 * => caller must call with object unlocked and map locked.
295 */
296
297 static void
298 udv_detach(uobj)
299 struct uvm_object *uobj;
300 {
301 struct uvm_device *udv = (struct uvm_device *)uobj;
302 UVMHIST_FUNC("udv_detach"); UVMHIST_CALLED(maphist);
303
304 /*
305 * loop until done
306 */
307 again:
308 simple_lock(&uobj->vmobjlock);
309 if (uobj->uo_refs > 1) {
310 uobj->uo_refs--;
311 simple_unlock(&uobj->vmobjlock);
312 UVMHIST_LOG(maphist," <- done, uobj=0x%x, ref=%d",
313 uobj,uobj->uo_refs,0,0);
314 return;
315 }
316
317 /*
318 * is it being held? if so, wait until others are done.
319 */
320
321 simple_lock(&udv_lock);
322 if (udv->u_flags & UVM_DEVICE_HOLD) {
323 udv->u_flags |= UVM_DEVICE_WANTED;
324 simple_unlock(&uobj->vmobjlock);
325 UVM_UNLOCK_AND_WAIT(udv, &udv_lock, FALSE, "udv_detach",0);
326 goto again;
327 }
328
329 /*
330 * got it! nuke it now.
331 */
332
333 LIST_REMOVE(udv, u_list);
334 if (udv->u_flags & UVM_DEVICE_WANTED)
335 wakeup(udv);
336 simple_unlock(&udv_lock);
337 simple_unlock(&uobj->vmobjlock);
338 FREE(udv, M_TEMP);
339 UVMHIST_LOG(maphist," <- done, freed uobj=0x%x", uobj,0,0,0);
340 }
341
342 /*
343 * udv_fault: non-standard fault routine for device "pages"
344 *
345 * => rather than having a "get" function, we have a fault routine
346 * since we don't return vm_pages we need full control over the
347 * pmap_enter map in
348 * => all the usual fault data structured are locked by the caller
349 * (i.e. maps(read), amap (if any), uobj)
350 * => on return, we unlock all fault data structures
351 * => flags: PGO_ALLPAGES: get all of the pages
352 * PGO_LOCKED: fault data structures are locked
353 * XXX: currently PGO_LOCKED is always required ... consider removing
354 * it as a flag
355 * => NOTE: vaddr is the VA of pps[0] in ufi->entry, _NOT_ pps[centeridx]
356 */
357
358 static int
359 udv_fault(ufi, vaddr, pps, npages, centeridx, fault_type, access_type, flags)
360 struct uvm_faultinfo *ufi;
361 vaddr_t vaddr;
362 struct vm_page **pps;
363 int npages, centeridx, flags;
364 vm_fault_t fault_type;
365 vm_prot_t access_type;
366 {
367 struct vm_map_entry *entry = ufi->entry;
368 struct uvm_object *uobj = entry->object.uvm_obj;
369 struct uvm_device *udv = (struct uvm_device *)uobj;
370 const struct cdevsw *cdev;
371 vaddr_t curr_va;
372 off_t curr_offset;
373 paddr_t paddr, mdpgno;
374 int lcv, retval;
375 dev_t device;
376 paddr_t (*mapfn)(dev_t, off_t, int);
377 vm_prot_t mapprot;
378 UVMHIST_FUNC("udv_fault"); UVMHIST_CALLED(maphist);
379 UVMHIST_LOG(maphist," flags=%d", flags,0,0,0);
380
381 /*
382 * we do not allow device mappings to be mapped copy-on-write
383 * so we kill any attempt to do so here.
384 */
385
386 if (UVM_ET_ISCOPYONWRITE(entry)) {
387 UVMHIST_LOG(maphist, "<- failed -- COW entry (etype=0x%x)",
388 entry->etype, 0,0,0);
389 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj, NULL);
390 return(EIO);
391 }
392
393 /*
394 * get device map function.
395 */
396
397 device = udv->u_device;
398 cdev = cdevsw_lookup(device);
399 if (cdev == NULL) {
400 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj, NULL);
401 return (EIO);
402 }
403 mapfn = cdev->d_mmap;
404
405 /*
406 * now we must determine the offset in udv to use and the VA to
407 * use for pmap_enter. note that we always use orig_map's pmap
408 * for pmap_enter (even if we have a submap). since virtual
409 * addresses in a submap must match the main map, this is ok.
410 */
411
412 /* udv offset = (offset from start of entry) + entry's offset */
413 curr_offset = entry->offset + (vaddr - entry->start);
414 /* pmap va = vaddr (virtual address of pps[0]) */
415 curr_va = vaddr;
416
417 /*
418 * loop over the page range entering in as needed
419 */
420
421 retval = 0;
422 for (lcv = 0 ; lcv < npages ; lcv++, curr_offset += PAGE_SIZE,
423 curr_va += PAGE_SIZE) {
424 if ((flags & PGO_ALLPAGES) == 0 && lcv != centeridx)
425 continue;
426
427 if (pps[lcv] == PGO_DONTCARE)
428 continue;
429
430 mdpgno = (*mapfn)(device, curr_offset, access_type);
431 if (mdpgno == -1) {
432 retval = EIO;
433 break;
434 }
435 paddr = pmap_phys_address(mdpgno);
436 mapprot = ufi->entry->protection;
437 UVMHIST_LOG(maphist,
438 " MAPPING: device: pm=0x%x, va=0x%x, pa=0x%lx, at=%d",
439 ufi->orig_map->pmap, curr_va, paddr, mapprot);
440 if (pmap_enter(ufi->orig_map->pmap, curr_va, paddr,
441 mapprot, PMAP_CANFAIL | mapprot) != 0) {
442 /*
443 * pmap_enter() didn't have the resource to
444 * enter this mapping. Unlock everything,
445 * wait for the pagedaemon to free up some
446 * pages, and then tell uvm_fault() to start
447 * the fault again.
448 *
449 * XXX Needs some rethinking for the PGO_ALLPAGES
450 * XXX case.
451 */
452 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap,
453 uobj, NULL);
454 pmap_update(ufi->orig_map->pmap); /* sync what we have so far */
455 uvm_wait("udv_fault");
456 return (ERESTART);
457 }
458 }
459
460 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj, NULL);
461 pmap_update(ufi->orig_map->pmap);
462 return (retval);
463 }
Cache object: f3d9cc66a2653490eaa8da2d353c46ab
|