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/platform/pc32/i386/pmap.c,v 1.49 2005/10/05 21:53:41 corecode 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/apicreg.h>
106 #endif /* SMP || APIC_IO */
107 #include <machine/globaldata.h>
108 #include <machine/pmap.h>
109 #include <machine/pmap_inval.h>
111 #define PMAP_KEEP_PDIRS
112 #ifndef PMAP_SHPGPERPROC
113 #define PMAP_SHPGPERPROC 200
116 #if defined(DIAGNOSTIC)
117 #define PMAP_DIAGNOSTIC
122 #if !defined(PMAP_DIAGNOSTIC)
123 #define PMAP_INLINE __inline
129 * Get PDEs and PTEs for user/kernel address space
131 #define pmap_pde(m, v) (&((m)->pm_pdir[(vm_offset_t)(v) >> PDRSHIFT]))
132 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
134 #define pmap_pde_v(pte) ((*(int *)pte & PG_V) != 0)
135 #define pmap_pte_w(pte) ((*(int *)pte & PG_W) != 0)
136 #define pmap_pte_m(pte) ((*(int *)pte & PG_M) != 0)
137 #define pmap_pte_u(pte) ((*(int *)pte & PG_A) != 0)
138 #define pmap_pte_v(pte) ((*(int *)pte & PG_V) != 0)
142 * Given a map and a machine independent protection code,
143 * convert to a vax protection code.
145 #define pte_prot(m, p) \
146 (protection_codes[p & (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)])
147 static int protection_codes[8];
149 static struct pmap kernel_pmap_store;
152 vm_paddr_t avail_start; /* PA of first available physical page */
153 vm_paddr_t avail_end; /* PA of last available physical page */
154 vm_offset_t virtual_avail; /* VA of first avail page (after kernel bss) */
155 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
156 static boolean_t pmap_initialized = FALSE; /* Has pmap_init completed? */
157 static int pgeflag; /* PG_G or-in */
158 static int pseflag; /* PG_PS or-in */
160 static vm_object_t kptobj;
163 vm_offset_t kernel_vm_end;
166 * Data for the pv entry allocation mechanism
168 static vm_zone_t pvzone;
169 static struct vm_zone pvzone_store;
170 static struct vm_object pvzone_obj;
171 static int pv_entry_count=0, pv_entry_max=0, pv_entry_high_water=0;
172 static int pmap_pagedaemon_waken = 0;
173 static struct pv_entry *pvinit;
176 * All those kernel PT submaps that BSD is so fond of
178 pt_entry_t *CMAP1 = 0, *ptmmap;
179 caddr_t CADDR1 = 0, ptvmmap = 0;
180 static pt_entry_t *msgbufmap;
181 struct msgbuf *msgbufp=0;
186 static pt_entry_t *pt_crashdumpmap;
187 static caddr_t crashdumpmap;
189 extern pt_entry_t *SMPpt;
191 static PMAP_INLINE void free_pv_entry (pv_entry_t pv);
192 static unsigned * get_ptbase (pmap_t pmap);
193 static pv_entry_t get_pv_entry (void);
194 static void i386_protection_init (void);
195 static __inline void pmap_changebit (vm_page_t m, int bit, boolean_t setem);
197 static void pmap_remove_all (vm_page_t m);
198 static vm_page_t pmap_enter_quick (pmap_t pmap, vm_offset_t va,
199 vm_page_t m, vm_page_t mpte);
200 static int pmap_remove_pte (struct pmap *pmap, unsigned *ptq,
201 vm_offset_t sva, pmap_inval_info_t info);
202 static void pmap_remove_page (struct pmap *pmap,
203 vm_offset_t va, pmap_inval_info_t info);
204 static int pmap_remove_entry (struct pmap *pmap, vm_page_t m,
205 vm_offset_t va, pmap_inval_info_t info);
206 static boolean_t pmap_testbit (vm_page_t m, int bit);
207 static void pmap_insert_entry (pmap_t pmap, vm_offset_t va,
208 vm_page_t mpte, vm_page_t m);
210 static vm_page_t pmap_allocpte (pmap_t pmap, vm_offset_t va);
212 static int pmap_release_free_page (pmap_t pmap, vm_page_t p);
213 static vm_page_t _pmap_allocpte (pmap_t pmap, unsigned ptepindex);
214 static unsigned * pmap_pte_quick (pmap_t pmap, vm_offset_t va);
215 static vm_page_t pmap_page_lookup (vm_object_t object, vm_pindex_t pindex);
216 static int pmap_unuse_pt (pmap_t, vm_offset_t, vm_page_t, pmap_inval_info_t);
217 static vm_offset_t pmap_kmem_choose(vm_offset_t addr);
219 static unsigned pdir4mb;
222 * Move the kernel virtual free pointer to the next
223 * 4MB. This is used to help improve performance
224 * by using a large (4MB) page for much of the kernel
225 * (.text, .data, .bss)
228 pmap_kmem_choose(vm_offset_t addr)
230 vm_offset_t newaddr = addr;
232 if (cpu_feature & CPUID_PSE) {
233 newaddr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
242 * Extract the page table entry associated with the given map/virtual
245 * This function may NOT be called from an interrupt.
247 PMAP_INLINE unsigned *
248 pmap_pte(pmap_t pmap, vm_offset_t va)
253 pdeaddr = (unsigned *) pmap_pde(pmap, va);
254 if (*pdeaddr & PG_PS)
257 return get_ptbase(pmap) + i386_btop(va);
266 * Super fast pmap_pte routine best used when scanning the pv lists.
267 * This eliminates many course-grained invltlb calls. Note that many of
268 * the pv list scans are across different pmaps and it is very wasteful
269 * to do an entire invltlb when checking a single mapping.
271 * Should only be called while in a critical section.
274 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
276 struct mdglobaldata *gd = mdcpu;
279 if ((pde = (unsigned) pmap->pm_pdir[va >> PDRSHIFT]) != 0) {
280 unsigned frame = (unsigned) pmap->pm_pdir[PTDPTDI] & PG_FRAME;
281 unsigned index = i386_btop(va);
282 /* are we current address space or kernel? */
283 if ((pmap == kernel_pmap) ||
284 (frame == (((unsigned) PTDpde) & PG_FRAME))) {
285 return (unsigned *) PTmap + index;
287 newpf = pde & PG_FRAME;
288 if ( ((* (unsigned *) gd->gd_PMAP1) & PG_FRAME) != newpf) {
289 * (unsigned *) gd->gd_PMAP1 = newpf | PG_RW | PG_V;
290 cpu_invlpg(gd->gd_PADDR1);
292 return gd->gd_PADDR1 + ((unsigned) index & (NPTEPG - 1));
299 * Bootstrap the system enough to run with virtual memory.
301 * On the i386 this is called after mapping has already been enabled
302 * and just syncs the pmap module with what has already been done.
303 * [We can't call it easily with mapping off since the kernel is not
304 * mapped with PA == VA, hence we would have to relocate every address
305 * from the linked base (virtual) address "KERNBASE" to the actual
306 * (physical) address starting relative to 0]
309 pmap_bootstrap(firstaddr, loadaddr)
310 vm_paddr_t firstaddr;
315 struct mdglobaldata *gd;
318 avail_start = firstaddr;
321 * XXX The calculation of virtual_avail is wrong. It's NKPT*PAGE_SIZE too
322 * large. It should instead be correctly calculated in locore.s and
323 * not based on 'first' (which is a physical address, not a virtual
324 * address, for the start of unused physical memory). The kernel
325 * page tables are NOT double mapped and thus should not be included
326 * in this calculation.
328 virtual_avail = (vm_offset_t) KERNBASE + firstaddr;
329 virtual_avail = pmap_kmem_choose(virtual_avail);
331 virtual_end = VM_MAX_KERNEL_ADDRESS;
334 * Initialize protection array.
336 i386_protection_init();
339 * The kernel's pmap is statically allocated so we don't have to use
340 * pmap_create, which is unlikely to work correctly at this part of
341 * the boot sequence (XXX and which no longer exists).
343 kernel_pmap = &kernel_pmap_store;
345 kernel_pmap->pm_pdir = (pd_entry_t *)(KERNBASE + (u_int)IdlePTD);
346 kernel_pmap->pm_count = 1;
347 kernel_pmap->pm_active = (cpumask_t)-1; /* don't allow deactivation */
348 TAILQ_INIT(&kernel_pmap->pm_pvlist);
352 * Reserve some special page table entries/VA space for temporary
355 #define SYSMAP(c, p, v, n) \
356 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
359 pte = (pt_entry_t *) pmap_pte(kernel_pmap, va);
362 * CMAP1/CMAP2 are used for zeroing and copying pages.
364 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
369 SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS);
372 * ptvmmap is used for reading arbitrary physical pages via
375 SYSMAP(caddr_t, ptmmap, ptvmmap, 1)
378 * msgbufp is used to map the system message buffer.
379 * XXX msgbufmap is not used.
381 SYSMAP(struct msgbuf *, msgbufmap, msgbufp,
382 atop(round_page(MSGBUF_SIZE)))
387 for (i = 0; i < NKPT; i++)
391 * PG_G is terribly broken on SMP because we IPI invltlb's in some
392 * cases rather then invl1pg. Actually, I don't even know why it
393 * works under UP because self-referential page table mappings
398 if (cpu_feature & CPUID_PGE)
403 * Initialize the 4MB page size flag
407 * The 4MB page version of the initial
408 * kernel page mapping.
412 #if !defined(DISABLE_PSE)
413 if (cpu_feature & CPUID_PSE) {
416 * Note that we have enabled PSE mode
419 ptditmp = *((unsigned *)PTmap + i386_btop(KERNBASE));
420 ptditmp &= ~(NBPDR - 1);
421 ptditmp |= PG_V | PG_RW | PG_PS | PG_U | pgeflag;
426 * Enable the PSE mode. If we are SMP we can't do this
427 * now because the APs will not be able to use it when
430 load_cr4(rcr4() | CR4_PSE);
433 * We can do the mapping here for the single processor
434 * case. We simply ignore the old page table page from
438 * For SMP, we still need 4K pages to bootstrap APs,
439 * PSE will be enabled as soon as all APs are up.
441 PTD[KPTDI] = (pd_entry_t)ptditmp;
442 kernel_pmap->pm_pdir[KPTDI] = (pd_entry_t)ptditmp;
448 if (cpu_apic_address == 0)
449 panic("pmap_bootstrap: no local apic!");
451 /* local apic is mapped on last page */
452 SMPpt[NPTEPG - 1] = (pt_entry_t)(PG_V | PG_RW | PG_N | pgeflag |
453 (cpu_apic_address & PG_FRAME));
456 /* BSP does this itself, AP's get it pre-set */
457 gd = &CPU_prvspace[0].mdglobaldata;
458 gd->gd_CMAP1 = &SMPpt[1];
459 gd->gd_CMAP2 = &SMPpt[2];
460 gd->gd_CMAP3 = &SMPpt[3];
461 gd->gd_PMAP1 = &SMPpt[4];
462 gd->gd_CADDR1 = CPU_prvspace[0].CPAGE1;
463 gd->gd_CADDR2 = CPU_prvspace[0].CPAGE2;
464 gd->gd_CADDR3 = CPU_prvspace[0].CPAGE3;
465 gd->gd_PADDR1 = (unsigned *)CPU_prvspace[0].PPAGE1;
472 * Set 4mb pdir for mp startup
477 if (pseflag && (cpu_feature & CPUID_PSE)) {
478 load_cr4(rcr4() | CR4_PSE);
479 if (pdir4mb && mycpu->gd_cpuid == 0) { /* only on BSP */
480 kernel_pmap->pm_pdir[KPTDI] =
481 PTD[KPTDI] = (pd_entry_t)pdir4mb;
489 * Initialize the pmap module.
490 * Called by vm_init, to initialize any structures that the pmap
491 * system needs to map virtual memory.
492 * pmap_init has been enhanced to support in a fairly consistant
493 * way, discontiguous physical memory.
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))
594 get_ptbase(pmap_t pmap)
596 unsigned frame = (unsigned) pmap->pm_pdir[PTDPTDI] & PG_FRAME;
597 struct globaldata *gd = mycpu;
599 /* are we current address space or kernel? */
600 if (pmap == kernel_pmap || frame == (((unsigned) PTDpde) & PG_FRAME)) {
601 return (unsigned *) PTmap;
604 /* otherwise, we are alternate address space */
605 KKASSERT(gd->gd_intr_nesting_level == 0 && (gd->gd_curthread->td_flags & TDF_INTTHREAD) == 0);
607 if (frame != (((unsigned) APTDpde) & PG_FRAME)) {
608 APTDpde = (pd_entry_t)(frame | PG_RW | PG_V);
609 /* The page directory is not shared between CPUs */
612 return (unsigned *) APTmap;
618 * Extract the physical page address associated with the map/VA pair.
620 * This function may not be called from an interrupt if the pmap is
624 pmap_extract(pmap_t pmap, vm_offset_t va)
627 vm_offset_t pdirindex;
629 pdirindex = va >> PDRSHIFT;
630 if (pmap && (rtval = (unsigned) pmap->pm_pdir[pdirindex])) {
632 if ((rtval & PG_PS) != 0) {
633 rtval &= ~(NBPDR - 1);
634 rtval |= va & (NBPDR - 1);
637 pte = get_ptbase(pmap) + i386_btop(va);
638 rtval = ((*pte & PG_FRAME) | (va & PAGE_MASK));
645 * Extract user accessible page only, return NULL if the page is not
646 * present or if it's current state is not sufficient. Caller will
647 * generally call vm_fault() on failure and try again.
650 pmap_extract_vmpage(pmap_t pmap, vm_offset_t va, int prot)
653 vm_offset_t pdirindex;
655 pdirindex = va >> PDRSHIFT;
656 if (pmap && (rtval = (unsigned) pmap->pm_pdir[pdirindex])) {
660 if ((rtval & PG_PS) != 0) {
661 if ((rtval & (PG_V|PG_U)) != (PG_V|PG_U))
663 if ((prot & VM_PROT_WRITE) && (rtval & PG_RW) == 0)
665 rtval &= ~(NBPDR - 1);
666 rtval |= va & (NBPDR - 1);
667 m = PHYS_TO_VM_PAGE(rtval);
669 pte = get_ptbase(pmap) + i386_btop(va);
670 if ((*pte & (PG_V|PG_U)) != (PG_V|PG_U))
672 if ((prot & VM_PROT_WRITE) && (*pte & PG_RW) == 0)
674 rtval = ((*pte & PG_FRAME) | (va & PAGE_MASK));
675 m = PHYS_TO_VM_PAGE(rtval);
682 /***************************************************
683 * Low level mapping routines.....
684 ***************************************************/
687 * Routine: pmap_kenter
689 * Add a wired page to the KVA
690 * NOTE! note that in order for the mapping to take effect -- you
691 * should do an invltlb after doing the pmap_kenter().
694 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
698 pmap_inval_info info;
700 pmap_inval_init(&info);
701 pmap_inval_add(&info, kernel_pmap, va);
702 npte = pa | PG_RW | PG_V | pgeflag;
703 pte = (unsigned *)vtopte(va);
705 pmap_inval_flush(&info);
709 * Routine: pmap_kenter_quick
711 * Similar to pmap_kenter(), except we only invalidate the
712 * mapping on the current CPU.
715 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
720 npte = pa | PG_RW | PG_V | pgeflag;
721 pte = (unsigned *)vtopte(va);
723 cpu_invlpg((void *)va);
727 pmap_kenter_sync(vm_offset_t va)
729 pmap_inval_info info;
731 pmap_inval_init(&info);
732 pmap_inval_add(&info, kernel_pmap, va);
733 pmap_inval_flush(&info);
737 pmap_kenter_sync_quick(vm_offset_t va)
739 cpu_invlpg((void *)va);
743 * remove a page from the kernel pagetables
746 pmap_kremove(vm_offset_t va)
749 pmap_inval_info info;
751 pmap_inval_init(&info);
752 pmap_inval_add(&info, kernel_pmap, va);
753 pte = (unsigned *)vtopte(va);
755 pmap_inval_flush(&info);
759 pmap_kremove_quick(vm_offset_t va)
762 pte = (unsigned *)vtopte(va);
764 cpu_invlpg((void *)va);
768 * Used to map a range of physical addresses into kernel
769 * virtual address space.
771 * For now, VM is already on, we only need to map the
775 pmap_map(vm_offset_t virt, vm_paddr_t start, vm_paddr_t end, int prot)
777 while (start < end) {
778 pmap_kenter(virt, start);
787 * Add a list of wired pages to the kva
788 * this routine is only used for temporary
789 * kernel mappings that do not need to have
790 * page modification or references recorded.
791 * Note that old mappings are simply written
792 * over. The page *must* be wired.
795 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
799 end_va = va + count * PAGE_SIZE;
801 while (va < end_va) {
804 pte = (unsigned *)vtopte(va);
805 *pte = VM_PAGE_TO_PHYS(*m) | PG_RW | PG_V | pgeflag;
806 cpu_invlpg((void *)va);
811 smp_invltlb(); /* XXX */
816 pmap_qenter2(vm_offset_t va, vm_page_t *m, int count, cpumask_t *mask)
819 cpumask_t cmask = mycpu->gd_cpumask;
821 end_va = va + count * PAGE_SIZE;
823 while (va < end_va) {
828 * Install the new PTE. If the pte changed from the prior
829 * mapping we must reset the cpu mask and invalidate the page.
830 * If the pte is the same but we have not seen it on the
831 * current cpu, invlpg the existing mapping. Otherwise the
832 * entry is optimal and no invalidation is required.
834 pte = (unsigned *)vtopte(va);
835 pteval = VM_PAGE_TO_PHYS(*m) | PG_A | PG_RW | PG_V | pgeflag;
836 if (*pte != pteval) {
839 cpu_invlpg((void *)va);
840 } else if ((*mask & cmask) == 0) {
841 cpu_invlpg((void *)va);
850 * this routine jerks page mappings from the
851 * kernel -- it is meant only for temporary mappings.
854 pmap_qremove(vm_offset_t va, int count)
858 end_va = va + count*PAGE_SIZE;
860 while (va < end_va) {
863 pte = (unsigned *)vtopte(va);
865 cpu_invlpg((void *)va);
874 * This routine works like vm_page_lookup() but also blocks as long as the
875 * page is busy. This routine does not busy the page it returns.
877 * Unless the caller is managing objects whos pages are in a known state,
878 * the call should be made with a critical section held so the page's object
879 * association remains valid on return.
882 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
887 m = vm_page_lookup(object, pindex);
888 if (m && vm_page_sleep_busy(m, FALSE, "pplookp"))
894 * Create a new thread and optionally associate it with a (new) process.
895 * NOTE! the new thread's cpu may not equal the current cpu.
898 pmap_init_thread(thread_t td)
900 /* enforce pcb placement */
901 td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
902 td->td_savefpu = &td->td_pcb->pcb_save;
903 td->td_sp = (char *)td->td_pcb - 16;
907 * Create the UPAGES for a new process.
908 * This routine directly affects the fork perf for a process.
911 pmap_init_proc(struct proc *p, struct thread *td)
913 p->p_addr = (void *)td->td_kstack;
916 td->td_lwp = &p->p_lwp;
917 td->td_switch = cpu_heavy_switch;
921 bzero(p->p_addr, sizeof(*p->p_addr));
925 * Dispose the UPAGES for a process that has exited.
926 * This routine directly impacts the exit perf of a process.
929 pmap_dispose_proc(struct proc *p)
933 KASSERT(p->p_lock == 0, ("attempt to dispose referenced proc! %p", p));
935 if ((td = p->p_thread) != NULL) {
944 * Allow the UPAGES for a process to be prejudicially paged out.
947 pmap_swapout_proc(struct proc *p)
955 upobj = p->p_upages_obj;
958 * Unwiring the pages allow them to be paged to their backing store
962 for (i = 0; i < UPAGES; i++) {
963 if ((m = vm_page_lookup(upobj, i)) == NULL)
964 panic("pmap_swapout_proc: upage already missing???");
966 vm_page_unwire(m, 0);
967 pmap_kremove((vm_offset_t)p->p_addr + (PAGE_SIZE * i));
974 * Bring the UPAGES for a specified process back in.
977 pmap_swapin_proc(struct proc *p)
985 upobj = p->p_upages_obj;
986 for (i = 0; i < UPAGES; i++) {
987 m = vm_page_grab(upobj, i, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
989 pmap_kenter((vm_offset_t)p->p_addr + (i * PAGE_SIZE),
992 if (m->valid != VM_PAGE_BITS_ALL) {
993 rv = vm_pager_get_pages(upobj, &m, 1, 0);
994 if (rv != VM_PAGER_OK)
995 panic("pmap_swapin_proc: cannot get upages for proc: %d\n", p->p_pid);
996 m = vm_page_lookup(upobj, i);
997 m->valid = VM_PAGE_BITS_ALL;
1001 vm_page_flag_set(m, PG_MAPPED | PG_WRITEABLE);
1007 /***************************************************
1008 * Page table page management routines.....
1009 ***************************************************/
1012 * This routine unholds page table pages, and if the hold count
1013 * drops to zero, then it decrements the wire count.
1016 _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, pmap_inval_info_t info)
1018 pmap_inval_flush(info);
1019 while (vm_page_sleep_busy(m, FALSE, "pmuwpt"))
1022 if (m->hold_count == 0) {
1024 * unmap the page table page
1026 pmap_inval_add(info, pmap, -1);
1027 pmap->pm_pdir[m->pindex] = 0;
1028 --pmap->pm_stats.resident_count;
1030 if (pmap->pm_ptphint == m)
1031 pmap->pm_ptphint = NULL;
1034 * If the page is finally unwired, simply free it.
1037 if (m->wire_count == 0) {
1040 vm_page_free_zero(m);
1041 --vmstats.v_wire_count;
1048 static PMAP_INLINE int
1049 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, pmap_inval_info_t info)
1052 if (m->hold_count == 0)
1053 return _pmap_unwire_pte_hold(pmap, m, info);
1059 * After removing a page table entry, this routine is used to
1060 * conditionally free the page, and manage the hold/wire counts.
1063 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte,
1064 pmap_inval_info_t info)
1067 if (va >= UPT_MIN_ADDRESS)
1071 ptepindex = (va >> PDRSHIFT);
1072 if (pmap->pm_ptphint &&
1073 (pmap->pm_ptphint->pindex == ptepindex)) {
1074 mpte = pmap->pm_ptphint;
1076 pmap_inval_flush(info);
1077 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1078 pmap->pm_ptphint = mpte;
1082 return pmap_unwire_pte_hold(pmap, mpte, info);
1086 pmap_pinit0(struct pmap *pmap)
1089 (pd_entry_t *)kmem_alloc_pageable(kernel_map, PAGE_SIZE);
1090 pmap_kenter((vm_offset_t)pmap->pm_pdir, (vm_offset_t) IdlePTD);
1092 pmap->pm_active = 0;
1093 pmap->pm_ptphint = NULL;
1094 TAILQ_INIT(&pmap->pm_pvlist);
1095 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1099 * Initialize a preallocated and zeroed pmap structure,
1100 * such as one in a vmspace structure.
1103 pmap_pinit(struct pmap *pmap)
1108 * No need to allocate page table space yet but we do need a valid
1109 * page directory table.
1111 if (pmap->pm_pdir == NULL) {
1113 (pd_entry_t *)kmem_alloc_pageable(kernel_map, PAGE_SIZE);
1117 * allocate object for the ptes
1119 if (pmap->pm_pteobj == NULL)
1120 pmap->pm_pteobj = vm_object_allocate( OBJT_DEFAULT, PTDPTDI + 1);
1123 * allocate the page directory page
1125 ptdpg = vm_page_grab( pmap->pm_pteobj, PTDPTDI,
1126 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1128 ptdpg->wire_count = 1;
1129 ++vmstats.v_wire_count;
1132 vm_page_flag_clear(ptdpg, PG_MAPPED | PG_BUSY); /* not usually mapped*/
1133 ptdpg->valid = VM_PAGE_BITS_ALL;
1135 pmap_kenter((vm_offset_t)pmap->pm_pdir, VM_PAGE_TO_PHYS(ptdpg));
1136 if ((ptdpg->flags & PG_ZERO) == 0)
1137 bzero(pmap->pm_pdir, PAGE_SIZE);
1139 pmap->pm_pdir[MPPTDI] = PTD[MPPTDI];
1141 /* install self-referential address mapping entry */
1142 *(unsigned *) (pmap->pm_pdir + PTDPTDI) =
1143 VM_PAGE_TO_PHYS(ptdpg) | PG_V | PG_RW | PG_A | PG_M;
1146 pmap->pm_active = 0;
1147 pmap->pm_ptphint = NULL;
1148 TAILQ_INIT(&pmap->pm_pvlist);
1149 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1153 * Wire in kernel global address entries. To avoid a race condition
1154 * between pmap initialization and pmap_growkernel, this procedure
1155 * should be called after the vmspace is attached to the process
1156 * but before this pmap is activated.
1159 pmap_pinit2(struct pmap *pmap)
1161 /* XXX copies current process, does not fill in MPPTDI */
1162 bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * PTESIZE);
1166 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1168 unsigned *pde = (unsigned *) pmap->pm_pdir;
1170 * This code optimizes the case of freeing non-busy
1171 * page-table pages. Those pages are zero now, and
1172 * might as well be placed directly into the zero queue.
1174 if (vm_page_sleep_busy(p, FALSE, "pmaprl"))
1180 * Remove the page table page from the processes address space.
1183 pmap->pm_stats.resident_count--;
1185 if (p->hold_count) {
1186 panic("pmap_release: freeing held page table page");
1189 * Page directory pages need to have the kernel
1190 * stuff cleared, so they can go into the zero queue also.
1192 if (p->pindex == PTDPTDI) {
1193 bzero(pde + KPTDI, nkpt * PTESIZE);
1196 pmap_kremove((vm_offset_t)pmap->pm_pdir);
1199 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1200 pmap->pm_ptphint = NULL;
1203 vmstats.v_wire_count--;
1204 vm_page_free_zero(p);
1209 * this routine is called if the page table page is not
1213 _pmap_allocpte(pmap_t pmap, unsigned ptepindex)
1215 vm_offset_t pteva, ptepa;
1219 * Find or fabricate a new pagetable page
1221 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1222 VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1224 KASSERT(m->queue == PQ_NONE,
1225 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1227 if (m->wire_count == 0)
1228 vmstats.v_wire_count++;
1232 * Increment the hold count for the page table page
1233 * (denoting a new mapping.)
1238 * Map the pagetable page into the process address space, if
1239 * it isn't already there.
1242 pmap->pm_stats.resident_count++;
1244 ptepa = VM_PAGE_TO_PHYS(m);
1245 pmap->pm_pdir[ptepindex] =
1246 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_A | PG_M);
1249 * Set the page table hint
1251 pmap->pm_ptphint = m;
1254 * Try to use the new mapping, but if we cannot, then
1255 * do it with the routine that maps the page explicitly.
1257 if ((m->flags & PG_ZERO) == 0) {
1258 if ((((unsigned)pmap->pm_pdir[PTDPTDI]) & PG_FRAME) ==
1259 (((unsigned) PTDpde) & PG_FRAME)) {
1260 pteva = UPT_MIN_ADDRESS + i386_ptob(ptepindex);
1261 bzero((caddr_t) pteva, PAGE_SIZE);
1263 pmap_zero_page(ptepa);
1267 m->valid = VM_PAGE_BITS_ALL;
1268 vm_page_flag_clear(m, PG_ZERO);
1269 vm_page_flag_set(m, PG_MAPPED);
1276 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1283 * Calculate pagetable page index
1285 ptepindex = va >> PDRSHIFT;
1288 * Get the page directory entry
1290 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1293 * This supports switching from a 4MB page to a
1296 if (ptepa & PG_PS) {
1297 pmap->pm_pdir[ptepindex] = 0;
1304 * If the page table page is mapped, we just increment the
1305 * hold count, and activate it.
1309 * In order to get the page table page, try the
1312 if (pmap->pm_ptphint &&
1313 (pmap->pm_ptphint->pindex == ptepindex)) {
1314 m = pmap->pm_ptphint;
1316 m = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1317 pmap->pm_ptphint = m;
1323 * Here if the pte page isn't mapped, or if it has been deallocated.
1325 return _pmap_allocpte(pmap, ptepindex);
1329 /***************************************************
1330 * Pmap allocation/deallocation routines.
1331 ***************************************************/
1334 * Release any resources held by the given physical map.
1335 * Called when a pmap initialized by pmap_pinit is being released.
1336 * Should only be called if the map contains no valid mappings.
1339 pmap_release(struct pmap *pmap)
1341 vm_page_t p,n,ptdpg;
1342 vm_object_t object = pmap->pm_pteobj;
1345 #if defined(DIAGNOSTIC)
1346 if (object->ref_count != 1)
1347 panic("pmap_release: pteobj reference count != 1");
1353 curgeneration = object->generation;
1354 for (p = TAILQ_FIRST(&object->memq); p != NULL; p = n) {
1355 n = TAILQ_NEXT(p, listq);
1356 if (p->pindex == PTDPTDI) {
1361 if (!pmap_release_free_page(pmap, p) &&
1362 (object->generation != curgeneration)) {
1370 if (ptdpg && !pmap_release_free_page(pmap, ptdpg))
1375 kvm_size(SYSCTL_HANDLER_ARGS)
1377 unsigned long ksize = VM_MAX_KERNEL_ADDRESS - KERNBASE;
1379 return sysctl_handle_long(oidp, &ksize, 0, req);
1381 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD,
1382 0, 0, kvm_size, "IU", "Size of KVM");
1385 kvm_free(SYSCTL_HANDLER_ARGS)
1387 unsigned long kfree = VM_MAX_KERNEL_ADDRESS - kernel_vm_end;
1389 return sysctl_handle_long(oidp, &kfree, 0, req);
1391 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD,
1392 0, 0, kvm_free, "IU", "Amount of KVM free");
1395 * grow the number of kernel page table entries, if needed
1398 pmap_growkernel(vm_offset_t addr)
1402 vm_offset_t ptppaddr;
1407 if (kernel_vm_end == 0) {
1408 kernel_vm_end = KERNBASE;
1410 while (pdir_pde(PTD, kernel_vm_end)) {
1411 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1415 addr = (addr + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1416 while (kernel_vm_end < addr) {
1417 if (pdir_pde(PTD, kernel_vm_end)) {
1418 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1423 * This index is bogus, but out of the way
1425 nkpg = vm_page_alloc(kptobj, nkpt,
1426 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM | VM_ALLOC_INTERRUPT);
1428 panic("pmap_growkernel: no memory to grow kernel");
1433 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1434 pmap_zero_page(ptppaddr);
1435 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
1436 pdir_pde(PTD, kernel_vm_end) = newpdir;
1438 FOREACH_PROC_IN_SYSTEM(p) {
1440 pmap = vmspace_pmap(p->p_vmspace);
1441 *pmap_pde(pmap, kernel_vm_end) = newpdir;
1444 *pmap_pde(kernel_pmap, kernel_vm_end) = newpdir;
1445 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1451 * Retire the given physical map from service.
1452 * Should only be called if the map contains
1453 * no valid mappings.
1456 pmap_destroy(pmap_t pmap)
1463 count = --pmap->pm_count;
1466 panic("destroying a pmap is not yet implemented");
1471 * Add a reference to the specified pmap.
1474 pmap_reference(pmap_t pmap)
1481 /***************************************************
1482 * page management routines.
1483 ***************************************************/
1486 * free the pv_entry back to the free list. This function may be
1487 * called from an interrupt.
1489 static PMAP_INLINE void
1490 free_pv_entry(pv_entry_t pv)
1497 * get a new pv_entry, allocating a block from the system
1498 * when needed. This function may be called from an interrupt.
1504 if (pv_entry_high_water &&
1505 (pv_entry_count > pv_entry_high_water) &&
1506 (pmap_pagedaemon_waken == 0)) {
1507 pmap_pagedaemon_waken = 1;
1508 wakeup (&vm_pages_needed);
1510 return zalloc(pvzone);
1514 * This routine is very drastic, but can save the system
1522 static int warningdone=0;
1524 if (pmap_pagedaemon_waken == 0)
1527 if (warningdone < 5) {
1528 printf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1532 for(i = 0; i < vm_page_array_size; i++) {
1533 m = &vm_page_array[i];
1534 if (m->wire_count || m->hold_count || m->busy ||
1535 (m->flags & PG_BUSY))
1539 pmap_pagedaemon_waken = 0;
1544 * If it is the first entry on the list, it is actually
1545 * in the header and we must copy the following entry up
1546 * to the header. Otherwise we must search the list for
1547 * the entry. In either case we free the now unused entry.
1550 pmap_remove_entry(struct pmap *pmap, vm_page_t m,
1551 vm_offset_t va, pmap_inval_info_t info)
1557 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1558 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1559 if (pmap == pv->pv_pmap && va == pv->pv_va)
1563 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1564 if (va == pv->pv_va)
1571 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem, info);
1572 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1573 m->md.pv_list_count--;
1574 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
1575 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1576 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1584 * Create a pv entry for page at pa for
1588 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
1593 pv = get_pv_entry();
1598 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1599 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1600 m->md.pv_list_count++;
1606 * pmap_remove_pte: do the things to unmap a page in a process
1609 pmap_remove_pte(struct pmap *pmap, unsigned *ptq, vm_offset_t va,
1610 pmap_inval_info_t info)
1615 pmap_inval_add(info, pmap, va);
1616 oldpte = loadandclear(ptq);
1618 pmap->pm_stats.wired_count -= 1;
1620 * Machines that don't support invlpg, also don't support
1621 * PG_G. XXX PG_G is disabled for SMP so don't worry about
1625 cpu_invlpg((void *)va);
1626 pmap->pm_stats.resident_count -= 1;
1627 if (oldpte & PG_MANAGED) {
1628 m = PHYS_TO_VM_PAGE(oldpte);
1629 if (oldpte & PG_M) {
1630 #if defined(PMAP_DIAGNOSTIC)
1631 if (pmap_nw_modified((pt_entry_t) oldpte)) {
1633 "pmap_remove: modified page not writable: va: 0x%x, pte: 0x%x\n",
1637 if (pmap_track_modified(va))
1641 vm_page_flag_set(m, PG_REFERENCED);
1642 return pmap_remove_entry(pmap, m, va, info);
1644 return pmap_unuse_pt(pmap, va, NULL, info);
1653 * Remove a single page from a process address space.
1655 * This function may not be called from an interrupt if the pmap is
1659 pmap_remove_page(struct pmap *pmap, vm_offset_t va, pmap_inval_info_t info)
1664 * if there is no pte for this address, just skip it!!! Otherwise
1665 * get a local va for mappings for this pmap and remove the entry.
1667 if (*pmap_pde(pmap, va) != 0) {
1668 ptq = get_ptbase(pmap) + i386_btop(va);
1670 pmap_remove_pte(pmap, ptq, va, info);
1678 * Remove the given range of addresses from the specified map.
1680 * It is assumed that the start and end are properly
1681 * rounded to the page size.
1683 * This function may not be called from an interrupt if the pmap is
1687 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
1691 vm_offset_t ptpaddr;
1692 vm_offset_t sindex, eindex;
1693 struct pmap_inval_info info;
1698 if (pmap->pm_stats.resident_count == 0)
1701 pmap_inval_init(&info);
1704 * special handling of removing one page. a very
1705 * common operation and easy to short circuit some
1708 if (((sva + PAGE_SIZE) == eva) &&
1709 (((unsigned) pmap->pm_pdir[(sva >> PDRSHIFT)] & PG_PS) == 0)) {
1710 pmap_remove_page(pmap, sva, &info);
1711 pmap_inval_flush(&info);
1716 * Get a local virtual address for the mappings that are being
1719 ptbase = get_ptbase(pmap);
1721 sindex = i386_btop(sva);
1722 eindex = i386_btop(eva);
1724 for (; sindex < eindex; sindex = pdnxt) {
1728 * Calculate index for next page table.
1730 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1731 if (pmap->pm_stats.resident_count == 0)
1734 pdirindex = sindex / NPDEPG;
1735 if (((ptpaddr = (unsigned) pmap->pm_pdir[pdirindex]) & PG_PS) != 0) {
1736 pmap_inval_add(&info, pmap, -1);
1737 pmap->pm_pdir[pdirindex] = 0;
1738 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1743 * Weed out invalid mappings. Note: we assume that the page
1744 * directory table is always allocated, and in kernel virtual.
1750 * Limit our scan to either the end of the va represented
1751 * by the current page table page, or to the end of the
1752 * range being removed.
1754 if (pdnxt > eindex) {
1758 for (; sindex != pdnxt; sindex++) {
1760 if (ptbase[sindex] == 0)
1762 va = i386_ptob(sindex);
1763 if (pmap_remove_pte(pmap, ptbase + sindex, va, &info))
1767 pmap_inval_flush(&info);
1773 * Removes this physical page from all physical maps in which it resides.
1774 * Reflects back modify bits to the pager.
1776 * This routine may not be called from an interrupt.
1780 pmap_remove_all(vm_page_t m)
1782 struct pmap_inval_info info;
1783 unsigned *pte, tpte;
1786 #if defined(PMAP_DIAGNOSTIC)
1788 * XXX this makes pmap_page_protect(NONE) illegal for non-managed
1791 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
1792 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%08llx", (long long)VM_PAGE_TO_PHYS(m));
1796 pmap_inval_init(&info);
1798 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
1799 pv->pv_pmap->pm_stats.resident_count--;
1801 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
1802 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
1804 tpte = loadandclear(pte);
1806 pv->pv_pmap->pm_stats.wired_count--;
1809 vm_page_flag_set(m, PG_REFERENCED);
1812 * Update the vm_page_t clean and reference bits.
1815 #if defined(PMAP_DIAGNOSTIC)
1816 if (pmap_nw_modified((pt_entry_t) tpte)) {
1818 "pmap_remove_all: modified page not writable: va: 0x%x, pte: 0x%x\n",
1822 if (pmap_track_modified(pv->pv_va))
1825 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
1826 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1827 m->md.pv_list_count--;
1828 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem, &info);
1832 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1834 pmap_inval_flush(&info);
1840 * Set the physical protection on the specified range of this map
1843 * This function may not be called from an interrupt if the map is
1844 * not the kernel_pmap.
1847 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
1850 vm_offset_t pdnxt, ptpaddr;
1851 vm_pindex_t sindex, eindex;
1852 pmap_inval_info info;
1857 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1858 pmap_remove(pmap, sva, eva);
1862 if (prot & VM_PROT_WRITE)
1865 pmap_inval_init(&info);
1867 ptbase = get_ptbase(pmap);
1869 sindex = i386_btop(sva);
1870 eindex = i386_btop(eva);
1872 for (; sindex < eindex; sindex = pdnxt) {
1876 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1878 pdirindex = sindex / NPDEPG;
1879 if (((ptpaddr = (unsigned) pmap->pm_pdir[pdirindex]) & PG_PS) != 0) {
1880 pmap_inval_add(&info, pmap, -1);
1881 (unsigned) pmap->pm_pdir[pdirindex] &= ~(PG_M|PG_RW);
1882 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1887 * Weed out invalid mappings. Note: we assume that the page
1888 * directory table is always allocated, and in kernel virtual.
1893 if (pdnxt > eindex) {
1897 for (; sindex != pdnxt; sindex++) {
1902 /* XXX this isn't optimal */
1903 pmap_inval_add(&info, pmap, i386_ptob(sindex));
1904 pbits = ptbase[sindex];
1906 if (pbits & PG_MANAGED) {
1909 m = PHYS_TO_VM_PAGE(pbits);
1910 vm_page_flag_set(m, PG_REFERENCED);
1914 if (pmap_track_modified(i386_ptob(sindex))) {
1916 m = PHYS_TO_VM_PAGE(pbits);
1925 if (pbits != ptbase[sindex]) {
1926 ptbase[sindex] = pbits;
1930 pmap_inval_flush(&info);
1934 * Insert the given physical page (p) at
1935 * the specified virtual address (v) in the
1936 * target physical map with the protection requested.
1938 * If specified, the page will be wired down, meaning
1939 * that the related pte can not be reclaimed.
1941 * NB: This is the only routine which MAY NOT lazy-evaluate
1942 * or lose information. That is, this routine must actually
1943 * insert this page into the given map NOW.
1946 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1952 vm_offset_t origpte, newpte;
1954 pmap_inval_info info;
1960 #ifdef PMAP_DIAGNOSTIC
1961 if (va > VM_MAX_KERNEL_ADDRESS)
1962 panic("pmap_enter: toobig");
1963 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
1964 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%x)", va);
1969 * In the case that a page table page is not
1970 * resident, we are creating it here.
1972 if (va < UPT_MIN_ADDRESS) {
1973 mpte = pmap_allocpte(pmap, va);
1976 pmap_inval_init(&info);
1977 pte = pmap_pte(pmap, va);
1980 * Page Directory table entry not valid, we need a new PT page
1983 panic("pmap_enter: invalid page directory pdir=%x, va=0x%x\n",
1984 (unsigned) pmap->pm_pdir[PTDPTDI], va);
1987 pa = VM_PAGE_TO_PHYS(m) & PG_FRAME;
1988 pmap_inval_add(&info, pmap, va); /* XXX non-optimal */
1989 origpte = *(vm_offset_t *)pte;
1990 opa = origpte & PG_FRAME;
1992 if (origpte & PG_PS)
1993 panic("pmap_enter: attempted pmap_enter on 4MB page");
1996 * Mapping has not changed, must be protection or wiring change.
1998 if (origpte && (opa == pa)) {
2000 * Wiring change, just update stats. We don't worry about
2001 * wiring PT pages as they remain resident as long as there
2002 * are valid mappings in them. Hence, if a user page is wired,
2003 * the PT page will be also.
2005 if (wired && ((origpte & PG_W) == 0))
2006 pmap->pm_stats.wired_count++;
2007 else if (!wired && (origpte & PG_W))
2008 pmap->pm_stats.wired_count--;
2010 #if defined(PMAP_DIAGNOSTIC)
2011 if (pmap_nw_modified((pt_entry_t) origpte)) {
2013 "pmap_enter: modified page not writable: va: 0x%x, pte: 0x%x\n",
2019 * Remove the extra pte reference. Note that we cannot
2020 * optimize the RO->RW case because we have adjusted the
2021 * wiring count above and may need to adjust the wiring
2028 * We might be turning off write access to the page,
2029 * so we go ahead and sense modify status.
2031 if (origpte & PG_MANAGED) {
2032 if ((origpte & PG_M) && pmap_track_modified(va)) {
2034 om = PHYS_TO_VM_PAGE(opa);
2042 * Mapping has changed, invalidate old range and fall through to
2043 * handle validating new mapping.
2047 err = pmap_remove_pte(pmap, pte, va, &info);
2049 panic("pmap_enter: pte vanished, va: 0x%x", va);
2053 * Enter on the PV list if part of our managed memory. Note that we
2054 * raise IPL while manipulating pv_table since pmap_enter can be
2055 * called at interrupt time.
2057 if (pmap_initialized &&
2058 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2059 pmap_insert_entry(pmap, va, mpte, m);
2064 * Increment counters
2066 pmap->pm_stats.resident_count++;
2068 pmap->pm_stats.wired_count++;
2072 * Now validate mapping with desired protection/wiring.
2074 newpte = (vm_offset_t) (pa | pte_prot(pmap, prot) | PG_V);
2078 if (va < UPT_MIN_ADDRESS)
2080 if (pmap == kernel_pmap)
2084 * if the mapping or permission bits are different, we need
2085 * to update the pte.
2087 if ((origpte & ~(PG_M|PG_A)) != newpte) {
2088 *pte = newpte | PG_A;
2090 pmap_inval_flush(&info);
2094 * this code makes some *MAJOR* assumptions:
2095 * 1. Current pmap & pmap exists.
2098 * 4. No page table pages.
2099 * 5. Tlbflush is deferred to calling procedure.
2100 * 6. Page IS managed.
2101 * but is *MUCH* faster than pmap_enter...
2105 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_page_t mpte)
2109 pmap_inval_info info;
2111 pmap_inval_init(&info);
2114 * In the case that a page table page is not
2115 * resident, we are creating it here.
2117 if (va < UPT_MIN_ADDRESS) {
2122 * Calculate pagetable page index
2124 ptepindex = va >> PDRSHIFT;
2125 if (mpte && (mpte->pindex == ptepindex)) {
2130 * Get the page directory entry
2132 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
2135 * If the page table page is mapped, we just increment
2136 * the hold count, and activate it.
2140 panic("pmap_enter_quick: unexpected mapping into 4MB page");
2141 if (pmap->pm_ptphint &&
2142 (pmap->pm_ptphint->pindex == ptepindex)) {
2143 mpte = pmap->pm_ptphint;
2145 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
2146 pmap->pm_ptphint = mpte;
2152 mpte = _pmap_allocpte(pmap, ptepindex);
2160 * This call to vtopte makes the assumption that we are
2161 * entering the page into the current pmap. In order to support
2162 * quick entry into any pmap, one would likely use pmap_pte_quick.
2163 * But that isn't as quick as vtopte.
2165 pte = (unsigned *)vtopte(va);
2168 pmap_unwire_pte_hold(pmap, mpte, &info);
2173 * Enter on the PV list if part of our managed memory. Note that we
2174 * raise IPL while manipulating pv_table since pmap_enter can be
2175 * called at interrupt time.
2177 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0)
2178 pmap_insert_entry(pmap, va, mpte, m);
2181 * Increment counters
2183 pmap->pm_stats.resident_count++;
2185 pa = VM_PAGE_TO_PHYS(m);
2188 * Now validate mapping with RO protection
2190 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2191 *pte = pa | PG_V | PG_U;
2193 *pte = pa | PG_V | PG_U | PG_MANAGED;
2199 * Make a temporary mapping for a physical address. This is only intended
2200 * to be used for panic dumps.
2203 pmap_kenter_temporary(vm_paddr_t pa, int i)
2205 pmap_kenter((vm_offset_t)crashdumpmap + (i * PAGE_SIZE), pa);
2206 return ((void *)crashdumpmap);
2209 #define MAX_INIT_PT (96)
2212 * This routine preloads the ptes for a given object into the specified pmap.
2213 * This eliminates the blast of soft faults on process startup and
2214 * immediately after an mmap.
2217 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2218 vm_object_t object, vm_pindex_t pindex,
2219 vm_size_t size, int limit)
2226 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2231 * XXX you must be joking, entering PTE's into a user page table
2232 * without any accounting? This could result in the page table
2233 * being freed while it still contains mappings (free with PG_ZERO
2234 * assumption leading to a non-zero page being marked PG_ZERO).
2237 * This code maps large physical mmap regions into the
2238 * processor address space. Note that some shortcuts
2239 * are taken, but the code works.
2242 (object->type == OBJT_DEVICE) &&
2243 ((addr & (NBPDR - 1)) == 0) &&
2244 ((size & (NBPDR - 1)) == 0) ) {
2247 unsigned int ptepindex;
2251 if (pmap->pm_pdir[ptepindex = (addr >> PDRSHIFT)])
2255 p = vm_page_lookup(object, pindex);
2256 if (p && vm_page_sleep_busy(p, FALSE, "init4p"))
2260 p = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL);
2265 if (vm_pager_get_pages(object, m, 1, 0) != VM_PAGER_OK) {
2270 p = vm_page_lookup(object, pindex);
2274 ptepa = (vm_offset_t) VM_PAGE_TO_PHYS(p);
2275 if (ptepa & (NBPDR - 1)) {
2279 p->valid = VM_PAGE_BITS_ALL;
2281 pmap->pm_stats.resident_count += size >> PAGE_SHIFT;
2282 npdes = size >> PDRSHIFT;
2283 for (i = 0; i < npdes; i++) {
2284 pmap->pm_pdir[ptepindex] =
2285 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_PS);
2289 vm_page_flag_set(p, PG_MAPPED);
2296 psize = i386_btop(size);
2298 if ((object->type != OBJT_VNODE) ||
2299 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2300 (object->resident_page_count > MAX_INIT_PT))) {
2304 if (psize + pindex > object->size) {
2305 if (object->size < pindex)
2307 psize = object->size - pindex;
2312 * If we are processing a major portion of the object, then scan the
2315 * We cannot safely scan the object's memq unless we are in a
2316 * critical section since interrupts can remove pages from objects.
2320 if (psize > (object->resident_page_count >> 2)) {
2323 for (p = TAILQ_FIRST(&object->memq);
2324 objpgs > 0 && p != NULL;
2325 p = TAILQ_NEXT(p, listq)
2328 if (tmpidx < pindex)
2331 if (tmpidx >= psize)
2335 * don't allow an madvise to blow away our really
2336 * free pages allocating pv entries.
2338 if ((limit & MAP_PREFAULT_MADVISE) &&
2339 vmstats.v_free_count < vmstats.v_free_reserved) {
2342 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2344 (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2345 if ((p->queue - p->pc) == PQ_CACHE)
2346 vm_page_deactivate(p);
2348 mpte = pmap_enter_quick(pmap,
2349 addr + i386_ptob(tmpidx), p, mpte);
2350 vm_page_flag_set(p, PG_MAPPED);
2357 * else lookup the pages one-by-one.
2359 for (tmpidx = 0; tmpidx < psize; tmpidx += 1) {
2361 * don't allow an madvise to blow away our really
2362 * free pages allocating pv entries.
2364 if ((limit & MAP_PREFAULT_MADVISE) &&
2365 vmstats.v_free_count < vmstats.v_free_reserved) {
2368 p = vm_page_lookup(object, tmpidx + pindex);
2370 ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2372 (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2373 if ((p->queue - p->pc) == PQ_CACHE)
2374 vm_page_deactivate(p);
2376 mpte = pmap_enter_quick(pmap,
2377 addr + i386_ptob(tmpidx), p, mpte);
2378 vm_page_flag_set(p, PG_MAPPED);
2387 * pmap_prefault provides a quick way of clustering pagefaults into a
2388 * processes address space. It is a "cousin" of pmap_object_init_pt,
2389 * except it runs at page fault time instead of mmap time.
2393 #define PAGEORDER_SIZE (PFBAK+PFFOR)
2395 static int pmap_prefault_pageorder[] = {
2396 -PAGE_SIZE, PAGE_SIZE,
2397 -2 * PAGE_SIZE, 2 * PAGE_SIZE,
2398 -3 * PAGE_SIZE, 3 * PAGE_SIZE,
2399 -4 * PAGE_SIZE, 4 * PAGE_SIZE
2403 pmap_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry)
2412 if (!curproc || (pmap != vmspace_pmap(curproc->p_vmspace)))
2415 object = entry->object.vm_object;
2417 starta = addra - PFBAK * PAGE_SIZE;
2418 if (starta < entry->start)
2419 starta = entry->start;
2420 else if (starta > addra)
2424 * critical section protection is required to maintain the
2425 * page/object association, interrupts can free pages and remove
2426 * them from their objects.
2430 for (i = 0; i < PAGEORDER_SIZE; i++) {
2431 vm_object_t lobject;
2434 addr = addra + pmap_prefault_pageorder[i];
2435 if (addr > addra + (PFFOR * PAGE_SIZE))
2438 if (addr < starta || addr >= entry->end)
2441 if ((*pmap_pde(pmap, addr)) == NULL)
2444 pte = (unsigned *) vtopte(addr);
2448 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2451 for (m = vm_page_lookup(lobject, pindex);
2452 (!m && (lobject->type == OBJT_DEFAULT) &&
2453 (lobject->backing_object));
2454 lobject = lobject->backing_object
2456 if (lobject->backing_object_offset & PAGE_MASK)
2458 pindex += (lobject->backing_object_offset >> PAGE_SHIFT);
2459 m = vm_page_lookup(lobject->backing_object, pindex);
2463 * give-up when a page is not in memory
2468 if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2470 (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2472 if ((m->queue - m->pc) == PQ_CACHE) {
2473 vm_page_deactivate(m);
2476 mpte = pmap_enter_quick(pmap, addr, m, mpte);
2477 vm_page_flag_set(m, PG_MAPPED);
2485 * Routine: pmap_change_wiring
2486 * Function: Change the wiring attribute for a map/virtual-address
2488 * In/out conditions:
2489 * The mapping must already exist in the pmap.
2492 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2499 pte = pmap_pte(pmap, va);
2501 if (wired && !pmap_pte_w(pte))
2502 pmap->pm_stats.wired_count++;
2503 else if (!wired && pmap_pte_w(pte))
2504 pmap->pm_stats.wired_count--;
2507 * Wiring is not a hardware characteristic so there is no need to
2508 * invalidate TLB. However, in an SMP environment we must use
2509 * a locked bus cycle to update the pte (if we are not using
2510 * the pmap_inval_*() API that is)... it's ok to do this for simple
2515 atomic_set_int(pte, PG_W);
2517 atomic_clear_int(pte, PG_W);
2520 atomic_set_int_nonlocked(pte, PG_W);
2522 atomic_clear_int_nonlocked(pte, PG_W);
2529 * Copy the range specified by src_addr/len
2530 * from the source map to the range dst_addr/len
2531 * in the destination map.
2533 * This routine is only advisory and need not do anything.
2536 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2537 vm_size_t len, vm_offset_t src_addr)
2539 pmap_inval_info info;
2541 vm_offset_t end_addr = src_addr + len;
2543 unsigned src_frame, dst_frame;
2546 if (dst_addr != src_addr)
2549 src_frame = ((unsigned) src_pmap->pm_pdir[PTDPTDI]) & PG_FRAME;
2550 if (src_frame != (((unsigned) PTDpde) & PG_FRAME)) {
2554 dst_frame = ((unsigned) dst_pmap->pm_pdir[PTDPTDI]) & PG_FRAME;
2555 if (dst_frame != (((unsigned) APTDpde) & PG_FRAME)) {
2556 APTDpde = (pd_entry_t) (dst_frame | PG_RW | PG_V);
2557 /* The page directory is not shared between CPUs */
2560 pmap_inval_init(&info);
2561 pmap_inval_add(&info, dst_pmap, -1);
2562 pmap_inval_add(&info, src_pmap, -1);
2565 * critical section protection is required to maintain the page/object
2566 * association, interrupts can free pages and remove them from
2570 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
2571 unsigned *src_pte, *dst_pte;
2572 vm_page_t dstmpte, srcmpte;
2573 vm_offset_t srcptepaddr;
2576 if (addr >= UPT_MIN_ADDRESS)
2577 panic("pmap_copy: invalid to pmap_copy page tables\n");
2580 * Don't let optional prefaulting of pages make us go
2581 * way below the low water mark of free pages or way
2582 * above high water mark of used pv entries.
2584 if (vmstats.v_free_count < vmstats.v_free_reserved ||
2585 pv_entry_count > pv_entry_high_water)
2588 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
2589 ptepindex = addr >> PDRSHIFT;
2591 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex];
2592 if (srcptepaddr == 0)
2595 if (srcptepaddr & PG_PS) {
2596 if (dst_pmap->pm_pdir[ptepindex] == 0) {
2597 dst_pmap->pm_pdir[ptepindex] = (pd_entry_t) srcptepaddr;
2598 dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
2603 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
2604 if ((srcmpte == NULL) ||
2605 (srcmpte->hold_count == 0) || (srcmpte->flags & PG_BUSY))
2608 if (pdnxt > end_addr)
2611 src_pte = (unsigned *) vtopte(addr);
2612 dst_pte = (unsigned *) avtopte(addr);
2613 while (addr < pdnxt) {
2617 * we only virtual copy managed pages
2619 if ((ptetemp & PG_MANAGED) != 0) {
2621 * We have to check after allocpte for the
2622 * pte still being around... allocpte can
2625 dstmpte = pmap_allocpte(dst_pmap, addr);
2626 if ((*dst_pte == 0) && (ptetemp = *src_pte)) {
2628 * Clear the modified and
2629 * accessed (referenced) bits
2632 m = PHYS_TO_VM_PAGE(ptetemp);
2633 *dst_pte = ptetemp & ~(PG_M | PG_A);
2634 dst_pmap->pm_stats.resident_count++;
2635 pmap_insert_entry(dst_pmap, addr,
2638 pmap_unwire_pte_hold(dst_pmap, dstmpte, &info);
2640 if (dstmpte->hold_count >= srcmpte->hold_count)
2649 pmap_inval_flush(&info);
2653 * Routine: pmap_kernel
2655 * Returns the physical map handle for the kernel.
2660 return (kernel_pmap);
2666 * Zero the specified PA by mapping the page into KVM and clearing its
2669 * This function may be called from an interrupt and no locking is
2673 pmap_zero_page(vm_paddr_t phys)
2675 struct mdglobaldata *gd = mdcpu;
2678 if (*(int *)gd->gd_CMAP3)
2679 panic("pmap_zero_page: CMAP3 busy");
2680 *(int *)gd->gd_CMAP3 =
2681 PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2682 cpu_invlpg(gd->gd_CADDR3);
2684 #if defined(I686_CPU)
2685 if (cpu_class == CPUCLASS_686)
2686 i686_pagezero(gd->gd_CADDR3);
2689 bzero(gd->gd_CADDR3, PAGE_SIZE);
2690 *(int *) gd->gd_CMAP3 = 0;
2695 * pmap_page_assertzero:
2697 * Assert that a page is empty, panic if it isn't.
2700 pmap_page_assertzero(vm_paddr_t phys)
2702 struct mdglobaldata *gd = mdcpu;
2706 if (*(int *)gd->gd_CMAP3)
2707 panic("pmap_zero_page: CMAP3 busy");
2708 *(int *)gd->gd_CMAP3 =
2709 PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2710 cpu_invlpg(gd->gd_CADDR3);
2711 for (i = 0; i < PAGE_SIZE; i += 4) {
2712 if (*(int *)((char *)gd->gd_CADDR3 + i) != 0) {
2713 panic("pmap_page_assertzero() @ %p not zero!\n",
2714 (void *)gd->gd_CADDR3);
2717 *(int *) gd->gd_CMAP3 = 0;
2724 * Zero part of a physical page by mapping it into memory and clearing
2725 * its contents with bzero.
2727 * off and size may not cover an area beyond a single hardware page.
2730 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
2732 struct mdglobaldata *gd = mdcpu;
2735 if (*(int *) gd->gd_CMAP3)
2736 panic("pmap_zero_page: CMAP3 busy");
2737 *(int *) gd->gd_CMAP3 = PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2738 cpu_invlpg(gd->gd_CADDR3);
2740 #if defined(I686_CPU)
2741 if (cpu_class == CPUCLASS_686 && off == 0 && size == PAGE_SIZE)
2742 i686_pagezero(gd->gd_CADDR3);
2745 bzero((char *)gd->gd_CADDR3 + off, size);
2746 *(int *) gd->gd_CMAP3 = 0;
2753 * Copy the physical page from the source PA to the target PA.
2754 * This function may be called from an interrupt. No locking
2758 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
2760 struct mdglobaldata *gd = mdcpu;
2763 if (*(int *) gd->gd_CMAP1)
2764 panic("pmap_copy_page: CMAP1 busy");
2765 if (*(int *) gd->gd_CMAP2)
2766 panic("pmap_copy_page: CMAP2 busy");
2768 *(int *) gd->gd_CMAP1 = PG_V | (src & PG_FRAME) | PG_A;
2769 *(int *) gd->gd_CMAP2 = PG_V | PG_RW | (dst & PG_FRAME) | PG_A | PG_M;
2771 cpu_invlpg(gd->gd_CADDR1);
2772 cpu_invlpg(gd->gd_CADDR2);
2774 bcopy(gd->gd_CADDR1, gd->gd_CADDR2, PAGE_SIZE);
2776 *(int *) gd->gd_CMAP1 = 0;
2777 *(int *) gd->gd_CMAP2 = 0;
2782 * pmap_copy_page_frag:
2784 * Copy the physical page from the source PA to the target PA.
2785 * This function may be called from an interrupt. No locking
2789 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
2791 struct mdglobaldata *gd = mdcpu;
2794 if (*(int *) gd->gd_CMAP1)
2795 panic("pmap_copy_page: CMAP1 busy");
2796 if (*(int *) gd->gd_CMAP2)
2797 panic("pmap_copy_page: CMAP2 busy");
2799 *(int *) gd->gd_CMAP1 = PG_V | (src & PG_FRAME) | PG_A;
2800 *(int *) gd->gd_CMAP2 = PG_V | PG_RW | (dst & PG_FRAME) | PG_A | PG_M;
2802 cpu_invlpg(gd->gd_CADDR1);
2803 cpu_invlpg(gd->gd_CADDR2);
2805 bcopy((char *)gd->gd_CADDR1 + (src & PAGE_MASK),
2806 (char *)gd->gd_CADDR2 + (dst & PAGE_MASK),
2809 *(int *) gd->gd_CMAP1 = 0;
2810 *(int *) gd->gd_CMAP2 = 0;
2815 * Returns true if the pmap's pv is one of the first
2816 * 16 pvs linked to from this page. This count may
2817 * be changed upwards or downwards in the future; it
2818 * is only necessary that true be returned for a small
2819 * subset of pmaps for proper page aging.
2822 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2827 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2832 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2833 if (pv->pv_pmap == pmap) {
2845 #define PMAP_REMOVE_PAGES_CURPROC_ONLY
2847 * Remove all pages from specified address space
2848 * this aids process exit speeds. Also, this code
2849 * is special cased for current process only, but
2850 * can have the more generic (and slightly slower)
2851 * mode enabled. This is much faster than pmap_remove
2852 * in the case of running down an entire address space.
2855 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2857 unsigned *pte, tpte;
2860 pmap_inval_info info;
2862 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2863 if (!curproc || (pmap != vmspace_pmap(curproc->p_vmspace))) {
2864 printf("warning: pmap_remove_pages called with non-current pmap\n");
2869 pmap_inval_init(&info);
2871 for(pv = TAILQ_FIRST(&pmap->pm_pvlist);
2875 if (pv->pv_va >= eva || pv->pv_va < sva) {
2876 npv = TAILQ_NEXT(pv, pv_plist);
2880 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2881 pte = (unsigned *)vtopte(pv->pv_va);
2883 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2885 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
2889 * We cannot remove wired pages from a process' mapping at this time
2892 npv = TAILQ_NEXT(pv, pv_plist);
2897 m = PHYS_TO_VM_PAGE(tpte);
2899 KASSERT(m < &vm_page_array[vm_page_array_size],
2900 ("pmap_remove_pages: bad tpte %x", tpte));
2902 pv->pv_pmap->pm_stats.resident_count--;
2905 * Update the vm_page_t clean and reference bits.
2912 npv = TAILQ_NEXT(pv, pv_plist);
2913 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2915 m->md.pv_list_count--;
2916 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2917 if (TAILQ_FIRST(&m->md.pv_list) == NULL) {
2918 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2921 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem, &info);
2924 pmap_inval_flush(&info);
2929 * pmap_testbit tests bits in pte's
2930 * note that the testbit/changebit routines are inline,
2931 * and a lot of things compile-time evaluate.
2934 pmap_testbit(vm_page_t m, int bit)
2939 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2942 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2947 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2949 * if the bit being tested is the modified bit, then
2950 * mark clean_map and ptes as never
2953 if (bit & (PG_A|PG_M)) {
2954 if (!pmap_track_modified(pv->pv_va))
2958 #if defined(PMAP_DIAGNOSTIC)
2960 printf("Null pmap (tb) at va: 0x%x\n", pv->pv_va);
2964 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2975 * this routine is used to modify bits in ptes
2977 static __inline void
2978 pmap_changebit(vm_page_t m, int bit, boolean_t setem)
2980 struct pmap_inval_info info;
2984 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2987 pmap_inval_init(&info);
2991 * Loop over all current mappings setting/clearing as appropos If
2992 * setting RO do we need to clear the VAC?
2994 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2996 * don't write protect pager mappings
2998 if (!setem && (bit == PG_RW)) {
2999 if (!pmap_track_modified(pv->pv_va))
3003 #if defined(PMAP_DIAGNOSTIC)
3005 printf("Null pmap (cb) at va: 0x%x\n", pv->pv_va);
3011 * Careful here. We can use a locked bus instruction to
3012 * clear PG_A or PG_M safely but we need to synchronize
3013 * with the target cpus when we mess with PG_RW.
3015 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3017 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
3021 atomic_set_int(pte, bit);
3023 atomic_set_int_nonlocked(pte, bit);
3026 vm_offset_t pbits = *(vm_offset_t *)pte;
3033 atomic_clear_int(pte, PG_M|PG_RW);
3035 atomic_clear_int_nonlocked(pte, PG_M|PG_RW);
3039 atomic_clear_int(pte, bit);
3041 atomic_clear_int_nonlocked(pte, bit);
3047 pmap_inval_flush(&info);
3052 * pmap_page_protect:
3054 * Lower the permission for all mappings to a given page.
3057 pmap_page_protect(vm_page_t m, vm_prot_t prot)
3059 if ((prot & VM_PROT_WRITE) == 0) {
3060 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
3061 pmap_changebit(m, PG_RW, FALSE);
3069 pmap_phys_address(int ppn)
3071 return (i386_ptob(ppn));
3075 * pmap_ts_referenced:
3077 * Return a count of reference bits for a page, clearing those bits.
3078 * It is not necessary for every reference bit to be cleared, but it
3079 * is necessary that 0 only be returned when there are truly no
3080 * reference bits set.
3082 * XXX: The exact number of bits to check and clear is a matter that
3083 * should be tested and standardized at some point in the future for
3084 * optimal aging of shared pages.
3087 pmap_ts_referenced(vm_page_t m)
3089 pv_entry_t pv, pvf, pvn;
3093 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3098 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3103 pvn = TAILQ_NEXT(pv, pv_list);
3105 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3107 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3109 if (!pmap_track_modified(pv->pv_va))
3112 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3114 if (pte && (*pte & PG_A)) {
3116 atomic_clear_int(pte, PG_A);
3118 atomic_clear_int_nonlocked(pte, PG_A);
3125 } while ((pv = pvn) != NULL && pv != pvf);
3135 * Return whether or not the specified physical page was modified
3136 * in any physical maps.
3139 pmap_is_modified(vm_page_t m)
3141 return pmap_testbit(m, PG_M);
3145 * Clear the modify bits on the specified physical page.
3148 pmap_clear_modify(vm_page_t m)
3150 pmap_changebit(m, PG_M, FALSE);
3154 * pmap_clear_reference:
3156 * Clear the reference bit on the specified physical page.
3159 pmap_clear_reference(vm_page_t m)
3161 pmap_changebit(m, PG_A, FALSE);
3165 * Miscellaneous support routines follow
3169 i386_protection_init(void)
3173 kp = protection_codes;
3174 for (prot = 0; prot < 8; prot++) {
3176 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE:
3178 * Read access is also 0. There isn't any execute bit,
3179 * so just make it readable.
3181 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE:
3182 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE:
3183 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE:
3186 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE:
3187 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE:
3188 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE:
3189 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
3197 * Map a set of physical memory pages into the kernel virtual
3198 * address space. Return a pointer to where it is mapped. This
3199 * routine is intended to be used for mapping device memory,
3202 * NOTE: we can't use pgeflag unless we invalidate the pages one at
3206 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
3208 vm_offset_t va, tmpva, offset;
3211 offset = pa & PAGE_MASK;
3212 size = roundup(offset + size, PAGE_SIZE);
3214 va = kmem_alloc_nofault(kernel_map, size);
3216 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3219 for (tmpva = va; size > 0;) {
3220 pte = (unsigned *)vtopte(tmpva);
3221 *pte = pa | PG_RW | PG_V; /* | pgeflag; */
3229 return ((void *)(va + offset));
3233 pmap_unmapdev(vm_offset_t va, vm_size_t size)
3235 vm_offset_t base, offset;
3237 base = va & PG_FRAME;
3238 offset = va & PAGE_MASK;
3239 size = roundup(offset + size, PAGE_SIZE);
3240 pmap_qremove(va, size >> PAGE_SHIFT);
3241 kmem_free(kernel_map, base, size);
3245 * perform the pmap work for mincore
3248 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3250 unsigned *ptep, pte;
3254 ptep = pmap_pte(pmap, addr);
3259 if ((pte = *ptep) != 0) {
3262 val = MINCORE_INCORE;
3263 if ((pte & PG_MANAGED) == 0)
3266 pa = pte & PG_FRAME;
3268 m = PHYS_TO_VM_PAGE(pa);
3274 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3276 * Modified by someone
3278 else if (m->dirty || pmap_is_modified(m))
3279 val |= MINCORE_MODIFIED_OTHER;
3284 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3287 * Referenced by someone
3289 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3290 val |= MINCORE_REFERENCED_OTHER;
3291 vm_page_flag_set(m, PG_REFERENCED);
3298 pmap_activate(struct proc *p)
3302 pmap = vmspace_pmap(p->p_vmspace);
3304 atomic_set_int(&pmap->pm_active, 1 << mycpu->gd_cpuid);
3306 pmap->pm_active |= 1;
3308 #if defined(SWTCH_OPTIM_STATS)
3311 p->p_thread->td_pcb->pcb_cr3 = vtophys(pmap->pm_pdir);
3312 load_cr3(p->p_thread->td_pcb->pcb_cr3);
3316 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3319 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3323 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3328 #if defined(PMAP_DEBUG)
3330 pmap_pid_dump(int pid)
3336 FOREACH_PROC_IN_SYSTEM(p) {
3337 if (p->p_pid != pid)
3343 pmap = vmspace_pmap(p->p_vmspace);
3344 for(i=0;i<1024;i++) {
3347 unsigned base = i << PDRSHIFT;
3349 pde = &pmap->pm_pdir[i];
3350 if (pde && pmap_pde_v(pde)) {
3351 for(j=0;j<1024;j++) {
3352 unsigned va = base + (j << PAGE_SHIFT);
3353 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) {
3360 pte = pmap_pte_quick( pmap, va);
3361 if (pte && pmap_pte_v(pte)) {
3365 m = PHYS_TO_VM_PAGE(pa);
3366 printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x",
3367 va, pa, m->hold_count, m->wire_count, m->flags);
3388 static void pads (pmap_t pm);
3389 void pmap_pvdump (vm_paddr_t pa);
3391 /* print address space of pmap*/
3398 if (pm == kernel_pmap)
3400 for (i = 0; i < 1024; i++)
3402 for (j = 0; j < 1024; j++) {
3403 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
3404 if (pm == kernel_pmap && va < KERNBASE)
3406 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS)
3408 ptep = pmap_pte_quick(pm, va);
3409 if (pmap_pte_v(ptep))
3410 printf("%x:%x ", va, *(int *) ptep);
3416 pmap_pvdump(vm_paddr_t pa)
3421 printf("pa %08llx", (long long)pa);
3422 m = PHYS_TO_VM_PAGE(pa);
3423 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3425 printf(" -> pmap %p, va %x, flags %x",
3426 (void *)pv->pv_pmap, pv->pv_va, pv->pv_flags);
3428 printf(" -> pmap %p, va %x", (void *)pv->pv_pmap, pv->pv_va);