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