Home | History | Annotate | Line # | Download | only in rumpkern
vm.c revision 1.87
      1 /*	$NetBSD: vm.c,v 1.87 2010/07/29 15:13:00 hannken 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.87 2010/07/29 15:13:00 hannken 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->flags & PG_MARKER) != 0)
    463 			continue;
    464 		if (pg->offset == off) {
    465 			return pg;
    466 		}
    467 	}
    468 
    469 	return NULL;
    470 }
    471 
    472 void
    473 uvm_page_unbusy(struct vm_page **pgs, int npgs)
    474 {
    475 	struct vm_page *pg;
    476 	int i;
    477 
    478 	for (i = 0; i < npgs; i++) {
    479 		pg = pgs[i];
    480 		if (pg == NULL)
    481 			continue;
    482 
    483 		KASSERT(pg->flags & PG_BUSY);
    484 		if (pg->flags & PG_WANTED)
    485 			wakeup(pg);
    486 		if (pg->flags & PG_RELEASED)
    487 			uvm_pagefree(pg);
    488 		else
    489 			pg->flags &= ~(PG_WANTED|PG_BUSY);
    490 	}
    491 }
    492 
    493 void
    494 uvm_estimatepageable(int *active, int *inactive)
    495 {
    496 
    497 	/* XXX: guessing game */
    498 	*active = 1024;
    499 	*inactive = 1024;
    500 }
    501 
    502 struct vm_map_kernel *
    503 vm_map_to_kernel(struct vm_map *map)
    504 {
    505 
    506 	return (struct vm_map_kernel *)map;
    507 }
    508 
    509 bool
    510 vm_map_starved_p(struct vm_map *map)
    511 {
    512 
    513 	if (map->flags & VM_MAP_WANTVA)
    514 		return true;
    515 
    516 	return false;
    517 }
    518 
    519 int
    520 uvm_loan(struct vm_map *map, vaddr_t start, vsize_t len, void *v, int flags)
    521 {
    522 
    523 	panic("%s: unimplemented", __func__);
    524 }
    525 
    526 void
    527 uvm_unloan(void *v, int npages, int flags)
    528 {
    529 
    530 	panic("%s: unimplemented", __func__);
    531 }
    532 
    533 int
    534 uvm_loanuobjpages(struct uvm_object *uobj, voff_t pgoff, int orignpages,
    535 	struct vm_page **opp)
    536 {
    537 
    538 	return EBUSY;
    539 }
    540 
    541 #ifdef DEBUGPRINT
    542 void
    543 uvm_object_printit(struct uvm_object *uobj, bool full,
    544 	void (*pr)(const char *, ...))
    545 {
    546 
    547 	pr("VM OBJECT at %p, refs %d", uobj, uobj->uo_refs);
    548 }
    549 #endif
    550 
    551 vaddr_t
    552 uvm_default_mapaddr(struct proc *p, vaddr_t base, vsize_t sz)
    553 {
    554 
    555 	return 0;
    556 }
    557 
    558 int
    559 uvm_map_protect(struct vm_map *map, vaddr_t start, vaddr_t end,
    560 	vm_prot_t prot, bool set_max)
    561 {
    562 
    563 	return EOPNOTSUPP;
    564 }
    565 
    566 /*
    567  * UVM km
    568  */
    569 
    570 vaddr_t
    571 uvm_km_alloc(struct vm_map *map, vsize_t size, vsize_t align, uvm_flag_t flags)
    572 {
    573 	void *rv, *desired = NULL;
    574 	int alignbit, error;
    575 
    576 #ifdef __x86_64__
    577 	/*
    578 	 * On amd64, allocate all module memory from the lowest 2GB.
    579 	 * This is because NetBSD kernel modules are compiled
    580 	 * with -mcmodel=kernel and reserve only 4 bytes for
    581 	 * offsets.  If we load code compiled with -mcmodel=kernel
    582 	 * anywhere except the lowest or highest 2GB, it will not
    583 	 * work.  Since userspace does not have access to the highest
    584 	 * 2GB, use the lowest 2GB.
    585 	 *
    586 	 * Note: this assumes the rump kernel resides in
    587 	 * the lowest 2GB as well.
    588 	 *
    589 	 * Note2: yes, it's a quick hack, but since this the only
    590 	 * place where we care about the map we're allocating from,
    591 	 * just use a simple "if" instead of coming up with a fancy
    592 	 * generic solution.
    593 	 */
    594 	extern struct vm_map *module_map;
    595 	if (map == module_map) {
    596 		desired = (void *)(0x80000000 - size);
    597 	}
    598 #endif
    599 
    600 	alignbit = 0;
    601 	if (align) {
    602 		alignbit = ffs(align)-1;
    603 	}
    604 
    605 	rv = rumpuser_anonmmap(desired, size, alignbit, flags & UVM_KMF_EXEC,
    606 	    &error);
    607 	if (rv == NULL) {
    608 		if (flags & (UVM_KMF_CANFAIL | UVM_KMF_NOWAIT))
    609 			return 0;
    610 		else
    611 			panic("uvm_km_alloc failed");
    612 	}
    613 
    614 	if (flags & UVM_KMF_ZERO)
    615 		memset(rv, 0, size);
    616 
    617 	return (vaddr_t)rv;
    618 }
    619 
    620 void
    621 uvm_km_free(struct vm_map *map, vaddr_t vaddr, vsize_t size, uvm_flag_t flags)
    622 {
    623 
    624 	rumpuser_unmap((void *)vaddr, size);
    625 }
    626 
    627 struct vm_map *
    628 uvm_km_suballoc(struct vm_map *map, vaddr_t *minaddr, vaddr_t *maxaddr,
    629 	vsize_t size, int pageable, bool fixed, struct vm_map_kernel *submap)
    630 {
    631 
    632 	return (struct vm_map *)417416;
    633 }
    634 
    635 vaddr_t
    636 uvm_km_alloc_poolpage(struct vm_map *map, bool waitok)
    637 {
    638 
    639 	return (vaddr_t)rump_hypermalloc(PAGE_SIZE, PAGE_SIZE,
    640 	    waitok, "kmalloc");
    641 }
    642 
    643 void
    644 uvm_km_free_poolpage(struct vm_map *map, vaddr_t addr)
    645 {
    646 
    647 	rump_hyperfree((void *)addr, PAGE_SIZE);
    648 }
    649 
    650 vaddr_t
    651 uvm_km_alloc_poolpage_cache(struct vm_map *map, bool waitok)
    652 {
    653 
    654 	return uvm_km_alloc_poolpage(map, waitok);
    655 }
    656 
    657 void
    658 uvm_km_free_poolpage_cache(struct vm_map *map, vaddr_t vaddr)
    659 {
    660 
    661 	uvm_km_free_poolpage(map, vaddr);
    662 }
    663 
    664 void
    665 uvm_km_va_drain(struct vm_map *map, uvm_flag_t flags)
    666 {
    667 
    668 	/* we eventually maybe want some model for available memory */
    669 }
    670 
    671 /*
    672  * Mapping and vm space locking routines.
    673  * XXX: these don't work for non-local vmspaces
    674  */
    675 int
    676 uvm_vslock(struct vmspace *vs, void *addr, size_t len, vm_prot_t access)
    677 {
    678 
    679 	KASSERT(vs == &vmspace0);
    680 	return 0;
    681 }
    682 
    683 void
    684 uvm_vsunlock(struct vmspace *vs, void *addr, size_t len)
    685 {
    686 
    687 	KASSERT(vs == &vmspace0);
    688 }
    689 
    690 void
    691 vmapbuf(struct buf *bp, vsize_t len)
    692 {
    693 
    694 	bp->b_saveaddr = bp->b_data;
    695 }
    696 
    697 void
    698 vunmapbuf(struct buf *bp, vsize_t len)
    699 {
    700 
    701 	bp->b_data = bp->b_saveaddr;
    702 	bp->b_saveaddr = 0;
    703 }
    704 
    705 void
    706 uvmspace_addref(struct vmspace *vm)
    707 {
    708 
    709 	/*
    710 	 * there is only vmspace0.  we're not planning on
    711 	 * feeding it to the fishes.
    712 	 */
    713 }
    714 
    715 void
    716 uvmspace_free(struct vmspace *vm)
    717 {
    718 
    719 	/* nothing for now */
    720 }
    721 
    722 int
    723 uvm_io(struct vm_map *map, struct uio *uio)
    724 {
    725 
    726 	/*
    727 	 * just do direct uio for now.  but this needs some vmspace
    728 	 * olympics for rump_sysproxy.
    729 	 */
    730 	return uiomove((void *)(vaddr_t)uio->uio_offset, uio->uio_resid, uio);
    731 }
    732 
    733 /*
    734  * page life cycle stuff.  it really doesn't exist, so just stubs.
    735  */
    736 
    737 void
    738 uvm_pageactivate(struct vm_page *pg)
    739 {
    740 
    741 	/* nada */
    742 }
    743 
    744 void
    745 uvm_pagedeactivate(struct vm_page *pg)
    746 {
    747 
    748 	/* nada */
    749 }
    750 
    751 void
    752 uvm_pagedequeue(struct vm_page *pg)
    753 {
    754 
    755 	/* nada*/
    756 }
    757 
    758 void
    759 uvm_pageenqueue(struct vm_page *pg)
    760 {
    761 
    762 	/* nada */
    763 }
    764 
    765 /*
    766  * Routines related to the Page Baroness.
    767  */
    768 
    769 void
    770 uvm_wait(const char *msg)
    771 {
    772 
    773 	if (__predict_false(curlwp == uvm.pagedaemon_lwp))
    774 		panic("pagedaemon out of memory");
    775 	if (__predict_false(rump_threads == 0))
    776 		panic("pagedaemon missing (RUMP_THREADS = 0)");
    777 
    778 	mutex_enter(&pdaemonmtx);
    779 	pdaemon_waiters++;
    780 	cv_signal(&pdaemoncv);
    781 	cv_wait(&oomwait, &pdaemonmtx);
    782 	mutex_exit(&pdaemonmtx);
    783 }
    784 
    785 void
    786 uvm_pageout_start(int npages)
    787 {
    788 
    789 	/* we don't have the heuristics */
    790 }
    791 
    792 void
    793 uvm_pageout_done(int npages)
    794 {
    795 
    796 	/* could wakeup waiters, but just let the pagedaemon do it */
    797 }
    798 
    799 /*
    800  * Under-construction page mistress.  This is lacking vfs support, namely:
    801  *
    802  *  1) draining vfs buffers
    803  *  2) paging out pages in vm vnode objects
    804  *     (we will not page out anon memory on the basis that
    805  *     that's the task of the host)
    806  */
    807 
    808 void
    809 uvm_pageout(void *arg)
    810 {
    811 	struct pool *pp, *pp_first;
    812 	uint64_t where;
    813 	int timo = 0;
    814 	bool succ;
    815 
    816 	mutex_enter(&pdaemonmtx);
    817 	for (;;) {
    818 		cv_timedwait(&pdaemoncv, &pdaemonmtx, timo);
    819 		uvmexp.pdwoke++;
    820 		kernel_map->flags |= VM_MAP_WANTVA;
    821 		mutex_exit(&pdaemonmtx);
    822 
    823 		succ = false;
    824 		pool_drain_start(&pp_first, &where);
    825 		pp = pp_first;
    826 		for (;;) {
    827 			succ = pool_drain_end(pp, where);
    828 			if (succ)
    829 				break;
    830 			pool_drain_start(&pp, &where);
    831 			if (pp == pp_first) {
    832 				succ = pool_drain_end(pp, where);
    833 				break;
    834 			}
    835 		}
    836 		mutex_enter(&pdaemonmtx);
    837 
    838 		if (!succ) {
    839 			rumpuser_dprintf("pagedaemoness: failed to reclaim "
    840 			    "memory ... sleeping (deadlock?)\n");
    841 			timo = hz;
    842 			continue;
    843 		}
    844 		kernel_map->flags &= ~VM_MAP_WANTVA;
    845 		timo = 0;
    846 
    847 		if (pdaemon_waiters) {
    848 			pdaemon_waiters = 0;
    849 			cv_broadcast(&oomwait);
    850 		}
    851 	}
    852 
    853 	panic("you can swap out any time you like, but you can never leave");
    854 }
    855 
    856 /*
    857  * In a regular kernel the pagedaemon is activated when memory becomes
    858  * low.  In a virtual rump kernel we do not know exactly how much memory
    859  * we have available -- it depends on the conditions on the host.
    860  * Therefore, we cannot preemptively kick the pagedaemon.  Rather, we
    861  * wait until things we desperate and we're forced to uvm_wait().
    862  *
    863  * The alternative would be to allocate a huge chunk of memory at
    864  * startup, but that solution has a number of problems including
    865  * being a resource hog, failing anyway due to host memory overcommit
    866  * and core dump size.
    867  */
    868 
    869 void
    870 uvm_kick_pdaemon()
    871 {
    872 
    873 	/* nada */
    874 }
    875 
    876 void *
    877 rump_hypermalloc(size_t howmuch, int alignment, bool waitok, const char *wmsg)
    878 {
    879 	unsigned long newmem;
    880 	void *rv;
    881 
    882 	/* first we must be within the limit */
    883  limitagain:
    884 	if (physmemlimit != RUMPMEM_UNLIMITED) {
    885 		newmem = atomic_add_long_nv(&curphysmem, howmuch);
    886 		if (newmem > physmemlimit) {
    887 			newmem = atomic_add_long_nv(&curphysmem, -howmuch);
    888 			if (!waitok)
    889 				return NULL;
    890 			uvm_wait(wmsg);
    891 			goto limitagain;
    892 		}
    893 	}
    894 
    895 	/* second, we must get something from the backend */
    896  again:
    897 	rv = rumpuser_malloc(howmuch, alignment);
    898 	if (__predict_false(rv == NULL && waitok)) {
    899 		uvm_wait(wmsg);
    900 		goto again;
    901 	}
    902 
    903 	return rv;
    904 }
    905 
    906 void
    907 rump_hyperfree(void *what, size_t size)
    908 {
    909 
    910 	if (physmemlimit != RUMPMEM_UNLIMITED) {
    911 		atomic_add_long(&curphysmem, -size);
    912 	}
    913 	rumpuser_free(what);
    914 }
    915