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 * Retire the given physical map from service. Should only be called if
380 * the map contains no valid mappings.
385 pmap_destroy(pmap_t pmap)
390 lwkt_gettoken(&vm_token);
391 if (--pmap->pm_count == 0) {
393 panic("destroying a pmap is not yet implemented");
395 lwkt_reltoken(&vm_token);
399 * Add a reference to the specified pmap.
404 pmap_reference(pmap_t pmap)
407 lwkt_gettoken(&vm_token);
409 lwkt_reltoken(&vm_token);
413 /************************************************************************
414 * VMSPACE MANAGEMENT *
415 ************************************************************************
417 * The VMSPACE management we do in our virtual kernel must be reflected
418 * in the real kernel. This is accomplished by making vmspace system
419 * calls to the real kernel.
422 cpu_vmspace_alloc(struct vmspace *vm)
427 #define LAST_EXTENT (VM_MAX_USER_ADDRESS - 0x80000000)
429 if (vmspace_create(&vm->vm_pmap, 0, NULL) < 0)
430 panic("vmspace_create() failed");
432 rp = vmspace_mmap(&vm->vm_pmap, (void *)0x00000000, 0x40000000,
433 PROT_READ|PROT_WRITE,
434 MAP_FILE|MAP_SHARED|MAP_VPAGETABLE|MAP_FIXED,
436 if (rp == MAP_FAILED)
437 panic("vmspace_mmap: failed1");
438 vmspace_mcontrol(&vm->vm_pmap, (void *)0x00000000, 0x40000000,
440 rp = vmspace_mmap(&vm->vm_pmap, (void *)0x40000000, 0x40000000,
441 PROT_READ|PROT_WRITE,
442 MAP_FILE|MAP_SHARED|MAP_VPAGETABLE|MAP_FIXED,
443 MemImageFd, 0x40000000);
444 if (rp == MAP_FAILED)
445 panic("vmspace_mmap: failed2");
446 vmspace_mcontrol(&vm->vm_pmap, (void *)0x40000000, 0x40000000,
448 rp = vmspace_mmap(&vm->vm_pmap, (void *)0x80000000, LAST_EXTENT,
449 PROT_READ|PROT_WRITE,
450 MAP_FILE|MAP_SHARED|MAP_VPAGETABLE|MAP_FIXED,
451 MemImageFd, 0x80000000);
452 vmspace_mcontrol(&vm->vm_pmap, (void *)0x80000000, LAST_EXTENT,
454 if (rp == MAP_FAILED)
455 panic("vmspace_mmap: failed3");
457 r = vmspace_mcontrol(&vm->vm_pmap, (void *)0x00000000, 0x40000000,
458 MADV_SETMAP, vmspace_pmap(vm)->pm_pdirpte);
460 panic("vmspace_mcontrol: failed1");
461 r = vmspace_mcontrol(&vm->vm_pmap, (void *)0x40000000, 0x40000000,
462 MADV_SETMAP, vmspace_pmap(vm)->pm_pdirpte);
464 panic("vmspace_mcontrol: failed2");
465 r = vmspace_mcontrol(&vm->vm_pmap, (void *)0x80000000, LAST_EXTENT,
466 MADV_SETMAP, vmspace_pmap(vm)->pm_pdirpte);
468 panic("vmspace_mcontrol: failed3");
472 cpu_vmspace_free(struct vmspace *vm)
474 if (vmspace_destroy(&vm->vm_pmap) < 0)
475 panic("vmspace_destroy() failed");
478 /************************************************************************
479 * Procedures which operate directly on the kernel PMAP *
480 ************************************************************************/
483 * This maps the requested page table and gives us access to it.
485 * This routine can be called from a potentially preempting interrupt
486 * thread or from a normal thread.
489 get_ptbase(struct pmap *pmap, vm_offset_t va)
491 struct mdglobaldata *gd = mdcpu;
493 if (pmap == &kernel_pmap) {
494 KKASSERT(va >= KvaStart && va < KvaEnd);
495 return(KernelPTA + (va >> PAGE_SHIFT));
496 } else if (pmap->pm_pdir == gd->gd_PT1pdir) {
497 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
498 *gd->gd_PT1pde = pmap->pm_pdirpte;
499 madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL);
500 atomic_set_cpumask(&pmap->pm_cpucachemask,
503 return(gd->gd_PT1map + (va >> PAGE_SHIFT));
504 } else if (pmap->pm_pdir == gd->gd_PT2pdir) {
505 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
506 *gd->gd_PT2pde = pmap->pm_pdirpte;
507 madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL);
508 atomic_set_cpumask(&pmap->pm_cpucachemask,
511 return(gd->gd_PT2map + (va >> PAGE_SHIFT));
515 * If we aren't running from a potentially preempting interrupt,
516 * load a new page table directory into the page table cache
518 if (gd->mi.gd_intr_nesting_level == 0 &&
519 (gd->mi.gd_curthread->td_flags & TDF_INTTHREAD) == 0) {
521 * Choose one or the other and map the page table
522 * in the KVA space reserved for it.
524 if ((gd->gd_PTflip = 1 - gd->gd_PTflip) == 0) {
525 gd->gd_PT1pdir = pmap->pm_pdir;
526 *gd->gd_PT1pde = pmap->pm_pdirpte;
527 madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL);
528 atomic_set_cpumask(&pmap->pm_cpucachemask,
530 return(gd->gd_PT1map + (va >> PAGE_SHIFT));
532 gd->gd_PT2pdir = pmap->pm_pdir;
533 *gd->gd_PT2pde = pmap->pm_pdirpte;
534 madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL);
535 atomic_set_cpumask(&pmap->pm_cpucachemask,
537 return(gd->gd_PT2map + (va >> PAGE_SHIFT));
542 * If we are running from a preempting interrupt use a private
543 * map. The caller must be in a critical section.
545 KKASSERT(IN_CRITICAL_SECT(curthread));
546 if (pmap->pm_pdir == gd->gd_PT3pdir) {
547 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
548 *gd->gd_PT3pde = pmap->pm_pdirpte;
549 madvise(gd->gd_PT3map, SEG_SIZE, MADV_INVAL);
550 atomic_set_cpumask(&pmap->pm_cpucachemask,
554 gd->gd_PT3pdir = pmap->pm_pdir;
555 *gd->gd_PT3pde = pmap->pm_pdirpte;
556 madvise(gd->gd_PT3map, SEG_SIZE, MADV_INVAL);
557 atomic_set_cpumask(&pmap->pm_cpucachemask,
560 return(gd->gd_PT3map + (va >> PAGE_SHIFT));
564 get_ptbase1(struct pmap *pmap, vm_offset_t va)
566 struct mdglobaldata *gd = mdcpu;
568 if (pmap == &kernel_pmap) {
569 KKASSERT(va >= KvaStart && va < KvaEnd);
570 return(KernelPTA + (va >> PAGE_SHIFT));
571 } else if (pmap->pm_pdir == gd->gd_PT1pdir) {
572 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
573 *gd->gd_PT1pde = pmap->pm_pdirpte;
574 madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL);
575 atomic_set_cpumask(&pmap->pm_cpucachemask,
578 return(gd->gd_PT1map + (va >> PAGE_SHIFT));
580 KKASSERT(gd->mi.gd_intr_nesting_level == 0 &&
581 (gd->mi.gd_curthread->td_flags & TDF_INTTHREAD) == 0);
582 gd->gd_PT1pdir = pmap->pm_pdir;
583 *gd->gd_PT1pde = pmap->pm_pdirpte;
584 madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL);
585 return(gd->gd_PT1map + (va >> PAGE_SHIFT));
589 get_ptbase2(struct pmap *pmap, vm_offset_t va)
591 struct mdglobaldata *gd = mdcpu;
593 if (pmap == &kernel_pmap) {
594 KKASSERT(va >= KvaStart && va < KvaEnd);
595 return(KernelPTA + (va >> PAGE_SHIFT));
596 } else if (pmap->pm_pdir == gd->gd_PT2pdir) {
597 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
598 *gd->gd_PT2pde = pmap->pm_pdirpte;
599 madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL);
600 atomic_set_cpumask(&pmap->pm_cpucachemask,
603 return(gd->gd_PT2map + (va >> PAGE_SHIFT));
605 KKASSERT(gd->mi.gd_intr_nesting_level == 0 &&
606 (gd->mi.gd_curthread->td_flags & TDF_INTTHREAD) == 0);
607 gd->gd_PT2pdir = pmap->pm_pdir;
608 *gd->gd_PT2pde = pmap->pm_pdirpte;
609 madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL);
610 return(gd->gd_PT2map + (va >> PAGE_SHIFT));
614 * Return a pointer to the page table entry for the specified va in the
615 * specified pmap. NULL is returned if there is no valid page table page
618 static __inline vpte_t *
619 pmap_pte(struct pmap *pmap, vm_offset_t va)
623 ptep = &pmap->pm_pdir[va >> SEG_SHIFT];
627 return (get_ptbase(pmap, va));
633 * Enter a mapping into kernel_pmap. Mappings created in this fashion
634 * are not managed. Mappings must be immediately accessible on all cpus.
636 * Call pmap_inval_pte() to invalidate the virtual pte and clean out the
637 * real pmap and handle related races before storing the new vpte.
640 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
645 KKASSERT(va >= KvaStart && va < KvaEnd);
646 npte = (vpte_t)pa | VPTE_R | VPTE_W | VPTE_V;
647 ptep = KernelPTA + (va >> PAGE_SHIFT);
649 pmap_inval_pte(ptep, &kernel_pmap, va);
654 * Synchronize a kvm mapping originally made for the private use on
655 * some other cpu so it can be used on all cpus.
657 * XXX add MADV_RESYNC to improve performance.
660 pmap_kenter_sync(vm_offset_t va)
662 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
666 * Synchronize a kvm mapping originally made for the private use on
667 * some other cpu so it can be used on our cpu. Turns out to be the
668 * same madvise() call, because we have to sync the real pmaps anyway.
670 * XXX add MADV_RESYNC to improve performance.
673 pmap_kenter_sync_quick(vm_offset_t va)
675 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
680 * Make a previously read-only kernel mapping R+W (not implemented by
684 pmap_kmodify_rw(vm_offset_t va)
686 *pmap_kpte(va) |= VPTE_R | VPTE_W;
687 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
691 * Make a kernel mapping non-cacheable (not applicable to virtual kernels)
694 pmap_kmodify_nc(vm_offset_t va)
696 *pmap_kpte(va) |= VPTE_N;
697 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
703 * Map a contiguous range of physical memory to a KVM
706 pmap_map(vm_offset_t *virtp, vm_paddr_t start, vm_paddr_t end, int prot)
708 vm_offset_t sva, virt;
711 while (start < end) {
712 pmap_kenter(virt, start);
721 pmap_kpte(vm_offset_t va)
725 KKASSERT(va >= KvaStart && va < KvaEnd);
726 ptep = KernelPTA + (va >> PAGE_SHIFT);
731 * Enter an unmanaged KVA mapping for the private use of the current
732 * cpu only. pmap_kenter_sync() may be called to make the mapping usable
735 * It is illegal for the mapping to be accessed by other cpus unleess
736 * pmap_kenter_sync*() is called.
739 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
744 KKASSERT(va >= KvaStart && va < KvaEnd);
746 npte = (vpte_t)pa | VPTE_R | VPTE_W | VPTE_V;
747 ptep = KernelPTA + (va >> PAGE_SHIFT);
749 pmap_inval_pte_quick(ptep, &kernel_pmap, va);
754 * Make a temporary mapping for a physical address. This is only intended
755 * to be used for panic dumps.
757 * The caller is responsible for calling smp_invltlb().
760 pmap_kenter_temporary(vm_paddr_t pa, long i)
762 pmap_kenter_quick(crashdumpmap + (i * PAGE_SIZE), pa);
763 return ((void *)crashdumpmap);
767 * Remove an unmanaged mapping created with pmap_kenter*().
770 pmap_kremove(vm_offset_t va)
774 KKASSERT(va >= KvaStart && va < KvaEnd);
776 ptep = KernelPTA + (va >> PAGE_SHIFT);
778 pmap_inval_pte(ptep, &kernel_pmap, va);
783 * Remove an unmanaged mapping created with pmap_kenter*() but synchronize
784 * only with this cpu.
786 * Unfortunately because we optimize new entries by testing VPTE_V later
787 * on, we actually still have to synchronize with all the cpus. XXX maybe
788 * store a junk value and test against 0 in the other places instead?
791 pmap_kremove_quick(vm_offset_t va)
795 KKASSERT(va >= KvaStart && va < KvaEnd);
797 ptep = KernelPTA + (va >> PAGE_SHIFT);
799 pmap_inval_pte(ptep, &kernel_pmap, va); /* NOT _quick */
804 * Extract the physical address from the kernel_pmap that is associated
805 * with the specified virtual address.
808 pmap_kextract(vm_offset_t va)
813 KKASSERT(va >= KvaStart && va < KvaEnd);
815 ptep = KernelPTA + (va >> PAGE_SHIFT);
816 pa = (vm_paddr_t)(*ptep & VPTE_FRAME) | (va & PAGE_MASK);
821 * Map a set of unmanaged VM pages into KVM.
824 pmap_qenter(vm_offset_t va, struct vm_page **m, 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);
833 *ptep = (vpte_t)(*m)->phys_addr | VPTE_R | VPTE_W | VPTE_V;
841 * Undo the effects of pmap_qenter*().
844 pmap_qremove(vm_offset_t va, int count)
846 KKASSERT(va >= KvaStart && va + count * PAGE_SIZE < KvaEnd);
850 ptep = KernelPTA + (va >> PAGE_SHIFT);
852 pmap_inval_pte(ptep, &kernel_pmap, va);
859 /************************************************************************
860 * Misc support glue called by machine independant code *
861 ************************************************************************
863 * These routines are called by machine independant code to operate on
864 * certain machine-dependant aspects of processes, threads, and pmaps.
868 * Initialize MD portions of the thread structure.
871 pmap_init_thread(thread_t td)
873 /* enforce pcb placement */
874 td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
875 td->td_savefpu = &td->td_pcb->pcb_save;
876 td->td_sp = (char *)td->td_pcb - 16;
880 * This routine directly affects the fork perf for a process.
883 pmap_init_proc(struct proc *p)
888 * We pre-allocate all page table pages for kernel virtual memory so
889 * this routine will only be called if KVM has been exhausted.
894 pmap_growkernel(vm_offset_t kstart, vm_offset_t kend)
898 addr = (kend + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
900 lwkt_gettoken(&vm_token);
901 if (addr > virtual_end - SEG_SIZE)
902 panic("KVM exhausted");
903 kernel_vm_end = addr;
904 lwkt_reltoken(&vm_token);
908 * The modification bit is not tracked for any pages in this range. XXX
909 * such pages in this maps should always use pmap_k*() functions and not
912 * XXX User and kernel address spaces are independant for virtual kernels,
913 * this function only applies to the kernel pmap.
916 pmap_track_modified(pmap_t pmap, vm_offset_t va)
918 if (pmap != &kernel_pmap)
920 if ((va < clean_sva) || (va >= clean_eva))
926 /************************************************************************
927 * Procedures supporting managed page table pages *
928 ************************************************************************
930 * These procedures are used to track managed page table pages. These pages
931 * use the page table page's vm_page_t to track PTEs in the page. The
932 * page table pages themselves are arranged in a VM object, pmap->pm_pteobj.
934 * This allows the system to throw away page table pages for user processes
935 * at will and reinstantiate them on demand.
939 * This routine works like vm_page_lookup() but also blocks as long as the
940 * page is busy. This routine does not busy the page it returns.
942 * Unless the caller is managing objects whos pages are in a known state,
943 * the call should be made with a critical section held so the page's object
944 * association remains valid on return.
947 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
951 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
952 m = vm_page_lookup_busy_wait(object, pindex, FALSE, "pplookp");
958 * This routine unholds page table pages, and if the hold count
959 * drops to zero, then it decrements the wire count.
961 * We must recheck that this is the last hold reference after busy-sleeping
965 _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
967 vm_page_busy_wait(m, FALSE, "pmuwpt");
968 KASSERT(m->queue == PQ_NONE,
969 ("_pmap_unwire_pte_hold: %p->queue != PQ_NONE", m));
971 if (m->hold_count == 1) {
973 * Unmap the page table page.
975 KKASSERT(pmap->pm_pdir[m->pindex] != 0);
976 pmap_inval_pde(&pmap->pm_pdir[m->pindex], pmap,
977 (vm_offset_t)m->pindex << SEG_SHIFT);
978 KKASSERT(pmap->pm_stats.resident_count > 0);
979 --pmap->pm_stats.resident_count;
981 if (pmap->pm_ptphint == m)
982 pmap->pm_ptphint = NULL;
985 * This was our last hold, the page had better be unwired
986 * after we decrement wire_count.
988 * FUTURE NOTE: shared page directory page could result in
989 * multiple wire counts.
993 KKASSERT(m->wire_count == 0);
994 atomic_add_int(&vmstats.v_wire_count, -1);
995 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
997 vm_page_free_zero(m);
1000 KKASSERT(m->hold_count > 1);
1008 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
1010 KKASSERT(m->hold_count > 0);
1011 if (m->hold_count > 1) {
1015 return _pmap_unwire_pte_hold(pmap, m);
1020 * After removing a page table entry, this routine is used to
1021 * conditionally free the page, and manage the hold/wire counts.
1024 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte)
1028 ASSERT_LWKT_TOKEN_HELD(vm_object_token(pmap->pm_pteobj));
1032 * page table pages in the kernel_pmap are not managed.
1034 if (pmap == &kernel_pmap)
1036 ptepindex = (va >> PDRSHIFT);
1037 if (pmap->pm_ptphint &&
1038 (pmap->pm_ptphint->pindex == ptepindex)) {
1039 mpte = pmap->pm_ptphint;
1041 mpte = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1042 pmap->pm_ptphint = mpte;
1043 vm_page_wakeup(mpte);
1046 return pmap_unwire_pte_hold(pmap, mpte);
1050 * Attempt to release and free the vm_page backing a page directory page
1051 * in a pmap. Returns 1 on success, 0 on failure (if the procedure had
1055 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1057 vpte_t *pde = pmap->pm_pdir;
1060 * This code optimizes the case of freeing non-busy
1061 * page-table pages. Those pages are zero now, and
1062 * might as well be placed directly into the zero queue.
1064 if (vm_page_busy_try(p, FALSE)) {
1065 vm_page_sleep_busy(p, FALSE, "pmaprl");
1068 KKASSERT(pmap->pm_stats.resident_count > 0);
1069 --pmap->pm_stats.resident_count;
1071 if (p->hold_count) {
1072 panic("pmap_release: freeing held page table page");
1075 * Page directory pages need to have the kernel stuff cleared, so
1076 * they can go into the zero queue also.
1078 * In virtual kernels there is no 'kernel stuff'. For the moment
1079 * I just make sure the whole thing has been zero'd even though
1080 * it should already be completely zero'd.
1082 * pmaps for vkernels do not self-map because they do not share
1083 * their address space with the vkernel. Clearing of pde[] thus
1084 * only applies to page table pages and not to the page directory
1087 if (p->pindex == pmap->pm_pdindex) {
1088 bzero(pde, VPTE_PAGETABLE_SIZE);
1089 pmap_kremove((vm_offset_t)pmap->pm_pdir);
1091 KKASSERT(pde[p->pindex] != 0);
1092 pmap_inval_pde(&pde[p->pindex], pmap,
1093 (vm_offset_t)p->pindex << SEG_SHIFT);
1097 * Clear the matching hint
1099 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1100 pmap->pm_ptphint = NULL;
1103 * And throw the page away. The page is completely zero'd out so
1104 * optimize the free call.
1107 atomic_add_int(&vmstats.v_wire_count, -1);
1108 vm_page_free_zero(p);
1113 * This routine is called if the page table page is not mapped in the page
1116 * The routine is broken up into two parts for readability.
1118 * It must return a held mpte and map the page directory page as required.
1119 * Because vm_page_grab() can block, we must re-check pm_pdir[ptepindex]
1122 _pmap_allocpte(pmap_t pmap, unsigned ptepindex)
1128 * Find or fabricate a new pagetable page. A busied page will be
1129 * returned. This call may block.
1131 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1132 VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1133 vm_page_flag_set(m, PG_MAPPED);
1135 KASSERT(m->queue == PQ_NONE,
1136 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1139 * Increment the hold count for the page we will be returning to
1145 * It is possible that someone else got in and mapped by the page
1146 * directory page while we were blocked, if so just unbusy and
1147 * return the held page.
1149 if ((ptepa = pmap->pm_pdir[ptepindex]) != 0) {
1150 KKASSERT((ptepa & VPTE_FRAME) == VM_PAGE_TO_PHYS(m));
1157 * Map the pagetable page into the process address space, if
1158 * it isn't already there.
1160 ++pmap->pm_stats.resident_count;
1162 ptepa = VM_PAGE_TO_PHYS(m);
1163 pmap->pm_pdir[ptepindex] = (vpte_t)ptepa | VPTE_R | VPTE_W | VPTE_V |
1167 * We are likely about to access this page table page, so set the
1168 * page table hint to reduce overhead.
1170 pmap->pm_ptphint = m;
1178 * Determine the page table page required to access the VA in the pmap
1179 * and allocate it if necessary. Return a held vm_page_t for the page.
1181 * Only used with user pmaps.
1184 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1190 ASSERT_LWKT_TOKEN_HELD(vm_object_token(pmap->pm_pteobj));
1193 * Calculate pagetable page index
1195 ptepindex = va >> PDRSHIFT;
1198 * Get the page directory entry
1200 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1203 * This supports switching from a 4MB page to a
1206 if (ptepa & VPTE_PS) {
1207 KKASSERT(pmap->pm_pdir[ptepindex] != 0);
1208 pmap_inval_pde(&pmap->pm_pdir[ptepindex], pmap,
1209 (vm_offset_t)ptepindex << SEG_SHIFT);
1214 * If the page table page is mapped, we just increment the
1215 * hold count, and activate it.
1219 * In order to get the page table page, try the
1222 if (pmap->pm_ptphint &&
1223 (pmap->pm_ptphint->pindex == ptepindex)) {
1224 m = pmap->pm_ptphint;
1226 m = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1227 pmap->pm_ptphint = m;
1234 * Here if the pte page isn't mapped, or if it has been deallocated.
1236 return _pmap_allocpte(pmap, ptepindex);
1239 /************************************************************************
1240 * Managed pages in pmaps *
1241 ************************************************************************
1243 * All pages entered into user pmaps and some pages entered into the kernel
1244 * pmap are managed, meaning that pmap_protect() and other related management
1245 * functions work on these pages.
1249 * free the pv_entry back to the free list. This function may be
1250 * called from an interrupt.
1252 static __inline void
1253 free_pv_entry(pv_entry_t pv)
1260 * get a new pv_entry, allocating a block from the system
1261 * when needed. This function may be called from an interrupt.
1267 if (pv_entry_high_water &&
1268 (pv_entry_count > pv_entry_high_water) &&
1269 (pmap_pagedaemon_waken == 0)) {
1270 pmap_pagedaemon_waken = 1;
1271 wakeup (&vm_pages_needed);
1273 return zalloc(&pvzone);
1277 * This routine is very drastic, but can save the system
1287 static int warningdone=0;
1289 if (pmap_pagedaemon_waken == 0)
1291 lwkt_gettoken(&vm_token);
1292 pmap_pagedaemon_waken = 0;
1294 if (warningdone < 5) {
1295 kprintf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1299 for (i = 0; i < vm_page_array_size; i++) {
1300 m = &vm_page_array[i];
1301 if (m->wire_count || m->hold_count)
1303 if (vm_page_busy_try(m, TRUE) == 0) {
1304 if (m->wire_count == 0 && m->hold_count == 0) {
1310 lwkt_reltoken(&vm_token);
1314 * If it is the first entry on the list, it is actually
1315 * in the header and we must copy the following entry up
1316 * to the header. Otherwise we must search the list for
1317 * the entry. In either case we free the now unused entry.
1319 * caller must hold vm_token
1322 pmap_remove_entry(struct pmap *pmap, vm_page_t m, vm_offset_t va)
1328 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1329 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1330 if (pmap == pv->pv_pmap && va == pv->pv_va)
1334 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1335 if (va == pv->pv_va)
1341 * Note that pv_ptem is NULL if the page table page itself is not
1342 * managed, even if the page being removed IS managed.
1346 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1347 m->md.pv_list_count--;
1348 atomic_add_int(&m->object->agg_pv_list_count, -1);
1349 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1350 if (TAILQ_EMPTY(&m->md.pv_list))
1351 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1352 ++pmap->pm_generation;
1353 vm_object_hold(pmap->pm_pteobj);
1354 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem);
1355 vm_object_drop(pmap->pm_pteobj);
1363 * Create a pv entry for page at pa for (pmap, va). If the page table page
1364 * holding the VA is managed, mpte will be non-NULL.
1367 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
1372 pv = get_pv_entry();
1377 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1378 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1379 ++pmap->pm_generation;
1380 m->md.pv_list_count++;
1381 atomic_add_int(&m->object->agg_pv_list_count, 1);
1387 * pmap_remove_pte: do the things to unmap a page in a process
1390 pmap_remove_pte(struct pmap *pmap, vpte_t *ptq, vm_offset_t va)
1395 oldpte = pmap_inval_loadandclear(ptq, pmap, va);
1396 if (oldpte & VPTE_WIRED)
1397 --pmap->pm_stats.wired_count;
1398 KKASSERT(pmap->pm_stats.wired_count >= 0);
1402 * Machines that don't support invlpg, also don't support
1403 * VPTE_G. XXX VPTE_G is disabled for SMP so don't worry about
1406 if (oldpte & VPTE_G)
1407 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
1409 KKASSERT(pmap->pm_stats.resident_count > 0);
1410 --pmap->pm_stats.resident_count;
1411 if (oldpte & VPTE_MANAGED) {
1412 m = PHYS_TO_VM_PAGE(oldpte);
1413 if (oldpte & VPTE_M) {
1414 #if defined(PMAP_DIAGNOSTIC)
1415 if (pmap_nw_modified((pt_entry_t) oldpte)) {
1417 "pmap_remove: modified page not writable: va: 0x%x, pte: 0x%x\n",
1421 if (pmap_track_modified(pmap, va))
1424 if (oldpte & VPTE_A)
1425 vm_page_flag_set(m, PG_REFERENCED);
1426 return pmap_remove_entry(pmap, m, va);
1428 return pmap_unuse_pt(pmap, va, NULL);
1437 * Remove a single page from a process address space.
1439 * This function may not be called from an interrupt if the pmap is
1443 pmap_remove_page(struct pmap *pmap, vm_offset_t va)
1448 * if there is no pte for this address, just skip it!!! Otherwise
1449 * get a local va for mappings for this pmap and remove the entry.
1451 if (*pmap_pde(pmap, va) != 0) {
1452 ptq = get_ptbase(pmap, va);
1454 pmap_remove_pte(pmap, ptq, va);
1460 * Remove the given range of addresses from the specified map.
1462 * It is assumed that the start and end are properly rounded to the
1465 * This function may not be called from an interrupt if the pmap is
1471 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
1475 vm_offset_t ptpaddr;
1476 vm_pindex_t sindex, eindex;
1481 vm_object_hold(pmap->pm_pteobj);
1482 lwkt_gettoken(&vm_token);
1483 KKASSERT(pmap->pm_stats.resident_count >= 0);
1484 if (pmap->pm_stats.resident_count == 0) {
1485 lwkt_reltoken(&vm_token);
1486 vm_object_drop(pmap->pm_pteobj);
1491 * special handling of removing one page. a very
1492 * common operation and easy to short circuit some
1495 if (((sva + PAGE_SIZE) == eva) &&
1496 ((pmap->pm_pdir[(sva >> PDRSHIFT)] & VPTE_PS) == 0)) {
1497 pmap_remove_page(pmap, sva);
1498 lwkt_reltoken(&vm_token);
1499 vm_object_drop(pmap->pm_pteobj);
1504 * Get a local virtual address for the mappings that are being
1507 * XXX this is really messy because the kernel pmap is not relative
1510 sindex = (sva >> PAGE_SHIFT);
1511 eindex = (eva >> PAGE_SHIFT);
1513 for (; sindex < eindex; sindex = pdnxt) {
1517 * Calculate index for next page table.
1519 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1520 if (pmap->pm_stats.resident_count == 0)
1523 pdirindex = sindex / NPDEPG;
1524 if (((ptpaddr = pmap->pm_pdir[pdirindex]) & VPTE_PS) != 0) {
1525 KKASSERT(pmap->pm_pdir[pdirindex] != 0);
1526 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1527 pmap_inval_pde(&pmap->pm_pdir[pdirindex], pmap,
1528 (vm_offset_t)pdirindex << SEG_SHIFT);
1533 * Weed out invalid mappings. Note: we assume that the page
1534 * directory table is always allocated, and in kernel virtual.
1540 * Limit our scan to either the end of the va represented
1541 * by the current page table page, or to the end of the
1542 * range being removed.
1548 * NOTE: pmap_remove_pte() can block.
1550 for (; sindex != pdnxt; sindex++) {
1553 ptbase = get_ptbase(pmap, sindex << PAGE_SHIFT);
1556 va = i386_ptob(sindex);
1557 if (pmap_remove_pte(pmap, ptbase, va))
1561 lwkt_reltoken(&vm_token);
1562 vm_object_drop(pmap->pm_pteobj);
1566 * Removes this physical page from all physical maps in which it resides.
1567 * Reflects back modify bits to the pager.
1569 * This routine may not be called from an interrupt.
1574 pmap_remove_all(vm_page_t m)
1579 #if defined(PMAP_DIAGNOSTIC)
1581 * XXX this makes pmap_page_protect(NONE) illegal for non-managed
1584 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
1585 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%08llx", (long long)VM_PAGE_TO_PHYS(m));
1589 lwkt_gettoken(&vm_token);
1590 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
1591 KKASSERT(pv->pv_pmap->pm_stats.resident_count > 0);
1592 --pv->pv_pmap->pm_stats.resident_count;
1594 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
1595 KKASSERT(pte != NULL);
1597 tpte = pmap_inval_loadandclear(pte, pv->pv_pmap, pv->pv_va);
1598 if (tpte & VPTE_WIRED)
1599 --pv->pv_pmap->pm_stats.wired_count;
1600 KKASSERT(pv->pv_pmap->pm_stats.wired_count >= 0);
1603 vm_page_flag_set(m, PG_REFERENCED);
1606 * Update the vm_page_t clean and reference bits.
1608 if (tpte & VPTE_M) {
1609 #if defined(PMAP_DIAGNOSTIC)
1610 if (pmap_nw_modified((pt_entry_t) tpte)) {
1612 "pmap_remove_all: modified page not writable: va: 0x%x, pte: 0x%x\n",
1616 if (pmap_track_modified(pv->pv_pmap, pv->pv_va))
1619 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1620 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
1621 ++pv->pv_pmap->pm_generation;
1622 m->md.pv_list_count--;
1623 atomic_add_int(&m->object->agg_pv_list_count, -1);
1624 if (TAILQ_EMPTY(&m->md.pv_list))
1625 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1626 vm_object_hold(pv->pv_pmap->pm_pteobj);
1627 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
1628 vm_object_drop(pv->pv_pmap->pm_pteobj);
1631 KKASSERT((m->flags & (PG_MAPPED | PG_WRITEABLE)) == 0);
1632 lwkt_reltoken(&vm_token);
1636 * Set the physical protection on the specified range of this map
1639 * This function may not be called from an interrupt if the map is
1640 * not the kernel_pmap.
1645 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
1649 vm_offset_t pdnxt, ptpaddr;
1650 vm_pindex_t sindex, eindex;
1656 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1657 pmap_remove(pmap, sva, eva);
1661 if (prot & VM_PROT_WRITE)
1664 lwkt_gettoken(&vm_token);
1665 ptbase = get_ptbase(pmap, sva);
1667 sindex = (sva >> PAGE_SHIFT);
1668 eindex = (eva >> PAGE_SHIFT);
1671 for (; sindex < eindex; sindex = pdnxt) {
1675 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1677 pdirindex = sindex / NPDEPG;
1680 * Clear the modified and writable bits for a 4m page.
1681 * Throw away the modified bit (?)
1683 if (((ptpaddr = pmap->pm_pdir[pdirindex]) & VPTE_PS) != 0) {
1684 pmap_clean_pde(&pmap->pm_pdir[pdirindex], pmap,
1685 (vm_offset_t)pdirindex << SEG_SHIFT);
1686 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1691 * Weed out invalid mappings. Note: we assume that the page
1692 * directory table is always allocated, and in kernel virtual.
1697 if (pdnxt > eindex) {
1701 for (; sindex != pdnxt; sindex++) {
1706 * Clean managed pages and also check the accessed
1707 * bit. Just remove write perms for unmanaged
1708 * pages. Be careful of races, turning off write
1709 * access will force a fault rather then setting
1710 * the modified bit at an unexpected time.
1712 ptep = &ptbase[sindex - sbase];
1713 if (*ptep & VPTE_MANAGED) {
1714 pbits = pmap_clean_pte(ptep, pmap,
1717 if (pbits & VPTE_A) {
1718 m = PHYS_TO_VM_PAGE(pbits);
1719 vm_page_flag_set(m, PG_REFERENCED);
1720 atomic_clear_long(ptep, VPTE_A);
1722 if (pbits & VPTE_M) {
1723 if (pmap_track_modified(pmap, i386_ptob(sindex))) {
1725 m = PHYS_TO_VM_PAGE(pbits);
1730 pbits = pmap_setro_pte(ptep, pmap,
1735 lwkt_reltoken(&vm_token);
1739 * Enter a managed page into a pmap. If the page is not wired related pmap
1740 * data can be destroyed at any time for later demand-operation.
1742 * Insert the vm_page (m) at virtual address (v) in (pmap), with the
1743 * specified protection, and wire the mapping if requested.
1745 * NOTE: This routine may not lazy-evaluate or lose information. The
1746 * page must actually be inserted into the given map NOW.
1748 * NOTE: When entering a page at a KVA address, the pmap must be the
1754 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1760 vpte_t origpte, newpte;
1768 vm_object_hold(pmap->pm_pteobj);
1769 lwkt_gettoken(&vm_token);
1772 * Get the page table page. The kernel_pmap's page table pages
1773 * are preallocated and have no associated vm_page_t.
1775 if (pmap == &kernel_pmap)
1778 mpte = pmap_allocpte(pmap, va);
1780 pte = pmap_pte(pmap, va);
1783 * Page Directory table entry not valid, we need a new PT page
1784 * and pmap_allocpte() didn't give us one. Oops!
1787 panic("pmap_enter: invalid page directory pmap=%p, va=0x%p\n",
1792 * Deal with races on the original mapping (though don't worry
1793 * about VPTE_A races) by cleaning it. This will force a fault
1794 * if an attempt is made to write to the page.
1796 pa = VM_PAGE_TO_PHYS(m) & VPTE_FRAME;
1797 origpte = pmap_clean_pte(pte, pmap, va);
1798 opa = origpte & VPTE_FRAME;
1800 if (origpte & VPTE_PS)
1801 panic("pmap_enter: attempted pmap_enter on 4MB page");
1804 * Mapping has not changed, must be protection or wiring change.
1806 if (origpte && (opa == pa)) {
1808 * Wiring change, just update stats. We don't worry about
1809 * wiring PT pages as they remain resident as long as there
1810 * are valid mappings in them. Hence, if a user page is wired,
1811 * the PT page will be also.
1813 if (wired && ((origpte & VPTE_WIRED) == 0))
1814 ++pmap->pm_stats.wired_count;
1815 else if (!wired && (origpte & VPTE_WIRED))
1816 --pmap->pm_stats.wired_count;
1817 KKASSERT(pmap->pm_stats.wired_count >= 0);
1820 * Remove the extra pte reference. Note that we cannot
1821 * optimize the RO->RW case because we have adjusted the
1822 * wiring count above and may need to adjust the wiring
1829 * We might be turning off write access to the page,
1830 * so we go ahead and sense modify status.
1832 if (origpte & VPTE_MANAGED) {
1833 if ((origpte & VPTE_M) &&
1834 pmap_track_modified(pmap, va)) {
1836 om = PHYS_TO_VM_PAGE(opa);
1840 KKASSERT(m->flags & PG_MAPPED);
1845 * Mapping has changed, invalidate old range and fall through to
1846 * handle validating new mapping.
1850 err = pmap_remove_pte(pmap, pte, va);
1852 panic("pmap_enter: pte vanished, va: %p", (void *)va);
1853 pte = pmap_pte(pmap, va);
1854 origpte = pmap_clean_pte(pte, pmap, va);
1855 opa = origpte & VPTE_FRAME;
1857 kprintf("pmap_enter: Warning, raced pmap %p va %p\n",
1863 * Enter on the PV list if part of our managed memory. Note that we
1864 * raise IPL while manipulating pv_table since pmap_enter can be
1865 * called at interrupt time.
1867 if (pmap_initialized &&
1868 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
1869 pmap_insert_entry(pmap, va, mpte, m);
1871 vm_page_flag_set(m, PG_MAPPED);
1875 * Increment counters
1877 ++pmap->pm_stats.resident_count;
1879 pmap->pm_stats.wired_count++;
1883 * Now validate mapping with desired protection/wiring.
1885 newpte = (vm_offset_t) (pa | pte_prot(pmap, prot) | VPTE_V);
1888 newpte |= VPTE_WIRED;
1889 if (pmap != &kernel_pmap)
1893 * If the mapping or permission bits are different from the
1894 * (now cleaned) original pte, an update is needed. We've
1895 * already downgraded or invalidated the page so all we have
1896 * to do now is update the bits.
1898 * XXX should we synchronize RO->RW changes to avoid another
1901 if ((origpte & ~(VPTE_W|VPTE_M|VPTE_A)) != newpte) {
1902 *pte = newpte | VPTE_A;
1903 if (newpte & VPTE_W)
1904 vm_page_flag_set(m, PG_WRITEABLE);
1906 KKASSERT((newpte & VPTE_MANAGED) == 0 || m->flags & PG_MAPPED);
1907 lwkt_reltoken(&vm_token);
1908 vm_object_drop(pmap->pm_pteobj);
1912 * This code works like pmap_enter() but assumes VM_PROT_READ and not-wired.
1914 * Currently this routine may only be used on user pmaps, not kernel_pmap.
1917 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m)
1925 KKASSERT(pmap != &kernel_pmap);
1927 KKASSERT(va >= VM_MIN_USER_ADDRESS && va < VM_MAX_USER_ADDRESS);
1930 * Calculate pagetable page (mpte), allocating it if necessary.
1932 * A held page table page (mpte), or NULL, is passed onto the
1933 * section following.
1935 ptepindex = va >> PDRSHIFT;
1937 vm_object_hold(pmap->pm_pteobj);
1938 lwkt_gettoken(&vm_token);
1942 * Get the page directory entry
1944 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1947 * If the page table page is mapped, we just increment
1948 * the hold count, and activate it.
1951 if (ptepa & VPTE_PS)
1952 panic("pmap_enter_quick: unexpected mapping into 4MB page");
1953 if (pmap->pm_ptphint &&
1954 (pmap->pm_ptphint->pindex == ptepindex)) {
1955 mpte = pmap->pm_ptphint;
1957 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1958 pmap->pm_ptphint = mpte;
1959 vm_page_wakeup(mpte);
1964 mpte = _pmap_allocpte(pmap, ptepindex);
1966 } while (mpte == NULL);
1969 * Ok, now that the page table page has been validated, get the pte.
1970 * If the pte is already mapped undo mpte's hold_count and
1973 pte = pmap_pte(pmap, va);
1975 pmap_unwire_pte_hold(pmap, mpte);
1976 lwkt_reltoken(&vm_token);
1977 vm_object_drop(pmap->pm_pteobj);
1982 * Enter on the PV list if part of our managed memory. Note that we
1983 * raise IPL while manipulating pv_table since pmap_enter can be
1984 * called at interrupt time.
1986 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
1987 pmap_insert_entry(pmap, va, mpte, m);
1988 vm_page_flag_set(m, PG_MAPPED);
1992 * Increment counters
1994 ++pmap->pm_stats.resident_count;
1996 pa = VM_PAGE_TO_PHYS(m);
1999 * Now validate mapping with RO protection
2001 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2002 *pte = (vpte_t)pa | VPTE_V | VPTE_U;
2004 *pte = (vpte_t)pa | VPTE_V | VPTE_U | VPTE_MANAGED;
2005 /*pmap_inval_add(&info, pmap, va); shouldn't be needed 0->valid */
2006 /*pmap_inval_flush(&info); don't need for vkernel */
2007 lwkt_reltoken(&vm_token);
2008 vm_object_drop(pmap->pm_pteobj);
2012 * Extract the physical address for the translation at the specified
2013 * virtual address in the pmap.
2015 * The caller must hold vm_token if non-blocking operation is desired.
2019 pmap_extract(pmap_t pmap, vm_offset_t va)
2024 lwkt_gettoken(&vm_token);
2025 if (pmap && (pte = pmap->pm_pdir[va >> SEG_SHIFT]) != 0) {
2026 if (pte & VPTE_PS) {
2027 rtval = pte & ~((vpte_t)(1 << SEG_SHIFT) - 1);
2028 rtval |= va & SEG_MASK;
2030 pte = *get_ptbase(pmap, va);
2031 rtval = (pte & VPTE_FRAME) | (va & PAGE_MASK);
2036 lwkt_reltoken(&vm_token);
2040 #define MAX_INIT_PT (96)
2043 * This routine preloads the ptes for a given object into the specified pmap.
2044 * This eliminates the blast of soft faults on process startup and
2045 * immediately after an mmap.
2049 static int pmap_object_init_pt_callback(vm_page_t p, void *data);
2052 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2053 vm_object_t object, vm_pindex_t pindex,
2054 vm_size_t size, int limit)
2056 struct rb_vm_page_scan_info info;
2061 * We can't preinit if read access isn't set or there is no pmap
2064 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2068 * We can't preinit if the pmap is not the current pmap
2070 lp = curthread->td_lwp;
2071 if (lp == NULL || pmap != vmspace_pmap(lp->lwp_vmspace))
2074 psize = size >> PAGE_SHIFT;
2076 if ((object->type != OBJT_VNODE) ||
2077 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2078 (object->resident_page_count > MAX_INIT_PT))) {
2082 if (psize + pindex > object->size) {
2083 if (object->size < pindex)
2085 psize = object->size - pindex;
2092 * Use a red-black scan to traverse the requested range and load
2093 * any valid pages found into the pmap.
2095 * We cannot safely scan the object's memq unless we are in a
2096 * critical section since interrupts can remove pages from objects.
2098 info.start_pindex = pindex;
2099 info.end_pindex = pindex + psize - 1;
2105 vm_object_hold(object);
2106 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2107 pmap_object_init_pt_callback, &info);
2108 vm_object_drop(object);
2112 * The caller must hold vm_token.
2116 pmap_object_init_pt_callback(vm_page_t p, void *data)
2118 struct rb_vm_page_scan_info *info = data;
2119 vm_pindex_t rel_index;
2122 * don't allow an madvise to blow away our really
2123 * free pages allocating pv entries.
2125 if ((info->limit & MAP_PREFAULT_MADVISE) &&
2126 vmstats.v_free_count < vmstats.v_free_reserved) {
2129 if (vm_page_busy_try(p, TRUE))
2131 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2132 (p->flags & PG_FICTITIOUS) == 0) {
2133 if ((p->queue - p->pc) == PQ_CACHE)
2134 vm_page_deactivate(p);
2135 rel_index = p->pindex - info->start_pindex;
2136 pmap_enter_quick(info->pmap,
2137 info->addr + i386_ptob(rel_index), p);
2144 * Return TRUE if the pmap is in shape to trivially
2145 * pre-fault the specified address.
2147 * Returns FALSE if it would be non-trivial or if a
2148 * pte is already loaded into the slot.
2153 pmap_prefault_ok(pmap_t pmap, vm_offset_t addr)
2158 lwkt_gettoken(&vm_token);
2159 if ((*pmap_pde(pmap, addr)) == 0) {
2162 pte = get_ptbase(pmap, addr);
2163 ret = (*pte) ? 0 : 1;
2165 lwkt_reltoken(&vm_token);
2170 * Change the wiring attribute for a map/virtual-address pair.
2171 * The mapping must already exist in the pmap.
2173 * No other requirements.
2176 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2183 lwkt_gettoken(&vm_token);
2184 pte = get_ptbase(pmap, va);
2186 if (wired && (*pte & VPTE_WIRED) == 0)
2187 ++pmap->pm_stats.wired_count;
2188 else if (!wired && (*pte & VPTE_WIRED))
2189 --pmap->pm_stats.wired_count;
2190 KKASSERT(pmap->pm_stats.wired_count >= 0);
2193 * Wiring is not a hardware characteristic so there is no need to
2194 * invalidate TLB. However, in an SMP environment we must use
2195 * a locked bus cycle to update the pte (if we are not using
2196 * the pmap_inval_*() API that is)... it's ok to do this for simple
2200 atomic_set_long(pte, VPTE_WIRED);
2202 atomic_clear_long(pte, VPTE_WIRED);
2203 lwkt_reltoken(&vm_token);
2207 * Copy the range specified by src_addr/len
2208 * from the source map to the range dst_addr/len
2209 * in the destination map.
2211 * This routine is only advisory and need not do anything.
2214 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2215 vm_size_t len, vm_offset_t src_addr)
2218 vm_offset_t end_addr = src_addr + len;
2225 * XXX BUGGY. Amoung other things srcmpte is assumed to remain
2226 * valid through blocking calls, and that's just not going to
2233 if (dst_addr != src_addr)
2235 if (dst_pmap->pm_pdir == NULL)
2237 if (src_pmap->pm_pdir == NULL)
2240 lwkt_gettoken(&vm_token);
2242 src_frame = get_ptbase1(src_pmap, src_addr);
2243 dst_frame = get_ptbase2(dst_pmap, src_addr);
2246 * critical section protection is required to maintain the page/object
2247 * association, interrupts can free pages and remove them from
2250 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
2251 vpte_t *src_pte, *dst_pte;
2252 vm_page_t dstmpte, srcmpte;
2253 vm_offset_t srcptepaddr;
2256 if (addr >= VM_MAX_USER_ADDRESS)
2257 panic("pmap_copy: invalid to pmap_copy page tables\n");
2260 * Don't let optional prefaulting of pages make us go
2261 * way below the low water mark of free pages or way
2262 * above high water mark of used pv entries.
2264 if (vmstats.v_free_count < vmstats.v_free_reserved ||
2265 pv_entry_count > pv_entry_high_water)
2268 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
2269 ptepindex = addr >> PDRSHIFT;
2271 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex];
2272 if (srcptepaddr == 0)
2275 if (srcptepaddr & VPTE_PS) {
2276 if (dst_pmap->pm_pdir[ptepindex] == 0) {
2277 dst_pmap->pm_pdir[ptepindex] = (vpte_t)srcptepaddr;
2278 dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
2283 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
2284 if ((srcmpte == NULL) || (srcmpte->hold_count == 0) ||
2285 (srcmpte->flags & PG_BUSY)) {
2289 if (pdnxt > end_addr)
2292 src_pte = src_frame + ((addr - src_addr) >> PAGE_SHIFT);
2293 dst_pte = dst_frame + ((addr - src_addr) >> PAGE_SHIFT);
2294 while (addr < pdnxt) {
2299 * we only virtual copy managed pages
2301 if ((ptetemp & VPTE_MANAGED) != 0) {
2303 * We have to check after allocpte for the
2304 * pte still being around... allocpte can
2307 * pmap_allocpte can block, unfortunately
2308 * we have to reload the tables.
2310 dstmpte = pmap_allocpte(dst_pmap, addr);
2311 src_frame = get_ptbase1(src_pmap, src_addr);
2312 dst_frame = get_ptbase2(dst_pmap, src_addr);
2314 if ((*dst_pte == 0) && (ptetemp = *src_pte) &&
2315 (ptetemp & VPTE_MANAGED) != 0) {
2317 * Clear the modified and accessed
2318 * (referenced) bits during the copy.
2320 * We do not have to clear the write
2321 * bit to force a fault-on-modify
2322 * because the real kernel's target
2323 * pmap is empty and will fault anyway.
2325 m = PHYS_TO_VM_PAGE(ptetemp);
2326 *dst_pte = ptetemp & ~(VPTE_M | VPTE_A);
2327 ++dst_pmap->pm_stats.resident_count;
2328 pmap_insert_entry(dst_pmap, addr,
2330 KKASSERT(m->flags & PG_MAPPED);
2332 pmap_unwire_pte_hold(dst_pmap, dstmpte);
2334 if (dstmpte->hold_count >= srcmpte->hold_count)
2342 lwkt_reltoken(&vm_token);
2348 * Zero the specified PA by mapping the page into KVM and clearing its
2351 * This function may be called from an interrupt and no locking is
2355 pmap_zero_page(vm_paddr_t phys)
2357 struct mdglobaldata *gd = mdcpu;
2361 panic("pmap_zero_page: CMAP3 busy");
2362 *gd->gd_CMAP3 = VPTE_V | VPTE_R | VPTE_W | (phys & VPTE_FRAME) | VPTE_A | VPTE_M;
2363 madvise(gd->gd_CADDR3, PAGE_SIZE, MADV_INVAL);
2365 bzero(gd->gd_CADDR3, PAGE_SIZE);
2371 * pmap_page_assertzero:
2373 * Assert that a page is empty, panic if it isn't.
2376 pmap_page_assertzero(vm_paddr_t phys)
2378 struct mdglobaldata *gd = mdcpu;
2383 panic("pmap_zero_page: CMAP3 busy");
2384 *gd->gd_CMAP3 = VPTE_V | VPTE_R | VPTE_W |
2385 (phys & VPTE_FRAME) | VPTE_A | VPTE_M;
2386 madvise(gd->gd_CADDR3, PAGE_SIZE, MADV_INVAL);
2387 for (i = 0; i < PAGE_SIZE; i += 4) {
2388 if (*(int *)((char *)gd->gd_CADDR3 + i) != 0) {
2389 panic("pmap_page_assertzero() @ %p not zero!\n",
2390 (void *)gd->gd_CADDR3);
2400 * Zero part of a physical page by mapping it into memory and clearing
2401 * its contents with bzero.
2403 * off and size may not cover an area beyond a single hardware page.
2406 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
2408 struct mdglobaldata *gd = mdcpu;
2412 panic("pmap_zero_page: CMAP3 busy");
2413 *gd->gd_CMAP3 = VPTE_V | VPTE_R | VPTE_W |
2414 (phys & VPTE_FRAME) | VPTE_A | VPTE_M;
2415 madvise(gd->gd_CADDR3, PAGE_SIZE, MADV_INVAL);
2417 bzero((char *)gd->gd_CADDR3 + off, size);
2425 * Copy the physical page from the source PA to the target PA.
2426 * This function may be called from an interrupt. No locking
2430 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
2432 struct mdglobaldata *gd = mdcpu;
2435 if (*(int *) gd->gd_CMAP1)
2436 panic("pmap_copy_page: CMAP1 busy");
2437 if (*(int *) gd->gd_CMAP2)
2438 panic("pmap_copy_page: CMAP2 busy");
2440 *(int *) gd->gd_CMAP1 = VPTE_V | VPTE_R | (src & PG_FRAME) | VPTE_A;
2441 *(int *) gd->gd_CMAP2 = VPTE_V | VPTE_R | VPTE_W | (dst & VPTE_FRAME) | VPTE_A | VPTE_M;
2443 madvise(gd->gd_CADDR1, PAGE_SIZE, MADV_INVAL);
2444 madvise(gd->gd_CADDR2, PAGE_SIZE, MADV_INVAL);
2446 bcopy(gd->gd_CADDR1, gd->gd_CADDR2, PAGE_SIZE);
2448 *(int *) gd->gd_CMAP1 = 0;
2449 *(int *) gd->gd_CMAP2 = 0;
2454 * pmap_copy_page_frag:
2456 * Copy the physical page from the source PA to the target PA.
2457 * This function may be called from an interrupt. No locking
2461 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
2463 struct mdglobaldata *gd = mdcpu;
2466 if (*(int *) gd->gd_CMAP1)
2467 panic("pmap_copy_page: CMAP1 busy");
2468 if (*(int *) gd->gd_CMAP2)
2469 panic("pmap_copy_page: CMAP2 busy");
2471 *(int *) gd->gd_CMAP1 = VPTE_V | (src & VPTE_FRAME) | VPTE_A;
2472 *(int *) gd->gd_CMAP2 = VPTE_V | VPTE_R | VPTE_W | (dst & VPTE_FRAME) | VPTE_A | VPTE_M;
2474 madvise(gd->gd_CADDR1, PAGE_SIZE, MADV_INVAL);
2475 madvise(gd->gd_CADDR2, PAGE_SIZE, MADV_INVAL);
2477 bcopy((char *)gd->gd_CADDR1 + (src & PAGE_MASK),
2478 (char *)gd->gd_CADDR2 + (dst & PAGE_MASK),
2481 *(int *) gd->gd_CMAP1 = 0;
2482 *(int *) gd->gd_CMAP2 = 0;
2487 * Returns true if the pmap's pv is one of the first
2488 * 16 pvs linked to from this page. This count may
2489 * be changed upwards or downwards in the future; it
2490 * is only necessary that true be returned for a small
2491 * subset of pmaps for proper page aging.
2496 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2501 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2505 lwkt_gettoken(&vm_token);
2507 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2508 if (pv->pv_pmap == pmap) {
2509 lwkt_reltoken(&vm_token);
2517 lwkt_reltoken(&vm_token);
2523 * Remove all pages from specified address space
2524 * this aids process exit speeds. Also, this code
2525 * is special cased for current process only, but
2526 * can have the more generic (and slightly slower)
2527 * mode enabled. This is much faster than pmap_remove
2528 * in the case of running down an entire address space.
2533 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2538 int32_t save_generation;
2540 if (pmap->pm_pteobj)
2541 vm_object_hold(pmap->pm_pteobj);
2542 lwkt_gettoken(&vm_token);
2543 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
2544 if (pv->pv_va >= eva || pv->pv_va < sva) {
2545 npv = TAILQ_NEXT(pv, pv_plist);
2549 KKASSERT(pmap == pv->pv_pmap);
2551 pte = pmap_pte(pmap, pv->pv_va);
2554 * We cannot remove wired pages from a process' mapping
2557 if (*pte & VPTE_WIRED) {
2558 npv = TAILQ_NEXT(pv, pv_plist);
2561 tpte = pmap_inval_loadandclear(pte, pmap, pv->pv_va);
2563 m = PHYS_TO_VM_PAGE(tpte);
2565 KASSERT(m < &vm_page_array[vm_page_array_size],
2566 ("pmap_remove_pages: bad tpte %lx", tpte));
2568 KKASSERT(pmap->pm_stats.resident_count > 0);
2569 --pmap->pm_stats.resident_count;
2572 * Update the vm_page_t clean and reference bits.
2574 if (tpte & VPTE_M) {
2578 npv = TAILQ_NEXT(pv, pv_plist);
2579 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
2580 save_generation = ++pmap->pm_generation;
2582 m->md.pv_list_count--;
2583 atomic_add_int(&m->object->agg_pv_list_count, -1);
2584 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2585 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2586 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2588 pmap_unuse_pt(pmap, pv->pv_va, pv->pv_ptem);
2592 * Restart the scan if we blocked during the unuse or free
2593 * calls and other removals were made.
2595 if (save_generation != pmap->pm_generation) {
2596 kprintf("Warning: pmap_remove_pages race-A avoided\n");
2597 npv = TAILQ_FIRST(&pmap->pm_pvlist);
2600 lwkt_reltoken(&vm_token);
2601 if (pmap->pm_pteobj)
2602 vm_object_drop(pmap->pm_pteobj);
2606 * pmap_testbit tests bits in active mappings of a VM page.
2608 * The caller must hold vm_token
2611 pmap_testbit(vm_page_t m, int bit)
2616 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2619 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2624 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2626 * if the bit being tested is the modified bit, then
2627 * mark clean_map and ptes as never
2630 if (bit & (VPTE_A|VPTE_M)) {
2631 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2635 #if defined(PMAP_DIAGNOSTIC)
2637 kprintf("Null pmap (tb) at va: 0x%x\n", pv->pv_va);
2641 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2652 * This routine is used to clear bits in ptes. Certain bits require special
2653 * handling, in particular (on virtual kernels) the VPTE_M (modify) bit.
2655 * This routine is only called with certain VPTE_* bit combinations.
2657 * The caller must hold vm_token
2659 static __inline void
2660 pmap_clearbit(vm_page_t m, int bit)
2666 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2672 * Loop over all current mappings setting/clearing as appropos If
2673 * setting RO do we need to clear the VAC?
2675 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2677 * don't write protect pager mappings
2679 if (bit == VPTE_W) {
2680 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2684 #if defined(PMAP_DIAGNOSTIC)
2686 kprintf("Null pmap (cb) at va: 0x%x\n", pv->pv_va);
2692 * Careful here. We can use a locked bus instruction to
2693 * clear VPTE_A or VPTE_M safely but we need to synchronize
2694 * with the target cpus when we mess with VPTE_W.
2696 * On virtual kernels we must force a new fault-on-write
2697 * in the real kernel if we clear the Modify bit ourselves,
2698 * otherwise the real kernel will not get a new fault and
2699 * will never set our Modify bit again.
2701 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2703 if (bit == VPTE_W) {
2705 * We must also clear VPTE_M when clearing
2708 pbits = pmap_clean_pte(pte, pv->pv_pmap,
2712 } else if (bit == VPTE_M) {
2714 * We do not have to make the page read-only
2715 * when clearing the Modify bit. The real
2716 * kernel will make the real PTE read-only
2717 * or otherwise detect the write and set
2718 * our VPTE_M again simply by us invalidating
2719 * the real kernel VA for the pmap (as we did
2720 * above). This allows the real kernel to
2721 * handle the write fault without forwarding
2724 atomic_clear_long(pte, VPTE_M);
2725 } else if ((bit & (VPTE_W|VPTE_M)) == (VPTE_W|VPTE_M)) {
2727 * We've been asked to clear W & M, I guess
2728 * the caller doesn't want us to update
2729 * the dirty status of the VM page.
2731 pmap_clean_pte(pte, pv->pv_pmap, pv->pv_va);
2734 * We've been asked to clear bits that do
2735 * not interact with hardware.
2737 atomic_clear_long(pte, bit);
2745 * Lower the permission for all mappings to a given page.
2750 pmap_page_protect(vm_page_t m, vm_prot_t prot)
2752 if ((prot & VM_PROT_WRITE) == 0) {
2753 lwkt_gettoken(&vm_token);
2754 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
2755 pmap_clearbit(m, VPTE_W);
2756 vm_page_flag_clear(m, PG_WRITEABLE);
2760 lwkt_reltoken(&vm_token);
2765 pmap_phys_address(vm_pindex_t ppn)
2767 return (i386_ptob(ppn));
2771 * Return a count of reference bits for a page, clearing those bits.
2772 * It is not necessary for every reference bit to be cleared, but it
2773 * is necessary that 0 only be returned when there are truly no
2774 * reference bits set.
2776 * XXX: The exact number of bits to check and clear is a matter that
2777 * should be tested and standardized at some point in the future for
2778 * optimal aging of shared pages.
2783 pmap_ts_referenced(vm_page_t m)
2785 pv_entry_t pv, pvf, pvn;
2789 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2793 lwkt_gettoken(&vm_token);
2795 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2800 pvn = TAILQ_NEXT(pv, pv_list);
2802 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2804 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2806 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2809 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2811 if (pte && (*pte & VPTE_A)) {
2813 atomic_clear_long(pte, VPTE_A);
2815 atomic_clear_long_nonlocked(pte, VPTE_A);
2822 } while ((pv = pvn) != NULL && pv != pvf);
2824 lwkt_reltoken(&vm_token);
2831 * Return whether or not the specified physical page was modified
2832 * in any physical maps.
2837 pmap_is_modified(vm_page_t m)
2841 lwkt_gettoken(&vm_token);
2842 res = pmap_testbit(m, VPTE_M);
2843 lwkt_reltoken(&vm_token);
2848 * Clear the modify bits on the specified physical page.
2853 pmap_clear_modify(vm_page_t m)
2855 lwkt_gettoken(&vm_token);
2856 pmap_clearbit(m, VPTE_M);
2857 lwkt_reltoken(&vm_token);
2861 * Clear the reference bit on the specified physical page.
2866 pmap_clear_reference(vm_page_t m)
2868 lwkt_gettoken(&vm_token);
2869 pmap_clearbit(m, VPTE_A);
2870 lwkt_reltoken(&vm_token);
2874 * Miscellaneous support routines follow
2878 i386_protection_init(void)
2882 kp = protection_codes;
2883 for (prot = 0; prot < 8; prot++) {
2884 if (prot & VM_PROT_READ)
2886 if (prot & VM_PROT_WRITE)
2888 if (prot & VM_PROT_EXECUTE)
2897 * Map a set of physical memory pages into the kernel virtual
2898 * address space. Return a pointer to where it is mapped. This
2899 * routine is intended to be used for mapping device memory,
2902 * NOTE: we can't use pgeflag unless we invalidate the pages one at
2906 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
2908 vm_offset_t va, tmpva, offset;
2911 offset = pa & PAGE_MASK;
2912 size = roundup(offset + size, PAGE_SIZE);
2914 va = kmem_alloc_nofault(&kernel_map, size, PAGE_SIZE);
2916 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
2918 pa = pa & VPTE_FRAME;
2919 for (tmpva = va; size > 0;) {
2920 pte = KernelPTA + (tmpva >> PAGE_SHIFT);
2921 *pte = pa | VPTE_R | VPTE_W | VPTE_V; /* | pgeflag; */
2929 return ((void *)(va + offset));
2933 pmap_unmapdev(vm_offset_t va, vm_size_t size)
2935 vm_offset_t base, offset;
2937 base = va & VPTE_FRAME;
2938 offset = va & PAGE_MASK;
2939 size = roundup(offset + size, PAGE_SIZE);
2940 pmap_qremove(va, size >> PAGE_SHIFT);
2941 kmem_free(&kernel_map, base, size);
2947 * Perform the pmap work for mincore
2952 pmap_mincore(pmap_t pmap, vm_offset_t addr)
2958 lwkt_gettoken(&vm_token);
2960 ptep = pmap_pte(pmap, addr);
2962 lwkt_reltoken(&vm_token);
2966 if ((pte = *ptep) != 0) {
2969 val = MINCORE_INCORE;
2970 if ((pte & VPTE_MANAGED) == 0)
2973 pa = pte & VPTE_FRAME;
2975 m = PHYS_TO_VM_PAGE(pa);
2981 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
2983 * Modified by someone
2985 else if (m->dirty || pmap_is_modified(m))
2986 val |= MINCORE_MODIFIED_OTHER;
2991 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
2994 * Referenced by someone
2996 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
2997 val |= MINCORE_REFERENCED_OTHER;
2998 vm_page_flag_set(m, PG_REFERENCED);
3002 lwkt_reltoken(&vm_token);
3007 * Caller must hold vmspace->vm_map.token for oldvm and newvm
3010 pmap_replacevm(struct proc *p, struct vmspace *newvm, int adjrefs)
3012 struct vmspace *oldvm;
3015 oldvm = p->p_vmspace;
3017 if (oldvm != newvm) {
3018 p->p_vmspace = newvm;
3019 KKASSERT(p->p_nthreads == 1);
3020 lp = RB_ROOT(&p->p_lwp_tree);
3021 pmap_setlwpvm(lp, newvm);
3023 sysref_get(&newvm->vm_sysref);
3024 sysref_put(&oldvm->vm_sysref);
3031 pmap_setlwpvm(struct lwp *lp, struct vmspace *newvm)
3033 struct vmspace *oldvm;
3037 oldvm = lp->lwp_vmspace;
3039 if (oldvm != newvm) {
3040 lp->lwp_vmspace = newvm;
3041 if (curthread->td_lwp == lp) {
3042 pmap = vmspace_pmap(newvm);
3044 atomic_set_cpumask(&pmap->pm_active, mycpu->gd_cpumask);
3046 pmap->pm_active |= 1;
3048 #if defined(SWTCH_OPTIM_STATS)
3051 pmap = vmspace_pmap(oldvm);
3053 atomic_clear_cpumask(&pmap->pm_active, mycpu->gd_cpumask);
3055 pmap->pm_active &= ~(cpumask_t)1;
3064 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3067 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3071 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3076 * Used by kmalloc/kfree, page already exists at va
3079 pmap_kvtom(vm_offset_t va)
3083 KKASSERT(va >= KvaStart && va < KvaEnd);
3084 ptep = KernelPTA + (va >> PAGE_SHIFT);
3085 return(PHYS_TO_VM_PAGE(*ptep & PG_FRAME));