Home | History | Annotate | Line # | Download | only in uvm
uvm_bio.c revision 1.10
      1  1.10    chs /*	$NetBSD: uvm_bio.c,v 1.10 2001/03/17 04:01:02 chs Exp $	*/
      2   1.2    chs 
      3   1.2    chs /*
      4   1.2    chs  * Copyright (c) 1998 Chuck Silvers.
      5   1.2    chs  * All rights reserved.
      6   1.2    chs  *
      7   1.2    chs  * Redistribution and use in source and binary forms, with or without
      8   1.2    chs  * modification, are permitted provided that the following conditions
      9   1.2    chs  * are met:
     10   1.2    chs  * 1. Redistributions of source code must retain the above copyright
     11   1.2    chs  *    notice, this list of conditions and the following disclaimer.
     12   1.2    chs  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.2    chs  *    notice, this list of conditions and the following disclaimer in the
     14   1.2    chs  *    documentation and/or other materials provided with the distribution.
     15   1.2    chs  * 3. The name of the author may not be used to endorse or promote products
     16   1.2    chs  *    derived from this software without specific prior written permission.
     17   1.2    chs  *
     18   1.2    chs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19   1.2    chs  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20   1.2    chs  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21   1.2    chs  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22   1.2    chs  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23   1.2    chs  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24   1.2    chs  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25   1.2    chs  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26   1.2    chs  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27   1.2    chs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28   1.2    chs  * SUCH DAMAGE.
     29   1.2    chs  *
     30   1.2    chs  */
     31   1.2    chs 
     32   1.2    chs #include "opt_uvmhist.h"
     33   1.2    chs 
     34   1.2    chs /*
     35   1.2    chs  * uvm_bio.c: buffered i/o vnode mapping cache
     36   1.2    chs  */
     37   1.2    chs 
     38   1.2    chs 
     39   1.2    chs #include <sys/param.h>
     40   1.2    chs #include <sys/systm.h>
     41   1.2    chs #include <sys/malloc.h>
     42   1.2    chs #include <sys/kernel.h>
     43   1.2    chs #include <sys/vnode.h>
     44   1.2    chs 
     45   1.2    chs #include <uvm/uvm.h>
     46   1.2    chs #include <uvm/uvm_page.h>
     47   1.2    chs 
     48   1.2    chs /*
     49   1.2    chs  * global data structures
     50   1.2    chs  */
     51   1.2    chs 
     52   1.2    chs /*
     53   1.2    chs  * local functions
     54   1.2    chs  */
     55   1.2    chs 
     56   1.2    chs static int	ubc_fault __P((struct uvm_faultinfo *, vaddr_t,
     57   1.2    chs 			       vm_page_t *, int, int, vm_fault_t, vm_prot_t,
     58   1.2    chs 			       int));
     59   1.2    chs static struct ubc_map *ubc_find_mapping __P((struct uvm_object *, voff_t));
     60   1.2    chs 
     61   1.2    chs /*
     62   1.2    chs  * local data structues
     63   1.2    chs  */
     64   1.2    chs 
     65   1.2    chs #define UBC_HASH(uobj, offset) (((((u_long)(uobj)) >> 8) + \
     66   1.2    chs 				 (((u_long)(offset)) >> PAGE_SHIFT)) & \
     67   1.2    chs 				ubc_object.hashmask)
     68   1.2    chs 
     69   1.4  enami #define UBC_QUEUE(offset) (&ubc_object.inactive[((offset) / ubc_winsize) & \
     70   1.2    chs 					       (UBC_NQUEUES - 1)])
     71   1.2    chs 
     72   1.2    chs struct ubc_map
     73   1.2    chs {
     74   1.2    chs 	struct uvm_object *	uobj;		/* mapped object */
     75   1.2    chs 	voff_t			offset;		/* offset into uobj */
     76   1.2    chs 	int			refcount;	/* refcount on mapping */
     77   1.2    chs 	voff_t			writeoff;	/* overwrite offset */
     78   1.2    chs 	vsize_t			writelen;	/* overwrite len */
     79   1.2    chs 
     80   1.2    chs 	LIST_ENTRY(ubc_map)	hash;		/* hash table */
     81   1.2    chs 	TAILQ_ENTRY(ubc_map)	inactive;	/* inactive queue */
     82   1.2    chs };
     83   1.2    chs 
     84   1.2    chs static struct ubc_object
     85   1.2    chs {
     86   1.2    chs 	struct uvm_object uobj;		/* glue for uvm_map() */
     87   1.2    chs 	char *kva;			/* where ubc_object is mapped */
     88   1.2    chs 	struct ubc_map *umap;		/* array of ubc_map's */
     89   1.2    chs 
     90   1.2    chs 	LIST_HEAD(, ubc_map) *hash;	/* hashtable for cached ubc_map's */
     91   1.2    chs 	u_long hashmask;		/* mask for hashtable */
     92   1.2    chs 
     93   1.2    chs 	TAILQ_HEAD(ubc_inactive_head, ubc_map) *inactive;
     94   1.2    chs 					/* inactive queues for ubc_map's */
     95   1.2    chs 
     96   1.2    chs } ubc_object;
     97   1.2    chs 
     98   1.2    chs struct uvm_pagerops ubc_pager =
     99   1.2    chs {
    100   1.2    chs 	NULL,		/* init */
    101   1.2    chs 	NULL,		/* reference */
    102   1.2    chs 	NULL,		/* detach */
    103   1.2    chs 	ubc_fault,	/* fault */
    104   1.2    chs 	/* ... rest are NULL */
    105   1.2    chs };
    106   1.2    chs 
    107   1.2    chs int ubc_nwins = UBC_NWINS;
    108   1.2    chs int ubc_winsize = UBC_WINSIZE;
    109   1.2    chs #ifdef PMAP_PREFER
    110   1.2    chs int ubc_nqueues;
    111   1.2    chs boolean_t ubc_release_unmap = FALSE;
    112   1.2    chs #define UBC_NQUEUES ubc_nqueues
    113   1.2    chs #define UBC_RELEASE_UNMAP ubc_release_unmap
    114   1.2    chs #else
    115   1.2    chs #define UBC_NQUEUES 1
    116   1.2    chs #define UBC_RELEASE_UNMAP FALSE
    117   1.2    chs #endif
    118   1.2    chs 
    119   1.2    chs /*
    120   1.2    chs  * ubc_init
    121   1.2    chs  *
    122   1.2    chs  * init pager private data structures.
    123   1.2    chs  */
    124   1.2    chs 
    125   1.2    chs void
    126   1.2    chs ubc_init(void)
    127   1.2    chs {
    128   1.2    chs 	struct ubc_map *umap;
    129   1.2    chs 	vaddr_t va;
    130   1.2    chs 	int i;
    131   1.2    chs 
    132   1.2    chs 	/*
    133   1.2    chs 	 * init ubc_object.
    134   1.2    chs 	 * alloc and init ubc_map's.
    135   1.2    chs 	 * init inactive queues.
    136   1.2    chs 	 * alloc and init hashtable.
    137   1.2    chs 	 * map in ubc_object.
    138   1.2    chs 	 */
    139   1.2    chs 
    140   1.2    chs 	simple_lock_init(&ubc_object.uobj.vmobjlock);
    141   1.2    chs 	ubc_object.uobj.pgops = &ubc_pager;
    142   1.2    chs 	TAILQ_INIT(&ubc_object.uobj.memq);
    143   1.2    chs 	ubc_object.uobj.uo_npages = 0;
    144   1.2    chs 	ubc_object.uobj.uo_refs = UVM_OBJ_KERN;
    145   1.2    chs 
    146   1.2    chs 	ubc_object.umap = malloc(ubc_nwins * sizeof(struct ubc_map),
    147   1.2    chs 				 M_TEMP, M_NOWAIT);
    148   1.7  enami 	if (ubc_object.umap == NULL)
    149   1.7  enami 		panic("ubc_init: failed to allocate ubc_map");
    150   1.2    chs 	bzero(ubc_object.umap, ubc_nwins * sizeof(struct ubc_map));
    151   1.2    chs 
    152   1.2    chs 	va = (vaddr_t)1L;
    153   1.2    chs #ifdef PMAP_PREFER
    154   1.2    chs 	PMAP_PREFER(0, &va);
    155   1.4  enami 	if (va < ubc_winsize) {
    156   1.4  enami 		va = ubc_winsize;
    157   1.2    chs 	}
    158   1.4  enami 	ubc_nqueues = va / ubc_winsize;
    159   1.2    chs 	if (ubc_nqueues != 1) {
    160   1.2    chs 		ubc_release_unmap = TRUE;
    161   1.2    chs 	}
    162   1.2    chs #endif
    163   1.2    chs 	ubc_object.inactive = malloc(UBC_NQUEUES *
    164   1.2    chs 				     sizeof(struct ubc_inactive_head),
    165   1.2    chs 				     M_TEMP, M_NOWAIT);
    166   1.7  enami 	if (ubc_object.inactive == NULL)
    167   1.7  enami 		panic("ubc_init: failed to allocate inactive queue heads");
    168   1.2    chs 	for (i = 0; i < UBC_NQUEUES; i++) {
    169   1.2    chs 		TAILQ_INIT(&ubc_object.inactive[i]);
    170   1.2    chs 	}
    171   1.2    chs 	for (i = 0; i < ubc_nwins; i++) {
    172   1.2    chs 		umap = &ubc_object.umap[i];
    173   1.2    chs 		TAILQ_INSERT_TAIL(&ubc_object.inactive[i & (UBC_NQUEUES - 1)],
    174   1.2    chs 				  umap, inactive);
    175   1.2    chs 	}
    176   1.2    chs 
    177   1.2    chs 	ubc_object.hash = hashinit(ubc_nwins, HASH_LIST, M_TEMP, M_NOWAIT,
    178   1.2    chs 				   &ubc_object.hashmask);
    179   1.2    chs 	for (i = 0; i <= ubc_object.hashmask; i++) {
    180   1.2    chs 		LIST_INIT(&ubc_object.hash[i]);
    181   1.2    chs 	}
    182   1.2    chs 
    183   1.2    chs 	if (uvm_map(kernel_map, (vaddr_t *)&ubc_object.kva,
    184   1.4  enami 		    ubc_nwins * ubc_winsize, &ubc_object.uobj, 0, (vsize_t)va,
    185   1.2    chs 		    UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
    186   1.9    chs 				UVM_ADV_RANDOM, UVM_FLAG_NOMERGE)) != 0) {
    187   1.2    chs 		panic("ubc_init: failed to map ubc_object\n");
    188   1.2    chs 	}
    189   1.2    chs 	UVMHIST_INIT(ubchist, 300);
    190   1.2    chs }
    191   1.2    chs 
    192   1.2    chs 
    193   1.2    chs /*
    194   1.2    chs  * ubc_fault: fault routine for ubc mapping
    195   1.2    chs  */
    196   1.2    chs static int
    197   1.2    chs ubc_fault(ufi, ign1, ign2, ign3, ign4, fault_type, access_type, flags)
    198   1.2    chs 	struct uvm_faultinfo *ufi;
    199   1.2    chs 	vaddr_t ign1;
    200   1.2    chs 	vm_page_t *ign2;
    201   1.2    chs 	int ign3, ign4;
    202   1.2    chs 	vm_fault_t fault_type;
    203   1.2    chs 	vm_prot_t access_type;
    204   1.2    chs 	int flags;
    205   1.2    chs {
    206   1.2    chs 	struct uvm_object *uobj;
    207   1.2    chs 	struct vnode *vp;
    208   1.2    chs 	struct ubc_map *umap;
    209   1.2    chs 	vaddr_t va, eva, ubc_offset, slot_offset;
    210   1.5    chs 	int i, error, rv, npages;
    211   1.4  enami 	struct vm_page *pgs[ubc_winsize >> PAGE_SHIFT], *pg;
    212   1.2    chs 	UVMHIST_FUNC("ubc_fault");  UVMHIST_CALLED(ubchist);
    213   1.2    chs 
    214   1.2    chs 	/*
    215   1.2    chs 	 * no need to try with PGO_LOCKED...
    216   1.2    chs 	 * we don't need to have the map locked since we know that
    217   1.2    chs 	 * no one will mess with it until our reference is released.
    218   1.2    chs 	 */
    219   1.2    chs 	if (flags & PGO_LOCKED) {
    220   1.2    chs #if 0
    221   1.8    chs 		return EBUSY;
    222   1.2    chs #else
    223   1.2    chs 		uvmfault_unlockall(ufi, NULL, &ubc_object.uobj, NULL);
    224   1.2    chs 		flags &= ~PGO_LOCKED;
    225   1.2    chs #endif
    226   1.2    chs 	}
    227   1.2    chs 
    228   1.2    chs 	va = ufi->orig_rvaddr;
    229   1.2    chs 	ubc_offset = va - (vaddr_t)ubc_object.kva;
    230   1.2    chs 
    231   1.2    chs 	UVMHIST_LOG(ubchist, "va 0x%lx ubc_offset 0x%lx at %d",
    232   1.2    chs 		    va, ubc_offset, access_type,0);
    233   1.2    chs 
    234   1.4  enami 	umap = &ubc_object.umap[ubc_offset / ubc_winsize];
    235   1.2    chs 	KASSERT(umap->refcount != 0);
    236   1.4  enami 	slot_offset = trunc_page(ubc_offset & (ubc_winsize - 1));
    237   1.2    chs 
    238   1.2    chs 	/* no umap locking needed since we have a ref on the umap */
    239   1.2    chs 	uobj = umap->uobj;
    240   1.2    chs 	vp = (struct vnode *)uobj;
    241   1.2    chs 	KASSERT(uobj != NULL);
    242   1.2    chs 
    243   1.4  enami 	npages = (ubc_winsize - slot_offset) >> PAGE_SHIFT;
    244   1.2    chs 
    245   1.2    chs 	/*
    246   1.2    chs 	 * XXXUBC
    247   1.2    chs 	 * if npages is more than 1 we have to be sure that
    248   1.2    chs 	 * we set PGO_OVERWRITE correctly.
    249   1.2    chs 	 */
    250   1.2    chs 	if (access_type == VM_PROT_WRITE) {
    251   1.2    chs 		npages = 1;
    252   1.2    chs 	}
    253   1.2    chs 
    254   1.2    chs again:
    255   1.2    chs 	memset(pgs, 0, sizeof (pgs));
    256   1.2    chs 	simple_lock(&uobj->vmobjlock);
    257   1.2    chs 
    258   1.2    chs 	UVMHIST_LOG(ubchist, "slot_offset 0x%x writeoff 0x%x writelen 0x%x "
    259   1.2    chs 		    "u_size 0x%x", slot_offset, umap->writeoff, umap->writelen,
    260   1.2    chs 		    vp->v_uvm.u_size);
    261   1.2    chs 
    262   1.2    chs 	if (access_type & VM_PROT_WRITE &&
    263   1.2    chs 	    slot_offset >= umap->writeoff &&
    264   1.2    chs 	    (slot_offset + PAGE_SIZE <= umap->writeoff + umap->writelen ||
    265   1.2    chs 	     slot_offset + PAGE_SIZE >= vp->v_uvm.u_size - umap->offset)) {
    266   1.2    chs 		UVMHIST_LOG(ubchist, "setting PGO_OVERWRITE", 0,0,0,0);
    267   1.2    chs 		flags |= PGO_OVERWRITE;
    268   1.2    chs 	}
    269   1.2    chs 	else { UVMHIST_LOG(ubchist, "NOT setting PGO_OVERWRITE", 0,0,0,0); }
    270   1.2    chs 	/* XXX be sure to zero any part of the page past EOF */
    271   1.2    chs 
    272   1.2    chs 	/*
    273   1.2    chs 	 * XXX
    274   1.2    chs 	 * ideally we'd like to pre-fault all of the pages we're overwriting.
    275   1.2    chs 	 * so for PGO_OVERWRITE, we should call VOP_GETPAGES() with all of the
    276   1.2    chs 	 * pages in [writeoff, writeoff+writesize] instead of just the one.
    277   1.2    chs 	 */
    278   1.2    chs 
    279   1.2    chs 	UVMHIST_LOG(ubchist, "getpages vp %p offset 0x%x npages %d",
    280   1.2    chs 		    uobj, umap->offset + slot_offset, npages, 0);
    281   1.2    chs 
    282   1.5    chs 	error = VOP_GETPAGES(vp, umap->offset + slot_offset, pgs, &npages, 0,
    283   1.5    chs 	    access_type, 0, flags);
    284   1.5    chs 	UVMHIST_LOG(ubchist, "getpages error %d npages %d", error, npages,0,0);
    285   1.2    chs 
    286   1.5    chs 	if (error == EAGAIN) {
    287   1.2    chs 		tsleep(&lbolt, PVM, "ubc_fault", 0);
    288   1.2    chs 		goto again;
    289   1.2    chs 	}
    290   1.5    chs 	if (error) {
    291  1.10    chs 		return error;
    292   1.5    chs 	}
    293   1.2    chs 	if (npages == 0) {
    294   1.8    chs 		return 0;
    295   1.2    chs 	}
    296   1.2    chs 
    297   1.2    chs 	va = ufi->orig_rvaddr;
    298   1.2    chs 	eva = ufi->orig_rvaddr + (npages << PAGE_SHIFT);
    299   1.2    chs 
    300   1.2    chs 	UVMHIST_LOG(ubchist, "va 0x%lx eva 0x%lx", va, eva, 0,0);
    301   1.2    chs 	simple_lock(&uobj->vmobjlock);
    302   1.2    chs 	for (i = 0; va < eva; i++, va += PAGE_SIZE) {
    303   1.2    chs 		UVMHIST_LOG(ubchist, "pgs[%d] = %p", i, pgs[i],0,0);
    304   1.2    chs 		pg = pgs[i];
    305   1.2    chs 
    306   1.2    chs 		if (pg == NULL || pg == PGO_DONTCARE) {
    307   1.2    chs 			continue;
    308   1.2    chs 		}
    309   1.2    chs 		if (pg->flags & PG_WANTED) {
    310   1.2    chs 			wakeup(pg);
    311   1.2    chs 		}
    312   1.2    chs 		KASSERT((pg->flags & PG_FAKE) == 0);
    313   1.2    chs 		if (pg->flags & PG_RELEASED) {
    314   1.2    chs 			rv = uobj->pgops->pgo_releasepg(pg, NULL);
    315   1.2    chs 			KASSERT(rv);
    316   1.2    chs 			continue;
    317   1.2    chs 		}
    318   1.2    chs 		KASSERT(access_type == VM_PROT_READ ||
    319   1.2    chs 			(pg->flags & PG_RDONLY) == 0);
    320   1.2    chs 
    321   1.2    chs 		uvm_lock_pageq();
    322   1.2    chs 		uvm_pageactivate(pg);
    323   1.2    chs 		uvm_unlock_pageq();
    324   1.2    chs 
    325   1.2    chs 		pmap_enter(ufi->orig_map->pmap, va, VM_PAGE_TO_PHYS(pg),
    326   1.3    chs 			   VM_PROT_READ | VM_PROT_WRITE, access_type);
    327   1.2    chs 
    328   1.2    chs 		pg->flags &= ~(PG_BUSY);
    329   1.2    chs 		UVM_PAGE_OWN(pg, NULL);
    330   1.2    chs 	}
    331   1.2    chs 	simple_unlock(&uobj->vmobjlock);
    332   1.8    chs 	return 0;
    333   1.2    chs }
    334   1.2    chs 
    335   1.2    chs /*
    336   1.2    chs  * local functions
    337   1.2    chs  */
    338   1.2    chs 
    339   1.2    chs static struct ubc_map *
    340   1.2    chs ubc_find_mapping(uobj, offset)
    341   1.2    chs 	struct uvm_object *uobj;
    342   1.2    chs 	voff_t offset;
    343   1.2    chs {
    344   1.2    chs 	struct ubc_map *umap;
    345   1.2    chs 
    346   1.2    chs 	LIST_FOREACH(umap, &ubc_object.hash[UBC_HASH(uobj, offset)], hash) {
    347   1.2    chs 		if (umap->uobj == uobj && umap->offset == offset) {
    348   1.2    chs 			return umap;
    349   1.2    chs 		}
    350   1.2    chs 	}
    351   1.2    chs 	return NULL;
    352   1.2    chs }
    353   1.2    chs 
    354   1.2    chs 
    355   1.2    chs /*
    356   1.2    chs  * ubc interface functions
    357   1.2    chs  */
    358   1.2    chs 
    359   1.2    chs /*
    360   1.2    chs  * ubc_alloc:  allocate a buffer mapping
    361   1.2    chs  */
    362   1.2    chs void *
    363   1.2    chs ubc_alloc(uobj, offset, lenp, flags)
    364   1.2    chs 	struct uvm_object *uobj;
    365   1.2    chs 	voff_t offset;
    366   1.2    chs 	vsize_t *lenp;
    367   1.2    chs 	int flags;
    368   1.2    chs {
    369   1.2    chs 	int s;
    370   1.6    chs 	vaddr_t slot_offset, va;
    371   1.2    chs 	struct ubc_map *umap;
    372   1.6    chs 	voff_t umap_offset;
    373   1.2    chs 	UVMHIST_FUNC("ubc_alloc"); UVMHIST_CALLED(ubchist);
    374   1.2    chs 
    375   1.2    chs 	UVMHIST_LOG(ubchist, "uobj %p offset 0x%lx len 0x%lx filesize 0x%x",
    376   1.2    chs 		    uobj, offset, *lenp, ((struct uvm_vnode *)uobj)->u_size);
    377   1.2    chs 
    378   1.6    chs 	umap_offset = (offset & ~((voff_t)ubc_winsize - 1));
    379   1.4  enami 	slot_offset = (vaddr_t)(offset & ((voff_t)ubc_winsize - 1));
    380   1.4  enami 	*lenp = min(*lenp, ubc_winsize - slot_offset);
    381   1.2    chs 
    382   1.2    chs 	/*
    383   1.2    chs 	 * the vnode is always locked here, so we don't need to add a ref.
    384   1.2    chs 	 */
    385   1.2    chs 
    386   1.2    chs 	s = splbio();
    387   1.2    chs 
    388   1.2    chs again:
    389   1.2    chs 	simple_lock(&ubc_object.uobj.vmobjlock);
    390   1.2    chs 	umap = ubc_find_mapping(uobj, umap_offset);
    391   1.2    chs 	if (umap == NULL) {
    392   1.2    chs 		umap = TAILQ_FIRST(UBC_QUEUE(offset));
    393   1.2    chs 		if (umap == NULL) {
    394   1.2    chs 			simple_unlock(&ubc_object.uobj.vmobjlock);
    395   1.2    chs 			tsleep(&lbolt, PVM, "ubc_alloc", 0);
    396   1.2    chs 			goto again;
    397   1.2    chs 		}
    398   1.2    chs 
    399   1.2    chs 		/*
    400   1.2    chs 		 * remove from old hash (if any),
    401   1.2    chs 		 * add to new hash.
    402   1.2    chs 		 */
    403   1.2    chs 
    404   1.2    chs 		if (umap->uobj != NULL) {
    405   1.2    chs 			LIST_REMOVE(umap, hash);
    406   1.2    chs 		}
    407   1.2    chs 
    408   1.2    chs 		umap->uobj = uobj;
    409   1.2    chs 		umap->offset = umap_offset;
    410   1.2    chs 
    411   1.2    chs 		LIST_INSERT_HEAD(&ubc_object.hash[UBC_HASH(uobj, umap_offset)],
    412   1.2    chs 				 umap, hash);
    413   1.2    chs 
    414   1.2    chs 		va = (vaddr_t)(ubc_object.kva +
    415   1.4  enami 			       (umap - ubc_object.umap) * ubc_winsize);
    416   1.4  enami 		pmap_remove(pmap_kernel(), va, va + ubc_winsize);
    417   1.2    chs 	}
    418   1.2    chs 
    419   1.2    chs 	if (umap->refcount == 0) {
    420   1.2    chs 		TAILQ_REMOVE(UBC_QUEUE(offset), umap, inactive);
    421   1.2    chs 	}
    422   1.2    chs 
    423   1.2    chs #ifdef DIAGNOSTIC
    424   1.2    chs 	if ((flags & UBC_WRITE) &&
    425   1.2    chs 	    (umap->writeoff || umap->writelen)) {
    426   1.2    chs 		panic("ubc_fault: concurrent writes vp %p", uobj);
    427   1.2    chs 	}
    428   1.2    chs #endif
    429   1.2    chs 	if (flags & UBC_WRITE) {
    430   1.2    chs 		umap->writeoff = slot_offset;
    431   1.2    chs 		umap->writelen = *lenp;
    432   1.2    chs 	}
    433   1.2    chs 
    434   1.2    chs 	umap->refcount++;
    435   1.2    chs 	simple_unlock(&ubc_object.uobj.vmobjlock);
    436   1.2    chs 	splx(s);
    437   1.2    chs 	UVMHIST_LOG(ubchist, "umap %p refs %d va %p",
    438   1.2    chs 		    umap, umap->refcount,
    439   1.4  enami 		    ubc_object.kva + (umap - ubc_object.umap) * ubc_winsize,0);
    440   1.2    chs 
    441   1.2    chs 	return ubc_object.kva +
    442   1.4  enami 		(umap - ubc_object.umap) * ubc_winsize + slot_offset;
    443   1.2    chs }
    444   1.2    chs 
    445   1.2    chs 
    446   1.2    chs void
    447   1.2    chs ubc_release(va, wlen)
    448   1.2    chs 	void *va;
    449   1.2    chs 	vsize_t wlen;
    450   1.2    chs {
    451   1.2    chs 	struct ubc_map *umap;
    452   1.2    chs 	struct uvm_object *uobj;
    453   1.2    chs 	int s;
    454   1.2    chs 	UVMHIST_FUNC("ubc_release"); UVMHIST_CALLED(ubchist);
    455   1.2    chs 
    456   1.2    chs 	UVMHIST_LOG(ubchist, "va %p", va,0,0,0);
    457   1.2    chs 
    458   1.2    chs 	s = splbio();
    459   1.2    chs 	simple_lock(&ubc_object.uobj.vmobjlock);
    460   1.2    chs 
    461   1.4  enami 	umap = &ubc_object.umap[((char *)va - ubc_object.kva) / ubc_winsize];
    462   1.2    chs 	uobj = umap->uobj;
    463   1.2    chs 	KASSERT(uobj != NULL);
    464   1.2    chs 
    465   1.2    chs 	umap->writeoff = 0;
    466   1.2    chs 	umap->writelen = 0;
    467   1.2    chs 	umap->refcount--;
    468   1.2    chs 	if (umap->refcount == 0) {
    469   1.2    chs 		if (UBC_RELEASE_UNMAP &&
    470   1.2    chs 		    (((struct vnode *)uobj)->v_flag & VTEXT)) {
    471   1.2    chs 			vaddr_t va;
    472   1.2    chs 
    473   1.2    chs 			/*
    474   1.2    chs 			 * if this file is the executable image of
    475   1.2    chs 			 * some process, that process will likely have
    476   1.2    chs 			 * the file mapped at an alignment other than
    477   1.2    chs 			 * what PMAP_PREFER() would like.  we'd like
    478   1.2    chs 			 * to have process text be able to use the
    479   1.2    chs 			 * cache even if someone is also reading the
    480   1.2    chs 			 * file, so invalidate mappings of such files
    481   1.2    chs 			 * as soon as possible.
    482   1.2    chs 			 */
    483   1.2    chs 
    484   1.2    chs 			va = (vaddr_t)(ubc_object.kva +
    485   1.4  enami 			    (umap - ubc_object.umap) * ubc_winsize);
    486   1.4  enami 			pmap_remove(pmap_kernel(), va, va + ubc_winsize);
    487   1.2    chs 			LIST_REMOVE(umap, hash);
    488   1.2    chs 			umap->uobj = NULL;
    489   1.2    chs 			TAILQ_INSERT_HEAD(UBC_QUEUE(umap->offset), umap,
    490   1.2    chs 			    inactive);
    491   1.2    chs 		} else {
    492   1.2    chs 			TAILQ_INSERT_TAIL(UBC_QUEUE(umap->offset), umap,
    493   1.2    chs 			    inactive);
    494   1.2    chs 		}
    495   1.2    chs 	}
    496   1.2    chs 	UVMHIST_LOG(ubchist, "umap %p refs %d", umap, umap->refcount,0,0);
    497   1.2    chs 	simple_unlock(&ubc_object.uobj.vmobjlock);
    498   1.2    chs 	splx(s);
    499   1.2    chs }
    500   1.2    chs 
    501   1.2    chs 
    502   1.2    chs /*
    503   1.2    chs  * removing a range of mappings from the ubc mapping cache.
    504   1.2    chs  */
    505   1.2    chs 
    506   1.2    chs void
    507   1.2    chs ubc_flush(uobj, start, end)
    508   1.2    chs 	struct uvm_object *uobj;
    509   1.2    chs 	voff_t start, end;
    510   1.2    chs {
    511   1.2    chs 	struct ubc_map *umap;
    512   1.2    chs 	vaddr_t va;
    513   1.2    chs 	int s;
    514   1.2    chs 	UVMHIST_FUNC("ubc_flush");  UVMHIST_CALLED(ubchist);
    515   1.2    chs 
    516   1.2    chs 	UVMHIST_LOG(ubchist, "uobj %p start 0x%lx end 0x%lx",
    517   1.2    chs 		    uobj, start, end,0);
    518   1.2    chs 
    519   1.2    chs 	s = splbio();
    520   1.2    chs 	simple_lock(&ubc_object.uobj.vmobjlock);
    521   1.2    chs 	for (umap = ubc_object.umap;
    522   1.2    chs 	     umap < &ubc_object.umap[ubc_nwins];
    523   1.2    chs 	     umap++) {
    524   1.2    chs 
    525   1.2    chs 		if (umap->uobj != uobj ||
    526   1.2    chs 		    umap->offset < start ||
    527   1.2    chs 		    (umap->offset >= end && end != 0) ||
    528   1.2    chs 		    umap->refcount > 0) {
    529   1.2    chs 			continue;
    530   1.2    chs 		}
    531   1.2    chs 
    532   1.2    chs 		/*
    533   1.2    chs 		 * remove from hash,
    534   1.2    chs 		 * move to head of inactive queue.
    535   1.2    chs 		 */
    536   1.2    chs 
    537   1.2    chs 		va = (vaddr_t)(ubc_object.kva +
    538   1.4  enami 			       (umap - ubc_object.umap) * ubc_winsize);
    539   1.4  enami 		pmap_remove(pmap_kernel(), va, va + ubc_winsize);
    540   1.2    chs 
    541   1.2    chs 		LIST_REMOVE(umap, hash);
    542   1.2    chs 		umap->uobj = NULL;
    543   1.2    chs 		TAILQ_REMOVE(UBC_QUEUE(umap->offset), umap, inactive);
    544   1.2    chs 		TAILQ_INSERT_HEAD(UBC_QUEUE(umap->offset), umap, inactive);
    545   1.2    chs 	}
    546   1.2    chs 	simple_unlock(&ubc_object.uobj.vmobjlock);
    547   1.2    chs 	splx(s);
    548   1.2    chs }
    549