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