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