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