pmap.c revision 16122
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.98 1996/06/02 22:28:53 dyson Exp $ 43 */ 44 45/* 46 * Derived from hp300 version by Mike Hibler, this version by William 47 * Jolitz uses a recursive map [a pde points to the page directory] to 48 * map the page tables using the pagetables themselves. This is done to 49 * reduce the impact on kernel virtual memory for lots of sparse address 50 * space, and to reduce the cost of memory to each process. 51 * 52 * Derived from: hp300/@(#)pmap.c 7.1 (Berkeley) 12/5/90 53 */ 54 55/* 56 * Manages physical address maps. 57 * 58 * In addition to hardware address maps, this 59 * module is called upon to provide software-use-only 60 * maps which may or may not be stored in the same 61 * form as hardware maps. These pseudo-maps are 62 * used to store intermediate results from copy 63 * operations to and from address spaces. 64 * 65 * Since the information managed by this module is 66 * also stored by the logical address mapping module, 67 * this module may throw away valid virtual-to-physical 68 * mappings at almost any time. However, invalidations 69 * of virtual-to-physical mappings must be done as 70 * requested. 71 * 72 * In order to cope with hardware architectures which 73 * make virtual-to-physical map invalidates expensive, 74 * this module may delay invalidate or reduced protection 75 * operations until such time as they are actually 76 * necessary. This module is given full information as 77 * to which processors are currently using which maps, 78 * and to when physical maps must be made correct. 79 */ 80 81#include <sys/param.h> 82#include <sys/systm.h> 83#include <sys/proc.h> 84#include <sys/malloc.h> 85#include <sys/msgbuf.h> 86#include <sys/queue.h> 87#include <sys/vmmeter.h> 88#include <sys/mman.h> 89 90#include <vm/vm.h> 91#include <vm/vm_param.h> 92#include <vm/vm_prot.h> 93#include <vm/lock.h> 94#include <vm/vm_kern.h> 95#include <vm/vm_page.h> 96#include <vm/vm_map.h> 97#include <vm/vm_object.h> 98#include <vm/vm_extern.h> 99#include <vm/vm_pageout.h> 100 101#include <machine/pcb.h> 102#include <machine/cputypes.h> 103#include <machine/md_var.h> 104 105#include <i386/isa/isa.h> 106 107#define PMAP_KEEP_PDIRS 108 109#if defined(DIAGNOSTIC) 110#define PMAP_DIAGNOSTIC 111#endif 112 113static void init_pv_entries __P((int)); 114 115/* 116 * Get PDEs and PTEs for user/kernel address space 117 */ 118#define pmap_pde(m, v) (&((m)->pm_pdir[(vm_offset_t)(v) >> PDRSHIFT])) 119#define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT]) 120 121#define pmap_pde_v(pte) ((*(int *)pte & PG_V) != 0) 122#define pmap_pte_w(pte) ((*(int *)pte & PG_W) != 0) 123#define pmap_pte_m(pte) ((*(int *)pte & PG_M) != 0) 124#define pmap_pte_u(pte) ((*(int *)pte & PG_A) != 0) 125#define pmap_pte_v(pte) ((*(int *)pte & PG_V) != 0) 126 127#define pmap_pte_set_w(pte, v) ((v)?(*(int *)pte |= PG_W):(*(int *)pte &= ~PG_W)) 128#define pmap_pte_set_prot(pte, v) ((*(int *)pte &= ~PG_PROT), (*(int *)pte |= (v))) 129 130/* 131 * Given a map and a machine independent protection code, 132 * convert to a vax protection code. 133 */ 134#define pte_prot(m, p) (protection_codes[p]) 135static int protection_codes[8]; 136 137static struct pmap kernel_pmap_store; 138pmap_t kernel_pmap; 139 140vm_offset_t avail_start; /* PA of first available physical page */ 141vm_offset_t avail_end; /* PA of last available physical page */ 142vm_offset_t virtual_avail; /* VA of first avail page (after kernel bss) */ 143vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */ 144static boolean_t pmap_initialized = FALSE; /* Has pmap_init completed? */ 145static vm_offset_t vm_first_phys; 146 147static int nkpt; 148static vm_page_t nkpg; 149vm_offset_t kernel_vm_end; 150 151extern vm_offset_t clean_sva, clean_eva; 152extern int cpu_class; 153 154#define PV_FREELIST_MIN ((PAGE_SIZE / sizeof (struct pv_entry)) / 2) 155 156/* 157 * Data for the pv entry allocation mechanism 158 */ 159static int pv_freelistcnt; 160static pv_entry_t pv_freelist; 161static vm_offset_t pvva; 162static int npvvapg; 163 164/* 165 * All those kernel PT submaps that BSD is so fond of 166 */ 167pt_entry_t *CMAP1; 168static pt_entry_t *CMAP2, *ptmmap; 169static pv_entry_t *pv_table; 170caddr_t CADDR1, ptvmmap; 171static caddr_t CADDR2; 172static pt_entry_t *msgbufmap; 173struct msgbuf *msgbufp; 174 175static void free_pv_entry __P((pv_entry_t pv)); 176static __inline unsigned * get_ptbase __P((pmap_t pmap)); 177static pv_entry_t get_pv_entry __P((void)); 178static void i386_protection_init __P((void)); 179static void pmap_alloc_pv_entry __P((void)); 180static void pmap_changebit __P((vm_offset_t pa, int bit, boolean_t setem)); 181static void pmap_enter_quick __P((pmap_t pmap, vm_offset_t va, 182 vm_offset_t pa)); 183static int pmap_is_managed __P((vm_offset_t pa)); 184static void pmap_remove_all __P((vm_offset_t pa)); 185static void pmap_remove_page __P((struct pmap *pmap, vm_offset_t va)); 186static __inline int pmap_remove_entry __P((struct pmap *pmap, pv_entry_t *pv, 187 vm_offset_t va)); 188static int pmap_remove_pte __P((struct pmap *pmap, unsigned *ptq, 189 vm_offset_t sva)); 190static boolean_t 191 pmap_testbit __P((vm_offset_t pa, int bit)); 192static __inline void pmap_insert_entry __P((pmap_t pmap, vm_offset_t va, 193 vm_page_t mpte, vm_offset_t pa)); 194 195static __inline vm_page_t pmap_allocpte __P((pmap_t pmap, vm_offset_t va)); 196static void pmap_remove_pte_mapping __P((vm_offset_t pa)); 197static __inline int pmap_release_free_page __P((pmap_t pmap, vm_page_t p)); 198static vm_page_t _pmap_allocpte __P((pmap_t pmap, vm_offset_t va, int ptepindex)); 199 200#define PDSTACKMAX 16 201static vm_offset_t pdstack[PDSTACKMAX]; 202static int pdstackptr; 203 204#if defined(PMAP_DIAGNOSTIC) 205 206/* 207 * This code checks for non-writeable/modified pages. 208 * This should be an invalid condition. 209 */ 210static int 211pmap_nw_modified(pt_entry_t ptea) { 212 int pte; 213 214 pte = (int) ptea; 215 216 if ((pte & (PG_M|PG_RW)) == PG_M) 217 return 1; 218 else 219 return 0; 220} 221#endif 222 223/* 224 * The below are finer grained pmap_update routines. These eliminate 225 * the gratuitious tlb flushes on non-i386 architectures. 226 */ 227static __inline void 228pmap_update_1pg( vm_offset_t va) { 229#if defined(I386_CPU) 230 if (cpu_class == CPUCLASS_386) 231 pmap_update(); 232 else 233#endif 234 __asm __volatile(".byte 0xf,0x1,0x38": :"a" (va)); 235} 236 237static __inline void 238pmap_update_2pg( vm_offset_t va1, vm_offset_t va2) { 239#if defined(I386_CPU) 240 if (cpu_class == CPUCLASS_386) { 241 pmap_update(); 242 } else 243#endif 244 { 245 __asm __volatile(".byte 0xf,0x1,0x38": :"a" (va1)); 246 __asm __volatile(".byte 0xf,0x1,0x38": :"a" (va2)); 247 } 248} 249 250static __inline __pure unsigned * 251get_ptbase(pmap) 252 pmap_t pmap; 253{ 254 unsigned frame = (unsigned) pmap->pm_pdir[PTDPTDI] & PG_FRAME; 255 256 /* are we current address space or kernel? */ 257 if (pmap == kernel_pmap || frame == (((unsigned) PTDpde) & PG_FRAME)) { 258 return (unsigned *) PTmap; 259 } 260 /* otherwise, we are alternate address space */ 261 if (frame != (((unsigned) APTDpde) & PG_FRAME)) { 262 APTDpde = (pd_entry_t) (frame | PG_RW | PG_V); 263 pmap_update(); 264 } 265 return (unsigned *) APTmap; 266} 267 268/* 269 * Routine: pmap_pte 270 * Function: 271 * Extract the page table entry associated 272 * with the given map/virtual_address pair. 273 */ 274 275__inline unsigned * __pure 276pmap_pte(pmap, va) 277 register pmap_t pmap; 278 vm_offset_t va; 279{ 280 if (pmap && *pmap_pde(pmap, va)) { 281 return get_ptbase(pmap) + i386_btop(va); 282 } 283 return (0); 284} 285 286/* 287 * Routine: pmap_extract 288 * Function: 289 * Extract the physical page address associated 290 * with the given map/virtual_address pair. 291 */ 292vm_offset_t __pure 293pmap_extract(pmap, va) 294 register pmap_t pmap; 295 vm_offset_t va; 296{ 297 if (pmap && *pmap_pde(pmap, va)) { 298 unsigned *pte; 299 pte = get_ptbase(pmap) + i386_btop(va); 300 return ((*pte & PG_FRAME) | (va & PAGE_MASK)); 301 } 302 return 0; 303 304} 305 306/* 307 * Add a list of wired pages to the kva 308 * this routine is only used for temporary 309 * kernel mappings that do not need to have 310 * page modification or references recorded. 311 * Note that old mappings are simply written 312 * over. The page *must* be wired. 313 */ 314void 315pmap_qenter(va, m, count) 316 vm_offset_t va; 317 vm_page_t *m; 318 int count; 319{ 320 int i; 321 register unsigned *pte; 322 323 for (i = 0; i < count; i++) { 324 vm_offset_t tva = va + i * PAGE_SIZE; 325 unsigned npte = VM_PAGE_TO_PHYS(m[i]) | PG_RW | PG_V; 326 unsigned opte; 327 pte = (unsigned *)vtopte(tva); 328 opte = *pte; 329 *pte = npte; 330 if (opte) 331 pmap_update_1pg(tva); 332 } 333} 334/* 335 * this routine jerks page mappings from the 336 * kernel -- it is meant only for temporary mappings. 337 */ 338void 339pmap_qremove(va, count) 340 vm_offset_t va; 341 int count; 342{ 343 int i; 344 register unsigned *pte; 345 346 for (i = 0; i < count; i++) { 347 pte = (unsigned *)vtopte(va); 348 *pte = 0; 349 pmap_update_1pg(va); 350 va += PAGE_SIZE; 351 } 352} 353 354/* 355 * add a wired page to the kva 356 * note that in order for the mapping to take effect -- you 357 * should do a pmap_update after doing the pmap_kenter... 358 */ 359__inline void 360pmap_kenter(va, pa) 361 vm_offset_t va; 362 register vm_offset_t pa; 363{ 364 register unsigned *pte; 365 unsigned npte, opte; 366 367 npte = pa | PG_RW | PG_V; 368 pte = (unsigned *)vtopte(va); 369 opte = *pte; 370 *pte = npte; 371 if (opte) 372 pmap_update_1pg(va); 373} 374 375/* 376 * remove a page from the kernel pagetables 377 */ 378__inline void 379pmap_kremove(va) 380 vm_offset_t va; 381{ 382 register unsigned *pte; 383 384 pte = (unsigned *)vtopte(va); 385 *pte = 0; 386 pmap_update_1pg(va); 387} 388 389/* 390 * determine if a page is managed (memory vs. device) 391 */ 392static __inline __pure int 393pmap_is_managed(pa) 394 vm_offset_t pa; 395{ 396 int i; 397 398 if (!pmap_initialized) 399 return 0; 400 401 for (i = 0; phys_avail[i + 1]; i += 2) { 402 if (pa < phys_avail[i + 1] && pa >= phys_avail[i]) 403 return 1; 404 } 405 return 0; 406} 407 408/* 409 * This routine unholds page table pages, and if the hold count 410 * drops to zero, then it decrements the wire count. 411 */ 412static __inline int 413pmap_unwire_pte_hold(vm_page_t m) { 414 vm_page_unhold(m); 415 if (m->hold_count == 0) { 416 --m->wire_count; 417 if (m->wire_count == 0) { 418 --cnt.v_wire_count; 419 m->dirty = 0; 420 vm_page_deactivate(m); 421 } 422 return 1; 423 } 424 return 0; 425} 426 427#if !defined(PMAP_DIAGNOSTIC) 428__inline 429#endif 430int 431pmap_unuse_pt(pmap, va, mpte) 432 pmap_t pmap; 433 vm_offset_t va; 434 vm_page_t mpte; 435{ 436 if (va >= UPT_MIN_ADDRESS) 437 return 0; 438 439 if (mpte == NULL) { 440 vm_offset_t ptepa; 441 ptepa = ((vm_offset_t) *pmap_pde(pmap, va)) /* & PG_FRAME */; 442#if defined(PMAP_DIAGNOSTIC) 443 if (!ptepa) 444 panic("pmap_unuse_pt: pagetable page missing, va: 0x%x", va); 445#endif 446 mpte = PHYS_TO_VM_PAGE(ptepa); 447 } 448 449#if defined(PMAP_DIAGNOSTIC) 450 if (mpte->hold_count == 0) { 451 panic("pmap_unuse_pt: hold count < 0, va: 0x%x", va); 452 } 453#endif 454 455/* 456 * We don't free page-table-pages anymore because it can have a negative 457 * impact on perf at times. Now we just deactivate, and it'll get cleaned 458 * up if needed... Also, if the page ends up getting used, it will be 459 * brought back into the process address space by pmap_allocpte and be 460 * reactivated. 461 */ 462 return pmap_unwire_pte_hold(mpte); 463} 464 465/* 466 * Bootstrap the system enough to run with virtual memory. 467 * 468 * On the i386 this is called after mapping has already been enabled 469 * and just syncs the pmap module with what has already been done. 470 * [We can't call it easily with mapping off since the kernel is not 471 * mapped with PA == VA, hence we would have to relocate every address 472 * from the linked base (virtual) address "KERNBASE" to the actual 473 * (physical) address starting relative to 0] 474 */ 475void 476pmap_bootstrap(firstaddr, loadaddr) 477 vm_offset_t firstaddr; 478 vm_offset_t loadaddr; 479{ 480 vm_offset_t va; 481 pt_entry_t *pte; 482 483 avail_start = firstaddr; 484 485 /* 486 * XXX The calculation of virtual_avail is wrong. It's NKPT*PAGE_SIZE too 487 * large. It should instead be correctly calculated in locore.s and 488 * not based on 'first' (which is a physical address, not a virtual 489 * address, for the start of unused physical memory). The kernel 490 * page tables are NOT double mapped and thus should not be included 491 * in this calculation. 492 */ 493 virtual_avail = (vm_offset_t) KERNBASE + firstaddr; 494 virtual_end = VM_MAX_KERNEL_ADDRESS; 495 496 /* 497 * Initialize protection array. 498 */ 499 i386_protection_init(); 500 501 /* 502 * The kernel's pmap is statically allocated so we don't have to use 503 * pmap_create, which is unlikely to work correctly at this part of 504 * the boot sequence (XXX and which no longer exists). 505 */ 506 kernel_pmap = &kernel_pmap_store; 507 508 kernel_pmap->pm_pdir = (pd_entry_t *) (KERNBASE + IdlePTD); 509 510 kernel_pmap->pm_count = 1; 511 nkpt = NKPT; 512 513 /* 514 * Reserve some special page table entries/VA space for temporary 515 * mapping of pages. 516 */ 517#define SYSMAP(c, p, v, n) \ 518 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n); 519 520 va = virtual_avail; 521 pte = (pt_entry_t *) pmap_pte(kernel_pmap, va); 522 523 /* 524 * CMAP1/CMAP2 are used for zeroing and copying pages. 525 */ 526 SYSMAP(caddr_t, CMAP1, CADDR1, 1) 527 SYSMAP(caddr_t, CMAP2, CADDR2, 1) 528 529 /* 530 * ptmmap is used for reading arbitrary physical pages via /dev/mem. 531 */ 532 SYSMAP(caddr_t, ptmmap, ptvmmap, 1) 533 534 /* 535 * msgbufmap is used to map the system message buffer. 536 */ 537 SYSMAP(struct msgbuf *, msgbufmap, msgbufp, 1) 538 539 virtual_avail = va; 540 541 *(int *) CMAP1 = *(int *) CMAP2 = *(int *) PTD = 0; 542 pmap_update(); 543 544} 545 546/* 547 * Initialize the pmap module. 548 * Called by vm_init, to initialize any structures that the pmap 549 * system needs to map virtual memory. 550 * pmap_init has been enhanced to support in a fairly consistant 551 * way, discontiguous physical memory. 552 */ 553void 554pmap_init(phys_start, phys_end) 555 vm_offset_t phys_start, phys_end; 556{ 557 vm_offset_t addr; 558 vm_size_t npg, s; 559 int i; 560 561 /* 562 * calculate the number of pv_entries needed 563 */ 564 vm_first_phys = phys_avail[0]; 565 for (i = 0; phys_avail[i + 1]; i += 2); 566 npg = (phys_avail[(i - 2) + 1] - vm_first_phys) / PAGE_SIZE; 567 568 /* 569 * Allocate memory for random pmap data structures. Includes the 570 * pv_head_table. 571 */ 572 s = (vm_size_t) (sizeof(struct pv_entry *) * npg); 573 s = round_page(s); 574 addr = (vm_offset_t) kmem_alloc(kernel_map, s); 575 pv_table = (pv_entry_t *) addr; 576 577 /* 578 * init the pv free list 579 */ 580 init_pv_entries(npg); 581 /* 582 * Now it is safe to enable pv_table recording. 583 */ 584 pmap_initialized = TRUE; 585} 586 587/* 588 * Used to map a range of physical addresses into kernel 589 * virtual address space. 590 * 591 * For now, VM is already on, we only need to map the 592 * specified memory. 593 */ 594vm_offset_t 595pmap_map(virt, start, end, prot) 596 vm_offset_t virt; 597 vm_offset_t start; 598 vm_offset_t end; 599 int prot; 600{ 601 while (start < end) { 602 pmap_enter(kernel_pmap, virt, start, prot, FALSE); 603 virt += PAGE_SIZE; 604 start += PAGE_SIZE; 605 } 606 return (virt); 607} 608 609/* 610 * Initialize a preallocated and zeroed pmap structure, 611 * such as one in a vmspace structure. 612 */ 613void 614pmap_pinit(pmap) 615 register struct pmap *pmap; 616{ 617 vm_page_t ptdpg; 618 /* 619 * No need to allocate page table space yet but we do need a valid 620 * page directory table. 621 */ 622 623 if (pdstackptr > 0) { 624 --pdstackptr; 625 pmap->pm_pdir = 626 (pd_entry_t *)pdstack[pdstackptr]; 627 } else { 628 pmap->pm_pdir = 629 (pd_entry_t *)kmem_alloc_pageable(kernel_map, PAGE_SIZE); 630 } 631 632 /* 633 * allocate object for the ptes 634 */ 635 pmap->pm_pteobj = vm_object_allocate( OBJT_DEFAULT, PTDPTDI + 1); 636 637 /* 638 * allocate the page directory page 639 */ 640retry: 641 ptdpg = vm_page_alloc( pmap->pm_pteobj, PTDPTDI, VM_ALLOC_ZERO); 642 if (ptdpg == NULL) { 643 VM_WAIT; 644 goto retry; 645 } 646 vm_page_wire(ptdpg); 647 ptdpg->flags &= ~(PG_MAPPED|PG_BUSY); /* not mapped normally */ 648 ptdpg->valid = VM_PAGE_BITS_ALL; 649 650 pmap_kenter((vm_offset_t) pmap->pm_pdir, VM_PAGE_TO_PHYS(ptdpg)); 651 if ((ptdpg->flags & PG_ZERO) == 0) 652 bzero(pmap->pm_pdir, PAGE_SIZE); 653 654 /* wire in kernel global address entries */ 655 bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * PTESIZE); 656 657 /* install self-referential address mapping entry */ 658 *(unsigned *) (pmap->pm_pdir + PTDPTDI) = 659 VM_PAGE_TO_PHYS(ptdpg) | PG_V | PG_RW; 660 661 pmap->pm_count = 1; 662} 663 664static int 665pmap_release_free_page(pmap, p) 666 struct pmap *pmap; 667 vm_page_t p; 668{ 669 int s; 670 /* 671 * This code optimizes the case of freeing non-busy 672 * page-table pages. Those pages are zero now, and 673 * might as well be placed directly into the zero queue. 674 */ 675 s = splvm(); 676 if (p->flags & PG_BUSY) { 677 p->flags |= PG_WANTED; 678 tsleep(p, PVM, "pmaprl", 0); 679 splx(s); 680 return 0; 681 } 682 683 pmap_remove_pte_mapping(VM_PAGE_TO_PHYS(p)); 684 685 if (p->hold_count) { 686#if defined(PMAP_DIAGNOSTIC) 687 panic("pmap_release: freeing held page table page"); 688#endif 689 /* 690 * HACK ALERT!!! 691 * If this failure happens, we must clear the page, because 692 * there is likely a mapping still valid. This condition 693 * is an error, but at least this zero operation will mitigate 694 * some Sig-11's or crashes, because this page is thought 695 * to be zero. This is a robustness fix, and not meant to 696 * be a long term work-around. 697 */ 698 pmap_zero_page(VM_PAGE_TO_PHYS(p)); 699 } 700 /* 701 * Page directory pages need to have the kernel 702 * stuff cleared, so they can go into the zero queue also. 703 */ 704 if (p->pindex == PTDPTDI) { 705 unsigned *pde = (unsigned *) pmap->pm_pdir; 706 bzero(pde + KPTDI, nkpt * PTESIZE); 707 pde[APTDPTDI] = 0; 708 pde[PTDPTDI] = 0; 709 pmap_kremove((vm_offset_t) pmap->pm_pdir); 710 } 711 712 vm_page_free_zero(p); 713 splx(s); 714 return 1; 715} 716 717/* 718 * Release any resources held by the given physical map. 719 * Called when a pmap initialized by pmap_pinit is being released. 720 * Should only be called if the map contains no valid mappings. 721 */ 722void 723pmap_release(pmap) 724 register struct pmap *pmap; 725{ 726 vm_page_t p,n,ptdpg; 727 vm_object_t object = pmap->pm_pteobj; 728 int s; 729 730 if (object->ref_count != 1) 731 panic("pmap_release: pteobj reference count != 1"); 732 733 /* 734 * Wait until any (bogus) paging activity on this object is 735 * complete. 736 */ 737 s = splvm(); 738 while (object->paging_in_progress) { 739 object->flags |= OBJ_PIPWNT; 740 tsleep(object,PVM,"pmrlob",0); 741 } 742 splx(s); 743 744 ptdpg = NULL; 745retry: 746 for (p = TAILQ_FIRST(&object->memq); p != NULL; p = n) { 747 n = TAILQ_NEXT(p, listq); 748 if (p->pindex == PTDPTDI) { 749 ptdpg = p; 750 continue; 751 } 752 if ((p->flags & PG_BUSY) || p->busy) 753 continue; 754 if (!pmap_release_free_page(pmap, p)) 755 goto retry; 756 } 757 if (ptdpg == NULL) 758 panic("pmap_release: missing page table directory page"); 759 760 pmap_release_free_page(pmap, ptdpg); 761 762 vm_object_deallocate(object); 763 if (pdstackptr < PDSTACKMAX) { 764 pdstack[pdstackptr] = (vm_offset_t) pmap->pm_pdir; 765 ++pdstackptr; 766 } else { 767 kmem_free(kernel_map, (vm_offset_t) pmap->pm_pdir, PAGE_SIZE); 768 } 769} 770 771/* 772 * grow the number of kernel page table entries, if needed 773 */ 774 775void 776pmap_growkernel(vm_offset_t addr) 777{ 778 struct proc *p; 779 struct pmap *pmap; 780 int s; 781 782 s = splhigh(); 783 if (kernel_vm_end == 0) { 784 kernel_vm_end = KERNBASE; 785 nkpt = 0; 786 while (pdir_pde(PTD, kernel_vm_end)) { 787 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1); 788 ++nkpt; 789 } 790 } 791 addr = (addr + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1); 792 while (kernel_vm_end < addr) { 793 if (pdir_pde(PTD, kernel_vm_end)) { 794 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1); 795 continue; 796 } 797 ++nkpt; 798 if (!nkpg) { 799 nkpg = vm_page_alloc(kernel_object, 0, VM_ALLOC_SYSTEM); 800 if (!nkpg) 801 panic("pmap_growkernel: no memory to grow kernel"); 802 vm_page_wire(nkpg); 803 vm_page_remove(nkpg); 804 pmap_zero_page(VM_PAGE_TO_PHYS(nkpg)); 805 } 806 pdir_pde(PTD, kernel_vm_end) = (pd_entry_t) (VM_PAGE_TO_PHYS(nkpg) | PG_V | PG_RW); 807 nkpg = NULL; 808 809 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) { 810 if (p->p_vmspace) { 811 pmap = &p->p_vmspace->vm_pmap; 812 *pmap_pde(pmap, kernel_vm_end) = pdir_pde(PTD, kernel_vm_end); 813 } 814 } 815 *pmap_pde(kernel_pmap, kernel_vm_end) = pdir_pde(PTD, kernel_vm_end); 816 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1); 817 } 818 splx(s); 819} 820 821/* 822 * Retire the given physical map from service. 823 * Should only be called if the map contains 824 * no valid mappings. 825 */ 826void 827pmap_destroy(pmap) 828 register pmap_t pmap; 829{ 830 int count; 831 832 if (pmap == NULL) 833 return; 834 835 count = --pmap->pm_count; 836 if (count == 0) { 837 pmap_release(pmap); 838 free((caddr_t) pmap, M_VMPMAP); 839 } 840} 841 842/* 843 * Add a reference to the specified pmap. 844 */ 845void 846pmap_reference(pmap) 847 pmap_t pmap; 848{ 849 if (pmap != NULL) { 850 pmap->pm_count++; 851 } 852} 853 854/* 855 * free the pv_entry back to the free list 856 */ 857static __inline void 858free_pv_entry(pv) 859 pv_entry_t pv; 860{ 861 ++pv_freelistcnt; 862 pv->pv_next = pv_freelist; 863 pv_freelist = pv; 864} 865 866/* 867 * get a new pv_entry, allocating a block from the system 868 * when needed. 869 * the memory allocation is performed bypassing the malloc code 870 * because of the possibility of allocations at interrupt time. 871 */ 872static __inline pv_entry_t 873get_pv_entry() 874{ 875 pv_entry_t tmp; 876 877 /* 878 * get more pv_entry pages if needed 879 */ 880 if (pv_freelistcnt < PV_FREELIST_MIN || pv_freelist == 0) { 881 pmap_alloc_pv_entry(); 882 } 883 /* 884 * get a pv_entry off of the free list 885 */ 886 --pv_freelistcnt; 887 tmp = pv_freelist; 888 pv_freelist = tmp->pv_next; 889 return tmp; 890} 891 892/* 893 * This *strange* allocation routine eliminates the possibility of a malloc 894 * failure (*FATAL*) for a pv_entry_t data structure. 895 * also -- this code is MUCH MUCH faster than the malloc equiv... 896 * We really need to do the slab allocator thingie here. 897 */ 898static void 899pmap_alloc_pv_entry() 900{ 901 /* 902 * do we have any pre-allocated map-pages left? 903 */ 904 if (npvvapg) { 905 vm_page_t m; 906 907 /* 908 * allocate a physical page out of the vm system 909 */ 910 m = vm_page_alloc(kernel_object, 911 OFF_TO_IDX(pvva - vm_map_min(kernel_map)), 912 VM_ALLOC_INTERRUPT); 913 if (m) { 914 int newentries; 915 int i; 916 pv_entry_t entry; 917 918 newentries = (PAGE_SIZE / sizeof(struct pv_entry)); 919 /* 920 * wire the page 921 */ 922 vm_page_wire(m); 923 m->flags &= ~PG_BUSY; 924 /* 925 * let the kernel see it 926 */ 927 pmap_kenter(pvva, VM_PAGE_TO_PHYS(m)); 928 929 entry = (pv_entry_t) pvva; 930 /* 931 * update the allocation pointers 932 */ 933 pvva += PAGE_SIZE; 934 --npvvapg; 935 936 /* 937 * free the entries into the free list 938 */ 939 for (i = 0; i < newentries; i++) { 940 free_pv_entry(entry); 941 entry++; 942 } 943 } 944 } 945 if (!pv_freelist) 946 panic("get_pv_entry: cannot get a pv_entry_t"); 947} 948 949/* 950 * init the pv_entry allocation system 951 */ 952#define PVSPERPAGE 64 953void 954init_pv_entries(npg) 955 int npg; 956{ 957 /* 958 * allocate enough kvm space for PVSPERPAGE entries per page (lots) 959 * kvm space is fairly cheap, be generous!!! (the system can panic if 960 * this is too small.) 961 */ 962 npvvapg = ((npg * PVSPERPAGE) * sizeof(struct pv_entry) 963 + PAGE_SIZE - 1) / PAGE_SIZE; 964 pvva = kmem_alloc_pageable(kernel_map, npvvapg * PAGE_SIZE); 965 /* 966 * get the first batch of entries 967 */ 968 pmap_alloc_pv_entry(); 969} 970 971/* 972 * If it is the first entry on the list, it is actually 973 * in the header and we must copy the following entry up 974 * to the header. Otherwise we must search the list for 975 * the entry. In either case we free the now unused entry. 976 */ 977static __inline int 978pmap_remove_entry(pmap, ppv, va) 979 struct pmap *pmap; 980 pv_entry_t *ppv; 981 vm_offset_t va; 982{ 983 pv_entry_t npv; 984 int s; 985 986 s = splvm(); 987 for (npv = *ppv; npv; (ppv = &npv->pv_next, npv = *ppv)) { 988 if (pmap == npv->pv_pmap && va == npv->pv_va) { 989 int rtval = pmap_unuse_pt(pmap, va, npv->pv_ptem); 990 *ppv = npv->pv_next; 991 free_pv_entry(npv); 992 splx(s); 993 return rtval; 994 } 995 } 996 splx(s); 997 return 0; 998} 999 1000/* 1001 * pmap_remove_pte: do the things to unmap a page in a process 1002 */ 1003static 1004#if !defined(PMAP_DIAGNOSTIC) 1005__inline 1006#endif 1007int 1008pmap_remove_pte(pmap, ptq, va) 1009 struct pmap *pmap; 1010 unsigned *ptq; 1011 vm_offset_t va; 1012{ 1013 unsigned oldpte; 1014 pv_entry_t *ppv; 1015 int rtval; 1016 1017 oldpte = *ptq; 1018 *ptq = 0; 1019 if (oldpte & PG_W) 1020 pmap->pm_stats.wired_count -= 1; 1021 pmap->pm_stats.resident_count -= 1; 1022 if (oldpte & PG_MANAGED) { 1023 if (oldpte & PG_M) { 1024#if defined(PMAP_DIAGNOSTIC) 1025 if (pmap_nw_modified((pt_entry_t) oldpte)) { 1026 printf("pmap_remove: modified page not writable: va: 0x%lx, pte: 0x%lx\n", va, (int) oldpte); 1027 } 1028#endif 1029 if (va < clean_sva || va >= clean_eva) { 1030 PHYS_TO_VM_PAGE(oldpte)->dirty = VM_PAGE_BITS_ALL; 1031 } 1032 } 1033 ppv = pa_to_pvh(oldpte); 1034 rtval = pmap_remove_entry(pmap, ppv, va); 1035#if defined(notyet) 1036 if (*ppv == NULL) { 1037 PHYS_TO_VM_PAGE(oldpte)->flags &= ~PG_MAPPED; 1038 } 1039#endif 1040 return rtval; 1041 } else { 1042 return pmap_unuse_pt(pmap, va, NULL); 1043 } 1044 1045 return 0; 1046} 1047 1048/* 1049 * Remove a single page from a process address space 1050 */ 1051static __inline void 1052pmap_remove_page(pmap, va) 1053 struct pmap *pmap; 1054 register vm_offset_t va; 1055{ 1056 register unsigned *ptq; 1057 1058 /* 1059 * if there is no pte for this address, just skip it!!! 1060 */ 1061 if (*pmap_pde(pmap, va) == 0) { 1062 return; 1063 } 1064 1065 /* 1066 * get a local va for mappings for this pmap. 1067 */ 1068 ptq = get_ptbase(pmap) + i386_btop(va); 1069 if (*ptq) { 1070 (void) pmap_remove_pte(pmap, ptq, va); 1071 pmap_update_1pg(va); 1072 } 1073 return; 1074} 1075 1076/* 1077 * Remove the given range of addresses from the specified map. 1078 * 1079 * It is assumed that the start and end are properly 1080 * rounded to the page size. 1081 */ 1082void 1083pmap_remove(pmap, sva, eva) 1084 struct pmap *pmap; 1085 register vm_offset_t sva; 1086 register vm_offset_t eva; 1087{ 1088 register unsigned *ptbase; 1089 vm_offset_t pdnxt; 1090 vm_offset_t ptpaddr; 1091 vm_offset_t sindex, eindex; 1092 vm_page_t mpte; 1093 int anyvalid; 1094 1095 if (pmap == NULL) 1096 return; 1097 1098 /* 1099 * special handling of removing one page. a very 1100 * common operation and easy to short circuit some 1101 * code. 1102 */ 1103 if ((sva + PAGE_SIZE) == eva) { 1104 pmap_remove_page(pmap, sva); 1105 return; 1106 } 1107 1108 anyvalid = 0; 1109 1110 /* 1111 * Get a local virtual address for the mappings that are being 1112 * worked with. 1113 */ 1114 ptbase = get_ptbase(pmap); 1115 1116 sindex = i386_btop(sva); 1117 eindex = i386_btop(eva); 1118 1119 for (; sindex < eindex; sindex = pdnxt) { 1120 1121 /* 1122 * Calculate index for next page table. 1123 */ 1124 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1)); 1125 ptpaddr = (vm_offset_t) *pmap_pde(pmap, i386_ptob(sindex)); 1126 1127 /* 1128 * Weed out invalid mappings. Note: we assume that the page 1129 * directory table is always allocated, and in kernel virtual. 1130 */ 1131 if (ptpaddr == 0) 1132 continue; 1133 1134 if (sindex < i386_btop(UPT_MIN_ADDRESS)) { 1135 /* 1136 * get the vm_page_t for the page table page 1137 */ 1138 mpte = PHYS_TO_VM_PAGE(ptpaddr); 1139 1140 /* 1141 * if the pte isn't wired, just skip it. 1142 */ 1143 if (mpte->wire_count == 0) 1144 continue; 1145 } 1146 1147 /* 1148 * Limit our scan to either the end of the va represented 1149 * by the current page table page, or to the end of the 1150 * range being removed. 1151 */ 1152 if (pdnxt > eindex) { 1153 pdnxt = eindex; 1154 } 1155 1156 for ( ;sindex != pdnxt; sindex++) { 1157 vm_offset_t va; 1158 if (ptbase[sindex] == 0) { 1159 continue; 1160 } 1161 va = i386_ptob(sindex); 1162 anyvalid = 1; 1163 if (pmap_remove_pte(pmap, 1164 ptbase + sindex, va)) 1165 break; 1166 } 1167 } 1168 1169 if (anyvalid) { 1170 pmap_update(); 1171 } 1172} 1173 1174 1175void 1176pmap_remove_pte_mapping(pa) 1177 vm_offset_t pa; 1178{ 1179 register pv_entry_t pv, *ppv, npv; 1180 register unsigned *pte; 1181 vm_offset_t va; 1182 int anyvalid = 0; 1183 1184 ppv = pa_to_pvh(pa); 1185 1186 for (pv = *ppv; pv; pv=pv->pv_next) { 1187 unsigned tpte; 1188 struct pmap *pmap; 1189 1190 anyvalid = 1; 1191 pmap = pv->pv_pmap; 1192 pte = get_ptbase(pmap) + i386_btop(pv->pv_va); 1193 if (tpte = *pte) { 1194 pmap->pm_stats.resident_count--; 1195 *pte = 0; 1196 if (tpte & PG_W) 1197 pmap->pm_stats.wired_count--; 1198 } 1199 } 1200 1201 if (anyvalid) { 1202 for (pv = *ppv; pv; pv = npv) { 1203 npv = pv->pv_next; 1204 free_pv_entry(pv); 1205 } 1206 *ppv = NULL; 1207 } 1208} 1209 1210/* 1211 * Routine: pmap_remove_all 1212 * Function: 1213 * Removes this physical page from 1214 * all physical maps in which it resides. 1215 * Reflects back modify bits to the pager. 1216 * 1217 * Notes: 1218 * Original versions of this routine were very 1219 * inefficient because they iteratively called 1220 * pmap_remove (slow...) 1221 */ 1222static __inline void 1223pmap_remove_all(pa) 1224 vm_offset_t pa; 1225{ 1226 register pv_entry_t pv, *ppv, npv; 1227 register unsigned *pte, *ptbase; 1228 vm_offset_t va; 1229 vm_page_t m; 1230 int s; 1231 1232#if defined(PMAP_DIAGNOSTIC) 1233 /* 1234 * XXX this makes pmap_page_protect(NONE) illegal for non-managed 1235 * pages! 1236 */ 1237 if (!pmap_is_managed(pa)) { 1238 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%lx", pa); 1239 } 1240#endif 1241 1242 m = PHYS_TO_VM_PAGE(pa); 1243 ppv = pa_to_pvh(pa); 1244 1245 s = splvm(); 1246 for (pv = *ppv; pv; pv=pv->pv_next) { 1247 int tpte; 1248 struct pmap *pmap; 1249 1250 pmap = pv->pv_pmap; 1251 ptbase = get_ptbase(pmap); 1252 va = pv->pv_va; 1253 if (*pmap_pde(pmap, va) == 0) 1254 continue; 1255 pte = ptbase + i386_btop(va); 1256 if (tpte = ((int) *pte)) { 1257 pmap->pm_stats.resident_count--; 1258 *pte = 0; 1259 if (tpte & PG_W) 1260 pmap->pm_stats.wired_count--; 1261 /* 1262 * Update the vm_page_t clean and reference bits. 1263 */ 1264 if (tpte & PG_M) { 1265#if defined(PMAP_DIAGNOSTIC) 1266 if (pmap_nw_modified((pt_entry_t) tpte)) { 1267 printf("pmap_remove_all: modified page not writable: va: 0x%lx, pte: 0x%lx\n", va, tpte); 1268 } 1269#endif 1270 if ((va >= UPT_MIN_ADDRESS) && 1271 (va < UPT_MAX_ADDRESS)) 1272 continue; 1273 1274 if (va < clean_sva || va >= clean_eva) { 1275 m->dirty = VM_PAGE_BITS_ALL; 1276 } 1277 } 1278 } 1279 } 1280 1281 for (pv = *ppv; pv; pv = npv) { 1282 npv = pv->pv_next; 1283 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem); 1284 free_pv_entry(pv); 1285 } 1286 *ppv = NULL; 1287 1288 splx(s); 1289} 1290 1291/* 1292 * Set the physical protection on the 1293 * specified range of this map as requested. 1294 */ 1295void 1296pmap_protect(pmap, sva, eva, prot) 1297 register pmap_t pmap; 1298 vm_offset_t sva, eva; 1299 vm_prot_t prot; 1300{ 1301 register unsigned *pte; 1302 register vm_offset_t va; 1303 register unsigned *ptbase; 1304 vm_offset_t pdnxt; 1305 vm_offset_t ptpaddr; 1306 vm_offset_t sindex, eindex; 1307 vm_page_t mpte; 1308 int anyvalid; 1309 1310 1311 if (pmap == NULL) 1312 return; 1313 1314 if ((prot & VM_PROT_READ) == VM_PROT_NONE) { 1315 pmap_remove(pmap, sva, eva); 1316 return; 1317 } 1318 if (prot & VM_PROT_WRITE) 1319 return; 1320 1321 anyvalid = 0; 1322 1323 ptbase = get_ptbase(pmap); 1324 1325 sindex = i386_btop(sva); 1326 eindex = i386_btop(eva); 1327 1328 for (; sindex < eindex; sindex = pdnxt) { 1329 1330 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1)); 1331 ptpaddr = (vm_offset_t) *pmap_pde(pmap, i386_ptob(sindex)); 1332 1333 /* 1334 * Weed out invalid mappings. Note: we assume that the page 1335 * directory table is always allocated, and in kernel virtual. 1336 */ 1337 if (ptpaddr == 0) 1338 continue; 1339 1340 /* 1341 * Don't look at kernel page table pages 1342 */ 1343 if (sindex < i386_btop(UPT_MIN_ADDRESS)) { 1344 mpte = PHYS_TO_VM_PAGE(ptpaddr); 1345 1346 if (mpte->wire_count == 0) 1347 continue; 1348 } 1349 1350 if (pdnxt > eindex) { 1351 pdnxt = eindex; 1352 } 1353 1354 for (; sindex != pdnxt; sindex++) { 1355 1356 unsigned pbits = ptbase[sindex]; 1357 1358 if (pbits & PG_RW) { 1359 if (pbits & PG_M) { 1360 vm_page_t m = PHYS_TO_VM_PAGE(pbits); 1361 m->dirty = VM_PAGE_BITS_ALL; 1362 } 1363 ptbase[sindex] = pbits & ~(PG_M|PG_RW); 1364 anyvalid = 1; 1365 } 1366 } 1367 } 1368 if (anyvalid) 1369 pmap_update(); 1370} 1371 1372/* 1373 * Create a pv entry for page at pa for 1374 * (pmap, va). 1375 */ 1376static __inline void 1377pmap_insert_entry(pmap, va, mpte, pa) 1378 pmap_t pmap; 1379 vm_offset_t va; 1380 vm_page_t mpte; 1381 vm_offset_t pa; 1382{ 1383 1384 int s; 1385 pv_entry_t *ppv, pv; 1386 1387 s = splvm(); 1388 pv = get_pv_entry(); 1389 pv->pv_va = va; 1390 pv->pv_pmap = pmap; 1391 pv->pv_ptem = mpte; 1392 1393 ppv = pa_to_pvh(pa); 1394 if (*ppv) 1395 pv->pv_next = *ppv; 1396 else 1397 pv->pv_next = NULL; 1398 *ppv = pv; 1399 splx(s); 1400} 1401 1402/* 1403 * this routine is called if the page table page is not 1404 * mapped correctly. 1405 */ 1406static vm_page_t 1407_pmap_allocpte(pmap, va, ptepindex) 1408 pmap_t pmap; 1409 vm_offset_t va; 1410 int ptepindex; 1411{ 1412 vm_offset_t pteva, ptepa; 1413 vm_page_t m; 1414 int s; 1415 1416 /* 1417 * Find or fabricate a new pagetable page 1418 */ 1419retry: 1420 m = vm_page_lookup(pmap->pm_pteobj, ptepindex); 1421 if (m == NULL) { 1422 m = vm_page_alloc(pmap->pm_pteobj, ptepindex, VM_ALLOC_ZERO); 1423 if (m == NULL) { 1424 VM_WAIT; 1425 goto retry; 1426 } 1427 if ((m->flags & PG_ZERO) == 0) 1428 pmap_zero_page(VM_PAGE_TO_PHYS(m)); 1429 m->flags &= ~(PG_ZERO|PG_BUSY); 1430 m->valid = VM_PAGE_BITS_ALL; 1431 } else { 1432 if ((m->flags & PG_BUSY) || m->busy) { 1433 m->flags |= PG_WANTED; 1434 tsleep(m, PVM, "ptewai", 0); 1435 goto retry; 1436 } 1437 } 1438 1439 /* 1440 * mark the object writeable 1441 */ 1442 pmap->pm_pteobj->flags |= OBJ_WRITEABLE; 1443 1444 if (m->hold_count == 0) { 1445 s = splvm(); 1446 vm_page_unqueue(m); 1447 splx(s); 1448 ++m->wire_count; 1449 ++cnt.v_wire_count; 1450 } 1451 1452 /* 1453 * Increment the hold count for the page table page 1454 * (denoting a new mapping.) 1455 */ 1456 ++m->hold_count; 1457 1458 /* 1459 * Map the pagetable page into the process address space, if 1460 * it isn't already there. 1461 */ 1462 pteva = ((vm_offset_t) vtopte(va)) & PG_FRAME; 1463 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex]; 1464 if (ptepa == 0) { 1465 pv_entry_t pv, *ppv; 1466 1467 pmap->pm_stats.resident_count++; 1468 1469 s = splvm(); 1470 pv = get_pv_entry(); 1471 1472 pv->pv_va = pteva; 1473 pv->pv_pmap = pmap; 1474 pv->pv_next = NULL; 1475 pv->pv_ptem = NULL; 1476 1477 ptepa = VM_PAGE_TO_PHYS(m); 1478 ppv = pa_to_pvh(ptepa); 1479#if defined(PMAP_DIAGNOSTIC) 1480 if (*ppv) 1481 panic("pmap_allocpte: page is already mapped"); 1482#endif 1483 *ppv = pv; 1484 splx(s); 1485 pmap_update_1pg(pteva); 1486 } else { 1487#if defined(PMAP_DIAGNOSTIC) 1488 if (VM_PAGE_TO_PHYS(m) != (ptepa & PG_FRAME)) 1489 panic("pmap_allocpte: mismatch"); 1490#endif 1491 } 1492 pmap->pm_pdir[ptepindex] = 1493 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_MANAGED); 1494 m->flags |= PG_MAPPED; 1495 return m; 1496} 1497 1498static __inline vm_page_t 1499pmap_allocpte(pmap, va) 1500 pmap_t pmap; 1501 vm_offset_t va; 1502{ 1503 int ptepindex; 1504 vm_offset_t ptepa; 1505 vm_page_t m; 1506 1507 /* 1508 * Calculate pagetable page index 1509 */ 1510 ptepindex = va >> PDRSHIFT; 1511 1512 /* 1513 * Get the page directory entry 1514 */ 1515 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex]; 1516 1517 /* 1518 * If the page table page is mapped, we just increment the 1519 * hold count, and activate it. 1520 */ 1521 if ((ptepa & (PG_RW|PG_U|PG_V)) == (PG_RW|PG_U|PG_V)) { 1522 m = PHYS_TO_VM_PAGE(ptepa); 1523 if (m->hold_count == 0) { 1524 int s = splvm(); 1525 vm_page_unqueue(m); 1526 splx(s); 1527 ++m->wire_count; 1528 ++cnt.v_wire_count; 1529 } 1530 ++m->hold_count; 1531 return m; 1532 } 1533 return _pmap_allocpte(pmap, va, ptepindex); 1534} 1535 1536/* 1537 * Insert the given physical page (p) at 1538 * the specified virtual address (v) in the 1539 * target physical map with the protection requested. 1540 * 1541 * If specified, the page will be wired down, meaning 1542 * that the related pte can not be reclaimed. 1543 * 1544 * NB: This is the only routine which MAY NOT lazy-evaluate 1545 * or lose information. That is, this routine must actually 1546 * insert this page into the given map NOW. 1547 */ 1548void 1549pmap_enter(pmap, va, pa, prot, wired) 1550 register pmap_t pmap; 1551 vm_offset_t va; 1552 register vm_offset_t pa; 1553 vm_prot_t prot; 1554 boolean_t wired; 1555{ 1556 register unsigned *pte; 1557 vm_offset_t opa; 1558 vm_offset_t origpte, newpte; 1559 vm_page_t mpte; 1560 1561 if (pmap == NULL) 1562 return; 1563 1564 va &= PG_FRAME; 1565 if (va > VM_MAX_KERNEL_ADDRESS) 1566 panic("pmap_enter: toobig"); 1567 1568 mpte = NULL; 1569 /* 1570 * In the case that a page table page is not 1571 * resident, we are creating it here. 1572 */ 1573 if (va < UPT_MIN_ADDRESS) 1574 mpte = pmap_allocpte(pmap, va); 1575 1576 pte = pmap_pte(pmap, va); 1577 /* 1578 * Page Directory table entry not valid, we need a new PT page 1579 */ 1580 if (pte == NULL) { 1581 printf("kernel page directory invalid pdir=%p, va=0x%lx\n", 1582 pmap->pm_pdir[PTDPTDI], va); 1583 panic("invalid kernel page directory"); 1584 } 1585 1586 origpte = *(vm_offset_t *)pte; 1587 pa &= PG_FRAME; 1588 opa = origpte & PG_FRAME; 1589 1590 /* 1591 * Mapping has not changed, must be protection or wiring change. 1592 */ 1593 if (opa == pa) { 1594 /* 1595 * Wiring change, just update stats. We don't worry about 1596 * wiring PT pages as they remain resident as long as there 1597 * are valid mappings in them. Hence, if a user page is wired, 1598 * the PT page will be also. 1599 */ 1600 if (wired && ((origpte & PG_W) == 0)) 1601 pmap->pm_stats.wired_count++; 1602 else if (!wired && (origpte & PG_W)) 1603 pmap->pm_stats.wired_count--; 1604 1605#if defined(PMAP_DIAGNOSTIC) 1606 if (pmap_nw_modified((pt_entry_t) origpte)) { 1607 printf("pmap_enter: modified page not writable: va: 0x%lx, pte: 0x%lx\n", va, origpte); 1608 } 1609#endif 1610 1611 /* 1612 * We might be turning off write access to the page, 1613 * so we go ahead and sense modify status. 1614 */ 1615 if (origpte & PG_MANAGED) { 1616 vm_page_t m; 1617 if (origpte & PG_M) { 1618 m = PHYS_TO_VM_PAGE(pa); 1619 m->dirty = VM_PAGE_BITS_ALL; 1620 } 1621 pa |= PG_MANAGED; 1622 } 1623 1624 if (mpte) 1625 --mpte->hold_count; 1626 1627 goto validate; 1628 } 1629 /* 1630 * Mapping has changed, invalidate old range and fall through to 1631 * handle validating new mapping. 1632 */ 1633 if (opa) 1634 (void) pmap_remove_pte(pmap, pte, va); 1635 1636 /* 1637 * Enter on the PV list if part of our managed memory Note that we 1638 * raise IPL while manipulating pv_table since pmap_enter can be 1639 * called at interrupt time. 1640 */ 1641 if (pmap_is_managed(pa)) { 1642 pmap_insert_entry(pmap, va, mpte, pa); 1643 pa |= PG_MANAGED; 1644 } 1645 1646 /* 1647 * Increment counters 1648 */ 1649 pmap->pm_stats.resident_count++; 1650 if (wired) 1651 pmap->pm_stats.wired_count++; 1652 1653validate: 1654 /* 1655 * Now validate mapping with desired protection/wiring. 1656 */ 1657 newpte = (vm_offset_t) (pa | pte_prot(pmap, prot) | PG_V); 1658 1659 if (wired) 1660 newpte |= PG_W; 1661 if (va < UPT_MIN_ADDRESS) 1662 newpte |= PG_U; 1663 1664 /* 1665 * if the mapping or permission bits are different, we need 1666 * to update the pte. 1667 */ 1668 if ((origpte & ~(PG_M|PG_A)) != newpte) { 1669 *pte = newpte; 1670 if (origpte) 1671 pmap_update_1pg(va); 1672 } 1673} 1674 1675/* 1676 * this code makes some *MAJOR* assumptions: 1677 * 1. Current pmap & pmap exists. 1678 * 2. Not wired. 1679 * 3. Read access. 1680 * 4. No page table pages. 1681 * 5. Tlbflush is deferred to calling procedure. 1682 * 6. Page IS managed. 1683 * but is *MUCH* faster than pmap_enter... 1684 */ 1685 1686static void 1687pmap_enter_quick(pmap, va, pa) 1688 register pmap_t pmap; 1689 vm_offset_t va; 1690 register vm_offset_t pa; 1691{ 1692 register unsigned *pte; 1693 vm_page_t mpte; 1694 1695 mpte = NULL; 1696 /* 1697 * In the case that a page table page is not 1698 * resident, we are creating it here. 1699 */ 1700 if (va < UPT_MIN_ADDRESS) 1701 mpte = pmap_allocpte(pmap, va); 1702 1703 pte = (unsigned *)vtopte(va); 1704 if (*pte) 1705 (void) pmap_remove_pte(pmap, pte, va); 1706 1707 /* 1708 * Enter on the PV list if part of our managed memory Note that we 1709 * raise IPL while manipulating pv_table since pmap_enter can be 1710 * called at interrupt time. 1711 */ 1712 pmap_insert_entry(pmap, va, mpte, pa); 1713 1714 /* 1715 * Increment counters 1716 */ 1717 pmap->pm_stats.resident_count++; 1718 1719 /* 1720 * Now validate mapping with RO protection 1721 */ 1722 *pte = pa | PG_V | PG_U | PG_MANAGED; 1723 1724 return; 1725} 1726 1727#define MAX_INIT_PT (96) 1728/* 1729 * pmap_object_init_pt preloads the ptes for a given object 1730 * into the specified pmap. This eliminates the blast of soft 1731 * faults on process startup and immediately after an mmap. 1732 */ 1733void 1734pmap_object_init_pt(pmap, addr, object, pindex, size, limit) 1735 pmap_t pmap; 1736 vm_offset_t addr; 1737 vm_object_t object; 1738 vm_pindex_t pindex; 1739 vm_size_t size; 1740 int limit; 1741{ 1742 vm_offset_t tmpidx; 1743 int psize; 1744 vm_page_t p; 1745 int objpgs; 1746 1747 psize = (size >> PAGE_SHIFT); 1748 1749 if (!pmap || (object->type != OBJT_VNODE) || 1750 (limit && (psize > MAX_INIT_PT) && 1751 (object->resident_page_count > MAX_INIT_PT))) { 1752 return; 1753 } 1754 1755 /* 1756 * if we are processing a major portion of the object, then scan the 1757 * entire thing. 1758 */ 1759 if (psize > (object->size >> 2)) { 1760 objpgs = psize; 1761 1762 for (p = TAILQ_FIRST(&object->memq); 1763 ((objpgs > 0) && (p != NULL)); 1764 p = TAILQ_NEXT(p, listq)) { 1765 1766 tmpidx = p->pindex; 1767 if (tmpidx < pindex) { 1768 continue; 1769 } 1770 tmpidx -= pindex; 1771 if (tmpidx >= psize) { 1772 continue; 1773 } 1774 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) && 1775 (p->busy == 0) && 1776 (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) { 1777 if (p->queue == PQ_CACHE) 1778 vm_page_deactivate(p); 1779 p->flags |= PG_BUSY; 1780 pmap_enter_quick(pmap, 1781 addr + (tmpidx << PAGE_SHIFT), 1782 VM_PAGE_TO_PHYS(p)); 1783 p->flags |= PG_MAPPED; 1784 PAGE_WAKEUP(p); 1785 } 1786 objpgs -= 1; 1787 } 1788 } else { 1789 /* 1790 * else lookup the pages one-by-one. 1791 */ 1792 for (tmpidx = 0; tmpidx < psize; tmpidx += 1) { 1793 p = vm_page_lookup(object, tmpidx + pindex); 1794 if (p && 1795 ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) && 1796 (p->busy == 0) && 1797 (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) { 1798 p->flags |= PG_BUSY; 1799 pmap_enter_quick(pmap, 1800 addr + (tmpidx << PAGE_SHIFT), 1801 VM_PAGE_TO_PHYS(p)); 1802 p->flags |= PG_MAPPED; 1803 PAGE_WAKEUP(p); 1804 } 1805 } 1806 } 1807 return; 1808} 1809 1810/* 1811 * pmap_prefault provides a quick way of clustering 1812 * pagefaults into a processes address space. It is a "cousin" 1813 * of pmap_object_init_pt, except it runs at page fault time instead 1814 * of mmap time. 1815 */ 1816#define PFBAK 2 1817#define PFFOR 2 1818#define PAGEORDER_SIZE (PFBAK+PFFOR) 1819 1820static int pmap_prefault_pageorder[] = { 1821 -PAGE_SIZE, PAGE_SIZE, -2 * PAGE_SIZE, 2 * PAGE_SIZE 1822}; 1823 1824void 1825pmap_prefault(pmap, addra, entry, object) 1826 pmap_t pmap; 1827 vm_offset_t addra; 1828 vm_map_entry_t entry; 1829 vm_object_t object; 1830{ 1831 int i; 1832 vm_offset_t starta; 1833 vm_offset_t addr; 1834 vm_pindex_t pindex; 1835 vm_page_t m; 1836 1837 if (entry->object.vm_object != object) 1838 return; 1839 1840 if (!curproc || (pmap != &curproc->p_vmspace->vm_pmap)) 1841 return; 1842 1843 starta = addra - PFBAK * PAGE_SIZE; 1844 if (starta < entry->start) { 1845 starta = entry->start; 1846 } else if (starta > addra) { 1847 starta = 0; 1848 } 1849 1850 for (i = 0; i < PAGEORDER_SIZE; i++) { 1851 vm_object_t lobject; 1852 unsigned *pte; 1853 1854 addr = addra + pmap_prefault_pageorder[i]; 1855 if (addr < starta || addr >= entry->end) 1856 continue; 1857 1858 if ((*pmap_pde(pmap, addr)) == NULL) 1859 continue; 1860 1861 pte = (unsigned *) vtopte(addr); 1862 if (*pte) 1863 continue; 1864 1865 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT; 1866 lobject = object; 1867 for (m = vm_page_lookup(lobject, pindex); 1868 (!m && (lobject->type == OBJT_DEFAULT) && (lobject->backing_object)); 1869 lobject = lobject->backing_object) { 1870 if (lobject->backing_object_offset & PAGE_MASK) 1871 break; 1872 pindex += (lobject->backing_object_offset >> PAGE_SHIFT); 1873 m = vm_page_lookup(lobject->backing_object, pindex); 1874 } 1875 1876 /* 1877 * give-up when a page is not in memory 1878 */ 1879 if (m == NULL) 1880 break; 1881 1882 if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) && 1883 (m->busy == 0) && 1884 (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) { 1885 1886 if (m->queue == PQ_CACHE) { 1887 vm_page_deactivate(m); 1888 } 1889 m->flags |= PG_BUSY; 1890 pmap_enter_quick(pmap, addr, VM_PAGE_TO_PHYS(m)); 1891 m->flags |= PG_MAPPED; 1892 PAGE_WAKEUP(m); 1893 } 1894 } 1895} 1896 1897/* 1898 * Routine: pmap_change_wiring 1899 * Function: Change the wiring attribute for a map/virtual-address 1900 * pair. 1901 * In/out conditions: 1902 * The mapping must already exist in the pmap. 1903 */ 1904void 1905pmap_change_wiring(pmap, va, wired) 1906 register pmap_t pmap; 1907 vm_offset_t va; 1908 boolean_t wired; 1909{ 1910 register unsigned *pte; 1911 1912 if (pmap == NULL) 1913 return; 1914 1915 pte = pmap_pte(pmap, va); 1916 1917 if (wired && !pmap_pte_w(pte)) 1918 pmap->pm_stats.wired_count++; 1919 else if (!wired && pmap_pte_w(pte)) 1920 pmap->pm_stats.wired_count--; 1921 1922 /* 1923 * Wiring is not a hardware characteristic so there is no need to 1924 * invalidate TLB. 1925 */ 1926 pmap_pte_set_w(pte, wired); 1927} 1928 1929 1930 1931/* 1932 * Copy the range specified by src_addr/len 1933 * from the source map to the range dst_addr/len 1934 * in the destination map. 1935 * 1936 * This routine is only advisory and need not do anything. 1937 */ 1938void 1939pmap_copy(dst_pmap, src_pmap, dst_addr, len, src_addr) 1940 pmap_t dst_pmap, src_pmap; 1941 vm_offset_t dst_addr; 1942 vm_size_t len; 1943 vm_offset_t src_addr; 1944{ 1945 vm_offset_t addr; 1946 vm_offset_t end_addr = src_addr + len; 1947 vm_offset_t pdnxt; 1948 unsigned src_frame, dst_frame; 1949 1950 if (dst_addr != src_addr) 1951 return; 1952 1953 src_frame = ((unsigned) src_pmap->pm_pdir[PTDPTDI]) & PG_FRAME; 1954 if (src_frame != (((unsigned) PTDpde) & PG_FRAME)) 1955 return; 1956 1957 dst_frame = ((unsigned) dst_pmap->pm_pdir[PTDPTDI]) & PG_FRAME; 1958 if (dst_frame != (((unsigned) APTDpde) & PG_FRAME)) { 1959 APTDpde = (pd_entry_t) (dst_frame | PG_RW | PG_V); 1960 pmap_update(); 1961 } 1962 1963 for(addr = src_addr; addr < end_addr; addr = pdnxt) { 1964 unsigned *src_pte, *dst_pte; 1965 vm_page_t dstmpte, srcmpte; 1966 vm_offset_t srcptepaddr; 1967 1968 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1)); 1969 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[addr >> PDRSHIFT]; 1970 if (srcptepaddr == 0) { 1971 continue; 1972 } 1973 1974 srcmpte = PHYS_TO_VM_PAGE(srcptepaddr); 1975 if (srcmpte->hold_count == 0) 1976 continue; 1977 1978 if (pdnxt > end_addr) 1979 pdnxt = end_addr; 1980 1981 src_pte = (unsigned *) vtopte(addr); 1982 dst_pte = (unsigned *) avtopte(addr); 1983 while (addr < pdnxt) { 1984 unsigned ptetemp; 1985 ptetemp = *src_pte; 1986 /* 1987 * we only virtual copy managed pages 1988 */ 1989 if ((ptetemp & PG_MANAGED) != 0) { 1990 /* 1991 * We have to check after allocpte for the 1992 * pte still being around... allocpte can 1993 * block. 1994 */ 1995 dstmpte = pmap_allocpte(dst_pmap, addr); 1996 if (ptetemp = *src_pte) { 1997 /* 1998 * Simply clear the modified and accessed (referenced) 1999 * bits. 2000 */ 2001 *dst_pte = ptetemp & ~(PG_M|PG_A); 2002 dst_pmap->pm_stats.resident_count++; 2003 pmap_insert_entry(dst_pmap, addr, dstmpte, 2004 (ptetemp & PG_FRAME)); 2005 } else { 2006 pmap_unwire_pte_hold(dstmpte); 2007 } 2008 if (dstmpte->hold_count >= srcmpte->hold_count) 2009 break; 2010 } 2011 addr += PAGE_SIZE; 2012 ++src_pte; 2013 ++dst_pte; 2014 } 2015 } 2016} 2017 2018/* 2019 * Routine: pmap_kernel 2020 * Function: 2021 * Returns the physical map handle for the kernel. 2022 */ 2023pmap_t 2024pmap_kernel() 2025{ 2026 return (kernel_pmap); 2027} 2028 2029/* 2030 * pmap_zero_page zeros the specified (machine independent) 2031 * page by mapping the page into virtual memory and using 2032 * bzero to clear its contents, one machine dependent page 2033 * at a time. 2034 */ 2035void 2036pmap_zero_page(phys) 2037 vm_offset_t phys; 2038{ 2039 if (*(int *) CMAP2) 2040 panic("pmap_zero_page: CMAP busy"); 2041 2042 *(int *) CMAP2 = PG_V | PG_RW | (phys & PG_FRAME); 2043 bzero(CADDR2, PAGE_SIZE); 2044 *(int *) CMAP2 = 0; 2045 pmap_update_1pg((vm_offset_t) CADDR2); 2046} 2047 2048/* 2049 * pmap_copy_page copies the specified (machine independent) 2050 * page by mapping the page into virtual memory and using 2051 * bcopy to copy the page, one machine dependent page at a 2052 * time. 2053 */ 2054void 2055pmap_copy_page(src, dst) 2056 vm_offset_t src; 2057 vm_offset_t dst; 2058{ 2059 if (*(int *) CMAP1 || *(int *) CMAP2) 2060 panic("pmap_copy_page: CMAP busy"); 2061 2062 *(int *) CMAP1 = PG_V | PG_RW | (src & PG_FRAME); 2063 *(int *) CMAP2 = PG_V | PG_RW | (dst & PG_FRAME); 2064 2065#if __GNUC__ > 1 2066 memcpy(CADDR2, CADDR1, PAGE_SIZE); 2067#else 2068 bcopy(CADDR1, CADDR2, PAGE_SIZE); 2069#endif 2070 *(int *) CMAP1 = 0; 2071 *(int *) CMAP2 = 0; 2072 pmap_update_2pg( (vm_offset_t) CADDR1, (vm_offset_t) CADDR2); 2073} 2074 2075 2076/* 2077 * Routine: pmap_pageable 2078 * Function: 2079 * Make the specified pages (by pmap, offset) 2080 * pageable (or not) as requested. 2081 * 2082 * A page which is not pageable may not take 2083 * a fault; therefore, its page table entry 2084 * must remain valid for the duration. 2085 * 2086 * This routine is merely advisory; pmap_enter 2087 * will specify that these pages are to be wired 2088 * down (or not) as appropriate. 2089 */ 2090void 2091pmap_pageable(pmap, sva, eva, pageable) 2092 pmap_t pmap; 2093 vm_offset_t sva, eva; 2094 boolean_t pageable; 2095{ 2096} 2097 2098/* 2099 * this routine returns true if a physical page resides 2100 * in the given pmap. 2101 */ 2102boolean_t 2103pmap_page_exists(pmap, pa) 2104 pmap_t pmap; 2105 vm_offset_t pa; 2106{ 2107 register pv_entry_t *ppv, pv; 2108 int s; 2109 2110 if (!pmap_is_managed(pa)) 2111 return FALSE; 2112 2113 s = splvm(); 2114 2115 ppv = pa_to_pvh(pa); 2116 /* 2117 * Not found, check current mappings returning immediately if found. 2118 */ 2119 for (pv = *ppv; pv; pv = pv->pv_next) { 2120 if (pv->pv_pmap == pmap) { 2121 splx(s); 2122 return TRUE; 2123 } 2124 } 2125 splx(s); 2126 return (FALSE); 2127} 2128 2129/* 2130 * pmap_testbit tests bits in pte's 2131 * note that the testbit/changebit routines are inline, 2132 * and a lot of things compile-time evaluate. 2133 */ 2134static __inline boolean_t 2135pmap_testbit(pa, bit) 2136 register vm_offset_t pa; 2137 int bit; 2138{ 2139 register pv_entry_t *ppv, pv; 2140 unsigned *pte; 2141 int s; 2142 2143 if (!pmap_is_managed(pa)) 2144 return FALSE; 2145 2146 s = splvm(); 2147 2148 ppv = pa_to_pvh(pa); 2149 /* 2150 * Not found, check current mappings returning immediately if found. 2151 */ 2152 for (pv = *ppv ;pv; pv = pv->pv_next) { 2153 /* 2154 * if the bit being tested is the modified bit, then 2155 * mark UPAGES as always modified, and ptes as never 2156 * modified. 2157 */ 2158 if (bit & (PG_A|PG_M)) { 2159 if ((pv->pv_va >= UPT_MIN_ADDRESS) && 2160 (pv->pv_va < UPT_MAX_ADDRESS)) { 2161 continue; 2162 } 2163 if ((pv->pv_va >= clean_sva) && 2164 (pv->pv_va < clean_eva)) { 2165 continue; 2166 } 2167 } 2168 if (!pv->pv_pmap) { 2169#if defined(PMAP_DIAGNOSTIC) 2170 printf("Null pmap (tb) at va: 0x%lx\n", pv->pv_va); 2171#endif 2172 continue; 2173 } 2174 pte = pmap_pte(pv->pv_pmap, pv->pv_va); 2175 if (pte == NULL) 2176 continue; 2177 if ((int) *pte & bit) { 2178 splx(s); 2179 return TRUE; 2180 } 2181 } 2182 splx(s); 2183 return (FALSE); 2184} 2185 2186/* 2187 * this routine is used to modify bits in ptes 2188 */ 2189static __inline void 2190pmap_changebit(pa, bit, setem) 2191 vm_offset_t pa; 2192 int bit; 2193 boolean_t setem; 2194{ 2195 register pv_entry_t pv, *ppv; 2196 register unsigned *pte; 2197 vm_offset_t va; 2198 int changed; 2199 int s; 2200 2201 if (!pmap_is_managed(pa)) 2202 return; 2203 2204 s = splvm(); 2205 2206 changed = 0; 2207 ppv = pa_to_pvh(pa); 2208 /* 2209 * Loop over all current mappings setting/clearing as appropos If 2210 * setting RO do we need to clear the VAC? 2211 */ 2212 for ( pv = *ppv; pv; pv = pv->pv_next) { 2213 va = pv->pv_va; 2214 2215 /* 2216 * don't write protect pager mappings 2217 */ 2218 if (!setem && (bit == PG_RW)) { 2219 if (va >= clean_sva && va < clean_eva) 2220 continue; 2221 } 2222 if (!pv->pv_pmap) { 2223#if defined(PMAP_DIAGNOSTIC) 2224 printf("Null pmap (cb) at va: 0x%lx\n", va); 2225#endif 2226 continue; 2227 } 2228 2229 pte = pmap_pte(pv->pv_pmap, va); 2230 if (pte == NULL) 2231 continue; 2232 if (setem) { 2233 *(int *)pte |= bit; 2234 changed = 1; 2235 } else { 2236 vm_offset_t pbits = *(vm_offset_t *)pte; 2237 if (pbits & bit) 2238 changed = 1; 2239 if (bit == PG_RW) { 2240 if (pbits & PG_M) { 2241 vm_page_t m; 2242 vm_offset_t pa = pbits & PG_FRAME; 2243 m = PHYS_TO_VM_PAGE(pa); 2244 m->dirty = VM_PAGE_BITS_ALL; 2245 } 2246 *(int *)pte = pbits & ~(PG_M|PG_RW); 2247 } else { 2248 *(int *)pte = pbits & ~bit; 2249 } 2250 } 2251 } 2252 splx(s); 2253 if (changed) 2254 pmap_update(); 2255} 2256 2257/* 2258 * pmap_page_protect: 2259 * 2260 * Lower the permission for all mappings to a given page. 2261 */ 2262void 2263pmap_page_protect(phys, prot) 2264 vm_offset_t phys; 2265 vm_prot_t prot; 2266{ 2267 if ((prot & VM_PROT_WRITE) == 0) { 2268 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) 2269 pmap_changebit(phys, PG_RW, FALSE); 2270 else { 2271 pmap_remove_all(phys); 2272 pmap_update(); 2273 } 2274 } 2275} 2276 2277vm_offset_t 2278pmap_phys_address(ppn) 2279 int ppn; 2280{ 2281 return (i386_ptob(ppn)); 2282} 2283 2284/* 2285 * pmap_is_referenced: 2286 * 2287 * Return whether or not the specified physical page was referenced 2288 * by any physical maps. 2289 */ 2290boolean_t 2291pmap_is_referenced(vm_offset_t pa) 2292{ 2293 return pmap_testbit((pa), PG_A); 2294} 2295 2296/* 2297 * pmap_is_modified: 2298 * 2299 * Return whether or not the specified physical page was modified 2300 * in any physical maps. 2301 */ 2302boolean_t 2303pmap_is_modified(vm_offset_t pa) 2304{ 2305 return pmap_testbit((pa), PG_M); 2306} 2307 2308/* 2309 * Clear the modify bits on the specified physical page. 2310 */ 2311void 2312pmap_clear_modify(vm_offset_t pa) 2313{ 2314 pmap_changebit((pa), PG_M, FALSE); 2315} 2316 2317/* 2318 * pmap_clear_reference: 2319 * 2320 * Clear the reference bit on the specified physical page. 2321 */ 2322void 2323pmap_clear_reference(vm_offset_t pa) 2324{ 2325 pmap_changebit((pa), PG_A, FALSE); 2326} 2327 2328/* 2329 * Miscellaneous support routines follow 2330 */ 2331 2332static void 2333i386_protection_init() 2334{ 2335 register int *kp, prot; 2336 2337 kp = protection_codes; 2338 for (prot = 0; prot < 8; prot++) { 2339 switch (prot) { 2340 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE: 2341 /* 2342 * Read access is also 0. There isn't any execute bit, 2343 * so just make it readable. 2344 */ 2345 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE: 2346 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE: 2347 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE: 2348 *kp++ = 0; 2349 break; 2350 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE: 2351 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE: 2352 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE: 2353 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE: 2354 *kp++ = PG_RW; 2355 break; 2356 } 2357 } 2358} 2359 2360/* 2361 * Map a set of physical memory pages into the kernel virtual 2362 * address space. Return a pointer to where it is mapped. This 2363 * routine is intended to be used for mapping device memory, 2364 * NOT real memory. The non-cacheable bits are set on each 2365 * mapped page. 2366 */ 2367void * 2368pmap_mapdev(pa, size) 2369 vm_offset_t pa; 2370 vm_size_t size; 2371{ 2372 vm_offset_t va, tmpva; 2373 unsigned *pte; 2374 2375 size = roundup(size, PAGE_SIZE); 2376 2377 va = kmem_alloc_pageable(kernel_map, size); 2378 if (!va) 2379 panic("pmap_mapdev: Couldn't alloc kernel virtual memory"); 2380 2381 pa = pa & PG_FRAME; 2382 for (tmpva = va; size > 0;) { 2383 pte = (unsigned *)vtopte(tmpva); 2384 *pte = pa | PG_RW | PG_V | PG_N; 2385 size -= PAGE_SIZE; 2386 tmpva += PAGE_SIZE; 2387 pa += PAGE_SIZE; 2388 } 2389 pmap_update(); 2390 2391 return ((void *) va); 2392} 2393 2394int 2395pmap_mincore(pmap, addr) 2396 pmap_t pmap; 2397 vm_offset_t addr; 2398{ 2399 2400 unsigned *ptep, pte; 2401 int val = 0; 2402 2403 ptep = pmap_pte(pmap, addr); 2404 if (ptep == 0) { 2405 return 0; 2406 } 2407 2408 if ((pte = *ptep)) { 2409 vm_offset_t pa; 2410 val = MINCORE_INCORE; 2411 pa = pte & PG_FRAME; 2412 2413 /* 2414 * Modified by us 2415 */ 2416 if (pte & PG_M) 2417 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER; 2418 /* 2419 * Modified by someone 2420 */ 2421 else if (PHYS_TO_VM_PAGE(pa)->dirty || 2422 pmap_is_modified(pa)) 2423 val |= MINCORE_MODIFIED_OTHER; 2424 /* 2425 * Referenced by us 2426 */ 2427 if (pte & PG_U) 2428 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER; 2429 2430 /* 2431 * Referenced by someone 2432 */ 2433 else if ((PHYS_TO_VM_PAGE(pa)->flags & PG_REFERENCED) || 2434 pmap_is_referenced(pa)) 2435 val |= MINCORE_REFERENCED_OTHER; 2436 } 2437 return val; 2438} 2439 2440#if defined(PMAP_DEBUG) 2441pmap_pid_dump(int pid) { 2442 pmap_t pmap; 2443 struct proc *p; 2444 int npte = 0; 2445 int index; 2446 for (p = allproc.lh_first; p != NULL; p = p->p_list.le_next) { 2447 if (p->p_pid != pid) 2448 continue; 2449 2450 if (p->p_vmspace) { 2451 int i,j; 2452 index = 0; 2453 pmap = &p->p_vmspace->vm_pmap; 2454 for(i=0;i<1024;i++) { 2455 pd_entry_t *pde; 2456 unsigned *pte; 2457 unsigned base = i << PDRSHIFT; 2458 2459 pde = &pmap->pm_pdir[i]; 2460 if (pde && pmap_pde_v(pde)) { 2461 for(j=0;j<1024;j++) { 2462 unsigned va = base + (j << PAGE_SHIFT); 2463 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) { 2464 if (index) { 2465 index = 0; 2466 printf("\n"); 2467 } 2468 return npte; 2469 } 2470 pte = pmap_pte( pmap, va); 2471 if (pte && pmap_pte_v(pte)) { 2472 vm_offset_t pa; 2473 vm_page_t m; 2474 pa = *(int *)pte; 2475 m = PHYS_TO_VM_PAGE((pa & PG_FRAME)); 2476 printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x", 2477 va, pa, m->hold_count, m->wire_count, m->flags); 2478 npte++; 2479 index++; 2480 if (index >= 2) { 2481 index = 0; 2482 printf("\n"); 2483 } else { 2484 printf(" "); 2485 } 2486 } 2487 } 2488 } 2489 } 2490 } 2491 } 2492 return npte; 2493} 2494#endif 2495 2496#if defined(DEBUG) 2497 2498static void pads __P((pmap_t pm)); 2499static void pmap_pvdump __P((vm_offset_t pa)); 2500 2501/* print address space of pmap*/ 2502static void 2503pads(pm) 2504 pmap_t pm; 2505{ 2506 unsigned va, i, j; 2507 unsigned *ptep; 2508 2509 if (pm == kernel_pmap) 2510 return; 2511 for (i = 0; i < 1024; i++) 2512 if (pm->pm_pdir[i]) 2513 for (j = 0; j < 1024; j++) { 2514 va = (i << PDRSHIFT) + (j << PAGE_SHIFT); 2515 if (pm == kernel_pmap && va < KERNBASE) 2516 continue; 2517 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS) 2518 continue; 2519 ptep = pmap_pte(pm, va); 2520 if (pmap_pte_v(ptep)) 2521 printf("%x:%x ", va, *(int *) ptep); 2522 }; 2523 2524} 2525 2526static void 2527pmap_pvdump(pa) 2528 vm_offset_t pa; 2529{ 2530 register pv_entry_t pv; 2531 2532 printf("pa %x", pa); 2533 for (pv = pa_to_pvh(pa); pv; pv = pv->pv_next) { 2534#ifdef used_to_be 2535 printf(" -> pmap %x, va %x, flags %x", 2536 pv->pv_pmap, pv->pv_va, pv->pv_flags); 2537#endif 2538 printf(" -> pmap %x, va %x", 2539 pv->pv_pmap, pv->pv_va); 2540 pads(pv->pv_pmap); 2541 } 2542 printf(" "); 2543} 2544#endif 2545