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