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