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
144 * Get PDEs and PTEs for user/kernel address space
147 #define pmap_pde(m, v) (&((m)->pm_pdir[(vm_offset_t)(v) >> PDRSHIFT]))
149 static pd_entry_t *pmap_pde(pmap_t pmap, vm_offset_t va);
150 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
152 #define pmap_pde_v(pte) ((*(pd_entry_t *)pte & PG_V) != 0)
153 #define pmap_pte_w(pte) ((*(pt_entry_t *)pte & PG_W) != 0)
154 #define pmap_pte_m(pte) ((*(pt_entry_t *)pte & PG_M) != 0)
155 #define pmap_pte_u(pte) ((*(pt_entry_t *)pte & PG_A) != 0)
156 #define pmap_pte_v(pte) ((*(pt_entry_t *)pte & PG_V) != 0)
160 * Given a map and a machine independent protection code,
161 * convert to a vax protection code.
163 #define pte_prot(m, p) \
164 (protection_codes[p & (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)])
165 static int protection_codes[8];
167 struct pmap kernel_pmap;
168 static TAILQ_HEAD(,pmap) pmap_list = TAILQ_HEAD_INITIALIZER(pmap_list);
170 vm_paddr_t avail_start; /* PA of first available physical page */
171 vm_paddr_t avail_end; /* PA of last available physical page */
172 vm_offset_t virtual_start; /* VA of first avail page (after kernel bss) */
173 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
174 vm_offset_t KvaStart; /* VA start of KVA space */
175 vm_offset_t KvaEnd; /* VA end of KVA space (non-inclusive) */
176 vm_offset_t KvaSize; /* max size of kernel virtual address space */
177 static boolean_t pmap_initialized = FALSE; /* Has pmap_init completed? */
178 static int pgeflag; /* PG_G or-in */
179 static int pseflag; /* PG_PS or-in */
181 static vm_object_t kptobj;
184 static vm_paddr_t dmaplimit;
186 vm_offset_t kernel_vm_end;
188 static uint64_t KPDphys; /* phys addr of kernel level 2 */
189 uint64_t KPDPphys; /* phys addr of kernel level 3 */
190 uint64_t KPML4phys; /* phys addr of kernel level 4 */
192 static uint64_t DMPDphys; /* phys addr of direct mapped level 2 */
193 static uint64_t DMPDPphys; /* phys addr of direct mapped level 3 */
196 * Data for the pv entry allocation mechanism
198 static vm_zone_t pvzone;
199 static struct vm_zone pvzone_store;
200 static struct vm_object pvzone_obj;
201 static int pv_entry_count=0, pv_entry_max=0, pv_entry_high_water=0;
202 static int pmap_pagedaemon_waken = 0;
203 static struct pv_entry *pvinit;
206 * All those kernel PT submaps that BSD is so fond of
208 pt_entry_t *CMAP1 = 0, *ptmmap;
209 caddr_t CADDR1 = 0, ptvmmap = 0;
210 static pt_entry_t *msgbufmap;
211 struct msgbuf *msgbufp=0;
216 static pt_entry_t *pt_crashdumpmap;
217 static caddr_t crashdumpmap;
219 extern uint64_t KPTphys;
220 extern pt_entry_t *SMPpt;
221 extern uint64_t SMPptpa;
225 static PMAP_INLINE void free_pv_entry (pv_entry_t pv);
226 static pv_entry_t get_pv_entry (void);
227 static void i386_protection_init (void);
228 static __inline void pmap_clearbit (vm_page_t m, int bit);
230 static void pmap_remove_all (vm_page_t m);
231 static void pmap_enter_quick (pmap_t pmap, vm_offset_t va, vm_page_t m);
232 static int pmap_remove_pte (struct pmap *pmap, pt_entry_t *ptq,
233 vm_offset_t sva, pmap_inval_info_t info);
234 static void pmap_remove_page (struct pmap *pmap,
235 vm_offset_t va, pmap_inval_info_t info);
236 static int pmap_remove_entry (struct pmap *pmap, vm_page_t m,
237 vm_offset_t va, pmap_inval_info_t info);
238 static boolean_t pmap_testbit (vm_page_t m, int bit);
239 static void pmap_insert_entry (pmap_t pmap, vm_offset_t va,
240 vm_page_t mpte, vm_page_t m);
242 static vm_page_t pmap_allocpte (pmap_t pmap, vm_offset_t va);
244 static int pmap_release_free_page (pmap_t pmap, vm_page_t p);
245 static vm_page_t _pmap_allocpte (pmap_t pmap, vm_pindex_t ptepindex);
246 static pt_entry_t * pmap_pte_quick (pmap_t pmap, vm_offset_t va);
247 static vm_page_t pmap_page_lookup (vm_object_t object, vm_pindex_t pindex);
248 static int pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m,
249 pmap_inval_info_t info);
250 static int pmap_unuse_pt (pmap_t, vm_offset_t, vm_page_t, pmap_inval_info_t);
251 static vm_offset_t pmap_kmem_choose(vm_offset_t addr);
253 static unsigned pdir4mb;
256 * Move the kernel virtual free pointer to the next
257 * 2MB. This is used to help improve performance
258 * by using a large (2MB) page for much of the kernel
259 * (.text, .data, .bss)
262 pmap_kmem_choose(vm_offset_t addr)
265 vm_offset_t newaddr = addr;
267 newaddr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
274 * Super fast pmap_pte routine best used when scanning the pv lists.
275 * This eliminates many course-grained invltlb calls. Note that many of
276 * the pv list scans are across different pmaps and it is very wasteful
277 * to do an entire invltlb when checking a single mapping.
279 * Should only be called while in a critical section.
281 static __inline pt_entry_t *pmap_pte(pmap_t pmap, vm_offset_t va);
284 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
287 return pmap_pte(pmap, va);
290 /* Return a non-clipped PD index for a given VA */
291 static __inline vm_pindex_t
292 pmap_pde_pindex(vm_offset_t va)
295 return va >> PDRSHIFT;
298 /* Return various clipped indexes for a given VA */
299 static __inline vm_pindex_t
300 pmap_pte_index(vm_offset_t va)
304 return ((va >> PAGE_SHIFT) & ((1ul << NPTEPGSHIFT) - 1));
307 static __inline vm_pindex_t
308 pmap_pde_index(vm_offset_t va)
312 return ((va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1));
315 static __inline vm_pindex_t
316 pmap_pdpe_index(vm_offset_t va)
320 return ((va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1));
323 static __inline vm_pindex_t
324 pmap_pml4e_index(vm_offset_t va)
328 return ((va >> PML4SHIFT) & ((1ul << NPML4EPGSHIFT) - 1));
331 /* Return a pointer to the PML4 slot that corresponds to a VA */
332 static __inline pml4_entry_t *
333 pmap_pml4e(pmap_t pmap, vm_offset_t va)
337 return (&pmap->pm_pml4[pmap_pml4e_index(va)]);
340 /* Return a pointer to the PDP slot that corresponds to a VA */
341 static __inline pdp_entry_t *
342 pmap_pml4e_to_pdpe(pml4_entry_t *pml4e, vm_offset_t va)
347 pdpe = (pdp_entry_t *)PHYS_TO_DMAP(*pml4e & PG_FRAME);
348 return (&pdpe[pmap_pdpe_index(va)]);
351 /* Return a pointer to the PDP slot that corresponds to a VA */
352 static __inline pdp_entry_t *
353 pmap_pdpe(pmap_t pmap, vm_offset_t va)
358 pml4e = pmap_pml4e(pmap, va);
359 if ((*pml4e & PG_V) == 0)
361 return (pmap_pml4e_to_pdpe(pml4e, va));
364 /* Return a pointer to the PD slot that corresponds to a VA */
365 static __inline pd_entry_t *
366 pmap_pdpe_to_pde(pdp_entry_t *pdpe, vm_offset_t va)
371 pde = (pd_entry_t *)PHYS_TO_DMAP(*pdpe & PG_FRAME);
372 return (&pde[pmap_pde_index(va)]);
375 /* Return a pointer to the PD slot that corresponds to a VA */
376 static __inline pd_entry_t *
377 pmap_pde(pmap_t pmap, vm_offset_t va)
382 pdpe = pmap_pdpe(pmap, va);
383 if (pdpe == NULL || (*pdpe & PG_V) == 0)
385 return (pmap_pdpe_to_pde(pdpe, va));
388 /* Return a pointer to the PT slot that corresponds to a VA */
389 static __inline pt_entry_t *
390 pmap_pde_to_pte(pd_entry_t *pde, vm_offset_t va)
395 pte = (pt_entry_t *)PHYS_TO_DMAP(*pde & PG_FRAME);
396 return (&pte[pmap_pte_index(va)]);
399 /* Return a pointer to the PT slot that corresponds to a VA */
400 static __inline pt_entry_t *
401 pmap_pte(pmap_t pmap, vm_offset_t va)
406 pde = pmap_pde(pmap, va);
407 if (pde == NULL || (*pde & PG_V) == 0)
409 if ((*pde & PG_PS) != 0) /* compat with i386 pmap_pte() */
410 return ((pt_entry_t *)pde);
411 return (pmap_pde_to_pte(pde, va));
415 PMAP_INLINE pt_entry_t *
416 vtopte(vm_offset_t va)
419 uint64_t mask = ((1ul << (NPTEPGSHIFT + NPDEPGSHIFT + NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1);
421 return (PTmap + ((va >> PAGE_SHIFT) & mask));
424 static __inline pd_entry_t *
425 vtopde(vm_offset_t va)
428 uint64_t mask = ((1ul << (NPDEPGSHIFT + NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1);
430 return (PDmap + ((va >> PDRSHIFT) & mask));
434 allocpages(vm_paddr_t *firstaddr, int n)
440 bzero((void *)ret, n * PAGE_SIZE);
441 *firstaddr += n * PAGE_SIZE;
446 create_pagetables(vm_paddr_t *firstaddr)
451 uint64_t cpu0pp, cpu0idlestk;
452 int idlestk_page_offset = offsetof(struct privatespace, idlestack) / PAGE_SIZE;
454 /* we are running (mostly) V=P at this point */
457 KPTphys = allocpages(firstaddr, NKPT);
458 KPML4phys = allocpages(firstaddr, 1);
459 KPDPphys = allocpages(firstaddr, NKPML4E);
460 KPDphys = allocpages(firstaddr, NKPDPE);
462 ndmpdp = (ptoa(Maxmem) + NBPDP - 1) >> PDPSHIFT;
463 if (ndmpdp < 4) /* Minimum 4GB of dirmap */
465 DMPDPphys = allocpages(firstaddr, NDMPML4E);
466 if ((amd_feature & AMDID_PAGE1GB) == 0)
467 DMPDphys = allocpages(firstaddr, ndmpdp);
468 dmaplimit = (vm_paddr_t)ndmpdp << PDPSHIFT;
470 /* Fill in the underlying page table pages */
471 /* Read-only from zero to physfree */
472 /* XXX not fully used, underneath 2M pages */
473 for (i = 0; (i << PAGE_SHIFT) < *firstaddr; i++) {
474 ((pt_entry_t *)KPTphys)[i] = i << PAGE_SHIFT;
475 ((pt_entry_t *)KPTphys)[i] |= PG_RW | PG_V | PG_G;
478 /* Now map the page tables at their location within PTmap */
479 for (i = 0; i < NKPT; i++) {
480 ((pd_entry_t *)KPDphys)[i] = KPTphys + (i << PAGE_SHIFT);
481 ((pd_entry_t *)KPDphys)[i] |= PG_RW | PG_V;
484 /* Map from zero to end of allocations under 2M pages */
485 /* This replaces some of the KPTphys entries above */
486 for (i = 0; (i << PDRSHIFT) < *firstaddr; i++) {
487 ((pd_entry_t *)KPDphys)[i] = i << PDRSHIFT;
488 ((pd_entry_t *)KPDphys)[i] |= PG_RW | PG_V | PG_PS | PG_G;
491 /* And connect up the PD to the PDP */
492 for (i = 0; i < NKPDPE; i++) {
493 ((pdp_entry_t *)KPDPphys)[i + KPDPI] = KPDphys +
495 ((pdp_entry_t *)KPDPphys)[i + KPDPI] |= PG_RW | PG_V | PG_U;
498 /* Now set up the direct map space using either 2MB or 1GB pages */
499 /* Preset PG_M and PG_A because demotion expects it */
500 if ((amd_feature & AMDID_PAGE1GB) == 0) {
501 for (i = 0; i < NPDEPG * ndmpdp; i++) {
502 ((pd_entry_t *)DMPDphys)[i] = (vm_paddr_t)i << PDRSHIFT;
503 ((pd_entry_t *)DMPDphys)[i] |= PG_RW | PG_V | PG_PS |
506 /* And the direct map space's PDP */
507 for (i = 0; i < ndmpdp; i++) {
508 ((pdp_entry_t *)DMPDPphys)[i] = DMPDphys +
510 ((pdp_entry_t *)DMPDPphys)[i] |= PG_RW | PG_V | PG_U;
513 for (i = 0; i < ndmpdp; i++) {
514 ((pdp_entry_t *)DMPDPphys)[i] =
515 (vm_paddr_t)i << PDPSHIFT;
516 ((pdp_entry_t *)DMPDPphys)[i] |= PG_RW | PG_V | PG_PS |
521 /* And recursively map PML4 to itself in order to get PTmap */
522 ((pdp_entry_t *)KPML4phys)[PML4PML4I] = KPML4phys;
523 ((pdp_entry_t *)KPML4phys)[PML4PML4I] |= PG_RW | PG_V | PG_U;
525 /* Connect the Direct Map slot up to the PML4 */
526 ((pdp_entry_t *)KPML4phys)[DMPML4I] = DMPDPphys;
527 ((pdp_entry_t *)KPML4phys)[DMPML4I] |= PG_RW | PG_V | PG_U;
529 /* Connect the KVA slot up to the PML4 */
530 ((pdp_entry_t *)KPML4phys)[KPML4I] = KPDPphys;
531 ((pdp_entry_t *)KPML4phys)[KPML4I] |= PG_RW | PG_V | PG_U;
533 common_lvl4_phys = allocpages(firstaddr, 1); /* 512 512G mappings */
534 common_lvl3_phys = allocpages(firstaddr, 1); /* 512 1G mappings */
535 KPTphys = allocpages(firstaddr, NKPT); /* kernel page table */
536 IdlePTD = allocpages(firstaddr, 1); /* kernel page dir */
537 cpu0pp = allocpages(firstaddr, MDGLOBALDATA_BASEALLOC_PAGES);
538 cpu0idlestk = allocpages(firstaddr, UPAGES);
539 SMPptpa = allocpages(firstaddr, 1);
540 SMPpt = (void *)(SMPptpa + KERNBASE);
544 * Load kernel page table with kernel memory mappings
546 for (i = 0; (i << PAGE_SHIFT) < *firstaddr; i++) {
547 ((pt_entry_t *)KPTphys)[i] = i << PAGE_SHIFT;
548 ((pt_entry_t *)KPTphys)[i] |= PG_RW | PG_V;
552 for (i = 0; i < NKPT; i++) {
553 ((pd_entry_t *)IdlePTD)[i] = KPTphys + (i << PAGE_SHIFT);
554 ((pd_entry_t *)IdlePTD)[i] |= PG_RW | PG_V;
559 * Set up the kernel page table itself.
561 for (i = 0; i < NKPT; i++) {
562 ((pd_entry_t *)IdlePTD)[KPTDI + i] = KPTphys + (i << PAGE_SHIFT);
563 ((pd_entry_t *)IdlePTD)[KPTDI + i] |= PG_RW | PG_V;
567 count = ISA_HOLE_LENGTH >> PAGE_SHIFT;
568 for (i = 0; i < count; i++) {
569 ((pt_entry_t *)KPTphys)[amd64_btop(ISA_HOLE_START) + i] = \
570 (ISA_HOLE_START + i * PAGE_SIZE) | PG_RW | PG_V;
577 ((pd_entry_t *)IdlePTD)[PTDPTDI] = (pd_entry_t)IdlePTD | PG_RW | PG_V;
580 * Map CPU_prvspace[0].mdglobaldata
582 for (i = 0; i < MDGLOBALDATA_BASEALLOC_PAGES; i++) {
583 ((pt_entry_t *)SMPptpa)[i] = \
584 (cpu0pp + i * PAGE_SIZE) | PG_RW | PG_V;
588 * Map CPU_prvspace[0].idlestack
590 for (i = 0; i < UPAGES; i++) {
591 ((pt_entry_t *)SMPptpa)[idlestk_page_offset + i] = \
592 (cpu0idlestk + i * PAGE_SIZE) | PG_RW | PG_V;
598 ((pd_entry_t *)IdlePTD)[MPPTDI] = SMPptpa | PG_RW | PG_V;
603 ((pml4_entry_t *)common_lvl4_phys)[LINKPML4I] = common_lvl3_phys | PG_RW | PG_V | PG_U;
606 * location of "virtual CR3" - a PDP entry that is loaded
607 * with a PD physical address (+ page attributes).
608 * Matt: location of user page directory entry (representing 1G)
610 link_pdpe = &((pdp_entry_t *)common_lvl3_phys)[LINKPDPI];
611 #endif /* JGPMAP32 */
616 init_paging(vm_paddr_t *firstaddr) {
617 create_pagetables(firstaddr);
620 /* switch to the newly created page table */
621 *link_pdpe = IdlePTD | PG_RW | PG_V | PG_U;
622 load_cr3(common_lvl4_phys);
623 link_pdpe = (void *)((char *)link_pdpe + KERNBASE);
625 KvaStart = (vm_offset_t)VADDR(PTDPTDI, 0);
626 KvaEnd = (vm_offset_t)VADDR(APTDPTDI, 0);
627 KvaSize = KvaEnd - KvaStart;
632 * Bootstrap the system enough to run with virtual memory.
634 * On the i386 this is called after mapping has already been enabled
635 * and just syncs the pmap module with what has already been done.
636 * [We can't call it easily with mapping off since the kernel is not
637 * mapped with PA == VA, hence we would have to relocate every address
638 * from the linked base (virtual) address "KERNBASE" to the actual
639 * (physical) address starting relative to 0]
642 pmap_bootstrap(vm_paddr_t *firstaddr)
647 struct mdglobaldata *gd;
651 KvaStart = VM_MIN_KERNEL_ADDRESS;
652 KvaEnd = VM_MAX_KERNEL_ADDRESS;
653 KvaSize = KvaEnd - KvaStart;
655 avail_start = *firstaddr;
658 * Create an initial set of page tables to run the kernel in.
660 create_pagetables(firstaddr);
662 virtual_start = (vm_offset_t) PTOV_OFFSET + *firstaddr;
663 virtual_start = pmap_kmem_choose(virtual_start);
665 virtual_end = VM_MAX_KERNEL_ADDRESS;
667 /* XXX do %cr0 as well */
668 load_cr4(rcr4() | CR4_PGE | CR4_PSE);
672 * Initialize protection array.
674 i386_protection_init();
677 * The kernel's pmap is statically allocated so we don't have to use
678 * pmap_create, which is unlikely to work correctly at this part of
679 * the boot sequence (XXX and which no longer exists).
682 kernel_pmap.pm_pdir = (pd_entry_t *)(PTOV_OFFSET + (uint64_t)IdlePTD);
684 kernel_pmap.pm_pml4 = (pdp_entry_t *) (PTOV_OFFSET + KPML4phys);
685 kernel_pmap.pm_count = 1;
686 kernel_pmap.pm_active = (cpumask_t)-1; /* don't allow deactivation */
687 TAILQ_INIT(&kernel_pmap.pm_pvlist);
691 * Reserve some special page table entries/VA space for temporary
694 #define SYSMAP(c, p, v, n) \
695 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
699 pte = (pt_entry_t *) pmap_pte(&kernel_pmap, va);
705 * CMAP1/CMAP2 are used for zeroing and copying pages.
707 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
712 SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS);
715 * ptvmmap is used for reading arbitrary physical pages via
718 SYSMAP(caddr_t, ptmmap, ptvmmap, 1)
721 * msgbufp is used to map the system message buffer.
722 * XXX msgbufmap is not used.
724 SYSMAP(struct msgbuf *, msgbufmap, msgbufp,
725 atop(round_page(MSGBUF_SIZE)))
731 for (i = 0; i < NKPT; i++)
736 * PG_G is terribly broken on SMP because we IPI invltlb's in some
737 * cases rather then invl1pg. Actually, I don't even know why it
738 * works under UP because self-referential page table mappings
743 if (cpu_feature & CPUID_PGE)
748 * Initialize the 4MB page size flag
752 * The 4MB page version of the initial
753 * kernel page mapping.
757 #if !defined(DISABLE_PSE)
758 if (cpu_feature & CPUID_PSE) {
761 * Note that we have enabled PSE mode
764 ptditmp = *(PTmap + amd64_btop(KERNBASE));
765 ptditmp &= ~(NBPDR - 1);
766 ptditmp |= PG_V | PG_RW | PG_PS | PG_U | pgeflag;
771 * Enable the PSE mode. If we are SMP we can't do this
772 * now because the APs will not be able to use it when
775 load_cr4(rcr4() | CR4_PSE);
778 * We can do the mapping here for the single processor
779 * case. We simply ignore the old page table page from
783 * For SMP, we still need 4K pages to bootstrap APs,
784 * PSE will be enabled as soon as all APs are up.
786 PTD[KPTDI] = (pd_entry_t)ptditmp;
788 kernel_pmap.pm_pdir[KPTDI] = (pd_entry_t)ptditmp;
795 if (cpu_apic_address == 0)
796 panic("pmap_bootstrap: no local apic!");
798 /* local apic is mapped on last page */
799 SMPpt[NPTEPG - 1] = (pt_entry_t)(PG_V | PG_RW | PG_N | pgeflag |
800 (cpu_apic_address & PG_FRAME));
804 * We need to finish setting up the globaldata page for the BSP.
805 * locore has already populated the page table for the mdglobaldata
808 pg = MDGLOBALDATA_BASEALLOC_PAGES;
809 gd = &CPU_prvspace[0].mdglobaldata;
810 gd->gd_CMAP1 = &SMPpt[pg + 0];
811 gd->gd_CMAP2 = &SMPpt[pg + 1];
812 gd->gd_CMAP3 = &SMPpt[pg + 2];
813 gd->gd_PMAP1 = &SMPpt[pg + 3];
814 gd->gd_CADDR1 = CPU_prvspace[0].CPAGE1;
815 gd->gd_CADDR2 = CPU_prvspace[0].CPAGE2;
816 gd->gd_CADDR3 = CPU_prvspace[0].CPAGE3;
817 gd->gd_PADDR1 = (pt_entry_t *)CPU_prvspace[0].PPAGE1;
824 * Set 4mb pdir for mp startup
830 if (pseflag && (cpu_feature & CPUID_PSE)) {
831 load_cr4(rcr4() | CR4_PSE);
832 if (pdir4mb && mycpu->gd_cpuid == 0) { /* only on BSP */
834 kernel_pmap.pm_pdir[KPTDI] =
835 PTD[KPTDI] = (pd_entry_t)pdir4mb;
844 * Initialize the pmap module.
845 * Called by vm_init, to initialize any structures that the pmap
846 * system needs to map virtual memory.
847 * pmap_init has been enhanced to support in a fairly consistant
848 * way, discontiguous physical memory.
858 * object for kernel page table pages
860 /* JG I think the number can be arbitrary */
861 kptobj = vm_object_allocate(OBJT_DEFAULT, 5);
864 * Allocate memory for random pmap data structures. Includes the
868 for(i = 0; i < vm_page_array_size; i++) {
871 m = &vm_page_array[i];
872 TAILQ_INIT(&m->md.pv_list);
873 m->md.pv_list_count = 0;
877 * init the pv free list
879 initial_pvs = vm_page_array_size;
880 if (initial_pvs < MINPV)
882 pvzone = &pvzone_store;
883 pvinit = (struct pv_entry *) kmem_alloc(&kernel_map,
884 initial_pvs * sizeof (struct pv_entry));
885 zbootinit(pvzone, "PV ENTRY", sizeof (struct pv_entry), pvinit,
889 * Now it is safe to enable pv_table recording.
891 pmap_initialized = TRUE;
895 * Initialize the address space (zone) for the pv_entries. Set a
896 * high water mark so that the system can recover from excessive
897 * numbers of pv entries.
903 int shpgperproc = PMAP_SHPGPERPROC;
905 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
906 pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
907 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
908 pv_entry_high_water = 9 * (pv_entry_max / 10);
909 zinitna(pvzone, &pvzone_obj, NULL, 0, pv_entry_max, ZONE_INTERRUPT, 1);
913 /***************************************************
914 * Low level helper routines.....
915 ***************************************************/
917 #if defined(PMAP_DIAGNOSTIC)
920 * This code checks for non-writeable/modified pages.
921 * This should be an invalid condition.
924 pmap_nw_modified(pt_entry_t pte)
927 if ((pte & (PG_M|PG_RW)) == PG_M)
936 * this routine defines the region(s) of memory that should
937 * not be tested for the modified bit.
939 static PMAP_INLINE int
940 pmap_track_modified(vm_offset_t va)
943 if ((va < clean_sva) || (va >= clean_eva))
952 * Extract the physical page address associated with the map/VA pair.
954 * This function may not be called from an interrupt if the pmap is
958 pmap_extract(pmap_t pmap, vm_offset_t va)
963 pd_entry_t pde, *pdep;
966 pdep = pmap_pde(pmap, va);
970 if ((pde & PG_PS) != 0) {
971 rtval = (pde & PG_PS_FRAME) | (va & PDRMASK);
973 pte = pmap_pde_to_pte(pdep, va);
974 rtval = (*pte & PG_FRAME) | (va & PAGE_MASK);
982 * Routine: pmap_kextract
984 * Extract the physical page address associated
985 * kernel virtual address.
988 pmap_kextract(vm_offset_t va)
994 if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS) {
995 pa = DMAP_TO_PHYS(va);
999 pa = (pde & PG_PS_FRAME) | (va & PDRMASK);
1002 * Beware of a concurrent promotion that changes the
1003 * PDE at this point! For example, vtopte() must not
1004 * be used to access the PTE because it would use the
1005 * new PDE. It is, however, safe to use the old PDE
1006 * because the page table page is preserved by the
1009 pa = *pmap_pde_to_pte(&pde, va);
1010 pa = (pa & PG_FRAME) | (va & PAGE_MASK);
1016 /***************************************************
1017 * Low level mapping routines.....
1018 ***************************************************/
1021 * Routine: pmap_kenter
1023 * Add a wired page to the KVA
1024 * NOTE! note that in order for the mapping to take effect -- you
1025 * should do an invltlb after doing the pmap_kenter().
1028 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
1033 pmap_inval_info info;
1035 pmap_inval_init(&info);
1036 npte = pa | PG_RW | PG_V | pgeflag;
1038 pmap_inval_add(&info, &kernel_pmap, va);
1040 pmap_inval_flush(&info);
1044 * Routine: pmap_kenter_quick
1046 * Similar to pmap_kenter(), except we only invalidate the
1047 * mapping on the current CPU.
1050 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
1056 npte = pa | PG_RW | PG_V | pgeflag;
1059 cpu_invlpg((void *)va);
1063 pmap_kenter_sync(vm_offset_t va)
1066 pmap_inval_info info;
1068 pmap_inval_init(&info);
1069 pmap_inval_add(&info, &kernel_pmap, va);
1070 pmap_inval_flush(&info);
1074 pmap_kenter_sync_quick(vm_offset_t va)
1077 cpu_invlpg((void *)va);
1081 * remove a page from the kernel pagetables
1084 pmap_kremove(vm_offset_t va)
1088 pmap_inval_info info;
1090 pmap_inval_init(&info);
1092 pmap_inval_add(&info, &kernel_pmap, va);
1094 pmap_inval_flush(&info);
1098 pmap_kremove_quick(vm_offset_t va)
1104 cpu_invlpg((void *)va);
1108 * XXX these need to be recoded. They are not used in any critical path.
1111 pmap_kmodify_rw(vm_offset_t va)
1114 *vtopte(va) |= PG_RW;
1115 cpu_invlpg((void *)va);
1119 pmap_kmodify_nc(vm_offset_t va)
1122 *vtopte(va) |= PG_N;
1123 cpu_invlpg((void *)va);
1127 * Used to map a range of physical addresses into kernel
1128 * virtual address space.
1130 * For now, VM is already on, we only need to map the
1134 pmap_map(vm_offset_t virt, vm_paddr_t start, vm_paddr_t end, int prot)
1138 * JG Are callers prepared to get an address in the DMAP,
1139 * instead of the passed-in virt?
1141 while (start < end) {
1142 pmap_kenter(virt, start);
1151 * Add a list of wired pages to the kva
1152 * this routine is only used for temporary
1153 * kernel mappings that do not need to have
1154 * page modification or references recorded.
1155 * Note that old mappings are simply written
1156 * over. The page *must* be wired.
1159 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
1164 end_va = va + count * PAGE_SIZE;
1166 while (va < end_va) {
1170 *pte = VM_PAGE_TO_PHYS(*m) | PG_RW | PG_V | pgeflag;
1171 cpu_invlpg((void *)va);
1176 smp_invltlb(); /* XXX */
1181 pmap_qenter2(vm_offset_t va, vm_page_t *m, int count, cpumask_t *mask)
1185 cpumask_t cmask = mycpu->gd_cpumask;
1187 end_va = va + count * PAGE_SIZE;
1189 while (va < end_va) {
1194 * Install the new PTE. If the pte changed from the prior
1195 * mapping we must reset the cpu mask and invalidate the page.
1196 * If the pte is the same but we have not seen it on the
1197 * current cpu, invlpg the existing mapping. Otherwise the
1198 * entry is optimal and no invalidation is required.
1201 pteval = VM_PAGE_TO_PHYS(*m) | PG_A | PG_RW | PG_V | pgeflag;
1202 if (*pte != pteval) {
1205 cpu_invlpg((void *)va);
1206 } else if ((*mask & cmask) == 0) {
1207 cpu_invlpg((void *)va);
1216 * this routine jerks page mappings from the
1217 * kernel -- it is meant only for temporary mappings.
1220 pmap_qremove(vm_offset_t va, int count)
1225 end_va = va + count * PAGE_SIZE;
1227 while (va < end_va) {
1232 cpu_invlpg((void *)va);
1241 * This routine works like vm_page_lookup() but also blocks as long as the
1242 * page is busy. This routine does not busy the page it returns.
1244 * Unless the caller is managing objects whos pages are in a known state,
1245 * the call should be made with a critical section held so the page's object
1246 * association remains valid on return.
1249 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
1255 m = vm_page_lookup(object, pindex);
1256 } while (m && vm_page_sleep_busy(m, FALSE, "pplookp"));
1262 * Create a new thread and optionally associate it with a (new) process.
1263 * NOTE! the new thread's cpu may not equal the current cpu.
1266 pmap_init_thread(thread_t td)
1269 /* enforce pcb placement */
1270 td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
1271 td->td_savefpu = &td->td_pcb->pcb_save;
1272 td->td_sp = (char *)td->td_pcb - 16; /* JG is -16 needed on amd64? */
1276 * This routine directly affects the fork perf for a process.
1279 pmap_init_proc(struct proc *p)
1285 * Dispose the UPAGES for a process that has exited.
1286 * This routine directly impacts the exit perf of a process.
1289 pmap_dispose_proc(struct proc *p)
1292 KASSERT(p->p_lock == 0, ("attempt to dispose referenced proc! %p", p));
1295 /***************************************************
1296 * Page table page management routines.....
1297 ***************************************************/
1300 * This routine unholds page table pages, and if the hold count
1301 * drops to zero, then it decrements the wire count.
1304 _pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m, pmap_inval_info_t info)
1308 * Wait until we can busy the page ourselves. We cannot have
1309 * any active flushes if we block.
1311 if (m->flags & PG_BUSY) {
1312 pmap_inval_flush(info);
1313 while (vm_page_sleep_busy(m, FALSE, "pmuwpt"))
1316 KASSERT(m->queue == PQ_NONE,
1317 ("_pmap_unwire_pte_hold: %p->queue != PQ_NONE", m));
1319 if (m->hold_count == 1) {
1321 * Unmap the page table page
1324 pmap_inval_add(info, pmap, -1);
1326 if (m->pindex >= (NUPDE + NUPDPE)) {
1329 pml4 = pmap_pml4e(pmap, va);
1331 } else if (m->pindex >= NUPDE) {
1334 pdp = pmap_pdpe(pmap, va);
1339 pd = pmap_pde(pmap, va);
1343 KKASSERT(pmap->pm_stats.resident_count > 0);
1344 --pmap->pm_stats.resident_count;
1346 if (pmap->pm_ptphint == m)
1347 pmap->pm_ptphint = NULL;
1350 if (m->pindex < NUPDE) {
1351 /* We just released a PT, unhold the matching PD */
1354 pdpg = PHYS_TO_VM_PAGE(*pmap_pdpe(pmap, va) & PG_FRAME);
1355 pmap_unwire_pte_hold(pmap, va, pdpg, info);
1357 if (m->pindex >= NUPDE && m->pindex < (NUPDE + NUPDPE)) {
1358 /* We just released a PD, unhold the matching PDP */
1361 pdppg = PHYS_TO_VM_PAGE(*pmap_pml4e(pmap, va) & PG_FRAME);
1362 pmap_unwire_pte_hold(pmap, va, pdppg, info);
1367 * This was our last hold, the page had better be unwired
1368 * after we decrement wire_count.
1370 * FUTURE NOTE: shared page directory page could result in
1371 * multiple wire counts.
1375 KKASSERT(m->wire_count == 0);
1376 --vmstats.v_wire_count;
1377 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1379 vm_page_free_zero(m);
1382 KKASSERT(m->hold_count > 1);
1388 static PMAP_INLINE int
1389 pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m, pmap_inval_info_t info)
1392 KKASSERT(m->hold_count > 0);
1393 if (m->hold_count > 1) {
1397 return _pmap_unwire_pte_hold(pmap, va, m, info);
1402 * After removing a page table entry, this routine is used to
1403 * conditionally free the page, and manage the hold/wire counts.
1406 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte,
1407 pmap_inval_info_t info)
1410 /* JG Use FreeBSD/amd64 or FreeBSD/i386 ptepde approaches? */
1411 vm_pindex_t ptepindex;
1412 if (va >= VM_MAX_USER_ADDRESS)
1416 ptepindex = pmap_pde_pindex(va);
1418 if (pmap->pm_ptphint &&
1419 (pmap->pm_ptphint->pindex == ptepindex)) {
1420 mpte = pmap->pm_ptphint;
1423 pmap_inval_flush(info);
1424 mpte = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1425 pmap->pm_ptphint = mpte;
1431 return pmap_unwire_pte_hold(pmap, va, mpte, info);
1435 * Initialize pmap0/vmspace0. This pmap is not added to pmap_list because
1436 * it, and IdlePTD, represents the template used to update all other pmaps.
1438 * On architectures where the kernel pmap is not integrated into the user
1439 * process pmap, this pmap represents the process pmap, not the kernel pmap.
1440 * kernel_pmap should be used to directly access the kernel_pmap.
1443 pmap_pinit0(struct pmap *pmap)
1448 (pd_entry_t *)kmem_alloc_pageable(&kernel_map, PAGE_SIZE);
1449 pmap_kenter((vm_offset_t)pmap->pm_pdir, (vm_offset_t) IdlePTD);
1451 pmap->pm_pml4 = (pml4_entry_t *)(PTOV_OFFSET + KPML4phys);
1453 pmap->pm_active = 0;
1454 pmap->pm_ptphint = NULL;
1455 TAILQ_INIT(&pmap->pm_pvlist);
1456 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1460 * Initialize a preallocated and zeroed pmap structure,
1461 * such as one in a vmspace structure.
1464 pmap_pinit(struct pmap *pmap)
1470 * No need to allocate page table space yet but we do need a valid
1471 * page directory table.
1473 if (pmap->pm_pml4 == NULL) {
1475 (pml4_entry_t *)kmem_alloc_pageable(&kernel_map, PAGE_SIZE);
1479 * Allocate an object for the ptes
1481 if (pmap->pm_pteobj == NULL)
1482 pmap->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, PML4PML4I + 1);
1485 * Allocate the page directory page, unless we already have
1486 * one cached. If we used the cached page the wire_count will
1487 * already be set appropriately.
1489 if ((ptdpg = pmap->pm_pdirm) == NULL) {
1490 ptdpg = vm_page_grab(pmap->pm_pteobj, PML4PML4I,
1491 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1492 pmap->pm_pdirm = ptdpg;
1493 vm_page_flag_clear(ptdpg, PG_MAPPED | PG_BUSY);
1494 ptdpg->valid = VM_PAGE_BITS_ALL;
1495 ptdpg->wire_count = 1;
1496 ++vmstats.v_wire_count;
1497 pmap_kenter((vm_offset_t)pmap->pm_pml4, VM_PAGE_TO_PHYS(ptdpg));
1499 if ((ptdpg->flags & PG_ZERO) == 0)
1500 bzero(pmap->pm_pml4, PAGE_SIZE);
1502 pmap->pm_pml4[KPML4I] = KPDPphys | PG_RW | PG_V | PG_U;
1503 pmap->pm_pml4[DMPML4I] = DMPDPphys | PG_RW | PG_V | PG_U;
1505 /* install self-referential address mapping entry */
1506 pmap->pm_pml4[PML4PML4I] = VM_PAGE_TO_PHYS(ptdpg) | PG_V | PG_RW | PG_A | PG_M;
1509 pmap->pm_active = 0;
1510 pmap->pm_ptphint = NULL;
1511 TAILQ_INIT(&pmap->pm_pvlist);
1512 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1513 pmap->pm_stats.resident_count = 1;
1517 * Clean up a pmap structure so it can be physically freed. This routine
1518 * is called by the vmspace dtor function. A great deal of pmap data is
1519 * left passively mapped to improve vmspace management so we have a bit
1520 * of cleanup work to do here.
1523 pmap_puninit(pmap_t pmap)
1528 KKASSERT(pmap->pm_active == 0);
1529 if ((p = pmap->pm_pdirm) != NULL) {
1530 KKASSERT(pmap->pm_pml4 != NULL);
1531 KKASSERT(pmap->pm_pml4 != (PTOV_OFFSET + KPML4phys));
1532 pmap_kremove((vm_offset_t)pmap->pm_pml4);
1534 vmstats.v_wire_count--;
1535 KKASSERT((p->flags & PG_BUSY) == 0);
1537 vm_page_free_zero(p);
1538 pmap->pm_pdirm = NULL;
1540 if (pmap->pm_pml4) {
1541 KKASSERT(pmap->pm_pml4 != (PTOV_OFFSET + KPML4phys));
1542 kmem_free(&kernel_map, (vm_offset_t)pmap->pm_pml4, PAGE_SIZE);
1543 pmap->pm_pml4 = NULL;
1545 if (pmap->pm_pteobj) {
1546 vm_object_deallocate(pmap->pm_pteobj);
1547 pmap->pm_pteobj = NULL;
1552 * Wire in kernel global address entries. To avoid a race condition
1553 * between pmap initialization and pmap_growkernel, this procedure
1554 * adds the pmap to the master list (which growkernel scans to update),
1555 * then copies the template.
1558 pmap_pinit2(struct pmap *pmap)
1562 TAILQ_INSERT_TAIL(&pmap_list, pmap, pm_pmnode);
1563 /* XXX copies current process, does not fill in MPPTDI */
1565 bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * PTESIZE);
1571 * Attempt to release and free a vm_page in a pmap. Returns 1 on success,
1572 * 0 on failure (if the procedure had to sleep).
1574 * When asked to remove the page directory page itself, we actually just
1575 * leave it cached so we do not have to incur the SMP inval overhead of
1576 * removing the kernel mapping. pmap_puninit() will take care of it.
1579 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1582 pml4_entry_t *pml4 = pmap->pm_pml4;
1584 * This code optimizes the case of freeing non-busy
1585 * page-table pages. Those pages are zero now, and
1586 * might as well be placed directly into the zero queue.
1588 if (vm_page_sleep_busy(p, FALSE, "pmaprl"))
1594 * Remove the page table page from the processes address space.
1596 /* JG XXX we need to turn 'pindex' into a page table level
1597 * (PML4, PDP, PD, PT) and index within the page table page
1602 KKASSERT(pmap->pm_stats.resident_count > 0);
1603 --pmap->pm_stats.resident_count;
1605 if (p->hold_count) {
1606 panic("pmap_release: freeing held page table page");
1608 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1609 pmap->pm_ptphint = NULL;
1612 vmstats.v_wire_count--;
1613 vm_page_free_zero(p);
1618 * this routine is called if the page table page is not
1622 _pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex)
1625 vm_page_t m, pdppg, pdpg;
1628 * Find or fabricate a new pagetable page
1630 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1631 VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1634 if ((m->flags & PG_ZERO) == 0) {
1635 pmap_zero_page(VM_PAGE_TO_PHYS(m));
1638 KASSERT(m->queue == PQ_NONE,
1639 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1642 * Increment the hold count for the page we will be returning to
1648 * It is possible that someone else got in and mapped by the page
1649 * directory page while we were blocked, if so just unbusy and
1650 * return the held page.
1653 if ((ptepa = pmap->pm_pdir[ptepindex]) != 0) {
1654 KKASSERT((ptepa & PG_FRAME) == VM_PAGE_TO_PHYS(m));
1660 if (m->wire_count == 0)
1661 vmstats.v_wire_count++;
1666 * Map the pagetable page into the process address space, if
1667 * it isn't already there.
1670 ++pmap->pm_stats.resident_count;
1673 ptepa = VM_PAGE_TO_PHYS(m);
1674 pmap->pm_pdir[ptepindex] =
1675 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_A | PG_M);
1677 if (ptepindex >= (NUPDE + NUPDPE)) {
1679 vm_pindex_t pml4index;
1681 /* Wire up a new PDP page */
1682 pml4index = ptepindex - (NUPDE + NUPDPE);
1683 pml4 = &pmap->pm_pml4[pml4index];
1684 *pml4 = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
1686 } else if (ptepindex >= NUPDE) {
1687 vm_pindex_t pml4index;
1688 vm_pindex_t pdpindex;
1692 /* Wire up a new PD page */
1693 pdpindex = ptepindex - NUPDE;
1694 pml4index = pdpindex >> NPML4EPGSHIFT;
1696 pml4 = &pmap->pm_pml4[pml4index];
1697 if ((*pml4 & PG_V) == 0) {
1698 /* Have to allocate a new PDP page, recurse */
1699 if (_pmap_allocpte(pmap, NUPDE + NUPDPE + pml4index)
1706 /* Add reference to the PDP page */
1707 pdppg = PHYS_TO_VM_PAGE(*pml4 & PG_FRAME);
1708 pdppg->wire_count++;
1710 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
1712 /* Now find the pdp page */
1713 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1714 *pdp = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
1717 vm_pindex_t pml4index;
1718 vm_pindex_t pdpindex;
1723 /* Wire up a new PT page */
1724 pdpindex = ptepindex >> NPDPEPGSHIFT;
1725 pml4index = pdpindex >> NPML4EPGSHIFT;
1727 /* First, find the pdp and check that its valid. */
1728 pml4 = &pmap->pm_pml4[pml4index];
1729 if ((*pml4 & PG_V) == 0) {
1730 /* We miss a PDP page. We ultimately need a PD page.
1731 * Recursively allocating a PD page will allocate
1732 * the missing PDP page and will also allocate
1733 * the PD page we need.
1735 /* Have to allocate a new PD page, recurse */
1736 if (_pmap_allocpte(pmap, NUPDE + pdpindex)
1742 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
1743 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1745 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
1746 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1747 if ((*pdp & PG_V) == 0) {
1748 /* Have to allocate a new PD page, recurse */
1749 if (_pmap_allocpte(pmap, NUPDE + pdpindex)
1756 /* Add reference to the PD page */
1757 pdpg = PHYS_TO_VM_PAGE(*pdp & PG_FRAME);
1761 pd = (pd_entry_t *)PHYS_TO_DMAP(*pdp & PG_FRAME);
1763 /* Now we know where the page directory page is */
1764 pd = &pd[ptepindex & ((1ul << NPDEPGSHIFT) - 1)];
1765 *pd = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
1770 * Set the page table hint
1772 pmap->pm_ptphint = m;
1774 m->valid = VM_PAGE_BITS_ALL;
1775 vm_page_flag_clear(m, PG_ZERO);
1776 vm_page_flag_set(m, PG_MAPPED);
1783 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1786 vm_pindex_t ptepindex;
1791 * Calculate pagetable page index
1793 ptepindex = pmap_pde_pindex(va);
1796 * Get the page directory entry
1798 pd = pmap_pde(pmap, va);
1801 * This supports switching from a 2MB page to a
1804 if (pd != NULL && (*pd & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
1812 * If the page table page is mapped, we just increment the
1813 * hold count, and activate it.
1815 if (pd != NULL && (*pd & PG_V) != 0) {
1816 /* YYY hint is used here on i386 */
1817 m = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1818 pmap->pm_ptphint = m;
1823 * Here if the pte page isn't mapped, or if it has been deallocated.
1825 return _pmap_allocpte(pmap, ptepindex);
1829 /***************************************************
1830 * Pmap allocation/deallocation routines.
1831 ***************************************************/
1834 * Release any resources held by the given physical map.
1835 * Called when a pmap initialized by pmap_pinit is being released.
1836 * Should only be called if the map contains no valid mappings.
1838 static int pmap_release_callback(struct vm_page *p, void *data);
1841 pmap_release(struct pmap *pmap)
1844 vm_object_t object = pmap->pm_pteobj;
1845 struct rb_vm_page_scan_info info;
1847 KASSERT(pmap->pm_active == 0, ("pmap still active! %08x", pmap->pm_active));
1848 #if defined(DIAGNOSTIC)
1849 if (object->ref_count != 1)
1850 panic("pmap_release: pteobj reference count != 1");
1854 info.object = object;
1856 TAILQ_REMOVE(&pmap_list, pmap, pm_pmnode);
1863 info.limit = object->generation;
1865 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
1866 pmap_release_callback, &info);
1867 if (info.error == 0 && info.mpte) {
1868 if (!pmap_release_free_page(pmap, info.mpte))
1872 } while (info.error);
1876 pmap_release_callback(struct vm_page *p, void *data)
1879 struct rb_vm_page_scan_info *info = data;
1881 if (p->pindex == PML4PML4I) {
1885 if (!pmap_release_free_page(info->pmap, p)) {
1889 if (info->object->generation != info->limit) {
1897 * Grow the number of kernel page table entries, if needed.
1901 pmap_growkernel(vm_offset_t addr)
1906 vm_offset_t ptppaddr;
1908 pd_entry_t *pde, newpdir;
1912 if (kernel_vm_end == 0) {
1913 kernel_vm_end = KERNBASE;
1915 while ((*pmap_pde(&kernel_pmap, kernel_vm_end) & PG_V) != 0) {
1916 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1918 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1919 kernel_vm_end = kernel_map.max_offset;
1924 addr = roundup2(addr, PAGE_SIZE * NPTEPG);
1925 if (addr - 1 >= kernel_map.max_offset)
1926 addr = kernel_map.max_offset;
1927 while (kernel_vm_end < addr) {
1928 pde = pmap_pde(&kernel_pmap, kernel_vm_end);
1930 /* We need a new PDP entry */
1931 nkpg = vm_page_alloc(kptobj, nkpt,
1932 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM
1933 | VM_ALLOC_INTERRUPT);
1935 panic("pmap_growkernel: no memory to grow kernel");
1936 if ((nkpg->flags & PG_ZERO) == 0)
1937 pmap_zero_page(nkpg);
1938 paddr = VM_PAGE_TO_PHYS(nkpg);
1939 newpdp = (pdp_entry_t)
1940 (paddr | PG_V | PG_RW | PG_A | PG_M);
1941 *pmap_pdpe(&kernel_pmap, kernel_vm_end) = newpdp;
1942 continue; /* try again */
1944 if ((*pde & PG_V) != 0) {
1945 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1946 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1947 kernel_vm_end = kernel_map.max_offset;
1954 * This index is bogus, but out of the way
1956 nkpg = vm_page_alloc(kptobj, nkpt,
1957 VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM | VM_ALLOC_INTERRUPT);
1959 panic("pmap_growkernel: no memory to grow kernel");
1962 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1963 pmap_zero_page(ptppaddr);
1964 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
1965 *pmap_pde(&kernel_pmap, kernel_vm_end) = newpdir;
1968 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1969 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1970 kernel_vm_end = kernel_map.max_offset;
1978 * Retire the given physical map from service.
1979 * Should only be called if the map contains
1980 * no valid mappings.
1983 pmap_destroy(pmap_t pmap)
1991 count = --pmap->pm_count;
1994 panic("destroying a pmap is not yet implemented");
1999 * Add a reference to the specified pmap.
2002 pmap_reference(pmap_t pmap)
2010 /***************************************************
2011 * page management routines.
2012 ***************************************************/
2015 * free the pv_entry back to the free list. This function may be
2016 * called from an interrupt.
2018 static PMAP_INLINE void
2019 free_pv_entry(pv_entry_t pv)
2023 KKASSERT(pv_entry_count >= 0);
2028 * get a new pv_entry, allocating a block from the system
2029 * when needed. This function may be called from an interrupt.
2036 if (pv_entry_high_water &&
2037 (pv_entry_count > pv_entry_high_water) &&
2038 (pmap_pagedaemon_waken == 0)) {
2039 pmap_pagedaemon_waken = 1;
2040 wakeup(&vm_pages_needed);
2042 return zalloc(pvzone);
2046 * This routine is very drastic, but can save the system
2055 static int warningdone=0;
2057 if (pmap_pagedaemon_waken == 0)
2060 if (warningdone < 5) {
2061 kprintf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
2065 for(i = 0; i < vm_page_array_size; i++) {
2066 m = &vm_page_array[i];
2067 if (m->wire_count || m->hold_count || m->busy ||
2068 (m->flags & PG_BUSY))
2072 pmap_pagedaemon_waken = 0;
2077 * If it is the first entry on the list, it is actually
2078 * in the header and we must copy the following entry up
2079 * to the header. Otherwise we must search the list for
2080 * the entry. In either case we free the now unused entry.
2083 pmap_remove_entry(struct pmap *pmap, vm_page_t m,
2084 vm_offset_t va, pmap_inval_info_t info)
2091 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
2092 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2093 if (pmap == pv->pv_pmap && va == pv->pv_va)
2097 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
2098 if (va == pv->pv_va)
2104 /* JGXXX When can 'pv' be NULL? */
2106 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2107 m->md.pv_list_count--;
2108 KKASSERT(m->md.pv_list_count >= 0);
2109 if (TAILQ_EMPTY(&m->md.pv_list))
2110 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2111 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
2112 ++pmap->pm_generation;
2113 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem, info);
2121 * Create a pv entry for page at pa for
2125 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
2131 pv = get_pv_entry();
2136 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
2137 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2138 m->md.pv_list_count++;
2144 * pmap_remove_pte: do the things to unmap a page in a process
2147 pmap_remove_pte(struct pmap *pmap, pt_entry_t *ptq, vm_offset_t va,
2148 pmap_inval_info_t info)
2154 pmap_inval_add(info, pmap, va);
2155 oldpte = pte_load_clear(ptq);
2157 pmap->pm_stats.wired_count -= 1;
2159 * Machines that don't support invlpg, also don't support
2160 * PG_G. XXX PG_G is disabled for SMP so don't worry about
2164 cpu_invlpg((void *)va);
2165 KKASSERT(pmap->pm_stats.resident_count > 0);
2166 --pmap->pm_stats.resident_count;
2167 if (oldpte & PG_MANAGED) {
2168 m = PHYS_TO_VM_PAGE(oldpte);
2169 if (oldpte & PG_M) {
2170 #if defined(PMAP_DIAGNOSTIC)
2171 if (pmap_nw_modified((pt_entry_t) oldpte)) {
2173 "pmap_remove: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2177 if (pmap_track_modified(va))
2181 vm_page_flag_set(m, PG_REFERENCED);
2182 return pmap_remove_entry(pmap, m, va, info);
2184 return pmap_unuse_pt(pmap, va, NULL, info);
2193 * Remove a single page from a process address space.
2195 * This function may not be called from an interrupt if the pmap is
2199 pmap_remove_page(struct pmap *pmap, vm_offset_t va, pmap_inval_info_t info)
2204 pte = pmap_pte(pmap, va);
2207 if ((*pte & PG_V) == 0)
2209 pmap_remove_pte(pmap, pte, va, info);
2215 * Remove the given range of addresses from the specified map.
2217 * It is assumed that the start and end are properly
2218 * rounded to the page size.
2220 * This function may not be called from an interrupt if the pmap is
2224 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
2227 vm_offset_t va_next;
2228 pml4_entry_t *pml4e;
2230 pd_entry_t ptpaddr, *pde;
2232 struct pmap_inval_info info;
2237 if (pmap->pm_stats.resident_count == 0)
2240 pmap_inval_init(&info);
2243 * special handling of removing one page. a very
2244 * common operation and easy to short circuit some
2247 if (sva + PAGE_SIZE == eva) {
2248 pde = pmap_pde(pmap, sva);
2249 if (pde && (*pde & PG_PS) == 0) {
2250 pmap_remove_page(pmap, sva, &info);
2251 pmap_inval_flush(&info);
2256 for (; sva < eva; sva = va_next) {
2257 pml4e = pmap_pml4e(pmap, sva);
2258 if ((*pml4e & PG_V) == 0) {
2259 va_next = (sva + NBPML4) & ~PML4MASK;
2265 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
2266 if ((*pdpe & PG_V) == 0) {
2267 va_next = (sva + NBPDP) & ~PDPMASK;
2274 * Calculate index for next page table.
2276 va_next = (sva + NBPDR) & ~PDRMASK;
2280 pde = pmap_pdpe_to_pde(pdpe, sva);
2284 * Weed out invalid mappings.
2290 * Check for large page.
2292 if ((ptpaddr & PG_PS) != 0) {
2293 /* JG FreeBSD has more complex treatment here */
2294 pmap_inval_add(&info, pmap, -1);
2296 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2301 * Limit our scan to either the end of the va represented
2302 * by the current page table page, or to the end of the
2303 * range being removed.
2309 * NOTE: pmap_remove_pte() can block.
2311 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2315 if (pmap_remove_pte(pmap, pte, sva, &info))
2319 pmap_inval_flush(&info);
2325 * Removes this physical page from all physical maps in which it resides.
2326 * Reflects back modify bits to the pager.
2328 * This routine may not be called from an interrupt.
2332 pmap_remove_all(vm_page_t m)
2335 struct pmap_inval_info info;
2336 pt_entry_t *pte, tpte;
2339 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2342 pmap_inval_init(&info);
2344 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2345 KKASSERT(pv->pv_pmap->pm_stats.resident_count > 0);
2346 --pv->pv_pmap->pm_stats.resident_count;
2348 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2349 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
2350 tpte = pte_load_clear(pte);
2353 pv->pv_pmap->pm_stats.wired_count--;
2356 vm_page_flag_set(m, PG_REFERENCED);
2359 * Update the vm_page_t clean and reference bits.
2362 #if defined(PMAP_DIAGNOSTIC)
2363 if (pmap_nw_modified(tpte)) {
2365 "pmap_remove_all: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2369 if (pmap_track_modified(pv->pv_va))
2372 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2373 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2374 ++pv->pv_pmap->pm_generation;
2375 m->md.pv_list_count--;
2376 KKASSERT(m->md.pv_list_count >= 0);
2377 if (TAILQ_EMPTY(&m->md.pv_list))
2378 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2379 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem, &info);
2383 KKASSERT((m->flags & (PG_MAPPED|PG_WRITEABLE)) == 0);
2384 pmap_inval_flush(&info);
2390 * Set the physical protection on the specified range of this map
2393 * This function may not be called from an interrupt if the map is
2394 * not the kernel_pmap.
2397 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
2400 vm_offset_t va_next;
2401 pml4_entry_t *pml4e;
2403 pd_entry_t ptpaddr, *pde;
2405 pmap_inval_info info;
2407 /* JG review for NX */
2412 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
2413 pmap_remove(pmap, sva, eva);
2417 if (prot & VM_PROT_WRITE)
2420 pmap_inval_init(&info);
2422 for (; sva < eva; sva = va_next) {
2424 pml4e = pmap_pml4e(pmap, sva);
2425 if ((*pml4e & PG_V) == 0) {
2426 va_next = (sva + NBPML4) & ~PML4MASK;
2432 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
2433 if ((*pdpe & PG_V) == 0) {
2434 va_next = (sva + NBPDP) & ~PDPMASK;
2440 va_next = (sva + NBPDR) & ~PDRMASK;
2444 pde = pmap_pdpe_to_pde(pdpe, sva);
2448 * Check for large page.
2450 if ((ptpaddr & PG_PS) != 0) {
2451 pmap_inval_add(&info, pmap, -1);
2452 *pde &= ~(PG_M|PG_RW);
2453 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2458 * Weed out invalid mappings. Note: we assume that the page
2459 * directory table is always allocated, and in kernel virtual.
2467 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2469 pt_entry_t obits, pbits;
2473 * XXX non-optimal. Note also that there can be
2474 * no pmap_inval_flush() calls until after we modify
2475 * ptbase[sindex] (or otherwise we have to do another
2476 * pmap_inval_add() call).
2478 pmap_inval_add(&info, pmap, sva);
2479 obits = pbits = *pte;
2480 if ((pbits & PG_V) == 0)
2482 if (pbits & PG_MANAGED) {
2485 m = PHYS_TO_VM_PAGE(pbits & PG_FRAME);
2486 vm_page_flag_set(m, PG_REFERENCED);
2490 if (pmap_track_modified(sva)) {
2492 KKASSERT(pbits == (pbits & PG_FRAME));
2493 m = PHYS_TO_VM_PAGE(pbits);
2502 if (pbits != obits) {
2507 pmap_inval_flush(&info);
2511 * Insert the given physical page (p) at
2512 * the specified virtual address (v) in the
2513 * target physical map with the protection requested.
2515 * If specified, the page will be wired down, meaning
2516 * that the related pte can not be reclaimed.
2518 * NB: This is the only routine which MAY NOT lazy-evaluate
2519 * or lose information. That is, this routine must actually
2520 * insert this page into the given map NOW.
2523 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
2531 pt_entry_t origpte, newpte;
2533 pmap_inval_info info;
2538 va = trunc_page(va);
2539 #ifdef PMAP_DIAGNOSTIC
2541 panic("pmap_enter: toobig");
2542 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
2543 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%lx)", va);
2545 if (va < UPT_MAX_ADDRESS && pmap == &kernel_pmap) {
2546 kprintf("Warning: pmap_enter called on UVA with kernel_pmap\n");
2548 db_print_backtrace();
2551 if (va >= UPT_MAX_ADDRESS && pmap != &kernel_pmap) {
2552 kprintf("Warning: pmap_enter called on KVA without kernel_pmap\n");
2554 db_print_backtrace();
2559 * In the case that a page table page is not
2560 * resident, we are creating it here.
2562 if (va < VM_MAX_USER_ADDRESS)
2563 mpte = pmap_allocpte(pmap, va);
2567 pmap_inval_init(&info);
2568 pde = pmap_pde(pmap, va);
2569 if (pde != NULL && (*pde & PG_V) != 0) {
2570 if ((*pde & PG_PS) != 0)
2571 panic("pmap_enter: attempted pmap_enter on 2MB page");
2572 pte = pmap_pde_to_pte(pde, va);
2574 panic("pmap_enter: invalid page directory va=%#lx", va);
2576 KKASSERT(pte != NULL);
2577 pa = VM_PAGE_TO_PHYS(m);
2578 KKASSERT(pa == (pa & PG_FRAME));
2580 opa = origpte & PG_FRAME;
2583 * Mapping has not changed, must be protection or wiring change.
2585 if (origpte && (opa == pa)) {
2587 * Wiring change, just update stats. We don't worry about
2588 * wiring PT pages as they remain resident as long as there
2589 * are valid mappings in them. Hence, if a user page is wired,
2590 * the PT page will be also.
2592 if (wired && ((origpte & PG_W) == 0))
2593 pmap->pm_stats.wired_count++;
2594 else if (!wired && (origpte & PG_W))
2595 pmap->pm_stats.wired_count--;
2597 #if defined(PMAP_DIAGNOSTIC)
2598 if (pmap_nw_modified(origpte)) {
2600 "pmap_enter: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2606 * Remove the extra pte reference. Note that we cannot
2607 * optimize the RO->RW case because we have adjusted the
2608 * wiring count above and may need to adjust the wiring
2615 * We might be turning off write access to the page,
2616 * so we go ahead and sense modify status.
2618 if (origpte & PG_MANAGED) {
2619 if ((origpte & PG_M) && pmap_track_modified(va)) {
2621 om = PHYS_TO_VM_PAGE(opa);
2625 KKASSERT(m->flags & PG_MAPPED);
2630 * Mapping has changed, invalidate old range and fall through to
2631 * handle validating new mapping.
2635 err = pmap_remove_pte(pmap, pte, va, &info);
2637 panic("pmap_enter: pte vanished, va: 0x%lx", va);
2641 * Enter on the PV list if part of our managed memory. Note that we
2642 * raise IPL while manipulating pv_table since pmap_enter can be
2643 * called at interrupt time.
2645 if (pmap_initialized &&
2646 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2647 pmap_insert_entry(pmap, va, mpte, m);
2649 vm_page_flag_set(m, PG_MAPPED);
2653 * Increment counters
2655 ++pmap->pm_stats.resident_count;
2657 pmap->pm_stats.wired_count++;
2661 * Now validate mapping with desired protection/wiring.
2663 newpte = (pt_entry_t) (pa | pte_prot(pmap, prot) | PG_V);
2667 if (va < VM_MAX_USER_ADDRESS)
2669 if (pmap == &kernel_pmap)
2673 * if the mapping or permission bits are different, we need
2674 * to update the pte.
2676 if ((origpte & ~(PG_M|PG_A)) != newpte) {
2677 pmap_inval_add(&info, pmap, va);
2678 *pte = newpte | PG_A;
2680 vm_page_flag_set(m, PG_WRITEABLE);
2682 KKASSERT((newpte & PG_MANAGED) == 0 || (m->flags & PG_MAPPED));
2683 pmap_inval_flush(&info);
2687 * This code works like pmap_enter() but assumes VM_PROT_READ and not-wired.
2688 * This code also assumes that the pmap has no pre-existing entry for this
2691 * This code currently may only be used on user pmaps, not kernel_pmap.
2694 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m)
2700 vm_pindex_t ptepindex;
2702 pmap_inval_info info;
2704 pmap_inval_init(&info);
2706 if (va < UPT_MAX_ADDRESS && pmap == &kernel_pmap) {
2707 kprintf("Warning: pmap_enter_quick called on UVA with kernel_pmap\n");
2709 db_print_backtrace();
2712 if (va >= UPT_MAX_ADDRESS && pmap != &kernel_pmap) {
2713 kprintf("Warning: pmap_enter_quick called on KVA without kernel_pmap\n");
2715 db_print_backtrace();
2719 KKASSERT(va < UPT_MIN_ADDRESS); /* assert used on user pmaps only */
2722 * Calculate the page table page (mpte), allocating it if necessary.
2724 * A held page table page (mpte), or NULL, is passed onto the
2725 * section following.
2727 if (va < VM_MAX_USER_ADDRESS) {
2729 * Calculate pagetable page index
2731 ptepindex = pmap_pde_pindex(va);
2735 * Get the page directory entry
2737 ptepa = pmap_pde(pmap, va);
2740 * If the page table page is mapped, we just increment
2741 * the hold count, and activate it.
2743 if (ptepa && (*ptepa & PG_V) != 0) {
2745 panic("pmap_enter_quick: unexpected mapping into 2MB page");
2746 // if (pmap->pm_ptphint &&
2747 // (pmap->pm_ptphint->pindex == ptepindex)) {
2748 // mpte = pmap->pm_ptphint;
2750 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
2751 pmap->pm_ptphint = mpte;
2756 mpte = _pmap_allocpte(pmap, ptepindex);
2758 } while (mpte == NULL);
2761 /* this code path is not yet used */
2765 * With a valid (and held) page directory page, we can just use
2766 * vtopte() to get to the pte. If the pte is already present
2767 * we do not disturb it.
2772 pmap_unwire_pte_hold(pmap, va, mpte, &info);
2773 pa = VM_PAGE_TO_PHYS(m);
2774 KKASSERT(((*pte ^ pa) & PG_FRAME) == 0);
2779 * Enter on the PV list if part of our managed memory
2781 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2782 pmap_insert_entry(pmap, va, mpte, m);
2783 vm_page_flag_set(m, PG_MAPPED);
2787 * Increment counters
2789 ++pmap->pm_stats.resident_count;
2791 pa = VM_PAGE_TO_PHYS(m);
2794 * Now validate mapping with RO protection
2796 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2797 *pte = pa | PG_V | PG_U;
2799 *pte = pa | PG_V | PG_U | PG_MANAGED;
2800 /* pmap_inval_add(&info, pmap, va); shouldn't be needed inval->valid */
2801 pmap_inval_flush(&info);
2805 * Make a temporary mapping for a physical address. This is only intended
2806 * to be used for panic dumps.
2808 /* JG Needed on amd64? */
2810 pmap_kenter_temporary(vm_paddr_t pa, int i)
2813 pmap_kenter((vm_offset_t)crashdumpmap + (i * PAGE_SIZE), pa);
2814 return ((void *)crashdumpmap);
2817 #define MAX_INIT_PT (96)
2820 * This routine preloads the ptes for a given object into the specified pmap.
2821 * This eliminates the blast of soft faults on process startup and
2822 * immediately after an mmap.
2824 static int pmap_object_init_pt_callback(vm_page_t p, void *data);
2827 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2828 vm_object_t object, vm_pindex_t pindex,
2829 vm_size_t size, int limit)
2832 struct rb_vm_page_scan_info info;
2837 * We can't preinit if read access isn't set or there is no pmap
2840 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2844 * We can't preinit if the pmap is not the current pmap
2846 lp = curthread->td_lwp;
2847 if (lp == NULL || pmap != vmspace_pmap(lp->lwp_vmspace))
2850 psize = amd64_btop(size);
2852 if ((object->type != OBJT_VNODE) ||
2853 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2854 (object->resident_page_count > MAX_INIT_PT))) {
2858 if (psize + pindex > object->size) {
2859 if (object->size < pindex)
2861 psize = object->size - pindex;
2868 * Use a red-black scan to traverse the requested range and load
2869 * any valid pages found into the pmap.
2871 * We cannot safely scan the object's memq unless we are in a
2872 * critical section since interrupts can remove pages from objects.
2874 info.start_pindex = pindex;
2875 info.end_pindex = pindex + psize - 1;
2882 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2883 pmap_object_init_pt_callback, &info);
2889 pmap_object_init_pt_callback(vm_page_t p, void *data)
2892 struct rb_vm_page_scan_info *info = data;
2893 vm_pindex_t rel_index;
2895 * don't allow an madvise to blow away our really
2896 * free pages allocating pv entries.
2898 if ((info->limit & MAP_PREFAULT_MADVISE) &&
2899 vmstats.v_free_count < vmstats.v_free_reserved) {
2902 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2903 (p->busy == 0) && (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2904 if ((p->queue - p->pc) == PQ_CACHE)
2905 vm_page_deactivate(p);
2907 rel_index = p->pindex - info->start_pindex;
2908 pmap_enter_quick(info->pmap,
2909 info->addr + amd64_ptob(rel_index), p);
2916 * pmap_prefault provides a quick way of clustering pagefaults into a
2917 * processes address space. It is a "cousin" of pmap_object_init_pt,
2918 * except it runs at page fault time instead of mmap time.
2922 #define PAGEORDER_SIZE (PFBAK+PFFOR)
2924 static int pmap_prefault_pageorder[] = {
2925 -PAGE_SIZE, PAGE_SIZE,
2926 -2 * PAGE_SIZE, 2 * PAGE_SIZE,
2927 -3 * PAGE_SIZE, 3 * PAGE_SIZE,
2928 -4 * PAGE_SIZE, 4 * PAGE_SIZE
2932 pmap_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry)
2944 * We do not currently prefault mappings that use virtual page
2945 * tables. We do not prefault foreign pmaps.
2947 if (entry->maptype == VM_MAPTYPE_VPAGETABLE)
2949 lp = curthread->td_lwp;
2950 if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
2953 object = entry->object.vm_object;
2955 starta = addra - PFBAK * PAGE_SIZE;
2956 if (starta < entry->start)
2957 starta = entry->start;
2958 else if (starta > addra)
2962 * critical section protection is required to maintain the
2963 * page/object association, interrupts can free pages and remove
2964 * them from their objects.
2967 for (i = 0; i < PAGEORDER_SIZE; i++) {
2968 vm_object_t lobject;
2971 addr = addra + pmap_prefault_pageorder[i];
2972 if (addr > addra + (PFFOR * PAGE_SIZE))
2975 if (addr < starta || addr >= entry->end)
2978 if ((*pmap_pde(pmap, addr)) == 0)
2985 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2988 for (m = vm_page_lookup(lobject, pindex);
2989 (!m && (lobject->type == OBJT_DEFAULT) &&
2990 (lobject->backing_object));
2991 lobject = lobject->backing_object
2993 if (lobject->backing_object_offset & PAGE_MASK)
2995 pindex += (lobject->backing_object_offset >> PAGE_SHIFT);
2996 m = vm_page_lookup(lobject->backing_object, pindex);
3000 * give-up when a page is not in memory
3005 if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
3007 (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
3009 if ((m->queue - m->pc) == PQ_CACHE) {
3010 vm_page_deactivate(m);
3013 pmap_enter_quick(pmap, addr, m);
3021 * Routine: pmap_change_wiring
3022 * Function: Change the wiring attribute for a map/virtual-address
3024 * In/out conditions:
3025 * The mapping must already exist in the pmap.
3028 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
3036 pte = pmap_pte(pmap, va);
3038 if (wired && !pmap_pte_w(pte))
3039 pmap->pm_stats.wired_count++;
3040 else if (!wired && pmap_pte_w(pte))
3041 pmap->pm_stats.wired_count--;
3044 * Wiring is not a hardware characteristic so there is no need to
3045 * invalidate TLB. However, in an SMP environment we must use
3046 * a locked bus cycle to update the pte (if we are not using
3047 * the pmap_inval_*() API that is)... it's ok to do this for simple
3052 atomic_set_int(pte, PG_W);
3054 atomic_clear_int(pte, PG_W);
3057 atomic_set_int_nonlocked(pte, PG_W);
3059 atomic_clear_int_nonlocked(pte, PG_W);
3066 * Copy the range specified by src_addr/len
3067 * from the source map to the range dst_addr/len
3068 * in the destination map.
3070 * This routine is only advisory and need not do anything.
3073 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
3074 vm_size_t len, vm_offset_t src_addr)
3077 pmap_inval_info info;
3079 vm_offset_t end_addr = src_addr + len;
3081 pd_entry_t src_frame, dst_frame;
3084 if (dst_addr != src_addr)
3087 * XXX BUGGY. Amoung other things srcmpte is assumed to remain
3088 * valid through blocking calls, and that's just not going to
3096 src_frame = src_pmap->pm_pdir[PTDPTDI] & PG_FRAME;
3097 if (src_frame != (PTDpde & PG_FRAME)) {
3101 dst_frame = dst_pmap->pm_pdir[PTDPTDI] & PG_FRAME;
3102 if (dst_frame != (APTDpde & PG_FRAME)) {
3103 APTDpde = (pd_entry_t) (dst_frame | PG_RW | PG_V);
3104 /* The page directory is not shared between CPUs */
3108 pmap_inval_init(&info);
3109 pmap_inval_add(&info, dst_pmap, -1);
3110 pmap_inval_add(&info, src_pmap, -1);
3113 * critical section protection is required to maintain the page/object
3114 * association, interrupts can free pages and remove them from
3118 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
3119 pt_entry_t *src_pte, *dst_pte;
3120 vm_page_t dstmpte, srcmpte;
3121 vm_offset_t srcptepaddr;
3122 vm_pindex_t ptepindex;
3124 if (addr >= UPT_MIN_ADDRESS)
3125 panic("pmap_copy: invalid to pmap_copy page tables\n");
3128 * Don't let optional prefaulting of pages make us go
3129 * way below the low water mark of free pages or way
3130 * above high water mark of used pv entries.
3132 if (vmstats.v_free_count < vmstats.v_free_reserved ||
3133 pv_entry_count > pv_entry_high_water)
3136 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
3137 ptepindex = addr >> PDRSHIFT;
3140 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex];
3142 if (srcptepaddr == 0)
3145 if (srcptepaddr & PG_PS) {
3147 if (dst_pmap->pm_pdir[ptepindex] == 0) {
3148 dst_pmap->pm_pdir[ptepindex] = (pd_entry_t) srcptepaddr;
3149 dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
3155 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
3156 if ((srcmpte == NULL) || (srcmpte->hold_count == 0) ||
3157 (srcmpte->flags & PG_BUSY)) {
3161 if (pdnxt > end_addr)
3164 src_pte = vtopte(addr);
3166 dst_pte = avtopte(addr);
3168 while (addr < pdnxt) {
3173 * we only virtual copy managed pages
3175 if ((ptetemp & PG_MANAGED) != 0) {
3177 * We have to check after allocpte for the
3178 * pte still being around... allocpte can
3181 * pmap_allocpte() can block. If we lose
3182 * our page directory mappings we stop.
3184 dstmpte = pmap_allocpte(dst_pmap, addr);
3187 if (src_frame != (PTDpde & PG_FRAME) ||
3188 dst_frame != (APTDpde & PG_FRAME)
3190 kprintf("WARNING: pmap_copy: detected and corrected race\n");
3191 pmap_unwire_pte_hold(dst_pmap, dstmpte, &info);
3193 } else if ((*dst_pte == 0) &&
3194 (ptetemp = *src_pte) != 0 &&
3195 (ptetemp & PG_MANAGED)) {
3197 * Clear the modified and
3198 * accessed (referenced) bits
3201 m = PHYS_TO_VM_PAGE(ptetemp);
3202 *dst_pte = ptetemp & ~(PG_M | PG_A);
3203 ++dst_pmap->pm_stats.resident_count;
3204 pmap_insert_entry(dst_pmap, addr,
3206 KKASSERT(m->flags & PG_MAPPED);
3208 kprintf("WARNING: pmap_copy: dst_pte race detected and corrected\n");
3209 pmap_unwire_pte_hold(dst_pmap, dstmpte, &info);
3213 if (dstmpte->hold_count >= srcmpte->hold_count)
3223 pmap_inval_flush(&info);
3229 * Zero the specified physical page.
3231 * This function may be called from an interrupt and no locking is
3235 pmap_zero_page(vm_paddr_t phys)
3238 vm_offset_t va = PHYS_TO_DMAP(phys);
3240 pagezero((void *)va);
3244 * pmap_page_assertzero:
3246 * Assert that a page is empty, panic if it isn't.
3249 pmap_page_assertzero(vm_paddr_t phys)
3252 struct mdglobaldata *gd = mdcpu;
3256 vm_offset_t virt = PHYS_TO_DMAP(phys);
3258 for (i = 0; i < PAGE_SIZE; i += sizeof(int)) {
3259 if (*(int *)((char *)virt + i) != 0) {
3260 panic("pmap_page_assertzero() @ %p not zero!\n",
3270 * Zero part of a physical page by mapping it into memory and clearing
3271 * its contents with bzero.
3273 * off and size may not cover an area beyond a single hardware page.
3276 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
3279 struct mdglobaldata *gd = mdcpu;
3282 vm_offset_t virt = PHYS_TO_DMAP(phys);
3283 bzero((char *)virt + off, size);
3290 * Copy the physical page from the source PA to the target PA.
3291 * This function may be called from an interrupt. No locking
3295 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
3298 vm_offset_t src_virt, dst_virt;
3301 src_virt = PHYS_TO_DMAP(src);
3302 dst_virt = PHYS_TO_DMAP(dst);
3303 bcopy(src_virt, dst_virt, PAGE_SIZE);
3308 * pmap_copy_page_frag:
3310 * Copy the physical page from the source PA to the target PA.
3311 * This function may be called from an interrupt. No locking
3315 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
3318 vm_offset_t src_virt, dst_virt;
3321 src_virt = PHYS_TO_DMAP(src);
3322 dst_virt = PHYS_TO_DMAP(dst);
3323 bcopy((char *)src_virt + (src & PAGE_MASK),
3324 (char *)dst_virt + (dst & PAGE_MASK),
3330 * Returns true if the pmap's pv is one of the first
3331 * 16 pvs linked to from this page. This count may
3332 * be changed upwards or downwards in the future; it
3333 * is only necessary that true be returned for a small
3334 * subset of pmaps for proper page aging.
3337 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
3343 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3348 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3349 if (pv->pv_pmap == pmap) {
3362 * Remove all pages from specified address space
3363 * this aids process exit speeds. Also, this code
3364 * is special cased for current process only, but
3365 * can have the more generic (and slightly slower)
3366 * mode enabled. This is much faster than pmap_remove
3367 * in the case of running down an entire address space.
3370 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
3374 pt_entry_t *pte, tpte;
3377 pmap_inval_info info;
3379 int save_generation;
3381 lp = curthread->td_lwp;
3382 if (lp && pmap == vmspace_pmap(lp->lwp_vmspace))
3387 pmap_inval_init(&info);
3389 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
3390 if (pv->pv_va >= eva || pv->pv_va < sva) {
3391 npv = TAILQ_NEXT(pv, pv_plist);
3395 KKASSERT(pmap == pv->pv_pmap);
3398 pte = vtopte(pv->pv_va);
3400 pte = pmap_pte_quick(pmap, pv->pv_va);
3401 if (pmap->pm_active)
3402 pmap_inval_add(&info, pmap, pv->pv_va);
3405 * We cannot remove wired pages from a process' mapping
3409 npv = TAILQ_NEXT(pv, pv_plist);
3412 tpte = pte_load_clear(pte);
3414 m = PHYS_TO_VM_PAGE(tpte & PG_FRAME);
3416 KASSERT(m < &vm_page_array[vm_page_array_size],
3417 ("pmap_remove_pages: bad tpte %lx", tpte));
3419 KKASSERT(pmap->pm_stats.resident_count > 0);
3420 --pmap->pm_stats.resident_count;
3423 * Update the vm_page_t clean and reference bits.
3429 npv = TAILQ_NEXT(pv, pv_plist);
3430 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
3431 save_generation = ++pmap->pm_generation;
3433 m->md.pv_list_count--;
3434 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3435 if (TAILQ_EMPTY(&m->md.pv_list))
3436 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
3438 pmap_unuse_pt(pmap, pv->pv_va, pv->pv_ptem, &info);
3442 * Restart the scan if we blocked during the unuse or free
3443 * calls and other removals were made.
3445 if (save_generation != pmap->pm_generation) {
3446 kprintf("Warning: pmap_remove_pages race-A avoided\n");
3447 pv = TAILQ_FIRST(&pmap->pm_pvlist);
3450 pmap_inval_flush(&info);
3455 * pmap_testbit tests bits in pte's
3456 * note that the testbit/clearbit routines are inline,
3457 * and a lot of things compile-time evaluate.
3460 pmap_testbit(vm_page_t m, int bit)
3466 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3469 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
3474 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3476 * if the bit being tested is the modified bit, then
3477 * mark clean_map and ptes as never
3480 if (bit & (PG_A|PG_M)) {
3481 if (!pmap_track_modified(pv->pv_va))
3485 #if defined(PMAP_DIAGNOSTIC)
3486 if (pv->pv_pmap == NULL) {
3487 kprintf("Null pmap (tb) at va: 0x%lx\n", pv->pv_va);
3491 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3502 * this routine is used to modify bits in ptes
3504 static __inline void
3505 pmap_clearbit(vm_page_t m, int bit)
3508 struct pmap_inval_info info;
3513 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3516 pmap_inval_init(&info);
3520 * Loop over all current mappings setting/clearing as appropos If
3521 * setting RO do we need to clear the VAC?
3523 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3525 * don't write protect pager mappings
3528 if (!pmap_track_modified(pv->pv_va))
3532 #if defined(PMAP_DIAGNOSTIC)
3533 if (pv->pv_pmap == NULL) {
3534 kprintf("Null pmap (cb) at va: 0x%lx\n", pv->pv_va);
3540 * Careful here. We can use a locked bus instruction to
3541 * clear PG_A or PG_M safely but we need to synchronize
3542 * with the target cpus when we mess with PG_RW.
3544 * We do not have to force synchronization when clearing
3545 * PG_M even for PTEs generated via virtual memory maps,
3546 * because the virtual kernel will invalidate the pmap
3547 * entry when/if it needs to resynchronize the Modify bit.
3550 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
3551 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3558 atomic_clear_long(pte, PG_M|PG_RW);
3561 * The cpu may be trying to set PG_M
3562 * simultaniously with our clearing
3565 if (!atomic_cmpset_long(pte, pbits,
3569 } else if (bit == PG_M) {
3571 * We could also clear PG_RW here to force
3572 * a fault on write to redetect PG_M for
3573 * virtual kernels, but it isn't necessary
3574 * since virtual kernels invalidate the pte
3575 * when they clear the VPTE_M bit in their
3576 * virtual page tables.
3578 atomic_clear_long(pte, PG_M);
3580 atomic_clear_long(pte, bit);
3584 pmap_inval_flush(&info);
3589 * pmap_page_protect:
3591 * Lower the permission for all mappings to a given page.
3594 pmap_page_protect(vm_page_t m, vm_prot_t prot)
3597 /* JG NX support? */
3598 if ((prot & VM_PROT_WRITE) == 0) {
3599 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
3600 pmap_clearbit(m, PG_RW);
3601 vm_page_flag_clear(m, PG_WRITEABLE);
3609 pmap_phys_address(vm_pindex_t ppn)
3612 return (amd64_ptob(ppn));
3616 * pmap_ts_referenced:
3618 * Return a count of reference bits for a page, clearing those bits.
3619 * It is not necessary for every reference bit to be cleared, but it
3620 * is necessary that 0 only be returned when there are truly no
3621 * reference bits set.
3623 * XXX: The exact number of bits to check and clear is a matter that
3624 * should be tested and standardized at some point in the future for
3625 * optimal aging of shared pages.
3628 pmap_ts_referenced(vm_page_t m)
3631 pv_entry_t pv, pvf, pvn;
3635 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3640 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3645 pvn = TAILQ_NEXT(pv, pv_list);
3647 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3649 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3651 if (!pmap_track_modified(pv->pv_va))
3654 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3656 if (pte && (*pte & PG_A)) {
3658 atomic_clear_long(pte, PG_A);
3660 atomic_clear_long_nonlocked(pte, PG_A);
3667 } while ((pv = pvn) != NULL && pv != pvf);
3677 * Return whether or not the specified physical page was modified
3678 * in any physical maps.
3681 pmap_is_modified(vm_page_t m)
3684 return pmap_testbit(m, PG_M);
3688 * Clear the modify bits on the specified physical page.
3691 pmap_clear_modify(vm_page_t m)
3694 pmap_clearbit(m, PG_M);
3698 * pmap_clear_reference:
3700 * Clear the reference bit on the specified physical page.
3703 pmap_clear_reference(vm_page_t m)
3706 pmap_clearbit(m, PG_A);
3710 * Miscellaneous support routines follow
3714 i386_protection_init(void)
3719 /* JG NX support may go here; No VM_PROT_EXECUTE ==> set NX bit */
3720 kp = protection_codes;
3721 for (prot = 0; prot < 8; prot++) {
3723 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE:
3725 * Read access is also 0. There isn't any execute bit,
3726 * so just make it readable.
3728 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE:
3729 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE:
3730 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE:
3733 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE:
3734 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE:
3735 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE:
3736 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
3744 * Map a set of physical memory pages into the kernel virtual
3745 * address space. Return a pointer to where it is mapped. This
3746 * routine is intended to be used for mapping device memory,
3749 * NOTE: we can't use pgeflag unless we invalidate the pages one at
3753 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
3756 vm_offset_t va, tmpva, offset;
3759 offset = pa & PAGE_MASK;
3760 size = roundup(offset + size, PAGE_SIZE);
3762 va = kmem_alloc_nofault(&kernel_map, size);
3764 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3766 pa = pa & ~PAGE_MASK;
3767 for (tmpva = va; size > 0;) {
3768 pte = vtopte(tmpva);
3769 *pte = pa | PG_RW | PG_V; /* | pgeflag; */
3777 return ((void *)(va + offset));
3781 pmap_unmapdev(vm_offset_t va, vm_size_t size)
3784 vm_offset_t base, offset;
3786 base = va & ~PAGE_MASK;
3787 offset = va & PAGE_MASK;
3788 size = roundup(offset + size, PAGE_SIZE);
3789 pmap_qremove(va, size >> PAGE_SHIFT);
3790 kmem_free(&kernel_map, base, size);
3794 * perform the pmap work for mincore
3797 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3800 pt_entry_t *ptep, pte;
3804 ptep = pmap_pte(pmap, addr);
3809 if ((pte = *ptep) != 0) {
3812 val = MINCORE_INCORE;
3813 if ((pte & PG_MANAGED) == 0)
3816 pa = pte & PG_FRAME;
3818 m = PHYS_TO_VM_PAGE(pa);
3824 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3826 * Modified by someone
3828 else if (m->dirty || pmap_is_modified(m))
3829 val |= MINCORE_MODIFIED_OTHER;
3834 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3837 * Referenced by someone
3839 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3840 val |= MINCORE_REFERENCED_OTHER;
3841 vm_page_flag_set(m, PG_REFERENCED);
3848 * Replace p->p_vmspace with a new one. If adjrefs is non-zero the new
3849 * vmspace will be ref'd and the old one will be deref'd.
3851 * The vmspace for all lwps associated with the process will be adjusted
3852 * and cr3 will be reloaded if any lwp is the current lwp.
3855 pmap_replacevm(struct proc *p, struct vmspace *newvm, int adjrefs)
3858 struct vmspace *oldvm;
3862 oldvm = p->p_vmspace;
3863 if (oldvm != newvm) {
3864 p->p_vmspace = newvm;
3865 KKASSERT(p->p_nthreads == 1);
3866 lp = RB_ROOT(&p->p_lwp_tree);
3867 pmap_setlwpvm(lp, newvm);
3869 sysref_get(&newvm->vm_sysref);
3870 sysref_put(&oldvm->vm_sysref);
3877 * Set the vmspace for a LWP. The vmspace is almost universally set the
3878 * same as the process vmspace, but virtual kernels need to swap out contexts
3879 * on a per-lwp basis.
3882 pmap_setlwpvm(struct lwp *lp, struct vmspace *newvm)
3885 struct vmspace *oldvm;
3889 oldvm = lp->lwp_vmspace;
3891 if (oldvm != newvm) {
3892 lp->lwp_vmspace = newvm;
3893 if (curthread->td_lwp == lp) {
3894 pmap = vmspace_pmap(newvm);
3896 atomic_set_int(&pmap->pm_active, 1 << mycpu->gd_cpuid);
3898 pmap->pm_active |= 1;
3900 #if defined(SWTCH_OPTIM_STATS)
3903 curthread->td_pcb->pcb_cr3 = vtophys(pmap->pm_pml4);
3904 load_cr3(curthread->td_pcb->pcb_cr3);
3905 pmap = vmspace_pmap(oldvm);
3907 atomic_clear_int(&pmap->pm_active,
3908 1 << mycpu->gd_cpuid);
3910 pmap->pm_active &= ~1;
3918 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3922 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3926 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3933 static void pads (pmap_t pm);
3934 void pmap_pvdump (vm_paddr_t pa);
3936 /* print address space of pmap*/
3945 if (pm == &kernel_pmap)
3948 for (i = 0; i < NPDEPG; i++) {
3950 if (pm->pm_pdir[i]) {
3951 for (j = 0; j < NPTEPG; j++) {
3952 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
3953 if (pm == &kernel_pmap && va < KERNBASE)
3955 if (pm != &kernel_pmap && va > UPT_MAX_ADDRESS)
3957 ptep = pmap_pte_quick(pm, va);
3958 if (pmap_pte_v(ptep))
3959 kprintf("%lx:%lx ", va, *ptep);
3969 pmap_pvdump(vm_paddr_t pa)
3975 kprintf("pa %08llx", (long long)pa);
3976 m = PHYS_TO_VM_PAGE(pa);
3977 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3979 kprintf(" -> pmap %p, va %x, flags %x",
3980 (void *)pv->pv_pmap, pv->pv_va, pv->pv_flags);
3982 kprintf(" -> pmap %p, va %x", (void *)pv->pv_pmap, pv->pv_va);