kernel - Fix some rare pmap races in i386 and x86_64.
[dragonfly.git] / sys / platform / vkernel / platform / pmap.c
blob0c8594f7e794ad3983d35b71e307e24fe2a64818
1 /*
2 * Copyright (c) 2006 The DragonFly Project. All rights reserved.
3 * Copyright (c) 1991 Regents of the University of California.
4 * All rights reserved.
5 * Copyright (c) 1994 John S. Dyson
6 * All rights reserved.
7 * Copyright (c) 1994 David Greenman
8 * All rights reserved.
9 * Copyright (c) 2004-2006 Matthew Dillon
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in
20 * the documentation and/or other materials provided with the
21 * distribution.
22 * 3. Neither the name of The DragonFly Project nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific, prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
29 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
30 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
32 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
34 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
36 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
39 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91
40 * $FreeBSD: src/sys/i386/i386/pmap.c,v 1.250.2.18 2002/03/06 22:48:53 silby Exp $
41 * $DragonFly: src/sys/platform/vkernel/platform/pmap.c,v 1.31 2008/08/25 17:01:40 dillon Exp $
44 * NOTE: PMAP_INVAL_ADD: In pc32 this function is called prior to adjusting
45 * the PTE in the page table, because a cpu synchronization might be required.
46 * The actual invalidation is delayed until the following call or flush. In
47 * the VKERNEL build this function is called prior to adjusting the PTE and
48 * invalidates the table synchronously (not delayed), and is not SMP safe
49 * as a consequence.
52 #include <sys/types.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/stat.h>
56 #include <sys/mman.h>
57 #include <sys/vkernel.h>
58 #include <sys/proc.h>
59 #include <sys/thread.h>
60 #include <sys/user.h>
61 #include <sys/vmspace.h>
63 #include <vm/pmap.h>
64 #include <vm/vm_page.h>
65 #include <vm/vm_extern.h>
66 #include <vm/vm_kern.h>
67 #include <vm/vm_object.h>
68 #include <vm/vm_zone.h>
69 #include <vm/vm_pageout.h>
71 #include <machine/md_var.h>
72 #include <machine/pcb.h>
73 #include <machine/pmap_inval.h>
74 #include <machine/globaldata.h>
76 #include <sys/sysref2.h>
78 #include <assert.h>
80 struct pmap kernel_pmap;
82 static struct vm_zone pvzone;
83 static struct vm_object pvzone_obj;
84 static TAILQ_HEAD(,pmap) pmap_list = TAILQ_HEAD_INITIALIZER(pmap_list);
85 static int pv_entry_count;
86 static int pv_entry_max;
87 static int pv_entry_high_water;
88 static int pmap_pagedaemon_waken;
89 static boolean_t pmap_initialized = FALSE;
90 static int protection_codes[8];
92 static void i386_protection_init(void);
93 static void pmap_remove_all(vm_page_t m);
94 static int pmap_release_free_page(struct pmap *pmap, vm_page_t p);
96 #define MINPV 2048
97 #ifndef PMAP_SHPGPERPROC
98 #define PMAP_SHPGPERPROC 200
99 #endif
101 #define pmap_pde(m, v) (&((m)->pm_pdir[(vm_offset_t)(v) >> PDRSHIFT]))
103 #define pte_prot(m, p) \
104 (protection_codes[p & (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)])
106 void
107 pmap_init(void)
109 int i;
110 struct pv_entry *pvinit;
112 for (i = 0; i < vm_page_array_size; i++) {
113 vm_page_t m;
115 m = &vm_page_array[i];
116 TAILQ_INIT(&m->md.pv_list);
117 m->md.pv_list_count = 0;
120 i = vm_page_array_size;
121 if (i < MINPV)
122 i = MINPV;
123 pvinit = (struct pv_entry *)kmem_alloc(&kernel_map, i*sizeof(*pvinit));
124 zbootinit(&pvzone, "PV ENTRY", sizeof(*pvinit), pvinit, i);
125 pmap_initialized = TRUE;
128 void
129 pmap_init2(void)
131 int shpgperproc = PMAP_SHPGPERPROC;
133 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
134 pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
135 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
136 pv_entry_high_water = 9 * (pv_entry_max / 10);
137 zinitna(&pvzone, &pvzone_obj, NULL, 0, pv_entry_max, ZONE_INTERRUPT, 1);
141 * Bootstrap the kernel_pmap so it can be used with pmap_enter().
143 * NOTE! pm_pdir for the kernel pmap is offset so VA's translate
144 * directly into PTD indexes (PTA is also offset for the same reason).
145 * This is necessary because, for now, KVA is not mapped at address 0.
147 * Page table pages are not managed like they are in normal pmaps, so
148 * no pteobj is needed.
150 void
151 pmap_bootstrap(void)
153 vm_pindex_t i = (vm_offset_t)KernelPTD >> PAGE_SHIFT;
155 kernel_pmap.pm_pdir = KernelPTD - (KvaStart >> SEG_SHIFT);
156 kernel_pmap.pm_pdirpte = KernelPTA[i];
157 kernel_pmap.pm_count = 1;
158 kernel_pmap.pm_active = (cpumask_t)-1 & ~CPUMASK_LOCK;
159 TAILQ_INIT(&kernel_pmap.pm_pvlist);
160 i386_protection_init();
164 * Initialize pmap0/vmspace0 . Since process 0 never enters user mode we
165 * just dummy it up so it works well enough for fork().
167 * In DragonFly, process pmaps may only be used to manipulate user address
168 * space, never kernel address space.
170 void
171 pmap_pinit0(struct pmap *pmap)
173 pmap_pinit(pmap);
176 /************************************************************************
177 * Procedures to manage whole physical maps *
178 ************************************************************************
180 * Initialize a preallocated and zeroed pmap structure,
181 * such as one in a vmspace structure.
183 void
184 pmap_pinit(struct pmap *pmap)
186 vm_page_t ptdpg;
187 int npages;
190 * No need to allocate page table space yet but we do need a valid
191 * page directory table.
193 if (pmap->pm_pdir == NULL) {
194 pmap->pm_pdir =
195 (pd_entry_t *)kmem_alloc_pageable(&kernel_map, PAGE_SIZE);
199 * allocate object for the pte array and page directory
201 npages = VPTE_PAGETABLE_SIZE +
202 (VM_MAX_USER_ADDRESS / PAGE_SIZE) * sizeof(vpte_t);
203 npages = (npages + PAGE_MASK) / PAGE_SIZE;
205 if (pmap->pm_pteobj == NULL)
206 pmap->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, npages);
207 pmap->pm_pdindex = npages - 1;
210 * allocate the page directory page
212 ptdpg = vm_page_grab(pmap->pm_pteobj, pmap->pm_pdindex,
213 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
215 ptdpg->wire_count = 1;
216 ++vmstats.v_wire_count;
218 /* not usually mapped */
219 vm_page_flag_clear(ptdpg, PG_MAPPED | PG_BUSY);
220 ptdpg->valid = VM_PAGE_BITS_ALL;
222 pmap_kenter((vm_offset_t)pmap->pm_pdir, VM_PAGE_TO_PHYS(ptdpg));
223 pmap->pm_pdirpte = KernelPTA[(vm_offset_t)pmap->pm_pdir >> PAGE_SHIFT];
224 if ((ptdpg->flags & PG_ZERO) == 0)
225 bzero(pmap->pm_pdir, PAGE_SIZE);
227 pmap->pm_count = 1;
228 pmap->pm_active = 0;
229 pmap->pm_ptphint = NULL;
230 pmap->pm_cpucachemask = 0;
231 TAILQ_INIT(&pmap->pm_pvlist);
232 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
233 pmap->pm_stats.resident_count = 1;
237 * Clean up a pmap structure so it can be physically freed
239 void
240 pmap_puninit(pmap_t pmap)
242 if (pmap->pm_pdir) {
243 kmem_free(&kernel_map, (vm_offset_t)pmap->pm_pdir, PAGE_SIZE);
244 pmap->pm_pdir = NULL;
246 if (pmap->pm_pteobj) {
247 vm_object_deallocate(pmap->pm_pteobj);
248 pmap->pm_pteobj = NULL;
254 * Wire in kernel global address entries. To avoid a race condition
255 * between pmap initialization and pmap_growkernel, this procedure
256 * adds the pmap to the master list (which growkernel scans to update),
257 * then copies the template.
259 * In a virtual kernel there are no kernel global address entries.
261 void
262 pmap_pinit2(struct pmap *pmap)
264 crit_enter();
265 TAILQ_INSERT_TAIL(&pmap_list, pmap, pm_pmnode);
266 crit_exit();
270 * Release all resources held by the given physical map.
272 * Should only be called if the map contains no valid mappings.
274 static int pmap_release_callback(struct vm_page *p, void *data);
276 void
277 pmap_release(struct pmap *pmap)
279 struct mdglobaldata *gd = mdcpu;
280 vm_object_t object = pmap->pm_pteobj;
281 struct rb_vm_page_scan_info info;
283 KKASSERT(pmap != &kernel_pmap);
285 #if defined(DIAGNOSTIC)
286 if (object->ref_count != 1)
287 panic("pmap_release: pteobj reference count != 1");
288 #endif
290 * Once we destroy the page table, the mapping becomes invalid.
291 * Don't waste time doing a madvise to invalidate the mapping, just
292 * set cpucachemask to 0.
294 if (pmap->pm_pdir == gd->gd_PT1pdir) {
295 gd->gd_PT1pdir = NULL;
296 *gd->gd_PT1pde = 0;
297 /* madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL); */
299 if (pmap->pm_pdir == gd->gd_PT2pdir) {
300 gd->gd_PT2pdir = NULL;
301 *gd->gd_PT2pde = 0;
302 /* madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL); */
304 if (pmap->pm_pdir == gd->gd_PT3pdir) {
305 gd->gd_PT3pdir = NULL;
306 *gd->gd_PT3pde = 0;
307 /* madvise(gd->gd_PT3map, SEG_SIZE, MADV_INVAL); */
310 info.pmap = pmap;
311 info.object = object;
312 crit_enter();
313 TAILQ_REMOVE(&pmap_list, pmap, pm_pmnode);
314 crit_exit();
316 do {
317 crit_enter();
318 info.error = 0;
319 info.mpte = NULL;
320 info.limit = object->generation;
322 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL,
323 pmap_release_callback, &info);
324 if (info.error == 0 && info.mpte) {
325 if (!pmap_release_free_page(pmap, info.mpte))
326 info.error = 1;
328 crit_exit();
329 } while (info.error);
332 * Leave the KVA reservation for pm_pdir cached for later reuse.
334 pmap->pm_pdirpte = 0;
335 pmap->pm_cpucachemask = 0;
339 * Callback to release a page table page backing a directory
340 * entry.
342 static int
343 pmap_release_callback(struct vm_page *p, void *data)
345 struct rb_vm_page_scan_info *info = data;
347 if (p->pindex == info->pmap->pm_pdindex) {
348 info->mpte = p;
349 return(0);
351 if (!pmap_release_free_page(info->pmap, p)) {
352 info->error = 1;
353 return(-1);
355 if (info->object->generation != info->limit) {
356 info->error = 1;
357 return(-1);
359 return(0);
363 * Retire the given physical map from service. Should only be called if
364 * the map contains no valid mappings.
366 void
367 pmap_destroy(pmap_t pmap)
369 int count;
371 if (pmap == NULL)
372 return;
374 count = --pmap->pm_count;
375 if (count == 0) {
376 pmap_release(pmap);
377 panic("destroying a pmap is not yet implemented");
382 * Add a reference to the specified pmap.
384 void
385 pmap_reference(pmap_t pmap)
387 if (pmap != NULL) {
388 pmap->pm_count++;
392 /************************************************************************
393 * VMSPACE MANAGEMENT *
394 ************************************************************************
396 * The VMSPACE management we do in our virtual kernel must be reflected
397 * in the real kernel. This is accomplished by making vmspace system
398 * calls to the real kernel.
400 void
401 cpu_vmspace_alloc(struct vmspace *vm)
403 int r;
404 void *rp;
406 #define LAST_EXTENT (VM_MAX_USER_ADDRESS - 0x80000000)
408 if (vmspace_create(&vm->vm_pmap, 0, NULL) < 0)
409 panic("vmspace_create() failed");
411 rp = vmspace_mmap(&vm->vm_pmap, (void *)0x00000000, 0x40000000,
412 PROT_READ|PROT_WRITE,
413 MAP_FILE|MAP_SHARED|MAP_VPAGETABLE|MAP_FIXED,
414 MemImageFd, 0);
415 if (rp == MAP_FAILED)
416 panic("vmspace_mmap: failed1");
417 vmspace_mcontrol(&vm->vm_pmap, (void *)0x00000000, 0x40000000,
418 MADV_NOSYNC, 0);
419 rp = vmspace_mmap(&vm->vm_pmap, (void *)0x40000000, 0x40000000,
420 PROT_READ|PROT_WRITE,
421 MAP_FILE|MAP_SHARED|MAP_VPAGETABLE|MAP_FIXED,
422 MemImageFd, 0x40000000);
423 if (rp == MAP_FAILED)
424 panic("vmspace_mmap: failed2");
425 vmspace_mcontrol(&vm->vm_pmap, (void *)0x40000000, 0x40000000,
426 MADV_NOSYNC, 0);
427 rp = vmspace_mmap(&vm->vm_pmap, (void *)0x80000000, LAST_EXTENT,
428 PROT_READ|PROT_WRITE,
429 MAP_FILE|MAP_SHARED|MAP_VPAGETABLE|MAP_FIXED,
430 MemImageFd, 0x80000000);
431 vmspace_mcontrol(&vm->vm_pmap, (void *)0x80000000, LAST_EXTENT,
432 MADV_NOSYNC, 0);
433 if (rp == MAP_FAILED)
434 panic("vmspace_mmap: failed3");
436 r = vmspace_mcontrol(&vm->vm_pmap, (void *)0x00000000, 0x40000000,
437 MADV_SETMAP, vmspace_pmap(vm)->pm_pdirpte);
438 if (r < 0)
439 panic("vmspace_mcontrol: failed1");
440 r = vmspace_mcontrol(&vm->vm_pmap, (void *)0x40000000, 0x40000000,
441 MADV_SETMAP, vmspace_pmap(vm)->pm_pdirpte);
442 if (r < 0)
443 panic("vmspace_mcontrol: failed2");
444 r = vmspace_mcontrol(&vm->vm_pmap, (void *)0x80000000, LAST_EXTENT,
445 MADV_SETMAP, vmspace_pmap(vm)->pm_pdirpte);
446 if (r < 0)
447 panic("vmspace_mcontrol: failed3");
450 void
451 cpu_vmspace_free(struct vmspace *vm)
453 if (vmspace_destroy(&vm->vm_pmap) < 0)
454 panic("vmspace_destroy() failed");
457 /************************************************************************
458 * Procedures which operate directly on the kernel PMAP *
459 ************************************************************************/
462 * This maps the requested page table and gives us access to it.
464 * This routine can be called from a potentially preempting interrupt
465 * thread or from a normal thread.
467 static vpte_t *
468 get_ptbase(struct pmap *pmap, vm_offset_t va)
470 struct mdglobaldata *gd = mdcpu;
472 if (pmap == &kernel_pmap) {
473 KKASSERT(va >= KvaStart && va < KvaEnd);
474 return(KernelPTA + (va >> PAGE_SHIFT));
475 } else if (pmap->pm_pdir == gd->gd_PT1pdir) {
476 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
477 *gd->gd_PT1pde = pmap->pm_pdirpte;
478 madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL);
479 atomic_set_int(&pmap->pm_cpucachemask, gd->mi.gd_cpumask);
481 return(gd->gd_PT1map + (va >> PAGE_SHIFT));
482 } else if (pmap->pm_pdir == gd->gd_PT2pdir) {
483 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
484 *gd->gd_PT2pde = pmap->pm_pdirpte;
485 madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL);
486 atomic_set_int(&pmap->pm_cpucachemask, gd->mi.gd_cpumask);
488 return(gd->gd_PT2map + (va >> PAGE_SHIFT));
492 * If we aren't running from a potentially preempting interrupt,
493 * load a new page table directory into the page table cache
495 if (gd->mi.gd_intr_nesting_level == 0 &&
496 (gd->mi.gd_curthread->td_flags & TDF_INTTHREAD) == 0) {
498 * Choose one or the other and map the page table
499 * in the KVA space reserved for it.
501 if ((gd->gd_PTflip = 1 - gd->gd_PTflip) == 0) {
502 gd->gd_PT1pdir = pmap->pm_pdir;
503 *gd->gd_PT1pde = pmap->pm_pdirpte;
504 madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL);
505 atomic_set_int(&pmap->pm_cpucachemask,
506 gd->mi.gd_cpumask);
507 return(gd->gd_PT1map + (va >> PAGE_SHIFT));
508 } else {
509 gd->gd_PT2pdir = pmap->pm_pdir;
510 *gd->gd_PT2pde = pmap->pm_pdirpte;
511 madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL);
512 atomic_set_int(&pmap->pm_cpucachemask,
513 gd->mi.gd_cpumask);
514 return(gd->gd_PT2map + (va >> PAGE_SHIFT));
519 * If we are running from a preempting interrupt use a private
520 * map. The caller must be in a critical section.
522 KKASSERT(IN_CRITICAL_SECT(curthread));
523 if (pmap->pm_pdir == gd->gd_PT3pdir) {
524 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
525 *gd->gd_PT3pde = pmap->pm_pdirpte;
526 madvise(gd->gd_PT3map, SEG_SIZE, MADV_INVAL);
527 atomic_set_int(&pmap->pm_cpucachemask,
528 gd->mi.gd_cpumask);
530 } else {
531 gd->gd_PT3pdir = pmap->pm_pdir;
532 *gd->gd_PT3pde = pmap->pm_pdirpte;
533 madvise(gd->gd_PT3map, SEG_SIZE, MADV_INVAL);
534 atomic_set_int(&pmap->pm_cpucachemask,
535 gd->mi.gd_cpumask);
537 return(gd->gd_PT3map + (va >> PAGE_SHIFT));
540 static vpte_t *
541 get_ptbase1(struct pmap *pmap, vm_offset_t va)
543 struct mdglobaldata *gd = mdcpu;
545 if (pmap == &kernel_pmap) {
546 KKASSERT(va >= KvaStart && va < KvaEnd);
547 return(KernelPTA + (va >> PAGE_SHIFT));
548 } else if (pmap->pm_pdir == gd->gd_PT1pdir) {
549 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
550 *gd->gd_PT1pde = pmap->pm_pdirpte;
551 madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL);
552 atomic_set_int(&pmap->pm_cpucachemask, gd->mi.gd_cpumask);
554 return(gd->gd_PT1map + (va >> PAGE_SHIFT));
556 KKASSERT(gd->mi.gd_intr_nesting_level == 0 &&
557 (gd->mi.gd_curthread->td_flags & TDF_INTTHREAD) == 0);
558 gd->gd_PT1pdir = pmap->pm_pdir;
559 *gd->gd_PT1pde = pmap->pm_pdirpte;
560 madvise(gd->gd_PT1map, SEG_SIZE, MADV_INVAL);
561 return(gd->gd_PT1map + (va >> PAGE_SHIFT));
564 static vpte_t *
565 get_ptbase2(struct pmap *pmap, vm_offset_t va)
567 struct mdglobaldata *gd = mdcpu;
569 if (pmap == &kernel_pmap) {
570 KKASSERT(va >= KvaStart && va < KvaEnd);
571 return(KernelPTA + (va >> PAGE_SHIFT));
572 } else if (pmap->pm_pdir == gd->gd_PT2pdir) {
573 if ((pmap->pm_cpucachemask & gd->mi.gd_cpumask) == 0) {
574 *gd->gd_PT2pde = pmap->pm_pdirpte;
575 madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL);
576 atomic_set_int(&pmap->pm_cpucachemask, gd->mi.gd_cpumask);
578 return(gd->gd_PT2map + (va >> PAGE_SHIFT));
580 KKASSERT(gd->mi.gd_intr_nesting_level == 0 &&
581 (gd->mi.gd_curthread->td_flags & TDF_INTTHREAD) == 0);
582 gd->gd_PT2pdir = pmap->pm_pdir;
583 *gd->gd_PT2pde = pmap->pm_pdirpte;
584 madvise(gd->gd_PT2map, SEG_SIZE, MADV_INVAL);
585 return(gd->gd_PT2map + (va >> PAGE_SHIFT));
589 * Return a pointer to the page table entry for the specified va in the
590 * specified pmap. NULL is returned if there is no valid page table page
591 * for the VA.
593 static __inline vpte_t *
594 pmap_pte(struct pmap *pmap, vm_offset_t va)
596 vpte_t *ptep;
598 ptep = &pmap->pm_pdir[va >> SEG_SHIFT];
599 if (*ptep & VPTE_PS)
600 return(ptep);
601 if (*ptep)
602 return (get_ptbase(pmap, va));
603 return(NULL);
608 * Enter a mapping into kernel_pmap. Mappings created in this fashion
609 * are not managed. Mappings must be immediately accessible on all cpus.
611 * Call pmap_inval_pte() to invalidate the virtual pte and clean out the
612 * real pmap and handle related races before storing the new vpte.
614 void
615 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
617 vpte_t *ptep;
618 vpte_t npte;
620 KKASSERT(va >= KvaStart && va < KvaEnd);
621 npte = (vpte_t)pa | VPTE_R | VPTE_W | VPTE_V;
622 ptep = KernelPTA + (va >> PAGE_SHIFT);
623 if (*ptep & VPTE_V)
624 pmap_inval_pte(ptep, &kernel_pmap, va);
625 *ptep = npte;
629 * Synchronize a kvm mapping originally made for the private use on
630 * some other cpu so it can be used on all cpus.
632 * XXX add MADV_RESYNC to improve performance.
634 void
635 pmap_kenter_sync(vm_offset_t va)
637 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
641 * Synchronize a kvm mapping originally made for the private use on
642 * some other cpu so it can be used on our cpu. Turns out to be the
643 * same madvise() call, because we have to sync the real pmaps anyway.
645 * XXX add MADV_RESYNC to improve performance.
647 void
648 pmap_kenter_sync_quick(vm_offset_t va)
650 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
653 #if 0
655 * Make a previously read-only kernel mapping R+W (not implemented by
656 * virtual kernels).
658 void
659 pmap_kmodify_rw(vm_offset_t va)
661 *pmap_kpte(va) |= VPTE_R | VPTE_W;
662 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
666 * Make a kernel mapping non-cacheable (not applicable to virtual kernels)
668 void
669 pmap_kmodify_nc(vm_offset_t va)
671 *pmap_kpte(va) |= VPTE_N;
672 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
675 #endif
678 * Map a contiguous range of physical memory to a KVM
680 vm_offset_t
681 pmap_map(vm_offset_t *virtp, vm_paddr_t start, vm_paddr_t end, int prot)
683 vm_offset_t sva, virt;
685 sva = virt = *virtp;
686 while (start < end) {
687 pmap_kenter(virt, start);
688 virt += PAGE_SIZE;
689 start += PAGE_SIZE;
691 *virtp = virt;
692 return (sva);
695 vpte_t *
696 pmap_kpte(vm_offset_t va)
698 vpte_t *ptep;
700 KKASSERT(va >= KvaStart && va < KvaEnd);
701 ptep = KernelPTA + (va >> PAGE_SHIFT);
702 return(ptep);
706 * Enter an unmanaged KVA mapping for the private use of the current
707 * cpu only. pmap_kenter_sync() may be called to make the mapping usable
708 * by other cpus.
710 * It is illegal for the mapping to be accessed by other cpus unleess
711 * pmap_kenter_sync*() is called.
713 void
714 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
716 vpte_t *ptep;
717 vpte_t npte;
719 KKASSERT(va >= KvaStart && va < KvaEnd);
721 npte = (vpte_t)pa | VPTE_R | VPTE_W | VPTE_V;
722 ptep = KernelPTA + (va >> PAGE_SHIFT);
723 if (*ptep & VPTE_V)
724 pmap_inval_pte_quick(ptep, &kernel_pmap, va);
725 *ptep = npte;
729 * Make a temporary mapping for a physical address. This is only intended
730 * to be used for panic dumps.
732 void *
733 pmap_kenter_temporary(vm_paddr_t pa, int i)
735 pmap_kenter(crashdumpmap + (i * PAGE_SIZE), pa);
736 return ((void *)crashdumpmap);
740 * Remove an unmanaged mapping created with pmap_kenter*().
742 void
743 pmap_kremove(vm_offset_t va)
745 vpte_t *ptep;
747 KKASSERT(va >= KvaStart && va < KvaEnd);
749 ptep = KernelPTA + (va >> PAGE_SHIFT);
750 if (*ptep & VPTE_V)
751 pmap_inval_pte(ptep, &kernel_pmap, va);
752 *ptep = 0;
756 * Remove an unmanaged mapping created with pmap_kenter*() but synchronize
757 * only with this cpu.
759 * Unfortunately because we optimize new entries by testing VPTE_V later
760 * on, we actually still have to synchronize with all the cpus. XXX maybe
761 * store a junk value and test against 0 in the other places instead?
763 void
764 pmap_kremove_quick(vm_offset_t va)
766 vpte_t *ptep;
768 KKASSERT(va >= KvaStart && va < KvaEnd);
770 ptep = KernelPTA + (va >> PAGE_SHIFT);
771 if (*ptep & VPTE_V)
772 pmap_inval_pte(ptep, &kernel_pmap, va); /* NOT _quick */
773 *ptep = 0;
777 * Extract the physical address from the kernel_pmap that is associated
778 * with the specified virtual address.
780 vm_paddr_t
781 pmap_kextract(vm_offset_t va)
783 vpte_t *ptep;
784 vm_paddr_t pa;
786 KKASSERT(va >= KvaStart && va < KvaEnd);
788 ptep = KernelPTA + (va >> PAGE_SHIFT);
789 pa = (vm_paddr_t)(*ptep & VPTE_FRAME) | (va & PAGE_MASK);
790 return(pa);
794 * Map a set of unmanaged VM pages into KVM.
796 void
797 pmap_qenter(vm_offset_t va, struct vm_page **m, int count)
799 KKASSERT(va >= KvaStart && va + count * PAGE_SIZE < KvaEnd);
800 while (count) {
801 vpte_t *ptep;
803 ptep = KernelPTA + (va >> PAGE_SHIFT);
804 if (*ptep & VPTE_V)
805 pmap_inval_pte(ptep, &kernel_pmap, va);
806 *ptep = (vpte_t)(*m)->phys_addr | VPTE_R | VPTE_W | VPTE_V;
807 --count;
808 ++m;
809 va += PAGE_SIZE;
814 * Map a set of VM pages to kernel virtual memory. If a mapping changes
815 * clear the supplied mask. The caller handles any SMP interactions.
816 * The mask is used to provide the caller with hints on what SMP interactions
817 * might be needed.
819 void
820 pmap_qenter2(vm_offset_t va, struct vm_page **m, int count, cpumask_t *mask)
822 cpumask_t cmask = mycpu->gd_cpumask;
824 KKASSERT(va >= KvaStart && va + count * PAGE_SIZE < KvaEnd);
825 while (count) {
826 vpte_t *ptep;
827 vpte_t npte;
829 ptep = KernelPTA + (va >> PAGE_SHIFT);
830 npte = (vpte_t)(*m)->phys_addr | VPTE_R | VPTE_W | VPTE_V;
831 if (*ptep != npte) {
832 *mask = 0;
833 pmap_inval_pte_quick(ptep, &kernel_pmap, va);
834 *ptep = npte;
835 } else if ((*mask & cmask) == 0) {
836 pmap_kenter_sync_quick(va);
838 --count;
839 ++m;
840 va += PAGE_SIZE;
842 *mask |= cmask;
846 * Undo the effects of pmap_qenter*().
848 void
849 pmap_qremove(vm_offset_t va, int count)
851 KKASSERT(va >= KvaStart && va + count * PAGE_SIZE < KvaEnd);
852 while (count) {
853 vpte_t *ptep;
855 ptep = KernelPTA + (va >> PAGE_SHIFT);
856 if (*ptep & VPTE_V)
857 pmap_inval_pte(ptep, &kernel_pmap, va);
858 *ptep = 0;
859 --count;
860 va += PAGE_SIZE;
864 /************************************************************************
865 * Misc support glue called by machine independant code *
866 ************************************************************************
868 * These routines are called by machine independant code to operate on
869 * certain machine-dependant aspects of processes, threads, and pmaps.
873 * Initialize MD portions of the thread structure.
875 void
876 pmap_init_thread(thread_t td)
878 /* enforce pcb placement */
879 td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
880 td->td_savefpu = &td->td_pcb->pcb_save;
881 td->td_sp = (char *)td->td_pcb - 16;
885 * This routine directly affects the fork perf for a process.
887 void
888 pmap_init_proc(struct proc *p)
893 * Destroy the UPAGES for a process that has exited and disassociate
894 * the process from its thread.
896 void
897 pmap_dispose_proc(struct proc *p)
899 KASSERT(p->p_lock == 0, ("attempt to dispose referenced proc! %p", p));
903 * We pre-allocate all page table pages for kernel virtual memory so
904 * this routine will only be called if KVM has been exhausted.
906 void
907 pmap_growkernel(vm_offset_t addr)
909 addr = (addr + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
911 if (addr > virtual_end - SEG_SIZE)
912 panic("KVM exhausted");
913 kernel_vm_end = addr;
917 * The modification bit is not tracked for any pages in this range. XXX
918 * such pages in this maps should always use pmap_k*() functions and not
919 * be managed anyhow.
921 * XXX User and kernel address spaces are independant for virtual kernels,
922 * this function only applies to the kernel pmap.
924 static int
925 pmap_track_modified(pmap_t pmap, vm_offset_t va)
927 if (pmap != &kernel_pmap)
928 return 1;
929 if ((va < clean_sva) || (va >= clean_eva))
930 return 1;
931 else
932 return 0;
935 /************************************************************************
936 * Procedures supporting managed page table pages *
937 ************************************************************************
939 * These procedures are used to track managed page table pages. These pages
940 * use the page table page's vm_page_t to track PTEs in the page. The
941 * page table pages themselves are arranged in a VM object, pmap->pm_pteobj.
943 * This allows the system to throw away page table pages for user processes
944 * at will and reinstantiate them on demand.
948 * This routine works like vm_page_lookup() but also blocks as long as the
949 * page is busy. This routine does not busy the page it returns.
951 * Unless the caller is managing objects whos pages are in a known state,
952 * the call should be made with a critical section held so the page's object
953 * association remains valid on return.
955 static vm_page_t
956 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
958 vm_page_t m;
960 retry:
961 m = vm_page_lookup(object, pindex);
962 if (m && vm_page_sleep_busy(m, FALSE, "pplookp"))
963 goto retry;
964 return(m);
968 * This routine unholds page table pages, and if the hold count
969 * drops to zero, then it decrements the wire count.
971 * We must recheck that this is the last hold reference after busy-sleeping
972 * on the page.
974 static int
975 _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
977 while (vm_page_sleep_busy(m, FALSE, "pmuwpt"))
979 KASSERT(m->queue == PQ_NONE,
980 ("_pmap_unwire_pte_hold: %p->queue != PQ_NONE", m));
982 if (m->hold_count == 1) {
984 * Unmap the page table page.
986 vm_page_busy(m);
987 KKASSERT(pmap->pm_pdir[m->pindex] != 0);
988 pmap_inval_pde(&pmap->pm_pdir[m->pindex], pmap,
989 (vm_offset_t)m->pindex << SEG_SHIFT);
990 KKASSERT(pmap->pm_stats.resident_count > 0);
991 --pmap->pm_stats.resident_count;
993 if (pmap->pm_ptphint == m)
994 pmap->pm_ptphint = NULL;
997 * This was our last hold, the page had better be unwired
998 * after we decrement wire_count.
1000 * FUTURE NOTE: shared page directory page could result in
1001 * multiple wire counts.
1003 vm_page_unhold(m);
1004 --m->wire_count;
1005 KKASSERT(m->wire_count == 0);
1006 --vmstats.v_wire_count;
1007 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1008 vm_page_flash(m);
1009 vm_page_free_zero(m);
1010 return 1;
1012 KKASSERT(m->hold_count > 1);
1013 vm_page_unhold(m);
1014 return 0;
1017 static __inline int
1018 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
1020 KKASSERT(m->hold_count > 0);
1021 if (m->hold_count > 1) {
1022 vm_page_unhold(m);
1023 return 0;
1024 } else {
1025 return _pmap_unwire_pte_hold(pmap, m);
1030 * After removing a page table entry, this routine is used to
1031 * conditionally free the page, and manage the hold/wire counts.
1033 static int
1034 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte)
1036 unsigned ptepindex;
1038 if (mpte == NULL) {
1040 * page table pages in the kernel_pmap are not managed.
1042 if (pmap == &kernel_pmap)
1043 return(0);
1044 ptepindex = (va >> PDRSHIFT);
1045 if (pmap->pm_ptphint &&
1046 (pmap->pm_ptphint->pindex == ptepindex)) {
1047 mpte = pmap->pm_ptphint;
1048 } else {
1049 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1050 pmap->pm_ptphint = mpte;
1053 return pmap_unwire_pte_hold(pmap, mpte);
1057 * Attempt to release and free the vm_page backing a page directory page
1058 * in a pmap. Returns 1 on success, 0 on failure (if the procedure had
1059 * to sleep).
1061 static int
1062 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1064 vpte_t *pde = pmap->pm_pdir;
1067 * This code optimizes the case of freeing non-busy
1068 * page-table pages. Those pages are zero now, and
1069 * might as well be placed directly into the zero queue.
1071 if (vm_page_sleep_busy(p, FALSE, "pmaprl"))
1072 return 0;
1074 vm_page_busy(p);
1075 KKASSERT(pmap->pm_stats.resident_count > 0);
1076 --pmap->pm_stats.resident_count;
1078 if (p->hold_count) {
1079 panic("pmap_release: freeing held page table page");
1082 * Page directory pages need to have the kernel stuff cleared, so
1083 * they can go into the zero queue also.
1085 * In virtual kernels there is no 'kernel stuff'. For the moment
1086 * I just make sure the whole thing has been zero'd even though
1087 * it should already be completely zero'd.
1089 * pmaps for vkernels do not self-map because they do not share
1090 * their address space with the vkernel. Clearing of pde[] thus
1091 * only applies to page table pages and not to the page directory
1092 * page.
1094 if (p->pindex == pmap->pm_pdindex) {
1095 bzero(pde, VPTE_PAGETABLE_SIZE);
1096 pmap_kremove((vm_offset_t)pmap->pm_pdir);
1097 } else {
1098 KKASSERT(pde[p->pindex] != 0);
1099 pmap_inval_pde(&pde[p->pindex], pmap,
1100 (vm_offset_t)p->pindex << SEG_SHIFT);
1104 * Clear the matching hint
1106 if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1107 pmap->pm_ptphint = NULL;
1110 * And throw the page away. The page is completely zero'd out so
1111 * optimize the free call.
1113 p->wire_count--;
1114 vmstats.v_wire_count--;
1115 vm_page_free_zero(p);
1116 return 1;
1120 * This routine is called if the page table page is not mapped in the page
1121 * table directory.
1123 * The routine is broken up into two parts for readability.
1125 * It must return a held mpte and map the page directory page as required.
1126 * Because vm_page_grab() can block, we must re-check pm_pdir[ptepindex]
1128 static vm_page_t
1129 _pmap_allocpte(pmap_t pmap, unsigned ptepindex)
1131 vm_paddr_t ptepa;
1132 vm_page_t m;
1135 * Find or fabricate a new pagetable page. A busied page will be
1136 * returned. This call may block.
1138 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1139 VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1141 KASSERT(m->queue == PQ_NONE,
1142 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1145 * Increment the hold count for the page we will be returning to
1146 * the caller.
1148 m->hold_count++;
1151 * It is possible that someone else got in and mapped by the page
1152 * directory page while we were blocked, if so just unbusy and
1153 * return the held page.
1155 if ((ptepa = pmap->pm_pdir[ptepindex]) != 0) {
1156 KKASSERT((ptepa & VPTE_FRAME) == VM_PAGE_TO_PHYS(m));
1157 vm_page_wakeup(m);
1158 return(m);
1161 if (m->wire_count == 0)
1162 vmstats.v_wire_count++;
1163 m->wire_count++;
1166 * Map the pagetable page into the process address space, if
1167 * it isn't already there.
1169 ++pmap->pm_stats.resident_count;
1171 ptepa = VM_PAGE_TO_PHYS(m);
1172 pmap->pm_pdir[ptepindex] = (vpte_t)ptepa | VPTE_R | VPTE_W | VPTE_V |
1173 VPTE_A | VPTE_M;
1176 * We are likely about to access this page table page, so set the
1177 * page table hint to reduce overhead.
1179 pmap->pm_ptphint = m;
1182 * Try to use the new mapping, but if we cannot, then
1183 * do it with the routine that maps the page explicitly.
1185 if ((m->flags & PG_ZERO) == 0)
1186 pmap_zero_page(ptepa);
1188 m->valid = VM_PAGE_BITS_ALL;
1189 vm_page_flag_clear(m, PG_ZERO);
1190 vm_page_flag_set(m, PG_MAPPED);
1191 vm_page_wakeup(m);
1193 return (m);
1197 * Determine the page table page required to access the VA in the pmap
1198 * and allocate it if necessary. Return a held vm_page_t for the page.
1200 * Only used with user pmaps.
1202 static vm_page_t
1203 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1205 unsigned ptepindex;
1206 vm_offset_t ptepa;
1207 vm_page_t m;
1210 * Calculate pagetable page index
1212 ptepindex = va >> PDRSHIFT;
1215 * Get the page directory entry
1217 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1220 * This supports switching from a 4MB page to a
1221 * normal 4K page.
1223 if (ptepa & VPTE_PS) {
1224 KKASSERT(pmap->pm_pdir[ptepindex] != 0);
1225 pmap_inval_pde(&pmap->pm_pdir[ptepindex], pmap,
1226 (vm_offset_t)ptepindex << SEG_SHIFT);
1227 ptepa = 0;
1231 * If the page table page is mapped, we just increment the
1232 * hold count, and activate it.
1234 if (ptepa) {
1236 * In order to get the page table page, try the
1237 * hint first.
1239 if (pmap->pm_ptphint &&
1240 (pmap->pm_ptphint->pindex == ptepindex)) {
1241 m = pmap->pm_ptphint;
1242 } else {
1243 m = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1244 pmap->pm_ptphint = m;
1246 m->hold_count++;
1247 return m;
1250 * Here if the pte page isn't mapped, or if it has been deallocated.
1252 return _pmap_allocpte(pmap, ptepindex);
1255 /************************************************************************
1256 * Managed pages in pmaps *
1257 ************************************************************************
1259 * All pages entered into user pmaps and some pages entered into the kernel
1260 * pmap are managed, meaning that pmap_protect() and other related management
1261 * functions work on these pages.
1265 * free the pv_entry back to the free list. This function may be
1266 * called from an interrupt.
1268 static __inline void
1269 free_pv_entry(pv_entry_t pv)
1271 pv_entry_count--;
1272 zfree(&pvzone, pv);
1276 * get a new pv_entry, allocating a block from the system
1277 * when needed. This function may be called from an interrupt.
1279 static pv_entry_t
1280 get_pv_entry(void)
1282 pv_entry_count++;
1283 if (pv_entry_high_water &&
1284 (pv_entry_count > pv_entry_high_water) &&
1285 (pmap_pagedaemon_waken == 0)) {
1286 pmap_pagedaemon_waken = 1;
1287 wakeup (&vm_pages_needed);
1289 return zalloc(&pvzone);
1293 * This routine is very drastic, but can save the system
1294 * in a pinch.
1296 void
1297 pmap_collect(void)
1299 int i;
1300 vm_page_t m;
1301 static int warningdone=0;
1303 if (pmap_pagedaemon_waken == 0)
1304 return;
1305 pmap_pagedaemon_waken = 0;
1307 if (warningdone < 5) {
1308 kprintf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1309 warningdone++;
1312 for(i = 0; i < vm_page_array_size; i++) {
1313 m = &vm_page_array[i];
1314 if (m->wire_count || m->hold_count || m->busy ||
1315 (m->flags & PG_BUSY))
1316 continue;
1317 pmap_remove_all(m);
1322 * If it is the first entry on the list, it is actually
1323 * in the header and we must copy the following entry up
1324 * to the header. Otherwise we must search the list for
1325 * the entry. In either case we free the now unused entry.
1327 static int
1328 pmap_remove_entry(struct pmap *pmap, vm_page_t m, vm_offset_t va)
1330 pv_entry_t pv;
1331 int rtval;
1333 crit_enter();
1334 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1335 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1336 if (pmap == pv->pv_pmap && va == pv->pv_va)
1337 break;
1339 } else {
1340 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1341 if (va == pv->pv_va)
1342 break;
1347 * Note that pv_ptem is NULL if the page table page itself is not
1348 * managed, even if the page being removed IS managed.
1350 rtval = 0;
1352 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1353 m->md.pv_list_count--;
1354 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1355 if (TAILQ_EMPTY(&m->md.pv_list))
1356 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1357 ++pmap->pm_generation;
1358 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem);
1359 free_pv_entry(pv);
1361 crit_exit();
1362 return rtval;
1366 * Create a pv entry for page at pa for (pmap, va). If the page table page
1367 * holding the VA is managed, mpte will be non-NULL.
1369 static void
1370 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
1372 pv_entry_t pv;
1374 crit_enter();
1375 pv = get_pv_entry();
1376 pv->pv_va = va;
1377 pv->pv_pmap = pmap;
1378 pv->pv_ptem = mpte;
1380 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1381 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1382 ++pmap->pm_generation;
1383 m->md.pv_list_count++;
1385 crit_exit();
1389 * pmap_remove_pte: do the things to unmap a page in a process
1391 static int
1392 pmap_remove_pte(struct pmap *pmap, vpte_t *ptq, vm_offset_t va)
1394 vpte_t oldpte;
1395 vm_page_t m;
1397 oldpte = pmap_inval_loadandclear(ptq, pmap, va);
1398 if (oldpte & VPTE_WIRED)
1399 --pmap->pm_stats.wired_count;
1400 KKASSERT(pmap->pm_stats.wired_count >= 0);
1402 #if 0
1404 * Machines that don't support invlpg, also don't support
1405 * VPTE_G. XXX VPTE_G is disabled for SMP so don't worry about
1406 * the SMP case.
1408 if (oldpte & VPTE_G)
1409 madvise((void *)va, PAGE_SIZE, MADV_INVAL);
1410 #endif
1411 KKASSERT(pmap->pm_stats.resident_count > 0);
1412 --pmap->pm_stats.resident_count;
1413 if (oldpte & VPTE_MANAGED) {
1414 m = PHYS_TO_VM_PAGE(oldpte);
1415 if (oldpte & VPTE_M) {
1416 #if defined(PMAP_DIAGNOSTIC)
1417 if (pmap_nw_modified((pt_entry_t) oldpte)) {
1418 kprintf(
1419 "pmap_remove: modified page not writable: va: 0x%x, pte: 0x%x\n",
1420 va, oldpte);
1422 #endif
1423 if (pmap_track_modified(pmap, va))
1424 vm_page_dirty(m);
1426 if (oldpte & VPTE_A)
1427 vm_page_flag_set(m, PG_REFERENCED);
1428 return pmap_remove_entry(pmap, m, va);
1429 } else {
1430 return pmap_unuse_pt(pmap, va, NULL);
1433 return 0;
1437 * pmap_remove_page:
1439 * Remove a single page from a process address space.
1441 * This function may not be called from an interrupt if the pmap is
1442 * not kernel_pmap.
1444 static void
1445 pmap_remove_page(struct pmap *pmap, vm_offset_t va)
1447 vpte_t *ptq;
1450 * if there is no pte for this address, just skip it!!! Otherwise
1451 * get a local va for mappings for this pmap and remove the entry.
1453 if (*pmap_pde(pmap, va) != 0) {
1454 ptq = get_ptbase(pmap, va);
1455 if (*ptq) {
1456 pmap_remove_pte(pmap, ptq, va);
1462 * pmap_remove:
1464 * Remove the given range of addresses from the specified map.
1466 * It is assumed that the start and end are properly
1467 * rounded to the page size.
1469 * This function may not be called from an interrupt if the pmap is
1470 * not kernel_pmap.
1472 void
1473 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
1475 vpte_t *ptbase;
1476 vm_offset_t pdnxt;
1477 vm_offset_t ptpaddr;
1478 vm_pindex_t sindex, eindex;
1480 if (pmap == NULL)
1481 return;
1483 KKASSERT(pmap->pm_stats.resident_count >= 0);
1484 if (pmap->pm_stats.resident_count == 0)
1485 return;
1488 * special handling of removing one page. a very
1489 * common operation and easy to short circuit some
1490 * code.
1492 if (((sva + PAGE_SIZE) == eva) &&
1493 ((pmap->pm_pdir[(sva >> PDRSHIFT)] & VPTE_PS) == 0)) {
1494 pmap_remove_page(pmap, sva);
1495 return;
1499 * Get a local virtual address for the mappings that are being
1500 * worked with.
1502 * XXX this is really messy because the kernel pmap is not relative
1503 * to address 0
1505 sindex = (sva >> PAGE_SHIFT);
1506 eindex = (eva >> PAGE_SHIFT);
1508 for (; sindex < eindex; sindex = pdnxt) {
1509 vpte_t pdirindex;
1512 * Calculate index for next page table.
1514 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1515 if (pmap->pm_stats.resident_count == 0)
1516 break;
1518 pdirindex = sindex / NPDEPG;
1519 if (((ptpaddr = pmap->pm_pdir[pdirindex]) & VPTE_PS) != 0) {
1520 KKASSERT(pmap->pm_pdir[pdirindex] != 0);
1521 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1522 pmap_inval_pde(&pmap->pm_pdir[pdirindex], pmap,
1523 (vm_offset_t)pdirindex << SEG_SHIFT);
1524 continue;
1528 * Weed out invalid mappings. Note: we assume that the page
1529 * directory table is always allocated, and in kernel virtual.
1531 if (ptpaddr == 0)
1532 continue;
1535 * Limit our scan to either the end of the va represented
1536 * by the current page table page, or to the end of the
1537 * range being removed.
1539 if (pdnxt > eindex)
1540 pdnxt = eindex;
1543 * NOTE: pmap_remove_pte() can block.
1545 for (; sindex != pdnxt; sindex++) {
1546 vm_offset_t va;
1548 ptbase = get_ptbase(pmap, sindex << PAGE_SHIFT);
1549 if (*ptbase == 0)
1550 continue;
1551 va = i386_ptob(sindex);
1552 if (pmap_remove_pte(pmap, ptbase, va))
1553 break;
1559 * pmap_remove_all:
1561 * Removes this physical page from all physical maps in which it resides.
1562 * Reflects back modify bits to the pager.
1564 * This routine may not be called from an interrupt.
1566 static void
1567 pmap_remove_all(vm_page_t m)
1569 vpte_t *pte, tpte;
1570 pv_entry_t pv;
1572 #if defined(PMAP_DIAGNOSTIC)
1574 * XXX this makes pmap_page_protect(NONE) illegal for non-managed
1575 * pages!
1577 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
1578 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%08llx", (long long)VM_PAGE_TO_PHYS(m));
1580 #endif
1582 crit_enter();
1583 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
1584 KKASSERT(pv->pv_pmap->pm_stats.resident_count > 0);
1585 --pv->pv_pmap->pm_stats.resident_count;
1587 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
1588 KKASSERT(pte != NULL);
1590 tpte = pmap_inval_loadandclear(pte, pv->pv_pmap, pv->pv_va);
1591 if (tpte & VPTE_WIRED)
1592 --pv->pv_pmap->pm_stats.wired_count;
1593 KKASSERT(pv->pv_pmap->pm_stats.wired_count >= 0);
1595 if (tpte & VPTE_A)
1596 vm_page_flag_set(m, PG_REFERENCED);
1599 * Update the vm_page_t clean and reference bits.
1601 if (tpte & VPTE_M) {
1602 #if defined(PMAP_DIAGNOSTIC)
1603 if (pmap_nw_modified((pt_entry_t) tpte)) {
1604 kprintf(
1605 "pmap_remove_all: modified page not writable: va: 0x%x, pte: 0x%x\n",
1606 pv->pv_va, tpte);
1608 #endif
1609 if (pmap_track_modified(pv->pv_pmap, pv->pv_va))
1610 vm_page_dirty(m);
1612 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1613 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
1614 ++pv->pv_pmap->pm_generation;
1615 m->md.pv_list_count--;
1616 if (TAILQ_EMPTY(&m->md.pv_list))
1617 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1618 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
1619 free_pv_entry(pv);
1621 KKASSERT((m->flags & (PG_MAPPED | PG_WRITEABLE)) == 0);
1622 crit_exit();
1626 * pmap_protect:
1628 * Set the physical protection on the specified range of this map
1629 * as requested.
1631 * This function may not be called from an interrupt if the map is
1632 * not the kernel_pmap.
1634 void
1635 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
1637 vpte_t *ptbase;
1638 vpte_t *ptep;
1639 vm_offset_t pdnxt, ptpaddr;
1640 vm_pindex_t sindex, eindex;
1641 vm_pindex_t sbase;
1643 if (pmap == NULL)
1644 return;
1646 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1647 pmap_remove(pmap, sva, eva);
1648 return;
1651 if (prot & VM_PROT_WRITE)
1652 return;
1654 ptbase = get_ptbase(pmap, sva);
1656 sindex = (sva >> PAGE_SHIFT);
1657 eindex = (eva >> PAGE_SHIFT);
1658 sbase = sindex;
1660 for (; sindex < eindex; sindex = pdnxt) {
1662 unsigned pdirindex;
1664 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1666 pdirindex = sindex / NPDEPG;
1669 * Clear the modified and writable bits for a 4m page.
1670 * Throw away the modified bit (?)
1672 if (((ptpaddr = pmap->pm_pdir[pdirindex]) & VPTE_PS) != 0) {
1673 pmap_clean_pde(&pmap->pm_pdir[pdirindex], pmap,
1674 (vm_offset_t)pdirindex << SEG_SHIFT);
1675 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1676 continue;
1680 * Weed out invalid mappings. Note: we assume that the page
1681 * directory table is always allocated, and in kernel virtual.
1683 if (ptpaddr == 0)
1684 continue;
1686 if (pdnxt > eindex) {
1687 pdnxt = eindex;
1690 for (; sindex != pdnxt; sindex++) {
1691 vpte_t pbits;
1692 vm_page_t m;
1695 * Clean managed pages and also check the accessed
1696 * bit. Just remove write perms for unmanaged
1697 * pages. Be careful of races, turning off write
1698 * access will force a fault rather then setting
1699 * the modified bit at an unexpected time.
1701 ptep = &ptbase[sindex - sbase];
1702 if (*ptep & VPTE_MANAGED) {
1703 pbits = pmap_clean_pte(ptep, pmap,
1704 i386_ptob(sindex));
1705 m = NULL;
1706 if (pbits & VPTE_A) {
1707 m = PHYS_TO_VM_PAGE(pbits);
1708 vm_page_flag_set(m, PG_REFERENCED);
1709 atomic_clear_int(ptep, VPTE_A);
1711 if (pbits & VPTE_M) {
1712 if (pmap_track_modified(pmap, i386_ptob(sindex))) {
1713 if (m == NULL)
1714 m = PHYS_TO_VM_PAGE(pbits);
1715 vm_page_dirty(m);
1718 } else {
1719 pbits = pmap_setro_pte(ptep, pmap,
1720 i386_ptob(sindex));
1727 * Enter a managed page into a pmap. If the page is not wired related pmap
1728 * data can be destroyed at any time for later demand-operation.
1730 * Insert the vm_page (m) at virtual address (v) in (pmap), with the
1731 * specified protection, and wire the mapping if requested.
1733 * NOTE: This routine may not lazy-evaluate or lose information. The
1734 * page must actually be inserted into the given map NOW.
1736 * NOTE: When entering a page at a KVA address, the pmap must be the
1737 * kernel_pmap.
1739 void
1740 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1741 boolean_t wired)
1743 vm_paddr_t pa;
1744 vpte_t *pte;
1745 vm_paddr_t opa;
1746 vm_offset_t origpte, newpte;
1747 vm_page_t mpte;
1749 if (pmap == NULL)
1750 return;
1752 va &= VPTE_FRAME;
1755 * Get the page table page. The kernel_pmap's page table pages
1756 * are preallocated and have no associated vm_page_t.
1758 if (pmap == &kernel_pmap)
1759 mpte = NULL;
1760 else
1761 mpte = pmap_allocpte(pmap, va);
1763 pte = pmap_pte(pmap, va);
1766 * Page Directory table entry not valid, we need a new PT page
1767 * and pmap_allocpte() didn't give us one. Oops!
1769 if (pte == NULL) {
1770 panic("pmap_enter: invalid page directory pmap=%p, va=0x%p\n",
1771 pmap, (void *)va);
1775 * Deal with races on the original mapping (though don't worry
1776 * about VPTE_A races) by cleaning it. This will force a fault
1777 * if an attempt is made to write to the page.
1779 pa = VM_PAGE_TO_PHYS(m) & VPTE_FRAME;
1780 origpte = pmap_clean_pte(pte, pmap, va);
1781 opa = origpte & VPTE_FRAME;
1783 if (origpte & VPTE_PS)
1784 panic("pmap_enter: attempted pmap_enter on 4MB page");
1787 * Mapping has not changed, must be protection or wiring change.
1789 if (origpte && (opa == pa)) {
1791 * Wiring change, just update stats. We don't worry about
1792 * wiring PT pages as they remain resident as long as there
1793 * are valid mappings in them. Hence, if a user page is wired,
1794 * the PT page will be also.
1796 if (wired && ((origpte & VPTE_WIRED) == 0))
1797 ++pmap->pm_stats.wired_count;
1798 else if (!wired && (origpte & VPTE_WIRED))
1799 --pmap->pm_stats.wired_count;
1800 KKASSERT(pmap->pm_stats.wired_count >= 0);
1803 * Remove the extra pte reference. Note that we cannot
1804 * optimize the RO->RW case because we have adjusted the
1805 * wiring count above and may need to adjust the wiring
1806 * bits below.
1808 if (mpte)
1809 mpte->hold_count--;
1812 * We might be turning off write access to the page,
1813 * so we go ahead and sense modify status.
1815 if (origpte & VPTE_MANAGED) {
1816 if ((origpte & VPTE_M) &&
1817 pmap_track_modified(pmap, va)) {
1818 vm_page_t om;
1819 om = PHYS_TO_VM_PAGE(opa);
1820 vm_page_dirty(om);
1822 pa |= VPTE_MANAGED;
1823 KKASSERT(m->flags & PG_MAPPED);
1825 goto validate;
1828 * Mapping has changed, invalidate old range and fall through to
1829 * handle validating new mapping.
1831 while (opa) {
1832 int err;
1833 err = pmap_remove_pte(pmap, pte, va);
1834 if (err)
1835 panic("pmap_enter: pte vanished, va: %p", (void *)va);
1836 pte = pmap_pte(pmap, va);
1837 origpte = pmap_clean_pte(pte, pmap, va);
1838 opa = origpte & VPTE_FRAME;
1839 if (opa) {
1840 kprintf("pmap_enter: Warning, raced pmap %p va %p\n",
1841 pmap, (void *)va);
1846 * Enter on the PV list if part of our managed memory. Note that we
1847 * raise IPL while manipulating pv_table since pmap_enter can be
1848 * called at interrupt time.
1850 if (pmap_initialized &&
1851 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
1852 pmap_insert_entry(pmap, va, mpte, m);
1853 pa |= VPTE_MANAGED;
1854 vm_page_flag_set(m, PG_MAPPED);
1858 * Increment counters
1860 ++pmap->pm_stats.resident_count;
1861 if (wired)
1862 pmap->pm_stats.wired_count++;
1864 validate:
1866 * Now validate mapping with desired protection/wiring.
1868 newpte = (vm_offset_t) (pa | pte_prot(pmap, prot) | VPTE_V);
1870 if (wired)
1871 newpte |= VPTE_WIRED;
1872 if (pmap != &kernel_pmap)
1873 newpte |= VPTE_U;
1876 * If the mapping or permission bits are different from the
1877 * (now cleaned) original pte, an update is needed. We've
1878 * already downgraded or invalidated the page so all we have
1879 * to do now is update the bits.
1881 * XXX should we synchronize RO->RW changes to avoid another
1882 * fault?
1884 if ((origpte & ~(VPTE_W|VPTE_M|VPTE_A)) != newpte) {
1885 *pte = newpte | VPTE_A;
1886 if (newpte & VPTE_W)
1887 vm_page_flag_set(m, PG_WRITEABLE);
1889 KKASSERT((newpte & VPTE_MANAGED) == 0 || m->flags & PG_MAPPED);
1893 * This code works like pmap_enter() but assumes VM_PROT_READ and not-wired.
1895 * Currently this routine may only be used on user pmaps, not kernel_pmap.
1897 void
1898 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m)
1900 vpte_t *pte;
1901 vm_paddr_t pa;
1902 vm_page_t mpte;
1903 unsigned ptepindex;
1904 vm_offset_t ptepa;
1906 KKASSERT(pmap != &kernel_pmap);
1908 KKASSERT(va >= VM_MIN_USER_ADDRESS && va < VM_MAX_USER_ADDRESS);
1911 * Calculate pagetable page (mpte), allocating it if necessary.
1913 * A held page table page (mpte), or NULL, is passed onto the
1914 * section following.
1916 ptepindex = va >> PDRSHIFT;
1918 do {
1920 * Get the page directory entry
1922 ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1925 * If the page table page is mapped, we just increment
1926 * the hold count, and activate it.
1928 if (ptepa) {
1929 if (ptepa & VPTE_PS)
1930 panic("pmap_enter_quick: unexpected mapping into 4MB page");
1931 if (pmap->pm_ptphint &&
1932 (pmap->pm_ptphint->pindex == ptepindex)) {
1933 mpte = pmap->pm_ptphint;
1934 } else {
1935 mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1936 pmap->pm_ptphint = mpte;
1938 if (mpte)
1939 mpte->hold_count++;
1940 } else {
1941 mpte = _pmap_allocpte(pmap, ptepindex);
1943 } while (mpte == NULL);
1946 * Ok, now that the page table page has been validated, get the pte.
1947 * If the pte is already mapped undo mpte's hold_count and
1948 * just return.
1950 pte = pmap_pte(pmap, va);
1951 if (*pte) {
1952 pmap_unwire_pte_hold(pmap, mpte);
1953 return;
1957 * Enter on the PV list if part of our managed memory. Note that we
1958 * raise IPL while manipulating pv_table since pmap_enter can be
1959 * called at interrupt time.
1961 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
1962 pmap_insert_entry(pmap, va, mpte, m);
1963 vm_page_flag_set(m, PG_MAPPED);
1967 * Increment counters
1969 ++pmap->pm_stats.resident_count;
1971 pa = VM_PAGE_TO_PHYS(m);
1974 * Now validate mapping with RO protection
1976 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
1977 *pte = (vpte_t)pa | VPTE_V | VPTE_U;
1978 else
1979 *pte = (vpte_t)pa | VPTE_V | VPTE_U | VPTE_MANAGED;
1980 /*pmap_inval_add(&info, pmap, va); shouldn't be needed 0->valid */
1981 /*pmap_inval_flush(&info); don't need for vkernel */
1985 * Extract the physical address for the translation at the specified
1986 * virtual address in the pmap.
1988 vm_paddr_t
1989 pmap_extract(pmap_t pmap, vm_offset_t va)
1991 vm_paddr_t rtval;
1992 vpte_t pte;
1994 if (pmap && (pte = pmap->pm_pdir[va >> SEG_SHIFT]) != 0) {
1995 if (pte & VPTE_PS) {
1996 rtval = pte & ~((vpte_t)(1 << SEG_SHIFT) - 1);
1997 rtval |= va & SEG_MASK;
1998 } else {
1999 pte = *get_ptbase(pmap, va);
2000 rtval = (pte & VPTE_FRAME) | (va & PAGE_MASK);
2002 return(rtval);
2004 return(0);
2007 #define MAX_INIT_PT (96)
2010 * This routine preloads the ptes for a given object into the specified pmap.
2011 * This eliminates the blast of soft faults on process startup and
2012 * immediately after an mmap.
2014 static int pmap_object_init_pt_callback(vm_page_t p, void *data);
2016 void
2017 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2018 vm_object_t object, vm_pindex_t pindex,
2019 vm_size_t size, int limit)
2021 struct rb_vm_page_scan_info info;
2022 struct lwp *lp;
2023 int psize;
2026 * We can't preinit if read access isn't set or there is no pmap
2027 * or object.
2029 if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2030 return;
2033 * We can't preinit if the pmap is not the current pmap
2035 lp = curthread->td_lwp;
2036 if (lp == NULL || pmap != vmspace_pmap(lp->lwp_vmspace))
2037 return;
2039 psize = size >> PAGE_SHIFT;
2041 if ((object->type != OBJT_VNODE) ||
2042 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2043 (object->resident_page_count > MAX_INIT_PT))) {
2044 return;
2047 if (psize + pindex > object->size) {
2048 if (object->size < pindex)
2049 return;
2050 psize = object->size - pindex;
2053 if (psize == 0)
2054 return;
2057 * Use a red-black scan to traverse the requested range and load
2058 * any valid pages found into the pmap.
2060 * We cannot safely scan the object's memq unless we are in a
2061 * critical section since interrupts can remove pages from objects.
2063 info.start_pindex = pindex;
2064 info.end_pindex = pindex + psize - 1;
2065 info.limit = limit;
2066 info.mpte = NULL;
2067 info.addr = addr;
2068 info.pmap = pmap;
2070 crit_enter();
2071 vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2072 pmap_object_init_pt_callback, &info);
2073 crit_exit();
2076 static
2078 pmap_object_init_pt_callback(vm_page_t p, void *data)
2080 struct rb_vm_page_scan_info *info = data;
2081 vm_pindex_t rel_index;
2083 * don't allow an madvise to blow away our really
2084 * free pages allocating pv entries.
2086 if ((info->limit & MAP_PREFAULT_MADVISE) &&
2087 vmstats.v_free_count < vmstats.v_free_reserved) {
2088 return(-1);
2090 if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2091 (p->busy == 0) && (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2092 if ((p->queue - p->pc) == PQ_CACHE)
2093 vm_page_deactivate(p);
2094 vm_page_busy(p);
2095 rel_index = p->pindex - info->start_pindex;
2096 pmap_enter_quick(info->pmap,
2097 info->addr + i386_ptob(rel_index), p);
2098 vm_page_wakeup(p);
2100 return(0);
2104 * Return TRUE if the pmap is in shape to trivially
2105 * pre-fault the specified address.
2107 * Returns FALSE if it would be non-trivial or if a
2108 * pte is already loaded into the slot.
2111 pmap_prefault_ok(pmap_t pmap, vm_offset_t addr)
2113 vpte_t *pte;
2115 if ((*pmap_pde(pmap, addr)) == 0)
2116 return(0);
2118 pte = get_ptbase(pmap, addr);
2119 if (*pte)
2120 return(0);
2122 return(1);
2126 * Routine: pmap_change_wiring
2127 * Function: Change the wiring attribute for a map/virtual-address
2128 * pair.
2129 * In/out conditions:
2130 * The mapping must already exist in the pmap.
2132 void
2133 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2135 vpte_t *pte;
2137 if (pmap == NULL)
2138 return;
2140 pte = get_ptbase(pmap, va);
2142 if (wired && (*pte & VPTE_WIRED) == 0)
2143 ++pmap->pm_stats.wired_count;
2144 else if (!wired && (*pte & VPTE_WIRED))
2145 --pmap->pm_stats.wired_count;
2146 KKASSERT(pmap->pm_stats.wired_count >= 0);
2149 * Wiring is not a hardware characteristic so there is no need to
2150 * invalidate TLB. However, in an SMP environment we must use
2151 * a locked bus cycle to update the pte (if we are not using
2152 * the pmap_inval_*() API that is)... it's ok to do this for simple
2153 * wiring changes.
2155 if (wired)
2156 atomic_set_int(pte, VPTE_WIRED);
2157 else
2158 atomic_clear_int(pte, VPTE_WIRED);
2162 * Copy the range specified by src_addr/len
2163 * from the source map to the range dst_addr/len
2164 * in the destination map.
2166 * This routine is only advisory and need not do anything.
2168 void
2169 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
2170 vm_size_t len, vm_offset_t src_addr)
2172 vm_offset_t addr;
2173 vm_offset_t end_addr = src_addr + len;
2174 vm_offset_t pdnxt;
2175 vpte_t *src_frame;
2176 vpte_t *dst_frame;
2177 vm_page_t m;
2180 * XXX BUGGY. Amoung other things srcmpte is assumed to remain
2181 * valid through blocking calls, and that's just not going to
2182 * be the case.
2184 * FIXME!
2186 return;
2188 if (dst_addr != src_addr)
2189 return;
2190 if (dst_pmap->pm_pdir == NULL)
2191 return;
2192 if (src_pmap->pm_pdir == NULL)
2193 return;
2195 crit_enter();
2197 src_frame = get_ptbase1(src_pmap, src_addr);
2198 dst_frame = get_ptbase2(dst_pmap, src_addr);
2201 * critical section protection is required to maintain the page/object
2202 * association, interrupts can free pages and remove them from
2203 * their objects.
2205 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
2206 vpte_t *src_pte, *dst_pte;
2207 vm_page_t dstmpte, srcmpte;
2208 vm_offset_t srcptepaddr;
2209 unsigned ptepindex;
2211 if (addr >= VM_MAX_USER_ADDRESS)
2212 panic("pmap_copy: invalid to pmap_copy page tables\n");
2215 * Don't let optional prefaulting of pages make us go
2216 * way below the low water mark of free pages or way
2217 * above high water mark of used pv entries.
2219 if (vmstats.v_free_count < vmstats.v_free_reserved ||
2220 pv_entry_count > pv_entry_high_water)
2221 break;
2223 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
2224 ptepindex = addr >> PDRSHIFT;
2226 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex];
2227 if (srcptepaddr == 0)
2228 continue;
2230 if (srcptepaddr & VPTE_PS) {
2231 if (dst_pmap->pm_pdir[ptepindex] == 0) {
2232 dst_pmap->pm_pdir[ptepindex] = (pd_entry_t) srcptepaddr;
2233 dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
2235 continue;
2238 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
2239 if ((srcmpte == NULL) || (srcmpte->hold_count == 0) ||
2240 (srcmpte->flags & PG_BUSY)) {
2241 continue;
2244 if (pdnxt > end_addr)
2245 pdnxt = end_addr;
2247 src_pte = src_frame + ((addr - src_addr) >> PAGE_SHIFT);
2248 dst_pte = dst_frame + ((addr - src_addr) >> PAGE_SHIFT);
2249 while (addr < pdnxt) {
2250 vpte_t ptetemp;
2252 ptetemp = *src_pte;
2254 * we only virtual copy managed pages
2256 if ((ptetemp & VPTE_MANAGED) != 0) {
2258 * We have to check after allocpte for the
2259 * pte still being around... allocpte can
2260 * block.
2262 * pmap_allocpte can block, unfortunately
2263 * we have to reload the tables.
2265 dstmpte = pmap_allocpte(dst_pmap, addr);
2266 src_frame = get_ptbase1(src_pmap, src_addr);
2267 dst_frame = get_ptbase2(dst_pmap, src_addr);
2269 if ((*dst_pte == 0) && (ptetemp = *src_pte) &&
2270 (ptetemp & VPTE_MANAGED) != 0) {
2272 * Clear the modified and accessed
2273 * (referenced) bits during the copy.
2275 * We do not have to clear the write
2276 * bit to force a fault-on-modify
2277 * because the real kernel's target
2278 * pmap is empty and will fault anyway.
2280 m = PHYS_TO_VM_PAGE(ptetemp);
2281 *dst_pte = ptetemp & ~(VPTE_M | VPTE_A);
2282 ++dst_pmap->pm_stats.resident_count;
2283 pmap_insert_entry(dst_pmap, addr,
2284 dstmpte, m);
2285 KKASSERT(m->flags & PG_MAPPED);
2286 } else {
2287 pmap_unwire_pte_hold(dst_pmap, dstmpte);
2289 if (dstmpte->hold_count >= srcmpte->hold_count)
2290 break;
2292 addr += PAGE_SIZE;
2293 src_pte++;
2294 dst_pte++;
2297 crit_exit();
2301 * pmap_zero_page:
2303 * Zero the specified PA by mapping the page into KVM and clearing its
2304 * contents.
2306 * This function may be called from an interrupt and no locking is
2307 * required.
2309 void
2310 pmap_zero_page(vm_paddr_t phys)
2312 struct mdglobaldata *gd = mdcpu;
2314 crit_enter();
2315 if (*gd->gd_CMAP3)
2316 panic("pmap_zero_page: CMAP3 busy");
2317 *gd->gd_CMAP3 = VPTE_V | VPTE_R | VPTE_W | (phys & VPTE_FRAME) | VPTE_A | VPTE_M;
2318 madvise(gd->gd_CADDR3, PAGE_SIZE, MADV_INVAL);
2320 bzero(gd->gd_CADDR3, PAGE_SIZE);
2321 *gd->gd_CMAP3 = 0;
2322 crit_exit();
2326 * pmap_page_assertzero:
2328 * Assert that a page is empty, panic if it isn't.
2330 void
2331 pmap_page_assertzero(vm_paddr_t phys)
2333 struct mdglobaldata *gd = mdcpu;
2334 int i;
2336 crit_enter();
2337 if (*gd->gd_CMAP3)
2338 panic("pmap_zero_page: CMAP3 busy");
2339 *gd->gd_CMAP3 = VPTE_V | VPTE_R | VPTE_W |
2340 (phys & VPTE_FRAME) | VPTE_A | VPTE_M;
2341 madvise(gd->gd_CADDR3, PAGE_SIZE, MADV_INVAL);
2342 for (i = 0; i < PAGE_SIZE; i += 4) {
2343 if (*(int *)((char *)gd->gd_CADDR3 + i) != 0) {
2344 panic("pmap_page_assertzero() @ %p not zero!\n",
2345 (void *)gd->gd_CADDR3);
2348 *gd->gd_CMAP3 = 0;
2349 crit_exit();
2353 * pmap_zero_page:
2355 * Zero part of a physical page by mapping it into memory and clearing
2356 * its contents with bzero.
2358 * off and size may not cover an area beyond a single hardware page.
2360 void
2361 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
2363 struct mdglobaldata *gd = mdcpu;
2365 crit_enter();
2366 if (*gd->gd_CMAP3)
2367 panic("pmap_zero_page: CMAP3 busy");
2368 *gd->gd_CMAP3 = VPTE_V | VPTE_R | VPTE_W |
2369 (phys & VPTE_FRAME) | VPTE_A | VPTE_M;
2370 madvise(gd->gd_CADDR3, PAGE_SIZE, MADV_INVAL);
2372 bzero((char *)gd->gd_CADDR3 + off, size);
2373 *gd->gd_CMAP3 = 0;
2374 crit_exit();
2378 * pmap_copy_page:
2380 * Copy the physical page from the source PA to the target PA.
2381 * This function may be called from an interrupt. No locking
2382 * is required.
2384 void
2385 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
2387 struct mdglobaldata *gd = mdcpu;
2389 crit_enter();
2390 if (*(int *) gd->gd_CMAP1)
2391 panic("pmap_copy_page: CMAP1 busy");
2392 if (*(int *) gd->gd_CMAP2)
2393 panic("pmap_copy_page: CMAP2 busy");
2395 *(int *) gd->gd_CMAP1 = VPTE_V | VPTE_R | (src & PG_FRAME) | VPTE_A;
2396 *(int *) gd->gd_CMAP2 = VPTE_V | VPTE_R | VPTE_W | (dst & VPTE_FRAME) | VPTE_A | VPTE_M;
2398 madvise(gd->gd_CADDR1, PAGE_SIZE, MADV_INVAL);
2399 madvise(gd->gd_CADDR2, PAGE_SIZE, MADV_INVAL);
2401 bcopy(gd->gd_CADDR1, gd->gd_CADDR2, PAGE_SIZE);
2403 *(int *) gd->gd_CMAP1 = 0;
2404 *(int *) gd->gd_CMAP2 = 0;
2405 crit_exit();
2409 * pmap_copy_page_frag:
2411 * Copy the physical page from the source PA to the target PA.
2412 * This function may be called from an interrupt. No locking
2413 * is required.
2415 void
2416 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
2418 struct mdglobaldata *gd = mdcpu;
2420 crit_enter();
2421 if (*(int *) gd->gd_CMAP1)
2422 panic("pmap_copy_page: CMAP1 busy");
2423 if (*(int *) gd->gd_CMAP2)
2424 panic("pmap_copy_page: CMAP2 busy");
2426 *(int *) gd->gd_CMAP1 = VPTE_V | (src & VPTE_FRAME) | VPTE_A;
2427 *(int *) gd->gd_CMAP2 = VPTE_V | VPTE_R | VPTE_W | (dst & VPTE_FRAME) | VPTE_A | VPTE_M;
2429 madvise(gd->gd_CADDR1, PAGE_SIZE, MADV_INVAL);
2430 madvise(gd->gd_CADDR2, PAGE_SIZE, MADV_INVAL);
2432 bcopy((char *)gd->gd_CADDR1 + (src & PAGE_MASK),
2433 (char *)gd->gd_CADDR2 + (dst & PAGE_MASK),
2434 bytes);
2436 *(int *) gd->gd_CMAP1 = 0;
2437 *(int *) gd->gd_CMAP2 = 0;
2438 crit_exit();
2442 * Returns true if the pmap's pv is one of the first
2443 * 16 pvs linked to from this page. This count may
2444 * be changed upwards or downwards in the future; it
2445 * is only necessary that true be returned for a small
2446 * subset of pmaps for proper page aging.
2448 boolean_t
2449 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2451 pv_entry_t pv;
2452 int loops = 0;
2454 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2455 return FALSE;
2457 crit_enter();
2459 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2460 if (pv->pv_pmap == pmap) {
2461 crit_exit();
2462 return TRUE;
2464 loops++;
2465 if (loops >= 16)
2466 break;
2468 crit_exit();
2469 return (FALSE);
2473 * Remove all pages from specified address space
2474 * this aids process exit speeds. Also, this code
2475 * is special cased for current process only, but
2476 * can have the more generic (and slightly slower)
2477 * mode enabled. This is much faster than pmap_remove
2478 * in the case of running down an entire address space.
2480 void
2481 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2483 vpte_t *pte, tpte;
2484 pv_entry_t pv, npv;
2485 vm_page_t m;
2486 int iscurrentpmap;
2487 int32_t save_generation;
2488 struct lwp *lp;
2490 lp = curthread->td_lwp;
2491 if (lp && pmap == vmspace_pmap(lp->lwp_vmspace))
2492 iscurrentpmap = 1;
2493 else
2494 iscurrentpmap = 0;
2496 crit_enter();
2497 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
2498 if (pv->pv_va >= eva || pv->pv_va < sva) {
2499 npv = TAILQ_NEXT(pv, pv_plist);
2500 continue;
2503 KKASSERT(pmap == pv->pv_pmap);
2505 pte = pmap_pte(pmap, pv->pv_va);
2508 * We cannot remove wired pages from a process' mapping
2509 * at this time
2511 if (*pte & VPTE_WIRED) {
2512 npv = TAILQ_NEXT(pv, pv_plist);
2513 continue;
2515 tpte = pmap_inval_loadandclear(pte, pmap, pv->pv_va);
2517 m = PHYS_TO_VM_PAGE(tpte);
2519 KASSERT(m < &vm_page_array[vm_page_array_size],
2520 ("pmap_remove_pages: bad tpte %x", tpte));
2522 KKASSERT(pmap->pm_stats.resident_count > 0);
2523 --pmap->pm_stats.resident_count;
2526 * Update the vm_page_t clean and reference bits.
2528 if (tpte & VPTE_M) {
2529 vm_page_dirty(m);
2532 npv = TAILQ_NEXT(pv, pv_plist);
2533 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
2534 save_generation = ++pmap->pm_generation;
2536 m->md.pv_list_count--;
2537 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2538 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2539 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2541 pmap_unuse_pt(pmap, pv->pv_va, pv->pv_ptem);
2542 free_pv_entry(pv);
2545 * Restart the scan if we blocked during the unuse or free
2546 * calls and other removals were made.
2548 if (save_generation != pmap->pm_generation) {
2549 kprintf("Warning: pmap_remove_pages race-A avoided\n");
2550 pv = TAILQ_FIRST(&pmap->pm_pvlist);
2553 crit_exit();
2557 * pmap_testbit tests bits in active mappings of a VM page.
2559 static boolean_t
2560 pmap_testbit(vm_page_t m, int bit)
2562 pv_entry_t pv;
2563 vpte_t *pte;
2565 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2566 return FALSE;
2568 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2569 return FALSE;
2571 crit_enter();
2573 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2575 * if the bit being tested is the modified bit, then
2576 * mark clean_map and ptes as never
2577 * modified.
2579 if (bit & (VPTE_A|VPTE_M)) {
2580 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2581 continue;
2584 #if defined(PMAP_DIAGNOSTIC)
2585 if (!pv->pv_pmap) {
2586 kprintf("Null pmap (tb) at va: 0x%x\n", pv->pv_va);
2587 continue;
2589 #endif
2590 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2591 if (*pte & bit) {
2592 crit_exit();
2593 return TRUE;
2596 crit_exit();
2597 return (FALSE);
2601 * This routine is used to clear bits in ptes. Certain bits require special
2602 * handling, in particular (on virtual kernels) the VPTE_M (modify) bit.
2604 * This routine is only called with certain VPTE_* bit combinations.
2606 static __inline void
2607 pmap_clearbit(vm_page_t m, int bit)
2609 pv_entry_t pv;
2610 vpte_t *pte;
2611 vpte_t pbits;
2613 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2614 return;
2616 crit_enter();
2619 * Loop over all current mappings setting/clearing as appropos If
2620 * setting RO do we need to clear the VAC?
2622 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2624 * don't write protect pager mappings
2626 if (bit == VPTE_W) {
2627 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2628 continue;
2631 #if defined(PMAP_DIAGNOSTIC)
2632 if (!pv->pv_pmap) {
2633 kprintf("Null pmap (cb) at va: 0x%x\n", pv->pv_va);
2634 continue;
2636 #endif
2639 * Careful here. We can use a locked bus instruction to
2640 * clear VPTE_A or VPTE_M safely but we need to synchronize
2641 * with the target cpus when we mess with VPTE_W.
2643 * On virtual kernels we must force a new fault-on-write
2644 * in the real kernel if we clear the Modify bit ourselves,
2645 * otherwise the real kernel will not get a new fault and
2646 * will never set our Modify bit again.
2648 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2649 if (*pte & bit) {
2650 if (bit == VPTE_W) {
2652 * We must also clear VPTE_M when clearing
2653 * VPTE_W
2655 pbits = pmap_clean_pte(pte, pv->pv_pmap,
2656 pv->pv_va);
2657 if (pbits & VPTE_M)
2658 vm_page_dirty(m);
2659 } else if (bit == VPTE_M) {
2661 * We do not have to make the page read-only
2662 * when clearing the Modify bit. The real
2663 * kernel will make the real PTE read-only
2664 * or otherwise detect the write and set
2665 * our VPTE_M again simply by us invalidating
2666 * the real kernel VA for the pmap (as we did
2667 * above). This allows the real kernel to
2668 * handle the write fault without forwarding
2669 * the fault to us.
2671 atomic_clear_int(pte, VPTE_M);
2672 } else if ((bit & (VPTE_W|VPTE_M)) == (VPTE_W|VPTE_M)) {
2674 * We've been asked to clear W & M, I guess
2675 * the caller doesn't want us to update
2676 * the dirty status of the VM page.
2678 pmap_clean_pte(pte, pv->pv_pmap, pv->pv_va);
2679 } else {
2681 * We've been asked to clear bits that do
2682 * not interact with hardware.
2684 atomic_clear_int(pte, bit);
2688 crit_exit();
2692 * pmap_page_protect:
2694 * Lower the permission for all mappings to a given page.
2696 void
2697 pmap_page_protect(vm_page_t m, vm_prot_t prot)
2699 if ((prot & VM_PROT_WRITE) == 0) {
2700 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
2701 pmap_clearbit(m, VPTE_W);
2702 vm_page_flag_clear(m, PG_WRITEABLE);
2703 } else {
2704 pmap_remove_all(m);
2709 vm_paddr_t
2710 pmap_phys_address(vm_pindex_t ppn)
2712 return (i386_ptob(ppn));
2716 * pmap_ts_referenced:
2718 * Return a count of reference bits for a page, clearing those bits.
2719 * It is not necessary for every reference bit to be cleared, but it
2720 * is necessary that 0 only be returned when there are truly no
2721 * reference bits set.
2723 * XXX: The exact number of bits to check and clear is a matter that
2724 * should be tested and standardized at some point in the future for
2725 * optimal aging of shared pages.
2728 pmap_ts_referenced(vm_page_t m)
2730 pv_entry_t pv, pvf, pvn;
2731 vpte_t *pte;
2732 int rtval = 0;
2734 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2735 return (rtval);
2737 crit_enter();
2739 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2741 pvf = pv;
2743 do {
2744 pvn = TAILQ_NEXT(pv, pv_list);
2746 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2748 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2750 if (!pmap_track_modified(pv->pv_pmap, pv->pv_va))
2751 continue;
2753 pte = pmap_pte(pv->pv_pmap, pv->pv_va);
2755 if (pte && (*pte & VPTE_A)) {
2756 #ifdef SMP
2757 atomic_clear_int(pte, VPTE_A);
2758 #else
2759 atomic_clear_int_nonlocked(pte, VPTE_A);
2760 #endif
2761 rtval++;
2762 if (rtval > 4) {
2763 break;
2766 } while ((pv = pvn) != NULL && pv != pvf);
2768 crit_exit();
2770 return (rtval);
2774 * pmap_is_modified:
2776 * Return whether or not the specified physical page was modified
2777 * in any physical maps.
2779 boolean_t
2780 pmap_is_modified(vm_page_t m)
2782 return pmap_testbit(m, VPTE_M);
2786 * Clear the modify bits on the specified physical page.
2788 void
2789 pmap_clear_modify(vm_page_t m)
2791 pmap_clearbit(m, VPTE_M);
2795 * pmap_clear_reference:
2797 * Clear the reference bit on the specified physical page.
2799 void
2800 pmap_clear_reference(vm_page_t m)
2802 pmap_clearbit(m, VPTE_A);
2806 * Miscellaneous support routines follow
2809 static void
2810 i386_protection_init(void)
2812 int *kp, prot;
2814 kp = protection_codes;
2815 for (prot = 0; prot < 8; prot++) {
2816 if (prot & VM_PROT_READ)
2817 *kp |= VPTE_R;
2818 if (prot & VM_PROT_WRITE)
2819 *kp |= VPTE_W;
2820 if (prot & VM_PROT_EXECUTE)
2821 *kp |= VPTE_X;
2822 ++kp;
2826 #if 0
2829 * Map a set of physical memory pages into the kernel virtual
2830 * address space. Return a pointer to where it is mapped. This
2831 * routine is intended to be used for mapping device memory,
2832 * NOT real memory.
2834 * NOTE: we can't use pgeflag unless we invalidate the pages one at
2835 * a time.
2837 void *
2838 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
2840 vm_offset_t va, tmpva, offset;
2841 vpte_t *pte;
2843 offset = pa & PAGE_MASK;
2844 size = roundup(offset + size, PAGE_SIZE);
2846 va = kmem_alloc_nofault(&kernel_map, size);
2847 if (!va)
2848 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
2850 pa = pa & VPTE_FRAME;
2851 for (tmpva = va; size > 0;) {
2852 pte = KernelPTA + (tmpva >> PAGE_SHIFT);
2853 *pte = pa | VPTE_R | VPTE_W | VPTE_V; /* | pgeflag; */
2854 size -= PAGE_SIZE;
2855 tmpva += PAGE_SIZE;
2856 pa += PAGE_SIZE;
2858 cpu_invltlb();
2859 smp_invltlb();
2861 return ((void *)(va + offset));
2864 void
2865 pmap_unmapdev(vm_offset_t va, vm_size_t size)
2867 vm_offset_t base, offset;
2869 base = va & VPTE_FRAME;
2870 offset = va & PAGE_MASK;
2871 size = roundup(offset + size, PAGE_SIZE);
2872 pmap_qremove(va, size >> PAGE_SHIFT);
2873 kmem_free(&kernel_map, base, size);
2876 #endif
2879 * perform the pmap work for mincore
2882 pmap_mincore(pmap_t pmap, vm_offset_t addr)
2884 vpte_t *ptep, pte;
2885 vm_page_t m;
2886 int val = 0;
2888 ptep = pmap_pte(pmap, addr);
2889 if (ptep == 0) {
2890 return 0;
2893 if ((pte = *ptep) != 0) {
2894 vm_offset_t pa;
2896 val = MINCORE_INCORE;
2897 if ((pte & VPTE_MANAGED) == 0)
2898 return val;
2900 pa = pte & VPTE_FRAME;
2902 m = PHYS_TO_VM_PAGE(pa);
2905 * Modified by us
2907 if (pte & VPTE_M)
2908 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
2910 * Modified by someone
2912 else if (m->dirty || pmap_is_modified(m))
2913 val |= MINCORE_MODIFIED_OTHER;
2915 * Referenced by us
2917 if (pte & VPTE_A)
2918 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
2921 * Referenced by someone
2923 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
2924 val |= MINCORE_REFERENCED_OTHER;
2925 vm_page_flag_set(m, PG_REFERENCED);
2928 return val;
2931 void
2932 pmap_replacevm(struct proc *p, struct vmspace *newvm, int adjrefs)
2934 struct vmspace *oldvm;
2935 struct lwp *lp;
2937 oldvm = p->p_vmspace;
2938 crit_enter();
2939 if (oldvm != newvm) {
2940 p->p_vmspace = newvm;
2941 KKASSERT(p->p_nthreads == 1);
2942 lp = RB_ROOT(&p->p_lwp_tree);
2943 pmap_setlwpvm(lp, newvm);
2944 if (adjrefs) {
2945 sysref_get(&newvm->vm_sysref);
2946 sysref_put(&oldvm->vm_sysref);
2949 crit_exit();
2952 void
2953 pmap_setlwpvm(struct lwp *lp, struct vmspace *newvm)
2955 struct vmspace *oldvm;
2956 struct pmap *pmap;
2958 crit_enter();
2959 oldvm = lp->lwp_vmspace;
2961 if (oldvm != newvm) {
2962 lp->lwp_vmspace = newvm;
2963 if (curthread->td_lwp == lp) {
2964 pmap = vmspace_pmap(newvm);
2965 #if defined(SMP)
2966 atomic_set_int(&pmap->pm_active, mycpu->gd_cpumask);
2967 #else
2968 pmap->pm_active |= 1;
2969 #endif
2970 #if defined(SWTCH_OPTIM_STATS)
2971 tlb_flush_count++;
2972 #endif
2973 pmap = vmspace_pmap(oldvm);
2974 #if defined(SMP)
2975 atomic_clear_int(&pmap->pm_active, mycpu->gd_cpumask);
2976 #else
2977 pmap->pm_active &= ~1;
2978 #endif
2981 crit_exit();
2985 vm_offset_t
2986 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
2989 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
2990 return addr;
2993 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
2994 return addr;
2998 #if defined(DEBUG)
3000 static void pads (pmap_t pm);
3001 void pmap_pvdump (vm_paddr_t pa);
3003 /* print address space of pmap*/
3004 static void
3005 pads(pmap_t pm)
3007 vm_offset_t va;
3008 int i, j;
3009 vpte_t *ptep;
3011 if (pm == &kernel_pmap)
3012 return;
3013 for (i = 0; i < 1024; i++)
3014 if (pm->pm_pdir[i])
3015 for (j = 0; j < 1024; j++) {
3016 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
3017 if (pm == &kernel_pmap && va < KERNBASE)
3018 continue;
3019 if (pm != &kernel_pmap && va > UPT_MAX_ADDRESS)
3020 continue;
3021 ptep = pmap_pte(pm, va);
3022 if (ptep && (*ptep & VPTE_V)) {
3023 kprintf("%p:%x ",
3024 (void *)va, (unsigned)*ptep);
3030 void
3031 pmap_pvdump(vm_paddr_t pa)
3033 pv_entry_t pv;
3034 vm_page_t m;
3036 kprintf("pa %08llx", (long long)pa);
3037 m = PHYS_TO_VM_PAGE(pa);
3038 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3039 #ifdef used_to_be
3040 kprintf(" -> pmap %p, va %x, flags %x",
3041 (void *)pv->pv_pmap, pv->pv_va, pv->pv_flags);
3042 #endif
3043 kprintf(" -> pmap %p, va %x", (void *)pv->pv_pmap, pv->pv_va);
3044 pads(pv->pv_pmap);
3046 kprintf(" ");
3048 #endif