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