2 * Copyright (c) 1991 Regents of the University of California.
3 * Copyright (c) 1994 John S. Dyson
4 * Copyright (c) 1994 David Greenman
5 * Copyright (c) 2003 Peter Wemm
6 * Copyright (c) 2005-2008 Alan L. Cox <alc@cs.rice.edu>
7 * Copyright (c) 2008, 2009 The DragonFly Project.
8 * Copyright (c) 2008, 2009 Jordan Gordeev.
11 * This code is derived from software contributed to Berkeley by
12 * the Systems Programming Group of the University of Utah Computer
13 * Science Department and William Jolitz of UUNET Technologies Inc.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. All advertising materials mentioning features or use of this software
24 * must display the following acknowledgement:
25 * This product includes software developed by the University of
26 * California, Berkeley and its contributors.
27 * 4. Neither the name of the University nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91
44 * $FreeBSD: src/sys/i386/i386/pmap.c,v 1.250.2.18 2002/03/06 22:48:53 silby Exp $
45 * $DragonFly: src/sys/platform/pc64/amd64/pmap.c,v 1.3 2008/08/29 17:07:10 dillon Exp $
49 * Manages physical address maps.
51 * In addition to hardware address maps, this
52 * module is called upon to provide software-use-only
53 * maps which may or may not be stored in the same
54 * form as hardware maps. These pseudo-maps are
55 * used to store intermediate results from copy
56 * operations to and from address spaces.
58 * Since the information managed by this module is
59 * also stored by the logical address mapping module,
60 * this module may throw away valid virtual-to-physical
61 * mappings at almost any time. However, invalidations
62 * of virtual-to-physical mappings must be done as
65 * In order to cope with hardware architectures which
66 * make virtual-to-physical map invalidates expensive,
67 * this module may delay invalidate or reduced protection
68 * operations until such time as they are actually
69 * necessary. This module is given full information as
70 * to which processors are currently using which maps,
71 * and to when physical maps must be made correct.
75 #include "opt_disable_pse.h"
78 #include "opt_msgbuf.h"
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/kernel.h>
84 #include <sys/msgbuf.h>
85 #include <sys/vmmeter.h>
89 #include <vm/vm_param.h>
90 #include <sys/sysctl.h>
92 #include <vm/vm_kern.h>
93 #include <vm/vm_page.h>
94 #include <vm/vm_map.h>
95 #include <vm/vm_object.h>
96 #include <vm/vm_extern.h>
97 #include <vm/vm_pageout.h>
98 #include <vm/vm_pager.h>
99 #include <vm/vm_zone.h>
101 #include <sys/user.h>
102 #include <sys/thread2.h>
103 #include <sys/sysref2.h>
105 #include <machine/cputypes.h>
106 #include <machine/md_var.h>
107 #include <machine/specialreg.h>
108 #include <machine/smp.h>
109 #include <machine_base/apic/apicreg.h>
110 #include <machine/globaldata.h>
111 #include <machine/pmap.h>
112 #include <machine/pmap_inval.h>
116 #define PMAP_KEEP_PDIRS
117 #ifndef PMAP_SHPGPERPROC
118 #define PMAP_SHPGPERPROC 200
121 #if defined(DIAGNOSTIC)
122 #define PMAP_DIAGNOSTIC
127 #if !defined(PMAP_DIAGNOSTIC)
128 #define PMAP_INLINE __inline
134 * Get PDEs and PTEs for user/kernel address space
136 static pd_entry_t *pmap_pde(pmap_t pmap, vm_offset_t va);
137 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
139 #define pmap_pde_v(pte) ((*(pd_entry_t *)pte & PG_V) != 0)
140 #define pmap_pte_w(pte) ((*(pt_entry_t *)pte & PG_W) != 0)
141 #define pmap_pte_m(pte) ((*(pt_entry_t *)pte & PG_M) != 0)
142 #define pmap_pte_u(pte) ((*(pt_entry_t *)pte & PG_A) != 0)
143 #define pmap_pte_v(pte) ((*(pt_entry_t *)pte & PG_V) != 0)
147 * Given a map and a machine independent protection code,
148 * convert to a vax protection code.
150 #define pte_prot(m, p) \
151 (protection_codes[p & (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)])
152 static int protection_codes[8];
154 struct pmap kernel_pmap;
155 static TAILQ_HEAD(,pmap) pmap_list = TAILQ_HEAD_INITIALIZER(pmap_list);
157 vm_paddr_t avail_start; /* PA of first available physical page */
158 vm_paddr_t avail_end; /* PA of last available physical page */
159 vm_offset_t virtual_start; /* VA of first avail page (after kernel bss) */
160 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
161 vm_offset_t KvaStart; /* VA start of KVA space */
162 vm_offset_t KvaEnd; /* VA end of KVA space (non-inclusive) */
163 vm_offset_t KvaSize; /* max size of kernel virtual address space */
164 static boolean_t pmap_initialized = FALSE; /* Has pmap_init completed? */
165 static int pgeflag; /* PG_G or-in */
166 static int pseflag; /* PG_PS or-in */
168 static vm_object_t kptobj;
171 static vm_paddr_t dmaplimit;
173 vm_offset_t kernel_vm_end;
175 static uint64_t KPDphys; /* phys addr of kernel level 2 */
176 uint64_t KPDPphys; /* phys addr of kernel level 3 */
177 uint64_t KPML4phys; /* phys addr of kernel level 4 */
179 static uint64_t DMPDphys; /* phys addr of direct mapped level 2 */
180 static uint64_t DMPDPphys; /* phys addr of direct mapped level 3 */
183 * Data for the pv entry allocation mechanism
185 static vm_zone_t pvzone;
186 static struct vm_zone pvzone_store;
187 static struct vm_object pvzone_obj;
188 static int pv_entry_count=0, pv_entry_max=0, pv_entry_high_water=0;
189 static int pmap_pagedaemon_waken = 0;
190 static struct pv_entry *pvinit;
193 * All those kernel PT submaps that BSD is so fond of
195 pt_entry_t *CMAP1 = 0, *ptmmap;
196 caddr_t CADDR1 = 0, ptvmmap = 0;
197 static pt_entry_t *msgbufmap;
198 struct msgbuf *msgbufp=0;
203 static pt_entry_t *pt_crashdumpmap;
204 static caddr_t crashdumpmap;
206 extern uint64_t KPTphys;
207 extern pt_entry_t *SMPpt;
208 extern uint64_t SMPptpa;
212 static PMAP_INLINE void free_pv_entry (pv_entry_t pv);
213 static pv_entry_t get_pv_entry (void);
214 static void i386_protection_init (void);
215 static __inline void pmap_clearbit (vm_page_t m, int bit);
217 static void pmap_remove_all (vm_page_t m);
218 static void pmap_enter_quick (pmap_t pmap, vm_offset_t va, vm_page_t m);
219 static int pmap_remove_pte (struct pmap *pmap, pt_entry_t *ptq,
220 vm_offset_t sva, pmap_inval_info_t info);
221 static void pmap_remove_page (struct pmap *pmap,
222 vm_offset_t va, pmap_inval_info_t info);
223 static int pmap_remove_entry (struct pmap *pmap, vm_page_t m,
224 vm_offset_t va, pmap_inval_info_t info);
225 static boolean_t pmap_testbit (vm_page_t m, int bit);
226 static void pmap_insert_entry (pmap_t pmap, vm_offset_t va,
227 vm_page_t mpte, vm_page_t m);
229 static vm_page_t pmap_allocpte (pmap_t pmap, vm_offset_t va);
231 static int pmap_release_free_page (pmap_t pmap, vm_page_t p);
232 static vm_page_t _pmap_allocpte (pmap_t pmap, vm_pindex_t ptepindex);
233 static pt_entry_t * pmap_pte_quick (pmap_t pmap, vm_offset_t va);
234 static vm_page_t pmap_page_lookup (vm_object_t object, vm_pindex_t pindex);
235 static int pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m,
236 pmap_inval_info_t info);
237 static int pmap_unuse_pt (pmap_t, vm_offset_t, vm_page_t, pmap_inval_info_t);
238 static vm_offset_t pmap_kmem_choose(vm_offset_t addr);
240 static unsigned pdir4mb;
243 * Move the kernel virtual free pointer to the next
244 * 2MB. This is used to help improve performance
245 * by using a large (2MB) page for much of the kernel
246 * (.text, .data, .bss)
249 pmap_kmem_choose(vm_offset_t addr)
251 vm_offset_t newaddr = addr;
253 newaddr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
260 * Super fast pmap_pte routine best used when scanning the pv lists.
261 * This eliminates many course-grained invltlb calls. Note that many of
262 * the pv list scans are across different pmaps and it is very wasteful
263 * to do an entire invltlb when checking a single mapping.
265 * Should only be called while in a critical section.
267 static __inline pt_entry_t *pmap_pte(pmap_t pmap, vm_offset_t va);
270 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
272 return pmap_pte(pmap, va);
275 /* Return a non-clipped PD index for a given VA */
276 static __inline vm_pindex_t
277 pmap_pde_pindex(vm_offset_t va)
279 return va >> PDRSHIFT;
282 /* Return various clipped indexes for a given VA */
283 static __inline vm_pindex_t
284 pmap_pte_index(vm_offset_t va)
287 return ((va >> PAGE_SHIFT) & ((1ul << NPTEPGSHIFT) - 1));
290 static __inline vm_pindex_t
291 pmap_pde_index(vm_offset_t va)
294 return ((va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1));
297 static __inline vm_pindex_t
298 pmap_pdpe_index(vm_offset_t va)
301 return ((va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1));
304 static __inline vm_pindex_t
305 pmap_pml4e_index(vm_offset_t va)
308 return ((va >> PML4SHIFT) & ((1ul << NPML4EPGSHIFT) - 1));
311 /* Return a pointer to the PML4 slot that corresponds to a VA */
312 static __inline pml4_entry_t *
313 pmap_pml4e(pmap_t pmap, vm_offset_t va)
316 return (&pmap->pm_pml4[pmap_pml4e_index(va)]);
319 /* Return a pointer to the PDP slot that corresponds to a VA */
320 static __inline pdp_entry_t *
321 pmap_pml4e_to_pdpe(pml4_entry_t *pml4e, vm_offset_t va)
325 pdpe = (pdp_entry_t *)PHYS_TO_DMAP(*pml4e & PG_FRAME);
326 return (&pdpe[pmap_pdpe_index(va)]);
329 /* Return a pointer to the PDP slot that corresponds to a VA */
330 static __inline pdp_entry_t *
331 pmap_pdpe(pmap_t pmap, vm_offset_t va)
335 pml4e = pmap_pml4e(pmap, va);
336 if ((*pml4e & PG_V) == 0)
338 return (pmap_pml4e_to_pdpe(pml4e, va));
341 /* Return a pointer to the PD slot that corresponds to a VA */
342 static __inline pd_entry_t *
343 pmap_pdpe_to_pde(pdp_entry_t *pdpe, vm_offset_t va)
347 pde = (pd_entry_t *)PHYS_TO_DMAP(*pdpe & PG_FRAME);
348 return (&pde[pmap_pde_index(va)]);
351 /* Return a pointer to the PD slot that corresponds to a VA */
352 static __inline pd_entry_t *
353 pmap_pde(pmap_t pmap, vm_offset_t va)
357 pdpe = pmap_pdpe(pmap, va);
358 if (pdpe == NULL || (*pdpe & PG_V) == 0)
360 return (pmap_pdpe_to_pde(pdpe, va));
363 /* Return a pointer to the PT slot that corresponds to a VA */
364 static __inline pt_entry_t *
365 pmap_pde_to_pte(pd_entry_t *pde, vm_offset_t va)
369 pte = (pt_entry_t *)PHYS_TO_DMAP(*pde & PG_FRAME);
370 return (&pte[pmap_pte_index(va)]);
373 /* Return a pointer to the PT slot that corresponds to a VA */
374 static __inline pt_entry_t *
375 pmap_pte(pmap_t pmap, vm_offset_t va)
379 pde = pmap_pde(pmap, va);
380 if (pde == NULL || (*pde & PG_V) == 0)
382 if ((*pde & PG_PS) != 0) /* compat with i386 pmap_pte() */
383 return ((pt_entry_t *)pde);
384 return (pmap_pde_to_pte(pde, va));
388 PMAP_INLINE pt_entry_t *
389 vtopte(vm_offset_t va)
391 uint64_t mask = ((1ul << (NPTEPGSHIFT + NPDEPGSHIFT + NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1);
393 return (PTmap + ((va >> PAGE_SHIFT) & mask));
396 static __inline pd_entry_t *
397 vtopde(vm_offset_t va)
399 uint64_t mask = ((1ul << (NPDEPGSHIFT + NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1);
401 return (PDmap + ((va >> PDRSHIFT) & mask));
405 allocpages(vm_paddr_t *firstaddr, int n)
410 bzero((void *)ret, n * PAGE_SIZE);
411 *firstaddr += n * PAGE_SIZE;
416 create_pagetables(vm_paddr_t *firstaddr)
420 uint64_t cpu0pp, cpu0idlestk;
421 int idlestk_page_offset = offsetof(struct privatespace, idlestack) / PAGE_SIZE;
423 /* we are running (mostly) V=P at this point */
426 KPTphys = allocpages(firstaddr, NKPT);
427 KPML4phys = allocpages(firstaddr, 1);
428 KPDPphys = allocpages(firstaddr, NKPML4E);
429 KPDphys = allocpages(firstaddr, NKPDPE);
431 ndmpdp = (ptoa(Maxmem) + NBPDP - 1) >> PDPSHIFT;
432 if (ndmpdp < 4) /* Minimum 4GB of dirmap */
434 DMPDPphys = allocpages(firstaddr, NDMPML4E);
435 if ((amd_feature & AMDID_PAGE1GB) == 0)
436 DMPDphys = allocpages(firstaddr, ndmpdp);
437 dmaplimit = (vm_paddr_t)ndmpdp << PDPSHIFT;
439 /* Fill in the underlying page table pages */
440 /* Read-only from zero to physfree */
441 /* XXX not fully used, underneath 2M pages */
442 for (i = 0; (i << PAGE_SHIFT) < *firstaddr; i++) {
443 ((pt_entry_t *)KPTphys)[i] = i << PAGE_SHIFT;
444 ((pt_entry_t *)KPTphys)[i] |= PG_RW | PG_V | PG_G;
447 /* Now map the page tables at their location within PTmap */
448 for (i = 0; i < NKPT; i++) {
449 ((pd_entry_t *)KPDphys)[i] = KPTphys + (i << PAGE_SHIFT);
450 ((pd_entry_t *)KPDphys)[i] |= PG_RW | PG_V;
453 /* Map from zero to end of allocations under 2M pages */
454 /* This replaces some of the KPTphys entries above */
455 for (i = 0; (i << PDRSHIFT) < *firstaddr; i++) {
456 ((pd_entry_t *)KPDphys)[i] = i << PDRSHIFT;
457 ((pd_entry_t *)KPDphys)[i] |= PG_RW | PG_V | PG_PS | PG_G;
460 /* And connect up the PD to the PDP */
461 for (i = 0; i < NKPDPE; i++) {
462 ((pdp_entry_t *)KPDPphys)[i + KPDPI] = KPDphys +
464 ((pdp_entry_t *)KPDPphys)[i + KPDPI] |= PG_RW | PG_V | PG_U;
467 /* Now set up the direct map space using either 2MB or 1GB pages */
468 /* Preset PG_M and PG_A because demotion expects it */
469 if ((amd_feature & AMDID_PAGE1GB) == 0) {
470 for (i = 0; i < NPDEPG * ndmpdp; i++) {
471 ((pd_entry_t *)DMPDphys)[i] = (vm_paddr_t)i << PDRSHIFT;
472 ((pd_entry_t *)DMPDphys)[i] |= PG_RW | PG_V | PG_PS |
475 /* And the direct map space's PDP */
476 for (i = 0; i < ndmpdp; i++) {
477 ((pdp_entry_t *)DMPDPphys)[i] = DMPDphys +
479 ((pdp_entry_t *)DMPDPphys)[i] |= PG_RW | PG_V | PG_U;
482 for (i = 0; i < ndmpdp; i++) {
483 ((pdp_entry_t *)DMPDPphys)[i] =
484 (vm_paddr_t)i << PDPSHIFT;
485 ((pdp_entry_t *)DMPDPphys)[i] |= PG_RW | PG_V | PG_PS |
490 /* And recursively map PML4 to itself in order to get PTmap */
491 ((pdp_entry_t *)KPML4phys)[PML4PML4I] = KPML4phys;
492 ((pdp_entry_t *)KPML4phys)[PML4PML4I] |= PG_RW | PG_V | PG_U;
494 /* Connect the Direct Map slot up to the PML4 */
495 ((pdp_entry_t *)KPML4phys)[DMPML4I] = DMPDPphys;
496 ((pdp_entry_t *)KPML4phys)[DMPML4I] |= PG_RW | PG_V | PG_U;
498 /* Connect the KVA slot up to the PML4 */
499 ((pdp_entry_t *)KPML4phys)[KPML4I] = KPDPphys;
500 ((pdp_entry_t *)KPML4phys)[KPML4I] |= PG_RW | PG_V | PG_U;
504 init_paging(vm_paddr_t *firstaddr)
506 create_pagetables(firstaddr);
510 * Bootstrap the system enough to run with virtual memory.
512 * On the i386 this is called after mapping has already been enabled
513 * and just syncs the pmap module with what has already been done.
514 * [We can't call it easily with mapping off since the kernel is not
515 * mapped with PA == VA, hence we would have to relocate every address
516 * from the linked base (virtual) address "KERNBASE" to the actual
517 * (physical) address starting relative to 0]
520 pmap_bootstrap(vm_paddr_t *firstaddr)
524 struct mdglobaldata *gd;
528 KvaStart = VM_MIN_KERNEL_ADDRESS;
529 KvaEnd = VM_MAX_KERNEL_ADDRESS;
530 KvaSize = KvaEnd - KvaStart;
532 avail_start = *firstaddr;
535 * Create an initial set of page tables to run the kernel in.
537 create_pagetables(firstaddr);
539 virtual_start = (vm_offset_t) PTOV_OFFSET + *firstaddr;
540 virtual_start = pmap_kmem_choose(virtual_start);
542 virtual_end = VM_MAX_KERNEL_ADDRESS;
544 /* XXX do %cr0 as well */
545 load_cr4(rcr4() | CR4_PGE | CR4_PSE);
549 * Initialize protection array.
551 i386_protection_init();
554 * The kernel's pmap is statically allocated so we don't have to use
555 * pmap_create, which is unlikely to work correctly at this part of
556 * the boot sequence (XXX and which no longer exists).
558 kernel_pmap.pm_pml4 = (pdp_entry_t *) (PTOV_OFFSET + KPML4phys);
559 kernel_pmap.pm_count = 1;
560 kernel_pmap.pm_active = (cpumask_t)-1; /* don't allow deactivation */
561 TAILQ_INIT(&kernel_pmap.pm_pvlist);
565 * Reserve some special page table entries/VA space for temporary
568 #define SYSMAP(c, p, v, n) \
569 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
573 pte = (pt_entry_t *) pmap_pte(&kernel_pmap, va);
579 * CMAP1/CMAP2 are used for zeroing and copying pages.
581 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
586 SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS);
589 * ptvmmap is used for reading arbitrary physical pages via
592 SYSMAP(caddr_t, ptmmap, ptvmmap, 1)
595 * msgbufp is used to map the system message buffer.
596 * XXX msgbufmap is not used.
598 SYSMAP(struct msgbuf *, msgbufmap, msgbufp,
599 atop(round_page(MSGBUF_SIZE)))
606 * PG_G is terribly broken on SMP because we IPI invltlb's in some
607 * cases rather then invl1pg. Actually, I don't even know why it
608 * works under UP because self-referential page table mappings
613 if (cpu_feature & CPUID_PGE)
618 * Initialize the 4MB page size flag
622 * The 4MB page version of the initial
623 * kernel page mapping.
627 #if !defined(DISABLE_PSE)
628 if (cpu_feature & CPUID_PSE) {
631 * Note that we have enabled PSE mode
634 ptditmp = *(PTmap + amd64_btop(KERNBASE));
635 ptditmp &= ~(NBPDR - 1);
636 ptditmp |= PG_V | PG_RW | PG_PS | PG_U | pgeflag;
641 * Enable the PSE mode. If we are SMP we can't do this
642 * now because the APs will not be able to use it when
645 load_cr4(rcr4() | CR4_PSE);
648 * We can do the mapping here for the single processor
649 * case. We simply ignore the old page table page from
653 * For SMP, we still need 4K pages to bootstrap APs,
654 * PSE will be enabled as soon as all APs are up.
656 PTD[KPTDI] = (pd_entry_t)ptditmp;
662 if (cpu_apic_address == 0)
663 panic("pmap_bootstrap: no local apic!");
667 * We need to finish setting up the globaldata page for the BSP.
668 * locore has already populated the page table for the mdglobaldata
671 pg = MDGLOBALDATA_BASEALLOC_PAGES;
672 gd = &CPU_prvspace[0].mdglobaldata;
673 gd->gd_CMAP1 = &SMPpt[pg + 0];
674 gd->gd_CMAP2 = &SMPpt[pg + 1];
675 gd->gd_CMAP3 = &SMPpt[pg + 2];
676 gd->gd_PMAP1 = &SMPpt[pg + 3];
677 gd->gd_CADDR1 = CPU_prvspace[0].CPAGE1;
678 gd->gd_CADDR2 = CPU_prvspace[0].CPAGE2;
679 gd->gd_CADDR3 = CPU_prvspace[0].CPAGE3;
680 gd->gd_PADDR1 = (pt_entry_t *)CPU_prvspace[0].PPAGE1;
687 * Set 4mb pdir for mp startup
692 if (pseflag && (cpu_feature & CPUID_PSE)) {
693 load_cr4(rcr4() | CR4_PSE);
694 if (pdir4mb && mycpu->gd_cpuid == 0) { /* only on BSP */
702 * Initialize the pmap module.
703 * Called by vm_init, to initialize any structures that the pmap
704 * system needs to map virtual memory.
705 * pmap_init has been enhanced to support in a fairly consistant
706 * way, discontiguous physical memory.
715 * object for kernel page table pages
717 /* JG I think the number can be arbitrary */
718 kptobj = vm_object_allocate(OBJT_DEFAULT, 5);
721 * Allocate memory for random pmap data structures. Includes the
725 for(i = 0; i < vm_page_array_size; i++) {
728 m = &vm_page_array[i];
729 TAILQ_INIT(&m->md.pv_list);
730 m->md.pv_list_count = 0;
734 * init the pv free list
736 initial_pvs = vm_page_array_size;
737 if (initial_pvs < MINPV)
739 pvzone = &pvzone_store;
740 pvinit = (struct pv_entry *) kmem_alloc(&kernel_map,
741 initial_pvs * sizeof (struct pv_entry));
742 zbootinit(pvzone, "PV ENTRY", sizeof (struct pv_entry), pvinit,
746 * Now it is safe to enable pv_table recording.
748 pmap_initialized = TRUE;
750 lapic = pmap_mapdev_uncacheable(cpu_apic_address, sizeof(struct LAPIC));
755 * Initialize the address space (zone) for the pv_entries. Set a
756 * high water mark so that the system can recover from excessive
757 * numbers of pv entries.
762 int shpgperproc = PMAP_SHPGPERPROC;
764 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
765 pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
766 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
767 pv_entry_high_water = 9 * (pv_entry_max / 10);
768 zinitna(pvzone, &pvzone_obj, NULL, 0, pv_entry_max, ZONE_INTERRUPT, 1);
772 /***************************************************
773 * Low level helper routines.....
774 ***************************************************/
776 #if defined(PMAP_DIAGNOSTIC)
779 * This code checks for non-writeable/modified pages.
780 * This should be an invalid condition.
783 pmap_nw_modified(pt_entry_t pte)
785 if ((pte & (PG_M|PG_RW)) == PG_M)
794 * this routine defines the region(s) of memory that should
795 * not be tested for the modified bit.
797 static PMAP_INLINE int
798 pmap_track_modified(vm_offset_t va)
800 if ((va < clean_sva) || (va >= clean_eva))
809 * Extract the physical page address associated with the map/VA pair.
811 * This function may not be called from an interrupt if the pmap is
815 pmap_extract(pmap_t pmap, vm_offset_t va)
819 pd_entry_t pde, *pdep;
822 pdep = pmap_pde(pmap, va);
826 if ((pde & PG_PS) != 0) {
827 rtval = (pde & PG_PS_FRAME) | (va & PDRMASK);
829 pte = pmap_pde_to_pte(pdep, va);
830 rtval = (*pte & PG_FRAME) | (va & PAGE_MASK);
838 * Routine: pmap_kextract
840 * Extract the physical page address associated
841 * kernel virtual address.
844 pmap_kextract(vm_offset_t va)
849 if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS) {
850 pa = DMAP_TO_PHYS(va);
854 pa = (pde & PG_PS_FRAME) | (va & PDRMASK);
857 * Beware of a concurrent promotion that changes the
858 * PDE at this point! For example, vtopte() must not
859 * be used to access the PTE because it would use the
860 * new PDE. It is, however, safe to use the old PDE
861 * because the page table page is preserved by the
864 pa = *pmap_pde_to_pte(&pde, va);
865 pa = (pa & PG_FRAME) | (va & PAGE_MASK);
871 /***************************************************
872 * Low level mapping routines.....
873 ***************************************************/
876 * Routine: pmap_kenter
878 * Add a wired page to the KVA
879 * NOTE! note that in order for the mapping to take effect -- you
880 * should do an invltlb after doing the pmap_kenter().
883 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
887 pmap_inval_info info;
889 pmap_inval_init(&info);
890 npte = pa | PG_RW | PG_V | pgeflag;
892 pmap_inval_add(&info, &kernel_pmap, va);
894 pmap_inval_flush(&info);
898 * Routine: pmap_kenter_quick
900 * Similar to pmap_kenter(), except we only invalidate the
901 * mapping on the current CPU.
904 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
909 npte = pa | PG_RW | PG_V | pgeflag;
912 cpu_invlpg((void *)va);
916 pmap_kenter_sync(vm_offset_t va)
918 pmap_inval_info info;
920 pmap_inval_init(&info);
921 pmap_inval_add(&info, &kernel_pmap, va);
922 pmap_inval_flush(&info);
926 pmap_kenter_sync_quick(vm_offset_t va)
928 cpu_invlpg((void *)va);
932 * remove a page from the kernel pagetables
935 pmap_kremove(vm_offset_t va)
938 pmap_inval_info info;
940 pmap_inval_init(&info);
942 pmap_inval_add(&info, &kernel_pmap, va);
944 pmap_inval_flush(&info);
948 pmap_kremove_quick(vm_offset_t va)
953 cpu_invlpg((void *)va);
957 * XXX these need to be recoded. They are not used in any critical path.
960 pmap_kmodify_rw(vm_offset_t va)
962 *vtopte(va) |= PG_RW;
963 cpu_invlpg((void *)va);
967 pmap_kmodify_nc(vm_offset_t va)
970 cpu_invlpg((void *)va);
974 * Used to map a range of physical addresses into kernel
975 * virtual address space.
977 * For now, VM is already on, we only need to map the
981 pmap_map(vm_offset_t virt, vm_paddr_t start, vm_paddr_t end, int prot)
983 return PHYS_TO_DMAP(start);
988 * Add a list of wired pages to the kva
989 * this routine is only used for temporary
990 * kernel mappings that do not need to have
991 * page modification or references recorded.
992 * Note that old mappings are simply written
993 * over. The page *must* be wired.
996 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
1000 end_va = va + count * PAGE_SIZE;
1002 while (va < end_va) {
1006 *pte = VM_PAGE_TO_PHYS(*m) | PG_RW | PG_V | pgeflag;
1007 cpu_invlpg((void *)va);
1012 smp_invltlb(); /* XXX */
1017 pmap_qenter2(vm_offset_t va, vm_page_t *m, int count, cpumask_t *mask)
1020 cpumask_t cmask = mycpu->gd_cpumask;
1022 end_va = va + count * PAGE_SIZE;
1024 while (va < end_va) {
1029 * Install the new PTE. If the pte changed from the prior
1030 * mapping we must reset the cpu mask and invalidate the page.
1031 * If the pte is the same but we have not seen it on the
1032 * current cpu, invlpg the existing mapping. Otherwise the
1033 * entry is optimal and no invalidation is required.
1036 pteval = VM_PAGE_TO_PHYS(*m) | PG_A | PG_RW | PG_V | pgeflag;
1037 if (*pte != pteval) {
1040 cpu_invlpg((void *)va);
1041 } else if ((*mask & cmask) == 0) {
1042 cpu_invlpg((void *)va);
1051 * This routine jerks page mappings from the
1052 * kernel -- it is meant only for temporary mappings.
1054 * MPSAFE, INTERRUPT SAFE (cluster callback)
1057 pmap_qremove(vm_offset_t va, int count)
1061 end_va = va + count * PAGE_SIZE;
1063 while (va < end_va) {
1068 cpu_invlpg((void *)va);
1077 * This routine works like vm_page_lookup() but also blocks as long as the
1078 * page is busy. This routine does not busy the page it returns.
1080 * Unless the caller is managing objects whos pages are in a known state,
1081 * the call should be made with a critical section held so the page's object
1082 * association remains valid on return.
1085 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
1090 m = vm_page_lookup(object, pindex);
1091 } while (m && vm_page_sleep_busy(m, FALSE, "pplookp"));
1097 * Create a new thread and optionally associate it with a (new) process.
1098 * NOTE! the new thread's cpu may not equal the current cpu.
1101 pmap_init_thread(thread_t td)
1103 /* enforce pcb placement */
1104 td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
1105 td->td_savefpu = &td->td_pcb->pcb_save;
1106 td->td_sp = (char *)td->td_pcb - 16; /* JG is -16 needed on amd64? */
1110 * This routine directly affects the fork perf for a process.
1113 pmap_init_proc(struct proc *p)
1118 * Dispose the UPAGES for a process that has exited.
1119 * This routine directly impacts the exit perf of a process.
1122 pmap_dispose_proc(struct proc *p)
1124 KASSERT(p->p_lock == 0, ("attempt to dispose referenced proc! %p", p));
1127 /***************************************************
1128 * Page table page management routines.....
1129 ***************************************************/
1132 * This routine unholds page table pages, and if the hold count
1133 * drops to zero, then it decrements the wire count.
1136 _pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m, pmap_inval_info_t info)
1139 * Wait until we can busy the page ourselves. We cannot have
1140 * any active flushes if we block.
1142 if (m->flags & PG_BUSY) {
1143 pmap_inval_flush(info);
1144 while (vm_page_sleep_busy(m, FALSE, "pmuwpt"))
1147 KASSERT(m->queue == PQ_NONE,
1148 ("_pmap_unwire_pte_hold: %p->queue != PQ_NONE", m));
1150 if (m->hold_count == 1) {
1152 * Unmap the page table page
1155 pmap_inval_add(info, pmap, -1);
1157 if (m->pindex >= (NUPDE + NUPDPE)) {
1160 pml4 = pmap_pml4e(pmap, va);
1162 } else if (m->pindex >= NUPDE) {
1165 pdp = pmap_pdpe(pmap, va);
1170 pd = pmap_pde(pmap, va);
1174 KKASSERT(pmap->pm_stats.resident_count > 0);
1175 --pmap->pm_stats.resident_count;
1177 if (pmap->pm_ptphint == m)
1178 pmap->pm_ptphint = NULL;
1180 if (m->pindex < NUPDE) {
1181 /* We just released a PT, unhold the matching PD */
1184 pdpg = PHYS_TO_VM_PAGE(*pmap_pdpe(pmap, va) & PG_FRAME);
1185 pmap_unwire_pte_hold(pmap, va, pdpg, info);
1187 if (m->pindex >= NUPDE && m->pindex < (NUPDE + NUPDPE)) {
1188 /* We just released a PD, unhold the matching PDP */
1191 pdppg = PHYS_TO_VM_PAGE(*pmap_pml4e(pmap, va) & PG_FRAME);
1192 pmap_unwire_pte_hold(pmap, va, pdppg, info);
1196 * This was our last hold, the page had better be unwired
1197 * after we decrement wire_count.
1199 * FUTURE NOTE: shared page directory page could result in
1200 * multiple wire counts.
1204 KKASSERT(m->wire_count == 0);
1205 --vmstats.v_wire_count;
1206 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1208 vm_page_free_zero(m);
1211 /* JG Can we get here? */
1212 KKASSERT(m->hold_count > 1);
1218 static PMAP_INLINE int
1219 pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m, pmap_inval_info_t info)
1221 KKASSERT(m->hold_count > 0);
1222 if (m->hold_count > 1) {
1226 return _pmap_unwire_pte_hold(pmap, va, m, info);
1231 * After removing a page table entry, this routine is used to
1232 * conditionally free the page, and manage the hold/wire counts.
1235 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte,
1236 pmap_inval_info_t info)
1238 /* JG Use FreeBSD/amd64 or FreeBSD/i386 ptepde approaches? */
1239 vm_pindex_t ptepindex;
1240 if (va >= VM_MAX_USER_ADDRESS)
1244 ptepindex = pmap_pde_pindex(va);
1246 if (pmap->pm_ptphint &&
1247 (pmap->pm_ptphint->pindex == ptepindex)) {
1248 mpte = pmap->pm_ptphint;
1251 pmap_inval_flush(info);
1252 mpte = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1253 pmap->pm_ptphint = mpte;
1259 return pmap_unwire_pte_hold(pmap, va, mpte, info);
1263 * Initialize pmap0/vmspace0. This pmap is not added to pmap_list because
1264 * it, and IdlePTD, represents the template used to update all other pmaps.
1266 * On architectures where the kernel pmap is not integrated into the user
1267 * process pmap, this pmap represents the process pmap, not the kernel pmap.
1268 * kernel_pmap should be used to directly access the kernel_pmap.
1271 pmap_pinit0(struct pmap *pmap)
1273 pmap->pm_pml4 = (pml4_entry_t *)(PTOV_OFFSET + KPML4phys);
1275 pmap->pm_active = 0;
1276 pmap->pm_ptphint = NULL;
1277 TAILQ_INIT(&pmap->pm_pvlist);
1278 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1282 * Initialize a preallocated and zeroed pmap structure,
1283 * such as one in a vmspace structure.
1286 pmap_pinit(struct pmap *pmap)
1291 * No need to allocate page table space yet but we do need a valid
1292 * page directory table.
1294 if (pmap->pm_pml4 == NULL) {
1296 (pml4_entry_t *)kmem_alloc_pageable(&kernel_map, PAGE_SIZE);
1300 * Allocate an object for the ptes
1302 if (pmap->pm_pteobj == NULL)
1303 pmap->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, NUPDE + NUPDPE + PML4PML4I + 1);
1306 * Allocate the page directory page, unless we already have
1307 * one cached. If we used the cached page the wire_count will
1308 * already be set appropriately.
1310 if ((ptdpg = pmap->pm_pdirm) == NULL) {
1311 ptdpg = vm_page_grab(pmap->pm_pteobj, NUPDE + NUPDPE + PML4PML4I,
1312 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1313 pmap->pm_pdirm = ptdpg;
1314 vm_page_flag_clear(ptdpg, PG_MAPPED | PG_BUSY);
1315 ptdpg->valid = VM_PAGE_BITS_ALL;
1316 ptdpg->wire_count = 1;
1317 ++vmstats.v_wire_count;
1318 pmap_kenter((vm_offset_t)pmap->pm_pml4, VM_PAGE_TO_PHYS(ptdpg));
1320 if ((ptdpg->flags & PG_ZERO) == 0)
1321 bzero(pmap->pm_pml4, PAGE_SIZE);
1323 pmap->pm_pml4[KPML4I] = KPDPphys | PG_RW | PG_V | PG_U;
1324 pmap->pm_pml4[DMPML4I] = DMPDPphys | PG_RW | PG_V | PG_U;
1326 /* install self-referential address mapping entry */
1327 pmap->pm_pml4[PML4PML4I] = VM_PAGE_TO_PHYS(ptdpg) | PG_V | PG_RW | PG_A | PG_M;
1330 pmap->pm_active = 0;
1331 pmap->pm_ptphint = NULL;
1332 TAILQ_INIT(&pmap->pm_pvlist);
1333 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1334 pmap->pm_stats.resident_count = 1;
1338 * Clean up a pmap structure so it can be physically freed. This routine
1339 * is called by the vmspace dtor function. A great deal of pmap data is
1340 * left passively mapped to improve vmspace management so we have a bit
1341 * of cleanup work to do here.
1344 pmap_puninit(pmap_t pmap)
1348 KKASSERT(pmap->pm_active == 0);
1349 if ((p = pmap->pm_pdirm) != NULL) {
1350 KKASSERT(pmap->pm_pml4 != NULL);
1351 KKASSERT(pmap->pm_pml4 != (PTOV_OFFSET + KPML4phys));
1352 pmap_kremove((vm_offset_t)pmap->pm_pml4);
1354 vmstats.v_wire_count--;
1355 KKASSERT((p->flags & PG_BUSY) == 0);
1357 vm_page_free_zero(p);
1358 pmap->pm_pdirm = NULL;
1360 if (pmap->pm_pml4) {
1361 KKASSERT(pmap->pm_pml4 != (PTOV_OFFSET + KPML4phys));
1362 kmem_free(&kernel_map, (vm_offset_t)pmap->pm_pml4, PAGE_SIZE);
1363 pmap->pm_pml4 = NULL;
1365 if (pmap->pm_pteobj) {
1366 vm_object_deallocate(pmap->pm_pteobj);
1367 pmap->pm_pteobj = NULL;
1372 * Wire in kernel global address entries. To avoid a race condition
1373 * between pmap initialization and pmap_growkernel, this procedure
1374 * adds the pmap to the master list (which growkernel scans to update),
1375 * then copies the template.
1378 pmap_pinit2(struct pmap *pmap)
1381 TAILQ_INSERT_TAIL(&pmap_list, pmap, pm_pmnode);
1382 /* XXX copies current process, does not fill in MPPTDI */
1387 * Attempt to release and free a vm_page in a pmap. Returns 1 on success,
1388 * 0 on failure (if the procedure had to sleep).
1390 * When asked to remove the page directory page itself, we actually just
1391 * leave it cached so we do not have to incur the SMP inval overhead of
1392 * removing the kernel mapping. pmap_puninit() will take care of it.
1395 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1397 pml4_entry_t *pml4 = pmap->pm_pml4;
1399 * This code optimizes the case of freeing non-busy
1400 * page-table pages. Those pages are zero now, and
1401 * might as well be placed directly into the zero queue.
1403 if (vm_page_sleep_busy(p, FALSE, "pmaprl"))
1409 * Remove the page table page from the processes address space.
1411 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1413 * We are the pml4 table itself.
1415 /* XXX anything to do here? */
1416 } else if (p->pindex >= (NUPDE + NUPDPE)) {
1418 * We are a PDP page.
1419 * We look for the PML4 entry that points to us.
1421 vm_page_t m4 = vm_page_lookup(pmap->pm_pteobj, NUPDE + NUPDPE + PML4PML4I);
1422 KKASSERT(m4 != NULL);
1423 pml4_entry_t *pml4 = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m4));
1424 int idx = (p->pindex - (NUPDE + NUPDPE)) % NPML4EPG;
1425 KKASSERT(pml4[idx] != 0);
1428 /* JG What about wire_count? */
1429 } else if (p->pindex >= NUPDE) {
1432 * We look for the PDP entry that points to us.
1434 vm_page_t m3 = vm_page_lookup(pmap->pm_pteobj, NUPDE + NUPDPE + (p->pindex - NUPDE) / NPDPEPG);
1435 KKASSERT(m3 != NULL);
1436 pdp_entry_t *pdp = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m3));
1437 int idx = (p->pindex - NUPDE) % NPDPEPG;
1438 KKASSERT(pdp[idx] != 0);
1441 /* JG What about wire_count? */
1443 /* We are a PT page.
1444 * We look for the PD entry that points to us.
1446 vm_page_t m2 = vm_page_lookup(pmap->pm_pteobj, NUPDE + p->pindex / NPDEPG);
1447 KKASSERT(m2 != NULL);
1448 pd_entry_t *pd = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m2));
1449 int idx = p->pindex % NPDEPG;
1452 /* JG What about wire_count? */
1454 KKASSERT(pmap->pm_stats.resident_count > 0);
1455 --pmap->pm_stats.resident_count;
1457 if (p->hold_count) {
1458 panic("pmap_release: freeing held page table page");
1460 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1461 pmap->pm_ptphint = NULL;
1464 * We leave the top-level page table page cached, wired, and mapped in
1465 * the pmap until the dtor function (pmap_puninit()) gets called.
1466 * However, still clean it up so we can set PG_ZERO.
1468 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1469 bzero(pmap->pm_pml4, PAGE_SIZE);
1470 vm_page_flag_set(p, PG_ZERO);
1474 vmstats.v_wire_count--;
1475 /* JG eventually revert to using vm_page_free_zero() */
1482 * This routine is called when various levels in the page table need to
1483 * be populated. This routine cannot fail.
1486 _pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex)
1491 * Find or fabricate a new pagetable page. This will busy the page.
1493 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1494 VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1497 if ((m->flags & PG_ZERO) == 0) {
1498 pmap_zero_page(VM_PAGE_TO_PHYS(m));
1501 KASSERT(m->queue == PQ_NONE,
1502 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1505 * Increment the hold count for the page we will be returning to
1509 if (m->wire_count == 0)
1510 vmstats.v_wire_count++;
1514 * Map the pagetable page into the process address space, if
1515 * it isn't already there.
1517 * It is possible that someone else got in and mapped the page
1518 * directory page while we were blocked, if so just unbusy and
1519 * return the held page.
1521 ++pmap->pm_stats.resident_count;
1523 if (ptepindex >= (NUPDE + NUPDPE)) {
1525 * Wire up a new PDP page in the PML4
1528 vm_pindex_t pml4index;
1530 pml4index = ptepindex - (NUPDE + NUPDPE);
1531 pml4 = &pmap->pm_pml4[pml4index];
1537 *pml4 = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
1538 } else if (ptepindex >= NUPDE) {
1540 * Wire up a new PD page in the PDP
1542 vm_pindex_t pml4index;
1543 vm_pindex_t pdpindex;
1548 pdpindex = ptepindex - NUPDE;
1549 pml4index = pdpindex >> NPML4EPGSHIFT;
1551 pml4 = &pmap->pm_pml4[pml4index];
1552 if ((*pml4 & PG_V) == 0) {
1554 * Have to allocate a new PDP page, recurse.
1555 * This always succeeds. Returned page will
1558 pdppg = _pmap_allocpte(pmap,
1559 NUPDE + NUPDPE + pml4index);
1562 * Add a held reference to the PDP page.
1564 pdppg = PHYS_TO_VM_PAGE(*pml4 & PG_FRAME);
1565 pdppg->hold_count++;
1569 * Now find the pdp_entry and map the PDP. If the PDP
1570 * has already been mapped unwind and return the
1571 * already-mapped PDP held.
1573 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
1574 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1576 vm_page_unhold(pdppg);
1581 *pdp = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
1584 * Wire up the new PT page in the PD
1586 vm_pindex_t pml4index;
1587 vm_pindex_t pdpindex;
1593 pdpindex = ptepindex >> NPDPEPGSHIFT;
1594 pml4index = pdpindex >> NPML4EPGSHIFT;
1597 * Locate the PDP page in the PML4, then the PD page in
1598 * the PDP. If either does not exist we simply recurse
1601 * We can just recurse on the PD page as it will recurse
1602 * on the PDP if necessary.
1604 pml4 = &pmap->pm_pml4[pml4index];
1605 if ((*pml4 & PG_V) == 0) {
1606 pdpg = _pmap_allocpte(pmap, NUPDE + pdpindex);
1607 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
1608 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1610 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
1611 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1612 if ((*pdp & PG_V) == 0) {
1613 pdpg = _pmap_allocpte(pmap, NUPDE + pdpindex);
1615 pdpg = PHYS_TO_VM_PAGE(*pdp & PG_FRAME);
1621 * Now fill in the pte in the PD. If the pte already exists
1622 * (again, if we raced the grab), unhold pdpg and unwire
1623 * m, returning a held m.
1625 pd = (pd_entry_t *)PHYS_TO_DMAP(*pdp & PG_FRAME);
1626 pd = &pd[ptepindex & ((1ul << NPDEPGSHIFT) - 1)];
1628 *pd = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW |
1631 vm_page_unhold(pdpg);
1639 * We successfully loaded a PDP, PD, or PTE. Set the page table hint,
1640 * valid bits, mapped flag, unbusy, and we're done.
1642 pmap->pm_ptphint = m;
1644 m->valid = VM_PAGE_BITS_ALL;
1645 vm_page_flag_clear(m, PG_ZERO);
1646 vm_page_flag_set(m, PG_MAPPED);
1653 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1655 vm_pindex_t ptepindex;
1660 * Calculate pagetable page index
1662 ptepindex = pmap_pde_pindex(va);
1665 * Get the page directory entry
1667 pd = pmap_pde(pmap, va);
1670 * This supports switching from a 2MB page to a
1673 if (pd != NULL && (*pd & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
1674 panic("no promotion/demotion yet");
1682 * If the page table page is mapped, we just increment the
1683 * hold count, and activate it.
1685 if (pd != NULL && (*pd & PG_V) != 0) {
1686 /* YYY hint is used here on i386 */
1687 m = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1688 pmap->pm_ptphint = m;
1693 * Here if the pte page isn't mapped, or if it has been deallocated.
1695 return _pmap_allocpte(pmap, ptepindex);
1699 /***************************************************
1700 * Pmap allocation/deallocation routines.
1701 ***************************************************/
1704 * Release any resources held by the given physical map.
1705 * Called when a pmap initialized by pmap_pinit is being released.
1706 * Should only be called if the map contains no valid mappings.
1708 static int pmap_release_callback(struct vm_page *p, void *data);
1711 pmap_release(struct pmap *pmap)
1713 vm_object_t object = pmap->pm_pteobj;
1714 struct rb_vm_page_scan_info info;
1716 KASSERT(pmap->pm_active == 0, ("pmap still active! %08x", pmap->pm_active));
1717 #if defined(DIAGNOSTIC)
1718 if (object->ref_count != 1)
1719 panic("pmap_release: pteobj reference count != 1");
1723 info.object = object;
1725 TAILQ_REMOVE(&pmap_list, pmap, pm_pmnode);
1732 info.limit = object->generation;
1734 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
1735 pmap_release_callback, &info);
1736 if (info.error == 0 && info.mpte) {
1737 if (!pmap_release_free_page(pmap, info.mpte))
1741 } while (info.error);
1745 pmap_release_callback(struct vm_page *p, void *data)
1747 struct rb_vm_page_scan_info *info = data;
1749 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1753 if (!pmap_release_free_page(info->pmap, p)) {
1757 if (info->object->generation != info->limit) {
1765 * Grow the number of kernel page table entries, if needed.
1769 pmap_growkernel(vm_offset_t addr)
1773 vm_offset_t ptppaddr;
1775 pd_entry_t *pde, newpdir;
1779 if (kernel_vm_end == 0) {
1780 kernel_vm_end = KERNBASE;
1782 while ((*pmap_pde(&kernel_pmap, kernel_vm_end) & PG_V) != 0) {
1783 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1785 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1786 kernel_vm_end = kernel_map.max_offset;
1791 addr = roundup2(addr, PAGE_SIZE * NPTEPG);
1792 if (addr - 1 >= kernel_map.max_offset)
1793 addr = kernel_map.max_offset;
1794 while (kernel_vm_end < addr) {
1795 pde = pmap_pde(&kernel_pmap, kernel_vm_end);
1797 /* We need a new PDP entry */
1798 nkpg = vm_page_alloc(kptobj, nkpt,
1799 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM
1800 | VM_ALLOC_INTERRUPT);
1802 panic("pmap_growkernel: no memory to grow kernel");
1803 paddr = VM_PAGE_TO_PHYS(nkpg);
1804 if ((nkpg->flags & PG_ZERO) == 0)
1805 pmap_zero_page(paddr);
1806 vm_page_flag_clear(nkpg, PG_ZERO);
1807 newpdp = (pdp_entry_t)
1808 (paddr | PG_V | PG_RW | PG_A | PG_M);
1809 *pmap_pdpe(&kernel_pmap, kernel_vm_end) = newpdp;
1811 continue; /* try again */
1813 if ((*pde & PG_V) != 0) {
1814 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1815 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1816 kernel_vm_end = kernel_map.max_offset;
1823 * This index is bogus, but out of the way
1825 nkpg = vm_page_alloc(kptobj, nkpt,
1826 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM | VM_ALLOC_INTERRUPT);
1828 panic("pmap_growkernel: no memory to grow kernel");
1831 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1832 pmap_zero_page(ptppaddr);
1833 vm_page_flag_clear(nkpg, PG_ZERO);
1834 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
1835 *pmap_pde(&kernel_pmap, kernel_vm_end) = newpdir;
1838 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1839 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1840 kernel_vm_end = kernel_map.max_offset;
1848 * Retire the given physical map from service.
1849 * Should only be called if the map contains
1850 * no valid mappings.
1853 pmap_destroy(pmap_t pmap)
1860 count = --pmap->pm_count;
1863 panic("destroying a pmap is not yet implemented");
1868 * Add a reference to the specified pmap.
1871 pmap_reference(pmap_t pmap)
1878 /***************************************************
1879 * page management routines.
1880 ***************************************************/
1883 * free the pv_entry back to the free list. This function may be
1884 * called from an interrupt.
1886 static PMAP_INLINE void
1887 free_pv_entry(pv_entry_t pv)
1890 KKASSERT(pv_entry_count >= 0);
1895 * get a new pv_entry, allocating a block from the system
1896 * when needed. This function may be called from an interrupt.
1902 if (pv_entry_high_water &&
1903 (pv_entry_count > pv_entry_high_water) &&
1904 (pmap_pagedaemon_waken == 0)) {
1905 pmap_pagedaemon_waken = 1;
1906 wakeup(&vm_pages_needed);
1908 return zalloc(pvzone);
1912 * This routine is very drastic, but can save the system
1920 static int warningdone=0;
1922 if (pmap_pagedaemon_waken == 0)
1925 if (warningdone < 5) {
1926 kprintf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1930 for(i = 0; i < vm_page_array_size; i++) {
1931 m = &vm_page_array[i];
1932 if (m->wire_count || m->hold_count || m->busy ||
1933 (m->flags & PG_BUSY))
1937 pmap_pagedaemon_waken = 0;
1942 * If it is the first entry on the list, it is actually
1943 * in the header and we must copy the following entry up
1944 * to the header. Otherwise we must search the list for
1945 * the entry. In either case we free the now unused entry.
1948 pmap_remove_entry(struct pmap *pmap, vm_page_t m,
1949 vm_offset_t va, pmap_inval_info_t info)
1955 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1956 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1957 if (pmap == pv->pv_pmap && va == pv->pv_va)
1961 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1962 if (va == pv->pv_va)
1968 /* JGXXX When can 'pv' be NULL? */
1970 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1971 m->md.pv_list_count--;
1972 KKASSERT(m->md.pv_list_count >= 0);
1973 if (TAILQ_EMPTY(&m->md.pv_list))
1974 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1975 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1976 ++pmap->pm_generation;
1977 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem, info);
1985 * Create a pv entry for page at pa for
1989 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
1994 pv = get_pv_entry();
1999 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
2000 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2001 m->md.pv_list_count++;
2007 * pmap_remove_pte: do the things to unmap a page in a process
2010 pmap_remove_pte(struct pmap *pmap, pt_entry_t *ptq, vm_offset_t va,
2011 pmap_inval_info_t info)
2016 pmap_inval_add(info, pmap, va);
2017 oldpte = pte_load_clear(ptq);
2019 pmap->pm_stats.wired_count -= 1;
2021 * Machines that don't support invlpg, also don't support
2022 * PG_G. XXX PG_G is disabled for SMP so don't worry about
2026 cpu_invlpg((void *)va);
2027 KKASSERT(pmap->pm_stats.resident_count > 0);
2028 --pmap->pm_stats.resident_count;
2029 if (oldpte & PG_MANAGED) {
2030 m = PHYS_TO_VM_PAGE(oldpte);
2031 if (oldpte & PG_M) {
2032 #if defined(PMAP_DIAGNOSTIC)
2033 if (pmap_nw_modified((pt_entry_t) oldpte)) {
2035 "pmap_remove: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2039 if (pmap_track_modified(va))
2043 vm_page_flag_set(m, PG_REFERENCED);
2044 return pmap_remove_entry(pmap, m, va, info);
2046 return pmap_unuse_pt(pmap, va, NULL, info);
2055 * Remove a single page from a process address space.
2057 * This function may not be called from an interrupt if the pmap is
2061 pmap_remove_page(struct pmap *pmap, vm_offset_t va, pmap_inval_info_t info)
2065 pte = pmap_pte(pmap, va);
2068 if ((*pte & PG_V) == 0)
2070 pmap_remove_pte(pmap, pte, va, info);
2076 * Remove the given range of addresses from the specified map.
2078 * It is assumed that the start and end are properly
2079 * rounded to the page size.
2081 * This function may not be called from an interrupt if the pmap is
2085 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
2087 vm_offset_t va_next;
2088 pml4_entry_t *pml4e;
2090 pd_entry_t ptpaddr, *pde;
2092 struct pmap_inval_info info;
2097 if (pmap->pm_stats.resident_count == 0)
2100 pmap_inval_init(&info);
2103 * special handling of removing one page. a very
2104 * common operation and easy to short circuit some
2107 if (sva + PAGE_SIZE == eva) {
2108 pde = pmap_pde(pmap, sva);
2109 if (pde && (*pde & PG_PS) == 0) {
2110 pmap_remove_page(pmap, sva, &info);
2111 pmap_inval_flush(&info);
2116 for (; sva < eva; sva = va_next) {
2117 pml4e = pmap_pml4e(pmap, sva);
2118 if ((*pml4e & PG_V) == 0) {
2119 va_next = (sva + NBPML4) & ~PML4MASK;
2125 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
2126 if ((*pdpe & PG_V) == 0) {
2127 va_next = (sva + NBPDP) & ~PDPMASK;
2134 * Calculate index for next page table.
2136 va_next = (sva + NBPDR) & ~PDRMASK;
2140 pde = pmap_pdpe_to_pde(pdpe, sva);
2144 * Weed out invalid mappings.
2150 * Check for large page.
2152 if ((ptpaddr & PG_PS) != 0) {
2153 /* JG FreeBSD has more complex treatment here */
2154 pmap_inval_add(&info, pmap, -1);
2156 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2161 * Limit our scan to either the end of the va represented
2162 * by the current page table page, or to the end of the
2163 * range being removed.
2169 * NOTE: pmap_remove_pte() can block.
2171 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2175 if (pmap_remove_pte(pmap, pte, sva, &info))
2179 pmap_inval_flush(&info);
2185 * Removes this physical page from all physical maps in which it resides.
2186 * Reflects back modify bits to the pager.
2188 * This routine may not be called from an interrupt.
2192 pmap_remove_all(vm_page_t m)
2194 struct pmap_inval_info info;
2195 pt_entry_t *pte, tpte;
2198 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2201 pmap_inval_init(&info);
2203 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2204 KKASSERT(pv->pv_pmap->pm_stats.resident_count > 0);
2205 --pv->pv_pmap->pm_stats.resident_count;
2207 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2208 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
2209 tpte = pte_load_clear(pte);
2212 pv->pv_pmap->pm_stats.wired_count--;
2215 vm_page_flag_set(m, PG_REFERENCED);
2218 * Update the vm_page_t clean and reference bits.
2221 #if defined(PMAP_DIAGNOSTIC)
2222 if (pmap_nw_modified(tpte)) {
2224 "pmap_remove_all: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2228 if (pmap_track_modified(pv->pv_va))
2231 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2232 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2233 ++pv->pv_pmap->pm_generation;
2234 m->md.pv_list_count--;
2235 KKASSERT(m->md.pv_list_count >= 0);
2236 if (TAILQ_EMPTY(&m->md.pv_list))
2237 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2238 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem, &info);
2242 KKASSERT((m->flags & (PG_MAPPED|PG_WRITEABLE)) == 0);
2243 pmap_inval_flush(&info);
2249 * Set the physical protection on the specified range of this map
2252 * This function may not be called from an interrupt if the map is
2253 * not the kernel_pmap.
2256 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
2258 vm_offset_t va_next;
2259 pml4_entry_t *pml4e;
2261 pd_entry_t ptpaddr, *pde;
2263 pmap_inval_info info;
2265 /* JG review for NX */
2270 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
2271 pmap_remove(pmap, sva, eva);
2275 if (prot & VM_PROT_WRITE)
2278 pmap_inval_init(&info);
2280 for (; sva < eva; sva = va_next) {
2282 pml4e = pmap_pml4e(pmap, sva);
2283 if ((*pml4e & PG_V) == 0) {
2284 va_next = (sva + NBPML4) & ~PML4MASK;
2290 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
2291 if ((*pdpe & PG_V) == 0) {
2292 va_next = (sva + NBPDP) & ~PDPMASK;
2298 va_next = (sva + NBPDR) & ~PDRMASK;
2302 pde = pmap_pdpe_to_pde(pdpe, sva);
2306 * Check for large page.
2308 if ((ptpaddr & PG_PS) != 0) {
2309 pmap_inval_add(&info, pmap, -1);
2310 *pde &= ~(PG_M|PG_RW);
2311 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2316 * Weed out invalid mappings. Note: we assume that the page
2317 * directory table is always allocated, and in kernel virtual.
2325 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2327 pt_entry_t obits, pbits;
2331 * XXX non-optimal. Note also that there can be
2332 * no pmap_inval_flush() calls until after we modify
2333 * ptbase[sindex] (or otherwise we have to do another
2334 * pmap_inval_add() call).
2336 pmap_inval_add(&info, pmap, sva);
2337 obits = pbits = *pte;
2338 if ((pbits & PG_V) == 0)
2340 if (pbits & PG_MANAGED) {
2343 m = PHYS_TO_VM_PAGE(pbits & PG_FRAME);
2344 vm_page_flag_set(m, PG_REFERENCED);
2348 if (pmap_track_modified(sva)) {
2350 m = PHYS_TO_VM_PAGE(pbits & PG_FRAME);
2359 if (pbits != obits) {
2364 pmap_inval_flush(&info);
2368 * Insert the given physical page (p) at
2369 * the specified virtual address (v) in the
2370 * target physical map with the protection requested.
2372 * If specified, the page will be wired down, meaning
2373 * that the related pte can not be reclaimed.
2375 * NB: This is the only routine which MAY NOT lazy-evaluate
2376 * or lose information. That is, this routine must actually
2377 * insert this page into the given map NOW.
2380 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
2387 pt_entry_t origpte, newpte;
2389 pmap_inval_info info;
2394 va = trunc_page(va);
2395 #ifdef PMAP_DIAGNOSTIC
2397 panic("pmap_enter: toobig");
2398 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
2399 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%lx)", va);
2401 if (va < UPT_MAX_ADDRESS && pmap == &kernel_pmap) {
2402 kprintf("Warning: pmap_enter called on UVA with kernel_pmap\n");
2404 db_print_backtrace();
2407 if (va >= UPT_MAX_ADDRESS && pmap != &kernel_pmap) {
2408 kprintf("Warning: pmap_enter called on KVA without kernel_pmap\n");
2410 db_print_backtrace();
2415 * In the case that a page table page is not
2416 * resident, we are creating it here.
2418 if (va < VM_MAX_USER_ADDRESS)
2419 mpte = pmap_allocpte(pmap, va);
2423 pmap_inval_init(&info);
2424 pde = pmap_pde(pmap, va);
2425 if (pde != NULL && (*pde & PG_V) != 0) {
2426 if ((*pde & PG_PS) != 0)
2427 panic("pmap_enter: attempted pmap_enter on 2MB page");
2428 pte = pmap_pde_to_pte(pde, va);
2430 panic("pmap_enter: invalid page directory va=%#lx", va);
2432 KKASSERT(pte != NULL);
2433 pa = VM_PAGE_TO_PHYS(m);
2435 opa = origpte & PG_FRAME;
2438 * Mapping has not changed, must be protection or wiring change.
2440 if (origpte && (opa == pa)) {
2442 * Wiring change, just update stats. We don't worry about
2443 * wiring PT pages as they remain resident as long as there
2444 * are valid mappings in them. Hence, if a user page is wired,
2445 * the PT page will be also.
2447 if (wired && ((origpte & PG_W) == 0))
2448 pmap->pm_stats.wired_count++;
2449 else if (!wired && (origpte & PG_W))
2450 pmap->pm_stats.wired_count--;
2452 #if defined(PMAP_DIAGNOSTIC)
2453 if (pmap_nw_modified(origpte)) {
2455 "pmap_enter: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2461 * Remove the extra pte reference. Note that we cannot
2462 * optimize the RO->RW case because we have adjusted the
2463 * wiring count above and may need to adjust the wiring
2470 * We might be turning off write access to the page,
2471 * so we go ahead and sense modify status.
2473 if (origpte & PG_MANAGED) {
2474 if ((origpte & PG_M) && pmap_track_modified(va)) {
2476 om = PHYS_TO_VM_PAGE(opa);
2480 KKASSERT(m->flags & PG_MAPPED);
2485 * Mapping has changed, invalidate old range and fall through to
2486 * handle validating new mapping.
2490 err = pmap_remove_pte(pmap, pte, va, &info);
2492 panic("pmap_enter: pte vanished, va: 0x%lx", va);
2496 * Enter on the PV list if part of our managed memory. Note that we
2497 * raise IPL while manipulating pv_table since pmap_enter can be
2498 * called at interrupt time.
2500 if (pmap_initialized &&
2501 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2502 pmap_insert_entry(pmap, va, mpte, m);
2504 vm_page_flag_set(m, PG_MAPPED);
2508 * Increment counters
2510 ++pmap->pm_stats.resident_count;
2512 pmap->pm_stats.wired_count++;
2516 * Now validate mapping with desired protection/wiring.
2518 newpte = (pt_entry_t) (pa | pte_prot(pmap, prot) | PG_V);
2522 if (va < VM_MAX_USER_ADDRESS)
2524 if (pmap == &kernel_pmap)
2528 * if the mapping or permission bits are different, we need
2529 * to update the pte.
2531 if ((origpte & ~(PG_M|PG_A)) != newpte) {
2532 pmap_inval_add(&info, pmap, va);
2533 *pte = newpte | PG_A;
2535 vm_page_flag_set(m, PG_WRITEABLE);
2537 KKASSERT((newpte & PG_MANAGED) == 0 || (m->flags & PG_MAPPED));
2538 pmap_inval_flush(&info);
2542 * This code works like pmap_enter() but assumes VM_PROT_READ and not-wired.
2543 * This code also assumes that the pmap has no pre-existing entry for this
2546 * This code currently may only be used on user pmaps, not kernel_pmap.
2549 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m)
2554 vm_pindex_t ptepindex;
2556 pmap_inval_info info;
2558 pmap_inval_init(&info);
2560 if (va < UPT_MAX_ADDRESS && pmap == &kernel_pmap) {
2561 kprintf("Warning: pmap_enter_quick called on UVA with kernel_pmap\n");
2563 db_print_backtrace();
2566 if (va >= UPT_MAX_ADDRESS && pmap != &kernel_pmap) {
2567 kprintf("Warning: pmap_enter_quick called on KVA without kernel_pmap\n");
2569 db_print_backtrace();
2573 KKASSERT(va < UPT_MIN_ADDRESS); /* assert used on user pmaps only */
2576 * Calculate the page table page (mpte), allocating it if necessary.
2578 * A held page table page (mpte), or NULL, is passed onto the
2579 * section following.
2581 if (va < VM_MAX_USER_ADDRESS) {
2583 * Calculate pagetable page index
2585 ptepindex = pmap_pde_pindex(va);
2589 * Get the page directory entry
2591 ptepa = pmap_pde(pmap, va);
2594 * If the page table page is mapped, we just increment
2595 * the hold count, and activate it.
2597 if (ptepa && (*ptepa & PG_V) != 0) {
2599 panic("pmap_enter_quick: unexpected mapping into 2MB page");
2600 // if (pmap->pm_ptphint &&
2601 // (pmap->pm_ptphint->pindex == ptepindex)) {
2602 // mpte = pmap->pm_ptphint;
2604 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
2605 pmap->pm_ptphint = mpte;
2610 mpte = _pmap_allocpte(pmap, ptepindex);
2612 } while (mpte == NULL);
2615 /* this code path is not yet used */
2619 * With a valid (and held) page directory page, we can just use
2620 * vtopte() to get to the pte. If the pte is already present
2621 * we do not disturb it.
2626 pmap_unwire_pte_hold(pmap, va, mpte, &info);
2627 pa = VM_PAGE_TO_PHYS(m);
2628 KKASSERT(((*pte ^ pa) & PG_FRAME) == 0);
2633 * Enter on the PV list if part of our managed memory
2635 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2636 pmap_insert_entry(pmap, va, mpte, m);
2637 vm_page_flag_set(m, PG_MAPPED);
2641 * Increment counters
2643 ++pmap->pm_stats.resident_count;
2645 pa = VM_PAGE_TO_PHYS(m);
2648 * Now validate mapping with RO protection
2650 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2651 *pte = pa | PG_V | PG_U;
2653 *pte = pa | PG_V | PG_U | PG_MANAGED;
2654 /* pmap_inval_add(&info, pmap, va); shouldn't be needed inval->valid */
2655 pmap_inval_flush(&info);
2659 * Make a temporary mapping for a physical address. This is only intended
2660 * to be used for panic dumps.
2662 /* JG Needed on amd64? */
2664 pmap_kenter_temporary(vm_paddr_t pa, int i)
2666 pmap_kenter((vm_offset_t)crashdumpmap + (i * PAGE_SIZE), pa);
2667 return ((void *)crashdumpmap);
2670 #define MAX_INIT_PT (96)
2673 * This routine preloads the ptes for a given object into the specified pmap.
2674 * This eliminates the blast of soft faults on process startup and
2675 * immediately after an mmap.
2677 static int pmap_object_init_pt_callback(vm_page_t p, void *data);
2680 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2681 vm_object_t object, vm_pindex_t pindex,
2682 vm_size_t size, int limit)
2684 struct rb_vm_page_scan_info info;
2689 * We can't preinit if read access isn't set or there is no pmap
2692 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2696 * We can't preinit if the pmap is not the current pmap
2698 lp = curthread->td_lwp;
2699 if (lp == NULL || pmap != vmspace_pmap(lp->lwp_vmspace))
2702 psize = amd64_btop(size);
2704 if ((object->type != OBJT_VNODE) ||
2705 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2706 (object->resident_page_count > MAX_INIT_PT))) {
2710 if (psize + pindex > object->size) {
2711 if (object->size < pindex)
2713 psize = object->size - pindex;
2720 * Use a red-black scan to traverse the requested range and load
2721 * any valid pages found into the pmap.
2723 * We cannot safely scan the object's memq unless we are in a
2724 * critical section since interrupts can remove pages from objects.
2726 info.start_pindex = pindex;
2727 info.end_pindex = pindex + psize - 1;
2734 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2735 pmap_object_init_pt_callback, &info);
2741 pmap_object_init_pt_callback(vm_page_t p, void *data)
2743 struct rb_vm_page_scan_info *info = data;
2744 vm_pindex_t rel_index;
2746 * don't allow an madvise to blow away our really
2747 * free pages allocating pv entries.
2749 if ((info->limit & MAP_PREFAULT_MADVISE) &&
2750 vmstats.v_free_count < vmstats.v_free_reserved) {
2753 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2754 (p->busy == 0) && (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2755 if ((p->queue - p->pc) == PQ_CACHE)
2756 vm_page_deactivate(p);
2758 rel_index = p->pindex - info->start_pindex;
2759 pmap_enter_quick(info->pmap,
2760 info->addr + amd64_ptob(rel_index), p);
2767 * pmap_prefault provides a quick way of clustering pagefaults into a
2768 * processes address space. It is a "cousin" of pmap_object_init_pt,
2769 * except it runs at page fault time instead of mmap time.
2773 #define PAGEORDER_SIZE (PFBAK+PFFOR)
2775 static int pmap_prefault_pageorder[] = {
2776 -PAGE_SIZE, PAGE_SIZE,
2777 -2 * PAGE_SIZE, 2 * PAGE_SIZE,
2778 -3 * PAGE_SIZE, 3 * PAGE_SIZE,
2779 -4 * PAGE_SIZE, 4 * PAGE_SIZE
2783 pmap_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry)
2794 * We do not currently prefault mappings that use virtual page
2795 * tables. We do not prefault foreign pmaps.
2797 if (entry->maptype == VM_MAPTYPE_VPAGETABLE)
2799 lp = curthread->td_lwp;
2800 if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
2803 object = entry->object.vm_object;
2805 starta = addra - PFBAK * PAGE_SIZE;
2806 if (starta < entry->start)
2807 starta = entry->start;
2808 else if (starta > addra)
2812 * critical section protection is required to maintain the
2813 * page/object association, interrupts can free pages and remove
2814 * them from their objects.
2817 for (i = 0; i < PAGEORDER_SIZE; i++) {
2818 vm_object_t lobject;
2822 addr = addra + pmap_prefault_pageorder[i];
2823 if (addr > addra + (PFFOR * PAGE_SIZE))
2826 if (addr < starta || addr >= entry->end)
2829 pde = pmap_pde(pmap, addr);
2830 if (pde == NULL || *pde == 0)
2837 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2840 for (m = vm_page_lookup(lobject, pindex);
2841 (!m && (lobject->type == OBJT_DEFAULT) &&
2842 (lobject->backing_object));
2843 lobject = lobject->backing_object
2845 if (lobject->backing_object_offset & PAGE_MASK)
2847 pindex += (lobject->backing_object_offset >> PAGE_SHIFT);
2848 m = vm_page_lookup(lobject->backing_object, pindex);
2852 * give-up when a page is not in memory
2857 if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2859 (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2861 if ((m->queue - m->pc) == PQ_CACHE) {
2862 vm_page_deactivate(m);
2865 pmap_enter_quick(pmap, addr, m);
2873 * Routine: pmap_change_wiring
2874 * Function: Change the wiring attribute for a map/virtual-address
2876 * In/out conditions:
2877 * The mapping must already exist in the pmap.
2880 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2887 pte = pmap_pte(pmap, va);
2889 if (wired && !pmap_pte_w(pte))
2890 pmap->pm_stats.wired_count++;
2891 else if (!wired && pmap_pte_w(pte))
2892 pmap->pm_stats.wired_count--;
2895 * Wiring is not a hardware characteristic so there is no need to
2896 * invalidate TLB. However, in an SMP environment we must use
2897 * a locked bus cycle to update the pte (if we are not using
2898 * the pmap_inval_*() API that is)... it's ok to do this for simple
2903 atomic_set_long(pte, PG_W);
2905 atomic_clear_long(pte, PG_W);
2908 atomic_set_long_nonlocked(pte, PG_W);
2910 atomic_clear_long_nonlocked(pte, PG_W);
2917 * Copy the range specified by src_addr/len
2918 * from the source map to the range dst_addr/len
2919 * in the destination map.
2921 * This routine is only advisory and need not do anything.
2924 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2925 vm_size_t len, vm_offset_t src_addr)
2927 pmap_inval_info info;
2929 vm_offset_t end_addr = src_addr + len;
2931 pd_entry_t src_frame, dst_frame;
2934 if (dst_addr != src_addr)
2937 * XXX BUGGY. Amoung other things srcmpte is assumed to remain
2938 * valid through blocking calls, and that's just not going to
2947 src_frame = src_pmap->pm_pdir[PTDPTDI] & PG_FRAME;
2948 if (src_frame != (PTDpde & PG_FRAME)) {
2952 dst_frame = dst_pmap->pm_pdir[PTDPTDI] & PG_FRAME;
2953 if (dst_frame != (APTDpde & PG_FRAME)) {
2954 APTDpde = (pd_entry_t) (dst_frame | PG_RW | PG_V);
2955 /* The page directory is not shared between CPUs */
2959 pmap_inval_init(&info);
2960 pmap_inval_add(&info, dst_pmap, -1);
2961 pmap_inval_add(&info, src_pmap, -1);
2964 * critical section protection is required to maintain the page/object
2965 * association, interrupts can free pages and remove them from
2969 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
2970 pt_entry_t *src_pte, *dst_pte;
2971 vm_page_t dstmpte, srcmpte;
2972 vm_offset_t srcptepaddr;
2973 vm_pindex_t ptepindex;
2975 if (addr >= UPT_MIN_ADDRESS)
2976 panic("pmap_copy: invalid to pmap_copy page tables\n");
2979 * Don't let optional prefaulting of pages make us go
2980 * way below the low water mark of free pages or way
2981 * above high water mark of used pv entries.
2983 if (vmstats.v_free_count < vmstats.v_free_reserved ||
2984 pv_entry_count > pv_entry_high_water)
2987 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
2988 ptepindex = addr >> PDRSHIFT;
2991 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex];
2993 if (srcptepaddr == 0)
2996 if (srcptepaddr & PG_PS) {
2998 if (dst_pmap->pm_pdir[ptepindex] == 0) {
2999 dst_pmap->pm_pdir[ptepindex] = (pd_entry_t) srcptepaddr;
3000 dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
3006 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
3007 if ((srcmpte == NULL) || (srcmpte->hold_count == 0) ||
3008 (srcmpte->flags & PG_BUSY)) {
3012 if (pdnxt > end_addr)
3015 src_pte = vtopte(addr);
3017 dst_pte = avtopte(addr);
3019 while (addr < pdnxt) {
3024 * we only virtual copy managed pages
3026 if ((ptetemp & PG_MANAGED) != 0) {
3028 * We have to check after allocpte for the
3029 * pte still being around... allocpte can
3032 * pmap_allocpte() can block. If we lose
3033 * our page directory mappings we stop.
3035 dstmpte = pmap_allocpte(dst_pmap, addr);
3038 if (src_frame != (PTDpde & PG_FRAME) ||
3039 dst_frame != (APTDpde & PG_FRAME)
3041 kprintf("WARNING: pmap_copy: detected and corrected race\n");
3042 pmap_unwire_pte_hold(dst_pmap, dstmpte, &info);
3044 } else if ((*dst_pte == 0) &&
3045 (ptetemp = *src_pte) != 0 &&
3046 (ptetemp & PG_MANAGED)) {
3048 * Clear the modified and
3049 * accessed (referenced) bits
3052 m = PHYS_TO_VM_PAGE(ptetemp);
3053 *dst_pte = ptetemp & ~(PG_M | PG_A);
3054 ++dst_pmap->pm_stats.resident_count;
3055 pmap_insert_entry(dst_pmap, addr,
3057 KKASSERT(m->flags & PG_MAPPED);
3059 kprintf("WARNING: pmap_copy: dst_pte race detected and corrected\n");
3060 pmap_unwire_pte_hold(dst_pmap, dstmpte, &info);
3064 if (dstmpte->hold_count >= srcmpte->hold_count)
3074 pmap_inval_flush(&info);
3081 * Zero the specified physical page.
3083 * This function may be called from an interrupt and no locking is
3087 pmap_zero_page(vm_paddr_t phys)
3089 vm_offset_t va = PHYS_TO_DMAP(phys);
3091 pagezero((void *)va);
3095 * pmap_page_assertzero:
3097 * Assert that a page is empty, panic if it isn't.
3100 pmap_page_assertzero(vm_paddr_t phys)
3102 struct mdglobaldata *gd = mdcpu;
3106 vm_offset_t virt = PHYS_TO_DMAP(phys);
3108 for (i = 0; i < PAGE_SIZE; i += sizeof(int)) {
3109 if (*(int *)((char *)virt + i) != 0) {
3110 panic("pmap_page_assertzero() @ %p not zero!\n",
3120 * Zero part of a physical page by mapping it into memory and clearing
3121 * its contents with bzero.
3123 * off and size may not cover an area beyond a single hardware page.
3126 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
3128 struct mdglobaldata *gd = mdcpu;
3131 vm_offset_t virt = PHYS_TO_DMAP(phys);
3132 bzero((char *)virt + off, size);
3139 * Copy the physical page from the source PA to the target PA.
3140 * This function may be called from an interrupt. No locking
3144 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
3146 vm_offset_t src_virt, dst_virt;
3149 src_virt = PHYS_TO_DMAP(src);
3150 dst_virt = PHYS_TO_DMAP(dst);
3151 bcopy(src_virt, dst_virt, PAGE_SIZE);
3156 * pmap_copy_page_frag:
3158 * Copy the physical page from the source PA to the target PA.
3159 * This function may be called from an interrupt. No locking
3163 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
3165 vm_offset_t src_virt, dst_virt;
3168 src_virt = PHYS_TO_DMAP(src);
3169 dst_virt = PHYS_TO_DMAP(dst);
3170 bcopy((char *)src_virt + (src & PAGE_MASK),
3171 (char *)dst_virt + (dst & PAGE_MASK),
3177 * Returns true if the pmap's pv is one of the first
3178 * 16 pvs linked to from this page. This count may
3179 * be changed upwards or downwards in the future; it
3180 * is only necessary that true be returned for a small
3181 * subset of pmaps for proper page aging.
3184 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
3189 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3194 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3195 if (pv->pv_pmap == pmap) {
3208 * Remove all pages from specified address space
3209 * this aids process exit speeds. Also, this code
3210 * is special cased for current process only, but
3211 * can have the more generic (and slightly slower)
3212 * mode enabled. This is much faster than pmap_remove
3213 * in the case of running down an entire address space.
3216 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
3219 pt_entry_t *pte, tpte;
3222 pmap_inval_info info;
3224 int save_generation;
3226 lp = curthread->td_lwp;
3227 if (lp && pmap == vmspace_pmap(lp->lwp_vmspace))
3232 pmap_inval_init(&info);
3234 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
3235 if (pv->pv_va >= eva || pv->pv_va < sva) {
3236 npv = TAILQ_NEXT(pv, pv_plist);
3240 KKASSERT(pmap == pv->pv_pmap);
3243 pte = vtopte(pv->pv_va);
3245 pte = pmap_pte_quick(pmap, pv->pv_va);
3246 if (pmap->pm_active)
3247 pmap_inval_add(&info, pmap, pv->pv_va);
3250 * We cannot remove wired pages from a process' mapping
3254 npv = TAILQ_NEXT(pv, pv_plist);
3257 tpte = pte_load_clear(pte);
3259 m = PHYS_TO_VM_PAGE(tpte & PG_FRAME);
3261 KASSERT(m < &vm_page_array[vm_page_array_size],
3262 ("pmap_remove_pages: bad tpte %lx", tpte));
3264 KKASSERT(pmap->pm_stats.resident_count > 0);
3265 --pmap->pm_stats.resident_count;
3268 * Update the vm_page_t clean and reference bits.
3274 npv = TAILQ_NEXT(pv, pv_plist);
3275 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
3276 save_generation = ++pmap->pm_generation;
3278 m->md.pv_list_count--;
3279 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3280 if (TAILQ_EMPTY(&m->md.pv_list))
3281 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
3283 pmap_unuse_pt(pmap, pv->pv_va, pv->pv_ptem, &info);
3287 * Restart the scan if we blocked during the unuse or free
3288 * calls and other removals were made.
3290 if (save_generation != pmap->pm_generation) {
3291 kprintf("Warning: pmap_remove_pages race-A avoided\n");
3292 pv = TAILQ_FIRST(&pmap->pm_pvlist);
3295 pmap_inval_flush(&info);
3300 * pmap_testbit tests bits in pte's
3301 * note that the testbit/clearbit routines are inline,
3302 * and a lot of things compile-time evaluate.
3305 pmap_testbit(vm_page_t m, int bit)
3310 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3313 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
3318 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3320 * if the bit being tested is the modified bit, then
3321 * mark clean_map and ptes as never
3324 if (bit & (PG_A|PG_M)) {
3325 if (!pmap_track_modified(pv->pv_va))
3329 #if defined(PMAP_DIAGNOSTIC)
3330 if (pv->pv_pmap == NULL) {
3331 kprintf("Null pmap (tb) at va: 0x%lx\n", pv->pv_va);
3335 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3346 * this routine is used to modify bits in ptes
3348 static __inline void
3349 pmap_clearbit(vm_page_t m, int bit)
3351 struct pmap_inval_info info;
3356 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3359 pmap_inval_init(&info);
3363 * Loop over all current mappings setting/clearing as appropos If
3364 * setting RO do we need to clear the VAC?
3366 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3368 * don't write protect pager mappings
3371 if (!pmap_track_modified(pv->pv_va))
3375 #if defined(PMAP_DIAGNOSTIC)
3376 if (pv->pv_pmap == NULL) {
3377 kprintf("Null pmap (cb) at va: 0x%lx\n", pv->pv_va);
3383 * Careful here. We can use a locked bus instruction to
3384 * clear PG_A or PG_M safely but we need to synchronize
3385 * with the target cpus when we mess with PG_RW.
3387 * We do not have to force synchronization when clearing
3388 * PG_M even for PTEs generated via virtual memory maps,
3389 * because the virtual kernel will invalidate the pmap
3390 * entry when/if it needs to resynchronize the Modify bit.
3393 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
3394 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3401 atomic_clear_long(pte, PG_M|PG_RW);
3404 * The cpu may be trying to set PG_M
3405 * simultaniously with our clearing
3408 if (!atomic_cmpset_long(pte, pbits,
3412 } else if (bit == PG_M) {
3414 * We could also clear PG_RW here to force
3415 * a fault on write to redetect PG_M for
3416 * virtual kernels, but it isn't necessary
3417 * since virtual kernels invalidate the pte
3418 * when they clear the VPTE_M bit in their
3419 * virtual page tables.
3421 atomic_clear_long(pte, PG_M);
3423 atomic_clear_long(pte, bit);
3427 pmap_inval_flush(&info);
3432 * pmap_page_protect:
3434 * Lower the permission for all mappings to a given page.
3437 pmap_page_protect(vm_page_t m, vm_prot_t prot)
3439 /* JG NX support? */
3440 if ((prot & VM_PROT_WRITE) == 0) {
3441 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
3442 pmap_clearbit(m, PG_RW);
3443 vm_page_flag_clear(m, PG_WRITEABLE);
3451 pmap_phys_address(vm_pindex_t ppn)
3453 return (amd64_ptob(ppn));
3457 * pmap_ts_referenced:
3459 * Return a count of reference bits for a page, clearing those bits.
3460 * It is not necessary for every reference bit to be cleared, but it
3461 * is necessary that 0 only be returned when there are truly no
3462 * reference bits set.
3464 * XXX: The exact number of bits to check and clear is a matter that
3465 * should be tested and standardized at some point in the future for
3466 * optimal aging of shared pages.
3469 pmap_ts_referenced(vm_page_t m)
3471 pv_entry_t pv, pvf, pvn;
3475 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3480 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3485 pvn = TAILQ_NEXT(pv, pv_list);
3487 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3489 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3491 if (!pmap_track_modified(pv->pv_va))
3494 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3496 if (pte && (*pte & PG_A)) {
3498 atomic_clear_long(pte, PG_A);
3500 atomic_clear_long_nonlocked(pte, PG_A);
3507 } while ((pv = pvn) != NULL && pv != pvf);
3517 * Return whether or not the specified physical page was modified
3518 * in any physical maps.
3521 pmap_is_modified(vm_page_t m)
3523 return pmap_testbit(m, PG_M);
3527 * Clear the modify bits on the specified physical page.
3530 pmap_clear_modify(vm_page_t m)
3532 pmap_clearbit(m, PG_M);
3536 * pmap_clear_reference:
3538 * Clear the reference bit on the specified physical page.
3541 pmap_clear_reference(vm_page_t m)
3543 pmap_clearbit(m, PG_A);
3547 * Miscellaneous support routines follow
3551 i386_protection_init(void)
3555 /* JG NX support may go here; No VM_PROT_EXECUTE ==> set NX bit */
3556 kp = protection_codes;
3557 for (prot = 0; prot < 8; prot++) {
3559 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE:
3561 * Read access is also 0. There isn't any execute bit,
3562 * so just make it readable.
3564 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE:
3565 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE:
3566 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE:
3569 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE:
3570 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE:
3571 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE:
3572 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
3580 * Map a set of physical memory pages into the kernel virtual
3581 * address space. Return a pointer to where it is mapped. This
3582 * routine is intended to be used for mapping device memory,
3585 * NOTE: we can't use pgeflag unless we invalidate the pages one at
3589 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
3591 vm_offset_t va, tmpva, offset;
3594 offset = pa & PAGE_MASK;
3595 size = roundup(offset + size, PAGE_SIZE);
3597 va = kmem_alloc_nofault(&kernel_map, size);
3599 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3601 pa = pa & ~PAGE_MASK;
3602 for (tmpva = va; size > 0;) {
3603 pte = vtopte(tmpva);
3604 *pte = pa | PG_RW | PG_V; /* | pgeflag; */
3612 return ((void *)(va + offset));
3616 pmap_mapdev_uncacheable(vm_paddr_t pa, vm_size_t size)
3618 vm_offset_t va, tmpva, offset;
3621 offset = pa & PAGE_MASK;
3622 size = roundup(offset + size, PAGE_SIZE);
3624 va = kmem_alloc_nofault(&kernel_map, size);
3626 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3628 pa = pa & ~PAGE_MASK;
3629 for (tmpva = va; size > 0;) {
3630 pte = vtopte(tmpva);
3631 *pte = pa | PG_RW | PG_V | PG_N; /* | pgeflag; */
3639 return ((void *)(va + offset));
3643 pmap_unmapdev(vm_offset_t va, vm_size_t size)
3645 vm_offset_t base, offset;
3647 base = va & ~PAGE_MASK;
3648 offset = va & PAGE_MASK;
3649 size = roundup(offset + size, PAGE_SIZE);
3650 pmap_qremove(va, size >> PAGE_SHIFT);
3651 kmem_free(&kernel_map, base, size);
3655 * perform the pmap work for mincore
3658 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3660 pt_entry_t *ptep, pte;
3664 ptep = pmap_pte(pmap, addr);
3669 if ((pte = *ptep) != 0) {
3672 val = MINCORE_INCORE;
3673 if ((pte & PG_MANAGED) == 0)
3676 pa = pte & PG_FRAME;
3678 m = PHYS_TO_VM_PAGE(pa);
3684 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3686 * Modified by someone
3688 else if (m->dirty || pmap_is_modified(m))
3689 val |= MINCORE_MODIFIED_OTHER;
3694 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3697 * Referenced by someone
3699 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3700 val |= MINCORE_REFERENCED_OTHER;
3701 vm_page_flag_set(m, PG_REFERENCED);
3708 * Replace p->p_vmspace with a new one. If adjrefs is non-zero the new
3709 * vmspace will be ref'd and the old one will be deref'd.
3711 * The vmspace for all lwps associated with the process will be adjusted
3712 * and cr3 will be reloaded if any lwp is the current lwp.
3715 pmap_replacevm(struct proc *p, struct vmspace *newvm, int adjrefs)
3717 struct vmspace *oldvm;
3721 oldvm = p->p_vmspace;
3722 if (oldvm != newvm) {
3723 p->p_vmspace = newvm;
3724 KKASSERT(p->p_nthreads == 1);
3725 lp = RB_ROOT(&p->p_lwp_tree);
3726 pmap_setlwpvm(lp, newvm);
3728 sysref_get(&newvm->vm_sysref);
3729 sysref_put(&oldvm->vm_sysref);
3736 * Set the vmspace for a LWP. The vmspace is almost universally set the
3737 * same as the process vmspace, but virtual kernels need to swap out contexts
3738 * on a per-lwp basis.
3741 pmap_setlwpvm(struct lwp *lp, struct vmspace *newvm)
3743 struct vmspace *oldvm;
3747 oldvm = lp->lwp_vmspace;
3749 if (oldvm != newvm) {
3750 lp->lwp_vmspace = newvm;
3751 if (curthread->td_lwp == lp) {
3752 pmap = vmspace_pmap(newvm);
3754 atomic_set_int(&pmap->pm_active, 1 << mycpu->gd_cpuid);
3756 pmap->pm_active |= 1;
3758 #if defined(SWTCH_OPTIM_STATS)
3761 curthread->td_pcb->pcb_cr3 = vtophys(pmap->pm_pml4);
3762 load_cr3(curthread->td_pcb->pcb_cr3);
3763 pmap = vmspace_pmap(oldvm);
3765 atomic_clear_int(&pmap->pm_active,
3766 1 << mycpu->gd_cpuid);
3768 pmap->pm_active &= ~1;
3776 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3779 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3783 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3790 static void pads (pmap_t pm);
3791 void pmap_pvdump (vm_paddr_t pa);
3793 /* print address space of pmap*/
3801 if (pm == &kernel_pmap)
3804 for (i = 0; i < NPDEPG; i++) {
3812 pmap_pvdump(vm_paddr_t pa)
3817 kprintf("pa %08llx", (long long)pa);
3818 m = PHYS_TO_VM_PAGE(pa);
3819 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3821 kprintf(" -> pmap %p, va %x, flags %x",
3822 (void *)pv->pv_pmap, pv->pv_va, pv->pv_flags);
3824 kprintf(" -> pmap %p, va %x", (void *)pv->pv_pmap, pv->pv_va);