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