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