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