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