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