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.6 2003/06/19 06:26:06 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 static pt_entry_t *CMAP2, *ptmmap;
181 caddr_t CADDR1 = 0, ptvmmap = 0;
182 static caddr_t CADDR2;
183 static pt_entry_t *msgbufmap;
184 struct msgbuf *msgbufp=0;
189 static pt_entry_t *pt_crashdumpmap;
190 static caddr_t crashdumpmap;
193 extern pt_entry_t *SMPpt;
195 static pt_entry_t *PMAP1 = 0;
196 static unsigned *PADDR1 = 0;
199 static PMAP_INLINE void free_pv_entry __P((pv_entry_t pv));
200 static unsigned * get_ptbase __P((pmap_t pmap));
201 static pv_entry_t get_pv_entry __P((void));
202 static void i386_protection_init __P((void));
203 static __inline void pmap_changebit __P((vm_page_t m, int bit, boolean_t setem));
205 static void pmap_remove_all __P((vm_page_t m));
206 static vm_page_t pmap_enter_quick __P((pmap_t pmap, vm_offset_t va,
207 vm_page_t m, vm_page_t mpte));
208 static int pmap_remove_pte __P((struct pmap *pmap, unsigned *ptq,
210 static void pmap_remove_page __P((struct pmap *pmap, vm_offset_t va));
211 static int pmap_remove_entry __P((struct pmap *pmap, vm_page_t m,
213 static boolean_t pmap_testbit __P((vm_page_t m, int bit));
214 static void pmap_insert_entry __P((pmap_t pmap, vm_offset_t va,
215 vm_page_t mpte, vm_page_t m));
217 static vm_page_t pmap_allocpte __P((pmap_t pmap, vm_offset_t va));
219 static int pmap_release_free_page __P((pmap_t pmap, vm_page_t p));
220 static vm_page_t _pmap_allocpte __P((pmap_t pmap, unsigned ptepindex));
221 static unsigned * pmap_pte_quick __P((pmap_t pmap, vm_offset_t va));
222 static vm_page_t pmap_page_lookup __P((vm_object_t object, vm_pindex_t pindex));
223 static int pmap_unuse_pt __P((pmap_t, vm_offset_t, vm_page_t));
224 static vm_offset_t pmap_kmem_choose(vm_offset_t addr);
226 static unsigned pdir4mb;
231 * Extract the page table entry associated
232 * with the given map/virtual_address pair.
235 PMAP_INLINE unsigned *
237 register pmap_t pmap;
243 pdeaddr = (unsigned *) pmap_pde(pmap, va);
244 if (*pdeaddr & PG_PS)
247 return get_ptbase(pmap) + i386_btop(va);
254 * Move the kernel virtual free pointer to the next
255 * 4MB. This is used to help improve performance
256 * by using a large (4MB) page for much of the kernel
257 * (.text, .data, .bss)
260 pmap_kmem_choose(vm_offset_t addr)
262 vm_offset_t newaddr = addr;
264 if (cpu_feature & CPUID_PSE) {
265 newaddr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
272 * Bootstrap the system enough to run with virtual memory.
274 * On the i386 this is called after mapping has already been enabled
275 * and just syncs the pmap module with what has already been done.
276 * [We can't call it easily with mapping off since the kernel is not
277 * mapped with PA == VA, hence we would have to relocate every address
278 * from the linked base (virtual) address "KERNBASE" to the actual
279 * (physical) address starting relative to 0]
282 pmap_bootstrap(firstaddr, loadaddr)
283 vm_offset_t firstaddr;
284 vm_offset_t loadaddr;
289 struct globaldata *gd;
293 avail_start = firstaddr;
296 * XXX The calculation of virtual_avail is wrong. It's NKPT*PAGE_SIZE too
297 * large. It should instead be correctly calculated in locore.s and
298 * not based on 'first' (which is a physical address, not a virtual
299 * address, for the start of unused physical memory). The kernel
300 * page tables are NOT double mapped and thus should not be included
301 * in this calculation.
303 virtual_avail = (vm_offset_t) KERNBASE + firstaddr;
304 virtual_avail = pmap_kmem_choose(virtual_avail);
306 virtual_end = VM_MAX_KERNEL_ADDRESS;
309 * Initialize protection array.
311 i386_protection_init();
314 * The kernel's pmap is statically allocated so we don't have to use
315 * pmap_create, which is unlikely to work correctly at this part of
316 * the boot sequence (XXX and which no longer exists).
318 kernel_pmap = &kernel_pmap_store;
320 kernel_pmap->pm_pdir = (pd_entry_t *) (KERNBASE + (u_int)IdlePTD);
321 kernel_pmap->pm_count = 1;
322 kernel_pmap->pm_active = -1; /* don't allow deactivation */
323 TAILQ_INIT(&kernel_pmap->pm_pvlist);
327 * Reserve some special page table entries/VA space for temporary
330 #define SYSMAP(c, p, v, n) \
331 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
334 pte = (pt_entry_t *) pmap_pte(kernel_pmap, va);
337 * CMAP1/CMAP2 are used for zeroing and copying pages.
339 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
340 SYSMAP(caddr_t, CMAP2, CADDR2, 1)
345 SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS);
348 * ptvmmap is used for reading arbitrary physical pages via /dev/mem.
349 * XXX ptmmap is not used.
351 SYSMAP(caddr_t, ptmmap, ptvmmap, 1)
354 * msgbufp is used to map the system message buffer.
355 * XXX msgbufmap is not used.
357 SYSMAP(struct msgbuf *, msgbufmap, msgbufp,
358 atop(round_page(MSGBUF_SIZE)))
362 * ptemap is used for pmap_pte_quick
364 SYSMAP(unsigned *, PMAP1, PADDR1, 1);
369 *(int *) CMAP1 = *(int *) CMAP2 = 0;
370 for (i = 0; i < NKPT; i++)
374 #if !defined(SMP) /* XXX - see also mp_machdep.c */
375 if (cpu_feature & CPUID_PGE) {
381 * Initialize the 4MB page size flag
385 * The 4MB page version of the initial
386 * kernel page mapping.
390 #if !defined(DISABLE_PSE)
391 if (cpu_feature & CPUID_PSE) {
394 * Note that we have enabled PSE mode
397 ptditmp = *((unsigned *)PTmap + i386_btop(KERNBASE));
398 ptditmp &= ~(NBPDR - 1);
399 ptditmp |= PG_V | PG_RW | PG_PS | PG_U | pgeflag;
404 * Enable the PSE mode.
406 load_cr4(rcr4() | CR4_PSE);
409 * We can do the mapping here for the single processor
410 * case. We simply ignore the old page table page from
414 * For SMP, we still need 4K pages to bootstrap APs,
415 * PSE will be enabled as soon as all APs are up.
417 PTD[KPTDI] = (pd_entry_t) ptditmp;
418 kernel_pmap->pm_pdir[KPTDI] = (pd_entry_t) ptditmp;
424 if (cpu_apic_address == 0)
425 panic("pmap_bootstrap: no local apic!");
427 /* local apic is mapped on last page */
428 SMPpt[NPTEPG - 1] = (pt_entry_t)(PG_V | PG_RW | PG_N | pgeflag |
429 (cpu_apic_address & PG_FRAME));
431 /* BSP does this itself, AP's get it pre-set */
432 gd = &SMP_prvspace[0].globaldata;
433 gd->gd_prv_CMAP1 = &SMPpt[1];
434 gd->gd_prv_CMAP2 = &SMPpt[2];
435 gd->gd_prv_CMAP3 = &SMPpt[3];
436 gd->gd_prv_PMAP1 = &SMPpt[4];
437 gd->gd_prv_CADDR1 = SMP_prvspace[0].CPAGE1;
438 gd->gd_prv_CADDR2 = SMP_prvspace[0].CPAGE2;
439 gd->gd_prv_CADDR3 = SMP_prvspace[0].CPAGE3;
440 gd->gd_prv_PADDR1 = (unsigned *)SMP_prvspace[0].PPAGE1;
448 * Set 4mb pdir for mp startup
453 if (pseflag && (cpu_feature & CPUID_PSE)) {
454 load_cr4(rcr4() | CR4_PSE);
455 if (pdir4mb && cpuid == 0) { /* only on BSP */
456 kernel_pmap->pm_pdir[KPTDI] =
457 PTD[KPTDI] = (pd_entry_t)pdir4mb;
465 * Initialize the pmap module.
466 * Called by vm_init, to initialize any structures that the pmap
467 * system needs to map virtual memory.
468 * pmap_init has been enhanced to support in a fairly consistant
469 * way, discontiguous physical memory.
472 pmap_init(phys_start, phys_end)
473 vm_offset_t phys_start, phys_end;
479 * object for kernel page table pages
481 kptobj = vm_object_allocate(OBJT_DEFAULT, NKPDE);
484 * Allocate memory for random pmap data structures. Includes the
488 for(i = 0; i < vm_page_array_size; i++) {
491 m = &vm_page_array[i];
492 TAILQ_INIT(&m->md.pv_list);
493 m->md.pv_list_count = 0;
497 * init the pv free list
499 initial_pvs = vm_page_array_size;
500 if (initial_pvs < MINPV)
502 pvzone = &pvzone_store;
503 pvinit = (struct pv_entry *) kmem_alloc(kernel_map,
504 initial_pvs * sizeof (struct pv_entry));
505 zbootinit(pvzone, "PV ENTRY", sizeof (struct pv_entry), pvinit,
509 * Now it is safe to enable pv_table recording.
511 pmap_initialized = TRUE;
515 * Initialize the address space (zone) for the pv_entries. Set a
516 * high water mark so that the system can recover from excessive
517 * numbers of pv entries.
522 int shpgperproc = PMAP_SHPGPERPROC;
524 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
525 pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
526 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
527 pv_entry_high_water = 9 * (pv_entry_max / 10);
528 zinitna(pvzone, &pvzone_obj, NULL, 0, pv_entry_max, ZONE_INTERRUPT, 1);
532 /***************************************************
533 * Low level helper routines.....
534 ***************************************************/
536 #if defined(PMAP_DIAGNOSTIC)
539 * This code checks for non-writeable/modified pages.
540 * This should be an invalid condition.
543 pmap_nw_modified(pt_entry_t ptea)
549 if ((pte & (PG_M|PG_RW)) == PG_M)
558 * this routine defines the region(s) of memory that should
559 * not be tested for the modified bit.
561 static PMAP_INLINE int
562 pmap_track_modified(vm_offset_t va)
564 if ((va < clean_sva) || (va >= clean_eva))
570 static PMAP_INLINE void
571 invltlb_1pg(vm_offset_t va)
573 #if defined(I386_CPU)
574 if (cpu_class == CPUCLASS_386) {
584 pmap_TLB_invalidate(pmap_t pmap, vm_offset_t va)
587 if (pmap->pm_active & (1 << cpuid))
588 cpu_invlpg((void *)va);
589 if (pmap->pm_active & other_cpus)
598 pmap_TLB_invalidate_all(pmap_t pmap)
601 if (pmap->pm_active & (1 << cpuid))
603 if (pmap->pm_active & other_cpus)
615 unsigned frame = (unsigned) pmap->pm_pdir[PTDPTDI] & PG_FRAME;
617 /* are we current address space or kernel? */
618 if (pmap == kernel_pmap || frame == (((unsigned) PTDpde) & PG_FRAME)) {
619 return (unsigned *) PTmap;
621 /* otherwise, we are alternate address space */
622 if (frame != (((unsigned) APTDpde) & PG_FRAME)) {
623 APTDpde = (pd_entry_t) (frame | PG_RW | PG_V);
625 /* The page directory is not shared between CPUs */
631 return (unsigned *) APTmap;
635 * Super fast pmap_pte routine best used when scanning
636 * the pv lists. This eliminates many coarse-grained
637 * invltlb calls. Note that many of the pv list
638 * scans are across different pmaps. It is very wasteful
639 * to do an entire invltlb for checking a single mapping.
643 pmap_pte_quick(pmap, va)
644 register pmap_t pmap;
648 if ((pde = (unsigned) pmap->pm_pdir[va >> PDRSHIFT]) != 0) {
649 unsigned frame = (unsigned) pmap->pm_pdir[PTDPTDI] & PG_FRAME;
650 unsigned index = i386_btop(va);
651 /* are we current address space or kernel? */
652 if ((pmap == kernel_pmap) ||
653 (frame == (((unsigned) PTDpde) & PG_FRAME))) {
654 return (unsigned *) PTmap + index;
656 newpf = pde & PG_FRAME;
658 if ( ((* (unsigned *) prv_PMAP1) & PG_FRAME) != newpf) {
659 * (unsigned *) prv_PMAP1 = newpf | PG_RW | PG_V;
660 cpu_invlpg(prv_PADDR1);
662 return prv_PADDR1 + ((unsigned) index & (NPTEPG - 1));
664 if ( ((* (unsigned *) PMAP1) & PG_FRAME) != newpf) {
665 * (unsigned *) PMAP1 = newpf | PG_RW | PG_V;
666 invltlb_1pg((vm_offset_t) PADDR1);
668 return PADDR1 + ((unsigned) index & (NPTEPG - 1));
675 * Routine: pmap_extract
677 * Extract the physical page address associated
678 * with the given map/virtual_address pair.
681 pmap_extract(pmap, va)
682 register pmap_t pmap;
686 vm_offset_t pdirindex;
687 pdirindex = va >> PDRSHIFT;
688 if (pmap && (rtval = (unsigned) pmap->pm_pdir[pdirindex])) {
690 if ((rtval & PG_PS) != 0) {
691 rtval &= ~(NBPDR - 1);
692 rtval |= va & (NBPDR - 1);
695 pte = get_ptbase(pmap) + i386_btop(va);
696 rtval = ((*pte & PG_FRAME) | (va & PAGE_MASK));
703 /***************************************************
704 * Low level mapping routines.....
705 ***************************************************/
708 * add a wired page to the kva
709 * note that in order for the mapping to take effect -- you
710 * should do a invltlb after doing the pmap_kenter...
715 register vm_offset_t pa;
717 register unsigned *pte;
720 npte = pa | PG_RW | PG_V | pgeflag;
721 pte = (unsigned *)vtopte(va);
728 * remove a page from the kernel pagetables
734 register unsigned *pte;
736 pte = (unsigned *)vtopte(va);
742 * Used to map a range of physical addresses into kernel
743 * virtual address space.
745 * For now, VM is already on, we only need to map the
749 pmap_map(virt, start, end, prot)
755 while (start < end) {
756 pmap_kenter(virt, start);
765 * Add a list of wired pages to the kva
766 * this routine is only used for temporary
767 * kernel mappings that do not need to have
768 * page modification or references recorded.
769 * Note that old mappings are simply written
770 * over. The page *must* be wired.
773 pmap_qenter(va, m, count)
780 end_va = va + count * PAGE_SIZE;
782 while (va < end_va) {
785 pte = (unsigned *)vtopte(va);
786 *pte = VM_PAGE_TO_PHYS(*m) | PG_RW | PG_V | pgeflag;
788 cpu_invlpg((void *)va);
801 * this routine jerks page mappings from the
802 * kernel -- it is meant only for temporary mappings.
805 pmap_qremove(va, count)
811 end_va = va + count*PAGE_SIZE;
813 while (va < end_va) {
816 pte = (unsigned *)vtopte(va);
819 cpu_invlpg((void *)va);
831 pmap_page_lookup(object, pindex)
837 m = vm_page_lookup(object, pindex);
838 if (m && vm_page_sleep_busy(m, FALSE, "pplookp"))
844 * Create a new thread and optionally associate it with a (new) process.
852 if (mycpu->gd_freethreadcnt > 0) {
853 --mycpu->gd_freethreadcnt;
854 td = TAILQ_FIRST(&mycpu->gd_freethreads);
855 KASSERT(td != NULL, ("unexpected null cache td"));
856 TAILQ_REMOVE(&mycpu->gd_freethreads, td, td_threadq);
858 td = zalloc(thread_zone);
860 (void *)kmem_alloc(kernel_map, UPAGES * PAGE_SIZE);
862 td->td_pcb = (struct pcb *)(td->td_kstack + UPAGES * PAGE_SIZE) - 1;
867 * Dispose of a thread, unlink it from its related proc (if any). Keep
868 * CACHE_NTHREAD threads around for fast-startup.
871 pmap_dispose_thread(struct thread *td)
874 if (mycpu->gd_freethreadcnt < CACHE_NTHREADS) {
875 ++mycpu->gd_freethreadcnt;
876 TAILQ_INSERT_HEAD(&mycpu->gd_freethreads, td, td_threadq);
879 kmem_free(kernel_map,
880 (vm_offset_t)td->td_kstack, UPAGES * PAGE_SIZE);
881 td->td_kstack = NULL;
883 zfree(thread_zone, td);
888 * Create the UPAGES for a new process.
889 * This routine directly affects the fork perf for a process.
892 pmap_new_proc(struct proc *p, struct thread *td)
894 p->p_addr = (void *)td->td_kstack;
897 bzero(p->p_addr, sizeof(*p->p_addr));
904 unsigned *ptek, oldpte;
907 * allocate object for the upages
909 if ((upobj = p->p_upages_obj) == NULL) {
910 upobj = vm_object_allocate( OBJT_DEFAULT, UPAGES);
911 p->p_upages_obj = upobj;
914 /* get a kernel virtual address for the UPAGES for this proc */
915 if ((up = p->p_addr) == NULL) {
916 up = (struct user *) kmem_alloc_nofault(kernel_map,
919 panic("pmap_new_proc: u_map allocation failed");
923 ptek = (unsigned *) vtopte((vm_offset_t) up);
926 for(i=0;i<UPAGES;i++) {
928 * Get a kernel stack page
930 m = vm_page_grab(upobj, i, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
938 oldpte = *(ptek + i);
940 * Enter the page into the kernel address space.
942 *(ptek + i) = VM_PAGE_TO_PHYS(m) | PG_RW | PG_V | pgeflag;
944 if ((oldpte & PG_G) || (cpu_class > CPUCLASS_386)) {
945 invlpg((vm_offset_t) up + i * PAGE_SIZE);
952 vm_page_flag_clear(m, PG_ZERO);
953 vm_page_flag_set(m, PG_MAPPED | PG_WRITEABLE);
954 m->valid = VM_PAGE_BITS_ALL;
962 * Dispose the UPAGES for a process that has exited.
963 * This routine directly impacts the exit perf of a process.
966 pmap_dispose_proc(struct proc *p)
970 if ((td = p->p_thread) != NULL) {
980 unsigned *ptek, oldpte;
982 upobj = p->p_upages_obj;
984 ptek = (unsigned *) vtopte((vm_offset_t) p->p_addr);
985 for(i=0;i<UPAGES;i++) {
987 if ((m = vm_page_lookup(upobj, i)) == NULL)
988 panic("pmap_dispose_proc: upage already missing???");
992 oldpte = *(ptek + i);
994 if ((oldpte & PG_G) || (cpu_class > CPUCLASS_386))
995 invlpg((vm_offset_t) p->p_addr + i * PAGE_SIZE);
996 vm_page_unwire(m, 0);
999 #if defined(I386_CPU)
1000 if (cpu_class <= CPUCLASS_386)
1005 * If the process got swapped out some of its UPAGES might have gotten
1006 * swapped. Just get rid of the object to clean up the swap use
1007 * proactively. NOTE! might block waiting for paging I/O to complete.
1009 if (upobj->type == OBJT_SWAP) {
1010 p->p_upages_obj = NULL;
1011 vm_object_deallocate(upobj);
1017 * Allow the UPAGES for a process to be prejudicially paged out.
1020 pmap_swapout_proc(p)
1028 upobj = p->p_upages_obj;
1030 * let the upages be paged
1032 for(i=0;i<UPAGES;i++) {
1033 if ((m = vm_page_lookup(upobj, i)) == NULL)
1034 panic("pmap_swapout_proc: upage already missing???");
1036 vm_page_unwire(m, 0);
1037 pmap_kremove( (vm_offset_t) p->p_addr + PAGE_SIZE * i);
1043 * Bring the UPAGES for a specified process back in.
1054 upobj = p->p_upages_obj;
1055 for(i=0;i<UPAGES;i++) {
1057 m = vm_page_grab(upobj, i, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1059 pmap_kenter(((vm_offset_t) p->p_addr) + i * PAGE_SIZE,
1060 VM_PAGE_TO_PHYS(m));
1062 if (m->valid != VM_PAGE_BITS_ALL) {
1063 rv = vm_pager_get_pages(upobj, &m, 1, 0);
1064 if (rv != VM_PAGER_OK)
1065 panic("pmap_swapin_proc: cannot get upages for proc: %d\n", p->p_pid);
1066 m = vm_page_lookup(upobj, i);
1067 m->valid = VM_PAGE_BITS_ALL;
1072 vm_page_flag_set(m, PG_MAPPED | PG_WRITEABLE);
1077 /***************************************************
1078 * Page table page management routines.....
1079 ***************************************************/
1082 * This routine unholds page table pages, and if the hold count
1083 * drops to zero, then it decrements the wire count.
1086 _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m) {
1088 while (vm_page_sleep_busy(m, FALSE, "pmuwpt"))
1091 if (m->hold_count == 0) {
1094 * unmap the page table page
1096 pmap->pm_pdir[m->pindex] = 0;
1097 --pmap->pm_stats.resident_count;
1098 if ((((unsigned)pmap->pm_pdir[PTDPTDI]) & PG_FRAME) ==
1099 (((unsigned) PTDpde) & PG_FRAME)) {
1101 * Do a invltlb to make the invalidated mapping
1102 * take effect immediately.
1104 pteva = UPT_MIN_ADDRESS + i386_ptob(m->pindex);
1105 pmap_TLB_invalidate(pmap, pteva);
1108 if (pmap->pm_ptphint == m)
1109 pmap->pm_ptphint = NULL;
1112 * If the page is finally unwired, simply free it.
1115 if (m->wire_count == 0) {
1119 vm_page_free_zero(m);
1127 static PMAP_INLINE int
1128 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
1131 if (m->hold_count == 0)
1132 return _pmap_unwire_pte_hold(pmap, m);
1138 * After removing a page table entry, this routine is used to
1139 * conditionally free the page, and manage the hold/wire counts.
1142 pmap_unuse_pt(pmap, va, mpte)
1148 if (va >= UPT_MIN_ADDRESS)
1152 ptepindex = (va >> PDRSHIFT);
1153 if (pmap->pm_ptphint &&
1154 (pmap->pm_ptphint->pindex == ptepindex)) {
1155 mpte = pmap->pm_ptphint;
1157 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1158 pmap->pm_ptphint = mpte;
1162 return pmap_unwire_pte_hold(pmap, mpte);
1170 (pd_entry_t *)kmem_alloc_pageable(kernel_map, PAGE_SIZE);
1171 pmap_kenter((vm_offset_t) pmap->pm_pdir, (vm_offset_t) IdlePTD);
1173 pmap->pm_active = 0;
1174 pmap->pm_ptphint = NULL;
1175 TAILQ_INIT(&pmap->pm_pvlist);
1176 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1180 * Initialize a preallocated and zeroed pmap structure,
1181 * such as one in a vmspace structure.
1185 register struct pmap *pmap;
1190 * No need to allocate page table space yet but we do need a valid
1191 * page directory table.
1193 if (pmap->pm_pdir == NULL)
1195 (pd_entry_t *)kmem_alloc_pageable(kernel_map, PAGE_SIZE);
1198 * allocate object for the ptes
1200 if (pmap->pm_pteobj == NULL)
1201 pmap->pm_pteobj = vm_object_allocate( OBJT_DEFAULT, PTDPTDI + 1);
1204 * allocate the page directory page
1206 ptdpg = vm_page_grab( pmap->pm_pteobj, PTDPTDI,
1207 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1209 ptdpg->wire_count = 1;
1213 vm_page_flag_clear(ptdpg, PG_MAPPED | PG_BUSY); /* not usually mapped*/
1214 ptdpg->valid = VM_PAGE_BITS_ALL;
1216 pmap_kenter((vm_offset_t) pmap->pm_pdir, VM_PAGE_TO_PHYS(ptdpg));
1217 if ((ptdpg->flags & PG_ZERO) == 0)
1218 bzero(pmap->pm_pdir, PAGE_SIZE);
1221 pmap->pm_pdir[MPPTDI] = PTD[MPPTDI];
1224 /* install self-referential address mapping entry */
1225 *(unsigned *) (pmap->pm_pdir + PTDPTDI) =
1226 VM_PAGE_TO_PHYS(ptdpg) | PG_V | PG_RW | PG_A | PG_M;
1229 pmap->pm_active = 0;
1230 pmap->pm_ptphint = NULL;
1231 TAILQ_INIT(&pmap->pm_pvlist);
1232 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1236 * Wire in kernel global address entries. To avoid a race condition
1237 * between pmap initialization and pmap_growkernel, this procedure
1238 * should be called after the vmspace is attached to the process
1239 * but before this pmap is activated.
1245 /* XXX copies current process, does not fill in MPPTDI */
1246 bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * PTESIZE);
1250 pmap_release_free_page(pmap, p)
1254 unsigned *pde = (unsigned *) pmap->pm_pdir;
1256 * This code optimizes the case of freeing non-busy
1257 * page-table pages. Those pages are zero now, and
1258 * might as well be placed directly into the zero queue.
1260 if (vm_page_sleep_busy(p, FALSE, "pmaprl"))
1266 * Remove the page table page from the processes address space.
1269 pmap->pm_stats.resident_count--;
1271 if (p->hold_count) {
1272 panic("pmap_release: freeing held page table page");
1275 * Page directory pages need to have the kernel
1276 * stuff cleared, so they can go into the zero queue also.
1278 if (p->pindex == PTDPTDI) {
1279 bzero(pde + KPTDI, nkpt * PTESIZE);
1284 pmap_kremove((vm_offset_t) pmap->pm_pdir);
1287 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1288 pmap->pm_ptphint = NULL;
1292 vm_page_free_zero(p);
1297 * this routine is called if the page table page is not
1301 _pmap_allocpte(pmap, ptepindex)
1305 vm_offset_t pteva, ptepa;
1309 * Find or fabricate a new pagetable page
1311 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1312 VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1314 KASSERT(m->queue == PQ_NONE,
1315 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1317 if (m->wire_count == 0)
1322 * Increment the hold count for the page table page
1323 * (denoting a new mapping.)
1328 * Map the pagetable page into the process address space, if
1329 * it isn't already there.
1332 pmap->pm_stats.resident_count++;
1334 ptepa = VM_PAGE_TO_PHYS(m);
1335 pmap->pm_pdir[ptepindex] =
1336 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_A | PG_M);
1339 * Set the page table hint
1341 pmap->pm_ptphint = m;
1344 * Try to use the new mapping, but if we cannot, then
1345 * do it with the routine that maps the page explicitly.
1347 if ((m->flags & PG_ZERO) == 0) {
1348 if ((((unsigned)pmap->pm_pdir[PTDPTDI]) & PG_FRAME) ==
1349 (((unsigned) PTDpde) & PG_FRAME)) {
1350 pteva = UPT_MIN_ADDRESS + i386_ptob(ptepindex);
1351 bzero((caddr_t) pteva, PAGE_SIZE);
1353 pmap_zero_page(ptepa);
1357 m->valid = VM_PAGE_BITS_ALL;
1358 vm_page_flag_clear(m, PG_ZERO);
1359 vm_page_flag_set(m, PG_MAPPED);
1366 pmap_allocpte(pmap, va)
1375 * Calculate pagetable page index
1377 ptepindex = va >> PDRSHIFT;
1380 * Get the page directory entry
1382 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1385 * This supports switching from a 4MB page to a
1388 if (ptepa & PG_PS) {
1389 pmap->pm_pdir[ptepindex] = 0;
1395 * If the page table page is mapped, we just increment the
1396 * hold count, and activate it.
1400 * In order to get the page table page, try the
1403 if (pmap->pm_ptphint &&
1404 (pmap->pm_ptphint->pindex == ptepindex)) {
1405 m = pmap->pm_ptphint;
1407 m = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1408 pmap->pm_ptphint = m;
1414 * Here if the pte page isn't mapped, or if it has been deallocated.
1416 return _pmap_allocpte(pmap, ptepindex);
1420 /***************************************************
1421 * Pmap allocation/deallocation routines.
1422 ***************************************************/
1425 * Release any resources held by the given physical map.
1426 * Called when a pmap initialized by pmap_pinit is being released.
1427 * Should only be called if the map contains no valid mappings.
1431 register struct pmap *pmap;
1433 vm_page_t p,n,ptdpg;
1434 vm_object_t object = pmap->pm_pteobj;
1437 #if defined(DIAGNOSTIC)
1438 if (object->ref_count != 1)
1439 panic("pmap_release: pteobj reference count != 1");
1444 curgeneration = object->generation;
1445 for (p = TAILQ_FIRST(&object->memq); p != NULL; p = n) {
1446 n = TAILQ_NEXT(p, listq);
1447 if (p->pindex == PTDPTDI) {
1452 if (!pmap_release_free_page(pmap, p) &&
1453 (object->generation != curgeneration))
1458 if (ptdpg && !pmap_release_free_page(pmap, ptdpg))
1463 kvm_size(SYSCTL_HANDLER_ARGS)
1465 unsigned long ksize = VM_MAX_KERNEL_ADDRESS - KERNBASE;
1467 return sysctl_handle_long(oidp, &ksize, 0, req);
1469 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD,
1470 0, 0, kvm_size, "IU", "Size of KVM");
1473 kvm_free(SYSCTL_HANDLER_ARGS)
1475 unsigned long kfree = VM_MAX_KERNEL_ADDRESS - kernel_vm_end;
1477 return sysctl_handle_long(oidp, &kfree, 0, req);
1479 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD,
1480 0, 0, kvm_free, "IU", "Amount of KVM free");
1483 * grow the number of kernel page table entries, if needed
1486 pmap_growkernel(vm_offset_t addr)
1491 vm_offset_t ptppaddr;
1496 if (kernel_vm_end == 0) {
1497 kernel_vm_end = KERNBASE;
1499 while (pdir_pde(PTD, kernel_vm_end)) {
1500 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1504 addr = (addr + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1505 while (kernel_vm_end < addr) {
1506 if (pdir_pde(PTD, kernel_vm_end)) {
1507 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1512 * This index is bogus, but out of the way
1514 nkpg = vm_page_alloc(kptobj, nkpt, VM_ALLOC_SYSTEM);
1516 panic("pmap_growkernel: no memory to grow kernel");
1521 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1522 pmap_zero_page(ptppaddr);
1523 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
1524 pdir_pde(PTD, kernel_vm_end) = newpdir;
1526 LIST_FOREACH(p, &allproc, p_list) {
1528 pmap = vmspace_pmap(p->p_vmspace);
1529 *pmap_pde(pmap, kernel_vm_end) = newpdir;
1532 *pmap_pde(kernel_pmap, kernel_vm_end) = newpdir;
1533 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1539 * Retire the given physical map from service.
1540 * Should only be called if the map contains
1541 * no valid mappings.
1545 register pmap_t pmap;
1552 count = --pmap->pm_count;
1555 panic("destroying a pmap is not yet implemented");
1560 * Add a reference to the specified pmap.
1563 pmap_reference(pmap)
1571 /***************************************************
1572 * page management routines.
1573 ***************************************************/
1576 * free the pv_entry back to the free list
1578 static PMAP_INLINE void
1587 * get a new pv_entry, allocating a block from the system
1589 * the memory allocation is performed bypassing the malloc code
1590 * because of the possibility of allocations at interrupt time.
1596 if (pv_entry_high_water &&
1597 (pv_entry_count > pv_entry_high_water) &&
1598 (pmap_pagedaemon_waken == 0)) {
1599 pmap_pagedaemon_waken = 1;
1600 wakeup (&vm_pages_needed);
1602 return zalloci(pvzone);
1606 * This routine is very drastic, but can save the system
1614 static int warningdone=0;
1616 if (pmap_pagedaemon_waken == 0)
1619 if (warningdone < 5) {
1620 printf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1624 for(i = 0; i < vm_page_array_size; i++) {
1625 m = &vm_page_array[i];
1626 if (m->wire_count || m->hold_count || m->busy ||
1627 (m->flags & PG_BUSY))
1631 pmap_pagedaemon_waken = 0;
1636 * If it is the first entry on the list, it is actually
1637 * in the header and we must copy the following entry up
1638 * to the header. Otherwise we must search the list for
1639 * the entry. In either case we free the now unused entry.
1643 pmap_remove_entry(pmap, m, va)
1653 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1654 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1655 if (pmap == pv->pv_pmap && va == pv->pv_va)
1659 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1660 if (va == pv->pv_va)
1668 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem);
1669 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1670 m->md.pv_list_count--;
1671 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
1672 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1674 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1683 * Create a pv entry for page at pa for
1687 pmap_insert_entry(pmap, va, mpte, m)
1698 pv = get_pv_entry();
1703 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1704 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1705 m->md.pv_list_count++;
1711 * pmap_remove_pte: do the things to unmap a page in a process
1714 pmap_remove_pte(pmap, ptq, va)
1722 oldpte = loadandclear(ptq);
1724 pmap->pm_stats.wired_count -= 1;
1726 * Machines that don't support invlpg, also don't support
1731 pmap->pm_stats.resident_count -= 1;
1732 if (oldpte & PG_MANAGED) {
1733 m = PHYS_TO_VM_PAGE(oldpte);
1734 if (oldpte & PG_M) {
1735 #if defined(PMAP_DIAGNOSTIC)
1736 if (pmap_nw_modified((pt_entry_t) oldpte)) {
1738 "pmap_remove: modified page not writable: va: 0x%x, pte: 0x%x\n",
1742 if (pmap_track_modified(va))
1746 vm_page_flag_set(m, PG_REFERENCED);
1747 return pmap_remove_entry(pmap, m, va);
1749 return pmap_unuse_pt(pmap, va, NULL);
1756 * Remove a single page from a process address space
1759 pmap_remove_page(pmap, va)
1761 register vm_offset_t va;
1763 register unsigned *ptq;
1766 * if there is no pte for this address, just skip it!!!
1768 if (*pmap_pde(pmap, va) == 0) {
1773 * get a local va for mappings for this pmap.
1775 ptq = get_ptbase(pmap) + i386_btop(va);
1777 (void) pmap_remove_pte(pmap, ptq, va);
1778 pmap_TLB_invalidate(pmap, va);
1784 * Remove the given range of addresses from the specified map.
1786 * It is assumed that the start and end are properly
1787 * rounded to the page size.
1790 pmap_remove(pmap, sva, eva)
1792 register vm_offset_t sva;
1793 register vm_offset_t eva;
1795 register unsigned *ptbase;
1797 vm_offset_t ptpaddr;
1798 vm_offset_t sindex, eindex;
1804 if (pmap->pm_stats.resident_count == 0)
1808 * special handling of removing one page. a very
1809 * common operation and easy to short circuit some
1812 if (((sva + PAGE_SIZE) == eva) &&
1813 (((unsigned) pmap->pm_pdir[(sva >> PDRSHIFT)] & PG_PS) == 0)) {
1814 pmap_remove_page(pmap, sva);
1821 * Get a local virtual address for the mappings that are being
1824 ptbase = get_ptbase(pmap);
1826 sindex = i386_btop(sva);
1827 eindex = i386_btop(eva);
1829 for (; sindex < eindex; sindex = pdnxt) {
1833 * Calculate index for next page table.
1835 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1836 if (pmap->pm_stats.resident_count == 0)
1839 pdirindex = sindex / NPDEPG;
1840 if (((ptpaddr = (unsigned) pmap->pm_pdir[pdirindex]) & PG_PS) != 0) {
1841 pmap->pm_pdir[pdirindex] = 0;
1842 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1848 * Weed out invalid mappings. Note: we assume that the page
1849 * directory table is always allocated, and in kernel virtual.
1855 * Limit our scan to either the end of the va represented
1856 * by the current page table page, or to the end of the
1857 * range being removed.
1859 if (pdnxt > eindex) {
1863 for ( ;sindex != pdnxt; sindex++) {
1865 if (ptbase[sindex] == 0) {
1868 va = i386_ptob(sindex);
1871 if (pmap_remove_pte(pmap,
1872 ptbase + sindex, va))
1878 pmap_TLB_invalidate_all(pmap);
1882 * Routine: pmap_remove_all
1884 * Removes this physical page from
1885 * all physical maps in which it resides.
1886 * Reflects back modify bits to the pager.
1889 * Original versions of this routine were very
1890 * inefficient because they iteratively called
1891 * pmap_remove (slow...)
1898 register pv_entry_t pv;
1899 register unsigned *pte, tpte;
1902 #if defined(PMAP_DIAGNOSTIC)
1904 * XXX this makes pmap_page_protect(NONE) illegal for non-managed
1907 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
1908 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%x", VM_PAGE_TO_PHYS(m));
1913 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
1914 pv->pv_pmap->pm_stats.resident_count--;
1916 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
1918 tpte = loadandclear(pte);
1920 pv->pv_pmap->pm_stats.wired_count--;
1923 vm_page_flag_set(m, PG_REFERENCED);
1926 * Update the vm_page_t clean and reference bits.
1929 #if defined(PMAP_DIAGNOSTIC)
1930 if (pmap_nw_modified((pt_entry_t) tpte)) {
1932 "pmap_remove_all: modified page not writable: va: 0x%x, pte: 0x%x\n",
1936 if (pmap_track_modified(pv->pv_va))
1939 pmap_TLB_invalidate(pv->pv_pmap, pv->pv_va);
1941 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
1942 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1943 m->md.pv_list_count--;
1944 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
1948 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1954 * Set the physical protection on the
1955 * specified range of this map as requested.
1958 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
1960 register unsigned *ptbase;
1961 vm_offset_t pdnxt, ptpaddr;
1962 vm_pindex_t sindex, eindex;
1968 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1969 pmap_remove(pmap, sva, eva);
1973 if (prot & VM_PROT_WRITE)
1978 ptbase = get_ptbase(pmap);
1980 sindex = i386_btop(sva);
1981 eindex = i386_btop(eva);
1983 for (; sindex < eindex; sindex = pdnxt) {
1987 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1989 pdirindex = sindex / NPDEPG;
1990 if (((ptpaddr = (unsigned) pmap->pm_pdir[pdirindex]) & PG_PS) != 0) {
1991 (unsigned) pmap->pm_pdir[pdirindex] &= ~(PG_M|PG_RW);
1992 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1998 * Weed out invalid mappings. Note: we assume that the page
1999 * directory table is always allocated, and in kernel virtual.
2004 if (pdnxt > eindex) {
2008 for (; sindex != pdnxt; sindex++) {
2013 pbits = ptbase[sindex];
2015 if (pbits & PG_MANAGED) {
2018 m = PHYS_TO_VM_PAGE(pbits);
2019 vm_page_flag_set(m, PG_REFERENCED);
2023 if (pmap_track_modified(i386_ptob(sindex))) {
2025 m = PHYS_TO_VM_PAGE(pbits);
2034 if (pbits != ptbase[sindex]) {
2035 ptbase[sindex] = pbits;
2041 pmap_TLB_invalidate_all(pmap);
2045 * Insert the given physical page (p) at
2046 * the specified virtual address (v) in the
2047 * target physical map with the protection requested.
2049 * If specified, the page will be wired down, meaning
2050 * that the related pte can not be reclaimed.
2052 * NB: This is the only routine which MAY NOT lazy-evaluate
2053 * or lose information. That is, this routine must actually
2054 * insert this page into the given map NOW.
2057 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
2061 register unsigned *pte;
2063 vm_offset_t origpte, newpte;
2070 #ifdef PMAP_DIAGNOSTIC
2071 if (va > VM_MAX_KERNEL_ADDRESS)
2072 panic("pmap_enter: toobig");
2073 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
2074 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%x)", va);
2079 * In the case that a page table page is not
2080 * resident, we are creating it here.
2082 if (va < UPT_MIN_ADDRESS) {
2083 mpte = pmap_allocpte(pmap, va);
2085 #if 0 && defined(PMAP_DIAGNOSTIC)
2087 vm_offset_t *pdeaddr = (vm_offset_t *)pmap_pde(pmap, va);
2088 if (((origpte = (vm_offset_t) *pdeaddr) & PG_V) == 0) {
2089 panic("pmap_enter: invalid kernel page table page(0), pdir=%p, pde=%p, va=%p\n",
2090 pmap->pm_pdir[PTDPTDI], origpte, va);
2093 pdeaddr = (vm_offset_t *) IdlePTDS[cpuid];
2094 if (((newpte = pdeaddr[va >> PDRSHIFT]) & PG_V) == 0) {
2095 if ((vm_offset_t) my_idlePTD != (vm_offset_t) vtophys(pdeaddr))
2096 printf("pde mismatch: %x, %x\n", my_idlePTD, pdeaddr);
2097 printf("cpuid: %d, pdeaddr: 0x%x\n", cpuid, pdeaddr);
2098 panic("pmap_enter: invalid kernel page table page(1), pdir=%p, npde=%p, pde=%p, va=%p\n",
2099 pmap->pm_pdir[PTDPTDI], newpte, origpte, va);
2105 pte = pmap_pte(pmap, va);
2108 * Page Directory table entry not valid, we need a new PT page
2111 panic("pmap_enter: invalid page directory, pdir=%p, va=0x%x\n",
2112 (void *)pmap->pm_pdir[PTDPTDI], va);
2115 pa = VM_PAGE_TO_PHYS(m) & PG_FRAME;
2116 origpte = *(vm_offset_t *)pte;
2117 opa = origpte & PG_FRAME;
2119 if (origpte & PG_PS)
2120 panic("pmap_enter: attempted pmap_enter on 4MB page");
2123 * Mapping has not changed, must be protection or wiring change.
2125 if (origpte && (opa == pa)) {
2127 * Wiring change, just update stats. We don't worry about
2128 * wiring PT pages as they remain resident as long as there
2129 * are valid mappings in them. Hence, if a user page is wired,
2130 * the PT page will be also.
2132 if (wired && ((origpte & PG_W) == 0))
2133 pmap->pm_stats.wired_count++;
2134 else if (!wired && (origpte & PG_W))
2135 pmap->pm_stats.wired_count--;
2137 #if defined(PMAP_DIAGNOSTIC)
2138 if (pmap_nw_modified((pt_entry_t) origpte)) {
2140 "pmap_enter: modified page not writable: va: 0x%x, pte: 0x%x\n",
2146 * Remove extra pte reference
2151 if ((prot & VM_PROT_WRITE) && (origpte & PG_V)) {
2152 if ((origpte & PG_RW) == 0) {
2155 cpu_invlpg((void *)va);
2156 if (pmap->pm_active & other_cpus)
2166 * We might be turning off write access to the page,
2167 * so we go ahead and sense modify status.
2169 if (origpte & PG_MANAGED) {
2170 if ((origpte & PG_M) && pmap_track_modified(va)) {
2172 om = PHYS_TO_VM_PAGE(opa);
2180 * Mapping has changed, invalidate old range and fall through to
2181 * handle validating new mapping.
2185 err = pmap_remove_pte(pmap, pte, va);
2187 panic("pmap_enter: pte vanished, va: 0x%x", va);
2191 * Enter on the PV list if part of our managed memory. Note that we
2192 * raise IPL while manipulating pv_table since pmap_enter can be
2193 * called at interrupt time.
2195 if (pmap_initialized &&
2196 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2197 pmap_insert_entry(pmap, va, mpte, m);
2202 * Increment counters
2204 pmap->pm_stats.resident_count++;
2206 pmap->pm_stats.wired_count++;
2210 * Now validate mapping with desired protection/wiring.
2212 newpte = (vm_offset_t) (pa | pte_prot(pmap, prot) | PG_V);
2216 if (va < UPT_MIN_ADDRESS)
2218 if (pmap == kernel_pmap)
2222 * if the mapping or permission bits are different, we need
2223 * to update the pte.
2225 if ((origpte & ~(PG_M|PG_A)) != newpte) {
2226 *pte = newpte | PG_A;
2229 cpu_invlpg((void *)va);
2230 if (pmap->pm_active & other_cpus)
2240 * this code makes some *MAJOR* assumptions:
2241 * 1. Current pmap & pmap exists.
2244 * 4. No page table pages.
2245 * 5. Tlbflush is deferred to calling procedure.
2246 * 6. Page IS managed.
2247 * but is *MUCH* faster than pmap_enter...
2251 pmap_enter_quick(pmap, va, m, mpte)
2252 register pmap_t pmap;
2261 * In the case that a page table page is not
2262 * resident, we are creating it here.
2264 if (va < UPT_MIN_ADDRESS) {
2269 * Calculate pagetable page index
2271 ptepindex = va >> PDRSHIFT;
2272 if (mpte && (mpte->pindex == ptepindex)) {
2277 * Get the page directory entry
2279 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
2282 * If the page table page is mapped, we just increment
2283 * the hold count, and activate it.
2287 panic("pmap_enter_quick: unexpected mapping into 4MB page");
2288 if (pmap->pm_ptphint &&
2289 (pmap->pm_ptphint->pindex == ptepindex)) {
2290 mpte = pmap->pm_ptphint;
2292 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
2293 pmap->pm_ptphint = mpte;
2299 mpte = _pmap_allocpte(pmap, ptepindex);
2307 * This call to vtopte makes the assumption that we are
2308 * entering the page into the current pmap. In order to support
2309 * quick entry into any pmap, one would likely use pmap_pte_quick.
2310 * But that isn't as quick as vtopte.
2312 pte = (unsigned *)vtopte(va);
2315 pmap_unwire_pte_hold(pmap, mpte);
2320 * Enter on the PV list if part of our managed memory. Note that we
2321 * raise IPL while manipulating pv_table since pmap_enter can be
2322 * called at interrupt time.
2324 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0)
2325 pmap_insert_entry(pmap, va, mpte, m);
2328 * Increment counters
2330 pmap->pm_stats.resident_count++;
2332 pa = VM_PAGE_TO_PHYS(m);
2335 * Now validate mapping with RO protection
2337 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2338 *pte = pa | PG_V | PG_U;
2340 *pte = pa | PG_V | PG_U | PG_MANAGED;
2346 * Make a temporary mapping for a physical address. This is only intended
2347 * to be used for panic dumps.
2350 pmap_kenter_temporary(vm_offset_t pa, int i)
2352 pmap_kenter((vm_offset_t)crashdumpmap + (i * PAGE_SIZE), pa);
2353 return ((void *)crashdumpmap);
2356 #define MAX_INIT_PT (96)
2358 * pmap_object_init_pt preloads the ptes for a given object
2359 * into the specified pmap. This eliminates the blast of soft
2360 * faults on process startup and immediately after an mmap.
2363 pmap_object_init_pt(pmap, addr, object, pindex, size, limit)
2376 if (pmap == NULL || object == NULL)
2380 * This code maps large physical mmap regions into the
2381 * processor address space. Note that some shortcuts
2382 * are taken, but the code works.
2385 (object->type == OBJT_DEVICE) &&
2386 ((addr & (NBPDR - 1)) == 0) &&
2387 ((size & (NBPDR - 1)) == 0) ) {
2390 unsigned int ptepindex;
2394 if (pmap->pm_pdir[ptepindex = (addr >> PDRSHIFT)])
2398 p = vm_page_lookup(object, pindex);
2399 if (p && vm_page_sleep_busy(p, FALSE, "init4p"))
2403 p = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL);
2408 if (vm_pager_get_pages(object, m, 1, 0) != VM_PAGER_OK) {
2413 p = vm_page_lookup(object, pindex);
2417 ptepa = (vm_offset_t) VM_PAGE_TO_PHYS(p);
2418 if (ptepa & (NBPDR - 1)) {
2422 p->valid = VM_PAGE_BITS_ALL;
2424 pmap->pm_stats.resident_count += size >> PAGE_SHIFT;
2425 npdes = size >> PDRSHIFT;
2426 for(i=0;i<npdes;i++) {
2427 pmap->pm_pdir[ptepindex] =
2428 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_PS);
2432 vm_page_flag_set(p, PG_MAPPED);
2437 psize = i386_btop(size);
2439 if ((object->type != OBJT_VNODE) ||
2440 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2441 (object->resident_page_count > MAX_INIT_PT))) {
2445 if (psize + pindex > object->size) {
2446 if (object->size < pindex)
2448 psize = object->size - pindex;
2453 * if we are processing a major portion of the object, then scan the
2456 if (psize > (object->resident_page_count >> 2)) {
2459 for (p = TAILQ_FIRST(&object->memq);
2460 ((objpgs > 0) && (p != NULL));
2461 p = TAILQ_NEXT(p, listq)) {
2464 if (tmpidx < pindex) {
2468 if (tmpidx >= psize) {
2472 * don't allow an madvise to blow away our really
2473 * free pages allocating pv entries.
2475 if ((limit & MAP_PREFAULT_MADVISE) &&
2476 cnt.v_free_count < cnt.v_free_reserved) {
2479 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2481 (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2482 if ((p->queue - p->pc) == PQ_CACHE)
2483 vm_page_deactivate(p);
2485 mpte = pmap_enter_quick(pmap,
2486 addr + i386_ptob(tmpidx), p, mpte);
2487 vm_page_flag_set(p, PG_MAPPED);
2494 * else lookup the pages one-by-one.
2496 for (tmpidx = 0; tmpidx < psize; tmpidx += 1) {
2498 * don't allow an madvise to blow away our really
2499 * free pages allocating pv entries.
2501 if ((limit & MAP_PREFAULT_MADVISE) &&
2502 cnt.v_free_count < cnt.v_free_reserved) {
2505 p = vm_page_lookup(object, tmpidx + pindex);
2507 ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2509 (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2510 if ((p->queue - p->pc) == PQ_CACHE)
2511 vm_page_deactivate(p);
2513 mpte = pmap_enter_quick(pmap,
2514 addr + i386_ptob(tmpidx), p, mpte);
2515 vm_page_flag_set(p, PG_MAPPED);
2524 * pmap_prefault provides a quick way of clustering
2525 * pagefaults into a processes address space. It is a "cousin"
2526 * of pmap_object_init_pt, except it runs at page fault time instead
2531 #define PAGEORDER_SIZE (PFBAK+PFFOR)
2533 static int pmap_prefault_pageorder[] = {
2534 -PAGE_SIZE, PAGE_SIZE,
2535 -2 * PAGE_SIZE, 2 * PAGE_SIZE,
2536 -3 * PAGE_SIZE, 3 * PAGE_SIZE
2537 -4 * PAGE_SIZE, 4 * PAGE_SIZE
2541 pmap_prefault(pmap, addra, entry)
2544 vm_map_entry_t entry;
2553 if (!curproc || (pmap != vmspace_pmap(curproc->p_vmspace)))
2556 object = entry->object.vm_object;
2558 starta = addra - PFBAK * PAGE_SIZE;
2559 if (starta < entry->start) {
2560 starta = entry->start;
2561 } else if (starta > addra) {
2566 for (i = 0; i < PAGEORDER_SIZE; i++) {
2567 vm_object_t lobject;
2570 addr = addra + pmap_prefault_pageorder[i];
2571 if (addr > addra + (PFFOR * PAGE_SIZE))
2574 if (addr < starta || addr >= entry->end)
2577 if ((*pmap_pde(pmap, addr)) == NULL)
2580 pte = (unsigned *) vtopte(addr);
2584 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2586 for (m = vm_page_lookup(lobject, pindex);
2587 (!m && (lobject->type == OBJT_DEFAULT) && (lobject->backing_object));
2588 lobject = lobject->backing_object) {
2589 if (lobject->backing_object_offset & PAGE_MASK)
2591 pindex += (lobject->backing_object_offset >> PAGE_SHIFT);
2592 m = vm_page_lookup(lobject->backing_object, pindex);
2596 * give-up when a page is not in memory
2601 if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2603 (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2605 if ((m->queue - m->pc) == PQ_CACHE) {
2606 vm_page_deactivate(m);
2609 mpte = pmap_enter_quick(pmap, addr, m, mpte);
2610 vm_page_flag_set(m, PG_MAPPED);
2617 * Routine: pmap_change_wiring
2618 * Function: Change the wiring attribute for a map/virtual-address
2620 * In/out conditions:
2621 * The mapping must already exist in the pmap.
2624 pmap_change_wiring(pmap, va, wired)
2625 register pmap_t pmap;
2629 register unsigned *pte;
2634 pte = pmap_pte(pmap, va);
2636 if (wired && !pmap_pte_w(pte))
2637 pmap->pm_stats.wired_count++;
2638 else if (!wired && pmap_pte_w(pte))
2639 pmap->pm_stats.wired_count--;
2642 * Wiring is not a hardware characteristic so there is no need to
2645 pmap_pte_set_w(pte, wired);
2651 * Copy the range specified by src_addr/len
2652 * from the source map to the range dst_addr/len
2653 * in the destination map.
2655 * This routine is only advisory and need not do anything.
2659 pmap_copy(dst_pmap, src_pmap, dst_addr, len, src_addr)
2660 pmap_t dst_pmap, src_pmap;
2661 vm_offset_t dst_addr;
2663 vm_offset_t src_addr;
2666 vm_offset_t end_addr = src_addr + len;
2668 unsigned src_frame, dst_frame;
2671 if (dst_addr != src_addr)
2674 src_frame = ((unsigned) src_pmap->pm_pdir[PTDPTDI]) & PG_FRAME;
2675 if (src_frame != (((unsigned) PTDpde) & PG_FRAME)) {
2679 dst_frame = ((unsigned) dst_pmap->pm_pdir[PTDPTDI]) & PG_FRAME;
2680 if (dst_frame != (((unsigned) APTDpde) & PG_FRAME)) {
2681 APTDpde = (pd_entry_t) (dst_frame | PG_RW | PG_V);
2683 /* The page directory is not shared between CPUs */
2690 for(addr = src_addr; addr < end_addr; addr = pdnxt) {
2691 unsigned *src_pte, *dst_pte;
2692 vm_page_t dstmpte, srcmpte;
2693 vm_offset_t srcptepaddr;
2696 if (addr >= UPT_MIN_ADDRESS)
2697 panic("pmap_copy: invalid to pmap_copy page tables\n");
2700 * Don't let optional prefaulting of pages make us go
2701 * way below the low water mark of free pages or way
2702 * above high water mark of used pv entries.
2704 if (cnt.v_free_count < cnt.v_free_reserved ||
2705 pv_entry_count > pv_entry_high_water)
2708 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
2709 ptepindex = addr >> PDRSHIFT;
2711 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex];
2712 if (srcptepaddr == 0)
2715 if (srcptepaddr & PG_PS) {
2716 if (dst_pmap->pm_pdir[ptepindex] == 0) {
2717 dst_pmap->pm_pdir[ptepindex] = (pd_entry_t) srcptepaddr;
2718 dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
2723 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
2724 if ((srcmpte == NULL) ||
2725 (srcmpte->hold_count == 0) || (srcmpte->flags & PG_BUSY))
2728 if (pdnxt > end_addr)
2731 src_pte = (unsigned *) vtopte(addr);
2732 dst_pte = (unsigned *) avtopte(addr);
2733 while (addr < pdnxt) {
2737 * we only virtual copy managed pages
2739 if ((ptetemp & PG_MANAGED) != 0) {
2741 * We have to check after allocpte for the
2742 * pte still being around... allocpte can
2745 dstmpte = pmap_allocpte(dst_pmap, addr);
2746 if ((*dst_pte == 0) && (ptetemp = *src_pte)) {
2748 * Clear the modified and
2749 * accessed (referenced) bits
2752 m = PHYS_TO_VM_PAGE(ptetemp);
2753 *dst_pte = ptetemp & ~(PG_M | PG_A);
2754 dst_pmap->pm_stats.resident_count++;
2755 pmap_insert_entry(dst_pmap, addr,
2758 pmap_unwire_pte_hold(dst_pmap, dstmpte);
2760 if (dstmpte->hold_count >= srcmpte->hold_count)
2771 * Routine: pmap_kernel
2773 * Returns the physical map handle for the kernel.
2778 return (kernel_pmap);
2782 * pmap_zero_page zeros the specified hardware page by mapping
2783 * the page into KVM and using bzero to clear its contents.
2786 pmap_zero_page(phys)
2790 if (*(int *) prv_CMAP3)
2791 panic("pmap_zero_page: prv_CMAP3 busy");
2793 *(int *) prv_CMAP3 = PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2794 cpu_invlpg(prv_CADDR3);
2796 #if defined(I686_CPU)
2797 if (cpu_class == CPUCLASS_686)
2798 i686_pagezero(prv_CADDR3);
2801 bzero(prv_CADDR3, PAGE_SIZE);
2803 *(int *) prv_CMAP3 = 0;
2806 panic("pmap_zero_page: CMAP2 busy");
2808 *(int *) CMAP2 = PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2809 invltlb_1pg((vm_offset_t)CADDR2);
2811 #if defined(I686_CPU)
2812 if (cpu_class == CPUCLASS_686)
2813 i686_pagezero(CADDR2);
2816 bzero(CADDR2, PAGE_SIZE);
2822 * pmap_zero_page_area zeros the specified hardware page by mapping
2823 * the page into KVM and using bzero to clear its contents.
2825 * off and size may not cover an area beyond a single hardware page.
2828 pmap_zero_page_area(phys, off, size)
2834 if (*(int *) prv_CMAP3)
2835 panic("pmap_zero_page: prv_CMAP3 busy");
2837 *(int *) prv_CMAP3 = PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2838 cpu_invlpg(prv_CADDR3);
2840 #if defined(I686_CPU)
2841 if (cpu_class == CPUCLASS_686 && off == 0 && size == PAGE_SIZE)
2842 i686_pagezero(prv_CADDR3);
2845 bzero((char *)prv_CADDR3 + off, size);
2847 *(int *) prv_CMAP3 = 0;
2850 panic("pmap_zero_page: CMAP2 busy");
2852 *(int *) CMAP2 = PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2853 invltlb_1pg((vm_offset_t)CADDR2);
2855 #if defined(I686_CPU)
2856 if (cpu_class == CPUCLASS_686 && off == 0 && size == PAGE_SIZE)
2857 i686_pagezero(CADDR2);
2860 bzero((char *)CADDR2 + off, size);
2866 * pmap_copy_page copies the specified (machine independent)
2867 * page by mapping the page into virtual memory and using
2868 * bcopy to copy the page, one machine dependent page at a
2872 pmap_copy_page(src, dst)
2877 if (*(int *) prv_CMAP1)
2878 panic("pmap_copy_page: prv_CMAP1 busy");
2879 if (*(int *) prv_CMAP2)
2880 panic("pmap_copy_page: prv_CMAP2 busy");
2882 *(int *) prv_CMAP1 = PG_V | (src & PG_FRAME) | PG_A;
2883 *(int *) prv_CMAP2 = PG_V | PG_RW | (dst & PG_FRAME) | PG_A | PG_M;
2885 cpu_invlpg(prv_CADDR1);
2886 cpu_invlpg(prv_CADDR2);
2888 bcopy(prv_CADDR1, prv_CADDR2, PAGE_SIZE);
2890 *(int *) prv_CMAP1 = 0;
2891 *(int *) prv_CMAP2 = 0;
2893 if (*(int *) CMAP1 || *(int *) CMAP2)
2894 panic("pmap_copy_page: CMAP busy");
2896 *(int *) CMAP1 = PG_V | (src & PG_FRAME) | PG_A;
2897 *(int *) CMAP2 = PG_V | PG_RW | (dst & PG_FRAME) | PG_A | PG_M;
2898 #if defined(I386_CPU)
2899 if (cpu_class == CPUCLASS_386) {
2904 invlpg((u_int)CADDR1);
2905 invlpg((u_int)CADDR2);
2908 bcopy(CADDR1, CADDR2, PAGE_SIZE);
2917 * Routine: pmap_pageable
2919 * Make the specified pages (by pmap, offset)
2920 * pageable (or not) as requested.
2922 * A page which is not pageable may not take
2923 * a fault; therefore, its page table entry
2924 * must remain valid for the duration.
2926 * This routine is merely advisory; pmap_enter
2927 * will specify that these pages are to be wired
2928 * down (or not) as appropriate.
2931 pmap_pageable(pmap, sva, eva, pageable)
2933 vm_offset_t sva, eva;
2939 * Returns true if the pmap's pv is one of the first
2940 * 16 pvs linked to from this page. This count may
2941 * be changed upwards or downwards in the future; it
2942 * is only necessary that true be returned for a small
2943 * subset of pmaps for proper page aging.
2946 pmap_page_exists_quick(pmap, m)
2954 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2959 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2960 if (pv->pv_pmap == pmap) {
2972 #define PMAP_REMOVE_PAGES_CURPROC_ONLY
2974 * Remove all pages from specified address space
2975 * this aids process exit speeds. Also, this code
2976 * is special cased for current process only, but
2977 * can have the more generic (and slightly slower)
2978 * mode enabled. This is much faster than pmap_remove
2979 * in the case of running down an entire address space.
2982 pmap_remove_pages(pmap, sva, eva)
2984 vm_offset_t sva, eva;
2986 unsigned *pte, tpte;
2991 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2992 if (!curproc || (pmap != vmspace_pmap(curproc->p_vmspace))) {
2993 printf("warning: pmap_remove_pages called with non-current pmap\n");
2999 for(pv = TAILQ_FIRST(&pmap->pm_pvlist);
3003 if (pv->pv_va >= eva || pv->pv_va < sva) {
3004 npv = TAILQ_NEXT(pv, pv_plist);
3008 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
3009 pte = (unsigned *)vtopte(pv->pv_va);
3011 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3016 * We cannot remove wired pages from a process' mapping at this time
3019 npv = TAILQ_NEXT(pv, pv_plist);
3024 m = PHYS_TO_VM_PAGE(tpte);
3026 KASSERT(m < &vm_page_array[vm_page_array_size],
3027 ("pmap_remove_pages: bad tpte %x", tpte));
3029 pv->pv_pmap->pm_stats.resident_count--;
3032 * Update the vm_page_t clean and reference bits.
3039 npv = TAILQ_NEXT(pv, pv_plist);
3040 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
3042 m->md.pv_list_count--;
3043 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3044 if (TAILQ_FIRST(&m->md.pv_list) == NULL) {
3045 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
3048 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
3052 pmap_TLB_invalidate_all(pmap);
3056 * pmap_testbit tests bits in pte's
3057 * note that the testbit/changebit routines are inline,
3058 * and a lot of things compile-time evaluate.
3061 pmap_testbit(m, bit)
3069 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3072 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
3077 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3079 * if the bit being tested is the modified bit, then
3080 * mark clean_map and ptes as never
3083 if (bit & (PG_A|PG_M)) {
3084 if (!pmap_track_modified(pv->pv_va))
3088 #if defined(PMAP_DIAGNOSTIC)
3090 printf("Null pmap (tb) at va: 0x%x\n", pv->pv_va);
3094 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3105 * this routine is used to modify bits in ptes
3107 static __inline void
3108 pmap_changebit(m, bit, setem)
3113 register pv_entry_t pv;
3114 register unsigned *pte;
3117 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3123 * Loop over all current mappings setting/clearing as appropos If
3124 * setting RO do we need to clear the VAC?
3126 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3128 * don't write protect pager mappings
3130 if (!setem && (bit == PG_RW)) {
3131 if (!pmap_track_modified(pv->pv_va))
3135 #if defined(PMAP_DIAGNOSTIC)
3137 printf("Null pmap (cb) at va: 0x%x\n", pv->pv_va);
3142 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3146 pmap_TLB_invalidate(pv->pv_pmap, pv->pv_va);
3148 vm_offset_t pbits = *(vm_offset_t *)pte;
3154 *(int *)pte = pbits & ~(PG_M|PG_RW);
3156 *(int *)pte = pbits & ~bit;
3158 pmap_TLB_invalidate(pv->pv_pmap, pv->pv_va);
3166 * pmap_page_protect:
3168 * Lower the permission for all mappings to a given page.
3171 pmap_page_protect(vm_page_t m, vm_prot_t prot)
3173 if ((prot & VM_PROT_WRITE) == 0) {
3174 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
3175 pmap_changebit(m, PG_RW, FALSE);
3183 pmap_phys_address(ppn)
3186 return (i386_ptob(ppn));
3190 * pmap_ts_referenced:
3192 * Return a count of reference bits for a page, clearing those bits.
3193 * It is not necessary for every reference bit to be cleared, but it
3194 * is necessary that 0 only be returned when there are truly no
3195 * reference bits set.
3197 * XXX: The exact number of bits to check and clear is a matter that
3198 * should be tested and standardized at some point in the future for
3199 * optimal aging of shared pages.
3202 pmap_ts_referenced(vm_page_t m)
3204 register pv_entry_t pv, pvf, pvn;
3209 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3214 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3219 pvn = TAILQ_NEXT(pv, pv_list);
3221 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3223 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3225 if (!pmap_track_modified(pv->pv_va))
3228 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3230 if (pte && (*pte & PG_A)) {
3233 pmap_TLB_invalidate(pv->pv_pmap, pv->pv_va);
3240 } while ((pv = pvn) != NULL && pv != pvf);
3250 * Return whether or not the specified physical page was modified
3251 * in any physical maps.
3254 pmap_is_modified(vm_page_t m)
3256 return pmap_testbit(m, PG_M);
3260 * Clear the modify bits on the specified physical page.
3263 pmap_clear_modify(vm_page_t m)
3265 pmap_changebit(m, PG_M, FALSE);
3269 * pmap_clear_reference:
3271 * Clear the reference bit on the specified physical page.
3274 pmap_clear_reference(vm_page_t m)
3276 pmap_changebit(m, PG_A, FALSE);
3280 * Miscellaneous support routines follow
3284 i386_protection_init()
3286 register int *kp, prot;
3288 kp = protection_codes;
3289 for (prot = 0; prot < 8; prot++) {
3291 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE:
3293 * Read access is also 0. There isn't any execute bit,
3294 * so just make it readable.
3296 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE:
3297 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE:
3298 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE:
3301 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE:
3302 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE:
3303 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE:
3304 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
3312 * Map a set of physical memory pages into the kernel virtual
3313 * address space. Return a pointer to where it is mapped. This
3314 * routine is intended to be used for mapping device memory,
3318 pmap_mapdev(pa, size)
3322 vm_offset_t va, tmpva, offset;
3325 offset = pa & PAGE_MASK;
3326 size = roundup(offset + size, PAGE_SIZE);
3328 va = kmem_alloc_pageable(kernel_map, size);
3330 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3333 for (tmpva = va; size > 0;) {
3334 pte = (unsigned *)vtopte(tmpva);
3335 *pte = pa | PG_RW | PG_V | pgeflag;
3342 return ((void *)(va + offset));
3346 pmap_unmapdev(va, size)
3350 vm_offset_t base, offset;
3352 base = va & PG_FRAME;
3353 offset = va & PAGE_MASK;
3354 size = roundup(offset + size, PAGE_SIZE);
3355 kmem_free(kernel_map, base, size);
3359 * perform the pmap work for mincore
3362 pmap_mincore(pmap, addr)
3367 unsigned *ptep, pte;
3371 ptep = pmap_pte(pmap, addr);
3376 if ((pte = *ptep) != 0) {
3379 val = MINCORE_INCORE;
3380 if ((pte & PG_MANAGED) == 0)
3383 pa = pte & PG_FRAME;
3385 m = PHYS_TO_VM_PAGE(pa);
3391 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3393 * Modified by someone
3395 else if (m->dirty || pmap_is_modified(m))
3396 val |= MINCORE_MODIFIED_OTHER;
3401 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3404 * Referenced by someone
3406 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3407 val |= MINCORE_REFERENCED_OTHER;
3408 vm_page_flag_set(m, PG_REFERENCED);
3415 pmap_activate(struct proc *p)
3419 pmap = vmspace_pmap(p->p_vmspace);
3421 pmap->pm_active |= 1 << cpuid;
3423 pmap->pm_active |= 1;
3425 #if defined(SWTCH_OPTIM_STATS)
3428 p->p_thread->td_pcb->pcb_cr3 = vtophys(pmap->pm_pdir);
3429 load_cr3(p->p_thread->td_pcb->pcb_cr3);
3433 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3436 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3440 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3445 #if defined(PMAP_DEBUG)
3446 pmap_pid_dump(int pid)
3452 LIST_FOREACH(p, &allproc, p_list) {
3453 if (p->p_pid != pid)
3459 pmap = vmspace_pmap(p->p_vmspace);
3460 for(i=0;i<1024;i++) {
3463 unsigned base = i << PDRSHIFT;
3465 pde = &pmap->pm_pdir[i];
3466 if (pde && pmap_pde_v(pde)) {
3467 for(j=0;j<1024;j++) {
3468 unsigned va = base + (j << PAGE_SHIFT);
3469 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) {
3476 pte = pmap_pte_quick( pmap, va);
3477 if (pte && pmap_pte_v(pte)) {
3481 m = PHYS_TO_VM_PAGE(pa);
3482 printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x",
3483 va, pa, m->hold_count, m->wire_count, m->flags);
3504 static void pads __P((pmap_t pm));
3505 void pmap_pvdump __P((vm_offset_t pa));
3507 /* print address space of pmap*/
3515 if (pm == kernel_pmap)
3517 for (i = 0; i < 1024; i++)
3519 for (j = 0; j < 1024; j++) {
3520 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
3521 if (pm == kernel_pmap && va < KERNBASE)
3523 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS)
3525 ptep = pmap_pte_quick(pm, va);
3526 if (pmap_pte_v(ptep))
3527 printf("%x:%x ", va, *(int *) ptep);
3536 register pv_entry_t pv;
3539 printf("pa %x", pa);
3540 m = PHYS_TO_VM_PAGE(pa);
3541 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3543 printf(" -> pmap %p, va %x, flags %x",
3544 (void *)pv->pv_pmap, pv->pv_va, pv->pv_flags);
3546 printf(" -> pmap %p, va %x", (void *)pv->pv_pmap, pv->pv_va);