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