Get rid of some conditionalized code which the pmap invalidation API took
[dragonfly.git] / sys / platform / pc32 / i386 / pmap.c
1 /*
2  * Copyright (c) 1991 Regents of the University of California.
3  * All rights reserved.
4  * Copyright (c) 1994 John S. Dyson
5  * All rights reserved.
6  * Copyright (c) 1994 David Greenman
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * the Systems Programming Group of the University of Utah Computer
11  * Science Department and William Jolitz of UUNET Technologies Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * 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 the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *      This product includes software developed by the University of
24  *      California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
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 $
43  * $DragonFly: src/sys/platform/pc32/i386/pmap.c,v 1.46 2004/09/26 01:53:51 dillon Exp $
44  */
45
46 /*
47  *      Manages physical address maps.
48  *
49  *      In addition to hardware address maps, this
50  *      module is called upon to provide software-use-only
51  *      maps which may or may not be stored in the same
52  *      form as hardware maps.  These pseudo-maps are
53  *      used to store intermediate results from copy
54  *      operations to and from address spaces.
55  *
56  *      Since the information managed by this module is
57  *      also stored by the logical address mapping module,
58  *      this module may throw away valid virtual-to-physical
59  *      mappings at almost any time.  However, invalidations
60  *      of virtual-to-physical mappings must be done as
61  *      requested.
62  *
63  *      In order to cope with hardware architectures which
64  *      make virtual-to-physical map invalidates expensive,
65  *      this module may delay invalidate or reduced protection
66  *      operations until such time as they are actually
67  *      necessary.  This module is given full information as
68  *      to which processors are currently using which maps,
69  *      and to when physical maps must be made correct.
70  */
71
72 #include "opt_disable_pse.h"
73 #include "opt_pmap.h"
74 #include "opt_msgbuf.h"
75
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/proc.h>
80 #include <sys/msgbuf.h>
81 #include <sys/vmmeter.h>
82 #include <sys/mman.h>
83
84 #include <vm/vm.h>
85 #include <vm/vm_param.h>
86 #include <sys/sysctl.h>
87 #include <sys/lock.h>
88 #include <vm/vm_kern.h>
89 #include <vm/vm_page.h>
90 #include <vm/vm_map.h>
91 #include <vm/vm_object.h>
92 #include <vm/vm_extern.h>
93 #include <vm/vm_pageout.h>
94 #include <vm/vm_pager.h>
95 #include <vm/vm_zone.h>
96
97 #include <sys/user.h>
98 #include <sys/thread2.h>
99
100 #include <machine/cputypes.h>
101 #include <machine/md_var.h>
102 #include <machine/specialreg.h>
103 #if defined(SMP) || defined(APIC_IO)
104 #include <machine/smp.h>
105 #include <machine/apicreg.h>
106 #endif /* SMP || APIC_IO */
107 #include <machine/globaldata.h>
108 #include <machine/pmap.h>
109 #include <machine/pmap_inval.h>
110
111 #define PMAP_KEEP_PDIRS
112 #ifndef PMAP_SHPGPERPROC
113 #define PMAP_SHPGPERPROC 200
114 #endif
115
116 #if defined(DIAGNOSTIC)
117 #define PMAP_DIAGNOSTIC
118 #endif
119
120 #define MINPV 2048
121
122 #if !defined(PMAP_DIAGNOSTIC)
123 #define PMAP_INLINE __inline
124 #else
125 #define PMAP_INLINE
126 #endif
127
128 /*
129  * Get PDEs and PTEs for user/kernel address space
130  */
131 #define pmap_pde(m, v)  (&((m)->pm_pdir[(vm_offset_t)(v) >> PDRSHIFT]))
132 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
133
134 #define pmap_pde_v(pte)         ((*(int *)pte & PG_V) != 0)
135 #define pmap_pte_w(pte)         ((*(int *)pte & PG_W) != 0)
136 #define pmap_pte_m(pte)         ((*(int *)pte & PG_M) != 0)
137 #define pmap_pte_u(pte)         ((*(int *)pte & PG_A) != 0)
138 #define pmap_pte_v(pte)         ((*(int *)pte & PG_V) != 0)
139
140
141 /*
142  * Given a map and a machine independent protection code,
143  * convert to a vax protection code.
144  */
145 #define pte_prot(m, p)          \
146         (protection_codes[p & (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)])
147 static int protection_codes[8];
148
149 static struct pmap kernel_pmap_store;
150 pmap_t kernel_pmap;
151
152 vm_paddr_t avail_start; /* PA of first available physical page */
153 vm_paddr_t avail_end;           /* PA of last available physical page */
154 vm_offset_t virtual_avail;      /* VA of first avail page (after kernel bss) */
155 vm_offset_t virtual_end;        /* VA of last avail page (end of kernel AS) */
156 static boolean_t pmap_initialized = FALSE;      /* Has pmap_init completed? */
157 static int pgeflag;             /* PG_G or-in */
158 static int pseflag;             /* PG_PS or-in */
159
160 static vm_object_t kptobj;
161
162 static int nkpt;
163 vm_offset_t kernel_vm_end;
164
165 /*
166  * Data for the pv entry allocation mechanism
167  */
168 static vm_zone_t pvzone;
169 static struct vm_zone pvzone_store;
170 static struct vm_object pvzone_obj;
171 static int pv_entry_count=0, pv_entry_max=0, pv_entry_high_water=0;
172 static int pmap_pagedaemon_waken = 0;
173 static struct pv_entry *pvinit;
174
175 /*
176  * All those kernel PT submaps that BSD is so fond of
177  */
178 pt_entry_t *CMAP1 = 0, *ptmmap;
179 caddr_t CADDR1 = 0, ptvmmap = 0;
180 static pt_entry_t *msgbufmap;
181 struct msgbuf *msgbufp=0;
182
183 /*
184  * Crashdump maps.
185  */
186 static pt_entry_t *pt_crashdumpmap;
187 static caddr_t crashdumpmap;
188
189 extern pt_entry_t *SMPpt;
190
191 static PMAP_INLINE void free_pv_entry (pv_entry_t pv);
192 static unsigned * get_ptbase (pmap_t pmap);
193 static pv_entry_t get_pv_entry (void);
194 static void     i386_protection_init (void);
195 static __inline void    pmap_changebit (vm_page_t m, int bit, boolean_t setem);
196
197 static void     pmap_remove_all (vm_page_t m);
198 static vm_page_t pmap_enter_quick (pmap_t pmap, vm_offset_t va,
199                                       vm_page_t m, vm_page_t mpte);
200 static int pmap_remove_pte (struct pmap *pmap, unsigned *ptq, 
201                                 vm_offset_t sva, pmap_inval_info_t info);
202 static void pmap_remove_page (struct pmap *pmap, 
203                                 vm_offset_t va, pmap_inval_info_t info);
204 static int pmap_remove_entry (struct pmap *pmap, vm_page_t m,
205                                 vm_offset_t va, pmap_inval_info_t info);
206 static boolean_t pmap_testbit (vm_page_t m, int bit);
207 static void pmap_insert_entry (pmap_t pmap, vm_offset_t va,
208                 vm_page_t mpte, vm_page_t m);
209
210 static vm_page_t pmap_allocpte (pmap_t pmap, vm_offset_t va);
211
212 static int pmap_release_free_page (pmap_t pmap, vm_page_t p);
213 static vm_page_t _pmap_allocpte (pmap_t pmap, unsigned ptepindex);
214 static unsigned * pmap_pte_quick (pmap_t pmap, vm_offset_t va);
215 static vm_page_t pmap_page_lookup (vm_object_t object, vm_pindex_t pindex);
216 static int pmap_unuse_pt (pmap_t, vm_offset_t, vm_page_t, pmap_inval_info_t);
217 static vm_offset_t pmap_kmem_choose(vm_offset_t addr);
218
219 static unsigned pdir4mb;
220
221 /*
222  * Move the kernel virtual free pointer to the next
223  * 4MB.  This is used to help improve performance
224  * by using a large (4MB) page for much of the kernel
225  * (.text, .data, .bss)
226  */
227 static vm_offset_t
228 pmap_kmem_choose(vm_offset_t addr)
229 {
230         vm_offset_t newaddr = addr;
231 #ifndef DISABLE_PSE
232         if (cpu_feature & CPUID_PSE) {
233                 newaddr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
234         }
235 #endif
236         return newaddr;
237 }
238
239 /*
240  * pmap_pte:
241  *
242  *      Extract the page table entry associated with the given map/virtual
243  *      pair.
244  *
245  *      This function may NOT be called from an interrupt.
246  */
247 PMAP_INLINE unsigned *
248 pmap_pte(pmap_t pmap, vm_offset_t va)
249 {
250         unsigned *pdeaddr;
251
252         if (pmap) {
253                 pdeaddr = (unsigned *) pmap_pde(pmap, va);
254                 if (*pdeaddr & PG_PS)
255                         return pdeaddr;
256                 if (*pdeaddr) {
257                         return get_ptbase(pmap) + i386_btop(va);
258                 }
259         }
260         return (0);
261 }
262
263 /*
264  * pmap_pte_quick:
265  *
266  *      Super fast pmap_pte routine best used when scanning the pv lists.
267  *      This eliminates many course-grained invltlb calls.  Note that many of
268  *      the pv list scans are across different pmaps and it is very wasteful
269  *      to do an entire invltlb when checking a single mapping.
270  *
271  *      Should only be called while splvm() is held or from a critical
272  *      section.
273  */
274 static unsigned * 
275 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
276 {
277         struct mdglobaldata *gd = mdcpu;
278         unsigned pde, newpf;
279
280         if ((pde = (unsigned) pmap->pm_pdir[va >> PDRSHIFT]) != 0) {
281                 unsigned frame = (unsigned) pmap->pm_pdir[PTDPTDI] & PG_FRAME;
282                 unsigned index = i386_btop(va);
283                 /* are we current address space or kernel? */
284                 if ((pmap == kernel_pmap) ||
285                         (frame == (((unsigned) PTDpde) & PG_FRAME))) {
286                         return (unsigned *) PTmap + index;
287                 }
288                 newpf = pde & PG_FRAME;
289                 if ( ((* (unsigned *) gd->gd_PMAP1) & PG_FRAME) != newpf) {
290                         * (unsigned *) gd->gd_PMAP1 = newpf | PG_RW | PG_V;
291                         cpu_invlpg(gd->gd_PADDR1);
292                 }
293                 return gd->gd_PADDR1 + ((unsigned) index & (NPTEPG - 1));
294         }
295         return (0);
296 }
297
298
299 /*
300  *      Bootstrap the system enough to run with virtual memory.
301  *
302  *      On the i386 this is called after mapping has already been enabled
303  *      and just syncs the pmap module with what has already been done.
304  *      [We can't call it easily with mapping off since the kernel is not
305  *      mapped with PA == VA, hence we would have to relocate every address
306  *      from the linked base (virtual) address "KERNBASE" to the actual
307  *      (physical) address starting relative to 0]
308  */
309 void
310 pmap_bootstrap(firstaddr, loadaddr)
311         vm_paddr_t firstaddr;
312         vm_paddr_t loadaddr;
313 {
314         vm_offset_t va;
315         pt_entry_t *pte;
316         struct mdglobaldata *gd;
317         int i;
318
319         avail_start = firstaddr;
320
321         /*
322          * XXX The calculation of virtual_avail is wrong. It's NKPT*PAGE_SIZE too
323          * large. It should instead be correctly calculated in locore.s and
324          * not based on 'first' (which is a physical address, not a virtual
325          * address, for the start of unused physical memory). The kernel
326          * page tables are NOT double mapped and thus should not be included
327          * in this calculation.
328          */
329         virtual_avail = (vm_offset_t) KERNBASE + firstaddr;
330         virtual_avail = pmap_kmem_choose(virtual_avail);
331
332         virtual_end = VM_MAX_KERNEL_ADDRESS;
333
334         /*
335          * Initialize protection array.
336          */
337         i386_protection_init();
338
339         /*
340          * The kernel's pmap is statically allocated so we don't have to use
341          * pmap_create, which is unlikely to work correctly at this part of
342          * the boot sequence (XXX and which no longer exists).
343          */
344         kernel_pmap = &kernel_pmap_store;
345
346         kernel_pmap->pm_pdir = (pd_entry_t *)(KERNBASE + (u_int)IdlePTD);
347         kernel_pmap->pm_count = 1;
348         kernel_pmap->pm_active = (cpumask_t)-1; /* don't allow deactivation */
349         TAILQ_INIT(&kernel_pmap->pm_pvlist);
350         nkpt = NKPT;
351
352         /*
353          * Reserve some special page table entries/VA space for temporary
354          * mapping of pages.
355          */
356 #define SYSMAP(c, p, v, n)      \
357         v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
358
359         va = virtual_avail;
360         pte = (pt_entry_t *) pmap_pte(kernel_pmap, va);
361
362         /*
363          * CMAP1/CMAP2 are used for zeroing and copying pages.
364          */
365         SYSMAP(caddr_t, CMAP1, CADDR1, 1)
366
367         /*
368          * Crashdump maps.
369          */
370         SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS);
371
372         /*
373          * ptvmmap is used for reading arbitrary physical pages via
374          * /dev/mem.
375          */
376         SYSMAP(caddr_t, ptmmap, ptvmmap, 1)
377
378         /*
379          * msgbufp is used to map the system message buffer.
380          * XXX msgbufmap is not used.
381          */
382         SYSMAP(struct msgbuf *, msgbufmap, msgbufp,
383                atop(round_page(MSGBUF_SIZE)))
384
385         virtual_avail = va;
386
387         *(int *) CMAP1 = 0;
388         for (i = 0; i < NKPT; i++)
389                 PTD[i] = 0;
390
391         /*
392          * PG_G is terribly broken on SMP because we IPI invltlb's in some
393          * cases rather then invl1pg.  Actually, I don't even know why it
394          * works under UP because self-referential page table mappings
395          */
396 #ifdef SMP
397         pgeflag = 0;
398 #else
399         if (cpu_feature & CPUID_PGE)
400                 pgeflag = PG_G;
401 #endif
402         
403 /*
404  * Initialize the 4MB page size flag
405  */
406         pseflag = 0;
407 /*
408  * The 4MB page version of the initial
409  * kernel page mapping.
410  */
411         pdir4mb = 0;
412
413 #if !defined(DISABLE_PSE)
414         if (cpu_feature & CPUID_PSE) {
415                 unsigned ptditmp;
416                 /*
417                  * Note that we have enabled PSE mode
418                  */
419                 pseflag = PG_PS;
420                 ptditmp = *((unsigned *)PTmap + i386_btop(KERNBASE));
421                 ptditmp &= ~(NBPDR - 1);
422                 ptditmp |= PG_V | PG_RW | PG_PS | PG_U | pgeflag;
423                 pdir4mb = ptditmp;
424
425 #ifndef SMP
426                 /*
427                  * Enable the PSE mode.  If we are SMP we can't do this
428                  * now because the APs will not be able to use it when
429                  * they boot up.
430                  */
431                 load_cr4(rcr4() | CR4_PSE);
432
433                 /*
434                  * We can do the mapping here for the single processor
435                  * case.  We simply ignore the old page table page from
436                  * now on.
437                  */
438                 /*
439                  * For SMP, we still need 4K pages to bootstrap APs,
440                  * PSE will be enabled as soon as all APs are up.
441                  */
442                 PTD[KPTDI] = (pd_entry_t)ptditmp;
443                 kernel_pmap->pm_pdir[KPTDI] = (pd_entry_t)ptditmp;
444                 cpu_invltlb();
445 #endif
446         }
447 #endif
448 #ifdef APIC_IO
449         if (cpu_apic_address == 0)
450                 panic("pmap_bootstrap: no local apic!");
451
452         /* local apic is mapped on last page */
453         SMPpt[NPTEPG - 1] = (pt_entry_t)(PG_V | PG_RW | PG_N | pgeflag |
454             (cpu_apic_address & PG_FRAME));
455 #endif
456
457         /* BSP does this itself, AP's get it pre-set */
458         gd = &CPU_prvspace[0].mdglobaldata;
459         gd->gd_CMAP1 = &SMPpt[1];
460         gd->gd_CMAP2 = &SMPpt[2];
461         gd->gd_CMAP3 = &SMPpt[3];
462         gd->gd_PMAP1 = &SMPpt[4];
463         gd->gd_CADDR1 = CPU_prvspace[0].CPAGE1;
464         gd->gd_CADDR2 = CPU_prvspace[0].CPAGE2;
465         gd->gd_CADDR3 = CPU_prvspace[0].CPAGE3;
466         gd->gd_PADDR1 = (unsigned *)CPU_prvspace[0].PPAGE1;
467
468         cpu_invltlb();
469 }
470
471 #ifdef SMP
472 /*
473  * Set 4mb pdir for mp startup
474  */
475 void
476 pmap_set_opt(void)
477 {
478         if (pseflag && (cpu_feature & CPUID_PSE)) {
479                 load_cr4(rcr4() | CR4_PSE);
480                 if (pdir4mb && mycpu->gd_cpuid == 0) {  /* only on BSP */
481                         kernel_pmap->pm_pdir[KPTDI] =
482                             PTD[KPTDI] = (pd_entry_t)pdir4mb;
483                         cpu_invltlb();
484                 }
485         }
486 }
487 #endif
488
489 /*
490  *      Initialize the pmap module.
491  *      Called by vm_init, to initialize any structures that the pmap
492  *      system needs to map virtual memory.
493  *      pmap_init has been enhanced to support in a fairly consistant
494  *      way, discontiguous physical memory.
495  */
496 void
497 pmap_init(void)
498 {
499         int i;
500         int initial_pvs;
501
502         /*
503          * object for kernel page table pages
504          */
505         kptobj = vm_object_allocate(OBJT_DEFAULT, NKPDE);
506
507         /*
508          * Allocate memory for random pmap data structures.  Includes the
509          * pv_head_table.
510          */
511
512         for(i = 0; i < vm_page_array_size; i++) {
513                 vm_page_t m;
514
515                 m = &vm_page_array[i];
516                 TAILQ_INIT(&m->md.pv_list);
517                 m->md.pv_list_count = 0;
518         }
519
520         /*
521          * init the pv free list
522          */
523         initial_pvs = vm_page_array_size;
524         if (initial_pvs < MINPV)
525                 initial_pvs = MINPV;
526         pvzone = &pvzone_store;
527         pvinit = (struct pv_entry *) kmem_alloc(kernel_map,
528                 initial_pvs * sizeof (struct pv_entry));
529         zbootinit(pvzone, "PV ENTRY", sizeof (struct pv_entry), pvinit,
530             vm_page_array_size);
531
532         /*
533          * Now it is safe to enable pv_table recording.
534          */
535         pmap_initialized = TRUE;
536 }
537
538 /*
539  * Initialize the address space (zone) for the pv_entries.  Set a
540  * high water mark so that the system can recover from excessive
541  * numbers of pv entries.
542  */
543 void
544 pmap_init2()
545 {
546         int shpgperproc = PMAP_SHPGPERPROC;
547
548         TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
549         pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
550         TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
551         pv_entry_high_water = 9 * (pv_entry_max / 10);
552         zinitna(pvzone, &pvzone_obj, NULL, 0, pv_entry_max, ZONE_INTERRUPT, 1);
553 }
554
555
556 /***************************************************
557  * Low level helper routines.....
558  ***************************************************/
559
560 #if defined(PMAP_DIAGNOSTIC)
561
562 /*
563  * This code checks for non-writeable/modified pages.
564  * This should be an invalid condition.
565  */
566 static int
567 pmap_nw_modified(pt_entry_t ptea)
568 {
569         int pte;
570
571         pte = (int) ptea;
572
573         if ((pte & (PG_M|PG_RW)) == PG_M)
574                 return 1;
575         else
576                 return 0;
577 }
578 #endif
579
580
581 /*
582  * this routine defines the region(s) of memory that should
583  * not be tested for the modified bit.
584  */
585 static PMAP_INLINE int
586 pmap_track_modified(vm_offset_t va)
587 {
588         if ((va < clean_sva) || (va >= clean_eva)) 
589                 return 1;
590         else
591                 return 0;
592 }
593
594 static unsigned *
595 get_ptbase(pmap_t pmap)
596 {
597         unsigned frame = (unsigned) pmap->pm_pdir[PTDPTDI] & PG_FRAME;
598         struct globaldata *gd = mycpu;
599
600         /* are we current address space or kernel? */
601         if (pmap == kernel_pmap || frame == (((unsigned) PTDpde) & PG_FRAME)) {
602                 return (unsigned *) PTmap;
603         }
604
605         /* otherwise, we are alternate address space */
606         KKASSERT(gd->gd_intr_nesting_level == 0 && (gd->gd_curthread->td_flags & TDF_INTTHREAD) == 0);
607
608         if (frame != (((unsigned) APTDpde) & PG_FRAME)) {
609                 APTDpde = (pd_entry_t)(frame | PG_RW | PG_V);
610                 /* The page directory is not shared between CPUs */
611                 cpu_invltlb();
612         }
613         return (unsigned *) APTmap;
614 }
615
616 /*
617  * pmap_extract:
618  *
619  *      Extract the physical page address associated with the map/VA pair.
620  *
621  *      This function may not be called from an interrupt if the pmap is
622  *      not kernel_pmap.
623  */
624 vm_paddr_t 
625 pmap_extract(pmap_t pmap, vm_offset_t va)
626 {
627         vm_offset_t rtval;
628         vm_offset_t pdirindex;
629
630         pdirindex = va >> PDRSHIFT;
631         if (pmap && (rtval = (unsigned) pmap->pm_pdir[pdirindex])) {
632                 unsigned *pte;
633                 if ((rtval & PG_PS) != 0) {
634                         rtval &= ~(NBPDR - 1);
635                         rtval |= va & (NBPDR - 1);
636                         return rtval;
637                 }
638                 pte = get_ptbase(pmap) + i386_btop(va);
639                 rtval = ((*pte & PG_FRAME) | (va & PAGE_MASK));
640                 return rtval;
641         }
642         return 0;
643 }
644
645 /*
646  * Extract user accessible page only, return NULL if the page is not
647  * present or if it's current state is not sufficient.  Caller will
648  * generally call vm_fault() on failure and try again.
649  */
650 vm_page_t
651 pmap_extract_vmpage(pmap_t pmap, vm_offset_t va, int prot)
652 {
653         vm_offset_t rtval;
654         vm_offset_t pdirindex;
655
656         pdirindex = va >> PDRSHIFT;
657         if (pmap && (rtval = (unsigned) pmap->pm_pdir[pdirindex])) {
658                 unsigned *pte;
659                 vm_page_t m;
660
661                 if ((rtval & PG_PS) != 0) {
662                         if ((rtval & (PG_V|PG_U)) != (PG_V|PG_U))
663                                 return (NULL);
664                         if ((prot & VM_PROT_WRITE) && (rtval & PG_RW) == 0)
665                                 return (NULL);
666                         rtval &= ~(NBPDR - 1);
667                         rtval |= va & (NBPDR - 1);
668                         m = PHYS_TO_VM_PAGE(rtval);
669                 } else {
670                         pte = get_ptbase(pmap) + i386_btop(va);
671                         if ((*pte & (PG_V|PG_U)) != (PG_V|PG_U))
672                                 return (NULL);
673                         if ((prot & VM_PROT_WRITE) && (*pte & PG_RW) == 0)
674                                 return (NULL);
675                         rtval = ((*pte & PG_FRAME) | (va & PAGE_MASK));
676                         m = PHYS_TO_VM_PAGE(rtval);
677                 }
678                 return(m);
679         }
680         return (NULL);
681 }
682
683 /***************************************************
684  * Low level mapping routines.....
685  ***************************************************/
686
687 /*
688  * Routine: pmap_kenter
689  * Function:
690  *      Add a wired page to the KVA
691  *      NOTE! note that in order for the mapping to take effect -- you
692  *      should do an invltlb after doing the pmap_kenter().
693  */
694 void 
695 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
696 {
697         unsigned *pte;
698         unsigned npte;
699         pmap_inval_info info;
700
701         pmap_inval_init(&info);
702         pmap_inval_add(&info, kernel_pmap, va);
703         npte = pa | PG_RW | PG_V | pgeflag;
704         pte = (unsigned *)vtopte(va);
705         *pte = npte;
706         pmap_inval_flush(&info);
707 }
708
709 /*
710  * Routine: pmap_kenter_quick
711  * Function:
712  *      Similar to pmap_kenter(), except we only invalidate the
713  *      mapping on the current CPU.
714  */
715 void
716 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
717 {
718         unsigned *pte;
719         unsigned npte;
720
721         npte = pa | PG_RW | PG_V | pgeflag;
722         pte = (unsigned *)vtopte(va);
723         *pte = npte;
724         cpu_invlpg((void *)va);
725 }
726
727 void
728 pmap_kenter_sync(vm_offset_t va)
729 {
730         pmap_inval_info info;
731
732         pmap_inval_init(&info);
733         pmap_inval_add(&info, kernel_pmap, va);
734         pmap_inval_flush(&info);
735 }
736
737 void
738 pmap_kenter_sync_quick(vm_offset_t va)
739 {
740         cpu_invlpg((void *)va);
741 }
742
743 /*
744  * remove a page from the kernel pagetables
745  */
746 void
747 pmap_kremove(vm_offset_t va)
748 {
749         unsigned *pte;
750         pmap_inval_info info;
751
752         pmap_inval_init(&info);
753         pmap_inval_add(&info, kernel_pmap, va);
754         pte = (unsigned *)vtopte(va);
755         *pte = 0;
756         pmap_inval_flush(&info);
757 }
758
759 void
760 pmap_kremove_quick(vm_offset_t va)
761 {
762         unsigned *pte;
763         pte = (unsigned *)vtopte(va);
764         *pte = 0;
765         cpu_invlpg((void *)va);
766 }
767
768 /*
769  *      Used to map a range of physical addresses into kernel
770  *      virtual address space.
771  *
772  *      For now, VM is already on, we only need to map the
773  *      specified memory.
774  */
775 vm_offset_t
776 pmap_map(vm_offset_t virt, vm_paddr_t start, vm_paddr_t end, int prot)
777 {
778         while (start < end) {
779                 pmap_kenter(virt, start);
780                 virt += PAGE_SIZE;
781                 start += PAGE_SIZE;
782         }
783         return (virt);
784 }
785
786
787 /*
788  * Add a list of wired pages to the kva
789  * this routine is only used for temporary
790  * kernel mappings that do not need to have
791  * page modification or references recorded.
792  * Note that old mappings are simply written
793  * over.  The page *must* be wired.
794  */
795 void
796 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
797 {
798         vm_offset_t end_va;
799
800         end_va = va + count * PAGE_SIZE;
801                 
802         while (va < end_va) {
803                 unsigned *pte;
804
805                 pte = (unsigned *)vtopte(va);
806                 *pte = VM_PAGE_TO_PHYS(*m) | PG_RW | PG_V | pgeflag;
807                 cpu_invlpg((void *)va);
808                 va += PAGE_SIZE;
809                 m++;
810         }
811 #ifdef SMP
812         smp_invltlb();  /* XXX */
813 #endif
814 }
815
816 void
817 pmap_qenter2(vm_offset_t va, vm_page_t *m, int count, cpumask_t *mask)
818 {
819         vm_offset_t end_va;
820         cpumask_t cmask = mycpu->gd_cpumask;
821
822         end_va = va + count * PAGE_SIZE;
823
824         while (va < end_va) {
825                 unsigned *pte;
826                 unsigned pteval;
827
828                 /*
829                  * Install the new PTE.  If the pte changed from the prior
830                  * mapping we must reset the cpu mask and invalidate the page.
831                  * If the pte is the same but we have not seen it on the
832                  * current cpu, invlpg the existing mapping.  Otherwise the
833                  * entry is optimal and no invalidation is required.
834                  */
835                 pte = (unsigned *)vtopte(va);
836                 pteval = VM_PAGE_TO_PHYS(*m) | PG_A | PG_RW | PG_V | pgeflag;
837                 if (*pte != pteval) {
838                         *mask = 0;
839                         *pte = pteval;
840                         cpu_invlpg((void *)va);
841                 } else if ((*mask & cmask) == 0) {
842                         cpu_invlpg((void *)va);
843                 }
844                 va += PAGE_SIZE;
845                 m++;
846         }
847         *mask |= cmask;
848 }
849
850 /*
851  * this routine jerks page mappings from the
852  * kernel -- it is meant only for temporary mappings.
853  */
854 void
855 pmap_qremove(vm_offset_t va, int count)
856 {
857         vm_offset_t end_va;
858
859         end_va = va + count*PAGE_SIZE;
860
861         while (va < end_va) {
862                 unsigned *pte;
863
864                 pte = (unsigned *)vtopte(va);
865                 *pte = 0;
866                 cpu_invlpg((void *)va);
867                 va += PAGE_SIZE;
868         }
869 #ifdef SMP
870         smp_invltlb();
871 #endif
872 }
873
874 /*
875  * This routine works like vm_page_lookup() but also blocks as long as the
876  * page is busy.  This routine does not busy the page it returns.
877  *
878  * Unless the caller is managing objects whos pages are in a known state,
879  * the call should be made at splvm() so the page's object association
880  * remains valid on return.
881  */
882 static vm_page_t
883 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
884 {
885         vm_page_t m;
886
887 retry:
888         m = vm_page_lookup(object, pindex);
889         if (m && vm_page_sleep_busy(m, FALSE, "pplookp"))
890                 goto retry;
891         return(m);
892 }
893
894 /*
895  * Create a new thread and optionally associate it with a (new) process.
896  * NOTE! the new thread's cpu may not equal the current cpu.
897  */
898 void
899 pmap_init_thread(thread_t td)
900 {
901         /* enforce pcb placement */
902         td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
903         td->td_savefpu = &td->td_pcb->pcb_save;
904         td->td_sp = (char *)td->td_pcb - 16;
905 }
906
907 /*
908  * Create the UPAGES for a new process.
909  * This routine directly affects the fork perf for a process.
910  */
911 void
912 pmap_init_proc(struct proc *p, struct thread *td)
913 {
914         p->p_addr = (void *)td->td_kstack;
915         p->p_thread = td;
916         td->td_proc = p;
917         td->td_switch = cpu_heavy_switch;
918 #ifdef SMP
919         td->td_mpcount = 1;
920 #endif
921         bzero(p->p_addr, sizeof(*p->p_addr));
922 }
923
924 /*
925  * Dispose the UPAGES for a process that has exited.
926  * This routine directly impacts the exit perf of a process.
927  */
928 struct thread *
929 pmap_dispose_proc(struct proc *p)
930 {
931         struct thread *td;
932
933         KASSERT(p->p_lock == 0, ("attempt to dispose referenced proc! %p", p));
934
935         if ((td = p->p_thread) != NULL) {
936             p->p_thread = NULL;
937             td->td_proc = NULL;
938         }
939         p->p_addr = NULL;
940         return(td);
941 }
942
943 /*
944  * Allow the UPAGES for a process to be prejudicially paged out.
945  */
946 void
947 pmap_swapout_proc(struct proc *p)
948 {
949 #if 0
950         int i;
951         int s;
952         vm_object_t upobj;
953         vm_page_t m;
954
955         upobj = p->p_upages_obj;
956
957         /*
958          * Unwiring the pages allow them to be paged to their backing store
959          * (swap).
960          *
961          * splvm() protection not required since nobody will be messing with
962          * the pages but us.
963          */
964         for (i = 0; i < UPAGES; i++) {
965                 if ((m = vm_page_lookup(upobj, i)) == NULL)
966                         panic("pmap_swapout_proc: upage already missing???");
967                 vm_page_dirty(m);
968                 vm_page_unwire(m, 0);
969                 pmap_kremove((vm_offset_t)p->p_addr + (PAGE_SIZE * i));
970         }
971 #endif
972 }
973
974 /*
975  * Bring the UPAGES for a specified process back in.
976  */
977 void
978 pmap_swapin_proc(struct proc *p)
979 {
980 #if 0
981         int i,rv;
982         vm_object_t upobj;
983         vm_page_t m;
984
985         /*
986          * splvm() protection not required since nobody will be messing with
987          * the pages but us.
988          */
989         upobj = p->p_upages_obj;
990         for (i = 0; i < UPAGES; i++) {
991                 m = vm_page_grab(upobj, i, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
992
993                 pmap_kenter((vm_offset_t)p->p_addr + (i * PAGE_SIZE),
994                         VM_PAGE_TO_PHYS(m));
995
996                 if (m->valid != VM_PAGE_BITS_ALL) {
997                         rv = vm_pager_get_pages(upobj, &m, 1, 0);
998                         if (rv != VM_PAGER_OK)
999                                 panic("pmap_swapin_proc: cannot get upages for proc: %d\n", p->p_pid);
1000                         m = vm_page_lookup(upobj, i);
1001                         m->valid = VM_PAGE_BITS_ALL;
1002                 }
1003                 vm_page_wire(m);
1004                 vm_page_wakeup(m);
1005                 vm_page_flag_set(m, PG_MAPPED | PG_WRITEABLE);
1006         }
1007 #endif
1008 }
1009
1010 /***************************************************
1011  * Page table page management routines.....
1012  ***************************************************/
1013
1014 /*
1015  * This routine unholds page table pages, and if the hold count
1016  * drops to zero, then it decrements the wire count.
1017  */
1018 static int 
1019 _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, pmap_inval_info_t info) 
1020 {
1021         pmap_inval_flush(info);
1022         while (vm_page_sleep_busy(m, FALSE, "pmuwpt"))
1023                 ;
1024
1025         if (m->hold_count == 0) {
1026                 /*
1027                  * unmap the page table page
1028                  */
1029                 pmap_inval_add(info, pmap, -1);
1030                 pmap->pm_pdir[m->pindex] = 0;
1031                 --pmap->pm_stats.resident_count;
1032
1033                 if (pmap->pm_ptphint == m)
1034                         pmap->pm_ptphint = NULL;
1035
1036                 /*
1037                  * If the page is finally unwired, simply free it.
1038                  */
1039                 --m->wire_count;
1040                 if (m->wire_count == 0) {
1041                         vm_page_flash(m);
1042                         vm_page_busy(m);
1043                         vm_page_free_zero(m);
1044                         --vmstats.v_wire_count;
1045                 }
1046                 return 1;
1047         }
1048         return 0;
1049 }
1050
1051 static PMAP_INLINE int
1052 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, pmap_inval_info_t info)
1053 {
1054         vm_page_unhold(m);
1055         if (m->hold_count == 0)
1056                 return _pmap_unwire_pte_hold(pmap, m, info);
1057         else
1058                 return 0;
1059 }
1060
1061 /*
1062  * After removing a page table entry, this routine is used to
1063  * conditionally free the page, and manage the hold/wire counts.
1064  */
1065 static int
1066 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte,
1067                 pmap_inval_info_t info)
1068 {
1069         unsigned ptepindex;
1070         if (va >= UPT_MIN_ADDRESS)
1071                 return 0;
1072
1073         if (mpte == NULL) {
1074                 ptepindex = (va >> PDRSHIFT);
1075                 if (pmap->pm_ptphint &&
1076                         (pmap->pm_ptphint->pindex == ptepindex)) {
1077                         mpte = pmap->pm_ptphint;
1078                 } else {
1079                         pmap_inval_flush(info);
1080                         mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1081                         pmap->pm_ptphint = mpte;
1082                 }
1083         }
1084
1085         return pmap_unwire_pte_hold(pmap, mpte, info);
1086 }
1087
1088 void
1089 pmap_pinit0(struct pmap *pmap)
1090 {
1091         pmap->pm_pdir =
1092                 (pd_entry_t *)kmem_alloc_pageable(kernel_map, PAGE_SIZE);
1093         pmap_kenter((vm_offset_t)pmap->pm_pdir, (vm_offset_t) IdlePTD);
1094         pmap->pm_count = 1;
1095         pmap->pm_active = 0;
1096         pmap->pm_ptphint = NULL;
1097         TAILQ_INIT(&pmap->pm_pvlist);
1098         bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1099 }
1100
1101 /*
1102  * Initialize a preallocated and zeroed pmap structure,
1103  * such as one in a vmspace structure.
1104  */
1105 void
1106 pmap_pinit(struct pmap *pmap)
1107 {
1108         vm_page_t ptdpg;
1109
1110         /*
1111          * No need to allocate page table space yet but we do need a valid
1112          * page directory table.
1113          */
1114         if (pmap->pm_pdir == NULL) {
1115                 pmap->pm_pdir =
1116                         (pd_entry_t *)kmem_alloc_pageable(kernel_map, PAGE_SIZE);
1117         }
1118
1119         /*
1120          * allocate object for the ptes
1121          */
1122         if (pmap->pm_pteobj == NULL)
1123                 pmap->pm_pteobj = vm_object_allocate( OBJT_DEFAULT, PTDPTDI + 1);
1124
1125         /*
1126          * allocate the page directory page
1127          */
1128         ptdpg = vm_page_grab( pmap->pm_pteobj, PTDPTDI,
1129                         VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1130
1131         ptdpg->wire_count = 1;
1132         ++vmstats.v_wire_count;
1133
1134
1135         vm_page_flag_clear(ptdpg, PG_MAPPED | PG_BUSY); /* not usually mapped*/
1136         ptdpg->valid = VM_PAGE_BITS_ALL;
1137
1138         pmap_kenter((vm_offset_t)pmap->pm_pdir, VM_PAGE_TO_PHYS(ptdpg));
1139         if ((ptdpg->flags & PG_ZERO) == 0)
1140                 bzero(pmap->pm_pdir, PAGE_SIZE);
1141
1142         pmap->pm_pdir[MPPTDI] = PTD[MPPTDI];
1143
1144         /* install self-referential address mapping entry */
1145         *(unsigned *) (pmap->pm_pdir + PTDPTDI) =
1146                 VM_PAGE_TO_PHYS(ptdpg) | PG_V | PG_RW | PG_A | PG_M;
1147
1148         pmap->pm_count = 1;
1149         pmap->pm_active = 0;
1150         pmap->pm_ptphint = NULL;
1151         TAILQ_INIT(&pmap->pm_pvlist);
1152         bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1153 }
1154
1155 /*
1156  * Wire in kernel global address entries.  To avoid a race condition
1157  * between pmap initialization and pmap_growkernel, this procedure
1158  * should be called after the vmspace is attached to the process
1159  * but before this pmap is activated.
1160  */
1161 void
1162 pmap_pinit2(struct pmap *pmap)
1163 {
1164         /* XXX copies current process, does not fill in MPPTDI */
1165         bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * PTESIZE);
1166 }
1167
1168 static int
1169 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1170 {
1171         unsigned *pde = (unsigned *) pmap->pm_pdir;
1172         /*
1173          * This code optimizes the case of freeing non-busy
1174          * page-table pages.  Those pages are zero now, and
1175          * might as well be placed directly into the zero queue.
1176          */
1177         if (vm_page_sleep_busy(p, FALSE, "pmaprl"))
1178                 return 0;
1179
1180         vm_page_busy(p);
1181
1182         /*
1183          * Remove the page table page from the processes address space.
1184          */
1185         pde[p->pindex] = 0;
1186         pmap->pm_stats.resident_count--;
1187
1188         if (p->hold_count)  {
1189                 panic("pmap_release: freeing held page table page");
1190         }
1191         /*
1192          * Page directory pages need to have the kernel
1193          * stuff cleared, so they can go into the zero queue also.
1194          */
1195         if (p->pindex == PTDPTDI) {
1196                 bzero(pde + KPTDI, nkpt * PTESIZE);
1197                 pde[MPPTDI] = 0;
1198                 pde[APTDPTDI] = 0;
1199                 pmap_kremove((vm_offset_t)pmap->pm_pdir);
1200         }
1201
1202         if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1203                 pmap->pm_ptphint = NULL;
1204
1205         p->wire_count--;
1206         vmstats.v_wire_count--;
1207         vm_page_free_zero(p);
1208         return 1;
1209 }
1210
1211 /*
1212  * this routine is called if the page table page is not
1213  * mapped correctly.
1214  */
1215 static vm_page_t
1216 _pmap_allocpte(pmap_t pmap, unsigned ptepindex)
1217 {
1218         vm_offset_t pteva, ptepa;
1219         vm_page_t m;
1220
1221         /*
1222          * Find or fabricate a new pagetable page
1223          */
1224         m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1225                         VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1226
1227         KASSERT(m->queue == PQ_NONE,
1228                 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1229
1230         if (m->wire_count == 0)
1231                 vmstats.v_wire_count++;
1232         m->wire_count++;
1233
1234         /*
1235          * Increment the hold count for the page table page
1236          * (denoting a new mapping.)
1237          */
1238         m->hold_count++;
1239
1240         /*
1241          * Map the pagetable page into the process address space, if
1242          * it isn't already there.
1243          */
1244
1245         pmap->pm_stats.resident_count++;
1246
1247         ptepa = VM_PAGE_TO_PHYS(m);
1248         pmap->pm_pdir[ptepindex] =
1249                 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_A | PG_M);
1250
1251         /*
1252          * Set the page table hint
1253          */
1254         pmap->pm_ptphint = m;
1255
1256         /*
1257          * Try to use the new mapping, but if we cannot, then
1258          * do it with the routine that maps the page explicitly.
1259          */
1260         if ((m->flags & PG_ZERO) == 0) {
1261                 if ((((unsigned)pmap->pm_pdir[PTDPTDI]) & PG_FRAME) ==
1262                         (((unsigned) PTDpde) & PG_FRAME)) {
1263                         pteva = UPT_MIN_ADDRESS + i386_ptob(ptepindex);
1264                         bzero((caddr_t) pteva, PAGE_SIZE);
1265                 } else {
1266                         pmap_zero_page(ptepa);
1267                 }
1268         }
1269
1270         m->valid = VM_PAGE_BITS_ALL;
1271         vm_page_flag_clear(m, PG_ZERO);
1272         vm_page_flag_set(m, PG_MAPPED);
1273         vm_page_wakeup(m);
1274
1275         return m;
1276 }
1277
1278 static vm_page_t
1279 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1280 {
1281         unsigned ptepindex;
1282         vm_offset_t ptepa;
1283         vm_page_t m;
1284
1285         /*
1286          * Calculate pagetable page index
1287          */
1288         ptepindex = va >> PDRSHIFT;
1289
1290         /*
1291          * Get the page directory entry
1292          */
1293         ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1294
1295         /*
1296          * This supports switching from a 4MB page to a
1297          * normal 4K page.
1298          */
1299         if (ptepa & PG_PS) {
1300                 pmap->pm_pdir[ptepindex] = 0;
1301                 ptepa = 0;
1302                 cpu_invltlb();
1303                 smp_invltlb();
1304         }
1305
1306         /*
1307          * If the page table page is mapped, we just increment the
1308          * hold count, and activate it.
1309          */
1310         if (ptepa) {
1311                 /*
1312                  * In order to get the page table page, try the
1313                  * hint first.
1314                  */
1315                 if (pmap->pm_ptphint &&
1316                         (pmap->pm_ptphint->pindex == ptepindex)) {
1317                         m = pmap->pm_ptphint;
1318                 } else {
1319                         m = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1320                         pmap->pm_ptphint = m;
1321                 }
1322                 m->hold_count++;
1323                 return m;
1324         }
1325         /*
1326          * Here if the pte page isn't mapped, or if it has been deallocated.
1327          */
1328         return _pmap_allocpte(pmap, ptepindex);
1329 }
1330
1331
1332 /***************************************************
1333 * Pmap allocation/deallocation routines.
1334  ***************************************************/
1335
1336 /*
1337  * Release any resources held by the given physical map.
1338  * Called when a pmap initialized by pmap_pinit is being released.
1339  * Should only be called if the map contains no valid mappings.
1340  */
1341 void
1342 pmap_release(struct pmap *pmap)
1343 {
1344         vm_page_t p,n,ptdpg;
1345         vm_object_t object = pmap->pm_pteobj;
1346         int curgeneration;
1347         int s;
1348
1349 #if defined(DIAGNOSTIC)
1350         if (object->ref_count != 1)
1351                 panic("pmap_release: pteobj reference count != 1");
1352 #endif
1353         
1354         ptdpg = NULL;
1355 retry:
1356         s = splvm();
1357         curgeneration = object->generation;
1358         for (p = TAILQ_FIRST(&object->memq); p != NULL; p = n) {
1359                 n = TAILQ_NEXT(p, listq);
1360                 if (p->pindex == PTDPTDI) {
1361                         ptdpg = p;
1362                         continue;
1363                 }
1364                 while (1) {
1365                         if (!pmap_release_free_page(pmap, p) &&
1366                             (object->generation != curgeneration)) {
1367                                 splx(s);
1368                                 goto retry;
1369                         }
1370                 }
1371         }
1372         splx(s);
1373
1374         if (ptdpg && !pmap_release_free_page(pmap, ptdpg))
1375                 goto retry;
1376 }
1377 \f
1378 static int
1379 kvm_size(SYSCTL_HANDLER_ARGS)
1380 {
1381         unsigned long ksize = VM_MAX_KERNEL_ADDRESS - KERNBASE;
1382
1383         return sysctl_handle_long(oidp, &ksize, 0, req);
1384 }
1385 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD, 
1386     0, 0, kvm_size, "IU", "Size of KVM");
1387
1388 static int
1389 kvm_free(SYSCTL_HANDLER_ARGS)
1390 {
1391         unsigned long kfree = VM_MAX_KERNEL_ADDRESS - kernel_vm_end;
1392
1393         return sysctl_handle_long(oidp, &kfree, 0, req);
1394 }
1395 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD, 
1396     0, 0, kvm_free, "IU", "Amount of KVM free");
1397
1398 /*
1399  * grow the number of kernel page table entries, if needed
1400  */
1401 void
1402 pmap_growkernel(vm_offset_t addr)
1403 {
1404         struct proc *p;
1405         struct pmap *pmap;
1406         int s;
1407         vm_offset_t ptppaddr;
1408         vm_page_t nkpg;
1409         pd_entry_t newpdir;
1410
1411         s = splhigh();
1412         if (kernel_vm_end == 0) {
1413                 kernel_vm_end = KERNBASE;
1414                 nkpt = 0;
1415                 while (pdir_pde(PTD, kernel_vm_end)) {
1416                         kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1417                         nkpt++;
1418                 }
1419         }
1420         addr = (addr + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1421         while (kernel_vm_end < addr) {
1422                 if (pdir_pde(PTD, kernel_vm_end)) {
1423                         kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1424                         continue;
1425                 }
1426
1427                 /*
1428                  * This index is bogus, but out of the way
1429                  */
1430                 nkpg = vm_page_alloc(kptobj, nkpt, 
1431                         VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM | VM_ALLOC_INTERRUPT);
1432                 if (nkpg == NULL)
1433                         panic("pmap_growkernel: no memory to grow kernel");
1434
1435                 nkpt++;
1436
1437                 vm_page_wire(nkpg);
1438                 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1439                 pmap_zero_page(ptppaddr);
1440                 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
1441                 pdir_pde(PTD, kernel_vm_end) = newpdir;
1442
1443                 FOREACH_PROC_IN_SYSTEM(p) {
1444                         if (p->p_vmspace) {
1445                                 pmap = vmspace_pmap(p->p_vmspace);
1446                                 *pmap_pde(pmap, kernel_vm_end) = newpdir;
1447                         }
1448                 }
1449                 *pmap_pde(kernel_pmap, kernel_vm_end) = newpdir;
1450                 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1451         }
1452         splx(s);
1453 }
1454
1455 /*
1456  *      Retire the given physical map from service.
1457  *      Should only be called if the map contains
1458  *      no valid mappings.
1459  */
1460 void
1461 pmap_destroy(pmap_t pmap)
1462 {
1463         int count;
1464
1465         if (pmap == NULL)
1466                 return;
1467
1468         count = --pmap->pm_count;
1469         if (count == 0) {
1470                 pmap_release(pmap);
1471                 panic("destroying a pmap is not yet implemented");
1472         }
1473 }
1474
1475 /*
1476  *      Add a reference to the specified pmap.
1477  */
1478 void
1479 pmap_reference(pmap_t pmap)
1480 {
1481         if (pmap != NULL) {
1482                 pmap->pm_count++;
1483         }
1484 }
1485
1486 /***************************************************
1487 * page management routines.
1488  ***************************************************/
1489
1490 /*
1491  * free the pv_entry back to the free list.  This function may be
1492  * called from an interrupt.
1493  */
1494 static PMAP_INLINE void
1495 free_pv_entry(pv_entry_t pv)
1496 {
1497         pv_entry_count--;
1498         zfree(pvzone, pv);
1499 }
1500
1501 /*
1502  * get a new pv_entry, allocating a block from the system
1503  * when needed.  This function may be called from an interrupt.
1504  */
1505 static pv_entry_t
1506 get_pv_entry(void)
1507 {
1508         pv_entry_count++;
1509         if (pv_entry_high_water &&
1510                 (pv_entry_count > pv_entry_high_water) &&
1511                 (pmap_pagedaemon_waken == 0)) {
1512                 pmap_pagedaemon_waken = 1;
1513                 wakeup (&vm_pages_needed);
1514         }
1515         return zalloc(pvzone);
1516 }
1517
1518 /*
1519  * This routine is very drastic, but can save the system
1520  * in a pinch.
1521  */
1522 void
1523 pmap_collect(void)
1524 {
1525         int i;
1526         vm_page_t m;
1527         static int warningdone=0;
1528
1529         if (pmap_pagedaemon_waken == 0)
1530                 return;
1531
1532         if (warningdone < 5) {
1533                 printf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1534                 warningdone++;
1535         }
1536
1537         for(i = 0; i < vm_page_array_size; i++) {
1538                 m = &vm_page_array[i];
1539                 if (m->wire_count || m->hold_count || m->busy ||
1540                     (m->flags & PG_BUSY))
1541                         continue;
1542                 pmap_remove_all(m);
1543         }
1544         pmap_pagedaemon_waken = 0;
1545 }
1546         
1547
1548 /*
1549  * If it is the first entry on the list, it is actually
1550  * in the header and we must copy the following entry up
1551  * to the header.  Otherwise we must search the list for
1552  * the entry.  In either case we free the now unused entry.
1553  */
1554 static int
1555 pmap_remove_entry(struct pmap *pmap, vm_page_t m, 
1556                         vm_offset_t va, pmap_inval_info_t info)
1557 {
1558         pv_entry_t pv;
1559         int rtval;
1560         int s;
1561
1562         s = splvm();
1563         if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1564                 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1565                         if (pmap == pv->pv_pmap && va == pv->pv_va) 
1566                                 break;
1567                 }
1568         } else {
1569                 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1570                         if (va == pv->pv_va) 
1571                                 break;
1572                 }
1573         }
1574
1575         rtval = 0;
1576         if (pv) {
1577                 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem, info);
1578                 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1579                 m->md.pv_list_count--;
1580                 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
1581                         vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1582                 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1583                 free_pv_entry(pv);
1584         }
1585         splx(s);
1586         return rtval;
1587 }
1588
1589 /*
1590  * Create a pv entry for page at pa for
1591  * (pmap, va).
1592  */
1593 static void
1594 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
1595 {
1596         int s;
1597         pv_entry_t pv;
1598
1599         s = splvm();
1600         pv = get_pv_entry();
1601         pv->pv_va = va;
1602         pv->pv_pmap = pmap;
1603         pv->pv_ptem = mpte;
1604
1605         TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1606         TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1607         m->md.pv_list_count++;
1608
1609         splx(s);
1610 }
1611
1612 /*
1613  * pmap_remove_pte: do the things to unmap a page in a process
1614  */
1615 static int
1616 pmap_remove_pte(struct pmap *pmap, unsigned *ptq, vm_offset_t va,
1617         pmap_inval_info_t info)
1618 {
1619         unsigned oldpte;
1620         vm_page_t m;
1621
1622         pmap_inval_add(info, pmap, va);
1623         oldpte = loadandclear(ptq);
1624         if (oldpte & PG_W)
1625                 pmap->pm_stats.wired_count -= 1;
1626         /*
1627          * Machines that don't support invlpg, also don't support
1628          * PG_G.  XXX PG_G is disabled for SMP so don't worry about
1629          * the SMP case.
1630          */
1631         if (oldpte & PG_G)
1632                 cpu_invlpg((void *)va);
1633         pmap->pm_stats.resident_count -= 1;
1634         if (oldpte & PG_MANAGED) {
1635                 m = PHYS_TO_VM_PAGE(oldpte);
1636                 if (oldpte & PG_M) {
1637 #if defined(PMAP_DIAGNOSTIC)
1638                         if (pmap_nw_modified((pt_entry_t) oldpte)) {
1639                                 printf(
1640         "pmap_remove: modified page not writable: va: 0x%x, pte: 0x%x\n",
1641                                     va, oldpte);
1642                         }
1643 #endif
1644                         if (pmap_track_modified(va))
1645                                 vm_page_dirty(m);
1646                 }
1647                 if (oldpte & PG_A)
1648                         vm_page_flag_set(m, PG_REFERENCED);
1649                 return pmap_remove_entry(pmap, m, va, info);
1650         } else {
1651                 return pmap_unuse_pt(pmap, va, NULL, info);
1652         }
1653
1654         return 0;
1655 }
1656
1657 /*
1658  * pmap_remove_page:
1659  *
1660  *      Remove a single page from a process address space.
1661  *
1662  *      This function may not be called from an interrupt if the pmap is
1663  *      not kernel_pmap.
1664  */
1665 static void
1666 pmap_remove_page(struct pmap *pmap, vm_offset_t va, pmap_inval_info_t info)
1667 {
1668         unsigned *ptq;
1669
1670         /*
1671          * if there is no pte for this address, just skip it!!!  Otherwise
1672          * get a local va for mappings for this pmap and remove the entry.
1673          */
1674         if (*pmap_pde(pmap, va) != 0) {
1675                 ptq = get_ptbase(pmap) + i386_btop(va);
1676                 if (*ptq) {
1677                         pmap_remove_pte(pmap, ptq, va, info);
1678                 }
1679         }
1680 }
1681
1682 /*
1683  * pmap_remove:
1684  *
1685  *      Remove the given range of addresses from the specified map.
1686  *
1687  *      It is assumed that the start and end are properly
1688  *      rounded to the page size.
1689  *
1690  *      This function may not be called from an interrupt if the pmap is
1691  *      not kernel_pmap.
1692  */
1693 void
1694 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
1695 {
1696         unsigned *ptbase;
1697         vm_offset_t pdnxt;
1698         vm_offset_t ptpaddr;
1699         vm_offset_t sindex, eindex;
1700         struct pmap_inval_info info;
1701
1702         if (pmap == NULL)
1703                 return;
1704
1705         if (pmap->pm_stats.resident_count == 0)
1706                 return;
1707
1708         pmap_inval_init(&info);
1709
1710         /*
1711          * special handling of removing one page.  a very
1712          * common operation and easy to short circuit some
1713          * code.
1714          */
1715         if (((sva + PAGE_SIZE) == eva) && 
1716                 (((unsigned) pmap->pm_pdir[(sva >> PDRSHIFT)] & PG_PS) == 0)) {
1717                 pmap_remove_page(pmap, sva, &info);
1718                 pmap_inval_flush(&info);
1719                 return;
1720         }
1721
1722         /*
1723          * Get a local virtual address for the mappings that are being
1724          * worked with.
1725          */
1726         ptbase = get_ptbase(pmap);
1727
1728         sindex = i386_btop(sva);
1729         eindex = i386_btop(eva);
1730
1731         for (; sindex < eindex; sindex = pdnxt) {
1732                 unsigned pdirindex;
1733
1734                 /*
1735                  * Calculate index for next page table.
1736                  */
1737                 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1738                 if (pmap->pm_stats.resident_count == 0)
1739                         break;
1740
1741                 pdirindex = sindex / NPDEPG;
1742                 if (((ptpaddr = (unsigned) pmap->pm_pdir[pdirindex]) & PG_PS) != 0) {
1743                         pmap_inval_add(&info, pmap, -1);
1744                         pmap->pm_pdir[pdirindex] = 0;
1745                         pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1746                         continue;
1747                 }
1748
1749                 /*
1750                  * Weed out invalid mappings. Note: we assume that the page
1751                  * directory table is always allocated, and in kernel virtual.
1752                  */
1753                 if (ptpaddr == 0)
1754                         continue;
1755
1756                 /*
1757                  * Limit our scan to either the end of the va represented
1758                  * by the current page table page, or to the end of the
1759                  * range being removed.
1760                  */
1761                 if (pdnxt > eindex) {
1762                         pdnxt = eindex;
1763                 }
1764
1765                 for (; sindex != pdnxt; sindex++) {
1766                         vm_offset_t va;
1767                         if (ptbase[sindex] == 0)
1768                                 continue;
1769                         va = i386_ptob(sindex);
1770                         if (pmap_remove_pte(pmap, ptbase + sindex, va, &info))
1771                                 break;
1772                 }
1773         }
1774         pmap_inval_flush(&info);
1775 }
1776
1777 /*
1778  * pmap_remove_all:
1779  *
1780  *      Removes this physical page from all physical maps in which it resides.
1781  *      Reflects back modify bits to the pager.
1782  *
1783  *      This routine may not be called from an interrupt.
1784  */
1785
1786 static void
1787 pmap_remove_all(vm_page_t m)
1788 {
1789         struct pmap_inval_info info;
1790         unsigned *pte, tpte;
1791         pv_entry_t pv;
1792         int s;
1793
1794 #if defined(PMAP_DIAGNOSTIC)
1795         /*
1796          * XXX this makes pmap_page_protect(NONE) illegal for non-managed
1797          * pages!
1798          */
1799         if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
1800                 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%08llx", (long long)VM_PAGE_TO_PHYS(m));
1801         }
1802 #endif
1803
1804         pmap_inval_init(&info);
1805         s = splvm();
1806         while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
1807                 pv->pv_pmap->pm_stats.resident_count--;
1808
1809                 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
1810                 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
1811
1812                 tpte = loadandclear(pte);
1813                 if (tpte & PG_W)
1814                         pv->pv_pmap->pm_stats.wired_count--;
1815
1816                 if (tpte & PG_A)
1817                         vm_page_flag_set(m, PG_REFERENCED);
1818
1819                 /*
1820                  * Update the vm_page_t clean and reference bits.
1821                  */
1822                 if (tpte & PG_M) {
1823 #if defined(PMAP_DIAGNOSTIC)
1824                         if (pmap_nw_modified((pt_entry_t) tpte)) {
1825                                 printf(
1826         "pmap_remove_all: modified page not writable: va: 0x%x, pte: 0x%x\n",
1827                                     pv->pv_va, tpte);
1828                         }
1829 #endif
1830                         if (pmap_track_modified(pv->pv_va))
1831                                 vm_page_dirty(m);
1832                 }
1833                 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
1834                 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1835                 m->md.pv_list_count--;
1836                 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem, &info);
1837                 free_pv_entry(pv);
1838         }
1839
1840         vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1841         splx(s);
1842         pmap_inval_flush(&info);
1843 }
1844
1845 /*
1846  * pmap_protect:
1847  *
1848  *      Set the physical protection on the specified range of this map
1849  *      as requested.
1850  *
1851  *      This function may not be called from an interrupt if the map is
1852  *      not the kernel_pmap.
1853  */
1854 void
1855 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
1856 {
1857         unsigned *ptbase;
1858         vm_offset_t pdnxt, ptpaddr;
1859         vm_pindex_t sindex, eindex;
1860         pmap_inval_info info;
1861
1862         if (pmap == NULL)
1863                 return;
1864
1865         if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1866                 pmap_remove(pmap, sva, eva);
1867                 return;
1868         }
1869
1870         if (prot & VM_PROT_WRITE)
1871                 return;
1872
1873         pmap_inval_init(&info);
1874
1875         ptbase = get_ptbase(pmap);
1876
1877         sindex = i386_btop(sva);
1878         eindex = i386_btop(eva);
1879
1880         for (; sindex < eindex; sindex = pdnxt) {
1881
1882                 unsigned pdirindex;
1883
1884                 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1885
1886                 pdirindex = sindex / NPDEPG;
1887                 if (((ptpaddr = (unsigned) pmap->pm_pdir[pdirindex]) & PG_PS) != 0) {
1888                         pmap_inval_add(&info, pmap, -1);
1889                         (unsigned) pmap->pm_pdir[pdirindex] &= ~(PG_M|PG_RW);
1890                         pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1891                         continue;
1892                 }
1893
1894                 /*
1895                  * Weed out invalid mappings. Note: we assume that the page
1896                  * directory table is always allocated, and in kernel virtual.
1897                  */
1898                 if (ptpaddr == 0)
1899                         continue;
1900
1901                 if (pdnxt > eindex) {
1902                         pdnxt = eindex;
1903                 }
1904
1905                 for (; sindex != pdnxt; sindex++) {
1906
1907                         unsigned pbits;
1908                         vm_page_t m;
1909
1910                         /* XXX this isn't optimal */
1911                         pmap_inval_add(&info, pmap, i386_ptob(sindex));
1912                         pbits = ptbase[sindex];
1913
1914                         if (pbits & PG_MANAGED) {
1915                                 m = NULL;
1916                                 if (pbits & PG_A) {
1917                                         m = PHYS_TO_VM_PAGE(pbits);
1918                                         vm_page_flag_set(m, PG_REFERENCED);
1919                                         pbits &= ~PG_A;
1920                                 }
1921                                 if (pbits & PG_M) {
1922                                         if (pmap_track_modified(i386_ptob(sindex))) {
1923                                                 if (m == NULL)
1924                                                         m = PHYS_TO_VM_PAGE(pbits);
1925                                                 vm_page_dirty(m);
1926                                                 pbits &= ~PG_M;
1927                                         }
1928                                 }
1929                         }
1930
1931                         pbits &= ~PG_RW;
1932
1933                         if (pbits != ptbase[sindex]) {
1934                                 ptbase[sindex] = pbits;
1935                         }
1936                 }
1937         }
1938         pmap_inval_flush(&info);
1939 }
1940
1941 /*
1942  *      Insert the given physical page (p) at
1943  *      the specified virtual address (v) in the
1944  *      target physical map with the protection requested.
1945  *
1946  *      If specified, the page will be wired down, meaning
1947  *      that the related pte can not be reclaimed.
1948  *
1949  *      NB:  This is the only routine which MAY NOT lazy-evaluate
1950  *      or lose information.  That is, this routine must actually
1951  *      insert this page into the given map NOW.
1952  */
1953 void
1954 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1955            boolean_t wired)
1956 {
1957         vm_paddr_t pa;
1958         unsigned *pte;
1959         vm_paddr_t opa;
1960         vm_offset_t origpte, newpte;
1961         vm_page_t mpte;
1962         pmap_inval_info info;
1963
1964         if (pmap == NULL)
1965                 return;
1966
1967         va &= PG_FRAME;
1968 #ifdef PMAP_DIAGNOSTIC
1969         if (va > VM_MAX_KERNEL_ADDRESS)
1970                 panic("pmap_enter: toobig");
1971         if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
1972                 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%x)", va);
1973 #endif
1974
1975         mpte = NULL;
1976         /*
1977          * In the case that a page table page is not
1978          * resident, we are creating it here.
1979          */
1980         if (va < UPT_MIN_ADDRESS) {
1981                 mpte = pmap_allocpte(pmap, va);
1982         }
1983
1984         pmap_inval_init(&info);
1985         pte = pmap_pte(pmap, va);
1986
1987         /*
1988          * Page Directory table entry not valid, we need a new PT page
1989          */
1990         if (pte == NULL) {
1991                 panic("pmap_enter: invalid page directory pdir=%x, va=0x%x\n",
1992                      (unsigned) pmap->pm_pdir[PTDPTDI], va);
1993         }
1994
1995         pa = VM_PAGE_TO_PHYS(m) & PG_FRAME;
1996         pmap_inval_add(&info, pmap, va); /* XXX non-optimal */
1997         origpte = *(vm_offset_t *)pte;
1998         opa = origpte & PG_FRAME;
1999
2000         if (origpte & PG_PS)
2001                 panic("pmap_enter: attempted pmap_enter on 4MB page");
2002
2003         /*
2004          * Mapping has not changed, must be protection or wiring change.
2005          */
2006         if (origpte && (opa == pa)) {
2007                 /*
2008                  * Wiring change, just update stats. We don't worry about
2009                  * wiring PT pages as they remain resident as long as there
2010                  * are valid mappings in them. Hence, if a user page is wired,
2011                  * the PT page will be also.
2012                  */
2013                 if (wired && ((origpte & PG_W) == 0))
2014                         pmap->pm_stats.wired_count++;
2015                 else if (!wired && (origpte & PG_W))
2016                         pmap->pm_stats.wired_count--;
2017
2018 #if defined(PMAP_DIAGNOSTIC)
2019                 if (pmap_nw_modified((pt_entry_t) origpte)) {
2020                         printf(
2021         "pmap_enter: modified page not writable: va: 0x%x, pte: 0x%x\n",
2022                             va, origpte);
2023                 }
2024 #endif
2025
2026                 /*
2027                  * Remove the extra pte reference.  Note that we cannot
2028                  * optimize the RO->RW case because we have adjusted the
2029                  * wiring count above and may need to adjust the wiring
2030                  * bits below.
2031                  */
2032                 if (mpte)
2033                         mpte->hold_count--;
2034
2035                 /*
2036                  * We might be turning off write access to the page,
2037                  * so we go ahead and sense modify status.
2038                  */
2039                 if (origpte & PG_MANAGED) {
2040                         if ((origpte & PG_M) && pmap_track_modified(va)) {
2041                                 vm_page_t om;
2042                                 om = PHYS_TO_VM_PAGE(opa);
2043                                 vm_page_dirty(om);
2044                         }
2045                         pa |= PG_MANAGED;
2046                 }
2047                 goto validate;
2048         } 
2049         /*
2050          * Mapping has changed, invalidate old range and fall through to
2051          * handle validating new mapping.
2052          */
2053         if (opa) {
2054                 int err;
2055                 err = pmap_remove_pte(pmap, pte, va, &info);
2056                 if (err)
2057                         panic("pmap_enter: pte vanished, va: 0x%x", va);
2058         }
2059
2060         /*
2061          * Enter on the PV list if part of our managed memory. Note that we
2062          * raise IPL while manipulating pv_table since pmap_enter can be
2063          * called at interrupt time.
2064          */
2065         if (pmap_initialized && 
2066             (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2067                 pmap_insert_entry(pmap, va, mpte, m);
2068                 pa |= PG_MANAGED;
2069         }
2070
2071         /*
2072          * Increment counters
2073          */
2074         pmap->pm_stats.resident_count++;
2075         if (wired)
2076                 pmap->pm_stats.wired_count++;
2077
2078 validate:
2079         /*
2080          * Now validate mapping with desired protection/wiring.
2081          */
2082         newpte = (vm_offset_t) (pa | pte_prot(pmap, prot) | PG_V);
2083
2084         if (wired)
2085                 newpte |= PG_W;
2086         if (va < UPT_MIN_ADDRESS)
2087                 newpte |= PG_U;
2088         if (pmap == kernel_pmap)
2089                 newpte |= pgeflag;
2090
2091         /*
2092          * if the mapping or permission bits are different, we need
2093          * to update the pte.
2094          */
2095         if ((origpte & ~(PG_M|PG_A)) != newpte) {
2096                 *pte = newpte | PG_A;
2097         }
2098         pmap_inval_flush(&info);
2099 }
2100
2101 /*
2102  * this code makes some *MAJOR* assumptions:
2103  * 1. Current pmap & pmap exists.
2104  * 2. Not wired.
2105  * 3. Read access.
2106  * 4. No page table pages.
2107  * 5. Tlbflush is deferred to calling procedure.
2108  * 6. Page IS managed.
2109  * but is *MUCH* faster than pmap_enter...
2110  */
2111
2112 static vm_page_t
2113 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_page_t mpte)
2114 {
2115         unsigned *pte;
2116         vm_paddr_t pa;
2117         pmap_inval_info info;
2118
2119         pmap_inval_init(&info);
2120
2121         /*
2122          * In the case that a page table page is not
2123          * resident, we are creating it here.
2124          */
2125         if (va < UPT_MIN_ADDRESS) {
2126                 unsigned ptepindex;
2127                 vm_offset_t ptepa;
2128
2129                 /*
2130                  * Calculate pagetable page index
2131                  */
2132                 ptepindex = va >> PDRSHIFT;
2133                 if (mpte && (mpte->pindex == ptepindex)) {
2134                         mpte->hold_count++;
2135                 } else {
2136 retry:
2137                         /*
2138                          * Get the page directory entry
2139                          */
2140                         ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
2141
2142                         /*
2143                          * If the page table page is mapped, we just increment
2144                          * the hold count, and activate it.
2145                          */
2146                         if (ptepa) {
2147                                 if (ptepa & PG_PS)
2148                                         panic("pmap_enter_quick: unexpected mapping into 4MB page");
2149                                 if (pmap->pm_ptphint &&
2150                                         (pmap->pm_ptphint->pindex == ptepindex)) {
2151                                         mpte = pmap->pm_ptphint;
2152                                 } else {
2153                                         mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
2154                                         pmap->pm_ptphint = mpte;
2155                                 }
2156                                 if (mpte == NULL)
2157                                         goto retry;
2158                                 mpte->hold_count++;
2159                         } else {
2160                                 mpte = _pmap_allocpte(pmap, ptepindex);
2161                         }
2162                 }
2163         } else {
2164                 mpte = NULL;
2165         }
2166
2167         /*
2168          * This call to vtopte makes the assumption that we are
2169          * entering the page into the current pmap.  In order to support
2170          * quick entry into any pmap, one would likely use pmap_pte_quick.
2171          * But that isn't as quick as vtopte.
2172          */
2173         pte = (unsigned *)vtopte(va);
2174         if (*pte) {
2175                 if (mpte)
2176                         pmap_unwire_pte_hold(pmap, mpte, &info);
2177                 return 0;
2178         }
2179
2180         /*
2181          * Enter on the PV list if part of our managed memory. Note that we
2182          * raise IPL while manipulating pv_table since pmap_enter can be
2183          * called at interrupt time.
2184          */
2185         if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0)
2186                 pmap_insert_entry(pmap, va, mpte, m);
2187
2188         /*
2189          * Increment counters
2190          */
2191         pmap->pm_stats.resident_count++;
2192
2193         pa = VM_PAGE_TO_PHYS(m);
2194
2195         /*
2196          * Now validate mapping with RO protection
2197          */
2198         if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2199                 *pte = pa | PG_V | PG_U;
2200         else
2201                 *pte = pa | PG_V | PG_U | PG_MANAGED;
2202
2203         return mpte;
2204 }
2205
2206 /*
2207  * Make a temporary mapping for a physical address.  This is only intended
2208  * to be used for panic dumps.
2209  */
2210 void *
2211 pmap_kenter_temporary(vm_paddr_t pa, int i)
2212 {
2213         pmap_kenter((vm_offset_t)crashdumpmap + (i * PAGE_SIZE), pa);
2214         return ((void *)crashdumpmap);
2215 }
2216
2217 #define MAX_INIT_PT (96)
2218
2219 /*
2220  * This routine preloads the ptes for a given object into the specified pmap.
2221  * This eliminates the blast of soft faults on process startup and
2222  * immediately after an mmap.
2223  */
2224 void
2225 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2226                     vm_object_t object, vm_pindex_t pindex, 
2227                     vm_size_t size, int limit)
2228 {
2229         vm_offset_t tmpidx;
2230         int psize;
2231         vm_page_t p, mpte;
2232         int objpgs;
2233         int s;
2234
2235         if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2236                 return;
2237
2238 #if 0
2239         /* 
2240          * XXX you must be joking, entering PTE's into a user page table
2241          * without any accounting?  This could result in the page table
2242          * being freed while it still contains mappings (free with PG_ZERO
2243          * assumption leading to a non-zero page being marked PG_ZERO).
2244          */
2245         /*
2246          * This code maps large physical mmap regions into the
2247          * processor address space.  Note that some shortcuts
2248          * are taken, but the code works.
2249          */
2250         if (pseflag &&
2251             (object->type == OBJT_DEVICE) &&
2252             ((addr & (NBPDR - 1)) == 0) &&
2253             ((size & (NBPDR - 1)) == 0) ) {
2254                 int i;
2255                 vm_page_t m[1];
2256                 unsigned int ptepindex;
2257                 int npdes;
2258                 vm_offset_t ptepa;
2259
2260                 if (pmap->pm_pdir[ptepindex = (addr >> PDRSHIFT)])
2261                         return;
2262
2263 retry:
2264                 p = vm_page_lookup(object, pindex);
2265                 if (p && vm_page_sleep_busy(p, FALSE, "init4p"))
2266                         goto retry;
2267
2268                 if (p == NULL) {
2269                         p = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL);
2270                         if (p == NULL)
2271                                 return;
2272                         m[0] = p;
2273
2274                         if (vm_pager_get_pages(object, m, 1, 0) != VM_PAGER_OK) {
2275                                 vm_page_free(p);
2276                                 return;
2277                         }
2278
2279                         p = vm_page_lookup(object, pindex);
2280                         vm_page_wakeup(p);
2281                 }
2282
2283                 ptepa = (vm_offset_t) VM_PAGE_TO_PHYS(p);
2284                 if (ptepa & (NBPDR - 1)) {
2285                         return;
2286                 }
2287
2288                 p->valid = VM_PAGE_BITS_ALL;
2289
2290                 pmap->pm_stats.resident_count += size >> PAGE_SHIFT;
2291                 npdes = size >> PDRSHIFT;
2292                 for (i = 0; i < npdes; i++) {
2293                         pmap->pm_pdir[ptepindex] =
2294                             (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_PS);
2295                         ptepa += NBPDR;
2296                         ptepindex += 1;
2297                 }
2298                 vm_page_flag_set(p, PG_MAPPED);
2299                 cpu_invltlb();
2300                 smp_invltlb();
2301                 return;
2302         }
2303 #endif
2304
2305         psize = i386_btop(size);
2306
2307         if ((object->type != OBJT_VNODE) ||
2308                 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2309                         (object->resident_page_count > MAX_INIT_PT))) {
2310                 return;
2311         }
2312
2313         if (psize + pindex > object->size) {
2314                 if (object->size < pindex)
2315                         return;           
2316                 psize = object->size - pindex;
2317         }
2318
2319
2320         /*
2321          * If we are processing a major portion of the object, then scan the
2322          * entire thing.
2323          *
2324          * We cannot safely scan the object's memq unless we are at splvm(),
2325          * since interrupts can remove pages from objects.
2326          */
2327         s = splvm();
2328         mpte = NULL;
2329         if (psize > (object->resident_page_count >> 2)) {
2330                 objpgs = psize;
2331
2332                 for (p = TAILQ_FIRST(&object->memq);
2333                     objpgs > 0 && p != NULL;
2334                     p = TAILQ_NEXT(p, listq)
2335                 ) {
2336                         tmpidx = p->pindex;
2337                         if (tmpidx < pindex)
2338                                 continue;
2339                         tmpidx -= pindex;
2340                         if (tmpidx >= psize)
2341                                 continue;
2342
2343                         /*
2344                          * don't allow an madvise to blow away our really
2345                          * free pages allocating pv entries.
2346                          */
2347                         if ((limit & MAP_PREFAULT_MADVISE) &&
2348                             vmstats.v_free_count < vmstats.v_free_reserved) {
2349                                 break;
2350                         }
2351                         if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2352                                 (p->busy == 0) &&
2353                             (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2354                                 if ((p->queue - p->pc) == PQ_CACHE)
2355                                         vm_page_deactivate(p);
2356                                 vm_page_busy(p);
2357                                 mpte = pmap_enter_quick(pmap, 
2358                                         addr + i386_ptob(tmpidx), p, mpte);
2359                                 vm_page_flag_set(p, PG_MAPPED);
2360                                 vm_page_wakeup(p);
2361                         }
2362                         objpgs -= 1;
2363                 }
2364         } else {
2365                 /*
2366                  * else lookup the pages one-by-one.
2367                  */
2368                 for (tmpidx = 0; tmpidx < psize; tmpidx += 1) {
2369                         /*
2370                          * don't allow an madvise to blow away our really
2371                          * free pages allocating pv entries.
2372                          */
2373                         if ((limit & MAP_PREFAULT_MADVISE) &&
2374                             vmstats.v_free_count < vmstats.v_free_reserved) {
2375                                 break;
2376                         }
2377                         p = vm_page_lookup(object, tmpidx + pindex);
2378                         if (p &&
2379                             ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2380                                 (p->busy == 0) &&
2381                             (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2382                                 if ((p->queue - p->pc) == PQ_CACHE)
2383                                         vm_page_deactivate(p);
2384                                 vm_page_busy(p);
2385                                 mpte = pmap_enter_quick(pmap, 
2386                                         addr + i386_ptob(tmpidx), p, mpte);
2387                                 vm_page_flag_set(p, PG_MAPPED);
2388                                 vm_page_wakeup(p);
2389                         }
2390                 }
2391         }
2392         splx(s);
2393 }
2394
2395 /*
2396  * pmap_prefault provides a quick way of clustering pagefaults into a
2397  * processes address space.  It is a "cousin" of pmap_object_init_pt, 
2398  * except it runs at page fault time instead of mmap time.
2399  */
2400 #define PFBAK 4
2401 #define PFFOR 4
2402 #define PAGEORDER_SIZE (PFBAK+PFFOR)
2403
2404 static int pmap_prefault_pageorder[] = {
2405         -PAGE_SIZE, PAGE_SIZE,
2406         -2 * PAGE_SIZE, 2 * PAGE_SIZE,
2407         -3 * PAGE_SIZE, 3 * PAGE_SIZE,
2408         -4 * PAGE_SIZE, 4 * PAGE_SIZE
2409 };
2410
2411 void
2412 pmap_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry)
2413 {
2414         int i;
2415         int s;
2416         vm_offset_t starta;
2417         vm_offset_t addr;
2418         vm_pindex_t pindex;
2419         vm_page_t m, mpte;
2420         vm_object_t object;
2421
2422         if (!curproc || (pmap != vmspace_pmap(curproc->p_vmspace)))
2423                 return;
2424
2425         object = entry->object.vm_object;
2426
2427         starta = addra - PFBAK * PAGE_SIZE;
2428         if (starta < entry->start)
2429                 starta = entry->start;
2430         else if (starta > addra)
2431                 starta = 0;
2432
2433         /*
2434          * splvm() protection is required to maintain the page/object 
2435          * association, interrupts can free pages and remove them from
2436          * their objects.
2437          */
2438         mpte = NULL;
2439         s = splvm();
2440         for (i = 0; i < PAGEORDER_SIZE; i++) {
2441                 vm_object_t lobject;
2442                 unsigned *pte;
2443
2444                 addr = addra + pmap_prefault_pageorder[i];
2445                 if (addr > addra + (PFFOR * PAGE_SIZE))
2446                         addr = 0;
2447
2448                 if (addr < starta || addr >= entry->end)
2449                         continue;
2450
2451                 if ((*pmap_pde(pmap, addr)) == NULL) 
2452                         continue;
2453
2454                 pte = (unsigned *) vtopte(addr);
2455                 if (*pte)
2456                         continue;
2457
2458                 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2459                 lobject = object;
2460
2461                 for (m = vm_page_lookup(lobject, pindex);
2462                     (!m && (lobject->type == OBJT_DEFAULT) &&
2463                      (lobject->backing_object));
2464                     lobject = lobject->backing_object
2465                 ) {
2466                         if (lobject->backing_object_offset & PAGE_MASK)
2467                                 break;
2468                         pindex += (lobject->backing_object_offset >> PAGE_SHIFT);
2469                         m = vm_page_lookup(lobject->backing_object, pindex);
2470                 }
2471
2472                 /*
2473                  * give-up when a page is not in memory
2474                  */
2475                 if (m == NULL)
2476                         break;
2477
2478                 if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2479                         (m->busy == 0) &&
2480                     (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2481
2482                         if ((m->queue - m->pc) == PQ_CACHE) {
2483                                 vm_page_deactivate(m);
2484                         }
2485                         vm_page_busy(m);
2486                         mpte = pmap_enter_quick(pmap, addr, m, mpte);
2487                         vm_page_flag_set(m, PG_MAPPED);
2488                         vm_page_wakeup(m);
2489                 }
2490         }
2491         splx(s);
2492 }
2493
2494 /*
2495  *      Routine:        pmap_change_wiring
2496  *      Function:       Change the wiring attribute for a map/virtual-address
2497  *                      pair.
2498  *      In/out conditions:
2499  *                      The mapping must already exist in the pmap.
2500  */
2501 void
2502 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2503 {
2504         unsigned *pte;
2505
2506         if (pmap == NULL)
2507                 return;
2508
2509         pte = pmap_pte(pmap, va);
2510
2511         if (wired && !pmap_pte_w(pte))
2512                 pmap->pm_stats.wired_count++;
2513         else if (!wired && pmap_pte_w(pte))
2514                 pmap->pm_stats.wired_count--;
2515
2516         /*
2517          * Wiring is not a hardware characteristic so there is no need to
2518          * invalidate TLB.  However, in an SMP environment we must use
2519          * a locked bus cycle to update the pte (if we are not using 
2520          * the pmap_inval_*() API that is)... it's ok to do this for simple
2521          * wiring changes.
2522          */
2523 #ifdef SMP
2524         if (wired)
2525                 atomic_set_int(pte, PG_W);
2526         else
2527                 atomic_clear_int(pte, PG_W);
2528 #else
2529         if (wired)
2530                 atomic_set_int_nonlocked(pte, PG_W);
2531         else
2532                 atomic_clear_int_nonlocked(pte, PG_W);
2533 #endif
2534 }
2535
2536
2537
2538 /*
2539  *      Copy the range specified by src_addr/len
2540  *      from the source map to the range dst_addr/len
2541  *      in the destination map.
2542  *
2543  *      This routine is only advisory and need not do anything.
2544  */
2545 void
2546 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, 
2547         vm_size_t len, vm_offset_t src_addr)
2548 {
2549         pmap_inval_info info;
2550         vm_offset_t addr;
2551         vm_offset_t end_addr = src_addr + len;
2552         vm_offset_t pdnxt;
2553         unsigned src_frame, dst_frame;
2554         vm_page_t m;
2555         int s;
2556
2557         if (dst_addr != src_addr)
2558                 return;
2559
2560         src_frame = ((unsigned) src_pmap->pm_pdir[PTDPTDI]) & PG_FRAME;
2561         if (src_frame != (((unsigned) PTDpde) & PG_FRAME)) {
2562                 return;
2563         }
2564
2565         dst_frame = ((unsigned) dst_pmap->pm_pdir[PTDPTDI]) & PG_FRAME;
2566         if (dst_frame != (((unsigned) APTDpde) & PG_FRAME)) {
2567                 APTDpde = (pd_entry_t) (dst_frame | PG_RW | PG_V);
2568                 /* The page directory is not shared between CPUs */
2569                 cpu_invltlb();
2570         }
2571         pmap_inval_init(&info);
2572         pmap_inval_add(&info, dst_pmap, -1);
2573         pmap_inval_add(&info, src_pmap, -1);
2574
2575         /*
2576          * splvm() protection is required to maintain the page/object
2577          * association, interrupts can free pages and remove them from 
2578          * their objects.
2579          */
2580         s = splvm();
2581         for (addr = src_addr; addr < end_addr; addr = pdnxt) {
2582                 unsigned *src_pte, *dst_pte;
2583                 vm_page_t dstmpte, srcmpte;
2584                 vm_offset_t srcptepaddr;
2585                 unsigned ptepindex;
2586
2587                 if (addr >= UPT_MIN_ADDRESS)
2588                         panic("pmap_copy: invalid to pmap_copy page tables\n");
2589
2590                 /*
2591                  * Don't let optional prefaulting of pages make us go
2592                  * way below the low water mark of free pages or way
2593                  * above high water mark of used pv entries.
2594                  */
2595                 if (vmstats.v_free_count < vmstats.v_free_reserved ||
2596                     pv_entry_count > pv_entry_high_water)
2597                         break;
2598                 
2599                 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
2600                 ptepindex = addr >> PDRSHIFT;
2601
2602                 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex];
2603                 if (srcptepaddr == 0)
2604                         continue;
2605                         
2606                 if (srcptepaddr & PG_PS) {
2607                         if (dst_pmap->pm_pdir[ptepindex] == 0) {
2608                                 dst_pmap->pm_pdir[ptepindex] = (pd_entry_t) srcptepaddr;
2609                                 dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
2610                         }
2611                         continue;
2612                 }
2613
2614                 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
2615                 if ((srcmpte == NULL) ||
2616                         (srcmpte->hold_count == 0) || (srcmpte->flags & PG_BUSY))
2617                         continue;
2618
2619                 if (pdnxt > end_addr)
2620                         pdnxt = end_addr;
2621
2622                 src_pte = (unsigned *) vtopte(addr);
2623                 dst_pte = (unsigned *) avtopte(addr);
2624                 while (addr < pdnxt) {
2625                         unsigned ptetemp;
2626                         ptetemp = *src_pte;
2627                         /*
2628                          * we only virtual copy managed pages
2629                          */
2630                         if ((ptetemp & PG_MANAGED) != 0) {
2631                                 /*
2632                                  * We have to check after allocpte for the
2633                                  * pte still being around...  allocpte can
2634                                  * block.
2635                                  */
2636                                 dstmpte = pmap_allocpte(dst_pmap, addr);
2637                                 if ((*dst_pte == 0) && (ptetemp = *src_pte)) {
2638                                         /*
2639                                          * Clear the modified and
2640                                          * accessed (referenced) bits
2641                                          * during the copy.
2642                                          */
2643                                         m = PHYS_TO_VM_PAGE(ptetemp);
2644                                         *dst_pte = ptetemp & ~(PG_M | PG_A);
2645                                         dst_pmap->pm_stats.resident_count++;
2646                                         pmap_insert_entry(dst_pmap, addr,
2647                                                 dstmpte, m);
2648                                 } else {
2649                                         pmap_unwire_pte_hold(dst_pmap, dstmpte, &info);
2650                                 }
2651                                 if (dstmpte->hold_count >= srcmpte->hold_count)
2652                                         break;
2653                         }
2654                         addr += PAGE_SIZE;
2655                         src_pte++;
2656                         dst_pte++;
2657                 }
2658         }
2659         splx(s);
2660         pmap_inval_flush(&info);
2661 }       
2662
2663 /*
2664  *      Routine:        pmap_kernel
2665  *      Function:
2666  *              Returns the physical map handle for the kernel.
2667  */
2668 pmap_t
2669 pmap_kernel(void)
2670 {
2671         return (kernel_pmap);
2672 }
2673
2674 /*
2675  * pmap_zero_page:
2676  *
2677  *      Zero the specified PA by mapping the page into KVM and clearing its
2678  *      contents.
2679  *
2680  *      This function may be called from an interrupt and no locking is
2681  *      required.
2682  */
2683 void
2684 pmap_zero_page(vm_paddr_t phys)
2685 {
2686         struct mdglobaldata *gd = mdcpu;
2687
2688         crit_enter();
2689         if (*(int *)gd->gd_CMAP3)
2690                 panic("pmap_zero_page: CMAP3 busy");
2691         *(int *)gd->gd_CMAP3 =
2692                     PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2693         cpu_invlpg(gd->gd_CADDR3);
2694
2695 #if defined(I686_CPU)
2696         if (cpu_class == CPUCLASS_686)
2697                 i686_pagezero(gd->gd_CADDR3);
2698         else
2699 #endif
2700                 bzero(gd->gd_CADDR3, PAGE_SIZE);
2701         *(int *) gd->gd_CMAP3 = 0;
2702         crit_exit();
2703 }
2704
2705 /*
2706  * pmap_page_assertzero:
2707  *
2708  *      Assert that a page is empty, panic if it isn't.
2709  */
2710 void
2711 pmap_page_assertzero(vm_paddr_t phys)
2712 {
2713         struct mdglobaldata *gd = mdcpu;
2714         int i;
2715
2716         crit_enter();
2717         if (*(int *)gd->gd_CMAP3)
2718                 panic("pmap_zero_page: CMAP3 busy");
2719         *(int *)gd->gd_CMAP3 =
2720                     PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2721         cpu_invlpg(gd->gd_CADDR3);
2722         for (i = 0; i < PAGE_SIZE; i += 4) {
2723             if (*(int *)((char *)gd->gd_CADDR3 + i) != 0) {
2724                 panic("pmap_page_assertzero() @ %p not zero!\n",
2725                     (void *)gd->gd_CADDR3);
2726             }
2727         }
2728         *(int *) gd->gd_CMAP3 = 0;
2729         crit_exit();
2730 }
2731
2732 /*
2733  * pmap_zero_page:
2734  *
2735  *      Zero part of a physical page by mapping it into memory and clearing
2736  *      its contents with bzero.
2737  *
2738  *      off and size may not cover an area beyond a single hardware page.
2739  */
2740 void
2741 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
2742 {
2743         struct mdglobaldata *gd = mdcpu;
2744
2745         crit_enter();
2746         if (*(int *) gd->gd_CMAP3)
2747                 panic("pmap_zero_page: CMAP3 busy");
2748         *(int *) gd->gd_CMAP3 = PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2749         cpu_invlpg(gd->gd_CADDR3);
2750
2751 #if defined(I686_CPU)
2752         if (cpu_class == CPUCLASS_686 && off == 0 && size == PAGE_SIZE)
2753                 i686_pagezero(gd->gd_CADDR3);
2754         else
2755 #endif
2756                 bzero((char *)gd->gd_CADDR3 + off, size);
2757         *(int *) gd->gd_CMAP3 = 0;
2758         crit_exit();
2759 }
2760
2761 /*
2762  * pmap_copy_page:
2763  *
2764  *      Copy the physical page from the source PA to the target PA.
2765  *      This function may be called from an interrupt.  No locking
2766  *      is required.
2767  */
2768 void
2769 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
2770 {
2771         struct mdglobaldata *gd = mdcpu;
2772
2773         crit_enter();
2774         if (*(int *) gd->gd_CMAP1)
2775                 panic("pmap_copy_page: CMAP1 busy");
2776         if (*(int *) gd->gd_CMAP2)
2777                 panic("pmap_copy_page: CMAP2 busy");
2778
2779         *(int *) gd->gd_CMAP1 = PG_V | (src & PG_FRAME) | PG_A;
2780         *(int *) gd->gd_CMAP2 = PG_V | PG_RW | (dst & PG_FRAME) | PG_A | PG_M;
2781
2782         cpu_invlpg(gd->gd_CADDR1);
2783         cpu_invlpg(gd->gd_CADDR2);
2784
2785         bcopy(gd->gd_CADDR1, gd->gd_CADDR2, PAGE_SIZE);
2786
2787         *(int *) gd->gd_CMAP1 = 0;
2788         *(int *) gd->gd_CMAP2 = 0;
2789         crit_exit();
2790 }
2791
2792 /*
2793  * pmap_copy_page_frag:
2794  *
2795  *      Copy the physical page from the source PA to the target PA.
2796  *      This function may be called from an interrupt.  No locking
2797  *      is required.
2798  */
2799 void
2800 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
2801 {
2802         struct mdglobaldata *gd = mdcpu;
2803
2804         crit_enter();
2805         if (*(int *) gd->gd_CMAP1)
2806                 panic("pmap_copy_page: CMAP1 busy");
2807         if (*(int *) gd->gd_CMAP2)
2808                 panic("pmap_copy_page: CMAP2 busy");
2809
2810         *(int *) gd->gd_CMAP1 = PG_V | (src & PG_FRAME) | PG_A;
2811         *(int *) gd->gd_CMAP2 = PG_V | PG_RW | (dst & PG_FRAME) | PG_A | PG_M;
2812
2813         cpu_invlpg(gd->gd_CADDR1);
2814         cpu_invlpg(gd->gd_CADDR2);
2815
2816         bcopy((char *)gd->gd_CADDR1 + (src & PAGE_MASK),
2817               (char *)gd->gd_CADDR2 + (dst & PAGE_MASK),
2818               bytes);
2819
2820         *(int *) gd->gd_CMAP1 = 0;
2821         *(int *) gd->gd_CMAP2 = 0;
2822         crit_exit();
2823 }
2824
2825 /*
2826  * Returns true if the pmap's pv is one of the first
2827  * 16 pvs linked to from this page.  This count may
2828  * be changed upwards or downwards in the future; it
2829  * is only necessary that true be returned for a small
2830  * subset of pmaps for proper page aging.
2831  */
2832 boolean_t
2833 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2834 {
2835         pv_entry_t pv;
2836         int loops = 0;
2837         int s;
2838
2839         if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2840                 return FALSE;
2841
2842         s = splvm();
2843
2844         TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2845                 if (pv->pv_pmap == pmap) {
2846                         splx(s);
2847                         return TRUE;
2848                 }
2849                 loops++;
2850                 if (loops >= 16)
2851                         break;
2852         }
2853         splx(s);
2854         return (FALSE);
2855 }
2856
2857 #define PMAP_REMOVE_PAGES_CURPROC_ONLY
2858 /*
2859  * Remove all pages from specified address space
2860  * this aids process exit speeds.  Also, this code
2861  * is special cased for current process only, but
2862  * can have the more generic (and slightly slower)
2863  * mode enabled.  This is much faster than pmap_remove
2864  * in the case of running down an entire address space.
2865  */
2866 void
2867 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2868 {
2869         unsigned *pte, tpte;
2870         pv_entry_t pv, npv;
2871         int s;
2872         vm_page_t m;
2873         pmap_inval_info info;
2874
2875 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2876         if (!curproc || (pmap != vmspace_pmap(curproc->p_vmspace))) {
2877                 printf("warning: pmap_remove_pages called with non-current pmap\n");
2878                 return;
2879         }
2880 #endif
2881
2882         pmap_inval_init(&info);
2883         s = splvm();
2884         for(pv = TAILQ_FIRST(&pmap->pm_pvlist);
2885                 pv;
2886                 pv = npv) {
2887
2888                 if (pv->pv_va >= eva || pv->pv_va < sva) {
2889                         npv = TAILQ_NEXT(pv, pv_plist);
2890                         continue;
2891                 }
2892
2893 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2894                 pte = (unsigned *)vtopte(pv->pv_va);
2895 #else
2896                 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2897 #endif
2898                 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
2899                 tpte = *pte;
2900
2901 /*
2902  * We cannot remove wired pages from a process' mapping at this time
2903  */
2904                 if (tpte & PG_W) {
2905                         npv = TAILQ_NEXT(pv, pv_plist);
2906                         continue;
2907                 }
2908                 *pte = 0;
2909
2910                 m = PHYS_TO_VM_PAGE(tpte);
2911
2912                 KASSERT(m < &vm_page_array[vm_page_array_size],
2913                         ("pmap_remove_pages: bad tpte %x", tpte));
2914
2915                 pv->pv_pmap->pm_stats.resident_count--;
2916
2917                 /*
2918                  * Update the vm_page_t clean and reference bits.
2919                  */
2920                 if (tpte & PG_M) {
2921                         vm_page_dirty(m);
2922                 }
2923
2924
2925                 npv = TAILQ_NEXT(pv, pv_plist);
2926                 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2927
2928                 m->md.pv_list_count--;
2929                 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2930                 if (TAILQ_FIRST(&m->md.pv_list) == NULL) {
2931                         vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2932                 }
2933
2934                 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem, &info);
2935                 free_pv_entry(pv);
2936         }
2937         pmap_inval_flush(&info);
2938         splx(s);
2939 }
2940
2941 /*
2942  * pmap_testbit tests bits in pte's
2943  * note that the testbit/changebit routines are inline,
2944  * and a lot of things compile-time evaluate.
2945  */
2946 static boolean_t
2947 pmap_testbit(vm_page_t m, int bit)
2948 {
2949         pv_entry_t pv;
2950         unsigned *pte;
2951         int s;
2952
2953         if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2954                 return FALSE;
2955
2956         if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2957                 return FALSE;
2958
2959         s = splvm();
2960
2961         TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2962                 /*
2963                  * if the bit being tested is the modified bit, then
2964                  * mark clean_map and ptes as never
2965                  * modified.
2966                  */
2967                 if (bit & (PG_A|PG_M)) {
2968                         if (!pmap_track_modified(pv->pv_va))
2969                                 continue;
2970                 }
2971
2972 #if defined(PMAP_DIAGNOSTIC)
2973                 if (!pv->pv_pmap) {
2974                         printf("Null pmap (tb) at va: 0x%x\n", pv->pv_va);
2975                         continue;
2976                 }
2977 #endif
2978                 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2979                 if (*pte & bit) {
2980                         splx(s);
2981                         return TRUE;
2982                 }
2983         }
2984         splx(s);
2985         return (FALSE);
2986 }
2987
2988 /*
2989  * this routine is used to modify bits in ptes
2990  */
2991 static __inline void
2992 pmap_changebit(vm_page_t m, int bit, boolean_t setem)
2993 {
2994         struct pmap_inval_info info;
2995         pv_entry_t pv;
2996         unsigned *pte;
2997         int s;
2998
2999         if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3000                 return;
3001
3002         pmap_inval_init(&info);
3003         s = splvm();
3004
3005         /*
3006          * Loop over all current mappings setting/clearing as appropos If
3007          * setting RO do we need to clear the VAC?
3008          */
3009         TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3010                 /*
3011                  * don't write protect pager mappings
3012                  */
3013                 if (!setem && (bit == PG_RW)) {
3014                         if (!pmap_track_modified(pv->pv_va))
3015                                 continue;
3016                 }
3017
3018 #if defined(PMAP_DIAGNOSTIC)
3019                 if (!pv->pv_pmap) {
3020                         printf("Null pmap (cb) at va: 0x%x\n", pv->pv_va);
3021                         continue;
3022                 }
3023 #endif
3024
3025                 /*
3026                  * Careful here.  We can use a locked bus instruction to
3027                  * clear PG_A or PG_M safely but we need to synchronize
3028                  * with the target cpus when we mess with PG_RW.
3029                  */
3030                 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3031                 if (bit == PG_RW)
3032                         pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
3033
3034                 if (setem) {
3035 #ifdef SMP
3036                         atomic_set_int(pte, bit);
3037 #else
3038                         atomic_set_int_nonlocked(pte, bit);
3039 #endif
3040                 } else {
3041                         vm_offset_t pbits = *(vm_offset_t *)pte;
3042                         if (pbits & bit) {
3043                                 if (bit == PG_RW) {
3044                                         if (pbits & PG_M) {
3045                                                 vm_page_dirty(m);
3046                                         }
3047 #ifdef SMP
3048                                         atomic_clear_int(pte, PG_M|PG_RW);
3049 #else
3050                                         atomic_clear_int_nonlocked(pte, PG_M|PG_RW);
3051 #endif
3052                                 } else {
3053 #ifdef SMP
3054                                         atomic_clear_int(pte, bit);
3055 #else
3056                                         atomic_clear_int_nonlocked(pte, bit);
3057 #endif
3058                                 }
3059                         }
3060                 }
3061         }
3062         pmap_inval_flush(&info);
3063         splx(s);
3064 }
3065
3066 /*
3067  *      pmap_page_protect:
3068  *
3069  *      Lower the permission for all mappings to a given page.
3070  */
3071 void
3072 pmap_page_protect(vm_page_t m, vm_prot_t prot)
3073 {
3074         if ((prot & VM_PROT_WRITE) == 0) {
3075                 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
3076                         pmap_changebit(m, PG_RW, FALSE);
3077                 } else {
3078                         pmap_remove_all(m);
3079                 }
3080         }
3081 }
3082
3083 vm_paddr_t
3084 pmap_phys_address(int ppn)
3085 {
3086         return (i386_ptob(ppn));
3087 }
3088
3089 /*
3090  *      pmap_ts_referenced:
3091  *
3092  *      Return a count of reference bits for a page, clearing those bits.
3093  *      It is not necessary for every reference bit to be cleared, but it
3094  *      is necessary that 0 only be returned when there are truly no
3095  *      reference bits set.
3096  *
3097  *      XXX: The exact number of bits to check and clear is a matter that
3098  *      should be tested and standardized at some point in the future for
3099  *      optimal aging of shared pages.
3100  */
3101 int
3102 pmap_ts_referenced(vm_page_t m)
3103 {
3104         pv_entry_t pv, pvf, pvn;
3105         unsigned *pte;
3106         int s;
3107         int rtval = 0;
3108
3109         if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3110                 return (rtval);
3111
3112         s = splvm();
3113
3114         if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3115
3116                 pvf = pv;
3117
3118                 do {
3119                         pvn = TAILQ_NEXT(pv, pv_list);
3120
3121                         TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3122
3123                         TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3124
3125                         if (!pmap_track_modified(pv->pv_va))
3126                                 continue;
3127
3128                         pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3129
3130                         if (pte && (*pte & PG_A)) {
3131 #ifdef SMP
3132                                 atomic_clear_int(pte, PG_A);
3133 #else
3134                                 atomic_clear_int_nonlocked(pte, PG_A);
3135 #endif
3136                                 rtval++;
3137                                 if (rtval > 4) {
3138                                         break;
3139                                 }
3140                         }
3141                 } while ((pv = pvn) != NULL && pv != pvf);
3142         }
3143         splx(s);
3144
3145         return (rtval);
3146 }
3147
3148 /*
3149  *      pmap_is_modified:
3150  *
3151  *      Return whether or not the specified physical page was modified
3152  *      in any physical maps.
3153  */
3154 boolean_t
3155 pmap_is_modified(vm_page_t m)
3156 {
3157         return pmap_testbit(m, PG_M);
3158 }
3159
3160 /*
3161  *      Clear the modify bits on the specified physical page.
3162  */
3163 void
3164 pmap_clear_modify(vm_page_t m)
3165 {
3166         pmap_changebit(m, PG_M, FALSE);
3167 }
3168
3169 /*
3170  *      pmap_clear_reference:
3171  *
3172  *      Clear the reference bit on the specified physical page.
3173  */
3174 void
3175 pmap_clear_reference(vm_page_t m)
3176 {
3177         pmap_changebit(m, PG_A, FALSE);
3178 }
3179
3180 /*
3181  * Miscellaneous support routines follow
3182  */
3183
3184 static void
3185 i386_protection_init(void)
3186 {
3187         int *kp, prot;
3188
3189         kp = protection_codes;
3190         for (prot = 0; prot < 8; prot++) {
3191                 switch (prot) {
3192                 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE:
3193                         /*
3194                          * Read access is also 0. There isn't any execute bit,
3195                          * so just make it readable.
3196                          */
3197                 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE:
3198                 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE:
3199                 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE:
3200                         *kp++ = 0;
3201                         break;
3202                 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE:
3203                 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE:
3204                 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE:
3205                 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
3206                         *kp++ = PG_RW;
3207                         break;
3208                 }
3209         }
3210 }
3211
3212 /*
3213  * Map a set of physical memory pages into the kernel virtual
3214  * address space. Return a pointer to where it is mapped. This
3215  * routine is intended to be used for mapping device memory,
3216  * NOT real memory.
3217  *
3218  * NOTE: we can't use pgeflag unless we invalidate the pages one at
3219  * a time.
3220  */
3221 void *
3222 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
3223 {
3224         vm_offset_t va, tmpva, offset;
3225         unsigned *pte;
3226
3227         offset = pa & PAGE_MASK;
3228         size = roundup(offset + size, PAGE_SIZE);
3229
3230         va = kmem_alloc_nofault(kernel_map, size);
3231         if (!va)
3232                 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3233
3234         pa = pa & PG_FRAME;
3235         for (tmpva = va; size > 0;) {
3236                 pte = (unsigned *)vtopte(tmpva);
3237                 *pte = pa | PG_RW | PG_V; /* | pgeflag; */
3238                 size -= PAGE_SIZE;
3239                 tmpva += PAGE_SIZE;
3240                 pa += PAGE_SIZE;
3241         }
3242         cpu_invltlb();
3243         smp_invltlb();
3244
3245         return ((void *)(va + offset));
3246 }
3247
3248 void
3249 pmap_unmapdev(vm_offset_t va, vm_size_t size)
3250 {
3251         vm_offset_t base, offset;
3252
3253         base = va & PG_FRAME;
3254         offset = va & PAGE_MASK;
3255         size = roundup(offset + size, PAGE_SIZE);
3256         pmap_qremove(va, size >> PAGE_SHIFT);
3257         kmem_free(kernel_map, base, size);
3258 }
3259
3260 /*
3261  * perform the pmap work for mincore
3262  */
3263 int
3264 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3265 {
3266         unsigned *ptep, pte;
3267         vm_page_t m;
3268         int val = 0;
3269         
3270         ptep = pmap_pte(pmap, addr);
3271         if (ptep == 0) {
3272                 return 0;
3273         }
3274
3275         if ((pte = *ptep) != 0) {
3276                 vm_offset_t pa;
3277
3278                 val = MINCORE_INCORE;
3279                 if ((pte & PG_MANAGED) == 0)
3280                         return val;
3281
3282                 pa = pte & PG_FRAME;
3283
3284                 m = PHYS_TO_VM_PAGE(pa);
3285
3286                 /*
3287                  * Modified by us
3288                  */
3289                 if (pte & PG_M)
3290                         val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3291                 /*
3292                  * Modified by someone
3293                  */
3294                 else if (m->dirty || pmap_is_modified(m))
3295                         val |= MINCORE_MODIFIED_OTHER;
3296                 /*
3297                  * Referenced by us
3298                  */
3299                 if (pte & PG_A)
3300                         val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3301
3302                 /*
3303                  * Referenced by someone
3304                  */
3305                 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3306                         val |= MINCORE_REFERENCED_OTHER;
3307                         vm_page_flag_set(m, PG_REFERENCED);
3308                 }
3309         } 
3310         return val;
3311 }
3312
3313 void
3314 pmap_activate(struct proc *p)
3315 {
3316         pmap_t  pmap;
3317
3318         pmap = vmspace_pmap(p->p_vmspace);
3319 #if defined(SMP)
3320         atomic_set_int(&pmap->pm_active, 1 << mycpu->gd_cpuid);
3321 #else
3322         pmap->pm_active |= 1;
3323 #endif
3324 #if defined(SWTCH_OPTIM_STATS)
3325         tlb_flush_count++;
3326 #endif
3327         p->p_thread->td_pcb->pcb_cr3 = vtophys(pmap->pm_pdir);
3328         load_cr3(p->p_thread->td_pcb->pcb_cr3);
3329 }
3330
3331 vm_offset_t
3332 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3333 {
3334
3335         if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3336                 return addr;
3337         }
3338
3339         addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3340         return addr;
3341 }
3342
3343
3344 #if defined(PMAP_DEBUG)
3345 int
3346 pmap_pid_dump(int pid)
3347 {
3348         pmap_t pmap;
3349         struct proc *p;
3350         int npte = 0;
3351         int index;
3352         FOREACH_PROC_IN_SYSTEM(p) {
3353                 if (p->p_pid != pid)
3354                         continue;
3355
3356                 if (p->p_vmspace) {
3357                         int i,j;
3358                         index = 0;
3359                         pmap = vmspace_pmap(p->p_vmspace);
3360                         for(i=0;i<1024;i++) {
3361                                 pd_entry_t *pde;
3362                                 unsigned *pte;
3363                                 unsigned base = i << PDRSHIFT;
3364                                 
3365                                 pde = &pmap->pm_pdir[i];
3366                                 if (pde && pmap_pde_v(pde)) {
3367                                         for(j=0;j<1024;j++) {
3368                                                 unsigned va = base + (j << PAGE_SHIFT);
3369                                                 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) {
3370                                                         if (index) {
3371                                                                 index = 0;
3372                                                                 printf("\n");
3373                                                         }
3374                                                         return npte;
3375                                                 }
3376                                                 pte = pmap_pte_quick( pmap, va);
3377                                                 if (pte && pmap_pte_v(pte)) {
3378                                                         vm_offset_t pa;
3379                                                         vm_page_t m;
3380                                                         pa = *(int *)pte;
3381                                                         m = PHYS_TO_VM_PAGE(pa);
3382                                                         printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x",
3383                                                                 va, pa, m->hold_count, m->wire_count, m->flags);
3384                                                         npte++;
3385                                                         index++;
3386                                                         if (index >= 2) {
3387                                                                 index = 0;
3388                                                                 printf("\n");
3389                                                         } else {
3390                                                                 printf(" ");
3391                                                         }
3392                                                 }
3393                                         }
3394                                 }
3395                         }
3396                 }
3397         }
3398         return npte;
3399 }
3400 #endif
3401
3402 #if defined(DEBUG)
3403
3404 static void     pads (pmap_t pm);
3405 void            pmap_pvdump (vm_paddr_t pa);
3406
3407 /* print address space of pmap*/
3408 static void
3409 pads(pmap_t pm)
3410 {
3411         unsigned va, i, j;
3412         unsigned *ptep;
3413
3414         if (pm == kernel_pmap)
3415                 return;
3416         for (i = 0; i < 1024; i++)
3417                 if (pm->pm_pdir[i])
3418                         for (j = 0; j < 1024; j++) {
3419                                 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
3420                                 if (pm == kernel_pmap && va < KERNBASE)
3421                                         continue;
3422                                 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS)
3423                                         continue;
3424                                 ptep = pmap_pte_quick(pm, va);
3425                                 if (pmap_pte_v(ptep))
3426                                         printf("%x:%x ", va, *(int *) ptep);
3427                         };
3428
3429 }
3430
3431 void
3432 pmap_pvdump(vm_paddr_t pa)
3433 {
3434         pv_entry_t pv;
3435         vm_page_t m;
3436
3437         printf("pa %08llx", (long long)pa);
3438         m = PHYS_TO_VM_PAGE(pa);
3439         TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3440 #ifdef used_to_be
3441                 printf(" -> pmap %p, va %x, flags %x",
3442                     (void *)pv->pv_pmap, pv->pv_va, pv->pv_flags);
3443 #endif
3444                 printf(" -> pmap %p, va %x", (void *)pv->pv_pmap, pv->pv_va);
3445                 pads(pv->pv_pmap);
3446         }
3447         printf(" ");
3448 }
3449 #endif