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