Home | History | Annotate | Line # | Download | only in uvm
uvm_device.c revision 1.75
      1 /*	$NetBSD: uvm_device.c,v 1.75 2022/07/06 01:13:30 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Charles D. Cranor and Washington University.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  *
     27  * from: Id: uvm_device.c,v 1.1.2.9 1998/02/06 05:11:47 chs Exp
     28  */
     29 
     30 /*
     31  * uvm_device.c: the device pager.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: uvm_device.c,v 1.75 2022/07/06 01:13:30 riastradh Exp $");
     36 
     37 #include "opt_uvmhist.h"
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/conf.h>
     42 #include <sys/proc.h>
     43 #include <sys/kmem.h>
     44 
     45 #include <uvm/uvm.h>
     46 #include <uvm/uvm_device.h>
     47 #include <uvm/uvm_pmap.h>
     48 
     49 /*
     50  * private global data structure
     51  *
     52  * we keep a list of active device objects in the system.
     53  */
     54 
     55 LIST_HEAD(udv_list_struct, uvm_device);
     56 static struct udv_list_struct udv_list;
     57 static kmutex_t udv_lock __cacheline_aligned;
     58 
     59 /*
     60  * functions
     61  */
     62 
     63 static void	udv_init(void);
     64 static void	udv_reference(struct uvm_object *);
     65 static void	udv_detach(struct uvm_object *);
     66 static int	udv_fault(struct uvm_faultinfo *, vaddr_t,
     67 			  struct vm_page **, int, int, vm_prot_t,
     68 			  int);
     69 
     70 /*
     71  * master pager structure
     72  */
     73 
     74 const struct uvm_pagerops uvm_deviceops = {
     75 	.pgo_init = udv_init,
     76 	.pgo_reference = udv_reference,
     77 	.pgo_detach = udv_detach,
     78 	.pgo_fault = udv_fault,
     79 };
     80 
     81 /*
     82  * the ops!
     83  */
     84 
     85 /*
     86  * udv_init
     87  *
     88  * init pager private data structures.
     89  */
     90 
     91 static void
     92 udv_init(void)
     93 {
     94 	LIST_INIT(&udv_list);
     95 	mutex_init(&udv_lock, MUTEX_DEFAULT, IPL_NONE);
     96 }
     97 
     98 /*
     99  * udv_attach
    100  *
    101  * get a VM object that is associated with a device.   allocate a new
    102  * one if needed.
    103  *
    104  * => caller must _not_ already be holding the lock on the uvm_object.
    105  * => in fact, nothing should be locked so that we can sleep here.
    106  */
    107 
    108 struct uvm_object *
    109 udv_attach(dev_t device, vm_prot_t accessprot,
    110     voff_t off,		/* used only for access check */
    111     vsize_t size	/* used only for access check */)
    112 {
    113 	struct uvm_device *udv, *lcv;
    114 	const struct cdevsw *cdev;
    115 	dev_mmap_t *mapfn;
    116 
    117 	UVMHIST_FUNC(__func__);
    118 	UVMHIST_CALLARGS(maphist, "(device=%#jx)", device,0,0,0);
    119 
    120 	KASSERT(size > 0);
    121 
    122 	/*
    123 	 * before we do anything, ensure this device supports mmap
    124 	 */
    125 
    126 	cdev = cdevsw_lookup(device);
    127 	if (cdev == NULL) {
    128 		return (NULL);
    129 	}
    130 	mapfn = cdev->d_mmap;
    131 	if (mapfn == NULL || mapfn == nommap) {
    132 		return(NULL);
    133 	}
    134 
    135 	/*
    136 	 * Negative offsets on the object are not allowed, unless the
    137 	 * device has affirmatively set D_NEGOFFSAFE.
    138 	 */
    139 	if ((cdev->d_flag & D_NEGOFFSAFE) == 0 && off != UVM_UNKNOWN_OFFSET) {
    140 		if (off < 0)
    141 			return NULL;
    142 		if (size > __type_max(voff_t))
    143 			return NULL;
    144 		if (off > __type_max(voff_t) - size)
    145 			return NULL;
    146 	}
    147 
    148 	/*
    149 	 * Check that the specified range of the device allows the
    150 	 * desired protection.
    151 	 *
    152 	 * XXX assumes VM_PROT_* == PROT_*
    153 	 * XXX clobbers off and size, but nothing else here needs them.
    154 	 */
    155 
    156 	while (size != 0) {
    157 		if (cdev_mmap(device, off, accessprot) == -1) {
    158 			return (NULL);
    159 		}
    160 		off += PAGE_SIZE; size -= PAGE_SIZE;
    161 	}
    162 
    163 	/*
    164 	 * keep looping until we get it
    165 	 */
    166 
    167 	for (;;) {
    168 
    169 		/*
    170 		 * first, attempt to find it on the main list
    171 		 */
    172 
    173 		mutex_enter(&udv_lock);
    174 		LIST_FOREACH(lcv, &udv_list, u_list) {
    175 			if (device == lcv->u_device)
    176 				break;
    177 		}
    178 
    179 		/*
    180 		 * got it on main list.  put a hold on it and unlock udv_lock.
    181 		 */
    182 
    183 		if (lcv) {
    184 
    185 			/*
    186 			 * if someone else has a hold on it, sleep and start
    187 			 * over again.
    188 			 */
    189 
    190 			if (lcv->u_flags & UVM_DEVICE_HOLD) {
    191 				lcv->u_flags |= UVM_DEVICE_WANTED;
    192 				UVM_UNLOCK_AND_WAIT(lcv, &udv_lock, false,
    193 				    "udv_attach",0);
    194 				continue;
    195 			}
    196 
    197 			/* we are now holding it */
    198 			lcv->u_flags |= UVM_DEVICE_HOLD;
    199 			mutex_exit(&udv_lock);
    200 
    201 			/*
    202 			 * bump reference count, unhold, return.
    203 			 */
    204 
    205 			rw_enter(lcv->u_obj.vmobjlock, RW_WRITER);
    206 			lcv->u_obj.uo_refs++;
    207 			rw_exit(lcv->u_obj.vmobjlock);
    208 
    209 			mutex_enter(&udv_lock);
    210 			if (lcv->u_flags & UVM_DEVICE_WANTED)
    211 				wakeup(lcv);
    212 			lcv->u_flags &= ~(UVM_DEVICE_WANTED|UVM_DEVICE_HOLD);
    213 			mutex_exit(&udv_lock);
    214 			return(&lcv->u_obj);
    215 		}
    216 
    217 		/*
    218 		 * Did not find it on main list.  Need to allocate a new one.
    219 		 */
    220 
    221 		mutex_exit(&udv_lock);
    222 
    223 		/* Note: both calls may allocate memory and sleep. */
    224 		udv = kmem_alloc(sizeof(*udv), KM_SLEEP);
    225 		uvm_obj_init(&udv->u_obj, &uvm_deviceops, true, 1);
    226 
    227 		mutex_enter(&udv_lock);
    228 
    229 		/*
    230 		 * now we have to double check to make sure no one added it
    231 		 * to the list while we were sleeping...
    232 		 */
    233 
    234 		LIST_FOREACH(lcv, &udv_list, u_list) {
    235 			if (device == lcv->u_device)
    236 				break;
    237 		}
    238 
    239 		/*
    240 		 * did we lose a race to someone else?
    241 		 * free our memory and retry.
    242 		 */
    243 
    244 		if (lcv) {
    245 			mutex_exit(&udv_lock);
    246 			uvm_obj_destroy(&udv->u_obj, true);
    247 			kmem_free(udv, sizeof(*udv));
    248 			continue;
    249 		}
    250 
    251 		/*
    252 		 * we have it!   init the data structures, add to list
    253 		 * and return.
    254 		 */
    255 
    256 		udv->u_flags = 0;
    257 		udv->u_device = device;
    258 		LIST_INSERT_HEAD(&udv_list, udv, u_list);
    259 		mutex_exit(&udv_lock);
    260 		return(&udv->u_obj);
    261 	}
    262 	/*NOTREACHED*/
    263 }
    264 
    265 /*
    266  * udv_reference
    267  *
    268  * add a reference to a VM object.   Note that the reference count must
    269  * already be one (the passed in reference) so there is no chance of the
    270  * udv being released or locked out here.
    271  *
    272  * => caller must call with object unlocked.
    273  */
    274 
    275 static void
    276 udv_reference(struct uvm_object *uobj)
    277 {
    278 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
    279 
    280 	rw_enter(uobj->vmobjlock, RW_WRITER);
    281 	uobj->uo_refs++;
    282 	UVMHIST_LOG(maphist, "<- done (uobj=%#jx, ref = %jd)",
    283 	    (uintptr_t)uobj, uobj->uo_refs,0,0);
    284 	rw_exit(uobj->vmobjlock);
    285 }
    286 
    287 /*
    288  * udv_detach
    289  *
    290  * remove a reference to a VM object.
    291  *
    292  * => caller must call with object unlocked and map locked.
    293  */
    294 
    295 static void
    296 udv_detach(struct uvm_object *uobj)
    297 {
    298 	struct uvm_device *udv = (struct uvm_device *)uobj;
    299 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
    300 
    301 	/*
    302 	 * loop until done
    303 	 */
    304 again:
    305 	rw_enter(uobj->vmobjlock, RW_WRITER);
    306 	if (uobj->uo_refs > 1) {
    307 		uobj->uo_refs--;
    308 		rw_exit(uobj->vmobjlock);
    309 		UVMHIST_LOG(maphist," <- done, uobj=%#jx, ref=%jd",
    310 		    (uintptr_t)uobj,uobj->uo_refs,0,0);
    311 		return;
    312 	}
    313 
    314 	/*
    315 	 * is it being held?   if so, wait until others are done.
    316 	 */
    317 
    318 	mutex_enter(&udv_lock);
    319 	if (udv->u_flags & UVM_DEVICE_HOLD) {
    320 		udv->u_flags |= UVM_DEVICE_WANTED;
    321 		rw_exit(uobj->vmobjlock);
    322 		UVM_UNLOCK_AND_WAIT(udv, &udv_lock, false, "udv_detach",0);
    323 		goto again;
    324 	}
    325 
    326 	/*
    327 	 * got it!   nuke it now.
    328 	 */
    329 
    330 	LIST_REMOVE(udv, u_list);
    331 	if (udv->u_flags & UVM_DEVICE_WANTED)
    332 		wakeup(udv);
    333 	mutex_exit(&udv_lock);
    334 	rw_exit(uobj->vmobjlock);
    335 
    336 	uvm_obj_destroy(uobj, true);
    337 	kmem_free(udv, sizeof(*udv));
    338 	UVMHIST_LOG(maphist," <- done, freed uobj=%#jx", (uintptr_t)uobj,
    339 	    0, 0, 0);
    340 }
    341 
    342 /*
    343  * udv_fault: non-standard fault routine for device "pages"
    344  *
    345  * => rather than having a "get" function, we have a fault routine
    346  *	since we don't return vm_pages we need full control over the
    347  *	pmap_enter map in
    348  * => all the usual fault data structured are locked by the caller
    349  *	(i.e. maps(read), amap (if any), uobj)
    350  * => on return, we unlock all fault data structures
    351  * => flags: PGO_ALLPAGES: get all of the pages
    352  *	     PGO_LOCKED: fault data structures are locked
    353  *    XXX: currently PGO_LOCKED is always required ... consider removing
    354  *	it as a flag
    355  * => NOTE: vaddr is the VA of pps[0] in ufi->entry, _NOT_ pps[centeridx]
    356  */
    357 
    358 static int
    359 udv_fault(struct uvm_faultinfo *ufi, vaddr_t vaddr, struct vm_page **pps,
    360     int npages, int centeridx, vm_prot_t access_type,
    361     int flags)
    362 {
    363 	struct vm_map_entry *entry = ufi->entry;
    364 	struct uvm_object *uobj = entry->object.uvm_obj;
    365 	struct uvm_device *udv = (struct uvm_device *)uobj;
    366 	vaddr_t curr_va;
    367 	off_t curr_offset;
    368 	paddr_t paddr, mdpgno;
    369 	u_int mmapflags;
    370 	int lcv, retval;
    371 	dev_t device;
    372 	vm_prot_t mapprot;
    373 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
    374 	UVMHIST_LOG(maphist,"  flags=%#jx", flags,0,0,0);
    375 
    376 	/*
    377 	 * we do not allow device mappings to be mapped copy-on-write
    378 	 * so we kill any attempt to do so here.
    379 	 */
    380 
    381 	if (UVM_ET_ISCOPYONWRITE(entry)) {
    382 		UVMHIST_LOG(maphist, "<- failed -- COW entry (etype=%#jx)",
    383 		    entry->etype, 0,0,0);
    384 		uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj);
    385 		return(EIO);
    386 	}
    387 
    388 	/*
    389 	 * get device map function.
    390 	 */
    391 
    392 	device = udv->u_device;
    393 	if (cdevsw_lookup(device) == NULL) {
    394 		/* XXX This should not happen */
    395 		uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj);
    396 		return (EIO);
    397 	}
    398 
    399 	/*
    400 	 * now we must determine the offset in udv to use and the VA to
    401 	 * use for pmap_enter.  note that we always use orig_map's pmap
    402 	 * for pmap_enter (even if we have a submap).   since virtual
    403 	 * addresses in a submap must match the main map, this is ok.
    404 	 */
    405 
    406 	/* udv offset = (offset from start of entry) + entry's offset */
    407 	curr_offset = entry->offset + (vaddr - entry->start);
    408 	/* pmap va = vaddr (virtual address of pps[0]) */
    409 	curr_va = vaddr;
    410 
    411 	/*
    412 	 * loop over the page range entering in as needed
    413 	 */
    414 
    415 	retval = 0;
    416 	for (lcv = 0 ; lcv < npages ; lcv++, curr_offset += PAGE_SIZE,
    417 	    curr_va += PAGE_SIZE) {
    418 		if ((flags & PGO_ALLPAGES) == 0 && lcv != centeridx)
    419 			continue;
    420 
    421 		if (pps[lcv] == PGO_DONTCARE)
    422 			continue;
    423 
    424 		mdpgno = cdev_mmap(device, curr_offset, access_type);
    425 		if (mdpgno == -1) {
    426 			retval = EIO;
    427 			break;
    428 		}
    429 		paddr = pmap_phys_address(mdpgno);
    430 		mmapflags = pmap_mmap_flags(mdpgno);
    431 		mapprot = ufi->entry->protection;
    432 		UVMHIST_LOG(maphist,
    433 		    "  MAPPING: device: pm=%#jx, va=%#jx, pa=%#jx, at=%jd",
    434 		    (uintptr_t)ufi->orig_map->pmap, curr_va, paddr, mapprot);
    435 		if (pmap_enter(ufi->orig_map->pmap, curr_va, paddr, mapprot,
    436 		    PMAP_CANFAIL | mapprot | mmapflags) != 0) {
    437 			/*
    438 			 * pmap_enter() didn't have the resource to
    439 			 * enter this mapping.  Unlock everything,
    440 			 * wait for the pagedaemon to free up some
    441 			 * pages, and then tell uvm_fault() to start
    442 			 * the fault again.
    443 			 *
    444 			 * XXX Needs some rethinking for the PGO_ALLPAGES
    445 			 * XXX case.
    446 			 */
    447 			pmap_update(ufi->orig_map->pmap);	/* sync what we have so far */
    448 			uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap,
    449 			    uobj);
    450 			return ENOMEM;
    451 		}
    452 	}
    453 
    454 	pmap_update(ufi->orig_map->pmap);
    455 	uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj);
    456 	return (retval);
    457 }
    458