Home | History | Annotate | Line # | Download | only in rumpkern
vm.c revision 1.30.4.1
      1 /*	$NetBSD: vm.c,v 1.30.4.1 2009/05/04 08:14:30 yamt 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/cdefs.h>
     45 __KERNEL_RCSID(0, "$NetBSD: vm.c,v 1.30.4.1 2009/05/04 08:14:30 yamt Exp $");
     46 
     47 #include <sys/param.h>
     48 #include <sys/atomic.h>
     49 #include <sys/null.h>
     50 #include <sys/vnode.h>
     51 #include <sys/buf.h>
     52 #include <sys/kmem.h>
     53 
     54 #include <machine/pmap.h>
     55 
     56 #include <rump/rumpuser.h>
     57 
     58 #include <uvm/uvm.h>
     59 #include <uvm/uvm_ddb.h>
     60 #include <uvm/uvm_prot.h>
     61 
     62 #include "rump_private.h"
     63 
     64 static int ao_get(struct uvm_object *, voff_t, struct vm_page **,
     65 	int *, int, vm_prot_t, int, int);
     66 static int ao_put(struct uvm_object *, voff_t, voff_t, int);
     67 
     68 const struct uvm_pagerops aobj_pager = {
     69 	.pgo_get = ao_get,
     70 	.pgo_put = ao_put,
     71 };
     72 
     73 kmutex_t uvm_pageqlock;
     74 
     75 struct uvmexp uvmexp;
     76 struct uvm uvm;
     77 
     78 struct vmspace rump_vmspace;
     79 struct vm_map rump_vmmap;
     80 static struct vm_map_kernel kmem_map_store;
     81 struct vm_map *kmem_map = &kmem_map_store.vmk_map;
     82 const struct rb_tree_ops uvm_page_tree_ops;
     83 
     84 static struct vm_map_kernel kernel_map_store;
     85 struct vm_map *kernel_map = &kernel_map_store.vmk_map;
     86 
     87 /*
     88  * vm pages
     89  */
     90 
     91 /* called with the object locked */
     92 struct vm_page *
     93 rumpvm_makepage(struct uvm_object *uobj, voff_t off)
     94 {
     95 	struct vm_page *pg;
     96 
     97 	pg = kmem_zalloc(sizeof(struct vm_page), KM_SLEEP);
     98 	pg->offset = off;
     99 	pg->uobject = uobj;
    100 
    101 	pg->uanon = (void *)kmem_zalloc(PAGE_SIZE, KM_SLEEP);
    102 	pg->flags = PG_CLEAN|PG_BUSY|PG_FAKE;
    103 
    104 	TAILQ_INSERT_TAIL(&uobj->memq, pg, listq.queue);
    105 
    106 	return pg;
    107 }
    108 
    109 /*
    110  * Release a page.
    111  *
    112  * Called with the vm object locked.
    113  */
    114 void
    115 uvm_pagefree(struct vm_page *pg)
    116 {
    117 	struct uvm_object *uobj = pg->uobject;
    118 
    119 	if (pg->flags & PG_WANTED)
    120 		wakeup(pg);
    121 
    122 	TAILQ_REMOVE(&uobj->memq, pg, listq.queue);
    123 	kmem_free((void *)pg->uanon, PAGE_SIZE);
    124 	kmem_free(pg, sizeof(*pg));
    125 }
    126 
    127 struct rumpva {
    128 	vaddr_t addr;
    129 	struct vm_page *pg;
    130 
    131 	LIST_ENTRY(rumpva) entries;
    132 };
    133 static LIST_HEAD(, rumpva) rvahead = LIST_HEAD_INITIALIZER(rvahead);
    134 static kmutex_t rvamtx;
    135 
    136 void
    137 rumpvm_enterva(vaddr_t addr, struct vm_page *pg)
    138 {
    139 	struct rumpva *rva;
    140 
    141 	rva = kmem_alloc(sizeof(struct rumpva), KM_SLEEP);
    142 	rva->addr = addr;
    143 	rva->pg = pg;
    144 	mutex_enter(&rvamtx);
    145 	LIST_INSERT_HEAD(&rvahead, rva, entries);
    146 	mutex_exit(&rvamtx);
    147 }
    148 
    149 void
    150 rumpvm_flushva(struct uvm_object *uobj)
    151 {
    152 	struct rumpva *rva, *rva_next;
    153 
    154 	mutex_enter(&rvamtx);
    155 	for (rva = LIST_FIRST(&rvahead); rva; rva = rva_next) {
    156 		rva_next = LIST_NEXT(rva, entries);
    157 		if (rva->pg->uobject == uobj) {
    158 			LIST_REMOVE(rva, entries);
    159 			uvm_page_unbusy(&rva->pg, 1);
    160 			kmem_free(rva, sizeof(*rva));
    161 		}
    162 	}
    163 	mutex_exit(&rvamtx);
    164 }
    165 
    166 /*
    167  * Anon object stuff
    168  */
    169 
    170 static int
    171 ao_get(struct uvm_object *uobj, voff_t off, struct vm_page **pgs,
    172 	int *npages, int centeridx, vm_prot_t access_type,
    173 	int advice, int flags)
    174 {
    175 	struct vm_page *pg;
    176 	int i;
    177 
    178 	if (centeridx)
    179 		panic("%s: centeridx != 0 not supported", __func__);
    180 
    181 	/* loop over pages */
    182 	off = trunc_page(off);
    183 	for (i = 0; i < *npages; i++) {
    184  retrylookup:
    185 		pg = uvm_pagelookup(uobj, off + (i << PAGE_SHIFT));
    186 		if (pg) {
    187 			if (pg->flags & PG_BUSY) {
    188 				pg->flags |= PG_WANTED;
    189 				UVM_UNLOCK_AND_WAIT(pg, &uobj->vmobjlock, 0,
    190 				    "aogetpg", 0);
    191 				goto retrylookup;
    192 			}
    193 			pg->flags |= PG_BUSY;
    194 			pgs[i] = pg;
    195 		} else {
    196 			pg = rumpvm_makepage(uobj, off + (i << PAGE_SHIFT));
    197 			pgs[i] = pg;
    198 		}
    199 	}
    200 	mutex_exit(&uobj->vmobjlock);
    201 
    202 	return 0;
    203 
    204 }
    205 
    206 static int
    207 ao_put(struct uvm_object *uobj, voff_t start, voff_t stop, int flags)
    208 {
    209 	struct vm_page *pg;
    210 
    211 	/* we only free all pages for now */
    212 	if ((flags & PGO_FREE) == 0 || (flags & PGO_ALLPAGES) == 0) {
    213 		mutex_exit(&uobj->vmobjlock);
    214 		return 0;
    215 	}
    216 
    217 	while ((pg = TAILQ_FIRST(&uobj->memq)) != NULL)
    218 		uvm_pagefree(pg);
    219 	mutex_exit(&uobj->vmobjlock);
    220 
    221 	return 0;
    222 }
    223 
    224 struct uvm_object *
    225 uao_create(vsize_t size, int flags)
    226 {
    227 	struct uvm_object *uobj;
    228 
    229 	uobj = kmem_zalloc(sizeof(struct uvm_object), KM_SLEEP);
    230 	uobj->pgops = &aobj_pager;
    231 	TAILQ_INIT(&uobj->memq);
    232 	mutex_init(&uobj->vmobjlock, MUTEX_DEFAULT, IPL_NONE);
    233 
    234 	return uobj;
    235 }
    236 
    237 void
    238 uao_detach(struct uvm_object *uobj)
    239 {
    240 
    241 	mutex_enter(&uobj->vmobjlock);
    242 	ao_put(uobj, 0, 0, PGO_ALLPAGES | PGO_FREE);
    243 	mutex_destroy(&uobj->vmobjlock);
    244 	kmem_free(uobj, sizeof(*uobj));
    245 }
    246 
    247 /*
    248  * Misc routines
    249  */
    250 
    251 static kmutex_t cachepgmtx;
    252 
    253 void
    254 rumpvm_init(void)
    255 {
    256 
    257 	uvmexp.free = 1024*1024; /* XXX */
    258 	uvm.pagedaemon_lwp = NULL; /* doesn't match curlwp */
    259 	rump_vmspace.vm_map.pmap = pmap_kernel();
    260 
    261 	mutex_init(&rvamtx, MUTEX_DEFAULT, 0);
    262 	mutex_init(&uvm_pageqlock, MUTEX_DEFAULT, 0);
    263 	mutex_init(&cachepgmtx, MUTEX_DEFAULT, 0);
    264 
    265 	kernel_map->pmap = pmap_kernel();
    266 	callback_head_init(&kernel_map_store.vmk_reclaim_callback, IPL_VM);
    267 	kmem_map->pmap = pmap_kernel();
    268 	callback_head_init(&kmem_map_store.vmk_reclaim_callback, IPL_VM);
    269 }
    270 
    271 void
    272 uvm_pageactivate(struct vm_page *pg)
    273 {
    274 
    275 	/* nada */
    276 }
    277 
    278 void
    279 uvm_pagewire(struct vm_page *pg)
    280 {
    281 
    282 	/* nada */
    283 }
    284 
    285 void
    286 uvm_pageunwire(struct vm_page *pg)
    287 {
    288 
    289 	/* nada */
    290 }
    291 
    292 int
    293 uvm_mmap(struct vm_map *map, vaddr_t *addr, vsize_t size, vm_prot_t prot,
    294 	vm_prot_t maxprot, int flags, void *handle, voff_t off, vsize_t locklim)
    295 {
    296 
    297 	panic("%s: unimplemented", __func__);
    298 }
    299 
    300 vaddr_t
    301 uvm_pagermapin(struct vm_page **pps, int npages, int flags)
    302 {
    303 
    304 	panic("%s: unimplemented", __func__);
    305 }
    306 
    307 /* Called with the vm object locked */
    308 struct vm_page *
    309 uvm_pagelookup(struct uvm_object *uobj, voff_t off)
    310 {
    311 	struct vm_page *pg;
    312 
    313 	TAILQ_FOREACH(pg, &uobj->memq, listq.queue) {
    314 		if (pg->offset == off) {
    315 			return pg;
    316 		}
    317 	}
    318 
    319 	return NULL;
    320 }
    321 
    322 struct vm_page *
    323 uvm_pageratop(vaddr_t va)
    324 {
    325 	struct rumpva *rva;
    326 
    327 	mutex_enter(&rvamtx);
    328 	LIST_FOREACH(rva, &rvahead, entries)
    329 		if (rva->addr == va)
    330 			break;
    331 	mutex_exit(&rvamtx);
    332 
    333 	if (rva == NULL)
    334 		panic("%s: va %llu", __func__, (unsigned long long)va);
    335 
    336 	return rva->pg;
    337 }
    338 
    339 void
    340 uvm_page_unbusy(struct vm_page **pgs, int npgs)
    341 {
    342 	struct vm_page *pg;
    343 	int i;
    344 
    345 	for (i = 0; i < npgs; i++) {
    346 		pg = pgs[i];
    347 		if (pg == NULL)
    348 			continue;
    349 
    350 		KASSERT(pg->flags & PG_BUSY);
    351 		if (pg->flags & PG_WANTED)
    352 			wakeup(pg);
    353 		if (pg->flags & PG_RELEASED)
    354 			uvm_pagefree(pg);
    355 		else
    356 			pg->flags &= ~(PG_WANTED|PG_BUSY);
    357 	}
    358 }
    359 
    360 void
    361 uvm_estimatepageable(int *active, int *inactive)
    362 {
    363 
    364 	/* XXX: guessing game */
    365 	*active = 1024;
    366 	*inactive = 1024;
    367 }
    368 
    369 struct vm_map_kernel *
    370 vm_map_to_kernel(struct vm_map *map)
    371 {
    372 
    373 	return (struct vm_map_kernel *)map;
    374 }
    375 
    376 bool
    377 vm_map_starved_p(struct vm_map *map)
    378 {
    379 
    380 	return false;
    381 }
    382 
    383 void
    384 uvm_pageout_start(int npages)
    385 {
    386 
    387 	uvmexp.paging += npages;
    388 }
    389 
    390 void
    391 uvm_pageout_done(int npages)
    392 {
    393 
    394 	uvmexp.paging -= npages;
    395 
    396 	/*
    397 	 * wake up either of pagedaemon or LWPs waiting for it.
    398 	 */
    399 
    400 	if (uvmexp.free <= uvmexp.reserve_kernel) {
    401 		wakeup(&uvm.pagedaemon);
    402 	} else {
    403 		wakeup(&uvmexp.free);
    404 	}
    405 }
    406 
    407 /* XXX: following two are unfinished because lwp's are not refcounted yet */
    408 void
    409 uvm_lwp_hold(struct lwp *l)
    410 {
    411 
    412 	atomic_inc_uint(&l->l_holdcnt);
    413 }
    414 
    415 void
    416 uvm_lwp_rele(struct lwp *l)
    417 {
    418 
    419 	atomic_dec_uint(&l->l_holdcnt);
    420 }
    421 
    422 int
    423 uvm_loan(struct vm_map *map, vaddr_t start, vsize_t len, void *v, int flags)
    424 {
    425 
    426 	panic("%s: unimplemented", __func__);
    427 }
    428 
    429 void
    430 uvm_unloan(void *v, int npages, int flags)
    431 {
    432 
    433 	panic("%s: unimplemented", __func__);
    434 }
    435 
    436 int
    437 uvm_loanuobjpages(struct uvm_object *uobj, voff_t pgoff, int orignpages,
    438 	struct vm_page **opp)
    439 {
    440 
    441 	panic("%s: unimplemented", __func__);
    442 }
    443 
    444 void
    445 uvm_object_printit(struct uvm_object *uobj, bool full,
    446 	void (*pr)(const char *, ...))
    447 {
    448 
    449 	/* nada for now */
    450 }
    451 
    452 /*
    453  * Kmem
    454  */
    455 
    456 #ifndef RUMP_USE_REAL_ALLOCATORS
    457 void
    458 kmem_init()
    459 {
    460 
    461 	/* nothing to do */
    462 }
    463 
    464 void *
    465 kmem_alloc(size_t size, km_flag_t kmflag)
    466 {
    467 
    468 	return rumpuser_malloc(size, kmflag == KM_NOSLEEP);
    469 }
    470 
    471 void *
    472 kmem_zalloc(size_t size, km_flag_t kmflag)
    473 {
    474 	void *rv;
    475 
    476 	rv = kmem_alloc(size, kmflag);
    477 	if (rv)
    478 		memset(rv, 0, size);
    479 
    480 	return rv;
    481 }
    482 
    483 void
    484 kmem_free(void *p, size_t size)
    485 {
    486 
    487 	rumpuser_free(p);
    488 }
    489 #endif /* RUMP_USE_REAL_ALLOCATORS */
    490 
    491 /*
    492  * UVM km
    493  */
    494 
    495 vaddr_t
    496 uvm_km_alloc(struct vm_map *map, vsize_t size, vsize_t align, uvm_flag_t flags)
    497 {
    498 	void *rv;
    499 	int alignbit, error;
    500 
    501 	alignbit = 0;
    502 	if (align) {
    503 		alignbit = ffs(align)-1;
    504 	}
    505 
    506 	rv = rumpuser_anonmmap(size, alignbit, flags & UVM_KMF_EXEC, &error);
    507 	if (rv == NULL) {
    508 		if (flags & (UVM_KMF_CANFAIL | UVM_KMF_NOWAIT))
    509 			return 0;
    510 		else
    511 			panic("uvm_km_alloc failed");
    512 	}
    513 
    514 	if (flags & UVM_KMF_ZERO)
    515 		memset(rv, 0, size);
    516 
    517 	return (vaddr_t)rv;
    518 }
    519 
    520 void
    521 uvm_km_free(struct vm_map *map, vaddr_t vaddr, vsize_t size, uvm_flag_t flags)
    522 {
    523 
    524 	rumpuser_unmap((void *)vaddr, size);
    525 }
    526 
    527 struct vm_map *
    528 uvm_km_suballoc(struct vm_map *map, vaddr_t *minaddr, vaddr_t *maxaddr,
    529 	vsize_t size, int pageable, bool fixed, struct vm_map_kernel *submap)
    530 {
    531 
    532 	return (struct vm_map *)417416;
    533 }
    534 
    535 vaddr_t
    536 uvm_km_alloc_poolpage(struct vm_map *map, bool waitok)
    537 {
    538 
    539 	return (vaddr_t)rumpuser_malloc(PAGE_SIZE, !waitok);
    540 }
    541 
    542 void
    543 uvm_km_free_poolpage(struct vm_map *map, vaddr_t addr)
    544 {
    545 
    546 	rumpuser_unmap((void *)addr, PAGE_SIZE);
    547 }
    548 
    549 vaddr_t
    550 uvm_km_alloc_poolpage_cache(struct vm_map *map, bool waitok)
    551 {
    552 	void *rv;
    553 	int error;
    554 
    555 	rv = rumpuser_anonmmap(PAGE_SIZE, PAGE_SHIFT, 0, &error);
    556 	if (rv == NULL && waitok)
    557 		panic("fixme: poolpage alloc failed");
    558 
    559 	return (vaddr_t)rv;
    560 }
    561 
    562 void
    563 uvm_km_free_poolpage_cache(struct vm_map *map, vaddr_t vaddr)
    564 {
    565 
    566 	rumpuser_unmap((void *)vaddr, PAGE_SIZE);
    567 }
    568