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