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