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