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