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