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