Home | History | Annotate | Line # | Download | only in rumpkern
vm.c revision 1.88
      1 /*	$NetBSD: vm.c,v 1.88 2010/09/06 20:10:20 pooka 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.88 2010/09/06 20:10:20 pooka Exp $");
     45 
     46 #include <sys/param.h>
     47 #include <sys/atomic.h>
     48 #include <sys/buf.h>
     49 #include <sys/kernel.h>
     50 #include <sys/kmem.h>
     51 #include <sys/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 
     67 kmutex_t uvm_pageqlock;
     68 kmutex_t uvm_swap_data_lock;
     69 
     70 struct uvmexp uvmexp;
     71 struct uvm uvm;
     72 
     73 struct vm_map rump_vmmap;
     74 static struct vm_map_kernel kmem_map_store;
     75 struct vm_map *kmem_map = &kmem_map_store.vmk_map;
     76 const struct rb_tree_ops uvm_page_tree_ops;
     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 #define RUMPMEM_UNLIMITED ((unsigned long)-1)
     86 static unsigned long physmemlimit = RUMPMEM_UNLIMITED;
     87 static unsigned long curphysmem;
     88 
     89 /*
     90  * vm pages
     91  */
     92 
     93 /* called with the object locked */
     94 struct vm_page *
     95 uvm_pagealloc_strat(struct uvm_object *uobj, voff_t off, struct vm_anon *anon,
     96 	int flags, int strat, int free_list)
     97 {
     98 	struct vm_page *pg;
     99 
    100 	pg = kmem_zalloc(sizeof(struct vm_page), KM_SLEEP);
    101 	pg->offset = off;
    102 	pg->uobject = uobj;
    103 
    104 	pg->uanon = (void *)kmem_alloc(PAGE_SIZE, KM_SLEEP);
    105 	if (flags & UVM_PGA_ZERO)
    106 		memset(pg->uanon, 0, PAGE_SIZE);
    107 	pg->flags = PG_CLEAN|PG_BUSY|PG_FAKE;
    108 
    109 	TAILQ_INSERT_TAIL(&uobj->memq, pg, listq.queue);
    110 	uobj->uo_npages++;
    111 
    112 	return pg;
    113 }
    114 
    115 /*
    116  * Release a page.
    117  *
    118  * Called with the vm object locked.
    119  */
    120 void
    121 uvm_pagefree(struct vm_page *pg)
    122 {
    123 	struct uvm_object *uobj = pg->uobject;
    124 
    125 	if (pg->flags & PG_WANTED)
    126 		wakeup(pg);
    127 
    128 	uobj->uo_npages--;
    129 	TAILQ_REMOVE(&uobj->memq, pg, listq.queue);
    130 	kmem_free((void *)pg->uanon, PAGE_SIZE);
    131 	kmem_free(pg, sizeof(*pg));
    132 }
    133 
    134 void
    135 uvm_pagezero(struct vm_page *pg)
    136 {
    137 
    138 	pg->flags &= ~PG_CLEAN;
    139 	memset((void *)pg->uanon, 0, PAGE_SIZE);
    140 }
    141 
    142 /*
    143  * Misc routines
    144  */
    145 
    146 static kmutex_t pagermtx;
    147 
    148 void
    149 uvm_init(void)
    150 {
    151 	char buf[64];
    152 	int error;
    153 
    154 	if (rumpuser_getenv("RUMP_MEMLIMIT", buf, sizeof(buf), &error) == 0) {
    155 		physmemlimit = strtoll(buf, NULL, 10);
    156 		/* it's not like we'd get far with, say, 1 byte, but ... */
    157 		if (physmemlimit == 0)
    158 			panic("uvm_init: no memory available");
    159 #define HUMANIZE_BYTES 9
    160 		CTASSERT(sizeof(buf) >= HUMANIZE_BYTES);
    161 		format_bytes(buf, HUMANIZE_BYTES, physmemlimit);
    162 #undef HUMANIZE_BYTES
    163 	} else {
    164 		strlcpy(buf, "unlimited (host limit)", sizeof(buf));
    165 	}
    166 	aprint_verbose("total memory = %s\n", buf);
    167 
    168 	uvmexp.free = 1024*1024; /* XXX: arbitrary & not updated */
    169 
    170 	mutex_init(&pagermtx, MUTEX_DEFAULT, 0);
    171 	mutex_init(&uvm_pageqlock, MUTEX_DEFAULT, 0);
    172 	mutex_init(&uvm_swap_data_lock, MUTEX_DEFAULT, 0);
    173 
    174 	mutex_init(&pdaemonmtx, MUTEX_DEFAULT, 0);
    175 	cv_init(&pdaemoncv, "pdaemon");
    176 	cv_init(&oomwait, "oomwait");
    177 
    178 	kernel_map->pmap = pmap_kernel();
    179 	callback_head_init(&kernel_map_store.vmk_reclaim_callback, IPL_VM);
    180 	kmem_map->pmap = pmap_kernel();
    181 	callback_head_init(&kmem_map_store.vmk_reclaim_callback, IPL_VM);
    182 }
    183 
    184 void
    185 uvmspace_init(struct vmspace *vm, struct pmap *pmap, vaddr_t vmin, vaddr_t vmax)
    186 {
    187 
    188 	vm->vm_map.pmap = pmap_kernel();
    189 	vm->vm_refcnt = 1;
    190 }
    191 
    192 void
    193 uvm_pagewire(struct vm_page *pg)
    194 {
    195 
    196 	/* nada */
    197 }
    198 
    199 void
    200 uvm_pageunwire(struct vm_page *pg)
    201 {
    202 
    203 	/* nada */
    204 }
    205 
    206 /* where's your schmonz now? */
    207 #define PUNLIMIT(a)	\
    208 p->p_rlimit[a].rlim_cur = p->p_rlimit[a].rlim_max = RLIM_INFINITY;
    209 void
    210 uvm_init_limits(struct proc *p)
    211 {
    212 
    213 	PUNLIMIT(RLIMIT_STACK);
    214 	PUNLIMIT(RLIMIT_DATA);
    215 	PUNLIMIT(RLIMIT_RSS);
    216 	PUNLIMIT(RLIMIT_AS);
    217 	/* nice, cascade */
    218 }
    219 #undef PUNLIMIT
    220 
    221 /*
    222  * This satisfies the "disgusting mmap hack" used by proplib.
    223  * We probably should grow some more assertables to make sure we're
    224  * not satisfying anything we shouldn't be satisfying.  At least we
    225  * should make sure it's the local machine we're mmapping ...
    226  */
    227 int
    228 uvm_mmap(struct vm_map *map, vaddr_t *addr, vsize_t size, vm_prot_t prot,
    229 	vm_prot_t maxprot, int flags, void *handle, voff_t off, vsize_t locklim)
    230 {
    231 	void *uaddr;
    232 	int error;
    233 
    234 	if (prot != (VM_PROT_READ | VM_PROT_WRITE))
    235 		panic("uvm_mmap() variant unsupported");
    236 	if (flags != (MAP_PRIVATE | MAP_ANON))
    237 		panic("uvm_mmap() variant unsupported");
    238 	/* no reason in particular, but cf. uvm_default_mapaddr() */
    239 	if (*addr != 0)
    240 		panic("uvm_mmap() variant unsupported");
    241 
    242 	uaddr = rumpuser_anonmmap(NULL, size, 0, 0, &error);
    243 	if (uaddr == NULL)
    244 		return error;
    245 
    246 	*addr = (vaddr_t)uaddr;
    247 	return 0;
    248 }
    249 
    250 struct pagerinfo {
    251 	vaddr_t pgr_kva;
    252 	int pgr_npages;
    253 	struct vm_page **pgr_pgs;
    254 	bool pgr_read;
    255 
    256 	LIST_ENTRY(pagerinfo) pgr_entries;
    257 };
    258 static LIST_HEAD(, pagerinfo) pagerlist = LIST_HEAD_INITIALIZER(pagerlist);
    259 
    260 /*
    261  * Pager "map" in routine.  Instead of mapping, we allocate memory
    262  * and copy page contents there.  Not optimal or even strictly
    263  * correct (the caller might modify the page contents after mapping
    264  * them in), but what the heck.  Assumes UVMPAGER_MAPIN_WAITOK.
    265  */
    266 vaddr_t
    267 uvm_pagermapin(struct vm_page **pgs, int npages, int flags)
    268 {
    269 	struct pagerinfo *pgri;
    270 	vaddr_t curkva;
    271 	int i;
    272 
    273 	/* allocate structures */
    274 	pgri = kmem_alloc(sizeof(*pgri), KM_SLEEP);
    275 	pgri->pgr_kva = (vaddr_t)kmem_alloc(npages * PAGE_SIZE, KM_SLEEP);
    276 	pgri->pgr_npages = npages;
    277 	pgri->pgr_pgs = kmem_alloc(sizeof(struct vm_page *) * npages, KM_SLEEP);
    278 	pgri->pgr_read = (flags & UVMPAGER_MAPIN_READ) != 0;
    279 
    280 	/* copy contents to "mapped" memory */
    281 	for (i = 0, curkva = pgri->pgr_kva;
    282 	    i < npages;
    283 	    i++, curkva += PAGE_SIZE) {
    284 		/*
    285 		 * We need to copy the previous contents of the pages to
    286 		 * the window even if we are reading from the
    287 		 * device, since the device might not fill the contents of
    288 		 * the full mapped range and we will end up corrupting
    289 		 * data when we unmap the window.
    290 		 */
    291 		memcpy((void*)curkva, pgs[i]->uanon, PAGE_SIZE);
    292 		pgri->pgr_pgs[i] = pgs[i];
    293 	}
    294 
    295 	mutex_enter(&pagermtx);
    296 	LIST_INSERT_HEAD(&pagerlist, pgri, pgr_entries);
    297 	mutex_exit(&pagermtx);
    298 
    299 	return pgri->pgr_kva;
    300 }
    301 
    302 /*
    303  * map out the pager window.  return contents from VA to page storage
    304  * and free structures.
    305  *
    306  * Note: does not currently support partial frees
    307  */
    308 void
    309 uvm_pagermapout(vaddr_t kva, int npages)
    310 {
    311 	struct pagerinfo *pgri;
    312 	vaddr_t curkva;
    313 	int i;
    314 
    315 	mutex_enter(&pagermtx);
    316 	LIST_FOREACH(pgri, &pagerlist, pgr_entries) {
    317 		if (pgri->pgr_kva == kva)
    318 			break;
    319 	}
    320 	KASSERT(pgri);
    321 	if (pgri->pgr_npages != npages)
    322 		panic("uvm_pagermapout: partial unmapping not supported");
    323 	LIST_REMOVE(pgri, pgr_entries);
    324 	mutex_exit(&pagermtx);
    325 
    326 	if (pgri->pgr_read) {
    327 		for (i = 0, curkva = pgri->pgr_kva;
    328 		    i < pgri->pgr_npages;
    329 		    i++, curkva += PAGE_SIZE) {
    330 			memcpy(pgri->pgr_pgs[i]->uanon,(void*)curkva,PAGE_SIZE);
    331 		}
    332 	}
    333 
    334 	kmem_free(pgri->pgr_pgs, npages * sizeof(struct vm_page *));
    335 	kmem_free((void*)pgri->pgr_kva, npages * PAGE_SIZE);
    336 	kmem_free(pgri, sizeof(*pgri));
    337 }
    338 
    339 /*
    340  * convert va in pager window to page structure.
    341  * XXX: how expensive is this (global lock, list traversal)?
    342  */
    343 struct vm_page *
    344 uvm_pageratop(vaddr_t va)
    345 {
    346 	struct pagerinfo *pgri;
    347 	struct vm_page *pg = NULL;
    348 	int i;
    349 
    350 	mutex_enter(&pagermtx);
    351 	LIST_FOREACH(pgri, &pagerlist, pgr_entries) {
    352 		if (pgri->pgr_kva <= va
    353 		    && va < pgri->pgr_kva + pgri->pgr_npages*PAGE_SIZE)
    354 			break;
    355 	}
    356 	if (pgri) {
    357 		i = (va - pgri->pgr_kva) >> PAGE_SHIFT;
    358 		pg = pgri->pgr_pgs[i];
    359 	}
    360 	mutex_exit(&pagermtx);
    361 
    362 	return pg;
    363 }
    364 
    365 /* Called with the vm object locked */
    366 struct vm_page *
    367 uvm_pagelookup(struct uvm_object *uobj, voff_t off)
    368 {
    369 	struct vm_page *pg;
    370 
    371 	TAILQ_FOREACH(pg, &uobj->memq, listq.queue) {
    372 		if ((pg->flags & PG_MARKER) != 0)
    373 			continue;
    374 		if (pg->offset == off) {
    375 			return pg;
    376 		}
    377 	}
    378 
    379 	return NULL;
    380 }
    381 
    382 void
    383 uvm_page_unbusy(struct vm_page **pgs, int npgs)
    384 {
    385 	struct vm_page *pg;
    386 	int i;
    387 
    388 	for (i = 0; i < npgs; i++) {
    389 		pg = pgs[i];
    390 		if (pg == NULL)
    391 			continue;
    392 
    393 		KASSERT(pg->flags & PG_BUSY);
    394 		if (pg->flags & PG_WANTED)
    395 			wakeup(pg);
    396 		if (pg->flags & PG_RELEASED)
    397 			uvm_pagefree(pg);
    398 		else
    399 			pg->flags &= ~(PG_WANTED|PG_BUSY);
    400 	}
    401 }
    402 
    403 void
    404 uvm_estimatepageable(int *active, int *inactive)
    405 {
    406 
    407 	/* XXX: guessing game */
    408 	*active = 1024;
    409 	*inactive = 1024;
    410 }
    411 
    412 struct vm_map_kernel *
    413 vm_map_to_kernel(struct vm_map *map)
    414 {
    415 
    416 	return (struct vm_map_kernel *)map;
    417 }
    418 
    419 bool
    420 vm_map_starved_p(struct vm_map *map)
    421 {
    422 
    423 	if (map->flags & VM_MAP_WANTVA)
    424 		return true;
    425 
    426 	return false;
    427 }
    428 
    429 int
    430 uvm_loan(struct vm_map *map, vaddr_t start, vsize_t len, void *v, int flags)
    431 {
    432 
    433 	panic("%s: unimplemented", __func__);
    434 }
    435 
    436 void
    437 uvm_unloan(void *v, int npages, int flags)
    438 {
    439 
    440 	panic("%s: unimplemented", __func__);
    441 }
    442 
    443 int
    444 uvm_loanuobjpages(struct uvm_object *uobj, voff_t pgoff, int orignpages,
    445 	struct vm_page **opp)
    446 {
    447 
    448 	return EBUSY;
    449 }
    450 
    451 #ifdef DEBUGPRINT
    452 void
    453 uvm_object_printit(struct uvm_object *uobj, bool full,
    454 	void (*pr)(const char *, ...))
    455 {
    456 
    457 	pr("VM OBJECT at %p, refs %d", uobj, uobj->uo_refs);
    458 }
    459 #endif
    460 
    461 vaddr_t
    462 uvm_default_mapaddr(struct proc *p, vaddr_t base, vsize_t sz)
    463 {
    464 
    465 	return 0;
    466 }
    467 
    468 int
    469 uvm_map_protect(struct vm_map *map, vaddr_t start, vaddr_t end,
    470 	vm_prot_t prot, bool set_max)
    471 {
    472 
    473 	return EOPNOTSUPP;
    474 }
    475 
    476 /*
    477  * UVM km
    478  */
    479 
    480 vaddr_t
    481 uvm_km_alloc(struct vm_map *map, vsize_t size, vsize_t align, uvm_flag_t flags)
    482 {
    483 	void *rv, *desired = NULL;
    484 	int alignbit, error;
    485 
    486 #ifdef __x86_64__
    487 	/*
    488 	 * On amd64, allocate all module memory from the lowest 2GB.
    489 	 * This is because NetBSD kernel modules are compiled
    490 	 * with -mcmodel=kernel and reserve only 4 bytes for
    491 	 * offsets.  If we load code compiled with -mcmodel=kernel
    492 	 * anywhere except the lowest or highest 2GB, it will not
    493 	 * work.  Since userspace does not have access to the highest
    494 	 * 2GB, use the lowest 2GB.
    495 	 *
    496 	 * Note: this assumes the rump kernel resides in
    497 	 * the lowest 2GB as well.
    498 	 *
    499 	 * Note2: yes, it's a quick hack, but since this the only
    500 	 * place where we care about the map we're allocating from,
    501 	 * just use a simple "if" instead of coming up with a fancy
    502 	 * generic solution.
    503 	 */
    504 	extern struct vm_map *module_map;
    505 	if (map == module_map) {
    506 		desired = (void *)(0x80000000 - size);
    507 	}
    508 #endif
    509 
    510 	alignbit = 0;
    511 	if (align) {
    512 		alignbit = ffs(align)-1;
    513 	}
    514 
    515 	rv = rumpuser_anonmmap(desired, size, alignbit, flags & UVM_KMF_EXEC,
    516 	    &error);
    517 	if (rv == NULL) {
    518 		if (flags & (UVM_KMF_CANFAIL | UVM_KMF_NOWAIT))
    519 			return 0;
    520 		else
    521 			panic("uvm_km_alloc failed");
    522 	}
    523 
    524 	if (flags & UVM_KMF_ZERO)
    525 		memset(rv, 0, size);
    526 
    527 	return (vaddr_t)rv;
    528 }
    529 
    530 void
    531 uvm_km_free(struct vm_map *map, vaddr_t vaddr, vsize_t size, uvm_flag_t flags)
    532 {
    533 
    534 	rumpuser_unmap((void *)vaddr, size);
    535 }
    536 
    537 struct vm_map *
    538 uvm_km_suballoc(struct vm_map *map, vaddr_t *minaddr, vaddr_t *maxaddr,
    539 	vsize_t size, int pageable, bool fixed, struct vm_map_kernel *submap)
    540 {
    541 
    542 	return (struct vm_map *)417416;
    543 }
    544 
    545 vaddr_t
    546 uvm_km_alloc_poolpage(struct vm_map *map, bool waitok)
    547 {
    548 
    549 	return (vaddr_t)rump_hypermalloc(PAGE_SIZE, PAGE_SIZE,
    550 	    waitok, "kmalloc");
    551 }
    552 
    553 void
    554 uvm_km_free_poolpage(struct vm_map *map, vaddr_t addr)
    555 {
    556 
    557 	rump_hyperfree((void *)addr, PAGE_SIZE);
    558 }
    559 
    560 vaddr_t
    561 uvm_km_alloc_poolpage_cache(struct vm_map *map, bool waitok)
    562 {
    563 
    564 	return uvm_km_alloc_poolpage(map, waitok);
    565 }
    566 
    567 void
    568 uvm_km_free_poolpage_cache(struct vm_map *map, vaddr_t vaddr)
    569 {
    570 
    571 	uvm_km_free_poolpage(map, vaddr);
    572 }
    573 
    574 void
    575 uvm_km_va_drain(struct vm_map *map, uvm_flag_t flags)
    576 {
    577 
    578 	/* we eventually maybe want some model for available memory */
    579 }
    580 
    581 /*
    582  * Mapping and vm space locking routines.
    583  * XXX: these don't work for non-local vmspaces
    584  */
    585 int
    586 uvm_vslock(struct vmspace *vs, void *addr, size_t len, vm_prot_t access)
    587 {
    588 
    589 	KASSERT(vs == &vmspace0);
    590 	return 0;
    591 }
    592 
    593 void
    594 uvm_vsunlock(struct vmspace *vs, void *addr, size_t len)
    595 {
    596 
    597 	KASSERT(vs == &vmspace0);
    598 }
    599 
    600 void
    601 vmapbuf(struct buf *bp, vsize_t len)
    602 {
    603 
    604 	bp->b_saveaddr = bp->b_data;
    605 }
    606 
    607 void
    608 vunmapbuf(struct buf *bp, vsize_t len)
    609 {
    610 
    611 	bp->b_data = bp->b_saveaddr;
    612 	bp->b_saveaddr = 0;
    613 }
    614 
    615 void
    616 uvmspace_addref(struct vmspace *vm)
    617 {
    618 
    619 	/*
    620 	 * there is only vmspace0.  we're not planning on
    621 	 * feeding it to the fishes.
    622 	 */
    623 }
    624 
    625 void
    626 uvmspace_free(struct vmspace *vm)
    627 {
    628 
    629 	/* nothing for now */
    630 }
    631 
    632 int
    633 uvm_io(struct vm_map *map, struct uio *uio)
    634 {
    635 
    636 	/*
    637 	 * just do direct uio for now.  but this needs some vmspace
    638 	 * olympics for rump_sysproxy.
    639 	 */
    640 	return uiomove((void *)(vaddr_t)uio->uio_offset, uio->uio_resid, uio);
    641 }
    642 
    643 /*
    644  * page life cycle stuff.  it really doesn't exist, so just stubs.
    645  */
    646 
    647 void
    648 uvm_pageactivate(struct vm_page *pg)
    649 {
    650 
    651 	/* nada */
    652 }
    653 
    654 void
    655 uvm_pagedeactivate(struct vm_page *pg)
    656 {
    657 
    658 	/* nada */
    659 }
    660 
    661 void
    662 uvm_pagedequeue(struct vm_page *pg)
    663 {
    664 
    665 	/* nada*/
    666 }
    667 
    668 void
    669 uvm_pageenqueue(struct vm_page *pg)
    670 {
    671 
    672 	/* nada */
    673 }
    674 
    675 void
    676 uvmpdpol_anfree(struct vm_anon *an)
    677 {
    678 
    679 	/* nada */
    680 }
    681 
    682 /*
    683  * Routines related to the Page Baroness.
    684  */
    685 
    686 void
    687 uvm_wait(const char *msg)
    688 {
    689 
    690 	if (__predict_false(curlwp == uvm.pagedaemon_lwp))
    691 		panic("pagedaemon out of memory");
    692 	if (__predict_false(rump_threads == 0))
    693 		panic("pagedaemon missing (RUMP_THREADS = 0)");
    694 
    695 	mutex_enter(&pdaemonmtx);
    696 	pdaemon_waiters++;
    697 	cv_signal(&pdaemoncv);
    698 	cv_wait(&oomwait, &pdaemonmtx);
    699 	mutex_exit(&pdaemonmtx);
    700 }
    701 
    702 void
    703 uvm_pageout_start(int npages)
    704 {
    705 
    706 	/* we don't have the heuristics */
    707 }
    708 
    709 void
    710 uvm_pageout_done(int npages)
    711 {
    712 
    713 	/* could wakeup waiters, but just let the pagedaemon do it */
    714 }
    715 
    716 /*
    717  * Under-construction page mistress.  This is lacking vfs support, namely:
    718  *
    719  *  1) draining vfs buffers
    720  *  2) paging out pages in vm vnode objects
    721  *     (we will not page out anon memory on the basis that
    722  *     that's the task of the host)
    723  */
    724 
    725 void
    726 uvm_pageout(void *arg)
    727 {
    728 	struct pool *pp, *pp_first;
    729 	uint64_t where;
    730 	int timo = 0;
    731 	bool succ;
    732 
    733 	mutex_enter(&pdaemonmtx);
    734 	for (;;) {
    735 		cv_timedwait(&pdaemoncv, &pdaemonmtx, timo);
    736 		uvmexp.pdwoke++;
    737 		kernel_map->flags |= VM_MAP_WANTVA;
    738 		mutex_exit(&pdaemonmtx);
    739 
    740 		succ = false;
    741 		pool_drain_start(&pp_first, &where);
    742 		pp = pp_first;
    743 		for (;;) {
    744 			succ = pool_drain_end(pp, where);
    745 			if (succ)
    746 				break;
    747 			pool_drain_start(&pp, &where);
    748 			if (pp == pp_first) {
    749 				succ = pool_drain_end(pp, where);
    750 				break;
    751 			}
    752 		}
    753 		mutex_enter(&pdaemonmtx);
    754 
    755 		if (!succ) {
    756 			rumpuser_dprintf("pagedaemoness: failed to reclaim "
    757 			    "memory ... sleeping (deadlock?)\n");
    758 			timo = hz;
    759 			continue;
    760 		}
    761 		kernel_map->flags &= ~VM_MAP_WANTVA;
    762 		timo = 0;
    763 
    764 		if (pdaemon_waiters) {
    765 			pdaemon_waiters = 0;
    766 			cv_broadcast(&oomwait);
    767 		}
    768 	}
    769 
    770 	panic("you can swap out any time you like, but you can never leave");
    771 }
    772 
    773 /*
    774  * In a regular kernel the pagedaemon is activated when memory becomes
    775  * low.  In a virtual rump kernel we do not know exactly how much memory
    776  * we have available -- it depends on the conditions on the host.
    777  * Therefore, we cannot preemptively kick the pagedaemon.  Rather, we
    778  * wait until things we desperate and we're forced to uvm_wait().
    779  *
    780  * The alternative would be to allocate a huge chunk of memory at
    781  * startup, but that solution has a number of problems including
    782  * being a resource hog, failing anyway due to host memory overcommit
    783  * and core dump size.
    784  */
    785 
    786 void
    787 uvm_kick_pdaemon()
    788 {
    789 
    790 	/* nada */
    791 }
    792 
    793 void *
    794 rump_hypermalloc(size_t howmuch, int alignment, bool waitok, const char *wmsg)
    795 {
    796 	unsigned long newmem;
    797 	void *rv;
    798 
    799 	/* first we must be within the limit */
    800  limitagain:
    801 	if (physmemlimit != RUMPMEM_UNLIMITED) {
    802 		newmem = atomic_add_long_nv(&curphysmem, howmuch);
    803 		if (newmem > physmemlimit) {
    804 			newmem = atomic_add_long_nv(&curphysmem, -howmuch);
    805 			if (!waitok)
    806 				return NULL;
    807 			uvm_wait(wmsg);
    808 			goto limitagain;
    809 		}
    810 	}
    811 
    812 	/* second, we must get something from the backend */
    813  again:
    814 	rv = rumpuser_malloc(howmuch, alignment);
    815 	if (__predict_false(rv == NULL && waitok)) {
    816 		uvm_wait(wmsg);
    817 		goto again;
    818 	}
    819 
    820 	return rv;
    821 }
    822 
    823 void
    824 rump_hyperfree(void *what, size_t size)
    825 {
    826 
    827 	if (physmemlimit != RUMPMEM_UNLIMITED) {
    828 		atomic_add_long(&curphysmem, -size);
    829 	}
    830 	rumpuser_free(what);
    831 }
    832