FreeBSD/Linux Kernel Cross Reference
sys/uvm/uvm_device.c
1 /* $NetBSD: uvm_device.c,v 1.54.8.1 2009/06/23 06:56:52 snj 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.54.8.1 2009/06/23 06:56:52 snj 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 kmutex_t 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_prot_t,
75 int);
76
77 /*
78 * master pager structure
79 */
80
81 const struct uvm_pagerops uvm_deviceops = {
82 .pgo_init = udv_init,
83 .pgo_reference = udv_reference,
84 .pgo_detach = udv_detach,
85 .pgo_fault = udv_fault,
86 };
87
88 /*
89 * the ops!
90 */
91
92 /*
93 * udv_init
94 *
95 * init pager private data structures.
96 */
97
98 static void
99 udv_init(void)
100 {
101 LIST_INIT(&udv_list);
102 mutex_init(&udv_lock, MUTEX_DEFAULT, IPL_NONE);
103 }
104
105 /*
106 * udv_attach
107 *
108 * get a VM object that is associated with a device. allocate a new
109 * one if needed.
110 *
111 * => caller must _not_ already be holding the lock on the uvm_object.
112 * => in fact, nothing should be locked so that we can sleep here.
113 */
114
115 struct uvm_object *
116 udv_attach(void *arg, vm_prot_t accessprot,
117 voff_t off, /* used only for access check */
118 vsize_t size /* used only for access check */)
119 {
120 dev_t device = *((dev_t *)arg);
121 struct uvm_device *udv, *lcv;
122 const struct cdevsw *cdev;
123 dev_type_mmap((*mapfn));
124
125 UVMHIST_FUNC("udv_attach"); UVMHIST_CALLED(maphist);
126
127 UVMHIST_LOG(maphist, "(device=0x%x)", device,0,0,0);
128
129 /*
130 * before we do anything, ensure this device supports mmap
131 */
132
133 cdev = cdevsw_lookup(device);
134 if (cdev == NULL) {
135 return (NULL);
136 }
137 mapfn = cdev->d_mmap;
138 if (mapfn == NULL || mapfn == nommap || mapfn == nullmmap) {
139 return(NULL);
140 }
141
142 /*
143 * Negative offsets on the object are not allowed.
144 */
145
146 if ((cdev->d_flag & D_NEGOFFSAFE) == 0 &&
147 off != UVM_UNKNOWN_OFFSET && off < 0)
148 return(NULL);
149
150 /*
151 * Check that the specified range of the device allows the
152 * desired protection.
153 *
154 * XXX assumes VM_PROT_* == PROT_*
155 * XXX clobbers off and size, but nothing else here needs them.
156 */
157
158 while (size != 0) {
159 if (cdev_mmap(device, off, accessprot) == -1) {
160 return (NULL);
161 }
162 off += PAGE_SIZE; size -= PAGE_SIZE;
163 }
164
165 /*
166 * keep looping until we get it
167 */
168
169 for (;;) {
170
171 /*
172 * first, attempt to find it on the main list
173 */
174
175 mutex_enter(&udv_lock);
176 LIST_FOREACH(lcv, &udv_list, u_list) {
177 if (device == lcv->u_device)
178 break;
179 }
180
181 /*
182 * got it on main list. put a hold on it and unlock udv_lock.
183 */
184
185 if (lcv) {
186
187 /*
188 * if someone else has a hold on it, sleep and start
189 * over again.
190 */
191
192 if (lcv->u_flags & UVM_DEVICE_HOLD) {
193 lcv->u_flags |= UVM_DEVICE_WANTED;
194 UVM_UNLOCK_AND_WAIT(lcv, &udv_lock, false,
195 "udv_attach",0);
196 continue;
197 }
198
199 /* we are now holding it */
200 lcv->u_flags |= UVM_DEVICE_HOLD;
201 mutex_exit(&udv_lock);
202
203 /*
204 * bump reference count, unhold, return.
205 */
206
207 mutex_enter(&lcv->u_obj.vmobjlock);
208 lcv->u_obj.uo_refs++;
209 mutex_exit(&lcv->u_obj.vmobjlock);
210
211 mutex_enter(&udv_lock);
212 if (lcv->u_flags & UVM_DEVICE_WANTED)
213 wakeup(lcv);
214 lcv->u_flags &= ~(UVM_DEVICE_WANTED|UVM_DEVICE_HOLD);
215 mutex_exit(&udv_lock);
216 return(&lcv->u_obj);
217 }
218
219 /*
220 * did not find it on main list. need to malloc a new one.
221 */
222
223 mutex_exit(&udv_lock);
224 /* NOTE: we could sleep in the following malloc() */
225 MALLOC(udv, struct uvm_device *, sizeof(*udv), M_TEMP,
226 M_WAITOK);
227 mutex_enter(&udv_lock);
228
229 /*
230 * now we have to double check to make sure no one added it
231 * to the list while we were sleeping...
232 */
233
234 LIST_FOREACH(lcv, &udv_list, u_list) {
235 if (device == lcv->u_device)
236 break;
237 }
238
239 /*
240 * did we lose a race to someone else?
241 * free our memory and retry.
242 */
243
244 if (lcv) {
245 mutex_exit(&udv_lock);
246 FREE(udv, M_TEMP);
247 continue;
248 }
249
250 /*
251 * we have it! init the data structures, add to list
252 * and return.
253 */
254
255 UVM_OBJ_INIT(&udv->u_obj, &uvm_deviceops, 1);
256 udv->u_flags = 0;
257 udv->u_device = device;
258 LIST_INSERT_HEAD(&udv_list, udv, u_list);
259 mutex_exit(&udv_lock);
260 return(&udv->u_obj);
261 }
262 /*NOTREACHED*/
263 }
264
265 /*
266 * udv_reference
267 *
268 * add a reference to a VM object. Note that the reference count must
269 * already be one (the passed in reference) so there is no chance of the
270 * udv being released or locked out here.
271 *
272 * => caller must call with object unlocked.
273 */
274
275 static void
276 udv_reference(struct uvm_object *uobj)
277 {
278 UVMHIST_FUNC("udv_reference"); UVMHIST_CALLED(maphist);
279
280 mutex_enter(&uobj->vmobjlock);
281 uobj->uo_refs++;
282 UVMHIST_LOG(maphist, "<- done (uobj=0x%x, ref = %d)",
283 uobj, uobj->uo_refs,0,0);
284 mutex_exit(&uobj->vmobjlock);
285 }
286
287 /*
288 * udv_detach
289 *
290 * remove a reference to a VM object.
291 *
292 * => caller must call with object unlocked and map locked.
293 */
294
295 static void
296 udv_detach(struct uvm_object *uobj)
297 {
298 struct uvm_device *udv = (struct uvm_device *)uobj;
299 UVMHIST_FUNC("udv_detach"); UVMHIST_CALLED(maphist);
300
301 /*
302 * loop until done
303 */
304 again:
305 mutex_enter(&uobj->vmobjlock);
306 if (uobj->uo_refs > 1) {
307 uobj->uo_refs--;
308 mutex_exit(&uobj->vmobjlock);
309 UVMHIST_LOG(maphist," <- done, uobj=0x%x, ref=%d",
310 uobj,uobj->uo_refs,0,0);
311 return;
312 }
313
314 /*
315 * is it being held? if so, wait until others are done.
316 */
317
318 mutex_enter(&udv_lock);
319 if (udv->u_flags & UVM_DEVICE_HOLD) {
320 udv->u_flags |= UVM_DEVICE_WANTED;
321 mutex_exit(&uobj->vmobjlock);
322 UVM_UNLOCK_AND_WAIT(udv, &udv_lock, false, "udv_detach",0);
323 goto again;
324 }
325
326 /*
327 * got it! nuke it now.
328 */
329
330 LIST_REMOVE(udv, u_list);
331 if (udv->u_flags & UVM_DEVICE_WANTED)
332 wakeup(udv);
333 mutex_exit(&udv_lock);
334 mutex_exit(&uobj->vmobjlock);
335 UVM_OBJ_DESTROY(uobj);
336 FREE(udv, M_TEMP);
337 UVMHIST_LOG(maphist," <- done, freed uobj=0x%x", uobj,0,0,0);
338 }
339
340 /*
341 * udv_fault: non-standard fault routine for device "pages"
342 *
343 * => rather than having a "get" function, we have a fault routine
344 * since we don't return vm_pages we need full control over the
345 * pmap_enter map in
346 * => all the usual fault data structured are locked by the caller
347 * (i.e. maps(read), amap (if any), uobj)
348 * => on return, we unlock all fault data structures
349 * => flags: PGO_ALLPAGES: get all of the pages
350 * PGO_LOCKED: fault data structures are locked
351 * XXX: currently PGO_LOCKED is always required ... consider removing
352 * it as a flag
353 * => NOTE: vaddr is the VA of pps[0] in ufi->entry, _NOT_ pps[centeridx]
354 */
355
356 static int
357 udv_fault(struct uvm_faultinfo *ufi, vaddr_t vaddr, struct vm_page **pps,
358 int npages, int centeridx, vm_prot_t access_type,
359 int flags)
360 {
361 struct vm_map_entry *entry = ufi->entry;
362 struct uvm_object *uobj = entry->object.uvm_obj;
363 struct uvm_device *udv = (struct uvm_device *)uobj;
364 vaddr_t curr_va;
365 off_t curr_offset;
366 paddr_t paddr, mdpgno;
367 int lcv, retval;
368 dev_t device;
369 vm_prot_t mapprot;
370 UVMHIST_FUNC("udv_fault"); UVMHIST_CALLED(maphist);
371 UVMHIST_LOG(maphist," flags=%d", flags,0,0,0);
372
373 /*
374 * we do not allow device mappings to be mapped copy-on-write
375 * so we kill any attempt to do so here.
376 */
377
378 if (UVM_ET_ISCOPYONWRITE(entry)) {
379 UVMHIST_LOG(maphist, "<- failed -- COW entry (etype=0x%x)",
380 entry->etype, 0,0,0);
381 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj, NULL);
382 return(EIO);
383 }
384
385 /*
386 * get device map function.
387 */
388
389 device = udv->u_device;
390 if (cdevsw_lookup(device) == NULL) {
391 /* XXX This should not happen */
392 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj, NULL);
393 return (EIO);
394 }
395
396 /*
397 * now we must determine the offset in udv to use and the VA to
398 * use for pmap_enter. note that we always use orig_map's pmap
399 * for pmap_enter (even if we have a submap). since virtual
400 * addresses in a submap must match the main map, this is ok.
401 */
402
403 /* udv offset = (offset from start of entry) + entry's offset */
404 curr_offset = entry->offset + (vaddr - entry->start);
405 /* pmap va = vaddr (virtual address of pps[0]) */
406 curr_va = vaddr;
407
408 /*
409 * loop over the page range entering in as needed
410 */
411
412 retval = 0;
413 for (lcv = 0 ; lcv < npages ; lcv++, curr_offset += PAGE_SIZE,
414 curr_va += PAGE_SIZE) {
415 if ((flags & PGO_ALLPAGES) == 0 && lcv != centeridx)
416 continue;
417
418 if (pps[lcv] == PGO_DONTCARE)
419 continue;
420
421 mdpgno = cdev_mmap(device, curr_offset, access_type);
422 if (mdpgno == -1) {
423 retval = EIO;
424 break;
425 }
426 paddr = pmap_phys_address(mdpgno);
427 mapprot = ufi->entry->protection;
428 UVMHIST_LOG(maphist,
429 " MAPPING: device: pm=0x%x, va=0x%x, pa=0x%lx, at=%d",
430 ufi->orig_map->pmap, curr_va, paddr, mapprot);
431 if (pmap_enter(ufi->orig_map->pmap, curr_va, paddr,
432 mapprot, PMAP_CANFAIL | mapprot) != 0) {
433 /*
434 * pmap_enter() didn't have the resource to
435 * enter this mapping. Unlock everything,
436 * wait for the pagedaemon to free up some
437 * pages, and then tell uvm_fault() to start
438 * the fault again.
439 *
440 * XXX Needs some rethinking for the PGO_ALLPAGES
441 * XXX case.
442 */
443 pmap_update(ufi->orig_map->pmap); /* sync what we have so far */
444 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap,
445 uobj, NULL);
446 uvm_wait("udv_fault");
447 return (ERESTART);
448 }
449 }
450
451 pmap_update(ufi->orig_map->pmap);
452 uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj, NULL);
453 return (retval);
454 }
Cache object: 4eac978687ce2cc89d7a5a0fc9cd1ccb
|