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