Home | History | Annotate | Line # | Download | only in rumpkern
vm.c revision 1.84
      1 /*	$NetBSD: vm.c,v 1.84 2010/06/14 21:04:56 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2007-2010 Antti Kantee.  All Rights Reserved.
      5  *
      6  * Development of this software was supported by
      7  * The Finnish Cultural Foundation and the Research Foundation of
      8  * The Helsinki University of Technology.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     20  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     22  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Virtual memory emulation routines.  Contents:
     34  *  + anon objects & pager
     35  *  + misc support routines
     36  */
     37 
     38 /*
     39  * XXX: we abuse pg->uanon for the virtual address of the storage
     40  * for each page.  phys_addr would fit the job description better,
     41  * except that it will create unnecessary lossage on some platforms
     42  * due to not being a pointer type.
     43  */
     44 
     45 #include <sys/cdefs.h>
     46 __KERNEL_RCSID(0, "$NetBSD: vm.c,v 1.84 2010/06/14 21:04:56 pooka Exp $");
     47 
     48 #include <sys/param.h>
     49 #include <sys/atomic.h>
     50 #include <sys/buf.h>
     51 #include <sys/kernel.h>
     52 #include <sys/kmem.h>
     53 #include <sys/mman.h>
     54 #include <sys/null.h>
     55 #include <sys/vnode.h>
     56 
     57 #include <machine/pmap.h>
     58 
     59 #include <rump/rumpuser.h>
     60 
     61 #include <uvm/uvm.h>
     62 #include <uvm/uvm_ddb.h>
     63 #include <uvm/uvm_prot.h>
     64 #include <uvm/uvm_readahead.h>
     65 
     66 #include "rump_private.h"
     67 
     68 static int ao_get(struct uvm_object *, voff_t, struct vm_page **,
     69 	int *, int, vm_prot_t, int, int);
     70 static int ao_put(struct uvm_object *, voff_t, voff_t, int);
     71 
     72 const struct uvm_pagerops aobj_pager = {
     73 	.pgo_get = ao_get,
     74 	.pgo_put = ao_put,
     75 };
     76 
     77 kmutex_t uvm_pageqlock;
     78 
     79 struct uvmexp uvmexp;
     80 struct uvm uvm;
     81 
     82 struct vm_map rump_vmmap;
     83 static struct vm_map_kernel kmem_map_store;
     84 struct vm_map *kmem_map = &kmem_map_store.vmk_map;
     85 const struct rb_tree_ops uvm_page_tree_ops;
     86 
     87 static struct vm_map_kernel kernel_map_store;
     88 struct vm_map *kernel_map = &kernel_map_store.vmk_map;
     89 
     90 static unsigned int pdaemon_waiters;
     91 static kmutex_t pdaemonmtx;
     92 static kcondvar_t pdaemoncv, oomwait;
     93 
     94 #define RUMPMEM_UNLIMITED ((unsigned long)-1)
     95 static unsigned long physmemlimit = RUMPMEM_UNLIMITED;
     96 static unsigned long curphysmem;
     97 
     98 /*
     99  * vm pages
    100  */
    101 
    102 /* called with the object locked */
    103 struct vm_page *
    104 uvm_pagealloc_strat(struct uvm_object *uobj, voff_t off, struct vm_anon *anon,
    105 	int flags, int strat, int free_list)
    106 {
    107 	struct vm_page *pg;
    108 
    109 	pg = kmem_zalloc(sizeof(struct vm_page), KM_SLEEP);
    110 	pg->offset = off;
    111 	pg->uobject = uobj;
    112 
    113 	pg->uanon = (void *)kmem_alloc(PAGE_SIZE, KM_SLEEP);
    114 	if (flags & UVM_PGA_ZERO)
    115 		memset(pg->uanon, 0, PAGE_SIZE);
    116 	pg->flags = PG_CLEAN|PG_BUSY|PG_FAKE;
    117 
    118 	TAILQ_INSERT_TAIL(&uobj->memq, pg, listq.queue);
    119 	uobj->uo_npages++;
    120 
    121 	return pg;
    122 }
    123 
    124 /*
    125  * Release a page.
    126  *
    127  * Called with the vm object locked.
    128  */
    129 void
    130 uvm_pagefree(struct vm_page *pg)
    131 {
    132 	struct uvm_object *uobj = pg->uobject;
    133 
    134 	if (pg->flags & PG_WANTED)
    135 		wakeup(pg);
    136 
    137 	uobj->uo_npages--;
    138 	TAILQ_REMOVE(&uobj->memq, pg, listq.queue);
    139 	kmem_free((void *)pg->uanon, PAGE_SIZE);
    140 	kmem_free(pg, sizeof(*pg));
    141 }
    142 
    143 void
    144 uvm_pagezero(struct vm_page *pg)
    145 {
    146 
    147 	pg->flags &= ~PG_CLEAN;
    148 	memset((void *)pg->uanon, 0, PAGE_SIZE);
    149 }
    150 
    151 /*
    152  * Anon object stuff
    153  */
    154 
    155 static int
    156 ao_get(struct uvm_object *uobj, voff_t off, struct vm_page **pgs,
    157 	int *npages, int centeridx, vm_prot_t access_type,
    158 	int advice, int flags)
    159 {
    160 	struct vm_page *pg;
    161 	int i;
    162 
    163 	if (centeridx)
    164 		panic("%s: centeridx != 0 not supported", __func__);
    165 
    166 	/* loop over pages */
    167 	off = trunc_page(off);
    168 	for (i = 0; i < *npages; i++) {
    169  retrylookup:
    170 		pg = uvm_pagelookup(uobj, off + (i << PAGE_SHIFT));
    171 		if (pg) {
    172 			if (pg->flags & PG_BUSY) {
    173 				pg->flags |= PG_WANTED;
    174 				UVM_UNLOCK_AND_WAIT(pg, &uobj->vmobjlock, 0,
    175 				    "aogetpg", 0);
    176 				goto retrylookup;
    177 			}
    178 			pg->flags |= PG_BUSY;
    179 			pgs[i] = pg;
    180 		} else {
    181 			pg = uvm_pagealloc(uobj,
    182 			    off + (i << PAGE_SHIFT), NULL, UVM_PGA_ZERO);
    183 			pgs[i] = pg;
    184 		}
    185 	}
    186 	mutex_exit(&uobj->vmobjlock);
    187 
    188 	return 0;
    189 
    190 }
    191 
    192 static int
    193 ao_put(struct uvm_object *uobj, voff_t start, voff_t stop, int flags)
    194 {
    195 	struct vm_page *pg;
    196 
    197 	/* we only free all pages for now */
    198 	if ((flags & PGO_FREE) == 0 || (flags & PGO_ALLPAGES) == 0) {
    199 		mutex_exit(&uobj->vmobjlock);
    200 		return 0;
    201 	}
    202 
    203 	while ((pg = TAILQ_FIRST(&uobj->memq)) != NULL)
    204 		uvm_pagefree(pg);
    205 	mutex_exit(&uobj->vmobjlock);
    206 
    207 	return 0;
    208 }
    209 
    210 struct uvm_object *
    211 uao_create(vsize_t size, int flags)
    212 {
    213 	struct uvm_object *uobj;
    214 
    215 	uobj = kmem_zalloc(sizeof(struct uvm_object), KM_SLEEP);
    216 	uobj->pgops = &aobj_pager;
    217 	TAILQ_INIT(&uobj->memq);
    218 	mutex_init(&uobj->vmobjlock, MUTEX_DEFAULT, IPL_NONE);
    219 
    220 	return uobj;
    221 }
    222 
    223 void
    224 uao_detach(struct uvm_object *uobj)
    225 {
    226 
    227 	mutex_enter(&uobj->vmobjlock);
    228 	ao_put(uobj, 0, 0, PGO_ALLPAGES | PGO_FREE);
    229 	mutex_destroy(&uobj->vmobjlock);
    230 	kmem_free(uobj, sizeof(*uobj));
    231 }
    232 
    233 /*
    234  * Misc routines
    235  */
    236 
    237 static kmutex_t pagermtx;
    238 
    239 void
    240 uvm_init(void)
    241 {
    242 	char buf[64];
    243 	int error;
    244 
    245 	if (rumpuser_getenv("RUMP_MEMLIMIT", buf, sizeof(buf), &error) == 0) {
    246 		physmemlimit = strtoll(buf, NULL, 10);
    247 		/* it's not like we'd get far with, say, 1 byte, but ... */
    248 		if (physmemlimit == 0)
    249 			panic("uvm_init: no memory available");
    250 #define HUMANIZE_BYTES 9
    251 		CTASSERT(sizeof(buf) >= HUMANIZE_BYTES);
    252 		format_bytes(buf, HUMANIZE_BYTES, physmemlimit);
    253 #undef HUMANIZE_BYTES
    254 	} else {
    255 		strlcpy(buf, "unlimited (host limit)", sizeof(buf));
    256 	}
    257 	aprint_verbose("total memory = %s\n", buf);
    258 
    259 	uvmexp.free = 1024*1024; /* XXX: arbitrary & not updated */
    260 
    261 	mutex_init(&pagermtx, MUTEX_DEFAULT, 0);
    262 	mutex_init(&uvm_pageqlock, MUTEX_DEFAULT, 0);
    263 
    264 	mutex_init(&pdaemonmtx, MUTEX_DEFAULT, 0);
    265 	cv_init(&pdaemoncv, "pdaemon");
    266 	cv_init(&oomwait, "oomwait");
    267 
    268 	kernel_map->pmap = pmap_kernel();
    269 	callback_head_init(&kernel_map_store.vmk_reclaim_callback, IPL_VM);
    270 	kmem_map->pmap = pmap_kernel();
    271 	callback_head_init(&kmem_map_store.vmk_reclaim_callback, IPL_VM);
    272 }
    273 
    274 void
    275 uvmspace_init(struct vmspace *vm, struct pmap *pmap, vaddr_t vmin, vaddr_t vmax)
    276 {
    277 
    278 	vm->vm_map.pmap = pmap_kernel();
    279 	vm->vm_refcnt = 1;
    280 }
    281 
    282 void
    283 uvm_pagewire(struct vm_page *pg)
    284 {
    285 
    286 	/* nada */
    287 }
    288 
    289 void
    290 uvm_pageunwire(struct vm_page *pg)
    291 {
    292 
    293 	/* nada */
    294 }
    295 
    296 /* where's your schmonz now? */
    297 #define PUNLIMIT(a)	\
    298 p->p_rlimit[a].rlim_cur = p->p_rlimit[a].rlim_max = RLIM_INFINITY;
    299 void
    300 uvm_init_limits(struct proc *p)
    301 {
    302 
    303 	PUNLIMIT(RLIMIT_STACK);
    304 	PUNLIMIT(RLIMIT_DATA);
    305 	PUNLIMIT(RLIMIT_RSS);
    306 	PUNLIMIT(RLIMIT_AS);
    307 	/* nice, cascade */
    308 }
    309 #undef PUNLIMIT
    310 
    311 /*
    312  * This satisfies the "disgusting mmap hack" used by proplib.
    313  * We probably should grow some more assertables to make sure we're
    314  * not satisfying anything we shouldn't be satisfying.  At least we
    315  * should make sure it's the local machine we're mmapping ...
    316  */
    317 int
    318 uvm_mmap(struct vm_map *map, vaddr_t *addr, vsize_t size, vm_prot_t prot,
    319 	vm_prot_t maxprot, int flags, void *handle, voff_t off, vsize_t locklim)
    320 {
    321 	void *uaddr;
    322 	int error;
    323 
    324 	if (prot != (VM_PROT_READ | VM_PROT_WRITE))
    325 		panic("uvm_mmap() variant unsupported");
    326 	if (flags != (MAP_PRIVATE | MAP_ANON))
    327 		panic("uvm_mmap() variant unsupported");
    328 	/* no reason in particular, but cf. uvm_default_mapaddr() */
    329 	if (*addr != 0)
    330 		panic("uvm_mmap() variant unsupported");
    331 
    332 	uaddr = rumpuser_anonmmap(NULL, size, 0, 0, &error);
    333 	if (uaddr == NULL)
    334 		return error;
    335 
    336 	*addr = (vaddr_t)uaddr;
    337 	return 0;
    338 }
    339 
    340 struct pagerinfo {
    341 	vaddr_t pgr_kva;
    342 	int pgr_npages;
    343 	struct vm_page **pgr_pgs;
    344 	bool pgr_read;
    345 
    346 	LIST_ENTRY(pagerinfo) pgr_entries;
    347 };
    348 static LIST_HEAD(, pagerinfo) pagerlist = LIST_HEAD_INITIALIZER(pagerlist);
    349 
    350 /*
    351  * Pager "map" in routine.  Instead of mapping, we allocate memory
    352  * and copy page contents there.  Not optimal or even strictly
    353  * correct (the caller might modify the page contents after mapping
    354  * them in), but what the heck.  Assumes UVMPAGER_MAPIN_WAITOK.
    355  */
    356 vaddr_t
    357 uvm_pagermapin(struct vm_page **pgs, int npages, int flags)
    358 {
    359 	struct pagerinfo *pgri;
    360 	vaddr_t curkva;
    361 	int i;
    362 
    363 	/* allocate structures */
    364 	pgri = kmem_alloc(sizeof(*pgri), KM_SLEEP);
    365 	pgri->pgr_kva = (vaddr_t)kmem_alloc(npages * PAGE_SIZE, KM_SLEEP);
    366 	pgri->pgr_npages = npages;
    367 	pgri->pgr_pgs = kmem_alloc(sizeof(struct vm_page *) * npages, KM_SLEEP);
    368 	pgri->pgr_read = (flags & UVMPAGER_MAPIN_READ) != 0;
    369 
    370 	/* copy contents to "mapped" memory */
    371 	for (i = 0, curkva = pgri->pgr_kva;
    372 	    i < npages;
    373 	    i++, curkva += PAGE_SIZE) {
    374 		/*
    375 		 * We need to copy the previous contents of the pages to
    376 		 * the window even if we are reading from the
    377 		 * device, since the device might not fill the contents of
    378 		 * the full mapped range and we will end up corrupting
    379 		 * data when we unmap the window.
    380 		 */
    381 		memcpy((void*)curkva, pgs[i]->uanon, PAGE_SIZE);
    382 		pgri->pgr_pgs[i] = pgs[i];
    383 	}
    384 
    385 	mutex_enter(&pagermtx);
    386 	LIST_INSERT_HEAD(&pagerlist, pgri, pgr_entries);
    387 	mutex_exit(&pagermtx);
    388 
    389 	return pgri->pgr_kva;
    390 }
    391 
    392 /*
    393  * map out the pager window.  return contents from VA to page storage
    394  * and free structures.
    395  *
    396  * Note: does not currently support partial frees
    397  */
    398 void
    399 uvm_pagermapout(vaddr_t kva, int npages)
    400 {
    401 	struct pagerinfo *pgri;
    402 	vaddr_t curkva;
    403 	int i;
    404 
    405 	mutex_enter(&pagermtx);
    406 	LIST_FOREACH(pgri, &pagerlist, pgr_entries) {
    407 		if (pgri->pgr_kva == kva)
    408 			break;
    409 	}
    410 	KASSERT(pgri);
    411 	if (pgri->pgr_npages != npages)
    412 		panic("uvm_pagermapout: partial unmapping not supported");
    413 	LIST_REMOVE(pgri, pgr_entries);
    414 	mutex_exit(&pagermtx);
    415 
    416 	if (pgri->pgr_read) {
    417 		for (i = 0, curkva = pgri->pgr_kva;
    418 		    i < pgri->pgr_npages;
    419 		    i++, curkva += PAGE_SIZE) {
    420 			memcpy(pgri->pgr_pgs[i]->uanon,(void*)curkva,PAGE_SIZE);
    421 		}
    422 	}
    423 
    424 	kmem_free(pgri->pgr_pgs, npages * sizeof(struct vm_page *));
    425 	kmem_free((void*)pgri->pgr_kva, npages * PAGE_SIZE);
    426 	kmem_free(pgri, sizeof(*pgri));
    427 }
    428 
    429 /*
    430  * convert va in pager window to page structure.
    431  * XXX: how expensive is this (global lock, list traversal)?
    432  */
    433 struct vm_page *
    434 uvm_pageratop(vaddr_t va)
    435 {
    436 	struct pagerinfo *pgri;
    437 	struct vm_page *pg = NULL;
    438 	int i;
    439 
    440 	mutex_enter(&pagermtx);
    441 	LIST_FOREACH(pgri, &pagerlist, pgr_entries) {
    442 		if (pgri->pgr_kva <= va
    443 		    && va < pgri->pgr_kva + pgri->pgr_npages*PAGE_SIZE)
    444 			break;
    445 	}
    446 	if (pgri) {
    447 		i = (va - pgri->pgr_kva) >> PAGE_SHIFT;
    448 		pg = pgri->pgr_pgs[i];
    449 	}
    450 	mutex_exit(&pagermtx);
    451 
    452 	return pg;
    453 }
    454 
    455 /* Called with the vm object locked */
    456 struct vm_page *
    457 uvm_pagelookup(struct uvm_object *uobj, voff_t off)
    458 {
    459 	struct vm_page *pg;
    460 
    461 	TAILQ_FOREACH(pg, &uobj->memq, listq.queue) {
    462 		if (pg->offset == off) {
    463 			return pg;
    464 		}
    465 	}
    466 
    467 	return NULL;
    468 }
    469 
    470 void
    471 uvm_page_unbusy(struct vm_page **pgs, int npgs)
    472 {
    473 	struct vm_page *pg;
    474 	int i;
    475 
    476 	for (i = 0; i < npgs; i++) {
    477 		pg = pgs[i];
    478 		if (pg == NULL)
    479 			continue;
    480 
    481 		KASSERT(pg->flags & PG_BUSY);
    482 		if (pg->flags & PG_WANTED)
    483 			wakeup(pg);
    484 		if (pg->flags & PG_RELEASED)
    485 			uvm_pagefree(pg);
    486 		else
    487 			pg->flags &= ~(PG_WANTED|PG_BUSY);
    488 	}
    489 }
    490 
    491 void
    492 uvm_estimatepageable(int *active, int *inactive)
    493 {
    494 
    495 	/* XXX: guessing game */
    496 	*active = 1024;
    497 	*inactive = 1024;
    498 }
    499 
    500 struct vm_map_kernel *
    501 vm_map_to_kernel(struct vm_map *map)
    502 {
    503 
    504 	return (struct vm_map_kernel *)map;
    505 }
    506 
    507 bool
    508 vm_map_starved_p(struct vm_map *map)
    509 {
    510 
    511 	if (map->flags & VM_MAP_WANTVA)
    512 		return true;
    513 
    514 	return false;
    515 }
    516 
    517 int
    518 uvm_loan(struct vm_map *map, vaddr_t start, vsize_t len, void *v, int flags)
    519 {
    520 
    521 	panic("%s: unimplemented", __func__);
    522 }
    523 
    524 void
    525 uvm_unloan(void *v, int npages, int flags)
    526 {
    527 
    528 	panic("%s: unimplemented", __func__);
    529 }
    530 
    531 int
    532 uvm_loanuobjpages(struct uvm_object *uobj, voff_t pgoff, int orignpages,
    533 	struct vm_page **opp)
    534 {
    535 
    536 	return EBUSY;
    537 }
    538 
    539 #ifdef DEBUGPRINT
    540 void
    541 uvm_object_printit(struct uvm_object *uobj, bool full,
    542 	void (*pr)(const char *, ...))
    543 {
    544 
    545 	pr("VM OBJECT at %p, refs %d", uobj, uobj->uo_refs);
    546 }
    547 #endif
    548 
    549 vaddr_t
    550 uvm_default_mapaddr(struct proc *p, vaddr_t base, vsize_t sz)
    551 {
    552 
    553 	return 0;
    554 }
    555 
    556 int
    557 uvm_map_protect(struct vm_map *map, vaddr_t start, vaddr_t end,
    558 	vm_prot_t prot, bool set_max)
    559 {
    560 
    561 	return EOPNOTSUPP;
    562 }
    563 
    564 /*
    565  * UVM km
    566  */
    567 
    568 vaddr_t
    569 uvm_km_alloc(struct vm_map *map, vsize_t size, vsize_t align, uvm_flag_t flags)
    570 {
    571 	void *rv, *desired = NULL;
    572 	int alignbit, error;
    573 
    574 #ifdef __x86_64__
    575 	/*
    576 	 * On amd64, allocate all module memory from the lowest 2GB.
    577 	 * This is because NetBSD kernel modules are compiled
    578 	 * with -mcmodel=kernel and reserve only 4 bytes for
    579 	 * offsets.  If we load code compiled with -mcmodel=kernel
    580 	 * anywhere except the lowest or highest 2GB, it will not
    581 	 * work.  Since userspace does not have access to the highest
    582 	 * 2GB, use the lowest 2GB.
    583 	 *
    584 	 * Note: this assumes the rump kernel resides in
    585 	 * the lowest 2GB as well.
    586 	 *
    587 	 * Note2: yes, it's a quick hack, but since this the only
    588 	 * place where we care about the map we're allocating from,
    589 	 * just use a simple "if" instead of coming up with a fancy
    590 	 * generic solution.
    591 	 */
    592 	extern struct vm_map *module_map;
    593 	if (map == module_map) {
    594 		desired = (void *)(0x80000000 - size);
    595 	}
    596 #endif
    597 
    598 	alignbit = 0;
    599 	if (align) {
    600 		alignbit = ffs(align)-1;
    601 	}
    602 
    603 	rv = rumpuser_anonmmap(desired, size, alignbit, flags & UVM_KMF_EXEC,
    604 	    &error);
    605 	if (rv == NULL) {
    606 		if (flags & (UVM_KMF_CANFAIL | UVM_KMF_NOWAIT))
    607 			return 0;
    608 		else
    609 			panic("uvm_km_alloc failed");
    610 	}
    611 
    612 	if (flags & UVM_KMF_ZERO)
    613 		memset(rv, 0, size);
    614 
    615 	return (vaddr_t)rv;
    616 }
    617 
    618 void
    619 uvm_km_free(struct vm_map *map, vaddr_t vaddr, vsize_t size, uvm_flag_t flags)
    620 {
    621 
    622 	rumpuser_unmap((void *)vaddr, size);
    623 }
    624 
    625 struct vm_map *
    626 uvm_km_suballoc(struct vm_map *map, vaddr_t *minaddr, vaddr_t *maxaddr,
    627 	vsize_t size, int pageable, bool fixed, struct vm_map_kernel *submap)
    628 {
    629 
    630 	return (struct vm_map *)417416;
    631 }
    632 
    633 vaddr_t
    634 uvm_km_alloc_poolpage(struct vm_map *map, bool waitok)
    635 {
    636 
    637 	return (vaddr_t)rump_hypermalloc(PAGE_SIZE, PAGE_SIZE,
    638 	    waitok, "kmalloc");
    639 }
    640 
    641 void
    642 uvm_km_free_poolpage(struct vm_map *map, vaddr_t addr)
    643 {
    644 
    645 	rump_hyperfree((void *)addr, PAGE_SIZE);
    646 }
    647 
    648 vaddr_t
    649 uvm_km_alloc_poolpage_cache(struct vm_map *map, bool waitok)
    650 {
    651 
    652 	return uvm_km_alloc_poolpage(map, waitok);
    653 }
    654 
    655 void
    656 uvm_km_free_poolpage_cache(struct vm_map *map, vaddr_t vaddr)
    657 {
    658 
    659 	uvm_km_free_poolpage(map, vaddr);
    660 }
    661 
    662 void
    663 uvm_km_va_drain(struct vm_map *map, uvm_flag_t flags)
    664 {
    665 
    666 	/* we eventually maybe want some model for available memory */
    667 }
    668 
    669 /*
    670  * Mapping and vm space locking routines.
    671  * XXX: these don't work for non-local vmspaces
    672  */
    673 int
    674 uvm_vslock(struct vmspace *vs, void *addr, size_t len, vm_prot_t access)
    675 {
    676 
    677 	KASSERT(vs == &vmspace0);
    678 	return 0;
    679 }
    680 
    681 void
    682 uvm_vsunlock(struct vmspace *vs, void *addr, size_t len)
    683 {
    684 
    685 	KASSERT(vs == &vmspace0);
    686 }
    687 
    688 void
    689 vmapbuf(struct buf *bp, vsize_t len)
    690 {
    691 
    692 	bp->b_saveaddr = bp->b_data;
    693 }
    694 
    695 void
    696 vunmapbuf(struct buf *bp, vsize_t len)
    697 {
    698 
    699 	bp->b_data = bp->b_saveaddr;
    700 	bp->b_saveaddr = 0;
    701 }
    702 
    703 void
    704 uvmspace_addref(struct vmspace *vm)
    705 {
    706 
    707 	/*
    708 	 * there is only vmspace0.  we're not planning on
    709 	 * feeding it to the fishes.
    710 	 */
    711 }
    712 
    713 void
    714 uvmspace_free(struct vmspace *vm)
    715 {
    716 
    717 	/* nothing for now */
    718 }
    719 
    720 int
    721 uvm_io(struct vm_map *map, struct uio *uio)
    722 {
    723 
    724 	/*
    725 	 * just do direct uio for now.  but this needs some vmspace
    726 	 * olympics for rump_sysproxy.
    727 	 */
    728 	return uiomove((void *)(vaddr_t)uio->uio_offset, uio->uio_resid, uio);
    729 }
    730 
    731 /*
    732  * page life cycle stuff.  it really doesn't exist, so just stubs.
    733  */
    734 
    735 void
    736 uvm_pageactivate(struct vm_page *pg)
    737 {
    738 
    739 	/* nada */
    740 }
    741 
    742 void
    743 uvm_pagedeactivate(struct vm_page *pg)
    744 {
    745 
    746 	/* nada */
    747 }
    748 
    749 void
    750 uvm_pagedequeue(struct vm_page *pg)
    751 {
    752 
    753 	/* nada*/
    754 }
    755 
    756 void
    757 uvm_pageenqueue(struct vm_page *pg)
    758 {
    759 
    760 	/* nada */
    761 }
    762 
    763 /*
    764  * Routines related to the Page Baroness.
    765  */
    766 
    767 void
    768 uvm_wait(const char *msg)
    769 {
    770 
    771 	if (__predict_false(curlwp == uvm.pagedaemon_lwp))
    772 		panic("pagedaemon out of memory");
    773 	if (__predict_false(rump_threads == 0))
    774 		panic("pagedaemon missing (RUMP_THREADS = 0)");
    775 
    776 	mutex_enter(&pdaemonmtx);
    777 	pdaemon_waiters++;
    778 	cv_signal(&pdaemoncv);
    779 	cv_wait(&oomwait, &pdaemonmtx);
    780 	mutex_exit(&pdaemonmtx);
    781 }
    782 
    783 void
    784 uvm_pageout_start(int npages)
    785 {
    786 
    787 	/* we don't have the heuristics */
    788 }
    789 
    790 void
    791 uvm_pageout_done(int npages)
    792 {
    793 
    794 	/* could wakeup waiters, but just let the pagedaemon do it */
    795 }
    796 
    797 /*
    798  * Under-construction page mistress.  This is lacking vfs support, namely:
    799  *
    800  *  1) draining vfs buffers
    801  *  2) paging out pages in vm vnode objects
    802  *     (we will not page out anon memory on the basis that
    803  *     that's the task of the host)
    804  */
    805 
    806 void
    807 uvm_pageout(void *arg)
    808 {
    809 	struct pool *pp, *pp_first;
    810 	uint64_t where;
    811 	int timo = 0;
    812 	bool succ;
    813 
    814 	mutex_enter(&pdaemonmtx);
    815 	for (;;) {
    816 		cv_timedwait(&pdaemoncv, &pdaemonmtx, timo);
    817 		uvmexp.pdwoke++;
    818 		kernel_map->flags |= VM_MAP_WANTVA;
    819 		mutex_exit(&pdaemonmtx);
    820 
    821 		succ = false;
    822 		pool_drain_start(&pp_first, &where);
    823 		pp = pp_first;
    824 		for (;;) {
    825 			succ = pool_drain_end(pp, where);
    826 			if (succ)
    827 				break;
    828 			pool_drain_start(&pp, &where);
    829 			if (pp == pp_first) {
    830 				succ = pool_drain_end(pp, where);
    831 				break;
    832 			}
    833 		}
    834 		mutex_enter(&pdaemonmtx);
    835 
    836 		if (!succ) {
    837 			rumpuser_dprintf("pagedaemoness: failed to reclaim "
    838 			    "memory ... sleeping (deadlock?)\n");
    839 			timo = hz;
    840 			continue;
    841 		}
    842 		kernel_map->flags &= ~VM_MAP_WANTVA;
    843 		timo = 0;
    844 
    845 		if (pdaemon_waiters) {
    846 			pdaemon_waiters = 0;
    847 			cv_broadcast(&oomwait);
    848 		}
    849 	}
    850 
    851 	panic("you can swap out any time you like, but you can never leave");
    852 }
    853 
    854 /*
    855  * In a regular kernel the pagedaemon is activated when memory becomes
    856  * low.  In a virtual rump kernel we do not know exactly how much memory
    857  * we have available -- it depends on the conditions on the host.
    858  * Therefore, we cannot preemptively kick the pagedaemon.  Rather, we
    859  * wait until things we desperate and we're forced to uvm_wait().
    860  *
    861  * The alternative would be to allocate a huge chunk of memory at
    862  * startup, but that solution has a number of problems including
    863  * being a resource hog, failing anyway due to host memory overcommit
    864  * and core dump size.
    865  */
    866 
    867 void
    868 uvm_kick_pdaemon()
    869 {
    870 
    871 	/* nada */
    872 }
    873 
    874 void *
    875 rump_hypermalloc(size_t howmuch, int alignment, bool waitok, const char *wmsg)
    876 {
    877 	unsigned long newmem;
    878 	void *rv;
    879 
    880 	/* first we must be within the limit */
    881  limitagain:
    882 	if (physmemlimit != RUMPMEM_UNLIMITED) {
    883 		newmem = atomic_add_long_nv(&curphysmem, howmuch);
    884 		if (newmem > physmemlimit) {
    885 			newmem = atomic_add_long_nv(&curphysmem, -howmuch);
    886 			if (!waitok)
    887 				return NULL;
    888 			uvm_wait(wmsg);
    889 			goto limitagain;
    890 		}
    891 	}
    892 
    893 	/* second, we must get something from the backend */
    894  again:
    895 	rv = rumpuser_malloc(howmuch, alignment);
    896 	if (__predict_false(rv == NULL && waitok)) {
    897 		uvm_wait(wmsg);
    898 		goto again;
    899 	}
    900 
    901 	return rv;
    902 }
    903 
    904 void
    905 rump_hyperfree(void *what, size_t size)
    906 {
    907 
    908 	if (physmemlimit != RUMPMEM_UNLIMITED) {
    909 		atomic_add_long(&curphysmem, -size);
    910 	}
    911 	rumpuser_free(what);
    912 }
    913