Home | History | Annotate | Line # | Download | only in rumpkern
vm.c revision 1.41
      1  1.41  pooka /*	$NetBSD: vm.c,v 1.41 2008/10/15 13:04:26 pooka 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.40  pooka #include <sys/atomic.h>
     48   1.1  pooka #include <sys/null.h>
     49   1.1  pooka #include <sys/vnode.h>
     50   1.1  pooka #include <sys/buf.h>
     51   1.9  pooka #include <sys/kmem.h>
     52   1.1  pooka 
     53  1.34  pooka #include <machine/pmap.h>
     54  1.34  pooka 
     55  1.34  pooka #include <rump/rumpuser.h>
     56  1.34  pooka 
     57   1.1  pooka #include <uvm/uvm.h>
     58   1.1  pooka #include <uvm/uvm_prot.h>
     59  1.11  pooka #include <uvm/uvm_readahead.h>
     60   1.1  pooka 
     61  1.13  pooka #include "rump_private.h"
     62   1.1  pooka 
     63   1.1  pooka /* dumdidumdum */
     64   1.1  pooka #define len2npages(off, len)						\
     65   1.1  pooka   (((((len) + PAGE_MASK) & ~(PAGE_MASK)) >> PAGE_SHIFT)			\
     66   1.1  pooka     + (((off & PAGE_MASK) + (len & PAGE_MASK)) > PAGE_SIZE))
     67   1.1  pooka 
     68  1.24   yamt static int vn_get(struct uvm_object *, voff_t, struct vm_page **,
     69  1.24   yamt 	int *, int, vm_prot_t, int, int);
     70  1.24   yamt static int vn_put(struct uvm_object *, voff_t, voff_t, int);
     71  1.24   yamt static int ao_get(struct uvm_object *, voff_t, struct vm_page **,
     72  1.24   yamt 	int *, int, vm_prot_t, int, int);
     73  1.24   yamt static int ao_put(struct uvm_object *, voff_t, voff_t, int);
     74  1.24   yamt 
     75  1.24   yamt const struct uvm_pagerops uvm_vnodeops = {
     76  1.24   yamt 	.pgo_get = vn_get,
     77  1.24   yamt 	.pgo_put = vn_put,
     78  1.24   yamt };
     79  1.24   yamt const struct uvm_pagerops aobj_pager = {
     80  1.24   yamt 	.pgo_get = ao_get,
     81  1.24   yamt 	.pgo_put = ao_put,
     82  1.24   yamt };
     83  1.24   yamt 
     84  1.25     ad kmutex_t uvm_pageqlock;
     85  1.25     ad 
     86   1.1  pooka struct uvmexp uvmexp;
     87   1.7  pooka struct uvm uvm;
     88   1.1  pooka 
     89   1.1  pooka struct vmspace rump_vmspace;
     90   1.1  pooka struct vm_map rump_vmmap;
     91  1.32     ad const struct rb_tree_ops uvm_page_tree_ops;
     92   1.1  pooka 
     93  1.35  pooka static struct vm_map_kernel kernel_map_store;
     94  1.35  pooka struct vm_map *kernel_map = &kernel_map_store.vmk_map;
     95  1.35  pooka 
     96   1.1  pooka /*
     97   1.1  pooka  * vm pages
     98   1.1  pooka  */
     99   1.1  pooka 
    100  1.22  pooka /* called with the object locked */
    101   1.1  pooka struct vm_page *
    102   1.6  pooka rumpvm_makepage(struct uvm_object *uobj, voff_t off)
    103   1.1  pooka {
    104   1.1  pooka 	struct vm_page *pg;
    105   1.1  pooka 
    106  1.27  pooka 	pg = kmem_zalloc(sizeof(struct vm_page), KM_SLEEP);
    107   1.1  pooka 	pg->offset = off;
    108   1.5  pooka 	pg->uobject = uobj;
    109   1.1  pooka 
    110  1.27  pooka 	pg->uanon = (void *)kmem_zalloc(PAGE_SIZE, KM_SLEEP);
    111  1.22  pooka 	pg->flags = PG_CLEAN|PG_BUSY|PG_FAKE;
    112   1.1  pooka 
    113  1.31     ad 	TAILQ_INSERT_TAIL(&uobj->memq, pg, listq.queue);
    114  1.21  pooka 
    115   1.1  pooka 	return pg;
    116   1.1  pooka }
    117   1.1  pooka 
    118  1.21  pooka /*
    119  1.21  pooka  * Release a page.
    120  1.21  pooka  *
    121  1.22  pooka  * Called with the vm object locked.
    122  1.21  pooka  */
    123   1.1  pooka void
    124  1.22  pooka uvm_pagefree(struct vm_page *pg)
    125   1.1  pooka {
    126   1.5  pooka 	struct uvm_object *uobj = pg->uobject;
    127   1.1  pooka 
    128  1.22  pooka 	if (pg->flags & PG_WANTED)
    129  1.22  pooka 		wakeup(pg);
    130  1.22  pooka 
    131  1.31     ad 	TAILQ_REMOVE(&uobj->memq, pg, listq.queue);
    132  1.27  pooka 	kmem_free((void *)pg->uanon, PAGE_SIZE);
    133  1.27  pooka 	kmem_free(pg, sizeof(*pg));
    134   1.1  pooka }
    135   1.1  pooka 
    136  1.15  pooka struct rumpva {
    137  1.15  pooka 	vaddr_t addr;
    138  1.15  pooka 	struct vm_page *pg;
    139  1.15  pooka 
    140  1.15  pooka 	LIST_ENTRY(rumpva) entries;
    141  1.15  pooka };
    142  1.15  pooka static LIST_HEAD(, rumpva) rvahead = LIST_HEAD_INITIALIZER(rvahead);
    143  1.21  pooka static kmutex_t rvamtx;
    144  1.15  pooka 
    145  1.15  pooka void
    146  1.15  pooka rumpvm_enterva(vaddr_t addr, struct vm_page *pg)
    147  1.15  pooka {
    148  1.15  pooka 	struct rumpva *rva;
    149  1.15  pooka 
    150  1.27  pooka 	rva = kmem_alloc(sizeof(struct rumpva), KM_SLEEP);
    151  1.15  pooka 	rva->addr = addr;
    152  1.15  pooka 	rva->pg = pg;
    153  1.21  pooka 	mutex_enter(&rvamtx);
    154  1.15  pooka 	LIST_INSERT_HEAD(&rvahead, rva, entries);
    155  1.21  pooka 	mutex_exit(&rvamtx);
    156  1.15  pooka }
    157  1.15  pooka 
    158  1.15  pooka void
    159  1.15  pooka rumpvm_flushva()
    160  1.15  pooka {
    161  1.15  pooka 	struct rumpva *rva;
    162  1.15  pooka 
    163  1.21  pooka 	mutex_enter(&rvamtx);
    164  1.15  pooka 	while ((rva = LIST_FIRST(&rvahead)) != NULL) {
    165  1.15  pooka 		LIST_REMOVE(rva, entries);
    166  1.27  pooka 		kmem_free(rva, sizeof(*rva));
    167  1.15  pooka 	}
    168  1.21  pooka 	mutex_exit(&rvamtx);
    169  1.15  pooka }
    170  1.15  pooka 
    171   1.1  pooka /*
    172   1.1  pooka  * vnode pager
    173   1.1  pooka  */
    174   1.1  pooka 
    175   1.1  pooka static int
    176   1.1  pooka vn_get(struct uvm_object *uobj, voff_t off, struct vm_page **pgs,
    177   1.1  pooka 	int *npages, int centeridx, vm_prot_t access_type,
    178   1.1  pooka 	int advice, int flags)
    179   1.1  pooka {
    180   1.1  pooka 	struct vnode *vp = (struct vnode *)uobj;
    181   1.1  pooka 
    182   1.1  pooka 	return VOP_GETPAGES(vp, off, pgs, npages, centeridx, access_type,
    183   1.1  pooka 	    advice, flags);
    184   1.1  pooka }
    185   1.1  pooka 
    186   1.1  pooka static int
    187   1.1  pooka vn_put(struct uvm_object *uobj, voff_t offlo, voff_t offhi, int flags)
    188   1.1  pooka {
    189   1.1  pooka 	struct vnode *vp = (struct vnode *)uobj;
    190   1.1  pooka 
    191   1.1  pooka 	return VOP_PUTPAGES(vp, offlo, offhi, flags);
    192   1.1  pooka }
    193   1.1  pooka 
    194   1.1  pooka /*
    195   1.1  pooka  * Anon object stuff
    196   1.1  pooka  */
    197   1.1  pooka 
    198   1.1  pooka static int
    199   1.1  pooka ao_get(struct uvm_object *uobj, voff_t off, struct vm_page **pgs,
    200   1.1  pooka 	int *npages, int centeridx, vm_prot_t access_type,
    201   1.1  pooka 	int advice, int flags)
    202   1.1  pooka {
    203   1.1  pooka 	struct vm_page *pg;
    204   1.1  pooka 	int i;
    205   1.1  pooka 
    206   1.1  pooka 	if (centeridx)
    207   1.1  pooka 		panic("%s: centeridx != 0 not supported", __func__);
    208   1.1  pooka 
    209   1.1  pooka 	/* loop over pages */
    210   1.1  pooka 	off = trunc_page(off);
    211   1.1  pooka 	for (i = 0; i < *npages; i++) {
    212  1.23  pooka  retrylookup:
    213  1.10  pooka 		pg = uvm_pagelookup(uobj, off + (i << PAGE_SHIFT));
    214   1.1  pooka 		if (pg) {
    215  1.23  pooka 			if (pg->flags & PG_BUSY) {
    216  1.23  pooka 				pg->flags |= PG_WANTED;
    217  1.23  pooka 				UVM_UNLOCK_AND_WAIT(pg, &uobj->vmobjlock, 0,
    218  1.23  pooka 				    "aogetpg", 0);
    219  1.23  pooka 				goto retrylookup;
    220  1.23  pooka 			}
    221  1.23  pooka 			pg->flags |= PG_BUSY;
    222   1.1  pooka 			pgs[i] = pg;
    223   1.1  pooka 		} else {
    224   1.6  pooka 			pg = rumpvm_makepage(uobj, off + (i << PAGE_SHIFT));
    225   1.1  pooka 			pgs[i] = pg;
    226   1.1  pooka 		}
    227   1.1  pooka 	}
    228  1.26  pooka 	mutex_exit(&uobj->vmobjlock);
    229   1.1  pooka 
    230   1.1  pooka 	return 0;
    231   1.1  pooka 
    232   1.1  pooka }
    233   1.1  pooka 
    234   1.1  pooka static int
    235   1.1  pooka ao_put(struct uvm_object *uobj, voff_t start, voff_t stop, int flags)
    236   1.1  pooka {
    237   1.1  pooka 	struct vm_page *pg;
    238   1.1  pooka 
    239   1.1  pooka 	/* we only free all pages for now */
    240  1.23  pooka 	if ((flags & PGO_FREE) == 0 || (flags & PGO_ALLPAGES) == 0) {
    241  1.26  pooka 		mutex_exit(&uobj->vmobjlock);
    242   1.1  pooka 		return 0;
    243  1.23  pooka 	}
    244   1.1  pooka 
    245   1.1  pooka 	while ((pg = TAILQ_FIRST(&uobj->memq)) != NULL)
    246  1.22  pooka 		uvm_pagefree(pg);
    247  1.26  pooka 	mutex_exit(&uobj->vmobjlock);
    248   1.1  pooka 
    249   1.1  pooka 	return 0;
    250   1.1  pooka }
    251   1.1  pooka 
    252   1.1  pooka struct uvm_object *
    253   1.1  pooka uao_create(vsize_t size, int flags)
    254   1.1  pooka {
    255   1.1  pooka 	struct uvm_object *uobj;
    256   1.1  pooka 
    257  1.27  pooka 	uobj = kmem_zalloc(sizeof(struct uvm_object), KM_SLEEP);
    258   1.1  pooka 	uobj->pgops = &aobj_pager;
    259   1.1  pooka 	TAILQ_INIT(&uobj->memq);
    260  1.26  pooka 	mutex_init(&uobj->vmobjlock, MUTEX_DEFAULT, IPL_NONE);
    261   1.1  pooka 
    262   1.1  pooka 	return uobj;
    263   1.1  pooka }
    264   1.1  pooka 
    265   1.1  pooka void
    266   1.1  pooka uao_detach(struct uvm_object *uobj)
    267   1.1  pooka {
    268   1.1  pooka 
    269  1.29  pooka 	mutex_enter(&uobj->vmobjlock);
    270   1.1  pooka 	ao_put(uobj, 0, 0, PGO_ALLPAGES | PGO_FREE);
    271  1.27  pooka 	kmem_free(uobj, sizeof(*uobj));
    272   1.1  pooka }
    273   1.1  pooka 
    274   1.1  pooka /*
    275   1.1  pooka  * UBC
    276   1.1  pooka  */
    277   1.1  pooka 
    278  1.20  pooka struct ubc_window {
    279  1.20  pooka 	struct uvm_object	*uwin_obj;
    280  1.20  pooka 	voff_t			uwin_off;
    281  1.20  pooka 	uint8_t			*uwin_mem;
    282  1.20  pooka 	size_t			uwin_mapsize;
    283  1.20  pooka 
    284  1.20  pooka 	LIST_ENTRY(ubc_window)	uwin_entries;
    285  1.20  pooka };
    286  1.20  pooka 
    287  1.20  pooka static LIST_HEAD(, ubc_window) uwinlst = LIST_HEAD_INITIALIZER(uwinlst);
    288  1.21  pooka static kmutex_t uwinmtx;
    289  1.20  pooka 
    290   1.1  pooka int
    291  1.21  pooka rump_ubc_magic_uiomove(void *va, size_t n, struct uio *uio, int *rvp,
    292  1.21  pooka 	struct ubc_window *uwinp)
    293   1.1  pooka {
    294  1.17  pooka 	struct vm_page **pgs;
    295   1.1  pooka 	int npages = len2npages(uio->uio_offset, n);
    296  1.22  pooka 	size_t allocsize;
    297   1.1  pooka 	int i, rv;
    298   1.1  pooka 
    299  1.20  pooka 	if (uwinp == NULL) {
    300  1.21  pooka 		mutex_enter(&uwinmtx);
    301  1.21  pooka 		LIST_FOREACH(uwinp, &uwinlst, uwin_entries)
    302  1.21  pooka 			if ((uint8_t *)va >= uwinp->uwin_mem
    303  1.21  pooka 			    && (uint8_t *)va
    304  1.21  pooka 			      < (uwinp->uwin_mem + uwinp->uwin_mapsize))
    305  1.21  pooka 				break;
    306  1.21  pooka 		mutex_exit(&uwinmtx);
    307  1.21  pooka 		if (uwinp == NULL) {
    308  1.21  pooka 			KASSERT(rvp != NULL);
    309  1.21  pooka 			return 0;
    310  1.21  pooka 		}
    311  1.20  pooka 	}
    312   1.1  pooka 
    313  1.22  pooka 	allocsize = npages * sizeof(pgs);
    314  1.22  pooka 	pgs = kmem_zalloc(allocsize, KM_SLEEP);
    315  1.28  pooka 	mutex_enter(&uwinp->uwin_obj->vmobjlock);
    316  1.20  pooka 	rv = uwinp->uwin_obj->pgops->pgo_get(uwinp->uwin_obj,
    317  1.20  pooka 	    uwinp->uwin_off + ((uint8_t *)va - uwinp->uwin_mem),
    318   1.1  pooka 	    pgs, &npages, 0, 0, 0, 0);
    319   1.1  pooka 	if (rv)
    320  1.17  pooka 		goto out;
    321   1.1  pooka 
    322   1.1  pooka 	for (i = 0; i < npages; i++) {
    323   1.1  pooka 		size_t xfersize;
    324   1.1  pooka 		off_t pageoff;
    325   1.1  pooka 
    326   1.1  pooka 		pageoff = uio->uio_offset & PAGE_MASK;
    327   1.1  pooka 		xfersize = MIN(MIN(n, PAGE_SIZE), PAGE_SIZE-pageoff);
    328   1.5  pooka 		uiomove((uint8_t *)pgs[i]->uanon + pageoff, xfersize, uio);
    329   1.1  pooka 		if (uio->uio_rw == UIO_WRITE)
    330  1.10  pooka 			pgs[i]->flags &= ~PG_CLEAN;
    331   1.1  pooka 		n -= xfersize;
    332   1.1  pooka 	}
    333  1.22  pooka 	uvm_page_unbusy(pgs, npages);
    334   1.1  pooka 
    335  1.17  pooka  out:
    336  1.22  pooka 	kmem_free(pgs, allocsize);
    337  1.20  pooka 	if (rvp)
    338  1.20  pooka 		*rvp = rv;
    339  1.20  pooka 	return 1;
    340   1.1  pooka }
    341   1.1  pooka 
    342  1.21  pooka static struct ubc_window *
    343  1.21  pooka uwin_alloc(struct uvm_object *uobj, voff_t off, vsize_t len)
    344   1.1  pooka {
    345  1.20  pooka 	struct ubc_window *uwinp; /* pronounced: you wimp! */
    346   1.1  pooka 
    347  1.20  pooka 	uwinp = kmem_alloc(sizeof(struct ubc_window), KM_SLEEP);
    348  1.20  pooka 	uwinp->uwin_obj = uobj;
    349  1.21  pooka 	uwinp->uwin_off = off;
    350  1.21  pooka 	uwinp->uwin_mapsize = len;
    351  1.21  pooka 	uwinp->uwin_mem = kmem_alloc(len, KM_SLEEP);
    352  1.20  pooka 
    353  1.21  pooka 	return uwinp;
    354  1.21  pooka }
    355  1.21  pooka 
    356  1.21  pooka static void
    357  1.21  pooka uwin_free(struct ubc_window *uwinp)
    358  1.21  pooka {
    359  1.21  pooka 
    360  1.21  pooka 	kmem_free(uwinp->uwin_mem, uwinp->uwin_mapsize);
    361  1.21  pooka 	kmem_free(uwinp, sizeof(struct ubc_window));
    362  1.21  pooka }
    363  1.21  pooka 
    364  1.21  pooka void *
    365  1.21  pooka ubc_alloc(struct uvm_object *uobj, voff_t offset, vsize_t *lenp, int advice,
    366  1.21  pooka 	int flags)
    367  1.21  pooka {
    368  1.21  pooka 	struct ubc_window *uwinp;
    369  1.21  pooka 
    370  1.21  pooka 	uwinp = uwin_alloc(uobj, offset, *lenp);
    371  1.21  pooka 	mutex_enter(&uwinmtx);
    372  1.20  pooka 	LIST_INSERT_HEAD(&uwinlst, uwinp, uwin_entries);
    373  1.21  pooka 	mutex_exit(&uwinmtx);
    374  1.20  pooka 
    375  1.20  pooka 	DPRINTF(("UBC_ALLOC offset 0x%llx, uwin %p, mem %p\n",
    376  1.20  pooka 	    (unsigned long long)offset, uwinp, uwinp->uwin_mem));
    377  1.20  pooka 
    378  1.20  pooka 	return uwinp->uwin_mem;
    379   1.1  pooka }
    380   1.1  pooka 
    381   1.1  pooka void
    382   1.1  pooka ubc_release(void *va, int flags)
    383   1.1  pooka {
    384  1.20  pooka 	struct ubc_window *uwinp;
    385   1.1  pooka 
    386  1.21  pooka 	mutex_enter(&uwinmtx);
    387  1.20  pooka 	LIST_FOREACH(uwinp, &uwinlst, uwin_entries)
    388  1.20  pooka 		if ((uint8_t *)va >= uwinp->uwin_mem
    389  1.20  pooka 		    && (uint8_t *)va < (uwinp->uwin_mem + uwinp->uwin_mapsize))
    390  1.20  pooka 			break;
    391  1.21  pooka 	mutex_exit(&uwinmtx);
    392  1.20  pooka 	if (uwinp == NULL)
    393  1.20  pooka 		panic("%s: releasing invalid window at %p", __func__, va);
    394  1.20  pooka 
    395  1.20  pooka 	LIST_REMOVE(uwinp, uwin_entries);
    396  1.21  pooka 	uwin_free(uwinp);
    397   1.1  pooka }
    398   1.1  pooka 
    399   1.1  pooka int
    400   1.1  pooka ubc_uiomove(struct uvm_object *uobj, struct uio *uio, vsize_t todo,
    401   1.1  pooka 	int advice, int flags)
    402   1.1  pooka {
    403  1.21  pooka 	struct ubc_window *uwinp;
    404   1.1  pooka 	vsize_t len;
    405   1.1  pooka 
    406   1.1  pooka 	while (todo > 0) {
    407   1.1  pooka 		len = todo;
    408   1.1  pooka 
    409  1.21  pooka 		uwinp = uwin_alloc(uobj, uio->uio_offset, len);
    410  1.21  pooka 		rump_ubc_magic_uiomove(uwinp->uwin_mem, len, uio, NULL, uwinp);
    411  1.21  pooka 		uwin_free(uwinp);
    412   1.1  pooka 
    413   1.1  pooka 		todo -= len;
    414   1.1  pooka 	}
    415   1.1  pooka 	return 0;
    416   1.1  pooka }
    417   1.1  pooka 
    418   1.1  pooka 
    419   1.1  pooka /*
    420   1.1  pooka  * Misc routines
    421   1.1  pooka  */
    422   1.1  pooka 
    423   1.1  pooka void
    424   1.1  pooka rumpvm_init()
    425   1.1  pooka {
    426   1.1  pooka 
    427   1.1  pooka 	uvmexp.free = 1024*1024; /* XXX */
    428   1.7  pooka 	uvm.pagedaemon_lwp = NULL; /* doesn't match curlwp */
    429  1.38  pooka 	rump_vmspace.vm_map.pmap = pmap_kernel();
    430  1.21  pooka 
    431  1.21  pooka 	mutex_init(&rvamtx, MUTEX_DEFAULT, 0);
    432  1.21  pooka 	mutex_init(&uwinmtx, MUTEX_DEFAULT, 0);
    433  1.25     ad 	mutex_init(&uvm_pageqlock, MUTEX_DEFAULT, 0);
    434  1.35  pooka 
    435  1.35  pooka 	callback_head_init(&kernel_map_store.vmk_reclaim_callback, IPL_VM);
    436   1.1  pooka }
    437   1.1  pooka 
    438   1.1  pooka void
    439   1.1  pooka uvm_pageactivate(struct vm_page *pg)
    440   1.1  pooka {
    441   1.1  pooka 
    442   1.1  pooka 	/* nada */
    443   1.1  pooka }
    444   1.1  pooka 
    445   1.1  pooka void
    446   1.7  pooka uvm_pagewire(struct vm_page *pg)
    447   1.7  pooka {
    448   1.7  pooka 
    449   1.7  pooka 	/* nada */
    450   1.7  pooka }
    451   1.7  pooka 
    452   1.7  pooka void
    453   1.7  pooka uvm_pageunwire(struct vm_page *pg)
    454   1.7  pooka {
    455   1.7  pooka 
    456   1.7  pooka 	/* nada */
    457   1.7  pooka }
    458   1.7  pooka 
    459   1.7  pooka vaddr_t
    460   1.7  pooka uvm_pagermapin(struct vm_page **pps, int npages, int flags)
    461   1.7  pooka {
    462   1.7  pooka 
    463   1.7  pooka 	panic("%s: unimplemented", __func__);
    464   1.7  pooka }
    465   1.7  pooka 
    466  1.22  pooka /* Called with the vm object locked */
    467   1.7  pooka struct vm_page *
    468   1.7  pooka uvm_pagelookup(struct uvm_object *uobj, voff_t off)
    469   1.7  pooka {
    470  1.10  pooka 	struct vm_page *pg;
    471   1.7  pooka 
    472  1.31     ad 	TAILQ_FOREACH(pg, &uobj->memq, listq.queue) {
    473  1.21  pooka 		if (pg->offset == off) {
    474  1.10  pooka 			return pg;
    475  1.21  pooka 		}
    476  1.21  pooka 	}
    477  1.10  pooka 
    478  1.10  pooka 	return NULL;
    479   1.7  pooka }
    480   1.7  pooka 
    481  1.14  pooka struct vm_page *
    482  1.14  pooka uvm_pageratop(vaddr_t va)
    483  1.14  pooka {
    484  1.15  pooka 	struct rumpva *rva;
    485  1.14  pooka 
    486  1.21  pooka 	mutex_enter(&rvamtx);
    487  1.15  pooka 	LIST_FOREACH(rva, &rvahead, entries)
    488  1.15  pooka 		if (rva->addr == va)
    489  1.21  pooka 			break;
    490  1.21  pooka 	mutex_exit(&rvamtx);
    491  1.21  pooka 
    492  1.21  pooka 	if (rva == NULL)
    493  1.21  pooka 		panic("%s: va %llu", __func__, (unsigned long long)va);
    494  1.15  pooka 
    495  1.21  pooka 	return rva->pg;
    496  1.14  pooka }
    497  1.14  pooka 
    498   1.7  pooka void
    499  1.22  pooka uvm_page_unbusy(struct vm_page **pgs, int npgs)
    500  1.22  pooka {
    501  1.22  pooka 	struct vm_page *pg;
    502  1.22  pooka 	int i;
    503  1.22  pooka 
    504  1.22  pooka 	for (i = 0; i < npgs; i++) {
    505  1.22  pooka 		pg = pgs[i];
    506  1.22  pooka 		if (pg == NULL)
    507  1.22  pooka 			continue;
    508  1.22  pooka 
    509  1.22  pooka 		KASSERT(pg->flags & PG_BUSY);
    510  1.22  pooka 		if (pg->flags & PG_WANTED)
    511  1.22  pooka 			wakeup(pg);
    512  1.36  pooka 		if (pg->flags & PG_RELEASED)
    513  1.36  pooka 			uvm_pagefree(pg);
    514  1.36  pooka 		else
    515  1.36  pooka 			pg->flags &= ~(PG_WANTED|PG_BUSY);
    516  1.22  pooka 	}
    517  1.22  pooka }
    518  1.22  pooka 
    519  1.22  pooka void
    520   1.7  pooka uvm_estimatepageable(int *active, int *inactive)
    521   1.7  pooka {
    522   1.7  pooka 
    523  1.19  pooka 	/* XXX: guessing game */
    524  1.19  pooka 	*active = 1024;
    525  1.19  pooka 	*inactive = 1024;
    526   1.7  pooka }
    527   1.7  pooka 
    528   1.7  pooka void
    529   1.7  pooka uvm_aio_biodone1(struct buf *bp)
    530   1.7  pooka {
    531   1.7  pooka 
    532   1.7  pooka 	panic("%s: unimplemented", __func__);
    533   1.7  pooka }
    534   1.7  pooka 
    535   1.7  pooka void
    536   1.7  pooka uvm_aio_biodone(struct buf *bp)
    537   1.7  pooka {
    538   1.7  pooka 
    539  1.15  pooka 	uvm_aio_aiodone(bp);
    540  1.15  pooka }
    541  1.15  pooka 
    542  1.15  pooka void
    543  1.15  pooka uvm_aio_aiodone(struct buf *bp)
    544  1.15  pooka {
    545  1.15  pooka 
    546  1.25     ad 	if (((bp->b_flags | bp->b_cflags) & (B_READ | BC_NOCACHE)) == 0 && bioopsp)
    547  1.16  pooka 		bioopsp->io_pageiodone(bp);
    548   1.7  pooka }
    549   1.7  pooka 
    550   1.7  pooka void
    551   1.1  pooka uvm_vnp_setsize(struct vnode *vp, voff_t newsize)
    552   1.1  pooka {
    553   1.1  pooka 
    554  1.30  pooka 	mutex_enter(&vp->v_interlock);
    555   1.1  pooka 	vp->v_size = vp->v_writesize = newsize;
    556  1.30  pooka 	mutex_exit(&vp->v_interlock);
    557   1.1  pooka }
    558   1.1  pooka 
    559   1.1  pooka void
    560   1.1  pooka uvm_vnp_setwritesize(struct vnode *vp, voff_t newsize)
    561   1.1  pooka {
    562   1.1  pooka 
    563  1.30  pooka 	mutex_enter(&vp->v_interlock);
    564   1.1  pooka 	vp->v_writesize = newsize;
    565  1.30  pooka 	mutex_exit(&vp->v_interlock);
    566   1.1  pooka }
    567   1.1  pooka 
    568   1.1  pooka void
    569   1.1  pooka uvm_vnp_zerorange(struct vnode *vp, off_t off, size_t len)
    570   1.1  pooka {
    571  1.17  pooka 	struct uvm_object *uobj = &vp->v_uobj;
    572  1.17  pooka 	struct vm_page **pgs;
    573   1.2  pooka 	int maxpages = MIN(32, round_page(len) >> PAGE_SHIFT);
    574   1.2  pooka 	int rv, npages, i;
    575   1.2  pooka 
    576  1.22  pooka 	pgs = kmem_zalloc(maxpages * sizeof(pgs), KM_SLEEP);
    577   1.2  pooka 	while (len) {
    578   1.2  pooka 		npages = MIN(maxpages, round_page(len) >> PAGE_SHIFT);
    579  1.10  pooka 		memset(pgs, 0, npages * sizeof(struct vm_page *));
    580  1.26  pooka 		mutex_enter(&uobj->vmobjlock);
    581   1.2  pooka 		rv = uobj->pgops->pgo_get(uobj, off, pgs, &npages, 0, 0, 0, 0);
    582  1.33  pooka 		KASSERT(npages > 0);
    583   1.2  pooka 
    584   1.2  pooka 		for (i = 0; i < npages; i++) {
    585   1.2  pooka 			uint8_t *start;
    586   1.2  pooka 			size_t chunkoff, chunklen;
    587   1.2  pooka 
    588   1.2  pooka 			chunkoff = off & PAGE_MASK;
    589   1.2  pooka 			chunklen = MIN(PAGE_SIZE - chunkoff, len);
    590   1.5  pooka 			start = (uint8_t *)pgs[i]->uanon + chunkoff;
    591   1.2  pooka 
    592   1.2  pooka 			memset(start, 0, chunklen);
    593  1.37  pooka 			pgs[i]->flags &= ~PG_CLEAN;
    594   1.2  pooka 
    595   1.2  pooka 			off += chunklen;
    596   1.2  pooka 			len -= chunklen;
    597   1.2  pooka 		}
    598  1.22  pooka 		uvm_page_unbusy(pgs, npages);
    599   1.2  pooka 	}
    600  1.22  pooka 	kmem_free(pgs, maxpages * sizeof(pgs));
    601   1.1  pooka 
    602   1.1  pooka 	return;
    603   1.1  pooka }
    604   1.1  pooka 
    605  1.11  pooka struct uvm_ractx *
    606  1.11  pooka uvm_ra_allocctx()
    607  1.11  pooka {
    608  1.11  pooka 
    609  1.11  pooka 	return NULL;
    610  1.11  pooka }
    611  1.11  pooka 
    612  1.11  pooka void
    613  1.11  pooka uvm_ra_freectx(struct uvm_ractx *ra)
    614  1.11  pooka {
    615  1.11  pooka 
    616  1.11  pooka 	return;
    617  1.11  pooka }
    618  1.11  pooka 
    619   1.1  pooka bool
    620   1.1  pooka uvn_clean_p(struct uvm_object *uobj)
    621   1.1  pooka {
    622  1.10  pooka 	struct vnode *vp = (void *)uobj;
    623   1.1  pooka 
    624  1.18     ad 	return (vp->v_iflag & VI_ONWORKLST) == 0;
    625   1.1  pooka }
    626   1.9  pooka 
    627  1.39  pooka struct vm_map_kernel *
    628  1.39  pooka vm_map_to_kernel(struct vm_map *map)
    629  1.39  pooka {
    630  1.39  pooka 
    631  1.39  pooka 	return (struct vm_map_kernel *)map;
    632  1.39  pooka }
    633  1.39  pooka 
    634  1.41  pooka bool
    635  1.41  pooka vm_map_starved_p(struct vm_map *map)
    636  1.41  pooka {
    637  1.41  pooka 
    638  1.41  pooka 	return false;
    639  1.41  pooka }
    640  1.41  pooka 
    641  1.39  pooka void
    642  1.39  pooka uvm_pageout_start(int npages)
    643  1.39  pooka {
    644  1.39  pooka 
    645  1.39  pooka 	uvmexp.paging += npages;
    646  1.39  pooka }
    647  1.39  pooka 
    648  1.39  pooka void
    649  1.39  pooka uvm_pageout_done(int npages)
    650  1.39  pooka {
    651  1.39  pooka 
    652  1.39  pooka 	uvmexp.paging -= npages;
    653  1.39  pooka 
    654  1.39  pooka 	/*
    655  1.39  pooka 	 * wake up either of pagedaemon or LWPs waiting for it.
    656  1.39  pooka 	 */
    657  1.39  pooka 
    658  1.39  pooka 	if (uvmexp.free <= uvmexp.reserve_kernel) {
    659  1.39  pooka 		wakeup(&uvm.pagedaemon);
    660  1.39  pooka 	} else {
    661  1.39  pooka 		wakeup(&uvmexp.free);
    662  1.39  pooka 	}
    663  1.39  pooka }
    664  1.39  pooka 
    665  1.40  pooka /* XXX: following two are unfinished because lwp's are not refcounted yet */
    666  1.40  pooka void
    667  1.40  pooka uvm_lwp_hold(struct lwp *l)
    668  1.40  pooka {
    669  1.40  pooka 
    670  1.40  pooka 	atomic_inc_uint(&l->l_holdcnt);
    671  1.40  pooka }
    672  1.40  pooka 
    673  1.40  pooka void
    674  1.40  pooka uvm_lwp_rele(struct lwp *l)
    675  1.40  pooka {
    676  1.40  pooka 
    677  1.40  pooka 	atomic_dec_uint(&l->l_holdcnt);
    678  1.40  pooka }
    679  1.40  pooka 
    680  1.41  pooka int
    681  1.41  pooka uvm_loan(struct vm_map *map, vaddr_t start, vsize_t len, void *v, int flags)
    682  1.41  pooka {
    683  1.41  pooka 
    684  1.41  pooka 	panic("%s: unimplemented", __func__);
    685  1.41  pooka }
    686  1.41  pooka 
    687  1.41  pooka void
    688  1.41  pooka uvm_unloan(void *v, int npages, int flags)
    689  1.41  pooka {
    690  1.41  pooka 
    691  1.41  pooka 	panic("%s: unimplemented", __func__);
    692  1.41  pooka }
    693  1.41  pooka 
    694   1.9  pooka /*
    695   1.9  pooka  * Kmem
    696   1.9  pooka  */
    697   1.9  pooka 
    698  1.39  pooka #ifndef RUMP_USE_REAL_KMEM
    699   1.9  pooka void *
    700   1.9  pooka kmem_alloc(size_t size, km_flag_t kmflag)
    701   1.9  pooka {
    702   1.9  pooka 
    703   1.9  pooka 	return rumpuser_malloc(size, kmflag == KM_NOSLEEP);
    704   1.9  pooka }
    705   1.9  pooka 
    706   1.9  pooka void *
    707   1.9  pooka kmem_zalloc(size_t size, km_flag_t kmflag)
    708   1.9  pooka {
    709   1.9  pooka 	void *rv;
    710   1.9  pooka 
    711   1.9  pooka 	rv = kmem_alloc(size, kmflag);
    712   1.9  pooka 	if (rv)
    713   1.9  pooka 		memset(rv, 0, size);
    714   1.9  pooka 
    715   1.9  pooka 	return rv;
    716   1.9  pooka }
    717   1.9  pooka 
    718   1.9  pooka void
    719   1.9  pooka kmem_free(void *p, size_t size)
    720   1.9  pooka {
    721   1.9  pooka 
    722   1.9  pooka 	rumpuser_free(p);
    723   1.9  pooka }
    724  1.35  pooka #endif /* RUMP_USE_REAL_KMEM */
    725  1.12  pooka 
    726  1.12  pooka /*
    727  1.12  pooka  * UVM km
    728  1.12  pooka  */
    729  1.12  pooka 
    730  1.12  pooka vaddr_t
    731  1.12  pooka uvm_km_alloc(struct vm_map *map, vsize_t size, vsize_t align, uvm_flag_t flags)
    732  1.12  pooka {
    733  1.12  pooka 	void *rv;
    734  1.12  pooka 
    735  1.12  pooka 	rv = rumpuser_malloc(size, flags & (UVM_KMF_CANFAIL | UVM_KMF_NOWAIT));
    736  1.12  pooka 	if (rv && flags & UVM_KMF_ZERO)
    737  1.12  pooka 		memset(rv, 0, size);
    738  1.12  pooka 
    739  1.12  pooka 	return (vaddr_t)rv;
    740  1.12  pooka }
    741  1.12  pooka 
    742  1.12  pooka void
    743  1.12  pooka uvm_km_free(struct vm_map *map, vaddr_t vaddr, vsize_t size, uvm_flag_t flags)
    744  1.12  pooka {
    745  1.12  pooka 
    746  1.12  pooka 	rumpuser_free((void *)vaddr);
    747  1.12  pooka }
    748  1.12  pooka 
    749  1.12  pooka struct vm_map *
    750  1.12  pooka uvm_km_suballoc(struct vm_map *map, vaddr_t *minaddr, vaddr_t *maxaddr,
    751  1.12  pooka 	vsize_t size, int pageable, bool fixed, struct vm_map_kernel *submap)
    752  1.12  pooka {
    753  1.12  pooka 
    754  1.12  pooka 	return (struct vm_map *)417416;
    755  1.12  pooka }
    756  1.40  pooka 
    757  1.40  pooka vaddr_t
    758  1.40  pooka uvm_km_alloc_poolpage(struct vm_map *map, bool waitok)
    759  1.40  pooka {
    760  1.40  pooka 
    761  1.40  pooka 	return (vaddr_t)rumpuser_malloc(PAGE_SIZE, !waitok);
    762  1.40  pooka }
    763  1.40  pooka 
    764  1.40  pooka void
    765  1.40  pooka uvm_km_free_poolpage(struct vm_map *map, vaddr_t addr)
    766  1.40  pooka {
    767  1.40  pooka 
    768  1.40  pooka 	rumpuser_free((void *)addr);
    769  1.40  pooka }
    770