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