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