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