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