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