amd64: Fix UP kernel build.
[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 #if JGPMAP32
799         /* local apic is mapped on last page */
800         SMPpt[NPTEPG - 1] = (pt_entry_t)(PG_V | PG_RW | PG_N | pgeflag |
801             (cpu_apic_address & PG_FRAME));
802 #endif
803 #endif
804
805         /*
806          * We need to finish setting up the globaldata page for the BSP.
807          * locore has already populated the page table for the mdglobaldata
808          * portion.
809          */
810         pg = MDGLOBALDATA_BASEALLOC_PAGES;
811         gd = &CPU_prvspace[0].mdglobaldata;
812         gd->gd_CMAP1 = &SMPpt[pg + 0];
813         gd->gd_CMAP2 = &SMPpt[pg + 1];
814         gd->gd_CMAP3 = &SMPpt[pg + 2];
815         gd->gd_PMAP1 = &SMPpt[pg + 3];
816         gd->gd_CADDR1 = CPU_prvspace[0].CPAGE1;
817         gd->gd_CADDR2 = CPU_prvspace[0].CPAGE2;
818         gd->gd_CADDR3 = CPU_prvspace[0].CPAGE3;
819         gd->gd_PADDR1 = (pt_entry_t *)CPU_prvspace[0].PPAGE1;
820
821         cpu_invltlb();
822 }
823
824 #ifdef SMP
825 /*
826  * Set 4mb pdir for mp startup
827  */
828 void
829 pmap_set_opt(void)
830 READY0
831 {
832         if (pseflag && (cpu_feature & CPUID_PSE)) {
833                 load_cr4(rcr4() | CR4_PSE);
834                 if (pdir4mb && mycpu->gd_cpuid == 0) {  /* only on BSP */
835 #if JGPMAP32
836                         kernel_pmap.pm_pdir[KPTDI] =
837                             PTD[KPTDI] = (pd_entry_t)pdir4mb;
838 #endif
839                         cpu_invltlb();
840                 }
841         }
842 }
843 #endif
844
845 /*
846  *      Initialize the pmap module.
847  *      Called by vm_init, to initialize any structures that the pmap
848  *      system needs to map virtual memory.
849  *      pmap_init has been enhanced to support in a fairly consistant
850  *      way, discontiguous physical memory.
851  */
852 void
853 pmap_init(void)
854 READY0
855 {
856         int i;
857         int initial_pvs;
858
859         /*
860          * object for kernel page table pages
861          */
862         /* JG I think the number can be arbitrary */
863         kptobj = vm_object_allocate(OBJT_DEFAULT, 5);
864
865         /*
866          * Allocate memory for random pmap data structures.  Includes the
867          * pv_head_table.
868          */
869
870         for(i = 0; i < vm_page_array_size; i++) {
871                 vm_page_t m;
872
873                 m = &vm_page_array[i];
874                 TAILQ_INIT(&m->md.pv_list);
875                 m->md.pv_list_count = 0;
876         }
877
878         /*
879          * init the pv free list
880          */
881         initial_pvs = vm_page_array_size;
882         if (initial_pvs < MINPV)
883                 initial_pvs = MINPV;
884         pvzone = &pvzone_store;
885         pvinit = (struct pv_entry *) kmem_alloc(&kernel_map,
886                 initial_pvs * sizeof (struct pv_entry));
887         zbootinit(pvzone, "PV ENTRY", sizeof (struct pv_entry), pvinit,
888                 initial_pvs);
889
890         /*
891          * Now it is safe to enable pv_table recording.
892          */
893         pmap_initialized = TRUE;
894 #ifdef SMP
895         lapic = pmap_mapdev_uncacheable(cpu_apic_address, sizeof(struct LAPIC));
896 #endif
897 }
898
899 /*
900  * Initialize the address space (zone) for the pv_entries.  Set a
901  * high water mark so that the system can recover from excessive
902  * numbers of pv entries.
903  */
904 void
905 pmap_init2(void)
906 READY0
907 {
908         int shpgperproc = PMAP_SHPGPERPROC;
909
910         TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
911         pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
912         TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
913         pv_entry_high_water = 9 * (pv_entry_max / 10);
914         zinitna(pvzone, &pvzone_obj, NULL, 0, pv_entry_max, ZONE_INTERRUPT, 1);
915 }
916
917
918 /***************************************************
919  * Low level helper routines.....
920  ***************************************************/
921
922 #if defined(PMAP_DIAGNOSTIC)
923
924 /*
925  * This code checks for non-writeable/modified pages.
926  * This should be an invalid condition.
927  */
928 static int
929 pmap_nw_modified(pt_entry_t pte)
930 READY1
931 {
932         if ((pte & (PG_M|PG_RW)) == PG_M)
933                 return 1;
934         else
935                 return 0;
936 }
937 #endif
938
939
940 /*
941  * this routine defines the region(s) of memory that should
942  * not be tested for the modified bit.
943  */
944 static PMAP_INLINE int
945 pmap_track_modified(vm_offset_t va)
946 READY0
947 {
948         if ((va < clean_sva) || (va >= clean_eva)) 
949                 return 1;
950         else
951                 return 0;
952 }
953
954 /*
955  * pmap_extract:
956  *
957  *      Extract the physical page address associated with the map/VA pair.
958  *
959  *      This function may not be called from an interrupt if the pmap is
960  *      not kernel_pmap.
961  */
962 vm_paddr_t 
963 pmap_extract(pmap_t pmap, vm_offset_t va)
964 READY1
965 {
966         vm_paddr_t rtval;
967         pt_entry_t *pte;
968         pd_entry_t pde, *pdep;
969
970         rtval = 0;
971         pdep = pmap_pde(pmap, va);
972         if (pdep != NULL) {
973                 pde = *pdep;
974                 if (pde) {
975                         if ((pde & PG_PS) != 0) {
976                                 rtval = (pde & PG_PS_FRAME) | (va & PDRMASK);
977                         } else {
978                                 pte = pmap_pde_to_pte(pdep, va);
979                                 rtval = (*pte & PG_FRAME) | (va & PAGE_MASK);
980                         }
981                 }
982         }
983         return rtval;
984 }
985
986 /*
987  *      Routine:        pmap_kextract
988  *      Function:
989  *              Extract the physical page address associated
990  *              kernel virtual address.
991  */
992 vm_paddr_t
993 pmap_kextract(vm_offset_t va)
994 READY1
995 {
996         pd_entry_t pde;
997         vm_paddr_t pa;
998
999         if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS) {
1000                 pa = DMAP_TO_PHYS(va);
1001         } else {
1002                 pde = *vtopde(va);
1003                 if (pde & PG_PS) {
1004                         pa = (pde & PG_PS_FRAME) | (va & PDRMASK);
1005                 } else {
1006                         /*
1007                          * Beware of a concurrent promotion that changes the
1008                          * PDE at this point!  For example, vtopte() must not
1009                          * be used to access the PTE because it would use the
1010                          * new PDE.  It is, however, safe to use the old PDE
1011                          * because the page table page is preserved by the
1012                          * promotion.
1013                          */
1014                         pa = *pmap_pde_to_pte(&pde, va);
1015                         pa = (pa & PG_FRAME) | (va & PAGE_MASK);
1016                 }
1017         }
1018         return pa;
1019 }
1020
1021 /***************************************************
1022  * Low level mapping routines.....
1023  ***************************************************/
1024
1025 /*
1026  * Routine: pmap_kenter
1027  * Function:
1028  *      Add a wired page to the KVA
1029  *      NOTE! note that in order for the mapping to take effect -- you
1030  *      should do an invltlb after doing the pmap_kenter().
1031  */
1032 void 
1033 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
1034 READY1
1035 {
1036         pt_entry_t *pte;
1037         pt_entry_t npte;
1038         pmap_inval_info info;
1039
1040         pmap_inval_init(&info);
1041         npte = pa | PG_RW | PG_V | pgeflag;
1042         pte = vtopte(va);
1043         pmap_inval_add(&info, &kernel_pmap, va);
1044         *pte = npte;
1045         pmap_inval_flush(&info);
1046 }
1047
1048 /*
1049  * Routine: pmap_kenter_quick
1050  * Function:
1051  *      Similar to pmap_kenter(), except we only invalidate the
1052  *      mapping on the current CPU.
1053  */
1054 void
1055 pmap_kenter_quick(vm_offset_t va, vm_paddr_t pa)
1056 READY1
1057 {
1058         pt_entry_t *pte;
1059         pt_entry_t npte;
1060
1061         npte = pa | PG_RW | PG_V | pgeflag;
1062         pte = vtopte(va);
1063         *pte = npte;
1064         cpu_invlpg((void *)va);
1065 }
1066
1067 void
1068 pmap_kenter_sync(vm_offset_t va)
1069 READY1
1070 {
1071         pmap_inval_info info;
1072
1073         pmap_inval_init(&info);
1074         pmap_inval_add(&info, &kernel_pmap, va);
1075         pmap_inval_flush(&info);
1076 }
1077
1078 void
1079 pmap_kenter_sync_quick(vm_offset_t va)
1080 READY1
1081 {
1082         cpu_invlpg((void *)va);
1083 }
1084
1085 /*
1086  * remove a page from the kernel pagetables
1087  */
1088 void
1089 pmap_kremove(vm_offset_t va)
1090 READY1
1091 {
1092         pt_entry_t *pte;
1093         pmap_inval_info info;
1094
1095         pmap_inval_init(&info);
1096         pte = vtopte(va);
1097         pmap_inval_add(&info, &kernel_pmap, va);
1098         *pte = 0;
1099         pmap_inval_flush(&info);
1100 }
1101
1102 void
1103 pmap_kremove_quick(vm_offset_t va)
1104 READY1
1105 {
1106         pt_entry_t *pte;
1107         pte = vtopte(va);
1108         *pte = 0;
1109         cpu_invlpg((void *)va);
1110 }
1111
1112 /*
1113  * XXX these need to be recoded.  They are not used in any critical path.
1114  */
1115 void
1116 pmap_kmodify_rw(vm_offset_t va)
1117 READY1
1118 {
1119         *vtopte(va) |= PG_RW;
1120         cpu_invlpg((void *)va);
1121 }
1122
1123 void
1124 pmap_kmodify_nc(vm_offset_t va)
1125 READY1
1126 {
1127         *vtopte(va) |= PG_N;
1128         cpu_invlpg((void *)va);
1129 }
1130
1131 /*
1132  *      Used to map a range of physical addresses into kernel
1133  *      virtual address space.
1134  *
1135  *      For now, VM is already on, we only need to map the
1136  *      specified memory.
1137  */
1138 vm_offset_t
1139 pmap_map(vm_offset_t virt, vm_paddr_t start, vm_paddr_t end, int prot)
1140 READY3
1141 {
1142         return PHYS_TO_DMAP(start);
1143 }
1144
1145
1146 /*
1147  * Add a list of wired pages to the kva
1148  * this routine is only used for temporary
1149  * kernel mappings that do not need to have
1150  * page modification or references recorded.
1151  * Note that old mappings are simply written
1152  * over.  The page *must* be wired.
1153  */
1154 void
1155 pmap_qenter(vm_offset_t va, vm_page_t *m, int count)
1156 READY1
1157 {
1158         vm_offset_t end_va;
1159
1160         end_va = va + count * PAGE_SIZE;
1161                 
1162         while (va < end_va) {
1163                 pt_entry_t *pte;
1164
1165                 pte = vtopte(va);
1166                 *pte = VM_PAGE_TO_PHYS(*m) | PG_RW | PG_V | pgeflag;
1167                 cpu_invlpg((void *)va);
1168                 va += PAGE_SIZE;
1169                 m++;
1170         }
1171 #ifdef SMP
1172         smp_invltlb();  /* XXX */
1173 #endif
1174 }
1175
1176 void
1177 pmap_qenter2(vm_offset_t va, vm_page_t *m, int count, cpumask_t *mask)
1178 READY1
1179 {
1180         vm_offset_t end_va;
1181         cpumask_t cmask = mycpu->gd_cpumask;
1182
1183         end_va = va + count * PAGE_SIZE;
1184
1185         while (va < end_va) {
1186                 pt_entry_t *pte;
1187                 pt_entry_t pteval;
1188
1189                 /*
1190                  * Install the new PTE.  If the pte changed from the prior
1191                  * mapping we must reset the cpu mask and invalidate the page.
1192                  * If the pte is the same but we have not seen it on the
1193                  * current cpu, invlpg the existing mapping.  Otherwise the
1194                  * entry is optimal and no invalidation is required.
1195                  */
1196                 pte = vtopte(va);
1197                 pteval = VM_PAGE_TO_PHYS(*m) | PG_A | PG_RW | PG_V | pgeflag;
1198                 if (*pte != pteval) {
1199                         *mask = 0;
1200                         *pte = pteval;
1201                         cpu_invlpg((void *)va);
1202                 } else if ((*mask & cmask) == 0) {
1203                         cpu_invlpg((void *)va);
1204                 }
1205                 va += PAGE_SIZE;
1206                 m++;
1207         }
1208         *mask |= cmask;
1209 }
1210
1211 /*
1212  * this routine jerks page mappings from the
1213  * kernel -- it is meant only for temporary mappings.
1214  */
1215 void
1216 pmap_qremove(vm_offset_t va, int count)
1217 READY1
1218 {
1219         vm_offset_t end_va;
1220
1221         end_va = va + count * PAGE_SIZE;
1222
1223         while (va < end_va) {
1224                 pt_entry_t *pte;
1225
1226                 pte = vtopte(va);
1227                 *pte = 0;
1228                 cpu_invlpg((void *)va);
1229                 va += PAGE_SIZE;
1230         }
1231 #ifdef SMP
1232         smp_invltlb();
1233 #endif
1234 }
1235
1236 /*
1237  * This routine works like vm_page_lookup() but also blocks as long as the
1238  * page is busy.  This routine does not busy the page it returns.
1239  *
1240  * Unless the caller is managing objects whos pages are in a known state,
1241  * the call should be made with a critical section held so the page's object
1242  * association remains valid on return.
1243  */
1244 static vm_page_t
1245 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
1246 READY1
1247 {
1248         vm_page_t m;
1249
1250         do {
1251                 m = vm_page_lookup(object, pindex);
1252         } while (m && vm_page_sleep_busy(m, FALSE, "pplookp"));
1253
1254         return(m);
1255 }
1256
1257 /*
1258  * Create a new thread and optionally associate it with a (new) process.
1259  * NOTE! the new thread's cpu may not equal the current cpu.
1260  */
1261 void
1262 pmap_init_thread(thread_t td)
1263 READY1
1264 {
1265         /* enforce pcb placement */
1266         td->td_pcb = (struct pcb *)(td->td_kstack + td->td_kstack_size) - 1;
1267         td->td_savefpu = &td->td_pcb->pcb_save;
1268         td->td_sp = (char *)td->td_pcb - 16; /* JG is -16 needed on amd64? */
1269 }
1270
1271 /*
1272  * This routine directly affects the fork perf for a process.
1273  */
1274 void
1275 pmap_init_proc(struct proc *p)
1276 READY1
1277 {
1278 }
1279
1280 /*
1281  * Dispose the UPAGES for a process that has exited.
1282  * This routine directly impacts the exit perf of a process.
1283  */
1284 void
1285 pmap_dispose_proc(struct proc *p)
1286 READY1
1287 {
1288         KASSERT(p->p_lock == 0, ("attempt to dispose referenced proc! %p", p));
1289 }
1290
1291 /***************************************************
1292  * Page table page management routines.....
1293  ***************************************************/
1294
1295 /*
1296  * This routine unholds page table pages, and if the hold count
1297  * drops to zero, then it decrements the wire count.
1298  */
1299 static int 
1300 _pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m, pmap_inval_info_t info) 
1301 READY1
1302 {
1303         /* 
1304          * Wait until we can busy the page ourselves.  We cannot have
1305          * any active flushes if we block.
1306          */
1307         if (m->flags & PG_BUSY) {
1308                 pmap_inval_flush(info);
1309                 while (vm_page_sleep_busy(m, FALSE, "pmuwpt"))
1310                         ;
1311         }
1312         KASSERT(m->queue == PQ_NONE,
1313                 ("_pmap_unwire_pte_hold: %p->queue != PQ_NONE", m));
1314
1315         if (m->hold_count == 1) {
1316                 /*
1317                  * Unmap the page table page
1318                  */
1319                 vm_page_busy(m);
1320                 pmap_inval_add(info, pmap, -1);
1321
1322                 if (m->pindex >= (NUPDE + NUPDPE)) {
1323                         /* PDP page */
1324                         pml4_entry_t *pml4;
1325                         pml4 = pmap_pml4e(pmap, va);
1326                         *pml4 = 0;
1327                 } else if (m->pindex >= NUPDE) {
1328                         /* PD page */
1329                         pdp_entry_t *pdp;
1330                         pdp = pmap_pdpe(pmap, va);
1331                         *pdp = 0;
1332                 } else {
1333                         /* PT page */
1334                         pd_entry_t *pd;
1335                         pd = pmap_pde(pmap, va);
1336                         *pd = 0;
1337                 }
1338
1339                 KKASSERT(pmap->pm_stats.resident_count > 0);
1340                 --pmap->pm_stats.resident_count;
1341
1342                 if (pmap->pm_ptphint == m)
1343                         pmap->pm_ptphint = NULL;
1344
1345                 if (m->pindex < NUPDE) {
1346                         /* We just released a PT, unhold the matching PD */
1347                         vm_page_t pdpg;
1348         
1349                         pdpg = PHYS_TO_VM_PAGE(*pmap_pdpe(pmap, va) & PG_FRAME);
1350                         pmap_unwire_pte_hold(pmap, va, pdpg, info);
1351                 }
1352                 if (m->pindex >= NUPDE && m->pindex < (NUPDE + NUPDPE)) {
1353                         /* We just released a PD, unhold the matching PDP */
1354                         vm_page_t pdppg;
1355         
1356                         pdppg = PHYS_TO_VM_PAGE(*pmap_pml4e(pmap, va) & PG_FRAME);
1357                         pmap_unwire_pte_hold(pmap, va, pdppg, info);
1358                 }
1359
1360                 /*
1361                  * This was our last hold, the page had better be unwired
1362                  * after we decrement wire_count.
1363                  * 
1364                  * FUTURE NOTE: shared page directory page could result in
1365                  * multiple wire counts.
1366                  */
1367                 vm_page_unhold(m);
1368                 --m->wire_count;
1369                 KKASSERT(m->wire_count == 0);
1370                 --vmstats.v_wire_count;
1371                 vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
1372                 vm_page_flash(m);
1373                 vm_page_free_zero(m);
1374                 return 1;
1375         } else {
1376                 /* JG Can we get here? */
1377                 KKASSERT(m->hold_count > 1);
1378                 vm_page_unhold(m);
1379                 return 0;
1380         }
1381 }
1382
1383 static PMAP_INLINE int
1384 pmap_unwire_pte_hold(pmap_t pmap, vm_offset_t va, vm_page_t m, pmap_inval_info_t info)
1385 READY1
1386 {
1387         KKASSERT(m->hold_count > 0);
1388         if (m->hold_count > 1) {
1389                 vm_page_unhold(m);
1390                 return 0;
1391         } else {
1392                 return _pmap_unwire_pte_hold(pmap, va, m, info);
1393         }
1394 }
1395
1396 /*
1397  * After removing a page table entry, this routine is used to
1398  * conditionally free the page, and manage the hold/wire counts.
1399  */
1400 static int
1401 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte,
1402                 pmap_inval_info_t info)
1403 READY1
1404 {
1405         /* JG Use FreeBSD/amd64 or FreeBSD/i386 ptepde approaches? */
1406         vm_pindex_t ptepindex;
1407         if (va >= VM_MAX_USER_ADDRESS)
1408                 return 0;
1409
1410         if (mpte == NULL) {
1411                 ptepindex = pmap_pde_pindex(va);
1412 #if JGHINT
1413                 if (pmap->pm_ptphint &&
1414                         (pmap->pm_ptphint->pindex == ptepindex)) {
1415                         mpte = pmap->pm_ptphint;
1416                 } else {
1417 #endif
1418                         pmap_inval_flush(info);
1419                         mpte = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1420                         pmap->pm_ptphint = mpte;
1421 #if JGHINT
1422                 }
1423 #endif
1424         }
1425
1426         return pmap_unwire_pte_hold(pmap, va, mpte, info);
1427 }
1428
1429 /*
1430  * Initialize pmap0/vmspace0.  This pmap is not added to pmap_list because
1431  * it, and IdlePTD, represents the template used to update all other pmaps.
1432  *
1433  * On architectures where the kernel pmap is not integrated into the user
1434  * process pmap, this pmap represents the process pmap, not the kernel pmap.
1435  * kernel_pmap should be used to directly access the kernel_pmap.
1436  */
1437 void
1438 pmap_pinit0(struct pmap *pmap)
1439 READY1
1440 {
1441 #if JGPMAP32
1442         pmap->pm_pdir =
1443                 (pd_entry_t *)kmem_alloc_pageable(&kernel_map, PAGE_SIZE);
1444         pmap_kenter((vm_offset_t)pmap->pm_pdir, (vm_offset_t) IdlePTD);
1445 #endif
1446         pmap->pm_pml4 = (pml4_entry_t *)(PTOV_OFFSET + KPML4phys);
1447         pmap->pm_count = 1;
1448         pmap->pm_active = 0;
1449         pmap->pm_ptphint = NULL;
1450         TAILQ_INIT(&pmap->pm_pvlist);
1451         bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1452 }
1453
1454 /*
1455  * Initialize a preallocated and zeroed pmap structure,
1456  * such as one in a vmspace structure.
1457  */
1458 void
1459 pmap_pinit(struct pmap *pmap)
1460 READY1
1461 {
1462         vm_page_t ptdpg;
1463
1464         /*
1465          * No need to allocate page table space yet but we do need a valid
1466          * page directory table.
1467          */
1468         if (pmap->pm_pml4 == NULL) {
1469                 pmap->pm_pml4 =
1470                     (pml4_entry_t *)kmem_alloc_pageable(&kernel_map, PAGE_SIZE);
1471         }
1472
1473         /*
1474          * Allocate an object for the ptes
1475          */
1476         if (pmap->pm_pteobj == NULL)
1477                 pmap->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, NUPDE + NUPDPE + PML4PML4I + 1);
1478
1479         /*
1480          * Allocate the page directory page, unless we already have
1481          * one cached.  If we used the cached page the wire_count will
1482          * already be set appropriately.
1483          */
1484         if ((ptdpg = pmap->pm_pdirm) == NULL) {
1485                 ptdpg = vm_page_grab(pmap->pm_pteobj, NUPDE + NUPDPE + PML4PML4I,
1486                                      VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1487                 pmap->pm_pdirm = ptdpg;
1488                 vm_page_flag_clear(ptdpg, PG_MAPPED | PG_BUSY);
1489                 ptdpg->valid = VM_PAGE_BITS_ALL;
1490                 ptdpg->wire_count = 1;
1491                 ++vmstats.v_wire_count;
1492                 pmap_kenter((vm_offset_t)pmap->pm_pml4, VM_PAGE_TO_PHYS(ptdpg));
1493         }
1494         if ((ptdpg->flags & PG_ZERO) == 0)
1495                 bzero(pmap->pm_pml4, PAGE_SIZE);
1496
1497         pmap->pm_pml4[KPML4I] = KPDPphys | PG_RW | PG_V | PG_U;
1498         pmap->pm_pml4[DMPML4I] = DMPDPphys | PG_RW | PG_V | PG_U;
1499
1500         /* install self-referential address mapping entry */
1501         pmap->pm_pml4[PML4PML4I] = VM_PAGE_TO_PHYS(ptdpg) | PG_V | PG_RW | PG_A | PG_M;
1502
1503         pmap->pm_count = 1;
1504         pmap->pm_active = 0;
1505         pmap->pm_ptphint = NULL;
1506         TAILQ_INIT(&pmap->pm_pvlist);
1507         bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1508         pmap->pm_stats.resident_count = 1;
1509 }
1510
1511 /*
1512  * Clean up a pmap structure so it can be physically freed.  This routine
1513  * is called by the vmspace dtor function.  A great deal of pmap data is
1514  * left passively mapped to improve vmspace management so we have a bit
1515  * of cleanup work to do here.
1516  */
1517 void
1518 pmap_puninit(pmap_t pmap)
1519 READY1
1520 {
1521         vm_page_t p;
1522
1523         KKASSERT(pmap->pm_active == 0);
1524         if ((p = pmap->pm_pdirm) != NULL) {
1525                 KKASSERT(pmap->pm_pml4 != NULL);
1526                 KKASSERT(pmap->pm_pml4 != (PTOV_OFFSET + KPML4phys));
1527                 pmap_kremove((vm_offset_t)pmap->pm_pml4);
1528                 p->wire_count--;
1529                 vmstats.v_wire_count--;
1530                 KKASSERT((p->flags & PG_BUSY) == 0);
1531                 vm_page_busy(p);
1532                 vm_page_free_zero(p);
1533                 pmap->pm_pdirm = NULL;
1534         }
1535         if (pmap->pm_pml4) {
1536                 KKASSERT(pmap->pm_pml4 != (PTOV_OFFSET + KPML4phys));
1537                 kmem_free(&kernel_map, (vm_offset_t)pmap->pm_pml4, PAGE_SIZE);
1538                 pmap->pm_pml4 = NULL;
1539         }
1540         if (pmap->pm_pteobj) {
1541                 vm_object_deallocate(pmap->pm_pteobj);
1542                 pmap->pm_pteobj = NULL;
1543         }
1544 }
1545
1546 /*
1547  * Wire in kernel global address entries.  To avoid a race condition
1548  * between pmap initialization and pmap_growkernel, this procedure
1549  * adds the pmap to the master list (which growkernel scans to update),
1550  * then copies the template.
1551  */
1552 void
1553 pmap_pinit2(struct pmap *pmap)
1554 READY0
1555 {
1556         crit_enter();
1557         TAILQ_INSERT_TAIL(&pmap_list, pmap, pm_pmnode);
1558         /* XXX copies current process, does not fill in MPPTDI */
1559 #if JGPMAP32
1560         bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * PTESIZE);
1561 #endif
1562         crit_exit();
1563 }
1564
1565 /*
1566  * Attempt to release and free a vm_page in a pmap.  Returns 1 on success,
1567  * 0 on failure (if the procedure had to sleep).
1568  *
1569  * When asked to remove the page directory page itself, we actually just
1570  * leave it cached so we do not have to incur the SMP inval overhead of
1571  * removing the kernel mapping.  pmap_puninit() will take care of it.
1572  */
1573 static int
1574 pmap_release_free_page(struct pmap *pmap, vm_page_t p)
1575 READY1
1576 {
1577         pml4_entry_t *pml4 = pmap->pm_pml4;
1578         /*
1579          * This code optimizes the case of freeing non-busy
1580          * page-table pages.  Those pages are zero now, and
1581          * might as well be placed directly into the zero queue.
1582          */
1583         if (vm_page_sleep_busy(p, FALSE, "pmaprl"))
1584                 return 0;
1585
1586         vm_page_busy(p);
1587
1588         /*
1589          * Remove the page table page from the processes address space.
1590          */
1591         if (p->pindex >= (NUPDE + NUPDPE) && p->pindex != (NUPDE + NUPDPE + PML4PML4I)) {
1592                 /*
1593                  * We are a PDP page.
1594                  * We look for the PML4 entry that points to us.
1595                  */
1596                 vm_page_t m4 = vm_page_lookup(pmap->pm_pteobj, NUPDE + NUPDPE + PML4PML4I);
1597                 KKASSERT(m4 != NULL);
1598                 pml4_entry_t *pml4 = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m4));
1599                 int idx = (p->pindex - (NUPDE + NUPDPE)) % NPML4EPG;
1600                 KKASSERT(pml4[idx] != 0);
1601                 pml4[idx] = 0;
1602                 m4->hold_count--;
1603                 /* JG What about wire_count? */
1604         } else if (p->pindex >= NUPDE) {
1605                 /*
1606                  * We are a PD page.
1607                  * We look for the PDP entry that points to us.
1608                  */
1609                 vm_page_t m3 = vm_page_lookup(pmap->pm_pteobj, NUPDE + NUPDPE + (p->pindex - NUPDE) / NPDPEPG);
1610                 KKASSERT(m3 != NULL);
1611                 pdp_entry_t *pdp = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m3));
1612                 int idx = (p->pindex - NUPDE) % NPDPEPG;
1613                 KKASSERT(pdp[idx] != 0);
1614                 pdp[idx] = 0;
1615                 m3->hold_count--;
1616                 /* JG What about wire_count? */
1617         } else {
1618                 /* We are a PT page.
1619                  * We look for the PD entry that points to us.
1620                  */
1621                 vm_page_t m2 = vm_page_lookup(pmap->pm_pteobj, NUPDE + p->pindex / NPDEPG);
1622                 KKASSERT(m2 != NULL);
1623                 pd_entry_t *pd = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m2));
1624                 int idx = p->pindex % NPDEPG;
1625                 pd[idx] = 0;
1626                 m2->hold_count--;
1627                 /* JG What about wire_count? */
1628         }
1629         KKASSERT(pmap->pm_stats.resident_count > 0);
1630         --pmap->pm_stats.resident_count;
1631
1632         if (p->hold_count)  {
1633                 panic("pmap_release: freeing held page table page");
1634         }
1635         if (pmap->pm_ptphint && (pmap->pm_ptphint->pindex == p->pindex))
1636                 pmap->pm_ptphint = NULL;
1637
1638         /*
1639          * We leave the top-level page table page cached, wired, and mapped in
1640          * the pmap until the dtor function (pmap_puninit()) gets called.
1641          * However, still clean it up so we can set PG_ZERO.
1642          */
1643         if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1644                 bzero(pmap->pm_pml4, PAGE_SIZE);
1645                 vm_page_flag_set(p, PG_ZERO);
1646                 vm_page_wakeup(p);
1647         } else {
1648                 p->wire_count--;
1649                 vmstats.v_wire_count--;
1650                 /* JG eventually revert to using vm_page_free_zero() */
1651                 vm_page_free(p);
1652         }
1653         return 1;
1654 }
1655
1656 /*
1657  * this routine is called if the page table page is not
1658  * mapped correctly.
1659  */
1660 static vm_page_t
1661 _pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex)
1662 READY1
1663 {
1664         vm_page_t m, pdppg, pdpg;
1665
1666         /*
1667          * Find or fabricate a new pagetable page
1668          */
1669         m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1670                         VM_ALLOC_NORMAL | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1671
1672
1673         if ((m->flags & PG_ZERO) == 0) {
1674                 pmap_zero_page(VM_PAGE_TO_PHYS(m));
1675         }
1676
1677         KASSERT(m->queue == PQ_NONE,
1678                 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1679
1680         /*
1681          * Increment the hold count for the page we will be returning to
1682          * the caller.
1683          */
1684         m->hold_count++;
1685
1686         /*
1687          * It is possible that someone else got in and mapped by the page
1688          * directory page while we were blocked, if so just unbusy and
1689          * return the held page.
1690          */
1691 #if JGPMAP32
1692         if ((ptepa = pmap->pm_pdir[ptepindex]) != 0) {
1693                 KKASSERT((ptepa & PG_FRAME) == VM_PAGE_TO_PHYS(m));
1694                 vm_page_wakeup(m);
1695                 return(m);
1696         }
1697 #endif
1698
1699         if (m->wire_count == 0)
1700                 vmstats.v_wire_count++;
1701         m->wire_count++;
1702
1703
1704         /*
1705          * Map the pagetable page into the process address space, if
1706          * it isn't already there.
1707          */
1708
1709         ++pmap->pm_stats.resident_count;
1710
1711 #if JGPMAP32
1712         ptepa = VM_PAGE_TO_PHYS(m);
1713         pmap->pm_pdir[ptepindex] =
1714                 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_A | PG_M);
1715 #endif
1716         if (ptepindex >= (NUPDE + NUPDPE)) {
1717                 pml4_entry_t *pml4;
1718                 vm_pindex_t pml4index;
1719
1720                 /* Wire up a new PDP page */
1721                 pml4index = ptepindex - (NUPDE + NUPDPE);
1722                 pml4 = &pmap->pm_pml4[pml4index];
1723                 *pml4 = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
1724
1725         } else if (ptepindex >= NUPDE) {
1726                 vm_pindex_t pml4index;
1727                 vm_pindex_t pdpindex;
1728                 pml4_entry_t *pml4;
1729                 pdp_entry_t *pdp;
1730
1731                 /* Wire up a new PD page */
1732                 pdpindex = ptepindex - NUPDE;
1733                 pml4index = pdpindex >> NPML4EPGSHIFT;
1734
1735                 pml4 = &pmap->pm_pml4[pml4index];
1736                 if ((*pml4 & PG_V) == 0) {
1737                         /* Have to allocate a new PDP page, recurse */
1738                         if (_pmap_allocpte(pmap, NUPDE + NUPDPE + pml4index)
1739                              == NULL) {
1740                                 --m->wire_count;
1741                                 vm_page_free(m);
1742                                 return (NULL);
1743                         }
1744                 } else {
1745                         /* Add reference to the PDP page */
1746                         pdppg = PHYS_TO_VM_PAGE(*pml4 & PG_FRAME);
1747                         pdppg->hold_count++;
1748                 }
1749                 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
1750
1751                 /* Now find the pdp page */
1752                 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1753                 KKASSERT(*pdp == 0);    /* JG DEBUG64 */
1754                 *pdp = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
1755
1756         } else {
1757                 vm_pindex_t pml4index;
1758                 vm_pindex_t pdpindex;
1759                 pml4_entry_t *pml4;
1760                 pdp_entry_t *pdp;
1761                 pd_entry_t *pd;
1762
1763                 /* Wire up a new PT page */
1764                 pdpindex = ptepindex >> NPDPEPGSHIFT;
1765                 pml4index = pdpindex >> NPML4EPGSHIFT;
1766
1767                 /* First, find the pdp and check that its valid. */
1768                 pml4 = &pmap->pm_pml4[pml4index];
1769                 if ((*pml4 & PG_V) == 0) {
1770                         /* We miss a PDP page. We ultimately need a PD page.
1771                          * Recursively allocating a PD page will allocate
1772                          * the missing PDP page and will also allocate
1773                          * the PD page we need.
1774                          */
1775                         /* Have to allocate a new PD page, recurse */
1776                         if (_pmap_allocpte(pmap, NUPDE + pdpindex)
1777                              == NULL) {
1778                                 --m->wire_count;
1779                                 vm_page_free(m);
1780                                 return (NULL);
1781                         }
1782                         pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
1783                         pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1784                 } else {
1785                         pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
1786                         pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
1787                         if ((*pdp & PG_V) == 0) {
1788                                 /* Have to allocate a new PD page, recurse */
1789                                 if (_pmap_allocpte(pmap, NUPDE + pdpindex)
1790                                      == NULL) {
1791                                         --m->wire_count;
1792                                         vm_page_free(m);
1793                                         return (NULL);
1794                                 }
1795                         } else {
1796                                 /* Add reference to the PD page */
1797                                 pdpg = PHYS_TO_VM_PAGE(*pdp & PG_FRAME);
1798                                 pdpg->hold_count++;
1799                         }
1800                 }
1801                 pd = (pd_entry_t *)PHYS_TO_DMAP(*pdp & PG_FRAME);
1802
1803                 /* Now we know where the page directory page is */
1804                 pd = &pd[ptepindex & ((1ul << NPDEPGSHIFT) - 1)];
1805                 KKASSERT(*pd == 0);     /* JG DEBUG64 */
1806                 *pd = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
1807         }
1808
1809
1810         /*
1811          * Set the page table hint
1812          */
1813         pmap->pm_ptphint = m;
1814
1815         m->valid = VM_PAGE_BITS_ALL;
1816         vm_page_flag_clear(m, PG_ZERO);
1817         vm_page_flag_set(m, PG_MAPPED);
1818         vm_page_wakeup(m);
1819
1820         return m;
1821 }
1822
1823 static vm_page_t
1824 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1825 READY1
1826 {
1827         vm_pindex_t ptepindex;
1828         pd_entry_t *pd;
1829         vm_page_t m;
1830
1831         /*
1832          * Calculate pagetable page index
1833          */
1834         ptepindex = pmap_pde_pindex(va);
1835
1836         /*
1837          * Get the page directory entry
1838          */
1839         pd = pmap_pde(pmap, va);
1840
1841         /*
1842          * This supports switching from a 2MB page to a
1843          * normal 4K page.
1844          */
1845         if (pd != NULL && (*pd & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
1846                 panic("no promotion/demotion yet");
1847                 *pd = 0;
1848                 pd = NULL;
1849                 cpu_invltlb();
1850                 smp_invltlb();
1851         }
1852
1853         /*
1854          * If the page table page is mapped, we just increment the
1855          * hold count, and activate it.
1856          */
1857         if (pd != NULL && (*pd & PG_V) != 0) {
1858                 /* YYY hint is used here on i386 */
1859                 m = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
1860                 pmap->pm_ptphint = m;
1861                 m->hold_count++;
1862                 return m;
1863         }
1864         /*
1865          * Here if the pte page isn't mapped, or if it has been deallocated.
1866          */
1867         return _pmap_allocpte(pmap, ptepindex);
1868 }
1869
1870
1871 /***************************************************
1872  * Pmap allocation/deallocation routines.
1873  ***************************************************/
1874
1875 /*
1876  * Release any resources held by the given physical map.
1877  * Called when a pmap initialized by pmap_pinit is being released.
1878  * Should only be called if the map contains no valid mappings.
1879  */
1880 static int pmap_release_callback(struct vm_page *p, void *data);
1881
1882 void
1883 pmap_release(struct pmap *pmap)
1884 READY1
1885 {
1886         vm_object_t object = pmap->pm_pteobj;
1887         struct rb_vm_page_scan_info info;
1888
1889         KASSERT(pmap->pm_active == 0, ("pmap still active! %08x", pmap->pm_active));
1890 #if defined(DIAGNOSTIC)
1891         if (object->ref_count != 1)
1892                 panic("pmap_release: pteobj reference count != 1");
1893 #endif
1894         
1895         info.pmap = pmap;
1896         info.object = object;
1897         crit_enter();
1898         TAILQ_REMOVE(&pmap_list, pmap, pm_pmnode);
1899         crit_exit();
1900
1901         do {
1902                 crit_enter();
1903                 info.error = 0;
1904                 info.mpte = NULL;
1905                 info.limit = object->generation;
1906
1907                 vm_page_rb_tree_RB_SCAN(&object->rb_memq, NULL, 
1908                                         pmap_release_callback, &info);
1909                 if (info.error == 0 && info.mpte) {
1910                         if (!pmap_release_free_page(pmap, info.mpte))
1911                                 info.error = 1;
1912                 }
1913                 crit_exit();
1914         } while (info.error);
1915 }
1916
1917 static int
1918 pmap_release_callback(struct vm_page *p, void *data)
1919 READY1
1920 {
1921         struct rb_vm_page_scan_info *info = data;
1922
1923         if (p->pindex == NUPDE + NUPDPE + PML4PML4I) {
1924                 info->mpte = p;
1925                 return(0);
1926         }
1927         if (!pmap_release_free_page(info->pmap, p)) {
1928                 info->error = 1;
1929                 return(-1);
1930         }
1931         if (info->object->generation != info->limit) {
1932                 info->error = 1;
1933                 return(-1);
1934         }
1935         return(0);
1936 }
1937
1938 /*
1939  * Grow the number of kernel page table entries, if needed.
1940  */
1941
1942 void
1943 pmap_growkernel(vm_offset_t addr)
1944 READY1
1945 {
1946         vm_paddr_t paddr;
1947         struct pmap *pmap;
1948         vm_offset_t ptppaddr;
1949         vm_page_t nkpg;
1950         pd_entry_t *pde, newpdir;
1951         pdp_entry_t newpdp;
1952
1953         crit_enter();
1954         if (kernel_vm_end == 0) {
1955                 kernel_vm_end = KERNBASE;
1956                 nkpt = 0;
1957                 while ((*pmap_pde(&kernel_pmap, kernel_vm_end) & PG_V) != 0) {
1958                         kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1959                         nkpt++;
1960                         if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1961                                 kernel_vm_end = kernel_map.max_offset;
1962                                 break;                       
1963                         }
1964                 }
1965         }
1966         addr = roundup2(addr, PAGE_SIZE * NPTEPG);
1967         if (addr - 1 >= kernel_map.max_offset)
1968                 addr = kernel_map.max_offset;
1969         while (kernel_vm_end < addr) {
1970                 pde = pmap_pde(&kernel_pmap, kernel_vm_end);
1971                 if (pde == NULL) {
1972                         /* We need a new PDP entry */
1973                         nkpg = vm_page_alloc(kptobj, nkpt,
1974                                              VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM
1975                                              | VM_ALLOC_INTERRUPT);
1976                         if (nkpg == NULL)
1977                                 panic("pmap_growkernel: no memory to grow kernel");
1978                         if ((nkpg->flags & PG_ZERO) == 0)
1979                                 pmap_zero_page(nkpg);
1980                         paddr = VM_PAGE_TO_PHYS(nkpg);
1981                         newpdp = (pdp_entry_t)
1982                                 (paddr | PG_V | PG_RW | PG_A | PG_M);
1983                         *pmap_pdpe(&kernel_pmap, kernel_vm_end) = newpdp;
1984                         continue; /* try again */
1985                 }
1986                 if ((*pde & PG_V) != 0) {
1987                         kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1988                         if (kernel_vm_end - 1 >= kernel_map.max_offset) {
1989                                 kernel_vm_end = kernel_map.max_offset;
1990                                 break;                       
1991                         }
1992                         continue;
1993                 }
1994
1995                 /*
1996                  * This index is bogus, but out of the way
1997                  */
1998                 nkpg = vm_page_alloc(kptobj, nkpt,
1999                         VM_ALLOC_NORMAL | VM_ALLOC_SYSTEM | VM_ALLOC_INTERRUPT);
2000                 if (nkpg == NULL)
2001                         panic("pmap_growkernel: no memory to grow kernel");
2002
2003                 vm_page_wire(nkpg);
2004                 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
2005                 pmap_zero_page(ptppaddr);
2006                 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
2007                 *pmap_pde(&kernel_pmap, kernel_vm_end) = newpdir;
2008                 nkpt++;
2009
2010                 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
2011                 if (kernel_vm_end - 1 >= kernel_map.max_offset) {
2012                         kernel_vm_end = kernel_map.max_offset;
2013                         break;                       
2014                 }
2015         }
2016         crit_exit();
2017 }
2018
2019 /*
2020  *      Retire the given physical map from service.
2021  *      Should only be called if the map contains
2022  *      no valid mappings.
2023  */
2024 void
2025 pmap_destroy(pmap_t pmap)
2026 READY0
2027 {
2028         int count;
2029
2030         if (pmap == NULL)
2031                 return;
2032
2033         count = --pmap->pm_count;
2034         if (count == 0) {
2035                 pmap_release(pmap);
2036                 panic("destroying a pmap is not yet implemented");
2037         }
2038 }
2039
2040 /*
2041  *      Add a reference to the specified pmap.
2042  */
2043 void
2044 pmap_reference(pmap_t pmap)
2045 READY2
2046 {
2047         if (pmap != NULL) {
2048                 pmap->pm_count++;
2049         }
2050 }
2051
2052 /***************************************************
2053 * page management routines.
2054  ***************************************************/
2055
2056 /*
2057  * free the pv_entry back to the free list.  This function may be
2058  * called from an interrupt.
2059  */
2060 static PMAP_INLINE void
2061 free_pv_entry(pv_entry_t pv)
2062 READY2
2063 {
2064         pv_entry_count--;
2065         KKASSERT(pv_entry_count >= 0);
2066         zfree(pvzone, pv);
2067 }
2068
2069 /*
2070  * get a new pv_entry, allocating a block from the system
2071  * when needed.  This function may be called from an interrupt.
2072  */
2073 static pv_entry_t
2074 get_pv_entry(void)
2075 READY2
2076 {
2077         pv_entry_count++;
2078         if (pv_entry_high_water &&
2079                 (pv_entry_count > pv_entry_high_water) &&
2080                 (pmap_pagedaemon_waken == 0)) {
2081                 pmap_pagedaemon_waken = 1;
2082                 wakeup(&vm_pages_needed);
2083         }
2084         return zalloc(pvzone);
2085 }
2086
2087 /*
2088  * This routine is very drastic, but can save the system
2089  * in a pinch.
2090  */
2091 void
2092 pmap_collect(void)
2093 READY0
2094 {
2095         int i;
2096         vm_page_t m;
2097         static int warningdone=0;
2098
2099         if (pmap_pagedaemon_waken == 0)
2100                 return;
2101
2102         if (warningdone < 5) {
2103                 kprintf("pmap_collect: collecting pv entries -- suggest increasing PMAP_SHPGPERPROC\n");
2104                 warningdone++;
2105         }
2106
2107         for(i = 0; i < vm_page_array_size; i++) {
2108                 m = &vm_page_array[i];
2109                 if (m->wire_count || m->hold_count || m->busy ||
2110                     (m->flags & PG_BUSY))
2111                         continue;
2112                 pmap_remove_all(m);
2113         }
2114         pmap_pagedaemon_waken = 0;
2115 }
2116         
2117
2118 /*
2119  * If it is the first entry on the list, it is actually
2120  * in the header and we must copy the following entry up
2121  * to the header.  Otherwise we must search the list for
2122  * the entry.  In either case we free the now unused entry.
2123  */
2124 static int
2125 pmap_remove_entry(struct pmap *pmap, vm_page_t m, 
2126                         vm_offset_t va, pmap_inval_info_t info)
2127 READY1
2128 {
2129         pv_entry_t pv;
2130         int rtval;
2131
2132         crit_enter();
2133         if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
2134                 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2135                         if (pmap == pv->pv_pmap && va == pv->pv_va) 
2136                                 break;
2137                 }
2138         } else {
2139                 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
2140                         if (va == pv->pv_va) 
2141                                 break;
2142                 }
2143         }
2144
2145         rtval = 0;
2146         /* JGXXX When can 'pv' be NULL? */
2147         if (pv) {
2148                 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2149                 m->md.pv_list_count--;
2150                 KKASSERT(m->md.pv_list_count >= 0);
2151                 if (TAILQ_EMPTY(&m->md.pv_list))
2152                         vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2153                 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
2154                 ++pmap->pm_generation;
2155                 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem, info);
2156                 free_pv_entry(pv);
2157         }
2158         crit_exit();
2159         return rtval;
2160 }
2161
2162 /*
2163  * Create a pv entry for page at pa for
2164  * (pmap, va).
2165  */
2166 static void
2167 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
2168 READY1
2169 {
2170         pv_entry_t pv;
2171
2172         crit_enter();
2173         pv = get_pv_entry();
2174         pv->pv_va = va;
2175         pv->pv_pmap = pmap;
2176         pv->pv_ptem = mpte;
2177
2178         TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
2179         TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
2180         m->md.pv_list_count++;
2181
2182         crit_exit();
2183 }
2184
2185 /*
2186  * pmap_remove_pte: do the things to unmap a page in a process
2187  */
2188 static int
2189 pmap_remove_pte(struct pmap *pmap, pt_entry_t *ptq, vm_offset_t va,
2190         pmap_inval_info_t info)
2191 READY1
2192 {
2193         pt_entry_t oldpte;
2194         vm_page_t m;
2195
2196         pmap_inval_add(info, pmap, va);
2197         oldpte = pte_load_clear(ptq);
2198         if (oldpte & PG_W)
2199                 pmap->pm_stats.wired_count -= 1;
2200         /*
2201          * Machines that don't support invlpg, also don't support
2202          * PG_G.  XXX PG_G is disabled for SMP so don't worry about
2203          * the SMP case.
2204          */
2205         if (oldpte & PG_G)
2206                 cpu_invlpg((void *)va);
2207         KKASSERT(pmap->pm_stats.resident_count > 0);
2208         --pmap->pm_stats.resident_count;
2209         if (oldpte & PG_MANAGED) {
2210                 m = PHYS_TO_VM_PAGE(oldpte);
2211                 if (oldpte & PG_M) {
2212 #if defined(PMAP_DIAGNOSTIC)
2213                         if (pmap_nw_modified((pt_entry_t) oldpte)) {
2214                                 kprintf(
2215         "pmap_remove: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2216                                     va, oldpte);
2217                         }
2218 #endif
2219                         if (pmap_track_modified(va))
2220                                 vm_page_dirty(m);
2221                 }
2222                 if (oldpte & PG_A)
2223                         vm_page_flag_set(m, PG_REFERENCED);
2224                 return pmap_remove_entry(pmap, m, va, info);
2225         } else {
2226                 return pmap_unuse_pt(pmap, va, NULL, info);
2227         }
2228
2229         return 0;
2230 }
2231
2232 /*
2233  * pmap_remove_page:
2234  *
2235  *      Remove a single page from a process address space.
2236  *
2237  *      This function may not be called from an interrupt if the pmap is
2238  *      not kernel_pmap.
2239  */
2240 static void
2241 pmap_remove_page(struct pmap *pmap, vm_offset_t va, pmap_inval_info_t info)
2242 READY1
2243 {
2244         pt_entry_t *pte;
2245
2246         pte = pmap_pte(pmap, va);
2247         if (pte == NULL)
2248                 return;
2249         if ((*pte & PG_V) == 0)
2250                 return;
2251         pmap_remove_pte(pmap, pte, va, info);
2252 }
2253
2254 /*
2255  * pmap_remove:
2256  *
2257  *      Remove the given range of addresses from the specified map.
2258  *
2259  *      It is assumed that the start and end are properly
2260  *      rounded to the page size.
2261  *
2262  *      This function may not be called from an interrupt if the pmap is
2263  *      not kernel_pmap.
2264  */
2265 void
2266 pmap_remove(struct pmap *pmap, vm_offset_t sva, vm_offset_t eva)
2267 READY1
2268 {
2269         vm_offset_t va_next;
2270         pml4_entry_t *pml4e;
2271         pdp_entry_t *pdpe;
2272         pd_entry_t ptpaddr, *pde;
2273         pt_entry_t *pte;
2274         struct pmap_inval_info info;
2275
2276         if (pmap == NULL)
2277                 return;
2278
2279         if (pmap->pm_stats.resident_count == 0)
2280                 return;
2281
2282         pmap_inval_init(&info);
2283
2284         /*
2285          * special handling of removing one page.  a very
2286          * common operation and easy to short circuit some
2287          * code.
2288          */
2289         if (sva + PAGE_SIZE == eva) {
2290                 pde = pmap_pde(pmap, sva);
2291                 if (pde && (*pde & PG_PS) == 0) {
2292                         pmap_remove_page(pmap, sva, &info);
2293                         pmap_inval_flush(&info);
2294                         return;
2295                 }
2296         }
2297
2298         for (; sva < eva; sva = va_next) {
2299                 pml4e = pmap_pml4e(pmap, sva);
2300                 if ((*pml4e & PG_V) == 0) {
2301                         va_next = (sva + NBPML4) & ~PML4MASK;
2302                         if (va_next < sva)
2303                                 va_next = eva;
2304                         continue;
2305                 }
2306
2307                 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
2308                 if ((*pdpe & PG_V) == 0) {
2309                         va_next = (sva + NBPDP) & ~PDPMASK;
2310                         if (va_next < sva)
2311                                 va_next = eva;
2312                         continue;
2313                 }
2314
2315                 /*
2316                  * Calculate index for next page table.
2317                  */
2318                 va_next = (sva + NBPDR) & ~PDRMASK;
2319                 if (va_next < sva)
2320                         va_next = eva;
2321
2322                 pde = pmap_pdpe_to_pde(pdpe, sva);
2323                 ptpaddr = *pde;
2324
2325                 /*
2326                  * Weed out invalid mappings.
2327                  */
2328                 if (ptpaddr == 0)
2329                         continue;
2330
2331                 /*
2332                  * Check for large page.
2333                  */
2334                 if ((ptpaddr & PG_PS) != 0) {
2335                         /* JG FreeBSD has more complex treatment here */
2336                         pmap_inval_add(&info, pmap, -1);
2337                         *pde = 0;
2338                         pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2339                         continue;
2340                 }
2341
2342                 /*
2343                  * Limit our scan to either the end of the va represented
2344                  * by the current page table page, or to the end of the
2345                  * range being removed.
2346                  */
2347                 if (va_next > eva)
2348                         va_next = eva;
2349
2350                 /*
2351                  * NOTE: pmap_remove_pte() can block.
2352                  */
2353                 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2354                     sva += PAGE_SIZE) {
2355                         if (*pte == 0)
2356                                 continue;
2357                         if (pmap_remove_pte(pmap, pte, sva, &info))
2358                                 break;
2359                 }
2360         }
2361         pmap_inval_flush(&info);
2362 }
2363
2364 /*
2365  * pmap_remove_all:
2366  *
2367  *      Removes this physical page from all physical maps in which it resides.
2368  *      Reflects back modify bits to the pager.
2369  *
2370  *      This routine may not be called from an interrupt.
2371  */
2372
2373 static void
2374 pmap_remove_all(vm_page_t m)
2375 READY1
2376 {
2377         struct pmap_inval_info info;
2378         pt_entry_t *pte, tpte;
2379         pv_entry_t pv;
2380
2381         if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2382                 return;
2383
2384         pmap_inval_init(&info);
2385         crit_enter();
2386         while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2387                 KKASSERT(pv->pv_pmap->pm_stats.resident_count > 0);
2388                 --pv->pv_pmap->pm_stats.resident_count;
2389
2390                 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2391                 pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
2392                 tpte = pte_load_clear(pte);
2393
2394                 if (tpte & PG_W)
2395                         pv->pv_pmap->pm_stats.wired_count--;
2396
2397                 if (tpte & PG_A)
2398                         vm_page_flag_set(m, PG_REFERENCED);
2399
2400                 /*
2401                  * Update the vm_page_t clean and reference bits.
2402                  */
2403                 if (tpte & PG_M) {
2404 #if defined(PMAP_DIAGNOSTIC)
2405                         if (pmap_nw_modified(tpte)) {
2406                                 kprintf(
2407         "pmap_remove_all: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2408                                     pv->pv_va, tpte);
2409                         }
2410 #endif
2411                         if (pmap_track_modified(pv->pv_va))
2412                                 vm_page_dirty(m);
2413                 }
2414                 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2415                 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2416                 ++pv->pv_pmap->pm_generation;
2417                 m->md.pv_list_count--;
2418                 KKASSERT(m->md.pv_list_count >= 0);
2419                 if (TAILQ_EMPTY(&m->md.pv_list))
2420                         vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
2421                 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem, &info);
2422                 free_pv_entry(pv);
2423         }
2424         crit_exit();
2425         KKASSERT((m->flags & (PG_MAPPED|PG_WRITEABLE)) == 0);
2426         pmap_inval_flush(&info);
2427 }
2428
2429 /*
2430  * pmap_protect:
2431  *
2432  *      Set the physical protection on the specified range of this map
2433  *      as requested.
2434  *
2435  *      This function may not be called from an interrupt if the map is
2436  *      not the kernel_pmap.
2437  */
2438 void
2439 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
2440 READY1
2441 {
2442         vm_offset_t va_next;
2443         pml4_entry_t *pml4e;
2444         pdp_entry_t *pdpe;
2445         pd_entry_t ptpaddr, *pde;
2446         pt_entry_t *pte;
2447         pmap_inval_info info;
2448
2449         /* JG review for NX */
2450
2451         if (pmap == NULL)
2452                 return;
2453
2454         if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
2455                 pmap_remove(pmap, sva, eva);
2456                 return;
2457         }
2458
2459         if (prot & VM_PROT_WRITE)
2460                 return;
2461
2462         pmap_inval_init(&info);
2463
2464         for (; sva < eva; sva = va_next) {
2465
2466                 pml4e = pmap_pml4e(pmap, sva);
2467                 if ((*pml4e & PG_V) == 0) {
2468                         va_next = (sva + NBPML4) & ~PML4MASK;
2469                         if (va_next < sva)
2470                                 va_next = eva;
2471                         continue;
2472                 }
2473
2474                 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
2475                 if ((*pdpe & PG_V) == 0) {
2476                         va_next = (sva + NBPDP) & ~PDPMASK;
2477                         if (va_next < sva)
2478                                 va_next = eva;
2479                         continue;
2480                 }
2481
2482                 va_next = (sva + NBPDR) & ~PDRMASK;
2483                 if (va_next < sva)
2484                         va_next = eva;
2485
2486                 pde = pmap_pdpe_to_pde(pdpe, sva);
2487                 ptpaddr = *pde;
2488
2489                 /*
2490                  * Check for large page.
2491                  */
2492                 if ((ptpaddr & PG_PS) != 0) {
2493                         pmap_inval_add(&info, pmap, -1);
2494                         *pde &= ~(PG_M|PG_RW);
2495                         pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2496                         continue;
2497                 }
2498
2499                 /*
2500                  * Weed out invalid mappings. Note: we assume that the page
2501                  * directory table is always allocated, and in kernel virtual.
2502                  */
2503                 if (ptpaddr == 0)
2504                         continue;
2505
2506                 if (va_next > eva)
2507                         va_next = eva;
2508
2509                 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
2510                     sva += PAGE_SIZE) {
2511                         pt_entry_t obits, pbits;
2512                         vm_page_t m;
2513
2514                         /*
2515                          * XXX non-optimal.  Note also that there can be
2516                          * no pmap_inval_flush() calls until after we modify
2517                          * ptbase[sindex] (or otherwise we have to do another
2518                          * pmap_inval_add() call).
2519                          */
2520                         pmap_inval_add(&info, pmap, sva);
2521                         obits = pbits = *pte;
2522                         if ((pbits & PG_V) == 0)
2523                                 continue;
2524                         if (pbits & PG_MANAGED) {
2525                                 m = NULL;
2526                                 if (pbits & PG_A) {
2527                                         m = PHYS_TO_VM_PAGE(pbits & PG_FRAME);
2528                                         vm_page_flag_set(m, PG_REFERENCED);
2529                                         pbits &= ~PG_A;
2530                                 }
2531                                 if (pbits & PG_M) {
2532                                         if (pmap_track_modified(sva)) {
2533                                                 if (m == NULL)
2534                                                         m = PHYS_TO_VM_PAGE(pbits & PG_FRAME);
2535                                                 vm_page_dirty(m);
2536                                                 pbits &= ~PG_M;
2537                                         }
2538                                 }
2539                         }
2540
2541                         pbits &= ~PG_RW;
2542
2543                         if (pbits != obits) {
2544                                 *pte = pbits;
2545                         }
2546                 }
2547         }
2548         pmap_inval_flush(&info);
2549 }
2550
2551 /*
2552  *      Insert the given physical page (p) at
2553  *      the specified virtual address (v) in the
2554  *      target physical map with the protection requested.
2555  *
2556  *      If specified, the page will be wired down, meaning
2557  *      that the related pte can not be reclaimed.
2558  *
2559  *      NB:  This is the only routine which MAY NOT lazy-evaluate
2560  *      or lose information.  That is, this routine must actually
2561  *      insert this page into the given map NOW.
2562  */
2563 void
2564 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
2565            boolean_t wired)
2566 READY1
2567 {
2568         vm_paddr_t pa;
2569         pd_entry_t *pde;
2570         pt_entry_t *pte;
2571         vm_paddr_t opa;
2572         pt_entry_t origpte, newpte;
2573         vm_page_t mpte;
2574         pmap_inval_info info;
2575
2576         if (pmap == NULL)
2577                 return;
2578
2579         va = trunc_page(va);
2580 #ifdef PMAP_DIAGNOSTIC
2581         if (va >= KvaEnd)
2582                 panic("pmap_enter: toobig");
2583         if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
2584                 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%lx)", va);
2585 #endif
2586         if (va < UPT_MAX_ADDRESS && pmap == &kernel_pmap) {
2587                 kprintf("Warning: pmap_enter called on UVA with kernel_pmap\n");
2588 #ifdef DDB
2589                 db_print_backtrace();
2590 #endif
2591         }
2592         if (va >= UPT_MAX_ADDRESS && pmap != &kernel_pmap) {
2593                 kprintf("Warning: pmap_enter called on KVA without kernel_pmap\n");
2594 #ifdef DDB
2595                 db_print_backtrace();
2596 #endif
2597         }
2598
2599         /*
2600          * In the case that a page table page is not
2601          * resident, we are creating it here.
2602          */
2603         if (va < VM_MAX_USER_ADDRESS)
2604                 mpte = pmap_allocpte(pmap, va);
2605         else
2606                 mpte = NULL;
2607
2608         pmap_inval_init(&info);
2609         pde = pmap_pde(pmap, va);
2610         if (pde != NULL && (*pde & PG_V) != 0) {
2611                 if ((*pde & PG_PS) != 0)
2612                         panic("pmap_enter: attempted pmap_enter on 2MB page");
2613                 pte = pmap_pde_to_pte(pde, va);
2614         } else
2615                 panic("pmap_enter: invalid page directory va=%#lx", va);
2616
2617         KKASSERT(pte != NULL);
2618         pa = VM_PAGE_TO_PHYS(m);
2619         origpte = *pte;
2620         opa = origpte & PG_FRAME;
2621
2622         /*
2623          * Mapping has not changed, must be protection or wiring change.
2624          */
2625         if (origpte && (opa == pa)) {
2626                 /*
2627                  * Wiring change, just update stats. We don't worry about
2628                  * wiring PT pages as they remain resident as long as there
2629                  * are valid mappings in them. Hence, if a user page is wired,
2630                  * the PT page will be also.
2631                  */
2632                 if (wired && ((origpte & PG_W) == 0))
2633                         pmap->pm_stats.wired_count++;
2634                 else if (!wired && (origpte & PG_W))
2635                         pmap->pm_stats.wired_count--;
2636
2637 #if defined(PMAP_DIAGNOSTIC)
2638                 if (pmap_nw_modified(origpte)) {
2639                         kprintf(
2640         "pmap_enter: modified page not writable: va: 0x%lx, pte: 0x%lx\n",
2641                             va, origpte);
2642                 }
2643 #endif
2644
2645                 /*
2646                  * Remove the extra pte reference.  Note that we cannot
2647                  * optimize the RO->RW case because we have adjusted the
2648                  * wiring count above and may need to adjust the wiring
2649                  * bits below.
2650                  */
2651                 if (mpte)
2652                         mpte->hold_count--;
2653
2654                 /*
2655                  * We might be turning off write access to the page,
2656                  * so we go ahead and sense modify status.
2657                  */
2658                 if (origpte & PG_MANAGED) {
2659                         if ((origpte & PG_M) && pmap_track_modified(va)) {
2660                                 vm_page_t om;
2661                                 om = PHYS_TO_VM_PAGE(opa);
2662                                 vm_page_dirty(om);
2663                         }
2664                         pa |= PG_MANAGED;
2665                         KKASSERT(m->flags & PG_MAPPED);
2666                 }
2667                 goto validate;
2668         } 
2669         /*
2670          * Mapping has changed, invalidate old range and fall through to
2671          * handle validating new mapping.
2672          */
2673         if (opa) {
2674                 int err;
2675                 err = pmap_remove_pte(pmap, pte, va, &info);
2676                 if (err)
2677                         panic("pmap_enter: pte vanished, va: 0x%lx", va);
2678         }
2679
2680         /*
2681          * Enter on the PV list if part of our managed memory. Note that we
2682          * raise IPL while manipulating pv_table since pmap_enter can be
2683          * called at interrupt time.
2684          */
2685         if (pmap_initialized && 
2686             (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2687                 pmap_insert_entry(pmap, va, mpte, m);
2688                 pa |= PG_MANAGED;
2689                 vm_page_flag_set(m, PG_MAPPED);
2690         }
2691
2692         /*
2693          * Increment counters
2694          */
2695         ++pmap->pm_stats.resident_count;
2696         if (wired)
2697                 pmap->pm_stats.wired_count++;
2698
2699 validate:
2700         /*
2701          * Now validate mapping with desired protection/wiring.
2702          */
2703         newpte = (pt_entry_t) (pa | pte_prot(pmap, prot) | PG_V);
2704
2705         if (wired)
2706                 newpte |= PG_W;
2707         if (va < VM_MAX_USER_ADDRESS)
2708                 newpte |= PG_U;
2709         if (pmap == &kernel_pmap)
2710                 newpte |= pgeflag;
2711
2712         /*
2713          * if the mapping or permission bits are different, we need
2714          * to update the pte.
2715          */
2716         if ((origpte & ~(PG_M|PG_A)) != newpte) {
2717                 pmap_inval_add(&info, pmap, va);
2718                 *pte = newpte | PG_A;
2719                 if (newpte & PG_RW)
2720                         vm_page_flag_set(m, PG_WRITEABLE);
2721         }
2722         KKASSERT((newpte & PG_MANAGED) == 0 || (m->flags & PG_MAPPED));
2723         pmap_inval_flush(&info);
2724 }
2725
2726 /*
2727  * This code works like pmap_enter() but assumes VM_PROT_READ and not-wired.
2728  * This code also assumes that the pmap has no pre-existing entry for this
2729  * VA.
2730  *
2731  * This code currently may only be used on user pmaps, not kernel_pmap.
2732  */
2733 static void
2734 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m)
2735 READY1
2736 {
2737         pt_entry_t *pte;
2738         vm_paddr_t pa;
2739         vm_page_t mpte;
2740         vm_pindex_t ptepindex;
2741         pd_entry_t *ptepa;
2742         pmap_inval_info info;
2743
2744         pmap_inval_init(&info);
2745
2746         if (va < UPT_MAX_ADDRESS && pmap == &kernel_pmap) {
2747                 kprintf("Warning: pmap_enter_quick called on UVA with kernel_pmap\n");
2748 #ifdef DDB
2749                 db_print_backtrace();
2750 #endif
2751         }
2752         if (va >= UPT_MAX_ADDRESS && pmap != &kernel_pmap) {
2753                 kprintf("Warning: pmap_enter_quick called on KVA without kernel_pmap\n");
2754 #ifdef DDB
2755                 db_print_backtrace();
2756 #endif
2757         }
2758
2759         KKASSERT(va < UPT_MIN_ADDRESS); /* assert used on user pmaps only */
2760
2761         /*
2762          * Calculate the page table page (mpte), allocating it if necessary.
2763          *
2764          * A held page table page (mpte), or NULL, is passed onto the
2765          * section following.
2766          */
2767         if (va < VM_MAX_USER_ADDRESS) {
2768                 /*
2769                  * Calculate pagetable page index
2770                  */
2771                 ptepindex = pmap_pde_pindex(va);
2772
2773                 do {
2774                         /*
2775                          * Get the page directory entry
2776                          */
2777                         ptepa = pmap_pde(pmap, va);
2778
2779                         /*
2780                          * If the page table page is mapped, we just increment
2781                          * the hold count, and activate it.
2782                          */
2783                         if (ptepa && (*ptepa & PG_V) != 0) {
2784                                 if (*ptepa & PG_PS)
2785                                         panic("pmap_enter_quick: unexpected mapping into 2MB page");
2786 //                              if (pmap->pm_ptphint &&
2787 //                                  (pmap->pm_ptphint->pindex == ptepindex)) {
2788 //                                      mpte = pmap->pm_ptphint;
2789 //                              } else {
2790                                         mpte = pmap_page_lookup( pmap->pm_pteobj, ptepindex);
2791                                         pmap->pm_ptphint = mpte;
2792 //                              }
2793                                 if (mpte)
2794                                         mpte->hold_count++;
2795                         } else {
2796                                 mpte = _pmap_allocpte(pmap, ptepindex);
2797                         }
2798                 } while (mpte == NULL);
2799         } else {
2800                 mpte = NULL;
2801                 /* this code path is not yet used */
2802         }
2803
2804         /*
2805          * With a valid (and held) page directory page, we can just use
2806          * vtopte() to get to the pte.  If the pte is already present
2807          * we do not disturb it.
2808          */
2809         pte = vtopte(va);
2810         if (*pte & PG_V) {
2811                 if (mpte)
2812                         pmap_unwire_pte_hold(pmap, va, mpte, &info);
2813                 pa = VM_PAGE_TO_PHYS(m);
2814                 KKASSERT(((*pte ^ pa) & PG_FRAME) == 0);
2815                 return;
2816         }
2817
2818         /*
2819          * Enter on the PV list if part of our managed memory
2820          */
2821         if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2822                 pmap_insert_entry(pmap, va, mpte, m);
2823                 vm_page_flag_set(m, PG_MAPPED);
2824         }
2825
2826         /*
2827          * Increment counters
2828          */
2829         ++pmap->pm_stats.resident_count;
2830
2831         pa = VM_PAGE_TO_PHYS(m);
2832
2833         /*
2834          * Now validate mapping with RO protection
2835          */
2836         if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2837                 *pte = pa | PG_V | PG_U;
2838         else
2839                 *pte = pa | PG_V | PG_U | PG_MANAGED;
2840 /*      pmap_inval_add(&info, pmap, va); shouldn't be needed inval->valid */
2841         pmap_inval_flush(&info);
2842 }
2843
2844 /*
2845  * Make a temporary mapping for a physical address.  This is only intended
2846  * to be used for panic dumps.
2847  */
2848 /* JG Needed on amd64? */
2849 void *
2850 pmap_kenter_temporary(vm_paddr_t pa, int i)
2851 READY2
2852 {
2853         pmap_kenter((vm_offset_t)crashdumpmap + (i * PAGE_SIZE), pa);
2854         return ((void *)crashdumpmap);
2855 }
2856
2857 #define MAX_INIT_PT (96)
2858
2859 /*
2860  * This routine preloads the ptes for a given object into the specified pmap.
2861  * This eliminates the blast of soft faults on process startup and
2862  * immediately after an mmap.
2863  */
2864 static int pmap_object_init_pt_callback(vm_page_t p, void *data);
2865
2866 void
2867 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_prot_t prot,
2868                     vm_object_t object, vm_pindex_t pindex, 
2869                     vm_size_t size, int limit)
2870 READY1
2871 {
2872         struct rb_vm_page_scan_info info;
2873         struct lwp *lp;
2874         vm_size_t psize;
2875
2876         /*
2877          * We can't preinit if read access isn't set or there is no pmap
2878          * or object.
2879          */
2880         if ((prot & VM_PROT_READ) == 0 || pmap == NULL || object == NULL)
2881                 return;
2882
2883         /*
2884          * We can't preinit if the pmap is not the current pmap
2885          */
2886         lp = curthread->td_lwp;
2887         if (lp == NULL || pmap != vmspace_pmap(lp->lwp_vmspace))
2888                 return;
2889
2890         psize = amd64_btop(size);
2891
2892         if ((object->type != OBJT_VNODE) ||
2893                 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2894                         (object->resident_page_count > MAX_INIT_PT))) {
2895                 return;
2896         }
2897
2898         if (psize + pindex > object->size) {
2899                 if (object->size < pindex)
2900                         return;           
2901                 psize = object->size - pindex;
2902         }
2903
2904         if (psize == 0)
2905                 return;
2906
2907         /*
2908          * Use a red-black scan to traverse the requested range and load
2909          * any valid pages found into the pmap.
2910          *
2911          * We cannot safely scan the object's memq unless we are in a
2912          * critical section since interrupts can remove pages from objects.
2913          */
2914         info.start_pindex = pindex;
2915         info.end_pindex = pindex + psize - 1;
2916         info.limit = limit;
2917         info.mpte = NULL;
2918         info.addr = addr;
2919         info.pmap = pmap;
2920
2921         crit_enter();
2922         vm_page_rb_tree_RB_SCAN(&object->rb_memq, rb_vm_page_scancmp,
2923                                 pmap_object_init_pt_callback, &info);
2924         crit_exit();
2925 }
2926
2927 static
2928 int
2929 pmap_object_init_pt_callback(vm_page_t p, void *data)
2930 READY1
2931 {
2932         struct rb_vm_page_scan_info *info = data;
2933         vm_pindex_t rel_index;
2934         /*
2935          * don't allow an madvise to blow away our really
2936          * free pages allocating pv entries.
2937          */
2938         if ((info->limit & MAP_PREFAULT_MADVISE) &&
2939                 vmstats.v_free_count < vmstats.v_free_reserved) {
2940                     return(-1);
2941         }
2942         if (((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2943             (p->busy == 0) && (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2944                 if ((p->queue - p->pc) == PQ_CACHE)
2945                         vm_page_deactivate(p);
2946                 vm_page_busy(p);
2947                 rel_index = p->pindex - info->start_pindex;
2948                 pmap_enter_quick(info->pmap,
2949                                  info->addr + amd64_ptob(rel_index), p);
2950                 vm_page_wakeup(p);
2951         }
2952         return(0);
2953 }
2954
2955 /*
2956  * pmap_prefault provides a quick way of clustering pagefaults into a
2957  * processes address space.  It is a "cousin" of pmap_object_init_pt, 
2958  * except it runs at page fault time instead of mmap time.
2959  */
2960 #define PFBAK 4
2961 #define PFFOR 4
2962 #define PAGEORDER_SIZE (PFBAK+PFFOR)
2963
2964 static int pmap_prefault_pageorder[] = {
2965         -PAGE_SIZE, PAGE_SIZE,
2966         -2 * PAGE_SIZE, 2 * PAGE_SIZE,
2967         -3 * PAGE_SIZE, 3 * PAGE_SIZE,
2968         -4 * PAGE_SIZE, 4 * PAGE_SIZE
2969 };
2970
2971 void
2972 pmap_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry)
2973 READY0
2974 {
2975         int i;
2976         vm_offset_t starta;
2977         vm_offset_t addr;
2978         vm_pindex_t pindex;
2979         vm_page_t m;
2980         vm_object_t object;
2981         struct lwp *lp;
2982
2983         /*
2984          * We do not currently prefault mappings that use virtual page
2985          * tables.  We do not prefault foreign pmaps.
2986          */
2987         if (entry->maptype == VM_MAPTYPE_VPAGETABLE)
2988                 return;
2989         lp = curthread->td_lwp;
2990         if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
2991                 return;
2992
2993         object = entry->object.vm_object;
2994
2995         starta = addra - PFBAK * PAGE_SIZE;
2996         if (starta < entry->start)
2997                 starta = entry->start;
2998         else if (starta > addra)
2999                 starta = 0;
3000
3001         /*
3002          * critical section protection is required to maintain the 
3003          * page/object association, interrupts can free pages and remove 
3004          * them from their objects.
3005          */
3006         crit_enter();
3007         for (i = 0; i < PAGEORDER_SIZE; i++) {
3008                 vm_object_t lobject;
3009                 pt_entry_t *pte;
3010
3011                 addr = addra + pmap_prefault_pageorder[i];
3012                 if (addr > addra + (PFFOR * PAGE_SIZE))
3013                         addr = 0;
3014
3015                 if (addr < starta || addr >= entry->end)
3016                         continue;
3017
3018                 if ((*pmap_pde(pmap, addr)) == 0)
3019                         continue;
3020
3021                 pte = vtopte(addr);
3022                 if (*pte)
3023                         continue;
3024
3025                 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
3026                 lobject = object;
3027
3028                 for (m = vm_page_lookup(lobject, pindex);
3029                     (!m && (lobject->type == OBJT_DEFAULT) &&
3030                      (lobject->backing_object));
3031                     lobject = lobject->backing_object
3032                 ) {
3033                         if (lobject->backing_object_offset & PAGE_MASK)
3034                                 break;
3035                         pindex += (lobject->backing_object_offset >> PAGE_SHIFT);
3036                         m = vm_page_lookup(lobject->backing_object, pindex);
3037                 }
3038
3039                 /*
3040                  * give-up when a page is not in memory
3041                  */
3042                 if (m == NULL)
3043                         break;
3044
3045                 if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
3046                         (m->busy == 0) &&
3047                     (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
3048
3049                         if ((m->queue - m->pc) == PQ_CACHE) {
3050                                 vm_page_deactivate(m);
3051                         }
3052                         vm_page_busy(m);
3053                         pmap_enter_quick(pmap, addr, m);
3054                         vm_page_wakeup(m);
3055                 }
3056         }
3057         crit_exit();
3058 }
3059
3060 /*
3061  *      Routine:        pmap_change_wiring
3062  *      Function:       Change the wiring attribute for a map/virtual-address
3063  *                      pair.
3064  *      In/out conditions:
3065  *                      The mapping must already exist in the pmap.
3066  */
3067 void
3068 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
3069 READY0
3070 {
3071         pt_entry_t *pte;
3072
3073         if (pmap == NULL)
3074                 return;
3075
3076         pte = pmap_pte(pmap, va);
3077
3078         if (wired && !pmap_pte_w(pte))
3079                 pmap->pm_stats.wired_count++;
3080         else if (!wired && pmap_pte_w(pte))
3081                 pmap->pm_stats.wired_count--;
3082
3083         /*
3084          * Wiring is not a hardware characteristic so there is no need to
3085          * invalidate TLB.  However, in an SMP environment we must use
3086          * a locked bus cycle to update the pte (if we are not using 
3087          * the pmap_inval_*() API that is)... it's ok to do this for simple
3088          * wiring changes.
3089          */
3090 #ifdef SMP
3091         if (wired)
3092                 atomic_set_long(pte, PG_W);
3093         else
3094                 atomic_clear_long(pte, PG_W);
3095 #else
3096         if (wired)
3097                 atomic_set_long_nonlocked(pte, PG_W);
3098         else
3099                 atomic_clear_long_nonlocked(pte, PG_W);
3100 #endif
3101 }
3102
3103
3104
3105 /*
3106  *      Copy the range specified by src_addr/len
3107  *      from the source map to the range dst_addr/len
3108  *      in the destination map.
3109  *
3110  *      This routine is only advisory and need not do anything.
3111  */
3112 void
3113 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, 
3114         vm_size_t len, vm_offset_t src_addr)
3115 READY0
3116 {
3117         pmap_inval_info info;
3118         vm_offset_t addr;
3119         vm_offset_t end_addr = src_addr + len;
3120         vm_offset_t pdnxt;
3121         pd_entry_t src_frame, dst_frame;
3122         vm_page_t m;
3123
3124         if (dst_addr != src_addr)
3125                 return;
3126         /*
3127          * XXX BUGGY.  Amoung other things srcmpte is assumed to remain
3128          * valid through blocking calls, and that's just not going to
3129          * be the case.
3130          *
3131          * FIXME!
3132          */
3133         return;
3134
3135 #if JGPMAP32
3136         src_frame = src_pmap->pm_pdir[PTDPTDI] & PG_FRAME;
3137         if (src_frame != (PTDpde & PG_FRAME)) {
3138                 return;
3139         }
3140
3141         dst_frame = dst_pmap->pm_pdir[PTDPTDI] & PG_FRAME;
3142         if (dst_frame != (APTDpde & PG_FRAME)) {
3143                 APTDpde = (pd_entry_t) (dst_frame | PG_RW | PG_V);
3144                 /* The page directory is not shared between CPUs */
3145                 cpu_invltlb();
3146         }
3147 #endif
3148         pmap_inval_init(&info);
3149         pmap_inval_add(&info, dst_pmap, -1);
3150         pmap_inval_add(&info, src_pmap, -1);
3151
3152         /*
3153          * critical section protection is required to maintain the page/object
3154          * association, interrupts can free pages and remove them from 
3155          * their objects.
3156          */
3157         crit_enter();
3158         for (addr = src_addr; addr < end_addr; addr = pdnxt) {
3159                 pt_entry_t *src_pte, *dst_pte;
3160                 vm_page_t dstmpte, srcmpte;
3161                 vm_offset_t srcptepaddr;
3162                 vm_pindex_t ptepindex;
3163
3164                 if (addr >= UPT_MIN_ADDRESS)
3165                         panic("pmap_copy: invalid to pmap_copy page tables\n");
3166
3167                 /*
3168                  * Don't let optional prefaulting of pages make us go
3169                  * way below the low water mark of free pages or way
3170                  * above high water mark of used pv entries.
3171                  */
3172                 if (vmstats.v_free_count < vmstats.v_free_reserved ||
3173                     pv_entry_count > pv_entry_high_water)
3174                         break;
3175                 
3176                 pdnxt = ((addr + PAGE_SIZE*NPTEPG) & ~(PAGE_SIZE*NPTEPG - 1));
3177                 ptepindex = addr >> PDRSHIFT;
3178
3179 #if JGPMAP32
3180                 srcptepaddr = (vm_offset_t) src_pmap->pm_pdir[ptepindex];
3181 #endif
3182                 if (srcptepaddr == 0)
3183                         continue;
3184                         
3185                 if (srcptepaddr & PG_PS) {
3186 #if JGPMAP32
3187                         if (dst_pmap->pm_pdir[ptepindex] == 0) {
3188                                 dst_pmap->pm_pdir[ptepindex] = (pd_entry_t) srcptepaddr;
3189                                 dst_pmap->pm_stats.resident_count += NBPDR / PAGE_SIZE;
3190                         }
3191 #endif
3192                         continue;
3193                 }
3194
3195                 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
3196                 if ((srcmpte == NULL) || (srcmpte->hold_count == 0) ||
3197                     (srcmpte->flags & PG_BUSY)) {
3198                         continue;
3199                 }
3200
3201                 if (pdnxt > end_addr)
3202                         pdnxt = end_addr;
3203
3204                 src_pte = vtopte(addr);
3205 #if JGPMAP32
3206                 dst_pte = avtopte(addr);
3207 #endif
3208                 while (addr < pdnxt) {
3209                         pt_entry_t ptetemp;
3210
3211                         ptetemp = *src_pte;
3212                         /*
3213                          * we only virtual copy managed pages
3214                          */
3215                         if ((ptetemp & PG_MANAGED) != 0) {
3216                                 /*
3217                                  * We have to check after allocpte for the
3218                                  * pte still being around...  allocpte can
3219                                  * block.
3220                                  *
3221                                  * pmap_allocpte() can block.  If we lose
3222                                  * our page directory mappings we stop.
3223                                  */
3224                                 dstmpte = pmap_allocpte(dst_pmap, addr);
3225
3226 #if JGPMAP32
3227                                 if (src_frame != (PTDpde & PG_FRAME) ||
3228                                     dst_frame != (APTDpde & PG_FRAME)
3229                                 ) {
3230                                         kprintf("WARNING: pmap_copy: detected and corrected race\n");
3231                                         pmap_unwire_pte_hold(dst_pmap, dstmpte, &info);
3232                                         goto failed;
3233                                 } else if ((*dst_pte == 0) &&
3234                                            (ptetemp = *src_pte) != 0 &&
3235                                            (ptetemp & PG_MANAGED)) {
3236                                         /*
3237                                          * Clear the modified and
3238                                          * accessed (referenced) bits
3239                                          * during the copy.
3240                                          */
3241                                         m = PHYS_TO_VM_PAGE(ptetemp);
3242                                         *dst_pte = ptetemp & ~(PG_M | PG_A);
3243                                         ++dst_pmap->pm_stats.resident_count;
3244                                         pmap_insert_entry(dst_pmap, addr,
3245                                                 dstmpte, m);
3246                                         KKASSERT(m->flags & PG_MAPPED);
3247                                 } else {
3248                                         kprintf("WARNING: pmap_copy: dst_pte race detected and corrected\n");
3249                                         pmap_unwire_pte_hold(dst_pmap, dstmpte, &info);
3250                                         goto failed;
3251                                 }
3252 #endif
3253                                 if (dstmpte->hold_count >= srcmpte->hold_count)
3254                                         break;
3255                         }
3256                         addr += PAGE_SIZE;
3257                         src_pte++;
3258                         dst_pte++;
3259                 }
3260         }
3261 failed:
3262         crit_exit();
3263         pmap_inval_flush(&info);
3264 }       
3265
3266 /*
3267  * pmap_zero_page:
3268  *
3269  *      Zero the specified physical page.
3270  *
3271  *      This function may be called from an interrupt and no locking is
3272  *      required.
3273  */
3274 void
3275 pmap_zero_page(vm_paddr_t phys)
3276 READY1
3277 {
3278         vm_offset_t va = PHYS_TO_DMAP(phys);
3279
3280         pagezero((void *)va);
3281 }
3282
3283 /*
3284  * pmap_page_assertzero:
3285  *
3286  *      Assert that a page is empty, panic if it isn't.
3287  */
3288 void
3289 pmap_page_assertzero(vm_paddr_t phys)
3290 READY1
3291 {
3292         struct mdglobaldata *gd = mdcpu;
3293         int i;
3294
3295         crit_enter();
3296         vm_offset_t virt = PHYS_TO_DMAP(phys);
3297
3298         for (i = 0; i < PAGE_SIZE; i += sizeof(int)) {
3299             if (*(int *)((char *)virt + i) != 0) {
3300                 panic("pmap_page_assertzero() @ %p not zero!\n",
3301                     (void *)virt);
3302             }
3303         }
3304         crit_exit();
3305 }
3306
3307 /*
3308  * pmap_zero_page:
3309  *
3310  *      Zero part of a physical page by mapping it into memory and clearing
3311  *      its contents with bzero.
3312  *
3313  *      off and size may not cover an area beyond a single hardware page.
3314  */
3315 void
3316 pmap_zero_page_area(vm_paddr_t phys, int off, int size)
3317 READY1
3318 {
3319         struct mdglobaldata *gd = mdcpu;
3320
3321         crit_enter();
3322         vm_offset_t virt = PHYS_TO_DMAP(phys);
3323         bzero((char *)virt + off, size);
3324         crit_exit();
3325 }
3326
3327 /*
3328  * pmap_copy_page:
3329  *
3330  *      Copy the physical page from the source PA to the target PA.
3331  *      This function may be called from an interrupt.  No locking
3332  *      is required.
3333  */
3334 void
3335 pmap_copy_page(vm_paddr_t src, vm_paddr_t dst)
3336 READY1
3337 {
3338         vm_offset_t src_virt, dst_virt;
3339
3340         crit_enter();
3341         src_virt = PHYS_TO_DMAP(src);
3342         dst_virt = PHYS_TO_DMAP(dst);
3343         bcopy(src_virt, dst_virt, PAGE_SIZE);
3344         crit_exit();
3345 }
3346
3347 /*
3348  * pmap_copy_page_frag:
3349  *
3350  *      Copy the physical page from the source PA to the target PA.
3351  *      This function may be called from an interrupt.  No locking
3352  *      is required.
3353  */
3354 void
3355 pmap_copy_page_frag(vm_paddr_t src, vm_paddr_t dst, size_t bytes)
3356 READY1
3357 {
3358         vm_offset_t src_virt, dst_virt;
3359
3360         crit_enter();
3361         src_virt = PHYS_TO_DMAP(src);
3362         dst_virt = PHYS_TO_DMAP(dst);
3363         bcopy((char *)src_virt + (src & PAGE_MASK),
3364               (char *)dst_virt + (dst & PAGE_MASK),
3365               bytes);
3366         crit_exit();
3367 }
3368
3369 /*
3370  * Returns true if the pmap's pv is one of the first
3371  * 16 pvs linked to from this page.  This count may
3372  * be changed upwards or downwards in the future; it
3373  * is only necessary that true be returned for a small
3374  * subset of pmaps for proper page aging.
3375  */
3376 boolean_t
3377 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
3378 READY2
3379 {
3380         pv_entry_t pv;
3381         int loops = 0;
3382
3383         if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3384                 return FALSE;
3385
3386         crit_enter();
3387
3388         TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3389                 if (pv->pv_pmap == pmap) {
3390                         crit_exit();
3391                         return TRUE;
3392                 }
3393                 loops++;
3394                 if (loops >= 16)
3395                         break;
3396         }
3397         crit_exit();
3398         return (FALSE);
3399 }
3400
3401 /*
3402  * Remove all pages from specified address space
3403  * this aids process exit speeds.  Also, this code
3404  * is special cased for current process only, but
3405  * can have the more generic (and slightly slower)
3406  * mode enabled.  This is much faster than pmap_remove
3407  * in the case of running down an entire address space.
3408  */
3409 void
3410 pmap_remove_pages(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
3411 READY1
3412 {
3413         struct lwp *lp;
3414         pt_entry_t *pte, tpte;
3415         pv_entry_t pv, npv;
3416         vm_page_t m;
3417         pmap_inval_info info;
3418         int iscurrentpmap;
3419         int save_generation;
3420
3421         lp = curthread->td_lwp;
3422         if (lp && pmap == vmspace_pmap(lp->lwp_vmspace))
3423                 iscurrentpmap = 1;
3424         else
3425                 iscurrentpmap = 0;
3426
3427         pmap_inval_init(&info);
3428         crit_enter();
3429         for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
3430                 if (pv->pv_va >= eva || pv->pv_va < sva) {
3431                         npv = TAILQ_NEXT(pv, pv_plist);
3432                         continue;
3433                 }
3434
3435                 KKASSERT(pmap == pv->pv_pmap);
3436
3437                 if (iscurrentpmap)
3438                         pte = vtopte(pv->pv_va);
3439                 else
3440                         pte = pmap_pte_quick(pmap, pv->pv_va);
3441                 if (pmap->pm_active)
3442                         pmap_inval_add(&info, pmap, pv->pv_va);
3443
3444                 /*
3445                  * We cannot remove wired pages from a process' mapping
3446                  * at this time
3447                  */
3448                 if (*pte & PG_W) {
3449                         npv = TAILQ_NEXT(pv, pv_plist);
3450                         continue;
3451                 }
3452                 tpte = pte_load_clear(pte);
3453
3454                 m = PHYS_TO_VM_PAGE(tpte & PG_FRAME);
3455
3456                 KASSERT(m < &vm_page_array[vm_page_array_size],
3457                         ("pmap_remove_pages: bad tpte %lx", tpte));
3458
3459                 KKASSERT(pmap->pm_stats.resident_count > 0);
3460                 --pmap->pm_stats.resident_count;
3461
3462                 /*
3463                  * Update the vm_page_t clean and reference bits.
3464                  */
3465                 if (tpte & PG_M) {
3466                         vm_page_dirty(m);
3467                 }
3468
3469                 npv = TAILQ_NEXT(pv, pv_plist);
3470                 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
3471                 save_generation = ++pmap->pm_generation;
3472
3473                 m->md.pv_list_count--;
3474                 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3475                 if (TAILQ_EMPTY(&m->md.pv_list))
3476                         vm_page_flag_clear(m, PG_MAPPED | PG_WRITEABLE);
3477
3478                 pmap_unuse_pt(pmap, pv->pv_va, pv->pv_ptem, &info);
3479                 free_pv_entry(pv);
3480
3481                 /*
3482                  * Restart the scan if we blocked during the unuse or free
3483                  * calls and other removals were made.
3484                  */
3485                 if (save_generation != pmap->pm_generation) {
3486                         kprintf("Warning: pmap_remove_pages race-A avoided\n");
3487                         pv = TAILQ_FIRST(&pmap->pm_pvlist);
3488                 }
3489         }
3490         pmap_inval_flush(&info);
3491         crit_exit();
3492 }
3493
3494 /*
3495  * pmap_testbit tests bits in pte's
3496  * note that the testbit/clearbit routines are inline,
3497  * and a lot of things compile-time evaluate.
3498  */
3499 static boolean_t
3500 pmap_testbit(vm_page_t m, int bit)
3501 READY1
3502 {
3503         pv_entry_t pv;
3504         pt_entry_t *pte;
3505
3506         if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3507                 return FALSE;
3508
3509         if (TAILQ_FIRST(&m->md.pv_list) == NULL)
3510                 return FALSE;
3511
3512         crit_enter();
3513
3514         TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3515                 /*
3516                  * if the bit being tested is the modified bit, then
3517                  * mark clean_map and ptes as never
3518                  * modified.
3519                  */
3520                 if (bit & (PG_A|PG_M)) {
3521                         if (!pmap_track_modified(pv->pv_va))
3522                                 continue;
3523                 }
3524
3525 #if defined(PMAP_DIAGNOSTIC)
3526                 if (pv->pv_pmap == NULL) {
3527                         kprintf("Null pmap (tb) at va: 0x%lx\n", pv->pv_va);
3528                         continue;
3529                 }
3530 #endif
3531                 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3532                 if (*pte & bit) {
3533                         crit_exit();
3534                         return TRUE;
3535                 }
3536         }
3537         crit_exit();
3538         return (FALSE);
3539 }
3540
3541 /*
3542  * this routine is used to modify bits in ptes
3543  */
3544 static __inline void
3545 pmap_clearbit(vm_page_t m, int bit)
3546 READY1
3547 {
3548         struct pmap_inval_info info;
3549         pv_entry_t pv;
3550         pt_entry_t *pte;
3551         pt_entry_t pbits;
3552
3553         if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3554                 return;
3555
3556         pmap_inval_init(&info);
3557         crit_enter();
3558
3559         /*
3560          * Loop over all current mappings setting/clearing as appropos If
3561          * setting RO do we need to clear the VAC?
3562          */
3563         TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3564                 /*
3565                  * don't write protect pager mappings
3566                  */
3567                 if (bit == PG_RW) {
3568                         if (!pmap_track_modified(pv->pv_va))
3569                                 continue;
3570                 }
3571
3572 #if defined(PMAP_DIAGNOSTIC)
3573                 if (pv->pv_pmap == NULL) {
3574                         kprintf("Null pmap (cb) at va: 0x%lx\n", pv->pv_va);
3575                         continue;
3576                 }
3577 #endif
3578
3579                 /*
3580                  * Careful here.  We can use a locked bus instruction to
3581                  * clear PG_A or PG_M safely but we need to synchronize
3582                  * with the target cpus when we mess with PG_RW.
3583                  *
3584                  * We do not have to force synchronization when clearing
3585                  * PG_M even for PTEs generated via virtual memory maps,
3586                  * because the virtual kernel will invalidate the pmap
3587                  * entry when/if it needs to resynchronize the Modify bit.
3588                  */
3589                 if (bit & PG_RW)
3590                         pmap_inval_add(&info, pv->pv_pmap, pv->pv_va);
3591                 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3592 again:
3593                 pbits = *pte;
3594                 if (pbits & bit) {
3595                         if (bit == PG_RW) {
3596                                 if (pbits & PG_M) {
3597                                         vm_page_dirty(m);
3598                                         atomic_clear_long(pte, PG_M|PG_RW);
3599                                 } else {
3600                                         /*
3601                                          * The cpu may be trying to set PG_M
3602                                          * simultaniously with our clearing
3603                                          * of PG_RW.
3604                                          */
3605                                         if (!atomic_cmpset_long(pte, pbits,
3606                                                                pbits & ~PG_RW))
3607                                                 goto again;
3608                                 }
3609                         } else if (bit == PG_M) {
3610                                 /*
3611                                  * We could also clear PG_RW here to force
3612                                  * a fault on write to redetect PG_M for
3613                                  * virtual kernels, but it isn't necessary
3614                                  * since virtual kernels invalidate the pte 
3615                                  * when they clear the VPTE_M bit in their
3616                                  * virtual page tables.
3617                                  */
3618                                 atomic_clear_long(pte, PG_M);
3619                         } else {
3620                                 atomic_clear_long(pte, bit);
3621                         }
3622                 }
3623         }
3624         pmap_inval_flush(&info);
3625         crit_exit();
3626 }
3627
3628 /*
3629  *      pmap_page_protect:
3630  *
3631  *      Lower the permission for all mappings to a given page.
3632  */
3633 void
3634 pmap_page_protect(vm_page_t m, vm_prot_t prot)
3635 READY1
3636 {
3637         /* JG NX support? */
3638         if ((prot & VM_PROT_WRITE) == 0) {
3639                 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
3640                         pmap_clearbit(m, PG_RW);
3641                         vm_page_flag_clear(m, PG_WRITEABLE);
3642                 } else {
3643                         pmap_remove_all(m);
3644                 }
3645         }
3646 }
3647
3648 vm_paddr_t
3649 pmap_phys_address(vm_pindex_t ppn)
3650 READY2
3651 {
3652         return (amd64_ptob(ppn));
3653 }
3654
3655 /*
3656  *      pmap_ts_referenced:
3657  *
3658  *      Return a count of reference bits for a page, clearing those bits.
3659  *      It is not necessary for every reference bit to be cleared, but it
3660  *      is necessary that 0 only be returned when there are truly no
3661  *      reference bits set.
3662  *
3663  *      XXX: The exact number of bits to check and clear is a matter that
3664  *      should be tested and standardized at some point in the future for
3665  *      optimal aging of shared pages.
3666  */
3667 int
3668 pmap_ts_referenced(vm_page_t m)
3669 READY1
3670 {
3671         pv_entry_t pv, pvf, pvn;
3672         pt_entry_t *pte;
3673         int rtval = 0;
3674
3675         if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3676                 return (rtval);
3677
3678         crit_enter();
3679
3680         if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3681
3682                 pvf = pv;
3683
3684                 do {
3685                         pvn = TAILQ_NEXT(pv, pv_list);
3686
3687                         TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3688
3689                         TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3690
3691                         if (!pmap_track_modified(pv->pv_va))
3692                                 continue;
3693
3694                         pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3695
3696                         if (pte && (*pte & PG_A)) {
3697 #ifdef SMP
3698                                 atomic_clear_long(pte, PG_A);
3699 #else
3700                                 atomic_clear_long_nonlocked(pte, PG_A);
3701 #endif
3702                                 rtval++;
3703                                 if (rtval > 4) {
3704                                         break;
3705                                 }
3706                         }
3707                 } while ((pv = pvn) != NULL && pv != pvf);
3708         }
3709         crit_exit();
3710
3711         return (rtval);
3712 }
3713
3714 /*
3715  *      pmap_is_modified:
3716  *
3717  *      Return whether or not the specified physical page was modified
3718  *      in any physical maps.
3719  */
3720 boolean_t
3721 pmap_is_modified(vm_page_t m)
3722 READY2
3723 {
3724         return pmap_testbit(m, PG_M);
3725 }
3726
3727 /*
3728  *      Clear the modify bits on the specified physical page.
3729  */
3730 void
3731 pmap_clear_modify(vm_page_t m)
3732 READY2
3733 {
3734         pmap_clearbit(m, PG_M);
3735 }
3736
3737 /*
3738  *      pmap_clear_reference:
3739  *
3740  *      Clear the reference bit on the specified physical page.
3741  */
3742 void
3743 pmap_clear_reference(vm_page_t m)
3744 READY2
3745 {
3746         pmap_clearbit(m, PG_A);
3747 }
3748
3749 /*
3750  * Miscellaneous support routines follow
3751  */
3752
3753 static void
3754 i386_protection_init(void)
3755 READY0
3756 {
3757         int *kp, prot;
3758
3759         /* JG NX support may go here; No VM_PROT_EXECUTE ==> set NX bit  */
3760         kp = protection_codes;
3761         for (prot = 0; prot < 8; prot++) {
3762                 switch (prot) {
3763                 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE:
3764                         /*
3765                          * Read access is also 0. There isn't any execute bit,
3766                          * so just make it readable.
3767                          */
3768                 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE:
3769                 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE:
3770                 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE:
3771                         *kp++ = 0;
3772                         break;
3773                 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE:
3774                 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE:
3775                 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE:
3776                 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
3777                         *kp++ = PG_RW;
3778                         break;
3779                 }
3780         }
3781 }
3782
3783 /*
3784  * Map a set of physical memory pages into the kernel virtual
3785  * address space. Return a pointer to where it is mapped. This
3786  * routine is intended to be used for mapping device memory,
3787  * NOT real memory.
3788  *
3789  * NOTE: we can't use pgeflag unless we invalidate the pages one at
3790  * a time.
3791  */
3792 void *
3793 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
3794 READY1
3795 {
3796         vm_offset_t va, tmpva, offset;
3797         pt_entry_t *pte;
3798
3799         offset = pa & PAGE_MASK;
3800         size = roundup(offset + size, PAGE_SIZE);
3801
3802         va = kmem_alloc_nofault(&kernel_map, size);
3803         if (va == 0)
3804                 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3805
3806         pa = pa & ~PAGE_MASK;
3807         for (tmpva = va; size > 0;) {
3808                 pte = vtopte(tmpva);
3809                 *pte = pa | PG_RW | PG_V; /* | pgeflag; */
3810                 size -= PAGE_SIZE;
3811                 tmpva += PAGE_SIZE;
3812                 pa += PAGE_SIZE;
3813         }
3814         cpu_invltlb();
3815         smp_invltlb();
3816
3817         return ((void *)(va + offset));
3818 }
3819
3820 void *
3821 pmap_mapdev_uncacheable(vm_paddr_t pa, vm_size_t size)
3822 READY1
3823 {
3824         vm_offset_t va, tmpva, offset;
3825         pt_entry_t *pte;
3826
3827         offset = pa & PAGE_MASK;
3828         size = roundup(offset + size, PAGE_SIZE);
3829
3830         va = kmem_alloc_nofault(&kernel_map, size);
3831         if (va == 0)
3832                 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3833
3834         pa = pa & ~PAGE_MASK;
3835         for (tmpva = va; size > 0;) {
3836                 pte = vtopte(tmpva);
3837                 *pte = pa | PG_RW | PG_V | PG_N; /* | pgeflag; */
3838                 size -= PAGE_SIZE;
3839                 tmpva += PAGE_SIZE;
3840                 pa += PAGE_SIZE;
3841         }
3842         cpu_invltlb();
3843         smp_invltlb();
3844
3845         return ((void *)(va + offset));
3846 }
3847
3848 void
3849 pmap_unmapdev(vm_offset_t va, vm_size_t size)
3850 READY1
3851 {
3852         vm_offset_t base, offset;
3853
3854         base = va & ~PAGE_MASK;
3855         offset = va & PAGE_MASK;
3856         size = roundup(offset + size, PAGE_SIZE);
3857         pmap_qremove(va, size >> PAGE_SHIFT);
3858         kmem_free(&kernel_map, base, size);
3859 }
3860
3861 /*
3862  * perform the pmap work for mincore
3863  */
3864 int
3865 pmap_mincore(pmap_t pmap, vm_offset_t addr)
3866 READY0
3867 {
3868         pt_entry_t *ptep, pte;
3869         vm_page_t m;
3870         int val = 0;
3871         
3872         ptep = pmap_pte(pmap, addr);
3873         if (ptep == 0) {
3874                 return 0;
3875         }
3876
3877         if ((pte = *ptep) != 0) {
3878                 vm_offset_t pa;
3879
3880                 val = MINCORE_INCORE;
3881                 if ((pte & PG_MANAGED) == 0)
3882                         return val;
3883
3884                 pa = pte & PG_FRAME;
3885
3886                 m = PHYS_TO_VM_PAGE(pa);
3887
3888                 /*
3889                  * Modified by us
3890                  */
3891                 if (pte & PG_M)
3892                         val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3893                 /*
3894                  * Modified by someone
3895                  */
3896                 else if (m->dirty || pmap_is_modified(m))
3897                         val |= MINCORE_MODIFIED_OTHER;
3898                 /*
3899                  * Referenced by us
3900                  */
3901                 if (pte & PG_A)
3902                         val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3903
3904                 /*
3905                  * Referenced by someone
3906                  */
3907                 else if ((m->flags & PG_REFERENCED) || pmap_ts_referenced(m)) {
3908                         val |= MINCORE_REFERENCED_OTHER;
3909                         vm_page_flag_set(m, PG_REFERENCED);
3910                 }
3911         } 
3912         return val;
3913 }
3914
3915 /*
3916  * Replace p->p_vmspace with a new one.  If adjrefs is non-zero the new
3917  * vmspace will be ref'd and the old one will be deref'd.
3918  *
3919  * The vmspace for all lwps associated with the process will be adjusted
3920  * and cr3 will be reloaded if any lwp is the current lwp.
3921  */
3922 void
3923 pmap_replacevm(struct proc *p, struct vmspace *newvm, int adjrefs)
3924 READY2
3925 {
3926         struct vmspace *oldvm;
3927         struct lwp *lp;
3928
3929         crit_enter();
3930         oldvm = p->p_vmspace;
3931         if (oldvm != newvm) {
3932                 p->p_vmspace = newvm;
3933                 KKASSERT(p->p_nthreads == 1);
3934                 lp = RB_ROOT(&p->p_lwp_tree);
3935                 pmap_setlwpvm(lp, newvm);
3936                 if (adjrefs) {
3937                         sysref_get(&newvm->vm_sysref);
3938                         sysref_put(&oldvm->vm_sysref);
3939                 }
3940         }
3941         crit_exit();
3942 }
3943
3944 /*
3945  * Set the vmspace for a LWP.  The vmspace is almost universally set the
3946  * same as the process vmspace, but virtual kernels need to swap out contexts
3947  * on a per-lwp basis.
3948  */
3949 void
3950 pmap_setlwpvm(struct lwp *lp, struct vmspace *newvm)
3951 READY1
3952 {
3953         struct vmspace *oldvm;
3954         struct pmap *pmap;
3955
3956         crit_enter();
3957         oldvm = lp->lwp_vmspace;
3958
3959         if (oldvm != newvm) {
3960                 lp->lwp_vmspace = newvm;
3961                 if (curthread->td_lwp == lp) {
3962                         pmap = vmspace_pmap(newvm);
3963 #if defined(SMP)
3964                         atomic_set_int(&pmap->pm_active, 1 << mycpu->gd_cpuid);
3965 #else
3966                         pmap->pm_active |= 1;
3967 #endif
3968 #if defined(SWTCH_OPTIM_STATS)
3969                         tlb_flush_count++;
3970 #endif
3971                         curthread->td_pcb->pcb_cr3 = vtophys(pmap->pm_pml4);
3972                         load_cr3(curthread->td_pcb->pcb_cr3);
3973                         pmap = vmspace_pmap(oldvm);
3974 #if defined(SMP)
3975                         atomic_clear_int(&pmap->pm_active,
3976                                           1 << mycpu->gd_cpuid);
3977 #else
3978                         pmap->pm_active &= ~1;
3979 #endif
3980                 }
3981         }
3982         crit_exit();
3983 }
3984
3985 vm_offset_t
3986 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3987 READY0
3988 {
3989
3990         if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3991                 return addr;
3992         }
3993
3994         addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3995         return addr;
3996 }
3997
3998
3999 #if defined(DEBUG)
4000
4001 static void     pads (pmap_t pm);
4002 void            pmap_pvdump (vm_paddr_t pa);
4003
4004 /* print address space of pmap*/
4005 static void
4006 pads(pmap_t pm)
4007 READY0
4008 {
4009         vm_offset_t va;
4010         unsigned i, j;
4011         pt_entry_t *ptep;
4012
4013         if (pm == &kernel_pmap)
4014                 return;
4015         crit_enter();
4016         for (i = 0; i < NPDEPG; i++) {
4017 #if JGPMAP32
4018                 if (pm->pm_pdir[i]) {
4019                         for (j = 0; j < NPTEPG; j++) {
4020                                 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
4021                                 if (pm == &kernel_pmap && va < KERNBASE)
4022                                         continue;
4023                                 if (pm != &kernel_pmap && va > UPT_MAX_ADDRESS)
4024                                         continue;
4025                                 ptep = pmap_pte_quick(pm, va);
4026                                 if (pmap_pte_v(ptep))
4027                                         kprintf("%lx:%lx ", va, *ptep);
4028                         };
4029                 }
4030 #endif
4031         }
4032         crit_exit();
4033
4034 }
4035
4036 void
4037 pmap_pvdump(vm_paddr_t pa)
4038 READY0
4039 {
4040         pv_entry_t pv;
4041         vm_page_t m;
4042
4043         kprintf("pa %08llx", (long long)pa);
4044         m = PHYS_TO_VM_PAGE(pa);
4045         TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
4046 #ifdef used_to_be
4047                 kprintf(" -> pmap %p, va %x, flags %x",
4048                     (void *)pv->pv_pmap, pv->pv_va, pv->pv_flags);
4049 #endif
4050                 kprintf(" -> pmap %p, va %x", (void *)pv->pv_pmap, pv->pv_va);
4051                 pads(pv->pv_pmap);
4052         }
4053         kprintf(" ");
4054 }
4055 #endif