kernel - Add Proportional RSS (PRES)
[dragonfly.git] / sys / platform / pc64 / x86_64 / pmap.c
blob6bb1e25a1ca989cc100f9ee903647a5901b807bb
1 /*
2 * (MPSAFE)
4 * Copyright (c) 1991 Regents of the University of California.
5 * Copyright (c) 1994 John S. Dyson
6 * Copyright (c) 1994 David Greenman
7 * Copyright (c) 2003 Peter Wemm
8 * Copyright (c) 2005-2008 Alan L. Cox <alc@cs.rice.edu>
9 * Copyright (c) 2008, 2009 The DragonFly Project.
10 * Copyright (c) 2008, 2009 Jordan Gordeev.
11 * All rights reserved.
13 * This code is derived from software contributed to Berkeley by
14 * the Systems Programming Group of the University of Utah Computer
15 * Science Department and William Jolitz of UUNET Technologies Inc.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. All advertising materials mentioning features or use of this software
26 * must display the following acknowledgement:
27 * This product includes software developed by the University of
28 * California, Berkeley and its contributors.
29 * 4. Neither the name of the University nor the names of its contributors
30 * may be used to endorse or promote products derived from this software
31 * without specific prior written permission.
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * SUCH DAMAGE.
45 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91
46 * $FreeBSD: src/sys/i386/i386/pmap.c,v 1.250.2.18 2002/03/06 22:48:53 silby Exp $
50 * Manages physical address maps.
52 * In addition to hardware address maps, this
53 * module is called upon to provide software-use-only
54 * maps which may or may not be stored in the same
55 * form as hardware maps. These pseudo-maps are
56 * used to store intermediate results from copy
57 * operations to and from address spaces.
59 * Since the information managed by this module is
60 * also stored by the logical address mapping module,
61 * this module may throw away valid virtual-to-physical
62 * mappings at almost any time. However, invalidations
63 * of virtual-to-physical mappings must be done as
64 * requested.
66 * In order to cope with hardware architectures which
67 * make virtual-to-physical map invalidates expensive,
68 * this module may delay invalidate or reduced protection
69 * operations until such time as they are actually
70 * necessary. This module is given full information as
71 * to which processors are currently using which maps,
72 * and to when physical maps must be made correct.
75 #if JG
76 #include "opt_disable_pse.h"
77 #include "opt_pmap.h"
78 #endif
79 #include "opt_msgbuf.h"
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/kernel.h>
84 #include <sys/proc.h>
85 #include <sys/msgbuf.h>
86 #include <sys/vmmeter.h>
87 #include <sys/mman.h>
89 #include <vm/vm.h>
90 #include <vm/vm_param.h>
91 #include <sys/sysctl.h>
92 #include <sys/lock.h>
93 #include <vm/vm_kern.h>
94 #include <vm/vm_page.h>
95 #include <vm/vm_map.h>
96 #include <vm/vm_object.h>
97 #include <vm/vm_extern.h>
98 #include <vm/vm_pageout.h>
99 #include <vm/vm_pager.h>
100 #include <vm/vm_zone.h>
102 #include <sys/user.h>
103 #include <sys/thread2.h>
104 #include <sys/sysref2.h>
106 #include <machine/cputypes.h>
107 #include <machine/md_var.h>
108 #include <machine/specialreg.h>
109 #include <machine/smp.h>
110 #include <machine_base/apic/apicreg.h>
111 #include <machine/globaldata.h>
112 #include <machine/pmap.h>
113 #include <machine/pmap_inval.h>
115 #include <ddb/ddb.h>
117 #define PMAP_KEEP_PDIRS
118 #ifndef PMAP_SHPGPERPROC
119 #define PMAP_SHPGPERPROC 200
120 #endif
122 #if defined(DIAGNOSTIC)
123 #define PMAP_DIAGNOSTIC
124 #endif
126 #define MINPV 2048
129 * Get PDEs and PTEs for user/kernel address space
131 static pd_entry_t *pmap_pde(pmap_t pmap, vm_offset_t va);
132 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
134 #define pmap_pde_v(pte) ((*(pd_entry_t *)pte & PG_V) != 0)
135 #define pmap_pte_w(pte) ((*(pt_entry_t *)pte & PG_W) != 0)
136 #define pmap_pte_m(pte) ((*(pt_entry_t *)pte & PG_M) != 0)
137 #define pmap_pte_u(pte) ((*(pt_entry_t *)pte & PG_A) != 0)
138 #define pmap_pte_v(pte) ((*(pt_entry_t *)pte & PG_V) != 0)
142 * Given a map and a machine independent protection code,
143 * convert to a vax protection code.
145 #define pte_prot(m, p) \
146 (protection_codes[p & (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)])
147 static int protection_codes[8];
149 struct pmap kernel_pmap;
150 static TAILQ_HEAD(,pmap) pmap_list = TAILQ_HEAD_INITIALIZER(pmap_list);
152 vm_paddr_t avail_start; /* PA of first available physical page */
153 vm_paddr_t avail_end; /* PA of last available physical page */
154 vm_offset_t virtual2_start; /* cutout free area prior to kernel start */
155 vm_offset_t virtual2_end;
156 vm_offset_t virtual_start; /* VA of first avail page (after kernel bss) */
157 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
158 vm_offset_t KvaStart; /* VA start of KVA space */
159 vm_offset_t KvaEnd; /* VA end of KVA space (non-inclusive) */
160 vm_offset_t KvaSize; /* max size of kernel virtual address space */
161 static boolean_t pmap_initialized = FALSE; /* Has pmap_init completed? */
162 static int pgeflag; /* PG_G or-in */
163 static int pseflag; /* PG_PS or-in */
165 static vm_object_t kptobj;
167 static int ndmpdp;
168 static vm_paddr_t dmaplimit;
169 static int nkpt;
170 vm_offset_t kernel_vm_end = VM_MIN_KERNEL_ADDRESS;
172 static uint64_t KPTbase;
173 static uint64_t KPTphys;
174 static uint64_t KPDphys; /* phys addr of kernel level 2 */
175 static uint64_t KPDbase; /* phys addr of kernel level 2 @ KERNBASE */
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;
201 * Crashdump maps.
203 static pt_entry_t *pt_crashdumpmap;
204 static caddr_t crashdumpmap;
206 extern pt_entry_t *SMPpt;
207 extern uint64_t SMPptpa;
209 #define DISABLE_PSE
211 static pv_entry_t get_pv_entry (void);
212 static void i386_protection_init (void);
213 static void create_pagetables(vm_paddr_t *firstaddr);
214 static void pmap_remove_all (vm_page_t m);
215 static int pmap_remove_pte (struct pmap *pmap, pt_entry_t *ptq,
216 vm_offset_t sva, pmap_inval_info_t info);
217 static void pmap_remove_page (struct pmap *pmap,
218 vm_offset_t va, pmap_inval_info_t info);
219 static int pmap_remove_entry (struct pmap *pmap, vm_page_t m,
220 vm_offset_t va, pmap_inval_info_t info);
221 static boolean_t pmap_testbit (vm_page_t m, int bit);
222 static void pmap_insert_entry (pmap_t pmap, vm_offset_t va,
223 vm_page_t mpte, vm_page_t m);
225 static vm_page_t pmap_allocpte (pmap_t pmap, vm_offset_t va);
227 static int pmap_release_free_page (pmap_t pmap, vm_page_t p);
228 static vm_page_t _pmap_allocpte (pmap_t pmap, vm_pindex_t ptepindex);
229 static pt_entry_t * pmap_pte_quick (pmap_t pmap, vm_offset_t va);
230 static vm_page_t pmap_page_lookup (vm_object_t object, vm_pindex_t pindex);
231 static int _pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m,
232 pmap_inval_info_t info);
233 static int pmap_unuse_pt (pmap_t, vm_offset_t, vm_page_t, pmap_inval_info_t);
234 static vm_offset_t pmap_kmem_choose(vm_offset_t addr);
236 static unsigned pdir4mb;
239 * Move the kernel virtual free pointer to the next
240 * 2MB. This is used to help improve performance
241 * by using a large (2MB) page for much of the kernel
242 * (.text, .data, .bss)
244 static
245 vm_offset_t
246 pmap_kmem_choose(vm_offset_t addr)
248 vm_offset_t newaddr = addr;
250 newaddr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
251 return newaddr;
255 * pmap_pte_quick:
257 * Super fast pmap_pte routine best used when scanning the pv lists.
258 * This eliminates many course-grained invltlb calls. Note that many of
259 * the pv list scans are across different pmaps and it is very wasteful
260 * to do an entire invltlb when checking a single mapping.
262 * Should only be called while in a critical section.
264 static __inline pt_entry_t *pmap_pte(pmap_t pmap, vm_offset_t va);
266 static
267 pt_entry_t *
268 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
270 return pmap_pte(pmap, va);
273 /* Return a non-clipped PD index for a given VA */
274 static __inline
275 vm_pindex_t
276 pmap_pde_pindex(vm_offset_t va)
278 return va >> PDRSHIFT;
281 /* Return various clipped indexes for a given VA */
282 static __inline
283 vm_pindex_t
284 pmap_pte_index(vm_offset_t va)
287 return ((va >> PAGE_SHIFT) & ((1ul << NPTEPGSHIFT) - 1));
290 static __inline
291 vm_pindex_t
292 pmap_pde_index(vm_offset_t va)
295 return ((va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1));
298 static __inline
299 vm_pindex_t
300 pmap_pdpe_index(vm_offset_t va)
303 return ((va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1));
306 static __inline
307 vm_pindex_t
308 pmap_pml4e_index(vm_offset_t va)
311 return ((va >> PML4SHIFT) & ((1ul << NPML4EPGSHIFT) - 1));
314 /* Return a pointer to the PML4 slot that corresponds to a VA */
315 static __inline
316 pml4_entry_t *
317 pmap_pml4e(pmap_t pmap, vm_offset_t va)
320 return (&pmap->pm_pml4[pmap_pml4e_index(va)]);
323 /* Return a pointer to the PDP slot that corresponds to a VA */
324 static __inline
325 pdp_entry_t *
326 pmap_pml4e_to_pdpe(pml4_entry_t *pml4e, vm_offset_t va)
328 pdp_entry_t *pdpe;
330 pdpe = (pdp_entry_t *)PHYS_TO_DMAP(*pml4e & PG_FRAME);
331 return (&pdpe[pmap_pdpe_index(va)]);
334 /* Return a pointer to the PDP slot that corresponds to a VA */
335 static __inline
336 pdp_entry_t *
337 pmap_pdpe(pmap_t pmap, vm_offset_t va)
339 pml4_entry_t *pml4e;
341 pml4e = pmap_pml4e(pmap, va);
342 if ((*pml4e & PG_V) == 0)
343 return NULL;
344 return (pmap_pml4e_to_pdpe(pml4e, va));
347 /* Return a pointer to the PD slot that corresponds to a VA */
348 static __inline
349 pd_entry_t *
350 pmap_pdpe_to_pde(pdp_entry_t *pdpe, vm_offset_t va)
352 pd_entry_t *pde;
354 pde = (pd_entry_t *)PHYS_TO_DMAP(*pdpe & PG_FRAME);
355 return (&pde[pmap_pde_index(va)]);
358 /* Return a pointer to the PD slot that corresponds to a VA */
359 static __inline
360 pd_entry_t *
361 pmap_pde(pmap_t pmap, vm_offset_t va)
363 pdp_entry_t *pdpe;
365 pdpe = pmap_pdpe(pmap, va);
366 if (pdpe == NULL || (*pdpe & PG_V) == 0)
367 return NULL;
368 return (pmap_pdpe_to_pde(pdpe, va));
371 /* Return a pointer to the PT slot that corresponds to a VA */
372 static __inline
373 pt_entry_t *
374 pmap_pde_to_pte(pd_entry_t *pde, vm_offset_t va)
376 pt_entry_t *pte;
378 pte = (pt_entry_t *)PHYS_TO_DMAP(*pde & PG_FRAME);
379 return (&pte[pmap_pte_index(va)]);
382 /* Return a pointer to the PT slot that corresponds to a VA */
383 static __inline
384 pt_entry_t *
385 pmap_pte(pmap_t pmap, vm_offset_t va)
387 pd_entry_t *pde;
389 pde = pmap_pde(pmap, va);
390 if (pde == NULL || (*pde & PG_V) == 0)
391 return NULL;
392 if ((*pde & PG_PS) != 0) /* compat with i386 pmap_pte() */
393 return ((pt_entry_t *)pde);
394 return (pmap_pde_to_pte(pde, va));
397 static __inline
398 pt_entry_t *
399 vtopte(vm_offset_t va)
401 uint64_t mask = ((1ul << (NPTEPGSHIFT + NPDEPGSHIFT + NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1);
403 return (PTmap + ((va >> PAGE_SHIFT) & mask));
406 static __inline
407 pd_entry_t *
408 vtopde(vm_offset_t va)
410 uint64_t mask = ((1ul << (NPDEPGSHIFT + NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1);
412 return (PDmap + ((va >> PDRSHIFT) & mask));
415 static uint64_t
416 allocpages(vm_paddr_t *firstaddr, int n)
418 uint64_t ret;
420 ret = *firstaddr;
421 bzero((void *)ret, n * PAGE_SIZE);
422 *firstaddr += n * PAGE_SIZE;
423 return (ret);
426 static
427 void
428 create_pagetables(vm_paddr_t *firstaddr)
430 int i;
433 * We are running (mostly) V=P at this point
435 * Calculate NKPT - number of kernel page tables. We have to
436 * accomodoate prealloction of the vm_page_array, dump bitmap,
437 * MSGBUF_SIZE, and other stuff. Be generous.
439 * Maxmem is in pages.
441 ndmpdp = (ptoa(Maxmem) + NBPDP - 1) >> PDPSHIFT;
442 if (ndmpdp < 4) /* Minimum 4GB of dirmap */
443 ndmpdp = 4;
445 nkpt = (Maxmem * sizeof(struct vm_page) + NBPDR - 1) / NBPDR;
446 nkpt += ((nkpt + nkpt + 1 + NKPML4E + NKPDPE + NDMPML4E + ndmpdp) +
447 511) / 512;
448 nkpt += 128;
451 * Allocate pages
453 KPTbase = allocpages(firstaddr, nkpt);
454 KPTphys = allocpages(firstaddr, nkpt);
455 KPML4phys = allocpages(firstaddr, 1);
456 KPDPphys = allocpages(firstaddr, NKPML4E);
459 * Calculate the page directory base for KERNBASE,
460 * that is where we start populating the page table pages.
461 * Basically this is the end - 2.
463 KPDphys = allocpages(firstaddr, NKPDPE);
464 KPDbase = KPDphys + ((NKPDPE - (NPDPEPG - KPDPI)) << PAGE_SHIFT);
466 DMPDPphys = allocpages(firstaddr, NDMPML4E);
467 if ((amd_feature & AMDID_PAGE1GB) == 0)
468 DMPDphys = allocpages(firstaddr, ndmpdp);
469 dmaplimit = (vm_paddr_t)ndmpdp << PDPSHIFT;
472 * Fill in the underlying page table pages for the area around
473 * KERNBASE. This remaps low physical memory to KERNBASE.
475 * Read-only from zero to physfree
476 * XXX not fully used, underneath 2M pages
478 for (i = 0; (i << PAGE_SHIFT) < *firstaddr; i++) {
479 ((pt_entry_t *)KPTbase)[i] = i << PAGE_SHIFT;
480 ((pt_entry_t *)KPTbase)[i] |= PG_RW | PG_V | PG_G;
484 * Now map the initial kernel page tables. One block of page
485 * tables is placed at the beginning of kernel virtual memory,
486 * and another block is placed at KERNBASE to map the kernel binary,
487 * data, bss, and initial pre-allocations.
489 for (i = 0; i < nkpt; i++) {
490 ((pd_entry_t *)KPDbase)[i] = KPTbase + (i << PAGE_SHIFT);
491 ((pd_entry_t *)KPDbase)[i] |= PG_RW | PG_V;
493 for (i = 0; i < nkpt; i++) {
494 ((pd_entry_t *)KPDphys)[i] = KPTphys + (i << PAGE_SHIFT);
495 ((pd_entry_t *)KPDphys)[i] |= PG_RW | PG_V;
499 * Map from zero to end of allocations using 2M pages as an
500 * optimization. This will bypass some of the KPTBase pages
501 * above in the KERNBASE area.
503 for (i = 0; (i << PDRSHIFT) < *firstaddr; i++) {
504 ((pd_entry_t *)KPDbase)[i] = i << PDRSHIFT;
505 ((pd_entry_t *)KPDbase)[i] |= PG_RW | PG_V | PG_PS | PG_G;
509 * And connect up the PD to the PDP. The kernel pmap is expected
510 * to pre-populate all of its PDs. See NKPDPE in vmparam.h.
512 for (i = 0; i < NKPDPE; i++) {
513 ((pdp_entry_t *)KPDPphys)[NPDPEPG - NKPDPE + i] =
514 KPDphys + (i << PAGE_SHIFT);
515 ((pdp_entry_t *)KPDPphys)[NPDPEPG - NKPDPE + i] |=
516 PG_RW | PG_V | PG_U;
519 /* Now set up the direct map space using either 2MB or 1GB pages */
520 /* Preset PG_M and PG_A because demotion expects it */
521 if ((amd_feature & AMDID_PAGE1GB) == 0) {
522 for (i = 0; i < NPDEPG * ndmpdp; i++) {
523 ((pd_entry_t *)DMPDphys)[i] = (vm_paddr_t)i << PDRSHIFT;
524 ((pd_entry_t *)DMPDphys)[i] |= PG_RW | PG_V | PG_PS |
525 PG_G | PG_M | PG_A;
527 /* And the direct map space's PDP */
528 for (i = 0; i < ndmpdp; i++) {
529 ((pdp_entry_t *)DMPDPphys)[i] = DMPDphys +
530 (i << PAGE_SHIFT);
531 ((pdp_entry_t *)DMPDPphys)[i] |= PG_RW | PG_V | PG_U;
533 } else {
534 for (i = 0; i < ndmpdp; i++) {
535 ((pdp_entry_t *)DMPDPphys)[i] =
536 (vm_paddr_t)i << PDPSHIFT;
537 ((pdp_entry_t *)DMPDPphys)[i] |= PG_RW | PG_V | PG_PS |
538 PG_G | PG_M | PG_A;
542 /* And recursively map PML4 to itself in order to get PTmap */
543 ((pdp_entry_t *)KPML4phys)[PML4PML4I] = KPML4phys;
544 ((pdp_entry_t *)KPML4phys)[PML4PML4I] |= PG_RW | PG_V | PG_U;
546 /* Connect the Direct Map slot up to the PML4 */
547 ((pdp_entry_t *)KPML4phys)[DMPML4I] = DMPDPphys;
548 ((pdp_entry_t *)KPML4phys)[DMPML4I] |= PG_RW | PG_V | PG_U;
550 /* Connect the KVA slot up to the PML4 */
551 ((pdp_entry_t *)KPML4phys)[KPML4I] = KPDPphys;
552 ((pdp_entry_t *)KPML4phys)[KPML4I] |= PG_RW | PG_V | PG_U;
556 * Bootstrap the system enough to run with virtual memory.
558 * On the i386 this is called after mapping has already been enabled
559 * and just syncs the pmap module with what has already been done.
560 * [We can't call it easily with mapping off since the kernel is not
561 * mapped with PA == VA, hence we would have to relocate every address
562 * from the linked base (virtual) address "KERNBASE" to the actual
563 * (physical) address starting relative to 0]
565 void
566 pmap_bootstrap(vm_paddr_t *firstaddr)
568 vm_offset_t va;
569 pt_entry_t *pte;
570 struct mdglobaldata *gd;
571 int pg;
573 KvaStart = VM_MIN_KERNEL_ADDRESS;
574 KvaEnd = VM_MAX_KERNEL_ADDRESS;
575 KvaSize = KvaEnd - KvaStart;
577 avail_start = *firstaddr;
580 * Create an initial set of page tables to run the kernel in.
582 create_pagetables(firstaddr);
584 virtual2_start = KvaStart;
585 virtual2_end = PTOV_OFFSET;
587 virtual_start = (vm_offset_t) PTOV_OFFSET + *firstaddr;
588 virtual_start = pmap_kmem_choose(virtual_start);
590 virtual_end = VM_MAX_KERNEL_ADDRESS;
592 /* XXX do %cr0 as well */
593 load_cr4(rcr4() | CR4_PGE | CR4_PSE);
594 load_cr3(KPML4phys);
597 * Initialize protection array.
599 i386_protection_init();
602 * The kernel's pmap is statically allocated so we don't have to use
603 * pmap_create, which is unlikely to work correctly at this part of
604 * the boot sequence (XXX and which no longer exists).
606 kernel_pmap.pm_pml4 = (pdp_entry_t *) (PTOV_OFFSET + KPML4phys);
607 kernel_pmap.pm_count = 1;
608 kernel_pmap.pm_active = (cpumask_t)-1 & ~CPUMASK_LOCK;
609 TAILQ_INIT(&kernel_pmap.pm_pvlist);
612 * Reserve some special page table entries/VA space for temporary
613 * mapping of pages.
615 #define SYSMAP(c, p, v, n) \
616 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
618 va = virtual_start;
619 pte = vtopte(va);
622 * CMAP1/CMAP2 are used for zeroing and copying pages.
624 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
627 * Crashdump maps.
629 SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS);
632 * ptvmmap is used for reading arbitrary physical pages via
633 * /dev/mem.
635 SYSMAP(caddr_t, ptmmap, ptvmmap, 1)
638 * msgbufp is used to map the system message buffer.
639 * XXX msgbufmap is not used.
641 SYSMAP(struct msgbuf *, msgbufmap, msgbufp,
642 atop(round_page(MSGBUF_SIZE)))
644 virtual_start = va;
646 *CMAP1 = 0;
649 * PG_G is terribly broken on SMP because we IPI invltlb's in some
650 * cases rather then invl1pg. Actually, I don't even know why it
651 * works under UP because self-referential page table mappings
653 #ifdef SMP
654 pgeflag = 0;
655 #else
656 if (cpu_feature & CPUID_PGE)
657 pgeflag = PG_G;
658 #endif
661 * Initialize the 4MB page size flag
663 pseflag = 0;
665 * The 4MB page version of the initial
666 * kernel page mapping.
668 pdir4mb = 0;
670 #if !defined(DISABLE_PSE)
671 if (cpu_feature & CPUID_PSE) {
672 pt_entry_t ptditmp;
674 * Note that we have enabled PSE mode
676 pseflag = PG_PS;
677 ptditmp = *(PTmap + x86_64_btop(KERNBASE));
678 ptditmp &= ~(NBPDR - 1);
679 ptditmp |= PG_V | PG_RW | PG_PS | PG_U | pgeflag;
680 pdir4mb = ptditmp;
682 #ifndef SMP
684 * Enable the PSE mode. If we are SMP we can't do this
685 * now because the APs will not be able to use it when
686 * they boot up.
688 load_cr4(rcr4() | CR4_PSE);
691 * We can do the mapping here for the single processor
692 * case. We simply ignore the old page table page from
693 * now on.
696 * For SMP, we still need 4K pages to bootstrap APs,
697 * PSE will be enabled as soon as all APs are up.
699 PTD[KPTDI] = (pd_entry_t)ptditmp;
700 cpu_invltlb();
701 #endif
703 #endif
706 * We need to finish setting up the globaldata page for the BSP.
707 * locore has already populated the page table for the mdglobaldata
708 * portion.
710 pg = MDGLOBALDATA_BASEALLOC_PAGES;
711 gd = &CPU_prvspace[0].mdglobaldata;
712 gd->gd_CMAP1 = &SMPpt[pg + 0];
713 gd->gd_CMAP2 = &SMPpt[pg + 1];
714 gd->gd_CMAP3 = &SMPpt[pg + 2];
715 gd->gd_PMAP1 = &SMPpt[pg + 3];
716 gd->gd_CADDR1 = CPU_prvspace[0].CPAGE1;
717 gd->gd_CADDR2 = CPU_prvspace[0].CPAGE2;
718 gd->gd_CADDR3 = CPU_prvspace[0].CPAGE3;
719 gd->gd_PADDR1 = (pt_entry_t *)CPU_prvspace[0].PPAGE1;
721 cpu_invltlb();
724 #ifdef SMP
726 * Set 4mb pdir for mp startup
728 void
729 pmap_set_opt(void)
731 if (pseflag && (cpu_feature & CPUID_PSE)) {
732 load_cr4(rcr4() | CR4_PSE);
733 if (pdir4mb && mycpu->gd_cpuid == 0) { /* only on BSP */
734 cpu_invltlb();
738 #endif
741 * Initialize the pmap module.
742 * Called by vm_init, to initialize any structures that the pmap
743 * system needs to map virtual memory.
744 * pmap_init has been enhanced to support in a fairly consistant
745 * way, discontiguous physical memory.
747 void
748 pmap_init(void)
750 int i;
751 int initial_pvs;
754 * object for kernel page table pages
756 /* JG I think the number can be arbitrary */
757 kptobj = vm_object_allocate(OBJT_DEFAULT, 5);
760 * Allocate memory for random pmap data structures. Includes the
761 * pv_head_table.
764 for(i = 0; i < vm_page_array_size; i++) {
765 vm_page_t m;
767 m = &vm_page_array[i];
768 TAILQ_INIT(&m->md.pv_list);
769 m->md.pv_list_count = 0;
773 * init the pv free list
775 initial_pvs = vm_page_array_size;
776 if (initial_pvs < MINPV)
777 initial_pvs = MINPV;
778 pvzone = &pvzone_store;
779 pvinit = (void *)kmem_alloc(&kernel_map,
780 initial_pvs * sizeof (struct pv_entry));
781 zbootinit(pvzone, "PV ENTRY", sizeof (struct pv_entry),
782 pvinit, initial_pvs);
785 * Now it is safe to enable pv_table recording.
787 pmap_initialized = TRUE;
788 #ifdef SMP
789 lapic = pmap_mapdev_uncacheable(cpu_apic_address, sizeof(struct LAPIC));
790 #endif
794 * Initialize the address space (zone) for the pv_entries. Set a
795 * high water mark so that the system can recover from excessive
796 * numbers of pv entries.
798 void
799 pmap_init2(void)
801 int shpgperproc = PMAP_SHPGPERPROC;
802 int entry_max;
804 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
805 pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
806 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
807 pv_entry_high_water = 9 * (pv_entry_max / 10);
810 * Subtract out pages already installed in the zone (hack)
812 entry_max = pv_entry_max - vm_page_array_size;
813 if (entry_max <= 0)
814 entry_max = 1;
816 zinitna(pvzone, &pvzone_obj, NULL, 0, entry_max, ZONE_INTERRUPT, 1);
820 /***************************************************
821 * Low level helper routines.....
822 ***************************************************/
824 #if defined(PMAP_DIAGNOSTIC)
827 * This code checks for non-writeable/modified pages.
828 * This should be an invalid condition.
830 static
832 pmap_nw_modified(pt_entry_t pte)
834 if ((pte & (PG_M|PG_RW)) == PG_M)
835 return 1;
836 else
837 return 0;
839 #endif
843 * this routine defines the region(s) of memory that should
844 * not be tested for the modified bit.
846 static __inline
848 pmap_track_modified(vm_offset_t va)
850 if ((va < clean_sva) || (va >= clean_eva))
851 return 1;
852 else
853 return 0;
857 * Extract the physical page address associated with the map/VA pair.
859 * The caller must hold vm_token if non-blocking operation is desired.
861 vm_paddr_t
862 pmap_extract(pmap_t pmap, vm_offset_t va)
864 vm_paddr_t rtval;
865 pt_entry_t *pte;
866 pd_entry_t pde, *pdep;
868 lwkt_gettoken(&vm_token);
869 rtval = 0;
870 pdep = pmap_pde(pmap, va);
871 if (pdep != NULL) {
872 pde = *pdep;
873 if (pde) {
874 if ((pde & PG_PS) != 0) {
875 rtval = (pde & PG_PS_FRAME) | (va & PDRMASK);
876 } else {
877 pte = pmap_pde_to_pte(pdep, va);
878 rtval = (*pte & PG_FRAME) | (va & PAGE_MASK);
882 lwkt_reltoken(&vm_token);
883 return rtval;
887 * Extract the physical page address associated kernel virtual address.
889 vm_paddr_t
890 pmap_kextract(vm_offset_t va)
892 pd_entry_t pde;
893 vm_paddr_t pa;
895 if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS) {
896 pa = DMAP_TO_PHYS(va);
897 } else {
898 pde = *vtopde(va);
899 if (pde & PG_PS) {
900 pa = (pde & PG_PS_FRAME) | (va & PDRMASK);
901 } else {
903 * Beware of a concurrent promotion that changes the
904 * PDE at this point! For example, vtopte() must not
905 * be used to access the PTE because it would use the
906 * new PDE. It is, however, safe to use the old PDE
907 * because the page table page is preserved by the
908 * promotion.
910 pa = *pmap_pde_to_pte(&pde, va);
911 pa = (pa & PG_FRAME) | (va & PAGE_MASK);
914 return pa;
917 /***************************************************
918 * Low level mapping routines.....
919 ***************************************************/
922 * Routine: pmap_kenter
923 * Function:
924 * Add a wired page to the KVA
925 * NOTE! note that in order for the mapping to take effect -- you
926 * should do an invltlb after doing the pmap_kenter().
928 void
929 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
931 pt_entry_t *pte;
932 pt_entry_t npte;
933 pmap_inval_info info;
935 pmap_inval_init(&info);
936 npte = pa | PG_RW | PG_V | pgeflag;
937 pte = vtopte(va);
938 pmap_inval_interlock(&info, &kernel_pmap, va);
939 *pte = npte;
940 pmap_inval_deinterlock(&info, &kernel_pmap);
941 pmap_inval_done(&info);
945 * Routine: pmap_kenter_quick
946 * Function:
947 * Similar to pmap_kenter(), except we only invalidate the
948 * mapping on the current CPU.
950 void
951 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
953 pt_entry_t *pte;
954 pt_entry_t npte;
956 npte = pa | PG_RW | PG_V | pgeflag;
957 pte = vtopte(va);
958 *pte = npte;
959 cpu_invlpg((void *)va);
962 void
963 pmap_kenter_sync(vm_offset_t va)
965 pmap_inval_info info;
967 pmap_inval_init(&info);
968 pmap_inval_interlock(&info, &kernel_pmap, va);
969 pmap_inval_deinterlock(&info, &kernel_pmap);
970 pmap_inval_done(&info);
973 void
974 pmap_kenter_sync_quick(vm_offset_t va)
976 cpu_invlpg((void *)va);
980 * remove a page from the kernel pagetables
982 void
983 pmap_kremove(vm_offset_t va)
985 pt_entry_t *pte;
986 pmap_inval_info info;
988 pmap_inval_init(&info);
989 pte = vtopte(va);
990 pmap_inval_interlock(&info, &kernel_pmap, va);
991 *pte = 0;
992 pmap_inval_deinterlock(&info, &kernel_pmap);
993 pmap_inval_done(&info);
996 void
997 pmap_kremove_quick(vm_offset_t va)
999 pt_entry_t *pte;
1000 pte = vtopte(va);
1001 *pte = 0;
1002 cpu_invlpg((void *)va);
1006 * XXX these need to be recoded. They are not used in any critical path.
1008 void
1009 pmap_kmodify_rw(vm_offset_t va)
1011 *vtopte(va) |= PG_RW;
1012 cpu_invlpg((void *)va);
1015 void
1016 pmap_kmodify_nc(vm_offset_t va)
1018 *vtopte(va) |= PG_N;
1019 cpu_invlpg((void *)va);
1023 * Used to map a range of physical addresses into kernel virtual
1024 * address space during the low level boot, typically to map the
1025 * dump bitmap, message buffer, and vm_page_array.
1027 * These mappings are typically made at some pointer after the end of the
1028 * kernel text+data.
1030 * We could return PHYS_TO_DMAP(start) here and not allocate any
1031 * via (*virtp), but then kmem from userland and kernel dumps won't
1032 * have access to the related pointers.
1034 vm_offset_t
1035 pmap_map(vm_offset_t *virtp, vm_paddr_t start, vm_paddr_t end, int prot)
1037 vm_offset_t va;
1038 vm_offset_t va_start;
1040 /*return PHYS_TO_DMAP(start);*/
1042 va_start = *virtp;
1043 va = va_start;
1045 while (start < end) {
1046 pmap_kenter_quick(va, start);
1047 va += PAGE_SIZE;
1048 start += PAGE_SIZE;
1050 *virtp = va;
1051 return va_start;
1056 * Add a list of wired pages to the kva
1057 * this routine is only used for temporary
1058 * kernel mappings that do not need to have
1059 * page modification or references recorded.
1060 * Note that old mappings are simply written
1061 * over. The page *must* be wired.
1063 void
1064 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
1066 vm_offset_t end_va;
1068 end_va = va + count * PAGE_SIZE;
1070 while (va < end_va) {
1071 pt_entry_t *pte;
1073 pte = vtopte(va);
1074 *pte = VM_PAGE_TO_PHYS(*m) | PG_RW | PG_V | pgeflag;
1075 cpu_invlpg((void *)va);
1076 va += PAGE_SIZE;
1077 m++;
1079 #ifdef SMP
1080 smp_invltlb(); /* XXX */
1081 #endif
1085 * This routine jerks page mappings from the
1086 * kernel -- it is meant only for temporary mappings.
1088 * MPSAFE, INTERRUPT SAFE (cluster callback)
1090 void
1091 pmap_qremove(vm_offset_t va, int count)
1093 vm_offset_t end_va;
1095 end_va = va + count * PAGE_SIZE;
1097 while (va < end_va) {
1098 pt_entry_t *pte;
1100 pte = vtopte(va);
1101 *pte = 0;
1102 cpu_invlpg((void *)va);
1103 va += PAGE_SIZE;
1105 #ifdef SMP
1106 smp_invltlb();
1107 #endif
1111 * This routine works like vm_page_lookup() but also blocks as long as the
1112 * page is busy. This routine does not busy the page it returns.
1114 * Unless the caller is managing objects whos pages are in a known state,
1115 * the call should be made with a critical section held so the page's object
1116 * association remains valid on return.
1118 static
1119 vm_page_t
1120 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
1122 vm_page_t m;
1124 do {
1125 m = vm_page_lookup(object, pindex);
1126 } while (m && vm_page_sleep_busy(m, FALSE, "pplookp"));
1128 return(m);
1132 * Create a new thread and optionally associate it with a (new) process.
1133 * NOTE! the new thread's cpu may not equal the current cpu.
1135 void
1136 pmap_init_thread(thread_t td)
1138 /* enforce pcb placement */
1139 td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
1140 td->td_savefpu = &td->td_pcb->pcb_save;
1141 td->td_sp = (char *)td->td_pcb - 16; /* JG is -16 needed on x86_64? */
1145 * This routine directly affects the fork perf for a process.
1147 void
1148 pmap_init_proc(struct proc *p)
1153 * Dispose the UPAGES for a process that has exited.
1154 * This routine directly impacts the exit perf of a process.
1156 void
1157 pmap_dispose_proc(struct proc *p)
1159 KASSERT(p->p_lock == 0, ("attempt to dispose referenced proc! %p", p));
1162 /***************************************************
1163 * Page table page management routines.....
1164 ***************************************************/
1167 * This routine unholds page table pages, and if the hold count
1168 * drops to zero, then it decrements the wire count.
1170 static __inline
1172 pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m,
1173 pmap_inval_info_t info)
1175 KKASSERT(m->hold_count > 0);
1176 if (m->hold_count > 1) {
1177 vm_page_unhold(m);
1178 return 0;
1179 } else {
1180 return _pmap_unwire_pte_hold(pmap, va, m, info);
1184 static
1186 _pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m,
1187 pmap_inval_info_t info)
1190 * Wait until we can busy the page ourselves. We cannot have
1191 * any active flushes if we block. We own one hold count on the
1192 * page so it cannot be freed out from under us.
1194 if (m->flags & PG_BUSY) {
1195 pmap_inval_flush(info);
1196 while (vm_page_sleep_busy(m, FALSE, "pmuwpt"))
1199 KASSERT(m->queue == PQ_NONE,
1200 ("_pmap_unwire_pte_hold: %p->queue != PQ_NONE", m));
1203 * This case can occur if new references were acquired while
1204 * we were blocked.
1206 if (m->hold_count > 1) {
1207 KKASSERT(m->hold_count > 1);
1208 vm_page_unhold(m);
1209 return 0;
1213 * Unmap the page table page
1215 KKASSERT(m->hold_count == 1);
1216 vm_page_busy(m);
1217 pmap_inval_interlock(info, pmap, -1);
1219 if (m->pindex >= (NUPDE + NUPDPE)) {
1220 /* PDP page */
1221 pml4_entry_t *pml4;
1222 pml4 = pmap_pml4e(pmap, va);
1223 *pml4 = 0;
1224 } else if (m->pindex >= NUPDE) {
1225 /* PD page */
1226 pdp_entry_t *pdp;
1227 pdp = pmap_pdpe(pmap, va);
1228 *pdp = 0;
1229 } else {
1230 /* PT page */
1231 pd_entry_t *pd;
1232 pd = pmap_pde(pmap, va);
1233 *pd = 0;
1236 KKASSERT(pmap->pm_stats.resident_count > 0);
1237 --pmap->pm_stats.resident_count;
1239 if (pmap->pm_ptphint == m)
1240 pmap->pm_ptphint = NULL;
1241 pmap_inval_deinterlock(info, pmap);
1243 if (m->pindex < NUPDE) {
1244 /* We just released a PT, unhold the matching PD */
1245 vm_page_t pdpg;
1247 pdpg = PHYS_TO_VM_PAGE(*pmap_pdpe(pmap, va) & PG_FRAME);
1248 pmap_unwire_pte_hold(pmap, va, pdpg, info);
1250 if (m->pindex >= NUPDE && m->pindex < (NUPDE + NUPDPE)) {
1251 /* We just released a PD, unhold the matching PDP */
1252 vm_page_t pdppg;
1254 pdppg = PHYS_TO_VM_PAGE(*pmap_pml4e(pmap, va) & PG_FRAME);
1255 pmap_unwire_pte_hold(pmap, va, pdppg, info);
1259 * This was our last hold, the page had better be unwired
1260 * after we decrement wire_count.
1262 * FUTURE NOTE: shared page directory page could result in
1263 * multiple wire counts.
1265 vm_page_unhold(m);
1266 --m->wire_count;
1267 KKASSERT(m->wire_count == 0);
1268 --vmstats.v_wire_count;
1269 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1270 vm_page_flash(m);
1271 vm_page_free_zero(m);
1273 return 1;
1277 * After removing a page table entry, this routine is used to
1278 * conditionally free the page, and manage the hold/wire counts.
1280 static
1282 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte,
1283 pmap_inval_info_t info)
1285 vm_pindex_t ptepindex;
1287 if (va >= VM_MAX_USER_ADDRESS)
1288 return 0;
1290 if (mpte == NULL) {
1291 ptepindex = pmap_pde_pindex(va);
1292 #if JGHINT
1293 if (pmap->pm_ptphint &&
1294 (pmap->pm_ptphint->pindex == ptepindex)) {
1295 mpte = pmap->pm_ptphint;
1296 } else {
1297 #endif
1298 pmap_inval_flush(info);
1299 mpte = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1300 pmap->pm_ptphint = mpte;
1301 #if JGHINT
1303 #endif
1305 return pmap_unwire_pte_hold(pmap, va, mpte, info);
1309 * Initialize pmap0/vmspace0. This pmap is not added to pmap_list because
1310 * it, and IdlePTD, represents the template used to update all other pmaps.
1312 * On architectures where the kernel pmap is not integrated into the user
1313 * process pmap, this pmap represents the process pmap, not the kernel pmap.
1314 * kernel_pmap should be used to directly access the kernel_pmap.
1316 void
1317 pmap_pinit0(struct pmap *pmap)
1319 pmap->pm_pml4 = (pml4_entry_t *)(PTOV_OFFSET + KPML4phys);
1320 pmap->pm_count = 1;
1321 pmap->pm_active = 0;
1322 pmap->pm_ptphint = NULL;
1323 TAILQ_INIT(&pmap->pm_pvlist);
1324 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1328 * Initialize a preallocated and zeroed pmap structure,
1329 * such as one in a vmspace structure.
1331 void
1332 pmap_pinit(struct pmap *pmap)
1334 vm_page_t ptdpg;
1337 * No need to allocate page table space yet but we do need a valid
1338 * page directory table.
1340 if (pmap->pm_pml4 == NULL) {
1341 pmap->pm_pml4 =
1342 (pml4_entry_t *)kmem_alloc_pageable(&kernel_map, PAGE_SIZE);
1346 * Allocate an object for the ptes
1348 if (pmap->pm_pteobj == NULL)
1349 pmap->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, NUPDE + NUPDPE + PML4PML4I + 1);
1352 * Allocate the page directory page, unless we already have
1353 * one cached. If we used the cached page the wire_count will
1354 * already be set appropriately.
1356 if ((ptdpg = pmap->pm_pdirm) == NULL) {
1357 ptdpg = vm_page_grab(pmap->pm_pteobj, NUPDE + NUPDPE + PML4PML4I,
1358 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1359 pmap->pm_pdirm = ptdpg;
1360 vm_page_flag_clear(ptdpg, PG_MAPPED | PG_BUSY);
1361 ptdpg->valid = VM_PAGE_BITS_ALL;
1362 if (ptdpg->wire_count == 0)
1363 ++vmstats.v_wire_count;
1364 ptdpg->wire_count = 1;
1365 pmap_kenter((vm_offset_t)pmap->pm_pml4, VM_PAGE_TO_PHYS(ptdpg));
1367 if ((ptdpg->flags & PG_ZERO) == 0)
1368 bzero(pmap->pm_pml4, PAGE_SIZE);
1370 pmap->pm_pml4[KPML4I] = KPDPphys | PG_RW | PG_V | PG_U;
1371 pmap->pm_pml4[DMPML4I] = DMPDPphys | PG_RW | PG_V | PG_U;
1373 /* install self-referential address mapping entry */
1374 pmap->pm_pml4[PML4PML4I] = VM_PAGE_TO_PHYS(ptdpg) | PG_V | PG_RW | PG_A | PG_M;
1376 pmap->pm_count = 1;
1377 pmap->pm_active = 0;
1378 pmap->pm_ptphint = NULL;
1379 TAILQ_INIT(&pmap->pm_pvlist);
1380 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1381 pmap->pm_stats.resident_count = 1;
1385 * Clean up a pmap structure so it can be physically freed. This routine
1386 * is called by the vmspace dtor function. A great deal of pmap data is
1387 * left passively mapped to improve vmspace management so we have a bit
1388 * of cleanup work to do here.
1390 void
1391 pmap_puninit(pmap_t pmap)
1393 vm_page_t p;
1395 KKASSERT(pmap->pm_active == 0);
1396 lwkt_gettoken(&vm_token);
1397 if ((p = pmap->pm_pdirm) != NULL) {
1398 KKASSERT(pmap->pm_pml4 != NULL);
1399 KKASSERT(pmap->pm_pml4 != (void *)(PTOV_OFFSET + KPML4phys));
1400 pmap_kremove((vm_offset_t)pmap->pm_pml4);
1401 p->wire_count--;
1402 vmstats.v_wire_count--;
1403 KKASSERT((p->flags & PG_BUSY) == 0);
1404 vm_page_busy(p);
1405 vm_page_free_zero(p);
1406 pmap->pm_pdirm = NULL;
1408 if (pmap->pm_pml4) {
1409 KKASSERT(pmap->pm_pml4 != (void *)(PTOV_OFFSET + KPML4phys));
1410 kmem_free(&kernel_map, (vm_offset_t)pmap->pm_pml4, PAGE_SIZE);
1411 pmap->pm_pml4 = NULL;
1413 if (pmap->pm_pteobj) {
1414 vm_object_deallocate(pmap->pm_pteobj);
1415 pmap->pm_pteobj = NULL;
1417 lwkt_reltoken(&vm_token);
1421 * Wire in kernel global address entries. To avoid a race condition
1422 * between pmap initialization and pmap_growkernel, this procedure
1423 * adds the pmap to the master list (which growkernel scans to update),
1424 * then copies the template.
1426 void
1427 pmap_pinit2(struct pmap *pmap)
1429 crit_enter();
1430 lwkt_gettoken(&vm_token);
1431 TAILQ_INSERT_TAIL(&pmap_list, pmap, pm_pmnode);
1432 /* XXX copies current process, does not fill in MPPTDI */
1433 lwkt_reltoken(&vm_token);
1434 crit_exit();
1438 * Attempt to release and free a vm_page in a pmap. Returns 1 on success,
1439 * 0 on failure (if the procedure had to sleep).
1441 * When asked to remove the page directory page itself, we actually just
1442 * leave it cached so we do not have to incur the SMP inval overhead of
1443 * removing the kernel mapping. pmap_puninit() will take care of it.
1445 static
1447 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1450 * This code optimizes the case of freeing non-busy
1451 * page-table pages. Those pages are zero now, and
1452 * might as well be placed directly into the zero queue.
1454 if (vm_page_sleep_busy(p, FALSE, "pmaprl"))
1455 return 0;
1457 vm_page_busy(p);
1460 * Remove the page table page from the processes address space.
1462 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1464 * We are the pml4 table itself.
1466 /* XXX anything to do here? */
1467 } else if (p->pindex >= (NUPDE + NUPDPE)) {
1469 * Remove a PDP page from the PML4. We do not maintain
1470 * hold counts on the PML4 page.
1472 pml4_entry_t *pml4;
1473 vm_page_t m4;
1474 int idx;
1476 m4 = vm_page_lookup(pmap->pm_pteobj, NUPDE + NUPDPE + PML4PML4I);
1477 KKASSERT(m4 != NULL);
1478 pml4 = (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m4));
1479 idx = (p->pindex - (NUPDE + NUPDPE)) % NPML4EPG;
1480 KKASSERT(pml4[idx] != 0);
1481 pml4[idx] = 0;
1482 } else if (p->pindex >= NUPDE) {
1484 * Remove a PD page from the PDP and drop the hold count
1485 * on the PDP. The PDP is left cached in the pmap if
1486 * the hold count drops to 0 so the wire count remains
1487 * intact.
1489 vm_page_t m3;
1490 pdp_entry_t *pdp;
1491 int idx;
1493 m3 = vm_page_lookup(pmap->pm_pteobj,
1494 NUPDE + NUPDPE + (p->pindex - NUPDE) / NPDPEPG);
1495 KKASSERT(m3 != NULL);
1496 pdp = (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m3));
1497 idx = (p->pindex - NUPDE) % NPDPEPG;
1498 KKASSERT(pdp[idx] != 0);
1499 pdp[idx] = 0;
1500 m3->hold_count--;
1501 } else {
1503 * Remove a PT page from the PD and drop the hold count
1504 * on the PD. The PD is left cached in the pmap if
1505 * the hold count drops to 0 so the wire count remains
1506 * intact.
1508 vm_page_t m2;
1509 pd_entry_t *pd;
1510 int idx;
1512 m2 = vm_page_lookup(pmap->pm_pteobj,
1513 NUPDE + p->pindex / NPDEPG);
1514 KKASSERT(m2 != NULL);
1515 pd = (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m2));
1516 idx = p->pindex % NPDEPG;
1517 pd[idx] = 0;
1518 m2->hold_count--;
1522 * One fewer mappings in the pmap. p's hold count had better
1523 * be zero.
1525 KKASSERT(pmap->pm_stats.resident_count > 0);
1526 --pmap->pm_stats.resident_count;
1527 if (p->hold_count)
1528 panic("pmap_release: freeing held page table page");
1529 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1530 pmap->pm_ptphint = NULL;
1533 * We leave the top-level page table page cached, wired, and mapped in
1534 * the pmap until the dtor function (pmap_puninit()) gets called.
1535 * However, still clean it up so we can set PG_ZERO.
1537 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1538 bzero(pmap->pm_pml4, PAGE_SIZE);
1539 vm_page_flag_set(p, PG_ZERO);
1540 vm_page_wakeup(p);
1541 } else {
1542 p->wire_count--;
1543 KKASSERT(p->wire_count == 0);
1544 vmstats.v_wire_count--;
1545 /* JG eventually revert to using vm_page_free_zero() */
1546 vm_page_free(p);
1548 return 1;
1552 * This routine is called when various levels in the page table need to
1553 * be populated. This routine cannot fail.
1555 static
1556 vm_page_t
1557 _pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex)
1559 vm_page_t m;
1562 * Find or fabricate a new pagetable page. This will busy the page.
1564 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1565 VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1566 if ((m->flags & PG_ZERO) == 0) {
1567 pmap_zero_page(VM_PAGE_TO_PHYS(m));
1570 KASSERT(m->queue == PQ_NONE,
1571 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1574 * Increment the hold count for the page we will be returning to
1575 * the caller.
1577 m->hold_count++;
1578 if (m->wire_count++ == 0)
1579 vmstats.v_wire_count++;
1582 * Map the pagetable page into the process address space, if
1583 * it isn't already there.
1585 * It is possible that someone else got in and mapped the page
1586 * directory page while we were blocked, if so just unbusy and
1587 * return the held page.
1589 if (ptepindex >= (NUPDE + NUPDPE)) {
1591 * Wire up a new PDP page in the PML4
1593 vm_pindex_t pml4index;
1594 pml4_entry_t *pml4;
1596 pml4index = ptepindex - (NUPDE + NUPDPE);
1597 pml4 = &pmap->pm_pml4[pml4index];
1598 if (*pml4 & PG_V) {
1599 if (--m->wire_count == 0)
1600 --vmstats.v_wire_count;
1601 vm_page_wakeup(m);
1602 return(m);
1604 *pml4 = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
1605 } else if (ptepindex >= NUPDE) {
1607 * Wire up a new PD page in the PDP
1609 vm_pindex_t pml4index;
1610 vm_pindex_t pdpindex;
1611 vm_page_t pdppg;
1612 pml4_entry_t *pml4;
1613 pdp_entry_t *pdp;
1615 pdpindex = ptepindex - NUPDE;
1616 pml4index = pdpindex >> NPML4EPGSHIFT;
1618 pml4 = &pmap->pm_pml4[pml4index];
1619 if ((*pml4 & PG_V) == 0) {
1621 * Have to allocate a new PDP page, recurse.
1622 * This always succeeds. Returned page will
1623 * be held.
1625 pdppg = _pmap_allocpte(pmap,
1626 NUPDE + NUPDPE + pml4index);
1627 } else {
1629 * Add a held reference to the PDP page.
1631 pdppg = PHYS_TO_VM_PAGE(*pml4 & PG_FRAME);
1632 pdppg->hold_count++;
1636 * Now find the pdp_entry and map the PDP. If the PDP
1637 * has already been mapped unwind and return the
1638 * already-mapped PDP held.
1640 * pdppg is left held (hold_count is incremented for
1641 * each PD in the PDP).
1643 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
1644 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1645 if (*pdp & PG_V) {
1646 vm_page_unhold(pdppg);
1647 if (--m->wire_count == 0)
1648 --vmstats.v_wire_count;
1649 vm_page_wakeup(m);
1650 return(m);
1652 *pdp = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
1653 } else {
1655 * Wire up the new PT page in the PD
1657 vm_pindex_t pml4index;
1658 vm_pindex_t pdpindex;
1659 pml4_entry_t *pml4;
1660 pdp_entry_t *pdp;
1661 pd_entry_t *pd;
1662 vm_page_t pdpg;
1664 pdpindex = ptepindex >> NPDPEPGSHIFT;
1665 pml4index = pdpindex >> NPML4EPGSHIFT;
1668 * Locate the PDP page in the PML4, then the PD page in
1669 * the PDP. If either does not exist we simply recurse
1670 * to allocate them.
1672 * We can just recurse on the PD page as it will recurse
1673 * on the PDP if necessary.
1675 pml4 = &pmap->pm_pml4[pml4index];
1676 if ((*pml4 & PG_V) == 0) {
1677 pdpg = _pmap_allocpte(pmap, NUPDE + pdpindex);
1678 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
1679 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1680 } else {
1681 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
1682 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1683 if ((*pdp & PG_V) == 0) {
1684 pdpg = _pmap_allocpte(pmap, NUPDE + pdpindex);
1685 } else {
1686 pdpg = PHYS_TO_VM_PAGE(*pdp & PG_FRAME);
1687 pdpg->hold_count++;
1692 * Now fill in the pte in the PD. If the pte already exists
1693 * (again, if we raced the grab), unhold pdpg and unwire
1694 * m, returning a held m.
1696 * pdpg is left held (hold_count is incremented for
1697 * each PT in the PD).
1699 pd = (pd_entry_t *)PHYS_TO_DMAP(*pdp & PG_FRAME);
1700 pd = &pd[ptepindex & ((1ul << NPDEPGSHIFT) - 1)];
1701 if (*pd != 0) {
1702 vm_page_unhold(pdpg);
1703 if (--m->wire_count == 0)
1704 --vmstats.v_wire_count;
1705 vm_page_wakeup(m);
1706 return(m);
1708 *pd = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
1712 * We successfully loaded a PDP, PD, or PTE. Set the page table hint,
1713 * valid bits, mapped flag, unbusy, and we're done.
1715 pmap->pm_ptphint = m;
1716 ++pmap->pm_stats.resident_count;
1718 m->valid = VM_PAGE_BITS_ALL;
1719 vm_page_flag_clear(m, PG_ZERO);
1720 vm_page_flag_set(m, PG_MAPPED);
1721 vm_page_wakeup(m);
1723 return (m);
1726 static
1727 vm_page_t
1728 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1730 vm_pindex_t ptepindex;
1731 pd_entry_t *pd;
1732 vm_page_t m;
1735 * Calculate pagetable page index
1737 ptepindex = pmap_pde_pindex(va);
1740 * Get the page directory entry
1742 pd = pmap_pde(pmap, va);
1745 * This supports switching from a 2MB page to a
1746 * normal 4K page.
1748 if (pd != NULL && (*pd & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
1749 panic("no promotion/demotion yet");
1750 *pd = 0;
1751 pd = NULL;
1752 cpu_invltlb();
1753 smp_invltlb();
1757 * If the page table page is mapped, we just increment the
1758 * hold count, and activate it.
1760 if (pd != NULL && (*pd & PG_V) != 0) {
1761 /* YYY hint is used here on i386 */
1762 m = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1763 pmap->pm_ptphint = m;
1764 m->hold_count++;
1765 return m;
1768 * Here if the pte page isn't mapped, or if it has been deallocated.
1770 return _pmap_allocpte(pmap, ptepindex);
1774 /***************************************************
1775 * Pmap allocation/deallocation routines.
1776 ***************************************************/
1779 * Release any resources held by the given physical map.
1780 * Called when a pmap initialized by pmap_pinit is being released.
1781 * Should only be called if the map contains no valid mappings.
1783 static int pmap_release_callback(struct vm_page *p, void *data);
1785 void
1786 pmap_release(struct pmap *pmap)
1788 vm_object_t object = pmap->pm_pteobj;
1789 struct rb_vm_page_scan_info info;
1791 KASSERT(pmap->pm_active == 0, ("pmap still active! %08x", pmap->pm_active));
1792 #if defined(DIAGNOSTIC)
1793 if (object->ref_count != 1)
1794 panic("pmap_release: pteobj reference count != 1");
1795 #endif
1797 info.pmap = pmap;
1798 info.object = object;
1799 crit_enter();
1800 lwkt_gettoken(&vm_token);
1801 TAILQ_REMOVE(&pmap_list, pmap, pm_pmnode);
1802 crit_exit();
1804 do {
1805 crit_enter();
1806 info.error = 0;
1807 info.mpte = NULL;
1808 info.limit = object->generation;
1810 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
1811 pmap_release_callback, &info);
1812 if (info.error == 0 && info.mpte) {
1813 if (!pmap_release_free_page(pmap, info.mpte))
1814 info.error = 1;
1816 crit_exit();
1817 } while (info.error);
1818 lwkt_reltoken(&vm_token);
1821 static
1823 pmap_release_callback(struct vm_page *p, void *data)
1825 struct rb_vm_page_scan_info *info = data;
1827 if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1828 info->mpte = p;
1829 return(0);
1831 if (!pmap_release_free_page(info->pmap, p)) {
1832 info->error = 1;
1833 return(-1);
1835 if (info->object->generation != info->limit) {
1836 info->error = 1;
1837 return(-1);
1839 return(0);
1843 * Grow the number of kernel page table entries, if needed.
1845 * This routine is always called to validate any address space
1846 * beyond KERNBASE (for kldloads). kernel_vm_end only governs the address
1847 * space below KERNBASE.
1849 void
1850 pmap_growkernel(vm_offset_t kstart, vm_offset_t kend)
1852 vm_paddr_t paddr;
1853 vm_offset_t ptppaddr;
1854 vm_page_t nkpg;
1855 pd_entry_t *pde, newpdir;
1856 pdp_entry_t newpdp;
1857 int update_kernel_vm_end;
1859 crit_enter();
1860 lwkt_gettoken(&vm_token);
1863 * bootstrap kernel_vm_end on first real VM use
1865 if (kernel_vm_end == 0) {
1866 kernel_vm_end = VM_MIN_KERNEL_ADDRESS;
1867 nkpt = 0;
1868 while ((*pmap_pde(&kernel_pmap, kernel_vm_end) & PG_V) != 0) {
1869 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) &
1870 ~(PAGE_SIZE * NPTEPG - 1);
1871 nkpt++;
1872 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1873 kernel_vm_end = kernel_map.max_offset;
1874 break;
1880 * Fill in the gaps. kernel_vm_end is only adjusted for ranges
1881 * below KERNBASE. Ranges above KERNBASE are kldloaded and we
1882 * do not want to force-fill 128G worth of page tables.
1884 if (kstart < KERNBASE) {
1885 if (kstart > kernel_vm_end)
1886 kstart = kernel_vm_end;
1887 KKASSERT(kend <= KERNBASE);
1888 update_kernel_vm_end = 1;
1889 } else {
1890 update_kernel_vm_end = 0;
1893 kstart = rounddown2(kstart, PAGE_SIZE * NPTEPG);
1894 kend = roundup2(kend, PAGE_SIZE * NPTEPG);
1896 if (kend - 1 >= kernel_map.max_offset)
1897 kend = kernel_map.max_offset;
1899 while (kstart < kend) {
1900 pde = pmap_pde(&kernel_pmap, kstart);
1901 if (pde == NULL) {
1902 /* We need a new PDP entry */
1903 nkpg = vm_page_alloc(kptobj, nkpt,
1904 VM_ALLOC_NORMAL |
1905 VM_ALLOC_SYSTEM |
1906 VM_ALLOC_INTERRUPT);
1907 if (nkpg == NULL) {
1908 panic("pmap_growkernel: no memory to grow "
1909 "kernel");
1911 paddr = VM_PAGE_TO_PHYS(nkpg);
1912 if ((nkpg->flags & PG_ZERO) == 0)
1913 pmap_zero_page(paddr);
1914 vm_page_flag_clear(nkpg, PG_ZERO);
1915 newpdp = (pdp_entry_t)
1916 (paddr | PG_V | PG_RW | PG_A | PG_M);
1917 *pmap_pdpe(&kernel_pmap, kstart) = newpdp;
1918 nkpt++;
1919 continue; /* try again */
1921 if ((*pde & PG_V) != 0) {
1922 kstart = (kstart + PAGE_SIZE * NPTEPG) &
1923 ~(PAGE_SIZE * NPTEPG - 1);
1924 if (kstart - 1 >= kernel_map.max_offset) {
1925 kstart = kernel_map.max_offset;
1926 break;
1928 continue;
1932 * This index is bogus, but out of the way
1934 nkpg = vm_page_alloc(kptobj, nkpt,
1935 VM_ALLOC_NORMAL |
1936 VM_ALLOC_SYSTEM |
1937 VM_ALLOC_INTERRUPT);
1938 if (nkpg == NULL)
1939 panic("pmap_growkernel: no memory to grow kernel");
1941 vm_page_wire(nkpg);
1942 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1943 pmap_zero_page(ptppaddr);
1944 vm_page_flag_clear(nkpg, PG_ZERO);
1945 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
1946 *pmap_pde(&kernel_pmap, kstart) = newpdir;
1947 nkpt++;
1949 kstart = (kstart + PAGE_SIZE * NPTEPG) &
1950 ~(PAGE_SIZE * NPTEPG - 1);
1952 if (kstart - 1 >= kernel_map.max_offset) {
1953 kstart = kernel_map.max_offset;
1954 break;
1959 * Only update kernel_vm_end for areas below KERNBASE.
1961 if (update_kernel_vm_end && kernel_vm_end < kstart)
1962 kernel_vm_end = kstart;
1964 lwkt_reltoken(&vm_token);
1965 crit_exit();
1969 * Retire the given physical map from service.
1970 * Should only be called if the map contains
1971 * no valid mappings.
1973 void
1974 pmap_destroy(pmap_t pmap)
1976 int count;
1978 if (pmap == NULL)
1979 return;
1981 lwkt_gettoken(&vm_token);
1982 count = --pmap->pm_count;
1983 if (count == 0) {
1984 pmap_release(pmap);
1985 panic("destroying a pmap is not yet implemented");
1987 lwkt_reltoken(&vm_token);
1991 * Add a reference to the specified pmap.
1993 void
1994 pmap_reference(pmap_t pmap)
1996 if (pmap != NULL) {
1997 lwkt_gettoken(&vm_token);
1998 pmap->pm_count++;
1999 lwkt_reltoken(&vm_token);
2003 /***************************************************
2004 * page management routines.
2005 ***************************************************/
2008 * free the pv_entry back to the free list. This function may be
2009 * called from an interrupt.
2011 static __inline
2012 void
2013 free_pv_entry(pv_entry_t pv)
2015 pv_entry_count--;
2016 KKASSERT(pv_entry_count >= 0);
2017 zfree(pvzone, pv);
2021 * get a new pv_entry, allocating a block from the system
2022 * when needed. This function may be called from an interrupt.
2024 static
2025 pv_entry_t
2026 get_pv_entry(void)
2028 pv_entry_count++;
2029 if (pv_entry_high_water &&
2030 (pv_entry_count > pv_entry_high_water) &&
2031 (pmap_pagedaemon_waken == 0)) {
2032 pmap_pagedaemon_waken = 1;
2033 wakeup(&vm_pages_needed);
2035 return zalloc(pvzone);
2039 * This routine is very drastic, but can save the system
2040 * in a pinch.
2042 void
2043 pmap_collect(void)
2045 int i;
2046 vm_page_t m;
2047 static int warningdone=0;
2049 if (pmap_pagedaemon_waken == 0)
2050 return;
2051 lwkt_gettoken(&vm_token);
2052 if (warningdone < 5) {
2053 kprintf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
2054 warningdone++;
2057 for(i = 0; i < vm_page_array_size; i++) {
2058 m = &vm_page_array[i];
2059 if (m->wire_count || m->hold_count || m->busy ||
2060 (m->flags & PG_BUSY))
2061 continue;
2062 pmap_remove_all(m);
2064 pmap_pagedaemon_waken = 0;
2065 lwkt_reltoken(&vm_token);
2070 * If it is the first entry on the list, it is actually
2071 * in the header and we must copy the following entry up
2072 * to the header. Otherwise we must search the list for
2073 * the entry. In either case we free the now unused entry.
2075 static
2077 pmap_remove_entry(struct pmap *pmap, vm_page_t m,
2078 vm_offset_t va, pmap_inval_info_t info)
2080 pv_entry_t pv;
2081 int rtval;
2083 crit_enter();
2084 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
2085 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2086 if (pmap == pv->pv_pmap && va == pv->pv_va)
2087 break;
2089 } else {
2090 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
2091 if (va == pv->pv_va)
2092 break;
2096 rtval = 0;
2097 KKASSERT(pv);
2099 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2100 m->md.pv_list_count--;
2101 m->object->agg_pv_list_count--;
2102 KKASSERT(m->md.pv_list_count >= 0);
2103 if (TAILQ_EMPTY(&m->md.pv_list))
2104 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2105 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
2106 ++pmap->pm_generation;
2107 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem, info);
2108 free_pv_entry(pv);
2110 crit_exit();
2111 return rtval;
2115 * Create a pv entry for page at pa for
2116 * (pmap, va).
2118 static
2119 void
2120 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
2122 pv_entry_t pv;
2124 crit_enter();
2125 pv = get_pv_entry();
2126 pv->pv_va = va;
2127 pv->pv_pmap = pmap;
2128 pv->pv_ptem = mpte;
2130 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
2131 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2132 ++pmap->pm_generation;
2133 m->md.pv_list_count++;
2134 m->object->agg_pv_list_count++;
2136 crit_exit();
2140 * pmap_remove_pte: do the things to unmap a page in a process
2142 static
2144 pmap_remove_pte(struct pmap *pmap, pt_entry_t *ptq, vm_offset_t va,
2145 pmap_inval_info_t info)
2147 pt_entry_t oldpte;
2148 vm_page_t m;
2150 pmap_inval_interlock(info, pmap, va);
2151 oldpte = pte_load_clear(ptq);
2152 pmap_inval_deinterlock(info, pmap);
2153 if (oldpte & PG_W)
2154 pmap->pm_stats.wired_count -= 1;
2156 * Machines that don't support invlpg, also don't support
2157 * PG_G. XXX PG_G is disabled for SMP so don't worry about
2158 * the SMP case.
2160 if (oldpte & PG_G)
2161 cpu_invlpg((void *)va);
2162 KKASSERT(pmap->pm_stats.resident_count > 0);
2163 --pmap->pm_stats.resident_count;
2164 if (oldpte & PG_MANAGED) {
2165 m = PHYS_TO_VM_PAGE(oldpte);
2166 if (oldpte & PG_M) {
2167 #if defined(PMAP_DIAGNOSTIC)
2168 if (pmap_nw_modified((pt_entry_t) oldpte)) {
2169 kprintf(
2170 "pmap_remove: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2171 va, oldpte);
2173 #endif
2174 if (pmap_track_modified(va))
2175 vm_page_dirty(m);
2177 if (oldpte & PG_A)
2178 vm_page_flag_set(m, PG_REFERENCED);
2179 return pmap_remove_entry(pmap, m, va, info);
2180 } else {
2181 return pmap_unuse_pt(pmap, va, NULL, info);
2184 return 0;
2188 * pmap_remove_page:
2190 * Remove a single page from a process address space.
2192 * This function may not be called from an interrupt if the pmap is
2193 * not kernel_pmap.
2195 static
2196 void
2197 pmap_remove_page(struct pmap *pmap, vm_offset_t va, pmap_inval_info_t info)
2199 pt_entry_t *pte;
2201 pte = pmap_pte(pmap, va);
2202 if (pte == NULL)
2203 return;
2204 if ((*pte & PG_V) == 0)
2205 return;
2206 pmap_remove_pte(pmap, pte, va, info);
2210 * pmap_remove:
2212 * Remove the given range of addresses from the specified map.
2214 * It is assumed that the start and end are properly
2215 * rounded to the page size.
2217 * This function may not be called from an interrupt if the pmap is
2218 * not kernel_pmap.
2220 void
2221 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
2223 vm_offset_t va_next;
2224 pml4_entry_t *pml4e;
2225 pdp_entry_t *pdpe;
2226 pd_entry_t ptpaddr, *pde;
2227 pt_entry_t *pte;
2228 struct pmap_inval_info info;
2230 if (pmap == NULL)
2231 return;
2233 lwkt_gettoken(&vm_token);
2234 if (pmap->pm_stats.resident_count == 0) {
2235 lwkt_reltoken(&vm_token);
2236 return;
2239 pmap_inval_init(&info);
2242 * special handling of removing one page. a very
2243 * common operation and easy to short circuit some
2244 * code.
2246 if (sva + PAGE_SIZE == eva) {
2247 pde = pmap_pde(pmap, sva);
2248 if (pde && (*pde & PG_PS) == 0) {
2249 pmap_remove_page(pmap, sva, &info);
2250 pmap_inval_done(&info);
2251 lwkt_reltoken(&vm_token);
2252 return;
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;
2260 if (va_next < sva)
2261 va_next = eva;
2262 continue;
2265 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
2266 if ((*pdpe & PG_V) == 0) {
2267 va_next = (sva + NBPDP) & ~PDPMASK;
2268 if (va_next < sva)
2269 va_next = eva;
2270 continue;
2274 * Calculate index for next page table.
2276 va_next = (sva + NBPDR) & ~PDRMASK;
2277 if (va_next < sva)
2278 va_next = eva;
2280 pde = pmap_pdpe_to_pde(pdpe, sva);
2281 ptpaddr = *pde;
2284 * Weed out invalid mappings.
2286 if (ptpaddr == 0)
2287 continue;
2290 * Check for large page.
2292 if ((ptpaddr & PG_PS) != 0) {
2293 /* JG FreeBSD has more complex treatment here */
2294 pmap_inval_interlock(&info, pmap, -1);
2295 *pde = 0;
2296 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2297 pmap_inval_deinterlock(&info, pmap);
2298 continue;
2302 * Limit our scan to either the end of the va represented
2303 * by the current page table page, or to the end of the
2304 * range being removed.
2306 if (va_next > eva)
2307 va_next = eva;
2310 * NOTE: pmap_remove_pte() can block.
2312 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2313 sva += PAGE_SIZE) {
2314 if (*pte == 0)
2315 continue;
2316 if (pmap_remove_pte(pmap, pte, sva, &info))
2317 break;
2320 pmap_inval_done(&info);
2321 lwkt_reltoken(&vm_token);
2325 * pmap_remove_all:
2327 * Removes this physical page from all physical maps in which it resides.
2328 * Reflects back modify bits to the pager.
2330 * This routine may not be called from an interrupt.
2333 static
2334 void
2335 pmap_remove_all(vm_page_t m)
2337 struct pmap_inval_info info;
2338 pt_entry_t *pte, tpte;
2339 pv_entry_t pv;
2341 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2342 return;
2344 lwkt_gettoken(&vm_token);
2345 pmap_inval_init(&info);
2346 crit_enter();
2347 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2348 KKASSERT(pv->pv_pmap->pm_stats.resident_count > 0);
2349 --pv->pv_pmap->pm_stats.resident_count;
2351 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2352 pmap_inval_interlock(&info, pv->pv_pmap, pv->pv_va);
2353 tpte = pte_load_clear(pte);
2354 if (tpte & PG_W)
2355 pv->pv_pmap->pm_stats.wired_count--;
2356 pmap_inval_deinterlock(&info, pv->pv_pmap);
2357 if (tpte & PG_A)
2358 vm_page_flag_set(m, PG_REFERENCED);
2361 * Update the vm_page_t clean and reference bits.
2363 if (tpte & PG_M) {
2364 #if defined(PMAP_DIAGNOSTIC)
2365 if (pmap_nw_modified(tpte)) {
2366 kprintf(
2367 "pmap_remove_all: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2368 pv->pv_va, tpte);
2370 #endif
2371 if (pmap_track_modified(pv->pv_va))
2372 vm_page_dirty(m);
2374 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2375 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2376 ++pv->pv_pmap->pm_generation;
2377 m->md.pv_list_count--;
2378 m->object->agg_pv_list_count--;
2379 KKASSERT(m->md.pv_list_count >= 0);
2380 if (TAILQ_EMPTY(&m->md.pv_list))
2381 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2382 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem, &info);
2383 free_pv_entry(pv);
2385 crit_exit();
2386 KKASSERT((m->flags & (PG_MAPPED|PG_WRITEABLE)) == 0);
2387 pmap_inval_done(&info);
2388 lwkt_reltoken(&vm_token);
2392 * pmap_protect:
2394 * Set the physical protection on the specified range of this map
2395 * as requested.
2397 * This function may not be called from an interrupt if the map is
2398 * not the kernel_pmap.
2400 void
2401 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
2403 vm_offset_t va_next;
2404 pml4_entry_t *pml4e;
2405 pdp_entry_t *pdpe;
2406 pd_entry_t ptpaddr, *pde;
2407 pt_entry_t *pte;
2408 pmap_inval_info info;
2410 /* JG review for NX */
2412 if (pmap == NULL)
2413 return;
2415 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
2416 pmap_remove(pmap, sva, eva);
2417 return;
2420 if (prot & VM_PROT_WRITE)
2421 return;
2423 lwkt_gettoken(&vm_token);
2424 pmap_inval_init(&info);
2426 for (; sva < eva; sva = va_next) {
2428 pml4e = pmap_pml4e(pmap, sva);
2429 if ((*pml4e & PG_V) == 0) {
2430 va_next = (sva + NBPML4) & ~PML4MASK;
2431 if (va_next < sva)
2432 va_next = eva;
2433 continue;
2436 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
2437 if ((*pdpe & PG_V) == 0) {
2438 va_next = (sva + NBPDP) & ~PDPMASK;
2439 if (va_next < sva)
2440 va_next = eva;
2441 continue;
2444 va_next = (sva + NBPDR) & ~PDRMASK;
2445 if (va_next < sva)
2446 va_next = eva;
2448 pde = pmap_pdpe_to_pde(pdpe, sva);
2449 ptpaddr = *pde;
2452 * Check for large page.
2454 if ((ptpaddr & PG_PS) != 0) {
2455 pmap_inval_interlock(&info, pmap, -1);
2456 *pde &= ~(PG_M|PG_RW);
2457 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2458 pmap_inval_deinterlock(&info, pmap);
2459 continue;
2463 * Weed out invalid mappings. Note: we assume that the page
2464 * directory table is always allocated, and in kernel virtual.
2466 if (ptpaddr == 0)
2467 continue;
2469 if (va_next > eva)
2470 va_next = eva;
2472 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2473 sva += PAGE_SIZE) {
2474 pt_entry_t pbits;
2475 pt_entry_t cbits;
2476 vm_page_t m;
2479 * XXX non-optimal. Note also that there can be
2480 * no pmap_inval_flush() calls until after we modify
2481 * ptbase[sindex] (or otherwise we have to do another
2482 * pmap_inval_add() call).
2484 pmap_inval_interlock(&info, pmap, sva);
2485 again:
2486 pbits = *pte;
2487 cbits = pbits;
2488 if ((pbits & PG_V) == 0) {
2489 pmap_inval_deinterlock(&info, pmap);
2490 continue;
2492 if (pbits & PG_MANAGED) {
2493 m = NULL;
2494 if (pbits & PG_A) {
2495 m = PHYS_TO_VM_PAGE(pbits & PG_FRAME);
2496 vm_page_flag_set(m, PG_REFERENCED);
2497 cbits &= ~PG_A;
2499 if (pbits & PG_M) {
2500 if (pmap_track_modified(sva)) {
2501 if (m == NULL)
2502 m = PHYS_TO_VM_PAGE(pbits & PG_FRAME);
2503 vm_page_dirty(m);
2504 cbits &= ~PG_M;
2508 cbits &= ~PG_RW;
2509 if (pbits != cbits &&
2510 !atomic_cmpset_long(pte, pbits, cbits)) {
2511 goto again;
2513 pmap_inval_deinterlock(&info, pmap);
2516 pmap_inval_done(&info);
2517 lwkt_reltoken(&vm_token);
2521 * Insert the given physical page (p) at
2522 * the specified virtual address (v) in the
2523 * target physical map with the protection requested.
2525 * If specified, the page will be wired down, meaning
2526 * that the related pte can not be reclaimed.
2528 * NB: This is the only routine which MAY NOT lazy-evaluate
2529 * or lose information. That is, this routine must actually
2530 * insert this page into the given map NOW.
2532 void
2533 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
2534 boolean_t wired)
2536 vm_paddr_t pa;
2537 pd_entry_t *pde;
2538 pt_entry_t *pte;
2539 vm_paddr_t opa;
2540 pt_entry_t origpte, newpte;
2541 vm_page_t mpte;
2542 pmap_inval_info info;
2544 if (pmap == NULL)
2545 return;
2547 va = trunc_page(va);
2548 #ifdef PMAP_DIAGNOSTIC
2549 if (va >= KvaEnd)
2550 panic("pmap_enter: toobig");
2551 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
2552 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%lx)", va);
2553 #endif
2554 if (va < UPT_MAX_ADDRESS && pmap == &kernel_pmap) {
2555 kprintf("Warning: pmap_enter called on UVA with kernel_pmap\n");
2556 #ifdef DDB
2557 db_print_backtrace();
2558 #endif
2560 if (va >= UPT_MAX_ADDRESS && pmap != &kernel_pmap) {
2561 kprintf("Warning: pmap_enter called on KVA without kernel_pmap\n");
2562 #ifdef DDB
2563 db_print_backtrace();
2564 #endif
2567 lwkt_gettoken(&vm_token);
2570 * In the case that a page table page is not
2571 * resident, we are creating it here.
2573 if (va < VM_MAX_USER_ADDRESS)
2574 mpte = pmap_allocpte(pmap, va);
2575 else
2576 mpte = NULL;
2578 pmap_inval_init(&info);
2579 pde = pmap_pde(pmap, va);
2580 if (pde != NULL && (*pde & PG_V) != 0) {
2581 if ((*pde & PG_PS) != 0)
2582 panic("pmap_enter: attempted pmap_enter on 2MB page");
2583 pte = pmap_pde_to_pte(pde, va);
2584 } else
2585 panic("pmap_enter: invalid page directory va=%#lx", va);
2587 KKASSERT(pte != NULL);
2588 pa = VM_PAGE_TO_PHYS(m);
2589 origpte = *pte;
2590 opa = origpte & PG_FRAME;
2593 * Mapping has not changed, must be protection or wiring change.
2595 if (origpte && (opa == pa)) {
2597 * Wiring change, just update stats. We don't worry about
2598 * wiring PT pages as they remain resident as long as there
2599 * are valid mappings in them. Hence, if a user page is wired,
2600 * the PT page will be also.
2602 if (wired && ((origpte & PG_W) == 0))
2603 pmap->pm_stats.wired_count++;
2604 else if (!wired && (origpte & PG_W))
2605 pmap->pm_stats.wired_count--;
2607 #if defined(PMAP_DIAGNOSTIC)
2608 if (pmap_nw_modified(origpte)) {
2609 kprintf(
2610 "pmap_enter: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2611 va, origpte);
2613 #endif
2616 * Remove the extra pte reference. Note that we cannot
2617 * optimize the RO->RW case because we have adjusted the
2618 * wiring count above and may need to adjust the wiring
2619 * bits below.
2621 if (mpte)
2622 mpte->hold_count--;
2625 * We might be turning off write access to the page,
2626 * so we go ahead and sense modify status.
2628 if (origpte & PG_MANAGED) {
2629 if ((origpte & PG_M) && pmap_track_modified(va)) {
2630 vm_page_t om;
2631 om = PHYS_TO_VM_PAGE(opa);
2632 vm_page_dirty(om);
2634 pa |= PG_MANAGED;
2635 KKASSERT(m->flags & PG_MAPPED);
2637 goto validate;
2640 * Mapping has changed, invalidate old range and fall through to
2641 * handle validating new mapping.
2643 while (opa) {
2644 int err;
2645 err = pmap_remove_pte(pmap, pte, va, &info);
2646 if (err)
2647 panic("pmap_enter: pte vanished, va: 0x%lx", va);
2648 origpte = *pte;
2649 opa = origpte & PG_FRAME;
2650 if (opa) {
2651 kprintf("pmap_enter: Warning, raced pmap %p va %p\n",
2652 pmap, (void *)va);
2657 * Enter on the PV list if part of our managed memory. Note that we
2658 * raise IPL while manipulating pv_table since pmap_enter can be
2659 * called at interrupt time.
2661 if (pmap_initialized &&
2662 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2663 pmap_insert_entry(pmap, va, mpte, m);
2664 pa |= PG_MANAGED;
2665 vm_page_flag_set(m, PG_MAPPED);
2669 * Increment counters
2671 ++pmap->pm_stats.resident_count;
2672 if (wired)
2673 pmap->pm_stats.wired_count++;
2675 validate:
2677 * Now validate mapping with desired protection/wiring.
2679 newpte = (pt_entry_t) (pa | pte_prot(pmap, prot) | PG_V);
2681 if (wired)
2682 newpte |= PG_W;
2683 if (va < VM_MAX_USER_ADDRESS)
2684 newpte |= PG_U;
2685 if (pmap == &kernel_pmap)
2686 newpte |= pgeflag;
2689 * if the mapping or permission bits are different, we need
2690 * to update the pte.
2692 if ((origpte & ~(PG_M|PG_A)) != newpte) {
2693 pmap_inval_interlock(&info, pmap, va);
2694 *pte = newpte | PG_A;
2695 pmap_inval_deinterlock(&info, pmap);
2696 if (newpte & PG_RW)
2697 vm_page_flag_set(m, PG_WRITEABLE);
2699 KKASSERT((newpte & PG_MANAGED) == 0 || (m->flags & PG_MAPPED));
2700 pmap_inval_done(&info);
2701 lwkt_reltoken(&vm_token);
2705 * This code works like pmap_enter() but assumes VM_PROT_READ and not-wired.
2706 * This code also assumes that the pmap has no pre-existing entry for this
2707 * VA.
2709 * This code currently may only be used on user pmaps, not kernel_pmap.
2711 void
2712 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m)
2714 pt_entry_t *pte;
2715 vm_paddr_t pa;
2716 vm_page_t mpte;
2717 vm_pindex_t ptepindex;
2718 pd_entry_t *ptepa;
2719 pmap_inval_info info;
2721 lwkt_gettoken(&vm_token);
2722 pmap_inval_init(&info);
2724 if (va < UPT_MAX_ADDRESS && pmap == &kernel_pmap) {
2725 kprintf("Warning: pmap_enter_quick called on UVA with kernel_pmap\n");
2726 #ifdef DDB
2727 db_print_backtrace();
2728 #endif
2730 if (va >= UPT_MAX_ADDRESS && pmap != &kernel_pmap) {
2731 kprintf("Warning: pmap_enter_quick called on KVA without kernel_pmap\n");
2732 #ifdef DDB
2733 db_print_backtrace();
2734 #endif
2737 KKASSERT(va < UPT_MIN_ADDRESS); /* assert used on user pmaps only */
2740 * Calculate the page table page (mpte), allocating it if necessary.
2742 * A held page table page (mpte), or NULL, is passed onto the
2743 * section following.
2745 if (va < VM_MAX_USER_ADDRESS) {
2747 * Calculate pagetable page index
2749 ptepindex = pmap_pde_pindex(va);
2751 do {
2753 * Get the page directory entry
2755 ptepa = pmap_pde(pmap, va);
2758 * If the page table page is mapped, we just increment
2759 * the hold count, and activate it.
2761 if (ptepa && (*ptepa & PG_V) != 0) {
2762 if (*ptepa & PG_PS)
2763 panic("pmap_enter_quick: unexpected mapping into 2MB page");
2764 // if (pmap->pm_ptphint &&
2765 // (pmap->pm_ptphint->pindex == ptepindex)) {
2766 // mpte = pmap->pm_ptphint;
2767 // } else {
2768 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
2769 pmap->pm_ptphint = mpte;
2770 // }
2771 if (mpte)
2772 mpte->hold_count++;
2773 } else {
2774 mpte = _pmap_allocpte(pmap, ptepindex);
2776 } while (mpte == NULL);
2777 } else {
2778 mpte = NULL;
2779 /* this code path is not yet used */
2783 * With a valid (and held) page directory page, we can just use
2784 * vtopte() to get to the pte. If the pte is already present
2785 * we do not disturb it.
2787 pte = vtopte(va);
2788 if (*pte & PG_V) {
2789 if (mpte)
2790 pmap_unwire_pte_hold(pmap, va, mpte, &info);
2791 pa = VM_PAGE_TO_PHYS(m);
2792 KKASSERT(((*pte ^ pa) & PG_FRAME) == 0);
2793 pmap_inval_done(&info);
2794 lwkt_reltoken(&vm_token);
2795 return;
2799 * Enter on the PV list if part of our managed memory
2801 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2802 pmap_insert_entry(pmap, va, mpte, m);
2803 vm_page_flag_set(m, PG_MAPPED);
2807 * Increment counters
2809 ++pmap->pm_stats.resident_count;
2811 pa = VM_PAGE_TO_PHYS(m);
2814 * Now validate mapping with RO protection
2816 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2817 *pte = pa | PG_V | PG_U;
2818 else
2819 *pte = pa | PG_V | PG_U | PG_MANAGED;
2820 /* pmap_inval_add(&info, pmap, va); shouldn't be needed inval->valid */
2821 pmap_inval_done(&info);
2822 lwkt_reltoken(&vm_token);
2826 * Make a temporary mapping for a physical address. This is only intended
2827 * to be used for panic dumps.
2829 /* JG Needed on x86_64? */
2830 void *
2831 pmap_kenter_temporary(vm_paddr_t pa, int i)
2833 pmap_kenter((vm_offset_t)crashdumpmap + (i * PAGE_SIZE), pa);
2834 return ((void *)crashdumpmap);
2837 #define MAX_INIT_PT (96)
2840 * This routine preloads the ptes for a given object into the specified pmap.
2841 * This eliminates the blast of soft faults on process startup and
2842 * immediately after an mmap.
2844 static int pmap_object_init_pt_callback(vm_page_t p, void *data);
2846 void
2847 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2848 vm_object_t object, vm_pindex_t pindex,
2849 vm_size_t size, int limit)
2851 struct rb_vm_page_scan_info info;
2852 struct lwp *lp;
2853 vm_size_t psize;
2856 * We can't preinit if read access isn't set or there is no pmap
2857 * or object.
2859 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2860 return;
2863 * We can't preinit if the pmap is not the current pmap
2865 lp = curthread->td_lwp;
2866 if (lp == NULL || pmap != vmspace_pmap(lp->lwp_vmspace))
2867 return;
2869 psize = x86_64_btop(size);
2871 if ((object->type != OBJT_VNODE) ||
2872 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2873 (object->resident_page_count > MAX_INIT_PT))) {
2874 return;
2877 if (psize + pindex > object->size) {
2878 if (object->size < pindex)
2879 return;
2880 psize = object->size - pindex;
2883 if (psize == 0)
2884 return;
2887 * Use a red-black scan to traverse the requested range and load
2888 * any valid pages found into the pmap.
2890 * We cannot safely scan the object's memq unless we are in a
2891 * critical section since interrupts can remove pages from objects.
2893 info.start_pindex = pindex;
2894 info.end_pindex = pindex + psize - 1;
2895 info.limit = limit;
2896 info.mpte = NULL;
2897 info.addr = addr;
2898 info.pmap = pmap;
2900 crit_enter();
2901 lwkt_gettoken(&vm_token);
2902 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2903 pmap_object_init_pt_callback, &info);
2904 lwkt_reltoken(&vm_token);
2905 crit_exit();
2908 static
2910 pmap_object_init_pt_callback(vm_page_t p, void *data)
2912 struct rb_vm_page_scan_info *info = data;
2913 vm_pindex_t rel_index;
2915 * don't allow an madvise to blow away our really
2916 * free pages allocating pv entries.
2918 if ((info->limit & MAP_PREFAULT_MADVISE) &&
2919 vmstats.v_free_count < vmstats.v_free_reserved) {
2920 return(-1);
2922 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2923 (p->busy == 0) && (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2924 if ((p->queue - p->pc) == PQ_CACHE)
2925 vm_page_deactivate(p);
2926 vm_page_busy(p);
2927 rel_index = p->pindex - info->start_pindex;
2928 pmap_enter_quick(info->pmap,
2929 info->addr + x86_64_ptob(rel_index), p);
2930 vm_page_wakeup(p);
2932 return(0);
2936 * Return TRUE if the pmap is in shape to trivially
2937 * pre-fault the specified address.
2939 * Returns FALSE if it would be non-trivial or if a
2940 * pte is already loaded into the slot.
2943 pmap_prefault_ok(pmap_t pmap, vm_offset_t addr)
2945 pt_entry_t *pte;
2946 pd_entry_t *pde;
2947 int ret;
2949 lwkt_gettoken(&vm_token);
2950 pde = pmap_pde(pmap, addr);
2951 if (pde == NULL || *pde == 0) {
2952 ret = 0;
2953 } else {
2954 pte = vtopte(addr);
2955 ret = (*pte) ? 0 : 1;
2957 lwkt_reltoken(&vm_token);
2958 return(ret);
2962 * Routine: pmap_change_wiring
2963 * Function: Change the wiring attribute for a map/virtual-address
2964 * pair.
2965 * In/out conditions:
2966 * The mapping must already exist in the pmap.
2968 void
2969 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2971 pt_entry_t *pte;
2973 if (pmap == NULL)
2974 return;
2976 lwkt_gettoken(&vm_token);
2977 pte = pmap_pte(pmap, va);
2979 if (wired && !pmap_pte_w(pte))
2980 pmap->pm_stats.wired_count++;
2981 else if (!wired && pmap_pte_w(pte))
2982 pmap->pm_stats.wired_count--;
2985 * Wiring is not a hardware characteristic so there is no need to
2986 * invalidate TLB. However, in an SMP environment we must use
2987 * a locked bus cycle to update the pte (if we are not using
2988 * the pmap_inval_*() API that is)... it's ok to do this for simple
2989 * wiring changes.
2991 #ifdef SMP
2992 if (wired)
2993 atomic_set_long(pte, PG_W);
2994 else
2995 atomic_clear_long(pte, PG_W);
2996 #else
2997 if (wired)
2998 atomic_set_long_nonlocked(pte, PG_W);
2999 else
3000 atomic_clear_long_nonlocked(pte, PG_W);
3001 #endif
3002 lwkt_reltoken(&vm_token);
3008 * Copy the range specified by src_addr/len
3009 * from the source map to the range dst_addr/len
3010 * in the destination map.
3012 * This routine is only advisory and need not do anything.
3014 void
3015 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
3016 vm_size_t len, vm_offset_t src_addr)
3018 return;
3019 #if 0
3020 pmap_inval_info info;
3021 vm_offset_t addr;
3022 vm_offset_t end_addr = src_addr + len;
3023 vm_offset_t pdnxt;
3024 pd_entry_t src_frame, dst_frame;
3025 vm_page_t m;
3027 if (dst_addr != src_addr)
3028 return;
3029 #if JGPMAP32
3030 src_frame = src_pmap->pm_pdir[PTDPTDI] & PG_FRAME;
3031 if (src_frame != (PTDpde & PG_FRAME)) {
3032 return;
3035 dst_frame = dst_pmap->pm_pdir[PTDPTDI] & PG_FRAME;
3036 if (dst_frame != (APTDpde & PG_FRAME)) {
3037 APTDpde = (pd_entry_t) (dst_frame | PG_RW | PG_V);
3038 /* The page directory is not shared between CPUs */
3039 cpu_invltlb();
3041 #endif
3042 pmap_inval_init(&info);
3043 pmap_inval_add(&info, dst_pmap, -1);
3044 pmap_inval_add(&info, src_pmap, -1);
3047 * critical section protection is required to maintain the page/object
3048 * association, interrupts can free pages and remove them from
3049 * their objects.
3051 crit_enter();
3052 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
3053 pt_entry_t *src_pte, *dst_pte;
3054 vm_page_t dstmpte, srcmpte;
3055 vm_offset_t srcptepaddr;
3056 vm_pindex_t ptepindex;
3058 if (addr >= UPT_MIN_ADDRESS)
3059 panic("pmap_copy: invalid to pmap_copy page tables\n");
3062 * Don't let optional prefaulting of pages make us go
3063 * way below the low water mark of free pages or way
3064 * above high water mark of used pv entries.
3066 if (vmstats.v_free_count < vmstats.v_free_reserved ||
3067 pv_entry_count > pv_entry_high_water)
3068 break;
3070 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
3071 ptepindex = addr >> PDRSHIFT;
3073 #if JGPMAP32
3074 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex];
3075 #endif
3076 if (srcptepaddr == 0)
3077 continue;
3079 if (srcptepaddr & PG_PS) {
3080 #if JGPMAP32
3081 if (dst_pmap->pm_pdir[ptepindex] == 0) {
3082 dst_pmap->pm_pdir[ptepindex] = (pd_entry_t) srcptepaddr;
3083 dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
3085 #endif
3086 continue;
3089 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
3090 if ((srcmpte == NULL) || (srcmpte->hold_count == 0) ||
3091 (srcmpte->flags & PG_BUSY)) {
3092 continue;
3095 if (pdnxt > end_addr)
3096 pdnxt = end_addr;
3098 src_pte = vtopte(addr);
3099 #if JGPMAP32
3100 dst_pte = avtopte(addr);
3101 #endif
3102 while (addr < pdnxt) {
3103 pt_entry_t ptetemp;
3105 ptetemp = *src_pte;
3107 * we only virtual copy managed pages
3109 if ((ptetemp & PG_MANAGED) != 0) {
3111 * We have to check after allocpte for the
3112 * pte still being around... allocpte can
3113 * block.
3115 * pmap_allocpte() can block. If we lose
3116 * our page directory mappings we stop.
3118 dstmpte = pmap_allocpte(dst_pmap, addr);
3120 #if JGPMAP32
3121 if (src_frame != (PTDpde & PG_FRAME) ||
3122 dst_frame != (APTDpde & PG_FRAME)
3124 kprintf("WARNING: pmap_copy: detected and corrected race\n");
3125 pmap_unwire_pte_hold(dst_pmap, dstmpte, &info);
3126 goto failed;
3127 } else if ((*dst_pte == 0) &&
3128 (ptetemp = *src_pte) != 0 &&
3129 (ptetemp & PG_MANAGED)) {
3131 * Clear the modified and
3132 * accessed (referenced) bits
3133 * during the copy.
3135 m = PHYS_TO_VM_PAGE(ptetemp);
3136 *dst_pte = ptetemp & ~(PG_M | PG_A);
3137 ++dst_pmap->pm_stats.resident_count;
3138 pmap_insert_entry(dst_pmap, addr,
3139 dstmpte, m);
3140 KKASSERT(m->flags & PG_MAPPED);
3141 } else {
3142 kprintf("WARNING: pmap_copy: dst_pte race detected and corrected\n");
3143 pmap_unwire_pte_hold(dst_pmap, dstmpte, &info);
3144 goto failed;
3146 #endif
3147 if (dstmpte->hold_count >= srcmpte->hold_count)
3148 break;
3150 addr += PAGE_SIZE;
3151 src_pte++;
3152 dst_pte++;
3155 failed:
3156 crit_exit();
3157 pmap_inval_done(&info);
3158 #endif
3162 * pmap_zero_page:
3164 * Zero the specified physical page.
3166 * This function may be called from an interrupt and no locking is
3167 * required.
3169 void
3170 pmap_zero_page(vm_paddr_t phys)
3172 vm_offset_t va = PHYS_TO_DMAP(phys);
3174 pagezero((void *)va);
3178 * pmap_page_assertzero:
3180 * Assert that a page is empty, panic if it isn't.
3182 void
3183 pmap_page_assertzero(vm_paddr_t phys)
3185 vm_offset_t virt = PHYS_TO_DMAP(phys);
3186 int i;
3188 for (i = 0; i < PAGE_SIZE; i += sizeof(long)) {
3189 if (*(long *)((char *)virt + i) != 0) {
3190 panic("pmap_page_assertzero() @ %p not zero!\n", (void *)virt);
3196 * pmap_zero_page:
3198 * Zero part of a physical page by mapping it into memory and clearing
3199 * its contents with bzero.
3201 * off and size may not cover an area beyond a single hardware page.
3203 void
3204 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
3206 vm_offset_t virt = PHYS_TO_DMAP(phys);
3208 bzero((char *)virt + off, size);
3212 * pmap_copy_page:
3214 * Copy the physical page from the source PA to the target PA.
3215 * This function may be called from an interrupt. No locking
3216 * is required.
3218 void
3219 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
3221 vm_offset_t src_virt, dst_virt;
3223 src_virt = PHYS_TO_DMAP(src);
3224 dst_virt = PHYS_TO_DMAP(dst);
3225 bcopy((void *)src_virt, (void *)dst_virt, PAGE_SIZE);
3229 * pmap_copy_page_frag:
3231 * Copy the physical page from the source PA to the target PA.
3232 * This function may be called from an interrupt. No locking
3233 * is required.
3235 void
3236 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
3238 vm_offset_t src_virt, dst_virt;
3240 src_virt = PHYS_TO_DMAP(src);
3241 dst_virt = PHYS_TO_DMAP(dst);
3243 bcopy((char *)src_virt + (src & PAGE_MASK),
3244 (char *)dst_virt + (dst & PAGE_MASK),
3245 bytes);
3249 * Returns true if the pmap's pv is one of the first
3250 * 16 pvs linked to from this page. This count may
3251 * be changed upwards or downwards in the future; it
3252 * is only necessary that true be returned for a small
3253 * subset of pmaps for proper page aging.
3255 boolean_t
3256 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
3258 pv_entry_t pv;
3259 int loops = 0;
3261 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3262 return FALSE;
3264 crit_enter();
3265 lwkt_gettoken(&vm_token);
3267 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3268 if (pv->pv_pmap == pmap) {
3269 lwkt_reltoken(&vm_token);
3270 crit_exit();
3271 return TRUE;
3273 loops++;
3274 if (loops >= 16)
3275 break;
3277 lwkt_reltoken(&vm_token);
3278 crit_exit();
3279 return (FALSE);
3283 * Remove all pages from specified address space
3284 * this aids process exit speeds. Also, this code
3285 * is special cased for current process only, but
3286 * can have the more generic (and slightly slower)
3287 * mode enabled. This is much faster than pmap_remove
3288 * in the case of running down an entire address space.
3290 void
3291 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
3293 struct lwp *lp;
3294 pt_entry_t *pte, tpte;
3295 pv_entry_t pv, npv;
3296 vm_page_t m;
3297 pmap_inval_info info;
3298 int iscurrentpmap;
3299 int save_generation;
3301 lp = curthread->td_lwp;
3302 if (lp && pmap == vmspace_pmap(lp->lwp_vmspace))
3303 iscurrentpmap = 1;
3304 else
3305 iscurrentpmap = 0;
3307 lwkt_gettoken(&vm_token);
3308 pmap_inval_init(&info);
3309 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
3310 if (pv->pv_va >= eva || pv->pv_va < sva) {
3311 npv = TAILQ_NEXT(pv, pv_plist);
3312 continue;
3315 KKASSERT(pmap == pv->pv_pmap);
3317 if (iscurrentpmap)
3318 pte = vtopte(pv->pv_va);
3319 else
3320 pte = pmap_pte_quick(pmap, pv->pv_va);
3321 pmap_inval_interlock(&info, pmap, pv->pv_va);
3324 * We cannot remove wired pages from a process' mapping
3325 * at this time
3327 if (*pte & PG_W) {
3328 pmap_inval_deinterlock(&info, pmap);
3329 npv = TAILQ_NEXT(pv, pv_plist);
3330 continue;
3332 tpte = pte_load_clear(pte);
3334 m = PHYS_TO_VM_PAGE(tpte & PG_FRAME);
3336 KASSERT(m < &vm_page_array[vm_page_array_size],
3337 ("pmap_remove_pages: bad tpte %lx", tpte));
3339 KKASSERT(pmap->pm_stats.resident_count > 0);
3340 --pmap->pm_stats.resident_count;
3341 pmap_inval_deinterlock(&info, pmap);
3344 * Update the vm_page_t clean and reference bits.
3346 if (tpte & PG_M) {
3347 vm_page_dirty(m);
3350 npv = TAILQ_NEXT(pv, pv_plist);
3351 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
3352 save_generation = ++pmap->pm_generation;
3354 m->md.pv_list_count--;
3355 m->object->agg_pv_list_count--;
3356 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3357 if (TAILQ_EMPTY(&m->md.pv_list))
3358 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
3360 pmap_unuse_pt(pmap, pv->pv_va, pv->pv_ptem, &info);
3361 free_pv_entry(pv);
3364 * Restart the scan if we blocked during the unuse or free
3365 * calls and other removals were made.
3367 if (save_generation != pmap->pm_generation) {
3368 kprintf("Warning: pmap_remove_pages race-A avoided\n");
3369 npv = TAILQ_FIRST(&pmap->pm_pvlist);
3372 pmap_inval_done(&info);
3373 lwkt_reltoken(&vm_token);
3377 * pmap_testbit tests bits in pte's
3378 * note that the testbit/clearbit routines are inline,
3379 * and a lot of things compile-time evaluate.
3381 static
3382 boolean_t
3383 pmap_testbit(vm_page_t m, int bit)
3385 pv_entry_t pv;
3386 pt_entry_t *pte;
3388 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3389 return FALSE;
3391 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
3392 return FALSE;
3394 crit_enter();
3396 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3398 * if the bit being tested is the modified bit, then
3399 * mark clean_map and ptes as never
3400 * modified.
3402 if (bit & (PG_A|PG_M)) {
3403 if (!pmap_track_modified(pv->pv_va))
3404 continue;
3407 #if defined(PMAP_DIAGNOSTIC)
3408 if (pv->pv_pmap == NULL) {
3409 kprintf("Null pmap (tb) at va: 0x%lx\n", pv->pv_va);
3410 continue;
3412 #endif
3413 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3414 if (*pte & bit) {
3415 crit_exit();
3416 return TRUE;
3419 crit_exit();
3420 return (FALSE);
3424 * this routine is used to modify bits in ptes
3426 static __inline
3427 void
3428 pmap_clearbit(vm_page_t m, int bit)
3430 struct pmap_inval_info info;
3431 pv_entry_t pv;
3432 pt_entry_t *pte;
3433 pt_entry_t pbits;
3435 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3436 return;
3438 pmap_inval_init(&info);
3441 * Loop over all current mappings setting/clearing as appropos If
3442 * setting RO do we need to clear the VAC?
3444 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3446 * don't write protect pager mappings
3448 if (bit == PG_RW) {
3449 if (!pmap_track_modified(pv->pv_va))
3450 continue;
3453 #if defined(PMAP_DIAGNOSTIC)
3454 if (pv->pv_pmap == NULL) {
3455 kprintf("Null pmap (cb) at va: 0x%lx\n", pv->pv_va);
3456 continue;
3458 #endif
3461 * Careful here. We can use a locked bus instruction to
3462 * clear PG_A or PG_M safely but we need to synchronize
3463 * with the target cpus when we mess with PG_RW.
3465 * We do not have to force synchronization when clearing
3466 * PG_M even for PTEs generated via virtual memory maps,
3467 * because the virtual kernel will invalidate the pmap
3468 * entry when/if it needs to resynchronize the Modify bit.
3470 if (bit & PG_RW)
3471 pmap_inval_interlock(&info, pv->pv_pmap, pv->pv_va);
3472 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3473 again:
3474 pbits = *pte;
3475 if (pbits & bit) {
3476 if (bit == PG_RW) {
3477 if (pbits & PG_M) {
3478 vm_page_dirty(m);
3479 atomic_clear_long(pte, PG_M|PG_RW);
3480 } else {
3482 * The cpu may be trying to set PG_M
3483 * simultaniously with our clearing
3484 * of PG_RW.
3486 if (!atomic_cmpset_long(pte, pbits,
3487 pbits & ~PG_RW))
3488 goto again;
3490 } else if (bit == PG_M) {
3492 * We could also clear PG_RW here to force
3493 * a fault on write to redetect PG_M for
3494 * virtual kernels, but it isn't necessary
3495 * since virtual kernels invalidate the pte
3496 * when they clear the VPTE_M bit in their
3497 * virtual page tables.
3499 atomic_clear_long(pte, PG_M);
3500 } else {
3501 atomic_clear_long(pte, bit);
3504 if (bit & PG_RW)
3505 pmap_inval_deinterlock(&info, pv->pv_pmap);
3507 pmap_inval_done(&info);
3511 * pmap_page_protect:
3513 * Lower the permission for all mappings to a given page.
3515 void
3516 pmap_page_protect(vm_page_t m, vm_prot_t prot)
3518 /* JG NX support? */
3519 if ((prot & VM_PROT_WRITE) == 0) {
3520 lwkt_gettoken(&vm_token);
3521 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
3522 pmap_clearbit(m, PG_RW);
3523 vm_page_flag_clear(m, PG_WRITEABLE);
3524 } else {
3525 pmap_remove_all(m);
3527 lwkt_reltoken(&vm_token);
3531 vm_paddr_t
3532 pmap_phys_address(vm_pindex_t ppn)
3534 return (x86_64_ptob(ppn));
3538 * pmap_ts_referenced:
3540 * Return a count of reference bits for a page, clearing those bits.
3541 * It is not necessary for every reference bit to be cleared, but it
3542 * is necessary that 0 only be returned when there are truly no
3543 * reference bits set.
3545 * XXX: The exact number of bits to check and clear is a matter that
3546 * should be tested and standardized at some point in the future for
3547 * optimal aging of shared pages.
3550 pmap_ts_referenced(vm_page_t m)
3552 pv_entry_t pv, pvf, pvn;
3553 pt_entry_t *pte;
3554 int rtval = 0;
3556 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3557 return (rtval);
3559 crit_enter();
3560 lwkt_gettoken(&vm_token);
3562 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3564 pvf = pv;
3566 do {
3567 pvn = TAILQ_NEXT(pv, pv_list);
3569 crit_enter();
3570 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3571 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3572 crit_exit();
3574 if (!pmap_track_modified(pv->pv_va))
3575 continue;
3577 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3579 if (pte && (*pte & PG_A)) {
3580 #ifdef SMP
3581 atomic_clear_long(pte, PG_A);
3582 #else
3583 atomic_clear_long_nonlocked(pte, PG_A);
3584 #endif
3585 rtval++;
3586 if (rtval > 4) {
3587 break;
3590 } while ((pv = pvn) != NULL && pv != pvf);
3592 lwkt_reltoken(&vm_token);
3593 crit_exit();
3595 return (rtval);
3599 * pmap_is_modified:
3601 * Return whether or not the specified physical page was modified
3602 * in any physical maps.
3604 boolean_t
3605 pmap_is_modified(vm_page_t m)
3607 boolean_t res;
3609 lwkt_gettoken(&vm_token);
3610 res = pmap_testbit(m, PG_M);
3611 lwkt_reltoken(&vm_token);
3612 return (res);
3616 * Clear the modify bits on the specified physical page.
3618 void
3619 pmap_clear_modify(vm_page_t m)
3621 lwkt_gettoken(&vm_token);
3622 pmap_clearbit(m, PG_M);
3623 lwkt_reltoken(&vm_token);
3627 * pmap_clear_reference:
3629 * Clear the reference bit on the specified physical page.
3631 void
3632 pmap_clear_reference(vm_page_t m)
3634 lwkt_gettoken(&vm_token);
3635 pmap_clearbit(m, PG_A);
3636 lwkt_reltoken(&vm_token);
3640 * Miscellaneous support routines follow
3643 static
3644 void
3645 i386_protection_init(void)
3647 int *kp, prot;
3649 /* JG NX support may go here; No VM_PROT_EXECUTE ==> set NX bit */
3650 kp = protection_codes;
3651 for (prot = 0; prot < 8; prot++) {
3652 switch (prot) {
3653 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE:
3655 * Read access is also 0. There isn't any execute bit,
3656 * so just make it readable.
3658 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE:
3659 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE:
3660 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE:
3661 *kp++ = 0;
3662 break;
3663 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE:
3664 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE:
3665 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE:
3666 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
3667 *kp++ = PG_RW;
3668 break;
3674 * Map a set of physical memory pages into the kernel virtual
3675 * address space. Return a pointer to where it is mapped. This
3676 * routine is intended to be used for mapping device memory,
3677 * NOT real memory.
3679 * NOTE: we can't use pgeflag unless we invalidate the pages one at
3680 * a time.
3682 void *
3683 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
3685 vm_offset_t va, tmpva, offset;
3686 pt_entry_t *pte;
3688 offset = pa & PAGE_MASK;
3689 size = roundup(offset + size, PAGE_SIZE);
3691 va = kmem_alloc_nofault(&kernel_map, size, PAGE_SIZE);
3692 if (va == 0)
3693 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3695 pa = pa & ~PAGE_MASK;
3696 for (tmpva = va; size > 0;) {
3697 pte = vtopte(tmpva);
3698 *pte = pa | PG_RW | PG_V; /* | pgeflag; */
3699 size -= PAGE_SIZE;
3700 tmpva += PAGE_SIZE;
3701 pa += PAGE_SIZE;
3703 cpu_invltlb();
3704 smp_invltlb();
3706 return ((void *)(va + offset));
3709 void *
3710 pmap_mapdev_uncacheable(vm_paddr_t pa, vm_size_t size)
3712 vm_offset_t va, tmpva, offset;
3713 pt_entry_t *pte;
3715 offset = pa & PAGE_MASK;
3716 size = roundup(offset + size, PAGE_SIZE);
3718 va = kmem_alloc_nofault(&kernel_map, size, PAGE_SIZE);
3719 if (va == 0)
3720 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3722 pa = pa & ~PAGE_MASK;
3723 for (tmpva = va; size > 0;) {
3724 pte = vtopte(tmpva);
3725 *pte = pa | PG_RW | PG_V | PG_N; /* | pgeflag; */
3726 size -= PAGE_SIZE;
3727 tmpva += PAGE_SIZE;
3728 pa += PAGE_SIZE;
3730 cpu_invltlb();
3731 smp_invltlb();
3733 return ((void *)(va + offset));
3736 void
3737 pmap_unmapdev(vm_offset_t va, vm_size_t size)
3739 vm_offset_t base, offset;
3741 base = va & ~PAGE_MASK;
3742 offset = va & PAGE_MASK;
3743 size = roundup(offset + size, PAGE_SIZE);
3744 pmap_qremove(va, size >> PAGE_SHIFT);
3745 kmem_free(&kernel_map, base, size);
3749 * perform the pmap work for mincore
3752 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3754 pt_entry_t *ptep, pte;
3755 vm_page_t m;
3756 int val = 0;
3758 lwkt_gettoken(&vm_token);
3759 ptep = pmap_pte(pmap, addr);
3761 if (ptep && (pte = *ptep) != 0) {
3762 vm_offset_t pa;
3764 val = MINCORE_INCORE;
3765 if ((pte & PG_MANAGED) == 0)
3766 goto done;
3768 pa = pte & PG_FRAME;
3770 m = PHYS_TO_VM_PAGE(pa);
3773 * Modified by us
3775 if (pte & PG_M)
3776 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3778 * Modified by someone
3780 else if (m->dirty || pmap_is_modified(m))
3781 val |= MINCORE_MODIFIED_OTHER;
3783 * Referenced by us
3785 if (pte & PG_A)
3786 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3789 * Referenced by someone
3791 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3792 val |= MINCORE_REFERENCED_OTHER;
3793 vm_page_flag_set(m, PG_REFERENCED);
3796 done:
3797 lwkt_reltoken(&vm_token);
3798 return val;
3802 * Replace p->p_vmspace with a new one. If adjrefs is non-zero the new
3803 * vmspace will be ref'd and the old one will be deref'd.
3805 * The vmspace for all lwps associated with the process will be adjusted
3806 * and cr3 will be reloaded if any lwp is the current lwp.
3808 void
3809 pmap_replacevm(struct proc *p, struct vmspace *newvm, int adjrefs)
3811 struct vmspace *oldvm;
3812 struct lwp *lp;
3814 crit_enter();
3815 oldvm = p->p_vmspace;
3816 if (oldvm != newvm) {
3817 p->p_vmspace = newvm;
3818 KKASSERT(p->p_nthreads == 1);
3819 lp = RB_ROOT(&p->p_lwp_tree);
3820 pmap_setlwpvm(lp, newvm);
3821 if (adjrefs) {
3822 sysref_get(&newvm->vm_sysref);
3823 sysref_put(&oldvm->vm_sysref);
3826 crit_exit();
3830 * Set the vmspace for a LWP. The vmspace is almost universally set the
3831 * same as the process vmspace, but virtual kernels need to swap out contexts
3832 * on a per-lwp basis.
3834 void
3835 pmap_setlwpvm(struct lwp *lp, struct vmspace *newvm)
3837 struct vmspace *oldvm;
3838 struct pmap *pmap;
3840 crit_enter();
3841 oldvm = lp->lwp_vmspace;
3843 if (oldvm != newvm) {
3844 lp->lwp_vmspace = newvm;
3845 if (curthread->td_lwp == lp) {
3846 pmap = vmspace_pmap(newvm);
3847 #if defined(SMP)
3848 atomic_set_int(&pmap->pm_active, mycpu->gd_cpumask);
3849 if (pmap->pm_active & CPUMASK_LOCK)
3850 pmap_interlock_wait(newvm);
3851 #else
3852 pmap->pm_active |= 1;
3853 #endif
3854 #if defined(SWTCH_OPTIM_STATS)
3855 tlb_flush_count++;
3856 #endif
3857 curthread->td_pcb->pcb_cr3 = vtophys(pmap->pm_pml4);
3858 curthread->td_pcb->pcb_cr3 |= PG_RW | PG_U | PG_V;
3859 load_cr3(curthread->td_pcb->pcb_cr3);
3860 pmap = vmspace_pmap(oldvm);
3861 #if defined(SMP)
3862 atomic_clear_int(&pmap->pm_active, mycpu->gd_cpumask);
3863 #else
3864 pmap->pm_active &= ~1;
3865 #endif
3868 crit_exit();
3871 #ifdef SMP
3874 * Called when switching to a locked pmap
3876 void
3877 pmap_interlock_wait(struct vmspace *vm)
3879 struct pmap *pmap = &vm->vm_pmap;
3881 if (pmap->pm_active & CPUMASK_LOCK) {
3882 while (pmap->pm_active & CPUMASK_LOCK) {
3883 cpu_pause();
3884 cpu_ccfence();
3885 lwkt_process_ipiq();
3890 #endif
3892 vm_offset_t
3893 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3896 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3897 return addr;
3900 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3901 return addr;
3905 * Used by kmalloc/kfree, page already exists at va
3907 vm_page_t
3908 pmap_kvtom(vm_offset_t va)
3910 return(PHYS_TO_VM_PAGE(*vtopte(va) & PG_FRAME));