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