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
46 * Manages physical address maps.
48 * In addition to hardware address maps, this
49 * module is called upon to provide software-use-only
50 * maps which may or may not be stored in the same
51 * form as hardware maps. These pseudo-maps are
52 * used to store intermediate results from copy
53 * operations to and from address spaces.
55 * Since the information managed by this module is
56 * also stored by the logical address mapping module,
57 * this module may throw away valid virtual-to-physical
58 * mappings at almost any time. However, invalidations
59 * of virtual-to-physical mappings must be done as
62 * In order to cope with hardware architectures which
63 * make virtual-to-physical map invalidates expensive,
64 * this module may delay invalidate or reduced protection
65 * operations until such time as they are actually
66 * necessary. This module is given full information as
67 * to which processors are currently using which maps,
68 * and to when physical maps must be made correct.
71 #include "opt_disable_pse.h"
73 #include "opt_msgbuf.h"
74 #include "opt_kstack_pages.h"
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
81 #include <sys/msgbuf.h>
82 #include <sys/mutex.h>
86 #include <sys/vmmeter.h>
87 #include <sys/sysctl.h>
90 #include <vm/vm_param.h>
91 #include <vm/vm_kern.h>
92 #include <vm/vm_page.h>
93 #include <vm/vm_map.h>
94 #include <vm/vm_object.h>
95 #include <vm/vm_extern.h>
96 #include <vm/vm_pageout.h>
97 #include <vm/vm_pager.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 #include <machine/segments.h>
107 #include <machine/tss.h>
108 #endif /* SMP || APIC_IO */
110 #define PMAP_KEEP_PDIRS
111 #ifndef PMAP_SHPGPERPROC
112 #define PMAP_SHPGPERPROC 200
115 #if defined(DIAGNOSTIC)
116 #define PMAP_DIAGNOSTIC
121 #if !defined(PMAP_DIAGNOSTIC)
122 #define PMAP_INLINE __inline
128 * Get PDEs and PTEs for user/kernel address space
130 #define pmap_pde(m, v) (&((m)->pm_pdir[(vm_offset_t)(v) >> PDRSHIFT]))
131 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
133 #define pmap_pde_v(pte) ((*(int *)pte & PG_V) != 0)
134 #define pmap_pte_w(pte) ((*(int *)pte & PG_W) != 0)
135 #define pmap_pte_m(pte) ((*(int *)pte & PG_M) != 0)
136 #define pmap_pte_u(pte) ((*(int *)pte & PG_A) != 0)
137 #define pmap_pte_v(pte) ((*(int *)pte & PG_V) != 0)
139 #define pmap_pte_set_w(pte, v) ((v)?(*(int *)pte |= PG_W):(*(int *)pte &= ~PG_W))
140 #define pmap_pte_set_prot(pte, v) ((*(int *)pte &= ~PG_PROT), (*(int *)pte |= (v)))
143 * Given a map and a machine independent protection code,
144 * convert to a vax protection code.
146 #define pte_prot(m, p) (protection_codes[p])
147 static int protection_codes[8];
149 struct pmap kernel_pmap_store;
150 LIST_HEAD(pmaplist, pmap);
151 struct pmaplist allpmaps;
153 vm_offset_t avail_start; /* PA of first available physical page */
154 vm_offset_t avail_end; /* PA of last available physical page */
155 vm_offset_t virtual_avail; /* VA of first avail page (after kernel bss) */
156 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
157 static boolean_t pmap_initialized = FALSE; /* Has pmap_init completed? */
158 static int pgeflag; /* PG_G or-in */
159 static int pseflag; /* PG_PS or-in */
161 static vm_object_t kptobj;
164 vm_offset_t kernel_vm_end;
167 * Data for the pv entry allocation mechanism
169 static uma_zone_t pvzone;
170 static struct vm_object pvzone_obj;
171 static int pv_entry_count = 0, pv_entry_max = 0, pv_entry_high_water = 0;
172 static int pmap_pagedaemon_waken = 0;
175 * All those kernel PT submaps that BSD is so fond of
177 pt_entry_t *CMAP1 = 0;
178 static pt_entry_t *CMAP2, *ptmmap;
179 caddr_t CADDR1 = 0, ptvmmap = 0;
180 static caddr_t CADDR2;
181 static pt_entry_t *msgbufmap;
182 struct msgbuf *msgbufp = 0;
187 static pt_entry_t *pt_crashdumpmap;
188 static caddr_t crashdumpmap;
191 extern pt_entry_t *SMPpt;
193 static pt_entry_t *PMAP1 = 0;
194 static pt_entry_t *PADDR1 = 0;
196 static PMAP_INLINE void free_pv_entry(pv_entry_t pv);
197 static pt_entry_t *get_ptbase(pmap_t pmap);
198 static pv_entry_t get_pv_entry(void);
199 static void i386_protection_init(void);
200 static __inline void pmap_changebit(vm_page_t m, int bit, boolean_t setem);
202 static void pmap_remove_all(vm_page_t m);
203 static vm_page_t pmap_enter_quick(pmap_t pmap, vm_offset_t va,
204 vm_page_t m, vm_page_t mpte);
205 static int pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t sva);
206 static void pmap_remove_page(struct pmap *pmap, vm_offset_t va);
207 static int pmap_remove_entry(struct pmap *pmap, vm_page_t m,
209 static boolean_t pmap_testbit(vm_page_t m, int bit);
210 static void pmap_insert_entry(pmap_t pmap, vm_offset_t va,
211 vm_page_t mpte, vm_page_t m);
213 static vm_page_t pmap_allocpte(pmap_t pmap, vm_offset_t va);
215 static int pmap_release_free_page(pmap_t pmap, vm_page_t p);
216 static vm_page_t _pmap_allocpte(pmap_t pmap, unsigned ptepindex);
217 static pt_entry_t *pmap_pte_quick(pmap_t pmap, vm_offset_t va);
218 static vm_page_t pmap_page_lookup(vm_object_t object, vm_pindex_t pindex);
219 static int pmap_unuse_pt(pmap_t, vm_offset_t, vm_page_t);
220 static vm_offset_t pmap_kmem_choose(vm_offset_t addr);
221 static void *pmap_allocf(uma_zone_t zone, int bytes, u_int8_t *flags, int wait);
223 static pd_entry_t pdir4mb;
228 * Extract the page table entry associated
229 * with the given map/virtual_address pair.
232 PMAP_INLINE pt_entry_t *
234 register pmap_t pmap;
240 pdeaddr = pmap_pde(pmap, va);
241 if (*pdeaddr & PG_PS)
244 return get_ptbase(pmap) + i386_btop(va);
251 * Move the kernel virtual free pointer to the next
252 * 4MB. This is used to help improve performance
253 * by using a large (4MB) page for much of the kernel
254 * (.text, .data, .bss)
257 pmap_kmem_choose(vm_offset_t addr)
259 vm_offset_t newaddr = addr;
261 if (cpu_feature & CPUID_PSE) {
262 newaddr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
269 * Bootstrap the system enough to run with virtual memory.
271 * On the i386 this is called after mapping has already been enabled
272 * and just syncs the pmap module with what has already been done.
273 * [We can't call it easily with mapping off since the kernel is not
274 * mapped with PA == VA, hence we would have to relocate every address
275 * from the linked base (virtual) address "KERNBASE" to the actual
276 * (physical) address starting relative to 0]
279 pmap_bootstrap(firstaddr, loadaddr)
280 vm_offset_t firstaddr;
281 vm_offset_t loadaddr;
287 avail_start = firstaddr;
290 * XXX The calculation of virtual_avail is wrong. It's NKPT*PAGE_SIZE too
291 * large. It should instead be correctly calculated in locore.s and
292 * not based on 'first' (which is a physical address, not a virtual
293 * address, for the start of unused physical memory). The kernel
294 * page tables are NOT double mapped and thus should not be included
295 * in this calculation.
297 virtual_avail = (vm_offset_t) KERNBASE + firstaddr;
298 virtual_avail = pmap_kmem_choose(virtual_avail);
300 virtual_end = VM_MAX_KERNEL_ADDRESS;
303 * Initialize protection array.
305 i386_protection_init();
308 * Initialize the kernel pmap (which is statically allocated).
310 kernel_pmap->pm_pdir = (pd_entry_t *) (KERNBASE + (u_int)IdlePTD);
311 kernel_pmap->pm_active = -1; /* don't allow deactivation */
312 TAILQ_INIT(&kernel_pmap->pm_pvlist);
313 LIST_INIT(&allpmaps);
314 LIST_INSERT_HEAD(&allpmaps, kernel_pmap, pm_list);
318 * Reserve some special page table entries/VA space for temporary
321 #define SYSMAP(c, p, v, n) \
322 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
325 pte = (pt_entry_t *) pmap_pte(kernel_pmap, va);
328 * CMAP1/CMAP2 are used for zeroing and copying pages.
330 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
331 SYSMAP(caddr_t, CMAP2, CADDR2, 1)
336 SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS);
339 * ptvmmap is used for reading arbitrary physical pages via /dev/mem.
340 * XXX ptmmap is not used.
342 SYSMAP(caddr_t, ptmmap, ptvmmap, 1)
345 * msgbufp is used to map the system message buffer.
346 * XXX msgbufmap is not used.
348 SYSMAP(struct msgbuf *, msgbufmap, msgbufp,
349 atop(round_page(MSGBUF_SIZE)))
352 * ptemap is used for pmap_pte_quick
354 SYSMAP(pt_entry_t *, PMAP1, PADDR1, 1);
359 for (i = 0; i < NKPT; i++)
363 #if !defined(SMP) /* XXX - see also mp_machdep.c */
364 if (cpu_feature & CPUID_PGE) {
370 * Initialize the 4MB page size flag
374 * The 4MB page version of the initial
375 * kernel page mapping.
379 #if !defined(DISABLE_PSE)
380 if (cpu_feature & CPUID_PSE) {
383 * Note that we have enabled PSE mode
386 ptditmp = *(PTmap + i386_btop(KERNBASE));
387 ptditmp &= ~(NBPDR - 1);
388 ptditmp |= PG_V | PG_RW | PG_PS | PG_U | pgeflag;
393 * Enable the PSE mode.
395 load_cr4(rcr4() | CR4_PSE);
398 * We can do the mapping here for the single processor
399 * case. We simply ignore the old page table page from
403 * For SMP, we still need 4K pages to bootstrap APs,
404 * PSE will be enabled as soon as all APs are up.
406 PTD[KPTDI] = (pd_entry_t) ptditmp;
407 kernel_pmap->pm_pdir[KPTDI] = (pd_entry_t) ptditmp;
414 if (cpu_apic_address == 0)
415 panic("pmap_bootstrap: no local apic! (non-SMP hardware?)");
417 /* local apic is mapped on last page */
418 SMPpt[NPTEPG - 1] = (pt_entry_t)(PG_V | PG_RW | PG_N | pgeflag |
419 (cpu_apic_address & PG_FRAME));
427 * Set 4mb pdir for mp startup
432 if (pseflag && (cpu_feature & CPUID_PSE)) {
433 load_cr4(rcr4() | CR4_PSE);
434 if (pdir4mb && PCPU_GET(cpuid) == 0) { /* only on BSP */
435 kernel_pmap->pm_pdir[KPTDI] = PTD[KPTDI] = pdir4mb;
443 pmap_allocf(uma_zone_t zone, int bytes, u_int8_t *flags, int wait)
445 *flags = UMA_SLAB_PRIV;
446 return (void *)kmem_alloc(kernel_map, bytes);
450 * Initialize the pmap module.
451 * Called by vm_init, to initialize any structures that the pmap
452 * system needs to map virtual memory.
453 * pmap_init has been enhanced to support in a fairly consistant
454 * way, discontiguous physical memory.
457 pmap_init(phys_start, phys_end)
458 vm_offset_t phys_start, phys_end;
464 * object for kernel page table pages
466 kptobj = vm_object_allocate(OBJT_DEFAULT, NKPDE);
469 * Allocate memory for random pmap data structures. Includes the
473 for(i = 0; i < vm_page_array_size; i++) {
476 m = &vm_page_array[i];
477 TAILQ_INIT(&m->md.pv_list);
478 m->md.pv_list_count = 0;
482 * init the pv free list
484 initial_pvs = vm_page_array_size;
485 if (initial_pvs < MINPV)
487 pvzone = uma_zcreate("PV ENTRY", sizeof (struct pv_entry), NULL, NULL,
488 NULL, NULL, UMA_ALIGN_PTR, 0);
489 uma_zone_set_allocf(pvzone, pmap_allocf);
490 uma_prealloc(pvzone, initial_pvs);
493 * Now it is safe to enable pv_table recording.
495 pmap_initialized = TRUE;
499 * Initialize the address space (zone) for the pv_entries. Set a
500 * high water mark so that the system can recover from excessive
501 * numbers of pv entries.
506 int shpgperproc = PMAP_SHPGPERPROC;
508 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
509 pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
510 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
511 pv_entry_high_water = 9 * (pv_entry_max / 10);
512 uma_zone_set_obj(pvzone, &pvzone_obj, pv_entry_max);
516 /***************************************************
517 * Low level helper routines.....
518 ***************************************************/
520 #if defined(PMAP_DIAGNOSTIC)
523 * This code checks for non-writeable/modified pages.
524 * This should be an invalid condition.
527 pmap_nw_modified(pt_entry_t ptea)
533 if ((pte & (PG_M|PG_RW)) == PG_M)
542 * this routine defines the region(s) of memory that should
543 * not be tested for the modified bit.
545 static PMAP_INLINE int
546 pmap_track_modified(vm_offset_t va)
548 if ((va < kmi.clean_sva) || (va >= kmi.clean_eva))
554 static PMAP_INLINE void
555 invltlb_1pg(vm_offset_t va)
565 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
568 if (pmap->pm_active & PCPU_GET(cpumask))
569 cpu_invlpg((void *)va);
570 if (pmap->pm_active & PCPU_GET(other_cpus))
579 pmap_invalidate_all(pmap_t pmap)
582 if (pmap->pm_active & PCPU_GET(cpumask))
584 if (pmap->pm_active & PCPU_GET(other_cpus))
593 * Return an address which is the base of the Virtual mapping of
594 * all the PTEs for the given pmap. Note this doesn't say that
595 * all the PTEs will be present or that the pages there are valid.
596 * The PTEs are made available by the recursive mapping trick.
597 * It will map in the alternate PTE space if needed.
603 pd_entry_t frame = pmap->pm_pdir[PTDPTDI] & PG_FRAME;
605 /* are we current address space or kernel? */
606 if (pmap == kernel_pmap || frame == (PTDpde & PG_FRAME))
608 /* otherwise, we are alternate address space */
609 if (frame != (APTDpde & PG_FRAME)) {
610 APTDpde = (pd_entry_t) (frame | PG_RW | PG_V);
612 /* The page directory is not shared between CPUs */
622 * Super fast pmap_pte routine best used when scanning
623 * the pv lists. This eliminates many coarse-grained
624 * invltlb calls. Note that many of the pv list
625 * scans are across different pmaps. It is very wasteful
626 * to do an entire invltlb for checking a single mapping.
630 pmap_pte_quick(pmap, va)
631 register pmap_t pmap;
634 pd_entry_t pde, newpf;
635 pde = pmap->pm_pdir[va >> PDRSHIFT];
637 pd_entry_t frame = pmap->pm_pdir[PTDPTDI] & PG_FRAME;
638 unsigned index = i386_btop(va);
639 /* are we current address space or kernel? */
640 if (pmap == kernel_pmap || frame == (PTDpde & PG_FRAME))
641 return PTmap + index;
642 newpf = pde & PG_FRAME;
643 if (((*PMAP1) & PG_FRAME) != newpf) {
644 *PMAP1 = newpf | PG_RW | PG_V;
645 invltlb_1pg((vm_offset_t) PADDR1);
647 return PADDR1 + (index & (NPTEPG - 1));
653 * Routine: pmap_extract
655 * Extract the physical page address associated
656 * with the given map/virtual_address pair.
659 pmap_extract(pmap, va)
660 register pmap_t pmap;
663 vm_offset_t rtval; /* XXX FIXME */
664 vm_offset_t pdirindex;
668 pdirindex = va >> PDRSHIFT;
669 rtval = pmap->pm_pdir[pdirindex];
672 if ((rtval & PG_PS) != 0) {
673 rtval &= ~(NBPDR - 1);
674 rtval |= va & (NBPDR - 1);
677 pte = get_ptbase(pmap) + i386_btop(va);
678 rtval = ((*pte & PG_FRAME) | (va & PAGE_MASK));
685 /***************************************************
686 * Low level mapping routines.....
687 ***************************************************/
690 * add a wired page to the kva
691 * note that in order for the mapping to take effect -- you
692 * should do a invltlb after doing the pmap_kenter...
695 pmap_kenter(vm_offset_t va, vm_offset_t pa)
698 pt_entry_t npte, opte;
700 npte = pa | PG_RW | PG_V | pgeflag;
708 * remove a page from the kernel pagetables
711 pmap_kremove(vm_offset_t va)
713 register pt_entry_t *pte;
721 * Used to map a range of physical addresses into kernel
722 * virtual address space.
724 * The value passed in '*virt' is a suggested virtual address for
725 * the mapping. Architectures which can support a direct-mapped
726 * physical to virtual region can return the appropriate address
727 * within that region, leaving '*virt' unchanged. Other
728 * architectures should map the pages starting at '*virt' and
729 * update '*virt' with the first usable address after the mapped
733 pmap_map(vm_offset_t *virt, vm_offset_t start, vm_offset_t end, int prot)
735 vm_offset_t sva = *virt;
736 vm_offset_t va = sva;
737 while (start < end) {
738 pmap_kenter(va, start);
748 * Add a list of wired pages to the kva
749 * this routine is only used for temporary
750 * kernel mappings that do not need to have
751 * page modification or references recorded.
752 * Note that old mappings are simply written
753 * over. The page *must* be wired.
756 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
760 end_va = va + count * PAGE_SIZE;
762 while (va < end_va) {
766 *pte = VM_PAGE_TO_PHYS(*m) | PG_RW | PG_V | pgeflag;
768 cpu_invlpg((void *)va);
781 * this routine jerks page mappings from the
782 * kernel -- it is meant only for temporary mappings.
785 pmap_qremove(vm_offset_t va, int count)
789 end_va = va + count*PAGE_SIZE;
791 while (va < end_va) {
797 cpu_invlpg((void *)va);
809 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
813 m = vm_page_lookup(object, pindex);
814 if (m && vm_page_sleep_busy(m, FALSE, "pplookp"))
820 * Create the Uarea stack for a new process.
821 * This routine directly affects the fork perf for a process.
824 pmap_new_proc(struct proc *p)
827 int updateneeded = 0;
833 pt_entry_t *ptek, oldpte;
836 * allocate object for the upages
838 upobj = p->p_upages_obj;
840 upobj = vm_object_allocate(OBJT_DEFAULT, UAREA_PAGES);
841 p->p_upages_obj = upobj;
844 /* get a kernel virtual address for the U area for this thread */
845 up = (vm_offset_t)p->p_uarea;
847 up = kmem_alloc_nofault(kernel_map, UAREA_PAGES * PAGE_SIZE);
849 panic("pmap_new_proc: upage allocation failed");
850 p->p_uarea = (struct user *)up;
855 for (i = 0; i < UAREA_PAGES; i++) {
857 * Get a kernel stack page
859 m = vm_page_grab(upobj, i, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
867 oldpte = *(ptek + i);
869 * Enter the page into the kernel address space.
871 *(ptek + i) = VM_PAGE_TO_PHYS(m) | PG_RW | PG_V | pgeflag;
876 invlpg(up + i * PAGE_SIZE);
881 vm_page_flag_clear(m, PG_ZERO);
882 vm_page_flag_set(m, PG_MAPPED | PG_WRITEABLE);
883 m->valid = VM_PAGE_BITS_ALL;
892 * Dispose the U-Area for a process that has exited.
893 * This routine directly impacts the exit perf of a process.
903 pt_entry_t *ptek, oldpte;
905 upobj = p->p_upages_obj;
906 up = (vm_offset_t)p->p_uarea;
908 for (i = 0; i < UAREA_PAGES; i++) {
909 m = vm_page_lookup(upobj, i);
911 panic("pmap_dispose_proc: upage already missing?");
913 oldpte = *(ptek + i);
916 invlpg(up + i * PAGE_SIZE);
918 vm_page_unwire(m, 0);
926 * If the process got swapped out some of its UPAGES might have gotten
927 * swapped. Just get rid of the object to clean up the swap use
928 * proactively. NOTE! might block waiting for paging I/O to complete.
930 if (upobj->type == OBJT_SWAP) {
931 p->p_upages_obj = NULL;
932 vm_object_deallocate(upobj);
937 * Allow the U_AREA for a process to be prejudicially paged out.
948 upobj = p->p_upages_obj;
949 up = (vm_offset_t)p->p_uarea;
950 for (i = 0; i < UAREA_PAGES; i++) {
951 m = vm_page_lookup(upobj, i);
953 panic("pmap_swapout_proc: upage already missing?");
955 vm_page_unwire(m, 0);
956 pmap_kremove(up + i * PAGE_SIZE);
961 * Bring the U-Area for a specified process back in.
972 upobj = p->p_upages_obj;
973 up = (vm_offset_t)p->p_uarea;
974 for (i = 0; i < UAREA_PAGES; i++) {
975 m = vm_page_grab(upobj, i, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
976 pmap_kenter(up + i * PAGE_SIZE, VM_PAGE_TO_PHYS(m));
977 if (m->valid != VM_PAGE_BITS_ALL) {
978 rv = vm_pager_get_pages(upobj, &m, 1, 0);
979 if (rv != VM_PAGER_OK)
980 panic("pmap_swapin_proc: cannot get upage for proc: %d\n", p->p_pid);
981 m = vm_page_lookup(upobj, i);
982 m->valid = VM_PAGE_BITS_ALL;
986 vm_page_flag_set(m, PG_MAPPED | PG_WRITEABLE);
991 * Create the kernel stack (including pcb for i386) for a new thread.
992 * This routine directly affects the fork perf for a process and
993 * create performance for a thread.
996 pmap_new_thread(struct thread *td)
999 int updateneeded = 0;
1005 pt_entry_t *ptek, oldpte;
1008 * allocate object for the kstack
1010 ksobj = td->td_kstack_obj;
1011 if (ksobj == NULL) {
1012 ksobj = vm_object_allocate(OBJT_DEFAULT, KSTACK_PAGES);
1013 td->td_kstack_obj = ksobj;
1017 /* get a kernel virtual address for the kstack for this thread */
1020 ks = kmem_alloc_nofault(kernel_map,
1021 (KSTACK_PAGES + 1) * PAGE_SIZE);
1023 panic("pmap_new_thread: kstack allocation failed");
1028 ptek = vtopte(ks - PAGE_SIZE);
1035 invlpg(ks - PAGE_SIZE);
1040 /* get a kernel virtual address for the kstack for this thread */
1043 ks = kmem_alloc_nofault(kernel_map, KSTACK_PAGES * PAGE_SIZE);
1045 panic("pmap_new_thread: kstack allocation failed");
1050 for (i = 0; i < KSTACK_PAGES; i++) {
1052 * Get a kernel stack page
1054 m = vm_page_grab(ksobj, i, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1062 oldpte = *(ptek + i);
1064 * Enter the page into the kernel address space.
1066 *(ptek + i) = VM_PAGE_TO_PHYS(m) | PG_RW | PG_V | pgeflag;
1071 invlpg(ks + i * PAGE_SIZE);
1076 vm_page_flag_clear(m, PG_ZERO);
1077 vm_page_flag_set(m, PG_MAPPED | PG_WRITEABLE);
1078 m->valid = VM_PAGE_BITS_ALL;
1087 * Dispose the kernel stack for a thread that has exited.
1088 * This routine directly impacts the exit perf of a process and thread.
1091 pmap_dispose_thread(td)
1098 pt_entry_t *ptek, oldpte;
1100 ksobj = td->td_kstack_obj;
1103 for (i = 0; i < KSTACK_PAGES; i++) {
1104 m = vm_page_lookup(ksobj, i);
1106 panic("pmap_dispose_thread: kstack already missing?");
1108 oldpte = *(ptek + i);
1111 invlpg(ks + i * PAGE_SIZE);
1113 vm_page_unwire(m, 0);
1121 * If the thread got swapped out some of its KSTACK might have gotten
1122 * swapped. Just get rid of the object to clean up the swap use
1123 * proactively. NOTE! might block waiting for paging I/O to complete.
1125 if (ksobj->type == OBJT_SWAP) {
1126 td->td_kstack_obj = NULL;
1127 vm_object_deallocate(ksobj);
1132 * Allow the Kernel stack for a thread to be prejudicially paged out.
1135 pmap_swapout_thread(td)
1143 ksobj = td->td_kstack_obj;
1145 for (i = 0; i < KSTACK_PAGES; i++) {
1146 m = vm_page_lookup(ksobj, i);
1148 panic("pmap_swapout_thread: kstack already missing?");
1150 vm_page_unwire(m, 0);
1151 pmap_kremove(ks + i * PAGE_SIZE);
1156 * Bring the kernel stack for a specified thread back in.
1159 pmap_swapin_thread(td)
1167 ksobj = td->td_kstack_obj;
1169 for (i = 0; i < KSTACK_PAGES; i++) {
1170 m = vm_page_grab(ksobj, i, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1171 pmap_kenter(ks + i * PAGE_SIZE, VM_PAGE_TO_PHYS(m));
1172 if (m->valid != VM_PAGE_BITS_ALL) {
1173 rv = vm_pager_get_pages(ksobj, &m, 1, 0);
1174 if (rv != VM_PAGER_OK)
1175 panic("pmap_swapin_thread: cannot get kstack for proc: %d\n", td->td_proc->p_pid);
1176 m = vm_page_lookup(ksobj, i);
1177 m->valid = VM_PAGE_BITS_ALL;
1181 vm_page_flag_set(m, PG_MAPPED | PG_WRITEABLE);
1185 /***************************************************
1186 * Page table page management routines.....
1187 ***************************************************/
1190 * This routine unholds page table pages, and if the hold count
1191 * drops to zero, then it decrements the wire count.
1194 _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
1197 while (vm_page_sleep_busy(m, FALSE, "pmuwpt"))
1200 if (m->hold_count == 0) {
1203 * unmap the page table page
1205 pmap->pm_pdir[m->pindex] = 0;
1206 --pmap->pm_stats.resident_count;
1207 if ((pmap->pm_pdir[PTDPTDI] & PG_FRAME) ==
1208 (PTDpde & PG_FRAME)) {
1210 * Do a invltlb to make the invalidated mapping
1211 * take effect immediately.
1213 pteva = VM_MAXUSER_ADDRESS + i386_ptob(m->pindex);
1214 pmap_invalidate_page(pmap, pteva);
1217 if (pmap->pm_ptphint == m)
1218 pmap->pm_ptphint = NULL;
1221 * If the page is finally unwired, simply free it.
1224 if (m->wire_count == 0) {
1228 vm_page_free_zero(m);
1236 static PMAP_INLINE int
1237 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
1240 if (m->hold_count == 0)
1241 return _pmap_unwire_pte_hold(pmap, m);
1247 * After removing a page table entry, this routine is used to
1248 * conditionally free the page, and manage the hold/wire counts.
1251 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte)
1254 if (va >= VM_MAXUSER_ADDRESS)
1258 ptepindex = (va >> PDRSHIFT);
1259 if (pmap->pm_ptphint &&
1260 (pmap->pm_ptphint->pindex == ptepindex)) {
1261 mpte = pmap->pm_ptphint;
1263 mpte = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1264 pmap->pm_ptphint = mpte;
1268 return pmap_unwire_pte_hold(pmap, mpte);
1276 (pd_entry_t *)kmem_alloc_pageable(kernel_map, PAGE_SIZE);
1277 pmap_kenter((vm_offset_t) pmap->pm_pdir, (vm_offset_t) IdlePTD);
1278 pmap->pm_ptphint = NULL;
1279 pmap->pm_active = 0;
1280 TAILQ_INIT(&pmap->pm_pvlist);
1281 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1282 LIST_INSERT_HEAD(&allpmaps, pmap, pm_list);
1286 * Initialize a preallocated and zeroed pmap structure,
1287 * such as one in a vmspace structure.
1291 register struct pmap *pmap;
1296 * No need to allocate page table space yet but we do need a valid
1297 * page directory table.
1299 if (pmap->pm_pdir == NULL)
1301 (pd_entry_t *)kmem_alloc_pageable(kernel_map, PAGE_SIZE);
1304 * allocate object for the ptes
1306 if (pmap->pm_pteobj == NULL)
1307 pmap->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, PTDPTDI + 1);
1310 * allocate the page directory page
1312 ptdpg = vm_page_grab(pmap->pm_pteobj, PTDPTDI,
1313 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1315 ptdpg->wire_count = 1;
1319 vm_page_flag_clear(ptdpg, PG_MAPPED | PG_BUSY); /* not usually mapped*/
1320 ptdpg->valid = VM_PAGE_BITS_ALL;
1322 pmap_kenter((vm_offset_t) pmap->pm_pdir, VM_PAGE_TO_PHYS(ptdpg));
1323 if ((ptdpg->flags & PG_ZERO) == 0)
1324 bzero(pmap->pm_pdir, PAGE_SIZE);
1326 LIST_INSERT_HEAD(&allpmaps, pmap, pm_list);
1327 /* Wire in kernel global address entries. */
1328 /* XXX copies current process, does not fill in MPPTDI */
1329 bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * PTESIZE);
1331 pmap->pm_pdir[MPPTDI] = PTD[MPPTDI];
1334 /* install self-referential address mapping entry */
1335 pmap->pm_pdir[PTDPTDI] =
1336 VM_PAGE_TO_PHYS(ptdpg) | PG_V | PG_RW | PG_A | PG_M;
1338 pmap->pm_active = 0;
1339 pmap->pm_ptphint = NULL;
1340 TAILQ_INIT(&pmap->pm_pvlist);
1341 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1345 * Wire in kernel global address entries. To avoid a race condition
1346 * between pmap initialization and pmap_growkernel, this procedure
1347 * should be called after the vmspace is attached to the process
1348 * but before this pmap is activated.
1354 /* XXX: Remove this stub when no longer called */
1358 pmap_release_free_page(pmap_t pmap, vm_page_t p)
1360 pd_entry_t *pde = pmap->pm_pdir;
1362 * This code optimizes the case of freeing non-busy
1363 * page-table pages. Those pages are zero now, and
1364 * might as well be placed directly into the zero queue.
1366 if (vm_page_sleep_busy(p, FALSE, "pmaprl"))
1372 * Remove the page table page from the processes address space.
1375 pmap->pm_stats.resident_count--;
1377 if (p->hold_count) {
1378 panic("pmap_release: freeing held page table page");
1381 * Page directory pages need to have the kernel
1382 * stuff cleared, so they can go into the zero queue also.
1384 if (p->pindex == PTDPTDI) {
1385 bzero(pde + KPTDI, nkpt * PTESIZE);
1390 pmap_kremove((vm_offset_t) pmap->pm_pdir);
1393 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1394 pmap->pm_ptphint = NULL;
1398 vm_page_free_zero(p);
1403 * this routine is called if the page table page is not
1407 _pmap_allocpte(pmap, ptepindex)
1411 vm_offset_t pteva, ptepa; /* XXXPA */
1415 * Find or fabricate a new pagetable page
1417 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1418 VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1420 KASSERT(m->queue == PQ_NONE,
1421 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1423 if (m->wire_count == 0)
1428 * Increment the hold count for the page table page
1429 * (denoting a new mapping.)
1434 * Map the pagetable page into the process address space, if
1435 * it isn't already there.
1438 pmap->pm_stats.resident_count++;
1440 ptepa = VM_PAGE_TO_PHYS(m);
1441 pmap->pm_pdir[ptepindex] =
1442 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_A | PG_M);
1445 * Set the page table hint
1447 pmap->pm_ptphint = m;
1450 * Try to use the new mapping, but if we cannot, then
1451 * do it with the routine that maps the page explicitly.
1453 if ((m->flags & PG_ZERO) == 0) {
1454 if ((pmap->pm_pdir[PTDPTDI] & PG_FRAME) ==
1455 (PTDpde & PG_FRAME)) {
1456 pteva = VM_MAXUSER_ADDRESS + i386_ptob(ptepindex);
1457 bzero((caddr_t) pteva, PAGE_SIZE);
1463 m->valid = VM_PAGE_BITS_ALL;
1464 vm_page_flag_clear(m, PG_ZERO);
1465 vm_page_flag_set(m, PG_MAPPED);
1472 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1479 * Calculate pagetable page index
1481 ptepindex = va >> PDRSHIFT;
1484 * Get the page directory entry
1486 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1489 * This supports switching from a 4MB page to a
1492 if (ptepa & PG_PS) {
1493 pmap->pm_pdir[ptepindex] = 0;
1499 * If the page table page is mapped, we just increment the
1500 * hold count, and activate it.
1504 * In order to get the page table page, try the
1507 if (pmap->pm_ptphint &&
1508 (pmap->pm_ptphint->pindex == ptepindex)) {
1509 m = pmap->pm_ptphint;
1511 m = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1512 pmap->pm_ptphint = m;
1518 * Here if the pte page isn't mapped, or if it has been deallocated.
1520 return _pmap_allocpte(pmap, ptepindex);
1524 /***************************************************
1525 * Pmap allocation/deallocation routines.
1526 ***************************************************/
1529 * Release any resources held by the given physical map.
1530 * Called when a pmap initialized by pmap_pinit is being released.
1531 * Should only be called if the map contains no valid mappings.
1534 pmap_release(pmap_t pmap)
1536 vm_page_t p,n,ptdpg;
1537 vm_object_t object = pmap->pm_pteobj;
1540 #if defined(DIAGNOSTIC)
1541 if (object->ref_count != 1)
1542 panic("pmap_release: pteobj reference count != 1");
1546 LIST_REMOVE(pmap, pm_list);
1548 curgeneration = object->generation;
1549 for (p = TAILQ_FIRST(&object->memq); p != NULL; p = n) {
1550 n = TAILQ_NEXT(p, listq);
1551 if (p->pindex == PTDPTDI) {
1556 if (!pmap_release_free_page(pmap, p) &&
1557 (object->generation != curgeneration))
1562 if (ptdpg && !pmap_release_free_page(pmap, ptdpg))
1567 kvm_size(SYSCTL_HANDLER_ARGS)
1569 unsigned long ksize = VM_MAX_KERNEL_ADDRESS - KERNBASE;
1571 return sysctl_handle_long(oidp, &ksize, 0, req);
1573 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD,
1574 0, 0, kvm_size, "IU", "Size of KVM");
1577 kvm_free(SYSCTL_HANDLER_ARGS)
1579 unsigned long kfree = VM_MAX_KERNEL_ADDRESS - kernel_vm_end;
1581 return sysctl_handle_long(oidp, &kfree, 0, req);
1583 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD,
1584 0, 0, kvm_free, "IU", "Amount of KVM free");
1587 * grow the number of kernel page table entries, if needed
1590 pmap_growkernel(vm_offset_t addr)
1594 vm_offset_t ptppaddr;
1599 if (kernel_vm_end == 0) {
1600 kernel_vm_end = KERNBASE;
1602 while (pdir_pde(PTD, kernel_vm_end)) {
1603 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1607 addr = (addr + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1608 while (kernel_vm_end < addr) {
1609 if (pdir_pde(PTD, kernel_vm_end)) {
1610 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1615 * This index is bogus, but out of the way
1617 nkpg = vm_page_alloc(kptobj, nkpt, VM_ALLOC_SYSTEM);
1619 panic("pmap_growkernel: no memory to grow kernel");
1624 pmap_zero_page(nkpg);
1625 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1626 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
1627 pdir_pde(PTD, kernel_vm_end) = newpdir;
1629 LIST_FOREACH(pmap, &allpmaps, pm_list) {
1630 *pmap_pde(pmap, kernel_vm_end) = newpdir;
1632 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1638 /***************************************************
1639 * page management routines.
1640 ***************************************************/
1643 * free the pv_entry back to the free list
1645 static PMAP_INLINE void
1646 free_pv_entry(pv_entry_t pv)
1649 uma_zfree(pvzone, pv);
1653 * get a new pv_entry, allocating a block from the system
1655 * the memory allocation is performed bypassing the malloc code
1656 * because of the possibility of allocations at interrupt time.
1662 if (pv_entry_high_water &&
1663 (pv_entry_count > pv_entry_high_water) &&
1664 (pmap_pagedaemon_waken == 0)) {
1665 pmap_pagedaemon_waken = 1;
1666 wakeup (&vm_pages_needed);
1668 return uma_zalloc(pvzone, M_NOWAIT);
1672 * This routine is very drastic, but can save the system
1680 static int warningdone = 0;
1682 if (pmap_pagedaemon_waken == 0)
1685 if (warningdone < 5) {
1686 printf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1690 for(i = 0; i < vm_page_array_size; i++) {
1691 m = &vm_page_array[i];
1692 if (m->wire_count || m->hold_count || m->busy ||
1693 (m->flags & (PG_BUSY | PG_UNMANAGED)))
1697 pmap_pagedaemon_waken = 0;
1702 * If it is the first entry on the list, it is actually
1703 * in the header and we must copy the following entry up
1704 * to the header. Otherwise we must search the list for
1705 * the entry. In either case we free the now unused entry.
1709 pmap_remove_entry(pmap_t pmap, vm_page_t m, vm_offset_t va)
1716 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1717 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1718 if (pmap == pv->pv_pmap && va == pv->pv_va)
1722 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1723 if (va == pv->pv_va)
1730 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem);
1731 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1732 m->md.pv_list_count--;
1733 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
1734 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1736 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1745 * Create a pv entry for page at pa for
1749 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
1756 pv = get_pv_entry();
1761 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1762 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1763 m->md.pv_list_count++;
1769 * pmap_remove_pte: do the things to unmap a page in a process
1772 pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t va)
1777 oldpte = atomic_readandclear_int(ptq);
1779 pmap->pm_stats.wired_count -= 1;
1781 * Machines that don't support invlpg, also don't support
1786 pmap->pm_stats.resident_count -= 1;
1787 if (oldpte & PG_MANAGED) {
1788 m = PHYS_TO_VM_PAGE(oldpte);
1789 if (oldpte & PG_M) {
1790 #if defined(PMAP_DIAGNOSTIC)
1791 if (pmap_nw_modified((pt_entry_t) oldpte)) {
1793 "pmap_remove: modified page not writable: va: 0x%x, pte: 0x%x\n",
1797 if (pmap_track_modified(va))
1801 vm_page_flag_set(m, PG_REFERENCED);
1802 return pmap_remove_entry(pmap, m, va);
1804 return pmap_unuse_pt(pmap, va, NULL);
1811 * Remove a single page from a process address space
1814 pmap_remove_page(pmap_t pmap, vm_offset_t va)
1816 register pt_entry_t *ptq;
1819 * if there is no pte for this address, just skip it!!!
1821 if (*pmap_pde(pmap, va) == 0) {
1826 * get a local va for mappings for this pmap.
1828 ptq = get_ptbase(pmap) + i386_btop(va);
1830 (void) pmap_remove_pte(pmap, ptq, va);
1831 pmap_invalidate_page(pmap, va);
1837 * Remove the given range of addresses from the specified map.
1839 * It is assumed that the start and end are properly
1840 * rounded to the page size.
1843 pmap_remove(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
1845 register pt_entry_t *ptbase;
1848 vm_offset_t sindex, eindex;
1854 if (pmap->pm_stats.resident_count == 0)
1858 * special handling of removing one page. a very
1859 * common operation and easy to short circuit some
1862 if ((sva + PAGE_SIZE == eva) &&
1863 ((pmap->pm_pdir[(sva >> PDRSHIFT)] & PG_PS) == 0)) {
1864 pmap_remove_page(pmap, sva);
1871 * Get a local virtual address for the mappings that are being
1874 ptbase = get_ptbase(pmap);
1876 sindex = i386_btop(sva);
1877 eindex = i386_btop(eva);
1879 for (; sindex < eindex; sindex = pdnxt) {
1883 * Calculate index for next page table.
1885 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1886 if (pmap->pm_stats.resident_count == 0)
1889 pdirindex = sindex / NPDEPG;
1890 ptpaddr = pmap->pm_pdir[pdirindex];
1891 if ((ptpaddr & PG_PS) != 0) {
1892 pmap->pm_pdir[pdirindex] = 0;
1893 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1899 * Weed out invalid mappings. Note: we assume that the page
1900 * directory table is always allocated, and in kernel virtual.
1906 * Limit our scan to either the end of the va represented
1907 * by the current page table page, or to the end of the
1908 * range being removed.
1910 if (pdnxt > eindex) {
1914 for (; sindex != pdnxt; sindex++) {
1916 if (ptbase[sindex] == 0) {
1919 va = i386_ptob(sindex);
1922 if (pmap_remove_pte(pmap,
1923 ptbase + sindex, va))
1929 pmap_invalidate_all(pmap);
1933 * Routine: pmap_remove_all
1935 * Removes this physical page from
1936 * all physical maps in which it resides.
1937 * Reflects back modify bits to the pager.
1940 * Original versions of this routine were very
1941 * inefficient because they iteratively called
1942 * pmap_remove (slow...)
1946 pmap_remove_all(vm_page_t m)
1948 register pv_entry_t pv;
1949 pt_entry_t *pte, tpte;
1952 #if defined(PMAP_DIAGNOSTIC)
1954 * XXX this makes pmap_page_protect(NONE) illegal for non-managed
1957 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
1958 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%x", VM_PAGE_TO_PHYS(m));
1963 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
1964 pv->pv_pmap->pm_stats.resident_count--;
1966 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
1968 tpte = atomic_readandclear_int(pte);
1970 pv->pv_pmap->pm_stats.wired_count--;
1973 vm_page_flag_set(m, PG_REFERENCED);
1976 * Update the vm_page_t clean and reference bits.
1979 #if defined(PMAP_DIAGNOSTIC)
1980 if (pmap_nw_modified((pt_entry_t) tpte)) {
1982 "pmap_remove_all: modified page not writable: va: 0x%x, pte: 0x%x\n",
1986 if (pmap_track_modified(pv->pv_va))
1989 pmap_invalidate_page(pv->pv_pmap, pv->pv_va);
1991 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
1992 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1993 m->md.pv_list_count--;
1994 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
1998 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2004 * Set the physical protection on the
2005 * specified range of this map as requested.
2008 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
2010 register pt_entry_t *ptbase;
2013 vm_pindex_t sindex, eindex;
2019 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
2020 pmap_remove(pmap, sva, eva);
2024 if (prot & VM_PROT_WRITE)
2029 ptbase = get_ptbase(pmap);
2031 sindex = i386_btop(sva);
2032 eindex = i386_btop(eva);
2034 for (; sindex < eindex; sindex = pdnxt) {
2038 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
2040 pdirindex = sindex / NPDEPG;
2041 ptpaddr = pmap->pm_pdir[pdirindex];
2042 if ((ptpaddr & PG_PS) != 0) {
2043 pmap->pm_pdir[pdirindex] &= ~(PG_M|PG_RW);
2044 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2050 * Weed out invalid mappings. Note: we assume that the page
2051 * directory table is always allocated, and in kernel virtual.
2056 if (pdnxt > eindex) {
2060 for (; sindex != pdnxt; sindex++) {
2065 pbits = ptbase[sindex];
2067 if (pbits & PG_MANAGED) {
2070 m = PHYS_TO_VM_PAGE(pbits);
2071 vm_page_flag_set(m, PG_REFERENCED);
2075 if (pmap_track_modified(i386_ptob(sindex))) {
2077 m = PHYS_TO_VM_PAGE(pbits);
2086 if (pbits != ptbase[sindex]) {
2087 ptbase[sindex] = pbits;
2093 pmap_invalidate_all(pmap);
2097 * Insert the given physical page (p) at
2098 * the specified virtual address (v) in the
2099 * target physical map with the protection requested.
2101 * If specified, the page will be wired down, meaning
2102 * that the related pte can not be reclaimed.
2104 * NB: This is the only routine which MAY NOT lazy-evaluate
2105 * or lose information. That is, this routine must actually
2106 * insert this page into the given map NOW.
2109 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
2113 register pt_entry_t *pte;
2115 pt_entry_t origpte, newpte;
2122 #ifdef PMAP_DIAGNOSTIC
2123 if (va > VM_MAX_KERNEL_ADDRESS)
2124 panic("pmap_enter: toobig");
2125 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
2126 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%x)", va);
2131 * In the case that a page table page is not
2132 * resident, we are creating it here.
2134 if (va < VM_MAXUSER_ADDRESS) {
2135 mpte = pmap_allocpte(pmap, va);
2137 #if 0 && defined(PMAP_DIAGNOSTIC)
2139 pd_entry_t *pdeaddr = pmap_pde(pmap, va);
2141 if ((origpte & PG_V) == 0) {
2142 panic("pmap_enter: invalid kernel page table page, pdir=%p, pde=%p, va=%p\n",
2143 pmap->pm_pdir[PTDPTDI], origpte, va);
2148 pte = pmap_pte(pmap, va);
2151 * Page Directory table entry not valid, we need a new PT page
2154 panic("pmap_enter: invalid page directory, pdir=%p, va=0x%x\n",
2155 (void *)pmap->pm_pdir[PTDPTDI], va);
2158 pa = VM_PAGE_TO_PHYS(m) & PG_FRAME;
2159 origpte = *(vm_offset_t *)pte;
2160 opa = origpte & PG_FRAME;
2162 if (origpte & PG_PS)
2163 panic("pmap_enter: attempted pmap_enter on 4MB page");
2166 * Mapping has not changed, must be protection or wiring change.
2168 if (origpte && (opa == pa)) {
2170 * Wiring change, just update stats. We don't worry about
2171 * wiring PT pages as they remain resident as long as there
2172 * are valid mappings in them. Hence, if a user page is wired,
2173 * the PT page will be also.
2175 if (wired && ((origpte & PG_W) == 0))
2176 pmap->pm_stats.wired_count++;
2177 else if (!wired && (origpte & PG_W))
2178 pmap->pm_stats.wired_count--;
2180 #if defined(PMAP_DIAGNOSTIC)
2181 if (pmap_nw_modified((pt_entry_t) origpte)) {
2183 "pmap_enter: modified page not writable: va: 0x%x, pte: 0x%x\n",
2189 * Remove extra pte reference
2194 if ((prot & VM_PROT_WRITE) && (origpte & PG_V)) {
2195 if ((origpte & PG_RW) == 0) {
2198 cpu_invlpg((void *)va);
2199 if (pmap->pm_active & PCPU_GET(other_cpus))
2209 * We might be turning off write access to the page,
2210 * so we go ahead and sense modify status.
2212 if (origpte & PG_MANAGED) {
2213 if ((origpte & PG_M) && pmap_track_modified(va)) {
2215 om = PHYS_TO_VM_PAGE(opa);
2223 * Mapping has changed, invalidate old range and fall through to
2224 * handle validating new mapping.
2228 err = pmap_remove_pte(pmap, pte, va);
2230 panic("pmap_enter: pte vanished, va: 0x%x", va);
2234 * Enter on the PV list if part of our managed memory. Note that we
2235 * raise IPL while manipulating pv_table since pmap_enter can be
2236 * called at interrupt time.
2238 if (pmap_initialized &&
2239 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2240 pmap_insert_entry(pmap, va, mpte, m);
2245 * Increment counters
2247 pmap->pm_stats.resident_count++;
2249 pmap->pm_stats.wired_count++;
2253 * Now validate mapping with desired protection/wiring.
2255 newpte = (vm_offset_t) (pa | pte_prot(pmap, prot) | PG_V);
2259 if (va < VM_MAXUSER_ADDRESS)
2261 if (pmap == kernel_pmap)
2265 * if the mapping or permission bits are different, we need
2266 * to update the pte.
2268 if ((origpte & ~(PG_M|PG_A)) != newpte) {
2269 *pte = newpte | PG_A;
2272 cpu_invlpg((void *)va);
2273 if (pmap->pm_active & PCPU_GET(other_cpus))
2283 * this code makes some *MAJOR* assumptions:
2284 * 1. Current pmap & pmap exists.
2287 * 4. No page table pages.
2288 * 5. Tlbflush is deferred to calling procedure.
2289 * 6. Page IS managed.
2290 * but is *MUCH* faster than pmap_enter...
2294 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_page_t mpte)
2300 * In the case that a page table page is not
2301 * resident, we are creating it here.
2303 if (va < VM_MAXUSER_ADDRESS) {
2308 * Calculate pagetable page index
2310 ptepindex = va >> PDRSHIFT;
2311 if (mpte && (mpte->pindex == ptepindex)) {
2316 * Get the page directory entry
2318 ptepa = pmap->pm_pdir[ptepindex];
2321 * If the page table page is mapped, we just increment
2322 * the hold count, and activate it.
2326 panic("pmap_enter_quick: unexpected mapping into 4MB page");
2327 if (pmap->pm_ptphint &&
2328 (pmap->pm_ptphint->pindex == ptepindex)) {
2329 mpte = pmap->pm_ptphint;
2331 mpte = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
2332 pmap->pm_ptphint = mpte;
2338 mpte = _pmap_allocpte(pmap, ptepindex);
2346 * This call to vtopte makes the assumption that we are
2347 * entering the page into the current pmap. In order to support
2348 * quick entry into any pmap, one would likely use pmap_pte_quick.
2349 * But that isn't as quick as vtopte.
2354 pmap_unwire_pte_hold(pmap, mpte);
2359 * Enter on the PV list if part of our managed memory. Note that we
2360 * raise IPL while manipulating pv_table since pmap_enter can be
2361 * called at interrupt time.
2363 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0)
2364 pmap_insert_entry(pmap, va, mpte, m);
2367 * Increment counters
2369 pmap->pm_stats.resident_count++;
2371 pa = VM_PAGE_TO_PHYS(m);
2374 * Now validate mapping with RO protection
2376 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2377 *pte = pa | PG_V | PG_U;
2379 *pte = pa | PG_V | PG_U | PG_MANAGED;
2385 * Make a temporary mapping for a physical address. This is only intended
2386 * to be used for panic dumps.
2389 pmap_kenter_temporary(vm_offset_t pa, int i)
2391 pmap_kenter((vm_offset_t)crashdumpmap + (i * PAGE_SIZE), pa);
2392 return ((void *)crashdumpmap);
2395 #define MAX_INIT_PT (96)
2397 * pmap_object_init_pt preloads the ptes for a given object
2398 * into the specified pmap. This eliminates the blast of soft
2399 * faults on process startup and immediately after an mmap.
2402 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr,
2403 vm_object_t object, vm_pindex_t pindex,
2404 vm_size_t size, int limit)
2411 if (pmap == NULL || object == NULL)
2415 * This code maps large physical mmap regions into the
2416 * processor address space. Note that some shortcuts
2417 * are taken, but the code works.
2419 if (pseflag && (object->type == OBJT_DEVICE) &&
2420 ((addr & (NBPDR - 1)) == 0) && ((size & (NBPDR - 1)) == 0)) {
2423 unsigned int ptepindex;
2427 if (pmap->pm_pdir[ptepindex = (addr >> PDRSHIFT)])
2431 p = vm_page_lookup(object, pindex);
2432 if (p && vm_page_sleep_busy(p, FALSE, "init4p"))
2436 p = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL);
2441 if (vm_pager_get_pages(object, m, 1, 0) != VM_PAGER_OK) {
2446 p = vm_page_lookup(object, pindex);
2450 ptepa = VM_PAGE_TO_PHYS(p);
2451 if (ptepa & (NBPDR - 1)) {
2455 p->valid = VM_PAGE_BITS_ALL;
2457 pmap->pm_stats.resident_count += size >> PAGE_SHIFT;
2458 npdes = size >> PDRSHIFT;
2459 for(i = 0; i < npdes; i++) {
2460 pmap->pm_pdir[ptepindex] =
2461 ptepa | PG_U | PG_RW | PG_V | PG_PS;
2465 vm_page_flag_set(p, PG_MAPPED);
2470 psize = i386_btop(size);
2472 if ((object->type != OBJT_VNODE) ||
2473 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2474 (object->resident_page_count > MAX_INIT_PT))) {
2478 if (psize + pindex > object->size) {
2479 if (object->size < pindex)
2481 psize = object->size - pindex;
2486 * if we are processing a major portion of the object, then scan the
2489 if (psize > (object->resident_page_count >> 2)) {
2492 for (p = TAILQ_FIRST(&object->memq);
2493 ((objpgs > 0) && (p != NULL));
2494 p = TAILQ_NEXT(p, listq)) {
2497 if (tmpidx < pindex) {
2501 if (tmpidx >= psize) {
2505 * don't allow an madvise to blow away our really
2506 * free pages allocating pv entries.
2508 if ((limit & MAP_PREFAULT_MADVISE) &&
2509 cnt.v_free_count < cnt.v_free_reserved) {
2512 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2514 (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2515 if ((p->queue - p->pc) == PQ_CACHE)
2516 vm_page_deactivate(p);
2518 mpte = pmap_enter_quick(pmap,
2519 addr + i386_ptob(tmpidx), p, mpte);
2520 vm_page_flag_set(p, PG_MAPPED);
2527 * else lookup the pages one-by-one.
2529 for (tmpidx = 0; tmpidx < psize; tmpidx += 1) {
2531 * don't allow an madvise to blow away our really
2532 * free pages allocating pv entries.
2534 if ((limit & MAP_PREFAULT_MADVISE) &&
2535 cnt.v_free_count < cnt.v_free_reserved) {
2538 p = vm_page_lookup(object, tmpidx + pindex);
2540 ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2542 (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2543 if ((p->queue - p->pc) == PQ_CACHE)
2544 vm_page_deactivate(p);
2546 mpte = pmap_enter_quick(pmap,
2547 addr + i386_ptob(tmpidx), p, mpte);
2548 vm_page_flag_set(p, PG_MAPPED);
2557 * pmap_prefault provides a quick way of clustering
2558 * pagefaults into a processes address space. It is a "cousin"
2559 * of pmap_object_init_pt, except it runs at page fault time instead
2564 #define PAGEORDER_SIZE (PFBAK+PFFOR)
2566 static int pmap_prefault_pageorder[] = {
2567 -PAGE_SIZE, PAGE_SIZE,
2568 -2 * PAGE_SIZE, 2 * PAGE_SIZE,
2569 -3 * PAGE_SIZE, 3 * PAGE_SIZE
2570 -4 * PAGE_SIZE, 4 * PAGE_SIZE
2574 pmap_prefault(pmap, addra, entry)
2577 vm_map_entry_t entry;
2586 if (!curthread || (pmap != vmspace_pmap(curthread->td_proc->p_vmspace)))
2589 object = entry->object.vm_object;
2591 starta = addra - PFBAK * PAGE_SIZE;
2592 if (starta < entry->start) {
2593 starta = entry->start;
2594 } else if (starta > addra) {
2599 for (i = 0; i < PAGEORDER_SIZE; i++) {
2600 vm_object_t lobject;
2603 addr = addra + pmap_prefault_pageorder[i];
2604 if (addr > addra + (PFFOR * PAGE_SIZE))
2607 if (addr < starta || addr >= entry->end)
2610 if ((*pmap_pde(pmap, addr)) == NULL)
2617 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2619 for (m = vm_page_lookup(lobject, pindex);
2620 (!m && (lobject->type == OBJT_DEFAULT) && (lobject->backing_object));
2621 lobject = lobject->backing_object) {
2622 if (lobject->backing_object_offset & PAGE_MASK)
2624 pindex += (lobject->backing_object_offset >> PAGE_SHIFT);
2625 m = vm_page_lookup(lobject->backing_object, pindex);
2629 * give-up when a page is not in memory
2634 if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2636 (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2638 if ((m->queue - m->pc) == PQ_CACHE) {
2639 vm_page_deactivate(m);
2642 mpte = pmap_enter_quick(pmap, addr, m, mpte);
2643 vm_page_flag_set(m, PG_MAPPED);
2650 * Routine: pmap_change_wiring
2651 * Function: Change the wiring attribute for a map/virtual-address
2653 * In/out conditions:
2654 * The mapping must already exist in the pmap.
2657 pmap_change_wiring(pmap, va, wired)
2658 register pmap_t pmap;
2662 register pt_entry_t *pte;
2667 pte = pmap_pte(pmap, va);
2669 if (wired && !pmap_pte_w(pte))
2670 pmap->pm_stats.wired_count++;
2671 else if (!wired && pmap_pte_w(pte))
2672 pmap->pm_stats.wired_count--;
2675 * Wiring is not a hardware characteristic so there is no need to
2678 pmap_pte_set_w(pte, wired);
2684 * Copy the range specified by src_addr/len
2685 * from the source map to the range dst_addr/len
2686 * in the destination map.
2688 * This routine is only advisory and need not do anything.
2692 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, vm_size_t len,
2693 vm_offset_t src_addr)
2696 vm_offset_t end_addr = src_addr + len;
2698 pd_entry_t src_frame, dst_frame;
2700 pd_entry_t saved_pde;
2702 if (dst_addr != src_addr)
2705 src_frame = src_pmap->pm_pdir[PTDPTDI] & PG_FRAME;
2706 if (src_frame != (PTDpde & PG_FRAME))
2709 dst_frame = dst_pmap->pm_pdir[PTDPTDI] & PG_FRAME;
2710 if (dst_frame != (APTDpde & PG_FRAME)) {
2711 APTDpde = dst_frame | PG_RW | PG_V;
2713 /* The page directory is not shared between CPUs */
2719 saved_pde = APTDpde & (PG_FRAME | PG_RW | PG_V);
2720 for(addr = src_addr; addr < end_addr; addr = pdnxt) {
2721 pt_entry_t *src_pte, *dst_pte;
2722 vm_page_t dstmpte, srcmpte;
2723 pd_entry_t srcptepaddr;
2726 if (addr >= UPT_MIN_ADDRESS)
2727 panic("pmap_copy: invalid to pmap_copy page tables\n");
2730 * Don't let optional prefaulting of pages make us go
2731 * way below the low water mark of free pages or way
2732 * above high water mark of used pv entries.
2734 if (cnt.v_free_count < cnt.v_free_reserved ||
2735 pv_entry_count > pv_entry_high_water)
2738 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
2739 ptepindex = addr >> PDRSHIFT;
2741 srcptepaddr = src_pmap->pm_pdir[ptepindex];
2742 if (srcptepaddr == 0)
2745 if (srcptepaddr & PG_PS) {
2746 if (dst_pmap->pm_pdir[ptepindex] == 0) {
2747 dst_pmap->pm_pdir[ptepindex] = srcptepaddr;
2748 dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
2753 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
2754 if ((srcmpte == NULL) ||
2755 (srcmpte->hold_count == 0) || (srcmpte->flags & PG_BUSY))
2758 if (pdnxt > end_addr)
2761 src_pte = vtopte(addr);
2762 dst_pte = avtopte(addr);
2763 while (addr < pdnxt) {
2767 * we only virtual copy managed pages
2769 if ((ptetemp & PG_MANAGED) != 0) {
2771 * We have to check after allocpte for the
2772 * pte still being around... allocpte can
2775 dstmpte = pmap_allocpte(dst_pmap, addr);
2776 if ((APTDpde & PG_FRAME) !=
2777 (saved_pde & PG_FRAME)) {
2778 APTDpde = saved_pde;
2779 printf ("IT HAPPENNED!");
2786 if ((*dst_pte == 0) && (ptetemp = *src_pte)) {
2788 * Clear the modified and
2789 * accessed (referenced) bits
2792 m = PHYS_TO_VM_PAGE(ptetemp);
2793 *dst_pte = ptetemp & ~(PG_M | PG_A);
2794 dst_pmap->pm_stats.resident_count++;
2795 pmap_insert_entry(dst_pmap, addr,
2798 pmap_unwire_pte_hold(dst_pmap, dstmpte);
2800 if (dstmpte->hold_count >= srcmpte->hold_count)
2811 * pmap_zero_page zeros the specified hardware page by mapping
2812 * the page into KVM and using bzero to clear its contents.
2815 pmap_zero_page(vm_page_t m)
2817 vm_offset_t phys = VM_PAGE_TO_PHYS(m);
2820 panic("pmap_zero_page: CMAP2 busy");
2822 *CMAP2 = PG_V | PG_RW | phys | PG_A | PG_M;
2823 invltlb_1pg((vm_offset_t)CADDR2);
2825 #if defined(I686_CPU)
2826 if (cpu_class == CPUCLASS_686)
2827 i686_pagezero(CADDR2);
2830 bzero(CADDR2, PAGE_SIZE);
2835 * pmap_zero_page_area zeros the specified hardware page by mapping
2836 * the page into KVM and using bzero to clear its contents.
2838 * off and size may not cover an area beyond a single hardware page.
2841 pmap_zero_page_area(vm_page_t m, int off, int size)
2843 vm_offset_t phys = VM_PAGE_TO_PHYS(m);
2846 panic("pmap_zero_page: CMAP2 busy");
2848 *CMAP2 = PG_V | PG_RW | phys | PG_A | PG_M;
2849 invltlb_1pg((vm_offset_t)CADDR2);
2851 #if defined(I686_CPU)
2852 if (cpu_class == CPUCLASS_686 && off == 0 && size == PAGE_SIZE)
2853 i686_pagezero(CADDR2);
2856 bzero((char *)CADDR2 + off, size);
2861 * pmap_copy_page copies the specified (machine independent)
2862 * page by mapping the page into virtual memory and using
2863 * bcopy to copy the page, one machine dependent page at a
2867 pmap_copy_page(vm_page_t src, vm_page_t dst)
2871 panic("pmap_copy_page: CMAP1 busy");
2873 panic("pmap_copy_page: CMAP2 busy");
2875 *CMAP1 = PG_V | VM_PAGE_TO_PHYS(src) | PG_A;
2876 *CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(dst) | PG_A | PG_M;
2880 invlpg((u_int)CADDR1);
2881 invlpg((u_int)CADDR2);
2884 bcopy(CADDR1, CADDR2, PAGE_SIZE);
2892 * Routine: pmap_pageable
2894 * Make the specified pages (by pmap, offset)
2895 * pageable (or not) as requested.
2897 * A page which is not pageable may not take
2898 * a fault; therefore, its page table entry
2899 * must remain valid for the duration.
2901 * This routine is merely advisory; pmap_enter
2902 * will specify that these pages are to be wired
2903 * down (or not) as appropriate.
2906 pmap_pageable(pmap, sva, eva, pageable)
2908 vm_offset_t sva, eva;
2914 * Returns true if the pmap's pv is one of the first
2915 * 16 pvs linked to from this page. This count may
2916 * be changed upwards or downwards in the future; it
2917 * is only necessary that true be returned for a small
2918 * subset of pmaps for proper page aging.
2921 pmap_page_exists_quick(pmap, m)
2929 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2934 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2935 if (pv->pv_pmap == pmap) {
2947 #define PMAP_REMOVE_PAGES_CURPROC_ONLY
2949 * Remove all pages from specified address space
2950 * this aids process exit speeds. Also, this code
2951 * is special cased for current process only, but
2952 * can have the more generic (and slightly slower)
2953 * mode enabled. This is much faster than pmap_remove
2954 * in the case of running down an entire address space.
2957 pmap_remove_pages(pmap, sva, eva)
2959 vm_offset_t sva, eva;
2961 pt_entry_t *pte, tpte;
2966 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2967 if (!curthread || (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))) {
2968 printf("warning: pmap_remove_pages called with non-current pmap\n");
2974 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
2976 if (pv->pv_va >= eva || pv->pv_va < sva) {
2977 npv = TAILQ_NEXT(pv, pv_plist);
2981 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2982 pte = vtopte(pv->pv_va);
2984 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2989 printf("TPTE at %p IS ZERO @ VA %08x\n",
2995 * We cannot remove wired pages from a process' mapping at this time
2998 npv = TAILQ_NEXT(pv, pv_plist);
3002 m = PHYS_TO_VM_PAGE(tpte);
3003 KASSERT(m->phys_addr == (tpte & PG_FRAME),
3004 ("vm_page_t %p phys_addr mismatch %08x %08x",
3005 m, m->phys_addr, tpte));
3007 KASSERT(m < &vm_page_array[vm_page_array_size],
3008 ("pmap_remove_pages: bad tpte %x", tpte));
3010 pv->pv_pmap->pm_stats.resident_count--;
3015 * Update the vm_page_t clean and reference bits.
3021 npv = TAILQ_NEXT(pv, pv_plist);
3022 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
3024 m->md.pv_list_count--;
3025 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3026 if (TAILQ_FIRST(&m->md.pv_list) == NULL) {
3027 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
3030 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
3034 pmap_invalidate_all(pmap);
3038 * pmap_testbit tests bits in pte's
3039 * note that the testbit/changebit routines are inline,
3040 * and a lot of things compile-time evaluate.
3043 pmap_testbit(m, bit)
3051 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3054 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
3059 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3061 * if the bit being tested is the modified bit, then
3062 * mark clean_map and ptes as never
3065 if (bit & (PG_A|PG_M)) {
3066 if (!pmap_track_modified(pv->pv_va))
3070 #if defined(PMAP_DIAGNOSTIC)
3072 printf("Null pmap (tb) at va: 0x%x\n", pv->pv_va);
3076 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3087 * this routine is used to modify bits in ptes
3089 static __inline void
3090 pmap_changebit(vm_page_t m, int bit, boolean_t setem)
3092 register pv_entry_t pv;
3093 register pt_entry_t *pte;
3096 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3102 * Loop over all current mappings setting/clearing as appropos If
3103 * setting RO do we need to clear the VAC?
3105 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3107 * don't write protect pager mappings
3109 if (!setem && (bit == PG_RW)) {
3110 if (!pmap_track_modified(pv->pv_va))
3114 #if defined(PMAP_DIAGNOSTIC)
3116 printf("Null pmap (cb) at va: 0x%x\n", pv->pv_va);
3121 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3125 pmap_invalidate_page(pv->pv_pmap, pv->pv_va);
3127 pt_entry_t pbits = *pte;
3133 *pte = pbits & ~(PG_M|PG_RW);
3135 *pte = pbits & ~bit;
3137 pmap_invalidate_page(pv->pv_pmap, pv->pv_va);
3145 * pmap_page_protect:
3147 * Lower the permission for all mappings to a given page.
3150 pmap_page_protect(vm_page_t m, vm_prot_t prot)
3152 if ((prot & VM_PROT_WRITE) == 0) {
3153 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
3154 pmap_changebit(m, PG_RW, FALSE);
3162 pmap_phys_address(ppn)
3165 return (i386_ptob(ppn));
3169 * pmap_ts_referenced:
3171 * Return a count of reference bits for a page, clearing those bits.
3172 * It is not necessary for every reference bit to be cleared, but it
3173 * is necessary that 0 only be returned when there are truly no
3174 * reference bits set.
3176 * XXX: The exact number of bits to check and clear is a matter that
3177 * should be tested and standardized at some point in the future for
3178 * optimal aging of shared pages.
3181 pmap_ts_referenced(vm_page_t m)
3183 register pv_entry_t pv, pvf, pvn;
3188 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3193 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3198 pvn = TAILQ_NEXT(pv, pv_list);
3200 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3202 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3204 if (!pmap_track_modified(pv->pv_va))
3207 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3209 if (pte && (*pte & PG_A)) {
3212 pmap_invalidate_page(pv->pv_pmap, pv->pv_va);
3219 } while ((pv = pvn) != NULL && pv != pvf);
3229 * Return whether or not the specified physical page was modified
3230 * in any physical maps.
3233 pmap_is_modified(vm_page_t m)
3235 return pmap_testbit(m, PG_M);
3239 * Clear the modify bits on the specified physical page.
3242 pmap_clear_modify(vm_page_t m)
3244 pmap_changebit(m, PG_M, FALSE);
3248 * pmap_clear_reference:
3250 * Clear the reference bit on the specified physical page.
3253 pmap_clear_reference(vm_page_t m)
3255 pmap_changebit(m, PG_A, FALSE);
3259 * Miscellaneous support routines follow
3263 i386_protection_init()
3265 register int *kp, prot;
3267 kp = protection_codes;
3268 for (prot = 0; prot < 8; prot++) {
3270 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE:
3272 * Read access is also 0. There isn't any execute bit,
3273 * so just make it readable.
3275 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE:
3276 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE:
3277 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE:
3280 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE:
3281 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE:
3282 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE:
3283 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
3291 * Map a set of physical memory pages into the kernel virtual
3292 * address space. Return a pointer to where it is mapped. This
3293 * routine is intended to be used for mapping device memory,
3297 pmap_mapdev(pa, size)
3301 vm_offset_t va, tmpva, offset;
3304 offset = pa & PAGE_MASK;
3305 size = roundup(offset + size, PAGE_SIZE);
3309 va = kmem_alloc_pageable(kernel_map, size);
3311 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3314 for (tmpva = va; size > 0;) {
3315 pte = vtopte(tmpva);
3316 *pte = pa | PG_RW | PG_V | pgeflag;
3323 return ((void *)(va + offset));
3327 pmap_unmapdev(va, size)
3331 vm_offset_t base, offset;
3333 base = va & PG_FRAME;
3334 offset = va & PAGE_MASK;
3335 size = roundup(offset + size, PAGE_SIZE);
3336 kmem_free(kernel_map, base, size);
3340 * perform the pmap work for mincore
3343 pmap_mincore(pmap, addr)
3347 pt_entry_t *ptep, pte;
3351 ptep = pmap_pte(pmap, addr);
3356 if ((pte = *ptep) != 0) {
3359 val = MINCORE_INCORE;
3360 if ((pte & PG_MANAGED) == 0)
3363 pa = pte & PG_FRAME;
3365 m = PHYS_TO_VM_PAGE(pa);
3371 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3373 * Modified by someone
3375 else if (m->dirty || pmap_is_modified(m))
3376 val |= MINCORE_MODIFIED_OTHER;
3381 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3384 * Referenced by someone
3386 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3387 val |= MINCORE_REFERENCED_OTHER;
3388 vm_page_flag_set(m, PG_REFERENCED);
3395 pmap_activate(struct thread *td)
3397 struct proc *p = td->td_proc;
3401 pmap = vmspace_pmap(td->td_proc->p_vmspace);
3403 pmap->pm_active |= PCPU_GET(cpumask);
3405 pmap->pm_active |= 1;
3407 #if defined(SWTCH_OPTIM_STATS)
3410 cr3 = vtophys(pmap->pm_pdir);
3411 /* XXXKSE this is wrong.
3412 * pmap_activate is for the current thread on the current cpu
3414 if (p->p_flag & P_KSES) {
3415 /* Make sure all other cr3 entries are updated. */
3416 /* what if they are running? XXXKSE (maybe abort them) */
3417 FOREACH_THREAD_IN_PROC(p, td) {
3418 td->td_pcb->pcb_cr3 = cr3;
3421 td->td_pcb->pcb_cr3 = cr3;
3427 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3430 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3434 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3439 #if defined(PMAP_DEBUG)
3440 pmap_pid_dump(int pid)
3447 sx_slock(&allproc_lock);
3448 LIST_FOREACH(p, &allproc, p_list) {
3449 if (p->p_pid != pid)
3455 pmap = vmspace_pmap(p->p_vmspace);
3456 for (i = 0; i < NPDEPG; i++) {
3459 vm_offset_t base = i << PDRSHIFT;
3461 pde = &pmap->pm_pdir[i];
3462 if (pde && pmap_pde_v(pde)) {
3463 for (j = 0; j < NPTEPG; j++) {
3464 vm_offset_t va = base + (j << PAGE_SHIFT);
3465 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) {
3470 sx_sunlock(&allproc_lock);
3473 pte = pmap_pte_quick(pmap, va);
3474 if (pte && pmap_pte_v(pte)) {
3478 m = PHYS_TO_VM_PAGE(pa);
3479 printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x",
3480 va, pa, m->hold_count, m->wire_count, m->flags);
3495 sx_sunlock(&allproc_lock);
3502 static void pads(pmap_t pm);
3503 void pmap_pvdump(vm_offset_t pa);
3505 /* print address space of pmap*/
3514 if (pm == kernel_pmap)
3516 for (i = 0; i < NPDEPG; i++)
3518 for (j = 0; j < NPTEPG; j++) {
3519 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
3520 if (pm == kernel_pmap && va < KERNBASE)
3522 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS)
3524 ptep = pmap_pte_quick(pm, va);
3525 if (pmap_pte_v(ptep))
3526 printf("%x:%x ", va, *ptep);
3538 printf("pa %x", pa);
3539 m = PHYS_TO_VM_PAGE(pa);
3540 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3541 printf(" -> pmap %p, va %x", (void *)pv->pv_pmap, pv->pv_va);