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