Home | History | Annotate | Line # | Download | only in uvm
uvm_device.c revision 1.9
      1 /*	$NetBSD: uvm_device.c,v 1.9 1998/08/13 02:11:00 eeh 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));
     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_attach,
     97 	udv_reference,
     98 	udv_detach,
     99 	udv_fault,
    100 	udv_flush,
    101 	NULL,		/* no get function since we have udv_fault */
    102 	udv_asyncget,
    103 	udv_put,
    104 	NULL,		/* no cluster function */
    105 	NULL,		/* no put cluster function */
    106 	NULL,		/* no share protect.   no share maps for us */
    107 	NULL,		/* no AIO-DONE function since no async i/o */
    108 	NULL,		/* no releasepg function since no normal pages */
    109 };
    110 
    111 /*
    112  * the ops!
    113  */
    114 
    115 /*
    116  * udv_init
    117  *
    118  * init pager private data structures.
    119  */
    120 
    121 void
    122 udv_init()
    123 {
    124 
    125 	LIST_INIT(&udv_list);
    126 	simple_lock_init(&udv_lock);
    127 }
    128 
    129 /*
    130  * udv_attach
    131  *
    132  * get a VM object that is associated with a device.   allocate a new
    133  * one if needed.
    134  *
    135  * => caller must _not_ already be holding the lock on the uvm_object.
    136  * => in fact, nothing should be locked so that we can sleep here.
    137  */
    138 struct uvm_object *
    139 udv_attach(arg, accessprot)
    140 	void *arg;
    141 	vm_prot_t accessprot;
    142 {
    143 	dev_t device = *((dev_t *) arg);
    144 	struct uvm_device *udv, *lcv;
    145 	int (*mapfn) __P((dev_t, int, int));
    146 	UVMHIST_FUNC("udv_attach"); UVMHIST_CALLED(maphist);
    147 
    148 	UVMHIST_LOG(maphist, "(device=0x%x)", device,0,0,0);
    149 
    150 	/*
    151 	 * before we do anything, ensure this device supports mmap
    152 	 */
    153 
    154 	mapfn = cdevsw[major(device)].d_mmap;
    155 	if (mapfn == NULL ||
    156 			mapfn == (int (*) __P((dev_t, int, int))) enodev ||
    157 			mapfn == (int (*) __P((dev_t, int, int))) nullop)
    158 		return(NULL);
    159 
    160 	/*
    161 	 * keep looping until we get it
    162 	 */
    163 
    164 	while (1) {
    165 
    166 		/*
    167 		 * first, attempt to find it on the main list
    168 		 */
    169 
    170 		simple_lock(&udv_lock);
    171 		for (lcv = udv_list.lh_first ; lcv != NULL ; lcv = lcv->u_list.le_next) {
    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, M_WAITOK);
    221 		simple_lock(&udv_lock);
    222 
    223 		/*
    224 		 * now we have to double check to make sure no one added it
    225 		 * to the list while we were sleeping...
    226 		 */
    227 
    228 		for (lcv = udv_list.lh_first ; lcv != NULL ;
    229 		    lcv = lcv->u_list.le_next) {
    230 			if (device == lcv->u_device)
    231 				break;
    232 		}
    233 
    234 		/*
    235 		 * did we lose a race to someone else?   free our memory and retry.
    236 		 */
    237 
    238 		if (lcv) {
    239 			simple_unlock(&udv_lock);
    240 			FREE(udv, M_TEMP);
    241 			continue;
    242 		}
    243 
    244 		/*
    245 		 * we have it!   init the data structures, add to list
    246 		 * and return.
    247 		 */
    248 
    249 		simple_lock_init(&udv->u_obj.vmobjlock);
    250 		udv->u_obj.pgops = &uvm_deviceops;
    251 		TAILQ_INIT(&udv->u_obj.memq);	/* not used, but be safe */
    252 		udv->u_obj.uo_npages = 0;
    253 		udv->u_obj.uo_refs = 1;
    254 		udv->u_flags = 0;
    255 		udv->u_device = device;
    256 		LIST_INSERT_HEAD(&udv_list, udv, u_list);
    257 		simple_unlock(&udv_lock);
    258 
    259 		return(&udv->u_obj);
    260 
    261 	}  /* while(1) loop */
    262 
    263 	/*NOTREACHED*/
    264 }
    265 
    266 /*
    267  * udv_reference
    268  *
    269  * add a reference to a VM object.   Note that the reference count must
    270  * already be one (the passed in reference) so there is no chance of the
    271  * udv being released or locked out here.
    272  *
    273  * => caller must call with object unlocked.
    274  */
    275 
    276 static void
    277 udv_reference(uobj)
    278 	struct uvm_object *uobj;
    279 {
    280 	UVMHIST_FUNC("udv_reference"); UVMHIST_CALLED(maphist);
    281 
    282 	simple_lock(&uobj->vmobjlock);
    283 	uobj->uo_refs++;
    284 	UVMHIST_LOG(maphist, "<- done (uobj=0x%x, ref = %d)",
    285 	uobj, uobj->uo_refs,0,0);
    286 	simple_unlock(&uobj->vmobjlock);
    287 }
    288 
    289 /*
    290  * udv_detach
    291  *
    292  * remove a reference to a VM object.
    293  *
    294  * => caller must call with object unlocked and map locked.
    295  */
    296 
    297 static void
    298 udv_detach(uobj)
    299 	struct uvm_object *uobj;
    300 {
    301 	struct uvm_device *udv = (struct uvm_device *) uobj;
    302 	UVMHIST_FUNC("udv_detach"); UVMHIST_CALLED(maphist);
    303 
    304 	/*
    305 	 * loop until done
    306 	 */
    307 
    308 	while (1) {
    309 		simple_lock(&uobj->vmobjlock);
    310 
    311 		if (uobj->uo_refs > 1) {
    312 			uobj->uo_refs--;			/* drop ref! */
    313 			simple_unlock(&uobj->vmobjlock);
    314 			UVMHIST_LOG(maphist," <- done, uobj=0x%x, ref=%d",
    315 				  uobj,uobj->uo_refs,0,0);
    316 			return;
    317 		}
    318 
    319 #ifdef DIAGNOSTIC
    320 		if (uobj->uo_npages || uobj->memq.tqh_first)
    321 			panic("udv_detach: pages in a device object?");
    322 #endif
    323 
    324 		/*
    325 		 * now lock udv_lock
    326 		 */
    327 		simple_lock(&udv_lock);
    328 
    329 		/*
    330 		 * is it being held?   if so, wait until others are done.
    331 		 */
    332 		if (udv->u_flags & UVM_DEVICE_HOLD) {
    333 
    334 			/*
    335 			 * want it
    336 			 */
    337 			udv->u_flags |= UVM_DEVICE_WANTED;
    338 			simple_unlock(&uobj->vmobjlock);
    339 			UVM_UNLOCK_AND_WAIT(udv, &udv_lock, FALSE, "udv_detach",0);
    340 			continue;
    341 		}
    342 
    343 		/*
    344 		 * got it!   nuke it now.
    345 		 */
    346 
    347 		LIST_REMOVE(udv, u_list);
    348 		if (udv->u_flags & UVM_DEVICE_WANTED)
    349 			wakeup(udv);
    350 		FREE(udv, M_TEMP);
    351 		break;	/* DONE! */
    352 
    353 	}	/* while (1) loop */
    354 
    355 	UVMHIST_LOG(maphist," <- done, freed uobj=0x%x", uobj,0,0,0);
    356 	return;
    357 }
    358 
    359 
    360 /*
    361  * udv_flush
    362  *
    363  * flush pages out of a uvm object.   a no-op for devices.
    364  */
    365 
    366 static boolean_t udv_flush(uobj, start, stop, flags)
    367 	struct uvm_object *uobj;
    368 	vaddr_t start, stop;
    369 	int flags;
    370 {
    371 
    372 	return(TRUE);
    373 }
    374 
    375 /*
    376  * udv_fault: non-standard fault routine for device "pages"
    377  *
    378  * => rather than having a "get" function, we have a fault routine
    379  *	since we don't return vm_pages we need full control over the
    380  *	pmap_enter map in
    381  * => all the usual fault data structured are locked by the caller
    382  *	(i.e. maps(read), amap (if any), uobj)
    383  * => on return, we unlock all fault data structures
    384  * => flags: PGO_ALLPAGES: get all of the pages
    385  *	     PGO_LOCKED: fault data structures are locked
    386  *    XXX: currently PGO_LOCKED is always required ... consider removing
    387  *	it as a flag
    388  * => NOTE: vaddr is the VA of pps[0] in ufi->entry, _NOT_ pps[centeridx]
    389  */
    390 
    391 static int
    392 udv_fault(ufi, vaddr, pps, npages, centeridx, fault_type, access_type, flags)
    393 	struct uvm_faultinfo *ufi;
    394 	vaddr_t vaddr;
    395 	vm_page_t *pps;
    396 	int npages, centeridx, flags;
    397 	vm_fault_t fault_type;
    398 	vm_prot_t access_type;
    399 {
    400 	struct vm_map_entry *entry = ufi->entry;
    401 	struct uvm_object *uobj = entry->object.uvm_obj;
    402 	struct uvm_device *udv = (struct uvm_device *)uobj;
    403 	vaddr_t curr_offset, curr_va;
    404 	paddr_t paddr;
    405 	int lcv, retval;
    406 	dev_t device;
    407 	int (*mapfn) __P((dev_t, int, int));
    408 	UVMHIST_FUNC("udv_fault"); UVMHIST_CALLED(maphist);
    409 	UVMHIST_LOG(maphist,"  flags=%d", flags,0,0,0);
    410 
    411 	/*
    412 	 * XXX: !PGO_LOCKED calls are currently not allowed (or used)
    413 	 */
    414 
    415 	if ((flags & PGO_LOCKED) == 0)
    416 		panic("udv_fault: !PGO_LOCKED fault");
    417 
    418 	/*
    419 	 * we do not allow device mappings to be mapped copy-on-write
    420 	 * so we kill any attempt to do so here.
    421 	 */
    422 
    423 	if (UVM_ET_ISCOPYONWRITE(entry)) {
    424 		UVMHIST_LOG(maphist, "<- failed -- COW entry (etype=0x%x)",
    425 		entry->etype, 0,0,0);
    426 		uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj, NULL);
    427 		return(VM_PAGER_ERROR);
    428 	}
    429 
    430 	/*
    431 	 * get device map function.
    432 	 */
    433 	device = udv->u_device;
    434 	mapfn = cdevsw[major(device)].d_mmap;
    435 
    436 	/*
    437 	 * now we must determine the offset in udv to use and the VA to use
    438 	 * for pmap_enter.  note that we always pmap_enter() in the
    439 	 * ufi->orig_map's pmap, but that our ufi->entry may be from some
    440 	 * other map (in the submap/sharemap case).  so we must convert the
    441 	 * VA from ufi->map to ufi->orig_map (note that in many cases these
    442 	 * maps are the same).   note that ufi->orig_rvaddr and ufi->rvaddr
    443 	 * refer to the same physical page.
    444 	 */
    445 	/* udv offset = (offset from start of entry) + entry's offset */
    446 	curr_offset = (vaddr - entry->start) + entry->offset;
    447 	/* pmap va = orig_va + (offset of vaddr from translated va) */
    448 	curr_va = ufi->orig_rvaddr + (vaddr - ufi->rvaddr);
    449 
    450 	/*
    451 	 * loop over the page range entering in as needed
    452 	 */
    453 
    454 	retval = VM_PAGER_OK;
    455 	for (lcv = 0 ; lcv < npages ; lcv++, curr_offset += PAGE_SIZE,
    456 	    curr_va += PAGE_SIZE) {
    457 		if ((flags & PGO_ALLPAGES) == 0 && lcv != centeridx)
    458 			continue;
    459 
    460 		if (pps[lcv] == PGO_DONTCARE)
    461 			continue;
    462 
    463 		paddr = pmap_phys_address((*mapfn)(device, (int)curr_offset,
    464 		    access_type));
    465 		if (paddr == -1) {
    466 			retval = VM_PAGER_ERROR;
    467 			break;
    468 		}
    469 		UVMHIST_LOG(maphist,
    470 		    "  MAPPING: device: pm=0x%x, va=0x%x, pa=0x%x, at=%d",
    471 		    ufi->orig_map->pmap, curr_va, (int)paddr, access_type);
    472 		pmap_enter(ufi->orig_map->pmap, curr_va, paddr, access_type, 0);
    473 
    474 	}
    475 
    476 	uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj, NULL);
    477 	return(retval);
    478 }
    479 
    480 /*
    481  * udv_asyncget: start async I/O to bring pages into ram
    482  *
    483  * => caller must lock object(???XXX: see if this is best)
    484  * => a no-op for devices
    485  */
    486 
    487 static int
    488 udv_asyncget(uobj, offset, npages)
    489 	struct uvm_object *uobj;
    490 	vaddr_t offset;
    491 	int npages;
    492 {
    493 
    494 	return(KERN_SUCCESS);
    495 }
    496 
    497 /*
    498  * udv_put: flush page data to backing store.
    499  *
    500  * => this function should never be called (since we never have any
    501  *	page structures to "put")
    502  */
    503 
    504 static int
    505 udv_put(uobj, pps, npages, flags)
    506 	struct uvm_object *uobj;
    507 	struct vm_page **pps;
    508 	int npages, flags;
    509 {
    510 
    511 	panic("udv_put: trying to page out to a device!");
    512 }
    513