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