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