1 /* $NetBSD: vm.c,v 1.69 2009/12/04 17:15:47 pooka Exp $ */
4 * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
6 * Development of this software was supported by Google Summer of Code.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
31 * Virtual memory emulation routines. Contents:
32 * + anon objects & pager
33 * + misc support routines
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.
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: vm.c,v 1.69 2009/12/04 17:15:47 pooka Exp $");
46 #include <sys/param.h>
47 #include <sys/atomic.h>
51 #include <sys/vnode.h>
54 #include <machine/pmap.h>
56 #include <rump/rumpuser.h>
59 #include <uvm/uvm_ddb.h>
60 #include <uvm/uvm_prot.h>
61 #include <uvm/uvm_readahead.h>
63 #include "rump_private.h"
65 static int ao_get(struct uvm_object
*, voff_t
, struct vm_page
**,
66 int *, int, vm_prot_t
, int, int);
67 static int ao_put(struct uvm_object
*, voff_t
, voff_t
, int);
69 const struct uvm_pagerops aobj_pager
= {
74 kmutex_t uvm_pageqlock
;
79 struct vmspace rump_vmspace
;
80 struct vm_map rump_vmmap
;
81 static struct vm_map_kernel kmem_map_store
;
82 struct vm_map
*kmem_map
= &kmem_map_store
.vmk_map
;
83 const struct rb_tree_ops uvm_page_tree_ops
;
85 static struct vm_map_kernel kernel_map_store
;
86 struct vm_map
*kernel_map
= &kernel_map_store
.vmk_map
;
92 /* called with the object locked */
94 rumpvm_makepage(struct uvm_object
*uobj
, voff_t off
)
98 pg
= kmem_zalloc(sizeof(struct vm_page
), KM_SLEEP
);
102 pg
->uanon
= (void *)kmem_zalloc(PAGE_SIZE
, KM_SLEEP
);
103 pg
->flags
= PG_CLEAN
|PG_BUSY
|PG_FAKE
;
105 TAILQ_INSERT_TAIL(&uobj
->memq
, pg
, listq
.queue
);
112 uvm_pagealloc_strat(struct uvm_object
*uobj
, voff_t off
, struct vm_anon
*anon
,
113 int flags
, int strat
, int free_list
)
116 return rumpvm_makepage(uobj
, off
);
122 * Called with the vm object locked.
125 uvm_pagefree(struct vm_page
*pg
)
127 struct uvm_object
*uobj
= pg
->uobject
;
129 if (pg
->flags
& PG_WANTED
)
133 TAILQ_REMOVE(&uobj
->memq
, pg
, listq
.queue
);
134 kmem_free((void *)pg
->uanon
, PAGE_SIZE
);
135 kmem_free(pg
, sizeof(*pg
));
139 uvm_pagezero(struct vm_page
*pg
)
142 pg
->flags
&= ~PG_CLEAN
;
143 memset((void *)pg
->uanon
, 0, PAGE_SIZE
);
151 ao_get(struct uvm_object
*uobj
, voff_t off
, struct vm_page
**pgs
,
152 int *npages
, int centeridx
, vm_prot_t access_type
,
153 int advice
, int flags
)
159 panic("%s: centeridx != 0 not supported", __func__
);
161 /* loop over pages */
162 off
= trunc_page(off
);
163 for (i
= 0; i
< *npages
; i
++) {
165 pg
= uvm_pagelookup(uobj
, off
+ (i
<< PAGE_SHIFT
));
167 if (pg
->flags
& PG_BUSY
) {
168 pg
->flags
|= PG_WANTED
;
169 UVM_UNLOCK_AND_WAIT(pg
, &uobj
->vmobjlock
, 0,
173 pg
->flags
|= PG_BUSY
;
176 pg
= rumpvm_makepage(uobj
, off
+ (i
<< PAGE_SHIFT
));
180 mutex_exit(&uobj
->vmobjlock
);
187 ao_put(struct uvm_object
*uobj
, voff_t start
, voff_t stop
, int flags
)
191 /* we only free all pages for now */
192 if ((flags
& PGO_FREE
) == 0 || (flags
& PGO_ALLPAGES
) == 0) {
193 mutex_exit(&uobj
->vmobjlock
);
197 while ((pg
= TAILQ_FIRST(&uobj
->memq
)) != NULL
)
199 mutex_exit(&uobj
->vmobjlock
);
205 uao_create(vsize_t size
, int flags
)
207 struct uvm_object
*uobj
;
209 uobj
= kmem_zalloc(sizeof(struct uvm_object
), KM_SLEEP
);
210 uobj
->pgops
= &aobj_pager
;
211 TAILQ_INIT(&uobj
->memq
);
212 mutex_init(&uobj
->vmobjlock
, MUTEX_DEFAULT
, IPL_NONE
);
218 uao_detach(struct uvm_object
*uobj
)
221 mutex_enter(&uobj
->vmobjlock
);
222 ao_put(uobj
, 0, 0, PGO_ALLPAGES
| PGO_FREE
);
223 mutex_destroy(&uobj
->vmobjlock
);
224 kmem_free(uobj
, sizeof(*uobj
));
231 static kmutex_t pagermtx
;
237 uvmexp
.free
= 1024*1024; /* XXX */
238 uvm
.pagedaemon_lwp
= NULL
; /* doesn't match curlwp */
239 rump_vmspace
.vm_map
.pmap
= pmap_kernel();
241 mutex_init(&pagermtx
, MUTEX_DEFAULT
, 0);
242 mutex_init(&uvm_pageqlock
, MUTEX_DEFAULT
, 0);
244 kernel_map
->pmap
= pmap_kernel();
245 callback_head_init(&kernel_map_store
.vmk_reclaim_callback
, IPL_VM
);
246 kmem_map
->pmap
= pmap_kernel();
247 callback_head_init(&kmem_map_store
.vmk_reclaim_callback
, IPL_VM
);
252 uvm_pagewire(struct vm_page
*pg
)
259 uvm_pageunwire(struct vm_page
*pg
)
266 * This satisfies the "disgusting mmap hack" used by proplib.
267 * We probably should grow some more assertables to make sure we're
268 * not satisfying anything we shouldn't be satisfying. At least we
269 * should make sure it's the local machine we're mmapping ...
272 uvm_mmap(struct vm_map
*map
, vaddr_t
*addr
, vsize_t size
, vm_prot_t prot
,
273 vm_prot_t maxprot
, int flags
, void *handle
, voff_t off
, vsize_t locklim
)
278 if (prot
!= (VM_PROT_READ
| VM_PROT_WRITE
))
279 panic("uvm_mmap() variant unsupported");
280 if (flags
!= (MAP_PRIVATE
| MAP_ANON
))
281 panic("uvm_mmap() variant unsupported");
282 /* no reason in particular, but cf. uvm_default_mapaddr() */
284 panic("uvm_mmap() variant unsupported");
286 uaddr
= rumpuser_anonmmap(size
, 0, 0, &error
);
290 *addr
= (vaddr_t
)uaddr
;
297 struct vm_page
**pgr_pgs
;
300 LIST_ENTRY(pagerinfo
) pgr_entries
;
302 static LIST_HEAD(, pagerinfo
) pagerlist
= LIST_HEAD_INITIALIZER(pagerlist
);
305 * Pager "map" in routine. Instead of mapping, we allocate memory
306 * and copy page contents there. Not optimal or even strictly
307 * correct (the caller might modify the page contents after mapping
308 * them in), but what the heck. Assumes UVMPAGER_MAPIN_WAITOK.
311 uvm_pagermapin(struct vm_page
**pgs
, int npages
, int flags
)
313 struct pagerinfo
*pgri
;
317 /* allocate structures */
318 pgri
= kmem_alloc(sizeof(*pgri
), KM_SLEEP
);
319 pgri
->pgr_kva
= (vaddr_t
)kmem_alloc(npages
* PAGE_SIZE
, KM_SLEEP
);
320 pgri
->pgr_npages
= npages
;
321 pgri
->pgr_pgs
= kmem_alloc(sizeof(struct vm_page
*) * npages
, KM_SLEEP
);
322 pgri
->pgr_read
= (flags
& UVMPAGER_MAPIN_READ
) != 0;
324 /* copy contents to "mapped" memory */
325 for (i
= 0, curkva
= pgri
->pgr_kva
;
327 i
++, curkva
+= PAGE_SIZE
) {
329 * We need to copy the previous contents of the pages to
330 * the window even if we are reading from the
331 * device, since the device might not fill the contents of
332 * the full mapped range and we will end up corrupting
333 * data when we unmap the window.
335 memcpy((void*)curkva
, pgs
[i
]->uanon
, PAGE_SIZE
);
336 pgri
->pgr_pgs
[i
] = pgs
[i
];
339 mutex_enter(&pagermtx
);
340 LIST_INSERT_HEAD(&pagerlist
, pgri
, pgr_entries
);
341 mutex_exit(&pagermtx
);
343 return pgri
->pgr_kva
;
347 * map out the pager window. return contents from VA to page storage
348 * and free structures.
350 * Note: does not currently support partial frees
353 uvm_pagermapout(vaddr_t kva
, int npages
)
355 struct pagerinfo
*pgri
;
359 mutex_enter(&pagermtx
);
360 LIST_FOREACH(pgri
, &pagerlist
, pgr_entries
) {
361 if (pgri
->pgr_kva
== kva
)
365 if (pgri
->pgr_npages
!= npages
)
366 panic("uvm_pagermapout: partial unmapping not supported");
367 LIST_REMOVE(pgri
, pgr_entries
);
368 mutex_exit(&pagermtx
);
370 if (pgri
->pgr_read
) {
371 for (i
= 0, curkva
= pgri
->pgr_kva
;
372 i
< pgri
->pgr_npages
;
373 i
++, curkva
+= PAGE_SIZE
) {
374 memcpy(pgri
->pgr_pgs
[i
]->uanon
,(void*)curkva
,PAGE_SIZE
);
378 kmem_free(pgri
->pgr_pgs
, npages
* sizeof(struct vm_page
*));
379 kmem_free((void*)pgri
->pgr_kva
, npages
* PAGE_SIZE
);
380 kmem_free(pgri
, sizeof(*pgri
));
384 * convert va in pager window to page structure.
385 * XXX: how expensive is this (global lock, list traversal)?
388 uvm_pageratop(vaddr_t va
)
390 struct pagerinfo
*pgri
;
391 struct vm_page
*pg
= NULL
;
394 mutex_enter(&pagermtx
);
395 LIST_FOREACH(pgri
, &pagerlist
, pgr_entries
) {
396 if (pgri
->pgr_kva
<= va
397 && va
< pgri
->pgr_kva
+ pgri
->pgr_npages
*PAGE_SIZE
)
401 i
= (va
- pgri
->pgr_kva
) >> PAGE_SHIFT
;
402 pg
= pgri
->pgr_pgs
[i
];
404 mutex_exit(&pagermtx
);
409 /* Called with the vm object locked */
411 uvm_pagelookup(struct uvm_object
*uobj
, voff_t off
)
415 TAILQ_FOREACH(pg
, &uobj
->memq
, listq
.queue
) {
416 if (pg
->offset
== off
) {
425 uvm_page_unbusy(struct vm_page
**pgs
, int npgs
)
430 for (i
= 0; i
< npgs
; i
++) {
435 KASSERT(pg
->flags
& PG_BUSY
);
436 if (pg
->flags
& PG_WANTED
)
438 if (pg
->flags
& PG_RELEASED
)
441 pg
->flags
&= ~(PG_WANTED
|PG_BUSY
);
446 uvm_estimatepageable(int *active
, int *inactive
)
449 /* XXX: guessing game */
454 struct vm_map_kernel
*
455 vm_map_to_kernel(struct vm_map
*map
)
458 return (struct vm_map_kernel
*)map
;
462 vm_map_starved_p(struct vm_map
*map
)
469 uvm_pageout_start(int npages
)
472 uvmexp
.paging
+= npages
;
476 uvm_pageout_done(int npages
)
479 uvmexp
.paging
-= npages
;
482 * wake up either of pagedaemon or LWPs waiting for it.
485 if (uvmexp
.free
<= uvmexp
.reserve_kernel
) {
486 wakeup(&uvm
.pagedaemon
);
488 wakeup(&uvmexp
.free
);
493 uvm_loan(struct vm_map
*map
, vaddr_t start
, vsize_t len
, void *v
, int flags
)
496 panic("%s: unimplemented", __func__
);
500 uvm_unloan(void *v
, int npages
, int flags
)
503 panic("%s: unimplemented", __func__
);
507 uvm_loanuobjpages(struct uvm_object
*uobj
, voff_t pgoff
, int orignpages
,
508 struct vm_page
**opp
)
511 panic("%s: unimplemented", __func__
);
515 uvm_object_printit(struct uvm_object
*uobj
, bool full
,
516 void (*pr
)(const char *, ...))
523 uvm_default_mapaddr(struct proc
*p
, vaddr_t base
, vsize_t sz
)
534 uvm_km_alloc(struct vm_map
*map
, vsize_t size
, vsize_t align
, uvm_flag_t flags
)
541 alignbit
= ffs(align
)-1;
544 rv
= rumpuser_anonmmap(size
, alignbit
, flags
& UVM_KMF_EXEC
, &error
);
546 if (flags
& (UVM_KMF_CANFAIL
| UVM_KMF_NOWAIT
))
549 panic("uvm_km_alloc failed");
552 if (flags
& UVM_KMF_ZERO
)
559 uvm_km_free(struct vm_map
*map
, vaddr_t vaddr
, vsize_t size
, uvm_flag_t flags
)
562 rumpuser_unmap((void *)vaddr
, size
);
566 uvm_km_suballoc(struct vm_map
*map
, vaddr_t
*minaddr
, vaddr_t
*maxaddr
,
567 vsize_t size
, int pageable
, bool fixed
, struct vm_map_kernel
*submap
)
570 return (struct vm_map
*)417416;
574 uvm_km_alloc_poolpage(struct vm_map
*map
, bool waitok
)
577 return (vaddr_t
)rumpuser_malloc(PAGE_SIZE
, !waitok
);
581 uvm_km_free_poolpage(struct vm_map
*map
, vaddr_t addr
)
584 rumpuser_unmap((void *)addr
, PAGE_SIZE
);
588 uvm_km_alloc_poolpage_cache(struct vm_map
*map
, bool waitok
)
593 rv
= rumpuser_anonmmap(PAGE_SIZE
, PAGE_SHIFT
, 0, &error
);
594 if (rv
== NULL
&& waitok
)
595 panic("fixme: poolpage alloc failed");
601 uvm_km_free_poolpage_cache(struct vm_map
*map
, vaddr_t vaddr
)
604 rumpuser_unmap((void *)vaddr
, PAGE_SIZE
);
608 * Mapping and vm space locking routines.
609 * XXX: these don't work for non-local vmspaces
612 uvm_vslock(struct vmspace
*vs
, void *addr
, size_t len
, vm_prot_t access
)
615 KASSERT(vs
== &rump_vmspace
);
620 uvm_vsunlock(struct vmspace
*vs
, void *addr
, size_t len
)
623 KASSERT(vs
== &rump_vmspace
);
627 vmapbuf(struct buf
*bp
, vsize_t len
)
630 bp
->b_saveaddr
= bp
->b_data
;
634 vunmapbuf(struct buf
*bp
, vsize_t len
)
637 bp
->b_data
= bp
->b_saveaddr
;
642 uvm_wait(const char *msg
)
645 /* nothing to wait for */
649 uvmspace_free(struct vmspace
*vm
)
652 /* nothing for now */
656 uvm_io(struct vm_map
*map
, struct uio
*uio
)
660 * just do direct uio for now. but this needs some vmspace
661 * olympics for rump_sysproxy.
663 return uiomove((void *)(vaddr_t
)uio
->uio_offset
, uio
->uio_resid
, uio
);
667 * page life cycle stuff. it really doesn't exist, so just stubs.
671 uvm_pageactivate(struct vm_page
*pg
)
678 uvm_pagedeactivate(struct vm_page
*pg
)
685 uvm_pagedequeue(struct vm_page
*pg
)
692 uvm_pageenqueue(struct vm_page
*pg
)