1 /* $OpenBSD: uvm_page.c,v 1.131 2014/07/11 16:35:40 jsg Exp $ */ 2 /* $NetBSD: uvm_page.c,v 1.44 2000/11/27 08:40:04 chs Exp $ */ 3 4 /* 5 * Copyright (c) 1997 Charles D. Cranor and Washington University. 6 * Copyright (c) 1991, 1993, The Regents of the University of California. 7 * 8 * All rights reserved. 9 * 10 * This code is derived from software contributed to Berkeley by 11 * The Mach Operating System project at Carnegie-Mellon University. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)vm_page.c 8.3 (Berkeley) 3/21/94 38 * from: Id: uvm_page.c,v 1.1.2.18 1998/02/06 05:24:42 chs Exp 39 * 40 * 41 * Copyright (c) 1987, 1990 Carnegie-Mellon University. 42 * All rights reserved. 43 * 44 * Permission to use, copy, modify and distribute this software and 45 * its documentation is hereby granted, provided that both the copyright 46 * notice and this permission notice appear in all copies of the 47 * software, derivative works or modified versions, and any portions 48 * thereof, and that both notices appear in supporting documentation. 49 * 50 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 51 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 52 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 53 * 54 * Carnegie Mellon requests users of this software to return to 55 * 56 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 57 * School of Computer Science 58 * Carnegie Mellon University 59 * Pittsburgh PA 15213-3890 60 * 61 * any improvements or extensions that they make and grant Carnegie the 62 * rights to redistribute these changes. 63 */ 64 65 /* 66 * uvm_page.c: page ops. 67 */ 68 69 #include <sys/param.h> 70 #include <sys/systm.h> 71 #include <sys/sched.h> 72 #include <sys/kernel.h> 73 #include <sys/vnode.h> 74 #include <sys/mount.h> 75 #include <sys/proc.h> 76 77 #include <uvm/uvm.h> 78 79 /* 80 * for object trees 81 */ 82 RB_GENERATE(uvm_objtree, vm_page, objt, uvm_pagecmp); 83 84 int 85 uvm_pagecmp(struct vm_page *a, struct vm_page *b) 86 { 87 return (a->offset < b->offset ? -1 : a->offset > b->offset); 88 } 89 90 /* 91 * global vars... XXXCDC: move to uvm. structure. 92 */ 93 /* 94 * physical memory config is stored in vm_physmem. 95 */ 96 struct vm_physseg vm_physmem[VM_PHYSSEG_MAX]; /* XXXCDC: uvm.physmem */ 97 int vm_nphysseg = 0; /* XXXCDC: uvm.nphysseg */ 98 99 /* 100 * Some supported CPUs in a given architecture don't support all 101 * of the things necessary to do idle page zero'ing efficiently. 102 * We therefore provide a way to disable it from machdep code here. 103 */ 104 /* 105 * XXX disabled until we can find a way to do this without causing 106 * problems for either cpu caches or DMA latency. 107 */ 108 boolean_t vm_page_zero_enable = FALSE; 109 110 /* 111 * local variables 112 */ 113 /* 114 * these variables record the values returned by vm_page_bootstrap, 115 * for debugging purposes. The implementation of uvm_pageboot_alloc 116 * and pmap_startup here also uses them internally. 117 */ 118 static vaddr_t virtual_space_start; 119 static vaddr_t virtual_space_end; 120 121 /* 122 * local prototypes 123 */ 124 static void uvm_pageinsert(struct vm_page *); 125 static void uvm_pageremove(struct vm_page *); 126 127 /* 128 * inline functions 129 */ 130 /* 131 * uvm_pageinsert: insert a page in the object 132 * 133 * => caller must lock page queues XXX questionable 134 * => call should have already set pg's object and offset pointers 135 * and bumped the version counter 136 */ 137 __inline static void 138 uvm_pageinsert(struct vm_page *pg) 139 { 140 struct vm_page *dupe; 141 142 KASSERT((pg->pg_flags & PG_TABLED) == 0); 143 dupe = RB_INSERT(uvm_objtree, &pg->uobject->memt, pg); 144 /* not allowed to insert over another page */ 145 KASSERT(dupe == NULL); 146 atomic_setbits_int(&pg->pg_flags, PG_TABLED); 147 pg->uobject->uo_npages++; 148 } 149 150 /* 151 * uvm_page_remove: remove page from object 152 * 153 * => caller must lock page queues 154 */ 155 static __inline void 156 uvm_pageremove(struct vm_page *pg) 157 { 158 159 KASSERT(pg->pg_flags & PG_TABLED); 160 RB_REMOVE(uvm_objtree, &pg->uobject->memt, pg); 161 162 atomic_clearbits_int(&pg->pg_flags, PG_TABLED); 163 pg->uobject->uo_npages--; 164 pg->uobject = NULL; 165 pg->pg_version++; 166 } 167 168 /* 169 * uvm_page_init: init the page system. called from uvm_init(). 170 * 171 * => we return the range of kernel virtual memory in kvm_startp/kvm_endp 172 */ 173 void 174 uvm_page_init(vaddr_t *kvm_startp, vaddr_t *kvm_endp) 175 { 176 vsize_t freepages, pagecount, n; 177 vm_page_t pagearray, curpg; 178 int lcv, i; 179 paddr_t paddr, pgno; 180 struct vm_physseg *seg; 181 182 /* 183 * init the page queues and page queue locks 184 */ 185 186 TAILQ_INIT(&uvm.page_active); 187 TAILQ_INIT(&uvm.page_inactive_swp); 188 TAILQ_INIT(&uvm.page_inactive_obj); 189 mtx_init(&uvm.fpageqlock, IPL_VM); 190 uvm_pmr_init(); 191 192 /* 193 * allocate vm_page structures. 194 */ 195 196 /* 197 * sanity check: 198 * before calling this function the MD code is expected to register 199 * some free RAM with the uvm_page_physload() function. our job 200 * now is to allocate vm_page structures for this memory. 201 */ 202 203 if (vm_nphysseg == 0) 204 panic("uvm_page_bootstrap: no memory pre-allocated"); 205 206 /* 207 * first calculate the number of free pages... 208 * 209 * note that we use start/end rather than avail_start/avail_end. 210 * this allows us to allocate extra vm_page structures in case we 211 * want to return some memory to the pool after booting. 212 */ 213 214 freepages = 0; 215 for (lcv = 0, seg = vm_physmem; lcv < vm_nphysseg ; lcv++, seg++) 216 freepages += (seg->end - seg->start); 217 218 /* 219 * we now know we have (PAGE_SIZE * freepages) bytes of memory we can 220 * use. for each page of memory we use we need a vm_page structure. 221 * thus, the total number of pages we can use is the total size of 222 * the memory divided by the PAGE_SIZE plus the size of the vm_page 223 * structure. we add one to freepages as a fudge factor to avoid 224 * truncation errors (since we can only allocate in terms of whole 225 * pages). 226 */ 227 228 pagecount = (((paddr_t)freepages + 1) << PAGE_SHIFT) / 229 (PAGE_SIZE + sizeof(struct vm_page)); 230 pagearray = (vm_page_t)uvm_pageboot_alloc(pagecount * 231 sizeof(struct vm_page)); 232 memset(pagearray, 0, pagecount * sizeof(struct vm_page)); 233 234 /* init the vm_page structures and put them in the correct place. */ 235 for (lcv = 0, seg = vm_physmem; lcv < vm_nphysseg ; lcv++, seg++) { 236 n = seg->end - seg->start; 237 if (n > pagecount) { 238 panic("uvm_page_init: lost %ld page(s) in init", 239 (long)(n - pagecount)); 240 /* XXXCDC: shouldn't happen? */ 241 /* n = pagecount; */ 242 } 243 244 /* set up page array pointers */ 245 seg->pgs = pagearray; 246 pagearray += n; 247 pagecount -= n; 248 seg->lastpg = seg->pgs + (n - 1); 249 250 /* init and free vm_pages (we've already zeroed them) */ 251 pgno = seg->start; 252 paddr = ptoa(pgno); 253 for (i = 0, curpg = seg->pgs; i < n; 254 i++, curpg++, pgno++, paddr += PAGE_SIZE) { 255 curpg->phys_addr = paddr; 256 VM_MDPAGE_INIT(curpg); 257 if (pgno >= seg->avail_start && 258 pgno <= seg->avail_end) { 259 uvmexp.npages++; 260 } 261 } 262 263 /* Add pages to free pool. */ 264 uvm_pmr_freepages(&seg->pgs[seg->avail_start - seg->start], 265 seg->avail_end - seg->avail_start); 266 } 267 268 /* 269 * pass up the values of virtual_space_start and 270 * virtual_space_end (obtained by uvm_pageboot_alloc) to the upper 271 * layers of the VM. 272 */ 273 274 *kvm_startp = round_page(virtual_space_start); 275 *kvm_endp = trunc_page(virtual_space_end); 276 277 /* init locks for kernel threads */ 278 mtx_init(&uvm.aiodoned_lock, IPL_BIO); 279 280 /* 281 * init reserve thresholds 282 * XXXCDC - values may need adjusting 283 */ 284 uvmexp.reserve_pagedaemon = 4; 285 uvmexp.reserve_kernel = 6; 286 uvmexp.anonminpct = 10; 287 uvmexp.vnodeminpct = 10; 288 uvmexp.vtextminpct = 5; 289 uvmexp.anonmin = uvmexp.anonminpct * 256 / 100; 290 uvmexp.vnodemin = uvmexp.vnodeminpct * 256 / 100; 291 uvmexp.vtextmin = uvmexp.vtextminpct * 256 / 100; 292 293 /* determine if we should zero pages in the idle loop. */ 294 uvm.page_idle_zero = vm_page_zero_enable; 295 296 uvm.page_init_done = TRUE; 297 } 298 299 /* 300 * uvm_setpagesize: set the page size 301 * 302 * => sets page_shift and page_mask from uvmexp.pagesize. 303 */ 304 void 305 uvm_setpagesize(void) 306 { 307 if (uvmexp.pagesize == 0) 308 uvmexp.pagesize = DEFAULT_PAGE_SIZE; 309 uvmexp.pagemask = uvmexp.pagesize - 1; 310 if ((uvmexp.pagemask & uvmexp.pagesize) != 0) 311 panic("uvm_setpagesize: page size not a power of two"); 312 for (uvmexp.pageshift = 0; ; uvmexp.pageshift++) 313 if ((1 << uvmexp.pageshift) == uvmexp.pagesize) 314 break; 315 } 316 317 /* 318 * uvm_pageboot_alloc: steal memory from physmem for bootstrapping 319 */ 320 vaddr_t 321 uvm_pageboot_alloc(vsize_t size) 322 { 323 #if defined(PMAP_STEAL_MEMORY) 324 vaddr_t addr; 325 326 /* 327 * defer bootstrap allocation to MD code (it may want to allocate 328 * from a direct-mapped segment). pmap_steal_memory should round 329 * off virtual_space_start/virtual_space_end. 330 */ 331 332 addr = pmap_steal_memory(size, &virtual_space_start, 333 &virtual_space_end); 334 335 return(addr); 336 337 #else /* !PMAP_STEAL_MEMORY */ 338 339 static boolean_t initialized = FALSE; 340 vaddr_t addr, vaddr; 341 paddr_t paddr; 342 343 /* round to page size */ 344 size = round_page(size); 345 346 /* on first call to this function, initialize ourselves. */ 347 if (initialized == FALSE) { 348 pmap_virtual_space(&virtual_space_start, &virtual_space_end); 349 350 /* round it the way we like it */ 351 virtual_space_start = round_page(virtual_space_start); 352 virtual_space_end = trunc_page(virtual_space_end); 353 354 initialized = TRUE; 355 } 356 357 /* allocate virtual memory for this request */ 358 if (virtual_space_start == virtual_space_end || 359 (virtual_space_end - virtual_space_start) < size) 360 panic("uvm_pageboot_alloc: out of virtual space"); 361 362 addr = virtual_space_start; 363 364 #ifdef PMAP_GROWKERNEL 365 /* 366 * If the kernel pmap can't map the requested space, 367 * then allocate more resources for it. 368 */ 369 if (uvm_maxkaddr < (addr + size)) { 370 uvm_maxkaddr = pmap_growkernel(addr + size); 371 if (uvm_maxkaddr < (addr + size)) 372 panic("uvm_pageboot_alloc: pmap_growkernel() failed"); 373 } 374 #endif 375 376 virtual_space_start += size; 377 378 /* allocate and mapin physical pages to back new virtual pages */ 379 for (vaddr = round_page(addr) ; vaddr < addr + size ; 380 vaddr += PAGE_SIZE) { 381 if (!uvm_page_physget(&paddr)) 382 panic("uvm_pageboot_alloc: out of memory"); 383 384 /* 385 * Note this memory is no longer managed, so using 386 * pmap_kenter is safe. 387 */ 388 pmap_kenter_pa(vaddr, paddr, VM_PROT_READ|VM_PROT_WRITE); 389 } 390 pmap_update(pmap_kernel()); 391 return(addr); 392 #endif /* PMAP_STEAL_MEMORY */ 393 } 394 395 #if !defined(PMAP_STEAL_MEMORY) 396 /* 397 * uvm_page_physget: "steal" one page from the vm_physmem structure. 398 * 399 * => attempt to allocate it off the end of a segment in which the "avail" 400 * values match the start/end values. if we can't do that, then we 401 * will advance both values (making them equal, and removing some 402 * vm_page structures from the non-avail area). 403 * => return false if out of memory. 404 */ 405 406 boolean_t 407 uvm_page_physget(paddr_t *paddrp) 408 { 409 int lcv; 410 struct vm_physseg *seg; 411 412 /* pass 1: try allocating from a matching end */ 413 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST) || \ 414 (VM_PHYSSEG_STRAT == VM_PSTRAT_BSEARCH) 415 for (lcv = vm_nphysseg - 1, seg = vm_physmem + lcv; lcv >= 0; 416 lcv--, seg--) 417 #else 418 for (lcv = 0, seg = vm_physmem; lcv < vm_nphysseg ; lcv++, seg++) 419 #endif 420 { 421 if (uvm.page_init_done == TRUE) 422 panic("uvm_page_physget: called _after_ bootstrap"); 423 424 /* try from front */ 425 if (seg->avail_start == seg->start && 426 seg->avail_start < seg->avail_end) { 427 *paddrp = ptoa(seg->avail_start); 428 seg->avail_start++; 429 seg->start++; 430 /* nothing left? nuke it */ 431 if (seg->avail_start == seg->end) { 432 if (vm_nphysseg == 1) 433 panic("uvm_page_physget: out of memory!"); 434 vm_nphysseg--; 435 for (; lcv < vm_nphysseg; lcv++, seg++) 436 /* structure copy */ 437 seg[0] = seg[1]; 438 } 439 return (TRUE); 440 } 441 442 /* try from rear */ 443 if (seg->avail_end == seg->end && 444 seg->avail_start < seg->avail_end) { 445 *paddrp = ptoa(seg->avail_end - 1); 446 seg->avail_end--; 447 seg->end--; 448 /* nothing left? nuke it */ 449 if (seg->avail_end == seg->start) { 450 if (vm_nphysseg == 1) 451 panic("uvm_page_physget: out of memory!"); 452 vm_nphysseg--; 453 for (; lcv < vm_nphysseg ; lcv++, seg++) 454 /* structure copy */ 455 seg[0] = seg[1]; 456 } 457 return (TRUE); 458 } 459 } 460 461 /* pass2: forget about matching ends, just allocate something */ 462 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST) || \ 463 (VM_PHYSSEG_STRAT == VM_PSTRAT_BSEARCH) 464 for (lcv = vm_nphysseg - 1, seg = vm_physmem + lcv; lcv >= 0; 465 lcv--, seg--) 466 #else 467 for (lcv = 0, seg = vm_physmem; lcv < vm_nphysseg ; lcv++, seg++) 468 #endif 469 { 470 471 /* any room in this bank? */ 472 if (seg->avail_start >= seg->avail_end) 473 continue; /* nope */ 474 475 *paddrp = ptoa(seg->avail_start); 476 seg->avail_start++; 477 /* truncate! */ 478 seg->start = seg->avail_start; 479 480 /* nothing left? nuke it */ 481 if (seg->avail_start == seg->end) { 482 if (vm_nphysseg == 1) 483 panic("uvm_page_physget: out of memory!"); 484 vm_nphysseg--; 485 for (; lcv < vm_nphysseg ; lcv++, seg++) 486 /* structure copy */ 487 seg[0] = seg[1]; 488 } 489 return (TRUE); 490 } 491 492 return (FALSE); /* whoops! */ 493 } 494 495 #endif /* PMAP_STEAL_MEMORY */ 496 497 /* 498 * uvm_page_physload: load physical memory into VM system 499 * 500 * => all args are PFs 501 * => all pages in start/end get vm_page structures 502 * => areas marked by avail_start/avail_end get added to the free page pool 503 * => we are limited to VM_PHYSSEG_MAX physical memory segments 504 */ 505 506 void 507 uvm_page_physload(paddr_t start, paddr_t end, paddr_t avail_start, 508 paddr_t avail_end, int flags) 509 { 510 int preload, lcv; 511 psize_t npages; 512 struct vm_page *pgs; 513 struct vm_physseg *ps, *seg; 514 515 #ifdef DIAGNOSTIC 516 if (uvmexp.pagesize == 0) 517 panic("uvm_page_physload: page size not set!"); 518 519 if (start >= end) 520 panic("uvm_page_physload: start >= end"); 521 #endif 522 523 /* do we have room? */ 524 if (vm_nphysseg == VM_PHYSSEG_MAX) { 525 printf("uvm_page_physload: unable to load physical memory " 526 "segment\n"); 527 printf("\t%d segments allocated, ignoring 0x%llx -> 0x%llx\n", 528 VM_PHYSSEG_MAX, (long long)start, (long long)end); 529 printf("\tincrease VM_PHYSSEG_MAX\n"); 530 return; 531 } 532 533 /* 534 * check to see if this is a "preload" (i.e. uvm_mem_init hasn't been 535 * called yet, so malloc is not available). 536 */ 537 for (lcv = 0, seg = vm_physmem; lcv < vm_nphysseg; lcv++, seg++) { 538 if (seg->pgs) 539 break; 540 } 541 preload = (lcv == vm_nphysseg); 542 543 /* if VM is already running, attempt to malloc() vm_page structures */ 544 if (!preload) { 545 /* 546 * XXXCDC: need some sort of lockout for this case 547 * right now it is only used by devices so it should be alright. 548 */ 549 paddr_t paddr; 550 551 npages = end - start; /* # of pages */ 552 553 pgs = (struct vm_page *)uvm_km_zalloc(kernel_map, 554 npages * sizeof(*pgs)); 555 if (pgs == NULL) { 556 printf("uvm_page_physload: can not malloc vm_page " 557 "structs for segment\n"); 558 printf("\tignoring 0x%lx -> 0x%lx\n", start, end); 559 return; 560 } 561 /* init phys_addr and free pages, XXX uvmexp.npages */ 562 for (lcv = 0, paddr = ptoa(start); lcv < npages; 563 lcv++, paddr += PAGE_SIZE) { 564 pgs[lcv].phys_addr = paddr; 565 VM_MDPAGE_INIT(&pgs[lcv]); 566 if (atop(paddr) >= avail_start && 567 atop(paddr) <= avail_end) { 568 if (flags & PHYSLOAD_DEVICE) { 569 atomic_setbits_int(&pgs[lcv].pg_flags, 570 PG_DEV); 571 pgs[lcv].wire_count = 1; 572 } else { 573 #if defined(VM_PHYSSEG_NOADD) 574 panic("uvm_page_physload: tried to add RAM after vm_mem_init"); 575 #endif 576 } 577 } 578 } 579 580 /* Add pages to free pool. */ 581 if ((flags & PHYSLOAD_DEVICE) == 0) { 582 uvm_pmr_freepages(&pgs[avail_start - start], 583 avail_end - avail_start); 584 } 585 586 /* XXXCDC: need hook to tell pmap to rebuild pv_list, etc... */ 587 } else { 588 /* gcc complains if these don't get init'd */ 589 pgs = NULL; 590 npages = 0; 591 592 } 593 594 /* now insert us in the proper place in vm_physmem[] */ 595 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_RANDOM) 596 /* random: put it at the end (easy!) */ 597 ps = &vm_physmem[vm_nphysseg]; 598 #elif (VM_PHYSSEG_STRAT == VM_PSTRAT_BSEARCH) 599 { 600 int x; 601 /* sort by address for binary search */ 602 for (lcv = 0, seg = vm_physmem; lcv < vm_nphysseg; lcv++, seg++) 603 if (start < seg->start) 604 break; 605 ps = seg; 606 /* move back other entries, if necessary ... */ 607 for (x = vm_nphysseg, seg = vm_physmem + x - 1; x > lcv; 608 x--, seg--) 609 /* structure copy */ 610 seg[1] = seg[0]; 611 } 612 #elif (VM_PHYSSEG_STRAT == VM_PSTRAT_BIGFIRST) 613 { 614 int x; 615 /* sort by largest segment first */ 616 for (lcv = 0, seg = vm_physmem; lcv < vm_nphysseg; lcv++, seg++) 617 if ((end - start) > 618 (seg->end - seg->start)) 619 break; 620 ps = &vm_physmem[lcv]; 621 /* move back other entries, if necessary ... */ 622 for (x = vm_nphysseg, seg = vm_physmem + x - 1; x > lcv; 623 x--, seg--) 624 /* structure copy */ 625 seg[1] = seg[0]; 626 } 627 #else 628 panic("uvm_page_physload: unknown physseg strategy selected!"); 629 #endif 630 631 ps->start = start; 632 ps->end = end; 633 ps->avail_start = avail_start; 634 ps->avail_end = avail_end; 635 if (preload) { 636 ps->pgs = NULL; 637 } else { 638 ps->pgs = pgs; 639 ps->lastpg = pgs + npages - 1; 640 } 641 vm_nphysseg++; 642 643 return; 644 } 645 646 #ifdef DDB /* XXXCDC: TMP TMP TMP DEBUG DEBUG DEBUG */ 647 648 void uvm_page_physdump(void); /* SHUT UP GCC */ 649 650 /* call from DDB */ 651 void 652 uvm_page_physdump(void) 653 { 654 int lcv; 655 struct vm_physseg *seg; 656 657 printf("uvm_page_physdump: physical memory config [segs=%d of %d]:\n", 658 vm_nphysseg, VM_PHYSSEG_MAX); 659 for (lcv = 0, seg = vm_physmem; lcv < vm_nphysseg ; lcv++, seg++) 660 printf("0x%llx->0x%llx [0x%llx->0x%llx]\n", 661 (long long)seg->start, 662 (long long)seg->end, 663 (long long)seg->avail_start, 664 (long long)seg->avail_end); 665 printf("STRATEGY = "); 666 switch (VM_PHYSSEG_STRAT) { 667 case VM_PSTRAT_RANDOM: printf("RANDOM\n"); break; 668 case VM_PSTRAT_BSEARCH: printf("BSEARCH\n"); break; 669 case VM_PSTRAT_BIGFIRST: printf("BIGFIRST\n"); break; 670 default: printf("<<UNKNOWN>>!!!!\n"); 671 } 672 } 673 #endif 674 675 void 676 uvm_shutdown(void) 677 { 678 #ifdef UVM_SWAP_ENCRYPT 679 uvm_swap_finicrypt_all(); 680 #endif 681 } 682 683 /* 684 * Perform insert of a given page in the specified anon of obj. 685 * This is basically, uvm_pagealloc, but with the page already given. 686 */ 687 void 688 uvm_pagealloc_pg(struct vm_page *pg, struct uvm_object *obj, voff_t off, 689 struct vm_anon *anon) 690 { 691 int flags; 692 693 flags = PG_BUSY | PG_FAKE; 694 pg->offset = off; 695 pg->uobject = obj; 696 pg->uanon = anon; 697 698 if (anon) { 699 anon->an_page = pg; 700 flags |= PQ_ANON; 701 } else if (obj) 702 uvm_pageinsert(pg); 703 atomic_setbits_int(&pg->pg_flags, flags); 704 #if defined(UVM_PAGE_TRKOWN) 705 pg->owner_tag = NULL; 706 #endif 707 UVM_PAGE_OWN(pg, "new alloc"); 708 } 709 710 /* 711 * uvm_pglistalloc: allocate a list of pages 712 * 713 * => allocated pages are placed at the tail of rlist. rlist is 714 * assumed to be properly initialized by caller. 715 * => returns 0 on success or errno on failure 716 * => doesn't take into account clean non-busy pages on inactive list 717 * that could be used(?) 718 * => params: 719 * size the size of the allocation, rounded to page size. 720 * low the low address of the allowed allocation range. 721 * high the high address of the allowed allocation range. 722 * alignment memory must be aligned to this power-of-two boundary. 723 * boundary no segment in the allocation may cross this 724 * power-of-two boundary (relative to zero). 725 * => flags: 726 * UVM_PLA_NOWAIT fail if allocation fails 727 * UVM_PLA_WAITOK wait for memory to become avail 728 * UVM_PLA_ZERO return zeroed memory 729 */ 730 int 731 uvm_pglistalloc(psize_t size, paddr_t low, paddr_t high, paddr_t alignment, 732 paddr_t boundary, struct pglist *rlist, int nsegs, int flags) 733 { 734 KASSERT((alignment & (alignment - 1)) == 0); 735 KASSERT((boundary & (boundary - 1)) == 0); 736 KASSERT(!(flags & UVM_PLA_WAITOK) ^ !(flags & UVM_PLA_NOWAIT)); 737 738 if (size == 0) 739 return (EINVAL); 740 size = atop(round_page(size)); 741 742 /* 743 * check to see if we need to generate some free pages waking 744 * the pagedaemon. 745 */ 746 if ((uvmexp.free - BUFPAGES_DEFICIT) < uvmexp.freemin || 747 ((uvmexp.free - BUFPAGES_DEFICIT) < uvmexp.freetarg && 748 (uvmexp.inactive + BUFPAGES_INACT) < uvmexp.inactarg)) 749 wakeup(&uvm.pagedaemon); 750 751 /* 752 * XXX uvm_pglistalloc is currently only used for kernel 753 * objects. Unlike the checks in uvm_pagealloc, below, here 754 * we are always allowed to use the kernel reseve. However, we 755 * have to enforce the pagedaemon reserve here or allocations 756 * via this path could consume everything and we can't 757 * recover in the page daemon. 758 */ 759 again: 760 if ((uvmexp.free <= uvmexp.reserve_pagedaemon + size && 761 !((curproc == uvm.pagedaemon_proc) || 762 (curproc == syncerproc)))) { 763 if (flags & UVM_PLA_WAITOK) { 764 uvm_wait("uvm_pglistalloc"); 765 goto again; 766 } 767 return (ENOMEM); 768 } 769 770 if ((high & PAGE_MASK) != PAGE_MASK) { 771 printf("uvm_pglistalloc: Upper boundary 0x%lx " 772 "not on pagemask.\n", (unsigned long)high); 773 } 774 775 /* 776 * Our allocations are always page granularity, so our alignment 777 * must be, too. 778 */ 779 if (alignment < PAGE_SIZE) 780 alignment = PAGE_SIZE; 781 782 low = atop(roundup(low, alignment)); 783 /* 784 * high + 1 may result in overflow, in which case high becomes 0x0, 785 * which is the 'don't care' value. 786 * The only requirement in that case is that low is also 0x0, or the 787 * low<high assert will fail. 788 */ 789 high = atop(high + 1); 790 alignment = atop(alignment); 791 if (boundary < PAGE_SIZE && boundary != 0) 792 boundary = PAGE_SIZE; 793 boundary = atop(boundary); 794 795 return uvm_pmr_getpages(size, low, high, alignment, boundary, nsegs, 796 flags, rlist); 797 } 798 799 /* 800 * uvm_pglistfree: free a list of pages 801 * 802 * => pages should already be unmapped 803 */ 804 void 805 uvm_pglistfree(struct pglist *list) 806 { 807 uvm_pmr_freepageq(list); 808 } 809 810 /* 811 * interface used by the buffer cache to allocate a buffer at a time. 812 * The pages are allocated wired in DMA accessible memory 813 */ 814 void 815 uvm_pagealloc_multi(struct uvm_object *obj, voff_t off, vsize_t size, 816 int flags) 817 { 818 struct pglist plist; 819 struct vm_page *pg; 820 int i; 821 822 823 TAILQ_INIT(&plist); 824 (void) uvm_pglistalloc(size, dma_constraint.ucr_low, 825 dma_constraint.ucr_high, 0, 0, &plist, atop(round_page(size)), 826 UVM_PLA_WAITOK); 827 i = 0; 828 while ((pg = TAILQ_FIRST(&plist)) != NULL) { 829 pg->wire_count = 1; 830 atomic_setbits_int(&pg->pg_flags, PG_CLEAN | PG_FAKE); 831 KASSERT((pg->pg_flags & PG_DEV) == 0); 832 TAILQ_REMOVE(&plist, pg, pageq); 833 uvm_pagealloc_pg(pg, obj, off + ptoa(i++), NULL); 834 } 835 } 836 837 /* 838 * interface used by the buffer cache to reallocate a buffer at a time. 839 * The pages are reallocated wired outside the DMA accessible region. 840 * 841 */ 842 void 843 uvm_pagerealloc_multi(struct uvm_object *obj, voff_t off, vsize_t size, 844 int flags, struct uvm_constraint_range *where) 845 { 846 struct pglist plist; 847 struct vm_page *pg, *tpg; 848 int i; 849 voff_t offset; 850 851 852 TAILQ_INIT(&plist); 853 if (size == 0) 854 panic("size 0 uvm_pagerealloc"); 855 (void) uvm_pglistalloc(size, where->ucr_low, where->ucr_high, 0, 856 0, &plist, atop(round_page(size)), UVM_PLA_WAITOK); 857 i = 0; 858 while((pg = TAILQ_FIRST(&plist)) != NULL) { 859 offset = off + ptoa(i++); 860 tpg = uvm_pagelookup(obj, offset); 861 pg->wire_count = 1; 862 atomic_setbits_int(&pg->pg_flags, PG_CLEAN | PG_FAKE); 863 KASSERT((pg->pg_flags & PG_DEV) == 0); 864 TAILQ_REMOVE(&plist, pg, pageq); 865 uvm_pagecopy(tpg, pg); 866 uvm_pagefree(tpg); 867 uvm_pagealloc_pg(pg, obj, offset, NULL); 868 } 869 } 870 871 /* 872 * uvm_pagealloc_strat: allocate vm_page from a particular free list. 873 * 874 * => return null if no pages free 875 * => wake up pagedaemon if number of free pages drops below low water mark 876 * => only one of obj or anon can be non-null 877 * => caller must activate/deactivate page if it is not wired. 878 */ 879 880 struct vm_page * 881 uvm_pagealloc(struct uvm_object *obj, voff_t off, struct vm_anon *anon, 882 int flags) 883 { 884 struct vm_page *pg; 885 struct pglist pgl; 886 int pmr_flags; 887 boolean_t use_reserve; 888 889 KASSERT(obj == NULL || anon == NULL); 890 KASSERT(off == trunc_page(off)); 891 892 /* 893 * check to see if we need to generate some free pages waking 894 * the pagedaemon. 895 */ 896 if ((uvmexp.free - BUFPAGES_DEFICIT) < uvmexp.freemin || 897 ((uvmexp.free - BUFPAGES_DEFICIT) < uvmexp.freetarg && 898 (uvmexp.inactive + BUFPAGES_INACT) < uvmexp.inactarg)) 899 wakeup(&uvm.pagedaemon); 900 901 /* 902 * fail if any of these conditions is true: 903 * [1] there really are no free pages, or 904 * [2] only kernel "reserved" pages remain and 905 * the page isn't being allocated to a kernel object. 906 * [3] only pagedaemon "reserved" pages remain and 907 * the requestor isn't the pagedaemon. 908 */ 909 use_reserve = (flags & UVM_PGA_USERESERVE) || 910 (obj && UVM_OBJ_IS_KERN_OBJECT(obj)); 911 if ((uvmexp.free <= uvmexp.reserve_kernel && !use_reserve) || 912 (uvmexp.free <= uvmexp.reserve_pagedaemon && 913 !((curproc == uvm.pagedaemon_proc) || 914 (curproc == syncerproc)))) 915 goto fail; 916 917 pmr_flags = UVM_PLA_NOWAIT; 918 if (flags & UVM_PGA_ZERO) 919 pmr_flags |= UVM_PLA_ZERO; 920 TAILQ_INIT(&pgl); 921 if (uvm_pmr_getpages(1, 0, 0, 1, 0, 1, pmr_flags, &pgl) != 0) 922 goto fail; 923 924 pg = TAILQ_FIRST(&pgl); 925 KASSERT(pg != NULL && TAILQ_NEXT(pg, pageq) == NULL); 926 927 uvm_pagealloc_pg(pg, obj, off, anon); 928 KASSERT((pg->pg_flags & PG_DEV) == 0); 929 if (flags & UVM_PGA_ZERO) 930 atomic_clearbits_int(&pg->pg_flags, PG_CLEAN); 931 else 932 atomic_setbits_int(&pg->pg_flags, PG_CLEAN); 933 934 return(pg); 935 936 fail: 937 return (NULL); 938 } 939 940 /* 941 * uvm_pagerealloc: reallocate a page from one object to another 942 */ 943 944 void 945 uvm_pagerealloc(struct vm_page *pg, struct uvm_object *newobj, voff_t newoff) 946 { 947 948 /* remove it from the old object */ 949 if (pg->uobject) { 950 uvm_pageremove(pg); 951 } 952 953 /* put it in the new object */ 954 if (newobj) { 955 pg->uobject = newobj; 956 pg->offset = newoff; 957 pg->pg_version++; 958 uvm_pageinsert(pg); 959 } 960 } 961 962 963 /* 964 * uvm_pagefree: free page 965 * 966 * => erase page's identity (i.e. remove from object) 967 * => put page on free list 968 * => caller must lock page queues 969 * => assumes all valid mappings of pg are gone 970 */ 971 void 972 uvm_pagefree(struct vm_page *pg) 973 { 974 int saved_loan_count = pg->loan_count; 975 u_int flags_to_clear = 0; 976 977 #ifdef DEBUG 978 if (pg->uobject == (void *)0xdeadbeef && 979 pg->uanon == (void *)0xdeadbeef) { 980 panic("uvm_pagefree: freeing free page %p", pg); 981 } 982 #endif 983 984 KASSERT((pg->pg_flags & PG_DEV) == 0); 985 986 /* 987 * if the page was an object page (and thus "TABLED"), remove it 988 * from the object. 989 */ 990 if (pg->pg_flags & PG_TABLED) { 991 /* 992 * if the object page is on loan we are going to drop ownership. 993 * it is possible that an anon will take over as owner for this 994 * page later on. the anon will want a !PG_CLEAN page so that 995 * it knows it needs to allocate swap if it wants to page the 996 * page out. 997 */ 998 999 /* in case an anon takes over */ 1000 if (saved_loan_count) 1001 atomic_clearbits_int(&pg->pg_flags, PG_CLEAN); 1002 uvm_pageremove(pg); 1003 1004 /* 1005 * if our page was on loan, then we just lost control over it 1006 * (in fact, if it was loaned to an anon, the anon may have 1007 * already taken over ownership of the page by now and thus 1008 * changed the loan_count [e.g. in uvmfault_anonget()]) we just 1009 * return (when the last loan is dropped, then the page can be 1010 * freed by whatever was holding the last loan). 1011 */ 1012 if (saved_loan_count) 1013 return; 1014 } else if (saved_loan_count && pg->uanon) { 1015 /* 1016 * if our page is owned by an anon and is loaned out to the 1017 * kernel then we just want to drop ownership and return. 1018 * the kernel must free the page when all its loans clear ... 1019 * note that the kernel can't change the loan status of our 1020 * page as long as we are holding PQ lock. 1021 */ 1022 atomic_clearbits_int(&pg->pg_flags, PQ_ANON); 1023 pg->uanon->an_page = NULL; 1024 pg->uanon = NULL; 1025 return; 1026 } 1027 KASSERT(saved_loan_count == 0); 1028 1029 /* now remove the page from the queues */ 1030 if (pg->pg_flags & PQ_ACTIVE) { 1031 TAILQ_REMOVE(&uvm.page_active, pg, pageq); 1032 flags_to_clear |= PQ_ACTIVE; 1033 uvmexp.active--; 1034 } 1035 if (pg->pg_flags & PQ_INACTIVE) { 1036 if (pg->pg_flags & PQ_SWAPBACKED) 1037 TAILQ_REMOVE(&uvm.page_inactive_swp, pg, pageq); 1038 else 1039 TAILQ_REMOVE(&uvm.page_inactive_obj, pg, pageq); 1040 flags_to_clear |= PQ_INACTIVE; 1041 uvmexp.inactive--; 1042 } 1043 1044 /* if the page was wired, unwire it now. */ 1045 if (pg->wire_count) { 1046 pg->wire_count = 0; 1047 uvmexp.wired--; 1048 } 1049 if (pg->uanon) { 1050 pg->uanon->an_page = NULL; 1051 pg->uanon = NULL; 1052 flags_to_clear |= PQ_ANON; 1053 } 1054 1055 /* Clean page state bits. */ 1056 flags_to_clear |= PQ_AOBJ; /* XXX: find culprit */ 1057 flags_to_clear |= PQ_ENCRYPT|PG_ZERO|PG_FAKE|PG_BUSY|PG_RELEASED| 1058 PG_CLEAN|PG_CLEANCHK; 1059 atomic_clearbits_int(&pg->pg_flags, flags_to_clear); 1060 1061 /* and put on free queue */ 1062 #ifdef DEBUG 1063 pg->uobject = (void *)0xdeadbeef; 1064 pg->offset = 0xdeadbeef; 1065 pg->uanon = (void *)0xdeadbeef; 1066 #endif 1067 1068 uvm_pmr_freepages(pg, 1); 1069 1070 if (uvmexp.zeropages < UVM_PAGEZERO_TARGET) 1071 uvm.page_idle_zero = vm_page_zero_enable; 1072 } 1073 1074 /* 1075 * uvm_page_unbusy: unbusy an array of pages. 1076 * 1077 * => pages must either all belong to the same object, or all belong to anons. 1078 * => if pages are anon-owned, anons must have 0 refcount. 1079 */ 1080 void 1081 uvm_page_unbusy(struct vm_page **pgs, int npgs) 1082 { 1083 struct vm_page *pg; 1084 struct uvm_object *uobj; 1085 int i; 1086 1087 for (i = 0; i < npgs; i++) { 1088 pg = pgs[i]; 1089 1090 if (pg == NULL || pg == PGO_DONTCARE) { 1091 continue; 1092 } 1093 if (pg->pg_flags & PG_WANTED) { 1094 wakeup(pg); 1095 } 1096 if (pg->pg_flags & PG_RELEASED) { 1097 uobj = pg->uobject; 1098 if (uobj != NULL) { 1099 uvm_lock_pageq(); 1100 pmap_page_protect(pg, VM_PROT_NONE); 1101 /* XXX won't happen right now */ 1102 if (pg->pg_flags & PQ_AOBJ) 1103 uao_dropswap(uobj, 1104 pg->offset >> PAGE_SHIFT); 1105 uvm_pagefree(pg); 1106 uvm_unlock_pageq(); 1107 } else { 1108 atomic_clearbits_int(&pg->pg_flags, PG_BUSY); 1109 UVM_PAGE_OWN(pg, NULL); 1110 uvm_anfree(pg->uanon); 1111 } 1112 } else { 1113 atomic_clearbits_int(&pg->pg_flags, PG_WANTED|PG_BUSY); 1114 UVM_PAGE_OWN(pg, NULL); 1115 } 1116 } 1117 } 1118 1119 #if defined(UVM_PAGE_TRKOWN) 1120 /* 1121 * uvm_page_own: set or release page ownership 1122 * 1123 * => this is a debugging function that keeps track of who sets PG_BUSY 1124 * and where they do it. it can be used to track down problems 1125 * such a process setting "PG_BUSY" and never releasing it. 1126 * => if "tag" is NULL then we are releasing page ownership 1127 */ 1128 void 1129 uvm_page_own(struct vm_page *pg, char *tag) 1130 { 1131 /* gain ownership? */ 1132 if (tag) { 1133 if (pg->owner_tag) { 1134 printf("uvm_page_own: page %p already owned " 1135 "by proc %d [%s]\n", pg, 1136 pg->owner, pg->owner_tag); 1137 panic("uvm_page_own"); 1138 } 1139 pg->owner = (curproc) ? curproc->p_pid : (pid_t) -1; 1140 pg->owner_tag = tag; 1141 return; 1142 } 1143 1144 /* drop ownership */ 1145 if (pg->owner_tag == NULL) { 1146 printf("uvm_page_own: dropping ownership of an non-owned " 1147 "page (%p)\n", pg); 1148 panic("uvm_page_own"); 1149 } 1150 pg->owner_tag = NULL; 1151 return; 1152 } 1153 #endif 1154 1155 /* 1156 * uvm_pageidlezero: zero free pages while the system is idle. 1157 * 1158 * => we do at least one iteration per call, if we are below the target. 1159 * => we loop until we either reach the target or whichqs indicates that 1160 * there is a process ready to run. 1161 */ 1162 void 1163 uvm_pageidlezero(void) 1164 { 1165 #if 0 /* disabled: need new code */ 1166 struct vm_page *pg; 1167 struct pgfreelist *pgfl; 1168 int free_list; 1169 1170 do { 1171 uvm_lock_fpageq(); 1172 1173 if (uvmexp.zeropages >= UVM_PAGEZERO_TARGET) { 1174 uvm.page_idle_zero = FALSE; 1175 uvm_unlock_fpageq(); 1176 return; 1177 } 1178 1179 for (free_list = 0; free_list < VM_NFREELIST; free_list++) { 1180 pgfl = &uvm.page_free[free_list]; 1181 if ((pg = TAILQ_FIRST(&pgfl->pgfl_queues[ 1182 PGFL_UNKNOWN])) != NULL) 1183 break; 1184 } 1185 1186 if (pg == NULL) { 1187 /* 1188 * No non-zero'd pages; don't bother trying again 1189 * until we know we have non-zero'd pages free. 1190 */ 1191 uvm.page_idle_zero = FALSE; 1192 uvm_unlock_fpageq(); 1193 return; 1194 } 1195 1196 TAILQ_REMOVE(&pgfl->pgfl_queues[PGFL_UNKNOWN], pg, pageq); 1197 uvmexp.free--; 1198 uvm_unlock_fpageq(); 1199 1200 #ifdef PMAP_PAGEIDLEZERO 1201 if (PMAP_PAGEIDLEZERO(pg) == FALSE) { 1202 /* 1203 * The machine-dependent code detected some 1204 * reason for us to abort zeroing pages, 1205 * probably because there is a process now 1206 * ready to run. 1207 */ 1208 uvm_lock_fpageq(); 1209 TAILQ_INSERT_HEAD(&pgfl->pgfl_queues[PGFL_UNKNOWN], 1210 pg, pageq); 1211 uvmexp.free++; 1212 uvmexp.zeroaborts++; 1213 uvm_unlock_fpageq(); 1214 return; 1215 } 1216 #else 1217 /* 1218 * XXX This will toast the cache unless the pmap_zero_page() 1219 * XXX implementation does uncached access. 1220 */ 1221 pmap_zero_page(pg); 1222 #endif 1223 atomic_setbits_int(&pg->pg_flags, PG_ZERO); 1224 1225 uvm_lock_fpageq(); 1226 TAILQ_INSERT_HEAD(&pgfl->pgfl_queues[PGFL_ZEROS], pg, pageq); 1227 uvmexp.free++; 1228 uvmexp.zeropages++; 1229 uvm_unlock_fpageq(); 1230 } while (curcpu_is_idle()); 1231 #endif /* 0 */ 1232 } 1233 1234 /* 1235 * when VM_PHYSSEG_MAX is 1, we can simplify these functions 1236 */ 1237 1238 #if VM_PHYSSEG_MAX > 1 1239 /* 1240 * vm_physseg_find: find vm_physseg structure that belongs to a PA 1241 */ 1242 int 1243 vm_physseg_find(paddr_t pframe, int *offp) 1244 { 1245 struct vm_physseg *seg; 1246 1247 #if (VM_PHYSSEG_STRAT == VM_PSTRAT_BSEARCH) 1248 /* binary search for it */ 1249 int start, len, try; 1250 1251 /* 1252 * if try is too large (thus target is less than than try) we reduce 1253 * the length to trunc(len/2) [i.e. everything smaller than "try"] 1254 * 1255 * if the try is too small (thus target is greater than try) then 1256 * we set the new start to be (try + 1). this means we need to 1257 * reduce the length to (round(len/2) - 1). 1258 * 1259 * note "adjust" below which takes advantage of the fact that 1260 * (round(len/2) - 1) == trunc((len - 1) / 2) 1261 * for any value of len we may have 1262 */ 1263 1264 for (start = 0, len = vm_nphysseg ; len != 0 ; len = len / 2) { 1265 try = start + (len / 2); /* try in the middle */ 1266 seg = vm_physmem + try; 1267 1268 /* start past our try? */ 1269 if (pframe >= seg->start) { 1270 /* was try correct? */ 1271 if (pframe < seg->end) { 1272 if (offp) 1273 *offp = pframe - seg->start; 1274 return(try); /* got it */ 1275 } 1276 start = try + 1; /* next time, start here */ 1277 len--; /* "adjust" */ 1278 } else { 1279 /* 1280 * pframe before try, just reduce length of 1281 * region, done in "for" loop 1282 */ 1283 } 1284 } 1285 return(-1); 1286 1287 #else 1288 /* linear search for it */ 1289 int lcv; 1290 1291 for (lcv = 0, seg = vm_physmem; lcv < vm_nphysseg ; lcv++, seg++) { 1292 if (pframe >= seg->start && pframe < seg->end) { 1293 if (offp) 1294 *offp = pframe - seg->start; 1295 return(lcv); /* got it */ 1296 } 1297 } 1298 return(-1); 1299 1300 #endif 1301 } 1302 1303 /* 1304 * PHYS_TO_VM_PAGE: find vm_page for a PA. used by MI code to get vm_pages 1305 * back from an I/O mapping (ugh!). used in some MD code as well. 1306 */ 1307 struct vm_page * 1308 PHYS_TO_VM_PAGE(paddr_t pa) 1309 { 1310 paddr_t pf = atop(pa); 1311 int off; 1312 int psi; 1313 1314 psi = vm_physseg_find(pf, &off); 1315 1316 return ((psi == -1) ? NULL : &vm_physmem[psi].pgs[off]); 1317 } 1318 #endif /* VM_PHYSSEG_MAX > 1 */ 1319 1320 /* 1321 * uvm_pagelookup: look up a page 1322 */ 1323 struct vm_page * 1324 uvm_pagelookup(struct uvm_object *obj, voff_t off) 1325 { 1326 /* XXX if stack is too much, handroll */ 1327 struct vm_page pg; 1328 1329 pg.offset = off; 1330 return (RB_FIND(uvm_objtree, &obj->memt, &pg)); 1331 } 1332 1333 /* 1334 * uvm_pagewire: wire the page, thus removing it from the daemon's grasp 1335 * 1336 * => caller must lock page queues 1337 */ 1338 void 1339 uvm_pagewire(struct vm_page *pg) 1340 { 1341 if (pg->wire_count == 0) { 1342 if (pg->pg_flags & PQ_ACTIVE) { 1343 TAILQ_REMOVE(&uvm.page_active, pg, pageq); 1344 atomic_clearbits_int(&pg->pg_flags, PQ_ACTIVE); 1345 uvmexp.active--; 1346 } 1347 if (pg->pg_flags & PQ_INACTIVE) { 1348 if (pg->pg_flags & PQ_SWAPBACKED) 1349 TAILQ_REMOVE(&uvm.page_inactive_swp, pg, pageq); 1350 else 1351 TAILQ_REMOVE(&uvm.page_inactive_obj, pg, pageq); 1352 atomic_clearbits_int(&pg->pg_flags, PQ_INACTIVE); 1353 uvmexp.inactive--; 1354 } 1355 uvmexp.wired++; 1356 } 1357 pg->wire_count++; 1358 } 1359 1360 /* 1361 * uvm_pageunwire: unwire the page. 1362 * 1363 * => activate if wire count goes to zero. 1364 * => caller must lock page queues 1365 */ 1366 void 1367 uvm_pageunwire(struct vm_page *pg) 1368 { 1369 pg->wire_count--; 1370 if (pg->wire_count == 0) { 1371 TAILQ_INSERT_TAIL(&uvm.page_active, pg, pageq); 1372 uvmexp.active++; 1373 atomic_setbits_int(&pg->pg_flags, PQ_ACTIVE); 1374 uvmexp.wired--; 1375 } 1376 } 1377 1378 /* 1379 * uvm_pagedeactivate: deactivate page -- no pmaps have access to page 1380 * 1381 * => caller must lock page queues 1382 * => caller must check to make sure page is not wired 1383 * => object that page belongs to must be locked (so we can adjust pg->flags) 1384 */ 1385 void 1386 uvm_pagedeactivate(struct vm_page *pg) 1387 { 1388 if (pg->pg_flags & PQ_ACTIVE) { 1389 TAILQ_REMOVE(&uvm.page_active, pg, pageq); 1390 atomic_clearbits_int(&pg->pg_flags, PQ_ACTIVE); 1391 uvmexp.active--; 1392 } 1393 if ((pg->pg_flags & PQ_INACTIVE) == 0) { 1394 KASSERT(pg->wire_count == 0); 1395 if (pg->pg_flags & PQ_SWAPBACKED) 1396 TAILQ_INSERT_TAIL(&uvm.page_inactive_swp, pg, pageq); 1397 else 1398 TAILQ_INSERT_TAIL(&uvm.page_inactive_obj, pg, pageq); 1399 atomic_setbits_int(&pg->pg_flags, PQ_INACTIVE); 1400 uvmexp.inactive++; 1401 pmap_clear_reference(pg); 1402 /* 1403 * update the "clean" bit. this isn't 100% 1404 * accurate, and doesn't have to be. we'll 1405 * re-sync it after we zap all mappings when 1406 * scanning the inactive list. 1407 */ 1408 if ((pg->pg_flags & PG_CLEAN) != 0 && 1409 pmap_is_modified(pg)) 1410 atomic_clearbits_int(&pg->pg_flags, PG_CLEAN); 1411 } 1412 } 1413 1414 /* 1415 * uvm_pageactivate: activate page 1416 * 1417 * => caller must lock page queues 1418 */ 1419 void 1420 uvm_pageactivate(struct vm_page *pg) 1421 { 1422 if (pg->pg_flags & PQ_INACTIVE) { 1423 if (pg->pg_flags & PQ_SWAPBACKED) 1424 TAILQ_REMOVE(&uvm.page_inactive_swp, pg, pageq); 1425 else 1426 TAILQ_REMOVE(&uvm.page_inactive_obj, pg, pageq); 1427 atomic_clearbits_int(&pg->pg_flags, PQ_INACTIVE); 1428 uvmexp.inactive--; 1429 } 1430 if (pg->wire_count == 0) { 1431 /* 1432 * if page is already active, remove it from list so we 1433 * can put it at tail. if it wasn't active, then mark 1434 * it active and bump active count 1435 */ 1436 if (pg->pg_flags & PQ_ACTIVE) 1437 TAILQ_REMOVE(&uvm.page_active, pg, pageq); 1438 else { 1439 atomic_setbits_int(&pg->pg_flags, PQ_ACTIVE); 1440 uvmexp.active++; 1441 } 1442 1443 TAILQ_INSERT_TAIL(&uvm.page_active, pg, pageq); 1444 } 1445 } 1446 1447 /* 1448 * uvm_pagezero: zero fill a page 1449 */ 1450 void 1451 uvm_pagezero(struct vm_page *pg) 1452 { 1453 atomic_clearbits_int(&pg->pg_flags, PG_CLEAN); 1454 pmap_zero_page(pg); 1455 } 1456 1457 /* 1458 * uvm_pagecopy: copy a page 1459 */ 1460 void 1461 uvm_pagecopy(struct vm_page *src, struct vm_page *dst) 1462 { 1463 atomic_clearbits_int(&dst->pg_flags, PG_CLEAN); 1464 pmap_copy_page(src, dst); 1465 } 1466 1467 /* 1468 * uvm_pagecount: count the number of physical pages in the address range. 1469 */ 1470 psize_t 1471 uvm_pagecount(struct uvm_constraint_range* constraint) 1472 { 1473 int lcv; 1474 psize_t sz; 1475 paddr_t low, high; 1476 paddr_t ps_low, ps_high; 1477 1478 /* Algorithm uses page numbers. */ 1479 low = atop(constraint->ucr_low); 1480 high = atop(constraint->ucr_high); 1481 1482 sz = 0; 1483 for (lcv = 0; lcv < vm_nphysseg; lcv++) { 1484 ps_low = MAX(low, vm_physmem[lcv].avail_start); 1485 ps_high = MIN(high, vm_physmem[lcv].avail_end); 1486 if (ps_low < ps_high) 1487 sz += ps_high - ps_low; 1488 } 1489 return sz; 1490 } 1491