Home | History | Annotate | Line # | Download | only in rumpkern
vm.c revision 1.93
      1 /*	$NetBSD: vm.c,v 1.93 2010/09/08 21:14:32 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.
     34  */
     35 
     36 /*
     37  * XXX: we abuse pg->uanon for the virtual address of the storage
     38  * for each page.  phys_addr would fit the job description better,
     39  * except that it will create unnecessary lossage on some platforms
     40  * due to not being a pointer type.
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: vm.c,v 1.93 2010/09/08 21:14:32 pooka Exp $");
     45 
     46 #include <sys/param.h>
     47 #include <sys/atomic.h>
     48 #include <sys/buf.h>
     49 #include <sys/kernel.h>
     50 #include <sys/kmem.h>
     51 #include <sys/mman.h>
     52 #include <sys/null.h>
     53 #include <sys/vnode.h>
     54 
     55 #include <machine/pmap.h>
     56 
     57 #include <rump/rumpuser.h>
     58 
     59 #include <uvm/uvm.h>
     60 #include <uvm/uvm_ddb.h>
     61 #include <uvm/uvm_pdpolicy.h>
     62 #include <uvm/uvm_prot.h>
     63 #include <uvm/uvm_readahead.h>
     64 
     65 #include "rump_private.h"
     66 #include "rump_vfs_private.h"
     67 
     68 kmutex_t uvm_pageqlock;
     69 kmutex_t uvm_swap_data_lock;
     70 
     71 struct uvmexp uvmexp;
     72 struct uvm uvm;
     73 
     74 struct vm_map rump_vmmap;
     75 static struct vm_map_kernel kmem_map_store;
     76 struct vm_map *kmem_map = &kmem_map_store.vmk_map;
     77 
     78 static struct vm_map_kernel kernel_map_store;
     79 struct vm_map *kernel_map = &kernel_map_store.vmk_map;
     80 
     81 static unsigned int pdaemon_waiters;
     82 static kmutex_t pdaemonmtx;
     83 static kcondvar_t pdaemoncv, oomwait;
     84 
     85 unsigned long rump_physmemlimit = RUMPMEM_UNLIMITED;
     86 static unsigned long curphysmem;
     87 static unsigned long dddlim;		/* 90% of memory limit used */
     88 #define NEED_PAGEDAEMON() \
     89     (rump_physmemlimit != RUMPMEM_UNLIMITED && curphysmem > dddlim)
     90 
     91 /*
     92  * Try to free two pages worth of pages from objects.
     93  * If this succesfully frees a full page cache page, we'll
     94  * free the released page plus PAGE_SIZE/sizeof(vm_page).
     95  */
     96 #define PAGEDAEMON_OBJCHUNK (2*PAGE_SIZE / sizeof(struct vm_page))
     97 
     98 /*
     99  * Keep a list of least recently used pages.  Since the only way a
    100  * rump kernel can "access" a page is via lookup, we put the page
    101  * at the back of queue every time a lookup for it is done.  If the
    102  * page is in front of this global queue and we're short of memory,
    103  * it's a candidate for pageout.
    104  */
    105 static struct pglist vmpage_lruqueue;
    106 static unsigned vmpage_onqueue;
    107 
    108 static int
    109 pg_compare_key(const struct rb_node *n, const void *key)
    110 {
    111 	voff_t a = ((const struct vm_page *)n)->offset;
    112 	voff_t b = *(const voff_t *)key;
    113 
    114 	if (a < b)
    115 		return 1;
    116 	else if (a > b)
    117 		return -1;
    118 	else
    119 		return 0;
    120 }
    121 
    122 static int
    123 pg_compare_nodes(const struct rb_node *n1, const struct rb_node *n2)
    124 {
    125 
    126 	return pg_compare_key(n1, &((const struct vm_page *)n2)->offset);
    127 }
    128 
    129 const struct rb_tree_ops uvm_page_tree_ops = {
    130 	.rbto_compare_nodes = pg_compare_nodes,
    131 	.rbto_compare_key = pg_compare_key,
    132 };
    133 
    134 /*
    135  * vm pages
    136  */
    137 
    138 static int
    139 pgctor(void *arg, void *obj, int flags)
    140 {
    141 	struct vm_page *pg = obj;
    142 
    143 	memset(pg, 0, sizeof(*pg));
    144 	pg->uanon = rump_hypermalloc(PAGE_SIZE, PAGE_SIZE, true, "pgalloc");
    145 	return 0;
    146 }
    147 
    148 static void
    149 pgdtor(void *arg, void *obj)
    150 {
    151 	struct vm_page *pg = obj;
    152 
    153 	rump_hyperfree(pg->uanon, PAGE_SIZE);
    154 }
    155 
    156 static struct pool_cache pagecache;
    157 
    158 /*
    159  * Called with the object locked.  We don't support anons.
    160  */
    161 struct vm_page *
    162 uvm_pagealloc_strat(struct uvm_object *uobj, voff_t off, struct vm_anon *anon,
    163 	int flags, int strat, int free_list)
    164 {
    165 	struct vm_page *pg;
    166 
    167 	KASSERT(uobj && mutex_owned(&uobj->vmobjlock));
    168 	KASSERT(anon == NULL);
    169 
    170 	pg = pool_cache_get(&pagecache, PR_WAITOK);
    171 	pg->offset = off;
    172 	pg->uobject = uobj;
    173 
    174 	pg->flags = PG_CLEAN|PG_BUSY|PG_FAKE;
    175 	if (flags & UVM_PGA_ZERO) {
    176 		uvm_pagezero(pg);
    177 	}
    178 
    179 	TAILQ_INSERT_TAIL(&uobj->memq, pg, listq.queue);
    180 	rb_tree_insert_node(&uobj->rb_tree, &pg->rb_node);
    181 
    182 	/*
    183 	 * Don't put anons on the LRU page queue.  We can't flush them
    184 	 * (there's no concept of swap in a rump kernel), so no reason
    185 	 * to bother with them.
    186 	 */
    187 	if (!UVM_OBJ_IS_AOBJ(uobj)) {
    188 		atomic_inc_uint(&vmpage_onqueue);
    189 		mutex_enter(&uvm_pageqlock);
    190 		TAILQ_INSERT_TAIL(&vmpage_lruqueue, pg, pageq.queue);
    191 		mutex_exit(&uvm_pageqlock);
    192 	}
    193 
    194 	uobj->uo_npages++;
    195 
    196 	return pg;
    197 }
    198 
    199 /*
    200  * Release a page.
    201  *
    202  * Called with the vm object locked.
    203  */
    204 void
    205 uvm_pagefree(struct vm_page *pg)
    206 {
    207 	struct uvm_object *uobj = pg->uobject;
    208 
    209 	KASSERT(mutex_owned(&uvm_pageqlock));
    210 
    211 	if (pg->flags & PG_WANTED)
    212 		wakeup(pg);
    213 
    214 	TAILQ_REMOVE(&uobj->memq, pg, listq.queue);
    215 
    216 	uobj->uo_npages--;
    217 	rb_tree_remove_node(&uobj->rb_tree, &pg->rb_node);
    218 
    219 	if (!UVM_OBJ_IS_AOBJ(uobj)) {
    220 		TAILQ_REMOVE(&vmpage_lruqueue, pg, pageq.queue);
    221 		atomic_dec_uint(&vmpage_onqueue);
    222 	}
    223 
    224 	pool_cache_put(&pagecache, pg);
    225 }
    226 
    227 void
    228 uvm_pagezero(struct vm_page *pg)
    229 {
    230 
    231 	pg->flags &= ~PG_CLEAN;
    232 	memset((void *)pg->uanon, 0, PAGE_SIZE);
    233 }
    234 
    235 /*
    236  * Misc routines
    237  */
    238 
    239 static kmutex_t pagermtx;
    240 
    241 void
    242 uvm_init(void)
    243 {
    244 	char buf[64];
    245 	int error;
    246 
    247 	if (rumpuser_getenv("RUMP_MEMLIMIT", buf, sizeof(buf), &error) == 0) {
    248 		rump_physmemlimit = strtoll(buf, NULL, 10);
    249 		/* it's not like we'd get far with, say, 1 byte, but ... */
    250 		if (rump_physmemlimit == 0)
    251 			panic("uvm_init: no memory available");
    252 #define HUMANIZE_BYTES 9
    253 		CTASSERT(sizeof(buf) >= HUMANIZE_BYTES);
    254 		format_bytes(buf, HUMANIZE_BYTES, rump_physmemlimit);
    255 #undef HUMANIZE_BYTES
    256 		dddlim = 9 * (rump_physmemlimit / 10);
    257 	} else {
    258 		strlcpy(buf, "unlimited (host limit)", sizeof(buf));
    259 	}
    260 	aprint_verbose("total memory = %s\n", buf);
    261 
    262 	TAILQ_INIT(&vmpage_lruqueue);
    263 
    264 	uvmexp.free = 1024*1024; /* XXX: arbitrary & not updated */
    265 
    266 	mutex_init(&pagermtx, MUTEX_DEFAULT, 0);
    267 	mutex_init(&uvm_pageqlock, MUTEX_DEFAULT, 0);
    268 	mutex_init(&uvm_swap_data_lock, MUTEX_DEFAULT, 0);
    269 
    270 	mutex_init(&pdaemonmtx, MUTEX_DEFAULT, 0);
    271 	cv_init(&pdaemoncv, "pdaemon");
    272 	cv_init(&oomwait, "oomwait");
    273 
    274 	kernel_map->pmap = pmap_kernel();
    275 	callback_head_init(&kernel_map_store.vmk_reclaim_callback, IPL_VM);
    276 	kmem_map->pmap = pmap_kernel();
    277 	callback_head_init(&kmem_map_store.vmk_reclaim_callback, IPL_VM);
    278 
    279 	pool_cache_bootstrap(&pagecache, sizeof(struct vm_page), 0, 0, 0,
    280 	    "page$", NULL, IPL_NONE, pgctor, pgdtor, NULL);
    281 }
    282 
    283 void
    284 uvmspace_init(struct vmspace *vm, struct pmap *pmap, vaddr_t vmin, vaddr_t vmax)
    285 {
    286 
    287 	vm->vm_map.pmap = pmap_kernel();
    288 	vm->vm_refcnt = 1;
    289 }
    290 
    291 void
    292 uvm_pagewire(struct vm_page *pg)
    293 {
    294 
    295 	/* nada */
    296 }
    297 
    298 void
    299 uvm_pageunwire(struct vm_page *pg)
    300 {
    301 
    302 	/* nada */
    303 }
    304 
    305 /* where's your schmonz now? */
    306 #define PUNLIMIT(a)	\
    307 p->p_rlimit[a].rlim_cur = p->p_rlimit[a].rlim_max = RLIM_INFINITY;
    308 void
    309 uvm_init_limits(struct proc *p)
    310 {
    311 
    312 	PUNLIMIT(RLIMIT_STACK);
    313 	PUNLIMIT(RLIMIT_DATA);
    314 	PUNLIMIT(RLIMIT_RSS);
    315 	PUNLIMIT(RLIMIT_AS);
    316 	/* nice, cascade */
    317 }
    318 #undef PUNLIMIT
    319 
    320 /*
    321  * This satisfies the "disgusting mmap hack" used by proplib.
    322  * We probably should grow some more assertables to make sure we're
    323  * not satisfying anything we shouldn't be satisfying.  At least we
    324  * should make sure it's the local machine we're mmapping ...
    325  */
    326 int
    327 uvm_mmap(struct vm_map *map, vaddr_t *addr, vsize_t size, vm_prot_t prot,
    328 	vm_prot_t maxprot, int flags, void *handle, voff_t off, vsize_t locklim)
    329 {
    330 	void *uaddr;
    331 	int error;
    332 
    333 	if (prot != (VM_PROT_READ | VM_PROT_WRITE))
    334 		panic("uvm_mmap() variant unsupported");
    335 	if (flags != (MAP_PRIVATE | MAP_ANON))
    336 		panic("uvm_mmap() variant unsupported");
    337 	/* no reason in particular, but cf. uvm_default_mapaddr() */
    338 	if (*addr != 0)
    339 		panic("uvm_mmap() variant unsupported");
    340 
    341 	uaddr = rumpuser_anonmmap(NULL, size, 0, 0, &error);
    342 	if (uaddr == NULL)
    343 		return error;
    344 
    345 	*addr = (vaddr_t)uaddr;
    346 	return 0;
    347 }
    348 
    349 struct pagerinfo {
    350 	vaddr_t pgr_kva;
    351 	int pgr_npages;
    352 	struct vm_page **pgr_pgs;
    353 	bool pgr_read;
    354 
    355 	LIST_ENTRY(pagerinfo) pgr_entries;
    356 };
    357 static LIST_HEAD(, pagerinfo) pagerlist = LIST_HEAD_INITIALIZER(pagerlist);
    358 
    359 /*
    360  * Pager "map" in routine.  Instead of mapping, we allocate memory
    361  * and copy page contents there.  Not optimal or even strictly
    362  * correct (the caller might modify the page contents after mapping
    363  * them in), but what the heck.  Assumes UVMPAGER_MAPIN_WAITOK.
    364  */
    365 vaddr_t
    366 uvm_pagermapin(struct vm_page **pgs, int npages, int flags)
    367 {
    368 	struct pagerinfo *pgri;
    369 	vaddr_t curkva;
    370 	int i;
    371 
    372 	/* allocate structures */
    373 	pgri = kmem_alloc(sizeof(*pgri), KM_SLEEP);
    374 	pgri->pgr_kva = (vaddr_t)kmem_alloc(npages * PAGE_SIZE, KM_SLEEP);
    375 	pgri->pgr_npages = npages;
    376 	pgri->pgr_pgs = kmem_alloc(sizeof(struct vm_page *) * npages, KM_SLEEP);
    377 	pgri->pgr_read = (flags & UVMPAGER_MAPIN_READ) != 0;
    378 
    379 	/* copy contents to "mapped" memory */
    380 	for (i = 0, curkva = pgri->pgr_kva;
    381 	    i < npages;
    382 	    i++, curkva += PAGE_SIZE) {
    383 		/*
    384 		 * We need to copy the previous contents of the pages to
    385 		 * the window even if we are reading from the
    386 		 * device, since the device might not fill the contents of
    387 		 * the full mapped range and we will end up corrupting
    388 		 * data when we unmap the window.
    389 		 */
    390 		memcpy((void*)curkva, pgs[i]->uanon, PAGE_SIZE);
    391 		pgri->pgr_pgs[i] = pgs[i];
    392 	}
    393 
    394 	mutex_enter(&pagermtx);
    395 	LIST_INSERT_HEAD(&pagerlist, pgri, pgr_entries);
    396 	mutex_exit(&pagermtx);
    397 
    398 	return pgri->pgr_kva;
    399 }
    400 
    401 /*
    402  * map out the pager window.  return contents from VA to page storage
    403  * and free structures.
    404  *
    405  * Note: does not currently support partial frees
    406  */
    407 void
    408 uvm_pagermapout(vaddr_t kva, int npages)
    409 {
    410 	struct pagerinfo *pgri;
    411 	vaddr_t curkva;
    412 	int i;
    413 
    414 	mutex_enter(&pagermtx);
    415 	LIST_FOREACH(pgri, &pagerlist, pgr_entries) {
    416 		if (pgri->pgr_kva == kva)
    417 			break;
    418 	}
    419 	KASSERT(pgri);
    420 	if (pgri->pgr_npages != npages)
    421 		panic("uvm_pagermapout: partial unmapping not supported");
    422 	LIST_REMOVE(pgri, pgr_entries);
    423 	mutex_exit(&pagermtx);
    424 
    425 	if (pgri->pgr_read) {
    426 		for (i = 0, curkva = pgri->pgr_kva;
    427 		    i < pgri->pgr_npages;
    428 		    i++, curkva += PAGE_SIZE) {
    429 			memcpy(pgri->pgr_pgs[i]->uanon,(void*)curkva,PAGE_SIZE);
    430 		}
    431 	}
    432 
    433 	kmem_free(pgri->pgr_pgs, npages * sizeof(struct vm_page *));
    434 	kmem_free((void*)pgri->pgr_kva, npages * PAGE_SIZE);
    435 	kmem_free(pgri, sizeof(*pgri));
    436 }
    437 
    438 /*
    439  * convert va in pager window to page structure.
    440  * XXX: how expensive is this (global lock, list traversal)?
    441  */
    442 struct vm_page *
    443 uvm_pageratop(vaddr_t va)
    444 {
    445 	struct pagerinfo *pgri;
    446 	struct vm_page *pg = NULL;
    447 	int i;
    448 
    449 	mutex_enter(&pagermtx);
    450 	LIST_FOREACH(pgri, &pagerlist, pgr_entries) {
    451 		if (pgri->pgr_kva <= va
    452 		    && va < pgri->pgr_kva + pgri->pgr_npages*PAGE_SIZE)
    453 			break;
    454 	}
    455 	if (pgri) {
    456 		i = (va - pgri->pgr_kva) >> PAGE_SHIFT;
    457 		pg = pgri->pgr_pgs[i];
    458 	}
    459 	mutex_exit(&pagermtx);
    460 
    461 	return pg;
    462 }
    463 
    464 /* Called with the vm object locked */
    465 struct vm_page *
    466 uvm_pagelookup(struct uvm_object *uobj, voff_t off)
    467 {
    468 	struct vm_page *pg;
    469 
    470 	pg = (struct vm_page *)rb_tree_find_node(&uobj->rb_tree, &off);
    471 	if (pg && !UVM_OBJ_IS_AOBJ(pg->uobject)) {
    472 		mutex_enter(&uvm_pageqlock);
    473 		TAILQ_REMOVE(&vmpage_lruqueue, pg, pageq.queue);
    474 		TAILQ_INSERT_TAIL(&vmpage_lruqueue, pg, pageq.queue);
    475 		mutex_exit(&uvm_pageqlock);
    476 	}
    477 
    478 	return pg;
    479 }
    480 
    481 void
    482 uvm_page_unbusy(struct vm_page **pgs, int npgs)
    483 {
    484 	struct vm_page *pg;
    485 	int i;
    486 
    487 	for (i = 0; i < npgs; i++) {
    488 		pg = pgs[i];
    489 		if (pg == NULL)
    490 			continue;
    491 
    492 		KASSERT(pg->flags & PG_BUSY);
    493 		if (pg->flags & PG_WANTED)
    494 			wakeup(pg);
    495 		if (pg->flags & PG_RELEASED)
    496 			uvm_pagefree(pg);
    497 		else
    498 			pg->flags &= ~(PG_WANTED|PG_BUSY);
    499 	}
    500 }
    501 
    502 void
    503 uvm_estimatepageable(int *active, int *inactive)
    504 {
    505 
    506 	/* XXX: guessing game */
    507 	*active = 1024;
    508 	*inactive = 1024;
    509 }
    510 
    511 struct vm_map_kernel *
    512 vm_map_to_kernel(struct vm_map *map)
    513 {
    514 
    515 	return (struct vm_map_kernel *)map;
    516 }
    517 
    518 bool
    519 vm_map_starved_p(struct vm_map *map)
    520 {
    521 
    522 	if (map->flags & VM_MAP_WANTVA)
    523 		return true;
    524 
    525 	return false;
    526 }
    527 
    528 int
    529 uvm_loan(struct vm_map *map, vaddr_t start, vsize_t len, void *v, int flags)
    530 {
    531 
    532 	panic("%s: unimplemented", __func__);
    533 }
    534 
    535 void
    536 uvm_unloan(void *v, int npages, int flags)
    537 {
    538 
    539 	panic("%s: unimplemented", __func__);
    540 }
    541 
    542 int
    543 uvm_loanuobjpages(struct uvm_object *uobj, voff_t pgoff, int orignpages,
    544 	struct vm_page **opp)
    545 {
    546 
    547 	return EBUSY;
    548 }
    549 
    550 #ifdef DEBUGPRINT
    551 void
    552 uvm_object_printit(struct uvm_object *uobj, bool full,
    553 	void (*pr)(const char *, ...))
    554 {
    555 
    556 	pr("VM OBJECT at %p, refs %d", uobj, uobj->uo_refs);
    557 }
    558 #endif
    559 
    560 vaddr_t
    561 uvm_default_mapaddr(struct proc *p, vaddr_t base, vsize_t sz)
    562 {
    563 
    564 	return 0;
    565 }
    566 
    567 int
    568 uvm_map_protect(struct vm_map *map, vaddr_t start, vaddr_t end,
    569 	vm_prot_t prot, bool set_max)
    570 {
    571 
    572 	return EOPNOTSUPP;
    573 }
    574 
    575 /*
    576  * UVM km
    577  */
    578 
    579 vaddr_t
    580 uvm_km_alloc(struct vm_map *map, vsize_t size, vsize_t align, uvm_flag_t flags)
    581 {
    582 	void *rv, *desired = NULL;
    583 	int alignbit, error;
    584 
    585 #ifdef __x86_64__
    586 	/*
    587 	 * On amd64, allocate all module memory from the lowest 2GB.
    588 	 * This is because NetBSD kernel modules are compiled
    589 	 * with -mcmodel=kernel and reserve only 4 bytes for
    590 	 * offsets.  If we load code compiled with -mcmodel=kernel
    591 	 * anywhere except the lowest or highest 2GB, it will not
    592 	 * work.  Since userspace does not have access to the highest
    593 	 * 2GB, use the lowest 2GB.
    594 	 *
    595 	 * Note: this assumes the rump kernel resides in
    596 	 * the lowest 2GB as well.
    597 	 *
    598 	 * Note2: yes, it's a quick hack, but since this the only
    599 	 * place where we care about the map we're allocating from,
    600 	 * just use a simple "if" instead of coming up with a fancy
    601 	 * generic solution.
    602 	 */
    603 	extern struct vm_map *module_map;
    604 	if (map == module_map) {
    605 		desired = (void *)(0x80000000 - size);
    606 	}
    607 #endif
    608 
    609 	alignbit = 0;
    610 	if (align) {
    611 		alignbit = ffs(align)-1;
    612 	}
    613 
    614 	rv = rumpuser_anonmmap(desired, size, alignbit, flags & UVM_KMF_EXEC,
    615 	    &error);
    616 	if (rv == NULL) {
    617 		if (flags & (UVM_KMF_CANFAIL | UVM_KMF_NOWAIT))
    618 			return 0;
    619 		else
    620 			panic("uvm_km_alloc failed");
    621 	}
    622 
    623 	if (flags & UVM_KMF_ZERO)
    624 		memset(rv, 0, size);
    625 
    626 	return (vaddr_t)rv;
    627 }
    628 
    629 void
    630 uvm_km_free(struct vm_map *map, vaddr_t vaddr, vsize_t size, uvm_flag_t flags)
    631 {
    632 
    633 	rumpuser_unmap((void *)vaddr, size);
    634 }
    635 
    636 struct vm_map *
    637 uvm_km_suballoc(struct vm_map *map, vaddr_t *minaddr, vaddr_t *maxaddr,
    638 	vsize_t size, int pageable, bool fixed, struct vm_map_kernel *submap)
    639 {
    640 
    641 	return (struct vm_map *)417416;
    642 }
    643 
    644 vaddr_t
    645 uvm_km_alloc_poolpage(struct vm_map *map, bool waitok)
    646 {
    647 
    648 	return (vaddr_t)rump_hypermalloc(PAGE_SIZE, PAGE_SIZE,
    649 	    waitok, "kmalloc");
    650 }
    651 
    652 void
    653 uvm_km_free_poolpage(struct vm_map *map, vaddr_t addr)
    654 {
    655 
    656 	rump_hyperfree((void *)addr, PAGE_SIZE);
    657 }
    658 
    659 vaddr_t
    660 uvm_km_alloc_poolpage_cache(struct vm_map *map, bool waitok)
    661 {
    662 
    663 	return uvm_km_alloc_poolpage(map, waitok);
    664 }
    665 
    666 void
    667 uvm_km_free_poolpage_cache(struct vm_map *map, vaddr_t vaddr)
    668 {
    669 
    670 	uvm_km_free_poolpage(map, vaddr);
    671 }
    672 
    673 void
    674 uvm_km_va_drain(struct vm_map *map, uvm_flag_t flags)
    675 {
    676 
    677 	/* we eventually maybe want some model for available memory */
    678 }
    679 
    680 /*
    681  * Mapping and vm space locking routines.
    682  * XXX: these don't work for non-local vmspaces
    683  */
    684 int
    685 uvm_vslock(struct vmspace *vs, void *addr, size_t len, vm_prot_t access)
    686 {
    687 
    688 	KASSERT(vs == &vmspace0);
    689 	return 0;
    690 }
    691 
    692 void
    693 uvm_vsunlock(struct vmspace *vs, void *addr, size_t len)
    694 {
    695 
    696 	KASSERT(vs == &vmspace0);
    697 }
    698 
    699 void
    700 vmapbuf(struct buf *bp, vsize_t len)
    701 {
    702 
    703 	bp->b_saveaddr = bp->b_data;
    704 }
    705 
    706 void
    707 vunmapbuf(struct buf *bp, vsize_t len)
    708 {
    709 
    710 	bp->b_data = bp->b_saveaddr;
    711 	bp->b_saveaddr = 0;
    712 }
    713 
    714 void
    715 uvmspace_addref(struct vmspace *vm)
    716 {
    717 
    718 	/*
    719 	 * there is only vmspace0.  we're not planning on
    720 	 * feeding it to the fishes.
    721 	 */
    722 }
    723 
    724 void
    725 uvmspace_free(struct vmspace *vm)
    726 {
    727 
    728 	/* nothing for now */
    729 }
    730 
    731 int
    732 uvm_io(struct vm_map *map, struct uio *uio)
    733 {
    734 
    735 	/*
    736 	 * just do direct uio for now.  but this needs some vmspace
    737 	 * olympics for rump_sysproxy.
    738 	 */
    739 	return uiomove((void *)(vaddr_t)uio->uio_offset, uio->uio_resid, uio);
    740 }
    741 
    742 /*
    743  * page life cycle stuff.  it really doesn't exist, so just stubs.
    744  */
    745 
    746 void
    747 uvm_pageactivate(struct vm_page *pg)
    748 {
    749 
    750 	/* nada */
    751 }
    752 
    753 void
    754 uvm_pagedeactivate(struct vm_page *pg)
    755 {
    756 
    757 	/* nada */
    758 }
    759 
    760 void
    761 uvm_pagedequeue(struct vm_page *pg)
    762 {
    763 
    764 	/* nada*/
    765 }
    766 
    767 void
    768 uvm_pageenqueue(struct vm_page *pg)
    769 {
    770 
    771 	/* nada */
    772 }
    773 
    774 void
    775 uvmpdpol_anfree(struct vm_anon *an)
    776 {
    777 
    778 	/* nada */
    779 }
    780 
    781 /*
    782  * Routines related to the Page Baroness.
    783  */
    784 
    785 void
    786 uvm_wait(const char *msg)
    787 {
    788 
    789 	if (__predict_false(curlwp == uvm.pagedaemon_lwp))
    790 		panic("pagedaemon out of memory");
    791 	if (__predict_false(rump_threads == 0))
    792 		panic("pagedaemon missing (RUMP_THREADS = 0)");
    793 
    794 	mutex_enter(&pdaemonmtx);
    795 	pdaemon_waiters++;
    796 	cv_signal(&pdaemoncv);
    797 	cv_wait(&oomwait, &pdaemonmtx);
    798 	mutex_exit(&pdaemonmtx);
    799 }
    800 
    801 void
    802 uvm_pageout_start(int npages)
    803 {
    804 
    805 	/* we don't have the heuristics */
    806 }
    807 
    808 void
    809 uvm_pageout_done(int npages)
    810 {
    811 
    812 	/* could wakeup waiters, but just let the pagedaemon do it */
    813 }
    814 
    815 /*
    816  * The Diabolical pageDaemon Director (DDD).
    817  */
    818 void
    819 uvm_pageout(void *arg)
    820 {
    821 	struct vm_page *pg;
    822 	struct pool *pp, *pp_first;
    823 	uint64_t where;
    824 	int timo = 0;
    825 	int cleaned, skip, skipped;
    826 	bool succ = false;
    827 
    828 	mutex_enter(&pdaemonmtx);
    829 	for (;;) {
    830 		if (succ) {
    831 			kernel_map->flags &= ~VM_MAP_WANTVA;
    832 			kmem_map->flags &= VM_MAP_WANTVA;
    833 			timo = 0;
    834 		}
    835 		succ = false;
    836 
    837 		/*
    838 		 * Wake up everyone regardless of perceived success.
    839 		 * They will just resleep if we're stil out of juice.
    840 		 */
    841 		if (pdaemon_waiters) {
    842 			pdaemon_waiters = 0;
    843 			cv_broadcast(&oomwait);
    844 		}
    845 
    846 		cv_timedwait(&pdaemoncv, &pdaemonmtx, 0);
    847 		uvmexp.pdwoke++;
    848 
    849 		/* tell the world that we are hungry */
    850 		kernel_map->flags |= VM_MAP_WANTVA;
    851 		kmem_map->flags |= VM_MAP_WANTVA;
    852 
    853 		if (pdaemon_waiters == 0 && !NEED_PAGEDAEMON())
    854 			continue;
    855 		mutex_exit(&pdaemonmtx);
    856 
    857 		/*
    858 		 * step one: reclaim the page cache.  this should give
    859 		 * us the biggest earnings since whole pages are released
    860 		 * into backing memory.
    861 		 */
    862 		pool_cache_reclaim(&pagecache);
    863 		if (!NEED_PAGEDAEMON()) {
    864 			succ = true;
    865 			mutex_enter(&pdaemonmtx);
    866 			continue;
    867 		}
    868 
    869 		/*
    870 		 * Ok, so that didn't help.  Next, try to hunt memory
    871 		 * by pushing out vnode pages.  The pages might contain
    872 		 * useful cached data, but we need the memory.
    873 		 */
    874 		cleaned = 0;
    875 		skip = 0;
    876  again:
    877 		mutex_enter(&uvm_pageqlock);
    878 		while (cleaned < PAGEDAEMON_OBJCHUNK) {
    879 			skipped = 0;
    880 			TAILQ_FOREACH(pg, &vmpage_lruqueue, pageq.queue) {
    881 				struct uvm_object *uobj;
    882 
    883 				/*
    884 				 * skip over pages we _might_ have tried
    885 				 * to handle earlier.  they might not be
    886 				 * exactly the same ones, but I'm not too
    887 				 * concerned.
    888 				 */
    889 				while (skipped++ < skip)
    890 					continue;
    891 
    892 				uobj = pg->uobject;
    893 				if (mutex_tryenter(&uobj->vmobjlock)) {
    894 					if ((pg->flags & PG_BUSY) == 0) {
    895 						mutex_exit(&uvm_pageqlock);
    896 						uobj->pgops->pgo_put(uobj,
    897 						    pg->offset,
    898 						    pg->offset + PAGE_SIZE,
    899 						    PGO_CLEANIT|PGO_FREE);
    900 						cleaned++;
    901 						goto again;
    902 					}
    903 				}
    904 
    905 				skip++;
    906 			}
    907 			break;
    908 		}
    909 		mutex_exit(&uvm_pageqlock);
    910 
    911 		/*
    912 		 * And of course we need to reclaim the page cache
    913 		 * again to actually release memory.
    914 		 */
    915 		pool_cache_reclaim(&pagecache);
    916 		if (!NEED_PAGEDAEMON()) {
    917 			succ = true;
    918 			mutex_enter(&pdaemonmtx);
    919 			continue;
    920 		}
    921 
    922 		/*
    923 		 * Still not there?  sleeves come off right about now.
    924 		 * First: do reclaim on kernel/kmem map.
    925 		 */
    926 		callback_run_roundrobin(&kernel_map_store.vmk_reclaim_callback,
    927 		    NULL);
    928 		callback_run_roundrobin(&kmem_map_store.vmk_reclaim_callback,
    929 		    NULL);
    930 
    931 		/*
    932 		 * And then drain the pools.  Wipe them out ... all of them.
    933 		 */
    934 
    935 		pool_drain_start(&pp_first, &where);
    936 		pp = pp_first;
    937 		for (;;) {
    938 			rump_vfs_drainbufs(10 /* XXX: estimate better */);
    939 			succ = pool_drain_end(pp, where);
    940 			if (succ)
    941 				break;
    942 			pool_drain_start(&pp, &where);
    943 			if (pp == pp_first) {
    944 				succ = pool_drain_end(pp, where);
    945 				break;
    946 			}
    947 		}
    948 
    949 		/*
    950 		 * Need to use PYEC on our bag of tricks.
    951 		 * Unfortunately, the wife just borrowed it.
    952 		 */
    953 
    954 		if (!succ) {
    955 			rumpuser_dprintf("pagedaemoness: failed to reclaim "
    956 			    "memory ... sleeping (deadlock?)\n");
    957 			kpause("dpdd", false, hz, NULL);
    958 		}
    959 
    960 		mutex_enter(&pdaemonmtx);
    961 	}
    962 
    963 	panic("you can swap out any time you like, but you can never leave");
    964 }
    965 
    966 void
    967 uvm_kick_pdaemon()
    968 {
    969 
    970 	/*
    971 	 * Wake up the diabolical pagedaemon director if we are over
    972 	 * 90% of the memory limit.  This is a complete and utter
    973 	 * stetson-harrison decision which you are allowed to finetune.
    974 	 * Don't bother locking.  If we have some unflushed caches,
    975 	 * other waker-uppers will deal with the issue.
    976 	 */
    977 	if (NEED_PAGEDAEMON()) {
    978 		cv_signal(&pdaemoncv);
    979 	}
    980 }
    981 
    982 void *
    983 rump_hypermalloc(size_t howmuch, int alignment, bool waitok, const char *wmsg)
    984 {
    985 	unsigned long newmem;
    986 	void *rv;
    987 
    988 	uvm_kick_pdaemon(); /* ouch */
    989 
    990 	/* first we must be within the limit */
    991  limitagain:
    992 	if (rump_physmemlimit != RUMPMEM_UNLIMITED) {
    993 		newmem = atomic_add_long_nv(&curphysmem, howmuch);
    994 		if (newmem > rump_physmemlimit) {
    995 			newmem = atomic_add_long_nv(&curphysmem, -howmuch);
    996 			if (!waitok)
    997 				return NULL;
    998 			uvm_wait(wmsg);
    999 			goto limitagain;
   1000 		}
   1001 	}
   1002 
   1003 	/* second, we must get something from the backend */
   1004  again:
   1005 	rv = rumpuser_malloc(howmuch, alignment);
   1006 	if (__predict_false(rv == NULL && waitok)) {
   1007 		uvm_wait(wmsg);
   1008 		goto again;
   1009 	}
   1010 
   1011 	return rv;
   1012 }
   1013 
   1014 void
   1015 rump_hyperfree(void *what, size_t size)
   1016 {
   1017 
   1018 	if (rump_physmemlimit != RUMPMEM_UNLIMITED) {
   1019 		atomic_add_long(&curphysmem, -size);
   1020 	}
   1021 	rumpuser_free(what);
   1022 }
   1023