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