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