Home | History | Annotate | Line # | Download | only in rumpkern
vm.c revision 1.152
      1 /*	$NetBSD: vm.c,v 1.152 2014/03/11 20:32:05 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.152 2014/03/11 20:32:05 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 		/* it's not like we'd get far with, say, 1 byte, but ... */
    310 		if (rump_physmemlimit < 1024*1024)
    311 			printf("uvm_init: WARNING: <1MB RAM limit, "
    312 			    "hope you know what you're doing\n");
    313 
    314 		/* reserve some memory for the pager */
    315 		pdlimit = rump_physmemlimit;
    316 		rump_physmemlimit -= 2*MAXPHYS;
    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 	uvmexp.free = 1024*1024; /* XXX: arbitrary & not updated */
    331 
    332 #ifndef __uvmexp_pagesize
    333 	uvmexp.pagesize = PAGE_SIZE;
    334 	uvmexp.pagemask = PAGE_MASK;
    335 	uvmexp.pageshift = PAGE_SHIFT;
    336 #else
    337 #define FAKE_PAGE_SHIFT 12
    338 	uvmexp.pageshift = FAKE_PAGE_SHIFT;
    339 	uvmexp.pagesize = 1<<FAKE_PAGE_SHIFT;
    340 	uvmexp.pagemask = (1<<FAKE_PAGE_SHIFT)-1;
    341 #undef FAKE_PAGE_SHIFT
    342 #endif
    343 
    344 	mutex_init(&pagermtx, MUTEX_DEFAULT, IPL_NONE);
    345 	mutex_init(&uvm_pageqlock, MUTEX_DEFAULT, IPL_NONE);
    346 	mutex_init(&uvm_swap_data_lock, MUTEX_DEFAULT, IPL_NONE);
    347 
    348 	/* just to appease linkage */
    349 	mutex_init(&uvm_fpageqlock, MUTEX_SPIN, IPL_VM);
    350 
    351 	mutex_init(&pdaemonmtx, MUTEX_DEFAULT, IPL_NONE);
    352 	cv_init(&pdaemoncv, "pdaemon");
    353 	cv_init(&oomwait, "oomwait");
    354 
    355 	module_map = &module_map_store;
    356 
    357 	kernel_map->pmap = pmap_kernel();
    358 
    359 	pool_subsystem_init();
    360 
    361 	kmem_arena = vmem_create("kmem", 0, 1024*1024, PAGE_SIZE,
    362 	    NULL, NULL, NULL,
    363 	    0, VM_NOSLEEP | VM_BOOTSTRAP, IPL_VM);
    364 
    365 	vmem_subsystem_init(kmem_arena);
    366 
    367 	kmem_va_arena = vmem_create("kva", 0, 0, PAGE_SIZE,
    368 	    vmem_alloc, vmem_free, kmem_arena,
    369 	    8 * PAGE_SIZE, VM_NOSLEEP | VM_BOOTSTRAP, IPL_VM);
    370 
    371 	pool_cache_bootstrap(&pagecache, sizeof(struct vm_page), 0, 0, 0,
    372 	    "page$", NULL, IPL_NONE, pgctor, pgdtor, NULL);
    373 }
    374 
    375 void
    376 uvmspace_init(struct vmspace *vm, struct pmap *pmap, vaddr_t vmin, vaddr_t vmax,
    377     bool topdown)
    378 {
    379 
    380 	vm->vm_map.pmap = pmap_kernel();
    381 	vm->vm_refcnt = 1;
    382 }
    383 
    384 void
    385 uvm_pagewire(struct vm_page *pg)
    386 {
    387 
    388 	/* nada */
    389 }
    390 
    391 void
    392 uvm_pageunwire(struct vm_page *pg)
    393 {
    394 
    395 	/* nada */
    396 }
    397 
    398 /* where's your schmonz now? */
    399 #define PUNLIMIT(a)	\
    400 p->p_rlimit[a].rlim_cur = p->p_rlimit[a].rlim_max = RLIM_INFINITY;
    401 void
    402 uvm_init_limits(struct proc *p)
    403 {
    404 
    405 	PUNLIMIT(RLIMIT_STACK);
    406 	PUNLIMIT(RLIMIT_DATA);
    407 	PUNLIMIT(RLIMIT_RSS);
    408 	PUNLIMIT(RLIMIT_AS);
    409 	/* nice, cascade */
    410 }
    411 #undef PUNLIMIT
    412 
    413 /*
    414  * This satisfies the "disgusting mmap hack" used by proplib.
    415  * We probably should grow some more assertables to make sure we're
    416  * not satisfying anything we shouldn't be satisfying.
    417  */
    418 int
    419 uvm_mmap(struct vm_map *map, vaddr_t *addr, vsize_t size, vm_prot_t prot,
    420 	vm_prot_t maxprot, int flags, void *handle, voff_t off, vsize_t locklim)
    421 {
    422 	void *uaddr;
    423 	int error;
    424 
    425 	if (prot != (VM_PROT_READ | VM_PROT_WRITE))
    426 		panic("uvm_mmap() variant unsupported");
    427 	if (flags != (MAP_PRIVATE | MAP_ANON))
    428 		panic("uvm_mmap() variant unsupported");
    429 
    430 	/* no reason in particular, but cf. uvm_default_mapaddr() */
    431 	if (*addr != 0)
    432 		panic("uvm_mmap() variant unsupported");
    433 
    434 	if (RUMP_LOCALPROC_P(curproc)) {
    435 		error = rumpuser_anonmmap(NULL, size, 0, 0, &uaddr);
    436 	} else {
    437 		error = rumpuser_sp_anonmmap(curproc->p_vmspace->vm_map.pmap,
    438 		    size, &uaddr);
    439 	}
    440 	if (error)
    441 		return error;
    442 
    443 	*addr = (vaddr_t)uaddr;
    444 	return 0;
    445 }
    446 
    447 struct pagerinfo {
    448 	vaddr_t pgr_kva;
    449 	int pgr_npages;
    450 	struct vm_page **pgr_pgs;
    451 	bool pgr_read;
    452 
    453 	LIST_ENTRY(pagerinfo) pgr_entries;
    454 };
    455 static LIST_HEAD(, pagerinfo) pagerlist = LIST_HEAD_INITIALIZER(pagerlist);
    456 
    457 /*
    458  * Pager "map" in routine.  Instead of mapping, we allocate memory
    459  * and copy page contents there.  Not optimal or even strictly
    460  * correct (the caller might modify the page contents after mapping
    461  * them in), but what the heck.  Assumes UVMPAGER_MAPIN_WAITOK.
    462  */
    463 vaddr_t
    464 uvm_pagermapin(struct vm_page **pgs, int npages, int flags)
    465 {
    466 	struct pagerinfo *pgri;
    467 	vaddr_t curkva;
    468 	int i;
    469 
    470 	/* allocate structures */
    471 	pgri = kmem_alloc(sizeof(*pgri), KM_SLEEP);
    472 	pgri->pgr_kva = (vaddr_t)kmem_alloc(npages * PAGE_SIZE, KM_SLEEP);
    473 	pgri->pgr_npages = npages;
    474 	pgri->pgr_pgs = kmem_alloc(sizeof(struct vm_page *) * npages, KM_SLEEP);
    475 	pgri->pgr_read = (flags & UVMPAGER_MAPIN_READ) != 0;
    476 
    477 	/* copy contents to "mapped" memory */
    478 	for (i = 0, curkva = pgri->pgr_kva;
    479 	    i < npages;
    480 	    i++, curkva += PAGE_SIZE) {
    481 		/*
    482 		 * We need to copy the previous contents of the pages to
    483 		 * the window even if we are reading from the
    484 		 * device, since the device might not fill the contents of
    485 		 * the full mapped range and we will end up corrupting
    486 		 * data when we unmap the window.
    487 		 */
    488 		memcpy((void*)curkva, pgs[i]->uanon, PAGE_SIZE);
    489 		pgri->pgr_pgs[i] = pgs[i];
    490 	}
    491 
    492 	mutex_enter(&pagermtx);
    493 	LIST_INSERT_HEAD(&pagerlist, pgri, pgr_entries);
    494 	mutex_exit(&pagermtx);
    495 
    496 	return pgri->pgr_kva;
    497 }
    498 
    499 /*
    500  * map out the pager window.  return contents from VA to page storage
    501  * and free structures.
    502  *
    503  * Note: does not currently support partial frees
    504  */
    505 void
    506 uvm_pagermapout(vaddr_t kva, int npages)
    507 {
    508 	struct pagerinfo *pgri;
    509 	vaddr_t curkva;
    510 	int i;
    511 
    512 	mutex_enter(&pagermtx);
    513 	LIST_FOREACH(pgri, &pagerlist, pgr_entries) {
    514 		if (pgri->pgr_kva == kva)
    515 			break;
    516 	}
    517 	KASSERT(pgri);
    518 	if (pgri->pgr_npages != npages)
    519 		panic("uvm_pagermapout: partial unmapping not supported");
    520 	LIST_REMOVE(pgri, pgr_entries);
    521 	mutex_exit(&pagermtx);
    522 
    523 	if (pgri->pgr_read) {
    524 		for (i = 0, curkva = pgri->pgr_kva;
    525 		    i < pgri->pgr_npages;
    526 		    i++, curkva += PAGE_SIZE) {
    527 			memcpy(pgri->pgr_pgs[i]->uanon,(void*)curkva,PAGE_SIZE);
    528 		}
    529 	}
    530 
    531 	kmem_free(pgri->pgr_pgs, npages * sizeof(struct vm_page *));
    532 	kmem_free((void*)pgri->pgr_kva, npages * PAGE_SIZE);
    533 	kmem_free(pgri, sizeof(*pgri));
    534 }
    535 
    536 /*
    537  * convert va in pager window to page structure.
    538  * XXX: how expensive is this (global lock, list traversal)?
    539  */
    540 struct vm_page *
    541 uvm_pageratop(vaddr_t va)
    542 {
    543 	struct pagerinfo *pgri;
    544 	struct vm_page *pg = NULL;
    545 	int i;
    546 
    547 	mutex_enter(&pagermtx);
    548 	LIST_FOREACH(pgri, &pagerlist, pgr_entries) {
    549 		if (pgri->pgr_kva <= va
    550 		    && va < pgri->pgr_kva + pgri->pgr_npages*PAGE_SIZE)
    551 			break;
    552 	}
    553 	if (pgri) {
    554 		i = (va - pgri->pgr_kva) >> PAGE_SHIFT;
    555 		pg = pgri->pgr_pgs[i];
    556 	}
    557 	mutex_exit(&pagermtx);
    558 
    559 	return pg;
    560 }
    561 
    562 /*
    563  * Called with the vm object locked.
    564  *
    565  * Put vnode object pages at the end of the access queue to indicate
    566  * they have been recently accessed and should not be immediate
    567  * candidates for pageout.  Do not do this for lookups done by
    568  * the pagedaemon to mimic pmap_kentered mappings which don't track
    569  * access information.
    570  */
    571 struct vm_page *
    572 uvm_pagelookup(struct uvm_object *uobj, voff_t off)
    573 {
    574 	struct vm_page *pg;
    575 	bool ispagedaemon = curlwp == uvm.pagedaemon_lwp;
    576 
    577 	pg = rb_tree_find_node(&uobj->rb_tree, &off);
    578 	if (pg && !UVM_OBJ_IS_AOBJ(pg->uobject) && !ispagedaemon) {
    579 		mutex_enter(&uvm_pageqlock);
    580 		TAILQ_REMOVE(&vmpage_lruqueue, pg, pageq.queue);
    581 		TAILQ_INSERT_TAIL(&vmpage_lruqueue, pg, pageq.queue);
    582 		mutex_exit(&uvm_pageqlock);
    583 	}
    584 
    585 	return pg;
    586 }
    587 
    588 void
    589 uvm_page_unbusy(struct vm_page **pgs, int npgs)
    590 {
    591 	struct vm_page *pg;
    592 	int i;
    593 
    594 	KASSERT(npgs > 0);
    595 	KASSERT(mutex_owned(pgs[0]->uobject->vmobjlock));
    596 
    597 	for (i = 0; i < npgs; i++) {
    598 		pg = pgs[i];
    599 		if (pg == NULL)
    600 			continue;
    601 
    602 		KASSERT(pg->flags & PG_BUSY);
    603 		if (pg->flags & PG_WANTED)
    604 			wakeup(pg);
    605 		if (pg->flags & PG_RELEASED)
    606 			uvm_pagefree(pg);
    607 		else
    608 			pg->flags &= ~(PG_WANTED|PG_BUSY);
    609 	}
    610 }
    611 
    612 void
    613 uvm_estimatepageable(int *active, int *inactive)
    614 {
    615 
    616 	/* XXX: guessing game */
    617 	*active = 1024;
    618 	*inactive = 1024;
    619 }
    620 
    621 bool
    622 vm_map_starved_p(struct vm_map *map)
    623 {
    624 
    625 	if (map->flags & VM_MAP_WANTVA)
    626 		return true;
    627 
    628 	return false;
    629 }
    630 
    631 int
    632 uvm_loan(struct vm_map *map, vaddr_t start, vsize_t len, void *v, int flags)
    633 {
    634 
    635 	panic("%s: unimplemented", __func__);
    636 }
    637 
    638 void
    639 uvm_unloan(void *v, int npages, int flags)
    640 {
    641 
    642 	panic("%s: unimplemented", __func__);
    643 }
    644 
    645 int
    646 uvm_loanuobjpages(struct uvm_object *uobj, voff_t pgoff, int orignpages,
    647 	struct vm_page **opp)
    648 {
    649 
    650 	return EBUSY;
    651 }
    652 
    653 struct vm_page *
    654 uvm_loanbreak(struct vm_page *pg)
    655 {
    656 
    657 	panic("%s: unimplemented", __func__);
    658 }
    659 
    660 void
    661 ubc_purge(struct uvm_object *uobj)
    662 {
    663 
    664 }
    665 
    666 vaddr_t
    667 uvm_default_mapaddr(struct proc *p, vaddr_t base, vsize_t sz)
    668 {
    669 
    670 	return 0;
    671 }
    672 
    673 int
    674 uvm_map_protect(struct vm_map *map, vaddr_t start, vaddr_t end,
    675 	vm_prot_t prot, bool set_max)
    676 {
    677 
    678 	return EOPNOTSUPP;
    679 }
    680 
    681 /*
    682  * UVM km
    683  */
    684 
    685 vaddr_t
    686 uvm_km_alloc(struct vm_map *map, vsize_t size, vsize_t align, uvm_flag_t flags)
    687 {
    688 	void *rv, *desired = NULL;
    689 	int alignbit, error;
    690 
    691 #ifdef __x86_64__
    692 	/*
    693 	 * On amd64, allocate all module memory from the lowest 2GB.
    694 	 * This is because NetBSD kernel modules are compiled
    695 	 * with -mcmodel=kernel and reserve only 4 bytes for
    696 	 * offsets.  If we load code compiled with -mcmodel=kernel
    697 	 * anywhere except the lowest or highest 2GB, it will not
    698 	 * work.  Since userspace does not have access to the highest
    699 	 * 2GB, use the lowest 2GB.
    700 	 *
    701 	 * Note: this assumes the rump kernel resides in
    702 	 * the lowest 2GB as well.
    703 	 *
    704 	 * Note2: yes, it's a quick hack, but since this the only
    705 	 * place where we care about the map we're allocating from,
    706 	 * just use a simple "if" instead of coming up with a fancy
    707 	 * generic solution.
    708 	 */
    709 	if (map == module_map) {
    710 		desired = (void *)(0x80000000 - size);
    711 	}
    712 #endif
    713 
    714 	if (__predict_false(map == module_map)) {
    715 		alignbit = 0;
    716 		if (align) {
    717 			alignbit = ffs(align)-1;
    718 		}
    719 		error = rumpuser_anonmmap(desired, size, alignbit,
    720 		    flags & UVM_KMF_EXEC, &rv);
    721 	} else {
    722 		error = rumpuser_malloc(size, align, &rv);
    723 	}
    724 
    725 	if (error) {
    726 		if (flags & (UVM_KMF_CANFAIL | UVM_KMF_NOWAIT))
    727 			return 0;
    728 		else
    729 			panic("uvm_km_alloc failed");
    730 	}
    731 
    732 	if (flags & UVM_KMF_ZERO)
    733 		memset(rv, 0, size);
    734 
    735 	return (vaddr_t)rv;
    736 }
    737 
    738 void
    739 uvm_km_free(struct vm_map *map, vaddr_t vaddr, vsize_t size, uvm_flag_t flags)
    740 {
    741 
    742 	if (__predict_false(map == module_map))
    743 		rumpuser_unmap((void *)vaddr, size);
    744 	else
    745 		rumpuser_free((void *)vaddr, size);
    746 }
    747 
    748 struct vm_map *
    749 uvm_km_suballoc(struct vm_map *map, vaddr_t *minaddr, vaddr_t *maxaddr,
    750 	vsize_t size, int pageable, bool fixed, struct vm_map *submap)
    751 {
    752 
    753 	return (struct vm_map *)417416;
    754 }
    755 
    756 int
    757 uvm_km_kmem_alloc(vmem_t *vm, vmem_size_t size, vm_flag_t flags,
    758     vmem_addr_t *addr)
    759 {
    760 	vaddr_t va;
    761 	va = (vaddr_t)rump_hypermalloc(size, PAGE_SIZE,
    762 	    (flags & VM_SLEEP), "kmalloc");
    763 
    764 	if (va) {
    765 		*addr = va;
    766 		return 0;
    767 	} else {
    768 		return ENOMEM;
    769 	}
    770 }
    771 
    772 void
    773 uvm_km_kmem_free(vmem_t *vm, vmem_addr_t addr, vmem_size_t size)
    774 {
    775 
    776 	rump_hyperfree((void *)addr, size);
    777 }
    778 
    779 /*
    780  * VM space locking routines.  We don't really have to do anything,
    781  * since the pages are always "wired" (both local and remote processes).
    782  */
    783 int
    784 uvm_vslock(struct vmspace *vs, void *addr, size_t len, vm_prot_t access)
    785 {
    786 
    787 	return 0;
    788 }
    789 
    790 void
    791 uvm_vsunlock(struct vmspace *vs, void *addr, size_t len)
    792 {
    793 
    794 }
    795 
    796 /*
    797  * For the local case the buffer mappers don't need to do anything.
    798  * For the remote case we need to reserve space and copy data in or
    799  * out, depending on B_READ/B_WRITE.
    800  */
    801 int
    802 vmapbuf(struct buf *bp, vsize_t len)
    803 {
    804 	int error = 0;
    805 
    806 	bp->b_saveaddr = bp->b_data;
    807 
    808 	/* remote case */
    809 	if (!RUMP_LOCALPROC_P(curproc)) {
    810 		bp->b_data = rump_hypermalloc(len, 0, true, "vmapbuf");
    811 		if (BUF_ISWRITE(bp)) {
    812 			error = copyin(bp->b_saveaddr, bp->b_data, len);
    813 			if (error) {
    814 				rump_hyperfree(bp->b_data, len);
    815 				bp->b_data = bp->b_saveaddr;
    816 				bp->b_saveaddr = 0;
    817 			}
    818 		}
    819 	}
    820 
    821 	return error;
    822 }
    823 
    824 void
    825 vunmapbuf(struct buf *bp, vsize_t len)
    826 {
    827 
    828 	/* remote case */
    829 	if (!RUMP_LOCALPROC_P(bp->b_proc)) {
    830 		if (BUF_ISREAD(bp)) {
    831 			bp->b_error = copyout_proc(bp->b_proc,
    832 			    bp->b_data, bp->b_saveaddr, len);
    833 		}
    834 		rump_hyperfree(bp->b_data, len);
    835 	}
    836 
    837 	bp->b_data = bp->b_saveaddr;
    838 	bp->b_saveaddr = 0;
    839 }
    840 
    841 void
    842 uvmspace_addref(struct vmspace *vm)
    843 {
    844 
    845 	/*
    846 	 * No dynamically allocated vmspaces exist.
    847 	 */
    848 }
    849 
    850 void
    851 uvmspace_free(struct vmspace *vm)
    852 {
    853 
    854 	/* nothing for now */
    855 }
    856 
    857 /*
    858  * page life cycle stuff.  it really doesn't exist, so just stubs.
    859  */
    860 
    861 void
    862 uvm_pageactivate(struct vm_page *pg)
    863 {
    864 
    865 	/* nada */
    866 }
    867 
    868 void
    869 uvm_pagedeactivate(struct vm_page *pg)
    870 {
    871 
    872 	/* nada */
    873 }
    874 
    875 void
    876 uvm_pagedequeue(struct vm_page *pg)
    877 {
    878 
    879 	/* nada*/
    880 }
    881 
    882 void
    883 uvm_pageenqueue(struct vm_page *pg)
    884 {
    885 
    886 	/* nada */
    887 }
    888 
    889 void
    890 uvmpdpol_anfree(struct vm_anon *an)
    891 {
    892 
    893 	/* nada */
    894 }
    895 
    896 /*
    897  * Physical address accessors.
    898  */
    899 
    900 struct vm_page *
    901 uvm_phys_to_vm_page(paddr_t pa)
    902 {
    903 
    904 	return NULL;
    905 }
    906 
    907 paddr_t
    908 uvm_vm_page_to_phys(const struct vm_page *pg)
    909 {
    910 
    911 	return 0;
    912 }
    913 
    914 /*
    915  * Routines related to the Page Baroness.
    916  */
    917 
    918 void
    919 uvm_wait(const char *msg)
    920 {
    921 
    922 	if (__predict_false(rump_threads == 0))
    923 		panic("pagedaemon missing (RUMP_THREADS = 0)");
    924 
    925 	if (curlwp == uvm.pagedaemon_lwp) {
    926 		/* is it possible for us to later get memory? */
    927 		if (!uvmexp.paging)
    928 			panic("pagedaemon out of memory");
    929 	}
    930 
    931 	mutex_enter(&pdaemonmtx);
    932 	pdaemon_waiters++;
    933 	cv_signal(&pdaemoncv);
    934 	cv_wait(&oomwait, &pdaemonmtx);
    935 	mutex_exit(&pdaemonmtx);
    936 }
    937 
    938 void
    939 uvm_pageout_start(int npages)
    940 {
    941 
    942 	mutex_enter(&pdaemonmtx);
    943 	uvmexp.paging += npages;
    944 	mutex_exit(&pdaemonmtx);
    945 }
    946 
    947 void
    948 uvm_pageout_done(int npages)
    949 {
    950 
    951 	if (!npages)
    952 		return;
    953 
    954 	mutex_enter(&pdaemonmtx);
    955 	KASSERT(uvmexp.paging >= npages);
    956 	uvmexp.paging -= npages;
    957 
    958 	if (pdaemon_waiters) {
    959 		pdaemon_waiters = 0;
    960 		cv_broadcast(&oomwait);
    961 	}
    962 	mutex_exit(&pdaemonmtx);
    963 }
    964 
    965 static bool
    966 processpage(struct vm_page *pg, bool *lockrunning)
    967 {
    968 	struct uvm_object *uobj;
    969 
    970 	uobj = pg->uobject;
    971 	if (mutex_tryenter(uobj->vmobjlock)) {
    972 		if ((pg->flags & PG_BUSY) == 0) {
    973 			mutex_exit(&uvm_pageqlock);
    974 			uobj->pgops->pgo_put(uobj, pg->offset,
    975 			    pg->offset + PAGE_SIZE,
    976 			    PGO_CLEANIT|PGO_FREE);
    977 			KASSERT(!mutex_owned(uobj->vmobjlock));
    978 			return true;
    979 		} else {
    980 			mutex_exit(uobj->vmobjlock);
    981 		}
    982 	} else if (*lockrunning == false && ncpu > 1) {
    983 		CPU_INFO_ITERATOR cii;
    984 		struct cpu_info *ci;
    985 		struct lwp *l;
    986 
    987 		l = mutex_owner(uobj->vmobjlock);
    988 		for (CPU_INFO_FOREACH(cii, ci)) {
    989 			if (ci->ci_curlwp == l) {
    990 				*lockrunning = true;
    991 				break;
    992 			}
    993 		}
    994 	}
    995 
    996 	return false;
    997 }
    998 
    999 /*
   1000  * The Diabolical pageDaemon Director (DDD).
   1001  *
   1002  * This routine can always use better heuristics.
   1003  */
   1004 void
   1005 uvm_pageout(void *arg)
   1006 {
   1007 	struct vm_page *pg;
   1008 	struct pool *pp, *pp_first;
   1009 	int cleaned, skip, skipped;
   1010 	bool succ;
   1011 	bool lockrunning;
   1012 
   1013 	mutex_enter(&pdaemonmtx);
   1014 	for (;;) {
   1015 		if (!NEED_PAGEDAEMON()) {
   1016 			kernel_map->flags &= ~VM_MAP_WANTVA;
   1017 		}
   1018 
   1019 		if (pdaemon_waiters) {
   1020 			pdaemon_waiters = 0;
   1021 			cv_broadcast(&oomwait);
   1022 		}
   1023 
   1024 		cv_wait(&pdaemoncv, &pdaemonmtx);
   1025 		uvmexp.pdwoke++;
   1026 
   1027 		/* tell the world that we are hungry */
   1028 		kernel_map->flags |= VM_MAP_WANTVA;
   1029 		mutex_exit(&pdaemonmtx);
   1030 
   1031 		/*
   1032 		 * step one: reclaim the page cache.  this should give
   1033 		 * us the biggest earnings since whole pages are released
   1034 		 * into backing memory.
   1035 		 */
   1036 		pool_cache_reclaim(&pagecache);
   1037 		if (!NEED_PAGEDAEMON()) {
   1038 			mutex_enter(&pdaemonmtx);
   1039 			continue;
   1040 		}
   1041 
   1042 		/*
   1043 		 * Ok, so that didn't help.  Next, try to hunt memory
   1044 		 * by pushing out vnode pages.  The pages might contain
   1045 		 * useful cached data, but we need the memory.
   1046 		 */
   1047 		cleaned = 0;
   1048 		skip = 0;
   1049 		lockrunning = false;
   1050  again:
   1051 		mutex_enter(&uvm_pageqlock);
   1052 		while (cleaned < PAGEDAEMON_OBJCHUNK) {
   1053 			skipped = 0;
   1054 			TAILQ_FOREACH(pg, &vmpage_lruqueue, pageq.queue) {
   1055 
   1056 				/*
   1057 				 * skip over pages we _might_ have tried
   1058 				 * to handle earlier.  they might not be
   1059 				 * exactly the same ones, but I'm not too
   1060 				 * concerned.
   1061 				 */
   1062 				while (skipped++ < skip)
   1063 					continue;
   1064 
   1065 				if (processpage(pg, &lockrunning)) {
   1066 					cleaned++;
   1067 					goto again;
   1068 				}
   1069 
   1070 				skip++;
   1071 			}
   1072 			break;
   1073 		}
   1074 		mutex_exit(&uvm_pageqlock);
   1075 
   1076 		/*
   1077 		 * Ok, someone is running with an object lock held.
   1078 		 * We want to yield the host CPU to make sure the
   1079 		 * thread is not parked on the host.  Since sched_yield()
   1080 		 * doesn't appear to do anything on NetBSD, nanosleep
   1081 		 * for the smallest possible time and hope we're back in
   1082 		 * the game soon.
   1083 		 */
   1084 		if (cleaned == 0 && lockrunning) {
   1085 			rumpuser_clock_sleep(RUMPUSER_CLOCK_RELWALL, 0, 1);
   1086 
   1087 			lockrunning = false;
   1088 			skip = 0;
   1089 
   1090 			/* and here we go again */
   1091 			goto again;
   1092 		}
   1093 
   1094 		/*
   1095 		 * And of course we need to reclaim the page cache
   1096 		 * again to actually release memory.
   1097 		 */
   1098 		pool_cache_reclaim(&pagecache);
   1099 		if (!NEED_PAGEDAEMON()) {
   1100 			mutex_enter(&pdaemonmtx);
   1101 			continue;
   1102 		}
   1103 
   1104 		/*
   1105 		 * And then drain the pools.  Wipe them out ... all of them.
   1106 		 */
   1107 		for (pp_first = NULL;;) {
   1108 			if (rump_vfs_drainbufs)
   1109 				rump_vfs_drainbufs(10 /* XXX: estimate! */);
   1110 
   1111 			succ = pool_drain(&pp);
   1112 			if (succ || pp == pp_first)
   1113 				break;
   1114 
   1115 			if (pp_first == NULL)
   1116 				pp_first = pp;
   1117 		}
   1118 
   1119 		/*
   1120 		 * Need to use PYEC on our bag of tricks.
   1121 		 * Unfortunately, the wife just borrowed it.
   1122 		 */
   1123 
   1124 		mutex_enter(&pdaemonmtx);
   1125 		if (!succ && cleaned == 0 && pdaemon_waiters &&
   1126 		    uvmexp.paging == 0) {
   1127 			rumpuser_dprintf("pagedaemoness: failed to reclaim "
   1128 			    "memory ... sleeping (deadlock?)\n");
   1129 			cv_timedwait(&pdaemoncv, &pdaemonmtx, hz);
   1130 		}
   1131 	}
   1132 
   1133 	panic("you can swap out any time you like, but you can never leave");
   1134 }
   1135 
   1136 void
   1137 uvm_kick_pdaemon()
   1138 {
   1139 
   1140 	/*
   1141 	 * Wake up the diabolical pagedaemon director if we are over
   1142 	 * 90% of the memory limit.  This is a complete and utter
   1143 	 * stetson-harrison decision which you are allowed to finetune.
   1144 	 * Don't bother locking.  If we have some unflushed caches,
   1145 	 * other waker-uppers will deal with the issue.
   1146 	 */
   1147 	if (NEED_PAGEDAEMON()) {
   1148 		cv_signal(&pdaemoncv);
   1149 	}
   1150 }
   1151 
   1152 void *
   1153 rump_hypermalloc(size_t howmuch, int alignment, bool waitok, const char *wmsg)
   1154 {
   1155 	const unsigned long thelimit =
   1156 	    curlwp == uvm.pagedaemon_lwp ? pdlimit : rump_physmemlimit;
   1157 	unsigned long newmem;
   1158 	void *rv;
   1159 	int error;
   1160 
   1161 	uvm_kick_pdaemon(); /* ouch */
   1162 
   1163 	/* first we must be within the limit */
   1164  limitagain:
   1165 	if (thelimit != RUMPMEM_UNLIMITED) {
   1166 		newmem = atomic_add_long_nv(&curphysmem, howmuch);
   1167 		if (newmem > thelimit) {
   1168 			newmem = atomic_add_long_nv(&curphysmem, -howmuch);
   1169 			if (!waitok) {
   1170 				return NULL;
   1171 			}
   1172 			uvm_wait(wmsg);
   1173 			goto limitagain;
   1174 		}
   1175 	}
   1176 
   1177 	/* second, we must get something from the backend */
   1178  again:
   1179 	error = rumpuser_malloc(howmuch, alignment, &rv);
   1180 	if (__predict_false(error && waitok)) {
   1181 		uvm_wait(wmsg);
   1182 		goto again;
   1183 	}
   1184 
   1185 	return rv;
   1186 }
   1187 
   1188 void
   1189 rump_hyperfree(void *what, size_t size)
   1190 {
   1191 
   1192 	if (rump_physmemlimit != RUMPMEM_UNLIMITED) {
   1193 		atomic_add_long(&curphysmem, -size);
   1194 	}
   1195 	rumpuser_free(what, size);
   1196 }
   1197