]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/amd64/pmap.c
MFC r324665:
[FreeBSD/FreeBSD.git] / sys / amd64 / amd64 / pmap.c
1 /*-
2  * Copyright (c) 1991 Regents of the University of California.
3  * All rights reserved.
4  * Copyright (c) 1994 John S. Dyson
5  * All rights reserved.
6  * Copyright (c) 1994 David Greenman
7  * All rights reserved.
8  * Copyright (c) 2003 Peter Wemm
9  * All rights reserved.
10  * Copyright (c) 2005-2010 Alan L. Cox <alc@cs.rice.edu>
11  * All rights reserved.
12  *
13  * This code is derived from software contributed to Berkeley by
14  * the Systems Programming Group of the University of Utah Computer
15  * Science Department and William Jolitz of UUNET Technologies Inc.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  * 3. All advertising materials mentioning features or use of this software
26  *    must display the following acknowledgement:
27  *      This product includes software developed by the University of
28  *      California, Berkeley and its contributors.
29  * 4. Neither the name of the University nor the names of its contributors
30  *    may be used to endorse or promote products derived from this software
31  *    without specific prior written permission.
32  *
33  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43  * SUCH DAMAGE.
44  *
45  *      from:   @(#)pmap.c      7.7 (Berkeley)  5/12/91
46  */
47 /*-
48  * Copyright (c) 2003 Networks Associates Technology, Inc.
49  * All rights reserved.
50  *
51  * This software was developed for the FreeBSD Project by Jake Burkholder,
52  * Safeport Network Services, and Network Associates Laboratories, the
53  * Security Research Division of Network Associates, Inc. under
54  * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
55  * CHATS research program.
56  *
57  * Redistribution and use in source and binary forms, with or without
58  * modification, are permitted provided that the following conditions
59  * are met:
60  * 1. Redistributions of source code must retain the above copyright
61  *    notice, this list of conditions and the following disclaimer.
62  * 2. Redistributions in binary form must reproduce the above copyright
63  *    notice, this list of conditions and the following disclaimer in the
64  *    documentation and/or other materials provided with the distribution.
65  *
66  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
67  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
68  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
69  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
70  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
71  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
72  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
73  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
74  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
75  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
76  * SUCH DAMAGE.
77  */
78
79 #define AMD64_NPT_AWARE
80
81 #include <sys/cdefs.h>
82 __FBSDID("$FreeBSD$");
83
84 /*
85  *      Manages physical address maps.
86  *
87  *      Since the information managed by this module is
88  *      also stored by the logical address mapping module,
89  *      this module may throw away valid virtual-to-physical
90  *      mappings at almost any time.  However, invalidations
91  *      of virtual-to-physical mappings must be done as
92  *      requested.
93  *
94  *      In order to cope with hardware architectures which
95  *      make virtual-to-physical map invalidates expensive,
96  *      this module may delay invalidate or reduced protection
97  *      operations until such time as they are actually
98  *      necessary.  This module is given full information as
99  *      to which processors are currently using which maps,
100  *      and to when physical maps must be made correct.
101  */
102
103 #include "opt_pmap.h"
104 #include "opt_vm.h"
105
106 #include <sys/param.h>
107 #include <sys/bitstring.h>
108 #include <sys/bus.h>
109 #include <sys/systm.h>
110 #include <sys/kernel.h>
111 #include <sys/ktr.h>
112 #include <sys/lock.h>
113 #include <sys/malloc.h>
114 #include <sys/mman.h>
115 #include <sys/mutex.h>
116 #include <sys/proc.h>
117 #include <sys/rwlock.h>
118 #include <sys/sx.h>
119 #include <sys/turnstile.h>
120 #include <sys/vmem.h>
121 #include <sys/vmmeter.h>
122 #include <sys/sched.h>
123 #include <sys/sysctl.h>
124 #include <sys/smp.h>
125
126 #include <vm/vm.h>
127 #include <vm/vm_param.h>
128 #include <vm/vm_kern.h>
129 #include <vm/vm_page.h>
130 #include <vm/vm_map.h>
131 #include <vm/vm_object.h>
132 #include <vm/vm_extern.h>
133 #include <vm/vm_pageout.h>
134 #include <vm/vm_pager.h>
135 #include <vm/vm_phys.h>
136 #include <vm/vm_radix.h>
137 #include <vm/vm_reserv.h>
138 #include <vm/uma.h>
139
140 #include <machine/intr_machdep.h>
141 #include <x86/apicvar.h>
142 #include <machine/cpu.h>
143 #include <machine/cputypes.h>
144 #include <machine/md_var.h>
145 #include <machine/pcb.h>
146 #include <machine/specialreg.h>
147 #ifdef SMP
148 #include <machine/smp.h>
149 #endif
150
151 static __inline boolean_t
152 pmap_type_guest(pmap_t pmap)
153 {
154
155         return ((pmap->pm_type == PT_EPT) || (pmap->pm_type == PT_RVI));
156 }
157
158 static __inline boolean_t
159 pmap_emulate_ad_bits(pmap_t pmap)
160 {
161
162         return ((pmap->pm_flags & PMAP_EMULATE_AD_BITS) != 0);
163 }
164
165 static __inline pt_entry_t
166 pmap_valid_bit(pmap_t pmap)
167 {
168         pt_entry_t mask;
169
170         switch (pmap->pm_type) {
171         case PT_X86:
172         case PT_RVI:
173                 mask = X86_PG_V;
174                 break;
175         case PT_EPT:
176                 if (pmap_emulate_ad_bits(pmap))
177                         mask = EPT_PG_EMUL_V;
178                 else
179                         mask = EPT_PG_READ;
180                 break;
181         default:
182                 panic("pmap_valid_bit: invalid pm_type %d", pmap->pm_type);
183         }
184
185         return (mask);
186 }
187
188 static __inline pt_entry_t
189 pmap_rw_bit(pmap_t pmap)
190 {
191         pt_entry_t mask;
192
193         switch (pmap->pm_type) {
194         case PT_X86:
195         case PT_RVI:
196                 mask = X86_PG_RW;
197                 break;
198         case PT_EPT:
199                 if (pmap_emulate_ad_bits(pmap))
200                         mask = EPT_PG_EMUL_RW;
201                 else
202                         mask = EPT_PG_WRITE;
203                 break;
204         default:
205                 panic("pmap_rw_bit: invalid pm_type %d", pmap->pm_type);
206         }
207
208         return (mask);
209 }
210
211 static __inline pt_entry_t
212 pmap_global_bit(pmap_t pmap)
213 {
214         pt_entry_t mask;
215
216         switch (pmap->pm_type) {
217         case PT_X86:
218                 mask = X86_PG_G;
219                 break;
220         case PT_RVI:
221         case PT_EPT:
222                 mask = 0;
223                 break;
224         default:
225                 panic("pmap_global_bit: invalid pm_type %d", pmap->pm_type);
226         }
227
228         return (mask);
229 }
230
231 static __inline pt_entry_t
232 pmap_accessed_bit(pmap_t pmap)
233 {
234         pt_entry_t mask;
235
236         switch (pmap->pm_type) {
237         case PT_X86:
238         case PT_RVI:
239                 mask = X86_PG_A;
240                 break;
241         case PT_EPT:
242                 if (pmap_emulate_ad_bits(pmap))
243                         mask = EPT_PG_READ;
244                 else
245                         mask = EPT_PG_A;
246                 break;
247         default:
248                 panic("pmap_accessed_bit: invalid pm_type %d", pmap->pm_type);
249         }
250
251         return (mask);
252 }
253
254 static __inline pt_entry_t
255 pmap_modified_bit(pmap_t pmap)
256 {
257         pt_entry_t mask;
258
259         switch (pmap->pm_type) {
260         case PT_X86:
261         case PT_RVI:
262                 mask = X86_PG_M;
263                 break;
264         case PT_EPT:
265                 if (pmap_emulate_ad_bits(pmap))
266                         mask = EPT_PG_WRITE;
267                 else
268                         mask = EPT_PG_M;
269                 break;
270         default:
271                 panic("pmap_modified_bit: invalid pm_type %d", pmap->pm_type);
272         }
273
274         return (mask);
275 }
276
277 extern  struct pcpu __pcpu[];
278
279 #if !defined(DIAGNOSTIC)
280 #ifdef __GNUC_GNU_INLINE__
281 #define PMAP_INLINE     __attribute__((__gnu_inline__)) inline
282 #else
283 #define PMAP_INLINE     extern inline
284 #endif
285 #else
286 #define PMAP_INLINE
287 #endif
288
289 #ifdef PV_STATS
290 #define PV_STAT(x)      do { x ; } while (0)
291 #else
292 #define PV_STAT(x)      do { } while (0)
293 #endif
294
295 #define pa_index(pa)    ((pa) >> PDRSHIFT)
296 #define pa_to_pvh(pa)   (&pv_table[pa_index(pa)])
297
298 #define NPV_LIST_LOCKS  MAXCPU
299
300 #define PHYS_TO_PV_LIST_LOCK(pa)        \
301                         (&pv_list_locks[pa_index(pa) % NPV_LIST_LOCKS])
302
303 #define CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, pa)  do {    \
304         struct rwlock **_lockp = (lockp);               \
305         struct rwlock *_new_lock;                       \
306                                                         \
307         _new_lock = PHYS_TO_PV_LIST_LOCK(pa);           \
308         if (_new_lock != *_lockp) {                     \
309                 if (*_lockp != NULL)                    \
310                         rw_wunlock(*_lockp);            \
311                 *_lockp = _new_lock;                    \
312                 rw_wlock(*_lockp);                      \
313         }                                               \
314 } while (0)
315
316 #define CHANGE_PV_LIST_LOCK_TO_VM_PAGE(lockp, m)        \
317                         CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, VM_PAGE_TO_PHYS(m))
318
319 #define RELEASE_PV_LIST_LOCK(lockp)             do {    \
320         struct rwlock **_lockp = (lockp);               \
321                                                         \
322         if (*_lockp != NULL) {                          \
323                 rw_wunlock(*_lockp);                    \
324                 *_lockp = NULL;                         \
325         }                                               \
326 } while (0)
327
328 #define VM_PAGE_TO_PV_LIST_LOCK(m)      \
329                         PHYS_TO_PV_LIST_LOCK(VM_PAGE_TO_PHYS(m))
330
331 struct pmap kernel_pmap_store;
332
333 vm_offset_t virtual_avail;      /* VA of first avail page (after kernel bss) */
334 vm_offset_t virtual_end;        /* VA of last avail page (end of kernel AS) */
335
336 int nkpt;
337 SYSCTL_INT(_machdep, OID_AUTO, nkpt, CTLFLAG_RD, &nkpt, 0,
338     "Number of kernel page table pages allocated on bootup");
339
340 static int ndmpdp;
341 vm_paddr_t dmaplimit;
342 vm_offset_t kernel_vm_end = VM_MIN_KERNEL_ADDRESS;
343 pt_entry_t pg_nx;
344
345 static SYSCTL_NODE(_vm, OID_AUTO, pmap, CTLFLAG_RD, 0, "VM/pmap parameters");
346
347 static int pat_works = 1;
348 SYSCTL_INT(_vm_pmap, OID_AUTO, pat_works, CTLFLAG_RD, &pat_works, 1,
349     "Is page attribute table fully functional?");
350
351 static int pg_ps_enabled = 1;
352 SYSCTL_INT(_vm_pmap, OID_AUTO, pg_ps_enabled, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
353     &pg_ps_enabled, 0, "Are large page mappings enabled?");
354
355 #define PAT_INDEX_SIZE  8
356 static int pat_index[PAT_INDEX_SIZE];   /* cache mode to PAT index conversion */
357
358 static u_int64_t        KPTphys;        /* phys addr of kernel level 1 */
359 static u_int64_t        KPDphys;        /* phys addr of kernel level 2 */
360 u_int64_t               KPDPphys;       /* phys addr of kernel level 3 */
361 u_int64_t               KPML4phys;      /* phys addr of kernel level 4 */
362
363 static u_int64_t        DMPDphys;       /* phys addr of direct mapped level 2 */
364 static u_int64_t        DMPDPphys;      /* phys addr of direct mapped level 3 */
365 static int              ndmpdpphys;     /* number of DMPDPphys pages */
366
367 /*
368  * pmap_mapdev support pre initialization (i.e. console)
369  */
370 #define PMAP_PREINIT_MAPPING_COUNT      8
371 static struct pmap_preinit_mapping {
372         vm_paddr_t      pa;
373         vm_offset_t     va;
374         vm_size_t       sz;
375         int             mode;
376 } pmap_preinit_mapping[PMAP_PREINIT_MAPPING_COUNT];
377 static int pmap_initialized;
378
379 /*
380  * Data for the pv entry allocation mechanism.
381  * Updates to pv_invl_gen are protected by the pv_list_locks[]
382  * elements, but reads are not.
383  */
384 static TAILQ_HEAD(pch, pv_chunk) pv_chunks = TAILQ_HEAD_INITIALIZER(pv_chunks);
385 static struct mtx pv_chunks_mutex;
386 static struct rwlock pv_list_locks[NPV_LIST_LOCKS];
387 static u_long pv_invl_gen[NPV_LIST_LOCKS];
388 static struct md_page *pv_table;
389 static struct md_page pv_dummy;
390
391 /*
392  * All those kernel PT submaps that BSD is so fond of
393  */
394 pt_entry_t *CMAP1 = NULL;
395 caddr_t CADDR1 = 0;
396 static vm_offset_t qframe = 0;
397 static struct mtx qframe_mtx;
398
399 static int pmap_flags = PMAP_PDE_SUPERPAGE;     /* flags for x86 pmaps */
400
401 int pmap_pcid_enabled = 1;
402 SYSCTL_INT(_vm_pmap, OID_AUTO, pcid_enabled, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
403     &pmap_pcid_enabled, 0, "Is TLB Context ID enabled ?");
404 int invpcid_works = 0;
405 SYSCTL_INT(_vm_pmap, OID_AUTO, invpcid_works, CTLFLAG_RD, &invpcid_works, 0,
406     "Is the invpcid instruction available ?");
407
408 static int
409 pmap_pcid_save_cnt_proc(SYSCTL_HANDLER_ARGS)
410 {
411         int i;
412         uint64_t res;
413
414         res = 0;
415         CPU_FOREACH(i) {
416                 res += cpuid_to_pcpu[i]->pc_pm_save_cnt;
417         }
418         return (sysctl_handle_64(oidp, &res, 0, req));
419 }
420 SYSCTL_PROC(_vm_pmap, OID_AUTO, pcid_save_cnt, CTLTYPE_U64 | CTLFLAG_RW |
421     CTLFLAG_MPSAFE, NULL, 0, pmap_pcid_save_cnt_proc, "QU",
422     "Count of saved TLB context on switch");
423
424 static LIST_HEAD(, pmap_invl_gen) pmap_invl_gen_tracker =
425     LIST_HEAD_INITIALIZER(&pmap_invl_gen_tracker);
426 static struct mtx invl_gen_mtx;
427 static u_long pmap_invl_gen = 0;
428 /* Fake lock object to satisfy turnstiles interface. */
429 static struct lock_object invl_gen_ts = {
430         .lo_name = "invlts",
431 };
432
433 static bool
434 pmap_not_in_di(void)
435 {
436
437         return (curthread->td_md.md_invl_gen.gen == 0);
438 }
439
440 #define PMAP_ASSERT_NOT_IN_DI() \
441     KASSERT(pmap_not_in_di(), ("DI already started"))
442
443 /*
444  * Start a new Delayed Invalidation (DI) block of code, executed by
445  * the current thread.  Within a DI block, the current thread may
446  * destroy both the page table and PV list entries for a mapping and
447  * then release the corresponding PV list lock before ensuring that
448  * the mapping is flushed from the TLBs of any processors with the
449  * pmap active.
450  */
451 static void
452 pmap_delayed_invl_started(void)
453 {
454         struct pmap_invl_gen *invl_gen;
455         u_long currgen;
456
457         invl_gen = &curthread->td_md.md_invl_gen;
458         PMAP_ASSERT_NOT_IN_DI();
459         mtx_lock(&invl_gen_mtx);
460         if (LIST_EMPTY(&pmap_invl_gen_tracker))
461                 currgen = pmap_invl_gen;
462         else
463                 currgen = LIST_FIRST(&pmap_invl_gen_tracker)->gen;
464         invl_gen->gen = currgen + 1;
465         LIST_INSERT_HEAD(&pmap_invl_gen_tracker, invl_gen, link);
466         mtx_unlock(&invl_gen_mtx);
467 }
468
469 /*
470  * Finish the DI block, previously started by the current thread.  All
471  * required TLB flushes for the pages marked by
472  * pmap_delayed_invl_page() must be finished before this function is
473  * called.
474  *
475  * This function works by bumping the global DI generation number to
476  * the generation number of the current thread's DI, unless there is a
477  * pending DI that started earlier.  In the latter case, bumping the
478  * global DI generation number would incorrectly signal that the
479  * earlier DI had finished.  Instead, this function bumps the earlier
480  * DI's generation number to match the generation number of the
481  * current thread's DI.
482  */
483 static void
484 pmap_delayed_invl_finished(void)
485 {
486         struct pmap_invl_gen *invl_gen, *next;
487         struct turnstile *ts;
488
489         invl_gen = &curthread->td_md.md_invl_gen;
490         KASSERT(invl_gen->gen != 0, ("missed invl_started"));
491         mtx_lock(&invl_gen_mtx);
492         next = LIST_NEXT(invl_gen, link);
493         if (next == NULL) {
494                 turnstile_chain_lock(&invl_gen_ts);
495                 ts = turnstile_lookup(&invl_gen_ts);
496                 pmap_invl_gen = invl_gen->gen;
497                 if (ts != NULL) {
498                         turnstile_broadcast(ts, TS_SHARED_QUEUE);
499                         turnstile_unpend(ts, TS_SHARED_LOCK);
500                 }
501                 turnstile_chain_unlock(&invl_gen_ts);
502         } else {
503                 next->gen = invl_gen->gen;
504         }
505         LIST_REMOVE(invl_gen, link);
506         mtx_unlock(&invl_gen_mtx);
507         invl_gen->gen = 0;
508 }
509
510 #ifdef PV_STATS
511 static long invl_wait;
512 SYSCTL_LONG(_vm_pmap, OID_AUTO, invl_wait, CTLFLAG_RD, &invl_wait, 0,
513     "Number of times DI invalidation blocked pmap_remove_all/write");
514 #endif
515
516 static u_long *
517 pmap_delayed_invl_genp(vm_page_t m)
518 {
519
520         return (&pv_invl_gen[pa_index(VM_PAGE_TO_PHYS(m)) % NPV_LIST_LOCKS]);
521 }
522
523 /*
524  * Ensure that all currently executing DI blocks, that need to flush
525  * TLB for the given page m, actually flushed the TLB at the time the
526  * function returned.  If the page m has an empty PV list and we call
527  * pmap_delayed_invl_wait(), upon its return we know that no CPU has a
528  * valid mapping for the page m in either its page table or TLB.
529  *
530  * This function works by blocking until the global DI generation
531  * number catches up with the generation number associated with the
532  * given page m and its PV list.  Since this function's callers
533  * typically own an object lock and sometimes own a page lock, it
534  * cannot sleep.  Instead, it blocks on a turnstile to relinquish the
535  * processor.
536  */
537 static void
538 pmap_delayed_invl_wait(vm_page_t m)
539 {
540         struct thread *td;
541         struct turnstile *ts;
542         u_long *m_gen;
543 #ifdef PV_STATS
544         bool accounted = false;
545 #endif
546
547         td = curthread;
548         m_gen = pmap_delayed_invl_genp(m);
549         while (*m_gen > pmap_invl_gen) {
550 #ifdef PV_STATS
551                 if (!accounted) {
552                         atomic_add_long(&invl_wait, 1);
553                         accounted = true;
554                 }
555 #endif
556                 ts = turnstile_trywait(&invl_gen_ts);
557                 if (*m_gen > pmap_invl_gen)
558                         turnstile_wait(ts, NULL, TS_SHARED_QUEUE);
559                 else
560                         turnstile_cancel(ts);
561         }
562 }
563
564 /*
565  * Mark the page m's PV list as participating in the current thread's
566  * DI block.  Any threads concurrently using m's PV list to remove or
567  * restrict all mappings to m will wait for the current thread's DI
568  * block to complete before proceeding.
569  *
570  * The function works by setting the DI generation number for m's PV
571  * list to at least the DI generation number of the current thread.
572  * This forces a caller of pmap_delayed_invl_wait() to block until
573  * current thread calls pmap_delayed_invl_finished().
574  */
575 static void
576 pmap_delayed_invl_page(vm_page_t m)
577 {
578         u_long gen, *m_gen;
579
580         rw_assert(VM_PAGE_TO_PV_LIST_LOCK(m), RA_WLOCKED);
581         gen = curthread->td_md.md_invl_gen.gen;
582         if (gen == 0)
583                 return;
584         m_gen = pmap_delayed_invl_genp(m);
585         if (*m_gen < gen)
586                 *m_gen = gen;
587 }
588
589 /*
590  * Crashdump maps.
591  */
592 static caddr_t crashdumpmap;
593
594 /*
595  * Internal flags for pmap_enter()'s helper functions.
596  */
597 #define PMAP_ENTER_NORECLAIM    0x1000000       /* Don't reclaim PV entries. */
598 #define PMAP_ENTER_NOREPLACE    0x2000000       /* Don't replace mappings. */
599
600 static void     free_pv_chunk(struct pv_chunk *pc);
601 static void     free_pv_entry(pmap_t pmap, pv_entry_t pv);
602 static pv_entry_t get_pv_entry(pmap_t pmap, struct rwlock **lockp);
603 static int      popcnt_pc_map_pq(uint64_t *map);
604 static vm_page_t reclaim_pv_chunk(pmap_t locked_pmap, struct rwlock **lockp);
605 static void     reserve_pv_entries(pmap_t pmap, int needed,
606                     struct rwlock **lockp);
607 static void     pmap_pv_demote_pde(pmap_t pmap, vm_offset_t va, vm_paddr_t pa,
608                     struct rwlock **lockp);
609 static bool     pmap_pv_insert_pde(pmap_t pmap, vm_offset_t va, pd_entry_t pde,
610                     u_int flags, struct rwlock **lockp);
611 static void     pmap_pv_promote_pde(pmap_t pmap, vm_offset_t va, vm_paddr_t pa,
612                     struct rwlock **lockp);
613 static void     pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va);
614 static pv_entry_t pmap_pvh_remove(struct md_page *pvh, pmap_t pmap,
615                     vm_offset_t va);
616
617 static int pmap_change_attr_locked(vm_offset_t va, vm_size_t size, int mode);
618 static boolean_t pmap_demote_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va);
619 static boolean_t pmap_demote_pde_locked(pmap_t pmap, pd_entry_t *pde,
620     vm_offset_t va, struct rwlock **lockp);
621 static boolean_t pmap_demote_pdpe(pmap_t pmap, pdp_entry_t *pdpe,
622     vm_offset_t va);
623 static bool     pmap_enter_2mpage(pmap_t pmap, vm_offset_t va, vm_page_t m,
624                     vm_prot_t prot, struct rwlock **lockp);
625 static int      pmap_enter_pde(pmap_t pmap, vm_offset_t va, pd_entry_t newpde,
626                     u_int flags, vm_page_t m, struct rwlock **lockp);
627 static vm_page_t pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va,
628     vm_page_t m, vm_prot_t prot, vm_page_t mpte, struct rwlock **lockp);
629 static void pmap_fill_ptp(pt_entry_t *firstpte, pt_entry_t newpte);
630 static int pmap_insert_pt_page(pmap_t pmap, vm_page_t mpte);
631 static void pmap_invalidate_pde_page(pmap_t pmap, vm_offset_t va,
632                     pd_entry_t pde);
633 static void pmap_kenter_attr(vm_offset_t va, vm_paddr_t pa, int mode);
634 static void pmap_pde_attr(pd_entry_t *pde, int cache_bits, int mask);
635 static void pmap_promote_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va,
636     struct rwlock **lockp);
637 static boolean_t pmap_protect_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t sva,
638     vm_prot_t prot);
639 static void pmap_pte_attr(pt_entry_t *pte, int cache_bits, int mask);
640 static int pmap_remove_pde(pmap_t pmap, pd_entry_t *pdq, vm_offset_t sva,
641     struct spglist *free, struct rwlock **lockp);
642 static int pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t sva,
643     pd_entry_t ptepde, struct spglist *free, struct rwlock **lockp);
644 static vm_page_t pmap_remove_pt_page(pmap_t pmap, vm_offset_t va);
645 static void pmap_remove_page(pmap_t pmap, vm_offset_t va, pd_entry_t *pde,
646     struct spglist *free);
647 static bool     pmap_remove_ptes(pmap_t pmap, vm_offset_t sva, vm_offset_t eva,
648                     pd_entry_t *pde, struct spglist *free,
649                     struct rwlock **lockp);
650 static boolean_t pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va,
651     vm_page_t m, struct rwlock **lockp);
652 static void pmap_update_pde(pmap_t pmap, vm_offset_t va, pd_entry_t *pde,
653     pd_entry_t newpde);
654 static void pmap_update_pde_invalidate(pmap_t, vm_offset_t va, pd_entry_t pde);
655
656 static vm_page_t _pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex,
657                 struct rwlock **lockp);
658 static vm_page_t pmap_allocpde(pmap_t pmap, vm_offset_t va,
659                 struct rwlock **lockp);
660 static vm_page_t pmap_allocpte(pmap_t pmap, vm_offset_t va,
661                 struct rwlock **lockp);
662
663 static void _pmap_unwire_ptp(pmap_t pmap, vm_offset_t va, vm_page_t m,
664     struct spglist *free);
665 static int pmap_unuse_pt(pmap_t, vm_offset_t, pd_entry_t, struct spglist *);
666 static vm_offset_t pmap_kmem_choose(vm_offset_t addr);
667
668 /*
669  * Move the kernel virtual free pointer to the next
670  * 2MB.  This is used to help improve performance
671  * by using a large (2MB) page for much of the kernel
672  * (.text, .data, .bss)
673  */
674 static vm_offset_t
675 pmap_kmem_choose(vm_offset_t addr)
676 {
677         vm_offset_t newaddr = addr;
678
679         newaddr = roundup2(addr, NBPDR);
680         return (newaddr);
681 }
682
683 /********************/
684 /* Inline functions */
685 /********************/
686
687 /* Return a non-clipped PD index for a given VA */
688 static __inline vm_pindex_t
689 pmap_pde_pindex(vm_offset_t va)
690 {
691         return (va >> PDRSHIFT);
692 }
693
694
695 /* Return a pointer to the PML4 slot that corresponds to a VA */
696 static __inline pml4_entry_t *
697 pmap_pml4e(pmap_t pmap, vm_offset_t va)
698 {
699
700         return (&pmap->pm_pml4[pmap_pml4e_index(va)]);
701 }
702
703 /* Return a pointer to the PDP slot that corresponds to a VA */
704 static __inline pdp_entry_t *
705 pmap_pml4e_to_pdpe(pml4_entry_t *pml4e, vm_offset_t va)
706 {
707         pdp_entry_t *pdpe;
708
709         pdpe = (pdp_entry_t *)PHYS_TO_DMAP(*pml4e & PG_FRAME);
710         return (&pdpe[pmap_pdpe_index(va)]);
711 }
712
713 /* Return a pointer to the PDP slot that corresponds to a VA */
714 static __inline pdp_entry_t *
715 pmap_pdpe(pmap_t pmap, vm_offset_t va)
716 {
717         pml4_entry_t *pml4e;
718         pt_entry_t PG_V;
719
720         PG_V = pmap_valid_bit(pmap);
721         pml4e = pmap_pml4e(pmap, va);
722         if ((*pml4e & PG_V) == 0)
723                 return (NULL);
724         return (pmap_pml4e_to_pdpe(pml4e, va));
725 }
726
727 /* Return a pointer to the PD slot that corresponds to a VA */
728 static __inline pd_entry_t *
729 pmap_pdpe_to_pde(pdp_entry_t *pdpe, vm_offset_t va)
730 {
731         pd_entry_t *pde;
732
733         pde = (pd_entry_t *)PHYS_TO_DMAP(*pdpe & PG_FRAME);
734         return (&pde[pmap_pde_index(va)]);
735 }
736
737 /* Return a pointer to the PD slot that corresponds to a VA */
738 static __inline pd_entry_t *
739 pmap_pde(pmap_t pmap, vm_offset_t va)
740 {
741         pdp_entry_t *pdpe;
742         pt_entry_t PG_V;
743
744         PG_V = pmap_valid_bit(pmap);
745         pdpe = pmap_pdpe(pmap, va);
746         if (pdpe == NULL || (*pdpe & PG_V) == 0)
747                 return (NULL);
748         return (pmap_pdpe_to_pde(pdpe, va));
749 }
750
751 /* Return a pointer to the PT slot that corresponds to a VA */
752 static __inline pt_entry_t *
753 pmap_pde_to_pte(pd_entry_t *pde, vm_offset_t va)
754 {
755         pt_entry_t *pte;
756
757         pte = (pt_entry_t *)PHYS_TO_DMAP(*pde & PG_FRAME);
758         return (&pte[pmap_pte_index(va)]);
759 }
760
761 /* Return a pointer to the PT slot that corresponds to a VA */
762 static __inline pt_entry_t *
763 pmap_pte(pmap_t pmap, vm_offset_t va)
764 {
765         pd_entry_t *pde;
766         pt_entry_t PG_V;
767
768         PG_V = pmap_valid_bit(pmap);
769         pde = pmap_pde(pmap, va);
770         if (pde == NULL || (*pde & PG_V) == 0)
771                 return (NULL);
772         if ((*pde & PG_PS) != 0)        /* compat with i386 pmap_pte() */
773                 return ((pt_entry_t *)pde);
774         return (pmap_pde_to_pte(pde, va));
775 }
776
777 static __inline void
778 pmap_resident_count_inc(pmap_t pmap, int count)
779 {
780
781         PMAP_LOCK_ASSERT(pmap, MA_OWNED);
782         pmap->pm_stats.resident_count += count;
783 }
784
785 static __inline void
786 pmap_resident_count_dec(pmap_t pmap, int count)
787 {
788
789         PMAP_LOCK_ASSERT(pmap, MA_OWNED);
790         KASSERT(pmap->pm_stats.resident_count >= count,
791             ("pmap %p resident count underflow %ld %d", pmap,
792             pmap->pm_stats.resident_count, count));
793         pmap->pm_stats.resident_count -= count;
794 }
795
796 PMAP_INLINE pt_entry_t *
797 vtopte(vm_offset_t va)
798 {
799         u_int64_t mask = ((1ul << (NPTEPGSHIFT + NPDEPGSHIFT + NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1);
800
801         KASSERT(va >= VM_MAXUSER_ADDRESS, ("vtopte on a uva/gpa 0x%0lx", va));
802
803         return (PTmap + ((va >> PAGE_SHIFT) & mask));
804 }
805
806 static __inline pd_entry_t *
807 vtopde(vm_offset_t va)
808 {
809         u_int64_t mask = ((1ul << (NPDEPGSHIFT + NPDPEPGSHIFT + NPML4EPGSHIFT)) - 1);
810
811         KASSERT(va >= VM_MAXUSER_ADDRESS, ("vtopde on a uva/gpa 0x%0lx", va));
812
813         return (PDmap + ((va >> PDRSHIFT) & mask));
814 }
815
816 static u_int64_t
817 allocpages(vm_paddr_t *firstaddr, int n)
818 {
819         u_int64_t ret;
820
821         ret = *firstaddr;
822         bzero((void *)ret, n * PAGE_SIZE);
823         *firstaddr += n * PAGE_SIZE;
824         return (ret);
825 }
826
827 CTASSERT(powerof2(NDMPML4E));
828
829 /* number of kernel PDP slots */
830 #define NKPDPE(ptpgs)           howmany(ptpgs, NPDEPG)
831
832 static void
833 nkpt_init(vm_paddr_t addr)
834 {
835         int pt_pages;
836         
837 #ifdef NKPT
838         pt_pages = NKPT;
839 #else
840         pt_pages = howmany(addr, 1 << PDRSHIFT);
841         pt_pages += NKPDPE(pt_pages);
842
843         /*
844          * Add some slop beyond the bare minimum required for bootstrapping
845          * the kernel.
846          *
847          * This is quite important when allocating KVA for kernel modules.
848          * The modules are required to be linked in the negative 2GB of
849          * the address space.  If we run out of KVA in this region then
850          * pmap_growkernel() will need to allocate page table pages to map
851          * the entire 512GB of KVA space which is an unnecessary tax on
852          * physical memory.
853          *
854          * Secondly, device memory mapped as part of setting up the low-
855          * level console(s) is taken from KVA, starting at virtual_avail.
856          * This is because cninit() is called after pmap_bootstrap() but
857          * before vm_init() and pmap_init(). 20MB for a frame buffer is
858          * not uncommon.
859          */
860         pt_pages += 32;         /* 64MB additional slop. */
861 #endif
862         nkpt = pt_pages;
863 }
864
865 static void
866 create_pagetables(vm_paddr_t *firstaddr)
867 {
868         int i, j, ndm1g, nkpdpe;
869         pt_entry_t *pt_p;
870         pd_entry_t *pd_p;
871         pdp_entry_t *pdp_p;
872         pml4_entry_t *p4_p;
873
874         /* Allocate page table pages for the direct map */
875         ndmpdp = howmany(ptoa(Maxmem), NBPDP);
876         if (ndmpdp < 4)         /* Minimum 4GB of dirmap */
877                 ndmpdp = 4;
878         ndmpdpphys = howmany(ndmpdp, NPDPEPG);
879         if (ndmpdpphys > NDMPML4E) {
880                 /*
881                  * Each NDMPML4E allows 512 GB, so limit to that,
882                  * and then readjust ndmpdp and ndmpdpphys.
883                  */
884                 printf("NDMPML4E limits system to %d GB\n", NDMPML4E * 512);
885                 Maxmem = atop(NDMPML4E * NBPML4);
886                 ndmpdpphys = NDMPML4E;
887                 ndmpdp = NDMPML4E * NPDEPG;
888         }
889         DMPDPphys = allocpages(firstaddr, ndmpdpphys);
890         ndm1g = 0;
891         if ((amd_feature & AMDID_PAGE1GB) != 0)
892                 ndm1g = ptoa(Maxmem) >> PDPSHIFT;
893         if (ndm1g < ndmpdp)
894                 DMPDphys = allocpages(firstaddr, ndmpdp - ndm1g);
895         dmaplimit = (vm_paddr_t)ndmpdp << PDPSHIFT;
896
897         /* Allocate pages */
898         KPML4phys = allocpages(firstaddr, 1);
899         KPDPphys = allocpages(firstaddr, NKPML4E);
900
901         /*
902          * Allocate the initial number of kernel page table pages required to
903          * bootstrap.  We defer this until after all memory-size dependent
904          * allocations are done (e.g. direct map), so that we don't have to
905          * build in too much slop in our estimate.
906          *
907          * Note that when NKPML4E > 1, we have an empty page underneath
908          * all but the KPML4I'th one, so we need NKPML4E-1 extra (zeroed)
909          * pages.  (pmap_enter requires a PD page to exist for each KPML4E.)
910          */
911         nkpt_init(*firstaddr);
912         nkpdpe = NKPDPE(nkpt);
913
914         KPTphys = allocpages(firstaddr, nkpt);
915         KPDphys = allocpages(firstaddr, nkpdpe);
916
917         /* Fill in the underlying page table pages */
918         /* Nominally read-only (but really R/W) from zero to physfree */
919         /* XXX not fully used, underneath 2M pages */
920         pt_p = (pt_entry_t *)KPTphys;
921         for (i = 0; ptoa(i) < *firstaddr; i++)
922                 pt_p[i] = ptoa(i) | X86_PG_RW | X86_PG_V | X86_PG_G;
923
924         /* Now map the page tables at their location within PTmap */
925         pd_p = (pd_entry_t *)KPDphys;
926         for (i = 0; i < nkpt; i++)
927                 pd_p[i] = (KPTphys + ptoa(i)) | X86_PG_RW | X86_PG_V;
928
929         /* Map from zero to end of allocations under 2M pages */
930         /* This replaces some of the KPTphys entries above */
931         for (i = 0; (i << PDRSHIFT) < *firstaddr; i++)
932                 pd_p[i] = (i << PDRSHIFT) | X86_PG_RW | X86_PG_V | PG_PS |
933                     X86_PG_G;
934
935         /* And connect up the PD to the PDP (leaving room for L4 pages) */
936         pdp_p = (pdp_entry_t *)(KPDPphys + ptoa(KPML4I - KPML4BASE));
937         for (i = 0; i < nkpdpe; i++)
938                 pdp_p[i + KPDPI] = (KPDphys + ptoa(i)) | X86_PG_RW | X86_PG_V |
939                     PG_U;
940
941         /*
942          * Now, set up the direct map region using 2MB and/or 1GB pages.  If
943          * the end of physical memory is not aligned to a 1GB page boundary,
944          * then the residual physical memory is mapped with 2MB pages.  Later,
945          * if pmap_mapdev{_attr}() uses the direct map for non-write-back
946          * memory, pmap_change_attr() will demote any 2MB or 1GB page mappings
947          * that are partially used. 
948          */
949         pd_p = (pd_entry_t *)DMPDphys;
950         for (i = NPDEPG * ndm1g, j = 0; i < NPDEPG * ndmpdp; i++, j++) {
951                 pd_p[j] = (vm_paddr_t)i << PDRSHIFT;
952                 /* Preset PG_M and PG_A because demotion expects it. */
953                 pd_p[j] |= X86_PG_RW | X86_PG_V | PG_PS | X86_PG_G |
954                     X86_PG_M | X86_PG_A;
955         }
956         pdp_p = (pdp_entry_t *)DMPDPphys;
957         for (i = 0; i < ndm1g; i++) {
958                 pdp_p[i] = (vm_paddr_t)i << PDPSHIFT;
959                 /* Preset PG_M and PG_A because demotion expects it. */
960                 pdp_p[i] |= X86_PG_RW | X86_PG_V | PG_PS | X86_PG_G |
961                     X86_PG_M | X86_PG_A;
962         }
963         for (j = 0; i < ndmpdp; i++, j++) {
964                 pdp_p[i] = DMPDphys + ptoa(j);
965                 pdp_p[i] |= X86_PG_RW | X86_PG_V | PG_U;
966         }
967
968         /* And recursively map PML4 to itself in order to get PTmap */
969         p4_p = (pml4_entry_t *)KPML4phys;
970         p4_p[PML4PML4I] = KPML4phys;
971         p4_p[PML4PML4I] |= X86_PG_RW | X86_PG_V | PG_U;
972
973         /* Connect the Direct Map slot(s) up to the PML4. */
974         for (i = 0; i < ndmpdpphys; i++) {
975                 p4_p[DMPML4I + i] = DMPDPphys + ptoa(i);
976                 p4_p[DMPML4I + i] |= X86_PG_RW | X86_PG_V | PG_U;
977         }
978
979         /* Connect the KVA slots up to the PML4 */
980         for (i = 0; i < NKPML4E; i++) {
981                 p4_p[KPML4BASE + i] = KPDPphys + ptoa(i);
982                 p4_p[KPML4BASE + i] |= X86_PG_RW | X86_PG_V | PG_U;
983         }
984 }
985
986 /*
987  *      Bootstrap the system enough to run with virtual memory.
988  *
989  *      On amd64 this is called after mapping has already been enabled
990  *      and just syncs the pmap module with what has already been done.
991  *      [We can't call it easily with mapping off since the kernel is not
992  *      mapped with PA == VA, hence we would have to relocate every address
993  *      from the linked base (virtual) address "KERNBASE" to the actual
994  *      (physical) address starting relative to 0]
995  */
996 void
997 pmap_bootstrap(vm_paddr_t *firstaddr)
998 {
999         vm_offset_t va;
1000         pt_entry_t *pte;
1001         int i;
1002
1003         /*
1004          * Create an initial set of page tables to run the kernel in.
1005          */
1006         create_pagetables(firstaddr);
1007
1008         /*
1009          * Add a physical memory segment (vm_phys_seg) corresponding to the
1010          * preallocated kernel page table pages so that vm_page structures
1011          * representing these pages will be created.  The vm_page structures
1012          * are required for promotion of the corresponding kernel virtual
1013          * addresses to superpage mappings.
1014          */
1015         vm_phys_add_seg(KPTphys, KPTphys + ptoa(nkpt));
1016
1017         virtual_avail = (vm_offset_t) KERNBASE + *firstaddr;
1018         virtual_avail = pmap_kmem_choose(virtual_avail);
1019
1020         virtual_end = VM_MAX_KERNEL_ADDRESS;
1021
1022
1023         /* XXX do %cr0 as well */
1024         load_cr4(rcr4() | CR4_PGE);
1025         load_cr3(KPML4phys);
1026         if (cpu_stdext_feature & CPUID_STDEXT_SMEP)
1027                 load_cr4(rcr4() | CR4_SMEP);
1028
1029         /*
1030          * Initialize the kernel pmap (which is statically allocated).
1031          */
1032         PMAP_LOCK_INIT(kernel_pmap);
1033         kernel_pmap->pm_pml4 = (pdp_entry_t *)PHYS_TO_DMAP(KPML4phys);
1034         kernel_pmap->pm_cr3 = KPML4phys;
1035         CPU_FILL(&kernel_pmap->pm_active);      /* don't allow deactivation */
1036         TAILQ_INIT(&kernel_pmap->pm_pvchunk);
1037         kernel_pmap->pm_flags = pmap_flags;
1038
1039         /*
1040          * Initialize the TLB invalidations generation number lock.
1041          */
1042         mtx_init(&invl_gen_mtx, "invlgn", NULL, MTX_DEF);
1043
1044         /*
1045          * Reserve some special page table entries/VA space for temporary
1046          * mapping of pages.
1047          */
1048 #define SYSMAP(c, p, v, n)      \
1049         v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
1050
1051         va = virtual_avail;
1052         pte = vtopte(va);
1053
1054         /*
1055          * Crashdump maps.  The first page is reused as CMAP1 for the
1056          * memory test.
1057          */
1058         SYSMAP(caddr_t, CMAP1, crashdumpmap, MAXDUMPPGS)
1059         CADDR1 = crashdumpmap;
1060
1061         virtual_avail = va;
1062
1063         /*
1064          * Initialize the PAT MSR.
1065          * pmap_init_pat() clears and sets CR4_PGE, which, as a
1066          * side-effect, invalidates stale PG_G TLB entries that might
1067          * have been created in our pre-boot environment.
1068          */
1069         pmap_init_pat();
1070
1071         /* Initialize TLB Context Id. */
1072         TUNABLE_INT_FETCH("vm.pmap.pcid_enabled", &pmap_pcid_enabled);
1073         if ((cpu_feature2 & CPUID2_PCID) != 0 && pmap_pcid_enabled) {
1074                 /* Check for INVPCID support */
1075                 invpcid_works = (cpu_stdext_feature & CPUID_STDEXT_INVPCID)
1076                     != 0;
1077                 for (i = 0; i < MAXCPU; i++) {
1078                         kernel_pmap->pm_pcids[i].pm_pcid = PMAP_PCID_KERN;
1079                         kernel_pmap->pm_pcids[i].pm_gen = 1;
1080                 }
1081                 __pcpu[0].pc_pcid_next = PMAP_PCID_KERN + 1;
1082                 __pcpu[0].pc_pcid_gen = 1;
1083                 /*
1084                  * pcpu area for APs is zeroed during AP startup.
1085                  * pc_pcid_next and pc_pcid_gen are initialized by AP
1086                  * during pcpu setup.
1087                  */
1088                 load_cr4(rcr4() | CR4_PCIDE);
1089         } else {
1090                 pmap_pcid_enabled = 0;
1091         }
1092 }
1093
1094 /*
1095  * Setup the PAT MSR.
1096  */
1097 void
1098 pmap_init_pat(void)
1099 {
1100         int pat_table[PAT_INDEX_SIZE];
1101         uint64_t pat_msr;
1102         u_long cr0, cr4;
1103         int i;
1104
1105         /* Bail if this CPU doesn't implement PAT. */
1106         if ((cpu_feature & CPUID_PAT) == 0)
1107                 panic("no PAT??");
1108
1109         /* Set default PAT index table. */
1110         for (i = 0; i < PAT_INDEX_SIZE; i++)
1111                 pat_table[i] = -1;
1112         pat_table[PAT_WRITE_BACK] = 0;
1113         pat_table[PAT_WRITE_THROUGH] = 1;
1114         pat_table[PAT_UNCACHEABLE] = 3;
1115         pat_table[PAT_WRITE_COMBINING] = 3;
1116         pat_table[PAT_WRITE_PROTECTED] = 3;
1117         pat_table[PAT_UNCACHED] = 3;
1118
1119         /* Initialize default PAT entries. */
1120         pat_msr = PAT_VALUE(0, PAT_WRITE_BACK) |
1121             PAT_VALUE(1, PAT_WRITE_THROUGH) |
1122             PAT_VALUE(2, PAT_UNCACHED) |
1123             PAT_VALUE(3, PAT_UNCACHEABLE) |
1124             PAT_VALUE(4, PAT_WRITE_BACK) |
1125             PAT_VALUE(5, PAT_WRITE_THROUGH) |
1126             PAT_VALUE(6, PAT_UNCACHED) |
1127             PAT_VALUE(7, PAT_UNCACHEABLE);
1128
1129         if (pat_works) {
1130                 /*
1131                  * Leave the indices 0-3 at the default of WB, WT, UC-, and UC.
1132                  * Program 5 and 6 as WP and WC.
1133                  * Leave 4 and 7 as WB and UC.
1134                  */
1135                 pat_msr &= ~(PAT_MASK(5) | PAT_MASK(6));
1136                 pat_msr |= PAT_VALUE(5, PAT_WRITE_PROTECTED) |
1137                     PAT_VALUE(6, PAT_WRITE_COMBINING);
1138                 pat_table[PAT_UNCACHED] = 2;
1139                 pat_table[PAT_WRITE_PROTECTED] = 5;
1140                 pat_table[PAT_WRITE_COMBINING] = 6;
1141         } else {
1142                 /*
1143                  * Just replace PAT Index 2 with WC instead of UC-.
1144                  */
1145                 pat_msr &= ~PAT_MASK(2);
1146                 pat_msr |= PAT_VALUE(2, PAT_WRITE_COMBINING);
1147                 pat_table[PAT_WRITE_COMBINING] = 2;
1148         }
1149
1150         /* Disable PGE. */
1151         cr4 = rcr4();
1152         load_cr4(cr4 & ~CR4_PGE);
1153
1154         /* Disable caches (CD = 1, NW = 0). */
1155         cr0 = rcr0();
1156         load_cr0((cr0 & ~CR0_NW) | CR0_CD);
1157
1158         /* Flushes caches and TLBs. */
1159         wbinvd();
1160         invltlb();
1161
1162         /* Update PAT and index table. */
1163         wrmsr(MSR_PAT, pat_msr);
1164         for (i = 0; i < PAT_INDEX_SIZE; i++)
1165                 pat_index[i] = pat_table[i];
1166
1167         /* Flush caches and TLBs again. */
1168         wbinvd();
1169         invltlb();
1170
1171         /* Restore caches and PGE. */
1172         load_cr0(cr0);
1173         load_cr4(cr4);
1174 }
1175
1176 /*
1177  *      Initialize a vm_page's machine-dependent fields.
1178  */
1179 void
1180 pmap_page_init(vm_page_t m)
1181 {
1182
1183         TAILQ_INIT(&m->md.pv_list);
1184         m->md.pat_mode = PAT_WRITE_BACK;
1185 }
1186
1187 /*
1188  *      Initialize the pmap module.
1189  *      Called by vm_init, to initialize any structures that the pmap
1190  *      system needs to map virtual memory.
1191  */
1192 void
1193 pmap_init(void)
1194 {
1195         struct pmap_preinit_mapping *ppim;
1196         vm_page_t mpte;
1197         vm_size_t s;
1198         int error, i, pv_npg;
1199
1200         /*
1201          * Initialize the vm page array entries for the kernel pmap's
1202          * page table pages.
1203          */ 
1204         for (i = 0; i < nkpt; i++) {
1205                 mpte = PHYS_TO_VM_PAGE(KPTphys + (i << PAGE_SHIFT));
1206                 KASSERT(mpte >= vm_page_array &&
1207                     mpte < &vm_page_array[vm_page_array_size],
1208                     ("pmap_init: page table page is out of range"));
1209                 mpte->pindex = pmap_pde_pindex(KERNBASE) + i;
1210                 mpte->phys_addr = KPTphys + (i << PAGE_SHIFT);
1211         }
1212
1213         /*
1214          * If the kernel is running on a virtual machine, then it must assume
1215          * that MCA is enabled by the hypervisor.  Moreover, the kernel must
1216          * be prepared for the hypervisor changing the vendor and family that
1217          * are reported by CPUID.  Consequently, the workaround for AMD Family
1218          * 10h Erratum 383 is enabled if the processor's feature set does not
1219          * include at least one feature that is only supported by older Intel
1220          * or newer AMD processors.
1221          */
1222         if (vm_guest != VM_GUEST_NO && (cpu_feature & CPUID_SS) == 0 &&
1223             (cpu_feature2 & (CPUID2_SSSE3 | CPUID2_SSE41 | CPUID2_AESNI |
1224             CPUID2_AVX | CPUID2_XSAVE)) == 0 && (amd_feature2 & (AMDID2_XOP |
1225             AMDID2_FMA4)) == 0)
1226                 workaround_erratum383 = 1;
1227
1228         /*
1229          * Are large page mappings enabled?
1230          */
1231         TUNABLE_INT_FETCH("vm.pmap.pg_ps_enabled", &pg_ps_enabled);
1232         if (pg_ps_enabled) {
1233                 KASSERT(MAXPAGESIZES > 1 && pagesizes[1] == 0,
1234                     ("pmap_init: can't assign to pagesizes[1]"));
1235                 pagesizes[1] = NBPDR;
1236         }
1237
1238         /*
1239          * Initialize the pv chunk list mutex.
1240          */
1241         mtx_init(&pv_chunks_mutex, "pmap pv chunk list", NULL, MTX_DEF);
1242
1243         /*
1244          * Initialize the pool of pv list locks.
1245          */
1246         for (i = 0; i < NPV_LIST_LOCKS; i++)
1247                 rw_init(&pv_list_locks[i], "pmap pv list");
1248
1249         /*
1250          * Calculate the size of the pv head table for superpages.
1251          */
1252         pv_npg = howmany(vm_phys_segs[vm_phys_nsegs - 1].end, NBPDR);
1253
1254         /*
1255          * Allocate memory for the pv head table for superpages.
1256          */
1257         s = (vm_size_t)(pv_npg * sizeof(struct md_page));
1258         s = round_page(s);
1259         pv_table = (struct md_page *)kmem_malloc(kernel_arena, s,
1260             M_WAITOK | M_ZERO);
1261         for (i = 0; i < pv_npg; i++)
1262                 TAILQ_INIT(&pv_table[i].pv_list);
1263         TAILQ_INIT(&pv_dummy.pv_list);
1264
1265         pmap_initialized = 1;
1266         for (i = 0; i < PMAP_PREINIT_MAPPING_COUNT; i++) {
1267                 ppim = pmap_preinit_mapping + i;
1268                 if (ppim->va == 0)
1269                         continue;
1270                 /* Make the direct map consistent */
1271                 if (ppim->pa < dmaplimit && ppim->pa + ppim->sz < dmaplimit) {
1272                         (void)pmap_change_attr(PHYS_TO_DMAP(ppim->pa),
1273                             ppim->sz, ppim->mode);
1274                 }
1275                 if (!bootverbose)
1276                         continue;
1277                 printf("PPIM %u: PA=%#lx, VA=%#lx, size=%#lx, mode=%#x\n", i,
1278                     ppim->pa, ppim->va, ppim->sz, ppim->mode);
1279         }
1280
1281         mtx_init(&qframe_mtx, "qfrmlk", NULL, MTX_SPIN);
1282         error = vmem_alloc(kernel_arena, PAGE_SIZE, M_BESTFIT | M_WAITOK,
1283             (vmem_addr_t *)&qframe);
1284         if (error != 0)
1285                 panic("qframe allocation failed");
1286 }
1287
1288 static SYSCTL_NODE(_vm_pmap, OID_AUTO, pde, CTLFLAG_RD, 0,
1289     "2MB page mapping counters");
1290
1291 static u_long pmap_pde_demotions;
1292 SYSCTL_ULONG(_vm_pmap_pde, OID_AUTO, demotions, CTLFLAG_RD,
1293     &pmap_pde_demotions, 0, "2MB page demotions");
1294
1295 static u_long pmap_pde_mappings;
1296 SYSCTL_ULONG(_vm_pmap_pde, OID_AUTO, mappings, CTLFLAG_RD,
1297     &pmap_pde_mappings, 0, "2MB page mappings");
1298
1299 static u_long pmap_pde_p_failures;
1300 SYSCTL_ULONG(_vm_pmap_pde, OID_AUTO, p_failures, CTLFLAG_RD,
1301     &pmap_pde_p_failures, 0, "2MB page promotion failures");
1302
1303 static u_long pmap_pde_promotions;
1304 SYSCTL_ULONG(_vm_pmap_pde, OID_AUTO, promotions, CTLFLAG_RD,
1305     &pmap_pde_promotions, 0, "2MB page promotions");
1306
1307 static SYSCTL_NODE(_vm_pmap, OID_AUTO, pdpe, CTLFLAG_RD, 0,
1308     "1GB page mapping counters");
1309
1310 static u_long pmap_pdpe_demotions;
1311 SYSCTL_ULONG(_vm_pmap_pdpe, OID_AUTO, demotions, CTLFLAG_RD,
1312     &pmap_pdpe_demotions, 0, "1GB page demotions");
1313
1314 /***************************************************
1315  * Low level helper routines.....
1316  ***************************************************/
1317
1318 static pt_entry_t
1319 pmap_swap_pat(pmap_t pmap, pt_entry_t entry)
1320 {
1321         int x86_pat_bits = X86_PG_PTE_PAT | X86_PG_PDE_PAT;
1322
1323         switch (pmap->pm_type) {
1324         case PT_X86:
1325         case PT_RVI:
1326                 /* Verify that both PAT bits are not set at the same time */
1327                 KASSERT((entry & x86_pat_bits) != x86_pat_bits,
1328                     ("Invalid PAT bits in entry %#lx", entry));
1329
1330                 /* Swap the PAT bits if one of them is set */
1331                 if ((entry & x86_pat_bits) != 0)
1332                         entry ^= x86_pat_bits;
1333                 break;
1334         case PT_EPT:
1335                 /*
1336                  * Nothing to do - the memory attributes are represented
1337                  * the same way for regular pages and superpages.
1338                  */
1339                 break;
1340         default:
1341                 panic("pmap_switch_pat_bits: bad pm_type %d", pmap->pm_type);
1342         }
1343
1344         return (entry);
1345 }
1346
1347 /*
1348  * Determine the appropriate bits to set in a PTE or PDE for a specified
1349  * caching mode.
1350  */
1351 int
1352 pmap_cache_bits(pmap_t pmap, int mode, boolean_t is_pde)
1353 {
1354         int cache_bits, pat_flag, pat_idx;
1355
1356         if (mode < 0 || mode >= PAT_INDEX_SIZE || pat_index[mode] < 0)
1357                 panic("Unknown caching mode %d\n", mode);
1358
1359         switch (pmap->pm_type) {
1360         case PT_X86:
1361         case PT_RVI:
1362                 /* The PAT bit is different for PTE's and PDE's. */
1363                 pat_flag = is_pde ? X86_PG_PDE_PAT : X86_PG_PTE_PAT;
1364
1365                 /* Map the caching mode to a PAT index. */
1366                 pat_idx = pat_index[mode];
1367
1368                 /* Map the 3-bit index value into the PAT, PCD, and PWT bits. */
1369                 cache_bits = 0;
1370                 if (pat_idx & 0x4)
1371                         cache_bits |= pat_flag;
1372                 if (pat_idx & 0x2)
1373                         cache_bits |= PG_NC_PCD;
1374                 if (pat_idx & 0x1)
1375                         cache_bits |= PG_NC_PWT;
1376                 break;
1377
1378         case PT_EPT:
1379                 cache_bits = EPT_PG_IGNORE_PAT | EPT_PG_MEMORY_TYPE(mode);
1380                 break;
1381
1382         default:
1383                 panic("unsupported pmap type %d", pmap->pm_type);
1384         }
1385
1386         return (cache_bits);
1387 }
1388
1389 static int
1390 pmap_cache_mask(pmap_t pmap, boolean_t is_pde)
1391 {
1392         int mask;
1393
1394         switch (pmap->pm_type) {
1395         case PT_X86:
1396         case PT_RVI:
1397                 mask = is_pde ? X86_PG_PDE_CACHE : X86_PG_PTE_CACHE;
1398                 break;
1399         case PT_EPT:
1400                 mask = EPT_PG_IGNORE_PAT | EPT_PG_MEMORY_TYPE(0x7);
1401                 break;
1402         default:
1403                 panic("pmap_cache_mask: invalid pm_type %d", pmap->pm_type);
1404         }
1405
1406         return (mask);
1407 }
1408
1409 bool
1410 pmap_ps_enabled(pmap_t pmap)
1411 {
1412
1413         return (pg_ps_enabled && (pmap->pm_flags & PMAP_PDE_SUPERPAGE) != 0);
1414 }
1415
1416 static void
1417 pmap_update_pde_store(pmap_t pmap, pd_entry_t *pde, pd_entry_t newpde)
1418 {
1419
1420         switch (pmap->pm_type) {
1421         case PT_X86:
1422                 break;
1423         case PT_RVI:
1424         case PT_EPT:
1425                 /*
1426                  * XXX
1427                  * This is a little bogus since the generation number is
1428                  * supposed to be bumped up when a region of the address
1429                  * space is invalidated in the page tables.
1430                  *
1431                  * In this case the old PDE entry is valid but yet we want
1432                  * to make sure that any mappings using the old entry are
1433                  * invalidated in the TLB.
1434                  *
1435                  * The reason this works as expected is because we rendezvous
1436                  * "all" host cpus and force any vcpu context to exit as a
1437                  * side-effect.
1438                  */
1439                 atomic_add_acq_long(&pmap->pm_eptgen, 1);
1440                 break;
1441         default:
1442                 panic("pmap_update_pde_store: bad pm_type %d", pmap->pm_type);
1443         }
1444         pde_store(pde, newpde);
1445 }
1446
1447 /*
1448  * After changing the page size for the specified virtual address in the page
1449  * table, flush the corresponding entries from the processor's TLB.  Only the
1450  * calling processor's TLB is affected.
1451  *
1452  * The calling thread must be pinned to a processor.
1453  */
1454 static void
1455 pmap_update_pde_invalidate(pmap_t pmap, vm_offset_t va, pd_entry_t newpde)
1456 {
1457         pt_entry_t PG_G;
1458
1459         if (pmap_type_guest(pmap))
1460                 return;
1461
1462         KASSERT(pmap->pm_type == PT_X86,
1463             ("pmap_update_pde_invalidate: invalid type %d", pmap->pm_type));
1464
1465         PG_G = pmap_global_bit(pmap);
1466
1467         if ((newpde & PG_PS) == 0)
1468                 /* Demotion: flush a specific 2MB page mapping. */
1469                 invlpg(va);
1470         else if ((newpde & PG_G) == 0)
1471                 /*
1472                  * Promotion: flush every 4KB page mapping from the TLB
1473                  * because there are too many to flush individually.
1474                  */
1475                 invltlb();
1476         else {
1477                 /*
1478                  * Promotion: flush every 4KB page mapping from the TLB,
1479                  * including any global (PG_G) mappings.
1480                  */
1481                 invltlb_glob();
1482         }
1483 }
1484 #ifdef SMP
1485
1486 /*
1487  * For SMP, these functions have to use the IPI mechanism for coherence.
1488  *
1489  * N.B.: Before calling any of the following TLB invalidation functions,
1490  * the calling processor must ensure that all stores updating a non-
1491  * kernel page table are globally performed.  Otherwise, another
1492  * processor could cache an old, pre-update entry without being
1493  * invalidated.  This can happen one of two ways: (1) The pmap becomes
1494  * active on another processor after its pm_active field is checked by
1495  * one of the following functions but before a store updating the page
1496  * table is globally performed. (2) The pmap becomes active on another
1497  * processor before its pm_active field is checked but due to
1498  * speculative loads one of the following functions stills reads the
1499  * pmap as inactive on the other processor.
1500  * 
1501  * The kernel page table is exempt because its pm_active field is
1502  * immutable.  The kernel page table is always active on every
1503  * processor.
1504  */
1505
1506 /*
1507  * Interrupt the cpus that are executing in the guest context.
1508  * This will force the vcpu to exit and the cached EPT mappings
1509  * will be invalidated by the host before the next vmresume.
1510  */
1511 static __inline void
1512 pmap_invalidate_ept(pmap_t pmap)
1513 {
1514         int ipinum;
1515
1516         sched_pin();
1517         KASSERT(!CPU_ISSET(curcpu, &pmap->pm_active),
1518             ("pmap_invalidate_ept: absurd pm_active"));
1519
1520         /*
1521          * The TLB mappings associated with a vcpu context are not
1522          * flushed each time a different vcpu is chosen to execute.
1523          *
1524          * This is in contrast with a process's vtop mappings that
1525          * are flushed from the TLB on each context switch.
1526          *
1527          * Therefore we need to do more than just a TLB shootdown on
1528          * the active cpus in 'pmap->pm_active'. To do this we keep
1529          * track of the number of invalidations performed on this pmap.
1530          *
1531          * Each vcpu keeps a cache of this counter and compares it
1532          * just before a vmresume. If the counter is out-of-date an
1533          * invept will be done to flush stale mappings from the TLB.
1534          */
1535         atomic_add_acq_long(&pmap->pm_eptgen, 1);
1536
1537         /*
1538          * Force the vcpu to exit and trap back into the hypervisor.
1539          */
1540         ipinum = pmap->pm_flags & PMAP_NESTED_IPIMASK;
1541         ipi_selected(pmap->pm_active, ipinum);
1542         sched_unpin();
1543 }
1544
1545 void
1546 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
1547 {
1548         cpuset_t *mask;
1549         u_int cpuid, i;
1550
1551         if (pmap_type_guest(pmap)) {
1552                 pmap_invalidate_ept(pmap);
1553                 return;
1554         }
1555
1556         KASSERT(pmap->pm_type == PT_X86,
1557             ("pmap_invalidate_page: invalid type %d", pmap->pm_type));
1558
1559         sched_pin();
1560         if (pmap == kernel_pmap) {
1561                 invlpg(va);
1562                 mask = &all_cpus;
1563         } else {
1564                 cpuid = PCPU_GET(cpuid);
1565                 if (pmap == PCPU_GET(curpmap))
1566                         invlpg(va);
1567                 else if (pmap_pcid_enabled)
1568                         pmap->pm_pcids[cpuid].pm_gen = 0;
1569                 if (pmap_pcid_enabled) {
1570                         CPU_FOREACH(i) {
1571                                 if (cpuid != i)
1572                                         pmap->pm_pcids[i].pm_gen = 0;
1573                         }
1574                 }
1575                 mask = &pmap->pm_active;
1576         }
1577         smp_masked_invlpg(*mask, va);
1578         sched_unpin();
1579 }
1580
1581 /* 4k PTEs -- Chosen to exceed the total size of Broadwell L2 TLB */
1582 #define PMAP_INVLPG_THRESHOLD   (4 * 1024 * PAGE_SIZE)
1583
1584 void
1585 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
1586 {
1587         cpuset_t *mask;
1588         vm_offset_t addr;
1589         u_int cpuid, i;
1590
1591         if (eva - sva >= PMAP_INVLPG_THRESHOLD) {
1592                 pmap_invalidate_all(pmap);
1593                 return;
1594         }
1595
1596         if (pmap_type_guest(pmap)) {
1597                 pmap_invalidate_ept(pmap);
1598                 return;
1599         }
1600
1601         KASSERT(pmap->pm_type == PT_X86,
1602             ("pmap_invalidate_range: invalid type %d", pmap->pm_type));
1603
1604         sched_pin();
1605         cpuid = PCPU_GET(cpuid);
1606         if (pmap == kernel_pmap) {
1607                 for (addr = sva; addr < eva; addr += PAGE_SIZE)
1608                         invlpg(addr);
1609                 mask = &all_cpus;
1610         } else {
1611                 if (pmap == PCPU_GET(curpmap)) {
1612                         for (addr = sva; addr < eva; addr += PAGE_SIZE)
1613                                 invlpg(addr);
1614                 } else if (pmap_pcid_enabled) {
1615                         pmap->pm_pcids[cpuid].pm_gen = 0;
1616                 }
1617                 if (pmap_pcid_enabled) {
1618                         CPU_FOREACH(i) {
1619                                 if (cpuid != i)
1620                                         pmap->pm_pcids[i].pm_gen = 0;
1621                         }
1622                 }
1623                 mask = &pmap->pm_active;
1624         }
1625         smp_masked_invlpg_range(*mask, sva, eva);
1626         sched_unpin();
1627 }
1628
1629 void
1630 pmap_invalidate_all(pmap_t pmap)
1631 {
1632         cpuset_t *mask;
1633         struct invpcid_descr d;
1634         u_int cpuid, i;
1635
1636         if (pmap_type_guest(pmap)) {
1637                 pmap_invalidate_ept(pmap);
1638                 return;
1639         }
1640
1641         KASSERT(pmap->pm_type == PT_X86,
1642             ("pmap_invalidate_all: invalid type %d", pmap->pm_type));
1643
1644         sched_pin();
1645         if (pmap == kernel_pmap) {
1646                 if (pmap_pcid_enabled && invpcid_works) {
1647                         bzero(&d, sizeof(d));
1648                         invpcid(&d, INVPCID_CTXGLOB);
1649                 } else {
1650                         invltlb_glob();
1651                 }
1652                 mask = &all_cpus;
1653         } else {
1654                 cpuid = PCPU_GET(cpuid);
1655                 if (pmap == PCPU_GET(curpmap)) {
1656                         if (pmap_pcid_enabled) {
1657                                 if (invpcid_works) {
1658                                         d.pcid = pmap->pm_pcids[cpuid].pm_pcid;
1659                                         d.pad = 0;
1660                                         d.addr = 0;
1661                                         invpcid(&d, INVPCID_CTX);
1662                                 } else {
1663                                         load_cr3(pmap->pm_cr3 | pmap->pm_pcids
1664                                             [PCPU_GET(cpuid)].pm_pcid);
1665                                 }
1666                         } else {
1667                                 invltlb();
1668                         }
1669                 } else if (pmap_pcid_enabled) {
1670                         pmap->pm_pcids[cpuid].pm_gen = 0;
1671                 }
1672                 if (pmap_pcid_enabled) {
1673                         CPU_FOREACH(i) {
1674                                 if (cpuid != i)
1675                                         pmap->pm_pcids[i].pm_gen = 0;
1676                         }
1677                 }
1678                 mask = &pmap->pm_active;
1679         }
1680         smp_masked_invltlb(*mask, pmap);
1681         sched_unpin();
1682 }
1683
1684 void
1685 pmap_invalidate_cache(void)
1686 {
1687
1688         sched_pin();
1689         wbinvd();
1690         smp_cache_flush();
1691         sched_unpin();
1692 }
1693
1694 struct pde_action {
1695         cpuset_t invalidate;    /* processors that invalidate their TLB */
1696         pmap_t pmap;
1697         vm_offset_t va;
1698         pd_entry_t *pde;
1699         pd_entry_t newpde;
1700         u_int store;            /* processor that updates the PDE */
1701 };
1702
1703 static void
1704 pmap_update_pde_action(void *arg)
1705 {
1706         struct pde_action *act = arg;
1707
1708         if (act->store == PCPU_GET(cpuid))
1709                 pmap_update_pde_store(act->pmap, act->pde, act->newpde);
1710 }
1711
1712 static void
1713 pmap_update_pde_teardown(void *arg)
1714 {
1715         struct pde_action *act = arg;
1716
1717         if (CPU_ISSET(PCPU_GET(cpuid), &act->invalidate))
1718                 pmap_update_pde_invalidate(act->pmap, act->va, act->newpde);
1719 }
1720
1721 /*
1722  * Change the page size for the specified virtual address in a way that
1723  * prevents any possibility of the TLB ever having two entries that map the
1724  * same virtual address using different page sizes.  This is the recommended
1725  * workaround for Erratum 383 on AMD Family 10h processors.  It prevents a
1726  * machine check exception for a TLB state that is improperly diagnosed as a
1727  * hardware error.
1728  */
1729 static void
1730 pmap_update_pde(pmap_t pmap, vm_offset_t va, pd_entry_t *pde, pd_entry_t newpde)
1731 {
1732         struct pde_action act;
1733         cpuset_t active, other_cpus;
1734         u_int cpuid;
1735
1736         sched_pin();
1737         cpuid = PCPU_GET(cpuid);
1738         other_cpus = all_cpus;
1739         CPU_CLR(cpuid, &other_cpus);
1740         if (pmap == kernel_pmap || pmap_type_guest(pmap)) 
1741                 active = all_cpus;
1742         else {
1743                 active = pmap->pm_active;
1744         }
1745         if (CPU_OVERLAP(&active, &other_cpus)) { 
1746                 act.store = cpuid;
1747                 act.invalidate = active;
1748                 act.va = va;
1749                 act.pmap = pmap;
1750                 act.pde = pde;
1751                 act.newpde = newpde;
1752                 CPU_SET(cpuid, &active);
1753                 smp_rendezvous_cpus(active,
1754                     smp_no_rendevous_barrier, pmap_update_pde_action,
1755                     pmap_update_pde_teardown, &act);
1756         } else {
1757                 pmap_update_pde_store(pmap, pde, newpde);
1758                 if (CPU_ISSET(cpuid, &active))
1759                         pmap_update_pde_invalidate(pmap, va, newpde);
1760         }
1761         sched_unpin();
1762 }
1763 #else /* !SMP */
1764 /*
1765  * Normal, non-SMP, invalidation functions.
1766  */
1767 void
1768 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
1769 {
1770
1771         if (pmap->pm_type == PT_RVI || pmap->pm_type == PT_EPT) {
1772                 pmap->pm_eptgen++;
1773                 return;
1774         }
1775         KASSERT(pmap->pm_type == PT_X86,
1776             ("pmap_invalidate_range: unknown type %d", pmap->pm_type));
1777
1778         if (pmap == kernel_pmap || pmap == PCPU_GET(curpmap))
1779                 invlpg(va);
1780         else if (pmap_pcid_enabled)
1781                 pmap->pm_pcids[0].pm_gen = 0;
1782 }
1783
1784 void
1785 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
1786 {
1787         vm_offset_t addr;
1788
1789         if (pmap->pm_type == PT_RVI || pmap->pm_type == PT_EPT) {
1790                 pmap->pm_eptgen++;
1791                 return;
1792         }
1793         KASSERT(pmap->pm_type == PT_X86,
1794             ("pmap_invalidate_range: unknown type %d", pmap->pm_type));
1795
1796         if (pmap == kernel_pmap || pmap == PCPU_GET(curpmap)) {
1797                 for (addr = sva; addr < eva; addr += PAGE_SIZE)
1798                         invlpg(addr);
1799         } else if (pmap_pcid_enabled) {
1800                 pmap->pm_pcids[0].pm_gen = 0;
1801         }
1802 }
1803
1804 void
1805 pmap_invalidate_all(pmap_t pmap)
1806 {
1807         struct invpcid_descr d;
1808
1809         if (pmap->pm_type == PT_RVI || pmap->pm_type == PT_EPT) {
1810                 pmap->pm_eptgen++;
1811                 return;
1812         }
1813         KASSERT(pmap->pm_type == PT_X86,
1814             ("pmap_invalidate_all: unknown type %d", pmap->pm_type));
1815
1816         if (pmap == kernel_pmap) {
1817                 if (pmap_pcid_enabled && invpcid_works) {
1818                         bzero(&d, sizeof(d));
1819                         invpcid(&d, INVPCID_CTXGLOB);
1820                 } else {
1821                         invltlb_glob();
1822                 }
1823         } else if (pmap == PCPU_GET(curpmap)) {
1824                 if (pmap_pcid_enabled) {
1825                         if (invpcid_works) {
1826                                 d.pcid = pmap->pm_pcids[0].pm_pcid;
1827                                 d.pad = 0;
1828                                 d.addr = 0;
1829                                 invpcid(&d, INVPCID_CTX);
1830                         } else {
1831                                 load_cr3(pmap->pm_cr3 | pmap->pm_pcids[0].
1832                                     pm_pcid);
1833                         }
1834                 } else {
1835                         invltlb();
1836                 }
1837         } else if (pmap_pcid_enabled) {
1838                 pmap->pm_pcids[0].pm_gen = 0;
1839         }
1840 }
1841
1842 PMAP_INLINE void
1843 pmap_invalidate_cache(void)
1844 {
1845
1846         wbinvd();
1847 }
1848
1849 static void
1850 pmap_update_pde(pmap_t pmap, vm_offset_t va, pd_entry_t *pde, pd_entry_t newpde)
1851 {
1852
1853         pmap_update_pde_store(pmap, pde, newpde);
1854         if (pmap == kernel_pmap || pmap == PCPU_GET(curpmap))
1855                 pmap_update_pde_invalidate(pmap, va, newpde);
1856         else
1857                 pmap->pm_pcids[0].pm_gen = 0;
1858 }
1859 #endif /* !SMP */
1860
1861 static void
1862 pmap_invalidate_pde_page(pmap_t pmap, vm_offset_t va, pd_entry_t pde)
1863 {
1864
1865         /*
1866          * When the PDE has PG_PROMOTED set, the 2MB page mapping was created
1867          * by a promotion that did not invalidate the 512 4KB page mappings
1868          * that might exist in the TLB.  Consequently, at this point, the TLB
1869          * may hold both 4KB and 2MB page mappings for the address range [va,
1870          * va + NBPDR).  Therefore, the entire range must be invalidated here.
1871          * In contrast, when PG_PROMOTED is clear, the TLB will not hold any
1872          * 4KB page mappings for the address range [va, va + NBPDR), and so a
1873          * single INVLPG suffices to invalidate the 2MB page mapping from the
1874          * TLB.
1875          */
1876         if ((pde & PG_PROMOTED) != 0)
1877                 pmap_invalidate_range(pmap, va, va + NBPDR - 1);
1878         else
1879                 pmap_invalidate_page(pmap, va);
1880 }
1881
1882 #define PMAP_CLFLUSH_THRESHOLD   (2 * 1024 * 1024)
1883
1884 void
1885 pmap_invalidate_cache_range(vm_offset_t sva, vm_offset_t eva, boolean_t force)
1886 {
1887
1888         if (force) {
1889                 sva &= ~(vm_offset_t)(cpu_clflush_line_size - 1);
1890         } else {
1891                 KASSERT((sva & PAGE_MASK) == 0,
1892                     ("pmap_invalidate_cache_range: sva not page-aligned"));
1893                 KASSERT((eva & PAGE_MASK) == 0,
1894                     ("pmap_invalidate_cache_range: eva not page-aligned"));
1895         }
1896
1897         if ((cpu_feature & CPUID_SS) != 0 && !force)
1898                 ; /* If "Self Snoop" is supported and allowed, do nothing. */
1899         else if ((cpu_stdext_feature & CPUID_STDEXT_CLFLUSHOPT) != 0 &&
1900             eva - sva < PMAP_CLFLUSH_THRESHOLD) {
1901                 /*
1902                  * XXX: Some CPUs fault, hang, or trash the local APIC
1903                  * registers if we use CLFLUSH on the local APIC
1904                  * range.  The local APIC is always uncached, so we
1905                  * don't need to flush for that range anyway.
1906                  */
1907                 if (pmap_kextract(sva) == lapic_paddr)
1908                         return;
1909
1910                 /*
1911                  * Otherwise, do per-cache line flush.  Use the sfence
1912                  * instruction to insure that previous stores are
1913                  * included in the write-back.  The processor
1914                  * propagates flush to other processors in the cache
1915                  * coherence domain.
1916                  */
1917                 sfence();
1918                 for (; sva < eva; sva += cpu_clflush_line_size)
1919                         clflushopt(sva);
1920                 sfence();
1921         } else if ((cpu_feature & CPUID_CLFSH) != 0 &&
1922             eva - sva < PMAP_CLFLUSH_THRESHOLD) {
1923                 if (pmap_kextract(sva) == lapic_paddr)
1924                         return;
1925                 /*
1926                  * Writes are ordered by CLFLUSH on Intel CPUs.
1927                  */
1928                 if (cpu_vendor_id != CPU_VENDOR_INTEL)
1929                         mfence();
1930                 for (; sva < eva; sva += cpu_clflush_line_size)
1931                         clflush(sva);
1932                 if (cpu_vendor_id != CPU_VENDOR_INTEL)
1933                         mfence();
1934         } else {
1935
1936                 /*
1937                  * No targeted cache flush methods are supported by CPU,
1938                  * or the supplied range is bigger than 2MB.
1939                  * Globally invalidate cache.
1940                  */
1941                 pmap_invalidate_cache();
1942         }
1943 }
1944
1945 /*
1946  * Remove the specified set of pages from the data and instruction caches.
1947  *
1948  * In contrast to pmap_invalidate_cache_range(), this function does not
1949  * rely on the CPU's self-snoop feature, because it is intended for use
1950  * when moving pages into a different cache domain.
1951  */
1952 void
1953 pmap_invalidate_cache_pages(vm_page_t *pages, int count)
1954 {
1955         vm_offset_t daddr, eva;
1956         int i;
1957         bool useclflushopt;
1958
1959         useclflushopt = (cpu_stdext_feature & CPUID_STDEXT_CLFLUSHOPT) != 0;
1960         if (count >= PMAP_CLFLUSH_THRESHOLD / PAGE_SIZE ||
1961             ((cpu_feature & CPUID_CLFSH) == 0 && !useclflushopt))
1962                 pmap_invalidate_cache();
1963         else {
1964                 if (useclflushopt)
1965                         sfence();
1966                 else if (cpu_vendor_id != CPU_VENDOR_INTEL)
1967                         mfence();
1968                 for (i = 0; i < count; i++) {
1969                         daddr = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(pages[i]));
1970                         eva = daddr + PAGE_SIZE;
1971                         for (; daddr < eva; daddr += cpu_clflush_line_size) {
1972                                 if (useclflushopt)
1973                                         clflushopt(daddr);
1974                                 else
1975                                         clflush(daddr);
1976                         }
1977                 }
1978                 if (useclflushopt)
1979                         sfence();
1980                 else if (cpu_vendor_id != CPU_VENDOR_INTEL)
1981                         mfence();
1982         }
1983 }
1984
1985 /*
1986  *      Routine:        pmap_extract
1987  *      Function:
1988  *              Extract the physical page address associated
1989  *              with the given map/virtual_address pair.
1990  */
1991 vm_paddr_t 
1992 pmap_extract(pmap_t pmap, vm_offset_t va)
1993 {
1994         pdp_entry_t *pdpe;
1995         pd_entry_t *pde;
1996         pt_entry_t *pte, PG_V;
1997         vm_paddr_t pa;
1998
1999         pa = 0;
2000         PG_V = pmap_valid_bit(pmap);
2001         PMAP_LOCK(pmap);
2002         pdpe = pmap_pdpe(pmap, va);
2003         if (pdpe != NULL && (*pdpe & PG_V) != 0) {
2004                 if ((*pdpe & PG_PS) != 0)
2005                         pa = (*pdpe & PG_PS_FRAME) | (va & PDPMASK);
2006                 else {
2007                         pde = pmap_pdpe_to_pde(pdpe, va);
2008                         if ((*pde & PG_V) != 0) {
2009                                 if ((*pde & PG_PS) != 0) {
2010                                         pa = (*pde & PG_PS_FRAME) |
2011                                             (va & PDRMASK);
2012                                 } else {
2013                                         pte = pmap_pde_to_pte(pde, va);
2014                                         pa = (*pte & PG_FRAME) |
2015                                             (va & PAGE_MASK);
2016                                 }
2017                         }
2018                 }
2019         }
2020         PMAP_UNLOCK(pmap);
2021         return (pa);
2022 }
2023
2024 /*
2025  *      Routine:        pmap_extract_and_hold
2026  *      Function:
2027  *              Atomically extract and hold the physical page
2028  *              with the given pmap and virtual address pair
2029  *              if that mapping permits the given protection.
2030  */
2031 vm_page_t
2032 pmap_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
2033 {
2034         pd_entry_t pde, *pdep;
2035         pt_entry_t pte, PG_RW, PG_V;
2036         vm_paddr_t pa;
2037         vm_page_t m;
2038
2039         pa = 0;
2040         m = NULL;
2041         PG_RW = pmap_rw_bit(pmap);
2042         PG_V = pmap_valid_bit(pmap);
2043         PMAP_LOCK(pmap);
2044 retry:
2045         pdep = pmap_pde(pmap, va);
2046         if (pdep != NULL && (pde = *pdep)) {
2047                 if (pde & PG_PS) {
2048                         if ((pde & PG_RW) || (prot & VM_PROT_WRITE) == 0) {
2049                                 if (vm_page_pa_tryrelock(pmap, (pde &
2050                                     PG_PS_FRAME) | (va & PDRMASK), &pa))
2051                                         goto retry;
2052                                 m = PHYS_TO_VM_PAGE((pde & PG_PS_FRAME) |
2053                                     (va & PDRMASK));
2054                                 vm_page_hold(m);
2055                         }
2056                 } else {
2057                         pte = *pmap_pde_to_pte(pdep, va);
2058                         if ((pte & PG_V) &&
2059                             ((pte & PG_RW) || (prot & VM_PROT_WRITE) == 0)) {
2060                                 if (vm_page_pa_tryrelock(pmap, pte & PG_FRAME,
2061                                     &pa))
2062                                         goto retry;
2063                                 m = PHYS_TO_VM_PAGE(pte & PG_FRAME);
2064                                 vm_page_hold(m);
2065                         }
2066                 }
2067         }
2068         PA_UNLOCK_COND(pa);
2069         PMAP_UNLOCK(pmap);
2070         return (m);
2071 }
2072
2073 vm_paddr_t
2074 pmap_kextract(vm_offset_t va)
2075 {
2076         pd_entry_t pde;
2077         vm_paddr_t pa;
2078
2079         if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS) {
2080                 pa = DMAP_TO_PHYS(va);
2081         } else {
2082                 pde = *vtopde(va);
2083                 if (pde & PG_PS) {
2084                         pa = (pde & PG_PS_FRAME) | (va & PDRMASK);
2085                 } else {
2086                         /*
2087                          * Beware of a concurrent promotion that changes the
2088                          * PDE at this point!  For example, vtopte() must not
2089                          * be used to access the PTE because it would use the
2090                          * new PDE.  It is, however, safe to use the old PDE
2091                          * because the page table page is preserved by the
2092                          * promotion.
2093                          */
2094                         pa = *pmap_pde_to_pte(&pde, va);
2095                         pa = (pa & PG_FRAME) | (va & PAGE_MASK);
2096                 }
2097         }
2098         return (pa);
2099 }
2100
2101 /***************************************************
2102  * Low level mapping routines.....
2103  ***************************************************/
2104
2105 /*
2106  * Add a wired page to the kva.
2107  * Note: not SMP coherent.
2108  */
2109 PMAP_INLINE void 
2110 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
2111 {
2112         pt_entry_t *pte;
2113
2114         pte = vtopte(va);
2115         pte_store(pte, pa | X86_PG_RW | X86_PG_V | X86_PG_G);
2116 }
2117
2118 static __inline void
2119 pmap_kenter_attr(vm_offset_t va, vm_paddr_t pa, int mode)
2120 {
2121         pt_entry_t *pte;
2122         int cache_bits;
2123
2124         pte = vtopte(va);
2125         cache_bits = pmap_cache_bits(kernel_pmap, mode, 0);
2126         pte_store(pte, pa | X86_PG_RW | X86_PG_V | X86_PG_G | cache_bits);
2127 }
2128
2129 /*
2130  * Remove a page from the kernel pagetables.
2131  * Note: not SMP coherent.
2132  */
2133 PMAP_INLINE void
2134 pmap_kremove(vm_offset_t va)
2135 {
2136         pt_entry_t *pte;
2137
2138         pte = vtopte(va);
2139         pte_clear(pte);
2140 }
2141
2142 /*
2143  *      Used to map a range of physical addresses into kernel
2144  *      virtual address space.
2145  *
2146  *      The value passed in '*virt' is a suggested virtual address for
2147  *      the mapping. Architectures which can support a direct-mapped
2148  *      physical to virtual region can return the appropriate address
2149  *      within that region, leaving '*virt' unchanged. Other
2150  *      architectures should map the pages starting at '*virt' and
2151  *      update '*virt' with the first usable address after the mapped
2152  *      region.
2153  */
2154 vm_offset_t
2155 pmap_map(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end, int prot)
2156 {
2157         return PHYS_TO_DMAP(start);
2158 }
2159
2160
2161 /*
2162  * Add a list of wired pages to the kva
2163  * this routine is only used for temporary
2164  * kernel mappings that do not need to have
2165  * page modification or references recorded.
2166  * Note that old mappings are simply written
2167  * over.  The page *must* be wired.
2168  * Note: SMP coherent.  Uses a ranged shootdown IPI.
2169  */
2170 void
2171 pmap_qenter(vm_offset_t sva, vm_page_t *ma, int count)
2172 {
2173         pt_entry_t *endpte, oldpte, pa, *pte;
2174         vm_page_t m;
2175         int cache_bits;
2176
2177         oldpte = 0;
2178         pte = vtopte(sva);
2179         endpte = pte + count;
2180         while (pte < endpte) {
2181                 m = *ma++;
2182                 cache_bits = pmap_cache_bits(kernel_pmap, m->md.pat_mode, 0);
2183                 pa = VM_PAGE_TO_PHYS(m) | cache_bits;
2184                 if ((*pte & (PG_FRAME | X86_PG_PTE_CACHE)) != pa) {
2185                         oldpte |= *pte;
2186                         pte_store(pte, pa | X86_PG_G | X86_PG_RW | X86_PG_V);
2187                 }
2188                 pte++;
2189         }
2190         if (__predict_false((oldpte & X86_PG_V) != 0))
2191                 pmap_invalidate_range(kernel_pmap, sva, sva + count *
2192                     PAGE_SIZE);
2193 }
2194
2195 /*
2196  * This routine tears out page mappings from the
2197  * kernel -- it is meant only for temporary mappings.
2198  * Note: SMP coherent.  Uses a ranged shootdown IPI.
2199  */
2200 void
2201 pmap_qremove(vm_offset_t sva, int count)
2202 {
2203         vm_offset_t va;
2204
2205         va = sva;
2206         while (count-- > 0) {
2207                 KASSERT(va >= VM_MIN_KERNEL_ADDRESS, ("usermode va %lx", va));
2208                 pmap_kremove(va);
2209                 va += PAGE_SIZE;
2210         }
2211         pmap_invalidate_range(kernel_pmap, sva, va);
2212 }
2213
2214 /***************************************************
2215  * Page table page management routines.....
2216  ***************************************************/
2217 static __inline void
2218 pmap_free_zero_pages(struct spglist *free)
2219 {
2220         vm_page_t m;
2221         int count;
2222
2223         for (count = 0; (m = SLIST_FIRST(free)) != NULL; count++) {
2224                 SLIST_REMOVE_HEAD(free, plinks.s.ss);
2225                 /* Preserve the page's PG_ZERO setting. */
2226                 vm_page_free_toq(m);
2227         }
2228         atomic_subtract_int(&vm_cnt.v_wire_count, count);
2229 }
2230
2231 /*
2232  * Schedule the specified unused page table page to be freed.  Specifically,
2233  * add the page to the specified list of pages that will be released to the
2234  * physical memory manager after the TLB has been updated.
2235  */
2236 static __inline void
2237 pmap_add_delayed_free_list(vm_page_t m, struct spglist *free,
2238     boolean_t set_PG_ZERO)
2239 {
2240
2241         if (set_PG_ZERO)
2242                 m->flags |= PG_ZERO;
2243         else
2244                 m->flags &= ~PG_ZERO;
2245         SLIST_INSERT_HEAD(free, m, plinks.s.ss);
2246 }
2247         
2248 /*
2249  * Inserts the specified page table page into the specified pmap's collection
2250  * of idle page table pages.  Each of a pmap's page table pages is responsible
2251  * for mapping a distinct range of virtual addresses.  The pmap's collection is
2252  * ordered by this virtual address range.
2253  */
2254 static __inline int
2255 pmap_insert_pt_page(pmap_t pmap, vm_page_t mpte)
2256 {
2257
2258         PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2259         return (vm_radix_insert(&pmap->pm_root, mpte));
2260 }
2261
2262 /*
2263  * Removes the page table page mapping the specified virtual address from the
2264  * specified pmap's collection of idle page table pages, and returns it.
2265  * Otherwise, returns NULL if there is no page table page corresponding to the
2266  * specified virtual address.
2267  */
2268 static __inline vm_page_t
2269 pmap_remove_pt_page(pmap_t pmap, vm_offset_t va)
2270 {
2271
2272         PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2273         return (vm_radix_remove(&pmap->pm_root, pmap_pde_pindex(va)));
2274 }
2275
2276 /*
2277  * Decrements a page table page's wire count, which is used to record the
2278  * number of valid page table entries within the page.  If the wire count
2279  * drops to zero, then the page table page is unmapped.  Returns TRUE if the
2280  * page table page was unmapped and FALSE otherwise.
2281  */
2282 static inline boolean_t
2283 pmap_unwire_ptp(pmap_t pmap, vm_offset_t va, vm_page_t m, struct spglist *free)
2284 {
2285
2286         --m->wire_count;
2287         if (m->wire_count == 0) {
2288                 _pmap_unwire_ptp(pmap, va, m, free);
2289                 return (TRUE);
2290         } else
2291                 return (FALSE);
2292 }
2293
2294 static void
2295 _pmap_unwire_ptp(pmap_t pmap, vm_offset_t va, vm_page_t m, struct spglist *free)
2296 {
2297
2298         PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2299         /*
2300          * unmap the page table page
2301          */
2302         if (m->pindex >= (NUPDE + NUPDPE)) {
2303                 /* PDP page */
2304                 pml4_entry_t *pml4;
2305                 pml4 = pmap_pml4e(pmap, va);
2306                 *pml4 = 0;
2307         } else if (m->pindex >= NUPDE) {
2308                 /* PD page */
2309                 pdp_entry_t *pdp;
2310                 pdp = pmap_pdpe(pmap, va);
2311                 *pdp = 0;
2312         } else {
2313                 /* PTE page */
2314                 pd_entry_t *pd;
2315                 pd = pmap_pde(pmap, va);
2316                 *pd = 0;
2317         }
2318         pmap_resident_count_dec(pmap, 1);
2319         if (m->pindex < NUPDE) {
2320                 /* We just released a PT, unhold the matching PD */
2321                 vm_page_t pdpg;
2322
2323                 pdpg = PHYS_TO_VM_PAGE(*pmap_pdpe(pmap, va) & PG_FRAME);
2324                 pmap_unwire_ptp(pmap, va, pdpg, free);
2325         }
2326         if (m->pindex >= NUPDE && m->pindex < (NUPDE + NUPDPE)) {
2327                 /* We just released a PD, unhold the matching PDP */
2328                 vm_page_t pdppg;
2329
2330                 pdppg = PHYS_TO_VM_PAGE(*pmap_pml4e(pmap, va) & PG_FRAME);
2331                 pmap_unwire_ptp(pmap, va, pdppg, free);
2332         }
2333
2334         /* 
2335          * Put page on a list so that it is released after
2336          * *ALL* TLB shootdown is done
2337          */
2338         pmap_add_delayed_free_list(m, free, TRUE);
2339 }
2340
2341 /*
2342  * After removing a page table entry, this routine is used to
2343  * conditionally free the page, and manage the hold/wire counts.
2344  */
2345 static int
2346 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, pd_entry_t ptepde,
2347     struct spglist *free)
2348 {
2349         vm_page_t mpte;
2350
2351         if (va >= VM_MAXUSER_ADDRESS)
2352                 return (0);
2353         KASSERT(ptepde != 0, ("pmap_unuse_pt: ptepde != 0"));
2354         mpte = PHYS_TO_VM_PAGE(ptepde & PG_FRAME);
2355         return (pmap_unwire_ptp(pmap, va, mpte, free));
2356 }
2357
2358 void
2359 pmap_pinit0(pmap_t pmap)
2360 {
2361         int i;
2362
2363         PMAP_LOCK_INIT(pmap);
2364         pmap->pm_pml4 = (pml4_entry_t *)PHYS_TO_DMAP(KPML4phys);
2365         pmap->pm_cr3 = KPML4phys;
2366         pmap->pm_root.rt_root = 0;
2367         CPU_ZERO(&pmap->pm_active);
2368         TAILQ_INIT(&pmap->pm_pvchunk);
2369         bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
2370         pmap->pm_flags = pmap_flags;
2371         CPU_FOREACH(i) {
2372                 pmap->pm_pcids[i].pm_pcid = PMAP_PCID_NONE;
2373                 pmap->pm_pcids[i].pm_gen = 0;
2374         }
2375         PCPU_SET(curpmap, kernel_pmap);
2376         pmap_activate(curthread);
2377         CPU_FILL(&kernel_pmap->pm_active);
2378 }
2379
2380 void
2381 pmap_pinit_pml4(vm_page_t pml4pg)
2382 {
2383         pml4_entry_t *pm_pml4;
2384         int i;
2385
2386         pm_pml4 = (pml4_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(pml4pg));
2387
2388         /* Wire in kernel global address entries. */
2389         for (i = 0; i < NKPML4E; i++) {
2390                 pm_pml4[KPML4BASE + i] = (KPDPphys + ptoa(i)) | X86_PG_RW |
2391                     X86_PG_V | PG_U;
2392         }
2393         for (i = 0; i < ndmpdpphys; i++) {
2394                 pm_pml4[DMPML4I + i] = (DMPDPphys + ptoa(i)) | X86_PG_RW |
2395                     X86_PG_V | PG_U;
2396         }
2397
2398         /* install self-referential address mapping entry(s) */
2399         pm_pml4[PML4PML4I] = VM_PAGE_TO_PHYS(pml4pg) | X86_PG_V | X86_PG_RW |
2400             X86_PG_A | X86_PG_M;
2401 }
2402
2403 /*
2404  * Initialize a preallocated and zeroed pmap structure,
2405  * such as one in a vmspace structure.
2406  */
2407 int
2408 pmap_pinit_type(pmap_t pmap, enum pmap_type pm_type, int flags)
2409 {
2410         vm_page_t pml4pg;
2411         vm_paddr_t pml4phys;
2412         int i;
2413
2414         /*
2415          * allocate the page directory page
2416          */
2417         while ((pml4pg = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL |
2418             VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | VM_ALLOC_ZERO)) == NULL)
2419                 VM_WAIT;
2420
2421         pml4phys = VM_PAGE_TO_PHYS(pml4pg);
2422         pmap->pm_pml4 = (pml4_entry_t *)PHYS_TO_DMAP(pml4phys);
2423         CPU_FOREACH(i) {
2424                 pmap->pm_pcids[i].pm_pcid = PMAP_PCID_NONE;
2425                 pmap->pm_pcids[i].pm_gen = 0;
2426         }
2427         pmap->pm_cr3 = ~0;      /* initialize to an invalid value */
2428
2429         if ((pml4pg->flags & PG_ZERO) == 0)
2430                 pagezero(pmap->pm_pml4);
2431
2432         /*
2433          * Do not install the host kernel mappings in the nested page
2434          * tables. These mappings are meaningless in the guest physical
2435          * address space.
2436          */
2437         if ((pmap->pm_type = pm_type) == PT_X86) {
2438                 pmap->pm_cr3 = pml4phys;
2439                 pmap_pinit_pml4(pml4pg);
2440         }
2441
2442         pmap->pm_root.rt_root = 0;
2443         CPU_ZERO(&pmap->pm_active);
2444         TAILQ_INIT(&pmap->pm_pvchunk);
2445         bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
2446         pmap->pm_flags = flags;
2447         pmap->pm_eptgen = 0;
2448
2449         return (1);
2450 }
2451
2452 int
2453 pmap_pinit(pmap_t pmap)
2454 {
2455
2456         return (pmap_pinit_type(pmap, PT_X86, pmap_flags));
2457 }
2458
2459 /*
2460  * This routine is called if the desired page table page does not exist.
2461  *
2462  * If page table page allocation fails, this routine may sleep before
2463  * returning NULL.  It sleeps only if a lock pointer was given.
2464  *
2465  * Note: If a page allocation fails at page table level two or three,
2466  * one or two pages may be held during the wait, only to be released
2467  * afterwards.  This conservative approach is easily argued to avoid
2468  * race conditions.
2469  */
2470 static vm_page_t
2471 _pmap_allocpte(pmap_t pmap, vm_pindex_t ptepindex, struct rwlock **lockp)
2472 {
2473         vm_page_t m, pdppg, pdpg;
2474         pt_entry_t PG_A, PG_M, PG_RW, PG_V;
2475
2476         PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2477
2478         PG_A = pmap_accessed_bit(pmap);
2479         PG_M = pmap_modified_bit(pmap);
2480         PG_V = pmap_valid_bit(pmap);
2481         PG_RW = pmap_rw_bit(pmap);
2482
2483         /*
2484          * Allocate a page table page.
2485          */
2486         if ((m = vm_page_alloc(NULL, ptepindex, VM_ALLOC_NOOBJ |
2487             VM_ALLOC_WIRED | VM_ALLOC_ZERO)) == NULL) {
2488                 if (lockp != NULL) {
2489                         RELEASE_PV_LIST_LOCK(lockp);
2490                         PMAP_UNLOCK(pmap);
2491                         PMAP_ASSERT_NOT_IN_DI();
2492                         VM_WAIT;
2493                         PMAP_LOCK(pmap);
2494                 }
2495
2496                 /*
2497                  * Indicate the need to retry.  While waiting, the page table
2498                  * page may have been allocated.
2499                  */
2500                 return (NULL);
2501         }
2502         if ((m->flags & PG_ZERO) == 0)
2503                 pmap_zero_page(m);
2504
2505         /*
2506          * Map the pagetable page into the process address space, if
2507          * it isn't already there.
2508          */
2509
2510         if (ptepindex >= (NUPDE + NUPDPE)) {
2511                 pml4_entry_t *pml4;
2512                 vm_pindex_t pml4index;
2513
2514                 /* Wire up a new PDPE page */
2515                 pml4index = ptepindex - (NUPDE + NUPDPE);
2516                 pml4 = &pmap->pm_pml4[pml4index];
2517                 *pml4 = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
2518
2519         } else if (ptepindex >= NUPDE) {
2520                 vm_pindex_t pml4index;
2521                 vm_pindex_t pdpindex;
2522                 pml4_entry_t *pml4;
2523                 pdp_entry_t *pdp;
2524
2525                 /* Wire up a new PDE page */
2526                 pdpindex = ptepindex - NUPDE;
2527                 pml4index = pdpindex >> NPML4EPGSHIFT;
2528
2529                 pml4 = &pmap->pm_pml4[pml4index];
2530                 if ((*pml4 & PG_V) == 0) {
2531                         /* Have to allocate a new pdp, recurse */
2532                         if (_pmap_allocpte(pmap, NUPDE + NUPDPE + pml4index,
2533                             lockp) == NULL) {
2534                                 --m->wire_count;
2535                                 atomic_subtract_int(&vm_cnt.v_wire_count, 1);
2536                                 vm_page_free_zero(m);
2537                                 return (NULL);
2538                         }
2539                 } else {
2540                         /* Add reference to pdp page */
2541                         pdppg = PHYS_TO_VM_PAGE(*pml4 & PG_FRAME);
2542                         pdppg->wire_count++;
2543                 }
2544                 pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
2545
2546                 /* Now find the pdp page */
2547                 pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
2548                 *pdp = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
2549
2550         } else {
2551                 vm_pindex_t pml4index;
2552                 vm_pindex_t pdpindex;
2553                 pml4_entry_t *pml4;
2554                 pdp_entry_t *pdp;
2555                 pd_entry_t *pd;
2556
2557                 /* Wire up a new PTE page */
2558                 pdpindex = ptepindex >> NPDPEPGSHIFT;
2559                 pml4index = pdpindex >> NPML4EPGSHIFT;
2560
2561                 /* First, find the pdp and check that its valid. */
2562                 pml4 = &pmap->pm_pml4[pml4index];
2563                 if ((*pml4 & PG_V) == 0) {
2564                         /* Have to allocate a new pd, recurse */
2565                         if (_pmap_allocpte(pmap, NUPDE + pdpindex,
2566                             lockp) == NULL) {
2567                                 --m->wire_count;
2568                                 atomic_subtract_int(&vm_cnt.v_wire_count, 1);
2569                                 vm_page_free_zero(m);
2570                                 return (NULL);
2571                         }
2572                         pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
2573                         pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
2574                 } else {
2575                         pdp = (pdp_entry_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
2576                         pdp = &pdp[pdpindex & ((1ul << NPDPEPGSHIFT) - 1)];
2577                         if ((*pdp & PG_V) == 0) {
2578                                 /* Have to allocate a new pd, recurse */
2579                                 if (_pmap_allocpte(pmap, NUPDE + pdpindex,
2580                                     lockp) == NULL) {
2581                                         --m->wire_count;
2582                                         atomic_subtract_int(&vm_cnt.v_wire_count,
2583                                             1);
2584                                         vm_page_free_zero(m);
2585                                         return (NULL);
2586                                 }
2587                         } else {
2588                                 /* Add reference to the pd page */
2589                                 pdpg = PHYS_TO_VM_PAGE(*pdp & PG_FRAME);
2590                                 pdpg->wire_count++;
2591                         }
2592                 }
2593                 pd = (pd_entry_t *)PHYS_TO_DMAP(*pdp & PG_FRAME);
2594
2595                 /* Now we know where the page directory page is */
2596                 pd = &pd[ptepindex & ((1ul << NPDEPGSHIFT) - 1)];
2597                 *pd = VM_PAGE_TO_PHYS(m) | PG_U | PG_RW | PG_V | PG_A | PG_M;
2598         }
2599
2600         pmap_resident_count_inc(pmap, 1);
2601
2602         return (m);
2603 }
2604
2605 static vm_page_t
2606 pmap_allocpde(pmap_t pmap, vm_offset_t va, struct rwlock **lockp)
2607 {
2608         vm_pindex_t pdpindex, ptepindex;
2609         pdp_entry_t *pdpe, PG_V;
2610         vm_page_t pdpg;
2611
2612         PG_V = pmap_valid_bit(pmap);
2613
2614 retry:
2615         pdpe = pmap_pdpe(pmap, va);
2616         if (pdpe != NULL && (*pdpe & PG_V) != 0) {
2617                 /* Add a reference to the pd page. */
2618                 pdpg = PHYS_TO_VM_PAGE(*pdpe & PG_FRAME);
2619                 pdpg->wire_count++;
2620         } else {
2621                 /* Allocate a pd page. */
2622                 ptepindex = pmap_pde_pindex(va);
2623                 pdpindex = ptepindex >> NPDPEPGSHIFT;
2624                 pdpg = _pmap_allocpte(pmap, NUPDE + pdpindex, lockp);
2625                 if (pdpg == NULL && lockp != NULL)
2626                         goto retry;
2627         }
2628         return (pdpg);
2629 }
2630
2631 static vm_page_t
2632 pmap_allocpte(pmap_t pmap, vm_offset_t va, struct rwlock **lockp)
2633 {
2634         vm_pindex_t ptepindex;
2635         pd_entry_t *pd, PG_V;
2636         vm_page_t m;
2637
2638         PG_V = pmap_valid_bit(pmap);
2639
2640         /*
2641          * Calculate pagetable page index
2642          */
2643         ptepindex = pmap_pde_pindex(va);
2644 retry:
2645         /*
2646          * Get the page directory entry
2647          */
2648         pd = pmap_pde(pmap, va);
2649
2650         /*
2651          * This supports switching from a 2MB page to a
2652          * normal 4K page.
2653          */
2654         if (pd != NULL && (*pd & (PG_PS | PG_V)) == (PG_PS | PG_V)) {
2655                 if (!pmap_demote_pde_locked(pmap, pd, va, lockp)) {
2656                         /*
2657                          * Invalidation of the 2MB page mapping may have caused
2658                          * the deallocation of the underlying PD page.
2659                          */
2660                         pd = NULL;
2661                 }
2662         }
2663
2664         /*
2665          * If the page table page is mapped, we just increment the
2666          * hold count, and activate it.
2667          */
2668         if (pd != NULL && (*pd & PG_V) != 0) {
2669                 m = PHYS_TO_VM_PAGE(*pd & PG_FRAME);
2670                 m->wire_count++;
2671         } else {
2672                 /*
2673                  * Here if the pte page isn't mapped, or if it has been
2674                  * deallocated.
2675                  */
2676                 m = _pmap_allocpte(pmap, ptepindex, lockp);
2677                 if (m == NULL && lockp != NULL)
2678                         goto retry;
2679         }
2680         return (m);
2681 }
2682
2683
2684 /***************************************************
2685  * Pmap allocation/deallocation routines.
2686  ***************************************************/
2687
2688 /*
2689  * Release any resources held by the given physical map.
2690  * Called when a pmap initialized by pmap_pinit is being released.
2691  * Should only be called if the map contains no valid mappings.
2692  */
2693 void
2694 pmap_release(pmap_t pmap)
2695 {
2696         vm_page_t m;
2697         int i;
2698
2699         KASSERT(pmap->pm_stats.resident_count == 0,
2700             ("pmap_release: pmap resident count %ld != 0",
2701             pmap->pm_stats.resident_count));
2702         KASSERT(vm_radix_is_empty(&pmap->pm_root),
2703             ("pmap_release: pmap has reserved page table page(s)"));
2704         KASSERT(CPU_EMPTY(&pmap->pm_active),
2705             ("releasing active pmap %p", pmap));
2706
2707         m = PHYS_TO_VM_PAGE(DMAP_TO_PHYS((vm_offset_t)pmap->pm_pml4));
2708
2709         for (i = 0; i < NKPML4E; i++)   /* KVA */
2710                 pmap->pm_pml4[KPML4BASE + i] = 0;
2711         for (i = 0; i < ndmpdpphys; i++)/* Direct Map */
2712                 pmap->pm_pml4[DMPML4I + i] = 0;
2713         pmap->pm_pml4[PML4PML4I] = 0;   /* Recursive Mapping */
2714
2715         m->wire_count--;
2716         atomic_subtract_int(&vm_cnt.v_wire_count, 1);
2717         vm_page_free_zero(m);
2718 }
2719 \f
2720 static int
2721 kvm_size(SYSCTL_HANDLER_ARGS)
2722 {
2723         unsigned long ksize = VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS;
2724
2725         return sysctl_handle_long(oidp, &ksize, 0, req);
2726 }
2727 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD, 
2728     0, 0, kvm_size, "LU", "Size of KVM");
2729
2730 static int
2731 kvm_free(SYSCTL_HANDLER_ARGS)
2732 {
2733         unsigned long kfree = VM_MAX_KERNEL_ADDRESS - kernel_vm_end;
2734
2735         return sysctl_handle_long(oidp, &kfree, 0, req);
2736 }
2737 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD, 
2738     0, 0, kvm_free, "LU", "Amount of KVM free");
2739
2740 /*
2741  * grow the number of kernel page table entries, if needed
2742  */
2743 void
2744 pmap_growkernel(vm_offset_t addr)
2745 {
2746         vm_paddr_t paddr;
2747         vm_page_t nkpg;
2748         pd_entry_t *pde, newpdir;
2749         pdp_entry_t *pdpe;
2750
2751         mtx_assert(&kernel_map->system_mtx, MA_OWNED);
2752
2753         /*
2754          * Return if "addr" is within the range of kernel page table pages
2755          * that were preallocated during pmap bootstrap.  Moreover, leave
2756          * "kernel_vm_end" and the kernel page table as they were.
2757          *
2758          * The correctness of this action is based on the following
2759          * argument: vm_map_insert() allocates contiguous ranges of the
2760          * kernel virtual address space.  It calls this function if a range
2761          * ends after "kernel_vm_end".  If the kernel is mapped between
2762          * "kernel_vm_end" and "addr", then the range cannot begin at
2763          * "kernel_vm_end".  In fact, its beginning address cannot be less
2764          * than the kernel.  Thus, there is no immediate need to allocate
2765          * any new kernel page table pages between "kernel_vm_end" and
2766          * "KERNBASE".
2767          */
2768         if (KERNBASE < addr && addr <= KERNBASE + nkpt * NBPDR)
2769                 return;
2770
2771         addr = roundup2(addr, NBPDR);
2772         if (addr - 1 >= kernel_map->max_offset)
2773                 addr = kernel_map->max_offset;
2774         while (kernel_vm_end < addr) {
2775                 pdpe = pmap_pdpe(kernel_pmap, kernel_vm_end);
2776                 if ((*pdpe & X86_PG_V) == 0) {
2777                         /* We need a new PDP entry */
2778                         nkpg = vm_page_alloc(NULL, kernel_vm_end >> PDPSHIFT,
2779                             VM_ALLOC_INTERRUPT | VM_ALLOC_NOOBJ |
2780                             VM_ALLOC_WIRED | VM_ALLOC_ZERO);
2781                         if (nkpg == NULL)
2782                                 panic("pmap_growkernel: no memory to grow kernel");
2783                         if ((nkpg->flags & PG_ZERO) == 0)
2784                                 pmap_zero_page(nkpg);
2785                         paddr = VM_PAGE_TO_PHYS(nkpg);
2786                         *pdpe = (pdp_entry_t)(paddr | X86_PG_V | X86_PG_RW |
2787                             X86_PG_A | X86_PG_M);
2788                         continue; /* try again */
2789                 }
2790                 pde = pmap_pdpe_to_pde(pdpe, kernel_vm_end);
2791                 if ((*pde & X86_PG_V) != 0) {
2792                         kernel_vm_end = (kernel_vm_end + NBPDR) & ~PDRMASK;
2793                         if (kernel_vm_end - 1 >= kernel_map->max_offset) {
2794                                 kernel_vm_end = kernel_map->max_offset;
2795                                 break;                       
2796                         }
2797                         continue;
2798                 }
2799
2800                 nkpg = vm_page_alloc(NULL, pmap_pde_pindex(kernel_vm_end),
2801                     VM_ALLOC_INTERRUPT | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED |
2802                     VM_ALLOC_ZERO);
2803                 if (nkpg == NULL)
2804                         panic("pmap_growkernel: no memory to grow kernel");
2805                 if ((nkpg->flags & PG_ZERO) == 0)
2806                         pmap_zero_page(nkpg);
2807                 paddr = VM_PAGE_TO_PHYS(nkpg);
2808                 newpdir = paddr | X86_PG_V | X86_PG_RW | X86_PG_A | X86_PG_M;
2809                 pde_store(pde, newpdir);
2810
2811                 kernel_vm_end = (kernel_vm_end + NBPDR) & ~PDRMASK;
2812                 if (kernel_vm_end - 1 >= kernel_map->max_offset) {
2813                         kernel_vm_end = kernel_map->max_offset;
2814                         break;                       
2815                 }
2816         }
2817 }
2818
2819
2820 /***************************************************
2821  * page management routines.
2822  ***************************************************/
2823
2824 CTASSERT(sizeof(struct pv_chunk) == PAGE_SIZE);
2825 CTASSERT(_NPCM == 3);
2826 CTASSERT(_NPCPV == 168);
2827
2828 static __inline struct pv_chunk *
2829 pv_to_chunk(pv_entry_t pv)
2830 {
2831
2832         return ((struct pv_chunk *)((uintptr_t)pv & ~(uintptr_t)PAGE_MASK));
2833 }
2834
2835 #define PV_PMAP(pv) (pv_to_chunk(pv)->pc_pmap)
2836
2837 #define PC_FREE0        0xfffffffffffffffful
2838 #define PC_FREE1        0xfffffffffffffffful
2839 #define PC_FREE2        0x000000fffffffffful
2840
2841 static const uint64_t pc_freemask[_NPCM] = { PC_FREE0, PC_FREE1, PC_FREE2 };
2842
2843 #ifdef PV_STATS
2844 static int pc_chunk_count, pc_chunk_allocs, pc_chunk_frees, pc_chunk_tryfail;
2845
2846 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_count, CTLFLAG_RD, &pc_chunk_count, 0,
2847         "Current number of pv entry chunks");
2848 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_allocs, CTLFLAG_RD, &pc_chunk_allocs, 0,
2849         "Current number of pv entry chunks allocated");
2850 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_frees, CTLFLAG_RD, &pc_chunk_frees, 0,
2851         "Current number of pv entry chunks frees");
2852 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_tryfail, CTLFLAG_RD, &pc_chunk_tryfail, 0,
2853         "Number of times tried to get a chunk page but failed.");
2854
2855 static long pv_entry_frees, pv_entry_allocs, pv_entry_count;
2856 static int pv_entry_spare;
2857
2858 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_frees, CTLFLAG_RD, &pv_entry_frees, 0,
2859         "Current number of pv entry frees");
2860 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_allocs, CTLFLAG_RD, &pv_entry_allocs, 0,
2861         "Current number of pv entry allocs");
2862 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_count, CTLFLAG_RD, &pv_entry_count, 0,
2863         "Current number of pv entries");
2864 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_spare, CTLFLAG_RD, &pv_entry_spare, 0,
2865         "Current number of spare pv entries");
2866 #endif
2867
2868 static void
2869 reclaim_pv_chunk_leave_pmap(pmap_t pmap, pmap_t locked_pmap, bool start_di)
2870 {
2871
2872         if (pmap == NULL)
2873                 return;
2874         pmap_invalidate_all(pmap);
2875         if (pmap != locked_pmap)
2876                 PMAP_UNLOCK(pmap);
2877         if (start_di)
2878                 pmap_delayed_invl_finished();
2879 }
2880
2881 /*
2882  * We are in a serious low memory condition.  Resort to
2883  * drastic measures to free some pages so we can allocate
2884  * another pv entry chunk.
2885  *
2886  * Returns NULL if PV entries were reclaimed from the specified pmap.
2887  *
2888  * We do not, however, unmap 2mpages because subsequent accesses will
2889  * allocate per-page pv entries until repromotion occurs, thereby
2890  * exacerbating the shortage of free pv entries.
2891  */
2892 static vm_page_t
2893 reclaim_pv_chunk(pmap_t locked_pmap, struct rwlock **lockp)
2894 {
2895         struct pv_chunk *pc, *pc_marker;
2896         struct pv_chunk_header pc_marker_b;
2897         struct md_page *pvh;
2898         pd_entry_t *pde;
2899         pmap_t next_pmap, pmap;
2900         pt_entry_t *pte, tpte;
2901         pt_entry_t PG_G, PG_A, PG_M, PG_RW;
2902         pv_entry_t pv;
2903         vm_offset_t va;
2904         vm_page_t m, m_pc;
2905         struct spglist free;
2906         uint64_t inuse;
2907         int bit, field, freed;
2908         bool start_di;
2909
2910         PMAP_LOCK_ASSERT(locked_pmap, MA_OWNED);
2911         KASSERT(lockp != NULL, ("reclaim_pv_chunk: lockp is NULL"));
2912         pmap = NULL;
2913         m_pc = NULL;
2914         PG_G = PG_A = PG_M = PG_RW = 0;
2915         SLIST_INIT(&free);
2916         bzero(&pc_marker_b, sizeof(pc_marker_b));
2917         pc_marker = (struct pv_chunk *)&pc_marker_b;
2918
2919         /*
2920          * A delayed invalidation block should already be active if
2921          * pmap_advise() or pmap_remove() called this function by way
2922          * of pmap_demote_pde_locked().
2923          */
2924         start_di = pmap_not_in_di();
2925
2926         mtx_lock(&pv_chunks_mutex);
2927         TAILQ_INSERT_HEAD(&pv_chunks, pc_marker, pc_lru);
2928         while ((pc = TAILQ_NEXT(pc_marker, pc_lru)) != NULL &&
2929             SLIST_EMPTY(&free)) {
2930                 next_pmap = pc->pc_pmap;
2931                 if (next_pmap == NULL)          /* marker */
2932                         goto next_chunk;
2933                 mtx_unlock(&pv_chunks_mutex);
2934
2935                 /*
2936                  * A pv_chunk can only be removed from the pc_lru list
2937                  * when both pc_chunks_mutex is owned and the
2938                  * corresponding pmap is locked.
2939                  */
2940                 if (pmap != next_pmap) {
2941                         reclaim_pv_chunk_leave_pmap(pmap, locked_pmap,
2942                             start_di);
2943                         pmap = next_pmap;
2944                         /* Avoid deadlock and lock recursion. */
2945                         if (pmap > locked_pmap) {
2946                                 RELEASE_PV_LIST_LOCK(lockp);
2947                                 PMAP_LOCK(pmap);
2948                                 if (start_di)
2949                                         pmap_delayed_invl_started();
2950                                 mtx_lock(&pv_chunks_mutex);
2951                                 continue;
2952                         } else if (pmap != locked_pmap) {
2953                                 if (PMAP_TRYLOCK(pmap)) {
2954                                         if (start_di)
2955                                                 pmap_delayed_invl_started();
2956                                         mtx_lock(&pv_chunks_mutex);
2957                                         continue;
2958                                 } else {
2959                                         pmap = NULL; /* pmap is not locked */
2960                                         mtx_lock(&pv_chunks_mutex);
2961                                         pc = TAILQ_NEXT(pc_marker, pc_lru);
2962                                         if (pc == NULL ||
2963                                             pc->pc_pmap != next_pmap)
2964                                                 continue;
2965                                         goto next_chunk;
2966                                 }
2967                         } else if (start_di)
2968                                 pmap_delayed_invl_started();
2969                         PG_G = pmap_global_bit(pmap);
2970                         PG_A = pmap_accessed_bit(pmap);
2971                         PG_M = pmap_modified_bit(pmap);
2972                         PG_RW = pmap_rw_bit(pmap);
2973                 }
2974
2975                 /*
2976                  * Destroy every non-wired, 4 KB page mapping in the chunk.
2977                  */
2978                 freed = 0;
2979                 for (field = 0; field < _NPCM; field++) {
2980                         for (inuse = ~pc->pc_map[field] & pc_freemask[field];
2981                             inuse != 0; inuse &= ~(1UL << bit)) {
2982                                 bit = bsfq(inuse);
2983                                 pv = &pc->pc_pventry[field * 64 + bit];
2984                                 va = pv->pv_va;
2985                                 pde = pmap_pde(pmap, va);
2986                                 if ((*pde & PG_PS) != 0)
2987                                         continue;
2988                                 pte = pmap_pde_to_pte(pde, va);
2989                                 if ((*pte & PG_W) != 0)
2990                                         continue;
2991                                 tpte = pte_load_clear(pte);
2992                                 if ((tpte & PG_G) != 0)
2993                                         pmap_invalidate_page(pmap, va);
2994                                 m = PHYS_TO_VM_PAGE(tpte & PG_FRAME);
2995                                 if ((tpte & (PG_M | PG_RW)) == (PG_M | PG_RW))
2996                                         vm_page_dirty(m);
2997                                 if ((tpte & PG_A) != 0)
2998                                         vm_page_aflag_set(m, PGA_REFERENCED);
2999                                 CHANGE_PV_LIST_LOCK_TO_VM_PAGE(lockp, m);
3000                                 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
3001                                 m->md.pv_gen++;
3002                                 if (TAILQ_EMPTY(&m->md.pv_list) &&
3003                                     (m->flags & PG_FICTITIOUS) == 0) {
3004                                         pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
3005                                         if (TAILQ_EMPTY(&pvh->pv_list)) {
3006                                                 vm_page_aflag_clear(m,
3007                                                     PGA_WRITEABLE);
3008                                         }
3009                                 }
3010                                 pmap_delayed_invl_page(m);
3011                                 pc->pc_map[field] |= 1UL << bit;
3012                                 pmap_unuse_pt(pmap, va, *pde, &free);
3013                                 freed++;
3014                         }
3015                 }
3016                 if (freed == 0) {
3017                         mtx_lock(&pv_chunks_mutex);
3018                         goto next_chunk;
3019                 }
3020                 /* Every freed mapping is for a 4 KB page. */
3021                 pmap_resident_count_dec(pmap, freed);
3022                 PV_STAT(atomic_add_long(&pv_entry_frees, freed));
3023                 PV_STAT(atomic_add_int(&pv_entry_spare, freed));
3024                 PV_STAT(atomic_subtract_long(&pv_entry_count, freed));
3025                 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
3026                 if (pc->pc_map[0] == PC_FREE0 && pc->pc_map[1] == PC_FREE1 &&
3027                     pc->pc_map[2] == PC_FREE2) {
3028                         PV_STAT(atomic_subtract_int(&pv_entry_spare, _NPCPV));
3029                         PV_STAT(atomic_subtract_int(&pc_chunk_count, 1));
3030                         PV_STAT(atomic_add_int(&pc_chunk_frees, 1));
3031                         /* Entire chunk is free; return it. */
3032                         m_pc = PHYS_TO_VM_PAGE(DMAP_TO_PHYS((vm_offset_t)pc));
3033                         dump_drop_page(m_pc->phys_addr);
3034                         mtx_lock(&pv_chunks_mutex);
3035                         TAILQ_REMOVE(&pv_chunks, pc, pc_lru);
3036                         break;
3037                 }
3038                 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
3039                 mtx_lock(&pv_chunks_mutex);
3040                 /* One freed pv entry in locked_pmap is sufficient. */
3041                 if (pmap == locked_pmap)
3042                         break;
3043 next_chunk:
3044                 TAILQ_REMOVE(&pv_chunks, pc_marker, pc_lru);
3045                 TAILQ_INSERT_AFTER(&pv_chunks, pc, pc_marker, pc_lru);
3046         }
3047         TAILQ_REMOVE(&pv_chunks, pc_marker, pc_lru);
3048         mtx_unlock(&pv_chunks_mutex);
3049         reclaim_pv_chunk_leave_pmap(pmap, locked_pmap, start_di);
3050         if (m_pc == NULL && !SLIST_EMPTY(&free)) {
3051                 m_pc = SLIST_FIRST(&free);
3052                 SLIST_REMOVE_HEAD(&free, plinks.s.ss);
3053                 /* Recycle a freed page table page. */
3054                 m_pc->wire_count = 1;
3055         }
3056         pmap_free_zero_pages(&free);
3057         return (m_pc);
3058 }
3059
3060 /*
3061  * free the pv_entry back to the free list
3062  */
3063 static void
3064 free_pv_entry(pmap_t pmap, pv_entry_t pv)
3065 {
3066         struct pv_chunk *pc;
3067         int idx, field, bit;
3068
3069         PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3070         PV_STAT(atomic_add_long(&pv_entry_frees, 1));
3071         PV_STAT(atomic_add_int(&pv_entry_spare, 1));
3072         PV_STAT(atomic_subtract_long(&pv_entry_count, 1));
3073         pc = pv_to_chunk(pv);
3074         idx = pv - &pc->pc_pventry[0];
3075         field = idx / 64;
3076         bit = idx % 64;
3077         pc->pc_map[field] |= 1ul << bit;
3078         if (pc->pc_map[0] != PC_FREE0 || pc->pc_map[1] != PC_FREE1 ||
3079             pc->pc_map[2] != PC_FREE2) {
3080                 /* 98% of the time, pc is already at the head of the list. */
3081                 if (__predict_false(pc != TAILQ_FIRST(&pmap->pm_pvchunk))) {
3082                         TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
3083                         TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
3084                 }
3085                 return;
3086         }
3087         TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
3088         free_pv_chunk(pc);
3089 }
3090
3091 static void
3092 free_pv_chunk(struct pv_chunk *pc)
3093 {
3094         vm_page_t m;
3095
3096         mtx_lock(&pv_chunks_mutex);
3097         TAILQ_REMOVE(&pv_chunks, pc, pc_lru);
3098         mtx_unlock(&pv_chunks_mutex);
3099         PV_STAT(atomic_subtract_int(&pv_entry_spare, _NPCPV));
3100         PV_STAT(atomic_subtract_int(&pc_chunk_count, 1));
3101         PV_STAT(atomic_add_int(&pc_chunk_frees, 1));
3102         /* entire chunk is free, return it */
3103         m = PHYS_TO_VM_PAGE(DMAP_TO_PHYS((vm_offset_t)pc));
3104         dump_drop_page(m->phys_addr);
3105         vm_page_unwire(m, PQ_NONE);
3106         vm_page_free(m);
3107 }
3108
3109 /*
3110  * Returns a new PV entry, allocating a new PV chunk from the system when
3111  * needed.  If this PV chunk allocation fails and a PV list lock pointer was
3112  * given, a PV chunk is reclaimed from an arbitrary pmap.  Otherwise, NULL is
3113  * returned.
3114  *
3115  * The given PV list lock may be released.
3116  */
3117 static pv_entry_t
3118 get_pv_entry(pmap_t pmap, struct rwlock **lockp)
3119 {
3120         int bit, field;
3121         pv_entry_t pv;
3122         struct pv_chunk *pc;
3123         vm_page_t m;
3124
3125         PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3126         PV_STAT(atomic_add_long(&pv_entry_allocs, 1));
3127 retry:
3128         pc = TAILQ_FIRST(&pmap->pm_pvchunk);
3129         if (pc != NULL) {
3130                 for (field = 0; field < _NPCM; field++) {
3131                         if (pc->pc_map[field]) {
3132                                 bit = bsfq(pc->pc_map[field]);
3133                                 break;
3134                         }
3135                 }
3136                 if (field < _NPCM) {
3137                         pv = &pc->pc_pventry[field * 64 + bit];
3138                         pc->pc_map[field] &= ~(1ul << bit);
3139                         /* If this was the last item, move it to tail */
3140                         if (pc->pc_map[0] == 0 && pc->pc_map[1] == 0 &&
3141                             pc->pc_map[2] == 0) {
3142                                 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
3143                                 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc,
3144                                     pc_list);
3145                         }
3146                         PV_STAT(atomic_add_long(&pv_entry_count, 1));
3147                         PV_STAT(atomic_subtract_int(&pv_entry_spare, 1));
3148                         return (pv);
3149                 }
3150         }
3151         /* No free items, allocate another chunk */
3152         m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ |
3153             VM_ALLOC_WIRED);
3154         if (m == NULL) {
3155                 if (lockp == NULL) {
3156                         PV_STAT(pc_chunk_tryfail++);
3157                         return (NULL);
3158                 }
3159                 m = reclaim_pv_chunk(pmap, lockp);
3160                 if (m == NULL)
3161                         goto retry;
3162         }
3163         PV_STAT(atomic_add_int(&pc_chunk_count, 1));
3164         PV_STAT(atomic_add_int(&pc_chunk_allocs, 1));
3165         dump_add_page(m->phys_addr);
3166         pc = (void *)PHYS_TO_DMAP(m->phys_addr);
3167         pc->pc_pmap = pmap;
3168         pc->pc_map[0] = PC_FREE0 & ~1ul;        /* preallocated bit 0 */
3169         pc->pc_map[1] = PC_FREE1;
3170         pc->pc_map[2] = PC_FREE2;
3171         mtx_lock(&pv_chunks_mutex);
3172         TAILQ_INSERT_TAIL(&pv_chunks, pc, pc_lru);
3173         mtx_unlock(&pv_chunks_mutex);
3174         pv = &pc->pc_pventry[0];
3175         TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
3176         PV_STAT(atomic_add_long(&pv_entry_count, 1));
3177         PV_STAT(atomic_add_int(&pv_entry_spare, _NPCPV - 1));
3178         return (pv);
3179 }
3180
3181 /*
3182  * Returns the number of one bits within the given PV chunk map.
3183  *
3184  * The erratas for Intel processors state that "POPCNT Instruction May
3185  * Take Longer to Execute Than Expected".  It is believed that the
3186  * issue is the spurious dependency on the destination register.
3187  * Provide a hint to the register rename logic that the destination
3188  * value is overwritten, by clearing it, as suggested in the
3189  * optimization manual.  It should be cheap for unaffected processors
3190  * as well.
3191  *
3192  * Reference numbers for erratas are
3193  * 4th Gen Core: HSD146
3194  * 5th Gen Core: BDM85
3195  * 6th Gen Core: SKL029
3196  */
3197 static int
3198 popcnt_pc_map_pq(uint64_t *map)
3199 {
3200         u_long result, tmp;
3201
3202         __asm __volatile("xorl %k0,%k0;popcntq %2,%0;"
3203             "xorl %k1,%k1;popcntq %3,%1;addl %k1,%k0;"
3204             "xorl %k1,%k1;popcntq %4,%1;addl %k1,%k0"
3205             : "=&r" (result), "=&r" (tmp)
3206             : "m" (map[0]), "m" (map[1]), "m" (map[2]));
3207         return (result);
3208 }
3209
3210 /*
3211  * Ensure that the number of spare PV entries in the specified pmap meets or
3212  * exceeds the given count, "needed".
3213  *
3214  * The given PV list lock may be released.
3215  */
3216 static void
3217 reserve_pv_entries(pmap_t pmap, int needed, struct rwlock **lockp)
3218 {
3219         struct pch new_tail;
3220         struct pv_chunk *pc;
3221         int avail, free;
3222         vm_page_t m;
3223
3224         PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3225         KASSERT(lockp != NULL, ("reserve_pv_entries: lockp is NULL"));
3226
3227         /*
3228          * Newly allocated PV chunks must be stored in a private list until
3229          * the required number of PV chunks have been allocated.  Otherwise,
3230          * reclaim_pv_chunk() could recycle one of these chunks.  In
3231          * contrast, these chunks must be added to the pmap upon allocation.
3232          */
3233         TAILQ_INIT(&new_tail);
3234 retry:
3235         avail = 0;
3236         TAILQ_FOREACH(pc, &pmap->pm_pvchunk, pc_list) {
3237 #ifndef __POPCNT__
3238                 if ((cpu_feature2 & CPUID2_POPCNT) == 0)
3239                         bit_count((bitstr_t *)pc->pc_map, 0,
3240                             sizeof(pc->pc_map) * NBBY, &free);
3241                 else
3242 #endif
3243                 free = popcnt_pc_map_pq(pc->pc_map);
3244                 if (free == 0)
3245                         break;
3246                 avail += free;
3247                 if (avail >= needed)
3248                         break;
3249         }
3250         for (; avail < needed; avail += _NPCPV) {
3251                 m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ |
3252                     VM_ALLOC_WIRED);
3253                 if (m == NULL) {
3254                         m = reclaim_pv_chunk(pmap, lockp);
3255                         if (m == NULL)
3256                                 goto retry;
3257                 }
3258                 PV_STAT(atomic_add_int(&pc_chunk_count, 1));
3259                 PV_STAT(atomic_add_int(&pc_chunk_allocs, 1));
3260                 dump_add_page(m->phys_addr);
3261                 pc = (void *)PHYS_TO_DMAP(m->phys_addr);
3262                 pc->pc_pmap = pmap;
3263                 pc->pc_map[0] = PC_FREE0;
3264                 pc->pc_map[1] = PC_FREE1;
3265                 pc->pc_map[2] = PC_FREE2;
3266                 TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
3267                 TAILQ_INSERT_TAIL(&new_tail, pc, pc_lru);
3268                 PV_STAT(atomic_add_int(&pv_entry_spare, _NPCPV));
3269         }
3270         if (!TAILQ_EMPTY(&new_tail)) {
3271                 mtx_lock(&pv_chunks_mutex);
3272                 TAILQ_CONCAT(&pv_chunks, &new_tail, pc_lru);
3273                 mtx_unlock(&pv_chunks_mutex);
3274         }
3275 }
3276
3277 /*
3278  * First find and then remove the pv entry for the specified pmap and virtual
3279  * address from the specified pv list.  Returns the pv entry if found and NULL
3280  * otherwise.  This operation can be performed on pv lists for either 4KB or
3281  * 2MB page mappings.
3282  */
3283 static __inline pv_entry_t
3284 pmap_pvh_remove(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
3285 {
3286         pv_entry_t pv;
3287
3288         TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
3289                 if (pmap == PV_PMAP(pv) && va == pv->pv_va) {
3290                         TAILQ_REMOVE(&pvh->pv_list, pv, pv_next);
3291                         pvh->pv_gen++;
3292                         break;
3293                 }
3294         }
3295         return (pv);
3296 }
3297
3298 /*
3299  * After demotion from a 2MB page mapping to 512 4KB page mappings,
3300  * destroy the pv entry for the 2MB page mapping and reinstantiate the pv
3301  * entries for each of the 4KB page mappings.
3302  */
3303 static void
3304 pmap_pv_demote_pde(pmap_t pmap, vm_offset_t va, vm_paddr_t pa,
3305     struct rwlock **lockp)
3306 {
3307         struct md_page *pvh;
3308         struct pv_chunk *pc;
3309         pv_entry_t pv;
3310         vm_offset_t va_last;
3311         vm_page_t m;
3312         int bit, field;
3313
3314         PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3315         KASSERT((pa & PDRMASK) == 0,
3316             ("pmap_pv_demote_pde: pa is not 2mpage aligned"));
3317         CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, pa);
3318
3319         /*
3320          * Transfer the 2mpage's pv entry for this mapping to the first
3321          * page's pv list.  Once this transfer begins, the pv list lock
3322          * must not be released until the last pv entry is reinstantiated.
3323          */
3324         pvh = pa_to_pvh(pa);
3325         va = trunc_2mpage(va);
3326         pv = pmap_pvh_remove(pvh, pmap, va);
3327         KASSERT(pv != NULL, ("pmap_pv_demote_pde: pv not found"));
3328         m = PHYS_TO_VM_PAGE(pa);
3329         TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
3330         m->md.pv_gen++;
3331         /* Instantiate the remaining NPTEPG - 1 pv entries. */
3332         PV_STAT(atomic_add_long(&pv_entry_allocs, NPTEPG - 1));
3333         va_last = va + NBPDR - PAGE_SIZE;
3334         for (;;) {
3335                 pc = TAILQ_FIRST(&pmap->pm_pvchunk);
3336                 KASSERT(pc->pc_map[0] != 0 || pc->pc_map[1] != 0 ||
3337                     pc->pc_map[2] != 0, ("pmap_pv_demote_pde: missing spare"));
3338                 for (field = 0; field < _NPCM; field++) {
3339                         while (pc->pc_map[field]) {
3340                                 bit = bsfq(pc->pc_map[field]);
3341                                 pc->pc_map[field] &= ~(1ul << bit);
3342                                 pv = &pc->pc_pventry[field * 64 + bit];
3343                                 va += PAGE_SIZE;
3344                                 pv->pv_va = va;
3345                                 m++;
3346                                 KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3347                             ("pmap_pv_demote_pde: page %p is not managed", m));
3348                                 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
3349                                 m->md.pv_gen++;
3350                                 if (va == va_last)
3351                                         goto out;
3352                         }
3353                 }
3354                 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
3355                 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc, pc_list);
3356         }
3357 out:
3358         if (pc->pc_map[0] == 0 && pc->pc_map[1] == 0 && pc->pc_map[2] == 0) {
3359                 TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
3360                 TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc, pc_list);
3361         }
3362         PV_STAT(atomic_add_long(&pv_entry_count, NPTEPG - 1));
3363         PV_STAT(atomic_subtract_int(&pv_entry_spare, NPTEPG - 1));
3364 }
3365
3366 /*
3367  * After promotion from 512 4KB page mappings to a single 2MB page mapping,
3368  * replace the many pv entries for the 4KB page mappings by a single pv entry
3369  * for the 2MB page mapping.
3370  */
3371 static void
3372 pmap_pv_promote_pde(pmap_t pmap, vm_offset_t va, vm_paddr_t pa,
3373     struct rwlock **lockp)
3374 {
3375         struct md_page *pvh;
3376         pv_entry_t pv;
3377         vm_offset_t va_last;
3378         vm_page_t m;
3379
3380         KASSERT((pa & PDRMASK) == 0,
3381             ("pmap_pv_promote_pde: pa is not 2mpage aligned"));
3382         CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, pa);
3383
3384         /*
3385          * Transfer the first page's pv entry for this mapping to the 2mpage's
3386          * pv list.  Aside from avoiding the cost of a call to get_pv_entry(),
3387          * a transfer avoids the possibility that get_pv_entry() calls
3388          * reclaim_pv_chunk() and that reclaim_pv_chunk() removes one of the
3389          * mappings that is being promoted.
3390          */
3391         m = PHYS_TO_VM_PAGE(pa);
3392         va = trunc_2mpage(va);
3393         pv = pmap_pvh_remove(&m->md, pmap, va);
3394         KASSERT(pv != NULL, ("pmap_pv_promote_pde: pv not found"));
3395         pvh = pa_to_pvh(pa);
3396         TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_next);
3397         pvh->pv_gen++;
3398         /* Free the remaining NPTEPG - 1 pv entries. */
3399         va_last = va + NBPDR - PAGE_SIZE;
3400         do {
3401                 m++;
3402                 va += PAGE_SIZE;
3403                 pmap_pvh_free(&m->md, pmap, va);
3404         } while (va < va_last);
3405 }
3406
3407 /*
3408  * First find and then destroy the pv entry for the specified pmap and virtual
3409  * address.  This operation can be performed on pv lists for either 4KB or 2MB
3410  * page mappings.
3411  */
3412 static void
3413 pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
3414 {
3415         pv_entry_t pv;
3416
3417         pv = pmap_pvh_remove(pvh, pmap, va);
3418         KASSERT(pv != NULL, ("pmap_pvh_free: pv not found"));
3419         free_pv_entry(pmap, pv);
3420 }
3421
3422 /*
3423  * Conditionally create the PV entry for a 4KB page mapping if the required
3424  * memory can be allocated without resorting to reclamation.
3425  */
3426 static boolean_t
3427 pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va, vm_page_t m,
3428     struct rwlock **lockp)
3429 {
3430         pv_entry_t pv;
3431
3432         PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3433         /* Pass NULL instead of the lock pointer to disable reclamation. */
3434         if ((pv = get_pv_entry(pmap, NULL)) != NULL) {
3435                 pv->pv_va = va;
3436                 CHANGE_PV_LIST_LOCK_TO_VM_PAGE(lockp, m);
3437                 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
3438                 m->md.pv_gen++;
3439                 return (TRUE);
3440         } else
3441                 return (FALSE);
3442 }
3443
3444 /*
3445  * Create the PV entry for a 2MB page mapping.  Always returns true unless the
3446  * flag PMAP_ENTER_NORECLAIM is specified.  If that flag is specified, returns
3447  * false if the PV entry cannot be allocated without resorting to reclamation.
3448  */
3449 static bool
3450 pmap_pv_insert_pde(pmap_t pmap, vm_offset_t va, pd_entry_t pde, u_int flags,
3451     struct rwlock **lockp)
3452 {
3453         struct md_page *pvh;
3454         pv_entry_t pv;
3455         vm_paddr_t pa;
3456
3457         PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3458         /* Pass NULL instead of the lock pointer to disable reclamation. */
3459         if ((pv = get_pv_entry(pmap, (flags & PMAP_ENTER_NORECLAIM) != 0 ?
3460             NULL : lockp)) == NULL)
3461                 return (false);
3462         pv->pv_va = va;
3463         pa = pde & PG_PS_FRAME;
3464         CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, pa);
3465         pvh = pa_to_pvh(pa);
3466         TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_next);
3467         pvh->pv_gen++;
3468         return (true);
3469 }
3470
3471 /*
3472  * Fills a page table page with mappings to consecutive physical pages.
3473  */
3474 static void
3475 pmap_fill_ptp(pt_entry_t *firstpte, pt_entry_t newpte)
3476 {
3477         pt_entry_t *pte;
3478
3479         for (pte = firstpte; pte < firstpte + NPTEPG; pte++) {
3480                 *pte = newpte;
3481                 newpte += PAGE_SIZE;
3482         }
3483 }
3484
3485 /*
3486  * Tries to demote a 2MB page mapping.  If demotion fails, the 2MB page
3487  * mapping is invalidated.
3488  */
3489 static boolean_t
3490 pmap_demote_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va)
3491 {
3492         struct rwlock *lock;
3493         boolean_t rv;
3494
3495         lock = NULL;
3496         rv = pmap_demote_pde_locked(pmap, pde, va, &lock);
3497         if (lock != NULL)
3498                 rw_wunlock(lock);
3499         return (rv);
3500 }
3501
3502 static boolean_t
3503 pmap_demote_pde_locked(pmap_t pmap, pd_entry_t *pde, vm_offset_t va,
3504     struct rwlock **lockp)
3505 {
3506         pd_entry_t newpde, oldpde;
3507         pt_entry_t *firstpte, newpte;
3508         pt_entry_t PG_A, PG_G, PG_M, PG_RW, PG_V;
3509         vm_paddr_t mptepa;
3510         vm_page_t mpte;
3511         struct spglist free;
3512         vm_offset_t sva;
3513         int PG_PTE_CACHE;
3514
3515         PG_G = pmap_global_bit(pmap);
3516         PG_A = pmap_accessed_bit(pmap);
3517         PG_M = pmap_modified_bit(pmap);
3518         PG_RW = pmap_rw_bit(pmap);
3519         PG_V = pmap_valid_bit(pmap);
3520         PG_PTE_CACHE = pmap_cache_mask(pmap, 0);
3521
3522         PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3523         oldpde = *pde;
3524         KASSERT((oldpde & (PG_PS | PG_V)) == (PG_PS | PG_V),
3525             ("pmap_demote_pde: oldpde is missing PG_PS and/or PG_V"));
3526         if ((oldpde & PG_A) == 0 || (mpte = pmap_remove_pt_page(pmap, va)) ==
3527             NULL) {
3528                 KASSERT((oldpde & PG_W) == 0,
3529                     ("pmap_demote_pde: page table page for a wired mapping"
3530                     " is missing"));
3531
3532                 /*
3533                  * Invalidate the 2MB page mapping and return "failure" if the
3534                  * mapping was never accessed or the allocation of the new
3535                  * page table page fails.  If the 2MB page mapping belongs to
3536                  * the direct map region of the kernel's address space, then
3537                  * the page allocation request specifies the highest possible
3538                  * priority (VM_ALLOC_INTERRUPT).  Otherwise, the priority is
3539                  * normal.  Page table pages are preallocated for every other
3540                  * part of the kernel address space, so the direct map region
3541                  * is the only part of the kernel address space that must be
3542                  * handled here.
3543                  */
3544                 if ((oldpde & PG_A) == 0 || (mpte = vm_page_alloc(NULL,
3545                     pmap_pde_pindex(va), (va >= DMAP_MIN_ADDRESS && va <
3546                     DMAP_MAX_ADDRESS ? VM_ALLOC_INTERRUPT : VM_ALLOC_NORMAL) |
3547                     VM_ALLOC_NOOBJ | VM_ALLOC_WIRED)) == NULL) {
3548                         SLIST_INIT(&free);
3549                         sva = trunc_2mpage(va);
3550                         pmap_remove_pde(pmap, pde, sva, &free, lockp);
3551                         if ((oldpde & PG_G) == 0)
3552                                 pmap_invalidate_pde_page(pmap, sva, oldpde);
3553                         pmap_free_zero_pages(&free);
3554                         CTR2(KTR_PMAP, "pmap_demote_pde: failure for va %#lx"
3555                             " in pmap %p", va, pmap);
3556                         return (FALSE);
3557                 }
3558                 if (va < VM_MAXUSER_ADDRESS)
3559                         pmap_resident_count_inc(pmap, 1);
3560         }
3561         mptepa = VM_PAGE_TO_PHYS(mpte);
3562         firstpte = (pt_entry_t *)PHYS_TO_DMAP(mptepa);
3563         newpde = mptepa | PG_M | PG_A | (oldpde & PG_U) | PG_RW | PG_V;
3564         KASSERT((oldpde & PG_A) != 0,
3565             ("pmap_demote_pde: oldpde is missing PG_A"));
3566         KASSERT((oldpde & (PG_M | PG_RW)) != PG_RW,
3567             ("pmap_demote_pde: oldpde is missing PG_M"));
3568         newpte = oldpde & ~PG_PS;
3569         newpte = pmap_swap_pat(pmap, newpte);
3570
3571         /*
3572          * If the page table page is new, initialize it.
3573          */
3574         if (mpte->wire_count == 1) {
3575                 mpte->wire_count = NPTEPG;
3576                 pmap_fill_ptp(firstpte, newpte);
3577         }
3578         KASSERT((*firstpte & PG_FRAME) == (newpte & PG_FRAME),
3579             ("pmap_demote_pde: firstpte and newpte map different physical"
3580             " addresses"));
3581
3582         /*
3583          * If the mapping has changed attributes, update the page table
3584          * entries.
3585          */
3586         if ((*firstpte & PG_PTE_PROMOTE) != (newpte & PG_PTE_PROMOTE))
3587                 pmap_fill_ptp(firstpte, newpte);
3588
3589         /*
3590          * The spare PV entries must be reserved prior to demoting the
3591          * mapping, that is, prior to changing the PDE.  Otherwise, the state
3592          * of the PDE and the PV lists will be inconsistent, which can result
3593          * in reclaim_pv_chunk() attempting to remove a PV entry from the
3594          * wrong PV list and pmap_pv_demote_pde() failing to find the expected
3595          * PV entry for the 2MB page mapping that is being demoted.
3596          */
3597         if ((oldpde & PG_MANAGED) != 0)
3598                 reserve_pv_entries(pmap, NPTEPG - 1, lockp);
3599
3600         /*
3601          * Demote the mapping.  This pmap is locked.  The old PDE has
3602          * PG_A set.  If the old PDE has PG_RW set, it also has PG_M
3603          * set.  Thus, there is no danger of a race with another
3604          * processor changing the setting of PG_A and/or PG_M between
3605          * the read above and the store below. 
3606          */
3607         if (workaround_erratum383)
3608                 pmap_update_pde(pmap, va, pde, newpde);
3609         else
3610                 pde_store(pde, newpde);
3611
3612         /*
3613          * Invalidate a stale recursive mapping of the page table page.
3614          */
3615         if (va >= VM_MAXUSER_ADDRESS)
3616                 pmap_invalidate_page(pmap, (vm_offset_t)vtopte(va));
3617
3618         /*
3619          * Demote the PV entry.
3620          */
3621         if ((oldpde & PG_MANAGED) != 0)
3622                 pmap_pv_demote_pde(pmap, va, oldpde & PG_PS_FRAME, lockp);
3623
3624         atomic_add_long(&pmap_pde_demotions, 1);
3625         CTR2(KTR_PMAP, "pmap_demote_pde: success for va %#lx"
3626             " in pmap %p", va, pmap);
3627         return (TRUE);
3628 }
3629
3630 /*
3631  * pmap_remove_kernel_pde: Remove a kernel superpage mapping.
3632  */
3633 static void
3634 pmap_remove_kernel_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va)
3635 {
3636         pd_entry_t newpde;
3637         vm_paddr_t mptepa;
3638         vm_page_t mpte;
3639
3640         KASSERT(pmap == kernel_pmap, ("pmap %p is not kernel_pmap", pmap));
3641         PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3642         mpte = pmap_remove_pt_page(pmap, va);
3643         if (mpte == NULL)
3644                 panic("pmap_remove_kernel_pde: Missing pt page.");
3645
3646         mptepa = VM_PAGE_TO_PHYS(mpte);
3647         newpde = mptepa | X86_PG_M | X86_PG_A | X86_PG_RW | X86_PG_V;
3648
3649         /*
3650          * Initialize the page table page.
3651          */
3652         pagezero((void *)PHYS_TO_DMAP(mptepa));
3653
3654         /*
3655          * Demote the mapping.
3656          */
3657         if (workaround_erratum383)
3658                 pmap_update_pde(pmap, va, pde, newpde);
3659         else
3660                 pde_store(pde, newpde);
3661
3662         /*
3663          * Invalidate a stale recursive mapping of the page table page.
3664          */
3665         pmap_invalidate_page(pmap, (vm_offset_t)vtopte(va));
3666 }
3667
3668 /*
3669  * pmap_remove_pde: do the things to unmap a superpage in a process
3670  */
3671 static int
3672 pmap_remove_pde(pmap_t pmap, pd_entry_t *pdq, vm_offset_t sva,
3673     struct spglist *free, struct rwlock **lockp)
3674 {
3675         struct md_page *pvh;
3676         pd_entry_t oldpde;
3677         vm_offset_t eva, va;
3678         vm_page_t m, mpte;
3679         pt_entry_t PG_G, PG_A, PG_M, PG_RW;
3680
3681         PG_G = pmap_global_bit(pmap);
3682         PG_A = pmap_accessed_bit(pmap);
3683         PG_M = pmap_modified_bit(pmap);
3684         PG_RW = pmap_rw_bit(pmap);
3685
3686         PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3687         KASSERT((sva & PDRMASK) == 0,
3688             ("pmap_remove_pde: sva is not 2mpage aligned"));
3689         oldpde = pte_load_clear(pdq);
3690         if (oldpde & PG_W)
3691                 pmap->pm_stats.wired_count -= NBPDR / PAGE_SIZE;
3692         if ((oldpde & PG_G) != 0)
3693                 pmap_invalidate_pde_page(kernel_pmap, sva, oldpde);
3694         pmap_resident_count_dec(pmap, NBPDR / PAGE_SIZE);
3695         if (oldpde & PG_MANAGED) {
3696                 CHANGE_PV_LIST_LOCK_TO_PHYS(lockp, oldpde & PG_PS_FRAME);
3697                 pvh = pa_to_pvh(oldpde & PG_PS_FRAME);
3698                 pmap_pvh_free(pvh, pmap, sva);
3699                 eva = sva + NBPDR;
3700                 for (va = sva, m = PHYS_TO_VM_PAGE(oldpde & PG_PS_FRAME);
3701                     va < eva; va += PAGE_SIZE, m++) {
3702                         if ((oldpde & (PG_M | PG_RW)) == (PG_M | PG_RW))
3703                                 vm_page_dirty(m);
3704                         if (oldpde & PG_A)
3705                                 vm_page_aflag_set(m, PGA_REFERENCED);
3706                         if (TAILQ_EMPTY(&m->md.pv_list) &&
3707                             TAILQ_EMPTY(&pvh->pv_list))
3708                                 vm_page_aflag_clear(m, PGA_WRITEABLE);
3709                         pmap_delayed_invl_page(m);
3710                 }
3711         }
3712         if (pmap == kernel_pmap) {
3713                 pmap_remove_kernel_pde(pmap, pdq, sva);
3714         } else {
3715                 mpte = pmap_remove_pt_page(pmap, sva);
3716                 if (mpte != NULL) {
3717                         pmap_resident_count_dec(pmap, 1);
3718                         KASSERT(mpte->wire_count == NPTEPG,
3719                             ("pmap_remove_pde: pte page wire count error"));
3720                         mpte->wire_count = 0;
3721                         pmap_add_delayed_free_list(mpte, free, FALSE);
3722                 }
3723         }
3724         return (pmap_unuse_pt(pmap, sva, *pmap_pdpe(pmap, sva), free));
3725 }
3726
3727 /*
3728  * pmap_remove_pte: do the things to unmap a page in a process
3729  */
3730 static int
3731 pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t va, 
3732     pd_entry_t ptepde, struct spglist *free, struct rwlock **lockp)
3733 {
3734         struct md_page *pvh;
3735         pt_entry_t oldpte, PG_A, PG_M, PG_RW;
3736         vm_page_t m;
3737
3738         PG_A = pmap_accessed_bit(pmap);
3739         PG_M = pmap_modified_bit(pmap);
3740         PG_RW = pmap_rw_bit(pmap);
3741
3742         PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3743         oldpte = pte_load_clear(ptq);
3744         if (oldpte & PG_W)
3745                 pmap->pm_stats.wired_count -= 1;
3746         pmap_resident_count_dec(pmap, 1);
3747         if (oldpte & PG_MANAGED) {
3748                 m = PHYS_TO_VM_PAGE(oldpte & PG_FRAME);
3749                 if ((oldpte & (PG_M | PG_RW)) == (PG_M | PG_RW))
3750                         vm_page_dirty(m);
3751                 if (oldpte & PG_A)
3752                         vm_page_aflag_set(m, PGA_REFERENCED);
3753                 CHANGE_PV_LIST_LOCK_TO_VM_PAGE(lockp, m);
3754                 pmap_pvh_free(&m->md, pmap, va);
3755                 if (TAILQ_EMPTY(&m->md.pv_list) &&
3756                     (m->flags & PG_FICTITIOUS) == 0) {
3757                         pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
3758                         if (TAILQ_EMPTY(&pvh->pv_list))
3759                                 vm_page_aflag_clear(m, PGA_WRITEABLE);
3760                 }
3761                 pmap_delayed_invl_page(m);
3762         }
3763         return (pmap_unuse_pt(pmap, va, ptepde, free));
3764 }
3765
3766 /*
3767  * Remove a single page from a process address space
3768  */
3769 static void
3770 pmap_remove_page(pmap_t pmap, vm_offset_t va, pd_entry_t *pde,
3771     struct spglist *free)
3772 {
3773         struct rwlock *lock;
3774         pt_entry_t *pte, PG_V;
3775
3776         PG_V = pmap_valid_bit(pmap);
3777         PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3778         if ((*pde & PG_V) == 0)
3779                 return;
3780         pte = pmap_pde_to_pte(pde, va);
3781         if ((*pte & PG_V) == 0)
3782                 return;
3783         lock = NULL;
3784         pmap_remove_pte(pmap, pte, va, *pde, free, &lock);
3785         if (lock != NULL)
3786                 rw_wunlock(lock);
3787         pmap_invalidate_page(pmap, va);
3788 }
3789
3790 /*
3791  * Removes the specified range of addresses from the page table page.
3792  */
3793 static bool
3794 pmap_remove_ptes(pmap_t pmap, vm_offset_t sva, vm_offset_t eva,
3795     pd_entry_t *pde, struct spglist *free, struct rwlock **lockp)
3796 {
3797         pt_entry_t PG_G, *pte;
3798         vm_offset_t va;
3799         bool anyvalid;
3800
3801         PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3802         PG_G = pmap_global_bit(pmap);
3803         anyvalid = false;
3804         va = eva;
3805         for (pte = pmap_pde_to_pte(pde, sva); sva != eva; pte++,
3806             sva += PAGE_SIZE) {
3807                 if (*pte == 0) {
3808                         if (va != eva) {
3809                                 pmap_invalidate_range(pmap, va, sva);
3810                                 va = eva;
3811                         }
3812                         continue;
3813                 }
3814                 if ((*pte & PG_G) == 0)
3815                         anyvalid = true;
3816                 else if (va == eva)
3817                         va = sva;
3818                 if (pmap_remove_pte(pmap, pte, sva, *pde, free, lockp)) {
3819                         sva += PAGE_SIZE;
3820                         break;
3821                 }
3822         }
3823         if (va != eva)
3824                 pmap_invalidate_range(pmap, va, sva);
3825         return (anyvalid);
3826 }
3827
3828 /*
3829  *      Remove the given range of addresses from the specified map.
3830  *
3831  *      It is assumed that the start and end are properly
3832  *      rounded to the page size.
3833  */
3834 void
3835 pmap_remove(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
3836 {
3837         struct rwlock *lock;
3838         vm_offset_t va_next;
3839         pml4_entry_t *pml4e;
3840         pdp_entry_t *pdpe;
3841         pd_entry_t ptpaddr, *pde;
3842         pt_entry_t PG_G, PG_V;
3843         struct spglist free;
3844         int anyvalid;
3845
3846         PG_G = pmap_global_bit(pmap);
3847         PG_V = pmap_valid_bit(pmap);
3848
3849         /*
3850          * Perform an unsynchronized read.  This is, however, safe.
3851          */
3852         if (pmap->pm_stats.resident_count == 0)
3853                 return;
3854
3855         anyvalid = 0;
3856         SLIST_INIT(&free);
3857
3858         pmap_delayed_invl_started();
3859         PMAP_LOCK(pmap);
3860
3861         /*
3862          * special handling of removing one page.  a very
3863          * common operation and easy to short circuit some
3864          * code.
3865          */
3866         if (sva + PAGE_SIZE == eva) {
3867                 pde = pmap_pde(pmap, sva);
3868                 if (pde && (*pde & PG_PS) == 0) {
3869                         pmap_remove_page(pmap, sva, pde, &free);
3870                         goto out;
3871                 }
3872         }
3873
3874         lock = NULL;
3875         for (; sva < eva; sva = va_next) {
3876
3877                 if (pmap->pm_stats.resident_count == 0)
3878                         break;
3879
3880                 pml4e = pmap_pml4e(pmap, sva);
3881                 if ((*pml4e & PG_V) == 0) {
3882                         va_next = (sva + NBPML4) & ~PML4MASK;
3883                         if (va_next < sva)
3884                                 va_next = eva;
3885                         continue;
3886                 }
3887
3888                 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
3889                 if ((*pdpe & PG_V) == 0) {
3890                         va_next = (sva + NBPDP) & ~PDPMASK;
3891                         if (va_next < sva)
3892                                 va_next = eva;
3893                         continue;
3894                 }
3895
3896                 /*
3897                  * Calculate index for next page table.
3898                  */
3899                 va_next = (sva + NBPDR) & ~PDRMASK;
3900                 if (va_next < sva)
3901                         va_next = eva;
3902
3903                 pde = pmap_pdpe_to_pde(pdpe, sva);
3904                 ptpaddr = *pde;
3905
3906                 /*
3907                  * Weed out invalid mappings.
3908                  */
3909                 if (ptpaddr == 0)
3910                         continue;
3911
3912                 /*
3913                  * Check for large page.
3914                  */
3915                 if ((ptpaddr & PG_PS) != 0) {
3916                         /*
3917                          * Are we removing the entire large page?  If not,
3918                          * demote the mapping and fall through.
3919                          */
3920                         if (sva + NBPDR == va_next && eva >= va_next) {
3921                                 /*
3922                                  * The TLB entry for a PG_G mapping is
3923                                  * invalidated by pmap_remove_pde().
3924                                  */
3925                                 if ((ptpaddr & PG_G) == 0)
3926                                         anyvalid = 1;
3927                                 pmap_remove_pde(pmap, pde, sva, &free, &lock);
3928                                 continue;
3929                         } else if (!pmap_demote_pde_locked(pmap, pde, sva,
3930                             &lock)) {
3931                                 /* The large page mapping was destroyed. */
3932                                 continue;
3933                         } else
3934                                 ptpaddr = *pde;
3935                 }
3936
3937                 /*
3938                  * Limit our scan to either the end of the va represented
3939                  * by the current page table page, or to the end of the
3940                  * range being removed.
3941                  */
3942                 if (va_next > eva)
3943                         va_next = eva;
3944
3945                 if (pmap_remove_ptes(pmap, sva, va_next, pde, &free, &lock))
3946                         anyvalid = 1;
3947         }
3948         if (lock != NULL)
3949                 rw_wunlock(lock);
3950 out:
3951         if (anyvalid)
3952                 pmap_invalidate_all(pmap);
3953         PMAP_UNLOCK(pmap);
3954         pmap_delayed_invl_finished();
3955         pmap_free_zero_pages(&free);
3956 }
3957
3958 /*
3959  *      Routine:        pmap_remove_all
3960  *      Function:
3961  *              Removes this physical page from
3962  *              all physical maps in which it resides.
3963  *              Reflects back modify bits to the pager.
3964  *
3965  *      Notes:
3966  *              Original versions of this routine were very
3967  *              inefficient because they iteratively called
3968  *              pmap_remove (slow...)
3969  */
3970
3971 void
3972 pmap_remove_all(vm_page_t m)
3973 {
3974         struct md_page *pvh;
3975         pv_entry_t pv;
3976         pmap_t pmap;
3977         struct rwlock *lock;
3978         pt_entry_t *pte, tpte, PG_A, PG_M, PG_RW;
3979         pd_entry_t *pde;
3980         vm_offset_t va;
3981         struct spglist free;
3982         int pvh_gen, md_gen;
3983
3984         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3985             ("pmap_remove_all: page %p is not managed", m));
3986         SLIST_INIT(&free);
3987         lock = VM_PAGE_TO_PV_LIST_LOCK(m);
3988         pvh = (m->flags & PG_FICTITIOUS) != 0 ? &pv_dummy :
3989             pa_to_pvh(VM_PAGE_TO_PHYS(m));
3990 retry:
3991         rw_wlock(lock);
3992         while ((pv = TAILQ_FIRST(&pvh->pv_list)) != NULL) {
3993                 pmap = PV_PMAP(pv);
3994                 if (!PMAP_TRYLOCK(pmap)) {
3995                         pvh_gen = pvh->pv_gen;
3996                         rw_wunlock(lock);
3997                         PMAP_LOCK(pmap);
3998                         rw_wlock(lock);
3999                         if (pvh_gen != pvh->pv_gen) {
4000                                 rw_wunlock(lock);
4001                                 PMAP_UNLOCK(pmap);
4002                                 goto retry;
4003                         }
4004                 }
4005                 va = pv->pv_va;
4006                 pde = pmap_pde(pmap, va);
4007                 (void)pmap_demote_pde_locked(pmap, pde, va, &lock);
4008                 PMAP_UNLOCK(pmap);
4009         }
4010         while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
4011                 pmap = PV_PMAP(pv);
4012                 if (!PMAP_TRYLOCK(pmap)) {
4013                         pvh_gen = pvh->pv_gen;
4014                         md_gen = m->md.pv_gen;
4015                         rw_wunlock(lock);
4016                         PMAP_LOCK(pmap);
4017                         rw_wlock(lock);
4018                         if (pvh_gen != pvh->pv_gen || md_gen != m->md.pv_gen) {
4019                                 rw_wunlock(lock);
4020                                 PMAP_UNLOCK(pmap);
4021                                 goto retry;
4022                         }
4023                 }
4024                 PG_A = pmap_accessed_bit(pmap);
4025                 PG_M = pmap_modified_bit(pmap);
4026                 PG_RW = pmap_rw_bit(pmap);
4027                 pmap_resident_count_dec(pmap, 1);
4028                 pde = pmap_pde(pmap, pv->pv_va);
4029                 KASSERT((*pde & PG_PS) == 0, ("pmap_remove_all: found"
4030                     " a 2mpage in page %p's pv list", m));
4031                 pte = pmap_pde_to_pte(pde, pv->pv_va);
4032                 tpte = pte_load_clear(pte);
4033                 if (tpte & PG_W)
4034                         pmap->pm_stats.wired_count--;
4035                 if (tpte & PG_A)
4036                         vm_page_aflag_set(m, PGA_REFERENCED);
4037
4038                 /*
4039                  * Update the vm_page_t clean and reference bits.
4040                  */
4041                 if ((tpte & (PG_M | PG_RW)) == (PG_M | PG_RW))
4042                         vm_page_dirty(m);
4043                 pmap_unuse_pt(pmap, pv->pv_va, *pde, &free);
4044                 pmap_invalidate_page(pmap, pv->pv_va);
4045                 TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
4046                 m->md.pv_gen++;
4047                 free_pv_entry(pmap, pv);
4048                 PMAP_UNLOCK(pmap);
4049         }
4050         vm_page_aflag_clear(m, PGA_WRITEABLE);
4051         rw_wunlock(lock);
4052         pmap_delayed_invl_wait(m);
4053         pmap_free_zero_pages(&free);
4054 }
4055
4056 /*
4057  * pmap_protect_pde: do the things to protect a 2mpage in a process
4058  */
4059 static boolean_t
4060 pmap_protect_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t sva, vm_prot_t prot)
4061 {
4062         pd_entry_t newpde, oldpde;
4063         vm_offset_t eva, va;
4064         vm_page_t m;
4065         boolean_t anychanged;
4066         pt_entry_t PG_G, PG_M, PG_RW;
4067
4068         PG_G = pmap_global_bit(pmap);
4069         PG_M = pmap_modified_bit(pmap);
4070         PG_RW = pmap_rw_bit(pmap);
4071
4072         PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4073         KASSERT((sva & PDRMASK) == 0,
4074             ("pmap_protect_pde: sva is not 2mpage aligned"));
4075         anychanged = FALSE;
4076 retry:
4077         oldpde = newpde = *pde;
4078         if ((oldpde & (PG_MANAGED | PG_M | PG_RW)) ==
4079             (PG_MANAGED | PG_M | PG_RW)) {
4080                 eva = sva + NBPDR;
4081                 for (va = sva, m = PHYS_TO_VM_PAGE(oldpde & PG_PS_FRAME);
4082                     va < eva; va += PAGE_SIZE, m++)
4083                         vm_page_dirty(m);
4084         }
4085         if ((prot & VM_PROT_WRITE) == 0)
4086                 newpde &= ~(PG_RW | PG_M);
4087         if ((prot & VM_PROT_EXECUTE) == 0)
4088                 newpde |= pg_nx;
4089         if (newpde != oldpde) {
4090                 /*
4091                  * As an optimization to future operations on this PDE, clear
4092                  * PG_PROMOTED.  The impending invalidation will remove any
4093                  * lingering 4KB page mappings from the TLB.
4094                  */
4095                 if (!atomic_cmpset_long(pde, oldpde, newpde & ~PG_PROMOTED))
4096                         goto retry;
4097                 if ((oldpde & PG_G) != 0)
4098                         pmap_invalidate_pde_page(kernel_pmap, sva, oldpde);
4099                 else
4100                         anychanged = TRUE;
4101         }
4102         return (anychanged);
4103 }
4104
4105 /*
4106  *      Set the physical protection on the
4107  *      specified range of this map as requested.
4108  */
4109 void
4110 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
4111 {
4112         vm_offset_t va_next;
4113         pml4_entry_t *pml4e;
4114         pdp_entry_t *pdpe;
4115         pd_entry_t ptpaddr, *pde;
4116         pt_entry_t *pte, PG_G, PG_M, PG_RW, PG_V;
4117         boolean_t anychanged;
4118
4119         KASSERT((prot & ~VM_PROT_ALL) == 0, ("invalid prot %x", prot));
4120         if (prot == VM_PROT_NONE) {
4121                 pmap_remove(pmap, sva, eva);
4122                 return;
4123         }
4124
4125         if ((prot & (VM_PROT_WRITE|VM_PROT_EXECUTE)) ==
4126             (VM_PROT_WRITE|VM_PROT_EXECUTE))
4127                 return;
4128
4129         PG_G = pmap_global_bit(pmap);
4130         PG_M = pmap_modified_bit(pmap);
4131         PG_V = pmap_valid_bit(pmap);
4132         PG_RW = pmap_rw_bit(pmap);
4133         anychanged = FALSE;
4134
4135         /*
4136          * Although this function delays and batches the invalidation
4137          * of stale TLB entries, it does not need to call
4138          * pmap_delayed_invl_started() and
4139          * pmap_delayed_invl_finished(), because it does not
4140          * ordinarily destroy mappings.  Stale TLB entries from
4141          * protection-only changes need only be invalidated before the
4142          * pmap lock is released, because protection-only changes do
4143          * not destroy PV entries.  Even operations that iterate over
4144          * a physical page's PV list of mappings, like
4145          * pmap_remove_write(), acquire the pmap lock for each
4146          * mapping.  Consequently, for protection-only changes, the
4147          * pmap lock suffices to synchronize both page table and TLB
4148          * updates.
4149          *
4150          * This function only destroys a mapping if pmap_demote_pde()
4151          * fails.  In that case, stale TLB entries are immediately
4152          * invalidated.
4153          */
4154         
4155         PMAP_LOCK(pmap);
4156         for (; sva < eva; sva = va_next) {
4157
4158                 pml4e = pmap_pml4e(pmap, sva);
4159                 if ((*pml4e & PG_V) == 0) {
4160                         va_next = (sva + NBPML4) & ~PML4MASK;
4161                         if (va_next < sva)
4162                                 va_next = eva;
4163                         continue;
4164                 }
4165
4166                 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
4167                 if ((*pdpe & PG_V) == 0) {
4168                         va_next = (sva + NBPDP) & ~PDPMASK;
4169                         if (va_next < sva)
4170                                 va_next = eva;
4171                         continue;
4172                 }
4173
4174                 va_next = (sva + NBPDR) & ~PDRMASK;
4175                 if (va_next < sva)
4176                         va_next = eva;
4177
4178                 pde = pmap_pdpe_to_pde(pdpe, sva);
4179                 ptpaddr = *pde;
4180
4181                 /*
4182                  * Weed out invalid mappings.
4183                  */
4184                 if (ptpaddr == 0)
4185                         continue;
4186
4187                 /*
4188                  * Check for large page.
4189                  */
4190                 if ((ptpaddr & PG_PS) != 0) {
4191                         /*
4192                          * Are we protecting the entire large page?  If not,
4193                          * demote the mapping and fall through.
4194                          */
4195                         if (sva + NBPDR == va_next && eva >= va_next) {
4196                                 /*
4197                                  * The TLB entry for a PG_G mapping is
4198                                  * invalidated by pmap_protect_pde().
4199                                  */
4200                                 if (pmap_protect_pde(pmap, pde, sva, prot))
4201                                         anychanged = TRUE;
4202                                 continue;
4203                         } else if (!pmap_demote_pde(pmap, pde, sva)) {
4204                                 /*
4205                                  * The large page mapping was destroyed.
4206                                  */
4207                                 continue;
4208                         }
4209                 }
4210
4211                 if (va_next > eva)
4212                         va_next = eva;
4213
4214                 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
4215                     sva += PAGE_SIZE) {
4216                         pt_entry_t obits, pbits;
4217                         vm_page_t m;
4218
4219 retry:
4220                         obits = pbits = *pte;
4221                         if ((pbits & PG_V) == 0)
4222                                 continue;
4223
4224                         if ((prot & VM_PROT_WRITE) == 0) {
4225                                 if ((pbits & (PG_MANAGED | PG_M | PG_RW)) ==
4226                                     (PG_MANAGED | PG_M | PG_RW)) {
4227                                         m = PHYS_TO_VM_PAGE(pbits & PG_FRAME);
4228                                         vm_page_dirty(m);
4229                                 }
4230                                 pbits &= ~(PG_RW | PG_M);
4231                         }
4232                         if ((prot & VM_PROT_EXECUTE) == 0)
4233                                 pbits |= pg_nx;
4234
4235                         if (pbits != obits) {
4236                                 if (!atomic_cmpset_long(pte, obits, pbits))
4237                                         goto retry;
4238                                 if (obits & PG_G)
4239                                         pmap_invalidate_page(pmap, sva);
4240                                 else
4241                                         anychanged = TRUE;
4242                         }
4243                 }
4244         }
4245         if (anychanged)
4246                 pmap_invalidate_all(pmap);
4247         PMAP_UNLOCK(pmap);
4248 }
4249
4250 /*
4251  * Tries to promote the 512, contiguous 4KB page mappings that are within a
4252  * single page table page (PTP) to a single 2MB page mapping.  For promotion
4253  * to occur, two conditions must be met: (1) the 4KB page mappings must map
4254  * aligned, contiguous physical memory and (2) the 4KB page mappings must have
4255  * identical characteristics. 
4256  */
4257 static void
4258 pmap_promote_pde(pmap_t pmap, pd_entry_t *pde, vm_offset_t va,
4259     struct rwlock **lockp)
4260 {
4261         pd_entry_t newpde;
4262         pt_entry_t *firstpte, oldpte, pa, *pte;
4263         pt_entry_t PG_G, PG_A, PG_M, PG_RW, PG_V;
4264         vm_page_t mpte;
4265         int PG_PTE_CACHE;
4266
4267         PG_A = pmap_accessed_bit(pmap);
4268         PG_G = pmap_global_bit(pmap);
4269         PG_M = pmap_modified_bit(pmap);
4270         PG_V = pmap_valid_bit(pmap);
4271         PG_RW = pmap_rw_bit(pmap);
4272         PG_PTE_CACHE = pmap_cache_mask(pmap, 0);
4273
4274         PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4275
4276         /*
4277          * Examine the first PTE in the specified PTP.  Abort if this PTE is
4278          * either invalid, unused, or does not map the first 4KB physical page
4279          * within a 2MB page. 
4280          */
4281         firstpte = (pt_entry_t *)PHYS_TO_DMAP(*pde & PG_FRAME);
4282 setpde:
4283         newpde = *firstpte;
4284         if ((newpde & ((PG_FRAME & PDRMASK) | PG_A | PG_V)) != (PG_A | PG_V)) {
4285                 atomic_add_long(&pmap_pde_p_failures, 1);
4286                 CTR2(KTR_PMAP, "pmap_promote_pde: failure for va %#lx"
4287                     " in pmap %p", va, pmap);
4288                 return;
4289         }
4290         if ((newpde & (PG_M | PG_RW)) == PG_RW) {
4291                 /*
4292                  * When PG_M is already clear, PG_RW can be cleared without
4293                  * a TLB invalidation.
4294                  */
4295                 if (!atomic_cmpset_long(firstpte, newpde, newpde & ~PG_RW))
4296                         goto setpde;
4297                 newpde &= ~PG_RW;
4298         }
4299
4300         /*
4301          * Examine each of the other PTEs in the specified PTP.  Abort if this
4302          * PTE maps an unexpected 4KB physical page or does not have identical
4303          * characteristics to the first PTE.
4304          */
4305         pa = (newpde & (PG_PS_FRAME | PG_A | PG_V)) + NBPDR - PAGE_SIZE;
4306         for (pte = firstpte + NPTEPG - 1; pte > firstpte; pte--) {
4307 setpte:
4308                 oldpte = *pte;
4309                 if ((oldpte & (PG_FRAME | PG_A | PG_V)) != pa) {
4310                         atomic_add_long(&pmap_pde_p_failures, 1);
4311                         CTR2(KTR_PMAP, "pmap_promote_pde: failure for va %#lx"
4312                             " in pmap %p", va, pmap);
4313                         return;
4314                 }
4315                 if ((oldpte & (PG_M | PG_RW)) == PG_RW) {
4316                         /*
4317                          * When PG_M is already clear, PG_RW can be cleared
4318                          * without a TLB invalidation.
4319                          */
4320                         if (!atomic_cmpset_long(pte, oldpte, oldpte & ~PG_RW))
4321                                 goto setpte;
4322                         oldpte &= ~PG_RW;
4323                         CTR2(KTR_PMAP, "pmap_promote_pde: protect for va %#lx"
4324                             " in pmap %p", (oldpte & PG_FRAME & PDRMASK) |
4325                             (va & ~PDRMASK), pmap);
4326                 }
4327                 if ((oldpte & PG_PTE_PROMOTE) != (newpde & PG_PTE_PROMOTE)) {
4328                         atomic_add_long(&pmap_pde_p_failures, 1);
4329                         CTR2(KTR_PMAP, "pmap_promote_pde: failure for va %#lx"
4330                             " in pmap %p", va, pmap);
4331                         return;
4332                 }
4333                 pa -= PAGE_SIZE;
4334         }
4335
4336         /*
4337          * Save the page table page in its current state until the PDE
4338          * mapping the superpage is demoted by pmap_demote_pde() or
4339          * destroyed by pmap_remove_pde(). 
4340          */
4341         mpte = PHYS_TO_VM_PAGE(*pde & PG_FRAME);
4342         KASSERT(mpte >= vm_page_array &&
4343             mpte < &vm_page_array[vm_page_array_size],
4344             ("pmap_promote_pde: page table page is out of range"));
4345         KASSERT(mpte->pindex == pmap_pde_pindex(va),
4346             ("pmap_promote_pde: page table page's pindex is wrong"));
4347         if (pmap_insert_pt_page(pmap, mpte)) {
4348                 atomic_add_long(&pmap_pde_p_failures, 1);
4349                 CTR2(KTR_PMAP,
4350                     "pmap_promote_pde: failure for va %#lx in pmap %p", va,
4351                     pmap);
4352                 return;
4353         }
4354
4355         /*
4356          * Promote the pv entries.
4357          */
4358         if ((newpde & PG_MANAGED) != 0)
4359                 pmap_pv_promote_pde(pmap, va, newpde & PG_PS_FRAME, lockp);
4360
4361         /*
4362          * Propagate the PAT index to its proper position.
4363          */
4364         newpde = pmap_swap_pat(pmap, newpde);
4365
4366         /*
4367          * Map the superpage.
4368          */
4369         if (workaround_erratum383)
4370                 pmap_update_pde(pmap, va, pde, PG_PS | newpde);
4371         else
4372                 pde_store(pde, PG_PROMOTED | PG_PS | newpde);
4373
4374         atomic_add_long(&pmap_pde_promotions, 1);
4375         CTR2(KTR_PMAP, "pmap_promote_pde: success for va %#lx"
4376             " in pmap %p", va, pmap);
4377 }
4378
4379 /*
4380  *      Insert the given physical page (p) at
4381  *      the specified virtual address (v) in the
4382  *      target physical map with the protection requested.
4383  *
4384  *      If specified, the page will be wired down, meaning
4385  *      that the related pte can not be reclaimed.
4386  *
4387  *      NB:  This is the only routine which MAY NOT lazy-evaluate
4388  *      or lose information.  That is, this routine must actually
4389  *      insert this page into the given map NOW.
4390  *
4391  *      When destroying both a page table and PV entry, this function
4392  *      performs the TLB invalidation before releasing the PV list
4393  *      lock, so we do not need pmap_delayed_invl_page() calls here.
4394  */
4395 int
4396 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
4397     u_int flags, int8_t psind)
4398 {
4399         struct rwlock *lock;
4400         pd_entry_t *pde;
4401         pt_entry_t *pte, PG_G, PG_A, PG_M, PG_RW, PG_V;
4402         pt_entry_t newpte, origpte;
4403         pv_entry_t pv;
4404         vm_paddr_t opa, pa;
4405         vm_page_t mpte, om;
4406         int rv;
4407         boolean_t nosleep;
4408
4409         PG_A = pmap_accessed_bit(pmap);
4410         PG_G = pmap_global_bit(pmap);
4411         PG_M = pmap_modified_bit(pmap);
4412         PG_V = pmap_valid_bit(pmap);
4413         PG_RW = pmap_rw_bit(pmap);
4414
4415         va = trunc_page(va);
4416         KASSERT(va <= VM_MAX_KERNEL_ADDRESS, ("pmap_enter: toobig"));
4417         KASSERT(va < UPT_MIN_ADDRESS || va >= UPT_MAX_ADDRESS,
4418             ("pmap_enter: invalid to pmap_enter page table pages (va: 0x%lx)",
4419             va));
4420         KASSERT((m->oflags & VPO_UNMANAGED) != 0 || va < kmi.clean_sva ||
4421             va >= kmi.clean_eva,
4422             ("pmap_enter: managed mapping within the clean submap"));
4423         if ((m->oflags & VPO_UNMANAGED) == 0 && !vm_page_xbusied(m))
4424                 VM_OBJECT_ASSERT_LOCKED(m->object);
4425         KASSERT((flags & PMAP_ENTER_RESERVED) == 0,
4426             ("pmap_enter: flags %u has reserved bits set", flags));
4427         pa = VM_PAGE_TO_PHYS(m);
4428         newpte = (pt_entry_t)(pa | PG_A | PG_V);
4429         if ((flags & VM_PROT_WRITE) != 0)
4430                 newpte |= PG_M;
4431         if ((prot & VM_PROT_WRITE) != 0)
4432                 newpte |= PG_RW;
4433         KASSERT((newpte & (PG_M | PG_RW)) != PG_M,
4434             ("pmap_enter: flags includes VM_PROT_WRITE but prot doesn't"));
4435         if ((prot & VM_PROT_EXECUTE) == 0)
4436                 newpte |= pg_nx;
4437         if ((flags & PMAP_ENTER_WIRED) != 0)
4438                 newpte |= PG_W;
4439         if (va < VM_MAXUSER_ADDRESS)
4440                 newpte |= PG_U;
4441         if (pmap == kernel_pmap)
4442                 newpte |= PG_G;
4443         newpte |= pmap_cache_bits(pmap, m->md.pat_mode, psind > 0);
4444
4445         /*
4446          * Set modified bit gratuitously for writeable mappings if
4447          * the page is unmanaged. We do not want to take a fault
4448          * to do the dirty bit accounting for these mappings.
4449          */
4450         if ((m->oflags & VPO_UNMANAGED) != 0) {
4451                 if ((newpte & PG_RW) != 0)
4452                         newpte |= PG_M;
4453         } else
4454                 newpte |= PG_MANAGED;
4455
4456         lock = NULL;
4457         PMAP_LOCK(pmap);
4458         if (psind == 1) {
4459                 /* Assert the required virtual and physical alignment. */ 
4460                 KASSERT((va & PDRMASK) == 0, ("pmap_enter: va unaligned"));
4461                 KASSERT(m->psind > 0, ("pmap_enter: m->psind < psind"));
4462                 rv = pmap_enter_pde(pmap, va, newpte | PG_PS, flags, m, &lock);
4463                 goto out;
4464         }
4465         mpte = NULL;
4466
4467         /*
4468          * In the case that a page table page is not
4469          * resident, we are creating it here.
4470          */
4471 retry:
4472         pde = pmap_pde(pmap, va);
4473         if (pde != NULL && (*pde & PG_V) != 0 && ((*pde & PG_PS) == 0 ||
4474             pmap_demote_pde_locked(pmap, pde, va, &lock))) {
4475                 pte = pmap_pde_to_pte(pde, va);
4476                 if (va < VM_MAXUSER_ADDRESS && mpte == NULL) {
4477                         mpte = PHYS_TO_VM_PAGE(*pde & PG_FRAME);
4478                         mpte->wire_count++;
4479                 }
4480         } else if (va < VM_MAXUSER_ADDRESS) {
4481                 /*
4482                  * Here if the pte page isn't mapped, or if it has been
4483                  * deallocated.
4484                  */
4485                 nosleep = (flags & PMAP_ENTER_NOSLEEP) != 0;
4486                 mpte = _pmap_allocpte(pmap, pmap_pde_pindex(va),
4487                     nosleep ? NULL : &lock);
4488                 if (mpte == NULL && nosleep) {
4489                         rv = KERN_RESOURCE_SHORTAGE;
4490                         goto out;
4491                 }
4492                 goto retry;
4493         } else
4494                 panic("pmap_enter: invalid page directory va=%#lx", va);
4495
4496         origpte = *pte;
4497
4498         /*
4499          * Is the specified virtual address already mapped?
4500          */
4501         if ((origpte & PG_V) != 0) {
4502                 /*
4503                  * Wiring change, just update stats. We don't worry about
4504                  * wiring PT pages as they remain resident as long as there
4505                  * are valid mappings in them. Hence, if a user page is wired,
4506                  * the PT page will be also.
4507                  */
4508                 if ((newpte & PG_W) != 0 && (origpte & PG_W) == 0)
4509                         pmap->pm_stats.wired_count++;
4510                 else if ((newpte & PG_W) == 0 && (origpte & PG_W) != 0)
4511                         pmap->pm_stats.wired_count--;
4512
4513                 /*
4514                  * Remove the extra PT page reference.
4515                  */
4516                 if (mpte != NULL) {
4517                         mpte->wire_count--;
4518                         KASSERT(mpte->wire_count > 0,
4519                             ("pmap_enter: missing reference to page table page,"
4520                              " va: 0x%lx", va));
4521                 }
4522
4523                 /*
4524                  * Has the physical page changed?
4525                  */
4526                 opa = origpte & PG_FRAME;
4527                 if (opa == pa) {
4528                         /*
4529                          * No, might be a protection or wiring change.
4530                          */
4531                         if ((origpte & PG_MANAGED) != 0 &&
4532                             (newpte & PG_RW) != 0)
4533                                 vm_page_aflag_set(m, PGA_WRITEABLE);
4534                         if (((origpte ^ newpte) & ~(PG_M | PG_A)) == 0)
4535                                 goto unchanged;
4536                         goto validate;
4537                 }
4538         } else {
4539                 /*
4540                  * Increment the counters.
4541                  */
4542                 if ((newpte & PG_W) != 0)
4543                         pmap->pm_stats.wired_count++;
4544                 pmap_resident_count_inc(pmap, 1);
4545         }
4546
4547         /*
4548          * Enter on the PV list if part of our managed memory.
4549          */
4550         if ((newpte & PG_MANAGED) != 0) {
4551                 pv = get_pv_entry(pmap, &lock);
4552                 pv->pv_va = va;
4553                 CHANGE_PV_LIST_LOCK_TO_PHYS(&lock, pa);
4554                 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
4555                 m->md.pv_gen++;
4556                 if ((newpte & PG_RW) != 0)
4557                         vm_page_aflag_set(m, PGA_WRITEABLE);
4558         }
4559
4560         /*
4561          * Update the PTE.
4562          */
4563         if ((origpte & PG_V) != 0) {
4564 validate:
4565                 origpte = pte_load_store(pte, newpte);
4566                 opa = origpte & PG_FRAME;
4567                 if (opa != pa) {
4568                         if ((origpte & PG_MANAGED) != 0) {
4569                                 om = PHYS_TO_VM_PAGE(opa);
4570                                 if ((origpte & (PG_M | PG_RW)) == (PG_M |
4571                                     PG_RW))
4572                                         vm_page_dirty(om);
4573                                 if ((origpte & PG_A) != 0)
4574                                         vm_page_aflag_set(om, PGA_REFERENCED);
4575                                 CHANGE_PV_LIST_LOCK_TO_PHYS(&lock, opa);
4576                                 pmap_pvh_free(&om->md, pmap, va);
4577                                 if ((om->aflags & PGA_WRITEABLE) != 0 &&
4578                                     TAILQ_EMPTY(&om->md.pv_list) &&
4579                                     ((om->flags & PG_FICTITIOUS) != 0 ||
4580                                     TAILQ_EMPTY(&pa_to_pvh(opa)->pv_list)))
4581                                         vm_page_aflag_clear(om, PGA_WRITEABLE);
4582                         }
4583                 } else if ((newpte & PG_M) == 0 && (origpte & (PG_M |
4584                     PG_RW)) == (PG_M | PG_RW)) {
4585                         if ((origpte & PG_MANAGED) != 0)
4586                                 vm_page_dirty(m);
4587
4588                         /*
4589                          * Although the PTE may still have PG_RW set, TLB
4590                          * invalidation may nonetheless be required because
4591                          * the PTE no longer has PG_M set.
4592                          */
4593                 } else if ((origpte & PG_NX) != 0 || (newpte & PG_NX) == 0) {
4594                         /*
4595                          * This PTE change does not require TLB invalidation.
4596                          */
4597                         goto unchanged;
4598                 }
4599                 if ((origpte & PG_A) != 0)
4600                         pmap_invalidate_page(pmap, va);
4601         } else
4602                 pte_store(pte, newpte);
4603
4604 unchanged:
4605
4606         /*
4607          * If both the page table page and the reservation are fully
4608          * populated, then attempt promotion.
4609          */
4610         if ((mpte == NULL || mpte->wire_count == NPTEPG) &&
4611             pmap_ps_enabled(pmap) &&
4612             (m->flags & PG_FICTITIOUS) == 0 &&
4613             vm_reserv_level_iffullpop(m) == 0)
4614                 pmap_promote_pde(pmap, pde, va, &lock);
4615
4616         rv = KERN_SUCCESS;
4617 out:
4618         if (lock != NULL)
4619                 rw_wunlock(lock);
4620         PMAP_UNLOCK(pmap);
4621         return (rv);
4622 }
4623
4624 /*
4625  * Tries to create a read- and/or execute-only 2MB page mapping.  Returns true
4626  * if successful.  Returns false if (1) a page table page cannot be allocated
4627  * without sleeping, (2) a mapping already exists at the specified virtual
4628  * address, or (3) a PV entry cannot be allocated without reclaiming another
4629  * PV entry.
4630  */
4631 static bool
4632 pmap_enter_2mpage(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
4633     struct rwlock **lockp)
4634 {
4635         pd_entry_t newpde;
4636         pt_entry_t PG_V;
4637
4638         PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4639         PG_V = pmap_valid_bit(pmap);
4640         newpde = VM_PAGE_TO_PHYS(m) | pmap_cache_bits(pmap, m->md.pat_mode, 1) |
4641             PG_PS | PG_V;
4642         if ((m->oflags & VPO_UNMANAGED) == 0)
4643                 newpde |= PG_MANAGED;
4644         if ((prot & VM_PROT_EXECUTE) == 0)
4645                 newpde |= pg_nx;
4646         if (va < VM_MAXUSER_ADDRESS)
4647                 newpde |= PG_U;
4648         return (pmap_enter_pde(pmap, va, newpde, PMAP_ENTER_NOSLEEP |
4649             PMAP_ENTER_NOREPLACE | PMAP_ENTER_NORECLAIM, NULL, lockp) ==
4650             KERN_SUCCESS);
4651 }
4652
4653 /*
4654  * Tries to create the specified 2MB page mapping.  Returns KERN_SUCCESS if
4655  * the mapping was created, and either KERN_FAILURE or KERN_RESOURCE_SHORTAGE
4656  * otherwise.  Returns KERN_FAILURE if PMAP_ENTER_NOREPLACE was specified and
4657  * a mapping already exists at the specified virtual address.  Returns
4658  * KERN_RESOURCE_SHORTAGE if PMAP_ENTER_NOSLEEP was specified and a page table
4659  * page allocation failed.  Returns KERN_RESOURCE_SHORTAGE if
4660  * PMAP_ENTER_NORECLAIM was specified and a PV entry allocation failed.
4661  *
4662  * The parameter "m" is only used when creating a managed, writeable mapping.
4663  */
4664 static int
4665 pmap_enter_pde(pmap_t pmap, vm_offset_t va, pd_entry_t newpde, u_int flags,
4666     vm_page_t m, struct rwlock **lockp)
4667 {
4668         struct spglist free;
4669         pd_entry_t oldpde, *pde;
4670         pt_entry_t PG_G, PG_RW, PG_V;
4671         vm_page_t mt, pdpg;
4672
4673         PG_G = pmap_global_bit(pmap);
4674         PG_RW = pmap_rw_bit(pmap);
4675         KASSERT((newpde & (pmap_modified_bit(pmap) | PG_RW)) != PG_RW,
4676             ("pmap_enter_pde: newpde is missing PG_M"));
4677         PG_V = pmap_valid_bit(pmap);
4678         PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4679
4680         if ((pdpg = pmap_allocpde(pmap, va, (flags & PMAP_ENTER_NOSLEEP) != 0 ?
4681             NULL : lockp)) == NULL) {
4682                 CTR2(KTR_PMAP, "pmap_enter_pde: failure for va %#lx"
4683                     " in pmap %p", va, pmap);
4684                 return (KERN_RESOURCE_SHORTAGE);
4685         }
4686         pde = (pd_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(pdpg));
4687         pde = &pde[pmap_pde_index(va)];
4688         oldpde = *pde;
4689         if ((oldpde & PG_V) != 0) {
4690                 KASSERT(pdpg->wire_count > 1,
4691                     ("pmap_enter_pde: pdpg's wire count is too low"));
4692                 if ((flags & PMAP_ENTER_NOREPLACE) != 0) {
4693                         pdpg->wire_count--;
4694                         CTR2(KTR_PMAP, "pmap_enter_pde: failure for va %#lx"
4695                             " in pmap %p", va, pmap);
4696                         return (KERN_FAILURE);
4697                 }
4698                 /* Break the existing mapping(s). */
4699                 SLIST_INIT(&free);
4700                 if ((oldpde & PG_PS) != 0) {
4701                         /*
4702                          * The reference to the PD page that was acquired by
4703                          * pmap_allocpde() ensures that it won't be freed.
4704                          * However, if the PDE resulted from a promotion, then
4705                          * a reserved PT page could be freed.
4706                          */
4707                         (void)pmap_remove_pde(pmap, pde, va, &free, lockp);
4708                         if ((oldpde & PG_G) == 0)
4709                                 pmap_invalidate_pde_page(pmap, va, oldpde);
4710                 } else {
4711                         pmap_delayed_invl_started();
4712                         if (pmap_remove_ptes(pmap, va, va + NBPDR, pde, &free,
4713                             lockp))
4714                                pmap_invalidate_all(pmap);
4715                         pmap_delayed_invl_finished();
4716                 }
4717                 pmap_free_zero_pages(&free);
4718                 if (va >= VM_MAXUSER_ADDRESS) {
4719                         mt = PHYS_TO_VM_PAGE(*pde & PG_FRAME);
4720                         if (pmap_insert_pt_page(pmap, mt)) {
4721                                 /*
4722                                  * XXX Currently, this can't happen because
4723                                  * we do not perform pmap_enter(psind == 1)
4724                                  * on the kernel pmap.
4725                                  */
4726                                 panic("pmap_enter_pde: trie insert failed");
4727                         }
4728                 } else
4729                         KASSERT(*pde == 0, ("pmap_enter_pde: non-zero pde %p",
4730                             pde));
4731         }
4732         if ((newpde & PG_MANAGED) != 0) {
4733                 /*
4734                  * Abort this mapping if its PV entry could not be created.
4735                  */
4736                 if (!pmap_pv_insert_pde(pmap, va, newpde, flags, lockp)) {
4737                         SLIST_INIT(&free);
4738                         if (pmap_unwire_ptp(pmap, va, pdpg, &free)) {
4739                                 /*
4740                                  * Although "va" is not mapped, paging-
4741                                  * structure caches could nonetheless have
4742                                  * entries that refer to the freed page table
4743                                  * pages.  Invalidate those entries.
4744                                  */
4745                                 pmap_invalidate_page(pmap, va);
4746                                 pmap_free_zero_pages(&free);
4747                         }
4748                         CTR2(KTR_PMAP, "pmap_enter_pde: failure for va %#lx"
4749                             " in pmap %p", va, pmap);
4750                         return (KERN_RESOURCE_SHORTAGE);
4751                 }
4752                 if ((newpde & PG_RW) != 0) {
4753                         for (mt = m; mt < &m[NBPDR / PAGE_SIZE]; mt++)
4754                                 vm_page_aflag_set(mt, PGA_WRITEABLE);
4755                 }
4756         }
4757
4758         /*
4759          * Increment counters.
4760          */
4761         if ((newpde & PG_W) != 0)
4762                 pmap->pm_stats.wired_count += NBPDR / PAGE_SIZE;
4763         pmap_resident_count_inc(pmap, NBPDR / PAGE_SIZE);
4764
4765         /*
4766          * Map the superpage.  (This is not a promoted mapping; there will not
4767          * be any lingering 4KB page mappings in the TLB.)
4768          */
4769         pde_store(pde, newpde);
4770
4771         atomic_add_long(&pmap_pde_mappings, 1);
4772         CTR2(KTR_PMAP, "pmap_enter_pde: success for va %#lx"
4773             " in pmap %p", va, pmap);
4774         return (KERN_SUCCESS);
4775 }
4776
4777 /*
4778  * Maps a sequence of resident pages belonging to the same object.
4779  * The sequence begins with the given page m_start.  This page is
4780  * mapped at the given virtual address start.  Each subsequent page is
4781  * mapped at a virtual address that is offset from start by the same
4782  * amount as the page is offset from m_start within the object.  The
4783  * last page in the sequence is the page with the largest offset from
4784  * m_start that can be mapped at a virtual address less than the given
4785  * virtual address end.  Not every virtual page between start and end
4786  * is mapped; only those for which a resident page exists with the
4787  * corresponding offset from m_start are mapped.
4788  */
4789 void
4790 pmap_enter_object(pmap_t pmap, vm_offset_t start, vm_offset_t end,
4791     vm_page_t m_start, vm_prot_t prot)
4792 {
4793         struct rwlock *lock;
4794         vm_offset_t va;
4795         vm_page_t m, mpte;
4796         vm_pindex_t diff, psize;
4797
4798         VM_OBJECT_ASSERT_LOCKED(m_start->object);
4799
4800         psize = atop(end - start);
4801         mpte = NULL;
4802         m = m_start;
4803         lock = NULL;
4804         PMAP_LOCK(pmap);
4805         while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
4806                 va = start + ptoa(diff);
4807                 if ((va & PDRMASK) == 0 && va + NBPDR <= end &&
4808                     m->psind == 1 && pmap_ps_enabled(pmap) &&
4809                     pmap_enter_2mpage(pmap, va, m, prot, &lock))
4810                         m = &m[NBPDR / PAGE_SIZE - 1];
4811                 else
4812                         mpte = pmap_enter_quick_locked(pmap, va, m, prot,
4813                             mpte, &lock);
4814                 m = TAILQ_NEXT(m, listq);
4815         }
4816         if (lock != NULL)
4817                 rw_wunlock(lock);
4818         PMAP_UNLOCK(pmap);
4819 }
4820
4821 /*
4822  * this code makes some *MAJOR* assumptions:
4823  * 1. Current pmap & pmap exists.
4824  * 2. Not wired.
4825  * 3. Read access.
4826  * 4. No page table pages.
4827  * but is *MUCH* faster than pmap_enter...
4828  */
4829
4830 void
4831 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
4832 {
4833         struct rwlock *lock;
4834
4835         lock = NULL;
4836         PMAP_LOCK(pmap);
4837         (void)pmap_enter_quick_locked(pmap, va, m, prot, NULL, &lock);
4838         if (lock != NULL)
4839                 rw_wunlock(lock);
4840         PMAP_UNLOCK(pmap);
4841 }
4842
4843 static vm_page_t
4844 pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va, vm_page_t m,
4845     vm_prot_t prot, vm_page_t mpte, struct rwlock **lockp)
4846 {
4847         struct spglist free;
4848         pt_entry_t *pte, PG_V;
4849         vm_paddr_t pa;
4850
4851         KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva ||
4852             (m->oflags & VPO_UNMANAGED) != 0,
4853             ("pmap_enter_quick_locked: managed mapping within the clean submap"));
4854         PG_V = pmap_valid_bit(pmap);
4855         PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4856
4857         /*
4858          * In the case that a page table page is not
4859          * resident, we are creating it here.
4860          */
4861         if (va < VM_MAXUSER_ADDRESS) {
4862                 vm_pindex_t ptepindex;
4863                 pd_entry_t *ptepa;
4864
4865                 /*
4866                  * Calculate pagetable page index
4867                  */
4868                 ptepindex = pmap_pde_pindex(va);
4869                 if (mpte && (mpte->pindex == ptepindex)) {
4870                         mpte->wire_count++;
4871                 } else {
4872                         /*
4873                          * Get the page directory entry
4874                          */
4875                         ptepa = pmap_pde(pmap, va);
4876
4877                         /*
4878                          * If the page table page is mapped, we just increment
4879                          * the hold count, and activate it.  Otherwise, we
4880                          * attempt to allocate a page table page.  If this
4881                          * attempt fails, we don't retry.  Instead, we give up.
4882                          */
4883                         if (ptepa && (*ptepa & PG_V) != 0) {
4884                                 if (*ptepa & PG_PS)
4885                                         return (NULL);
4886                                 mpte = PHYS_TO_VM_PAGE(*ptepa & PG_FRAME);
4887                                 mpte->wire_count++;
4888                         } else {
4889                                 /*
4890                                  * Pass NULL instead of the PV list lock
4891                                  * pointer, because we don't intend to sleep.
4892                                  */
4893                                 mpte = _pmap_allocpte(pmap, ptepindex, NULL);
4894                                 if (mpte == NULL)
4895                                         return (mpte);
4896                         }
4897                 }
4898                 pte = (pt_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(mpte));
4899                 pte = &pte[pmap_pte_index(va)];
4900         } else {
4901                 mpte = NULL;
4902                 pte = vtopte(va);
4903         }
4904         if (*pte) {
4905                 if (mpte != NULL) {
4906                         mpte->wire_count--;
4907                         mpte = NULL;
4908                 }
4909                 return (mpte);
4910         }
4911
4912         /*
4913          * Enter on the PV list if part of our managed memory.
4914          */
4915         if ((m->oflags & VPO_UNMANAGED) == 0 &&
4916             !pmap_try_insert_pv_entry(pmap, va, m, lockp)) {
4917                 if (mpte != NULL) {
4918                         SLIST_INIT(&free);
4919                         if (pmap_unwire_ptp(pmap, va, mpte, &free)) {
4920                                 /*
4921                                  * Although "va" is not mapped, paging-
4922                                  * structure caches could nonetheless have
4923                                  * entries that refer to the freed page table
4924                                  * pages.  Invalidate those entries.
4925                                  */
4926                                 pmap_invalidate_page(pmap, va);
4927                                 pmap_free_zero_pages(&free);
4928                         }
4929                         mpte = NULL;
4930                 }
4931                 return (mpte);
4932         }
4933
4934         /*
4935          * Increment counters
4936          */
4937         pmap_resident_count_inc(pmap, 1);
4938
4939         pa = VM_PAGE_TO_PHYS(m) | pmap_cache_bits(pmap, m->md.pat_mode, 0);
4940         if ((prot & VM_PROT_EXECUTE) == 0)
4941                 pa |= pg_nx;
4942
4943         /*
4944          * Now validate mapping with RO protection
4945          */
4946         if ((m->oflags & VPO_UNMANAGED) != 0)
4947                 pte_store(pte, pa | PG_V | PG_U);
4948         else
4949                 pte_store(pte, pa | PG_V | PG_U | PG_MANAGED);
4950         return (mpte);
4951 }
4952
4953 /*
4954  * Make a temporary mapping for a physical address.  This is only intended
4955  * to be used for panic dumps.
4956  */
4957 void *
4958 pmap_kenter_temporary(vm_paddr_t pa, int i)
4959 {
4960         vm_offset_t va;
4961
4962         va = (vm_offset_t)crashdumpmap + (i * PAGE_SIZE);
4963         pmap_kenter(va, pa);
4964         invlpg(va);
4965         return ((void *)crashdumpmap);
4966 }
4967
4968 /*
4969  * This code maps large physical mmap regions into the
4970  * processor address space.  Note that some shortcuts
4971  * are taken, but the code works.
4972  */
4973 void
4974 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_object_t object,
4975     vm_pindex_t pindex, vm_size_t size)
4976 {
4977         pd_entry_t *pde;
4978         pt_entry_t PG_A, PG_M, PG_RW, PG_V;
4979         vm_paddr_t pa, ptepa;
4980         vm_page_t p, pdpg;
4981         int pat_mode;
4982
4983         PG_A = pmap_accessed_bit(pmap);
4984         PG_M = pmap_modified_bit(pmap);
4985         PG_V = pmap_valid_bit(pmap);
4986         PG_RW = pmap_rw_bit(pmap);
4987
4988         VM_OBJECT_ASSERT_WLOCKED(object);
4989         KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
4990             ("pmap_object_init_pt: non-device object"));
4991         if ((addr & (NBPDR - 1)) == 0 && (size & (NBPDR - 1)) == 0) {
4992                 if (!pmap_ps_enabled(pmap))
4993                         return;
4994                 if (!vm_object_populate(object, pindex, pindex + atop(size)))
4995                         return;
4996                 p = vm_page_lookup(object, pindex);
4997                 KASSERT(p->valid == VM_PAGE_BITS_ALL,
4998                     ("pmap_object_init_pt: invalid page %p", p));
4999                 pat_mode = p->md.pat_mode;
5000
5001                 /*
5002                  * Abort the mapping if the first page is not physically
5003                  * aligned to a 2MB page boundary.
5004                  */
5005                 ptepa = VM_PAGE_TO_PHYS(p);
5006                 if (ptepa & (NBPDR - 1))
5007                         return;
5008
5009                 /*
5010                  * Skip the first page.  Abort the mapping if the rest of
5011                  * the pages are not physically contiguous or have differing
5012                  * memory attributes.
5013                  */
5014                 p = TAILQ_NEXT(p, listq);
5015                 for (pa = ptepa + PAGE_SIZE; pa < ptepa + size;
5016                     pa += PAGE_SIZE) {
5017                         KASSERT(p->valid == VM_PAGE_BITS_ALL,
5018                             ("pmap_object_init_pt: invalid page %p", p));
5019                         if (pa != VM_PAGE_TO_PHYS(p) ||
5020                             pat_mode != p->md.pat_mode)
5021                                 return;
5022                         p = TAILQ_NEXT(p, listq);
5023                 }
5024
5025                 /*
5026                  * Map using 2MB pages.  Since "ptepa" is 2M aligned and
5027                  * "size" is a multiple of 2M, adding the PAT setting to "pa"
5028                  * will not affect the termination of this loop.
5029                  */ 
5030                 PMAP_LOCK(pmap);
5031                 for (pa = ptepa | pmap_cache_bits(pmap, pat_mode, 1);
5032                     pa < ptepa + size; pa += NBPDR) {
5033                         pdpg = pmap_allocpde(pmap, addr, NULL);
5034                         if (pdpg == NULL) {
5035                                 /*
5036                                  * The creation of mappings below is only an
5037                                  * optimization.  If a page directory page
5038                                  * cannot be allocated without blocking,
5039                                  * continue on to the next mapping rather than
5040                                  * blocking.
5041                                  */
5042                                 addr += NBPDR;
5043                                 continue;
5044                         }
5045                         pde = (pd_entry_t *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(pdpg));
5046                         pde = &pde[pmap_pde_index(addr)];
5047                         if ((*pde & PG_V) == 0) {
5048                                 pde_store(pde, pa | PG_PS | PG_M | PG_A |
5049                                     PG_U | PG_RW | PG_V);
5050                                 pmap_resident_count_inc(pmap, NBPDR / PAGE_SIZE);
5051                                 atomic_add_long(&pmap_pde_mappings, 1);
5052                         } else {
5053                                 /* Continue on if the PDE is already valid. */
5054                                 pdpg->wire_count--;
5055                                 KASSERT(pdpg->wire_count > 0,
5056                                     ("pmap_object_init_pt: missing reference "
5057                                     "to page directory page, va: 0x%lx", addr));
5058                         }
5059                         addr += NBPDR;
5060                 }
5061                 PMAP_UNLOCK(pmap);
5062         }
5063 }
5064
5065 /*
5066  *      Clear the wired attribute from the mappings for the specified range of
5067  *      addresses in the given pmap.  Every valid mapping within that range
5068  *      must have the wired attribute set.  In contrast, invalid mappings
5069  *      cannot have the wired attribute set, so they are ignored.
5070  *
5071  *      The wired attribute of the page table entry is not a hardware
5072  *      feature, so there is no need to invalidate any TLB entries.
5073  *      Since pmap_demote_pde() for the wired entry must never fail,
5074  *      pmap_delayed_invl_started()/finished() calls around the
5075  *      function are not needed.
5076  */
5077 void
5078 pmap_unwire(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
5079 {
5080         vm_offset_t va_next;
5081         pml4_entry_t *pml4e;
5082         pdp_entry_t *pdpe;
5083         pd_entry_t *pde;
5084         pt_entry_t *pte, PG_V;
5085
5086         PG_V = pmap_valid_bit(pmap);
5087         PMAP_LOCK(pmap);
5088         for (; sva < eva; sva = va_next) {
5089                 pml4e = pmap_pml4e(pmap, sva);
5090                 if ((*pml4e & PG_V) == 0) {
5091                         va_next = (sva + NBPML4) & ~PML4MASK;
5092                         if (va_next < sva)
5093                                 va_next = eva;
5094                         continue;
5095                 }
5096                 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
5097                 if ((*pdpe & PG_V) == 0) {
5098                         va_next = (sva + NBPDP) & ~PDPMASK;
5099                         if (va_next < sva)
5100                                 va_next = eva;
5101                         continue;
5102                 }
5103                 va_next = (sva + NBPDR) & ~PDRMASK;
5104                 if (va_next < sva)
5105                         va_next = eva;
5106                 pde = pmap_pdpe_to_pde(pdpe, sva);
5107                 if ((*pde & PG_V) == 0)
5108                         continue;
5109                 if ((*pde & PG_PS) != 0) {
5110                         if ((*pde & PG_W) == 0)
5111                                 panic("pmap_unwire: pde %#jx is missing PG_W",
5112                                     (uintmax_t)*pde);
5113
5114                         /*
5115                          * Are we unwiring the entire large page?  If not,
5116                          * demote the mapping and fall through.
5117                          */
5118                         if (sva + NBPDR == va_next && eva >= va_next) {
5119                                 atomic_clear_long(pde, PG_W);
5120                                 pmap->pm_stats.wired_count -= NBPDR /
5121                                     PAGE_SIZE;
5122                                 continue;
5123                         } else if (!pmap_demote_pde(pmap, pde, sva))
5124                                 panic("pmap_unwire: demotion failed");
5125                 }
5126                 if (va_next > eva)
5127                         va_next = eva;
5128                 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
5129                     sva += PAGE_SIZE) {
5130                         if ((*pte & PG_V) == 0)
5131                                 continue;
5132                         if ((*pte & PG_W) == 0)
5133                                 panic("pmap_unwire: pte %#jx is missing PG_W",
5134                                     (uintmax_t)*pte);
5135
5136                         /*
5137                          * PG_W must be cleared atomically.  Although the pmap
5138                          * lock synchronizes access to PG_W, another processor
5139                          * could be setting PG_M and/or PG_A concurrently.
5140                          */
5141                         atomic_clear_long(pte, PG_W);
5142                         pmap->pm_stats.wired_count--;
5143                 }
5144         }
5145         PMAP_UNLOCK(pmap);
5146 }
5147
5148 /*
5149  *      Copy the range specified by src_addr/len
5150  *      from the source map to the range dst_addr/len
5151  *      in the destination map.
5152  *
5153  *      This routine is only advisory and need not do anything.
5154  */
5155
5156 void
5157 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, vm_size_t len,
5158     vm_offset_t src_addr)
5159 {
5160         struct rwlock *lock;
5161         struct spglist free;
5162         vm_offset_t addr;
5163         vm_offset_t end_addr = src_addr + len;
5164         vm_offset_t va_next;
5165         vm_page_t dst_pdpg, dstmpte, srcmpte;
5166         pt_entry_t PG_A, PG_M, PG_V;
5167
5168         if (dst_addr != src_addr)
5169                 return;
5170
5171         if (dst_pmap->pm_type != src_pmap->pm_type)
5172                 return;
5173
5174         /*
5175          * EPT page table entries that require emulation of A/D bits are
5176          * sensitive to clearing the PG_A bit (aka EPT_PG_READ). Although
5177          * we clear PG_M (aka EPT_PG_WRITE) concomitantly, the PG_U bit
5178          * (aka EPT_PG_EXECUTE) could still be set. Since some EPT
5179          * implementations flag an EPT misconfiguration for exec-only
5180          * mappings we skip this function entirely for emulated pmaps.
5181          */
5182         if (pmap_emulate_ad_bits(dst_pmap))
5183                 return;
5184
5185         lock = NULL;
5186         if (dst_pmap < src_pmap) {
5187                 PMAP_LOCK(dst_pmap);
5188                 PMAP_LOCK(src_pmap);
5189         } else {
5190                 PMAP_LOCK(src_pmap);
5191                 PMAP_LOCK(dst_pmap);
5192         }
5193
5194         PG_A = pmap_accessed_bit(dst_pmap);
5195         PG_M = pmap_modified_bit(dst_pmap);
5196         PG_V = pmap_valid_bit(dst_pmap);
5197
5198         for (addr = src_addr; addr < end_addr; addr = va_next) {
5199                 pt_entry_t *src_pte, *dst_pte;
5200                 pml4_entry_t *pml4e;
5201                 pdp_entry_t *pdpe;
5202                 pd_entry_t srcptepaddr, *pde;
5203
5204                 KASSERT(addr < UPT_MIN_ADDRESS,
5205                     ("pmap_copy: invalid to pmap_copy page tables"));
5206
5207                 pml4e = pmap_pml4e(src_pmap, addr);
5208                 if ((*pml4e & PG_V) == 0) {
5209                         va_next = (addr + NBPML4) & ~PML4MASK;
5210                         if (va_next < addr)
5211                                 va_next = end_addr;
5212                         continue;
5213                 }
5214
5215                 pdpe = pmap_pml4e_to_pdpe(pml4e, addr);
5216                 if ((*pdpe & PG_V) == 0) {
5217                         va_next = (addr + NBPDP) & ~PDPMASK;
5218                         if (va_next < addr)
5219                                 va_next = end_addr;
5220                         continue;
5221                 }
5222
5223                 va_next = (addr + NBPDR) & ~PDRMASK;
5224                 if (va_next < addr)
5225                         va_next = end_addr;
5226
5227                 pde = pmap_pdpe_to_pde(pdpe, addr);
5228                 srcptepaddr = *pde;
5229                 if (srcptepaddr == 0)
5230                         continue;
5231                         
5232                 if (srcptepaddr & PG_PS) {
5233                         if ((addr & PDRMASK) != 0 || addr + NBPDR > end_addr)
5234                                 continue;
5235                         dst_pdpg = pmap_allocpde(dst_pmap, addr, NULL);
5236                         if (dst_pdpg == NULL)
5237                                 break;
5238                         pde = (pd_entry_t *)
5239                             PHYS_TO_DMAP(VM_PAGE_TO_PHYS(dst_pdpg));
5240                         pde = &pde[pmap_pde_index(addr)];
5241                         if (*pde == 0 && ((srcptepaddr & PG_MANAGED) == 0 ||
5242                             pmap_pv_insert_pde(dst_pmap, addr, srcptepaddr,
5243                             PMAP_ENTER_NORECLAIM, &lock))) {
5244                                 *pde = srcptepaddr & ~PG_W;
5245                                 pmap_resident_count_inc(dst_pmap, NBPDR / PAGE_SIZE);
5246                                 atomic_add_long(&pmap_pde_mappings, 1);
5247                         } else
5248                                 dst_pdpg->wire_count--;
5249                         continue;
5250                 }
5251
5252                 srcptepaddr &= PG_FRAME;
5253                 srcmpte = PHYS_TO_VM_PAGE(srcptepaddr);
5254                 KASSERT(srcmpte->wire_count > 0,
5255                     ("pmap_copy: source page table page is unused"));
5256
5257                 if (va_next > end_addr)
5258                         va_next = end_addr;
5259
5260                 src_pte = (pt_entry_t *)PHYS_TO_DMAP(srcptepaddr);
5261                 src_pte = &src_pte[pmap_pte_index(addr)];
5262                 dstmpte = NULL;
5263                 while (addr < va_next) {
5264                         pt_entry_t ptetemp;
5265                         ptetemp = *src_pte;
5266                         /*
5267                          * we only virtual copy managed pages
5268                          */
5269                         if ((ptetemp & PG_MANAGED) != 0) {
5270                                 if (dstmpte != NULL &&
5271                                     dstmpte->pindex == pmap_pde_pindex(addr))
5272                                         dstmpte->wire_count++;
5273                                 else if ((dstmpte = pmap_allocpte(dst_pmap,
5274                                     addr, NULL)) == NULL)
5275                                         goto out;
5276                                 dst_pte = (pt_entry_t *)
5277                                     PHYS_TO_DMAP(VM_PAGE_TO_PHYS(dstmpte));
5278                                 dst_pte = &dst_pte[pmap_pte_index(addr)];
5279                                 if (*dst_pte == 0 &&
5280                                     pmap_try_insert_pv_entry(dst_pmap, addr,
5281                                     PHYS_TO_VM_PAGE(ptetemp & PG_FRAME),
5282                                     &lock)) {
5283                                         /*
5284                                          * Clear the wired, modified, and
5285                                          * accessed (referenced) bits
5286                                          * during the copy.
5287                                          */
5288                                         *dst_pte = ptetemp & ~(PG_W | PG_M |
5289                                             PG_A);
5290                                         pmap_resident_count_inc(dst_pmap, 1);
5291                                 } else {
5292                                         SLIST_INIT(&free);
5293                                         if (pmap_unwire_ptp(dst_pmap, addr,
5294                                             dstmpte, &free)) {
5295                                                 /*
5296                                                  * Although "addr" is not
5297                                                  * mapped, paging-structure
5298                                                  * caches could nonetheless
5299                                                  * have entries that refer to
5300                                                  * the freed page table pages.
5301                                                  * Invalidate those entries.
5302                                                  */
5303                                                 pmap_invalidate_page(dst_pmap,
5304                                                     addr);
5305                                                 pmap_free_zero_pages(&free);
5306                                         }
5307                                         goto out;
5308                                 }
5309                                 if (dstmpte->wire_count >= srcmpte->wire_count)
5310                                         break;
5311                         }
5312                         addr += PAGE_SIZE;
5313                         src_pte++;
5314                 }
5315         }
5316 out:
5317         if (lock != NULL)
5318                 rw_wunlock(lock);
5319         PMAP_UNLOCK(src_pmap);
5320         PMAP_UNLOCK(dst_pmap);
5321 }
5322
5323 /*
5324  *      pmap_zero_page zeros the specified hardware page by mapping
5325  *      the page into KVM and using bzero to clear its contents.
5326  */
5327 void
5328 pmap_zero_page(vm_page_t m)
5329 {
5330         vm_offset_t va = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m));
5331
5332         pagezero((void *)va);
5333 }
5334
5335 /*
5336  *      pmap_zero_page_area zeros the specified hardware page by mapping 
5337  *      the page into KVM and using bzero to clear its contents.
5338  *
5339  *      off and size may not cover an area beyond a single hardware page.
5340  */
5341 void
5342 pmap_zero_page_area(vm_page_t m, int off, int size)
5343 {
5344         vm_offset_t va = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m));
5345
5346         if (off == 0 && size == PAGE_SIZE)
5347                 pagezero((void *)va);
5348         else
5349                 bzero((char *)va + off, size);
5350 }
5351
5352 /*
5353  *      pmap_zero_page_idle zeros the specified hardware page by mapping 
5354  *      the page into KVM and using bzero to clear its contents.  This
5355  *      is intended to be called from the vm_pagezero process only and
5356  *      outside of Giant.
5357  */
5358 void
5359 pmap_zero_page_idle(vm_page_t m)
5360 {
5361         vm_offset_t va = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m));
5362
5363         pagezero((void *)va);
5364 }
5365
5366 /*
5367  *      pmap_copy_page copies the specified (machine independent)
5368  *      page by mapping the page into virtual memory and using
5369  *      bcopy to copy the page, one machine dependent page at a
5370  *      time.
5371  */
5372 void
5373 pmap_copy_page(vm_page_t msrc, vm_page_t mdst)
5374 {
5375         vm_offset_t src = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(msrc));
5376         vm_offset_t dst = PHYS_TO_DMAP(VM_PAGE_TO_PHYS(mdst));
5377
5378         pagecopy((void *)src, (void *)dst);
5379 }
5380
5381 int unmapped_buf_allowed = 1;
5382
5383 void
5384 pmap_copy_pages(vm_page_t ma[], vm_offset_t a_offset, vm_page_t mb[],
5385     vm_offset_t b_offset, int xfersize)
5386 {
5387         void *a_cp, *b_cp;
5388         vm_page_t pages[2];
5389         vm_offset_t vaddr[2], a_pg_offset, b_pg_offset;
5390         int cnt;
5391         boolean_t mapped;
5392
5393         while (xfersize > 0) {
5394                 a_pg_offset = a_offset & PAGE_MASK;
5395                 pages[0] = ma[a_offset >> PAGE_SHIFT];
5396                 b_pg_offset = b_offset & PAGE_MASK;
5397                 pages[1] = mb[b_offset >> PAGE_SHIFT];
5398                 cnt = min(xfersize, PAGE_SIZE - a_pg_offset);
5399                 cnt = min(cnt, PAGE_SIZE - b_pg_offset);
5400                 mapped = pmap_map_io_transient(pages, vaddr, 2, FALSE);
5401                 a_cp = (char *)vaddr[0] + a_pg_offset;
5402                 b_cp = (char *)vaddr[1] + b_pg_offset;
5403                 bcopy(a_cp, b_cp, cnt);
5404                 if (__predict_false(mapped))
5405                         pmap_unmap_io_transient(pages, vaddr, 2, FALSE);
5406                 a_offset += cnt;
5407                 b_offset += cnt;
5408                 xfersize -= cnt;
5409         }
5410 }
5411
5412 /*
5413  * Returns true if the pmap's pv is one of the first
5414  * 16 pvs linked to from this page.  This count may
5415  * be changed upwards or downwards in the future; it
5416  * is only necessary that true be returned for a small
5417  * subset of pmaps for proper page aging.
5418  */
5419 boolean_t
5420 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
5421 {
5422         struct md_page *pvh;
5423         struct rwlock *lock;
5424         pv_entry_t pv;
5425         int loops = 0;
5426         boolean_t rv;
5427
5428         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5429             ("pmap_page_exists_quick: page %p is not managed", m));
5430         rv = FALSE;
5431         lock = VM_PAGE_TO_PV_LIST_LOCK(m);
5432         rw_rlock(lock);
5433         TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
5434                 if (PV_PMAP(pv) == pmap) {
5435                         rv = TRUE;
5436                         break;
5437                 }
5438                 loops++;
5439                 if (loops >= 16)
5440                         break;
5441         }
5442         if (!rv && loops < 16 && (m->flags & PG_FICTITIOUS) == 0) {
5443                 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
5444                 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
5445                         if (PV_PMAP(pv) == pmap) {
5446                                 rv = TRUE;
5447                                 break;
5448                         }
5449                         loops++;
5450                         if (loops >= 16)
5451                                 break;
5452                 }
5453         }
5454         rw_runlock(lock);
5455         return (rv);
5456 }
5457
5458 /*
5459  *      pmap_page_wired_mappings:
5460  *
5461  *      Return the number of managed mappings to the given physical page
5462  *      that are wired.
5463  */
5464 int
5465 pmap_page_wired_mappings(vm_page_t m)
5466 {
5467         struct rwlock *lock;
5468         struct md_page *pvh;
5469         pmap_t pmap;
5470         pt_entry_t *pte;
5471         pv_entry_t pv;
5472         int count, md_gen, pvh_gen;
5473
5474         if ((m->oflags & VPO_UNMANAGED) != 0)
5475                 return (0);
5476         lock = VM_PAGE_TO_PV_LIST_LOCK(m);
5477         rw_rlock(lock);
5478 restart:
5479         count = 0;
5480         TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
5481                 pmap = PV_PMAP(pv);
5482                 if (!PMAP_TRYLOCK(pmap)) {
5483                         md_gen = m->md.pv_gen;
5484                         rw_runlock(lock);
5485                         PMAP_LOCK(pmap);
5486                         rw_rlock(lock);
5487                         if (md_gen != m->md.pv_gen) {
5488                                 PMAP_UNLOCK(pmap);
5489                                 goto restart;
5490                         }
5491                 }
5492                 pte = pmap_pte(pmap, pv->pv_va);
5493                 if ((*pte & PG_W) != 0)
5494                         count++;
5495                 PMAP_UNLOCK(pmap);
5496         }
5497         if ((m->flags & PG_FICTITIOUS) == 0) {
5498                 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
5499                 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
5500                         pmap = PV_PMAP(pv);
5501                         if (!PMAP_TRYLOCK(pmap)) {
5502                                 md_gen = m->md.pv_gen;
5503                                 pvh_gen = pvh->pv_gen;
5504                                 rw_runlock(lock);
5505                                 PMAP_LOCK(pmap);
5506                                 rw_rlock(lock);
5507                                 if (md_gen != m->md.pv_gen ||
5508                                     pvh_gen != pvh->pv_gen) {
5509                                         PMAP_UNLOCK(pmap);
5510                                         goto restart;
5511                                 }
5512                         }
5513                         pte = pmap_pde(pmap, pv->pv_va);
5514                         if ((*pte & PG_W) != 0)
5515                                 count++;
5516                         PMAP_UNLOCK(pmap);
5517                 }
5518         }
5519         rw_runlock(lock);
5520         return (count);
5521 }
5522
5523 /*
5524  * Returns TRUE if the given page is mapped individually or as part of
5525  * a 2mpage.  Otherwise, returns FALSE.
5526  */
5527 boolean_t
5528 pmap_page_is_mapped(vm_page_t m)
5529 {
5530         struct rwlock *lock;
5531         boolean_t rv;
5532
5533         if ((m->oflags & VPO_UNMANAGED) != 0)
5534                 return (FALSE);
5535         lock = VM_PAGE_TO_PV_LIST_LOCK(m);
5536         rw_rlock(lock);
5537         rv = !TAILQ_EMPTY(&m->md.pv_list) ||
5538             ((m->flags & PG_FICTITIOUS) == 0 &&
5539             !TAILQ_EMPTY(&pa_to_pvh(VM_PAGE_TO_PHYS(m))->pv_list));
5540         rw_runlock(lock);
5541         return (rv);
5542 }
5543
5544 /*
5545  * Destroy all managed, non-wired mappings in the given user-space
5546  * pmap.  This pmap cannot be active on any processor besides the
5547  * caller.
5548  *
5549  * This function cannot be applied to the kernel pmap.  Moreover, it
5550  * is not intended for general use.  It is only to be used during
5551  * process termination.  Consequently, it can be implemented in ways
5552  * that make it faster than pmap_remove().  First, it can more quickly
5553  * destroy mappings by iterating over the pmap's collection of PV
5554  * entries, rather than searching the page table.  Second, it doesn't
5555  * have to test and clear the page table entries atomically, because
5556  * no processor is currently accessing the user address space.  In
5557  * particular, a page table entry's dirty bit won't change state once
5558  * this function starts.
5559  *
5560  * Although this function destroys all of the pmap's managed,
5561  * non-wired mappings, it can delay and batch the invalidation of TLB
5562  * entries without calling pmap_delayed_invl_started() and
5563  * pmap_delayed_invl_finished().  Because the pmap is not active on
5564  * any other processor, none of these TLB entries will ever be used
5565  * before their eventual invalidation.  Consequently, there is no need
5566  * for either pmap_remove_all() or pmap_remove_write() to wait for
5567  * that eventual TLB invalidation.
5568  */
5569 void
5570 pmap_remove_pages(pmap_t pmap)
5571 {
5572         pd_entry_t ptepde;
5573         pt_entry_t *pte, tpte;
5574         pt_entry_t PG_M, PG_RW, PG_V;
5575         struct spglist free;
5576         vm_page_t m, mpte, mt;
5577         pv_entry_t pv;
5578         struct md_page *pvh;
5579         struct pv_chunk *pc, *npc;
5580         struct rwlock *lock;
5581         int64_t bit;
5582         uint64_t inuse, bitmask;
5583         int allfree, field, freed, idx;
5584         boolean_t superpage;
5585         vm_paddr_t pa;
5586
5587         /*
5588          * Assert that the given pmap is only active on the current
5589          * CPU.  Unfortunately, we cannot block another CPU from
5590          * activating the pmap while this function is executing.
5591          */
5592         KASSERT(pmap == PCPU_GET(curpmap), ("non-current pmap %p", pmap));
5593 #ifdef INVARIANTS
5594         {
5595                 cpuset_t other_cpus;
5596
5597                 other_cpus = all_cpus;
5598                 critical_enter();
5599                 CPU_CLR(PCPU_GET(cpuid), &other_cpus);
5600                 CPU_AND(&other_cpus, &pmap->pm_active);
5601                 critical_exit();
5602                 KASSERT(CPU_EMPTY(&other_cpus), ("pmap active %p", pmap));
5603         }
5604 #endif
5605
5606         lock = NULL;
5607         PG_M = pmap_modified_bit(pmap);
5608         PG_V = pmap_valid_bit(pmap);
5609         PG_RW = pmap_rw_bit(pmap);
5610
5611         SLIST_INIT(&free);
5612         PMAP_LOCK(pmap);
5613         TAILQ_FOREACH_SAFE(pc, &pmap->pm_pvchunk, pc_list, npc) {
5614                 allfree = 1;
5615                 freed = 0;
5616                 for (field = 0; field < _NPCM; field++) {
5617                         inuse = ~pc->pc_map[field] & pc_freemask[field];
5618                         while (inuse != 0) {
5619                                 bit = bsfq(inuse);
5620                                 bitmask = 1UL << bit;
5621                                 idx = field * 64 + bit;
5622                                 pv = &pc->pc_pventry[idx];
5623                                 inuse &= ~bitmask;
5624
5625                                 pte = pmap_pdpe(pmap, pv->pv_va);
5626                                 ptepde = *pte;
5627                                 pte = pmap_pdpe_to_pde(pte, pv->pv_va);
5628                                 tpte = *pte;
5629                                 if ((tpte & (PG_PS | PG_V)) == PG_V) {
5630                                         superpage = FALSE;
5631                                         ptepde = tpte;
5632                                         pte = (pt_entry_t *)PHYS_TO_DMAP(tpte &
5633                                             PG_FRAME);
5634                                         pte = &pte[pmap_pte_index(pv->pv_va)];
5635                                         tpte = *pte;
5636                                 } else {
5637                                         /*
5638                                          * Keep track whether 'tpte' is a
5639                                          * superpage explicitly instead of
5640                                          * relying on PG_PS being set.
5641                                          *
5642                                          * This is because PG_PS is numerically
5643                                          * identical to PG_PTE_PAT and thus a
5644                                          * regular page could be mistaken for
5645                                          * a superpage.
5646                                          */
5647                                         superpage = TRUE;
5648                                 }
5649
5650                                 if ((tpte & PG_V) == 0) {
5651                                         panic("bad pte va %lx pte %lx",
5652                                             pv->pv_va, tpte);
5653                                 }
5654
5655 /*
5656  * We cannot remove wired pages from a process' mapping at this time
5657  */
5658                                 if (tpte & PG_W) {
5659                                         allfree = 0;
5660                                         continue;
5661                                 }
5662
5663                                 if (superpage)
5664                                         pa = tpte & PG_PS_FRAME;
5665                                 else
5666                                         pa = tpte & PG_FRAME;
5667
5668                                 m = PHYS_TO_VM_PAGE(pa);
5669                                 KASSERT(m->phys_addr == pa,
5670                                     ("vm_page_t %p phys_addr mismatch %016jx %016jx",
5671                                     m, (uintmax_t)m->phys_addr,
5672                                     (uintmax_t)tpte));
5673
5674                                 KASSERT((m->flags & PG_FICTITIOUS) != 0 ||
5675                                     m < &vm_page_array[vm_page_array_size],
5676                                     ("pmap_remove_pages: bad tpte %#jx",
5677                                     (uintmax_t)tpte));
5678
5679                                 pte_clear(pte);
5680
5681                                 /*
5682                                  * Update the vm_page_t clean/reference bits.
5683                                  */
5684                                 if ((tpte & (PG_M | PG_RW)) == (PG_M | PG_RW)) {
5685                                         if (superpage) {
5686                                                 for (mt = m; mt < &m[NBPDR / PAGE_SIZE]; mt++)
5687                                                         vm_page_dirty(mt);
5688                                         } else
5689                                                 vm_page_dirty(m);
5690                                 }
5691
5692                                 CHANGE_PV_LIST_LOCK_TO_VM_PAGE(&lock, m);
5693
5694                                 /* Mark free */
5695                                 pc->pc_map[field] |= bitmask;
5696                                 if (superpage) {
5697                                         pmap_resident_count_dec(pmap, NBPDR / PAGE_SIZE);
5698                                         pvh = pa_to_pvh(tpte & PG_PS_FRAME);
5699                                         TAILQ_REMOVE(&pvh->pv_list, pv, pv_next);
5700                                         pvh->pv_gen++;
5701                                         if (TAILQ_EMPTY(&pvh->pv_list)) {
5702                                                 for (mt = m; mt < &m[NBPDR / PAGE_SIZE]; mt++)
5703                                                         if ((mt->aflags & PGA_WRITEABLE) != 0 &&
5704                                                             TAILQ_EMPTY(&mt->md.pv_list))
5705                                                                 vm_page_aflag_clear(mt, PGA_WRITEABLE);
5706                                         }
5707                                         mpte = pmap_remove_pt_page(pmap, pv->pv_va);
5708                                         if (mpte != NULL) {
5709                                                 pmap_resident_count_dec(pmap, 1);
5710                                                 KASSERT(mpte->wire_count == NPTEPG,
5711                                                     ("pmap_remove_pages: pte page wire count error"));
5712                                                 mpte->wire_count = 0;
5713                                                 pmap_add_delayed_free_list(mpte, &free, FALSE);
5714                                         }
5715                                 } else {
5716                                         pmap_resident_count_dec(pmap, 1);
5717                                         TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
5718                                         m->md.pv_gen++;
5719                                         if ((m->aflags & PGA_WRITEABLE) != 0 &&
5720                                             TAILQ_EMPTY(&m->md.pv_list) &&
5721                                             (m->flags & PG_FICTITIOUS) == 0) {
5722                                                 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
5723                                                 if (TAILQ_EMPTY(&pvh->pv_list))
5724                                                         vm_page_aflag_clear(m, PGA_WRITEABLE);
5725                                         }
5726                                 }
5727                                 pmap_unuse_pt(pmap, pv->pv_va, ptepde, &free);
5728                                 freed++;
5729                         }
5730                 }
5731                 PV_STAT(atomic_add_long(&pv_entry_frees, freed));
5732                 PV_STAT(atomic_add_int(&pv_entry_spare, freed));
5733                 PV_STAT(atomic_subtract_long(&pv_entry_count, freed));
5734                 if (allfree) {
5735                         TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
5736                         free_pv_chunk(pc);
5737                 }
5738         }
5739         if (lock != NULL)
5740                 rw_wunlock(lock);
5741         pmap_invalidate_all(pmap);
5742         PMAP_UNLOCK(pmap);
5743         pmap_free_zero_pages(&free);
5744 }
5745
5746 static boolean_t
5747 pmap_page_test_mappings(vm_page_t m, boolean_t accessed, boolean_t modified)
5748 {
5749         struct rwlock *lock;
5750         pv_entry_t pv;
5751         struct md_page *pvh;
5752         pt_entry_t *pte, mask;
5753         pt_entry_t PG_A, PG_M, PG_RW, PG_V;
5754         pmap_t pmap;
5755         int md_gen, pvh_gen;
5756         boolean_t rv;
5757
5758         rv = FALSE;
5759         lock = VM_PAGE_TO_PV_LIST_LOCK(m);
5760         rw_rlock(lock);
5761 restart:
5762         TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
5763                 pmap = PV_PMAP(pv);
5764                 if (!PMAP_TRYLOCK(pmap)) {
5765                         md_gen = m->md.pv_gen;
5766                         rw_runlock(lock);
5767                         PMAP_LOCK(pmap);
5768                         rw_rlock(lock);
5769                         if (md_gen != m->md.pv_gen) {
5770                                 PMAP_UNLOCK(pmap);
5771                                 goto restart;
5772                         }
5773                 }
5774                 pte = pmap_pte(pmap, pv->pv_va);
5775                 mask = 0;
5776                 if (modified) {
5777                         PG_M = pmap_modified_bit(pmap);
5778                         PG_RW = pmap_rw_bit(pmap);
5779                         mask |= PG_RW | PG_M;
5780                 }
5781                 if (accessed) {
5782                         PG_A = pmap_accessed_bit(pmap);
5783                         PG_V = pmap_valid_bit(pmap);
5784                         mask |= PG_V | PG_A;
5785                 }
5786                 rv = (*pte & mask) == mask;
5787                 PMAP_UNLOCK(pmap);
5788                 if (rv)
5789                         goto out;
5790         }
5791         if ((m->flags & PG_FICTITIOUS) == 0) {
5792                 pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
5793                 TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
5794                         pmap = PV_PMAP(pv);
5795                         if (!PMAP_TRYLOCK(pmap)) {
5796                                 md_gen = m->md.pv_gen;
5797                                 pvh_gen = pvh->pv_gen;
5798                                 rw_runlock(lock);
5799                                 PMAP_LOCK(pmap);
5800                                 rw_rlock(lock);
5801                                 if (md_gen != m->md.pv_gen ||
5802                                     pvh_gen != pvh->pv_gen) {
5803                                         PMAP_UNLOCK(pmap);
5804                                         goto restart;
5805                                 }
5806                         }
5807                         pte = pmap_pde(pmap, pv->pv_va);
5808                         mask = 0;
5809                         if (modified) {
5810                                 PG_M = pmap_modified_bit(pmap);
5811                                 PG_RW = pmap_rw_bit(pmap);
5812                                 mask |= PG_RW | PG_M;
5813                         }
5814                         if (accessed) {
5815                                 PG_A = pmap_accessed_bit(pmap);
5816                                 PG_V = pmap_valid_bit(pmap);
5817                                 mask |= PG_V | PG_A;
5818                         }
5819                         rv = (*pte & mask) == mask;
5820                         PMAP_UNLOCK(pmap);
5821                         if (rv)
5822                                 goto out;
5823                 }
5824         }
5825 out:
5826         rw_runlock(lock);
5827         return (rv);
5828 }
5829
5830 /*
5831  *      pmap_is_modified:
5832  *
5833  *      Return whether or not the specified physical page was modified
5834  *      in any physical maps.
5835  */
5836 boolean_t
5837 pmap_is_modified(vm_page_t m)
5838 {
5839
5840         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5841             ("pmap_is_modified: page %p is not managed", m));
5842
5843         /*
5844          * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
5845          * concurrently set while the object is locked.  Thus, if PGA_WRITEABLE
5846          * is clear, no PTEs can have PG_M set.
5847          */
5848         VM_OBJECT_ASSERT_WLOCKED(m->object);
5849         if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
5850                 return (FALSE);
5851         return (pmap_page_test_mappings(m, FALSE, TRUE));
5852 }
5853
5854 /*
5855  *      pmap_is_prefaultable:
5856  *
5857  *      Return whether or not the specified virtual address is eligible
5858  *      for prefault.
5859  */
5860 boolean_t
5861 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
5862 {
5863         pd_entry_t *pde;
5864         pt_entry_t *pte, PG_V;
5865         boolean_t rv;
5866
5867         PG_V = pmap_valid_bit(pmap);
5868         rv = FALSE;
5869         PMAP_LOCK(pmap);
5870         pde = pmap_pde(pmap, addr);
5871         if (pde != NULL && (*pde & (PG_PS | PG_V)) == PG_V) {
5872                 pte = pmap_pde_to_pte(pde, addr);
5873                 rv = (*pte & PG_V) == 0;
5874         }
5875         PMAP_UNLOCK(pmap);
5876         return (rv);
5877 }
5878
5879 /*
5880  *      pmap_is_referenced:
5881  *
5882  *      Return whether or not the specified physical page was referenced
5883  *      in any physical maps.
5884  */
5885 boolean_t
5886 pmap_is_referenced(vm_page_t m)
5887 {
5888
5889         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5890             ("pmap_is_referenced: page %p is not managed", m));
5891         return (pmap_page_test_mappings(m, TRUE, FALSE));
5892 }
5893
5894 /*
5895  * Clear the write and modified bits in each of the given page's mappings.
5896  */
5897 void
5898 pmap_remove_write(vm_page_t m)
5899 {
5900         struct md_page *pvh;
5901         pmap_t pmap;
5902         struct rwlock *lock;
5903         pv_entry_t next_pv, pv;
5904         pd_entry_t *pde;
5905         pt_entry_t oldpte, *pte, PG_M, PG_RW;
5906         vm_offset_t va;
5907         int pvh_gen, md_gen;
5908
5909         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5910             ("pmap_remove_write: page %p is not managed", m));
5911
5912         /*
5913          * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
5914          * set by another thread while the object is locked.  Thus,
5915          * if PGA_WRITEABLE is clear, no page table entries need updating.
5916          */
5917         VM_OBJECT_ASSERT_WLOCKED(m->object);
5918         if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
5919                 return;
5920         lock = VM_PAGE_TO_PV_LIST_LOCK(m);
5921         pvh = (m->flags & PG_FICTITIOUS) != 0 ? &pv_dummy :
5922             pa_to_pvh(VM_PAGE_TO_PHYS(m));
5923 retry_pv_loop:
5924         rw_wlock(lock);
5925         TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_next, next_pv) {
5926                 pmap = PV_PMAP(pv);
5927                 if (!PMAP_TRYLOCK(pmap)) {
5928                         pvh_gen = pvh->pv_gen;
5929                         rw_wunlock(lock);
5930                         PMAP_LOCK(pmap);
5931                         rw_wlock(lock);
5932                         if (pvh_gen != pvh->pv_gen) {
5933                                 PMAP_UNLOCK(pmap);
5934                                 rw_wunlock(lock);
5935                                 goto retry_pv_loop;
5936                         }
5937                 }
5938                 PG_RW = pmap_rw_bit(pmap);
5939                 va = pv->pv_va;
5940                 pde = pmap_pde(pmap, va);
5941                 if ((*pde & PG_RW) != 0)
5942                         (void)pmap_demote_pde_locked(pmap, pde, va, &lock);
5943                 KASSERT(lock == VM_PAGE_TO_PV_LIST_LOCK(m),
5944                     ("inconsistent pv lock %p %p for page %p",
5945                     lock, VM_PAGE_TO_PV_LIST_LOCK(m), m));
5946                 PMAP_UNLOCK(pmap);
5947         }
5948         TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
5949                 pmap = PV_PMAP(pv);
5950                 if (!PMAP_TRYLOCK(pmap)) {
5951                         pvh_gen = pvh->pv_gen;
5952                         md_gen = m->md.pv_gen;
5953                         rw_wunlock(lock);
5954                         PMAP_LOCK(pmap);
5955                         rw_wlock(lock);
5956                         if (pvh_gen != pvh->pv_gen ||
5957                             md_gen != m->md.pv_gen) {
5958                                 PMAP_UNLOCK(pmap);
5959                                 rw_wunlock(lock);
5960                                 goto retry_pv_loop;
5961                         }
5962                 }
5963                 PG_M = pmap_modified_bit(pmap);
5964                 PG_RW = pmap_rw_bit(pmap);
5965                 pde = pmap_pde(pmap, pv->pv_va);
5966                 KASSERT((*pde & PG_PS) == 0,
5967                     ("pmap_remove_write: found a 2mpage in page %p's pv list",
5968                     m));
5969                 pte = pmap_pde_to_pte(pde, pv->pv_va);
5970 retry:
5971                 oldpte = *pte;
5972                 if (oldpte & PG_RW) {
5973                         if (!atomic_cmpset_long(pte, oldpte, oldpte &
5974                             ~(PG_RW | PG_M)))
5975                                 goto retry;
5976                         if ((oldpte & PG_M) != 0)
5977                                 vm_page_dirty(m);
5978                         pmap_invalidate_page(pmap, pv->pv_va);
5979                 }
5980                 PMAP_UNLOCK(pmap);
5981         }
5982         rw_wunlock(lock);
5983         vm_page_aflag_clear(m, PGA_WRITEABLE);
5984         pmap_delayed_invl_wait(m);
5985 }
5986
5987 static __inline boolean_t
5988 safe_to_clear_referenced(pmap_t pmap, pt_entry_t pte)
5989 {
5990
5991         if (!pmap_emulate_ad_bits(pmap))
5992                 return (TRUE);
5993
5994         KASSERT(pmap->pm_type == PT_EPT, ("invalid pm_type %d", pmap->pm_type));
5995
5996         /*
5997          * XWR = 010 or 110 will cause an unconditional EPT misconfiguration
5998          * so we don't let the referenced (aka EPT_PG_READ) bit to be cleared
5999          * if the EPT_PG_WRITE bit is set.
6000          */
6001         if ((pte & EPT_PG_WRITE) != 0)
6002                 return (FALSE);
6003
6004         /*
6005          * XWR = 100 is allowed only if the PMAP_SUPPORTS_EXEC_ONLY is set.
6006          */
6007         if ((pte & EPT_PG_EXECUTE) == 0 ||
6008             ((pmap->pm_flags & PMAP_SUPPORTS_EXEC_ONLY) != 0))
6009                 return (TRUE);
6010         else
6011                 return (FALSE);
6012 }
6013
6014 /*
6015  *      pmap_ts_referenced:
6016  *
6017  *      Return a count of reference bits for a page, clearing those bits.
6018  *      It is not necessary for every reference bit to be cleared, but it
6019  *      is necessary that 0 only be returned when there are truly no
6020  *      reference bits set.
6021  *
6022  *      As an optimization, update the page's dirty field if a modified bit is
6023  *      found while counting reference bits.  This opportunistic update can be
6024  *      performed at low cost and can eliminate the need for some future calls
6025  *      to pmap_is_modified().  However, since this function stops after
6026  *      finding PMAP_TS_REFERENCED_MAX reference bits, it may not detect some
6027  *      dirty pages.  Those dirty pages will only be detected by a future call
6028  *      to pmap_is_modified().
6029  *
6030  *      A DI block is not needed within this function, because
6031  *      invalidations are performed before the PV list lock is
6032  *      released.
6033  */
6034 int
6035 pmap_ts_referenced(vm_page_t m)
6036 {
6037         struct md_page *pvh;
6038         pv_entry_t pv, pvf;
6039         pmap_t pmap;
6040         struct rwlock *lock;
6041         pd_entry_t oldpde, *pde;
6042         pt_entry_t *pte, PG_A, PG_M, PG_RW;
6043         vm_offset_t va;
6044         vm_paddr_t pa;
6045         int cleared, md_gen, not_cleared, pvh_gen;
6046         struct spglist free;
6047         boolean_t demoted;
6048
6049         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
6050             ("pmap_ts_referenced: page %p is not managed", m));
6051         SLIST_INIT(&free);
6052         cleared = 0;
6053         pa = VM_PAGE_TO_PHYS(m);
6054         lock = PHYS_TO_PV_LIST_LOCK(pa);
6055         pvh = (m->flags & PG_FICTITIOUS) != 0 ? &pv_dummy : pa_to_pvh(pa);
6056         rw_wlock(lock);
6057 retry:
6058         not_cleared = 0;
6059         if ((pvf = TAILQ_FIRST(&pvh->pv_list)) == NULL)
6060                 goto small_mappings;
6061         pv = pvf;
6062         do {
6063                 if (pvf == NULL)
6064                         pvf = pv;
6065                 pmap = PV_PMAP(pv);
6066                 if (!PMAP_TRYLOCK(pmap)) {
6067                         pvh_gen = pvh->pv_gen;
6068                         rw_wunlock(lock);
6069                         PMAP_LOCK(pmap);
6070                         rw_wlock(lock);
6071                         if (pvh_gen != pvh->pv_gen) {
6072                                 PMAP_UNLOCK(pmap);
6073                                 goto retry;
6074                         }
6075                 }
6076                 PG_A = pmap_accessed_bit(pmap);
6077                 PG_M = pmap_modified_bit(pmap);
6078                 PG_RW = pmap_rw_bit(pmap);
6079                 va = pv->pv_va;
6080                 pde = pmap_pde(pmap, pv->pv_va);
6081                 oldpde = *pde;
6082                 if ((oldpde & (PG_M | PG_RW)) == (PG_M | PG_RW)) {
6083                         /*
6084                          * Although "oldpde" is mapping a 2MB page, because
6085                          * this function is called at a 4KB page granularity,
6086                          * we only update the 4KB page under test.
6087                          */
6088                         vm_page_dirty(m);
6089                 }
6090                 if ((oldpde & PG_A) != 0) {
6091                         /*
6092                          * Since this reference bit is shared by 512 4KB
6093                          * pages, it should not be cleared every time it is
6094                          * tested.  Apply a simple "hash" function on the
6095                          * physical page number, the virtual superpage number,
6096                          * and the pmap address to select one 4KB page out of
6097                          * the 512 on which testing the reference bit will
6098                          * result in clearing that reference bit.  This
6099                          * function is designed to avoid the selection of the
6100                          * same 4KB page for every 2MB page mapping.
6101                          *
6102                          * On demotion, a mapping that hasn't been referenced
6103                          * is simply destroyed.  To avoid the possibility of a
6104                          * subsequent page fault on a demoted wired mapping,
6105                          * always leave its reference bit set.  Moreover,
6106                          * since the superpage is wired, the current state of
6107                          * its reference bit won't affect page replacement.
6108                          */
6109                         if ((((pa >> PAGE_SHIFT) ^ (pv->pv_va >> PDRSHIFT) ^
6110                             (uintptr_t)pmap) & (NPTEPG - 1)) == 0 &&
6111                             (oldpde & PG_W) == 0) {
6112                                 if (safe_to_clear_referenced(pmap, oldpde)) {
6113                                         atomic_clear_long(pde, PG_A);
6114                                         pmap_invalidate_page(pmap, pv->pv_va);
6115                                         demoted = FALSE;
6116                                 } else if (pmap_demote_pde_locked(pmap, pde,
6117                                     pv->pv_va, &lock)) {
6118                                         /*
6119                                          * Remove the mapping to a single page
6120                                          * so that a subsequent access may
6121                                          * repromote.  Since the underlying
6122                                          * page table page is fully populated,
6123                                          * this removal never frees a page
6124                                          * table page.
6125                                          */
6126                                         demoted = TRUE;
6127                                         va += VM_PAGE_TO_PHYS(m) - (oldpde &
6128                                             PG_PS_FRAME);
6129                                         pte = pmap_pde_to_pte(pde, va);
6130                                         pmap_remove_pte(pmap, pte, va, *pde,
6131                                             NULL, &lock);
6132                                         pmap_invalidate_page(pmap, va);
6133                                 } else
6134                                         demoted = TRUE;
6135
6136                                 if (demoted) {
6137                                         /*
6138                                          * The superpage mapping was removed
6139                                          * entirely and therefore 'pv' is no
6140                                          * longer valid.
6141                                          */
6142                                         if (pvf == pv)
6143                                                 pvf = NULL;
6144                                         pv = NULL;
6145                                 }
6146                                 cleared++;
6147                                 KASSERT(lock == VM_PAGE_TO_PV_LIST_LOCK(m),
6148                                     ("inconsistent pv lock %p %p for page %p",
6149                                     lock, VM_PAGE_TO_PV_LIST_LOCK(m), m));
6150                         } else
6151                                 not_cleared++;
6152                 }
6153                 PMAP_UNLOCK(pmap);
6154                 /* Rotate the PV list if it has more than one entry. */
6155                 if (pv != NULL && TAILQ_NEXT(pv, pv_next) != NULL) {
6156                         TAILQ_REMOVE(&pvh->pv_list, pv, pv_next);
6157                         TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_next);
6158                         pvh->pv_gen++;
6159                 }
6160                 if (cleared + not_cleared >= PMAP_TS_REFERENCED_MAX)
6161                         goto out;
6162         } while ((pv = TAILQ_FIRST(&pvh->pv_list)) != pvf);
6163 small_mappings:
6164         if ((pvf = TAILQ_FIRST(&m->md.pv_list)) == NULL)
6165                 goto out;
6166         pv = pvf;
6167         do {
6168                 if (pvf == NULL)
6169                         pvf = pv;
6170                 pmap = PV_PMAP(pv);
6171                 if (!PMAP_TRYLOCK(pmap)) {
6172                         pvh_gen = pvh->pv_gen;
6173                         md_gen = m->md.pv_gen;
6174                         rw_wunlock(lock);
6175                         PMAP_LOCK(pmap);
6176                         rw_wlock(lock);
6177                         if (pvh_gen != pvh->pv_gen || md_gen != m->md.pv_gen) {
6178                                 PMAP_UNLOCK(pmap);
6179                                 goto retry;
6180                         }
6181                 }
6182                 PG_A = pmap_accessed_bit(pmap);
6183                 PG_M = pmap_modified_bit(pmap);
6184                 PG_RW = pmap_rw_bit(pmap);
6185                 pde = pmap_pde(pmap, pv->pv_va);
6186                 KASSERT((*pde & PG_PS) == 0,
6187                     ("pmap_ts_referenced: found a 2mpage in page %p's pv list",
6188                     m));
6189                 pte = pmap_pde_to_pte(pde, pv->pv_va);
6190                 if ((*pte & (PG_M | PG_RW)) == (PG_M | PG_RW))
6191                         vm_page_dirty(m);
6192                 if ((*pte & PG_A) != 0) {
6193                         if (safe_to_clear_referenced(pmap, *pte)) {
6194                                 atomic_clear_long(pte, PG_A);
6195                                 pmap_invalidate_page(pmap, pv->pv_va);
6196                                 cleared++;
6197                         } else if ((*pte & PG_W) == 0) {
6198                                 /*
6199                                  * Wired pages cannot be paged out so
6200                                  * doing accessed bit emulation for
6201                                  * them is wasted effort. We do the
6202                                  * hard work for unwired pages only.
6203                                  */
6204                                 pmap_remove_pte(pmap, pte, pv->pv_va,
6205                                     *pde, &free, &lock);
6206                                 pmap_invalidate_page(pmap, pv->pv_va);
6207                                 cleared++;
6208                                 if (pvf == pv)
6209                                         pvf = NULL;
6210                                 pv = NULL;
6211                                 KASSERT(lock == VM_PAGE_TO_PV_LIST_LOCK(m),
6212                                     ("inconsistent pv lock %p %p for page %p",
6213                                     lock, VM_PAGE_TO_PV_LIST_LOCK(m), m));
6214                         } else
6215                                 not_cleared++;
6216                 }
6217                 PMAP_UNLOCK(pmap);
6218                 /* Rotate the PV list if it has more than one entry. */
6219                 if (pv != NULL && TAILQ_NEXT(pv, pv_next) != NULL) {
6220                         TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
6221                         TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
6222                         m->md.pv_gen++;
6223                 }
6224         } while ((pv = TAILQ_FIRST(&m->md.pv_list)) != pvf && cleared +
6225             not_cleared < PMAP_TS_REFERENCED_MAX);
6226 out:
6227         rw_wunlock(lock);
6228         pmap_free_zero_pages(&free);
6229         return (cleared + not_cleared);
6230 }
6231
6232 /*
6233  *      Apply the given advice to the specified range of addresses within the
6234  *      given pmap.  Depending on the advice, clear the referenced and/or
6235  *      modified flags in each mapping and set the mapped page's dirty field.
6236  */
6237 void
6238 pmap_advise(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, int advice)
6239 {
6240         struct rwlock *lock;
6241         pml4_entry_t *pml4e;
6242         pdp_entry_t *pdpe;
6243         pd_entry_t oldpde, *pde;
6244         pt_entry_t *pte, PG_A, PG_G, PG_M, PG_RW, PG_V;
6245         vm_offset_t va, va_next;
6246         vm_page_t m;
6247         boolean_t anychanged;
6248
6249         if (advice != MADV_DONTNEED && advice != MADV_FREE)
6250                 return;
6251
6252         /*
6253          * A/D bit emulation requires an alternate code path when clearing
6254          * the modified and accessed bits below. Since this function is
6255          * advisory in nature we skip it entirely for pmaps that require
6256          * A/D bit emulation.
6257          */
6258         if (pmap_emulate_ad_bits(pmap))
6259                 return;
6260
6261         PG_A = pmap_accessed_bit(pmap);
6262         PG_G = pmap_global_bit(pmap);
6263         PG_M = pmap_modified_bit(pmap);
6264         PG_V = pmap_valid_bit(pmap);
6265         PG_RW = pmap_rw_bit(pmap);
6266         anychanged = FALSE;
6267         pmap_delayed_invl_started();
6268         PMAP_LOCK(pmap);
6269         for (; sva < eva; sva = va_next) {
6270                 pml4e = pmap_pml4e(pmap, sva);
6271                 if ((*pml4e & PG_V) == 0) {
6272                         va_next = (sva + NBPML4) & ~PML4MASK;
6273                         if (va_next < sva)
6274                                 va_next = eva;
6275                         continue;
6276                 }
6277                 pdpe = pmap_pml4e_to_pdpe(pml4e, sva);
6278                 if ((*pdpe & PG_V) == 0) {
6279                         va_next = (sva + NBPDP) & ~PDPMASK;
6280                         if (va_next < sva)
6281                                 va_next = eva;
6282                         continue;
6283                 }
6284                 va_next = (sva + NBPDR) & ~PDRMASK;
6285                 if (va_next < sva)
6286                         va_next = eva;
6287                 pde = pmap_pdpe_to_pde(pdpe, sva);
6288                 oldpde = *pde;
6289                 if ((oldpde & PG_V) == 0)
6290                         continue;
6291                 else if ((oldpde & PG_PS) != 0) {
6292                         if ((oldpde & PG_MANAGED) == 0)
6293                                 continue;
6294                         lock = NULL;
6295                         if (!pmap_demote_pde_locked(pmap, pde, sva, &lock)) {
6296                                 if (lock != NULL)
6297                                         rw_wunlock(lock);
6298
6299                                 /*
6300                                  * The large page mapping was destroyed.
6301                                  */
6302                                 continue;
6303                         }
6304
6305                         /*
6306                          * Unless the page mappings are wired, remove the
6307                          * mapping to a single page so that a subsequent
6308                          * access may repromote.  Since the underlying page
6309                          * table page is fully populated, this removal never
6310                          * frees a page table page.
6311                          */
6312                         if ((oldpde & PG_W) == 0) {
6313                                 pte = pmap_pde_to_pte(pde, sva);
6314                                 KASSERT((*pte & PG_V) != 0,
6315                                     ("pmap_advise: invalid PTE"));
6316                                 pmap_remove_pte(pmap, pte, sva, *pde, NULL,
6317                                     &lock);
6318                                 anychanged = TRUE;
6319                         }
6320                         if (lock != NULL)
6321                                 rw_wunlock(lock);
6322                 }
6323                 if (va_next > eva)
6324                         va_next = eva;
6325                 va = va_next;
6326                 for (pte = pmap_pde_to_pte(pde, sva); sva != va_next; pte++,
6327                     sva += PAGE_SIZE) {
6328                         if ((*pte & (PG_MANAGED | PG_V)) != (PG_MANAGED | PG_V))
6329                                 goto maybe_invlrng;
6330                         else if ((*pte & (PG_M | PG_RW)) == (PG_M | PG_RW)) {
6331                                 if (advice == MADV_DONTNEED) {
6332                                         /*
6333                                          * Future calls to pmap_is_modified()
6334                                          * can be avoided by making the page
6335                                          * dirty now.
6336                                          */
6337                                         m = PHYS_TO_VM_PAGE(*pte & PG_FRAME);
6338                                         vm_page_dirty(m);
6339                                 }
6340                                 atomic_clear_long(pte, PG_M | PG_A);
6341                         } else if ((*pte & PG_A) != 0)
6342                                 atomic_clear_long(pte, PG_A);
6343                         else
6344                                 goto maybe_invlrng;
6345
6346                         if ((*pte & PG_G) != 0) {
6347                                 if (va == va_next)
6348                                         va = sva;
6349                         } else
6350                                 anychanged = TRUE;
6351                         continue;
6352 maybe_invlrng:
6353                         if (va != va_next) {
6354                                 pmap_invalidate_range(pmap, va, sva);
6355                                 va = va_next;
6356                         }
6357                 }
6358                 if (va != va_next)
6359                         pmap_invalidate_range(pmap, va, sva);
6360         }
6361         if (anychanged)
6362                 pmap_invalidate_all(pmap);
6363         PMAP_UNLOCK(pmap);
6364         pmap_delayed_invl_finished();
6365 }
6366
6367 /*
6368  *      Clear the modify bits on the specified physical page.
6369  */
6370 void
6371 pmap_clear_modify(vm_page_t m)
6372 {
6373         struct md_page *pvh;
6374         pmap_t pmap;
6375         pv_entry_t next_pv, pv;
6376         pd_entry_t oldpde, *pde;
6377         pt_entry_t oldpte, *pte, PG_M, PG_RW, PG_V;
6378         struct rwlock *lock;
6379         vm_offset_t va;
6380         int md_gen, pvh_gen;
6381
6382         KASSERT((m->oflags & VPO_UNMANAGED) == 0,
6383             ("pmap_clear_modify: page %p is not managed", m));
6384         VM_OBJECT_ASSERT_WLOCKED(m->object);
6385         KASSERT(!vm_page_xbusied(m),
6386             ("pmap_clear_modify: page %p is exclusive busied", m));
6387
6388         /*
6389          * If the page is not PGA_WRITEABLE, then no PTEs can have PG_M set.
6390          * If the object containing the page is locked and the page is not
6391          * exclusive busied, then PGA_WRITEABLE cannot be concurrently set.
6392          */
6393         if ((m->aflags & PGA_WRITEABLE) == 0)
6394                 return;
6395         pvh = (m->flags & PG_FICTITIOUS) != 0 ? &pv_dummy :
6396             pa_to_pvh(VM_PAGE_TO_PHYS(m));
6397         lock = VM_PAGE_TO_PV_LIST_LOCK(m);
6398         rw_wlock(lock);
6399 restart:
6400         TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_next, next_pv) {
6401                 pmap = PV_PMAP(pv);
6402                 if (!PMAP_TRYLOCK(pmap)) {
6403                         pvh_gen = pvh->pv_gen;
6404                         rw_wunlock(lock);
6405                         PMAP_LOCK(pmap);
6406                         rw_wlock(lock);
6407                         if (pvh_gen != pvh->pv_gen) {
6408                                 PMAP_UNLOCK(pmap);
6409                                 goto restart;
6410                         }
6411                 }
6412                 PG_M = pmap_modified_bit(pmap);
6413                 PG_V = pmap_valid_bit(pmap);
6414                 PG_RW = pmap_rw_bit(pmap);
6415                 va = pv->pv_va;
6416                 pde = pmap_pde(pmap, va);
6417                 oldpde = *pde;
6418                 if ((oldpde & PG_RW) != 0) {
6419                         if (pmap_demote_pde_locked(pmap, pde, va, &lock)) {
6420                                 if ((oldpde & PG_W) == 0) {
6421                                         /*
6422                                          * Write protect the mapping to a
6423                                          * single page so that a subsequent
6424                                          * write access may repromote.
6425                                          */
6426                                         va += VM_PAGE_TO_PHYS(m) - (oldpde &
6427                                             PG_PS_FRAME);
6428                                         pte = pmap_pde_to_pte(pde, va);
6429                                         oldpte = *pte;
6430                                         if ((oldpte & PG_V) != 0) {
6431                                                 while (!atomic_cmpset_long(pte,
6432                                                     oldpte,
6433                                                     oldpte & ~(PG_M | PG_RW)))
6434                                                         oldpte = *pte;
6435                                                 vm_page_dirty(m);
6436                                                 pmap_invalidate_page(pmap, va);
6437                                         }
6438                                 }
6439                         }
6440                 }
6441                 PMAP_UNLOCK(pmap);
6442         }
6443         TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
6444                 pmap = PV_PMAP(pv);
6445                 if (!PMAP_TRYLOCK(pmap)) {
6446                         md_gen = m->md.pv_gen;
6447                         pvh_gen = pvh->pv_gen;
6448                         rw_wunlock(lock);
6449                         PMAP_LOCK(pmap);
6450                         rw_wlock(lock);
6451                         if (pvh_gen != pvh->pv_gen || md_gen != m->md.pv_gen) {
6452                                 PMAP_UNLOCK(pmap);
6453                                 goto restart;
6454                         }
6455                 }
6456                 PG_M = pmap_modified_bit(pmap);
6457                 PG_RW = pmap_rw_bit(pmap);
6458                 pde = pmap_pde(pmap, pv->pv_va);
6459                 KASSERT((*pde & PG_PS) == 0, ("pmap_clear_modify: found"
6460                     " a 2mpage in page %p's pv list", m));
6461                 pte = pmap_pde_to_pte(pde, pv->pv_va);
6462                 if ((*pte & (PG_M | PG_RW)) == (PG_M | PG_RW)) {
6463                         atomic_clear_long(pte, PG_M);
6464                         pmap_invalidate_page(pmap, pv->pv_va);
6465                 }
6466                 PMAP_UNLOCK(pmap);
6467         }
6468         rw_wunlock(lock);
6469 }
6470
6471 /*
6472  * Miscellaneous support routines follow
6473  */
6474
6475 /* Adjust the cache mode for a 4KB page mapped via a PTE. */
6476 static __inline void
6477 pmap_pte_attr(pt_entry_t *pte, int cache_bits, int mask)
6478 {
6479         u_int opte, npte;
6480
6481         /*
6482          * The cache mode bits are all in the low 32-bits of the
6483          * PTE, so we can just spin on updating the low 32-bits.
6484          */
6485         do {
6486                 opte = *(u_int *)pte;
6487                 npte = opte & ~mask;
6488                 npte |= cache_bits;
6489         } while (npte != opte && !atomic_cmpset_int((u_int *)pte, opte, npte));
6490 }
6491
6492 /* Adjust the cache mode for a 2MB page mapped via a PDE. */
6493 static __inline void
6494 pmap_pde_attr(pd_entry_t *pde, int cache_bits, int mask)
6495 {
6496         u_int opde, npde;
6497
6498         /*
6499          * The cache mode bits are all in the low 32-bits of the
6500          * PDE, so we can just spin on updating the low 32-bits.
6501          */
6502         do {
6503                 opde = *(u_int *)pde;
6504                 npde = opde & ~mask;
6505                 npde |= cache_bits;
6506         } while (npde != opde && !atomic_cmpset_int((u_int *)pde, opde, npde));
6507 }
6508
6509 /*
6510  * Map a set of physical memory pages into the kernel virtual
6511  * address space. Return a pointer to where it is mapped. This
6512  * routine is intended to be used for mapping device memory,
6513  * NOT real memory.
6514  */
6515 void *
6516 pmap_mapdev_attr(vm_paddr_t pa, vm_size_t size, int mode)
6517 {
6518         struct pmap_preinit_mapping *ppim;
6519         vm_offset_t va, offset;
6520         vm_size_t tmpsize;
6521         int i;
6522
6523         offset = pa & PAGE_MASK;
6524         size = round_page(offset + size);
6525         pa = trunc_page(pa);
6526
6527         if (!pmap_initialized) {
6528                 va = 0;
6529                 for (i = 0; i < PMAP_PREINIT_MAPPING_COUNT; i++) {
6530                         ppim = pmap_preinit_mapping + i;
6531                         if (ppim->va == 0) {
6532                                 ppim->pa = pa;
6533                                 ppim->sz = size;
6534                                 ppim->mode = mode;
6535                                 ppim->va = virtual_avail;
6536                                 virtual_avail += size;
6537                                 va = ppim->va;
6538                                 break;
6539                         }
6540                 }
6541                 if (va == 0)
6542                         panic("%s: too many preinit mappings", __func__);
6543         } else {
6544                 /*
6545                  * If we have a preinit mapping, re-use it.
6546                  */
6547                 for (i = 0; i < PMAP_PREINIT_MAPPING_COUNT; i++) {
6548                         ppim = pmap_preinit_mapping + i;
6549                         if (ppim->pa == pa && ppim->sz == size &&
6550                             ppim->mode == mode)
6551                                 return ((void *)(ppim->va + offset));
6552                 }
6553                 /*
6554                  * If the specified range of physical addresses fits within
6555                  * the direct map window, use the direct map.
6556                  */
6557                 if (pa < dmaplimit && pa + size < dmaplimit) {
6558                         va = PHYS_TO_DMAP(pa);
6559                         if (!pmap_change_attr(va, size, mode))
6560                                 return ((void *)(va + offset));
6561                 }
6562                 va = kva_alloc(size);
6563                 if (va == 0)
6564                         panic("%s: Couldn't allocate KVA", __func__);
6565         }
6566         for (tmpsize = 0; tmpsize < size; tmpsize += PAGE_SIZE)
6567                 pmap_kenter_attr(va + tmpsize, pa + tmpsize, mode);
6568         pmap_invalidate_range(kernel_pmap, va, va + tmpsize);
6569         pmap_invalidate_cache_range(va, va + tmpsize, FALSE);
6570         return ((void *)(va + offset));
6571 }
6572
6573 void *
6574 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
6575 {
6576
6577         return (pmap_mapdev_attr(pa, size, PAT_UNCACHEABLE));
6578 }
6579
6580 void *
6581 pmap_mapbios(vm_paddr_t pa, vm_size_t size)
6582 {
6583
6584         return (pmap_mapdev_attr(pa, size, PAT_WRITE_BACK));
6585 }
6586
6587 void
6588 pmap_unmapdev(vm_offset_t va, vm_size_t size)
6589 {
6590         struct pmap_preinit_mapping *ppim;
6591         vm_offset_t offset;
6592         int i;
6593
6594         /* If we gave a direct map region in pmap_mapdev, do nothing */
6595         if (va >= DMAP_MIN_ADDRESS && va < DMAP_MAX_ADDRESS)
6596                 return;
6597         offset = va & PAGE_MASK;
6598         size = round_page(offset + size);
6599         va = trunc_page(va);
6600         for (i = 0; i < PMAP_PREINIT_MAPPING_COUNT; i++) {
6601                 ppim = pmap_preinit_mapping + i;
6602                 if (ppim->va == va && ppim->sz == size) {
6603                         if (pmap_initialized)
6604                                 return;
6605                         ppim->pa = 0;
6606                         ppim->va = 0;
6607                         ppim->sz = 0;
6608                         ppim->mode = 0;
6609                         if (va + size == virtual_avail)
6610                                 virtual_avail = va;
6611                         return;
6612                 }
6613         }
6614         if (pmap_initialized)
6615                 kva_free(va, size);
6616 }
6617
6618 /*
6619  * Tries to demote a 1GB page mapping.
6620  */
6621 static boolean_t
6622 pmap_demote_pdpe(pmap_t pmap, pdp_entry_t *pdpe, vm_offset_t va)
6623 {
6624         pdp_entry_t newpdpe, oldpdpe;
6625         pd_entry_t *firstpde, newpde, *pde;
6626         pt_entry_t PG_A, PG_M, PG_RW, PG_V;
6627         vm_paddr_t pdpgpa;
6628         vm_page_t pdpg;
6629
6630         PG_A = pmap_accessed_bit(pmap);
6631         PG_M = pmap_modified_bit(pmap);
6632         PG_V = pmap_valid_bit(pmap);
6633         PG_RW = pmap_rw_bit(pmap);
6634
6635         PMAP_LOCK_ASSERT(pmap, MA_OWNED);
6636         oldpdpe = *pdpe;
6637         KASSERT((oldpdpe & (PG_PS | PG_V)) == (PG_PS | PG_V),
6638             ("pmap_demote_pdpe: oldpdpe is missing PG_PS and/or PG_V"));
6639         if ((pdpg = vm_page_alloc(NULL, va >> PDPSHIFT, VM_ALLOC_INTERRUPT |
6640             VM_ALLOC_NOOBJ | VM_ALLOC_WIRED)) == NULL) {
6641                 CTR2(KTR_PMAP, "pmap_demote_pdpe: failure for va %#lx"
6642                     " in pmap %p", va, pmap);
6643                 return (FALSE);
6644         }
6645         pdpgpa = VM_PAGE_TO_PHYS(pdpg);
6646         firstpde = (pd_entry_t *)PHYS_TO_DMAP(pdpgpa);
6647         newpdpe = pdpgpa | PG_M | PG_A | (oldpdpe & PG_U) | PG_RW | PG_V;
6648         KASSERT((oldpdpe & PG_A) != 0,
6649             ("pmap_demote_pdpe: oldpdpe is missing PG_A"));
6650         KASSERT((oldpdpe & (PG_M | PG_RW)) != PG_RW,
6651             ("pmap_demote_pdpe: oldpdpe is missing PG_M"));
6652         newpde = oldpdpe;
6653
6654         /*
6655          * Initialize the page directory page.
6656          */
6657         for (pde = firstpde; pde < firstpde + NPDEPG; pde++) {
6658                 *pde = newpde;
6659                 newpde += NBPDR;
6660         }
6661
6662         /*
6663          * Demote the mapping.
6664          */
6665         *pdpe = newpdpe;
6666
6667         /*
6668          * Invalidate a stale recursive mapping of the page directory page.
6669          */
6670         pmap_invalidate_page(pmap, (vm_offset_t)vtopde(va));
6671
6672         pmap_pdpe_demotions++;
6673         CTR2(KTR_PMAP, "pmap_demote_pdpe: success for va %#lx"
6674             " in pmap %p", va, pmap);
6675         return (TRUE);
6676 }
6677
6678 /*
6679  * Sets the memory attribute for the specified page.
6680  */
6681 void
6682 pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma)
6683 {
6684
6685         m->md.pat_mode = ma;
6686
6687         /*
6688          * If "m" is a normal page, update its direct mapping.  This update
6689          * can be relied upon to perform any cache operations that are
6690          * required for data coherence.
6691          */
6692         if ((m->flags & PG_FICTITIOUS) == 0 &&
6693             pmap_change_attr(PHYS_TO_DMAP(VM_PAGE_TO_PHYS(m)), PAGE_SIZE,
6694             m->md.pat_mode))
6695                 panic("memory attribute change on the direct map failed");
6696 }
6697
6698 /*
6699  * Changes the specified virtual address range's memory type to that given by
6700  * the parameter "mode".  The specified virtual address range must be
6701  * completely contained within either the direct map or the kernel map.  If
6702  * the virtual address range is contained within the kernel map, then the
6703  * memory type for each of the corresponding ranges of the direct map is also
6704  * changed.  (The corresponding ranges of the direct map are those ranges that
6705  * map the same physical pages as the specified virtual address range.)  These
6706  * changes to the direct map are necessary because Intel describes the
6707  * behavior of their processors as "undefined" if two or more mappings to the
6708  * same physical page have different memory types.
6709  *
6710  * Returns zero if the change completed successfully, and either EINVAL or
6711  * ENOMEM if the change failed.  Specifically, EINVAL is returned if some part
6712  * of the virtual address range was not mapped, and ENOMEM is returned if
6713  * there was insufficient memory available to complete the change.  In the
6714  * latter case, the memory type may have been changed on some part of the
6715  * virtual address range or the direct map.
6716  */
6717 int
6718 pmap_change_attr(vm_offset_t va, vm_size_t size, int mode)
6719 {
6720         int error;
6721
6722         PMAP_LOCK(kernel_pmap);
6723         error = pmap_change_attr_locked(va, size, mode);
6724         PMAP_UNLOCK(kernel_pmap);
6725         return (error);
6726 }
6727
6728 static int
6729 pmap_change_attr_locked(vm_offset_t va, vm_size_t size, int mode)
6730 {
6731         vm_offset_t base, offset, tmpva;
6732         vm_paddr_t pa_start, pa_end, pa_end1;
6733         pdp_entry_t *pdpe;
6734         pd_entry_t *pde;
6735         pt_entry_t *pte;
6736         int cache_bits_pte, cache_bits_pde, error;
6737         boolean_t changed;
6738
6739         PMAP_LOCK_ASSERT(kernel_pmap, MA_OWNED);
6740         base = trunc_page(va);
6741         offset = va & PAGE_MASK;
6742         size = round_page(offset + size);
6743
6744         /*
6745          * Only supported on kernel virtual addresses, including the direct
6746          * map but excluding the recursive map.
6747          */
6748         if (base < DMAP_MIN_ADDRESS)
6749                 return (EINVAL);
6750
6751         cache_bits_pde = pmap_cache_bits(kernel_pmap, mode, 1);
6752         cache_bits_pte = pmap_cache_bits(kernel_pmap, mode, 0);
6753         changed = FALSE;
6754
6755         /*
6756          * Pages that aren't mapped aren't supported.  Also break down 2MB pages
6757          * into 4KB pages if required.
6758          */
6759         for (tmpva = base; tmpva < base + size; ) {
6760                 pdpe = pmap_pdpe(kernel_pmap, tmpva);
6761                 if (pdpe == NULL || *pdpe == 0)
6762                         return (EINVAL);
6763                 if (*pdpe & PG_PS) {
6764                         /*
6765                          * If the current 1GB page already has the required
6766                          * memory type, then we need not demote this page. Just
6767                          * increment tmpva to the next 1GB page frame.
6768                          */
6769                         if ((*pdpe & X86_PG_PDE_CACHE) == cache_bits_pde) {
6770                                 tmpva = trunc_1gpage(tmpva) + NBPDP;
6771                                 continue;
6772                         }
6773
6774                         /*
6775                          * If the current offset aligns with a 1GB page frame
6776                          * and there is at least 1GB left within the range, then
6777                          * we need not break down this page into 2MB pages.
6778                          */
6779                         if ((tmpva & PDPMASK) == 0 &&
6780                             tmpva + PDPMASK < base + size) {
6781                                 tmpva += NBPDP;
6782                                 continue;
6783                         }
6784                         if (!pmap_demote_pdpe(kernel_pmap, pdpe, tmpva))
6785                                 return (ENOMEM);
6786                 }
6787                 pde = pmap_pdpe_to_pde(pdpe, tmpva);
6788                 if (*pde == 0)
6789                         return (EINVAL);
6790                 if (*pde & PG_PS) {
6791                         /*
6792                          * If the current 2MB page already has the required
6793                          * memory type, then we need not demote this page. Just
6794                          * increment tmpva to the next 2MB page frame.
6795                          */
6796                         if ((*pde & X86_PG_PDE_CACHE) == cache_bits_pde) {
6797                                 tmpva = trunc_2mpage(tmpva) + NBPDR;
6798                                 continue;
6799                         }
6800
6801                         /*
6802                          * If the current offset aligns with a 2MB page frame
6803                          * and there is at least 2MB left within the range, then
6804                          * we need not break down this page into 4KB pages.
6805                          */
6806                         if ((tmpva & PDRMASK) == 0 &&
6807                             tmpva + PDRMASK < base + size) {
6808                                 tmpva += NBPDR;
6809                                 continue;
6810                         }
6811                         if (!pmap_demote_pde(kernel_pmap, pde, tmpva))
6812                                 return (ENOMEM);
6813                 }
6814                 pte = pmap_pde_to_pte(pde, tmpva);
6815                 if (*pte == 0)
6816                         return (EINVAL);
6817                 tmpva += PAGE_SIZE;
6818         }
6819         error = 0;
6820
6821         /*
6822          * Ok, all the pages exist, so run through them updating their
6823          * cache mode if required.
6824          */
6825         pa_start = pa_end = 0;
6826         for (tmpva = base; tmpva < base + size; ) {
6827                 pdpe = pmap_pdpe(kernel_pmap, tmpva);
6828                 if (*pdpe & PG_PS) {
6829                         if ((*pdpe & X86_PG_PDE_CACHE) != cache_bits_pde) {
6830                                 pmap_pde_attr(pdpe, cache_bits_pde,
6831                                     X86_PG_PDE_CACHE);
6832                                 changed = TRUE;
6833                         }
6834                         if (tmpva >= VM_MIN_KERNEL_ADDRESS &&
6835                             (*pdpe & PG_PS_FRAME) < dmaplimit) {
6836                                 if (pa_start == pa_end) {
6837                                         /* Start physical address run. */
6838                                         pa_start = *pdpe & PG_PS_FRAME;
6839                                         pa_end = pa_start + NBPDP;
6840                                 } else if (pa_end == (*pdpe & PG_PS_FRAME))
6841                                         pa_end += NBPDP;
6842                                 else {
6843                                         /* Run ended, update direct map. */
6844                                         error = pmap_change_attr_locked(
6845                                             PHYS_TO_DMAP(pa_start),
6846                                             pa_end - pa_start, mode);
6847                                         if (error != 0)
6848                                                 break;
6849                                         /* Start physical address run. */
6850                                         pa_start = *pdpe & PG_PS_FRAME;
6851                                         pa_end = pa_start + NBPDP;
6852                                 }
6853                         }
6854                         tmpva = trunc_1gpage(tmpva) + NBPDP;
6855                         continue;
6856                 }
6857                 pde = pmap_pdpe_to_pde(pdpe, tmpva);
6858                 if (*pde & PG_PS) {
6859                         if ((*pde & X86_PG_PDE_CACHE) != cache_bits_pde) {
6860                                 pmap_pde_attr(pde, cache_bits_pde,
6861                                     X86_PG_PDE_CACHE);
6862                                 changed = TRUE;
6863                         }
6864                         if (tmpva >= VM_MIN_KERNEL_ADDRESS &&
6865                             (*pde & PG_PS_FRAME) < dmaplimit) {
6866                                 if (pa_start == pa_end) {
6867                                         /* Start physical address run. */
6868                                         pa_start = *pde & PG_PS_FRAME;
6869                                         pa_end = pa_start + NBPDR;
6870                                 } else if (pa_end == (*pde & PG_PS_FRAME))
6871                                         pa_end += NBPDR;
6872                                 else {
6873                                         /* Run ended, update direct map. */
6874                                         error = pmap_change_attr_locked(
6875                                             PHYS_TO_DMAP(pa_start),
6876                                             pa_end - pa_start, mode);
6877                                         if (error != 0)
6878                                                 break;
6879                                         /* Start physical address run. */
6880                                         pa_start = *pde & PG_PS_FRAME;
6881                                         pa_end = pa_start + NBPDR;
6882                                 }
6883                         }
6884                         tmpva = trunc_2mpage(tmpva) + NBPDR;
6885                 } else {
6886                         pte = pmap_pde_to_pte(pde, tmpva);
6887                         if ((*pte & X86_PG_PTE_CACHE) != cache_bits_pte) {
6888                                 pmap_pte_attr(pte, cache_bits_pte,
6889                                     X86_PG_PTE_CACHE);
6890                                 changed = TRUE;
6891                         }
6892                         if (tmpva >= VM_MIN_KERNEL_ADDRESS &&
6893                             (*pte & PG_FRAME) < dmaplimit) {
6894                                 if (pa_start == pa_end) {
6895                                         /* Start physical address run. */
6896                                         pa_start = *pte & PG_FRAME;
6897                                         pa_end = pa_start + PAGE_SIZE;
6898                                 } else if (pa_end == (*pte & PG_FRAME))
6899                                         pa_end += PAGE_SIZE;
6900                                 else {
6901                                         /* Run ended, update direct map. */
6902                                         error = pmap_change_attr_locked(
6903                                             PHYS_TO_DMAP(pa_start),
6904                                             pa_end - pa_start, mode);
6905                                         if (error != 0)
6906                                                 break;
6907                                         /* Start physical address run. */
6908                                         pa_start = *pte & PG_FRAME;
6909                                         pa_end = pa_start + PAGE_SIZE;
6910                                 }
6911                         }
6912                         tmpva += PAGE_SIZE;
6913                 }
6914         }
6915         if (error == 0 && pa_start != pa_end && pa_start < dmaplimit) {
6916                 pa_end1 = MIN(pa_end, dmaplimit);
6917                 if (pa_start != pa_end1)
6918                         error = pmap_change_attr_locked(PHYS_TO_DMAP(pa_start),
6919                             pa_end1 - pa_start, mode);
6920         }
6921
6922         /*
6923          * Flush CPU caches if required to make sure any data isn't cached that
6924          * shouldn't be, etc.
6925          */
6926         if (changed) {
6927                 pmap_invalidate_range(kernel_pmap, base, tmpva);
6928                 pmap_invalidate_cache_range(base, tmpva, FALSE);
6929         }
6930         return (error);
6931 }
6932
6933 /*
6934  * Demotes any mapping within the direct map region that covers more than the
6935  * specified range of physical addresses.  This range's size must be a power
6936  * of two and its starting address must be a multiple of its size.  Since the
6937  * demotion does not change any attributes of the mapping, a TLB invalidation
6938  * is not mandatory.  The caller may, however, request a TLB invalidation.
6939  */
6940 void
6941 pmap_demote_DMAP(vm_paddr_t base, vm_size_t len, boolean_t invalidate)
6942 {
6943         pdp_entry_t *pdpe;
6944         pd_entry_t *pde;
6945         vm_offset_t va;
6946         boolean_t changed;
6947
6948         if (len == 0)
6949                 return;
6950         KASSERT(powerof2(len), ("pmap_demote_DMAP: len is not a power of 2"));
6951         KASSERT((base & (len - 1)) == 0,
6952             ("pmap_demote_DMAP: base is not a multiple of len"));
6953         if (len < NBPDP && base < dmaplimit) {
6954                 va = PHYS_TO_DMAP(base);
6955                 changed = FALSE;
6956                 PMAP_LOCK(kernel_pmap);
6957                 pdpe = pmap_pdpe(kernel_pmap, va);
6958                 if ((*pdpe & X86_PG_V) == 0)
6959                         panic("pmap_demote_DMAP: invalid PDPE");
6960                 if ((*pdpe & PG_PS) != 0) {
6961                         if (!pmap_demote_pdpe(kernel_pmap, pdpe, va))
6962                                 panic("pmap_demote_DMAP: PDPE failed");
6963                         changed = TRUE;
6964                 }
6965                 if (len < NBPDR) {
6966                         pde = pmap_pdpe_to_pde(pdpe, va);
6967                         if ((*pde & X86_PG_V) == 0)
6968                                 panic("pmap_demote_DMAP: invalid PDE");
6969                         if ((*pde & PG_PS) != 0) {
6970                                 if (!pmap_demote_pde(kernel_pmap, pde, va))
6971                                         panic("pmap_demote_DMAP: PDE failed");
6972                                 changed = TRUE;
6973                         }
6974                 }
6975                 if (changed && invalidate)
6976                         pmap_invalidate_page(kernel_pmap, va);
6977                 PMAP_UNLOCK(kernel_pmap);
6978         }
6979 }
6980
6981 /*
6982  * perform the pmap work for mincore
6983  */
6984 int
6985 pmap_mincore(pmap_t pmap, vm_offset_t addr, vm_paddr_t *locked_pa)
6986 {
6987         pd_entry_t *pdep;
6988         pt_entry_t pte, PG_A, PG_M, PG_RW, PG_V;
6989         vm_paddr_t pa;
6990         int val;
6991
6992         PG_A = pmap_accessed_bit(pmap);
6993         PG_M = pmap_modified_bit(pmap);
6994         PG_V = pmap_valid_bit(pmap);
6995         PG_RW = pmap_rw_bit(pmap);
6996
6997         PMAP_LOCK(pmap);
6998 retry:
6999         pdep = pmap_pde(pmap, addr);
7000         if (pdep != NULL && (*pdep & PG_V)) {
7001                 if (*pdep & PG_PS) {
7002                         pte = *pdep;
7003                         /* Compute the physical address of the 4KB page. */
7004                         pa = ((*pdep & PG_PS_FRAME) | (addr & PDRMASK)) &
7005                             PG_FRAME;
7006                         val = MINCORE_SUPER;
7007                 } else {
7008                         pte = *pmap_pde_to_pte(pdep, addr);
7009                         pa = pte & PG_FRAME;
7010                         val = 0;
7011                 }
7012         } else {
7013                 pte = 0;
7014                 pa = 0;
7015                 val = 0;
7016         }
7017         if ((pte & PG_V) != 0) {
7018                 val |= MINCORE_INCORE;
7019                 if ((pte & (PG_M | PG_RW)) == (PG_M | PG_RW))
7020                         val |= MINCORE_MODIFIED | MINCORE_MODIFIED_OTHER;
7021                 if ((pte & PG_A) != 0)
7022                         val |= MINCORE_REFERENCED | MINCORE_REFERENCED_OTHER;
7023         }
7024         if ((val & (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER)) !=
7025             (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER) &&
7026             (pte & (PG_MANAGED | PG_V)) == (PG_MANAGED | PG_V)) {
7027                 /* Ensure that "PHYS_TO_VM_PAGE(pa)->object" doesn't change. */
7028                 if (vm_page_pa_tryrelock(pmap, pa, locked_pa))
7029                         goto retry;
7030         } else
7031                 PA_UNLOCK_COND(*locked_pa);
7032         PMAP_UNLOCK(pmap);
7033         return (val);
7034 }
7035
7036 static uint64_t
7037 pmap_pcid_alloc(pmap_t pmap, u_int cpuid)
7038 {
7039         uint32_t gen, new_gen, pcid_next;
7040
7041         CRITICAL_ASSERT(curthread);
7042         gen = PCPU_GET(pcid_gen);
7043         if (pmap->pm_pcids[cpuid].pm_pcid == PMAP_PCID_KERN ||
7044             pmap->pm_pcids[cpuid].pm_gen == gen)
7045                 return (CR3_PCID_SAVE);
7046         pcid_next = PCPU_GET(pcid_next);
7047         KASSERT(pcid_next <= PMAP_PCID_OVERMAX, ("cpu %d pcid_next %#x",
7048             cpuid, pcid_next));
7049         if (pcid_next == PMAP_PCID_OVERMAX) {
7050                 new_gen = gen + 1;
7051                 if (new_gen == 0)
7052                         new_gen = 1;
7053                 PCPU_SET(pcid_gen, new_gen);
7054                 pcid_next = PMAP_PCID_KERN + 1;
7055         } else {
7056                 new_gen = gen;
7057         }
7058         pmap->pm_pcids[cpuid].pm_pcid = pcid_next;
7059         pmap->pm_pcids[cpuid].pm_gen = new_gen;
7060         PCPU_SET(pcid_next, pcid_next + 1);
7061         return (0);
7062 }
7063
7064 void
7065 pmap_activate_sw(struct thread *td)
7066 {
7067         pmap_t oldpmap, pmap;
7068         uint64_t cached, cr3;
7069         register_t rflags;
7070         u_int cpuid;
7071
7072         oldpmap = PCPU_GET(curpmap);
7073         pmap = vmspace_pmap(td->td_proc->p_vmspace);
7074         if (oldpmap == pmap)
7075                 return;
7076         cpuid = PCPU_GET(cpuid);
7077 #ifdef SMP
7078         CPU_SET_ATOMIC(cpuid, &pmap->pm_active);
7079 #else
7080         CPU_SET(cpuid, &pmap->pm_active);
7081 #endif
7082         cr3 = rcr3();
7083         if (pmap_pcid_enabled) {
7084                 cached = pmap_pcid_alloc(pmap, cpuid);
7085                 KASSERT(pmap->pm_pcids[cpuid].pm_pcid >= 0 &&
7086                     pmap->pm_pcids[cpuid].pm_pcid < PMAP_PCID_OVERMAX,
7087                     ("pmap %p cpu %d pcid %#x", pmap, cpuid,
7088                     pmap->pm_pcids[cpuid].pm_pcid));
7089                 KASSERT(pmap->pm_pcids[cpuid].pm_pcid != PMAP_PCID_KERN ||
7090                     pmap == kernel_pmap,
7091                     ("non-kernel pmap thread %p pmap %p cpu %d pcid %#x",
7092                     td, pmap, cpuid, pmap->pm_pcids[cpuid].pm_pcid));
7093
7094                 /*
7095                  * If the INVPCID instruction is not available,
7096                  * invltlb_pcid_handler() is used for handle
7097                  * invalidate_all IPI, which checks for curpmap ==
7098                  * smp_tlb_pmap.  Below operations sequence has a
7099                  * window where %CR3 is loaded with the new pmap's
7100                  * PML4 address, but curpmap value is not yet updated.
7101                  * This causes invltlb IPI handler, called between the
7102                  * updates, to execute as NOP, which leaves stale TLB
7103                  * entries.
7104                  *
7105                  * Note that the most typical use of
7106                  * pmap_activate_sw(), from the context switch, is
7107                  * immune to this race, because interrupts are
7108                  * disabled (while the thread lock is owned), and IPI
7109                  * happends after curpmap is updated.  Protect other
7110                  * callers in a similar way, by disabling interrupts
7111                  * around the %cr3 register reload and curpmap
7112                  * assignment.
7113                  */
7114                 if (!invpcid_works)
7115                         rflags = intr_disable();
7116
7117                 if (!cached || (cr3 & ~CR3_PCID_MASK) != pmap->pm_cr3) {
7118                         load_cr3(pmap->pm_cr3 | pmap->pm_pcids[cpuid].pm_pcid |
7119                             cached);
7120                         if (cached)
7121                                 PCPU_INC(pm_save_cnt);
7122                 }
7123                 PCPU_SET(curpmap, pmap);
7124                 if (!invpcid_works)
7125                         intr_restore(rflags);
7126         } else if (cr3 != pmap->pm_cr3) {
7127                 load_cr3(pmap->pm_cr3);
7128                 PCPU_SET(curpmap, pmap);
7129         }
7130 #ifdef SMP
7131         CPU_CLR_ATOMIC(cpuid, &oldpmap->pm_active);
7132 #else
7133         CPU_CLR(cpuid, &oldpmap->pm_active);
7134 #endif
7135 }
7136
7137 void
7138 pmap_activate(struct thread *td)
7139 {
7140
7141         critical_enter();
7142         pmap_activate_sw(td);
7143         critical_exit();
7144 }
7145
7146 void
7147 pmap_sync_icache(pmap_t pm, vm_offset_t va, vm_size_t sz)
7148 {
7149 }
7150
7151 /*
7152  *      Increase the starting virtual address of the given mapping if a
7153  *      different alignment might result in more superpage mappings.
7154  */
7155 void
7156 pmap_align_superpage(vm_object_t object, vm_ooffset_t offset,
7157     vm_offset_t *addr, vm_size_t size)
7158 {
7159         vm_offset_t superpage_offset;
7160
7161         if (size < NBPDR)
7162                 return;
7163         if (object != NULL && (object->flags & OBJ_COLORED) != 0)
7164                 offset += ptoa(object->pg_color);
7165         superpage_offset = offset & PDRMASK;
7166         if (size - ((NBPDR - superpage_offset) & PDRMASK) < NBPDR ||
7167             (*addr & PDRMASK) == superpage_offset)
7168                 return;
7169         if ((*addr & PDRMASK) < superpage_offset)
7170                 *addr = (*addr & ~PDRMASK) + superpage_offset;
7171         else
7172                 *addr = ((*addr + PDRMASK) & ~PDRMASK) + superpage_offset;
7173 }
7174
7175 #ifdef INVARIANTS
7176 static unsigned long num_dirty_emulations;
7177 SYSCTL_ULONG(_vm_pmap, OID_AUTO, num_dirty_emulations, CTLFLAG_RW,
7178              &num_dirty_emulations, 0, NULL);
7179
7180 static unsigned long num_accessed_emulations;
7181 SYSCTL_ULONG(_vm_pmap, OID_AUTO, num_accessed_emulations, CTLFLAG_RW,
7182              &num_accessed_emulations, 0, NULL);
7183
7184 static unsigned long num_superpage_accessed_emulations;
7185 SYSCTL_ULONG(_vm_pmap, OID_AUTO, num_superpage_accessed_emulations, CTLFLAG_RW,
7186              &num_superpage_accessed_emulations, 0, NULL);
7187
7188 static unsigned long ad_emulation_superpage_promotions;
7189 SYSCTL_ULONG(_vm_pmap, OID_AUTO, ad_emulation_superpage_promotions, CTLFLAG_RW,
7190              &ad_emulation_superpage_promotions, 0, NULL);
7191 #endif  /* INVARIANTS */
7192
7193 int
7194 pmap_emulate_accessed_dirty(pmap_t pmap, vm_offset_t va, int ftype)
7195 {
7196         int rv;
7197         struct rwlock *lock;
7198         vm_page_t m, mpte;
7199         pd_entry_t *pde;
7200         pt_entry_t *pte, PG_A, PG_M, PG_RW, PG_V;
7201
7202         KASSERT(ftype == VM_PROT_READ || ftype == VM_PROT_WRITE,
7203             ("pmap_emulate_accessed_dirty: invalid fault type %d", ftype));
7204
7205         if (!pmap_emulate_ad_bits(pmap))
7206                 return (-1);
7207
7208         PG_A = pmap_accessed_bit(pmap);
7209         PG_M = pmap_modified_bit(pmap);
7210         PG_V = pmap_valid_bit(pmap);
7211         PG_RW = pmap_rw_bit(pmap);
7212
7213         rv = -1;
7214         lock = NULL;
7215         PMAP_LOCK(pmap);
7216
7217         pde = pmap_pde(pmap, va);
7218         if (pde == NULL || (*pde & PG_V) == 0)
7219                 goto done;
7220
7221         if ((*pde & PG_PS) != 0) {
7222                 if (ftype == VM_PROT_READ) {
7223 #ifdef INVARIANTS
7224                         atomic_add_long(&num_superpage_accessed_emulations, 1);
7225 #endif
7226                         *pde |= PG_A;
7227                         rv = 0;
7228                 }
7229                 goto done;
7230         }
7231
7232         pte = pmap_pde_to_pte(pde, va);
7233         if ((*pte & PG_V) == 0)
7234                 goto done;
7235
7236         if (ftype == VM_PROT_WRITE) {
7237                 if ((*pte & PG_RW) == 0)
7238                         goto done;
7239                 /*
7240                  * Set the modified and accessed bits simultaneously.
7241                  *
7242                  * Intel EPT PTEs that do software emulation of A/D bits map
7243                  * PG_A and PG_M to EPT_PG_READ and EPT_PG_WRITE respectively.
7244                  * An EPT misconfiguration is triggered if the PTE is writable
7245                  * but not readable (WR=10). This is avoided by setting PG_A
7246                  * and PG_M simultaneously.
7247                  */
7248                 *pte |= PG_M | PG_A;
7249         } else {
7250                 *pte |= PG_A;
7251         }
7252
7253         /* try to promote the mapping */
7254         if (va < VM_MAXUSER_ADDRESS)
7255                 mpte = PHYS_TO_VM_PAGE(*pde & PG_FRAME);
7256         else
7257                 mpte = NULL;
7258
7259         m = PHYS_TO_VM_PAGE(*pte & PG_FRAME);
7260
7261         if ((mpte == NULL || mpte->wire_count == NPTEPG) &&
7262             pmap_ps_enabled(pmap) &&
7263             (m->flags & PG_FICTITIOUS) == 0 &&
7264             vm_reserv_level_iffullpop(m) == 0) {
7265                 pmap_promote_pde(pmap, pde, va, &lock);
7266 #ifdef INVARIANTS
7267                 atomic_add_long(&ad_emulation_superpage_promotions, 1);
7268 #endif
7269         }
7270 #ifdef INVARIANTS
7271         if (ftype == VM_PROT_WRITE)
7272                 atomic_add_long(&num_dirty_emulations, 1);
7273         else
7274                 atomic_add_long(&num_accessed_emulations, 1);
7275 #endif
7276         rv = 0;         /* success */
7277 done:
7278         if (lock != NULL)
7279                 rw_wunlock(lock);
7280         PMAP_UNLOCK(pmap);
7281         return (rv);
7282 }
7283
7284 void
7285 pmap_get_mapping(pmap_t pmap, vm_offset_t va, uint64_t *ptr, int *num)
7286 {
7287         pml4_entry_t *pml4;
7288         pdp_entry_t *pdp;
7289         pd_entry_t *pde;
7290         pt_entry_t *pte, PG_V;
7291         int idx;
7292
7293         idx = 0;
7294         PG_V = pmap_valid_bit(pmap);
7295         PMAP_LOCK(pmap);
7296
7297         pml4 = pmap_pml4e(pmap, va);
7298         ptr[idx++] = *pml4;
7299         if ((*pml4 & PG_V) == 0)
7300                 goto done;
7301
7302         pdp = pmap_pml4e_to_pdpe(pml4, va);
7303         ptr[idx++] = *pdp;
7304         if ((*pdp & PG_V) == 0 || (*pdp & PG_PS) != 0)
7305                 goto done;
7306
7307         pde = pmap_pdpe_to_pde(pdp, va);
7308         ptr[idx++] = *pde;
7309         if ((*pde & PG_V) == 0 || (*pde & PG_PS) != 0)
7310                 goto done;
7311
7312         pte = pmap_pde_to_pte(pde, va);
7313         ptr[idx++] = *pte;
7314
7315 done:
7316         PMAP_UNLOCK(pmap);
7317         *num = idx;
7318 }
7319
7320 /**
7321  * Get the kernel virtual address of a set of physical pages. If there are
7322  * physical addresses not covered by the DMAP perform a transient mapping
7323  * that will be removed when calling pmap_unmap_io_transient.
7324  *
7325  * \param page        The pages the caller wishes to obtain the virtual
7326  *                    address on the kernel memory map.
7327  * \param vaddr       On return contains the kernel virtual memory address
7328  *                    of the pages passed in the page parameter.
7329  * \param count       Number of pages passed in.
7330  * \param can_fault   TRUE if the thread using the mapped pages can take
7331  *                    page faults, FALSE otherwise.
7332  *
7333  * \returns TRUE if the caller must call pmap_unmap_io_transient when
7334  *          finished or FALSE otherwise.
7335  *
7336  */
7337 boolean_t
7338 pmap_map_io_transient(vm_page_t page[], vm_offset_t vaddr[], int count,
7339     boolean_t can_fault)
7340 {
7341         vm_paddr_t paddr;
7342         boolean_t needs_mapping;
7343         pt_entry_t *pte;
7344         int cache_bits, error, i;
7345
7346         /*
7347          * Allocate any KVA space that we need, this is done in a separate
7348          * loop to prevent calling vmem_alloc while pinned.
7349          */
7350         needs_mapping = FALSE;
7351         for (i = 0; i < count; i++) {
7352                 paddr = VM_PAGE_TO_PHYS(page[i]);
7353                 if (__predict_false(paddr >= dmaplimit)) {
7354                         error = vmem_alloc(kernel_arena, PAGE_SIZE,
7355                             M_BESTFIT | M_WAITOK, &vaddr[i]);
7356                         KASSERT(error == 0, ("vmem_alloc failed: %d", error));
7357                         needs_mapping = TRUE;
7358                 } else {
7359                         vaddr[i] = PHYS_TO_DMAP(paddr);
7360                 }
7361         }
7362
7363         /* Exit early if everything is covered by the DMAP */
7364         if (!needs_mapping)
7365                 return (FALSE);
7366
7367         /*
7368          * NB:  The sequence of updating a page table followed by accesses
7369          * to the corresponding pages used in the !DMAP case is subject to
7370          * the situation described in the "AMD64 Architecture Programmer's
7371          * Manual Volume 2: System Programming" rev. 3.23, "7.3.1 Special
7372          * Coherency Considerations".  Therefore, issuing the INVLPG right
7373          * after modifying the PTE bits is crucial.
7374          */
7375         if (!can_fault)
7376                 sched_pin();
7377         for (i = 0; i < count; i++) {
7378                 paddr = VM_PAGE_TO_PHYS(page[i]);
7379                 if (paddr >= dmaplimit) {
7380                         if (can_fault) {
7381                                 /*
7382                                  * Slow path, since we can get page faults
7383                                  * while mappings are active don't pin the
7384                                  * thread to the CPU and instead add a global
7385                                  * mapping visible to all CPUs.
7386                                  */
7387                                 pmap_qenter(vaddr[i], &page[i], 1);
7388                         } else {
7389                                 pte = vtopte(vaddr[i]);
7390                                 cache_bits = pmap_cache_bits(kernel_pmap,
7391                                     page[i]->md.pat_mode, 0);
7392                                 pte_store(pte, paddr | X86_PG_RW | X86_PG_V |
7393                                     cache_bits);
7394                                 invlpg(vaddr[i]);
7395                         }
7396                 }
7397         }
7398
7399         return (needs_mapping);
7400 }
7401
7402 void
7403 pmap_unmap_io_transient(vm_page_t page[], vm_offset_t vaddr[], int count,
7404     boolean_t can_fault)
7405 {
7406         vm_paddr_t paddr;
7407         int i;
7408
7409         if (!can_fault)
7410                 sched_unpin();
7411         for (i = 0; i < count; i++) {
7412                 paddr = VM_PAGE_TO_PHYS(page[i]);
7413                 if (paddr >= dmaplimit) {
7414                         if (can_fault)
7415                                 pmap_qremove(vaddr[i], 1);
7416                         vmem_free(kernel_arena, vaddr[i], PAGE_SIZE);
7417                 }
7418         }
7419 }
7420
7421 vm_offset_t
7422 pmap_quick_enter_page(vm_page_t m)
7423 {
7424         vm_paddr_t paddr;
7425
7426         paddr = VM_PAGE_TO_PHYS(m);
7427         if (paddr < dmaplimit)
7428                 return (PHYS_TO_DMAP(paddr));
7429         mtx_lock_spin(&qframe_mtx);
7430         KASSERT(*vtopte(qframe) == 0, ("qframe busy"));
7431         pte_store(vtopte(qframe), paddr | X86_PG_RW | X86_PG_V | X86_PG_A |
7432             X86_PG_M | pmap_cache_bits(kernel_pmap, m->md.pat_mode, 0));
7433         return (qframe);
7434 }
7435
7436 void
7437 pmap_quick_remove_page(vm_offset_t addr)
7438 {
7439
7440         if (addr != qframe)
7441                 return;
7442         pte_store(vtopte(qframe), 0);
7443         invlpg(qframe);
7444         mtx_unlock_spin(&qframe_mtx);
7445 }
7446
7447 #include "opt_ddb.h"
7448 #ifdef DDB
7449 #include <ddb/ddb.h>
7450
7451 DB_SHOW_COMMAND(pte, pmap_print_pte)
7452 {
7453         pmap_t pmap;
7454         pml4_entry_t *pml4;
7455         pdp_entry_t *pdp;
7456         pd_entry_t *pde;
7457         pt_entry_t *pte, PG_V;
7458         vm_offset_t va;
7459
7460         if (have_addr) {
7461                 va = (vm_offset_t)addr;
7462                 pmap = PCPU_GET(curpmap); /* XXX */
7463         } else {
7464                 db_printf("show pte addr\n");
7465                 return;
7466         }
7467         PG_V = pmap_valid_bit(pmap);
7468         pml4 = pmap_pml4e(pmap, va);
7469         db_printf("VA %#016lx pml4e %#016lx", va, *pml4);
7470         if ((*pml4 & PG_V) == 0) {
7471                 db_printf("\n");
7472                 return;
7473         }
7474         pdp = pmap_pml4e_to_pdpe(pml4, va);
7475         db_printf(" pdpe %#016lx", *pdp);
7476         if ((*pdp & PG_V) == 0 || (*pdp & PG_PS) != 0) {
7477                 db_printf("\n");
7478                 return;
7479         }
7480         pde = pmap_pdpe_to_pde(pdp, va);
7481         db_printf(" pde %#016lx", *pde);
7482         if ((*pde & PG_V) == 0 || (*pde & PG_PS) != 0) {
7483                 db_printf("\n");
7484                 return;
7485         }
7486         pte = pmap_pde_to_pte(pde, va);
7487         db_printf(" pte %#016lx\n", *pte);
7488 }
7489
7490 DB_SHOW_COMMAND(phys2dmap, pmap_phys2dmap)
7491 {
7492         vm_paddr_t a;
7493
7494         if (have_addr) {
7495                 a = (vm_paddr_t)addr;
7496                 db_printf("0x%jx\n", (uintmax_t)PHYS_TO_DMAP(a));
7497         } else {
7498                 db_printf("show phys2dmap addr\n");
7499         }
7500 }
7501 #endif