6b3cdcc975c9ba6c3c95c7d434773c4066188440
[dragonfly.git] / sys / platform / pc64 / amd64 / pmap.c
1 /*
2  * Copyright (c) 1991 Regents of the University of California.
3  * Copyright (c) 1994 John S. Dyson
4  * Copyright (c) 1994 David Greenman
5  * Copyright (c) 2003 Peter Wemm
6  * Copyright (c) 2005-2008 Alan L. Cox <alc@cs.rice.edu>
7  * Copyright (c) 2008, 2009 The DragonFly Project.
8  * Copyright (c) 2008, 2009 Jordan Gordeev.
9  * All rights reserved.
10  *
11  * This code is derived from software contributed to Berkeley by
12  * the Systems Programming Group of the University of Utah Computer
13  * Science Department and William Jolitz of UUNET Technologies Inc.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  * 3. All advertising materials mentioning features or use of this software
24  *    must display the following acknowledgement:
25  *      This product includes software developed by the University of
26  *      California, Berkeley and its contributors.
27  * 4. Neither the name of the University nor the names of its contributors
28  *    may be used to endorse or promote products derived from this software
29  *    without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41  * SUCH DAMAGE.
42  *
43  *      from:   @(#)pmap.c      7.7 (Berkeley)  5/12/91
44  * $FreeBSD: src/sys/i386/i386/pmap.c,v 1.250.2.18 2002/03/06 22:48:53 silby Exp $
45  * $DragonFly: src/sys/platform/pc64/amd64/pmap.c,v 1.3 2008/08/29 17:07:10 dillon Exp $
46  */
47
48 /*
49  *      Manages physical address maps.
50  *
51  *      In addition to hardware address maps, this
52  *      module is called upon to provide software-use-only
53  *      maps which may or may not be stored in the same
54  *      form as hardware maps.  These pseudo-maps are
55  *      used to store intermediate results from copy
56  *      operations to and from address spaces.
57  *
58  *      Since the information managed by this module is
59  *      also stored by the logical address mapping module,
60  *      this module may throw away valid virtual-to-physical
61  *      mappings at almost any time.  However, invalidations
62  *      of virtual-to-physical mappings must be done as
63  *      requested.
64  *
65  *      In order to cope with hardware architectures which
66  *      make virtual-to-physical map invalidates expensive,
67  *      this module may delay invalidate or reduced protection
68  *      operations until such time as they are actually
69  *      necessary.  This module is given full information as
70  *      to which processors are currently using which maps,
71  *      and to when physical maps must be made correct.
72  */
73
74 #if JG
75 #include "opt_disable_pse.h"
76 #include "opt_pmap.h"
77 #endif
78 #include "opt_msgbuf.h"
79
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/kernel.h>
83 #include <sys/proc.h>
84 #include <sys/msgbuf.h>
85 #include <sys/vmmeter.h>
86 #include <sys/mman.h>
87
88 #include <vm/vm.h>
89 #include <vm/vm_param.h>
90 #include <sys/sysctl.h>
91 #include <sys/lock.h>
92 #include <vm/vm_kern.h>
93 #include <vm/vm_page.h>
94 #include <vm/vm_map.h>
95 #include <vm/vm_object.h>
96 #include <vm/vm_extern.h>
97 #include <vm/vm_pageout.h>
98 #include <vm/vm_pager.h>
99 #include <vm/vm_zone.h>
100
101 #include <sys/user.h>
102 #include <sys/thread2.h>
103 #include <sys/sysref2.h>
104
105 #include <machine/cputypes.h>
106 #include <machine/md_var.h>
107 #include <machine/specialreg.h>
108 #include <machine/smp.h>
109 #include <machine_base/apic/apicreg.h>
110 #include <machine/globaldata.h>
111 #include <machine/pmap.h>
112 #include <machine/pmap_inval.h>
113
114 #include <ddb/ddb.h>
115
116 #define PMAP_KEEP_PDIRS
117 #ifndef PMAP_SHPGPERPROC
118 #define PMAP_SHPGPERPROC 200
119 #endif
120
121 #if defined(DIAGNOSTIC)
122 #define PMAP_DIAGNOSTIC
123 #endif
124
125 #define MINPV 2048
126
127 #if !defined(PMAP_DIAGNOSTIC)
128 #define PMAP_INLINE __inline
129 #else
130 #define PMAP_INLINE
131 #endif
132
133 /* JGPMAP32 */
134 #define PTDPTDI 0
135
136 #define READY0
137 #define READY1
138 #define READY2
139 #define READY3
140 #define READY4
141 #define READY5
142
143 /*
144  * Get PDEs and PTEs for user/kernel address space
145  */
146 #if JGPMAP32
147 #define pmap_pde(m, v)  (&((m)->pm_pdir[(vm_offset_t)(v) >> PDRSHIFT]))
148 #endif
149 static pd_entry_t *pmap_pde(pmap_t pmap, vm_offset_t va);
150 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
151
152 #define pmap_pde_v(pte)         ((*(pd_entry_t *)pte & PG_V) != 0)
153 #define pmap_pte_w(pte)         ((*(pt_entry_t *)pte & PG_W) != 0)
154 #define pmap_pte_m(pte)         ((*(pt_entry_t *)pte & PG_M) != 0)
155 #define pmap_pte_u(pte)         ((*(pt_entry_t *)pte & PG_A) != 0)
156 #define pmap_pte_v(pte)         ((*(pt_entry_t *)pte & PG_V) != 0)
157
158
159 /*
160  * Given a map and a machine independent protection code,
161  * convert to a vax protection code.
162  */
163 #define pte_prot(m, p)          \
164         (protection_codes[p & (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE)])
165 static int protection_codes[8];
166
167 struct pmap kernel_pmap;
168 static TAILQ_HEAD(,pmap)        pmap_list = TAILQ_HEAD_INITIALIZER(pmap_list);
169
170 vm_paddr_t avail_start;         /* PA of first available physical page */
171 vm_paddr_t avail_end;           /* PA of last available physical page */
172 vm_offset_t virtual_start;      /* VA of first avail page (after kernel bss) */
173 vm_offset_t virtual_end;        /* VA of last avail page (end of kernel AS) */
174 vm_offset_t KvaStart;           /* VA start of KVA space */
175 vm_offset_t KvaEnd;             /* VA end of KVA space (non-inclusive) */
176 vm_offset_t KvaSize;            /* max size of kernel virtual address space */
177 static boolean_t pmap_initialized = FALSE;      /* Has pmap_init completed? */
178 static int pgeflag;             /* PG_G or-in */
179 static int pseflag;             /* PG_PS or-in */
180
181 static vm_object_t kptobj;
182
183 static int ndmpdp;
184 static vm_paddr_t dmaplimit;
185 static int nkpt;
186 vm_offset_t kernel_vm_end;
187
188 static uint64_t KPDphys;        /* phys addr of kernel level 2 */
189 uint64_t                KPDPphys;       /* phys addr of kernel level 3 */
190 uint64_t                KPML4phys;      /* phys addr of kernel level 4 */
191
192 static uint64_t DMPDphys;       /* phys addr of direct mapped level 2 */
193 static uint64_t DMPDPphys;      /* phys addr of direct mapped level 3 */
194
195 /*
196  * Data for the pv entry allocation mechanism
197  */
198 static vm_zone_t pvzone;
199 static struct vm_zone pvzone_store;
200 static struct vm_object pvzone_obj;
201 static int pv_entry_count=0, pv_entry_max=0, pv_entry_high_water=0;
202 static int pmap_pagedaemon_waken = 0;
203 static struct pv_entry *pvinit;
204
205 /*
206  * All those kernel PT submaps that BSD is so fond of
207  */
208 pt_entry_t *CMAP1 = 0, *ptmmap;
209 caddr_t CADDR1 = 0, ptvmmap = 0;
210 static pt_entry_t *msgbufmap;
211 struct msgbuf *msgbufp=0;
212
213 /*
214  * Crashdump maps.
215  */
216 static pt_entry_t *pt_crashdumpmap;
217 static caddr_t crashdumpmap;
218
219 extern uint64_t KPTphys;
220 extern pt_entry_t *SMPpt;
221 extern uint64_t SMPptpa;
222
223 #define DISABLE_PSE
224
225 static PMAP_INLINE void free_pv_entry (pv_entry_t pv);
226 static pv_entry_t get_pv_entry (void);
227 static void     i386_protection_init (void);
228 static __inline void    pmap_clearbit (vm_page_t m, int bit);
229
230 static void     pmap_remove_all (vm_page_t m);
231 static void     pmap_enter_quick (pmap_t pmap, vm_offset_t va, vm_page_t m);
232 static int pmap_remove_pte (struct pmap *pmap, pt_entry_t *ptq, 
233                                 vm_offset_t sva, pmap_inval_info_t info);
234 static void pmap_remove_page (struct pmap *pmap, 
235                                 vm_offset_t va, pmap_inval_info_t info);
236 static int pmap_remove_entry (struct pmap *pmap, vm_page_t m,
237                                 vm_offset_t va, pmap_inval_info_t info);
238 static boolean_t pmap_testbit (vm_page_t m, int bit);
239 static void pmap_insert_entry (pmap_t pmap, vm_offset_t va,
240                 vm_page_t mpte, vm_page_t m);
241
242 static vm_page_t pmap_allocpte (pmap_t pmap, vm_offset_t va);
243
244 static int pmap_release_free_page (pmap_t pmap, vm_page_t p);
245 static vm_page_t _pmap_allocpte (pmap_t pmap, vm_pindex_t ptepindex);
246 static pt_entry_t * pmap_pte_quick (pmap_t pmap, vm_offset_t va);
247 static vm_page_t pmap_page_lookup (vm_object_t object, vm_pindex_t pindex);
248 static int pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m,
249                 pmap_inval_info_t info);
250 static int pmap_unuse_pt (pmap_t, vm_offset_t, vm_page_t, pmap_inval_info_t);
251 static vm_offset_t pmap_kmem_choose(vm_offset_t addr);
252
253 static unsigned pdir4mb;
254
255 /*
256  * Move the kernel virtual free pointer to the next
257  * 2MB.  This is used to help improve performance
258  * by using a large (2MB) page for much of the kernel
259  * (.text, .data, .bss)
260  */
261 static vm_offset_t
262 pmap_kmem_choose(vm_offset_t addr)
263 READY2
264 {
265         vm_offset_t newaddr = addr;
266
267         newaddr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
268         return newaddr;
269 }
270
271 /*
272  * pmap_pte_quick:
273  *
274  *      Super fast pmap_pte routine best used when scanning the pv lists.
275  *      This eliminates many course-grained invltlb calls.  Note that many of
276  *      the pv list scans are across different pmaps and it is very wasteful
277  *      to do an entire invltlb when checking a single mapping.
278  *
279  *      Should only be called while in a critical section.
280  */
281 static __inline pt_entry_t *pmap_pte(pmap_t pmap, vm_offset_t va);
282
283 static pt_entry_t * 
284 pmap_pte_quick(pmap_t pmap, vm_offset_t va)
285 READY0
286 {
287         return pmap_pte(pmap, va);
288 }
289
290 /* Return a non-clipped PD index for a given VA */
291 static __inline vm_pindex_t
292 pmap_pde_pindex(vm_offset_t va)
293 READY1
294 {
295         return va >> PDRSHIFT;
296 }
297
298 /* Return various clipped indexes for a given VA */
299 static __inline vm_pindex_t
300 pmap_pte_index(vm_offset_t va)
301 READY1
302 {
303
304         return ((va >> PAGE_SHIFT) & ((1ul << NPTEPGSHIFT) - 1));
305 }
306
307 static __inline vm_pindex_t
308 pmap_pde_index(vm_offset_t va)
309 READY1
310 {
311
312         return ((va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1));
313 }
314
315 static __inline vm_pindex_t
316 pmap_pdpe_index(vm_offset_t va)
317 READY1
318 {
319
320         return ((va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1));
321 }
322
323 static __inline vm_pindex_t
324 pmap_pml4e_index(vm_offset_t va)
325 READY1
326 {
327
328         return ((va >> PML4SHIFT) & ((1ul << NPML4EPGSHIFT) - 1));
329 }
330
331 /* Return a pointer to the PML4 slot that corresponds to a VA */
332 static __inline pml4_entry_t *
333 pmap_pml4e(pmap_t pmap, vm_offset_t va)
334 READY1
335 {
336
337         return (&pmap->pm_pml4[pmap_pml4e_index(va)]);
338 }
339
340 /* Return a pointer to the PDP slot that corresponds to a VA */
341 static __inline pdp_entry_t *
342 pmap_pml4e_to_pdpe(pml4_entry_t *pml4e, vm_offset_t va)
343 READY1
344 {
345         pdp_entry_t *pdpe;
346
347         pdpe = (pdp_entry_t *)PHYS_TO_DMAP(*pml4e & PG_FRAME);
348         return (&pdpe[pmap_pdpe_index(va)]);
349 }
350
351 /* Return a pointer to the PDP slot that corresponds to a VA */
352 static __inline pdp_entry_t *
353 pmap_pdpe(pmap_t pmap, vm_offset_t va)
354 READY1
355 {
356         pml4_entry_t *pml4e;
357
358         pml4e = pmap_pml4e(pmap, va);
359         if ((*pml4e & PG_V) == 0)
360                 return NULL;
361         return (pmap_pml4e_to_pdpe(pml4e, va));
362 }
363
364 /* Return a pointer to the PD slot that corresponds to a VA */
365 static __inline pd_entry_t *
366 pmap_pdpe_to_pde(pdp_entry_t *pdpe, vm_offset_t va)
367 READY1
368 {
369         pd_entry_t *pde;
370
371         pde = (pd_entry_t *)PHYS_TO_DMAP(*pdpe & PG_FRAME);
372         return (&pde[pmap_pde_index(va)]);
373 }
374
375 /* Return a pointer to the PD slot that corresponds to a VA */
376 static __inline pd_entry_t *
377 pmap_pde(pmap_t pmap, vm_offset_t va)
378 READY1
379 {
380         pdp_entry_t *pdpe;
381
382         pdpe = pmap_pdpe(pmap, va);
383         if (pdpe == NULL || (*pdpe & PG_V) == 0)
384                  return NULL;
385         return (pmap_pdpe_to_pde(pdpe, va));
386 }
387
388 /* Return a pointer to the PT slot that corresponds to a VA */
389 static __inline pt_entry_t *
390 pmap_pde_to_pte(pd_entry_t *pde, vm_offset_t va)
391 READY1
392 {
393         pt_entry_t *pte;
394
395         pte = (pt_entry_t *)PHYS_TO_DMAP(*pde & PG_FRAME);
396         return (&pte[pmap_pte_index(va)]);
397 }
398
399 /* Return a pointer to the PT slot that corresponds to a VA */
400 static __inline pt_entry_t *
401 pmap_pte(pmap_t pmap, vm_offset_t va)
402 READY1
403 {
404         pd_entry_t *pde;
405
406         pde = pmap_pde(pmap, va);
407         if (pde == NULL || (*pde & PG_V) == 0)
408                 return NULL;
409         if ((*pde & PG_PS) != 0)        /* compat with i386 pmap_pte() */
410                 return ((pt_entry_t *)pde);
411         return (pmap_pde_to_pte(pde, va));
412 }
413
414
415 PMAP_INLINE pt_entry_t *
416 vtopte(vm_offset_t va)
417 READY1
418 {
419         uint64_t mask = ((1ul << (NPTEPGSHIFT + NPDEPGSHIFT + NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1);
420
421         return (PTmap + ((va >> PAGE_SHIFT) & mask));
422 }
423
424 static __inline pd_entry_t *
425 vtopde(vm_offset_t va)
426 READY1
427 {
428         uint64_t mask = ((1ul << (NPDEPGSHIFT + NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1);
429
430         return (PDmap + ((va >> PDRSHIFT) & mask));
431 }
432
433 static uint64_t
434 allocpages(vm_paddr_t *firstaddr, int n)
435 READY1
436 {
437         uint64_t ret;
438
439         ret = *firstaddr;
440         bzero((void *)ret, n * PAGE_SIZE);
441         *firstaddr += n * PAGE_SIZE;
442         return (ret);
443 }
444
445 void
446 create_pagetables(vm_paddr_t *firstaddr)
447 READY0
448 {
449         int i;
450         int count;
451         uint64_t cpu0pp, cpu0idlestk;
452         int idlestk_page_offset = offsetof(struct privatespace, idlestack) / PAGE_SIZE;
453
454         /* we are running (mostly) V=P at this point */
455
456         /* Allocate pages */
457         KPTphys = allocpages(firstaddr, NKPT);
458         KPML4phys = allocpages(firstaddr, 1);
459         KPDPphys = allocpages(firstaddr, NKPML4E);
460         KPDphys = allocpages(firstaddr, NKPDPE);
461
462         ndmpdp = (ptoa(Maxmem) + NBPDP - 1) >> PDPSHIFT;
463         if (ndmpdp < 4)         /* Minimum 4GB of dirmap */
464                 ndmpdp = 4;
465         DMPDPphys = allocpages(firstaddr, NDMPML4E);
466         if ((amd_feature & AMDID_PAGE1GB) == 0)
467                 DMPDphys = allocpages(firstaddr, ndmpdp);
468         dmaplimit = (vm_paddr_t)ndmpdp << PDPSHIFT;
469
470         /* Fill in the underlying page table pages */
471         /* Read-only from zero to physfree */
472         /* XXX not fully used, underneath 2M pages */
473         for (i = 0; (i << PAGE_SHIFT) < *firstaddr; i++) {
474                 ((pt_entry_t *)KPTphys)[i] = i << PAGE_SHIFT;
475                 ((pt_entry_t *)KPTphys)[i] |= PG_RW | PG_V | PG_G;
476         }
477
478         /* Now map the page tables at their location within PTmap */
479         for (i = 0; i < NKPT; i++) {
480                 ((pd_entry_t *)KPDphys)[i] = KPTphys + (i << PAGE_SHIFT);
481                 ((pd_entry_t *)KPDphys)[i] |= PG_RW | PG_V;
482         }
483
484         /* Map from zero to end of allocations under 2M pages */
485         /* This replaces some of the KPTphys entries above */
486         for (i = 0; (i << PDRSHIFT) < *firstaddr; i++) {
487                 ((pd_entry_t *)KPDphys)[i] = i << PDRSHIFT;
488                 ((pd_entry_t *)KPDphys)[i] |= PG_RW | PG_V | PG_PS | PG_G;
489         }
490
491         /* And connect up the PD to the PDP */
492         for (i = 0; i < NKPDPE; i++) {
493                 ((pdp_entry_t *)KPDPphys)[i + KPDPI] = KPDphys +
494                     (i << PAGE_SHIFT);
495                 ((pdp_entry_t *)KPDPphys)[i + KPDPI] |= PG_RW | PG_V | PG_U;
496         }
497
498         /* Now set up the direct map space using either 2MB or 1GB pages */
499         /* Preset PG_M and PG_A because demotion expects it */
500         if ((amd_feature & AMDID_PAGE1GB) == 0) {
501                 for (i = 0; i < NPDEPG * ndmpdp; i++) {
502                         ((pd_entry_t *)DMPDphys)[i] = (vm_paddr_t)i << PDRSHIFT;
503                         ((pd_entry_t *)DMPDphys)[i] |= PG_RW | PG_V | PG_PS |
504                             PG_G | PG_M | PG_A;
505                 }
506                 /* And the direct map space's PDP */
507                 for (i = 0; i < ndmpdp; i++) {
508                         ((pdp_entry_t *)DMPDPphys)[i] = DMPDphys +
509                             (i << PAGE_SHIFT);
510                         ((pdp_entry_t *)DMPDPphys)[i] |= PG_RW | PG_V | PG_U;
511                 }
512         } else {
513                 for (i = 0; i < ndmpdp; i++) {
514                         ((pdp_entry_t *)DMPDPphys)[i] =
515                             (vm_paddr_t)i << PDPSHIFT;
516                         ((pdp_entry_t *)DMPDPphys)[i] |= PG_RW | PG_V | PG_PS |
517                             PG_G | PG_M | PG_A;
518                 }
519         }
520
521         /* And recursively map PML4 to itself in order to get PTmap */
522         ((pdp_entry_t *)KPML4phys)[PML4PML4I] = KPML4phys;
523         ((pdp_entry_t *)KPML4phys)[PML4PML4I] |= PG_RW | PG_V | PG_U;
524
525         /* Connect the Direct Map slot up to the PML4 */
526         ((pdp_entry_t *)KPML4phys)[DMPML4I] = DMPDPphys;
527         ((pdp_entry_t *)KPML4phys)[DMPML4I] |= PG_RW | PG_V | PG_U;
528
529         /* Connect the KVA slot up to the PML4 */
530         ((pdp_entry_t *)KPML4phys)[KPML4I] = KPDPphys;
531         ((pdp_entry_t *)KPML4phys)[KPML4I] |= PG_RW | PG_V | PG_U;
532 #if JGPMAP32
533         common_lvl4_phys = allocpages(firstaddr, 1);    /* 512 512G mappings */
534         common_lvl3_phys = allocpages(firstaddr, 1);    /* 512 1G mappings */
535         KPTphys = allocpages(firstaddr, NKPT);          /* kernel page table */
536         IdlePTD = allocpages(firstaddr, 1);             /* kernel page dir */
537         cpu0pp = allocpages(firstaddr, MDGLOBALDATA_BASEALLOC_PAGES);
538         cpu0idlestk = allocpages(firstaddr, UPAGES);
539         SMPptpa = allocpages(firstaddr, 1);
540         SMPpt = (void *)(SMPptpa + KERNBASE);
541
542
543         /*
544          * Load kernel page table with kernel memory mappings
545          */
546         for (i = 0; (i << PAGE_SHIFT) < *firstaddr; i++) {
547                 ((pt_entry_t *)KPTphys)[i] = i << PAGE_SHIFT;
548                 ((pt_entry_t *)KPTphys)[i] |= PG_RW | PG_V;
549         }
550
551 #ifndef JG
552         for (i = 0; i < NKPT; i++) {
553                 ((pd_entry_t *)IdlePTD)[i] = KPTphys + (i << PAGE_SHIFT);
554                 ((pd_entry_t *)IdlePTD)[i] |= PG_RW | PG_V;
555         }
556 #endif
557
558         /*
559          * Set up the kernel page table itself.
560          */
561         for (i = 0; i < NKPT; i++) {
562                 ((pd_entry_t *)IdlePTD)[KPTDI + i] = KPTphys + (i << PAGE_SHIFT);
563                 ((pd_entry_t *)IdlePTD)[KPTDI + i] |= PG_RW | PG_V;
564         }
565
566 #ifndef JG
567         count = ISA_HOLE_LENGTH >> PAGE_SHIFT;
568         for (i = 0; i < count; i++) {
569                 ((pt_entry_t *)KPTphys)[amd64_btop(ISA_HOLE_START) + i] = \
570                         (ISA_HOLE_START + i * PAGE_SIZE) | PG_RW | PG_V;
571         }
572 #endif
573
574         /*
575          * Self-mapping
576          */
577         ((pd_entry_t *)IdlePTD)[PTDPTDI] = (pd_entry_t)IdlePTD | PG_RW | PG_V;
578
579         /*
580          * Map CPU_prvspace[0].mdglobaldata
581          */
582         for (i = 0; i < MDGLOBALDATA_BASEALLOC_PAGES; i++) {
583                 ((pt_entry_t *)SMPptpa)[i] = \
584                         (cpu0pp + i * PAGE_SIZE) | PG_RW | PG_V;
585         }
586
587         /*
588          * Map CPU_prvspace[0].idlestack
589          */
590         for (i = 0; i < UPAGES; i++) {
591                 ((pt_entry_t *)SMPptpa)[idlestk_page_offset + i] = \
592                         (cpu0idlestk + i * PAGE_SIZE) | PG_RW | PG_V;
593         }
594
595         /*
596          * Link SMPpt.
597          */
598         ((pd_entry_t *)IdlePTD)[MPPTDI] = SMPptpa | PG_RW | PG_V;
599
600         /*
601          * PML4 maps level 3
602          */
603         ((pml4_entry_t *)common_lvl4_phys)[LINKPML4I] = common_lvl3_phys | PG_RW | PG_V | PG_U;
604
605         /*
606          * location of "virtual CR3" - a PDP entry that is loaded
607          * with a PD physical address (+ page attributes).
608          * Matt: location of user page directory entry (representing 1G)
609          */
610         link_pdpe = &((pdp_entry_t *)common_lvl3_phys)[LINKPDPI];
611 #endif  /* JGPMAP32 */
612 }
613
614 READY0
615 void
616 init_paging(vm_paddr_t *firstaddr) {
617         create_pagetables(firstaddr);
618
619 #if JGPMAP32
620         /* switch to the newly created page table */
621         *link_pdpe = IdlePTD | PG_RW | PG_V | PG_U;
622         load_cr3(common_lvl4_phys);
623         link_pdpe = (void *)((char *)link_pdpe + KERNBASE);
624
625         KvaStart = (vm_offset_t)VADDR(PTDPTDI, 0);
626         KvaEnd = (vm_offset_t)VADDR(APTDPTDI, 0);
627         KvaSize = KvaEnd - KvaStart;
628 #endif
629 }
630
631 /*
632  *      Bootstrap the system enough to run with virtual memory.
633  *
634  *      On the i386 this is called after mapping has already been enabled
635  *      and just syncs the pmap module with what has already been done.
636  *      [We can't call it easily with mapping off since the kernel is not
637  *      mapped with PA == VA, hence we would have to relocate every address
638  *      from the linked base (virtual) address "KERNBASE" to the actual
639  *      (physical) address starting relative to 0]
640  */
641 void
642 pmap_bootstrap(vm_paddr_t *firstaddr)
643 READY0
644 {
645         vm_offset_t va;
646         pt_entry_t *pte;
647         struct mdglobaldata *gd;
648         int i;
649         int pg;
650
651         KvaStart = VM_MIN_KERNEL_ADDRESS;
652         KvaEnd = VM_MAX_KERNEL_ADDRESS;
653         KvaSize = KvaEnd - KvaStart;
654
655         avail_start = *firstaddr;
656
657         /*
658          * Create an initial set of page tables to run the kernel in.
659          */
660         create_pagetables(firstaddr);
661
662         virtual_start = (vm_offset_t) PTOV_OFFSET + *firstaddr;
663         virtual_start = pmap_kmem_choose(virtual_start);
664
665         virtual_end = VM_MAX_KERNEL_ADDRESS;
666
667         /* XXX do %cr0 as well */
668         load_cr4(rcr4() | CR4_PGE | CR4_PSE);
669         load_cr3(KPML4phys);
670
671         /*
672          * Initialize protection array.
673          */
674         i386_protection_init();
675
676         /*
677          * The kernel's pmap is statically allocated so we don't have to use
678          * pmap_create, which is unlikely to work correctly at this part of
679          * the boot sequence (XXX and which no longer exists).
680          */
681 #if JGPMAP32
682         kernel_pmap.pm_pdir = (pd_entry_t *)(PTOV_OFFSET + (uint64_t)IdlePTD);
683 #endif
684         kernel_pmap.pm_pml4 = (pdp_entry_t *) (PTOV_OFFSET + KPML4phys);
685         kernel_pmap.pm_count = 1;
686         kernel_pmap.pm_active = (cpumask_t)-1;  /* don't allow deactivation */
687         TAILQ_INIT(&kernel_pmap.pm_pvlist);
688         nkpt = NKPT;
689
690         /*
691          * Reserve some special page table entries/VA space for temporary
692          * mapping of pages.
693          */
694 #define SYSMAP(c, p, v, n)      \
695         v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
696
697         va = virtual_start;
698 #ifdef JG
699         pte = (pt_entry_t *) pmap_pte(&kernel_pmap, va);
700 #else
701         pte = vtopte(va);
702 #endif
703
704         /*
705          * CMAP1/CMAP2 are used for zeroing and copying pages.
706          */
707         SYSMAP(caddr_t, CMAP1, CADDR1, 1)
708
709         /*
710          * Crashdump maps.
711          */
712         SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS);
713
714         /*
715          * ptvmmap is used for reading arbitrary physical pages via
716          * /dev/mem.
717          */
718         SYSMAP(caddr_t, ptmmap, ptvmmap, 1)
719
720         /*
721          * msgbufp is used to map the system message buffer.
722          * XXX msgbufmap is not used.
723          */
724         SYSMAP(struct msgbuf *, msgbufmap, msgbufp,
725                atop(round_page(MSGBUF_SIZE)))
726
727         virtual_start = va;
728
729         *CMAP1 = 0;
730 #if JGPMAP32
731         for (i = 0; i < NKPT; i++)
732                 PTD[i] = 0;
733 #endif
734
735         /*
736          * PG_G is terribly broken on SMP because we IPI invltlb's in some
737          * cases rather then invl1pg.  Actually, I don't even know why it
738          * works under UP because self-referential page table mappings
739          */
740 #ifdef SMP
741         pgeflag = 0;
742 #else
743         if (cpu_feature & CPUID_PGE)
744                 pgeflag = PG_G;
745 #endif
746         
747 /*
748  * Initialize the 4MB page size flag
749  */
750         pseflag = 0;
751 /*
752  * The 4MB page version of the initial
753  * kernel page mapping.
754  */
755         pdir4mb = 0;
756
757 #if !defined(DISABLE_PSE)
758         if (cpu_feature & CPUID_PSE) {
759                 pt_entry_t ptditmp;
760                 /*
761                  * Note that we have enabled PSE mode
762                  */
763                 pseflag = PG_PS;
764                 ptditmp = *(PTmap + amd64_btop(KERNBASE));
765                 ptditmp &= ~(NBPDR - 1);
766                 ptditmp |= PG_V | PG_RW | PG_PS | PG_U | pgeflag;
767                 pdir4mb = ptditmp;
768
769 #ifndef SMP
770                 /*
771                  * Enable the PSE mode.  If we are SMP we can't do this
772                  * now because the APs will not be able to use it when
773                  * they boot up.
774                  */
775                 load_cr4(rcr4() | CR4_PSE);
776
777                 /*
778                  * We can do the mapping here for the single processor
779                  * case.  We simply ignore the old page table page from
780                  * now on.
781                  */
782                 /*
783                  * For SMP, we still need 4K pages to bootstrap APs,
784                  * PSE will be enabled as soon as all APs are up.
785                  */
786                 PTD[KPTDI] = (pd_entry_t)ptditmp;
787 #if JGPMAP32
788                 kernel_pmap.pm_pdir[KPTDI] = (pd_entry_t)ptditmp;
789 #endif
790                 cpu_invltlb();
791 #endif
792         }
793 #endif
794 #ifdef SMP
795         if (cpu_apic_address == 0)
796                 panic("pmap_bootstrap: no local apic!");
797
798         /* local apic is mapped on last page */
799         SMPpt[NPTEPG - 1] = (pt_entry_t)(PG_V | PG_RW | PG_N | pgeflag |
800             (cpu_apic_address & PG_FRAME));
801 #endif
802
803         /*
804          * We need to finish setting up the globaldata page for the BSP.
805          * locore has already populated the page table for the mdglobaldata
806          * portion.
807          */
808         pg = MDGLOBALDATA_BASEALLOC_PAGES;
809         gd = &CPU_prvspace[0].mdglobaldata;
810         gd->gd_CMAP1 = &SMPpt[pg + 0];
811         gd->gd_CMAP2 = &SMPpt[pg + 1];
812         gd->gd_CMAP3 = &SMPpt[pg + 2];
813         gd->gd_PMAP1 = &SMPpt[pg + 3];
814         gd->gd_CADDR1 = CPU_prvspace[0].CPAGE1;
815         gd->gd_CADDR2 = CPU_prvspace[0].CPAGE2;
816         gd->gd_CADDR3 = CPU_prvspace[0].CPAGE3;
817         gd->gd_PADDR1 = (pt_entry_t *)CPU_prvspace[0].PPAGE1;
818
819         cpu_invltlb();
820 }
821
822 #ifdef SMP
823 /*
824  * Set 4mb pdir for mp startup
825  */
826 void
827 pmap_set_opt(void)
828 READY0
829 {
830         if (pseflag && (cpu_feature & CPUID_PSE)) {
831                 load_cr4(rcr4() | CR4_PSE);
832                 if (pdir4mb && mycpu->gd_cpuid == 0) {  /* only on BSP */
833 #if JGPMAP32
834                         kernel_pmap.pm_pdir[KPTDI] =
835                             PTD[KPTDI] = (pd_entry_t)pdir4mb;
836 #endif
837                         cpu_invltlb();
838                 }
839         }
840 }
841 #endif
842
843 /*
844  *      Initialize the pmap module.
845  *      Called by vm_init, to initialize any structures that the pmap
846  *      system needs to map virtual memory.
847  *      pmap_init has been enhanced to support in a fairly consistant
848  *      way, discontiguous physical memory.
849  */
850 void
851 pmap_init(void)
852 READY0
853 {
854         int i;
855         int initial_pvs;
856
857         /*
858          * object for kernel page table pages
859          */
860         /* JG I think the number can be arbitrary */
861         kptobj = vm_object_allocate(OBJT_DEFAULT, 5);
862
863         /*
864          * Allocate memory for random pmap data structures.  Includes the
865          * pv_head_table.
866          */
867
868         for(i = 0; i < vm_page_array_size; i++) {
869                 vm_page_t m;
870
871                 m = &vm_page_array[i];
872                 TAILQ_INIT(&m->md.pv_list);
873                 m->md.pv_list_count = 0;
874         }
875
876         /*
877          * init the pv free list
878          */
879         initial_pvs = vm_page_array_size;
880         if (initial_pvs < MINPV)
881                 initial_pvs = MINPV;
882         pvzone = &pvzone_store;
883         pvinit = (struct pv_entry *) kmem_alloc(&kernel_map,
884                 initial_pvs * sizeof (struct pv_entry));
885         zbootinit(pvzone, "PV ENTRY", sizeof (struct pv_entry), pvinit,
886                 initial_pvs);
887
888         /*
889          * Now it is safe to enable pv_table recording.
890          */
891         pmap_initialized = TRUE;
892 }
893
894 /*
895  * Initialize the address space (zone) for the pv_entries.  Set a
896  * high water mark so that the system can recover from excessive
897  * numbers of pv entries.
898  */
899 void
900 pmap_init2(void)
901 READY0
902 {
903         int shpgperproc = PMAP_SHPGPERPROC;
904
905         TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
906         pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
907         TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
908         pv_entry_high_water = 9 * (pv_entry_max / 10);
909         zinitna(pvzone, &pvzone_obj, NULL, 0, pv_entry_max, ZONE_INTERRUPT, 1);
910 }
911
912
913 /***************************************************
914  * Low level helper routines.....
915  ***************************************************/
916
917 #if defined(PMAP_DIAGNOSTIC)
918
919 /*
920  * This code checks for non-writeable/modified pages.
921  * This should be an invalid condition.
922  */
923 static int
924 pmap_nw_modified(pt_entry_t pte)
925 READY1
926 {
927         if ((pte & (PG_M|PG_RW)) == PG_M)
928                 return 1;
929         else
930                 return 0;
931 }
932 #endif
933
934
935 /*
936  * this routine defines the region(s) of memory that should
937  * not be tested for the modified bit.
938  */
939 static PMAP_INLINE int
940 pmap_track_modified(vm_offset_t va)
941 READY0
942 {
943         if ((va < clean_sva) || (va >= clean_eva)) 
944                 return 1;
945         else
946                 return 0;
947 }
948
949 /*
950  * pmap_extract:
951  *
952  *      Extract the physical page address associated with the map/VA pair.
953  *
954  *      This function may not be called from an interrupt if the pmap is
955  *      not kernel_pmap.
956  */
957 vm_paddr_t 
958 pmap_extract(pmap_t pmap, vm_offset_t va)
959 READY1
960 {
961         vm_paddr_t rtval;
962         pt_entry_t *pte;
963         pd_entry_t pde, *pdep;
964
965         rtval = 0;
966         pdep = pmap_pde(pmap, va);
967         if (pdep != NULL) {
968                 pde = *pdep;
969                 if (pde) {
970                         if ((pde & PG_PS) != 0) {
971                                 rtval = (pde & PG_PS_FRAME) | (va & PDRMASK);
972                         } else {
973                                 pte = pmap_pde_to_pte(pdep, va);
974                                 rtval = (*pte & PG_FRAME) | (va & PAGE_MASK);
975                         }
976                 }
977         }
978         return rtval;
979 }
980
981 /*
982  *      Routine:        pmap_kextract
983  *      Function:
984  *              Extract the physical page address associated
985  *              kernel virtual address.
986  */
987 vm_paddr_t
988 pmap_kextract(vm_offset_t va)
989 READY1
990 {
991         pd_entry_t pde;
992         vm_paddr_t pa;
993
994         if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS) {
995                 pa = DMAP_TO_PHYS(va);
996         } else {
997                 pde = *vtopde(va);
998                 if (pde & PG_PS) {
999                         pa = (pde & PG_PS_FRAME) | (va & PDRMASK);
1000                 } else {
1001                         /*
1002                          * Beware of a concurrent promotion that changes the
1003                          * PDE at this point!  For example, vtopte() must not
1004                          * be used to access the PTE because it would use the
1005                          * new PDE.  It is, however, safe to use the old PDE
1006                          * because the page table page is preserved by the
1007                          * promotion.
1008                          */
1009                         pa = *pmap_pde_to_pte(&pde, va);
1010                         pa = (pa & PG_FRAME) | (va & PAGE_MASK);
1011                 }
1012         }
1013         return pa;
1014 }
1015
1016 /***************************************************
1017  * Low level mapping routines.....
1018  ***************************************************/
1019
1020 /*
1021  * Routine: pmap_kenter
1022  * Function:
1023  *      Add a wired page to the KVA
1024  *      NOTE! note that in order for the mapping to take effect -- you
1025  *      should do an invltlb after doing the pmap_kenter().
1026  */
1027 void 
1028 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
1029 READY1
1030 {
1031         pt_entry_t *pte;
1032         pt_entry_t npte;
1033         pmap_inval_info info;
1034
1035         pmap_inval_init(&info);
1036         npte = pa | PG_RW | PG_V | pgeflag;
1037         pte = vtopte(va);
1038         pmap_inval_add(&info, &kernel_pmap, va);
1039         *pte = npte;
1040         pmap_inval_flush(&info);
1041 }
1042
1043 /*
1044  * Routine: pmap_kenter_quick
1045  * Function:
1046  *      Similar to pmap_kenter(), except we only invalidate the
1047  *      mapping on the current CPU.
1048  */
1049 void
1050 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
1051 READY1
1052 {
1053         pt_entry_t *pte;
1054         pt_entry_t npte;
1055
1056         npte = pa | PG_RW | PG_V | pgeflag;
1057         pte = vtopte(va);
1058         *pte = npte;
1059         cpu_invlpg((void *)va);
1060 }
1061
1062 void
1063 pmap_kenter_sync(vm_offset_t va)
1064 READY1
1065 {
1066         pmap_inval_info info;
1067
1068         pmap_inval_init(&info);
1069         pmap_inval_add(&info, &kernel_pmap, va);
1070         pmap_inval_flush(&info);
1071 }
1072
1073 void
1074 pmap_kenter_sync_quick(vm_offset_t va)
1075 READY1
1076 {
1077         cpu_invlpg((void *)va);
1078 }
1079
1080 /*
1081  * remove a page from the kernel pagetables
1082  */
1083 void
1084 pmap_kremove(vm_offset_t va)
1085 READY1
1086 {
1087         pt_entry_t *pte;
1088         pmap_inval_info info;
1089
1090         pmap_inval_init(&info);
1091         pte = vtopte(va);
1092         pmap_inval_add(&info, &kernel_pmap, va);
1093         *pte = 0;
1094         pmap_inval_flush(&info);
1095 }
1096
1097 void
1098 pmap_kremove_quick(vm_offset_t va)
1099 READY1
1100 {
1101         pt_entry_t *pte;
1102         pte = vtopte(va);
1103         *pte = 0;
1104         cpu_invlpg((void *)va);
1105 }
1106
1107 /*
1108  * XXX these need to be recoded.  They are not used in any critical path.
1109  */
1110 void
1111 pmap_kmodify_rw(vm_offset_t va)
1112 READY1
1113 {
1114         *vtopte(va) |= PG_RW;
1115         cpu_invlpg((void *)va);
1116 }
1117
1118 void
1119 pmap_kmodify_nc(vm_offset_t va)
1120 READY1
1121 {
1122         *vtopte(va) |= PG_N;
1123         cpu_invlpg((void *)va);
1124 }
1125
1126 /*
1127  *      Used to map a range of physical addresses into kernel
1128  *      virtual address space.
1129  *
1130  *      For now, VM is already on, we only need to map the
1131  *      specified memory.
1132  */
1133 vm_offset_t
1134 pmap_map(vm_offset_t virt, vm_paddr_t start, vm_paddr_t end, int prot)
1135 READY3
1136 {
1137         return PHYS_TO_DMAP(start);
1138 }
1139
1140
1141 /*
1142  * Add a list of wired pages to the kva
1143  * this routine is only used for temporary
1144  * kernel mappings that do not need to have
1145  * page modification or references recorded.
1146  * Note that old mappings are simply written
1147  * over.  The page *must* be wired.
1148  */
1149 void
1150 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
1151 READY1
1152 {
1153         vm_offset_t end_va;
1154
1155         end_va = va + count * PAGE_SIZE;
1156                 
1157         while (va < end_va) {
1158                 pt_entry_t *pte;
1159
1160                 pte = vtopte(va);
1161                 *pte = VM_PAGE_TO_PHYS(*m) | PG_RW | PG_V | pgeflag;
1162                 cpu_invlpg((void *)va);
1163                 va += PAGE_SIZE;
1164                 m++;
1165         }
1166 #ifdef SMP
1167         smp_invltlb();  /* XXX */
1168 #endif
1169 }
1170
1171 void
1172 pmap_qenter2(vm_offset_t va, vm_page_t *m, int count, cpumask_t *mask)
1173 READY1
1174 {
1175         vm_offset_t end_va;
1176         cpumask_t cmask = mycpu->gd_cpumask;
1177
1178         end_va = va + count * PAGE_SIZE;
1179
1180         while (va < end_va) {
1181                 pt_entry_t *pte;
1182                 pt_entry_t pteval;
1183
1184                 /*
1185                  * Install the new PTE.  If the pte changed from the prior
1186                  * mapping we must reset the cpu mask and invalidate the page.
1187                  * If the pte is the same but we have not seen it on the
1188                  * current cpu, invlpg the existing mapping.  Otherwise the
1189                  * entry is optimal and no invalidation is required.
1190                  */
1191                 pte = vtopte(va);
1192                 pteval = VM_PAGE_TO_PHYS(*m) | PG_A | PG_RW | PG_V | pgeflag;
1193                 if (*pte != pteval) {
1194                         *mask = 0;
1195                         *pte = pteval;
1196                         cpu_invlpg((void *)va);
1197                 } else if ((*mask & cmask) == 0) {
1198                         cpu_invlpg((void *)va);
1199                 }
1200                 va += PAGE_SIZE;
1201                 m++;
1202         }
1203         *mask |= cmask;
1204 }
1205
1206 /*
1207  * this routine jerks page mappings from the
1208  * kernel -- it is meant only for temporary mappings.
1209  */
1210 void
1211 pmap_qremove(vm_offset_t va, int count)
1212 READY1
1213 {
1214         vm_offset_t end_va;
1215
1216         end_va = va + count * PAGE_SIZE;
1217
1218         while (va < end_va) {
1219                 pt_entry_t *pte;
1220
1221                 pte = vtopte(va);
1222                 *pte = 0;
1223                 cpu_invlpg((void *)va);
1224                 va += PAGE_SIZE;
1225         }
1226 #ifdef SMP
1227         smp_invltlb();
1228 #endif
1229 }
1230
1231 /*
1232  * This routine works like vm_page_lookup() but also blocks as long as the
1233  * page is busy.  This routine does not busy the page it returns.
1234  *
1235  * Unless the caller is managing objects whos pages are in a known state,
1236  * the call should be made with a critical section held so the page's object
1237  * association remains valid on return.
1238  */
1239 static vm_page_t
1240 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
1241 READY1
1242 {
1243         vm_page_t m;
1244
1245         do {
1246                 m = vm_page_lookup(object, pindex);
1247         } while (m && vm_page_sleep_busy(m, FALSE, "pplookp"));
1248
1249         return(m);
1250 }
1251
1252 /*
1253  * Create a new thread and optionally associate it with a (new) process.
1254  * NOTE! the new thread's cpu may not equal the current cpu.
1255  */
1256 void
1257 pmap_init_thread(thread_t td)
1258 READY1
1259 {
1260         /* enforce pcb placement */
1261         td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
1262         td->td_savefpu = &td->td_pcb->pcb_save;
1263         td->td_sp = (char *)td->td_pcb - 16; /* JG is -16 needed on amd64? */
1264 }
1265
1266 /*
1267  * This routine directly affects the fork perf for a process.
1268  */
1269 void
1270 pmap_init_proc(struct proc *p)
1271 READY1
1272 {
1273 }
1274
1275 /*
1276  * Dispose the UPAGES for a process that has exited.
1277  * This routine directly impacts the exit perf of a process.
1278  */
1279 void
1280 pmap_dispose_proc(struct proc *p)
1281 READY1
1282 {
1283         KASSERT(p->p_lock == 0, ("attempt to dispose referenced proc! %p", p));
1284 }
1285
1286 /***************************************************
1287  * Page table page management routines.....
1288  ***************************************************/
1289
1290 /*
1291  * This routine unholds page table pages, and if the hold count
1292  * drops to zero, then it decrements the wire count.
1293  */
1294 static int 
1295 _pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m, pmap_inval_info_t info) 
1296 READY1
1297 {
1298         /* 
1299          * Wait until we can busy the page ourselves.  We cannot have
1300          * any active flushes if we block.
1301          */
1302         if (m->flags & PG_BUSY) {
1303                 pmap_inval_flush(info);
1304                 while (vm_page_sleep_busy(m, FALSE, "pmuwpt"))
1305                         ;
1306         }
1307         KASSERT(m->queue == PQ_NONE,
1308                 ("_pmap_unwire_pte_hold: %p->queue != PQ_NONE", m));
1309
1310         if (m->hold_count == 1) {
1311                 /*
1312                  * Unmap the page table page
1313                  */
1314                 vm_page_busy(m);
1315                 pmap_inval_add(info, pmap, -1);
1316
1317                 if (m->pindex >= (NUPDE + NUPDPE)) {
1318                         /* PDP page */
1319                         pml4_entry_t *pml4;
1320                         pml4 = pmap_pml4e(pmap, va);
1321                         *pml4 = 0;
1322                 } else if (m->pindex >= NUPDE) {
1323                         /* PD page */
1324                         pdp_entry_t *pdp;
1325                         pdp = pmap_pdpe(pmap, va);
1326                         *pdp = 0;
1327                 } else {
1328                         /* PT page */
1329                         pd_entry_t *pd;
1330                         pd = pmap_pde(pmap, va);
1331                         *pd = 0;
1332                 }
1333
1334                 KKASSERT(pmap->pm_stats.resident_count > 0);
1335                 --pmap->pm_stats.resident_count;
1336
1337                 if (pmap->pm_ptphint == m)
1338                         pmap->pm_ptphint = NULL;
1339
1340                 if (m->pindex < NUPDE) {
1341                         /* We just released a PT, unhold the matching PD */
1342                         vm_page_t pdpg;
1343         
1344                         pdpg = PHYS_TO_VM_PAGE(*pmap_pdpe(pmap, va) & PG_FRAME);
1345                         pmap_unwire_pte_hold(pmap, va, pdpg, info);
1346                 }
1347                 if (m->pindex >= NUPDE && m->pindex < (NUPDE + NUPDPE)) {
1348                         /* We just released a PD, unhold the matching PDP */
1349                         vm_page_t pdppg;
1350         
1351                         pdppg = PHYS_TO_VM_PAGE(*pmap_pml4e(pmap, va) & PG_FRAME);
1352                         pmap_unwire_pte_hold(pmap, va, pdppg, info);
1353                 }
1354
1355                 /*
1356                  * This was our last hold, the page had better be unwired
1357                  * after we decrement wire_count.
1358                  * 
1359                  * FUTURE NOTE: shared page directory page could result in
1360                  * multiple wire counts.
1361                  */
1362                 vm_page_unhold(m);
1363                 --m->wire_count;
1364                 KKASSERT(m->wire_count == 0);
1365                 --vmstats.v_wire_count;
1366                 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1367                 vm_page_flash(m);
1368                 vm_page_free_zero(m);
1369                 return 1;
1370         } else {
1371                 /* JG Can we get here? */
1372                 KKASSERT(m->hold_count > 1);
1373                 vm_page_unhold(m);
1374                 return 0;
1375         }
1376 }
1377
1378 static PMAP_INLINE int
1379 pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m, pmap_inval_info_t info)
1380 READY1
1381 {
1382         KKASSERT(m->hold_count > 0);
1383         if (m->hold_count > 1) {
1384                 vm_page_unhold(m);
1385                 return 0;
1386         } else {
1387                 return _pmap_unwire_pte_hold(pmap, va, m, info);
1388         }
1389 }
1390
1391 /*
1392  * After removing a page table entry, this routine is used to
1393  * conditionally free the page, and manage the hold/wire counts.
1394  */
1395 static int
1396 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte,
1397                 pmap_inval_info_t info)
1398 READY1
1399 {
1400         /* JG Use FreeBSD/amd64 or FreeBSD/i386 ptepde approaches? */
1401         vm_pindex_t ptepindex;
1402         if (va >= VM_MAX_USER_ADDRESS)
1403                 return 0;
1404
1405         if (mpte == NULL) {
1406                 ptepindex = pmap_pde_pindex(va);
1407 #if JGHINT
1408                 if (pmap->pm_ptphint &&
1409                         (pmap->pm_ptphint->pindex == ptepindex)) {
1410                         mpte = pmap->pm_ptphint;
1411                 } else {
1412 #endif
1413                         pmap_inval_flush(info);
1414                         mpte = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1415                         pmap->pm_ptphint = mpte;
1416 #if JGHINT
1417                 }
1418 #endif
1419         }
1420
1421         return pmap_unwire_pte_hold(pmap, va, mpte, info);
1422 }
1423
1424 /*
1425  * Initialize pmap0/vmspace0.  This pmap is not added to pmap_list because
1426  * it, and IdlePTD, represents the template used to update all other pmaps.
1427  *
1428  * On architectures where the kernel pmap is not integrated into the user
1429  * process pmap, this pmap represents the process pmap, not the kernel pmap.
1430  * kernel_pmap should be used to directly access the kernel_pmap.
1431  */
1432 void
1433 pmap_pinit0(struct pmap *pmap)
1434 READY1
1435 {
1436 #if JGPMAP32
1437         pmap->pm_pdir =
1438                 (pd_entry_t *)kmem_alloc_pageable(&kernel_map, PAGE_SIZE);
1439         pmap_kenter((vm_offset_t)pmap->pm_pdir, (vm_offset_t) IdlePTD);
1440 #endif
1441         pmap->pm_pml4 = (pml4_entry_t *)(PTOV_OFFSET + KPML4phys);
1442         pmap->pm_count = 1;
1443         pmap->pm_active = 0;
1444         pmap->pm_ptphint = NULL;
1445         TAILQ_INIT(&pmap->pm_pvlist);
1446         bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1447 }
1448
1449 /*
1450  * Initialize a preallocated and zeroed pmap structure,
1451  * such as one in a vmspace structure.
1452  */
1453 void
1454 pmap_pinit(struct pmap *pmap)
1455 READY1
1456 {
1457         vm_page_t ptdpg;
1458
1459         /*
1460          * No need to allocate page table space yet but we do need a valid
1461          * page directory table.
1462          */
1463         if (pmap->pm_pml4 == NULL) {
1464                 pmap->pm_pml4 =
1465                     (pml4_entry_t *)kmem_alloc_pageable(&kernel_map, PAGE_SIZE);
1466         }
1467
1468         /*
1469          * Allocate an object for the ptes
1470          */
1471         if (pmap->pm_pteobj == NULL)
1472                 pmap->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, NUPDE + NUPDPE + PML4PML4I + 1);
1473
1474         /*
1475          * Allocate the page directory page, unless we already have
1476          * one cached.  If we used the cached page the wire_count will
1477          * already be set appropriately.
1478          */
1479         if ((ptdpg = pmap->pm_pdirm) == NULL) {
1480                 ptdpg = vm_page_grab(pmap->pm_pteobj, NUPDE + NUPDPE + PML4PML4I,
1481                                      VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1482                 pmap->pm_pdirm = ptdpg;
1483                 vm_page_flag_clear(ptdpg, PG_MAPPED | PG_BUSY);
1484                 ptdpg->valid = VM_PAGE_BITS_ALL;
1485                 ptdpg->wire_count = 1;
1486                 ++vmstats.v_wire_count;
1487                 pmap_kenter((vm_offset_t)pmap->pm_pml4, VM_PAGE_TO_PHYS(ptdpg));
1488         }
1489         if ((ptdpg->flags & PG_ZERO) == 0)
1490                 bzero(pmap->pm_pml4, PAGE_SIZE);
1491
1492         pmap->pm_pml4[KPML4I] = KPDPphys | PG_RW | PG_V | PG_U;
1493         pmap->pm_pml4[DMPML4I] = DMPDPphys | PG_RW | PG_V | PG_U;
1494
1495         /* install self-referential address mapping entry */
1496         pmap->pm_pml4[PML4PML4I] = VM_PAGE_TO_PHYS(ptdpg) | PG_V | PG_RW | PG_A | PG_M;
1497
1498         pmap->pm_count = 1;
1499         pmap->pm_active = 0;
1500         pmap->pm_ptphint = NULL;
1501         TAILQ_INIT(&pmap->pm_pvlist);
1502         bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1503         pmap->pm_stats.resident_count = 1;
1504 }
1505
1506 /*
1507  * Clean up a pmap structure so it can be physically freed.  This routine
1508  * is called by the vmspace dtor function.  A great deal of pmap data is
1509  * left passively mapped to improve vmspace management so we have a bit
1510  * of cleanup work to do here.
1511  */
1512 void
1513 pmap_puninit(pmap_t pmap)
1514 READY1
1515 {
1516         vm_page_t p;
1517
1518         KKASSERT(pmap->pm_active == 0);
1519         if ((p = pmap->pm_pdirm) != NULL) {
1520                 KKASSERT(pmap->pm_pml4 != NULL);
1521                 KKASSERT(pmap->pm_pml4 != (PTOV_OFFSET + KPML4phys));
1522                 pmap_kremove((vm_offset_t)pmap->pm_pml4);
1523                 p->wire_count--;
1524                 vmstats.v_wire_count--;
1525                 KKASSERT((p->flags & PG_BUSY) == 0);
1526                 vm_page_busy(p);
1527                 vm_page_free_zero(p);
1528                 pmap->pm_pdirm = NULL;
1529         }
1530         if (pmap->pm_pml4) {
1531                 KKASSERT(pmap->pm_pml4 != (PTOV_OFFSET + KPML4phys));
1532                 kmem_free(&kernel_map, (vm_offset_t)pmap->pm_pml4, PAGE_SIZE);
1533                 pmap->pm_pml4 = NULL;
1534         }
1535         if (pmap->pm_pteobj) {
1536                 vm_object_deallocate(pmap->pm_pteobj);
1537                 pmap->pm_pteobj = NULL;
1538         }
1539 }
1540
1541 /*
1542  * Wire in kernel global address entries.  To avoid a race condition
1543  * between pmap initialization and pmap_growkernel, this procedure
1544  * adds the pmap to the master list (which growkernel scans to update),
1545  * then copies the template.
1546  */
1547 void
1548 pmap_pinit2(struct pmap *pmap)
1549 READY0
1550 {
1551         crit_enter();
1552         TAILQ_INSERT_TAIL(&pmap_list, pmap, pm_pmnode);
1553         /* XXX copies current process, does not fill in MPPTDI */
1554 #if JGPMAP32
1555         bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * PTESIZE);
1556 #endif
1557         crit_exit();
1558 }
1559
1560 /*
1561  * Attempt to release and free a vm_page in a pmap.  Returns 1 on success,
1562  * 0 on failure (if the procedure had to sleep).
1563  *
1564  * When asked to remove the page directory page itself, we actually just
1565  * leave it cached so we do not have to incur the SMP inval overhead of
1566  * removing the kernel mapping.  pmap_puninit() will take care of it.
1567  */
1568 static int
1569 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1570 READY1
1571 {
1572         pml4_entry_t *pml4 = pmap->pm_pml4;
1573         /*
1574          * This code optimizes the case of freeing non-busy
1575          * page-table pages.  Those pages are zero now, and
1576          * might as well be placed directly into the zero queue.
1577          */
1578         if (vm_page_sleep_busy(p, FALSE, "pmaprl"))
1579                 return 0;
1580
1581         vm_page_busy(p);
1582
1583         /*
1584          * Remove the page table page from the processes address space.
1585          */
1586         if (p->pindex >= (NUPDE + NUPDPE) && p->pindex != (NUPDE + NUPDPE + PML4PML4I)) {
1587                 /*
1588                  * We are a PDP page.
1589                  * We look for the PML4 entry that points to us.
1590                  */
1591                 vm_page_t m4 = vm_page_lookup(pmap->pm_pteobj, NUPDE + NUPDPE + PML4PML4I);
1592                 KKASSERT(m4 != NULL);
1593                 pml4_entry_t *pml4 = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m4));
1594                 int idx = (p->pindex - (NUPDE + NUPDPE)) % NPML4EPG;
1595                 KKASSERT(pml4[idx] != 0);
1596                 pml4[idx] = 0;
1597                 m4->hold_count--;
1598                 /* JG What about wire_count? */
1599         } else if (p->pindex >= NUPDE) {
1600                 /*
1601                  * We are a PD page.
1602                  * We look for the PDP entry that points to us.
1603                  */
1604                 vm_page_t m3 = vm_page_lookup(pmap->pm_pteobj, NUPDE + NUPDPE + (p->pindex - NUPDE) / NPDPEPG);
1605                 KKASSERT(m3 != NULL);
1606                 pdp_entry_t *pdp = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m3));
1607                 int idx = (p->pindex - NUPDE) % NPDPEPG;
1608                 KKASSERT(pdp[idx] != 0);
1609                 pdp[idx] = 0;
1610                 m3->hold_count--;
1611                 /* JG What about wire_count? */
1612         } else {
1613                 /* We are a PT page.
1614                  * We look for the PD entry that points to us.
1615                  */
1616                 vm_page_t m2 = vm_page_lookup(pmap->pm_pteobj, NUPDE + p->pindex / NPDEPG);
1617                 KKASSERT(m2 != NULL);
1618                 pd_entry_t *pd = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m2));
1619                 int idx = p->pindex % NPDEPG;
1620                 pd[idx] = 0;
1621                 m2->hold_count--;
1622                 /* JG What about wire_count? */
1623         }
1624         KKASSERT(pmap->pm_stats.resident_count > 0);
1625         --pmap->pm_stats.resident_count;
1626
1627         if (p->hold_count)  {
1628                 panic("pmap_release: freeing held page table page");
1629         }
1630         if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1631                 pmap->pm_ptphint = NULL;
1632
1633         /*
1634          * We leave the top-level page table page cached, wired, and mapped in
1635          * the pmap until the dtor function (pmap_puninit()) gets called.
1636          * However, still clean it up so we can set PG_ZERO.
1637          */
1638         if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1639                 bzero(pmap->pm_pml4, PAGE_SIZE);
1640                 vm_page_flag_set(p, PG_ZERO);
1641                 vm_page_wakeup(p);
1642         } else {
1643                 p->wire_count--;
1644                 vmstats.v_wire_count--;
1645                 /* JG eventually revert to using vm_page_free_zero() */
1646                 vm_page_free(p);
1647         }
1648         return 1;
1649 }
1650
1651 /*
1652  * this routine is called if the page table page is not
1653  * mapped correctly.
1654  */
1655 static vm_page_t
1656 _pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex)
1657 READY1
1658 {
1659         vm_page_t m, pdppg, pdpg;
1660
1661         /*
1662          * Find or fabricate a new pagetable page
1663          */
1664         m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1665                         VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1666
1667
1668         if ((m->flags & PG_ZERO) == 0) {
1669                 pmap_zero_page(VM_PAGE_TO_PHYS(m));
1670         }
1671
1672         KASSERT(m->queue == PQ_NONE,
1673                 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1674
1675         /*
1676          * Increment the hold count for the page we will be returning to
1677          * the caller.
1678          */
1679         m->hold_count++;
1680
1681         /*
1682          * It is possible that someone else got in and mapped by the page
1683          * directory page while we were blocked, if so just unbusy and
1684          * return the held page.
1685          */
1686 #if JGPMAP32
1687         if ((ptepa = pmap->pm_pdir[ptepindex]) != 0) {
1688                 KKASSERT((ptepa & PG_FRAME) == VM_PAGE_TO_PHYS(m));
1689                 vm_page_wakeup(m);
1690                 return(m);
1691         }
1692 #endif
1693
1694         if (m->wire_count == 0)
1695                 vmstats.v_wire_count++;
1696         m->wire_count++;
1697
1698
1699         /*
1700          * Map the pagetable page into the process address space, if
1701          * it isn't already there.
1702          */
1703
1704         ++pmap->pm_stats.resident_count;
1705
1706 #if JGPMAP32
1707         ptepa = VM_PAGE_TO_PHYS(m);
1708         pmap->pm_pdir[ptepindex] =
1709                 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_A | PG_M);
1710 #endif
1711         if (ptepindex >= (NUPDE + NUPDPE)) {
1712                 pml4_entry_t *pml4;
1713                 vm_pindex_t pml4index;
1714
1715                 /* Wire up a new PDP page */
1716                 pml4index = ptepindex - (NUPDE + NUPDPE);
1717                 pml4 = &pmap->pm_pml4[pml4index];
1718                 *pml4 = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
1719
1720         } else if (ptepindex >= NUPDE) {
1721                 vm_pindex_t pml4index;
1722                 vm_pindex_t pdpindex;
1723                 pml4_entry_t *pml4;
1724                 pdp_entry_t *pdp;
1725
1726                 /* Wire up a new PD page */
1727                 pdpindex = ptepindex - NUPDE;
1728                 pml4index = pdpindex >> NPML4EPGSHIFT;
1729
1730                 pml4 = &pmap->pm_pml4[pml4index];
1731                 if ((*pml4 & PG_V) == 0) {
1732                         /* Have to allocate a new PDP page, recurse */
1733                         if (_pmap_allocpte(pmap, NUPDE + NUPDPE + pml4index)
1734                              == NULL) {
1735                                 --m->wire_count;
1736                                 vm_page_free(m);
1737                                 return (NULL);
1738                         }
1739                 } else {
1740                         /* Add reference to the PDP page */
1741                         pdppg = PHYS_TO_VM_PAGE(*pml4 & PG_FRAME);
1742                         pdppg->hold_count++;
1743                 }
1744                 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
1745
1746                 /* Now find the pdp page */
1747                 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1748                 KKASSERT(*pdp == 0);    /* JG DEBUG64 */
1749                 *pdp = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
1750
1751         } else {
1752                 vm_pindex_t pml4index;
1753                 vm_pindex_t pdpindex;
1754                 pml4_entry_t *pml4;
1755                 pdp_entry_t *pdp;
1756                 pd_entry_t *pd;
1757
1758                 /* Wire up a new PT page */
1759                 pdpindex = ptepindex >> NPDPEPGSHIFT;
1760                 pml4index = pdpindex >> NPML4EPGSHIFT;
1761
1762                 /* First, find the pdp and check that its valid. */
1763                 pml4 = &pmap->pm_pml4[pml4index];
1764                 if ((*pml4 & PG_V) == 0) {
1765                         /* We miss a PDP page. We ultimately need a PD page.
1766                          * Recursively allocating a PD page will allocate
1767                          * the missing PDP page and will also allocate
1768                          * the PD page we need.
1769                          */
1770                         /* Have to allocate a new PD page, recurse */
1771                         if (_pmap_allocpte(pmap, NUPDE + pdpindex)
1772                              == NULL) {
1773                                 --m->wire_count;
1774                                 vm_page_free(m);
1775                                 return (NULL);
1776                         }
1777                         pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
1778                         pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1779                 } else {
1780                         pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
1781                         pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1782                         if ((*pdp & PG_V) == 0) {
1783                                 /* Have to allocate a new PD page, recurse */
1784                                 if (_pmap_allocpte(pmap, NUPDE + pdpindex)
1785                                      == NULL) {
1786                                         --m->wire_count;
1787                                         vm_page_free(m);
1788                                         return (NULL);
1789                                 }
1790                         } else {
1791                                 /* Add reference to the PD page */
1792                                 pdpg = PHYS_TO_VM_PAGE(*pdp & PG_FRAME);
1793                                 pdpg->hold_count++;
1794                         }
1795                 }
1796                 pd = (pd_entry_t *)PHYS_TO_DMAP(*pdp & PG_FRAME);
1797
1798                 /* Now we know where the page directory page is */
1799                 pd = &pd[ptepindex & ((1ul << NPDEPGSHIFT) - 1)];
1800                 KKASSERT(*pd == 0);     /* JG DEBUG64 */
1801                 *pd = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
1802         }
1803
1804
1805         /*
1806          * Set the page table hint
1807          */
1808         pmap->pm_ptphint = m;
1809
1810         m->valid = VM_PAGE_BITS_ALL;
1811         vm_page_flag_clear(m, PG_ZERO);
1812         vm_page_flag_set(m, PG_MAPPED);
1813         vm_page_wakeup(m);
1814
1815         return m;
1816 }
1817
1818 static vm_page_t
1819 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1820 READY1
1821 {
1822         vm_pindex_t ptepindex;
1823         pd_entry_t *pd;
1824         vm_page_t m;
1825
1826         /*
1827          * Calculate pagetable page index
1828          */
1829         ptepindex = pmap_pde_pindex(va);
1830
1831         /*
1832          * Get the page directory entry
1833          */
1834         pd = pmap_pde(pmap, va);
1835
1836         /*
1837          * This supports switching from a 2MB page to a
1838          * normal 4K page.
1839          */
1840         if (pd != NULL && (*pd & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
1841                 panic("no promotion/demotion yet");
1842                 *pd = 0;
1843                 pd = NULL;
1844                 cpu_invltlb();
1845                 smp_invltlb();
1846         }
1847
1848         /*
1849          * If the page table page is mapped, we just increment the
1850          * hold count, and activate it.
1851          */
1852         if (pd != NULL && (*pd & PG_V) != 0) {
1853                 /* YYY hint is used here on i386 */
1854                 m = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1855                 pmap->pm_ptphint = m;
1856                 m->hold_count++;
1857                 return m;
1858         }
1859         /*
1860          * Here if the pte page isn't mapped, or if it has been deallocated.
1861          */
1862         return _pmap_allocpte(pmap, ptepindex);
1863 }
1864
1865
1866 /***************************************************
1867  * Pmap allocation/deallocation routines.
1868  ***************************************************/
1869
1870 /*
1871  * Release any resources held by the given physical map.
1872  * Called when a pmap initialized by pmap_pinit is being released.
1873  * Should only be called if the map contains no valid mappings.
1874  */
1875 static int pmap_release_callback(struct vm_page *p, void *data);
1876
1877 void
1878 pmap_release(struct pmap *pmap)
1879 READY1
1880 {
1881         vm_object_t object = pmap->pm_pteobj;
1882         struct rb_vm_page_scan_info info;
1883
1884         KASSERT(pmap->pm_active == 0, ("pmap still active! %08x", pmap->pm_active));
1885 #if defined(DIAGNOSTIC)
1886         if (object->ref_count != 1)
1887                 panic("pmap_release: pteobj reference count != 1");
1888 #endif
1889         
1890         info.pmap = pmap;
1891         info.object = object;
1892         crit_enter();
1893         TAILQ_REMOVE(&pmap_list, pmap, pm_pmnode);
1894         crit_exit();
1895
1896         do {
1897                 crit_enter();
1898                 info.error = 0;
1899                 info.mpte = NULL;
1900                 info.limit = object->generation;
1901
1902                 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL, 
1903                                         pmap_release_callback, &info);
1904                 if (info.error == 0 && info.mpte) {
1905                         if (!pmap_release_free_page(pmap, info.mpte))
1906                                 info.error = 1;
1907                 }
1908                 crit_exit();
1909         } while (info.error);
1910 }
1911
1912 static int
1913 pmap_release_callback(struct vm_page *p, void *data)
1914 READY1
1915 {
1916         struct rb_vm_page_scan_info *info = data;
1917
1918         if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1919                 info->mpte = p;
1920                 return(0);
1921         }
1922         if (!pmap_release_free_page(info->pmap, p)) {
1923                 info->error = 1;
1924                 return(-1);
1925         }
1926         if (info->object->generation != info->limit) {
1927                 info->error = 1;
1928                 return(-1);
1929         }
1930         return(0);
1931 }
1932
1933 /*
1934  * Grow the number of kernel page table entries, if needed.
1935  */
1936
1937 void
1938 pmap_growkernel(vm_offset_t addr)
1939 READY1
1940 {
1941         vm_paddr_t paddr;
1942         struct pmap *pmap;
1943         vm_offset_t ptppaddr;
1944         vm_page_t nkpg;
1945         pd_entry_t *pde, newpdir;
1946         pdp_entry_t newpdp;
1947
1948         crit_enter();
1949         if (kernel_vm_end == 0) {
1950                 kernel_vm_end = KERNBASE;
1951                 nkpt = 0;
1952                 while ((*pmap_pde(&kernel_pmap, kernel_vm_end) & PG_V) != 0) {
1953                         kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1954                         nkpt++;
1955                         if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1956                                 kernel_vm_end = kernel_map.max_offset;
1957                                 break;                       
1958                         }
1959                 }
1960         }
1961         addr = roundup2(addr, PAGE_SIZE * NPTEPG);
1962         if (addr - 1 >= kernel_map.max_offset)
1963                 addr = kernel_map.max_offset;
1964         while (kernel_vm_end < addr) {
1965                 pde = pmap_pde(&kernel_pmap, kernel_vm_end);
1966                 if (pde == NULL) {
1967                         /* We need a new PDP entry */
1968                         nkpg = vm_page_alloc(kptobj, nkpt,
1969                                              VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM
1970                                              | VM_ALLOC_INTERRUPT);
1971                         if (nkpg == NULL)
1972                                 panic("pmap_growkernel: no memory to grow kernel");
1973                         if ((nkpg->flags & PG_ZERO) == 0)
1974                                 pmap_zero_page(nkpg);
1975                         paddr = VM_PAGE_TO_PHYS(nkpg);
1976                         newpdp = (pdp_entry_t)
1977                                 (paddr | PG_V | PG_RW | PG_A | PG_M);
1978                         *pmap_pdpe(&kernel_pmap, kernel_vm_end) = newpdp;
1979                         continue; /* try again */
1980                 }
1981                 if ((*pde & PG_V) != 0) {
1982                         kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1983                         if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1984                                 kernel_vm_end = kernel_map.max_offset;
1985                                 break;                       
1986                         }
1987                         continue;
1988                 }
1989
1990                 /*
1991                  * This index is bogus, but out of the way
1992                  */
1993                 nkpg = vm_page_alloc(kptobj, nkpt,
1994                         VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM | VM_ALLOC_INTERRUPT);
1995                 if (nkpg == NULL)
1996                         panic("pmap_growkernel: no memory to grow kernel");
1997
1998                 vm_page_wire(nkpg);
1999                 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
2000                 pmap_zero_page(ptppaddr);
2001                 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
2002                 *pmap_pde(&kernel_pmap, kernel_vm_end) = newpdir;
2003                 nkpt++;
2004
2005                 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
2006                 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
2007                         kernel_vm_end = kernel_map.max_offset;
2008                         break;                       
2009                 }
2010         }
2011         crit_exit();
2012 }
2013
2014 /*
2015  *      Retire the given physical map from service.
2016  *      Should only be called if the map contains
2017  *      no valid mappings.
2018  */
2019 void
2020 pmap_destroy(pmap_t pmap)
2021 READY0
2022 {
2023         int count;
2024
2025         if (pmap == NULL)
2026                 return;
2027
2028         count = --pmap->pm_count;
2029         if (count == 0) {
2030                 pmap_release(pmap);
2031                 panic("destroying a pmap is not yet implemented");
2032         }
2033 }
2034
2035 /*
2036  *      Add a reference to the specified pmap.
2037  */
2038 void
2039 pmap_reference(pmap_t pmap)
2040 READY2
2041 {
2042         if (pmap != NULL) {
2043                 pmap->pm_count++;
2044         }
2045 }
2046
2047 /***************************************************
2048 * page management routines.
2049  ***************************************************/
2050
2051 /*
2052  * free the pv_entry back to the free list.  This function may be
2053  * called from an interrupt.
2054  */
2055 static PMAP_INLINE void
2056 free_pv_entry(pv_entry_t pv)
2057 READY2
2058 {
2059         pv_entry_count--;
2060         KKASSERT(pv_entry_count >= 0);
2061         zfree(pvzone, pv);
2062 }
2063
2064 /*
2065  * get a new pv_entry, allocating a block from the system
2066  * when needed.  This function may be called from an interrupt.
2067  */
2068 static pv_entry_t
2069 get_pv_entry(void)
2070 READY2
2071 {
2072         pv_entry_count++;
2073         if (pv_entry_high_water &&
2074                 (pv_entry_count > pv_entry_high_water) &&
2075                 (pmap_pagedaemon_waken == 0)) {
2076                 pmap_pagedaemon_waken = 1;
2077                 wakeup(&vm_pages_needed);
2078         }
2079         return zalloc(pvzone);
2080 }
2081
2082 /*
2083  * This routine is very drastic, but can save the system
2084  * in a pinch.
2085  */
2086 void
2087 pmap_collect(void)
2088 READY0
2089 {
2090         int i;
2091         vm_page_t m;
2092         static int warningdone=0;
2093
2094         if (pmap_pagedaemon_waken == 0)
2095                 return;
2096
2097         if (warningdone < 5) {
2098                 kprintf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
2099                 warningdone++;
2100         }
2101
2102         for(i = 0; i < vm_page_array_size; i++) {
2103                 m = &vm_page_array[i];
2104                 if (m->wire_count || m->hold_count || m->busy ||
2105                     (m->flags & PG_BUSY))
2106                         continue;
2107                 pmap_remove_all(m);
2108         }
2109         pmap_pagedaemon_waken = 0;
2110 }
2111         
2112
2113 /*
2114  * If it is the first entry on the list, it is actually
2115  * in the header and we must copy the following entry up
2116  * to the header.  Otherwise we must search the list for
2117  * the entry.  In either case we free the now unused entry.
2118  */
2119 static int
2120 pmap_remove_entry(struct pmap *pmap, vm_page_t m, 
2121                         vm_offset_t va, pmap_inval_info_t info)
2122 READY1
2123 {
2124         pv_entry_t pv;
2125         int rtval;
2126
2127         crit_enter();
2128         if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
2129                 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2130                         if (pmap == pv->pv_pmap && va == pv->pv_va) 
2131                                 break;
2132                 }
2133         } else {
2134                 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
2135                         if (va == pv->pv_va) 
2136                                 break;
2137                 }
2138         }
2139
2140         rtval = 0;
2141         /* JGXXX When can 'pv' be NULL? */
2142         if (pv) {
2143                 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2144                 m->md.pv_list_count--;
2145                 KKASSERT(m->md.pv_list_count >= 0);
2146                 if (TAILQ_EMPTY(&m->md.pv_list))
2147                         vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2148                 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
2149                 ++pmap->pm_generation;
2150                 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem, info);
2151                 free_pv_entry(pv);
2152         }
2153         crit_exit();
2154         return rtval;
2155 }
2156
2157 /*
2158  * Create a pv entry for page at pa for
2159  * (pmap, va).
2160  */
2161 static void
2162 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
2163 READY1
2164 {
2165         pv_entry_t pv;
2166
2167         crit_enter();
2168         pv = get_pv_entry();
2169         pv->pv_va = va;
2170         pv->pv_pmap = pmap;
2171         pv->pv_ptem = mpte;
2172
2173         TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
2174         TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2175         m->md.pv_list_count++;
2176
2177         crit_exit();
2178 }
2179
2180 /*
2181  * pmap_remove_pte: do the things to unmap a page in a process
2182  */
2183 static int
2184 pmap_remove_pte(struct pmap *pmap, pt_entry_t *ptq, vm_offset_t va,
2185         pmap_inval_info_t info)
2186 READY1
2187 {
2188         pt_entry_t oldpte;
2189         vm_page_t m;
2190
2191         pmap_inval_add(info, pmap, va);
2192         oldpte = pte_load_clear(ptq);
2193         if (oldpte & PG_W)
2194                 pmap->pm_stats.wired_count -= 1;
2195         /*
2196          * Machines that don't support invlpg, also don't support
2197          * PG_G.  XXX PG_G is disabled for SMP so don't worry about
2198          * the SMP case.
2199          */
2200         if (oldpte & PG_G)
2201                 cpu_invlpg((void *)va);
2202         KKASSERT(pmap->pm_stats.resident_count > 0);
2203         --pmap->pm_stats.resident_count;
2204         if (oldpte & PG_MANAGED) {
2205                 m = PHYS_TO_VM_PAGE(oldpte);
2206                 if (oldpte & PG_M) {
2207 #if defined(PMAP_DIAGNOSTIC)
2208                         if (pmap_nw_modified((pt_entry_t) oldpte)) {
2209                                 kprintf(
2210         "pmap_remove: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2211                                     va, oldpte);
2212                         }
2213 #endif
2214                         if (pmap_track_modified(va))
2215                                 vm_page_dirty(m);
2216                 }
2217                 if (oldpte & PG_A)
2218                         vm_page_flag_set(m, PG_REFERENCED);
2219                 return pmap_remove_entry(pmap, m, va, info);
2220         } else {
2221                 return pmap_unuse_pt(pmap, va, NULL, info);
2222         }
2223
2224         return 0;
2225 }
2226
2227 /*
2228  * pmap_remove_page:
2229  *
2230  *      Remove a single page from a process address space.
2231  *
2232  *      This function may not be called from an interrupt if the pmap is
2233  *      not kernel_pmap.
2234  */
2235 static void
2236 pmap_remove_page(struct pmap *pmap, vm_offset_t va, pmap_inval_info_t info)
2237 READY1
2238 {
2239         pt_entry_t *pte;
2240
2241         pte = pmap_pte(pmap, va);
2242         if (pte == NULL)
2243                 return;
2244         if ((*pte & PG_V) == 0)
2245                 return;
2246         pmap_remove_pte(pmap, pte, va, info);
2247 }
2248
2249 /*
2250  * pmap_remove:
2251  *
2252  *      Remove the given range of addresses from the specified map.
2253  *
2254  *      It is assumed that the start and end are properly
2255  *      rounded to the page size.
2256  *
2257  *      This function may not be called from an interrupt if the pmap is
2258  *      not kernel_pmap.
2259  */
2260 void
2261 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
2262 READY1
2263 {
2264         vm_offset_t va_next;
2265         pml4_entry_t *pml4e;
2266         pdp_entry_t *pdpe;
2267         pd_entry_t ptpaddr, *pde;
2268         pt_entry_t *pte;
2269         struct pmap_inval_info info;
2270
2271         if (pmap == NULL)
2272                 return;
2273
2274         if (pmap->pm_stats.resident_count == 0)
2275                 return;
2276
2277         pmap_inval_init(&info);
2278
2279         /*
2280          * special handling of removing one page.  a very
2281          * common operation and easy to short circuit some
2282          * code.
2283          */
2284         if (sva + PAGE_SIZE == eva) {
2285                 pde = pmap_pde(pmap, sva);
2286                 if (pde && (*pde & PG_PS) == 0) {
2287                         pmap_remove_page(pmap, sva, &info);
2288                         pmap_inval_flush(&info);
2289                         return;
2290                 }
2291         }
2292
2293         for (; sva < eva; sva = va_next) {
2294                 pml4e = pmap_pml4e(pmap, sva);
2295                 if ((*pml4e & PG_V) == 0) {
2296                         va_next = (sva + NBPML4) & ~PML4MASK;
2297                         if (va_next < sva)
2298                                 va_next = eva;
2299                         continue;
2300                 }
2301
2302                 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
2303                 if ((*pdpe & PG_V) == 0) {
2304                         va_next = (sva + NBPDP) & ~PDPMASK;
2305                         if (va_next < sva)
2306                                 va_next = eva;
2307                         continue;
2308                 }
2309
2310                 /*
2311                  * Calculate index for next page table.
2312                  */
2313                 va_next = (sva + NBPDR) & ~PDRMASK;
2314                 if (va_next < sva)
2315                         va_next = eva;
2316
2317                 pde = pmap_pdpe_to_pde(pdpe, sva);
2318                 ptpaddr = *pde;
2319
2320                 /*
2321                  * Weed out invalid mappings.
2322                  */
2323                 if (ptpaddr == 0)
2324                         continue;
2325
2326                 /*
2327                  * Check for large page.
2328                  */
2329                 if ((ptpaddr & PG_PS) != 0) {
2330                         /* JG FreeBSD has more complex treatment here */
2331                         pmap_inval_add(&info, pmap, -1);
2332                         *pde = 0;
2333                         pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2334                         continue;
2335                 }
2336
2337                 /*
2338                  * Limit our scan to either the end of the va represented
2339                  * by the current page table page, or to the end of the
2340                  * range being removed.
2341                  */
2342                 if (va_next > eva)
2343                         va_next = eva;
2344
2345                 /*
2346                  * NOTE: pmap_remove_pte() can block.
2347                  */
2348                 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2349                     sva += PAGE_SIZE) {
2350                         if (*pte == 0)
2351                                 continue;
2352                         if (pmap_remove_pte(pmap, pte, sva, &info))
2353                                 break;
2354                 }
2355         }
2356         pmap_inval_flush(&info);
2357 }
2358
2359 /*
2360  * pmap_remove_all:
2361  *
2362  *      Removes this physical page from all physical maps in which it resides.
2363  *      Reflects back modify bits to the pager.
2364  *
2365  *      This routine may not be called from an interrupt.
2366  */
2367
2368 static void
2369 pmap_remove_all(vm_page_t m)
2370 READY1
2371 {
2372         struct pmap_inval_info info;
2373         pt_entry_t *pte, tpte;
2374         pv_entry_t pv;
2375
2376         if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2377                 return;
2378
2379         pmap_inval_init(&info);
2380         crit_enter();
2381         while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2382                 KKASSERT(pv->pv_pmap->pm_stats.resident_count > 0);
2383                 --pv->pv_pmap->pm_stats.resident_count;
2384
2385                 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2386                 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
2387                 tpte = pte_load_clear(pte);
2388
2389                 if (tpte & PG_W)
2390                         pv->pv_pmap->pm_stats.wired_count--;
2391
2392                 if (tpte & PG_A)
2393                         vm_page_flag_set(m, PG_REFERENCED);
2394
2395                 /*
2396                  * Update the vm_page_t clean and reference bits.
2397                  */
2398                 if (tpte & PG_M) {
2399 #if defined(PMAP_DIAGNOSTIC)
2400                         if (pmap_nw_modified(tpte)) {
2401                                 kprintf(
2402         "pmap_remove_all: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2403                                     pv->pv_va, tpte);
2404                         }
2405 #endif
2406                         if (pmap_track_modified(pv->pv_va))
2407                                 vm_page_dirty(m);
2408                 }
2409                 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2410                 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2411                 ++pv->pv_pmap->pm_generation;
2412                 m->md.pv_list_count--;
2413                 KKASSERT(m->md.pv_list_count >= 0);
2414                 if (TAILQ_EMPTY(&m->md.pv_list))
2415                         vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2416                 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem, &info);
2417                 free_pv_entry(pv);
2418         }
2419         crit_exit();
2420         KKASSERT((m->flags & (PG_MAPPED|PG_WRITEABLE)) == 0);
2421         pmap_inval_flush(&info);
2422 }
2423
2424 /*
2425  * pmap_protect:
2426  *
2427  *      Set the physical protection on the specified range of this map
2428  *      as requested.
2429  *
2430  *      This function may not be called from an interrupt if the map is
2431  *      not the kernel_pmap.
2432  */
2433 void
2434 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
2435 READY1
2436 {
2437         vm_offset_t va_next;
2438         pml4_entry_t *pml4e;
2439         pdp_entry_t *pdpe;
2440         pd_entry_t ptpaddr, *pde;
2441         pt_entry_t *pte;
2442         pmap_inval_info info;
2443
2444         /* JG review for NX */
2445
2446         if (pmap == NULL)
2447                 return;
2448
2449         if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
2450                 pmap_remove(pmap, sva, eva);
2451                 return;
2452         }
2453
2454         if (prot & VM_PROT_WRITE)
2455                 return;
2456
2457         pmap_inval_init(&info);
2458
2459         for (; sva < eva; sva = va_next) {
2460
2461                 pml4e = pmap_pml4e(pmap, sva);
2462                 if ((*pml4e & PG_V) == 0) {
2463                         va_next = (sva + NBPML4) & ~PML4MASK;
2464                         if (va_next < sva)
2465                                 va_next = eva;
2466                         continue;
2467                 }
2468
2469                 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
2470                 if ((*pdpe & PG_V) == 0) {
2471                         va_next = (sva + NBPDP) & ~PDPMASK;
2472                         if (va_next < sva)
2473                                 va_next = eva;
2474                         continue;
2475                 }
2476
2477                 va_next = (sva + NBPDR) & ~PDRMASK;
2478                 if (va_next < sva)
2479                         va_next = eva;
2480
2481                 pde = pmap_pdpe_to_pde(pdpe, sva);
2482                 ptpaddr = *pde;
2483
2484                 /*
2485                  * Check for large page.
2486                  */
2487                 if ((ptpaddr & PG_PS) != 0) {
2488                         pmap_inval_add(&info, pmap, -1);
2489                         *pde &= ~(PG_M|PG_RW);
2490                         pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2491                         continue;
2492                 }
2493
2494                 /*
2495                  * Weed out invalid mappings. Note: we assume that the page
2496                  * directory table is always allocated, and in kernel virtual.
2497                  */
2498                 if (ptpaddr == 0)
2499                         continue;
2500
2501                 if (va_next > eva)
2502                         va_next = eva;
2503
2504                 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2505                     sva += PAGE_SIZE) {
2506                         pt_entry_t obits, pbits;
2507                         vm_page_t m;
2508
2509                         /*
2510                          * XXX non-optimal.  Note also that there can be
2511                          * no pmap_inval_flush() calls until after we modify
2512                          * ptbase[sindex] (or otherwise we have to do another
2513                          * pmap_inval_add() call).
2514                          */
2515                         pmap_inval_add(&info, pmap, sva);
2516                         obits = pbits = *pte;
2517                         if ((pbits & PG_V) == 0)
2518                                 continue;
2519                         if (pbits & PG_MANAGED) {
2520                                 m = NULL;
2521                                 if (pbits & PG_A) {
2522                                         m = PHYS_TO_VM_PAGE(pbits & PG_FRAME);
2523                                         vm_page_flag_set(m, PG_REFERENCED);
2524                                         pbits &= ~PG_A;
2525                                 }
2526                                 if (pbits & PG_M) {
2527                                         if (pmap_track_modified(sva)) {
2528                                                 if (m == NULL)
2529                                                         m = PHYS_TO_VM_PAGE(pbits & PG_FRAME);
2530                                                 vm_page_dirty(m);
2531                                                 pbits &= ~PG_M;
2532                                         }
2533                                 }
2534                         }
2535
2536                         pbits &= ~PG_RW;
2537
2538                         if (pbits != obits) {
2539                                 *pte = pbits;
2540                         }
2541                 }
2542         }
2543         pmap_inval_flush(&info);
2544 }
2545
2546 /*
2547  *      Insert the given physical page (p) at
2548  *      the specified virtual address (v) in the
2549  *      target physical map with the protection requested.
2550  *
2551  *      If specified, the page will be wired down, meaning
2552  *      that the related pte can not be reclaimed.
2553  *
2554  *      NB:  This is the only routine which MAY NOT lazy-evaluate
2555  *      or lose information.  That is, this routine must actually
2556  *      insert this page into the given map NOW.
2557  */
2558 void
2559 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
2560            boolean_t wired)
2561 READY1
2562 {
2563         vm_paddr_t pa;
2564         pd_entry_t *pde;
2565         pt_entry_t *pte;
2566         vm_paddr_t opa;
2567         pt_entry_t origpte, newpte;
2568         vm_page_t mpte;
2569         pmap_inval_info info;
2570
2571         if (pmap == NULL)
2572                 return;
2573
2574         va = trunc_page(va);
2575 #ifdef PMAP_DIAGNOSTIC
2576         if (va >= KvaEnd)
2577                 panic("pmap_enter: toobig");
2578         if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
2579                 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%lx)", va);
2580 #endif
2581         if (va < UPT_MAX_ADDRESS && pmap == &kernel_pmap) {
2582                 kprintf("Warning: pmap_enter called on UVA with kernel_pmap\n");
2583 #ifdef DDB
2584                 db_print_backtrace();
2585 #endif
2586         }
2587         if (va >= UPT_MAX_ADDRESS && pmap != &kernel_pmap) {
2588                 kprintf("Warning: pmap_enter called on KVA without kernel_pmap\n");
2589 #ifdef DDB
2590                 db_print_backtrace();
2591 #endif
2592         }
2593
2594         /*
2595          * In the case that a page table page is not
2596          * resident, we are creating it here.
2597          */
2598         if (va < VM_MAX_USER_ADDRESS)
2599                 mpte = pmap_allocpte(pmap, va);
2600         else
2601                 mpte = NULL;
2602
2603         pmap_inval_init(&info);
2604         pde = pmap_pde(pmap, va);
2605         if (pde != NULL && (*pde & PG_V) != 0) {
2606                 if ((*pde & PG_PS) != 0)
2607                         panic("pmap_enter: attempted pmap_enter on 2MB page");
2608                 pte = pmap_pde_to_pte(pde, va);
2609         } else
2610                 panic("pmap_enter: invalid page directory va=%#lx", va);
2611
2612         KKASSERT(pte != NULL);
2613         pa = VM_PAGE_TO_PHYS(m);
2614         origpte = *pte;
2615         opa = origpte & PG_FRAME;
2616
2617         /*
2618          * Mapping has not changed, must be protection or wiring change.
2619          */
2620         if (origpte && (opa == pa)) {
2621                 /*
2622                  * Wiring change, just update stats. We don't worry about
2623                  * wiring PT pages as they remain resident as long as there
2624                  * are valid mappings in them. Hence, if a user page is wired,
2625                  * the PT page will be also.
2626                  */
2627                 if (wired && ((origpte & PG_W) == 0))
2628                         pmap->pm_stats.wired_count++;
2629                 else if (!wired && (origpte & PG_W))
2630                         pmap->pm_stats.wired_count--;
2631
2632 #if defined(PMAP_DIAGNOSTIC)
2633                 if (pmap_nw_modified(origpte)) {
2634                         kprintf(
2635         "pmap_enter: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2636                             va, origpte);
2637                 }
2638 #endif
2639
2640                 /*
2641                  * Remove the extra pte reference.  Note that we cannot
2642                  * optimize the RO->RW case because we have adjusted the
2643                  * wiring count above and may need to adjust the wiring
2644                  * bits below.
2645                  */
2646                 if (mpte)
2647                         mpte->hold_count--;
2648
2649                 /*
2650                  * We might be turning off write access to the page,
2651                  * so we go ahead and sense modify status.
2652                  */
2653                 if (origpte & PG_MANAGED) {
2654                         if ((origpte & PG_M) && pmap_track_modified(va)) {
2655                                 vm_page_t om;
2656                                 om = PHYS_TO_VM_PAGE(opa);
2657                                 vm_page_dirty(om);
2658                         }
2659                         pa |= PG_MANAGED;
2660                         KKASSERT(m->flags & PG_MAPPED);
2661                 }
2662                 goto validate;
2663         } 
2664         /*
2665          * Mapping has changed, invalidate old range and fall through to
2666          * handle validating new mapping.
2667          */
2668         if (opa) {
2669                 int err;
2670                 err = pmap_remove_pte(pmap, pte, va, &info);
2671                 if (err)
2672                         panic("pmap_enter: pte vanished, va: 0x%lx", va);
2673         }
2674
2675         /*
2676          * Enter on the PV list if part of our managed memory. Note that we
2677          * raise IPL while manipulating pv_table since pmap_enter can be
2678          * called at interrupt time.
2679          */
2680         if (pmap_initialized && 
2681             (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2682                 pmap_insert_entry(pmap, va, mpte, m);
2683                 pa |= PG_MANAGED;
2684                 vm_page_flag_set(m, PG_MAPPED);
2685         }
2686
2687         /*
2688          * Increment counters
2689          */
2690         ++pmap->pm_stats.resident_count;
2691         if (wired)
2692                 pmap->pm_stats.wired_count++;
2693
2694 validate:
2695         /*
2696          * Now validate mapping with desired protection/wiring.
2697          */
2698         newpte = (pt_entry_t) (pa | pte_prot(pmap, prot) | PG_V);
2699
2700         if (wired)
2701                 newpte |= PG_W;
2702         if (va < VM_MAX_USER_ADDRESS)
2703                 newpte |= PG_U;
2704         if (pmap == &kernel_pmap)
2705                 newpte |= pgeflag;
2706
2707         /*
2708          * if the mapping or permission bits are different, we need
2709          * to update the pte.
2710          */
2711         if ((origpte & ~(PG_M|PG_A)) != newpte) {
2712                 pmap_inval_add(&info, pmap, va);
2713                 *pte = newpte | PG_A;
2714                 if (newpte & PG_RW)
2715                         vm_page_flag_set(m, PG_WRITEABLE);
2716         }
2717         KKASSERT((newpte & PG_MANAGED) == 0 || (m->flags & PG_MAPPED));
2718         pmap_inval_flush(&info);
2719 }
2720
2721 /*
2722  * This code works like pmap_enter() but assumes VM_PROT_READ and not-wired.
2723  * This code also assumes that the pmap has no pre-existing entry for this
2724  * VA.
2725  *
2726  * This code currently may only be used on user pmaps, not kernel_pmap.
2727  */
2728 static void
2729 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m)
2730 READY1
2731 {
2732         pt_entry_t *pte;
2733         vm_paddr_t pa;
2734         vm_page_t mpte;
2735         vm_pindex_t ptepindex;
2736         pd_entry_t *ptepa;
2737         pmap_inval_info info;
2738
2739         pmap_inval_init(&info);
2740
2741         if (va < UPT_MAX_ADDRESS && pmap == &kernel_pmap) {
2742                 kprintf("Warning: pmap_enter_quick called on UVA with kernel_pmap\n");
2743 #ifdef DDB
2744                 db_print_backtrace();
2745 #endif
2746         }
2747         if (va >= UPT_MAX_ADDRESS && pmap != &kernel_pmap) {
2748                 kprintf("Warning: pmap_enter_quick called on KVA without kernel_pmap\n");
2749 #ifdef DDB
2750                 db_print_backtrace();
2751 #endif
2752         }
2753
2754         KKASSERT(va < UPT_MIN_ADDRESS); /* assert used on user pmaps only */
2755
2756         /*
2757          * Calculate the page table page (mpte), allocating it if necessary.
2758          *
2759          * A held page table page (mpte), or NULL, is passed onto the
2760          * section following.
2761          */
2762         if (va < VM_MAX_USER_ADDRESS) {
2763                 /*
2764                  * Calculate pagetable page index
2765                  */
2766                 ptepindex = pmap_pde_pindex(va);
2767
2768                 do {
2769                         /*
2770                          * Get the page directory entry
2771                          */
2772                         ptepa = pmap_pde(pmap, va);
2773
2774                         /*
2775                          * If the page table page is mapped, we just increment
2776                          * the hold count, and activate it.
2777                          */
2778                         if (ptepa && (*ptepa & PG_V) != 0) {
2779                                 if (*ptepa & PG_PS)
2780                                         panic("pmap_enter_quick: unexpected mapping into 2MB page");
2781 //                              if (pmap->pm_ptphint &&
2782 //                                  (pmap->pm_ptphint->pindex == ptepindex)) {
2783 //                                      mpte = pmap->pm_ptphint;
2784 //                              } else {
2785                                         mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
2786                                         pmap->pm_ptphint = mpte;
2787 //                              }
2788                                 if (mpte)
2789                                         mpte->hold_count++;
2790                         } else {
2791                                 mpte = _pmap_allocpte(pmap, ptepindex);
2792                         }
2793                 } while (mpte == NULL);
2794         } else {
2795                 mpte = NULL;
2796                 /* this code path is not yet used */
2797         }
2798
2799         /*
2800          * With a valid (and held) page directory page, we can just use
2801          * vtopte() to get to the pte.  If the pte is already present
2802          * we do not disturb it.
2803          */
2804         pte = vtopte(va);
2805         if (*pte & PG_V) {
2806                 if (mpte)
2807                         pmap_unwire_pte_hold(pmap, va, mpte, &info);
2808                 pa = VM_PAGE_TO_PHYS(m);
2809                 KKASSERT(((*pte ^ pa) & PG_FRAME) == 0);
2810                 return;
2811         }
2812
2813         /*
2814          * Enter on the PV list if part of our managed memory
2815          */
2816         if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2817                 pmap_insert_entry(pmap, va, mpte, m);
2818                 vm_page_flag_set(m, PG_MAPPED);
2819         }
2820
2821         /*
2822          * Increment counters
2823          */
2824         ++pmap->pm_stats.resident_count;
2825
2826         pa = VM_PAGE_TO_PHYS(m);
2827
2828         /*
2829          * Now validate mapping with RO protection
2830          */
2831         if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2832                 *pte = pa | PG_V | PG_U;
2833         else
2834                 *pte = pa | PG_V | PG_U | PG_MANAGED;
2835 /*      pmap_inval_add(&info, pmap, va); shouldn't be needed inval->valid */
2836         pmap_inval_flush(&info);
2837 }
2838
2839 /*
2840  * Make a temporary mapping for a physical address.  This is only intended
2841  * to be used for panic dumps.
2842  */
2843 /* JG Needed on amd64? */
2844 void *
2845 pmap_kenter_temporary(vm_paddr_t pa, int i)
2846 READY2
2847 {
2848         pmap_kenter((vm_offset_t)crashdumpmap + (i * PAGE_SIZE), pa);
2849         return ((void *)crashdumpmap);
2850 }
2851
2852 #define MAX_INIT_PT (96)
2853
2854 /*
2855  * This routine preloads the ptes for a given object into the specified pmap.
2856  * This eliminates the blast of soft faults on process startup and
2857  * immediately after an mmap.
2858  */
2859 static int pmap_object_init_pt_callback(vm_page_t p, void *data);
2860
2861 void
2862 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2863                     vm_object_t object, vm_pindex_t pindex, 
2864                     vm_size_t size, int limit)
2865 READY1
2866 {
2867         struct rb_vm_page_scan_info info;
2868         struct lwp *lp;
2869         vm_size_t psize;
2870
2871         /*
2872          * We can't preinit if read access isn't set or there is no pmap
2873          * or object.
2874          */
2875         if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2876                 return;
2877
2878         /*
2879          * We can't preinit if the pmap is not the current pmap
2880          */
2881         lp = curthread->td_lwp;
2882         if (lp == NULL || pmap != vmspace_pmap(lp->lwp_vmspace))
2883                 return;
2884
2885         psize = amd64_btop(size);
2886
2887         if ((object->type != OBJT_VNODE) ||
2888                 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2889                         (object->resident_page_count > MAX_INIT_PT))) {
2890                 return;
2891         }
2892
2893         if (psize + pindex > object->size) {
2894                 if (object->size < pindex)
2895                         return;           
2896                 psize = object->size - pindex;
2897         }
2898
2899         if (psize == 0)
2900                 return;
2901
2902         /*
2903          * Use a red-black scan to traverse the requested range and load
2904          * any valid pages found into the pmap.
2905          *
2906          * We cannot safely scan the object's memq unless we are in a
2907          * critical section since interrupts can remove pages from objects.
2908          */
2909         info.start_pindex = pindex;
2910         info.end_pindex = pindex + psize - 1;
2911         info.limit = limit;
2912         info.mpte = NULL;
2913         info.addr = addr;
2914         info.pmap = pmap;
2915
2916         crit_enter();
2917         vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2918                                 pmap_object_init_pt_callback, &info);
2919         crit_exit();
2920 }
2921
2922 static
2923 int
2924 pmap_object_init_pt_callback(vm_page_t p, void *data)
2925 READY1
2926 {
2927         struct rb_vm_page_scan_info *info = data;
2928         vm_pindex_t rel_index;
2929         /*
2930          * don't allow an madvise to blow away our really
2931          * free pages allocating pv entries.
2932          */
2933         if ((info->limit & MAP_PREFAULT_MADVISE) &&
2934                 vmstats.v_free_count < vmstats.v_free_reserved) {
2935                     return(-1);
2936         }
2937         if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2938             (p->busy == 0) && (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2939                 if ((p->queue - p->pc) == PQ_CACHE)
2940                         vm_page_deactivate(p);
2941                 vm_page_busy(p);
2942                 rel_index = p->pindex - info->start_pindex;
2943                 pmap_enter_quick(info->pmap,
2944                                  info->addr + amd64_ptob(rel_index), p);
2945                 vm_page_wakeup(p);
2946         }
2947         return(0);
2948 }
2949
2950 /*
2951  * pmap_prefault provides a quick way of clustering pagefaults into a
2952  * processes address space.  It is a "cousin" of pmap_object_init_pt, 
2953  * except it runs at page fault time instead of mmap time.
2954  */
2955 #define PFBAK 4
2956 #define PFFOR 4
2957 #define PAGEORDER_SIZE (PFBAK+PFFOR)
2958
2959 static int pmap_prefault_pageorder[] = {
2960         -PAGE_SIZE, PAGE_SIZE,
2961         -2 * PAGE_SIZE, 2 * PAGE_SIZE,
2962         -3 * PAGE_SIZE, 3 * PAGE_SIZE,
2963         -4 * PAGE_SIZE, 4 * PAGE_SIZE
2964 };
2965
2966 void
2967 pmap_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry)
2968 READY0
2969 {
2970         int i;
2971         vm_offset_t starta;
2972         vm_offset_t addr;
2973         vm_pindex_t pindex;
2974         vm_page_t m;
2975         vm_object_t object;
2976         struct lwp *lp;
2977
2978         /*
2979          * We do not currently prefault mappings that use virtual page
2980          * tables.  We do not prefault foreign pmaps.
2981          */
2982         if (entry->maptype == VM_MAPTYPE_VPAGETABLE)
2983                 return;
2984         lp = curthread->td_lwp;
2985         if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
2986                 return;
2987
2988         object = entry->object.vm_object;
2989
2990         starta = addra - PFBAK * PAGE_SIZE;
2991         if (starta < entry->start)
2992                 starta = entry->start;
2993         else if (starta > addra)
2994                 starta = 0;
2995
2996         /*
2997          * critical section protection is required to maintain the 
2998          * page/object association, interrupts can free pages and remove 
2999          * them from their objects.
3000          */
3001         crit_enter();
3002         for (i = 0; i < PAGEORDER_SIZE; i++) {
3003                 vm_object_t lobject;
3004                 pt_entry_t *pte;
3005
3006                 addr = addra + pmap_prefault_pageorder[i];
3007                 if (addr > addra + (PFFOR * PAGE_SIZE))
3008                         addr = 0;
3009
3010                 if (addr < starta || addr >= entry->end)
3011                         continue;
3012
3013                 if ((*pmap_pde(pmap, addr)) == 0)
3014                         continue;
3015
3016                 pte = vtopte(addr);
3017                 if (*pte)
3018                         continue;
3019
3020                 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
3021                 lobject = object;
3022
3023                 for (m = vm_page_lookup(lobject, pindex);
3024                     (!m && (lobject->type == OBJT_DEFAULT) &&
3025                      (lobject->backing_object));
3026                     lobject = lobject->backing_object
3027                 ) {
3028                         if (lobject->backing_object_offset & PAGE_MASK)
3029                                 break;
3030                         pindex += (lobject->backing_object_offset >> PAGE_SHIFT);
3031                         m = vm_page_lookup(lobject->backing_object, pindex);
3032                 }
3033
3034                 /*
3035                  * give-up when a page is not in memory
3036                  */
3037                 if (m == NULL)
3038                         break;
3039
3040                 if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
3041                         (m->busy == 0) &&
3042                     (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
3043
3044                         if ((m->queue - m->pc) == PQ_CACHE) {
3045                                 vm_page_deactivate(m);
3046                         }
3047                         vm_page_busy(m);
3048                         pmap_enter_quick(pmap, addr, m);
3049                         vm_page_wakeup(m);
3050                 }
3051         }
3052         crit_exit();
3053 }
3054
3055 /*
3056  *      Routine:        pmap_change_wiring
3057  *      Function:       Change the wiring attribute for a map/virtual-address
3058  *                      pair.
3059  *      In/out conditions:
3060  *                      The mapping must already exist in the pmap.
3061  */
3062 void
3063 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
3064 READY0
3065 {
3066         pt_entry_t *pte;
3067
3068         if (pmap == NULL)
3069                 return;
3070
3071         pte = pmap_pte(pmap, va);
3072
3073         if (wired && !pmap_pte_w(pte))
3074                 pmap->pm_stats.wired_count++;
3075         else if (!wired && pmap_pte_w(pte))
3076                 pmap->pm_stats.wired_count--;
3077
3078         /*
3079          * Wiring is not a hardware characteristic so there is no need to
3080          * invalidate TLB.  However, in an SMP environment we must use
3081          * a locked bus cycle to update the pte (if we are not using 
3082          * the pmap_inval_*() API that is)... it's ok to do this for simple
3083          * wiring changes.
3084          */
3085 #ifdef SMP
3086         if (wired)
3087                 atomic_set_int(pte, PG_W);
3088         else
3089                 atomic_clear_int(pte, PG_W);
3090 #else
3091         if (wired)
3092                 atomic_set_int_nonlocked(pte, PG_W);
3093         else
3094                 atomic_clear_int_nonlocked(pte, PG_W);
3095 #endif
3096 }
3097
3098
3099
3100 /*
3101  *      Copy the range specified by src_addr/len
3102  *      from the source map to the range dst_addr/len
3103  *      in the destination map.
3104  *
3105  *      This routine is only advisory and need not do anything.
3106  */
3107 void
3108 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, 
3109         vm_size_t len, vm_offset_t src_addr)
3110 READY0
3111 {
3112         pmap_inval_info info;
3113         vm_offset_t addr;
3114         vm_offset_t end_addr = src_addr + len;
3115         vm_offset_t pdnxt;
3116         pd_entry_t src_frame, dst_frame;
3117         vm_page_t m;
3118
3119         if (dst_addr != src_addr)
3120                 return;
3121         /*
3122          * XXX BUGGY.  Amoung other things srcmpte is assumed to remain
3123          * valid through blocking calls, and that's just not going to
3124          * be the case.
3125          *
3126          * FIXME!
3127          */
3128         return;
3129
3130 #if JGPMAP32
3131         src_frame = src_pmap->pm_pdir[PTDPTDI] & PG_FRAME;
3132         if (src_frame != (PTDpde & PG_FRAME)) {
3133                 return;
3134         }
3135
3136         dst_frame = dst_pmap->pm_pdir[PTDPTDI] & PG_FRAME;
3137         if (dst_frame != (APTDpde & PG_FRAME)) {
3138                 APTDpde = (pd_entry_t) (dst_frame | PG_RW | PG_V);
3139                 /* The page directory is not shared between CPUs */
3140                 cpu_invltlb();
3141         }
3142 #endif
3143         pmap_inval_init(&info);
3144         pmap_inval_add(&info, dst_pmap, -1);
3145         pmap_inval_add(&info, src_pmap, -1);
3146
3147         /*
3148          * critical section protection is required to maintain the page/object
3149          * association, interrupts can free pages and remove them from 
3150          * their objects.
3151          */
3152         crit_enter();
3153         for (addr = src_addr; addr < end_addr; addr = pdnxt) {
3154                 pt_entry_t *src_pte, *dst_pte;
3155                 vm_page_t dstmpte, srcmpte;
3156                 vm_offset_t srcptepaddr;
3157                 vm_pindex_t ptepindex;
3158
3159                 if (addr >= UPT_MIN_ADDRESS)
3160                         panic("pmap_copy: invalid to pmap_copy page tables\n");
3161
3162                 /*
3163                  * Don't let optional prefaulting of pages make us go
3164                  * way below the low water mark of free pages or way
3165                  * above high water mark of used pv entries.
3166                  */
3167                 if (vmstats.v_free_count < vmstats.v_free_reserved ||
3168                     pv_entry_count > pv_entry_high_water)
3169                         break;
3170                 
3171                 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
3172                 ptepindex = addr >> PDRSHIFT;
3173
3174 #if JGPMAP32
3175                 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex];
3176 #endif
3177                 if (srcptepaddr == 0)
3178                         continue;
3179                         
3180                 if (srcptepaddr & PG_PS) {
3181 #if JGPMAP32
3182                         if (dst_pmap->pm_pdir[ptepindex] == 0) {
3183                                 dst_pmap->pm_pdir[ptepindex] = (pd_entry_t) srcptepaddr;
3184                                 dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
3185                         }
3186 #endif
3187                         continue;
3188                 }
3189
3190                 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
3191                 if ((srcmpte == NULL) || (srcmpte->hold_count == 0) ||
3192                     (srcmpte->flags & PG_BUSY)) {
3193                         continue;
3194                 }
3195
3196                 if (pdnxt > end_addr)
3197                         pdnxt = end_addr;
3198
3199                 src_pte = vtopte(addr);
3200 #if JGPMAP32
3201                 dst_pte = avtopte(addr);
3202 #endif
3203                 while (addr < pdnxt) {
3204                         pt_entry_t ptetemp;
3205
3206                         ptetemp = *src_pte;
3207                         /*
3208                          * we only virtual copy managed pages
3209                          */
3210                         if ((ptetemp & PG_MANAGED) != 0) {
3211                                 /*
3212                                  * We have to check after allocpte for the
3213                                  * pte still being around...  allocpte can
3214                                  * block.
3215                                  *
3216                                  * pmap_allocpte() can block.  If we lose
3217                                  * our page directory mappings we stop.
3218                                  */
3219                                 dstmpte = pmap_allocpte(dst_pmap, addr);
3220
3221 #if JGPMAP32
3222                                 if (src_frame != (PTDpde & PG_FRAME) ||
3223                                     dst_frame != (APTDpde & PG_FRAME)
3224                                 ) {
3225                                         kprintf("WARNING: pmap_copy: detected and corrected race\n");
3226                                         pmap_unwire_pte_hold(dst_pmap, dstmpte, &info);
3227                                         goto failed;
3228                                 } else if ((*dst_pte == 0) &&
3229                                            (ptetemp = *src_pte) != 0 &&
3230                                            (ptetemp & PG_MANAGED)) {
3231                                         /*
3232                                          * Clear the modified and
3233                                          * accessed (referenced) bits
3234                                          * during the copy.
3235                                          */
3236                                         m = PHYS_TO_VM_PAGE(ptetemp);
3237                                         *dst_pte = ptetemp & ~(PG_M | PG_A);
3238                                         ++dst_pmap->pm_stats.resident_count;
3239                                         pmap_insert_entry(dst_pmap, addr,
3240                                                 dstmpte, m);
3241                                         KKASSERT(m->flags & PG_MAPPED);
3242                                 } else {
3243                                         kprintf("WARNING: pmap_copy: dst_pte race detected and corrected\n");
3244                                         pmap_unwire_pte_hold(dst_pmap, dstmpte, &info);
3245                                         goto failed;
3246                                 }
3247 #endif
3248                                 if (dstmpte->hold_count >= srcmpte->hold_count)
3249                                         break;
3250                         }
3251                         addr += PAGE_SIZE;
3252                         src_pte++;
3253                         dst_pte++;
3254                 }
3255         }
3256 failed:
3257         crit_exit();
3258         pmap_inval_flush(&info);
3259 }       
3260
3261 /*
3262  * pmap_zero_page:
3263  *
3264  *      Zero the specified physical page.
3265  *
3266  *      This function may be called from an interrupt and no locking is
3267  *      required.
3268  */
3269 void
3270 pmap_zero_page(vm_paddr_t phys)
3271 READY1
3272 {
3273         vm_offset_t va = PHYS_TO_DMAP(phys);
3274
3275         pagezero((void *)va);
3276 }
3277
3278 /*
3279  * pmap_page_assertzero:
3280  *
3281  *      Assert that a page is empty, panic if it isn't.
3282  */
3283 void
3284 pmap_page_assertzero(vm_paddr_t phys)
3285 READY1
3286 {
3287         struct mdglobaldata *gd = mdcpu;
3288         int i;
3289
3290         crit_enter();
3291         vm_offset_t virt = PHYS_TO_DMAP(phys);
3292
3293         for (i = 0; i < PAGE_SIZE; i += sizeof(int)) {
3294             if (*(int *)((char *)virt + i) != 0) {
3295                 panic("pmap_page_assertzero() @ %p not zero!\n",
3296                     (void *)virt);
3297             }
3298         }
3299         crit_exit();
3300 }
3301
3302 /*
3303  * pmap_zero_page:
3304  *
3305  *      Zero part of a physical page by mapping it into memory and clearing
3306  *      its contents with bzero.
3307  *
3308  *      off and size may not cover an area beyond a single hardware page.
3309  */
3310 void
3311 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
3312 READY1
3313 {
3314         struct mdglobaldata *gd = mdcpu;
3315
3316         crit_enter();
3317         vm_offset_t virt = PHYS_TO_DMAP(phys);
3318         bzero((char *)virt + off, size);
3319         crit_exit();
3320 }
3321
3322 /*
3323  * pmap_copy_page:
3324  *
3325  *      Copy the physical page from the source PA to the target PA.
3326  *      This function may be called from an interrupt.  No locking
3327  *      is required.
3328  */
3329 void
3330 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
3331 READY1
3332 {
3333         vm_offset_t src_virt, dst_virt;
3334
3335         crit_enter();
3336         src_virt = PHYS_TO_DMAP(src);
3337         dst_virt = PHYS_TO_DMAP(dst);
3338         bcopy(src_virt, dst_virt, PAGE_SIZE);
3339         crit_exit();
3340 }
3341
3342 /*
3343  * pmap_copy_page_frag:
3344  *
3345  *      Copy the physical page from the source PA to the target PA.
3346  *      This function may be called from an interrupt.  No locking
3347  *      is required.
3348  */
3349 void
3350 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
3351 READY1
3352 {
3353         vm_offset_t src_virt, dst_virt;
3354
3355         crit_enter();
3356         src_virt = PHYS_TO_DMAP(src);
3357         dst_virt = PHYS_TO_DMAP(dst);
3358         bcopy((char *)src_virt + (src & PAGE_MASK),
3359               (char *)dst_virt + (dst & PAGE_MASK),
3360               bytes);
3361         crit_exit();
3362 }
3363
3364 /*
3365  * Returns true if the pmap's pv is one of the first
3366  * 16 pvs linked to from this page.  This count may
3367  * be changed upwards or downwards in the future; it
3368  * is only necessary that true be returned for a small
3369  * subset of pmaps for proper page aging.
3370  */
3371 boolean_t
3372 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
3373 READY2
3374 {
3375         pv_entry_t pv;
3376         int loops = 0;
3377
3378         if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3379                 return FALSE;
3380
3381         crit_enter();
3382
3383         TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3384                 if (pv->pv_pmap == pmap) {
3385                         crit_exit();
3386                         return TRUE;
3387                 }
3388                 loops++;
3389                 if (loops >= 16)
3390                         break;
3391         }
3392         crit_exit();
3393         return (FALSE);
3394 }
3395
3396 /*
3397  * Remove all pages from specified address space
3398  * this aids process exit speeds.  Also, this code
3399  * is special cased for current process only, but
3400  * can have the more generic (and slightly slower)
3401  * mode enabled.  This is much faster than pmap_remove
3402  * in the case of running down an entire address space.
3403  */
3404 void
3405 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
3406 READY1
3407 {
3408         struct lwp *lp;
3409         pt_entry_t *pte, tpte;
3410         pv_entry_t pv, npv;
3411         vm_page_t m;
3412         pmap_inval_info info;
3413         int iscurrentpmap;
3414         int save_generation;
3415
3416         lp = curthread->td_lwp;
3417         if (lp && pmap == vmspace_pmap(lp->lwp_vmspace))
3418                 iscurrentpmap = 1;
3419         else
3420                 iscurrentpmap = 0;
3421
3422         pmap_inval_init(&info);
3423         crit_enter();
3424         for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
3425                 if (pv->pv_va >= eva || pv->pv_va < sva) {
3426                         npv = TAILQ_NEXT(pv, pv_plist);
3427                         continue;
3428                 }
3429
3430                 KKASSERT(pmap == pv->pv_pmap);
3431
3432                 if (iscurrentpmap)
3433                         pte = vtopte(pv->pv_va);
3434                 else
3435                         pte = pmap_pte_quick(pmap, pv->pv_va);
3436                 if (pmap->pm_active)
3437                         pmap_inval_add(&info, pmap, pv->pv_va);
3438
3439                 /*
3440                  * We cannot remove wired pages from a process' mapping
3441                  * at this time
3442                  */
3443                 if (*pte & PG_W) {
3444                         npv = TAILQ_NEXT(pv, pv_plist);
3445                         continue;
3446                 }
3447                 tpte = pte_load_clear(pte);
3448
3449                 m = PHYS_TO_VM_PAGE(tpte & PG_FRAME);
3450
3451                 KASSERT(m < &vm_page_array[vm_page_array_size],
3452                         ("pmap_remove_pages: bad tpte %lx", tpte));
3453
3454                 KKASSERT(pmap->pm_stats.resident_count > 0);
3455                 --pmap->pm_stats.resident_count;
3456
3457                 /*
3458                  * Update the vm_page_t clean and reference bits.
3459                  */
3460                 if (tpte & PG_M) {
3461                         vm_page_dirty(m);
3462                 }
3463
3464                 npv = TAILQ_NEXT(pv, pv_plist);
3465                 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
3466                 save_generation = ++pmap->pm_generation;
3467
3468                 m->md.pv_list_count--;
3469                 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3470                 if (TAILQ_EMPTY(&m->md.pv_list))
3471                         vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
3472
3473                 pmap_unuse_pt(pmap, pv->pv_va, pv->pv_ptem, &info);
3474                 free_pv_entry(pv);
3475
3476                 /*
3477                  * Restart the scan if we blocked during the unuse or free
3478                  * calls and other removals were made.
3479                  */
3480                 if (save_generation != pmap->pm_generation) {
3481                         kprintf("Warning: pmap_remove_pages race-A avoided\n");
3482                         pv = TAILQ_FIRST(&pmap->pm_pvlist);
3483                 }
3484         }
3485         pmap_inval_flush(&info);
3486         crit_exit();
3487 }
3488
3489 /*
3490  * pmap_testbit tests bits in pte's
3491  * note that the testbit/clearbit routines are inline,
3492  * and a lot of things compile-time evaluate.
3493  */
3494 static boolean_t
3495 pmap_testbit(vm_page_t m, int bit)
3496 READY1
3497 {
3498         pv_entry_t pv;
3499         pt_entry_t *pte;
3500
3501         if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3502                 return FALSE;
3503
3504         if (TAILQ_FIRST(&m->md.pv_list) == NULL)
3505                 return FALSE;
3506
3507         crit_enter();
3508
3509         TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3510                 /*
3511                  * if the bit being tested is the modified bit, then
3512                  * mark clean_map and ptes as never
3513                  * modified.
3514                  */
3515                 if (bit & (PG_A|PG_M)) {
3516                         if (!pmap_track_modified(pv->pv_va))
3517                                 continue;
3518                 }
3519
3520 #if defined(PMAP_DIAGNOSTIC)
3521                 if (pv->pv_pmap == NULL) {
3522                         kprintf("Null pmap (tb) at va: 0x%lx\n", pv->pv_va);
3523                         continue;
3524                 }
3525 #endif
3526                 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3527                 if (*pte & bit) {
3528                         crit_exit();
3529                         return TRUE;
3530                 }
3531         }
3532         crit_exit();
3533         return (FALSE);
3534 }
3535
3536 /*
3537  * this routine is used to modify bits in ptes
3538  */
3539 static __inline void
3540 pmap_clearbit(vm_page_t m, int bit)
3541 READY1
3542 {
3543         struct pmap_inval_info info;
3544         pv_entry_t pv;
3545         pt_entry_t *pte;
3546         pt_entry_t pbits;
3547
3548         if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3549                 return;
3550
3551         pmap_inval_init(&info);
3552         crit_enter();
3553
3554         /*
3555          * Loop over all current mappings setting/clearing as appropos If
3556          * setting RO do we need to clear the VAC?
3557          */
3558         TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3559                 /*
3560                  * don't write protect pager mappings
3561                  */
3562                 if (bit == PG_RW) {
3563                         if (!pmap_track_modified(pv->pv_va))
3564                                 continue;
3565                 }
3566
3567 #if defined(PMAP_DIAGNOSTIC)
3568                 if (pv->pv_pmap == NULL) {
3569                         kprintf("Null pmap (cb) at va: 0x%lx\n", pv->pv_va);
3570                         continue;
3571                 }
3572 #endif
3573
3574                 /*
3575                  * Careful here.  We can use a locked bus instruction to
3576                  * clear PG_A or PG_M safely but we need to synchronize
3577                  * with the target cpus when we mess with PG_RW.
3578                  *
3579                  * We do not have to force synchronization when clearing
3580                  * PG_M even for PTEs generated via virtual memory maps,
3581                  * because the virtual kernel will invalidate the pmap
3582                  * entry when/if it needs to resynchronize the Modify bit.
3583                  */
3584                 if (bit & PG_RW)
3585                         pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
3586                 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3587 again:
3588                 pbits = *pte;
3589                 if (pbits & bit) {
3590                         if (bit == PG_RW) {
3591                                 if (pbits & PG_M) {
3592                                         vm_page_dirty(m);
3593                                         atomic_clear_long(pte, PG_M|PG_RW);
3594                                 } else {
3595                                         /*
3596                                          * The cpu may be trying to set PG_M
3597                                          * simultaniously with our clearing
3598                                          * of PG_RW.
3599                                          */
3600                                         if (!atomic_cmpset_long(pte, pbits,
3601                                                                pbits & ~PG_RW))
3602                                                 goto again;
3603                                 }
3604                         } else if (bit == PG_M) {
3605                                 /*
3606                                  * We could also clear PG_RW here to force
3607                                  * a fault on write to redetect PG_M for
3608                                  * virtual kernels, but it isn't necessary
3609                                  * since virtual kernels invalidate the pte 
3610                                  * when they clear the VPTE_M bit in their
3611                                  * virtual page tables.
3612                                  */
3613                                 atomic_clear_long(pte, PG_M);
3614                         } else {
3615                                 atomic_clear_long(pte, bit);
3616                         }
3617                 }
3618         }
3619         pmap_inval_flush(&info);
3620         crit_exit();
3621 }
3622
3623 /*
3624  *      pmap_page_protect:
3625  *
3626  *      Lower the permission for all mappings to a given page.
3627  */
3628 void
3629 pmap_page_protect(vm_page_t m, vm_prot_t prot)
3630 READY1
3631 {
3632         /* JG NX support? */
3633         if ((prot & VM_PROT_WRITE) == 0) {
3634                 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
3635                         pmap_clearbit(m, PG_RW);
3636                         vm_page_flag_clear(m, PG_WRITEABLE);
3637                 } else {
3638                         pmap_remove_all(m);
3639                 }
3640         }
3641 }
3642
3643 vm_paddr_t
3644 pmap_phys_address(vm_pindex_t ppn)
3645 READY2
3646 {
3647         return (amd64_ptob(ppn));
3648 }
3649
3650 /*
3651  *      pmap_ts_referenced:
3652  *
3653  *      Return a count of reference bits for a page, clearing those bits.
3654  *      It is not necessary for every reference bit to be cleared, but it
3655  *      is necessary that 0 only be returned when there are truly no
3656  *      reference bits set.
3657  *
3658  *      XXX: The exact number of bits to check and clear is a matter that
3659  *      should be tested and standardized at some point in the future for
3660  *      optimal aging of shared pages.
3661  */
3662 int
3663 pmap_ts_referenced(vm_page_t m)
3664 READY1
3665 {
3666         pv_entry_t pv, pvf, pvn;
3667         pt_entry_t *pte;
3668         int rtval = 0;
3669
3670         if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3671                 return (rtval);
3672
3673         crit_enter();
3674
3675         if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3676
3677                 pvf = pv;
3678
3679                 do {
3680                         pvn = TAILQ_NEXT(pv, pv_list);
3681
3682                         TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3683
3684                         TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3685
3686                         if (!pmap_track_modified(pv->pv_va))
3687                                 continue;
3688
3689                         pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3690
3691                         if (pte && (*pte & PG_A)) {
3692 #ifdef SMP
3693                                 atomic_clear_long(pte, PG_A);
3694 #else
3695                                 atomic_clear_long_nonlocked(pte, PG_A);
3696 #endif
3697                                 rtval++;
3698                                 if (rtval > 4) {
3699                                         break;
3700                                 }
3701                         }
3702                 } while ((pv = pvn) != NULL && pv != pvf);
3703         }
3704         crit_exit();
3705
3706         return (rtval);
3707 }
3708
3709 /*
3710  *      pmap_is_modified:
3711  *
3712  *      Return whether or not the specified physical page was modified
3713  *      in any physical maps.
3714  */
3715 boolean_t
3716 pmap_is_modified(vm_page_t m)
3717 READY2
3718 {
3719         return pmap_testbit(m, PG_M);
3720 }
3721
3722 /*
3723  *      Clear the modify bits on the specified physical page.
3724  */
3725 void
3726 pmap_clear_modify(vm_page_t m)
3727 READY2
3728 {
3729         pmap_clearbit(m, PG_M);
3730 }
3731
3732 /*
3733  *      pmap_clear_reference:
3734  *
3735  *      Clear the reference bit on the specified physical page.
3736  */
3737 void
3738 pmap_clear_reference(vm_page_t m)
3739 READY2
3740 {
3741         pmap_clearbit(m, PG_A);
3742 }
3743
3744 /*
3745  * Miscellaneous support routines follow
3746  */
3747
3748 static void
3749 i386_protection_init(void)
3750 READY0
3751 {
3752         int *kp, prot;
3753
3754         /* JG NX support may go here; No VM_PROT_EXECUTE ==> set NX bit  */
3755         kp = protection_codes;
3756         for (prot = 0; prot < 8; prot++) {
3757                 switch (prot) {
3758                 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE:
3759                         /*
3760                          * Read access is also 0. There isn't any execute bit,
3761                          * so just make it readable.
3762                          */
3763                 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE:
3764                 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE:
3765                 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE:
3766                         *kp++ = 0;
3767                         break;
3768                 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE:
3769                 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE:
3770                 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE:
3771                 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
3772                         *kp++ = PG_RW;
3773                         break;
3774                 }
3775         }
3776 }
3777
3778 /*
3779  * Map a set of physical memory pages into the kernel virtual
3780  * address space. Return a pointer to where it is mapped. This
3781  * routine is intended to be used for mapping device memory,
3782  * NOT real memory.
3783  *
3784  * NOTE: we can't use pgeflag unless we invalidate the pages one at
3785  * a time.
3786  */
3787 void *
3788 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
3789 READY1
3790 {
3791         vm_offset_t va, tmpva, offset;
3792         pt_entry_t *pte;
3793
3794         offset = pa & PAGE_MASK;
3795         size = roundup(offset + size, PAGE_SIZE);
3796
3797         va = kmem_alloc_nofault(&kernel_map, size);
3798         if (va == 0)
3799                 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3800
3801         pa = pa & ~PAGE_MASK;
3802         for (tmpva = va; size > 0;) {
3803                 pte = vtopte(tmpva);
3804                 *pte = pa | PG_RW | PG_V; /* | pgeflag; */
3805                 size -= PAGE_SIZE;
3806                 tmpva += PAGE_SIZE;
3807                 pa += PAGE_SIZE;
3808         }
3809         cpu_invltlb();
3810         smp_invltlb();
3811
3812         return ((void *)(va + offset));
3813 }
3814
3815 void
3816 pmap_unmapdev(vm_offset_t va, vm_size_t size)
3817 READY1
3818 {
3819         vm_offset_t base, offset;
3820
3821         base = va & ~PAGE_MASK;
3822         offset = va & PAGE_MASK;
3823         size = roundup(offset + size, PAGE_SIZE);
3824         pmap_qremove(va, size >> PAGE_SHIFT);
3825         kmem_free(&kernel_map, base, size);
3826 }
3827
3828 /*
3829  * perform the pmap work for mincore
3830  */
3831 int
3832 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3833 READY0
3834 {
3835         pt_entry_t *ptep, pte;
3836         vm_page_t m;
3837         int val = 0;
3838         
3839         ptep = pmap_pte(pmap, addr);
3840         if (ptep == 0) {
3841                 return 0;
3842         }
3843
3844         if ((pte = *ptep) != 0) {
3845                 vm_offset_t pa;
3846
3847                 val = MINCORE_INCORE;
3848                 if ((pte & PG_MANAGED) == 0)
3849                         return val;
3850
3851                 pa = pte & PG_FRAME;
3852
3853                 m = PHYS_TO_VM_PAGE(pa);
3854
3855                 /*
3856                  * Modified by us
3857                  */
3858                 if (pte & PG_M)
3859                         val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3860                 /*
3861                  * Modified by someone
3862                  */
3863                 else if (m->dirty || pmap_is_modified(m))
3864                         val |= MINCORE_MODIFIED_OTHER;
3865                 /*
3866                  * Referenced by us
3867                  */
3868                 if (pte & PG_A)
3869                         val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3870
3871                 /*
3872                  * Referenced by someone
3873                  */
3874                 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3875                         val |= MINCORE_REFERENCED_OTHER;
3876                         vm_page_flag_set(m, PG_REFERENCED);
3877                 }
3878         } 
3879         return val;
3880 }
3881
3882 /*
3883  * Replace p->p_vmspace with a new one.  If adjrefs is non-zero the new
3884  * vmspace will be ref'd and the old one will be deref'd.
3885  *
3886  * The vmspace for all lwps associated with the process will be adjusted
3887  * and cr3 will be reloaded if any lwp is the current lwp.
3888  */
3889 void
3890 pmap_replacevm(struct proc *p, struct vmspace *newvm, int adjrefs)
3891 READY2
3892 {
3893         struct vmspace *oldvm;
3894         struct lwp *lp;
3895
3896         crit_enter();
3897         oldvm = p->p_vmspace;
3898         if (oldvm != newvm) {
3899                 p->p_vmspace = newvm;
3900                 KKASSERT(p->p_nthreads == 1);
3901                 lp = RB_ROOT(&p->p_lwp_tree);
3902                 pmap_setlwpvm(lp, newvm);
3903                 if (adjrefs) {
3904                         sysref_get(&newvm->vm_sysref);
3905                         sysref_put(&oldvm->vm_sysref);
3906                 }
3907         }
3908         crit_exit();
3909 }
3910
3911 /*
3912  * Set the vmspace for a LWP.  The vmspace is almost universally set the
3913  * same as the process vmspace, but virtual kernels need to swap out contexts
3914  * on a per-lwp basis.
3915  */
3916 void
3917 pmap_setlwpvm(struct lwp *lp, struct vmspace *newvm)
3918 READY1
3919 {
3920         struct vmspace *oldvm;
3921         struct pmap *pmap;
3922
3923         crit_enter();
3924         oldvm = lp->lwp_vmspace;
3925
3926         if (oldvm != newvm) {
3927                 lp->lwp_vmspace = newvm;
3928                 if (curthread->td_lwp == lp) {
3929                         pmap = vmspace_pmap(newvm);
3930 #if defined(SMP)
3931                         atomic_set_int(&pmap->pm_active, 1 << mycpu->gd_cpuid);
3932 #else
3933                         pmap->pm_active |= 1;
3934 #endif
3935 #if defined(SWTCH_OPTIM_STATS)
3936                         tlb_flush_count++;
3937 #endif
3938                         curthread->td_pcb->pcb_cr3 = vtophys(pmap->pm_pml4);
3939                         load_cr3(curthread->td_pcb->pcb_cr3);
3940                         pmap = vmspace_pmap(oldvm);
3941 #if defined(SMP)
3942                         atomic_clear_int(&pmap->pm_active,
3943                                           1 << mycpu->gd_cpuid);
3944 #else
3945                         pmap->pm_active &= ~1;
3946 #endif
3947                 }
3948         }
3949         crit_exit();
3950 }
3951
3952 vm_offset_t
3953 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3954 READY0
3955 {
3956
3957         if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3958                 return addr;
3959         }
3960
3961         addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3962         return addr;
3963 }
3964
3965
3966 #if defined(DEBUG)
3967
3968 static void     pads (pmap_t pm);
3969 void            pmap_pvdump (vm_paddr_t pa);
3970
3971 /* print address space of pmap*/
3972 static void
3973 pads(pmap_t pm)
3974 READY0
3975 {
3976         vm_offset_t va;
3977         unsigned i, j;
3978         pt_entry_t *ptep;
3979
3980         if (pm == &kernel_pmap)
3981                 return;
3982         crit_enter();
3983         for (i = 0; i < NPDEPG; i++) {
3984 #if JGPMAP32
3985                 if (pm->pm_pdir[i]) {
3986                         for (j = 0; j < NPTEPG; j++) {
3987                                 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
3988                                 if (pm == &kernel_pmap && va < KERNBASE)
3989                                         continue;
3990                                 if (pm != &kernel_pmap && va > UPT_MAX_ADDRESS)
3991                                         continue;
3992                                 ptep = pmap_pte_quick(pm, va);
3993                                 if (pmap_pte_v(ptep))
3994                                         kprintf("%lx:%lx ", va, *ptep);
3995                         };
3996                 }
3997 #endif
3998         }
3999         crit_exit();
4000
4001 }
4002
4003 void
4004 pmap_pvdump(vm_paddr_t pa)
4005 READY0
4006 {
4007         pv_entry_t pv;
4008         vm_page_t m;
4009
4010         kprintf("pa %08llx", (long long)pa);
4011         m = PHYS_TO_VM_PAGE(pa);
4012         TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
4013 #ifdef used_to_be
4014                 kprintf(" -> pmap %p, va %x, flags %x",
4015                     (void *)pv->pv_pmap, pv->pv_va, pv->pv_flags);
4016 #endif
4017                 kprintf(" -> pmap %p, va %x", (void *)pv->pv_pmap, pv->pv_va);
4018                 pads(pv->pv_pmap);
4019         }
4020         kprintf(" ");
4021 }
4022 #endif