Home | History | Annotate | Line # | Download | only in uvm
uvm_bio.c revision 1.18
      1  1.18      chs /*	$NetBSD: uvm_bio.c,v 1.18 2001/09/15 20:36:45 chs Exp $	*/
      2   1.2      chs 
      3  1.13      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 
     47   1.2      chs /*
     48   1.2      chs  * global data structures
     49   1.2      chs  */
     50   1.2      chs 
     51   1.2      chs /*
     52   1.2      chs  * local functions
     53   1.2      chs  */
     54   1.2      chs 
     55  1.18      chs int	ubc_fault __P((struct uvm_faultinfo *, vaddr_t, struct vm_page **, int,
     56  1.18      chs     int, vm_fault_t, vm_prot_t, int));
     57  1.18      chs struct ubc_map *ubc_find_mapping __P((struct uvm_object *, voff_t));
     58   1.2      chs 
     59   1.2      chs /*
     60   1.2      chs  * local data structues
     61   1.2      chs  */
     62   1.2      chs 
     63  1.18      chs #define UBC_HASH(uobj, offset) 						\
     64  1.18      chs 	(((((u_long)(uobj)) >> 8) + (((u_long)(offset)) >> PAGE_SHIFT)) & \
     65   1.2      chs 				ubc_object.hashmask)
     66   1.2      chs 
     67  1.18      chs #define UBC_QUEUE(offset)						\
     68  1.18      chs 	(&ubc_object.inactive[(((u_long)(offset)) >> ubc_winshift) &	\
     69  1.18      chs 			     (UBC_NQUEUES - 1)])
     70  1.18      chs 
     71  1.18      chs #define UBC_UMAP_ADDR(u)						\
     72  1.18      chs 	(vaddr_t)(ubc_object.kva + (((u) - ubc_object.umap) << ubc_winshift))
     73  1.18      chs 
     74  1.18      chs 
     75  1.18      chs #define UMAP_PAGES_LOCKED	0x0001
     76  1.18      chs #define UMAP_MAPPING_CACHED	0x0002
     77   1.2      chs 
     78   1.2      chs struct ubc_map
     79   1.2      chs {
     80   1.2      chs 	struct uvm_object *	uobj;		/* mapped object */
     81   1.2      chs 	voff_t			offset;		/* offset into uobj */
     82   1.2      chs 	voff_t			writeoff;	/* overwrite offset */
     83   1.2      chs 	vsize_t			writelen;	/* overwrite len */
     84  1.18      chs 	int			refcount;	/* refcount on mapping */
     85  1.18      chs 	int			flags;		/* extra state */
     86   1.2      chs 
     87   1.2      chs 	LIST_ENTRY(ubc_map)	hash;		/* hash table */
     88   1.2      chs 	TAILQ_ENTRY(ubc_map)	inactive;	/* inactive queue */
     89   1.2      chs };
     90   1.2      chs 
     91   1.2      chs static struct ubc_object
     92   1.2      chs {
     93   1.2      chs 	struct uvm_object uobj;		/* glue for uvm_map() */
     94   1.2      chs 	char *kva;			/* where ubc_object is mapped */
     95   1.2      chs 	struct ubc_map *umap;		/* array of ubc_map's */
     96   1.2      chs 
     97   1.2      chs 	LIST_HEAD(, ubc_map) *hash;	/* hashtable for cached ubc_map's */
     98   1.2      chs 	u_long hashmask;		/* mask for hashtable */
     99   1.2      chs 
    100   1.2      chs 	TAILQ_HEAD(ubc_inactive_head, ubc_map) *inactive;
    101   1.2      chs 					/* inactive queues for ubc_map's */
    102   1.2      chs 
    103   1.2      chs } ubc_object;
    104   1.2      chs 
    105   1.2      chs struct uvm_pagerops ubc_pager =
    106   1.2      chs {
    107   1.2      chs 	NULL,		/* init */
    108   1.2      chs 	NULL,		/* reference */
    109   1.2      chs 	NULL,		/* detach */
    110   1.2      chs 	ubc_fault,	/* fault */
    111   1.2      chs 	/* ... rest are NULL */
    112   1.2      chs };
    113   1.2      chs 
    114   1.2      chs int ubc_nwins = UBC_NWINS;
    115  1.11      chs int ubc_winshift = UBC_WINSHIFT;
    116  1.11      chs int ubc_winsize;
    117   1.2      chs #ifdef PMAP_PREFER
    118   1.2      chs int ubc_nqueues;
    119   1.2      chs boolean_t ubc_release_unmap = FALSE;
    120   1.2      chs #define UBC_NQUEUES ubc_nqueues
    121   1.2      chs #define UBC_RELEASE_UNMAP ubc_release_unmap
    122   1.2      chs #else
    123   1.2      chs #define UBC_NQUEUES 1
    124   1.2      chs #define UBC_RELEASE_UNMAP FALSE
    125   1.2      chs #endif
    126   1.2      chs 
    127   1.2      chs /*
    128   1.2      chs  * ubc_init
    129   1.2      chs  *
    130   1.2      chs  * init pager private data structures.
    131   1.2      chs  */
    132   1.2      chs 
    133   1.2      chs void
    134   1.2      chs ubc_init(void)
    135   1.2      chs {
    136   1.2      chs 	struct ubc_map *umap;
    137   1.2      chs 	vaddr_t va;
    138   1.2      chs 	int i;
    139  1.15   simonb 
    140  1.15   simonb 	/*
    141  1.15   simonb 	 * Make sure ubc_winshift is sane.
    142  1.15   simonb 	 */
    143  1.15   simonb 	if (ubc_winshift < PAGE_SHIFT)
    144  1.15   simonb 		ubc_winshift = PAGE_SHIFT;
    145   1.2      chs 
    146   1.2      chs 	/*
    147   1.2      chs 	 * init ubc_object.
    148   1.2      chs 	 * alloc and init ubc_map's.
    149   1.2      chs 	 * init inactive queues.
    150   1.2      chs 	 * alloc and init hashtable.
    151   1.2      chs 	 * map in ubc_object.
    152   1.2      chs 	 */
    153   1.2      chs 
    154   1.2      chs 	simple_lock_init(&ubc_object.uobj.vmobjlock);
    155   1.2      chs 	ubc_object.uobj.pgops = &ubc_pager;
    156   1.2      chs 	TAILQ_INIT(&ubc_object.uobj.memq);
    157   1.2      chs 	ubc_object.uobj.uo_npages = 0;
    158   1.2      chs 	ubc_object.uobj.uo_refs = UVM_OBJ_KERN;
    159   1.2      chs 
    160   1.2      chs 	ubc_object.umap = malloc(ubc_nwins * sizeof(struct ubc_map),
    161   1.2      chs 				 M_TEMP, M_NOWAIT);
    162   1.7    enami 	if (ubc_object.umap == NULL)
    163   1.7    enami 		panic("ubc_init: failed to allocate ubc_map");
    164  1.16  thorpej 	memset(ubc_object.umap, 0, ubc_nwins * sizeof(struct ubc_map));
    165   1.2      chs 
    166  1.18      chs 	if (ubc_winshift < PAGE_SHIFT) {
    167  1.18      chs 		ubc_winshift = PAGE_SHIFT;
    168  1.18      chs 	}
    169   1.2      chs 	va = (vaddr_t)1L;
    170   1.2      chs #ifdef PMAP_PREFER
    171   1.2      chs 	PMAP_PREFER(0, &va);
    172  1.11      chs 	ubc_nqueues = va >> ubc_winshift;
    173  1.11      chs 	if (ubc_nqueues == 0) {
    174  1.11      chs 		ubc_nqueues = 1;
    175   1.2      chs 	}
    176   1.2      chs 	if (ubc_nqueues != 1) {
    177   1.2      chs 		ubc_release_unmap = TRUE;
    178   1.2      chs 	}
    179   1.2      chs #endif
    180  1.11      chs 	ubc_winsize = 1 << ubc_winshift;
    181   1.2      chs 	ubc_object.inactive = malloc(UBC_NQUEUES *
    182  1.18      chs 	    sizeof(struct ubc_inactive_head), M_TEMP, M_NOWAIT);
    183   1.7    enami 	if (ubc_object.inactive == NULL)
    184   1.7    enami 		panic("ubc_init: failed to allocate inactive queue heads");
    185   1.2      chs 	for (i = 0; i < UBC_NQUEUES; i++) {
    186   1.2      chs 		TAILQ_INIT(&ubc_object.inactive[i]);
    187   1.2      chs 	}
    188   1.2      chs 	for (i = 0; i < ubc_nwins; i++) {
    189   1.2      chs 		umap = &ubc_object.umap[i];
    190   1.2      chs 		TAILQ_INSERT_TAIL(&ubc_object.inactive[i & (UBC_NQUEUES - 1)],
    191   1.2      chs 				  umap, inactive);
    192   1.2      chs 	}
    193   1.2      chs 
    194   1.2      chs 	ubc_object.hash = hashinit(ubc_nwins, HASH_LIST, M_TEMP, M_NOWAIT,
    195   1.2      chs 				   &ubc_object.hashmask);
    196   1.2      chs 	for (i = 0; i <= ubc_object.hashmask; i++) {
    197   1.2      chs 		LIST_INIT(&ubc_object.hash[i]);
    198   1.2      chs 	}
    199   1.2      chs 
    200   1.2      chs 	if (uvm_map(kernel_map, (vaddr_t *)&ubc_object.kva,
    201  1.11      chs 		    ubc_nwins << ubc_winshift, &ubc_object.uobj, 0, (vsize_t)va,
    202   1.2      chs 		    UVM_MAPFLAG(UVM_PROT_ALL, UVM_PROT_ALL, UVM_INH_NONE,
    203   1.9      chs 				UVM_ADV_RANDOM, UVM_FLAG_NOMERGE)) != 0) {
    204   1.2      chs 		panic("ubc_init: failed to map ubc_object\n");
    205   1.2      chs 	}
    206   1.2      chs 	UVMHIST_INIT(ubchist, 300);
    207   1.2      chs }
    208   1.2      chs 
    209   1.2      chs /*
    210   1.2      chs  * ubc_fault: fault routine for ubc mapping
    211   1.2      chs  */
    212  1.18      chs 
    213  1.11      chs int
    214   1.2      chs ubc_fault(ufi, ign1, ign2, ign3, ign4, fault_type, access_type, flags)
    215   1.2      chs 	struct uvm_faultinfo *ufi;
    216   1.2      chs 	vaddr_t ign1;
    217  1.14      chs 	struct vm_page **ign2;
    218   1.2      chs 	int ign3, ign4;
    219   1.2      chs 	vm_fault_t fault_type;
    220   1.2      chs 	vm_prot_t access_type;
    221   1.2      chs 	int flags;
    222   1.2      chs {
    223   1.2      chs 	struct uvm_object *uobj;
    224   1.2      chs 	struct vnode *vp;
    225   1.2      chs 	struct ubc_map *umap;
    226   1.2      chs 	vaddr_t va, eva, ubc_offset, slot_offset;
    227  1.18      chs 	int i, error, npages;
    228  1.18      chs 	struct vm_page *pgs[ubc_winsize >> PAGE_SHIFT], *pg;
    229   1.2      chs 	UVMHIST_FUNC("ubc_fault");  UVMHIST_CALLED(ubchist);
    230   1.2      chs 
    231   1.2      chs 	/*
    232   1.2      chs 	 * no need to try with PGO_LOCKED...
    233   1.2      chs 	 * we don't need to have the map locked since we know that
    234   1.2      chs 	 * no one will mess with it until our reference is released.
    235   1.2      chs 	 */
    236  1.18      chs 
    237   1.2      chs 	if (flags & PGO_LOCKED) {
    238   1.2      chs 		uvmfault_unlockall(ufi, NULL, &ubc_object.uobj, NULL);
    239   1.2      chs 		flags &= ~PGO_LOCKED;
    240   1.2      chs 	}
    241   1.2      chs 
    242   1.2      chs 	va = ufi->orig_rvaddr;
    243   1.2      chs 	ubc_offset = va - (vaddr_t)ubc_object.kva;
    244   1.2      chs 
    245   1.2      chs 	UVMHIST_LOG(ubchist, "va 0x%lx ubc_offset 0x%lx at %d",
    246  1.18      chs 	    va, ubc_offset, access_type,0);
    247   1.2      chs 
    248  1.11      chs 	umap = &ubc_object.umap[ubc_offset >> ubc_winshift];
    249   1.2      chs 	KASSERT(umap->refcount != 0);
    250  1.18      chs 	slot_offset = ubc_offset & (ubc_winsize - 1);
    251   1.2      chs 
    252   1.2      chs 	/* no umap locking needed since we have a ref on the umap */
    253   1.2      chs 	uobj = umap->uobj;
    254   1.2      chs 	vp = (struct vnode *)uobj;
    255  1.18      chs 	KASSERT(vp != NULL);
    256   1.2      chs 
    257  1.18      chs 	npages = MIN(ubc_winsize - slot_offset,
    258  1.18      chs 		     (int)(round_page(MAX(vp->v_size, umap->offset +
    259  1.18      chs 				    umap->writeoff + umap->writelen)) -
    260  1.18      chs 			   umap->offset)) >> PAGE_SHIFT;
    261   1.2      chs 
    262   1.2      chs again:
    263   1.2      chs 	memset(pgs, 0, sizeof (pgs));
    264   1.2      chs 	simple_lock(&uobj->vmobjlock);
    265   1.2      chs 
    266   1.2      chs 	UVMHIST_LOG(ubchist, "slot_offset 0x%x writeoff 0x%x writelen 0x%x "
    267  1.18      chs 	    "v_size 0x%x", slot_offset, umap->writeoff, umap->writelen,
    268  1.18      chs 	    vp->v_size);
    269   1.2      chs 	UVMHIST_LOG(ubchist, "getpages vp %p offset 0x%x npages %d",
    270  1.18      chs 	    uobj, umap->offset + slot_offset, npages, 0);
    271   1.2      chs 
    272  1.18      chs 	flags |= PGO_PASTEOF;
    273   1.5      chs 	error = VOP_GETPAGES(vp, umap->offset + slot_offset, pgs, &npages, 0,
    274   1.5      chs 	    access_type, 0, flags);
    275   1.5      chs 	UVMHIST_LOG(ubchist, "getpages error %d npages %d", error, npages,0,0);
    276   1.2      chs 
    277   1.5      chs 	if (error == EAGAIN) {
    278   1.2      chs 		tsleep(&lbolt, PVM, "ubc_fault", 0);
    279   1.2      chs 		goto again;
    280   1.2      chs 	}
    281   1.5      chs 	if (error) {
    282  1.10      chs 		return error;
    283   1.5      chs 	}
    284   1.2      chs 
    285   1.2      chs 	va = ufi->orig_rvaddr;
    286   1.2      chs 	eva = ufi->orig_rvaddr + (npages << PAGE_SHIFT);
    287   1.2      chs 
    288   1.2      chs 	UVMHIST_LOG(ubchist, "va 0x%lx eva 0x%lx", va, eva, 0,0);
    289   1.2      chs 	simple_lock(&uobj->vmobjlock);
    290  1.18      chs 	uvm_lock_pageq();
    291   1.2      chs 	for (i = 0; va < eva; i++, va += PAGE_SIZE) {
    292   1.2      chs 		UVMHIST_LOG(ubchist, "pgs[%d] = %p", i, pgs[i],0,0);
    293   1.2      chs 		pg = pgs[i];
    294   1.2      chs 
    295   1.2      chs 		if (pg == NULL || pg == PGO_DONTCARE) {
    296   1.2      chs 			continue;
    297   1.2      chs 		}
    298   1.2      chs 		if (pg->flags & PG_WANTED) {
    299   1.2      chs 			wakeup(pg);
    300   1.2      chs 		}
    301   1.2      chs 		KASSERT((pg->flags & PG_FAKE) == 0);
    302   1.2      chs 		if (pg->flags & PG_RELEASED) {
    303  1.18      chs 			uvm_pagefree(pg);
    304   1.2      chs 			continue;
    305   1.2      chs 		}
    306   1.2      chs 		KASSERT(access_type == VM_PROT_READ ||
    307  1.18      chs 		    (pg->flags & PG_RDONLY) == 0);
    308  1.18      chs 		pmap_enter(ufi->orig_map->pmap, va, VM_PAGE_TO_PHYS(pg),
    309  1.18      chs 		    VM_PROT_READ | VM_PROT_WRITE, access_type);
    310   1.2      chs 		uvm_pageactivate(pg);
    311   1.2      chs 		pg->flags &= ~(PG_BUSY);
    312   1.2      chs 		UVM_PAGE_OWN(pg, NULL);
    313   1.2      chs 	}
    314  1.18      chs 	uvm_unlock_pageq();
    315   1.2      chs 	simple_unlock(&uobj->vmobjlock);
    316  1.17    chris 	pmap_update(ufi->orig_map->pmap);
    317   1.8      chs 	return 0;
    318   1.2      chs }
    319   1.2      chs 
    320   1.2      chs /*
    321   1.2      chs  * local functions
    322   1.2      chs  */
    323   1.2      chs 
    324  1.11      chs struct ubc_map *
    325   1.2      chs ubc_find_mapping(uobj, offset)
    326   1.2      chs 	struct uvm_object *uobj;
    327   1.2      chs 	voff_t offset;
    328   1.2      chs {
    329   1.2      chs 	struct ubc_map *umap;
    330   1.2      chs 
    331   1.2      chs 	LIST_FOREACH(umap, &ubc_object.hash[UBC_HASH(uobj, offset)], hash) {
    332   1.2      chs 		if (umap->uobj == uobj && umap->offset == offset) {
    333   1.2      chs 			return umap;
    334   1.2      chs 		}
    335   1.2      chs 	}
    336   1.2      chs 	return NULL;
    337   1.2      chs }
    338   1.2      chs 
    339   1.2      chs 
    340   1.2      chs /*
    341   1.2      chs  * ubc interface functions
    342   1.2      chs  */
    343   1.2      chs 
    344   1.2      chs /*
    345  1.18      chs  * ubc_alloc:  allocate a file mapping window
    346   1.2      chs  */
    347  1.18      chs 
    348   1.2      chs void *
    349   1.2      chs ubc_alloc(uobj, offset, lenp, flags)
    350   1.2      chs 	struct uvm_object *uobj;
    351   1.2      chs 	voff_t offset;
    352   1.2      chs 	vsize_t *lenp;
    353   1.2      chs 	int flags;
    354   1.2      chs {
    355  1.18      chs 	struct vnode *vp = (struct vnode *)uobj;
    356   1.6      chs 	vaddr_t slot_offset, va;
    357   1.2      chs 	struct ubc_map *umap;
    358   1.6      chs 	voff_t umap_offset;
    359  1.18      chs 	int error;
    360   1.2      chs 	UVMHIST_FUNC("ubc_alloc"); UVMHIST_CALLED(ubchist);
    361   1.2      chs 
    362   1.2      chs 	UVMHIST_LOG(ubchist, "uobj %p offset 0x%lx len 0x%lx filesize 0x%x",
    363  1.18      chs 	    uobj, offset, *lenp, vp->v_size);
    364   1.2      chs 
    365   1.6      chs 	umap_offset = (offset & ~((voff_t)ubc_winsize - 1));
    366   1.4    enami 	slot_offset = (vaddr_t)(offset & ((voff_t)ubc_winsize - 1));
    367  1.18      chs 	*lenp = MIN(*lenp, ubc_winsize - slot_offset);
    368   1.2      chs 
    369   1.2      chs 	/*
    370   1.2      chs 	 * the vnode is always locked here, so we don't need to add a ref.
    371   1.2      chs 	 */
    372   1.2      chs 
    373   1.2      chs again:
    374   1.2      chs 	simple_lock(&ubc_object.uobj.vmobjlock);
    375   1.2      chs 	umap = ubc_find_mapping(uobj, umap_offset);
    376   1.2      chs 	if (umap == NULL) {
    377   1.2      chs 		umap = TAILQ_FIRST(UBC_QUEUE(offset));
    378   1.2      chs 		if (umap == NULL) {
    379   1.2      chs 			simple_unlock(&ubc_object.uobj.vmobjlock);
    380   1.2      chs 			tsleep(&lbolt, PVM, "ubc_alloc", 0);
    381   1.2      chs 			goto again;
    382   1.2      chs 		}
    383   1.2      chs 
    384   1.2      chs 		/*
    385  1.18      chs 		 * remove from old hash (if any), add to new hash.
    386   1.2      chs 		 */
    387   1.2      chs 
    388   1.2      chs 		if (umap->uobj != NULL) {
    389   1.2      chs 			LIST_REMOVE(umap, hash);
    390   1.2      chs 		}
    391   1.2      chs 		umap->uobj = uobj;
    392   1.2      chs 		umap->offset = umap_offset;
    393   1.2      chs 		LIST_INSERT_HEAD(&ubc_object.hash[UBC_HASH(uobj, umap_offset)],
    394  1.18      chs 		    umap, hash);
    395  1.18      chs 		va = UBC_UMAP_ADDR(umap);
    396  1.18      chs 		if (umap->flags & UMAP_MAPPING_CACHED) {
    397  1.18      chs 			umap->flags &= ~UMAP_MAPPING_CACHED;
    398  1.18      chs 			pmap_remove(pmap_kernel(), va, va + ubc_winsize);
    399  1.18      chs 			pmap_update(pmap_kernel());
    400  1.18      chs 		}
    401  1.18      chs 	} else {
    402  1.18      chs 		va = UBC_UMAP_ADDR(umap);
    403   1.2      chs 	}
    404   1.2      chs 
    405   1.2      chs 	if (umap->refcount == 0) {
    406   1.2      chs 		TAILQ_REMOVE(UBC_QUEUE(offset), umap, inactive);
    407   1.2      chs 	}
    408   1.2      chs 
    409   1.2      chs #ifdef DIAGNOSTIC
    410  1.18      chs 	if ((flags & UBC_WRITE) && (umap->writeoff || umap->writelen)) {
    411   1.2      chs 		panic("ubc_fault: concurrent writes vp %p", uobj);
    412   1.2      chs 	}
    413   1.2      chs #endif
    414   1.2      chs 	if (flags & UBC_WRITE) {
    415   1.2      chs 		umap->writeoff = slot_offset;
    416   1.2      chs 		umap->writelen = *lenp;
    417   1.2      chs 	}
    418   1.2      chs 
    419   1.2      chs 	umap->refcount++;
    420   1.2      chs 	simple_unlock(&ubc_object.uobj.vmobjlock);
    421  1.18      chs 	UVMHIST_LOG(ubchist, "umap %p refs %d va %p flags 0x%x",
    422  1.18      chs 	    umap, umap->refcount, va, flags);
    423  1.18      chs 
    424  1.18      chs 	if (flags & UBC_FAULTBUSY) {
    425  1.18      chs 		int npages = (*lenp + PAGE_SIZE - 1) >> PAGE_SHIFT;
    426  1.18      chs 		struct vm_page *pgs[npages];
    427  1.18      chs 		int gpflags = PGO_SYNCIO|PGO_OVERWRITE|PGO_PASTEOF;
    428  1.18      chs 		int i;
    429   1.2      chs 
    430  1.18      chs 		if (umap->flags & UMAP_MAPPING_CACHED) {
    431  1.18      chs 			umap->flags &= ~UMAP_MAPPING_CACHED;
    432  1.18      chs 			pmap_remove(pmap_kernel(), va, va + ubc_winsize);
    433  1.18      chs 		}
    434  1.18      chs 		simple_lock(&uobj->vmobjlock);
    435  1.18      chs 		error = VOP_GETPAGES(vp, trunc_page(offset), pgs, &npages, 0,
    436  1.18      chs 		    VM_PROT_READ|VM_PROT_WRITE, 0, gpflags);
    437  1.18      chs 		UVMHIST_LOG(ubchist, "faultbusy getpages %d", error,0,0,0);
    438  1.18      chs 		if (error) {
    439  1.18      chs 			goto out;
    440  1.18      chs 		}
    441  1.18      chs 		for (i = 0; i < npages; i++) {
    442  1.18      chs 			pmap_kenter_pa(va + slot_offset + (i << PAGE_SHIFT),
    443  1.18      chs 			    VM_PAGE_TO_PHYS(pgs[i]),
    444  1.18      chs 			    VM_PROT_READ | VM_PROT_WRITE);
    445  1.18      chs 		}
    446  1.18      chs 		pmap_update(pmap_kernel());
    447  1.18      chs 		umap->flags |= UMAP_PAGES_LOCKED;
    448  1.18      chs 	}
    449  1.18      chs 
    450  1.18      chs out:
    451  1.18      chs 	return (void *)(va + slot_offset);
    452   1.2      chs }
    453   1.2      chs 
    454  1.18      chs /*
    455  1.18      chs  * ubc_release:  free a file mapping window.
    456  1.18      chs  */
    457   1.2      chs 
    458   1.2      chs void
    459  1.18      chs ubc_release(va, flags)
    460   1.2      chs 	void *va;
    461  1.18      chs 	int flags;
    462   1.2      chs {
    463   1.2      chs 	struct ubc_map *umap;
    464   1.2      chs 	struct uvm_object *uobj;
    465  1.18      chs 	vaddr_t umapva;
    466  1.18      chs 	boolean_t unmapped;
    467   1.2      chs 	UVMHIST_FUNC("ubc_release"); UVMHIST_CALLED(ubchist);
    468   1.2      chs 
    469   1.2      chs 	UVMHIST_LOG(ubchist, "va %p", va,0,0,0);
    470  1.11      chs 	umap = &ubc_object.umap[((char *)va - ubc_object.kva) >> ubc_winshift];
    471  1.18      chs 	umapva = UBC_UMAP_ADDR(umap);
    472   1.2      chs 	uobj = umap->uobj;
    473   1.2      chs 	KASSERT(uobj != NULL);
    474   1.2      chs 
    475  1.18      chs 	if (umap->flags & UMAP_PAGES_LOCKED) {
    476  1.18      chs 		int slot_offset = umap->writeoff;
    477  1.18      chs 		int endoff = umap->writeoff + umap->writelen;
    478  1.18      chs 		int zerolen = round_page(endoff) - endoff;
    479  1.18      chs 		int npages = (int)(round_page(umap->writeoff + umap->writelen)
    480  1.18      chs 				   - trunc_page(umap->writeoff)) >> PAGE_SHIFT;
    481  1.18      chs 		struct vm_page *pgs[npages];
    482  1.18      chs 		paddr_t pa;
    483  1.18      chs 		int i;
    484  1.18      chs 		boolean_t rv;
    485  1.18      chs 
    486  1.18      chs 		if (zerolen) {
    487  1.18      chs 			memset((char *)umapva + endoff, 0, zerolen);
    488  1.18      chs 		}
    489  1.18      chs 		umap->flags &= ~UMAP_PAGES_LOCKED;
    490  1.18      chs 		uvm_lock_pageq();
    491  1.18      chs 		for (i = 0; i < npages; i++) {
    492  1.18      chs 			rv = pmap_extract(pmap_kernel(),
    493  1.18      chs 			    umapva + slot_offset + (i << PAGE_SHIFT), &pa);
    494  1.18      chs 			KASSERT(rv);
    495  1.18      chs 			pgs[i] = PHYS_TO_VM_PAGE(pa);
    496  1.18      chs 			pgs[i]->flags &= ~(PG_FAKE|PG_CLEAN);
    497  1.18      chs 			uvm_pageactivate(pgs[i]);
    498  1.18      chs 		}
    499  1.18      chs 		uvm_unlock_pageq();
    500  1.18      chs 		pmap_kremove(umapva, ubc_winsize);
    501  1.18      chs 		pmap_update(pmap_kernel());
    502  1.18      chs 		uvm_page_unbusy(pgs, npages);
    503  1.18      chs 		unmapped = TRUE;
    504  1.18      chs 	} else {
    505  1.18      chs 		unmapped = FALSE;
    506  1.18      chs 	}
    507  1.18      chs 
    508  1.18      chs 	simple_lock(&ubc_object.uobj.vmobjlock);
    509   1.2      chs 	umap->writeoff = 0;
    510   1.2      chs 	umap->writelen = 0;
    511   1.2      chs 	umap->refcount--;
    512   1.2      chs 	if (umap->refcount == 0) {
    513   1.2      chs 		if (UBC_RELEASE_UNMAP &&
    514   1.2      chs 		    (((struct vnode *)uobj)->v_flag & VTEXT)) {
    515   1.2      chs 
    516   1.2      chs 			/*
    517   1.2      chs 			 * if this file is the executable image of
    518   1.2      chs 			 * some process, that process will likely have
    519   1.2      chs 			 * the file mapped at an alignment other than
    520   1.2      chs 			 * what PMAP_PREFER() would like.  we'd like
    521   1.2      chs 			 * to have process text be able to use the
    522   1.2      chs 			 * cache even if someone is also reading the
    523   1.2      chs 			 * file, so invalidate mappings of such files
    524   1.2      chs 			 * as soon as possible.
    525   1.2      chs 			 */
    526   1.2      chs 
    527  1.18      chs 			pmap_remove(pmap_kernel(), umapva,
    528  1.18      chs 				    umapva + ubc_winsize);
    529  1.18      chs 			umap->flags &= ~UMAP_MAPPING_CACHED;
    530  1.17    chris 			pmap_update(pmap_kernel());
    531   1.2      chs 			LIST_REMOVE(umap, hash);
    532   1.2      chs 			umap->uobj = NULL;
    533   1.2      chs 			TAILQ_INSERT_HEAD(UBC_QUEUE(umap->offset), umap,
    534   1.2      chs 			    inactive);
    535   1.2      chs 		} else {
    536  1.18      chs 			if (!unmapped) {
    537  1.18      chs 				umap->flags |= UMAP_MAPPING_CACHED;
    538  1.18      chs 			}
    539   1.2      chs 			TAILQ_INSERT_TAIL(UBC_QUEUE(umap->offset), umap,
    540   1.2      chs 			    inactive);
    541   1.2      chs 		}
    542   1.2      chs 	}
    543   1.2      chs 	UVMHIST_LOG(ubchist, "umap %p refs %d", umap, umap->refcount,0,0);
    544   1.2      chs 	simple_unlock(&ubc_object.uobj.vmobjlock);
    545   1.2      chs }
    546   1.2      chs 
    547   1.2      chs 
    548   1.2      chs /*
    549   1.2      chs  * removing a range of mappings from the ubc mapping cache.
    550   1.2      chs  */
    551   1.2      chs 
    552   1.2      chs void
    553   1.2      chs ubc_flush(uobj, start, end)
    554   1.2      chs 	struct uvm_object *uobj;
    555   1.2      chs 	voff_t start, end;
    556   1.2      chs {
    557   1.2      chs 	struct ubc_map *umap;
    558   1.2      chs 	vaddr_t va;
    559   1.2      chs 	UVMHIST_FUNC("ubc_flush");  UVMHIST_CALLED(ubchist);
    560   1.2      chs 
    561   1.2      chs 	UVMHIST_LOG(ubchist, "uobj %p start 0x%lx end 0x%lx",
    562   1.2      chs 		    uobj, start, end,0);
    563   1.2      chs 
    564   1.2      chs 	simple_lock(&ubc_object.uobj.vmobjlock);
    565   1.2      chs 	for (umap = ubc_object.umap;
    566   1.2      chs 	     umap < &ubc_object.umap[ubc_nwins];
    567   1.2      chs 	     umap++) {
    568   1.2      chs 
    569  1.18      chs 		if (umap->uobj != uobj || umap->offset < start ||
    570   1.2      chs 		    (umap->offset >= end && end != 0) ||
    571   1.2      chs 		    umap->refcount > 0) {
    572   1.2      chs 			continue;
    573   1.2      chs 		}
    574   1.2      chs 
    575   1.2      chs 		/*
    576   1.2      chs 		 * remove from hash,
    577   1.2      chs 		 * move to head of inactive queue.
    578   1.2      chs 		 */
    579   1.2      chs 
    580   1.2      chs 		va = (vaddr_t)(ubc_object.kva +
    581  1.18      chs 		    ((umap - ubc_object.umap) << ubc_winshift));
    582   1.4    enami 		pmap_remove(pmap_kernel(), va, va + ubc_winsize);
    583   1.2      chs 
    584   1.2      chs 		LIST_REMOVE(umap, hash);
    585   1.2      chs 		umap->uobj = NULL;
    586   1.2      chs 		TAILQ_REMOVE(UBC_QUEUE(umap->offset), umap, inactive);
    587   1.2      chs 		TAILQ_INSERT_HEAD(UBC_QUEUE(umap->offset), umap, inactive);
    588   1.2      chs 	}
    589  1.18      chs 	pmap_update(pmap_kernel());
    590   1.2      chs 	simple_unlock(&ubc_object.uobj.vmobjlock);
    591   1.2      chs }
    592