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