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