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