xref: /netbsd-src/sys/uvm/uvm_device.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: uvm_device.c,v 1.28 2000/06/27 17:29:20 mrg 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 #include "opt_uvmhist.h"
38 
39 /*
40  * uvm_device.c: the device pager.
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/conf.h>
46 #include <sys/proc.h>
47 #include <sys/malloc.h>
48 #include <sys/vnode.h>
49 
50 #include <uvm/uvm.h>
51 #include <uvm/uvm_device.h>
52 
53 /*
54  * private global data structure
55  *
56  * we keep a list of active device objects in the system.
57  */
58 
59 LIST_HEAD(udv_list_struct, uvm_device);
60 static struct udv_list_struct udv_list;
61 static simple_lock_data_t udv_lock;
62 
63 /*
64  * functions
65  */
66 
67 static void		udv_init __P((void));
68 static void             udv_reference __P((struct uvm_object *));
69 static void             udv_detach __P((struct uvm_object *));
70 static int		udv_fault __P((struct uvm_faultinfo *, vaddr_t,
71 				       vm_page_t *, int, int, vm_fault_t,
72 				       vm_prot_t, int));
73 static int		udv_asyncget __P((struct uvm_object *, voff_t,
74 				       int));
75 static boolean_t        udv_flush __P((struct uvm_object *, voff_t, voff_t,
76 				       int));
77 static int		udv_put __P((struct uvm_object *, vm_page_t *,
78 					int, boolean_t));
79 
80 /*
81  * master pager structure
82  */
83 
84 struct uvm_pagerops uvm_deviceops = {
85 	udv_init,
86 	udv_reference,
87 	udv_detach,
88 	udv_fault,
89 	udv_flush,
90 	NULL,		/* no get function since we have udv_fault */
91 	udv_asyncget,
92 	udv_put,
93 	NULL,		/* no cluster function */
94 	NULL,		/* no put cluster function */
95 	NULL,		/* no AIO-DONE function since no async i/o */
96 	NULL,		/* no releasepg function since no normal pages */
97 };
98 
99 /*
100  * the ops!
101  */
102 
103 /*
104  * udv_init
105  *
106  * init pager private data structures.
107  */
108 
109 void
110 udv_init()
111 {
112 
113 	LIST_INIT(&udv_list);
114 	simple_lock_init(&udv_lock);
115 }
116 
117 /*
118  * udv_attach
119  *
120  * get a VM object that is associated with a device.   allocate a new
121  * one if needed.
122  *
123  * => caller must _not_ already be holding the lock on the uvm_object.
124  * => in fact, nothing should be locked so that we can sleep here.
125  */
126 struct uvm_object *
127 udv_attach(arg, accessprot, off, size)
128 	void *arg;
129 	vm_prot_t accessprot;
130 	voff_t off;			/* used only for access check */
131 	vsize_t size;			/* used only for access check */
132 {
133 	dev_t device = *((dev_t *) arg);
134 	struct uvm_device *udv, *lcv;
135 	paddr_t (*mapfn) __P((dev_t, off_t, int));
136 	UVMHIST_FUNC("udv_attach"); UVMHIST_CALLED(maphist);
137 
138 	UVMHIST_LOG(maphist, "(device=0x%x)", device,0,0,0);
139 
140 	/*
141 	 * before we do anything, ensure this device supports mmap
142 	 */
143 
144 	mapfn = cdevsw[major(device)].d_mmap;
145 	if (mapfn == NULL ||
146 			mapfn == (paddr_t (*) __P((dev_t, off_t, int))) enodev ||
147 			mapfn == (paddr_t (*) __P((dev_t, off_t, int))) nullop)
148 		return(NULL);
149 
150 	/*
151 	 * Negative offsets on the object are not allowed.
152 	 */
153 	if (off < 0)
154 		return(NULL);
155 
156 	/*
157 	 * Check that the specified range of the device allows the
158 	 * desired protection.
159 	 *
160 	 * XXX assumes VM_PROT_* == PROT_*
161 	 * XXX clobbers off and size, but nothing else here needs them.
162 	 */
163 
164 	while (size != 0) {
165 		if ((*mapfn)(device, off, accessprot) == -1)
166 			return (NULL);
167 		off += PAGE_SIZE; size -= PAGE_SIZE;
168 	}
169 
170 	/*
171 	 * keep looping until we get it
172 	 */
173 
174 	while (1) {
175 
176 		/*
177 		 * first, attempt to find it on the main list
178 		 */
179 
180 		simple_lock(&udv_lock);
181 		for (lcv = udv_list.lh_first ; lcv != NULL ; lcv = lcv->u_list.le_next) {
182 			if (device == lcv->u_device)
183 				break;
184 		}
185 
186 		/*
187 		 * got it on main list.  put a hold on it and unlock udv_lock.
188 		 */
189 
190 		if (lcv) {
191 
192 			/*
193 			 * if someone else has a hold on it, sleep and start
194 			 * over again.
195 			 */
196 
197 			if (lcv->u_flags & UVM_DEVICE_HOLD) {
198 				lcv->u_flags |= UVM_DEVICE_WANTED;
199 				UVM_UNLOCK_AND_WAIT(lcv, &udv_lock, FALSE,
200 				    "udv_attach",0);
201 				continue;
202 			}
203 
204 			/* we are now holding it */
205 			lcv->u_flags |= UVM_DEVICE_HOLD;
206 			simple_unlock(&udv_lock);
207 
208 			/*
209 			 * bump reference count, unhold, return.
210 			 */
211 
212 			simple_lock(&lcv->u_obj.vmobjlock);
213 			lcv->u_obj.uo_refs++;
214 			simple_unlock(&lcv->u_obj.vmobjlock);
215 
216 			simple_lock(&udv_lock);
217 			if (lcv->u_flags & UVM_DEVICE_WANTED)
218 				wakeup(lcv);
219 			lcv->u_flags &= ~(UVM_DEVICE_WANTED|UVM_DEVICE_HOLD);
220 			simple_unlock(&udv_lock);
221 			return(&lcv->u_obj);
222 		}
223 
224 		/*
225 		 * did not find it on main list.   need to malloc a new one.
226 		 */
227 
228 		simple_unlock(&udv_lock);
229 		/* NOTE: we could sleep in the following malloc() */
230 		MALLOC(udv, struct uvm_device *, sizeof(*udv), M_TEMP, M_WAITOK);
231 		simple_lock(&udv_lock);
232 
233 		/*
234 		 * now we have to double check to make sure no one added it
235 		 * to the list while we were sleeping...
236 		 */
237 
238 		for (lcv = udv_list.lh_first ; lcv != NULL ;
239 		    lcv = lcv->u_list.le_next) {
240 			if (device == lcv->u_device)
241 				break;
242 		}
243 
244 		/*
245 		 * did we lose a race to someone else?   free our memory and retry.
246 		 */
247 
248 		if (lcv) {
249 			simple_unlock(&udv_lock);
250 			FREE(udv, M_TEMP);
251 			continue;
252 		}
253 
254 		/*
255 		 * we have it!   init the data structures, add to list
256 		 * and return.
257 		 */
258 
259 		simple_lock_init(&udv->u_obj.vmobjlock);
260 		udv->u_obj.pgops = &uvm_deviceops;
261 		TAILQ_INIT(&udv->u_obj.memq);	/* not used, but be safe */
262 		udv->u_obj.uo_npages = 0;
263 		udv->u_obj.uo_refs = 1;
264 		udv->u_flags = 0;
265 		udv->u_device = device;
266 		LIST_INSERT_HEAD(&udv_list, udv, u_list);
267 		simple_unlock(&udv_lock);
268 
269 		return(&udv->u_obj);
270 
271 	}  /* while(1) loop */
272 
273 	/*NOTREACHED*/
274 }
275 
276 /*
277  * udv_reference
278  *
279  * add a reference to a VM object.   Note that the reference count must
280  * already be one (the passed in reference) so there is no chance of the
281  * udv being released or locked out here.
282  *
283  * => caller must call with object unlocked.
284  */
285 
286 static void
287 udv_reference(uobj)
288 	struct uvm_object *uobj;
289 {
290 	UVMHIST_FUNC("udv_reference"); UVMHIST_CALLED(maphist);
291 
292 	simple_lock(&uobj->vmobjlock);
293 	uobj->uo_refs++;
294 	UVMHIST_LOG(maphist, "<- done (uobj=0x%x, ref = %d)",
295 	uobj, uobj->uo_refs,0,0);
296 	simple_unlock(&uobj->vmobjlock);
297 }
298 
299 /*
300  * udv_detach
301  *
302  * remove a reference to a VM object.
303  *
304  * => caller must call with object unlocked and map locked.
305  */
306 
307 static void
308 udv_detach(uobj)
309 	struct uvm_object *uobj;
310 {
311 	struct uvm_device *udv = (struct uvm_device *) uobj;
312 	UVMHIST_FUNC("udv_detach"); UVMHIST_CALLED(maphist);
313 
314 
315 	/*
316 	 * loop until done
317 	 */
318 again:
319 	simple_lock(&uobj->vmobjlock);
320 
321 	if (uobj->uo_refs > 1) {
322 		uobj->uo_refs--;			/* drop ref! */
323 		simple_unlock(&uobj->vmobjlock);
324 		UVMHIST_LOG(maphist," <- done, uobj=0x%x, ref=%d",
325 			  uobj,uobj->uo_refs,0,0);
326 		return;
327 	}
328 
329 #ifdef DIAGNOSTIC
330 	if (uobj->uo_npages || !TAILQ_EMPTY(&uobj->memq))
331 		panic("udv_detach: pages in a device object?");
332 #endif
333 
334 	/*
335 	 * now lock udv_lock
336 	 */
337 	simple_lock(&udv_lock);
338 
339 	/*
340 	 * is it being held?   if so, wait until others are done.
341 	 */
342 	if (udv->u_flags & UVM_DEVICE_HOLD) {
343 		udv->u_flags |= UVM_DEVICE_WANTED;
344 		simple_unlock(&uobj->vmobjlock);
345 		UVM_UNLOCK_AND_WAIT(udv, &udv_lock, FALSE, "udv_detach",0);
346 		goto again;
347 	}
348 
349 	/*
350 	 * got it!   nuke it now.
351 	 */
352 	LIST_REMOVE(udv, u_list);
353 	if (udv->u_flags & UVM_DEVICE_WANTED)
354 		wakeup(udv);
355 	simple_unlock(&udv_lock);
356 	simple_unlock(&uobj->vmobjlock);
357 	FREE(udv, M_TEMP);
358 
359 	UVMHIST_LOG(maphist," <- done, freed uobj=0x%x", uobj,0,0,0);
360 	return;
361 }
362 
363 
364 /*
365  * udv_flush
366  *
367  * flush pages out of a uvm object.   a no-op for devices.
368  */
369 
370 static boolean_t udv_flush(uobj, start, stop, flags)
371 	struct uvm_object *uobj;
372 	voff_t start, stop;
373 	int flags;
374 {
375 
376 	return(TRUE);
377 }
378 
379 /*
380  * udv_fault: non-standard fault routine for device "pages"
381  *
382  * => rather than having a "get" function, we have a fault routine
383  *	since we don't return vm_pages we need full control over the
384  *	pmap_enter map in
385  * => all the usual fault data structured are locked by the caller
386  *	(i.e. maps(read), amap (if any), uobj)
387  * => on return, we unlock all fault data structures
388  * => flags: PGO_ALLPAGES: get all of the pages
389  *	     PGO_LOCKED: fault data structures are locked
390  *    XXX: currently PGO_LOCKED is always required ... consider removing
391  *	it as a flag
392  * => NOTE: vaddr is the VA of pps[0] in ufi->entry, _NOT_ pps[centeridx]
393  */
394 
395 static int
396 udv_fault(ufi, vaddr, pps, npages, centeridx, fault_type, access_type, flags)
397 	struct uvm_faultinfo *ufi;
398 	vaddr_t vaddr;
399 	vm_page_t *pps;
400 	int npages, centeridx, flags;
401 	vm_fault_t fault_type;
402 	vm_prot_t access_type;
403 {
404 	struct vm_map_entry *entry = ufi->entry;
405 	struct uvm_object *uobj = entry->object.uvm_obj;
406 	struct uvm_device *udv = (struct uvm_device *)uobj;
407 	vaddr_t curr_va;
408 	off_t curr_offset;
409 	paddr_t paddr, mdpgno;
410 	int lcv, retval;
411 	dev_t device;
412 	paddr_t (*mapfn) __P((dev_t, off_t, int));
413 	vm_prot_t mapprot;
414 	UVMHIST_FUNC("udv_fault"); UVMHIST_CALLED(maphist);
415 	UVMHIST_LOG(maphist,"  flags=%d", flags,0,0,0);
416 
417 	/*
418 	 * XXX: !PGO_LOCKED calls are currently not allowed (or used)
419 	 */
420 
421 	if ((flags & PGO_LOCKED) == 0)
422 		panic("udv_fault: !PGO_LOCKED fault");
423 
424 	/*
425 	 * we do not allow device mappings to be mapped copy-on-write
426 	 * so we kill any attempt to do so here.
427 	 */
428 
429 	if (UVM_ET_ISCOPYONWRITE(entry)) {
430 		UVMHIST_LOG(maphist, "<- failed -- COW entry (etype=0x%x)",
431 		entry->etype, 0,0,0);
432 		uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj, NULL);
433 		return(VM_PAGER_ERROR);
434 	}
435 
436 	/*
437 	 * get device map function.
438 	 */
439 	device = udv->u_device;
440 	mapfn = cdevsw[major(device)].d_mmap;
441 
442 	/*
443 	 * now we must determine the offset in udv to use and the VA to
444 	 * use for pmap_enter.  note that we always use orig_map's pmap
445 	 * for pmap_enter (even if we have a submap).   since virtual
446 	 * addresses in a submap must match the main map, this is ok.
447 	 */
448 	/* udv offset = (offset from start of entry) + entry's offset */
449 	curr_offset = entry->offset + (vaddr - entry->start);
450 	/* pmap va = vaddr (virtual address of pps[0]) */
451 	curr_va = vaddr;
452 
453 	/*
454 	 * loop over the page range entering in as needed
455 	 */
456 
457 	retval = VM_PAGER_OK;
458 	for (lcv = 0 ; lcv < npages ; lcv++, curr_offset += PAGE_SIZE,
459 	    curr_va += PAGE_SIZE) {
460 		if ((flags & PGO_ALLPAGES) == 0 && lcv != centeridx)
461 			continue;
462 
463 		if (pps[lcv] == PGO_DONTCARE)
464 			continue;
465 
466 		mdpgno = (*mapfn)(device, curr_offset, access_type);
467 		if (mdpgno == -1) {
468 			retval = VM_PAGER_ERROR;
469 			break;
470 		}
471 		paddr = pmap_phys_address(mdpgno);
472 		mapprot = ufi->entry->protection;
473 		UVMHIST_LOG(maphist,
474 		    "  MAPPING: device: pm=0x%x, va=0x%x, pa=0x%lx, at=%d",
475 		    ufi->orig_map->pmap, curr_va, paddr, mapprot);
476 		if (pmap_enter(ufi->orig_map->pmap, curr_va, paddr,
477 		    mapprot, PMAP_CANFAIL | mapprot) != KERN_SUCCESS) {
478 			/*
479 			 * pmap_enter() didn't have the resource to
480 			 * enter this mapping.  Unlock everything,
481 			 * wait for the pagedaemon to free up some
482 			 * pages, and then tell uvm_fault() to start
483 			 * the fault again.
484 			 *
485 			 * XXX Needs some rethinking for the PGO_ALLPAGES
486 			 * XXX case.
487 			 */
488 			uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap,
489 			    uobj, NULL);
490 			uvm_wait("udv_fault");
491 			return (VM_PAGER_REFAULT);
492 		}
493 	}
494 
495 	uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj, NULL);
496 	return (retval);
497 }
498 
499 /*
500  * udv_asyncget: start async I/O to bring pages into ram
501  *
502  * => caller must lock object(???XXX: see if this is best)
503  * => a no-op for devices
504  */
505 
506 static int
507 udv_asyncget(uobj, offset, npages)
508 	struct uvm_object *uobj;
509 	voff_t offset;
510 	int npages;
511 {
512 
513 	return(KERN_SUCCESS);
514 }
515 
516 /*
517  * udv_put: flush page data to backing store.
518  *
519  * => this function should never be called (since we never have any
520  *	page structures to "put")
521  */
522 
523 static int
524 udv_put(uobj, pps, npages, flags)
525 	struct uvm_object *uobj;
526 	struct vm_page **pps;
527 	int npages, flags;
528 {
529 
530 	panic("udv_put: trying to page out to a device!");
531 }
532