pmap.c revision 120613
1/*- 2 * Copyright (c) 1991 Regents of the University of California. 3 * All rights reserved. 4 * Copyright (c) 1994 John S. Dyson 5 * All rights reserved. 6 * Copyright (c) 1994 David Greenman 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to Berkeley by 10 * the Systems Programming Group of the University of Utah Computer 11 * Science Department and William Jolitz of UUNET Technologies Inc. 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 the University of 24 * California, Berkeley and 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 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91 42 */ 43/*- 44 * Copyright (c) 2003 Networks Associates Technology, Inc. 45 * All rights reserved. 46 * 47 * This software was developed for the FreeBSD Project by Jake Burkholder, 48 * Safeport Network Services, and Network Associates Laboratories, the 49 * Security Research Division of Network Associates, Inc. under 50 * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA 51 * CHATS research program. 52 * 53 * Redistribution and use in source and binary forms, with or without 54 * modification, are permitted provided that the following conditions 55 * are met: 56 * 1. Redistributions of source code must retain the above copyright 57 * notice, this list of conditions and the following disclaimer. 58 * 2. Redistributions in binary form must reproduce the above copyright 59 * notice, this list of conditions and the following disclaimer in the 60 * documentation and/or other materials provided with the distribution. 61 * 62 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 65 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 72 * SUCH DAMAGE. 73 */ 74 75#include <sys/cdefs.h> 76__FBSDID("$FreeBSD: head/sys/i386/i386/pmap.c 120613 2003-10-01 00:27:09Z jeff $"); 77 78/* 79 * Manages physical address maps. 80 * 81 * In addition to hardware address maps, this 82 * module is called upon to provide software-use-only 83 * maps which may or may not be stored in the same 84 * form as hardware maps. These pseudo-maps are 85 * used to store intermediate results from copy 86 * operations to and from address spaces. 87 * 88 * Since the information managed by this module is 89 * also stored by the logical address mapping module, 90 * this module may throw away valid virtual-to-physical 91 * mappings at almost any time. However, invalidations 92 * of virtual-to-physical mappings must be done as 93 * requested. 94 * 95 * In order to cope with hardware architectures which 96 * make virtual-to-physical map invalidates expensive, 97 * this module may delay invalidate or reduced protection 98 * operations until such time as they are actually 99 * necessary. This module is given full information as 100 * to which processors are currently using which maps, 101 * and to when physical maps must be made correct. 102 */ 103 104#include "opt_pmap.h" 105#include "opt_msgbuf.h" 106#include "opt_kstack_pages.h" 107 108#include <sys/param.h> 109#include <sys/systm.h> 110#include <sys/kernel.h> 111#include <sys/lock.h> 112#include <sys/mman.h> 113#include <sys/msgbuf.h> 114#include <sys/mutex.h> 115#include <sys/proc.h> 116#include <sys/sx.h> 117#include <sys/user.h> 118#include <sys/vmmeter.h> 119#include <sys/sysctl.h> 120#ifdef SMP 121#include <sys/smp.h> 122#endif 123 124#include <vm/vm.h> 125#include <vm/vm_param.h> 126#include <vm/vm_kern.h> 127#include <vm/vm_page.h> 128#include <vm/vm_map.h> 129#include <vm/vm_object.h> 130#include <vm/vm_extern.h> 131#include <vm/vm_pageout.h> 132#include <vm/vm_pager.h> 133#include <vm/uma.h> 134 135#include <machine/cpu.h> 136#include <machine/cputypes.h> 137#include <machine/md_var.h> 138#include <machine/specialreg.h> 139#if defined(SMP) || defined(APIC_IO) 140#include <machine/smp.h> 141#include <machine/apic.h> 142#include <machine/segments.h> 143#include <machine/tss.h> 144#endif /* SMP || APIC_IO */ 145 146#define PMAP_KEEP_PDIRS 147#ifndef PMAP_SHPGPERPROC 148#define PMAP_SHPGPERPROC 200 149#endif 150 151#if defined(DIAGNOSTIC) 152#define PMAP_DIAGNOSTIC 153#endif 154 155#define MINPV 2048 156 157#if !defined(PMAP_DIAGNOSTIC) 158#define PMAP_INLINE __inline 159#else 160#define PMAP_INLINE 161#endif 162 163/* 164 * Get PDEs and PTEs for user/kernel address space 165 */ 166#define pmap_pde(m, v) (&((m)->pm_pdir[(vm_offset_t)(v) >> PDRSHIFT])) 167#define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT]) 168 169#define pmap_pde_v(pte) ((*(int *)pte & PG_V) != 0) 170#define pmap_pte_w(pte) ((*(int *)pte & PG_W) != 0) 171#define pmap_pte_m(pte) ((*(int *)pte & PG_M) != 0) 172#define pmap_pte_u(pte) ((*(int *)pte & PG_A) != 0) 173#define pmap_pte_v(pte) ((*(int *)pte & PG_V) != 0) 174 175#define pmap_pte_set_w(pte, v) ((v)?(*(int *)pte |= PG_W):(*(int *)pte &= ~PG_W)) 176#define pmap_pte_set_prot(pte, v) ((*(int *)pte &= ~PG_PROT), (*(int *)pte |= (v))) 177 178/* 179 * Given a map and a machine independent protection code, 180 * convert to a vax protection code. 181 */ 182#define pte_prot(m, p) (protection_codes[p]) 183static int protection_codes[8]; 184 185struct pmap kernel_pmap_store; 186LIST_HEAD(pmaplist, pmap); 187static struct pmaplist allpmaps; 188static struct mtx allpmaps_lock; 189#ifdef SMP 190static struct mtx lazypmap_lock; 191#endif 192 193vm_paddr_t avail_start; /* PA of first available physical page */ 194vm_paddr_t avail_end; /* PA of last available physical page */ 195vm_offset_t virtual_avail; /* VA of first avail page (after kernel bss) */ 196vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */ 197static boolean_t pmap_initialized = FALSE; /* Has pmap_init completed? */ 198static int pgeflag; /* PG_G or-in */ 199static int pseflag; /* PG_PS or-in */ 200 201static int nkpt; 202vm_offset_t kernel_vm_end; 203extern u_int32_t KERNend; 204 205#ifdef PAE 206static uma_zone_t pdptzone; 207#endif 208 209/* 210 * Data for the pv entry allocation mechanism 211 */ 212static uma_zone_t pvzone; 213static struct vm_object pvzone_obj; 214static int pv_entry_count = 0, pv_entry_max = 0, pv_entry_high_water = 0; 215int pmap_pagedaemon_waken; 216 217/* 218 * All those kernel PT submaps that BSD is so fond of 219 */ 220pt_entry_t *CMAP1 = 0; 221static pt_entry_t *CMAP2, *CMAP3, *ptmmap; 222caddr_t CADDR1 = 0, ptvmmap = 0; 223static caddr_t CADDR2, CADDR3; 224static struct mtx CMAPCADDR12_lock; 225static pt_entry_t *msgbufmap; 226struct msgbuf *msgbufp = 0; 227 228/* 229 * Crashdump maps. 230 */ 231static pt_entry_t *pt_crashdumpmap; 232static caddr_t crashdumpmap; 233 234#ifdef SMP 235extern pt_entry_t *SMPpt; 236#endif 237static pt_entry_t *PMAP1 = 0; 238static pt_entry_t *PADDR1 = 0; 239 240static PMAP_INLINE void free_pv_entry(pv_entry_t pv); 241static pv_entry_t get_pv_entry(void); 242static void i386_protection_init(void); 243static void pmap_clear_ptes(vm_page_t m, int bit) 244 __always_inline; 245 246static int pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t sva); 247static void pmap_remove_page(struct pmap *pmap, vm_offset_t va); 248static int pmap_remove_entry(struct pmap *pmap, vm_page_t m, 249 vm_offset_t va); 250static void pmap_insert_entry(pmap_t pmap, vm_offset_t va, 251 vm_page_t mpte, vm_page_t m); 252 253static vm_page_t pmap_allocpte(pmap_t pmap, vm_offset_t va); 254 255static vm_page_t _pmap_allocpte(pmap_t pmap, unsigned ptepindex); 256static int pmap_unuse_pt(pmap_t, vm_offset_t, vm_page_t); 257static vm_offset_t pmap_kmem_choose(vm_offset_t addr); 258static void *pmap_pv_allocf(uma_zone_t zone, int bytes, u_int8_t *flags, int wait); 259#ifdef PAE 260static void *pmap_pdpt_allocf(uma_zone_t zone, int bytes, u_int8_t *flags, int wait); 261#endif 262 263static pd_entry_t pdir4mb; 264 265CTASSERT(1 << PDESHIFT == sizeof(pd_entry_t)); 266CTASSERT(1 << PTESHIFT == sizeof(pt_entry_t)); 267 268/* 269 * Move the kernel virtual free pointer to the next 270 * 4MB. This is used to help improve performance 271 * by using a large (4MB) page for much of the kernel 272 * (.text, .data, .bss) 273 */ 274static vm_offset_t 275pmap_kmem_choose(vm_offset_t addr) 276{ 277 vm_offset_t newaddr = addr; 278 279#ifdef I686_CPU_not /* Problem seems to have gone away */ 280 /* Deal with un-resolved Pentium4 issues */ 281 if (cpu_class == CPUCLASS_686 && 282 strcmp(cpu_vendor, "GenuineIntel") == 0 && 283 (cpu_id & 0xf00) == 0xf00) 284 return newaddr; 285#endif 286#ifndef DISABLE_PSE 287 if (cpu_feature & CPUID_PSE) 288 newaddr = (addr + (NBPDR - 1)) & ~(NBPDR - 1); 289#endif 290 return newaddr; 291} 292 293/* 294 * Bootstrap the system enough to run with virtual memory. 295 * 296 * On the i386 this is called after mapping has already been enabled 297 * and just syncs the pmap module with what has already been done. 298 * [We can't call it easily with mapping off since the kernel is not 299 * mapped with PA == VA, hence we would have to relocate every address 300 * from the linked base (virtual) address "KERNBASE" to the actual 301 * (physical) address starting relative to 0] 302 */ 303void 304pmap_bootstrap(firstaddr, loadaddr) 305 vm_paddr_t firstaddr; 306 vm_paddr_t loadaddr; 307{ 308 vm_offset_t va; 309 pt_entry_t *pte; 310 int i; 311 312 avail_start = firstaddr; 313 314 /* 315 * XXX The calculation of virtual_avail is wrong. It's NKPT*PAGE_SIZE too 316 * large. It should instead be correctly calculated in locore.s and 317 * not based on 'first' (which is a physical address, not a virtual 318 * address, for the start of unused physical memory). The kernel 319 * page tables are NOT double mapped and thus should not be included 320 * in this calculation. 321 */ 322 virtual_avail = (vm_offset_t) KERNBASE + firstaddr; 323 virtual_avail = pmap_kmem_choose(virtual_avail); 324 325 virtual_end = VM_MAX_KERNEL_ADDRESS; 326 327 /* 328 * Initialize protection array. 329 */ 330 i386_protection_init(); 331 332 /* 333 * Initialize the kernel pmap (which is statically allocated). 334 */ 335 kernel_pmap->pm_pdir = (pd_entry_t *) (KERNBASE + (u_int)IdlePTD); 336#ifdef PAE 337 kernel_pmap->pm_pdpt = (pdpt_entry_t *) (KERNBASE + (u_int)IdlePDPT); 338#endif 339 kernel_pmap->pm_active = -1; /* don't allow deactivation */ 340 TAILQ_INIT(&kernel_pmap->pm_pvlist); 341 LIST_INIT(&allpmaps); 342#ifdef SMP 343 mtx_init(&lazypmap_lock, "lazypmap", NULL, MTX_SPIN); 344#endif 345 mtx_init(&allpmaps_lock, "allpmaps", NULL, MTX_SPIN); 346 mtx_lock_spin(&allpmaps_lock); 347 LIST_INSERT_HEAD(&allpmaps, kernel_pmap, pm_list); 348 mtx_unlock_spin(&allpmaps_lock); 349 nkpt = NKPT; 350 351 /* 352 * Reserve some special page table entries/VA space for temporary 353 * mapping of pages. 354 */ 355#define SYSMAP(c, p, v, n) \ 356 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n); 357 358 va = virtual_avail; 359 pte = vtopte(va); 360 361 /* 362 * CMAP1/CMAP2 are used for zeroing and copying pages. 363 * CMAP3 is used for the idle process page zeroing. 364 */ 365 SYSMAP(caddr_t, CMAP1, CADDR1, 1) 366 SYSMAP(caddr_t, CMAP2, CADDR2, 1) 367 SYSMAP(caddr_t, CMAP3, CADDR3, 1) 368 369 mtx_init(&CMAPCADDR12_lock, "CMAPCADDR12", NULL, MTX_DEF); 370 371 /* 372 * Crashdump maps. 373 */ 374 SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS); 375 376 /* 377 * ptvmmap is used for reading arbitrary physical pages via /dev/mem. 378 * XXX ptmmap is not used. 379 */ 380 SYSMAP(caddr_t, ptmmap, ptvmmap, 1) 381 382 /* 383 * msgbufp is used to map the system message buffer. 384 * XXX msgbufmap is not used. 385 */ 386 SYSMAP(struct msgbuf *, msgbufmap, msgbufp, 387 atop(round_page(MSGBUF_SIZE))) 388 389 /* 390 * ptemap is used for pmap_pte_quick 391 */ 392 SYSMAP(pt_entry_t *, PMAP1, PADDR1, 1); 393 394 virtual_avail = va; 395 396 *CMAP1 = *CMAP2 = 0; 397 for (i = 0; i < NKPT; i++) 398 PTD[i] = 0; 399 400 pgeflag = 0; 401#ifndef DISABLE_PG_G 402 if (cpu_feature & CPUID_PGE) 403 pgeflag = PG_G; 404#endif 405#ifdef I686_CPU_not /* Problem seems to have gone away */ 406 /* Deal with un-resolved Pentium4 issues */ 407 if (cpu_class == CPUCLASS_686 && 408 strcmp(cpu_vendor, "GenuineIntel") == 0 && 409 (cpu_id & 0xf00) == 0xf00) { 410 printf("Warning: Pentium 4 cpu: PG_G disabled (global flag)\n"); 411 pgeflag = 0; 412 } 413#endif 414 415/* 416 * Initialize the 4MB page size flag 417 */ 418 pseflag = 0; 419/* 420 * The 4MB page version of the initial 421 * kernel page mapping. 422 */ 423 pdir4mb = 0; 424 425#ifndef DISABLE_PSE 426 if (cpu_feature & CPUID_PSE) 427 pseflag = PG_PS; 428#endif 429#ifdef I686_CPU_not /* Problem seems to have gone away */ 430 /* Deal with un-resolved Pentium4 issues */ 431 if (cpu_class == CPUCLASS_686 && 432 strcmp(cpu_vendor, "GenuineIntel") == 0 && 433 (cpu_id & 0xf00) == 0xf00) { 434 printf("Warning: Pentium 4 cpu: PG_PS disabled (4MB pages)\n"); 435 pseflag = 0; 436 } 437#endif 438#ifndef DISABLE_PSE 439 if (pseflag) { 440 pd_entry_t ptditmp; 441 /* 442 * Note that we have enabled PSE mode 443 */ 444 ptditmp = *(PTmap + i386_btop(KERNBASE)); 445 ptditmp &= ~(NBPDR - 1); 446 ptditmp |= PG_V | PG_RW | PG_PS | PG_U | pgeflag; 447 pdir4mb = ptditmp; 448 } 449#endif 450#ifndef SMP 451 /* 452 * Turn on PGE/PSE. SMP does this later on since the 453 * 4K page tables are required for AP boot (for now). 454 * XXX fixme. 455 */ 456 pmap_set_opt(); 457#endif 458#ifdef SMP 459 if (cpu_apic_address == 0) 460 panic("pmap_bootstrap: no local apic! (non-SMP hardware?)"); 461 462 /* local apic is mapped on last page */ 463 SMPpt[NPTEPG - 1] = (pt_entry_t)(PG_V | PG_RW | PG_N | pgeflag | 464 (cpu_apic_address & PG_FRAME)); 465#endif 466 invltlb(); 467} 468 469/* 470 * Enable 4MB page mode for MP startup. Turn on PG_G support. 471 * BSP will run this after all the AP's have started up. 472 */ 473void 474pmap_set_opt(void) 475{ 476 pt_entry_t *pte; 477 vm_offset_t va, endva; 478 479 if (pgeflag && (cpu_feature & CPUID_PGE)) { 480 load_cr4(rcr4() | CR4_PGE); 481 invltlb(); /* Insurance */ 482 } 483#ifndef DISABLE_PSE 484 if (pseflag && (cpu_feature & CPUID_PSE)) { 485 load_cr4(rcr4() | CR4_PSE); 486 invltlb(); /* Insurance */ 487 } 488#endif 489 if (PCPU_GET(cpuid) == 0) { 490#ifndef DISABLE_PSE 491 if (pdir4mb) { 492 kernel_pmap->pm_pdir[KPTDI] = PTD[KPTDI] = pdir4mb; 493 invltlb(); /* Insurance */ 494 } 495#endif 496 if (pgeflag) { 497 /* Turn on PG_G for text, data, bss pages. */ 498 va = (vm_offset_t)btext; 499#ifndef DISABLE_PSE 500 if (pseflag && (cpu_feature & CPUID_PSE)) { 501 if (va < KERNBASE + (1 << PDRSHIFT)) 502 va = KERNBASE + (1 << PDRSHIFT); 503 } 504#endif 505 endva = KERNBASE + KERNend; 506 while (va < endva) { 507 pte = vtopte(va); 508 if (*pte) 509 *pte |= pgeflag; 510 va += PAGE_SIZE; 511 } 512 invltlb(); /* Insurance */ 513 } 514 /* 515 * We do not need to broadcast the invltlb here, because 516 * each AP does it the moment it is released from the boot 517 * lock. See ap_init(). 518 */ 519 } 520} 521 522static void * 523pmap_pv_allocf(uma_zone_t zone, int bytes, u_int8_t *flags, int wait) 524{ 525 *flags = UMA_SLAB_PRIV; 526 return (void *)kmem_alloc(kernel_map, bytes); 527} 528 529#ifdef PAE 530static void * 531pmap_pdpt_allocf(uma_zone_t zone, int bytes, u_int8_t *flags, int wait) 532{ 533 *flags = UMA_SLAB_PRIV; 534 return (contigmalloc(PAGE_SIZE, NULL, 0, 0x0ULL, 0xffffffffULL, 1, 0)); 535} 536#endif 537 538/* 539 * Initialize the pmap module. 540 * Called by vm_init, to initialize any structures that the pmap 541 * system needs to map virtual memory. 542 * pmap_init has been enhanced to support in a fairly consistant 543 * way, discontiguous physical memory. 544 */ 545void 546pmap_init(phys_start, phys_end) 547 vm_paddr_t phys_start, phys_end; 548{ 549 int i; 550 int initial_pvs; 551 552 /* 553 * Allocate memory for random pmap data structures. Includes the 554 * pv_head_table. 555 */ 556 557 for(i = 0; i < vm_page_array_size; i++) { 558 vm_page_t m; 559 560 m = &vm_page_array[i]; 561 TAILQ_INIT(&m->md.pv_list); 562 m->md.pv_list_count = 0; 563 } 564 565 /* 566 * init the pv free list 567 */ 568 initial_pvs = vm_page_array_size; 569 if (initial_pvs < MINPV) 570 initial_pvs = MINPV; 571 pvzone = uma_zcreate("PV ENTRY", sizeof (struct pv_entry), NULL, NULL, 572 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM | UMA_ZONE_NOFREE); 573 uma_zone_set_allocf(pvzone, pmap_pv_allocf); 574 uma_prealloc(pvzone, initial_pvs); 575 576#ifdef PAE 577 pdptzone = uma_zcreate("PDPT", NPGPTD * sizeof(pdpt_entry_t), NULL, 578 NULL, NULL, NULL, (NPGPTD * sizeof(pdpt_entry_t)) - 1, 579 UMA_ZONE_VM | UMA_ZONE_NOFREE); 580 uma_zone_set_allocf(pdptzone, pmap_pdpt_allocf); 581#endif 582 583 /* 584 * Now it is safe to enable pv_table recording. 585 */ 586 pmap_initialized = TRUE; 587} 588 589/* 590 * Initialize the address space (zone) for the pv_entries. Set a 591 * high water mark so that the system can recover from excessive 592 * numbers of pv entries. 593 */ 594void 595pmap_init2() 596{ 597 int shpgperproc = PMAP_SHPGPERPROC; 598 599 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc); 600 pv_entry_max = shpgperproc * maxproc + vm_page_array_size; 601 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max); 602 pv_entry_high_water = 9 * (pv_entry_max / 10); 603 uma_zone_set_obj(pvzone, &pvzone_obj, pv_entry_max); 604} 605 606 607/*************************************************** 608 * Low level helper routines..... 609 ***************************************************/ 610 611#if defined(PMAP_DIAGNOSTIC) 612 613/* 614 * This code checks for non-writeable/modified pages. 615 * This should be an invalid condition. 616 */ 617static int 618pmap_nw_modified(pt_entry_t ptea) 619{ 620 int pte; 621 622 pte = (int) ptea; 623 624 if ((pte & (PG_M|PG_RW)) == PG_M) 625 return 1; 626 else 627 return 0; 628} 629#endif 630 631 632/* 633 * this routine defines the region(s) of memory that should 634 * not be tested for the modified bit. 635 */ 636static PMAP_INLINE int 637pmap_track_modified(vm_offset_t va) 638{ 639 if ((va < kmi.clean_sva) || (va >= kmi.clean_eva)) 640 return 1; 641 else 642 return 0; 643} 644 645#ifdef I386_CPU 646/* 647 * i386 only has "invalidate everything" and no SMP to worry about. 648 */ 649PMAP_INLINE void 650pmap_invalidate_page(pmap_t pmap, vm_offset_t va) 651{ 652 653 if (pmap == kernel_pmap || pmap->pm_active) 654 invltlb(); 655} 656 657PMAP_INLINE void 658pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva) 659{ 660 661 if (pmap == kernel_pmap || pmap->pm_active) 662 invltlb(); 663} 664 665PMAP_INLINE void 666pmap_invalidate_all(pmap_t pmap) 667{ 668 669 if (pmap == kernel_pmap || pmap->pm_active) 670 invltlb(); 671} 672#else /* !I386_CPU */ 673#ifdef SMP 674/* 675 * For SMP, these functions have to use the IPI mechanism for coherence. 676 */ 677void 678pmap_invalidate_page(pmap_t pmap, vm_offset_t va) 679{ 680 u_int cpumask; 681 u_int other_cpus; 682 683 critical_enter(); 684 /* 685 * We need to disable interrupt preemption but MUST NOT have 686 * interrupts disabled here. 687 * XXX we may need to hold schedlock to get a coherent pm_active 688 */ 689 if (pmap->pm_active == -1 || pmap->pm_active == all_cpus) { 690 invlpg(va); 691 smp_invlpg(va); 692 } else { 693 cpumask = PCPU_GET(cpumask); 694 other_cpus = PCPU_GET(other_cpus); 695 if (pmap->pm_active & cpumask) 696 invlpg(va); 697 if (pmap->pm_active & other_cpus) 698 smp_masked_invlpg(pmap->pm_active & other_cpus, va); 699 } 700 critical_exit(); 701} 702 703void 704pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva) 705{ 706 u_int cpumask; 707 u_int other_cpus; 708 vm_offset_t addr; 709 710 critical_enter(); 711 /* 712 * We need to disable interrupt preemption but MUST NOT have 713 * interrupts disabled here. 714 * XXX we may need to hold schedlock to get a coherent pm_active 715 */ 716 if (pmap->pm_active == -1 || pmap->pm_active == all_cpus) { 717 for (addr = sva; addr < eva; addr += PAGE_SIZE) 718 invlpg(addr); 719 smp_invlpg_range(sva, eva); 720 } else { 721 cpumask = PCPU_GET(cpumask); 722 other_cpus = PCPU_GET(other_cpus); 723 if (pmap->pm_active & cpumask) 724 for (addr = sva; addr < eva; addr += PAGE_SIZE) 725 invlpg(addr); 726 if (pmap->pm_active & other_cpus) 727 smp_masked_invlpg_range(pmap->pm_active & other_cpus, 728 sva, eva); 729 } 730 critical_exit(); 731} 732 733void 734pmap_invalidate_all(pmap_t pmap) 735{ 736 u_int cpumask; 737 u_int other_cpus; 738 739 critical_enter(); 740 /* 741 * We need to disable interrupt preemption but MUST NOT have 742 * interrupts disabled here. 743 * XXX we may need to hold schedlock to get a coherent pm_active 744 */ 745 if (pmap->pm_active == -1 || pmap->pm_active == all_cpus) { 746 invltlb(); 747 smp_invltlb(); 748 } else { 749 cpumask = PCPU_GET(cpumask); 750 other_cpus = PCPU_GET(other_cpus); 751 if (pmap->pm_active & cpumask) 752 invltlb(); 753 if (pmap->pm_active & other_cpus) 754 smp_masked_invltlb(pmap->pm_active & other_cpus); 755 } 756 critical_exit(); 757} 758#else /* !SMP */ 759/* 760 * Normal, non-SMP, 486+ invalidation functions. 761 * We inline these within pmap.c for speed. 762 */ 763PMAP_INLINE void 764pmap_invalidate_page(pmap_t pmap, vm_offset_t va) 765{ 766 767 if (pmap == kernel_pmap || pmap->pm_active) 768 invlpg(va); 769} 770 771PMAP_INLINE void 772pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva) 773{ 774 vm_offset_t addr; 775 776 if (pmap == kernel_pmap || pmap->pm_active) 777 for (addr = sva; addr < eva; addr += PAGE_SIZE) 778 invlpg(addr); 779} 780 781PMAP_INLINE void 782pmap_invalidate_all(pmap_t pmap) 783{ 784 785 if (pmap == kernel_pmap || pmap->pm_active) 786 invltlb(); 787} 788#endif /* !SMP */ 789#endif /* !I386_CPU */ 790 791/* 792 * Are we current address space or kernel? 793 */ 794static __inline int 795pmap_is_current(pmap_t pmap) 796{ 797 return (pmap == kernel_pmap || 798 (pmap->pm_pdir[PTDPTDI] & PG_FRAME) == (PTDpde[0] & PG_FRAME)); 799} 800 801/* 802 * Super fast pmap_pte routine best used when scanning 803 * the pv lists. This eliminates many coarse-grained 804 * invltlb calls. Note that many of the pv list 805 * scans are across different pmaps. It is very wasteful 806 * to do an entire invltlb for checking a single mapping. 807 */ 808pt_entry_t * 809pmap_pte_quick(pmap, va) 810 register pmap_t pmap; 811 vm_offset_t va; 812{ 813 pd_entry_t newpf; 814 pd_entry_t *pde; 815 816 pde = pmap_pde(pmap, va); 817 if (*pde & PG_PS) 818 return (pde); 819 if (*pde != 0) { 820 /* are we current address space or kernel? */ 821 if (pmap_is_current(pmap)) 822 return vtopte(va); 823 newpf = *pde & PG_FRAME; 824 if (((*PMAP1) & PG_FRAME) != newpf) { 825 *PMAP1 = newpf | PG_RW | PG_V; 826 pmap_invalidate_page(kernel_pmap, (vm_offset_t)PADDR1); 827 } 828 return PADDR1 + (i386_btop(va) & (NPTEPG - 1)); 829 } 830 return (0); 831} 832 833/* 834 * Routine: pmap_extract 835 * Function: 836 * Extract the physical page address associated 837 * with the given map/virtual_address pair. 838 */ 839vm_paddr_t 840pmap_extract(pmap, va) 841 register pmap_t pmap; 842 vm_offset_t va; 843{ 844 vm_paddr_t rtval; 845 pt_entry_t *pte; 846 pd_entry_t pde; 847 848 if (pmap == 0) 849 return 0; 850 pde = pmap->pm_pdir[va >> PDRSHIFT]; 851 if (pde != 0) { 852 if ((pde & PG_PS) != 0) { 853 rtval = (pde & ~PDRMASK) | (va & PDRMASK); 854 return rtval; 855 } 856 pte = pmap_pte_quick(pmap, va); 857 rtval = ((*pte & PG_FRAME) | (va & PAGE_MASK)); 858 return rtval; 859 } 860 return 0; 861 862} 863 864/* 865 * Routine: pmap_extract_and_hold 866 * Function: 867 * Atomically extract and hold the physical page 868 * with the given pmap and virtual address pair 869 * if that mapping permits the given protection. 870 */ 871vm_page_t 872pmap_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot) 873{ 874 vm_paddr_t pa; 875 vm_page_t m; 876 877 m = NULL; 878 mtx_lock(&Giant); 879 if ((pa = pmap_extract(pmap, va)) != 0) { 880 m = PHYS_TO_VM_PAGE(pa); 881 vm_page_lock_queues(); 882 vm_page_hold(m); 883 vm_page_unlock_queues(); 884 } 885 mtx_unlock(&Giant); 886 return (m); 887} 888 889/*************************************************** 890 * Low level mapping routines..... 891 ***************************************************/ 892 893/* 894 * Add a wired page to the kva. 895 * Note: not SMP coherent. 896 */ 897PMAP_INLINE void 898pmap_kenter(vm_offset_t va, vm_paddr_t pa) 899{ 900 pt_entry_t *pte; 901 902 pte = vtopte(va); 903 pte_store(pte, pa | PG_RW | PG_V | pgeflag); 904} 905 906/* 907 * Remove a page from the kernel pagetables. 908 * Note: not SMP coherent. 909 */ 910PMAP_INLINE void 911pmap_kremove(vm_offset_t va) 912{ 913 pt_entry_t *pte; 914 915 pte = vtopte(va); 916 pte_clear(pte); 917} 918 919/* 920 * Used to map a range of physical addresses into kernel 921 * virtual address space. 922 * 923 * The value passed in '*virt' is a suggested virtual address for 924 * the mapping. Architectures which can support a direct-mapped 925 * physical to virtual region can return the appropriate address 926 * within that region, leaving '*virt' unchanged. Other 927 * architectures should map the pages starting at '*virt' and 928 * update '*virt' with the first usable address after the mapped 929 * region. 930 */ 931vm_offset_t 932pmap_map(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end, int prot) 933{ 934 vm_offset_t va, sva; 935 936 va = sva = *virt; 937 while (start < end) { 938 pmap_kenter(va, start); 939 va += PAGE_SIZE; 940 start += PAGE_SIZE; 941 } 942 pmap_invalidate_range(kernel_pmap, sva, va); 943 *virt = va; 944 return (sva); 945} 946 947 948/* 949 * Add a list of wired pages to the kva 950 * this routine is only used for temporary 951 * kernel mappings that do not need to have 952 * page modification or references recorded. 953 * Note that old mappings are simply written 954 * over. The page *must* be wired. 955 * Note: SMP coherent. Uses a ranged shootdown IPI. 956 */ 957void 958pmap_qenter(vm_offset_t sva, vm_page_t *m, int count) 959{ 960 vm_offset_t va; 961 962 va = sva; 963 while (count-- > 0) { 964 pmap_kenter(va, VM_PAGE_TO_PHYS(*m)); 965 va += PAGE_SIZE; 966 m++; 967 } 968 pmap_invalidate_range(kernel_pmap, sva, va); 969} 970 971/* 972 * This routine tears out page mappings from the 973 * kernel -- it is meant only for temporary mappings. 974 * Note: SMP coherent. Uses a ranged shootdown IPI. 975 */ 976void 977pmap_qremove(vm_offset_t sva, int count) 978{ 979 vm_offset_t va; 980 981 va = sva; 982 while (count-- > 0) { 983 pmap_kremove(va); 984 va += PAGE_SIZE; 985 } 986 pmap_invalidate_range(kernel_pmap, sva, va); 987} 988 989/*************************************************** 990 * Page table page management routines..... 991 ***************************************************/ 992 993/* 994 * This routine unholds page table pages, and if the hold count 995 * drops to zero, then it decrements the wire count. 996 */ 997static int 998_pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m) 999{ 1000 1001 while (vm_page_sleep_if_busy(m, FALSE, "pmuwpt")) 1002 vm_page_lock_queues(); 1003 1004 if (m->hold_count == 0) { 1005 vm_offset_t pteva; 1006 /* 1007 * unmap the page table page 1008 */ 1009 pmap->pm_pdir[m->pindex] = 0; 1010 --pmap->pm_stats.resident_count; 1011 if (pmap_is_current(pmap)) { 1012 /* 1013 * Do an invltlb to make the invalidated mapping 1014 * take effect immediately. 1015 */ 1016 pteva = VM_MAXUSER_ADDRESS + i386_ptob(m->pindex); 1017 pmap_invalidate_page(pmap, pteva); 1018 } 1019 1020 /* 1021 * If the page is finally unwired, simply free it. 1022 */ 1023 --m->wire_count; 1024 if (m->wire_count == 0) { 1025 vm_page_busy(m); 1026 vm_page_free_zero(m); 1027 atomic_subtract_int(&cnt.v_wire_count, 1); 1028 } 1029 return 1; 1030 } 1031 return 0; 1032} 1033 1034static PMAP_INLINE int 1035pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m) 1036{ 1037 vm_page_unhold(m); 1038 if (m->hold_count == 0) 1039 return _pmap_unwire_pte_hold(pmap, m); 1040 else 1041 return 0; 1042} 1043 1044/* 1045 * After removing a page table entry, this routine is used to 1046 * conditionally free the page, and manage the hold/wire counts. 1047 */ 1048static int 1049pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte) 1050{ 1051 1052 if (va >= VM_MAXUSER_ADDRESS) 1053 return 0; 1054 1055 return pmap_unwire_pte_hold(pmap, mpte); 1056} 1057 1058void 1059pmap_pinit0(pmap) 1060 struct pmap *pmap; 1061{ 1062 1063 pmap->pm_pdir = (pd_entry_t *)(KERNBASE + (vm_offset_t)IdlePTD); 1064#ifdef PAE 1065 pmap->pm_pdpt = (pdpt_entry_t *)(KERNBASE + (vm_offset_t)IdlePDPT); 1066#endif 1067 pmap->pm_active = 0; 1068 PCPU_SET(curpmap, pmap); 1069 TAILQ_INIT(&pmap->pm_pvlist); 1070 bzero(&pmap->pm_stats, sizeof pmap->pm_stats); 1071 mtx_lock_spin(&allpmaps_lock); 1072 LIST_INSERT_HEAD(&allpmaps, pmap, pm_list); 1073 mtx_unlock_spin(&allpmaps_lock); 1074} 1075 1076/* 1077 * Initialize a preallocated and zeroed pmap structure, 1078 * such as one in a vmspace structure. 1079 */ 1080void 1081pmap_pinit(pmap) 1082 register struct pmap *pmap; 1083{ 1084 vm_page_t m, ptdpg[NPGPTD]; 1085 vm_paddr_t pa; 1086 static int color; 1087 int i; 1088 1089 /* 1090 * No need to allocate page table space yet but we do need a valid 1091 * page directory table. 1092 */ 1093 if (pmap->pm_pdir == NULL) { 1094 pmap->pm_pdir = (pd_entry_t *)kmem_alloc_nofault(kernel_map, 1095 NBPTD); 1096#ifdef PAE 1097 pmap->pm_pdpt = uma_zalloc(pdptzone, M_WAITOK | M_ZERO); 1098 KASSERT(((vm_offset_t)pmap->pm_pdpt & 1099 ((NPGPTD * sizeof(pdpt_entry_t)) - 1)) == 0, 1100 ("pmap_pinit: pdpt misaligned")); 1101 KASSERT(pmap_kextract((vm_offset_t)pmap->pm_pdpt) < (4ULL<<30), 1102 ("pmap_pinit: pdpt above 4g")); 1103#endif 1104 } 1105 1106 /* 1107 * allocate the page directory page(s) 1108 */ 1109 for (i = 0; i < NPGPTD;) { 1110 m = vm_page_alloc(NULL, color++, 1111 VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | 1112 VM_ALLOC_ZERO); 1113 if (m == NULL) 1114 VM_WAIT; 1115 else { 1116 vm_page_lock_queues(); 1117 vm_page_flag_clear(m, PG_BUSY); 1118 m->valid = VM_PAGE_BITS_ALL; 1119 vm_page_unlock_queues(); 1120 ptdpg[i++] = m; 1121 } 1122 } 1123 1124 pmap_qenter((vm_offset_t)pmap->pm_pdir, ptdpg, NPGPTD); 1125 1126 for (i = 0; i < NPGPTD; i++) { 1127 if ((ptdpg[i]->flags & PG_ZERO) == 0) 1128 bzero(pmap->pm_pdir + (i * NPDEPG), PAGE_SIZE); 1129 } 1130 1131 mtx_lock_spin(&allpmaps_lock); 1132 LIST_INSERT_HEAD(&allpmaps, pmap, pm_list); 1133 mtx_unlock_spin(&allpmaps_lock); 1134 /* Wire in kernel global address entries. */ 1135 /* XXX copies current process, does not fill in MPPTDI */ 1136 bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * sizeof(pd_entry_t)); 1137#ifdef SMP 1138 pmap->pm_pdir[MPPTDI] = PTD[MPPTDI]; 1139#endif 1140 1141 /* install self-referential address mapping entry(s) */ 1142 for (i = 0; i < NPGPTD; i++) { 1143 pa = VM_PAGE_TO_PHYS(ptdpg[i]); 1144 pmap->pm_pdir[PTDPTDI + i] = pa | PG_V | PG_RW | PG_A | PG_M; 1145#ifdef PAE 1146 pmap->pm_pdpt[i] = pa | PG_V; 1147#endif 1148 } 1149 1150 pmap->pm_active = 0; 1151 TAILQ_INIT(&pmap->pm_pvlist); 1152 bzero(&pmap->pm_stats, sizeof pmap->pm_stats); 1153} 1154 1155/* 1156 * Wire in kernel global address entries. To avoid a race condition 1157 * between pmap initialization and pmap_growkernel, this procedure 1158 * should be called after the vmspace is attached to the process 1159 * but before this pmap is activated. 1160 */ 1161void 1162pmap_pinit2(pmap) 1163 struct pmap *pmap; 1164{ 1165 /* XXX: Remove this stub when no longer called */ 1166} 1167 1168/* 1169 * this routine is called if the page table page is not 1170 * mapped correctly. 1171 */ 1172static vm_page_t 1173_pmap_allocpte(pmap, ptepindex) 1174 pmap_t pmap; 1175 unsigned ptepindex; 1176{ 1177 vm_paddr_t ptepa; 1178 vm_page_t m; 1179 1180 /* 1181 * Allocate a page table page. 1182 */ 1183 if ((m = vm_page_alloc(NULL, ptepindex, VM_ALLOC_NOOBJ | 1184 VM_ALLOC_WIRED | VM_ALLOC_ZERO)) == NULL) { 1185 VM_WAIT; 1186 /* 1187 * Indicate the need to retry. While waiting, the page table 1188 * page may have been allocated. 1189 */ 1190 return (NULL); 1191 } 1192 if ((m->flags & PG_ZERO) == 0) 1193 pmap_zero_page(m); 1194 1195 KASSERT(m->queue == PQ_NONE, 1196 ("_pmap_allocpte: %p->queue != PQ_NONE", m)); 1197 1198 /* 1199 * Increment the hold count for the page table page 1200 * (denoting a new mapping.) 1201 */ 1202 m->hold_count++; 1203 1204 /* 1205 * Map the pagetable page into the process address space, if 1206 * it isn't already there. 1207 */ 1208 1209 pmap->pm_stats.resident_count++; 1210 1211 ptepa = VM_PAGE_TO_PHYS(m); 1212 pmap->pm_pdir[ptepindex] = 1213 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_A | PG_M); 1214 1215 vm_page_lock_queues(); 1216 m->valid = VM_PAGE_BITS_ALL; 1217 vm_page_flag_clear(m, PG_ZERO); 1218 vm_page_wakeup(m); 1219 vm_page_unlock_queues(); 1220 1221 return m; 1222} 1223 1224static vm_page_t 1225pmap_allocpte(pmap_t pmap, vm_offset_t va) 1226{ 1227 unsigned ptepindex; 1228 pd_entry_t ptepa; 1229 vm_page_t m; 1230 1231 /* 1232 * Calculate pagetable page index 1233 */ 1234 ptepindex = va >> PDRSHIFT; 1235retry: 1236 /* 1237 * Get the page directory entry 1238 */ 1239 ptepa = pmap->pm_pdir[ptepindex]; 1240 1241 /* 1242 * This supports switching from a 4MB page to a 1243 * normal 4K page. 1244 */ 1245 if (ptepa & PG_PS) { 1246 pmap->pm_pdir[ptepindex] = 0; 1247 ptepa = 0; 1248 pmap_invalidate_all(kernel_pmap); 1249 } 1250 1251 /* 1252 * If the page table page is mapped, we just increment the 1253 * hold count, and activate it. 1254 */ 1255 if (ptepa) { 1256 m = PHYS_TO_VM_PAGE(ptepa); 1257 m->hold_count++; 1258 } else { 1259 /* 1260 * Here if the pte page isn't mapped, or if it has 1261 * been deallocated. 1262 */ 1263 m = _pmap_allocpte(pmap, ptepindex); 1264 if (m == NULL) 1265 goto retry; 1266 } 1267 return (m); 1268} 1269 1270 1271/*************************************************** 1272* Pmap allocation/deallocation routines. 1273 ***************************************************/ 1274 1275#ifdef SMP 1276/* 1277 * Deal with a SMP shootdown of other users of the pmap that we are 1278 * trying to dispose of. This can be a bit hairy. 1279 */ 1280static u_int *lazymask; 1281static u_int lazyptd; 1282static volatile u_int lazywait; 1283 1284void pmap_lazyfix_action(void); 1285 1286void 1287pmap_lazyfix_action(void) 1288{ 1289 u_int mymask = PCPU_GET(cpumask); 1290 1291 if (rcr3() == lazyptd) 1292 load_cr3(PCPU_GET(curpcb)->pcb_cr3); 1293 atomic_clear_int(lazymask, mymask); 1294 atomic_store_rel_int(&lazywait, 1); 1295} 1296 1297static void 1298pmap_lazyfix_self(u_int mymask) 1299{ 1300 1301 if (rcr3() == lazyptd) 1302 load_cr3(PCPU_GET(curpcb)->pcb_cr3); 1303 atomic_clear_int(lazymask, mymask); 1304} 1305 1306 1307static void 1308pmap_lazyfix(pmap_t pmap) 1309{ 1310 u_int mymask = PCPU_GET(cpumask); 1311 u_int mask; 1312 register u_int spins; 1313 1314 while ((mask = pmap->pm_active) != 0) { 1315 spins = 50000000; 1316 mask = mask & -mask; /* Find least significant set bit */ 1317 mtx_lock_spin(&lazypmap_lock); 1318#ifdef PAE 1319 lazyptd = vtophys(pmap->pm_pdpt); 1320#else 1321 lazyptd = vtophys(pmap->pm_pdir); 1322#endif 1323 if (mask == mymask) { 1324 lazymask = &pmap->pm_active; 1325 pmap_lazyfix_self(mymask); 1326 } else { 1327 atomic_store_rel_int((u_int *)&lazymask, 1328 (u_int)&pmap->pm_active); 1329 atomic_store_rel_int(&lazywait, 0); 1330 ipi_selected(mask, IPI_LAZYPMAP); 1331 while (lazywait == 0) { 1332 ia32_pause(); 1333 if (--spins == 0) 1334 break; 1335 } 1336 } 1337 mtx_unlock_spin(&lazypmap_lock); 1338 if (spins == 0) 1339 printf("pmap_lazyfix: spun for 50000000\n"); 1340 } 1341} 1342 1343#else /* SMP */ 1344 1345/* 1346 * Cleaning up on uniprocessor is easy. For various reasons, we're 1347 * unlikely to have to even execute this code, including the fact 1348 * that the cleanup is deferred until the parent does a wait(2), which 1349 * means that another userland process has run. 1350 */ 1351static void 1352pmap_lazyfix(pmap_t pmap) 1353{ 1354 u_int cr3; 1355 1356 cr3 = vtophys(pmap->pm_pdir); 1357 if (cr3 == rcr3()) { 1358 load_cr3(PCPU_GET(curpcb)->pcb_cr3); 1359 pmap->pm_active &= ~(PCPU_GET(cpumask)); 1360 } 1361} 1362#endif /* SMP */ 1363 1364/* 1365 * Release any resources held by the given physical map. 1366 * Called when a pmap initialized by pmap_pinit is being released. 1367 * Should only be called if the map contains no valid mappings. 1368 */ 1369void 1370pmap_release(pmap_t pmap) 1371{ 1372 vm_page_t m, ptdpg[NPGPTD]; 1373 int i; 1374 1375 KASSERT(pmap->pm_stats.resident_count == 0, 1376 ("pmap_release: pmap resident count %ld != 0", 1377 pmap->pm_stats.resident_count)); 1378 1379 pmap_lazyfix(pmap); 1380 mtx_lock_spin(&allpmaps_lock); 1381 LIST_REMOVE(pmap, pm_list); 1382 mtx_unlock_spin(&allpmaps_lock); 1383 1384 for (i = 0; i < NPGPTD; i++) 1385 ptdpg[i] = PHYS_TO_VM_PAGE(pmap->pm_pdir[PTDPTDI + i]); 1386 1387 bzero(pmap->pm_pdir + PTDPTDI, (nkpt + NPGPTD) * 1388 sizeof(*pmap->pm_pdir)); 1389#ifdef SMP 1390 pmap->pm_pdir[MPPTDI] = 0; 1391#endif 1392 1393 pmap_qremove((vm_offset_t)pmap->pm_pdir, NPGPTD); 1394 1395 vm_page_lock_queues(); 1396 for (i = 0; i < NPGPTD; i++) { 1397 m = ptdpg[i]; 1398#ifdef PAE 1399 KASSERT(VM_PAGE_TO_PHYS(m) == (pmap->pm_pdpt[i] & PG_FRAME), 1400 ("pmap_release: got wrong ptd page")); 1401#endif 1402 m->wire_count--; 1403 atomic_subtract_int(&cnt.v_wire_count, 1); 1404 vm_page_busy(m); 1405 vm_page_free_zero(m); 1406 } 1407 vm_page_unlock_queues(); 1408} 1409 1410static int 1411kvm_size(SYSCTL_HANDLER_ARGS) 1412{ 1413 unsigned long ksize = VM_MAX_KERNEL_ADDRESS - KERNBASE; 1414 1415 return sysctl_handle_long(oidp, &ksize, 0, req); 1416} 1417SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD, 1418 0, 0, kvm_size, "IU", "Size of KVM"); 1419 1420static int 1421kvm_free(SYSCTL_HANDLER_ARGS) 1422{ 1423 unsigned long kfree = VM_MAX_KERNEL_ADDRESS - kernel_vm_end; 1424 1425 return sysctl_handle_long(oidp, &kfree, 0, req); 1426} 1427SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD, 1428 0, 0, kvm_free, "IU", "Amount of KVM free"); 1429 1430/* 1431 * grow the number of kernel page table entries, if needed 1432 */ 1433void 1434pmap_growkernel(vm_offset_t addr) 1435{ 1436 struct pmap *pmap; 1437 int s; 1438 vm_paddr_t ptppaddr; 1439 vm_page_t nkpg; 1440 pd_entry_t newpdir; 1441 pt_entry_t *pde; 1442 1443 s = splhigh(); 1444 mtx_assert(&kernel_map->system_mtx, MA_OWNED); 1445 if (kernel_vm_end == 0) { 1446 kernel_vm_end = KERNBASE; 1447 nkpt = 0; 1448 while (pdir_pde(PTD, kernel_vm_end)) { 1449 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1); 1450 nkpt++; 1451 } 1452 } 1453 addr = roundup2(addr, PAGE_SIZE * NPTEPG); 1454 while (kernel_vm_end < addr) { 1455 if (pdir_pde(PTD, kernel_vm_end)) { 1456 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1); 1457 continue; 1458 } 1459 1460 /* 1461 * This index is bogus, but out of the way 1462 */ 1463 nkpg = vm_page_alloc(NULL, nkpt, 1464 VM_ALLOC_NOOBJ | VM_ALLOC_SYSTEM | VM_ALLOC_WIRED); 1465 if (!nkpg) 1466 panic("pmap_growkernel: no memory to grow kernel"); 1467 1468 nkpt++; 1469 1470 pmap_zero_page(nkpg); 1471 ptppaddr = VM_PAGE_TO_PHYS(nkpg); 1472 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M); 1473 pdir_pde(PTD, kernel_vm_end) = newpdir; 1474 1475 mtx_lock_spin(&allpmaps_lock); 1476 LIST_FOREACH(pmap, &allpmaps, pm_list) { 1477 pde = pmap_pde(pmap, kernel_vm_end); 1478 pde_store(pde, newpdir); 1479 } 1480 mtx_unlock_spin(&allpmaps_lock); 1481 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1); 1482 } 1483 splx(s); 1484} 1485 1486 1487/*************************************************** 1488 * page management routines. 1489 ***************************************************/ 1490 1491/* 1492 * free the pv_entry back to the free list 1493 */ 1494static PMAP_INLINE void 1495free_pv_entry(pv_entry_t pv) 1496{ 1497 pv_entry_count--; 1498 uma_zfree(pvzone, pv); 1499} 1500 1501/* 1502 * get a new pv_entry, allocating a block from the system 1503 * when needed. 1504 * the memory allocation is performed bypassing the malloc code 1505 * because of the possibility of allocations at interrupt time. 1506 */ 1507static pv_entry_t 1508get_pv_entry(void) 1509{ 1510 pv_entry_count++; 1511 if (pv_entry_high_water && 1512 (pv_entry_count > pv_entry_high_water) && 1513 (pmap_pagedaemon_waken == 0)) { 1514 pmap_pagedaemon_waken = 1; 1515 wakeup (&vm_pages_needed); 1516 } 1517 return uma_zalloc(pvzone, M_NOWAIT); 1518} 1519 1520/* 1521 * If it is the first entry on the list, it is actually 1522 * in the header and we must copy the following entry up 1523 * to the header. Otherwise we must search the list for 1524 * the entry. In either case we free the now unused entry. 1525 */ 1526 1527static int 1528pmap_remove_entry(pmap_t pmap, vm_page_t m, vm_offset_t va) 1529{ 1530 pv_entry_t pv; 1531 int rtval; 1532 int s; 1533 1534 s = splvm(); 1535 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1536 if (m->md.pv_list_count < pmap->pm_stats.resident_count) { 1537 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) { 1538 if (pmap == pv->pv_pmap && va == pv->pv_va) 1539 break; 1540 } 1541 } else { 1542 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) { 1543 if (va == pv->pv_va) 1544 break; 1545 } 1546 } 1547 1548 rtval = 0; 1549 if (pv) { 1550 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem); 1551 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list); 1552 m->md.pv_list_count--; 1553 if (TAILQ_FIRST(&m->md.pv_list) == NULL) 1554 vm_page_flag_clear(m, PG_WRITEABLE); 1555 1556 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist); 1557 free_pv_entry(pv); 1558 } 1559 1560 splx(s); 1561 return rtval; 1562} 1563 1564/* 1565 * Create a pv entry for page at pa for 1566 * (pmap, va). 1567 */ 1568static void 1569pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m) 1570{ 1571 1572 int s; 1573 pv_entry_t pv; 1574 1575 s = splvm(); 1576 pv = get_pv_entry(); 1577 pv->pv_va = va; 1578 pv->pv_pmap = pmap; 1579 pv->pv_ptem = mpte; 1580 1581 vm_page_lock_queues(); 1582 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist); 1583 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list); 1584 m->md.pv_list_count++; 1585 1586 vm_page_unlock_queues(); 1587 splx(s); 1588} 1589 1590/* 1591 * pmap_remove_pte: do the things to unmap a page in a process 1592 */ 1593static int 1594pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t va) 1595{ 1596 pt_entry_t oldpte; 1597 vm_page_t m, mpte; 1598 1599 oldpte = pte_load_clear(ptq); 1600 if (oldpte & PG_W) 1601 pmap->pm_stats.wired_count -= 1; 1602 /* 1603 * Machines that don't support invlpg, also don't support 1604 * PG_G. 1605 */ 1606 if (oldpte & PG_G) 1607 pmap_invalidate_page(kernel_pmap, va); 1608 pmap->pm_stats.resident_count -= 1; 1609 if (oldpte & PG_MANAGED) { 1610 m = PHYS_TO_VM_PAGE(oldpte); 1611 if (oldpte & PG_M) { 1612#if defined(PMAP_DIAGNOSTIC) 1613 if (pmap_nw_modified((pt_entry_t) oldpte)) { 1614 printf( 1615 "pmap_remove: modified page not writable: va: 0x%x, pte: 0x%x\n", 1616 va, oldpte); 1617 } 1618#endif 1619 if (pmap_track_modified(va)) 1620 vm_page_dirty(m); 1621 } 1622 if (oldpte & PG_A) 1623 vm_page_flag_set(m, PG_REFERENCED); 1624 return pmap_remove_entry(pmap, m, va); 1625 } else { 1626 mpte = PHYS_TO_VM_PAGE(*pmap_pde(pmap, va)); 1627 return pmap_unuse_pt(pmap, va, mpte); 1628 } 1629} 1630 1631/* 1632 * Remove a single page from a process address space 1633 */ 1634static void 1635pmap_remove_page(pmap_t pmap, vm_offset_t va) 1636{ 1637 pt_entry_t *pte; 1638 1639 if ((pte = pmap_pte_quick(pmap, va)) == NULL || *pte == 0) 1640 return; 1641 pmap_remove_pte(pmap, pte, va); 1642 pmap_invalidate_page(pmap, va); 1643} 1644 1645/* 1646 * Remove the given range of addresses from the specified map. 1647 * 1648 * It is assumed that the start and end are properly 1649 * rounded to the page size. 1650 */ 1651void 1652pmap_remove(pmap_t pmap, vm_offset_t sva, vm_offset_t eva) 1653{ 1654 vm_offset_t pdnxt; 1655 pd_entry_t ptpaddr; 1656 pt_entry_t *pte; 1657 int anyvalid; 1658 1659 if (pmap == NULL) 1660 return; 1661 1662 if (pmap->pm_stats.resident_count == 0) 1663 return; 1664 1665 /* 1666 * special handling of removing one page. a very 1667 * common operation and easy to short circuit some 1668 * code. 1669 */ 1670 if ((sva + PAGE_SIZE == eva) && 1671 ((pmap->pm_pdir[(sva >> PDRSHIFT)] & PG_PS) == 0)) { 1672 pmap_remove_page(pmap, sva); 1673 return; 1674 } 1675 1676 anyvalid = 0; 1677 1678 for (; sva < eva; sva = pdnxt) { 1679 unsigned pdirindex; 1680 1681 /* 1682 * Calculate index for next page table. 1683 */ 1684 pdnxt = (sva + NBPDR) & ~PDRMASK; 1685 if (pmap->pm_stats.resident_count == 0) 1686 break; 1687 1688 pdirindex = sva >> PDRSHIFT; 1689 ptpaddr = pmap->pm_pdir[pdirindex]; 1690 1691 /* 1692 * Weed out invalid mappings. Note: we assume that the page 1693 * directory table is always allocated, and in kernel virtual. 1694 */ 1695 if (ptpaddr == 0) 1696 continue; 1697 1698 /* 1699 * Check for large page. 1700 */ 1701 if ((ptpaddr & PG_PS) != 0) { 1702 pmap->pm_pdir[pdirindex] = 0; 1703 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE; 1704 anyvalid = 1; 1705 continue; 1706 } 1707 1708 /* 1709 * Limit our scan to either the end of the va represented 1710 * by the current page table page, or to the end of the 1711 * range being removed. 1712 */ 1713 if (pdnxt > eva) 1714 pdnxt = eva; 1715 1716 for (; sva != pdnxt; sva += PAGE_SIZE) { 1717 if ((pte = pmap_pte_quick(pmap, sva)) == NULL || 1718 *pte == 0) 1719 continue; 1720 anyvalid = 1; 1721 if (pmap_remove_pte(pmap, pte, sva)) 1722 break; 1723 } 1724 } 1725 1726 if (anyvalid) 1727 pmap_invalidate_all(pmap); 1728} 1729 1730/* 1731 * Routine: pmap_remove_all 1732 * Function: 1733 * Removes this physical page from 1734 * all physical maps in which it resides. 1735 * Reflects back modify bits to the pager. 1736 * 1737 * Notes: 1738 * Original versions of this routine were very 1739 * inefficient because they iteratively called 1740 * pmap_remove (slow...) 1741 */ 1742 1743void 1744pmap_remove_all(vm_page_t m) 1745{ 1746 register pv_entry_t pv; 1747 pt_entry_t *pte, tpte; 1748 int s; 1749 1750#if defined(PMAP_DIAGNOSTIC) 1751 /* 1752 * XXX This makes pmap_remove_all() illegal for non-managed pages! 1753 */ 1754 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) { 1755 panic("pmap_remove_all: illegal for unmanaged page, va: 0x%x", 1756 VM_PAGE_TO_PHYS(m)); 1757 } 1758#endif 1759 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1760 s = splvm(); 1761 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) { 1762 pv->pv_pmap->pm_stats.resident_count--; 1763 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va); 1764 tpte = pte_load_clear(pte); 1765 if (tpte & PG_W) 1766 pv->pv_pmap->pm_stats.wired_count--; 1767 if (tpte & PG_A) 1768 vm_page_flag_set(m, PG_REFERENCED); 1769 1770 /* 1771 * Update the vm_page_t clean and reference bits. 1772 */ 1773 if (tpte & PG_M) { 1774#if defined(PMAP_DIAGNOSTIC) 1775 if (pmap_nw_modified((pt_entry_t) tpte)) { 1776 printf( 1777 "pmap_remove_all: modified page not writable: va: 0x%x, pte: 0x%x\n", 1778 pv->pv_va, tpte); 1779 } 1780#endif 1781 if (pmap_track_modified(pv->pv_va)) 1782 vm_page_dirty(m); 1783 } 1784 pmap_invalidate_page(pv->pv_pmap, pv->pv_va); 1785 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist); 1786 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list); 1787 m->md.pv_list_count--; 1788 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem); 1789 free_pv_entry(pv); 1790 } 1791 vm_page_flag_clear(m, PG_WRITEABLE); 1792 splx(s); 1793} 1794 1795/* 1796 * Set the physical protection on the 1797 * specified range of this map as requested. 1798 */ 1799void 1800pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot) 1801{ 1802 vm_offset_t pdnxt; 1803 pd_entry_t ptpaddr; 1804 int anychanged; 1805 1806 if (pmap == NULL) 1807 return; 1808 1809 if ((prot & VM_PROT_READ) == VM_PROT_NONE) { 1810 pmap_remove(pmap, sva, eva); 1811 return; 1812 } 1813 1814 if (prot & VM_PROT_WRITE) 1815 return; 1816 1817 anychanged = 0; 1818 1819 for (; sva < eva; sva = pdnxt) { 1820 unsigned pdirindex; 1821 1822 pdnxt = (sva + NBPDR) & ~PDRMASK; 1823 1824 pdirindex = sva >> PDRSHIFT; 1825 ptpaddr = pmap->pm_pdir[pdirindex]; 1826 1827 /* 1828 * Weed out invalid mappings. Note: we assume that the page 1829 * directory table is always allocated, and in kernel virtual. 1830 */ 1831 if (ptpaddr == 0) 1832 continue; 1833 1834 /* 1835 * Check for large page. 1836 */ 1837 if ((ptpaddr & PG_PS) != 0) { 1838 pmap->pm_pdir[pdirindex] &= ~(PG_M|PG_RW); 1839 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE; 1840 anychanged = 1; 1841 continue; 1842 } 1843 1844 if (pdnxt > eva) 1845 pdnxt = eva; 1846 1847 for (; sva != pdnxt; sva += PAGE_SIZE) { 1848 pt_entry_t pbits; 1849 pt_entry_t *pte; 1850 vm_page_t m; 1851 1852 if ((pte = pmap_pte_quick(pmap, sva)) == NULL) 1853 continue; 1854 pbits = *pte; 1855 if (pbits & PG_MANAGED) { 1856 m = NULL; 1857 if (pbits & PG_A) { 1858 m = PHYS_TO_VM_PAGE(pbits); 1859 vm_page_flag_set(m, PG_REFERENCED); 1860 pbits &= ~PG_A; 1861 } 1862 if ((pbits & PG_M) != 0 && 1863 pmap_track_modified(sva)) { 1864 if (m == NULL) 1865 m = PHYS_TO_VM_PAGE(pbits); 1866 vm_page_dirty(m); 1867 pbits &= ~PG_M; 1868 } 1869 } 1870 1871 pbits &= ~PG_RW; 1872 1873 if (pbits != *pte) { 1874 pte_store(pte, pbits); 1875 anychanged = 1; 1876 } 1877 } 1878 } 1879 if (anychanged) 1880 pmap_invalidate_all(pmap); 1881} 1882 1883/* 1884 * Insert the given physical page (p) at 1885 * the specified virtual address (v) in the 1886 * target physical map with the protection requested. 1887 * 1888 * If specified, the page will be wired down, meaning 1889 * that the related pte can not be reclaimed. 1890 * 1891 * NB: This is the only routine which MAY NOT lazy-evaluate 1892 * or lose information. That is, this routine must actually 1893 * insert this page into the given map NOW. 1894 */ 1895void 1896pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot, 1897 boolean_t wired) 1898{ 1899 vm_paddr_t pa; 1900 register pt_entry_t *pte; 1901 vm_paddr_t opa; 1902 pt_entry_t origpte, newpte; 1903 vm_page_t mpte; 1904 1905 if (pmap == NULL) 1906 return; 1907 1908 va &= PG_FRAME; 1909#ifdef PMAP_DIAGNOSTIC 1910 if (va > VM_MAX_KERNEL_ADDRESS) 1911 panic("pmap_enter: toobig"); 1912 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS)) 1913 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%x)", va); 1914#endif 1915 1916 mpte = NULL; 1917 /* 1918 * In the case that a page table page is not 1919 * resident, we are creating it here. 1920 */ 1921 if (va < VM_MAXUSER_ADDRESS) { 1922 mpte = pmap_allocpte(pmap, va); 1923 } 1924#if 0 && defined(PMAP_DIAGNOSTIC) 1925 else { 1926 pd_entry_t *pdeaddr = pmap_pde(pmap, va); 1927 origpte = *pdeaddr; 1928 if ((origpte & PG_V) == 0) { 1929 panic("pmap_enter: invalid kernel page table page, pdir=%p, pde=%p, va=%p\n", 1930 pmap->pm_pdir[PTDPTDI], origpte, va); 1931 } 1932 } 1933#endif 1934 1935 pte = pmap_pte_quick(pmap, va); 1936 1937 /* 1938 * Page Directory table entry not valid, we need a new PT page 1939 */ 1940 if (pte == NULL) { 1941 panic("pmap_enter: invalid page directory pdir=%#jx, va=%#x\n", 1942 (uintmax_t)pmap->pm_pdir[PTDPTDI], va); 1943 } 1944 1945 pa = VM_PAGE_TO_PHYS(m) & PG_FRAME; 1946 origpte = *pte; 1947 opa = origpte & PG_FRAME; 1948 1949 if (origpte & PG_PS) 1950 panic("pmap_enter: attempted pmap_enter on 4MB page"); 1951 1952 /* 1953 * Mapping has not changed, must be protection or wiring change. 1954 */ 1955 if (origpte && (opa == pa)) { 1956 /* 1957 * Wiring change, just update stats. We don't worry about 1958 * wiring PT pages as they remain resident as long as there 1959 * are valid mappings in them. Hence, if a user page is wired, 1960 * the PT page will be also. 1961 */ 1962 if (wired && ((origpte & PG_W) == 0)) 1963 pmap->pm_stats.wired_count++; 1964 else if (!wired && (origpte & PG_W)) 1965 pmap->pm_stats.wired_count--; 1966 1967#if defined(PMAP_DIAGNOSTIC) 1968 if (pmap_nw_modified((pt_entry_t) origpte)) { 1969 printf( 1970 "pmap_enter: modified page not writable: va: 0x%x, pte: 0x%x\n", 1971 va, origpte); 1972 } 1973#endif 1974 1975 /* 1976 * Remove extra pte reference 1977 */ 1978 if (mpte) 1979 mpte->hold_count--; 1980 1981 if ((prot & VM_PROT_WRITE) && (origpte & PG_V)) { 1982 if ((origpte & PG_RW) == 0) { 1983 pte_store(pte, origpte | PG_RW); 1984 pmap_invalidate_page(pmap, va); 1985 } 1986 return; 1987 } 1988 1989 /* 1990 * We might be turning off write access to the page, 1991 * so we go ahead and sense modify status. 1992 */ 1993 if (origpte & PG_MANAGED) { 1994 if ((origpte & PG_M) && pmap_track_modified(va)) { 1995 vm_page_t om; 1996 om = PHYS_TO_VM_PAGE(opa); 1997 vm_page_dirty(om); 1998 } 1999 pa |= PG_MANAGED; 2000 } 2001 goto validate; 2002 } 2003 /* 2004 * Mapping has changed, invalidate old range and fall through to 2005 * handle validating new mapping. 2006 */ 2007 if (opa) { 2008 int err; 2009 vm_page_lock_queues(); 2010 err = pmap_remove_pte(pmap, pte, va); 2011 vm_page_unlock_queues(); 2012 if (err) 2013 panic("pmap_enter: pte vanished, va: 0x%x", va); 2014 } 2015 2016 /* 2017 * Enter on the PV list if part of our managed memory. Note that we 2018 * raise IPL while manipulating pv_table since pmap_enter can be 2019 * called at interrupt time. 2020 */ 2021 if (pmap_initialized && 2022 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) { 2023 pmap_insert_entry(pmap, va, mpte, m); 2024 pa |= PG_MANAGED; 2025 } 2026 2027 /* 2028 * Increment counters 2029 */ 2030 pmap->pm_stats.resident_count++; 2031 if (wired) 2032 pmap->pm_stats.wired_count++; 2033 2034validate: 2035 /* 2036 * Now validate mapping with desired protection/wiring. 2037 */ 2038 newpte = (pt_entry_t)(pa | pte_prot(pmap, prot) | PG_V); 2039 2040 if (wired) 2041 newpte |= PG_W; 2042 if (va < VM_MAXUSER_ADDRESS) 2043 newpte |= PG_U; 2044 if (pmap == kernel_pmap) 2045 newpte |= pgeflag; 2046 2047 /* 2048 * if the mapping or permission bits are different, we need 2049 * to update the pte. 2050 */ 2051 if ((origpte & ~(PG_M|PG_A)) != newpte) { 2052 pte_store(pte, newpte | PG_A); 2053 /*if (origpte)*/ { 2054 pmap_invalidate_page(pmap, va); 2055 } 2056 } 2057} 2058 2059/* 2060 * this code makes some *MAJOR* assumptions: 2061 * 1. Current pmap & pmap exists. 2062 * 2. Not wired. 2063 * 3. Read access. 2064 * 4. No page table pages. 2065 * 5. Tlbflush is deferred to calling procedure. 2066 * 6. Page IS managed. 2067 * but is *MUCH* faster than pmap_enter... 2068 */ 2069 2070vm_page_t 2071pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_page_t mpte) 2072{ 2073 pt_entry_t *pte; 2074 vm_paddr_t pa; 2075 2076 /* 2077 * In the case that a page table page is not 2078 * resident, we are creating it here. 2079 */ 2080 if (va < VM_MAXUSER_ADDRESS) { 2081 unsigned ptepindex; 2082 pd_entry_t ptepa; 2083 2084 /* 2085 * Calculate pagetable page index 2086 */ 2087 ptepindex = va >> PDRSHIFT; 2088 if (mpte && (mpte->pindex == ptepindex)) { 2089 mpte->hold_count++; 2090 } else { 2091retry: 2092 /* 2093 * Get the page directory entry 2094 */ 2095 ptepa = pmap->pm_pdir[ptepindex]; 2096 2097 /* 2098 * If the page table page is mapped, we just increment 2099 * the hold count, and activate it. 2100 */ 2101 if (ptepa) { 2102 if (ptepa & PG_PS) 2103 panic("pmap_enter_quick: unexpected mapping into 4MB page"); 2104 mpte = PHYS_TO_VM_PAGE(ptepa); 2105 mpte->hold_count++; 2106 } else { 2107 mpte = _pmap_allocpte(pmap, ptepindex); 2108 if (mpte == NULL) 2109 goto retry; 2110 } 2111 } 2112 } else { 2113 mpte = NULL; 2114 } 2115 2116 /* 2117 * This call to vtopte makes the assumption that we are 2118 * entering the page into the current pmap. In order to support 2119 * quick entry into any pmap, one would likely use pmap_pte_quick. 2120 * But that isn't as quick as vtopte. 2121 */ 2122 pte = vtopte(va); 2123 if (*pte) { 2124 if (mpte != NULL) { 2125 vm_page_lock_queues(); 2126 pmap_unwire_pte_hold(pmap, mpte); 2127 vm_page_unlock_queues(); 2128 } 2129 return 0; 2130 } 2131 2132 /* 2133 * Enter on the PV list if part of our managed memory. Note that we 2134 * raise IPL while manipulating pv_table since pmap_enter can be 2135 * called at interrupt time. 2136 */ 2137 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) 2138 pmap_insert_entry(pmap, va, mpte, m); 2139 2140 /* 2141 * Increment counters 2142 */ 2143 pmap->pm_stats.resident_count++; 2144 2145 pa = VM_PAGE_TO_PHYS(m); 2146 2147 /* 2148 * Now validate mapping with RO protection 2149 */ 2150 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) 2151 pte_store(pte, pa | PG_V | PG_U); 2152 else 2153 pte_store(pte, pa | PG_V | PG_U | PG_MANAGED); 2154 2155 return mpte; 2156} 2157 2158/* 2159 * Make a temporary mapping for a physical address. This is only intended 2160 * to be used for panic dumps. 2161 */ 2162void * 2163pmap_kenter_temporary(vm_offset_t pa, int i) 2164{ 2165 vm_offset_t va; 2166 2167 va = (vm_offset_t)crashdumpmap + (i * PAGE_SIZE); 2168 pmap_kenter(va, pa); 2169#ifndef I386_CPU 2170 invlpg(va); 2171#else 2172 invltlb(); 2173#endif 2174 return ((void *)crashdumpmap); 2175} 2176 2177/* 2178 * This code maps large physical mmap regions into the 2179 * processor address space. Note that some shortcuts 2180 * are taken, but the code works. 2181 */ 2182void 2183pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, 2184 vm_object_t object, vm_pindex_t pindex, 2185 vm_size_t size) 2186{ 2187 vm_page_t p; 2188 2189 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 2190 KASSERT(object->type == OBJT_DEVICE, 2191 ("pmap_object_init_pt: non-device object")); 2192 if (pseflag && 2193 ((addr & (NBPDR - 1)) == 0) && ((size & (NBPDR - 1)) == 0)) { 2194 int i; 2195 vm_page_t m[1]; 2196 unsigned int ptepindex; 2197 int npdes; 2198 pd_entry_t ptepa; 2199 2200 if (pmap->pm_pdir[ptepindex = (addr >> PDRSHIFT)]) 2201 return; 2202retry: 2203 p = vm_page_lookup(object, pindex); 2204 if (p != NULL) { 2205 vm_page_lock_queues(); 2206 if (vm_page_sleep_if_busy(p, FALSE, "init4p")) 2207 goto retry; 2208 } else { 2209 p = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL); 2210 if (p == NULL) 2211 return; 2212 m[0] = p; 2213 2214 if (vm_pager_get_pages(object, m, 1, 0) != VM_PAGER_OK) { 2215 vm_page_lock_queues(); 2216 vm_page_free(p); 2217 vm_page_unlock_queues(); 2218 return; 2219 } 2220 2221 p = vm_page_lookup(object, pindex); 2222 vm_page_lock_queues(); 2223 vm_page_wakeup(p); 2224 } 2225 vm_page_unlock_queues(); 2226 2227 ptepa = VM_PAGE_TO_PHYS(p); 2228 if (ptepa & (NBPDR - 1)) 2229 return; 2230 2231 p->valid = VM_PAGE_BITS_ALL; 2232 2233 pmap->pm_stats.resident_count += size >> PAGE_SHIFT; 2234 npdes = size >> PDRSHIFT; 2235 for(i = 0; i < npdes; i++) { 2236 pde_store(&pmap->pm_pdir[ptepindex], 2237 ptepa | PG_U | PG_RW | PG_V | PG_PS); 2238 ptepa += NBPDR; 2239 ptepindex += 1; 2240 } 2241 pmap_invalidate_all(pmap); 2242 } 2243} 2244 2245/* 2246 * pmap_prefault provides a quick way of clustering 2247 * pagefaults into a processes address space. It is a "cousin" 2248 * of pmap_object_init_pt, except it runs at page fault time instead 2249 * of mmap time. 2250 */ 2251#define PFBAK 4 2252#define PFFOR 4 2253#define PAGEORDER_SIZE (PFBAK+PFFOR) 2254 2255static int pmap_prefault_pageorder[] = { 2256 -1 * PAGE_SIZE, 1 * PAGE_SIZE, 2257 -2 * PAGE_SIZE, 2 * PAGE_SIZE, 2258 -3 * PAGE_SIZE, 3 * PAGE_SIZE, 2259 -4 * PAGE_SIZE, 4 * PAGE_SIZE 2260}; 2261 2262void 2263pmap_prefault(pmap, addra, entry) 2264 pmap_t pmap; 2265 vm_offset_t addra; 2266 vm_map_entry_t entry; 2267{ 2268 int i; 2269 vm_offset_t starta; 2270 vm_offset_t addr; 2271 vm_pindex_t pindex; 2272 vm_page_t m, mpte; 2273 vm_object_t object; 2274 2275 if (!curthread || (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))) 2276 return; 2277 2278 object = entry->object.vm_object; 2279 2280 starta = addra - PFBAK * PAGE_SIZE; 2281 if (starta < entry->start) { 2282 starta = entry->start; 2283 } else if (starta > addra) { 2284 starta = 0; 2285 } 2286 2287 mpte = NULL; 2288 for (i = 0; i < PAGEORDER_SIZE; i++) { 2289 vm_object_t backing_object, lobject; 2290 pt_entry_t *pte; 2291 2292 addr = addra + pmap_prefault_pageorder[i]; 2293 if (addr > addra + (PFFOR * PAGE_SIZE)) 2294 addr = 0; 2295 2296 if (addr < starta || addr >= entry->end) 2297 continue; 2298 2299 if ((*pmap_pde(pmap, addr)) == 0) 2300 continue; 2301 2302 pte = vtopte(addr); 2303 if (*pte) 2304 continue; 2305 2306 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT; 2307 lobject = object; 2308 VM_OBJECT_LOCK(lobject); 2309 while ((m = vm_page_lookup(lobject, pindex)) == NULL && 2310 lobject->type == OBJT_DEFAULT && 2311 (backing_object = lobject->backing_object) != NULL) { 2312 if (lobject->backing_object_offset & PAGE_MASK) 2313 break; 2314 pindex += lobject->backing_object_offset >> PAGE_SHIFT; 2315 VM_OBJECT_LOCK(backing_object); 2316 VM_OBJECT_UNLOCK(lobject); 2317 lobject = backing_object; 2318 } 2319 VM_OBJECT_UNLOCK(lobject); 2320 /* 2321 * give-up when a page is not in memory 2322 */ 2323 if (m == NULL) 2324 break; 2325 vm_page_lock_queues(); 2326 if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) && 2327 (m->busy == 0) && 2328 (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) { 2329 2330 if ((m->queue - m->pc) == PQ_CACHE) { 2331 vm_page_deactivate(m); 2332 } 2333 vm_page_busy(m); 2334 vm_page_unlock_queues(); 2335 mpte = pmap_enter_quick(pmap, addr, m, mpte); 2336 vm_page_lock_queues(); 2337 vm_page_wakeup(m); 2338 } 2339 vm_page_unlock_queues(); 2340 } 2341} 2342 2343/* 2344 * Routine: pmap_change_wiring 2345 * Function: Change the wiring attribute for a map/virtual-address 2346 * pair. 2347 * In/out conditions: 2348 * The mapping must already exist in the pmap. 2349 */ 2350void 2351pmap_change_wiring(pmap, va, wired) 2352 register pmap_t pmap; 2353 vm_offset_t va; 2354 boolean_t wired; 2355{ 2356 register pt_entry_t *pte; 2357 2358 if (pmap == NULL) 2359 return; 2360 2361 pte = pmap_pte_quick(pmap, va); 2362 2363 if (wired && !pmap_pte_w(pte)) 2364 pmap->pm_stats.wired_count++; 2365 else if (!wired && pmap_pte_w(pte)) 2366 pmap->pm_stats.wired_count--; 2367 2368 /* 2369 * Wiring is not a hardware characteristic so there is no need to 2370 * invalidate TLB. 2371 */ 2372 pmap_pte_set_w(pte, wired); 2373} 2374 2375 2376 2377/* 2378 * Copy the range specified by src_addr/len 2379 * from the source map to the range dst_addr/len 2380 * in the destination map. 2381 * 2382 * This routine is only advisory and need not do anything. 2383 */ 2384 2385void 2386pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, vm_size_t len, 2387 vm_offset_t src_addr) 2388{ 2389 vm_offset_t addr; 2390 vm_offset_t end_addr = src_addr + len; 2391 vm_offset_t pdnxt; 2392 vm_page_t m; 2393 2394 if (dst_addr != src_addr) 2395 return; 2396 2397 if (!pmap_is_current(src_pmap)) 2398 return; 2399 2400 for (addr = src_addr; addr < end_addr; addr = pdnxt) { 2401 pt_entry_t *src_pte, *dst_pte; 2402 vm_page_t dstmpte, srcmpte; 2403 pd_entry_t srcptepaddr; 2404 unsigned ptepindex; 2405 2406 if (addr >= UPT_MIN_ADDRESS) 2407 panic("pmap_copy: invalid to pmap_copy page tables\n"); 2408 2409 /* 2410 * Don't let optional prefaulting of pages make us go 2411 * way below the low water mark of free pages or way 2412 * above high water mark of used pv entries. 2413 */ 2414 if (cnt.v_free_count < cnt.v_free_reserved || 2415 pv_entry_count > pv_entry_high_water) 2416 break; 2417 2418 pdnxt = (addr + NBPDR) & ~PDRMASK; 2419 ptepindex = addr >> PDRSHIFT; 2420 2421 srcptepaddr = src_pmap->pm_pdir[ptepindex]; 2422 if (srcptepaddr == 0) 2423 continue; 2424 2425 if (srcptepaddr & PG_PS) { 2426 if (dst_pmap->pm_pdir[ptepindex] == 0) { 2427 dst_pmap->pm_pdir[ptepindex] = srcptepaddr; 2428 dst_pmap->pm_stats.resident_count += 2429 NBPDR / PAGE_SIZE; 2430 } 2431 continue; 2432 } 2433 2434 srcmpte = PHYS_TO_VM_PAGE(srcptepaddr); 2435 if (srcmpte->hold_count == 0 || (srcmpte->flags & PG_BUSY)) 2436 continue; 2437 2438 if (pdnxt > end_addr) 2439 pdnxt = end_addr; 2440 2441 src_pte = vtopte(addr); 2442 while (addr < pdnxt) { 2443 pt_entry_t ptetemp; 2444 ptetemp = *src_pte; 2445 /* 2446 * we only virtual copy managed pages 2447 */ 2448 if ((ptetemp & PG_MANAGED) != 0) { 2449 /* 2450 * We have to check after allocpte for the 2451 * pte still being around... allocpte can 2452 * block. 2453 */ 2454 dstmpte = pmap_allocpte(dst_pmap, addr); 2455 dst_pte = pmap_pte_quick(dst_pmap, addr); 2456 if ((*dst_pte == 0) && (ptetemp = *src_pte)) { 2457 /* 2458 * Clear the modified and 2459 * accessed (referenced) bits 2460 * during the copy. 2461 */ 2462 m = PHYS_TO_VM_PAGE(ptetemp); 2463 *dst_pte = ptetemp & ~(PG_M | PG_A); 2464 dst_pmap->pm_stats.resident_count++; 2465 pmap_insert_entry(dst_pmap, addr, 2466 dstmpte, m); 2467 } else { 2468 vm_page_lock_queues(); 2469 pmap_unwire_pte_hold(dst_pmap, dstmpte); 2470 vm_page_unlock_queues(); 2471 } 2472 if (dstmpte->hold_count >= srcmpte->hold_count) 2473 break; 2474 } 2475 addr += PAGE_SIZE; 2476 src_pte++; 2477 } 2478 } 2479} 2480 2481#ifdef SMP 2482 2483/* 2484 * pmap_zpi_switchout*() 2485 * 2486 * These functions allow us to avoid doing IPIs alltogether in certain 2487 * temporary page-mapping situations (page zeroing). Instead to deal 2488 * with being preempted and moved onto a different cpu we invalidate 2489 * the page when the scheduler switches us in. This does not occur 2490 * very often so we remain relatively optimal with very little effort. 2491 */ 2492static void 2493pmap_zpi_switchout12(void) 2494{ 2495 invlpg((u_int)CADDR1); 2496 invlpg((u_int)CADDR2); 2497} 2498 2499static void 2500pmap_zpi_switchout2(void) 2501{ 2502 invlpg((u_int)CADDR2); 2503} 2504 2505static void 2506pmap_zpi_switchout3(void) 2507{ 2508 invlpg((u_int)CADDR3); 2509} 2510 2511#endif 2512 2513/* 2514 * pmap_zero_page zeros the specified hardware page by mapping 2515 * the page into KVM and using bzero to clear its contents. 2516 */ 2517void 2518pmap_zero_page(vm_page_t m) 2519{ 2520 2521 mtx_lock(&CMAPCADDR12_lock); 2522 if (*CMAP2) 2523 panic("pmap_zero_page: CMAP2 busy"); 2524#ifdef SMP 2525 curthread->td_pcb->pcb_switchout = pmap_zpi_switchout2; 2526#endif 2527 *CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M; 2528#if defined(I686_CPU) 2529 if (cpu_class == CPUCLASS_686) 2530 i686_pagezero(CADDR2); 2531 else 2532#endif 2533 bzero(CADDR2, PAGE_SIZE); 2534 *CMAP2 = 0; 2535#ifdef I386_CPU 2536 invltlb(); 2537#else 2538 invlpg((u_int)CADDR2); 2539#endif 2540#ifdef SMP 2541 curthread->td_pcb->pcb_switchout = NULL; 2542#endif 2543 mtx_unlock(&CMAPCADDR12_lock); 2544} 2545 2546/* 2547 * pmap_zero_page_area zeros the specified hardware page by mapping 2548 * the page into KVM and using bzero to clear its contents. 2549 * 2550 * off and size may not cover an area beyond a single hardware page. 2551 */ 2552void 2553pmap_zero_page_area(vm_page_t m, int off, int size) 2554{ 2555 2556 mtx_lock(&CMAPCADDR12_lock); 2557 if (*CMAP2) 2558 panic("pmap_zero_page: CMAP2 busy"); 2559#ifdef SMP 2560 curthread->td_pcb->pcb_switchout = pmap_zpi_switchout2; 2561#endif 2562 *CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M; 2563#if defined(I686_CPU) 2564 if (cpu_class == CPUCLASS_686 && off == 0 && size == PAGE_SIZE) 2565 i686_pagezero(CADDR2); 2566 else 2567#endif 2568 bzero((char *)CADDR2 + off, size); 2569 *CMAP2 = 0; 2570#ifdef I386_CPU 2571 invltlb(); 2572#else 2573 invlpg((u_int)CADDR2); 2574#endif 2575#ifdef SMP 2576 curthread->td_pcb->pcb_switchout = NULL; 2577#endif 2578 mtx_unlock(&CMAPCADDR12_lock); 2579} 2580 2581/* 2582 * pmap_zero_page_idle zeros the specified hardware page by mapping 2583 * the page into KVM and using bzero to clear its contents. This 2584 * is intended to be called from the vm_pagezero process only and 2585 * outside of Giant. 2586 */ 2587void 2588pmap_zero_page_idle(vm_page_t m) 2589{ 2590 2591 if (*CMAP3) 2592 panic("pmap_zero_page: CMAP3 busy"); 2593#ifdef SMP 2594 curthread->td_pcb->pcb_switchout = pmap_zpi_switchout3; 2595#endif 2596 *CMAP3 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M; 2597#if defined(I686_CPU) 2598 if (cpu_class == CPUCLASS_686) 2599 i686_pagezero(CADDR3); 2600 else 2601#endif 2602 bzero(CADDR3, PAGE_SIZE); 2603 *CMAP3 = 0; 2604#ifdef I386_CPU 2605 invltlb(); 2606#else 2607 invlpg((u_int)CADDR3); 2608#endif 2609#ifdef SMP 2610 curthread->td_pcb->pcb_switchout = NULL; 2611#endif 2612} 2613 2614/* 2615 * pmap_copy_page copies the specified (machine independent) 2616 * page by mapping the page into virtual memory and using 2617 * bcopy to copy the page, one machine dependent page at a 2618 * time. 2619 */ 2620void 2621pmap_copy_page(vm_page_t src, vm_page_t dst) 2622{ 2623 2624 mtx_lock(&CMAPCADDR12_lock); 2625 if (*CMAP1) 2626 panic("pmap_copy_page: CMAP1 busy"); 2627 if (*CMAP2) 2628 panic("pmap_copy_page: CMAP2 busy"); 2629#ifdef SMP 2630 curthread->td_pcb->pcb_switchout = pmap_zpi_switchout12; 2631#endif 2632 *CMAP1 = PG_V | VM_PAGE_TO_PHYS(src) | PG_A; 2633 *CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(dst) | PG_A | PG_M; 2634 bcopy(CADDR1, CADDR2, PAGE_SIZE); 2635 *CMAP1 = 0; 2636 *CMAP2 = 0; 2637#ifdef I386_CPU 2638 invltlb(); 2639#else 2640 invlpg((u_int)CADDR1); 2641 invlpg((u_int)CADDR2); 2642#endif 2643#ifdef SMP 2644 curthread->td_pcb->pcb_switchout = NULL; 2645#endif 2646 mtx_unlock(&CMAPCADDR12_lock); 2647} 2648 2649/* 2650 * Returns true if the pmap's pv is one of the first 2651 * 16 pvs linked to from this page. This count may 2652 * be changed upwards or downwards in the future; it 2653 * is only necessary that true be returned for a small 2654 * subset of pmaps for proper page aging. 2655 */ 2656boolean_t 2657pmap_page_exists_quick(pmap, m) 2658 pmap_t pmap; 2659 vm_page_t m; 2660{ 2661 pv_entry_t pv; 2662 int loops = 0; 2663 int s; 2664 2665 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) 2666 return FALSE; 2667 2668 s = splvm(); 2669 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 2670 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) { 2671 if (pv->pv_pmap == pmap) { 2672 splx(s); 2673 return TRUE; 2674 } 2675 loops++; 2676 if (loops >= 16) 2677 break; 2678 } 2679 splx(s); 2680 return (FALSE); 2681} 2682 2683#define PMAP_REMOVE_PAGES_CURPROC_ONLY 2684/* 2685 * Remove all pages from specified address space 2686 * this aids process exit speeds. Also, this code 2687 * is special cased for current process only, but 2688 * can have the more generic (and slightly slower) 2689 * mode enabled. This is much faster than pmap_remove 2690 * in the case of running down an entire address space. 2691 */ 2692void 2693pmap_remove_pages(pmap, sva, eva) 2694 pmap_t pmap; 2695 vm_offset_t sva, eva; 2696{ 2697 pt_entry_t *pte, tpte; 2698 vm_page_t m; 2699 pv_entry_t pv, npv; 2700 int s; 2701 2702#ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY 2703 if (!curthread || (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))) { 2704 printf("warning: pmap_remove_pages called with non-current pmap\n"); 2705 return; 2706 } 2707#endif 2708 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 2709 s = splvm(); 2710 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) { 2711 2712 if (pv->pv_va >= eva || pv->pv_va < sva) { 2713 npv = TAILQ_NEXT(pv, pv_plist); 2714 continue; 2715 } 2716 2717#ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY 2718 pte = vtopte(pv->pv_va); 2719#else 2720 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va); 2721#endif 2722 tpte = *pte; 2723 2724 if (tpte == 0) { 2725 printf("TPTE at %p IS ZERO @ VA %08x\n", 2726 pte, pv->pv_va); 2727 panic("bad pte"); 2728 } 2729 2730/* 2731 * We cannot remove wired pages from a process' mapping at this time 2732 */ 2733 if (tpte & PG_W) { 2734 npv = TAILQ_NEXT(pv, pv_plist); 2735 continue; 2736 } 2737 2738 m = PHYS_TO_VM_PAGE(tpte); 2739 KASSERT(m->phys_addr == (tpte & PG_FRAME), 2740 ("vm_page_t %p phys_addr mismatch %016jx %016jx", 2741 m, (uintmax_t)m->phys_addr, (uintmax_t)tpte)); 2742 2743 KASSERT(m < &vm_page_array[vm_page_array_size], 2744 ("pmap_remove_pages: bad tpte %#jx", (uintmax_t)tpte)); 2745 2746 pv->pv_pmap->pm_stats.resident_count--; 2747 2748 pte_clear(pte); 2749 2750 /* 2751 * Update the vm_page_t clean and reference bits. 2752 */ 2753 if (tpte & PG_M) { 2754 vm_page_dirty(m); 2755 } 2756 2757 npv = TAILQ_NEXT(pv, pv_plist); 2758 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist); 2759 2760 m->md.pv_list_count--; 2761 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list); 2762 if (TAILQ_FIRST(&m->md.pv_list) == NULL) { 2763 vm_page_flag_clear(m, PG_WRITEABLE); 2764 } 2765 2766 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem); 2767 free_pv_entry(pv); 2768 } 2769 splx(s); 2770 pmap_invalidate_all(pmap); 2771} 2772 2773/* 2774 * pmap_is_modified: 2775 * 2776 * Return whether or not the specified physical page was modified 2777 * in any physical maps. 2778 */ 2779boolean_t 2780pmap_is_modified(vm_page_t m) 2781{ 2782 pv_entry_t pv; 2783 pt_entry_t *pte; 2784 int s; 2785 2786 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) 2787 return FALSE; 2788 2789 s = splvm(); 2790 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 2791 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) { 2792 /* 2793 * if the bit being tested is the modified bit, then 2794 * mark clean_map and ptes as never 2795 * modified. 2796 */ 2797 if (!pmap_track_modified(pv->pv_va)) 2798 continue; 2799#if defined(PMAP_DIAGNOSTIC) 2800 if (!pv->pv_pmap) { 2801 printf("Null pmap (tb) at va: 0x%x\n", pv->pv_va); 2802 continue; 2803 } 2804#endif 2805 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va); 2806 if (*pte & PG_M) { 2807 splx(s); 2808 return TRUE; 2809 } 2810 } 2811 splx(s); 2812 return (FALSE); 2813} 2814 2815/* 2816 * Clear the given bit in each of the given page's ptes. 2817 */ 2818static __inline void 2819pmap_clear_ptes(vm_page_t m, int bit) 2820{ 2821 register pv_entry_t pv; 2822 pt_entry_t pbits, *pte; 2823 int s; 2824 2825 if (!pmap_initialized || (m->flags & PG_FICTITIOUS) || 2826 (bit == PG_RW && (m->flags & PG_WRITEABLE) == 0)) 2827 return; 2828 2829 s = splvm(); 2830 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 2831 /* 2832 * Loop over all current mappings setting/clearing as appropos If 2833 * setting RO do we need to clear the VAC? 2834 */ 2835 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) { 2836 /* 2837 * don't write protect pager mappings 2838 */ 2839 if (bit == PG_RW) { 2840 if (!pmap_track_modified(pv->pv_va)) 2841 continue; 2842 } 2843 2844#if defined(PMAP_DIAGNOSTIC) 2845 if (!pv->pv_pmap) { 2846 printf("Null pmap (cb) at va: 0x%x\n", pv->pv_va); 2847 continue; 2848 } 2849#endif 2850 2851 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va); 2852 pbits = *pte; 2853 if (pbits & bit) { 2854 if (bit == PG_RW) { 2855 if (pbits & PG_M) { 2856 vm_page_dirty(m); 2857 } 2858 pte_store(pte, pbits & ~(PG_M|PG_RW)); 2859 } else { 2860 pte_store(pte, pbits & ~bit); 2861 } 2862 pmap_invalidate_page(pv->pv_pmap, pv->pv_va); 2863 } 2864 } 2865 if (bit == PG_RW) 2866 vm_page_flag_clear(m, PG_WRITEABLE); 2867 splx(s); 2868} 2869 2870/* 2871 * pmap_page_protect: 2872 * 2873 * Lower the permission for all mappings to a given page. 2874 */ 2875void 2876pmap_page_protect(vm_page_t m, vm_prot_t prot) 2877{ 2878 if ((prot & VM_PROT_WRITE) == 0) { 2879 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) { 2880 pmap_clear_ptes(m, PG_RW); 2881 } else { 2882 pmap_remove_all(m); 2883 } 2884 } 2885} 2886 2887/* 2888 * pmap_ts_referenced: 2889 * 2890 * Return a count of reference bits for a page, clearing those bits. 2891 * It is not necessary for every reference bit to be cleared, but it 2892 * is necessary that 0 only be returned when there are truly no 2893 * reference bits set. 2894 * 2895 * XXX: The exact number of bits to check and clear is a matter that 2896 * should be tested and standardized at some point in the future for 2897 * optimal aging of shared pages. 2898 */ 2899int 2900pmap_ts_referenced(vm_page_t m) 2901{ 2902 register pv_entry_t pv, pvf, pvn; 2903 pt_entry_t *pte; 2904 pt_entry_t v; 2905 int s; 2906 int rtval = 0; 2907 2908 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) 2909 return (rtval); 2910 2911 s = splvm(); 2912 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 2913 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) { 2914 2915 pvf = pv; 2916 2917 do { 2918 pvn = TAILQ_NEXT(pv, pv_list); 2919 2920 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list); 2921 2922 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list); 2923 2924 if (!pmap_track_modified(pv->pv_va)) 2925 continue; 2926 2927 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va); 2928 2929 if (pte && ((v = pte_load(pte)) & PG_A) != 0) { 2930 pte_store(pte, v & ~PG_A); 2931 pmap_invalidate_page(pv->pv_pmap, pv->pv_va); 2932 2933 rtval++; 2934 if (rtval > 4) { 2935 break; 2936 } 2937 } 2938 } while ((pv = pvn) != NULL && pv != pvf); 2939 } 2940 splx(s); 2941 2942 return (rtval); 2943} 2944 2945/* 2946 * Clear the modify bits on the specified physical page. 2947 */ 2948void 2949pmap_clear_modify(vm_page_t m) 2950{ 2951 pmap_clear_ptes(m, PG_M); 2952} 2953 2954/* 2955 * pmap_clear_reference: 2956 * 2957 * Clear the reference bit on the specified physical page. 2958 */ 2959void 2960pmap_clear_reference(vm_page_t m) 2961{ 2962 pmap_clear_ptes(m, PG_A); 2963} 2964 2965/* 2966 * Miscellaneous support routines follow 2967 */ 2968 2969static void 2970i386_protection_init() 2971{ 2972 register int *kp, prot; 2973 2974 kp = protection_codes; 2975 for (prot = 0; prot < 8; prot++) { 2976 switch (prot) { 2977 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE: 2978 /* 2979 * Read access is also 0. There isn't any execute bit, 2980 * so just make it readable. 2981 */ 2982 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE: 2983 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE: 2984 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE: 2985 *kp++ = 0; 2986 break; 2987 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE: 2988 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE: 2989 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE: 2990 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE: 2991 *kp++ = PG_RW; 2992 break; 2993 } 2994 } 2995} 2996 2997/* 2998 * Map a set of physical memory pages into the kernel virtual 2999 * address space. Return a pointer to where it is mapped. This 3000 * routine is intended to be used for mapping device memory, 3001 * NOT real memory. 3002 */ 3003void * 3004pmap_mapdev(pa, size) 3005 vm_paddr_t pa; 3006 vm_size_t size; 3007{ 3008 vm_offset_t va, tmpva, offset; 3009 3010 offset = pa & PAGE_MASK; 3011 size = roundup(offset + size, PAGE_SIZE); 3012 3013 va = kmem_alloc_nofault(kernel_map, size); 3014 if (!va) 3015 panic("pmap_mapdev: Couldn't alloc kernel virtual memory"); 3016 3017 pa = pa & PG_FRAME; 3018 for (tmpva = va; size > 0; ) { 3019 pmap_kenter(tmpva, pa); 3020 size -= PAGE_SIZE; 3021 tmpva += PAGE_SIZE; 3022 pa += PAGE_SIZE; 3023 } 3024 pmap_invalidate_range(kernel_pmap, va, tmpva); 3025 return ((void *)(va + offset)); 3026} 3027 3028void 3029pmap_unmapdev(va, size) 3030 vm_offset_t va; 3031 vm_size_t size; 3032{ 3033 vm_offset_t base, offset, tmpva; 3034 pt_entry_t *pte; 3035 3036 base = va & PG_FRAME; 3037 offset = va & PAGE_MASK; 3038 size = roundup(offset + size, PAGE_SIZE); 3039 for (tmpva = base; tmpva < (base + size); tmpva += PAGE_SIZE) { 3040 pte = vtopte(tmpva); 3041 pte_clear(pte); 3042 } 3043 pmap_invalidate_range(kernel_pmap, va, tmpva); 3044 kmem_free(kernel_map, base, size); 3045} 3046 3047/* 3048 * perform the pmap work for mincore 3049 */ 3050int 3051pmap_mincore(pmap, addr) 3052 pmap_t pmap; 3053 vm_offset_t addr; 3054{ 3055 pt_entry_t *ptep, pte; 3056 vm_page_t m; 3057 int val = 0; 3058 3059 ptep = pmap_pte_quick(pmap, addr); 3060 if (ptep == 0) { 3061 return 0; 3062 } 3063 3064 if ((pte = *ptep) != 0) { 3065 vm_paddr_t pa; 3066 3067 val = MINCORE_INCORE; 3068 if ((pte & PG_MANAGED) == 0) 3069 return val; 3070 3071 pa = pte & PG_FRAME; 3072 3073 m = PHYS_TO_VM_PAGE(pa); 3074 3075 /* 3076 * Modified by us 3077 */ 3078 if (pte & PG_M) 3079 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER; 3080 else { 3081 /* 3082 * Modified by someone else 3083 */ 3084 vm_page_lock_queues(); 3085 if (m->dirty || pmap_is_modified(m)) 3086 val |= MINCORE_MODIFIED_OTHER; 3087 vm_page_unlock_queues(); 3088 } 3089 /* 3090 * Referenced by us 3091 */ 3092 if (pte & PG_A) 3093 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER; 3094 else { 3095 /* 3096 * Referenced by someone else 3097 */ 3098 vm_page_lock_queues(); 3099 if ((m->flags & PG_REFERENCED) || 3100 pmap_ts_referenced(m)) { 3101 val |= MINCORE_REFERENCED_OTHER; 3102 vm_page_flag_set(m, PG_REFERENCED); 3103 } 3104 vm_page_unlock_queues(); 3105 } 3106 } 3107 return val; 3108} 3109 3110void 3111pmap_activate(struct thread *td) 3112{ 3113 struct proc *p = td->td_proc; 3114 pmap_t pmap, oldpmap; 3115 u_int32_t cr3; 3116 3117 critical_enter(); 3118 pmap = vmspace_pmap(td->td_proc->p_vmspace); 3119 oldpmap = PCPU_GET(curpmap); 3120#if defined(SMP) 3121 atomic_clear_int(&oldpmap->pm_active, PCPU_GET(cpumask)); 3122 atomic_set_int(&pmap->pm_active, PCPU_GET(cpumask)); 3123#else 3124 oldpmap->pm_active &= ~1; 3125 pmap->pm_active |= 1; 3126#endif 3127#ifdef PAE 3128 cr3 = vtophys(pmap->pm_pdpt); 3129#else 3130 cr3 = vtophys(pmap->pm_pdir); 3131#endif 3132 /* XXXKSE this is wrong. 3133 * pmap_activate is for the current thread on the current cpu 3134 */ 3135 if (p->p_flag & P_SA) { 3136 /* Make sure all other cr3 entries are updated. */ 3137 /* what if they are running? XXXKSE (maybe abort them) */ 3138 FOREACH_THREAD_IN_PROC(p, td) { 3139 td->td_pcb->pcb_cr3 = cr3; 3140 } 3141 } else { 3142 td->td_pcb->pcb_cr3 = cr3; 3143 } 3144 load_cr3(cr3); 3145 PCPU_SET(curpmap, pmap); 3146 critical_exit(); 3147} 3148 3149vm_offset_t 3150pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size) 3151{ 3152 3153 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) { 3154 return addr; 3155 } 3156 3157 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1); 3158 return addr; 3159} 3160 3161 3162#if defined(PMAP_DEBUG) 3163pmap_pid_dump(int pid) 3164{ 3165 pmap_t pmap; 3166 struct proc *p; 3167 int npte = 0; 3168 int index; 3169 3170 sx_slock(&allproc_lock); 3171 LIST_FOREACH(p, &allproc, p_list) { 3172 if (p->p_pid != pid) 3173 continue; 3174 3175 if (p->p_vmspace) { 3176 int i,j; 3177 index = 0; 3178 pmap = vmspace_pmap(p->p_vmspace); 3179 for (i = 0; i < NPDEPTD; i++) { 3180 pd_entry_t *pde; 3181 pt_entry_t *pte; 3182 vm_offset_t base = i << PDRSHIFT; 3183 3184 pde = &pmap->pm_pdir[i]; 3185 if (pde && pmap_pde_v(pde)) { 3186 for (j = 0; j < NPTEPG; j++) { 3187 vm_offset_t va = base + (j << PAGE_SHIFT); 3188 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) { 3189 if (index) { 3190 index = 0; 3191 printf("\n"); 3192 } 3193 sx_sunlock(&allproc_lock); 3194 return npte; 3195 } 3196 pte = pmap_pte_quick(pmap, va); 3197 if (pte && pmap_pte_v(pte)) { 3198 pt_entry_t pa; 3199 vm_page_t m; 3200 pa = *pte; 3201 m = PHYS_TO_VM_PAGE(pa); 3202 printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x", 3203 va, pa, m->hold_count, m->wire_count, m->flags); 3204 npte++; 3205 index++; 3206 if (index >= 2) { 3207 index = 0; 3208 printf("\n"); 3209 } else { 3210 printf(" "); 3211 } 3212 } 3213 } 3214 } 3215 } 3216 } 3217 } 3218 sx_sunlock(&allproc_lock); 3219 return npte; 3220} 3221#endif 3222 3223#if defined(DEBUG) 3224 3225static void pads(pmap_t pm); 3226void pmap_pvdump(vm_offset_t pa); 3227 3228/* print address space of pmap*/ 3229static void 3230pads(pm) 3231 pmap_t pm; 3232{ 3233 int i, j; 3234 vm_paddr_t va; 3235 pt_entry_t *ptep; 3236 3237 if (pm == kernel_pmap) 3238 return; 3239 for (i = 0; i < NPDEPTD; i++) 3240 if (pm->pm_pdir[i]) 3241 for (j = 0; j < NPTEPG; j++) { 3242 va = (i << PDRSHIFT) + (j << PAGE_SHIFT); 3243 if (pm == kernel_pmap && va < KERNBASE) 3244 continue; 3245 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS) 3246 continue; 3247 ptep = pmap_pte_quick(pm, va); 3248 if (pmap_pte_v(ptep)) 3249 printf("%x:%x ", va, *ptep); 3250 }; 3251 3252} 3253 3254void 3255pmap_pvdump(pa) 3256 vm_paddr_t pa; 3257{ 3258 pv_entry_t pv; 3259 vm_page_t m; 3260 3261 printf("pa %x", pa); 3262 m = PHYS_TO_VM_PAGE(pa); 3263 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) { 3264 printf(" -> pmap %p, va %x", (void *)pv->pv_pmap, pv->pv_va); 3265 pads(pv->pv_pmap); 3266 } 3267 printf(" "); 3268} 3269#endif 3270