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