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