Move all the softclock/callout initialization out of MD and into MI. Get rid
[dragonfly.git] / sys / i386 / 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/i386/i386/Attic/pmap.c,v 1.45 2004/07/29 20:30:32 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                 vm_offset_t pteva;
1027                 /*
1028                  * unmap the page table page
1029                  */
1030                 pmap_inval_add(info, pmap, -1);
1031                 pmap->pm_pdir[m->pindex] = 0;
1032                 --pmap->pm_stats.resident_count;
1033                 if ((((unsigned)pmap->pm_pdir[PTDPTDI]) & PG_FRAME) ==
1034                         (((unsigned) PTDpde) & PG_FRAME)) {
1035                         /*
1036                          * Do a invltlb to make the invalidated mapping
1037                          * take effect immediately.
1038                          */
1039                         pteva = UPT_MIN_ADDRESS + i386_ptob(m->pindex);
1040                 }
1041
1042                 if (pmap->pm_ptphint == m)
1043                         pmap->pm_ptphint = NULL;
1044
1045                 /*
1046                  * If the page is finally unwired, simply free it.
1047                  */
1048                 --m->wire_count;
1049                 if (m->wire_count == 0) {
1050                         vm_page_flash(m);
1051                         vm_page_busy(m);
1052                         vm_page_free_zero(m);
1053                         --vmstats.v_wire_count;
1054                 }
1055                 return 1;
1056         }
1057         return 0;
1058 }
1059
1060 static PMAP_INLINE int
1061 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m, pmap_inval_info_t info)
1062 {
1063         vm_page_unhold(m);
1064         if (m->hold_count == 0)
1065                 return _pmap_unwire_pte_hold(pmap, m, info);
1066         else
1067                 return 0;
1068 }
1069
1070 /*
1071  * After removing a page table entry, this routine is used to
1072  * conditionally free the page, and manage the hold/wire counts.
1073  */
1074 static int
1075 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte,
1076                 pmap_inval_info_t info)
1077 {
1078         unsigned ptepindex;
1079         if (va >= UPT_MIN_ADDRESS)
1080                 return 0;
1081
1082         if (mpte == NULL) {
1083                 ptepindex = (va >> PDRSHIFT);
1084                 if (pmap->pm_ptphint &&
1085                         (pmap->pm_ptphint->pindex == ptepindex)) {
1086                         mpte = pmap->pm_ptphint;
1087                 } else {
1088                         pmap_inval_flush(info);
1089                         mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1090                         pmap->pm_ptphint = mpte;
1091                 }
1092         }
1093
1094         return pmap_unwire_pte_hold(pmap, mpte, info);
1095 }
1096
1097 void
1098 pmap_pinit0(struct pmap *pmap)
1099 {
1100         pmap->pm_pdir =
1101                 (pd_entry_t *)kmem_alloc_pageable(kernel_map, PAGE_SIZE);
1102         pmap_kenter((vm_offset_t)pmap->pm_pdir, (vm_offset_t) IdlePTD);
1103         pmap->pm_count = 1;
1104         pmap->pm_active = 0;
1105         pmap->pm_ptphint = NULL;
1106         TAILQ_INIT(&pmap->pm_pvlist);
1107         bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1108 }
1109
1110 /*
1111  * Initialize a preallocated and zeroed pmap structure,
1112  * such as one in a vmspace structure.
1113  */
1114 void
1115 pmap_pinit(struct pmap *pmap)
1116 {
1117         vm_page_t ptdpg;
1118
1119         /*
1120          * No need to allocate page table space yet but we do need a valid
1121          * page directory table.
1122          */
1123         if (pmap->pm_pdir == NULL) {
1124                 pmap->pm_pdir =
1125                         (pd_entry_t *)kmem_alloc_pageable(kernel_map, PAGE_SIZE);
1126         }
1127
1128         /*
1129          * allocate object for the ptes
1130          */
1131         if (pmap->pm_pteobj == NULL)
1132                 pmap->pm_pteobj = vm_object_allocate( OBJT_DEFAULT, PTDPTDI + 1);
1133
1134         /*
1135          * allocate the page directory page
1136          */
1137         ptdpg = vm_page_grab( pmap->pm_pteobj, PTDPTDI,
1138                         VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1139
1140         ptdpg->wire_count = 1;
1141         ++vmstats.v_wire_count;
1142
1143
1144         vm_page_flag_clear(ptdpg, PG_MAPPED | PG_BUSY); /* not usually mapped*/
1145         ptdpg->valid = VM_PAGE_BITS_ALL;
1146
1147         pmap_kenter((vm_offset_t)pmap->pm_pdir, VM_PAGE_TO_PHYS(ptdpg));
1148         if ((ptdpg->flags & PG_ZERO) == 0)
1149                 bzero(pmap->pm_pdir, PAGE_SIZE);
1150
1151         pmap->pm_pdir[MPPTDI] = PTD[MPPTDI];
1152
1153         /* install self-referential address mapping entry */
1154         *(unsigned *) (pmap->pm_pdir + PTDPTDI) =
1155                 VM_PAGE_TO_PHYS(ptdpg) | PG_V | PG_RW | PG_A | PG_M;
1156
1157         pmap->pm_count = 1;
1158         pmap->pm_active = 0;
1159         pmap->pm_ptphint = NULL;
1160         TAILQ_INIT(&pmap->pm_pvlist);
1161         bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1162 }
1163
1164 /*
1165  * Wire in kernel global address entries.  To avoid a race condition
1166  * between pmap initialization and pmap_growkernel, this procedure
1167  * should be called after the vmspace is attached to the process
1168  * but before this pmap is activated.
1169  */
1170 void
1171 pmap_pinit2(struct pmap *pmap)
1172 {
1173         /* XXX copies current process, does not fill in MPPTDI */
1174         bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * PTESIZE);
1175 }
1176
1177 static int
1178 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1179 {
1180         unsigned *pde = (unsigned *) pmap->pm_pdir;
1181         /*
1182          * This code optimizes the case of freeing non-busy
1183          * page-table pages.  Those pages are zero now, and
1184          * might as well be placed directly into the zero queue.
1185          */
1186         if (vm_page_sleep_busy(p, FALSE, "pmaprl"))
1187                 return 0;
1188
1189         vm_page_busy(p);
1190
1191         /*
1192          * Remove the page table page from the processes address space.
1193          */
1194         pde[p->pindex] = 0;
1195         pmap->pm_stats.resident_count--;
1196
1197         if (p->hold_count)  {
1198                 panic("pmap_release: freeing held page table page");
1199         }
1200         /*
1201          * Page directory pages need to have the kernel
1202          * stuff cleared, so they can go into the zero queue also.
1203          */
1204         if (p->pindex == PTDPTDI) {
1205                 bzero(pde + KPTDI, nkpt * PTESIZE);
1206                 pde[MPPTDI] = 0;
1207                 pde[APTDPTDI] = 0;
1208                 pmap_kremove((vm_offset_t)pmap->pm_pdir);
1209         }
1210
1211         if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1212                 pmap->pm_ptphint = NULL;
1213
1214         p->wire_count--;
1215         vmstats.v_wire_count--;
1216         vm_page_free_zero(p);
1217         return 1;
1218 }
1219
1220 /*
1221  * this routine is called if the page table page is not
1222  * mapped correctly.
1223  */
1224 static vm_page_t
1225 _pmap_allocpte(pmap_t pmap, unsigned ptepindex)
1226 {
1227         vm_offset_t pteva, ptepa;
1228         vm_page_t m;
1229
1230         /*
1231          * Find or fabricate a new pagetable page
1232          */
1233         m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1234                         VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1235
1236         KASSERT(m->queue == PQ_NONE,
1237                 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1238
1239         if (m->wire_count == 0)
1240                 vmstats.v_wire_count++;
1241         m->wire_count++;
1242
1243         /*
1244          * Increment the hold count for the page table page
1245          * (denoting a new mapping.)
1246          */
1247         m->hold_count++;
1248
1249         /*
1250          * Map the pagetable page into the process address space, if
1251          * it isn't already there.
1252          */
1253
1254         pmap->pm_stats.resident_count++;
1255
1256         ptepa = VM_PAGE_TO_PHYS(m);
1257         pmap->pm_pdir[ptepindex] =
1258                 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_A | PG_M);
1259
1260         /*
1261          * Set the page table hint
1262          */
1263         pmap->pm_ptphint = m;
1264
1265         /*
1266          * Try to use the new mapping, but if we cannot, then
1267          * do it with the routine that maps the page explicitly.
1268          */
1269         if ((m->flags & PG_ZERO) == 0) {
1270                 if ((((unsigned)pmap->pm_pdir[PTDPTDI]) & PG_FRAME) ==
1271                         (((unsigned) PTDpde) & PG_FRAME)) {
1272                         pteva = UPT_MIN_ADDRESS + i386_ptob(ptepindex);
1273                         bzero((caddr_t) pteva, PAGE_SIZE);
1274                 } else {
1275                         pmap_zero_page(ptepa);
1276                 }
1277         }
1278
1279         m->valid = VM_PAGE_BITS_ALL;
1280         vm_page_flag_clear(m, PG_ZERO);
1281         vm_page_flag_set(m, PG_MAPPED);
1282         vm_page_wakeup(m);
1283
1284         return m;
1285 }
1286
1287 static vm_page_t
1288 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1289 {
1290         unsigned ptepindex;
1291         vm_offset_t ptepa;
1292         vm_page_t m;
1293
1294         /*
1295          * Calculate pagetable page index
1296          */
1297         ptepindex = va >> PDRSHIFT;
1298
1299         /*
1300          * Get the page directory entry
1301          */
1302         ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
1303
1304         /*
1305          * This supports switching from a 4MB page to a
1306          * normal 4K page.
1307          */
1308         if (ptepa & PG_PS) {
1309                 pmap->pm_pdir[ptepindex] = 0;
1310                 ptepa = 0;
1311                 cpu_invltlb();
1312                 smp_invltlb();
1313         }
1314
1315         /*
1316          * If the page table page is mapped, we just increment the
1317          * hold count, and activate it.
1318          */
1319         if (ptepa) {
1320                 /*
1321                  * In order to get the page table page, try the
1322                  * hint first.
1323                  */
1324                 if (pmap->pm_ptphint &&
1325                         (pmap->pm_ptphint->pindex == ptepindex)) {
1326                         m = pmap->pm_ptphint;
1327                 } else {
1328                         m = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1329                         pmap->pm_ptphint = m;
1330                 }
1331                 m->hold_count++;
1332                 return m;
1333         }
1334         /*
1335          * Here if the pte page isn't mapped, or if it has been deallocated.
1336          */
1337         return _pmap_allocpte(pmap, ptepindex);
1338 }
1339
1340
1341 /***************************************************
1342 * Pmap allocation/deallocation routines.
1343  ***************************************************/
1344
1345 /*
1346  * Release any resources held by the given physical map.
1347  * Called when a pmap initialized by pmap_pinit is being released.
1348  * Should only be called if the map contains no valid mappings.
1349  */
1350 void
1351 pmap_release(struct pmap *pmap)
1352 {
1353         vm_page_t p,n,ptdpg;
1354         vm_object_t object = pmap->pm_pteobj;
1355         int curgeneration;
1356         int s;
1357
1358 #if defined(DIAGNOSTIC)
1359         if (object->ref_count != 1)
1360                 panic("pmap_release: pteobj reference count != 1");
1361 #endif
1362         
1363         ptdpg = NULL;
1364 retry:
1365         s = splvm();
1366         curgeneration = object->generation;
1367         for (p = TAILQ_FIRST(&object->memq); p != NULL; p = n) {
1368                 n = TAILQ_NEXT(p, listq);
1369                 if (p->pindex == PTDPTDI) {
1370                         ptdpg = p;
1371                         continue;
1372                 }
1373                 while (1) {
1374                         if (!pmap_release_free_page(pmap, p) &&
1375                             (object->generation != curgeneration)) {
1376                                 splx(s);
1377                                 goto retry;
1378                         }
1379                 }
1380         }
1381         splx(s);
1382
1383         if (ptdpg && !pmap_release_free_page(pmap, ptdpg))
1384                 goto retry;
1385 }
1386 \f
1387 static int
1388 kvm_size(SYSCTL_HANDLER_ARGS)
1389 {
1390         unsigned long ksize = VM_MAX_KERNEL_ADDRESS - KERNBASE;
1391
1392         return sysctl_handle_long(oidp, &ksize, 0, req);
1393 }
1394 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD, 
1395     0, 0, kvm_size, "IU", "Size of KVM");
1396
1397 static int
1398 kvm_free(SYSCTL_HANDLER_ARGS)
1399 {
1400         unsigned long kfree = VM_MAX_KERNEL_ADDRESS - kernel_vm_end;
1401
1402         return sysctl_handle_long(oidp, &kfree, 0, req);
1403 }
1404 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD, 
1405     0, 0, kvm_free, "IU", "Amount of KVM free");
1406
1407 /*
1408  * grow the number of kernel page table entries, if needed
1409  */
1410 void
1411 pmap_growkernel(vm_offset_t addr)
1412 {
1413         struct proc *p;
1414         struct pmap *pmap;
1415         int s;
1416         vm_offset_t ptppaddr;
1417         vm_page_t nkpg;
1418         pd_entry_t newpdir;
1419
1420         s = splhigh();
1421         if (kernel_vm_end == 0) {
1422                 kernel_vm_end = KERNBASE;
1423                 nkpt = 0;
1424                 while (pdir_pde(PTD, kernel_vm_end)) {
1425                         kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1426                         nkpt++;
1427                 }
1428         }
1429         addr = (addr + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1430         while (kernel_vm_end < addr) {
1431                 if (pdir_pde(PTD, kernel_vm_end)) {
1432                         kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1433                         continue;
1434                 }
1435
1436                 /*
1437                  * This index is bogus, but out of the way
1438                  */
1439                 nkpg = vm_page_alloc(kptobj, nkpt, 
1440                         VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM | VM_ALLOC_INTERRUPT);
1441                 if (nkpg == NULL)
1442                         panic("pmap_growkernel: no memory to grow kernel");
1443
1444                 nkpt++;
1445
1446                 vm_page_wire(nkpg);
1447                 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1448                 pmap_zero_page(ptppaddr);
1449                 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
1450                 pdir_pde(PTD, kernel_vm_end) = newpdir;
1451
1452                 FOREACH_PROC_IN_SYSTEM(p) {
1453                         if (p->p_vmspace) {
1454                                 pmap = vmspace_pmap(p->p_vmspace);
1455                                 *pmap_pde(pmap, kernel_vm_end) = newpdir;
1456                         }
1457                 }
1458                 *pmap_pde(kernel_pmap, kernel_vm_end) = newpdir;
1459                 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1460         }
1461         splx(s);
1462 }
1463
1464 /*
1465  *      Retire the given physical map from service.
1466  *      Should only be called if the map contains
1467  *      no valid mappings.
1468  */
1469 void
1470 pmap_destroy(pmap_t pmap)
1471 {
1472         int count;
1473
1474         if (pmap == NULL)
1475                 return;
1476
1477         count = --pmap->pm_count;
1478         if (count == 0) {
1479                 pmap_release(pmap);
1480                 panic("destroying a pmap is not yet implemented");
1481         }
1482 }
1483
1484 /*
1485  *      Add a reference to the specified pmap.
1486  */
1487 void
1488 pmap_reference(pmap_t pmap)
1489 {
1490         if (pmap != NULL) {
1491                 pmap->pm_count++;
1492         }
1493 }
1494
1495 /***************************************************
1496 * page management routines.
1497  ***************************************************/
1498
1499 /*
1500  * free the pv_entry back to the free list.  This function may be
1501  * called from an interrupt.
1502  */
1503 static PMAP_INLINE void
1504 free_pv_entry(pv_entry_t pv)
1505 {
1506         pv_entry_count--;
1507         zfree(pvzone, pv);
1508 }
1509
1510 /*
1511  * get a new pv_entry, allocating a block from the system
1512  * when needed.  This function may be called from an interrupt.
1513  */
1514 static pv_entry_t
1515 get_pv_entry(void)
1516 {
1517         pv_entry_count++;
1518         if (pv_entry_high_water &&
1519                 (pv_entry_count > pv_entry_high_water) &&
1520                 (pmap_pagedaemon_waken == 0)) {
1521                 pmap_pagedaemon_waken = 1;
1522                 wakeup (&vm_pages_needed);
1523         }
1524         return zalloc(pvzone);
1525 }
1526
1527 /*
1528  * This routine is very drastic, but can save the system
1529  * in a pinch.
1530  */
1531 void
1532 pmap_collect(void)
1533 {
1534         int i;
1535         vm_page_t m;
1536         static int warningdone=0;
1537
1538         if (pmap_pagedaemon_waken == 0)
1539                 return;
1540
1541         if (warningdone < 5) {
1542                 printf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
1543                 warningdone++;
1544         }
1545
1546         for(i = 0; i < vm_page_array_size; i++) {
1547                 m = &vm_page_array[i];
1548                 if (m->wire_count || m->hold_count || m->busy ||
1549                     (m->flags & PG_BUSY))
1550                         continue;
1551                 pmap_remove_all(m);
1552         }
1553         pmap_pagedaemon_waken = 0;
1554 }
1555         
1556
1557 /*
1558  * If it is the first entry on the list, it is actually
1559  * in the header and we must copy the following entry up
1560  * to the header.  Otherwise we must search the list for
1561  * the entry.  In either case we free the now unused entry.
1562  */
1563 static int
1564 pmap_remove_entry(struct pmap *pmap, vm_page_t m, 
1565                         vm_offset_t va, pmap_inval_info_t info)
1566 {
1567         pv_entry_t pv;
1568         int rtval;
1569         int s;
1570
1571         s = splvm();
1572         if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1573                 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1574                         if (pmap == pv->pv_pmap && va == pv->pv_va) 
1575                                 break;
1576                 }
1577         } else {
1578                 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1579                         if (va == pv->pv_va) 
1580                                 break;
1581                 }
1582         }
1583
1584         rtval = 0;
1585         if (pv) {
1586                 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem, info);
1587                 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1588                 m->md.pv_list_count--;
1589                 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
1590                         vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1591                 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1592                 free_pv_entry(pv);
1593         }
1594         splx(s);
1595         return rtval;
1596 }
1597
1598 /*
1599  * Create a pv entry for page at pa for
1600  * (pmap, va).
1601  */
1602 static void
1603 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
1604 {
1605         int s;
1606         pv_entry_t pv;
1607
1608         s = splvm();
1609         pv = get_pv_entry();
1610         pv->pv_va = va;
1611         pv->pv_pmap = pmap;
1612         pv->pv_ptem = mpte;
1613
1614         TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1615         TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1616         m->md.pv_list_count++;
1617
1618         splx(s);
1619 }
1620
1621 /*
1622  * pmap_remove_pte: do the things to unmap a page in a process
1623  */
1624 static int
1625 pmap_remove_pte(struct pmap *pmap, unsigned *ptq, vm_offset_t va,
1626         pmap_inval_info_t info)
1627 {
1628         unsigned oldpte;
1629         vm_page_t m;
1630
1631         pmap_inval_add(info, pmap, va);
1632         oldpte = loadandclear(ptq);
1633         if (oldpte & PG_W)
1634                 pmap->pm_stats.wired_count -= 1;
1635         /*
1636          * Machines that don't support invlpg, also don't support
1637          * PG_G.  XXX PG_G is disabled for SMP so don't worry about
1638          * the SMP case.
1639          */
1640         if (oldpte & PG_G)
1641                 cpu_invlpg((void *)va);
1642         pmap->pm_stats.resident_count -= 1;
1643         if (oldpte & PG_MANAGED) {
1644                 m = PHYS_TO_VM_PAGE(oldpte);
1645                 if (oldpte & PG_M) {
1646 #if defined(PMAP_DIAGNOSTIC)
1647                         if (pmap_nw_modified((pt_entry_t) oldpte)) {
1648                                 printf(
1649         "pmap_remove: modified page not writable: va: 0x%x, pte: 0x%x\n",
1650                                     va, oldpte);
1651                         }
1652 #endif
1653                         if (pmap_track_modified(va))
1654                                 vm_page_dirty(m);
1655                 }
1656                 if (oldpte & PG_A)
1657                         vm_page_flag_set(m, PG_REFERENCED);
1658                 return pmap_remove_entry(pmap, m, va, info);
1659         } else {
1660                 return pmap_unuse_pt(pmap, va, NULL, info);
1661         }
1662
1663         return 0;
1664 }
1665
1666 /*
1667  * pmap_remove_page:
1668  *
1669  *      Remove a single page from a process address space.
1670  *
1671  *      This function may not be called from an interrupt if the pmap is
1672  *      not kernel_pmap.
1673  */
1674 static void
1675 pmap_remove_page(struct pmap *pmap, vm_offset_t va, pmap_inval_info_t info)
1676 {
1677         unsigned *ptq;
1678
1679         /*
1680          * if there is no pte for this address, just skip it!!!  Otherwise
1681          * get a local va for mappings for this pmap and remove the entry.
1682          */
1683         if (*pmap_pde(pmap, va) != 0) {
1684                 ptq = get_ptbase(pmap) + i386_btop(va);
1685                 if (*ptq) {
1686                         pmap_remove_pte(pmap, ptq, va, info);
1687                 }
1688         }
1689 }
1690
1691 /*
1692  * pmap_remove:
1693  *
1694  *      Remove the given range of addresses from the specified map.
1695  *
1696  *      It is assumed that the start and end are properly
1697  *      rounded to the page size.
1698  *
1699  *      This function may not be called from an interrupt if the pmap is
1700  *      not kernel_pmap.
1701  */
1702 void
1703 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
1704 {
1705         unsigned *ptbase;
1706         vm_offset_t pdnxt;
1707         vm_offset_t ptpaddr;
1708         vm_offset_t sindex, eindex;
1709         struct pmap_inval_info info;
1710
1711         if (pmap == NULL)
1712                 return;
1713
1714         if (pmap->pm_stats.resident_count == 0)
1715                 return;
1716
1717         pmap_inval_init(&info);
1718
1719         /*
1720          * special handling of removing one page.  a very
1721          * common operation and easy to short circuit some
1722          * code.
1723          */
1724         if (((sva + PAGE_SIZE) == eva) && 
1725                 (((unsigned) pmap->pm_pdir[(sva >> PDRSHIFT)] & PG_PS) == 0)) {
1726                 pmap_remove_page(pmap, sva, &info);
1727                 pmap_inval_flush(&info);
1728                 return;
1729         }
1730
1731         /*
1732          * Get a local virtual address for the mappings that are being
1733          * worked with.
1734          */
1735         ptbase = get_ptbase(pmap);
1736
1737         sindex = i386_btop(sva);
1738         eindex = i386_btop(eva);
1739
1740         for (; sindex < eindex; sindex = pdnxt) {
1741                 unsigned pdirindex;
1742
1743                 /*
1744                  * Calculate index for next page table.
1745                  */
1746                 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1747                 if (pmap->pm_stats.resident_count == 0)
1748                         break;
1749
1750                 pdirindex = sindex / NPDEPG;
1751                 if (((ptpaddr = (unsigned) pmap->pm_pdir[pdirindex]) & PG_PS) != 0) {
1752                         pmap_inval_add(&info, pmap, -1);
1753                         pmap->pm_pdir[pdirindex] = 0;
1754                         pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1755                         continue;
1756                 }
1757
1758                 /*
1759                  * Weed out invalid mappings. Note: we assume that the page
1760                  * directory table is always allocated, and in kernel virtual.
1761                  */
1762                 if (ptpaddr == 0)
1763                         continue;
1764
1765                 /*
1766                  * Limit our scan to either the end of the va represented
1767                  * by the current page table page, or to the end of the
1768                  * range being removed.
1769                  */
1770                 if (pdnxt > eindex) {
1771                         pdnxt = eindex;
1772                 }
1773
1774                 for (; sindex != pdnxt; sindex++) {
1775                         vm_offset_t va;
1776                         if (ptbase[sindex] == 0)
1777                                 continue;
1778                         va = i386_ptob(sindex);
1779                         if (pmap_remove_pte(pmap, ptbase + sindex, va, &info))
1780                                 break;
1781                 }
1782         }
1783         pmap_inval_flush(&info);
1784 }
1785
1786 /*
1787  * pmap_remove_all:
1788  *
1789  *      Removes this physical page from all physical maps in which it resides.
1790  *      Reflects back modify bits to the pager.
1791  *
1792  *      This routine may not be called from an interrupt.
1793  */
1794
1795 static void
1796 pmap_remove_all(vm_page_t m)
1797 {
1798         struct pmap_inval_info info;
1799         unsigned *pte, tpte;
1800         pv_entry_t pv;
1801         int s;
1802
1803 #if defined(PMAP_DIAGNOSTIC)
1804         /*
1805          * XXX this makes pmap_page_protect(NONE) illegal for non-managed
1806          * pages!
1807          */
1808         if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
1809                 panic("pmap_page_protect: illegal for unmanaged page, va: 0x%08llx", (long long)VM_PAGE_TO_PHYS(m));
1810         }
1811 #endif
1812
1813         pmap_inval_init(&info);
1814         s = splvm();
1815         while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
1816                 pv->pv_pmap->pm_stats.resident_count--;
1817
1818                 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
1819                 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
1820
1821                 tpte = loadandclear(pte);
1822                 if (tpte & PG_W)
1823                         pv->pv_pmap->pm_stats.wired_count--;
1824
1825                 if (tpte & PG_A)
1826                         vm_page_flag_set(m, PG_REFERENCED);
1827
1828                 /*
1829                  * Update the vm_page_t clean and reference bits.
1830                  */
1831                 if (tpte & PG_M) {
1832 #if defined(PMAP_DIAGNOSTIC)
1833                         if (pmap_nw_modified((pt_entry_t) tpte)) {
1834                                 printf(
1835         "pmap_remove_all: modified page not writable: va: 0x%x, pte: 0x%x\n",
1836                                     pv->pv_va, tpte);
1837                         }
1838 #endif
1839                         if (pmap_track_modified(pv->pv_va))
1840                                 vm_page_dirty(m);
1841                 }
1842                 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
1843                 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1844                 m->md.pv_list_count--;
1845                 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem, &info);
1846                 free_pv_entry(pv);
1847         }
1848
1849         vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1850         splx(s);
1851         pmap_inval_flush(&info);
1852 }
1853
1854 /*
1855  * pmap_protect:
1856  *
1857  *      Set the physical protection on the specified range of this map
1858  *      as requested.
1859  *
1860  *      This function may not be called from an interrupt if the map is
1861  *      not the kernel_pmap.
1862  */
1863 void
1864 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
1865 {
1866         unsigned *ptbase;
1867         vm_offset_t pdnxt, ptpaddr;
1868         vm_pindex_t sindex, eindex;
1869         pmap_inval_info info;
1870
1871         if (pmap == NULL)
1872                 return;
1873
1874         if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
1875                 pmap_remove(pmap, sva, eva);
1876                 return;
1877         }
1878
1879         if (prot & VM_PROT_WRITE)
1880                 return;
1881
1882         pmap_inval_init(&info);
1883
1884         ptbase = get_ptbase(pmap);
1885
1886         sindex = i386_btop(sva);
1887         eindex = i386_btop(eva);
1888
1889         for (; sindex < eindex; sindex = pdnxt) {
1890
1891                 unsigned pdirindex;
1892
1893                 pdnxt = ((sindex + NPTEPG) & ~(NPTEPG - 1));
1894
1895                 pdirindex = sindex / NPDEPG;
1896                 if (((ptpaddr = (unsigned) pmap->pm_pdir[pdirindex]) & PG_PS) != 0) {
1897                         pmap_inval_add(&info, pmap, -1);
1898                         (unsigned) pmap->pm_pdir[pdirindex] &= ~(PG_M|PG_RW);
1899                         pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1900                         continue;
1901                 }
1902
1903                 /*
1904                  * Weed out invalid mappings. Note: we assume that the page
1905                  * directory table is always allocated, and in kernel virtual.
1906                  */
1907                 if (ptpaddr == 0)
1908                         continue;
1909
1910                 if (pdnxt > eindex) {
1911                         pdnxt = eindex;
1912                 }
1913
1914                 for (; sindex != pdnxt; sindex++) {
1915
1916                         unsigned pbits;
1917                         vm_page_t m;
1918
1919                         /* XXX this isn't optimal */
1920                         pmap_inval_add(&info, pmap, i386_ptob(sindex));
1921                         pbits = ptbase[sindex];
1922
1923                         if (pbits & PG_MANAGED) {
1924                                 m = NULL;
1925                                 if (pbits & PG_A) {
1926                                         m = PHYS_TO_VM_PAGE(pbits);
1927                                         vm_page_flag_set(m, PG_REFERENCED);
1928                                         pbits &= ~PG_A;
1929                                 }
1930                                 if (pbits & PG_M) {
1931                                         if (pmap_track_modified(i386_ptob(sindex))) {
1932                                                 if (m == NULL)
1933                                                         m = PHYS_TO_VM_PAGE(pbits);
1934                                                 vm_page_dirty(m);
1935                                                 pbits &= ~PG_M;
1936                                         }
1937                                 }
1938                         }
1939
1940                         pbits &= ~PG_RW;
1941
1942                         if (pbits != ptbase[sindex]) {
1943                                 ptbase[sindex] = pbits;
1944                         }
1945                 }
1946         }
1947         pmap_inval_flush(&info);
1948 }
1949
1950 /*
1951  *      Insert the given physical page (p) at
1952  *      the specified virtual address (v) in the
1953  *      target physical map with the protection requested.
1954  *
1955  *      If specified, the page will be wired down, meaning
1956  *      that the related pte can not be reclaimed.
1957  *
1958  *      NB:  This is the only routine which MAY NOT lazy-evaluate
1959  *      or lose information.  That is, this routine must actually
1960  *      insert this page into the given map NOW.
1961  */
1962 void
1963 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
1964            boolean_t wired)
1965 {
1966         vm_paddr_t pa;
1967         unsigned *pte;
1968         vm_paddr_t opa;
1969         vm_offset_t origpte, newpte;
1970         vm_page_t mpte;
1971         pmap_inval_info info;
1972
1973         if (pmap == NULL)
1974                 return;
1975
1976         va &= PG_FRAME;
1977 #ifdef PMAP_DIAGNOSTIC
1978         if (va > VM_MAX_KERNEL_ADDRESS)
1979                 panic("pmap_enter: toobig");
1980         if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
1981                 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%x)", va);
1982 #endif
1983
1984         mpte = NULL;
1985         /*
1986          * In the case that a page table page is not
1987          * resident, we are creating it here.
1988          */
1989         if (va < UPT_MIN_ADDRESS) {
1990                 mpte = pmap_allocpte(pmap, va);
1991         }
1992
1993         pmap_inval_init(&info);
1994         pte = pmap_pte(pmap, va);
1995
1996         /*
1997          * Page Directory table entry not valid, we need a new PT page
1998          */
1999         if (pte == NULL) {
2000                 panic("pmap_enter: invalid page directory pdir=%x, va=0x%x\n",
2001                      (unsigned) pmap->pm_pdir[PTDPTDI], va);
2002         }
2003
2004         pa = VM_PAGE_TO_PHYS(m) & PG_FRAME;
2005         pmap_inval_add(&info, pmap, va); /* XXX non-optimal */
2006         origpte = *(vm_offset_t *)pte;
2007         opa = origpte & PG_FRAME;
2008
2009         if (origpte & PG_PS)
2010                 panic("pmap_enter: attempted pmap_enter on 4MB page");
2011
2012         /*
2013          * Mapping has not changed, must be protection or wiring change.
2014          */
2015         if (origpte && (opa == pa)) {
2016                 /*
2017                  * Wiring change, just update stats. We don't worry about
2018                  * wiring PT pages as they remain resident as long as there
2019                  * are valid mappings in them. Hence, if a user page is wired,
2020                  * the PT page will be also.
2021                  */
2022                 if (wired && ((origpte & PG_W) == 0))
2023                         pmap->pm_stats.wired_count++;
2024                 else if (!wired && (origpte & PG_W))
2025                         pmap->pm_stats.wired_count--;
2026
2027 #if defined(PMAP_DIAGNOSTIC)
2028                 if (pmap_nw_modified((pt_entry_t) origpte)) {
2029                         printf(
2030         "pmap_enter: modified page not writable: va: 0x%x, pte: 0x%x\n",
2031                             va, origpte);
2032                 }
2033 #endif
2034
2035                 /*
2036                  * Remove the extra pte reference.  Note that we cannot
2037                  * optimize the RO->RW case because we have adjusted the
2038                  * wiring count above and may need to adjust the wiring
2039                  * bits below.
2040                  */
2041                 if (mpte)
2042                         mpte->hold_count--;
2043
2044                 /*
2045                  * We might be turning off write access to the page,
2046                  * so we go ahead and sense modify status.
2047                  */
2048                 if (origpte & PG_MANAGED) {
2049                         if ((origpte & PG_M) && pmap_track_modified(va)) {
2050                                 vm_page_t om;
2051                                 om = PHYS_TO_VM_PAGE(opa);
2052                                 vm_page_dirty(om);
2053                         }
2054                         pa |= PG_MANAGED;
2055                 }
2056                 goto validate;
2057         } 
2058         /*
2059          * Mapping has changed, invalidate old range and fall through to
2060          * handle validating new mapping.
2061          */
2062         if (opa) {
2063                 int err;
2064                 err = pmap_remove_pte(pmap, pte, va, &info);
2065                 if (err)
2066                         panic("pmap_enter: pte vanished, va: 0x%x", va);
2067         }
2068
2069         /*
2070          * Enter on the PV list if part of our managed memory. Note that we
2071          * raise IPL while manipulating pv_table since pmap_enter can be
2072          * called at interrupt time.
2073          */
2074         if (pmap_initialized && 
2075             (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2076                 pmap_insert_entry(pmap, va, mpte, m);
2077                 pa |= PG_MANAGED;
2078         }
2079
2080         /*
2081          * Increment counters
2082          */
2083         pmap->pm_stats.resident_count++;
2084         if (wired)
2085                 pmap->pm_stats.wired_count++;
2086
2087 validate:
2088         /*
2089          * Now validate mapping with desired protection/wiring.
2090          */
2091         newpte = (vm_offset_t) (pa | pte_prot(pmap, prot) | PG_V);
2092
2093         if (wired)
2094                 newpte |= PG_W;
2095         if (va < UPT_MIN_ADDRESS)
2096                 newpte |= PG_U;
2097         if (pmap == kernel_pmap)
2098                 newpte |= pgeflag;
2099
2100         /*
2101          * if the mapping or permission bits are different, we need
2102          * to update the pte.
2103          */
2104         if ((origpte & ~(PG_M|PG_A)) != newpte) {
2105                 *pte = newpte | PG_A;
2106         }
2107         pmap_inval_flush(&info);
2108 }
2109
2110 /*
2111  * this code makes some *MAJOR* assumptions:
2112  * 1. Current pmap & pmap exists.
2113  * 2. Not wired.
2114  * 3. Read access.
2115  * 4. No page table pages.
2116  * 5. Tlbflush is deferred to calling procedure.
2117  * 6. Page IS managed.
2118  * but is *MUCH* faster than pmap_enter...
2119  */
2120
2121 static vm_page_t
2122 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_page_t mpte)
2123 {
2124         unsigned *pte;
2125         vm_paddr_t pa;
2126         pmap_inval_info info;
2127
2128         pmap_inval_init(&info);
2129
2130         /*
2131          * In the case that a page table page is not
2132          * resident, we are creating it here.
2133          */
2134         if (va < UPT_MIN_ADDRESS) {
2135                 unsigned ptepindex;
2136                 vm_offset_t ptepa;
2137
2138                 /*
2139                  * Calculate pagetable page index
2140                  */
2141                 ptepindex = va >> PDRSHIFT;
2142                 if (mpte && (mpte->pindex == ptepindex)) {
2143                         mpte->hold_count++;
2144                 } else {
2145 retry:
2146                         /*
2147                          * Get the page directory entry
2148                          */
2149                         ptepa = (vm_offset_t) pmap->pm_pdir[ptepindex];
2150
2151                         /*
2152                          * If the page table page is mapped, we just increment
2153                          * the hold count, and activate it.
2154                          */
2155                         if (ptepa) {
2156                                 if (ptepa & PG_PS)
2157                                         panic("pmap_enter_quick: unexpected mapping into 4MB page");
2158                                 if (pmap->pm_ptphint &&
2159                                         (pmap->pm_ptphint->pindex == ptepindex)) {
2160                                         mpte = pmap->pm_ptphint;
2161                                 } else {
2162                                         mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
2163                                         pmap->pm_ptphint = mpte;
2164                                 }
2165                                 if (mpte == NULL)
2166                                         goto retry;
2167                                 mpte->hold_count++;
2168                         } else {
2169                                 mpte = _pmap_allocpte(pmap, ptepindex);
2170                         }
2171                 }
2172         } else {
2173                 mpte = NULL;
2174         }
2175
2176         /*
2177          * This call to vtopte makes the assumption that we are
2178          * entering the page into the current pmap.  In order to support
2179          * quick entry into any pmap, one would likely use pmap_pte_quick.
2180          * But that isn't as quick as vtopte.
2181          */
2182         pte = (unsigned *)vtopte(va);
2183         if (*pte) {
2184                 if (mpte)
2185                         pmap_unwire_pte_hold(pmap, mpte, &info);
2186                 return 0;
2187         }
2188
2189         /*
2190          * Enter on the PV list if part of our managed memory. Note that we
2191          * raise IPL while manipulating pv_table since pmap_enter can be
2192          * called at interrupt time.
2193          */
2194         if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0)
2195                 pmap_insert_entry(pmap, va, mpte, m);
2196
2197         /*
2198          * Increment counters
2199          */
2200         pmap->pm_stats.resident_count++;
2201
2202         pa = VM_PAGE_TO_PHYS(m);
2203
2204         /*
2205          * Now validate mapping with RO protection
2206          */
2207         if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2208                 *pte = pa | PG_V | PG_U;
2209         else
2210                 *pte = pa | PG_V | PG_U | PG_MANAGED;
2211
2212         return mpte;
2213 }
2214
2215 /*
2216  * Make a temporary mapping for a physical address.  This is only intended
2217  * to be used for panic dumps.
2218  */
2219 void *
2220 pmap_kenter_temporary(vm_paddr_t pa, int i)
2221 {
2222         pmap_kenter((vm_offset_t)crashdumpmap + (i * PAGE_SIZE), pa);
2223         return ((void *)crashdumpmap);
2224 }
2225
2226 #define MAX_INIT_PT (96)
2227
2228 /*
2229  * This routine preloads the ptes for a given object into the specified pmap.
2230  * This eliminates the blast of soft faults on process startup and
2231  * immediately after an mmap.
2232  */
2233 void
2234 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2235                     vm_object_t object, vm_pindex_t pindex, 
2236                     vm_size_t size, int limit)
2237 {
2238         vm_offset_t tmpidx;
2239         int psize;
2240         vm_page_t p, mpte;
2241         int objpgs;
2242         int s;
2243
2244         if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2245                 return;
2246
2247 #if 0
2248         /* 
2249          * XXX you must be joking, entering PTE's into a user page table
2250          * without any accounting?  This could result in the page table
2251          * being freed while it still contains mappings (free with PG_ZERO
2252          * assumption leading to a non-zero page being marked PG_ZERO).
2253          */
2254         /*
2255          * This code maps large physical mmap regions into the
2256          * processor address space.  Note that some shortcuts
2257          * are taken, but the code works.
2258          */
2259         if (pseflag &&
2260             (object->type == OBJT_DEVICE) &&
2261             ((addr & (NBPDR - 1)) == 0) &&
2262             ((size & (NBPDR - 1)) == 0) ) {
2263                 int i;
2264                 vm_page_t m[1];
2265                 unsigned int ptepindex;
2266                 int npdes;
2267                 vm_offset_t ptepa;
2268
2269                 if (pmap->pm_pdir[ptepindex = (addr >> PDRSHIFT)])
2270                         return;
2271
2272 retry:
2273                 p = vm_page_lookup(object, pindex);
2274                 if (p && vm_page_sleep_busy(p, FALSE, "init4p"))
2275                         goto retry;
2276
2277                 if (p == NULL) {
2278                         p = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL);
2279                         if (p == NULL)
2280                                 return;
2281                         m[0] = p;
2282
2283                         if (vm_pager_get_pages(object, m, 1, 0) != VM_PAGER_OK) {
2284                                 vm_page_free(p);
2285                                 return;
2286                         }
2287
2288                         p = vm_page_lookup(object, pindex);
2289                         vm_page_wakeup(p);
2290                 }
2291
2292                 ptepa = (vm_offset_t) VM_PAGE_TO_PHYS(p);
2293                 if (ptepa & (NBPDR - 1)) {
2294                         return;
2295                 }
2296
2297                 p->valid = VM_PAGE_BITS_ALL;
2298
2299                 pmap->pm_stats.resident_count += size >> PAGE_SHIFT;
2300                 npdes = size >> PDRSHIFT;
2301                 for (i = 0; i < npdes; i++) {
2302                         pmap->pm_pdir[ptepindex] =
2303                             (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_PS);
2304                         ptepa += NBPDR;
2305                         ptepindex += 1;
2306                 }
2307                 vm_page_flag_set(p, PG_MAPPED);
2308                 cpu_invltlb();
2309                 smp_invltlb();
2310                 return;
2311         }
2312 #endif
2313
2314         psize = i386_btop(size);
2315
2316         if ((object->type != OBJT_VNODE) ||
2317                 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2318                         (object->resident_page_count > MAX_INIT_PT))) {
2319                 return;
2320         }
2321
2322         if (psize + pindex > object->size) {
2323                 if (object->size < pindex)
2324                         return;           
2325                 psize = object->size - pindex;
2326         }
2327
2328
2329         /*
2330          * If we are processing a major portion of the object, then scan the
2331          * entire thing.
2332          *
2333          * We cannot safely scan the object's memq unless we are at splvm(),
2334          * since interrupts can remove pages from objects.
2335          */
2336         s = splvm();
2337         mpte = NULL;
2338         if (psize > (object->resident_page_count >> 2)) {
2339                 objpgs = psize;
2340
2341                 for (p = TAILQ_FIRST(&object->memq);
2342                     objpgs > 0 && p != NULL;
2343                     p = TAILQ_NEXT(p, listq)
2344                 ) {
2345                         tmpidx = p->pindex;
2346                         if (tmpidx < pindex)
2347                                 continue;
2348                         tmpidx -= pindex;
2349                         if (tmpidx >= psize)
2350                                 continue;
2351
2352                         /*
2353                          * don't allow an madvise to blow away our really
2354                          * free pages allocating pv entries.
2355                          */
2356                         if ((limit & MAP_PREFAULT_MADVISE) &&
2357                             vmstats.v_free_count < vmstats.v_free_reserved) {
2358                                 break;
2359                         }
2360                         if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2361                                 (p->busy == 0) &&
2362                             (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2363                                 if ((p->queue - p->pc) == PQ_CACHE)
2364                                         vm_page_deactivate(p);
2365                                 vm_page_busy(p);
2366                                 mpte = pmap_enter_quick(pmap, 
2367                                         addr + i386_ptob(tmpidx), p, mpte);
2368                                 vm_page_flag_set(p, PG_MAPPED);
2369                                 vm_page_wakeup(p);
2370                         }
2371                         objpgs -= 1;
2372                 }
2373         } else {
2374                 /*
2375                  * else lookup the pages one-by-one.
2376                  */
2377                 for (tmpidx = 0; tmpidx < psize; tmpidx += 1) {
2378                         /*
2379                          * don't allow an madvise to blow away our really
2380                          * free pages allocating pv entries.
2381                          */
2382                         if ((limit & MAP_PREFAULT_MADVISE) &&
2383                             vmstats.v_free_count < vmstats.v_free_reserved) {
2384                                 break;
2385                         }
2386                         p = vm_page_lookup(object, tmpidx + pindex);
2387                         if (p &&
2388                             ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2389                                 (p->busy == 0) &&
2390                             (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2391                                 if ((p->queue - p->pc) == PQ_CACHE)
2392                                         vm_page_deactivate(p);
2393                                 vm_page_busy(p);
2394                                 mpte = pmap_enter_quick(pmap, 
2395                                         addr + i386_ptob(tmpidx), p, mpte);
2396                                 vm_page_flag_set(p, PG_MAPPED);
2397                                 vm_page_wakeup(p);
2398                         }
2399                 }
2400         }
2401         splx(s);
2402 }
2403
2404 /*
2405  * pmap_prefault provides a quick way of clustering pagefaults into a
2406  * processes address space.  It is a "cousin" of pmap_object_init_pt, 
2407  * except it runs at page fault time instead of mmap time.
2408  */
2409 #define PFBAK 4
2410 #define PFFOR 4
2411 #define PAGEORDER_SIZE (PFBAK+PFFOR)
2412
2413 static int pmap_prefault_pageorder[] = {
2414         -PAGE_SIZE, PAGE_SIZE,
2415         -2 * PAGE_SIZE, 2 * PAGE_SIZE,
2416         -3 * PAGE_SIZE, 3 * PAGE_SIZE,
2417         -4 * PAGE_SIZE, 4 * PAGE_SIZE
2418 };
2419
2420 void
2421 pmap_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry)
2422 {
2423         int i;
2424         int s;
2425         vm_offset_t starta;
2426         vm_offset_t addr;
2427         vm_pindex_t pindex;
2428         vm_page_t m, mpte;
2429         vm_object_t object;
2430
2431         if (!curproc || (pmap != vmspace_pmap(curproc->p_vmspace)))
2432                 return;
2433
2434         object = entry->object.vm_object;
2435
2436         starta = addra - PFBAK * PAGE_SIZE;
2437         if (starta < entry->start)
2438                 starta = entry->start;
2439         else if (starta > addra)
2440                 starta = 0;
2441
2442         /*
2443          * splvm() protection is required to maintain the page/object 
2444          * association, interrupts can free pages and remove them from
2445          * their objects.
2446          */
2447         mpte = NULL;
2448         s = splvm();
2449         for (i = 0; i < PAGEORDER_SIZE; i++) {
2450                 vm_object_t lobject;
2451                 unsigned *pte;
2452
2453                 addr = addra + pmap_prefault_pageorder[i];
2454                 if (addr > addra + (PFFOR * PAGE_SIZE))
2455                         addr = 0;
2456
2457                 if (addr < starta || addr >= entry->end)
2458                         continue;
2459
2460                 if ((*pmap_pde(pmap, addr)) == NULL) 
2461                         continue;
2462
2463                 pte = (unsigned *) vtopte(addr);
2464                 if (*pte)
2465                         continue;
2466
2467                 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2468                 lobject = object;
2469
2470                 for (m = vm_page_lookup(lobject, pindex);
2471                     (!m && (lobject->type == OBJT_DEFAULT) &&
2472                      (lobject->backing_object));
2473                     lobject = lobject->backing_object
2474                 ) {
2475                         if (lobject->backing_object_offset & PAGE_MASK)
2476                                 break;
2477                         pindex += (lobject->backing_object_offset >> PAGE_SHIFT);
2478                         m = vm_page_lookup(lobject->backing_object, pindex);
2479                 }
2480
2481                 /*
2482                  * give-up when a page is not in memory
2483                  */
2484                 if (m == NULL)
2485                         break;
2486
2487                 if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2488                         (m->busy == 0) &&
2489                     (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2490
2491                         if ((m->queue - m->pc) == PQ_CACHE) {
2492                                 vm_page_deactivate(m);
2493                         }
2494                         vm_page_busy(m);
2495                         mpte = pmap_enter_quick(pmap, addr, m, mpte);
2496                         vm_page_flag_set(m, PG_MAPPED);
2497                         vm_page_wakeup(m);
2498                 }
2499         }
2500         splx(s);
2501 }
2502
2503 /*
2504  *      Routine:        pmap_change_wiring
2505  *      Function:       Change the wiring attribute for a map/virtual-address
2506  *                      pair.
2507  *      In/out conditions:
2508  *                      The mapping must already exist in the pmap.
2509  */
2510 void
2511 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
2512 {
2513         unsigned *pte;
2514
2515         if (pmap == NULL)
2516                 return;
2517
2518         pte = pmap_pte(pmap, va);
2519
2520         if (wired && !pmap_pte_w(pte))
2521                 pmap->pm_stats.wired_count++;
2522         else if (!wired && pmap_pte_w(pte))
2523                 pmap->pm_stats.wired_count--;
2524
2525         /*
2526          * Wiring is not a hardware characteristic so there is no need to
2527          * invalidate TLB.  However, in an SMP environment we must use
2528          * a locked bus cycle to update the pte (if we are not using 
2529          * the pmap_inval_*() API that is)... it's ok to do this for simple
2530          * wiring changes.
2531          */
2532 #ifdef SMP
2533         if (wired)
2534                 atomic_set_int(pte, PG_W);
2535         else
2536                 atomic_clear_int(pte, PG_W);
2537 #else
2538         if (wired)
2539                 atomic_set_int_nonlocked(pte, PG_W);
2540         else
2541                 atomic_clear_int_nonlocked(pte, PG_W);
2542 #endif
2543 }
2544
2545
2546
2547 /*
2548  *      Copy the range specified by src_addr/len
2549  *      from the source map to the range dst_addr/len
2550  *      in the destination map.
2551  *
2552  *      This routine is only advisory and need not do anything.
2553  */
2554 void
2555 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, 
2556         vm_size_t len, vm_offset_t src_addr)
2557 {
2558         pmap_inval_info info;
2559         vm_offset_t addr;
2560         vm_offset_t end_addr = src_addr + len;
2561         vm_offset_t pdnxt;
2562         unsigned src_frame, dst_frame;
2563         vm_page_t m;
2564         int s;
2565
2566         if (dst_addr != src_addr)
2567                 return;
2568
2569         src_frame = ((unsigned) src_pmap->pm_pdir[PTDPTDI]) & PG_FRAME;
2570         if (src_frame != (((unsigned) PTDpde) & PG_FRAME)) {
2571                 return;
2572         }
2573
2574         dst_frame = ((unsigned) dst_pmap->pm_pdir[PTDPTDI]) & PG_FRAME;
2575         if (dst_frame != (((unsigned) APTDpde) & PG_FRAME)) {
2576                 APTDpde = (pd_entry_t) (dst_frame | PG_RW | PG_V);
2577                 /* The page directory is not shared between CPUs */
2578                 cpu_invltlb();
2579         }
2580         pmap_inval_init(&info);
2581         pmap_inval_add(&info, dst_pmap, -1);
2582         pmap_inval_add(&info, src_pmap, -1);
2583
2584         /*
2585          * splvm() protection is required to maintain the page/object
2586          * association, interrupts can free pages and remove them from 
2587          * their objects.
2588          */
2589         s = splvm();
2590         for (addr = src_addr; addr < end_addr; addr = pdnxt) {
2591                 unsigned *src_pte, *dst_pte;
2592                 vm_page_t dstmpte, srcmpte;
2593                 vm_offset_t srcptepaddr;
2594                 unsigned ptepindex;
2595
2596                 if (addr >= UPT_MIN_ADDRESS)
2597                         panic("pmap_copy: invalid to pmap_copy page tables\n");
2598
2599                 /*
2600                  * Don't let optional prefaulting of pages make us go
2601                  * way below the low water mark of free pages or way
2602                  * above high water mark of used pv entries.
2603                  */
2604                 if (vmstats.v_free_count < vmstats.v_free_reserved ||
2605                     pv_entry_count > pv_entry_high_water)
2606                         break;
2607                 
2608                 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
2609                 ptepindex = addr >> PDRSHIFT;
2610
2611                 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex];
2612                 if (srcptepaddr == 0)
2613                         continue;
2614                         
2615                 if (srcptepaddr & PG_PS) {
2616                         if (dst_pmap->pm_pdir[ptepindex] == 0) {
2617                                 dst_pmap->pm_pdir[ptepindex] = (pd_entry_t) srcptepaddr;
2618                                 dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
2619                         }
2620                         continue;
2621                 }
2622
2623                 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
2624                 if ((srcmpte == NULL) ||
2625                         (srcmpte->hold_count == 0) || (srcmpte->flags & PG_BUSY))
2626                         continue;
2627
2628                 if (pdnxt > end_addr)
2629                         pdnxt = end_addr;
2630
2631                 src_pte = (unsigned *) vtopte(addr);
2632                 dst_pte = (unsigned *) avtopte(addr);
2633                 while (addr < pdnxt) {
2634                         unsigned ptetemp;
2635                         ptetemp = *src_pte;
2636                         /*
2637                          * we only virtual copy managed pages
2638                          */
2639                         if ((ptetemp & PG_MANAGED) != 0) {
2640                                 /*
2641                                  * We have to check after allocpte for the
2642                                  * pte still being around...  allocpte can
2643                                  * block.
2644                                  */
2645                                 dstmpte = pmap_allocpte(dst_pmap, addr);
2646                                 if ((*dst_pte == 0) && (ptetemp = *src_pte)) {
2647                                         /*
2648                                          * Clear the modified and
2649                                          * accessed (referenced) bits
2650                                          * during the copy.
2651                                          */
2652                                         m = PHYS_TO_VM_PAGE(ptetemp);
2653                                         *dst_pte = ptetemp & ~(PG_M | PG_A);
2654                                         dst_pmap->pm_stats.resident_count++;
2655                                         pmap_insert_entry(dst_pmap, addr,
2656                                                 dstmpte, m);
2657                                 } else {
2658                                         pmap_unwire_pte_hold(dst_pmap, dstmpte, &info);
2659                                 }
2660                                 if (dstmpte->hold_count >= srcmpte->hold_count)
2661                                         break;
2662                         }
2663                         addr += PAGE_SIZE;
2664                         src_pte++;
2665                         dst_pte++;
2666                 }
2667         }
2668         splx(s);
2669         pmap_inval_flush(&info);
2670 }       
2671
2672 /*
2673  *      Routine:        pmap_kernel
2674  *      Function:
2675  *              Returns the physical map handle for the kernel.
2676  */
2677 pmap_t
2678 pmap_kernel(void)
2679 {
2680         return (kernel_pmap);
2681 }
2682
2683 /*
2684  * pmap_zero_page:
2685  *
2686  *      Zero the specified PA by mapping the page into KVM and clearing its
2687  *      contents.
2688  *
2689  *      This function may be called from an interrupt and no locking is
2690  *      required.
2691  */
2692 void
2693 pmap_zero_page(vm_paddr_t phys)
2694 {
2695         struct mdglobaldata *gd = mdcpu;
2696
2697         crit_enter();
2698         if (*(int *)gd->gd_CMAP3)
2699                 panic("pmap_zero_page: CMAP3 busy");
2700         *(int *)gd->gd_CMAP3 =
2701                     PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2702         cpu_invlpg(gd->gd_CADDR3);
2703
2704 #if defined(I686_CPU)
2705         if (cpu_class == CPUCLASS_686)
2706                 i686_pagezero(gd->gd_CADDR3);
2707         else
2708 #endif
2709                 bzero(gd->gd_CADDR3, PAGE_SIZE);
2710         *(int *) gd->gd_CMAP3 = 0;
2711         crit_exit();
2712 }
2713
2714 /*
2715  * pmap_page_assertzero:
2716  *
2717  *      Assert that a page is empty, panic if it isn't.
2718  */
2719 void
2720 pmap_page_assertzero(vm_paddr_t phys)
2721 {
2722         struct mdglobaldata *gd = mdcpu;
2723         int i;
2724
2725         crit_enter();
2726         if (*(int *)gd->gd_CMAP3)
2727                 panic("pmap_zero_page: CMAP3 busy");
2728         *(int *)gd->gd_CMAP3 =
2729                     PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2730         cpu_invlpg(gd->gd_CADDR3);
2731         for (i = 0; i < PAGE_SIZE; i += 4) {
2732             if (*(int *)((char *)gd->gd_CADDR3 + i) != 0) {
2733                 panic("pmap_page_assertzero() @ %p not zero!\n",
2734                     (void *)gd->gd_CADDR3);
2735             }
2736         }
2737         *(int *) gd->gd_CMAP3 = 0;
2738         crit_exit();
2739 }
2740
2741 /*
2742  * pmap_zero_page:
2743  *
2744  *      Zero part of a physical page by mapping it into memory and clearing
2745  *      its contents with bzero.
2746  *
2747  *      off and size may not cover an area beyond a single hardware page.
2748  */
2749 void
2750 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
2751 {
2752         struct mdglobaldata *gd = mdcpu;
2753
2754         crit_enter();
2755         if (*(int *) gd->gd_CMAP3)
2756                 panic("pmap_zero_page: CMAP3 busy");
2757         *(int *) gd->gd_CMAP3 = PG_V | PG_RW | (phys & PG_FRAME) | PG_A | PG_M;
2758         cpu_invlpg(gd->gd_CADDR3);
2759
2760 #if defined(I686_CPU)
2761         if (cpu_class == CPUCLASS_686 && off == 0 && size == PAGE_SIZE)
2762                 i686_pagezero(gd->gd_CADDR3);
2763         else
2764 #endif
2765                 bzero((char *)gd->gd_CADDR3 + off, size);
2766         *(int *) gd->gd_CMAP3 = 0;
2767         crit_exit();
2768 }
2769
2770 /*
2771  * pmap_copy_page:
2772  *
2773  *      Copy the physical page from the source PA to the target PA.
2774  *      This function may be called from an interrupt.  No locking
2775  *      is required.
2776  */
2777 void
2778 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
2779 {
2780         struct mdglobaldata *gd = mdcpu;
2781
2782         crit_enter();
2783         if (*(int *) gd->gd_CMAP1)
2784                 panic("pmap_copy_page: CMAP1 busy");
2785         if (*(int *) gd->gd_CMAP2)
2786                 panic("pmap_copy_page: CMAP2 busy");
2787
2788         *(int *) gd->gd_CMAP1 = PG_V | (src & PG_FRAME) | PG_A;
2789         *(int *) gd->gd_CMAP2 = PG_V | PG_RW | (dst & PG_FRAME) | PG_A | PG_M;
2790
2791         cpu_invlpg(gd->gd_CADDR1);
2792         cpu_invlpg(gd->gd_CADDR2);
2793
2794         bcopy(gd->gd_CADDR1, gd->gd_CADDR2, PAGE_SIZE);
2795
2796         *(int *) gd->gd_CMAP1 = 0;
2797         *(int *) gd->gd_CMAP2 = 0;
2798         crit_exit();
2799 }
2800
2801 /*
2802  * pmap_copy_page_frag:
2803  *
2804  *      Copy the physical page from the source PA to the target PA.
2805  *      This function may be called from an interrupt.  No locking
2806  *      is required.
2807  */
2808 void
2809 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
2810 {
2811         struct mdglobaldata *gd = mdcpu;
2812
2813         crit_enter();
2814         if (*(int *) gd->gd_CMAP1)
2815                 panic("pmap_copy_page: CMAP1 busy");
2816         if (*(int *) gd->gd_CMAP2)
2817                 panic("pmap_copy_page: CMAP2 busy");
2818
2819         *(int *) gd->gd_CMAP1 = PG_V | (src & PG_FRAME) | PG_A;
2820         *(int *) gd->gd_CMAP2 = PG_V | PG_RW | (dst & PG_FRAME) | PG_A | PG_M;
2821
2822         cpu_invlpg(gd->gd_CADDR1);
2823         cpu_invlpg(gd->gd_CADDR2);
2824
2825         bcopy((char *)gd->gd_CADDR1 + (src & PAGE_MASK),
2826               (char *)gd->gd_CADDR2 + (dst & PAGE_MASK),
2827               bytes);
2828
2829         *(int *) gd->gd_CMAP1 = 0;
2830         *(int *) gd->gd_CMAP2 = 0;
2831         crit_exit();
2832 }
2833
2834 /*
2835  * Returns true if the pmap's pv is one of the first
2836  * 16 pvs linked to from this page.  This count may
2837  * be changed upwards or downwards in the future; it
2838  * is only necessary that true be returned for a small
2839  * subset of pmaps for proper page aging.
2840  */
2841 boolean_t
2842 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
2843 {
2844         pv_entry_t pv;
2845         int loops = 0;
2846         int s;
2847
2848         if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2849                 return FALSE;
2850
2851         s = splvm();
2852
2853         TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2854                 if (pv->pv_pmap == pmap) {
2855                         splx(s);
2856                         return TRUE;
2857                 }
2858                 loops++;
2859                 if (loops >= 16)
2860                         break;
2861         }
2862         splx(s);
2863         return (FALSE);
2864 }
2865
2866 #define PMAP_REMOVE_PAGES_CURPROC_ONLY
2867 /*
2868  * Remove all pages from specified address space
2869  * this aids process exit speeds.  Also, this code
2870  * is special cased for current process only, but
2871  * can have the more generic (and slightly slower)
2872  * mode enabled.  This is much faster than pmap_remove
2873  * in the case of running down an entire address space.
2874  */
2875 void
2876 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
2877 {
2878         unsigned *pte, tpte;
2879         pv_entry_t pv, npv;
2880         int s;
2881         vm_page_t m;
2882         pmap_inval_info info;
2883
2884 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2885         if (!curproc || (pmap != vmspace_pmap(curproc->p_vmspace))) {
2886                 printf("warning: pmap_remove_pages called with non-current pmap\n");
2887                 return;
2888         }
2889 #endif
2890
2891         pmap_inval_init(&info);
2892         s = splvm();
2893         for(pv = TAILQ_FIRST(&pmap->pm_pvlist);
2894                 pv;
2895                 pv = npv) {
2896
2897                 if (pv->pv_va >= eva || pv->pv_va < sva) {
2898                         npv = TAILQ_NEXT(pv, pv_plist);
2899                         continue;
2900                 }
2901
2902 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
2903                 pte = (unsigned *)vtopte(pv->pv_va);
2904 #else
2905                 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2906 #endif
2907                 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
2908                 tpte = *pte;
2909
2910 /*
2911  * We cannot remove wired pages from a process' mapping at this time
2912  */
2913                 if (tpte & PG_W) {
2914                         npv = TAILQ_NEXT(pv, pv_plist);
2915                         continue;
2916                 }
2917                 *pte = 0;
2918
2919                 m = PHYS_TO_VM_PAGE(tpte);
2920
2921                 KASSERT(m < &vm_page_array[vm_page_array_size],
2922                         ("pmap_remove_pages: bad tpte %x", tpte));
2923
2924                 pv->pv_pmap->pm_stats.resident_count--;
2925
2926                 /*
2927                  * Update the vm_page_t clean and reference bits.
2928                  */
2929                 if (tpte & PG_M) {
2930                         vm_page_dirty(m);
2931                 }
2932
2933
2934                 npv = TAILQ_NEXT(pv, pv_plist);
2935                 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2936
2937                 m->md.pv_list_count--;
2938                 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2939                 if (TAILQ_FIRST(&m->md.pv_list) == NULL) {
2940                         vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2941                 }
2942
2943                 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem, &info);
2944                 free_pv_entry(pv);
2945         }
2946         pmap_inval_flush(&info);
2947         splx(s);
2948 }
2949
2950 /*
2951  * pmap_testbit tests bits in pte's
2952  * note that the testbit/changebit routines are inline,
2953  * and a lot of things compile-time evaluate.
2954  */
2955 static boolean_t
2956 pmap_testbit(vm_page_t m, int bit)
2957 {
2958         pv_entry_t pv;
2959         unsigned *pte;
2960         int s;
2961
2962         if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2963                 return FALSE;
2964
2965         if (TAILQ_FIRST(&m->md.pv_list) == NULL)
2966                 return FALSE;
2967
2968         s = splvm();
2969
2970         TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2971                 /*
2972                  * if the bit being tested is the modified bit, then
2973                  * mark clean_map and ptes as never
2974                  * modified.
2975                  */
2976                 if (bit & (PG_A|PG_M)) {
2977                         if (!pmap_track_modified(pv->pv_va))
2978                                 continue;
2979                 }
2980
2981 #if defined(PMAP_DIAGNOSTIC)
2982                 if (!pv->pv_pmap) {
2983                         printf("Null pmap (tb) at va: 0x%x\n", pv->pv_va);
2984                         continue;
2985                 }
2986 #endif
2987                 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2988                 if (*pte & bit) {
2989                         splx(s);
2990                         return TRUE;
2991                 }
2992         }
2993         splx(s);
2994         return (FALSE);
2995 }
2996
2997 /*
2998  * this routine is used to modify bits in ptes
2999  */
3000 static __inline void
3001 pmap_changebit(vm_page_t m, int bit, boolean_t setem)
3002 {
3003         struct pmap_inval_info info;
3004         pv_entry_t pv;
3005         unsigned *pte;
3006         int s;
3007
3008         if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3009                 return;
3010
3011         pmap_inval_init(&info);
3012         s = splvm();
3013
3014         /*
3015          * Loop over all current mappings setting/clearing as appropos If
3016          * setting RO do we need to clear the VAC?
3017          */
3018         TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3019                 /*
3020                  * don't write protect pager mappings
3021                  */
3022                 if (!setem && (bit == PG_RW)) {
3023                         if (!pmap_track_modified(pv->pv_va))
3024                                 continue;
3025                 }
3026
3027 #if defined(PMAP_DIAGNOSTIC)
3028                 if (!pv->pv_pmap) {
3029                         printf("Null pmap (cb) at va: 0x%x\n", pv->pv_va);
3030                         continue;
3031                 }
3032 #endif
3033
3034                 /*
3035                  * Careful here.  We can use a locked bus instruction to
3036                  * clear PG_A or PG_M safely but we need to synchronize
3037                  * with the target cpus when we mess with PG_RW.
3038                  */
3039                 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3040                 if (bit == PG_RW)
3041                         pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
3042
3043                 if (setem) {
3044 #ifdef SMP
3045                         atomic_set_int(pte, bit);
3046 #else
3047                         atomic_set_int_nonlocked(pte, bit);
3048 #endif
3049                 } else {
3050                         vm_offset_t pbits = *(vm_offset_t *)pte;
3051                         if (pbits & bit) {
3052                                 if (bit == PG_RW) {
3053                                         if (pbits & PG_M) {
3054                                                 vm_page_dirty(m);
3055                                         }
3056 #ifdef SMP
3057                                         atomic_clear_int(pte, PG_M|PG_RW);
3058 #else
3059                                         atomic_clear_int_nonlocked(pte, PG_M|PG_RW);
3060 #endif
3061                                 } else {
3062 #ifdef SMP
3063                                         atomic_clear_int(pte, bit);
3064 #else
3065                                         atomic_clear_int_nonlocked(pte, bit);
3066 #endif
3067                                 }
3068                         }
3069                 }
3070         }
3071         pmap_inval_flush(&info);
3072         splx(s);
3073 }
3074
3075 /*
3076  *      pmap_page_protect:
3077  *
3078  *      Lower the permission for all mappings to a given page.
3079  */
3080 void
3081 pmap_page_protect(vm_page_t m, vm_prot_t prot)
3082 {
3083         if ((prot & VM_PROT_WRITE) == 0) {
3084                 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
3085                         pmap_changebit(m, PG_RW, FALSE);
3086                 } else {
3087                         pmap_remove_all(m);
3088                 }
3089         }
3090 }
3091
3092 vm_paddr_t
3093 pmap_phys_address(int ppn)
3094 {
3095         return (i386_ptob(ppn));
3096 }
3097
3098 /*
3099  *      pmap_ts_referenced:
3100  *
3101  *      Return a count of reference bits for a page, clearing those bits.
3102  *      It is not necessary for every reference bit to be cleared, but it
3103  *      is necessary that 0 only be returned when there are truly no
3104  *      reference bits set.
3105  *
3106  *      XXX: The exact number of bits to check and clear is a matter that
3107  *      should be tested and standardized at some point in the future for
3108  *      optimal aging of shared pages.
3109  */
3110 int
3111 pmap_ts_referenced(vm_page_t m)
3112 {
3113         pv_entry_t pv, pvf, pvn;
3114         unsigned *pte;
3115         int s;
3116         int rtval = 0;
3117
3118         if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3119                 return (rtval);
3120
3121         s = splvm();
3122
3123         if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3124
3125                 pvf = pv;
3126
3127                 do {
3128                         pvn = TAILQ_NEXT(pv, pv_list);
3129
3130                         TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3131
3132                         TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3133
3134                         if (!pmap_track_modified(pv->pv_va))
3135                                 continue;
3136
3137                         pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3138
3139                         if (pte && (*pte & PG_A)) {
3140 #ifdef SMP
3141                                 atomic_clear_int(pte, PG_A);
3142 #else
3143                                 atomic_clear_int_nonlocked(pte, PG_A);
3144 #endif
3145                                 rtval++;
3146                                 if (rtval > 4) {
3147                                         break;
3148                                 }
3149                         }
3150                 } while ((pv = pvn) != NULL && pv != pvf);
3151         }
3152         splx(s);
3153
3154         return (rtval);
3155 }
3156
3157 /*
3158  *      pmap_is_modified:
3159  *
3160  *      Return whether or not the specified physical page was modified
3161  *      in any physical maps.
3162  */
3163 boolean_t
3164 pmap_is_modified(vm_page_t m)
3165 {
3166         return pmap_testbit(m, PG_M);
3167 }
3168
3169 /*
3170  *      Clear the modify bits on the specified physical page.
3171  */
3172 void
3173 pmap_clear_modify(vm_page_t m)
3174 {
3175         pmap_changebit(m, PG_M, FALSE);
3176 }
3177
3178 /*
3179  *      pmap_clear_reference:
3180  *
3181  *      Clear the reference bit on the specified physical page.
3182  */
3183 void
3184 pmap_clear_reference(vm_page_t m)
3185 {
3186         pmap_changebit(m, PG_A, FALSE);
3187 }
3188
3189 /*
3190  * Miscellaneous support routines follow
3191  */
3192
3193 static void
3194 i386_protection_init(void)
3195 {
3196         int *kp, prot;
3197
3198         kp = protection_codes;
3199         for (prot = 0; prot < 8; prot++) {
3200                 switch (prot) {
3201                 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE:
3202                         /*
3203                          * Read access is also 0. There isn't any execute bit,
3204                          * so just make it readable.
3205                          */
3206                 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE:
3207                 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE:
3208                 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE:
3209                         *kp++ = 0;
3210                         break;
3211                 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE:
3212                 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE:
3213                 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE:
3214                 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
3215                         *kp++ = PG_RW;
3216                         break;
3217                 }
3218         }
3219 }
3220
3221 /*
3222  * Map a set of physical memory pages into the kernel virtual
3223  * address space. Return a pointer to where it is mapped. This
3224  * routine is intended to be used for mapping device memory,
3225  * NOT real memory.
3226  *
3227  * NOTE: we can't use pgeflag unless we invalidate the pages one at
3228  * a time.
3229  */
3230 void *
3231 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
3232 {
3233         vm_offset_t va, tmpva, offset;
3234         unsigned *pte;
3235
3236         offset = pa & PAGE_MASK;
3237         size = roundup(offset + size, PAGE_SIZE);
3238
3239         va = kmem_alloc_nofault(kernel_map, size);
3240         if (!va)
3241                 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3242
3243         pa = pa & PG_FRAME;
3244         for (tmpva = va; size > 0;) {
3245                 pte = (unsigned *)vtopte(tmpva);
3246                 *pte = pa | PG_RW | PG_V; /* | pgeflag; */
3247                 size -= PAGE_SIZE;
3248                 tmpva += PAGE_SIZE;
3249                 pa += PAGE_SIZE;
3250         }
3251         cpu_invltlb();
3252         smp_invltlb();
3253
3254         return ((void *)(va + offset));
3255 }
3256
3257 void
3258 pmap_unmapdev(vm_offset_t va, vm_size_t size)
3259 {
3260         vm_offset_t base, offset;
3261
3262         base = va & PG_FRAME;
3263         offset = va & PAGE_MASK;
3264         size = roundup(offset + size, PAGE_SIZE);
3265         pmap_qremove(va, size >> PAGE_SHIFT);
3266         kmem_free(kernel_map, base, size);
3267 }
3268
3269 /*
3270  * perform the pmap work for mincore
3271  */
3272 int
3273 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3274 {
3275         unsigned *ptep, pte;
3276         vm_page_t m;
3277         int val = 0;
3278         
3279         ptep = pmap_pte(pmap, addr);
3280         if (ptep == 0) {
3281                 return 0;
3282         }
3283
3284         if ((pte = *ptep) != 0) {
3285                 vm_offset_t pa;
3286
3287                 val = MINCORE_INCORE;
3288                 if ((pte & PG_MANAGED) == 0)
3289                         return val;
3290
3291                 pa = pte & PG_FRAME;
3292
3293                 m = PHYS_TO_VM_PAGE(pa);
3294
3295                 /*
3296                  * Modified by us
3297                  */
3298                 if (pte & PG_M)
3299                         val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3300                 /*
3301                  * Modified by someone
3302                  */
3303                 else if (m->dirty || pmap_is_modified(m))
3304                         val |= MINCORE_MODIFIED_OTHER;
3305                 /*
3306                  * Referenced by us
3307                  */
3308                 if (pte & PG_A)
3309                         val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3310
3311                 /*
3312                  * Referenced by someone
3313                  */
3314                 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3315                         val |= MINCORE_REFERENCED_OTHER;
3316                         vm_page_flag_set(m, PG_REFERENCED);
3317                 }
3318         } 
3319         return val;
3320 }
3321
3322 void
3323 pmap_activate(struct proc *p)
3324 {
3325         pmap_t  pmap;
3326
3327         pmap = vmspace_pmap(p->p_vmspace);
3328 #if defined(SMP)
3329         atomic_set_int(&pmap->pm_active, 1 << mycpu->gd_cpuid);
3330 #else
3331         pmap->pm_active |= 1;
3332 #endif
3333 #if defined(SWTCH_OPTIM_STATS)
3334         tlb_flush_count++;
3335 #endif
3336         p->p_thread->td_pcb->pcb_cr3 = vtophys(pmap->pm_pdir);
3337         load_cr3(p->p_thread->td_pcb->pcb_cr3);
3338 }
3339
3340 vm_offset_t
3341 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3342 {
3343
3344         if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3345                 return addr;
3346         }
3347
3348         addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3349         return addr;
3350 }
3351
3352
3353 #if defined(PMAP_DEBUG)
3354 int
3355 pmap_pid_dump(int pid)
3356 {
3357         pmap_t pmap;
3358         struct proc *p;
3359         int npte = 0;
3360         int index;
3361         FOREACH_PROC_IN_SYSTEM(p) {
3362                 if (p->p_pid != pid)
3363                         continue;
3364
3365                 if (p->p_vmspace) {
3366                         int i,j;
3367                         index = 0;
3368                         pmap = vmspace_pmap(p->p_vmspace);
3369                         for(i=0;i<1024;i++) {
3370                                 pd_entry_t *pde;
3371                                 unsigned *pte;
3372                                 unsigned base = i << PDRSHIFT;
3373                                 
3374                                 pde = &pmap->pm_pdir[i];
3375                                 if (pde && pmap_pde_v(pde)) {
3376                                         for(j=0;j<1024;j++) {
3377                                                 unsigned va = base + (j << PAGE_SHIFT);
3378                                                 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) {
3379                                                         if (index) {
3380                                                                 index = 0;
3381                                                                 printf("\n");
3382                                                         }
3383                                                         return npte;
3384                                                 }
3385                                                 pte = pmap_pte_quick( pmap, va);
3386                                                 if (pte && pmap_pte_v(pte)) {
3387                                                         vm_offset_t pa;
3388                                                         vm_page_t m;
3389                                                         pa = *(int *)pte;
3390                                                         m = PHYS_TO_VM_PAGE(pa);
3391                                                         printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x",
3392                                                                 va, pa, m->hold_count, m->wire_count, m->flags);
3393                                                         npte++;
3394                                                         index++;
3395                                                         if (index >= 2) {
3396                                                                 index = 0;
3397                                                                 printf("\n");
3398                                                         } else {
3399                                                                 printf(" ");
3400                                                         }
3401                                                 }
3402                                         }
3403                                 }
3404                         }
3405                 }
3406         }
3407         return npte;
3408 }
3409 #endif
3410
3411 #if defined(DEBUG)
3412
3413 static void     pads (pmap_t pm);
3414 void            pmap_pvdump (vm_paddr_t pa);
3415
3416 /* print address space of pmap*/
3417 static void
3418 pads(pmap_t pm)
3419 {
3420         unsigned va, i, j;
3421         unsigned *ptep;
3422
3423         if (pm == kernel_pmap)
3424                 return;
3425         for (i = 0; i < 1024; i++)
3426                 if (pm->pm_pdir[i])
3427                         for (j = 0; j < 1024; j++) {
3428                                 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
3429                                 if (pm == kernel_pmap && va < KERNBASE)
3430                                         continue;
3431                                 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS)
3432                                         continue;
3433                                 ptep = pmap_pte_quick(pm, va);
3434                                 if (pmap_pte_v(ptep))
3435                                         printf("%x:%x ", va, *(int *) ptep);
3436                         };
3437
3438 }
3439
3440 void
3441 pmap_pvdump(vm_paddr_t pa)
3442 {
3443         pv_entry_t pv;
3444         vm_page_t m;
3445
3446         printf("pa %08llx", (long long)pa);
3447         m = PHYS_TO_VM_PAGE(pa);
3448         TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3449 #ifdef used_to_be
3450                 printf(" -> pmap %p, va %x, flags %x",
3451                     (void *)pv->pv_pmap, pv->pv_va, pv->pv_flags);
3452 #endif
3453                 printf(" -> pmap %p, va %x", (void *)pv->pv_pmap, pv->pv_va);
3454                 pads(pv->pv_pmap);
3455         }
3456         printf(" ");
3457 }
3458 #endif