2 * Copyright (c) 1991 Regents of the University of California.
4 * Copyright (c) 1994 John S. Dyson
6 * Copyright (c) 1994 David Greenman
9 * This code is derived from software contributed to Berkeley by
10 * the Systems Programming Group of the University of Utah Computer
11 * Science Department and William Jolitz of UUNET Technologies Inc.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91
42 * $FreeBSD: src/sys/i386/i386/pmap.c,v 1.250.2.18 2002/03/06 22:48:53 silby Exp $
43 * $DragonFly: src/sys/platform/pc32/i386/pmap.c,v 1.47 2004/10/12 19:29:26 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 with a critical section held so the page's object
880 * association 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 td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
903 td->td_savefpu = &td->td_pcb->pcb_save;
904 td->td_sp = (char *)td->td_pcb - 16;
908 * Create the UPAGES for a new process.
909 * This routine directly affects the fork perf for a process.
912 pmap_init_proc(struct proc *p, struct thread *td)
914 p->p_addr = (void *)td->td_kstack;
917 td->td_switch = cpu_heavy_switch;
921 bzero(p->p_addr, sizeof(*p->p_addr));
925 * Dispose the UPAGES for a process that has exited.
926 * This routine directly impacts the exit perf of a process.
929 pmap_dispose_proc(struct proc *p)
933 KASSERT(p->p_lock == 0, ("attempt to dispose referenced proc! %p", p));
935 if ((td = p->p_thread) != NULL) {
944 * Allow the UPAGES for a process to be prejudicially paged out.
947 pmap_swapout_proc(struct proc *p)
955 upobj = p->p_upages_obj;
958 * Unwiring the pages allow them to be paged to their backing store
962 for (i = 0; i < UPAGES; i++) {
963 if ((m = vm_page_lookup(upobj, i)) == NULL)
964 panic("pmap_swapout_proc: upage already missing???");
966 vm_page_unwire(m, 0);
967 pmap_kremove((vm_offset_t)p->p_addr + (PAGE_SIZE * i));
974 * Bring the UPAGES for a specified process back in.
977 pmap_swapin_proc(struct proc *p)
985 upobj = p->p_upages_obj;
986 for (i = 0; i < UPAGES; i++) {
987 m = vm_page_grab(upobj, i, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
989 pmap_kenter((vm_offset_t)p->p_addr + (i * PAGE_SIZE),
992 if (m->valid != VM_PAGE_BITS_ALL) {
993 rv = vm_pager_get_pages(upobj, &m, 1, 0);
994 if (rv != VM_PAGER_OK)
995 panic("pmap_swapin_proc: cannot get upages for proc: %d\n", p->p_pid);
996 m = vm_page_lookup(upobj, i);
997 m->valid = VM_PAGE_BITS_ALL;
1001 vm_page_flag_set(m, PG_MAPPED | PG_WRITEABLE);
1007 /***************************************************
1008 * Page table page management routines.....
1009 ***************************************************/
1012 * This routine unholds page table pages, and if the hold count
1013 * drops to zero, then it decrements the wire count.
1016 _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, pmap_inval_info_t info)
1018 pmap_inval_flush(info);
1019 while (vm_page_sleep_busy(m, FALSE, "pmuwpt"))
1022 if (m->hold_count == 0) {
1024 * unmap the page table page
1026 pmap_inval_add(info, pmap, -1);
1027 pmap->pm_pdir[m->pindex] = 0;
1028 --pmap->pm_stats.resident_count;
1030 if (pmap->pm_ptphint == m)
1031 pmap->pm_ptphint = NULL;
1034 * If the page is finally unwired, simply free it.
1037 if (m->wire_count == 0) {
1040 vm_page_free_zero(m);
1041 --vmstats.v_wire_count;
1048 static PMAP_INLINE int
1049 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, pmap_inval_info_t info)
1052 if (m->hold_count == 0)
1053 return _pmap_unwire_pte_hold(pmap, m, info);
1059 * After removing a page table entry, this routine is used to
1060 * conditionally free the page, and manage the hold/wire counts.
1063 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte,
1064 pmap_inval_info_t info)
1067 if (va >= UPT_MIN_ADDRESS)
1071 ptepindex = (va >> PDRSHIFT);
1072 if (pmap->pm_ptphint &&
1073 (pmap->pm_ptphint->pindex == ptepindex)) {
1074 mpte = pmap->pm_ptphint;
1076 pmap_inval_flush(info);
1077 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1078 pmap->pm_ptphint = mpte;
1082 return pmap_unwire_pte_hold(pmap, mpte, info);
1086 pmap_pinit0(struct pmap *pmap)
1089 (pd_entry_t *)kmem_alloc_pageable(kernel_map, PAGE_SIZE);
1090 pmap_kenter((vm_offset_t)pmap->pm_pdir, (vm_offset_t) IdlePTD);
1092 pmap->pm_active = 0;
1093 pmap->pm_ptphint = NULL;
1094 TAILQ_INIT(&pmap->pm_pvlist);
1095 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1099 * Initialize a preallocated and zeroed pmap structure,
1100 * such as one in a vmspace structure.
1103 pmap_pinit(struct pmap *pmap)
1108 * No need to allocate page table space yet but we do need a valid
1109 * page directory table.
1111 if (pmap->pm_pdir == NULL) {
1113 (pd_entry_t *)kmem_alloc_pageable(kernel_map, PAGE_SIZE);
1117 * allocate object for the ptes
1119 if (pmap->pm_pteobj == NULL)
1120 pmap->pm_pteobj = vm_object_allocate( OBJT_DEFAULT, PTDPTDI + 1);
1123 * allocate the page directory page
1125 ptdpg = vm_page_grab( pmap->pm_pteobj, PTDPTDI,
1126 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1128 ptdpg->wire_count = 1;
1129 ++vmstats.v_wire_count;
1132 vm_page_flag_clear(ptdpg, PG_MAPPED | PG_BUSY); /* not usually mapped*/
1133 ptdpg->valid = VM_PAGE_BITS_ALL;
1135 pmap_kenter((vm_offset_t)pmap->pm_pdir, VM_PAGE_TO_PHYS(ptdpg));
1136 if ((ptdpg->flags & PG_ZERO) == 0)
1137 bzero(pmap->pm_pdir, PAGE_SIZE);
1139 pmap->pm_pdir[MPPTDI] = PTD[MPPTDI];
1141 /* install self-referential address mapping entry */
1142 *(unsigned *) (pmap->pm_pdir + PTDPTDI) =
1143 VM_PAGE_TO_PHYS(ptdpg) | PG_V | PG_RW | PG_A | PG_M;
1146 pmap->pm_active = 0;
1147 pmap->pm_ptphint = NULL;
1148 TAILQ_INIT(&pmap->pm_pvlist);
1149 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1153 * Wire in kernel global address entries. To avoid a race condition
1154 * between pmap initialization and pmap_growkernel, this procedure
1155 * should be called after the vmspace is attached to the process
1156 * but before this pmap is activated.
1159 pmap_pinit2(struct pmap *pmap)
1161 /* XXX copies current process, does not fill in MPPTDI */
1162 bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * PTESIZE);
1166 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1168 unsigned *pde = (unsigned *) pmap->pm_pdir;
1170 * This code optimizes the case of freeing non-busy
1171 * page-table pages. Those pages are zero now, and
1172 * might as well be placed directly into the zero queue.
1174 if (vm_page_sleep_busy(p, FALSE, "pmaprl"))
1180 * Remove the page table page from the processes address space.
1183 pmap->pm_stats.resident_count--;
1185 if (p->hold_count) {
1186 panic("pmap_release: freeing held page table page");
1189 * Page directory pages need to have the kernel
1190 * stuff cleared, so they can go into the zero queue also.
1192 if (p->pindex == PTDPTDI) {
1193 bzero(pde + KPTDI, nkpt * PTESIZE);
1196 pmap_kremove((vm_offset_t)pmap->pm_pdir);
1199 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1200 pmap->pm_ptphint = NULL;
1203 vmstats.v_wire_count--;
1204 vm_page_free_zero(p);
1209 * this routine is called if the page table page is not
1213 _pmap_allocpte(pmap_t pmap, unsigned ptepindex)
1215 vm_offset_t pteva, ptepa;
1219 * Find or fabricate a new pagetable page
1221 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1222 VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1224 KASSERT(m->queue == PQ_NONE,
1225 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1227 if (m->wire_count == 0)
1228 vmstats.v_wire_count++;
1232 * Increment the hold count for the page table page
1233 * (denoting a new mapping.)
1238 * Map the pagetable page into the process address space, if
1239 * it isn't already there.
1242 pmap->pm_stats.resident_count++;
1244 ptepa = VM_PAGE_TO_PHYS(m);
1245 pmap->pm_pdir[ptepindex] =
1246 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_A | PG_M);
1249 * Set the page table hint
1251 pmap->pm_ptphint = m;
1254 * Try to use the new mapping, but if we cannot, then
1255 * do it with the routine that maps the page explicitly.
1257 if ((m->flags & PG_ZERO) == 0) {
1258 if ((((unsigned)pmap->pm_pdir[PTDPTDI]) & PG_FRAME) ==
1259 (((unsigned) PTDpde) & PG_FRAME)) {
1260 pteva = UPT_MIN_ADDRESS + i386_ptob(ptepindex);
1261 bzero((caddr_t) pteva, PAGE_SIZE);
1263 pmap_zero_page(ptepa);
1267 m->valid = VM_PAGE_BITS_ALL;
1268 vm_page_flag_clear(m, PG_ZERO);
1269 vm_page_flag_set(m, PG_MAPPED);
1276 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1283 * Calculate pagetable page index
1285 ptepindex = va >> PDRSHIFT;
1288 * Get the page directory entry
1290 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1293 * This supports switching from a 4MB page to a
1296 if (ptepa & PG_PS) {
1297 pmap->pm_pdir[ptepindex] = 0;
1304 * If the page table page is mapped, we just increment the
1305 * hold count, and activate it.
1309 * In order to get the page table page, try the
1312 if (pmap->pm_ptphint &&
1313 (pmap->pm_ptphint->pindex == ptepindex)) {
1314 m = pmap->pm_ptphint;
1316 m = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1317 pmap->pm_ptphint = m;
1323 * Here if the pte page isn't mapped, or if it has been deallocated.
1325 return _pmap_allocpte(pmap, ptepindex);
1329 /***************************************************
1330 * Pmap allocation/deallocation routines.
1331 ***************************************************/
1334 * Release any resources held by the given physical map.
1335 * Called when a pmap initialized by pmap_pinit is being released.
1336 * Should only be called if the map contains no valid mappings.
1339 pmap_release(struct pmap *pmap)
1341 vm_page_t p,n,ptdpg;
1342 vm_object_t object = pmap->pm_pteobj;
1346 #if defined(DIAGNOSTIC)
1347 if (object->ref_count != 1)
1348 panic("pmap_release: pteobj reference count != 1");
1354 curgeneration = object->generation;
1355 for (p = TAILQ_FIRST(&object->memq); p != NULL; p = n) {
1356 n = TAILQ_NEXT(p, listq);
1357 if (p->pindex == PTDPTDI) {
1362 if (!pmap_release_free_page(pmap, p) &&
1363 (object->generation != curgeneration)) {
1371 if (ptdpg && !pmap_release_free_page(pmap, ptdpg))
1376 kvm_size(SYSCTL_HANDLER_ARGS)
1378 unsigned long ksize = VM_MAX_KERNEL_ADDRESS - KERNBASE;
1380 return sysctl_handle_long(oidp, &ksize, 0, req);
1382 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD,
1383 0, 0, kvm_size, "IU", "Size of KVM");
1386 kvm_free(SYSCTL_HANDLER_ARGS)
1388 unsigned long kfree = VM_MAX_KERNEL_ADDRESS - kernel_vm_end;
1390 return sysctl_handle_long(oidp, &kfree, 0, req);
1392 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD,
1393 0, 0, kvm_free, "IU", "Amount of KVM free");
1396 * grow the number of kernel page table entries, if needed
1399 pmap_growkernel(vm_offset_t addr)
1404 vm_offset_t ptppaddr;
1409 if (kernel_vm_end == 0) {
1410 kernel_vm_end = KERNBASE;
1412 while (pdir_pde(PTD, kernel_vm_end)) {
1413 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1417 addr = (addr + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1418 while (kernel_vm_end < addr) {
1419 if (pdir_pde(PTD, kernel_vm_end)) {
1420 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1425 * This index is bogus, but out of the way
1427 nkpg = vm_page_alloc(kptobj, nkpt,
1428 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM | VM_ALLOC_INTERRUPT);
1430 panic("pmap_growkernel: no memory to grow kernel");
1435 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1436 pmap_zero_page(ptppaddr);
1437 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
1438 pdir_pde(PTD, kernel_vm_end) = newpdir;
1440 FOREACH_PROC_IN_SYSTEM(p) {
1442 pmap = vmspace_pmap(p->p_vmspace);
1443 *pmap_pde(pmap, kernel_vm_end) = newpdir;
1446 *pmap_pde(kernel_pmap, kernel_vm_end) = newpdir;
1447 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1453 * Retire the given physical map from service.
1454 * Should only be called if the map contains
1455 * no valid mappings.
1458 pmap_destroy(pmap_t pmap)
1465 count = --pmap->pm_count;
1468 panic("destroying a pmap is not yet implemented");
1473 * Add a reference to the specified pmap.
1476 pmap_reference(pmap_t pmap)
1483 /***************************************************
1484 * page management routines.
1485 ***************************************************/
1488 * free the pv_entry back to the free list. This function may be
1489 * called from an interrupt.
1491 static PMAP_INLINE void
1492 free_pv_entry(pv_entry_t pv)
1499 * get a new pv_entry, allocating a block from the system
1500 * when needed. This function may be called from an interrupt.
1506 if (pv_entry_high_water &&
1507 (pv_entry_count > pv_entry_high_water) &&
1508 (pmap_pagedaemon_waken == 0)) {
1509 pmap_pagedaemon_waken = 1;
1510 wakeup (&vm_pages_needed);
1512 return zalloc(pvzone);
1516 * This routine is very drastic, but can save the system
1524 static int warningdone=0;
1526 if (pmap_pagedaemon_waken == 0)
1529 if (warningdone < 5) {
1530 printf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1534 for(i = 0; i < vm_page_array_size; i++) {
1535 m = &vm_page_array[i];
1536 if (m->wire_count || m->hold_count || m->busy ||
1537 (m->flags & PG_BUSY))
1541 pmap_pagedaemon_waken = 0;
1546 * If it is the first entry on the list, it is actually
1547 * in the header and we must copy the following entry up
1548 * to the header. Otherwise we must search the list for
1549 * the entry. In either case we free the now unused entry.
1552 pmap_remove_entry(struct pmap *pmap, vm_page_t m,
1553 vm_offset_t va, pmap_inval_info_t info)
1560 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1561 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1562 if (pmap == pv->pv_pmap && va == pv->pv_va)
1566 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1567 if (va == pv->pv_va)
1574 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem, info);
1575 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1576 m->md.pv_list_count--;
1577 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
1578 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1579 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1587 * Create a pv entry for page at pa for
1591 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
1597 pv = get_pv_entry();
1602 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1603 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1604 m->md.pv_list_count++;
1610 * pmap_remove_pte: do the things to unmap a page in a process
1613 pmap_remove_pte(struct pmap *pmap, unsigned *ptq, vm_offset_t va,
1614 pmap_inval_info_t info)
1619 pmap_inval_add(info, pmap, va);
1620 oldpte = loadandclear(ptq);
1622 pmap->pm_stats.wired_count -= 1;
1624 * Machines that don't support invlpg, also don't support
1625 * PG_G. XXX PG_G is disabled for SMP so don't worry about
1629 cpu_invlpg((void *)va);
1630 pmap->pm_stats.resident_count -= 1;
1631 if (oldpte & PG_MANAGED) {
1632 m = PHYS_TO_VM_PAGE(oldpte);
1633 if (oldpte & PG_M) {
1634 #if defined(PMAP_DIAGNOSTIC)
1635 if (pmap_nw_modified((pt_entry_t) oldpte)) {
1637 "pmap_remove: modified page not writable: va: 0x%x, pte: 0x%x\n",
1641 if (pmap_track_modified(va))
1645 vm_page_flag_set(m, PG_REFERENCED);
1646 return pmap_remove_entry(pmap, m, va, info);
1648 return pmap_unuse_pt(pmap, va, NULL, info);
1657 * Remove a single page from a process address space.
1659 * This function may not be called from an interrupt if the pmap is
1663 pmap_remove_page(struct pmap *pmap, vm_offset_t va, pmap_inval_info_t info)
1668 * if there is no pte for this address, just skip it!!! Otherwise
1669 * get a local va for mappings for this pmap and remove the entry.
1671 if (*pmap_pde(pmap, va) != 0) {
1672 ptq = get_ptbase(pmap) + i386_btop(va);
1674 pmap_remove_pte(pmap, ptq, va, info);
1682 * Remove the given range of addresses from the specified map.
1684 * It is assumed that the start and end are properly
1685 * rounded to the page size.
1687 * This function may not be called from an interrupt if the pmap is
1691 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
1695 vm_offset_t ptpaddr;
1696 vm_offset_t sindex, eindex;
1697 struct pmap_inval_info info;
1702 if (pmap->pm_stats.resident_count == 0)
1705 pmap_inval_init(&info);
1708 * special handling of removing one page. a very
1709 * common operation and easy to short circuit some
1712 if (((sva + PAGE_SIZE) == eva) &&
1713 (((unsigned) pmap->pm_pdir[(sva >> PDRSHIFT)] & PG_PS) == 0)) {
1714 pmap_remove_page(pmap, sva, &info);
1715 pmap_inval_flush(&info);
1720 * Get a local virtual address for the mappings that are being
1723 ptbase = get_ptbase(pmap);
1725 sindex = i386_btop(sva);
1726 eindex = i386_btop(eva);
1728 for (; sindex < eindex; sindex = pdnxt) {
1732 * Calculate index for next page table.
1734 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1735 if (pmap->pm_stats.resident_count == 0)
1738 pdirindex = sindex / NPDEPG;
1739 if (((ptpaddr = (unsigned) pmap->pm_pdir[pdirindex]) & PG_PS) != 0) {
1740 pmap_inval_add(&info, pmap, -1);
1741 pmap->pm_pdir[pdirindex] = 0;
1742 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1747 * Weed out invalid mappings. Note: we assume that the page
1748 * directory table is always allocated, and in kernel virtual.
1754 * Limit our scan to either the end of the va represented
1755 * by the current page table page, or to the end of the
1756 * range being removed.
1758 if (pdnxt > eindex) {
1762 for (; sindex != pdnxt; sindex++) {
1764 if (ptbase[sindex] == 0)
1766 va = i386_ptob(sindex);
1767 if (pmap_remove_pte(pmap, ptbase + sindex, va, &info))
1771 pmap_inval_flush(&info);
1777 * Removes this physical page from all physical maps in which it resides.
1778 * Reflects back modify bits to the pager.
1780 * This routine may not be called from an interrupt.
1784 pmap_remove_all(vm_page_t m)
1786 struct pmap_inval_info info;
1787 unsigned *pte, tpte;
1791 #if defined(PMAP_DIAGNOSTIC)
1793 * XXX this makes pmap_page_protect(NONE) illegal for non-managed
1796 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
1797 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%08llx", (long long)VM_PAGE_TO_PHYS(m));
1801 pmap_inval_init(&info);
1803 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
1804 pv->pv_pmap->pm_stats.resident_count--;
1806 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
1807 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
1809 tpte = loadandclear(pte);
1811 pv->pv_pmap->pm_stats.wired_count--;
1814 vm_page_flag_set(m, PG_REFERENCED);
1817 * Update the vm_page_t clean and reference bits.
1820 #if defined(PMAP_DIAGNOSTIC)
1821 if (pmap_nw_modified((pt_entry_t) tpte)) {
1823 "pmap_remove_all: modified page not writable: va: 0x%x, pte: 0x%x\n",
1827 if (pmap_track_modified(pv->pv_va))
1830 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
1831 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1832 m->md.pv_list_count--;
1833 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem, &info);
1837 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1839 pmap_inval_flush(&info);
1845 * Set the physical protection on the specified range of this map
1848 * This function may not be called from an interrupt if the map is
1849 * not the kernel_pmap.
1852 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
1855 vm_offset_t pdnxt, ptpaddr;
1856 vm_pindex_t sindex, eindex;
1857 pmap_inval_info info;
1862 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1863 pmap_remove(pmap, sva, eva);
1867 if (prot & VM_PROT_WRITE)
1870 pmap_inval_init(&info);
1872 ptbase = get_ptbase(pmap);
1874 sindex = i386_btop(sva);
1875 eindex = i386_btop(eva);
1877 for (; sindex < eindex; sindex = pdnxt) {
1881 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1883 pdirindex = sindex / NPDEPG;
1884 if (((ptpaddr = (unsigned) pmap->pm_pdir[pdirindex]) & PG_PS) != 0) {
1885 pmap_inval_add(&info, pmap, -1);
1886 (unsigned) pmap->pm_pdir[pdirindex] &= ~(PG_M|PG_RW);
1887 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1892 * Weed out invalid mappings. Note: we assume that the page
1893 * directory table is always allocated, and in kernel virtual.
1898 if (pdnxt > eindex) {
1902 for (; sindex != pdnxt; sindex++) {
1907 /* XXX this isn't optimal */
1908 pmap_inval_add(&info, pmap, i386_ptob(sindex));
1909 pbits = ptbase[sindex];
1911 if (pbits & PG_MANAGED) {
1914 m = PHYS_TO_VM_PAGE(pbits);
1915 vm_page_flag_set(m, PG_REFERENCED);
1919 if (pmap_track_modified(i386_ptob(sindex))) {
1921 m = PHYS_TO_VM_PAGE(pbits);
1930 if (pbits != ptbase[sindex]) {
1931 ptbase[sindex] = pbits;
1935 pmap_inval_flush(&info);
1939 * Insert the given physical page (p) at
1940 * the specified virtual address (v) in the
1941 * target physical map with the protection requested.
1943 * If specified, the page will be wired down, meaning
1944 * that the related pte can not be reclaimed.
1946 * NB: This is the only routine which MAY NOT lazy-evaluate
1947 * or lose information. That is, this routine must actually
1948 * insert this page into the given map NOW.
1951 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1957 vm_offset_t origpte, newpte;
1959 pmap_inval_info info;
1965 #ifdef PMAP_DIAGNOSTIC
1966 if (va > VM_MAX_KERNEL_ADDRESS)
1967 panic("pmap_enter: toobig");
1968 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
1969 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%x)", va);
1974 * In the case that a page table page is not
1975 * resident, we are creating it here.
1977 if (va < UPT_MIN_ADDRESS) {
1978 mpte = pmap_allocpte(pmap, va);
1981 pmap_inval_init(&info);
1982 pte = pmap_pte(pmap, va);
1985 * Page Directory table entry not valid, we need a new PT page
1988 panic("pmap_enter: invalid page directory pdir=%x, va=0x%x\n",
1989 (unsigned) pmap->pm_pdir[PTDPTDI], va);
1992 pa = VM_PAGE_TO_PHYS(m) & PG_FRAME;
1993 pmap_inval_add(&info, pmap, va); /* XXX non-optimal */
1994 origpte = *(vm_offset_t *)pte;
1995 opa = origpte & PG_FRAME;
1997 if (origpte & PG_PS)
1998 panic("pmap_enter: attempted pmap_enter on 4MB page");
2001 * Mapping has not changed, must be protection or wiring change.
2003 if (origpte && (opa == pa)) {
2005 * Wiring change, just update stats. We don't worry about
2006 * wiring PT pages as they remain resident as long as there
2007 * are valid mappings in them. Hence, if a user page is wired,
2008 * the PT page will be also.
2010 if (wired && ((origpte & PG_W) == 0))
2011 pmap->pm_stats.wired_count++;
2012 else if (!wired && (origpte & PG_W))
2013 pmap->pm_stats.wired_count--;
2015 #if defined(PMAP_DIAGNOSTIC)
2016 if (pmap_nw_modified((pt_entry_t) origpte)) {
2018 "pmap_enter: modified page not writable: va: 0x%x, pte: 0x%x\n",
2024 * Remove the extra pte reference. Note that we cannot
2025 * optimize the RO->RW case because we have adjusted the
2026 * wiring count above and may need to adjust the wiring
2033 * We might be turning off write access to the page,
2034 * so we go ahead and sense modify status.
2036 if (origpte & PG_MANAGED) {
2037 if ((origpte & PG_M) && pmap_track_modified(va)) {
2039 om = PHYS_TO_VM_PAGE(opa);
2047 * Mapping has changed, invalidate old range and fall through to
2048 * handle validating new mapping.
2052 err = pmap_remove_pte(pmap, pte, va, &info);
2054 panic("pmap_enter: pte vanished, va: 0x%x", va);
2058 * Enter on the PV list if part of our managed memory. Note that we
2059 * raise IPL while manipulating pv_table since pmap_enter can be
2060 * called at interrupt time.
2062 if (pmap_initialized &&
2063 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2064 pmap_insert_entry(pmap, va, mpte, m);
2069 * Increment counters
2071 pmap->pm_stats.resident_count++;
2073 pmap->pm_stats.wired_count++;
2077 * Now validate mapping with desired protection/wiring.
2079 newpte = (vm_offset_t) (pa | pte_prot(pmap, prot) | PG_V);
2083 if (va < UPT_MIN_ADDRESS)
2085 if (pmap == kernel_pmap)
2089 * if the mapping or permission bits are different, we need
2090 * to update the pte.
2092 if ((origpte & ~(PG_M|PG_A)) != newpte) {
2093 *pte = newpte | PG_A;
2095 pmap_inval_flush(&info);
2099 * this code makes some *MAJOR* assumptions:
2100 * 1. Current pmap & pmap exists.
2103 * 4. No page table pages.
2104 * 5. Tlbflush is deferred to calling procedure.
2105 * 6. Page IS managed.
2106 * but is *MUCH* faster than pmap_enter...
2110 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_page_t mpte)
2114 pmap_inval_info info;
2116 pmap_inval_init(&info);
2119 * In the case that a page table page is not
2120 * resident, we are creating it here.
2122 if (va < UPT_MIN_ADDRESS) {
2127 * Calculate pagetable page index
2129 ptepindex = va >> PDRSHIFT;
2130 if (mpte && (mpte->pindex == ptepindex)) {
2135 * Get the page directory entry
2137 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
2140 * If the page table page is mapped, we just increment
2141 * the hold count, and activate it.
2145 panic("pmap_enter_quick: unexpected mapping into 4MB page");
2146 if (pmap->pm_ptphint &&
2147 (pmap->pm_ptphint->pindex == ptepindex)) {
2148 mpte = pmap->pm_ptphint;
2150 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
2151 pmap->pm_ptphint = mpte;
2157 mpte = _pmap_allocpte(pmap, ptepindex);
2165 * This call to vtopte makes the assumption that we are
2166 * entering the page into the current pmap. In order to support
2167 * quick entry into any pmap, one would likely use pmap_pte_quick.
2168 * But that isn't as quick as vtopte.
2170 pte = (unsigned *)vtopte(va);
2173 pmap_unwire_pte_hold(pmap, mpte, &info);
2178 * Enter on the PV list if part of our managed memory. Note that we
2179 * raise IPL while manipulating pv_table since pmap_enter can be
2180 * called at interrupt time.
2182 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0)
2183 pmap_insert_entry(pmap, va, mpte, m);
2186 * Increment counters
2188 pmap->pm_stats.resident_count++;
2190 pa = VM_PAGE_TO_PHYS(m);
2193 * Now validate mapping with RO protection
2195 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2196 *pte = pa | PG_V | PG_U;
2198 *pte = pa | PG_V | PG_U | PG_MANAGED;
2204 * Make a temporary mapping for a physical address. This is only intended
2205 * to be used for panic dumps.
2208 pmap_kenter_temporary(vm_paddr_t pa, int i)
2210 pmap_kenter((vm_offset_t)crashdumpmap + (i * PAGE_SIZE), pa);
2211 return ((void *)crashdumpmap);
2214 #define MAX_INIT_PT (96)
2217 * This routine preloads the ptes for a given object into the specified pmap.
2218 * This eliminates the blast of soft faults on process startup and
2219 * immediately after an mmap.
2222 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2223 vm_object_t object, vm_pindex_t pindex,
2224 vm_size_t size, int limit)
2231 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2236 * XXX you must be joking, entering PTE's into a user page table
2237 * without any accounting? This could result in the page table
2238 * being freed while it still contains mappings (free with PG_ZERO
2239 * assumption leading to a non-zero page being marked PG_ZERO).
2242 * This code maps large physical mmap regions into the
2243 * processor address space. Note that some shortcuts
2244 * are taken, but the code works.
2247 (object->type == OBJT_DEVICE) &&
2248 ((addr & (NBPDR - 1)) == 0) &&
2249 ((size & (NBPDR - 1)) == 0) ) {
2252 unsigned int ptepindex;
2256 if (pmap->pm_pdir[ptepindex = (addr >> PDRSHIFT)])
2260 p = vm_page_lookup(object, pindex);
2261 if (p && vm_page_sleep_busy(p, FALSE, "init4p"))
2265 p = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL);
2270 if (vm_pager_get_pages(object, m, 1, 0) != VM_PAGER_OK) {
2275 p = vm_page_lookup(object, pindex);
2279 ptepa = (vm_offset_t) VM_PAGE_TO_PHYS(p);
2280 if (ptepa & (NBPDR - 1)) {
2284 p->valid = VM_PAGE_BITS_ALL;
2286 pmap->pm_stats.resident_count += size >> PAGE_SHIFT;
2287 npdes = size >> PDRSHIFT;
2288 for (i = 0; i < npdes; i++) {
2289 pmap->pm_pdir[ptepindex] =
2290 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_PS);
2294 vm_page_flag_set(p, PG_MAPPED);
2301 psize = i386_btop(size);
2303 if ((object->type != OBJT_VNODE) ||
2304 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2305 (object->resident_page_count > MAX_INIT_PT))) {
2309 if (psize + pindex > object->size) {
2310 if (object->size < pindex)
2312 psize = object->size - pindex;
2317 * If we are processing a major portion of the object, then scan the
2320 * We cannot safely scan the object's memq unless we are at splvm(),
2321 * since interrupts can remove pages from objects.
2325 if (psize > (object->resident_page_count >> 2)) {
2328 for (p = TAILQ_FIRST(&object->memq);
2329 objpgs > 0 && p != NULL;
2330 p = TAILQ_NEXT(p, listq)
2333 if (tmpidx < pindex)
2336 if (tmpidx >= psize)
2340 * don't allow an madvise to blow away our really
2341 * free pages allocating pv entries.
2343 if ((limit & MAP_PREFAULT_MADVISE) &&
2344 vmstats.v_free_count < vmstats.v_free_reserved) {
2347 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2349 (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2350 if ((p->queue - p->pc) == PQ_CACHE)
2351 vm_page_deactivate(p);
2353 mpte = pmap_enter_quick(pmap,
2354 addr + i386_ptob(tmpidx), p, mpte);
2355 vm_page_flag_set(p, PG_MAPPED);
2362 * else lookup the pages one-by-one.
2364 for (tmpidx = 0; tmpidx < psize; tmpidx += 1) {
2366 * don't allow an madvise to blow away our really
2367 * free pages allocating pv entries.
2369 if ((limit & MAP_PREFAULT_MADVISE) &&
2370 vmstats.v_free_count < vmstats.v_free_reserved) {
2373 p = vm_page_lookup(object, tmpidx + pindex);
2375 ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2377 (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2378 if ((p->queue - p->pc) == PQ_CACHE)
2379 vm_page_deactivate(p);
2381 mpte = pmap_enter_quick(pmap,
2382 addr + i386_ptob(tmpidx), p, mpte);
2383 vm_page_flag_set(p, PG_MAPPED);
2392 * pmap_prefault provides a quick way of clustering pagefaults into a
2393 * processes address space. It is a "cousin" of pmap_object_init_pt,
2394 * except it runs at page fault time instead of mmap time.
2398 #define PAGEORDER_SIZE (PFBAK+PFFOR)
2400 static int pmap_prefault_pageorder[] = {
2401 -PAGE_SIZE, PAGE_SIZE,
2402 -2 * PAGE_SIZE, 2 * PAGE_SIZE,
2403 -3 * PAGE_SIZE, 3 * PAGE_SIZE,
2404 -4 * PAGE_SIZE, 4 * PAGE_SIZE
2408 pmap_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry)
2417 if (!curproc || (pmap != vmspace_pmap(curproc->p_vmspace)))
2420 object = entry->object.vm_object;
2422 starta = addra - PFBAK * PAGE_SIZE;
2423 if (starta < entry->start)
2424 starta = entry->start;
2425 else if (starta > addra)
2429 * splvm() protection is required to maintain the page/object
2430 * association, interrupts can free pages and remove them from
2435 for (i = 0; i < PAGEORDER_SIZE; i++) {
2436 vm_object_t lobject;
2439 addr = addra + pmap_prefault_pageorder[i];
2440 if (addr > addra + (PFFOR * PAGE_SIZE))
2443 if (addr < starta || addr >= entry->end)
2446 if ((*pmap_pde(pmap, addr)) == NULL)
2449 pte = (unsigned *) vtopte(addr);
2453 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2456 for (m = vm_page_lookup(lobject, pindex);
2457 (!m && (lobject->type == OBJT_DEFAULT) &&
2458 (lobject->backing_object));
2459 lobject = lobject->backing_object
2461 if (lobject->backing_object_offset & PAGE_MASK)
2463 pindex += (lobject->backing_object_offset >> PAGE_SHIFT);
2464 m = vm_page_lookup(lobject->backing_object, pindex);
2468 * give-up when a page is not in memory
2473 if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2475 (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2477 if ((m->queue - m->pc) == PQ_CACHE) {
2478 vm_page_deactivate(m);
2481 mpte = pmap_enter_quick(pmap, addr, m, mpte);
2482 vm_page_flag_set(m, PG_MAPPED);
2490 * Routine: pmap_change_wiring
2491 * Function: Change the wiring attribute for a map/virtual-address
2493 * In/out conditions:
2494 * The mapping must already exist in the pmap.
2497 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2504 pte = pmap_pte(pmap, va);
2506 if (wired && !pmap_pte_w(pte))
2507 pmap->pm_stats.wired_count++;
2508 else if (!wired && pmap_pte_w(pte))
2509 pmap->pm_stats.wired_count--;
2512 * Wiring is not a hardware characteristic so there is no need to
2513 * invalidate TLB. However, in an SMP environment we must use
2514 * a locked bus cycle to update the pte (if we are not using
2515 * the pmap_inval_*() API that is)... it's ok to do this for simple
2520 atomic_set_int(pte, PG_W);
2522 atomic_clear_int(pte, PG_W);
2525 atomic_set_int_nonlocked(pte, PG_W);
2527 atomic_clear_int_nonlocked(pte, PG_W);
2534 * Copy the range specified by src_addr/len
2535 * from the source map to the range dst_addr/len
2536 * in the destination map.
2538 * This routine is only advisory and need not do anything.
2541 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2542 vm_size_t len, vm_offset_t src_addr)
2544 pmap_inval_info info;
2546 vm_offset_t end_addr = src_addr + len;
2548 unsigned src_frame, dst_frame;
2551 if (dst_addr != src_addr)
2554 src_frame = ((unsigned) src_pmap->pm_pdir[PTDPTDI]) & PG_FRAME;
2555 if (src_frame != (((unsigned) PTDpde) & PG_FRAME)) {
2559 dst_frame = ((unsigned) dst_pmap->pm_pdir[PTDPTDI]) & PG_FRAME;
2560 if (dst_frame != (((unsigned) APTDpde) & PG_FRAME)) {
2561 APTDpde = (pd_entry_t) (dst_frame | PG_RW | PG_V);
2562 /* The page directory is not shared between CPUs */
2565 pmap_inval_init(&info);
2566 pmap_inval_add(&info, dst_pmap, -1);
2567 pmap_inval_add(&info, src_pmap, -1);
2570 * critical section protection is required to maintain the page/object
2571 * association, interrupts can free pages and remove them from
2575 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
2576 unsigned *src_pte, *dst_pte;
2577 vm_page_t dstmpte, srcmpte;
2578 vm_offset_t srcptepaddr;
2581 if (addr >= UPT_MIN_ADDRESS)
2582 panic("pmap_copy: invalid to pmap_copy page tables\n");
2585 * Don't let optional prefaulting of pages make us go
2586 * way below the low water mark of free pages or way
2587 * above high water mark of used pv entries.
2589 if (vmstats.v_free_count < vmstats.v_free_reserved ||
2590 pv_entry_count > pv_entry_high_water)
2593 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
2594 ptepindex = addr >> PDRSHIFT;
2596 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex];
2597 if (srcptepaddr == 0)
2600 if (srcptepaddr & PG_PS) {
2601 if (dst_pmap->pm_pdir[ptepindex] == 0) {
2602 dst_pmap->pm_pdir[ptepindex] = (pd_entry_t) srcptepaddr;
2603 dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
2608 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
2609 if ((srcmpte == NULL) ||
2610 (srcmpte->hold_count == 0) || (srcmpte->flags & PG_BUSY))
2613 if (pdnxt > end_addr)
2616 src_pte = (unsigned *) vtopte(addr);
2617 dst_pte = (unsigned *) avtopte(addr);
2618 while (addr < pdnxt) {
2622 * we only virtual copy managed pages
2624 if ((ptetemp & PG_MANAGED) != 0) {
2626 * We have to check after allocpte for the
2627 * pte still being around... allocpte can
2630 dstmpte = pmap_allocpte(dst_pmap, addr);
2631 if ((*dst_pte == 0) && (ptetemp = *src_pte)) {
2633 * Clear the modified and
2634 * accessed (referenced) bits
2637 m = PHYS_TO_VM_PAGE(ptetemp);
2638 *dst_pte = ptetemp & ~(PG_M | PG_A);
2639 dst_pmap->pm_stats.resident_count++;
2640 pmap_insert_entry(dst_pmap, addr,
2643 pmap_unwire_pte_hold(dst_pmap, dstmpte, &info);
2645 if (dstmpte->hold_count >= srcmpte->hold_count)
2654 pmap_inval_flush(&info);
2658 * Routine: pmap_kernel
2660 * Returns the physical map handle for the kernel.
2665 return (kernel_pmap);
2671 * Zero the specified PA by mapping the page into KVM and clearing its
2674 * This function may be called from an interrupt and no locking is
2678 pmap_zero_page(vm_paddr_t phys)
2680 struct mdglobaldata *gd = mdcpu;
2683 if (*(int *)gd->gd_CMAP3)
2684 panic("pmap_zero_page: CMAP3 busy");
2685 *(int *)gd->gd_CMAP3 =
2686 PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2687 cpu_invlpg(gd->gd_CADDR3);
2689 #if defined(I686_CPU)
2690 if (cpu_class == CPUCLASS_686)
2691 i686_pagezero(gd->gd_CADDR3);
2694 bzero(gd->gd_CADDR3, PAGE_SIZE);
2695 *(int *) gd->gd_CMAP3 = 0;
2700 * pmap_page_assertzero:
2702 * Assert that a page is empty, panic if it isn't.
2705 pmap_page_assertzero(vm_paddr_t phys)
2707 struct mdglobaldata *gd = mdcpu;
2711 if (*(int *)gd->gd_CMAP3)
2712 panic("pmap_zero_page: CMAP3 busy");
2713 *(int *)gd->gd_CMAP3 =
2714 PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2715 cpu_invlpg(gd->gd_CADDR3);
2716 for (i = 0; i < PAGE_SIZE; i += 4) {
2717 if (*(int *)((char *)gd->gd_CADDR3 + i) != 0) {
2718 panic("pmap_page_assertzero() @ %p not zero!\n",
2719 (void *)gd->gd_CADDR3);
2722 *(int *) gd->gd_CMAP3 = 0;
2729 * Zero part of a physical page by mapping it into memory and clearing
2730 * its contents with bzero.
2732 * off and size may not cover an area beyond a single hardware page.
2735 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
2737 struct mdglobaldata *gd = mdcpu;
2740 if (*(int *) gd->gd_CMAP3)
2741 panic("pmap_zero_page: CMAP3 busy");
2742 *(int *) gd->gd_CMAP3 = PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2743 cpu_invlpg(gd->gd_CADDR3);
2745 #if defined(I686_CPU)
2746 if (cpu_class == CPUCLASS_686 && off == 0 && size == PAGE_SIZE)
2747 i686_pagezero(gd->gd_CADDR3);
2750 bzero((char *)gd->gd_CADDR3 + off, size);
2751 *(int *) gd->gd_CMAP3 = 0;
2758 * Copy the physical page from the source PA to the target PA.
2759 * This function may be called from an interrupt. No locking
2763 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
2765 struct mdglobaldata *gd = mdcpu;
2768 if (*(int *) gd->gd_CMAP1)
2769 panic("pmap_copy_page: CMAP1 busy");
2770 if (*(int *) gd->gd_CMAP2)
2771 panic("pmap_copy_page: CMAP2 busy");
2773 *(int *) gd->gd_CMAP1 = PG_V | (src & PG_FRAME) | PG_A;
2774 *(int *) gd->gd_CMAP2 = PG_V | PG_RW | (dst & PG_FRAME) | PG_A | PG_M;
2776 cpu_invlpg(gd->gd_CADDR1);
2777 cpu_invlpg(gd->gd_CADDR2);
2779 bcopy(gd->gd_CADDR1, gd->gd_CADDR2, PAGE_SIZE);
2781 *(int *) gd->gd_CMAP1 = 0;
2782 *(int *) gd->gd_CMAP2 = 0;
2787 * pmap_copy_page_frag:
2789 * Copy the physical page from the source PA to the target PA.
2790 * This function may be called from an interrupt. No locking
2794 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
2796 struct mdglobaldata *gd = mdcpu;
2799 if (*(int *) gd->gd_CMAP1)
2800 panic("pmap_copy_page: CMAP1 busy");
2801 if (*(int *) gd->gd_CMAP2)
2802 panic("pmap_copy_page: CMAP2 busy");
2804 *(int *) gd->gd_CMAP1 = PG_V | (src & PG_FRAME) | PG_A;
2805 *(int *) gd->gd_CMAP2 = PG_V | PG_RW | (dst & PG_FRAME) | PG_A | PG_M;
2807 cpu_invlpg(gd->gd_CADDR1);
2808 cpu_invlpg(gd->gd_CADDR2);
2810 bcopy((char *)gd->gd_CADDR1 + (src & PAGE_MASK),
2811 (char *)gd->gd_CADDR2 + (dst & PAGE_MASK),
2814 *(int *) gd->gd_CMAP1 = 0;
2815 *(int *) gd->gd_CMAP2 = 0;
2820 * Returns true if the pmap's pv is one of the first
2821 * 16 pvs linked to from this page. This count may
2822 * be changed upwards or downwards in the future; it
2823 * is only necessary that true be returned for a small
2824 * subset of pmaps for proper page aging.
2827 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2833 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2838 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2839 if (pv->pv_pmap == pmap) {
2851 #define PMAP_REMOVE_PAGES_CURPROC_ONLY
2853 * Remove all pages from specified address space
2854 * this aids process exit speeds. Also, this code
2855 * is special cased for current process only, but
2856 * can have the more generic (and slightly slower)
2857 * mode enabled. This is much faster than pmap_remove
2858 * in the case of running down an entire address space.
2861 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2863 unsigned *pte, tpte;
2867 pmap_inval_info info;
2869 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2870 if (!curproc || (pmap != vmspace_pmap(curproc->p_vmspace))) {
2871 printf("warning: pmap_remove_pages called with non-current pmap\n");
2876 pmap_inval_init(&info);
2878 for(pv = TAILQ_FIRST(&pmap->pm_pvlist);
2882 if (pv->pv_va >= eva || pv->pv_va < sva) {
2883 npv = TAILQ_NEXT(pv, pv_plist);
2887 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2888 pte = (unsigned *)vtopte(pv->pv_va);
2890 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2892 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
2896 * We cannot remove wired pages from a process' mapping at this time
2899 npv = TAILQ_NEXT(pv, pv_plist);
2904 m = PHYS_TO_VM_PAGE(tpte);
2906 KASSERT(m < &vm_page_array[vm_page_array_size],
2907 ("pmap_remove_pages: bad tpte %x", tpte));
2909 pv->pv_pmap->pm_stats.resident_count--;
2912 * Update the vm_page_t clean and reference bits.
2919 npv = TAILQ_NEXT(pv, pv_plist);
2920 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2922 m->md.pv_list_count--;
2923 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2924 if (TAILQ_FIRST(&m->md.pv_list) == NULL) {
2925 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2928 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem, &info);
2931 pmap_inval_flush(&info);
2936 * pmap_testbit tests bits in pte's
2937 * note that the testbit/changebit routines are inline,
2938 * and a lot of things compile-time evaluate.
2941 pmap_testbit(vm_page_t m, int bit)
2947 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2950 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2955 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2957 * if the bit being tested is the modified bit, then
2958 * mark clean_map and ptes as never
2961 if (bit & (PG_A|PG_M)) {
2962 if (!pmap_track_modified(pv->pv_va))
2966 #if defined(PMAP_DIAGNOSTIC)
2968 printf("Null pmap (tb) at va: 0x%x\n", pv->pv_va);
2972 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2983 * this routine is used to modify bits in ptes
2985 static __inline void
2986 pmap_changebit(vm_page_t m, int bit, boolean_t setem)
2988 struct pmap_inval_info info;
2993 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2996 pmap_inval_init(&info);
3000 * Loop over all current mappings setting/clearing as appropos If
3001 * setting RO do we need to clear the VAC?
3003 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3005 * don't write protect pager mappings
3007 if (!setem && (bit == PG_RW)) {
3008 if (!pmap_track_modified(pv->pv_va))
3012 #if defined(PMAP_DIAGNOSTIC)
3014 printf("Null pmap (cb) at va: 0x%x\n", pv->pv_va);
3020 * Careful here. We can use a locked bus instruction to
3021 * clear PG_A or PG_M safely but we need to synchronize
3022 * with the target cpus when we mess with PG_RW.
3024 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3026 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
3030 atomic_set_int(pte, bit);
3032 atomic_set_int_nonlocked(pte, bit);
3035 vm_offset_t pbits = *(vm_offset_t *)pte;
3042 atomic_clear_int(pte, PG_M|PG_RW);
3044 atomic_clear_int_nonlocked(pte, PG_M|PG_RW);
3048 atomic_clear_int(pte, bit);
3050 atomic_clear_int_nonlocked(pte, bit);
3056 pmap_inval_flush(&info);
3061 * pmap_page_protect:
3063 * Lower the permission for all mappings to a given page.
3066 pmap_page_protect(vm_page_t m, vm_prot_t prot)
3068 if ((prot & VM_PROT_WRITE) == 0) {
3069 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
3070 pmap_changebit(m, PG_RW, FALSE);
3078 pmap_phys_address(int ppn)
3080 return (i386_ptob(ppn));
3084 * pmap_ts_referenced:
3086 * Return a count of reference bits for a page, clearing those bits.
3087 * It is not necessary for every reference bit to be cleared, but it
3088 * is necessary that 0 only be returned when there are truly no
3089 * reference bits set.
3091 * XXX: The exact number of bits to check and clear is a matter that
3092 * should be tested and standardized at some point in the future for
3093 * optimal aging of shared pages.
3096 pmap_ts_referenced(vm_page_t m)
3098 pv_entry_t pv, pvf, pvn;
3103 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3108 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3113 pvn = TAILQ_NEXT(pv, pv_list);
3115 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3117 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3119 if (!pmap_track_modified(pv->pv_va))
3122 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3124 if (pte && (*pte & PG_A)) {
3126 atomic_clear_int(pte, PG_A);
3128 atomic_clear_int_nonlocked(pte, PG_A);
3135 } while ((pv = pvn) != NULL && pv != pvf);
3145 * Return whether or not the specified physical page was modified
3146 * in any physical maps.
3149 pmap_is_modified(vm_page_t m)
3151 return pmap_testbit(m, PG_M);
3155 * Clear the modify bits on the specified physical page.
3158 pmap_clear_modify(vm_page_t m)
3160 pmap_changebit(m, PG_M, FALSE);
3164 * pmap_clear_reference:
3166 * Clear the reference bit on the specified physical page.
3169 pmap_clear_reference(vm_page_t m)
3171 pmap_changebit(m, PG_A, FALSE);
3175 * Miscellaneous support routines follow
3179 i386_protection_init(void)
3183 kp = protection_codes;
3184 for (prot = 0; prot < 8; prot++) {
3186 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE:
3188 * Read access is also 0. There isn't any execute bit,
3189 * so just make it readable.
3191 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE:
3192 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE:
3193 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE:
3196 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE:
3197 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE:
3198 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE:
3199 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
3207 * Map a set of physical memory pages into the kernel virtual
3208 * address space. Return a pointer to where it is mapped. This
3209 * routine is intended to be used for mapping device memory,
3212 * NOTE: we can't use pgeflag unless we invalidate the pages one at
3216 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
3218 vm_offset_t va, tmpva, offset;
3221 offset = pa & PAGE_MASK;
3222 size = roundup(offset + size, PAGE_SIZE);
3224 va = kmem_alloc_nofault(kernel_map, size);
3226 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3229 for (tmpva = va; size > 0;) {
3230 pte = (unsigned *)vtopte(tmpva);
3231 *pte = pa | PG_RW | PG_V; /* | pgeflag; */
3239 return ((void *)(va + offset));
3243 pmap_unmapdev(vm_offset_t va, vm_size_t size)
3245 vm_offset_t base, offset;
3247 base = va & PG_FRAME;
3248 offset = va & PAGE_MASK;
3249 size = roundup(offset + size, PAGE_SIZE);
3250 pmap_qremove(va, size >> PAGE_SHIFT);
3251 kmem_free(kernel_map, base, size);
3255 * perform the pmap work for mincore
3258 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3260 unsigned *ptep, pte;
3264 ptep = pmap_pte(pmap, addr);
3269 if ((pte = *ptep) != 0) {
3272 val = MINCORE_INCORE;
3273 if ((pte & PG_MANAGED) == 0)
3276 pa = pte & PG_FRAME;
3278 m = PHYS_TO_VM_PAGE(pa);
3284 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3286 * Modified by someone
3288 else if (m->dirty || pmap_is_modified(m))
3289 val |= MINCORE_MODIFIED_OTHER;
3294 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3297 * Referenced by someone
3299 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3300 val |= MINCORE_REFERENCED_OTHER;
3301 vm_page_flag_set(m, PG_REFERENCED);
3308 pmap_activate(struct proc *p)
3312 pmap = vmspace_pmap(p->p_vmspace);
3314 atomic_set_int(&pmap->pm_active, 1 << mycpu->gd_cpuid);
3316 pmap->pm_active |= 1;
3318 #if defined(SWTCH_OPTIM_STATS)
3321 p->p_thread->td_pcb->pcb_cr3 = vtophys(pmap->pm_pdir);
3322 load_cr3(p->p_thread->td_pcb->pcb_cr3);
3326 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3329 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3333 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3338 #if defined(PMAP_DEBUG)
3340 pmap_pid_dump(int pid)
3346 FOREACH_PROC_IN_SYSTEM(p) {
3347 if (p->p_pid != pid)
3353 pmap = vmspace_pmap(p->p_vmspace);
3354 for(i=0;i<1024;i++) {
3357 unsigned base = i << PDRSHIFT;
3359 pde = &pmap->pm_pdir[i];
3360 if (pde && pmap_pde_v(pde)) {
3361 for(j=0;j<1024;j++) {
3362 unsigned va = base + (j << PAGE_SHIFT);
3363 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) {
3370 pte = pmap_pte_quick( pmap, va);
3371 if (pte && pmap_pte_v(pte)) {
3375 m = PHYS_TO_VM_PAGE(pa);
3376 printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x",
3377 va, pa, m->hold_count, m->wire_count, m->flags);
3398 static void pads (pmap_t pm);
3399 void pmap_pvdump (vm_paddr_t pa);
3401 /* print address space of pmap*/
3408 if (pm == kernel_pmap)
3410 for (i = 0; i < 1024; i++)
3412 for (j = 0; j < 1024; j++) {
3413 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
3414 if (pm == kernel_pmap && va < KERNBASE)
3416 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS)
3418 ptep = pmap_pte_quick(pm, va);
3419 if (pmap_pte_v(ptep))
3420 printf("%x:%x ", va, *(int *) ptep);
3426 pmap_pvdump(vm_paddr_t pa)
3431 printf("pa %08llx", (long long)pa);
3432 m = PHYS_TO_VM_PAGE(pa);
3433 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3435 printf(" -> pmap %p, va %x, flags %x",
3436 (void *)pv->pv_pmap, pv->pv_va, pv->pv_flags);
3438 printf(" -> pmap %p, va %x", (void *)pv->pv_pmap, pv->pv_va);