4 * Copyright (c) 2006 The DragonFly Project. All rights reserved.
5 * Copyright (c) 1991 Regents of the University of California.
7 * Copyright (c) 1994 John S. Dyson
9 * Copyright (c) 1994 David Greenman
10 * All rights reserved.
11 * Copyright (c) 2004-2006 Matthew Dillon
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
24 * 3. Neither the name of The DragonFly Project nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific, prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
31 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
32 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
33 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
34 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
36 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
37 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
38 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91
42 * $FreeBSD: src/sys/i386/i386/pmap.c,v 1.250.2.18 2002/03/06 22:48:53 silby Exp $
45 * NOTE: PMAP_INVAL_ADD: In pc32 this function is called prior to adjusting
46 * the PTE in the page table, because a cpu synchronization might be required.
47 * The actual invalidation is delayed until the following call or flush. In
48 * the VKERNEL build this function is called prior to adjusting the PTE and
49 * invalidates the table synchronously (not delayed), and is not SMP safe
53 #include <sys/types.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
58 #include <sys/vkernel.h>
60 #include <sys/thread.h>
62 #include <sys/vmspace.h>
65 #include <vm/vm_page.h>
66 #include <vm/vm_extern.h>
67 #include <vm/vm_kern.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_zone.h>
70 #include <vm/vm_pageout.h>
72 #include <machine/md_var.h>
73 #include <machine/pcb.h>
74 #include <machine/pmap_inval.h>
75 #include <machine/globaldata.h>
77 #include <sys/sysref2.h>
78 #include <sys/spinlock2.h>
82 struct pmap kernel_pmap;
84 static struct vm_zone pvzone;
85 static struct vm_object pvzone_obj;
86 static TAILQ_HEAD(,pmap) pmap_list = TAILQ_HEAD_INITIALIZER(pmap_list);
87 static int pv_entry_count;
88 static int pv_entry_max;
89 static int pv_entry_high_water;
90 static int pmap_pagedaemon_waken;
91 static boolean_t pmap_initialized = FALSE;
92 static int protection_codes[8];
94 static void i386_protection_init(void);
95 static void pmap_remove_all(vm_page_t m);
96 static int pmap_release_free_page(struct pmap *pmap, vm_page_t p);
99 #ifndef PMAP_SHPGPERPROC
100 #define PMAP_SHPGPERPROC 200
103 #define pmap_pde(m, v) (&((m)->pm_pdir[(vm_offset_t)(v) >> PDRSHIFT]))
105 #define pte_prot(m, p) \
106 (protection_codes[p & (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)])
112 struct pv_entry *pvinit;
114 for (i = 0; i < vm_page_array_size; i++) {
117 m = &vm_page_array[i];
118 TAILQ_INIT(&m->md.pv_list);
119 m->md.pv_list_count = 0;
122 i = vm_page_array_size;
125 pvinit = (struct pv_entry *)kmem_alloc(&kernel_map, i*sizeof(*pvinit));
126 zbootinit(&pvzone, "PV ENTRY", sizeof(*pvinit), pvinit, i);
127 pmap_initialized = TRUE;
133 int shpgperproc = PMAP_SHPGPERPROC;
135 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
136 pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
137 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
138 pv_entry_high_water = 9 * (pv_entry_max / 10);
139 zinitna(&pvzone, &pvzone_obj, NULL, 0, pv_entry_max, ZONE_INTERRUPT, 1);
143 * Bootstrap the kernel_pmap so it can be used with pmap_enter().
145 * NOTE! pm_pdir for the kernel pmap is offset so VA's translate
146 * directly into PTD indexes (PTA is also offset for the same reason).
147 * This is necessary because, for now, KVA is not mapped at address 0.
149 * Page table pages are not managed like they are in normal pmaps, so
150 * no pteobj is needed.
155 vm_pindex_t i = (vm_offset_t)KernelPTD >> PAGE_SHIFT;
158 * The kernel_pmap's pm_pteobj is used only for locking and not
161 kernel_pmap.pm_pdir = KernelPTD - (KvaStart >> SEG_SHIFT);
162 kernel_pmap.pm_pdirpte = KernelPTA[i];
163 kernel_pmap.pm_count = 1;
164 kernel_pmap.pm_active = (cpumask_t)-1 & ~CPUMASK_LOCK;
165 kernel_pmap.pm_pteobj = &kernel_object;
166 TAILQ_INIT(&kernel_pmap.pm_pvlist);
167 TAILQ_INIT(&kernel_pmap.pm_pvlist_free);
168 spin_init(&kernel_pmap.pm_spin);
169 lwkt_token_init(&kernel_pmap.pm_token, "kpmap_tok");
170 i386_protection_init();
174 * Initialize pmap0/vmspace0 . Since process 0 never enters user mode we
175 * just dummy it up so it works well enough for fork().
177 * In DragonFly, process pmaps may only be used to manipulate user address
178 * space, never kernel address space.
181 pmap_pinit0(struct pmap *pmap)
186 /************************************************************************
187 * Procedures to manage whole physical maps *
188 ************************************************************************
190 * Initialize a preallocated and zeroed pmap structure,
191 * such as one in a vmspace structure.
194 pmap_pinit(struct pmap *pmap)
200 * No need to allocate page table space yet but we do need a valid
201 * page directory table.
203 if (pmap->pm_pdir == NULL) {
205 (vpte_t *)kmem_alloc_pageable(&kernel_map, PAGE_SIZE);
209 * allocate object for the pte array and page directory
211 npages = VPTE_PAGETABLE_SIZE +
212 (VM_MAX_USER_ADDRESS / PAGE_SIZE) * sizeof(vpte_t);
213 npages = (npages + PAGE_MASK) / PAGE_SIZE;
215 if (pmap->pm_pteobj == NULL)
216 pmap->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, npages);
217 pmap->pm_pdindex = npages - 1;
220 * allocate the page directory page
222 ptdpg = vm_page_grab(pmap->pm_pteobj, pmap->pm_pdindex,
223 VM_ALLOC_NORMAL | VM_ALLOC_RETRY | VM_ALLOC_ZERO);
226 /* not usually mapped */
227 vm_page_flag_clear(ptdpg, PG_MAPPED);
228 vm_page_wakeup(ptdpg);
230 pmap_kenter((vm_offset_t)pmap->pm_pdir, VM_PAGE_TO_PHYS(ptdpg));
231 pmap->pm_pdirpte = KernelPTA[(vm_offset_t)pmap->pm_pdir >> PAGE_SHIFT];
235 pmap->pm_ptphint = NULL;
236 pmap->pm_cpucachemask = 0;
237 TAILQ_INIT(&pmap->pm_pvlist);
238 TAILQ_INIT(&pmap->pm_pvlist_free);
239 spin_init(&pmap->pm_spin);
240 lwkt_token_init(&pmap->pm_token, "pmap_tok");
241 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
242 pmap->pm_stats.resident_count = 1;
246 * Clean up a pmap structure so it can be physically freed
251 pmap_puninit(pmap_t pmap)
254 kmem_free(&kernel_map, (vm_offset_t)pmap->pm_pdir, PAGE_SIZE);
255 pmap->pm_pdir = NULL;
257 if (pmap->pm_pteobj) {
258 vm_object_deallocate(pmap->pm_pteobj);
259 pmap->pm_pteobj = NULL;
265 * Wire in kernel global address entries. To avoid a race condition
266 * between pmap initialization and pmap_growkernel, this procedure
267 * adds the pmap to the master list (which growkernel scans to update),
268 * then copies the template.
270 * In a virtual kernel there are no kernel global address entries.
275 pmap_pinit2(struct pmap *pmap)
277 spin_lock(&pmap_spin);
278 TAILQ_INSERT_TAIL(&pmap_list, pmap, pm_pmnode);
279 spin_unlock(&pmap_spin);
283 * Release all resources held by the given physical map.
285 * Should only be called if the map contains no valid mappings.
287 * Caller must hold pmap->pm_token
289 static int pmap_release_callback(struct vm_page *p, void *data);
292 pmap_release(struct pmap *pmap)
294 struct mdglobaldata *gd = mdcpu;
295 vm_object_t object = pmap->pm_pteobj;
296 struct rb_vm_page_scan_info info;
298 KKASSERT(pmap != &kernel_pmap);
300 #if defined(DIAGNOSTIC)
301 if (object->ref_count != 1)
302 panic("pmap_release: pteobj reference count != 1");
305 * Once we destroy the page table, the mapping becomes invalid.
306 * Don't waste time doing a madvise to invalidate the mapping, just
307 * set cpucachemask to 0.
309 if (pmap->pm_pdir == gd->gd_PT1pdir) {
310 gd->gd_PT1pdir = NULL;
312 /* madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL); */
314 if (pmap->pm_pdir == gd->gd_PT2pdir) {
315 gd->gd_PT2pdir = NULL;
317 /* madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL); */
319 if (pmap->pm_pdir == gd->gd_PT3pdir) {
320 gd->gd_PT3pdir = NULL;
322 /* madvise(gd->gd_PT3map, SEG_SIZE, MADV_INVAL); */
326 info.object = object;
328 spin_lock(&pmap_spin);
329 TAILQ_REMOVE(&pmap_list, pmap, pm_pmnode);
330 spin_unlock(&pmap_spin);
332 vm_object_hold(object);
336 info.limit = object->generation;
338 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
339 pmap_release_callback, &info);
340 if (info.error == 0 && info.mpte) {
341 if (!pmap_release_free_page(pmap, info.mpte))
344 } while (info.error);
345 vm_object_drop(object);
348 * Leave the KVA reservation for pm_pdir cached for later reuse.
350 pmap->pm_pdirpte = 0;
351 pmap->pm_cpucachemask = 0;
355 * Callback to release a page table page backing a directory
359 pmap_release_callback(struct vm_page *p, void *data)
361 struct rb_vm_page_scan_info *info = data;
363 if (p->pindex == info->pmap->pm_pdindex) {
367 if (!pmap_release_free_page(info->pmap, p)) {
371 if (info->object->generation != info->limit) {
379 * Add a reference to the specified pmap.
384 pmap_reference(pmap_t pmap)
387 lwkt_gettoken(&vm_token);
389 lwkt_reltoken(&vm_token);
393 /************************************************************************
394 * VMSPACE MANAGEMENT *
395 ************************************************************************
397 * The VMSPACE management we do in our virtual kernel must be reflected
398 * in the real kernel. This is accomplished by making vmspace system
399 * calls to the real kernel.
402 cpu_vmspace_alloc(struct vmspace *vm)
407 #define LAST_EXTENT (VM_MAX_USER_ADDRESS - 0x80000000)
409 if (vmspace_create(&vm->vm_pmap, 0, NULL) < 0)
410 panic("vmspace_create() failed");
412 rp = vmspace_mmap(&vm->vm_pmap, (void *)0x00000000, 0x40000000,
413 PROT_READ|PROT_WRITE,
414 MAP_FILE|MAP_SHARED|MAP_VPAGETABLE|MAP_FIXED,
416 if (rp == MAP_FAILED)
417 panic("vmspace_mmap: failed1");
418 vmspace_mcontrol(&vm->vm_pmap, (void *)0x00000000, 0x40000000,
420 rp = vmspace_mmap(&vm->vm_pmap, (void *)0x40000000, 0x40000000,
421 PROT_READ|PROT_WRITE,
422 MAP_FILE|MAP_SHARED|MAP_VPAGETABLE|MAP_FIXED,
423 MemImageFd, 0x40000000);
424 if (rp == MAP_FAILED)
425 panic("vmspace_mmap: failed2");
426 vmspace_mcontrol(&vm->vm_pmap, (void *)0x40000000, 0x40000000,
428 rp = vmspace_mmap(&vm->vm_pmap, (void *)0x80000000, LAST_EXTENT,
429 PROT_READ|PROT_WRITE,
430 MAP_FILE|MAP_SHARED|MAP_VPAGETABLE|MAP_FIXED,
431 MemImageFd, 0x80000000);
432 vmspace_mcontrol(&vm->vm_pmap, (void *)0x80000000, LAST_EXTENT,
434 if (rp == MAP_FAILED)
435 panic("vmspace_mmap: failed3");
437 r = vmspace_mcontrol(&vm->vm_pmap, (void *)0x00000000, 0x40000000,
438 MADV_SETMAP, vmspace_pmap(vm)->pm_pdirpte);
440 panic("vmspace_mcontrol: failed1");
441 r = vmspace_mcontrol(&vm->vm_pmap, (void *)0x40000000, 0x40000000,
442 MADV_SETMAP, vmspace_pmap(vm)->pm_pdirpte);
444 panic("vmspace_mcontrol: failed2");
445 r = vmspace_mcontrol(&vm->vm_pmap, (void *)0x80000000, LAST_EXTENT,
446 MADV_SETMAP, vmspace_pmap(vm)->pm_pdirpte);
448 panic("vmspace_mcontrol: failed3");
452 cpu_vmspace_free(struct vmspace *vm)
454 if (vmspace_destroy(&vm->vm_pmap) < 0)
455 panic("vmspace_destroy() failed");
458 /************************************************************************
459 * Procedures which operate directly on the kernel PMAP *
460 ************************************************************************/
463 * This maps the requested page table and gives us access to it.
465 * This routine can be called from a potentially preempting interrupt
466 * thread or from a normal thread.
469 get_ptbase(struct pmap *pmap, vm_offset_t va)
471 struct mdglobaldata *gd = mdcpu;
473 if (pmap == &kernel_pmap) {
474 KKASSERT(va >= KvaStart && va < KvaEnd);
475 return(KernelPTA + (va >> PAGE_SHIFT));
476 } else if (pmap->pm_pdir == gd->gd_PT1pdir) {
477 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
478 *gd->gd_PT1pde = pmap->pm_pdirpte;
479 madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL);
480 atomic_set_cpumask(&pmap->pm_cpucachemask,
483 return(gd->gd_PT1map + (va >> PAGE_SHIFT));
484 } else if (pmap->pm_pdir == gd->gd_PT2pdir) {
485 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
486 *gd->gd_PT2pde = pmap->pm_pdirpte;
487 madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL);
488 atomic_set_cpumask(&pmap->pm_cpucachemask,
491 return(gd->gd_PT2map + (va >> PAGE_SHIFT));
495 * If we aren't running from a potentially preempting interrupt,
496 * load a new page table directory into the page table cache
498 if (gd->mi.gd_intr_nesting_level == 0 &&
499 (gd->mi.gd_curthread->td_flags & TDF_INTTHREAD) == 0) {
501 * Choose one or the other and map the page table
502 * in the KVA space reserved for it.
504 if ((gd->gd_PTflip = 1 - gd->gd_PTflip) == 0) {
505 gd->gd_PT1pdir = pmap->pm_pdir;
506 *gd->gd_PT1pde = pmap->pm_pdirpte;
507 madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL);
508 atomic_set_cpumask(&pmap->pm_cpucachemask,
510 return(gd->gd_PT1map + (va >> PAGE_SHIFT));
512 gd->gd_PT2pdir = pmap->pm_pdir;
513 *gd->gd_PT2pde = pmap->pm_pdirpte;
514 madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL);
515 atomic_set_cpumask(&pmap->pm_cpucachemask,
517 return(gd->gd_PT2map + (va >> PAGE_SHIFT));
522 * If we are running from a preempting interrupt use a private
523 * map. The caller must be in a critical section.
525 KKASSERT(IN_CRITICAL_SECT(curthread));
526 if (pmap->pm_pdir == gd->gd_PT3pdir) {
527 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
528 *gd->gd_PT3pde = pmap->pm_pdirpte;
529 madvise(gd->gd_PT3map, SEG_SIZE, MADV_INVAL);
530 atomic_set_cpumask(&pmap->pm_cpucachemask,
534 gd->gd_PT3pdir = pmap->pm_pdir;
535 *gd->gd_PT3pde = pmap->pm_pdirpte;
536 madvise(gd->gd_PT3map, SEG_SIZE, MADV_INVAL);
537 atomic_set_cpumask(&pmap->pm_cpucachemask,
540 return(gd->gd_PT3map + (va >> PAGE_SHIFT));
544 get_ptbase1(struct pmap *pmap, vm_offset_t va)
546 struct mdglobaldata *gd = mdcpu;
548 if (pmap == &kernel_pmap) {
549 KKASSERT(va >= KvaStart && va < KvaEnd);
550 return(KernelPTA + (va >> PAGE_SHIFT));
551 } else if (pmap->pm_pdir == gd->gd_PT1pdir) {
552 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
553 *gd->gd_PT1pde = pmap->pm_pdirpte;
554 madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL);
555 atomic_set_cpumask(&pmap->pm_cpucachemask,
558 return(gd->gd_PT1map + (va >> PAGE_SHIFT));
560 KKASSERT(gd->mi.gd_intr_nesting_level == 0 &&
561 (gd->mi.gd_curthread->td_flags & TDF_INTTHREAD) == 0);
562 gd->gd_PT1pdir = pmap->pm_pdir;
563 *gd->gd_PT1pde = pmap->pm_pdirpte;
564 madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL);
565 return(gd->gd_PT1map + (va >> PAGE_SHIFT));
569 get_ptbase2(struct pmap *pmap, vm_offset_t va)
571 struct mdglobaldata *gd = mdcpu;
573 if (pmap == &kernel_pmap) {
574 KKASSERT(va >= KvaStart && va < KvaEnd);
575 return(KernelPTA + (va >> PAGE_SHIFT));
576 } else if (pmap->pm_pdir == gd->gd_PT2pdir) {
577 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
578 *gd->gd_PT2pde = pmap->pm_pdirpte;
579 madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL);
580 atomic_set_cpumask(&pmap->pm_cpucachemask,
583 return(gd->gd_PT2map + (va >> PAGE_SHIFT));
585 KKASSERT(gd->mi.gd_intr_nesting_level == 0 &&
586 (gd->mi.gd_curthread->td_flags & TDF_INTTHREAD) == 0);
587 gd->gd_PT2pdir = pmap->pm_pdir;
588 *gd->gd_PT2pde = pmap->pm_pdirpte;
589 madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL);
590 return(gd->gd_PT2map + (va >> PAGE_SHIFT));
594 * Return a pointer to the page table entry for the specified va in the
595 * specified pmap. NULL is returned if there is no valid page table page
598 static __inline vpte_t *
599 pmap_pte(struct pmap *pmap, vm_offset_t va)
603 ptep = &pmap->pm_pdir[va >> SEG_SHIFT];
607 return (get_ptbase(pmap, va));
613 * Enter a mapping into kernel_pmap. Mappings created in this fashion
614 * are not managed. Mappings must be immediately accessible on all cpus.
616 * Call pmap_inval_pte() to invalidate the virtual pte and clean out the
617 * real pmap and handle related races before storing the new vpte.
620 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
625 KKASSERT(va >= KvaStart && va < KvaEnd);
626 npte = (vpte_t)pa | VPTE_R | VPTE_W | VPTE_V;
627 ptep = KernelPTA + (va >> PAGE_SHIFT);
629 pmap_inval_pte(ptep, &kernel_pmap, va);
634 * Synchronize a kvm mapping originally made for the private use on
635 * some other cpu so it can be used on all cpus.
637 * XXX add MADV_RESYNC to improve performance.
640 pmap_kenter_sync(vm_offset_t va)
642 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
646 * Synchronize a kvm mapping originally made for the private use on
647 * some other cpu so it can be used on our cpu. Turns out to be the
648 * same madvise() call, because we have to sync the real pmaps anyway.
650 * XXX add MADV_RESYNC to improve performance.
653 pmap_kenter_sync_quick(vm_offset_t va)
655 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
660 * Make a previously read-only kernel mapping R+W (not implemented by
664 pmap_kmodify_rw(vm_offset_t va)
666 *pmap_kpte(va) |= VPTE_R | VPTE_W;
667 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
671 * Make a kernel mapping non-cacheable (not applicable to virtual kernels)
674 pmap_kmodify_nc(vm_offset_t va)
676 *pmap_kpte(va) |= VPTE_N;
677 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
683 * Map a contiguous range of physical memory to a KVM
686 pmap_map(vm_offset_t *virtp, vm_paddr_t start, vm_paddr_t end, int prot)
688 vm_offset_t sva, virt;
691 while (start < end) {
692 pmap_kenter(virt, start);
701 pmap_kpte(vm_offset_t va)
705 KKASSERT(va >= KvaStart && va < KvaEnd);
706 ptep = KernelPTA + (va >> PAGE_SHIFT);
711 * Enter an unmanaged KVA mapping for the private use of the current
712 * cpu only. pmap_kenter_sync() may be called to make the mapping usable
715 * It is illegal for the mapping to be accessed by other cpus unleess
716 * pmap_kenter_sync*() is called.
719 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
724 KKASSERT(va >= KvaStart && va < KvaEnd);
726 npte = (vpte_t)pa | VPTE_R | VPTE_W | VPTE_V;
727 ptep = KernelPTA + (va >> PAGE_SHIFT);
729 pmap_inval_pte_quick(ptep, &kernel_pmap, va);
734 * Make a temporary mapping for a physical address. This is only intended
735 * to be used for panic dumps.
737 * The caller is responsible for calling smp_invltlb().
740 pmap_kenter_temporary(vm_paddr_t pa, long i)
742 pmap_kenter_quick(crashdumpmap + (i * PAGE_SIZE), pa);
743 return ((void *)crashdumpmap);
747 * Remove an unmanaged mapping created with pmap_kenter*().
750 pmap_kremove(vm_offset_t va)
754 KKASSERT(va >= KvaStart && va < KvaEnd);
756 ptep = KernelPTA + (va >> PAGE_SHIFT);
758 pmap_inval_pte(ptep, &kernel_pmap, va);
763 * Remove an unmanaged mapping created with pmap_kenter*() but synchronize
764 * only with this cpu.
766 * Unfortunately because we optimize new entries by testing VPTE_V later
767 * on, we actually still have to synchronize with all the cpus. XXX maybe
768 * store a junk value and test against 0 in the other places instead?
771 pmap_kremove_quick(vm_offset_t va)
775 KKASSERT(va >= KvaStart && va < KvaEnd);
777 ptep = KernelPTA + (va >> PAGE_SHIFT);
779 pmap_inval_pte(ptep, &kernel_pmap, va); /* NOT _quick */
784 * Extract the physical address from the kernel_pmap that is associated
785 * with the specified virtual address.
788 pmap_kextract(vm_offset_t va)
793 KKASSERT(va >= KvaStart && va < KvaEnd);
795 ptep = KernelPTA + (va >> PAGE_SHIFT);
796 pa = (vm_paddr_t)(*ptep & VPTE_FRAME) | (va & PAGE_MASK);
801 * Map a set of unmanaged VM pages into KVM.
804 pmap_qenter(vm_offset_t va, struct vm_page **m, int count)
806 KKASSERT(va >= KvaStart && va + count * PAGE_SIZE < KvaEnd);
810 ptep = KernelPTA + (va >> PAGE_SHIFT);
812 pmap_inval_pte(ptep, &kernel_pmap, va);
813 *ptep = (vpte_t)(*m)->phys_addr | VPTE_R | VPTE_W | VPTE_V;
821 * Undo the effects of pmap_qenter*().
824 pmap_qremove(vm_offset_t va, int count)
826 KKASSERT(va >= KvaStart && va + count * PAGE_SIZE < KvaEnd);
830 ptep = KernelPTA + (va >> PAGE_SHIFT);
832 pmap_inval_pte(ptep, &kernel_pmap, va);
839 /************************************************************************
840 * Misc support glue called by machine independant code *
841 ************************************************************************
843 * These routines are called by machine independant code to operate on
844 * certain machine-dependant aspects of processes, threads, and pmaps.
848 * Initialize MD portions of the thread structure.
851 pmap_init_thread(thread_t td)
853 /* enforce pcb placement */
854 td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
855 td->td_savefpu = &td->td_pcb->pcb_save;
856 td->td_sp = (char *)td->td_pcb - 16;
860 * This routine directly affects the fork perf for a process.
863 pmap_init_proc(struct proc *p)
868 * We pre-allocate all page table pages for kernel virtual memory so
869 * this routine will only be called if KVM has been exhausted.
874 pmap_growkernel(vm_offset_t kstart, vm_offset_t kend)
878 addr = (kend + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
880 lwkt_gettoken(&vm_token);
881 if (addr > virtual_end - SEG_SIZE)
882 panic("KVM exhausted");
883 kernel_vm_end = addr;
884 lwkt_reltoken(&vm_token);
888 * The modification bit is not tracked for any pages in this range. XXX
889 * such pages in this maps should always use pmap_k*() functions and not
892 * XXX User and kernel address spaces are independant for virtual kernels,
893 * this function only applies to the kernel pmap.
896 pmap_track_modified(pmap_t pmap, vm_offset_t va)
898 if (pmap != &kernel_pmap)
900 if ((va < clean_sva) || (va >= clean_eva))
906 /************************************************************************
907 * Procedures supporting managed page table pages *
908 ************************************************************************
910 * These procedures are used to track managed page table pages. These pages
911 * use the page table page's vm_page_t to track PTEs in the page. The
912 * page table pages themselves are arranged in a VM object, pmap->pm_pteobj.
914 * This allows the system to throw away page table pages for user processes
915 * at will and reinstantiate them on demand.
919 * This routine works like vm_page_lookup() but also blocks as long as the
920 * page is busy. This routine does not busy the page it returns.
922 * Unless the caller is managing objects whos pages are in a known state,
923 * the call should be made with a critical section held so the page's object
924 * association remains valid on return.
927 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
931 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
932 m = vm_page_lookup_busy_wait(object, pindex, FALSE, "pplookp");
938 * This routine unholds page table pages, and if the hold count
939 * drops to zero, then it decrements the wire count.
941 * We must recheck that this is the last hold reference after busy-sleeping
945 _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
947 vm_page_busy_wait(m, FALSE, "pmuwpt");
948 KASSERT(m->queue == PQ_NONE,
949 ("_pmap_unwire_pte_hold: %p->queue != PQ_NONE", m));
951 if (m->hold_count == 1) {
953 * Unmap the page table page.
955 KKASSERT(pmap->pm_pdir[m->pindex] != 0);
956 pmap_inval_pde(&pmap->pm_pdir[m->pindex], pmap,
957 (vm_offset_t)m->pindex << SEG_SHIFT);
958 KKASSERT(pmap->pm_stats.resident_count > 0);
959 --pmap->pm_stats.resident_count;
961 if (pmap->pm_ptphint == m)
962 pmap->pm_ptphint = NULL;
965 * This was our last hold, the page had better be unwired
966 * after we decrement wire_count.
968 * FUTURE NOTE: shared page directory page could result in
969 * multiple wire counts.
973 KKASSERT(m->wire_count == 0);
974 atomic_add_int(&vmstats.v_wire_count, -1);
975 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
977 vm_page_free_zero(m);
980 KKASSERT(m->hold_count > 1);
988 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
990 KKASSERT(m->hold_count > 0);
991 if (m->hold_count > 1) {
995 return _pmap_unwire_pte_hold(pmap, m);
1000 * After removing a page table entry, this routine is used to
1001 * conditionally free the page, and manage the hold/wire counts.
1004 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte)
1008 ASSERT_LWKT_TOKEN_HELD(vm_object_token(pmap->pm_pteobj));
1012 * page table pages in the kernel_pmap are not managed.
1014 if (pmap == &kernel_pmap)
1016 ptepindex = (va >> PDRSHIFT);
1017 if (pmap->pm_ptphint &&
1018 (pmap->pm_ptphint->pindex == ptepindex)) {
1019 mpte = pmap->pm_ptphint;
1021 mpte = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1022 pmap->pm_ptphint = mpte;
1023 vm_page_wakeup(mpte);
1026 return pmap_unwire_pte_hold(pmap, mpte);
1030 * Attempt to release and free the vm_page backing a page directory page
1031 * in a pmap. Returns 1 on success, 0 on failure (if the procedure had
1035 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1037 vpte_t *pde = pmap->pm_pdir;
1040 * This code optimizes the case of freeing non-busy
1041 * page-table pages. Those pages are zero now, and
1042 * might as well be placed directly into the zero queue.
1044 if (vm_page_busy_try(p, FALSE)) {
1045 vm_page_sleep_busy(p, FALSE, "pmaprl");
1048 KKASSERT(pmap->pm_stats.resident_count > 0);
1049 --pmap->pm_stats.resident_count;
1051 if (p->hold_count) {
1052 panic("pmap_release: freeing held page table page");
1055 * Page directory pages need to have the kernel stuff cleared, so
1056 * they can go into the zero queue also.
1058 * In virtual kernels there is no 'kernel stuff'. For the moment
1059 * I just make sure the whole thing has been zero'd even though
1060 * it should already be completely zero'd.
1062 * pmaps for vkernels do not self-map because they do not share
1063 * their address space with the vkernel. Clearing of pde[] thus
1064 * only applies to page table pages and not to the page directory
1067 if (p->pindex == pmap->pm_pdindex) {
1068 bzero(pde, VPTE_PAGETABLE_SIZE);
1069 pmap_kremove((vm_offset_t)pmap->pm_pdir);
1071 KKASSERT(pde[p->pindex] != 0);
1072 pmap_inval_pde(&pde[p->pindex], pmap,
1073 (vm_offset_t)p->pindex << SEG_SHIFT);
1077 * Clear the matching hint
1079 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1080 pmap->pm_ptphint = NULL;
1083 * And throw the page away. The page is completely zero'd out so
1084 * optimize the free call.
1087 atomic_add_int(&vmstats.v_wire_count, -1);
1088 vm_page_free_zero(p);
1093 * This routine is called if the page table page is not mapped in the page
1096 * The routine is broken up into two parts for readability.
1098 * It must return a held mpte and map the page directory page as required.
1099 * Because vm_page_grab() can block, we must re-check pm_pdir[ptepindex]
1102 _pmap_allocpte(pmap_t pmap, unsigned ptepindex)
1108 * Find or fabricate a new pagetable page. A busied page will be
1109 * returned. This call may block.
1111 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1112 VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1113 vm_page_flag_set(m, PG_MAPPED);
1115 KASSERT(m->queue == PQ_NONE,
1116 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1119 * Increment the hold count for the page we will be returning to
1125 * It is possible that someone else got in and mapped by the page
1126 * directory page while we were blocked, if so just unbusy and
1127 * return the held page.
1129 if ((ptepa = pmap->pm_pdir[ptepindex]) != 0) {
1130 KKASSERT((ptepa & VPTE_FRAME) == VM_PAGE_TO_PHYS(m));
1137 * Map the pagetable page into the process address space, if
1138 * it isn't already there.
1140 ++pmap->pm_stats.resident_count;
1142 ptepa = VM_PAGE_TO_PHYS(m);
1143 pmap->pm_pdir[ptepindex] = (vpte_t)ptepa | VPTE_R | VPTE_W | VPTE_V |
1147 * We are likely about to access this page table page, so set the
1148 * page table hint to reduce overhead.
1150 pmap->pm_ptphint = m;
1158 * Determine the page table page required to access the VA in the pmap
1159 * and allocate it if necessary. Return a held vm_page_t for the page.
1161 * Only used with user pmaps.
1164 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1170 ASSERT_LWKT_TOKEN_HELD(vm_object_token(pmap->pm_pteobj));
1173 * Calculate pagetable page index
1175 ptepindex = va >> PDRSHIFT;
1178 * Get the page directory entry
1180 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1183 * This supports switching from a 4MB page to a
1186 if (ptepa & VPTE_PS) {
1187 KKASSERT(pmap->pm_pdir[ptepindex] != 0);
1188 pmap_inval_pde(&pmap->pm_pdir[ptepindex], pmap,
1189 (vm_offset_t)ptepindex << SEG_SHIFT);
1194 * If the page table page is mapped, we just increment the
1195 * hold count, and activate it.
1199 * In order to get the page table page, try the
1202 if (pmap->pm_ptphint &&
1203 (pmap->pm_ptphint->pindex == ptepindex)) {
1204 m = pmap->pm_ptphint;
1206 m = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1207 pmap->pm_ptphint = m;
1214 * Here if the pte page isn't mapped, or if it has been deallocated.
1216 return _pmap_allocpte(pmap, ptepindex);
1219 /************************************************************************
1220 * Managed pages in pmaps *
1221 ************************************************************************
1223 * All pages entered into user pmaps and some pages entered into the kernel
1224 * pmap are managed, meaning that pmap_protect() and other related management
1225 * functions work on these pages.
1229 * free the pv_entry back to the free list. This function may be
1230 * called from an interrupt.
1232 static __inline void
1233 free_pv_entry(pv_entry_t pv)
1240 * get a new pv_entry, allocating a block from the system
1241 * when needed. This function may be called from an interrupt.
1247 if (pv_entry_high_water &&
1248 (pv_entry_count > pv_entry_high_water) &&
1249 (pmap_pagedaemon_waken == 0)) {
1250 pmap_pagedaemon_waken = 1;
1251 wakeup (&vm_pages_needed);
1253 return zalloc(&pvzone);
1257 * This routine is very drastic, but can save the system
1267 static int warningdone=0;
1269 if (pmap_pagedaemon_waken == 0)
1271 lwkt_gettoken(&vm_token);
1272 pmap_pagedaemon_waken = 0;
1274 if (warningdone < 5) {
1275 kprintf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1279 for (i = 0; i < vm_page_array_size; i++) {
1280 m = &vm_page_array[i];
1281 if (m->wire_count || m->hold_count)
1283 if (vm_page_busy_try(m, TRUE) == 0) {
1284 if (m->wire_count == 0 && m->hold_count == 0) {
1290 lwkt_reltoken(&vm_token);
1294 * If it is the first entry on the list, it is actually
1295 * in the header and we must copy the following entry up
1296 * to the header. Otherwise we must search the list for
1297 * the entry. In either case we free the now unused entry.
1299 * caller must hold vm_token
1302 pmap_remove_entry(struct pmap *pmap, vm_page_t m, vm_offset_t va)
1308 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1309 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1310 if (pmap == pv->pv_pmap && va == pv->pv_va)
1314 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1315 if (va == pv->pv_va)
1321 * Note that pv_ptem is NULL if the page table page itself is not
1322 * managed, even if the page being removed IS managed.
1326 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1327 m->md.pv_list_count--;
1328 atomic_add_int(&m->object->agg_pv_list_count, -1);
1329 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1330 if (TAILQ_EMPTY(&m->md.pv_list))
1331 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1332 ++pmap->pm_generation;
1333 vm_object_hold(pmap->pm_pteobj);
1334 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem);
1335 vm_object_drop(pmap->pm_pteobj);
1343 * Create a pv entry for page at pa for (pmap, va). If the page table page
1344 * holding the VA is managed, mpte will be non-NULL.
1347 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
1352 pv = get_pv_entry();
1357 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1358 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1359 ++pmap->pm_generation;
1360 m->md.pv_list_count++;
1361 atomic_add_int(&m->object->agg_pv_list_count, 1);
1367 * pmap_remove_pte: do the things to unmap a page in a process
1370 pmap_remove_pte(struct pmap *pmap, vpte_t *ptq, vm_offset_t va)
1375 oldpte = pmap_inval_loadandclear(ptq, pmap, va);
1376 if (oldpte & VPTE_WIRED)
1377 --pmap->pm_stats.wired_count;
1378 KKASSERT(pmap->pm_stats.wired_count >= 0);
1382 * Machines that don't support invlpg, also don't support
1383 * VPTE_G. XXX VPTE_G is disabled for SMP so don't worry about
1386 if (oldpte & VPTE_G)
1387 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
1389 KKASSERT(pmap->pm_stats.resident_count > 0);
1390 --pmap->pm_stats.resident_count;
1391 if (oldpte & VPTE_MANAGED) {
1392 m = PHYS_TO_VM_PAGE(oldpte);
1393 if (oldpte & VPTE_M) {
1394 #if defined(PMAP_DIAGNOSTIC)
1395 if (pmap_nw_modified((pt_entry_t) oldpte)) {
1397 "pmap_remove: modified page not writable: va: 0x%x, pte: 0x%x\n",
1401 if (pmap_track_modified(pmap, va))
1404 if (oldpte & VPTE_A)
1405 vm_page_flag_set(m, PG_REFERENCED);
1406 return pmap_remove_entry(pmap, m, va);
1408 return pmap_unuse_pt(pmap, va, NULL);
1417 * Remove a single page from a process address space.
1419 * This function may not be called from an interrupt if the pmap is
1423 pmap_remove_page(struct pmap *pmap, vm_offset_t va)
1428 * if there is no pte for this address, just skip it!!! Otherwise
1429 * get a local va for mappings for this pmap and remove the entry.
1431 if (*pmap_pde(pmap, va) != 0) {
1432 ptq = get_ptbase(pmap, va);
1434 pmap_remove_pte(pmap, ptq, va);
1440 * Remove the given range of addresses from the specified map.
1442 * It is assumed that the start and end are properly rounded to the
1445 * This function may not be called from an interrupt if the pmap is
1451 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
1455 vm_offset_t ptpaddr;
1456 vm_pindex_t sindex, eindex;
1461 vm_object_hold(pmap->pm_pteobj);
1462 lwkt_gettoken(&vm_token);
1463 KKASSERT(pmap->pm_stats.resident_count >= 0);
1464 if (pmap->pm_stats.resident_count == 0) {
1465 lwkt_reltoken(&vm_token);
1466 vm_object_drop(pmap->pm_pteobj);
1471 * special handling of removing one page. a very
1472 * common operation and easy to short circuit some
1475 if (((sva + PAGE_SIZE) == eva) &&
1476 ((pmap->pm_pdir[(sva >> PDRSHIFT)] & VPTE_PS) == 0)) {
1477 pmap_remove_page(pmap, sva);
1478 lwkt_reltoken(&vm_token);
1479 vm_object_drop(pmap->pm_pteobj);
1484 * Get a local virtual address for the mappings that are being
1487 * XXX this is really messy because the kernel pmap is not relative
1490 sindex = (sva >> PAGE_SHIFT);
1491 eindex = (eva >> PAGE_SHIFT);
1493 for (; sindex < eindex; sindex = pdnxt) {
1497 * Calculate index for next page table.
1499 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1500 if (pmap->pm_stats.resident_count == 0)
1503 pdirindex = sindex / NPDEPG;
1504 if (((ptpaddr = pmap->pm_pdir[pdirindex]) & VPTE_PS) != 0) {
1505 KKASSERT(pmap->pm_pdir[pdirindex] != 0);
1506 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1507 pmap_inval_pde(&pmap->pm_pdir[pdirindex], pmap,
1508 (vm_offset_t)pdirindex << SEG_SHIFT);
1513 * Weed out invalid mappings. Note: we assume that the page
1514 * directory table is always allocated, and in kernel virtual.
1520 * Limit our scan to either the end of the va represented
1521 * by the current page table page, or to the end of the
1522 * range being removed.
1528 * NOTE: pmap_remove_pte() can block.
1530 for (; sindex != pdnxt; sindex++) {
1533 ptbase = get_ptbase(pmap, sindex << PAGE_SHIFT);
1536 va = i386_ptob(sindex);
1537 if (pmap_remove_pte(pmap, ptbase, va))
1541 lwkt_reltoken(&vm_token);
1542 vm_object_drop(pmap->pm_pteobj);
1546 * Removes this physical page from all physical maps in which it resides.
1547 * Reflects back modify bits to the pager.
1549 * This routine may not be called from an interrupt.
1554 pmap_remove_all(vm_page_t m)
1559 #if defined(PMAP_DIAGNOSTIC)
1561 * XXX this makes pmap_page_protect(NONE) illegal for non-managed
1564 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
1565 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%08llx", (long long)VM_PAGE_TO_PHYS(m));
1569 lwkt_gettoken(&vm_token);
1570 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
1571 KKASSERT(pv->pv_pmap->pm_stats.resident_count > 0);
1572 --pv->pv_pmap->pm_stats.resident_count;
1574 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
1575 KKASSERT(pte != NULL);
1577 tpte = pmap_inval_loadandclear(pte, pv->pv_pmap, pv->pv_va);
1578 if (tpte & VPTE_WIRED)
1579 --pv->pv_pmap->pm_stats.wired_count;
1580 KKASSERT(pv->pv_pmap->pm_stats.wired_count >= 0);
1583 vm_page_flag_set(m, PG_REFERENCED);
1586 * Update the vm_page_t clean and reference bits.
1588 if (tpte & VPTE_M) {
1589 #if defined(PMAP_DIAGNOSTIC)
1590 if (pmap_nw_modified((pt_entry_t) tpte)) {
1592 "pmap_remove_all: modified page not writable: va: 0x%x, pte: 0x%x\n",
1596 if (pmap_track_modified(pv->pv_pmap, pv->pv_va))
1599 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1600 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
1601 ++pv->pv_pmap->pm_generation;
1602 m->md.pv_list_count--;
1603 atomic_add_int(&m->object->agg_pv_list_count, -1);
1604 if (TAILQ_EMPTY(&m->md.pv_list))
1605 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1606 vm_object_hold(pv->pv_pmap->pm_pteobj);
1607 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
1608 vm_object_drop(pv->pv_pmap->pm_pteobj);
1611 KKASSERT((m->flags & (PG_MAPPED | PG_WRITEABLE)) == 0);
1612 lwkt_reltoken(&vm_token);
1616 * Set the physical protection on the specified range of this map
1619 * This function may not be called from an interrupt if the map is
1620 * not the kernel_pmap.
1625 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
1629 vm_offset_t pdnxt, ptpaddr;
1630 vm_pindex_t sindex, eindex;
1636 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1637 pmap_remove(pmap, sva, eva);
1641 if (prot & VM_PROT_WRITE)
1644 lwkt_gettoken(&vm_token);
1645 ptbase = get_ptbase(pmap, sva);
1647 sindex = (sva >> PAGE_SHIFT);
1648 eindex = (eva >> PAGE_SHIFT);
1651 for (; sindex < eindex; sindex = pdnxt) {
1655 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1657 pdirindex = sindex / NPDEPG;
1660 * Clear the modified and writable bits for a 4m page.
1661 * Throw away the modified bit (?)
1663 if (((ptpaddr = pmap->pm_pdir[pdirindex]) & VPTE_PS) != 0) {
1664 pmap_clean_pde(&pmap->pm_pdir[pdirindex], pmap,
1665 (vm_offset_t)pdirindex << SEG_SHIFT);
1666 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1671 * Weed out invalid mappings. Note: we assume that the page
1672 * directory table is always allocated, and in kernel virtual.
1677 if (pdnxt > eindex) {
1681 for (; sindex != pdnxt; sindex++) {
1686 * Clean managed pages and also check the accessed
1687 * bit. Just remove write perms for unmanaged
1688 * pages. Be careful of races, turning off write
1689 * access will force a fault rather then setting
1690 * the modified bit at an unexpected time.
1692 ptep = &ptbase[sindex - sbase];
1693 if (*ptep & VPTE_MANAGED) {
1694 pbits = pmap_clean_pte(ptep, pmap,
1697 if (pbits & VPTE_A) {
1698 m = PHYS_TO_VM_PAGE(pbits);
1699 vm_page_flag_set(m, PG_REFERENCED);
1700 atomic_clear_long(ptep, VPTE_A);
1702 if (pbits & VPTE_M) {
1703 if (pmap_track_modified(pmap, i386_ptob(sindex))) {
1705 m = PHYS_TO_VM_PAGE(pbits);
1710 pbits = pmap_setro_pte(ptep, pmap,
1715 lwkt_reltoken(&vm_token);
1719 * Enter a managed page into a pmap. If the page is not wired related pmap
1720 * data can be destroyed at any time for later demand-operation.
1722 * Insert the vm_page (m) at virtual address (v) in (pmap), with the
1723 * specified protection, and wire the mapping if requested.
1725 * NOTE: This routine may not lazy-evaluate or lose information. The
1726 * page must actually be inserted into the given map NOW.
1728 * NOTE: When entering a page at a KVA address, the pmap must be the
1734 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1735 boolean_t wired, vm_map_entry_t entry __unused)
1740 vpte_t origpte, newpte;
1748 vm_object_hold(pmap->pm_pteobj);
1749 lwkt_gettoken(&vm_token);
1752 * Get the page table page. The kernel_pmap's page table pages
1753 * are preallocated and have no associated vm_page_t.
1755 if (pmap == &kernel_pmap)
1758 mpte = pmap_allocpte(pmap, va);
1760 pte = pmap_pte(pmap, va);
1763 * Page Directory table entry not valid, we need a new PT page
1764 * and pmap_allocpte() didn't give us one. Oops!
1767 panic("pmap_enter: invalid page directory pmap=%p, va=0x%p",
1772 * Deal with races on the original mapping (though don't worry
1773 * about VPTE_A races) by cleaning it. This will force a fault
1774 * if an attempt is made to write to the page.
1776 pa = VM_PAGE_TO_PHYS(m) & VPTE_FRAME;
1777 origpte = pmap_clean_pte(pte, pmap, va);
1778 opa = origpte & VPTE_FRAME;
1780 if (origpte & VPTE_PS)
1781 panic("pmap_enter: attempted pmap_enter on 4MB page");
1784 * Mapping has not changed, must be protection or wiring change.
1786 if (origpte && (opa == pa)) {
1788 * Wiring change, just update stats. We don't worry about
1789 * wiring PT pages as they remain resident as long as there
1790 * are valid mappings in them. Hence, if a user page is wired,
1791 * the PT page will be also.
1793 if (wired && ((origpte & VPTE_WIRED) == 0))
1794 ++pmap->pm_stats.wired_count;
1795 else if (!wired && (origpte & VPTE_WIRED))
1796 --pmap->pm_stats.wired_count;
1797 KKASSERT(pmap->pm_stats.wired_count >= 0);
1800 * Remove the extra pte reference. Note that we cannot
1801 * optimize the RO->RW case because we have adjusted the
1802 * wiring count above and may need to adjust the wiring
1809 * We might be turning off write access to the page,
1810 * so we go ahead and sense modify status.
1812 if (origpte & VPTE_MANAGED) {
1813 if ((origpte & VPTE_M) &&
1814 pmap_track_modified(pmap, va)) {
1816 om = PHYS_TO_VM_PAGE(opa);
1820 KKASSERT(m->flags & PG_MAPPED);
1825 * Mapping has changed, invalidate old range and fall through to
1826 * handle validating new mapping.
1830 err = pmap_remove_pte(pmap, pte, va);
1832 panic("pmap_enter: pte vanished, va: %p", (void *)va);
1833 pte = pmap_pte(pmap, va);
1834 origpte = pmap_clean_pte(pte, pmap, va);
1835 opa = origpte & VPTE_FRAME;
1837 kprintf("pmap_enter: Warning, raced pmap %p va %p\n",
1843 * Enter on the PV list if part of our managed memory. Note that we
1844 * raise IPL while manipulating pv_table since pmap_enter can be
1845 * called at interrupt time.
1847 if (pmap_initialized &&
1848 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
1849 pmap_insert_entry(pmap, va, mpte, m);
1851 vm_page_flag_set(m, PG_MAPPED);
1855 * Increment counters
1857 ++pmap->pm_stats.resident_count;
1859 pmap->pm_stats.wired_count++;
1863 * Now validate mapping with desired protection/wiring.
1865 newpte = (vm_offset_t) (pa | pte_prot(pmap, prot) | VPTE_V);
1868 newpte |= VPTE_WIRED;
1869 if (pmap != &kernel_pmap)
1873 * If the mapping or permission bits are different from the
1874 * (now cleaned) original pte, an update is needed. We've
1875 * already downgraded or invalidated the page so all we have
1876 * to do now is update the bits.
1878 * XXX should we synchronize RO->RW changes to avoid another
1881 if ((origpte & ~(VPTE_W|VPTE_M|VPTE_A)) != newpte) {
1882 *pte = newpte | VPTE_A;
1883 if (newpte & VPTE_W)
1884 vm_page_flag_set(m, PG_WRITEABLE);
1886 KKASSERT((newpte & VPTE_MANAGED) == 0 || m->flags & PG_MAPPED);
1887 lwkt_reltoken(&vm_token);
1888 vm_object_drop(pmap->pm_pteobj);
1892 * This code works like pmap_enter() but assumes VM_PROT_READ and not-wired.
1894 * Currently this routine may only be used on user pmaps, not kernel_pmap.
1897 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m)
1905 KKASSERT(pmap != &kernel_pmap);
1907 KKASSERT(va >= VM_MIN_USER_ADDRESS && va < VM_MAX_USER_ADDRESS);
1910 * Calculate pagetable page (mpte), allocating it if necessary.
1912 * A held page table page (mpte), or NULL, is passed onto the
1913 * section following.
1915 ptepindex = va >> PDRSHIFT;
1917 vm_object_hold(pmap->pm_pteobj);
1918 lwkt_gettoken(&vm_token);
1922 * Get the page directory entry
1924 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1927 * If the page table page is mapped, we just increment
1928 * the hold count, and activate it.
1931 if (ptepa & VPTE_PS)
1932 panic("pmap_enter_quick: unexpected mapping into 4MB page");
1933 if (pmap->pm_ptphint &&
1934 (pmap->pm_ptphint->pindex == ptepindex)) {
1935 mpte = pmap->pm_ptphint;
1937 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1938 pmap->pm_ptphint = mpte;
1939 vm_page_wakeup(mpte);
1944 mpte = _pmap_allocpte(pmap, ptepindex);
1946 } while (mpte == NULL);
1949 * Ok, now that the page table page has been validated, get the pte.
1950 * If the pte is already mapped undo mpte's hold_count and
1953 pte = pmap_pte(pmap, va);
1955 pmap_unwire_pte_hold(pmap, mpte);
1956 lwkt_reltoken(&vm_token);
1957 vm_object_drop(pmap->pm_pteobj);
1962 * Enter on the PV list if part of our managed memory. Note that we
1963 * raise IPL while manipulating pv_table since pmap_enter can be
1964 * called at interrupt time.
1966 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
1967 pmap_insert_entry(pmap, va, mpte, m);
1968 vm_page_flag_set(m, PG_MAPPED);
1972 * Increment counters
1974 ++pmap->pm_stats.resident_count;
1976 pa = VM_PAGE_TO_PHYS(m);
1979 * Now validate mapping with RO protection
1981 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
1982 *pte = (vpte_t)pa | VPTE_V | VPTE_U;
1984 *pte = (vpte_t)pa | VPTE_V | VPTE_U | VPTE_MANAGED;
1985 /*pmap_inval_add(&info, pmap, va); shouldn't be needed 0->valid */
1986 /*pmap_inval_flush(&info); don't need for vkernel */
1987 lwkt_reltoken(&vm_token);
1988 vm_object_drop(pmap->pm_pteobj);
1992 * Extract the physical address for the translation at the specified
1993 * virtual address in the pmap.
1995 * The caller must hold vm_token if non-blocking operation is desired.
1999 pmap_extract(pmap_t pmap, vm_offset_t va)
2004 lwkt_gettoken(&vm_token);
2005 if (pmap && (pte = pmap->pm_pdir[va >> SEG_SHIFT]) != 0) {
2006 if (pte & VPTE_PS) {
2007 rtval = pte & ~((vpte_t)(1 << SEG_SHIFT) - 1);
2008 rtval |= va & SEG_MASK;
2010 pte = *get_ptbase(pmap, va);
2011 rtval = (pte & VPTE_FRAME) | (va & PAGE_MASK);
2016 lwkt_reltoken(&vm_token);
2020 #define MAX_INIT_PT (96)
2023 * This routine preloads the ptes for a given object into the specified pmap.
2024 * This eliminates the blast of soft faults on process startup and
2025 * immediately after an mmap.
2029 static int pmap_object_init_pt_callback(vm_page_t p, void *data);
2032 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2033 vm_object_t object, vm_pindex_t pindex,
2034 vm_size_t size, int limit)
2036 struct rb_vm_page_scan_info info;
2041 * We can't preinit if read access isn't set or there is no pmap
2044 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2048 * We can't preinit if the pmap is not the current pmap
2050 lp = curthread->td_lwp;
2051 if (lp == NULL || pmap != vmspace_pmap(lp->lwp_vmspace))
2054 psize = size >> PAGE_SHIFT;
2056 if ((object->type != OBJT_VNODE) ||
2057 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2058 (object->resident_page_count > MAX_INIT_PT))) {
2062 if (psize + pindex > object->size) {
2063 if (object->size < pindex)
2065 psize = object->size - pindex;
2072 * Use a red-black scan to traverse the requested range and load
2073 * any valid pages found into the pmap.
2075 * We cannot safely scan the object's memq unless we are in a
2076 * critical section since interrupts can remove pages from objects.
2078 info.start_pindex = pindex;
2079 info.end_pindex = pindex + psize - 1;
2085 vm_object_hold(object);
2086 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2087 pmap_object_init_pt_callback, &info);
2088 vm_object_drop(object);
2092 * The caller must hold vm_token.
2096 pmap_object_init_pt_callback(vm_page_t p, void *data)
2098 struct rb_vm_page_scan_info *info = data;
2099 vm_pindex_t rel_index;
2102 * don't allow an madvise to blow away our really
2103 * free pages allocating pv entries.
2105 if ((info->limit & MAP_PREFAULT_MADVISE) &&
2106 vmstats.v_free_count < vmstats.v_free_reserved) {
2111 * Ignore list markers and ignore pages we cannot instantly
2112 * busy (while holding the object token).
2114 if (p->flags & PG_MARKER)
2116 if (vm_page_busy_try(p, TRUE))
2118 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2119 (p->flags & PG_FICTITIOUS) == 0) {
2120 if ((p->queue - p->pc) == PQ_CACHE)
2121 vm_page_deactivate(p);
2122 rel_index = p->pindex - info->start_pindex;
2123 pmap_enter_quick(info->pmap,
2124 info->addr + i386_ptob(rel_index), p);
2131 * Return TRUE if the pmap is in shape to trivially
2132 * pre-fault the specified address.
2134 * Returns FALSE if it would be non-trivial or if a
2135 * pte is already loaded into the slot.
2140 pmap_prefault_ok(pmap_t pmap, vm_offset_t addr)
2145 lwkt_gettoken(&vm_token);
2146 if ((*pmap_pde(pmap, addr)) == 0) {
2149 pte = get_ptbase(pmap, addr);
2150 ret = (*pte) ? 0 : 1;
2152 lwkt_reltoken(&vm_token);
2157 * Change the wiring attribute for a map/virtual-address pair.
2158 * The mapping must already exist in the pmap.
2160 * No other requirements.
2163 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired,
2164 vm_map_entry_t entry __unused)
2171 lwkt_gettoken(&vm_token);
2172 pte = get_ptbase(pmap, va);
2174 if (wired && (*pte & VPTE_WIRED) == 0)
2175 ++pmap->pm_stats.wired_count;
2176 else if (!wired && (*pte & VPTE_WIRED))
2177 --pmap->pm_stats.wired_count;
2178 KKASSERT(pmap->pm_stats.wired_count >= 0);
2181 * Wiring is not a hardware characteristic so there is no need to
2182 * invalidate TLB. However, in an SMP environment we must use
2183 * a locked bus cycle to update the pte (if we are not using
2184 * the pmap_inval_*() API that is)... it's ok to do this for simple
2188 atomic_set_long(pte, VPTE_WIRED);
2190 atomic_clear_long(pte, VPTE_WIRED);
2191 lwkt_reltoken(&vm_token);
2195 * Copy the range specified by src_addr/len
2196 * from the source map to the range dst_addr/len
2197 * in the destination map.
2199 * This routine is only advisory and need not do anything.
2202 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2203 vm_size_t len, vm_offset_t src_addr)
2206 vm_offset_t end_addr = src_addr + len;
2213 * XXX BUGGY. Amoung other things srcmpte is assumed to remain
2214 * valid through blocking calls, and that's just not going to
2221 if (dst_addr != src_addr)
2223 if (dst_pmap->pm_pdir == NULL)
2225 if (src_pmap->pm_pdir == NULL)
2228 lwkt_gettoken(&vm_token);
2230 src_frame = get_ptbase1(src_pmap, src_addr);
2231 dst_frame = get_ptbase2(dst_pmap, src_addr);
2234 * critical section protection is required to maintain the page/object
2235 * association, interrupts can free pages and remove them from
2238 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
2239 vpte_t *src_pte, *dst_pte;
2240 vm_page_t dstmpte, srcmpte;
2241 vm_offset_t srcptepaddr;
2244 if (addr >= VM_MAX_USER_ADDRESS)
2245 panic("pmap_copy: invalid to pmap_copy page tables");
2248 * Don't let optional prefaulting of pages make us go
2249 * way below the low water mark of free pages or way
2250 * above high water mark of used pv entries.
2252 if (vmstats.v_free_count < vmstats.v_free_reserved ||
2253 pv_entry_count > pv_entry_high_water)
2256 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
2257 ptepindex = addr >> PDRSHIFT;
2259 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex];
2260 if (srcptepaddr == 0)
2263 if (srcptepaddr & VPTE_PS) {
2264 if (dst_pmap->pm_pdir[ptepindex] == 0) {
2265 dst_pmap->pm_pdir[ptepindex] = (vpte_t)srcptepaddr;
2266 dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
2271 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
2272 if ((srcmpte == NULL) || (srcmpte->hold_count == 0) ||
2273 (srcmpte->flags & PG_BUSY)) {
2277 if (pdnxt > end_addr)
2280 src_pte = src_frame + ((addr - src_addr) >> PAGE_SHIFT);
2281 dst_pte = dst_frame + ((addr - src_addr) >> PAGE_SHIFT);
2282 while (addr < pdnxt) {
2287 * we only virtual copy managed pages
2289 if ((ptetemp & VPTE_MANAGED) != 0) {
2291 * We have to check after allocpte for the
2292 * pte still being around... allocpte can
2295 * pmap_allocpte can block, unfortunately
2296 * we have to reload the tables.
2298 dstmpte = pmap_allocpte(dst_pmap, addr);
2299 src_frame = get_ptbase1(src_pmap, src_addr);
2300 dst_frame = get_ptbase2(dst_pmap, src_addr);
2302 if ((*dst_pte == 0) && (ptetemp = *src_pte) &&
2303 (ptetemp & VPTE_MANAGED) != 0) {
2305 * Clear the modified and accessed
2306 * (referenced) bits during the copy.
2308 * We do not have to clear the write
2309 * bit to force a fault-on-modify
2310 * because the real kernel's target
2311 * pmap is empty and will fault anyway.
2313 m = PHYS_TO_VM_PAGE(ptetemp);
2314 *dst_pte = ptetemp & ~(VPTE_M | VPTE_A);
2315 ++dst_pmap->pm_stats.resident_count;
2316 pmap_insert_entry(dst_pmap, addr,
2318 KKASSERT(m->flags & PG_MAPPED);
2320 pmap_unwire_pte_hold(dst_pmap, dstmpte);
2322 if (dstmpte->hold_count >= srcmpte->hold_count)
2330 lwkt_reltoken(&vm_token);
2336 * Zero the specified PA by mapping the page into KVM and clearing its
2339 * This function may be called from an interrupt and no locking is
2343 pmap_zero_page(vm_paddr_t phys)
2345 struct mdglobaldata *gd = mdcpu;
2349 panic("pmap_zero_page: CMAP3 busy");
2350 *gd->gd_CMAP3 = VPTE_V | VPTE_R | VPTE_W | (phys & VPTE_FRAME) | VPTE_A | VPTE_M;
2351 madvise(gd->gd_CADDR3, PAGE_SIZE, MADV_INVAL);
2353 bzero(gd->gd_CADDR3, PAGE_SIZE);
2359 * pmap_page_assertzero:
2361 * Assert that a page is empty, panic if it isn't.
2364 pmap_page_assertzero(vm_paddr_t phys)
2366 struct mdglobaldata *gd = mdcpu;
2371 panic("pmap_zero_page: CMAP3 busy");
2372 *gd->gd_CMAP3 = VPTE_V | VPTE_R | VPTE_W |
2373 (phys & VPTE_FRAME) | VPTE_A | VPTE_M;
2374 madvise(gd->gd_CADDR3, PAGE_SIZE, MADV_INVAL);
2375 for (i = 0; i < PAGE_SIZE; i += 4) {
2376 if (*(int *)((char *)gd->gd_CADDR3 + i) != 0) {
2377 panic("pmap_page_assertzero() @ %p not zero!",
2378 (void *)gd->gd_CADDR3);
2388 * Zero part of a physical page by mapping it into memory and clearing
2389 * its contents with bzero.
2391 * off and size may not cover an area beyond a single hardware page.
2394 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
2396 struct mdglobaldata *gd = mdcpu;
2400 panic("pmap_zero_page: CMAP3 busy");
2401 *gd->gd_CMAP3 = VPTE_V | VPTE_R | VPTE_W |
2402 (phys & VPTE_FRAME) | VPTE_A | VPTE_M;
2403 madvise(gd->gd_CADDR3, PAGE_SIZE, MADV_INVAL);
2405 bzero((char *)gd->gd_CADDR3 + off, size);
2413 * Copy the physical page from the source PA to the target PA.
2414 * This function may be called from an interrupt. No locking
2418 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
2420 struct mdglobaldata *gd = mdcpu;
2423 if (*(int *) gd->gd_CMAP1)
2424 panic("pmap_copy_page: CMAP1 busy");
2425 if (*(int *) gd->gd_CMAP2)
2426 panic("pmap_copy_page: CMAP2 busy");
2428 *(int *) gd->gd_CMAP1 = VPTE_V | VPTE_R | (src & PG_FRAME) | VPTE_A;
2429 *(int *) gd->gd_CMAP2 = VPTE_V | VPTE_R | VPTE_W | (dst & VPTE_FRAME) | VPTE_A | VPTE_M;
2431 madvise(gd->gd_CADDR1, PAGE_SIZE, MADV_INVAL);
2432 madvise(gd->gd_CADDR2, PAGE_SIZE, MADV_INVAL);
2434 bcopy(gd->gd_CADDR1, gd->gd_CADDR2, PAGE_SIZE);
2436 *(int *) gd->gd_CMAP1 = 0;
2437 *(int *) gd->gd_CMAP2 = 0;
2442 * pmap_copy_page_frag:
2444 * Copy the physical page from the source PA to the target PA.
2445 * This function may be called from an interrupt. No locking
2449 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
2451 struct mdglobaldata *gd = mdcpu;
2454 if (*(int *) gd->gd_CMAP1)
2455 panic("pmap_copy_page: CMAP1 busy");
2456 if (*(int *) gd->gd_CMAP2)
2457 panic("pmap_copy_page: CMAP2 busy");
2459 *(int *) gd->gd_CMAP1 = VPTE_V | (src & VPTE_FRAME) | VPTE_A;
2460 *(int *) gd->gd_CMAP2 = VPTE_V | VPTE_R | VPTE_W | (dst & VPTE_FRAME) | VPTE_A | VPTE_M;
2462 madvise(gd->gd_CADDR1, PAGE_SIZE, MADV_INVAL);
2463 madvise(gd->gd_CADDR2, PAGE_SIZE, MADV_INVAL);
2465 bcopy((char *)gd->gd_CADDR1 + (src & PAGE_MASK),
2466 (char *)gd->gd_CADDR2 + (dst & PAGE_MASK),
2469 *(int *) gd->gd_CMAP1 = 0;
2470 *(int *) gd->gd_CMAP2 = 0;
2475 * Returns true if the pmap's pv is one of the first
2476 * 16 pvs linked to from this page. This count may
2477 * be changed upwards or downwards in the future; it
2478 * is only necessary that true be returned for a small
2479 * subset of pmaps for proper page aging.
2484 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2489 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2493 lwkt_gettoken(&vm_token);
2495 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2496 if (pv->pv_pmap == pmap) {
2497 lwkt_reltoken(&vm_token);
2505 lwkt_reltoken(&vm_token);
2511 * Remove all pages from specified address space
2512 * this aids process exit speeds. Also, this code
2513 * is special cased for current process only, but
2514 * can have the more generic (and slightly slower)
2515 * mode enabled. This is much faster than pmap_remove
2516 * in the case of running down an entire address space.
2521 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2526 int32_t save_generation;
2528 if (pmap->pm_pteobj)
2529 vm_object_hold(pmap->pm_pteobj);
2530 lwkt_gettoken(&vm_token);
2531 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
2532 if (pv->pv_va >= eva || pv->pv_va < sva) {
2533 npv = TAILQ_NEXT(pv, pv_plist);
2537 KKASSERT(pmap == pv->pv_pmap);
2539 pte = pmap_pte(pmap, pv->pv_va);
2542 * We cannot remove wired pages from a process' mapping
2545 if (*pte & VPTE_WIRED) {
2546 npv = TAILQ_NEXT(pv, pv_plist);
2549 tpte = pmap_inval_loadandclear(pte, pmap, pv->pv_va);
2551 m = PHYS_TO_VM_PAGE(tpte);
2553 KASSERT(m < &vm_page_array[vm_page_array_size],
2554 ("pmap_remove_pages: bad tpte %lx", tpte));
2556 KKASSERT(pmap->pm_stats.resident_count > 0);
2557 --pmap->pm_stats.resident_count;
2560 * Update the vm_page_t clean and reference bits.
2562 if (tpte & VPTE_M) {
2566 npv = TAILQ_NEXT(pv, pv_plist);
2567 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
2568 save_generation = ++pmap->pm_generation;
2570 m->md.pv_list_count--;
2571 atomic_add_int(&m->object->agg_pv_list_count, -1);
2572 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2573 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2574 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2576 pmap_unuse_pt(pmap, pv->pv_va, pv->pv_ptem);
2580 * Restart the scan if we blocked during the unuse or free
2581 * calls and other removals were made.
2583 if (save_generation != pmap->pm_generation) {
2584 kprintf("Warning: pmap_remove_pages race-A avoided\n");
2585 npv = TAILQ_FIRST(&pmap->pm_pvlist);
2588 lwkt_reltoken(&vm_token);
2589 if (pmap->pm_pteobj)
2590 vm_object_drop(pmap->pm_pteobj);
2594 * pmap_testbit tests bits in active mappings of a VM page.
2596 * The caller must hold vm_token
2599 pmap_testbit(vm_page_t m, int bit)
2604 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2607 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2612 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2614 * if the bit being tested is the modified bit, then
2615 * mark clean_map and ptes as never
2618 if (bit & (VPTE_A|VPTE_M)) {
2619 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2623 #if defined(PMAP_DIAGNOSTIC)
2625 kprintf("Null pmap (tb) at va: 0x%x\n", pv->pv_va);
2629 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2640 * This routine is used to clear bits in ptes. Certain bits require special
2641 * handling, in particular (on virtual kernels) the VPTE_M (modify) bit.
2643 * This routine is only called with certain VPTE_* bit combinations.
2645 * The caller must hold vm_token
2647 static __inline void
2648 pmap_clearbit(vm_page_t m, int bit)
2654 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2660 * Loop over all current mappings setting/clearing as appropos If
2661 * setting RO do we need to clear the VAC?
2663 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2665 * don't write protect pager mappings
2667 if (bit == VPTE_W) {
2668 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2672 #if defined(PMAP_DIAGNOSTIC)
2674 kprintf("Null pmap (cb) at va: 0x%x\n", pv->pv_va);
2680 * Careful here. We can use a locked bus instruction to
2681 * clear VPTE_A or VPTE_M safely but we need to synchronize
2682 * with the target cpus when we mess with VPTE_W.
2684 * On virtual kernels we must force a new fault-on-write
2685 * in the real kernel if we clear the Modify bit ourselves,
2686 * otherwise the real kernel will not get a new fault and
2687 * will never set our Modify bit again.
2689 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2691 if (bit == VPTE_W) {
2693 * We must also clear VPTE_M when clearing
2696 pbits = pmap_clean_pte(pte, pv->pv_pmap,
2700 } else if (bit == VPTE_M) {
2702 * We do not have to make the page read-only
2703 * when clearing the Modify bit. The real
2704 * kernel will make the real PTE read-only
2705 * or otherwise detect the write and set
2706 * our VPTE_M again simply by us invalidating
2707 * the real kernel VA for the pmap (as we did
2708 * above). This allows the real kernel to
2709 * handle the write fault without forwarding
2712 atomic_clear_long(pte, VPTE_M);
2713 } else if ((bit & (VPTE_W|VPTE_M)) == (VPTE_W|VPTE_M)) {
2715 * We've been asked to clear W & M, I guess
2716 * the caller doesn't want us to update
2717 * the dirty status of the VM page.
2719 pmap_clean_pte(pte, pv->pv_pmap, pv->pv_va);
2722 * We've been asked to clear bits that do
2723 * not interact with hardware.
2725 atomic_clear_long(pte, bit);
2733 * Lower the permission for all mappings to a given page.
2738 pmap_page_protect(vm_page_t m, vm_prot_t prot)
2740 if ((prot & VM_PROT_WRITE) == 0) {
2741 lwkt_gettoken(&vm_token);
2742 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
2743 pmap_clearbit(m, VPTE_W);
2744 vm_page_flag_clear(m, PG_WRITEABLE);
2748 lwkt_reltoken(&vm_token);
2753 pmap_phys_address(vm_pindex_t ppn)
2755 return (i386_ptob(ppn));
2759 * Return a count of reference bits for a page, clearing those bits.
2760 * It is not necessary for every reference bit to be cleared, but it
2761 * is necessary that 0 only be returned when there are truly no
2762 * reference bits set.
2764 * XXX: The exact number of bits to check and clear is a matter that
2765 * should be tested and standardized at some point in the future for
2766 * optimal aging of shared pages.
2771 pmap_ts_referenced(vm_page_t m)
2773 pv_entry_t pv, pvf, pvn;
2777 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2781 lwkt_gettoken(&vm_token);
2783 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2788 pvn = TAILQ_NEXT(pv, pv_list);
2790 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2792 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2794 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2797 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2799 if (pte && (*pte & VPTE_A)) {
2800 atomic_clear_long(pte, VPTE_A);
2806 } while ((pv = pvn) != NULL && pv != pvf);
2808 lwkt_reltoken(&vm_token);
2815 * Return whether or not the specified physical page was modified
2816 * in any physical maps.
2821 pmap_is_modified(vm_page_t m)
2825 lwkt_gettoken(&vm_token);
2826 res = pmap_testbit(m, VPTE_M);
2827 lwkt_reltoken(&vm_token);
2832 * Clear the modify bits on the specified physical page.
2837 pmap_clear_modify(vm_page_t m)
2839 lwkt_gettoken(&vm_token);
2840 pmap_clearbit(m, VPTE_M);
2841 lwkt_reltoken(&vm_token);
2845 * Clear the reference bit on the specified physical page.
2850 pmap_clear_reference(vm_page_t m)
2852 lwkt_gettoken(&vm_token);
2853 pmap_clearbit(m, VPTE_A);
2854 lwkt_reltoken(&vm_token);
2858 * Miscellaneous support routines follow
2862 i386_protection_init(void)
2866 kp = protection_codes;
2867 for (prot = 0; prot < 8; prot++) {
2868 if (prot & VM_PROT_READ)
2870 if (prot & VM_PROT_WRITE)
2872 if (prot & VM_PROT_EXECUTE)
2881 * Map a set of physical memory pages into the kernel virtual
2882 * address space. Return a pointer to where it is mapped. This
2883 * routine is intended to be used for mapping device memory,
2886 * NOTE: we can't use pgeflag unless we invalidate the pages one at
2890 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
2892 vm_offset_t va, tmpva, offset;
2895 offset = pa & PAGE_MASK;
2896 size = roundup(offset + size, PAGE_SIZE);
2898 va = kmem_alloc_nofault(&kernel_map, size, PAGE_SIZE);
2900 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
2902 pa = pa & VPTE_FRAME;
2903 for (tmpva = va; size > 0;) {
2904 pte = KernelPTA + (tmpva >> PAGE_SHIFT);
2905 *pte = pa | VPTE_R | VPTE_W | VPTE_V; /* | pgeflag; */
2913 return ((void *)(va + offset));
2917 pmap_unmapdev(vm_offset_t va, vm_size_t size)
2919 vm_offset_t base, offset;
2921 base = va & VPTE_FRAME;
2922 offset = va & PAGE_MASK;
2923 size = roundup(offset + size, PAGE_SIZE);
2924 pmap_qremove(va, size >> PAGE_SHIFT);
2925 kmem_free(&kernel_map, base, size);
2931 * Perform the pmap work for mincore
2936 pmap_mincore(pmap_t pmap, vm_offset_t addr)
2942 lwkt_gettoken(&vm_token);
2944 ptep = pmap_pte(pmap, addr);
2946 lwkt_reltoken(&vm_token);
2950 if ((pte = *ptep) != 0) {
2953 val = MINCORE_INCORE;
2954 if ((pte & VPTE_MANAGED) == 0)
2957 pa = pte & VPTE_FRAME;
2959 m = PHYS_TO_VM_PAGE(pa);
2965 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
2967 * Modified by someone
2969 else if (m->dirty || pmap_is_modified(m))
2970 val |= MINCORE_MODIFIED_OTHER;
2975 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
2978 * Referenced by someone
2980 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
2981 val |= MINCORE_REFERENCED_OTHER;
2982 vm_page_flag_set(m, PG_REFERENCED);
2986 lwkt_reltoken(&vm_token);
2991 * Caller must hold vmspace->vm_map.token for oldvm and newvm
2994 pmap_replacevm(struct proc *p, struct vmspace *newvm, int adjrefs)
2996 struct vmspace *oldvm;
2999 oldvm = p->p_vmspace;
3001 if (oldvm != newvm) {
3002 p->p_vmspace = newvm;
3003 KKASSERT(p->p_nthreads == 1);
3004 lp = RB_ROOT(&p->p_lwp_tree);
3005 pmap_setlwpvm(lp, newvm);
3007 sysref_get(&newvm->vm_sysref);
3008 sysref_put(&oldvm->vm_sysref);
3015 pmap_setlwpvm(struct lwp *lp, struct vmspace *newvm)
3017 struct vmspace *oldvm;
3021 oldvm = lp->lwp_vmspace;
3023 if (oldvm != newvm) {
3024 lp->lwp_vmspace = newvm;
3025 if (curthread->td_lwp == lp) {
3026 pmap = vmspace_pmap(newvm);
3027 atomic_set_cpumask(&pmap->pm_active, mycpu->gd_cpumask);
3028 #if defined(SWTCH_OPTIM_STATS)
3031 pmap = vmspace_pmap(oldvm);
3032 atomic_clear_cpumask(&pmap->pm_active, mycpu->gd_cpumask);
3040 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3043 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3047 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3052 * Used by kmalloc/kfree, page already exists at va
3055 pmap_kvtom(vm_offset_t va)
3059 KKASSERT(va >= KvaStart && va < KvaEnd);
3060 ptep = KernelPTA + (va >> PAGE_SHIFT);
3061 return(PHYS_TO_VM_PAGE(*ptep & PG_FRAME));
3065 pmap_object_init(vm_object_t object)
3071 pmap_object_free(vm_object_t object)