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