Home | History | Annotate | Line # | Download | only in rumpkern
vm.c revision 1.20.2.6
      1  1.20.2.6     joerg /*	$NetBSD: vm.c,v 1.20.2.6 2007/12/03 16:15:14 joerg Exp $	*/
      2  1.20.2.2     joerg 
      3  1.20.2.2     joerg /*
      4  1.20.2.2     joerg  * Copyright (c) 2007 Antti Kantee.  All Rights Reserved.
      5  1.20.2.2     joerg  *
      6  1.20.2.2     joerg  * Development of this software was supported by Google Summer of Code.
      7  1.20.2.2     joerg  *
      8  1.20.2.2     joerg  * Redistribution and use in source and binary forms, with or without
      9  1.20.2.2     joerg  * modification, are permitted provided that the following conditions
     10  1.20.2.2     joerg  * are met:
     11  1.20.2.2     joerg  * 1. Redistributions of source code must retain the above copyright
     12  1.20.2.2     joerg  *    notice, this list of conditions and the following disclaimer.
     13  1.20.2.2     joerg  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.20.2.2     joerg  *    notice, this list of conditions and the following disclaimer in the
     15  1.20.2.2     joerg  *    documentation and/or other materials provided with the distribution.
     16  1.20.2.2     joerg  *
     17  1.20.2.2     joerg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     18  1.20.2.2     joerg  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19  1.20.2.2     joerg  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     20  1.20.2.2     joerg  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21  1.20.2.2     joerg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  1.20.2.2     joerg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     23  1.20.2.2     joerg  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  1.20.2.2     joerg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  1.20.2.2     joerg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  1.20.2.2     joerg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  1.20.2.2     joerg  * SUCH DAMAGE.
     28  1.20.2.2     joerg  */
     29  1.20.2.2     joerg 
     30  1.20.2.2     joerg /*
     31  1.20.2.2     joerg  * Virtual memory emulation routines.  Contents:
     32  1.20.2.2     joerg  *  + UBC
     33  1.20.2.2     joerg  *  + anon objects & pager
     34  1.20.2.2     joerg  *  + vnode objects & pager
     35  1.20.2.2     joerg  *  + misc support routines
     36  1.20.2.2     joerg  *  + kmem
     37  1.20.2.2     joerg  */
     38  1.20.2.2     joerg 
     39  1.20.2.2     joerg /*
     40  1.20.2.2     joerg  * XXX: we abuse pg->uanon for the virtual address of the storage
     41  1.20.2.2     joerg  * for each page.  phys_addr would fit the job description better,
     42  1.20.2.2     joerg  * except that it will create unnecessary lossage on some platforms
     43  1.20.2.2     joerg  * due to not being a pointer type.
     44  1.20.2.2     joerg  */
     45  1.20.2.2     joerg 
     46  1.20.2.2     joerg #include <sys/param.h>
     47  1.20.2.2     joerg #include <sys/null.h>
     48  1.20.2.2     joerg #include <sys/vnode.h>
     49  1.20.2.2     joerg #include <sys/buf.h>
     50  1.20.2.2     joerg #include <sys/kmem.h>
     51  1.20.2.2     joerg 
     52  1.20.2.2     joerg #include <uvm/uvm.h>
     53  1.20.2.2     joerg #include <uvm/uvm_prot.h>
     54  1.20.2.2     joerg #include <uvm/uvm_readahead.h>
     55  1.20.2.2     joerg 
     56  1.20.2.2     joerg #include <machine/pmap.h>
     57  1.20.2.2     joerg 
     58  1.20.2.2     joerg #include "rump_private.h"
     59  1.20.2.2     joerg #include "rumpuser.h"
     60  1.20.2.2     joerg 
     61  1.20.2.2     joerg /* dumdidumdum */
     62  1.20.2.2     joerg #define len2npages(off, len)						\
     63  1.20.2.2     joerg   (((((len) + PAGE_MASK) & ~(PAGE_MASK)) >> PAGE_SHIFT)			\
     64  1.20.2.2     joerg     + (((off & PAGE_MASK) + (len & PAGE_MASK)) > PAGE_SIZE))
     65  1.20.2.2     joerg 
     66  1.20.2.6     joerg static int vn_get(struct uvm_object *, voff_t, struct vm_page **,
     67  1.20.2.6     joerg 	int *, int, vm_prot_t, int, int);
     68  1.20.2.6     joerg static int vn_put(struct uvm_object *, voff_t, voff_t, int);
     69  1.20.2.6     joerg static int ao_get(struct uvm_object *, voff_t, struct vm_page **,
     70  1.20.2.6     joerg 	int *, int, vm_prot_t, int, int);
     71  1.20.2.6     joerg static int ao_put(struct uvm_object *, voff_t, voff_t, int);
     72  1.20.2.6     joerg 
     73  1.20.2.6     joerg const struct uvm_pagerops uvm_vnodeops = {
     74  1.20.2.6     joerg 	.pgo_get = vn_get,
     75  1.20.2.6     joerg 	.pgo_put = vn_put,
     76  1.20.2.6     joerg };
     77  1.20.2.6     joerg const struct uvm_pagerops aobj_pager = {
     78  1.20.2.6     joerg 	.pgo_get = ao_get,
     79  1.20.2.6     joerg 	.pgo_put = ao_put,
     80  1.20.2.6     joerg };
     81  1.20.2.6     joerg 
     82  1.20.2.2     joerg struct uvmexp uvmexp;
     83  1.20.2.2     joerg struct uvm uvm;
     84  1.20.2.2     joerg 
     85  1.20.2.2     joerg struct vmspace rump_vmspace;
     86  1.20.2.2     joerg struct vm_map rump_vmmap;
     87  1.20.2.2     joerg 
     88  1.20.2.2     joerg /*
     89  1.20.2.2     joerg  * vm pages
     90  1.20.2.2     joerg  */
     91  1.20.2.2     joerg 
     92  1.20.2.5     joerg /* called with the object locked */
     93  1.20.2.2     joerg struct vm_page *
     94  1.20.2.2     joerg rumpvm_makepage(struct uvm_object *uobj, voff_t off)
     95  1.20.2.2     joerg {
     96  1.20.2.2     joerg 	struct vm_page *pg;
     97  1.20.2.2     joerg 
     98  1.20.2.2     joerg 	pg = rumpuser_malloc(sizeof(struct vm_page), 0);
     99  1.20.2.2     joerg 	memset(pg, 0, sizeof(struct vm_page));
    100  1.20.2.2     joerg 	pg->offset = off;
    101  1.20.2.2     joerg 	pg->uobject = uobj;
    102  1.20.2.2     joerg 
    103  1.20.2.2     joerg 	pg->uanon = (void *)rumpuser_malloc(PAGE_SIZE, 0);
    104  1.20.2.2     joerg 	memset((void *)pg->uanon, 0, PAGE_SIZE);
    105  1.20.2.5     joerg 	pg->flags = PG_CLEAN|PG_BUSY|PG_FAKE;
    106  1.20.2.2     joerg 
    107  1.20.2.4  jmcneill 	TAILQ_INSERT_TAIL(&uobj->memq, pg, listq);
    108  1.20.2.4  jmcneill 
    109  1.20.2.2     joerg 	return pg;
    110  1.20.2.2     joerg }
    111  1.20.2.2     joerg 
    112  1.20.2.4  jmcneill /*
    113  1.20.2.4  jmcneill  * Release a page.
    114  1.20.2.4  jmcneill  *
    115  1.20.2.5     joerg  * Called with the vm object locked.
    116  1.20.2.4  jmcneill  */
    117  1.20.2.2     joerg void
    118  1.20.2.5     joerg uvm_pagefree(struct vm_page *pg)
    119  1.20.2.2     joerg {
    120  1.20.2.2     joerg 	struct uvm_object *uobj = pg->uobject;
    121  1.20.2.2     joerg 
    122  1.20.2.5     joerg 	if (pg->flags & PG_WANTED)
    123  1.20.2.5     joerg 		wakeup(pg);
    124  1.20.2.4  jmcneill 
    125  1.20.2.5     joerg 	TAILQ_REMOVE(&uobj->memq, pg, listq);
    126  1.20.2.2     joerg 	rumpuser_free((void *)pg->uanon);
    127  1.20.2.2     joerg 	rumpuser_free(pg);
    128  1.20.2.2     joerg }
    129  1.20.2.2     joerg 
    130  1.20.2.2     joerg struct rumpva {
    131  1.20.2.2     joerg 	vaddr_t addr;
    132  1.20.2.2     joerg 	struct vm_page *pg;
    133  1.20.2.2     joerg 
    134  1.20.2.2     joerg 	LIST_ENTRY(rumpva) entries;
    135  1.20.2.2     joerg };
    136  1.20.2.2     joerg static LIST_HEAD(, rumpva) rvahead = LIST_HEAD_INITIALIZER(rvahead);
    137  1.20.2.4  jmcneill static kmutex_t rvamtx;
    138  1.20.2.2     joerg 
    139  1.20.2.2     joerg void
    140  1.20.2.2     joerg rumpvm_enterva(vaddr_t addr, struct vm_page *pg)
    141  1.20.2.2     joerg {
    142  1.20.2.2     joerg 	struct rumpva *rva;
    143  1.20.2.2     joerg 
    144  1.20.2.2     joerg 	rva = rumpuser_malloc(sizeof(struct rumpva), 0);
    145  1.20.2.2     joerg 	rva->addr = addr;
    146  1.20.2.2     joerg 	rva->pg = pg;
    147  1.20.2.4  jmcneill 	mutex_enter(&rvamtx);
    148  1.20.2.2     joerg 	LIST_INSERT_HEAD(&rvahead, rva, entries);
    149  1.20.2.4  jmcneill 	mutex_exit(&rvamtx);
    150  1.20.2.2     joerg }
    151  1.20.2.2     joerg 
    152  1.20.2.2     joerg void
    153  1.20.2.2     joerg rumpvm_flushva()
    154  1.20.2.2     joerg {
    155  1.20.2.2     joerg 	struct rumpva *rva;
    156  1.20.2.2     joerg 
    157  1.20.2.4  jmcneill 	mutex_enter(&rvamtx);
    158  1.20.2.2     joerg 	while ((rva = LIST_FIRST(&rvahead)) != NULL) {
    159  1.20.2.2     joerg 		LIST_REMOVE(rva, entries);
    160  1.20.2.2     joerg 		rumpuser_free(rva);
    161  1.20.2.2     joerg 	}
    162  1.20.2.4  jmcneill 	mutex_exit(&rvamtx);
    163  1.20.2.2     joerg }
    164  1.20.2.2     joerg 
    165  1.20.2.2     joerg /*
    166  1.20.2.2     joerg  * vnode pager
    167  1.20.2.2     joerg  */
    168  1.20.2.2     joerg 
    169  1.20.2.2     joerg static int
    170  1.20.2.2     joerg vn_get(struct uvm_object *uobj, voff_t off, struct vm_page **pgs,
    171  1.20.2.2     joerg 	int *npages, int centeridx, vm_prot_t access_type,
    172  1.20.2.2     joerg 	int advice, int flags)
    173  1.20.2.2     joerg {
    174  1.20.2.2     joerg 	struct vnode *vp = (struct vnode *)uobj;
    175  1.20.2.2     joerg 
    176  1.20.2.2     joerg 	return VOP_GETPAGES(vp, off, pgs, npages, centeridx, access_type,
    177  1.20.2.2     joerg 	    advice, flags);
    178  1.20.2.2     joerg }
    179  1.20.2.2     joerg 
    180  1.20.2.2     joerg static int
    181  1.20.2.2     joerg vn_put(struct uvm_object *uobj, voff_t offlo, voff_t offhi, int flags)
    182  1.20.2.2     joerg {
    183  1.20.2.2     joerg 	struct vnode *vp = (struct vnode *)uobj;
    184  1.20.2.2     joerg 
    185  1.20.2.2     joerg 	return VOP_PUTPAGES(vp, offlo, offhi, flags);
    186  1.20.2.2     joerg }
    187  1.20.2.2     joerg 
    188  1.20.2.2     joerg /*
    189  1.20.2.2     joerg  * Anon object stuff
    190  1.20.2.2     joerg  */
    191  1.20.2.2     joerg 
    192  1.20.2.2     joerg static int
    193  1.20.2.2     joerg ao_get(struct uvm_object *uobj, voff_t off, struct vm_page **pgs,
    194  1.20.2.2     joerg 	int *npages, int centeridx, vm_prot_t access_type,
    195  1.20.2.2     joerg 	int advice, int flags)
    196  1.20.2.2     joerg {
    197  1.20.2.2     joerg 	struct vm_page *pg;
    198  1.20.2.2     joerg 	int i;
    199  1.20.2.2     joerg 
    200  1.20.2.2     joerg 	if (centeridx)
    201  1.20.2.2     joerg 		panic("%s: centeridx != 0 not supported", __func__);
    202  1.20.2.2     joerg 
    203  1.20.2.2     joerg 	/* loop over pages */
    204  1.20.2.2     joerg 	off = trunc_page(off);
    205  1.20.2.2     joerg 	for (i = 0; i < *npages; i++) {
    206  1.20.2.5     joerg  retrylookup:
    207  1.20.2.2     joerg 		pg = uvm_pagelookup(uobj, off + (i << PAGE_SHIFT));
    208  1.20.2.2     joerg 		if (pg) {
    209  1.20.2.5     joerg 			if (pg->flags & PG_BUSY) {
    210  1.20.2.5     joerg 				pg->flags |= PG_WANTED;
    211  1.20.2.5     joerg 				UVM_UNLOCK_AND_WAIT(pg, &uobj->vmobjlock, 0,
    212  1.20.2.5     joerg 				    "aogetpg", 0);
    213  1.20.2.5     joerg 				goto retrylookup;
    214  1.20.2.5     joerg 			}
    215  1.20.2.5     joerg 			pg->flags |= PG_BUSY;
    216  1.20.2.2     joerg 			pgs[i] = pg;
    217  1.20.2.2     joerg 		} else {
    218  1.20.2.2     joerg 			pg = rumpvm_makepage(uobj, off + (i << PAGE_SHIFT));
    219  1.20.2.2     joerg 			pgs[i] = pg;
    220  1.20.2.2     joerg 		}
    221  1.20.2.2     joerg 	}
    222  1.20.2.5     joerg 	simple_unlock(&uobj->vmobjlock);
    223  1.20.2.2     joerg 
    224  1.20.2.2     joerg 	return 0;
    225  1.20.2.2     joerg 
    226  1.20.2.2     joerg }
    227  1.20.2.2     joerg 
    228  1.20.2.2     joerg static int
    229  1.20.2.2     joerg ao_put(struct uvm_object *uobj, voff_t start, voff_t stop, int flags)
    230  1.20.2.2     joerg {
    231  1.20.2.2     joerg 	struct vm_page *pg;
    232  1.20.2.2     joerg 
    233  1.20.2.2     joerg 	/* we only free all pages for now */
    234  1.20.2.5     joerg 	if ((flags & PGO_FREE) == 0 || (flags & PGO_ALLPAGES) == 0) {
    235  1.20.2.5     joerg 		simple_unlock(&uobj->vmobjlock);
    236  1.20.2.2     joerg 		return 0;
    237  1.20.2.5     joerg 	}
    238  1.20.2.2     joerg 
    239  1.20.2.2     joerg 	while ((pg = TAILQ_FIRST(&uobj->memq)) != NULL)
    240  1.20.2.5     joerg 		uvm_pagefree(pg);
    241  1.20.2.4  jmcneill 	simple_unlock(&uobj->vmobjlock);
    242  1.20.2.2     joerg 
    243  1.20.2.2     joerg 	return 0;
    244  1.20.2.2     joerg }
    245  1.20.2.2     joerg 
    246  1.20.2.2     joerg struct uvm_object *
    247  1.20.2.2     joerg uao_create(vsize_t size, int flags)
    248  1.20.2.2     joerg {
    249  1.20.2.2     joerg 	struct uvm_object *uobj;
    250  1.20.2.2     joerg 
    251  1.20.2.2     joerg 	uobj = rumpuser_malloc(sizeof(struct uvm_object), 0);
    252  1.20.2.2     joerg 	memset(uobj, 0, sizeof(struct uvm_object));
    253  1.20.2.2     joerg 	uobj->pgops = &aobj_pager;
    254  1.20.2.2     joerg 	TAILQ_INIT(&uobj->memq);
    255  1.20.2.5     joerg 	simple_lock_init(&uobj->vmobjlock);
    256  1.20.2.2     joerg 
    257  1.20.2.2     joerg 	return uobj;
    258  1.20.2.2     joerg }
    259  1.20.2.2     joerg 
    260  1.20.2.2     joerg void
    261  1.20.2.2     joerg uao_detach(struct uvm_object *uobj)
    262  1.20.2.2     joerg {
    263  1.20.2.2     joerg 
    264  1.20.2.2     joerg 	ao_put(uobj, 0, 0, PGO_ALLPAGES | PGO_FREE);
    265  1.20.2.2     joerg 	rumpuser_free(uobj);
    266  1.20.2.2     joerg }
    267  1.20.2.2     joerg 
    268  1.20.2.2     joerg /*
    269  1.20.2.2     joerg  * UBC
    270  1.20.2.2     joerg  */
    271  1.20.2.2     joerg 
    272  1.20.2.3     joerg struct ubc_window {
    273  1.20.2.3     joerg 	struct uvm_object	*uwin_obj;
    274  1.20.2.3     joerg 	voff_t			uwin_off;
    275  1.20.2.3     joerg 	uint8_t			*uwin_mem;
    276  1.20.2.3     joerg 	size_t			uwin_mapsize;
    277  1.20.2.3     joerg 
    278  1.20.2.3     joerg 	LIST_ENTRY(ubc_window)	uwin_entries;
    279  1.20.2.3     joerg };
    280  1.20.2.3     joerg 
    281  1.20.2.3     joerg static LIST_HEAD(, ubc_window) uwinlst = LIST_HEAD_INITIALIZER(uwinlst);
    282  1.20.2.4  jmcneill static kmutex_t uwinmtx;
    283  1.20.2.3     joerg 
    284  1.20.2.2     joerg int
    285  1.20.2.4  jmcneill rump_ubc_magic_uiomove(void *va, size_t n, struct uio *uio, int *rvp,
    286  1.20.2.4  jmcneill 	struct ubc_window *uwinp)
    287  1.20.2.2     joerg {
    288  1.20.2.2     joerg 	struct vm_page **pgs;
    289  1.20.2.2     joerg 	int npages = len2npages(uio->uio_offset, n);
    290  1.20.2.5     joerg 	size_t allocsize;
    291  1.20.2.2     joerg 	int i, rv;
    292  1.20.2.2     joerg 
    293  1.20.2.3     joerg 	if (uwinp == NULL) {
    294  1.20.2.4  jmcneill 		mutex_enter(&uwinmtx);
    295  1.20.2.4  jmcneill 		LIST_FOREACH(uwinp, &uwinlst, uwin_entries)
    296  1.20.2.4  jmcneill 			if ((uint8_t *)va >= uwinp->uwin_mem
    297  1.20.2.4  jmcneill 			    && (uint8_t *)va
    298  1.20.2.4  jmcneill 			      < (uwinp->uwin_mem + uwinp->uwin_mapsize))
    299  1.20.2.4  jmcneill 				break;
    300  1.20.2.4  jmcneill 		mutex_exit(&uwinmtx);
    301  1.20.2.4  jmcneill 		if (uwinp == NULL) {
    302  1.20.2.4  jmcneill 			KASSERT(rvp != NULL);
    303  1.20.2.4  jmcneill 			return 0;
    304  1.20.2.4  jmcneill 		}
    305  1.20.2.3     joerg 	}
    306  1.20.2.2     joerg 
    307  1.20.2.5     joerg 	allocsize = npages * sizeof(pgs);
    308  1.20.2.5     joerg 	pgs = kmem_zalloc(allocsize, KM_SLEEP);
    309  1.20.2.5     joerg 	simple_lock(&uwinp->uwin_obj->vmobjlock);
    310  1.20.2.3     joerg 	rv = uwinp->uwin_obj->pgops->pgo_get(uwinp->uwin_obj,
    311  1.20.2.3     joerg 	    uwinp->uwin_off + ((uint8_t *)va - uwinp->uwin_mem),
    312  1.20.2.2     joerg 	    pgs, &npages, 0, 0, 0, 0);
    313  1.20.2.2     joerg 	if (rv)
    314  1.20.2.2     joerg 		goto out;
    315  1.20.2.2     joerg 
    316  1.20.2.2     joerg 	for (i = 0; i < npages; i++) {
    317  1.20.2.2     joerg 		size_t xfersize;
    318  1.20.2.2     joerg 		off_t pageoff;
    319  1.20.2.2     joerg 
    320  1.20.2.2     joerg 		pageoff = uio->uio_offset & PAGE_MASK;
    321  1.20.2.2     joerg 		xfersize = MIN(MIN(n, PAGE_SIZE), PAGE_SIZE-pageoff);
    322  1.20.2.2     joerg 		uiomove((uint8_t *)pgs[i]->uanon + pageoff, xfersize, uio);
    323  1.20.2.2     joerg 		if (uio->uio_rw == UIO_WRITE)
    324  1.20.2.2     joerg 			pgs[i]->flags &= ~PG_CLEAN;
    325  1.20.2.2     joerg 		n -= xfersize;
    326  1.20.2.2     joerg 	}
    327  1.20.2.5     joerg 	uvm_page_unbusy(pgs, npages);
    328  1.20.2.2     joerg 
    329  1.20.2.2     joerg  out:
    330  1.20.2.5     joerg 	kmem_free(pgs, allocsize);
    331  1.20.2.3     joerg 	if (rvp)
    332  1.20.2.3     joerg 		*rvp = rv;
    333  1.20.2.3     joerg 	return 1;
    334  1.20.2.2     joerg }
    335  1.20.2.2     joerg 
    336  1.20.2.4  jmcneill static struct ubc_window *
    337  1.20.2.4  jmcneill uwin_alloc(struct uvm_object *uobj, voff_t off, vsize_t len)
    338  1.20.2.2     joerg {
    339  1.20.2.3     joerg 	struct ubc_window *uwinp; /* pronounced: you wimp! */
    340  1.20.2.2     joerg 
    341  1.20.2.3     joerg 	uwinp = kmem_alloc(sizeof(struct ubc_window), KM_SLEEP);
    342  1.20.2.3     joerg 	uwinp->uwin_obj = uobj;
    343  1.20.2.4  jmcneill 	uwinp->uwin_off = off;
    344  1.20.2.4  jmcneill 	uwinp->uwin_mapsize = len;
    345  1.20.2.4  jmcneill 	uwinp->uwin_mem = kmem_alloc(len, KM_SLEEP);
    346  1.20.2.3     joerg 
    347  1.20.2.4  jmcneill 	return uwinp;
    348  1.20.2.4  jmcneill }
    349  1.20.2.4  jmcneill 
    350  1.20.2.4  jmcneill static void
    351  1.20.2.4  jmcneill uwin_free(struct ubc_window *uwinp)
    352  1.20.2.4  jmcneill {
    353  1.20.2.4  jmcneill 
    354  1.20.2.4  jmcneill 	kmem_free(uwinp->uwin_mem, uwinp->uwin_mapsize);
    355  1.20.2.4  jmcneill 	kmem_free(uwinp, sizeof(struct ubc_window));
    356  1.20.2.4  jmcneill }
    357  1.20.2.4  jmcneill 
    358  1.20.2.4  jmcneill void *
    359  1.20.2.4  jmcneill ubc_alloc(struct uvm_object *uobj, voff_t offset, vsize_t *lenp, int advice,
    360  1.20.2.4  jmcneill 	int flags)
    361  1.20.2.4  jmcneill {
    362  1.20.2.4  jmcneill 	struct ubc_window *uwinp;
    363  1.20.2.4  jmcneill 
    364  1.20.2.4  jmcneill 	uwinp = uwin_alloc(uobj, offset, *lenp);
    365  1.20.2.4  jmcneill 	mutex_enter(&uwinmtx);
    366  1.20.2.3     joerg 	LIST_INSERT_HEAD(&uwinlst, uwinp, uwin_entries);
    367  1.20.2.4  jmcneill 	mutex_exit(&uwinmtx);
    368  1.20.2.3     joerg 
    369  1.20.2.3     joerg 	DPRINTF(("UBC_ALLOC offset 0x%llx, uwin %p, mem %p\n",
    370  1.20.2.3     joerg 	    (unsigned long long)offset, uwinp, uwinp->uwin_mem));
    371  1.20.2.3     joerg 
    372  1.20.2.3     joerg 	return uwinp->uwin_mem;
    373  1.20.2.2     joerg }
    374  1.20.2.2     joerg 
    375  1.20.2.2     joerg void
    376  1.20.2.2     joerg ubc_release(void *va, int flags)
    377  1.20.2.2     joerg {
    378  1.20.2.3     joerg 	struct ubc_window *uwinp;
    379  1.20.2.2     joerg 
    380  1.20.2.4  jmcneill 	mutex_enter(&uwinmtx);
    381  1.20.2.3     joerg 	LIST_FOREACH(uwinp, &uwinlst, uwin_entries)
    382  1.20.2.3     joerg 		if ((uint8_t *)va >= uwinp->uwin_mem
    383  1.20.2.3     joerg 		    && (uint8_t *)va < (uwinp->uwin_mem + uwinp->uwin_mapsize))
    384  1.20.2.3     joerg 			break;
    385  1.20.2.4  jmcneill 	mutex_exit(&uwinmtx);
    386  1.20.2.3     joerg 	if (uwinp == NULL)
    387  1.20.2.3     joerg 		panic("%s: releasing invalid window at %p", __func__, va);
    388  1.20.2.3     joerg 
    389  1.20.2.3     joerg 	LIST_REMOVE(uwinp, uwin_entries);
    390  1.20.2.4  jmcneill 	uwin_free(uwinp);
    391  1.20.2.2     joerg }
    392  1.20.2.2     joerg 
    393  1.20.2.2     joerg int
    394  1.20.2.2     joerg ubc_uiomove(struct uvm_object *uobj, struct uio *uio, vsize_t todo,
    395  1.20.2.2     joerg 	int advice, int flags)
    396  1.20.2.2     joerg {
    397  1.20.2.4  jmcneill 	struct ubc_window *uwinp;
    398  1.20.2.2     joerg 	vsize_t len;
    399  1.20.2.2     joerg 
    400  1.20.2.2     joerg 	while (todo > 0) {
    401  1.20.2.2     joerg 		len = todo;
    402  1.20.2.2     joerg 
    403  1.20.2.4  jmcneill 		uwinp = uwin_alloc(uobj, uio->uio_offset, len);
    404  1.20.2.4  jmcneill 		rump_ubc_magic_uiomove(uwinp->uwin_mem, len, uio, NULL, uwinp);
    405  1.20.2.4  jmcneill 		uwin_free(uwinp);
    406  1.20.2.2     joerg 
    407  1.20.2.2     joerg 		todo -= len;
    408  1.20.2.2     joerg 	}
    409  1.20.2.2     joerg 	return 0;
    410  1.20.2.2     joerg }
    411  1.20.2.2     joerg 
    412  1.20.2.2     joerg 
    413  1.20.2.2     joerg /*
    414  1.20.2.2     joerg  * Misc routines
    415  1.20.2.2     joerg  */
    416  1.20.2.2     joerg 
    417  1.20.2.2     joerg void
    418  1.20.2.2     joerg rumpvm_init()
    419  1.20.2.2     joerg {
    420  1.20.2.2     joerg 
    421  1.20.2.2     joerg 	uvmexp.free = 1024*1024; /* XXX */
    422  1.20.2.2     joerg 	uvm.pagedaemon_lwp = NULL; /* doesn't match curlwp */
    423  1.20.2.4  jmcneill 
    424  1.20.2.4  jmcneill 	mutex_init(&rvamtx, MUTEX_DEFAULT, 0);
    425  1.20.2.4  jmcneill 	mutex_init(&uwinmtx, MUTEX_DEFAULT, 0);
    426  1.20.2.2     joerg }
    427  1.20.2.2     joerg 
    428  1.20.2.2     joerg void
    429  1.20.2.2     joerg uvm_pageactivate(struct vm_page *pg)
    430  1.20.2.2     joerg {
    431  1.20.2.2     joerg 
    432  1.20.2.2     joerg 	/* nada */
    433  1.20.2.2     joerg }
    434  1.20.2.2     joerg 
    435  1.20.2.2     joerg void
    436  1.20.2.2     joerg uvm_pagewire(struct vm_page *pg)
    437  1.20.2.2     joerg {
    438  1.20.2.2     joerg 
    439  1.20.2.2     joerg 	/* nada */
    440  1.20.2.2     joerg }
    441  1.20.2.2     joerg 
    442  1.20.2.2     joerg void
    443  1.20.2.2     joerg uvm_pageunwire(struct vm_page *pg)
    444  1.20.2.2     joerg {
    445  1.20.2.2     joerg 
    446  1.20.2.2     joerg 	/* nada */
    447  1.20.2.2     joerg }
    448  1.20.2.2     joerg 
    449  1.20.2.2     joerg vaddr_t
    450  1.20.2.2     joerg uvm_pagermapin(struct vm_page **pps, int npages, int flags)
    451  1.20.2.2     joerg {
    452  1.20.2.2     joerg 
    453  1.20.2.2     joerg 	panic("%s: unimplemented", __func__);
    454  1.20.2.2     joerg }
    455  1.20.2.2     joerg 
    456  1.20.2.5     joerg /* Called with the vm object locked */
    457  1.20.2.2     joerg struct vm_page *
    458  1.20.2.2     joerg uvm_pagelookup(struct uvm_object *uobj, voff_t off)
    459  1.20.2.2     joerg {
    460  1.20.2.2     joerg 	struct vm_page *pg;
    461  1.20.2.2     joerg 
    462  1.20.2.4  jmcneill 	TAILQ_FOREACH(pg, &uobj->memq, listq) {
    463  1.20.2.4  jmcneill 		if (pg->offset == off) {
    464  1.20.2.4  jmcneill 			simple_unlock(&uobj->vmobjlock);
    465  1.20.2.2     joerg 			return pg;
    466  1.20.2.4  jmcneill 		}
    467  1.20.2.4  jmcneill 	}
    468  1.20.2.2     joerg 
    469  1.20.2.2     joerg 	return NULL;
    470  1.20.2.2     joerg }
    471  1.20.2.2     joerg 
    472  1.20.2.2     joerg struct vm_page *
    473  1.20.2.2     joerg uvm_pageratop(vaddr_t va)
    474  1.20.2.2     joerg {
    475  1.20.2.2     joerg 	struct rumpva *rva;
    476  1.20.2.2     joerg 
    477  1.20.2.4  jmcneill 	mutex_enter(&rvamtx);
    478  1.20.2.2     joerg 	LIST_FOREACH(rva, &rvahead, entries)
    479  1.20.2.2     joerg 		if (rva->addr == va)
    480  1.20.2.4  jmcneill 			break;
    481  1.20.2.4  jmcneill 	mutex_exit(&rvamtx);
    482  1.20.2.4  jmcneill 
    483  1.20.2.4  jmcneill 	if (rva == NULL)
    484  1.20.2.4  jmcneill 		panic("%s: va %llu", __func__, (unsigned long long)va);
    485  1.20.2.2     joerg 
    486  1.20.2.4  jmcneill 	return rva->pg;
    487  1.20.2.2     joerg }
    488  1.20.2.2     joerg 
    489  1.20.2.2     joerg void
    490  1.20.2.5     joerg uvm_page_unbusy(struct vm_page **pgs, int npgs)
    491  1.20.2.5     joerg {
    492  1.20.2.5     joerg 	struct vm_page *pg;
    493  1.20.2.5     joerg 	int i;
    494  1.20.2.5     joerg 
    495  1.20.2.5     joerg 	for (i = 0; i < npgs; i++) {
    496  1.20.2.5     joerg 		pg = pgs[i];
    497  1.20.2.5     joerg 		if (pg == NULL)
    498  1.20.2.5     joerg 			continue;
    499  1.20.2.5     joerg 
    500  1.20.2.5     joerg 		KASSERT(pg->flags & PG_BUSY);
    501  1.20.2.5     joerg 		if (pg->flags & PG_WANTED)
    502  1.20.2.5     joerg 			wakeup(pg);
    503  1.20.2.5     joerg 		pg->flags &= ~(PG_WANTED|PG_BUSY);
    504  1.20.2.5     joerg 	}
    505  1.20.2.5     joerg }
    506  1.20.2.5     joerg 
    507  1.20.2.5     joerg void
    508  1.20.2.2     joerg uvm_estimatepageable(int *active, int *inactive)
    509  1.20.2.2     joerg {
    510  1.20.2.2     joerg 
    511  1.20.2.2     joerg 	/* XXX: guessing game */
    512  1.20.2.2     joerg 	*active = 1024;
    513  1.20.2.2     joerg 	*inactive = 1024;
    514  1.20.2.2     joerg }
    515  1.20.2.2     joerg 
    516  1.20.2.2     joerg void
    517  1.20.2.2     joerg uvm_aio_biodone1(struct buf *bp)
    518  1.20.2.2     joerg {
    519  1.20.2.2     joerg 
    520  1.20.2.2     joerg 	panic("%s: unimplemented", __func__);
    521  1.20.2.2     joerg }
    522  1.20.2.2     joerg 
    523  1.20.2.2     joerg void
    524  1.20.2.2     joerg uvm_aio_biodone(struct buf *bp)
    525  1.20.2.2     joerg {
    526  1.20.2.2     joerg 
    527  1.20.2.2     joerg 	uvm_aio_aiodone(bp);
    528  1.20.2.2     joerg }
    529  1.20.2.2     joerg 
    530  1.20.2.2     joerg void
    531  1.20.2.2     joerg uvm_aio_aiodone(struct buf *bp)
    532  1.20.2.2     joerg {
    533  1.20.2.2     joerg 
    534  1.20.2.2     joerg 	if ((bp->b_flags & (B_READ | B_NOCACHE)) == 0 && bioopsp)
    535  1.20.2.2     joerg 		bioopsp->io_pageiodone(bp);
    536  1.20.2.2     joerg }
    537  1.20.2.2     joerg 
    538  1.20.2.2     joerg void
    539  1.20.2.2     joerg uvm_vnp_setsize(struct vnode *vp, voff_t newsize)
    540  1.20.2.2     joerg {
    541  1.20.2.2     joerg 
    542  1.20.2.2     joerg 	vp->v_size = vp->v_writesize = newsize;
    543  1.20.2.2     joerg }
    544  1.20.2.2     joerg 
    545  1.20.2.2     joerg void
    546  1.20.2.2     joerg uvm_vnp_setwritesize(struct vnode *vp, voff_t newsize)
    547  1.20.2.2     joerg {
    548  1.20.2.2     joerg 
    549  1.20.2.2     joerg 	vp->v_writesize = newsize;
    550  1.20.2.2     joerg }
    551  1.20.2.2     joerg 
    552  1.20.2.2     joerg void
    553  1.20.2.2     joerg uvm_vnp_zerorange(struct vnode *vp, off_t off, size_t len)
    554  1.20.2.2     joerg {
    555  1.20.2.2     joerg 	struct uvm_object *uobj = &vp->v_uobj;
    556  1.20.2.2     joerg 	struct vm_page **pgs;
    557  1.20.2.2     joerg 	int maxpages = MIN(32, round_page(len) >> PAGE_SHIFT);
    558  1.20.2.2     joerg 	int rv, npages, i;
    559  1.20.2.2     joerg 
    560  1.20.2.5     joerg 	pgs = kmem_zalloc(maxpages * sizeof(pgs), KM_SLEEP);
    561  1.20.2.2     joerg 	while (len) {
    562  1.20.2.2     joerg 		npages = MIN(maxpages, round_page(len) >> PAGE_SHIFT);
    563  1.20.2.2     joerg 		memset(pgs, 0, npages * sizeof(struct vm_page *));
    564  1.20.2.5     joerg 		simple_lock(&uobj->vmobjlock);
    565  1.20.2.2     joerg 		rv = uobj->pgops->pgo_get(uobj, off, pgs, &npages, 0, 0, 0, 0);
    566  1.20.2.2     joerg 		assert(npages > 0);
    567  1.20.2.2     joerg 
    568  1.20.2.2     joerg 		for (i = 0; i < npages; i++) {
    569  1.20.2.2     joerg 			uint8_t *start;
    570  1.20.2.2     joerg 			size_t chunkoff, chunklen;
    571  1.20.2.2     joerg 
    572  1.20.2.2     joerg 			chunkoff = off & PAGE_MASK;
    573  1.20.2.2     joerg 			chunklen = MIN(PAGE_SIZE - chunkoff, len);
    574  1.20.2.2     joerg 			start = (uint8_t *)pgs[i]->uanon + chunkoff;
    575  1.20.2.2     joerg 
    576  1.20.2.2     joerg 			memset(start, 0, chunklen);
    577  1.20.2.2     joerg 			pgs[i]->flags &= PG_CLEAN;
    578  1.20.2.2     joerg 
    579  1.20.2.2     joerg 			off += chunklen;
    580  1.20.2.2     joerg 			len -= chunklen;
    581  1.20.2.2     joerg 		}
    582  1.20.2.5     joerg 		uvm_page_unbusy(pgs, npages);
    583  1.20.2.2     joerg 	}
    584  1.20.2.5     joerg 	kmem_free(pgs, maxpages * sizeof(pgs));
    585  1.20.2.2     joerg 
    586  1.20.2.2     joerg 	return;
    587  1.20.2.2     joerg }
    588  1.20.2.2     joerg 
    589  1.20.2.2     joerg struct uvm_ractx *
    590  1.20.2.2     joerg uvm_ra_allocctx()
    591  1.20.2.2     joerg {
    592  1.20.2.2     joerg 
    593  1.20.2.2     joerg 	return NULL;
    594  1.20.2.2     joerg }
    595  1.20.2.2     joerg 
    596  1.20.2.2     joerg void
    597  1.20.2.2     joerg uvm_ra_freectx(struct uvm_ractx *ra)
    598  1.20.2.2     joerg {
    599  1.20.2.2     joerg 
    600  1.20.2.2     joerg 	return;
    601  1.20.2.2     joerg }
    602  1.20.2.2     joerg 
    603  1.20.2.2     joerg bool
    604  1.20.2.2     joerg uvn_clean_p(struct uvm_object *uobj)
    605  1.20.2.2     joerg {
    606  1.20.2.2     joerg 	struct vnode *vp = (void *)uobj;
    607  1.20.2.2     joerg 
    608  1.20.2.2     joerg 	return (vp->v_iflag & VI_ONWORKLST) == 0;
    609  1.20.2.2     joerg }
    610  1.20.2.2     joerg 
    611  1.20.2.2     joerg /*
    612  1.20.2.2     joerg  * Kmem
    613  1.20.2.2     joerg  */
    614  1.20.2.2     joerg 
    615  1.20.2.2     joerg void *
    616  1.20.2.2     joerg kmem_alloc(size_t size, km_flag_t kmflag)
    617  1.20.2.2     joerg {
    618  1.20.2.2     joerg 
    619  1.20.2.2     joerg 	return rumpuser_malloc(size, kmflag == KM_NOSLEEP);
    620  1.20.2.2     joerg }
    621  1.20.2.2     joerg 
    622  1.20.2.2     joerg void *
    623  1.20.2.2     joerg kmem_zalloc(size_t size, km_flag_t kmflag)
    624  1.20.2.2     joerg {
    625  1.20.2.2     joerg 	void *rv;
    626  1.20.2.2     joerg 
    627  1.20.2.2     joerg 	rv = kmem_alloc(size, kmflag);
    628  1.20.2.2     joerg 	if (rv)
    629  1.20.2.2     joerg 		memset(rv, 0, size);
    630  1.20.2.2     joerg 
    631  1.20.2.2     joerg 	return rv;
    632  1.20.2.2     joerg }
    633  1.20.2.2     joerg 
    634  1.20.2.2     joerg void
    635  1.20.2.2     joerg kmem_free(void *p, size_t size)
    636  1.20.2.2     joerg {
    637  1.20.2.2     joerg 
    638  1.20.2.2     joerg 	rumpuser_free(p);
    639  1.20.2.2     joerg }
    640  1.20.2.2     joerg 
    641  1.20.2.2     joerg /*
    642  1.20.2.2     joerg  * UVM km
    643  1.20.2.2     joerg  */
    644  1.20.2.2     joerg 
    645  1.20.2.2     joerg vaddr_t
    646  1.20.2.2     joerg uvm_km_alloc(struct vm_map *map, vsize_t size, vsize_t align, uvm_flag_t flags)
    647  1.20.2.2     joerg {
    648  1.20.2.2     joerg 	void *rv;
    649  1.20.2.2     joerg 
    650  1.20.2.2     joerg 	rv = rumpuser_malloc(size, flags & (UVM_KMF_CANFAIL | UVM_KMF_NOWAIT));
    651  1.20.2.2     joerg 	if (rv && flags & UVM_KMF_ZERO)
    652  1.20.2.2     joerg 		memset(rv, 0, size);
    653  1.20.2.2     joerg 
    654  1.20.2.2     joerg 	return (vaddr_t)rv;
    655  1.20.2.2     joerg }
    656  1.20.2.2     joerg 
    657  1.20.2.2     joerg void
    658  1.20.2.2     joerg uvm_km_free(struct vm_map *map, vaddr_t vaddr, vsize_t size, uvm_flag_t flags)
    659  1.20.2.2     joerg {
    660  1.20.2.2     joerg 
    661  1.20.2.2     joerg 	rumpuser_free((void *)vaddr);
    662  1.20.2.2     joerg }
    663  1.20.2.2     joerg 
    664  1.20.2.2     joerg struct vm_map *
    665  1.20.2.2     joerg uvm_km_suballoc(struct vm_map *map, vaddr_t *minaddr, vaddr_t *maxaddr,
    666  1.20.2.2     joerg 	vsize_t size, int pageable, bool fixed, struct vm_map_kernel *submap)
    667  1.20.2.2     joerg {
    668  1.20.2.2     joerg 
    669  1.20.2.2     joerg 	return (struct vm_map *)417416;
    670  1.20.2.2     joerg }
    671