2 * Copyright (c) 1991 Regents of the University of California.
4 * Copyright (c) 1994 John S. Dyson
6 * Copyright (c) 1994 David Greenman
9 * This code is derived from software contributed to Berkeley by
10 * the Systems Programming Group of the University of Utah Computer
11 * Science Department and William Jolitz of UUNET Technologies Inc.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91
42 * $FreeBSD: src/sys/i386/i386/pmap.c,v 1.250.2.18 2002/03/06 22:48:53 silby Exp $
43 * $DragonFly: src/sys/i386/i386/Attic/pmap.c,v 1.39 2004/05/13 17:40:14 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) (protection_codes[p])
146 static int protection_codes[8];
148 static struct pmap kernel_pmap_store;
151 vm_paddr_t avail_start; /* PA of first available physical page */
152 vm_paddr_t avail_end; /* PA of last available physical page */
153 vm_offset_t virtual_avail; /* VA of first avail page (after kernel bss) */
154 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
155 static boolean_t pmap_initialized = FALSE; /* Has pmap_init completed? */
156 static int pgeflag; /* PG_G or-in */
157 static int pseflag; /* PG_PS or-in */
159 static vm_object_t kptobj;
162 vm_offset_t kernel_vm_end;
165 * Data for the pv entry allocation mechanism
167 static vm_zone_t pvzone;
168 static struct vm_zone pvzone_store;
169 static struct vm_object pvzone_obj;
170 static int pv_entry_count=0, pv_entry_max=0, pv_entry_high_water=0;
171 static int pmap_pagedaemon_waken = 0;
172 static struct pv_entry *pvinit;
175 * All those kernel PT submaps that BSD is so fond of
177 pt_entry_t *CMAP1 = 0, *ptmmap;
178 caddr_t CADDR1 = 0, ptvmmap = 0;
179 static pt_entry_t *msgbufmap;
180 struct msgbuf *msgbufp=0;
185 static pt_entry_t *pt_crashdumpmap;
186 static caddr_t crashdumpmap;
188 extern pt_entry_t *SMPpt;
190 static PMAP_INLINE void free_pv_entry (pv_entry_t pv);
191 static unsigned * get_ptbase (pmap_t pmap);
192 static pv_entry_t get_pv_entry (void);
193 static void i386_protection_init (void);
194 static __inline void pmap_changebit (vm_page_t m, int bit, boolean_t setem);
196 static void pmap_remove_all (vm_page_t m);
197 static vm_page_t pmap_enter_quick (pmap_t pmap, vm_offset_t va,
198 vm_page_t m, vm_page_t mpte);
199 static int pmap_remove_pte (struct pmap *pmap, unsigned *ptq,
200 vm_offset_t sva, pmap_inval_info_t info);
201 static void pmap_remove_page (struct pmap *pmap,
202 vm_offset_t va, pmap_inval_info_t info);
203 static int pmap_remove_entry (struct pmap *pmap, vm_page_t m,
204 vm_offset_t va, pmap_inval_info_t info);
205 static boolean_t pmap_testbit (vm_page_t m, int bit);
206 static void pmap_insert_entry (pmap_t pmap, vm_offset_t va,
207 vm_page_t mpte, vm_page_t m);
209 static vm_page_t pmap_allocpte (pmap_t pmap, vm_offset_t va);
211 static int pmap_release_free_page (pmap_t pmap, vm_page_t p);
212 static vm_page_t _pmap_allocpte (pmap_t pmap, unsigned ptepindex);
213 static unsigned * pmap_pte_quick (pmap_t pmap, vm_offset_t va);
214 static vm_page_t pmap_page_lookup (vm_object_t object, vm_pindex_t pindex);
215 static int pmap_unuse_pt (pmap_t, vm_offset_t, vm_page_t, pmap_inval_info_t);
216 static vm_offset_t pmap_kmem_choose(vm_offset_t addr);
218 static unsigned pdir4mb;
221 * Move the kernel virtual free pointer to the next
222 * 4MB. This is used to help improve performance
223 * by using a large (4MB) page for much of the kernel
224 * (.text, .data, .bss)
227 pmap_kmem_choose(vm_offset_t addr)
229 vm_offset_t newaddr = addr;
231 if (cpu_feature & CPUID_PSE) {
232 newaddr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
241 * Extract the page table entry associated with the given map/virtual
244 * This function may NOT be called from an interrupt.
246 PMAP_INLINE unsigned *
247 pmap_pte(pmap_t pmap, vm_offset_t va)
252 pdeaddr = (unsigned *) pmap_pde(pmap, va);
253 if (*pdeaddr & PG_PS)
256 return get_ptbase(pmap) + i386_btop(va);
265 * Super fast pmap_pte routine best used when scanning the pv lists.
266 * This eliminates many course-grained invltlb calls. Note that many of
267 * the pv list scans are across different pmaps and it is very wasteful
268 * to do an entire invltlb when checking a single mapping.
270 * Should only be called while splvm() is held or from a critical
274 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
276 struct mdglobaldata *gd = mdcpu;
279 if ((pde = (unsigned) pmap->pm_pdir[va >> PDRSHIFT]) != 0) {
280 unsigned frame = (unsigned) pmap->pm_pdir[PTDPTDI] & PG_FRAME;
281 unsigned index = i386_btop(va);
282 /* are we current address space or kernel? */
283 if ((pmap == kernel_pmap) ||
284 (frame == (((unsigned) PTDpde) & PG_FRAME))) {
285 return (unsigned *) PTmap + index;
287 newpf = pde & PG_FRAME;
288 if ( ((* (unsigned *) gd->gd_PMAP1) & PG_FRAME) != newpf) {
289 * (unsigned *) gd->gd_PMAP1 = newpf | PG_RW | PG_V;
290 cpu_invlpg(gd->gd_PADDR1);
292 return gd->gd_PADDR1 + ((unsigned) index & (NPTEPG - 1));
299 * Bootstrap the system enough to run with virtual memory.
301 * On the i386 this is called after mapping has already been enabled
302 * and just syncs the pmap module with what has already been done.
303 * [We can't call it easily with mapping off since the kernel is not
304 * mapped with PA == VA, hence we would have to relocate every address
305 * from the linked base (virtual) address "KERNBASE" to the actual
306 * (physical) address starting relative to 0]
309 pmap_bootstrap(firstaddr, loadaddr)
310 vm_paddr_t firstaddr;
315 struct mdglobaldata *gd;
318 avail_start = firstaddr;
321 * XXX The calculation of virtual_avail is wrong. It's NKPT*PAGE_SIZE too
322 * large. It should instead be correctly calculated in locore.s and
323 * not based on 'first' (which is a physical address, not a virtual
324 * address, for the start of unused physical memory). The kernel
325 * page tables are NOT double mapped and thus should not be included
326 * in this calculation.
328 virtual_avail = (vm_offset_t) KERNBASE + firstaddr;
329 virtual_avail = pmap_kmem_choose(virtual_avail);
331 virtual_end = VM_MAX_KERNEL_ADDRESS;
334 * Initialize protection array.
336 i386_protection_init();
339 * The kernel's pmap is statically allocated so we don't have to use
340 * pmap_create, which is unlikely to work correctly at this part of
341 * the boot sequence (XXX and which no longer exists).
343 kernel_pmap = &kernel_pmap_store;
345 kernel_pmap->pm_pdir = (pd_entry_t *)(KERNBASE + (u_int)IdlePTD);
346 kernel_pmap->pm_count = 1;
347 kernel_pmap->pm_active = (cpumask_t)-1; /* don't allow deactivation */
348 TAILQ_INIT(&kernel_pmap->pm_pvlist);
352 * Reserve some special page table entries/VA space for temporary
355 #define SYSMAP(c, p, v, n) \
356 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
359 pte = (pt_entry_t *) pmap_pte(kernel_pmap, va);
362 * CMAP1/CMAP2 are used for zeroing and copying pages.
364 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
369 SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS);
372 * ptvmmap is used for reading arbitrary physical pages via
375 SYSMAP(caddr_t, ptmmap, ptvmmap, 1)
378 * msgbufp is used to map the system message buffer.
379 * XXX msgbufmap is not used.
381 SYSMAP(struct msgbuf *, msgbufmap, msgbufp,
382 atop(round_page(MSGBUF_SIZE)))
387 for (i = 0; i < NKPT; i++)
391 * PG_G is terribly broken on SMP because we IPI invltlb's in some
392 * cases rather then invl1pg. Actually, I don't even know why it
393 * works under UP because self-referential page table mappings
398 if (cpu_feature & CPUID_PGE)
403 * Initialize the 4MB page size flag
407 * The 4MB page version of the initial
408 * kernel page mapping.
412 #if !defined(DISABLE_PSE)
413 if (cpu_feature & CPUID_PSE) {
416 * Note that we have enabled PSE mode
419 ptditmp = *((unsigned *)PTmap + i386_btop(KERNBASE));
420 ptditmp &= ~(NBPDR - 1);
421 ptditmp |= PG_V | PG_RW | PG_PS | PG_U | pgeflag;
426 * Enable the PSE mode. If we are SMP we can't do this
427 * now because the APs will not be able to use it when
430 load_cr4(rcr4() | CR4_PSE);
433 * We can do the mapping here for the single processor
434 * case. We simply ignore the old page table page from
438 * For SMP, we still need 4K pages to bootstrap APs,
439 * PSE will be enabled as soon as all APs are up.
441 PTD[KPTDI] = (pd_entry_t)ptditmp;
442 kernel_pmap->pm_pdir[KPTDI] = (pd_entry_t)ptditmp;
448 if (cpu_apic_address == 0)
449 panic("pmap_bootstrap: no local apic!");
451 /* local apic is mapped on last page */
452 SMPpt[NPTEPG - 1] = (pt_entry_t)(PG_V | PG_RW | PG_N | pgeflag |
453 (cpu_apic_address & PG_FRAME));
456 /* BSP does this itself, AP's get it pre-set */
457 gd = &CPU_prvspace[0].mdglobaldata;
458 gd->gd_CMAP1 = &SMPpt[1];
459 gd->gd_CMAP2 = &SMPpt[2];
460 gd->gd_CMAP3 = &SMPpt[3];
461 gd->gd_PMAP1 = &SMPpt[4];
462 gd->gd_CADDR1 = CPU_prvspace[0].CPAGE1;
463 gd->gd_CADDR2 = CPU_prvspace[0].CPAGE2;
464 gd->gd_CADDR3 = CPU_prvspace[0].CPAGE3;
465 gd->gd_PADDR1 = (unsigned *)CPU_prvspace[0].PPAGE1;
472 * Set 4mb pdir for mp startup
477 if (pseflag && (cpu_feature & CPUID_PSE)) {
478 load_cr4(rcr4() | CR4_PSE);
479 if (pdir4mb && mycpu->gd_cpuid == 0) { /* only on BSP */
480 kernel_pmap->pm_pdir[KPTDI] =
481 PTD[KPTDI] = (pd_entry_t)pdir4mb;
489 * Initialize the pmap module.
490 * Called by vm_init, to initialize any structures that the pmap
491 * system needs to map virtual memory.
492 * pmap_init has been enhanced to support in a fairly consistant
493 * way, discontiguous physical memory.
496 pmap_init(phys_start, phys_end)
497 vm_paddr_t phys_start, phys_end;
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 * add a wired page to the kva
689 * note that in order for the mapping to take effect -- you
690 * should do a invltlb after doing the pmap_kenter...
693 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
697 pmap_inval_info info;
699 pmap_inval_init(&info);
700 pmap_inval_add(&info, kernel_pmap, va);
701 npte = pa | PG_RW | PG_V | pgeflag;
702 pte = (unsigned *)vtopte(va);
704 pmap_inval_flush(&info);
708 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
713 npte = pa | PG_RW | PG_V | pgeflag;
714 pte = (unsigned *)vtopte(va);
716 cpu_invlpg((void *)va);
720 pmap_kenter_sync(vm_offset_t va)
722 pmap_inval_info info;
724 pmap_inval_init(&info);
725 pmap_inval_add(&info, kernel_pmap, va);
726 pmap_inval_flush(&info);
730 pmap_kenter_sync_quick(vm_offset_t va)
732 cpu_invlpg((void *)va);
736 * remove a page from the kernel pagetables
739 pmap_kremove(vm_offset_t va)
742 pmap_inval_info info;
744 pmap_inval_init(&info);
745 pmap_inval_add(&info, kernel_pmap, va);
746 pte = (unsigned *)vtopte(va);
748 pmap_inval_flush(&info);
752 pmap_kremove_quick(vm_offset_t va)
755 pte = (unsigned *)vtopte(va);
757 cpu_invlpg((void *)va);
761 * Used to map a range of physical addresses into kernel
762 * virtual address space.
764 * For now, VM is already on, we only need to map the
768 pmap_map(vm_offset_t virt, vm_paddr_t start, vm_paddr_t end, int prot)
770 while (start < end) {
771 pmap_kenter(virt, start);
780 * Add a list of wired pages to the kva
781 * this routine is only used for temporary
782 * kernel mappings that do not need to have
783 * page modification or references recorded.
784 * Note that old mappings are simply written
785 * over. The page *must* be wired.
788 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
792 end_va = va + count * PAGE_SIZE;
794 while (va < end_va) {
797 pte = (unsigned *)vtopte(va);
798 *pte = VM_PAGE_TO_PHYS(*m) | PG_RW | PG_V | pgeflag;
799 cpu_invlpg((void *)va);
804 smp_invltlb(); /* XXX */
809 pmap_qenter2(vm_offset_t va, vm_page_t *m, int count, cpumask_t *mask)
812 cpumask_t cmask = mycpu->gd_cpumask;
814 end_va = va + count * PAGE_SIZE;
816 while (va < end_va) {
821 * Install the new PTE. If the pte changed from the prior
822 * mapping we must reset the cpu mask and invalidate the page.
823 * If the pte is the same but we have not seen it on the
824 * current cpu, invlpg the existing mapping. Otherwise the
825 * entry is optimal and no invalidation is required.
827 pte = (unsigned *)vtopte(va);
828 pteval = VM_PAGE_TO_PHYS(*m) | PG_A | PG_RW | PG_V | pgeflag;
829 if (*pte != pteval) {
832 cpu_invlpg((void *)va);
833 } else if ((*mask & cmask) == 0) {
834 cpu_invlpg((void *)va);
843 * this routine jerks page mappings from the
844 * kernel -- it is meant only for temporary mappings.
847 pmap_qremove(vm_offset_t va, int count)
851 end_va = va + count*PAGE_SIZE;
853 while (va < end_va) {
856 pte = (unsigned *)vtopte(va);
858 cpu_invlpg((void *)va);
867 * This routine works like vm_page_lookup() but also blocks as long as the
868 * page is busy. This routine does not busy the page it returns.
870 * Unless the caller is managing objects whos pages are in a known state,
871 * the call should be made at splvm() so the page's object association
872 * remains valid on return.
875 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
880 m = vm_page_lookup(object, pindex);
881 if (m && vm_page_sleep_busy(m, FALSE, "pplookp"))
887 * Create a new thread and optionally associate it with a (new) process.
888 * NOTE! the new thread's cpu may not equal the current cpu.
891 pmap_init_thread(thread_t td)
893 td->td_pcb = (struct pcb *)(td->td_kstack + UPAGES * PAGE_SIZE) - 1;
894 td->td_savefpu = &td->td_pcb->pcb_save;
895 td->td_sp = (char *)td->td_pcb - 16;
899 * Create the UPAGES for a new process.
900 * This routine directly affects the fork perf for a process.
903 pmap_init_proc(struct proc *p, struct thread *td)
905 p->p_addr = (void *)td->td_kstack;
908 td->td_switch = cpu_heavy_switch;
912 bzero(p->p_addr, sizeof(*p->p_addr));
916 * Dispose the UPAGES for a process that has exited.
917 * This routine directly impacts the exit perf of a process.
920 pmap_dispose_proc(struct proc *p)
924 KASSERT(p->p_lock == 0, ("attempt to dispose referenced proc! %p", p));
926 if ((td = p->p_thread) != NULL) {
935 * Allow the UPAGES for a process to be prejudicially paged out.
938 pmap_swapout_proc(struct proc *p)
946 upobj = p->p_upages_obj;
949 * Unwiring the pages allow them to be paged to their backing store
952 * splvm() protection not required since nobody will be messing with
955 for (i = 0; i < UPAGES; i++) {
956 if ((m = vm_page_lookup(upobj, i)) == NULL)
957 panic("pmap_swapout_proc: upage already missing???");
959 vm_page_unwire(m, 0);
960 pmap_kremove((vm_offset_t)p->p_addr + (PAGE_SIZE * i));
966 * Bring the UPAGES for a specified process back in.
969 pmap_swapin_proc(struct proc *p)
977 * splvm() protection not required since nobody will be messing with
980 upobj = p->p_upages_obj;
981 for (i = 0; i < UPAGES; i++) {
982 m = vm_page_grab(upobj, i, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
984 pmap_kenter((vm_offset_t)p->p_addr + (i * PAGE_SIZE),
987 if (m->valid != VM_PAGE_BITS_ALL) {
988 rv = vm_pager_get_pages(upobj, &m, 1, 0);
989 if (rv != VM_PAGER_OK)
990 panic("pmap_swapin_proc: cannot get upages for proc: %d\n", p->p_pid);
991 m = vm_page_lookup(upobj, i);
992 m->valid = VM_PAGE_BITS_ALL;
996 vm_page_flag_set(m, PG_MAPPED | PG_WRITEABLE);
1001 /***************************************************
1002 * Page table page management routines.....
1003 ***************************************************/
1006 * This routine unholds page table pages, and if the hold count
1007 * drops to zero, then it decrements the wire count.
1010 _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, pmap_inval_info_t info)
1012 pmap_inval_flush(info);
1013 while (vm_page_sleep_busy(m, FALSE, "pmuwpt"))
1016 if (m->hold_count == 0) {
1019 * unmap the page table page
1021 pmap_inval_add(info, pmap, -1);
1022 pmap->pm_pdir[m->pindex] = 0;
1023 --pmap->pm_stats.resident_count;
1024 if ((((unsigned)pmap->pm_pdir[PTDPTDI]) & PG_FRAME) ==
1025 (((unsigned) PTDpde) & PG_FRAME)) {
1027 * Do a invltlb to make the invalidated mapping
1028 * take effect immediately.
1030 pteva = UPT_MIN_ADDRESS + i386_ptob(m->pindex);
1033 if (pmap->pm_ptphint == m)
1034 pmap->pm_ptphint = NULL;
1037 * If the page is finally unwired, simply free it.
1040 if (m->wire_count == 0) {
1043 vm_page_free_zero(m);
1044 --vmstats.v_wire_count;
1051 static PMAP_INLINE int
1052 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, pmap_inval_info_t info)
1055 if (m->hold_count == 0)
1056 return _pmap_unwire_pte_hold(pmap, m, info);
1062 * After removing a page table entry, this routine is used to
1063 * conditionally free the page, and manage the hold/wire counts.
1066 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte,
1067 pmap_inval_info_t info)
1070 if (va >= UPT_MIN_ADDRESS)
1074 ptepindex = (va >> PDRSHIFT);
1075 if (pmap->pm_ptphint &&
1076 (pmap->pm_ptphint->pindex == ptepindex)) {
1077 mpte = pmap->pm_ptphint;
1079 pmap_inval_flush(info);
1080 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1081 pmap->pm_ptphint = mpte;
1085 return pmap_unwire_pte_hold(pmap, mpte, info);
1089 pmap_pinit0(struct pmap *pmap)
1092 (pd_entry_t *)kmem_alloc_pageable(kernel_map, PAGE_SIZE);
1093 pmap_kenter((vm_offset_t)pmap->pm_pdir, (vm_offset_t) IdlePTD);
1095 pmap->pm_active = 0;
1096 pmap->pm_ptphint = NULL;
1097 TAILQ_INIT(&pmap->pm_pvlist);
1098 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1102 * Initialize a preallocated and zeroed pmap structure,
1103 * such as one in a vmspace structure.
1106 pmap_pinit(struct pmap *pmap)
1111 * No need to allocate page table space yet but we do need a valid
1112 * page directory table.
1114 if (pmap->pm_pdir == NULL) {
1116 (pd_entry_t *)kmem_alloc_pageable(kernel_map, PAGE_SIZE);
1120 * allocate object for the ptes
1122 if (pmap->pm_pteobj == NULL)
1123 pmap->pm_pteobj = vm_object_allocate( OBJT_DEFAULT, PTDPTDI + 1);
1126 * allocate the page directory page
1128 ptdpg = vm_page_grab( pmap->pm_pteobj, PTDPTDI,
1129 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1131 ptdpg->wire_count = 1;
1132 ++vmstats.v_wire_count;
1135 vm_page_flag_clear(ptdpg, PG_MAPPED | PG_BUSY); /* not usually mapped*/
1136 ptdpg->valid = VM_PAGE_BITS_ALL;
1138 pmap_kenter((vm_offset_t)pmap->pm_pdir, VM_PAGE_TO_PHYS(ptdpg));
1139 if ((ptdpg->flags & PG_ZERO) == 0)
1140 bzero(pmap->pm_pdir, PAGE_SIZE);
1142 pmap->pm_pdir[MPPTDI] = PTD[MPPTDI];
1144 /* install self-referential address mapping entry */
1145 *(unsigned *) (pmap->pm_pdir + PTDPTDI) =
1146 VM_PAGE_TO_PHYS(ptdpg) | PG_V | PG_RW | PG_A | PG_M;
1149 pmap->pm_active = 0;
1150 pmap->pm_ptphint = NULL;
1151 TAILQ_INIT(&pmap->pm_pvlist);
1152 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1156 * Wire in kernel global address entries. To avoid a race condition
1157 * between pmap initialization and pmap_growkernel, this procedure
1158 * should be called after the vmspace is attached to the process
1159 * but before this pmap is activated.
1162 pmap_pinit2(struct pmap *pmap)
1164 /* XXX copies current process, does not fill in MPPTDI */
1165 bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * PTESIZE);
1169 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1171 unsigned *pde = (unsigned *) pmap->pm_pdir;
1173 * This code optimizes the case of freeing non-busy
1174 * page-table pages. Those pages are zero now, and
1175 * might as well be placed directly into the zero queue.
1177 if (vm_page_sleep_busy(p, FALSE, "pmaprl"))
1183 * Remove the page table page from the processes address space.
1186 pmap->pm_stats.resident_count--;
1188 if (p->hold_count) {
1189 panic("pmap_release: freeing held page table page");
1192 * Page directory pages need to have the kernel
1193 * stuff cleared, so they can go into the zero queue also.
1195 if (p->pindex == PTDPTDI) {
1196 bzero(pde + KPTDI, nkpt * PTESIZE);
1199 pmap_kremove((vm_offset_t)pmap->pm_pdir);
1202 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1203 pmap->pm_ptphint = NULL;
1206 vmstats.v_wire_count--;
1207 vm_page_free_zero(p);
1212 * this routine is called if the page table page is not
1216 _pmap_allocpte(pmap_t pmap, unsigned ptepindex)
1218 vm_offset_t pteva, ptepa;
1222 * Find or fabricate a new pagetable page
1224 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1225 VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1227 KASSERT(m->queue == PQ_NONE,
1228 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1230 if (m->wire_count == 0)
1231 vmstats.v_wire_count++;
1235 * Increment the hold count for the page table page
1236 * (denoting a new mapping.)
1241 * Map the pagetable page into the process address space, if
1242 * it isn't already there.
1245 pmap->pm_stats.resident_count++;
1247 ptepa = VM_PAGE_TO_PHYS(m);
1248 pmap->pm_pdir[ptepindex] =
1249 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_A | PG_M);
1252 * Set the page table hint
1254 pmap->pm_ptphint = m;
1257 * Try to use the new mapping, but if we cannot, then
1258 * do it with the routine that maps the page explicitly.
1260 if ((m->flags & PG_ZERO) == 0) {
1261 if ((((unsigned)pmap->pm_pdir[PTDPTDI]) & PG_FRAME) ==
1262 (((unsigned) PTDpde) & PG_FRAME)) {
1263 pteva = UPT_MIN_ADDRESS + i386_ptob(ptepindex);
1264 bzero((caddr_t) pteva, PAGE_SIZE);
1266 pmap_zero_page(ptepa);
1270 m->valid = VM_PAGE_BITS_ALL;
1271 vm_page_flag_clear(m, PG_ZERO);
1272 vm_page_flag_set(m, PG_MAPPED);
1279 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1286 * Calculate pagetable page index
1288 ptepindex = va >> PDRSHIFT;
1291 * Get the page directory entry
1293 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1296 * This supports switching from a 4MB page to a
1299 if (ptepa & PG_PS) {
1300 pmap->pm_pdir[ptepindex] = 0;
1307 * If the page table page is mapped, we just increment the
1308 * hold count, and activate it.
1312 * In order to get the page table page, try the
1315 if (pmap->pm_ptphint &&
1316 (pmap->pm_ptphint->pindex == ptepindex)) {
1317 m = pmap->pm_ptphint;
1319 m = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1320 pmap->pm_ptphint = m;
1326 * Here if the pte page isn't mapped, or if it has been deallocated.
1328 return _pmap_allocpte(pmap, ptepindex);
1332 /***************************************************
1333 * Pmap allocation/deallocation routines.
1334 ***************************************************/
1337 * Release any resources held by the given physical map.
1338 * Called when a pmap initialized by pmap_pinit is being released.
1339 * Should only be called if the map contains no valid mappings.
1342 pmap_release(struct pmap *pmap)
1344 vm_page_t p,n,ptdpg;
1345 vm_object_t object = pmap->pm_pteobj;
1349 #if defined(DIAGNOSTIC)
1350 if (object->ref_count != 1)
1351 panic("pmap_release: pteobj reference count != 1");
1357 curgeneration = object->generation;
1358 for (p = TAILQ_FIRST(&object->memq); p != NULL; p = n) {
1359 n = TAILQ_NEXT(p, listq);
1360 if (p->pindex == PTDPTDI) {
1365 if (!pmap_release_free_page(pmap, p) &&
1366 (object->generation != curgeneration)) {
1374 if (ptdpg && !pmap_release_free_page(pmap, ptdpg))
1379 kvm_size(SYSCTL_HANDLER_ARGS)
1381 unsigned long ksize = VM_MAX_KERNEL_ADDRESS - KERNBASE;
1383 return sysctl_handle_long(oidp, &ksize, 0, req);
1385 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD,
1386 0, 0, kvm_size, "IU", "Size of KVM");
1389 kvm_free(SYSCTL_HANDLER_ARGS)
1391 unsigned long kfree = VM_MAX_KERNEL_ADDRESS - kernel_vm_end;
1393 return sysctl_handle_long(oidp, &kfree, 0, req);
1395 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD,
1396 0, 0, kvm_free, "IU", "Amount of KVM free");
1399 * grow the number of kernel page table entries, if needed
1402 pmap_growkernel(vm_offset_t addr)
1407 vm_offset_t ptppaddr;
1412 if (kernel_vm_end == 0) {
1413 kernel_vm_end = KERNBASE;
1415 while (pdir_pde(PTD, kernel_vm_end)) {
1416 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1420 addr = (addr + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1421 while (kernel_vm_end < addr) {
1422 if (pdir_pde(PTD, kernel_vm_end)) {
1423 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1428 * This index is bogus, but out of the way
1430 nkpg = vm_page_alloc(kptobj, nkpt,
1431 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM | VM_ALLOC_INTERRUPT);
1433 panic("pmap_growkernel: no memory to grow kernel");
1438 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1439 pmap_zero_page(ptppaddr);
1440 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
1441 pdir_pde(PTD, kernel_vm_end) = newpdir;
1443 FOREACH_PROC_IN_SYSTEM(p) {
1445 pmap = vmspace_pmap(p->p_vmspace);
1446 *pmap_pde(pmap, kernel_vm_end) = newpdir;
1449 *pmap_pde(kernel_pmap, kernel_vm_end) = newpdir;
1450 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1456 * Retire the given physical map from service.
1457 * Should only be called if the map contains
1458 * no valid mappings.
1461 pmap_destroy(pmap_t pmap)
1468 count = --pmap->pm_count;
1471 panic("destroying a pmap is not yet implemented");
1476 * Add a reference to the specified pmap.
1479 pmap_reference(pmap_t pmap)
1486 /***************************************************
1487 * page management routines.
1488 ***************************************************/
1491 * free the pv_entry back to the free list. This function may be
1492 * called from an interrupt.
1494 static PMAP_INLINE void
1495 free_pv_entry(pv_entry_t pv)
1502 * get a new pv_entry, allocating a block from the system
1503 * when needed. This function may be called from an interrupt.
1509 if (pv_entry_high_water &&
1510 (pv_entry_count > pv_entry_high_water) &&
1511 (pmap_pagedaemon_waken == 0)) {
1512 pmap_pagedaemon_waken = 1;
1513 wakeup (&vm_pages_needed);
1515 return zalloc(pvzone);
1519 * This routine is very drastic, but can save the system
1527 static int warningdone=0;
1529 if (pmap_pagedaemon_waken == 0)
1532 if (warningdone < 5) {
1533 printf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1537 for(i = 0; i < vm_page_array_size; i++) {
1538 m = &vm_page_array[i];
1539 if (m->wire_count || m->hold_count || m->busy ||
1540 (m->flags & PG_BUSY))
1544 pmap_pagedaemon_waken = 0;
1549 * If it is the first entry on the list, it is actually
1550 * in the header and we must copy the following entry up
1551 * to the header. Otherwise we must search the list for
1552 * the entry. In either case we free the now unused entry.
1555 pmap_remove_entry(struct pmap *pmap, vm_page_t m,
1556 vm_offset_t va, pmap_inval_info_t info)
1563 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1564 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1565 if (pmap == pv->pv_pmap && va == pv->pv_va)
1569 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1570 if (va == pv->pv_va)
1577 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem, info);
1578 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1579 m->md.pv_list_count--;
1580 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
1581 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1582 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1590 * Create a pv entry for page at pa for
1594 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
1600 pv = get_pv_entry();
1605 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1606 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1607 m->md.pv_list_count++;
1613 * pmap_remove_pte: do the things to unmap a page in a process
1616 pmap_remove_pte(struct pmap *pmap, unsigned *ptq, vm_offset_t va,
1617 pmap_inval_info_t info)
1622 pmap_inval_add(info, pmap, va);
1623 oldpte = loadandclear(ptq);
1625 pmap->pm_stats.wired_count -= 1;
1627 * Machines that don't support invlpg, also don't support
1628 * PG_G. XXX PG_G is disabled for SMP so don't worry about
1632 cpu_invlpg((void *)va);
1633 pmap->pm_stats.resident_count -= 1;
1634 if (oldpte & PG_MANAGED) {
1635 m = PHYS_TO_VM_PAGE(oldpte);
1636 if (oldpte & PG_M) {
1637 #if defined(PMAP_DIAGNOSTIC)
1638 if (pmap_nw_modified((pt_entry_t) oldpte)) {
1640 "pmap_remove: modified page not writable: va: 0x%x, pte: 0x%x\n",
1644 if (pmap_track_modified(va))
1648 vm_page_flag_set(m, PG_REFERENCED);
1649 return pmap_remove_entry(pmap, m, va, info);
1651 return pmap_unuse_pt(pmap, va, NULL, info);
1660 * Remove a single page from a process address space.
1662 * This function may not be called from an interrupt if the pmap is
1666 pmap_remove_page(struct pmap *pmap, vm_offset_t va, pmap_inval_info_t info)
1671 * if there is no pte for this address, just skip it!!! Otherwise
1672 * get a local va for mappings for this pmap and remove the entry.
1674 if (*pmap_pde(pmap, va) != 0) {
1675 ptq = get_ptbase(pmap) + i386_btop(va);
1677 pmap_remove_pte(pmap, ptq, va, info);
1685 * Remove the given range of addresses from the specified map.
1687 * It is assumed that the start and end are properly
1688 * rounded to the page size.
1690 * This function may not be called from an interrupt if the pmap is
1694 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
1698 vm_offset_t ptpaddr;
1699 vm_offset_t sindex, eindex;
1700 struct pmap_inval_info info;
1705 if (pmap->pm_stats.resident_count == 0)
1708 pmap_inval_init(&info);
1711 * special handling of removing one page. a very
1712 * common operation and easy to short circuit some
1715 if (((sva + PAGE_SIZE) == eva) &&
1716 (((unsigned) pmap->pm_pdir[(sva >> PDRSHIFT)] & PG_PS) == 0)) {
1717 pmap_remove_page(pmap, sva, &info);
1718 pmap_inval_flush(&info);
1723 * Get a local virtual address for the mappings that are being
1726 ptbase = get_ptbase(pmap);
1728 sindex = i386_btop(sva);
1729 eindex = i386_btop(eva);
1731 for (; sindex < eindex; sindex = pdnxt) {
1735 * Calculate index for next page table.
1737 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1738 if (pmap->pm_stats.resident_count == 0)
1741 pdirindex = sindex / NPDEPG;
1742 if (((ptpaddr = (unsigned) pmap->pm_pdir[pdirindex]) & PG_PS) != 0) {
1743 pmap_inval_add(&info, pmap, -1);
1744 pmap->pm_pdir[pdirindex] = 0;
1745 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1750 * Weed out invalid mappings. Note: we assume that the page
1751 * directory table is always allocated, and in kernel virtual.
1757 * Limit our scan to either the end of the va represented
1758 * by the current page table page, or to the end of the
1759 * range being removed.
1761 if (pdnxt > eindex) {
1765 for (; sindex != pdnxt; sindex++) {
1767 if (ptbase[sindex] == 0)
1769 va = i386_ptob(sindex);
1770 if (pmap_remove_pte(pmap, ptbase + sindex, va, &info))
1774 pmap_inval_flush(&info);
1780 * Removes this physical page from all physical maps in which it resides.
1781 * Reflects back modify bits to the pager.
1783 * This routine may not be called from an interrupt.
1787 pmap_remove_all(vm_page_t m)
1789 struct pmap_inval_info info;
1790 unsigned *pte, tpte;
1794 #if defined(PMAP_DIAGNOSTIC)
1796 * XXX this makes pmap_page_protect(NONE) illegal for non-managed
1799 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
1800 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%08llx", (long long)VM_PAGE_TO_PHYS(m));
1804 pmap_inval_init(&info);
1806 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
1807 pv->pv_pmap->pm_stats.resident_count--;
1809 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
1810 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
1812 tpte = loadandclear(pte);
1814 pv->pv_pmap->pm_stats.wired_count--;
1817 vm_page_flag_set(m, PG_REFERENCED);
1820 * Update the vm_page_t clean and reference bits.
1823 #if defined(PMAP_DIAGNOSTIC)
1824 if (pmap_nw_modified((pt_entry_t) tpte)) {
1826 "pmap_remove_all: modified page not writable: va: 0x%x, pte: 0x%x\n",
1830 if (pmap_track_modified(pv->pv_va))
1833 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
1834 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1835 m->md.pv_list_count--;
1836 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem, &info);
1840 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1842 pmap_inval_flush(&info);
1848 * Set the physical protection on the specified range of this map
1851 * This function may not be called from an interrupt if the map is
1852 * not the kernel_pmap.
1855 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
1858 vm_offset_t pdnxt, ptpaddr;
1859 vm_pindex_t sindex, eindex;
1860 pmap_inval_info info;
1865 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1866 pmap_remove(pmap, sva, eva);
1870 if (prot & VM_PROT_WRITE)
1873 pmap_inval_init(&info);
1875 ptbase = get_ptbase(pmap);
1877 sindex = i386_btop(sva);
1878 eindex = i386_btop(eva);
1880 for (; sindex < eindex; sindex = pdnxt) {
1884 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1886 pdirindex = sindex / NPDEPG;
1887 if (((ptpaddr = (unsigned) pmap->pm_pdir[pdirindex]) & PG_PS) != 0) {
1888 pmap_inval_add(&info, pmap, -1);
1889 (unsigned) pmap->pm_pdir[pdirindex] &= ~(PG_M|PG_RW);
1890 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1895 * Weed out invalid mappings. Note: we assume that the page
1896 * directory table is always allocated, and in kernel virtual.
1901 if (pdnxt > eindex) {
1905 for (; sindex != pdnxt; sindex++) {
1910 /* XXX this isn't optimal */
1911 pmap_inval_add(&info, pmap, i386_ptob(sindex));
1912 pbits = ptbase[sindex];
1914 if (pbits & PG_MANAGED) {
1917 m = PHYS_TO_VM_PAGE(pbits);
1918 vm_page_flag_set(m, PG_REFERENCED);
1922 if (pmap_track_modified(i386_ptob(sindex))) {
1924 m = PHYS_TO_VM_PAGE(pbits);
1933 if (pbits != ptbase[sindex]) {
1934 ptbase[sindex] = pbits;
1938 pmap_inval_flush(&info);
1942 * Insert the given physical page (p) at
1943 * the specified virtual address (v) in the
1944 * target physical map with the protection requested.
1946 * If specified, the page will be wired down, meaning
1947 * that the related pte can not be reclaimed.
1949 * NB: This is the only routine which MAY NOT lazy-evaluate
1950 * or lose information. That is, this routine must actually
1951 * insert this page into the given map NOW.
1954 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1960 vm_offset_t origpte, newpte;
1962 pmap_inval_info info;
1968 #ifdef PMAP_DIAGNOSTIC
1969 if (va > VM_MAX_KERNEL_ADDRESS)
1970 panic("pmap_enter: toobig");
1971 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
1972 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%x)", va);
1977 * In the case that a page table page is not
1978 * resident, we are creating it here.
1980 if (va < UPT_MIN_ADDRESS) {
1981 mpte = pmap_allocpte(pmap, va);
1984 pmap_inval_init(&info);
1985 pte = pmap_pte(pmap, va);
1988 * Page Directory table entry not valid, we need a new PT page
1991 panic("pmap_enter: invalid page directory pdir=%x, va=0x%x\n",
1992 (unsigned) pmap->pm_pdir[PTDPTDI], va);
1995 pa = VM_PAGE_TO_PHYS(m) & PG_FRAME;
1996 pmap_inval_add(&info, pmap, va); /* XXX non-optimal */
1997 origpte = *(vm_offset_t *)pte;
1998 opa = origpte & PG_FRAME;
2000 if (origpte & PG_PS)
2001 panic("pmap_enter: attempted pmap_enter on 4MB page");
2004 * Mapping has not changed, must be protection or wiring change.
2006 if (origpte && (opa == pa)) {
2008 * Wiring change, just update stats. We don't worry about
2009 * wiring PT pages as they remain resident as long as there
2010 * are valid mappings in them. Hence, if a user page is wired,
2011 * the PT page will be also.
2013 if (wired && ((origpte & PG_W) == 0))
2014 pmap->pm_stats.wired_count++;
2015 else if (!wired && (origpte & PG_W))
2016 pmap->pm_stats.wired_count--;
2018 #if defined(PMAP_DIAGNOSTIC)
2019 if (pmap_nw_modified((pt_entry_t) origpte)) {
2021 "pmap_enter: modified page not writable: va: 0x%x, pte: 0x%x\n",
2027 * Remove extra pte reference
2032 if ((prot & VM_PROT_WRITE) && (origpte & PG_V)) {
2033 if ((origpte & PG_RW) == 0)
2035 pmap_inval_flush(&info);
2040 * We might be turning off write access to the page,
2041 * so we go ahead and sense modify status.
2043 if (origpte & PG_MANAGED) {
2044 if ((origpte & PG_M) && pmap_track_modified(va)) {
2046 om = PHYS_TO_VM_PAGE(opa);
2054 * Mapping has changed, invalidate old range and fall through to
2055 * handle validating new mapping.
2059 err = pmap_remove_pte(pmap, pte, va, &info);
2061 panic("pmap_enter: pte vanished, va: 0x%x", va);
2065 * Enter on the PV list if part of our managed memory. Note that we
2066 * raise IPL while manipulating pv_table since pmap_enter can be
2067 * called at interrupt time.
2069 if (pmap_initialized &&
2070 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2071 pmap_insert_entry(pmap, va, mpte, m);
2076 * Increment counters
2078 pmap->pm_stats.resident_count++;
2080 pmap->pm_stats.wired_count++;
2084 * Now validate mapping with desired protection/wiring.
2086 newpte = (vm_offset_t) (pa | pte_prot(pmap, prot) | PG_V);
2090 if (va < UPT_MIN_ADDRESS)
2092 if (pmap == kernel_pmap)
2096 * if the mapping or permission bits are different, we need
2097 * to update the pte.
2099 if ((origpte & ~(PG_M|PG_A)) != newpte) {
2100 *pte = newpte | PG_A;
2102 pmap_inval_flush(&info);
2106 * this code makes some *MAJOR* assumptions:
2107 * 1. Current pmap & pmap exists.
2110 * 4. No page table pages.
2111 * 5. Tlbflush is deferred to calling procedure.
2112 * 6. Page IS managed.
2113 * but is *MUCH* faster than pmap_enter...
2117 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_page_t mpte)
2121 pmap_inval_info info;
2123 pmap_inval_init(&info);
2126 * In the case that a page table page is not
2127 * resident, we are creating it here.
2129 if (va < UPT_MIN_ADDRESS) {
2134 * Calculate pagetable page index
2136 ptepindex = va >> PDRSHIFT;
2137 if (mpte && (mpte->pindex == ptepindex)) {
2142 * Get the page directory entry
2144 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
2147 * If the page table page is mapped, we just increment
2148 * the hold count, and activate it.
2152 panic("pmap_enter_quick: unexpected mapping into 4MB page");
2153 if (pmap->pm_ptphint &&
2154 (pmap->pm_ptphint->pindex == ptepindex)) {
2155 mpte = pmap->pm_ptphint;
2157 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
2158 pmap->pm_ptphint = mpte;
2164 mpte = _pmap_allocpte(pmap, ptepindex);
2172 * This call to vtopte makes the assumption that we are
2173 * entering the page into the current pmap. In order to support
2174 * quick entry into any pmap, one would likely use pmap_pte_quick.
2175 * But that isn't as quick as vtopte.
2177 pte = (unsigned *)vtopte(va);
2180 pmap_unwire_pte_hold(pmap, mpte, &info);
2185 * Enter on the PV list if part of our managed memory. Note that we
2186 * raise IPL while manipulating pv_table since pmap_enter can be
2187 * called at interrupt time.
2189 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0)
2190 pmap_insert_entry(pmap, va, mpte, m);
2193 * Increment counters
2195 pmap->pm_stats.resident_count++;
2197 pa = VM_PAGE_TO_PHYS(m);
2200 * Now validate mapping with RO protection
2202 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2203 *pte = pa | PG_V | PG_U;
2205 *pte = pa | PG_V | PG_U | PG_MANAGED;
2211 * Make a temporary mapping for a physical address. This is only intended
2212 * to be used for panic dumps.
2215 pmap_kenter_temporary(vm_paddr_t pa, int i)
2217 pmap_kenter((vm_offset_t)crashdumpmap + (i * PAGE_SIZE), pa);
2218 return ((void *)crashdumpmap);
2221 #define MAX_INIT_PT (96)
2224 * This routine preloads the ptes for a given object into the specified pmap.
2225 * This eliminates the blast of soft faults on process startup and
2226 * immediately after an mmap.
2229 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2230 vm_object_t object, vm_pindex_t pindex,
2231 vm_size_t size, int limit)
2239 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2244 * XXX you must be joking, entering PTE's into a user page table
2245 * without any accounting? This could result in the page table
2246 * being freed while it still contains mappings (free with PG_ZERO
2247 * assumption leading to a non-zero page being marked PG_ZERO).
2250 * This code maps large physical mmap regions into the
2251 * processor address space. Note that some shortcuts
2252 * are taken, but the code works.
2255 (object->type == OBJT_DEVICE) &&
2256 ((addr & (NBPDR - 1)) == 0) &&
2257 ((size & (NBPDR - 1)) == 0) ) {
2260 unsigned int ptepindex;
2264 if (pmap->pm_pdir[ptepindex = (addr >> PDRSHIFT)])
2268 p = vm_page_lookup(object, pindex);
2269 if (p && vm_page_sleep_busy(p, FALSE, "init4p"))
2273 p = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL);
2278 if (vm_pager_get_pages(object, m, 1, 0) != VM_PAGER_OK) {
2283 p = vm_page_lookup(object, pindex);
2287 ptepa = (vm_offset_t) VM_PAGE_TO_PHYS(p);
2288 if (ptepa & (NBPDR - 1)) {
2292 p->valid = VM_PAGE_BITS_ALL;
2294 pmap->pm_stats.resident_count += size >> PAGE_SHIFT;
2295 npdes = size >> PDRSHIFT;
2296 for (i = 0; i < npdes; i++) {
2297 pmap->pm_pdir[ptepindex] =
2298 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_PS);
2302 vm_page_flag_set(p, PG_MAPPED);
2309 psize = i386_btop(size);
2311 if ((object->type != OBJT_VNODE) ||
2312 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2313 (object->resident_page_count > MAX_INIT_PT))) {
2317 if (psize + pindex > object->size) {
2318 if (object->size < pindex)
2320 psize = object->size - pindex;
2325 * If we are processing a major portion of the object, then scan the
2328 * We cannot safely scan the object's memq unless we are at splvm(),
2329 * since interrupts can remove pages from objects.
2333 if (psize > (object->resident_page_count >> 2)) {
2336 for (p = TAILQ_FIRST(&object->memq);
2337 objpgs > 0 && p != NULL;
2338 p = TAILQ_NEXT(p, listq)
2341 if (tmpidx < pindex)
2344 if (tmpidx >= psize)
2348 * don't allow an madvise to blow away our really
2349 * free pages allocating pv entries.
2351 if ((limit & MAP_PREFAULT_MADVISE) &&
2352 vmstats.v_free_count < vmstats.v_free_reserved) {
2355 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2357 (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2358 if ((p->queue - p->pc) == PQ_CACHE)
2359 vm_page_deactivate(p);
2361 mpte = pmap_enter_quick(pmap,
2362 addr + i386_ptob(tmpidx), p, mpte);
2363 vm_page_flag_set(p, PG_MAPPED);
2370 * else lookup the pages one-by-one.
2372 for (tmpidx = 0; tmpidx < psize; tmpidx += 1) {
2374 * don't allow an madvise to blow away our really
2375 * free pages allocating pv entries.
2377 if ((limit & MAP_PREFAULT_MADVISE) &&
2378 vmstats.v_free_count < vmstats.v_free_reserved) {
2381 p = vm_page_lookup(object, tmpidx + pindex);
2383 ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2385 (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2386 if ((p->queue - p->pc) == PQ_CACHE)
2387 vm_page_deactivate(p);
2389 mpte = pmap_enter_quick(pmap,
2390 addr + i386_ptob(tmpidx), p, mpte);
2391 vm_page_flag_set(p, PG_MAPPED);
2400 * pmap_prefault provides a quick way of clustering pagefaults into a
2401 * processes address space. It is a "cousin" of pmap_object_init_pt,
2402 * except it runs at page fault time instead of mmap time.
2406 #define PAGEORDER_SIZE (PFBAK+PFFOR)
2408 static int pmap_prefault_pageorder[] = {
2409 -PAGE_SIZE, PAGE_SIZE,
2410 -2 * PAGE_SIZE, 2 * PAGE_SIZE,
2411 -3 * PAGE_SIZE, 3 * PAGE_SIZE
2412 -4 * PAGE_SIZE, 4 * PAGE_SIZE
2416 pmap_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry)
2426 if (!curproc || (pmap != vmspace_pmap(curproc->p_vmspace)))
2429 object = entry->object.vm_object;
2431 starta = addra - PFBAK * PAGE_SIZE;
2432 if (starta < entry->start)
2433 starta = entry->start;
2434 else if (starta > addra)
2438 * splvm() protection is required to maintain the page/object
2439 * association, interrupts can free pages and remove them from
2444 for (i = 0; i < PAGEORDER_SIZE; i++) {
2445 vm_object_t lobject;
2448 addr = addra + pmap_prefault_pageorder[i];
2449 if (addr > addra + (PFFOR * PAGE_SIZE))
2452 if (addr < starta || addr >= entry->end)
2455 if ((*pmap_pde(pmap, addr)) == NULL)
2458 pte = (unsigned *) vtopte(addr);
2462 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2465 for (m = vm_page_lookup(lobject, pindex);
2466 (!m && (lobject->type == OBJT_DEFAULT) &&
2467 (lobject->backing_object));
2468 lobject = lobject->backing_object
2470 if (lobject->backing_object_offset & PAGE_MASK)
2472 pindex += (lobject->backing_object_offset >> PAGE_SHIFT);
2473 m = vm_page_lookup(lobject->backing_object, pindex);
2477 * give-up when a page is not in memory
2482 if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2484 (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2486 if ((m->queue - m->pc) == PQ_CACHE) {
2487 vm_page_deactivate(m);
2490 mpte = pmap_enter_quick(pmap, addr, m, mpte);
2491 vm_page_flag_set(m, PG_MAPPED);
2499 * Routine: pmap_change_wiring
2500 * Function: Change the wiring attribute for a map/virtual-address
2502 * In/out conditions:
2503 * The mapping must already exist in the pmap.
2506 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2513 pte = pmap_pte(pmap, va);
2515 if (wired && !pmap_pte_w(pte))
2516 pmap->pm_stats.wired_count++;
2517 else if (!wired && pmap_pte_w(pte))
2518 pmap->pm_stats.wired_count--;
2521 * Wiring is not a hardware characteristic so there is no need to
2522 * invalidate TLB. However, in an SMP environment we must use
2523 * a locked bus cycle to update the pte (if we are not using
2524 * the pmap_inval_*() API that is)... it's ok to do this for simple
2529 atomic_set_int(pte, PG_W);
2531 atomic_clear_int(pte, PG_W);
2534 atomic_set_int_nonlocked(pte, PG_W);
2536 atomic_clear_int_nonlocked(pte, PG_W);
2543 * Copy the range specified by src_addr/len
2544 * from the source map to the range dst_addr/len
2545 * in the destination map.
2547 * This routine is only advisory and need not do anything.
2550 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2551 vm_size_t len, vm_offset_t src_addr)
2553 pmap_inval_info info;
2555 vm_offset_t end_addr = src_addr + len;
2557 unsigned src_frame, dst_frame;
2561 if (dst_addr != src_addr)
2564 src_frame = ((unsigned) src_pmap->pm_pdir[PTDPTDI]) & PG_FRAME;
2565 if (src_frame != (((unsigned) PTDpde) & PG_FRAME)) {
2569 dst_frame = ((unsigned) dst_pmap->pm_pdir[PTDPTDI]) & PG_FRAME;
2570 if (dst_frame != (((unsigned) APTDpde) & PG_FRAME)) {
2571 APTDpde = (pd_entry_t) (dst_frame | PG_RW | PG_V);
2572 /* The page directory is not shared between CPUs */
2575 pmap_inval_init(&info);
2576 pmap_inval_add(&info, dst_pmap, -1);
2577 pmap_inval_add(&info, src_pmap, -1);
2580 * splvm() protection is required to maintain the page/object
2581 * association, interrupts can free pages and remove them from
2585 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
2586 unsigned *src_pte, *dst_pte;
2587 vm_page_t dstmpte, srcmpte;
2588 vm_offset_t srcptepaddr;
2591 if (addr >= UPT_MIN_ADDRESS)
2592 panic("pmap_copy: invalid to pmap_copy page tables\n");
2595 * Don't let optional prefaulting of pages make us go
2596 * way below the low water mark of free pages or way
2597 * above high water mark of used pv entries.
2599 if (vmstats.v_free_count < vmstats.v_free_reserved ||
2600 pv_entry_count > pv_entry_high_water)
2603 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
2604 ptepindex = addr >> PDRSHIFT;
2606 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex];
2607 if (srcptepaddr == 0)
2610 if (srcptepaddr & PG_PS) {
2611 if (dst_pmap->pm_pdir[ptepindex] == 0) {
2612 dst_pmap->pm_pdir[ptepindex] = (pd_entry_t) srcptepaddr;
2613 dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
2618 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
2619 if ((srcmpte == NULL) ||
2620 (srcmpte->hold_count == 0) || (srcmpte->flags & PG_BUSY))
2623 if (pdnxt > end_addr)
2626 src_pte = (unsigned *) vtopte(addr);
2627 dst_pte = (unsigned *) avtopte(addr);
2628 while (addr < pdnxt) {
2632 * we only virtual copy managed pages
2634 if ((ptetemp & PG_MANAGED) != 0) {
2636 * We have to check after allocpte for the
2637 * pte still being around... allocpte can
2640 dstmpte = pmap_allocpte(dst_pmap, addr);
2641 if ((*dst_pte == 0) && (ptetemp = *src_pte)) {
2643 * Clear the modified and
2644 * accessed (referenced) bits
2647 m = PHYS_TO_VM_PAGE(ptetemp);
2648 *dst_pte = ptetemp & ~(PG_M | PG_A);
2649 dst_pmap->pm_stats.resident_count++;
2650 pmap_insert_entry(dst_pmap, addr,
2653 pmap_unwire_pte_hold(dst_pmap, dstmpte, &info);
2655 if (dstmpte->hold_count >= srcmpte->hold_count)
2664 pmap_inval_flush(&info);
2668 * Routine: pmap_kernel
2670 * Returns the physical map handle for the kernel.
2675 return (kernel_pmap);
2681 * Zero the specified PA by mapping the page into KVM and clearing its
2684 * This function may be called from an interrupt and no locking is
2688 pmap_zero_page(vm_paddr_t phys)
2690 struct mdglobaldata *gd = mdcpu;
2693 if (*(int *)gd->gd_CMAP3)
2694 panic("pmap_zero_page: CMAP3 busy");
2695 *(int *)gd->gd_CMAP3 =
2696 PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2697 cpu_invlpg(gd->gd_CADDR3);
2699 #if defined(I686_CPU)
2700 if (cpu_class == CPUCLASS_686)
2701 i686_pagezero(gd->gd_CADDR3);
2704 bzero(gd->gd_CADDR3, PAGE_SIZE);
2705 *(int *) gd->gd_CMAP3 = 0;
2710 * pmap_page_assertzero:
2712 * Assert that a page is empty, panic if it isn't.
2715 pmap_page_assertzero(vm_paddr_t phys)
2717 struct mdglobaldata *gd = mdcpu;
2721 if (*(int *)gd->gd_CMAP3)
2722 panic("pmap_zero_page: CMAP3 busy");
2723 *(int *)gd->gd_CMAP3 =
2724 PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2725 cpu_invlpg(gd->gd_CADDR3);
2726 for (i = 0; i < PAGE_SIZE; i += 4) {
2727 if (*(int *)((char *)gd->gd_CADDR3 + i) != 0) {
2728 panic("pmap_page_assertzero() @ %p not zero!\n",
2729 (void *)gd->gd_CADDR3);
2732 *(int *) gd->gd_CMAP3 = 0;
2739 * Zero part of a physical page by mapping it into memory and clearing
2740 * its contents with bzero.
2742 * off and size may not cover an area beyond a single hardware page.
2745 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
2747 struct mdglobaldata *gd = mdcpu;
2750 if (*(int *) gd->gd_CMAP3)
2751 panic("pmap_zero_page: CMAP3 busy");
2752 *(int *) gd->gd_CMAP3 = PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2753 cpu_invlpg(gd->gd_CADDR3);
2755 #if defined(I686_CPU)
2756 if (cpu_class == CPUCLASS_686 && off == 0 && size == PAGE_SIZE)
2757 i686_pagezero(gd->gd_CADDR3);
2760 bzero((char *)gd->gd_CADDR3 + off, size);
2761 *(int *) gd->gd_CMAP3 = 0;
2768 * Copy the physical page from the source PA to the target PA.
2769 * This function may be called from an interrupt. No locking
2773 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
2775 struct mdglobaldata *gd = mdcpu;
2778 if (*(int *) gd->gd_CMAP1)
2779 panic("pmap_copy_page: CMAP1 busy");
2780 if (*(int *) gd->gd_CMAP2)
2781 panic("pmap_copy_page: CMAP2 busy");
2783 *(int *) gd->gd_CMAP1 = PG_V | (src & PG_FRAME) | PG_A;
2784 *(int *) gd->gd_CMAP2 = PG_V | PG_RW | (dst & PG_FRAME) | PG_A | PG_M;
2786 cpu_invlpg(gd->gd_CADDR1);
2787 cpu_invlpg(gd->gd_CADDR2);
2789 bcopy(gd->gd_CADDR1, gd->gd_CADDR2, PAGE_SIZE);
2791 *(int *) gd->gd_CMAP1 = 0;
2792 *(int *) gd->gd_CMAP2 = 0;
2797 * pmap_copy_page_frag:
2799 * Copy the physical page from the source PA to the target PA.
2800 * This function may be called from an interrupt. No locking
2804 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
2806 struct mdglobaldata *gd = mdcpu;
2809 if (*(int *) gd->gd_CMAP1)
2810 panic("pmap_copy_page: CMAP1 busy");
2811 if (*(int *) gd->gd_CMAP2)
2812 panic("pmap_copy_page: CMAP2 busy");
2814 *(int *) gd->gd_CMAP1 = PG_V | (src & PG_FRAME) | PG_A;
2815 *(int *) gd->gd_CMAP2 = PG_V | PG_RW | (dst & PG_FRAME) | PG_A | PG_M;
2817 cpu_invlpg(gd->gd_CADDR1);
2818 cpu_invlpg(gd->gd_CADDR2);
2820 bcopy((char *)gd->gd_CADDR1 + (src & PAGE_MASK),
2821 (char *)gd->gd_CADDR2 + (dst & PAGE_MASK),
2824 *(int *) gd->gd_CMAP1 = 0;
2825 *(int *) gd->gd_CMAP2 = 0;
2831 * Routine: pmap_pageable
2833 * Make the specified pages (by pmap, offset)
2834 * pageable (or not) as requested.
2836 * A page which is not pageable may not take
2837 * a fault; therefore, its page table entry
2838 * must remain valid for the duration.
2840 * This routine is merely advisory; pmap_enter
2841 * will specify that these pages are to be wired
2842 * down (or not) as appropriate.
2845 pmap_pageable(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, boolean_t pageable)
2850 * Returns true if the pmap's pv is one of the first
2851 * 16 pvs linked to from this page. This count may
2852 * be changed upwards or downwards in the future; it
2853 * is only necessary that true be returned for a small
2854 * subset of pmaps for proper page aging.
2857 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2863 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2868 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2869 if (pv->pv_pmap == pmap) {
2881 #define PMAP_REMOVE_PAGES_CURPROC_ONLY
2883 * Remove all pages from specified address space
2884 * this aids process exit speeds. Also, this code
2885 * is special cased for current process only, but
2886 * can have the more generic (and slightly slower)
2887 * mode enabled. This is much faster than pmap_remove
2888 * in the case of running down an entire address space.
2891 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2893 unsigned *pte, tpte;
2897 pmap_inval_info info;
2899 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2900 if (!curproc || (pmap != vmspace_pmap(curproc->p_vmspace))) {
2901 printf("warning: pmap_remove_pages called with non-current pmap\n");
2906 pmap_inval_init(&info);
2908 for(pv = TAILQ_FIRST(&pmap->pm_pvlist);
2912 if (pv->pv_va >= eva || pv->pv_va < sva) {
2913 npv = TAILQ_NEXT(pv, pv_plist);
2917 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2918 pte = (unsigned *)vtopte(pv->pv_va);
2920 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2922 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
2926 * We cannot remove wired pages from a process' mapping at this time
2929 npv = TAILQ_NEXT(pv, pv_plist);
2934 m = PHYS_TO_VM_PAGE(tpte);
2936 KASSERT(m < &vm_page_array[vm_page_array_size],
2937 ("pmap_remove_pages: bad tpte %x", tpte));
2939 pv->pv_pmap->pm_stats.resident_count--;
2942 * Update the vm_page_t clean and reference bits.
2949 npv = TAILQ_NEXT(pv, pv_plist);
2950 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2952 m->md.pv_list_count--;
2953 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2954 if (TAILQ_FIRST(&m->md.pv_list) == NULL) {
2955 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2958 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem, &info);
2961 pmap_inval_flush(&info);
2966 * pmap_testbit tests bits in pte's
2967 * note that the testbit/changebit routines are inline,
2968 * and a lot of things compile-time evaluate.
2971 pmap_testbit(vm_page_t m, int bit)
2977 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2980 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2985 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2987 * if the bit being tested is the modified bit, then
2988 * mark clean_map and ptes as never
2991 if (bit & (PG_A|PG_M)) {
2992 if (!pmap_track_modified(pv->pv_va))
2996 #if defined(PMAP_DIAGNOSTIC)
2998 printf("Null pmap (tb) at va: 0x%x\n", pv->pv_va);
3002 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3013 * this routine is used to modify bits in ptes
3015 static __inline void
3016 pmap_changebit(vm_page_t m, int bit, boolean_t setem)
3018 struct pmap_inval_info info;
3023 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3026 pmap_inval_init(&info);
3030 * Loop over all current mappings setting/clearing as appropos If
3031 * setting RO do we need to clear the VAC?
3033 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3035 * don't write protect pager mappings
3037 if (!setem && (bit == PG_RW)) {
3038 if (!pmap_track_modified(pv->pv_va))
3042 #if defined(PMAP_DIAGNOSTIC)
3044 printf("Null pmap (cb) at va: 0x%x\n", pv->pv_va);
3050 * Careful here. We can use a locked bus instruction to
3051 * clear PG_A or PG_M safely but we need to synchronize
3052 * with the target cpus when we mess with PG_RW.
3054 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3056 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
3060 atomic_set_int(pte, bit);
3062 atomic_set_int_nonlocked(pte, bit);
3065 vm_offset_t pbits = *(vm_offset_t *)pte;
3072 atomic_clear_int(pte, PG_M|PG_RW);
3074 atomic_clear_int_nonlocked(pte, PG_M|PG_RW);
3078 atomic_clear_int(pte, bit);
3080 atomic_clear_int_nonlocked(pte, bit);
3086 pmap_inval_flush(&info);
3091 * pmap_page_protect:
3093 * Lower the permission for all mappings to a given page.
3096 pmap_page_protect(vm_page_t m, vm_prot_t prot)
3098 if ((prot & VM_PROT_WRITE) == 0) {
3099 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
3100 pmap_changebit(m, PG_RW, FALSE);
3108 pmap_phys_address(int ppn)
3110 return (i386_ptob(ppn));
3114 * pmap_ts_referenced:
3116 * Return a count of reference bits for a page, clearing those bits.
3117 * It is not necessary for every reference bit to be cleared, but it
3118 * is necessary that 0 only be returned when there are truly no
3119 * reference bits set.
3121 * XXX: The exact number of bits to check and clear is a matter that
3122 * should be tested and standardized at some point in the future for
3123 * optimal aging of shared pages.
3126 pmap_ts_referenced(vm_page_t m)
3128 pv_entry_t pv, pvf, pvn;
3133 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3138 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3143 pvn = TAILQ_NEXT(pv, pv_list);
3145 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3147 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3149 if (!pmap_track_modified(pv->pv_va))
3152 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3154 if (pte && (*pte & PG_A)) {
3156 atomic_clear_int(pte, PG_A);
3158 atomic_clear_int_nonlocked(pte, PG_A);
3165 } while ((pv = pvn) != NULL && pv != pvf);
3175 * Return whether or not the specified physical page was modified
3176 * in any physical maps.
3179 pmap_is_modified(vm_page_t m)
3181 return pmap_testbit(m, PG_M);
3185 * Clear the modify bits on the specified physical page.
3188 pmap_clear_modify(vm_page_t m)
3190 pmap_changebit(m, PG_M, FALSE);
3194 * pmap_clear_reference:
3196 * Clear the reference bit on the specified physical page.
3199 pmap_clear_reference(vm_page_t m)
3201 pmap_changebit(m, PG_A, FALSE);
3205 * Miscellaneous support routines follow
3209 i386_protection_init(void)
3213 kp = protection_codes;
3214 for (prot = 0; prot < 8; prot++) {
3216 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE:
3218 * Read access is also 0. There isn't any execute bit,
3219 * so just make it readable.
3221 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE:
3222 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE:
3223 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE:
3226 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE:
3227 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE:
3228 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE:
3229 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
3237 * Map a set of physical memory pages into the kernel virtual
3238 * address space. Return a pointer to where it is mapped. This
3239 * routine is intended to be used for mapping device memory,
3242 * NOTE: we can't use pgeflag unless we invalidate the pages one at
3246 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
3248 vm_offset_t va, tmpva, offset;
3251 offset = pa & PAGE_MASK;
3252 size = roundup(offset + size, PAGE_SIZE);
3254 va = kmem_alloc_nofault(kernel_map, size);
3256 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3259 for (tmpva = va; size > 0;) {
3260 pte = (unsigned *)vtopte(tmpva);
3261 *pte = pa | PG_RW | PG_V; /* | pgeflag; */
3269 return ((void *)(va + offset));
3273 pmap_unmapdev(vm_offset_t va, vm_size_t size)
3275 vm_offset_t base, offset;
3277 base = va & PG_FRAME;
3278 offset = va & PAGE_MASK;
3279 size = roundup(offset + size, PAGE_SIZE);
3280 pmap_qremove(va, size >> PAGE_SHIFT);
3281 kmem_free(kernel_map, base, size);
3285 * perform the pmap work for mincore
3288 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3290 unsigned *ptep, pte;
3294 ptep = pmap_pte(pmap, addr);
3299 if ((pte = *ptep) != 0) {
3302 val = MINCORE_INCORE;
3303 if ((pte & PG_MANAGED) == 0)
3306 pa = pte & PG_FRAME;
3308 m = PHYS_TO_VM_PAGE(pa);
3314 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3316 * Modified by someone
3318 else if (m->dirty || pmap_is_modified(m))
3319 val |= MINCORE_MODIFIED_OTHER;
3324 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3327 * Referenced by someone
3329 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3330 val |= MINCORE_REFERENCED_OTHER;
3331 vm_page_flag_set(m, PG_REFERENCED);
3338 pmap_activate(struct proc *p)
3342 pmap = vmspace_pmap(p->p_vmspace);
3344 atomic_set_int(&pmap->pm_active, 1 << mycpu->gd_cpuid);
3346 pmap->pm_active |= 1;
3348 #if defined(SWTCH_OPTIM_STATS)
3351 p->p_thread->td_pcb->pcb_cr3 = vtophys(pmap->pm_pdir);
3352 load_cr3(p->p_thread->td_pcb->pcb_cr3);
3356 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3359 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3363 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3368 #if defined(PMAP_DEBUG)
3370 pmap_pid_dump(int pid)
3376 FOREACH_PROC_IN_SYSTEM(p) {
3377 if (p->p_pid != pid)
3383 pmap = vmspace_pmap(p->p_vmspace);
3384 for(i=0;i<1024;i++) {
3387 unsigned base = i << PDRSHIFT;
3389 pde = &pmap->pm_pdir[i];
3390 if (pde && pmap_pde_v(pde)) {
3391 for(j=0;j<1024;j++) {
3392 unsigned va = base + (j << PAGE_SHIFT);
3393 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) {
3400 pte = pmap_pte_quick( pmap, va);
3401 if (pte && pmap_pte_v(pte)) {
3405 m = PHYS_TO_VM_PAGE(pa);
3406 printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x",
3407 va, pa, m->hold_count, m->wire_count, m->flags);
3428 static void pads (pmap_t pm);
3429 void pmap_pvdump (vm_paddr_t pa);
3431 /* print address space of pmap*/
3438 if (pm == kernel_pmap)
3440 for (i = 0; i < 1024; i++)
3442 for (j = 0; j < 1024; j++) {
3443 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
3444 if (pm == kernel_pmap && va < KERNBASE)
3446 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS)
3448 ptep = pmap_pte_quick(pm, va);
3449 if (pmap_pte_v(ptep))
3450 printf("%x:%x ", va, *(int *) ptep);
3456 pmap_pvdump(vm_paddr_t pa)
3461 printf("pa %08llx", (long long)pa);
3462 m = PHYS_TO_VM_PAGE(pa);
3463 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3465 printf(" -> pmap %p, va %x, flags %x",
3466 (void *)pv->pv_pmap, pv->pv_va, pv->pv_flags);
3468 printf(" -> pmap %p, va %x", (void *)pv->pv_pmap, pv->pv_va);