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