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