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