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