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