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