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