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>
81 struct pmap kernel_pmap;
83 static struct vm_zone pvzone;
84 static struct vm_object pvzone_obj;
85 static TAILQ_HEAD(,pmap) pmap_list = TAILQ_HEAD_INITIALIZER(pmap_list);
86 static int pv_entry_count;
87 static int pv_entry_max;
88 static int pv_entry_high_water;
89 static int pmap_pagedaemon_waken;
90 static boolean_t pmap_initialized = FALSE;
91 static int protection_codes[8];
93 static void i386_protection_init(void);
94 static void pmap_remove_all(vm_page_t m);
95 static int pmap_release_free_page(struct pmap *pmap, vm_page_t p);
98 #ifndef PMAP_SHPGPERPROC
99 #define PMAP_SHPGPERPROC 200
102 #define pmap_pde(m, v) (&((m)->pm_pdir[(vm_offset_t)(v) >> PDRSHIFT]))
104 #define pte_prot(m, p) \
105 (protection_codes[p & (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)])
111 struct pv_entry *pvinit;
113 for (i = 0; i < vm_page_array_size; i++) {
116 m = &vm_page_array[i];
117 TAILQ_INIT(&m->md.pv_list);
118 m->md.pv_list_count = 0;
121 i = vm_page_array_size;
124 pvinit = (struct pv_entry *)kmem_alloc(&kernel_map, i*sizeof(*pvinit));
125 zbootinit(&pvzone, "PV ENTRY", sizeof(*pvinit), pvinit, i);
126 pmap_initialized = TRUE;
132 int shpgperproc = PMAP_SHPGPERPROC;
134 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
135 pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
136 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
137 pv_entry_high_water = 9 * (pv_entry_max / 10);
138 zinitna(&pvzone, &pvzone_obj, NULL, 0, pv_entry_max, ZONE_INTERRUPT, 1);
142 * Bootstrap the kernel_pmap so it can be used with pmap_enter().
144 * NOTE! pm_pdir for the kernel pmap is offset so VA's translate
145 * directly into PTD indexes (PTA is also offset for the same reason).
146 * This is necessary because, for now, KVA is not mapped at address 0.
148 * Page table pages are not managed like they are in normal pmaps, so
149 * no pteobj is needed.
154 vm_pindex_t i = (vm_offset_t)KernelPTD >> PAGE_SHIFT;
156 kernel_pmap.pm_pdir = KernelPTD - (KvaStart >> SEG_SHIFT);
157 kernel_pmap.pm_pdirpte = KernelPTA[i];
158 kernel_pmap.pm_count = 1;
159 kernel_pmap.pm_active = (cpumask_t)-1 & ~CPUMASK_LOCK;
160 TAILQ_INIT(&kernel_pmap.pm_pvlist);
161 i386_protection_init();
165 * Initialize pmap0/vmspace0 . Since process 0 never enters user mode we
166 * just dummy it up so it works well enough for fork().
168 * In DragonFly, process pmaps may only be used to manipulate user address
169 * space, never kernel address space.
172 pmap_pinit0(struct pmap *pmap)
177 /************************************************************************
178 * Procedures to manage whole physical maps *
179 ************************************************************************
181 * Initialize a preallocated and zeroed pmap structure,
182 * such as one in a vmspace structure.
185 pmap_pinit(struct pmap *pmap)
191 * No need to allocate page table space yet but we do need a valid
192 * page directory table.
194 if (pmap->pm_pdir == NULL) {
196 (vpte_t *)kmem_alloc_pageable(&kernel_map, PAGE_SIZE);
200 * allocate object for the pte array and page directory
202 npages = VPTE_PAGETABLE_SIZE +
203 (VM_MAX_USER_ADDRESS / PAGE_SIZE) * sizeof(vpte_t);
204 npages = (npages + PAGE_MASK) / PAGE_SIZE;
206 if (pmap->pm_pteobj == NULL)
207 pmap->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, npages);
208 pmap->pm_pdindex = npages - 1;
211 * allocate the page directory page
213 ptdpg = vm_page_grab(pmap->pm_pteobj, pmap->pm_pdindex,
214 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
216 ptdpg->wire_count = 1;
217 ++vmstats.v_wire_count;
219 /* not usually mapped */
220 vm_page_flag_clear(ptdpg, PG_MAPPED | PG_BUSY);
221 ptdpg->valid = VM_PAGE_BITS_ALL;
223 pmap_kenter((vm_offset_t)pmap->pm_pdir, VM_PAGE_TO_PHYS(ptdpg));
224 pmap->pm_pdirpte = KernelPTA[(vm_offset_t)pmap->pm_pdir >> PAGE_SHIFT];
225 if ((ptdpg->flags & PG_ZERO) == 0)
226 bzero(pmap->pm_pdir, PAGE_SIZE);
230 pmap->pm_ptphint = NULL;
231 pmap->pm_cpucachemask = 0;
232 TAILQ_INIT(&pmap->pm_pvlist);
233 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
234 pmap->pm_stats.resident_count = 1;
238 * Clean up a pmap structure so it can be physically freed
243 pmap_puninit(pmap_t pmap)
245 lwkt_gettoken(&vm_token);
247 kmem_free(&kernel_map, (vm_offset_t)pmap->pm_pdir, PAGE_SIZE);
248 pmap->pm_pdir = NULL;
250 if (pmap->pm_pteobj) {
251 vm_object_deallocate(pmap->pm_pteobj);
252 pmap->pm_pteobj = NULL;
254 lwkt_reltoken(&vm_token);
259 * Wire in kernel global address entries. To avoid a race condition
260 * between pmap initialization and pmap_growkernel, this procedure
261 * adds the pmap to the master list (which growkernel scans to update),
262 * then copies the template.
264 * In a virtual kernel there are no kernel global address entries.
269 pmap_pinit2(struct pmap *pmap)
272 lwkt_gettoken(&vm_token);
273 TAILQ_INSERT_TAIL(&pmap_list, pmap, pm_pmnode);
274 lwkt_reltoken(&vm_token);
279 * Release all resources held by the given physical map.
281 * Should only be called if the map contains no valid mappings.
285 static int pmap_release_callback(struct vm_page *p, void *data);
288 pmap_release(struct pmap *pmap)
290 struct mdglobaldata *gd = mdcpu;
291 vm_object_t object = pmap->pm_pteobj;
292 struct rb_vm_page_scan_info info;
294 KKASSERT(pmap != &kernel_pmap);
296 #if defined(DIAGNOSTIC)
297 if (object->ref_count != 1)
298 panic("pmap_release: pteobj reference count != 1");
301 * Once we destroy the page table, the mapping becomes invalid.
302 * Don't waste time doing a madvise to invalidate the mapping, just
303 * set cpucachemask to 0.
305 if (pmap->pm_pdir == gd->gd_PT1pdir) {
306 gd->gd_PT1pdir = NULL;
308 /* madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL); */
310 if (pmap->pm_pdir == gd->gd_PT2pdir) {
311 gd->gd_PT2pdir = NULL;
313 /* madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL); */
315 if (pmap->pm_pdir == gd->gd_PT3pdir) {
316 gd->gd_PT3pdir = NULL;
318 /* madvise(gd->gd_PT3map, SEG_SIZE, MADV_INVAL); */
322 info.object = object;
324 lwkt_gettoken(&vm_token);
325 TAILQ_REMOVE(&pmap_list, pmap, pm_pmnode);
332 info.limit = object->generation;
334 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
335 pmap_release_callback, &info);
336 if (info.error == 0 && info.mpte) {
337 if (!pmap_release_free_page(pmap, info.mpte))
341 } while (info.error);
344 * Leave the KVA reservation for pm_pdir cached for later reuse.
346 pmap->pm_pdirpte = 0;
347 pmap->pm_cpucachemask = 0;
348 lwkt_reltoken(&vm_token);
352 * Callback to release a page table page backing a directory
356 pmap_release_callback(struct vm_page *p, void *data)
358 struct rb_vm_page_scan_info *info = data;
360 if (p->pindex == info->pmap->pm_pdindex) {
364 if (!pmap_release_free_page(info->pmap, p)) {
368 if (info->object->generation != info->limit) {
376 * Retire the given physical map from service. Should only be called if
377 * the map contains no valid mappings.
382 pmap_destroy(pmap_t pmap)
387 lwkt_gettoken(&vm_token);
388 if (--pmap->pm_count == 0) {
390 panic("destroying a pmap is not yet implemented");
392 lwkt_reltoken(&vm_token);
396 * Add a reference to the specified pmap.
401 pmap_reference(pmap_t pmap)
404 lwkt_gettoken(&vm_token);
406 lwkt_reltoken(&vm_token);
410 /************************************************************************
411 * VMSPACE MANAGEMENT *
412 ************************************************************************
414 * The VMSPACE management we do in our virtual kernel must be reflected
415 * in the real kernel. This is accomplished by making vmspace system
416 * calls to the real kernel.
419 cpu_vmspace_alloc(struct vmspace *vm)
424 #define LAST_EXTENT (VM_MAX_USER_ADDRESS - 0x80000000)
426 if (vmspace_create(&vm->vm_pmap, 0, NULL) < 0)
427 panic("vmspace_create() failed");
429 rp = vmspace_mmap(&vm->vm_pmap, (void *)0x00000000, 0x40000000,
430 PROT_READ|PROT_WRITE,
431 MAP_FILE|MAP_SHARED|MAP_VPAGETABLE|MAP_FIXED,
433 if (rp == MAP_FAILED)
434 panic("vmspace_mmap: failed1");
435 vmspace_mcontrol(&vm->vm_pmap, (void *)0x00000000, 0x40000000,
437 rp = vmspace_mmap(&vm->vm_pmap, (void *)0x40000000, 0x40000000,
438 PROT_READ|PROT_WRITE,
439 MAP_FILE|MAP_SHARED|MAP_VPAGETABLE|MAP_FIXED,
440 MemImageFd, 0x40000000);
441 if (rp == MAP_FAILED)
442 panic("vmspace_mmap: failed2");
443 vmspace_mcontrol(&vm->vm_pmap, (void *)0x40000000, 0x40000000,
445 rp = vmspace_mmap(&vm->vm_pmap, (void *)0x80000000, LAST_EXTENT,
446 PROT_READ|PROT_WRITE,
447 MAP_FILE|MAP_SHARED|MAP_VPAGETABLE|MAP_FIXED,
448 MemImageFd, 0x80000000);
449 vmspace_mcontrol(&vm->vm_pmap, (void *)0x80000000, LAST_EXTENT,
451 if (rp == MAP_FAILED)
452 panic("vmspace_mmap: failed3");
454 r = vmspace_mcontrol(&vm->vm_pmap, (void *)0x00000000, 0x40000000,
455 MADV_SETMAP, vmspace_pmap(vm)->pm_pdirpte);
457 panic("vmspace_mcontrol: failed1");
458 r = vmspace_mcontrol(&vm->vm_pmap, (void *)0x40000000, 0x40000000,
459 MADV_SETMAP, vmspace_pmap(vm)->pm_pdirpte);
461 panic("vmspace_mcontrol: failed2");
462 r = vmspace_mcontrol(&vm->vm_pmap, (void *)0x80000000, LAST_EXTENT,
463 MADV_SETMAP, vmspace_pmap(vm)->pm_pdirpte);
465 panic("vmspace_mcontrol: failed3");
469 cpu_vmspace_free(struct vmspace *vm)
471 if (vmspace_destroy(&vm->vm_pmap) < 0)
472 panic("vmspace_destroy() failed");
475 /************************************************************************
476 * Procedures which operate directly on the kernel PMAP *
477 ************************************************************************/
480 * This maps the requested page table and gives us access to it.
482 * This routine can be called from a potentially preempting interrupt
483 * thread or from a normal thread.
486 get_ptbase(struct pmap *pmap, vm_offset_t va)
488 struct mdglobaldata *gd = mdcpu;
490 if (pmap == &kernel_pmap) {
491 KKASSERT(va >= KvaStart && va < KvaEnd);
492 return(KernelPTA + (va >> PAGE_SHIFT));
493 } else if (pmap->pm_pdir == gd->gd_PT1pdir) {
494 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
495 *gd->gd_PT1pde = pmap->pm_pdirpte;
496 madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL);
497 atomic_set_cpumask(&pmap->pm_cpucachemask,
500 return(gd->gd_PT1map + (va >> PAGE_SHIFT));
501 } else if (pmap->pm_pdir == gd->gd_PT2pdir) {
502 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
503 *gd->gd_PT2pde = pmap->pm_pdirpte;
504 madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL);
505 atomic_set_cpumask(&pmap->pm_cpucachemask,
508 return(gd->gd_PT2map + (va >> PAGE_SHIFT));
512 * If we aren't running from a potentially preempting interrupt,
513 * load a new page table directory into the page table cache
515 if (gd->mi.gd_intr_nesting_level == 0 &&
516 (gd->mi.gd_curthread->td_flags & TDF_INTTHREAD) == 0) {
518 * Choose one or the other and map the page table
519 * in the KVA space reserved for it.
521 if ((gd->gd_PTflip = 1 - gd->gd_PTflip) == 0) {
522 gd->gd_PT1pdir = pmap->pm_pdir;
523 *gd->gd_PT1pde = pmap->pm_pdirpte;
524 madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL);
525 atomic_set_cpumask(&pmap->pm_cpucachemask,
527 return(gd->gd_PT1map + (va >> PAGE_SHIFT));
529 gd->gd_PT2pdir = pmap->pm_pdir;
530 *gd->gd_PT2pde = pmap->pm_pdirpte;
531 madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL);
532 atomic_set_cpumask(&pmap->pm_cpucachemask,
534 return(gd->gd_PT2map + (va >> PAGE_SHIFT));
539 * If we are running from a preempting interrupt use a private
540 * map. The caller must be in a critical section.
542 KKASSERT(IN_CRITICAL_SECT(curthread));
543 if (pmap->pm_pdir == gd->gd_PT3pdir) {
544 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
545 *gd->gd_PT3pde = pmap->pm_pdirpte;
546 madvise(gd->gd_PT3map, SEG_SIZE, MADV_INVAL);
547 atomic_set_cpumask(&pmap->pm_cpucachemask,
551 gd->gd_PT3pdir = pmap->pm_pdir;
552 *gd->gd_PT3pde = pmap->pm_pdirpte;
553 madvise(gd->gd_PT3map, SEG_SIZE, MADV_INVAL);
554 atomic_set_cpumask(&pmap->pm_cpucachemask,
557 return(gd->gd_PT3map + (va >> PAGE_SHIFT));
561 get_ptbase1(struct pmap *pmap, vm_offset_t va)
563 struct mdglobaldata *gd = mdcpu;
565 if (pmap == &kernel_pmap) {
566 KKASSERT(va >= KvaStart && va < KvaEnd);
567 return(KernelPTA + (va >> PAGE_SHIFT));
568 } else if (pmap->pm_pdir == gd->gd_PT1pdir) {
569 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
570 *gd->gd_PT1pde = pmap->pm_pdirpte;
571 madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL);
572 atomic_set_cpumask(&pmap->pm_cpucachemask,
575 return(gd->gd_PT1map + (va >> PAGE_SHIFT));
577 KKASSERT(gd->mi.gd_intr_nesting_level == 0 &&
578 (gd->mi.gd_curthread->td_flags & TDF_INTTHREAD) == 0);
579 gd->gd_PT1pdir = pmap->pm_pdir;
580 *gd->gd_PT1pde = pmap->pm_pdirpte;
581 madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL);
582 return(gd->gd_PT1map + (va >> PAGE_SHIFT));
586 get_ptbase2(struct pmap *pmap, vm_offset_t va)
588 struct mdglobaldata *gd = mdcpu;
590 if (pmap == &kernel_pmap) {
591 KKASSERT(va >= KvaStart && va < KvaEnd);
592 return(KernelPTA + (va >> PAGE_SHIFT));
593 } else if (pmap->pm_pdir == gd->gd_PT2pdir) {
594 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
595 *gd->gd_PT2pde = pmap->pm_pdirpte;
596 madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL);
597 atomic_set_cpumask(&pmap->pm_cpucachemask,
600 return(gd->gd_PT2map + (va >> PAGE_SHIFT));
602 KKASSERT(gd->mi.gd_intr_nesting_level == 0 &&
603 (gd->mi.gd_curthread->td_flags & TDF_INTTHREAD) == 0);
604 gd->gd_PT2pdir = pmap->pm_pdir;
605 *gd->gd_PT2pde = pmap->pm_pdirpte;
606 madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL);
607 return(gd->gd_PT2map + (va >> PAGE_SHIFT));
611 * Return a pointer to the page table entry for the specified va in the
612 * specified pmap. NULL is returned if there is no valid page table page
615 static __inline vpte_t *
616 pmap_pte(struct pmap *pmap, vm_offset_t va)
620 ptep = &pmap->pm_pdir[va >> SEG_SHIFT];
624 return (get_ptbase(pmap, va));
630 * Enter a mapping into kernel_pmap. Mappings created in this fashion
631 * are not managed. Mappings must be immediately accessible on all cpus.
633 * Call pmap_inval_pte() to invalidate the virtual pte and clean out the
634 * real pmap and handle related races before storing the new vpte.
637 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
642 KKASSERT(va >= KvaStart && va < KvaEnd);
643 npte = (vpte_t)pa | VPTE_R | VPTE_W | VPTE_V;
644 ptep = KernelPTA + (va >> PAGE_SHIFT);
646 pmap_inval_pte(ptep, &kernel_pmap, va);
651 * Synchronize a kvm mapping originally made for the private use on
652 * some other cpu so it can be used on all cpus.
654 * XXX add MADV_RESYNC to improve performance.
657 pmap_kenter_sync(vm_offset_t va)
659 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
663 * Synchronize a kvm mapping originally made for the private use on
664 * some other cpu so it can be used on our cpu. Turns out to be the
665 * same madvise() call, because we have to sync the real pmaps anyway.
667 * XXX add MADV_RESYNC to improve performance.
670 pmap_kenter_sync_quick(vm_offset_t va)
672 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
677 * Make a previously read-only kernel mapping R+W (not implemented by
681 pmap_kmodify_rw(vm_offset_t va)
683 *pmap_kpte(va) |= VPTE_R | VPTE_W;
684 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
688 * Make a kernel mapping non-cacheable (not applicable to virtual kernels)
691 pmap_kmodify_nc(vm_offset_t va)
693 *pmap_kpte(va) |= VPTE_N;
694 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
700 * Map a contiguous range of physical memory to a KVM
703 pmap_map(vm_offset_t *virtp, vm_paddr_t start, vm_paddr_t end, int prot)
705 vm_offset_t sva, virt;
708 while (start < end) {
709 pmap_kenter(virt, start);
718 pmap_kpte(vm_offset_t va)
722 KKASSERT(va >= KvaStart && va < KvaEnd);
723 ptep = KernelPTA + (va >> PAGE_SHIFT);
728 * Enter an unmanaged KVA mapping for the private use of the current
729 * cpu only. pmap_kenter_sync() may be called to make the mapping usable
732 * It is illegal for the mapping to be accessed by other cpus unleess
733 * pmap_kenter_sync*() is called.
736 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
741 KKASSERT(va >= KvaStart && va < KvaEnd);
743 npte = (vpte_t)pa | VPTE_R | VPTE_W | VPTE_V;
744 ptep = KernelPTA + (va >> PAGE_SHIFT);
746 pmap_inval_pte_quick(ptep, &kernel_pmap, va);
751 * Make a temporary mapping for a physical address. This is only intended
752 * to be used for panic dumps.
754 * The caller is responsible for calling smp_invltlb().
757 pmap_kenter_temporary(vm_paddr_t pa, long i)
759 pmap_kenter_quick(crashdumpmap + (i * PAGE_SIZE), pa);
760 return ((void *)crashdumpmap);
764 * Remove an unmanaged mapping created with pmap_kenter*().
767 pmap_kremove(vm_offset_t va)
771 KKASSERT(va >= KvaStart && va < KvaEnd);
773 ptep = KernelPTA + (va >> PAGE_SHIFT);
775 pmap_inval_pte(ptep, &kernel_pmap, va);
780 * Remove an unmanaged mapping created with pmap_kenter*() but synchronize
781 * only with this cpu.
783 * Unfortunately because we optimize new entries by testing VPTE_V later
784 * on, we actually still have to synchronize with all the cpus. XXX maybe
785 * store a junk value and test against 0 in the other places instead?
788 pmap_kremove_quick(vm_offset_t va)
792 KKASSERT(va >= KvaStart && va < KvaEnd);
794 ptep = KernelPTA + (va >> PAGE_SHIFT);
796 pmap_inval_pte(ptep, &kernel_pmap, va); /* NOT _quick */
801 * Extract the physical address from the kernel_pmap that is associated
802 * with the specified virtual address.
805 pmap_kextract(vm_offset_t va)
810 KKASSERT(va >= KvaStart && va < KvaEnd);
812 ptep = KernelPTA + (va >> PAGE_SHIFT);
813 pa = (vm_paddr_t)(*ptep & VPTE_FRAME) | (va & PAGE_MASK);
818 * Map a set of unmanaged VM pages into KVM.
821 pmap_qenter(vm_offset_t va, struct vm_page **m, int count)
823 KKASSERT(va >= KvaStart && va + count * PAGE_SIZE < KvaEnd);
827 ptep = KernelPTA + (va >> PAGE_SHIFT);
829 pmap_inval_pte(ptep, &kernel_pmap, va);
830 *ptep = (vpte_t)(*m)->phys_addr | VPTE_R | VPTE_W | VPTE_V;
838 * Undo the effects of pmap_qenter*().
841 pmap_qremove(vm_offset_t va, int count)
843 KKASSERT(va >= KvaStart && va + count * PAGE_SIZE < KvaEnd);
847 ptep = KernelPTA + (va >> PAGE_SHIFT);
849 pmap_inval_pte(ptep, &kernel_pmap, va);
856 /************************************************************************
857 * Misc support glue called by machine independant code *
858 ************************************************************************
860 * These routines are called by machine independant code to operate on
861 * certain machine-dependant aspects of processes, threads, and pmaps.
865 * Initialize MD portions of the thread structure.
868 pmap_init_thread(thread_t td)
870 /* enforce pcb placement */
871 td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
872 td->td_savefpu = &td->td_pcb->pcb_save;
873 td->td_sp = (char *)td->td_pcb - 16;
877 * This routine directly affects the fork perf for a process.
880 pmap_init_proc(struct proc *p)
885 * Destroy the UPAGES for a process that has exited and disassociate
886 * the process from its thread.
889 pmap_dispose_proc(struct proc *p)
891 KASSERT(p->p_lock == 0, ("attempt to dispose referenced proc! %p", p));
895 * We pre-allocate all page table pages for kernel virtual memory so
896 * this routine will only be called if KVM has been exhausted.
901 pmap_growkernel(vm_offset_t kstart, vm_offset_t kend)
905 addr = (kend + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
907 lwkt_gettoken(&vm_token);
908 if (addr > virtual_end - SEG_SIZE)
909 panic("KVM exhausted");
910 kernel_vm_end = addr;
911 lwkt_reltoken(&vm_token);
915 * The modification bit is not tracked for any pages in this range. XXX
916 * such pages in this maps should always use pmap_k*() functions and not
919 * XXX User and kernel address spaces are independant for virtual kernels,
920 * this function only applies to the kernel pmap.
923 pmap_track_modified(pmap_t pmap, vm_offset_t va)
925 if (pmap != &kernel_pmap)
927 if ((va < clean_sva) || (va >= clean_eva))
933 /************************************************************************
934 * Procedures supporting managed page table pages *
935 ************************************************************************
937 * These procedures are used to track managed page table pages. These pages
938 * use the page table page's vm_page_t to track PTEs in the page. The
939 * page table pages themselves are arranged in a VM object, pmap->pm_pteobj.
941 * This allows the system to throw away page table pages for user processes
942 * at will and reinstantiate them on demand.
946 * This routine works like vm_page_lookup() but also blocks as long as the
947 * page is busy. This routine does not busy the page it returns.
949 * Unless the caller is managing objects whos pages are in a known state,
950 * the call should be made with a critical section held so the page's object
951 * association remains valid on return.
954 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
959 m = vm_page_lookup(object, pindex);
960 if (m && vm_page_sleep_busy(m, FALSE, "pplookp"))
966 * This routine unholds page table pages, and if the hold count
967 * drops to zero, then it decrements the wire count.
969 * We must recheck that this is the last hold reference after busy-sleeping
973 _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
975 while (vm_page_sleep_busy(m, FALSE, "pmuwpt"))
977 KASSERT(m->queue == PQ_NONE,
978 ("_pmap_unwire_pte_hold: %p->queue != PQ_NONE", m));
980 if (m->hold_count == 1) {
982 * Unmap the page table page.
985 KKASSERT(pmap->pm_pdir[m->pindex] != 0);
986 pmap_inval_pde(&pmap->pm_pdir[m->pindex], pmap,
987 (vm_offset_t)m->pindex << SEG_SHIFT);
988 KKASSERT(pmap->pm_stats.resident_count > 0);
989 --pmap->pm_stats.resident_count;
991 if (pmap->pm_ptphint == m)
992 pmap->pm_ptphint = NULL;
995 * This was our last hold, the page had better be unwired
996 * after we decrement wire_count.
998 * FUTURE NOTE: shared page directory page could result in
999 * multiple wire counts.
1003 KKASSERT(m->wire_count == 0);
1004 --vmstats.v_wire_count;
1005 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1007 vm_page_free_zero(m);
1010 KKASSERT(m->hold_count > 1);
1016 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
1018 KKASSERT(m->hold_count > 0);
1019 if (m->hold_count > 1) {
1023 return _pmap_unwire_pte_hold(pmap, m);
1028 * After removing a page table entry, this routine is used to
1029 * conditionally free the page, and manage the hold/wire counts.
1032 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte)
1038 * page table pages in the kernel_pmap are not managed.
1040 if (pmap == &kernel_pmap)
1042 ptepindex = (va >> PDRSHIFT);
1043 if (pmap->pm_ptphint &&
1044 (pmap->pm_ptphint->pindex == ptepindex)) {
1045 mpte = pmap->pm_ptphint;
1047 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1048 pmap->pm_ptphint = mpte;
1051 return pmap_unwire_pte_hold(pmap, mpte);
1055 * Attempt to release and free the vm_page backing a page directory page
1056 * in a pmap. Returns 1 on success, 0 on failure (if the procedure had
1060 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1062 vpte_t *pde = pmap->pm_pdir;
1065 * This code optimizes the case of freeing non-busy
1066 * page-table pages. Those pages are zero now, and
1067 * might as well be placed directly into the zero queue.
1069 if (vm_page_sleep_busy(p, FALSE, "pmaprl"))
1073 KKASSERT(pmap->pm_stats.resident_count > 0);
1074 --pmap->pm_stats.resident_count;
1076 if (p->hold_count) {
1077 panic("pmap_release: freeing held page table page");
1080 * Page directory pages need to have the kernel stuff cleared, so
1081 * they can go into the zero queue also.
1083 * In virtual kernels there is no 'kernel stuff'. For the moment
1084 * I just make sure the whole thing has been zero'd even though
1085 * it should already be completely zero'd.
1087 * pmaps for vkernels do not self-map because they do not share
1088 * their address space with the vkernel. Clearing of pde[] thus
1089 * only applies to page table pages and not to the page directory
1092 if (p->pindex == pmap->pm_pdindex) {
1093 bzero(pde, VPTE_PAGETABLE_SIZE);
1094 pmap_kremove((vm_offset_t)pmap->pm_pdir);
1096 KKASSERT(pde[p->pindex] != 0);
1097 pmap_inval_pde(&pde[p->pindex], pmap,
1098 (vm_offset_t)p->pindex << SEG_SHIFT);
1102 * Clear the matching hint
1104 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1105 pmap->pm_ptphint = NULL;
1108 * And throw the page away. The page is completely zero'd out so
1109 * optimize the free call.
1112 vmstats.v_wire_count--;
1113 vm_page_free_zero(p);
1118 * This routine is called if the page table page is not mapped in the page
1121 * The routine is broken up into two parts for readability.
1123 * It must return a held mpte and map the page directory page as required.
1124 * Because vm_page_grab() can block, we must re-check pm_pdir[ptepindex]
1127 _pmap_allocpte(pmap_t pmap, unsigned ptepindex)
1133 * Find or fabricate a new pagetable page. A busied page will be
1134 * returned. This call may block.
1136 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1137 VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1139 KASSERT(m->queue == PQ_NONE,
1140 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1143 * Increment the hold count for the page we will be returning to
1149 * It is possible that someone else got in and mapped by the page
1150 * directory page while we were blocked, if so just unbusy and
1151 * return the held page.
1153 if ((ptepa = pmap->pm_pdir[ptepindex]) != 0) {
1154 KKASSERT((ptepa & VPTE_FRAME) == VM_PAGE_TO_PHYS(m));
1159 if (m->wire_count == 0)
1160 vmstats.v_wire_count++;
1164 * Map the pagetable page into the process address space, if
1165 * it isn't already there.
1167 ++pmap->pm_stats.resident_count;
1169 ptepa = VM_PAGE_TO_PHYS(m);
1170 pmap->pm_pdir[ptepindex] = (vpte_t)ptepa | VPTE_R | VPTE_W | VPTE_V |
1174 * We are likely about to access this page table page, so set the
1175 * page table hint to reduce overhead.
1177 pmap->pm_ptphint = m;
1180 * Try to use the new mapping, but if we cannot, then
1181 * do it with the routine that maps the page explicitly.
1183 if ((m->flags & PG_ZERO) == 0)
1184 pmap_zero_page(ptepa);
1186 m->valid = VM_PAGE_BITS_ALL;
1187 vm_page_flag_clear(m, PG_ZERO);
1188 vm_page_flag_set(m, PG_MAPPED);
1195 * Determine the page table page required to access the VA in the pmap
1196 * and allocate it if necessary. Return a held vm_page_t for the page.
1198 * Only used with user pmaps.
1201 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1208 * Calculate pagetable page index
1210 ptepindex = va >> PDRSHIFT;
1213 * Get the page directory entry
1215 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1218 * This supports switching from a 4MB page to a
1221 if (ptepa & VPTE_PS) {
1222 KKASSERT(pmap->pm_pdir[ptepindex] != 0);
1223 pmap_inval_pde(&pmap->pm_pdir[ptepindex], pmap,
1224 (vm_offset_t)ptepindex << SEG_SHIFT);
1229 * If the page table page is mapped, we just increment the
1230 * hold count, and activate it.
1234 * In order to get the page table page, try the
1237 if (pmap->pm_ptphint &&
1238 (pmap->pm_ptphint->pindex == ptepindex)) {
1239 m = pmap->pm_ptphint;
1241 m = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1242 pmap->pm_ptphint = m;
1248 * Here if the pte page isn't mapped, or if it has been deallocated.
1250 return _pmap_allocpte(pmap, ptepindex);
1253 /************************************************************************
1254 * Managed pages in pmaps *
1255 ************************************************************************
1257 * All pages entered into user pmaps and some pages entered into the kernel
1258 * pmap are managed, meaning that pmap_protect() and other related management
1259 * functions work on these pages.
1263 * free the pv_entry back to the free list. This function may be
1264 * called from an interrupt.
1266 static __inline void
1267 free_pv_entry(pv_entry_t pv)
1274 * get a new pv_entry, allocating a block from the system
1275 * when needed. This function may be called from an interrupt.
1281 if (pv_entry_high_water &&
1282 (pv_entry_count > pv_entry_high_water) &&
1283 (pmap_pagedaemon_waken == 0)) {
1284 pmap_pagedaemon_waken = 1;
1285 wakeup (&vm_pages_needed);
1287 return zalloc(&pvzone);
1291 * This routine is very drastic, but can save the system
1301 static int warningdone=0;
1303 if (pmap_pagedaemon_waken == 0)
1305 lwkt_gettoken(&vm_token);
1306 pmap_pagedaemon_waken = 0;
1308 if (warningdone < 5) {
1309 kprintf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1313 for(i = 0; i < vm_page_array_size; i++) {
1314 m = &vm_page_array[i];
1315 if (m->wire_count || m->hold_count || m->busy ||
1316 (m->flags & PG_BUSY))
1320 lwkt_reltoken(&vm_token);
1324 * If it is the first entry on the list, it is actually
1325 * in the header and we must copy the following entry up
1326 * to the header. Otherwise we must search the list for
1327 * the entry. In either case we free the now unused entry.
1330 pmap_remove_entry(struct pmap *pmap, vm_page_t m, vm_offset_t va)
1336 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1337 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1338 if (pmap == pv->pv_pmap && va == pv->pv_va)
1342 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1343 if (va == pv->pv_va)
1349 * Note that pv_ptem is NULL if the page table page itself is not
1350 * managed, even if the page being removed IS managed.
1354 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1355 m->md.pv_list_count--;
1356 m->object->agg_pv_list_count--;
1357 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1358 if (TAILQ_EMPTY(&m->md.pv_list))
1359 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1360 ++pmap->pm_generation;
1361 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem);
1369 * Create a pv entry for page at pa for (pmap, va). If the page table page
1370 * holding the VA is managed, mpte will be non-NULL.
1373 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
1378 pv = get_pv_entry();
1383 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1384 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1385 ++pmap->pm_generation;
1386 m->md.pv_list_count++;
1387 m->object->agg_pv_list_count++;
1393 * pmap_remove_pte: do the things to unmap a page in a process
1396 pmap_remove_pte(struct pmap *pmap, vpte_t *ptq, vm_offset_t va)
1401 oldpte = pmap_inval_loadandclear(ptq, pmap, va);
1402 if (oldpte & VPTE_WIRED)
1403 --pmap->pm_stats.wired_count;
1404 KKASSERT(pmap->pm_stats.wired_count >= 0);
1408 * Machines that don't support invlpg, also don't support
1409 * VPTE_G. XXX VPTE_G is disabled for SMP so don't worry about
1412 if (oldpte & VPTE_G)
1413 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
1415 KKASSERT(pmap->pm_stats.resident_count > 0);
1416 --pmap->pm_stats.resident_count;
1417 if (oldpte & VPTE_MANAGED) {
1418 m = PHYS_TO_VM_PAGE(oldpte);
1419 if (oldpte & VPTE_M) {
1420 #if defined(PMAP_DIAGNOSTIC)
1421 if (pmap_nw_modified((pt_entry_t) oldpte)) {
1423 "pmap_remove: modified page not writable: va: 0x%x, pte: 0x%x\n",
1427 if (pmap_track_modified(pmap, va))
1430 if (oldpte & VPTE_A)
1431 vm_page_flag_set(m, PG_REFERENCED);
1432 return pmap_remove_entry(pmap, m, va);
1434 return pmap_unuse_pt(pmap, va, NULL);
1443 * Remove a single page from a process address space.
1445 * This function may not be called from an interrupt if the pmap is
1449 pmap_remove_page(struct pmap *pmap, vm_offset_t va)
1454 * if there is no pte for this address, just skip it!!! Otherwise
1455 * get a local va for mappings for this pmap and remove the entry.
1457 if (*pmap_pde(pmap, va) != 0) {
1458 ptq = get_ptbase(pmap, va);
1460 pmap_remove_pte(pmap, ptq, va);
1466 * Remove the given range of addresses from the specified map.
1468 * It is assumed that the start and end are properly rounded to the
1471 * This function may not be called from an interrupt if the pmap is
1477 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
1481 vm_offset_t ptpaddr;
1482 vm_pindex_t sindex, eindex;
1487 lwkt_gettoken(&vm_token);
1488 KKASSERT(pmap->pm_stats.resident_count >= 0);
1489 if (pmap->pm_stats.resident_count == 0) {
1490 lwkt_reltoken(&vm_token);
1495 * special handling of removing one page. a very
1496 * common operation and easy to short circuit some
1499 if (((sva + PAGE_SIZE) == eva) &&
1500 ((pmap->pm_pdir[(sva >> PDRSHIFT)] & VPTE_PS) == 0)) {
1501 pmap_remove_page(pmap, sva);
1502 lwkt_reltoken(&vm_token);
1507 * Get a local virtual address for the mappings that are being
1510 * XXX this is really messy because the kernel pmap is not relative
1513 sindex = (sva >> PAGE_SHIFT);
1514 eindex = (eva >> PAGE_SHIFT);
1516 for (; sindex < eindex; sindex = pdnxt) {
1520 * Calculate index for next page table.
1522 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1523 if (pmap->pm_stats.resident_count == 0)
1526 pdirindex = sindex / NPDEPG;
1527 if (((ptpaddr = pmap->pm_pdir[pdirindex]) & VPTE_PS) != 0) {
1528 KKASSERT(pmap->pm_pdir[pdirindex] != 0);
1529 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1530 pmap_inval_pde(&pmap->pm_pdir[pdirindex], pmap,
1531 (vm_offset_t)pdirindex << SEG_SHIFT);
1536 * Weed out invalid mappings. Note: we assume that the page
1537 * directory table is always allocated, and in kernel virtual.
1543 * Limit our scan to either the end of the va represented
1544 * by the current page table page, or to the end of the
1545 * range being removed.
1551 * NOTE: pmap_remove_pte() can block.
1553 for (; sindex != pdnxt; sindex++) {
1556 ptbase = get_ptbase(pmap, sindex << PAGE_SHIFT);
1559 va = i386_ptob(sindex);
1560 if (pmap_remove_pte(pmap, ptbase, va))
1564 lwkt_reltoken(&vm_token);
1568 * Removes this physical page from all physical maps in which it resides.
1569 * Reflects back modify bits to the pager.
1571 * This routine may not be called from an interrupt.
1576 pmap_remove_all(vm_page_t m)
1581 #if defined(PMAP_DIAGNOSTIC)
1583 * XXX this makes pmap_page_protect(NONE) illegal for non-managed
1586 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
1587 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%08llx", (long long)VM_PAGE_TO_PHYS(m));
1592 lwkt_gettoken(&vm_token);
1593 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
1594 KKASSERT(pv->pv_pmap->pm_stats.resident_count > 0);
1595 --pv->pv_pmap->pm_stats.resident_count;
1597 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
1598 KKASSERT(pte != NULL);
1600 tpte = pmap_inval_loadandclear(pte, pv->pv_pmap, pv->pv_va);
1601 if (tpte & VPTE_WIRED)
1602 --pv->pv_pmap->pm_stats.wired_count;
1603 KKASSERT(pv->pv_pmap->pm_stats.wired_count >= 0);
1606 vm_page_flag_set(m, PG_REFERENCED);
1609 * Update the vm_page_t clean and reference bits.
1611 if (tpte & VPTE_M) {
1612 #if defined(PMAP_DIAGNOSTIC)
1613 if (pmap_nw_modified((pt_entry_t) tpte)) {
1615 "pmap_remove_all: modified page not writable: va: 0x%x, pte: 0x%x\n",
1619 if (pmap_track_modified(pv->pv_pmap, pv->pv_va))
1622 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1623 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
1624 ++pv->pv_pmap->pm_generation;
1625 m->md.pv_list_count--;
1626 m->object->agg_pv_list_count--;
1627 if (TAILQ_EMPTY(&m->md.pv_list))
1628 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1629 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
1632 KKASSERT((m->flags & (PG_MAPPED | PG_WRITEABLE)) == 0);
1633 lwkt_reltoken(&vm_token);
1638 * Set the physical protection on the specified range of this map
1641 * This function may not be called from an interrupt if the map is
1642 * not the kernel_pmap.
1647 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
1651 vm_offset_t pdnxt, ptpaddr;
1652 vm_pindex_t sindex, eindex;
1658 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1659 pmap_remove(pmap, sva, eva);
1663 if (prot & VM_PROT_WRITE)
1666 lwkt_gettoken(&vm_token);
1667 ptbase = get_ptbase(pmap, sva);
1669 sindex = (sva >> PAGE_SHIFT);
1670 eindex = (eva >> PAGE_SHIFT);
1673 for (; sindex < eindex; sindex = pdnxt) {
1677 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1679 pdirindex = sindex / NPDEPG;
1682 * Clear the modified and writable bits for a 4m page.
1683 * Throw away the modified bit (?)
1685 if (((ptpaddr = pmap->pm_pdir[pdirindex]) & VPTE_PS) != 0) {
1686 pmap_clean_pde(&pmap->pm_pdir[pdirindex], pmap,
1687 (vm_offset_t)pdirindex << SEG_SHIFT);
1688 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1693 * Weed out invalid mappings. Note: we assume that the page
1694 * directory table is always allocated, and in kernel virtual.
1699 if (pdnxt > eindex) {
1703 for (; sindex != pdnxt; sindex++) {
1708 * Clean managed pages and also check the accessed
1709 * bit. Just remove write perms for unmanaged
1710 * pages. Be careful of races, turning off write
1711 * access will force a fault rather then setting
1712 * the modified bit at an unexpected time.
1714 ptep = &ptbase[sindex - sbase];
1715 if (*ptep & VPTE_MANAGED) {
1716 pbits = pmap_clean_pte(ptep, pmap,
1719 if (pbits & VPTE_A) {
1720 m = PHYS_TO_VM_PAGE(pbits);
1721 vm_page_flag_set(m, PG_REFERENCED);
1722 atomic_clear_long(ptep, VPTE_A);
1724 if (pbits & VPTE_M) {
1725 if (pmap_track_modified(pmap, i386_ptob(sindex))) {
1727 m = PHYS_TO_VM_PAGE(pbits);
1732 pbits = pmap_setro_pte(ptep, pmap,
1737 lwkt_reltoken(&vm_token);
1741 * Enter a managed page into a pmap. If the page is not wired related pmap
1742 * data can be destroyed at any time for later demand-operation.
1744 * Insert the vm_page (m) at virtual address (v) in (pmap), with the
1745 * specified protection, and wire the mapping if requested.
1747 * NOTE: This routine may not lazy-evaluate or lose information. The
1748 * page must actually be inserted into the given map NOW.
1750 * NOTE: When entering a page at a KVA address, the pmap must be the
1756 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1762 vpte_t origpte, newpte;
1770 lwkt_gettoken(&vm_token);
1773 * Get the page table page. The kernel_pmap's page table pages
1774 * are preallocated and have no associated vm_page_t.
1776 if (pmap == &kernel_pmap)
1779 mpte = pmap_allocpte(pmap, va);
1781 pte = pmap_pte(pmap, va);
1784 * Page Directory table entry not valid, we need a new PT page
1785 * and pmap_allocpte() didn't give us one. Oops!
1788 panic("pmap_enter: invalid page directory pmap=%p, va=0x%p\n",
1793 * Deal with races on the original mapping (though don't worry
1794 * about VPTE_A races) by cleaning it. This will force a fault
1795 * if an attempt is made to write to the page.
1797 pa = VM_PAGE_TO_PHYS(m) & VPTE_FRAME;
1798 origpte = pmap_clean_pte(pte, pmap, va);
1799 opa = origpte & VPTE_FRAME;
1801 if (origpte & VPTE_PS)
1802 panic("pmap_enter: attempted pmap_enter on 4MB page");
1805 * Mapping has not changed, must be protection or wiring change.
1807 if (origpte && (opa == pa)) {
1809 * Wiring change, just update stats. We don't worry about
1810 * wiring PT pages as they remain resident as long as there
1811 * are valid mappings in them. Hence, if a user page is wired,
1812 * the PT page will be also.
1814 if (wired && ((origpte & VPTE_WIRED) == 0))
1815 ++pmap->pm_stats.wired_count;
1816 else if (!wired && (origpte & VPTE_WIRED))
1817 --pmap->pm_stats.wired_count;
1818 KKASSERT(pmap->pm_stats.wired_count >= 0);
1821 * Remove the extra pte reference. Note that we cannot
1822 * optimize the RO->RW case because we have adjusted the
1823 * wiring count above and may need to adjust the wiring
1830 * We might be turning off write access to the page,
1831 * so we go ahead and sense modify status.
1833 if (origpte & VPTE_MANAGED) {
1834 if ((origpte & VPTE_M) &&
1835 pmap_track_modified(pmap, va)) {
1837 om = PHYS_TO_VM_PAGE(opa);
1841 KKASSERT(m->flags & PG_MAPPED);
1846 * Mapping has changed, invalidate old range and fall through to
1847 * handle validating new mapping.
1851 err = pmap_remove_pte(pmap, pte, va);
1853 panic("pmap_enter: pte vanished, va: %p", (void *)va);
1854 pte = pmap_pte(pmap, va);
1855 origpte = pmap_clean_pte(pte, pmap, va);
1856 opa = origpte & VPTE_FRAME;
1858 kprintf("pmap_enter: Warning, raced pmap %p va %p\n",
1864 * Enter on the PV list if part of our managed memory. Note that we
1865 * raise IPL while manipulating pv_table since pmap_enter can be
1866 * called at interrupt time.
1868 if (pmap_initialized &&
1869 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
1870 pmap_insert_entry(pmap, va, mpte, m);
1872 vm_page_flag_set(m, PG_MAPPED);
1876 * Increment counters
1878 ++pmap->pm_stats.resident_count;
1880 pmap->pm_stats.wired_count++;
1884 * Now validate mapping with desired protection/wiring.
1886 newpte = (vm_offset_t) (pa | pte_prot(pmap, prot) | VPTE_V);
1889 newpte |= VPTE_WIRED;
1890 if (pmap != &kernel_pmap)
1894 * If the mapping or permission bits are different from the
1895 * (now cleaned) original pte, an update is needed. We've
1896 * already downgraded or invalidated the page so all we have
1897 * to do now is update the bits.
1899 * XXX should we synchronize RO->RW changes to avoid another
1902 if ((origpte & ~(VPTE_W|VPTE_M|VPTE_A)) != newpte) {
1903 *pte = newpte | VPTE_A;
1904 if (newpte & VPTE_W)
1905 vm_page_flag_set(m, PG_WRITEABLE);
1907 KKASSERT((newpte & VPTE_MANAGED) == 0 || m->flags & PG_MAPPED);
1908 lwkt_reltoken(&vm_token);
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 lwkt_gettoken(&vm_token);
1941 * Get the page directory entry
1943 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1946 * If the page table page is mapped, we just increment
1947 * the hold count, and activate it.
1950 if (ptepa & VPTE_PS)
1951 panic("pmap_enter_quick: unexpected mapping into 4MB page");
1952 if (pmap->pm_ptphint &&
1953 (pmap->pm_ptphint->pindex == ptepindex)) {
1954 mpte = pmap->pm_ptphint;
1956 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1957 pmap->pm_ptphint = mpte;
1962 mpte = _pmap_allocpte(pmap, ptepindex);
1964 } while (mpte == NULL);
1967 * Ok, now that the page table page has been validated, get the pte.
1968 * If the pte is already mapped undo mpte's hold_count and
1971 pte = pmap_pte(pmap, va);
1973 pmap_unwire_pte_hold(pmap, mpte);
1974 lwkt_reltoken(&vm_token);
1979 * Enter on the PV list if part of our managed memory. Note that we
1980 * raise IPL while manipulating pv_table since pmap_enter can be
1981 * called at interrupt time.
1983 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
1984 pmap_insert_entry(pmap, va, mpte, m);
1985 vm_page_flag_set(m, PG_MAPPED);
1989 * Increment counters
1991 ++pmap->pm_stats.resident_count;
1993 pa = VM_PAGE_TO_PHYS(m);
1996 * Now validate mapping with RO protection
1998 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
1999 *pte = (vpte_t)pa | VPTE_V | VPTE_U;
2001 *pte = (vpte_t)pa | VPTE_V | VPTE_U | VPTE_MANAGED;
2002 /*pmap_inval_add(&info, pmap, va); shouldn't be needed 0->valid */
2003 /*pmap_inval_flush(&info); don't need for vkernel */
2004 lwkt_reltoken(&vm_token);
2008 * Extract the physical address for the translation at the specified
2009 * virtual address in the pmap.
2011 * The caller must hold vm_token if non-blocking operation is desired.
2015 pmap_extract(pmap_t pmap, vm_offset_t va)
2020 lwkt_gettoken(&vm_token);
2021 if (pmap && (pte = pmap->pm_pdir[va >> SEG_SHIFT]) != 0) {
2022 if (pte & VPTE_PS) {
2023 rtval = pte & ~((vpte_t)(1 << SEG_SHIFT) - 1);
2024 rtval |= va & SEG_MASK;
2026 pte = *get_ptbase(pmap, va);
2027 rtval = (pte & VPTE_FRAME) | (va & PAGE_MASK);
2032 lwkt_reltoken(&vm_token);
2036 #define MAX_INIT_PT (96)
2039 * This routine preloads the ptes for a given object into the specified pmap.
2040 * This eliminates the blast of soft faults on process startup and
2041 * immediately after an mmap.
2045 static int pmap_object_init_pt_callback(vm_page_t p, void *data);
2048 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2049 vm_object_t object, vm_pindex_t pindex,
2050 vm_size_t size, int limit)
2052 struct rb_vm_page_scan_info info;
2057 * We can't preinit if read access isn't set or there is no pmap
2060 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2064 * We can't preinit if the pmap is not the current pmap
2066 lp = curthread->td_lwp;
2067 if (lp == NULL || pmap != vmspace_pmap(lp->lwp_vmspace))
2070 psize = size >> PAGE_SHIFT;
2072 if ((object->type != OBJT_VNODE) ||
2073 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2074 (object->resident_page_count > MAX_INIT_PT))) {
2078 if (psize + pindex > object->size) {
2079 if (object->size < pindex)
2081 psize = object->size - pindex;
2088 * Use a red-black scan to traverse the requested range and load
2089 * any valid pages found into the pmap.
2091 * We cannot safely scan the object's memq unless we are in a
2092 * critical section since interrupts can remove pages from objects.
2094 info.start_pindex = pindex;
2095 info.end_pindex = pindex + psize - 1;
2102 lwkt_gettoken(&vm_token);
2103 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2104 pmap_object_init_pt_callback, &info);
2105 lwkt_reltoken(&vm_token);
2110 * The caller must hold vm_token.
2114 pmap_object_init_pt_callback(vm_page_t p, void *data)
2116 struct rb_vm_page_scan_info *info = data;
2117 vm_pindex_t rel_index;
2119 * don't allow an madvise to blow away our really
2120 * free pages allocating pv entries.
2122 if ((info->limit & MAP_PREFAULT_MADVISE) &&
2123 vmstats.v_free_count < vmstats.v_free_reserved) {
2126 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2127 (p->busy == 0) && (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2129 if ((p->queue - p->pc) == PQ_CACHE)
2130 vm_page_deactivate(p);
2131 rel_index = p->pindex - info->start_pindex;
2132 pmap_enter_quick(info->pmap,
2133 info->addr + i386_ptob(rel_index), p);
2140 * Return TRUE if the pmap is in shape to trivially
2141 * pre-fault the specified address.
2143 * Returns FALSE if it would be non-trivial or if a
2144 * pte is already loaded into the slot.
2149 pmap_prefault_ok(pmap_t pmap, vm_offset_t addr)
2154 lwkt_gettoken(&vm_token);
2155 if ((*pmap_pde(pmap, addr)) == 0) {
2158 pte = get_ptbase(pmap, addr);
2159 ret = (*pte) ? 0 : 1;
2161 lwkt_reltoken(&vm_token);
2166 * Change the wiring attribute for a map/virtual-address pair.
2167 * The mapping must already exist in the pmap.
2169 * No other requirements.
2172 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2179 lwkt_gettoken(&vm_token);
2180 pte = get_ptbase(pmap, va);
2182 if (wired && (*pte & VPTE_WIRED) == 0)
2183 ++pmap->pm_stats.wired_count;
2184 else if (!wired && (*pte & VPTE_WIRED))
2185 --pmap->pm_stats.wired_count;
2186 KKASSERT(pmap->pm_stats.wired_count >= 0);
2189 * Wiring is not a hardware characteristic so there is no need to
2190 * invalidate TLB. However, in an SMP environment we must use
2191 * a locked bus cycle to update the pte (if we are not using
2192 * the pmap_inval_*() API that is)... it's ok to do this for simple
2196 atomic_set_long(pte, VPTE_WIRED);
2198 atomic_clear_long(pte, VPTE_WIRED);
2199 lwkt_reltoken(&vm_token);
2203 * Copy the range specified by src_addr/len
2204 * from the source map to the range dst_addr/len
2205 * in the destination map.
2207 * This routine is only advisory and need not do anything.
2210 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2211 vm_size_t len, vm_offset_t src_addr)
2214 vm_offset_t end_addr = src_addr + len;
2221 * XXX BUGGY. Amoung other things srcmpte is assumed to remain
2222 * valid through blocking calls, and that's just not going to
2229 if (dst_addr != src_addr)
2231 if (dst_pmap->pm_pdir == NULL)
2233 if (src_pmap->pm_pdir == NULL)
2238 src_frame = get_ptbase1(src_pmap, src_addr);
2239 dst_frame = get_ptbase2(dst_pmap, src_addr);
2242 * critical section protection is required to maintain the page/object
2243 * association, interrupts can free pages and remove them from
2246 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
2247 vpte_t *src_pte, *dst_pte;
2248 vm_page_t dstmpte, srcmpte;
2249 vm_offset_t srcptepaddr;
2252 if (addr >= VM_MAX_USER_ADDRESS)
2253 panic("pmap_copy: invalid to pmap_copy page tables\n");
2256 * Don't let optional prefaulting of pages make us go
2257 * way below the low water mark of free pages or way
2258 * above high water mark of used pv entries.
2260 if (vmstats.v_free_count < vmstats.v_free_reserved ||
2261 pv_entry_count > pv_entry_high_water)
2264 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
2265 ptepindex = addr >> PDRSHIFT;
2267 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex];
2268 if (srcptepaddr == 0)
2271 if (srcptepaddr & VPTE_PS) {
2272 if (dst_pmap->pm_pdir[ptepindex] == 0) {
2273 dst_pmap->pm_pdir[ptepindex] = (vpte_t)srcptepaddr;
2274 dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
2279 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
2280 if ((srcmpte == NULL) || (srcmpte->hold_count == 0) ||
2281 (srcmpte->flags & PG_BUSY)) {
2285 if (pdnxt > end_addr)
2288 src_pte = src_frame + ((addr - src_addr) >> PAGE_SHIFT);
2289 dst_pte = dst_frame + ((addr - src_addr) >> PAGE_SHIFT);
2290 while (addr < pdnxt) {
2295 * we only virtual copy managed pages
2297 if ((ptetemp & VPTE_MANAGED) != 0) {
2299 * We have to check after allocpte for the
2300 * pte still being around... allocpte can
2303 * pmap_allocpte can block, unfortunately
2304 * we have to reload the tables.
2306 dstmpte = pmap_allocpte(dst_pmap, addr);
2307 src_frame = get_ptbase1(src_pmap, src_addr);
2308 dst_frame = get_ptbase2(dst_pmap, src_addr);
2310 if ((*dst_pte == 0) && (ptetemp = *src_pte) &&
2311 (ptetemp & VPTE_MANAGED) != 0) {
2313 * Clear the modified and accessed
2314 * (referenced) bits during the copy.
2316 * We do not have to clear the write
2317 * bit to force a fault-on-modify
2318 * because the real kernel's target
2319 * pmap is empty and will fault anyway.
2321 m = PHYS_TO_VM_PAGE(ptetemp);
2322 *dst_pte = ptetemp & ~(VPTE_M | VPTE_A);
2323 ++dst_pmap->pm_stats.resident_count;
2324 pmap_insert_entry(dst_pmap, addr,
2326 KKASSERT(m->flags & PG_MAPPED);
2328 pmap_unwire_pte_hold(dst_pmap, dstmpte);
2330 if (dstmpte->hold_count >= srcmpte->hold_count)
2344 * Zero the specified PA by mapping the page into KVM and clearing its
2347 * This function may be called from an interrupt and no locking is
2351 pmap_zero_page(vm_paddr_t phys)
2353 struct mdglobaldata *gd = mdcpu;
2357 panic("pmap_zero_page: CMAP3 busy");
2358 *gd->gd_CMAP3 = VPTE_V | VPTE_R | VPTE_W | (phys & VPTE_FRAME) | VPTE_A | VPTE_M;
2359 madvise(gd->gd_CADDR3, PAGE_SIZE, MADV_INVAL);
2361 bzero(gd->gd_CADDR3, PAGE_SIZE);
2367 * pmap_page_assertzero:
2369 * Assert that a page is empty, panic if it isn't.
2372 pmap_page_assertzero(vm_paddr_t phys)
2374 struct mdglobaldata *gd = mdcpu;
2379 panic("pmap_zero_page: CMAP3 busy");
2380 *gd->gd_CMAP3 = VPTE_V | VPTE_R | VPTE_W |
2381 (phys & VPTE_FRAME) | VPTE_A | VPTE_M;
2382 madvise(gd->gd_CADDR3, PAGE_SIZE, MADV_INVAL);
2383 for (i = 0; i < PAGE_SIZE; i += 4) {
2384 if (*(int *)((char *)gd->gd_CADDR3 + i) != 0) {
2385 panic("pmap_page_assertzero() @ %p not zero!\n",
2386 (void *)gd->gd_CADDR3);
2396 * Zero part of a physical page by mapping it into memory and clearing
2397 * its contents with bzero.
2399 * off and size may not cover an area beyond a single hardware page.
2402 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
2404 struct mdglobaldata *gd = mdcpu;
2408 panic("pmap_zero_page: CMAP3 busy");
2409 *gd->gd_CMAP3 = VPTE_V | VPTE_R | VPTE_W |
2410 (phys & VPTE_FRAME) | VPTE_A | VPTE_M;
2411 madvise(gd->gd_CADDR3, PAGE_SIZE, MADV_INVAL);
2413 bzero((char *)gd->gd_CADDR3 + off, size);
2421 * Copy the physical page from the source PA to the target PA.
2422 * This function may be called from an interrupt. No locking
2426 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
2428 struct mdglobaldata *gd = mdcpu;
2431 if (*(int *) gd->gd_CMAP1)
2432 panic("pmap_copy_page: CMAP1 busy");
2433 if (*(int *) gd->gd_CMAP2)
2434 panic("pmap_copy_page: CMAP2 busy");
2436 *(int *) gd->gd_CMAP1 = VPTE_V | VPTE_R | (src & PG_FRAME) | VPTE_A;
2437 *(int *) gd->gd_CMAP2 = VPTE_V | VPTE_R | VPTE_W | (dst & VPTE_FRAME) | VPTE_A | VPTE_M;
2439 madvise(gd->gd_CADDR1, PAGE_SIZE, MADV_INVAL);
2440 madvise(gd->gd_CADDR2, PAGE_SIZE, MADV_INVAL);
2442 bcopy(gd->gd_CADDR1, gd->gd_CADDR2, PAGE_SIZE);
2444 *(int *) gd->gd_CMAP1 = 0;
2445 *(int *) gd->gd_CMAP2 = 0;
2450 * pmap_copy_page_frag:
2452 * Copy the physical page from the source PA to the target PA.
2453 * This function may be called from an interrupt. No locking
2457 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
2459 struct mdglobaldata *gd = mdcpu;
2462 if (*(int *) gd->gd_CMAP1)
2463 panic("pmap_copy_page: CMAP1 busy");
2464 if (*(int *) gd->gd_CMAP2)
2465 panic("pmap_copy_page: CMAP2 busy");
2467 *(int *) gd->gd_CMAP1 = VPTE_V | (src & VPTE_FRAME) | VPTE_A;
2468 *(int *) gd->gd_CMAP2 = VPTE_V | VPTE_R | VPTE_W | (dst & VPTE_FRAME) | VPTE_A | VPTE_M;
2470 madvise(gd->gd_CADDR1, PAGE_SIZE, MADV_INVAL);
2471 madvise(gd->gd_CADDR2, PAGE_SIZE, MADV_INVAL);
2473 bcopy((char *)gd->gd_CADDR1 + (src & PAGE_MASK),
2474 (char *)gd->gd_CADDR2 + (dst & PAGE_MASK),
2477 *(int *) gd->gd_CMAP1 = 0;
2478 *(int *) gd->gd_CMAP2 = 0;
2483 * Returns true if the pmap's pv is one of the first
2484 * 16 pvs linked to from this page. This count may
2485 * be changed upwards or downwards in the future; it
2486 * is only necessary that true be returned for a small
2487 * subset of pmaps for proper page aging.
2492 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2497 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2501 lwkt_gettoken(&vm_token);
2503 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2504 if (pv->pv_pmap == pmap) {
2505 lwkt_reltoken(&vm_token);
2513 lwkt_reltoken(&vm_token);
2519 * Remove all pages from specified address space
2520 * this aids process exit speeds. Also, this code
2521 * is special cased for current process only, but
2522 * can have the more generic (and slightly slower)
2523 * mode enabled. This is much faster than pmap_remove
2524 * in the case of running down an entire address space.
2529 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2534 int32_t save_generation;
2537 lwkt_gettoken(&vm_token);
2538 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
2539 if (pv->pv_va >= eva || pv->pv_va < sva) {
2540 npv = TAILQ_NEXT(pv, pv_plist);
2544 KKASSERT(pmap == pv->pv_pmap);
2546 pte = pmap_pte(pmap, pv->pv_va);
2549 * We cannot remove wired pages from a process' mapping
2552 if (*pte & VPTE_WIRED) {
2553 npv = TAILQ_NEXT(pv, pv_plist);
2556 tpte = pmap_inval_loadandclear(pte, pmap, pv->pv_va);
2558 m = PHYS_TO_VM_PAGE(tpte);
2560 KASSERT(m < &vm_page_array[vm_page_array_size],
2561 ("pmap_remove_pages: bad tpte %lx", tpte));
2563 KKASSERT(pmap->pm_stats.resident_count > 0);
2564 --pmap->pm_stats.resident_count;
2567 * Update the vm_page_t clean and reference bits.
2569 if (tpte & VPTE_M) {
2573 npv = TAILQ_NEXT(pv, pv_plist);
2574 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
2575 save_generation = ++pmap->pm_generation;
2577 m->md.pv_list_count--;
2578 m->object->agg_pv_list_count--;
2579 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2580 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2581 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2583 pmap_unuse_pt(pmap, pv->pv_va, pv->pv_ptem);
2587 * Restart the scan if we blocked during the unuse or free
2588 * calls and other removals were made.
2590 if (save_generation != pmap->pm_generation) {
2591 kprintf("Warning: pmap_remove_pages race-A avoided\n");
2592 npv = TAILQ_FIRST(&pmap->pm_pvlist);
2595 lwkt_reltoken(&vm_token);
2600 * pmap_testbit tests bits in active mappings of a VM page.
2602 * The caller must hold vm_token
2605 pmap_testbit(vm_page_t m, int bit)
2610 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2613 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2618 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2620 * if the bit being tested is the modified bit, then
2621 * mark clean_map and ptes as never
2624 if (bit & (VPTE_A|VPTE_M)) {
2625 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2629 #if defined(PMAP_DIAGNOSTIC)
2631 kprintf("Null pmap (tb) at va: 0x%x\n", pv->pv_va);
2635 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2646 * This routine is used to clear bits in ptes. Certain bits require special
2647 * handling, in particular (on virtual kernels) the VPTE_M (modify) bit.
2649 * This routine is only called with certain VPTE_* bit combinations.
2651 * The caller must hold vm_token
2653 static __inline void
2654 pmap_clearbit(vm_page_t m, int bit)
2660 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2666 * Loop over all current mappings setting/clearing as appropos If
2667 * setting RO do we need to clear the VAC?
2669 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2671 * don't write protect pager mappings
2673 if (bit == VPTE_W) {
2674 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2678 #if defined(PMAP_DIAGNOSTIC)
2680 kprintf("Null pmap (cb) at va: 0x%x\n", pv->pv_va);
2686 * Careful here. We can use a locked bus instruction to
2687 * clear VPTE_A or VPTE_M safely but we need to synchronize
2688 * with the target cpus when we mess with VPTE_W.
2690 * On virtual kernels we must force a new fault-on-write
2691 * in the real kernel if we clear the Modify bit ourselves,
2692 * otherwise the real kernel will not get a new fault and
2693 * will never set our Modify bit again.
2695 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2697 if (bit == VPTE_W) {
2699 * We must also clear VPTE_M when clearing
2702 pbits = pmap_clean_pte(pte, pv->pv_pmap,
2706 } else if (bit == VPTE_M) {
2708 * We do not have to make the page read-only
2709 * when clearing the Modify bit. The real
2710 * kernel will make the real PTE read-only
2711 * or otherwise detect the write and set
2712 * our VPTE_M again simply by us invalidating
2713 * the real kernel VA for the pmap (as we did
2714 * above). This allows the real kernel to
2715 * handle the write fault without forwarding
2718 atomic_clear_long(pte, VPTE_M);
2719 } else if ((bit & (VPTE_W|VPTE_M)) == (VPTE_W|VPTE_M)) {
2721 * We've been asked to clear W & M, I guess
2722 * the caller doesn't want us to update
2723 * the dirty status of the VM page.
2725 pmap_clean_pte(pte, pv->pv_pmap, pv->pv_va);
2728 * We've been asked to clear bits that do
2729 * not interact with hardware.
2731 atomic_clear_long(pte, bit);
2739 * Lower the permission for all mappings to a given page.
2744 pmap_page_protect(vm_page_t m, vm_prot_t prot)
2746 if ((prot & VM_PROT_WRITE) == 0) {
2747 lwkt_gettoken(&vm_token);
2748 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
2749 pmap_clearbit(m, VPTE_W);
2750 vm_page_flag_clear(m, PG_WRITEABLE);
2754 lwkt_reltoken(&vm_token);
2759 pmap_phys_address(vm_pindex_t ppn)
2761 return (i386_ptob(ppn));
2765 * Return a count of reference bits for a page, clearing those bits.
2766 * It is not necessary for every reference bit to be cleared, but it
2767 * is necessary that 0 only be returned when there are truly no
2768 * reference bits set.
2770 * XXX: The exact number of bits to check and clear is a matter that
2771 * should be tested and standardized at some point in the future for
2772 * optimal aging of shared pages.
2777 pmap_ts_referenced(vm_page_t m)
2779 pv_entry_t pv, pvf, pvn;
2783 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2787 lwkt_gettoken(&vm_token);
2789 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2794 pvn = TAILQ_NEXT(pv, pv_list);
2796 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2798 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2800 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2803 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2805 if (pte && (*pte & VPTE_A)) {
2807 atomic_clear_long(pte, VPTE_A);
2809 atomic_clear_long_nonlocked(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 * Perform the pmap work for mincore
2946 pmap_mincore(pmap_t pmap, vm_offset_t addr)
2952 lwkt_gettoken(&vm_token);
2954 ptep = pmap_pte(pmap, addr);
2956 lwkt_reltoken(&vm_token);
2960 if ((pte = *ptep) != 0) {
2963 val = MINCORE_INCORE;
2964 if ((pte & VPTE_MANAGED) == 0)
2967 pa = pte & VPTE_FRAME;
2969 m = PHYS_TO_VM_PAGE(pa);
2975 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
2977 * Modified by someone
2979 else if (m->dirty || pmap_is_modified(m))
2980 val |= MINCORE_MODIFIED_OTHER;
2985 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
2988 * Referenced by someone
2990 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
2991 val |= MINCORE_REFERENCED_OTHER;
2992 vm_page_flag_set(m, PG_REFERENCED);
2996 lwkt_reltoken(&vm_token);
3001 pmap_replacevm(struct proc *p, struct vmspace *newvm, int adjrefs)
3003 struct vmspace *oldvm;
3006 oldvm = p->p_vmspace;
3008 if (oldvm != newvm) {
3009 p->p_vmspace = newvm;
3010 KKASSERT(p->p_nthreads == 1);
3011 lp = RB_ROOT(&p->p_lwp_tree);
3012 pmap_setlwpvm(lp, newvm);
3014 sysref_get(&newvm->vm_sysref);
3015 sysref_put(&oldvm->vm_sysref);
3022 pmap_setlwpvm(struct lwp *lp, struct vmspace *newvm)
3024 struct vmspace *oldvm;
3028 oldvm = lp->lwp_vmspace;
3030 if (oldvm != newvm) {
3031 lp->lwp_vmspace = newvm;
3032 if (curthread->td_lwp == lp) {
3033 pmap = vmspace_pmap(newvm);
3035 atomic_set_cpumask(&pmap->pm_active, mycpu->gd_cpumask);
3037 pmap->pm_active |= 1;
3039 #if defined(SWTCH_OPTIM_STATS)
3042 pmap = vmspace_pmap(oldvm);
3044 atomic_clear_cpumask(&pmap->pm_active, mycpu->gd_cpumask);
3046 pmap->pm_active &= ~(cpumask_t)1;
3055 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3058 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3062 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3067 * Used by kmalloc/kfree, page already exists at va
3070 pmap_kvtom(vm_offset_t va)
3074 KKASSERT(va >= KvaStart && va < KvaEnd);
3075 ptep = KernelPTA + (va >> PAGE_SHIFT);
3076 return(PHYS_TO_VM_PAGE(*ptep & PG_FRAME));