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.48 2005/06/03 17:14:48 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/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_switch = cpu_heavy_switch;
920 bzero(p->p_addr, sizeof(*p->p_addr));
924 * Dispose the UPAGES for a process that has exited.
925 * This routine directly impacts the exit perf of a process.
928 pmap_dispose_proc(struct proc *p)
932 KASSERT(p->p_lock == 0, ("attempt to dispose referenced proc! %p", p));
934 if ((td = p->p_thread) != NULL) {
943 * Allow the UPAGES for a process to be prejudicially paged out.
946 pmap_swapout_proc(struct proc *p)
954 upobj = p->p_upages_obj;
957 * Unwiring the pages allow them to be paged to their backing store
961 for (i = 0; i < UPAGES; i++) {
962 if ((m = vm_page_lookup(upobj, i)) == NULL)
963 panic("pmap_swapout_proc: upage already missing???");
965 vm_page_unwire(m, 0);
966 pmap_kremove((vm_offset_t)p->p_addr + (PAGE_SIZE * i));
973 * Bring the UPAGES for a specified process back in.
976 pmap_swapin_proc(struct proc *p)
984 upobj = p->p_upages_obj;
985 for (i = 0; i < UPAGES; i++) {
986 m = vm_page_grab(upobj, i, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
988 pmap_kenter((vm_offset_t)p->p_addr + (i * PAGE_SIZE),
991 if (m->valid != VM_PAGE_BITS_ALL) {
992 rv = vm_pager_get_pages(upobj, &m, 1, 0);
993 if (rv != VM_PAGER_OK)
994 panic("pmap_swapin_proc: cannot get upages for proc: %d\n", p->p_pid);
995 m = vm_page_lookup(upobj, i);
996 m->valid = VM_PAGE_BITS_ALL;
1000 vm_page_flag_set(m, PG_MAPPED | PG_WRITEABLE);
1006 /***************************************************
1007 * Page table page management routines.....
1008 ***************************************************/
1011 * This routine unholds page table pages, and if the hold count
1012 * drops to zero, then it decrements the wire count.
1015 _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, pmap_inval_info_t info)
1017 pmap_inval_flush(info);
1018 while (vm_page_sleep_busy(m, FALSE, "pmuwpt"))
1021 if (m->hold_count == 0) {
1023 * unmap the page table page
1025 pmap_inval_add(info, pmap, -1);
1026 pmap->pm_pdir[m->pindex] = 0;
1027 --pmap->pm_stats.resident_count;
1029 if (pmap->pm_ptphint == m)
1030 pmap->pm_ptphint = NULL;
1033 * If the page is finally unwired, simply free it.
1036 if (m->wire_count == 0) {
1039 vm_page_free_zero(m);
1040 --vmstats.v_wire_count;
1047 static PMAP_INLINE int
1048 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, pmap_inval_info_t info)
1051 if (m->hold_count == 0)
1052 return _pmap_unwire_pte_hold(pmap, m, info);
1058 * After removing a page table entry, this routine is used to
1059 * conditionally free the page, and manage the hold/wire counts.
1062 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte,
1063 pmap_inval_info_t info)
1066 if (va >= UPT_MIN_ADDRESS)
1070 ptepindex = (va >> PDRSHIFT);
1071 if (pmap->pm_ptphint &&
1072 (pmap->pm_ptphint->pindex == ptepindex)) {
1073 mpte = pmap->pm_ptphint;
1075 pmap_inval_flush(info);
1076 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1077 pmap->pm_ptphint = mpte;
1081 return pmap_unwire_pte_hold(pmap, mpte, info);
1085 pmap_pinit0(struct pmap *pmap)
1088 (pd_entry_t *)kmem_alloc_pageable(kernel_map, PAGE_SIZE);
1089 pmap_kenter((vm_offset_t)pmap->pm_pdir, (vm_offset_t) IdlePTD);
1091 pmap->pm_active = 0;
1092 pmap->pm_ptphint = NULL;
1093 TAILQ_INIT(&pmap->pm_pvlist);
1094 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1098 * Initialize a preallocated and zeroed pmap structure,
1099 * such as one in a vmspace structure.
1102 pmap_pinit(struct pmap *pmap)
1107 * No need to allocate page table space yet but we do need a valid
1108 * page directory table.
1110 if (pmap->pm_pdir == NULL) {
1112 (pd_entry_t *)kmem_alloc_pageable(kernel_map, PAGE_SIZE);
1116 * allocate object for the ptes
1118 if (pmap->pm_pteobj == NULL)
1119 pmap->pm_pteobj = vm_object_allocate( OBJT_DEFAULT, PTDPTDI + 1);
1122 * allocate the page directory page
1124 ptdpg = vm_page_grab( pmap->pm_pteobj, PTDPTDI,
1125 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1127 ptdpg->wire_count = 1;
1128 ++vmstats.v_wire_count;
1131 vm_page_flag_clear(ptdpg, PG_MAPPED | PG_BUSY); /* not usually mapped*/
1132 ptdpg->valid = VM_PAGE_BITS_ALL;
1134 pmap_kenter((vm_offset_t)pmap->pm_pdir, VM_PAGE_TO_PHYS(ptdpg));
1135 if ((ptdpg->flags & PG_ZERO) == 0)
1136 bzero(pmap->pm_pdir, PAGE_SIZE);
1138 pmap->pm_pdir[MPPTDI] = PTD[MPPTDI];
1140 /* install self-referential address mapping entry */
1141 *(unsigned *) (pmap->pm_pdir + PTDPTDI) =
1142 VM_PAGE_TO_PHYS(ptdpg) | PG_V | PG_RW | PG_A | PG_M;
1145 pmap->pm_active = 0;
1146 pmap->pm_ptphint = NULL;
1147 TAILQ_INIT(&pmap->pm_pvlist);
1148 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1152 * Wire in kernel global address entries. To avoid a race condition
1153 * between pmap initialization and pmap_growkernel, this procedure
1154 * should be called after the vmspace is attached to the process
1155 * but before this pmap is activated.
1158 pmap_pinit2(struct pmap *pmap)
1160 /* XXX copies current process, does not fill in MPPTDI */
1161 bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * PTESIZE);
1165 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1167 unsigned *pde = (unsigned *) pmap->pm_pdir;
1169 * This code optimizes the case of freeing non-busy
1170 * page-table pages. Those pages are zero now, and
1171 * might as well be placed directly into the zero queue.
1173 if (vm_page_sleep_busy(p, FALSE, "pmaprl"))
1179 * Remove the page table page from the processes address space.
1182 pmap->pm_stats.resident_count--;
1184 if (p->hold_count) {
1185 panic("pmap_release: freeing held page table page");
1188 * Page directory pages need to have the kernel
1189 * stuff cleared, so they can go into the zero queue also.
1191 if (p->pindex == PTDPTDI) {
1192 bzero(pde + KPTDI, nkpt * PTESIZE);
1195 pmap_kremove((vm_offset_t)pmap->pm_pdir);
1198 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1199 pmap->pm_ptphint = NULL;
1202 vmstats.v_wire_count--;
1203 vm_page_free_zero(p);
1208 * this routine is called if the page table page is not
1212 _pmap_allocpte(pmap_t pmap, unsigned ptepindex)
1214 vm_offset_t pteva, ptepa;
1218 * Find or fabricate a new pagetable page
1220 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1221 VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1223 KASSERT(m->queue == PQ_NONE,
1224 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1226 if (m->wire_count == 0)
1227 vmstats.v_wire_count++;
1231 * Increment the hold count for the page table page
1232 * (denoting a new mapping.)
1237 * Map the pagetable page into the process address space, if
1238 * it isn't already there.
1241 pmap->pm_stats.resident_count++;
1243 ptepa = VM_PAGE_TO_PHYS(m);
1244 pmap->pm_pdir[ptepindex] =
1245 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_A | PG_M);
1248 * Set the page table hint
1250 pmap->pm_ptphint = m;
1253 * Try to use the new mapping, but if we cannot, then
1254 * do it with the routine that maps the page explicitly.
1256 if ((m->flags & PG_ZERO) == 0) {
1257 if ((((unsigned)pmap->pm_pdir[PTDPTDI]) & PG_FRAME) ==
1258 (((unsigned) PTDpde) & PG_FRAME)) {
1259 pteva = UPT_MIN_ADDRESS + i386_ptob(ptepindex);
1260 bzero((caddr_t) pteva, PAGE_SIZE);
1262 pmap_zero_page(ptepa);
1266 m->valid = VM_PAGE_BITS_ALL;
1267 vm_page_flag_clear(m, PG_ZERO);
1268 vm_page_flag_set(m, PG_MAPPED);
1275 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1282 * Calculate pagetable page index
1284 ptepindex = va >> PDRSHIFT;
1287 * Get the page directory entry
1289 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1292 * This supports switching from a 4MB page to a
1295 if (ptepa & PG_PS) {
1296 pmap->pm_pdir[ptepindex] = 0;
1303 * If the page table page is mapped, we just increment the
1304 * hold count, and activate it.
1308 * In order to get the page table page, try the
1311 if (pmap->pm_ptphint &&
1312 (pmap->pm_ptphint->pindex == ptepindex)) {
1313 m = pmap->pm_ptphint;
1315 m = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1316 pmap->pm_ptphint = m;
1322 * Here if the pte page isn't mapped, or if it has been deallocated.
1324 return _pmap_allocpte(pmap, ptepindex);
1328 /***************************************************
1329 * Pmap allocation/deallocation routines.
1330 ***************************************************/
1333 * Release any resources held by the given physical map.
1334 * Called when a pmap initialized by pmap_pinit is being released.
1335 * Should only be called if the map contains no valid mappings.
1338 pmap_release(struct pmap *pmap)
1340 vm_page_t p,n,ptdpg;
1341 vm_object_t object = pmap->pm_pteobj;
1344 #if defined(DIAGNOSTIC)
1345 if (object->ref_count != 1)
1346 panic("pmap_release: pteobj reference count != 1");
1352 curgeneration = object->generation;
1353 for (p = TAILQ_FIRST(&object->memq); p != NULL; p = n) {
1354 n = TAILQ_NEXT(p, listq);
1355 if (p->pindex == PTDPTDI) {
1360 if (!pmap_release_free_page(pmap, p) &&
1361 (object->generation != curgeneration)) {
1369 if (ptdpg && !pmap_release_free_page(pmap, ptdpg))
1374 kvm_size(SYSCTL_HANDLER_ARGS)
1376 unsigned long ksize = VM_MAX_KERNEL_ADDRESS - KERNBASE;
1378 return sysctl_handle_long(oidp, &ksize, 0, req);
1380 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD,
1381 0, 0, kvm_size, "IU", "Size of KVM");
1384 kvm_free(SYSCTL_HANDLER_ARGS)
1386 unsigned long kfree = VM_MAX_KERNEL_ADDRESS - kernel_vm_end;
1388 return sysctl_handle_long(oidp, &kfree, 0, req);
1390 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD,
1391 0, 0, kvm_free, "IU", "Amount of KVM free");
1394 * grow the number of kernel page table entries, if needed
1397 pmap_growkernel(vm_offset_t addr)
1401 vm_offset_t ptppaddr;
1406 if (kernel_vm_end == 0) {
1407 kernel_vm_end = KERNBASE;
1409 while (pdir_pde(PTD, kernel_vm_end)) {
1410 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1414 addr = (addr + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1415 while (kernel_vm_end < addr) {
1416 if (pdir_pde(PTD, kernel_vm_end)) {
1417 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1422 * This index is bogus, but out of the way
1424 nkpg = vm_page_alloc(kptobj, nkpt,
1425 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM | VM_ALLOC_INTERRUPT);
1427 panic("pmap_growkernel: no memory to grow kernel");
1432 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1433 pmap_zero_page(ptppaddr);
1434 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
1435 pdir_pde(PTD, kernel_vm_end) = newpdir;
1437 FOREACH_PROC_IN_SYSTEM(p) {
1439 pmap = vmspace_pmap(p->p_vmspace);
1440 *pmap_pde(pmap, kernel_vm_end) = newpdir;
1443 *pmap_pde(kernel_pmap, kernel_vm_end) = newpdir;
1444 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1450 * Retire the given physical map from service.
1451 * Should only be called if the map contains
1452 * no valid mappings.
1455 pmap_destroy(pmap_t pmap)
1462 count = --pmap->pm_count;
1465 panic("destroying a pmap is not yet implemented");
1470 * Add a reference to the specified pmap.
1473 pmap_reference(pmap_t pmap)
1480 /***************************************************
1481 * page management routines.
1482 ***************************************************/
1485 * free the pv_entry back to the free list. This function may be
1486 * called from an interrupt.
1488 static PMAP_INLINE void
1489 free_pv_entry(pv_entry_t pv)
1496 * get a new pv_entry, allocating a block from the system
1497 * when needed. This function may be called from an interrupt.
1503 if (pv_entry_high_water &&
1504 (pv_entry_count > pv_entry_high_water) &&
1505 (pmap_pagedaemon_waken == 0)) {
1506 pmap_pagedaemon_waken = 1;
1507 wakeup (&vm_pages_needed);
1509 return zalloc(pvzone);
1513 * This routine is very drastic, but can save the system
1521 static int warningdone=0;
1523 if (pmap_pagedaemon_waken == 0)
1526 if (warningdone < 5) {
1527 printf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1531 for(i = 0; i < vm_page_array_size; i++) {
1532 m = &vm_page_array[i];
1533 if (m->wire_count || m->hold_count || m->busy ||
1534 (m->flags & PG_BUSY))
1538 pmap_pagedaemon_waken = 0;
1543 * If it is the first entry on the list, it is actually
1544 * in the header and we must copy the following entry up
1545 * to the header. Otherwise we must search the list for
1546 * the entry. In either case we free the now unused entry.
1549 pmap_remove_entry(struct pmap *pmap, vm_page_t m,
1550 vm_offset_t va, pmap_inval_info_t info)
1556 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1557 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1558 if (pmap == pv->pv_pmap && va == pv->pv_va)
1562 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1563 if (va == pv->pv_va)
1570 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem, info);
1571 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1572 m->md.pv_list_count--;
1573 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
1574 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1575 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1583 * Create a pv entry for page at pa for
1587 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
1592 pv = get_pv_entry();
1597 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1598 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1599 m->md.pv_list_count++;
1605 * pmap_remove_pte: do the things to unmap a page in a process
1608 pmap_remove_pte(struct pmap *pmap, unsigned *ptq, vm_offset_t va,
1609 pmap_inval_info_t info)
1614 pmap_inval_add(info, pmap, va);
1615 oldpte = loadandclear(ptq);
1617 pmap->pm_stats.wired_count -= 1;
1619 * Machines that don't support invlpg, also don't support
1620 * PG_G. XXX PG_G is disabled for SMP so don't worry about
1624 cpu_invlpg((void *)va);
1625 pmap->pm_stats.resident_count -= 1;
1626 if (oldpte & PG_MANAGED) {
1627 m = PHYS_TO_VM_PAGE(oldpte);
1628 if (oldpte & PG_M) {
1629 #if defined(PMAP_DIAGNOSTIC)
1630 if (pmap_nw_modified((pt_entry_t) oldpte)) {
1632 "pmap_remove: modified page not writable: va: 0x%x, pte: 0x%x\n",
1636 if (pmap_track_modified(va))
1640 vm_page_flag_set(m, PG_REFERENCED);
1641 return pmap_remove_entry(pmap, m, va, info);
1643 return pmap_unuse_pt(pmap, va, NULL, info);
1652 * Remove a single page from a process address space.
1654 * This function may not be called from an interrupt if the pmap is
1658 pmap_remove_page(struct pmap *pmap, vm_offset_t va, pmap_inval_info_t info)
1663 * if there is no pte for this address, just skip it!!! Otherwise
1664 * get a local va for mappings for this pmap and remove the entry.
1666 if (*pmap_pde(pmap, va) != 0) {
1667 ptq = get_ptbase(pmap) + i386_btop(va);
1669 pmap_remove_pte(pmap, ptq, va, info);
1677 * Remove the given range of addresses from the specified map.
1679 * It is assumed that the start and end are properly
1680 * rounded to the page size.
1682 * This function may not be called from an interrupt if the pmap is
1686 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
1690 vm_offset_t ptpaddr;
1691 vm_offset_t sindex, eindex;
1692 struct pmap_inval_info info;
1697 if (pmap->pm_stats.resident_count == 0)
1700 pmap_inval_init(&info);
1703 * special handling of removing one page. a very
1704 * common operation and easy to short circuit some
1707 if (((sva + PAGE_SIZE) == eva) &&
1708 (((unsigned) pmap->pm_pdir[(sva >> PDRSHIFT)] & PG_PS) == 0)) {
1709 pmap_remove_page(pmap, sva, &info);
1710 pmap_inval_flush(&info);
1715 * Get a local virtual address for the mappings that are being
1718 ptbase = get_ptbase(pmap);
1720 sindex = i386_btop(sva);
1721 eindex = i386_btop(eva);
1723 for (; sindex < eindex; sindex = pdnxt) {
1727 * Calculate index for next page table.
1729 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1730 if (pmap->pm_stats.resident_count == 0)
1733 pdirindex = sindex / NPDEPG;
1734 if (((ptpaddr = (unsigned) pmap->pm_pdir[pdirindex]) & PG_PS) != 0) {
1735 pmap_inval_add(&info, pmap, -1);
1736 pmap->pm_pdir[pdirindex] = 0;
1737 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1742 * Weed out invalid mappings. Note: we assume that the page
1743 * directory table is always allocated, and in kernel virtual.
1749 * Limit our scan to either the end of the va represented
1750 * by the current page table page, or to the end of the
1751 * range being removed.
1753 if (pdnxt > eindex) {
1757 for (; sindex != pdnxt; sindex++) {
1759 if (ptbase[sindex] == 0)
1761 va = i386_ptob(sindex);
1762 if (pmap_remove_pte(pmap, ptbase + sindex, va, &info))
1766 pmap_inval_flush(&info);
1772 * Removes this physical page from all physical maps in which it resides.
1773 * Reflects back modify bits to the pager.
1775 * This routine may not be called from an interrupt.
1779 pmap_remove_all(vm_page_t m)
1781 struct pmap_inval_info info;
1782 unsigned *pte, tpte;
1785 #if defined(PMAP_DIAGNOSTIC)
1787 * XXX this makes pmap_page_protect(NONE) illegal for non-managed
1790 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
1791 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%08llx", (long long)VM_PAGE_TO_PHYS(m));
1795 pmap_inval_init(&info);
1797 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
1798 pv->pv_pmap->pm_stats.resident_count--;
1800 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
1801 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
1803 tpte = loadandclear(pte);
1805 pv->pv_pmap->pm_stats.wired_count--;
1808 vm_page_flag_set(m, PG_REFERENCED);
1811 * Update the vm_page_t clean and reference bits.
1814 #if defined(PMAP_DIAGNOSTIC)
1815 if (pmap_nw_modified((pt_entry_t) tpte)) {
1817 "pmap_remove_all: modified page not writable: va: 0x%x, pte: 0x%x\n",
1821 if (pmap_track_modified(pv->pv_va))
1824 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
1825 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1826 m->md.pv_list_count--;
1827 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem, &info);
1831 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1833 pmap_inval_flush(&info);
1839 * Set the physical protection on the specified range of this map
1842 * This function may not be called from an interrupt if the map is
1843 * not the kernel_pmap.
1846 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
1849 vm_offset_t pdnxt, ptpaddr;
1850 vm_pindex_t sindex, eindex;
1851 pmap_inval_info info;
1856 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1857 pmap_remove(pmap, sva, eva);
1861 if (prot & VM_PROT_WRITE)
1864 pmap_inval_init(&info);
1866 ptbase = get_ptbase(pmap);
1868 sindex = i386_btop(sva);
1869 eindex = i386_btop(eva);
1871 for (; sindex < eindex; sindex = pdnxt) {
1875 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1877 pdirindex = sindex / NPDEPG;
1878 if (((ptpaddr = (unsigned) pmap->pm_pdir[pdirindex]) & PG_PS) != 0) {
1879 pmap_inval_add(&info, pmap, -1);
1880 (unsigned) pmap->pm_pdir[pdirindex] &= ~(PG_M|PG_RW);
1881 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1886 * Weed out invalid mappings. Note: we assume that the page
1887 * directory table is always allocated, and in kernel virtual.
1892 if (pdnxt > eindex) {
1896 for (; sindex != pdnxt; sindex++) {
1901 /* XXX this isn't optimal */
1902 pmap_inval_add(&info, pmap, i386_ptob(sindex));
1903 pbits = ptbase[sindex];
1905 if (pbits & PG_MANAGED) {
1908 m = PHYS_TO_VM_PAGE(pbits);
1909 vm_page_flag_set(m, PG_REFERENCED);
1913 if (pmap_track_modified(i386_ptob(sindex))) {
1915 m = PHYS_TO_VM_PAGE(pbits);
1924 if (pbits != ptbase[sindex]) {
1925 ptbase[sindex] = pbits;
1929 pmap_inval_flush(&info);
1933 * Insert the given physical page (p) at
1934 * the specified virtual address (v) in the
1935 * target physical map with the protection requested.
1937 * If specified, the page will be wired down, meaning
1938 * that the related pte can not be reclaimed.
1940 * NB: This is the only routine which MAY NOT lazy-evaluate
1941 * or lose information. That is, this routine must actually
1942 * insert this page into the given map NOW.
1945 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1951 vm_offset_t origpte, newpte;
1953 pmap_inval_info info;
1959 #ifdef PMAP_DIAGNOSTIC
1960 if (va > VM_MAX_KERNEL_ADDRESS)
1961 panic("pmap_enter: toobig");
1962 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
1963 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%x)", va);
1968 * In the case that a page table page is not
1969 * resident, we are creating it here.
1971 if (va < UPT_MIN_ADDRESS) {
1972 mpte = pmap_allocpte(pmap, va);
1975 pmap_inval_init(&info);
1976 pte = pmap_pte(pmap, va);
1979 * Page Directory table entry not valid, we need a new PT page
1982 panic("pmap_enter: invalid page directory pdir=%x, va=0x%x\n",
1983 (unsigned) pmap->pm_pdir[PTDPTDI], va);
1986 pa = VM_PAGE_TO_PHYS(m) & PG_FRAME;
1987 pmap_inval_add(&info, pmap, va); /* XXX non-optimal */
1988 origpte = *(vm_offset_t *)pte;
1989 opa = origpte & PG_FRAME;
1991 if (origpte & PG_PS)
1992 panic("pmap_enter: attempted pmap_enter on 4MB page");
1995 * Mapping has not changed, must be protection or wiring change.
1997 if (origpte && (opa == pa)) {
1999 * Wiring change, just update stats. We don't worry about
2000 * wiring PT pages as they remain resident as long as there
2001 * are valid mappings in them. Hence, if a user page is wired,
2002 * the PT page will be also.
2004 if (wired && ((origpte & PG_W) == 0))
2005 pmap->pm_stats.wired_count++;
2006 else if (!wired && (origpte & PG_W))
2007 pmap->pm_stats.wired_count--;
2009 #if defined(PMAP_DIAGNOSTIC)
2010 if (pmap_nw_modified((pt_entry_t) origpte)) {
2012 "pmap_enter: modified page not writable: va: 0x%x, pte: 0x%x\n",
2018 * Remove the extra pte reference. Note that we cannot
2019 * optimize the RO->RW case because we have adjusted the
2020 * wiring count above and may need to adjust the wiring
2027 * We might be turning off write access to the page,
2028 * so we go ahead and sense modify status.
2030 if (origpte & PG_MANAGED) {
2031 if ((origpte & PG_M) && pmap_track_modified(va)) {
2033 om = PHYS_TO_VM_PAGE(opa);
2041 * Mapping has changed, invalidate old range and fall through to
2042 * handle validating new mapping.
2046 err = pmap_remove_pte(pmap, pte, va, &info);
2048 panic("pmap_enter: pte vanished, va: 0x%x", va);
2052 * Enter on the PV list if part of our managed memory. Note that we
2053 * raise IPL while manipulating pv_table since pmap_enter can be
2054 * called at interrupt time.
2056 if (pmap_initialized &&
2057 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2058 pmap_insert_entry(pmap, va, mpte, m);
2063 * Increment counters
2065 pmap->pm_stats.resident_count++;
2067 pmap->pm_stats.wired_count++;
2071 * Now validate mapping with desired protection/wiring.
2073 newpte = (vm_offset_t) (pa | pte_prot(pmap, prot) | PG_V);
2077 if (va < UPT_MIN_ADDRESS)
2079 if (pmap == kernel_pmap)
2083 * if the mapping or permission bits are different, we need
2084 * to update the pte.
2086 if ((origpte & ~(PG_M|PG_A)) != newpte) {
2087 *pte = newpte | PG_A;
2089 pmap_inval_flush(&info);
2093 * this code makes some *MAJOR* assumptions:
2094 * 1. Current pmap & pmap exists.
2097 * 4. No page table pages.
2098 * 5. Tlbflush is deferred to calling procedure.
2099 * 6. Page IS managed.
2100 * but is *MUCH* faster than pmap_enter...
2104 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_page_t mpte)
2108 pmap_inval_info info;
2110 pmap_inval_init(&info);
2113 * In the case that a page table page is not
2114 * resident, we are creating it here.
2116 if (va < UPT_MIN_ADDRESS) {
2121 * Calculate pagetable page index
2123 ptepindex = va >> PDRSHIFT;
2124 if (mpte && (mpte->pindex == ptepindex)) {
2129 * Get the page directory entry
2131 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
2134 * If the page table page is mapped, we just increment
2135 * the hold count, and activate it.
2139 panic("pmap_enter_quick: unexpected mapping into 4MB page");
2140 if (pmap->pm_ptphint &&
2141 (pmap->pm_ptphint->pindex == ptepindex)) {
2142 mpte = pmap->pm_ptphint;
2144 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
2145 pmap->pm_ptphint = mpte;
2151 mpte = _pmap_allocpte(pmap, ptepindex);
2159 * This call to vtopte makes the assumption that we are
2160 * entering the page into the current pmap. In order to support
2161 * quick entry into any pmap, one would likely use pmap_pte_quick.
2162 * But that isn't as quick as vtopte.
2164 pte = (unsigned *)vtopte(va);
2167 pmap_unwire_pte_hold(pmap, mpte, &info);
2172 * Enter on the PV list if part of our managed memory. Note that we
2173 * raise IPL while manipulating pv_table since pmap_enter can be
2174 * called at interrupt time.
2176 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0)
2177 pmap_insert_entry(pmap, va, mpte, m);
2180 * Increment counters
2182 pmap->pm_stats.resident_count++;
2184 pa = VM_PAGE_TO_PHYS(m);
2187 * Now validate mapping with RO protection
2189 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2190 *pte = pa | PG_V | PG_U;
2192 *pte = pa | PG_V | PG_U | PG_MANAGED;
2198 * Make a temporary mapping for a physical address. This is only intended
2199 * to be used for panic dumps.
2202 pmap_kenter_temporary(vm_paddr_t pa, int i)
2204 pmap_kenter((vm_offset_t)crashdumpmap + (i * PAGE_SIZE), pa);
2205 return ((void *)crashdumpmap);
2208 #define MAX_INIT_PT (96)
2211 * This routine preloads the ptes for a given object into the specified pmap.
2212 * This eliminates the blast of soft faults on process startup and
2213 * immediately after an mmap.
2216 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2217 vm_object_t object, vm_pindex_t pindex,
2218 vm_size_t size, int limit)
2225 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2230 * XXX you must be joking, entering PTE's into a user page table
2231 * without any accounting? This could result in the page table
2232 * being freed while it still contains mappings (free with PG_ZERO
2233 * assumption leading to a non-zero page being marked PG_ZERO).
2236 * This code maps large physical mmap regions into the
2237 * processor address space. Note that some shortcuts
2238 * are taken, but the code works.
2241 (object->type == OBJT_DEVICE) &&
2242 ((addr & (NBPDR - 1)) == 0) &&
2243 ((size & (NBPDR - 1)) == 0) ) {
2246 unsigned int ptepindex;
2250 if (pmap->pm_pdir[ptepindex = (addr >> PDRSHIFT)])
2254 p = vm_page_lookup(object, pindex);
2255 if (p && vm_page_sleep_busy(p, FALSE, "init4p"))
2259 p = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL);
2264 if (vm_pager_get_pages(object, m, 1, 0) != VM_PAGER_OK) {
2269 p = vm_page_lookup(object, pindex);
2273 ptepa = (vm_offset_t) VM_PAGE_TO_PHYS(p);
2274 if (ptepa & (NBPDR - 1)) {
2278 p->valid = VM_PAGE_BITS_ALL;
2280 pmap->pm_stats.resident_count += size >> PAGE_SHIFT;
2281 npdes = size >> PDRSHIFT;
2282 for (i = 0; i < npdes; i++) {
2283 pmap->pm_pdir[ptepindex] =
2284 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_PS);
2288 vm_page_flag_set(p, PG_MAPPED);
2295 psize = i386_btop(size);
2297 if ((object->type != OBJT_VNODE) ||
2298 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2299 (object->resident_page_count > MAX_INIT_PT))) {
2303 if (psize + pindex > object->size) {
2304 if (object->size < pindex)
2306 psize = object->size - pindex;
2311 * If we are processing a major portion of the object, then scan the
2314 * We cannot safely scan the object's memq unless we are in a
2315 * critical section since interrupts can remove pages from objects.
2319 if (psize > (object->resident_page_count >> 2)) {
2322 for (p = TAILQ_FIRST(&object->memq);
2323 objpgs > 0 && p != NULL;
2324 p = TAILQ_NEXT(p, listq)
2327 if (tmpidx < pindex)
2330 if (tmpidx >= psize)
2334 * don't allow an madvise to blow away our really
2335 * free pages allocating pv entries.
2337 if ((limit & MAP_PREFAULT_MADVISE) &&
2338 vmstats.v_free_count < vmstats.v_free_reserved) {
2341 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2343 (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2344 if ((p->queue - p->pc) == PQ_CACHE)
2345 vm_page_deactivate(p);
2347 mpte = pmap_enter_quick(pmap,
2348 addr + i386_ptob(tmpidx), p, mpte);
2349 vm_page_flag_set(p, PG_MAPPED);
2356 * else lookup the pages one-by-one.
2358 for (tmpidx = 0; tmpidx < psize; tmpidx += 1) {
2360 * don't allow an madvise to blow away our really
2361 * free pages allocating pv entries.
2363 if ((limit & MAP_PREFAULT_MADVISE) &&
2364 vmstats.v_free_count < vmstats.v_free_reserved) {
2367 p = vm_page_lookup(object, tmpidx + pindex);
2369 ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2371 (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2372 if ((p->queue - p->pc) == PQ_CACHE)
2373 vm_page_deactivate(p);
2375 mpte = pmap_enter_quick(pmap,
2376 addr + i386_ptob(tmpidx), p, mpte);
2377 vm_page_flag_set(p, PG_MAPPED);
2386 * pmap_prefault provides a quick way of clustering pagefaults into a
2387 * processes address space. It is a "cousin" of pmap_object_init_pt,
2388 * except it runs at page fault time instead of mmap time.
2392 #define PAGEORDER_SIZE (PFBAK+PFFOR)
2394 static int pmap_prefault_pageorder[] = {
2395 -PAGE_SIZE, PAGE_SIZE,
2396 -2 * PAGE_SIZE, 2 * PAGE_SIZE,
2397 -3 * PAGE_SIZE, 3 * PAGE_SIZE,
2398 -4 * PAGE_SIZE, 4 * PAGE_SIZE
2402 pmap_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry)
2411 if (!curproc || (pmap != vmspace_pmap(curproc->p_vmspace)))
2414 object = entry->object.vm_object;
2416 starta = addra - PFBAK * PAGE_SIZE;
2417 if (starta < entry->start)
2418 starta = entry->start;
2419 else if (starta > addra)
2423 * critical section protection is required to maintain the
2424 * page/object association, interrupts can free pages and remove
2425 * them from their objects.
2429 for (i = 0; i < PAGEORDER_SIZE; i++) {
2430 vm_object_t lobject;
2433 addr = addra + pmap_prefault_pageorder[i];
2434 if (addr > addra + (PFFOR * PAGE_SIZE))
2437 if (addr < starta || addr >= entry->end)
2440 if ((*pmap_pde(pmap, addr)) == NULL)
2443 pte = (unsigned *) vtopte(addr);
2447 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2450 for (m = vm_page_lookup(lobject, pindex);
2451 (!m && (lobject->type == OBJT_DEFAULT) &&
2452 (lobject->backing_object));
2453 lobject = lobject->backing_object
2455 if (lobject->backing_object_offset & PAGE_MASK)
2457 pindex += (lobject->backing_object_offset >> PAGE_SHIFT);
2458 m = vm_page_lookup(lobject->backing_object, pindex);
2462 * give-up when a page is not in memory
2467 if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2469 (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2471 if ((m->queue - m->pc) == PQ_CACHE) {
2472 vm_page_deactivate(m);
2475 mpte = pmap_enter_quick(pmap, addr, m, mpte);
2476 vm_page_flag_set(m, PG_MAPPED);
2484 * Routine: pmap_change_wiring
2485 * Function: Change the wiring attribute for a map/virtual-address
2487 * In/out conditions:
2488 * The mapping must already exist in the pmap.
2491 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2498 pte = pmap_pte(pmap, va);
2500 if (wired && !pmap_pte_w(pte))
2501 pmap->pm_stats.wired_count++;
2502 else if (!wired && pmap_pte_w(pte))
2503 pmap->pm_stats.wired_count--;
2506 * Wiring is not a hardware characteristic so there is no need to
2507 * invalidate TLB. However, in an SMP environment we must use
2508 * a locked bus cycle to update the pte (if we are not using
2509 * the pmap_inval_*() API that is)... it's ok to do this for simple
2514 atomic_set_int(pte, PG_W);
2516 atomic_clear_int(pte, PG_W);
2519 atomic_set_int_nonlocked(pte, PG_W);
2521 atomic_clear_int_nonlocked(pte, PG_W);
2528 * Copy the range specified by src_addr/len
2529 * from the source map to the range dst_addr/len
2530 * in the destination map.
2532 * This routine is only advisory and need not do anything.
2535 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2536 vm_size_t len, vm_offset_t src_addr)
2538 pmap_inval_info info;
2540 vm_offset_t end_addr = src_addr + len;
2542 unsigned src_frame, dst_frame;
2545 if (dst_addr != src_addr)
2548 src_frame = ((unsigned) src_pmap->pm_pdir[PTDPTDI]) & PG_FRAME;
2549 if (src_frame != (((unsigned) PTDpde) & PG_FRAME)) {
2553 dst_frame = ((unsigned) dst_pmap->pm_pdir[PTDPTDI]) & PG_FRAME;
2554 if (dst_frame != (((unsigned) APTDpde) & PG_FRAME)) {
2555 APTDpde = (pd_entry_t) (dst_frame | PG_RW | PG_V);
2556 /* The page directory is not shared between CPUs */
2559 pmap_inval_init(&info);
2560 pmap_inval_add(&info, dst_pmap, -1);
2561 pmap_inval_add(&info, src_pmap, -1);
2564 * critical section protection is required to maintain the page/object
2565 * association, interrupts can free pages and remove them from
2569 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
2570 unsigned *src_pte, *dst_pte;
2571 vm_page_t dstmpte, srcmpte;
2572 vm_offset_t srcptepaddr;
2575 if (addr >= UPT_MIN_ADDRESS)
2576 panic("pmap_copy: invalid to pmap_copy page tables\n");
2579 * Don't let optional prefaulting of pages make us go
2580 * way below the low water mark of free pages or way
2581 * above high water mark of used pv entries.
2583 if (vmstats.v_free_count < vmstats.v_free_reserved ||
2584 pv_entry_count > pv_entry_high_water)
2587 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
2588 ptepindex = addr >> PDRSHIFT;
2590 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex];
2591 if (srcptepaddr == 0)
2594 if (srcptepaddr & PG_PS) {
2595 if (dst_pmap->pm_pdir[ptepindex] == 0) {
2596 dst_pmap->pm_pdir[ptepindex] = (pd_entry_t) srcptepaddr;
2597 dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
2602 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
2603 if ((srcmpte == NULL) ||
2604 (srcmpte->hold_count == 0) || (srcmpte->flags & PG_BUSY))
2607 if (pdnxt > end_addr)
2610 src_pte = (unsigned *) vtopte(addr);
2611 dst_pte = (unsigned *) avtopte(addr);
2612 while (addr < pdnxt) {
2616 * we only virtual copy managed pages
2618 if ((ptetemp & PG_MANAGED) != 0) {
2620 * We have to check after allocpte for the
2621 * pte still being around... allocpte can
2624 dstmpte = pmap_allocpte(dst_pmap, addr);
2625 if ((*dst_pte == 0) && (ptetemp = *src_pte)) {
2627 * Clear the modified and
2628 * accessed (referenced) bits
2631 m = PHYS_TO_VM_PAGE(ptetemp);
2632 *dst_pte = ptetemp & ~(PG_M | PG_A);
2633 dst_pmap->pm_stats.resident_count++;
2634 pmap_insert_entry(dst_pmap, addr,
2637 pmap_unwire_pte_hold(dst_pmap, dstmpte, &info);
2639 if (dstmpte->hold_count >= srcmpte->hold_count)
2648 pmap_inval_flush(&info);
2652 * Routine: pmap_kernel
2654 * Returns the physical map handle for the kernel.
2659 return (kernel_pmap);
2665 * Zero the specified PA by mapping the page into KVM and clearing its
2668 * This function may be called from an interrupt and no locking is
2672 pmap_zero_page(vm_paddr_t phys)
2674 struct mdglobaldata *gd = mdcpu;
2677 if (*(int *)gd->gd_CMAP3)
2678 panic("pmap_zero_page: CMAP3 busy");
2679 *(int *)gd->gd_CMAP3 =
2680 PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2681 cpu_invlpg(gd->gd_CADDR3);
2683 #if defined(I686_CPU)
2684 if (cpu_class == CPUCLASS_686)
2685 i686_pagezero(gd->gd_CADDR3);
2688 bzero(gd->gd_CADDR3, PAGE_SIZE);
2689 *(int *) gd->gd_CMAP3 = 0;
2694 * pmap_page_assertzero:
2696 * Assert that a page is empty, panic if it isn't.
2699 pmap_page_assertzero(vm_paddr_t phys)
2701 struct mdglobaldata *gd = mdcpu;
2705 if (*(int *)gd->gd_CMAP3)
2706 panic("pmap_zero_page: CMAP3 busy");
2707 *(int *)gd->gd_CMAP3 =
2708 PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2709 cpu_invlpg(gd->gd_CADDR3);
2710 for (i = 0; i < PAGE_SIZE; i += 4) {
2711 if (*(int *)((char *)gd->gd_CADDR3 + i) != 0) {
2712 panic("pmap_page_assertzero() @ %p not zero!\n",
2713 (void *)gd->gd_CADDR3);
2716 *(int *) gd->gd_CMAP3 = 0;
2723 * Zero part of a physical page by mapping it into memory and clearing
2724 * its contents with bzero.
2726 * off and size may not cover an area beyond a single hardware page.
2729 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
2731 struct mdglobaldata *gd = mdcpu;
2734 if (*(int *) gd->gd_CMAP3)
2735 panic("pmap_zero_page: CMAP3 busy");
2736 *(int *) gd->gd_CMAP3 = PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2737 cpu_invlpg(gd->gd_CADDR3);
2739 #if defined(I686_CPU)
2740 if (cpu_class == CPUCLASS_686 && off == 0 && size == PAGE_SIZE)
2741 i686_pagezero(gd->gd_CADDR3);
2744 bzero((char *)gd->gd_CADDR3 + off, size);
2745 *(int *) gd->gd_CMAP3 = 0;
2752 * Copy the physical page from the source PA to the target PA.
2753 * This function may be called from an interrupt. No locking
2757 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
2759 struct mdglobaldata *gd = mdcpu;
2762 if (*(int *) gd->gd_CMAP1)
2763 panic("pmap_copy_page: CMAP1 busy");
2764 if (*(int *) gd->gd_CMAP2)
2765 panic("pmap_copy_page: CMAP2 busy");
2767 *(int *) gd->gd_CMAP1 = PG_V | (src & PG_FRAME) | PG_A;
2768 *(int *) gd->gd_CMAP2 = PG_V | PG_RW | (dst & PG_FRAME) | PG_A | PG_M;
2770 cpu_invlpg(gd->gd_CADDR1);
2771 cpu_invlpg(gd->gd_CADDR2);
2773 bcopy(gd->gd_CADDR1, gd->gd_CADDR2, PAGE_SIZE);
2775 *(int *) gd->gd_CMAP1 = 0;
2776 *(int *) gd->gd_CMAP2 = 0;
2781 * pmap_copy_page_frag:
2783 * Copy the physical page from the source PA to the target PA.
2784 * This function may be called from an interrupt. No locking
2788 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
2790 struct mdglobaldata *gd = mdcpu;
2793 if (*(int *) gd->gd_CMAP1)
2794 panic("pmap_copy_page: CMAP1 busy");
2795 if (*(int *) gd->gd_CMAP2)
2796 panic("pmap_copy_page: CMAP2 busy");
2798 *(int *) gd->gd_CMAP1 = PG_V | (src & PG_FRAME) | PG_A;
2799 *(int *) gd->gd_CMAP2 = PG_V | PG_RW | (dst & PG_FRAME) | PG_A | PG_M;
2801 cpu_invlpg(gd->gd_CADDR1);
2802 cpu_invlpg(gd->gd_CADDR2);
2804 bcopy((char *)gd->gd_CADDR1 + (src & PAGE_MASK),
2805 (char *)gd->gd_CADDR2 + (dst & PAGE_MASK),
2808 *(int *) gd->gd_CMAP1 = 0;
2809 *(int *) gd->gd_CMAP2 = 0;
2814 * Returns true if the pmap's pv is one of the first
2815 * 16 pvs linked to from this page. This count may
2816 * be changed upwards or downwards in the future; it
2817 * is only necessary that true be returned for a small
2818 * subset of pmaps for proper page aging.
2821 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2826 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2831 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2832 if (pv->pv_pmap == pmap) {
2844 #define PMAP_REMOVE_PAGES_CURPROC_ONLY
2846 * Remove all pages from specified address space
2847 * this aids process exit speeds. Also, this code
2848 * is special cased for current process only, but
2849 * can have the more generic (and slightly slower)
2850 * mode enabled. This is much faster than pmap_remove
2851 * in the case of running down an entire address space.
2854 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2856 unsigned *pte, tpte;
2859 pmap_inval_info info;
2861 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2862 if (!curproc || (pmap != vmspace_pmap(curproc->p_vmspace))) {
2863 printf("warning: pmap_remove_pages called with non-current pmap\n");
2868 pmap_inval_init(&info);
2870 for(pv = TAILQ_FIRST(&pmap->pm_pvlist);
2874 if (pv->pv_va >= eva || pv->pv_va < sva) {
2875 npv = TAILQ_NEXT(pv, pv_plist);
2879 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2880 pte = (unsigned *)vtopte(pv->pv_va);
2882 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2884 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
2888 * We cannot remove wired pages from a process' mapping at this time
2891 npv = TAILQ_NEXT(pv, pv_plist);
2896 m = PHYS_TO_VM_PAGE(tpte);
2898 KASSERT(m < &vm_page_array[vm_page_array_size],
2899 ("pmap_remove_pages: bad tpte %x", tpte));
2901 pv->pv_pmap->pm_stats.resident_count--;
2904 * Update the vm_page_t clean and reference bits.
2911 npv = TAILQ_NEXT(pv, pv_plist);
2912 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2914 m->md.pv_list_count--;
2915 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2916 if (TAILQ_FIRST(&m->md.pv_list) == NULL) {
2917 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2920 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem, &info);
2923 pmap_inval_flush(&info);
2928 * pmap_testbit tests bits in pte's
2929 * note that the testbit/changebit routines are inline,
2930 * and a lot of things compile-time evaluate.
2933 pmap_testbit(vm_page_t m, int bit)
2938 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2941 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2946 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2948 * if the bit being tested is the modified bit, then
2949 * mark clean_map and ptes as never
2952 if (bit & (PG_A|PG_M)) {
2953 if (!pmap_track_modified(pv->pv_va))
2957 #if defined(PMAP_DIAGNOSTIC)
2959 printf("Null pmap (tb) at va: 0x%x\n", pv->pv_va);
2963 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2974 * this routine is used to modify bits in ptes
2976 static __inline void
2977 pmap_changebit(vm_page_t m, int bit, boolean_t setem)
2979 struct pmap_inval_info info;
2983 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2986 pmap_inval_init(&info);
2990 * Loop over all current mappings setting/clearing as appropos If
2991 * setting RO do we need to clear the VAC?
2993 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2995 * don't write protect pager mappings
2997 if (!setem && (bit == PG_RW)) {
2998 if (!pmap_track_modified(pv->pv_va))
3002 #if defined(PMAP_DIAGNOSTIC)
3004 printf("Null pmap (cb) at va: 0x%x\n", pv->pv_va);
3010 * Careful here. We can use a locked bus instruction to
3011 * clear PG_A or PG_M safely but we need to synchronize
3012 * with the target cpus when we mess with PG_RW.
3014 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3016 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
3020 atomic_set_int(pte, bit);
3022 atomic_set_int_nonlocked(pte, bit);
3025 vm_offset_t pbits = *(vm_offset_t *)pte;
3032 atomic_clear_int(pte, PG_M|PG_RW);
3034 atomic_clear_int_nonlocked(pte, PG_M|PG_RW);
3038 atomic_clear_int(pte, bit);
3040 atomic_clear_int_nonlocked(pte, bit);
3046 pmap_inval_flush(&info);
3051 * pmap_page_protect:
3053 * Lower the permission for all mappings to a given page.
3056 pmap_page_protect(vm_page_t m, vm_prot_t prot)
3058 if ((prot & VM_PROT_WRITE) == 0) {
3059 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
3060 pmap_changebit(m, PG_RW, FALSE);
3068 pmap_phys_address(int ppn)
3070 return (i386_ptob(ppn));
3074 * pmap_ts_referenced:
3076 * Return a count of reference bits for a page, clearing those bits.
3077 * It is not necessary for every reference bit to be cleared, but it
3078 * is necessary that 0 only be returned when there are truly no
3079 * reference bits set.
3081 * XXX: The exact number of bits to check and clear is a matter that
3082 * should be tested and standardized at some point in the future for
3083 * optimal aging of shared pages.
3086 pmap_ts_referenced(vm_page_t m)
3088 pv_entry_t pv, pvf, pvn;
3092 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3097 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3102 pvn = TAILQ_NEXT(pv, pv_list);
3104 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3106 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3108 if (!pmap_track_modified(pv->pv_va))
3111 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3113 if (pte && (*pte & PG_A)) {
3115 atomic_clear_int(pte, PG_A);
3117 atomic_clear_int_nonlocked(pte, PG_A);
3124 } while ((pv = pvn) != NULL && pv != pvf);
3134 * Return whether or not the specified physical page was modified
3135 * in any physical maps.
3138 pmap_is_modified(vm_page_t m)
3140 return pmap_testbit(m, PG_M);
3144 * Clear the modify bits on the specified physical page.
3147 pmap_clear_modify(vm_page_t m)
3149 pmap_changebit(m, PG_M, FALSE);
3153 * pmap_clear_reference:
3155 * Clear the reference bit on the specified physical page.
3158 pmap_clear_reference(vm_page_t m)
3160 pmap_changebit(m, PG_A, FALSE);
3164 * Miscellaneous support routines follow
3168 i386_protection_init(void)
3172 kp = protection_codes;
3173 for (prot = 0; prot < 8; prot++) {
3175 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE:
3177 * Read access is also 0. There isn't any execute bit,
3178 * so just make it readable.
3180 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE:
3181 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE:
3182 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE:
3185 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE:
3186 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE:
3187 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE:
3188 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
3196 * Map a set of physical memory pages into the kernel virtual
3197 * address space. Return a pointer to where it is mapped. This
3198 * routine is intended to be used for mapping device memory,
3201 * NOTE: we can't use pgeflag unless we invalidate the pages one at
3205 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
3207 vm_offset_t va, tmpva, offset;
3210 offset = pa & PAGE_MASK;
3211 size = roundup(offset + size, PAGE_SIZE);
3213 va = kmem_alloc_nofault(kernel_map, size);
3215 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3218 for (tmpva = va; size > 0;) {
3219 pte = (unsigned *)vtopte(tmpva);
3220 *pte = pa | PG_RW | PG_V; /* | pgeflag; */
3228 return ((void *)(va + offset));
3232 pmap_unmapdev(vm_offset_t va, vm_size_t size)
3234 vm_offset_t base, offset;
3236 base = va & PG_FRAME;
3237 offset = va & PAGE_MASK;
3238 size = roundup(offset + size, PAGE_SIZE);
3239 pmap_qremove(va, size >> PAGE_SHIFT);
3240 kmem_free(kernel_map, base, size);
3244 * perform the pmap work for mincore
3247 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3249 unsigned *ptep, pte;
3253 ptep = pmap_pte(pmap, addr);
3258 if ((pte = *ptep) != 0) {
3261 val = MINCORE_INCORE;
3262 if ((pte & PG_MANAGED) == 0)
3265 pa = pte & PG_FRAME;
3267 m = PHYS_TO_VM_PAGE(pa);
3273 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3275 * Modified by someone
3277 else if (m->dirty || pmap_is_modified(m))
3278 val |= MINCORE_MODIFIED_OTHER;
3283 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3286 * Referenced by someone
3288 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3289 val |= MINCORE_REFERENCED_OTHER;
3290 vm_page_flag_set(m, PG_REFERENCED);
3297 pmap_activate(struct proc *p)
3301 pmap = vmspace_pmap(p->p_vmspace);
3303 atomic_set_int(&pmap->pm_active, 1 << mycpu->gd_cpuid);
3305 pmap->pm_active |= 1;
3307 #if defined(SWTCH_OPTIM_STATS)
3310 p->p_thread->td_pcb->pcb_cr3 = vtophys(pmap->pm_pdir);
3311 load_cr3(p->p_thread->td_pcb->pcb_cr3);
3315 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3318 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3322 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3327 #if defined(PMAP_DEBUG)
3329 pmap_pid_dump(int pid)
3335 FOREACH_PROC_IN_SYSTEM(p) {
3336 if (p->p_pid != pid)
3342 pmap = vmspace_pmap(p->p_vmspace);
3343 for(i=0;i<1024;i++) {
3346 unsigned base = i << PDRSHIFT;
3348 pde = &pmap->pm_pdir[i];
3349 if (pde && pmap_pde_v(pde)) {
3350 for(j=0;j<1024;j++) {
3351 unsigned va = base + (j << PAGE_SHIFT);
3352 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) {
3359 pte = pmap_pte_quick( pmap, va);
3360 if (pte && pmap_pte_v(pte)) {
3364 m = PHYS_TO_VM_PAGE(pa);
3365 printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x",
3366 va, pa, m->hold_count, m->wire_count, m->flags);
3387 static void pads (pmap_t pm);
3388 void pmap_pvdump (vm_paddr_t pa);
3390 /* print address space of pmap*/
3397 if (pm == kernel_pmap)
3399 for (i = 0; i < 1024; i++)
3401 for (j = 0; j < 1024; j++) {
3402 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
3403 if (pm == kernel_pmap && va < KERNBASE)
3405 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS)
3407 ptep = pmap_pte_quick(pm, va);
3408 if (pmap_pte_v(ptep))
3409 printf("%x:%x ", va, *(int *) ptep);
3415 pmap_pvdump(vm_paddr_t pa)
3420 printf("pa %08llx", (long long)pa);
3421 m = PHYS_TO_VM_PAGE(pa);
3422 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3424 printf(" -> pmap %p, va %x, flags %x",
3425 (void *)pv->pv_pmap, pv->pv_va, pv->pv_flags);
3427 printf(" -> pmap %p, va %x", (void *)pv->pv_pmap, pv->pv_va);