Home | History | Annotate | Line # | Download | only in rumpkern
vm.c revision 1.20.2.5
      1  1.20.2.5     joerg /*	$NetBSD: vm.c,v 1.20.2.5 2007/11/06 19:25:37 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.2     joerg struct uvm_pagerops uvm_vnodeops;
     67  1.20.2.2     joerg struct uvm_pagerops aobj_pager;
     68  1.20.2.2     joerg struct uvmexp uvmexp;
     69  1.20.2.2     joerg struct uvm uvm;
     70  1.20.2.2     joerg 
     71  1.20.2.2     joerg struct vmspace rump_vmspace;
     72  1.20.2.2     joerg struct vm_map rump_vmmap;
     73  1.20.2.2     joerg 
     74  1.20.2.2     joerg /*
     75  1.20.2.2     joerg  * vm pages
     76  1.20.2.2     joerg  */
     77  1.20.2.2     joerg 
     78  1.20.2.5     joerg /* called with the object locked */
     79  1.20.2.2     joerg struct vm_page *
     80  1.20.2.2     joerg rumpvm_makepage(struct uvm_object *uobj, voff_t off)
     81  1.20.2.2     joerg {
     82  1.20.2.2     joerg 	struct vm_page *pg;
     83  1.20.2.2     joerg 
     84  1.20.2.2     joerg 	pg = rumpuser_malloc(sizeof(struct vm_page), 0);
     85  1.20.2.2     joerg 	memset(pg, 0, sizeof(struct vm_page));
     86  1.20.2.2     joerg 	pg->offset = off;
     87  1.20.2.2     joerg 	pg->uobject = uobj;
     88  1.20.2.2     joerg 
     89  1.20.2.2     joerg 	pg->uanon = (void *)rumpuser_malloc(PAGE_SIZE, 0);
     90  1.20.2.2     joerg 	memset((void *)pg->uanon, 0, PAGE_SIZE);
     91  1.20.2.5     joerg 	pg->flags = PG_CLEAN|PG_BUSY|PG_FAKE;
     92  1.20.2.2     joerg 
     93  1.20.2.4  jmcneill 	TAILQ_INSERT_TAIL(&uobj->memq, pg, listq);
     94  1.20.2.4  jmcneill 
     95  1.20.2.2     joerg 	return pg;
     96  1.20.2.2     joerg }
     97  1.20.2.2     joerg 
     98  1.20.2.4  jmcneill /*
     99  1.20.2.4  jmcneill  * Release a page.
    100  1.20.2.4  jmcneill  *
    101  1.20.2.5     joerg  * Called with the vm object locked.
    102  1.20.2.4  jmcneill  */
    103  1.20.2.2     joerg void
    104  1.20.2.5     joerg uvm_pagefree(struct vm_page *pg)
    105  1.20.2.2     joerg {
    106  1.20.2.2     joerg 	struct uvm_object *uobj = pg->uobject;
    107  1.20.2.2     joerg 
    108  1.20.2.5     joerg 	if (pg->flags & PG_WANTED)
    109  1.20.2.5     joerg 		wakeup(pg);
    110  1.20.2.4  jmcneill 
    111  1.20.2.5     joerg 	TAILQ_REMOVE(&uobj->memq, pg, listq);
    112  1.20.2.2     joerg 	rumpuser_free((void *)pg->uanon);
    113  1.20.2.2     joerg 	rumpuser_free(pg);
    114  1.20.2.2     joerg }
    115  1.20.2.2     joerg 
    116  1.20.2.2     joerg struct rumpva {
    117  1.20.2.2     joerg 	vaddr_t addr;
    118  1.20.2.2     joerg 	struct vm_page *pg;
    119  1.20.2.2     joerg 
    120  1.20.2.2     joerg 	LIST_ENTRY(rumpva) entries;
    121  1.20.2.2     joerg };
    122  1.20.2.2     joerg static LIST_HEAD(, rumpva) rvahead = LIST_HEAD_INITIALIZER(rvahead);
    123  1.20.2.4  jmcneill static kmutex_t rvamtx;
    124  1.20.2.2     joerg 
    125  1.20.2.2     joerg void
    126  1.20.2.2     joerg rumpvm_enterva(vaddr_t addr, struct vm_page *pg)
    127  1.20.2.2     joerg {
    128  1.20.2.2     joerg 	struct rumpva *rva;
    129  1.20.2.2     joerg 
    130  1.20.2.2     joerg 	rva = rumpuser_malloc(sizeof(struct rumpva), 0);
    131  1.20.2.2     joerg 	rva->addr = addr;
    132  1.20.2.2     joerg 	rva->pg = pg;
    133  1.20.2.4  jmcneill 	mutex_enter(&rvamtx);
    134  1.20.2.2     joerg 	LIST_INSERT_HEAD(&rvahead, rva, entries);
    135  1.20.2.4  jmcneill 	mutex_exit(&rvamtx);
    136  1.20.2.2     joerg }
    137  1.20.2.2     joerg 
    138  1.20.2.2     joerg void
    139  1.20.2.2     joerg rumpvm_flushva()
    140  1.20.2.2     joerg {
    141  1.20.2.2     joerg 	struct rumpva *rva;
    142  1.20.2.2     joerg 
    143  1.20.2.4  jmcneill 	mutex_enter(&rvamtx);
    144  1.20.2.2     joerg 	while ((rva = LIST_FIRST(&rvahead)) != NULL) {
    145  1.20.2.2     joerg 		LIST_REMOVE(rva, entries);
    146  1.20.2.2     joerg 		rumpuser_free(rva);
    147  1.20.2.2     joerg 	}
    148  1.20.2.4  jmcneill 	mutex_exit(&rvamtx);
    149  1.20.2.2     joerg }
    150  1.20.2.2     joerg 
    151  1.20.2.2     joerg /*
    152  1.20.2.2     joerg  * vnode pager
    153  1.20.2.2     joerg  */
    154  1.20.2.2     joerg 
    155  1.20.2.2     joerg static int
    156  1.20.2.2     joerg vn_get(struct uvm_object *uobj, voff_t off, struct vm_page **pgs,
    157  1.20.2.2     joerg 	int *npages, int centeridx, vm_prot_t access_type,
    158  1.20.2.2     joerg 	int advice, int flags)
    159  1.20.2.2     joerg {
    160  1.20.2.2     joerg 	struct vnode *vp = (struct vnode *)uobj;
    161  1.20.2.2     joerg 
    162  1.20.2.2     joerg 	return VOP_GETPAGES(vp, off, pgs, npages, centeridx, access_type,
    163  1.20.2.2     joerg 	    advice, flags);
    164  1.20.2.2     joerg }
    165  1.20.2.2     joerg 
    166  1.20.2.2     joerg static int
    167  1.20.2.2     joerg vn_put(struct uvm_object *uobj, voff_t offlo, voff_t offhi, int flags)
    168  1.20.2.2     joerg {
    169  1.20.2.2     joerg 	struct vnode *vp = (struct vnode *)uobj;
    170  1.20.2.2     joerg 
    171  1.20.2.2     joerg 	return VOP_PUTPAGES(vp, offlo, offhi, flags);
    172  1.20.2.2     joerg }
    173  1.20.2.2     joerg 
    174  1.20.2.2     joerg /*
    175  1.20.2.2     joerg  * Anon object stuff
    176  1.20.2.2     joerg  */
    177  1.20.2.2     joerg 
    178  1.20.2.2     joerg static int
    179  1.20.2.2     joerg ao_get(struct uvm_object *uobj, voff_t off, struct vm_page **pgs,
    180  1.20.2.2     joerg 	int *npages, int centeridx, vm_prot_t access_type,
    181  1.20.2.2     joerg 	int advice, int flags)
    182  1.20.2.2     joerg {
    183  1.20.2.2     joerg 	struct vm_page *pg;
    184  1.20.2.2     joerg 	int i;
    185  1.20.2.2     joerg 
    186  1.20.2.2     joerg 	if (centeridx)
    187  1.20.2.2     joerg 		panic("%s: centeridx != 0 not supported", __func__);
    188  1.20.2.2     joerg 
    189  1.20.2.2     joerg 	/* loop over pages */
    190  1.20.2.2     joerg 	off = trunc_page(off);
    191  1.20.2.2     joerg 	for (i = 0; i < *npages; i++) {
    192  1.20.2.5     joerg  retrylookup:
    193  1.20.2.2     joerg 		pg = uvm_pagelookup(uobj, off + (i << PAGE_SHIFT));
    194  1.20.2.2     joerg 		if (pg) {
    195  1.20.2.5     joerg 			if (pg->flags & PG_BUSY) {
    196  1.20.2.5     joerg 				pg->flags |= PG_WANTED;
    197  1.20.2.5     joerg 				UVM_UNLOCK_AND_WAIT(pg, &uobj->vmobjlock, 0,
    198  1.20.2.5     joerg 				    "aogetpg", 0);
    199  1.20.2.5     joerg 				goto retrylookup;
    200  1.20.2.5     joerg 			}
    201  1.20.2.5     joerg 			pg->flags |= PG_BUSY;
    202  1.20.2.2     joerg 			pgs[i] = pg;
    203  1.20.2.2     joerg 		} else {
    204  1.20.2.2     joerg 			pg = rumpvm_makepage(uobj, off + (i << PAGE_SHIFT));
    205  1.20.2.2     joerg 			pgs[i] = pg;
    206  1.20.2.2     joerg 		}
    207  1.20.2.2     joerg 	}
    208  1.20.2.5     joerg 	simple_unlock(&uobj->vmobjlock);
    209  1.20.2.2     joerg 
    210  1.20.2.2     joerg 	return 0;
    211  1.20.2.2     joerg 
    212  1.20.2.2     joerg }
    213  1.20.2.2     joerg 
    214  1.20.2.2     joerg static int
    215  1.20.2.2     joerg ao_put(struct uvm_object *uobj, voff_t start, voff_t stop, int flags)
    216  1.20.2.2     joerg {
    217  1.20.2.2     joerg 	struct vm_page *pg;
    218  1.20.2.2     joerg 
    219  1.20.2.2     joerg 	/* we only free all pages for now */
    220  1.20.2.5     joerg 	if ((flags & PGO_FREE) == 0 || (flags & PGO_ALLPAGES) == 0) {
    221  1.20.2.5     joerg 		simple_unlock(&uobj->vmobjlock);
    222  1.20.2.2     joerg 		return 0;
    223  1.20.2.5     joerg 	}
    224  1.20.2.2     joerg 
    225  1.20.2.2     joerg 	while ((pg = TAILQ_FIRST(&uobj->memq)) != NULL)
    226  1.20.2.5     joerg 		uvm_pagefree(pg);
    227  1.20.2.4  jmcneill 	simple_unlock(&uobj->vmobjlock);
    228  1.20.2.2     joerg 
    229  1.20.2.2     joerg 	return 0;
    230  1.20.2.2     joerg }
    231  1.20.2.2     joerg 
    232  1.20.2.2     joerg struct uvm_object *
    233  1.20.2.2     joerg uao_create(vsize_t size, int flags)
    234  1.20.2.2     joerg {
    235  1.20.2.2     joerg 	struct uvm_object *uobj;
    236  1.20.2.2     joerg 
    237  1.20.2.2     joerg 	uobj = rumpuser_malloc(sizeof(struct uvm_object), 0);
    238  1.20.2.2     joerg 	memset(uobj, 0, sizeof(struct uvm_object));
    239  1.20.2.2     joerg 	uobj->pgops = &aobj_pager;
    240  1.20.2.2     joerg 	TAILQ_INIT(&uobj->memq);
    241  1.20.2.5     joerg 	simple_lock_init(&uobj->vmobjlock);
    242  1.20.2.2     joerg 
    243  1.20.2.2     joerg 	return uobj;
    244  1.20.2.2     joerg }
    245  1.20.2.2     joerg 
    246  1.20.2.2     joerg void
    247  1.20.2.2     joerg uao_detach(struct uvm_object *uobj)
    248  1.20.2.2     joerg {
    249  1.20.2.2     joerg 
    250  1.20.2.2     joerg 	ao_put(uobj, 0, 0, PGO_ALLPAGES | PGO_FREE);
    251  1.20.2.2     joerg 	rumpuser_free(uobj);
    252  1.20.2.2     joerg }
    253  1.20.2.2     joerg 
    254  1.20.2.2     joerg /*
    255  1.20.2.2     joerg  * UBC
    256  1.20.2.2     joerg  */
    257  1.20.2.2     joerg 
    258  1.20.2.3     joerg struct ubc_window {
    259  1.20.2.3     joerg 	struct uvm_object	*uwin_obj;
    260  1.20.2.3     joerg 	voff_t			uwin_off;
    261  1.20.2.3     joerg 	uint8_t			*uwin_mem;
    262  1.20.2.3     joerg 	size_t			uwin_mapsize;
    263  1.20.2.3     joerg 
    264  1.20.2.3     joerg 	LIST_ENTRY(ubc_window)	uwin_entries;
    265  1.20.2.3     joerg };
    266  1.20.2.3     joerg 
    267  1.20.2.3     joerg static LIST_HEAD(, ubc_window) uwinlst = LIST_HEAD_INITIALIZER(uwinlst);
    268  1.20.2.4  jmcneill static kmutex_t uwinmtx;
    269  1.20.2.3     joerg 
    270  1.20.2.2     joerg int
    271  1.20.2.4  jmcneill rump_ubc_magic_uiomove(void *va, size_t n, struct uio *uio, int *rvp,
    272  1.20.2.4  jmcneill 	struct ubc_window *uwinp)
    273  1.20.2.2     joerg {
    274  1.20.2.2     joerg 	struct vm_page **pgs;
    275  1.20.2.2     joerg 	int npages = len2npages(uio->uio_offset, n);
    276  1.20.2.5     joerg 	size_t allocsize;
    277  1.20.2.2     joerg 	int i, rv;
    278  1.20.2.2     joerg 
    279  1.20.2.3     joerg 	if (uwinp == NULL) {
    280  1.20.2.4  jmcneill 		mutex_enter(&uwinmtx);
    281  1.20.2.4  jmcneill 		LIST_FOREACH(uwinp, &uwinlst, uwin_entries)
    282  1.20.2.4  jmcneill 			if ((uint8_t *)va >= uwinp->uwin_mem
    283  1.20.2.4  jmcneill 			    && (uint8_t *)va
    284  1.20.2.4  jmcneill 			      < (uwinp->uwin_mem + uwinp->uwin_mapsize))
    285  1.20.2.4  jmcneill 				break;
    286  1.20.2.4  jmcneill 		mutex_exit(&uwinmtx);
    287  1.20.2.4  jmcneill 		if (uwinp == NULL) {
    288  1.20.2.4  jmcneill 			KASSERT(rvp != NULL);
    289  1.20.2.4  jmcneill 			return 0;
    290  1.20.2.4  jmcneill 		}
    291  1.20.2.3     joerg 	}
    292  1.20.2.2     joerg 
    293  1.20.2.5     joerg 	allocsize = npages * sizeof(pgs);
    294  1.20.2.5     joerg 	pgs = kmem_zalloc(allocsize, KM_SLEEP);
    295  1.20.2.5     joerg 	simple_lock(&uwinp->uwin_obj->vmobjlock);
    296  1.20.2.3     joerg 	rv = uwinp->uwin_obj->pgops->pgo_get(uwinp->uwin_obj,
    297  1.20.2.3     joerg 	    uwinp->uwin_off + ((uint8_t *)va - uwinp->uwin_mem),
    298  1.20.2.2     joerg 	    pgs, &npages, 0, 0, 0, 0);
    299  1.20.2.2     joerg 	if (rv)
    300  1.20.2.2     joerg 		goto out;
    301  1.20.2.2     joerg 
    302  1.20.2.2     joerg 	for (i = 0; i < npages; i++) {
    303  1.20.2.2     joerg 		size_t xfersize;
    304  1.20.2.2     joerg 		off_t pageoff;
    305  1.20.2.2     joerg 
    306  1.20.2.2     joerg 		pageoff = uio->uio_offset & PAGE_MASK;
    307  1.20.2.2     joerg 		xfersize = MIN(MIN(n, PAGE_SIZE), PAGE_SIZE-pageoff);
    308  1.20.2.2     joerg 		uiomove((uint8_t *)pgs[i]->uanon + pageoff, xfersize, uio);
    309  1.20.2.2     joerg 		if (uio->uio_rw == UIO_WRITE)
    310  1.20.2.2     joerg 			pgs[i]->flags &= ~PG_CLEAN;
    311  1.20.2.2     joerg 		n -= xfersize;
    312  1.20.2.2     joerg 	}
    313  1.20.2.5     joerg 	uvm_page_unbusy(pgs, npages);
    314  1.20.2.2     joerg 
    315  1.20.2.2     joerg  out:
    316  1.20.2.5     joerg 	kmem_free(pgs, allocsize);
    317  1.20.2.3     joerg 	if (rvp)
    318  1.20.2.3     joerg 		*rvp = rv;
    319  1.20.2.3     joerg 	return 1;
    320  1.20.2.2     joerg }
    321  1.20.2.2     joerg 
    322  1.20.2.4  jmcneill static struct ubc_window *
    323  1.20.2.4  jmcneill uwin_alloc(struct uvm_object *uobj, voff_t off, vsize_t len)
    324  1.20.2.2     joerg {
    325  1.20.2.3     joerg 	struct ubc_window *uwinp; /* pronounced: you wimp! */
    326  1.20.2.2     joerg 
    327  1.20.2.3     joerg 	uwinp = kmem_alloc(sizeof(struct ubc_window), KM_SLEEP);
    328  1.20.2.3     joerg 	uwinp->uwin_obj = uobj;
    329  1.20.2.4  jmcneill 	uwinp->uwin_off = off;
    330  1.20.2.4  jmcneill 	uwinp->uwin_mapsize = len;
    331  1.20.2.4  jmcneill 	uwinp->uwin_mem = kmem_alloc(len, KM_SLEEP);
    332  1.20.2.3     joerg 
    333  1.20.2.4  jmcneill 	return uwinp;
    334  1.20.2.4  jmcneill }
    335  1.20.2.4  jmcneill 
    336  1.20.2.4  jmcneill static void
    337  1.20.2.4  jmcneill uwin_free(struct ubc_window *uwinp)
    338  1.20.2.4  jmcneill {
    339  1.20.2.4  jmcneill 
    340  1.20.2.4  jmcneill 	kmem_free(uwinp->uwin_mem, uwinp->uwin_mapsize);
    341  1.20.2.4  jmcneill 	kmem_free(uwinp, sizeof(struct ubc_window));
    342  1.20.2.4  jmcneill }
    343  1.20.2.4  jmcneill 
    344  1.20.2.4  jmcneill void *
    345  1.20.2.4  jmcneill ubc_alloc(struct uvm_object *uobj, voff_t offset, vsize_t *lenp, int advice,
    346  1.20.2.4  jmcneill 	int flags)
    347  1.20.2.4  jmcneill {
    348  1.20.2.4  jmcneill 	struct ubc_window *uwinp;
    349  1.20.2.4  jmcneill 
    350  1.20.2.4  jmcneill 	uwinp = uwin_alloc(uobj, offset, *lenp);
    351  1.20.2.4  jmcneill 	mutex_enter(&uwinmtx);
    352  1.20.2.3     joerg 	LIST_INSERT_HEAD(&uwinlst, uwinp, uwin_entries);
    353  1.20.2.4  jmcneill 	mutex_exit(&uwinmtx);
    354  1.20.2.3     joerg 
    355  1.20.2.3     joerg 	DPRINTF(("UBC_ALLOC offset 0x%llx, uwin %p, mem %p\n",
    356  1.20.2.3     joerg 	    (unsigned long long)offset, uwinp, uwinp->uwin_mem));
    357  1.20.2.3     joerg 
    358  1.20.2.3     joerg 	return uwinp->uwin_mem;
    359  1.20.2.2     joerg }
    360  1.20.2.2     joerg 
    361  1.20.2.2     joerg void
    362  1.20.2.2     joerg ubc_release(void *va, int flags)
    363  1.20.2.2     joerg {
    364  1.20.2.3     joerg 	struct ubc_window *uwinp;
    365  1.20.2.2     joerg 
    366  1.20.2.4  jmcneill 	mutex_enter(&uwinmtx);
    367  1.20.2.3     joerg 	LIST_FOREACH(uwinp, &uwinlst, uwin_entries)
    368  1.20.2.3     joerg 		if ((uint8_t *)va >= uwinp->uwin_mem
    369  1.20.2.3     joerg 		    && (uint8_t *)va < (uwinp->uwin_mem + uwinp->uwin_mapsize))
    370  1.20.2.3     joerg 			break;
    371  1.20.2.4  jmcneill 	mutex_exit(&uwinmtx);
    372  1.20.2.3     joerg 	if (uwinp == NULL)
    373  1.20.2.3     joerg 		panic("%s: releasing invalid window at %p", __func__, va);
    374  1.20.2.3     joerg 
    375  1.20.2.3     joerg 	LIST_REMOVE(uwinp, uwin_entries);
    376  1.20.2.4  jmcneill 	uwin_free(uwinp);
    377  1.20.2.2     joerg }
    378  1.20.2.2     joerg 
    379  1.20.2.2     joerg int
    380  1.20.2.2     joerg ubc_uiomove(struct uvm_object *uobj, struct uio *uio, vsize_t todo,
    381  1.20.2.2     joerg 	int advice, int flags)
    382  1.20.2.2     joerg {
    383  1.20.2.4  jmcneill 	struct ubc_window *uwinp;
    384  1.20.2.2     joerg 	vsize_t len;
    385  1.20.2.2     joerg 
    386  1.20.2.2     joerg 	while (todo > 0) {
    387  1.20.2.2     joerg 		len = todo;
    388  1.20.2.2     joerg 
    389  1.20.2.4  jmcneill 		uwinp = uwin_alloc(uobj, uio->uio_offset, len);
    390  1.20.2.4  jmcneill 		rump_ubc_magic_uiomove(uwinp->uwin_mem, len, uio, NULL, uwinp);
    391  1.20.2.4  jmcneill 		uwin_free(uwinp);
    392  1.20.2.2     joerg 
    393  1.20.2.2     joerg 		todo -= len;
    394  1.20.2.2     joerg 	}
    395  1.20.2.2     joerg 	return 0;
    396  1.20.2.2     joerg }
    397  1.20.2.2     joerg 
    398  1.20.2.2     joerg 
    399  1.20.2.2     joerg /*
    400  1.20.2.2     joerg  * Misc routines
    401  1.20.2.2     joerg  */
    402  1.20.2.2     joerg 
    403  1.20.2.2     joerg void
    404  1.20.2.2     joerg rumpvm_init()
    405  1.20.2.2     joerg {
    406  1.20.2.2     joerg 
    407  1.20.2.2     joerg 	uvm_vnodeops.pgo_get = vn_get;
    408  1.20.2.2     joerg 	uvm_vnodeops.pgo_put = vn_put;
    409  1.20.2.2     joerg 	aobj_pager.pgo_get = ao_get;
    410  1.20.2.2     joerg 	aobj_pager.pgo_put = ao_put;
    411  1.20.2.2     joerg 
    412  1.20.2.2     joerg 	uvmexp.free = 1024*1024; /* XXX */
    413  1.20.2.2     joerg 	uvm.pagedaemon_lwp = NULL; /* doesn't match curlwp */
    414  1.20.2.4  jmcneill 
    415  1.20.2.4  jmcneill 	mutex_init(&rvamtx, MUTEX_DEFAULT, 0);
    416  1.20.2.4  jmcneill 	mutex_init(&uwinmtx, MUTEX_DEFAULT, 0);
    417  1.20.2.2     joerg }
    418  1.20.2.2     joerg 
    419  1.20.2.2     joerg void
    420  1.20.2.2     joerg uvm_pageactivate(struct vm_page *pg)
    421  1.20.2.2     joerg {
    422  1.20.2.2     joerg 
    423  1.20.2.2     joerg 	/* nada */
    424  1.20.2.2     joerg }
    425  1.20.2.2     joerg 
    426  1.20.2.2     joerg void
    427  1.20.2.2     joerg uvm_pagewire(struct vm_page *pg)
    428  1.20.2.2     joerg {
    429  1.20.2.2     joerg 
    430  1.20.2.2     joerg 	/* nada */
    431  1.20.2.2     joerg }
    432  1.20.2.2     joerg 
    433  1.20.2.2     joerg void
    434  1.20.2.2     joerg uvm_pageunwire(struct vm_page *pg)
    435  1.20.2.2     joerg {
    436  1.20.2.2     joerg 
    437  1.20.2.2     joerg 	/* nada */
    438  1.20.2.2     joerg }
    439  1.20.2.2     joerg 
    440  1.20.2.2     joerg vaddr_t
    441  1.20.2.2     joerg uvm_pagermapin(struct vm_page **pps, int npages, int flags)
    442  1.20.2.2     joerg {
    443  1.20.2.2     joerg 
    444  1.20.2.2     joerg 	panic("%s: unimplemented", __func__);
    445  1.20.2.2     joerg }
    446  1.20.2.2     joerg 
    447  1.20.2.5     joerg /* Called with the vm object locked */
    448  1.20.2.2     joerg struct vm_page *
    449  1.20.2.2     joerg uvm_pagelookup(struct uvm_object *uobj, voff_t off)
    450  1.20.2.2     joerg {
    451  1.20.2.2     joerg 	struct vm_page *pg;
    452  1.20.2.2     joerg 
    453  1.20.2.4  jmcneill 	TAILQ_FOREACH(pg, &uobj->memq, listq) {
    454  1.20.2.4  jmcneill 		if (pg->offset == off) {
    455  1.20.2.4  jmcneill 			simple_unlock(&uobj->vmobjlock);
    456  1.20.2.2     joerg 			return pg;
    457  1.20.2.4  jmcneill 		}
    458  1.20.2.4  jmcneill 	}
    459  1.20.2.2     joerg 
    460  1.20.2.2     joerg 	return NULL;
    461  1.20.2.2     joerg }
    462  1.20.2.2     joerg 
    463  1.20.2.2     joerg struct vm_page *
    464  1.20.2.2     joerg uvm_pageratop(vaddr_t va)
    465  1.20.2.2     joerg {
    466  1.20.2.2     joerg 	struct rumpva *rva;
    467  1.20.2.2     joerg 
    468  1.20.2.4  jmcneill 	mutex_enter(&rvamtx);
    469  1.20.2.2     joerg 	LIST_FOREACH(rva, &rvahead, entries)
    470  1.20.2.2     joerg 		if (rva->addr == va)
    471  1.20.2.4  jmcneill 			break;
    472  1.20.2.4  jmcneill 	mutex_exit(&rvamtx);
    473  1.20.2.4  jmcneill 
    474  1.20.2.4  jmcneill 	if (rva == NULL)
    475  1.20.2.4  jmcneill 		panic("%s: va %llu", __func__, (unsigned long long)va);
    476  1.20.2.2     joerg 
    477  1.20.2.4  jmcneill 	return rva->pg;
    478  1.20.2.2     joerg }
    479  1.20.2.2     joerg 
    480  1.20.2.2     joerg void
    481  1.20.2.5     joerg uvm_page_unbusy(struct vm_page **pgs, int npgs)
    482  1.20.2.5     joerg {
    483  1.20.2.5     joerg 	struct vm_page *pg;
    484  1.20.2.5     joerg 	int i;
    485  1.20.2.5     joerg 
    486  1.20.2.5     joerg 	for (i = 0; i < npgs; i++) {
    487  1.20.2.5     joerg 		pg = pgs[i];
    488  1.20.2.5     joerg 		if (pg == NULL)
    489  1.20.2.5     joerg 			continue;
    490  1.20.2.5     joerg 
    491  1.20.2.5     joerg 		KASSERT(pg->flags & PG_BUSY);
    492  1.20.2.5     joerg 		if (pg->flags & PG_WANTED)
    493  1.20.2.5     joerg 			wakeup(pg);
    494  1.20.2.5     joerg 		pg->flags &= ~(PG_WANTED|PG_BUSY);
    495  1.20.2.5     joerg 	}
    496  1.20.2.5     joerg }
    497  1.20.2.5     joerg 
    498  1.20.2.5     joerg void
    499  1.20.2.2     joerg uvm_estimatepageable(int *active, int *inactive)
    500  1.20.2.2     joerg {
    501  1.20.2.2     joerg 
    502  1.20.2.2     joerg 	/* XXX: guessing game */
    503  1.20.2.2     joerg 	*active = 1024;
    504  1.20.2.2     joerg 	*inactive = 1024;
    505  1.20.2.2     joerg }
    506  1.20.2.2     joerg 
    507  1.20.2.2     joerg void
    508  1.20.2.2     joerg uvm_aio_biodone1(struct buf *bp)
    509  1.20.2.2     joerg {
    510  1.20.2.2     joerg 
    511  1.20.2.2     joerg 	panic("%s: unimplemented", __func__);
    512  1.20.2.2     joerg }
    513  1.20.2.2     joerg 
    514  1.20.2.2     joerg void
    515  1.20.2.2     joerg uvm_aio_biodone(struct buf *bp)
    516  1.20.2.2     joerg {
    517  1.20.2.2     joerg 
    518  1.20.2.2     joerg 	uvm_aio_aiodone(bp);
    519  1.20.2.2     joerg }
    520  1.20.2.2     joerg 
    521  1.20.2.2     joerg void
    522  1.20.2.2     joerg uvm_aio_aiodone(struct buf *bp)
    523  1.20.2.2     joerg {
    524  1.20.2.2     joerg 
    525  1.20.2.2     joerg 	if ((bp->b_flags & (B_READ | B_NOCACHE)) == 0 && bioopsp)
    526  1.20.2.2     joerg 		bioopsp->io_pageiodone(bp);
    527  1.20.2.2     joerg }
    528  1.20.2.2     joerg 
    529  1.20.2.2     joerg void
    530  1.20.2.2     joerg uvm_vnp_setsize(struct vnode *vp, voff_t newsize)
    531  1.20.2.2     joerg {
    532  1.20.2.2     joerg 
    533  1.20.2.2     joerg 	vp->v_size = vp->v_writesize = newsize;
    534  1.20.2.2     joerg }
    535  1.20.2.2     joerg 
    536  1.20.2.2     joerg void
    537  1.20.2.2     joerg uvm_vnp_setwritesize(struct vnode *vp, voff_t newsize)
    538  1.20.2.2     joerg {
    539  1.20.2.2     joerg 
    540  1.20.2.2     joerg 	vp->v_writesize = newsize;
    541  1.20.2.2     joerg }
    542  1.20.2.2     joerg 
    543  1.20.2.2     joerg void
    544  1.20.2.2     joerg uvm_vnp_zerorange(struct vnode *vp, off_t off, size_t len)
    545  1.20.2.2     joerg {
    546  1.20.2.2     joerg 	struct uvm_object *uobj = &vp->v_uobj;
    547  1.20.2.2     joerg 	struct vm_page **pgs;
    548  1.20.2.2     joerg 	int maxpages = MIN(32, round_page(len) >> PAGE_SHIFT);
    549  1.20.2.2     joerg 	int rv, npages, i;
    550  1.20.2.2     joerg 
    551  1.20.2.5     joerg 	pgs = kmem_zalloc(maxpages * sizeof(pgs), KM_SLEEP);
    552  1.20.2.2     joerg 	while (len) {
    553  1.20.2.2     joerg 		npages = MIN(maxpages, round_page(len) >> PAGE_SHIFT);
    554  1.20.2.2     joerg 		memset(pgs, 0, npages * sizeof(struct vm_page *));
    555  1.20.2.5     joerg 		simple_lock(&uobj->vmobjlock);
    556  1.20.2.2     joerg 		rv = uobj->pgops->pgo_get(uobj, off, pgs, &npages, 0, 0, 0, 0);
    557  1.20.2.2     joerg 		assert(npages > 0);
    558  1.20.2.2     joerg 
    559  1.20.2.2     joerg 		for (i = 0; i < npages; i++) {
    560  1.20.2.2     joerg 			uint8_t *start;
    561  1.20.2.2     joerg 			size_t chunkoff, chunklen;
    562  1.20.2.2     joerg 
    563  1.20.2.2     joerg 			chunkoff = off & PAGE_MASK;
    564  1.20.2.2     joerg 			chunklen = MIN(PAGE_SIZE - chunkoff, len);
    565  1.20.2.2     joerg 			start = (uint8_t *)pgs[i]->uanon + chunkoff;
    566  1.20.2.2     joerg 
    567  1.20.2.2     joerg 			memset(start, 0, chunklen);
    568  1.20.2.2     joerg 			pgs[i]->flags &= PG_CLEAN;
    569  1.20.2.2     joerg 
    570  1.20.2.2     joerg 			off += chunklen;
    571  1.20.2.2     joerg 			len -= chunklen;
    572  1.20.2.2     joerg 		}
    573  1.20.2.5     joerg 		uvm_page_unbusy(pgs, npages);
    574  1.20.2.2     joerg 	}
    575  1.20.2.5     joerg 	kmem_free(pgs, maxpages * sizeof(pgs));
    576  1.20.2.2     joerg 
    577  1.20.2.2     joerg 	return;
    578  1.20.2.2     joerg }
    579  1.20.2.2     joerg 
    580  1.20.2.2     joerg struct uvm_ractx *
    581  1.20.2.2     joerg uvm_ra_allocctx()
    582  1.20.2.2     joerg {
    583  1.20.2.2     joerg 
    584  1.20.2.2     joerg 	return NULL;
    585  1.20.2.2     joerg }
    586  1.20.2.2     joerg 
    587  1.20.2.2     joerg void
    588  1.20.2.2     joerg uvm_ra_freectx(struct uvm_ractx *ra)
    589  1.20.2.2     joerg {
    590  1.20.2.2     joerg 
    591  1.20.2.2     joerg 	return;
    592  1.20.2.2     joerg }
    593  1.20.2.2     joerg 
    594  1.20.2.2     joerg bool
    595  1.20.2.2     joerg uvn_clean_p(struct uvm_object *uobj)
    596  1.20.2.2     joerg {
    597  1.20.2.2     joerg 	struct vnode *vp = (void *)uobj;
    598  1.20.2.2     joerg 
    599  1.20.2.2     joerg 	return (vp->v_iflag & VI_ONWORKLST) == 0;
    600  1.20.2.2     joerg }
    601  1.20.2.2     joerg 
    602  1.20.2.2     joerg /*
    603  1.20.2.2     joerg  * Kmem
    604  1.20.2.2     joerg  */
    605  1.20.2.2     joerg 
    606  1.20.2.2     joerg void *
    607  1.20.2.2     joerg kmem_alloc(size_t size, km_flag_t kmflag)
    608  1.20.2.2     joerg {
    609  1.20.2.2     joerg 
    610  1.20.2.2     joerg 	return rumpuser_malloc(size, kmflag == KM_NOSLEEP);
    611  1.20.2.2     joerg }
    612  1.20.2.2     joerg 
    613  1.20.2.2     joerg void *
    614  1.20.2.2     joerg kmem_zalloc(size_t size, km_flag_t kmflag)
    615  1.20.2.2     joerg {
    616  1.20.2.2     joerg 	void *rv;
    617  1.20.2.2     joerg 
    618  1.20.2.2     joerg 	rv = kmem_alloc(size, kmflag);
    619  1.20.2.2     joerg 	if (rv)
    620  1.20.2.2     joerg 		memset(rv, 0, size);
    621  1.20.2.2     joerg 
    622  1.20.2.2     joerg 	return rv;
    623  1.20.2.2     joerg }
    624  1.20.2.2     joerg 
    625  1.20.2.2     joerg void
    626  1.20.2.2     joerg kmem_free(void *p, size_t size)
    627  1.20.2.2     joerg {
    628  1.20.2.2     joerg 
    629  1.20.2.2     joerg 	rumpuser_free(p);
    630  1.20.2.2     joerg }
    631  1.20.2.2     joerg 
    632  1.20.2.2     joerg /*
    633  1.20.2.2     joerg  * UVM km
    634  1.20.2.2     joerg  */
    635  1.20.2.2     joerg 
    636  1.20.2.2     joerg vaddr_t
    637  1.20.2.2     joerg uvm_km_alloc(struct vm_map *map, vsize_t size, vsize_t align, uvm_flag_t flags)
    638  1.20.2.2     joerg {
    639  1.20.2.2     joerg 	void *rv;
    640  1.20.2.2     joerg 
    641  1.20.2.2     joerg 	rv = rumpuser_malloc(size, flags & (UVM_KMF_CANFAIL | UVM_KMF_NOWAIT));
    642  1.20.2.2     joerg 	if (rv && flags & UVM_KMF_ZERO)
    643  1.20.2.2     joerg 		memset(rv, 0, size);
    644  1.20.2.2     joerg 
    645  1.20.2.2     joerg 	return (vaddr_t)rv;
    646  1.20.2.2     joerg }
    647  1.20.2.2     joerg 
    648  1.20.2.2     joerg void
    649  1.20.2.2     joerg uvm_km_free(struct vm_map *map, vaddr_t vaddr, vsize_t size, uvm_flag_t flags)
    650  1.20.2.2     joerg {
    651  1.20.2.2     joerg 
    652  1.20.2.2     joerg 	rumpuser_free((void *)vaddr);
    653  1.20.2.2     joerg }
    654  1.20.2.2     joerg 
    655  1.20.2.2     joerg struct vm_map *
    656  1.20.2.2     joerg uvm_km_suballoc(struct vm_map *map, vaddr_t *minaddr, vaddr_t *maxaddr,
    657  1.20.2.2     joerg 	vsize_t size, int pageable, bool fixed, struct vm_map_kernel *submap)
    658  1.20.2.2     joerg {
    659  1.20.2.2     joerg 
    660  1.20.2.2     joerg 	return (struct vm_map *)417416;
    661  1.20.2.2     joerg }
    662