Home | History | Annotate | Line # | Download | only in rumpkern
vm.c revision 1.45
      1 /*	$NetBSD: vm.c,v 1.45 2008/11/27 08:13:15 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2007 Antti Kantee.  All Rights Reserved.
      5  *
      6  * Development of this software was supported by Google Summer of Code.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     20  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  */
     29 
     30 /*
     31  * Virtual memory emulation routines.  Contents:
     32  *  + anon objects & pager
     33  *  + misc support routines
     34  *  + kmem
     35  */
     36 
     37 /*
     38  * XXX: we abuse pg->uanon for the virtual address of the storage
     39  * for each page.  phys_addr would fit the job description better,
     40  * except that it will create unnecessary lossage on some platforms
     41  * due to not being a pointer type.
     42  */
     43 
     44 #include <sys/param.h>
     45 #include <sys/atomic.h>
     46 #include <sys/null.h>
     47 #include <sys/vnode.h>
     48 #include <sys/buf.h>
     49 #include <sys/kmem.h>
     50 
     51 #include <machine/pmap.h>
     52 
     53 #include <rump/rumpuser.h>
     54 
     55 #include <uvm/uvm.h>
     56 #include <uvm/uvm_prot.h>
     57 
     58 #include "rump_private.h"
     59 
     60 static int ao_get(struct uvm_object *, voff_t, struct vm_page **,
     61 	int *, int, vm_prot_t, int, int);
     62 static int ao_put(struct uvm_object *, voff_t, voff_t, int);
     63 
     64 const struct uvm_pagerops aobj_pager = {
     65 	.pgo_get = ao_get,
     66 	.pgo_put = ao_put,
     67 };
     68 
     69 kmutex_t uvm_pageqlock;
     70 
     71 struct uvmexp uvmexp;
     72 struct uvm uvm;
     73 
     74 struct vmspace rump_vmspace;
     75 struct vm_map rump_vmmap;
     76 const struct rb_tree_ops uvm_page_tree_ops;
     77 
     78 static struct vm_map_kernel kernel_map_store;
     79 struct vm_map *kernel_map = &kernel_map_store.vmk_map;
     80 
     81 /*
     82  * vm pages
     83  */
     84 
     85 /* called with the object locked */
     86 struct vm_page *
     87 rumpvm_makepage(struct uvm_object *uobj, voff_t off)
     88 {
     89 	struct vm_page *pg;
     90 
     91 	pg = kmem_zalloc(sizeof(struct vm_page), KM_SLEEP);
     92 	pg->offset = off;
     93 	pg->uobject = uobj;
     94 
     95 	pg->uanon = (void *)kmem_zalloc(PAGE_SIZE, KM_SLEEP);
     96 	pg->flags = PG_CLEAN|PG_BUSY|PG_FAKE;
     97 
     98 	TAILQ_INSERT_TAIL(&uobj->memq, pg, listq.queue);
     99 
    100 	return pg;
    101 }
    102 
    103 /*
    104  * Release a page.
    105  *
    106  * Called with the vm object locked.
    107  */
    108 void
    109 uvm_pagefree(struct vm_page *pg)
    110 {
    111 	struct uvm_object *uobj = pg->uobject;
    112 
    113 	if (pg->flags & PG_WANTED)
    114 		wakeup(pg);
    115 
    116 	TAILQ_REMOVE(&uobj->memq, pg, listq.queue);
    117 	kmem_free((void *)pg->uanon, PAGE_SIZE);
    118 	kmem_free(pg, sizeof(*pg));
    119 }
    120 
    121 struct rumpva {
    122 	vaddr_t addr;
    123 	struct vm_page *pg;
    124 
    125 	LIST_ENTRY(rumpva) entries;
    126 };
    127 static LIST_HEAD(, rumpva) rvahead = LIST_HEAD_INITIALIZER(rvahead);
    128 static kmutex_t rvamtx;
    129 
    130 void
    131 rumpvm_enterva(vaddr_t addr, struct vm_page *pg)
    132 {
    133 	struct rumpva *rva;
    134 
    135 	rva = kmem_alloc(sizeof(struct rumpva), KM_SLEEP);
    136 	rva->addr = addr;
    137 	rva->pg = pg;
    138 	mutex_enter(&rvamtx);
    139 	LIST_INSERT_HEAD(&rvahead, rva, entries);
    140 	mutex_exit(&rvamtx);
    141 }
    142 
    143 void
    144 rumpvm_flushva()
    145 {
    146 	struct rumpva *rva;
    147 
    148 	mutex_enter(&rvamtx);
    149 	while ((rva = LIST_FIRST(&rvahead)) != NULL) {
    150 		LIST_REMOVE(rva, entries);
    151 		kmem_free(rva, sizeof(*rva));
    152 	}
    153 	mutex_exit(&rvamtx);
    154 }
    155 
    156 /*
    157  * Anon object stuff
    158  */
    159 
    160 static int
    161 ao_get(struct uvm_object *uobj, voff_t off, struct vm_page **pgs,
    162 	int *npages, int centeridx, vm_prot_t access_type,
    163 	int advice, int flags)
    164 {
    165 	struct vm_page *pg;
    166 	int i;
    167 
    168 	if (centeridx)
    169 		panic("%s: centeridx != 0 not supported", __func__);
    170 
    171 	/* loop over pages */
    172 	off = trunc_page(off);
    173 	for (i = 0; i < *npages; i++) {
    174  retrylookup:
    175 		pg = uvm_pagelookup(uobj, off + (i << PAGE_SHIFT));
    176 		if (pg) {
    177 			if (pg->flags & PG_BUSY) {
    178 				pg->flags |= PG_WANTED;
    179 				UVM_UNLOCK_AND_WAIT(pg, &uobj->vmobjlock, 0,
    180 				    "aogetpg", 0);
    181 				goto retrylookup;
    182 			}
    183 			pg->flags |= PG_BUSY;
    184 			pgs[i] = pg;
    185 		} else {
    186 			pg = rumpvm_makepage(uobj, off + (i << PAGE_SHIFT));
    187 			pgs[i] = pg;
    188 		}
    189 	}
    190 	mutex_exit(&uobj->vmobjlock);
    191 
    192 	return 0;
    193 
    194 }
    195 
    196 static int
    197 ao_put(struct uvm_object *uobj, voff_t start, voff_t stop, int flags)
    198 {
    199 	struct vm_page *pg;
    200 
    201 	/* we only free all pages for now */
    202 	if ((flags & PGO_FREE) == 0 || (flags & PGO_ALLPAGES) == 0) {
    203 		mutex_exit(&uobj->vmobjlock);
    204 		return 0;
    205 	}
    206 
    207 	while ((pg = TAILQ_FIRST(&uobj->memq)) != NULL)
    208 		uvm_pagefree(pg);
    209 	mutex_exit(&uobj->vmobjlock);
    210 
    211 	return 0;
    212 }
    213 
    214 struct uvm_object *
    215 uao_create(vsize_t size, int flags)
    216 {
    217 	struct uvm_object *uobj;
    218 
    219 	uobj = kmem_zalloc(sizeof(struct uvm_object), KM_SLEEP);
    220 	uobj->pgops = &aobj_pager;
    221 	TAILQ_INIT(&uobj->memq);
    222 	mutex_init(&uobj->vmobjlock, MUTEX_DEFAULT, IPL_NONE);
    223 
    224 	return uobj;
    225 }
    226 
    227 void
    228 uao_detach(struct uvm_object *uobj)
    229 {
    230 
    231 	mutex_enter(&uobj->vmobjlock);
    232 	ao_put(uobj, 0, 0, PGO_ALLPAGES | PGO_FREE);
    233 	kmem_free(uobj, sizeof(*uobj));
    234 }
    235 
    236 /*
    237  * Misc routines
    238  */
    239 
    240 void
    241 rumpvm_init()
    242 {
    243 
    244 	uvmexp.free = 1024*1024; /* XXX */
    245 	uvm.pagedaemon_lwp = NULL; /* doesn't match curlwp */
    246 	rump_vmspace.vm_map.pmap = pmap_kernel();
    247 
    248 	mutex_init(&rvamtx, MUTEX_DEFAULT, 0);
    249 	mutex_init(&uvm_pageqlock, MUTEX_DEFAULT, 0);
    250 
    251 	callback_head_init(&kernel_map_store.vmk_reclaim_callback, IPL_VM);
    252 }
    253 
    254 void
    255 uvm_pageactivate(struct vm_page *pg)
    256 {
    257 
    258 	/* nada */
    259 }
    260 
    261 void
    262 uvm_pagewire(struct vm_page *pg)
    263 {
    264 
    265 	/* nada */
    266 }
    267 
    268 void
    269 uvm_pageunwire(struct vm_page *pg)
    270 {
    271 
    272 	/* nada */
    273 }
    274 
    275 vaddr_t
    276 uvm_pagermapin(struct vm_page **pps, int npages, int flags)
    277 {
    278 
    279 	panic("%s: unimplemented", __func__);
    280 }
    281 
    282 /* Called with the vm object locked */
    283 struct vm_page *
    284 uvm_pagelookup(struct uvm_object *uobj, voff_t off)
    285 {
    286 	struct vm_page *pg;
    287 
    288 	TAILQ_FOREACH(pg, &uobj->memq, listq.queue) {
    289 		if (pg->offset == off) {
    290 			return pg;
    291 		}
    292 	}
    293 
    294 	return NULL;
    295 }
    296 
    297 struct vm_page *
    298 uvm_pageratop(vaddr_t va)
    299 {
    300 	struct rumpva *rva;
    301 
    302 	mutex_enter(&rvamtx);
    303 	LIST_FOREACH(rva, &rvahead, entries)
    304 		if (rva->addr == va)
    305 			break;
    306 	mutex_exit(&rvamtx);
    307 
    308 	if (rva == NULL)
    309 		panic("%s: va %llu", __func__, (unsigned long long)va);
    310 
    311 	return rva->pg;
    312 }
    313 
    314 void
    315 uvm_page_unbusy(struct vm_page **pgs, int npgs)
    316 {
    317 	struct vm_page *pg;
    318 	int i;
    319 
    320 	for (i = 0; i < npgs; i++) {
    321 		pg = pgs[i];
    322 		if (pg == NULL)
    323 			continue;
    324 
    325 		KASSERT(pg->flags & PG_BUSY);
    326 		if (pg->flags & PG_WANTED)
    327 			wakeup(pg);
    328 		if (pg->flags & PG_RELEASED)
    329 			uvm_pagefree(pg);
    330 		else
    331 			pg->flags &= ~(PG_WANTED|PG_BUSY);
    332 	}
    333 }
    334 
    335 void
    336 uvm_estimatepageable(int *active, int *inactive)
    337 {
    338 
    339 	/* XXX: guessing game */
    340 	*active = 1024;
    341 	*inactive = 1024;
    342 }
    343 
    344 struct vm_map_kernel *
    345 vm_map_to_kernel(struct vm_map *map)
    346 {
    347 
    348 	return (struct vm_map_kernel *)map;
    349 }
    350 
    351 bool
    352 vm_map_starved_p(struct vm_map *map)
    353 {
    354 
    355 	return false;
    356 }
    357 
    358 void
    359 uvm_pageout_start(int npages)
    360 {
    361 
    362 	uvmexp.paging += npages;
    363 }
    364 
    365 void
    366 uvm_pageout_done(int npages)
    367 {
    368 
    369 	uvmexp.paging -= npages;
    370 
    371 	/*
    372 	 * wake up either of pagedaemon or LWPs waiting for it.
    373 	 */
    374 
    375 	if (uvmexp.free <= uvmexp.reserve_kernel) {
    376 		wakeup(&uvm.pagedaemon);
    377 	} else {
    378 		wakeup(&uvmexp.free);
    379 	}
    380 }
    381 
    382 /* XXX: following two are unfinished because lwp's are not refcounted yet */
    383 void
    384 uvm_lwp_hold(struct lwp *l)
    385 {
    386 
    387 	atomic_inc_uint(&l->l_holdcnt);
    388 }
    389 
    390 void
    391 uvm_lwp_rele(struct lwp *l)
    392 {
    393 
    394 	atomic_dec_uint(&l->l_holdcnt);
    395 }
    396 
    397 int
    398 uvm_loan(struct vm_map *map, vaddr_t start, vsize_t len, void *v, int flags)
    399 {
    400 
    401 	panic("%s: unimplemented", __func__);
    402 }
    403 
    404 void
    405 uvm_unloan(void *v, int npages, int flags)
    406 {
    407 
    408 	panic("%s: unimplemented", __func__);
    409 }
    410 
    411 int
    412 uvm_loanuobjpages(struct uvm_object *uobj, voff_t pgoff, int orignpages,
    413 	struct vm_page **opp)
    414 {
    415 
    416 	panic("%s: unimplemented", __func__);
    417 }
    418 
    419 /*
    420  * Kmem
    421  */
    422 
    423 #ifndef RUMP_USE_REAL_KMEM
    424 void *
    425 kmem_alloc(size_t size, km_flag_t kmflag)
    426 {
    427 
    428 	return rumpuser_malloc(size, kmflag == KM_NOSLEEP);
    429 }
    430 
    431 void *
    432 kmem_zalloc(size_t size, km_flag_t kmflag)
    433 {
    434 	void *rv;
    435 
    436 	rv = kmem_alloc(size, kmflag);
    437 	if (rv)
    438 		memset(rv, 0, size);
    439 
    440 	return rv;
    441 }
    442 
    443 void
    444 kmem_free(void *p, size_t size)
    445 {
    446 
    447 	rumpuser_free(p);
    448 }
    449 #endif /* RUMP_USE_REAL_KMEM */
    450 
    451 /*
    452  * UVM km
    453  */
    454 
    455 vaddr_t
    456 uvm_km_alloc(struct vm_map *map, vsize_t size, vsize_t align, uvm_flag_t flags)
    457 {
    458 	void *rv;
    459 
    460 	rv = rumpuser_malloc(size, flags & (UVM_KMF_CANFAIL | UVM_KMF_NOWAIT));
    461 	if (rv && flags & UVM_KMF_ZERO)
    462 		memset(rv, 0, size);
    463 
    464 	return (vaddr_t)rv;
    465 }
    466 
    467 void
    468 uvm_km_free(struct vm_map *map, vaddr_t vaddr, vsize_t size, uvm_flag_t flags)
    469 {
    470 
    471 	rumpuser_free((void *)vaddr);
    472 }
    473 
    474 struct vm_map *
    475 uvm_km_suballoc(struct vm_map *map, vaddr_t *minaddr, vaddr_t *maxaddr,
    476 	vsize_t size, int pageable, bool fixed, struct vm_map_kernel *submap)
    477 {
    478 
    479 	return (struct vm_map *)417416;
    480 }
    481 
    482 vaddr_t
    483 uvm_km_alloc_poolpage(struct vm_map *map, bool waitok)
    484 {
    485 
    486 	return (vaddr_t)rumpuser_malloc(PAGE_SIZE, !waitok);
    487 }
    488 
    489 void
    490 uvm_km_free_poolpage(struct vm_map *map, vaddr_t addr)
    491 {
    492 
    493 	rumpuser_free((void *)addr);
    494 }
    495