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.65 2006/12/28 18:29:04 dillon Exp $
47 * Manages physical address maps.
49 * In addition to hardware address maps, this
50 * module is called upon to provide software-use-only
51 * maps which may or may not be stored in the same
52 * form as hardware maps. These pseudo-maps are
53 * used to store intermediate results from copy
54 * operations to and from address spaces.
56 * Since the information managed by this module is
57 * also stored by the logical address mapping module,
58 * this module may throw away valid virtual-to-physical
59 * mappings at almost any time. However, invalidations
60 * of virtual-to-physical mappings must be done as
63 * In order to cope with hardware architectures which
64 * make virtual-to-physical map invalidates expensive,
65 * this module may delay invalidate or reduced protection
66 * operations until such time as they are actually
67 * necessary. This module is given full information as
68 * to which processors are currently using which maps,
69 * and to when physical maps must be made correct.
72 #include "opt_disable_pse.h"
74 #include "opt_msgbuf.h"
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
80 #include <sys/msgbuf.h>
81 #include <sys/vmmeter.h>
85 #include <vm/vm_param.h>
86 #include <sys/sysctl.h>
88 #include <vm/vm_kern.h>
89 #include <vm/vm_page.h>
90 #include <vm/vm_map.h>
91 #include <vm/vm_object.h>
92 #include <vm/vm_extern.h>
93 #include <vm/vm_pageout.h>
94 #include <vm/vm_pager.h>
95 #include <vm/vm_zone.h>
98 #include <sys/thread2.h>
100 #include <machine/cputypes.h>
101 #include <machine/md_var.h>
102 #include <machine/specialreg.h>
103 #include <machine/smp.h>
104 #include <machine_base/apic/apicreg.h>
105 #include <machine/globaldata.h>
106 #include <machine/pmap.h>
107 #include <machine/pmap_inval.h>
109 #define PMAP_KEEP_PDIRS
110 #ifndef PMAP_SHPGPERPROC
111 #define PMAP_SHPGPERPROC 200
114 #if defined(DIAGNOSTIC)
115 #define PMAP_DIAGNOSTIC
120 #if !defined(PMAP_DIAGNOSTIC)
121 #define PMAP_INLINE __inline
127 * Get PDEs and PTEs for user/kernel address space
129 #define pmap_pde(m, v) (&((m)->pm_pdir[(vm_offset_t)(v) >> PDRSHIFT]))
130 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
132 #define pmap_pde_v(pte) ((*(int *)pte & PG_V) != 0)
133 #define pmap_pte_w(pte) ((*(int *)pte & PG_W) != 0)
134 #define pmap_pte_m(pte) ((*(int *)pte & PG_M) != 0)
135 #define pmap_pte_u(pte) ((*(int *)pte & PG_A) != 0)
136 #define pmap_pte_v(pte) ((*(int *)pte & PG_V) != 0)
140 * Given a map and a machine independent protection code,
141 * convert to a vax protection code.
143 #define pte_prot(m, p) \
144 (protection_codes[p & (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)])
145 static int protection_codes[8];
147 static struct pmap kernel_pmap_store;
148 static TAILQ_HEAD(,pmap) pmap_list = TAILQ_HEAD_INITIALIZER(pmap_list);
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_start; /* VA of first avail page (after kernel bss) */
155 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
156 vm_offset_t KvaStart; /* VA start of KVA space */
157 vm_offset_t KvaEnd; /* VA end of KVA space (non-inclusive) */
158 vm_offset_t KvaSize; /* max size of kernel virtual address space */
159 static boolean_t pmap_initialized = FALSE; /* Has pmap_init completed? */
160 static int pgeflag; /* PG_G or-in */
161 static int pseflag; /* PG_PS or-in */
163 static vm_object_t kptobj;
166 vm_offset_t kernel_vm_end;
169 * Data for the pv entry allocation mechanism
171 static vm_zone_t pvzone;
172 static struct vm_zone pvzone_store;
173 static struct vm_object pvzone_obj;
174 static int pv_entry_count=0, pv_entry_max=0, pv_entry_high_water=0;
175 static int pmap_pagedaemon_waken = 0;
176 static struct pv_entry *pvinit;
179 * All those kernel PT submaps that BSD is so fond of
181 pt_entry_t *CMAP1 = 0, *ptmmap;
182 caddr_t CADDR1 = 0, ptvmmap = 0;
183 static pt_entry_t *msgbufmap;
184 struct msgbuf *msgbufp=0;
189 static pt_entry_t *pt_crashdumpmap;
190 static caddr_t crashdumpmap;
192 extern pt_entry_t *SMPpt;
194 static PMAP_INLINE void free_pv_entry (pv_entry_t pv);
195 static unsigned * get_ptbase (pmap_t pmap);
196 static pv_entry_t get_pv_entry (void);
197 static void i386_protection_init (void);
198 static __inline void pmap_changebit (vm_page_t m, int bit, boolean_t setem);
200 static void pmap_remove_all (vm_page_t m);
201 static vm_page_t pmap_enter_quick (pmap_t pmap, vm_offset_t va,
202 vm_page_t m, vm_page_t mpte);
203 static int pmap_remove_pte (struct pmap *pmap, unsigned *ptq,
204 vm_offset_t sva, pmap_inval_info_t info);
205 static void pmap_remove_page (struct pmap *pmap,
206 vm_offset_t va, pmap_inval_info_t info);
207 static int pmap_remove_entry (struct pmap *pmap, vm_page_t m,
208 vm_offset_t va, pmap_inval_info_t info);
209 static boolean_t pmap_testbit (vm_page_t m, int bit);
210 static void pmap_insert_entry (pmap_t pmap, vm_offset_t va,
211 vm_page_t mpte, vm_page_t m);
213 static vm_page_t pmap_allocpte (pmap_t pmap, vm_offset_t va);
215 static int pmap_release_free_page (pmap_t pmap, vm_page_t p);
216 static vm_page_t _pmap_allocpte (pmap_t pmap, unsigned ptepindex);
217 static unsigned * pmap_pte_quick (pmap_t pmap, vm_offset_t va);
218 static vm_page_t pmap_page_lookup (vm_object_t object, vm_pindex_t pindex);
219 static int pmap_unuse_pt (pmap_t, vm_offset_t, vm_page_t, pmap_inval_info_t);
220 static vm_offset_t pmap_kmem_choose(vm_offset_t addr);
222 static unsigned pdir4mb;
225 * Move the kernel virtual free pointer to the next
226 * 4MB. This is used to help improve performance
227 * by using a large (4MB) page for much of the kernel
228 * (.text, .data, .bss)
231 pmap_kmem_choose(vm_offset_t addr)
233 vm_offset_t newaddr = addr;
235 if (cpu_feature & CPUID_PSE) {
236 newaddr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
245 * Extract the page table entry associated with the given map/virtual
248 * This function may NOT be called from an interrupt.
250 PMAP_INLINE unsigned *
251 pmap_pte(pmap_t pmap, vm_offset_t va)
256 pdeaddr = (unsigned *) pmap_pde(pmap, va);
257 if (*pdeaddr & PG_PS)
260 return get_ptbase(pmap) + i386_btop(va);
269 * Super fast pmap_pte routine best used when scanning the pv lists.
270 * This eliminates many course-grained invltlb calls. Note that many of
271 * the pv list scans are across different pmaps and it is very wasteful
272 * to do an entire invltlb when checking a single mapping.
274 * Should only be called while in a critical section.
277 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
279 struct mdglobaldata *gd = mdcpu;
282 if ((pde = (unsigned) pmap->pm_pdir[va >> PDRSHIFT]) != 0) {
283 unsigned frame = (unsigned) pmap->pm_pdir[PTDPTDI] & PG_FRAME;
284 unsigned index = i386_btop(va);
285 /* are we current address space or kernel? */
286 if ((pmap == kernel_pmap) ||
287 (frame == (((unsigned) PTDpde) & PG_FRAME))) {
288 return (unsigned *) PTmap + index;
290 newpf = pde & PG_FRAME;
291 if ( ((* (unsigned *) gd->gd_PMAP1) & PG_FRAME) != newpf) {
292 * (unsigned *) gd->gd_PMAP1 = newpf | PG_RW | PG_V;
293 cpu_invlpg(gd->gd_PADDR1);
295 return gd->gd_PADDR1 + ((unsigned) index & (NPTEPG - 1));
302 * Bootstrap the system enough to run with virtual memory.
304 * On the i386 this is called after mapping has already been enabled
305 * and just syncs the pmap module with what has already been done.
306 * [We can't call it easily with mapping off since the kernel is not
307 * mapped with PA == VA, hence we would have to relocate every address
308 * from the linked base (virtual) address "KERNBASE" to the actual
309 * (physical) address starting relative to 0]
312 pmap_bootstrap(vm_paddr_t firstaddr, vm_paddr_t loadaddr)
316 struct mdglobaldata *gd;
320 KvaStart = (vm_offset_t)VADDR(PTDPTDI, 0);
321 KvaSize = (vm_offset_t)VADDR(APTDPTDI, 0) - KvaStart;
322 KvaEnd = KvaStart + KvaSize;
324 avail_start = firstaddr;
327 * XXX The calculation of virtual_start is wrong. It's NKPT*PAGE_SIZE
328 * too large. It should instead be correctly calculated in locore.s and
329 * not based on 'first' (which is a physical address, not a virtual
330 * address, for the start of unused physical memory). The kernel
331 * page tables are NOT double mapped and thus should not be included
332 * in this calculation.
334 virtual_start = (vm_offset_t) KERNBASE + firstaddr;
335 virtual_start = pmap_kmem_choose(virtual_start);
336 virtual_end = VADDR(KPTDI+NKPDE-1, NPTEPG-1);
339 * Initialize protection array.
341 i386_protection_init();
344 * The kernel's pmap is statically allocated so we don't have to use
345 * pmap_create, which is unlikely to work correctly at this part of
346 * the boot sequence (XXX and which no longer exists).
348 kernel_pmap = &kernel_pmap_store;
350 kernel_pmap->pm_pdir = (pd_entry_t *)(KERNBASE + (u_int)IdlePTD);
351 kernel_pmap->pm_count = 1;
352 kernel_pmap->pm_active = (cpumask_t)-1; /* don't allow deactivation */
353 TAILQ_INIT(&kernel_pmap->pm_pvlist);
357 * Reserve some special page table entries/VA space for temporary
360 #define SYSMAP(c, p, v, n) \
361 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
364 pte = (pt_entry_t *) pmap_pte(kernel_pmap, va);
367 * CMAP1/CMAP2 are used for zeroing and copying pages.
369 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
374 SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS);
377 * ptvmmap is used for reading arbitrary physical pages via
380 SYSMAP(caddr_t, ptmmap, ptvmmap, 1)
383 * msgbufp is used to map the system message buffer.
384 * XXX msgbufmap is not used.
386 SYSMAP(struct msgbuf *, msgbufmap, msgbufp,
387 atop(round_page(MSGBUF_SIZE)))
392 for (i = 0; i < NKPT; i++)
396 * PG_G is terribly broken on SMP because we IPI invltlb's in some
397 * cases rather then invl1pg. Actually, I don't even know why it
398 * works under UP because self-referential page table mappings
403 if (cpu_feature & CPUID_PGE)
408 * Initialize the 4MB page size flag
412 * The 4MB page version of the initial
413 * kernel page mapping.
417 #if !defined(DISABLE_PSE)
418 if (cpu_feature & CPUID_PSE) {
421 * Note that we have enabled PSE mode
424 ptditmp = *((unsigned *)PTmap + i386_btop(KERNBASE));
425 ptditmp &= ~(NBPDR - 1);
426 ptditmp |= PG_V | PG_RW | PG_PS | PG_U | pgeflag;
431 * Enable the PSE mode. If we are SMP we can't do this
432 * now because the APs will not be able to use it when
435 load_cr4(rcr4() | CR4_PSE);
438 * We can do the mapping here for the single processor
439 * case. We simply ignore the old page table page from
443 * For SMP, we still need 4K pages to bootstrap APs,
444 * PSE will be enabled as soon as all APs are up.
446 PTD[KPTDI] = (pd_entry_t)ptditmp;
447 kernel_pmap->pm_pdir[KPTDI] = (pd_entry_t)ptditmp;
453 if (cpu_apic_address == 0)
454 panic("pmap_bootstrap: no local apic!");
456 /* local apic is mapped on last page */
457 SMPpt[NPTEPG - 1] = (pt_entry_t)(PG_V | PG_RW | PG_N | pgeflag |
458 (cpu_apic_address & PG_FRAME));
462 * We need to finish setting up the globaldata page for the BSP.
463 * locore has already populated the page table for the mdglobaldata
466 pg = MDGLOBALDATA_BASEALLOC_PAGES;
467 gd = &CPU_prvspace[0].mdglobaldata;
468 gd->gd_CMAP1 = &SMPpt[pg + 0];
469 gd->gd_CMAP2 = &SMPpt[pg + 1];
470 gd->gd_CMAP3 = &SMPpt[pg + 2];
471 gd->gd_PMAP1 = &SMPpt[pg + 3];
472 gd->gd_CADDR1 = CPU_prvspace[0].CPAGE1;
473 gd->gd_CADDR2 = CPU_prvspace[0].CPAGE2;
474 gd->gd_CADDR3 = CPU_prvspace[0].CPAGE3;
475 gd->gd_PADDR1 = (unsigned *)CPU_prvspace[0].PPAGE1;
482 * Set 4mb pdir for mp startup
487 if (pseflag && (cpu_feature & CPUID_PSE)) {
488 load_cr4(rcr4() | CR4_PSE);
489 if (pdir4mb && mycpu->gd_cpuid == 0) { /* only on BSP */
490 kernel_pmap->pm_pdir[KPTDI] =
491 PTD[KPTDI] = (pd_entry_t)pdir4mb;
499 * Initialize the pmap module.
500 * Called by vm_init, to initialize any structures that the pmap
501 * system needs to map virtual memory.
502 * pmap_init has been enhanced to support in a fairly consistant
503 * way, discontiguous physical memory.
512 * object for kernel page table pages
514 kptobj = vm_object_allocate(OBJT_DEFAULT, NKPDE);
517 * Allocate memory for random pmap data structures. Includes the
521 for(i = 0; i < vm_page_array_size; i++) {
524 m = &vm_page_array[i];
525 TAILQ_INIT(&m->md.pv_list);
526 m->md.pv_list_count = 0;
530 * init the pv free list
532 initial_pvs = vm_page_array_size;
533 if (initial_pvs < MINPV)
535 pvzone = &pvzone_store;
536 pvinit = (struct pv_entry *) kmem_alloc(kernel_map,
537 initial_pvs * sizeof (struct pv_entry));
538 zbootinit(pvzone, "PV ENTRY", sizeof (struct pv_entry), pvinit,
542 * Now it is safe to enable pv_table recording.
544 pmap_initialized = TRUE;
548 * Initialize the address space (zone) for the pv_entries. Set a
549 * high water mark so that the system can recover from excessive
550 * numbers of pv entries.
555 int shpgperproc = PMAP_SHPGPERPROC;
557 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
558 pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
559 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
560 pv_entry_high_water = 9 * (pv_entry_max / 10);
561 zinitna(pvzone, &pvzone_obj, NULL, 0, pv_entry_max, ZONE_INTERRUPT, 1);
565 /***************************************************
566 * Low level helper routines.....
567 ***************************************************/
569 #if defined(PMAP_DIAGNOSTIC)
572 * This code checks for non-writeable/modified pages.
573 * This should be an invalid condition.
576 pmap_nw_modified(pt_entry_t ptea)
582 if ((pte & (PG_M|PG_RW)) == PG_M)
591 * this routine defines the region(s) of memory that should
592 * not be tested for the modified bit.
594 static PMAP_INLINE int
595 pmap_track_modified(vm_offset_t va)
597 if ((va < clean_sva) || (va >= clean_eva))
604 get_ptbase(pmap_t pmap)
606 unsigned frame = (unsigned) pmap->pm_pdir[PTDPTDI] & PG_FRAME;
607 struct globaldata *gd = mycpu;
609 /* are we current address space or kernel? */
610 if (pmap == kernel_pmap || frame == (((unsigned) PTDpde) & PG_FRAME)) {
611 return (unsigned *) PTmap;
614 /* otherwise, we are alternate address space */
615 KKASSERT(gd->gd_intr_nesting_level == 0 && (gd->gd_curthread->td_flags & TDF_INTTHREAD) == 0);
617 if (frame != (((unsigned) APTDpde) & PG_FRAME)) {
618 APTDpde = (pd_entry_t)(frame | PG_RW | PG_V);
619 /* The page directory is not shared between CPUs */
622 return (unsigned *) APTmap;
628 * Extract the physical page address associated with the map/VA pair.
630 * This function may not be called from an interrupt if the pmap is
634 pmap_extract(pmap_t pmap, vm_offset_t va)
637 vm_offset_t pdirindex;
639 pdirindex = va >> PDRSHIFT;
640 if (pmap && (rtval = (unsigned) pmap->pm_pdir[pdirindex])) {
642 if ((rtval & PG_PS) != 0) {
643 rtval &= ~(NBPDR - 1);
644 rtval |= va & (NBPDR - 1);
647 pte = get_ptbase(pmap) + i386_btop(va);
648 rtval = ((*pte & PG_FRAME) | (va & PAGE_MASK));
655 * Extract user accessible page only, return NULL if the page is not
656 * present or if it's current state is not sufficient. Caller will
657 * generally call vm_fault() on failure and try again.
660 pmap_extract_vmpage(pmap_t pmap, vm_offset_t va, int prot)
663 vm_offset_t pdirindex;
665 pdirindex = va >> PDRSHIFT;
666 if (pmap && (rtval = (unsigned) pmap->pm_pdir[pdirindex])) {
670 if ((rtval & PG_PS) != 0) {
671 if ((rtval & (PG_V|PG_U)) != (PG_V|PG_U))
673 if ((prot & VM_PROT_WRITE) && (rtval & PG_RW) == 0)
675 rtval &= ~(NBPDR - 1);
676 rtval |= va & (NBPDR - 1);
677 m = PHYS_TO_VM_PAGE(rtval);
679 pte = get_ptbase(pmap) + i386_btop(va);
680 if ((*pte & (PG_V|PG_U)) != (PG_V|PG_U))
682 if ((prot & VM_PROT_WRITE) && (*pte & PG_RW) == 0)
684 rtval = ((*pte & PG_FRAME) | (va & PAGE_MASK));
685 m = PHYS_TO_VM_PAGE(rtval);
692 /***************************************************
693 * Low level mapping routines.....
694 ***************************************************/
697 * Routine: pmap_kenter
699 * Add a wired page to the KVA
700 * NOTE! note that in order for the mapping to take effect -- you
701 * should do an invltlb after doing the pmap_kenter().
704 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
708 pmap_inval_info info;
710 pmap_inval_init(&info);
711 pmap_inval_add(&info, kernel_pmap, va);
712 npte = pa | PG_RW | PG_V | pgeflag;
713 pte = (unsigned *)vtopte(va);
715 pmap_inval_flush(&info);
719 * Routine: pmap_kenter_quick
721 * Similar to pmap_kenter(), except we only invalidate the
722 * mapping on the current CPU.
725 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
730 npte = pa | PG_RW | PG_V | pgeflag;
731 pte = (unsigned *)vtopte(va);
733 cpu_invlpg((void *)va);
737 pmap_kenter_sync(vm_offset_t va)
739 pmap_inval_info info;
741 pmap_inval_init(&info);
742 pmap_inval_add(&info, kernel_pmap, va);
743 pmap_inval_flush(&info);
747 pmap_kenter_sync_quick(vm_offset_t va)
749 cpu_invlpg((void *)va);
753 * remove a page from the kernel pagetables
756 pmap_kremove(vm_offset_t va)
759 pmap_inval_info info;
761 pmap_inval_init(&info);
762 pmap_inval_add(&info, kernel_pmap, va);
763 pte = (unsigned *)vtopte(va);
765 pmap_inval_flush(&info);
769 pmap_kremove_quick(vm_offset_t va)
772 pte = (unsigned *)vtopte(va);
774 cpu_invlpg((void *)va);
778 * Used to map a range of physical addresses into kernel
779 * virtual address space.
781 * For now, VM is already on, we only need to map the
785 pmap_map(vm_offset_t virt, vm_paddr_t start, vm_paddr_t end, int prot)
787 while (start < end) {
788 pmap_kenter(virt, start);
797 * Add a list of wired pages to the kva
798 * this routine is only used for temporary
799 * kernel mappings that do not need to have
800 * page modification or references recorded.
801 * Note that old mappings are simply written
802 * over. The page *must* be wired.
805 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
809 end_va = va + count * PAGE_SIZE;
811 while (va < end_va) {
814 pte = (unsigned *)vtopte(va);
815 *pte = VM_PAGE_TO_PHYS(*m) | PG_RW | PG_V | pgeflag;
816 cpu_invlpg((void *)va);
821 smp_invltlb(); /* XXX */
826 pmap_qenter2(vm_offset_t va, vm_page_t *m, int count, cpumask_t *mask)
829 cpumask_t cmask = mycpu->gd_cpumask;
831 end_va = va + count * PAGE_SIZE;
833 while (va < end_va) {
838 * Install the new PTE. If the pte changed from the prior
839 * mapping we must reset the cpu mask and invalidate the page.
840 * If the pte is the same but we have not seen it on the
841 * current cpu, invlpg the existing mapping. Otherwise the
842 * entry is optimal and no invalidation is required.
844 pte = (unsigned *)vtopte(va);
845 pteval = VM_PAGE_TO_PHYS(*m) | PG_A | PG_RW | PG_V | pgeflag;
846 if (*pte != pteval) {
849 cpu_invlpg((void *)va);
850 } else if ((*mask & cmask) == 0) {
851 cpu_invlpg((void *)va);
860 * this routine jerks page mappings from the
861 * kernel -- it is meant only for temporary mappings.
864 pmap_qremove(vm_offset_t va, int count)
868 end_va = va + count*PAGE_SIZE;
870 while (va < end_va) {
873 pte = (unsigned *)vtopte(va);
875 cpu_invlpg((void *)va);
884 * This routine works like vm_page_lookup() but also blocks as long as the
885 * page is busy. This routine does not busy the page it returns.
887 * Unless the caller is managing objects whos pages are in a known state,
888 * the call should be made with a critical section held so the page's object
889 * association remains valid on return.
892 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
897 m = vm_page_lookup(object, pindex);
898 if (m && vm_page_sleep_busy(m, FALSE, "pplookp"))
904 * Create a new thread and optionally associate it with a (new) process.
905 * NOTE! the new thread's cpu may not equal the current cpu.
908 pmap_init_thread(thread_t td)
910 /* enforce pcb placement */
911 td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
912 td->td_savefpu = &td->td_pcb->pcb_save;
913 td->td_sp = (char *)td->td_pcb - 16;
917 * Create the UPAGES for a new process.
918 * This routine directly affects the fork perf for a process.
921 pmap_init_proc(struct proc *p, struct thread *td)
923 p->p_addr = (void *)td->td_kstack;
926 td->td_lwp = &p->p_lwp;
927 td->td_switch = cpu_heavy_switch;
929 KKASSERT(td->td_mpcount == 1);
931 bzero(p->p_addr, sizeof(*p->p_addr));
935 * Dispose the UPAGES for a process that has exited.
936 * This routine directly impacts the exit perf of a process.
939 pmap_dispose_proc(struct proc *p)
943 KASSERT(p->p_lock == 0, ("attempt to dispose referenced proc! %p", p));
945 if ((td = p->p_thread) != NULL) {
953 /***************************************************
954 * Page table page management routines.....
955 ***************************************************/
958 * This routine unholds page table pages, and if the hold count
959 * drops to zero, then it decrements the wire count.
962 _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, pmap_inval_info_t info)
964 pmap_inval_flush(info);
965 while (vm_page_sleep_busy(m, FALSE, "pmuwpt"))
968 if (m->hold_count == 0) {
970 * unmap the page table page
972 pmap_inval_add(info, pmap, -1);
973 pmap->pm_pdir[m->pindex] = 0;
974 --pmap->pm_stats.resident_count;
976 if (pmap->pm_ptphint == m)
977 pmap->pm_ptphint = NULL;
980 * If the page is finally unwired, simply free it.
983 if (m->wire_count == 0) {
986 vm_page_free_zero(m);
987 --vmstats.v_wire_count;
994 static PMAP_INLINE int
995 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, pmap_inval_info_t info)
998 if (m->hold_count == 0)
999 return _pmap_unwire_pte_hold(pmap, m, info);
1005 * After removing a page table entry, this routine is used to
1006 * conditionally free the page, and manage the hold/wire counts.
1009 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte,
1010 pmap_inval_info_t info)
1013 if (va >= UPT_MIN_ADDRESS)
1017 ptepindex = (va >> PDRSHIFT);
1018 if (pmap->pm_ptphint &&
1019 (pmap->pm_ptphint->pindex == ptepindex)) {
1020 mpte = pmap->pm_ptphint;
1022 pmap_inval_flush(info);
1023 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1024 pmap->pm_ptphint = mpte;
1028 return pmap_unwire_pte_hold(pmap, mpte, info);
1032 * Initialize pmap0/vmspace0 - the kernel pmap. This pmap is not added
1033 * to pmap_list because it, and IdlePTD, represents the template used
1034 * to update all other pmaps.
1037 pmap_pinit0(struct pmap *pmap)
1040 (pd_entry_t *)kmem_alloc_pageable(kernel_map, PAGE_SIZE);
1041 pmap_kenter((vm_offset_t)pmap->pm_pdir, (vm_offset_t) IdlePTD);
1043 pmap->pm_active = 0;
1044 pmap->pm_ptphint = NULL;
1045 TAILQ_INIT(&pmap->pm_pvlist);
1046 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1050 * Initialize a preallocated and zeroed pmap structure,
1051 * such as one in a vmspace structure.
1054 pmap_pinit(struct pmap *pmap)
1059 * No need to allocate page table space yet but we do need a valid
1060 * page directory table.
1062 if (pmap->pm_pdir == NULL) {
1064 (pd_entry_t *)kmem_alloc_pageable(kernel_map, PAGE_SIZE);
1068 * allocate object for the ptes
1070 if (pmap->pm_pteobj == NULL)
1071 pmap->pm_pteobj = vm_object_allocate( OBJT_DEFAULT, PTDPTDI + 1);
1074 * allocate the page directory page
1076 ptdpg = vm_page_grab( pmap->pm_pteobj, PTDPTDI,
1077 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1079 ptdpg->wire_count = 1;
1080 ++vmstats.v_wire_count;
1083 vm_page_flag_clear(ptdpg, PG_MAPPED | PG_BUSY); /* not usually mapped*/
1084 ptdpg->valid = VM_PAGE_BITS_ALL;
1086 pmap_kenter((vm_offset_t)pmap->pm_pdir, VM_PAGE_TO_PHYS(ptdpg));
1087 if ((ptdpg->flags & PG_ZERO) == 0)
1088 bzero(pmap->pm_pdir, PAGE_SIZE);
1090 pmap->pm_pdir[MPPTDI] = PTD[MPPTDI];
1092 /* install self-referential address mapping entry */
1093 *(unsigned *) (pmap->pm_pdir + PTDPTDI) =
1094 VM_PAGE_TO_PHYS(ptdpg) | PG_V | PG_RW | PG_A | PG_M;
1097 pmap->pm_active = 0;
1098 pmap->pm_ptphint = NULL;
1099 TAILQ_INIT(&pmap->pm_pvlist);
1100 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1104 * Wire in kernel global address entries. To avoid a race condition
1105 * between pmap initialization and pmap_growkernel, this procedure
1106 * adds the pmap to the master list (which growkernel scans to update),
1107 * then copies the template.
1110 pmap_pinit2(struct pmap *pmap)
1113 TAILQ_INSERT_TAIL(&pmap_list, pmap, pm_pmnode);
1114 /* XXX copies current process, does not fill in MPPTDI */
1115 bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * PTESIZE);
1120 * Attempt to release and free and vm_page in a pmap. Returns 1 on success,
1121 * 0 on failure (if the procedure had to sleep).
1124 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1126 unsigned *pde = (unsigned *) pmap->pm_pdir;
1128 * This code optimizes the case of freeing non-busy
1129 * page-table pages. Those pages are zero now, and
1130 * might as well be placed directly into the zero queue.
1132 if (vm_page_sleep_busy(p, FALSE, "pmaprl"))
1138 * Remove the page table page from the processes address space.
1141 pmap->pm_stats.resident_count--;
1143 if (p->hold_count) {
1144 panic("pmap_release: freeing held page table page");
1147 * Page directory pages need to have the kernel
1148 * stuff cleared, so they can go into the zero queue also.
1150 if (p->pindex == PTDPTDI) {
1151 bzero(pde + KPTDI, nkpt * PTESIZE);
1154 pmap_kremove((vm_offset_t)pmap->pm_pdir);
1157 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1158 pmap->pm_ptphint = NULL;
1161 vmstats.v_wire_count--;
1162 vm_page_free_zero(p);
1167 * this routine is called if the page table page is not
1171 _pmap_allocpte(pmap_t pmap, unsigned ptepindex)
1173 vm_offset_t pteva, ptepa;
1177 * Find or fabricate a new pagetable page
1179 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1180 VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1182 KASSERT(m->queue == PQ_NONE,
1183 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1185 if (m->wire_count == 0)
1186 vmstats.v_wire_count++;
1190 * Increment the hold count for the page table page
1191 * (denoting a new mapping.)
1196 * Map the pagetable page into the process address space, if
1197 * it isn't already there.
1200 pmap->pm_stats.resident_count++;
1202 ptepa = VM_PAGE_TO_PHYS(m);
1203 pmap->pm_pdir[ptepindex] =
1204 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_A | PG_M);
1207 * Set the page table hint
1209 pmap->pm_ptphint = m;
1212 * Try to use the new mapping, but if we cannot, then
1213 * do it with the routine that maps the page explicitly.
1215 if ((m->flags & PG_ZERO) == 0) {
1216 if ((((unsigned)pmap->pm_pdir[PTDPTDI]) & PG_FRAME) ==
1217 (((unsigned) PTDpde) & PG_FRAME)) {
1218 pteva = UPT_MIN_ADDRESS + i386_ptob(ptepindex);
1219 bzero((caddr_t) pteva, PAGE_SIZE);
1221 pmap_zero_page(ptepa);
1225 m->valid = VM_PAGE_BITS_ALL;
1226 vm_page_flag_clear(m, PG_ZERO);
1227 vm_page_flag_set(m, PG_MAPPED);
1234 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1241 * Calculate pagetable page index
1243 ptepindex = va >> PDRSHIFT;
1246 * Get the page directory entry
1248 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1251 * This supports switching from a 4MB page to a
1254 if (ptepa & PG_PS) {
1255 pmap->pm_pdir[ptepindex] = 0;
1262 * If the page table page is mapped, we just increment the
1263 * hold count, and activate it.
1267 * In order to get the page table page, try the
1270 if (pmap->pm_ptphint &&
1271 (pmap->pm_ptphint->pindex == ptepindex)) {
1272 m = pmap->pm_ptphint;
1274 m = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1275 pmap->pm_ptphint = m;
1281 * Here if the pte page isn't mapped, or if it has been deallocated.
1283 return _pmap_allocpte(pmap, ptepindex);
1287 /***************************************************
1288 * Pmap allocation/deallocation routines.
1289 ***************************************************/
1292 * Release any resources held by the given physical map.
1293 * Called when a pmap initialized by pmap_pinit is being released.
1294 * Should only be called if the map contains no valid mappings.
1296 static int pmap_release_callback(struct vm_page *p, void *data);
1299 pmap_release(struct pmap *pmap)
1301 vm_object_t object = pmap->pm_pteobj;
1302 struct rb_vm_page_scan_info info;
1304 #if defined(DIAGNOSTIC)
1305 if (object->ref_count != 1)
1306 panic("pmap_release: pteobj reference count != 1");
1310 info.object = object;
1312 TAILQ_REMOVE(&pmap_list, pmap, pm_pmnode);
1319 info.limit = object->generation;
1321 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
1322 pmap_release_callback, &info);
1323 if (info.error == 0 && info.mpte) {
1324 if (!pmap_release_free_page(pmap, info.mpte))
1328 } while (info.error);
1332 pmap_release_callback(struct vm_page *p, void *data)
1334 struct rb_vm_page_scan_info *info = data;
1336 if (p->pindex == PTDPTDI) {
1340 if (!pmap_release_free_page(info->pmap, p)) {
1344 if (info->object->generation != info->limit) {
1352 kvm_size(SYSCTL_HANDLER_ARGS)
1354 unsigned long ksize = KvaSize;
1356 return sysctl_handle_long(oidp, &ksize, 0, req);
1358 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD,
1359 0, 0, kvm_size, "IU", "Size of KVM");
1362 kvm_free(SYSCTL_HANDLER_ARGS)
1364 unsigned long kfree = virtual_end - kernel_vm_end;
1366 return sysctl_handle_long(oidp, &kfree, 0, req);
1368 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD,
1369 0, 0, kvm_free, "IU", "Amount of KVM free");
1372 * Grow the number of kernel page table entries, if needed.
1376 pmap_growkernel(vm_offset_t addr)
1379 vm_offset_t ptppaddr;
1384 if (kernel_vm_end == 0) {
1385 kernel_vm_end = KERNBASE;
1387 while (pdir_pde(PTD, kernel_vm_end)) {
1388 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1392 addr = (addr + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1393 while (kernel_vm_end < addr) {
1394 if (pdir_pde(PTD, kernel_vm_end)) {
1395 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1400 * This index is bogus, but out of the way
1402 nkpg = vm_page_alloc(kptobj, nkpt,
1403 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM | VM_ALLOC_INTERRUPT);
1405 panic("pmap_growkernel: no memory to grow kernel");
1408 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1409 pmap_zero_page(ptppaddr);
1410 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
1411 pdir_pde(PTD, kernel_vm_end) = newpdir;
1412 *pmap_pde(kernel_pmap, kernel_vm_end) = newpdir;
1416 * This update must be interlocked with pmap_pinit2.
1418 TAILQ_FOREACH(pmap, &pmap_list, pm_pmnode) {
1419 *pmap_pde(pmap, kernel_vm_end) = newpdir;
1421 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) &
1422 ~(PAGE_SIZE * NPTEPG - 1);
1428 * Retire the given physical map from service.
1429 * Should only be called if the map contains
1430 * no valid mappings.
1433 pmap_destroy(pmap_t pmap)
1440 count = --pmap->pm_count;
1443 panic("destroying a pmap is not yet implemented");
1448 * Add a reference to the specified pmap.
1451 pmap_reference(pmap_t pmap)
1458 /***************************************************
1459 * page management routines.
1460 ***************************************************/
1463 * free the pv_entry back to the free list. This function may be
1464 * called from an interrupt.
1466 static PMAP_INLINE void
1467 free_pv_entry(pv_entry_t pv)
1474 * get a new pv_entry, allocating a block from the system
1475 * when needed. This function may be called from an interrupt.
1481 if (pv_entry_high_water &&
1482 (pv_entry_count > pv_entry_high_water) &&
1483 (pmap_pagedaemon_waken == 0)) {
1484 pmap_pagedaemon_waken = 1;
1485 wakeup (&vm_pages_needed);
1487 return zalloc(pvzone);
1491 * This routine is very drastic, but can save the system
1499 static int warningdone=0;
1501 if (pmap_pagedaemon_waken == 0)
1504 if (warningdone < 5) {
1505 kprintf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1509 for(i = 0; i < vm_page_array_size; i++) {
1510 m = &vm_page_array[i];
1511 if (m->wire_count || m->hold_count || m->busy ||
1512 (m->flags & PG_BUSY))
1516 pmap_pagedaemon_waken = 0;
1521 * If it is the first entry on the list, it is actually
1522 * in the header and we must copy the following entry up
1523 * to the header. Otherwise we must search the list for
1524 * the entry. In either case we free the now unused entry.
1527 pmap_remove_entry(struct pmap *pmap, vm_page_t m,
1528 vm_offset_t va, pmap_inval_info_t info)
1534 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1535 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1536 if (pmap == pv->pv_pmap && va == pv->pv_va)
1540 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1541 if (va == pv->pv_va)
1548 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem, info);
1549 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1550 m->md.pv_list_count--;
1551 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
1552 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1553 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1561 * Create a pv entry for page at pa for
1565 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
1570 pv = get_pv_entry();
1575 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1576 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1577 m->md.pv_list_count++;
1583 * pmap_remove_pte: do the things to unmap a page in a process
1586 pmap_remove_pte(struct pmap *pmap, unsigned *ptq, vm_offset_t va,
1587 pmap_inval_info_t info)
1592 pmap_inval_add(info, pmap, va);
1593 oldpte = loadandclear(ptq);
1595 pmap->pm_stats.wired_count -= 1;
1597 * Machines that don't support invlpg, also don't support
1598 * PG_G. XXX PG_G is disabled for SMP so don't worry about
1602 cpu_invlpg((void *)va);
1603 pmap->pm_stats.resident_count -= 1;
1604 if (oldpte & PG_MANAGED) {
1605 m = PHYS_TO_VM_PAGE(oldpte);
1606 if (oldpte & PG_M) {
1607 #if defined(PMAP_DIAGNOSTIC)
1608 if (pmap_nw_modified((pt_entry_t) oldpte)) {
1610 "pmap_remove: modified page not writable: va: 0x%x, pte: 0x%x\n",
1614 if (pmap_track_modified(va))
1618 vm_page_flag_set(m, PG_REFERENCED);
1619 return pmap_remove_entry(pmap, m, va, info);
1621 return pmap_unuse_pt(pmap, va, NULL, info);
1630 * Remove a single page from a process address space.
1632 * This function may not be called from an interrupt if the pmap is
1636 pmap_remove_page(struct pmap *pmap, vm_offset_t va, pmap_inval_info_t info)
1641 * if there is no pte for this address, just skip it!!! Otherwise
1642 * get a local va for mappings for this pmap and remove the entry.
1644 if (*pmap_pde(pmap, va) != 0) {
1645 ptq = get_ptbase(pmap) + i386_btop(va);
1647 pmap_remove_pte(pmap, ptq, va, info);
1655 * Remove the given range of addresses from the specified map.
1657 * It is assumed that the start and end are properly
1658 * rounded to the page size.
1660 * This function may not be called from an interrupt if the pmap is
1664 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
1668 vm_offset_t ptpaddr;
1669 vm_offset_t sindex, eindex;
1670 struct pmap_inval_info info;
1675 if (pmap->pm_stats.resident_count == 0)
1678 pmap_inval_init(&info);
1681 * special handling of removing one page. a very
1682 * common operation and easy to short circuit some
1685 if (((sva + PAGE_SIZE) == eva) &&
1686 (((unsigned) pmap->pm_pdir[(sva >> PDRSHIFT)] & PG_PS) == 0)) {
1687 pmap_remove_page(pmap, sva, &info);
1688 pmap_inval_flush(&info);
1693 * Get a local virtual address for the mappings that are being
1696 ptbase = get_ptbase(pmap);
1698 sindex = i386_btop(sva);
1699 eindex = i386_btop(eva);
1701 for (; sindex < eindex; sindex = pdnxt) {
1705 * Calculate index for next page table.
1707 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1708 if (pmap->pm_stats.resident_count == 0)
1711 pdirindex = sindex / NPDEPG;
1712 if (((ptpaddr = (unsigned) pmap->pm_pdir[pdirindex]) & PG_PS) != 0) {
1713 pmap_inval_add(&info, pmap, -1);
1714 pmap->pm_pdir[pdirindex] = 0;
1715 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1720 * Weed out invalid mappings. Note: we assume that the page
1721 * directory table is always allocated, and in kernel virtual.
1727 * Limit our scan to either the end of the va represented
1728 * by the current page table page, or to the end of the
1729 * range being removed.
1731 if (pdnxt > eindex) {
1735 for (; sindex != pdnxt; sindex++) {
1737 if (ptbase[sindex] == 0)
1739 va = i386_ptob(sindex);
1740 if (pmap_remove_pte(pmap, ptbase + sindex, va, &info))
1744 pmap_inval_flush(&info);
1750 * Removes this physical page from all physical maps in which it resides.
1751 * Reflects back modify bits to the pager.
1753 * This routine may not be called from an interrupt.
1757 pmap_remove_all(vm_page_t m)
1759 struct pmap_inval_info info;
1760 unsigned *pte, tpte;
1763 #if defined(PMAP_DIAGNOSTIC)
1765 * XXX this makes pmap_page_protect(NONE) illegal for non-managed
1768 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
1769 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%08llx", (long long)VM_PAGE_TO_PHYS(m));
1773 pmap_inval_init(&info);
1775 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
1776 pv->pv_pmap->pm_stats.resident_count--;
1778 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
1779 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
1781 tpte = loadandclear(pte);
1783 pv->pv_pmap->pm_stats.wired_count--;
1786 vm_page_flag_set(m, PG_REFERENCED);
1789 * Update the vm_page_t clean and reference bits.
1792 #if defined(PMAP_DIAGNOSTIC)
1793 if (pmap_nw_modified((pt_entry_t) tpte)) {
1795 "pmap_remove_all: modified page not writable: va: 0x%x, pte: 0x%x\n",
1799 if (pmap_track_modified(pv->pv_va))
1802 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
1803 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1804 m->md.pv_list_count--;
1805 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem, &info);
1809 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1811 pmap_inval_flush(&info);
1817 * Set the physical protection on the specified range of this map
1820 * This function may not be called from an interrupt if the map is
1821 * not the kernel_pmap.
1824 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
1827 vm_offset_t pdnxt, ptpaddr;
1828 vm_pindex_t sindex, eindex;
1829 pmap_inval_info info;
1834 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1835 pmap_remove(pmap, sva, eva);
1839 if (prot & VM_PROT_WRITE)
1842 pmap_inval_init(&info);
1844 ptbase = get_ptbase(pmap);
1846 sindex = i386_btop(sva);
1847 eindex = i386_btop(eva);
1849 for (; sindex < eindex; sindex = pdnxt) {
1853 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1855 pdirindex = sindex / NPDEPG;
1856 if (((ptpaddr = (unsigned) pmap->pm_pdir[pdirindex]) & PG_PS) != 0) {
1857 pmap_inval_add(&info, pmap, -1);
1858 pmap->pm_pdir[pdirindex] &= ~(PG_M|PG_RW);
1859 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1864 * Weed out invalid mappings. Note: we assume that the page
1865 * directory table is always allocated, and in kernel virtual.
1870 if (pdnxt > eindex) {
1874 for (; sindex != pdnxt; sindex++) {
1879 /* XXX this isn't optimal */
1880 pmap_inval_add(&info, pmap, i386_ptob(sindex));
1881 pbits = ptbase[sindex];
1883 if (pbits & PG_MANAGED) {
1886 m = PHYS_TO_VM_PAGE(pbits);
1887 vm_page_flag_set(m, PG_REFERENCED);
1891 if (pmap_track_modified(i386_ptob(sindex))) {
1893 m = PHYS_TO_VM_PAGE(pbits);
1902 if (pbits != ptbase[sindex]) {
1903 ptbase[sindex] = pbits;
1907 pmap_inval_flush(&info);
1911 * Insert the given physical page (p) at
1912 * the specified virtual address (v) in the
1913 * target physical map with the protection requested.
1915 * If specified, the page will be wired down, meaning
1916 * that the related pte can not be reclaimed.
1918 * NB: This is the only routine which MAY NOT lazy-evaluate
1919 * or lose information. That is, this routine must actually
1920 * insert this page into the given map NOW.
1923 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1929 vm_offset_t origpte, newpte;
1931 pmap_inval_info info;
1937 #ifdef PMAP_DIAGNOSTIC
1939 panic("pmap_enter: toobig");
1940 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
1941 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%x)", va);
1946 * In the case that a page table page is not
1947 * resident, we are creating it here.
1949 if (va < UPT_MIN_ADDRESS) {
1950 mpte = pmap_allocpte(pmap, va);
1953 pmap_inval_init(&info);
1954 pte = pmap_pte(pmap, va);
1957 * Page Directory table entry not valid, we need a new PT page
1960 panic("pmap_enter: invalid page directory pdir=%x, va=0x%x\n",
1961 (unsigned) pmap->pm_pdir[PTDPTDI], va);
1964 pa = VM_PAGE_TO_PHYS(m) & PG_FRAME;
1965 pmap_inval_add(&info, pmap, va); /* XXX non-optimal */
1966 origpte = *(vm_offset_t *)pte;
1967 opa = origpte & PG_FRAME;
1969 if (origpte & PG_PS)
1970 panic("pmap_enter: attempted pmap_enter on 4MB page");
1973 * Mapping has not changed, must be protection or wiring change.
1975 if (origpte && (opa == pa)) {
1977 * Wiring change, just update stats. We don't worry about
1978 * wiring PT pages as they remain resident as long as there
1979 * are valid mappings in them. Hence, if a user page is wired,
1980 * the PT page will be also.
1982 if (wired && ((origpte & PG_W) == 0))
1983 pmap->pm_stats.wired_count++;
1984 else if (!wired && (origpte & PG_W))
1985 pmap->pm_stats.wired_count--;
1987 #if defined(PMAP_DIAGNOSTIC)
1988 if (pmap_nw_modified((pt_entry_t) origpte)) {
1990 "pmap_enter: modified page not writable: va: 0x%x, pte: 0x%x\n",
1996 * Remove the extra pte reference. Note that we cannot
1997 * optimize the RO->RW case because we have adjusted the
1998 * wiring count above and may need to adjust the wiring
2005 * We might be turning off write access to the page,
2006 * so we go ahead and sense modify status.
2008 if (origpte & PG_MANAGED) {
2009 if ((origpte & PG_M) && pmap_track_modified(va)) {
2011 om = PHYS_TO_VM_PAGE(opa);
2019 * Mapping has changed, invalidate old range and fall through to
2020 * handle validating new mapping.
2024 err = pmap_remove_pte(pmap, pte, va, &info);
2026 panic("pmap_enter: pte vanished, va: 0x%x", va);
2030 * Enter on the PV list if part of our managed memory. Note that we
2031 * raise IPL while manipulating pv_table since pmap_enter can be
2032 * called at interrupt time.
2034 if (pmap_initialized &&
2035 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2036 pmap_insert_entry(pmap, va, mpte, m);
2041 * Increment counters
2043 pmap->pm_stats.resident_count++;
2045 pmap->pm_stats.wired_count++;
2049 * Now validate mapping with desired protection/wiring.
2051 newpte = (vm_offset_t) (pa | pte_prot(pmap, prot) | PG_V);
2055 if (va < UPT_MIN_ADDRESS)
2057 if (pmap == kernel_pmap)
2061 * if the mapping or permission bits are different, we need
2062 * to update the pte.
2064 if ((origpte & ~(PG_M|PG_A)) != newpte) {
2065 *pte = newpte | PG_A;
2067 pmap_inval_flush(&info);
2071 * this code makes some *MAJOR* assumptions:
2072 * 1. Current pmap & pmap exists.
2075 * 4. No page table pages.
2076 * 5. Tlbflush is deferred to calling procedure.
2077 * 6. Page IS managed.
2078 * but is *MUCH* faster than pmap_enter...
2082 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_page_t mpte)
2086 pmap_inval_info info;
2088 pmap_inval_init(&info);
2091 * In the case that a page table page is not
2092 * resident, we are creating it here.
2094 if (va < UPT_MIN_ADDRESS) {
2099 * Calculate pagetable page index
2101 ptepindex = va >> PDRSHIFT;
2102 if (mpte && (mpte->pindex == ptepindex)) {
2107 * Get the page directory entry
2109 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
2112 * If the page table page is mapped, we just increment
2113 * the hold count, and activate it.
2117 panic("pmap_enter_quick: unexpected mapping into 4MB page");
2118 if (pmap->pm_ptphint &&
2119 (pmap->pm_ptphint->pindex == ptepindex)) {
2120 mpte = pmap->pm_ptphint;
2122 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
2123 pmap->pm_ptphint = mpte;
2129 mpte = _pmap_allocpte(pmap, ptepindex);
2137 * This call to vtopte makes the assumption that we are
2138 * entering the page into the current pmap. In order to support
2139 * quick entry into any pmap, one would likely use pmap_pte_quick.
2140 * But that isn't as quick as vtopte.
2142 pte = (unsigned *)vtopte(va);
2145 pmap_unwire_pte_hold(pmap, mpte, &info);
2150 * Enter on the PV list if part of our managed memory. Note that we
2151 * raise IPL while manipulating pv_table since pmap_enter can be
2152 * called at interrupt time.
2154 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0)
2155 pmap_insert_entry(pmap, va, mpte, m);
2158 * Increment counters
2160 pmap->pm_stats.resident_count++;
2162 pa = VM_PAGE_TO_PHYS(m);
2165 * Now validate mapping with RO protection
2167 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2168 *pte = pa | PG_V | PG_U;
2170 *pte = pa | PG_V | PG_U | PG_MANAGED;
2176 * Make a temporary mapping for a physical address. This is only intended
2177 * to be used for panic dumps.
2180 pmap_kenter_temporary(vm_paddr_t pa, int i)
2182 pmap_kenter((vm_offset_t)crashdumpmap + (i * PAGE_SIZE), pa);
2183 return ((void *)crashdumpmap);
2186 #define MAX_INIT_PT (96)
2189 * This routine preloads the ptes for a given object into the specified pmap.
2190 * This eliminates the blast of soft faults on process startup and
2191 * immediately after an mmap.
2193 static int pmap_object_init_pt_callback(vm_page_t p, void *data);
2196 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2197 vm_object_t object, vm_pindex_t pindex,
2198 vm_size_t size, int limit)
2200 struct rb_vm_page_scan_info info;
2204 * We can't preinit if read access isn't set or there is no pmap
2207 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2211 * We can't preinit if the pmap is not the current pmap
2213 if (curproc == NULL || pmap != vmspace_pmap(curproc->p_vmspace))
2216 psize = i386_btop(size);
2218 if ((object->type != OBJT_VNODE) ||
2219 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2220 (object->resident_page_count > MAX_INIT_PT))) {
2224 if (psize + pindex > object->size) {
2225 if (object->size < pindex)
2227 psize = object->size - pindex;
2234 * Use a red-black scan to traverse the requested range and load
2235 * any valid pages found into the pmap.
2237 * We cannot safely scan the object's memq unless we are in a
2238 * critical section since interrupts can remove pages from objects.
2240 info.start_pindex = pindex;
2241 info.end_pindex = pindex + psize - 1;
2248 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2249 pmap_object_init_pt_callback, &info);
2255 pmap_object_init_pt_callback(vm_page_t p, void *data)
2257 struct rb_vm_page_scan_info *info = data;
2258 vm_pindex_t rel_index;
2260 * don't allow an madvise to blow away our really
2261 * free pages allocating pv entries.
2263 if ((info->limit & MAP_PREFAULT_MADVISE) &&
2264 vmstats.v_free_count < vmstats.v_free_reserved) {
2267 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2268 (p->busy == 0) && (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2269 if ((p->queue - p->pc) == PQ_CACHE)
2270 vm_page_deactivate(p);
2272 rel_index = p->pindex - info->start_pindex;
2273 info->mpte = pmap_enter_quick(info->pmap,
2274 info->addr + i386_ptob(rel_index),
2276 vm_page_flag_set(p, PG_MAPPED);
2283 * pmap_prefault provides a quick way of clustering pagefaults into a
2284 * processes address space. It is a "cousin" of pmap_object_init_pt,
2285 * except it runs at page fault time instead of mmap time.
2289 #define PAGEORDER_SIZE (PFBAK+PFFOR)
2291 static int pmap_prefault_pageorder[] = {
2292 -PAGE_SIZE, PAGE_SIZE,
2293 -2 * PAGE_SIZE, 2 * PAGE_SIZE,
2294 -3 * PAGE_SIZE, 3 * PAGE_SIZE,
2295 -4 * PAGE_SIZE, 4 * PAGE_SIZE
2299 pmap_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry)
2309 * We do not currently prefault mappings that use virtual page
2310 * tables. We do not prefault foreign pmaps.
2312 if (entry->maptype == VM_MAPTYPE_VPAGETABLE)
2314 if (curproc == NULL || (pmap != vmspace_pmap(curproc->p_vmspace)))
2317 object = entry->object.vm_object;
2319 starta = addra - PFBAK * PAGE_SIZE;
2320 if (starta < entry->start)
2321 starta = entry->start;
2322 else if (starta > addra)
2326 * critical section protection is required to maintain the
2327 * page/object association, interrupts can free pages and remove
2328 * them from their objects.
2332 for (i = 0; i < PAGEORDER_SIZE; i++) {
2333 vm_object_t lobject;
2336 addr = addra + pmap_prefault_pageorder[i];
2337 if (addr > addra + (PFFOR * PAGE_SIZE))
2340 if (addr < starta || addr >= entry->end)
2343 if ((*pmap_pde(pmap, addr)) == NULL)
2346 pte = (unsigned *) vtopte(addr);
2350 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2353 for (m = vm_page_lookup(lobject, pindex);
2354 (!m && (lobject->type == OBJT_DEFAULT) &&
2355 (lobject->backing_object));
2356 lobject = lobject->backing_object
2358 if (lobject->backing_object_offset & PAGE_MASK)
2360 pindex += (lobject->backing_object_offset >> PAGE_SHIFT);
2361 m = vm_page_lookup(lobject->backing_object, pindex);
2365 * give-up when a page is not in memory
2370 if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2372 (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2374 if ((m->queue - m->pc) == PQ_CACHE) {
2375 vm_page_deactivate(m);
2378 mpte = pmap_enter_quick(pmap, addr, m, mpte);
2379 vm_page_flag_set(m, PG_MAPPED);
2387 * Routine: pmap_change_wiring
2388 * Function: Change the wiring attribute for a map/virtual-address
2390 * In/out conditions:
2391 * The mapping must already exist in the pmap.
2394 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2401 pte = pmap_pte(pmap, va);
2403 if (wired && !pmap_pte_w(pte))
2404 pmap->pm_stats.wired_count++;
2405 else if (!wired && pmap_pte_w(pte))
2406 pmap->pm_stats.wired_count--;
2409 * Wiring is not a hardware characteristic so there is no need to
2410 * invalidate TLB. However, in an SMP environment we must use
2411 * a locked bus cycle to update the pte (if we are not using
2412 * the pmap_inval_*() API that is)... it's ok to do this for simple
2417 atomic_set_int(pte, PG_W);
2419 atomic_clear_int(pte, PG_W);
2422 atomic_set_int_nonlocked(pte, PG_W);
2424 atomic_clear_int_nonlocked(pte, PG_W);
2431 * Copy the range specified by src_addr/len
2432 * from the source map to the range dst_addr/len
2433 * in the destination map.
2435 * This routine is only advisory and need not do anything.
2438 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2439 vm_size_t len, vm_offset_t src_addr)
2441 pmap_inval_info info;
2443 vm_offset_t end_addr = src_addr + len;
2445 unsigned src_frame, dst_frame;
2448 if (dst_addr != src_addr)
2451 src_frame = ((unsigned) src_pmap->pm_pdir[PTDPTDI]) & PG_FRAME;
2452 if (src_frame != (((unsigned) PTDpde) & PG_FRAME)) {
2456 dst_frame = ((unsigned) dst_pmap->pm_pdir[PTDPTDI]) & PG_FRAME;
2457 if (dst_frame != (((unsigned) APTDpde) & PG_FRAME)) {
2458 APTDpde = (pd_entry_t) (dst_frame | PG_RW | PG_V);
2459 /* The page directory is not shared between CPUs */
2462 pmap_inval_init(&info);
2463 pmap_inval_add(&info, dst_pmap, -1);
2464 pmap_inval_add(&info, src_pmap, -1);
2467 * critical section protection is required to maintain the page/object
2468 * association, interrupts can free pages and remove them from
2472 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
2473 unsigned *src_pte, *dst_pte;
2474 vm_page_t dstmpte, srcmpte;
2475 vm_offset_t srcptepaddr;
2478 if (addr >= UPT_MIN_ADDRESS)
2479 panic("pmap_copy: invalid to pmap_copy page tables\n");
2482 * Don't let optional prefaulting of pages make us go
2483 * way below the low water mark of free pages or way
2484 * above high water mark of used pv entries.
2486 if (vmstats.v_free_count < vmstats.v_free_reserved ||
2487 pv_entry_count > pv_entry_high_water)
2490 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
2491 ptepindex = addr >> PDRSHIFT;
2493 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex];
2494 if (srcptepaddr == 0)
2497 if (srcptepaddr & PG_PS) {
2498 if (dst_pmap->pm_pdir[ptepindex] == 0) {
2499 dst_pmap->pm_pdir[ptepindex] = (pd_entry_t) srcptepaddr;
2500 dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
2505 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
2506 if ((srcmpte == NULL) ||
2507 (srcmpte->hold_count == 0) || (srcmpte->flags & PG_BUSY))
2510 if (pdnxt > end_addr)
2513 src_pte = (unsigned *) vtopte(addr);
2514 dst_pte = (unsigned *) avtopte(addr);
2515 while (addr < pdnxt) {
2519 * we only virtual copy managed pages
2521 if ((ptetemp & PG_MANAGED) != 0) {
2523 * We have to check after allocpte for the
2524 * pte still being around... allocpte can
2527 dstmpte = pmap_allocpte(dst_pmap, addr);
2528 if ((*dst_pte == 0) && (ptetemp = *src_pte)) {
2530 * Clear the modified and
2531 * accessed (referenced) bits
2534 m = PHYS_TO_VM_PAGE(ptetemp);
2535 *dst_pte = ptetemp & ~(PG_M | PG_A);
2536 dst_pmap->pm_stats.resident_count++;
2537 pmap_insert_entry(dst_pmap, addr,
2540 pmap_unwire_pte_hold(dst_pmap, dstmpte, &info);
2542 if (dstmpte->hold_count >= srcmpte->hold_count)
2551 pmap_inval_flush(&info);
2555 * Routine: pmap_kernel
2557 * Returns the physical map handle for the kernel.
2562 return (kernel_pmap);
2568 * Zero the specified PA by mapping the page into KVM and clearing its
2571 * This function may be called from an interrupt and no locking is
2575 pmap_zero_page(vm_paddr_t phys)
2577 struct mdglobaldata *gd = mdcpu;
2580 if (*(int *)gd->gd_CMAP3)
2581 panic("pmap_zero_page: CMAP3 busy");
2582 *(int *)gd->gd_CMAP3 =
2583 PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2584 cpu_invlpg(gd->gd_CADDR3);
2586 #if defined(I686_CPU)
2587 if (cpu_class == CPUCLASS_686)
2588 i686_pagezero(gd->gd_CADDR3);
2591 bzero(gd->gd_CADDR3, PAGE_SIZE);
2592 *(int *) gd->gd_CMAP3 = 0;
2597 * pmap_page_assertzero:
2599 * Assert that a page is empty, panic if it isn't.
2602 pmap_page_assertzero(vm_paddr_t phys)
2604 struct mdglobaldata *gd = mdcpu;
2608 if (*(int *)gd->gd_CMAP3)
2609 panic("pmap_zero_page: CMAP3 busy");
2610 *(int *)gd->gd_CMAP3 =
2611 PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2612 cpu_invlpg(gd->gd_CADDR3);
2613 for (i = 0; i < PAGE_SIZE; i += 4) {
2614 if (*(int *)((char *)gd->gd_CADDR3 + i) != 0) {
2615 panic("pmap_page_assertzero() @ %p not zero!\n",
2616 (void *)gd->gd_CADDR3);
2619 *(int *) gd->gd_CMAP3 = 0;
2626 * Zero part of a physical page by mapping it into memory and clearing
2627 * its contents with bzero.
2629 * off and size may not cover an area beyond a single hardware page.
2632 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
2634 struct mdglobaldata *gd = mdcpu;
2637 if (*(int *) gd->gd_CMAP3)
2638 panic("pmap_zero_page: CMAP3 busy");
2639 *(int *) gd->gd_CMAP3 = PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2640 cpu_invlpg(gd->gd_CADDR3);
2642 #if defined(I686_CPU)
2643 if (cpu_class == CPUCLASS_686 && off == 0 && size == PAGE_SIZE)
2644 i686_pagezero(gd->gd_CADDR3);
2647 bzero((char *)gd->gd_CADDR3 + off, size);
2648 *(int *) gd->gd_CMAP3 = 0;
2655 * Copy the physical page from the source PA to the target PA.
2656 * This function may be called from an interrupt. No locking
2660 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
2662 struct mdglobaldata *gd = mdcpu;
2665 if (*(int *) gd->gd_CMAP1)
2666 panic("pmap_copy_page: CMAP1 busy");
2667 if (*(int *) gd->gd_CMAP2)
2668 panic("pmap_copy_page: CMAP2 busy");
2670 *(int *) gd->gd_CMAP1 = PG_V | (src & PG_FRAME) | PG_A;
2671 *(int *) gd->gd_CMAP2 = PG_V | PG_RW | (dst & PG_FRAME) | PG_A | PG_M;
2673 cpu_invlpg(gd->gd_CADDR1);
2674 cpu_invlpg(gd->gd_CADDR2);
2676 bcopy(gd->gd_CADDR1, gd->gd_CADDR2, PAGE_SIZE);
2678 *(int *) gd->gd_CMAP1 = 0;
2679 *(int *) gd->gd_CMAP2 = 0;
2684 * pmap_copy_page_frag:
2686 * Copy the physical page from the source PA to the target PA.
2687 * This function may be called from an interrupt. No locking
2691 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
2693 struct mdglobaldata *gd = mdcpu;
2696 if (*(int *) gd->gd_CMAP1)
2697 panic("pmap_copy_page: CMAP1 busy");
2698 if (*(int *) gd->gd_CMAP2)
2699 panic("pmap_copy_page: CMAP2 busy");
2701 *(int *) gd->gd_CMAP1 = PG_V | (src & PG_FRAME) | PG_A;
2702 *(int *) gd->gd_CMAP2 = PG_V | PG_RW | (dst & PG_FRAME) | PG_A | PG_M;
2704 cpu_invlpg(gd->gd_CADDR1);
2705 cpu_invlpg(gd->gd_CADDR2);
2707 bcopy((char *)gd->gd_CADDR1 + (src & PAGE_MASK),
2708 (char *)gd->gd_CADDR2 + (dst & PAGE_MASK),
2711 *(int *) gd->gd_CMAP1 = 0;
2712 *(int *) gd->gd_CMAP2 = 0;
2717 * Returns true if the pmap's pv is one of the first
2718 * 16 pvs linked to from this page. This count may
2719 * be changed upwards or downwards in the future; it
2720 * is only necessary that true be returned for a small
2721 * subset of pmaps for proper page aging.
2724 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2729 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2734 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2735 if (pv->pv_pmap == pmap) {
2748 * Remove all pages from specified address space
2749 * this aids process exit speeds. Also, this code
2750 * is special cased for current process only, but
2751 * can have the more generic (and slightly slower)
2752 * mode enabled. This is much faster than pmap_remove
2753 * in the case of running down an entire address space.
2756 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2758 unsigned *pte, tpte;
2761 pmap_inval_info info;
2764 if (curproc && pmap == vmspace_pmap(curproc->p_vmspace))
2769 pmap_inval_init(&info);
2771 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
2773 if (pv->pv_va >= eva || pv->pv_va < sva) {
2774 npv = TAILQ_NEXT(pv, pv_plist);
2779 pte = (unsigned *)vtopte(pv->pv_va);
2781 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2782 if (pmap->pm_active)
2783 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
2787 * We cannot remove wired pages from a process' mapping
2791 npv = TAILQ_NEXT(pv, pv_plist);
2796 m = PHYS_TO_VM_PAGE(tpte);
2798 KASSERT(m < &vm_page_array[vm_page_array_size],
2799 ("pmap_remove_pages: bad tpte %x", tpte));
2801 pv->pv_pmap->pm_stats.resident_count--;
2804 * Update the vm_page_t clean and reference bits.
2811 npv = TAILQ_NEXT(pv, pv_plist);
2812 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2814 m->md.pv_list_count--;
2815 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2816 if (TAILQ_FIRST(&m->md.pv_list) == NULL) {
2817 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2820 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem, &info);
2823 pmap_inval_flush(&info);
2828 * pmap_testbit tests bits in pte's
2829 * note that the testbit/changebit routines are inline,
2830 * and a lot of things compile-time evaluate.
2833 pmap_testbit(vm_page_t m, int bit)
2838 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2841 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2846 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2848 * if the bit being tested is the modified bit, then
2849 * mark clean_map and ptes as never
2852 if (bit & (PG_A|PG_M)) {
2853 if (!pmap_track_modified(pv->pv_va))
2857 #if defined(PMAP_DIAGNOSTIC)
2859 kprintf("Null pmap (tb) at va: 0x%x\n", pv->pv_va);
2863 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2874 * this routine is used to modify bits in ptes
2876 static __inline void
2877 pmap_changebit(vm_page_t m, int bit, boolean_t setem)
2879 struct pmap_inval_info info;
2883 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2886 pmap_inval_init(&info);
2890 * Loop over all current mappings setting/clearing as appropos If
2891 * setting RO do we need to clear the VAC?
2893 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2895 * don't write protect pager mappings
2897 if (!setem && (bit == PG_RW)) {
2898 if (!pmap_track_modified(pv->pv_va))
2902 #if defined(PMAP_DIAGNOSTIC)
2904 kprintf("Null pmap (cb) at va: 0x%x\n", pv->pv_va);
2910 * Careful here. We can use a locked bus instruction to
2911 * clear PG_A or PG_M safely but we need to synchronize
2912 * with the target cpus when we mess with PG_RW.
2914 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2916 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
2920 atomic_set_int(pte, bit);
2922 atomic_set_int_nonlocked(pte, bit);
2925 vm_offset_t pbits = *(vm_offset_t *)pte;
2932 atomic_clear_int(pte, PG_M|PG_RW);
2934 atomic_clear_int_nonlocked(pte, PG_M|PG_RW);
2938 atomic_clear_int(pte, bit);
2940 atomic_clear_int_nonlocked(pte, bit);
2946 pmap_inval_flush(&info);
2951 * pmap_page_protect:
2953 * Lower the permission for all mappings to a given page.
2956 pmap_page_protect(vm_page_t m, vm_prot_t prot)
2958 if ((prot & VM_PROT_WRITE) == 0) {
2959 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
2960 pmap_changebit(m, PG_RW, FALSE);
2968 pmap_phys_address(int ppn)
2970 return (i386_ptob(ppn));
2974 * pmap_ts_referenced:
2976 * Return a count of reference bits for a page, clearing those bits.
2977 * It is not necessary for every reference bit to be cleared, but it
2978 * is necessary that 0 only be returned when there are truly no
2979 * reference bits set.
2981 * XXX: The exact number of bits to check and clear is a matter that
2982 * should be tested and standardized at some point in the future for
2983 * optimal aging of shared pages.
2986 pmap_ts_referenced(vm_page_t m)
2988 pv_entry_t pv, pvf, pvn;
2992 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2997 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3002 pvn = TAILQ_NEXT(pv, pv_list);
3004 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3006 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3008 if (!pmap_track_modified(pv->pv_va))
3011 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3013 if (pte && (*pte & PG_A)) {
3015 atomic_clear_int(pte, PG_A);
3017 atomic_clear_int_nonlocked(pte, PG_A);
3024 } while ((pv = pvn) != NULL && pv != pvf);
3034 * Return whether or not the specified physical page was modified
3035 * in any physical maps.
3038 pmap_is_modified(vm_page_t m)
3040 return pmap_testbit(m, PG_M);
3044 * Clear the modify bits on the specified physical page.
3047 pmap_clear_modify(vm_page_t m)
3049 pmap_changebit(m, PG_M, FALSE);
3053 * pmap_clear_reference:
3055 * Clear the reference bit on the specified physical page.
3058 pmap_clear_reference(vm_page_t m)
3060 pmap_changebit(m, PG_A, FALSE);
3064 * Miscellaneous support routines follow
3068 i386_protection_init(void)
3072 kp = protection_codes;
3073 for (prot = 0; prot < 8; prot++) {
3075 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE:
3077 * Read access is also 0. There isn't any execute bit,
3078 * so just make it readable.
3080 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE:
3081 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE:
3082 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE:
3085 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE:
3086 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE:
3087 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE:
3088 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
3096 * Map a set of physical memory pages into the kernel virtual
3097 * address space. Return a pointer to where it is mapped. This
3098 * routine is intended to be used for mapping device memory,
3101 * NOTE: we can't use pgeflag unless we invalidate the pages one at
3105 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
3107 vm_offset_t va, tmpva, offset;
3110 offset = pa & PAGE_MASK;
3111 size = roundup(offset + size, PAGE_SIZE);
3113 va = kmem_alloc_nofault(kernel_map, size);
3115 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3118 for (tmpva = va; size > 0;) {
3119 pte = (unsigned *)vtopte(tmpva);
3120 *pte = pa | PG_RW | PG_V; /* | pgeflag; */
3128 return ((void *)(va + offset));
3132 pmap_unmapdev(vm_offset_t va, vm_size_t size)
3134 vm_offset_t base, offset;
3136 base = va & PG_FRAME;
3137 offset = va & PAGE_MASK;
3138 size = roundup(offset + size, PAGE_SIZE);
3139 pmap_qremove(va, size >> PAGE_SHIFT);
3140 kmem_free(kernel_map, base, size);
3144 * perform the pmap work for mincore
3147 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3149 unsigned *ptep, pte;
3153 ptep = pmap_pte(pmap, addr);
3158 if ((pte = *ptep) != 0) {
3161 val = MINCORE_INCORE;
3162 if ((pte & PG_MANAGED) == 0)
3165 pa = pte & PG_FRAME;
3167 m = PHYS_TO_VM_PAGE(pa);
3173 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3175 * Modified by someone
3177 else if (m->dirty || pmap_is_modified(m))
3178 val |= MINCORE_MODIFIED_OTHER;
3183 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3186 * Referenced by someone
3188 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3189 val |= MINCORE_REFERENCED_OTHER;
3190 vm_page_flag_set(m, PG_REFERENCED);
3197 pmap_activate(struct proc *p)
3201 pmap = vmspace_pmap(p->p_vmspace);
3203 atomic_set_int(&pmap->pm_active, 1 << mycpu->gd_cpuid);
3205 pmap->pm_active |= 1;
3207 #if defined(SWTCH_OPTIM_STATS)
3210 p->p_thread->td_pcb->pcb_cr3 = vtophys(pmap->pm_pdir);
3211 load_cr3(p->p_thread->td_pcb->pcb_cr3);
3215 pmap_deactivate(struct proc *p)
3219 pmap = vmspace_pmap(p->p_vmspace);
3221 atomic_clear_int(&pmap->pm_active, 1 << mycpu->gd_cpuid);
3223 pmap->pm_active &= ~1;
3226 * XXX - note we do not adjust %cr3. The caller is expected to
3227 * activate a new pmap or do a thread-exit.
3232 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3235 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3239 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3246 static void pads (pmap_t pm);
3247 void pmap_pvdump (vm_paddr_t pa);
3249 /* print address space of pmap*/
3256 if (pm == kernel_pmap)
3258 for (i = 0; i < 1024; i++)
3260 for (j = 0; j < 1024; j++) {
3261 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
3262 if (pm == kernel_pmap && va < KERNBASE)
3264 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS)
3266 ptep = pmap_pte_quick(pm, va);
3267 if (pmap_pte_v(ptep))
3268 kprintf("%x:%x ", va, *(int *) ptep);
3274 pmap_pvdump(vm_paddr_t pa)
3279 kprintf("pa %08llx", (long long)pa);
3280 m = PHYS_TO_VM_PAGE(pa);
3281 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3283 kprintf(" -> pmap %p, va %x, flags %x",
3284 (void *)pv->pv_pmap, pv->pv_va, pv->pv_flags);
3286 kprintf(" -> pmap %p, va %x", (void *)pv->pv_pmap, pv->pv_va);