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