2 * Copyright (c) 2006 The DragonFly Project. All rights reserved.
3 * Copyright (c) 1991 Regents of the University of California.
5 * Copyright (c) 1994 John S. Dyson
7 * Copyright (c) 1994 David Greenman
9 * Copyright (c) 2004-2006 Matthew Dillon
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in
20 * the documentation and/or other materials provided with the
22 * 3. Neither the name of The DragonFly Project nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific, prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
29 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
30 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
32 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
34 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
36 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91
40 * $FreeBSD: src/sys/i386/i386/pmap.c,v 1.250.2.18 2002/03/06 22:48:53 silby Exp $
41 * $DragonFly: src/sys/platform/vkernel/platform/pmap.c,v 1.18 2007/02/24 14:25:07 corecode Exp $
44 * NOTE: PMAP_INVAL_ADD: In pc32 this function is called prior to adjusting
45 * the PTE in the page table, because a cpu synchronization might be required.
46 * The actual invalidation is delayed until the following call or flush. In
47 * the VKERNEL build this function is called prior to adjusting the PTE and
48 * invalidates the table synchronously (not delayed), and is not SMP safe
52 #include <sys/types.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
57 #include <sys/vkernel.h>
59 #include <sys/thread.h>
61 #include <sys/vmspace.h>
64 #include <vm/vm_page.h>
65 #include <vm/vm_extern.h>
66 #include <vm/vm_kern.h>
67 #include <vm/vm_object.h>
68 #include <vm/vm_zone.h>
69 #include <vm/vm_pageout.h>
71 #include <machine/md_var.h>
72 #include <machine/pcb.h>
73 #include <machine/pmap_inval.h>
74 #include <machine/globaldata.h>
78 struct pmap kernel_pmap;
80 static struct vm_zone pvzone;
81 static struct vm_object pvzone_obj;
82 static TAILQ_HEAD(,pmap) pmap_list = TAILQ_HEAD_INITIALIZER(pmap_list);
83 static int pv_entry_count;
84 static int pv_entry_max;
85 static int pv_entry_high_water;
86 static int pmap_pagedaemon_waken;
87 static boolean_t pmap_initialized = FALSE;
88 static int protection_codes[8];
90 static void i386_protection_init(void);
91 static void pmap_remove_all(vm_page_t m);
92 static int pmap_release_free_page(struct pmap *pmap, vm_page_t p);
95 #ifndef PMAP_SHPGPERPROC
96 #define PMAP_SHPGPERPROC 200
99 #define pmap_pde(m, v) (&((m)->pm_pdir[(vm_offset_t)(v) >> PDRSHIFT]))
101 #define pte_prot(m, p) \
102 (protection_codes[p & (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)])
108 struct pv_entry *pvinit;
110 for (i = 0; i < vm_page_array_size; i++) {
113 m = &vm_page_array[i];
114 TAILQ_INIT(&m->md.pv_list);
115 m->md.pv_list_count = 0;
118 i = vm_page_array_size;
121 pvinit = (struct pv_entry *)kmem_alloc(&kernel_map, i*sizeof(*pvinit));
122 zbootinit(&pvzone, "PV ENTRY", sizeof(*pvinit), pvinit, i);
123 pmap_initialized = TRUE;
129 int shpgperproc = PMAP_SHPGPERPROC;
131 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
132 pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
133 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
134 pv_entry_high_water = 9 * (pv_entry_max / 10);
135 zinitna(&pvzone, &pvzone_obj, NULL, 0, pv_entry_max, ZONE_INTERRUPT, 1);
139 * Bootstrap the kernel_pmap so it can be used with pmap_enter().
141 * NOTE! pm_pdir for the kernel pmap is offset so VA's translate
142 * directly into PTD indexes (PTA is also offset for the same reason).
143 * This is necessary because, for now, KVA is not mapped at address 0.
145 * Page table pages are not managed like they are in normal pmaps, so
146 * no pteobj is needed.
151 vm_pindex_t i = (vm_offset_t)KernelPTD >> PAGE_SHIFT;
153 kernel_pmap.pm_pdir = KernelPTD - (KvaStart >> SEG_SHIFT);
154 kernel_pmap.pm_pdirpte = KernelPTA[i];
155 kernel_pmap.pm_count = 1;
156 kernel_pmap.pm_active = (cpumask_t)-1;
157 TAILQ_INIT(&kernel_pmap.pm_pvlist);
158 i386_protection_init();
162 * Initialize pmap0/vmspace0 . Since process 0 never enters user mode we
163 * just dummy it up so it works well enough for fork().
165 * In DragonFly, process pmaps may only be used to manipulate user address
166 * space, never kernel address space.
169 pmap_pinit0(struct pmap *pmap)
174 /************************************************************************
175 * Procedures to manage whole physical maps *
176 ************************************************************************
178 * Initialize a preallocated and zeroed pmap structure,
179 * such as one in a vmspace structure.
182 pmap_pinit(struct pmap *pmap)
188 * No need to allocate page table space yet but we do need a valid
189 * page directory table.
191 if (pmap->pm_pdir == NULL) {
193 (pd_entry_t *)kmem_alloc_pageable(&kernel_map, PAGE_SIZE);
197 * allocate object for the pte array and page directory
199 npages = VPTE_PAGETABLE_SIZE +
200 (VM_MAX_USER_ADDRESS / PAGE_SIZE) * sizeof(vpte_t);
201 npages = (npages + PAGE_MASK) / PAGE_SIZE;
203 if (pmap->pm_pteobj == NULL)
204 pmap->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, npages);
205 pmap->pm_pdindex = npages - 1;
208 * allocate the page directory page
210 ptdpg = vm_page_grab(pmap->pm_pteobj, pmap->pm_pdindex,
211 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
213 ptdpg->wire_count = 1;
214 ++vmstats.v_wire_count;
216 /* not usually mapped */
217 vm_page_flag_clear(ptdpg, PG_MAPPED | PG_BUSY);
218 ptdpg->valid = VM_PAGE_BITS_ALL;
220 pmap_kenter((vm_offset_t)pmap->pm_pdir, VM_PAGE_TO_PHYS(ptdpg));
221 pmap->pm_pdirpte = KernelPTA[(vm_offset_t)pmap->pm_pdir >> PAGE_SHIFT];
222 if ((ptdpg->flags & PG_ZERO) == 0)
223 bzero(pmap->pm_pdir, PAGE_SIZE);
227 pmap->pm_ptphint = NULL;
228 TAILQ_INIT(&pmap->pm_pvlist);
229 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
233 * Wire in kernel global address entries. To avoid a race condition
234 * between pmap initialization and pmap_growkernel, this procedure
235 * adds the pmap to the master list (which growkernel scans to update),
236 * then copies the template.
238 * In a virtual kernel there are no kernel global address entries.
241 pmap_pinit2(struct pmap *pmap)
244 TAILQ_INSERT_TAIL(&pmap_list, pmap, pm_pmnode);
249 * Release all resources held by the given physical map.
251 * Should only be called if the map contains no valid mappings.
253 static int pmap_release_callback(struct vm_page *p, void *data);
256 pmap_release(struct pmap *pmap)
258 struct mdglobaldata *gd = mdcpu;
259 vm_object_t object = pmap->pm_pteobj;
260 struct rb_vm_page_scan_info info;
262 KKASSERT(pmap != &kernel_pmap);
264 #if defined(DIAGNOSTIC)
265 if (object->ref_count != 1)
266 panic("pmap_release: pteobj reference count != 1");
269 #error "Must write code to clear PTxpdir cache across all CPUs"
272 * Once we destroy the page table, the mapping becomes invalid.
273 * Rather then waste time doing a madvise
275 if (pmap->pm_pdir == gd->gd_PT1pdir) {
276 gd->gd_PT1pdir = NULL;
278 /* madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL); */
280 if (pmap->pm_pdir == gd->gd_PT2pdir) {
281 gd->gd_PT2pdir = NULL;
283 /* madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL); */
287 info.object = object;
289 TAILQ_REMOVE(&pmap_list, pmap, pm_pmnode);
296 info.limit = object->generation;
298 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
299 pmap_release_callback, &info);
300 if (info.error == 0 && info.mpte) {
301 if (!pmap_release_free_page(pmap, info.mpte))
305 } while (info.error);
308 * Leave the KVA reservation for pm_pdir cached for later reuse.
310 pmap->pm_pdirpte = 0;
314 pmap_release_callback(struct vm_page *p, void *data)
316 struct rb_vm_page_scan_info *info = data;
318 if (p->pindex == info->pmap->pm_pdindex) {
322 if (!pmap_release_free_page(info->pmap, p)) {
326 if (info->object->generation != info->limit) {
334 * Retire the given physical map from service. Should only be called if
335 * the map contains no valid mappings.
338 pmap_destroy(pmap_t pmap)
345 count = --pmap->pm_count;
348 panic("destroying a pmap is not yet implemented");
353 * Add a reference to the specified pmap.
356 pmap_reference(pmap_t pmap)
363 /************************************************************************
364 * VMSPACE MANAGEMENT *
365 ************************************************************************
367 * The VMSPACE management we do in our virtual kernel must be reflected
368 * in the real kernel. This is accomplished by making vmspace system
369 * calls to the real kernel.
372 cpu_vmspace_alloc(struct vmspace *vm)
377 #define LAST_EXTENT (VM_MAX_USER_ADDRESS - 0x80000000)
379 if (vmspace_create(&vm->vm_pmap, 0, NULL) < 0)
380 panic("vmspace_create() failed");
382 rp = vmspace_mmap(&vm->vm_pmap, (void *)0x00000000, 0x40000000,
383 PROT_READ|PROT_WRITE,
384 MAP_FILE|MAP_SHARED|MAP_VPAGETABLE|MAP_FIXED,
386 if (rp == MAP_FAILED)
387 panic("vmspace_mmap: failed1");
388 vmspace_mcontrol(&vm->vm_pmap, (void *)0x00000000, 0x40000000,
390 rp = vmspace_mmap(&vm->vm_pmap, (void *)0x40000000, 0x40000000,
391 PROT_READ|PROT_WRITE,
392 MAP_FILE|MAP_SHARED|MAP_VPAGETABLE|MAP_FIXED,
393 MemImageFd, 0x40000000);
394 if (rp == MAP_FAILED)
395 panic("vmspace_mmap: failed2");
396 vmspace_mcontrol(&vm->vm_pmap, (void *)0x40000000, 0x40000000,
398 rp = vmspace_mmap(&vm->vm_pmap, (void *)0x80000000, LAST_EXTENT,
399 PROT_READ|PROT_WRITE,
400 MAP_FILE|MAP_SHARED|MAP_VPAGETABLE|MAP_FIXED,
401 MemImageFd, 0x80000000);
402 vmspace_mcontrol(&vm->vm_pmap, (void *)0x80000000, LAST_EXTENT,
404 if (rp == MAP_FAILED)
405 panic("vmspace_mmap: failed3");
407 r = vmspace_mcontrol(&vm->vm_pmap, (void *)0x00000000, 0x40000000,
408 MADV_SETMAP, vmspace_pmap(vm)->pm_pdirpte);
410 panic("vmspace_mcontrol: failed1");
411 r = vmspace_mcontrol(&vm->vm_pmap, (void *)0x40000000, 0x40000000,
412 MADV_SETMAP, vmspace_pmap(vm)->pm_pdirpte);
414 panic("vmspace_mcontrol: failed2");
415 r = vmspace_mcontrol(&vm->vm_pmap, (void *)0x80000000, LAST_EXTENT,
416 MADV_SETMAP, vmspace_pmap(vm)->pm_pdirpte);
418 panic("vmspace_mcontrol: failed3");
422 cpu_vmspace_free(struct vmspace *vm)
424 if (vmspace_destroy(&vm->vm_pmap) < 0)
425 panic("vmspace_destroy() failed");
428 /************************************************************************
429 * Procedures which operate directly on the kernel PMAP *
430 ************************************************************************/
433 * This maps the requested page table and gives us access to it.
436 get_ptbase(struct pmap *pmap, vm_offset_t va)
438 struct mdglobaldata *gd = mdcpu;
440 if (pmap == &kernel_pmap) {
441 KKASSERT(va >= KvaStart && va < KvaEnd);
442 return(KernelPTA + (va >> PAGE_SHIFT));
443 } else if (pmap->pm_pdir == gd->gd_PT1pdir) {
444 return(gd->gd_PT1map + (va >> PAGE_SHIFT));
445 } else if (pmap->pm_pdir == gd->gd_PT2pdir) {
446 return(gd->gd_PT2map + (va >> PAGE_SHIFT));
450 * Otherwise choose one or the other and map the page table
451 * in the KVA space reserved for it.
453 KKASSERT(gd->mi.gd_intr_nesting_level == 0 &&
454 (gd->mi.gd_curthread->td_flags & TDF_INTTHREAD) == 0);
456 if ((gd->gd_PTflip = 1 - gd->gd_PTflip) == 0) {
457 gd->gd_PT1pdir = pmap->pm_pdir;
458 *gd->gd_PT1pde = pmap->pm_pdirpte;
459 madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL);
460 return(gd->gd_PT1map + (va >> PAGE_SHIFT));
462 gd->gd_PT2pdir = pmap->pm_pdir;
463 *gd->gd_PT2pde = pmap->pm_pdirpte;
464 madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL);
465 return(gd->gd_PT2map + (va >> PAGE_SHIFT));
470 get_ptbase1(struct pmap *pmap, vm_offset_t va)
472 struct mdglobaldata *gd = mdcpu;
474 if (pmap == &kernel_pmap) {
475 KKASSERT(va >= KvaStart && va < KvaEnd);
476 return(KernelPTA + (va >> PAGE_SHIFT));
477 } else if (pmap->pm_pdir == gd->gd_PT1pdir) {
478 return(gd->gd_PT1map + (va >> PAGE_SHIFT));
480 KKASSERT(gd->mi.gd_intr_nesting_level == 0 &&
481 (gd->mi.gd_curthread->td_flags & TDF_INTTHREAD) == 0);
482 gd->gd_PT1pdir = pmap->pm_pdir;
483 *gd->gd_PT1pde = pmap->pm_pdirpte;
484 madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL);
485 return(gd->gd_PT1map + (va >> PAGE_SHIFT));
489 get_ptbase2(struct pmap *pmap, vm_offset_t va)
491 struct mdglobaldata *gd = mdcpu;
493 if (pmap == &kernel_pmap) {
494 KKASSERT(va >= KvaStart && va < KvaEnd);
495 return(KernelPTA + (va >> PAGE_SHIFT));
496 } else if (pmap->pm_pdir == gd->gd_PT2pdir) {
497 return(gd->gd_PT2map + (va >> PAGE_SHIFT));
499 KKASSERT(gd->mi.gd_intr_nesting_level == 0 &&
500 (gd->mi.gd_curthread->td_flags & TDF_INTTHREAD) == 0);
501 gd->gd_PT2pdir = pmap->pm_pdir;
502 *gd->gd_PT2pde = pmap->pm_pdirpte;
503 madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL);
504 return(gd->gd_PT2map + (va >> PAGE_SHIFT));
508 * When removing a page directory the related VA range in the self-mapping
509 * of the page table must be invalidated.
512 inval_ptbase_pagedir(pmap_t pmap, vm_pindex_t pindex)
514 struct mdglobaldata *gd = mdcpu;
518 #error "Must inval self-mappings in all gd's"
520 if (pmap == &kernel_pmap) {
521 va = (vm_offset_t)KernelPTA + (pindex << PAGE_SHIFT);
522 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
525 * XXX this should not strictly be needed because the page
526 * dir should alread be invalidated. test and remove
528 va = (vm_offset_t)pindex << PAGE_SHIFT;
529 vmspace_mcontrol(pmap, (void *)va, SEG_SIZE, MADV_INVAL, 0);
531 if (pmap->pm_pdir == gd->gd_PT1pdir) {
532 va = (vm_offset_t)gd->gd_PT1map + (pindex << PAGE_SHIFT);
533 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
535 if (pmap->pm_pdir == gd->gd_PT2pdir) {
536 va = (vm_offset_t)gd->gd_PT2map + (pindex << PAGE_SHIFT);
537 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
542 * Return a pointer to the page table entry for the specified va in the
543 * specified pmap. NULL is returned if there is no valid page table page
546 static __inline vpte_t *
547 pmap_pte(struct pmap *pmap, vm_offset_t va)
551 ptep = &pmap->pm_pdir[va >> SEG_SHIFT];
555 return (get_ptbase(pmap, va));
561 * Enter a mapping into kernel_pmap. Mappings created in this fashion
565 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
570 pmap_inval_info info;
573 KKASSERT(va >= KvaStart && va < KvaEnd);
574 npte = (vpte_t)pa | VPTE_R | VPTE_W | VPTE_V;
575 ptep = KernelPTA + (va >> PAGE_SHIFT);
576 if (*ptep & VPTE_V) {
578 pmap_inval_init(&info);
579 pmap_inval_add(&info, &kernel_pmap, va);
583 pmap_inval_flush(&info);
585 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
593 pmap_kenter_sync(vm_offset_t va)
595 pmap_inval_info info;
597 pmap_inval_init(&info);
598 pmap_inval_add(&info, &kernel_pmap, va);
599 pmap_inval_flush(&info);
603 pmap_kenter_sync_quick(vm_offset_t va)
605 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
609 * XXX these need to be recoded. They are not used in any critical path.
612 pmap_kmodify_rw(vm_offset_t va)
614 *pmap_kpte(va) |= VPTE_R | VPTE_W;
615 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
619 pmap_kmodify_nc(vm_offset_t va)
622 *pmap_kpte(va) |= VPTE_N;
623 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
628 * Map a contiguous range of physical memory to a KVM
631 pmap_map(vm_offset_t virt, vm_paddr_t start, vm_paddr_t end, int prot)
633 while (start < end) {
634 pmap_kenter(virt, start);
642 pmap_kpte(vm_offset_t va)
646 KKASSERT(va >= KvaStart && va < KvaEnd);
647 ptep = KernelPTA + (va >> PAGE_SHIFT);
652 * Enter a mapping into kernel_pmap without any SMP interactions.
654 * Mappings created in this fashion are not managed.
657 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
662 KKASSERT(va >= KvaStart && va < KvaEnd);
664 npte = (vpte_t)pa | VPTE_R | VPTE_W | VPTE_V;
665 ptep = KernelPTA + (va >> PAGE_SHIFT);
666 if (*ptep & VPTE_V) {
668 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
675 * Make a temporary mapping for a physical address. This is only intended
676 * to be used for panic dumps.
679 pmap_kenter_temporary(vm_paddr_t pa, int i)
681 pmap_kenter(crashdumpmap + (i * PAGE_SIZE), pa);
682 return ((void *)crashdumpmap);
686 * Remove an unmanaged mapping created with pmap_kenter*().
689 pmap_kremove(vm_offset_t va)
693 pmap_inval_info info;
696 KKASSERT(va >= KvaStart && va < KvaEnd);
698 ptep = KernelPTA + (va >> PAGE_SHIFT);
699 if (*ptep & VPTE_V) {
701 pmap_inval_init(&info);
702 pmap_inval_add(&info, &kernel_pmap, va);
706 pmap_inval_flush(&info);
708 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
717 * Remove an unmanaged mapping created with pmap_kenter*() without
718 * going through any SMP interactions.
721 pmap_kremove_quick(vm_offset_t va)
725 KKASSERT(va >= KvaStart && va < KvaEnd);
727 ptep = KernelPTA + (va >> PAGE_SHIFT);
728 if (*ptep & VPTE_V) {
730 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
737 * Extract the physical address from the kernel_pmap that is associated
738 * with the specified virtual address.
741 pmap_kextract(vm_offset_t va)
746 KKASSERT(va >= KvaStart && va < KvaEnd);
748 ptep = KernelPTA + (va >> PAGE_SHIFT);
749 pa = (vm_paddr_t)(*ptep & VPTE_FRAME) | (va & PAGE_MASK);
754 * Map a set of unmanaged VM pages into KVM.
757 pmap_qenter(vm_offset_t va, struct vm_page **m, int count)
759 KKASSERT(va >= KvaStart && va + count * PAGE_SIZE < KvaEnd);
763 ptep = KernelPTA + (va >> PAGE_SHIFT);
765 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
766 *ptep = (vpte_t)(*m)->phys_addr | VPTE_R | VPTE_W | VPTE_V;
778 * Map a set of VM pages to kernel virtual memory. If a mapping changes
779 * clear the supplied mask. The caller handles any SMP interactions.
780 * The mask is used to provide the caller with hints on what SMP interactions
784 pmap_qenter2(vm_offset_t va, struct vm_page **m, int count, cpumask_t *mask)
786 cpumask_t cmask = mycpu->gd_cpumask;
788 KKASSERT(va >= KvaStart && va + count * PAGE_SIZE < KvaEnd);
793 ptep = KernelPTA + (va >> PAGE_SHIFT);
794 npte = (vpte_t)(*m)->phys_addr | VPTE_R | VPTE_W | VPTE_V;
798 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
799 } else if ((*mask & cmask) == 0) {
800 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
810 * Undo the effects of pmap_qenter*().
813 pmap_qremove(vm_offset_t va, int count)
815 KKASSERT(va >= KvaStart && va + count * PAGE_SIZE < KvaEnd);
819 ptep = KernelPTA + (va >> PAGE_SHIFT);
821 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
832 /************************************************************************
833 * Misc support glue called by machine independant code *
834 ************************************************************************
836 * These routines are called by machine independant code to operate on
837 * certain machine-dependant aspects of processes, threads, and pmaps.
841 * Initialize MD portions of the thread structure.
844 pmap_init_thread(thread_t td)
846 /* enforce pcb placement */
847 td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
848 td->td_savefpu = &td->td_pcb->pcb_save;
849 td->td_sp = (char *)td->td_pcb - 16;
853 * Initialize MD portions of a process structure. XXX this aint MD
856 pmap_init_proc(struct proc *p, struct thread *td)
858 struct lwp *lp = ONLY_LWP_IN_PROC(p);
860 p->p_addr = (void *)td->td_kstack;
864 td->td_switch = cpu_heavy_switch;
866 KKASSERT(td->td_mpcount == 1);
868 bzero(p->p_addr, sizeof(*p->p_addr));
872 * Destroy the UPAGES for a process that has exited and disassociate
873 * the process from its thread.
876 pmap_dispose_proc(struct proc *p)
878 KASSERT(p->p_lock == 0, ("attempt to dispose referenced proc! %p", p));
884 * We pre-allocate all page table pages for kernel virtual memory so
885 * this routine will only be called if KVM has been exhausted.
888 pmap_growkernel(vm_offset_t addr)
890 addr = (addr + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
892 if (addr > virtual_end - SEG_SIZE)
893 panic("KVM exhausted");
894 kernel_vm_end = addr;
898 * The modification bit is not tracked for any pages in this range. XXX
899 * such pages in this maps should always use pmap_k*() functions and not
902 * XXX User and kernel address spaces are independant for virtual kernels,
903 * this function only applies to the kernel pmap.
906 pmap_track_modified(pmap_t pmap, vm_offset_t va)
908 if (pmap != &kernel_pmap)
910 if ((va < clean_sva) || (va >= clean_eva))
916 /************************************************************************
917 * Procedures supporting managed page table pages *
918 ************************************************************************
920 * These procedures are used to track managed page table pages. These pages
921 * use the page table page's vm_page_t to track PTEs in the page. The
922 * page table pages themselves are arranged in a VM object, pmap->pm_pteobj.
924 * This allows the system to throw away page table pages for user processes
925 * at will and reinstantiate them on demand.
929 * This routine works like vm_page_lookup() but also blocks as long as the
930 * page is busy. This routine does not busy the page it returns.
932 * Unless the caller is managing objects whos pages are in a known state,
933 * the call should be made with a critical section held so the page's object
934 * association remains valid on return.
937 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
942 m = vm_page_lookup(object, pindex);
943 if (m && vm_page_sleep_busy(m, FALSE, "pplookp"))
949 * This routine unholds page table pages, and if the hold count
950 * drops to zero, then it decrements the wire count.
953 _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, pmap_inval_info_t info)
955 pmap_inval_flush(info);
956 while (vm_page_sleep_busy(m, FALSE, "pmuwpt"))
959 if (m->hold_count == 0) {
961 * unmap the page table page
963 pmap->pm_pdir[m->pindex] = 0;
964 --pmap->pm_stats.resident_count;
965 inval_ptbase_pagedir(pmap, m->pindex);
967 if (pmap->pm_ptphint == m)
968 pmap->pm_ptphint = NULL;
971 * If the page is finally unwired, simply free it.
974 if (m->wire_count == 0) {
977 vm_page_free_zero(m);
978 --vmstats.v_wire_count;
986 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, pmap_inval_info_t info)
989 if (m->hold_count == 0)
990 return _pmap_unwire_pte_hold(pmap, m, info);
996 * After removing a page table entry, this routine is used to
997 * conditionally free the page, and manage the hold/wire counts.
1000 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte,
1001 pmap_inval_info_t info)
1007 * page table pages in the kernel_pmap are not managed.
1009 if (pmap == &kernel_pmap)
1011 ptepindex = (va >> PDRSHIFT);
1012 if (pmap->pm_ptphint &&
1013 (pmap->pm_ptphint->pindex == ptepindex)) {
1014 mpte = pmap->pm_ptphint;
1016 pmap_inval_flush(info);
1017 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1018 pmap->pm_ptphint = mpte;
1021 return pmap_unwire_pte_hold(pmap, mpte, info);
1025 * Attempt to release and free an vm_page in a pmap. Returns 1 on success,
1026 * 0 on failure (if the procedure had to sleep).
1029 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1031 vpte_t *pde = pmap->pm_pdir;
1033 * This code optimizes the case of freeing non-busy
1034 * page-table pages. Those pages are zero now, and
1035 * might as well be placed directly into the zero queue.
1037 if (vm_page_sleep_busy(p, FALSE, "pmaprl"))
1041 pmap->pm_stats.resident_count--;
1043 if (p->hold_count) {
1044 panic("pmap_release: freeing held page table page");
1047 * Page directory pages need to have the kernel stuff cleared, so
1048 * they can go into the zero queue also.
1050 * In virtual kernels there is no 'kernel stuff'. For the moment
1051 * I just make sure the whole thing has been zero'd even though
1052 * it should already be completely zero'd.
1054 * pmaps for vkernels do not self-map because they do not share
1055 * their address space with the vkernel. Clearing of pde[] thus
1056 * only applies to page table pages and not to the page directory
1059 if (p->pindex == pmap->pm_pdindex) {
1060 bzero(pde, VPTE_PAGETABLE_SIZE);
1061 pmap_kremove((vm_offset_t)pmap->pm_pdir);
1067 * Clear the matching hint
1069 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1070 pmap->pm_ptphint = NULL;
1073 * And throw the page away. The page is completely zero'd out so
1074 * optimize the free call.
1077 vmstats.v_wire_count--;
1078 vm_page_free_zero(p);
1083 * This routine is called if the page table page is not mapped in the page
1086 * The routine is broken up into two parts for readability.
1089 _pmap_allocpte(pmap_t pmap, unsigned ptepindex)
1095 * Find or fabricate a new pagetable page
1097 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1098 VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1100 KASSERT(m->queue == PQ_NONE,
1101 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1103 if (m->wire_count == 0)
1104 vmstats.v_wire_count++;
1108 * Increment the hold count for the page table page
1109 * (denoting a new mapping.)
1114 * Map the pagetable page into the process address space, if
1115 * it isn't already there.
1117 pmap->pm_stats.resident_count++;
1119 ptepa = VM_PAGE_TO_PHYS(m);
1120 pmap->pm_pdir[ptepindex] = (vpte_t)ptepa | VPTE_R | VPTE_W | VPTE_V |
1124 * We are likely about to access this page table page, so set the
1125 * page table hint to reduce overhead.
1127 pmap->pm_ptphint = m;
1130 * Try to use the new mapping, but if we cannot, then
1131 * do it with the routine that maps the page explicitly.
1133 if ((m->flags & PG_ZERO) == 0)
1134 pmap_zero_page(ptepa);
1136 m->valid = VM_PAGE_BITS_ALL;
1137 vm_page_flag_clear(m, PG_ZERO);
1138 vm_page_flag_set(m, PG_MAPPED);
1145 * Determine the page table page required to access the VA in the pmap
1146 * and allocate it if necessary. Return a held vm_page_t for the page.
1148 * Only used with user pmaps.
1151 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1158 * Calculate pagetable page index
1160 ptepindex = va >> PDRSHIFT;
1163 * Get the page directory entry
1165 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1168 * This supports switching from a 4MB page to a
1171 if (ptepa & VPTE_PS) {
1172 pmap->pm_pdir[ptepindex] = 0;
1179 * If the page table page is mapped, we just increment the
1180 * hold count, and activate it.
1184 * In order to get the page table page, try the
1187 if (pmap->pm_ptphint &&
1188 (pmap->pm_ptphint->pindex == ptepindex)) {
1189 m = pmap->pm_ptphint;
1191 m = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1192 pmap->pm_ptphint = m;
1198 * Here if the pte page isn't mapped, or if it has been deallocated.
1200 return _pmap_allocpte(pmap, ptepindex);
1203 /************************************************************************
1204 * Managed pages in pmaps *
1205 ************************************************************************
1207 * All pages entered into user pmaps and some pages entered into the kernel
1208 * pmap are managed, meaning that pmap_protect() and other related management
1209 * functions work on these pages.
1213 * free the pv_entry back to the free list. This function may be
1214 * called from an interrupt.
1216 static __inline void
1217 free_pv_entry(pv_entry_t pv)
1224 * get a new pv_entry, allocating a block from the system
1225 * when needed. This function may be called from an interrupt.
1231 if (pv_entry_high_water &&
1232 (pv_entry_count > pv_entry_high_water) &&
1233 (pmap_pagedaemon_waken == 0)) {
1234 pmap_pagedaemon_waken = 1;
1235 wakeup (&vm_pages_needed);
1237 return zalloc(&pvzone);
1241 * This routine is very drastic, but can save the system
1249 static int warningdone=0;
1251 if (pmap_pagedaemon_waken == 0)
1254 if (warningdone < 5) {
1255 kprintf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1259 for(i = 0; i < vm_page_array_size; i++) {
1260 m = &vm_page_array[i];
1261 if (m->wire_count || m->hold_count || m->busy ||
1262 (m->flags & PG_BUSY))
1266 pmap_pagedaemon_waken = 0;
1270 * If it is the first entry on the list, it is actually
1271 * in the header and we must copy the following entry up
1272 * to the header. Otherwise we must search the list for
1273 * the entry. In either case we free the now unused entry.
1276 pmap_remove_entry(struct pmap *pmap, vm_page_t m,
1277 vm_offset_t va, pmap_inval_info_t info)
1283 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1284 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1285 if (pmap == pv->pv_pmap && va == pv->pv_va)
1289 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1290 if (va == pv->pv_va)
1296 * Note that pv_ptem is NULL if the page table page itself is not
1297 * managed, even if the page being removed IS managed.
1301 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1302 m->md.pv_list_count--;
1303 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
1304 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1305 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1306 ++pmap->pm_generation;
1307 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem, info);
1315 * Create a pv entry for page at pa for (pmap, va). If the page table page
1316 * holding the VA is managed, mpte will be non-NULL.
1319 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
1324 pv = get_pv_entry();
1329 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1330 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1331 m->md.pv_list_count++;
1337 * pmap_remove_pte: do the things to unmap a page in a process
1340 pmap_remove_pte(struct pmap *pmap, vpte_t *ptq, vm_offset_t va,
1341 pmap_inval_info_t info)
1346 oldpte = loadandclear(ptq);
1347 pmap_inval_add(info, pmap, va); /* See NOTE: PMAP_INVAL_ADD */
1348 if (oldpte & VPTE_WIRED)
1349 --pmap->pm_stats.wired_count;
1350 KKASSERT(pmap->pm_stats.wired_count >= 0);
1354 * Machines that don't support invlpg, also don't support
1355 * VPTE_G. XXX VPTE_G is disabled for SMP so don't worry about
1358 if (oldpte & VPTE_G)
1359 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
1361 pmap->pm_stats.resident_count -= 1;
1362 if (oldpte & VPTE_MANAGED) {
1363 m = PHYS_TO_VM_PAGE(oldpte);
1364 if (oldpte & VPTE_M) {
1365 #if defined(PMAP_DIAGNOSTIC)
1366 if (pmap_nw_modified((pt_entry_t) oldpte)) {
1368 "pmap_remove: modified page not writable: va: 0x%x, pte: 0x%x\n",
1372 if (pmap_track_modified(pmap, va))
1375 if (oldpte & VPTE_A)
1376 vm_page_flag_set(m, PG_REFERENCED);
1377 return pmap_remove_entry(pmap, m, va, info);
1379 return pmap_unuse_pt(pmap, va, NULL, info);
1388 * Remove a single page from a process address space.
1390 * This function may not be called from an interrupt if the pmap is
1394 pmap_remove_page(struct pmap *pmap, vm_offset_t va, pmap_inval_info_t info)
1399 * if there is no pte for this address, just skip it!!! Otherwise
1400 * get a local va for mappings for this pmap and remove the entry.
1402 if (*pmap_pde(pmap, va) != 0) {
1403 ptq = get_ptbase(pmap, va);
1405 pmap_remove_pte(pmap, ptq, va, info);
1413 * Remove the given range of addresses from the specified map.
1415 * It is assumed that the start and end are properly
1416 * rounded to the page size.
1418 * This function may not be called from an interrupt if the pmap is
1422 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
1426 vm_offset_t ptpaddr;
1427 vm_pindex_t sindex, eindex;
1428 struct pmap_inval_info info;
1433 KKASSERT(pmap->pm_stats.resident_count >= 0);
1434 if (pmap->pm_stats.resident_count == 0)
1437 pmap_inval_init(&info);
1440 * special handling of removing one page. a very
1441 * common operation and easy to short circuit some
1444 if (((sva + PAGE_SIZE) == eva) &&
1445 ((pmap->pm_pdir[(sva >> PDRSHIFT)] & VPTE_PS) == 0)) {
1446 pmap_remove_page(pmap, sva, &info);
1447 pmap_inval_flush(&info);
1452 * Get a local virtual address for the mappings that are being
1455 * XXX this is really messy because the kernel pmap is not relative
1458 sindex = (sva >> PAGE_SHIFT);
1459 eindex = (eva >> PAGE_SHIFT);
1461 for (; sindex < eindex; sindex = pdnxt) {
1465 * Calculate index for next page table.
1467 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1468 if (pmap->pm_stats.resident_count == 0)
1471 pdirindex = sindex / NPDEPG;
1472 if (((ptpaddr = pmap->pm_pdir[pdirindex]) & VPTE_PS) != 0) {
1473 pmap->pm_pdir[pdirindex] = 0;
1474 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1475 inval_ptbase_pagedir(pmap, pdirindex);
1480 * Weed out invalid mappings. Note: we assume that the page
1481 * directory table is always allocated, and in kernel virtual.
1487 * Limit our scan to either the end of the va represented
1488 * by the current page table page, or to the end of the
1489 * range being removed.
1495 * NOTE: pmap_remove_pte() can block.
1497 for (; sindex != pdnxt; sindex++) {
1500 ptbase = get_ptbase(pmap, sindex << PAGE_SHIFT);
1503 va = i386_ptob(sindex);
1504 if (pmap_remove_pte(pmap, ptbase, va, &info))
1508 pmap_inval_flush(&info);
1514 * Removes this physical page from all physical maps in which it resides.
1515 * Reflects back modify bits to the pager.
1517 * This routine may not be called from an interrupt.
1520 pmap_remove_all(vm_page_t m)
1522 struct pmap_inval_info info;
1526 #if defined(PMAP_DIAGNOSTIC)
1528 * XXX this makes pmap_page_protect(NONE) illegal for non-managed
1531 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
1532 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%08llx", (long long)VM_PAGE_TO_PHYS(m));
1536 pmap_inval_init(&info);
1538 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
1539 pv->pv_pmap->pm_stats.resident_count--;
1541 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
1542 KKASSERT(pte != NULL);
1544 tpte = loadandclear(pte);
1545 /* See NOTE: PMAP_INVAL_ADD */
1546 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
1547 if (tpte & VPTE_WIRED)
1548 --pv->pv_pmap->pm_stats.wired_count;
1549 KKASSERT(pv->pv_pmap->pm_stats.wired_count >= 0);
1552 vm_page_flag_set(m, PG_REFERENCED);
1555 * Update the vm_page_t clean and reference bits.
1557 if (tpte & VPTE_M) {
1558 #if defined(PMAP_DIAGNOSTIC)
1559 if (pmap_nw_modified((pt_entry_t) tpte)) {
1561 "pmap_remove_all: modified page not writable: va: 0x%x, pte: 0x%x\n",
1565 if (pmap_track_modified(pv->pv_pmap, pv->pv_va))
1568 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1569 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
1570 ++pv->pv_pmap->pm_generation;
1571 m->md.pv_list_count--;
1572 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem, &info);
1576 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1578 pmap_inval_flush(&info);
1584 * Set the physical protection on the specified range of this map
1587 * This function may not be called from an interrupt if the map is
1588 * not the kernel_pmap.
1591 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
1594 vm_offset_t pdnxt, ptpaddr;
1595 vm_pindex_t sindex, eindex;
1597 pmap_inval_info info;
1602 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1603 pmap_remove(pmap, sva, eva);
1607 if (prot & VM_PROT_WRITE)
1610 pmap_inval_init(&info);
1612 ptbase = get_ptbase(pmap, sva);
1614 sindex = (sva >> PAGE_SHIFT);
1615 eindex = (eva >> PAGE_SHIFT);
1618 for (; sindex < eindex; sindex = pdnxt) {
1622 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1624 pdirindex = sindex / NPDEPG;
1625 if (((ptpaddr = pmap->pm_pdir[pdirindex]) & VPTE_PS) != 0) {
1626 pmap->pm_pdir[pdirindex] &= ~(VPTE_M|VPTE_W);
1627 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1628 inval_ptbase_pagedir(pmap, pdirindex);
1633 * Weed out invalid mappings. Note: we assume that the page
1634 * directory table is always allocated, and in kernel virtual.
1639 if (pdnxt > eindex) {
1643 for (; sindex != pdnxt; sindex++) {
1647 pbits = ptbase[sindex - sbase];
1649 if (pbits & VPTE_MANAGED) {
1651 if (pbits & VPTE_A) {
1652 m = PHYS_TO_VM_PAGE(pbits);
1653 vm_page_flag_set(m, PG_REFERENCED);
1656 if (pbits & VPTE_M) {
1657 if (pmap_track_modified(pmap, i386_ptob(sindex))) {
1659 m = PHYS_TO_VM_PAGE(pbits);
1668 if (pbits != ptbase[sindex - sbase]) {
1669 ptbase[sindex - sbase] = pbits;
1670 /* See NOTE: PMAP_INVAL_ADD */
1671 pmap_inval_add(&info, pmap, i386_ptob(sindex));
1675 pmap_inval_flush(&info);
1679 * Enter a managed page into a pmap. If the page is not wired related pmap
1680 * data can be destroyed at any time for later demand-operation.
1682 * Insert the vm_page (m) at virtual address (v) in (pmap), with the
1683 * specified protection, and wire the mapping if requested.
1685 * NOTE: This routine may not lazy-evaluate or lose information. The
1686 * page must actually be inserted into the given map NOW.
1688 * NOTE: When entering a page at a KVA address, the pmap must be the
1692 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1698 vm_offset_t origpte, newpte;
1700 pmap_inval_info info;
1708 * Get the page table page. The kernel_pmap's page table pages
1709 * are preallocated and have no associated vm_page_t.
1711 if (pmap == &kernel_pmap)
1714 mpte = pmap_allocpte(pmap, va);
1716 pmap_inval_init(&info);
1717 pte = pmap_pte(pmap, va);
1720 * Page Directory table entry not valid, we need a new PT page
1721 * and pmap_allocpte() didn't give us one. Oops!
1724 panic("pmap_enter: invalid page directory pmap=%p, va=0x%p\n",
1728 pa = VM_PAGE_TO_PHYS(m) & VPTE_FRAME;
1730 opa = origpte & VPTE_FRAME;
1732 printf("pmap_enter: pmap %p va %08x pa %08x PDE %08x origpte %08x\n", pmap, va, (int)pa, pmap->pm_pdir[va >> SEG_SHIFT], origpte);
1735 if (origpte & VPTE_PS)
1736 panic("pmap_enter: attempted pmap_enter on 4MB page");
1739 * Mapping has not changed, must be protection or wiring change.
1741 if (origpte && (opa == pa)) {
1743 * Wiring change, just update stats. We don't worry about
1744 * wiring PT pages as they remain resident as long as there
1745 * are valid mappings in them. Hence, if a user page is wired,
1746 * the PT page will be also.
1748 if (wired && ((origpte & VPTE_WIRED) == 0))
1749 ++pmap->pm_stats.wired_count;
1750 else if (!wired && (origpte & VPTE_WIRED))
1751 --pmap->pm_stats.wired_count;
1752 KKASSERT(pmap->pm_stats.wired_count >= 0);
1754 #if defined(PMAP_DIAGNOSTIC)
1755 if (pmap_nw_modified((pt_entry_t) origpte)) {
1757 "pmap_enter: modified page not writable: va: 0x%x, pte: 0x%x\n",
1763 * Remove the extra pte reference. Note that we cannot
1764 * optimize the RO->RW case because we have adjusted the
1765 * wiring count above and may need to adjust the wiring
1772 * We might be turning off write access to the page,
1773 * so we go ahead and sense modify status.
1775 if (origpte & VPTE_MANAGED) {
1776 if ((origpte & VPTE_M) && pmap_track_modified(pmap, va)) {
1778 om = PHYS_TO_VM_PAGE(opa);
1786 * Mapping has changed, invalidate old range and fall through to
1787 * handle validating new mapping.
1791 err = pmap_remove_pte(pmap, pte, va, &info);
1793 panic("pmap_enter: pte vanished, va: 0x%x", va);
1797 * Enter on the PV list if part of our managed memory. Note that we
1798 * raise IPL while manipulating pv_table since pmap_enter can be
1799 * called at interrupt time.
1801 if (pmap_initialized &&
1802 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
1803 pmap_insert_entry(pmap, va, mpte, m);
1808 * Increment counters
1810 pmap->pm_stats.resident_count++;
1812 pmap->pm_stats.wired_count++;
1816 * Now validate mapping with desired protection/wiring.
1818 newpte = (vm_offset_t) (pa | pte_prot(pmap, prot) | VPTE_V);
1821 newpte |= VPTE_WIRED;
1825 * if the mapping or permission bits are different, we need
1826 * to update the pte.
1828 if ((origpte & ~(VPTE_M|VPTE_A)) != newpte) {
1829 *pte = newpte | VPTE_A;
1830 /* See NOTE: PMAP_INVAL_ADD */
1831 pmap_inval_add(&info, pmap, va); /* XXX non-optimal */
1833 pmap_inval_flush(&info);
1837 * This is a quick version of pmap_enter(). It is used only under the
1838 * following conditions:
1840 * (1) The pmap is not the kernel_pmap
1841 * (2) The page is not to be wired into the map
1842 * (3) The page is to mapped read-only in the pmap (initially that is)
1843 * (4) The calling procedure is responsible for flushing the TLB
1844 * (5) The page is always managed
1845 * (6) There is no prior mapping at the VA
1849 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_page_t mpte)
1853 pmap_inval_info info;
1857 KKASSERT(pmap != &kernel_pmap);
1858 pmap_inval_init(&info);
1860 KKASSERT(va >= VM_MIN_USER_ADDRESS && va < VM_MAX_USER_ADDRESS);
1863 * Instantiate the page table page if required
1867 * Calculate pagetable page index
1869 ptepindex = va >> PDRSHIFT;
1870 if (mpte && (mpte->pindex == ptepindex)) {
1875 * Get the page directory entry
1877 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1880 * If the page table page is mapped, we just increment
1881 * the hold count, and activate it.
1884 if (ptepa & VPTE_PS)
1885 panic("pmap_enter_quick: unexpected mapping into 4MB page");
1886 if (pmap->pm_ptphint &&
1887 (pmap->pm_ptphint->pindex == ptepindex)) {
1888 mpte = pmap->pm_ptphint;
1890 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1891 pmap->pm_ptphint = mpte;
1897 mpte = _pmap_allocpte(pmap, ptepindex);
1902 * Ok, now that the page table page has been validated, get the pte.
1903 * If the pte is already mapped undo mpte's hold_count and
1906 pte = pmap_pte(pmap, va);
1909 pmap_unwire_pte_hold(pmap, mpte, &info);
1914 * Enter on the PV list if part of our managed memory. Note that we
1915 * raise IPL while manipulating pv_table since pmap_enter can be
1916 * called at interrupt time.
1918 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0)
1919 pmap_insert_entry(pmap, va, mpte, m);
1922 * Increment counters
1924 pmap->pm_stats.resident_count++;
1926 pa = VM_PAGE_TO_PHYS(m);
1929 * Now validate mapping with RO protection
1931 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
1932 *pte = pa | VPTE_V | VPTE_U;
1934 *pte = pa | VPTE_V | VPTE_U | VPTE_MANAGED;
1940 * Extract the physical address for the translation at the specified
1941 * virtual address in the pmap.
1944 pmap_extract(pmap_t pmap, vm_offset_t va)
1949 if (pmap && (pte = pmap->pm_pdir[va >> SEG_SHIFT]) != 0) {
1950 if (pte & VPTE_PS) {
1951 rtval = pte & ~((vpte_t)(1 << SEG_SHIFT) - 1);
1952 rtval |= va & SEG_MASK;
1954 pte = *get_ptbase(pmap, va);
1955 rtval = (pte & VPTE_FRAME) | (va & PAGE_MASK);
1962 #define MAX_INIT_PT (96)
1965 * This routine preloads the ptes for a given object into the specified pmap.
1966 * This eliminates the blast of soft faults on process startup and
1967 * immediately after an mmap.
1969 static int pmap_object_init_pt_callback(vm_page_t p, void *data);
1972 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
1973 vm_object_t object, vm_pindex_t pindex,
1974 vm_size_t size, int limit)
1976 struct rb_vm_page_scan_info info;
1980 * We can't preinit if read access isn't set or there is no pmap
1983 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
1987 * We can't preinit if the pmap is not the current pmap
1989 if (curproc == NULL || pmap != vmspace_pmap(curproc->p_vmspace))
1992 psize = size >> PAGE_SHIFT;
1994 if ((object->type != OBJT_VNODE) ||
1995 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
1996 (object->resident_page_count > MAX_INIT_PT))) {
2000 if (psize + pindex > object->size) {
2001 if (object->size < pindex)
2003 psize = object->size - pindex;
2010 * Use a red-black scan to traverse the requested range and load
2011 * any valid pages found into the pmap.
2013 * We cannot safely scan the object's memq unless we are in a
2014 * critical section since interrupts can remove pages from objects.
2016 info.start_pindex = pindex;
2017 info.end_pindex = pindex + psize - 1;
2024 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2025 pmap_object_init_pt_callback, &info);
2031 pmap_object_init_pt_callback(vm_page_t p, void *data)
2033 struct rb_vm_page_scan_info *info = data;
2034 vm_pindex_t rel_index;
2036 * don't allow an madvise to blow away our really
2037 * free pages allocating pv entries.
2039 if ((info->limit & MAP_PREFAULT_MADVISE) &&
2040 vmstats.v_free_count < vmstats.v_free_reserved) {
2043 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2044 (p->busy == 0) && (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2045 if ((p->queue - p->pc) == PQ_CACHE)
2046 vm_page_deactivate(p);
2048 rel_index = p->pindex - info->start_pindex;
2049 info->mpte = pmap_enter_quick(info->pmap,
2050 info->addr + i386_ptob(rel_index),
2052 vm_page_flag_set(p, PG_MAPPED);
2059 * pmap_prefault provides a quick way of clustering pagefaults into a
2060 * processes address space. It is a "cousin" of pmap_object_init_pt,
2061 * except it runs at page fault time instead of mmap time.
2065 #define PAGEORDER_SIZE (PFBAK+PFFOR)
2067 static int pmap_prefault_pageorder[] = {
2068 -PAGE_SIZE, PAGE_SIZE,
2069 -2 * PAGE_SIZE, 2 * PAGE_SIZE,
2070 -3 * PAGE_SIZE, 3 * PAGE_SIZE,
2071 -4 * PAGE_SIZE, 4 * PAGE_SIZE
2075 pmap_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry)
2085 * We do not currently prefault mappings that use virtual page
2086 * tables. We do not prefault foreign pmaps.
2088 if (entry->maptype == VM_MAPTYPE_VPAGETABLE)
2090 if (curproc == NULL || (pmap != vmspace_pmap(curproc->p_vmspace)))
2093 object = entry->object.vm_object;
2095 starta = addra - PFBAK * PAGE_SIZE;
2096 if (starta < entry->start)
2097 starta = entry->start;
2098 else if (starta > addra)
2102 * critical section protection is required to maintain the
2103 * page/object association, interrupts can free pages and remove
2104 * them from their objects.
2108 for (i = 0; i < PAGEORDER_SIZE; i++) {
2109 vm_object_t lobject;
2112 addr = addra + pmap_prefault_pageorder[i];
2113 if (addr > addra + (PFFOR * PAGE_SIZE))
2116 if (addr < starta || addr >= entry->end)
2120 * Make sure the page table page already exists
2122 if ((*pmap_pde(pmap, addr)) == NULL)
2126 * Get a pointer to the pte and make sure that no valid page
2129 pte = get_ptbase(pmap, addr);
2134 * Get the page to be mapped
2136 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2139 for (m = vm_page_lookup(lobject, pindex);
2140 (!m && (lobject->type == OBJT_DEFAULT) &&
2141 (lobject->backing_object));
2142 lobject = lobject->backing_object
2144 if (lobject->backing_object_offset & PAGE_MASK)
2146 pindex += (lobject->backing_object_offset >> PAGE_SHIFT);
2147 m = vm_page_lookup(lobject->backing_object, pindex);
2151 * give-up when a page is not in memory
2157 * If everything meets the requirements for pmap_enter_quick(),
2158 * then enter the page.
2161 if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2163 (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2165 if ((m->queue - m->pc) == PQ_CACHE) {
2166 vm_page_deactivate(m);
2169 mpte = pmap_enter_quick(pmap, addr, m, mpte);
2170 vm_page_flag_set(m, PG_MAPPED);
2178 * Routine: pmap_change_wiring
2179 * Function: Change the wiring attribute for a map/virtual-address
2181 * In/out conditions:
2182 * The mapping must already exist in the pmap.
2185 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2192 pte = get_ptbase(pmap, va);
2194 if (wired && (*pte & VPTE_WIRED) == 0)
2195 ++pmap->pm_stats.wired_count;
2196 else if (!wired && (*pte & VPTE_WIRED))
2197 --pmap->pm_stats.wired_count;
2198 KKASSERT(pmap->pm_stats.wired_count >= 0);
2201 * Wiring is not a hardware characteristic so there is no need to
2202 * invalidate TLB. However, in an SMP environment we must use
2203 * a locked bus cycle to update the pte (if we are not using
2204 * the pmap_inval_*() API that is)... it's ok to do this for simple
2208 atomic_set_int(pte, VPTE_WIRED);
2210 atomic_clear_int(pte, VPTE_WIRED);
2214 * Copy the range specified by src_addr/len
2215 * from the source map to the range dst_addr/len
2216 * in the destination map.
2218 * This routine is only advisory and need not do anything.
2221 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2222 vm_size_t len, vm_offset_t src_addr)
2224 pmap_inval_info info;
2226 vm_offset_t end_addr = src_addr + len;
2232 if (dst_addr != src_addr)
2234 if (dst_pmap->pm_pdir == NULL)
2236 if (src_pmap->pm_pdir == NULL)
2239 src_frame = get_ptbase1(src_pmap, src_addr);
2240 dst_frame = get_ptbase2(dst_pmap, src_addr);
2242 pmap_inval_init(&info);
2245 pmap_inval_add(&info, dst_pmap, -1);
2246 pmap_inval_add(&info, src_pmap, -1);
2250 * critical section protection is required to maintain the page/object
2251 * association, interrupts can free pages and remove them from
2255 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
2256 vpte_t *src_pte, *dst_pte;
2257 vm_page_t dstmpte, srcmpte;
2258 vm_offset_t srcptepaddr;
2261 if (addr >= VM_MAX_USER_ADDRESS)
2262 panic("pmap_copy: invalid to pmap_copy page tables\n");
2265 * Don't let optional prefaulting of pages make us go
2266 * way below the low water mark of free pages or way
2267 * above high water mark of used pv entries.
2269 if (vmstats.v_free_count < vmstats.v_free_reserved ||
2270 pv_entry_count > pv_entry_high_water)
2273 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
2274 ptepindex = addr >> PDRSHIFT;
2276 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex];
2277 if (srcptepaddr == 0)
2280 if (srcptepaddr & VPTE_PS) {
2281 if (dst_pmap->pm_pdir[ptepindex] == 0) {
2282 dst_pmap->pm_pdir[ptepindex] = (pd_entry_t) srcptepaddr;
2283 dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
2288 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
2289 if ((srcmpte == NULL) ||
2290 (srcmpte->hold_count == 0) || (srcmpte->flags & PG_BUSY))
2293 if (pdnxt > end_addr)
2296 src_pte = src_frame + ((addr - src_addr) >> PAGE_SHIFT);
2297 dst_pte = dst_frame + ((addr - src_addr) >> PAGE_SHIFT);
2298 while (addr < pdnxt) {
2302 * we only virtual copy managed pages
2304 if ((ptetemp & VPTE_MANAGED) != 0) {
2306 * We have to check after allocpte for the
2307 * pte still being around... allocpte can
2310 dstmpte = pmap_allocpte(dst_pmap, addr);
2311 if ((*dst_pte == 0) && (ptetemp = *src_pte)) {
2313 * Clear the modified and accessed
2314 * (referenced) bits during the copy.
2316 * We do not have to clear the write
2317 * bit to force a fault-on-modify
2318 * because the real kernel's target
2319 * pmap is empty and will fault anyway.
2321 m = PHYS_TO_VM_PAGE(ptetemp);
2322 *dst_pte = ptetemp & ~(VPTE_M | VPTE_A);
2323 dst_pmap->pm_stats.resident_count++;
2324 pmap_insert_entry(dst_pmap, addr,
2327 pmap_unwire_pte_hold(dst_pmap, dstmpte, &info);
2329 if (dstmpte->hold_count >= srcmpte->hold_count)
2338 pmap_inval_flush(&info);
2344 * Zero the specified PA by mapping the page into KVM and clearing its
2347 * This function may be called from an interrupt and no locking is
2351 pmap_zero_page(vm_paddr_t phys)
2353 struct mdglobaldata *gd = mdcpu;
2357 panic("pmap_zero_page: CMAP3 busy");
2358 *gd->gd_CMAP3 = VPTE_V | VPTE_R | VPTE_W | (phys & VPTE_FRAME) | VPTE_A | VPTE_M;
2359 madvise(gd->gd_CADDR3, PAGE_SIZE, MADV_INVAL);
2361 bzero(gd->gd_CADDR3, PAGE_SIZE);
2367 * pmap_page_assertzero:
2369 * Assert that a page is empty, panic if it isn't.
2372 pmap_page_assertzero(vm_paddr_t phys)
2374 struct mdglobaldata *gd = mdcpu;
2379 panic("pmap_zero_page: CMAP3 busy");
2380 *gd->gd_CMAP3 = VPTE_V | VPTE_R | VPTE_W |
2381 (phys & VPTE_FRAME) | VPTE_A | VPTE_M;
2382 madvise(gd->gd_CADDR3, PAGE_SIZE, MADV_INVAL);
2383 for (i = 0; i < PAGE_SIZE; i += 4) {
2384 if (*(int *)((char *)gd->gd_CADDR3 + i) != 0) {
2385 panic("pmap_page_assertzero() @ %p not zero!\n",
2386 (void *)gd->gd_CADDR3);
2396 * Zero part of a physical page by mapping it into memory and clearing
2397 * its contents with bzero.
2399 * off and size may not cover an area beyond a single hardware page.
2402 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
2404 struct mdglobaldata *gd = mdcpu;
2408 panic("pmap_zero_page: CMAP3 busy");
2409 *gd->gd_CMAP3 = VPTE_V | VPTE_R | VPTE_W |
2410 (phys & VPTE_FRAME) | VPTE_A | VPTE_M;
2411 madvise(gd->gd_CADDR3, PAGE_SIZE, MADV_INVAL);
2413 bzero((char *)gd->gd_CADDR3 + off, size);
2421 * Copy the physical page from the source PA to the target PA.
2422 * This function may be called from an interrupt. No locking
2426 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
2428 struct mdglobaldata *gd = mdcpu;
2431 if (*(int *) gd->gd_CMAP1)
2432 panic("pmap_copy_page: CMAP1 busy");
2433 if (*(int *) gd->gd_CMAP2)
2434 panic("pmap_copy_page: CMAP2 busy");
2436 *(int *) gd->gd_CMAP1 = VPTE_V | VPTE_R | (src & PG_FRAME) | VPTE_A;
2437 *(int *) gd->gd_CMAP2 = VPTE_V | VPTE_R | VPTE_W | (dst & VPTE_FRAME) | VPTE_A | VPTE_M;
2439 madvise(gd->gd_CADDR1, PAGE_SIZE, MADV_INVAL);
2440 madvise(gd->gd_CADDR2, PAGE_SIZE, MADV_INVAL);
2442 bcopy(gd->gd_CADDR1, gd->gd_CADDR2, PAGE_SIZE);
2444 *(int *) gd->gd_CMAP1 = 0;
2445 *(int *) gd->gd_CMAP2 = 0;
2450 * pmap_copy_page_frag:
2452 * Copy the physical page from the source PA to the target PA.
2453 * This function may be called from an interrupt. No locking
2457 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
2459 struct mdglobaldata *gd = mdcpu;
2462 if (*(int *) gd->gd_CMAP1)
2463 panic("pmap_copy_page: CMAP1 busy");
2464 if (*(int *) gd->gd_CMAP2)
2465 panic("pmap_copy_page: CMAP2 busy");
2467 *(int *) gd->gd_CMAP1 = VPTE_V | (src & VPTE_FRAME) | VPTE_A;
2468 *(int *) gd->gd_CMAP2 = VPTE_V | VPTE_R | VPTE_W | (dst & VPTE_FRAME) | VPTE_A | VPTE_M;
2470 madvise(gd->gd_CADDR1, PAGE_SIZE, MADV_INVAL);
2471 madvise(gd->gd_CADDR2, PAGE_SIZE, MADV_INVAL);
2473 bcopy((char *)gd->gd_CADDR1 + (src & PAGE_MASK),
2474 (char *)gd->gd_CADDR2 + (dst & PAGE_MASK),
2477 *(int *) gd->gd_CMAP1 = 0;
2478 *(int *) gd->gd_CMAP2 = 0;
2483 * Returns true if the pmap's pv is one of the first
2484 * 16 pvs linked to from this page. This count may
2485 * be changed upwards or downwards in the future; it
2486 * is only necessary that true be returned for a small
2487 * subset of pmaps for proper page aging.
2490 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2495 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2500 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2501 if (pv->pv_pmap == pmap) {
2514 * Remove all pages from specified address space
2515 * this aids process exit speeds. Also, this code
2516 * is special cased for current process only, but
2517 * can have the more generic (and slightly slower)
2518 * mode enabled. This is much faster than pmap_remove
2519 * in the case of running down an entire address space.
2522 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2527 pmap_inval_info info;
2529 int32_t save_generation;
2531 if (curproc && pmap == vmspace_pmap(curproc->p_vmspace))
2536 pmap_inval_init(&info);
2538 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
2539 if (pv->pv_va >= eva || pv->pv_va < sva) {
2540 npv = TAILQ_NEXT(pv, pv_plist);
2544 KKASSERT(pmap == pv->pv_pmap);
2546 pte = pmap_pte(pmap, pv->pv_va);
2550 * We cannot remove wired pages from a process' mapping
2553 if (tpte & VPTE_WIRED) {
2554 npv = TAILQ_NEXT(pv, pv_plist);
2558 /* See NOTE: PMAP_INVAL_ADD */
2559 pmap_inval_add(&info, pmap, pv->pv_va);
2561 m = PHYS_TO_VM_PAGE(tpte);
2563 KASSERT(m < &vm_page_array[vm_page_array_size],
2564 ("pmap_remove_pages: bad tpte %x", tpte));
2566 pmap->pm_stats.resident_count--;
2569 * Update the vm_page_t clean and reference bits.
2571 if (tpte & VPTE_M) {
2575 npv = TAILQ_NEXT(pv, pv_plist);
2576 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
2577 save_generation = ++pmap->pm_generation;
2579 m->md.pv_list_count--;
2580 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2581 if (TAILQ_FIRST(&m->md.pv_list) == NULL) {
2582 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2585 pmap_unuse_pt(pmap, pv->pv_va, pv->pv_ptem, &info);
2589 * Restart the scan if we blocked during the unuse or free
2590 * calls and other removals were made.
2592 if (save_generation != pmap->pm_generation) {
2593 kprintf("Warning: pmap_remove_pages race-A avoided\n");
2594 pv = TAILQ_FIRST(&pmap->pm_pvlist);
2597 pmap_inval_flush(&info);
2602 * pmap_testbit tests bits in pte's
2603 * note that the testbit/changebit routines are inline,
2604 * and a lot of things compile-time evaluate.
2607 pmap_testbit(vm_page_t m, int bit)
2612 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2615 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2620 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2622 * if the bit being tested is the modified bit, then
2623 * mark clean_map and ptes as never
2626 if (bit & (VPTE_A|VPTE_M)) {
2627 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2631 #if defined(PMAP_DIAGNOSTIC)
2633 kprintf("Null pmap (tb) at va: 0x%x\n", pv->pv_va);
2637 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2648 * This routine is used to clear bits in ptes. Certain bits require special
2649 * handling, in particular (on virtual kernels) the VPTE_M (modify) bit.
2651 static __inline void
2652 pmap_clearbit(vm_page_t m, int bit)
2654 struct pmap_inval_info info;
2659 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2662 pmap_inval_init(&info);
2666 * Loop over all current mappings setting/clearing as appropos If
2667 * setting RO do we need to clear the VAC?
2669 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2671 * don't write protect pager mappings
2673 if (bit == VPTE_W) {
2674 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2678 #if defined(PMAP_DIAGNOSTIC)
2680 kprintf("Null pmap (cb) at va: 0x%x\n", pv->pv_va);
2686 * Careful here. We can use a locked bus instruction to
2687 * clear VPTE_A or VPTE_M safely but we need to synchronize
2688 * with the target cpus when we mess with VPTE_W.
2690 * On virtual kernels we must force a new fault-on-write
2691 * in the real kernel if we clear the Modify bit ourselves,
2692 * otherwise the real kernel will not get a new fault and
2693 * will never set our Modify bit again.
2695 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2696 if (bit & (VPTE_W|VPTE_M))
2697 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
2701 if (bit == VPTE_W) {
2702 if (pbits & VPTE_M) {
2705 atomic_clear_int(pte, VPTE_M|VPTE_W);
2706 } else if (bit == VPTE_M) {
2708 * We do not have to make the page read-only
2709 * when clearing the Modify bit. The real
2710 * kernel will make the real PTE read-only
2711 * or otherwise detect the write and set
2712 * our VPTE_M again simply by us invalidating
2713 * the real kernel VA for the pmap (as we did
2714 * above). This allows the real kernel to
2715 * handle the write fault without forwarding
2718 atomic_clear_int(pte, VPTE_M);
2720 atomic_clear_int(pte, bit);
2724 pmap_inval_flush(&info);
2729 * pmap_page_protect:
2731 * Lower the permission for all mappings to a given page.
2734 pmap_page_protect(vm_page_t m, vm_prot_t prot)
2736 if ((prot & VM_PROT_WRITE) == 0) {
2737 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
2738 pmap_clearbit(m, VPTE_W);
2746 pmap_phys_address(int ppn)
2748 return (i386_ptob(ppn));
2752 * pmap_ts_referenced:
2754 * Return a count of reference bits for a page, clearing those bits.
2755 * It is not necessary for every reference bit to be cleared, but it
2756 * is necessary that 0 only be returned when there are truly no
2757 * reference bits set.
2759 * XXX: The exact number of bits to check and clear is a matter that
2760 * should be tested and standardized at some point in the future for
2761 * optimal aging of shared pages.
2764 pmap_ts_referenced(vm_page_t m)
2766 pv_entry_t pv, pvf, pvn;
2770 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2775 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2780 pvn = TAILQ_NEXT(pv, pv_list);
2782 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2784 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2786 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2789 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2791 if (pte && (*pte & VPTE_A)) {
2793 atomic_clear_int(pte, VPTE_A);
2795 atomic_clear_int_nonlocked(pte, VPTE_A);
2802 } while ((pv = pvn) != NULL && pv != pvf);
2812 * Return whether or not the specified physical page was modified
2813 * in any physical maps.
2816 pmap_is_modified(vm_page_t m)
2818 return pmap_testbit(m, VPTE_M);
2822 * Clear the modify bits on the specified physical page.
2825 pmap_clear_modify(vm_page_t m)
2827 pmap_clearbit(m, VPTE_M);
2831 * pmap_clear_reference:
2833 * Clear the reference bit on the specified physical page.
2836 pmap_clear_reference(vm_page_t m)
2838 pmap_clearbit(m, VPTE_A);
2842 * Miscellaneous support routines follow
2846 i386_protection_init(void)
2850 kp = protection_codes;
2851 for (prot = 0; prot < 8; prot++) {
2852 if (prot & VM_PROT_READ)
2854 if (prot & VM_PROT_WRITE)
2856 if (prot & VM_PROT_EXECUTE)
2863 * Map a set of physical memory pages into the kernel virtual
2864 * address space. Return a pointer to where it is mapped. This
2865 * routine is intended to be used for mapping device memory,
2868 * NOTE: we can't use pgeflag unless we invalidate the pages one at
2872 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
2874 vm_offset_t va, tmpva, offset;
2877 offset = pa & PAGE_MASK;
2878 size = roundup(offset + size, PAGE_SIZE);
2880 va = kmem_alloc_nofault(&kernel_map, size);
2882 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
2884 pa = pa & VPTE_FRAME;
2885 for (tmpva = va; size > 0;) {
2886 pte = KernelPTA + (tmpva >> PAGE_SHIFT);
2887 *pte = pa | VPTE_R | VPTE_W | VPTE_V; /* | pgeflag; */
2895 return ((void *)(va + offset));
2899 pmap_unmapdev(vm_offset_t va, vm_size_t size)
2901 vm_offset_t base, offset;
2903 base = va & VPTE_FRAME;
2904 offset = va & PAGE_MASK;
2905 size = roundup(offset + size, PAGE_SIZE);
2906 pmap_qremove(va, size >> PAGE_SHIFT);
2907 kmem_free(&kernel_map, base, size);
2911 * perform the pmap work for mincore
2914 pmap_mincore(pmap_t pmap, vm_offset_t addr)
2920 ptep = pmap_pte(pmap, addr);
2925 if ((pte = *ptep) != 0) {
2928 val = MINCORE_INCORE;
2929 if ((pte & VPTE_MANAGED) == 0)
2932 pa = pte & VPTE_FRAME;
2934 m = PHYS_TO_VM_PAGE(pa);
2940 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
2942 * Modified by someone
2944 else if (m->dirty || pmap_is_modified(m))
2945 val |= MINCORE_MODIFIED_OTHER;
2950 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
2953 * Referenced by someone
2955 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
2956 val |= MINCORE_REFERENCED_OTHER;
2957 vm_page_flag_set(m, PG_REFERENCED);
2964 pmap_activate(struct proc *p)
2968 pmap = vmspace_pmap(p->p_vmspace);
2970 atomic_set_int(&pmap->pm_active, 1 << mycpu->gd_cpuid);
2972 pmap->pm_active |= 1;
2974 #if defined(SWTCH_OPTIM_STATS)
2978 KKASSERT((p == curproc));
2980 curthread->td_pcb->pcb_cr3 = vtophys(pmap->pm_pdir);
2981 load_cr3(curthread->td_pcb->pcb_cr3);
2986 pmap_deactivate(struct proc *p)
2990 pmap = vmspace_pmap(p->p_vmspace);
2992 atomic_clear_int(&pmap->pm_active, 1 << mycpu->gd_cpuid);
2994 pmap->pm_active &= ~1;
2997 * XXX - note we do not adjust %cr3. The caller is expected to
2998 * activate a new pmap or do a thread-exit.
3003 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3006 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3010 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3017 static void pads (pmap_t pm);
3018 void pmap_pvdump (vm_paddr_t pa);
3020 /* print address space of pmap*/
3028 if (pm == &kernel_pmap)
3030 for (i = 0; i < 1024; i++)
3032 for (j = 0; j < 1024; j++) {
3033 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
3034 if (pm == &kernel_pmap && va < KERNBASE)
3036 if (pm != &kernel_pmap && va > UPT_MAX_ADDRESS)
3038 ptep = pmap_pte(pm, va);
3039 if (ptep && (*ptep & VPTE_V)) {
3041 (void *)va, (unsigned)*ptep);
3048 pmap_pvdump(vm_paddr_t pa)
3053 kprintf("pa %08llx", (long long)pa);
3054 m = PHYS_TO_VM_PAGE(pa);
3055 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3057 kprintf(" -> pmap %p, va %x, flags %x",
3058 (void *)pv->pv_pmap, pv->pv_va, pv->pv_flags);
3060 kprintf(" -> pmap %p, va %x", (void *)pv->pv_pmap, pv->pv_va);