Home | History | Annotate | Line # | Download | only in uvm
uvm_device.c revision 1.13
      1 /*	$NetBSD: uvm_device.c,v 1.13 1999/03/24 03:52:41 cgd Exp $	*/
      2 
      3 /*
      4  * XXXCDC: "ROUGH DRAFT" QUALITY UVM PRE-RELEASE FILE!
      5  *	   >>>USE AT YOUR OWN RISK, WORK IS NOT FINISHED<<<
      6  */
      7 
      8 /*
      9  *
     10  * Copyright (c) 1997 Charles D. Cranor and Washington University.
     11  * All rights reserved.
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  * 3. All advertising materials mentioning features or use of this software
     22  *    must display the following acknowledgement:
     23  *      This product includes software developed by Charles D. Cranor and
     24  *      Washington University.
     25  * 4. The name of the author may not be used to endorse or promote products
     26  *    derived from this software without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     29  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     30  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     31  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     33  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     34  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     35  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     36  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     37  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     38  *
     39  * from: Id: uvm_device.c,v 1.1.2.9 1998/02/06 05:11:47 chs Exp
     40  */
     41 
     42 #include "opt_uvmhist.h"
     43 
     44 /*
     45  * uvm_device.c: the device pager.
     46  */
     47 
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/conf.h>
     51 #include <sys/proc.h>
     52 #include <sys/malloc.h>
     53 #include <sys/vnode.h>
     54 
     55 #include <vm/vm.h>
     56 #include <vm/vm_page.h>
     57 #include <vm/vm_kern.h>
     58 
     59 #include <uvm/uvm.h>
     60 #include <uvm/uvm_device.h>
     61 
     62 /*
     63  * private global data structure
     64  *
     65  * we keep a list of active device objects in the system.
     66  */
     67 
     68 LIST_HEAD(udv_list_struct, uvm_device);
     69 static struct udv_list_struct udv_list;
     70 static simple_lock_data_t udv_lock;
     71 
     72 /*
     73  * functions
     74  */
     75 
     76 static void		udv_init __P((void));
     77 struct uvm_object 	*udv_attach __P((void *, vm_prot_t, vaddr_t, vsize_t));
     78 static void             udv_reference __P((struct uvm_object *));
     79 static void             udv_detach __P((struct uvm_object *));
     80 static int		udv_fault __P((struct uvm_faultinfo *, vaddr_t,
     81 				       vm_page_t *, int, int, vm_fault_t,
     82 				       vm_prot_t, int));
     83 static boolean_t        udv_flush __P((struct uvm_object *, vaddr_t,
     84 					 vaddr_t, int));
     85 static int		udv_asyncget __P((struct uvm_object *, vaddr_t,
     86 					    int));
     87 static int		udv_put __P((struct uvm_object *, vm_page_t *,
     88 					int, boolean_t));
     89 
     90 /*
     91  * master pager structure
     92  */
     93 
     94 struct uvm_pagerops uvm_deviceops = {
     95 	udv_init,
     96 	udv_reference,
     97 	udv_detach,
     98 	udv_fault,
     99 	udv_flush,
    100 	NULL,		/* no get function since we have udv_fault */
    101 	udv_asyncget,
    102 	udv_put,
    103 	NULL,		/* no cluster function */
    104 	NULL,		/* no put cluster function */
    105 	NULL,		/* no share protect.   no share maps for us */
    106 	NULL,		/* no AIO-DONE function since no async i/o */
    107 	NULL,		/* no releasepg function since no normal pages */
    108 };
    109 
    110 /*
    111  * the ops!
    112  */
    113 
    114 /*
    115  * udv_init
    116  *
    117  * init pager private data structures.
    118  */
    119 
    120 void
    121 udv_init()
    122 {
    123 
    124 	LIST_INIT(&udv_list);
    125 	simple_lock_init(&udv_lock);
    126 }
    127 
    128 /*
    129  * udv_attach
    130  *
    131  * get a VM object that is associated with a device.   allocate a new
    132  * one if needed.
    133  *
    134  * => caller must _not_ already be holding the lock on the uvm_object.
    135  * => in fact, nothing should be locked so that we can sleep here.
    136  */
    137 struct uvm_object *
    138 udv_attach(arg, accessprot, off, size)
    139 	void *arg;
    140 	vm_prot_t accessprot;
    141 	vaddr_t off;			/* used only for access check */
    142 	vsize_t size;			/* used only for access check */
    143 {
    144 	dev_t device = *((dev_t *) arg);
    145 	struct uvm_device *udv, *lcv;
    146 	int (*mapfn) __P((dev_t, int, int));
    147 	UVMHIST_FUNC("udv_attach"); UVMHIST_CALLED(maphist);
    148 
    149 	UVMHIST_LOG(maphist, "(device=0x%x)", device,0,0,0);
    150 
    151 	/*
    152 	 * before we do anything, ensure this device supports mmap
    153 	 */
    154 
    155 	mapfn = cdevsw[major(device)].d_mmap;
    156 	if (mapfn == NULL ||
    157 			mapfn == (int (*) __P((dev_t, int, int))) enodev ||
    158 			mapfn == (int (*) __P((dev_t, int, int))) nullop)
    159 		return(NULL);
    160 
    161 	/*
    162 	 * Check that the specified range of the device allows the
    163 	 * desired protection.
    164 	 *
    165 	 * XXX assumes VM_PROT_* == PROT_*
    166 	 * XXX clobbers off and size, but nothing else here needs them.
    167 	 */
    168 
    169 	while (size != 0) {
    170 		if ((*mapfn)(device, off, accessprot) == -1)
    171 			return (NULL);
    172 		off += PAGE_SIZE, size -= PAGE_SIZE;
    173 	}
    174 
    175 	/*
    176 	 * keep looping until we get it
    177 	 */
    178 
    179 	while (1) {
    180 
    181 		/*
    182 		 * first, attempt to find it on the main list
    183 		 */
    184 
    185 		simple_lock(&udv_lock);
    186 		for (lcv = udv_list.lh_first ; lcv != NULL ; lcv = lcv->u_list.le_next) {
    187 			if (device == lcv->u_device)
    188 				break;
    189 		}
    190 
    191 		/*
    192 		 * got it on main list.  put a hold on it and unlock udv_lock.
    193 		 */
    194 
    195 		if (lcv) {
    196 
    197 			/*
    198 			 * if someone else has a hold on it, sleep and start
    199 			 * over again.
    200 			 */
    201 
    202 			if (lcv->u_flags & UVM_DEVICE_HOLD) {
    203 				lcv->u_flags |= UVM_DEVICE_WANTED;
    204 				UVM_UNLOCK_AND_WAIT(lcv, &udv_lock, FALSE,
    205 				    "udv_attach",0);
    206 				continue;
    207 			}
    208 
    209 			/* we are now holding it */
    210 			lcv->u_flags |= UVM_DEVICE_HOLD;
    211 			simple_unlock(&udv_lock);
    212 
    213 			/*
    214 			 * bump reference count, unhold, return.
    215 			 */
    216 
    217 			simple_lock(&lcv->u_obj.vmobjlock);
    218 			lcv->u_obj.uo_refs++;
    219 			simple_unlock(&lcv->u_obj.vmobjlock);
    220 
    221 			simple_lock(&udv_lock);
    222 			if (lcv->u_flags & UVM_DEVICE_WANTED)
    223 				wakeup(lcv);
    224 			lcv->u_flags &= ~(UVM_DEVICE_WANTED|UVM_DEVICE_HOLD);
    225 			simple_unlock(&udv_lock);
    226 			return(&lcv->u_obj);
    227 		}
    228 
    229 		/*
    230 		 * did not find it on main list.   need to malloc a new one.
    231 		 */
    232 
    233 		simple_unlock(&udv_lock);
    234 		/* NOTE: we could sleep in the following malloc() */
    235 		MALLOC(udv, struct uvm_device *, sizeof(*udv), M_TEMP, M_WAITOK);
    236 		simple_lock(&udv_lock);
    237 
    238 		/*
    239 		 * now we have to double check to make sure no one added it
    240 		 * to the list while we were sleeping...
    241 		 */
    242 
    243 		for (lcv = udv_list.lh_first ; lcv != NULL ;
    244 		    lcv = lcv->u_list.le_next) {
    245 			if (device == lcv->u_device)
    246 				break;
    247 		}
    248 
    249 		/*
    250 		 * did we lose a race to someone else?   free our memory and retry.
    251 		 */
    252 
    253 		if (lcv) {
    254 			simple_unlock(&udv_lock);
    255 			FREE(udv, M_TEMP);
    256 			continue;
    257 		}
    258 
    259 		/*
    260 		 * we have it!   init the data structures, add to list
    261 		 * and return.
    262 		 */
    263 
    264 		simple_lock_init(&udv->u_obj.vmobjlock);
    265 		udv->u_obj.pgops = &uvm_deviceops;
    266 		TAILQ_INIT(&udv->u_obj.memq);	/* not used, but be safe */
    267 		udv->u_obj.uo_npages = 0;
    268 		udv->u_obj.uo_refs = 1;
    269 		udv->u_flags = 0;
    270 		udv->u_device = device;
    271 		LIST_INSERT_HEAD(&udv_list, udv, u_list);
    272 		simple_unlock(&udv_lock);
    273 
    274 		return(&udv->u_obj);
    275 
    276 	}  /* while(1) loop */
    277 
    278 	/*NOTREACHED*/
    279 }
    280 
    281 /*
    282  * udv_reference
    283  *
    284  * add a reference to a VM object.   Note that the reference count must
    285  * already be one (the passed in reference) so there is no chance of the
    286  * udv being released or locked out here.
    287  *
    288  * => caller must call with object unlocked.
    289  */
    290 
    291 static void
    292 udv_reference(uobj)
    293 	struct uvm_object *uobj;
    294 {
    295 	UVMHIST_FUNC("udv_reference"); UVMHIST_CALLED(maphist);
    296 
    297 	simple_lock(&uobj->vmobjlock);
    298 	uobj->uo_refs++;
    299 	UVMHIST_LOG(maphist, "<- done (uobj=0x%x, ref = %d)",
    300 	uobj, uobj->uo_refs,0,0);
    301 	simple_unlock(&uobj->vmobjlock);
    302 }
    303 
    304 /*
    305  * udv_detach
    306  *
    307  * remove a reference to a VM object.
    308  *
    309  * => caller must call with object unlocked and map locked.
    310  */
    311 
    312 static void
    313 udv_detach(uobj)
    314 	struct uvm_object *uobj;
    315 {
    316 	struct uvm_device *udv = (struct uvm_device *) uobj;
    317 	UVMHIST_FUNC("udv_detach"); UVMHIST_CALLED(maphist);
    318 
    319 	/*
    320 	 * loop until done
    321 	 */
    322 
    323 	while (1) {
    324 		simple_lock(&uobj->vmobjlock);
    325 
    326 		if (uobj->uo_refs > 1) {
    327 			uobj->uo_refs--;			/* drop ref! */
    328 			simple_unlock(&uobj->vmobjlock);
    329 			UVMHIST_LOG(maphist," <- done, uobj=0x%x, ref=%d",
    330 				  uobj,uobj->uo_refs,0,0);
    331 			return;
    332 		}
    333 
    334 #ifdef DIAGNOSTIC
    335 		if (uobj->uo_npages || uobj->memq.tqh_first)
    336 			panic("udv_detach: pages in a device object?");
    337 #endif
    338 
    339 		/*
    340 		 * now lock udv_lock
    341 		 */
    342 		simple_lock(&udv_lock);
    343 
    344 		/*
    345 		 * is it being held?   if so, wait until others are done.
    346 		 */
    347 		if (udv->u_flags & UVM_DEVICE_HOLD) {
    348 
    349 			/*
    350 			 * want it
    351 			 */
    352 			udv->u_flags |= UVM_DEVICE_WANTED;
    353 			simple_unlock(&uobj->vmobjlock);
    354 			UVM_UNLOCK_AND_WAIT(udv, &udv_lock, FALSE, "udv_detach",0);
    355 			continue;
    356 		}
    357 
    358 		/*
    359 		 * got it!   nuke it now.
    360 		 */
    361 
    362 		LIST_REMOVE(udv, u_list);
    363 		if (udv->u_flags & UVM_DEVICE_WANTED)
    364 			wakeup(udv);
    365 		FREE(udv, M_TEMP);
    366 		break;	/* DONE! */
    367 
    368 	}	/* while (1) loop */
    369 
    370 	UVMHIST_LOG(maphist," <- done, freed uobj=0x%x", uobj,0,0,0);
    371 	return;
    372 }
    373 
    374 
    375 /*
    376  * udv_flush
    377  *
    378  * flush pages out of a uvm object.   a no-op for devices.
    379  */
    380 
    381 static boolean_t udv_flush(uobj, start, stop, flags)
    382 	struct uvm_object *uobj;
    383 	vaddr_t start, stop;
    384 	int flags;
    385 {
    386 
    387 	return(TRUE);
    388 }
    389 
    390 /*
    391  * udv_fault: non-standard fault routine for device "pages"
    392  *
    393  * => rather than having a "get" function, we have a fault routine
    394  *	since we don't return vm_pages we need full control over the
    395  *	pmap_enter map in
    396  * => all the usual fault data structured are locked by the caller
    397  *	(i.e. maps(read), amap (if any), uobj)
    398  * => on return, we unlock all fault data structures
    399  * => flags: PGO_ALLPAGES: get all of the pages
    400  *	     PGO_LOCKED: fault data structures are locked
    401  *    XXX: currently PGO_LOCKED is always required ... consider removing
    402  *	it as a flag
    403  * => NOTE: vaddr is the VA of pps[0] in ufi->entry, _NOT_ pps[centeridx]
    404  */
    405 
    406 static int
    407 udv_fault(ufi, vaddr, pps, npages, centeridx, fault_type, access_type, flags)
    408 	struct uvm_faultinfo *ufi;
    409 	vaddr_t vaddr;
    410 	vm_page_t *pps;
    411 	int npages, centeridx, flags;
    412 	vm_fault_t fault_type;
    413 	vm_prot_t access_type;
    414 {
    415 	struct vm_map_entry *entry = ufi->entry;
    416 	struct uvm_object *uobj = entry->object.uvm_obj;
    417 	struct uvm_device *udv = (struct uvm_device *)uobj;
    418 	vaddr_t curr_offset, curr_va;
    419 	paddr_t paddr;
    420 	int lcv, retval, mdpgno;
    421 	dev_t device;
    422 	int (*mapfn) __P((dev_t, int, int));
    423 	UVMHIST_FUNC("udv_fault"); UVMHIST_CALLED(maphist);
    424 	UVMHIST_LOG(maphist,"  flags=%d", flags,0,0,0);
    425 
    426 	/*
    427 	 * XXX: !PGO_LOCKED calls are currently not allowed (or used)
    428 	 */
    429 
    430 	if ((flags & PGO_LOCKED) == 0)
    431 		panic("udv_fault: !PGO_LOCKED fault");
    432 
    433 	/*
    434 	 * we do not allow device mappings to be mapped copy-on-write
    435 	 * so we kill any attempt to do so here.
    436 	 */
    437 
    438 	if (UVM_ET_ISCOPYONWRITE(entry)) {
    439 		UVMHIST_LOG(maphist, "<- failed -- COW entry (etype=0x%x)",
    440 		entry->etype, 0,0,0);
    441 		uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj, NULL);
    442 		return(VM_PAGER_ERROR);
    443 	}
    444 
    445 	/*
    446 	 * get device map function.
    447 	 */
    448 	device = udv->u_device;
    449 	mapfn = cdevsw[major(device)].d_mmap;
    450 
    451 	/*
    452 	 * now we must determine the offset in udv to use and the VA to
    453 	 * use for pmap_enter.  note that we always use orig_map's pmap
    454 	 * for pmap_enter (even if we have a submap).   since virtual
    455 	 * addresses in a submap must match the main map, this is ok.
    456 	 */
    457 	/* udv offset = (offset from start of entry) + entry's offset */
    458 	curr_offset = (vaddr - entry->start) + entry->offset;
    459 	/* pmap va = vaddr (virtual address of pps[0]) */
    460 	curr_va = vaddr;
    461 
    462 	/*
    463 	 * loop over the page range entering in as needed
    464 	 */
    465 
    466 	retval = VM_PAGER_OK;
    467 	for (lcv = 0 ; lcv < npages ; lcv++, curr_offset += PAGE_SIZE,
    468 	    curr_va += PAGE_SIZE) {
    469 		if ((flags & PGO_ALLPAGES) == 0 && lcv != centeridx)
    470 			continue;
    471 
    472 		if (pps[lcv] == PGO_DONTCARE)
    473 			continue;
    474 
    475 		mdpgno = (*mapfn)(device, (int)curr_offset, access_type);
    476 		if (mdpgno == -1) {
    477 			retval = VM_PAGER_ERROR;
    478 			break;
    479 		}
    480 		paddr = pmap_phys_address(mdpgno);
    481 		UVMHIST_LOG(maphist,
    482 		    "  MAPPING: device: pm=0x%x, va=0x%x, pa=0x%x, at=%d",
    483 		    ufi->orig_map->pmap, curr_va, (int)paddr, access_type);
    484 		pmap_enter(ufi->orig_map->pmap, curr_va, paddr, access_type, 0);
    485 
    486 	}
    487 
    488 	uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj, NULL);
    489 	return(retval);
    490 }
    491 
    492 /*
    493  * udv_asyncget: start async I/O to bring pages into ram
    494  *
    495  * => caller must lock object(???XXX: see if this is best)
    496  * => a no-op for devices
    497  */
    498 
    499 static int
    500 udv_asyncget(uobj, offset, npages)
    501 	struct uvm_object *uobj;
    502 	vaddr_t offset;
    503 	int npages;
    504 {
    505 
    506 	return(KERN_SUCCESS);
    507 }
    508 
    509 /*
    510  * udv_put: flush page data to backing store.
    511  *
    512  * => this function should never be called (since we never have any
    513  *	page structures to "put")
    514  */
    515 
    516 static int
    517 udv_put(uobj, pps, npages, flags)
    518 	struct uvm_object *uobj;
    519 	struct vm_page **pps;
    520 	int npages, flags;
    521 {
    522 
    523 	panic("udv_put: trying to page out to a device!");
    524 }
    525