pmap.c revision 120322
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 120322 2003-09-21 21:36:13Z alc $"); 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_pageable(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 object for the ptes 1108 */ 1109 if (pmap->pm_pteobj == NULL) 1110 pmap->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, PTDPTDI + 1111 NPGPTD); 1112 1113 /* 1114 * allocate the page directory page(s) 1115 */ 1116 for (i = 0; i < NPGPTD;) { 1117 m = vm_page_alloc(NULL, color++, 1118 VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | 1119 VM_ALLOC_ZERO); 1120 if (m == NULL) 1121 VM_WAIT; 1122 else { 1123 vm_page_lock_queues(); 1124 vm_page_flag_clear(m, PG_BUSY); 1125 m->valid = VM_PAGE_BITS_ALL; 1126 vm_page_unlock_queues(); 1127 ptdpg[i++] = m; 1128 } 1129 } 1130 1131 pmap_qenter((vm_offset_t)pmap->pm_pdir, ptdpg, NPGPTD); 1132 1133 for (i = 0; i < NPGPTD; i++) { 1134 if ((ptdpg[i]->flags & PG_ZERO) == 0) 1135 bzero(pmap->pm_pdir + (i * NPDEPG), PAGE_SIZE); 1136 } 1137 1138 mtx_lock_spin(&allpmaps_lock); 1139 LIST_INSERT_HEAD(&allpmaps, pmap, pm_list); 1140 mtx_unlock_spin(&allpmaps_lock); 1141 /* Wire in kernel global address entries. */ 1142 /* XXX copies current process, does not fill in MPPTDI */ 1143 bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * sizeof(pd_entry_t)); 1144#ifdef SMP 1145 pmap->pm_pdir[MPPTDI] = PTD[MPPTDI]; 1146#endif 1147 1148 /* install self-referential address mapping entry(s) */ 1149 for (i = 0; i < NPGPTD; i++) { 1150 pa = VM_PAGE_TO_PHYS(ptdpg[i]); 1151 pmap->pm_pdir[PTDPTDI + i] = pa | PG_V | PG_RW | PG_A | PG_M; 1152#ifdef PAE 1153 pmap->pm_pdpt[i] = pa | PG_V; 1154#endif 1155 } 1156 1157 pmap->pm_active = 0; 1158 TAILQ_INIT(&pmap->pm_pvlist); 1159 bzero(&pmap->pm_stats, sizeof pmap->pm_stats); 1160} 1161 1162/* 1163 * Wire in kernel global address entries. To avoid a race condition 1164 * between pmap initialization and pmap_growkernel, this procedure 1165 * should be called after the vmspace is attached to the process 1166 * but before this pmap is activated. 1167 */ 1168void 1169pmap_pinit2(pmap) 1170 struct pmap *pmap; 1171{ 1172 /* XXX: Remove this stub when no longer called */ 1173} 1174 1175/* 1176 * this routine is called if the page table page is not 1177 * mapped correctly. 1178 */ 1179static vm_page_t 1180_pmap_allocpte(pmap, ptepindex) 1181 pmap_t pmap; 1182 unsigned ptepindex; 1183{ 1184 vm_paddr_t ptepa; 1185 vm_page_t m; 1186 1187 /* 1188 * Find or fabricate a new pagetable page 1189 */ 1190 VM_OBJECT_LOCK(pmap->pm_pteobj); 1191 m = vm_page_grab(pmap->pm_pteobj, ptepindex, 1192 VM_ALLOC_WIRED | VM_ALLOC_ZERO | VM_ALLOC_RETRY); 1193 if ((m->flags & PG_ZERO) == 0) 1194 pmap_zero_page(m); 1195 1196 KASSERT(m->queue == PQ_NONE, 1197 ("_pmap_allocpte: %p->queue != PQ_NONE", m)); 1198 1199 /* 1200 * Increment the hold count for the page table page 1201 * (denoting a new mapping.) 1202 */ 1203 m->hold_count++; 1204 1205 /* 1206 * Map the pagetable page into the process address space, if 1207 * it isn't already there. 1208 */ 1209 1210 pmap->pm_stats.resident_count++; 1211 1212 ptepa = VM_PAGE_TO_PHYS(m); 1213 pmap->pm_pdir[ptepindex] = 1214 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_A | PG_M); 1215 1216 vm_page_lock_queues(); 1217 m->valid = VM_PAGE_BITS_ALL; 1218 vm_page_flag_clear(m, PG_ZERO); 1219 vm_page_wakeup(m); 1220 vm_page_unlock_queues(); 1221 VM_OBJECT_UNLOCK(pmap->pm_pteobj); 1222 1223 return m; 1224} 1225 1226static vm_page_t 1227pmap_allocpte(pmap_t pmap, vm_offset_t va) 1228{ 1229 unsigned ptepindex; 1230 pd_entry_t ptepa; 1231 vm_page_t m; 1232 1233 /* 1234 * Calculate pagetable page index 1235 */ 1236 ptepindex = va >> PDRSHIFT; 1237 1238 /* 1239 * Get the page directory entry 1240 */ 1241 ptepa = pmap->pm_pdir[ptepindex]; 1242 1243 /* 1244 * This supports switching from a 4MB page to a 1245 * normal 4K page. 1246 */ 1247 if (ptepa & PG_PS) { 1248 pmap->pm_pdir[ptepindex] = 0; 1249 ptepa = 0; 1250 pmap_invalidate_all(kernel_pmap); 1251 } 1252 1253 /* 1254 * If the page table page is mapped, we just increment the 1255 * hold count, and activate it. 1256 */ 1257 if (ptepa) { 1258 m = PHYS_TO_VM_PAGE(ptepa); 1259 m->hold_count++; 1260 return m; 1261 } 1262 /* 1263 * Here if the pte page isn't mapped, or if it has been deallocated. 1264 */ 1265 return _pmap_allocpte(pmap, ptepindex); 1266} 1267 1268 1269/*************************************************** 1270* Pmap allocation/deallocation routines. 1271 ***************************************************/ 1272 1273#ifdef SMP 1274/* 1275 * Deal with a SMP shootdown of other users of the pmap that we are 1276 * trying to dispose of. This can be a bit hairy. 1277 */ 1278static u_int *lazymask; 1279static u_int lazyptd; 1280static volatile u_int lazywait; 1281 1282void pmap_lazyfix_action(void); 1283 1284void 1285pmap_lazyfix_action(void) 1286{ 1287 u_int mymask = PCPU_GET(cpumask); 1288 1289 if (rcr3() == lazyptd) 1290 load_cr3(PCPU_GET(curpcb)->pcb_cr3); 1291 atomic_clear_int(lazymask, mymask); 1292 atomic_store_rel_int(&lazywait, 1); 1293} 1294 1295static void 1296pmap_lazyfix_self(u_int mymask) 1297{ 1298 1299 if (rcr3() == lazyptd) 1300 load_cr3(PCPU_GET(curpcb)->pcb_cr3); 1301 atomic_clear_int(lazymask, mymask); 1302} 1303 1304 1305static void 1306pmap_lazyfix(pmap_t pmap) 1307{ 1308 u_int mymask = PCPU_GET(cpumask); 1309 u_int mask; 1310 register u_int spins; 1311 1312 while ((mask = pmap->pm_active) != 0) { 1313 spins = 50000000; 1314 mask = mask & -mask; /* Find least significant set bit */ 1315 mtx_lock_spin(&lazypmap_lock); 1316#ifdef PAE 1317 lazyptd = vtophys(pmap->pm_pdpt); 1318#else 1319 lazyptd = vtophys(pmap->pm_pdir); 1320#endif 1321 if (mask == mymask) { 1322 lazymask = &pmap->pm_active; 1323 pmap_lazyfix_self(mymask); 1324 } else { 1325 atomic_store_rel_int((u_int *)&lazymask, 1326 (u_int)&pmap->pm_active); 1327 atomic_store_rel_int(&lazywait, 0); 1328 ipi_selected(mask, IPI_LAZYPMAP); 1329 while (lazywait == 0) { 1330 ia32_pause(); 1331 if (--spins == 0) 1332 break; 1333 } 1334 } 1335 mtx_unlock_spin(&lazypmap_lock); 1336 if (spins == 0) 1337 printf("pmap_lazyfix: spun for 50000000\n"); 1338 } 1339} 1340 1341#else /* SMP */ 1342 1343/* 1344 * Cleaning up on uniprocessor is easy. For various reasons, we're 1345 * unlikely to have to even execute this code, including the fact 1346 * that the cleanup is deferred until the parent does a wait(2), which 1347 * means that another userland process has run. 1348 */ 1349static void 1350pmap_lazyfix(pmap_t pmap) 1351{ 1352 u_int cr3; 1353 1354 cr3 = vtophys(pmap->pm_pdir); 1355 if (cr3 == rcr3()) { 1356 load_cr3(PCPU_GET(curpcb)->pcb_cr3); 1357 pmap->pm_active &= ~(PCPU_GET(cpumask)); 1358 } 1359} 1360#endif /* SMP */ 1361 1362/* 1363 * Release any resources held by the given physical map. 1364 * Called when a pmap initialized by pmap_pinit is being released. 1365 * Should only be called if the map contains no valid mappings. 1366 */ 1367void 1368pmap_release(pmap_t pmap) 1369{ 1370 vm_page_t m, ptdpg[NPGPTD]; 1371 int i; 1372 1373 KASSERT(pmap->pm_stats.resident_count == 0, 1374 ("pmap_release: pmap resident count %ld != 0", 1375 pmap->pm_stats.resident_count)); 1376 1377 pmap_lazyfix(pmap); 1378 mtx_lock_spin(&allpmaps_lock); 1379 LIST_REMOVE(pmap, pm_list); 1380 mtx_unlock_spin(&allpmaps_lock); 1381 1382 for (i = 0; i < NPGPTD; i++) 1383 ptdpg[i] = PHYS_TO_VM_PAGE(pmap->pm_pdir[PTDPTDI + i]); 1384 1385 bzero(pmap->pm_pdir + PTDPTDI, (nkpt + NPGPTD) * 1386 sizeof(*pmap->pm_pdir)); 1387#ifdef SMP 1388 pmap->pm_pdir[MPPTDI] = 0; 1389#endif 1390 1391 pmap_qremove((vm_offset_t)pmap->pm_pdir, NPGPTD); 1392 1393 vm_page_lock_queues(); 1394 for (i = 0; i < NPGPTD; i++) { 1395 m = ptdpg[i]; 1396#ifdef PAE 1397 KASSERT(VM_PAGE_TO_PHYS(m) == (pmap->pm_pdpt[i] & PG_FRAME), 1398 ("pmap_release: got wrong ptd page")); 1399#endif 1400 m->wire_count--; 1401 atomic_subtract_int(&cnt.v_wire_count, 1); 1402 vm_page_busy(m); 1403 vm_page_free_zero(m); 1404 } 1405 vm_page_unlock_queues(); 1406} 1407 1408static int 1409kvm_size(SYSCTL_HANDLER_ARGS) 1410{ 1411 unsigned long ksize = VM_MAX_KERNEL_ADDRESS - KERNBASE; 1412 1413 return sysctl_handle_long(oidp, &ksize, 0, req); 1414} 1415SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD, 1416 0, 0, kvm_size, "IU", "Size of KVM"); 1417 1418static int 1419kvm_free(SYSCTL_HANDLER_ARGS) 1420{ 1421 unsigned long kfree = VM_MAX_KERNEL_ADDRESS - kernel_vm_end; 1422 1423 return sysctl_handle_long(oidp, &kfree, 0, req); 1424} 1425SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD, 1426 0, 0, kvm_free, "IU", "Amount of KVM free"); 1427 1428/* 1429 * grow the number of kernel page table entries, if needed 1430 */ 1431void 1432pmap_growkernel(vm_offset_t addr) 1433{ 1434 struct pmap *pmap; 1435 int s; 1436 vm_paddr_t ptppaddr; 1437 vm_page_t nkpg; 1438 pd_entry_t newpdir; 1439 pt_entry_t *pde; 1440 1441 s = splhigh(); 1442 mtx_assert(&kernel_map->system_mtx, MA_OWNED); 1443 if (kernel_vm_end == 0) { 1444 kernel_vm_end = KERNBASE; 1445 nkpt = 0; 1446 while (pdir_pde(PTD, kernel_vm_end)) { 1447 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1); 1448 nkpt++; 1449 } 1450 } 1451 addr = roundup2(addr, PAGE_SIZE * NPTEPG); 1452 while (kernel_vm_end < addr) { 1453 if (pdir_pde(PTD, kernel_vm_end)) { 1454 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1); 1455 continue; 1456 } 1457 1458 /* 1459 * This index is bogus, but out of the way 1460 */ 1461 nkpg = vm_page_alloc(NULL, nkpt, 1462 VM_ALLOC_NOOBJ | VM_ALLOC_SYSTEM | VM_ALLOC_WIRED); 1463 if (!nkpg) 1464 panic("pmap_growkernel: no memory to grow kernel"); 1465 1466 nkpt++; 1467 1468 pmap_zero_page(nkpg); 1469 ptppaddr = VM_PAGE_TO_PHYS(nkpg); 1470 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M); 1471 pdir_pde(PTD, kernel_vm_end) = newpdir; 1472 1473 mtx_lock_spin(&allpmaps_lock); 1474 LIST_FOREACH(pmap, &allpmaps, pm_list) { 1475 pde = pmap_pde(pmap, kernel_vm_end); 1476 pde_store(pde, newpdir); 1477 } 1478 mtx_unlock_spin(&allpmaps_lock); 1479 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1); 1480 } 1481 splx(s); 1482} 1483 1484 1485/*************************************************** 1486 * page management routines. 1487 ***************************************************/ 1488 1489/* 1490 * free the pv_entry back to the free list 1491 */ 1492static PMAP_INLINE void 1493free_pv_entry(pv_entry_t pv) 1494{ 1495 pv_entry_count--; 1496 uma_zfree(pvzone, pv); 1497} 1498 1499/* 1500 * get a new pv_entry, allocating a block from the system 1501 * when needed. 1502 * the memory allocation is performed bypassing the malloc code 1503 * because of the possibility of allocations at interrupt time. 1504 */ 1505static pv_entry_t 1506get_pv_entry(void) 1507{ 1508 pv_entry_count++; 1509 if (pv_entry_high_water && 1510 (pv_entry_count > pv_entry_high_water) && 1511 (pmap_pagedaemon_waken == 0)) { 1512 pmap_pagedaemon_waken = 1; 1513 wakeup (&vm_pages_needed); 1514 } 1515 return uma_zalloc(pvzone, M_NOWAIT); 1516} 1517 1518/* 1519 * If it is the first entry on the list, it is actually 1520 * in the header and we must copy the following entry up 1521 * to the header. Otherwise we must search the list for 1522 * the entry. In either case we free the now unused entry. 1523 */ 1524 1525static int 1526pmap_remove_entry(pmap_t pmap, vm_page_t m, vm_offset_t va) 1527{ 1528 pv_entry_t pv; 1529 int rtval; 1530 int s; 1531 1532 s = splvm(); 1533 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1534 if (m->md.pv_list_count < pmap->pm_stats.resident_count) { 1535 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) { 1536 if (pmap == pv->pv_pmap && va == pv->pv_va) 1537 break; 1538 } 1539 } else { 1540 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) { 1541 if (va == pv->pv_va) 1542 break; 1543 } 1544 } 1545 1546 rtval = 0; 1547 if (pv) { 1548 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem); 1549 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list); 1550 m->md.pv_list_count--; 1551 if (TAILQ_FIRST(&m->md.pv_list) == NULL) 1552 vm_page_flag_clear(m, PG_WRITEABLE); 1553 1554 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist); 1555 free_pv_entry(pv); 1556 } 1557 1558 splx(s); 1559 return rtval; 1560} 1561 1562/* 1563 * Create a pv entry for page at pa for 1564 * (pmap, va). 1565 */ 1566static void 1567pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m) 1568{ 1569 1570 int s; 1571 pv_entry_t pv; 1572 1573 s = splvm(); 1574 pv = get_pv_entry(); 1575 pv->pv_va = va; 1576 pv->pv_pmap = pmap; 1577 pv->pv_ptem = mpte; 1578 1579 vm_page_lock_queues(); 1580 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist); 1581 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list); 1582 m->md.pv_list_count++; 1583 1584 vm_page_unlock_queues(); 1585 splx(s); 1586} 1587 1588/* 1589 * pmap_remove_pte: do the things to unmap a page in a process 1590 */ 1591static int 1592pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t va) 1593{ 1594 pt_entry_t oldpte; 1595 vm_page_t m, mpte; 1596 1597 oldpte = pte_load_clear(ptq); 1598 if (oldpte & PG_W) 1599 pmap->pm_stats.wired_count -= 1; 1600 /* 1601 * Machines that don't support invlpg, also don't support 1602 * PG_G. 1603 */ 1604 if (oldpte & PG_G) 1605 pmap_invalidate_page(kernel_pmap, va); 1606 pmap->pm_stats.resident_count -= 1; 1607 if (oldpte & PG_MANAGED) { 1608 m = PHYS_TO_VM_PAGE(oldpte); 1609 if (oldpte & PG_M) { 1610#if defined(PMAP_DIAGNOSTIC) 1611 if (pmap_nw_modified((pt_entry_t) oldpte)) { 1612 printf( 1613 "pmap_remove: modified page not writable: va: 0x%x, pte: 0x%x\n", 1614 va, oldpte); 1615 } 1616#endif 1617 if (pmap_track_modified(va)) 1618 vm_page_dirty(m); 1619 } 1620 if (oldpte & PG_A) 1621 vm_page_flag_set(m, PG_REFERENCED); 1622 return pmap_remove_entry(pmap, m, va); 1623 } else { 1624 mpte = PHYS_TO_VM_PAGE(*pmap_pde(pmap, va)); 1625 return pmap_unuse_pt(pmap, va, mpte); 1626 } 1627} 1628 1629/* 1630 * Remove a single page from a process address space 1631 */ 1632static void 1633pmap_remove_page(pmap_t pmap, vm_offset_t va) 1634{ 1635 pt_entry_t *pte; 1636 1637 if ((pte = pmap_pte_quick(pmap, va)) == NULL || *pte == 0) 1638 return; 1639 pmap_remove_pte(pmap, pte, va); 1640 pmap_invalidate_page(pmap, va); 1641} 1642 1643/* 1644 * Remove the given range of addresses from the specified map. 1645 * 1646 * It is assumed that the start and end are properly 1647 * rounded to the page size. 1648 */ 1649void 1650pmap_remove(pmap_t pmap, vm_offset_t sva, vm_offset_t eva) 1651{ 1652 vm_offset_t pdnxt; 1653 pd_entry_t ptpaddr; 1654 pt_entry_t *pte; 1655 int anyvalid; 1656 1657 if (pmap == NULL) 1658 return; 1659 1660 if (pmap->pm_stats.resident_count == 0) 1661 return; 1662 1663 /* 1664 * special handling of removing one page. a very 1665 * common operation and easy to short circuit some 1666 * code. 1667 */ 1668 if ((sva + PAGE_SIZE == eva) && 1669 ((pmap->pm_pdir[(sva >> PDRSHIFT)] & PG_PS) == 0)) { 1670 pmap_remove_page(pmap, sva); 1671 return; 1672 } 1673 1674 anyvalid = 0; 1675 1676 for (; sva < eva; sva = pdnxt) { 1677 unsigned pdirindex; 1678 1679 /* 1680 * Calculate index for next page table. 1681 */ 1682 pdnxt = (sva + NBPDR) & ~PDRMASK; 1683 if (pmap->pm_stats.resident_count == 0) 1684 break; 1685 1686 pdirindex = sva >> PDRSHIFT; 1687 ptpaddr = pmap->pm_pdir[pdirindex]; 1688 1689 /* 1690 * Weed out invalid mappings. Note: we assume that the page 1691 * directory table is always allocated, and in kernel virtual. 1692 */ 1693 if (ptpaddr == 0) 1694 continue; 1695 1696 /* 1697 * Check for large page. 1698 */ 1699 if ((ptpaddr & PG_PS) != 0) { 1700 pmap->pm_pdir[pdirindex] = 0; 1701 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE; 1702 anyvalid = 1; 1703 continue; 1704 } 1705 1706 /* 1707 * Limit our scan to either the end of the va represented 1708 * by the current page table page, or to the end of the 1709 * range being removed. 1710 */ 1711 if (pdnxt > eva) 1712 pdnxt = eva; 1713 1714 for (; sva != pdnxt; sva += PAGE_SIZE) { 1715 if ((pte = pmap_pte_quick(pmap, sva)) == NULL || 1716 *pte == 0) 1717 continue; 1718 anyvalid = 1; 1719 if (pmap_remove_pte(pmap, pte, sva)) 1720 break; 1721 } 1722 } 1723 1724 if (anyvalid) 1725 pmap_invalidate_all(pmap); 1726} 1727 1728/* 1729 * Routine: pmap_remove_all 1730 * Function: 1731 * Removes this physical page from 1732 * all physical maps in which it resides. 1733 * Reflects back modify bits to the pager. 1734 * 1735 * Notes: 1736 * Original versions of this routine were very 1737 * inefficient because they iteratively called 1738 * pmap_remove (slow...) 1739 */ 1740 1741void 1742pmap_remove_all(vm_page_t m) 1743{ 1744 register pv_entry_t pv; 1745 pt_entry_t *pte, tpte; 1746 int s; 1747 1748#if defined(PMAP_DIAGNOSTIC) 1749 /* 1750 * XXX This makes pmap_remove_all() illegal for non-managed pages! 1751 */ 1752 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) { 1753 panic("pmap_remove_all: illegal for unmanaged page, va: 0x%x", 1754 VM_PAGE_TO_PHYS(m)); 1755 } 1756#endif 1757 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 1758 s = splvm(); 1759 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) { 1760 pv->pv_pmap->pm_stats.resident_count--; 1761 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va); 1762 tpte = pte_load_clear(pte); 1763 if (tpte & PG_W) 1764 pv->pv_pmap->pm_stats.wired_count--; 1765 if (tpte & PG_A) 1766 vm_page_flag_set(m, PG_REFERENCED); 1767 1768 /* 1769 * Update the vm_page_t clean and reference bits. 1770 */ 1771 if (tpte & PG_M) { 1772#if defined(PMAP_DIAGNOSTIC) 1773 if (pmap_nw_modified((pt_entry_t) tpte)) { 1774 printf( 1775 "pmap_remove_all: modified page not writable: va: 0x%x, pte: 0x%x\n", 1776 pv->pv_va, tpte); 1777 } 1778#endif 1779 if (pmap_track_modified(pv->pv_va)) 1780 vm_page_dirty(m); 1781 } 1782 pmap_invalidate_page(pv->pv_pmap, pv->pv_va); 1783 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist); 1784 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list); 1785 m->md.pv_list_count--; 1786 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem); 1787 free_pv_entry(pv); 1788 } 1789 vm_page_flag_clear(m, PG_WRITEABLE); 1790 splx(s); 1791} 1792 1793/* 1794 * Set the physical protection on the 1795 * specified range of this map as requested. 1796 */ 1797void 1798pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot) 1799{ 1800 vm_offset_t pdnxt; 1801 pd_entry_t ptpaddr; 1802 int anychanged; 1803 1804 if (pmap == NULL) 1805 return; 1806 1807 if ((prot & VM_PROT_READ) == VM_PROT_NONE) { 1808 pmap_remove(pmap, sva, eva); 1809 return; 1810 } 1811 1812 if (prot & VM_PROT_WRITE) 1813 return; 1814 1815 anychanged = 0; 1816 1817 for (; sva < eva; sva = pdnxt) { 1818 unsigned pdirindex; 1819 1820 pdnxt = (sva + NBPDR) & ~PDRMASK; 1821 1822 pdirindex = sva >> PDRSHIFT; 1823 ptpaddr = pmap->pm_pdir[pdirindex]; 1824 1825 /* 1826 * Weed out invalid mappings. Note: we assume that the page 1827 * directory table is always allocated, and in kernel virtual. 1828 */ 1829 if (ptpaddr == 0) 1830 continue; 1831 1832 /* 1833 * Check for large page. 1834 */ 1835 if ((ptpaddr & PG_PS) != 0) { 1836 pmap->pm_pdir[pdirindex] &= ~(PG_M|PG_RW); 1837 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE; 1838 anychanged = 1; 1839 continue; 1840 } 1841 1842 if (pdnxt > eva) 1843 pdnxt = eva; 1844 1845 for (; sva != pdnxt; sva += PAGE_SIZE) { 1846 pt_entry_t pbits; 1847 pt_entry_t *pte; 1848 vm_page_t m; 1849 1850 if ((pte = pmap_pte_quick(pmap, sva)) == NULL) 1851 continue; 1852 pbits = *pte; 1853 if (pbits & PG_MANAGED) { 1854 m = NULL; 1855 if (pbits & PG_A) { 1856 m = PHYS_TO_VM_PAGE(pbits); 1857 vm_page_flag_set(m, PG_REFERENCED); 1858 pbits &= ~PG_A; 1859 } 1860 if ((pbits & PG_M) != 0 && 1861 pmap_track_modified(sva)) { 1862 if (m == NULL) 1863 m = PHYS_TO_VM_PAGE(pbits); 1864 vm_page_dirty(m); 1865 pbits &= ~PG_M; 1866 } 1867 } 1868 1869 pbits &= ~PG_RW; 1870 1871 if (pbits != *pte) { 1872 pte_store(pte, pbits); 1873 anychanged = 1; 1874 } 1875 } 1876 } 1877 if (anychanged) 1878 pmap_invalidate_all(pmap); 1879} 1880 1881/* 1882 * Insert the given physical page (p) at 1883 * the specified virtual address (v) in the 1884 * target physical map with the protection requested. 1885 * 1886 * If specified, the page will be wired down, meaning 1887 * that the related pte can not be reclaimed. 1888 * 1889 * NB: This is the only routine which MAY NOT lazy-evaluate 1890 * or lose information. That is, this routine must actually 1891 * insert this page into the given map NOW. 1892 */ 1893void 1894pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot, 1895 boolean_t wired) 1896{ 1897 vm_paddr_t pa; 1898 register pt_entry_t *pte; 1899 vm_paddr_t opa; 1900 pt_entry_t origpte, newpte; 1901 vm_page_t mpte; 1902 1903 if (pmap == NULL) 1904 return; 1905 1906 va &= PG_FRAME; 1907#ifdef PMAP_DIAGNOSTIC 1908 if (va > VM_MAX_KERNEL_ADDRESS) 1909 panic("pmap_enter: toobig"); 1910 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS)) 1911 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%x)", va); 1912#endif 1913 1914 mpte = NULL; 1915 /* 1916 * In the case that a page table page is not 1917 * resident, we are creating it here. 1918 */ 1919 if (va < VM_MAXUSER_ADDRESS) { 1920 mpte = pmap_allocpte(pmap, va); 1921 } 1922#if 0 && defined(PMAP_DIAGNOSTIC) 1923 else { 1924 pd_entry_t *pdeaddr = pmap_pde(pmap, va); 1925 origpte = *pdeaddr; 1926 if ((origpte & PG_V) == 0) { 1927 panic("pmap_enter: invalid kernel page table page, pdir=%p, pde=%p, va=%p\n", 1928 pmap->pm_pdir[PTDPTDI], origpte, va); 1929 } 1930 } 1931#endif 1932 1933 pte = pmap_pte_quick(pmap, va); 1934 1935 /* 1936 * Page Directory table entry not valid, we need a new PT page 1937 */ 1938 if (pte == NULL) { 1939 panic("pmap_enter: invalid page directory pdir=%#jx, va=%#x\n", 1940 (uintmax_t)pmap->pm_pdir[PTDPTDI], va); 1941 } 1942 1943 pa = VM_PAGE_TO_PHYS(m) & PG_FRAME; 1944 origpte = *pte; 1945 opa = origpte & PG_FRAME; 1946 1947 if (origpte & PG_PS) 1948 panic("pmap_enter: attempted pmap_enter on 4MB page"); 1949 1950 /* 1951 * Mapping has not changed, must be protection or wiring change. 1952 */ 1953 if (origpte && (opa == pa)) { 1954 /* 1955 * Wiring change, just update stats. We don't worry about 1956 * wiring PT pages as they remain resident as long as there 1957 * are valid mappings in them. Hence, if a user page is wired, 1958 * the PT page will be also. 1959 */ 1960 if (wired && ((origpte & PG_W) == 0)) 1961 pmap->pm_stats.wired_count++; 1962 else if (!wired && (origpte & PG_W)) 1963 pmap->pm_stats.wired_count--; 1964 1965#if defined(PMAP_DIAGNOSTIC) 1966 if (pmap_nw_modified((pt_entry_t) origpte)) { 1967 printf( 1968 "pmap_enter: modified page not writable: va: 0x%x, pte: 0x%x\n", 1969 va, origpte); 1970 } 1971#endif 1972 1973 /* 1974 * Remove extra pte reference 1975 */ 1976 if (mpte) 1977 mpte->hold_count--; 1978 1979 if ((prot & VM_PROT_WRITE) && (origpte & PG_V)) { 1980 if ((origpte & PG_RW) == 0) { 1981 pte_store(pte, origpte | PG_RW); 1982 pmap_invalidate_page(pmap, va); 1983 } 1984 return; 1985 } 1986 1987 /* 1988 * We might be turning off write access to the page, 1989 * so we go ahead and sense modify status. 1990 */ 1991 if (origpte & PG_MANAGED) { 1992 if ((origpte & PG_M) && pmap_track_modified(va)) { 1993 vm_page_t om; 1994 om = PHYS_TO_VM_PAGE(opa); 1995 vm_page_dirty(om); 1996 } 1997 pa |= PG_MANAGED; 1998 } 1999 goto validate; 2000 } 2001 /* 2002 * Mapping has changed, invalidate old range and fall through to 2003 * handle validating new mapping. 2004 */ 2005 if (opa) { 2006 int err; 2007 vm_page_lock_queues(); 2008 err = pmap_remove_pte(pmap, pte, va); 2009 vm_page_unlock_queues(); 2010 if (err) 2011 panic("pmap_enter: pte vanished, va: 0x%x", va); 2012 } 2013 2014 /* 2015 * Enter on the PV list if part of our managed memory. Note that we 2016 * raise IPL while manipulating pv_table since pmap_enter can be 2017 * called at interrupt time. 2018 */ 2019 if (pmap_initialized && 2020 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) { 2021 pmap_insert_entry(pmap, va, mpte, m); 2022 pa |= PG_MANAGED; 2023 } 2024 2025 /* 2026 * Increment counters 2027 */ 2028 pmap->pm_stats.resident_count++; 2029 if (wired) 2030 pmap->pm_stats.wired_count++; 2031 2032validate: 2033 /* 2034 * Now validate mapping with desired protection/wiring. 2035 */ 2036 newpte = (pt_entry_t)(pa | pte_prot(pmap, prot) | PG_V); 2037 2038 if (wired) 2039 newpte |= PG_W; 2040 if (va < VM_MAXUSER_ADDRESS) 2041 newpte |= PG_U; 2042 if (pmap == kernel_pmap) 2043 newpte |= pgeflag; 2044 2045 /* 2046 * if the mapping or permission bits are different, we need 2047 * to update the pte. 2048 */ 2049 if ((origpte & ~(PG_M|PG_A)) != newpte) { 2050 pte_store(pte, newpte | PG_A); 2051 /*if (origpte)*/ { 2052 pmap_invalidate_page(pmap, va); 2053 } 2054 } 2055} 2056 2057/* 2058 * this code makes some *MAJOR* assumptions: 2059 * 1. Current pmap & pmap exists. 2060 * 2. Not wired. 2061 * 3. Read access. 2062 * 4. No page table pages. 2063 * 5. Tlbflush is deferred to calling procedure. 2064 * 6. Page IS managed. 2065 * but is *MUCH* faster than pmap_enter... 2066 */ 2067 2068vm_page_t 2069pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_page_t mpte) 2070{ 2071 pt_entry_t *pte; 2072 vm_paddr_t pa; 2073 2074 /* 2075 * In the case that a page table page is not 2076 * resident, we are creating it here. 2077 */ 2078 if (va < VM_MAXUSER_ADDRESS) { 2079 unsigned ptepindex; 2080 pd_entry_t ptepa; 2081 2082 /* 2083 * Calculate pagetable page index 2084 */ 2085 ptepindex = va >> PDRSHIFT; 2086 if (mpte && (mpte->pindex == ptepindex)) { 2087 mpte->hold_count++; 2088 } else { 2089 /* 2090 * Get the page directory entry 2091 */ 2092 ptepa = pmap->pm_pdir[ptepindex]; 2093 2094 /* 2095 * If the page table page is mapped, we just increment 2096 * the hold count, and activate it. 2097 */ 2098 if (ptepa) { 2099 if (ptepa & PG_PS) 2100 panic("pmap_enter_quick: unexpected mapping into 4MB page"); 2101 mpte = PHYS_TO_VM_PAGE(ptepa); 2102 mpte->hold_count++; 2103 } else { 2104 mpte = _pmap_allocpte(pmap, ptepindex); 2105 } 2106 } 2107 } else { 2108 mpte = NULL; 2109 } 2110 2111 /* 2112 * This call to vtopte makes the assumption that we are 2113 * entering the page into the current pmap. In order to support 2114 * quick entry into any pmap, one would likely use pmap_pte_quick. 2115 * But that isn't as quick as vtopte. 2116 */ 2117 pte = vtopte(va); 2118 if (*pte) { 2119 if (mpte != NULL) { 2120 vm_page_lock_queues(); 2121 pmap_unwire_pte_hold(pmap, mpte); 2122 vm_page_unlock_queues(); 2123 } 2124 return 0; 2125 } 2126 2127 /* 2128 * Enter on the PV list if part of our managed memory. Note that we 2129 * raise IPL while manipulating pv_table since pmap_enter can be 2130 * called at interrupt time. 2131 */ 2132 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) 2133 pmap_insert_entry(pmap, va, mpte, m); 2134 2135 /* 2136 * Increment counters 2137 */ 2138 pmap->pm_stats.resident_count++; 2139 2140 pa = VM_PAGE_TO_PHYS(m); 2141 2142 /* 2143 * Now validate mapping with RO protection 2144 */ 2145 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) 2146 pte_store(pte, pa | PG_V | PG_U); 2147 else 2148 pte_store(pte, pa | PG_V | PG_U | PG_MANAGED); 2149 2150 return mpte; 2151} 2152 2153/* 2154 * Make a temporary mapping for a physical address. This is only intended 2155 * to be used for panic dumps. 2156 */ 2157void * 2158pmap_kenter_temporary(vm_offset_t pa, int i) 2159{ 2160 vm_offset_t va; 2161 2162 va = (vm_offset_t)crashdumpmap + (i * PAGE_SIZE); 2163 pmap_kenter(va, pa); 2164#ifndef I386_CPU 2165 invlpg(va); 2166#else 2167 invltlb(); 2168#endif 2169 return ((void *)crashdumpmap); 2170} 2171 2172/* 2173 * This code maps large physical mmap regions into the 2174 * processor address space. Note that some shortcuts 2175 * are taken, but the code works. 2176 */ 2177void 2178pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, 2179 vm_object_t object, vm_pindex_t pindex, 2180 vm_size_t size) 2181{ 2182 vm_page_t p; 2183 2184 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED); 2185 KASSERT(object->type == OBJT_DEVICE, 2186 ("pmap_object_init_pt: non-device object")); 2187 if (pseflag && 2188 ((addr & (NBPDR - 1)) == 0) && ((size & (NBPDR - 1)) == 0)) { 2189 int i; 2190 vm_page_t m[1]; 2191 unsigned int ptepindex; 2192 int npdes; 2193 pd_entry_t ptepa; 2194 2195 if (pmap->pm_pdir[ptepindex = (addr >> PDRSHIFT)]) 2196 return; 2197retry: 2198 p = vm_page_lookup(object, pindex); 2199 if (p != NULL) { 2200 vm_page_lock_queues(); 2201 if (vm_page_sleep_if_busy(p, FALSE, "init4p")) 2202 goto retry; 2203 } else { 2204 p = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL); 2205 if (p == NULL) 2206 return; 2207 m[0] = p; 2208 2209 if (vm_pager_get_pages(object, m, 1, 0) != VM_PAGER_OK) { 2210 vm_page_lock_queues(); 2211 vm_page_free(p); 2212 vm_page_unlock_queues(); 2213 return; 2214 } 2215 2216 p = vm_page_lookup(object, pindex); 2217 vm_page_lock_queues(); 2218 vm_page_wakeup(p); 2219 } 2220 vm_page_unlock_queues(); 2221 2222 ptepa = VM_PAGE_TO_PHYS(p); 2223 if (ptepa & (NBPDR - 1)) 2224 return; 2225 2226 p->valid = VM_PAGE_BITS_ALL; 2227 2228 pmap->pm_stats.resident_count += size >> PAGE_SHIFT; 2229 npdes = size >> PDRSHIFT; 2230 for(i = 0; i < npdes; i++) { 2231 pde_store(&pmap->pm_pdir[ptepindex], 2232 ptepa | PG_U | PG_RW | PG_V | PG_PS); 2233 ptepa += NBPDR; 2234 ptepindex += 1; 2235 } 2236 pmap_invalidate_all(pmap); 2237 } 2238} 2239 2240/* 2241 * pmap_prefault provides a quick way of clustering 2242 * pagefaults into a processes address space. It is a "cousin" 2243 * of pmap_object_init_pt, except it runs at page fault time instead 2244 * of mmap time. 2245 */ 2246#define PFBAK 4 2247#define PFFOR 4 2248#define PAGEORDER_SIZE (PFBAK+PFFOR) 2249 2250static int pmap_prefault_pageorder[] = { 2251 -1 * PAGE_SIZE, 1 * PAGE_SIZE, 2252 -2 * PAGE_SIZE, 2 * PAGE_SIZE, 2253 -3 * PAGE_SIZE, 3 * PAGE_SIZE, 2254 -4 * PAGE_SIZE, 4 * PAGE_SIZE 2255}; 2256 2257void 2258pmap_prefault(pmap, addra, entry) 2259 pmap_t pmap; 2260 vm_offset_t addra; 2261 vm_map_entry_t entry; 2262{ 2263 int i; 2264 vm_offset_t starta; 2265 vm_offset_t addr; 2266 vm_pindex_t pindex; 2267 vm_page_t m, mpte; 2268 vm_object_t object; 2269 2270 if (!curthread || (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))) 2271 return; 2272 2273 object = entry->object.vm_object; 2274 2275 starta = addra - PFBAK * PAGE_SIZE; 2276 if (starta < entry->start) { 2277 starta = entry->start; 2278 } else if (starta > addra) { 2279 starta = 0; 2280 } 2281 2282 mpte = NULL; 2283 for (i = 0; i < PAGEORDER_SIZE; i++) { 2284 vm_object_t backing_object, lobject; 2285 pt_entry_t *pte; 2286 2287 addr = addra + pmap_prefault_pageorder[i]; 2288 if (addr > addra + (PFFOR * PAGE_SIZE)) 2289 addr = 0; 2290 2291 if (addr < starta || addr >= entry->end) 2292 continue; 2293 2294 if ((*pmap_pde(pmap, addr)) == 0) 2295 continue; 2296 2297 pte = vtopte(addr); 2298 if (*pte) 2299 continue; 2300 2301 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT; 2302 lobject = object; 2303 VM_OBJECT_LOCK(lobject); 2304 while ((m = vm_page_lookup(lobject, pindex)) == NULL && 2305 lobject->type == OBJT_DEFAULT && 2306 (backing_object = lobject->backing_object) != NULL) { 2307 if (lobject->backing_object_offset & PAGE_MASK) 2308 break; 2309 pindex += lobject->backing_object_offset >> PAGE_SHIFT; 2310 VM_OBJECT_LOCK(backing_object); 2311 VM_OBJECT_UNLOCK(lobject); 2312 lobject = backing_object; 2313 } 2314 VM_OBJECT_UNLOCK(lobject); 2315 /* 2316 * give-up when a page is not in memory 2317 */ 2318 if (m == NULL) 2319 break; 2320 vm_page_lock_queues(); 2321 if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) && 2322 (m->busy == 0) && 2323 (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) { 2324 2325 if ((m->queue - m->pc) == PQ_CACHE) { 2326 vm_page_deactivate(m); 2327 } 2328 vm_page_busy(m); 2329 vm_page_unlock_queues(); 2330 mpte = pmap_enter_quick(pmap, addr, m, mpte); 2331 vm_page_lock_queues(); 2332 vm_page_wakeup(m); 2333 } 2334 vm_page_unlock_queues(); 2335 } 2336} 2337 2338/* 2339 * Routine: pmap_change_wiring 2340 * Function: Change the wiring attribute for a map/virtual-address 2341 * pair. 2342 * In/out conditions: 2343 * The mapping must already exist in the pmap. 2344 */ 2345void 2346pmap_change_wiring(pmap, va, wired) 2347 register pmap_t pmap; 2348 vm_offset_t va; 2349 boolean_t wired; 2350{ 2351 register pt_entry_t *pte; 2352 2353 if (pmap == NULL) 2354 return; 2355 2356 pte = pmap_pte_quick(pmap, va); 2357 2358 if (wired && !pmap_pte_w(pte)) 2359 pmap->pm_stats.wired_count++; 2360 else if (!wired && pmap_pte_w(pte)) 2361 pmap->pm_stats.wired_count--; 2362 2363 /* 2364 * Wiring is not a hardware characteristic so there is no need to 2365 * invalidate TLB. 2366 */ 2367 pmap_pte_set_w(pte, wired); 2368} 2369 2370 2371 2372/* 2373 * Copy the range specified by src_addr/len 2374 * from the source map to the range dst_addr/len 2375 * in the destination map. 2376 * 2377 * This routine is only advisory and need not do anything. 2378 */ 2379 2380void 2381pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, vm_size_t len, 2382 vm_offset_t src_addr) 2383{ 2384 vm_offset_t addr; 2385 vm_offset_t end_addr = src_addr + len; 2386 vm_offset_t pdnxt; 2387 vm_page_t m; 2388 2389 if (dst_addr != src_addr) 2390 return; 2391 2392 if (!pmap_is_current(src_pmap)) 2393 return; 2394 2395 for (addr = src_addr; addr < end_addr; addr = pdnxt) { 2396 pt_entry_t *src_pte, *dst_pte; 2397 vm_page_t dstmpte, srcmpte; 2398 pd_entry_t srcptepaddr; 2399 unsigned ptepindex; 2400 2401 if (addr >= UPT_MIN_ADDRESS) 2402 panic("pmap_copy: invalid to pmap_copy page tables\n"); 2403 2404 /* 2405 * Don't let optional prefaulting of pages make us go 2406 * way below the low water mark of free pages or way 2407 * above high water mark of used pv entries. 2408 */ 2409 if (cnt.v_free_count < cnt.v_free_reserved || 2410 pv_entry_count > pv_entry_high_water) 2411 break; 2412 2413 pdnxt = (addr + NBPDR) & ~PDRMASK; 2414 ptepindex = addr >> PDRSHIFT; 2415 2416 srcptepaddr = src_pmap->pm_pdir[ptepindex]; 2417 if (srcptepaddr == 0) 2418 continue; 2419 2420 if (srcptepaddr & PG_PS) { 2421 if (dst_pmap->pm_pdir[ptepindex] == 0) { 2422 dst_pmap->pm_pdir[ptepindex] = srcptepaddr; 2423 dst_pmap->pm_stats.resident_count += 2424 NBPDR / PAGE_SIZE; 2425 } 2426 continue; 2427 } 2428 2429 srcmpte = PHYS_TO_VM_PAGE(srcptepaddr); 2430 if (srcmpte->hold_count == 0 || (srcmpte->flags & PG_BUSY)) 2431 continue; 2432 2433 if (pdnxt > end_addr) 2434 pdnxt = end_addr; 2435 2436 src_pte = vtopte(addr); 2437 while (addr < pdnxt) { 2438 pt_entry_t ptetemp; 2439 ptetemp = *src_pte; 2440 /* 2441 * we only virtual copy managed pages 2442 */ 2443 if ((ptetemp & PG_MANAGED) != 0) { 2444 /* 2445 * We have to check after allocpte for the 2446 * pte still being around... allocpte can 2447 * block. 2448 */ 2449 dstmpte = pmap_allocpte(dst_pmap, addr); 2450 dst_pte = pmap_pte_quick(dst_pmap, addr); 2451 if ((*dst_pte == 0) && (ptetemp = *src_pte)) { 2452 /* 2453 * Clear the modified and 2454 * accessed (referenced) bits 2455 * during the copy. 2456 */ 2457 m = PHYS_TO_VM_PAGE(ptetemp); 2458 *dst_pte = ptetemp & ~(PG_M | PG_A); 2459 dst_pmap->pm_stats.resident_count++; 2460 pmap_insert_entry(dst_pmap, addr, 2461 dstmpte, m); 2462 } else { 2463 vm_page_lock_queues(); 2464 pmap_unwire_pte_hold(dst_pmap, dstmpte); 2465 vm_page_unlock_queues(); 2466 } 2467 if (dstmpte->hold_count >= srcmpte->hold_count) 2468 break; 2469 } 2470 addr += PAGE_SIZE; 2471 src_pte++; 2472 } 2473 } 2474} 2475 2476#ifdef SMP 2477 2478/* 2479 * pmap_zpi_switchin*() 2480 * 2481 * These functions allow us to avoid doing IPIs alltogether in certain 2482 * temporary page-mapping situations (page zeroing). Instead to deal 2483 * with being preempted and moved onto a different cpu we invalidate 2484 * the page when the scheduler switches us in. This does not occur 2485 * very often so we remain relatively optimal with very little effort. 2486 */ 2487static void 2488pmap_zpi_switchin12(void) 2489{ 2490 invlpg((u_int)CADDR1); 2491 invlpg((u_int)CADDR2); 2492} 2493 2494static void 2495pmap_zpi_switchin2(void) 2496{ 2497 invlpg((u_int)CADDR2); 2498} 2499 2500static void 2501pmap_zpi_switchin3(void) 2502{ 2503 invlpg((u_int)CADDR3); 2504} 2505 2506#endif 2507 2508/* 2509 * pmap_zero_page zeros the specified hardware page by mapping 2510 * the page into KVM and using bzero to clear its contents. 2511 */ 2512void 2513pmap_zero_page(vm_page_t m) 2514{ 2515 2516 mtx_lock(&CMAPCADDR12_lock); 2517 if (*CMAP2) 2518 panic("pmap_zero_page: CMAP2 busy"); 2519 *CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M; 2520#ifdef I386_CPU 2521 invltlb(); 2522#else 2523#ifdef SMP 2524 curthread->td_switchin = pmap_zpi_switchin2; 2525#endif 2526 invlpg((u_int)CADDR2); 2527#endif 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#ifdef SMP 2535 curthread->td_switchin = NULL; 2536#endif 2537 *CMAP2 = 0; 2538 mtx_unlock(&CMAPCADDR12_lock); 2539} 2540 2541/* 2542 * pmap_zero_page_area zeros the specified hardware page by mapping 2543 * the page into KVM and using bzero to clear its contents. 2544 * 2545 * off and size may not cover an area beyond a single hardware page. 2546 */ 2547void 2548pmap_zero_page_area(vm_page_t m, int off, int size) 2549{ 2550 2551 mtx_lock(&CMAPCADDR12_lock); 2552 if (*CMAP2) 2553 panic("pmap_zero_page: CMAP2 busy"); 2554 *CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M; 2555#ifdef I386_CPU 2556 invltlb(); 2557#else 2558#ifdef SMP 2559 curthread->td_switchin = pmap_zpi_switchin2; 2560#endif 2561 invlpg((u_int)CADDR2); 2562#endif 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#ifdef SMP 2570 curthread->td_switchin = NULL; 2571#endif 2572 *CMAP2 = 0; 2573 mtx_unlock(&CMAPCADDR12_lock); 2574} 2575 2576/* 2577 * pmap_zero_page_idle zeros the specified hardware page by mapping 2578 * the page into KVM and using bzero to clear its contents. This 2579 * is intended to be called from the vm_pagezero process only and 2580 * outside of Giant. 2581 */ 2582void 2583pmap_zero_page_idle(vm_page_t m) 2584{ 2585 2586 if (*CMAP3) 2587 panic("pmap_zero_page: CMAP3 busy"); 2588 *CMAP3 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M; 2589#ifdef I386_CPU 2590 invltlb(); 2591#else 2592#ifdef SMP 2593 curthread->td_switchin = pmap_zpi_switchin3; 2594#endif 2595 invlpg((u_int)CADDR3); 2596#endif 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#ifdef SMP 2604 curthread->td_switchin = NULL; 2605#endif 2606 *CMAP3 = 0; 2607} 2608 2609/* 2610 * pmap_copy_page copies the specified (machine independent) 2611 * page by mapping the page into virtual memory and using 2612 * bcopy to copy the page, one machine dependent page at a 2613 * time. 2614 */ 2615void 2616pmap_copy_page(vm_page_t src, vm_page_t dst) 2617{ 2618 2619 mtx_lock(&CMAPCADDR12_lock); 2620 if (*CMAP1) 2621 panic("pmap_copy_page: CMAP1 busy"); 2622 if (*CMAP2) 2623 panic("pmap_copy_page: CMAP2 busy"); 2624 *CMAP1 = PG_V | VM_PAGE_TO_PHYS(src) | PG_A; 2625 *CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(dst) | PG_A | PG_M; 2626#ifdef I386_CPU 2627 invltlb(); 2628#else 2629#ifdef SMP 2630 curthread->td_switchin = pmap_zpi_switchin12; 2631#endif 2632 invlpg((u_int)CADDR1); 2633 invlpg((u_int)CADDR2); 2634#endif 2635 bcopy(CADDR1, CADDR2, PAGE_SIZE); 2636#ifdef SMP 2637 curthread->td_switchin = NULL; 2638#endif 2639 *CMAP1 = 0; 2640 *CMAP2 = 0; 2641 mtx_unlock(&CMAPCADDR12_lock); 2642} 2643 2644/* 2645 * Returns true if the pmap's pv is one of the first 2646 * 16 pvs linked to from this page. This count may 2647 * be changed upwards or downwards in the future; it 2648 * is only necessary that true be returned for a small 2649 * subset of pmaps for proper page aging. 2650 */ 2651boolean_t 2652pmap_page_exists_quick(pmap, m) 2653 pmap_t pmap; 2654 vm_page_t m; 2655{ 2656 pv_entry_t pv; 2657 int loops = 0; 2658 int s; 2659 2660 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) 2661 return FALSE; 2662 2663 s = splvm(); 2664 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 2665 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) { 2666 if (pv->pv_pmap == pmap) { 2667 splx(s); 2668 return TRUE; 2669 } 2670 loops++; 2671 if (loops >= 16) 2672 break; 2673 } 2674 splx(s); 2675 return (FALSE); 2676} 2677 2678#define PMAP_REMOVE_PAGES_CURPROC_ONLY 2679/* 2680 * Remove all pages from specified address space 2681 * this aids process exit speeds. Also, this code 2682 * is special cased for current process only, but 2683 * can have the more generic (and slightly slower) 2684 * mode enabled. This is much faster than pmap_remove 2685 * in the case of running down an entire address space. 2686 */ 2687void 2688pmap_remove_pages(pmap, sva, eva) 2689 pmap_t pmap; 2690 vm_offset_t sva, eva; 2691{ 2692 pt_entry_t *pte, tpte; 2693 vm_page_t m; 2694 pv_entry_t pv, npv; 2695 int s; 2696 2697#ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY 2698 if (!curthread || (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))) { 2699 printf("warning: pmap_remove_pages called with non-current pmap\n"); 2700 return; 2701 } 2702#endif 2703 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 2704 s = splvm(); 2705 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) { 2706 2707 if (pv->pv_va >= eva || pv->pv_va < sva) { 2708 npv = TAILQ_NEXT(pv, pv_plist); 2709 continue; 2710 } 2711 2712#ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY 2713 pte = vtopte(pv->pv_va); 2714#else 2715 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va); 2716#endif 2717 tpte = *pte; 2718 2719 if (tpte == 0) { 2720 printf("TPTE at %p IS ZERO @ VA %08x\n", 2721 pte, pv->pv_va); 2722 panic("bad pte"); 2723 } 2724 2725/* 2726 * We cannot remove wired pages from a process' mapping at this time 2727 */ 2728 if (tpte & PG_W) { 2729 npv = TAILQ_NEXT(pv, pv_plist); 2730 continue; 2731 } 2732 2733 m = PHYS_TO_VM_PAGE(tpte); 2734 KASSERT(m->phys_addr == (tpte & PG_FRAME), 2735 ("vm_page_t %p phys_addr mismatch %016jx %016jx", 2736 m, (uintmax_t)m->phys_addr, (uintmax_t)tpte)); 2737 2738 KASSERT(m < &vm_page_array[vm_page_array_size], 2739 ("pmap_remove_pages: bad tpte %#jx", (uintmax_t)tpte)); 2740 2741 pv->pv_pmap->pm_stats.resident_count--; 2742 2743 pte_clear(pte); 2744 2745 /* 2746 * Update the vm_page_t clean and reference bits. 2747 */ 2748 if (tpte & PG_M) { 2749 vm_page_dirty(m); 2750 } 2751 2752 npv = TAILQ_NEXT(pv, pv_plist); 2753 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist); 2754 2755 m->md.pv_list_count--; 2756 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list); 2757 if (TAILQ_FIRST(&m->md.pv_list) == NULL) { 2758 vm_page_flag_clear(m, PG_WRITEABLE); 2759 } 2760 2761 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem); 2762 free_pv_entry(pv); 2763 } 2764 splx(s); 2765 pmap_invalidate_all(pmap); 2766} 2767 2768/* 2769 * pmap_is_modified: 2770 * 2771 * Return whether or not the specified physical page was modified 2772 * in any physical maps. 2773 */ 2774boolean_t 2775pmap_is_modified(vm_page_t m) 2776{ 2777 pv_entry_t pv; 2778 pt_entry_t *pte; 2779 int s; 2780 2781 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) 2782 return FALSE; 2783 2784 s = splvm(); 2785 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 2786 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) { 2787 /* 2788 * if the bit being tested is the modified bit, then 2789 * mark clean_map and ptes as never 2790 * modified. 2791 */ 2792 if (!pmap_track_modified(pv->pv_va)) 2793 continue; 2794#if defined(PMAP_DIAGNOSTIC) 2795 if (!pv->pv_pmap) { 2796 printf("Null pmap (tb) at va: 0x%x\n", pv->pv_va); 2797 continue; 2798 } 2799#endif 2800 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va); 2801 if (*pte & PG_M) { 2802 splx(s); 2803 return TRUE; 2804 } 2805 } 2806 splx(s); 2807 return (FALSE); 2808} 2809 2810/* 2811 * Clear the given bit in each of the given page's ptes. 2812 */ 2813static __inline void 2814pmap_clear_ptes(vm_page_t m, int bit) 2815{ 2816 register pv_entry_t pv; 2817 pt_entry_t pbits, *pte; 2818 int s; 2819 2820 if (!pmap_initialized || (m->flags & PG_FICTITIOUS) || 2821 (bit == PG_RW && (m->flags & PG_WRITEABLE) == 0)) 2822 return; 2823 2824 s = splvm(); 2825 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 2826 /* 2827 * Loop over all current mappings setting/clearing as appropos If 2828 * setting RO do we need to clear the VAC? 2829 */ 2830 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) { 2831 /* 2832 * don't write protect pager mappings 2833 */ 2834 if (bit == PG_RW) { 2835 if (!pmap_track_modified(pv->pv_va)) 2836 continue; 2837 } 2838 2839#if defined(PMAP_DIAGNOSTIC) 2840 if (!pv->pv_pmap) { 2841 printf("Null pmap (cb) at va: 0x%x\n", pv->pv_va); 2842 continue; 2843 } 2844#endif 2845 2846 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va); 2847 pbits = *pte; 2848 if (pbits & bit) { 2849 if (bit == PG_RW) { 2850 if (pbits & PG_M) { 2851 vm_page_dirty(m); 2852 } 2853 pte_store(pte, pbits & ~(PG_M|PG_RW)); 2854 } else { 2855 pte_store(pte, pbits & ~bit); 2856 } 2857 pmap_invalidate_page(pv->pv_pmap, pv->pv_va); 2858 } 2859 } 2860 if (bit == PG_RW) 2861 vm_page_flag_clear(m, PG_WRITEABLE); 2862 splx(s); 2863} 2864 2865/* 2866 * pmap_page_protect: 2867 * 2868 * Lower the permission for all mappings to a given page. 2869 */ 2870void 2871pmap_page_protect(vm_page_t m, vm_prot_t prot) 2872{ 2873 if ((prot & VM_PROT_WRITE) == 0) { 2874 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) { 2875 pmap_clear_ptes(m, PG_RW); 2876 } else { 2877 pmap_remove_all(m); 2878 } 2879 } 2880} 2881 2882/* 2883 * pmap_ts_referenced: 2884 * 2885 * Return a count of reference bits for a page, clearing those bits. 2886 * It is not necessary for every reference bit to be cleared, but it 2887 * is necessary that 0 only be returned when there are truly no 2888 * reference bits set. 2889 * 2890 * XXX: The exact number of bits to check and clear is a matter that 2891 * should be tested and standardized at some point in the future for 2892 * optimal aging of shared pages. 2893 */ 2894int 2895pmap_ts_referenced(vm_page_t m) 2896{ 2897 register pv_entry_t pv, pvf, pvn; 2898 pt_entry_t *pte; 2899 pt_entry_t v; 2900 int s; 2901 int rtval = 0; 2902 2903 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) 2904 return (rtval); 2905 2906 s = splvm(); 2907 mtx_assert(&vm_page_queue_mtx, MA_OWNED); 2908 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) { 2909 2910 pvf = pv; 2911 2912 do { 2913 pvn = TAILQ_NEXT(pv, pv_list); 2914 2915 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list); 2916 2917 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list); 2918 2919 if (!pmap_track_modified(pv->pv_va)) 2920 continue; 2921 2922 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va); 2923 2924 if (pte && ((v = pte_load(pte)) & PG_A) != 0) { 2925 pte_store(pte, v & ~PG_A); 2926 pmap_invalidate_page(pv->pv_pmap, pv->pv_va); 2927 2928 rtval++; 2929 if (rtval > 4) { 2930 break; 2931 } 2932 } 2933 } while ((pv = pvn) != NULL && pv != pvf); 2934 } 2935 splx(s); 2936 2937 return (rtval); 2938} 2939 2940/* 2941 * Clear the modify bits on the specified physical page. 2942 */ 2943void 2944pmap_clear_modify(vm_page_t m) 2945{ 2946 pmap_clear_ptes(m, PG_M); 2947} 2948 2949/* 2950 * pmap_clear_reference: 2951 * 2952 * Clear the reference bit on the specified physical page. 2953 */ 2954void 2955pmap_clear_reference(vm_page_t m) 2956{ 2957 pmap_clear_ptes(m, PG_A); 2958} 2959 2960/* 2961 * Miscellaneous support routines follow 2962 */ 2963 2964static void 2965i386_protection_init() 2966{ 2967 register int *kp, prot; 2968 2969 kp = protection_codes; 2970 for (prot = 0; prot < 8; prot++) { 2971 switch (prot) { 2972 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE: 2973 /* 2974 * Read access is also 0. There isn't any execute bit, 2975 * so just make it readable. 2976 */ 2977 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE: 2978 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE: 2979 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE: 2980 *kp++ = 0; 2981 break; 2982 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE: 2983 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE: 2984 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE: 2985 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE: 2986 *kp++ = PG_RW; 2987 break; 2988 } 2989 } 2990} 2991 2992/* 2993 * Map a set of physical memory pages into the kernel virtual 2994 * address space. Return a pointer to where it is mapped. This 2995 * routine is intended to be used for mapping device memory, 2996 * NOT real memory. 2997 */ 2998void * 2999pmap_mapdev(pa, size) 3000 vm_paddr_t pa; 3001 vm_size_t size; 3002{ 3003 vm_offset_t va, tmpva, offset; 3004 3005 offset = pa & PAGE_MASK; 3006 size = roundup(offset + size, PAGE_SIZE); 3007 3008 va = kmem_alloc_nofault(kernel_map, size); 3009 if (!va) 3010 panic("pmap_mapdev: Couldn't alloc kernel virtual memory"); 3011 3012 pa = pa & PG_FRAME; 3013 for (tmpva = va; size > 0; ) { 3014 pmap_kenter(tmpva, pa); 3015 size -= PAGE_SIZE; 3016 tmpva += PAGE_SIZE; 3017 pa += PAGE_SIZE; 3018 } 3019 pmap_invalidate_range(kernel_pmap, va, tmpva); 3020 return ((void *)(va + offset)); 3021} 3022 3023void 3024pmap_unmapdev(va, size) 3025 vm_offset_t va; 3026 vm_size_t size; 3027{ 3028 vm_offset_t base, offset, tmpva; 3029 pt_entry_t *pte; 3030 3031 base = va & PG_FRAME; 3032 offset = va & PAGE_MASK; 3033 size = roundup(offset + size, PAGE_SIZE); 3034 for (tmpva = base; tmpva < (base + size); tmpva += PAGE_SIZE) { 3035 pte = vtopte(tmpva); 3036 pte_clear(pte); 3037 } 3038 pmap_invalidate_range(kernel_pmap, va, tmpva); 3039 kmem_free(kernel_map, base, size); 3040} 3041 3042/* 3043 * perform the pmap work for mincore 3044 */ 3045int 3046pmap_mincore(pmap, addr) 3047 pmap_t pmap; 3048 vm_offset_t addr; 3049{ 3050 pt_entry_t *ptep, pte; 3051 vm_page_t m; 3052 int val = 0; 3053 3054 ptep = pmap_pte_quick(pmap, addr); 3055 if (ptep == 0) { 3056 return 0; 3057 } 3058 3059 if ((pte = *ptep) != 0) { 3060 vm_paddr_t pa; 3061 3062 val = MINCORE_INCORE; 3063 if ((pte & PG_MANAGED) == 0) 3064 return val; 3065 3066 pa = pte & PG_FRAME; 3067 3068 m = PHYS_TO_VM_PAGE(pa); 3069 3070 /* 3071 * Modified by us 3072 */ 3073 if (pte & PG_M) 3074 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER; 3075 else { 3076 /* 3077 * Modified by someone else 3078 */ 3079 vm_page_lock_queues(); 3080 if (m->dirty || pmap_is_modified(m)) 3081 val |= MINCORE_MODIFIED_OTHER; 3082 vm_page_unlock_queues(); 3083 } 3084 /* 3085 * Referenced by us 3086 */ 3087 if (pte & PG_A) 3088 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER; 3089 else { 3090 /* 3091 * Referenced by someone else 3092 */ 3093 vm_page_lock_queues(); 3094 if ((m->flags & PG_REFERENCED) || 3095 pmap_ts_referenced(m)) { 3096 val |= MINCORE_REFERENCED_OTHER; 3097 vm_page_flag_set(m, PG_REFERENCED); 3098 } 3099 vm_page_unlock_queues(); 3100 } 3101 } 3102 return val; 3103} 3104 3105void 3106pmap_activate(struct thread *td) 3107{ 3108 struct proc *p = td->td_proc; 3109 pmap_t pmap, oldpmap; 3110 u_int32_t cr3; 3111 3112 critical_enter(); 3113 pmap = vmspace_pmap(td->td_proc->p_vmspace); 3114 oldpmap = PCPU_GET(curpmap); 3115#if defined(SMP) 3116 atomic_clear_int(&oldpmap->pm_active, PCPU_GET(cpumask)); 3117 atomic_set_int(&pmap->pm_active, PCPU_GET(cpumask)); 3118#else 3119 oldpmap->pm_active &= ~1; 3120 pmap->pm_active |= 1; 3121#endif 3122#ifdef PAE 3123 cr3 = vtophys(pmap->pm_pdpt); 3124#else 3125 cr3 = vtophys(pmap->pm_pdir); 3126#endif 3127 /* XXXKSE this is wrong. 3128 * pmap_activate is for the current thread on the current cpu 3129 */ 3130 if (p->p_flag & P_SA) { 3131 /* Make sure all other cr3 entries are updated. */ 3132 /* what if they are running? XXXKSE (maybe abort them) */ 3133 FOREACH_THREAD_IN_PROC(p, td) { 3134 td->td_pcb->pcb_cr3 = cr3; 3135 } 3136 } else { 3137 td->td_pcb->pcb_cr3 = cr3; 3138 } 3139 load_cr3(cr3); 3140 PCPU_SET(curpmap, pmap); 3141 critical_exit(); 3142} 3143 3144vm_offset_t 3145pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size) 3146{ 3147 3148 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) { 3149 return addr; 3150 } 3151 3152 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1); 3153 return addr; 3154} 3155 3156 3157#if defined(PMAP_DEBUG) 3158pmap_pid_dump(int pid) 3159{ 3160 pmap_t pmap; 3161 struct proc *p; 3162 int npte = 0; 3163 int index; 3164 3165 sx_slock(&allproc_lock); 3166 LIST_FOREACH(p, &allproc, p_list) { 3167 if (p->p_pid != pid) 3168 continue; 3169 3170 if (p->p_vmspace) { 3171 int i,j; 3172 index = 0; 3173 pmap = vmspace_pmap(p->p_vmspace); 3174 for (i = 0; i < NPDEPTD; i++) { 3175 pd_entry_t *pde; 3176 pt_entry_t *pte; 3177 vm_offset_t base = i << PDRSHIFT; 3178 3179 pde = &pmap->pm_pdir[i]; 3180 if (pde && pmap_pde_v(pde)) { 3181 for (j = 0; j < NPTEPG; j++) { 3182 vm_offset_t va = base + (j << PAGE_SHIFT); 3183 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) { 3184 if (index) { 3185 index = 0; 3186 printf("\n"); 3187 } 3188 sx_sunlock(&allproc_lock); 3189 return npte; 3190 } 3191 pte = pmap_pte_quick(pmap, va); 3192 if (pte && pmap_pte_v(pte)) { 3193 pt_entry_t pa; 3194 vm_page_t m; 3195 pa = *pte; 3196 m = PHYS_TO_VM_PAGE(pa); 3197 printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x", 3198 va, pa, m->hold_count, m->wire_count, m->flags); 3199 npte++; 3200 index++; 3201 if (index >= 2) { 3202 index = 0; 3203 printf("\n"); 3204 } else { 3205 printf(" "); 3206 } 3207 } 3208 } 3209 } 3210 } 3211 } 3212 } 3213 sx_sunlock(&allproc_lock); 3214 return npte; 3215} 3216#endif 3217 3218#if defined(DEBUG) 3219 3220static void pads(pmap_t pm); 3221void pmap_pvdump(vm_offset_t pa); 3222 3223/* print address space of pmap*/ 3224static void 3225pads(pm) 3226 pmap_t pm; 3227{ 3228 int i, j; 3229 vm_paddr_t va; 3230 pt_entry_t *ptep; 3231 3232 if (pm == kernel_pmap) 3233 return; 3234 for (i = 0; i < NPDEPTD; i++) 3235 if (pm->pm_pdir[i]) 3236 for (j = 0; j < NPTEPG; j++) { 3237 va = (i << PDRSHIFT) + (j << PAGE_SHIFT); 3238 if (pm == kernel_pmap && va < KERNBASE) 3239 continue; 3240 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS) 3241 continue; 3242 ptep = pmap_pte_quick(pm, va); 3243 if (pmap_pte_v(ptep)) 3244 printf("%x:%x ", va, *ptep); 3245 }; 3246 3247} 3248 3249void 3250pmap_pvdump(pa) 3251 vm_paddr_t pa; 3252{ 3253 pv_entry_t pv; 3254 vm_page_t m; 3255 3256 printf("pa %x", pa); 3257 m = PHYS_TO_VM_PAGE(pa); 3258 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) { 3259 printf(" -> pmap %p, va %x", (void *)pv->pv_pmap, pv->pv_va); 3260 pads(pv->pv_pmap); 3261 } 3262 printf(" "); 3263} 3264#endif 3265