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