Home | History | Annotate | Line # | Download | only in uvm
uvm_bio.c revision 1.106
      1  1.106        ad /*	$NetBSD: uvm_bio.c,v 1.106 2020/03/17 18:31:39 ad 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 /*
     33   1.33       chs  * uvm_bio.c: buffered i/o object mapping cache
     34    1.2       chs  */
     35    1.2       chs 
     36   1.21     lukem #include <sys/cdefs.h>
     37  1.106        ad __KERNEL_RCSID(0, "$NetBSD: uvm_bio.c,v 1.106 2020/03/17 18:31:39 ad Exp $");
     38   1.21     lukem 
     39   1.21     lukem #include "opt_uvmhist.h"
     40   1.50      yamt #include "opt_ubc.h"
     41    1.2       chs 
     42    1.2       chs #include <sys/param.h>
     43    1.2       chs #include <sys/systm.h>
     44   1.65        ad #include <sys/kmem.h>
     45    1.2       chs #include <sys/kernel.h>
     46   1.60        ad #include <sys/proc.h>
     47   1.67     pooka #include <sys/vnode.h>
     48    1.2       chs 
     49    1.2       chs #include <uvm/uvm.h>
     50    1.2       chs 
     51   1.95  jdolecek #ifdef PMAP_DIRECT
     52   1.95  jdolecek #  define UBC_USE_PMAP_DIRECT
     53   1.95  jdolecek #endif
     54    1.2       chs 
     55    1.2       chs /*
     56    1.2       chs  * local functions
     57    1.2       chs  */
     58    1.2       chs 
     59   1.39   thorpej static int	ubc_fault(struct uvm_faultinfo *, vaddr_t, struct vm_page **,
     60   1.44  drochner 			  int, int, vm_prot_t, int);
     61   1.39   thorpej static struct ubc_map *ubc_find_mapping(struct uvm_object *, voff_t);
     62   1.95  jdolecek #ifdef UBC_USE_PMAP_DIRECT
     63   1.95  jdolecek static int __noinline ubc_uiomove_direct(struct uvm_object *, struct uio *, vsize_t,
     64   1.95  jdolecek 			  int, int);
     65   1.95  jdolecek static void __noinline ubc_zerorange_direct(struct uvm_object *, off_t, size_t, int);
     66   1.95  jdolecek 
     67   1.95  jdolecek bool ubc_direct = false; /* XXX */
     68   1.95  jdolecek #endif
     69    1.2       chs 
     70    1.2       chs /*
     71    1.2       chs  * local data structues
     72    1.2       chs  */
     73    1.2       chs 
     74   1.18       chs #define UBC_HASH(uobj, offset) 						\
     75   1.18       chs 	(((((u_long)(uobj)) >> 8) + (((u_long)(offset)) >> PAGE_SHIFT)) & \
     76    1.2       chs 				ubc_object.hashmask)
     77    1.2       chs 
     78   1.18       chs #define UBC_QUEUE(offset)						\
     79   1.18       chs 	(&ubc_object.inactive[(((u_long)(offset)) >> ubc_winshift) &	\
     80   1.18       chs 			     (UBC_NQUEUES - 1)])
     81   1.18       chs 
     82   1.18       chs #define UBC_UMAP_ADDR(u)						\
     83   1.18       chs 	(vaddr_t)(ubc_object.kva + (((u) - ubc_object.umap) << ubc_winshift))
     84   1.18       chs 
     85   1.18       chs 
     86   1.18       chs #define UMAP_PAGES_LOCKED	0x0001
     87   1.18       chs #define UMAP_MAPPING_CACHED	0x0002
     88    1.2       chs 
     89   1.73     rmind struct ubc_map {
     90    1.2       chs 	struct uvm_object *	uobj;		/* mapped object */
     91    1.2       chs 	voff_t			offset;		/* offset into uobj */
     92   1.33       chs 	voff_t			writeoff;	/* write offset */
     93   1.33       chs 	vsize_t			writelen;	/* write len */
     94   1.18       chs 	int			refcount;	/* refcount on mapping */
     95   1.18       chs 	int			flags;		/* extra state */
     96   1.42      yamt 	int			advice;
     97    1.2       chs 
     98    1.2       chs 	LIST_ENTRY(ubc_map)	hash;		/* hash table */
     99    1.2       chs 	TAILQ_ENTRY(ubc_map)	inactive;	/* inactive queue */
    100   1.73     rmind 	LIST_ENTRY(ubc_map)	list;		/* per-object list */
    101    1.2       chs };
    102    1.2       chs 
    103   1.82      matt TAILQ_HEAD(ubc_inactive_head, ubc_map);
    104   1.73     rmind static struct ubc_object {
    105    1.2       chs 	struct uvm_object uobj;		/* glue for uvm_map() */
    106    1.2       chs 	char *kva;			/* where ubc_object is mapped */
    107    1.2       chs 	struct ubc_map *umap;		/* array of ubc_map's */
    108    1.2       chs 
    109    1.2       chs 	LIST_HEAD(, ubc_map) *hash;	/* hashtable for cached ubc_map's */
    110    1.2       chs 	u_long hashmask;		/* mask for hashtable */
    111    1.2       chs 
    112   1.82      matt 	struct ubc_inactive_head *inactive;
    113    1.2       chs 					/* inactive queues for ubc_map's */
    114    1.2       chs } ubc_object;
    115    1.2       chs 
    116   1.63      yamt const struct uvm_pagerops ubc_pager = {
    117   1.48  christos 	.pgo_fault = ubc_fault,
    118    1.2       chs 	/* ... rest are NULL */
    119    1.2       chs };
    120    1.2       chs 
    121    1.2       chs int ubc_nwins = UBC_NWINS;
    122   1.93  jdolecek int ubc_winshift __read_mostly = UBC_WINSHIFT;
    123   1.93  jdolecek int ubc_winsize __read_mostly;
    124   1.27   thorpej #if defined(PMAP_PREFER)
    125    1.2       chs int ubc_nqueues;
    126    1.2       chs #define UBC_NQUEUES ubc_nqueues
    127    1.2       chs #else
    128    1.2       chs #define UBC_NQUEUES 1
    129    1.2       chs #endif
    130    1.2       chs 
    131   1.50      yamt #if defined(UBC_STATS)
    132   1.50      yamt 
    133   1.50      yamt #define	UBC_EVCNT_DEFINE(name) \
    134   1.50      yamt struct evcnt ubc_evcnt_##name = \
    135   1.50      yamt EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "ubc", #name); \
    136   1.50      yamt EVCNT_ATTACH_STATIC(ubc_evcnt_##name);
    137   1.50      yamt #define	UBC_EVCNT_INCR(name) ubc_evcnt_##name.ev_count++
    138   1.50      yamt 
    139   1.50      yamt #else /* defined(UBC_STATS) */
    140   1.50      yamt 
    141   1.50      yamt #define	UBC_EVCNT_DEFINE(name)	/* nothing */
    142   1.50      yamt #define	UBC_EVCNT_INCR(name)	/* nothing */
    143   1.50      yamt 
    144   1.50      yamt #endif /* defined(UBC_STATS) */
    145   1.50      yamt 
    146   1.50      yamt UBC_EVCNT_DEFINE(wincachehit)
    147   1.50      yamt UBC_EVCNT_DEFINE(wincachemiss)
    148   1.57      yamt UBC_EVCNT_DEFINE(faultbusy)
    149   1.50      yamt 
    150    1.2       chs /*
    151    1.2       chs  * ubc_init
    152    1.2       chs  *
    153    1.2       chs  * init pager private data structures.
    154    1.2       chs  */
    155    1.2       chs 
    156    1.2       chs void
    157    1.2       chs ubc_init(void)
    158    1.2       chs {
    159   1.15    simonb 	/*
    160   1.15    simonb 	 * Make sure ubc_winshift is sane.
    161   1.15    simonb 	 */
    162   1.15    simonb 	if (ubc_winshift < PAGE_SHIFT)
    163   1.15    simonb 		ubc_winshift = PAGE_SHIFT;
    164   1.95  jdolecek 	ubc_winsize = 1 << ubc_winshift;
    165    1.2       chs 
    166    1.2       chs 	/*
    167    1.2       chs 	 * init ubc_object.
    168    1.2       chs 	 * alloc and init ubc_map's.
    169    1.2       chs 	 * init inactive queues.
    170    1.2       chs 	 * alloc and init hashtable.
    171    1.2       chs 	 * map in ubc_object.
    172    1.2       chs 	 */
    173    1.2       chs 
    174   1.73     rmind 	uvm_obj_init(&ubc_object.uobj, &ubc_pager, true, UVM_OBJ_KERN);
    175    1.2       chs 
    176   1.65        ad 	ubc_object.umap = kmem_zalloc(ubc_nwins * sizeof(struct ubc_map),
    177   1.65        ad 	    KM_SLEEP);
    178    1.7     enami 	if (ubc_object.umap == NULL)
    179    1.7     enami 		panic("ubc_init: failed to allocate ubc_map");
    180    1.2       chs 
    181   1.95  jdolecek 	vaddr_t va = (vaddr_t)1L;
    182    1.2       chs #ifdef PMAP_PREFER
    183   1.36    atatat 	PMAP_PREFER(0, &va, 0, 0);	/* kernel is never topdown */
    184   1.11       chs 	ubc_nqueues = va >> ubc_winshift;
    185   1.11       chs 	if (ubc_nqueues == 0) {
    186   1.11       chs 		ubc_nqueues = 1;
    187    1.2       chs 	}
    188    1.2       chs #endif
    189   1.65        ad 	ubc_object.inactive = kmem_alloc(UBC_NQUEUES *
    190   1.65        ad 	    sizeof(struct ubc_inactive_head), KM_SLEEP);
    191   1.95  jdolecek 	for (int i = 0; i < UBC_NQUEUES; i++) {
    192    1.2       chs 		TAILQ_INIT(&ubc_object.inactive[i]);
    193    1.2       chs 	}
    194   1.95  jdolecek 	for (int i = 0; i < ubc_nwins; i++) {
    195   1.95  jdolecek 		struct ubc_map *umap;
    196    1.2       chs 		umap = &ubc_object.umap[i];
    197    1.2       chs 		TAILQ_INSERT_TAIL(&ubc_object.inactive[i & (UBC_NQUEUES - 1)],
    198    1.2       chs 				  umap, inactive);
    199    1.2       chs 	}
    200    1.2       chs 
    201   1.65        ad 	ubc_object.hash = hashinit(ubc_nwins, HASH_LIST, true,
    202   1.65        ad 	    &ubc_object.hashmask);
    203   1.95  jdolecek 	for (int i = 0; i <= ubc_object.hashmask; i++) {
    204    1.2       chs 		LIST_INIT(&ubc_object.hash[i]);
    205    1.2       chs 	}
    206    1.2       chs 
    207    1.2       chs 	if (uvm_map(kernel_map, (vaddr_t *)&ubc_object.kva,
    208   1.11       chs 		    ubc_nwins << ubc_winshift, &ubc_object.uobj, 0, (vsize_t)va,
    209   1.92      maxv 		    UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW, UVM_INH_NONE,
    210    1.9       chs 				UVM_ADV_RANDOM, UVM_FLAG_NOMERGE)) != 0) {
    211   1.26    provos 		panic("ubc_init: failed to map ubc_object");
    212    1.2       chs 	}
    213   1.81  riastrad }
    214   1.81  riastrad 
    215   1.81  riastrad void
    216   1.81  riastrad ubchist_init(void)
    217   1.81  riastrad {
    218   1.81  riastrad 
    219    1.2       chs 	UVMHIST_INIT(ubchist, 300);
    220    1.2       chs }
    221    1.2       chs 
    222    1.2       chs /*
    223   1.69     rmind  * ubc_fault_page: helper of ubc_fault to handle a single page.
    224   1.70     rmind  *
    225   1.70     rmind  * => Caller has UVM object locked.
    226   1.73     rmind  * => Caller will perform pmap_update().
    227   1.69     rmind  */
    228   1.69     rmind 
    229   1.70     rmind static inline int
    230   1.69     rmind ubc_fault_page(const struct uvm_faultinfo *ufi, const struct ubc_map *umap,
    231   1.69     rmind     struct vm_page *pg, vm_prot_t prot, vm_prot_t access_type, vaddr_t va)
    232   1.69     rmind {
    233   1.69     rmind 	vm_prot_t mask;
    234   1.69     rmind 	int error;
    235   1.69     rmind 	bool rdonly;
    236   1.69     rmind 
    237  1.104        ad 	KASSERT(rw_write_held(pg->uobject->vmobjlock));
    238   1.70     rmind 
    239   1.69     rmind 	KASSERT((pg->flags & PG_FAKE) == 0);
    240   1.69     rmind 	if (pg->flags & PG_RELEASED) {
    241   1.69     rmind 		uvm_pagefree(pg);
    242   1.70     rmind 		return 0;
    243   1.69     rmind 	}
    244   1.69     rmind 	if (pg->loan_count != 0) {
    245   1.69     rmind 
    246   1.69     rmind 		/*
    247   1.69     rmind 		 * Avoid unneeded loan break, if possible.
    248   1.69     rmind 		 */
    249   1.69     rmind 
    250   1.69     rmind 		if ((access_type & VM_PROT_WRITE) == 0) {
    251   1.69     rmind 			prot &= ~VM_PROT_WRITE;
    252   1.69     rmind 		}
    253   1.69     rmind 		if (prot & VM_PROT_WRITE) {
    254   1.69     rmind 			struct vm_page *newpg;
    255   1.69     rmind 
    256   1.69     rmind 			newpg = uvm_loanbreak(pg);
    257   1.69     rmind 			if (newpg == NULL) {
    258   1.69     rmind 				uvm_page_unbusy(&pg, 1);
    259   1.70     rmind 				return ENOMEM;
    260   1.69     rmind 			}
    261   1.69     rmind 			pg = newpg;
    262   1.69     rmind 		}
    263   1.69     rmind 	}
    264   1.69     rmind 
    265   1.69     rmind 	/*
    266   1.69     rmind 	 * Note that a page whose backing store is partially allocated
    267   1.69     rmind 	 * is marked as PG_RDONLY.
    268  1.103        ad 	 *
    269  1.103        ad 	 * it's a responsibility of ubc_alloc's caller to allocate backing
    270  1.103        ad 	 * blocks before writing to the window.
    271   1.69     rmind 	 */
    272   1.69     rmind 
    273   1.69     rmind 	KASSERT((pg->flags & PG_RDONLY) == 0 ||
    274   1.69     rmind 	    (access_type & VM_PROT_WRITE) == 0 ||
    275   1.69     rmind 	    pg->offset < umap->writeoff ||
    276   1.69     rmind 	    pg->offset + PAGE_SIZE > umap->writeoff + umap->writelen);
    277   1.69     rmind 
    278  1.103        ad 	rdonly = uvm_pagereadonly_p(pg);
    279   1.69     rmind 	mask = rdonly ? ~VM_PROT_WRITE : VM_PROT_ALL;
    280   1.69     rmind 
    281   1.69     rmind 	error = pmap_enter(ufi->orig_map->pmap, va, VM_PAGE_TO_PHYS(pg),
    282   1.69     rmind 	    prot & mask, PMAP_CANFAIL | (access_type & mask));
    283   1.69     rmind 
    284  1.102        ad 	uvm_pagelock(pg);
    285   1.69     rmind 	uvm_pageactivate(pg);
    286  1.106        ad 	uvm_pagewakeup(pg);
    287  1.102        ad 	uvm_pageunlock(pg);
    288  1.106        ad 	pg->flags &= ~PG_BUSY;
    289  1.106        ad 	UVM_PAGE_OWN(pg, NULL);
    290   1.69     rmind 
    291   1.70     rmind 	return error;
    292   1.69     rmind }
    293   1.69     rmind 
    294   1.69     rmind /*
    295    1.2       chs  * ubc_fault: fault routine for ubc mapping
    296    1.2       chs  */
    297   1.18       chs 
    298   1.39   thorpej static int
    299   1.54      yamt ubc_fault(struct uvm_faultinfo *ufi, vaddr_t ign1, struct vm_page **ign2,
    300   1.54      yamt     int ign3, int ign4, vm_prot_t access_type, int flags)
    301    1.2       chs {
    302    1.2       chs 	struct uvm_object *uobj;
    303    1.2       chs 	struct ubc_map *umap;
    304    1.2       chs 	vaddr_t va, eva, ubc_offset, slot_offset;
    305   1.69     rmind 	struct vm_page *pgs[ubc_winsize >> PAGE_SHIFT];
    306   1.18       chs 	int i, error, npages;
    307   1.23       chs 	vm_prot_t prot;
    308   1.69     rmind 
    309   1.69     rmind 	UVMHIST_FUNC("ubc_fault"); UVMHIST_CALLED(ubchist);
    310    1.2       chs 
    311    1.2       chs 	/*
    312    1.2       chs 	 * no need to try with PGO_LOCKED...
    313    1.2       chs 	 * we don't need to have the map locked since we know that
    314    1.2       chs 	 * no one will mess with it until our reference is released.
    315    1.2       chs 	 */
    316   1.18       chs 
    317    1.2       chs 	if (flags & PGO_LOCKED) {
    318   1.73     rmind 		uvmfault_unlockall(ufi, NULL, &ubc_object.uobj);
    319    1.2       chs 		flags &= ~PGO_LOCKED;
    320    1.2       chs 	}
    321    1.2       chs 
    322    1.2       chs 	va = ufi->orig_rvaddr;
    323    1.2       chs 	ubc_offset = va - (vaddr_t)ubc_object.kva;
    324   1.11       chs 	umap = &ubc_object.umap[ubc_offset >> ubc_winshift];
    325    1.2       chs 	KASSERT(umap->refcount != 0);
    326   1.53      yamt 	KASSERT((umap->flags & UMAP_PAGES_LOCKED) == 0);
    327   1.18       chs 	slot_offset = ubc_offset & (ubc_winsize - 1);
    328    1.2       chs 
    329   1.34       chs 	/*
    330   1.34       chs 	 * some platforms cannot write to individual bytes atomically, so
    331   1.34       chs 	 * software has to do read/modify/write of larger quantities instead.
    332   1.34       chs 	 * this means that the access_type for "write" operations
    333   1.34       chs 	 * can be VM_PROT_READ, which confuses us mightily.
    334   1.37     perry 	 *
    335   1.34       chs 	 * deal with this by resetting access_type based on the info
    336   1.34       chs 	 * that ubc_alloc() stores for us.
    337   1.34       chs 	 */
    338   1.34       chs 
    339   1.34       chs 	access_type = umap->writelen ? VM_PROT_WRITE : VM_PROT_READ;
    340   1.91  pgoyette 	UVMHIST_LOG(ubchist, "va 0x%jx ubc_offset 0x%jx access_type %jd",
    341   1.34       chs 	    va, ubc_offset, access_type, 0);
    342   1.34       chs 
    343   1.33       chs 	if ((access_type & VM_PROT_WRITE) != 0) {
    344   1.87       kre #ifndef PRIxOFF		/* XXX */
    345   1.88       kre #define PRIxOFF "jx"	/* XXX */
    346   1.87       kre #endif			/* XXX */
    347   1.84  riastrad 		KASSERTMSG((trunc_page(umap->writeoff) <= slot_offset),
    348   1.87       kre 		    "out of range write: slot=%#"PRIxVSIZE" off=%#"PRIxOFF,
    349   1.88       kre 		    slot_offset, (intmax_t)umap->writeoff);
    350   1.84  riastrad 		KASSERTMSG((slot_offset < umap->writeoff + umap->writelen),
    351   1.87       kre 		    "out of range write: slot=%#"PRIxVADDR
    352   1.87       kre 		        " off=%#"PRIxOFF" len=%#"PRIxVSIZE,
    353   1.89     ozaki 		    slot_offset, (intmax_t)umap->writeoff, umap->writelen);
    354   1.33       chs 	}
    355   1.33       chs 
    356    1.2       chs 	/* no umap locking needed since we have a ref on the umap */
    357    1.2       chs 	uobj = umap->uobj;
    358    1.2       chs 
    359   1.33       chs 	if ((access_type & VM_PROT_WRITE) == 0) {
    360   1.33       chs 		npages = (ubc_winsize - slot_offset) >> PAGE_SHIFT;
    361   1.33       chs 	} else {
    362   1.33       chs 		npages = (round_page(umap->offset + umap->writeoff +
    363   1.33       chs 		    umap->writelen) - (umap->offset + slot_offset))
    364   1.33       chs 		    >> PAGE_SHIFT;
    365   1.33       chs 		flags |= PGO_PASTEOF;
    366   1.33       chs 	}
    367    1.2       chs 
    368    1.2       chs again:
    369    1.2       chs 	memset(pgs, 0, sizeof (pgs));
    370  1.104        ad 	rw_enter(uobj->vmobjlock, RW_WRITER);
    371    1.2       chs 
    372   1.91  pgoyette 	UVMHIST_LOG(ubchist, "slot_offset 0x%jx writeoff 0x%jx writelen 0x%jx ",
    373   1.33       chs 	    slot_offset, umap->writeoff, umap->writelen, 0);
    374   1.91  pgoyette 	UVMHIST_LOG(ubchist, "getpages uobj %#jx offset 0x%jx npages %jd",
    375   1.91  pgoyette 	    (uintptr_t)uobj, umap->offset + slot_offset, npages, 0);
    376    1.2       chs 
    377   1.33       chs 	error = (*uobj->pgops->pgo_get)(uobj, umap->offset + slot_offset, pgs,
    378   1.42      yamt 	    &npages, 0, access_type, umap->advice, flags | PGO_NOBLOCKALLOC |
    379   1.41      yamt 	    PGO_NOTIMESTAMP);
    380   1.91  pgoyette 	UVMHIST_LOG(ubchist, "getpages error %jd npages %jd", error, npages, 0,
    381   1.24    simonb 	    0);
    382    1.2       chs 
    383    1.5       chs 	if (error == EAGAIN) {
    384   1.70     rmind 		kpause("ubc_fault", false, hz >> 2, NULL);
    385    1.2       chs 		goto again;
    386    1.2       chs 	}
    387    1.5       chs 	if (error) {
    388   1.10       chs 		return error;
    389    1.5       chs 	}
    390    1.2       chs 
    391   1.69     rmind 	/*
    392   1.69     rmind 	 * For virtually-indexed, virtually-tagged caches we should avoid
    393   1.69     rmind 	 * creating writable mappings when we do not absolutely need them,
    394   1.69     rmind 	 * since the "compatible alias" trick does not work on such caches.
    395   1.69     rmind 	 * Otherwise, we can always map the pages writable.
    396   1.69     rmind 	 */
    397   1.69     rmind 
    398   1.69     rmind #ifdef PMAP_CACHE_VIVT
    399   1.69     rmind 	prot = VM_PROT_READ | access_type;
    400   1.69     rmind #else
    401   1.69     rmind 	prot = VM_PROT_READ | VM_PROT_WRITE;
    402   1.69     rmind #endif
    403   1.70     rmind 
    404    1.2       chs 	va = ufi->orig_rvaddr;
    405    1.2       chs 	eva = ufi->orig_rvaddr + (npages << PAGE_SHIFT);
    406    1.2       chs 
    407   1.91  pgoyette 	UVMHIST_LOG(ubchist, "va 0x%jx eva 0x%jx", va, eva, 0, 0);
    408   1.73     rmind 
    409   1.73     rmind 	/*
    410   1.73     rmind 	 * Note: normally all returned pages would have the same UVM object.
    411   1.73     rmind 	 * However, layered file-systems and e.g. tmpfs, may return pages
    412   1.73     rmind 	 * which belong to underlying UVM object.  In such case, lock is
    413   1.73     rmind 	 * shared amongst the objects.
    414   1.73     rmind 	 */
    415  1.104        ad 	rw_enter(uobj->vmobjlock, RW_WRITER);
    416   1.28      yamt 	for (i = 0; va < eva; i++, va += PAGE_SIZE) {
    417   1.69     rmind 		struct vm_page *pg;
    418   1.34       chs 
    419   1.91  pgoyette 		UVMHIST_LOG(ubchist, "pgs[%jd] = %#jx", i, (uintptr_t)pgs[i],
    420   1.91  pgoyette 		    0, 0);
    421    1.2       chs 		pg = pgs[i];
    422    1.2       chs 
    423    1.2       chs 		if (pg == NULL || pg == PGO_DONTCARE) {
    424    1.2       chs 			continue;
    425    1.2       chs 		}
    426   1.73     rmind 		KASSERT(uobj->vmobjlock == pg->uobject->vmobjlock);
    427   1.70     rmind 		error = ubc_fault_page(ufi, umap, pg, prot, access_type, va);
    428   1.70     rmind 		if (error) {
    429   1.70     rmind 			/*
    430   1.70     rmind 			 * Flush (there might be pages entered), drop the lock,
    431   1.73     rmind 			 * and perform uvm_wait().  Note: page will re-fault.
    432   1.70     rmind 			 */
    433   1.70     rmind 			pmap_update(ufi->orig_map->pmap);
    434  1.104        ad 			rw_exit(uobj->vmobjlock);
    435   1.70     rmind 			uvm_wait("ubc_fault");
    436  1.104        ad 			rw_enter(uobj->vmobjlock, RW_WRITER);
    437   1.70     rmind 		}
    438   1.70     rmind 	}
    439   1.73     rmind 	/* Must make VA visible before the unlock. */
    440   1.73     rmind 	pmap_update(ufi->orig_map->pmap);
    441  1.104        ad 	rw_exit(uobj->vmobjlock);
    442   1.73     rmind 
    443    1.8       chs 	return 0;
    444    1.2       chs }
    445    1.2       chs 
    446    1.2       chs /*
    447    1.2       chs  * local functions
    448    1.2       chs  */
    449    1.2       chs 
    450   1.39   thorpej static struct ubc_map *
    451   1.39   thorpej ubc_find_mapping(struct uvm_object *uobj, voff_t offset)
    452    1.2       chs {
    453    1.2       chs 	struct ubc_map *umap;
    454    1.2       chs 
    455    1.2       chs 	LIST_FOREACH(umap, &ubc_object.hash[UBC_HASH(uobj, offset)], hash) {
    456    1.2       chs 		if (umap->uobj == uobj && umap->offset == offset) {
    457    1.2       chs 			return umap;
    458    1.2       chs 		}
    459    1.2       chs 	}
    460    1.2       chs 	return NULL;
    461    1.2       chs }
    462    1.2       chs 
    463    1.2       chs 
    464    1.2       chs /*
    465    1.2       chs  * ubc interface functions
    466    1.2       chs  */
    467    1.2       chs 
    468    1.2       chs /*
    469   1.18       chs  * ubc_alloc:  allocate a file mapping window
    470    1.2       chs  */
    471   1.18       chs 
    472   1.94  jdolecek static void * __noinline
    473   1.42      yamt ubc_alloc(struct uvm_object *uobj, voff_t offset, vsize_t *lenp, int advice,
    474   1.42      yamt     int flags)
    475    1.2       chs {
    476    1.6       chs 	vaddr_t slot_offset, va;
    477    1.2       chs 	struct ubc_map *umap;
    478    1.6       chs 	voff_t umap_offset;
    479   1.18       chs 	int error;
    480    1.2       chs 	UVMHIST_FUNC("ubc_alloc"); UVMHIST_CALLED(ubchist);
    481    1.2       chs 
    482   1.91  pgoyette 	UVMHIST_LOG(ubchist, "uobj %#jx offset 0x%jx len 0x%jx",
    483   1.91  pgoyette 	    (uintptr_t)uobj, offset, *lenp, 0);
    484    1.2       chs 
    485   1.34       chs 	KASSERT(*lenp > 0);
    486    1.6       chs 	umap_offset = (offset & ~((voff_t)ubc_winsize - 1));
    487    1.4     enami 	slot_offset = (vaddr_t)(offset & ((voff_t)ubc_winsize - 1));
    488   1.18       chs 	*lenp = MIN(*lenp, ubc_winsize - slot_offset);
    489    1.2       chs 
    490  1.104        ad 	rw_enter(ubc_object.uobj.vmobjlock, RW_WRITER);
    491   1.76     rmind again:
    492    1.2       chs 	/*
    493   1.76     rmind 	 * The UVM object is already referenced.
    494   1.73     rmind 	 * Lock order: UBC object -> ubc_map::uobj.
    495    1.2       chs 	 */
    496    1.2       chs 	umap = ubc_find_mapping(uobj, umap_offset);
    497    1.2       chs 	if (umap == NULL) {
    498   1.73     rmind 		struct uvm_object *oobj;
    499   1.73     rmind 
    500   1.50      yamt 		UBC_EVCNT_INCR(wincachemiss);
    501    1.2       chs 		umap = TAILQ_FIRST(UBC_QUEUE(offset));
    502    1.2       chs 		if (umap == NULL) {
    503  1.104        ad 			rw_exit(ubc_object.uobj.vmobjlock);
    504  1.104        ad 			kpause("ubc_alloc", false, hz >> 2, NULL);
    505  1.104        ad 			rw_enter(ubc_object.uobj.vmobjlock, RW_WRITER);
    506    1.2       chs 			goto again;
    507    1.2       chs 		}
    508    1.2       chs 
    509   1.73     rmind 		va = UBC_UMAP_ADDR(umap);
    510   1.73     rmind 		oobj = umap->uobj;
    511   1.73     rmind 
    512    1.2       chs 		/*
    513   1.76     rmind 		 * Remove from old hash (if any), add to new hash.
    514    1.2       chs 		 */
    515    1.2       chs 
    516   1.73     rmind 		if (oobj != NULL) {
    517   1.76     rmind 			/*
    518   1.76     rmind 			 * Mapping must be removed before the list entry,
    519   1.76     rmind 			 * since there is a race with ubc_purge().
    520   1.76     rmind 			 */
    521   1.73     rmind 			if (umap->flags & UMAP_MAPPING_CACHED) {
    522   1.73     rmind 				umap->flags &= ~UMAP_MAPPING_CACHED;
    523  1.104        ad 				rw_enter(oobj->vmobjlock, RW_WRITER);
    524   1.73     rmind 				pmap_remove(pmap_kernel(), va,
    525   1.73     rmind 				    va + ubc_winsize);
    526   1.73     rmind 				pmap_update(pmap_kernel());
    527  1.104        ad 				rw_exit(oobj->vmobjlock);
    528   1.73     rmind 			}
    529   1.75   hannken 			LIST_REMOVE(umap, hash);
    530   1.75   hannken 			LIST_REMOVE(umap, list);
    531   1.73     rmind 		} else {
    532   1.73     rmind 			KASSERT((umap->flags & UMAP_MAPPING_CACHED) == 0);
    533    1.2       chs 		}
    534    1.2       chs 		umap->uobj = uobj;
    535    1.2       chs 		umap->offset = umap_offset;
    536    1.2       chs 		LIST_INSERT_HEAD(&ubc_object.hash[UBC_HASH(uobj, umap_offset)],
    537   1.18       chs 		    umap, hash);
    538   1.73     rmind 		LIST_INSERT_HEAD(&uobj->uo_ubc, umap, list);
    539   1.18       chs 	} else {
    540   1.50      yamt 		UBC_EVCNT_INCR(wincachehit);
    541   1.18       chs 		va = UBC_UMAP_ADDR(umap);
    542    1.2       chs 	}
    543    1.2       chs 
    544    1.2       chs 	if (umap->refcount == 0) {
    545    1.2       chs 		TAILQ_REMOVE(UBC_QUEUE(offset), umap, inactive);
    546    1.2       chs 	}
    547    1.2       chs 
    548    1.2       chs 	if (flags & UBC_WRITE) {
    549   1.73     rmind 		KASSERTMSG(umap->writeoff == 0 && umap->writelen == 0,
    550   1.79       jym 		    "ubc_alloc: concurrent writes to uobj %p", uobj);
    551    1.2       chs 		umap->writeoff = slot_offset;
    552    1.2       chs 		umap->writelen = *lenp;
    553    1.2       chs 	}
    554    1.2       chs 
    555    1.2       chs 	umap->refcount++;
    556   1.42      yamt 	umap->advice = advice;
    557  1.104        ad 	rw_exit(ubc_object.uobj.vmobjlock);
    558   1.91  pgoyette 	UVMHIST_LOG(ubchist, "umap %#jx refs %jd va %#jx flags 0x%jx",
    559   1.91  pgoyette 	    (uintptr_t)umap, umap->refcount, (uintptr_t)va, flags);
    560   1.18       chs 
    561   1.18       chs 	if (flags & UBC_FAULTBUSY) {
    562   1.95  jdolecek 		// XXX add offset from slot_offset?
    563   1.18       chs 		int npages = (*lenp + PAGE_SIZE - 1) >> PAGE_SHIFT;
    564   1.18       chs 		struct vm_page *pgs[npages];
    565   1.40      yamt 		int gpflags =
    566   1.41      yamt 		    PGO_SYNCIO|PGO_OVERWRITE|PGO_PASTEOF|PGO_NOBLOCKALLOC|
    567   1.41      yamt 		    PGO_NOTIMESTAMP;
    568   1.18       chs 		int i;
    569   1.30       dbj 		KDASSERT(flags & UBC_WRITE);
    570   1.57      yamt 		KASSERT(umap->refcount == 1);
    571    1.2       chs 
    572   1.57      yamt 		UBC_EVCNT_INCR(faultbusy);
    573   1.73     rmind again_faultbusy:
    574  1.104        ad 		rw_enter(uobj->vmobjlock, RW_WRITER);
    575   1.18       chs 		if (umap->flags & UMAP_MAPPING_CACHED) {
    576   1.18       chs 			umap->flags &= ~UMAP_MAPPING_CACHED;
    577   1.18       chs 			pmap_remove(pmap_kernel(), va, va + ubc_winsize);
    578   1.18       chs 		}
    579   1.22     enami 		memset(pgs, 0, sizeof(pgs));
    580   1.73     rmind 
    581   1.33       chs 		error = (*uobj->pgops->pgo_get)(uobj, trunc_page(offset), pgs,
    582   1.42      yamt 		    &npages, 0, VM_PROT_READ | VM_PROT_WRITE, advice, gpflags);
    583   1.91  pgoyette 		UVMHIST_LOG(ubchist, "faultbusy getpages %jd", error, 0, 0, 0);
    584   1.18       chs 		if (error) {
    585   1.83     rmind 			/*
    586   1.83     rmind 			 * Flush: the mapping above might have been removed.
    587   1.83     rmind 			 */
    588   1.83     rmind 			pmap_update(pmap_kernel());
    589   1.18       chs 			goto out;
    590   1.18       chs 		}
    591   1.18       chs 		for (i = 0; i < npages; i++) {
    592   1.59      yamt 			struct vm_page *pg = pgs[i];
    593   1.59      yamt 
    594   1.59      yamt 			KASSERT(pg->uobject == uobj);
    595   1.59      yamt 			if (pg->loan_count != 0) {
    596  1.104        ad 				rw_enter(uobj->vmobjlock, RW_WRITER);
    597   1.59      yamt 				if (pg->loan_count != 0) {
    598   1.59      yamt 					pg = uvm_loanbreak(pg);
    599   1.59      yamt 				}
    600   1.59      yamt 				if (pg == NULL) {
    601   1.59      yamt 					pmap_kremove(va, ubc_winsize);
    602   1.59      yamt 					pmap_update(pmap_kernel());
    603   1.59      yamt 					uvm_page_unbusy(pgs, npages);
    604  1.104        ad 					rw_exit(uobj->vmobjlock);
    605   1.59      yamt 					uvm_wait("ubc_alloc");
    606   1.59      yamt 					goto again_faultbusy;
    607   1.59      yamt 				}
    608  1.104        ad 				rw_exit(uobj->vmobjlock);
    609   1.59      yamt 				pgs[i] = pg;
    610   1.59      yamt 			}
    611   1.18       chs 			pmap_kenter_pa(va + slot_offset + (i << PAGE_SHIFT),
    612   1.68    cegger 			    VM_PAGE_TO_PHYS(pg),
    613   1.68    cegger 			    VM_PROT_READ | VM_PROT_WRITE, 0);
    614   1.18       chs 		}
    615   1.18       chs 		pmap_update(pmap_kernel());
    616   1.18       chs 		umap->flags |= UMAP_PAGES_LOCKED;
    617   1.57      yamt 	} else {
    618   1.57      yamt 		KASSERT((umap->flags & UMAP_PAGES_LOCKED) == 0);
    619   1.18       chs 	}
    620   1.18       chs 
    621   1.18       chs out:
    622   1.18       chs 	return (void *)(va + slot_offset);
    623    1.2       chs }
    624    1.2       chs 
    625   1.18       chs /*
    626   1.18       chs  * ubc_release:  free a file mapping window.
    627   1.18       chs  */
    628    1.2       chs 
    629   1.94  jdolecek static void __noinline
    630   1.39   thorpej ubc_release(void *va, int flags)
    631    1.2       chs {
    632    1.2       chs 	struct ubc_map *umap;
    633    1.2       chs 	struct uvm_object *uobj;
    634   1.18       chs 	vaddr_t umapva;
    635   1.55   thorpej 	bool unmapped;
    636    1.2       chs 	UVMHIST_FUNC("ubc_release"); UVMHIST_CALLED(ubchist);
    637    1.2       chs 
    638   1.91  pgoyette 	UVMHIST_LOG(ubchist, "va %#jx", (uintptr_t)va, 0, 0, 0);
    639   1.11       chs 	umap = &ubc_object.umap[((char *)va - ubc_object.kva) >> ubc_winshift];
    640   1.18       chs 	umapva = UBC_UMAP_ADDR(umap);
    641    1.2       chs 	uobj = umap->uobj;
    642    1.2       chs 	KASSERT(uobj != NULL);
    643    1.2       chs 
    644   1.18       chs 	if (umap->flags & UMAP_PAGES_LOCKED) {
    645   1.72     rmind 		const voff_t slot_offset = umap->writeoff;
    646   1.72     rmind 		const voff_t endoff = umap->writeoff + umap->writelen;
    647   1.72     rmind 		const voff_t zerolen = round_page(endoff) - endoff;
    648   1.72     rmind 		const u_int npages = (round_page(endoff) -
    649   1.72     rmind 		    trunc_page(slot_offset)) >> PAGE_SHIFT;
    650   1.18       chs 		struct vm_page *pgs[npages];
    651   1.18       chs 
    652   1.57      yamt 		KASSERT((umap->flags & UMAP_MAPPING_CACHED) == 0);
    653   1.18       chs 		if (zerolen) {
    654   1.18       chs 			memset((char *)umapva + endoff, 0, zerolen);
    655   1.18       chs 		}
    656   1.18       chs 		umap->flags &= ~UMAP_PAGES_LOCKED;
    657  1.104        ad 		rw_enter(uobj->vmobjlock, RW_WRITER);
    658   1.72     rmind 		for (u_int i = 0; i < npages; i++) {
    659   1.72     rmind 			paddr_t pa;
    660   1.80    martin 			bool rv __diagused;
    661   1.72     rmind 
    662   1.18       chs 			rv = pmap_extract(pmap_kernel(),
    663   1.18       chs 			    umapva + slot_offset + (i << PAGE_SHIFT), &pa);
    664   1.18       chs 			KASSERT(rv);
    665   1.18       chs 			pgs[i] = PHYS_TO_VM_PAGE(pa);
    666  1.103        ad 			pgs[i]->flags &= ~PG_FAKE;
    667  1.103        ad 			KASSERTMSG(uvm_pagegetdirty(pgs[i]) ==
    668  1.103        ad 			    UVM_PAGE_STATUS_DIRTY,
    669  1.103        ad 			    "page %p not dirty", pgs[i]);
    670   1.28      yamt 			KASSERT(pgs[i]->loan_count == 0);
    671  1.102        ad 			uvm_pagelock(pgs[i]);
    672   1.18       chs 			uvm_pageactivate(pgs[i]);
    673  1.102        ad 			uvm_pageunlock(pgs[i]);
    674   1.18       chs 		}
    675   1.18       chs 		pmap_kremove(umapva, ubc_winsize);
    676   1.18       chs 		pmap_update(pmap_kernel());
    677   1.18       chs 		uvm_page_unbusy(pgs, npages);
    678  1.104        ad 		rw_exit(uobj->vmobjlock);
    679   1.56   thorpej 		unmapped = true;
    680   1.18       chs 	} else {
    681   1.56   thorpej 		unmapped = false;
    682   1.18       chs 	}
    683   1.18       chs 
    684  1.104        ad 	rw_enter(ubc_object.uobj.vmobjlock, RW_WRITER);
    685    1.2       chs 	umap->writeoff = 0;
    686    1.2       chs 	umap->writelen = 0;
    687    1.2       chs 	umap->refcount--;
    688    1.2       chs 	if (umap->refcount == 0) {
    689   1.33       chs 		if (flags & UBC_UNMAP) {
    690    1.2       chs 			/*
    691   1.33       chs 			 * Invalidate any cached mappings if requested.
    692   1.33       chs 			 * This is typically used to avoid leaving
    693   1.33       chs 			 * incompatible cache aliases around indefinitely.
    694    1.2       chs 			 */
    695  1.104        ad 			rw_enter(uobj->vmobjlock, RW_WRITER);
    696   1.18       chs 			pmap_remove(pmap_kernel(), umapva,
    697   1.18       chs 				    umapva + ubc_winsize);
    698   1.73     rmind 			pmap_update(pmap_kernel());
    699  1.104        ad 			rw_exit(uobj->vmobjlock);
    700   1.73     rmind 
    701   1.18       chs 			umap->flags &= ~UMAP_MAPPING_CACHED;
    702    1.2       chs 			LIST_REMOVE(umap, hash);
    703   1.77     rmind 			LIST_REMOVE(umap, list);
    704    1.2       chs 			umap->uobj = NULL;
    705    1.2       chs 			TAILQ_INSERT_HEAD(UBC_QUEUE(umap->offset), umap,
    706    1.2       chs 			    inactive);
    707    1.2       chs 		} else {
    708   1.18       chs 			if (!unmapped) {
    709   1.18       chs 				umap->flags |= UMAP_MAPPING_CACHED;
    710   1.18       chs 			}
    711    1.2       chs 			TAILQ_INSERT_TAIL(UBC_QUEUE(umap->offset), umap,
    712    1.2       chs 			    inactive);
    713    1.2       chs 		}
    714    1.2       chs 	}
    715  1.100     skrll 	UVMHIST_LOG(ubchist, "umap %#jx refs %jd", (uintptr_t)umap,
    716   1.91  pgoyette 	    umap->refcount, 0, 0);
    717  1.104        ad 	rw_exit(ubc_object.uobj.vmobjlock);
    718    1.2       chs }
    719    1.2       chs 
    720   1.58      yamt /*
    721   1.58      yamt  * ubc_uiomove: move data to/from an object.
    722   1.58      yamt  */
    723   1.58      yamt 
    724   1.58      yamt int
    725   1.62      yamt ubc_uiomove(struct uvm_object *uobj, struct uio *uio, vsize_t todo, int advice,
    726   1.62      yamt     int flags)
    727   1.58      yamt {
    728   1.72     rmind 	const bool overwrite = (flags & UBC_FAULTBUSY) != 0;
    729   1.58      yamt 	voff_t off;
    730   1.58      yamt 	int error;
    731   1.58      yamt 
    732   1.58      yamt 	KASSERT(todo <= uio->uio_resid);
    733   1.58      yamt 	KASSERT(((flags & UBC_WRITE) != 0 && uio->uio_rw == UIO_WRITE) ||
    734   1.58      yamt 	    ((flags & UBC_READ) != 0 && uio->uio_rw == UIO_READ));
    735   1.58      yamt 
    736   1.95  jdolecek #ifdef UBC_USE_PMAP_DIRECT
    737   1.95  jdolecek 	if (ubc_direct) {
    738   1.95  jdolecek 		return ubc_uiomove_direct(uobj, uio, todo, advice, flags);
    739   1.95  jdolecek 	}
    740   1.95  jdolecek #endif
    741   1.95  jdolecek 
    742   1.58      yamt 	off = uio->uio_offset;
    743   1.58      yamt 	error = 0;
    744   1.58      yamt 	while (todo > 0) {
    745   1.58      yamt 		vsize_t bytelen = todo;
    746   1.58      yamt 		void *win;
    747   1.58      yamt 
    748   1.62      yamt 		win = ubc_alloc(uobj, off, &bytelen, advice, flags);
    749   1.58      yamt 		if (error == 0) {
    750   1.58      yamt 			error = uiomove(win, bytelen, uio);
    751   1.58      yamt 		}
    752   1.58      yamt 		if (error != 0 && overwrite) {
    753   1.58      yamt 			/*
    754   1.58      yamt 			 * if we haven't initialized the pages yet,
    755   1.58      yamt 			 * do it now.  it's safe to use memset here
    756   1.58      yamt 			 * because we just mapped the pages above.
    757   1.58      yamt 			 */
    758   1.64        ad 			printf("%s: error=%d\n", __func__, error);
    759   1.58      yamt 			memset(win, 0, bytelen);
    760   1.58      yamt 		}
    761   1.58      yamt 		ubc_release(win, flags);
    762   1.58      yamt 		off += bytelen;
    763   1.58      yamt 		todo -= bytelen;
    764   1.58      yamt 		if (error != 0 && (flags & UBC_PARTIALOK) != 0) {
    765   1.58      yamt 			break;
    766   1.58      yamt 		}
    767   1.58      yamt 	}
    768   1.58      yamt 
    769   1.58      yamt 	return error;
    770   1.58      yamt }
    771   1.67     pooka 
    772   1.67     pooka /*
    773   1.74   hannken  * ubc_zerorange: set a range of bytes in an object to zero.
    774   1.67     pooka  */
    775   1.67     pooka 
    776   1.67     pooka void
    777   1.74   hannken ubc_zerorange(struct uvm_object *uobj, off_t off, size_t len, int flags)
    778   1.67     pooka {
    779   1.95  jdolecek 
    780   1.95  jdolecek #ifdef UBC_USE_PMAP_DIRECT
    781   1.95  jdolecek 	if (ubc_direct) {
    782   1.95  jdolecek 		ubc_zerorange_direct(uobj, off, len, flags);
    783   1.95  jdolecek 		return;
    784   1.95  jdolecek 	}
    785   1.95  jdolecek #endif
    786   1.67     pooka 
    787   1.67     pooka 	/*
    788   1.67     pooka 	 * XXXUBC invent kzero() and use it
    789   1.67     pooka 	 */
    790   1.67     pooka 
    791   1.67     pooka 	while (len) {
    792   1.95  jdolecek 		void *win;
    793   1.67     pooka 		vsize_t bytelen = len;
    794   1.67     pooka 
    795   1.74   hannken 		win = ubc_alloc(uobj, off, &bytelen, UVM_ADV_NORMAL, UBC_WRITE);
    796   1.67     pooka 		memset(win, 0, bytelen);
    797   1.67     pooka 		ubc_release(win, flags);
    798   1.67     pooka 
    799   1.67     pooka 		off += bytelen;
    800   1.67     pooka 		len -= bytelen;
    801   1.67     pooka 	}
    802   1.67     pooka }
    803   1.73     rmind 
    804   1.95  jdolecek #ifdef UBC_USE_PMAP_DIRECT
    805   1.95  jdolecek /* Copy data using direct map */
    806   1.95  jdolecek 
    807   1.95  jdolecek /*
    808   1.95  jdolecek  * ubc_alloc_direct:  allocate a file mapping window using direct map
    809   1.95  jdolecek  */
    810   1.95  jdolecek static int __noinline
    811   1.95  jdolecek ubc_alloc_direct(struct uvm_object *uobj, voff_t offset, vsize_t *lenp,
    812   1.95  jdolecek     int advice, int flags, struct vm_page **pgs, int *npages)
    813   1.95  jdolecek {
    814   1.95  jdolecek 	voff_t pgoff;
    815   1.95  jdolecek 	int error;
    816   1.99  jdolecek 	int gpflags = flags | PGO_NOTIMESTAMP | PGO_SYNCIO | PGO_ALLPAGES;
    817   1.95  jdolecek 	int access_type = VM_PROT_READ;
    818   1.97       chs 	UVMHIST_FUNC("ubc_alloc_direct"); UVMHIST_CALLED(ubchist);
    819   1.95  jdolecek 
    820   1.95  jdolecek 	if (flags & UBC_WRITE) {
    821   1.95  jdolecek 		if (flags & UBC_FAULTBUSY)
    822   1.95  jdolecek 			gpflags |= PGO_OVERWRITE;
    823   1.95  jdolecek #if 0
    824   1.95  jdolecek 		KASSERT(!UVM_OBJ_NEEDS_WRITEFAULT(uobj));
    825   1.95  jdolecek #endif
    826   1.95  jdolecek 
    827   1.99  jdolecek 		/*
    828   1.99  jdolecek 		 * Tell genfs_getpages() we already have the journal lock,
    829   1.99  jdolecek 		 * allow allocation past current EOF.
    830   1.99  jdolecek 		 */
    831   1.99  jdolecek 		gpflags |= PGO_JOURNALLOCKED | PGO_PASTEOF;
    832   1.95  jdolecek 		access_type |= VM_PROT_WRITE;
    833   1.99  jdolecek 	} else {
    834   1.99  jdolecek 		/* Don't need the empty blocks allocated, PG_RDONLY is okay */
    835   1.99  jdolecek 		gpflags |= PGO_NOBLOCKALLOC;
    836   1.95  jdolecek 	}
    837   1.95  jdolecek 
    838   1.95  jdolecek 	pgoff = (offset & PAGE_MASK);
    839   1.95  jdolecek 	*lenp = MIN(*lenp, ubc_winsize - pgoff);
    840   1.95  jdolecek 
    841   1.95  jdolecek again:
    842   1.95  jdolecek 	*npages = (*lenp + pgoff + PAGE_SIZE - 1) >> PAGE_SHIFT;
    843   1.95  jdolecek 	KASSERT((*npages * PAGE_SIZE) <= ubc_winsize);
    844   1.95  jdolecek 	KASSERT(*lenp + pgoff <= ubc_winsize);
    845   1.95  jdolecek 	memset(pgs, 0, *npages * sizeof(pgs[0]));
    846   1.95  jdolecek 
    847  1.104        ad 	rw_enter(uobj->vmobjlock, RW_WRITER);
    848   1.95  jdolecek 	error = (*uobj->pgops->pgo_get)(uobj, trunc_page(offset), pgs,
    849   1.95  jdolecek 	    npages, 0, access_type, advice, gpflags);
    850   1.95  jdolecek 	UVMHIST_LOG(ubchist, "alloc_direct getpages %jd", error, 0, 0, 0);
    851   1.95  jdolecek 	if (error) {
    852   1.95  jdolecek 		if (error == EAGAIN) {
    853   1.95  jdolecek 			kpause("ubc_alloc_directg", false, hz >> 2, NULL);
    854   1.95  jdolecek 			goto again;
    855   1.95  jdolecek 		}
    856   1.95  jdolecek 		return error;
    857   1.95  jdolecek 	}
    858   1.95  jdolecek 
    859  1.104        ad 	rw_enter(uobj->vmobjlock, RW_WRITER);
    860   1.95  jdolecek 	for (int i = 0; i < *npages; i++) {
    861   1.95  jdolecek 		struct vm_page *pg = pgs[i];
    862   1.95  jdolecek 
    863   1.95  jdolecek 		KASSERT(pg != NULL);
    864   1.95  jdolecek 		KASSERT(pg != PGO_DONTCARE);
    865   1.95  jdolecek 		KASSERT((pg->flags & PG_FAKE) == 0 || (gpflags & PGO_OVERWRITE));
    866   1.95  jdolecek 		KASSERT(pg->uobject->vmobjlock == uobj->vmobjlock);
    867   1.95  jdolecek 
    868   1.95  jdolecek 		/* Avoid breaking loan if possible, only do it on write */
    869   1.95  jdolecek 		if ((flags & UBC_WRITE) && pg->loan_count != 0) {
    870   1.95  jdolecek 			pg = uvm_loanbreak(pg);
    871   1.95  jdolecek 			if (pg == NULL) {
    872   1.95  jdolecek 				uvm_page_unbusy(pgs, *npages);
    873  1.104        ad 				rw_exit(uobj->vmobjlock);
    874   1.95  jdolecek 				uvm_wait("ubc_alloc_directl");
    875   1.95  jdolecek 				goto again;
    876   1.95  jdolecek 			}
    877   1.95  jdolecek 			pgs[i] = pg;
    878   1.95  jdolecek 		}
    879   1.95  jdolecek 
    880   1.95  jdolecek 		/* Page must be writable by now */
    881   1.95  jdolecek 		KASSERT((pg->flags & PG_RDONLY) == 0 || (flags & UBC_WRITE) == 0);
    882   1.96  jdolecek 	}
    883  1.104        ad 	rw_exit(uobj->vmobjlock);
    884   1.96  jdolecek 
    885   1.96  jdolecek 	return 0;
    886   1.96  jdolecek }
    887   1.96  jdolecek 
    888   1.96  jdolecek static void __noinline
    889   1.96  jdolecek ubc_direct_release(struct uvm_object *uobj,
    890   1.96  jdolecek 	int flags, struct vm_page **pgs, int npages)
    891   1.96  jdolecek {
    892  1.104        ad 	rw_enter(uobj->vmobjlock, RW_WRITER);
    893   1.96  jdolecek 	for (int i = 0; i < npages; i++) {
    894   1.96  jdolecek 		struct vm_page *pg = pgs[i];
    895   1.95  jdolecek 
    896  1.102        ad 		uvm_pagelock(pg);
    897   1.95  jdolecek 		uvm_pageactivate(pg);
    898  1.102        ad 		uvm_pageunlock(pg);
    899   1.95  jdolecek 
    900  1.103        ad 		/*
    901  1.103        ad 		 * Page was changed, no longer fake and neither clean.
    902  1.103        ad 		 * There's no managed mapping in the direct case, so
    903  1.103        ad 		 * mark the page dirty manually.
    904  1.103        ad 		 */
    905  1.103        ad 		if (flags & UBC_WRITE) {
    906  1.103        ad 			pg->flags &= ~PG_FAKE;
    907  1.103        ad 			KASSERTMSG(uvm_pagegetdirty(pg) ==
    908  1.103        ad 			    UVM_PAGE_STATUS_DIRTY,
    909  1.103        ad 			    "page %p not dirty", pg);
    910  1.103        ad 			uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
    911  1.103        ad 		}
    912   1.95  jdolecek 	}
    913   1.96  jdolecek 	uvm_page_unbusy(pgs, npages);
    914  1.104        ad 	rw_exit(uobj->vmobjlock);
    915   1.95  jdolecek }
    916   1.95  jdolecek 
    917   1.95  jdolecek static int
    918   1.95  jdolecek ubc_uiomove_process(void *win, size_t len, void *arg)
    919   1.95  jdolecek {
    920   1.95  jdolecek 	struct uio *uio = (struct uio *)arg;
    921   1.95  jdolecek 
    922   1.95  jdolecek 	return uiomove(win, len, uio);
    923   1.95  jdolecek }
    924   1.95  jdolecek 
    925   1.95  jdolecek static int
    926   1.95  jdolecek ubc_zerorange_process(void *win, size_t len, void *arg)
    927   1.95  jdolecek {
    928   1.95  jdolecek 	memset(win, 0, len);
    929   1.95  jdolecek 	return 0;
    930   1.95  jdolecek }
    931   1.95  jdolecek 
    932   1.95  jdolecek static int __noinline
    933   1.95  jdolecek ubc_uiomove_direct(struct uvm_object *uobj, struct uio *uio, vsize_t todo, int advice,
    934   1.95  jdolecek     int flags)
    935   1.95  jdolecek {
    936   1.95  jdolecek 	const bool overwrite = (flags & UBC_FAULTBUSY) != 0;
    937   1.95  jdolecek 	voff_t off;
    938   1.95  jdolecek 	int error, npages;
    939   1.95  jdolecek 	struct vm_page *pgs[ubc_winsize >> PAGE_SHIFT];
    940   1.95  jdolecek 
    941   1.95  jdolecek 	KASSERT(todo <= uio->uio_resid);
    942   1.95  jdolecek 	KASSERT(((flags & UBC_WRITE) != 0 && uio->uio_rw == UIO_WRITE) ||
    943   1.95  jdolecek 	    ((flags & UBC_READ) != 0 && uio->uio_rw == UIO_READ));
    944   1.95  jdolecek 
    945   1.95  jdolecek 	off = uio->uio_offset;
    946   1.95  jdolecek 	error = 0;
    947   1.95  jdolecek 	while (todo > 0) {
    948   1.95  jdolecek 		vsize_t bytelen = todo;
    949   1.95  jdolecek 
    950   1.95  jdolecek 		error = ubc_alloc_direct(uobj, off, &bytelen, advice, flags,
    951   1.95  jdolecek 		    pgs, &npages);
    952   1.95  jdolecek 		if (error != 0) {
    953   1.95  jdolecek 			/* can't do anything, failed to get the pages */
    954   1.95  jdolecek 			break;
    955   1.95  jdolecek 		}
    956   1.95  jdolecek 
    957   1.95  jdolecek 		if (error == 0) {
    958   1.95  jdolecek 			error = uvm_direct_process(pgs, npages, off, bytelen,
    959   1.95  jdolecek 			    ubc_uiomove_process, uio);
    960   1.95  jdolecek 		}
    961   1.95  jdolecek 		if (error != 0 && overwrite) {
    962   1.95  jdolecek 			/*
    963   1.95  jdolecek 			 * if we haven't initialized the pages yet,
    964   1.95  jdolecek 			 * do it now.  it's safe to use memset here
    965   1.95  jdolecek 			 * because we just mapped the pages above.
    966   1.95  jdolecek 			 */
    967   1.95  jdolecek 			printf("%s: error=%d\n", __func__, error);
    968   1.95  jdolecek 			(void) uvm_direct_process(pgs, npages, off, bytelen,
    969   1.95  jdolecek 			    ubc_zerorange_process, NULL);
    970   1.95  jdolecek 		}
    971   1.95  jdolecek 
    972   1.96  jdolecek 		ubc_direct_release(uobj, flags, pgs, npages);
    973   1.95  jdolecek 
    974   1.95  jdolecek 		off += bytelen;
    975   1.95  jdolecek 		todo -= bytelen;
    976   1.95  jdolecek 
    977   1.95  jdolecek 		if (error != 0 && ISSET(flags, UBC_PARTIALOK)) {
    978   1.95  jdolecek 			break;
    979   1.95  jdolecek 		}
    980   1.95  jdolecek 	}
    981   1.95  jdolecek 
    982   1.95  jdolecek 	return error;
    983   1.95  jdolecek }
    984   1.95  jdolecek 
    985   1.95  jdolecek static void __noinline
    986   1.95  jdolecek ubc_zerorange_direct(struct uvm_object *uobj, off_t off, size_t todo, int flags)
    987   1.95  jdolecek {
    988   1.95  jdolecek 	int error, npages;
    989   1.95  jdolecek 	struct vm_page *pgs[ubc_winsize >> PAGE_SHIFT];
    990   1.95  jdolecek 
    991   1.96  jdolecek 	flags |= UBC_WRITE;
    992   1.96  jdolecek 
    993   1.95  jdolecek 	error = 0;
    994   1.95  jdolecek 	while (todo > 0) {
    995   1.95  jdolecek 		vsize_t bytelen = todo;
    996   1.95  jdolecek 
    997   1.95  jdolecek 		error = ubc_alloc_direct(uobj, off, &bytelen, UVM_ADV_NORMAL,
    998   1.96  jdolecek 		    flags, pgs, &npages);
    999   1.95  jdolecek 		if (error != 0) {
   1000   1.95  jdolecek 			/* can't do anything, failed to get the pages */
   1001   1.95  jdolecek 			break;
   1002   1.95  jdolecek 		}
   1003   1.95  jdolecek 
   1004   1.95  jdolecek 		error = uvm_direct_process(pgs, npages, off, bytelen,
   1005   1.95  jdolecek 		    ubc_zerorange_process, NULL);
   1006   1.95  jdolecek 
   1007   1.96  jdolecek 		ubc_direct_release(uobj, flags, pgs, npages);
   1008   1.95  jdolecek 
   1009   1.95  jdolecek 		off += bytelen;
   1010   1.95  jdolecek 		todo -= bytelen;
   1011   1.95  jdolecek 	}
   1012   1.95  jdolecek }
   1013   1.95  jdolecek 
   1014   1.95  jdolecek #endif /* UBC_USE_PMAP_DIRECT */
   1015   1.95  jdolecek 
   1016   1.73     rmind /*
   1017   1.73     rmind  * ubc_purge: disassociate ubc_map structures from an empty uvm_object.
   1018   1.73     rmind  */
   1019   1.73     rmind 
   1020   1.73     rmind void
   1021   1.73     rmind ubc_purge(struct uvm_object *uobj)
   1022   1.73     rmind {
   1023   1.73     rmind 	struct ubc_map *umap;
   1024   1.73     rmind 	vaddr_t va;
   1025   1.73     rmind 
   1026   1.73     rmind 	KASSERT(uobj->uo_npages == 0);
   1027   1.73     rmind 
   1028   1.76     rmind 	/*
   1029   1.76     rmind 	 * Safe to check without lock held, as ubc_alloc() removes
   1030   1.76     rmind 	 * the mapping and list entry in the correct order.
   1031   1.76     rmind 	 */
   1032   1.76     rmind 	if (__predict_true(LIST_EMPTY(&uobj->uo_ubc))) {
   1033   1.76     rmind 		return;
   1034   1.76     rmind 	}
   1035  1.104        ad 	rw_enter(ubc_object.uobj.vmobjlock, RW_WRITER);
   1036   1.73     rmind 	while ((umap = LIST_FIRST(&uobj->uo_ubc)) != NULL) {
   1037   1.73     rmind 		KASSERT(umap->refcount == 0);
   1038   1.73     rmind 		for (va = 0; va < ubc_winsize; va += PAGE_SIZE) {
   1039   1.73     rmind 			KASSERT(!pmap_extract(pmap_kernel(),
   1040   1.73     rmind 			    va + UBC_UMAP_ADDR(umap), NULL));
   1041   1.73     rmind 		}
   1042   1.73     rmind 		LIST_REMOVE(umap, list);
   1043   1.73     rmind 		LIST_REMOVE(umap, hash);
   1044   1.73     rmind 		umap->flags &= ~UMAP_MAPPING_CACHED;
   1045   1.73     rmind 		umap->uobj = NULL;
   1046   1.73     rmind 	}
   1047  1.104        ad 	rw_exit(ubc_object.uobj.vmobjlock);
   1048   1.73     rmind }
   1049