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);
225 ptdpg->wire_count = 1;
226 atomic_add_int(&vmstats.v_wire_count, 1);
228 /* not usually mapped */
229 vm_page_flag_clear(ptdpg, PG_MAPPED);
230 vm_page_wakeup(ptdpg);
232 pmap_kenter((vm_offset_t)pmap->pm_pdir, VM_PAGE_TO_PHYS(ptdpg));
233 pmap->pm_pdirpte = KernelPTA[(vm_offset_t)pmap->pm_pdir >> PAGE_SHIFT];
237 pmap->pm_ptphint = NULL;
238 pmap->pm_cpucachemask = 0;
239 TAILQ_INIT(&pmap->pm_pvlist);
240 TAILQ_INIT(&pmap->pm_pvlist_free);
241 spin_init(&pmap->pm_spin);
242 lwkt_token_init(&pmap->pm_token, "pmap_tok");
243 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
244 pmap->pm_stats.resident_count = 1;
248 * Clean up a pmap structure so it can be physically freed
253 pmap_puninit(pmap_t pmap)
256 kmem_free(&kernel_map, (vm_offset_t)pmap->pm_pdir, PAGE_SIZE);
257 pmap->pm_pdir = NULL;
259 if (pmap->pm_pteobj) {
260 vm_object_deallocate(pmap->pm_pteobj);
261 pmap->pm_pteobj = NULL;
267 * Wire in kernel global address entries. To avoid a race condition
268 * between pmap initialization and pmap_growkernel, this procedure
269 * adds the pmap to the master list (which growkernel scans to update),
270 * then copies the template.
272 * In a virtual kernel there are no kernel global address entries.
277 pmap_pinit2(struct pmap *pmap)
279 spin_lock(&pmap_spin);
280 TAILQ_INSERT_TAIL(&pmap_list, pmap, pm_pmnode);
281 spin_unlock(&pmap_spin);
285 * Release all resources held by the given physical map.
287 * Should only be called if the map contains no valid mappings.
289 * Caller must hold pmap->pm_token
291 static int pmap_release_callback(struct vm_page *p, void *data);
294 pmap_release(struct pmap *pmap)
296 struct mdglobaldata *gd = mdcpu;
297 vm_object_t object = pmap->pm_pteobj;
298 struct rb_vm_page_scan_info info;
300 KKASSERT(pmap != &kernel_pmap);
302 #if defined(DIAGNOSTIC)
303 if (object->ref_count != 1)
304 panic("pmap_release: pteobj reference count != 1");
307 * Once we destroy the page table, the mapping becomes invalid.
308 * Don't waste time doing a madvise to invalidate the mapping, just
309 * set cpucachemask to 0.
311 if (pmap->pm_pdir == gd->gd_PT1pdir) {
312 gd->gd_PT1pdir = NULL;
314 /* madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL); */
316 if (pmap->pm_pdir == gd->gd_PT2pdir) {
317 gd->gd_PT2pdir = NULL;
319 /* madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL); */
321 if (pmap->pm_pdir == gd->gd_PT3pdir) {
322 gd->gd_PT3pdir = NULL;
324 /* madvise(gd->gd_PT3map, SEG_SIZE, MADV_INVAL); */
328 info.object = object;
330 spin_lock(&pmap_spin);
331 TAILQ_REMOVE(&pmap_list, pmap, pm_pmnode);
332 spin_unlock(&pmap_spin);
334 vm_object_hold(object);
338 info.limit = object->generation;
340 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
341 pmap_release_callback, &info);
342 if (info.error == 0 && info.mpte) {
343 if (!pmap_release_free_page(pmap, info.mpte))
346 } while (info.error);
347 vm_object_drop(object);
350 * Leave the KVA reservation for pm_pdir cached for later reuse.
352 pmap->pm_pdirpte = 0;
353 pmap->pm_cpucachemask = 0;
357 * Callback to release a page table page backing a directory
361 pmap_release_callback(struct vm_page *p, void *data)
363 struct rb_vm_page_scan_info *info = data;
365 if (p->pindex == info->pmap->pm_pdindex) {
369 if (!pmap_release_free_page(info->pmap, p)) {
373 if (info->object->generation != info->limit) {
381 * Retire the given physical map from service. Should only be called if
382 * the map contains no valid mappings.
387 pmap_destroy(pmap_t pmap)
392 lwkt_gettoken(&vm_token);
393 if (--pmap->pm_count == 0) {
395 panic("destroying a pmap is not yet implemented");
397 lwkt_reltoken(&vm_token);
401 * Add a reference to the specified pmap.
406 pmap_reference(pmap_t pmap)
409 lwkt_gettoken(&vm_token);
411 lwkt_reltoken(&vm_token);
415 /************************************************************************
416 * VMSPACE MANAGEMENT *
417 ************************************************************************
419 * The VMSPACE management we do in our virtual kernel must be reflected
420 * in the real kernel. This is accomplished by making vmspace system
421 * calls to the real kernel.
424 cpu_vmspace_alloc(struct vmspace *vm)
429 #define LAST_EXTENT (VM_MAX_USER_ADDRESS - 0x80000000)
431 if (vmspace_create(&vm->vm_pmap, 0, NULL) < 0)
432 panic("vmspace_create() failed");
434 rp = vmspace_mmap(&vm->vm_pmap, (void *)0x00000000, 0x40000000,
435 PROT_READ|PROT_WRITE,
436 MAP_FILE|MAP_SHARED|MAP_VPAGETABLE|MAP_FIXED,
438 if (rp == MAP_FAILED)
439 panic("vmspace_mmap: failed1");
440 vmspace_mcontrol(&vm->vm_pmap, (void *)0x00000000, 0x40000000,
442 rp = vmspace_mmap(&vm->vm_pmap, (void *)0x40000000, 0x40000000,
443 PROT_READ|PROT_WRITE,
444 MAP_FILE|MAP_SHARED|MAP_VPAGETABLE|MAP_FIXED,
445 MemImageFd, 0x40000000);
446 if (rp == MAP_FAILED)
447 panic("vmspace_mmap: failed2");
448 vmspace_mcontrol(&vm->vm_pmap, (void *)0x40000000, 0x40000000,
450 rp = vmspace_mmap(&vm->vm_pmap, (void *)0x80000000, LAST_EXTENT,
451 PROT_READ|PROT_WRITE,
452 MAP_FILE|MAP_SHARED|MAP_VPAGETABLE|MAP_FIXED,
453 MemImageFd, 0x80000000);
454 vmspace_mcontrol(&vm->vm_pmap, (void *)0x80000000, LAST_EXTENT,
456 if (rp == MAP_FAILED)
457 panic("vmspace_mmap: failed3");
459 r = vmspace_mcontrol(&vm->vm_pmap, (void *)0x00000000, 0x40000000,
460 MADV_SETMAP, vmspace_pmap(vm)->pm_pdirpte);
462 panic("vmspace_mcontrol: failed1");
463 r = vmspace_mcontrol(&vm->vm_pmap, (void *)0x40000000, 0x40000000,
464 MADV_SETMAP, vmspace_pmap(vm)->pm_pdirpte);
466 panic("vmspace_mcontrol: failed2");
467 r = vmspace_mcontrol(&vm->vm_pmap, (void *)0x80000000, LAST_EXTENT,
468 MADV_SETMAP, vmspace_pmap(vm)->pm_pdirpte);
470 panic("vmspace_mcontrol: failed3");
474 cpu_vmspace_free(struct vmspace *vm)
476 if (vmspace_destroy(&vm->vm_pmap) < 0)
477 panic("vmspace_destroy() failed");
480 /************************************************************************
481 * Procedures which operate directly on the kernel PMAP *
482 ************************************************************************/
485 * This maps the requested page table and gives us access to it.
487 * This routine can be called from a potentially preempting interrupt
488 * thread or from a normal thread.
491 get_ptbase(struct pmap *pmap, vm_offset_t va)
493 struct mdglobaldata *gd = mdcpu;
495 if (pmap == &kernel_pmap) {
496 KKASSERT(va >= KvaStart && va < KvaEnd);
497 return(KernelPTA + (va >> PAGE_SHIFT));
498 } else if (pmap->pm_pdir == gd->gd_PT1pdir) {
499 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
500 *gd->gd_PT1pde = pmap->pm_pdirpte;
501 madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL);
502 atomic_set_cpumask(&pmap->pm_cpucachemask,
505 return(gd->gd_PT1map + (va >> PAGE_SHIFT));
506 } else if (pmap->pm_pdir == gd->gd_PT2pdir) {
507 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
508 *gd->gd_PT2pde = pmap->pm_pdirpte;
509 madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL);
510 atomic_set_cpumask(&pmap->pm_cpucachemask,
513 return(gd->gd_PT2map + (va >> PAGE_SHIFT));
517 * If we aren't running from a potentially preempting interrupt,
518 * load a new page table directory into the page table cache
520 if (gd->mi.gd_intr_nesting_level == 0 &&
521 (gd->mi.gd_curthread->td_flags & TDF_INTTHREAD) == 0) {
523 * Choose one or the other and map the page table
524 * in the KVA space reserved for it.
526 if ((gd->gd_PTflip = 1 - gd->gd_PTflip) == 0) {
527 gd->gd_PT1pdir = pmap->pm_pdir;
528 *gd->gd_PT1pde = pmap->pm_pdirpte;
529 madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL);
530 atomic_set_cpumask(&pmap->pm_cpucachemask,
532 return(gd->gd_PT1map + (va >> PAGE_SHIFT));
534 gd->gd_PT2pdir = pmap->pm_pdir;
535 *gd->gd_PT2pde = pmap->pm_pdirpte;
536 madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL);
537 atomic_set_cpumask(&pmap->pm_cpucachemask,
539 return(gd->gd_PT2map + (va >> PAGE_SHIFT));
544 * If we are running from a preempting interrupt use a private
545 * map. The caller must be in a critical section.
547 KKASSERT(IN_CRITICAL_SECT(curthread));
548 if (pmap->pm_pdir == gd->gd_PT3pdir) {
549 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
550 *gd->gd_PT3pde = pmap->pm_pdirpte;
551 madvise(gd->gd_PT3map, SEG_SIZE, MADV_INVAL);
552 atomic_set_cpumask(&pmap->pm_cpucachemask,
556 gd->gd_PT3pdir = pmap->pm_pdir;
557 *gd->gd_PT3pde = pmap->pm_pdirpte;
558 madvise(gd->gd_PT3map, SEG_SIZE, MADV_INVAL);
559 atomic_set_cpumask(&pmap->pm_cpucachemask,
562 return(gd->gd_PT3map + (va >> PAGE_SHIFT));
566 get_ptbase1(struct pmap *pmap, vm_offset_t va)
568 struct mdglobaldata *gd = mdcpu;
570 if (pmap == &kernel_pmap) {
571 KKASSERT(va >= KvaStart && va < KvaEnd);
572 return(KernelPTA + (va >> PAGE_SHIFT));
573 } else if (pmap->pm_pdir == gd->gd_PT1pdir) {
574 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
575 *gd->gd_PT1pde = pmap->pm_pdirpte;
576 madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL);
577 atomic_set_cpumask(&pmap->pm_cpucachemask,
580 return(gd->gd_PT1map + (va >> PAGE_SHIFT));
582 KKASSERT(gd->mi.gd_intr_nesting_level == 0 &&
583 (gd->mi.gd_curthread->td_flags & TDF_INTTHREAD) == 0);
584 gd->gd_PT1pdir = pmap->pm_pdir;
585 *gd->gd_PT1pde = pmap->pm_pdirpte;
586 madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL);
587 return(gd->gd_PT1map + (va >> PAGE_SHIFT));
591 get_ptbase2(struct pmap *pmap, vm_offset_t va)
593 struct mdglobaldata *gd = mdcpu;
595 if (pmap == &kernel_pmap) {
596 KKASSERT(va >= KvaStart && va < KvaEnd);
597 return(KernelPTA + (va >> PAGE_SHIFT));
598 } else if (pmap->pm_pdir == gd->gd_PT2pdir) {
599 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
600 *gd->gd_PT2pde = pmap->pm_pdirpte;
601 madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL);
602 atomic_set_cpumask(&pmap->pm_cpucachemask,
605 return(gd->gd_PT2map + (va >> PAGE_SHIFT));
607 KKASSERT(gd->mi.gd_intr_nesting_level == 0 &&
608 (gd->mi.gd_curthread->td_flags & TDF_INTTHREAD) == 0);
609 gd->gd_PT2pdir = pmap->pm_pdir;
610 *gd->gd_PT2pde = pmap->pm_pdirpte;
611 madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL);
612 return(gd->gd_PT2map + (va >> PAGE_SHIFT));
616 * Return a pointer to the page table entry for the specified va in the
617 * specified pmap. NULL is returned if there is no valid page table page
620 static __inline vpte_t *
621 pmap_pte(struct pmap *pmap, vm_offset_t va)
625 ptep = &pmap->pm_pdir[va >> SEG_SHIFT];
629 return (get_ptbase(pmap, va));
635 * Enter a mapping into kernel_pmap. Mappings created in this fashion
636 * are not managed. Mappings must be immediately accessible on all cpus.
638 * Call pmap_inval_pte() to invalidate the virtual pte and clean out the
639 * real pmap and handle related races before storing the new vpte.
642 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
647 KKASSERT(va >= KvaStart && va < KvaEnd);
648 npte = (vpte_t)pa | VPTE_R | VPTE_W | VPTE_V;
649 ptep = KernelPTA + (va >> PAGE_SHIFT);
651 pmap_inval_pte(ptep, &kernel_pmap, va);
656 * Synchronize a kvm mapping originally made for the private use on
657 * some other cpu so it can be used on all cpus.
659 * XXX add MADV_RESYNC to improve performance.
662 pmap_kenter_sync(vm_offset_t va)
664 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
668 * Synchronize a kvm mapping originally made for the private use on
669 * some other cpu so it can be used on our cpu. Turns out to be the
670 * same madvise() call, because we have to sync the real pmaps anyway.
672 * XXX add MADV_RESYNC to improve performance.
675 pmap_kenter_sync_quick(vm_offset_t va)
677 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
682 * Make a previously read-only kernel mapping R+W (not implemented by
686 pmap_kmodify_rw(vm_offset_t va)
688 *pmap_kpte(va) |= VPTE_R | VPTE_W;
689 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
693 * Make a kernel mapping non-cacheable (not applicable to virtual kernels)
696 pmap_kmodify_nc(vm_offset_t va)
698 *pmap_kpte(va) |= VPTE_N;
699 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
705 * Map a contiguous range of physical memory to a KVM
708 pmap_map(vm_offset_t *virtp, vm_paddr_t start, vm_paddr_t end, int prot)
710 vm_offset_t sva, virt;
713 while (start < end) {
714 pmap_kenter(virt, start);
723 pmap_kpte(vm_offset_t va)
727 KKASSERT(va >= KvaStart && va < KvaEnd);
728 ptep = KernelPTA + (va >> PAGE_SHIFT);
733 * Enter an unmanaged KVA mapping for the private use of the current
734 * cpu only. pmap_kenter_sync() may be called to make the mapping usable
737 * It is illegal for the mapping to be accessed by other cpus unleess
738 * pmap_kenter_sync*() is called.
741 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
746 KKASSERT(va >= KvaStart && va < KvaEnd);
748 npte = (vpte_t)pa | VPTE_R | VPTE_W | VPTE_V;
749 ptep = KernelPTA + (va >> PAGE_SHIFT);
751 pmap_inval_pte_quick(ptep, &kernel_pmap, va);
756 * Make a temporary mapping for a physical address. This is only intended
757 * to be used for panic dumps.
759 * The caller is responsible for calling smp_invltlb().
762 pmap_kenter_temporary(vm_paddr_t pa, long i)
764 pmap_kenter_quick(crashdumpmap + (i * PAGE_SIZE), pa);
765 return ((void *)crashdumpmap);
769 * Remove an unmanaged mapping created with pmap_kenter*().
772 pmap_kremove(vm_offset_t va)
776 KKASSERT(va >= KvaStart && va < KvaEnd);
778 ptep = KernelPTA + (va >> PAGE_SHIFT);
780 pmap_inval_pte(ptep, &kernel_pmap, va);
785 * Remove an unmanaged mapping created with pmap_kenter*() but synchronize
786 * only with this cpu.
788 * Unfortunately because we optimize new entries by testing VPTE_V later
789 * on, we actually still have to synchronize with all the cpus. XXX maybe
790 * store a junk value and test against 0 in the other places instead?
793 pmap_kremove_quick(vm_offset_t va)
797 KKASSERT(va >= KvaStart && va < KvaEnd);
799 ptep = KernelPTA + (va >> PAGE_SHIFT);
801 pmap_inval_pte(ptep, &kernel_pmap, va); /* NOT _quick */
806 * Extract the physical address from the kernel_pmap that is associated
807 * with the specified virtual address.
810 pmap_kextract(vm_offset_t va)
815 KKASSERT(va >= KvaStart && va < KvaEnd);
817 ptep = KernelPTA + (va >> PAGE_SHIFT);
818 pa = (vm_paddr_t)(*ptep & VPTE_FRAME) | (va & PAGE_MASK);
823 * Map a set of unmanaged VM pages into KVM.
826 pmap_qenter(vm_offset_t va, struct vm_page **m, int count)
828 KKASSERT(va >= KvaStart && va + count * PAGE_SIZE < KvaEnd);
832 ptep = KernelPTA + (va >> PAGE_SHIFT);
834 pmap_inval_pte(ptep, &kernel_pmap, va);
835 *ptep = (vpte_t)(*m)->phys_addr | VPTE_R | VPTE_W | VPTE_V;
843 * Undo the effects of pmap_qenter*().
846 pmap_qremove(vm_offset_t va, int count)
848 KKASSERT(va >= KvaStart && va + count * PAGE_SIZE < KvaEnd);
852 ptep = KernelPTA + (va >> PAGE_SHIFT);
854 pmap_inval_pte(ptep, &kernel_pmap, va);
861 /************************************************************************
862 * Misc support glue called by machine independant code *
863 ************************************************************************
865 * These routines are called by machine independant code to operate on
866 * certain machine-dependant aspects of processes, threads, and pmaps.
870 * Initialize MD portions of the thread structure.
873 pmap_init_thread(thread_t td)
875 /* enforce pcb placement */
876 td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
877 td->td_savefpu = &td->td_pcb->pcb_save;
878 td->td_sp = (char *)td->td_pcb - 16;
882 * This routine directly affects the fork perf for a process.
885 pmap_init_proc(struct proc *p)
890 * Destroy the UPAGES for a process that has exited and disassociate
891 * the process from its thread.
894 pmap_dispose_proc(struct proc *p)
896 KASSERT(p->p_lock == 0, ("attempt to dispose referenced proc! %p", p));
900 * We pre-allocate all page table pages for kernel virtual memory so
901 * this routine will only be called if KVM has been exhausted.
906 pmap_growkernel(vm_offset_t kstart, vm_offset_t kend)
910 addr = (kend + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
912 lwkt_gettoken(&vm_token);
913 if (addr > virtual_end - SEG_SIZE)
914 panic("KVM exhausted");
915 kernel_vm_end = addr;
916 lwkt_reltoken(&vm_token);
920 * The modification bit is not tracked for any pages in this range. XXX
921 * such pages in this maps should always use pmap_k*() functions and not
924 * XXX User and kernel address spaces are independant for virtual kernels,
925 * this function only applies to the kernel pmap.
928 pmap_track_modified(pmap_t pmap, vm_offset_t va)
930 if (pmap != &kernel_pmap)
932 if ((va < clean_sva) || (va >= clean_eva))
938 /************************************************************************
939 * Procedures supporting managed page table pages *
940 ************************************************************************
942 * These procedures are used to track managed page table pages. These pages
943 * use the page table page's vm_page_t to track PTEs in the page. The
944 * page table pages themselves are arranged in a VM object, pmap->pm_pteobj.
946 * This allows the system to throw away page table pages for user processes
947 * at will and reinstantiate them on demand.
951 * This routine works like vm_page_lookup() but also blocks as long as the
952 * page is busy. This routine does not busy the page it returns.
954 * Unless the caller is managing objects whos pages are in a known state,
955 * the call should be made with a critical section held so the page's object
956 * association remains valid on return.
959 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
963 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
964 m = vm_page_lookup_busy_wait(object, pindex, FALSE, "pplookp");
970 * This routine unholds page table pages, and if the hold count
971 * drops to zero, then it decrements the wire count.
973 * We must recheck that this is the last hold reference after busy-sleeping
977 _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
979 vm_page_busy_wait(m, FALSE, "pmuwpt");
980 KASSERT(m->queue == PQ_NONE,
981 ("_pmap_unwire_pte_hold: %p->queue != PQ_NONE", m));
983 if (m->hold_count == 1) {
985 * Unmap the page table page.
987 KKASSERT(pmap->pm_pdir[m->pindex] != 0);
988 pmap_inval_pde(&pmap->pm_pdir[m->pindex], pmap,
989 (vm_offset_t)m->pindex << SEG_SHIFT);
990 KKASSERT(pmap->pm_stats.resident_count > 0);
991 --pmap->pm_stats.resident_count;
993 if (pmap->pm_ptphint == m)
994 pmap->pm_ptphint = NULL;
997 * This was our last hold, the page had better be unwired
998 * after we decrement wire_count.
1000 * FUTURE NOTE: shared page directory page could result in
1001 * multiple wire counts.
1005 KKASSERT(m->wire_count == 0);
1006 atomic_add_int(&vmstats.v_wire_count, -1);
1007 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1009 vm_page_free_zero(m);
1012 KKASSERT(m->hold_count > 1);
1020 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
1022 KKASSERT(m->hold_count > 0);
1023 if (m->hold_count > 1) {
1027 return _pmap_unwire_pte_hold(pmap, m);
1032 * After removing a page table entry, this routine is used to
1033 * conditionally free the page, and manage the hold/wire counts.
1036 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte)
1040 ASSERT_LWKT_TOKEN_HELD(vm_object_token(pmap->pm_pteobj));
1044 * page table pages in the kernel_pmap are not managed.
1046 if (pmap == &kernel_pmap)
1048 ptepindex = (va >> PDRSHIFT);
1049 if (pmap->pm_ptphint &&
1050 (pmap->pm_ptphint->pindex == ptepindex)) {
1051 mpte = pmap->pm_ptphint;
1053 mpte = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1054 pmap->pm_ptphint = mpte;
1055 vm_page_wakeup(mpte);
1058 return pmap_unwire_pte_hold(pmap, mpte);
1062 * Attempt to release and free the vm_page backing a page directory page
1063 * in a pmap. Returns 1 on success, 0 on failure (if the procedure had
1067 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1069 vpte_t *pde = pmap->pm_pdir;
1072 * This code optimizes the case of freeing non-busy
1073 * page-table pages. Those pages are zero now, and
1074 * might as well be placed directly into the zero queue.
1076 if (vm_page_busy_try(p, FALSE)) {
1077 vm_page_sleep_busy(p, FALSE, "pmaprl");
1080 KKASSERT(pmap->pm_stats.resident_count > 0);
1081 --pmap->pm_stats.resident_count;
1083 if (p->hold_count) {
1084 panic("pmap_release: freeing held page table page");
1087 * Page directory pages need to have the kernel stuff cleared, so
1088 * they can go into the zero queue also.
1090 * In virtual kernels there is no 'kernel stuff'. For the moment
1091 * I just make sure the whole thing has been zero'd even though
1092 * it should already be completely zero'd.
1094 * pmaps for vkernels do not self-map because they do not share
1095 * their address space with the vkernel. Clearing of pde[] thus
1096 * only applies to page table pages and not to the page directory
1099 if (p->pindex == pmap->pm_pdindex) {
1100 bzero(pde, VPTE_PAGETABLE_SIZE);
1101 pmap_kremove((vm_offset_t)pmap->pm_pdir);
1103 KKASSERT(pde[p->pindex] != 0);
1104 pmap_inval_pde(&pde[p->pindex], pmap,
1105 (vm_offset_t)p->pindex << SEG_SHIFT);
1109 * Clear the matching hint
1111 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1112 pmap->pm_ptphint = NULL;
1115 * And throw the page away. The page is completely zero'd out so
1116 * optimize the free call.
1119 atomic_add_int(&vmstats.v_wire_count, -1);
1120 vm_page_free_zero(p);
1125 * This routine is called if the page table page is not mapped in the page
1128 * The routine is broken up into two parts for readability.
1130 * It must return a held mpte and map the page directory page as required.
1131 * Because vm_page_grab() can block, we must re-check pm_pdir[ptepindex]
1134 _pmap_allocpte(pmap_t pmap, unsigned ptepindex)
1140 * Find or fabricate a new pagetable page. A busied page will be
1141 * returned. This call may block.
1143 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1144 VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1145 vm_page_flag_set(m, PG_MAPPED);
1147 KASSERT(m->queue == PQ_NONE,
1148 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1151 * Increment the hold count for the page we will be returning to
1157 * It is possible that someone else got in and mapped by the page
1158 * directory page while we were blocked, if so just unbusy and
1159 * return the held page.
1161 if ((ptepa = pmap->pm_pdir[ptepindex]) != 0) {
1162 KKASSERT((ptepa & VPTE_FRAME) == VM_PAGE_TO_PHYS(m));
1167 if (m->wire_count == 0)
1168 atomic_add_int(&vmstats.v_wire_count, 1);
1172 * Map the pagetable page into the process address space, if
1173 * it isn't already there.
1175 ++pmap->pm_stats.resident_count;
1177 ptepa = VM_PAGE_TO_PHYS(m);
1178 pmap->pm_pdir[ptepindex] = (vpte_t)ptepa | VPTE_R | VPTE_W | VPTE_V |
1182 * We are likely about to access this page table page, so set the
1183 * page table hint to reduce overhead.
1185 pmap->pm_ptphint = m;
1193 * Determine the page table page required to access the VA in the pmap
1194 * and allocate it if necessary. Return a held vm_page_t for the page.
1196 * Only used with user pmaps.
1199 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1205 ASSERT_LWKT_TOKEN_HELD(vm_object_token(pmap->pm_pteobj));
1208 * Calculate pagetable page index
1210 ptepindex = va >> PDRSHIFT;
1213 * Get the page directory entry
1215 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1218 * This supports switching from a 4MB page to a
1221 if (ptepa & VPTE_PS) {
1222 KKASSERT(pmap->pm_pdir[ptepindex] != 0);
1223 pmap_inval_pde(&pmap->pm_pdir[ptepindex], pmap,
1224 (vm_offset_t)ptepindex << SEG_SHIFT);
1229 * If the page table page is mapped, we just increment the
1230 * hold count, and activate it.
1234 * In order to get the page table page, try the
1237 if (pmap->pm_ptphint &&
1238 (pmap->pm_ptphint->pindex == ptepindex)) {
1239 m = pmap->pm_ptphint;
1241 m = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1242 pmap->pm_ptphint = m;
1249 * Here if the pte page isn't mapped, or if it has been deallocated.
1251 return _pmap_allocpte(pmap, ptepindex);
1254 /************************************************************************
1255 * Managed pages in pmaps *
1256 ************************************************************************
1258 * All pages entered into user pmaps and some pages entered into the kernel
1259 * pmap are managed, meaning that pmap_protect() and other related management
1260 * functions work on these pages.
1264 * free the pv_entry back to the free list. This function may be
1265 * called from an interrupt.
1267 static __inline void
1268 free_pv_entry(pv_entry_t pv)
1275 * get a new pv_entry, allocating a block from the system
1276 * when needed. This function may be called from an interrupt.
1282 if (pv_entry_high_water &&
1283 (pv_entry_count > pv_entry_high_water) &&
1284 (pmap_pagedaemon_waken == 0)) {
1285 pmap_pagedaemon_waken = 1;
1286 wakeup (&vm_pages_needed);
1288 return zalloc(&pvzone);
1292 * This routine is very drastic, but can save the system
1302 static int warningdone=0;
1304 if (pmap_pagedaemon_waken == 0)
1306 lwkt_gettoken(&vm_token);
1307 pmap_pagedaemon_waken = 0;
1309 if (warningdone < 5) {
1310 kprintf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1314 for (i = 0; i < vm_page_array_size; i++) {
1315 m = &vm_page_array[i];
1316 if (m->wire_count || m->hold_count)
1318 if (vm_page_busy_try(m, TRUE) == 0) {
1319 if (m->wire_count == 0 && m->hold_count == 0) {
1325 lwkt_reltoken(&vm_token);
1329 * If it is the first entry on the list, it is actually
1330 * in the header and we must copy the following entry up
1331 * to the header. Otherwise we must search the list for
1332 * the entry. In either case we free the now unused entry.
1334 * caller must hold vm_token
1337 pmap_remove_entry(struct pmap *pmap, vm_page_t m, vm_offset_t va)
1343 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1344 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1345 if (pmap == pv->pv_pmap && va == pv->pv_va)
1349 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1350 if (va == pv->pv_va)
1356 * Note that pv_ptem is NULL if the page table page itself is not
1357 * managed, even if the page being removed IS managed.
1361 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1362 m->md.pv_list_count--;
1363 atomic_add_int(&m->object->agg_pv_list_count, -1);
1364 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1365 if (TAILQ_EMPTY(&m->md.pv_list))
1366 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1367 ++pmap->pm_generation;
1368 vm_object_hold(pmap->pm_pteobj);
1369 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem);
1370 vm_object_drop(pmap->pm_pteobj);
1378 * Create a pv entry for page at pa for (pmap, va). If the page table page
1379 * holding the VA is managed, mpte will be non-NULL.
1382 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
1387 pv = get_pv_entry();
1392 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1393 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1394 ++pmap->pm_generation;
1395 m->md.pv_list_count++;
1396 atomic_add_int(&m->object->agg_pv_list_count, 1);
1402 * pmap_remove_pte: do the things to unmap a page in a process
1405 pmap_remove_pte(struct pmap *pmap, vpte_t *ptq, vm_offset_t va)
1410 oldpte = pmap_inval_loadandclear(ptq, pmap, va);
1411 if (oldpte & VPTE_WIRED)
1412 --pmap->pm_stats.wired_count;
1413 KKASSERT(pmap->pm_stats.wired_count >= 0);
1417 * Machines that don't support invlpg, also don't support
1418 * VPTE_G. XXX VPTE_G is disabled for SMP so don't worry about
1421 if (oldpte & VPTE_G)
1422 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
1424 KKASSERT(pmap->pm_stats.resident_count > 0);
1425 --pmap->pm_stats.resident_count;
1426 if (oldpte & VPTE_MANAGED) {
1427 m = PHYS_TO_VM_PAGE(oldpte);
1428 if (oldpte & VPTE_M) {
1429 #if defined(PMAP_DIAGNOSTIC)
1430 if (pmap_nw_modified((pt_entry_t) oldpte)) {
1432 "pmap_remove: modified page not writable: va: 0x%x, pte: 0x%x\n",
1436 if (pmap_track_modified(pmap, va))
1439 if (oldpte & VPTE_A)
1440 vm_page_flag_set(m, PG_REFERENCED);
1441 return pmap_remove_entry(pmap, m, va);
1443 return pmap_unuse_pt(pmap, va, NULL);
1452 * Remove a single page from a process address space.
1454 * This function may not be called from an interrupt if the pmap is
1458 pmap_remove_page(struct pmap *pmap, vm_offset_t va)
1463 * if there is no pte for this address, just skip it!!! Otherwise
1464 * get a local va for mappings for this pmap and remove the entry.
1466 if (*pmap_pde(pmap, va) != 0) {
1467 ptq = get_ptbase(pmap, va);
1469 pmap_remove_pte(pmap, ptq, va);
1475 * Remove the given range of addresses from the specified map.
1477 * It is assumed that the start and end are properly rounded to the
1480 * This function may not be called from an interrupt if the pmap is
1486 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
1490 vm_offset_t ptpaddr;
1491 vm_pindex_t sindex, eindex;
1496 vm_object_hold(pmap->pm_pteobj);
1497 lwkt_gettoken(&vm_token);
1498 KKASSERT(pmap->pm_stats.resident_count >= 0);
1499 if (pmap->pm_stats.resident_count == 0) {
1500 lwkt_reltoken(&vm_token);
1501 vm_object_drop(pmap->pm_pteobj);
1506 * special handling of removing one page. a very
1507 * common operation and easy to short circuit some
1510 if (((sva + PAGE_SIZE) == eva) &&
1511 ((pmap->pm_pdir[(sva >> PDRSHIFT)] & VPTE_PS) == 0)) {
1512 pmap_remove_page(pmap, sva);
1513 lwkt_reltoken(&vm_token);
1514 vm_object_drop(pmap->pm_pteobj);
1519 * Get a local virtual address for the mappings that are being
1522 * XXX this is really messy because the kernel pmap is not relative
1525 sindex = (sva >> PAGE_SHIFT);
1526 eindex = (eva >> PAGE_SHIFT);
1528 for (; sindex < eindex; sindex = pdnxt) {
1532 * Calculate index for next page table.
1534 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1535 if (pmap->pm_stats.resident_count == 0)
1538 pdirindex = sindex / NPDEPG;
1539 if (((ptpaddr = pmap->pm_pdir[pdirindex]) & VPTE_PS) != 0) {
1540 KKASSERT(pmap->pm_pdir[pdirindex] != 0);
1541 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1542 pmap_inval_pde(&pmap->pm_pdir[pdirindex], pmap,
1543 (vm_offset_t)pdirindex << SEG_SHIFT);
1548 * Weed out invalid mappings. Note: we assume that the page
1549 * directory table is always allocated, and in kernel virtual.
1555 * Limit our scan to either the end of the va represented
1556 * by the current page table page, or to the end of the
1557 * range being removed.
1563 * NOTE: pmap_remove_pte() can block.
1565 for (; sindex != pdnxt; sindex++) {
1568 ptbase = get_ptbase(pmap, sindex << PAGE_SHIFT);
1571 va = i386_ptob(sindex);
1572 if (pmap_remove_pte(pmap, ptbase, va))
1576 lwkt_reltoken(&vm_token);
1577 vm_object_drop(pmap->pm_pteobj);
1581 * Removes this physical page from all physical maps in which it resides.
1582 * Reflects back modify bits to the pager.
1584 * This routine may not be called from an interrupt.
1589 pmap_remove_all(vm_page_t m)
1594 #if defined(PMAP_DIAGNOSTIC)
1596 * XXX this makes pmap_page_protect(NONE) illegal for non-managed
1599 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
1600 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%08llx", (long long)VM_PAGE_TO_PHYS(m));
1604 lwkt_gettoken(&vm_token);
1605 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
1606 KKASSERT(pv->pv_pmap->pm_stats.resident_count > 0);
1607 --pv->pv_pmap->pm_stats.resident_count;
1609 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
1610 KKASSERT(pte != NULL);
1612 tpte = pmap_inval_loadandclear(pte, pv->pv_pmap, pv->pv_va);
1613 if (tpte & VPTE_WIRED)
1614 --pv->pv_pmap->pm_stats.wired_count;
1615 KKASSERT(pv->pv_pmap->pm_stats.wired_count >= 0);
1618 vm_page_flag_set(m, PG_REFERENCED);
1621 * Update the vm_page_t clean and reference bits.
1623 if (tpte & VPTE_M) {
1624 #if defined(PMAP_DIAGNOSTIC)
1625 if (pmap_nw_modified((pt_entry_t) tpte)) {
1627 "pmap_remove_all: modified page not writable: va: 0x%x, pte: 0x%x\n",
1631 if (pmap_track_modified(pv->pv_pmap, pv->pv_va))
1634 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1635 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
1636 ++pv->pv_pmap->pm_generation;
1637 m->md.pv_list_count--;
1638 atomic_add_int(&m->object->agg_pv_list_count, -1);
1639 if (TAILQ_EMPTY(&m->md.pv_list))
1640 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1641 vm_object_hold(pv->pv_pmap->pm_pteobj);
1642 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
1643 vm_object_drop(pv->pv_pmap->pm_pteobj);
1646 KKASSERT((m->flags & (PG_MAPPED | PG_WRITEABLE)) == 0);
1647 lwkt_reltoken(&vm_token);
1651 * Set the physical protection on the specified range of this map
1654 * This function may not be called from an interrupt if the map is
1655 * not the kernel_pmap.
1660 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
1664 vm_offset_t pdnxt, ptpaddr;
1665 vm_pindex_t sindex, eindex;
1671 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1672 pmap_remove(pmap, sva, eva);
1676 if (prot & VM_PROT_WRITE)
1679 lwkt_gettoken(&vm_token);
1680 ptbase = get_ptbase(pmap, sva);
1682 sindex = (sva >> PAGE_SHIFT);
1683 eindex = (eva >> PAGE_SHIFT);
1686 for (; sindex < eindex; sindex = pdnxt) {
1690 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1692 pdirindex = sindex / NPDEPG;
1695 * Clear the modified and writable bits for a 4m page.
1696 * Throw away the modified bit (?)
1698 if (((ptpaddr = pmap->pm_pdir[pdirindex]) & VPTE_PS) != 0) {
1699 pmap_clean_pde(&pmap->pm_pdir[pdirindex], pmap,
1700 (vm_offset_t)pdirindex << SEG_SHIFT);
1701 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1706 * Weed out invalid mappings. Note: we assume that the page
1707 * directory table is always allocated, and in kernel virtual.
1712 if (pdnxt > eindex) {
1716 for (; sindex != pdnxt; sindex++) {
1721 * Clean managed pages and also check the accessed
1722 * bit. Just remove write perms for unmanaged
1723 * pages. Be careful of races, turning off write
1724 * access will force a fault rather then setting
1725 * the modified bit at an unexpected time.
1727 ptep = &ptbase[sindex - sbase];
1728 if (*ptep & VPTE_MANAGED) {
1729 pbits = pmap_clean_pte(ptep, pmap,
1732 if (pbits & VPTE_A) {
1733 m = PHYS_TO_VM_PAGE(pbits);
1734 vm_page_flag_set(m, PG_REFERENCED);
1735 atomic_clear_long(ptep, VPTE_A);
1737 if (pbits & VPTE_M) {
1738 if (pmap_track_modified(pmap, i386_ptob(sindex))) {
1740 m = PHYS_TO_VM_PAGE(pbits);
1745 pbits = pmap_setro_pte(ptep, pmap,
1750 lwkt_reltoken(&vm_token);
1754 * Enter a managed page into a pmap. If the page is not wired related pmap
1755 * data can be destroyed at any time for later demand-operation.
1757 * Insert the vm_page (m) at virtual address (v) in (pmap), with the
1758 * specified protection, and wire the mapping if requested.
1760 * NOTE: This routine may not lazy-evaluate or lose information. The
1761 * page must actually be inserted into the given map NOW.
1763 * NOTE: When entering a page at a KVA address, the pmap must be the
1769 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1775 vpte_t origpte, newpte;
1783 vm_object_hold(pmap->pm_pteobj);
1784 lwkt_gettoken(&vm_token);
1787 * Get the page table page. The kernel_pmap's page table pages
1788 * are preallocated and have no associated vm_page_t.
1790 if (pmap == &kernel_pmap)
1793 mpte = pmap_allocpte(pmap, va);
1795 pte = pmap_pte(pmap, va);
1798 * Page Directory table entry not valid, we need a new PT page
1799 * and pmap_allocpte() didn't give us one. Oops!
1802 panic("pmap_enter: invalid page directory pmap=%p, va=0x%p\n",
1807 * Deal with races on the original mapping (though don't worry
1808 * about VPTE_A races) by cleaning it. This will force a fault
1809 * if an attempt is made to write to the page.
1811 pa = VM_PAGE_TO_PHYS(m) & VPTE_FRAME;
1812 origpte = pmap_clean_pte(pte, pmap, va);
1813 opa = origpte & VPTE_FRAME;
1815 if (origpte & VPTE_PS)
1816 panic("pmap_enter: attempted pmap_enter on 4MB page");
1819 * Mapping has not changed, must be protection or wiring change.
1821 if (origpte && (opa == pa)) {
1823 * Wiring change, just update stats. We don't worry about
1824 * wiring PT pages as they remain resident as long as there
1825 * are valid mappings in them. Hence, if a user page is wired,
1826 * the PT page will be also.
1828 if (wired && ((origpte & VPTE_WIRED) == 0))
1829 ++pmap->pm_stats.wired_count;
1830 else if (!wired && (origpte & VPTE_WIRED))
1831 --pmap->pm_stats.wired_count;
1832 KKASSERT(pmap->pm_stats.wired_count >= 0);
1835 * Remove the extra pte reference. Note that we cannot
1836 * optimize the RO->RW case because we have adjusted the
1837 * wiring count above and may need to adjust the wiring
1844 * We might be turning off write access to the page,
1845 * so we go ahead and sense modify status.
1847 if (origpte & VPTE_MANAGED) {
1848 if ((origpte & VPTE_M) &&
1849 pmap_track_modified(pmap, va)) {
1851 om = PHYS_TO_VM_PAGE(opa);
1855 KKASSERT(m->flags & PG_MAPPED);
1860 * Mapping has changed, invalidate old range and fall through to
1861 * handle validating new mapping.
1865 err = pmap_remove_pte(pmap, pte, va);
1867 panic("pmap_enter: pte vanished, va: %p", (void *)va);
1868 pte = pmap_pte(pmap, va);
1869 origpte = pmap_clean_pte(pte, pmap, va);
1870 opa = origpte & VPTE_FRAME;
1872 kprintf("pmap_enter: Warning, raced pmap %p va %p\n",
1878 * Enter on the PV list if part of our managed memory. Note that we
1879 * raise IPL while manipulating pv_table since pmap_enter can be
1880 * called at interrupt time.
1882 if (pmap_initialized &&
1883 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
1884 pmap_insert_entry(pmap, va, mpte, m);
1886 vm_page_flag_set(m, PG_MAPPED);
1890 * Increment counters
1892 ++pmap->pm_stats.resident_count;
1894 pmap->pm_stats.wired_count++;
1898 * Now validate mapping with desired protection/wiring.
1900 newpte = (vm_offset_t) (pa | pte_prot(pmap, prot) | VPTE_V);
1903 newpte |= VPTE_WIRED;
1904 if (pmap != &kernel_pmap)
1908 * If the mapping or permission bits are different from the
1909 * (now cleaned) original pte, an update is needed. We've
1910 * already downgraded or invalidated the page so all we have
1911 * to do now is update the bits.
1913 * XXX should we synchronize RO->RW changes to avoid another
1916 if ((origpte & ~(VPTE_W|VPTE_M|VPTE_A)) != newpte) {
1917 *pte = newpte | VPTE_A;
1918 if (newpte & VPTE_W)
1919 vm_page_flag_set(m, PG_WRITEABLE);
1921 KKASSERT((newpte & VPTE_MANAGED) == 0 || m->flags & PG_MAPPED);
1922 lwkt_reltoken(&vm_token);
1923 vm_object_drop(pmap->pm_pteobj);
1927 * This code works like pmap_enter() but assumes VM_PROT_READ and not-wired.
1929 * Currently this routine may only be used on user pmaps, not kernel_pmap.
1932 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m)
1940 KKASSERT(pmap != &kernel_pmap);
1942 KKASSERT(va >= VM_MIN_USER_ADDRESS && va < VM_MAX_USER_ADDRESS);
1945 * Calculate pagetable page (mpte), allocating it if necessary.
1947 * A held page table page (mpte), or NULL, is passed onto the
1948 * section following.
1950 ptepindex = va >> PDRSHIFT;
1952 vm_object_hold(pmap->pm_pteobj);
1953 lwkt_gettoken(&vm_token);
1957 * Get the page directory entry
1959 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1962 * If the page table page is mapped, we just increment
1963 * the hold count, and activate it.
1966 if (ptepa & VPTE_PS)
1967 panic("pmap_enter_quick: unexpected mapping into 4MB page");
1968 if (pmap->pm_ptphint &&
1969 (pmap->pm_ptphint->pindex == ptepindex)) {
1970 mpte = pmap->pm_ptphint;
1972 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1973 pmap->pm_ptphint = mpte;
1974 vm_page_wakeup(mpte);
1979 mpte = _pmap_allocpte(pmap, ptepindex);
1981 } while (mpte == NULL);
1984 * Ok, now that the page table page has been validated, get the pte.
1985 * If the pte is already mapped undo mpte's hold_count and
1988 pte = pmap_pte(pmap, va);
1990 pmap_unwire_pte_hold(pmap, mpte);
1991 lwkt_reltoken(&vm_token);
1992 vm_object_drop(pmap->pm_pteobj);
1997 * Enter on the PV list if part of our managed memory. Note that we
1998 * raise IPL while manipulating pv_table since pmap_enter can be
1999 * called at interrupt time.
2001 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2002 pmap_insert_entry(pmap, va, mpte, m);
2003 vm_page_flag_set(m, PG_MAPPED);
2007 * Increment counters
2009 ++pmap->pm_stats.resident_count;
2011 pa = VM_PAGE_TO_PHYS(m);
2014 * Now validate mapping with RO protection
2016 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2017 *pte = (vpte_t)pa | VPTE_V | VPTE_U;
2019 *pte = (vpte_t)pa | VPTE_V | VPTE_U | VPTE_MANAGED;
2020 /*pmap_inval_add(&info, pmap, va); shouldn't be needed 0->valid */
2021 /*pmap_inval_flush(&info); don't need for vkernel */
2022 lwkt_reltoken(&vm_token);
2023 vm_object_drop(pmap->pm_pteobj);
2027 * Extract the physical address for the translation at the specified
2028 * virtual address in the pmap.
2030 * The caller must hold vm_token if non-blocking operation is desired.
2034 pmap_extract(pmap_t pmap, vm_offset_t va)
2039 lwkt_gettoken(&vm_token);
2040 if (pmap && (pte = pmap->pm_pdir[va >> SEG_SHIFT]) != 0) {
2041 if (pte & VPTE_PS) {
2042 rtval = pte & ~((vpte_t)(1 << SEG_SHIFT) - 1);
2043 rtval |= va & SEG_MASK;
2045 pte = *get_ptbase(pmap, va);
2046 rtval = (pte & VPTE_FRAME) | (va & PAGE_MASK);
2051 lwkt_reltoken(&vm_token);
2055 #define MAX_INIT_PT (96)
2058 * This routine preloads the ptes for a given object into the specified pmap.
2059 * This eliminates the blast of soft faults on process startup and
2060 * immediately after an mmap.
2064 static int pmap_object_init_pt_callback(vm_page_t p, void *data);
2067 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2068 vm_object_t object, vm_pindex_t pindex,
2069 vm_size_t size, int limit)
2071 struct rb_vm_page_scan_info info;
2076 * We can't preinit if read access isn't set or there is no pmap
2079 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2083 * We can't preinit if the pmap is not the current pmap
2085 lp = curthread->td_lwp;
2086 if (lp == NULL || pmap != vmspace_pmap(lp->lwp_vmspace))
2089 psize = size >> PAGE_SHIFT;
2091 if ((object->type != OBJT_VNODE) ||
2092 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2093 (object->resident_page_count > MAX_INIT_PT))) {
2097 if (psize + pindex > object->size) {
2098 if (object->size < pindex)
2100 psize = object->size - pindex;
2107 * Use a red-black scan to traverse the requested range and load
2108 * any valid pages found into the pmap.
2110 * We cannot safely scan the object's memq unless we are in a
2111 * critical section since interrupts can remove pages from objects.
2113 info.start_pindex = pindex;
2114 info.end_pindex = pindex + psize - 1;
2120 vm_object_hold(object);
2121 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2122 pmap_object_init_pt_callback, &info);
2123 vm_object_drop(object);
2127 * The caller must hold vm_token.
2131 pmap_object_init_pt_callback(vm_page_t p, void *data)
2133 struct rb_vm_page_scan_info *info = data;
2134 vm_pindex_t rel_index;
2137 * don't allow an madvise to blow away our really
2138 * free pages allocating pv entries.
2140 if ((info->limit & MAP_PREFAULT_MADVISE) &&
2141 vmstats.v_free_count < vmstats.v_free_reserved) {
2144 if (vm_page_busy_try(p, TRUE))
2146 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2147 (p->flags & PG_FICTITIOUS) == 0) {
2148 if ((p->queue - p->pc) == PQ_CACHE)
2149 vm_page_deactivate(p);
2150 rel_index = p->pindex - info->start_pindex;
2151 pmap_enter_quick(info->pmap,
2152 info->addr + i386_ptob(rel_index), p);
2159 * Return TRUE if the pmap is in shape to trivially
2160 * pre-fault the specified address.
2162 * Returns FALSE if it would be non-trivial or if a
2163 * pte is already loaded into the slot.
2168 pmap_prefault_ok(pmap_t pmap, vm_offset_t addr)
2173 lwkt_gettoken(&vm_token);
2174 if ((*pmap_pde(pmap, addr)) == 0) {
2177 pte = get_ptbase(pmap, addr);
2178 ret = (*pte) ? 0 : 1;
2180 lwkt_reltoken(&vm_token);
2185 * Change the wiring attribute for a map/virtual-address pair.
2186 * The mapping must already exist in the pmap.
2188 * No other requirements.
2191 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2198 lwkt_gettoken(&vm_token);
2199 pte = get_ptbase(pmap, va);
2201 if (wired && (*pte & VPTE_WIRED) == 0)
2202 ++pmap->pm_stats.wired_count;
2203 else if (!wired && (*pte & VPTE_WIRED))
2204 --pmap->pm_stats.wired_count;
2205 KKASSERT(pmap->pm_stats.wired_count >= 0);
2208 * Wiring is not a hardware characteristic so there is no need to
2209 * invalidate TLB. However, in an SMP environment we must use
2210 * a locked bus cycle to update the pte (if we are not using
2211 * the pmap_inval_*() API that is)... it's ok to do this for simple
2215 atomic_set_long(pte, VPTE_WIRED);
2217 atomic_clear_long(pte, VPTE_WIRED);
2218 lwkt_reltoken(&vm_token);
2222 * Copy the range specified by src_addr/len
2223 * from the source map to the range dst_addr/len
2224 * in the destination map.
2226 * This routine is only advisory and need not do anything.
2229 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2230 vm_size_t len, vm_offset_t src_addr)
2233 vm_offset_t end_addr = src_addr + len;
2240 * XXX BUGGY. Amoung other things srcmpte is assumed to remain
2241 * valid through blocking calls, and that's just not going to
2248 if (dst_addr != src_addr)
2250 if (dst_pmap->pm_pdir == NULL)
2252 if (src_pmap->pm_pdir == NULL)
2255 lwkt_gettoken(&vm_token);
2257 src_frame = get_ptbase1(src_pmap, src_addr);
2258 dst_frame = get_ptbase2(dst_pmap, src_addr);
2261 * critical section protection is required to maintain the page/object
2262 * association, interrupts can free pages and remove them from
2265 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
2266 vpte_t *src_pte, *dst_pte;
2267 vm_page_t dstmpte, srcmpte;
2268 vm_offset_t srcptepaddr;
2271 if (addr >= VM_MAX_USER_ADDRESS)
2272 panic("pmap_copy: invalid to pmap_copy page tables\n");
2275 * Don't let optional prefaulting of pages make us go
2276 * way below the low water mark of free pages or way
2277 * above high water mark of used pv entries.
2279 if (vmstats.v_free_count < vmstats.v_free_reserved ||
2280 pv_entry_count > pv_entry_high_water)
2283 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
2284 ptepindex = addr >> PDRSHIFT;
2286 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex];
2287 if (srcptepaddr == 0)
2290 if (srcptepaddr & VPTE_PS) {
2291 if (dst_pmap->pm_pdir[ptepindex] == 0) {
2292 dst_pmap->pm_pdir[ptepindex] = (vpte_t)srcptepaddr;
2293 dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
2298 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
2299 if ((srcmpte == NULL) || (srcmpte->hold_count == 0) ||
2300 (srcmpte->flags & PG_BUSY)) {
2304 if (pdnxt > end_addr)
2307 src_pte = src_frame + ((addr - src_addr) >> PAGE_SHIFT);
2308 dst_pte = dst_frame + ((addr - src_addr) >> PAGE_SHIFT);
2309 while (addr < pdnxt) {
2314 * we only virtual copy managed pages
2316 if ((ptetemp & VPTE_MANAGED) != 0) {
2318 * We have to check after allocpte for the
2319 * pte still being around... allocpte can
2322 * pmap_allocpte can block, unfortunately
2323 * we have to reload the tables.
2325 dstmpte = pmap_allocpte(dst_pmap, addr);
2326 src_frame = get_ptbase1(src_pmap, src_addr);
2327 dst_frame = get_ptbase2(dst_pmap, src_addr);
2329 if ((*dst_pte == 0) && (ptetemp = *src_pte) &&
2330 (ptetemp & VPTE_MANAGED) != 0) {
2332 * Clear the modified and accessed
2333 * (referenced) bits during the copy.
2335 * We do not have to clear the write
2336 * bit to force a fault-on-modify
2337 * because the real kernel's target
2338 * pmap is empty and will fault anyway.
2340 m = PHYS_TO_VM_PAGE(ptetemp);
2341 *dst_pte = ptetemp & ~(VPTE_M | VPTE_A);
2342 ++dst_pmap->pm_stats.resident_count;
2343 pmap_insert_entry(dst_pmap, addr,
2345 KKASSERT(m->flags & PG_MAPPED);
2347 pmap_unwire_pte_hold(dst_pmap, dstmpte);
2349 if (dstmpte->hold_count >= srcmpte->hold_count)
2357 lwkt_reltoken(&vm_token);
2363 * Zero the specified PA by mapping the page into KVM and clearing its
2366 * This function may be called from an interrupt and no locking is
2370 pmap_zero_page(vm_paddr_t phys)
2372 struct mdglobaldata *gd = mdcpu;
2376 panic("pmap_zero_page: CMAP3 busy");
2377 *gd->gd_CMAP3 = VPTE_V | VPTE_R | VPTE_W | (phys & VPTE_FRAME) | VPTE_A | VPTE_M;
2378 madvise(gd->gd_CADDR3, PAGE_SIZE, MADV_INVAL);
2380 bzero(gd->gd_CADDR3, PAGE_SIZE);
2386 * pmap_page_assertzero:
2388 * Assert that a page is empty, panic if it isn't.
2391 pmap_page_assertzero(vm_paddr_t phys)
2393 struct mdglobaldata *gd = mdcpu;
2398 panic("pmap_zero_page: CMAP3 busy");
2399 *gd->gd_CMAP3 = VPTE_V | VPTE_R | VPTE_W |
2400 (phys & VPTE_FRAME) | VPTE_A | VPTE_M;
2401 madvise(gd->gd_CADDR3, PAGE_SIZE, MADV_INVAL);
2402 for (i = 0; i < PAGE_SIZE; i += 4) {
2403 if (*(int *)((char *)gd->gd_CADDR3 + i) != 0) {
2404 panic("pmap_page_assertzero() @ %p not zero!\n",
2405 (void *)gd->gd_CADDR3);
2415 * Zero part of a physical page by mapping it into memory and clearing
2416 * its contents with bzero.
2418 * off and size may not cover an area beyond a single hardware page.
2421 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
2423 struct mdglobaldata *gd = mdcpu;
2427 panic("pmap_zero_page: CMAP3 busy");
2428 *gd->gd_CMAP3 = VPTE_V | VPTE_R | VPTE_W |
2429 (phys & VPTE_FRAME) | VPTE_A | VPTE_M;
2430 madvise(gd->gd_CADDR3, PAGE_SIZE, MADV_INVAL);
2432 bzero((char *)gd->gd_CADDR3 + off, size);
2440 * Copy the physical page from the source PA to the target PA.
2441 * This function may be called from an interrupt. No locking
2445 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
2447 struct mdglobaldata *gd = mdcpu;
2450 if (*(int *) gd->gd_CMAP1)
2451 panic("pmap_copy_page: CMAP1 busy");
2452 if (*(int *) gd->gd_CMAP2)
2453 panic("pmap_copy_page: CMAP2 busy");
2455 *(int *) gd->gd_CMAP1 = VPTE_V | VPTE_R | (src & PG_FRAME) | VPTE_A;
2456 *(int *) gd->gd_CMAP2 = VPTE_V | VPTE_R | VPTE_W | (dst & VPTE_FRAME) | VPTE_A | VPTE_M;
2458 madvise(gd->gd_CADDR1, PAGE_SIZE, MADV_INVAL);
2459 madvise(gd->gd_CADDR2, PAGE_SIZE, MADV_INVAL);
2461 bcopy(gd->gd_CADDR1, gd->gd_CADDR2, PAGE_SIZE);
2463 *(int *) gd->gd_CMAP1 = 0;
2464 *(int *) gd->gd_CMAP2 = 0;
2469 * pmap_copy_page_frag:
2471 * Copy the physical page from the source PA to the target PA.
2472 * This function may be called from an interrupt. No locking
2476 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
2478 struct mdglobaldata *gd = mdcpu;
2481 if (*(int *) gd->gd_CMAP1)
2482 panic("pmap_copy_page: CMAP1 busy");
2483 if (*(int *) gd->gd_CMAP2)
2484 panic("pmap_copy_page: CMAP2 busy");
2486 *(int *) gd->gd_CMAP1 = VPTE_V | (src & VPTE_FRAME) | VPTE_A;
2487 *(int *) gd->gd_CMAP2 = VPTE_V | VPTE_R | VPTE_W | (dst & VPTE_FRAME) | VPTE_A | VPTE_M;
2489 madvise(gd->gd_CADDR1, PAGE_SIZE, MADV_INVAL);
2490 madvise(gd->gd_CADDR2, PAGE_SIZE, MADV_INVAL);
2492 bcopy((char *)gd->gd_CADDR1 + (src & PAGE_MASK),
2493 (char *)gd->gd_CADDR2 + (dst & PAGE_MASK),
2496 *(int *) gd->gd_CMAP1 = 0;
2497 *(int *) gd->gd_CMAP2 = 0;
2502 * Returns true if the pmap's pv is one of the first
2503 * 16 pvs linked to from this page. This count may
2504 * be changed upwards or downwards in the future; it
2505 * is only necessary that true be returned for a small
2506 * subset of pmaps for proper page aging.
2511 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2516 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2520 lwkt_gettoken(&vm_token);
2522 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2523 if (pv->pv_pmap == pmap) {
2524 lwkt_reltoken(&vm_token);
2532 lwkt_reltoken(&vm_token);
2538 * Remove all pages from specified address space
2539 * this aids process exit speeds. Also, this code
2540 * is special cased for current process only, but
2541 * can have the more generic (and slightly slower)
2542 * mode enabled. This is much faster than pmap_remove
2543 * in the case of running down an entire address space.
2548 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2553 int32_t save_generation;
2555 if (pmap->pm_pteobj)
2556 vm_object_hold(pmap->pm_pteobj);
2557 lwkt_gettoken(&vm_token);
2558 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
2559 if (pv->pv_va >= eva || pv->pv_va < sva) {
2560 npv = TAILQ_NEXT(pv, pv_plist);
2564 KKASSERT(pmap == pv->pv_pmap);
2566 pte = pmap_pte(pmap, pv->pv_va);
2569 * We cannot remove wired pages from a process' mapping
2572 if (*pte & VPTE_WIRED) {
2573 npv = TAILQ_NEXT(pv, pv_plist);
2576 tpte = pmap_inval_loadandclear(pte, pmap, pv->pv_va);
2578 m = PHYS_TO_VM_PAGE(tpte);
2580 KASSERT(m < &vm_page_array[vm_page_array_size],
2581 ("pmap_remove_pages: bad tpte %lx", tpte));
2583 KKASSERT(pmap->pm_stats.resident_count > 0);
2584 --pmap->pm_stats.resident_count;
2587 * Update the vm_page_t clean and reference bits.
2589 if (tpte & VPTE_M) {
2593 npv = TAILQ_NEXT(pv, pv_plist);
2594 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
2595 save_generation = ++pmap->pm_generation;
2597 m->md.pv_list_count--;
2598 atomic_add_int(&m->object->agg_pv_list_count, -1);
2599 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2600 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2601 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2603 pmap_unuse_pt(pmap, pv->pv_va, pv->pv_ptem);
2607 * Restart the scan if we blocked during the unuse or free
2608 * calls and other removals were made.
2610 if (save_generation != pmap->pm_generation) {
2611 kprintf("Warning: pmap_remove_pages race-A avoided\n");
2612 npv = TAILQ_FIRST(&pmap->pm_pvlist);
2615 lwkt_reltoken(&vm_token);
2616 if (pmap->pm_pteobj)
2617 vm_object_drop(pmap->pm_pteobj);
2621 * pmap_testbit tests bits in active mappings of a VM page.
2623 * The caller must hold vm_token
2626 pmap_testbit(vm_page_t m, int bit)
2631 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2634 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2639 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2641 * if the bit being tested is the modified bit, then
2642 * mark clean_map and ptes as never
2645 if (bit & (VPTE_A|VPTE_M)) {
2646 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2650 #if defined(PMAP_DIAGNOSTIC)
2652 kprintf("Null pmap (tb) at va: 0x%x\n", pv->pv_va);
2656 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2667 * This routine is used to clear bits in ptes. Certain bits require special
2668 * handling, in particular (on virtual kernels) the VPTE_M (modify) bit.
2670 * This routine is only called with certain VPTE_* bit combinations.
2672 * The caller must hold vm_token
2674 static __inline void
2675 pmap_clearbit(vm_page_t m, int bit)
2681 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2687 * Loop over all current mappings setting/clearing as appropos If
2688 * setting RO do we need to clear the VAC?
2690 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2692 * don't write protect pager mappings
2694 if (bit == VPTE_W) {
2695 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2699 #if defined(PMAP_DIAGNOSTIC)
2701 kprintf("Null pmap (cb) at va: 0x%x\n", pv->pv_va);
2707 * Careful here. We can use a locked bus instruction to
2708 * clear VPTE_A or VPTE_M safely but we need to synchronize
2709 * with the target cpus when we mess with VPTE_W.
2711 * On virtual kernels we must force a new fault-on-write
2712 * in the real kernel if we clear the Modify bit ourselves,
2713 * otherwise the real kernel will not get a new fault and
2714 * will never set our Modify bit again.
2716 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2718 if (bit == VPTE_W) {
2720 * We must also clear VPTE_M when clearing
2723 pbits = pmap_clean_pte(pte, pv->pv_pmap,
2727 } else if (bit == VPTE_M) {
2729 * We do not have to make the page read-only
2730 * when clearing the Modify bit. The real
2731 * kernel will make the real PTE read-only
2732 * or otherwise detect the write and set
2733 * our VPTE_M again simply by us invalidating
2734 * the real kernel VA for the pmap (as we did
2735 * above). This allows the real kernel to
2736 * handle the write fault without forwarding
2739 atomic_clear_long(pte, VPTE_M);
2740 } else if ((bit & (VPTE_W|VPTE_M)) == (VPTE_W|VPTE_M)) {
2742 * We've been asked to clear W & M, I guess
2743 * the caller doesn't want us to update
2744 * the dirty status of the VM page.
2746 pmap_clean_pte(pte, pv->pv_pmap, pv->pv_va);
2749 * We've been asked to clear bits that do
2750 * not interact with hardware.
2752 atomic_clear_long(pte, bit);
2760 * Lower the permission for all mappings to a given page.
2765 pmap_page_protect(vm_page_t m, vm_prot_t prot)
2767 if ((prot & VM_PROT_WRITE) == 0) {
2768 lwkt_gettoken(&vm_token);
2769 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
2770 pmap_clearbit(m, VPTE_W);
2771 vm_page_flag_clear(m, PG_WRITEABLE);
2775 lwkt_reltoken(&vm_token);
2780 pmap_phys_address(vm_pindex_t ppn)
2782 return (i386_ptob(ppn));
2786 * Return a count of reference bits for a page, clearing those bits.
2787 * It is not necessary for every reference bit to be cleared, but it
2788 * is necessary that 0 only be returned when there are truly no
2789 * reference bits set.
2791 * XXX: The exact number of bits to check and clear is a matter that
2792 * should be tested and standardized at some point in the future for
2793 * optimal aging of shared pages.
2798 pmap_ts_referenced(vm_page_t m)
2800 pv_entry_t pv, pvf, pvn;
2804 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2808 lwkt_gettoken(&vm_token);
2810 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2815 pvn = TAILQ_NEXT(pv, pv_list);
2817 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2819 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2821 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2824 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2826 if (pte && (*pte & VPTE_A)) {
2828 atomic_clear_long(pte, VPTE_A);
2830 atomic_clear_long_nonlocked(pte, VPTE_A);
2837 } while ((pv = pvn) != NULL && pv != pvf);
2839 lwkt_reltoken(&vm_token);
2846 * Return whether or not the specified physical page was modified
2847 * in any physical maps.
2852 pmap_is_modified(vm_page_t m)
2856 lwkt_gettoken(&vm_token);
2857 res = pmap_testbit(m, VPTE_M);
2858 lwkt_reltoken(&vm_token);
2863 * Clear the modify bits on the specified physical page.
2868 pmap_clear_modify(vm_page_t m)
2870 lwkt_gettoken(&vm_token);
2871 pmap_clearbit(m, VPTE_M);
2872 lwkt_reltoken(&vm_token);
2876 * Clear the reference bit on the specified physical page.
2881 pmap_clear_reference(vm_page_t m)
2883 lwkt_gettoken(&vm_token);
2884 pmap_clearbit(m, VPTE_A);
2885 lwkt_reltoken(&vm_token);
2889 * Miscellaneous support routines follow
2893 i386_protection_init(void)
2897 kp = protection_codes;
2898 for (prot = 0; prot < 8; prot++) {
2899 if (prot & VM_PROT_READ)
2901 if (prot & VM_PROT_WRITE)
2903 if (prot & VM_PROT_EXECUTE)
2912 * Map a set of physical memory pages into the kernel virtual
2913 * address space. Return a pointer to where it is mapped. This
2914 * routine is intended to be used for mapping device memory,
2917 * NOTE: we can't use pgeflag unless we invalidate the pages one at
2921 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
2923 vm_offset_t va, tmpva, offset;
2926 offset = pa & PAGE_MASK;
2927 size = roundup(offset + size, PAGE_SIZE);
2929 va = kmem_alloc_nofault(&kernel_map, size, PAGE_SIZE);
2931 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
2933 pa = pa & VPTE_FRAME;
2934 for (tmpva = va; size > 0;) {
2935 pte = KernelPTA + (tmpva >> PAGE_SHIFT);
2936 *pte = pa | VPTE_R | VPTE_W | VPTE_V; /* | pgeflag; */
2944 return ((void *)(va + offset));
2948 pmap_unmapdev(vm_offset_t va, vm_size_t size)
2950 vm_offset_t base, offset;
2952 base = va & VPTE_FRAME;
2953 offset = va & PAGE_MASK;
2954 size = roundup(offset + size, PAGE_SIZE);
2955 pmap_qremove(va, size >> PAGE_SHIFT);
2956 kmem_free(&kernel_map, base, size);
2962 * Perform the pmap work for mincore
2967 pmap_mincore(pmap_t pmap, vm_offset_t addr)
2973 lwkt_gettoken(&vm_token);
2975 ptep = pmap_pte(pmap, addr);
2977 lwkt_reltoken(&vm_token);
2981 if ((pte = *ptep) != 0) {
2984 val = MINCORE_INCORE;
2985 if ((pte & VPTE_MANAGED) == 0)
2988 pa = pte & VPTE_FRAME;
2990 m = PHYS_TO_VM_PAGE(pa);
2996 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
2998 * Modified by someone
3000 else if (m->dirty || pmap_is_modified(m))
3001 val |= MINCORE_MODIFIED_OTHER;
3006 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3009 * Referenced by someone
3011 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3012 val |= MINCORE_REFERENCED_OTHER;
3013 vm_page_flag_set(m, PG_REFERENCED);
3017 lwkt_reltoken(&vm_token);
3022 * Caller must hold vmspace->vm_map.token for oldvm and newvm
3025 pmap_replacevm(struct proc *p, struct vmspace *newvm, int adjrefs)
3027 struct vmspace *oldvm;
3030 oldvm = p->p_vmspace;
3032 if (oldvm != newvm) {
3033 p->p_vmspace = newvm;
3034 KKASSERT(p->p_nthreads == 1);
3035 lp = RB_ROOT(&p->p_lwp_tree);
3036 pmap_setlwpvm(lp, newvm);
3038 sysref_get(&newvm->vm_sysref);
3039 sysref_put(&oldvm->vm_sysref);
3046 pmap_setlwpvm(struct lwp *lp, struct vmspace *newvm)
3048 struct vmspace *oldvm;
3052 oldvm = lp->lwp_vmspace;
3054 if (oldvm != newvm) {
3055 lp->lwp_vmspace = newvm;
3056 if (curthread->td_lwp == lp) {
3057 pmap = vmspace_pmap(newvm);
3059 atomic_set_cpumask(&pmap->pm_active, mycpu->gd_cpumask);
3061 pmap->pm_active |= 1;
3063 #if defined(SWTCH_OPTIM_STATS)
3066 pmap = vmspace_pmap(oldvm);
3068 atomic_clear_cpumask(&pmap->pm_active, mycpu->gd_cpumask);
3070 pmap->pm_active &= ~(cpumask_t)1;
3079 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3082 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3086 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3091 * Used by kmalloc/kfree, page already exists at va
3094 pmap_kvtom(vm_offset_t va)
3098 KKASSERT(va >= KvaStart && va < KvaEnd);
3099 ptep = KernelPTA + (va >> PAGE_SHIFT);
3100 return(PHYS_TO_VM_PAGE(*ptep & PG_FRAME));