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