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