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