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.26 2003/12/20 05:52:26 dillon Exp $
47 * Manages physical address maps.
49 * In addition to hardware address maps, this
50 * module is called upon to provide software-use-only
51 * maps which may or may not be stored in the same
52 * form as hardware maps. These pseudo-maps are
53 * used to store intermediate results from copy
54 * operations to and from address spaces.
56 * Since the information managed by this module is
57 * also stored by the logical address mapping module,
58 * this module may throw away valid virtual-to-physical
59 * mappings at almost any time. However, invalidations
60 * of virtual-to-physical mappings must be done as
63 * In order to cope with hardware architectures which
64 * make virtual-to-physical map invalidates expensive,
65 * this module may delay invalidate or reduced protection
66 * operations until such time as they are actually
67 * necessary. This module is given full information as
68 * to which processors are currently using which maps,
69 * and to when physical maps must be made correct.
72 #include "opt_disable_pse.h"
74 #include "opt_msgbuf.h"
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
80 #include <sys/msgbuf.h>
81 #include <sys/vmmeter.h>
85 #include <vm/vm_param.h>
86 #include <sys/sysctl.h>
88 #include <vm/vm_kern.h>
89 #include <vm/vm_page.h>
90 #include <vm/vm_map.h>
91 #include <vm/vm_object.h>
92 #include <vm/vm_extern.h>
93 #include <vm/vm_pageout.h>
94 #include <vm/vm_pager.h>
95 #include <vm/vm_zone.h>
98 #include <sys/thread2.h>
100 #include <machine/cputypes.h>
101 #include <machine/md_var.h>
102 #include <machine/specialreg.h>
103 #if defined(SMP) || defined(APIC_IO)
104 #include <machine/smp.h>
105 #include <machine/apic.h>
106 #endif /* SMP || APIC_IO */
107 #include <machine/globaldata.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)
138 #define pmap_pte_set_w(pte, v) ((v)?(*(int *)pte |= PG_W):(*(int *)pte &= ~PG_W))
139 #define pmap_pte_set_prot(pte, v) ((*(int *)pte &= ~PG_PROT), (*(int *)pte |= (v)))
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,
201 static void pmap_remove_page (struct pmap *pmap, vm_offset_t va);
202 static int pmap_remove_entry (struct pmap *pmap, vm_page_t m,
204 static boolean_t pmap_testbit (vm_page_t m, int bit);
205 static void pmap_insert_entry (pmap_t pmap, vm_offset_t va,
206 vm_page_t mpte, vm_page_t m);
208 static vm_page_t pmap_allocpte (pmap_t pmap, vm_offset_t va);
210 static int pmap_release_free_page (pmap_t pmap, vm_page_t p);
211 static vm_page_t _pmap_allocpte (pmap_t pmap, unsigned ptepindex);
212 static unsigned * pmap_pte_quick (pmap_t pmap, vm_offset_t va);
213 static vm_page_t pmap_page_lookup (vm_object_t object, vm_pindex_t pindex);
214 static int pmap_unuse_pt (pmap_t, vm_offset_t, vm_page_t);
215 static vm_offset_t pmap_kmem_choose(vm_offset_t addr);
217 static unsigned pdir4mb;
220 * Move the kernel virtual free pointer to the next
221 * 4MB. This is used to help improve performance
222 * by using a large (4MB) page for much of the kernel
223 * (.text, .data, .bss)
226 pmap_kmem_choose(vm_offset_t addr)
228 vm_offset_t newaddr = addr;
230 if (cpu_feature & CPUID_PSE) {
231 newaddr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
240 * Extract the page table entry associated with the given map/virtual
243 * This function may NOT be called from an interrupt.
245 PMAP_INLINE unsigned *
246 pmap_pte(pmap_t pmap, vm_offset_t va)
251 pdeaddr = (unsigned *) pmap_pde(pmap, va);
252 if (*pdeaddr & PG_PS)
255 return get_ptbase(pmap) + i386_btop(va);
264 * Super fast pmap_pte routine best used when scanning the pv lists.
265 * This eliminates many course-grained invltlb calls. Note that many of
266 * the pv list scans are across different pmaps and it is very wasteful
267 * to do an entire invltlb when checking a single mapping.
269 * Should only be called while splvm() is held or from a critical
273 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
275 struct mdglobaldata *gd = mdcpu;
278 if ((pde = (unsigned) pmap->pm_pdir[va >> PDRSHIFT]) != 0) {
279 unsigned frame = (unsigned) pmap->pm_pdir[PTDPTDI] & PG_FRAME;
280 unsigned index = i386_btop(va);
281 /* are we current address space or kernel? */
282 if ((pmap == kernel_pmap) ||
283 (frame == (((unsigned) PTDpde) & PG_FRAME))) {
284 return (unsigned *) PTmap + index;
286 newpf = pde & PG_FRAME;
287 if ( ((* (unsigned *) gd->gd_PMAP1) & PG_FRAME) != newpf) {
288 * (unsigned *) gd->gd_PMAP1 = newpf | PG_RW | PG_V;
289 cpu_invlpg(gd->gd_PADDR1);
291 return gd->gd_PADDR1 + ((unsigned) index & (NPTEPG - 1));
298 * Bootstrap the system enough to run with virtual memory.
300 * On the i386 this is called after mapping has already been enabled
301 * and just syncs the pmap module with what has already been done.
302 * [We can't call it easily with mapping off since the kernel is not
303 * mapped with PA == VA, hence we would have to relocate every address
304 * from the linked base (virtual) address "KERNBASE" to the actual
305 * (physical) address starting relative to 0]
308 pmap_bootstrap(firstaddr, loadaddr)
309 vm_paddr_t firstaddr;
314 struct mdglobaldata *gd;
317 avail_start = firstaddr;
320 * XXX The calculation of virtual_avail is wrong. It's NKPT*PAGE_SIZE too
321 * large. It should instead be correctly calculated in locore.s and
322 * not based on 'first' (which is a physical address, not a virtual
323 * address, for the start of unused physical memory). The kernel
324 * page tables are NOT double mapped and thus should not be included
325 * in this calculation.
327 virtual_avail = (vm_offset_t) KERNBASE + firstaddr;
328 virtual_avail = pmap_kmem_choose(virtual_avail);
330 virtual_end = VM_MAX_KERNEL_ADDRESS;
333 * Initialize protection array.
335 i386_protection_init();
338 * The kernel's pmap is statically allocated so we don't have to use
339 * pmap_create, which is unlikely to work correctly at this part of
340 * the boot sequence (XXX and which no longer exists).
342 kernel_pmap = &kernel_pmap_store;
344 kernel_pmap->pm_pdir = (pd_entry_t *)(KERNBASE + (u_int)IdlePTD);
345 kernel_pmap->pm_count = 1;
346 kernel_pmap->pm_active = -1; /* don't allow deactivation */
347 TAILQ_INIT(&kernel_pmap->pm_pvlist);
351 * Reserve some special page table entries/VA space for temporary
354 #define SYSMAP(c, p, v, n) \
355 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
358 pte = (pt_entry_t *) pmap_pte(kernel_pmap, va);
361 * CMAP1/CMAP2 are used for zeroing and copying pages.
363 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
368 SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS);
371 * ptvmmap is used for reading arbitrary physical pages via
374 SYSMAP(caddr_t, ptmmap, ptvmmap, 1)
377 * msgbufp is used to map the system message buffer.
378 * XXX msgbufmap is not used.
380 SYSMAP(struct msgbuf *, msgbufmap, msgbufp,
381 atop(round_page(MSGBUF_SIZE)))
386 for (i = 0; i < NKPT; i++)
390 * PG_G is terribly broken on SMP because we IPI invltlb's in some
391 * cases rather then invl1pg. Actually, I don't even know why it
392 * works under UP because self-referential page table mappings
397 if (cpu_feature & CPUID_PGE)
402 * Initialize the 4MB page size flag
406 * The 4MB page version of the initial
407 * kernel page mapping.
411 #if !defined(DISABLE_PSE)
412 if (cpu_feature & CPUID_PSE) {
415 * Note that we have enabled PSE mode
418 ptditmp = *((unsigned *)PTmap + i386_btop(KERNBASE));
419 ptditmp &= ~(NBPDR - 1);
420 ptditmp |= PG_V | PG_RW | PG_PS | PG_U | pgeflag;
425 * Enable the PSE mode. If we are SMP we can't do this
426 * now because the APs will not be able to use it when
429 load_cr4(rcr4() | CR4_PSE);
432 * We can do the mapping here for the single processor
433 * case. We simply ignore the old page table page from
437 * For SMP, we still need 4K pages to bootstrap APs,
438 * PSE will be enabled as soon as all APs are up.
440 PTD[KPTDI] = (pd_entry_t)ptditmp;
441 kernel_pmap->pm_pdir[KPTDI] = (pd_entry_t)ptditmp;
447 if (cpu_apic_address == 0)
448 panic("pmap_bootstrap: no local apic!");
450 /* local apic is mapped on last page */
451 SMPpt[NPTEPG - 1] = (pt_entry_t)(PG_V | PG_RW | PG_N | pgeflag |
452 (cpu_apic_address & PG_FRAME));
455 /* BSP does this itself, AP's get it pre-set */
456 gd = &CPU_prvspace[0].mdglobaldata;
457 gd->gd_CMAP1 = &SMPpt[1];
458 gd->gd_CMAP2 = &SMPpt[2];
459 gd->gd_CMAP3 = &SMPpt[3];
460 gd->gd_PMAP1 = &SMPpt[4];
461 gd->gd_CADDR1 = CPU_prvspace[0].CPAGE1;
462 gd->gd_CADDR2 = CPU_prvspace[0].CPAGE2;
463 gd->gd_CADDR3 = CPU_prvspace[0].CPAGE3;
464 gd->gd_PADDR1 = (unsigned *)CPU_prvspace[0].PPAGE1;
471 * Set 4mb pdir for mp startup
476 if (pseflag && (cpu_feature & CPUID_PSE)) {
477 load_cr4(rcr4() | CR4_PSE);
478 if (pdir4mb && mycpu->gd_cpuid == 0) { /* only on BSP */
479 kernel_pmap->pm_pdir[KPTDI] =
480 PTD[KPTDI] = (pd_entry_t)pdir4mb;
488 * Initialize the pmap module.
489 * Called by vm_init, to initialize any structures that the pmap
490 * system needs to map virtual memory.
491 * pmap_init has been enhanced to support in a fairly consistant
492 * way, discontiguous physical memory.
495 pmap_init(phys_start, phys_end)
496 vm_paddr_t phys_start, phys_end;
502 * object for kernel page table pages
504 kptobj = vm_object_allocate(OBJT_DEFAULT, NKPDE);
507 * Allocate memory for random pmap data structures. Includes the
511 for(i = 0; i < vm_page_array_size; i++) {
514 m = &vm_page_array[i];
515 TAILQ_INIT(&m->md.pv_list);
516 m->md.pv_list_count = 0;
520 * init the pv free list
522 initial_pvs = vm_page_array_size;
523 if (initial_pvs < MINPV)
525 pvzone = &pvzone_store;
526 pvinit = (struct pv_entry *) kmem_alloc(kernel_map,
527 initial_pvs * sizeof (struct pv_entry));
528 zbootinit(pvzone, "PV ENTRY", sizeof (struct pv_entry), pvinit,
532 * Now it is safe to enable pv_table recording.
534 pmap_initialized = TRUE;
538 * Initialize the address space (zone) for the pv_entries. Set a
539 * high water mark so that the system can recover from excessive
540 * numbers of pv entries.
545 int shpgperproc = PMAP_SHPGPERPROC;
547 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
548 pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
549 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
550 pv_entry_high_water = 9 * (pv_entry_max / 10);
551 zinitna(pvzone, &pvzone_obj, NULL, 0, pv_entry_max, ZONE_INTERRUPT, 1);
555 /***************************************************
556 * Low level helper routines.....
557 ***************************************************/
559 #if defined(PMAP_DIAGNOSTIC)
562 * This code checks for non-writeable/modified pages.
563 * This should be an invalid condition.
566 pmap_nw_modified(pt_entry_t ptea)
572 if ((pte & (PG_M|PG_RW)) == PG_M)
581 * this routine defines the region(s) of memory that should
582 * not be tested for the modified bit.
584 static PMAP_INLINE int
585 pmap_track_modified(vm_offset_t va)
587 if ((va < clean_sva) || (va >= clean_eva))
593 static PMAP_INLINE void
594 invltlb_1pg(vm_offset_t va)
596 #if defined(I386_CPU)
597 if (cpu_class == CPUCLASS_386) {
607 pmap_TLB_invalidate(pmap_t pmap, vm_offset_t va)
610 if (pmap->pm_active & (1 << mycpu->gd_cpuid))
611 cpu_invlpg((void *)va);
612 if (pmap->pm_active & mycpu->gd_other_cpus)
621 pmap_TLB_invalidate_all(pmap_t pmap)
624 if (pmap->pm_active & (1 << mycpu->gd_cpuid))
626 if (pmap->pm_active & mycpu->gd_other_cpus)
635 get_ptbase(pmap_t pmap)
637 unsigned frame = (unsigned) pmap->pm_pdir[PTDPTDI] & PG_FRAME;
638 struct globaldata *gd = mycpu;
640 /* are we current address space or kernel? */
641 if (pmap == kernel_pmap || frame == (((unsigned) PTDpde) & PG_FRAME)) {
642 return (unsigned *) PTmap;
645 /* otherwise, we are alternate address space */
646 KKASSERT(gd->gd_intr_nesting_level == 0 && (gd->gd_curthread->td_flags & TDF_INTTHREAD) == 0);
648 if (frame != (((unsigned) APTDpde) & PG_FRAME)) {
649 APTDpde = (pd_entry_t)(frame | PG_RW | PG_V);
651 /* The page directory is not shared between CPUs */
657 return (unsigned *) APTmap;
663 * Extract the physical page address associated with the map/VA pair.
665 * This function may not be called from an interrupt if the pmap is
669 pmap_extract(pmap_t pmap, vm_offset_t va)
672 vm_offset_t pdirindex;
674 pdirindex = va >> PDRSHIFT;
675 if (pmap && (rtval = (unsigned) pmap->pm_pdir[pdirindex])) {
677 if ((rtval & PG_PS) != 0) {
678 rtval &= ~(NBPDR - 1);
679 rtval |= va & (NBPDR - 1);
682 pte = get_ptbase(pmap) + i386_btop(va);
683 rtval = ((*pte & PG_FRAME) | (va & PAGE_MASK));
690 /***************************************************
691 * Low level mapping routines.....
692 ***************************************************/
695 * add a wired page to the kva
696 * note that in order for the mapping to take effect -- you
697 * should do a invltlb after doing the pmap_kenter...
700 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
705 npte = pa | PG_RW | PG_V | pgeflag;
706 pte = (unsigned *)vtopte(va);
713 * remove a page from the kernel pagetables
716 pmap_kremove(vm_offset_t va)
720 pte = (unsigned *)vtopte(va);
726 * Used to map a range of physical addresses into kernel
727 * virtual address space.
729 * For now, VM is already on, we only need to map the
733 pmap_map(vm_offset_t virt, vm_paddr_t start, vm_paddr_t end, int prot)
735 while (start < end) {
736 pmap_kenter(virt, start);
745 * Add a list of wired pages to the kva
746 * this routine is only used for temporary
747 * kernel mappings that do not need to have
748 * page modification or references recorded.
749 * Note that old mappings are simply written
750 * over. The page *must* be wired.
753 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
757 end_va = va + count * PAGE_SIZE;
759 while (va < end_va) {
762 pte = (unsigned *)vtopte(va);
763 *pte = VM_PAGE_TO_PHYS(*m) | PG_RW | PG_V | pgeflag;
765 cpu_invlpg((void *)va);
778 * this routine jerks page mappings from the
779 * kernel -- it is meant only for temporary mappings.
782 pmap_qremove(vm_offset_t va, int count)
786 end_va = va + count*PAGE_SIZE;
788 while (va < end_va) {
791 pte = (unsigned *)vtopte(va);
794 cpu_invlpg((void *)va);
806 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
810 m = vm_page_lookup(object, pindex);
811 if (m && vm_page_sleep_busy(m, FALSE, "pplookp"))
817 * Create a new thread and optionally associate it with a (new) process.
818 * NOTE! the new thread's cpu may not equal the current cpu.
821 pmap_init_thread(thread_t td)
823 td->td_pcb = (struct pcb *)(td->td_kstack + UPAGES * PAGE_SIZE) - 1;
824 td->td_sp = (char *)td->td_pcb - 16;
828 * Create the UPAGES for a new process.
829 * This routine directly affects the fork perf for a process.
832 pmap_init_proc(struct proc *p, struct thread *td)
834 p->p_addr = (void *)td->td_kstack;
837 td->td_switch = cpu_heavy_switch;
841 bzero(p->p_addr, sizeof(*p->p_addr));
845 * Dispose the UPAGES for a process that has exited.
846 * This routine directly impacts the exit perf of a process.
849 pmap_dispose_proc(struct proc *p)
853 KASSERT(p->p_lock == 0, ("attempt to dispose referenced proc! %p", p));
855 if ((td = p->p_thread) != NULL) {
864 * Allow the UPAGES for a process to be prejudicially paged out.
867 pmap_swapout_proc(struct proc *p)
874 upobj = p->p_upages_obj;
876 * let the upages be paged
878 for(i=0;i<UPAGES;i++) {
879 if ((m = vm_page_lookup(upobj, i)) == NULL)
880 panic("pmap_swapout_proc: upage already missing???");
882 vm_page_unwire(m, 0);
883 pmap_kremove( (vm_offset_t) p->p_addr + PAGE_SIZE * i);
889 * Bring the UPAGES for a specified process back in.
892 pmap_swapin_proc(struct proc *p)
899 upobj = p->p_upages_obj;
900 for(i=0;i<UPAGES;i++) {
902 m = vm_page_grab(upobj, i, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
904 pmap_kenter(((vm_offset_t) p->p_addr) + i * PAGE_SIZE,
907 if (m->valid != VM_PAGE_BITS_ALL) {
908 rv = vm_pager_get_pages(upobj, &m, 1, 0);
909 if (rv != VM_PAGER_OK)
910 panic("pmap_swapin_proc: cannot get upages for proc: %d\n", p->p_pid);
911 m = vm_page_lookup(upobj, i);
912 m->valid = VM_PAGE_BITS_ALL;
917 vm_page_flag_set(m, PG_MAPPED | PG_WRITEABLE);
922 /***************************************************
923 * Page table page management routines.....
924 ***************************************************/
927 * This routine unholds page table pages, and if the hold count
928 * drops to zero, then it decrements the wire count.
931 _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
933 while (vm_page_sleep_busy(m, FALSE, "pmuwpt"))
936 if (m->hold_count == 0) {
939 * unmap the page table page
941 pmap->pm_pdir[m->pindex] = 0;
942 --pmap->pm_stats.resident_count;
943 if ((((unsigned)pmap->pm_pdir[PTDPTDI]) & PG_FRAME) ==
944 (((unsigned) PTDpde) & PG_FRAME)) {
946 * Do a invltlb to make the invalidated mapping
947 * take effect immediately.
949 pteva = UPT_MIN_ADDRESS + i386_ptob(m->pindex);
950 pmap_TLB_invalidate(pmap, pteva);
953 if (pmap->pm_ptphint == m)
954 pmap->pm_ptphint = NULL;
957 * If the page is finally unwired, simply free it.
960 if (m->wire_count == 0) {
964 vm_page_free_zero(m);
965 --vmstats.v_wire_count;
972 static PMAP_INLINE int
973 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
976 if (m->hold_count == 0)
977 return _pmap_unwire_pte_hold(pmap, m);
983 * After removing a page table entry, this routine is used to
984 * conditionally free the page, and manage the hold/wire counts.
987 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte)
990 if (va >= UPT_MIN_ADDRESS)
994 ptepindex = (va >> PDRSHIFT);
995 if (pmap->pm_ptphint &&
996 (pmap->pm_ptphint->pindex == ptepindex)) {
997 mpte = pmap->pm_ptphint;
999 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1000 pmap->pm_ptphint = mpte;
1004 return pmap_unwire_pte_hold(pmap, mpte);
1008 pmap_pinit0(struct pmap *pmap)
1011 (pd_entry_t *)kmem_alloc_pageable(kernel_map, PAGE_SIZE);
1012 pmap_kenter((vm_offset_t) pmap->pm_pdir, (vm_offset_t) IdlePTD);
1014 pmap->pm_active = 0;
1015 pmap->pm_ptphint = NULL;
1016 TAILQ_INIT(&pmap->pm_pvlist);
1017 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1021 * Initialize a preallocated and zeroed pmap structure,
1022 * such as one in a vmspace structure.
1025 pmap_pinit(struct pmap *pmap)
1030 * No need to allocate page table space yet but we do need a valid
1031 * page directory table.
1033 if (pmap->pm_pdir == NULL) {
1035 (pd_entry_t *)kmem_alloc_pageable(kernel_map, PAGE_SIZE);
1039 * allocate object for the ptes
1041 if (pmap->pm_pteobj == NULL)
1042 pmap->pm_pteobj = vm_object_allocate( OBJT_DEFAULT, PTDPTDI + 1);
1045 * allocate the page directory page
1047 ptdpg = vm_page_grab( pmap->pm_pteobj, PTDPTDI,
1048 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1050 ptdpg->wire_count = 1;
1051 ++vmstats.v_wire_count;
1054 vm_page_flag_clear(ptdpg, PG_MAPPED | PG_BUSY); /* not usually mapped*/
1055 ptdpg->valid = VM_PAGE_BITS_ALL;
1057 pmap_kenter((vm_offset_t) pmap->pm_pdir, VM_PAGE_TO_PHYS(ptdpg));
1058 if ((ptdpg->flags & PG_ZERO) == 0)
1059 bzero(pmap->pm_pdir, PAGE_SIZE);
1061 pmap->pm_pdir[MPPTDI] = PTD[MPPTDI];
1063 /* install self-referential address mapping entry */
1064 *(unsigned *) (pmap->pm_pdir + PTDPTDI) =
1065 VM_PAGE_TO_PHYS(ptdpg) | PG_V | PG_RW | PG_A | PG_M;
1068 pmap->pm_active = 0;
1069 pmap->pm_ptphint = NULL;
1070 TAILQ_INIT(&pmap->pm_pvlist);
1071 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1075 * Wire in kernel global address entries. To avoid a race condition
1076 * between pmap initialization and pmap_growkernel, this procedure
1077 * should be called after the vmspace is attached to the process
1078 * but before this pmap is activated.
1081 pmap_pinit2(struct pmap *pmap)
1083 /* XXX copies current process, does not fill in MPPTDI */
1084 bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * PTESIZE);
1088 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1090 unsigned *pde = (unsigned *) pmap->pm_pdir;
1092 * This code optimizes the case of freeing non-busy
1093 * page-table pages. Those pages are zero now, and
1094 * might as well be placed directly into the zero queue.
1096 if (vm_page_sleep_busy(p, FALSE, "pmaprl"))
1102 * Remove the page table page from the processes address space.
1105 pmap->pm_stats.resident_count--;
1107 if (p->hold_count) {
1108 panic("pmap_release: freeing held page table page");
1111 * Page directory pages need to have the kernel
1112 * stuff cleared, so they can go into the zero queue also.
1114 if (p->pindex == PTDPTDI) {
1115 bzero(pde + KPTDI, nkpt * PTESIZE);
1118 pmap_kremove((vm_offset_t) pmap->pm_pdir);
1121 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1122 pmap->pm_ptphint = NULL;
1125 vmstats.v_wire_count--;
1126 vm_page_free_zero(p);
1131 * this routine is called if the page table page is not
1135 _pmap_allocpte(pmap_t pmap, unsigned ptepindex)
1137 vm_offset_t pteva, ptepa;
1141 * Find or fabricate a new pagetable page
1143 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1144 VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1146 KASSERT(m->queue == PQ_NONE,
1147 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1149 if (m->wire_count == 0)
1150 vmstats.v_wire_count++;
1154 * Increment the hold count for the page table page
1155 * (denoting a new mapping.)
1160 * Map the pagetable page into the process address space, if
1161 * it isn't already there.
1164 pmap->pm_stats.resident_count++;
1166 ptepa = VM_PAGE_TO_PHYS(m);
1167 pmap->pm_pdir[ptepindex] =
1168 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_A | PG_M);
1171 * Set the page table hint
1173 pmap->pm_ptphint = m;
1176 * Try to use the new mapping, but if we cannot, then
1177 * do it with the routine that maps the page explicitly.
1179 if ((m->flags & PG_ZERO) == 0) {
1180 if ((((unsigned)pmap->pm_pdir[PTDPTDI]) & PG_FRAME) ==
1181 (((unsigned) PTDpde) & PG_FRAME)) {
1182 pteva = UPT_MIN_ADDRESS + i386_ptob(ptepindex);
1183 bzero((caddr_t) pteva, PAGE_SIZE);
1185 pmap_zero_page(ptepa);
1189 m->valid = VM_PAGE_BITS_ALL;
1190 vm_page_flag_clear(m, PG_ZERO);
1191 vm_page_flag_set(m, PG_MAPPED);
1198 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1205 * Calculate pagetable page index
1207 ptepindex = va >> PDRSHIFT;
1210 * Get the page directory entry
1212 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1215 * This supports switching from a 4MB page to a
1218 if (ptepa & PG_PS) {
1219 pmap->pm_pdir[ptepindex] = 0;
1225 * If the page table page is mapped, we just increment the
1226 * hold count, and activate it.
1230 * In order to get the page table page, try the
1233 if (pmap->pm_ptphint &&
1234 (pmap->pm_ptphint->pindex == ptepindex)) {
1235 m = pmap->pm_ptphint;
1237 m = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1238 pmap->pm_ptphint = m;
1244 * Here if the pte page isn't mapped, or if it has been deallocated.
1246 return _pmap_allocpte(pmap, ptepindex);
1250 /***************************************************
1251 * Pmap allocation/deallocation routines.
1252 ***************************************************/
1255 * Release any resources held by the given physical map.
1256 * Called when a pmap initialized by pmap_pinit is being released.
1257 * Should only be called if the map contains no valid mappings.
1260 pmap_release(struct pmap *pmap)
1262 vm_page_t p,n,ptdpg;
1263 vm_object_t object = pmap->pm_pteobj;
1266 #if defined(DIAGNOSTIC)
1267 if (object->ref_count != 1)
1268 panic("pmap_release: pteobj reference count != 1");
1273 curgeneration = object->generation;
1274 for (p = TAILQ_FIRST(&object->memq); p != NULL; p = n) {
1275 n = TAILQ_NEXT(p, listq);
1276 if (p->pindex == PTDPTDI) {
1281 if (!pmap_release_free_page(pmap, p) &&
1282 (object->generation != curgeneration))
1287 if (ptdpg && !pmap_release_free_page(pmap, ptdpg))
1292 kvm_size(SYSCTL_HANDLER_ARGS)
1294 unsigned long ksize = VM_MAX_KERNEL_ADDRESS - KERNBASE;
1296 return sysctl_handle_long(oidp, &ksize, 0, req);
1298 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD,
1299 0, 0, kvm_size, "IU", "Size of KVM");
1302 kvm_free(SYSCTL_HANDLER_ARGS)
1304 unsigned long kfree = VM_MAX_KERNEL_ADDRESS - kernel_vm_end;
1306 return sysctl_handle_long(oidp, &kfree, 0, req);
1308 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD,
1309 0, 0, kvm_free, "IU", "Amount of KVM free");
1312 * grow the number of kernel page table entries, if needed
1315 pmap_growkernel(vm_offset_t addr)
1320 vm_offset_t ptppaddr;
1325 if (kernel_vm_end == 0) {
1326 kernel_vm_end = KERNBASE;
1328 while (pdir_pde(PTD, kernel_vm_end)) {
1329 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1333 addr = (addr + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1334 while (kernel_vm_end < addr) {
1335 if (pdir_pde(PTD, kernel_vm_end)) {
1336 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1341 * This index is bogus, but out of the way
1343 nkpg = vm_page_alloc(kptobj, nkpt, VM_ALLOC_SYSTEM);
1345 panic("pmap_growkernel: no memory to grow kernel");
1350 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1351 pmap_zero_page(ptppaddr);
1352 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
1353 pdir_pde(PTD, kernel_vm_end) = newpdir;
1355 FOREACH_PROC_IN_SYSTEM(p) {
1357 pmap = vmspace_pmap(p->p_vmspace);
1358 *pmap_pde(pmap, kernel_vm_end) = newpdir;
1361 *pmap_pde(kernel_pmap, kernel_vm_end) = newpdir;
1362 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1368 * Retire the given physical map from service.
1369 * Should only be called if the map contains
1370 * no valid mappings.
1373 pmap_destroy(pmap_t pmap)
1380 count = --pmap->pm_count;
1383 panic("destroying a pmap is not yet implemented");
1388 * Add a reference to the specified pmap.
1391 pmap_reference(pmap_t pmap)
1398 /***************************************************
1399 * page management routines.
1400 ***************************************************/
1403 * free the pv_entry back to the free list. This function may be
1404 * called from an interrupt.
1406 static PMAP_INLINE void
1407 free_pv_entry(pv_entry_t pv)
1414 * get a new pv_entry, allocating a block from the system
1415 * when needed. This function may be called from an interrupt.
1421 if (pv_entry_high_water &&
1422 (pv_entry_count > pv_entry_high_water) &&
1423 (pmap_pagedaemon_waken == 0)) {
1424 pmap_pagedaemon_waken = 1;
1425 wakeup (&vm_pages_needed);
1427 return zalloc(pvzone);
1431 * This routine is very drastic, but can save the system
1439 static int warningdone=0;
1441 if (pmap_pagedaemon_waken == 0)
1444 if (warningdone < 5) {
1445 printf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1449 for(i = 0; i < vm_page_array_size; i++) {
1450 m = &vm_page_array[i];
1451 if (m->wire_count || m->hold_count || m->busy ||
1452 (m->flags & PG_BUSY))
1456 pmap_pagedaemon_waken = 0;
1461 * If it is the first entry on the list, it is actually
1462 * in the header and we must copy the following entry up
1463 * to the header. Otherwise we must search the list for
1464 * the entry. In either case we free the now unused entry.
1468 pmap_remove_entry(struct pmap *pmap, vm_page_t m, vm_offset_t va)
1475 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1476 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1477 if (pmap == pv->pv_pmap && va == pv->pv_va)
1481 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1482 if (va == pv->pv_va)
1490 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem);
1491 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1492 m->md.pv_list_count--;
1493 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
1494 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1496 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1505 * Create a pv entry for page at pa for
1509 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
1515 pv = get_pv_entry();
1520 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1521 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1522 m->md.pv_list_count++;
1528 * pmap_remove_pte: do the things to unmap a page in a process
1531 pmap_remove_pte(struct pmap *pmap, unsigned *ptq, vm_offset_t va)
1536 oldpte = loadandclear(ptq);
1538 pmap->pm_stats.wired_count -= 1;
1540 * Machines that don't support invlpg, also don't support
1545 pmap->pm_stats.resident_count -= 1;
1546 if (oldpte & PG_MANAGED) {
1547 m = PHYS_TO_VM_PAGE(oldpte);
1548 if (oldpte & PG_M) {
1549 #if defined(PMAP_DIAGNOSTIC)
1550 if (pmap_nw_modified((pt_entry_t) oldpte)) {
1552 "pmap_remove: modified page not writable: va: 0x%x, pte: 0x%x\n",
1556 if (pmap_track_modified(va))
1560 vm_page_flag_set(m, PG_REFERENCED);
1561 return pmap_remove_entry(pmap, m, va);
1563 return pmap_unuse_pt(pmap, va, NULL);
1572 * Remove a single page from a process address space.
1574 * This function may not be called from an interrupt if the pmap is
1578 pmap_remove_page(struct pmap *pmap, vm_offset_t va)
1583 * if there is no pte for this address, just skip it!!! Otherwise
1584 * get a local va for mappings for this pmap and remove the entry.
1586 if (*pmap_pde(pmap, va) != 0) {
1587 ptq = get_ptbase(pmap) + i386_btop(va);
1589 (void) pmap_remove_pte(pmap, ptq, va);
1590 pmap_TLB_invalidate(pmap, va);
1598 * Remove the given range of addresses from the specified map.
1600 * It is assumed that the start and end are properly
1601 * rounded to the page size.
1603 * This function may not be called from an interrupt if the pmap is
1607 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
1611 vm_offset_t ptpaddr;
1612 vm_offset_t sindex, eindex;
1618 if (pmap->pm_stats.resident_count == 0)
1622 * special handling of removing one page. a very
1623 * common operation and easy to short circuit some
1626 if (((sva + PAGE_SIZE) == eva) &&
1627 (((unsigned) pmap->pm_pdir[(sva >> PDRSHIFT)] & PG_PS) == 0)) {
1628 pmap_remove_page(pmap, sva);
1635 * Get a local virtual address for the mappings that are being
1638 ptbase = get_ptbase(pmap);
1640 sindex = i386_btop(sva);
1641 eindex = i386_btop(eva);
1643 for (; sindex < eindex; sindex = pdnxt) {
1647 * Calculate index for next page table.
1649 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1650 if (pmap->pm_stats.resident_count == 0)
1653 pdirindex = sindex / NPDEPG;
1654 if (((ptpaddr = (unsigned) pmap->pm_pdir[pdirindex]) & PG_PS) != 0) {
1655 pmap->pm_pdir[pdirindex] = 0;
1656 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1662 * Weed out invalid mappings. Note: we assume that the page
1663 * directory table is always allocated, and in kernel virtual.
1669 * Limit our scan to either the end of the va represented
1670 * by the current page table page, or to the end of the
1671 * range being removed.
1673 if (pdnxt > eindex) {
1677 for ( ;sindex != pdnxt; sindex++) {
1679 if (ptbase[sindex] == 0) {
1682 va = i386_ptob(sindex);
1685 if (pmap_remove_pte(pmap,
1686 ptbase + sindex, va))
1692 pmap_TLB_invalidate_all(pmap);
1698 * Removes this physical page from all physical maps in which it resides.
1699 * Reflects back modify bits to the pager.
1701 * This routine may not be called from an interrupt.
1705 pmap_remove_all(vm_page_t m)
1708 unsigned *pte, tpte;
1711 #if defined(PMAP_DIAGNOSTIC)
1713 * XXX this makes pmap_page_protect(NONE) illegal for non-managed
1716 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
1717 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%08llx", (long long)VM_PAGE_TO_PHYS(m));
1722 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
1723 pv->pv_pmap->pm_stats.resident_count--;
1725 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
1727 tpte = loadandclear(pte);
1729 pv->pv_pmap->pm_stats.wired_count--;
1732 vm_page_flag_set(m, PG_REFERENCED);
1735 * Update the vm_page_t clean and reference bits.
1738 #if defined(PMAP_DIAGNOSTIC)
1739 if (pmap_nw_modified((pt_entry_t) tpte)) {
1741 "pmap_remove_all: modified page not writable: va: 0x%x, pte: 0x%x\n",
1745 if (pmap_track_modified(pv->pv_va))
1748 pmap_TLB_invalidate(pv->pv_pmap, pv->pv_va);
1750 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
1751 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1752 m->md.pv_list_count--;
1753 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
1757 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1765 * Set the physical protection on the specified range of this map
1768 * This function may not be called from an interrupt if the map is
1769 * not the kernel_pmap.
1772 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
1775 vm_offset_t pdnxt, ptpaddr;
1776 vm_pindex_t sindex, eindex;
1782 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1783 pmap_remove(pmap, sva, eva);
1787 if (prot & VM_PROT_WRITE)
1792 ptbase = get_ptbase(pmap);
1794 sindex = i386_btop(sva);
1795 eindex = i386_btop(eva);
1797 for (; sindex < eindex; sindex = pdnxt) {
1801 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1803 pdirindex = sindex / NPDEPG;
1804 if (((ptpaddr = (unsigned) pmap->pm_pdir[pdirindex]) & PG_PS) != 0) {
1805 (unsigned) pmap->pm_pdir[pdirindex] &= ~(PG_M|PG_RW);
1806 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1812 * Weed out invalid mappings. Note: we assume that the page
1813 * directory table is always allocated, and in kernel virtual.
1818 if (pdnxt > eindex) {
1822 for (; sindex != pdnxt; sindex++) {
1827 pbits = ptbase[sindex];
1829 if (pbits & PG_MANAGED) {
1832 m = PHYS_TO_VM_PAGE(pbits);
1833 vm_page_flag_set(m, PG_REFERENCED);
1837 if (pmap_track_modified(i386_ptob(sindex))) {
1839 m = PHYS_TO_VM_PAGE(pbits);
1848 if (pbits != ptbase[sindex]) {
1849 ptbase[sindex] = pbits;
1855 pmap_TLB_invalidate_all(pmap);
1859 * Insert the given physical page (p) at
1860 * the specified virtual address (v) in the
1861 * target physical map with the protection requested.
1863 * If specified, the page will be wired down, meaning
1864 * that the related pte can not be reclaimed.
1866 * NB: This is the only routine which MAY NOT lazy-evaluate
1867 * or lose information. That is, this routine must actually
1868 * insert this page into the given map NOW.
1871 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1877 vm_offset_t origpte, newpte;
1884 #ifdef PMAP_DIAGNOSTIC
1885 if (va > VM_MAX_KERNEL_ADDRESS)
1886 panic("pmap_enter: toobig");
1887 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
1888 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%x)", va);
1893 * In the case that a page table page is not
1894 * resident, we are creating it here.
1896 if (va < UPT_MIN_ADDRESS) {
1897 mpte = pmap_allocpte(pmap, va);
1899 #if 0 && defined(PMAP_DIAGNOSTIC)
1901 vm_offset_t *pdeaddr = (vm_offset_t *)pmap_pde(pmap, va);
1902 if (((origpte = (vm_offset_t) *pdeaddr) & PG_V) == 0) {
1903 panic("pmap_enter: invalid kernel page table page(0), pdir=%p, pde=%p, va=%p\n",
1904 pmap->pm_pdir[PTDPTDI], origpte, va);
1907 pdeaddr = (vm_offset_t *) IdlePTDS[cpuid];
1908 if (((newpte = pdeaddr[va >> PDRSHIFT]) & PG_V) == 0) {
1909 if ((vm_offset_t) my_idlePTD != (vm_offset_t) vtophys(pdeaddr))
1910 printf("pde mismatch: %x, %x\n", my_idlePTD, pdeaddr);
1911 printf("cpuid: %d, pdeaddr: 0x%x\n", cpuid, pdeaddr);
1912 panic("pmap_enter: invalid kernel page table page(1), pdir=%p, npde=%p, pde=%p, va=%p\n",
1913 pmap->pm_pdir[PTDPTDI], newpte, origpte, va);
1919 pte = pmap_pte(pmap, va);
1922 * Page Directory table entry not valid, we need a new PT page
1925 panic("pmap_enter: invalid page directory pdir=%x, va=0x%x\n",
1926 (unsigned) pmap->pm_pdir[PTDPTDI], va);
1929 pa = VM_PAGE_TO_PHYS(m) & PG_FRAME;
1930 origpte = *(vm_offset_t *)pte;
1931 opa = origpte & PG_FRAME;
1933 if (origpte & PG_PS)
1934 panic("pmap_enter: attempted pmap_enter on 4MB page");
1937 * Mapping has not changed, must be protection or wiring change.
1939 if (origpte && (opa == pa)) {
1941 * Wiring change, just update stats. We don't worry about
1942 * wiring PT pages as they remain resident as long as there
1943 * are valid mappings in them. Hence, if a user page is wired,
1944 * the PT page will be also.
1946 if (wired && ((origpte & PG_W) == 0))
1947 pmap->pm_stats.wired_count++;
1948 else if (!wired && (origpte & PG_W))
1949 pmap->pm_stats.wired_count--;
1951 #if defined(PMAP_DIAGNOSTIC)
1952 if (pmap_nw_modified((pt_entry_t) origpte)) {
1954 "pmap_enter: modified page not writable: va: 0x%x, pte: 0x%x\n",
1960 * Remove extra pte reference
1965 if ((prot & VM_PROT_WRITE) && (origpte & PG_V)) {
1966 if ((origpte & PG_RW) == 0) {
1969 cpu_invlpg((void *)va);
1970 if (pmap->pm_active & mycpu->gd_other_cpus)
1980 * We might be turning off write access to the page,
1981 * so we go ahead and sense modify status.
1983 if (origpte & PG_MANAGED) {
1984 if ((origpte & PG_M) && pmap_track_modified(va)) {
1986 om = PHYS_TO_VM_PAGE(opa);
1994 * Mapping has changed, invalidate old range and fall through to
1995 * handle validating new mapping.
1999 err = pmap_remove_pte(pmap, pte, va);
2001 panic("pmap_enter: pte vanished, va: 0x%x", va);
2005 * Enter on the PV list if part of our managed memory. Note that we
2006 * raise IPL while manipulating pv_table since pmap_enter can be
2007 * called at interrupt time.
2009 if (pmap_initialized &&
2010 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2011 pmap_insert_entry(pmap, va, mpte, m);
2016 * Increment counters
2018 pmap->pm_stats.resident_count++;
2020 pmap->pm_stats.wired_count++;
2024 * Now validate mapping with desired protection/wiring.
2026 newpte = (vm_offset_t) (pa | pte_prot(pmap, prot) | PG_V);
2030 if (va < UPT_MIN_ADDRESS)
2032 if (pmap == kernel_pmap)
2036 * if the mapping or permission bits are different, we need
2037 * to update the pte.
2039 if ((origpte & ~(PG_M|PG_A)) != newpte) {
2040 *pte = newpte | PG_A;
2043 cpu_invlpg((void *)va);
2044 if (pmap->pm_active & mycpu->gd_other_cpus)
2054 * this code makes some *MAJOR* assumptions:
2055 * 1. Current pmap & pmap exists.
2058 * 4. No page table pages.
2059 * 5. Tlbflush is deferred to calling procedure.
2060 * 6. Page IS managed.
2061 * but is *MUCH* faster than pmap_enter...
2065 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_page_t mpte)
2071 * In the case that a page table page is not
2072 * resident, we are creating it here.
2074 if (va < UPT_MIN_ADDRESS) {
2079 * Calculate pagetable page index
2081 ptepindex = va >> PDRSHIFT;
2082 if (mpte && (mpte->pindex == ptepindex)) {
2087 * Get the page directory entry
2089 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
2092 * If the page table page is mapped, we just increment
2093 * the hold count, and activate it.
2097 panic("pmap_enter_quick: unexpected mapping into 4MB page");
2098 if (pmap->pm_ptphint &&
2099 (pmap->pm_ptphint->pindex == ptepindex)) {
2100 mpte = pmap->pm_ptphint;
2102 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
2103 pmap->pm_ptphint = mpte;
2109 mpte = _pmap_allocpte(pmap, ptepindex);
2117 * This call to vtopte makes the assumption that we are
2118 * entering the page into the current pmap. In order to support
2119 * quick entry into any pmap, one would likely use pmap_pte_quick.
2120 * But that isn't as quick as vtopte.
2122 pte = (unsigned *)vtopte(va);
2125 pmap_unwire_pte_hold(pmap, mpte);
2130 * Enter on the PV list if part of our managed memory. Note that we
2131 * raise IPL while manipulating pv_table since pmap_enter can be
2132 * called at interrupt time.
2134 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0)
2135 pmap_insert_entry(pmap, va, mpte, m);
2138 * Increment counters
2140 pmap->pm_stats.resident_count++;
2142 pa = VM_PAGE_TO_PHYS(m);
2145 * Now validate mapping with RO protection
2147 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2148 *pte = pa | PG_V | PG_U;
2150 *pte = pa | PG_V | PG_U | PG_MANAGED;
2156 * Make a temporary mapping for a physical address. This is only intended
2157 * to be used for panic dumps.
2160 pmap_kenter_temporary(vm_paddr_t pa, int i)
2162 pmap_kenter((vm_offset_t)crashdumpmap + (i * PAGE_SIZE), pa);
2163 return ((void *)crashdumpmap);
2166 #define MAX_INIT_PT (96)
2168 * pmap_object_init_pt preloads the ptes for a given object
2169 * into the specified pmap. This eliminates the blast of soft
2170 * faults on process startup and immediately after an mmap.
2173 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_object_t object,
2174 vm_pindex_t pindex, vm_size_t size, int limit)
2181 if (pmap == NULL || object == NULL)
2185 * This code maps large physical mmap regions into the
2186 * processor address space. Note that some shortcuts
2187 * are taken, but the code works.
2190 (object->type == OBJT_DEVICE) &&
2191 ((addr & (NBPDR - 1)) == 0) &&
2192 ((size & (NBPDR - 1)) == 0) ) {
2195 unsigned int ptepindex;
2199 if (pmap->pm_pdir[ptepindex = (addr >> PDRSHIFT)])
2203 p = vm_page_lookup(object, pindex);
2204 if (p && vm_page_sleep_busy(p, FALSE, "init4p"))
2208 p = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL);
2213 if (vm_pager_get_pages(object, m, 1, 0) != VM_PAGER_OK) {
2218 p = vm_page_lookup(object, pindex);
2222 ptepa = (vm_offset_t) VM_PAGE_TO_PHYS(p);
2223 if (ptepa & (NBPDR - 1)) {
2227 p->valid = VM_PAGE_BITS_ALL;
2229 pmap->pm_stats.resident_count += size >> PAGE_SHIFT;
2230 npdes = size >> PDRSHIFT;
2231 for(i=0;i<npdes;i++) {
2232 pmap->pm_pdir[ptepindex] =
2233 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_PS);
2237 vm_page_flag_set(p, PG_MAPPED);
2242 psize = i386_btop(size);
2244 if ((object->type != OBJT_VNODE) ||
2245 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2246 (object->resident_page_count > MAX_INIT_PT))) {
2250 if (psize + pindex > object->size) {
2251 if (object->size < pindex)
2253 psize = object->size - pindex;
2258 * if we are processing a major portion of the object, then scan the
2261 if (psize > (object->resident_page_count >> 2)) {
2264 for (p = TAILQ_FIRST(&object->memq);
2265 ((objpgs > 0) && (p != NULL));
2266 p = TAILQ_NEXT(p, listq)) {
2269 if (tmpidx < pindex) {
2273 if (tmpidx >= psize) {
2277 * don't allow an madvise to blow away our really
2278 * free pages allocating pv entries.
2280 if ((limit & MAP_PREFAULT_MADVISE) &&
2281 vmstats.v_free_count < vmstats.v_free_reserved) {
2284 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2286 (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2287 if ((p->queue - p->pc) == PQ_CACHE)
2288 vm_page_deactivate(p);
2290 mpte = pmap_enter_quick(pmap,
2291 addr + i386_ptob(tmpidx), p, mpte);
2292 vm_page_flag_set(p, PG_MAPPED);
2299 * else lookup the pages one-by-one.
2301 for (tmpidx = 0; tmpidx < psize; tmpidx += 1) {
2303 * don't allow an madvise to blow away our really
2304 * free pages allocating pv entries.
2306 if ((limit & MAP_PREFAULT_MADVISE) &&
2307 vmstats.v_free_count < vmstats.v_free_reserved) {
2310 p = vm_page_lookup(object, tmpidx + pindex);
2312 ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2314 (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2315 if ((p->queue - p->pc) == PQ_CACHE)
2316 vm_page_deactivate(p);
2318 mpte = pmap_enter_quick(pmap,
2319 addr + i386_ptob(tmpidx), p, mpte);
2320 vm_page_flag_set(p, PG_MAPPED);
2329 * pmap_prefault provides a quick way of clustering
2330 * pagefaults into a processes address space. It is a "cousin"
2331 * of pmap_object_init_pt, except it runs at page fault time instead
2336 #define PAGEORDER_SIZE (PFBAK+PFFOR)
2338 static int pmap_prefault_pageorder[] = {
2339 -PAGE_SIZE, PAGE_SIZE,
2340 -2 * PAGE_SIZE, 2 * PAGE_SIZE,
2341 -3 * PAGE_SIZE, 3 * PAGE_SIZE
2342 -4 * PAGE_SIZE, 4 * PAGE_SIZE
2346 pmap_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry)
2355 if (!curproc || (pmap != vmspace_pmap(curproc->p_vmspace)))
2358 object = entry->object.vm_object;
2360 starta = addra - PFBAK * PAGE_SIZE;
2361 if (starta < entry->start) {
2362 starta = entry->start;
2363 } else if (starta > addra) {
2368 for (i = 0; i < PAGEORDER_SIZE; i++) {
2369 vm_object_t lobject;
2372 addr = addra + pmap_prefault_pageorder[i];
2373 if (addr > addra + (PFFOR * PAGE_SIZE))
2376 if (addr < starta || addr >= entry->end)
2379 if ((*pmap_pde(pmap, addr)) == NULL)
2382 pte = (unsigned *) vtopte(addr);
2386 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2388 for (m = vm_page_lookup(lobject, pindex);
2389 (!m && (lobject->type == OBJT_DEFAULT) && (lobject->backing_object));
2390 lobject = lobject->backing_object) {
2391 if (lobject->backing_object_offset & PAGE_MASK)
2393 pindex += (lobject->backing_object_offset >> PAGE_SHIFT);
2394 m = vm_page_lookup(lobject->backing_object, pindex);
2398 * give-up when a page is not in memory
2403 if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2405 (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2407 if ((m->queue - m->pc) == PQ_CACHE) {
2408 vm_page_deactivate(m);
2411 mpte = pmap_enter_quick(pmap, addr, m, mpte);
2412 vm_page_flag_set(m, PG_MAPPED);
2419 * Routine: pmap_change_wiring
2420 * Function: Change the wiring attribute for a map/virtual-address
2422 * In/out conditions:
2423 * The mapping must already exist in the pmap.
2426 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2433 pte = pmap_pte(pmap, va);
2435 if (wired && !pmap_pte_w(pte))
2436 pmap->pm_stats.wired_count++;
2437 else if (!wired && pmap_pte_w(pte))
2438 pmap->pm_stats.wired_count--;
2441 * Wiring is not a hardware characteristic so there is no need to
2444 pmap_pte_set_w(pte, wired);
2450 * Copy the range specified by src_addr/len
2451 * from the source map to the range dst_addr/len
2452 * in the destination map.
2454 * This routine is only advisory and need not do anything.
2457 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2458 vm_size_t len, vm_offset_t src_addr)
2461 vm_offset_t end_addr = src_addr + len;
2463 unsigned src_frame, dst_frame;
2466 if (dst_addr != src_addr)
2469 src_frame = ((unsigned) src_pmap->pm_pdir[PTDPTDI]) & PG_FRAME;
2470 if (src_frame != (((unsigned) PTDpde) & PG_FRAME)) {
2474 dst_frame = ((unsigned) dst_pmap->pm_pdir[PTDPTDI]) & PG_FRAME;
2475 if (dst_frame != (((unsigned) APTDpde) & PG_FRAME)) {
2476 APTDpde = (pd_entry_t) (dst_frame | PG_RW | PG_V);
2478 /* The page directory is not shared between CPUs */
2485 for(addr = src_addr; addr < end_addr; addr = pdnxt) {
2486 unsigned *src_pte, *dst_pte;
2487 vm_page_t dstmpte, srcmpte;
2488 vm_offset_t srcptepaddr;
2491 if (addr >= UPT_MIN_ADDRESS)
2492 panic("pmap_copy: invalid to pmap_copy page tables\n");
2495 * Don't let optional prefaulting of pages make us go
2496 * way below the low water mark of free pages or way
2497 * above high water mark of used pv entries.
2499 if (vmstats.v_free_count < vmstats.v_free_reserved ||
2500 pv_entry_count > pv_entry_high_water)
2503 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
2504 ptepindex = addr >> PDRSHIFT;
2506 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex];
2507 if (srcptepaddr == 0)
2510 if (srcptepaddr & PG_PS) {
2511 if (dst_pmap->pm_pdir[ptepindex] == 0) {
2512 dst_pmap->pm_pdir[ptepindex] = (pd_entry_t) srcptepaddr;
2513 dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
2518 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
2519 if ((srcmpte == NULL) ||
2520 (srcmpte->hold_count == 0) || (srcmpte->flags & PG_BUSY))
2523 if (pdnxt > end_addr)
2526 src_pte = (unsigned *) vtopte(addr);
2527 dst_pte = (unsigned *) avtopte(addr);
2528 while (addr < pdnxt) {
2532 * we only virtual copy managed pages
2534 if ((ptetemp & PG_MANAGED) != 0) {
2536 * We have to check after allocpte for the
2537 * pte still being around... allocpte can
2540 dstmpte = pmap_allocpte(dst_pmap, addr);
2541 if ((*dst_pte == 0) && (ptetemp = *src_pte)) {
2543 * Clear the modified and
2544 * accessed (referenced) bits
2547 m = PHYS_TO_VM_PAGE(ptetemp);
2548 *dst_pte = ptetemp & ~(PG_M | PG_A);
2549 dst_pmap->pm_stats.resident_count++;
2550 pmap_insert_entry(dst_pmap, addr,
2553 pmap_unwire_pte_hold(dst_pmap, dstmpte);
2555 if (dstmpte->hold_count >= srcmpte->hold_count)
2566 * Routine: pmap_kernel
2568 * Returns the physical map handle for the kernel.
2573 return (kernel_pmap);
2579 * Zero the specified PA by mapping the page into KVM and clearing its
2582 * This function may be called from an interrupt and no locking is
2586 pmap_zero_page(vm_paddr_t phys)
2588 struct mdglobaldata *gd = mdcpu;
2591 if (*(int *)gd->gd_CMAP3)
2592 panic("pmap_zero_page: CMAP3 busy");
2593 *(int *)gd->gd_CMAP3 =
2594 PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2595 cpu_invlpg(gd->gd_CADDR3);
2597 #if defined(I686_CPU)
2598 if (cpu_class == CPUCLASS_686)
2599 i686_pagezero(gd->gd_CADDR3);
2602 bzero(gd->gd_CADDR3, PAGE_SIZE);
2603 *(int *) gd->gd_CMAP3 = 0;
2610 * Zero part of a physical page by mapping it into memory and clearing
2611 * its contents with bzero.
2613 * off and size may not cover an area beyond a single hardware page.
2616 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
2618 struct mdglobaldata *gd = mdcpu;
2621 if (*(int *) gd->gd_CMAP3)
2622 panic("pmap_zero_page: CMAP3 busy");
2623 *(int *) gd->gd_CMAP3 = PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2624 cpu_invlpg(gd->gd_CADDR3);
2626 #if defined(I686_CPU)
2627 if (cpu_class == CPUCLASS_686 && off == 0 && size == PAGE_SIZE)
2628 i686_pagezero(gd->gd_CADDR3);
2631 bzero((char *)gd->gd_CADDR3 + off, size);
2632 *(int *) gd->gd_CMAP3 = 0;
2639 * Copy the physical page from the source PA to the target PA.
2640 * This function may be called from an interrupt. No locking
2644 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
2646 struct mdglobaldata *gd = mdcpu;
2649 if (*(int *) gd->gd_CMAP1)
2650 panic("pmap_copy_page: CMAP1 busy");
2651 if (*(int *) gd->gd_CMAP2)
2652 panic("pmap_copy_page: CMAP2 busy");
2654 *(int *) gd->gd_CMAP1 = PG_V | (src & PG_FRAME) | PG_A;
2655 *(int *) gd->gd_CMAP2 = PG_V | PG_RW | (dst & PG_FRAME) | PG_A | PG_M;
2657 cpu_invlpg(gd->gd_CADDR1);
2658 cpu_invlpg(gd->gd_CADDR2);
2660 bcopy(gd->gd_CADDR1, gd->gd_CADDR2, PAGE_SIZE);
2662 *(int *) gd->gd_CMAP1 = 0;
2663 *(int *) gd->gd_CMAP2 = 0;
2669 * Routine: pmap_pageable
2671 * Make the specified pages (by pmap, offset)
2672 * pageable (or not) as requested.
2674 * A page which is not pageable may not take
2675 * a fault; therefore, its page table entry
2676 * must remain valid for the duration.
2678 * This routine is merely advisory; pmap_enter
2679 * will specify that these pages are to be wired
2680 * down (or not) as appropriate.
2683 pmap_pageable(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, boolean_t pageable)
2688 * Returns true if the pmap's pv is one of the first
2689 * 16 pvs linked to from this page. This count may
2690 * be changed upwards or downwards in the future; it
2691 * is only necessary that true be returned for a small
2692 * subset of pmaps for proper page aging.
2695 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2701 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2706 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2707 if (pv->pv_pmap == pmap) {
2719 #define PMAP_REMOVE_PAGES_CURPROC_ONLY
2721 * Remove all pages from specified address space
2722 * this aids process exit speeds. Also, this code
2723 * is special cased for current process only, but
2724 * can have the more generic (and slightly slower)
2725 * mode enabled. This is much faster than pmap_remove
2726 * in the case of running down an entire address space.
2729 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2731 unsigned *pte, tpte;
2736 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2737 if (!curproc || (pmap != vmspace_pmap(curproc->p_vmspace))) {
2738 printf("warning: pmap_remove_pages called with non-current pmap\n");
2744 for(pv = TAILQ_FIRST(&pmap->pm_pvlist);
2748 if (pv->pv_va >= eva || pv->pv_va < sva) {
2749 npv = TAILQ_NEXT(pv, pv_plist);
2753 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2754 pte = (unsigned *)vtopte(pv->pv_va);
2756 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2761 * We cannot remove wired pages from a process' mapping at this time
2764 npv = TAILQ_NEXT(pv, pv_plist);
2769 m = PHYS_TO_VM_PAGE(tpte);
2771 KASSERT(m < &vm_page_array[vm_page_array_size],
2772 ("pmap_remove_pages: bad tpte %x", tpte));
2774 pv->pv_pmap->pm_stats.resident_count--;
2777 * Update the vm_page_t clean and reference bits.
2784 npv = TAILQ_NEXT(pv, pv_plist);
2785 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2787 m->md.pv_list_count--;
2788 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2789 if (TAILQ_FIRST(&m->md.pv_list) == NULL) {
2790 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2793 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
2797 pmap_TLB_invalidate_all(pmap);
2801 * pmap_testbit tests bits in pte's
2802 * note that the testbit/changebit routines are inline,
2803 * and a lot of things compile-time evaluate.
2806 pmap_testbit(vm_page_t m, int bit)
2812 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2815 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2820 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2822 * if the bit being tested is the modified bit, then
2823 * mark clean_map and ptes as never
2826 if (bit & (PG_A|PG_M)) {
2827 if (!pmap_track_modified(pv->pv_va))
2831 #if defined(PMAP_DIAGNOSTIC)
2833 printf("Null pmap (tb) at va: 0x%x\n", pv->pv_va);
2837 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2848 * this routine is used to modify bits in ptes
2850 static __inline void
2851 pmap_changebit(vm_page_t m, int bit, boolean_t setem)
2857 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2863 * Loop over all current mappings setting/clearing as appropos If
2864 * setting RO do we need to clear the VAC?
2866 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2868 * don't write protect pager mappings
2870 if (!setem && (bit == PG_RW)) {
2871 if (!pmap_track_modified(pv->pv_va))
2875 #if defined(PMAP_DIAGNOSTIC)
2877 printf("Null pmap (cb) at va: 0x%x\n", pv->pv_va);
2882 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2886 pmap_TLB_invalidate(pv->pv_pmap, pv->pv_va);
2888 vm_offset_t pbits = *(vm_offset_t *)pte;
2894 *(int *)pte = pbits & ~(PG_M|PG_RW);
2896 *(int *)pte = pbits & ~bit;
2898 pmap_TLB_invalidate(pv->pv_pmap, pv->pv_va);
2906 * pmap_page_protect:
2908 * Lower the permission for all mappings to a given page.
2911 pmap_page_protect(vm_page_t m, vm_prot_t prot)
2913 if ((prot & VM_PROT_WRITE) == 0) {
2914 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
2915 pmap_changebit(m, PG_RW, FALSE);
2923 pmap_phys_address(int ppn)
2925 return (i386_ptob(ppn));
2929 * pmap_ts_referenced:
2931 * Return a count of reference bits for a page, clearing those bits.
2932 * It is not necessary for every reference bit to be cleared, but it
2933 * is necessary that 0 only be returned when there are truly no
2934 * reference bits set.
2936 * XXX: The exact number of bits to check and clear is a matter that
2937 * should be tested and standardized at some point in the future for
2938 * optimal aging of shared pages.
2941 pmap_ts_referenced(vm_page_t m)
2943 pv_entry_t pv, pvf, pvn;
2948 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2953 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2958 pvn = TAILQ_NEXT(pv, pv_list);
2960 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2962 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2964 if (!pmap_track_modified(pv->pv_va))
2967 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2969 if (pte && (*pte & PG_A)) {
2972 pmap_TLB_invalidate(pv->pv_pmap, pv->pv_va);
2979 } while ((pv = pvn) != NULL && pv != pvf);
2989 * Return whether or not the specified physical page was modified
2990 * in any physical maps.
2993 pmap_is_modified(vm_page_t m)
2995 return pmap_testbit(m, PG_M);
2999 * Clear the modify bits on the specified physical page.
3002 pmap_clear_modify(vm_page_t m)
3004 pmap_changebit(m, PG_M, FALSE);
3008 * pmap_clear_reference:
3010 * Clear the reference bit on the specified physical page.
3013 pmap_clear_reference(vm_page_t m)
3015 pmap_changebit(m, PG_A, FALSE);
3019 * Miscellaneous support routines follow
3023 i386_protection_init(void)
3027 kp = protection_codes;
3028 for (prot = 0; prot < 8; prot++) {
3030 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE:
3032 * Read access is also 0. There isn't any execute bit,
3033 * so just make it readable.
3035 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE:
3036 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE:
3037 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE:
3040 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE:
3041 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE:
3042 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE:
3043 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
3051 * Map a set of physical memory pages into the kernel virtual
3052 * address space. Return a pointer to where it is mapped. This
3053 * routine is intended to be used for mapping device memory,
3056 * NOTE: we can't use pgeflag unless we invalidate the pages one at
3060 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
3062 vm_offset_t va, tmpva, offset;
3065 offset = pa & PAGE_MASK;
3066 size = roundup(offset + size, PAGE_SIZE);
3068 va = kmem_alloc_pageable(kernel_map, size);
3070 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3073 for (tmpva = va; size > 0;) {
3074 pte = (unsigned *)vtopte(tmpva);
3075 *pte = pa | PG_RW | PG_V; /* | pgeflag; */
3082 return ((void *)(va + offset));
3086 pmap_unmapdev(vm_offset_t va, vm_size_t size)
3088 vm_offset_t base, offset;
3090 base = va & PG_FRAME;
3091 offset = va & PAGE_MASK;
3092 size = roundup(offset + size, PAGE_SIZE);
3093 kmem_free(kernel_map, base, size);
3097 * perform the pmap work for mincore
3100 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3102 unsigned *ptep, pte;
3106 ptep = pmap_pte(pmap, addr);
3111 if ((pte = *ptep) != 0) {
3114 val = MINCORE_INCORE;
3115 if ((pte & PG_MANAGED) == 0)
3118 pa = pte & PG_FRAME;
3120 m = PHYS_TO_VM_PAGE(pa);
3126 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3128 * Modified by someone
3130 else if (m->dirty || pmap_is_modified(m))
3131 val |= MINCORE_MODIFIED_OTHER;
3136 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3139 * Referenced by someone
3141 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3142 val |= MINCORE_REFERENCED_OTHER;
3143 vm_page_flag_set(m, PG_REFERENCED);
3150 pmap_activate(struct proc *p)
3154 pmap = vmspace_pmap(p->p_vmspace);
3156 atomic_set_int(&pmap->pm_active, 1 << mycpu->gd_cpuid);
3158 pmap->pm_active |= 1;
3160 #if defined(SWTCH_OPTIM_STATS)
3163 p->p_thread->td_pcb->pcb_cr3 = vtophys(pmap->pm_pdir);
3164 load_cr3(p->p_thread->td_pcb->pcb_cr3);
3168 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3171 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3175 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3180 #if defined(PMAP_DEBUG)
3182 pmap_pid_dump(int pid)
3188 FOREACH_PROC_IN_SYSTEM(p) {
3189 if (p->p_pid != pid)
3195 pmap = vmspace_pmap(p->p_vmspace);
3196 for(i=0;i<1024;i++) {
3199 unsigned base = i << PDRSHIFT;
3201 pde = &pmap->pm_pdir[i];
3202 if (pde && pmap_pde_v(pde)) {
3203 for(j=0;j<1024;j++) {
3204 unsigned va = base + (j << PAGE_SHIFT);
3205 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) {
3212 pte = pmap_pte_quick( pmap, va);
3213 if (pte && pmap_pte_v(pte)) {
3217 m = PHYS_TO_VM_PAGE(pa);
3218 printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x",
3219 va, pa, m->hold_count, m->wire_count, m->flags);
3240 static void pads (pmap_t pm);
3241 void pmap_pvdump (vm_paddr_t pa);
3243 /* print address space of pmap*/
3250 if (pm == kernel_pmap)
3252 for (i = 0; i < 1024; i++)
3254 for (j = 0; j < 1024; j++) {
3255 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
3256 if (pm == kernel_pmap && va < KERNBASE)
3258 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS)
3260 ptep = pmap_pte_quick(pm, va);
3261 if (pmap_pte_v(ptep))
3262 printf("%x:%x ", va, *(int *) ptep);
3268 pmap_pvdump(vm_paddr_t pa)
3273 printf("pa %08llx", (long long)pa);
3274 m = PHYS_TO_VM_PAGE(pa);
3275 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3277 printf(" -> pmap %p, va %x, flags %x",
3278 (void *)pv->pv_pmap, pv->pv_va, pv->pv_flags);
3280 printf(" -> pmap %p, va %x", (void *)pv->pv_pmap, pv->pv_va);