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