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