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 * Typically used to initialize a fictitious page by vm/device_pager.c
146 pmap_page_init(struct vm_page *m)
149 TAILQ_INIT(&m->md.pv_list);
153 * Bootstrap the kernel_pmap so it can be used with pmap_enter().
155 * NOTE! pm_pdir for the kernel pmap is offset so VA's translate
156 * directly into PTD indexes (PTA is also offset for the same reason).
157 * This is necessary because, for now, KVA is not mapped at address 0.
159 * Page table pages are not managed like they are in normal pmaps, so
160 * no pteobj is needed.
165 vm_pindex_t i = (vm_offset_t)KernelPTD >> PAGE_SHIFT;
168 * The kernel_pmap's pm_pteobj is used only for locking and not
171 kernel_pmap.pm_pdir = KernelPTD - (KvaStart >> SEG_SHIFT);
172 kernel_pmap.pm_pdirpte = KernelPTA[i];
173 kernel_pmap.pm_count = 1;
174 kernel_pmap.pm_active = (cpumask_t)-1 & ~CPUMASK_LOCK;
175 kernel_pmap.pm_pteobj = &kernel_object;
176 TAILQ_INIT(&kernel_pmap.pm_pvlist);
177 TAILQ_INIT(&kernel_pmap.pm_pvlist_free);
178 spin_init(&kernel_pmap.pm_spin);
179 lwkt_token_init(&kernel_pmap.pm_token, "kpmap_tok");
180 i386_protection_init();
184 * Initialize pmap0/vmspace0 . Since process 0 never enters user mode we
185 * just dummy it up so it works well enough for fork().
187 * In DragonFly, process pmaps may only be used to manipulate user address
188 * space, never kernel address space.
191 pmap_pinit0(struct pmap *pmap)
196 /************************************************************************
197 * Procedures to manage whole physical maps *
198 ************************************************************************
200 * Initialize a preallocated and zeroed pmap structure,
201 * such as one in a vmspace structure.
204 pmap_pinit(struct pmap *pmap)
210 * No need to allocate page table space yet but we do need a valid
211 * page directory table.
213 if (pmap->pm_pdir == NULL) {
215 (vpte_t *)kmem_alloc_pageable(&kernel_map, PAGE_SIZE);
219 * allocate object for the pte array and page directory
221 npages = VPTE_PAGETABLE_SIZE +
222 (VM_MAX_USER_ADDRESS / PAGE_SIZE) * sizeof(vpte_t);
223 npages = (npages + PAGE_MASK) / PAGE_SIZE;
225 if (pmap->pm_pteobj == NULL)
226 pmap->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, npages);
227 pmap->pm_pdindex = npages - 1;
230 * allocate the page directory page
232 ptdpg = vm_page_grab(pmap->pm_pteobj, pmap->pm_pdindex,
233 VM_ALLOC_NORMAL | VM_ALLOC_RETRY | VM_ALLOC_ZERO);
236 /* not usually mapped */
237 vm_page_flag_clear(ptdpg, PG_MAPPED);
238 vm_page_wakeup(ptdpg);
240 pmap_kenter((vm_offset_t)pmap->pm_pdir, VM_PAGE_TO_PHYS(ptdpg));
241 pmap->pm_pdirpte = KernelPTA[(vm_offset_t)pmap->pm_pdir >> PAGE_SHIFT];
245 pmap->pm_ptphint = NULL;
246 pmap->pm_cpucachemask = 0;
247 TAILQ_INIT(&pmap->pm_pvlist);
248 TAILQ_INIT(&pmap->pm_pvlist_free);
249 spin_init(&pmap->pm_spin);
250 lwkt_token_init(&pmap->pm_token, "pmap_tok");
251 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
252 pmap->pm_stats.resident_count = 1;
256 * Clean up a pmap structure so it can be physically freed
261 pmap_puninit(pmap_t pmap)
264 kmem_free(&kernel_map, (vm_offset_t)pmap->pm_pdir, PAGE_SIZE);
265 pmap->pm_pdir = NULL;
267 if (pmap->pm_pteobj) {
268 vm_object_deallocate(pmap->pm_pteobj);
269 pmap->pm_pteobj = NULL;
275 * Wire in kernel global address entries. To avoid a race condition
276 * between pmap initialization and pmap_growkernel, this procedure
277 * adds the pmap to the master list (which growkernel scans to update),
278 * then copies the template.
280 * In a virtual kernel there are no kernel global address entries.
285 pmap_pinit2(struct pmap *pmap)
287 spin_lock(&pmap_spin);
288 TAILQ_INSERT_TAIL(&pmap_list, pmap, pm_pmnode);
289 spin_unlock(&pmap_spin);
293 * Release all resources held by the given physical map.
295 * Should only be called if the map contains no valid mappings.
297 * Caller must hold pmap->pm_token
299 static int pmap_release_callback(struct vm_page *p, void *data);
302 pmap_release(struct pmap *pmap)
304 struct mdglobaldata *gd = mdcpu;
305 vm_object_t object = pmap->pm_pteobj;
306 struct rb_vm_page_scan_info info;
308 KKASSERT(pmap != &kernel_pmap);
310 #if defined(DIAGNOSTIC)
311 if (object->ref_count != 1)
312 panic("pmap_release: pteobj reference count != 1");
315 * Once we destroy the page table, the mapping becomes invalid.
316 * Don't waste time doing a madvise to invalidate the mapping, just
317 * set cpucachemask to 0.
319 if (pmap->pm_pdir == gd->gd_PT1pdir) {
320 gd->gd_PT1pdir = NULL;
322 /* madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL); */
324 if (pmap->pm_pdir == gd->gd_PT2pdir) {
325 gd->gd_PT2pdir = NULL;
327 /* madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL); */
329 if (pmap->pm_pdir == gd->gd_PT3pdir) {
330 gd->gd_PT3pdir = NULL;
332 /* madvise(gd->gd_PT3map, SEG_SIZE, MADV_INVAL); */
336 info.object = object;
338 spin_lock(&pmap_spin);
339 TAILQ_REMOVE(&pmap_list, pmap, pm_pmnode);
340 spin_unlock(&pmap_spin);
342 vm_object_hold(object);
346 info.limit = object->generation;
348 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
349 pmap_release_callback, &info);
350 if (info.error == 0 && info.mpte) {
351 if (!pmap_release_free_page(pmap, info.mpte))
354 } while (info.error);
355 vm_object_drop(object);
358 * Leave the KVA reservation for pm_pdir cached for later reuse.
360 pmap->pm_pdirpte = 0;
361 pmap->pm_cpucachemask = 0;
365 * Callback to release a page table page backing a directory
369 pmap_release_callback(struct vm_page *p, void *data)
371 struct rb_vm_page_scan_info *info = data;
373 if (p->pindex == info->pmap->pm_pdindex) {
377 if (!pmap_release_free_page(info->pmap, p)) {
381 if (info->object->generation != info->limit) {
389 * Add a reference to the specified pmap.
394 pmap_reference(pmap_t pmap)
397 lwkt_gettoken(&vm_token);
399 lwkt_reltoken(&vm_token);
403 /************************************************************************
404 * VMSPACE MANAGEMENT *
405 ************************************************************************
407 * The VMSPACE management we do in our virtual kernel must be reflected
408 * in the real kernel. This is accomplished by making vmspace system
409 * calls to the real kernel.
412 cpu_vmspace_alloc(struct vmspace *vm)
417 #define LAST_EXTENT (VM_MAX_USER_ADDRESS - 0x80000000)
419 if (vmspace_create(&vm->vm_pmap, 0, NULL) < 0)
420 panic("vmspace_create() failed");
422 rp = vmspace_mmap(&vm->vm_pmap, (void *)0x00000000, 0x40000000,
423 PROT_READ|PROT_WRITE,
424 MAP_FILE|MAP_SHARED|MAP_VPAGETABLE|MAP_FIXED,
426 if (rp == MAP_FAILED)
427 panic("vmspace_mmap: failed1");
428 vmspace_mcontrol(&vm->vm_pmap, (void *)0x00000000, 0x40000000,
430 rp = vmspace_mmap(&vm->vm_pmap, (void *)0x40000000, 0x40000000,
431 PROT_READ|PROT_WRITE,
432 MAP_FILE|MAP_SHARED|MAP_VPAGETABLE|MAP_FIXED,
433 MemImageFd, 0x40000000);
434 if (rp == MAP_FAILED)
435 panic("vmspace_mmap: failed2");
436 vmspace_mcontrol(&vm->vm_pmap, (void *)0x40000000, 0x40000000,
438 rp = vmspace_mmap(&vm->vm_pmap, (void *)0x80000000, LAST_EXTENT,
439 PROT_READ|PROT_WRITE,
440 MAP_FILE|MAP_SHARED|MAP_VPAGETABLE|MAP_FIXED,
441 MemImageFd, 0x80000000);
442 vmspace_mcontrol(&vm->vm_pmap, (void *)0x80000000, LAST_EXTENT,
444 if (rp == MAP_FAILED)
445 panic("vmspace_mmap: failed3");
447 r = vmspace_mcontrol(&vm->vm_pmap, (void *)0x00000000, 0x40000000,
448 MADV_SETMAP, vmspace_pmap(vm)->pm_pdirpte);
450 panic("vmspace_mcontrol: failed1");
451 r = vmspace_mcontrol(&vm->vm_pmap, (void *)0x40000000, 0x40000000,
452 MADV_SETMAP, vmspace_pmap(vm)->pm_pdirpte);
454 panic("vmspace_mcontrol: failed2");
455 r = vmspace_mcontrol(&vm->vm_pmap, (void *)0x80000000, LAST_EXTENT,
456 MADV_SETMAP, vmspace_pmap(vm)->pm_pdirpte);
458 panic("vmspace_mcontrol: failed3");
462 cpu_vmspace_free(struct vmspace *vm)
464 if (vmspace_destroy(&vm->vm_pmap) < 0)
465 panic("vmspace_destroy() failed");
468 /************************************************************************
469 * Procedures which operate directly on the kernel PMAP *
470 ************************************************************************/
473 * This maps the requested page table and gives us access to it.
475 * This routine can be called from a potentially preempting interrupt
476 * thread or from a normal thread.
479 get_ptbase(struct pmap *pmap, vm_offset_t va)
481 struct mdglobaldata *gd = mdcpu;
483 if (pmap == &kernel_pmap) {
484 KKASSERT(va >= KvaStart && va < KvaEnd);
485 return(KernelPTA + (va >> PAGE_SHIFT));
486 } else if (pmap->pm_pdir == gd->gd_PT1pdir) {
487 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
488 *gd->gd_PT1pde = pmap->pm_pdirpte;
489 madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL);
490 atomic_set_cpumask(&pmap->pm_cpucachemask,
493 return(gd->gd_PT1map + (va >> PAGE_SHIFT));
494 } else if (pmap->pm_pdir == gd->gd_PT2pdir) {
495 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
496 *gd->gd_PT2pde = pmap->pm_pdirpte;
497 madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL);
498 atomic_set_cpumask(&pmap->pm_cpucachemask,
501 return(gd->gd_PT2map + (va >> PAGE_SHIFT));
505 * If we aren't running from a potentially preempting interrupt,
506 * load a new page table directory into the page table cache
508 if (gd->mi.gd_intr_nesting_level == 0 &&
509 (gd->mi.gd_curthread->td_flags & TDF_INTTHREAD) == 0) {
511 * Choose one or the other and map the page table
512 * in the KVA space reserved for it.
514 if ((gd->gd_PTflip = 1 - gd->gd_PTflip) == 0) {
515 gd->gd_PT1pdir = pmap->pm_pdir;
516 *gd->gd_PT1pde = pmap->pm_pdirpte;
517 madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL);
518 atomic_set_cpumask(&pmap->pm_cpucachemask,
520 return(gd->gd_PT1map + (va >> PAGE_SHIFT));
522 gd->gd_PT2pdir = pmap->pm_pdir;
523 *gd->gd_PT2pde = pmap->pm_pdirpte;
524 madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL);
525 atomic_set_cpumask(&pmap->pm_cpucachemask,
527 return(gd->gd_PT2map + (va >> PAGE_SHIFT));
532 * If we are running from a preempting interrupt use a private
533 * map. The caller must be in a critical section.
535 KKASSERT(IN_CRITICAL_SECT(curthread));
536 if (pmap->pm_pdir == gd->gd_PT3pdir) {
537 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
538 *gd->gd_PT3pde = pmap->pm_pdirpte;
539 madvise(gd->gd_PT3map, SEG_SIZE, MADV_INVAL);
540 atomic_set_cpumask(&pmap->pm_cpucachemask,
544 gd->gd_PT3pdir = pmap->pm_pdir;
545 *gd->gd_PT3pde = pmap->pm_pdirpte;
546 madvise(gd->gd_PT3map, SEG_SIZE, MADV_INVAL);
547 atomic_set_cpumask(&pmap->pm_cpucachemask,
550 return(gd->gd_PT3map + (va >> PAGE_SHIFT));
554 get_ptbase1(struct pmap *pmap, vm_offset_t va)
556 struct mdglobaldata *gd = mdcpu;
558 if (pmap == &kernel_pmap) {
559 KKASSERT(va >= KvaStart && va < KvaEnd);
560 return(KernelPTA + (va >> PAGE_SHIFT));
561 } else if (pmap->pm_pdir == gd->gd_PT1pdir) {
562 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
563 *gd->gd_PT1pde = pmap->pm_pdirpte;
564 madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL);
565 atomic_set_cpumask(&pmap->pm_cpucachemask,
568 return(gd->gd_PT1map + (va >> PAGE_SHIFT));
570 KKASSERT(gd->mi.gd_intr_nesting_level == 0 &&
571 (gd->mi.gd_curthread->td_flags & TDF_INTTHREAD) == 0);
572 gd->gd_PT1pdir = pmap->pm_pdir;
573 *gd->gd_PT1pde = pmap->pm_pdirpte;
574 madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL);
575 return(gd->gd_PT1map + (va >> PAGE_SHIFT));
579 get_ptbase2(struct pmap *pmap, vm_offset_t va)
581 struct mdglobaldata *gd = mdcpu;
583 if (pmap == &kernel_pmap) {
584 KKASSERT(va >= KvaStart && va < KvaEnd);
585 return(KernelPTA + (va >> PAGE_SHIFT));
586 } else if (pmap->pm_pdir == gd->gd_PT2pdir) {
587 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
588 *gd->gd_PT2pde = pmap->pm_pdirpte;
589 madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL);
590 atomic_set_cpumask(&pmap->pm_cpucachemask,
593 return(gd->gd_PT2map + (va >> PAGE_SHIFT));
595 KKASSERT(gd->mi.gd_intr_nesting_level == 0 &&
596 (gd->mi.gd_curthread->td_flags & TDF_INTTHREAD) == 0);
597 gd->gd_PT2pdir = pmap->pm_pdir;
598 *gd->gd_PT2pde = pmap->pm_pdirpte;
599 madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL);
600 return(gd->gd_PT2map + (va >> PAGE_SHIFT));
604 * Return a pointer to the page table entry for the specified va in the
605 * specified pmap. NULL is returned if there is no valid page table page
608 static __inline vpte_t *
609 pmap_pte(struct pmap *pmap, vm_offset_t va)
613 ptep = &pmap->pm_pdir[va >> SEG_SHIFT];
617 return (get_ptbase(pmap, va));
623 * Enter a mapping into kernel_pmap. Mappings created in this fashion
624 * are not managed. Mappings must be immediately accessible on all cpus.
626 * Call pmap_inval_pte() to invalidate the virtual pte and clean out the
627 * real pmap and handle related races before storing the new vpte.
630 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
635 KKASSERT(va >= KvaStart && va < KvaEnd);
636 npte = (vpte_t)pa | VPTE_R | VPTE_W | VPTE_V;
637 ptep = KernelPTA + (va >> PAGE_SHIFT);
639 pmap_inval_pte(ptep, &kernel_pmap, va);
644 * Synchronize a kvm mapping originally made for the private use on
645 * some other cpu so it can be used on all cpus.
647 * XXX add MADV_RESYNC to improve performance.
650 pmap_kenter_sync(vm_offset_t va)
652 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
656 * Synchronize a kvm mapping originally made for the private use on
657 * some other cpu so it can be used on our cpu. Turns out to be the
658 * same madvise() call, because we have to sync the real pmaps anyway.
660 * XXX add MADV_RESYNC to improve performance.
663 pmap_kenter_sync_quick(vm_offset_t va)
665 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
670 * Make a previously read-only kernel mapping R+W (not implemented by
674 pmap_kmodify_rw(vm_offset_t va)
676 *pmap_kpte(va) |= VPTE_R | VPTE_W;
677 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
681 * Make a kernel mapping non-cacheable (not applicable to virtual kernels)
684 pmap_kmodify_nc(vm_offset_t va)
686 *pmap_kpte(va) |= VPTE_N;
687 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
693 * Map a contiguous range of physical memory to a KVM
696 pmap_map(vm_offset_t *virtp, vm_paddr_t start, vm_paddr_t end, int prot)
698 vm_offset_t sva, virt;
701 while (start < end) {
702 pmap_kenter(virt, start);
711 pmap_kpte(vm_offset_t va)
715 KKASSERT(va >= KvaStart && va < KvaEnd);
716 ptep = KernelPTA + (va >> PAGE_SHIFT);
721 * Enter an unmanaged KVA mapping for the private use of the current
722 * cpu only. pmap_kenter_sync() may be called to make the mapping usable
725 * It is illegal for the mapping to be accessed by other cpus unleess
726 * pmap_kenter_sync*() is called.
729 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
734 KKASSERT(va >= KvaStart && va < KvaEnd);
736 npte = (vpte_t)pa | VPTE_R | VPTE_W | VPTE_V;
737 ptep = KernelPTA + (va >> PAGE_SHIFT);
739 pmap_inval_pte_quick(ptep, &kernel_pmap, va);
744 * Make a temporary mapping for a physical address. This is only intended
745 * to be used for panic dumps.
747 * The caller is responsible for calling smp_invltlb().
750 pmap_kenter_temporary(vm_paddr_t pa, long i)
752 pmap_kenter_quick(crashdumpmap + (i * PAGE_SIZE), pa);
753 return ((void *)crashdumpmap);
757 * Remove an unmanaged mapping created with pmap_kenter*().
760 pmap_kremove(vm_offset_t va)
764 KKASSERT(va >= KvaStart && va < KvaEnd);
766 ptep = KernelPTA + (va >> PAGE_SHIFT);
768 pmap_inval_pte(ptep, &kernel_pmap, va);
773 * Remove an unmanaged mapping created with pmap_kenter*() but synchronize
774 * only with this cpu.
776 * Unfortunately because we optimize new entries by testing VPTE_V later
777 * on, we actually still have to synchronize with all the cpus. XXX maybe
778 * store a junk value and test against 0 in the other places instead?
781 pmap_kremove_quick(vm_offset_t va)
785 KKASSERT(va >= KvaStart && va < KvaEnd);
787 ptep = KernelPTA + (va >> PAGE_SHIFT);
789 pmap_inval_pte(ptep, &kernel_pmap, va); /* NOT _quick */
794 * Extract the physical address from the kernel_pmap that is associated
795 * with the specified virtual address.
798 pmap_kextract(vm_offset_t va)
803 KKASSERT(va >= KvaStart && va < KvaEnd);
805 ptep = KernelPTA + (va >> PAGE_SHIFT);
806 pa = (vm_paddr_t)(*ptep & VPTE_FRAME) | (va & PAGE_MASK);
811 * Map a set of unmanaged VM pages into KVM.
814 pmap_qenter(vm_offset_t va, struct vm_page **m, int count)
816 KKASSERT(va >= KvaStart && va + count * PAGE_SIZE < KvaEnd);
820 ptep = KernelPTA + (va >> PAGE_SHIFT);
822 pmap_inval_pte(ptep, &kernel_pmap, va);
823 *ptep = (vpte_t)(*m)->phys_addr | VPTE_R | VPTE_W | VPTE_V;
831 * Undo the effects of pmap_qenter*().
834 pmap_qremove(vm_offset_t va, int count)
836 KKASSERT(va >= KvaStart && va + count * PAGE_SIZE < KvaEnd);
840 ptep = KernelPTA + (va >> PAGE_SHIFT);
842 pmap_inval_pte(ptep, &kernel_pmap, va);
849 /************************************************************************
850 * Misc support glue called by machine independant code *
851 ************************************************************************
853 * These routines are called by machine independant code to operate on
854 * certain machine-dependant aspects of processes, threads, and pmaps.
858 * Initialize MD portions of the thread structure.
861 pmap_init_thread(thread_t td)
863 /* enforce pcb placement */
864 td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
865 td->td_savefpu = &td->td_pcb->pcb_save;
866 td->td_sp = (char *)td->td_pcb - 16;
870 * This routine directly affects the fork perf for a process.
873 pmap_init_proc(struct proc *p)
878 * We pre-allocate all page table pages for kernel virtual memory so
879 * this routine will only be called if KVM has been exhausted.
884 pmap_growkernel(vm_offset_t kstart, vm_offset_t kend)
888 addr = (kend + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
890 lwkt_gettoken(&vm_token);
891 if (addr > virtual_end - SEG_SIZE)
892 panic("KVM exhausted");
893 kernel_vm_end = addr;
894 lwkt_reltoken(&vm_token);
898 * The modification bit is not tracked for any pages in this range. XXX
899 * such pages in this maps should always use pmap_k*() functions and not
902 * XXX User and kernel address spaces are independant for virtual kernels,
903 * this function only applies to the kernel pmap.
906 pmap_track_modified(pmap_t pmap, vm_offset_t va)
908 if (pmap != &kernel_pmap)
910 if ((va < clean_sva) || (va >= clean_eva))
916 /************************************************************************
917 * Procedures supporting managed page table pages *
918 ************************************************************************
920 * These procedures are used to track managed page table pages. These pages
921 * use the page table page's vm_page_t to track PTEs in the page. The
922 * page table pages themselves are arranged in a VM object, pmap->pm_pteobj.
924 * This allows the system to throw away page table pages for user processes
925 * at will and reinstantiate them on demand.
929 * This routine works like vm_page_lookup() but also blocks as long as the
930 * page is busy. This routine does not busy the page it returns.
932 * Unless the caller is managing objects whos pages are in a known state,
933 * the call should be made with a critical section held so the page's object
934 * association remains valid on return.
937 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
941 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
942 m = vm_page_lookup_busy_wait(object, pindex, FALSE, "pplookp");
948 * This routine unholds page table pages, and if the hold count
949 * drops to zero, then it decrements the wire count.
951 * We must recheck that this is the last hold reference after busy-sleeping
955 _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
957 vm_page_busy_wait(m, FALSE, "pmuwpt");
958 KASSERT(m->queue == PQ_NONE,
959 ("_pmap_unwire_pte_hold: %p->queue != PQ_NONE", m));
961 if (m->hold_count == 1) {
963 * Unmap the page table page.
965 KKASSERT(pmap->pm_pdir[m->pindex] != 0);
966 pmap_inval_pde(&pmap->pm_pdir[m->pindex], pmap,
967 (vm_offset_t)m->pindex << SEG_SHIFT);
968 KKASSERT(pmap->pm_stats.resident_count > 0);
969 --pmap->pm_stats.resident_count;
971 if (pmap->pm_ptphint == m)
972 pmap->pm_ptphint = NULL;
975 * This was our last hold, the page had better be unwired
976 * after we decrement wire_count.
978 * FUTURE NOTE: shared page directory page could result in
979 * multiple wire counts.
983 KKASSERT(m->wire_count == 0);
984 atomic_add_int(&vmstats.v_wire_count, -1);
985 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
987 vm_page_free_zero(m);
990 KKASSERT(m->hold_count > 1);
998 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
1000 KKASSERT(m->hold_count > 0);
1001 if (m->hold_count > 1) {
1005 return _pmap_unwire_pte_hold(pmap, m);
1010 * After removing a page table entry, this routine is used to
1011 * conditionally free the page, and manage the hold/wire counts.
1014 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte)
1018 ASSERT_LWKT_TOKEN_HELD(vm_object_token(pmap->pm_pteobj));
1022 * page table pages in the kernel_pmap are not managed.
1024 if (pmap == &kernel_pmap)
1026 ptepindex = (va >> PDRSHIFT);
1027 if (pmap->pm_ptphint &&
1028 (pmap->pm_ptphint->pindex == ptepindex)) {
1029 mpte = pmap->pm_ptphint;
1031 mpte = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1032 pmap->pm_ptphint = mpte;
1033 vm_page_wakeup(mpte);
1036 return pmap_unwire_pte_hold(pmap, mpte);
1040 * Attempt to release and free the vm_page backing a page directory page
1041 * in a pmap. Returns 1 on success, 0 on failure (if the procedure had
1045 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1047 vpte_t *pde = pmap->pm_pdir;
1050 * This code optimizes the case of freeing non-busy
1051 * page-table pages. Those pages are zero now, and
1052 * might as well be placed directly into the zero queue.
1054 if (vm_page_busy_try(p, FALSE)) {
1055 vm_page_sleep_busy(p, FALSE, "pmaprl");
1058 KKASSERT(pmap->pm_stats.resident_count > 0);
1059 --pmap->pm_stats.resident_count;
1061 if (p->hold_count) {
1062 panic("pmap_release: freeing held page table page");
1065 * Page directory pages need to have the kernel stuff cleared, so
1066 * they can go into the zero queue also.
1068 * In virtual kernels there is no 'kernel stuff'. For the moment
1069 * I just make sure the whole thing has been zero'd even though
1070 * it should already be completely zero'd.
1072 * pmaps for vkernels do not self-map because they do not share
1073 * their address space with the vkernel. Clearing of pde[] thus
1074 * only applies to page table pages and not to the page directory
1077 if (p->pindex == pmap->pm_pdindex) {
1078 bzero(pde, VPTE_PAGETABLE_SIZE);
1079 pmap_kremove((vm_offset_t)pmap->pm_pdir);
1081 KKASSERT(pde[p->pindex] != 0);
1082 pmap_inval_pde(&pde[p->pindex], pmap,
1083 (vm_offset_t)p->pindex << SEG_SHIFT);
1087 * Clear the matching hint
1089 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1090 pmap->pm_ptphint = NULL;
1093 * And throw the page away. The page is completely zero'd out so
1094 * optimize the free call.
1097 atomic_add_int(&vmstats.v_wire_count, -1);
1098 vm_page_free_zero(p);
1103 * This routine is called if the page table page is not mapped in the page
1106 * The routine is broken up into two parts for readability.
1108 * It must return a held mpte and map the page directory page as required.
1109 * Because vm_page_grab() can block, we must re-check pm_pdir[ptepindex]
1112 _pmap_allocpte(pmap_t pmap, unsigned ptepindex)
1118 * Find or fabricate a new pagetable page. A busied page will be
1119 * returned. This call may block.
1121 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1122 VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1123 vm_page_flag_set(m, PG_MAPPED);
1125 KASSERT(m->queue == PQ_NONE,
1126 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1129 * Increment the hold count for the page we will be returning to
1135 * It is possible that someone else got in and mapped by the page
1136 * directory page while we were blocked, if so just unbusy and
1137 * return the held page.
1139 if ((ptepa = pmap->pm_pdir[ptepindex]) != 0) {
1140 KKASSERT((ptepa & VPTE_FRAME) == VM_PAGE_TO_PHYS(m));
1147 * Map the pagetable page into the process address space, if
1148 * it isn't already there.
1150 ++pmap->pm_stats.resident_count;
1152 ptepa = VM_PAGE_TO_PHYS(m);
1153 pmap->pm_pdir[ptepindex] = (vpte_t)ptepa | VPTE_R | VPTE_W | VPTE_V |
1157 * We are likely about to access this page table page, so set the
1158 * page table hint to reduce overhead.
1160 pmap->pm_ptphint = m;
1168 * Determine the page table page required to access the VA in the pmap
1169 * and allocate it if necessary. Return a held vm_page_t for the page.
1171 * Only used with user pmaps.
1174 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1180 ASSERT_LWKT_TOKEN_HELD(vm_object_token(pmap->pm_pteobj));
1183 * Calculate pagetable page index
1185 ptepindex = va >> PDRSHIFT;
1188 * Get the page directory entry
1190 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1193 * This supports switching from a 4MB page to a
1196 if (ptepa & VPTE_PS) {
1197 KKASSERT(pmap->pm_pdir[ptepindex] != 0);
1198 pmap_inval_pde(&pmap->pm_pdir[ptepindex], pmap,
1199 (vm_offset_t)ptepindex << SEG_SHIFT);
1204 * If the page table page is mapped, we just increment the
1205 * hold count, and activate it.
1209 * In order to get the page table page, try the
1212 if (pmap->pm_ptphint &&
1213 (pmap->pm_ptphint->pindex == ptepindex)) {
1214 m = pmap->pm_ptphint;
1216 m = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1217 pmap->pm_ptphint = m;
1224 * Here if the pte page isn't mapped, or if it has been deallocated.
1226 return _pmap_allocpte(pmap, ptepindex);
1229 /************************************************************************
1230 * Managed pages in pmaps *
1231 ************************************************************************
1233 * All pages entered into user pmaps and some pages entered into the kernel
1234 * pmap are managed, meaning that pmap_protect() and other related management
1235 * functions work on these pages.
1239 * free the pv_entry back to the free list. This function may be
1240 * called from an interrupt.
1242 static __inline void
1243 free_pv_entry(pv_entry_t pv)
1250 * get a new pv_entry, allocating a block from the system
1251 * when needed. This function may be called from an interrupt.
1257 if (pv_entry_high_water &&
1258 (pv_entry_count > pv_entry_high_water) &&
1259 (pmap_pagedaemon_waken == 0)) {
1260 pmap_pagedaemon_waken = 1;
1261 wakeup (&vm_pages_needed);
1263 return zalloc(&pvzone);
1267 * This routine is very drastic, but can save the system
1277 static int warningdone=0;
1279 if (pmap_pagedaemon_waken == 0)
1281 lwkt_gettoken(&vm_token);
1282 pmap_pagedaemon_waken = 0;
1284 if (warningdone < 5) {
1285 kprintf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1289 for (i = 0; i < vm_page_array_size; i++) {
1290 m = &vm_page_array[i];
1291 if (m->wire_count || m->hold_count)
1293 if (vm_page_busy_try(m, TRUE) == 0) {
1294 if (m->wire_count == 0 && m->hold_count == 0) {
1300 lwkt_reltoken(&vm_token);
1304 * If it is the first entry on the list, it is actually
1305 * in the header and we must copy the following entry up
1306 * to the header. Otherwise we must search the list for
1307 * the entry. In either case we free the now unused entry.
1309 * caller must hold vm_token
1312 pmap_remove_entry(struct pmap *pmap, vm_page_t m, vm_offset_t va)
1318 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1319 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1320 if (pmap == pv->pv_pmap && va == pv->pv_va)
1324 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1325 if (va == pv->pv_va)
1331 * Note that pv_ptem is NULL if the page table page itself is not
1332 * managed, even if the page being removed IS managed.
1336 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1337 m->md.pv_list_count--;
1338 atomic_add_int(&m->object->agg_pv_list_count, -1);
1339 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1340 if (TAILQ_EMPTY(&m->md.pv_list))
1341 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1342 ++pmap->pm_generation;
1343 vm_object_hold(pmap->pm_pteobj);
1344 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem);
1345 vm_object_drop(pmap->pm_pteobj);
1353 * Create a pv entry for page at pa for (pmap, va). If the page table page
1354 * holding the VA is managed, mpte will be non-NULL.
1357 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
1362 pv = get_pv_entry();
1367 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1368 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1369 ++pmap->pm_generation;
1370 m->md.pv_list_count++;
1371 atomic_add_int(&m->object->agg_pv_list_count, 1);
1377 * pmap_remove_pte: do the things to unmap a page in a process
1380 pmap_remove_pte(struct pmap *pmap, vpte_t *ptq, vm_offset_t va)
1385 oldpte = pmap_inval_loadandclear(ptq, pmap, va);
1386 if (oldpte & VPTE_WIRED)
1387 --pmap->pm_stats.wired_count;
1388 KKASSERT(pmap->pm_stats.wired_count >= 0);
1392 * Machines that don't support invlpg, also don't support
1393 * VPTE_G. XXX VPTE_G is disabled for SMP so don't worry about
1396 if (oldpte & VPTE_G)
1397 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
1399 KKASSERT(pmap->pm_stats.resident_count > 0);
1400 --pmap->pm_stats.resident_count;
1401 if (oldpte & VPTE_MANAGED) {
1402 m = PHYS_TO_VM_PAGE(oldpte);
1403 if (oldpte & VPTE_M) {
1404 #if defined(PMAP_DIAGNOSTIC)
1405 if (pmap_nw_modified((pt_entry_t) oldpte)) {
1407 "pmap_remove: modified page not writable: va: 0x%x, pte: 0x%x\n",
1411 if (pmap_track_modified(pmap, va))
1414 if (oldpte & VPTE_A)
1415 vm_page_flag_set(m, PG_REFERENCED);
1416 return pmap_remove_entry(pmap, m, va);
1418 return pmap_unuse_pt(pmap, va, NULL);
1427 * Remove a single page from a process address space.
1429 * This function may not be called from an interrupt if the pmap is
1433 pmap_remove_page(struct pmap *pmap, vm_offset_t va)
1438 * if there is no pte for this address, just skip it!!! Otherwise
1439 * get a local va for mappings for this pmap and remove the entry.
1441 if (*pmap_pde(pmap, va) != 0) {
1442 ptq = get_ptbase(pmap, va);
1444 pmap_remove_pte(pmap, ptq, va);
1450 * Remove the given range of addresses from the specified map.
1452 * It is assumed that the start and end are properly rounded to the
1455 * This function may not be called from an interrupt if the pmap is
1461 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
1465 vm_offset_t ptpaddr;
1466 vm_pindex_t sindex, eindex;
1471 vm_object_hold(pmap->pm_pteobj);
1472 lwkt_gettoken(&vm_token);
1473 KKASSERT(pmap->pm_stats.resident_count >= 0);
1474 if (pmap->pm_stats.resident_count == 0) {
1475 lwkt_reltoken(&vm_token);
1476 vm_object_drop(pmap->pm_pteobj);
1481 * special handling of removing one page. a very
1482 * common operation and easy to short circuit some
1485 if (((sva + PAGE_SIZE) == eva) &&
1486 ((pmap->pm_pdir[(sva >> PDRSHIFT)] & VPTE_PS) == 0)) {
1487 pmap_remove_page(pmap, sva);
1488 lwkt_reltoken(&vm_token);
1489 vm_object_drop(pmap->pm_pteobj);
1494 * Get a local virtual address for the mappings that are being
1497 * XXX this is really messy because the kernel pmap is not relative
1500 sindex = (sva >> PAGE_SHIFT);
1501 eindex = (eva >> PAGE_SHIFT);
1503 for (; sindex < eindex; sindex = pdnxt) {
1507 * Calculate index for next page table.
1509 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1510 if (pmap->pm_stats.resident_count == 0)
1513 pdirindex = sindex / NPDEPG;
1514 if (((ptpaddr = pmap->pm_pdir[pdirindex]) & VPTE_PS) != 0) {
1515 KKASSERT(pmap->pm_pdir[pdirindex] != 0);
1516 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1517 pmap_inval_pde(&pmap->pm_pdir[pdirindex], pmap,
1518 (vm_offset_t)pdirindex << SEG_SHIFT);
1523 * Weed out invalid mappings. Note: we assume that the page
1524 * directory table is always allocated, and in kernel virtual.
1530 * Limit our scan to either the end of the va represented
1531 * by the current page table page, or to the end of the
1532 * range being removed.
1538 * NOTE: pmap_remove_pte() can block.
1540 for (; sindex != pdnxt; sindex++) {
1543 ptbase = get_ptbase(pmap, sindex << PAGE_SHIFT);
1546 va = i386_ptob(sindex);
1547 if (pmap_remove_pte(pmap, ptbase, va))
1551 lwkt_reltoken(&vm_token);
1552 vm_object_drop(pmap->pm_pteobj);
1556 * Removes this physical page from all physical maps in which it resides.
1557 * Reflects back modify bits to the pager.
1559 * This routine may not be called from an interrupt.
1564 pmap_remove_all(vm_page_t m)
1569 #if defined(PMAP_DIAGNOSTIC)
1571 * XXX this makes pmap_page_protect(NONE) illegal for non-managed
1574 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
1575 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%08llx", (long long)VM_PAGE_TO_PHYS(m));
1579 lwkt_gettoken(&vm_token);
1580 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
1581 KKASSERT(pv->pv_pmap->pm_stats.resident_count > 0);
1582 --pv->pv_pmap->pm_stats.resident_count;
1584 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
1585 KKASSERT(pte != NULL);
1587 tpte = pmap_inval_loadandclear(pte, pv->pv_pmap, pv->pv_va);
1588 if (tpte & VPTE_WIRED)
1589 --pv->pv_pmap->pm_stats.wired_count;
1590 KKASSERT(pv->pv_pmap->pm_stats.wired_count >= 0);
1593 vm_page_flag_set(m, PG_REFERENCED);
1596 * Update the vm_page_t clean and reference bits.
1598 if (tpte & VPTE_M) {
1599 #if defined(PMAP_DIAGNOSTIC)
1600 if (pmap_nw_modified((pt_entry_t) tpte)) {
1602 "pmap_remove_all: modified page not writable: va: 0x%x, pte: 0x%x\n",
1606 if (pmap_track_modified(pv->pv_pmap, pv->pv_va))
1609 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1610 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
1611 ++pv->pv_pmap->pm_generation;
1612 m->md.pv_list_count--;
1613 atomic_add_int(&m->object->agg_pv_list_count, -1);
1614 if (TAILQ_EMPTY(&m->md.pv_list))
1615 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1616 vm_object_hold(pv->pv_pmap->pm_pteobj);
1617 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
1618 vm_object_drop(pv->pv_pmap->pm_pteobj);
1621 KKASSERT((m->flags & (PG_MAPPED | PG_WRITEABLE)) == 0);
1622 lwkt_reltoken(&vm_token);
1626 * Set the physical protection on the specified range of this map
1629 * This function may not be called from an interrupt if the map is
1630 * not the kernel_pmap.
1635 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
1639 vm_offset_t pdnxt, ptpaddr;
1640 vm_pindex_t sindex, eindex;
1646 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1647 pmap_remove(pmap, sva, eva);
1651 if (prot & VM_PROT_WRITE)
1654 lwkt_gettoken(&vm_token);
1655 ptbase = get_ptbase(pmap, sva);
1657 sindex = (sva >> PAGE_SHIFT);
1658 eindex = (eva >> PAGE_SHIFT);
1661 for (; sindex < eindex; sindex = pdnxt) {
1665 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1667 pdirindex = sindex / NPDEPG;
1670 * Clear the modified and writable bits for a 4m page.
1671 * Throw away the modified bit (?)
1673 if (((ptpaddr = pmap->pm_pdir[pdirindex]) & VPTE_PS) != 0) {
1674 pmap_clean_pde(&pmap->pm_pdir[pdirindex], pmap,
1675 (vm_offset_t)pdirindex << SEG_SHIFT);
1676 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1681 * Weed out invalid mappings. Note: we assume that the page
1682 * directory table is always allocated, and in kernel virtual.
1687 if (pdnxt > eindex) {
1691 for (; sindex != pdnxt; sindex++) {
1696 * Clean managed pages and also check the accessed
1697 * bit. Just remove write perms for unmanaged
1698 * pages. Be careful of races, turning off write
1699 * access will force a fault rather then setting
1700 * the modified bit at an unexpected time.
1702 ptep = &ptbase[sindex - sbase];
1703 if (*ptep & VPTE_MANAGED) {
1704 pbits = pmap_clean_pte(ptep, pmap,
1707 if (pbits & VPTE_A) {
1708 m = PHYS_TO_VM_PAGE(pbits);
1709 vm_page_flag_set(m, PG_REFERENCED);
1710 atomic_clear_long(ptep, VPTE_A);
1712 if (pbits & VPTE_M) {
1713 if (pmap_track_modified(pmap, i386_ptob(sindex))) {
1715 m = PHYS_TO_VM_PAGE(pbits);
1720 pbits = pmap_setro_pte(ptep, pmap,
1725 lwkt_reltoken(&vm_token);
1729 * Enter a managed page into a pmap. If the page is not wired related pmap
1730 * data can be destroyed at any time for later demand-operation.
1732 * Insert the vm_page (m) at virtual address (v) in (pmap), with the
1733 * specified protection, and wire the mapping if requested.
1735 * NOTE: This routine may not lazy-evaluate or lose information. The
1736 * page must actually be inserted into the given map NOW.
1738 * NOTE: When entering a page at a KVA address, the pmap must be the
1744 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1745 boolean_t wired, vm_map_entry_t entry __unused)
1750 vpte_t origpte, newpte;
1758 vm_object_hold(pmap->pm_pteobj);
1759 lwkt_gettoken(&vm_token);
1762 * Get the page table page. The kernel_pmap's page table pages
1763 * are preallocated and have no associated vm_page_t.
1765 if (pmap == &kernel_pmap)
1768 mpte = pmap_allocpte(pmap, va);
1770 pte = pmap_pte(pmap, va);
1773 * Page Directory table entry not valid, we need a new PT page
1774 * and pmap_allocpte() didn't give us one. Oops!
1777 panic("pmap_enter: invalid page directory pmap=%p, va=0x%p",
1782 * Deal with races on the original mapping (though don't worry
1783 * about VPTE_A races) by cleaning it. This will force a fault
1784 * if an attempt is made to write to the page.
1786 pa = VM_PAGE_TO_PHYS(m) & VPTE_FRAME;
1787 origpte = pmap_clean_pte(pte, pmap, va);
1788 opa = origpte & VPTE_FRAME;
1790 if (origpte & VPTE_PS)
1791 panic("pmap_enter: attempted pmap_enter on 4MB page");
1794 * Mapping has not changed, must be protection or wiring change.
1796 if (origpte && (opa == pa)) {
1798 * Wiring change, just update stats. We don't worry about
1799 * wiring PT pages as they remain resident as long as there
1800 * are valid mappings in them. Hence, if a user page is wired,
1801 * the PT page will be also.
1803 if (wired && ((origpte & VPTE_WIRED) == 0))
1804 ++pmap->pm_stats.wired_count;
1805 else if (!wired && (origpte & VPTE_WIRED))
1806 --pmap->pm_stats.wired_count;
1807 KKASSERT(pmap->pm_stats.wired_count >= 0);
1810 * Remove the extra pte reference. Note that we cannot
1811 * optimize the RO->RW case because we have adjusted the
1812 * wiring count above and may need to adjust the wiring
1819 * We might be turning off write access to the page,
1820 * so we go ahead and sense modify status.
1822 if (origpte & VPTE_MANAGED) {
1823 if ((origpte & VPTE_M) &&
1824 pmap_track_modified(pmap, va)) {
1826 om = PHYS_TO_VM_PAGE(opa);
1830 KKASSERT(m->flags & PG_MAPPED);
1835 * Mapping has changed, invalidate old range and fall through to
1836 * handle validating new mapping.
1840 err = pmap_remove_pte(pmap, pte, va);
1842 panic("pmap_enter: pte vanished, va: %p", (void *)va);
1843 pte = pmap_pte(pmap, va);
1844 origpte = pmap_clean_pte(pte, pmap, va);
1845 opa = origpte & VPTE_FRAME;
1847 kprintf("pmap_enter: Warning, raced pmap %p va %p\n",
1853 * Enter on the PV list if part of our managed memory. Note that we
1854 * raise IPL while manipulating pv_table since pmap_enter can be
1855 * called at interrupt time.
1857 if (pmap_initialized &&
1858 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
1859 pmap_insert_entry(pmap, va, mpte, m);
1861 vm_page_flag_set(m, PG_MAPPED);
1865 * Increment counters
1867 ++pmap->pm_stats.resident_count;
1869 pmap->pm_stats.wired_count++;
1873 * Now validate mapping with desired protection/wiring.
1875 newpte = (vm_offset_t) (pa | pte_prot(pmap, prot) | VPTE_V);
1878 newpte |= VPTE_WIRED;
1879 if (pmap != &kernel_pmap)
1883 * If the mapping or permission bits are different from the
1884 * (now cleaned) original pte, an update is needed. We've
1885 * already downgraded or invalidated the page so all we have
1886 * to do now is update the bits.
1888 * XXX should we synchronize RO->RW changes to avoid another
1891 if ((origpte & ~(VPTE_W|VPTE_M|VPTE_A)) != newpte) {
1892 *pte = newpte | VPTE_A;
1893 if (newpte & VPTE_W)
1894 vm_page_flag_set(m, PG_WRITEABLE);
1896 KKASSERT((newpte & VPTE_MANAGED) == 0 || m->flags & PG_MAPPED);
1897 lwkt_reltoken(&vm_token);
1898 vm_object_drop(pmap->pm_pteobj);
1902 * This code works like pmap_enter() but assumes VM_PROT_READ and not-wired.
1904 * Currently this routine may only be used on user pmaps, not kernel_pmap.
1907 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m)
1915 KKASSERT(pmap != &kernel_pmap);
1917 KKASSERT(va >= VM_MIN_USER_ADDRESS && va < VM_MAX_USER_ADDRESS);
1920 * Calculate pagetable page (mpte), allocating it if necessary.
1922 * A held page table page (mpte), or NULL, is passed onto the
1923 * section following.
1925 ptepindex = va >> PDRSHIFT;
1927 vm_object_hold(pmap->pm_pteobj);
1928 lwkt_gettoken(&vm_token);
1932 * Get the page directory entry
1934 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1937 * If the page table page is mapped, we just increment
1938 * the hold count, and activate it.
1941 if (ptepa & VPTE_PS)
1942 panic("pmap_enter_quick: unexpected mapping into 4MB page");
1943 if (pmap->pm_ptphint &&
1944 (pmap->pm_ptphint->pindex == ptepindex)) {
1945 mpte = pmap->pm_ptphint;
1947 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1948 pmap->pm_ptphint = mpte;
1949 vm_page_wakeup(mpte);
1954 mpte = _pmap_allocpte(pmap, ptepindex);
1956 } while (mpte == NULL);
1959 * Ok, now that the page table page has been validated, get the pte.
1960 * If the pte is already mapped undo mpte's hold_count and
1963 pte = pmap_pte(pmap, va);
1965 pmap_unwire_pte_hold(pmap, mpte);
1966 lwkt_reltoken(&vm_token);
1967 vm_object_drop(pmap->pm_pteobj);
1972 * Enter on the PV list if part of our managed memory. Note that we
1973 * raise IPL while manipulating pv_table since pmap_enter can be
1974 * called at interrupt time.
1976 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
1977 pmap_insert_entry(pmap, va, mpte, m);
1978 vm_page_flag_set(m, PG_MAPPED);
1982 * Increment counters
1984 ++pmap->pm_stats.resident_count;
1986 pa = VM_PAGE_TO_PHYS(m);
1989 * Now validate mapping with RO protection
1991 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
1992 *pte = (vpte_t)pa | VPTE_V | VPTE_U;
1994 *pte = (vpte_t)pa | VPTE_V | VPTE_U | VPTE_MANAGED;
1995 /*pmap_inval_add(&info, pmap, va); shouldn't be needed 0->valid */
1996 /*pmap_inval_flush(&info); don't need for vkernel */
1997 lwkt_reltoken(&vm_token);
1998 vm_object_drop(pmap->pm_pteobj);
2002 * Extract the physical address for the translation at the specified
2003 * virtual address in the pmap.
2005 * The caller must hold vm_token if non-blocking operation is desired.
2009 pmap_extract(pmap_t pmap, vm_offset_t va)
2014 lwkt_gettoken(&vm_token);
2015 if (pmap && (pte = pmap->pm_pdir[va >> SEG_SHIFT]) != 0) {
2016 if (pte & VPTE_PS) {
2017 rtval = pte & ~((vpte_t)(1 << SEG_SHIFT) - 1);
2018 rtval |= va & SEG_MASK;
2020 pte = *get_ptbase(pmap, va);
2021 rtval = (pte & VPTE_FRAME) | (va & PAGE_MASK);
2026 lwkt_reltoken(&vm_token);
2030 #define MAX_INIT_PT (96)
2033 * This routine preloads the ptes for a given object into the specified pmap.
2034 * This eliminates the blast of soft faults on process startup and
2035 * immediately after an mmap.
2039 static int pmap_object_init_pt_callback(vm_page_t p, void *data);
2042 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2043 vm_object_t object, vm_pindex_t pindex,
2044 vm_size_t size, int limit)
2046 struct rb_vm_page_scan_info info;
2051 * We can't preinit if read access isn't set or there is no pmap
2054 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2058 * We can't preinit if the pmap is not the current pmap
2060 lp = curthread->td_lwp;
2061 if (lp == NULL || pmap != vmspace_pmap(lp->lwp_vmspace))
2064 psize = size >> PAGE_SHIFT;
2066 if ((object->type != OBJT_VNODE) ||
2067 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2068 (object->resident_page_count > MAX_INIT_PT))) {
2072 if (psize + pindex > object->size) {
2073 if (object->size < pindex)
2075 psize = object->size - pindex;
2082 * Use a red-black scan to traverse the requested range and load
2083 * any valid pages found into the pmap.
2085 * We cannot safely scan the object's memq unless we are in a
2086 * critical section since interrupts can remove pages from objects.
2088 info.start_pindex = pindex;
2089 info.end_pindex = pindex + psize - 1;
2095 vm_object_hold_shared(object);
2096 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2097 pmap_object_init_pt_callback, &info);
2098 vm_object_drop(object);
2102 * The caller must hold vm_token.
2106 pmap_object_init_pt_callback(vm_page_t p, void *data)
2108 struct rb_vm_page_scan_info *info = data;
2109 vm_pindex_t rel_index;
2112 * don't allow an madvise to blow away our really
2113 * free pages allocating pv entries.
2115 if ((info->limit & MAP_PREFAULT_MADVISE) &&
2116 vmstats.v_free_count < vmstats.v_free_reserved) {
2121 * Ignore list markers and ignore pages we cannot instantly
2122 * busy (while holding the object token).
2124 if (p->flags & PG_MARKER)
2126 if (vm_page_busy_try(p, TRUE))
2128 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2129 (p->flags & PG_FICTITIOUS) == 0) {
2130 if ((p->queue - p->pc) == PQ_CACHE)
2131 vm_page_deactivate(p);
2132 rel_index = p->pindex - info->start_pindex;
2133 pmap_enter_quick(info->pmap,
2134 info->addr + i386_ptob(rel_index), p);
2141 * Return TRUE if the pmap is in shape to trivially
2142 * pre-fault the specified address.
2144 * Returns FALSE if it would be non-trivial or if a
2145 * pte is already loaded into the slot.
2150 pmap_prefault_ok(pmap_t pmap, vm_offset_t addr)
2155 lwkt_gettoken(&vm_token);
2156 if ((*pmap_pde(pmap, addr)) == 0) {
2159 pte = get_ptbase(pmap, addr);
2160 ret = (*pte) ? 0 : 1;
2162 lwkt_reltoken(&vm_token);
2167 * Change the wiring attribute for a map/virtual-address pair.
2168 * The mapping must already exist in the pmap.
2170 * No other requirements.
2173 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired,
2174 vm_map_entry_t entry __unused)
2181 lwkt_gettoken(&vm_token);
2182 pte = get_ptbase(pmap, va);
2184 if (wired && (*pte & VPTE_WIRED) == 0)
2185 ++pmap->pm_stats.wired_count;
2186 else if (!wired && (*pte & VPTE_WIRED))
2187 --pmap->pm_stats.wired_count;
2188 KKASSERT(pmap->pm_stats.wired_count >= 0);
2191 * Wiring is not a hardware characteristic so there is no need to
2192 * invalidate TLB. However, in an SMP environment we must use
2193 * a locked bus cycle to update the pte (if we are not using
2194 * the pmap_inval_*() API that is)... it's ok to do this for simple
2198 atomic_set_long(pte, VPTE_WIRED);
2200 atomic_clear_long(pte, VPTE_WIRED);
2201 lwkt_reltoken(&vm_token);
2205 * Copy the range specified by src_addr/len
2206 * from the source map to the range dst_addr/len
2207 * in the destination map.
2209 * This routine is only advisory and need not do anything.
2212 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2213 vm_size_t len, vm_offset_t src_addr)
2216 vm_offset_t end_addr = src_addr + len;
2223 * XXX BUGGY. Amoung other things srcmpte is assumed to remain
2224 * valid through blocking calls, and that's just not going to
2231 if (dst_addr != src_addr)
2233 if (dst_pmap->pm_pdir == NULL)
2235 if (src_pmap->pm_pdir == NULL)
2238 lwkt_gettoken(&vm_token);
2240 src_frame = get_ptbase1(src_pmap, src_addr);
2241 dst_frame = get_ptbase2(dst_pmap, src_addr);
2244 * critical section protection is required to maintain the page/object
2245 * association, interrupts can free pages and remove them from
2248 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
2249 vpte_t *src_pte, *dst_pte;
2250 vm_page_t dstmpte, srcmpte;
2251 vm_offset_t srcptepaddr;
2254 if (addr >= VM_MAX_USER_ADDRESS)
2255 panic("pmap_copy: invalid to pmap_copy page tables");
2258 * Don't let optional prefaulting of pages make us go
2259 * way below the low water mark of free pages or way
2260 * above high water mark of used pv entries.
2262 if (vmstats.v_free_count < vmstats.v_free_reserved ||
2263 pv_entry_count > pv_entry_high_water)
2266 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
2267 ptepindex = addr >> PDRSHIFT;
2269 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex];
2270 if (srcptepaddr == 0)
2273 if (srcptepaddr & VPTE_PS) {
2274 if (dst_pmap->pm_pdir[ptepindex] == 0) {
2275 dst_pmap->pm_pdir[ptepindex] = (vpte_t)srcptepaddr;
2276 dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
2281 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
2282 if ((srcmpte == NULL) || (srcmpte->hold_count == 0) ||
2283 (srcmpte->flags & PG_BUSY)) {
2287 if (pdnxt > end_addr)
2290 src_pte = src_frame + ((addr - src_addr) >> PAGE_SHIFT);
2291 dst_pte = dst_frame + ((addr - src_addr) >> PAGE_SHIFT);
2292 while (addr < pdnxt) {
2297 * we only virtual copy managed pages
2299 if ((ptetemp & VPTE_MANAGED) != 0) {
2301 * We have to check after allocpte for the
2302 * pte still being around... allocpte can
2305 * pmap_allocpte can block, unfortunately
2306 * we have to reload the tables.
2308 dstmpte = pmap_allocpte(dst_pmap, addr);
2309 src_frame = get_ptbase1(src_pmap, src_addr);
2310 dst_frame = get_ptbase2(dst_pmap, src_addr);
2312 if ((*dst_pte == 0) && (ptetemp = *src_pte) &&
2313 (ptetemp & VPTE_MANAGED) != 0) {
2315 * Clear the modified and accessed
2316 * (referenced) bits during the copy.
2318 * We do not have to clear the write
2319 * bit to force a fault-on-modify
2320 * because the real kernel's target
2321 * pmap is empty and will fault anyway.
2323 m = PHYS_TO_VM_PAGE(ptetemp);
2324 *dst_pte = ptetemp & ~(VPTE_M | VPTE_A);
2325 ++dst_pmap->pm_stats.resident_count;
2326 pmap_insert_entry(dst_pmap, addr,
2328 KKASSERT(m->flags & PG_MAPPED);
2330 pmap_unwire_pte_hold(dst_pmap, dstmpte);
2332 if (dstmpte->hold_count >= srcmpte->hold_count)
2340 lwkt_reltoken(&vm_token);
2346 * Zero the specified PA by mapping the page into KVM and clearing its
2349 * This function may be called from an interrupt and no locking is
2353 pmap_zero_page(vm_paddr_t phys)
2355 struct mdglobaldata *gd = mdcpu;
2359 panic("pmap_zero_page: CMAP3 busy");
2360 *gd->gd_CMAP3 = VPTE_V | VPTE_R | VPTE_W | (phys & VPTE_FRAME) | VPTE_A | VPTE_M;
2361 madvise(gd->gd_CADDR3, PAGE_SIZE, MADV_INVAL);
2363 bzero(gd->gd_CADDR3, PAGE_SIZE);
2369 * pmap_page_assertzero:
2371 * Assert that a page is empty, panic if it isn't.
2374 pmap_page_assertzero(vm_paddr_t phys)
2376 struct mdglobaldata *gd = mdcpu;
2381 panic("pmap_zero_page: CMAP3 busy");
2382 *gd->gd_CMAP3 = VPTE_V | VPTE_R | VPTE_W |
2383 (phys & VPTE_FRAME) | VPTE_A | VPTE_M;
2384 madvise(gd->gd_CADDR3, PAGE_SIZE, MADV_INVAL);
2385 for (i = 0; i < PAGE_SIZE; i += 4) {
2386 if (*(int *)((char *)gd->gd_CADDR3 + i) != 0) {
2387 panic("pmap_page_assertzero() @ %p not zero!",
2388 (void *)gd->gd_CADDR3);
2398 * Zero part of a physical page by mapping it into memory and clearing
2399 * its contents with bzero.
2401 * off and size may not cover an area beyond a single hardware page.
2404 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
2406 struct mdglobaldata *gd = mdcpu;
2410 panic("pmap_zero_page: CMAP3 busy");
2411 *gd->gd_CMAP3 = VPTE_V | VPTE_R | VPTE_W |
2412 (phys & VPTE_FRAME) | VPTE_A | VPTE_M;
2413 madvise(gd->gd_CADDR3, PAGE_SIZE, MADV_INVAL);
2415 bzero((char *)gd->gd_CADDR3 + off, size);
2423 * Copy the physical page from the source PA to the target PA.
2424 * This function may be called from an interrupt. No locking
2428 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
2430 struct mdglobaldata *gd = mdcpu;
2433 if (*(int *) gd->gd_CMAP1)
2434 panic("pmap_copy_page: CMAP1 busy");
2435 if (*(int *) gd->gd_CMAP2)
2436 panic("pmap_copy_page: CMAP2 busy");
2438 *(int *) gd->gd_CMAP1 = VPTE_V | VPTE_R | (src & PG_FRAME) | VPTE_A;
2439 *(int *) gd->gd_CMAP2 = VPTE_V | VPTE_R | VPTE_W | (dst & VPTE_FRAME) | VPTE_A | VPTE_M;
2441 madvise(gd->gd_CADDR1, PAGE_SIZE, MADV_INVAL);
2442 madvise(gd->gd_CADDR2, PAGE_SIZE, MADV_INVAL);
2444 bcopy(gd->gd_CADDR1, gd->gd_CADDR2, PAGE_SIZE);
2446 *(int *) gd->gd_CMAP1 = 0;
2447 *(int *) gd->gd_CMAP2 = 0;
2452 * pmap_copy_page_frag:
2454 * Copy the physical page from the source PA to the target PA.
2455 * This function may be called from an interrupt. No locking
2459 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
2461 struct mdglobaldata *gd = mdcpu;
2464 if (*(int *) gd->gd_CMAP1)
2465 panic("pmap_copy_page: CMAP1 busy");
2466 if (*(int *) gd->gd_CMAP2)
2467 panic("pmap_copy_page: CMAP2 busy");
2469 *(int *) gd->gd_CMAP1 = VPTE_V | (src & VPTE_FRAME) | VPTE_A;
2470 *(int *) gd->gd_CMAP2 = VPTE_V | VPTE_R | VPTE_W | (dst & VPTE_FRAME) | VPTE_A | VPTE_M;
2472 madvise(gd->gd_CADDR1, PAGE_SIZE, MADV_INVAL);
2473 madvise(gd->gd_CADDR2, PAGE_SIZE, MADV_INVAL);
2475 bcopy((char *)gd->gd_CADDR1 + (src & PAGE_MASK),
2476 (char *)gd->gd_CADDR2 + (dst & PAGE_MASK),
2479 *(int *) gd->gd_CMAP1 = 0;
2480 *(int *) gd->gd_CMAP2 = 0;
2485 * Returns true if the pmap's pv is one of the first
2486 * 16 pvs linked to from this page. This count may
2487 * be changed upwards or downwards in the future; it
2488 * is only necessary that true be returned for a small
2489 * subset of pmaps for proper page aging.
2494 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2499 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2503 lwkt_gettoken(&vm_token);
2505 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2506 if (pv->pv_pmap == pmap) {
2507 lwkt_reltoken(&vm_token);
2515 lwkt_reltoken(&vm_token);
2521 * Remove all pages from specified address space
2522 * this aids process exit speeds. Also, this code
2523 * is special cased for current process only, but
2524 * can have the more generic (and slightly slower)
2525 * mode enabled. This is much faster than pmap_remove
2526 * in the case of running down an entire address space.
2531 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2536 int32_t save_generation;
2538 if (pmap->pm_pteobj)
2539 vm_object_hold(pmap->pm_pteobj);
2540 lwkt_gettoken(&vm_token);
2541 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
2542 if (pv->pv_va >= eva || pv->pv_va < sva) {
2543 npv = TAILQ_NEXT(pv, pv_plist);
2547 KKASSERT(pmap == pv->pv_pmap);
2549 pte = pmap_pte(pmap, pv->pv_va);
2552 * We cannot remove wired pages from a process' mapping
2555 if (*pte & VPTE_WIRED) {
2556 npv = TAILQ_NEXT(pv, pv_plist);
2559 tpte = pmap_inval_loadandclear(pte, pmap, pv->pv_va);
2561 m = PHYS_TO_VM_PAGE(tpte);
2563 KASSERT(m < &vm_page_array[vm_page_array_size],
2564 ("pmap_remove_pages: bad tpte %lx", tpte));
2566 KKASSERT(pmap->pm_stats.resident_count > 0);
2567 --pmap->pm_stats.resident_count;
2570 * Update the vm_page_t clean and reference bits.
2572 if (tpte & VPTE_M) {
2576 npv = TAILQ_NEXT(pv, pv_plist);
2577 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
2578 save_generation = ++pmap->pm_generation;
2580 m->md.pv_list_count--;
2581 atomic_add_int(&m->object->agg_pv_list_count, -1);
2582 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2583 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2584 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2586 pmap_unuse_pt(pmap, pv->pv_va, pv->pv_ptem);
2590 * Restart the scan if we blocked during the unuse or free
2591 * calls and other removals were made.
2593 if (save_generation != pmap->pm_generation) {
2594 kprintf("Warning: pmap_remove_pages race-A avoided\n");
2595 npv = TAILQ_FIRST(&pmap->pm_pvlist);
2598 lwkt_reltoken(&vm_token);
2599 if (pmap->pm_pteobj)
2600 vm_object_drop(pmap->pm_pteobj);
2604 * pmap_testbit tests bits in active mappings of a VM page.
2606 * The caller must hold vm_token
2609 pmap_testbit(vm_page_t m, int bit)
2614 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2617 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2622 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2624 * if the bit being tested is the modified bit, then
2625 * mark clean_map and ptes as never
2628 if (bit & (VPTE_A|VPTE_M)) {
2629 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2633 #if defined(PMAP_DIAGNOSTIC)
2635 kprintf("Null pmap (tb) at va: 0x%x\n", pv->pv_va);
2639 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2650 * This routine is used to clear bits in ptes. Certain bits require special
2651 * handling, in particular (on virtual kernels) the VPTE_M (modify) bit.
2653 * This routine is only called with certain VPTE_* bit combinations.
2655 * The caller must hold vm_token
2657 static __inline void
2658 pmap_clearbit(vm_page_t m, int bit)
2664 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2670 * Loop over all current mappings setting/clearing as appropos If
2671 * setting RO do we need to clear the VAC?
2673 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2675 * don't write protect pager mappings
2677 if (bit == VPTE_W) {
2678 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2682 #if defined(PMAP_DIAGNOSTIC)
2684 kprintf("Null pmap (cb) at va: 0x%x\n", pv->pv_va);
2690 * Careful here. We can use a locked bus instruction to
2691 * clear VPTE_A or VPTE_M safely but we need to synchronize
2692 * with the target cpus when we mess with VPTE_W.
2694 * On virtual kernels we must force a new fault-on-write
2695 * in the real kernel if we clear the Modify bit ourselves,
2696 * otherwise the real kernel will not get a new fault and
2697 * will never set our Modify bit again.
2699 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2701 if (bit == VPTE_W) {
2703 * We must also clear VPTE_M when clearing
2706 pbits = pmap_clean_pte(pte, pv->pv_pmap,
2710 } else if (bit == VPTE_M) {
2712 * We do not have to make the page read-only
2713 * when clearing the Modify bit. The real
2714 * kernel will make the real PTE read-only
2715 * or otherwise detect the write and set
2716 * our VPTE_M again simply by us invalidating
2717 * the real kernel VA for the pmap (as we did
2718 * above). This allows the real kernel to
2719 * handle the write fault without forwarding
2722 atomic_clear_long(pte, VPTE_M);
2723 } else if ((bit & (VPTE_W|VPTE_M)) == (VPTE_W|VPTE_M)) {
2725 * We've been asked to clear W & M, I guess
2726 * the caller doesn't want us to update
2727 * the dirty status of the VM page.
2729 pmap_clean_pte(pte, pv->pv_pmap, pv->pv_va);
2732 * We've been asked to clear bits that do
2733 * not interact with hardware.
2735 atomic_clear_long(pte, bit);
2743 * Lower the permission for all mappings to a given page.
2748 pmap_page_protect(vm_page_t m, vm_prot_t prot)
2750 if ((prot & VM_PROT_WRITE) == 0) {
2751 lwkt_gettoken(&vm_token);
2752 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
2753 pmap_clearbit(m, VPTE_W);
2754 vm_page_flag_clear(m, PG_WRITEABLE);
2758 lwkt_reltoken(&vm_token);
2763 pmap_phys_address(vm_pindex_t ppn)
2765 return (i386_ptob(ppn));
2769 * Return a count of reference bits for a page, clearing those bits.
2770 * It is not necessary for every reference bit to be cleared, but it
2771 * is necessary that 0 only be returned when there are truly no
2772 * reference bits set.
2774 * XXX: The exact number of bits to check and clear is a matter that
2775 * should be tested and standardized at some point in the future for
2776 * optimal aging of shared pages.
2781 pmap_ts_referenced(vm_page_t m)
2783 pv_entry_t pv, pvf, pvn;
2787 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2791 lwkt_gettoken(&vm_token);
2793 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2798 pvn = TAILQ_NEXT(pv, pv_list);
2800 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2802 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2804 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2807 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2809 if (pte && (*pte & VPTE_A)) {
2810 atomic_clear_long(pte, VPTE_A);
2816 } while ((pv = pvn) != NULL && pv != pvf);
2818 lwkt_reltoken(&vm_token);
2825 * Return whether or not the specified physical page was modified
2826 * in any physical maps.
2831 pmap_is_modified(vm_page_t m)
2835 lwkt_gettoken(&vm_token);
2836 res = pmap_testbit(m, VPTE_M);
2837 lwkt_reltoken(&vm_token);
2842 * Clear the modify bits on the specified physical page.
2847 pmap_clear_modify(vm_page_t m)
2849 lwkt_gettoken(&vm_token);
2850 pmap_clearbit(m, VPTE_M);
2851 lwkt_reltoken(&vm_token);
2855 * Clear the reference bit on the specified physical page.
2860 pmap_clear_reference(vm_page_t m)
2862 lwkt_gettoken(&vm_token);
2863 pmap_clearbit(m, VPTE_A);
2864 lwkt_reltoken(&vm_token);
2868 * Miscellaneous support routines follow
2872 i386_protection_init(void)
2876 kp = protection_codes;
2877 for (prot = 0; prot < 8; prot++) {
2878 if (prot & VM_PROT_READ)
2880 if (prot & VM_PROT_WRITE)
2882 if (prot & VM_PROT_EXECUTE)
2891 * Map a set of physical memory pages into the kernel virtual
2892 * address space. Return a pointer to where it is mapped. This
2893 * routine is intended to be used for mapping device memory,
2896 * NOTE: we can't use pgeflag unless we invalidate the pages one at
2900 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
2902 vm_offset_t va, tmpva, offset;
2905 offset = pa & PAGE_MASK;
2906 size = roundup(offset + size, PAGE_SIZE);
2908 va = kmem_alloc_nofault(&kernel_map, size, PAGE_SIZE);
2910 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
2912 pa = pa & VPTE_FRAME;
2913 for (tmpva = va; size > 0;) {
2914 pte = KernelPTA + (tmpva >> PAGE_SHIFT);
2915 *pte = pa | VPTE_R | VPTE_W | VPTE_V; /* | pgeflag; */
2923 return ((void *)(va + offset));
2927 pmap_unmapdev(vm_offset_t va, vm_size_t size)
2929 vm_offset_t base, offset;
2931 base = va & VPTE_FRAME;
2932 offset = va & PAGE_MASK;
2933 size = roundup(offset + size, PAGE_SIZE);
2934 pmap_qremove(va, size >> PAGE_SHIFT);
2935 kmem_free(&kernel_map, base, size);
2941 * Change the PAT attribute on an existing kernel memory map. Caller
2942 * must ensure that the virtual memory in question is not accessed
2943 * during the adjustment.
2946 pmap_change_attr(vm_offset_t va, vm_size_t count, int mode)
2948 /* This is a vkernel, do nothing */
2952 * Perform the pmap work for mincore
2957 pmap_mincore(pmap_t pmap, vm_offset_t addr)
2963 lwkt_gettoken(&vm_token);
2965 ptep = pmap_pte(pmap, addr);
2967 lwkt_reltoken(&vm_token);
2971 if ((pte = *ptep) != 0) {
2974 val = MINCORE_INCORE;
2975 if ((pte & VPTE_MANAGED) == 0)
2978 pa = pte & VPTE_FRAME;
2980 m = PHYS_TO_VM_PAGE(pa);
2986 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
2988 * Modified by someone
2990 else if (m->dirty || pmap_is_modified(m))
2991 val |= MINCORE_MODIFIED_OTHER;
2996 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
2999 * Referenced by someone
3001 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3002 val |= MINCORE_REFERENCED_OTHER;
3003 vm_page_flag_set(m, PG_REFERENCED);
3007 lwkt_reltoken(&vm_token);
3012 * Caller must hold vmspace->vm_map.token for oldvm and newvm
3015 pmap_replacevm(struct proc *p, struct vmspace *newvm, int adjrefs)
3017 struct vmspace *oldvm;
3020 oldvm = p->p_vmspace;
3022 if (oldvm != newvm) {
3023 p->p_vmspace = newvm;
3024 KKASSERT(p->p_nthreads == 1);
3025 lp = RB_ROOT(&p->p_lwp_tree);
3026 pmap_setlwpvm(lp, newvm);
3028 sysref_get(&newvm->vm_sysref);
3029 sysref_put(&oldvm->vm_sysref);
3036 pmap_setlwpvm(struct lwp *lp, struct vmspace *newvm)
3038 struct vmspace *oldvm;
3042 oldvm = lp->lwp_vmspace;
3044 if (oldvm != newvm) {
3045 lp->lwp_vmspace = newvm;
3046 if (curthread->td_lwp == lp) {
3047 pmap = vmspace_pmap(newvm);
3048 atomic_set_cpumask(&pmap->pm_active, mycpu->gd_cpumask);
3049 #if defined(SWTCH_OPTIM_STATS)
3052 pmap = vmspace_pmap(oldvm);
3053 atomic_clear_cpumask(&pmap->pm_active, mycpu->gd_cpumask);
3061 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3064 if ((obj == NULL) || (size < NBPDR) ||
3065 ((obj->type != OBJT_DEVICE) && (obj->type != OBJT_MGTDEVICE))) {
3069 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3074 * Used by kmalloc/kfree, page already exists at va
3077 pmap_kvtom(vm_offset_t va)
3081 KKASSERT(va >= KvaStart && va < KvaEnd);
3082 ptep = KernelPTA + (va >> PAGE_SHIFT);
3083 return(PHYS_TO_VM_PAGE(*ptep & PG_FRAME));
3087 pmap_object_init(vm_object_t object)
3093 pmap_object_free(vm_object_t object)