pmap.c revision 16136
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.99 1996/06/05 03:31:26 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 1030 if (va < clean_sva || va >= clean_eva) { 1031 if ((va < UPT_MIN_ADDRESS) || (va >= UPT_MAX_ADDRESS)) 1032 PHYS_TO_VM_PAGE(oldpte)->dirty = VM_PAGE_BITS_ALL; 1033 } 1034 } 1035 ppv = pa_to_pvh(oldpte); 1036 rtval = pmap_remove_entry(pmap, ppv, va); 1037#if defined(notyet) 1038 if (*ppv == NULL) { 1039 PHYS_TO_VM_PAGE(oldpte)->flags &= ~PG_MAPPED; 1040 } 1041#endif 1042 return rtval; 1043 } else { 1044 return pmap_unuse_pt(pmap, va, NULL); 1045 } 1046 1047 return 0; 1048} 1049 1050/* 1051 * Remove a single page from a process address space 1052 */ 1053static __inline void 1054pmap_remove_page(pmap, va) 1055 struct pmap *pmap; 1056 register vm_offset_t va; 1057{ 1058 register unsigned *ptq; 1059 1060 /* 1061 * if there is no pte for this address, just skip it!!! 1062 */ 1063 if (*pmap_pde(pmap, va) == 0) { 1064 return; 1065 } 1066 1067 /* 1068 * get a local va for mappings for this pmap. 1069 */ 1070 ptq = get_ptbase(pmap) + i386_btop(va); 1071 if (*ptq) { 1072 (void) pmap_remove_pte(pmap, ptq, va); 1073 pmap_update_1pg(va); 1074 } 1075 return; 1076} 1077 1078/* 1079 * Remove the given range of addresses from the specified map. 1080 * 1081 * It is assumed that the start and end are properly 1082 * rounded to the page size. 1083 */ 1084void 1085pmap_remove(pmap, sva, eva) 1086 struct pmap *pmap; 1087 register vm_offset_t sva; 1088 register vm_offset_t eva; 1089{ 1090 register unsigned *ptbase; 1091 vm_offset_t pdnxt; 1092 vm_offset_t ptpaddr; 1093 vm_offset_t sindex, eindex; 1094 vm_page_t mpte; 1095 int anyvalid; 1096 1097 if (pmap == NULL) 1098 return; 1099 1100 /* 1101 * special handling of removing one page. a very 1102 * common operation and easy to short circuit some 1103 * code. 1104 */ 1105 if ((sva + PAGE_SIZE) == eva) { 1106 pmap_remove_page(pmap, sva); 1107 return; 1108 } 1109 1110 anyvalid = 0; 1111 1112 /* 1113 * Get a local virtual address for the mappings that are being 1114 * worked with. 1115 */ 1116 ptbase = get_ptbase(pmap); 1117 1118 sindex = i386_btop(sva); 1119 eindex = i386_btop(eva); 1120 1121 for (; sindex < eindex; sindex = pdnxt) { 1122 1123 /* 1124 * Calculate index for next page table. 1125 */ 1126 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1)); 1127 ptpaddr = (vm_offset_t) *pmap_pde(pmap, i386_ptob(sindex)); 1128 1129 /* 1130 * Weed out invalid mappings. Note: we assume that the page 1131 * directory table is always allocated, and in kernel virtual. 1132 */ 1133 if (ptpaddr == 0) 1134 continue; 1135 1136 if (sindex < i386_btop(UPT_MIN_ADDRESS)) { 1137 /* 1138 * get the vm_page_t for the page table page 1139 */ 1140 mpte = PHYS_TO_VM_PAGE(ptpaddr); 1141 1142 /* 1143 * if the pte isn't wired, just skip it. 1144 */ 1145 if (mpte->wire_count == 0) 1146 continue; 1147 } 1148 1149 /* 1150 * Limit our scan to either the end of the va represented 1151 * by the current page table page, or to the end of the 1152 * range being removed. 1153 */ 1154 if (pdnxt > eindex) { 1155 pdnxt = eindex; 1156 } 1157 1158 for ( ;sindex != pdnxt; sindex++) { 1159 vm_offset_t va; 1160 if (ptbase[sindex] == 0) { 1161 continue; 1162 } 1163 va = i386_ptob(sindex); 1164 anyvalid = 1; 1165 if (pmap_remove_pte(pmap, 1166 ptbase + sindex, va)) 1167 break; 1168 } 1169 } 1170 1171 if (anyvalid) { 1172 pmap_update(); 1173 } 1174} 1175 1176 1177void 1178pmap_remove_pte_mapping(pa) 1179 vm_offset_t pa; 1180{ 1181 register pv_entry_t pv, *ppv, npv; 1182 register unsigned *pte; 1183 vm_offset_t va; 1184 int anyvalid = 0; 1185 1186 ppv = pa_to_pvh(pa); 1187 1188 for (pv = *ppv; pv; pv=pv->pv_next) { 1189 unsigned tpte; 1190 struct pmap *pmap; 1191 1192 anyvalid = 1; 1193 pmap = pv->pv_pmap; 1194 pte = get_ptbase(pmap) + i386_btop(pv->pv_va); 1195 if (tpte = *pte) { 1196 pmap->pm_stats.resident_count--; 1197 *pte = 0; 1198 if (tpte & PG_W) 1199 pmap->pm_stats.wired_count--; 1200 } 1201 } 1202 1203 if (anyvalid) { 1204 for (pv = *ppv; pv; pv = npv) { 1205 npv = pv->pv_next; 1206 free_pv_entry(pv); 1207 } 1208 *ppv = NULL; 1209 } 1210} 1211 1212/* 1213 * Routine: pmap_remove_all 1214 * Function: 1215 * Removes this physical page from 1216 * all physical maps in which it resides. 1217 * Reflects back modify bits to the pager. 1218 * 1219 * Notes: 1220 * Original versions of this routine were very 1221 * inefficient because they iteratively called 1222 * pmap_remove (slow...) 1223 */ 1224static __inline void 1225pmap_remove_all(pa) 1226 vm_offset_t pa; 1227{ 1228 register pv_entry_t pv, *ppv, npv; 1229 register unsigned *pte, *ptbase; 1230 vm_offset_t va; 1231 vm_page_t m; 1232 int s; 1233 1234#if defined(PMAP_DIAGNOSTIC) 1235 /* 1236 * XXX this makes pmap_page_protect(NONE) illegal for non-managed 1237 * pages! 1238 */ 1239 if (!pmap_is_managed(pa)) { 1240 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%lx", pa); 1241 } 1242#endif 1243 1244 m = PHYS_TO_VM_PAGE(pa); 1245 ppv = pa_to_pvh(pa); 1246 1247 s = splvm(); 1248 for (pv = *ppv; pv; pv=pv->pv_next) { 1249 int tpte; 1250 struct pmap *pmap; 1251 1252 pmap = pv->pv_pmap; 1253 ptbase = get_ptbase(pmap); 1254 va = pv->pv_va; 1255 if (*pmap_pde(pmap, va) == 0) 1256 continue; 1257 pte = ptbase + i386_btop(va); 1258 if (tpte = ((int) *pte)) { 1259 pmap->pm_stats.resident_count--; 1260 *pte = 0; 1261 if (tpte & PG_W) 1262 pmap->pm_stats.wired_count--; 1263 /* 1264 * Update the vm_page_t clean and reference bits. 1265 */ 1266 if (tpte & PG_M) { 1267#if defined(PMAP_DIAGNOSTIC) 1268 if (pmap_nw_modified((pt_entry_t) tpte)) { 1269 printf("pmap_remove_all: modified page not writable: va: 0x%lx, pte: 0x%lx\n", va, tpte); 1270 } 1271#endif 1272 if ((va >= UPT_MIN_ADDRESS) && 1273 (va < UPT_MAX_ADDRESS)) 1274 continue; 1275 1276 if (va < clean_sva || va >= clean_eva) { 1277 m->dirty = VM_PAGE_BITS_ALL; 1278 } 1279 } 1280 } 1281 } 1282 1283 for (pv = *ppv; pv; pv = npv) { 1284 npv = pv->pv_next; 1285 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem); 1286 free_pv_entry(pv); 1287 } 1288 *ppv = NULL; 1289 1290 splx(s); 1291} 1292 1293/* 1294 * Set the physical protection on the 1295 * specified range of this map as requested. 1296 */ 1297void 1298pmap_protect(pmap, sva, eva, prot) 1299 register pmap_t pmap; 1300 vm_offset_t sva, eva; 1301 vm_prot_t prot; 1302{ 1303 register unsigned *pte; 1304 register vm_offset_t va; 1305 register unsigned *ptbase; 1306 vm_offset_t pdnxt; 1307 vm_offset_t ptpaddr; 1308 vm_offset_t sindex, eindex; 1309 vm_page_t mpte; 1310 int anyvalid; 1311 1312 1313 if (pmap == NULL) 1314 return; 1315 1316 if ((prot & VM_PROT_READ) == VM_PROT_NONE) { 1317 pmap_remove(pmap, sva, eva); 1318 return; 1319 } 1320 if (prot & VM_PROT_WRITE) 1321 return; 1322 1323 anyvalid = 0; 1324 1325 ptbase = get_ptbase(pmap); 1326 1327 sindex = i386_btop(sva); 1328 eindex = i386_btop(eva); 1329 1330 for (; sindex < eindex; sindex = pdnxt) { 1331 1332 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1)); 1333 ptpaddr = (vm_offset_t) *pmap_pde(pmap, i386_ptob(sindex)); 1334 1335 /* 1336 * Weed out invalid mappings. Note: we assume that the page 1337 * directory table is always allocated, and in kernel virtual. 1338 */ 1339 if (ptpaddr == 0) 1340 continue; 1341 1342 /* 1343 * Don't look at kernel page table pages 1344 */ 1345 if (sindex < i386_btop(UPT_MIN_ADDRESS)) { 1346 mpte = PHYS_TO_VM_PAGE(ptpaddr); 1347 1348 if (mpte->wire_count == 0) 1349 continue; 1350 } 1351 1352 if (pdnxt > eindex) { 1353 pdnxt = eindex; 1354 } 1355 1356 for (; sindex != pdnxt; sindex++) { 1357 1358 unsigned pbits = ptbase[sindex]; 1359 1360 if (pbits & PG_RW) { 1361 if (pbits & PG_M) { 1362 vm_page_t m = PHYS_TO_VM_PAGE(pbits); 1363 m->dirty = VM_PAGE_BITS_ALL; 1364 } 1365 ptbase[sindex] = pbits & ~(PG_M|PG_RW); 1366 anyvalid = 1; 1367 } 1368 } 1369 } 1370 if (anyvalid) 1371 pmap_update(); 1372} 1373 1374/* 1375 * Create a pv entry for page at pa for 1376 * (pmap, va). 1377 */ 1378static __inline void 1379pmap_insert_entry(pmap, va, mpte, pa) 1380 pmap_t pmap; 1381 vm_offset_t va; 1382 vm_page_t mpte; 1383 vm_offset_t pa; 1384{ 1385 1386 int s; 1387 pv_entry_t *ppv, pv; 1388 1389 s = splvm(); 1390 pv = get_pv_entry(); 1391 pv->pv_va = va; 1392 pv->pv_pmap = pmap; 1393 pv->pv_ptem = mpte; 1394 1395 ppv = pa_to_pvh(pa); 1396 if (*ppv) 1397 pv->pv_next = *ppv; 1398 else 1399 pv->pv_next = NULL; 1400 *ppv = pv; 1401 splx(s); 1402} 1403 1404/* 1405 * this routine is called if the page table page is not 1406 * mapped correctly. 1407 */ 1408static vm_page_t 1409_pmap_allocpte(pmap, va, ptepindex) 1410 pmap_t pmap; 1411 vm_offset_t va; 1412 int ptepindex; 1413{ 1414 vm_offset_t pteva, ptepa; 1415 vm_page_t m; 1416 int s; 1417 1418 /* 1419 * Find or fabricate a new pagetable page 1420 */ 1421retry: 1422 m = vm_page_lookup(pmap->pm_pteobj, ptepindex); 1423 if (m == NULL) { 1424 m = vm_page_alloc(pmap->pm_pteobj, ptepindex, VM_ALLOC_ZERO); 1425 if (m == NULL) { 1426 VM_WAIT; 1427 goto retry; 1428 } 1429 if ((m->flags & PG_ZERO) == 0) 1430 pmap_zero_page(VM_PAGE_TO_PHYS(m)); 1431 m->flags &= ~(PG_ZERO|PG_BUSY); 1432 m->valid = VM_PAGE_BITS_ALL; 1433 } else { 1434 if ((m->flags & PG_BUSY) || m->busy) { 1435 m->flags |= PG_WANTED; 1436 tsleep(m, PVM, "ptewai", 0); 1437 goto retry; 1438 } 1439 } 1440 1441 /* 1442 * mark the object writeable 1443 */ 1444 pmap->pm_pteobj->flags |= OBJ_WRITEABLE; 1445 1446 if (m->hold_count == 0) { 1447 s = splvm(); 1448 vm_page_unqueue(m); 1449 splx(s); 1450 ++m->wire_count; 1451 ++cnt.v_wire_count; 1452 } 1453 1454 /* 1455 * Increment the hold count for the page table page 1456 * (denoting a new mapping.) 1457 */ 1458 ++m->hold_count; 1459 1460 /* 1461 * Map the pagetable page into the process address space, if 1462 * it isn't already there. 1463 */ 1464 pteva = ((vm_offset_t) vtopte(va)) & PG_FRAME; 1465 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex]; 1466 if (ptepa == 0) { 1467 pv_entry_t pv, *ppv; 1468 1469 pmap->pm_stats.resident_count++; 1470 1471 s = splvm(); 1472 pv = get_pv_entry(); 1473 1474 pv->pv_va = pteva; 1475 pv->pv_pmap = pmap; 1476 pv->pv_next = NULL; 1477 pv->pv_ptem = NULL; 1478 1479 ptepa = VM_PAGE_TO_PHYS(m); 1480 ppv = pa_to_pvh(ptepa); 1481#if defined(PMAP_DIAGNOSTIC) 1482 if (*ppv) 1483 panic("pmap_allocpte: page is already mapped"); 1484#endif 1485 *ppv = pv; 1486 splx(s); 1487 pmap_update_1pg(pteva); 1488 } else { 1489#if defined(PMAP_DIAGNOSTIC) 1490 if (VM_PAGE_TO_PHYS(m) != (ptepa & PG_FRAME)) 1491 panic("pmap_allocpte: mismatch"); 1492#endif 1493 } 1494 pmap->pm_pdir[ptepindex] = 1495 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_MANAGED); 1496 m->flags |= PG_MAPPED; 1497 return m; 1498} 1499 1500static __inline vm_page_t 1501pmap_allocpte(pmap, va) 1502 pmap_t pmap; 1503 vm_offset_t va; 1504{ 1505 int ptepindex; 1506 vm_offset_t ptepa; 1507 vm_page_t m; 1508 1509 /* 1510 * Calculate pagetable page index 1511 */ 1512 ptepindex = va >> PDRSHIFT; 1513 1514 /* 1515 * Get the page directory entry 1516 */ 1517 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex]; 1518 1519 /* 1520 * If the page table page is mapped, we just increment the 1521 * hold count, and activate it. 1522 */ 1523 if ((ptepa & (PG_RW|PG_U|PG_V)) == (PG_RW|PG_U|PG_V)) { 1524 m = PHYS_TO_VM_PAGE(ptepa); 1525 if (m->hold_count == 0) { 1526 int s = splvm(); 1527 vm_page_unqueue(m); 1528 splx(s); 1529 ++m->wire_count; 1530 ++cnt.v_wire_count; 1531 } 1532 ++m->hold_count; 1533 return m; 1534 } 1535 return _pmap_allocpte(pmap, va, ptepindex); 1536} 1537 1538/* 1539 * Insert the given physical page (p) at 1540 * the specified virtual address (v) in the 1541 * target physical map with the protection requested. 1542 * 1543 * If specified, the page will be wired down, meaning 1544 * that the related pte can not be reclaimed. 1545 * 1546 * NB: This is the only routine which MAY NOT lazy-evaluate 1547 * or lose information. That is, this routine must actually 1548 * insert this page into the given map NOW. 1549 */ 1550void 1551pmap_enter(pmap, va, pa, prot, wired) 1552 register pmap_t pmap; 1553 vm_offset_t va; 1554 register vm_offset_t pa; 1555 vm_prot_t prot; 1556 boolean_t wired; 1557{ 1558 register unsigned *pte; 1559 vm_offset_t opa; 1560 vm_offset_t origpte, newpte; 1561 vm_page_t mpte; 1562 1563 if (pmap == NULL) 1564 return; 1565 1566 va &= PG_FRAME; 1567 if (va > VM_MAX_KERNEL_ADDRESS) 1568 panic("pmap_enter: toobig"); 1569 1570 mpte = NULL; 1571 /* 1572 * In the case that a page table page is not 1573 * resident, we are creating it here. 1574 */ 1575 if (va < UPT_MIN_ADDRESS) 1576 mpte = pmap_allocpte(pmap, va); 1577 1578 pte = pmap_pte(pmap, va); 1579 /* 1580 * Page Directory table entry not valid, we need a new PT page 1581 */ 1582 if (pte == NULL) { 1583 printf("kernel page directory invalid pdir=%p, va=0x%lx\n", 1584 pmap->pm_pdir[PTDPTDI], va); 1585 panic("invalid kernel page directory"); 1586 } 1587 1588 origpte = *(vm_offset_t *)pte; 1589 pa &= PG_FRAME; 1590 opa = origpte & PG_FRAME; 1591 1592 /* 1593 * Mapping has not changed, must be protection or wiring change. 1594 */ 1595 if (opa == pa) { 1596 /* 1597 * Wiring change, just update stats. We don't worry about 1598 * wiring PT pages as they remain resident as long as there 1599 * are valid mappings in them. Hence, if a user page is wired, 1600 * the PT page will be also. 1601 */ 1602 if (wired && ((origpte & PG_W) == 0)) 1603 pmap->pm_stats.wired_count++; 1604 else if (!wired && (origpte & PG_W)) 1605 pmap->pm_stats.wired_count--; 1606 1607#if defined(PMAP_DIAGNOSTIC) 1608 if (pmap_nw_modified((pt_entry_t) origpte)) { 1609 printf("pmap_enter: modified page not writable: va: 0x%lx, pte: 0x%lx\n", va, origpte); 1610 } 1611#endif 1612 1613 /* 1614 * We might be turning off write access to the page, 1615 * so we go ahead and sense modify status. 1616 */ 1617 if (origpte & PG_MANAGED) { 1618 vm_page_t m; 1619 if (origpte & PG_M) { 1620 m = PHYS_TO_VM_PAGE(pa); 1621 m->dirty = VM_PAGE_BITS_ALL; 1622 } 1623 pa |= PG_MANAGED; 1624 } 1625 1626 if (mpte) 1627 --mpte->hold_count; 1628 1629 goto validate; 1630 } 1631 /* 1632 * Mapping has changed, invalidate old range and fall through to 1633 * handle validating new mapping. 1634 */ 1635 if (opa) 1636 (void) pmap_remove_pte(pmap, pte, va); 1637 1638 /* 1639 * Enter on the PV list if part of our managed memory Note that we 1640 * raise IPL while manipulating pv_table since pmap_enter can be 1641 * called at interrupt time. 1642 */ 1643 if (pmap_is_managed(pa)) { 1644 pmap_insert_entry(pmap, va, mpte, pa); 1645 pa |= PG_MANAGED; 1646 } 1647 1648 /* 1649 * Increment counters 1650 */ 1651 pmap->pm_stats.resident_count++; 1652 if (wired) 1653 pmap->pm_stats.wired_count++; 1654 1655validate: 1656 /* 1657 * Now validate mapping with desired protection/wiring. 1658 */ 1659 newpte = (vm_offset_t) (pa | pte_prot(pmap, prot) | PG_V); 1660 1661 if (wired) 1662 newpte |= PG_W; 1663 if (va < UPT_MIN_ADDRESS) 1664 newpte |= PG_U; 1665 1666 /* 1667 * if the mapping or permission bits are different, we need 1668 * to update the pte. 1669 */ 1670 if ((origpte & ~(PG_M|PG_A)) != newpte) { 1671 *pte = newpte; 1672 if (origpte) 1673 pmap_update_1pg(va); 1674 } 1675} 1676 1677/* 1678 * this code makes some *MAJOR* assumptions: 1679 * 1. Current pmap & pmap exists. 1680 * 2. Not wired. 1681 * 3. Read access. 1682 * 4. No page table pages. 1683 * 5. Tlbflush is deferred to calling procedure. 1684 * 6. Page IS managed. 1685 * but is *MUCH* faster than pmap_enter... 1686 */ 1687 1688static void 1689pmap_enter_quick(pmap, va, pa) 1690 register pmap_t pmap; 1691 vm_offset_t va; 1692 register vm_offset_t pa; 1693{ 1694 register unsigned *pte; 1695 vm_page_t mpte; 1696 1697 mpte = NULL; 1698 /* 1699 * In the case that a page table page is not 1700 * resident, we are creating it here. 1701 */ 1702 if (va < UPT_MIN_ADDRESS) 1703 mpte = pmap_allocpte(pmap, va); 1704 1705 pte = (unsigned *)vtopte(va); 1706 if (*pte) 1707 (void) pmap_remove_pte(pmap, pte, va); 1708 1709 /* 1710 * Enter on the PV list if part of our managed memory Note that we 1711 * raise IPL while manipulating pv_table since pmap_enter can be 1712 * called at interrupt time. 1713 */ 1714 pmap_insert_entry(pmap, va, mpte, pa); 1715 1716 /* 1717 * Increment counters 1718 */ 1719 pmap->pm_stats.resident_count++; 1720 1721 /* 1722 * Now validate mapping with RO protection 1723 */ 1724 *pte = pa | PG_V | PG_U | PG_MANAGED; 1725 1726 return; 1727} 1728 1729#define MAX_INIT_PT (96) 1730/* 1731 * pmap_object_init_pt preloads the ptes for a given object 1732 * into the specified pmap. This eliminates the blast of soft 1733 * faults on process startup and immediately after an mmap. 1734 */ 1735void 1736pmap_object_init_pt(pmap, addr, object, pindex, size, limit) 1737 pmap_t pmap; 1738 vm_offset_t addr; 1739 vm_object_t object; 1740 vm_pindex_t pindex; 1741 vm_size_t size; 1742 int limit; 1743{ 1744 vm_offset_t tmpidx; 1745 int psize; 1746 vm_page_t p; 1747 int objpgs; 1748 1749 psize = (size >> PAGE_SHIFT); 1750 1751 if (!pmap || (object->type != OBJT_VNODE) || 1752 (limit && (psize > MAX_INIT_PT) && 1753 (object->resident_page_count > MAX_INIT_PT))) { 1754 return; 1755 } 1756 1757 /* 1758 * if we are processing a major portion of the object, then scan the 1759 * entire thing. 1760 */ 1761 if (psize > (object->size >> 2)) { 1762 objpgs = psize; 1763 1764 for (p = TAILQ_FIRST(&object->memq); 1765 ((objpgs > 0) && (p != NULL)); 1766 p = TAILQ_NEXT(p, listq)) { 1767 1768 tmpidx = p->pindex; 1769 if (tmpidx < pindex) { 1770 continue; 1771 } 1772 tmpidx -= pindex; 1773 if (tmpidx >= psize) { 1774 continue; 1775 } 1776 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) && 1777 (p->busy == 0) && 1778 (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) { 1779 if (p->queue == PQ_CACHE) 1780 vm_page_deactivate(p); 1781 p->flags |= PG_BUSY; 1782 pmap_enter_quick(pmap, 1783 addr + (tmpidx << PAGE_SHIFT), 1784 VM_PAGE_TO_PHYS(p)); 1785 p->flags |= PG_MAPPED; 1786 PAGE_WAKEUP(p); 1787 } 1788 objpgs -= 1; 1789 } 1790 } else { 1791 /* 1792 * else lookup the pages one-by-one. 1793 */ 1794 for (tmpidx = 0; tmpidx < psize; tmpidx += 1) { 1795 p = vm_page_lookup(object, tmpidx + pindex); 1796 if (p && 1797 ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) && 1798 (p->busy == 0) && 1799 (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) { 1800 p->flags |= PG_BUSY; 1801 pmap_enter_quick(pmap, 1802 addr + (tmpidx << PAGE_SHIFT), 1803 VM_PAGE_TO_PHYS(p)); 1804 p->flags |= PG_MAPPED; 1805 PAGE_WAKEUP(p); 1806 } 1807 } 1808 } 1809 return; 1810} 1811 1812/* 1813 * pmap_prefault provides a quick way of clustering 1814 * pagefaults into a processes address space. It is a "cousin" 1815 * of pmap_object_init_pt, except it runs at page fault time instead 1816 * of mmap time. 1817 */ 1818#define PFBAK 2 1819#define PFFOR 2 1820#define PAGEORDER_SIZE (PFBAK+PFFOR) 1821 1822static int pmap_prefault_pageorder[] = { 1823 -PAGE_SIZE, PAGE_SIZE, -2 * PAGE_SIZE, 2 * PAGE_SIZE 1824}; 1825 1826void 1827pmap_prefault(pmap, addra, entry, object) 1828 pmap_t pmap; 1829 vm_offset_t addra; 1830 vm_map_entry_t entry; 1831 vm_object_t object; 1832{ 1833 int i; 1834 vm_offset_t starta; 1835 vm_offset_t addr; 1836 vm_pindex_t pindex; 1837 vm_page_t m; 1838 1839 if (entry->object.vm_object != object) 1840 return; 1841 1842 if (!curproc || (pmap != &curproc->p_vmspace->vm_pmap)) 1843 return; 1844 1845 starta = addra - PFBAK * PAGE_SIZE; 1846 if (starta < entry->start) { 1847 starta = entry->start; 1848 } else if (starta > addra) { 1849 starta = 0; 1850 } 1851 1852 for (i = 0; i < PAGEORDER_SIZE; i++) { 1853 vm_object_t lobject; 1854 unsigned *pte; 1855 1856 addr = addra + pmap_prefault_pageorder[i]; 1857 if (addr < starta || addr >= entry->end) 1858 continue; 1859 1860 if ((*pmap_pde(pmap, addr)) == NULL) 1861 continue; 1862 1863 pte = (unsigned *) vtopte(addr); 1864 if (*pte) 1865 continue; 1866 1867 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT; 1868 lobject = object; 1869 for (m = vm_page_lookup(lobject, pindex); 1870 (!m && (lobject->type == OBJT_DEFAULT) && (lobject->backing_object)); 1871 lobject = lobject->backing_object) { 1872 if (lobject->backing_object_offset & PAGE_MASK) 1873 break; 1874 pindex += (lobject->backing_object_offset >> PAGE_SHIFT); 1875 m = vm_page_lookup(lobject->backing_object, pindex); 1876 } 1877 1878 /* 1879 * give-up when a page is not in memory 1880 */ 1881 if (m == NULL) 1882 break; 1883 1884 if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) && 1885 (m->busy == 0) && 1886 (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) { 1887 1888 if (m->queue == PQ_CACHE) { 1889 vm_page_deactivate(m); 1890 } 1891 m->flags |= PG_BUSY; 1892 pmap_enter_quick(pmap, addr, VM_PAGE_TO_PHYS(m)); 1893 m->flags |= PG_MAPPED; 1894 PAGE_WAKEUP(m); 1895 } 1896 } 1897} 1898 1899/* 1900 * Routine: pmap_change_wiring 1901 * Function: Change the wiring attribute for a map/virtual-address 1902 * pair. 1903 * In/out conditions: 1904 * The mapping must already exist in the pmap. 1905 */ 1906void 1907pmap_change_wiring(pmap, va, wired) 1908 register pmap_t pmap; 1909 vm_offset_t va; 1910 boolean_t wired; 1911{ 1912 register unsigned *pte; 1913 1914 if (pmap == NULL) 1915 return; 1916 1917 pte = pmap_pte(pmap, va); 1918 1919 if (wired && !pmap_pte_w(pte)) 1920 pmap->pm_stats.wired_count++; 1921 else if (!wired && pmap_pte_w(pte)) 1922 pmap->pm_stats.wired_count--; 1923 1924 /* 1925 * Wiring is not a hardware characteristic so there is no need to 1926 * invalidate TLB. 1927 */ 1928 pmap_pte_set_w(pte, wired); 1929} 1930 1931 1932 1933/* 1934 * Copy the range specified by src_addr/len 1935 * from the source map to the range dst_addr/len 1936 * in the destination map. 1937 * 1938 * This routine is only advisory and need not do anything. 1939 */ 1940void 1941pmap_copy(dst_pmap, src_pmap, dst_addr, len, src_addr) 1942 pmap_t dst_pmap, src_pmap; 1943 vm_offset_t dst_addr; 1944 vm_size_t len; 1945 vm_offset_t src_addr; 1946{ 1947 vm_offset_t addr; 1948 vm_offset_t end_addr = src_addr + len; 1949 vm_offset_t pdnxt; 1950 unsigned src_frame, dst_frame; 1951 1952 if (dst_addr != src_addr) 1953 return; 1954 1955 src_frame = ((unsigned) src_pmap->pm_pdir[PTDPTDI]) & PG_FRAME; 1956 if (src_frame != (((unsigned) PTDpde) & PG_FRAME)) 1957 return; 1958 1959 dst_frame = ((unsigned) dst_pmap->pm_pdir[PTDPTDI]) & PG_FRAME; 1960 if (dst_frame != (((unsigned) APTDpde) & PG_FRAME)) { 1961 APTDpde = (pd_entry_t) (dst_frame | PG_RW | PG_V); 1962 pmap_update(); 1963 } 1964 1965 for(addr = src_addr; addr < end_addr; addr = pdnxt) { 1966 unsigned *src_pte, *dst_pte; 1967 vm_page_t dstmpte, srcmpte; 1968 vm_offset_t srcptepaddr; 1969 1970 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1)); 1971 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[addr >> PDRSHIFT]; 1972 if (srcptepaddr == 0) { 1973 continue; 1974 } 1975 1976 srcmpte = PHYS_TO_VM_PAGE(srcptepaddr); 1977 if (srcmpte->hold_count == 0) 1978 continue; 1979 1980 if (pdnxt > end_addr) 1981 pdnxt = end_addr; 1982 1983 src_pte = (unsigned *) vtopte(addr); 1984 dst_pte = (unsigned *) avtopte(addr); 1985 while (addr < pdnxt) { 1986 unsigned ptetemp; 1987 ptetemp = *src_pte; 1988 /* 1989 * we only virtual copy managed pages 1990 */ 1991 if ((ptetemp & PG_MANAGED) != 0) { 1992 /* 1993 * We have to check after allocpte for the 1994 * pte still being around... allocpte can 1995 * block. 1996 */ 1997 dstmpte = pmap_allocpte(dst_pmap, addr); 1998 if (ptetemp = *src_pte) { 1999 /* 2000 * Simply clear the modified and accessed (referenced) 2001 * bits. 2002 */ 2003 *dst_pte = ptetemp & ~(PG_M|PG_A); 2004 dst_pmap->pm_stats.resident_count++; 2005 pmap_insert_entry(dst_pmap, addr, dstmpte, 2006 (ptetemp & PG_FRAME)); 2007 } else { 2008 pmap_unwire_pte_hold(dstmpte); 2009 } 2010 if (dstmpte->hold_count >= srcmpte->hold_count) 2011 break; 2012 } 2013 addr += PAGE_SIZE; 2014 ++src_pte; 2015 ++dst_pte; 2016 } 2017 } 2018} 2019 2020/* 2021 * Routine: pmap_kernel 2022 * Function: 2023 * Returns the physical map handle for the kernel. 2024 */ 2025pmap_t 2026pmap_kernel() 2027{ 2028 return (kernel_pmap); 2029} 2030 2031/* 2032 * pmap_zero_page zeros the specified (machine independent) 2033 * page by mapping the page into virtual memory and using 2034 * bzero to clear its contents, one machine dependent page 2035 * at a time. 2036 */ 2037void 2038pmap_zero_page(phys) 2039 vm_offset_t phys; 2040{ 2041 if (*(int *) CMAP2) 2042 panic("pmap_zero_page: CMAP busy"); 2043 2044 *(int *) CMAP2 = PG_V | PG_RW | (phys & PG_FRAME); 2045 bzero(CADDR2, PAGE_SIZE); 2046 *(int *) CMAP2 = 0; 2047 pmap_update_1pg((vm_offset_t) CADDR2); 2048} 2049 2050/* 2051 * pmap_copy_page copies the specified (machine independent) 2052 * page by mapping the page into virtual memory and using 2053 * bcopy to copy the page, one machine dependent page at a 2054 * time. 2055 */ 2056void 2057pmap_copy_page(src, dst) 2058 vm_offset_t src; 2059 vm_offset_t dst; 2060{ 2061 if (*(int *) CMAP1 || *(int *) CMAP2) 2062 panic("pmap_copy_page: CMAP busy"); 2063 2064 *(int *) CMAP1 = PG_V | PG_RW | (src & PG_FRAME); 2065 *(int *) CMAP2 = PG_V | PG_RW | (dst & PG_FRAME); 2066 2067#if __GNUC__ > 1 2068 memcpy(CADDR2, CADDR1, PAGE_SIZE); 2069#else 2070 bcopy(CADDR1, CADDR2, PAGE_SIZE); 2071#endif 2072 *(int *) CMAP1 = 0; 2073 *(int *) CMAP2 = 0; 2074 pmap_update_2pg( (vm_offset_t) CADDR1, (vm_offset_t) CADDR2); 2075} 2076 2077 2078/* 2079 * Routine: pmap_pageable 2080 * Function: 2081 * Make the specified pages (by pmap, offset) 2082 * pageable (or not) as requested. 2083 * 2084 * A page which is not pageable may not take 2085 * a fault; therefore, its page table entry 2086 * must remain valid for the duration. 2087 * 2088 * This routine is merely advisory; pmap_enter 2089 * will specify that these pages are to be wired 2090 * down (or not) as appropriate. 2091 */ 2092void 2093pmap_pageable(pmap, sva, eva, pageable) 2094 pmap_t pmap; 2095 vm_offset_t sva, eva; 2096 boolean_t pageable; 2097{ 2098} 2099 2100/* 2101 * this routine returns true if a physical page resides 2102 * in the given pmap. 2103 */ 2104boolean_t 2105pmap_page_exists(pmap, pa) 2106 pmap_t pmap; 2107 vm_offset_t pa; 2108{ 2109 register pv_entry_t *ppv, pv; 2110 int s; 2111 2112 if (!pmap_is_managed(pa)) 2113 return FALSE; 2114 2115 s = splvm(); 2116 2117 ppv = pa_to_pvh(pa); 2118 /* 2119 * Not found, check current mappings returning immediately if found. 2120 */ 2121 for (pv = *ppv; pv; pv = pv->pv_next) { 2122 if (pv->pv_pmap == pmap) { 2123 splx(s); 2124 return TRUE; 2125 } 2126 } 2127 splx(s); 2128 return (FALSE); 2129} 2130 2131/* 2132 * pmap_testbit tests bits in pte's 2133 * note that the testbit/changebit routines are inline, 2134 * and a lot of things compile-time evaluate. 2135 */ 2136static __inline boolean_t 2137pmap_testbit(pa, bit) 2138 register vm_offset_t pa; 2139 int bit; 2140{ 2141 register pv_entry_t *ppv, pv; 2142 unsigned *pte; 2143 int s; 2144 2145 if (!pmap_is_managed(pa)) 2146 return FALSE; 2147 2148 s = splvm(); 2149 2150 ppv = pa_to_pvh(pa); 2151 /* 2152 * Not found, check current mappings returning immediately if found. 2153 */ 2154 for (pv = *ppv ;pv; pv = pv->pv_next) { 2155 /* 2156 * if the bit being tested is the modified bit, then 2157 * mark UPAGES as always modified, and ptes as never 2158 * modified. 2159 */ 2160 if (bit & (PG_A|PG_M)) { 2161 if ((pv->pv_va >= UPT_MIN_ADDRESS) && 2162 (pv->pv_va < UPT_MAX_ADDRESS)) { 2163 continue; 2164 } 2165 if ((pv->pv_va >= clean_sva) && 2166 (pv->pv_va < clean_eva)) { 2167 continue; 2168 } 2169 } 2170 if (!pv->pv_pmap) { 2171#if defined(PMAP_DIAGNOSTIC) 2172 printf("Null pmap (tb) at va: 0x%lx\n", pv->pv_va); 2173#endif 2174 continue; 2175 } 2176 pte = pmap_pte(pv->pv_pmap, pv->pv_va); 2177 if (pte == NULL) 2178 continue; 2179 if ((int) *pte & bit) { 2180 splx(s); 2181 return TRUE; 2182 } 2183 } 2184 splx(s); 2185 return (FALSE); 2186} 2187 2188/* 2189 * this routine is used to modify bits in ptes 2190 */ 2191static __inline void 2192pmap_changebit(pa, bit, setem) 2193 vm_offset_t pa; 2194 int bit; 2195 boolean_t setem; 2196{ 2197 register pv_entry_t pv, *ppv; 2198 register unsigned *pte; 2199 vm_offset_t va; 2200 int changed; 2201 int s; 2202 2203 if (!pmap_is_managed(pa)) 2204 return; 2205 2206 s = splvm(); 2207 2208 changed = 0; 2209 ppv = pa_to_pvh(pa); 2210 /* 2211 * Loop over all current mappings setting/clearing as appropos If 2212 * setting RO do we need to clear the VAC? 2213 */ 2214 for ( pv = *ppv; pv; pv = pv->pv_next) { 2215 va = pv->pv_va; 2216 2217 /* 2218 * don't write protect pager mappings 2219 */ 2220 if (!setem && (bit == PG_RW)) { 2221 if (va >= clean_sva && va < clean_eva) 2222 continue; 2223 } 2224 if (!pv->pv_pmap) { 2225#if defined(PMAP_DIAGNOSTIC) 2226 printf("Null pmap (cb) at va: 0x%lx\n", va); 2227#endif 2228 continue; 2229 } 2230 2231 pte = pmap_pte(pv->pv_pmap, va); 2232 if (pte == NULL) 2233 continue; 2234 if (setem) { 2235 *(int *)pte |= bit; 2236 changed = 1; 2237 } else { 2238 vm_offset_t pbits = *(vm_offset_t *)pte; 2239 if (pbits & bit) 2240 changed = 1; 2241 if (bit == PG_RW) { 2242 if (pbits & PG_M) { 2243 vm_page_t m; 2244 vm_offset_t pa = pbits & PG_FRAME; 2245 m = PHYS_TO_VM_PAGE(pa); 2246 m->dirty = VM_PAGE_BITS_ALL; 2247 } 2248 *(int *)pte = pbits & ~(PG_M|PG_RW); 2249 } else { 2250 *(int *)pte = pbits & ~bit; 2251 } 2252 } 2253 } 2254 splx(s); 2255 if (changed) 2256 pmap_update(); 2257} 2258 2259/* 2260 * pmap_page_protect: 2261 * 2262 * Lower the permission for all mappings to a given page. 2263 */ 2264void 2265pmap_page_protect(phys, prot) 2266 vm_offset_t phys; 2267 vm_prot_t prot; 2268{ 2269 if ((prot & VM_PROT_WRITE) == 0) { 2270 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) 2271 pmap_changebit(phys, PG_RW, FALSE); 2272 else { 2273 pmap_remove_all(phys); 2274 pmap_update(); 2275 } 2276 } 2277} 2278 2279vm_offset_t 2280pmap_phys_address(ppn) 2281 int ppn; 2282{ 2283 return (i386_ptob(ppn)); 2284} 2285 2286/* 2287 * pmap_is_referenced: 2288 * 2289 * Return whether or not the specified physical page was referenced 2290 * by any physical maps. 2291 */ 2292boolean_t 2293pmap_is_referenced(vm_offset_t pa) 2294{ 2295 return pmap_testbit((pa), PG_A); 2296} 2297 2298/* 2299 * pmap_is_modified: 2300 * 2301 * Return whether or not the specified physical page was modified 2302 * in any physical maps. 2303 */ 2304boolean_t 2305pmap_is_modified(vm_offset_t pa) 2306{ 2307 return pmap_testbit((pa), PG_M); 2308} 2309 2310/* 2311 * Clear the modify bits on the specified physical page. 2312 */ 2313void 2314pmap_clear_modify(vm_offset_t pa) 2315{ 2316 pmap_changebit((pa), PG_M, FALSE); 2317} 2318 2319/* 2320 * pmap_clear_reference: 2321 * 2322 * Clear the reference bit on the specified physical page. 2323 */ 2324void 2325pmap_clear_reference(vm_offset_t pa) 2326{ 2327 pmap_changebit((pa), PG_A, FALSE); 2328} 2329 2330/* 2331 * Miscellaneous support routines follow 2332 */ 2333 2334static void 2335i386_protection_init() 2336{ 2337 register int *kp, prot; 2338 2339 kp = protection_codes; 2340 for (prot = 0; prot < 8; prot++) { 2341 switch (prot) { 2342 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE: 2343 /* 2344 * Read access is also 0. There isn't any execute bit, 2345 * so just make it readable. 2346 */ 2347 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE: 2348 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE: 2349 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE: 2350 *kp++ = 0; 2351 break; 2352 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE: 2353 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE: 2354 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE: 2355 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE: 2356 *kp++ = PG_RW; 2357 break; 2358 } 2359 } 2360} 2361 2362/* 2363 * Map a set of physical memory pages into the kernel virtual 2364 * address space. Return a pointer to where it is mapped. This 2365 * routine is intended to be used for mapping device memory, 2366 * NOT real memory. The non-cacheable bits are set on each 2367 * mapped page. 2368 */ 2369void * 2370pmap_mapdev(pa, size) 2371 vm_offset_t pa; 2372 vm_size_t size; 2373{ 2374 vm_offset_t va, tmpva; 2375 unsigned *pte; 2376 2377 size = roundup(size, PAGE_SIZE); 2378 2379 va = kmem_alloc_pageable(kernel_map, size); 2380 if (!va) 2381 panic("pmap_mapdev: Couldn't alloc kernel virtual memory"); 2382 2383 pa = pa & PG_FRAME; 2384 for (tmpva = va; size > 0;) { 2385 pte = (unsigned *)vtopte(tmpva); 2386 *pte = pa | PG_RW | PG_V | PG_N; 2387 size -= PAGE_SIZE; 2388 tmpva += PAGE_SIZE; 2389 pa += PAGE_SIZE; 2390 } 2391 pmap_update(); 2392 2393 return ((void *) va); 2394} 2395 2396int 2397pmap_mincore(pmap, addr) 2398 pmap_t pmap; 2399 vm_offset_t addr; 2400{ 2401 2402 unsigned *ptep, pte; 2403 int val = 0; 2404 2405 ptep = pmap_pte(pmap, addr); 2406 if (ptep == 0) { 2407 return 0; 2408 } 2409 2410 if ((pte = *ptep)) { 2411 vm_offset_t pa; 2412 val = MINCORE_INCORE; 2413 pa = pte & PG_FRAME; 2414 2415 /* 2416 * Modified by us 2417 */ 2418 if (pte & PG_M) 2419 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER; 2420 /* 2421 * Modified by someone 2422 */ 2423 else if (PHYS_TO_VM_PAGE(pa)->dirty || 2424 pmap_is_modified(pa)) 2425 val |= MINCORE_MODIFIED_OTHER; 2426 /* 2427 * Referenced by us 2428 */ 2429 if (pte & PG_U) 2430 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER; 2431 2432 /* 2433 * Referenced by someone 2434 */ 2435 else if ((PHYS_TO_VM_PAGE(pa)->flags & PG_REFERENCED) || 2436 pmap_is_referenced(pa)) 2437 val |= MINCORE_REFERENCED_OTHER; 2438 } 2439 return val; 2440} 2441 2442#if defined(PMAP_DEBUG) 2443pmap_pid_dump(int pid) { 2444 pmap_t pmap; 2445 struct proc *p; 2446 int npte = 0; 2447 int index; 2448 for (p = allproc.lh_first; p != NULL; p = p->p_list.le_next) { 2449 if (p->p_pid != pid) 2450 continue; 2451 2452 if (p->p_vmspace) { 2453 int i,j; 2454 index = 0; 2455 pmap = &p->p_vmspace->vm_pmap; 2456 for(i=0;i<1024;i++) { 2457 pd_entry_t *pde; 2458 unsigned *pte; 2459 unsigned base = i << PDRSHIFT; 2460 2461 pde = &pmap->pm_pdir[i]; 2462 if (pde && pmap_pde_v(pde)) { 2463 for(j=0;j<1024;j++) { 2464 unsigned va = base + (j << PAGE_SHIFT); 2465 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) { 2466 if (index) { 2467 index = 0; 2468 printf("\n"); 2469 } 2470 return npte; 2471 } 2472 pte = pmap_pte( pmap, va); 2473 if (pte && pmap_pte_v(pte)) { 2474 vm_offset_t pa; 2475 vm_page_t m; 2476 pa = *(int *)pte; 2477 m = PHYS_TO_VM_PAGE((pa & PG_FRAME)); 2478 printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x", 2479 va, pa, m->hold_count, m->wire_count, m->flags); 2480 npte++; 2481 index++; 2482 if (index >= 2) { 2483 index = 0; 2484 printf("\n"); 2485 } else { 2486 printf(" "); 2487 } 2488 } 2489 } 2490 } 2491 } 2492 } 2493 } 2494 return npte; 2495} 2496#endif 2497 2498#if defined(DEBUG) 2499 2500static void pads __P((pmap_t pm)); 2501static void pmap_pvdump __P((vm_offset_t pa)); 2502 2503/* print address space of pmap*/ 2504static void 2505pads(pm) 2506 pmap_t pm; 2507{ 2508 unsigned va, i, j; 2509 unsigned *ptep; 2510 2511 if (pm == kernel_pmap) 2512 return; 2513 for (i = 0; i < 1024; i++) 2514 if (pm->pm_pdir[i]) 2515 for (j = 0; j < 1024; j++) { 2516 va = (i << PDRSHIFT) + (j << PAGE_SHIFT); 2517 if (pm == kernel_pmap && va < KERNBASE) 2518 continue; 2519 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS) 2520 continue; 2521 ptep = pmap_pte(pm, va); 2522 if (pmap_pte_v(ptep)) 2523 printf("%x:%x ", va, *(int *) ptep); 2524 }; 2525 2526} 2527 2528static void 2529pmap_pvdump(pa) 2530 vm_offset_t pa; 2531{ 2532 register pv_entry_t pv; 2533 2534 printf("pa %x", pa); 2535 for (pv = pa_to_pvh(pa); pv; pv = pv->pv_next) { 2536#ifdef used_to_be 2537 printf(" -> pmap %x, va %x, flags %x", 2538 pv->pv_pmap, pv->pv_va, pv->pv_flags); 2539#endif 2540 printf(" -> pmap %x, va %x", 2541 pv->pv_pmap, pv->pv_va); 2542 pads(pv->pv_pmap); 2543 } 2544 printf(" "); 2545} 2546#endif 2547