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