pmap.c revision 19621
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.130 1996/11/07 14:44:01 joerg 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#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 if (prot & VM_PROT_WRITE) { 1839 return; 1840 } 1841 1842 anychanged = 0; 1843 1844 ptbase = get_ptbase(pmap); 1845 1846 sindex = i386_btop(sva); 1847 eindex = i386_btop(eva); 1848 1849 for (; sindex < eindex; sindex = pdnxt) { 1850 1851 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1)); 1852 ptpaddr = (vm_offset_t) *pmap_pde(pmap, i386_ptob(sindex)); 1853 1854 /* 1855 * Weed out invalid mappings. Note: we assume that the page 1856 * directory table is always allocated, and in kernel virtual. 1857 */ 1858 if (ptpaddr == 0) 1859 continue; 1860 1861 if (pdnxt > eindex) { 1862 pdnxt = eindex; 1863 } 1864 1865 for (; sindex != pdnxt; sindex++) { 1866 1867 unsigned pbits = ptbase[sindex]; 1868 1869 if (pbits & PG_RW) { 1870 if (pbits & PG_M) { 1871 vm_offset_t sva = i386_ptob(sindex); 1872 if (pmap_track_modified(sva)) { 1873 vm_page_t m = PHYS_TO_VM_PAGE(pbits); 1874 m->dirty = VM_PAGE_BITS_ALL; 1875 } 1876 } 1877 ptbase[sindex] = pbits & ~(PG_M|PG_RW); 1878 anychanged = 1; 1879 } 1880 } 1881 } 1882 if (anychanged) 1883 invltlb(); 1884} 1885 1886/* 1887 * Insert the given physical page (p) at 1888 * the specified virtual address (v) in the 1889 * target physical map with the protection requested. 1890 * 1891 * If specified, the page will be wired down, meaning 1892 * that the related pte can not be reclaimed. 1893 * 1894 * NB: This is the only routine which MAY NOT lazy-evaluate 1895 * or lose information. That is, this routine must actually 1896 * insert this page into the given map NOW. 1897 */ 1898void 1899pmap_enter(pmap, va, pa, prot, wired) 1900 register pmap_t pmap; 1901 vm_offset_t va; 1902 register vm_offset_t pa; 1903 vm_prot_t prot; 1904 boolean_t wired; 1905{ 1906 register unsigned *pte; 1907 vm_offset_t opa; 1908 vm_offset_t origpte, newpte; 1909 vm_page_t mpte; 1910 1911 if (pmap == NULL) 1912 return; 1913 1914 va &= PG_FRAME; 1915#ifdef PMAP_DIAGNOSTIC 1916 if (va > VM_MAX_KERNEL_ADDRESS) 1917 panic("pmap_enter: toobig"); 1918 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS)) 1919 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%x)", va); 1920#endif 1921 1922 mpte = NULL; 1923 /* 1924 * In the case that a page table page is not 1925 * resident, we are creating it here. 1926 */ 1927 if (va < UPT_MIN_ADDRESS) 1928 mpte = pmap_allocpte(pmap, va); 1929 1930 pte = pmap_pte(pmap, va); 1931 /* 1932 * Page Directory table entry not valid, we need a new PT page 1933 */ 1934 if (pte == NULL) { 1935 panic("pmap_enter: invalid page directory, pdir=%p, va=0x%lx\n", 1936 pmap->pm_pdir[PTDPTDI], va); 1937 } 1938 1939 origpte = *(vm_offset_t *)pte; 1940 pa &= PG_FRAME; 1941 opa = origpte & PG_FRAME; 1942 1943 /* 1944 * Mapping has not changed, must be protection or wiring change. 1945 */ 1946 if (origpte && (opa == pa)) { 1947 /* 1948 * Wiring change, just update stats. We don't worry about 1949 * wiring PT pages as they remain resident as long as there 1950 * are valid mappings in them. Hence, if a user page is wired, 1951 * the PT page will be also. 1952 */ 1953 if (wired && ((origpte & PG_W) == 0)) 1954 pmap->pm_stats.wired_count++; 1955 else if (!wired && (origpte & PG_W)) 1956 pmap->pm_stats.wired_count--; 1957 1958#if defined(PMAP_DIAGNOSTIC) 1959 if (pmap_nw_modified((pt_entry_t) origpte)) { 1960 printf("pmap_enter: modified page not writable: va: 0x%lx, pte: 0x%lx\n", va, origpte); 1961 } 1962#endif 1963 1964 /* 1965 * We might be turning off write access to the page, 1966 * so we go ahead and sense modify status. 1967 */ 1968 if (origpte & PG_MANAGED) { 1969 vm_page_t m; 1970 if (origpte & PG_M) { 1971 if (pmap_track_modified(va)) { 1972 m = PHYS_TO_VM_PAGE(pa); 1973 m->dirty = VM_PAGE_BITS_ALL; 1974 } 1975 } 1976 pa |= PG_MANAGED; 1977 } 1978 1979 if (mpte) 1980 --mpte->hold_count; 1981 1982 goto validate; 1983 } 1984 /* 1985 * Mapping has changed, invalidate old range and fall through to 1986 * handle validating new mapping. 1987 */ 1988 if (opa) { 1989 int err; 1990 err = pmap_remove_pte(pmap, pte, va); 1991 if (err) 1992 panic("pmap_enter: pte vanished, va: 0x%x", va); 1993 } 1994 1995 /* 1996 * Enter on the PV list if part of our managed memory Note that we 1997 * raise IPL while manipulating pv_table since pmap_enter can be 1998 * called at interrupt time. 1999 */ 2000 if (pmap_is_managed(pa)) { 2001 pmap_insert_entry(pmap, va, mpte, pa); 2002 pa |= PG_MANAGED; 2003 } 2004 2005 /* 2006 * Increment counters 2007 */ 2008 pmap->pm_stats.resident_count++; 2009 if (wired) 2010 pmap->pm_stats.wired_count++; 2011 2012validate: 2013 /* 2014 * Now validate mapping with desired protection/wiring. 2015 */ 2016 newpte = (vm_offset_t) (pa | pte_prot(pmap, prot) | PG_V); 2017 2018 if (wired) 2019 newpte |= PG_W; 2020 if (va < UPT_MIN_ADDRESS) 2021 newpte |= PG_U; 2022 if (pmap == kernel_pmap) 2023 newpte |= pgeflag; 2024 2025 /* 2026 * if the mapping or permission bits are different, we need 2027 * to update the pte. 2028 */ 2029 if ((origpte & ~(PG_M|PG_A)) != newpte) { 2030 *pte = newpte; 2031 if (origpte) 2032 invltlb_1pg(va); 2033 } 2034} 2035 2036/* 2037 * this code makes some *MAJOR* assumptions: 2038 * 1. Current pmap & pmap exists. 2039 * 2. Not wired. 2040 * 3. Read access. 2041 * 4. No page table pages. 2042 * 5. Tlbflush is deferred to calling procedure. 2043 * 6. Page IS managed. 2044 * but is *MUCH* faster than pmap_enter... 2045 */ 2046 2047static vm_page_t 2048pmap_enter_quick(pmap, va, pa, mpte) 2049 register pmap_t pmap; 2050 vm_offset_t va; 2051 register vm_offset_t pa; 2052 vm_page_t mpte; 2053{ 2054 register unsigned *pte; 2055 2056 /* 2057 * In the case that a page table page is not 2058 * resident, we are creating it here. 2059 */ 2060 if (va < UPT_MIN_ADDRESS) { 2061 unsigned ptepindex; 2062 vm_offset_t ptepa; 2063 2064 /* 2065 * Calculate pagetable page index 2066 */ 2067 ptepindex = va >> PDRSHIFT; 2068 if (mpte && (mpte->pindex == ptepindex)) { 2069 ++mpte->hold_count; 2070 } else { 2071retry: 2072 /* 2073 * Get the page directory entry 2074 */ 2075 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex]; 2076 2077 /* 2078 * If the page table page is mapped, we just increment 2079 * the hold count, and activate it. 2080 */ 2081 if (ptepa) { 2082#if defined(PTPHINT) 2083 if (pmap->pm_ptphint && 2084 (pmap->pm_ptphint->pindex == ptepindex)) { 2085 mpte = pmap->pm_ptphint; 2086 } else { 2087 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex); 2088 pmap->pm_ptphint = mpte; 2089 } 2090#else 2091 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex); 2092#endif 2093 if (mpte == NULL) 2094 goto retry; 2095 ++mpte->hold_count; 2096 } else { 2097 mpte = _pmap_allocpte(pmap, ptepindex); 2098 } 2099 } 2100 } else { 2101 mpte = NULL; 2102 } 2103 2104 /* 2105 * This call to vtopte makes the assumption that we are 2106 * entering the page into the current pmap. In order to support 2107 * quick entry into any pmap, one would likely use pmap_pte_quick. 2108 * But that isn't as quick as vtopte. 2109 */ 2110 pte = (unsigned *)vtopte(va); 2111 if (*pte) { 2112 if (mpte) 2113 pmap_unwire_pte_hold(pmap, mpte); 2114 return 0; 2115 } 2116 2117 /* 2118 * Enter on the PV list if part of our managed memory Note that we 2119 * raise IPL while manipulating pv_table since pmap_enter can be 2120 * called at interrupt time. 2121 */ 2122 pmap_insert_entry(pmap, va, mpte, pa); 2123 2124 /* 2125 * Increment counters 2126 */ 2127 pmap->pm_stats.resident_count++; 2128 2129 /* 2130 * Now validate mapping with RO protection 2131 */ 2132 *pte = pa | PG_V | PG_U | PG_MANAGED; 2133 2134 return mpte; 2135} 2136 2137#define MAX_INIT_PT (96) 2138/* 2139 * pmap_object_init_pt preloads the ptes for a given object 2140 * into the specified pmap. This eliminates the blast of soft 2141 * faults on process startup and immediately after an mmap. 2142 */ 2143void 2144pmap_object_init_pt(pmap, addr, object, pindex, size, limit) 2145 pmap_t pmap; 2146 vm_offset_t addr; 2147 vm_object_t object; 2148 vm_pindex_t pindex; 2149 vm_size_t size; 2150 int limit; 2151{ 2152 vm_offset_t tmpidx; 2153 int psize; 2154 vm_page_t p, mpte; 2155 int objpgs; 2156 2157 psize = i386_btop(size); 2158 2159 if (!pmap || (object->type != OBJT_VNODE) || 2160 (limit && (psize > MAX_INIT_PT) && 2161 (object->resident_page_count > MAX_INIT_PT))) { 2162 return; 2163 } 2164 2165 if (psize + pindex > object->size) 2166 psize = object->size - pindex; 2167 2168 mpte = NULL; 2169 /* 2170 * if we are processing a major portion of the object, then scan the 2171 * entire thing. 2172 */ 2173 if (psize > (object->size >> 2)) { 2174 objpgs = psize; 2175 2176 for (p = TAILQ_FIRST(&object->memq); 2177 ((objpgs > 0) && (p != NULL)); 2178 p = TAILQ_NEXT(p, listq)) { 2179 2180 tmpidx = p->pindex; 2181 if (tmpidx < pindex) { 2182 continue; 2183 } 2184 tmpidx -= pindex; 2185 if (tmpidx >= psize) { 2186 continue; 2187 } 2188 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) && 2189 (p->busy == 0) && 2190 (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) { 2191 if ((p->queue - p->pc) == PQ_CACHE) 2192 vm_page_deactivate(p); 2193 p->flags |= PG_BUSY; 2194 mpte = pmap_enter_quick(pmap, 2195 addr + i386_ptob(tmpidx), 2196 VM_PAGE_TO_PHYS(p), mpte); 2197 p->flags |= PG_MAPPED; 2198 PAGE_WAKEUP(p); 2199 } 2200 objpgs -= 1; 2201 } 2202 } else { 2203 /* 2204 * else lookup the pages one-by-one. 2205 */ 2206 for (tmpidx = 0; tmpidx < psize; tmpidx += 1) { 2207 p = vm_page_lookup(object, tmpidx + pindex); 2208 if (p && 2209 ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) && 2210 (p->busy == 0) && 2211 (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) { 2212 if ((p->queue - p->pc) == PQ_CACHE) 2213 vm_page_deactivate(p); 2214 p->flags |= PG_BUSY; 2215 mpte = pmap_enter_quick(pmap, 2216 addr + i386_ptob(tmpidx), 2217 VM_PAGE_TO_PHYS(p), mpte); 2218 p->flags |= PG_MAPPED; 2219 PAGE_WAKEUP(p); 2220 } 2221 } 2222 } 2223 return; 2224} 2225 2226/* 2227 * pmap_prefault provides a quick way of clustering 2228 * pagefaults into a processes address space. It is a "cousin" 2229 * of pmap_object_init_pt, except it runs at page fault time instead 2230 * of mmap time. 2231 */ 2232#define PFBAK 2 2233#define PFFOR 2 2234#define PAGEORDER_SIZE (PFBAK+PFFOR) 2235 2236static int pmap_prefault_pageorder[] = { 2237 -PAGE_SIZE, PAGE_SIZE, -2 * PAGE_SIZE, 2 * PAGE_SIZE 2238}; 2239 2240void 2241pmap_prefault(pmap, addra, entry, object) 2242 pmap_t pmap; 2243 vm_offset_t addra; 2244 vm_map_entry_t entry; 2245 vm_object_t object; 2246{ 2247 int i; 2248 vm_offset_t starta; 2249 vm_offset_t addr; 2250 vm_pindex_t pindex; 2251 vm_page_t m, mpte; 2252 2253 if (entry->object.vm_object != object) 2254 return; 2255 2256 if (!curproc || (pmap != &curproc->p_vmspace->vm_pmap)) 2257 return; 2258 2259 starta = addra - PFBAK * PAGE_SIZE; 2260 if (starta < entry->start) { 2261 starta = entry->start; 2262 } else if (starta > addra) { 2263 starta = 0; 2264 } 2265 2266 mpte = NULL; 2267 for (i = 0; i < PAGEORDER_SIZE; i++) { 2268 vm_object_t lobject; 2269 unsigned *pte; 2270 2271 addr = addra + pmap_prefault_pageorder[i]; 2272 if (addr < starta || addr >= entry->end) 2273 continue; 2274 2275 if ((*pmap_pde(pmap, addr)) == NULL) 2276 continue; 2277 2278 pte = (unsigned *) vtopte(addr); 2279 if (*pte) 2280 continue; 2281 2282 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT; 2283 lobject = object; 2284 for (m = vm_page_lookup(lobject, pindex); 2285 (!m && (lobject->type == OBJT_DEFAULT) && (lobject->backing_object)); 2286 lobject = lobject->backing_object) { 2287 if (lobject->backing_object_offset & PAGE_MASK) 2288 break; 2289 pindex += (lobject->backing_object_offset >> PAGE_SHIFT); 2290 m = vm_page_lookup(lobject->backing_object, pindex); 2291 } 2292 2293 /* 2294 * give-up when a page is not in memory 2295 */ 2296 if (m == NULL) 2297 break; 2298 2299 if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) && 2300 (m->busy == 0) && 2301 (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) { 2302 2303 if ((m->queue - m->pc) == PQ_CACHE) { 2304 vm_page_deactivate(m); 2305 } 2306 m->flags |= PG_BUSY; 2307 mpte = pmap_enter_quick(pmap, addr, 2308 VM_PAGE_TO_PHYS(m), mpte); 2309 m->flags |= PG_MAPPED; 2310 PAGE_WAKEUP(m); 2311 } 2312 } 2313} 2314 2315/* 2316 * Routine: pmap_change_wiring 2317 * Function: Change the wiring attribute for a map/virtual-address 2318 * pair. 2319 * In/out conditions: 2320 * The mapping must already exist in the pmap. 2321 */ 2322void 2323pmap_change_wiring(pmap, va, wired) 2324 register pmap_t pmap; 2325 vm_offset_t va; 2326 boolean_t wired; 2327{ 2328 register unsigned *pte; 2329 2330 if (pmap == NULL) 2331 return; 2332 2333 pte = pmap_pte(pmap, va); 2334 2335 if (wired && !pmap_pte_w(pte)) 2336 pmap->pm_stats.wired_count++; 2337 else if (!wired && pmap_pte_w(pte)) 2338 pmap->pm_stats.wired_count--; 2339 2340 /* 2341 * Wiring is not a hardware characteristic so there is no need to 2342 * invalidate TLB. 2343 */ 2344 pmap_pte_set_w(pte, wired); 2345} 2346 2347 2348 2349/* 2350 * Copy the range specified by src_addr/len 2351 * from the source map to the range dst_addr/len 2352 * in the destination map. 2353 * 2354 * This routine is only advisory and need not do anything. 2355 */ 2356 2357void 2358pmap_copy(dst_pmap, src_pmap, dst_addr, len, src_addr) 2359 pmap_t dst_pmap, src_pmap; 2360 vm_offset_t dst_addr; 2361 vm_size_t len; 2362 vm_offset_t src_addr; 2363{ 2364 vm_offset_t addr; 2365 vm_offset_t end_addr = src_addr + len; 2366 vm_offset_t pdnxt; 2367 unsigned src_frame, dst_frame; 2368 2369 if (dst_addr != src_addr) 2370 return; 2371 2372 src_frame = ((unsigned) src_pmap->pm_pdir[PTDPTDI]) & PG_FRAME; 2373 if (src_frame != (((unsigned) PTDpde) & PG_FRAME)) { 2374 return; 2375 } 2376 2377 dst_frame = ((unsigned) dst_pmap->pm_pdir[PTDPTDI]) & PG_FRAME; 2378 if (dst_frame != (((unsigned) APTDpde) & PG_FRAME)) { 2379 APTDpde = (pd_entry_t) (dst_frame | PG_RW | PG_V); 2380 invltlb(); 2381 } 2382 2383 for(addr = src_addr; addr < end_addr; addr = pdnxt) { 2384 unsigned *src_pte, *dst_pte; 2385 vm_page_t dstmpte, srcmpte; 2386 vm_offset_t srcptepaddr; 2387 unsigned ptepindex; 2388 2389 if (addr >= UPT_MIN_ADDRESS) 2390 panic("pmap_copy: invalid to pmap_copy page tables\n"); 2391 2392 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1)); 2393 ptepindex = addr >> PDRSHIFT; 2394 2395 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex]; 2396 if (srcptepaddr == 0) 2397 continue; 2398 2399 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex); 2400 if ((srcmpte->hold_count == 0) || (srcmpte->flags & PG_BUSY)) 2401 continue; 2402 2403 if (pdnxt > end_addr) 2404 pdnxt = end_addr; 2405 2406 src_pte = (unsigned *) vtopte(addr); 2407 dst_pte = (unsigned *) avtopte(addr); 2408 while (addr < pdnxt) { 2409 unsigned ptetemp; 2410 ptetemp = *src_pte; 2411 /* 2412 * we only virtual copy managed pages 2413 */ 2414 if ((ptetemp & PG_MANAGED) != 0) { 2415 /* 2416 * We have to check after allocpte for the 2417 * pte still being around... allocpte can 2418 * block. 2419 */ 2420 dstmpte = pmap_allocpte(dst_pmap, addr); 2421 if ((*dst_pte == 0) && (ptetemp = *src_pte)) { 2422 /* 2423 * Clear the modified and 2424 * accessed (referenced) bits 2425 * during the copy. 2426 */ 2427 *dst_pte = ptetemp & ~(PG_M|PG_A); 2428 dst_pmap->pm_stats.resident_count++; 2429 pmap_insert_entry(dst_pmap, addr, 2430 dstmpte, 2431 (ptetemp & PG_FRAME)); 2432 } else { 2433 pmap_unwire_pte_hold(dst_pmap, dstmpte); 2434 } 2435 if (dstmpte->hold_count >= srcmpte->hold_count) 2436 break; 2437 } 2438 addr += PAGE_SIZE; 2439 ++src_pte; 2440 ++dst_pte; 2441 } 2442 } 2443} 2444 2445/* 2446 * Routine: pmap_kernel 2447 * Function: 2448 * Returns the physical map handle for the kernel. 2449 */ 2450pmap_t 2451pmap_kernel() 2452{ 2453 return (kernel_pmap); 2454} 2455 2456/* 2457 * pmap_zero_page zeros the specified (machine independent) 2458 * page by mapping the page into virtual memory and using 2459 * bzero to clear its contents, one machine dependent page 2460 * at a time. 2461 */ 2462void 2463pmap_zero_page(phys) 2464 vm_offset_t phys; 2465{ 2466 if (*(int *) CMAP2) 2467 panic("pmap_zero_page: CMAP busy"); 2468 2469 *(int *) CMAP2 = PG_V | PG_RW | (phys & PG_FRAME); 2470 bzero(CADDR2, PAGE_SIZE); 2471 *(int *) CMAP2 = 0; 2472 invltlb_1pg((vm_offset_t) CADDR2); 2473} 2474 2475/* 2476 * pmap_copy_page copies the specified (machine independent) 2477 * page by mapping the page into virtual memory and using 2478 * bcopy to copy the page, one machine dependent page at a 2479 * time. 2480 */ 2481void 2482pmap_copy_page(src, dst) 2483 vm_offset_t src; 2484 vm_offset_t dst; 2485{ 2486 if (*(int *) CMAP1 || *(int *) CMAP2) 2487 panic("pmap_copy_page: CMAP busy"); 2488 2489 *(int *) CMAP1 = PG_V | PG_RW | (src & PG_FRAME); 2490 *(int *) CMAP2 = PG_V | PG_RW | (dst & PG_FRAME); 2491 2492 bcopy(CADDR1, CADDR2, PAGE_SIZE); 2493 2494 *(int *) CMAP1 = 0; 2495 *(int *) CMAP2 = 0; 2496 invltlb_2pg( (vm_offset_t) CADDR1, (vm_offset_t) CADDR2); 2497} 2498 2499 2500/* 2501 * Routine: pmap_pageable 2502 * Function: 2503 * Make the specified pages (by pmap, offset) 2504 * pageable (or not) as requested. 2505 * 2506 * A page which is not pageable may not take 2507 * a fault; therefore, its page table entry 2508 * must remain valid for the duration. 2509 * 2510 * This routine is merely advisory; pmap_enter 2511 * will specify that these pages are to be wired 2512 * down (or not) as appropriate. 2513 */ 2514void 2515pmap_pageable(pmap, sva, eva, pageable) 2516 pmap_t pmap; 2517 vm_offset_t sva, eva; 2518 boolean_t pageable; 2519{ 2520} 2521 2522/* 2523 * this routine returns true if a physical page resides 2524 * in the given pmap. 2525 */ 2526boolean_t 2527pmap_page_exists(pmap, pa) 2528 pmap_t pmap; 2529 vm_offset_t pa; 2530{ 2531 register pv_entry_t pv; 2532 pv_table_t *ppv; 2533 int s; 2534 2535 if (!pmap_is_managed(pa)) 2536 return FALSE; 2537 2538 s = splvm(); 2539 2540 ppv = pa_to_pvh(pa); 2541 /* 2542 * Not found, check current mappings returning immediately if found. 2543 */ 2544 for (pv = TAILQ_FIRST(&ppv->pv_list); 2545 pv; 2546 pv = TAILQ_NEXT(pv, pv_list)) { 2547 if (pv->pv_pmap == pmap) { 2548 splx(s); 2549 return TRUE; 2550 } 2551 } 2552 splx(s); 2553 return (FALSE); 2554} 2555 2556#define PMAP_REMOVE_PAGES_CURPROC_ONLY 2557/* 2558 * Remove all pages from specified address space 2559 * this aids process exit speeds. Also, this code 2560 * is special cased for current process only, but 2561 * can have the more generic (and slightly slower) 2562 * mode enabled. This is much faster than pmap_remove 2563 * in the case of running down an entire address space. 2564 */ 2565void 2566pmap_remove_pages(pmap, sva, eva) 2567 pmap_t pmap; 2568 vm_offset_t sva, eva; 2569{ 2570 unsigned *pte, tpte; 2571 pv_table_t *ppv; 2572 pv_entry_t pv, npv; 2573 int s; 2574 2575#if PMAP_PVLIST 2576 2577#ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY 2578 if (!curproc || (pmap != &curproc->p_vmspace->vm_pmap)) { 2579 printf("warning: pmap_remove_pages called with non-current pmap\n"); 2580 return; 2581 } 2582#endif 2583 2584 s = splvm(); 2585 for(pv = TAILQ_FIRST(&pmap->pm_pvlist); 2586 pv; 2587 pv = npv) { 2588 2589 if (pv->pv_va >= eva || pv->pv_va < sva) { 2590 npv = TAILQ_NEXT(pv, pv_plist); 2591 continue; 2592 } 2593 2594#ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY 2595 pte = (unsigned *)vtopte(pv->pv_va); 2596#else 2597 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va); 2598#endif 2599 tpte = *pte; 2600 2601/* 2602 * We cannot remove wired pages from a process' mapping at this time 2603 */ 2604 if (tpte & PG_W) { 2605 npv = TAILQ_NEXT(pv, pv_plist); 2606 continue; 2607 } 2608 *pte = 0; 2609 2610 ppv = pa_to_pvh(tpte); 2611 2612 pv->pv_pmap->pm_stats.resident_count--; 2613 2614 /* 2615 * Update the vm_page_t clean and reference bits. 2616 */ 2617 if (tpte & PG_M) { 2618 ppv->pv_vm_page->dirty = VM_PAGE_BITS_ALL; 2619 } 2620 2621 2622 npv = TAILQ_NEXT(pv, pv_plist); 2623 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist); 2624 2625 --ppv->pv_list_count; 2626 TAILQ_REMOVE(&ppv->pv_list, pv, pv_list); 2627 if (TAILQ_FIRST(&ppv->pv_list) == NULL) { 2628 ppv->pv_vm_page->flags &= ~(PG_MAPPED|PG_WRITEABLE); 2629 } 2630 2631 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem); 2632 free_pv_entry(pv); 2633 } 2634 splx(s); 2635 invltlb(); 2636#endif 2637} 2638 2639/* 2640 * pmap_testbit tests bits in pte's 2641 * note that the testbit/changebit routines are inline, 2642 * and a lot of things compile-time evaluate. 2643 */ 2644static boolean_t 2645pmap_testbit(pa, bit) 2646 register vm_offset_t pa; 2647 int bit; 2648{ 2649 register pv_entry_t pv; 2650 pv_table_t *ppv; 2651 unsigned *pte; 2652 int s; 2653 2654 if (!pmap_is_managed(pa)) 2655 return FALSE; 2656 2657 ppv = pa_to_pvh(pa); 2658 if (TAILQ_FIRST(&ppv->pv_list) == NULL) 2659 return FALSE; 2660 2661 s = splvm(); 2662 2663 for (pv = TAILQ_FIRST(&ppv->pv_list); 2664 pv; 2665 pv = TAILQ_NEXT(pv, pv_list)) { 2666 2667 /* 2668 * if the bit being tested is the modified bit, then 2669 * mark clean_map and ptes as never 2670 * modified. 2671 */ 2672 if (bit & (PG_A|PG_M)) { 2673 if (!pmap_track_modified(pv->pv_va)) 2674 continue; 2675 } 2676 2677#if defined(PMAP_DIAGNOSTIC) 2678 if (!pv->pv_pmap) { 2679 printf("Null pmap (tb) at va: 0x%lx\n", pv->pv_va); 2680 continue; 2681 } 2682#endif 2683 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va); 2684 if (*pte & bit) { 2685 splx(s); 2686 return TRUE; 2687 } 2688 } 2689 splx(s); 2690 return (FALSE); 2691} 2692 2693/* 2694 * this routine is used to modify bits in ptes 2695 */ 2696static void 2697pmap_changebit(pa, bit, setem) 2698 vm_offset_t pa; 2699 int bit; 2700 boolean_t setem; 2701{ 2702 register pv_entry_t pv; 2703 pv_table_t *ppv; 2704 register unsigned *pte; 2705 int changed; 2706 int s; 2707 2708 if (!pmap_is_managed(pa)) 2709 return; 2710 2711 s = splvm(); 2712 changed = 0; 2713 ppv = pa_to_pvh(pa); 2714 2715 /* 2716 * Loop over all current mappings setting/clearing as appropos If 2717 * setting RO do we need to clear the VAC? 2718 */ 2719 for (pv = TAILQ_FIRST(&ppv->pv_list); 2720 pv; 2721 pv = TAILQ_NEXT(pv, pv_list)) { 2722 2723 /* 2724 * don't write protect pager mappings 2725 */ 2726 if (!setem && (bit == PG_RW)) { 2727 if (!pmap_track_modified(pv->pv_va)) 2728 continue; 2729 } 2730 2731#if defined(PMAP_DIAGNOSTIC) 2732 if (!pv->pv_pmap) { 2733 printf("Null pmap (cb) at va: 0x%lx\n", pv->pv_va); 2734 continue; 2735 } 2736#endif 2737 2738 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va); 2739 2740 if (setem) { 2741 *(int *)pte |= bit; 2742 changed = 1; 2743 } else { 2744 vm_offset_t pbits = *(vm_offset_t *)pte; 2745 if (pbits & bit) { 2746 changed = 1; 2747 if (bit == PG_RW) { 2748 if (pbits & PG_M) { 2749 ppv->pv_vm_page->dirty = VM_PAGE_BITS_ALL; 2750 } 2751 *(int *)pte = pbits & ~(PG_M|PG_RW); 2752 } else { 2753 *(int *)pte = pbits & ~bit; 2754 } 2755 } 2756 } 2757 } 2758 splx(s); 2759 if (changed) 2760 invltlb(); 2761} 2762 2763/* 2764 * pmap_page_protect: 2765 * 2766 * Lower the permission for all mappings to a given page. 2767 */ 2768void 2769pmap_page_protect(phys, prot) 2770 vm_offset_t phys; 2771 vm_prot_t prot; 2772{ 2773 if ((prot & VM_PROT_WRITE) == 0) { 2774 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) { 2775 pmap_changebit(phys, PG_RW, FALSE); 2776 } else { 2777 pmap_remove_all(phys); 2778 } 2779 } 2780} 2781 2782vm_offset_t 2783pmap_phys_address(ppn) 2784 int ppn; 2785{ 2786 return (i386_ptob(ppn)); 2787} 2788 2789/* 2790 * pmap_is_referenced: 2791 * 2792 * Return whether or not the specified physical page was referenced 2793 * by any physical maps. 2794 */ 2795boolean_t 2796pmap_is_referenced(vm_offset_t pa) 2797{ 2798 register pv_entry_t pv; 2799 pv_table_t *ppv; 2800 unsigned *pte; 2801 int s; 2802 2803 if (!pmap_is_managed(pa)) 2804 return FALSE; 2805 2806 ppv = pa_to_pvh(pa); 2807 2808 s = splvm(); 2809 /* 2810 * Not found, check current mappings returning immediately if found. 2811 */ 2812 for (pv = TAILQ_FIRST(&ppv->pv_list); 2813 pv; 2814 pv = TAILQ_NEXT(pv, pv_list)) { 2815 2816 /* 2817 * if the bit being tested is the modified bit, then 2818 * mark clean_map and ptes as never 2819 * modified. 2820 */ 2821 if (!pmap_track_modified(pv->pv_va)) 2822 continue; 2823 2824 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va); 2825 if ((int) *pte & PG_A) { 2826 splx(s); 2827 return TRUE; 2828 } 2829 } 2830 splx(s); 2831 return (FALSE); 2832} 2833 2834/* 2835 * pmap_ts_referenced: 2836 * 2837 * Return the count of reference bits for a page, clearing all of them. 2838 * 2839 */ 2840int 2841pmap_ts_referenced(vm_offset_t pa) 2842{ 2843 register pv_entry_t pv; 2844 pv_table_t *ppv; 2845 unsigned *pte; 2846 int s; 2847 int rtval = 0; 2848 2849 if (!pmap_is_managed(pa)) 2850 return FALSE; 2851 2852 s = splvm(); 2853 2854 ppv = pa_to_pvh(pa); 2855 2856 if (TAILQ_FIRST(&ppv->pv_list) == NULL) { 2857 splx(s); 2858 return 0; 2859 } 2860 2861 /* 2862 * Not found, check current mappings returning immediately if found. 2863 */ 2864 for (pv = TAILQ_FIRST(&ppv->pv_list); 2865 pv; 2866 pv = TAILQ_NEXT(pv, pv_list)) { 2867 /* 2868 * if the bit being tested is the modified bit, then 2869 * mark clean_map and ptes as never 2870 * modified. 2871 */ 2872 if (!pmap_track_modified(pv->pv_va)) 2873 continue; 2874 2875 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va); 2876 if (pte == NULL) { 2877 continue; 2878 } 2879 if (*pte & PG_A) { 2880 rtval++; 2881 *pte &= ~PG_A; 2882 } 2883 } 2884 splx(s); 2885 if (rtval) { 2886 invltlb(); 2887 } 2888 return (rtval); 2889} 2890 2891/* 2892 * pmap_is_modified: 2893 * 2894 * Return whether or not the specified physical page was modified 2895 * in any physical maps. 2896 */ 2897boolean_t 2898pmap_is_modified(vm_offset_t pa) 2899{ 2900 return pmap_testbit((pa), PG_M); 2901} 2902 2903/* 2904 * Clear the modify bits on the specified physical page. 2905 */ 2906void 2907pmap_clear_modify(vm_offset_t pa) 2908{ 2909 pmap_changebit((pa), PG_M, FALSE); 2910} 2911 2912/* 2913 * pmap_clear_reference: 2914 * 2915 * Clear the reference bit on the specified physical page. 2916 */ 2917void 2918pmap_clear_reference(vm_offset_t pa) 2919{ 2920 pmap_changebit((pa), PG_A, FALSE); 2921} 2922 2923/* 2924 * Miscellaneous support routines follow 2925 */ 2926 2927static void 2928i386_protection_init() 2929{ 2930 register int *kp, prot; 2931 2932 kp = protection_codes; 2933 for (prot = 0; prot < 8; prot++) { 2934 switch (prot) { 2935 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE: 2936 /* 2937 * Read access is also 0. There isn't any execute bit, 2938 * so just make it readable. 2939 */ 2940 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE: 2941 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE: 2942 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE: 2943 *kp++ = 0; 2944 break; 2945 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE: 2946 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE: 2947 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE: 2948 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE: 2949 *kp++ = PG_RW; 2950 break; 2951 } 2952 } 2953} 2954 2955/* 2956 * Map a set of physical memory pages into the kernel virtual 2957 * address space. Return a pointer to where it is mapped. This 2958 * routine is intended to be used for mapping device memory, 2959 * NOT real memory. The non-cacheable bits are set on each 2960 * mapped page. 2961 */ 2962void * 2963pmap_mapdev(pa, size) 2964 vm_offset_t pa; 2965 vm_size_t size; 2966{ 2967 vm_offset_t va, tmpva; 2968 unsigned *pte; 2969 2970 size = roundup(size, PAGE_SIZE); 2971 2972 va = kmem_alloc_pageable(kernel_map, size); 2973 if (!va) 2974 panic("pmap_mapdev: Couldn't alloc kernel virtual memory"); 2975 2976 pa = pa & PG_FRAME; 2977 for (tmpva = va; size > 0;) { 2978 pte = (unsigned *)vtopte(tmpva); 2979 *pte = pa | PG_RW | PG_V | PG_N; 2980 size -= PAGE_SIZE; 2981 tmpva += PAGE_SIZE; 2982 pa += PAGE_SIZE; 2983 } 2984 invltlb(); 2985 2986 return ((void *) va); 2987} 2988 2989/* 2990 * perform the pmap work for mincore 2991 */ 2992int 2993pmap_mincore(pmap, addr) 2994 pmap_t pmap; 2995 vm_offset_t addr; 2996{ 2997 2998 unsigned *ptep, pte; 2999 int val = 0; 3000 3001 ptep = pmap_pte(pmap, addr); 3002 if (ptep == 0) { 3003 return 0; 3004 } 3005 3006 if (pte = *ptep) { 3007 vm_offset_t pa; 3008 val = MINCORE_INCORE; 3009 pa = pte & PG_FRAME; 3010 3011 /* 3012 * Modified by us 3013 */ 3014 if (pte & PG_M) 3015 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER; 3016 /* 3017 * Modified by someone 3018 */ 3019 else if (PHYS_TO_VM_PAGE(pa)->dirty || 3020 pmap_is_modified(pa)) 3021 val |= MINCORE_MODIFIED_OTHER; 3022 /* 3023 * Referenced by us 3024 */ 3025 if (pte & PG_U) 3026 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER; 3027 3028 /* 3029 * Referenced by someone 3030 */ 3031 else if ((PHYS_TO_VM_PAGE(pa)->flags & PG_REFERENCED) || 3032 pmap_is_referenced(pa)) 3033 val |= MINCORE_REFERENCED_OTHER; 3034 } 3035 return val; 3036} 3037 3038#if defined(PMAP_DEBUG) 3039pmap_pid_dump(int pid) { 3040 pmap_t pmap; 3041 struct proc *p; 3042 int npte = 0; 3043 int index; 3044 for (p = allproc.lh_first; p != NULL; p = p->p_list.le_next) { 3045 if (p->p_pid != pid) 3046 continue; 3047 3048 if (p->p_vmspace) { 3049 int i,j; 3050 index = 0; 3051 pmap = &p->p_vmspace->vm_pmap; 3052 for(i=0;i<1024;i++) { 3053 pd_entry_t *pde; 3054 unsigned *pte; 3055 unsigned base = i << PDRSHIFT; 3056 3057 pde = &pmap->pm_pdir[i]; 3058 if (pde && pmap_pde_v(pde)) { 3059 for(j=0;j<1024;j++) { 3060 unsigned va = base + (j << PAGE_SHIFT); 3061 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) { 3062 if (index) { 3063 index = 0; 3064 printf("\n"); 3065 } 3066 return npte; 3067 } 3068 pte = pmap_pte_quick( pmap, va); 3069 if (pte && pmap_pte_v(pte)) { 3070 vm_offset_t pa; 3071 vm_page_t m; 3072 pa = *(int *)pte; 3073 m = PHYS_TO_VM_PAGE((pa & PG_FRAME)); 3074 printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x", 3075 va, pa, m->hold_count, m->wire_count, m->flags); 3076 npte++; 3077 index++; 3078 if (index >= 2) { 3079 index = 0; 3080 printf("\n"); 3081 } else { 3082 printf(" "); 3083 } 3084 } 3085 } 3086 } 3087 } 3088 } 3089 } 3090 return npte; 3091} 3092#endif 3093 3094#if defined(DEBUG) 3095 3096static void pads __P((pmap_t pm)); 3097static void pmap_pvdump __P((vm_offset_t pa)); 3098 3099/* print address space of pmap*/ 3100static void 3101pads(pm) 3102 pmap_t pm; 3103{ 3104 unsigned va, i, j; 3105 unsigned *ptep; 3106 3107 if (pm == kernel_pmap) 3108 return; 3109 for (i = 0; i < 1024; i++) 3110 if (pm->pm_pdir[i]) 3111 for (j = 0; j < 1024; j++) { 3112 va = (i << PDRSHIFT) + (j << PAGE_SHIFT); 3113 if (pm == kernel_pmap && va < KERNBASE) 3114 continue; 3115 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS) 3116 continue; 3117 ptep = pmap_pte_quick(pm, va); 3118 if (pmap_pte_v(ptep)) 3119 printf("%x:%x ", va, *(int *) ptep); 3120 }; 3121 3122} 3123 3124static void 3125pmap_pvdump(pa) 3126 vm_offset_t pa; 3127{ 3128 pv_table_t *ppv; 3129 register pv_entry_t pv; 3130 3131 printf("pa %x", pa); 3132 ppv = pa_to_pvh(pa); 3133 for (pv = TAILQ_FIRST(&ppv->pv_list); 3134 pv; 3135 pv = TAILQ_NEXT(pv, pv_list)) { 3136#ifdef used_to_be 3137 printf(" -> pmap %x, va %x, flags %x", 3138 pv->pv_pmap, pv->pv_va, pv->pv_flags); 3139#endif 3140 printf(" -> pmap %x, va %x", 3141 pv->pv_pmap, pv->pv_va); 3142 pads(pv->pv_pmap); 3143 } 3144 printf(" "); 3145} 3146#endif 3147