vm.c revision 1.83 1 /* $NetBSD: vm.c,v 1.83 2010/06/10 21:40:42 pooka 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. Contents:
34 * + anon objects & pager
35 * + misc support routines
36 */
37
38 /*
39 * XXX: we abuse pg->uanon for the virtual address of the storage
40 * for each page. phys_addr would fit the job description better,
41 * except that it will create unnecessary lossage on some platforms
42 * due to not being a pointer type.
43 */
44
45 #include <sys/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: vm.c,v 1.83 2010/06/10 21:40:42 pooka Exp $");
47
48 #include <sys/param.h>
49 #include <sys/atomic.h>
50 #include <sys/buf.h>
51 #include <sys/kernel.h>
52 #include <sys/kmem.h>
53 #include <sys/mman.h>
54 #include <sys/null.h>
55 #include <sys/vnode.h>
56
57 #include <machine/pmap.h>
58
59 #include <rump/rumpuser.h>
60
61 #include <uvm/uvm.h>
62 #include <uvm/uvm_ddb.h>
63 #include <uvm/uvm_prot.h>
64 #include <uvm/uvm_readahead.h>
65
66 #include "rump_private.h"
67
68 static int ao_get(struct uvm_object *, voff_t, struct vm_page **,
69 int *, int, vm_prot_t, int, int);
70 static int ao_put(struct uvm_object *, voff_t, voff_t, int);
71
72 const struct uvm_pagerops aobj_pager = {
73 .pgo_get = ao_get,
74 .pgo_put = ao_put,
75 };
76
77 kmutex_t uvm_pageqlock;
78
79 struct uvmexp uvmexp;
80 struct uvm uvm;
81
82 struct vm_map rump_vmmap;
83 static struct vm_map_kernel kmem_map_store;
84 struct vm_map *kmem_map = &kmem_map_store.vmk_map;
85 const struct rb_tree_ops uvm_page_tree_ops;
86
87 static struct vm_map_kernel kernel_map_store;
88 struct vm_map *kernel_map = &kernel_map_store.vmk_map;
89
90 static unsigned int pdaemon_waiters;
91 static kmutex_t pdaemonmtx;
92 static kcondvar_t pdaemoncv, oomwait;
93
94 /*
95 * vm pages
96 */
97
98 /* called with the object locked */
99 struct vm_page *
100 uvm_pagealloc_strat(struct uvm_object *uobj, voff_t off, struct vm_anon *anon,
101 int flags, int strat, int free_list)
102 {
103 struct vm_page *pg;
104
105 pg = kmem_zalloc(sizeof(struct vm_page), KM_SLEEP);
106 pg->offset = off;
107 pg->uobject = uobj;
108
109 pg->uanon = (void *)kmem_alloc(PAGE_SIZE, KM_SLEEP);
110 if (flags & UVM_PGA_ZERO)
111 memset(pg->uanon, 0, PAGE_SIZE);
112 pg->flags = PG_CLEAN|PG_BUSY|PG_FAKE;
113
114 TAILQ_INSERT_TAIL(&uobj->memq, pg, listq.queue);
115 uobj->uo_npages++;
116
117 return pg;
118 }
119
120 /*
121 * Release a page.
122 *
123 * Called with the vm object locked.
124 */
125 void
126 uvm_pagefree(struct vm_page *pg)
127 {
128 struct uvm_object *uobj = pg->uobject;
129
130 if (pg->flags & PG_WANTED)
131 wakeup(pg);
132
133 uobj->uo_npages--;
134 TAILQ_REMOVE(&uobj->memq, pg, listq.queue);
135 kmem_free((void *)pg->uanon, PAGE_SIZE);
136 kmem_free(pg, sizeof(*pg));
137 }
138
139 void
140 uvm_pagezero(struct vm_page *pg)
141 {
142
143 pg->flags &= ~PG_CLEAN;
144 memset((void *)pg->uanon, 0, PAGE_SIZE);
145 }
146
147 /*
148 * Anon object stuff
149 */
150
151 static int
152 ao_get(struct uvm_object *uobj, voff_t off, struct vm_page **pgs,
153 int *npages, int centeridx, vm_prot_t access_type,
154 int advice, int flags)
155 {
156 struct vm_page *pg;
157 int i;
158
159 if (centeridx)
160 panic("%s: centeridx != 0 not supported", __func__);
161
162 /* loop over pages */
163 off = trunc_page(off);
164 for (i = 0; i < *npages; i++) {
165 retrylookup:
166 pg = uvm_pagelookup(uobj, off + (i << PAGE_SHIFT));
167 if (pg) {
168 if (pg->flags & PG_BUSY) {
169 pg->flags |= PG_WANTED;
170 UVM_UNLOCK_AND_WAIT(pg, &uobj->vmobjlock, 0,
171 "aogetpg", 0);
172 goto retrylookup;
173 }
174 pg->flags |= PG_BUSY;
175 pgs[i] = pg;
176 } else {
177 pg = uvm_pagealloc(uobj,
178 off + (i << PAGE_SHIFT), NULL, UVM_PGA_ZERO);
179 pgs[i] = pg;
180 }
181 }
182 mutex_exit(&uobj->vmobjlock);
183
184 return 0;
185
186 }
187
188 static int
189 ao_put(struct uvm_object *uobj, voff_t start, voff_t stop, int flags)
190 {
191 struct vm_page *pg;
192
193 /* we only free all pages for now */
194 if ((flags & PGO_FREE) == 0 || (flags & PGO_ALLPAGES) == 0) {
195 mutex_exit(&uobj->vmobjlock);
196 return 0;
197 }
198
199 while ((pg = TAILQ_FIRST(&uobj->memq)) != NULL)
200 uvm_pagefree(pg);
201 mutex_exit(&uobj->vmobjlock);
202
203 return 0;
204 }
205
206 struct uvm_object *
207 uao_create(vsize_t size, int flags)
208 {
209 struct uvm_object *uobj;
210
211 uobj = kmem_zalloc(sizeof(struct uvm_object), KM_SLEEP);
212 uobj->pgops = &aobj_pager;
213 TAILQ_INIT(&uobj->memq);
214 mutex_init(&uobj->vmobjlock, MUTEX_DEFAULT, IPL_NONE);
215
216 return uobj;
217 }
218
219 void
220 uao_detach(struct uvm_object *uobj)
221 {
222
223 mutex_enter(&uobj->vmobjlock);
224 ao_put(uobj, 0, 0, PGO_ALLPAGES | PGO_FREE);
225 mutex_destroy(&uobj->vmobjlock);
226 kmem_free(uobj, sizeof(*uobj));
227 }
228
229 /*
230 * Misc routines
231 */
232
233 static kmutex_t pagermtx;
234
235 void
236 uvm_init(void)
237 {
238
239 uvmexp.free = 1024*1024; /* XXX */
240
241 mutex_init(&pagermtx, MUTEX_DEFAULT, 0);
242 mutex_init(&uvm_pageqlock, MUTEX_DEFAULT, 0);
243
244 mutex_init(&pdaemonmtx, MUTEX_DEFAULT, 0);
245 cv_init(&pdaemoncv, "pdaemon");
246 cv_init(&oomwait, "oomwait");
247
248 kernel_map->pmap = pmap_kernel();
249 callback_head_init(&kernel_map_store.vmk_reclaim_callback, IPL_VM);
250 kmem_map->pmap = pmap_kernel();
251 callback_head_init(&kmem_map_store.vmk_reclaim_callback, IPL_VM);
252 }
253
254 void
255 uvmspace_init(struct vmspace *vm, struct pmap *pmap, vaddr_t vmin, vaddr_t vmax)
256 {
257
258 vm->vm_map.pmap = pmap_kernel();
259 vm->vm_refcnt = 1;
260 }
261
262 void
263 uvm_pagewire(struct vm_page *pg)
264 {
265
266 /* nada */
267 }
268
269 void
270 uvm_pageunwire(struct vm_page *pg)
271 {
272
273 /* nada */
274 }
275
276 /* where's your schmonz now? */
277 #define PUNLIMIT(a) \
278 p->p_rlimit[a].rlim_cur = p->p_rlimit[a].rlim_max = RLIM_INFINITY;
279 void
280 uvm_init_limits(struct proc *p)
281 {
282
283 PUNLIMIT(RLIMIT_STACK);
284 PUNLIMIT(RLIMIT_DATA);
285 PUNLIMIT(RLIMIT_RSS);
286 PUNLIMIT(RLIMIT_AS);
287 /* nice, cascade */
288 }
289 #undef PUNLIMIT
290
291 /*
292 * This satisfies the "disgusting mmap hack" used by proplib.
293 * We probably should grow some more assertables to make sure we're
294 * not satisfying anything we shouldn't be satisfying. At least we
295 * should make sure it's the local machine we're mmapping ...
296 */
297 int
298 uvm_mmap(struct vm_map *map, vaddr_t *addr, vsize_t size, vm_prot_t prot,
299 vm_prot_t maxprot, int flags, void *handle, voff_t off, vsize_t locklim)
300 {
301 void *uaddr;
302 int error;
303
304 if (prot != (VM_PROT_READ | VM_PROT_WRITE))
305 panic("uvm_mmap() variant unsupported");
306 if (flags != (MAP_PRIVATE | MAP_ANON))
307 panic("uvm_mmap() variant unsupported");
308 /* no reason in particular, but cf. uvm_default_mapaddr() */
309 if (*addr != 0)
310 panic("uvm_mmap() variant unsupported");
311
312 uaddr = rumpuser_anonmmap(NULL, size, 0, 0, &error);
313 if (uaddr == NULL)
314 return error;
315
316 *addr = (vaddr_t)uaddr;
317 return 0;
318 }
319
320 struct pagerinfo {
321 vaddr_t pgr_kva;
322 int pgr_npages;
323 struct vm_page **pgr_pgs;
324 bool pgr_read;
325
326 LIST_ENTRY(pagerinfo) pgr_entries;
327 };
328 static LIST_HEAD(, pagerinfo) pagerlist = LIST_HEAD_INITIALIZER(pagerlist);
329
330 /*
331 * Pager "map" in routine. Instead of mapping, we allocate memory
332 * and copy page contents there. Not optimal or even strictly
333 * correct (the caller might modify the page contents after mapping
334 * them in), but what the heck. Assumes UVMPAGER_MAPIN_WAITOK.
335 */
336 vaddr_t
337 uvm_pagermapin(struct vm_page **pgs, int npages, int flags)
338 {
339 struct pagerinfo *pgri;
340 vaddr_t curkva;
341 int i;
342
343 /* allocate structures */
344 pgri = kmem_alloc(sizeof(*pgri), KM_SLEEP);
345 pgri->pgr_kva = (vaddr_t)kmem_alloc(npages * PAGE_SIZE, KM_SLEEP);
346 pgri->pgr_npages = npages;
347 pgri->pgr_pgs = kmem_alloc(sizeof(struct vm_page *) * npages, KM_SLEEP);
348 pgri->pgr_read = (flags & UVMPAGER_MAPIN_READ) != 0;
349
350 /* copy contents to "mapped" memory */
351 for (i = 0, curkva = pgri->pgr_kva;
352 i < npages;
353 i++, curkva += PAGE_SIZE) {
354 /*
355 * We need to copy the previous contents of the pages to
356 * the window even if we are reading from the
357 * device, since the device might not fill the contents of
358 * the full mapped range and we will end up corrupting
359 * data when we unmap the window.
360 */
361 memcpy((void*)curkva, pgs[i]->uanon, PAGE_SIZE);
362 pgri->pgr_pgs[i] = pgs[i];
363 }
364
365 mutex_enter(&pagermtx);
366 LIST_INSERT_HEAD(&pagerlist, pgri, pgr_entries);
367 mutex_exit(&pagermtx);
368
369 return pgri->pgr_kva;
370 }
371
372 /*
373 * map out the pager window. return contents from VA to page storage
374 * and free structures.
375 *
376 * Note: does not currently support partial frees
377 */
378 void
379 uvm_pagermapout(vaddr_t kva, int npages)
380 {
381 struct pagerinfo *pgri;
382 vaddr_t curkva;
383 int i;
384
385 mutex_enter(&pagermtx);
386 LIST_FOREACH(pgri, &pagerlist, pgr_entries) {
387 if (pgri->pgr_kva == kva)
388 break;
389 }
390 KASSERT(pgri);
391 if (pgri->pgr_npages != npages)
392 panic("uvm_pagermapout: partial unmapping not supported");
393 LIST_REMOVE(pgri, pgr_entries);
394 mutex_exit(&pagermtx);
395
396 if (pgri->pgr_read) {
397 for (i = 0, curkva = pgri->pgr_kva;
398 i < pgri->pgr_npages;
399 i++, curkva += PAGE_SIZE) {
400 memcpy(pgri->pgr_pgs[i]->uanon,(void*)curkva,PAGE_SIZE);
401 }
402 }
403
404 kmem_free(pgri->pgr_pgs, npages * sizeof(struct vm_page *));
405 kmem_free((void*)pgri->pgr_kva, npages * PAGE_SIZE);
406 kmem_free(pgri, sizeof(*pgri));
407 }
408
409 /*
410 * convert va in pager window to page structure.
411 * XXX: how expensive is this (global lock, list traversal)?
412 */
413 struct vm_page *
414 uvm_pageratop(vaddr_t va)
415 {
416 struct pagerinfo *pgri;
417 struct vm_page *pg = NULL;
418 int i;
419
420 mutex_enter(&pagermtx);
421 LIST_FOREACH(pgri, &pagerlist, pgr_entries) {
422 if (pgri->pgr_kva <= va
423 && va < pgri->pgr_kva + pgri->pgr_npages*PAGE_SIZE)
424 break;
425 }
426 if (pgri) {
427 i = (va - pgri->pgr_kva) >> PAGE_SHIFT;
428 pg = pgri->pgr_pgs[i];
429 }
430 mutex_exit(&pagermtx);
431
432 return pg;
433 }
434
435 /* Called with the vm object locked */
436 struct vm_page *
437 uvm_pagelookup(struct uvm_object *uobj, voff_t off)
438 {
439 struct vm_page *pg;
440
441 TAILQ_FOREACH(pg, &uobj->memq, listq.queue) {
442 if (pg->offset == off) {
443 return pg;
444 }
445 }
446
447 return NULL;
448 }
449
450 void
451 uvm_page_unbusy(struct vm_page **pgs, int npgs)
452 {
453 struct vm_page *pg;
454 int i;
455
456 for (i = 0; i < npgs; i++) {
457 pg = pgs[i];
458 if (pg == NULL)
459 continue;
460
461 KASSERT(pg->flags & PG_BUSY);
462 if (pg->flags & PG_WANTED)
463 wakeup(pg);
464 if (pg->flags & PG_RELEASED)
465 uvm_pagefree(pg);
466 else
467 pg->flags &= ~(PG_WANTED|PG_BUSY);
468 }
469 }
470
471 void
472 uvm_estimatepageable(int *active, int *inactive)
473 {
474
475 /* XXX: guessing game */
476 *active = 1024;
477 *inactive = 1024;
478 }
479
480 struct vm_map_kernel *
481 vm_map_to_kernel(struct vm_map *map)
482 {
483
484 return (struct vm_map_kernel *)map;
485 }
486
487 bool
488 vm_map_starved_p(struct vm_map *map)
489 {
490
491 if (map->flags & VM_MAP_WANTVA)
492 return true;
493
494 return false;
495 }
496
497 int
498 uvm_loan(struct vm_map *map, vaddr_t start, vsize_t len, void *v, int flags)
499 {
500
501 panic("%s: unimplemented", __func__);
502 }
503
504 void
505 uvm_unloan(void *v, int npages, int flags)
506 {
507
508 panic("%s: unimplemented", __func__);
509 }
510
511 int
512 uvm_loanuobjpages(struct uvm_object *uobj, voff_t pgoff, int orignpages,
513 struct vm_page **opp)
514 {
515
516 return EBUSY;
517 }
518
519 #ifdef DEBUGPRINT
520 void
521 uvm_object_printit(struct uvm_object *uobj, bool full,
522 void (*pr)(const char *, ...))
523 {
524
525 pr("VM OBJECT at %p, refs %d", uobj, uobj->uo_refs);
526 }
527 #endif
528
529 vaddr_t
530 uvm_default_mapaddr(struct proc *p, vaddr_t base, vsize_t sz)
531 {
532
533 return 0;
534 }
535
536 int
537 uvm_map_protect(struct vm_map *map, vaddr_t start, vaddr_t end,
538 vm_prot_t prot, bool set_max)
539 {
540
541 return EOPNOTSUPP;
542 }
543
544 /*
545 * UVM km
546 */
547
548 vaddr_t
549 uvm_km_alloc(struct vm_map *map, vsize_t size, vsize_t align, uvm_flag_t flags)
550 {
551 void *rv, *desired = NULL;
552 int alignbit, error;
553
554 #ifdef __x86_64__
555 /*
556 * On amd64, allocate all module memory from the lowest 2GB.
557 * This is because NetBSD kernel modules are compiled
558 * with -mcmodel=kernel and reserve only 4 bytes for
559 * offsets. If we load code compiled with -mcmodel=kernel
560 * anywhere except the lowest or highest 2GB, it will not
561 * work. Since userspace does not have access to the highest
562 * 2GB, use the lowest 2GB.
563 *
564 * Note: this assumes the rump kernel resides in
565 * the lowest 2GB as well.
566 *
567 * Note2: yes, it's a quick hack, but since this the only
568 * place where we care about the map we're allocating from,
569 * just use a simple "if" instead of coming up with a fancy
570 * generic solution.
571 */
572 extern struct vm_map *module_map;
573 if (map == module_map) {
574 desired = (void *)(0x80000000 - size);
575 }
576 #endif
577
578 alignbit = 0;
579 if (align) {
580 alignbit = ffs(align)-1;
581 }
582
583 rv = rumpuser_anonmmap(desired, size, alignbit, flags & UVM_KMF_EXEC,
584 &error);
585 if (rv == NULL) {
586 if (flags & (UVM_KMF_CANFAIL | UVM_KMF_NOWAIT))
587 return 0;
588 else
589 panic("uvm_km_alloc failed");
590 }
591
592 if (flags & UVM_KMF_ZERO)
593 memset(rv, 0, size);
594
595 return (vaddr_t)rv;
596 }
597
598 void
599 uvm_km_free(struct vm_map *map, vaddr_t vaddr, vsize_t size, uvm_flag_t flags)
600 {
601
602 rumpuser_unmap((void *)vaddr, size);
603 }
604
605 struct vm_map *
606 uvm_km_suballoc(struct vm_map *map, vaddr_t *minaddr, vaddr_t *maxaddr,
607 vsize_t size, int pageable, bool fixed, struct vm_map_kernel *submap)
608 {
609
610 return (struct vm_map *)417416;
611 }
612
613 vaddr_t
614 uvm_km_alloc_poolpage(struct vm_map *map, bool waitok)
615 {
616
617 return (vaddr_t)rump_hypermalloc(PAGE_SIZE, PAGE_SIZE,
618 waitok, "kmalloc");
619 }
620
621 void
622 uvm_km_free_poolpage(struct vm_map *map, vaddr_t addr)
623 {
624
625 rumpuser_free((void *)addr);
626 }
627
628 vaddr_t
629 uvm_km_alloc_poolpage_cache(struct vm_map *map, bool waitok)
630 {
631
632 return uvm_km_alloc_poolpage(map, waitok);
633 }
634
635 void
636 uvm_km_free_poolpage_cache(struct vm_map *map, vaddr_t vaddr)
637 {
638
639 uvm_km_free_poolpage(map, vaddr);
640 }
641
642 void
643 uvm_km_va_drain(struct vm_map *map, uvm_flag_t flags)
644 {
645
646 /* we eventually maybe want some model for available memory */
647 }
648
649 /*
650 * Mapping and vm space locking routines.
651 * XXX: these don't work for non-local vmspaces
652 */
653 int
654 uvm_vslock(struct vmspace *vs, void *addr, size_t len, vm_prot_t access)
655 {
656
657 KASSERT(vs == &vmspace0);
658 return 0;
659 }
660
661 void
662 uvm_vsunlock(struct vmspace *vs, void *addr, size_t len)
663 {
664
665 KASSERT(vs == &vmspace0);
666 }
667
668 void
669 vmapbuf(struct buf *bp, vsize_t len)
670 {
671
672 bp->b_saveaddr = bp->b_data;
673 }
674
675 void
676 vunmapbuf(struct buf *bp, vsize_t len)
677 {
678
679 bp->b_data = bp->b_saveaddr;
680 bp->b_saveaddr = 0;
681 }
682
683 void
684 uvmspace_addref(struct vmspace *vm)
685 {
686
687 /*
688 * there is only vmspace0. we're not planning on
689 * feeding it to the fishes.
690 */
691 }
692
693 void
694 uvmspace_free(struct vmspace *vm)
695 {
696
697 /* nothing for now */
698 }
699
700 int
701 uvm_io(struct vm_map *map, struct uio *uio)
702 {
703
704 /*
705 * just do direct uio for now. but this needs some vmspace
706 * olympics for rump_sysproxy.
707 */
708 return uiomove((void *)(vaddr_t)uio->uio_offset, uio->uio_resid, uio);
709 }
710
711 /*
712 * page life cycle stuff. it really doesn't exist, so just stubs.
713 */
714
715 void
716 uvm_pageactivate(struct vm_page *pg)
717 {
718
719 /* nada */
720 }
721
722 void
723 uvm_pagedeactivate(struct vm_page *pg)
724 {
725
726 /* nada */
727 }
728
729 void
730 uvm_pagedequeue(struct vm_page *pg)
731 {
732
733 /* nada*/
734 }
735
736 void
737 uvm_pageenqueue(struct vm_page *pg)
738 {
739
740 /* nada */
741 }
742
743 /*
744 * Routines related to the Page Baroness.
745 */
746
747 void
748 uvm_wait(const char *msg)
749 {
750
751 if (__predict_false(curlwp == uvm.pagedaemon_lwp))
752 panic("pagedaemon out of memory");
753 if (__predict_false(rump_threads == 0))
754 panic("pagedaemon missing (RUMP_THREADS = 0)");
755
756 mutex_enter(&pdaemonmtx);
757 pdaemon_waiters++;
758 cv_signal(&pdaemoncv);
759 cv_wait(&oomwait, &pdaemonmtx);
760 mutex_exit(&pdaemonmtx);
761 }
762
763 void
764 uvm_pageout_start(int npages)
765 {
766
767 /* we don't have the heuristics */
768 }
769
770 void
771 uvm_pageout_done(int npages)
772 {
773
774 /* could wakeup waiters, but just let the pagedaemon do it */
775 }
776
777 /*
778 * Under-construction page mistress. This is lacking vfs support, namely:
779 *
780 * 1) draining vfs buffers
781 * 2) paging out pages in vm vnode objects
782 * (we will not page out anon memory on the basis that
783 * that's the task of the host)
784 */
785
786 void
787 uvm_pageout(void *arg)
788 {
789 struct pool *pp, *pp_first;
790 uint64_t where;
791 int timo = 0;
792 bool succ;
793
794 mutex_enter(&pdaemonmtx);
795 for (;;) {
796 cv_timedwait(&pdaemoncv, &pdaemonmtx, timo);
797 uvmexp.pdwoke++;
798 kernel_map->flags |= VM_MAP_WANTVA;
799 mutex_exit(&pdaemonmtx);
800
801 succ = false;
802 pool_drain_start(&pp_first, &where);
803 pp = pp_first;
804 for (;;) {
805 succ = pool_drain_end(pp, where);
806 if (succ)
807 break;
808 pool_drain_start(&pp, &where);
809 if (pp == pp_first) {
810 succ = pool_drain_end(pp, where);
811 break;
812 }
813 }
814 mutex_enter(&pdaemonmtx);
815
816 if (!succ) {
817 rumpuser_dprintf("pagedaemoness: failed to reclaim "
818 "memory ... sleeping (deadlock?)\n");
819 timo = hz;
820 continue;
821 }
822 kernel_map->flags &= ~VM_MAP_WANTVA;
823 timo = 0;
824
825 if (pdaemon_waiters) {
826 pdaemon_waiters = 0;
827 cv_broadcast(&oomwait);
828 }
829 }
830
831 panic("you can swap out any time you like, but you can never leave");
832 }
833
834 /*
835 * In a regular kernel the pagedaemon is activated when memory becomes
836 * low. In a virtual rump kernel we do not know exactly how much memory
837 * we have available -- it depends on the conditions on the host.
838 * Therefore, we cannot preemptively kick the pagedaemon. Rather, we
839 * wait until things we desperate and we're forced to uvm_wait().
840 *
841 * The alternative would be to allocate a huge chunk of memory at
842 * startup, but that solution has a number of problems including
843 * being a resource hog, failing anyway due to host memory overcommit
844 * and core dump size.
845 */
846
847 void
848 uvm_kick_pdaemon()
849 {
850
851 /* nada */
852 }
853
854 void *
855 rump_hypermalloc(size_t howmuch, int alignment, bool waitok, const char *wmsg)
856 {
857 void *rv;
858
859 again:
860 rv = rumpuser_malloc(howmuch, alignment);
861 if (__predict_false(rv == NULL && waitok)) {
862 uvm_wait(wmsg);
863 goto again;
864 }
865
866 return rv;
867 }
868