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