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