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