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