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