2 * Copyright (c) 1991 Regents of the University of California.
4 * Copyright (c) 1994 John S. Dyson
6 * Copyright (c) 1994 David Greenman
9 * This code is derived from software contributed to Berkeley by
10 * the Systems Programming Group of the University of Utah Computer
11 * Science Department and William Jolitz of UUNET Technologies Inc.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91
42 * $FreeBSD: src/sys/i386/i386/pmap.c,v 1.250.2.18 2002/03/06 22:48:53 silby Exp $
43 * $DragonFly: src/sys/i386/i386/Attic/pmap.c,v 1.12 2003/06/28 02:09:47 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"
75 #include "opt_user_ldt.h"
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/kernel.h>
81 #include <sys/msgbuf.h>
82 #include <sys/vmmeter.h>
86 #include <vm/vm_param.h>
87 #include <sys/sysctl.h>
89 #include <vm/vm_kern.h>
90 #include <vm/vm_page.h>
91 #include <vm/vm_map.h>
92 #include <vm/vm_object.h>
93 #include <vm/vm_extern.h>
94 #include <vm/vm_pageout.h>
95 #include <vm/vm_pager.h>
96 #include <vm/vm_zone.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 #include <machine/globaldata.h>
109 #endif /* SMP || APIC_IO */
111 #define PMAP_KEEP_PDIRS
112 #ifndef PMAP_SHPGPERPROC
113 #define PMAP_SHPGPERPROC 200
116 #if defined(DIAGNOSTIC)
117 #define PMAP_DIAGNOSTIC
122 #if !defined(PMAP_DIAGNOSTIC)
123 #define PMAP_INLINE __inline
129 * Get PDEs and PTEs for user/kernel address space
131 #define pmap_pde(m, v) (&((m)->pm_pdir[(vm_offset_t)(v) >> PDRSHIFT]))
132 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
134 #define pmap_pde_v(pte) ((*(int *)pte & PG_V) != 0)
135 #define pmap_pte_w(pte) ((*(int *)pte & PG_W) != 0)
136 #define pmap_pte_m(pte) ((*(int *)pte & PG_M) != 0)
137 #define pmap_pte_u(pte) ((*(int *)pte & PG_A) != 0)
138 #define pmap_pte_v(pte) ((*(int *)pte & PG_V) != 0)
140 #define pmap_pte_set_w(pte, v) ((v)?(*(int *)pte |= PG_W):(*(int *)pte &= ~PG_W))
141 #define pmap_pte_set_prot(pte, v) ((*(int *)pte &= ~PG_PROT), (*(int *)pte |= (v)))
144 * Given a map and a machine independent protection code,
145 * convert to a vax protection code.
147 #define pte_prot(m, p) (protection_codes[p])
148 static int protection_codes[8];
150 static struct pmap kernel_pmap_store;
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 vm_zone_t pvzone;
170 static struct vm_zone pvzone_store;
171 static struct vm_object pvzone_obj;
172 static int pv_entry_count=0, pv_entry_max=0, pv_entry_high_water=0;
173 static int pmap_pagedaemon_waken = 0;
174 static struct pv_entry *pvinit;
177 * All those kernel PT submaps that BSD is so fond of
179 pt_entry_t *CMAP1 = 0;
180 caddr_t CADDR1 = 0, ptvmmap = 0;
181 static pt_entry_t *msgbufmap;
182 struct msgbuf *msgbufp=0;
187 static pt_entry_t *pt_crashdumpmap;
188 static caddr_t crashdumpmap;
190 extern pt_entry_t *SMPpt;
192 static PMAP_INLINE void free_pv_entry __P((pv_entry_t pv));
193 static unsigned * get_ptbase __P((pmap_t pmap));
194 static pv_entry_t get_pv_entry __P((void));
195 static void i386_protection_init __P((void));
196 static __inline void pmap_changebit __P((vm_page_t m, int bit, boolean_t setem));
198 static void pmap_remove_all __P((vm_page_t m));
199 static vm_page_t pmap_enter_quick __P((pmap_t pmap, vm_offset_t va,
200 vm_page_t m, vm_page_t mpte));
201 static int pmap_remove_pte __P((struct pmap *pmap, unsigned *ptq,
203 static void pmap_remove_page __P((struct pmap *pmap, vm_offset_t va));
204 static int pmap_remove_entry __P((struct pmap *pmap, vm_page_t m,
206 static boolean_t pmap_testbit __P((vm_page_t m, int bit));
207 static void pmap_insert_entry __P((pmap_t pmap, vm_offset_t va,
208 vm_page_t mpte, vm_page_t m));
210 static vm_page_t pmap_allocpte __P((pmap_t pmap, vm_offset_t va));
212 static int pmap_release_free_page __P((pmap_t pmap, vm_page_t p));
213 static vm_page_t _pmap_allocpte __P((pmap_t pmap, unsigned ptepindex));
214 static unsigned * pmap_pte_quick __P((pmap_t pmap, vm_offset_t va));
215 static vm_page_t pmap_page_lookup __P((vm_object_t object, vm_pindex_t pindex));
216 static int pmap_unuse_pt __P((pmap_t, vm_offset_t, vm_page_t));
217 static vm_offset_t pmap_kmem_choose(vm_offset_t addr);
219 static unsigned pdir4mb;
224 * Extract the page table entry associated
225 * with the given map/virtual_address pair.
228 PMAP_INLINE unsigned *
230 register pmap_t pmap;
236 pdeaddr = (unsigned *) pmap_pde(pmap, va);
237 if (*pdeaddr & PG_PS)
240 return get_ptbase(pmap) + i386_btop(va);
247 * Move the kernel virtual free pointer to the next
248 * 4MB. This is used to help improve performance
249 * by using a large (4MB) page for much of the kernel
250 * (.text, .data, .bss)
253 pmap_kmem_choose(vm_offset_t addr)
255 vm_offset_t newaddr = addr;
257 if (cpu_feature & CPUID_PSE) {
258 newaddr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
265 * Bootstrap the system enough to run with virtual memory.
267 * On the i386 this is called after mapping has already been enabled
268 * and just syncs the pmap module with what has already been done.
269 * [We can't call it easily with mapping off since the kernel is not
270 * mapped with PA == VA, hence we would have to relocate every address
271 * from the linked base (virtual) address "KERNBASE" to the actual
272 * (physical) address starting relative to 0]
275 pmap_bootstrap(firstaddr, loadaddr)
276 vm_offset_t firstaddr;
277 vm_offset_t loadaddr;
281 struct globaldata *gd;
284 avail_start = firstaddr;
287 * XXX The calculation of virtual_avail is wrong. It's NKPT*PAGE_SIZE too
288 * large. It should instead be correctly calculated in locore.s and
289 * not based on 'first' (which is a physical address, not a virtual
290 * address, for the start of unused physical memory). The kernel
291 * page tables are NOT double mapped and thus should not be included
292 * in this calculation.
294 virtual_avail = (vm_offset_t) KERNBASE + firstaddr;
295 virtual_avail = pmap_kmem_choose(virtual_avail);
297 virtual_end = VM_MAX_KERNEL_ADDRESS;
300 * Initialize protection array.
302 i386_protection_init();
305 * The kernel's pmap is statically allocated so we don't have to use
306 * pmap_create, which is unlikely to work correctly at this part of
307 * the boot sequence (XXX and which no longer exists).
309 kernel_pmap = &kernel_pmap_store;
311 kernel_pmap->pm_pdir = (pd_entry_t *) (KERNBASE + (u_int)IdlePTD);
312 kernel_pmap->pm_count = 1;
313 kernel_pmap->pm_active = -1; /* don't allow deactivation */
314 TAILQ_INIT(&kernel_pmap->pm_pvlist);
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)
335 SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS);
338 * msgbufp is used to map the system message buffer.
339 * XXX msgbufmap is not used.
341 SYSMAP(struct msgbuf *, msgbufmap, msgbufp,
342 atop(round_page(MSGBUF_SIZE)))
347 for (i = 0; i < NKPT; i++)
350 /* XXX - see also mp_machdep.c */
351 if (ncpus == 1 && (cpu_feature & CPUID_PGE))
357 * Initialize the 4MB page size flag
361 * The 4MB page version of the initial
362 * kernel page mapping.
366 #if !defined(DISABLE_PSE)
367 if (cpu_feature & CPUID_PSE) {
370 * Note that we have enabled PSE mode
373 ptditmp = *((unsigned *)PTmap + i386_btop(KERNBASE));
374 ptditmp &= ~(NBPDR - 1);
375 ptditmp |= PG_V | PG_RW | PG_PS | PG_U | pgeflag;
380 * Enable the PSE mode.
382 load_cr4(rcr4() | CR4_PSE);
385 * We can do the mapping here for the single processor
386 * case. We simply ignore the old page table page from
390 * For SMP, we still need 4K pages to bootstrap APs,
391 * PSE will be enabled as soon as all APs are up.
393 PTD[KPTDI] = (pd_entry_t) ptditmp;
394 kernel_pmap->pm_pdir[KPTDI] = (pd_entry_t) ptditmp;
400 if (cpu_apic_address == 0)
401 panic("pmap_bootstrap: no local apic!");
403 /* local apic is mapped on last page */
404 SMPpt[NPTEPG - 1] = (pt_entry_t)(PG_V | PG_RW | PG_N | pgeflag |
405 (cpu_apic_address & PG_FRAME));
408 /* BSP does this itself, AP's get it pre-set */
409 gd = &CPU_prvspace[0].globaldata;
410 gd->gd_prv_CMAP1 = &SMPpt[1];
411 gd->gd_prv_CMAP2 = &SMPpt[2];
412 gd->gd_prv_CMAP3 = &SMPpt[3];
413 gd->gd_prv_PMAP1 = &SMPpt[4];
414 gd->gd_prv_CADDR1 = CPU_prvspace[0].CPAGE1;
415 gd->gd_prv_CADDR2 = CPU_prvspace[0].CPAGE2;
416 gd->gd_prv_CADDR3 = CPU_prvspace[0].CPAGE3;
417 gd->gd_prv_PADDR1 = (unsigned *)CPU_prvspace[0].PPAGE1;
424 * Set 4mb pdir for mp startup
429 if (pseflag && (cpu_feature & CPUID_PSE)) {
430 load_cr4(rcr4() | CR4_PSE);
431 if (pdir4mb && cpuid == 0) { /* only on BSP */
432 kernel_pmap->pm_pdir[KPTDI] =
433 PTD[KPTDI] = (pd_entry_t)pdir4mb;
441 * Initialize the pmap module.
442 * Called by vm_init, to initialize any structures that the pmap
443 * system needs to map virtual memory.
444 * pmap_init has been enhanced to support in a fairly consistant
445 * way, discontiguous physical memory.
448 pmap_init(phys_start, phys_end)
449 vm_offset_t phys_start, phys_end;
455 * object for kernel page table pages
457 kptobj = vm_object_allocate(OBJT_DEFAULT, NKPDE);
460 * Allocate memory for random pmap data structures. Includes the
464 for(i = 0; i < vm_page_array_size; i++) {
467 m = &vm_page_array[i];
468 TAILQ_INIT(&m->md.pv_list);
469 m->md.pv_list_count = 0;
473 * init the pv free list
475 initial_pvs = vm_page_array_size;
476 if (initial_pvs < MINPV)
478 pvzone = &pvzone_store;
479 pvinit = (struct pv_entry *) kmem_alloc(kernel_map,
480 initial_pvs * sizeof (struct pv_entry));
481 zbootinit(pvzone, "PV ENTRY", sizeof (struct pv_entry), pvinit,
485 * Now it is safe to enable pv_table recording.
487 pmap_initialized = TRUE;
491 * Initialize the address space (zone) for the pv_entries. Set a
492 * high water mark so that the system can recover from excessive
493 * numbers of pv entries.
498 int shpgperproc = PMAP_SHPGPERPROC;
500 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
501 pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
502 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
503 pv_entry_high_water = 9 * (pv_entry_max / 10);
504 zinitna(pvzone, &pvzone_obj, NULL, 0, pv_entry_max, ZONE_INTERRUPT, 1);
508 /***************************************************
509 * Low level helper routines.....
510 ***************************************************/
512 #if defined(PMAP_DIAGNOSTIC)
515 * This code checks for non-writeable/modified pages.
516 * This should be an invalid condition.
519 pmap_nw_modified(pt_entry_t ptea)
525 if ((pte & (PG_M|PG_RW)) == PG_M)
534 * this routine defines the region(s) of memory that should
535 * not be tested for the modified bit.
537 static PMAP_INLINE int
538 pmap_track_modified(vm_offset_t va)
540 if ((va < clean_sva) || (va >= clean_eva))
546 static PMAP_INLINE void
547 invltlb_1pg(vm_offset_t va)
549 #if defined(I386_CPU)
550 if (cpu_class == CPUCLASS_386) {
560 pmap_TLB_invalidate(pmap_t pmap, vm_offset_t va)
563 if (pmap->pm_active & (1 << cpuid))
564 cpu_invlpg((void *)va);
565 if (pmap->pm_active & other_cpus)
574 pmap_TLB_invalidate_all(pmap_t pmap)
577 if (pmap->pm_active & (1 << cpuid))
579 if (pmap->pm_active & other_cpus)
591 unsigned frame = (unsigned) pmap->pm_pdir[PTDPTDI] & PG_FRAME;
593 /* are we current address space or kernel? */
594 if (pmap == kernel_pmap || frame == (((unsigned) PTDpde) & PG_FRAME)) {
595 return (unsigned *) PTmap;
597 /* otherwise, we are alternate address space */
598 if (frame != (((unsigned) APTDpde) & PG_FRAME)) {
599 APTDpde = (pd_entry_t) (frame | PG_RW | PG_V);
601 /* The page directory is not shared between CPUs */
607 return (unsigned *) APTmap;
611 * Super fast pmap_pte routine best used when scanning
612 * the pv lists. This eliminates many coarse-grained
613 * invltlb calls. Note that many of the pv list
614 * scans are across different pmaps. It is very wasteful
615 * to do an entire invltlb for checking a single mapping.
619 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
621 struct globaldata *gd = mycpu;
624 if ((pde = (unsigned) pmap->pm_pdir[va >> PDRSHIFT]) != 0) {
625 unsigned frame = (unsigned) pmap->pm_pdir[PTDPTDI] & PG_FRAME;
626 unsigned index = i386_btop(va);
627 /* are we current address space or kernel? */
628 if ((pmap == kernel_pmap) ||
629 (frame == (((unsigned) PTDpde) & PG_FRAME))) {
630 return (unsigned *) PTmap + index;
632 newpf = pde & PG_FRAME;
633 if ( ((* (unsigned *) gd->gd_prv_PMAP1) & PG_FRAME) != newpf) {
634 * (unsigned *) gd->gd_prv_PMAP1 = newpf | PG_RW | PG_V;
635 cpu_invlpg(gd->gd_prv_PADDR1);
637 return gd->gd_prv_PADDR1 + ((unsigned) index & (NPTEPG - 1));
643 * Routine: pmap_extract
645 * Extract the physical page address associated
646 * with the given map/virtual_address pair.
649 pmap_extract(pmap, va)
650 register pmap_t pmap;
654 vm_offset_t pdirindex;
655 pdirindex = va >> PDRSHIFT;
656 if (pmap && (rtval = (unsigned) pmap->pm_pdir[pdirindex])) {
658 if ((rtval & PG_PS) != 0) {
659 rtval &= ~(NBPDR - 1);
660 rtval |= va & (NBPDR - 1);
663 pte = get_ptbase(pmap) + i386_btop(va);
664 rtval = ((*pte & PG_FRAME) | (va & PAGE_MASK));
671 /***************************************************
672 * Low level mapping routines.....
673 ***************************************************/
676 * add a wired page to the kva
677 * note that in order for the mapping to take effect -- you
678 * should do a invltlb after doing the pmap_kenter...
683 register vm_offset_t pa;
685 register unsigned *pte;
688 npte = pa | PG_RW | PG_V | pgeflag;
689 pte = (unsigned *)vtopte(va);
696 * remove a page from the kernel pagetables
702 register unsigned *pte;
704 pte = (unsigned *)vtopte(va);
710 * Used to map a range of physical addresses into kernel
711 * virtual address space.
713 * For now, VM is already on, we only need to map the
717 pmap_map(virt, start, end, prot)
723 while (start < end) {
724 pmap_kenter(virt, start);
733 * Add a list of wired pages to the kva
734 * this routine is only used for temporary
735 * kernel mappings that do not need to have
736 * page modification or references recorded.
737 * Note that old mappings are simply written
738 * over. The page *must* be wired.
741 pmap_qenter(va, m, count)
748 end_va = va + count * PAGE_SIZE;
750 while (va < end_va) {
753 pte = (unsigned *)vtopte(va);
754 *pte = VM_PAGE_TO_PHYS(*m) | PG_RW | PG_V | pgeflag;
756 cpu_invlpg((void *)va);
769 * this routine jerks page mappings from the
770 * kernel -- it is meant only for temporary mappings.
773 pmap_qremove(va, count)
779 end_va = va + count*PAGE_SIZE;
781 while (va < end_va) {
784 pte = (unsigned *)vtopte(va);
787 cpu_invlpg((void *)va);
799 pmap_page_lookup(object, pindex)
805 m = vm_page_lookup(object, pindex);
806 if (m && vm_page_sleep_busy(m, FALSE, "pplookp"))
812 * Create a new thread and optionally associate it with a (new) process.
815 pmap_init_thread(thread_t td)
817 td->td_pcb = (struct pcb *)(td->td_kstack + UPAGES * PAGE_SIZE) - 1;
818 td->td_sp = (char *)td->td_pcb - 16;
822 * Create the UPAGES for a new process.
823 * This routine directly affects the fork perf for a process.
826 pmap_init_proc(struct proc *p, struct thread *td)
828 p->p_addr = (void *)td->td_kstack;
831 td->td_switch = cpu_heavy_switch;
832 bzero(p->p_addr, sizeof(*p->p_addr));
836 * Dispose the UPAGES for a process that has exited.
837 * This routine directly impacts the exit perf of a process.
840 pmap_dispose_proc(struct proc *p)
844 KASSERT(p->p_lock == 0, ("attempt to dispose referenced proc! %p", p));
846 if ((td = p->p_thread) != NULL) {
855 * Allow the UPAGES for a process to be prejudicially paged out.
866 upobj = p->p_upages_obj;
868 * let the upages be paged
870 for(i=0;i<UPAGES;i++) {
871 if ((m = vm_page_lookup(upobj, i)) == NULL)
872 panic("pmap_swapout_proc: upage already missing???");
874 vm_page_unwire(m, 0);
875 pmap_kremove( (vm_offset_t) p->p_addr + PAGE_SIZE * i);
881 * Bring the UPAGES for a specified process back in.
892 upobj = p->p_upages_obj;
893 for(i=0;i<UPAGES;i++) {
895 m = vm_page_grab(upobj, i, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
897 pmap_kenter(((vm_offset_t) p->p_addr) + i * PAGE_SIZE,
900 if (m->valid != VM_PAGE_BITS_ALL) {
901 rv = vm_pager_get_pages(upobj, &m, 1, 0);
902 if (rv != VM_PAGER_OK)
903 panic("pmap_swapin_proc: cannot get upages for proc: %d\n", p->p_pid);
904 m = vm_page_lookup(upobj, i);
905 m->valid = VM_PAGE_BITS_ALL;
910 vm_page_flag_set(m, PG_MAPPED | PG_WRITEABLE);
915 /***************************************************
916 * Page table page management routines.....
917 ***************************************************/
920 * This routine unholds page table pages, and if the hold count
921 * drops to zero, then it decrements the wire count.
924 _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m) {
926 while (vm_page_sleep_busy(m, FALSE, "pmuwpt"))
929 if (m->hold_count == 0) {
932 * unmap the page table page
934 pmap->pm_pdir[m->pindex] = 0;
935 --pmap->pm_stats.resident_count;
936 if ((((unsigned)pmap->pm_pdir[PTDPTDI]) & PG_FRAME) ==
937 (((unsigned) PTDpde) & PG_FRAME)) {
939 * Do a invltlb to make the invalidated mapping
940 * take effect immediately.
942 pteva = UPT_MIN_ADDRESS + i386_ptob(m->pindex);
943 pmap_TLB_invalidate(pmap, pteva);
946 if (pmap->pm_ptphint == m)
947 pmap->pm_ptphint = NULL;
950 * If the page is finally unwired, simply free it.
953 if (m->wire_count == 0) {
957 vm_page_free_zero(m);
965 static PMAP_INLINE int
966 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
969 if (m->hold_count == 0)
970 return _pmap_unwire_pte_hold(pmap, m);
976 * After removing a page table entry, this routine is used to
977 * conditionally free the page, and manage the hold/wire counts.
980 pmap_unuse_pt(pmap, va, mpte)
986 if (va >= UPT_MIN_ADDRESS)
990 ptepindex = (va >> PDRSHIFT);
991 if (pmap->pm_ptphint &&
992 (pmap->pm_ptphint->pindex == ptepindex)) {
993 mpte = pmap->pm_ptphint;
995 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
996 pmap->pm_ptphint = mpte;
1000 return pmap_unwire_pte_hold(pmap, mpte);
1008 (pd_entry_t *)kmem_alloc_pageable(kernel_map, PAGE_SIZE);
1009 pmap_kenter((vm_offset_t) pmap->pm_pdir, (vm_offset_t) IdlePTD);
1011 pmap->pm_active = 0;
1012 pmap->pm_ptphint = NULL;
1013 TAILQ_INIT(&pmap->pm_pvlist);
1014 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1018 * Initialize a preallocated and zeroed pmap structure,
1019 * such as one in a vmspace structure.
1023 register struct pmap *pmap;
1028 * No need to allocate page table space yet but we do need a valid
1029 * page directory table.
1031 if (pmap->pm_pdir == NULL)
1033 (pd_entry_t *)kmem_alloc_pageable(kernel_map, PAGE_SIZE);
1036 * allocate object for the ptes
1038 if (pmap->pm_pteobj == NULL)
1039 pmap->pm_pteobj = vm_object_allocate( OBJT_DEFAULT, PTDPTDI + 1);
1042 * allocate the page directory page
1044 ptdpg = vm_page_grab( pmap->pm_pteobj, PTDPTDI,
1045 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1047 ptdpg->wire_count = 1;
1051 vm_page_flag_clear(ptdpg, PG_MAPPED | PG_BUSY); /* not usually mapped*/
1052 ptdpg->valid = VM_PAGE_BITS_ALL;
1054 pmap_kenter((vm_offset_t) pmap->pm_pdir, VM_PAGE_TO_PHYS(ptdpg));
1055 if ((ptdpg->flags & PG_ZERO) == 0)
1056 bzero(pmap->pm_pdir, PAGE_SIZE);
1058 pmap->pm_pdir[MPPTDI] = PTD[MPPTDI];
1060 /* install self-referential address mapping entry */
1061 *(unsigned *) (pmap->pm_pdir + PTDPTDI) =
1062 VM_PAGE_TO_PHYS(ptdpg) | PG_V | PG_RW | PG_A | PG_M;
1065 pmap->pm_active = 0;
1066 pmap->pm_ptphint = NULL;
1067 TAILQ_INIT(&pmap->pm_pvlist);
1068 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1072 * Wire in kernel global address entries. To avoid a race condition
1073 * between pmap initialization and pmap_growkernel, this procedure
1074 * should be called after the vmspace is attached to the process
1075 * but before this pmap is activated.
1081 /* XXX copies current process, does not fill in MPPTDI */
1082 bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * PTESIZE);
1086 pmap_release_free_page(pmap, 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;
1126 vm_page_free_zero(p);
1131 * this routine is called if the page table page is not
1135 _pmap_allocpte(pmap, ptepindex)
1139 vm_offset_t pteva, ptepa;
1143 * Find or fabricate a new pagetable page
1145 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1146 VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1148 KASSERT(m->queue == PQ_NONE,
1149 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1151 if (m->wire_count == 0)
1156 * Increment the hold count for the page table page
1157 * (denoting a new mapping.)
1162 * Map the pagetable page into the process address space, if
1163 * it isn't already there.
1166 pmap->pm_stats.resident_count++;
1168 ptepa = VM_PAGE_TO_PHYS(m);
1169 pmap->pm_pdir[ptepindex] =
1170 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_A | PG_M);
1173 * Set the page table hint
1175 pmap->pm_ptphint = m;
1178 * Try to use the new mapping, but if we cannot, then
1179 * do it with the routine that maps the page explicitly.
1181 if ((m->flags & PG_ZERO) == 0) {
1182 if ((((unsigned)pmap->pm_pdir[PTDPTDI]) & PG_FRAME) ==
1183 (((unsigned) PTDpde) & PG_FRAME)) {
1184 pteva = UPT_MIN_ADDRESS + i386_ptob(ptepindex);
1185 bzero((caddr_t) pteva, PAGE_SIZE);
1187 pmap_zero_page(ptepa);
1191 m->valid = VM_PAGE_BITS_ALL;
1192 vm_page_flag_clear(m, PG_ZERO);
1193 vm_page_flag_set(m, PG_MAPPED);
1200 pmap_allocpte(pmap, va)
1209 * Calculate pagetable page index
1211 ptepindex = va >> PDRSHIFT;
1214 * Get the page directory entry
1216 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1219 * This supports switching from a 4MB page to a
1222 if (ptepa & PG_PS) {
1223 pmap->pm_pdir[ptepindex] = 0;
1229 * If the page table page is mapped, we just increment the
1230 * hold count, and activate it.
1234 * In order to get the page table page, try the
1237 if (pmap->pm_ptphint &&
1238 (pmap->pm_ptphint->pindex == ptepindex)) {
1239 m = pmap->pm_ptphint;
1241 m = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1242 pmap->pm_ptphint = m;
1248 * Here if the pte page isn't mapped, or if it has been deallocated.
1250 return _pmap_allocpte(pmap, ptepindex);
1254 /***************************************************
1255 * Pmap allocation/deallocation routines.
1256 ***************************************************/
1259 * Release any resources held by the given physical map.
1260 * Called when a pmap initialized by pmap_pinit is being released.
1261 * Should only be called if the map contains no valid mappings.
1265 register struct pmap *pmap;
1267 vm_page_t p,n,ptdpg;
1268 vm_object_t object = pmap->pm_pteobj;
1271 #if defined(DIAGNOSTIC)
1272 if (object->ref_count != 1)
1273 panic("pmap_release: pteobj reference count != 1");
1278 curgeneration = object->generation;
1279 for (p = TAILQ_FIRST(&object->memq); p != NULL; p = n) {
1280 n = TAILQ_NEXT(p, listq);
1281 if (p->pindex == PTDPTDI) {
1286 if (!pmap_release_free_page(pmap, p) &&
1287 (object->generation != curgeneration))
1292 if (ptdpg && !pmap_release_free_page(pmap, ptdpg))
1297 kvm_size(SYSCTL_HANDLER_ARGS)
1299 unsigned long ksize = VM_MAX_KERNEL_ADDRESS - KERNBASE;
1301 return sysctl_handle_long(oidp, &ksize, 0, req);
1303 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD,
1304 0, 0, kvm_size, "IU", "Size of KVM");
1307 kvm_free(SYSCTL_HANDLER_ARGS)
1309 unsigned long kfree = VM_MAX_KERNEL_ADDRESS - kernel_vm_end;
1311 return sysctl_handle_long(oidp, &kfree, 0, req);
1313 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD,
1314 0, 0, kvm_free, "IU", "Amount of KVM free");
1317 * grow the number of kernel page table entries, if needed
1320 pmap_growkernel(vm_offset_t addr)
1325 vm_offset_t ptppaddr;
1330 if (kernel_vm_end == 0) {
1331 kernel_vm_end = KERNBASE;
1333 while (pdir_pde(PTD, kernel_vm_end)) {
1334 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1338 addr = (addr + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1339 while (kernel_vm_end < addr) {
1340 if (pdir_pde(PTD, kernel_vm_end)) {
1341 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1346 * This index is bogus, but out of the way
1348 nkpg = vm_page_alloc(kptobj, nkpt, VM_ALLOC_SYSTEM);
1350 panic("pmap_growkernel: no memory to grow kernel");
1355 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1356 pmap_zero_page(ptppaddr);
1357 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
1358 pdir_pde(PTD, kernel_vm_end) = newpdir;
1360 LIST_FOREACH(p, &allproc, p_list) {
1362 pmap = vmspace_pmap(p->p_vmspace);
1363 *pmap_pde(pmap, kernel_vm_end) = newpdir;
1366 *pmap_pde(kernel_pmap, kernel_vm_end) = newpdir;
1367 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1373 * Retire the given physical map from service.
1374 * Should only be called if the map contains
1375 * no valid mappings.
1379 register pmap_t pmap;
1386 count = --pmap->pm_count;
1389 panic("destroying a pmap is not yet implemented");
1394 * Add a reference to the specified pmap.
1397 pmap_reference(pmap)
1405 /***************************************************
1406 * page management routines.
1407 ***************************************************/
1410 * free the pv_entry back to the free list
1412 static PMAP_INLINE void
1421 * get a new pv_entry, allocating a block from the system
1423 * the memory allocation is performed bypassing the malloc code
1424 * because of the possibility of allocations at interrupt time.
1430 if (pv_entry_high_water &&
1431 (pv_entry_count > pv_entry_high_water) &&
1432 (pmap_pagedaemon_waken == 0)) {
1433 pmap_pagedaemon_waken = 1;
1434 wakeup (&vm_pages_needed);
1436 return zalloci(pvzone);
1440 * This routine is very drastic, but can save the system
1448 static int warningdone=0;
1450 if (pmap_pagedaemon_waken == 0)
1453 if (warningdone < 5) {
1454 printf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1458 for(i = 0; i < vm_page_array_size; i++) {
1459 m = &vm_page_array[i];
1460 if (m->wire_count || m->hold_count || m->busy ||
1461 (m->flags & PG_BUSY))
1465 pmap_pagedaemon_waken = 0;
1470 * If it is the first entry on the list, it is actually
1471 * in the header and we must copy the following entry up
1472 * to the header. Otherwise we must search the list for
1473 * the entry. In either case we free the now unused entry.
1477 pmap_remove_entry(pmap, m, va)
1487 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1488 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1489 if (pmap == pv->pv_pmap && va == pv->pv_va)
1493 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1494 if (va == pv->pv_va)
1502 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem);
1503 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1504 m->md.pv_list_count--;
1505 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
1506 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1508 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1517 * Create a pv entry for page at pa for
1521 pmap_insert_entry(pmap, va, mpte, m)
1532 pv = get_pv_entry();
1537 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1538 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1539 m->md.pv_list_count++;
1545 * pmap_remove_pte: do the things to unmap a page in a process
1548 pmap_remove_pte(pmap, ptq, va)
1556 oldpte = loadandclear(ptq);
1558 pmap->pm_stats.wired_count -= 1;
1560 * Machines that don't support invlpg, also don't support
1565 pmap->pm_stats.resident_count -= 1;
1566 if (oldpte & PG_MANAGED) {
1567 m = PHYS_TO_VM_PAGE(oldpte);
1568 if (oldpte & PG_M) {
1569 #if defined(PMAP_DIAGNOSTIC)
1570 if (pmap_nw_modified((pt_entry_t) oldpte)) {
1572 "pmap_remove: modified page not writable: va: 0x%x, pte: 0x%x\n",
1576 if (pmap_track_modified(va))
1580 vm_page_flag_set(m, PG_REFERENCED);
1581 return pmap_remove_entry(pmap, m, va);
1583 return pmap_unuse_pt(pmap, va, NULL);
1590 * Remove a single page from a process address space
1593 pmap_remove_page(pmap, va)
1595 register vm_offset_t va;
1597 register unsigned *ptq;
1600 * if there is no pte for this address, just skip it!!!
1602 if (*pmap_pde(pmap, va) == 0) {
1607 * get a local va for mappings for this pmap.
1609 ptq = get_ptbase(pmap) + i386_btop(va);
1611 (void) pmap_remove_pte(pmap, ptq, va);
1612 pmap_TLB_invalidate(pmap, va);
1618 * Remove the given range of addresses from the specified map.
1620 * It is assumed that the start and end are properly
1621 * rounded to the page size.
1624 pmap_remove(pmap, sva, eva)
1626 register vm_offset_t sva;
1627 register vm_offset_t eva;
1629 register unsigned *ptbase;
1631 vm_offset_t ptpaddr;
1632 vm_offset_t sindex, eindex;
1638 if (pmap->pm_stats.resident_count == 0)
1642 * special handling of removing one page. a very
1643 * common operation and easy to short circuit some
1646 if (((sva + PAGE_SIZE) == eva) &&
1647 (((unsigned) pmap->pm_pdir[(sva >> PDRSHIFT)] & PG_PS) == 0)) {
1648 pmap_remove_page(pmap, sva);
1655 * Get a local virtual address for the mappings that are being
1658 ptbase = get_ptbase(pmap);
1660 sindex = i386_btop(sva);
1661 eindex = i386_btop(eva);
1663 for (; sindex < eindex; sindex = pdnxt) {
1667 * Calculate index for next page table.
1669 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1670 if (pmap->pm_stats.resident_count == 0)
1673 pdirindex = sindex / NPDEPG;
1674 if (((ptpaddr = (unsigned) pmap->pm_pdir[pdirindex]) & PG_PS) != 0) {
1675 pmap->pm_pdir[pdirindex] = 0;
1676 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1682 * Weed out invalid mappings. Note: we assume that the page
1683 * directory table is always allocated, and in kernel virtual.
1689 * Limit our scan to either the end of the va represented
1690 * by the current page table page, or to the end of the
1691 * range being removed.
1693 if (pdnxt > eindex) {
1697 for ( ;sindex != pdnxt; sindex++) {
1699 if (ptbase[sindex] == 0) {
1702 va = i386_ptob(sindex);
1705 if (pmap_remove_pte(pmap,
1706 ptbase + sindex, va))
1712 pmap_TLB_invalidate_all(pmap);
1716 * Routine: pmap_remove_all
1718 * Removes this physical page from
1719 * all physical maps in which it resides.
1720 * Reflects back modify bits to the pager.
1723 * Original versions of this routine were very
1724 * inefficient because they iteratively called
1725 * pmap_remove (slow...)
1732 register pv_entry_t pv;
1733 register unsigned *pte, tpte;
1736 #if defined(PMAP_DIAGNOSTIC)
1738 * XXX this makes pmap_page_protect(NONE) illegal for non-managed
1741 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
1742 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%x", VM_PAGE_TO_PHYS(m));
1747 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
1748 pv->pv_pmap->pm_stats.resident_count--;
1750 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
1752 tpte = loadandclear(pte);
1754 pv->pv_pmap->pm_stats.wired_count--;
1757 vm_page_flag_set(m, PG_REFERENCED);
1760 * Update the vm_page_t clean and reference bits.
1763 #if defined(PMAP_DIAGNOSTIC)
1764 if (pmap_nw_modified((pt_entry_t) tpte)) {
1766 "pmap_remove_all: modified page not writable: va: 0x%x, pte: 0x%x\n",
1770 if (pmap_track_modified(pv->pv_va))
1773 pmap_TLB_invalidate(pv->pv_pmap, pv->pv_va);
1775 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
1776 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1777 m->md.pv_list_count--;
1778 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
1782 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1788 * Set the physical protection on the
1789 * specified range of this map as requested.
1792 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
1794 register unsigned *ptbase;
1795 vm_offset_t pdnxt, ptpaddr;
1796 vm_pindex_t sindex, eindex;
1802 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1803 pmap_remove(pmap, sva, eva);
1807 if (prot & VM_PROT_WRITE)
1812 ptbase = get_ptbase(pmap);
1814 sindex = i386_btop(sva);
1815 eindex = i386_btop(eva);
1817 for (; sindex < eindex; sindex = pdnxt) {
1821 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1823 pdirindex = sindex / NPDEPG;
1824 if (((ptpaddr = (unsigned) pmap->pm_pdir[pdirindex]) & PG_PS) != 0) {
1825 (unsigned) pmap->pm_pdir[pdirindex] &= ~(PG_M|PG_RW);
1826 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1832 * Weed out invalid mappings. Note: we assume that the page
1833 * directory table is always allocated, and in kernel virtual.
1838 if (pdnxt > eindex) {
1842 for (; sindex != pdnxt; sindex++) {
1847 pbits = ptbase[sindex];
1849 if (pbits & PG_MANAGED) {
1852 m = PHYS_TO_VM_PAGE(pbits);
1853 vm_page_flag_set(m, PG_REFERENCED);
1857 if (pmap_track_modified(i386_ptob(sindex))) {
1859 m = PHYS_TO_VM_PAGE(pbits);
1868 if (pbits != ptbase[sindex]) {
1869 ptbase[sindex] = pbits;
1875 pmap_TLB_invalidate_all(pmap);
1879 * Insert the given physical page (p) at
1880 * the specified virtual address (v) in the
1881 * target physical map with the protection requested.
1883 * If specified, the page will be wired down, meaning
1884 * that the related pte can not be reclaimed.
1886 * NB: This is the only routine which MAY NOT lazy-evaluate
1887 * or lose information. That is, this routine must actually
1888 * insert this page into the given map NOW.
1891 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1895 register unsigned *pte;
1897 vm_offset_t origpte, newpte;
1904 #ifdef PMAP_DIAGNOSTIC
1905 if (va > VM_MAX_KERNEL_ADDRESS)
1906 panic("pmap_enter: toobig");
1907 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
1908 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%x)", va);
1913 * In the case that a page table page is not
1914 * resident, we are creating it here.
1916 if (va < UPT_MIN_ADDRESS) {
1917 mpte = pmap_allocpte(pmap, va);
1919 #if 0 && defined(PMAP_DIAGNOSTIC)
1921 vm_offset_t *pdeaddr = (vm_offset_t *)pmap_pde(pmap, va);
1922 if (((origpte = (vm_offset_t) *pdeaddr) & PG_V) == 0) {
1923 panic("pmap_enter: invalid kernel page table page(0), pdir=%p, pde=%p, va=%p\n",
1924 pmap->pm_pdir[PTDPTDI], origpte, va);
1927 pdeaddr = (vm_offset_t *) IdlePTDS[cpuid];
1928 if (((newpte = pdeaddr[va >> PDRSHIFT]) & PG_V) == 0) {
1929 if ((vm_offset_t) my_idlePTD != (vm_offset_t) vtophys(pdeaddr))
1930 printf("pde mismatch: %x, %x\n", my_idlePTD, pdeaddr);
1931 printf("cpuid: %d, pdeaddr: 0x%x\n", cpuid, pdeaddr);
1932 panic("pmap_enter: invalid kernel page table page(1), pdir=%p, npde=%p, pde=%p, va=%p\n",
1933 pmap->pm_pdir[PTDPTDI], newpte, origpte, va);
1939 pte = pmap_pte(pmap, va);
1942 * Page Directory table entry not valid, we need a new PT page
1945 panic("pmap_enter: invalid page directory, pdir=%p, va=0x%x\n",
1946 (void *)pmap->pm_pdir[PTDPTDI], va);
1949 pa = VM_PAGE_TO_PHYS(m) & PG_FRAME;
1950 origpte = *(vm_offset_t *)pte;
1951 opa = origpte & PG_FRAME;
1953 if (origpte & PG_PS)
1954 panic("pmap_enter: attempted pmap_enter on 4MB page");
1957 * Mapping has not changed, must be protection or wiring change.
1959 if (origpte && (opa == pa)) {
1961 * Wiring change, just update stats. We don't worry about
1962 * wiring PT pages as they remain resident as long as there
1963 * are valid mappings in them. Hence, if a user page is wired,
1964 * the PT page will be also.
1966 if (wired && ((origpte & PG_W) == 0))
1967 pmap->pm_stats.wired_count++;
1968 else if (!wired && (origpte & PG_W))
1969 pmap->pm_stats.wired_count--;
1971 #if defined(PMAP_DIAGNOSTIC)
1972 if (pmap_nw_modified((pt_entry_t) origpte)) {
1974 "pmap_enter: modified page not writable: va: 0x%x, pte: 0x%x\n",
1980 * Remove extra pte reference
1985 if ((prot & VM_PROT_WRITE) && (origpte & PG_V)) {
1986 if ((origpte & PG_RW) == 0) {
1989 cpu_invlpg((void *)va);
1990 if (pmap->pm_active & other_cpus)
2000 * We might be turning off write access to the page,
2001 * so we go ahead and sense modify status.
2003 if (origpte & PG_MANAGED) {
2004 if ((origpte & PG_M) && pmap_track_modified(va)) {
2006 om = PHYS_TO_VM_PAGE(opa);
2014 * Mapping has changed, invalidate old range and fall through to
2015 * handle validating new mapping.
2019 err = pmap_remove_pte(pmap, pte, va);
2021 panic("pmap_enter: pte vanished, va: 0x%x", va);
2025 * Enter on the PV list if part of our managed memory. Note that we
2026 * raise IPL while manipulating pv_table since pmap_enter can be
2027 * called at interrupt time.
2029 if (pmap_initialized &&
2030 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2031 pmap_insert_entry(pmap, va, mpte, m);
2036 * Increment counters
2038 pmap->pm_stats.resident_count++;
2040 pmap->pm_stats.wired_count++;
2044 * Now validate mapping with desired protection/wiring.
2046 newpte = (vm_offset_t) (pa | pte_prot(pmap, prot) | PG_V);
2050 if (va < UPT_MIN_ADDRESS)
2052 if (pmap == kernel_pmap)
2056 * if the mapping or permission bits are different, we need
2057 * to update the pte.
2059 if ((origpte & ~(PG_M|PG_A)) != newpte) {
2060 *pte = newpte | PG_A;
2063 cpu_invlpg((void *)va);
2064 if (pmap->pm_active & other_cpus)
2074 * this code makes some *MAJOR* assumptions:
2075 * 1. Current pmap & pmap exists.
2078 * 4. No page table pages.
2079 * 5. Tlbflush is deferred to calling procedure.
2080 * 6. Page IS managed.
2081 * but is *MUCH* faster than pmap_enter...
2085 pmap_enter_quick(pmap, va, m, mpte)
2086 register pmap_t pmap;
2095 * In the case that a page table page is not
2096 * resident, we are creating it here.
2098 if (va < UPT_MIN_ADDRESS) {
2103 * Calculate pagetable page index
2105 ptepindex = va >> PDRSHIFT;
2106 if (mpte && (mpte->pindex == ptepindex)) {
2111 * Get the page directory entry
2113 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
2116 * If the page table page is mapped, we just increment
2117 * the hold count, and activate it.
2121 panic("pmap_enter_quick: unexpected mapping into 4MB page");
2122 if (pmap->pm_ptphint &&
2123 (pmap->pm_ptphint->pindex == ptepindex)) {
2124 mpte = pmap->pm_ptphint;
2126 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
2127 pmap->pm_ptphint = mpte;
2133 mpte = _pmap_allocpte(pmap, ptepindex);
2141 * This call to vtopte makes the assumption that we are
2142 * entering the page into the current pmap. In order to support
2143 * quick entry into any pmap, one would likely use pmap_pte_quick.
2144 * But that isn't as quick as vtopte.
2146 pte = (unsigned *)vtopte(va);
2149 pmap_unwire_pte_hold(pmap, mpte);
2154 * Enter on the PV list if part of our managed memory. Note that we
2155 * raise IPL while manipulating pv_table since pmap_enter can be
2156 * called at interrupt time.
2158 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0)
2159 pmap_insert_entry(pmap, va, mpte, m);
2162 * Increment counters
2164 pmap->pm_stats.resident_count++;
2166 pa = VM_PAGE_TO_PHYS(m);
2169 * Now validate mapping with RO protection
2171 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2172 *pte = pa | PG_V | PG_U;
2174 *pte = pa | PG_V | PG_U | PG_MANAGED;
2180 * Make a temporary mapping for a physical address. This is only intended
2181 * to be used for panic dumps.
2184 pmap_kenter_temporary(vm_offset_t pa, int i)
2186 pmap_kenter((vm_offset_t)crashdumpmap + (i * PAGE_SIZE), pa);
2187 return ((void *)crashdumpmap);
2190 #define MAX_INIT_PT (96)
2192 * pmap_object_init_pt preloads the ptes for a given object
2193 * into the specified pmap. This eliminates the blast of soft
2194 * faults on process startup and immediately after an mmap.
2197 pmap_object_init_pt(pmap, addr, object, pindex, size, limit)
2210 if (pmap == NULL || object == NULL)
2214 * This code maps large physical mmap regions into the
2215 * processor address space. Note that some shortcuts
2216 * are taken, but the code works.
2219 (object->type == OBJT_DEVICE) &&
2220 ((addr & (NBPDR - 1)) == 0) &&
2221 ((size & (NBPDR - 1)) == 0) ) {
2224 unsigned int ptepindex;
2228 if (pmap->pm_pdir[ptepindex = (addr >> PDRSHIFT)])
2232 p = vm_page_lookup(object, pindex);
2233 if (p && vm_page_sleep_busy(p, FALSE, "init4p"))
2237 p = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL);
2242 if (vm_pager_get_pages(object, m, 1, 0) != VM_PAGER_OK) {
2247 p = vm_page_lookup(object, pindex);
2251 ptepa = (vm_offset_t) VM_PAGE_TO_PHYS(p);
2252 if (ptepa & (NBPDR - 1)) {
2256 p->valid = VM_PAGE_BITS_ALL;
2258 pmap->pm_stats.resident_count += size >> PAGE_SHIFT;
2259 npdes = size >> PDRSHIFT;
2260 for(i=0;i<npdes;i++) {
2261 pmap->pm_pdir[ptepindex] =
2262 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_PS);
2266 vm_page_flag_set(p, PG_MAPPED);
2271 psize = i386_btop(size);
2273 if ((object->type != OBJT_VNODE) ||
2274 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2275 (object->resident_page_count > MAX_INIT_PT))) {
2279 if (psize + pindex > object->size) {
2280 if (object->size < pindex)
2282 psize = object->size - pindex;
2287 * if we are processing a major portion of the object, then scan the
2290 if (psize > (object->resident_page_count >> 2)) {
2293 for (p = TAILQ_FIRST(&object->memq);
2294 ((objpgs > 0) && (p != NULL));
2295 p = TAILQ_NEXT(p, listq)) {
2298 if (tmpidx < pindex) {
2302 if (tmpidx >= psize) {
2306 * don't allow an madvise to blow away our really
2307 * free pages allocating pv entries.
2309 if ((limit & MAP_PREFAULT_MADVISE) &&
2310 cnt.v_free_count < cnt.v_free_reserved) {
2313 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2315 (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2316 if ((p->queue - p->pc) == PQ_CACHE)
2317 vm_page_deactivate(p);
2319 mpte = pmap_enter_quick(pmap,
2320 addr + i386_ptob(tmpidx), p, mpte);
2321 vm_page_flag_set(p, PG_MAPPED);
2328 * else lookup the pages one-by-one.
2330 for (tmpidx = 0; tmpidx < psize; tmpidx += 1) {
2332 * don't allow an madvise to blow away our really
2333 * free pages allocating pv entries.
2335 if ((limit & MAP_PREFAULT_MADVISE) &&
2336 cnt.v_free_count < cnt.v_free_reserved) {
2339 p = vm_page_lookup(object, tmpidx + pindex);
2341 ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2343 (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2344 if ((p->queue - p->pc) == PQ_CACHE)
2345 vm_page_deactivate(p);
2347 mpte = pmap_enter_quick(pmap,
2348 addr + i386_ptob(tmpidx), p, mpte);
2349 vm_page_flag_set(p, PG_MAPPED);
2358 * pmap_prefault provides a quick way of clustering
2359 * pagefaults into a processes address space. It is a "cousin"
2360 * of pmap_object_init_pt, except it runs at page fault time instead
2365 #define PAGEORDER_SIZE (PFBAK+PFFOR)
2367 static int pmap_prefault_pageorder[] = {
2368 -PAGE_SIZE, PAGE_SIZE,
2369 -2 * PAGE_SIZE, 2 * PAGE_SIZE,
2370 -3 * PAGE_SIZE, 3 * PAGE_SIZE
2371 -4 * PAGE_SIZE, 4 * PAGE_SIZE
2375 pmap_prefault(pmap, addra, entry)
2378 vm_map_entry_t entry;
2387 if (!curproc || (pmap != vmspace_pmap(curproc->p_vmspace)))
2390 object = entry->object.vm_object;
2392 starta = addra - PFBAK * PAGE_SIZE;
2393 if (starta < entry->start) {
2394 starta = entry->start;
2395 } else if (starta > addra) {
2400 for (i = 0; i < PAGEORDER_SIZE; i++) {
2401 vm_object_t lobject;
2404 addr = addra + pmap_prefault_pageorder[i];
2405 if (addr > addra + (PFFOR * PAGE_SIZE))
2408 if (addr < starta || addr >= entry->end)
2411 if ((*pmap_pde(pmap, addr)) == NULL)
2414 pte = (unsigned *) vtopte(addr);
2418 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2420 for (m = vm_page_lookup(lobject, pindex);
2421 (!m && (lobject->type == OBJT_DEFAULT) && (lobject->backing_object));
2422 lobject = lobject->backing_object) {
2423 if (lobject->backing_object_offset & PAGE_MASK)
2425 pindex += (lobject->backing_object_offset >> PAGE_SHIFT);
2426 m = vm_page_lookup(lobject->backing_object, pindex);
2430 * give-up when a page is not in memory
2435 if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2437 (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2439 if ((m->queue - m->pc) == PQ_CACHE) {
2440 vm_page_deactivate(m);
2443 mpte = pmap_enter_quick(pmap, addr, m, mpte);
2444 vm_page_flag_set(m, PG_MAPPED);
2451 * Routine: pmap_change_wiring
2452 * Function: Change the wiring attribute for a map/virtual-address
2454 * In/out conditions:
2455 * The mapping must already exist in the pmap.
2458 pmap_change_wiring(pmap, va, wired)
2459 register pmap_t pmap;
2463 register unsigned *pte;
2468 pte = pmap_pte(pmap, va);
2470 if (wired && !pmap_pte_w(pte))
2471 pmap->pm_stats.wired_count++;
2472 else if (!wired && pmap_pte_w(pte))
2473 pmap->pm_stats.wired_count--;
2476 * Wiring is not a hardware characteristic so there is no need to
2479 pmap_pte_set_w(pte, wired);
2485 * Copy the range specified by src_addr/len
2486 * from the source map to the range dst_addr/len
2487 * in the destination map.
2489 * This routine is only advisory and need not do anything.
2493 pmap_copy(dst_pmap, src_pmap, dst_addr, len, src_addr)
2494 pmap_t dst_pmap, src_pmap;
2495 vm_offset_t dst_addr;
2497 vm_offset_t src_addr;
2500 vm_offset_t end_addr = src_addr + len;
2502 unsigned src_frame, dst_frame;
2505 if (dst_addr != src_addr)
2508 src_frame = ((unsigned) src_pmap->pm_pdir[PTDPTDI]) & PG_FRAME;
2509 if (src_frame != (((unsigned) PTDpde) & PG_FRAME)) {
2513 dst_frame = ((unsigned) dst_pmap->pm_pdir[PTDPTDI]) & PG_FRAME;
2514 if (dst_frame != (((unsigned) APTDpde) & PG_FRAME)) {
2515 APTDpde = (pd_entry_t) (dst_frame | PG_RW | PG_V);
2517 /* The page directory is not shared between CPUs */
2524 for(addr = src_addr; addr < end_addr; addr = pdnxt) {
2525 unsigned *src_pte, *dst_pte;
2526 vm_page_t dstmpte, srcmpte;
2527 vm_offset_t srcptepaddr;
2530 if (addr >= UPT_MIN_ADDRESS)
2531 panic("pmap_copy: invalid to pmap_copy page tables\n");
2534 * Don't let optional prefaulting of pages make us go
2535 * way below the low water mark of free pages or way
2536 * above high water mark of used pv entries.
2538 if (cnt.v_free_count < cnt.v_free_reserved ||
2539 pv_entry_count > pv_entry_high_water)
2542 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
2543 ptepindex = addr >> PDRSHIFT;
2545 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex];
2546 if (srcptepaddr == 0)
2549 if (srcptepaddr & PG_PS) {
2550 if (dst_pmap->pm_pdir[ptepindex] == 0) {
2551 dst_pmap->pm_pdir[ptepindex] = (pd_entry_t) srcptepaddr;
2552 dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
2557 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
2558 if ((srcmpte == NULL) ||
2559 (srcmpte->hold_count == 0) || (srcmpte->flags & PG_BUSY))
2562 if (pdnxt > end_addr)
2565 src_pte = (unsigned *) vtopte(addr);
2566 dst_pte = (unsigned *) avtopte(addr);
2567 while (addr < pdnxt) {
2571 * we only virtual copy managed pages
2573 if ((ptetemp & PG_MANAGED) != 0) {
2575 * We have to check after allocpte for the
2576 * pte still being around... allocpte can
2579 dstmpte = pmap_allocpte(dst_pmap, addr);
2580 if ((*dst_pte == 0) && (ptetemp = *src_pte)) {
2582 * Clear the modified and
2583 * accessed (referenced) bits
2586 m = PHYS_TO_VM_PAGE(ptetemp);
2587 *dst_pte = ptetemp & ~(PG_M | PG_A);
2588 dst_pmap->pm_stats.resident_count++;
2589 pmap_insert_entry(dst_pmap, addr,
2592 pmap_unwire_pte_hold(dst_pmap, dstmpte);
2594 if (dstmpte->hold_count >= srcmpte->hold_count)
2605 * Routine: pmap_kernel
2607 * Returns the physical map handle for the kernel.
2612 return (kernel_pmap);
2616 * pmap_zero_page zeros the specified hardware page by mapping
2617 * the page into KVM and using bzero to clear its contents.
2620 pmap_zero_page(vm_offset_t phys)
2622 struct globaldata *gd = mycpu;
2624 if (*(int *)gd->gd_prv_CMAP3)
2625 panic("pmap_zero_page: prv_CMAP3 busy");
2627 *(int *)gd->gd_prv_CMAP3 =
2628 PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2629 cpu_invlpg(gd->gd_prv_CADDR3);
2631 #if defined(I686_CPU)
2632 if (cpu_class == CPUCLASS_686)
2633 i686_pagezero(gd->gd_prv_CADDR3);
2636 bzero(gd->gd_prv_CADDR3, PAGE_SIZE);
2638 *(int *) gd->gd_prv_CMAP3 = 0;
2642 * pmap_zero_page_area zeros the specified hardware page by mapping
2643 * the page into KVM and using bzero to clear its contents.
2645 * off and size may not cover an area beyond a single hardware page.
2648 pmap_zero_page_area(phys, off, size)
2653 struct globaldata *gd = mycpu;
2655 if (*(int *) gd->gd_prv_CMAP3)
2656 panic("pmap_zero_page: prv_CMAP3 busy");
2658 *(int *) gd->gd_prv_CMAP3 = PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2659 cpu_invlpg(gd->gd_prv_CADDR3);
2661 #if defined(I686_CPU)
2662 if (cpu_class == CPUCLASS_686 && off == 0 && size == PAGE_SIZE)
2663 i686_pagezero(gd->gd_prv_CADDR3);
2666 bzero((char *)gd->gd_prv_CADDR3 + off, size);
2668 *(int *) gd->gd_prv_CMAP3 = 0;
2672 * pmap_copy_page copies the specified (machine independent)
2673 * page by mapping the page into virtual memory and using
2674 * bcopy to copy the page, one machine dependent page at a
2678 pmap_copy_page(src, dst)
2682 struct globaldata *gd = mycpu;
2684 if (*(int *) gd->gd_prv_CMAP1)
2685 panic("pmap_copy_page: prv_CMAP1 busy");
2686 if (*(int *) gd->gd_prv_CMAP2)
2687 panic("pmap_copy_page: prv_CMAP2 busy");
2689 *(int *) gd->gd_prv_CMAP1 = PG_V | (src & PG_FRAME) | PG_A;
2690 *(int *) gd->gd_prv_CMAP2 = PG_V | PG_RW | (dst & PG_FRAME) | PG_A | PG_M;
2692 cpu_invlpg(gd->gd_prv_CADDR1);
2693 cpu_invlpg(gd->gd_prv_CADDR2);
2695 bcopy(gd->gd_prv_CADDR1, gd->gd_prv_CADDR2, PAGE_SIZE);
2697 *(int *) gd->gd_prv_CMAP1 = 0;
2698 *(int *) gd->gd_prv_CMAP2 = 0;
2703 * Routine: pmap_pageable
2705 * Make the specified pages (by pmap, offset)
2706 * pageable (or not) as requested.
2708 * A page which is not pageable may not take
2709 * a fault; therefore, its page table entry
2710 * must remain valid for the duration.
2712 * This routine is merely advisory; pmap_enter
2713 * will specify that these pages are to be wired
2714 * down (or not) as appropriate.
2717 pmap_pageable(pmap, sva, eva, pageable)
2719 vm_offset_t sva, eva;
2725 * Returns true if the pmap's pv is one of the first
2726 * 16 pvs linked to from this page. This count may
2727 * be changed upwards or downwards in the future; it
2728 * is only necessary that true be returned for a small
2729 * subset of pmaps for proper page aging.
2732 pmap_page_exists_quick(pmap, m)
2740 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2745 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2746 if (pv->pv_pmap == pmap) {
2758 #define PMAP_REMOVE_PAGES_CURPROC_ONLY
2760 * Remove all pages from specified address space
2761 * this aids process exit speeds. Also, this code
2762 * is special cased for current process only, but
2763 * can have the more generic (and slightly slower)
2764 * mode enabled. This is much faster than pmap_remove
2765 * in the case of running down an entire address space.
2768 pmap_remove_pages(pmap, sva, eva)
2770 vm_offset_t sva, eva;
2772 unsigned *pte, tpte;
2777 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2778 if (!curproc || (pmap != vmspace_pmap(curproc->p_vmspace))) {
2779 printf("warning: pmap_remove_pages called with non-current pmap\n");
2785 for(pv = TAILQ_FIRST(&pmap->pm_pvlist);
2789 if (pv->pv_va >= eva || pv->pv_va < sva) {
2790 npv = TAILQ_NEXT(pv, pv_plist);
2794 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2795 pte = (unsigned *)vtopte(pv->pv_va);
2797 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2802 * We cannot remove wired pages from a process' mapping at this time
2805 npv = TAILQ_NEXT(pv, pv_plist);
2810 m = PHYS_TO_VM_PAGE(tpte);
2812 KASSERT(m < &vm_page_array[vm_page_array_size],
2813 ("pmap_remove_pages: bad tpte %x", tpte));
2815 pv->pv_pmap->pm_stats.resident_count--;
2818 * Update the vm_page_t clean and reference bits.
2825 npv = TAILQ_NEXT(pv, pv_plist);
2826 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2828 m->md.pv_list_count--;
2829 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2830 if (TAILQ_FIRST(&m->md.pv_list) == NULL) {
2831 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2834 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
2838 pmap_TLB_invalidate_all(pmap);
2842 * pmap_testbit tests bits in pte's
2843 * note that the testbit/changebit routines are inline,
2844 * and a lot of things compile-time evaluate.
2847 pmap_testbit(m, bit)
2855 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2858 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2863 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2865 * if the bit being tested is the modified bit, then
2866 * mark clean_map and ptes as never
2869 if (bit & (PG_A|PG_M)) {
2870 if (!pmap_track_modified(pv->pv_va))
2874 #if defined(PMAP_DIAGNOSTIC)
2876 printf("Null pmap (tb) at va: 0x%x\n", pv->pv_va);
2880 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2891 * this routine is used to modify bits in ptes
2893 static __inline void
2894 pmap_changebit(m, bit, setem)
2899 register pv_entry_t pv;
2900 register unsigned *pte;
2903 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2909 * Loop over all current mappings setting/clearing as appropos If
2910 * setting RO do we need to clear the VAC?
2912 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2914 * don't write protect pager mappings
2916 if (!setem && (bit == PG_RW)) {
2917 if (!pmap_track_modified(pv->pv_va))
2921 #if defined(PMAP_DIAGNOSTIC)
2923 printf("Null pmap (cb) at va: 0x%x\n", pv->pv_va);
2928 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2932 pmap_TLB_invalidate(pv->pv_pmap, pv->pv_va);
2934 vm_offset_t pbits = *(vm_offset_t *)pte;
2940 *(int *)pte = pbits & ~(PG_M|PG_RW);
2942 *(int *)pte = pbits & ~bit;
2944 pmap_TLB_invalidate(pv->pv_pmap, pv->pv_va);
2952 * pmap_page_protect:
2954 * Lower the permission for all mappings to a given page.
2957 pmap_page_protect(vm_page_t m, vm_prot_t prot)
2959 if ((prot & VM_PROT_WRITE) == 0) {
2960 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
2961 pmap_changebit(m, PG_RW, FALSE);
2969 pmap_phys_address(ppn)
2972 return (i386_ptob(ppn));
2976 * pmap_ts_referenced:
2978 * Return a count of reference bits for a page, clearing those bits.
2979 * It is not necessary for every reference bit to be cleared, but it
2980 * is necessary that 0 only be returned when there are truly no
2981 * reference bits set.
2983 * XXX: The exact number of bits to check and clear is a matter that
2984 * should be tested and standardized at some point in the future for
2985 * optimal aging of shared pages.
2988 pmap_ts_referenced(vm_page_t m)
2990 register pv_entry_t pv, pvf, pvn;
2995 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3000 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3005 pvn = TAILQ_NEXT(pv, pv_list);
3007 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3009 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3011 if (!pmap_track_modified(pv->pv_va))
3014 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3016 if (pte && (*pte & PG_A)) {
3019 pmap_TLB_invalidate(pv->pv_pmap, pv->pv_va);
3026 } while ((pv = pvn) != NULL && pv != pvf);
3036 * Return whether or not the specified physical page was modified
3037 * in any physical maps.
3040 pmap_is_modified(vm_page_t m)
3042 return pmap_testbit(m, PG_M);
3046 * Clear the modify bits on the specified physical page.
3049 pmap_clear_modify(vm_page_t m)
3051 pmap_changebit(m, PG_M, FALSE);
3055 * pmap_clear_reference:
3057 * Clear the reference bit on the specified physical page.
3060 pmap_clear_reference(vm_page_t m)
3062 pmap_changebit(m, PG_A, FALSE);
3066 * Miscellaneous support routines follow
3070 i386_protection_init()
3072 register int *kp, prot;
3074 kp = protection_codes;
3075 for (prot = 0; prot < 8; prot++) {
3077 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE:
3079 * Read access is also 0. There isn't any execute bit,
3080 * so just make it readable.
3082 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE:
3083 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE:
3084 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE:
3087 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE:
3088 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE:
3089 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE:
3090 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
3098 * Map a set of physical memory pages into the kernel virtual
3099 * address space. Return a pointer to where it is mapped. This
3100 * routine is intended to be used for mapping device memory,
3104 pmap_mapdev(pa, size)
3108 vm_offset_t va, tmpva, offset;
3111 offset = pa & PAGE_MASK;
3112 size = roundup(offset + size, PAGE_SIZE);
3114 va = kmem_alloc_pageable(kernel_map, size);
3116 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3119 for (tmpva = va; size > 0;) {
3120 pte = (unsigned *)vtopte(tmpva);
3121 *pte = pa | PG_RW | PG_V | pgeflag;
3128 return ((void *)(va + offset));
3132 pmap_unmapdev(va, size)
3136 vm_offset_t base, offset;
3138 base = va & PG_FRAME;
3139 offset = va & PAGE_MASK;
3140 size = roundup(offset + size, PAGE_SIZE);
3141 kmem_free(kernel_map, base, size);
3145 * perform the pmap work for mincore
3148 pmap_mincore(pmap, addr)
3153 unsigned *ptep, pte;
3157 ptep = pmap_pte(pmap, addr);
3162 if ((pte = *ptep) != 0) {
3165 val = MINCORE_INCORE;
3166 if ((pte & PG_MANAGED) == 0)
3169 pa = pte & PG_FRAME;
3171 m = PHYS_TO_VM_PAGE(pa);
3177 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3179 * Modified by someone
3181 else if (m->dirty || pmap_is_modified(m))
3182 val |= MINCORE_MODIFIED_OTHER;
3187 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3190 * Referenced by someone
3192 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3193 val |= MINCORE_REFERENCED_OTHER;
3194 vm_page_flag_set(m, PG_REFERENCED);
3201 pmap_activate(struct proc *p)
3205 pmap = vmspace_pmap(p->p_vmspace);
3207 pmap->pm_active |= 1 << cpuid;
3209 pmap->pm_active |= 1;
3211 #if defined(SWTCH_OPTIM_STATS)
3214 p->p_thread->td_pcb->pcb_cr3 = vtophys(pmap->pm_pdir);
3215 load_cr3(p->p_thread->td_pcb->pcb_cr3);
3219 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3222 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3226 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3231 #if defined(PMAP_DEBUG)
3232 pmap_pid_dump(int pid)
3238 LIST_FOREACH(p, &allproc, p_list) {
3239 if (p->p_pid != pid)
3245 pmap = vmspace_pmap(p->p_vmspace);
3246 for(i=0;i<1024;i++) {
3249 unsigned base = i << PDRSHIFT;
3251 pde = &pmap->pm_pdir[i];
3252 if (pde && pmap_pde_v(pde)) {
3253 for(j=0;j<1024;j++) {
3254 unsigned va = base + (j << PAGE_SHIFT);
3255 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) {
3262 pte = pmap_pte_quick( pmap, va);
3263 if (pte && pmap_pte_v(pte)) {
3267 m = PHYS_TO_VM_PAGE(pa);
3268 printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x",
3269 va, pa, m->hold_count, m->wire_count, m->flags);
3290 static void pads __P((pmap_t pm));
3291 void pmap_pvdump __P((vm_offset_t pa));
3293 /* print address space of pmap*/
3301 if (pm == kernel_pmap)
3303 for (i = 0; i < 1024; i++)
3305 for (j = 0; j < 1024; j++) {
3306 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
3307 if (pm == kernel_pmap && va < KERNBASE)
3309 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS)
3311 ptep = pmap_pte_quick(pm, va);
3312 if (pmap_pte_v(ptep))
3313 printf("%x:%x ", va, *(int *) ptep);
3322 register pv_entry_t pv;
3325 printf("pa %x", pa);
3326 m = PHYS_TO_VM_PAGE(pa);
3327 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3329 printf(" -> pmap %p, va %x, flags %x",
3330 (void *)pv->pv_pmap, pv->pv_va, pv->pv_flags);
3332 printf(" -> pmap %p, va %x", (void *)pv->pv_pmap, pv->pv_va);