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