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