2 * Copyright (c) 1991 Regents of the University of California.
4 * Copyright (c) 1994 John S. Dyson
6 * Copyright (c) 1994 David Greenman
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.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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
41 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91
42 * $FreeBSD: src/sys/i386/i386/pmap.c,v 1.250.2.18 2002/03/06 22:48:53 silby Exp $
43 * $DragonFly: src/sys/i386/i386/Attic/pmap.c,v 1.28 2004/01/20 05:04:04 dillon Exp $
47 * Manages physical address maps.
49 * In addition to hardware address maps, this
50 * module is called upon to provide software-use-only
51 * maps which may or may not be stored in the same
52 * form as hardware maps. These pseudo-maps are
53 * used to store intermediate results from copy
54 * operations to and from address spaces.
56 * Since the information managed by this module is
57 * also stored by the logical address mapping module,
58 * this module may throw away valid virtual-to-physical
59 * mappings at almost any time. However, invalidations
60 * of virtual-to-physical mappings must be done as
63 * In order to cope with hardware architectures which
64 * make virtual-to-physical map invalidates expensive,
65 * this module may delay invalidate or reduced protection
66 * operations until such time as they are actually
67 * necessary. This module is given full information as
68 * to which processors are currently using which maps,
69 * and to when physical maps must be made correct.
72 #include "opt_disable_pse.h"
74 #include "opt_msgbuf.h"
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
80 #include <sys/msgbuf.h>
81 #include <sys/vmmeter.h>
85 #include <vm/vm_param.h>
86 #include <sys/sysctl.h>
88 #include <vm/vm_kern.h>
89 #include <vm/vm_page.h>
90 #include <vm/vm_map.h>
91 #include <vm/vm_object.h>
92 #include <vm/vm_extern.h>
93 #include <vm/vm_pageout.h>
94 #include <vm/vm_pager.h>
95 #include <vm/vm_zone.h>
98 #include <sys/thread2.h>
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 #endif /* SMP || APIC_IO */
107 #include <machine/globaldata.h>
109 #define PMAP_KEEP_PDIRS
110 #ifndef PMAP_SHPGPERPROC
111 #define PMAP_SHPGPERPROC 200
114 #if defined(DIAGNOSTIC)
115 #define PMAP_DIAGNOSTIC
120 #if !defined(PMAP_DIAGNOSTIC)
121 #define PMAP_INLINE __inline
127 * Get PDEs and PTEs for user/kernel address space
129 #define pmap_pde(m, v) (&((m)->pm_pdir[(vm_offset_t)(v) >> PDRSHIFT]))
130 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
132 #define pmap_pde_v(pte) ((*(int *)pte & PG_V) != 0)
133 #define pmap_pte_w(pte) ((*(int *)pte & PG_W) != 0)
134 #define pmap_pte_m(pte) ((*(int *)pte & PG_M) != 0)
135 #define pmap_pte_u(pte) ((*(int *)pte & PG_A) != 0)
136 #define pmap_pte_v(pte) ((*(int *)pte & PG_V) != 0)
138 #define pmap_pte_set_w(pte, v) ((v)?(*(int *)pte |= PG_W):(*(int *)pte &= ~PG_W))
139 #define pmap_pte_set_prot(pte, v) ((*(int *)pte &= ~PG_PROT), (*(int *)pte |= (v)))
142 * Given a map and a machine independent protection code,
143 * convert to a vax protection code.
145 #define pte_prot(m, p) (protection_codes[p])
146 static int protection_codes[8];
148 static struct pmap kernel_pmap_store;
151 vm_paddr_t avail_start; /* PA of first available physical page */
152 vm_paddr_t avail_end; /* PA of last available physical page */
153 vm_offset_t virtual_avail; /* VA of first avail page (after kernel bss) */
154 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
155 static boolean_t pmap_initialized = FALSE; /* Has pmap_init completed? */
156 static int pgeflag; /* PG_G or-in */
157 static int pseflag; /* PG_PS or-in */
159 static vm_object_t kptobj;
162 vm_offset_t kernel_vm_end;
165 * Data for the pv entry allocation mechanism
167 static vm_zone_t pvzone;
168 static struct vm_zone pvzone_store;
169 static struct vm_object pvzone_obj;
170 static int pv_entry_count=0, pv_entry_max=0, pv_entry_high_water=0;
171 static int pmap_pagedaemon_waken = 0;
172 static struct pv_entry *pvinit;
175 * All those kernel PT submaps that BSD is so fond of
177 pt_entry_t *CMAP1 = 0, *ptmmap;
178 caddr_t CADDR1 = 0, ptvmmap = 0;
179 static pt_entry_t *msgbufmap;
180 struct msgbuf *msgbufp=0;
185 static pt_entry_t *pt_crashdumpmap;
186 static caddr_t crashdumpmap;
188 extern pt_entry_t *SMPpt;
190 static PMAP_INLINE void free_pv_entry (pv_entry_t pv);
191 static unsigned * get_ptbase (pmap_t pmap);
192 static pv_entry_t get_pv_entry (void);
193 static void i386_protection_init (void);
194 static __inline void pmap_changebit (vm_page_t m, int bit, boolean_t setem);
196 static void pmap_remove_all (vm_page_t m);
197 static vm_page_t pmap_enter_quick (pmap_t pmap, vm_offset_t va,
198 vm_page_t m, vm_page_t mpte);
199 static int pmap_remove_pte (struct pmap *pmap, unsigned *ptq,
201 static void pmap_remove_page (struct pmap *pmap, vm_offset_t va);
202 static int pmap_remove_entry (struct pmap *pmap, vm_page_t m,
204 static boolean_t pmap_testbit (vm_page_t m, int bit);
205 static void pmap_insert_entry (pmap_t pmap, vm_offset_t va,
206 vm_page_t mpte, vm_page_t m);
208 static vm_page_t pmap_allocpte (pmap_t pmap, vm_offset_t va);
210 static int pmap_release_free_page (pmap_t pmap, vm_page_t p);
211 static vm_page_t _pmap_allocpte (pmap_t pmap, unsigned ptepindex);
212 static unsigned * pmap_pte_quick (pmap_t pmap, vm_offset_t va);
213 static vm_page_t pmap_page_lookup (vm_object_t object, vm_pindex_t pindex);
214 static int pmap_unuse_pt (pmap_t, vm_offset_t, vm_page_t);
215 static vm_offset_t pmap_kmem_choose(vm_offset_t addr);
217 static unsigned pdir4mb;
220 * Move the kernel virtual free pointer to the next
221 * 4MB. This is used to help improve performance
222 * by using a large (4MB) page for much of the kernel
223 * (.text, .data, .bss)
226 pmap_kmem_choose(vm_offset_t addr)
228 vm_offset_t newaddr = addr;
230 if (cpu_feature & CPUID_PSE) {
231 newaddr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
240 * Extract the page table entry associated with the given map/virtual
243 * This function may NOT be called from an interrupt.
245 PMAP_INLINE unsigned *
246 pmap_pte(pmap_t pmap, vm_offset_t va)
251 pdeaddr = (unsigned *) pmap_pde(pmap, va);
252 if (*pdeaddr & PG_PS)
255 return get_ptbase(pmap) + i386_btop(va);
264 * Super fast pmap_pte routine best used when scanning the pv lists.
265 * This eliminates many course-grained invltlb calls. Note that many of
266 * the pv list scans are across different pmaps and it is very wasteful
267 * to do an entire invltlb when checking a single mapping.
269 * Should only be called while splvm() is held or from a critical
273 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
275 struct mdglobaldata *gd = mdcpu;
278 if ((pde = (unsigned) pmap->pm_pdir[va >> PDRSHIFT]) != 0) {
279 unsigned frame = (unsigned) pmap->pm_pdir[PTDPTDI] & PG_FRAME;
280 unsigned index = i386_btop(va);
281 /* are we current address space or kernel? */
282 if ((pmap == kernel_pmap) ||
283 (frame == (((unsigned) PTDpde) & PG_FRAME))) {
284 return (unsigned *) PTmap + index;
286 newpf = pde & PG_FRAME;
287 if ( ((* (unsigned *) gd->gd_PMAP1) & PG_FRAME) != newpf) {
288 * (unsigned *) gd->gd_PMAP1 = newpf | PG_RW | PG_V;
289 cpu_invlpg(gd->gd_PADDR1);
291 return gd->gd_PADDR1 + ((unsigned) index & (NPTEPG - 1));
298 * Bootstrap the system enough to run with virtual memory.
300 * On the i386 this is called after mapping has already been enabled
301 * and just syncs the pmap module with what has already been done.
302 * [We can't call it easily with mapping off since the kernel is not
303 * mapped with PA == VA, hence we would have to relocate every address
304 * from the linked base (virtual) address "KERNBASE" to the actual
305 * (physical) address starting relative to 0]
308 pmap_bootstrap(firstaddr, loadaddr)
309 vm_paddr_t firstaddr;
314 struct mdglobaldata *gd;
317 avail_start = firstaddr;
320 * XXX The calculation of virtual_avail is wrong. It's NKPT*PAGE_SIZE too
321 * large. It should instead be correctly calculated in locore.s and
322 * not based on 'first' (which is a physical address, not a virtual
323 * address, for the start of unused physical memory). The kernel
324 * page tables are NOT double mapped and thus should not be included
325 * in this calculation.
327 virtual_avail = (vm_offset_t) KERNBASE + firstaddr;
328 virtual_avail = pmap_kmem_choose(virtual_avail);
330 virtual_end = VM_MAX_KERNEL_ADDRESS;
333 * Initialize protection array.
335 i386_protection_init();
338 * The kernel's pmap is statically allocated so we don't have to use
339 * pmap_create, which is unlikely to work correctly at this part of
340 * the boot sequence (XXX and which no longer exists).
342 kernel_pmap = &kernel_pmap_store;
344 kernel_pmap->pm_pdir = (pd_entry_t *)(KERNBASE + (u_int)IdlePTD);
345 kernel_pmap->pm_count = 1;
346 kernel_pmap->pm_active = -1; /* don't allow deactivation */
347 TAILQ_INIT(&kernel_pmap->pm_pvlist);
351 * Reserve some special page table entries/VA space for temporary
354 #define SYSMAP(c, p, v, n) \
355 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
358 pte = (pt_entry_t *) pmap_pte(kernel_pmap, va);
361 * CMAP1/CMAP2 are used for zeroing and copying pages.
363 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
368 SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS);
371 * ptvmmap is used for reading arbitrary physical pages via
374 SYSMAP(caddr_t, ptmmap, ptvmmap, 1)
377 * msgbufp is used to map the system message buffer.
378 * XXX msgbufmap is not used.
380 SYSMAP(struct msgbuf *, msgbufmap, msgbufp,
381 atop(round_page(MSGBUF_SIZE)))
386 for (i = 0; i < NKPT; i++)
390 * PG_G is terribly broken on SMP because we IPI invltlb's in some
391 * cases rather then invl1pg. Actually, I don't even know why it
392 * works under UP because self-referential page table mappings
397 if (cpu_feature & CPUID_PGE)
402 * Initialize the 4MB page size flag
406 * The 4MB page version of the initial
407 * kernel page mapping.
411 #if !defined(DISABLE_PSE)
412 if (cpu_feature & CPUID_PSE) {
415 * Note that we have enabled PSE mode
418 ptditmp = *((unsigned *)PTmap + i386_btop(KERNBASE));
419 ptditmp &= ~(NBPDR - 1);
420 ptditmp |= PG_V | PG_RW | PG_PS | PG_U | pgeflag;
425 * Enable the PSE mode. If we are SMP we can't do this
426 * now because the APs will not be able to use it when
429 load_cr4(rcr4() | CR4_PSE);
432 * We can do the mapping here for the single processor
433 * case. We simply ignore the old page table page from
437 * For SMP, we still need 4K pages to bootstrap APs,
438 * PSE will be enabled as soon as all APs are up.
440 PTD[KPTDI] = (pd_entry_t)ptditmp;
441 kernel_pmap->pm_pdir[KPTDI] = (pd_entry_t)ptditmp;
447 if (cpu_apic_address == 0)
448 panic("pmap_bootstrap: no local apic!");
450 /* local apic is mapped on last page */
451 SMPpt[NPTEPG - 1] = (pt_entry_t)(PG_V | PG_RW | PG_N | pgeflag |
452 (cpu_apic_address & PG_FRAME));
455 /* BSP does this itself, AP's get it pre-set */
456 gd = &CPU_prvspace[0].mdglobaldata;
457 gd->gd_CMAP1 = &SMPpt[1];
458 gd->gd_CMAP2 = &SMPpt[2];
459 gd->gd_CMAP3 = &SMPpt[3];
460 gd->gd_PMAP1 = &SMPpt[4];
461 gd->gd_CADDR1 = CPU_prvspace[0].CPAGE1;
462 gd->gd_CADDR2 = CPU_prvspace[0].CPAGE2;
463 gd->gd_CADDR3 = CPU_prvspace[0].CPAGE3;
464 gd->gd_PADDR1 = (unsigned *)CPU_prvspace[0].PPAGE1;
471 * Set 4mb pdir for mp startup
476 if (pseflag && (cpu_feature & CPUID_PSE)) {
477 load_cr4(rcr4() | CR4_PSE);
478 if (pdir4mb && mycpu->gd_cpuid == 0) { /* only on BSP */
479 kernel_pmap->pm_pdir[KPTDI] =
480 PTD[KPTDI] = (pd_entry_t)pdir4mb;
488 * Initialize the pmap module.
489 * Called by vm_init, to initialize any structures that the pmap
490 * system needs to map virtual memory.
491 * pmap_init has been enhanced to support in a fairly consistant
492 * way, discontiguous physical memory.
495 pmap_init(phys_start, phys_end)
496 vm_paddr_t phys_start, phys_end;
502 * object for kernel page table pages
504 kptobj = vm_object_allocate(OBJT_DEFAULT, NKPDE);
507 * Allocate memory for random pmap data structures. Includes the
511 for(i = 0; i < vm_page_array_size; i++) {
514 m = &vm_page_array[i];
515 TAILQ_INIT(&m->md.pv_list);
516 m->md.pv_list_count = 0;
520 * init the pv free list
522 initial_pvs = vm_page_array_size;
523 if (initial_pvs < MINPV)
525 pvzone = &pvzone_store;
526 pvinit = (struct pv_entry *) kmem_alloc(kernel_map,
527 initial_pvs * sizeof (struct pv_entry));
528 zbootinit(pvzone, "PV ENTRY", sizeof (struct pv_entry), pvinit,
532 * Now it is safe to enable pv_table recording.
534 pmap_initialized = TRUE;
538 * Initialize the address space (zone) for the pv_entries. Set a
539 * high water mark so that the system can recover from excessive
540 * numbers of pv entries.
545 int shpgperproc = PMAP_SHPGPERPROC;
547 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
548 pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
549 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
550 pv_entry_high_water = 9 * (pv_entry_max / 10);
551 zinitna(pvzone, &pvzone_obj, NULL, 0, pv_entry_max, ZONE_INTERRUPT, 1);
555 /***************************************************
556 * Low level helper routines.....
557 ***************************************************/
559 #if defined(PMAP_DIAGNOSTIC)
562 * This code checks for non-writeable/modified pages.
563 * This should be an invalid condition.
566 pmap_nw_modified(pt_entry_t ptea)
572 if ((pte & (PG_M|PG_RW)) == PG_M)
581 * this routine defines the region(s) of memory that should
582 * not be tested for the modified bit.
584 static PMAP_INLINE int
585 pmap_track_modified(vm_offset_t va)
587 if ((va < clean_sva) || (va >= clean_eva))
593 static PMAP_INLINE void
594 invltlb_1pg(vm_offset_t va)
596 #if defined(I386_CPU)
597 if (cpu_class == CPUCLASS_386) {
607 pmap_TLB_invalidate(pmap_t pmap, vm_offset_t va)
610 if (pmap->pm_active & (1 << mycpu->gd_cpuid))
611 cpu_invlpg((void *)va);
612 if (pmap->pm_active & mycpu->gd_other_cpus)
621 pmap_TLB_invalidate_all(pmap_t pmap)
624 if (pmap->pm_active & (1 << mycpu->gd_cpuid))
626 if (pmap->pm_active & mycpu->gd_other_cpus)
635 get_ptbase(pmap_t pmap)
637 unsigned frame = (unsigned) pmap->pm_pdir[PTDPTDI] & PG_FRAME;
638 struct globaldata *gd = mycpu;
640 /* are we current address space or kernel? */
641 if (pmap == kernel_pmap || frame == (((unsigned) PTDpde) & PG_FRAME)) {
642 return (unsigned *) PTmap;
645 /* otherwise, we are alternate address space */
646 KKASSERT(gd->gd_intr_nesting_level == 0 && (gd->gd_curthread->td_flags & TDF_INTTHREAD) == 0);
648 if (frame != (((unsigned) APTDpde) & PG_FRAME)) {
649 APTDpde = (pd_entry_t)(frame | PG_RW | PG_V);
651 /* The page directory is not shared between CPUs */
657 return (unsigned *) APTmap;
663 * Extract the physical page address associated with the map/VA pair.
665 * This function may not be called from an interrupt if the pmap is
669 pmap_extract(pmap_t pmap, vm_offset_t va)
672 vm_offset_t pdirindex;
674 pdirindex = va >> PDRSHIFT;
675 if (pmap && (rtval = (unsigned) pmap->pm_pdir[pdirindex])) {
677 if ((rtval & PG_PS) != 0) {
678 rtval &= ~(NBPDR - 1);
679 rtval |= va & (NBPDR - 1);
682 pte = get_ptbase(pmap) + i386_btop(va);
683 rtval = ((*pte & PG_FRAME) | (va & PAGE_MASK));
690 * Extract user accessible page only, return NULL if the page is not
691 * present or if it's current state is not sufficient. Caller will
692 * generally call vm_fault() on failure and try again.
695 pmap_extract_vmpage(pmap_t pmap, vm_offset_t va, int prot)
698 vm_offset_t pdirindex;
700 pdirindex = va >> PDRSHIFT;
701 if (pmap && (rtval = (unsigned) pmap->pm_pdir[pdirindex])) {
705 if ((rtval & PG_PS) != 0) {
706 if ((rtval & (PG_V|PG_U)) != (PG_V|PG_U))
708 if ((prot & VM_PROT_WRITE) && (rtval & PG_RW) == 0)
710 rtval &= ~(NBPDR - 1);
711 rtval |= va & (NBPDR - 1);
712 m = PHYS_TO_VM_PAGE(rtval);
714 pte = get_ptbase(pmap) + i386_btop(va);
715 if ((*pte & (PG_V|PG_U)) != (PG_V|PG_U))
717 if ((prot & VM_PROT_WRITE) && (*pte & PG_RW) == 0)
719 rtval = ((*pte & PG_FRAME) | (va & PAGE_MASK));
720 m = PHYS_TO_VM_PAGE(rtval);
727 /***************************************************
728 * Low level mapping routines.....
729 ***************************************************/
732 * add a wired page to the kva
733 * note that in order for the mapping to take effect -- you
734 * should do a invltlb after doing the pmap_kenter...
737 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
742 npte = pa | PG_RW | PG_V | pgeflag;
743 pte = (unsigned *)vtopte(va);
750 * remove a page from the kernel pagetables
753 pmap_kremove(vm_offset_t va)
757 pte = (unsigned *)vtopte(va);
763 * Used to map a range of physical addresses into kernel
764 * virtual address space.
766 * For now, VM is already on, we only need to map the
770 pmap_map(vm_offset_t virt, vm_paddr_t start, vm_paddr_t end, int prot)
772 while (start < end) {
773 pmap_kenter(virt, start);
782 * Add a list of wired pages to the kva
783 * this routine is only used for temporary
784 * kernel mappings that do not need to have
785 * page modification or references recorded.
786 * Note that old mappings are simply written
787 * over. The page *must* be wired.
790 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
794 end_va = va + count * PAGE_SIZE;
796 while (va < end_va) {
799 pte = (unsigned *)vtopte(va);
800 *pte = VM_PAGE_TO_PHYS(*m) | PG_RW | PG_V | pgeflag;
802 cpu_invlpg((void *)va);
815 * this routine jerks page mappings from the
816 * kernel -- it is meant only for temporary mappings.
819 pmap_qremove(vm_offset_t va, int count)
823 end_va = va + count*PAGE_SIZE;
825 while (va < end_va) {
828 pte = (unsigned *)vtopte(va);
831 cpu_invlpg((void *)va);
843 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
847 m = vm_page_lookup(object, pindex);
848 if (m && vm_page_sleep_busy(m, FALSE, "pplookp"))
854 * Create a new thread and optionally associate it with a (new) process.
855 * NOTE! the new thread's cpu may not equal the current cpu.
858 pmap_init_thread(thread_t td)
860 td->td_pcb = (struct pcb *)(td->td_kstack + UPAGES * PAGE_SIZE) - 1;
861 td->td_sp = (char *)td->td_pcb - 16;
865 * Create the UPAGES for a new process.
866 * This routine directly affects the fork perf for a process.
869 pmap_init_proc(struct proc *p, struct thread *td)
871 p->p_addr = (void *)td->td_kstack;
874 td->td_switch = cpu_heavy_switch;
878 bzero(p->p_addr, sizeof(*p->p_addr));
882 * Dispose the UPAGES for a process that has exited.
883 * This routine directly impacts the exit perf of a process.
886 pmap_dispose_proc(struct proc *p)
890 KASSERT(p->p_lock == 0, ("attempt to dispose referenced proc! %p", p));
892 if ((td = p->p_thread) != NULL) {
901 * Allow the UPAGES for a process to be prejudicially paged out.
904 pmap_swapout_proc(struct proc *p)
911 upobj = p->p_upages_obj;
913 * let the upages be paged
915 for(i=0;i<UPAGES;i++) {
916 if ((m = vm_page_lookup(upobj, i)) == NULL)
917 panic("pmap_swapout_proc: upage already missing???");
919 vm_page_unwire(m, 0);
920 pmap_kremove( (vm_offset_t) p->p_addr + PAGE_SIZE * i);
926 * Bring the UPAGES for a specified process back in.
929 pmap_swapin_proc(struct proc *p)
936 upobj = p->p_upages_obj;
937 for(i=0;i<UPAGES;i++) {
939 m = vm_page_grab(upobj, i, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
941 pmap_kenter(((vm_offset_t) p->p_addr) + i * PAGE_SIZE,
944 if (m->valid != VM_PAGE_BITS_ALL) {
945 rv = vm_pager_get_pages(upobj, &m, 1, 0);
946 if (rv != VM_PAGER_OK)
947 panic("pmap_swapin_proc: cannot get upages for proc: %d\n", p->p_pid);
948 m = vm_page_lookup(upobj, i);
949 m->valid = VM_PAGE_BITS_ALL;
954 vm_page_flag_set(m, PG_MAPPED | PG_WRITEABLE);
959 /***************************************************
960 * Page table page management routines.....
961 ***************************************************/
964 * This routine unholds page table pages, and if the hold count
965 * drops to zero, then it decrements the wire count.
968 _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
970 while (vm_page_sleep_busy(m, FALSE, "pmuwpt"))
973 if (m->hold_count == 0) {
976 * unmap the page table page
978 pmap->pm_pdir[m->pindex] = 0;
979 --pmap->pm_stats.resident_count;
980 if ((((unsigned)pmap->pm_pdir[PTDPTDI]) & PG_FRAME) ==
981 (((unsigned) PTDpde) & PG_FRAME)) {
983 * Do a invltlb to make the invalidated mapping
984 * take effect immediately.
986 pteva = UPT_MIN_ADDRESS + i386_ptob(m->pindex);
987 pmap_TLB_invalidate(pmap, pteva);
990 if (pmap->pm_ptphint == m)
991 pmap->pm_ptphint = NULL;
994 * If the page is finally unwired, simply free it.
997 if (m->wire_count == 0) {
1001 vm_page_free_zero(m);
1002 --vmstats.v_wire_count;
1009 static PMAP_INLINE int
1010 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
1013 if (m->hold_count == 0)
1014 return _pmap_unwire_pte_hold(pmap, m);
1020 * After removing a page table entry, this routine is used to
1021 * conditionally free the page, and manage the hold/wire counts.
1024 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte)
1027 if (va >= UPT_MIN_ADDRESS)
1031 ptepindex = (va >> PDRSHIFT);
1032 if (pmap->pm_ptphint &&
1033 (pmap->pm_ptphint->pindex == ptepindex)) {
1034 mpte = pmap->pm_ptphint;
1036 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1037 pmap->pm_ptphint = mpte;
1041 return pmap_unwire_pte_hold(pmap, mpte);
1045 pmap_pinit0(struct pmap *pmap)
1048 (pd_entry_t *)kmem_alloc_pageable(kernel_map, PAGE_SIZE);
1049 pmap_kenter((vm_offset_t) pmap->pm_pdir, (vm_offset_t) IdlePTD);
1051 pmap->pm_active = 0;
1052 pmap->pm_ptphint = NULL;
1053 TAILQ_INIT(&pmap->pm_pvlist);
1054 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1058 * Initialize a preallocated and zeroed pmap structure,
1059 * such as one in a vmspace structure.
1062 pmap_pinit(struct pmap *pmap)
1067 * No need to allocate page table space yet but we do need a valid
1068 * page directory table.
1070 if (pmap->pm_pdir == NULL) {
1072 (pd_entry_t *)kmem_alloc_pageable(kernel_map, PAGE_SIZE);
1076 * allocate object for the ptes
1078 if (pmap->pm_pteobj == NULL)
1079 pmap->pm_pteobj = vm_object_allocate( OBJT_DEFAULT, PTDPTDI + 1);
1082 * allocate the page directory page
1084 ptdpg = vm_page_grab( pmap->pm_pteobj, PTDPTDI,
1085 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1087 ptdpg->wire_count = 1;
1088 ++vmstats.v_wire_count;
1091 vm_page_flag_clear(ptdpg, PG_MAPPED | PG_BUSY); /* not usually mapped*/
1092 ptdpg->valid = VM_PAGE_BITS_ALL;
1094 pmap_kenter((vm_offset_t) pmap->pm_pdir, VM_PAGE_TO_PHYS(ptdpg));
1095 if ((ptdpg->flags & PG_ZERO) == 0)
1096 bzero(pmap->pm_pdir, PAGE_SIZE);
1098 pmap->pm_pdir[MPPTDI] = PTD[MPPTDI];
1100 /* install self-referential address mapping entry */
1101 *(unsigned *) (pmap->pm_pdir + PTDPTDI) =
1102 VM_PAGE_TO_PHYS(ptdpg) | PG_V | PG_RW | PG_A | PG_M;
1105 pmap->pm_active = 0;
1106 pmap->pm_ptphint = NULL;
1107 TAILQ_INIT(&pmap->pm_pvlist);
1108 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1112 * Wire in kernel global address entries. To avoid a race condition
1113 * between pmap initialization and pmap_growkernel, this procedure
1114 * should be called after the vmspace is attached to the process
1115 * but before this pmap is activated.
1118 pmap_pinit2(struct pmap *pmap)
1120 /* XXX copies current process, does not fill in MPPTDI */
1121 bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * PTESIZE);
1125 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1127 unsigned *pde = (unsigned *) pmap->pm_pdir;
1129 * This code optimizes the case of freeing non-busy
1130 * page-table pages. Those pages are zero now, and
1131 * might as well be placed directly into the zero queue.
1133 if (vm_page_sleep_busy(p, FALSE, "pmaprl"))
1139 * Remove the page table page from the processes address space.
1142 pmap->pm_stats.resident_count--;
1144 if (p->hold_count) {
1145 panic("pmap_release: freeing held page table page");
1148 * Page directory pages need to have the kernel
1149 * stuff cleared, so they can go into the zero queue also.
1151 if (p->pindex == PTDPTDI) {
1152 bzero(pde + KPTDI, nkpt * PTESIZE);
1155 pmap_kremove((vm_offset_t) pmap->pm_pdir);
1158 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1159 pmap->pm_ptphint = NULL;
1162 vmstats.v_wire_count--;
1163 vm_page_free_zero(p);
1168 * this routine is called if the page table page is not
1172 _pmap_allocpte(pmap_t pmap, unsigned ptepindex)
1174 vm_offset_t pteva, ptepa;
1178 * Find or fabricate a new pagetable page
1180 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1181 VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1183 KASSERT(m->queue == PQ_NONE,
1184 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1186 if (m->wire_count == 0)
1187 vmstats.v_wire_count++;
1191 * Increment the hold count for the page table page
1192 * (denoting a new mapping.)
1197 * Map the pagetable page into the process address space, if
1198 * it isn't already there.
1201 pmap->pm_stats.resident_count++;
1203 ptepa = VM_PAGE_TO_PHYS(m);
1204 pmap->pm_pdir[ptepindex] =
1205 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_A | PG_M);
1208 * Set the page table hint
1210 pmap->pm_ptphint = m;
1213 * Try to use the new mapping, but if we cannot, then
1214 * do it with the routine that maps the page explicitly.
1216 if ((m->flags & PG_ZERO) == 0) {
1217 if ((((unsigned)pmap->pm_pdir[PTDPTDI]) & PG_FRAME) ==
1218 (((unsigned) PTDpde) & PG_FRAME)) {
1219 pteva = UPT_MIN_ADDRESS + i386_ptob(ptepindex);
1220 bzero((caddr_t) pteva, PAGE_SIZE);
1222 pmap_zero_page(ptepa);
1226 m->valid = VM_PAGE_BITS_ALL;
1227 vm_page_flag_clear(m, PG_ZERO);
1228 vm_page_flag_set(m, PG_MAPPED);
1235 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1242 * Calculate pagetable page index
1244 ptepindex = va >> PDRSHIFT;
1247 * Get the page directory entry
1249 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1252 * This supports switching from a 4MB page to a
1255 if (ptepa & PG_PS) {
1256 pmap->pm_pdir[ptepindex] = 0;
1262 * If the page table page is mapped, we just increment the
1263 * hold count, and activate it.
1267 * In order to get the page table page, try the
1270 if (pmap->pm_ptphint &&
1271 (pmap->pm_ptphint->pindex == ptepindex)) {
1272 m = pmap->pm_ptphint;
1274 m = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1275 pmap->pm_ptphint = m;
1281 * Here if the pte page isn't mapped, or if it has been deallocated.
1283 return _pmap_allocpte(pmap, ptepindex);
1287 /***************************************************
1288 * Pmap allocation/deallocation routines.
1289 ***************************************************/
1292 * Release any resources held by the given physical map.
1293 * Called when a pmap initialized by pmap_pinit is being released.
1294 * Should only be called if the map contains no valid mappings.
1297 pmap_release(struct pmap *pmap)
1299 vm_page_t p,n,ptdpg;
1300 vm_object_t object = pmap->pm_pteobj;
1303 #if defined(DIAGNOSTIC)
1304 if (object->ref_count != 1)
1305 panic("pmap_release: pteobj reference count != 1");
1310 curgeneration = object->generation;
1311 for (p = TAILQ_FIRST(&object->memq); p != NULL; p = n) {
1312 n = TAILQ_NEXT(p, listq);
1313 if (p->pindex == PTDPTDI) {
1318 if (!pmap_release_free_page(pmap, p) &&
1319 (object->generation != curgeneration))
1324 if (ptdpg && !pmap_release_free_page(pmap, ptdpg))
1329 kvm_size(SYSCTL_HANDLER_ARGS)
1331 unsigned long ksize = VM_MAX_KERNEL_ADDRESS - KERNBASE;
1333 return sysctl_handle_long(oidp, &ksize, 0, req);
1335 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD,
1336 0, 0, kvm_size, "IU", "Size of KVM");
1339 kvm_free(SYSCTL_HANDLER_ARGS)
1341 unsigned long kfree = VM_MAX_KERNEL_ADDRESS - kernel_vm_end;
1343 return sysctl_handle_long(oidp, &kfree, 0, req);
1345 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD,
1346 0, 0, kvm_free, "IU", "Amount of KVM free");
1349 * grow the number of kernel page table entries, if needed
1352 pmap_growkernel(vm_offset_t addr)
1357 vm_offset_t ptppaddr;
1362 if (kernel_vm_end == 0) {
1363 kernel_vm_end = KERNBASE;
1365 while (pdir_pde(PTD, kernel_vm_end)) {
1366 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1370 addr = (addr + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1371 while (kernel_vm_end < addr) {
1372 if (pdir_pde(PTD, kernel_vm_end)) {
1373 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1378 * This index is bogus, but out of the way
1380 nkpg = vm_page_alloc(kptobj, nkpt,
1381 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM | VM_ALLOC_INTERRUPT);
1383 panic("pmap_growkernel: no memory to grow kernel");
1388 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1389 pmap_zero_page(ptppaddr);
1390 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
1391 pdir_pde(PTD, kernel_vm_end) = newpdir;
1393 FOREACH_PROC_IN_SYSTEM(p) {
1395 pmap = vmspace_pmap(p->p_vmspace);
1396 *pmap_pde(pmap, kernel_vm_end) = newpdir;
1399 *pmap_pde(kernel_pmap, kernel_vm_end) = newpdir;
1400 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1406 * Retire the given physical map from service.
1407 * Should only be called if the map contains
1408 * no valid mappings.
1411 pmap_destroy(pmap_t pmap)
1418 count = --pmap->pm_count;
1421 panic("destroying a pmap is not yet implemented");
1426 * Add a reference to the specified pmap.
1429 pmap_reference(pmap_t pmap)
1436 /***************************************************
1437 * page management routines.
1438 ***************************************************/
1441 * free the pv_entry back to the free list. This function may be
1442 * called from an interrupt.
1444 static PMAP_INLINE void
1445 free_pv_entry(pv_entry_t pv)
1452 * get a new pv_entry, allocating a block from the system
1453 * when needed. This function may be called from an interrupt.
1459 if (pv_entry_high_water &&
1460 (pv_entry_count > pv_entry_high_water) &&
1461 (pmap_pagedaemon_waken == 0)) {
1462 pmap_pagedaemon_waken = 1;
1463 wakeup (&vm_pages_needed);
1465 return zalloc(pvzone);
1469 * This routine is very drastic, but can save the system
1477 static int warningdone=0;
1479 if (pmap_pagedaemon_waken == 0)
1482 if (warningdone < 5) {
1483 printf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1487 for(i = 0; i < vm_page_array_size; i++) {
1488 m = &vm_page_array[i];
1489 if (m->wire_count || m->hold_count || m->busy ||
1490 (m->flags & PG_BUSY))
1494 pmap_pagedaemon_waken = 0;
1499 * If it is the first entry on the list, it is actually
1500 * in the header and we must copy the following entry up
1501 * to the header. Otherwise we must search the list for
1502 * the entry. In either case we free the now unused entry.
1506 pmap_remove_entry(struct pmap *pmap, vm_page_t m, vm_offset_t va)
1513 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1514 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1515 if (pmap == pv->pv_pmap && va == pv->pv_va)
1519 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1520 if (va == pv->pv_va)
1528 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem);
1529 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1530 m->md.pv_list_count--;
1531 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
1532 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1534 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1543 * Create a pv entry for page at pa for
1547 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
1553 pv = get_pv_entry();
1558 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1559 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1560 m->md.pv_list_count++;
1566 * pmap_remove_pte: do the things to unmap a page in a process
1569 pmap_remove_pte(struct pmap *pmap, unsigned *ptq, vm_offset_t va)
1574 oldpte = loadandclear(ptq);
1576 pmap->pm_stats.wired_count -= 1;
1578 * Machines that don't support invlpg, also don't support
1583 pmap->pm_stats.resident_count -= 1;
1584 if (oldpte & PG_MANAGED) {
1585 m = PHYS_TO_VM_PAGE(oldpte);
1586 if (oldpte & PG_M) {
1587 #if defined(PMAP_DIAGNOSTIC)
1588 if (pmap_nw_modified((pt_entry_t) oldpte)) {
1590 "pmap_remove: modified page not writable: va: 0x%x, pte: 0x%x\n",
1594 if (pmap_track_modified(va))
1598 vm_page_flag_set(m, PG_REFERENCED);
1599 return pmap_remove_entry(pmap, m, va);
1601 return pmap_unuse_pt(pmap, va, NULL);
1610 * Remove a single page from a process address space.
1612 * This function may not be called from an interrupt if the pmap is
1616 pmap_remove_page(struct pmap *pmap, vm_offset_t va)
1621 * if there is no pte for this address, just skip it!!! Otherwise
1622 * get a local va for mappings for this pmap and remove the entry.
1624 if (*pmap_pde(pmap, va) != 0) {
1625 ptq = get_ptbase(pmap) + i386_btop(va);
1627 (void) pmap_remove_pte(pmap, ptq, va);
1628 pmap_TLB_invalidate(pmap, va);
1636 * Remove the given range of addresses from the specified map.
1638 * It is assumed that the start and end are properly
1639 * rounded to the page size.
1641 * This function may not be called from an interrupt if the pmap is
1645 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
1649 vm_offset_t ptpaddr;
1650 vm_offset_t sindex, eindex;
1656 if (pmap->pm_stats.resident_count == 0)
1660 * special handling of removing one page. a very
1661 * common operation and easy to short circuit some
1664 if (((sva + PAGE_SIZE) == eva) &&
1665 (((unsigned) pmap->pm_pdir[(sva >> PDRSHIFT)] & PG_PS) == 0)) {
1666 pmap_remove_page(pmap, sva);
1673 * Get a local virtual address for the mappings that are being
1676 ptbase = get_ptbase(pmap);
1678 sindex = i386_btop(sva);
1679 eindex = i386_btop(eva);
1681 for (; sindex < eindex; sindex = pdnxt) {
1685 * Calculate index for next page table.
1687 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1688 if (pmap->pm_stats.resident_count == 0)
1691 pdirindex = sindex / NPDEPG;
1692 if (((ptpaddr = (unsigned) pmap->pm_pdir[pdirindex]) & PG_PS) != 0) {
1693 pmap->pm_pdir[pdirindex] = 0;
1694 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1700 * Weed out invalid mappings. Note: we assume that the page
1701 * directory table is always allocated, and in kernel virtual.
1707 * Limit our scan to either the end of the va represented
1708 * by the current page table page, or to the end of the
1709 * range being removed.
1711 if (pdnxt > eindex) {
1715 for ( ;sindex != pdnxt; sindex++) {
1717 if (ptbase[sindex] == 0) {
1720 va = i386_ptob(sindex);
1723 if (pmap_remove_pte(pmap,
1724 ptbase + sindex, va))
1730 pmap_TLB_invalidate_all(pmap);
1736 * Removes this physical page from all physical maps in which it resides.
1737 * Reflects back modify bits to the pager.
1739 * This routine may not be called from an interrupt.
1743 pmap_remove_all(vm_page_t m)
1746 unsigned *pte, tpte;
1749 #if defined(PMAP_DIAGNOSTIC)
1751 * XXX this makes pmap_page_protect(NONE) illegal for non-managed
1754 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
1755 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%08llx", (long long)VM_PAGE_TO_PHYS(m));
1760 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
1761 pv->pv_pmap->pm_stats.resident_count--;
1763 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
1765 tpte = loadandclear(pte);
1767 pv->pv_pmap->pm_stats.wired_count--;
1770 vm_page_flag_set(m, PG_REFERENCED);
1773 * Update the vm_page_t clean and reference bits.
1776 #if defined(PMAP_DIAGNOSTIC)
1777 if (pmap_nw_modified((pt_entry_t) tpte)) {
1779 "pmap_remove_all: modified page not writable: va: 0x%x, pte: 0x%x\n",
1783 if (pmap_track_modified(pv->pv_va))
1786 pmap_TLB_invalidate(pv->pv_pmap, pv->pv_va);
1788 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
1789 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1790 m->md.pv_list_count--;
1791 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
1795 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1803 * Set the physical protection on the specified range of this map
1806 * This function may not be called from an interrupt if the map is
1807 * not the kernel_pmap.
1810 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
1813 vm_offset_t pdnxt, ptpaddr;
1814 vm_pindex_t sindex, eindex;
1820 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1821 pmap_remove(pmap, sva, eva);
1825 if (prot & VM_PROT_WRITE)
1830 ptbase = get_ptbase(pmap);
1832 sindex = i386_btop(sva);
1833 eindex = i386_btop(eva);
1835 for (; sindex < eindex; sindex = pdnxt) {
1839 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1841 pdirindex = sindex / NPDEPG;
1842 if (((ptpaddr = (unsigned) pmap->pm_pdir[pdirindex]) & PG_PS) != 0) {
1843 (unsigned) pmap->pm_pdir[pdirindex] &= ~(PG_M|PG_RW);
1844 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1850 * Weed out invalid mappings. Note: we assume that the page
1851 * directory table is always allocated, and in kernel virtual.
1856 if (pdnxt > eindex) {
1860 for (; sindex != pdnxt; sindex++) {
1865 pbits = ptbase[sindex];
1867 if (pbits & PG_MANAGED) {
1870 m = PHYS_TO_VM_PAGE(pbits);
1871 vm_page_flag_set(m, PG_REFERENCED);
1875 if (pmap_track_modified(i386_ptob(sindex))) {
1877 m = PHYS_TO_VM_PAGE(pbits);
1886 if (pbits != ptbase[sindex]) {
1887 ptbase[sindex] = pbits;
1893 pmap_TLB_invalidate_all(pmap);
1897 * Insert the given physical page (p) at
1898 * the specified virtual address (v) in the
1899 * target physical map with the protection requested.
1901 * If specified, the page will be wired down, meaning
1902 * that the related pte can not be reclaimed.
1904 * NB: This is the only routine which MAY NOT lazy-evaluate
1905 * or lose information. That is, this routine must actually
1906 * insert this page into the given map NOW.
1909 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1915 vm_offset_t origpte, newpte;
1922 #ifdef PMAP_DIAGNOSTIC
1923 if (va > VM_MAX_KERNEL_ADDRESS)
1924 panic("pmap_enter: toobig");
1925 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
1926 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%x)", va);
1931 * In the case that a page table page is not
1932 * resident, we are creating it here.
1934 if (va < UPT_MIN_ADDRESS) {
1935 mpte = pmap_allocpte(pmap, va);
1937 #if 0 && defined(PMAP_DIAGNOSTIC)
1939 vm_offset_t *pdeaddr = (vm_offset_t *)pmap_pde(pmap, va);
1940 if (((origpte = (vm_offset_t) *pdeaddr) & PG_V) == 0) {
1941 panic("pmap_enter: invalid kernel page table page(0), pdir=%p, pde=%p, va=%p\n",
1942 pmap->pm_pdir[PTDPTDI], origpte, va);
1945 pdeaddr = (vm_offset_t *) IdlePTDS[cpuid];
1946 if (((newpte = pdeaddr[va >> PDRSHIFT]) & PG_V) == 0) {
1947 if ((vm_offset_t) my_idlePTD != (vm_offset_t) vtophys(pdeaddr))
1948 printf("pde mismatch: %x, %x\n", my_idlePTD, pdeaddr);
1949 printf("cpuid: %d, pdeaddr: 0x%x\n", cpuid, pdeaddr);
1950 panic("pmap_enter: invalid kernel page table page(1), pdir=%p, npde=%p, pde=%p, va=%p\n",
1951 pmap->pm_pdir[PTDPTDI], newpte, origpte, va);
1957 pte = pmap_pte(pmap, va);
1960 * Page Directory table entry not valid, we need a new PT page
1963 panic("pmap_enter: invalid page directory pdir=%x, va=0x%x\n",
1964 (unsigned) pmap->pm_pdir[PTDPTDI], va);
1967 pa = VM_PAGE_TO_PHYS(m) & PG_FRAME;
1968 origpte = *(vm_offset_t *)pte;
1969 opa = origpte & PG_FRAME;
1971 if (origpte & PG_PS)
1972 panic("pmap_enter: attempted pmap_enter on 4MB page");
1975 * Mapping has not changed, must be protection or wiring change.
1977 if (origpte && (opa == pa)) {
1979 * Wiring change, just update stats. We don't worry about
1980 * wiring PT pages as they remain resident as long as there
1981 * are valid mappings in them. Hence, if a user page is wired,
1982 * the PT page will be also.
1984 if (wired && ((origpte & PG_W) == 0))
1985 pmap->pm_stats.wired_count++;
1986 else if (!wired && (origpte & PG_W))
1987 pmap->pm_stats.wired_count--;
1989 #if defined(PMAP_DIAGNOSTIC)
1990 if (pmap_nw_modified((pt_entry_t) origpte)) {
1992 "pmap_enter: modified page not writable: va: 0x%x, pte: 0x%x\n",
1998 * Remove extra pte reference
2003 if ((prot & VM_PROT_WRITE) && (origpte & PG_V)) {
2004 if ((origpte & PG_RW) == 0) {
2007 cpu_invlpg((void *)va);
2008 if (pmap->pm_active & mycpu->gd_other_cpus)
2018 * We might be turning off write access to the page,
2019 * so we go ahead and sense modify status.
2021 if (origpte & PG_MANAGED) {
2022 if ((origpte & PG_M) && pmap_track_modified(va)) {
2024 om = PHYS_TO_VM_PAGE(opa);
2032 * Mapping has changed, invalidate old range and fall through to
2033 * handle validating new mapping.
2037 err = pmap_remove_pte(pmap, pte, va);
2039 panic("pmap_enter: pte vanished, va: 0x%x", va);
2043 * Enter on the PV list if part of our managed memory. Note that we
2044 * raise IPL while manipulating pv_table since pmap_enter can be
2045 * called at interrupt time.
2047 if (pmap_initialized &&
2048 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2049 pmap_insert_entry(pmap, va, mpte, m);
2054 * Increment counters
2056 pmap->pm_stats.resident_count++;
2058 pmap->pm_stats.wired_count++;
2062 * Now validate mapping with desired protection/wiring.
2064 newpte = (vm_offset_t) (pa | pte_prot(pmap, prot) | PG_V);
2068 if (va < UPT_MIN_ADDRESS)
2070 if (pmap == kernel_pmap)
2074 * if the mapping or permission bits are different, we need
2075 * to update the pte.
2077 if ((origpte & ~(PG_M|PG_A)) != newpte) {
2078 *pte = newpte | PG_A;
2081 cpu_invlpg((void *)va);
2082 if (pmap->pm_active & mycpu->gd_other_cpus)
2092 * this code makes some *MAJOR* assumptions:
2093 * 1. Current pmap & pmap exists.
2096 * 4. No page table pages.
2097 * 5. Tlbflush is deferred to calling procedure.
2098 * 6. Page IS managed.
2099 * but is *MUCH* faster than pmap_enter...
2103 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_page_t mpte)
2109 * In the case that a page table page is not
2110 * resident, we are creating it here.
2112 if (va < UPT_MIN_ADDRESS) {
2117 * Calculate pagetable page index
2119 ptepindex = va >> PDRSHIFT;
2120 if (mpte && (mpte->pindex == ptepindex)) {
2125 * Get the page directory entry
2127 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
2130 * If the page table page is mapped, we just increment
2131 * the hold count, and activate it.
2135 panic("pmap_enter_quick: unexpected mapping into 4MB page");
2136 if (pmap->pm_ptphint &&
2137 (pmap->pm_ptphint->pindex == ptepindex)) {
2138 mpte = pmap->pm_ptphint;
2140 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
2141 pmap->pm_ptphint = mpte;
2147 mpte = _pmap_allocpte(pmap, ptepindex);
2155 * This call to vtopte makes the assumption that we are
2156 * entering the page into the current pmap. In order to support
2157 * quick entry into any pmap, one would likely use pmap_pte_quick.
2158 * But that isn't as quick as vtopte.
2160 pte = (unsigned *)vtopte(va);
2163 pmap_unwire_pte_hold(pmap, mpte);
2168 * Enter on the PV list if part of our managed memory. Note that we
2169 * raise IPL while manipulating pv_table since pmap_enter can be
2170 * called at interrupt time.
2172 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0)
2173 pmap_insert_entry(pmap, va, mpte, m);
2176 * Increment counters
2178 pmap->pm_stats.resident_count++;
2180 pa = VM_PAGE_TO_PHYS(m);
2183 * Now validate mapping with RO protection
2185 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2186 *pte = pa | PG_V | PG_U;
2188 *pte = pa | PG_V | PG_U | PG_MANAGED;
2194 * Make a temporary mapping for a physical address. This is only intended
2195 * to be used for panic dumps.
2198 pmap_kenter_temporary(vm_paddr_t pa, int i)
2200 pmap_kenter((vm_offset_t)crashdumpmap + (i * PAGE_SIZE), pa);
2201 return ((void *)crashdumpmap);
2204 #define MAX_INIT_PT (96)
2206 * pmap_object_init_pt preloads the ptes for a given object
2207 * into the specified pmap. This eliminates the blast of soft
2208 * faults on process startup and immediately after an mmap.
2211 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_object_t object,
2212 vm_pindex_t pindex, vm_size_t size, int limit)
2219 if (pmap == NULL || object == NULL)
2223 * This code maps large physical mmap regions into the
2224 * processor address space. Note that some shortcuts
2225 * are taken, but the code works.
2228 (object->type == OBJT_DEVICE) &&
2229 ((addr & (NBPDR - 1)) == 0) &&
2230 ((size & (NBPDR - 1)) == 0) ) {
2233 unsigned int ptepindex;
2237 if (pmap->pm_pdir[ptepindex = (addr >> PDRSHIFT)])
2241 p = vm_page_lookup(object, pindex);
2242 if (p && vm_page_sleep_busy(p, FALSE, "init4p"))
2246 p = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL);
2251 if (vm_pager_get_pages(object, m, 1, 0) != VM_PAGER_OK) {
2256 p = vm_page_lookup(object, pindex);
2260 ptepa = (vm_offset_t) VM_PAGE_TO_PHYS(p);
2261 if (ptepa & (NBPDR - 1)) {
2265 p->valid = VM_PAGE_BITS_ALL;
2267 pmap->pm_stats.resident_count += size >> PAGE_SHIFT;
2268 npdes = size >> PDRSHIFT;
2269 for(i=0;i<npdes;i++) {
2270 pmap->pm_pdir[ptepindex] =
2271 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_PS);
2275 vm_page_flag_set(p, PG_MAPPED);
2280 psize = i386_btop(size);
2282 if ((object->type != OBJT_VNODE) ||
2283 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2284 (object->resident_page_count > MAX_INIT_PT))) {
2288 if (psize + pindex > object->size) {
2289 if (object->size < pindex)
2291 psize = object->size - pindex;
2296 * if we are processing a major portion of the object, then scan the
2299 if (psize > (object->resident_page_count >> 2)) {
2302 for (p = TAILQ_FIRST(&object->memq);
2303 ((objpgs > 0) && (p != NULL));
2304 p = TAILQ_NEXT(p, listq)) {
2307 if (tmpidx < pindex) {
2311 if (tmpidx >= psize) {
2315 * don't allow an madvise to blow away our really
2316 * free pages allocating pv entries.
2318 if ((limit & MAP_PREFAULT_MADVISE) &&
2319 vmstats.v_free_count < vmstats.v_free_reserved) {
2322 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2324 (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2325 if ((p->queue - p->pc) == PQ_CACHE)
2326 vm_page_deactivate(p);
2328 mpte = pmap_enter_quick(pmap,
2329 addr + i386_ptob(tmpidx), p, mpte);
2330 vm_page_flag_set(p, PG_MAPPED);
2337 * else lookup the pages one-by-one.
2339 for (tmpidx = 0; tmpidx < psize; tmpidx += 1) {
2341 * don't allow an madvise to blow away our really
2342 * free pages allocating pv entries.
2344 if ((limit & MAP_PREFAULT_MADVISE) &&
2345 vmstats.v_free_count < vmstats.v_free_reserved) {
2348 p = vm_page_lookup(object, tmpidx + pindex);
2350 ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2352 (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2353 if ((p->queue - p->pc) == PQ_CACHE)
2354 vm_page_deactivate(p);
2356 mpte = pmap_enter_quick(pmap,
2357 addr + i386_ptob(tmpidx), p, mpte);
2358 vm_page_flag_set(p, PG_MAPPED);
2367 * pmap_prefault provides a quick way of clustering
2368 * pagefaults into a processes address space. It is a "cousin"
2369 * of pmap_object_init_pt, except it runs at page fault time instead
2374 #define PAGEORDER_SIZE (PFBAK+PFFOR)
2376 static int pmap_prefault_pageorder[] = {
2377 -PAGE_SIZE, PAGE_SIZE,
2378 -2 * PAGE_SIZE, 2 * PAGE_SIZE,
2379 -3 * PAGE_SIZE, 3 * PAGE_SIZE
2380 -4 * PAGE_SIZE, 4 * PAGE_SIZE
2384 pmap_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry)
2393 if (!curproc || (pmap != vmspace_pmap(curproc->p_vmspace)))
2396 object = entry->object.vm_object;
2398 starta = addra - PFBAK * PAGE_SIZE;
2399 if (starta < entry->start) {
2400 starta = entry->start;
2401 } else if (starta > addra) {
2406 for (i = 0; i < PAGEORDER_SIZE; i++) {
2407 vm_object_t lobject;
2410 addr = addra + pmap_prefault_pageorder[i];
2411 if (addr > addra + (PFFOR * PAGE_SIZE))
2414 if (addr < starta || addr >= entry->end)
2417 if ((*pmap_pde(pmap, addr)) == NULL)
2420 pte = (unsigned *) vtopte(addr);
2424 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2426 for (m = vm_page_lookup(lobject, pindex);
2427 (!m && (lobject->type == OBJT_DEFAULT) && (lobject->backing_object));
2428 lobject = lobject->backing_object) {
2429 if (lobject->backing_object_offset & PAGE_MASK)
2431 pindex += (lobject->backing_object_offset >> PAGE_SHIFT);
2432 m = vm_page_lookup(lobject->backing_object, pindex);
2436 * give-up when a page is not in memory
2441 if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2443 (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2445 if ((m->queue - m->pc) == PQ_CACHE) {
2446 vm_page_deactivate(m);
2449 mpte = pmap_enter_quick(pmap, addr, m, mpte);
2450 vm_page_flag_set(m, PG_MAPPED);
2457 * Routine: pmap_change_wiring
2458 * Function: Change the wiring attribute for a map/virtual-address
2460 * In/out conditions:
2461 * The mapping must already exist in the pmap.
2464 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2471 pte = pmap_pte(pmap, va);
2473 if (wired && !pmap_pte_w(pte))
2474 pmap->pm_stats.wired_count++;
2475 else if (!wired && pmap_pte_w(pte))
2476 pmap->pm_stats.wired_count--;
2479 * Wiring is not a hardware characteristic so there is no need to
2482 pmap_pte_set_w(pte, wired);
2488 * Copy the range specified by src_addr/len
2489 * from the source map to the range dst_addr/len
2490 * in the destination map.
2492 * This routine is only advisory and need not do anything.
2495 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2496 vm_size_t len, vm_offset_t src_addr)
2499 vm_offset_t end_addr = src_addr + len;
2501 unsigned src_frame, dst_frame;
2504 if (dst_addr != src_addr)
2507 src_frame = ((unsigned) src_pmap->pm_pdir[PTDPTDI]) & PG_FRAME;
2508 if (src_frame != (((unsigned) PTDpde) & PG_FRAME)) {
2512 dst_frame = ((unsigned) dst_pmap->pm_pdir[PTDPTDI]) & PG_FRAME;
2513 if (dst_frame != (((unsigned) APTDpde) & PG_FRAME)) {
2514 APTDpde = (pd_entry_t) (dst_frame | PG_RW | PG_V);
2516 /* The page directory is not shared between CPUs */
2523 for(addr = src_addr; addr < end_addr; addr = pdnxt) {
2524 unsigned *src_pte, *dst_pte;
2525 vm_page_t dstmpte, srcmpte;
2526 vm_offset_t srcptepaddr;
2529 if (addr >= UPT_MIN_ADDRESS)
2530 panic("pmap_copy: invalid to pmap_copy page tables\n");
2533 * Don't let optional prefaulting of pages make us go
2534 * way below the low water mark of free pages or way
2535 * above high water mark of used pv entries.
2537 if (vmstats.v_free_count < vmstats.v_free_reserved ||
2538 pv_entry_count > pv_entry_high_water)
2541 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
2542 ptepindex = addr >> PDRSHIFT;
2544 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex];
2545 if (srcptepaddr == 0)
2548 if (srcptepaddr & PG_PS) {
2549 if (dst_pmap->pm_pdir[ptepindex] == 0) {
2550 dst_pmap->pm_pdir[ptepindex] = (pd_entry_t) srcptepaddr;
2551 dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
2556 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
2557 if ((srcmpte == NULL) ||
2558 (srcmpte->hold_count == 0) || (srcmpte->flags & PG_BUSY))
2561 if (pdnxt > end_addr)
2564 src_pte = (unsigned *) vtopte(addr);
2565 dst_pte = (unsigned *) avtopte(addr);
2566 while (addr < pdnxt) {
2570 * we only virtual copy managed pages
2572 if ((ptetemp & PG_MANAGED) != 0) {
2574 * We have to check after allocpte for the
2575 * pte still being around... allocpte can
2578 dstmpte = pmap_allocpte(dst_pmap, addr);
2579 if ((*dst_pte == 0) && (ptetemp = *src_pte)) {
2581 * Clear the modified and
2582 * accessed (referenced) bits
2585 m = PHYS_TO_VM_PAGE(ptetemp);
2586 *dst_pte = ptetemp & ~(PG_M | PG_A);
2587 dst_pmap->pm_stats.resident_count++;
2588 pmap_insert_entry(dst_pmap, addr,
2591 pmap_unwire_pte_hold(dst_pmap, dstmpte);
2593 if (dstmpte->hold_count >= srcmpte->hold_count)
2604 * Routine: pmap_kernel
2606 * Returns the physical map handle for the kernel.
2611 return (kernel_pmap);
2617 * Zero the specified PA by mapping the page into KVM and clearing its
2620 * This function may be called from an interrupt and no locking is
2624 pmap_zero_page(vm_paddr_t phys)
2626 struct mdglobaldata *gd = mdcpu;
2629 if (*(int *)gd->gd_CMAP3)
2630 panic("pmap_zero_page: CMAP3 busy");
2631 *(int *)gd->gd_CMAP3 =
2632 PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2633 cpu_invlpg(gd->gd_CADDR3);
2635 #if defined(I686_CPU)
2636 if (cpu_class == CPUCLASS_686)
2637 i686_pagezero(gd->gd_CADDR3);
2640 bzero(gd->gd_CADDR3, PAGE_SIZE);
2641 *(int *) gd->gd_CMAP3 = 0;
2648 * Zero part of a physical page by mapping it into memory and clearing
2649 * its contents with bzero.
2651 * off and size may not cover an area beyond a single hardware page.
2654 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
2656 struct mdglobaldata *gd = mdcpu;
2659 if (*(int *) gd->gd_CMAP3)
2660 panic("pmap_zero_page: CMAP3 busy");
2661 *(int *) gd->gd_CMAP3 = PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2662 cpu_invlpg(gd->gd_CADDR3);
2664 #if defined(I686_CPU)
2665 if (cpu_class == CPUCLASS_686 && off == 0 && size == PAGE_SIZE)
2666 i686_pagezero(gd->gd_CADDR3);
2669 bzero((char *)gd->gd_CADDR3 + off, size);
2670 *(int *) gd->gd_CMAP3 = 0;
2677 * Copy the physical page from the source PA to the target PA.
2678 * This function may be called from an interrupt. No locking
2682 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
2684 struct mdglobaldata *gd = mdcpu;
2687 if (*(int *) gd->gd_CMAP1)
2688 panic("pmap_copy_page: CMAP1 busy");
2689 if (*(int *) gd->gd_CMAP2)
2690 panic("pmap_copy_page: CMAP2 busy");
2692 *(int *) gd->gd_CMAP1 = PG_V | (src & PG_FRAME) | PG_A;
2693 *(int *) gd->gd_CMAP2 = PG_V | PG_RW | (dst & PG_FRAME) | PG_A | PG_M;
2695 cpu_invlpg(gd->gd_CADDR1);
2696 cpu_invlpg(gd->gd_CADDR2);
2698 bcopy(gd->gd_CADDR1, gd->gd_CADDR2, PAGE_SIZE);
2700 *(int *) gd->gd_CMAP1 = 0;
2701 *(int *) gd->gd_CMAP2 = 0;
2706 * pmap_copy_page_frag:
2708 * Copy the physical page from the source PA to the target PA.
2709 * This function may be called from an interrupt. No locking
2713 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
2715 struct mdglobaldata *gd = mdcpu;
2718 if (*(int *) gd->gd_CMAP1)
2719 panic("pmap_copy_page: CMAP1 busy");
2720 if (*(int *) gd->gd_CMAP2)
2721 panic("pmap_copy_page: CMAP2 busy");
2723 *(int *) gd->gd_CMAP1 = PG_V | (src & PG_FRAME) | PG_A;
2724 *(int *) gd->gd_CMAP2 = PG_V | PG_RW | (dst & PG_FRAME) | PG_A | PG_M;
2726 cpu_invlpg(gd->gd_CADDR1);
2727 cpu_invlpg(gd->gd_CADDR2);
2729 bcopy((char *)gd->gd_CADDR1 + (src & PAGE_MASK),
2730 (char *)gd->gd_CADDR2 + (dst & PAGE_MASK),
2733 *(int *) gd->gd_CMAP1 = 0;
2734 *(int *) gd->gd_CMAP2 = 0;
2740 * Routine: pmap_pageable
2742 * Make the specified pages (by pmap, offset)
2743 * pageable (or not) as requested.
2745 * A page which is not pageable may not take
2746 * a fault; therefore, its page table entry
2747 * must remain valid for the duration.
2749 * This routine is merely advisory; pmap_enter
2750 * will specify that these pages are to be wired
2751 * down (or not) as appropriate.
2754 pmap_pageable(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, boolean_t pageable)
2759 * Returns true if the pmap's pv is one of the first
2760 * 16 pvs linked to from this page. This count may
2761 * be changed upwards or downwards in the future; it
2762 * is only necessary that true be returned for a small
2763 * subset of pmaps for proper page aging.
2766 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2772 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2777 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2778 if (pv->pv_pmap == pmap) {
2790 #define PMAP_REMOVE_PAGES_CURPROC_ONLY
2792 * Remove all pages from specified address space
2793 * this aids process exit speeds. Also, this code
2794 * is special cased for current process only, but
2795 * can have the more generic (and slightly slower)
2796 * mode enabled. This is much faster than pmap_remove
2797 * in the case of running down an entire address space.
2800 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2802 unsigned *pte, tpte;
2807 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2808 if (!curproc || (pmap != vmspace_pmap(curproc->p_vmspace))) {
2809 printf("warning: pmap_remove_pages called with non-current pmap\n");
2815 for(pv = TAILQ_FIRST(&pmap->pm_pvlist);
2819 if (pv->pv_va >= eva || pv->pv_va < sva) {
2820 npv = TAILQ_NEXT(pv, pv_plist);
2824 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2825 pte = (unsigned *)vtopte(pv->pv_va);
2827 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2832 * We cannot remove wired pages from a process' mapping at this time
2835 npv = TAILQ_NEXT(pv, pv_plist);
2840 m = PHYS_TO_VM_PAGE(tpte);
2842 KASSERT(m < &vm_page_array[vm_page_array_size],
2843 ("pmap_remove_pages: bad tpte %x", tpte));
2845 pv->pv_pmap->pm_stats.resident_count--;
2848 * Update the vm_page_t clean and reference bits.
2855 npv = TAILQ_NEXT(pv, pv_plist);
2856 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2858 m->md.pv_list_count--;
2859 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2860 if (TAILQ_FIRST(&m->md.pv_list) == NULL) {
2861 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2864 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
2868 pmap_TLB_invalidate_all(pmap);
2872 * pmap_testbit tests bits in pte's
2873 * note that the testbit/changebit routines are inline,
2874 * and a lot of things compile-time evaluate.
2877 pmap_testbit(vm_page_t m, int bit)
2883 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2886 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2891 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2893 * if the bit being tested is the modified bit, then
2894 * mark clean_map and ptes as never
2897 if (bit & (PG_A|PG_M)) {
2898 if (!pmap_track_modified(pv->pv_va))
2902 #if defined(PMAP_DIAGNOSTIC)
2904 printf("Null pmap (tb) at va: 0x%x\n", pv->pv_va);
2908 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2919 * this routine is used to modify bits in ptes
2921 static __inline void
2922 pmap_changebit(vm_page_t m, int bit, boolean_t setem)
2928 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2934 * Loop over all current mappings setting/clearing as appropos If
2935 * setting RO do we need to clear the VAC?
2937 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2939 * don't write protect pager mappings
2941 if (!setem && (bit == PG_RW)) {
2942 if (!pmap_track_modified(pv->pv_va))
2946 #if defined(PMAP_DIAGNOSTIC)
2948 printf("Null pmap (cb) at va: 0x%x\n", pv->pv_va);
2953 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2957 pmap_TLB_invalidate(pv->pv_pmap, pv->pv_va);
2959 vm_offset_t pbits = *(vm_offset_t *)pte;
2965 *(int *)pte = pbits & ~(PG_M|PG_RW);
2967 *(int *)pte = pbits & ~bit;
2969 pmap_TLB_invalidate(pv->pv_pmap, pv->pv_va);
2977 * pmap_page_protect:
2979 * Lower the permission for all mappings to a given page.
2982 pmap_page_protect(vm_page_t m, vm_prot_t prot)
2984 if ((prot & VM_PROT_WRITE) == 0) {
2985 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
2986 pmap_changebit(m, PG_RW, FALSE);
2994 pmap_phys_address(int ppn)
2996 return (i386_ptob(ppn));
3000 * pmap_ts_referenced:
3002 * Return a count of reference bits for a page, clearing those bits.
3003 * It is not necessary for every reference bit to be cleared, but it
3004 * is necessary that 0 only be returned when there are truly no
3005 * reference bits set.
3007 * XXX: The exact number of bits to check and clear is a matter that
3008 * should be tested and standardized at some point in the future for
3009 * optimal aging of shared pages.
3012 pmap_ts_referenced(vm_page_t m)
3014 pv_entry_t pv, pvf, pvn;
3019 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3024 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3029 pvn = TAILQ_NEXT(pv, pv_list);
3031 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3033 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3035 if (!pmap_track_modified(pv->pv_va))
3038 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3040 if (pte && (*pte & PG_A)) {
3043 pmap_TLB_invalidate(pv->pv_pmap, pv->pv_va);
3050 } while ((pv = pvn) != NULL && pv != pvf);
3060 * Return whether or not the specified physical page was modified
3061 * in any physical maps.
3064 pmap_is_modified(vm_page_t m)
3066 return pmap_testbit(m, PG_M);
3070 * Clear the modify bits on the specified physical page.
3073 pmap_clear_modify(vm_page_t m)
3075 pmap_changebit(m, PG_M, FALSE);
3079 * pmap_clear_reference:
3081 * Clear the reference bit on the specified physical page.
3084 pmap_clear_reference(vm_page_t m)
3086 pmap_changebit(m, PG_A, FALSE);
3090 * Miscellaneous support routines follow
3094 i386_protection_init(void)
3098 kp = protection_codes;
3099 for (prot = 0; prot < 8; prot++) {
3101 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE:
3103 * Read access is also 0. There isn't any execute bit,
3104 * so just make it readable.
3106 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE:
3107 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE:
3108 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE:
3111 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE:
3112 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE:
3113 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE:
3114 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
3122 * Map a set of physical memory pages into the kernel virtual
3123 * address space. Return a pointer to where it is mapped. This
3124 * routine is intended to be used for mapping device memory,
3127 * NOTE: we can't use pgeflag unless we invalidate the pages one at
3131 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
3133 vm_offset_t va, tmpva, offset;
3136 offset = pa & PAGE_MASK;
3137 size = roundup(offset + size, PAGE_SIZE);
3139 va = kmem_alloc_pageable(kernel_map, size);
3141 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3144 for (tmpva = va; size > 0;) {
3145 pte = (unsigned *)vtopte(tmpva);
3146 *pte = pa | PG_RW | PG_V; /* | pgeflag; */
3153 return ((void *)(va + offset));
3157 pmap_unmapdev(vm_offset_t va, vm_size_t size)
3159 vm_offset_t base, offset;
3161 base = va & PG_FRAME;
3162 offset = va & PAGE_MASK;
3163 size = roundup(offset + size, PAGE_SIZE);
3164 kmem_free(kernel_map, base, size);
3168 * perform the pmap work for mincore
3171 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3173 unsigned *ptep, pte;
3177 ptep = pmap_pte(pmap, addr);
3182 if ((pte = *ptep) != 0) {
3185 val = MINCORE_INCORE;
3186 if ((pte & PG_MANAGED) == 0)
3189 pa = pte & PG_FRAME;
3191 m = PHYS_TO_VM_PAGE(pa);
3197 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3199 * Modified by someone
3201 else if (m->dirty || pmap_is_modified(m))
3202 val |= MINCORE_MODIFIED_OTHER;
3207 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3210 * Referenced by someone
3212 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3213 val |= MINCORE_REFERENCED_OTHER;
3214 vm_page_flag_set(m, PG_REFERENCED);
3221 pmap_activate(struct proc *p)
3225 pmap = vmspace_pmap(p->p_vmspace);
3227 atomic_set_int(&pmap->pm_active, 1 << mycpu->gd_cpuid);
3229 pmap->pm_active |= 1;
3231 #if defined(SWTCH_OPTIM_STATS)
3234 p->p_thread->td_pcb->pcb_cr3 = vtophys(pmap->pm_pdir);
3235 load_cr3(p->p_thread->td_pcb->pcb_cr3);
3239 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3242 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3246 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3251 #if defined(PMAP_DEBUG)
3253 pmap_pid_dump(int pid)
3259 FOREACH_PROC_IN_SYSTEM(p) {
3260 if (p->p_pid != pid)
3266 pmap = vmspace_pmap(p->p_vmspace);
3267 for(i=0;i<1024;i++) {
3270 unsigned base = i << PDRSHIFT;
3272 pde = &pmap->pm_pdir[i];
3273 if (pde && pmap_pde_v(pde)) {
3274 for(j=0;j<1024;j++) {
3275 unsigned va = base + (j << PAGE_SHIFT);
3276 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) {
3283 pte = pmap_pte_quick( pmap, va);
3284 if (pte && pmap_pte_v(pte)) {
3288 m = PHYS_TO_VM_PAGE(pa);
3289 printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x",
3290 va, pa, m->hold_count, m->wire_count, m->flags);
3311 static void pads (pmap_t pm);
3312 void pmap_pvdump (vm_paddr_t pa);
3314 /* print address space of pmap*/
3321 if (pm == kernel_pmap)
3323 for (i = 0; i < 1024; i++)
3325 for (j = 0; j < 1024; j++) {
3326 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
3327 if (pm == kernel_pmap && va < KERNBASE)
3329 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS)
3331 ptep = pmap_pte_quick(pm, va);
3332 if (pmap_pte_v(ptep))
3333 printf("%x:%x ", va, *(int *) ptep);
3339 pmap_pvdump(vm_paddr_t pa)
3344 printf("pa %08llx", (long long)pa);
3345 m = PHYS_TO_VM_PAGE(pa);
3346 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3348 printf(" -> pmap %p, va %x, flags %x",
3349 (void *)pv->pv_pmap, pv->pv_va, pv->pv_flags);
3351 printf(" -> pmap %p, va %x", (void *)pv->pv_pmap, pv->pv_va);