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