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