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