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.44 2004/07/29 08:54:58 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 splvm() is held or from a critical
275 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
277 struct mdglobaldata *gd = mdcpu;
280 if ((pde = (unsigned) pmap->pm_pdir[va >> PDRSHIFT]) != 0) {
281 unsigned frame = (unsigned) pmap->pm_pdir[PTDPTDI] & PG_FRAME;
282 unsigned index = i386_btop(va);
283 /* are we current address space or kernel? */
284 if ((pmap == kernel_pmap) ||
285 (frame == (((unsigned) PTDpde) & PG_FRAME))) {
286 return (unsigned *) PTmap + index;
288 newpf = pde & PG_FRAME;
289 if ( ((* (unsigned *) gd->gd_PMAP1) & PG_FRAME) != newpf) {
290 * (unsigned *) gd->gd_PMAP1 = newpf | PG_RW | PG_V;
291 cpu_invlpg(gd->gd_PADDR1);
293 return gd->gd_PADDR1 + ((unsigned) index & (NPTEPG - 1));
300 * Bootstrap the system enough to run with virtual memory.
302 * On the i386 this is called after mapping has already been enabled
303 * and just syncs the pmap module with what has already been done.
304 * [We can't call it easily with mapping off since the kernel is not
305 * mapped with PA == VA, hence we would have to relocate every address
306 * from the linked base (virtual) address "KERNBASE" to the actual
307 * (physical) address starting relative to 0]
310 pmap_bootstrap(firstaddr, loadaddr)
311 vm_paddr_t firstaddr;
316 struct mdglobaldata *gd;
319 avail_start = firstaddr;
322 * XXX The calculation of virtual_avail is wrong. It's NKPT*PAGE_SIZE too
323 * large. It should instead be correctly calculated in locore.s and
324 * not based on 'first' (which is a physical address, not a virtual
325 * address, for the start of unused physical memory). The kernel
326 * page tables are NOT double mapped and thus should not be included
327 * in this calculation.
329 virtual_avail = (vm_offset_t) KERNBASE + firstaddr;
330 virtual_avail = pmap_kmem_choose(virtual_avail);
332 virtual_end = VM_MAX_KERNEL_ADDRESS;
335 * Initialize protection array.
337 i386_protection_init();
340 * The kernel's pmap is statically allocated so we don't have to use
341 * pmap_create, which is unlikely to work correctly at this part of
342 * the boot sequence (XXX and which no longer exists).
344 kernel_pmap = &kernel_pmap_store;
346 kernel_pmap->pm_pdir = (pd_entry_t *)(KERNBASE + (u_int)IdlePTD);
347 kernel_pmap->pm_count = 1;
348 kernel_pmap->pm_active = (cpumask_t)-1; /* don't allow deactivation */
349 TAILQ_INIT(&kernel_pmap->pm_pvlist);
353 * Reserve some special page table entries/VA space for temporary
356 #define SYSMAP(c, p, v, n) \
357 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
360 pte = (pt_entry_t *) pmap_pte(kernel_pmap, va);
363 * CMAP1/CMAP2 are used for zeroing and copying pages.
365 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
370 SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS);
373 * ptvmmap is used for reading arbitrary physical pages via
376 SYSMAP(caddr_t, ptmmap, ptvmmap, 1)
379 * msgbufp is used to map the system message buffer.
380 * XXX msgbufmap is not used.
382 SYSMAP(struct msgbuf *, msgbufmap, msgbufp,
383 atop(round_page(MSGBUF_SIZE)))
388 for (i = 0; i < NKPT; i++)
392 * PG_G is terribly broken on SMP because we IPI invltlb's in some
393 * cases rather then invl1pg. Actually, I don't even know why it
394 * works under UP because self-referential page table mappings
399 if (cpu_feature & CPUID_PGE)
404 * Initialize the 4MB page size flag
408 * The 4MB page version of the initial
409 * kernel page mapping.
413 #if !defined(DISABLE_PSE)
414 if (cpu_feature & CPUID_PSE) {
417 * Note that we have enabled PSE mode
420 ptditmp = *((unsigned *)PTmap + i386_btop(KERNBASE));
421 ptditmp &= ~(NBPDR - 1);
422 ptditmp |= PG_V | PG_RW | PG_PS | PG_U | pgeflag;
427 * Enable the PSE mode. If we are SMP we can't do this
428 * now because the APs will not be able to use it when
431 load_cr4(rcr4() | CR4_PSE);
434 * We can do the mapping here for the single processor
435 * case. We simply ignore the old page table page from
439 * For SMP, we still need 4K pages to bootstrap APs,
440 * PSE will be enabled as soon as all APs are up.
442 PTD[KPTDI] = (pd_entry_t)ptditmp;
443 kernel_pmap->pm_pdir[KPTDI] = (pd_entry_t)ptditmp;
449 if (cpu_apic_address == 0)
450 panic("pmap_bootstrap: no local apic!");
452 /* local apic is mapped on last page */
453 SMPpt[NPTEPG - 1] = (pt_entry_t)(PG_V | PG_RW | PG_N | pgeflag |
454 (cpu_apic_address & PG_FRAME));
457 /* BSP does this itself, AP's get it pre-set */
458 gd = &CPU_prvspace[0].mdglobaldata;
459 gd->gd_CMAP1 = &SMPpt[1];
460 gd->gd_CMAP2 = &SMPpt[2];
461 gd->gd_CMAP3 = &SMPpt[3];
462 gd->gd_PMAP1 = &SMPpt[4];
463 gd->gd_CADDR1 = CPU_prvspace[0].CPAGE1;
464 gd->gd_CADDR2 = CPU_prvspace[0].CPAGE2;
465 gd->gd_CADDR3 = CPU_prvspace[0].CPAGE3;
466 gd->gd_PADDR1 = (unsigned *)CPU_prvspace[0].PPAGE1;
473 * Set 4mb pdir for mp startup
478 if (pseflag && (cpu_feature & CPUID_PSE)) {
479 load_cr4(rcr4() | CR4_PSE);
480 if (pdir4mb && mycpu->gd_cpuid == 0) { /* only on BSP */
481 kernel_pmap->pm_pdir[KPTDI] =
482 PTD[KPTDI] = (pd_entry_t)pdir4mb;
490 * Initialize the pmap module.
491 * Called by vm_init, to initialize any structures that the pmap
492 * system needs to map virtual memory.
493 * pmap_init has been enhanced to support in a fairly consistant
494 * way, discontiguous physical memory.
503 * object for kernel page table pages
505 kptobj = vm_object_allocate(OBJT_DEFAULT, NKPDE);
508 * Allocate memory for random pmap data structures. Includes the
512 for(i = 0; i < vm_page_array_size; i++) {
515 m = &vm_page_array[i];
516 TAILQ_INIT(&m->md.pv_list);
517 m->md.pv_list_count = 0;
521 * init the pv free list
523 initial_pvs = vm_page_array_size;
524 if (initial_pvs < MINPV)
526 pvzone = &pvzone_store;
527 pvinit = (struct pv_entry *) kmem_alloc(kernel_map,
528 initial_pvs * sizeof (struct pv_entry));
529 zbootinit(pvzone, "PV ENTRY", sizeof (struct pv_entry), pvinit,
533 * Now it is safe to enable pv_table recording.
535 pmap_initialized = TRUE;
539 * Initialize the address space (zone) for the pv_entries. Set a
540 * high water mark so that the system can recover from excessive
541 * numbers of pv entries.
546 int shpgperproc = PMAP_SHPGPERPROC;
548 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
549 pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
550 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
551 pv_entry_high_water = 9 * (pv_entry_max / 10);
552 zinitna(pvzone, &pvzone_obj, NULL, 0, pv_entry_max, ZONE_INTERRUPT, 1);
556 /***************************************************
557 * Low level helper routines.....
558 ***************************************************/
560 #if defined(PMAP_DIAGNOSTIC)
563 * This code checks for non-writeable/modified pages.
564 * This should be an invalid condition.
567 pmap_nw_modified(pt_entry_t ptea)
573 if ((pte & (PG_M|PG_RW)) == PG_M)
582 * this routine defines the region(s) of memory that should
583 * not be tested for the modified bit.
585 static PMAP_INLINE int
586 pmap_track_modified(vm_offset_t va)
588 if ((va < clean_sva) || (va >= clean_eva))
595 get_ptbase(pmap_t pmap)
597 unsigned frame = (unsigned) pmap->pm_pdir[PTDPTDI] & PG_FRAME;
598 struct globaldata *gd = mycpu;
600 /* are we current address space or kernel? */
601 if (pmap == kernel_pmap || frame == (((unsigned) PTDpde) & PG_FRAME)) {
602 return (unsigned *) PTmap;
605 /* otherwise, we are alternate address space */
606 KKASSERT(gd->gd_intr_nesting_level == 0 && (gd->gd_curthread->td_flags & TDF_INTTHREAD) == 0);
608 if (frame != (((unsigned) APTDpde) & PG_FRAME)) {
609 APTDpde = (pd_entry_t)(frame | PG_RW | PG_V);
610 /* The page directory is not shared between CPUs */
613 return (unsigned *) APTmap;
619 * Extract the physical page address associated with the map/VA pair.
621 * This function may not be called from an interrupt if the pmap is
625 pmap_extract(pmap_t pmap, vm_offset_t va)
628 vm_offset_t pdirindex;
630 pdirindex = va >> PDRSHIFT;
631 if (pmap && (rtval = (unsigned) pmap->pm_pdir[pdirindex])) {
633 if ((rtval & PG_PS) != 0) {
634 rtval &= ~(NBPDR - 1);
635 rtval |= va & (NBPDR - 1);
638 pte = get_ptbase(pmap) + i386_btop(va);
639 rtval = ((*pte & PG_FRAME) | (va & PAGE_MASK));
646 * Extract user accessible page only, return NULL if the page is not
647 * present or if it's current state is not sufficient. Caller will
648 * generally call vm_fault() on failure and try again.
651 pmap_extract_vmpage(pmap_t pmap, vm_offset_t va, int prot)
654 vm_offset_t pdirindex;
656 pdirindex = va >> PDRSHIFT;
657 if (pmap && (rtval = (unsigned) pmap->pm_pdir[pdirindex])) {
661 if ((rtval & PG_PS) != 0) {
662 if ((rtval & (PG_V|PG_U)) != (PG_V|PG_U))
664 if ((prot & VM_PROT_WRITE) && (rtval & PG_RW) == 0)
666 rtval &= ~(NBPDR - 1);
667 rtval |= va & (NBPDR - 1);
668 m = PHYS_TO_VM_PAGE(rtval);
670 pte = get_ptbase(pmap) + i386_btop(va);
671 if ((*pte & (PG_V|PG_U)) != (PG_V|PG_U))
673 if ((prot & VM_PROT_WRITE) && (*pte & PG_RW) == 0)
675 rtval = ((*pte & PG_FRAME) | (va & PAGE_MASK));
676 m = PHYS_TO_VM_PAGE(rtval);
683 /***************************************************
684 * Low level mapping routines.....
685 ***************************************************/
688 * Routine: pmap_kenter
690 * Add a wired page to the KVA
691 * NOTE! note that in order for the mapping to take effect -- you
692 * should do an invltlb after doing the pmap_kenter().
695 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
699 pmap_inval_info info;
701 pmap_inval_init(&info);
702 pmap_inval_add(&info, kernel_pmap, va);
703 npte = pa | PG_RW | PG_V | pgeflag;
704 pte = (unsigned *)vtopte(va);
706 pmap_inval_flush(&info);
710 * Routine: pmap_kenter_quick
712 * Similar to pmap_kenter(), except we only invalidate the
713 * mapping on the current CPU.
716 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
721 npte = pa | PG_RW | PG_V | pgeflag;
722 pte = (unsigned *)vtopte(va);
724 cpu_invlpg((void *)va);
728 pmap_kenter_sync(vm_offset_t va)
730 pmap_inval_info info;
732 pmap_inval_init(&info);
733 pmap_inval_add(&info, kernel_pmap, va);
734 pmap_inval_flush(&info);
738 pmap_kenter_sync_quick(vm_offset_t va)
740 cpu_invlpg((void *)va);
744 * remove a page from the kernel pagetables
747 pmap_kremove(vm_offset_t va)
750 pmap_inval_info info;
752 pmap_inval_init(&info);
753 pmap_inval_add(&info, kernel_pmap, va);
754 pte = (unsigned *)vtopte(va);
756 pmap_inval_flush(&info);
760 pmap_kremove_quick(vm_offset_t va)
763 pte = (unsigned *)vtopte(va);
765 cpu_invlpg((void *)va);
769 * Used to map a range of physical addresses into kernel
770 * virtual address space.
772 * For now, VM is already on, we only need to map the
776 pmap_map(vm_offset_t virt, vm_paddr_t start, vm_paddr_t end, int prot)
778 while (start < end) {
779 pmap_kenter(virt, start);
788 * Add a list of wired pages to the kva
789 * this routine is only used for temporary
790 * kernel mappings that do not need to have
791 * page modification or references recorded.
792 * Note that old mappings are simply written
793 * over. The page *must* be wired.
796 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
800 end_va = va + count * PAGE_SIZE;
802 while (va < end_va) {
805 pte = (unsigned *)vtopte(va);
806 *pte = VM_PAGE_TO_PHYS(*m) | PG_RW | PG_V | pgeflag;
807 cpu_invlpg((void *)va);
812 smp_invltlb(); /* XXX */
817 pmap_qenter2(vm_offset_t va, vm_page_t *m, int count, cpumask_t *mask)
820 cpumask_t cmask = mycpu->gd_cpumask;
822 end_va = va + count * PAGE_SIZE;
824 while (va < end_va) {
829 * Install the new PTE. If the pte changed from the prior
830 * mapping we must reset the cpu mask and invalidate the page.
831 * If the pte is the same but we have not seen it on the
832 * current cpu, invlpg the existing mapping. Otherwise the
833 * entry is optimal and no invalidation is required.
835 pte = (unsigned *)vtopte(va);
836 pteval = VM_PAGE_TO_PHYS(*m) | PG_A | PG_RW | PG_V | pgeflag;
837 if (*pte != pteval) {
840 cpu_invlpg((void *)va);
841 } else if ((*mask & cmask) == 0) {
842 cpu_invlpg((void *)va);
851 * this routine jerks page mappings from the
852 * kernel -- it is meant only for temporary mappings.
855 pmap_qremove(vm_offset_t va, int count)
859 end_va = va + count*PAGE_SIZE;
861 while (va < end_va) {
864 pte = (unsigned *)vtopte(va);
866 cpu_invlpg((void *)va);
875 * This routine works like vm_page_lookup() but also blocks as long as the
876 * page is busy. This routine does not busy the page it returns.
878 * Unless the caller is managing objects whos pages are in a known state,
879 * the call should be made at splvm() so the page's object association
880 * remains valid on return.
883 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
888 m = vm_page_lookup(object, pindex);
889 if (m && vm_page_sleep_busy(m, FALSE, "pplookp"))
895 * Create a new thread and optionally associate it with a (new) process.
896 * NOTE! the new thread's cpu may not equal the current cpu.
899 pmap_init_thread(thread_t td)
901 /* enforce pcb placement */
902 KKASSERT(td->td_kstack_size == UPAGES * PAGE_SIZE);
903 td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
904 td->td_savefpu = &td->td_pcb->pcb_save;
905 td->td_sp = (char *)td->td_pcb - 16;
909 * Create the UPAGES for a new process.
910 * This routine directly affects the fork perf for a process.
913 pmap_init_proc(struct proc *p, struct thread *td)
915 p->p_addr = (void *)td->td_kstack;
918 td->td_switch = cpu_heavy_switch;
922 bzero(p->p_addr, sizeof(*p->p_addr));
926 * Dispose the UPAGES for a process that has exited.
927 * This routine directly impacts the exit perf of a process.
930 pmap_dispose_proc(struct proc *p)
934 KASSERT(p->p_lock == 0, ("attempt to dispose referenced proc! %p", p));
936 if ((td = p->p_thread) != NULL) {
945 * Allow the UPAGES for a process to be prejudicially paged out.
948 pmap_swapout_proc(struct proc *p)
956 upobj = p->p_upages_obj;
959 * Unwiring the pages allow them to be paged to their backing store
962 * splvm() protection not required since nobody will be messing with
965 for (i = 0; i < UPAGES; i++) {
966 if ((m = vm_page_lookup(upobj, i)) == NULL)
967 panic("pmap_swapout_proc: upage already missing???");
969 vm_page_unwire(m, 0);
970 pmap_kremove((vm_offset_t)p->p_addr + (PAGE_SIZE * i));
976 * Bring the UPAGES for a specified process back in.
979 pmap_swapin_proc(struct proc *p)
987 * splvm() protection not required since nobody will be messing with
990 upobj = p->p_upages_obj;
991 for (i = 0; i < UPAGES; i++) {
992 m = vm_page_grab(upobj, i, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
994 pmap_kenter((vm_offset_t)p->p_addr + (i * PAGE_SIZE),
997 if (m->valid != VM_PAGE_BITS_ALL) {
998 rv = vm_pager_get_pages(upobj, &m, 1, 0);
999 if (rv != VM_PAGER_OK)
1000 panic("pmap_swapin_proc: cannot get upages for proc: %d\n", p->p_pid);
1001 m = vm_page_lookup(upobj, i);
1002 m->valid = VM_PAGE_BITS_ALL;
1006 vm_page_flag_set(m, PG_MAPPED | PG_WRITEABLE);
1011 /***************************************************
1012 * Page table page management routines.....
1013 ***************************************************/
1016 * This routine unholds page table pages, and if the hold count
1017 * drops to zero, then it decrements the wire count.
1020 _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, pmap_inval_info_t info)
1022 pmap_inval_flush(info);
1023 while (vm_page_sleep_busy(m, FALSE, "pmuwpt"))
1026 if (m->hold_count == 0) {
1029 * unmap the page table page
1031 pmap_inval_add(info, pmap, -1);
1032 pmap->pm_pdir[m->pindex] = 0;
1033 --pmap->pm_stats.resident_count;
1034 if ((((unsigned)pmap->pm_pdir[PTDPTDI]) & PG_FRAME) ==
1035 (((unsigned) PTDpde) & PG_FRAME)) {
1037 * Do a invltlb to make the invalidated mapping
1038 * take effect immediately.
1040 pteva = UPT_MIN_ADDRESS + i386_ptob(m->pindex);
1043 if (pmap->pm_ptphint == m)
1044 pmap->pm_ptphint = NULL;
1047 * If the page is finally unwired, simply free it.
1050 if (m->wire_count == 0) {
1053 vm_page_free_zero(m);
1054 --vmstats.v_wire_count;
1061 static PMAP_INLINE int
1062 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, pmap_inval_info_t info)
1065 if (m->hold_count == 0)
1066 return _pmap_unwire_pte_hold(pmap, m, info);
1072 * After removing a page table entry, this routine is used to
1073 * conditionally free the page, and manage the hold/wire counts.
1076 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte,
1077 pmap_inval_info_t info)
1080 if (va >= UPT_MIN_ADDRESS)
1084 ptepindex = (va >> PDRSHIFT);
1085 if (pmap->pm_ptphint &&
1086 (pmap->pm_ptphint->pindex == ptepindex)) {
1087 mpte = pmap->pm_ptphint;
1089 pmap_inval_flush(info);
1090 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1091 pmap->pm_ptphint = mpte;
1095 return pmap_unwire_pte_hold(pmap, mpte, info);
1099 pmap_pinit0(struct pmap *pmap)
1102 (pd_entry_t *)kmem_alloc_pageable(kernel_map, PAGE_SIZE);
1103 pmap_kenter((vm_offset_t)pmap->pm_pdir, (vm_offset_t) IdlePTD);
1105 pmap->pm_active = 0;
1106 pmap->pm_ptphint = NULL;
1107 TAILQ_INIT(&pmap->pm_pvlist);
1108 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1112 * Initialize a preallocated and zeroed pmap structure,
1113 * such as one in a vmspace structure.
1116 pmap_pinit(struct pmap *pmap)
1121 * No need to allocate page table space yet but we do need a valid
1122 * page directory table.
1124 if (pmap->pm_pdir == NULL) {
1126 (pd_entry_t *)kmem_alloc_pageable(kernel_map, PAGE_SIZE);
1130 * allocate object for the ptes
1132 if (pmap->pm_pteobj == NULL)
1133 pmap->pm_pteobj = vm_object_allocate( OBJT_DEFAULT, PTDPTDI + 1);
1136 * allocate the page directory page
1138 ptdpg = vm_page_grab( pmap->pm_pteobj, PTDPTDI,
1139 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1141 ptdpg->wire_count = 1;
1142 ++vmstats.v_wire_count;
1145 vm_page_flag_clear(ptdpg, PG_MAPPED | PG_BUSY); /* not usually mapped*/
1146 ptdpg->valid = VM_PAGE_BITS_ALL;
1148 pmap_kenter((vm_offset_t)pmap->pm_pdir, VM_PAGE_TO_PHYS(ptdpg));
1149 if ((ptdpg->flags & PG_ZERO) == 0)
1150 bzero(pmap->pm_pdir, PAGE_SIZE);
1152 pmap->pm_pdir[MPPTDI] = PTD[MPPTDI];
1154 /* install self-referential address mapping entry */
1155 *(unsigned *) (pmap->pm_pdir + PTDPTDI) =
1156 VM_PAGE_TO_PHYS(ptdpg) | PG_V | PG_RW | PG_A | PG_M;
1159 pmap->pm_active = 0;
1160 pmap->pm_ptphint = NULL;
1161 TAILQ_INIT(&pmap->pm_pvlist);
1162 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1166 * Wire in kernel global address entries. To avoid a race condition
1167 * between pmap initialization and pmap_growkernel, this procedure
1168 * should be called after the vmspace is attached to the process
1169 * but before this pmap is activated.
1172 pmap_pinit2(struct pmap *pmap)
1174 /* XXX copies current process, does not fill in MPPTDI */
1175 bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * PTESIZE);
1179 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1181 unsigned *pde = (unsigned *) pmap->pm_pdir;
1183 * This code optimizes the case of freeing non-busy
1184 * page-table pages. Those pages are zero now, and
1185 * might as well be placed directly into the zero queue.
1187 if (vm_page_sleep_busy(p, FALSE, "pmaprl"))
1193 * Remove the page table page from the processes address space.
1196 pmap->pm_stats.resident_count--;
1198 if (p->hold_count) {
1199 panic("pmap_release: freeing held page table page");
1202 * Page directory pages need to have the kernel
1203 * stuff cleared, so they can go into the zero queue also.
1205 if (p->pindex == PTDPTDI) {
1206 bzero(pde + KPTDI, nkpt * PTESIZE);
1209 pmap_kremove((vm_offset_t)pmap->pm_pdir);
1212 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1213 pmap->pm_ptphint = NULL;
1216 vmstats.v_wire_count--;
1217 vm_page_free_zero(p);
1222 * this routine is called if the page table page is not
1226 _pmap_allocpte(pmap_t pmap, unsigned ptepindex)
1228 vm_offset_t pteva, ptepa;
1232 * Find or fabricate a new pagetable page
1234 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1235 VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1237 KASSERT(m->queue == PQ_NONE,
1238 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1240 if (m->wire_count == 0)
1241 vmstats.v_wire_count++;
1245 * Increment the hold count for the page table page
1246 * (denoting a new mapping.)
1251 * Map the pagetable page into the process address space, if
1252 * it isn't already there.
1255 pmap->pm_stats.resident_count++;
1257 ptepa = VM_PAGE_TO_PHYS(m);
1258 pmap->pm_pdir[ptepindex] =
1259 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_A | PG_M);
1262 * Set the page table hint
1264 pmap->pm_ptphint = m;
1267 * Try to use the new mapping, but if we cannot, then
1268 * do it with the routine that maps the page explicitly.
1270 if ((m->flags & PG_ZERO) == 0) {
1271 if ((((unsigned)pmap->pm_pdir[PTDPTDI]) & PG_FRAME) ==
1272 (((unsigned) PTDpde) & PG_FRAME)) {
1273 pteva = UPT_MIN_ADDRESS + i386_ptob(ptepindex);
1274 bzero((caddr_t) pteva, PAGE_SIZE);
1276 pmap_zero_page(ptepa);
1280 m->valid = VM_PAGE_BITS_ALL;
1281 vm_page_flag_clear(m, PG_ZERO);
1282 vm_page_flag_set(m, PG_MAPPED);
1289 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1296 * Calculate pagetable page index
1298 ptepindex = va >> PDRSHIFT;
1301 * Get the page directory entry
1303 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1306 * This supports switching from a 4MB page to a
1309 if (ptepa & PG_PS) {
1310 pmap->pm_pdir[ptepindex] = 0;
1317 * If the page table page is mapped, we just increment the
1318 * hold count, and activate it.
1322 * In order to get the page table page, try the
1325 if (pmap->pm_ptphint &&
1326 (pmap->pm_ptphint->pindex == ptepindex)) {
1327 m = pmap->pm_ptphint;
1329 m = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1330 pmap->pm_ptphint = m;
1336 * Here if the pte page isn't mapped, or if it has been deallocated.
1338 return _pmap_allocpte(pmap, ptepindex);
1342 /***************************************************
1343 * Pmap allocation/deallocation routines.
1344 ***************************************************/
1347 * Release any resources held by the given physical map.
1348 * Called when a pmap initialized by pmap_pinit is being released.
1349 * Should only be called if the map contains no valid mappings.
1352 pmap_release(struct pmap *pmap)
1354 vm_page_t p,n,ptdpg;
1355 vm_object_t object = pmap->pm_pteobj;
1359 #if defined(DIAGNOSTIC)
1360 if (object->ref_count != 1)
1361 panic("pmap_release: pteobj reference count != 1");
1367 curgeneration = object->generation;
1368 for (p = TAILQ_FIRST(&object->memq); p != NULL; p = n) {
1369 n = TAILQ_NEXT(p, listq);
1370 if (p->pindex == PTDPTDI) {
1375 if (!pmap_release_free_page(pmap, p) &&
1376 (object->generation != curgeneration)) {
1384 if (ptdpg && !pmap_release_free_page(pmap, ptdpg))
1389 kvm_size(SYSCTL_HANDLER_ARGS)
1391 unsigned long ksize = VM_MAX_KERNEL_ADDRESS - KERNBASE;
1393 return sysctl_handle_long(oidp, &ksize, 0, req);
1395 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD,
1396 0, 0, kvm_size, "IU", "Size of KVM");
1399 kvm_free(SYSCTL_HANDLER_ARGS)
1401 unsigned long kfree = VM_MAX_KERNEL_ADDRESS - kernel_vm_end;
1403 return sysctl_handle_long(oidp, &kfree, 0, req);
1405 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD,
1406 0, 0, kvm_free, "IU", "Amount of KVM free");
1409 * grow the number of kernel page table entries, if needed
1412 pmap_growkernel(vm_offset_t addr)
1417 vm_offset_t ptppaddr;
1422 if (kernel_vm_end == 0) {
1423 kernel_vm_end = KERNBASE;
1425 while (pdir_pde(PTD, kernel_vm_end)) {
1426 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1430 addr = (addr + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1431 while (kernel_vm_end < addr) {
1432 if (pdir_pde(PTD, kernel_vm_end)) {
1433 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1438 * This index is bogus, but out of the way
1440 nkpg = vm_page_alloc(kptobj, nkpt,
1441 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM | VM_ALLOC_INTERRUPT);
1443 panic("pmap_growkernel: no memory to grow kernel");
1448 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1449 pmap_zero_page(ptppaddr);
1450 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
1451 pdir_pde(PTD, kernel_vm_end) = newpdir;
1453 FOREACH_PROC_IN_SYSTEM(p) {
1455 pmap = vmspace_pmap(p->p_vmspace);
1456 *pmap_pde(pmap, kernel_vm_end) = newpdir;
1459 *pmap_pde(kernel_pmap, kernel_vm_end) = newpdir;
1460 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1466 * Retire the given physical map from service.
1467 * Should only be called if the map contains
1468 * no valid mappings.
1471 pmap_destroy(pmap_t pmap)
1478 count = --pmap->pm_count;
1481 panic("destroying a pmap is not yet implemented");
1486 * Add a reference to the specified pmap.
1489 pmap_reference(pmap_t pmap)
1496 /***************************************************
1497 * page management routines.
1498 ***************************************************/
1501 * free the pv_entry back to the free list. This function may be
1502 * called from an interrupt.
1504 static PMAP_INLINE void
1505 free_pv_entry(pv_entry_t pv)
1512 * get a new pv_entry, allocating a block from the system
1513 * when needed. This function may be called from an interrupt.
1519 if (pv_entry_high_water &&
1520 (pv_entry_count > pv_entry_high_water) &&
1521 (pmap_pagedaemon_waken == 0)) {
1522 pmap_pagedaemon_waken = 1;
1523 wakeup (&vm_pages_needed);
1525 return zalloc(pvzone);
1529 * This routine is very drastic, but can save the system
1537 static int warningdone=0;
1539 if (pmap_pagedaemon_waken == 0)
1542 if (warningdone < 5) {
1543 printf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1547 for(i = 0; i < vm_page_array_size; i++) {
1548 m = &vm_page_array[i];
1549 if (m->wire_count || m->hold_count || m->busy ||
1550 (m->flags & PG_BUSY))
1554 pmap_pagedaemon_waken = 0;
1559 * If it is the first entry on the list, it is actually
1560 * in the header and we must copy the following entry up
1561 * to the header. Otherwise we must search the list for
1562 * the entry. In either case we free the now unused entry.
1565 pmap_remove_entry(struct pmap *pmap, vm_page_t m,
1566 vm_offset_t va, pmap_inval_info_t info)
1573 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1574 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1575 if (pmap == pv->pv_pmap && va == pv->pv_va)
1579 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1580 if (va == pv->pv_va)
1587 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem, info);
1588 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1589 m->md.pv_list_count--;
1590 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
1591 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1592 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1600 * Create a pv entry for page at pa for
1604 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
1610 pv = get_pv_entry();
1615 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1616 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1617 m->md.pv_list_count++;
1623 * pmap_remove_pte: do the things to unmap a page in a process
1626 pmap_remove_pte(struct pmap *pmap, unsigned *ptq, vm_offset_t va,
1627 pmap_inval_info_t info)
1632 pmap_inval_add(info, pmap, va);
1633 oldpte = loadandclear(ptq);
1635 pmap->pm_stats.wired_count -= 1;
1637 * Machines that don't support invlpg, also don't support
1638 * PG_G. XXX PG_G is disabled for SMP so don't worry about
1642 cpu_invlpg((void *)va);
1643 pmap->pm_stats.resident_count -= 1;
1644 if (oldpte & PG_MANAGED) {
1645 m = PHYS_TO_VM_PAGE(oldpte);
1646 if (oldpte & PG_M) {
1647 #if defined(PMAP_DIAGNOSTIC)
1648 if (pmap_nw_modified((pt_entry_t) oldpte)) {
1650 "pmap_remove: modified page not writable: va: 0x%x, pte: 0x%x\n",
1654 if (pmap_track_modified(va))
1658 vm_page_flag_set(m, PG_REFERENCED);
1659 return pmap_remove_entry(pmap, m, va, info);
1661 return pmap_unuse_pt(pmap, va, NULL, info);
1670 * Remove a single page from a process address space.
1672 * This function may not be called from an interrupt if the pmap is
1676 pmap_remove_page(struct pmap *pmap, vm_offset_t va, pmap_inval_info_t info)
1681 * if there is no pte for this address, just skip it!!! Otherwise
1682 * get a local va for mappings for this pmap and remove the entry.
1684 if (*pmap_pde(pmap, va) != 0) {
1685 ptq = get_ptbase(pmap) + i386_btop(va);
1687 pmap_remove_pte(pmap, ptq, va, info);
1695 * Remove the given range of addresses from the specified map.
1697 * It is assumed that the start and end are properly
1698 * rounded to the page size.
1700 * This function may not be called from an interrupt if the pmap is
1704 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
1708 vm_offset_t ptpaddr;
1709 vm_offset_t sindex, eindex;
1710 struct pmap_inval_info info;
1715 if (pmap->pm_stats.resident_count == 0)
1718 pmap_inval_init(&info);
1721 * special handling of removing one page. a very
1722 * common operation and easy to short circuit some
1725 if (((sva + PAGE_SIZE) == eva) &&
1726 (((unsigned) pmap->pm_pdir[(sva >> PDRSHIFT)] & PG_PS) == 0)) {
1727 pmap_remove_page(pmap, sva, &info);
1728 pmap_inval_flush(&info);
1733 * Get a local virtual address for the mappings that are being
1736 ptbase = get_ptbase(pmap);
1738 sindex = i386_btop(sva);
1739 eindex = i386_btop(eva);
1741 for (; sindex < eindex; sindex = pdnxt) {
1745 * Calculate index for next page table.
1747 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1748 if (pmap->pm_stats.resident_count == 0)
1751 pdirindex = sindex / NPDEPG;
1752 if (((ptpaddr = (unsigned) pmap->pm_pdir[pdirindex]) & PG_PS) != 0) {
1753 pmap_inval_add(&info, pmap, -1);
1754 pmap->pm_pdir[pdirindex] = 0;
1755 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1760 * Weed out invalid mappings. Note: we assume that the page
1761 * directory table is always allocated, and in kernel virtual.
1767 * Limit our scan to either the end of the va represented
1768 * by the current page table page, or to the end of the
1769 * range being removed.
1771 if (pdnxt > eindex) {
1775 for (; sindex != pdnxt; sindex++) {
1777 if (ptbase[sindex] == 0)
1779 va = i386_ptob(sindex);
1780 if (pmap_remove_pte(pmap, ptbase + sindex, va, &info))
1784 pmap_inval_flush(&info);
1790 * Removes this physical page from all physical maps in which it resides.
1791 * Reflects back modify bits to the pager.
1793 * This routine may not be called from an interrupt.
1797 pmap_remove_all(vm_page_t m)
1799 struct pmap_inval_info info;
1800 unsigned *pte, tpte;
1804 #if defined(PMAP_DIAGNOSTIC)
1806 * XXX this makes pmap_page_protect(NONE) illegal for non-managed
1809 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
1810 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%08llx", (long long)VM_PAGE_TO_PHYS(m));
1814 pmap_inval_init(&info);
1816 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
1817 pv->pv_pmap->pm_stats.resident_count--;
1819 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
1820 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
1822 tpte = loadandclear(pte);
1824 pv->pv_pmap->pm_stats.wired_count--;
1827 vm_page_flag_set(m, PG_REFERENCED);
1830 * Update the vm_page_t clean and reference bits.
1833 #if defined(PMAP_DIAGNOSTIC)
1834 if (pmap_nw_modified((pt_entry_t) tpte)) {
1836 "pmap_remove_all: modified page not writable: va: 0x%x, pte: 0x%x\n",
1840 if (pmap_track_modified(pv->pv_va))
1843 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
1844 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1845 m->md.pv_list_count--;
1846 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem, &info);
1850 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1852 pmap_inval_flush(&info);
1858 * Set the physical protection on the specified range of this map
1861 * This function may not be called from an interrupt if the map is
1862 * not the kernel_pmap.
1865 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
1868 vm_offset_t pdnxt, ptpaddr;
1869 vm_pindex_t sindex, eindex;
1870 pmap_inval_info info;
1875 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1876 pmap_remove(pmap, sva, eva);
1880 if (prot & VM_PROT_WRITE)
1883 pmap_inval_init(&info);
1885 ptbase = get_ptbase(pmap);
1887 sindex = i386_btop(sva);
1888 eindex = i386_btop(eva);
1890 for (; sindex < eindex; sindex = pdnxt) {
1894 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1896 pdirindex = sindex / NPDEPG;
1897 if (((ptpaddr = (unsigned) pmap->pm_pdir[pdirindex]) & PG_PS) != 0) {
1898 pmap_inval_add(&info, pmap, -1);
1899 (unsigned) pmap->pm_pdir[pdirindex] &= ~(PG_M|PG_RW);
1900 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1905 * Weed out invalid mappings. Note: we assume that the page
1906 * directory table is always allocated, and in kernel virtual.
1911 if (pdnxt > eindex) {
1915 for (; sindex != pdnxt; sindex++) {
1920 /* XXX this isn't optimal */
1921 pmap_inval_add(&info, pmap, i386_ptob(sindex));
1922 pbits = ptbase[sindex];
1924 if (pbits & PG_MANAGED) {
1927 m = PHYS_TO_VM_PAGE(pbits);
1928 vm_page_flag_set(m, PG_REFERENCED);
1932 if (pmap_track_modified(i386_ptob(sindex))) {
1934 m = PHYS_TO_VM_PAGE(pbits);
1943 if (pbits != ptbase[sindex]) {
1944 ptbase[sindex] = pbits;
1948 pmap_inval_flush(&info);
1952 * Insert the given physical page (p) at
1953 * the specified virtual address (v) in the
1954 * target physical map with the protection requested.
1956 * If specified, the page will be wired down, meaning
1957 * that the related pte can not be reclaimed.
1959 * NB: This is the only routine which MAY NOT lazy-evaluate
1960 * or lose information. That is, this routine must actually
1961 * insert this page into the given map NOW.
1964 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1970 vm_offset_t origpte, newpte;
1972 pmap_inval_info info;
1978 #ifdef PMAP_DIAGNOSTIC
1979 if (va > VM_MAX_KERNEL_ADDRESS)
1980 panic("pmap_enter: toobig");
1981 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
1982 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%x)", va);
1987 * In the case that a page table page is not
1988 * resident, we are creating it here.
1990 if (va < UPT_MIN_ADDRESS) {
1991 mpte = pmap_allocpte(pmap, va);
1994 pmap_inval_init(&info);
1995 pte = pmap_pte(pmap, va);
1998 * Page Directory table entry not valid, we need a new PT page
2001 panic("pmap_enter: invalid page directory pdir=%x, va=0x%x\n",
2002 (unsigned) pmap->pm_pdir[PTDPTDI], va);
2005 pa = VM_PAGE_TO_PHYS(m) & PG_FRAME;
2006 pmap_inval_add(&info, pmap, va); /* XXX non-optimal */
2007 origpte = *(vm_offset_t *)pte;
2008 opa = origpte & PG_FRAME;
2010 if (origpte & PG_PS)
2011 panic("pmap_enter: attempted pmap_enter on 4MB page");
2014 * Mapping has not changed, must be protection or wiring change.
2016 if (origpte && (opa == pa)) {
2018 * Wiring change, just update stats. We don't worry about
2019 * wiring PT pages as they remain resident as long as there
2020 * are valid mappings in them. Hence, if a user page is wired,
2021 * the PT page will be also.
2023 if (wired && ((origpte & PG_W) == 0))
2024 pmap->pm_stats.wired_count++;
2025 else if (!wired && (origpte & PG_W))
2026 pmap->pm_stats.wired_count--;
2028 #if defined(PMAP_DIAGNOSTIC)
2029 if (pmap_nw_modified((pt_entry_t) origpte)) {
2031 "pmap_enter: modified page not writable: va: 0x%x, pte: 0x%x\n",
2037 * Remove the extra pte reference. Note that we cannot
2038 * optimize the RO->RW case because we have adjusted the
2039 * wiring count above and may need to adjust the wiring
2046 * We might be turning off write access to the page,
2047 * so we go ahead and sense modify status.
2049 if (origpte & PG_MANAGED) {
2050 if ((origpte & PG_M) && pmap_track_modified(va)) {
2052 om = PHYS_TO_VM_PAGE(opa);
2060 * Mapping has changed, invalidate old range and fall through to
2061 * handle validating new mapping.
2065 err = pmap_remove_pte(pmap, pte, va, &info);
2067 panic("pmap_enter: pte vanished, va: 0x%x", va);
2071 * Enter on the PV list if part of our managed memory. Note that we
2072 * raise IPL while manipulating pv_table since pmap_enter can be
2073 * called at interrupt time.
2075 if (pmap_initialized &&
2076 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2077 pmap_insert_entry(pmap, va, mpte, m);
2082 * Increment counters
2084 pmap->pm_stats.resident_count++;
2086 pmap->pm_stats.wired_count++;
2090 * Now validate mapping with desired protection/wiring.
2092 newpte = (vm_offset_t) (pa | pte_prot(pmap, prot) | PG_V);
2096 if (va < UPT_MIN_ADDRESS)
2098 if (pmap == kernel_pmap)
2102 * if the mapping or permission bits are different, we need
2103 * to update the pte.
2105 if ((origpte & ~(PG_M|PG_A)) != newpte) {
2106 *pte = newpte | PG_A;
2108 pmap_inval_flush(&info);
2112 * this code makes some *MAJOR* assumptions:
2113 * 1. Current pmap & pmap exists.
2116 * 4. No page table pages.
2117 * 5. Tlbflush is deferred to calling procedure.
2118 * 6. Page IS managed.
2119 * but is *MUCH* faster than pmap_enter...
2123 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_page_t mpte)
2127 pmap_inval_info info;
2129 pmap_inval_init(&info);
2132 * In the case that a page table page is not
2133 * resident, we are creating it here.
2135 if (va < UPT_MIN_ADDRESS) {
2140 * Calculate pagetable page index
2142 ptepindex = va >> PDRSHIFT;
2143 if (mpte && (mpte->pindex == ptepindex)) {
2148 * Get the page directory entry
2150 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
2153 * If the page table page is mapped, we just increment
2154 * the hold count, and activate it.
2158 panic("pmap_enter_quick: unexpected mapping into 4MB page");
2159 if (pmap->pm_ptphint &&
2160 (pmap->pm_ptphint->pindex == ptepindex)) {
2161 mpte = pmap->pm_ptphint;
2163 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
2164 pmap->pm_ptphint = mpte;
2170 mpte = _pmap_allocpte(pmap, ptepindex);
2178 * This call to vtopte makes the assumption that we are
2179 * entering the page into the current pmap. In order to support
2180 * quick entry into any pmap, one would likely use pmap_pte_quick.
2181 * But that isn't as quick as vtopte.
2183 pte = (unsigned *)vtopte(va);
2186 pmap_unwire_pte_hold(pmap, mpte, &info);
2191 * Enter on the PV list if part of our managed memory. Note that we
2192 * raise IPL while manipulating pv_table since pmap_enter can be
2193 * called at interrupt time.
2195 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0)
2196 pmap_insert_entry(pmap, va, mpte, m);
2199 * Increment counters
2201 pmap->pm_stats.resident_count++;
2203 pa = VM_PAGE_TO_PHYS(m);
2206 * Now validate mapping with RO protection
2208 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2209 *pte = pa | PG_V | PG_U;
2211 *pte = pa | PG_V | PG_U | PG_MANAGED;
2217 * Make a temporary mapping for a physical address. This is only intended
2218 * to be used for panic dumps.
2221 pmap_kenter_temporary(vm_paddr_t pa, int i)
2223 pmap_kenter((vm_offset_t)crashdumpmap + (i * PAGE_SIZE), pa);
2224 return ((void *)crashdumpmap);
2227 #define MAX_INIT_PT (96)
2230 * This routine preloads the ptes for a given object into the specified pmap.
2231 * This eliminates the blast of soft faults on process startup and
2232 * immediately after an mmap.
2235 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2236 vm_object_t object, vm_pindex_t pindex,
2237 vm_size_t size, int limit)
2245 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2250 * XXX you must be joking, entering PTE's into a user page table
2251 * without any accounting? This could result in the page table
2252 * being freed while it still contains mappings (free with PG_ZERO
2253 * assumption leading to a non-zero page being marked PG_ZERO).
2256 * This code maps large physical mmap regions into the
2257 * processor address space. Note that some shortcuts
2258 * are taken, but the code works.
2261 (object->type == OBJT_DEVICE) &&
2262 ((addr & (NBPDR - 1)) == 0) &&
2263 ((size & (NBPDR - 1)) == 0) ) {
2266 unsigned int ptepindex;
2270 if (pmap->pm_pdir[ptepindex = (addr >> PDRSHIFT)])
2274 p = vm_page_lookup(object, pindex);
2275 if (p && vm_page_sleep_busy(p, FALSE, "init4p"))
2279 p = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL);
2284 if (vm_pager_get_pages(object, m, 1, 0) != VM_PAGER_OK) {
2289 p = vm_page_lookup(object, pindex);
2293 ptepa = (vm_offset_t) VM_PAGE_TO_PHYS(p);
2294 if (ptepa & (NBPDR - 1)) {
2298 p->valid = VM_PAGE_BITS_ALL;
2300 pmap->pm_stats.resident_count += size >> PAGE_SHIFT;
2301 npdes = size >> PDRSHIFT;
2302 for (i = 0; i < npdes; i++) {
2303 pmap->pm_pdir[ptepindex] =
2304 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_PS);
2308 vm_page_flag_set(p, PG_MAPPED);
2315 psize = i386_btop(size);
2317 if ((object->type != OBJT_VNODE) ||
2318 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2319 (object->resident_page_count > MAX_INIT_PT))) {
2323 if (psize + pindex > object->size) {
2324 if (object->size < pindex)
2326 psize = object->size - pindex;
2331 * If we are processing a major portion of the object, then scan the
2334 * We cannot safely scan the object's memq unless we are at splvm(),
2335 * since interrupts can remove pages from objects.
2339 if (psize > (object->resident_page_count >> 2)) {
2342 for (p = TAILQ_FIRST(&object->memq);
2343 objpgs > 0 && p != NULL;
2344 p = TAILQ_NEXT(p, listq)
2347 if (tmpidx < pindex)
2350 if (tmpidx >= psize)
2354 * don't allow an madvise to blow away our really
2355 * free pages allocating pv entries.
2357 if ((limit & MAP_PREFAULT_MADVISE) &&
2358 vmstats.v_free_count < vmstats.v_free_reserved) {
2361 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2363 (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2364 if ((p->queue - p->pc) == PQ_CACHE)
2365 vm_page_deactivate(p);
2367 mpte = pmap_enter_quick(pmap,
2368 addr + i386_ptob(tmpidx), p, mpte);
2369 vm_page_flag_set(p, PG_MAPPED);
2376 * else lookup the pages one-by-one.
2378 for (tmpidx = 0; tmpidx < psize; tmpidx += 1) {
2380 * don't allow an madvise to blow away our really
2381 * free pages allocating pv entries.
2383 if ((limit & MAP_PREFAULT_MADVISE) &&
2384 vmstats.v_free_count < vmstats.v_free_reserved) {
2387 p = vm_page_lookup(object, tmpidx + pindex);
2389 ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2391 (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2392 if ((p->queue - p->pc) == PQ_CACHE)
2393 vm_page_deactivate(p);
2395 mpte = pmap_enter_quick(pmap,
2396 addr + i386_ptob(tmpidx), p, mpte);
2397 vm_page_flag_set(p, PG_MAPPED);
2406 * pmap_prefault provides a quick way of clustering pagefaults into a
2407 * processes address space. It is a "cousin" of pmap_object_init_pt,
2408 * except it runs at page fault time instead of mmap time.
2412 #define PAGEORDER_SIZE (PFBAK+PFFOR)
2414 static int pmap_prefault_pageorder[] = {
2415 -PAGE_SIZE, PAGE_SIZE,
2416 -2 * PAGE_SIZE, 2 * PAGE_SIZE,
2417 -3 * PAGE_SIZE, 3 * PAGE_SIZE,
2418 -4 * PAGE_SIZE, 4 * PAGE_SIZE
2422 pmap_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry)
2432 if (!curproc || (pmap != vmspace_pmap(curproc->p_vmspace)))
2435 object = entry->object.vm_object;
2437 starta = addra - PFBAK * PAGE_SIZE;
2438 if (starta < entry->start)
2439 starta = entry->start;
2440 else if (starta > addra)
2444 * splvm() protection is required to maintain the page/object
2445 * association, interrupts can free pages and remove them from
2450 for (i = 0; i < PAGEORDER_SIZE; i++) {
2451 vm_object_t lobject;
2454 addr = addra + pmap_prefault_pageorder[i];
2455 if (addr > addra + (PFFOR * PAGE_SIZE))
2458 if (addr < starta || addr >= entry->end)
2461 if ((*pmap_pde(pmap, addr)) == NULL)
2464 pte = (unsigned *) vtopte(addr);
2468 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2471 for (m = vm_page_lookup(lobject, pindex);
2472 (!m && (lobject->type == OBJT_DEFAULT) &&
2473 (lobject->backing_object));
2474 lobject = lobject->backing_object
2476 if (lobject->backing_object_offset & PAGE_MASK)
2478 pindex += (lobject->backing_object_offset >> PAGE_SHIFT);
2479 m = vm_page_lookup(lobject->backing_object, pindex);
2483 * give-up when a page is not in memory
2488 if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2490 (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2492 if ((m->queue - m->pc) == PQ_CACHE) {
2493 vm_page_deactivate(m);
2496 mpte = pmap_enter_quick(pmap, addr, m, mpte);
2497 vm_page_flag_set(m, PG_MAPPED);
2505 * Routine: pmap_change_wiring
2506 * Function: Change the wiring attribute for a map/virtual-address
2508 * In/out conditions:
2509 * The mapping must already exist in the pmap.
2512 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2519 pte = pmap_pte(pmap, va);
2521 if (wired && !pmap_pte_w(pte))
2522 pmap->pm_stats.wired_count++;
2523 else if (!wired && pmap_pte_w(pte))
2524 pmap->pm_stats.wired_count--;
2527 * Wiring is not a hardware characteristic so there is no need to
2528 * invalidate TLB. However, in an SMP environment we must use
2529 * a locked bus cycle to update the pte (if we are not using
2530 * the pmap_inval_*() API that is)... it's ok to do this for simple
2535 atomic_set_int(pte, PG_W);
2537 atomic_clear_int(pte, PG_W);
2540 atomic_set_int_nonlocked(pte, PG_W);
2542 atomic_clear_int_nonlocked(pte, PG_W);
2549 * Copy the range specified by src_addr/len
2550 * from the source map to the range dst_addr/len
2551 * in the destination map.
2553 * This routine is only advisory and need not do anything.
2556 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2557 vm_size_t len, vm_offset_t src_addr)
2559 pmap_inval_info info;
2561 vm_offset_t end_addr = src_addr + len;
2563 unsigned src_frame, dst_frame;
2567 if (dst_addr != src_addr)
2570 src_frame = ((unsigned) src_pmap->pm_pdir[PTDPTDI]) & PG_FRAME;
2571 if (src_frame != (((unsigned) PTDpde) & PG_FRAME)) {
2575 dst_frame = ((unsigned) dst_pmap->pm_pdir[PTDPTDI]) & PG_FRAME;
2576 if (dst_frame != (((unsigned) APTDpde) & PG_FRAME)) {
2577 APTDpde = (pd_entry_t) (dst_frame | PG_RW | PG_V);
2578 /* The page directory is not shared between CPUs */
2581 pmap_inval_init(&info);
2582 pmap_inval_add(&info, dst_pmap, -1);
2583 pmap_inval_add(&info, src_pmap, -1);
2586 * splvm() protection is required to maintain the page/object
2587 * association, interrupts can free pages and remove them from
2591 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
2592 unsigned *src_pte, *dst_pte;
2593 vm_page_t dstmpte, srcmpte;
2594 vm_offset_t srcptepaddr;
2597 if (addr >= UPT_MIN_ADDRESS)
2598 panic("pmap_copy: invalid to pmap_copy page tables\n");
2601 * Don't let optional prefaulting of pages make us go
2602 * way below the low water mark of free pages or way
2603 * above high water mark of used pv entries.
2605 if (vmstats.v_free_count < vmstats.v_free_reserved ||
2606 pv_entry_count > pv_entry_high_water)
2609 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
2610 ptepindex = addr >> PDRSHIFT;
2612 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex];
2613 if (srcptepaddr == 0)
2616 if (srcptepaddr & PG_PS) {
2617 if (dst_pmap->pm_pdir[ptepindex] == 0) {
2618 dst_pmap->pm_pdir[ptepindex] = (pd_entry_t) srcptepaddr;
2619 dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
2624 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
2625 if ((srcmpte == NULL) ||
2626 (srcmpte->hold_count == 0) || (srcmpte->flags & PG_BUSY))
2629 if (pdnxt > end_addr)
2632 src_pte = (unsigned *) vtopte(addr);
2633 dst_pte = (unsigned *) avtopte(addr);
2634 while (addr < pdnxt) {
2638 * we only virtual copy managed pages
2640 if ((ptetemp & PG_MANAGED) != 0) {
2642 * We have to check after allocpte for the
2643 * pte still being around... allocpte can
2646 dstmpte = pmap_allocpte(dst_pmap, addr);
2647 if ((*dst_pte == 0) && (ptetemp = *src_pte)) {
2649 * Clear the modified and
2650 * accessed (referenced) bits
2653 m = PHYS_TO_VM_PAGE(ptetemp);
2654 *dst_pte = ptetemp & ~(PG_M | PG_A);
2655 dst_pmap->pm_stats.resident_count++;
2656 pmap_insert_entry(dst_pmap, addr,
2659 pmap_unwire_pte_hold(dst_pmap, dstmpte, &info);
2661 if (dstmpte->hold_count >= srcmpte->hold_count)
2670 pmap_inval_flush(&info);
2674 * Routine: pmap_kernel
2676 * Returns the physical map handle for the kernel.
2681 return (kernel_pmap);
2687 * Zero the specified PA by mapping the page into KVM and clearing its
2690 * This function may be called from an interrupt and no locking is
2694 pmap_zero_page(vm_paddr_t phys)
2696 struct mdglobaldata *gd = mdcpu;
2699 if (*(int *)gd->gd_CMAP3)
2700 panic("pmap_zero_page: CMAP3 busy");
2701 *(int *)gd->gd_CMAP3 =
2702 PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2703 cpu_invlpg(gd->gd_CADDR3);
2705 #if defined(I686_CPU)
2706 if (cpu_class == CPUCLASS_686)
2707 i686_pagezero(gd->gd_CADDR3);
2710 bzero(gd->gd_CADDR3, PAGE_SIZE);
2711 *(int *) gd->gd_CMAP3 = 0;
2716 * pmap_page_assertzero:
2718 * Assert that a page is empty, panic if it isn't.
2721 pmap_page_assertzero(vm_paddr_t phys)
2723 struct mdglobaldata *gd = mdcpu;
2727 if (*(int *)gd->gd_CMAP3)
2728 panic("pmap_zero_page: CMAP3 busy");
2729 *(int *)gd->gd_CMAP3 =
2730 PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2731 cpu_invlpg(gd->gd_CADDR3);
2732 for (i = 0; i < PAGE_SIZE; i += 4) {
2733 if (*(int *)((char *)gd->gd_CADDR3 + i) != 0) {
2734 panic("pmap_page_assertzero() @ %p not zero!\n",
2735 (void *)gd->gd_CADDR3);
2738 *(int *) gd->gd_CMAP3 = 0;
2745 * Zero part of a physical page by mapping it into memory and clearing
2746 * its contents with bzero.
2748 * off and size may not cover an area beyond a single hardware page.
2751 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
2753 struct mdglobaldata *gd = mdcpu;
2756 if (*(int *) gd->gd_CMAP3)
2757 panic("pmap_zero_page: CMAP3 busy");
2758 *(int *) gd->gd_CMAP3 = PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2759 cpu_invlpg(gd->gd_CADDR3);
2761 #if defined(I686_CPU)
2762 if (cpu_class == CPUCLASS_686 && off == 0 && size == PAGE_SIZE)
2763 i686_pagezero(gd->gd_CADDR3);
2766 bzero((char *)gd->gd_CADDR3 + off, size);
2767 *(int *) gd->gd_CMAP3 = 0;
2774 * Copy the physical page from the source PA to the target PA.
2775 * This function may be called from an interrupt. No locking
2779 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
2781 struct mdglobaldata *gd = mdcpu;
2784 if (*(int *) gd->gd_CMAP1)
2785 panic("pmap_copy_page: CMAP1 busy");
2786 if (*(int *) gd->gd_CMAP2)
2787 panic("pmap_copy_page: CMAP2 busy");
2789 *(int *) gd->gd_CMAP1 = PG_V | (src & PG_FRAME) | PG_A;
2790 *(int *) gd->gd_CMAP2 = PG_V | PG_RW | (dst & PG_FRAME) | PG_A | PG_M;
2792 cpu_invlpg(gd->gd_CADDR1);
2793 cpu_invlpg(gd->gd_CADDR2);
2795 bcopy(gd->gd_CADDR1, gd->gd_CADDR2, PAGE_SIZE);
2797 *(int *) gd->gd_CMAP1 = 0;
2798 *(int *) gd->gd_CMAP2 = 0;
2803 * pmap_copy_page_frag:
2805 * Copy the physical page from the source PA to the target PA.
2806 * This function may be called from an interrupt. No locking
2810 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
2812 struct mdglobaldata *gd = mdcpu;
2815 if (*(int *) gd->gd_CMAP1)
2816 panic("pmap_copy_page: CMAP1 busy");
2817 if (*(int *) gd->gd_CMAP2)
2818 panic("pmap_copy_page: CMAP2 busy");
2820 *(int *) gd->gd_CMAP1 = PG_V | (src & PG_FRAME) | PG_A;
2821 *(int *) gd->gd_CMAP2 = PG_V | PG_RW | (dst & PG_FRAME) | PG_A | PG_M;
2823 cpu_invlpg(gd->gd_CADDR1);
2824 cpu_invlpg(gd->gd_CADDR2);
2826 bcopy((char *)gd->gd_CADDR1 + (src & PAGE_MASK),
2827 (char *)gd->gd_CADDR2 + (dst & PAGE_MASK),
2830 *(int *) gd->gd_CMAP1 = 0;
2831 *(int *) gd->gd_CMAP2 = 0;
2836 * Returns true if the pmap's pv is one of the first
2837 * 16 pvs linked to from this page. This count may
2838 * be changed upwards or downwards in the future; it
2839 * is only necessary that true be returned for a small
2840 * subset of pmaps for proper page aging.
2843 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2849 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2854 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2855 if (pv->pv_pmap == pmap) {
2867 #define PMAP_REMOVE_PAGES_CURPROC_ONLY
2869 * Remove all pages from specified address space
2870 * this aids process exit speeds. Also, this code
2871 * is special cased for current process only, but
2872 * can have the more generic (and slightly slower)
2873 * mode enabled. This is much faster than pmap_remove
2874 * in the case of running down an entire address space.
2877 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2879 unsigned *pte, tpte;
2883 pmap_inval_info info;
2885 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2886 if (!curproc || (pmap != vmspace_pmap(curproc->p_vmspace))) {
2887 printf("warning: pmap_remove_pages called with non-current pmap\n");
2892 pmap_inval_init(&info);
2894 for(pv = TAILQ_FIRST(&pmap->pm_pvlist);
2898 if (pv->pv_va >= eva || pv->pv_va < sva) {
2899 npv = TAILQ_NEXT(pv, pv_plist);
2903 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2904 pte = (unsigned *)vtopte(pv->pv_va);
2906 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2908 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
2912 * We cannot remove wired pages from a process' mapping at this time
2915 npv = TAILQ_NEXT(pv, pv_plist);
2920 m = PHYS_TO_VM_PAGE(tpte);
2922 KASSERT(m < &vm_page_array[vm_page_array_size],
2923 ("pmap_remove_pages: bad tpte %x", tpte));
2925 pv->pv_pmap->pm_stats.resident_count--;
2928 * Update the vm_page_t clean and reference bits.
2935 npv = TAILQ_NEXT(pv, pv_plist);
2936 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2938 m->md.pv_list_count--;
2939 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2940 if (TAILQ_FIRST(&m->md.pv_list) == NULL) {
2941 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2944 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem, &info);
2947 pmap_inval_flush(&info);
2952 * pmap_testbit tests bits in pte's
2953 * note that the testbit/changebit routines are inline,
2954 * and a lot of things compile-time evaluate.
2957 pmap_testbit(vm_page_t m, int bit)
2963 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2966 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2971 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2973 * if the bit being tested is the modified bit, then
2974 * mark clean_map and ptes as never
2977 if (bit & (PG_A|PG_M)) {
2978 if (!pmap_track_modified(pv->pv_va))
2982 #if defined(PMAP_DIAGNOSTIC)
2984 printf("Null pmap (tb) at va: 0x%x\n", pv->pv_va);
2988 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2999 * this routine is used to modify bits in ptes
3001 static __inline void
3002 pmap_changebit(vm_page_t m, int bit, boolean_t setem)
3004 struct pmap_inval_info info;
3009 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3012 pmap_inval_init(&info);
3016 * Loop over all current mappings setting/clearing as appropos If
3017 * setting RO do we need to clear the VAC?
3019 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3021 * don't write protect pager mappings
3023 if (!setem && (bit == PG_RW)) {
3024 if (!pmap_track_modified(pv->pv_va))
3028 #if defined(PMAP_DIAGNOSTIC)
3030 printf("Null pmap (cb) at va: 0x%x\n", pv->pv_va);
3036 * Careful here. We can use a locked bus instruction to
3037 * clear PG_A or PG_M safely but we need to synchronize
3038 * with the target cpus when we mess with PG_RW.
3040 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3042 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
3046 atomic_set_int(pte, bit);
3048 atomic_set_int_nonlocked(pte, bit);
3051 vm_offset_t pbits = *(vm_offset_t *)pte;
3058 atomic_clear_int(pte, PG_M|PG_RW);
3060 atomic_clear_int_nonlocked(pte, PG_M|PG_RW);
3064 atomic_clear_int(pte, bit);
3066 atomic_clear_int_nonlocked(pte, bit);
3072 pmap_inval_flush(&info);
3077 * pmap_page_protect:
3079 * Lower the permission for all mappings to a given page.
3082 pmap_page_protect(vm_page_t m, vm_prot_t prot)
3084 if ((prot & VM_PROT_WRITE) == 0) {
3085 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
3086 pmap_changebit(m, PG_RW, FALSE);
3094 pmap_phys_address(int ppn)
3096 return (i386_ptob(ppn));
3100 * pmap_ts_referenced:
3102 * Return a count of reference bits for a page, clearing those bits.
3103 * It is not necessary for every reference bit to be cleared, but it
3104 * is necessary that 0 only be returned when there are truly no
3105 * reference bits set.
3107 * XXX: The exact number of bits to check and clear is a matter that
3108 * should be tested and standardized at some point in the future for
3109 * optimal aging of shared pages.
3112 pmap_ts_referenced(vm_page_t m)
3114 pv_entry_t pv, pvf, pvn;
3119 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3124 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3129 pvn = TAILQ_NEXT(pv, pv_list);
3131 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3133 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3135 if (!pmap_track_modified(pv->pv_va))
3138 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3140 if (pte && (*pte & PG_A)) {
3142 atomic_clear_int(pte, PG_A);
3144 atomic_clear_int_nonlocked(pte, PG_A);
3151 } while ((pv = pvn) != NULL && pv != pvf);
3161 * Return whether or not the specified physical page was modified
3162 * in any physical maps.
3165 pmap_is_modified(vm_page_t m)
3167 return pmap_testbit(m, PG_M);
3171 * Clear the modify bits on the specified physical page.
3174 pmap_clear_modify(vm_page_t m)
3176 pmap_changebit(m, PG_M, FALSE);
3180 * pmap_clear_reference:
3182 * Clear the reference bit on the specified physical page.
3185 pmap_clear_reference(vm_page_t m)
3187 pmap_changebit(m, PG_A, FALSE);
3191 * Miscellaneous support routines follow
3195 i386_protection_init(void)
3199 kp = protection_codes;
3200 for (prot = 0; prot < 8; prot++) {
3202 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE:
3204 * Read access is also 0. There isn't any execute bit,
3205 * so just make it readable.
3207 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE:
3208 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE:
3209 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE:
3212 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE:
3213 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE:
3214 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE:
3215 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
3223 * Map a set of physical memory pages into the kernel virtual
3224 * address space. Return a pointer to where it is mapped. This
3225 * routine is intended to be used for mapping device memory,
3228 * NOTE: we can't use pgeflag unless we invalidate the pages one at
3232 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
3234 vm_offset_t va, tmpva, offset;
3237 offset = pa & PAGE_MASK;
3238 size = roundup(offset + size, PAGE_SIZE);
3240 va = kmem_alloc_nofault(kernel_map, size);
3242 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3245 for (tmpva = va; size > 0;) {
3246 pte = (unsigned *)vtopte(tmpva);
3247 *pte = pa | PG_RW | PG_V; /* | pgeflag; */
3255 return ((void *)(va + offset));
3259 pmap_unmapdev(vm_offset_t va, vm_size_t size)
3261 vm_offset_t base, offset;
3263 base = va & PG_FRAME;
3264 offset = va & PAGE_MASK;
3265 size = roundup(offset + size, PAGE_SIZE);
3266 pmap_qremove(va, size >> PAGE_SHIFT);
3267 kmem_free(kernel_map, base, size);
3271 * perform the pmap work for mincore
3274 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3276 unsigned *ptep, pte;
3280 ptep = pmap_pte(pmap, addr);
3285 if ((pte = *ptep) != 0) {
3288 val = MINCORE_INCORE;
3289 if ((pte & PG_MANAGED) == 0)
3292 pa = pte & PG_FRAME;
3294 m = PHYS_TO_VM_PAGE(pa);
3300 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3302 * Modified by someone
3304 else if (m->dirty || pmap_is_modified(m))
3305 val |= MINCORE_MODIFIED_OTHER;
3310 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3313 * Referenced by someone
3315 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3316 val |= MINCORE_REFERENCED_OTHER;
3317 vm_page_flag_set(m, PG_REFERENCED);
3324 pmap_activate(struct proc *p)
3328 pmap = vmspace_pmap(p->p_vmspace);
3330 atomic_set_int(&pmap->pm_active, 1 << mycpu->gd_cpuid);
3332 pmap->pm_active |= 1;
3334 #if defined(SWTCH_OPTIM_STATS)
3337 p->p_thread->td_pcb->pcb_cr3 = vtophys(pmap->pm_pdir);
3338 load_cr3(p->p_thread->td_pcb->pcb_cr3);
3342 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3345 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3349 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3354 #if defined(PMAP_DEBUG)
3356 pmap_pid_dump(int pid)
3362 FOREACH_PROC_IN_SYSTEM(p) {
3363 if (p->p_pid != pid)
3369 pmap = vmspace_pmap(p->p_vmspace);
3370 for(i=0;i<1024;i++) {
3373 unsigned base = i << PDRSHIFT;
3375 pde = &pmap->pm_pdir[i];
3376 if (pde && pmap_pde_v(pde)) {
3377 for(j=0;j<1024;j++) {
3378 unsigned va = base + (j << PAGE_SHIFT);
3379 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) {
3386 pte = pmap_pte_quick( pmap, va);
3387 if (pte && pmap_pte_v(pte)) {
3391 m = PHYS_TO_VM_PAGE(pa);
3392 printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x",
3393 va, pa, m->hold_count, m->wire_count, m->flags);
3414 static void pads (pmap_t pm);
3415 void pmap_pvdump (vm_paddr_t pa);
3417 /* print address space of pmap*/
3424 if (pm == kernel_pmap)
3426 for (i = 0; i < 1024; i++)
3428 for (j = 0; j < 1024; j++) {
3429 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
3430 if (pm == kernel_pmap && va < KERNBASE)
3432 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS)
3434 ptep = pmap_pte_quick(pm, va);
3435 if (pmap_pte_v(ptep))
3436 printf("%x:%x ", va, *(int *) ptep);
3442 pmap_pvdump(vm_paddr_t pa)
3447 printf("pa %08llx", (long long)pa);
3448 m = PHYS_TO_VM_PAGE(pa);
3449 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3451 printf(" -> pmap %p, va %x, flags %x",
3452 (void *)pv->pv_pmap, pv->pv_va, pv->pv_flags);
3454 printf(" -> pmap %p, va %x", (void *)pv->pv_pmap, pv->pv_va);