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