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