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