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