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