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