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