1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause AND BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1991 Regents of the University of California.
5  * Copyright (c) 1994 John S. Dyson
6  * Copyright (c) 1994 David Greenman
7  * Copyright (c) 2005-2010 Alan L. Cox <alc@cs.rice.edu>
8  * Copyright (c) 2014-2016 Svatopluk Kraus <skra@FreeBSD.org>
9  * Copyright (c) 2014-2016 Michal Meloun <mmel@FreeBSD.org>
10  * All rights reserved.
11  *
12  * This code is derived from software contributed to Berkeley by
13  * the Systems Programming Group of the University of Utah Computer
14  * Science Department and William Jolitz of UUNET Technologies Inc.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  * 3. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	from:	@(#)pmap.c	7.7 (Berkeley)	5/12/91
41  */
42 /*-
43  * Copyright (c) 2003 Networks Associates Technology, Inc.
44  * All rights reserved.
45  *
46  * This software was developed for the FreeBSD Project by Jake Burkholder,
47  * Safeport Network Services, and Network Associates Laboratories, the
48  * Security Research Division of Network Associates, Inc. under
49  * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
50  * CHATS research program.
51  *
52  * Redistribution and use in source and binary forms, with or without
53  * modification, are permitted provided that the following conditions
54  * are met:
55  * 1. Redistributions of source code must retain the above copyright
56  *    notice, this list of conditions and the following disclaimer.
57  * 2. Redistributions in binary form must reproduce the above copyright
58  *    notice, this list of conditions and the following disclaimer in the
59  *    documentation and/or other materials provided with the distribution.
60  *
61  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
62  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
65  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71  * SUCH DAMAGE.
72  */
73 
74 #include <sys/cdefs.h>
75 __FBSDID("$FreeBSD: stable/12/sys/arm/arm/pmap-v6.c 373255 2023-10-23 10:19:27Z zlei $");
76 
77 /*
78  *	Manages physical address maps.
79  *
80  *	Since the information managed by this module is
81  *	also stored by the logical address mapping module,
82  *	this module may throw away valid virtual-to-physical
83  *	mappings at almost any time.  However, invalidations
84  *	of virtual-to-physical mappings must be done as
85  *	requested.
86  *
87  *	In order to cope with hardware architectures which
88  *	make virtual-to-physical map invalidates expensive,
89  *	this module may delay invalidate or reduced protection
90  *	operations until such time as they are actually
91  *	necessary.  This module is given full information as
92  *	to which processors are currently using which maps,
93  *	and to when physical maps must be made correct.
94  */
95 
96 #include "opt_vm.h"
97 #include "opt_pmap.h"
98 #include "opt_ddb.h"
99 
100 #include <sys/param.h>
101 #include <sys/systm.h>
102 #include <sys/kernel.h>
103 #include <sys/ktr.h>
104 #include <sys/lock.h>
105 #include <sys/proc.h>
106 #include <sys/rwlock.h>
107 #include <sys/malloc.h>
108 #include <sys/vmmeter.h>
109 #include <sys/malloc.h>
110 #include <sys/mman.h>
111 #include <sys/sf_buf.h>
112 #include <sys/smp.h>
113 #include <sys/sched.h>
114 #include <sys/sysctl.h>
115 
116 #ifdef DDB
117 #include <ddb/ddb.h>
118 #endif
119 
120 #include <vm/vm.h>
121 #include <vm/uma.h>
122 #include <vm/pmap.h>
123 #include <vm/vm_param.h>
124 #include <vm/vm_kern.h>
125 #include <vm/vm_object.h>
126 #include <vm/vm_map.h>
127 #include <vm/vm_page.h>
128 #include <vm/vm_pageout.h>
129 #include <vm/vm_phys.h>
130 #include <vm/vm_extern.h>
131 #include <vm/vm_reserv.h>
132 #include <sys/lock.h>
133 #include <sys/mutex.h>
134 
135 #include <machine/md_var.h>
136 #include <machine/pmap_var.h>
137 #include <machine/cpu.h>
138 #include <machine/pcb.h>
139 #include <machine/sf_buf.h>
140 #ifdef SMP
141 #include <machine/smp.h>
142 #endif
143 #ifndef PMAP_SHPGPERPROC
144 #define PMAP_SHPGPERPROC 200
145 #endif
146 
147 #ifndef DIAGNOSTIC
148 #define PMAP_INLINE	__inline
149 #else
150 #define PMAP_INLINE
151 #endif
152 
153 #ifdef PMAP_DEBUG
154 static void pmap_zero_page_check(vm_page_t m);
155 void pmap_debug(int level);
156 int pmap_pid_dump(int pid);
157 
158 #define PDEBUG(_lev_,_stat_) \
159 	if (pmap_debug_level >= (_lev_)) \
160 		((_stat_))
161 #define dprintf printf
162 int pmap_debug_level = 1;
163 #else   /* PMAP_DEBUG */
164 #define PDEBUG(_lev_,_stat_) /* Nothing */
165 #define dprintf(x, arg...)
166 #endif  /* PMAP_DEBUG */
167 
168 /*
169  *  Level 2 page tables map definion ('max' is excluded).
170  */
171 
172 #define PT2V_MIN_ADDRESS	((vm_offset_t)PT2MAP)
173 #define PT2V_MAX_ADDRESS	((vm_offset_t)PT2MAP + PT2MAP_SIZE)
174 
175 #define UPT2V_MIN_ADDRESS	((vm_offset_t)PT2MAP)
176 #define UPT2V_MAX_ADDRESS \
177     ((vm_offset_t)(PT2MAP + (KERNBASE >> PT2MAP_SHIFT)))
178 
179 /*
180  *  Promotion to a 1MB (PTE1) page mapping requires that the corresponding
181  *  4KB (PTE2) page mappings have identical settings for the following fields:
182  */
183 #define PTE2_PROMOTE	(PTE2_V | PTE2_A | PTE2_NM | PTE2_S | PTE2_NG |	\
184 			 PTE2_NX | PTE2_RO | PTE2_U | PTE2_W |		\
185 			 PTE2_ATTR_MASK)
186 
187 #define PTE1_PROMOTE	(PTE1_V | PTE1_A | PTE1_NM | PTE1_S | PTE1_NG |	\
188 			 PTE1_NX | PTE1_RO | PTE1_U | PTE1_W |		\
189 			 PTE1_ATTR_MASK)
190 
191 #define ATTR_TO_L1(l2_attr)	((((l2_attr) & L2_TEX0) ? L1_S_TEX0 : 0) | \
192 				 (((l2_attr) & L2_C)    ? L1_S_C    : 0) | \
193 				 (((l2_attr) & L2_B)    ? L1_S_B    : 0) | \
194 				 (((l2_attr) & PTE2_A)  ? PTE1_A    : 0) | \
195 				 (((l2_attr) & PTE2_NM) ? PTE1_NM   : 0) | \
196 				 (((l2_attr) & PTE2_S)  ? PTE1_S    : 0) | \
197 				 (((l2_attr) & PTE2_NG) ? PTE1_NG   : 0) | \
198 				 (((l2_attr) & PTE2_NX) ? PTE1_NX   : 0) | \
199 				 (((l2_attr) & PTE2_RO) ? PTE1_RO   : 0) | \
200 				 (((l2_attr) & PTE2_U)  ? PTE1_U    : 0) | \
201 				 (((l2_attr) & PTE2_W)  ? PTE1_W    : 0))
202 
203 #define ATTR_TO_L2(l1_attr)	((((l1_attr) & L1_S_TEX0) ? L2_TEX0 : 0) | \
204 				 (((l1_attr) & L1_S_C)    ? L2_C    : 0) | \
205 				 (((l1_attr) & L1_S_B)    ? L2_B    : 0) | \
206 				 (((l1_attr) & PTE1_A)    ? PTE2_A  : 0) | \
207 				 (((l1_attr) & PTE1_NM)   ? PTE2_NM : 0) | \
208 				 (((l1_attr) & PTE1_S)    ? PTE2_S  : 0) | \
209 				 (((l1_attr) & PTE1_NG)   ? PTE2_NG : 0) | \
210 				 (((l1_attr) & PTE1_NX)   ? PTE2_NX : 0) | \
211 				 (((l1_attr) & PTE1_RO)   ? PTE2_RO : 0) | \
212 				 (((l1_attr) & PTE1_U)    ? PTE2_U  : 0) | \
213 				 (((l1_attr) & PTE1_W)    ? PTE2_W  : 0))
214 
215 /*
216  *  PTE2 descriptors creation macros.
217  */
218 #define PTE2_ATTR_DEFAULT	vm_memattr_to_pte2(VM_MEMATTR_DEFAULT)
219 #define PTE2_ATTR_PT		vm_memattr_to_pte2(pt_memattr)
220 
221 #define PTE2_KPT(pa)	PTE2_KERN(pa, PTE2_AP_KRW, PTE2_ATTR_PT)
222 #define PTE2_KPT_NG(pa)	PTE2_KERN_NG(pa, PTE2_AP_KRW, PTE2_ATTR_PT)
223 
224 #define PTE2_KRW(pa)	PTE2_KERN(pa, PTE2_AP_KRW, PTE2_ATTR_DEFAULT)
225 #define PTE2_KRO(pa)	PTE2_KERN(pa, PTE2_AP_KR, PTE2_ATTR_DEFAULT)
226 
227 #define PV_STATS
228 #ifdef PV_STATS
229 #define PV_STAT(x)	do { x ; } while (0)
230 #else
231 #define PV_STAT(x)	do { } while (0)
232 #endif
233 
234 /*
235  *  The boot_pt1 is used temporary in very early boot stage as L1 page table.
236  *  We can init many things with no memory allocation thanks to its static
237  *  allocation and this brings two main advantages:
238  *  (1) other cores can be started very simply,
239  *  (2) various boot loaders can be supported as its arguments can be processed
240  *      in virtual address space and can be moved to safe location before
241  *      first allocation happened.
242  *  Only disadvantage is that boot_pt1 is used only in very early boot stage.
243  *  However, the table is uninitialized and so lays in bss. Therefore kernel
244  *  image size is not influenced.
245  *
246  *  QQQ: In the future, maybe, boot_pt1 can be used for soft reset and
247  *       CPU suspend/resume game.
248  */
249 extern pt1_entry_t boot_pt1[];
250 
251 vm_paddr_t base_pt1;
252 pt1_entry_t *kern_pt1;
253 pt2_entry_t *kern_pt2tab;
254 pt2_entry_t *PT2MAP;
255 
256 static uint32_t ttb_flags;
257 static vm_memattr_t pt_memattr;
258 ttb_entry_t pmap_kern_ttb;
259 
260 struct pmap kernel_pmap_store;
261 LIST_HEAD(pmaplist, pmap);
262 static struct pmaplist allpmaps;
263 static struct mtx allpmaps_lock;
264 
265 vm_offset_t virtual_avail;	/* VA of first avail page (after kernel bss) */
266 vm_offset_t virtual_end;	/* VA of last avail page (end of kernel AS) */
267 
268 static vm_offset_t kernel_vm_end_new;
269 vm_offset_t kernel_vm_end = KERNBASE + NKPT2PG * NPT2_IN_PG * PTE1_SIZE;
270 vm_offset_t vm_max_kernel_address;
271 vm_paddr_t kernel_l1pa;
272 
273 static struct rwlock __aligned(CACHE_LINE_SIZE) pvh_global_lock;
274 
275 /*
276  *  Data for the pv entry allocation mechanism
277  */
278 static TAILQ_HEAD(pch, pv_chunk) pv_chunks = TAILQ_HEAD_INITIALIZER(pv_chunks);
279 static int pv_entry_count = 0, pv_entry_max = 0, pv_entry_high_water = 0;
280 static struct md_page *pv_table; /* XXX: Is it used only the list in md_page? */
281 static int shpgperproc = PMAP_SHPGPERPROC;
282 
283 struct pv_chunk *pv_chunkbase;		/* KVA block for pv_chunks */
284 int pv_maxchunks;			/* How many chunks we have KVA for */
285 vm_offset_t pv_vafree;			/* freelist stored in the PTE */
286 
287 vm_paddr_t first_managed_pa;
288 #define	pa_to_pvh(pa)	(&pv_table[pte1_index(pa - first_managed_pa)])
289 
290 /*
291  *  All those kernel PT submaps that BSD is so fond of
292  */
293 caddr_t _tmppt = 0;
294 
295 /*
296  *  Crashdump maps.
297  */
298 static caddr_t crashdumpmap;
299 
300 static pt2_entry_t *PMAP1 = NULL, *PMAP2;
301 static pt2_entry_t *PADDR1 = NULL, *PADDR2;
302 #ifdef DDB
303 static pt2_entry_t *PMAP3;
304 static pt2_entry_t *PADDR3;
305 static int PMAP3cpu __unused; /* for SMP only */
306 #endif
307 #ifdef SMP
308 static int PMAP1cpu;
309 static int PMAP1changedcpu;
310 SYSCTL_INT(_debug, OID_AUTO, PMAP1changedcpu, CTLFLAG_RD,
311     &PMAP1changedcpu, 0,
312     "Number of times pmap_pte2_quick changed CPU with same PMAP1");
313 #endif
314 static int PMAP1changed;
315 SYSCTL_INT(_debug, OID_AUTO, PMAP1changed, CTLFLAG_RD,
316     &PMAP1changed, 0,
317     "Number of times pmap_pte2_quick changed PMAP1");
318 static int PMAP1unchanged;
319 SYSCTL_INT(_debug, OID_AUTO, PMAP1unchanged, CTLFLAG_RD,
320     &PMAP1unchanged, 0,
321     "Number of times pmap_pte2_quick didn't change PMAP1");
322 static struct mtx PMAP2mutex;
323 
324 /*
325  * Internal flags for pmap_enter()'s helper functions.
326  */
327 #define	PMAP_ENTER_NORECLAIM	0x1000000	/* Don't reclaim PV entries. */
328 #define	PMAP_ENTER_NOREPLACE	0x2000000	/* Don't replace mappings. */
329 
330 static __inline void pt2_wirecount_init(vm_page_t m);
331 static boolean_t pmap_demote_pte1(pmap_t pmap, pt1_entry_t *pte1p,
332     vm_offset_t va);
333 static int pmap_enter_pte1(pmap_t pmap, vm_offset_t va, pt1_entry_t pte1,
334     u_int flags, vm_page_t m);
335 void cache_icache_sync_fresh(vm_offset_t va, vm_paddr_t pa, vm_size_t size);
336 
337 /*
338  *  Function to set the debug level of the pmap code.
339  */
340 #ifdef PMAP_DEBUG
341 void
pmap_debug(int level)342 pmap_debug(int level)
343 {
344 
345 	pmap_debug_level = level;
346 	dprintf("pmap_debug: level=%d\n", pmap_debug_level);
347 }
348 #endif /* PMAP_DEBUG */
349 
350 /*
351  *  This table must corespond with memory attribute configuration in vm.h.
352  *  First entry is used for normal system mapping.
353  *
354  *  Device memory is always marked as shared.
355  *  Normal memory is shared only in SMP .
356  *  Not outer shareable bits are not used yet.
357  *  Class 6 cannot be used on ARM11.
358  */
359 #define TEXDEF_TYPE_SHIFT	0
360 #define TEXDEF_TYPE_MASK	0x3
361 #define TEXDEF_INNER_SHIFT	2
362 #define TEXDEF_INNER_MASK	0x3
363 #define TEXDEF_OUTER_SHIFT	4
364 #define TEXDEF_OUTER_MASK	0x3
365 #define TEXDEF_NOS_SHIFT	6
366 #define TEXDEF_NOS_MASK		0x1
367 
368 #define TEX(t, i, o, s) 			\
369 		((t) << TEXDEF_TYPE_SHIFT) |	\
370 		((i) << TEXDEF_INNER_SHIFT) |	\
371 		((o) << TEXDEF_OUTER_SHIFT | 	\
372 		((s) << TEXDEF_NOS_SHIFT))
373 
374 static uint32_t tex_class[8] = {
375 /*	    type      inner cache outer cache */
376 	TEX(PRRR_MEM, NMRR_WB_WA, NMRR_WB_WA, 0),  /* 0 - ATTR_WB_WA	*/
377 	TEX(PRRR_MEM, NMRR_NC,	  NMRR_NC,    0),  /* 1 - ATTR_NOCACHE	*/
378 	TEX(PRRR_DEV, NMRR_NC,	  NMRR_NC,    0),  /* 2 - ATTR_DEVICE	*/
379 	TEX(PRRR_SO,  NMRR_NC,	  NMRR_NC,    0),  /* 3 - ATTR_SO	*/
380 	TEX(PRRR_MEM, NMRR_WT,	  NMRR_WT,    0),  /* 4 - ATTR_WT	*/
381 	TEX(PRRR_MEM, NMRR_NC,	  NMRR_NC,    0),  /* 5 - NOT USED YET	*/
382 	TEX(PRRR_MEM, NMRR_NC,	  NMRR_NC,    0),  /* 6 - NOT USED YET	*/
383 	TEX(PRRR_MEM, NMRR_NC,	  NMRR_NC,    0),  /* 7 - NOT USED YET	*/
384 };
385 #undef TEX
386 
387 static uint32_t pte2_attr_tab[8] = {
388 	PTE2_ATTR_WB_WA,	/* 0 - VM_MEMATTR_WB_WA */
389 	PTE2_ATTR_NOCACHE,	/* 1 - VM_MEMATTR_NOCACHE */
390 	PTE2_ATTR_DEVICE,	/* 2 - VM_MEMATTR_DEVICE */
391 	PTE2_ATTR_SO,		/* 3 - VM_MEMATTR_SO */
392 	PTE2_ATTR_WT,		/* 4 - VM_MEMATTR_WRITE_THROUGH */
393 	0,			/* 5 - NOT USED YET */
394 	0,			/* 6 - NOT USED YET */
395 	0			/* 7 - NOT USED YET */
396 };
397 CTASSERT(VM_MEMATTR_WB_WA == 0);
398 CTASSERT(VM_MEMATTR_NOCACHE == 1);
399 CTASSERT(VM_MEMATTR_DEVICE == 2);
400 CTASSERT(VM_MEMATTR_SO == 3);
401 CTASSERT(VM_MEMATTR_WRITE_THROUGH == 4);
402 #define	VM_MEMATTR_END	(VM_MEMATTR_WRITE_THROUGH + 1)
403 
404 boolean_t
pmap_is_valid_memattr(pmap_t pmap __unused,vm_memattr_t mode)405 pmap_is_valid_memattr(pmap_t pmap __unused, vm_memattr_t mode)
406 {
407 
408 	return (mode >= 0 && mode < VM_MEMATTR_END);
409 }
410 
411 static inline uint32_t
vm_memattr_to_pte2(vm_memattr_t ma)412 vm_memattr_to_pte2(vm_memattr_t ma)
413 {
414 
415 	KASSERT((u_int)ma < VM_MEMATTR_END,
416 	    ("%s: bad vm_memattr_t %d", __func__, ma));
417 	return (pte2_attr_tab[(u_int)ma]);
418 }
419 
420 static inline uint32_t
vm_page_pte2_attr(vm_page_t m)421 vm_page_pte2_attr(vm_page_t m)
422 {
423 
424 	return (vm_memattr_to_pte2(m->md.pat_mode));
425 }
426 
427 /*
428  * Convert TEX definition entry to TTB flags.
429  */
430 static uint32_t
encode_ttb_flags(int idx)431 encode_ttb_flags(int idx)
432 {
433 	uint32_t inner, outer, nos, reg;
434 
435 	inner = (tex_class[idx] >> TEXDEF_INNER_SHIFT) &
436 		TEXDEF_INNER_MASK;
437 	outer = (tex_class[idx] >> TEXDEF_OUTER_SHIFT) &
438 		TEXDEF_OUTER_MASK;
439 	nos = (tex_class[idx] >> TEXDEF_NOS_SHIFT) &
440 		TEXDEF_NOS_MASK;
441 
442 	reg = nos << 5;
443 	reg |= outer << 3;
444 	if (cpuinfo.coherent_walk)
445 		reg |= (inner & 0x1) << 6;
446 	reg |= (inner & 0x2) >> 1;
447 #ifdef SMP
448 	ARM_SMP_UP(
449 		reg |= 1 << 1,
450 	);
451 #endif
452 	return reg;
453 }
454 
455 /*
456  *  Set TEX remapping registers in current CPU.
457  */
458 void
pmap_set_tex(void)459 pmap_set_tex(void)
460 {
461 	uint32_t prrr, nmrr;
462 	uint32_t type, inner, outer, nos;
463 	int i;
464 
465 #ifdef PMAP_PTE_NOCACHE
466 	/* XXX fixme */
467 	if (cpuinfo.coherent_walk) {
468 		pt_memattr = VM_MEMATTR_WB_WA;
469 		ttb_flags = encode_ttb_flags(0);
470 	}
471 	else {
472 		pt_memattr = VM_MEMATTR_NOCACHE;
473 		ttb_flags = encode_ttb_flags(1);
474 	}
475 #else
476 	pt_memattr = VM_MEMATTR_WB_WA;
477 	ttb_flags = encode_ttb_flags(0);
478 #endif
479 
480 	prrr = 0;
481 	nmrr = 0;
482 
483 	/* Build remapping register from TEX classes. */
484 	for (i = 0; i < 8; i++) {
485 		type = (tex_class[i] >> TEXDEF_TYPE_SHIFT) &
486 			TEXDEF_TYPE_MASK;
487 		inner = (tex_class[i] >> TEXDEF_INNER_SHIFT) &
488 			TEXDEF_INNER_MASK;
489 		outer = (tex_class[i] >> TEXDEF_OUTER_SHIFT) &
490 			TEXDEF_OUTER_MASK;
491 		nos = (tex_class[i] >> TEXDEF_NOS_SHIFT) &
492 			TEXDEF_NOS_MASK;
493 
494 		prrr |= type  << (i * 2);
495 		prrr |= nos   << (i + 24);
496 		nmrr |= inner << (i * 2);
497 		nmrr |= outer << (i * 2 + 16);
498 	}
499 	/* Add shareable bits for device memory. */
500 	prrr |= PRRR_DS0 | PRRR_DS1;
501 
502 	/* Add shareable bits for normal memory in SMP case. */
503 #ifdef SMP
504 	ARM_SMP_UP(
505 		prrr |= PRRR_NS1,
506 	);
507 #endif
508 	cp15_prrr_set(prrr);
509 	cp15_nmrr_set(nmrr);
510 
511 	/* Caches are disabled, so full TLB flush should be enough. */
512 	tlb_flush_all_local();
513 }
514 
515 /*
516  * Remap one vm_meattr class to another one. This can be useful as
517  * workaround for SOC errata, e.g. if devices must be accessed using
518  * SO memory class.
519  *
520  * !!! Please note that this function is absolutely last resort thing.
521  * It should not be used under normal circumstances. !!!
522  *
523  * Usage rules:
524  * - it shall be called after pmap_bootstrap_prepare() and before
525  *   cpu_mp_start() (thus only on boot CPU). In practice, it's expected
526  *   to be called from platform_attach() or platform_late_init().
527  *
528  * - if remapping doesn't change caching mode, or until uncached class
529  *   is remapped to any kind of cached one, then no other restriction exists.
530  *
531  * - if pmap_remap_vm_attr() changes caching mode, but both (original and
532  *   remapped) remain cached, then caller is resposible for calling
533  *   of dcache_wbinv_poc_all().
534  *
535  * - remapping of any kind of cached class to uncached is not permitted.
536  */
537 void
pmap_remap_vm_attr(vm_memattr_t old_attr,vm_memattr_t new_attr)538 pmap_remap_vm_attr(vm_memattr_t old_attr, vm_memattr_t new_attr)
539 {
540 	int old_idx, new_idx;
541 
542 	/* Map VM memattrs to indexes to tex_class table. */
543 	old_idx = PTE2_ATTR2IDX(pte2_attr_tab[(int)old_attr]);
544 	new_idx = PTE2_ATTR2IDX(pte2_attr_tab[(int)new_attr]);
545 
546 	/* Replace TEX attribute and apply it. */
547 	tex_class[old_idx] = tex_class[new_idx];
548 	pmap_set_tex();
549 }
550 
551 /*
552  * KERNBASE must be multiple of NPT2_IN_PG * PTE1_SIZE. In other words,
553  * KERNBASE is mapped by first L2 page table in L2 page table page. It
554  * meets same constrain due to PT2MAP being placed just under KERNBASE.
555  */
556 CTASSERT((KERNBASE & (NPT2_IN_PG * PTE1_SIZE - 1)) == 0);
557 CTASSERT((KERNBASE - VM_MAXUSER_ADDRESS) >= PT2MAP_SIZE);
558 
559 /*
560  *  In crazy dreams, PAGE_SIZE could be a multiple of PTE2_SIZE in general.
561  *  For now, anyhow, the following check must be fulfilled.
562  */
563 CTASSERT(PAGE_SIZE == PTE2_SIZE);
564 /*
565  *  We don't want to mess up MI code with all MMU and PMAP definitions,
566  *  so some things, which depend on other ones, are defined independently.
567  *  Now, it is time to check that we don't screw up something.
568  */
569 CTASSERT(PDRSHIFT == PTE1_SHIFT);
570 /*
571  *  Check L1 and L2 page table entries definitions consistency.
572  */
573 CTASSERT(NB_IN_PT1 == (sizeof(pt1_entry_t) * NPTE1_IN_PT1));
574 CTASSERT(NB_IN_PT2 == (sizeof(pt2_entry_t) * NPTE2_IN_PT2));
575 /*
576  *  Check L2 page tables page consistency.
577  */
578 CTASSERT(PAGE_SIZE == (NPT2_IN_PG * NB_IN_PT2));
579 CTASSERT((1 << PT2PG_SHIFT) == NPT2_IN_PG);
580 /*
581  *  Check PT2TAB consistency.
582  *  PT2TAB_ENTRIES is defined as a division of NPTE1_IN_PT1 by NPT2_IN_PG.
583  *  This should be done without remainder.
584  */
585 CTASSERT(NPTE1_IN_PT1 == (PT2TAB_ENTRIES * NPT2_IN_PG));
586 
587 /*
588  *	A PT2MAP magic.
589  *
590  *  All level 2 page tables (PT2s) are mapped continuously and accordingly
591  *  into PT2MAP address space. As PT2 size is less than PAGE_SIZE, this can
592  *  be done only if PAGE_SIZE is a multiple of PT2 size. All PT2s in one page
593  *  must be used together, but not necessary at once. The first PT2 in a page
594  *  must map things on correctly aligned address and the others must follow
595  *  in right order.
596  */
597 #define NB_IN_PT2TAB	(PT2TAB_ENTRIES * sizeof(pt2_entry_t))
598 #define NPT2_IN_PT2TAB	(NB_IN_PT2TAB / NB_IN_PT2)
599 #define NPG_IN_PT2TAB	(NB_IN_PT2TAB / PAGE_SIZE)
600 
601 /*
602  *  Check PT2TAB consistency.
603  *  NPT2_IN_PT2TAB is defined as a division of NB_IN_PT2TAB by NB_IN_PT2.
604  *  NPG_IN_PT2TAB is defined as a division of NB_IN_PT2TAB by PAGE_SIZE.
605  *  The both should be done without remainder.
606  */
607 CTASSERT(NB_IN_PT2TAB == (NPT2_IN_PT2TAB * NB_IN_PT2));
608 CTASSERT(NB_IN_PT2TAB == (NPG_IN_PT2TAB * PAGE_SIZE));
609 /*
610  *  The implementation was made general, however, with the assumption
611  *  bellow in mind. In case of another value of NPG_IN_PT2TAB,
612  *  the code should be once more rechecked.
613  */
614 CTASSERT(NPG_IN_PT2TAB == 1);
615 
616 /*
617  *  Get offset of PT2 in a page
618  *  associated with given PT1 index.
619  */
620 static __inline u_int
page_pt2off(u_int pt1_idx)621 page_pt2off(u_int pt1_idx)
622 {
623 
624 	return ((pt1_idx & PT2PG_MASK) * NB_IN_PT2);
625 }
626 
627 /*
628  *  Get physical address of PT2
629  *  associated with given PT2s page and PT1 index.
630  */
631 static __inline vm_paddr_t
page_pt2pa(vm_paddr_t pgpa,u_int pt1_idx)632 page_pt2pa(vm_paddr_t pgpa, u_int pt1_idx)
633 {
634 
635 	return (pgpa + page_pt2off(pt1_idx));
636 }
637 
638 /*
639  *  Get first entry of PT2
640  *  associated with given PT2s page and PT1 index.
641  */
642 static __inline pt2_entry_t *
page_pt2(vm_offset_t pgva,u_int pt1_idx)643 page_pt2(vm_offset_t pgva, u_int pt1_idx)
644 {
645 
646 	return ((pt2_entry_t *)(pgva + page_pt2off(pt1_idx)));
647 }
648 
649 /*
650  *  Get virtual address of PT2s page (mapped in PT2MAP)
651  *  which holds PT2 which holds entry which maps given virtual address.
652  */
653 static __inline vm_offset_t
pt2map_pt2pg(vm_offset_t va)654 pt2map_pt2pg(vm_offset_t va)
655 {
656 
657 	va &= ~(NPT2_IN_PG * PTE1_SIZE - 1);
658 	return ((vm_offset_t)pt2map_entry(va));
659 }
660 
661 /*****************************************************************************
662  *
663  *     THREE pmap initialization milestones exist:
664  *
665  *  locore.S
666  *    -> fundamental init (including MMU) in ASM
667  *
668  *  initarm()
669  *    -> fundamental init continues in C
670  *    -> first available physical address is known
671  *
672  *    pmap_bootstrap_prepare() -> FIRST PMAP MILESTONE (first epoch begins)
673  *      -> basic (safe) interface for physical address allocation is made
674  *      -> basic (safe) interface for virtual mapping is made
675  *      -> limited not SMP coherent work is possible
676  *
677  *    -> more fundamental init continues in C
678  *    -> locks and some more things are available
679  *    -> all fundamental allocations and mappings are done
680  *
681  *    pmap_bootstrap() -> SECOND PMAP MILESTONE (second epoch begins)
682  *      -> phys_avail[] and virtual_avail is set
683  *      -> control is passed to vm subsystem
684  *      -> physical and virtual address allocation are off limit
685  *      -> low level mapping functions, some SMP coherent,
686  *         are available, which cannot be used before vm subsystem
687  *         is being inited
688  *
689  *  mi_startup()
690  *    -> vm subsystem is being inited
691  *
692  *      pmap_init() -> THIRD PMAP MILESTONE (third epoch begins)
693  *        -> pmap is fully inited
694  *
695  *****************************************************************************/
696 
697 /*****************************************************************************
698  *
699  *	PMAP first stage initialization and utility functions
700  *	for pre-bootstrap epoch.
701  *
702  *  After pmap_bootstrap_prepare() is called, the following functions
703  *  can be used:
704  *
705  *  (1) strictly only for this stage functions for physical page allocations,
706  *      virtual space allocations, and mappings:
707  *
708  *  vm_paddr_t pmap_preboot_get_pages(u_int num);
709  *  void pmap_preboot_map_pages(vm_paddr_t pa, vm_offset_t va, u_int num);
710  *  vm_offset_t pmap_preboot_reserve_pages(u_int num);
711  *  vm_offset_t pmap_preboot_get_vpages(u_int num);
712  *  void pmap_preboot_map_attr(vm_paddr_t pa, vm_offset_t va, vm_size_t size,
713  *      vm_prot_t prot, vm_memattr_t attr);
714  *
715  *  (2) for all stages:
716  *
717  *  vm_paddr_t pmap_kextract(vm_offset_t va);
718  *
719  *  NOTE: This is not SMP coherent stage.
720  *
721  *****************************************************************************/
722 
723 #define KERNEL_P2V(pa) \
724     ((vm_offset_t)((pa) - arm_physmem_kernaddr + KERNVIRTADDR))
725 #define KERNEL_V2P(va) \
726     ((vm_paddr_t)((va) - KERNVIRTADDR + arm_physmem_kernaddr))
727 
728 static vm_paddr_t last_paddr;
729 
730 /*
731  *  Pre-bootstrap epoch page allocator.
732  */
733 vm_paddr_t
pmap_preboot_get_pages(u_int num)734 pmap_preboot_get_pages(u_int num)
735 {
736 	vm_paddr_t ret;
737 
738 	ret = last_paddr;
739 	last_paddr += num * PAGE_SIZE;
740 
741 	return (ret);
742 }
743 
744 /*
745  *	The fundamental initialization of PMAP stuff.
746  *
747  *  Some things already happened in locore.S and some things could happen
748  *  before pmap_bootstrap_prepare() is called, so let's recall what is done:
749  *  1. Caches are disabled.
750  *  2. We are running on virtual addresses already with 'boot_pt1'
751  *     as L1 page table.
752  *  3. So far, all virtual addresses can be converted to physical ones and
753  *     vice versa by the following macros:
754  *       KERNEL_P2V(pa) .... physical to virtual ones,
755  *       KERNEL_V2P(va) .... virtual to physical ones.
756  *
757  *  What is done herein:
758  *  1. The 'boot_pt1' is replaced by real kernel L1 page table 'kern_pt1'.
759  *  2. PT2MAP magic is brought to live.
760  *  3. Basic preboot functions for page allocations and mappings can be used.
761  *  4. Everything is prepared for L1 cache enabling.
762  *
763  *  Variations:
764  *  1. To use second TTB register, so kernel and users page tables will be
765  *     separated. This way process forking - pmap_pinit() - could be faster,
766  *     it saves physical pages and KVA per a process, and it's simple change.
767  *     However, it will lead, due to hardware matter, to the following:
768  *     (a) 2G space for kernel and 2G space for users.
769  *     (b) 1G space for kernel in low addresses and 3G for users above it.
770  *     A question is: Is the case (b) really an option? Note that case (b)
771  *     does save neither physical memory and KVA.
772  */
773 void
pmap_bootstrap_prepare(vm_paddr_t last)774 pmap_bootstrap_prepare(vm_paddr_t last)
775 {
776 	vm_paddr_t pt2pg_pa, pt2tab_pa, pa, size;
777 	vm_offset_t pt2pg_va;
778 	pt1_entry_t *pte1p;
779 	pt2_entry_t *pte2p;
780 	u_int i;
781 	uint32_t l1_attr;
782 
783 	/*
784 	 * Now, we are going to make real kernel mapping. Note that we are
785 	 * already running on some mapping made in locore.S and we expect
786 	 * that it's large enough to ensure nofault access to physical memory
787 	 * allocated herein before switch.
788 	 *
789 	 * As kernel image and everything needed before are and will be mapped
790 	 * by section mappings, we align last physical address to PTE1_SIZE.
791 	 */
792 	last_paddr = pte1_roundup(last);
793 
794 	/*
795 	 * Allocate and zero page(s) for kernel L1 page table.
796 	 *
797 	 * Note that it's first allocation on space which was PTE1_SIZE
798 	 * aligned and as such base_pt1 is aligned to NB_IN_PT1 too.
799 	 */
800 	base_pt1 = pmap_preboot_get_pages(NPG_IN_PT1);
801 	kern_pt1 = (pt1_entry_t *)KERNEL_P2V(base_pt1);
802 	bzero((void*)kern_pt1, NB_IN_PT1);
803 	pte1_sync_range(kern_pt1, NB_IN_PT1);
804 
805 	/* Allocate and zero page(s) for kernel PT2TAB. */
806 	pt2tab_pa = pmap_preboot_get_pages(NPG_IN_PT2TAB);
807 	kern_pt2tab = (pt2_entry_t *)KERNEL_P2V(pt2tab_pa);
808 	bzero(kern_pt2tab, NB_IN_PT2TAB);
809 	pte2_sync_range(kern_pt2tab, NB_IN_PT2TAB);
810 
811 	/* Allocate and zero page(s) for kernel L2 page tables. */
812 	pt2pg_pa = pmap_preboot_get_pages(NKPT2PG);
813 	pt2pg_va = KERNEL_P2V(pt2pg_pa);
814 	size = NKPT2PG * PAGE_SIZE;
815 	bzero((void*)pt2pg_va, size);
816 	pte2_sync_range((pt2_entry_t *)pt2pg_va, size);
817 
818 	/*
819 	 * Add a physical memory segment (vm_phys_seg) corresponding to the
820 	 * preallocated pages for kernel L2 page tables so that vm_page
821 	 * structures representing these pages will be created. The vm_page
822 	 * structures are required for promotion of the corresponding kernel
823 	 * virtual addresses to section mappings.
824 	 */
825 	vm_phys_add_seg(pt2tab_pa, pmap_preboot_get_pages(0));
826 
827 	/*
828 	 * Insert allocated L2 page table pages to PT2TAB and make
829 	 * link to all PT2s in L1 page table. See how kernel_vm_end
830 	 * is initialized.
831 	 *
832 	 * We play simple and safe. So every KVA will have underlaying
833 	 * L2 page table, even kernel image mapped by sections.
834 	 */
835 	pte2p = kern_pt2tab_entry(KERNBASE);
836 	for (pa = pt2pg_pa; pa < pt2pg_pa + size; pa += PTE2_SIZE)
837 		pt2tab_store(pte2p++, PTE2_KPT(pa));
838 
839 	pte1p = kern_pte1(KERNBASE);
840 	for (pa = pt2pg_pa; pa < pt2pg_pa + size; pa += NB_IN_PT2)
841 		pte1_store(pte1p++, PTE1_LINK(pa));
842 
843 	/* Make section mappings for kernel. */
844 	l1_attr = ATTR_TO_L1(PTE2_ATTR_DEFAULT);
845 	pte1p = kern_pte1(KERNBASE);
846 	for (pa = KERNEL_V2P(KERNBASE); pa < last; pa += PTE1_SIZE)
847 		pte1_store(pte1p++, PTE1_KERN(pa, PTE1_AP_KRW, l1_attr));
848 
849 	/*
850 	 * Get free and aligned space for PT2MAP and make L1 page table links
851 	 * to L2 page tables held in PT2TAB.
852 	 *
853 	 * Note that pages holding PT2s are stored in PT2TAB as pt2_entry_t
854 	 * descriptors and PT2TAB page(s) itself is(are) used as PT2s. Thus
855 	 * each entry in PT2TAB maps all PT2s in a page. This implies that
856 	 * virtual address of PT2MAP must be aligned to NPT2_IN_PG * PTE1_SIZE.
857 	 */
858 	PT2MAP = (pt2_entry_t *)(KERNBASE - PT2MAP_SIZE);
859 	pte1p = kern_pte1((vm_offset_t)PT2MAP);
860 	for (pa = pt2tab_pa, i = 0; i < NPT2_IN_PT2TAB; i++, pa += NB_IN_PT2) {
861 		pte1_store(pte1p++, PTE1_LINK(pa));
862 	}
863 
864 	/*
865 	 * Store PT2TAB in PT2TAB itself, i.e. self reference mapping.
866 	 * Each pmap will hold own PT2TAB, so the mapping should be not global.
867 	 */
868 	pte2p = kern_pt2tab_entry((vm_offset_t)PT2MAP);
869 	for (pa = pt2tab_pa, i = 0; i < NPG_IN_PT2TAB; i++, pa += PTE2_SIZE) {
870 		pt2tab_store(pte2p++, PTE2_KPT_NG(pa));
871 	}
872 
873 	/*
874 	 * Choose correct L2 page table and make mappings for allocations
875 	 * made herein which replaces temporary locore.S mappings after a while.
876 	 * Note that PT2MAP cannot be used until we switch to kern_pt1.
877 	 *
878 	 * Note, that these allocations started aligned on 1M section and
879 	 * kernel PT1 was allocated first. Making of mappings must follow
880 	 * order of physical allocations as we've used KERNEL_P2V() macro
881 	 * for virtual addresses resolution.
882 	 */
883 	pte2p = kern_pt2tab_entry((vm_offset_t)kern_pt1);
884 	pt2pg_va = KERNEL_P2V(pte2_pa(pte2_load(pte2p)));
885 
886 	pte2p = page_pt2(pt2pg_va, pte1_index((vm_offset_t)kern_pt1));
887 
888 	/* Make mapping for kernel L1 page table. */
889 	for (pa = base_pt1, i = 0; i < NPG_IN_PT1; i++, pa += PTE2_SIZE)
890 		pte2_store(pte2p++, PTE2_KPT(pa));
891 
892 	/* Make mapping for kernel PT2TAB. */
893 	for (pa = pt2tab_pa, i = 0; i < NPG_IN_PT2TAB; i++, pa += PTE2_SIZE)
894 		pte2_store(pte2p++, PTE2_KPT(pa));
895 
896 	/* Finally, switch from 'boot_pt1' to 'kern_pt1'. */
897 	pmap_kern_ttb = base_pt1 | ttb_flags;
898 	cpuinfo_reinit_mmu(pmap_kern_ttb);
899 	/*
900 	 * Initialize the first available KVA. As kernel image is mapped by
901 	 * sections, we are leaving some gap behind.
902 	 */
903 	virtual_avail = (vm_offset_t)kern_pt2tab + NPG_IN_PT2TAB * PAGE_SIZE;
904 }
905 
906 /*
907  *  Setup L2 page table page for given KVA.
908  *  Used in pre-bootstrap epoch.
909  *
910  *  Note that we have allocated NKPT2PG pages for L2 page tables in advance
911  *  and used them for mapping KVA starting from KERNBASE. However, this is not
912  *  enough. Vectors and devices need L2 page tables too. Note that they are
913  *  even above VM_MAX_KERNEL_ADDRESS.
914  */
915 static __inline vm_paddr_t
pmap_preboot_pt2pg_setup(vm_offset_t va)916 pmap_preboot_pt2pg_setup(vm_offset_t va)
917 {
918 	pt2_entry_t *pte2p, pte2;
919 	vm_paddr_t pt2pg_pa;
920 
921 	/* Get associated entry in PT2TAB. */
922 	pte2p = kern_pt2tab_entry(va);
923 
924 	/* Just return, if PT2s page exists already. */
925 	pte2 = pt2tab_load(pte2p);
926 	if (pte2_is_valid(pte2))
927 		return (pte2_pa(pte2));
928 
929 	KASSERT(va >= VM_MAX_KERNEL_ADDRESS,
930 	    ("%s: NKPT2PG too small", __func__));
931 
932 	/*
933 	 * Allocate page for PT2s and insert it to PT2TAB.
934 	 * In other words, map it into PT2MAP space.
935 	 */
936 	pt2pg_pa = pmap_preboot_get_pages(1);
937 	pt2tab_store(pte2p, PTE2_KPT(pt2pg_pa));
938 
939 	/* Zero all PT2s in allocated page. */
940 	bzero((void*)pt2map_pt2pg(va), PAGE_SIZE);
941 	pte2_sync_range((pt2_entry_t *)pt2map_pt2pg(va), PAGE_SIZE);
942 
943 	return (pt2pg_pa);
944 }
945 
946 /*
947  *  Setup L2 page table for given KVA.
948  *  Used in pre-bootstrap epoch.
949  */
950 static void
pmap_preboot_pt2_setup(vm_offset_t va)951 pmap_preboot_pt2_setup(vm_offset_t va)
952 {
953 	pt1_entry_t *pte1p;
954 	vm_paddr_t pt2pg_pa, pt2_pa;
955 
956 	/* Setup PT2's page. */
957 	pt2pg_pa = pmap_preboot_pt2pg_setup(va);
958 	pt2_pa = page_pt2pa(pt2pg_pa, pte1_index(va));
959 
960 	/* Insert PT2 to PT1. */
961 	pte1p = kern_pte1(va);
962 	pte1_store(pte1p, PTE1_LINK(pt2_pa));
963 }
964 
965 /*
966  *  Get L2 page entry associated with given KVA.
967  *  Used in pre-bootstrap epoch.
968  */
969 static __inline pt2_entry_t*
pmap_preboot_vtopte2(vm_offset_t va)970 pmap_preboot_vtopte2(vm_offset_t va)
971 {
972 	pt1_entry_t *pte1p;
973 
974 	/* Setup PT2 if needed. */
975 	pte1p = kern_pte1(va);
976 	if (!pte1_is_valid(pte1_load(pte1p))) /* XXX - sections ?! */
977 		pmap_preboot_pt2_setup(va);
978 
979 	return (pt2map_entry(va));
980 }
981 
982 /*
983  *  Pre-bootstrap epoch page(s) mapping(s).
984  */
985 void
pmap_preboot_map_pages(vm_paddr_t pa,vm_offset_t va,u_int num)986 pmap_preboot_map_pages(vm_paddr_t pa, vm_offset_t va, u_int num)
987 {
988 	u_int i;
989 	pt2_entry_t *pte2p;
990 
991 	/* Map all the pages. */
992 	for (i = 0; i < num; i++) {
993 		pte2p = pmap_preboot_vtopte2(va);
994 		pte2_store(pte2p, PTE2_KRW(pa));
995 		va += PAGE_SIZE;
996 		pa += PAGE_SIZE;
997 	}
998 }
999 
1000 /*
1001  *  Pre-bootstrap epoch virtual space alocator.
1002  */
1003 vm_offset_t
pmap_preboot_reserve_pages(u_int num)1004 pmap_preboot_reserve_pages(u_int num)
1005 {
1006 	u_int i;
1007 	vm_offset_t start, va;
1008 	pt2_entry_t *pte2p;
1009 
1010 	/* Allocate virtual space. */
1011 	start = va = virtual_avail;
1012 	virtual_avail += num * PAGE_SIZE;
1013 
1014 	/* Zero the mapping. */
1015 	for (i = 0; i < num; i++) {
1016 		pte2p = pmap_preboot_vtopte2(va);
1017 		pte2_store(pte2p, 0);
1018 		va += PAGE_SIZE;
1019 	}
1020 
1021 	return (start);
1022 }
1023 
1024 /*
1025  *  Pre-bootstrap epoch page(s) allocation and mapping(s).
1026  */
1027 vm_offset_t
pmap_preboot_get_vpages(u_int num)1028 pmap_preboot_get_vpages(u_int num)
1029 {
1030 	vm_paddr_t  pa;
1031 	vm_offset_t va;
1032 
1033 	/* Allocate physical page(s). */
1034 	pa = pmap_preboot_get_pages(num);
1035 
1036 	/* Allocate virtual space. */
1037 	va = virtual_avail;
1038 	virtual_avail += num * PAGE_SIZE;
1039 
1040 	/* Map and zero all. */
1041 	pmap_preboot_map_pages(pa, va, num);
1042 	bzero((void *)va, num * PAGE_SIZE);
1043 
1044 	return (va);
1045 }
1046 
1047 /*
1048  *  Pre-bootstrap epoch page mapping(s) with attributes.
1049  */
1050 void
pmap_preboot_map_attr(vm_paddr_t pa,vm_offset_t va,vm_size_t size,vm_prot_t prot,vm_memattr_t attr)1051 pmap_preboot_map_attr(vm_paddr_t pa, vm_offset_t va, vm_size_t size,
1052     vm_prot_t prot, vm_memattr_t attr)
1053 {
1054 	u_int num;
1055 	u_int l1_attr, l1_prot, l2_prot, l2_attr;
1056 	pt1_entry_t *pte1p;
1057 	pt2_entry_t *pte2p;
1058 
1059 	l2_prot = prot & VM_PROT_WRITE ? PTE2_AP_KRW : PTE2_AP_KR;
1060 	l2_prot |= (prot & VM_PROT_EXECUTE) ? PTE2_X : PTE2_NX;
1061 	l2_attr = vm_memattr_to_pte2(attr);
1062 	l1_prot = ATTR_TO_L1(l2_prot);
1063 	l1_attr = ATTR_TO_L1(l2_attr);
1064 
1065 	/* Map all the pages. */
1066 	num = round_page(size);
1067 	while (num > 0) {
1068 		if ((((va | pa) & PTE1_OFFSET) == 0) && (num >= PTE1_SIZE)) {
1069 			pte1p = kern_pte1(va);
1070 			pte1_store(pte1p, PTE1_KERN(pa, l1_prot, l1_attr));
1071 			va += PTE1_SIZE;
1072 			pa += PTE1_SIZE;
1073 			num -= PTE1_SIZE;
1074 		} else {
1075 			pte2p = pmap_preboot_vtopte2(va);
1076 			pte2_store(pte2p, PTE2_KERN(pa, l2_prot, l2_attr));
1077 			va += PAGE_SIZE;
1078 			pa += PAGE_SIZE;
1079 			num -= PAGE_SIZE;
1080 		}
1081 	}
1082 }
1083 
1084 /*
1085  *  Extract from the kernel page table the physical address
1086  *  that is mapped by the given virtual address "va".
1087  */
1088 vm_paddr_t
pmap_kextract(vm_offset_t va)1089 pmap_kextract(vm_offset_t va)
1090 {
1091 	vm_paddr_t pa;
1092 	pt1_entry_t pte1;
1093 	pt2_entry_t pte2;
1094 
1095 	pte1 = pte1_load(kern_pte1(va));
1096 	if (pte1_is_section(pte1)) {
1097 		pa = pte1_pa(pte1) | (va & PTE1_OFFSET);
1098 	} else if (pte1_is_link(pte1)) {
1099 		/*
1100 		 * We should beware of concurrent promotion that changes
1101 		 * pte1 at this point. However, it's not a problem as PT2
1102 		 * page is preserved by promotion in PT2TAB. So even if
1103 		 * it happens, using of PT2MAP is still safe.
1104 		 *
1105 		 * QQQ: However, concurrent removing is a problem which
1106 		 *      ends in abort on PT2MAP space. Locking must be used
1107 		 *      to deal with this.
1108 		 */
1109 		pte2 = pte2_load(pt2map_entry(va));
1110 		pa = pte2_pa(pte2) | (va & PTE2_OFFSET);
1111 	}
1112 	else {
1113 		panic("%s: va %#x pte1 %#x", __func__, va, pte1);
1114 	}
1115 	return (pa);
1116 }
1117 
1118 /*
1119  *  Extract from the kernel page table the physical address
1120  *  that is mapped by the given virtual address "va". Also
1121  *  return L2 page table entry which maps the address.
1122  *
1123  *  This is only intended to be used for panic dumps.
1124  */
1125 vm_paddr_t
pmap_dump_kextract(vm_offset_t va,pt2_entry_t * pte2p)1126 pmap_dump_kextract(vm_offset_t va, pt2_entry_t *pte2p)
1127 {
1128 	vm_paddr_t pa;
1129 	pt1_entry_t pte1;
1130 	pt2_entry_t pte2;
1131 
1132 	pte1 = pte1_load(kern_pte1(va));
1133 	if (pte1_is_section(pte1)) {
1134 		pa = pte1_pa(pte1) | (va & PTE1_OFFSET);
1135 		pte2 = pa | ATTR_TO_L2(pte1) | PTE2_V;
1136 	} else if (pte1_is_link(pte1)) {
1137 		pte2 = pte2_load(pt2map_entry(va));
1138 		pa = pte2_pa(pte2);
1139 	} else {
1140 		pte2 = 0;
1141 		pa = 0;
1142 	}
1143 	if (pte2p != NULL)
1144 		*pte2p = pte2;
1145 	return (pa);
1146 }
1147 
1148 /*****************************************************************************
1149  *
1150  *	PMAP second stage initialization and utility functions
1151  *	for bootstrap epoch.
1152  *
1153  *  After pmap_bootstrap() is called, the following functions for
1154  *  mappings can be used:
1155  *
1156  *  void pmap_kenter(vm_offset_t va, vm_paddr_t pa);
1157  *  void pmap_kremove(vm_offset_t va);
1158  *  vm_offset_t pmap_map(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end,
1159  *      int prot);
1160  *
1161  *  NOTE: This is not SMP coherent stage. And physical page allocation is not
1162  *        allowed during this stage.
1163  *
1164  *****************************************************************************/
1165 
1166 /*
1167  *  Initialize kernel PMAP locks and lists, kernel_pmap itself, and
1168  *  reserve various virtual spaces for temporary mappings.
1169  */
1170 void
pmap_bootstrap(vm_offset_t firstaddr)1171 pmap_bootstrap(vm_offset_t firstaddr)
1172 {
1173 	pt2_entry_t *unused __unused;
1174 	struct pcpu *pc;
1175 
1176 	/*
1177 	 * Initialize the kernel pmap (which is statically allocated).
1178 	 */
1179 	PMAP_LOCK_INIT(kernel_pmap);
1180 	kernel_l1pa = (vm_paddr_t)kern_pt1;  /* for libkvm */
1181 	kernel_pmap->pm_pt1 = kern_pt1;
1182 	kernel_pmap->pm_pt2tab = kern_pt2tab;
1183 	CPU_FILL(&kernel_pmap->pm_active);  /* don't allow deactivation */
1184 	TAILQ_INIT(&kernel_pmap->pm_pvchunk);
1185 
1186 	/*
1187 	 * Initialize the global pv list lock.
1188 	 */
1189 	rw_init(&pvh_global_lock, "pmap pv global");
1190 
1191 	LIST_INIT(&allpmaps);
1192 
1193 	/*
1194 	 * Request a spin mutex so that changes to allpmaps cannot be
1195 	 * preempted by smp_rendezvous_cpus().
1196 	 */
1197 	mtx_init(&allpmaps_lock, "allpmaps", NULL, MTX_SPIN);
1198 	mtx_lock_spin(&allpmaps_lock);
1199 	LIST_INSERT_HEAD(&allpmaps, kernel_pmap, pm_list);
1200 	mtx_unlock_spin(&allpmaps_lock);
1201 
1202 	/*
1203 	 * Reserve some special page table entries/VA space for temporary
1204 	 * mapping of pages.
1205 	 */
1206 #define	SYSMAP(c, p, v, n)  do {		\
1207 	v = (c)pmap_preboot_reserve_pages(n);	\
1208 	p = pt2map_entry((vm_offset_t)v);	\
1209 	} while (0)
1210 
1211 	/*
1212 	 * Local CMAP1/CMAP2 are used for zeroing and copying pages.
1213 	 * Local CMAP2 is also used for data cache cleaning.
1214 	 */
1215 	pc = get_pcpu();
1216 	mtx_init(&pc->pc_cmap_lock, "SYSMAPS", NULL, MTX_DEF);
1217 	SYSMAP(caddr_t, pc->pc_cmap1_pte2p, pc->pc_cmap1_addr, 1);
1218 	SYSMAP(caddr_t, pc->pc_cmap2_pte2p, pc->pc_cmap2_addr, 1);
1219 	SYSMAP(vm_offset_t, pc->pc_qmap_pte2p, pc->pc_qmap_addr, 1);
1220 
1221 	/*
1222 	 * Crashdump maps.
1223 	 */
1224 	SYSMAP(caddr_t, unused, crashdumpmap, MAXDUMPPGS);
1225 
1226 	/*
1227 	 * _tmppt is used for reading arbitrary physical pages via /dev/mem.
1228 	 */
1229 	SYSMAP(caddr_t, unused, _tmppt, 1);
1230 
1231 	/*
1232 	 * PADDR1 and PADDR2 are used by pmap_pte2_quick() and pmap_pte2(),
1233 	 * respectively. PADDR3 is used by pmap_pte2_ddb().
1234 	 */
1235 	SYSMAP(pt2_entry_t *, PMAP1, PADDR1, 1);
1236 	SYSMAP(pt2_entry_t *, PMAP2, PADDR2, 1);
1237 #ifdef DDB
1238 	SYSMAP(pt2_entry_t *, PMAP3, PADDR3, 1);
1239 #endif
1240 	mtx_init(&PMAP2mutex, "PMAP2", NULL, MTX_DEF);
1241 
1242 	/*
1243 	 * Note that in very short time in initarm(), we are going to
1244 	 * initialize phys_avail[] array and no further page allocation
1245 	 * can happen after that until vm subsystem will be initialized.
1246 	 */
1247 	kernel_vm_end_new = kernel_vm_end;
1248 	virtual_end = vm_max_kernel_address;
1249 }
1250 
1251 static void
pmap_init_reserved_pages(void)1252 pmap_init_reserved_pages(void)
1253 {
1254 	struct pcpu *pc;
1255 	vm_offset_t pages;
1256 	int i;
1257 
1258 	CPU_FOREACH(i) {
1259 		pc = pcpu_find(i);
1260 		/*
1261 		 * Skip if the mapping has already been initialized,
1262 		 * i.e. this is the BSP.
1263 		 */
1264 		if (pc->pc_cmap1_addr != 0)
1265 			continue;
1266 		mtx_init(&pc->pc_cmap_lock, "SYSMAPS", NULL, MTX_DEF);
1267 		pages = kva_alloc(PAGE_SIZE * 3);
1268 		if (pages == 0)
1269 			panic("%s: unable to allocate KVA", __func__);
1270 		pc->pc_cmap1_pte2p = pt2map_entry(pages);
1271 		pc->pc_cmap2_pte2p = pt2map_entry(pages + PAGE_SIZE);
1272 		pc->pc_qmap_pte2p = pt2map_entry(pages + (PAGE_SIZE * 2));
1273 		pc->pc_cmap1_addr = (caddr_t)pages;
1274 		pc->pc_cmap2_addr = (caddr_t)(pages + PAGE_SIZE);
1275 		pc->pc_qmap_addr = pages + (PAGE_SIZE * 2);
1276 	}
1277 }
1278 SYSINIT(rpages_init, SI_SUB_CPU, SI_ORDER_ANY, pmap_init_reserved_pages, NULL);
1279 
1280 /*
1281  *  The function can already be use in second initialization stage.
1282  *  As such, the function DOES NOT call pmap_growkernel() where PT2
1283  *  allocation can happen. So if used, be sure that PT2 for given
1284  *  virtual address is allocated already!
1285  *
1286  *  Add a wired page to the kva.
1287  *  Note: not SMP coherent.
1288  */
1289 static __inline void
pmap_kenter_prot_attr(vm_offset_t va,vm_paddr_t pa,uint32_t prot,uint32_t attr)1290 pmap_kenter_prot_attr(vm_offset_t va, vm_paddr_t pa, uint32_t prot,
1291     uint32_t attr)
1292 {
1293 	pt1_entry_t *pte1p;
1294 	pt2_entry_t *pte2p;
1295 
1296 	pte1p = kern_pte1(va);
1297 	if (!pte1_is_valid(pte1_load(pte1p))) { /* XXX - sections ?! */
1298 		/*
1299 		 * This is a very low level function, so PT2 and particularly
1300 		 * PT2PG associated with given virtual address must be already
1301 		 * allocated. It's a pain mainly during pmap initialization
1302 		 * stage. However, called after pmap initialization with
1303 		 * virtual address not under kernel_vm_end will lead to
1304 		 * the same misery.
1305 		 */
1306 		if (!pte2_is_valid(pte2_load(kern_pt2tab_entry(va))))
1307 			panic("%s: kernel PT2 not allocated!", __func__);
1308 	}
1309 
1310 	pte2p = pt2map_entry(va);
1311 	pte2_store(pte2p, PTE2_KERN(pa, prot, attr));
1312 }
1313 
1314 PMAP_INLINE void
pmap_kenter(vm_offset_t va,vm_paddr_t pa)1315 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
1316 {
1317 
1318 	pmap_kenter_prot_attr(va, pa, PTE2_AP_KRW, PTE2_ATTR_DEFAULT);
1319 }
1320 
1321 /*
1322  *  Remove a page from the kernel pagetables.
1323  *  Note: not SMP coherent.
1324  */
1325 PMAP_INLINE void
pmap_kremove(vm_offset_t va)1326 pmap_kremove(vm_offset_t va)
1327 {
1328 	pt1_entry_t *pte1p;
1329 	pt2_entry_t *pte2p;
1330 
1331 	pte1p = kern_pte1(va);
1332 	if (pte1_is_section(pte1_load(pte1p))) {
1333 		pte1_clear(pte1p);
1334 	} else {
1335 		pte2p = pt2map_entry(va);
1336 		pte2_clear(pte2p);
1337 	}
1338 }
1339 
1340 /*
1341  *  Share new kernel PT2PG with all pmaps.
1342  *  The caller is responsible for maintaining TLB consistency.
1343  */
1344 static void
pmap_kenter_pt2tab(vm_offset_t va,pt2_entry_t npte2)1345 pmap_kenter_pt2tab(vm_offset_t va, pt2_entry_t npte2)
1346 {
1347 	pmap_t pmap;
1348 	pt2_entry_t *pte2p;
1349 
1350 	mtx_lock_spin(&allpmaps_lock);
1351 	LIST_FOREACH(pmap, &allpmaps, pm_list) {
1352 		pte2p = pmap_pt2tab_entry(pmap, va);
1353 		pt2tab_store(pte2p, npte2);
1354 	}
1355 	mtx_unlock_spin(&allpmaps_lock);
1356 }
1357 
1358 /*
1359  *  Share new kernel PTE1 with all pmaps.
1360  *  The caller is responsible for maintaining TLB consistency.
1361  */
1362 static void
pmap_kenter_pte1(vm_offset_t va,pt1_entry_t npte1)1363 pmap_kenter_pte1(vm_offset_t va, pt1_entry_t npte1)
1364 {
1365 	pmap_t pmap;
1366 	pt1_entry_t *pte1p;
1367 
1368 	mtx_lock_spin(&allpmaps_lock);
1369 	LIST_FOREACH(pmap, &allpmaps, pm_list) {
1370 		pte1p = pmap_pte1(pmap, va);
1371 		pte1_store(pte1p, npte1);
1372 	}
1373 	mtx_unlock_spin(&allpmaps_lock);
1374 }
1375 
1376 /*
1377  *  Used to map a range of physical addresses into kernel
1378  *  virtual address space.
1379  *
1380  *  The value passed in '*virt' is a suggested virtual address for
1381  *  the mapping. Architectures which can support a direct-mapped
1382  *  physical to virtual region can return the appropriate address
1383  *  within that region, leaving '*virt' unchanged. Other
1384  *  architectures should map the pages starting at '*virt' and
1385  *  update '*virt' with the first usable address after the mapped
1386  *  region.
1387  *
1388  *  NOTE: Read the comments above pmap_kenter_prot_attr() as
1389  *        the function is used herein!
1390  */
1391 vm_offset_t
pmap_map(vm_offset_t * virt,vm_paddr_t start,vm_paddr_t end,int prot)1392 pmap_map(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end, int prot)
1393 {
1394 	vm_offset_t va, sva;
1395 	vm_paddr_t pte1_offset;
1396 	pt1_entry_t npte1;
1397 	uint32_t l1prot, l2prot;
1398 	uint32_t l1attr, l2attr;
1399 
1400 	PDEBUG(1, printf("%s: virt = %#x, start = %#x, end = %#x (size = %#x),"
1401 	    " prot = %d\n", __func__, *virt, start, end, end - start,  prot));
1402 
1403 	l2prot = (prot & VM_PROT_WRITE) ? PTE2_AP_KRW : PTE2_AP_KR;
1404 	l2prot |= (prot & VM_PROT_EXECUTE) ? PTE2_X : PTE2_NX;
1405 	l1prot = ATTR_TO_L1(l2prot);
1406 
1407 	l2attr = PTE2_ATTR_DEFAULT;
1408 	l1attr = ATTR_TO_L1(l2attr);
1409 
1410 	va = *virt;
1411 	/*
1412 	 * Does the physical address range's size and alignment permit at
1413 	 * least one section mapping to be created?
1414 	 */
1415 	pte1_offset = start & PTE1_OFFSET;
1416 	if ((end - start) - ((PTE1_SIZE - pte1_offset) & PTE1_OFFSET) >=
1417 	    PTE1_SIZE) {
1418 		/*
1419 		 * Increase the starting virtual address so that its alignment
1420 		 * does not preclude the use of section mappings.
1421 		 */
1422 		if ((va & PTE1_OFFSET) < pte1_offset)
1423 			va = pte1_trunc(va) + pte1_offset;
1424 		else if ((va & PTE1_OFFSET) > pte1_offset)
1425 			va = pte1_roundup(va) + pte1_offset;
1426 	}
1427 	sva = va;
1428 	while (start < end) {
1429 		if ((start & PTE1_OFFSET) == 0 && end - start >= PTE1_SIZE) {
1430 			KASSERT((va & PTE1_OFFSET) == 0,
1431 			    ("%s: misaligned va %#x", __func__, va));
1432 			npte1 = PTE1_KERN(start, l1prot, l1attr);
1433 			pmap_kenter_pte1(va, npte1);
1434 			va += PTE1_SIZE;
1435 			start += PTE1_SIZE;
1436 		} else {
1437 			pmap_kenter_prot_attr(va, start, l2prot, l2attr);
1438 			va += PAGE_SIZE;
1439 			start += PAGE_SIZE;
1440 		}
1441 	}
1442 	tlb_flush_range(sva, va - sva);
1443 	*virt = va;
1444 	return (sva);
1445 }
1446 
1447 /*
1448  *  Make a temporary mapping for a physical address.
1449  *  This is only intended to be used for panic dumps.
1450  */
1451 void *
pmap_kenter_temporary(vm_paddr_t pa,int i)1452 pmap_kenter_temporary(vm_paddr_t pa, int i)
1453 {
1454 	vm_offset_t va;
1455 
1456 	/* QQQ: 'i' should be less or equal to MAXDUMPPGS. */
1457 
1458 	va = (vm_offset_t)crashdumpmap + (i * PAGE_SIZE);
1459 	pmap_kenter(va, pa);
1460 	tlb_flush_local(va);
1461 	return ((void *)crashdumpmap);
1462 }
1463 
1464 
1465 /*************************************
1466  *
1467  *  TLB & cache maintenance routines.
1468  *
1469  *************************************/
1470 
1471 /*
1472  *  We inline these within pmap.c for speed.
1473  */
1474 PMAP_INLINE void
pmap_tlb_flush(pmap_t pmap,vm_offset_t va)1475 pmap_tlb_flush(pmap_t pmap, vm_offset_t va)
1476 {
1477 
1478 	if (pmap == kernel_pmap || !CPU_EMPTY(&pmap->pm_active))
1479 		tlb_flush(va);
1480 }
1481 
1482 PMAP_INLINE void
pmap_tlb_flush_range(pmap_t pmap,vm_offset_t sva,vm_size_t size)1483 pmap_tlb_flush_range(pmap_t pmap, vm_offset_t sva, vm_size_t size)
1484 {
1485 
1486 	if (pmap == kernel_pmap || !CPU_EMPTY(&pmap->pm_active))
1487 		tlb_flush_range(sva, size);
1488 }
1489 
1490 /*
1491  *  Abuse the pte2 nodes for unmapped kva to thread a kva freelist through.
1492  *  Requirements:
1493  *   - Must deal with pages in order to ensure that none of the PTE2_* bits
1494  *     are ever set, PTE2_V in particular.
1495  *   - Assumes we can write to pte2s without pte2_store() atomic ops.
1496  *   - Assumes nothing will ever test these addresses for 0 to indicate
1497  *     no mapping instead of correctly checking PTE2_V.
1498  *   - Assumes a vm_offset_t will fit in a pte2 (true for arm).
1499  *  Because PTE2_V is never set, there can be no mappings to invalidate.
1500  */
1501 static vm_offset_t
pmap_pte2list_alloc(vm_offset_t * head)1502 pmap_pte2list_alloc(vm_offset_t *head)
1503 {
1504 	pt2_entry_t *pte2p;
1505 	vm_offset_t va;
1506 
1507 	va = *head;
1508 	if (va == 0)
1509 		panic("pmap_ptelist_alloc: exhausted ptelist KVA");
1510 	pte2p = pt2map_entry(va);
1511 	*head = *pte2p;
1512 	if (*head & PTE2_V)
1513 		panic("%s: va with PTE2_V set!", __func__);
1514 	*pte2p = 0;
1515 	return (va);
1516 }
1517 
1518 static void
pmap_pte2list_free(vm_offset_t * head,vm_offset_t va)1519 pmap_pte2list_free(vm_offset_t *head, vm_offset_t va)
1520 {
1521 	pt2_entry_t *pte2p;
1522 
1523 	if (va & PTE2_V)
1524 		panic("%s: freeing va with PTE2_V set!", __func__);
1525 	pte2p = pt2map_entry(va);
1526 	*pte2p = *head;		/* virtual! PTE2_V is 0 though */
1527 	*head = va;
1528 }
1529 
1530 static void
pmap_pte2list_init(vm_offset_t * head,void * base,int npages)1531 pmap_pte2list_init(vm_offset_t *head, void *base, int npages)
1532 {
1533 	int i;
1534 	vm_offset_t va;
1535 
1536 	*head = 0;
1537 	for (i = npages - 1; i >= 0; i--) {
1538 		va = (vm_offset_t)base + i * PAGE_SIZE;
1539 		pmap_pte2list_free(head, va);
1540 	}
1541 }
1542 
1543 /*****************************************************************************
1544  *
1545  *	PMAP third and final stage initialization.
1546  *
1547  *  After pmap_init() is called, PMAP subsystem is fully initialized.
1548  *
1549  *****************************************************************************/
1550 
1551 SYSCTL_NODE(_vm, OID_AUTO, pmap, CTLFLAG_RD, 0, "VM/pmap parameters");
1552 
1553 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_max, CTLFLAG_RD, &pv_entry_max, 0,
1554     "Max number of PV entries");
1555 SYSCTL_INT(_vm_pmap, OID_AUTO, shpgperproc, CTLFLAG_RD, &shpgperproc, 0,
1556     "Page share factor per proc");
1557 
1558 static u_long nkpt2pg = NKPT2PG;
1559 SYSCTL_ULONG(_vm_pmap, OID_AUTO, nkpt2pg, CTLFLAG_RD,
1560     &nkpt2pg, 0, "Pre-allocated pages for kernel PT2s");
1561 
1562 static int sp_enabled = 1;
1563 SYSCTL_INT(_vm_pmap, OID_AUTO, sp_enabled, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
1564     &sp_enabled, 0, "Are large page mappings enabled?");
1565 
1566 bool
pmap_ps_enabled(pmap_t pmap __unused)1567 pmap_ps_enabled(pmap_t pmap __unused)
1568 {
1569 
1570 	return (sp_enabled != 0);
1571 }
1572 
1573 static SYSCTL_NODE(_vm_pmap, OID_AUTO, pte1, CTLFLAG_RD, 0,
1574     "1MB page mapping counters");
1575 
1576 static u_long pmap_pte1_demotions;
1577 SYSCTL_ULONG(_vm_pmap_pte1, OID_AUTO, demotions, CTLFLAG_RD,
1578     &pmap_pte1_demotions, 0, "1MB page demotions");
1579 
1580 static u_long pmap_pte1_mappings;
1581 SYSCTL_ULONG(_vm_pmap_pte1, OID_AUTO, mappings, CTLFLAG_RD,
1582     &pmap_pte1_mappings, 0, "1MB page mappings");
1583 
1584 static u_long pmap_pte1_p_failures;
1585 SYSCTL_ULONG(_vm_pmap_pte1, OID_AUTO, p_failures, CTLFLAG_RD,
1586     &pmap_pte1_p_failures, 0, "1MB page promotion failures");
1587 
1588 static u_long pmap_pte1_promotions;
1589 SYSCTL_ULONG(_vm_pmap_pte1, OID_AUTO, promotions, CTLFLAG_RD,
1590     &pmap_pte1_promotions, 0, "1MB page promotions");
1591 
1592 static u_long pmap_pte1_kern_demotions;
1593 SYSCTL_ULONG(_vm_pmap_pte1, OID_AUTO, kern_demotions, CTLFLAG_RD,
1594     &pmap_pte1_kern_demotions, 0, "1MB page kernel demotions");
1595 
1596 static u_long pmap_pte1_kern_promotions;
1597 SYSCTL_ULONG(_vm_pmap_pte1, OID_AUTO, kern_promotions, CTLFLAG_RD,
1598     &pmap_pte1_kern_promotions, 0, "1MB page kernel promotions");
1599 
1600 static __inline ttb_entry_t
pmap_ttb_get(pmap_t pmap)1601 pmap_ttb_get(pmap_t pmap)
1602 {
1603 
1604 	return (vtophys(pmap->pm_pt1) | ttb_flags);
1605 }
1606 
1607 /*
1608  *  Initialize a vm_page's machine-dependent fields.
1609  *
1610  *  Variations:
1611  *  1. Pages for L2 page tables are always not managed. So, pv_list and
1612  *     pt2_wirecount can share same physical space. However, proper
1613  *     initialization on a page alloc for page tables and reinitialization
1614  *     on the page free must be ensured.
1615  */
1616 void
pmap_page_init(vm_page_t m)1617 pmap_page_init(vm_page_t m)
1618 {
1619 
1620 	TAILQ_INIT(&m->md.pv_list);
1621 	pt2_wirecount_init(m);
1622 	m->md.pat_mode = VM_MEMATTR_DEFAULT;
1623 }
1624 
1625 /*
1626  *  Virtualization for faster way how to zero whole page.
1627  */
1628 static __inline void
pagezero(void * page)1629 pagezero(void *page)
1630 {
1631 
1632 	bzero(page, PAGE_SIZE);
1633 }
1634 
1635 /*
1636  *  Zero L2 page table page.
1637  *  Use same KVA as in pmap_zero_page().
1638  */
1639 static __inline vm_paddr_t
pmap_pt2pg_zero(vm_page_t m)1640 pmap_pt2pg_zero(vm_page_t m)
1641 {
1642 	pt2_entry_t *cmap2_pte2p;
1643 	vm_paddr_t pa;
1644 	struct pcpu *pc;
1645 
1646 	pa = VM_PAGE_TO_PHYS(m);
1647 
1648 	/*
1649 	 * XXX: For now, we map whole page even if it's already zero,
1650 	 *      to sync it even if the sync is only DSB.
1651 	 */
1652 	sched_pin();
1653 	pc = get_pcpu();
1654 	cmap2_pte2p = pc->pc_cmap2_pte2p;
1655 	mtx_lock(&pc->pc_cmap_lock);
1656 	if (pte2_load(cmap2_pte2p) != 0)
1657 		panic("%s: CMAP2 busy", __func__);
1658 	pte2_store(cmap2_pte2p, PTE2_KERN_NG(pa, PTE2_AP_KRW,
1659 	    vm_page_pte2_attr(m)));
1660 	/*  Even VM_ALLOC_ZERO request is only advisory. */
1661 	if ((m->flags & PG_ZERO) == 0)
1662 		pagezero(pc->pc_cmap2_addr);
1663 	pte2_sync_range((pt2_entry_t *)pc->pc_cmap2_addr, PAGE_SIZE);
1664 	pte2_clear(cmap2_pte2p);
1665 	tlb_flush((vm_offset_t)pc->pc_cmap2_addr);
1666 
1667 	/*
1668 	 * Unpin the thread before releasing the lock.  Otherwise the thread
1669 	 * could be rescheduled while still bound to the current CPU, only
1670 	 * to unpin itself immediately upon resuming execution.
1671 	 */
1672 	sched_unpin();
1673 	mtx_unlock(&pc->pc_cmap_lock);
1674 
1675 	return (pa);
1676 }
1677 
1678 /*
1679  *  Init just allocated page as L2 page table(s) holder
1680  *  and return its physical address.
1681  */
1682 static __inline vm_paddr_t
pmap_pt2pg_init(pmap_t pmap,vm_offset_t va,vm_page_t m)1683 pmap_pt2pg_init(pmap_t pmap, vm_offset_t va, vm_page_t m)
1684 {
1685 	vm_paddr_t pa;
1686 	pt2_entry_t *pte2p;
1687 
1688 	/* Check page attributes. */
1689 	if (m->md.pat_mode != pt_memattr)
1690 		pmap_page_set_memattr(m, pt_memattr);
1691 
1692 	/* Zero page and init wire counts. */
1693 	pa = pmap_pt2pg_zero(m);
1694 	pt2_wirecount_init(m);
1695 
1696 	/*
1697 	 * Map page to PT2MAP address space for given pmap.
1698 	 * Note that PT2MAP space is shared with all pmaps.
1699 	 */
1700 	if (pmap == kernel_pmap)
1701 		pmap_kenter_pt2tab(va, PTE2_KPT(pa));
1702 	else {
1703 		pte2p = pmap_pt2tab_entry(pmap, va);
1704 		pt2tab_store(pte2p, PTE2_KPT_NG(pa));
1705 	}
1706 
1707 	return (pa);
1708 }
1709 
1710 /*
1711  *  Initialize the pmap module.
1712  *  Called by vm_init, to initialize any structures that the pmap
1713  *  system needs to map virtual memory.
1714  */
1715 void
pmap_init(void)1716 pmap_init(void)
1717 {
1718 	vm_size_t s;
1719 	pt2_entry_t *pte2p, pte2;
1720 	u_int i, pte1_idx, pv_npg;
1721 
1722 	PDEBUG(1, printf("%s: phys_start = %#x\n", __func__, PHYSADDR));
1723 
1724 	/*
1725 	 * Initialize the vm page array entries for kernel pmap's
1726 	 * L2 page table pages allocated in advance.
1727 	 */
1728 	pte1_idx = pte1_index(KERNBASE - PT2MAP_SIZE);
1729 	pte2p = kern_pt2tab_entry(KERNBASE - PT2MAP_SIZE);
1730 	for (i = 0; i < nkpt2pg + NPG_IN_PT2TAB; i++, pte2p++) {
1731 		vm_paddr_t pa;
1732 		vm_page_t m;
1733 
1734 		pte2 = pte2_load(pte2p);
1735 		KASSERT(pte2_is_valid(pte2), ("%s: no valid entry", __func__));
1736 
1737 		pa = pte2_pa(pte2);
1738 		m = PHYS_TO_VM_PAGE(pa);
1739 		KASSERT(m >= vm_page_array &&
1740 		    m < &vm_page_array[vm_page_array_size],
1741 		    ("%s: L2 page table page is out of range", __func__));
1742 
1743 		m->pindex = pte1_idx;
1744 		m->phys_addr = pa;
1745 		pte1_idx += NPT2_IN_PG;
1746 	}
1747 
1748 	/*
1749 	 * Initialize the address space (zone) for the pv entries.  Set a
1750 	 * high water mark so that the system can recover from excessive
1751 	 * numbers of pv entries.
1752 	 */
1753 	TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
1754 	pv_entry_max = shpgperproc * maxproc + vm_cnt.v_page_count;
1755 	TUNABLE_INT_FETCH("vm.pmap.pv_entry_max", &pv_entry_max);
1756 	pv_entry_max = roundup(pv_entry_max, _NPCPV);
1757 	pv_entry_high_water = 9 * (pv_entry_max / 10);
1758 
1759 	/*
1760 	 * Are large page mappings enabled?
1761 	 */
1762 	TUNABLE_INT_FETCH("vm.pmap.sp_enabled", &sp_enabled);
1763 	if (sp_enabled) {
1764 		KASSERT(MAXPAGESIZES > 1 && pagesizes[1] == 0,
1765 		    ("%s: can't assign to pagesizes[1]", __func__));
1766 		pagesizes[1] = PTE1_SIZE;
1767 	}
1768 
1769 	/*
1770 	 * Calculate the size of the pv head table for sections.
1771 	 * Handle the possibility that "vm_phys_segs[...].end" is zero.
1772 	 * Note that the table is only for sections which could be promoted.
1773 	 */
1774 	first_managed_pa = pte1_trunc(vm_phys_segs[0].start);
1775 	pv_npg = (pte1_trunc(vm_phys_segs[vm_phys_nsegs - 1].end - PAGE_SIZE)
1776 	    - first_managed_pa) / PTE1_SIZE + 1;
1777 
1778 	/*
1779 	 * Allocate memory for the pv head table for sections.
1780 	 */
1781 	s = (vm_size_t)(pv_npg * sizeof(struct md_page));
1782 	s = round_page(s);
1783 	pv_table = (struct md_page *)kmem_malloc(s, M_WAITOK | M_ZERO);
1784 	for (i = 0; i < pv_npg; i++)
1785 		TAILQ_INIT(&pv_table[i].pv_list);
1786 
1787 	pv_maxchunks = MAX(pv_entry_max / _NPCPV, maxproc);
1788 	pv_chunkbase = (struct pv_chunk *)kva_alloc(PAGE_SIZE * pv_maxchunks);
1789 	if (pv_chunkbase == NULL)
1790 		panic("%s: not enough kvm for pv chunks", __func__);
1791 	pmap_pte2list_init(&pv_vafree, pv_chunkbase, pv_maxchunks);
1792 }
1793 
1794 /*
1795  *  Add a list of wired pages to the kva
1796  *  this routine is only used for temporary
1797  *  kernel mappings that do not need to have
1798  *  page modification or references recorded.
1799  *  Note that old mappings are simply written
1800  *  over.  The page *must* be wired.
1801  *  Note: SMP coherent.  Uses a ranged shootdown IPI.
1802  */
1803 void
pmap_qenter(vm_offset_t sva,vm_page_t * ma,int count)1804 pmap_qenter(vm_offset_t sva, vm_page_t *ma, int count)
1805 {
1806 	u_int anychanged;
1807 	pt2_entry_t *epte2p, *pte2p, pte2;
1808 	vm_page_t m;
1809 	vm_paddr_t pa;
1810 
1811 	anychanged = 0;
1812 	pte2p = pt2map_entry(sva);
1813 	epte2p = pte2p + count;
1814 	while (pte2p < epte2p) {
1815 		m = *ma++;
1816 		pa = VM_PAGE_TO_PHYS(m);
1817 		pte2 = pte2_load(pte2p);
1818 		if ((pte2_pa(pte2) != pa) ||
1819 		    (pte2_attr(pte2) != vm_page_pte2_attr(m))) {
1820 			anychanged++;
1821 			pte2_store(pte2p, PTE2_KERN(pa, PTE2_AP_KRW,
1822 			    vm_page_pte2_attr(m)));
1823 		}
1824 		pte2p++;
1825 	}
1826 	if (__predict_false(anychanged))
1827 		tlb_flush_range(sva, count * PAGE_SIZE);
1828 }
1829 
1830 /*
1831  *  This routine tears out page mappings from the
1832  *  kernel -- it is meant only for temporary mappings.
1833  *  Note: SMP coherent.  Uses a ranged shootdown IPI.
1834  */
1835 void
pmap_qremove(vm_offset_t sva,int count)1836 pmap_qremove(vm_offset_t sva, int count)
1837 {
1838 	vm_offset_t va;
1839 
1840 	va = sva;
1841 	while (count-- > 0) {
1842 		pmap_kremove(va);
1843 		va += PAGE_SIZE;
1844 	}
1845 	tlb_flush_range(sva, va - sva);
1846 }
1847 
1848 /*
1849  *  Are we current address space or kernel?
1850  */
1851 static __inline int
pmap_is_current(pmap_t pmap)1852 pmap_is_current(pmap_t pmap)
1853 {
1854 
1855 	return (pmap == kernel_pmap ||
1856 		(pmap == vmspace_pmap(curthread->td_proc->p_vmspace)));
1857 }
1858 
1859 /*
1860  *  If the given pmap is not the current or kernel pmap, the returned
1861  *  pte2 must be released by passing it to pmap_pte2_release().
1862  */
1863 static pt2_entry_t *
pmap_pte2(pmap_t pmap,vm_offset_t va)1864 pmap_pte2(pmap_t pmap, vm_offset_t va)
1865 {
1866 	pt1_entry_t pte1;
1867 	vm_paddr_t pt2pg_pa;
1868 
1869 	pte1 = pte1_load(pmap_pte1(pmap, va));
1870 	if (pte1_is_section(pte1))
1871 		panic("%s: attempt to map PTE1", __func__);
1872 	if (pte1_is_link(pte1)) {
1873 		/* Are we current address space or kernel? */
1874 		if (pmap_is_current(pmap))
1875 			return (pt2map_entry(va));
1876 		/* Note that L2 page table size is not equal to PAGE_SIZE. */
1877 		pt2pg_pa = trunc_page(pte1_link_pa(pte1));
1878 		mtx_lock(&PMAP2mutex);
1879 		if (pte2_pa(pte2_load(PMAP2)) != pt2pg_pa) {
1880 			pte2_store(PMAP2, PTE2_KPT(pt2pg_pa));
1881 			tlb_flush((vm_offset_t)PADDR2);
1882 		}
1883 		return (PADDR2 + (arm32_btop(va) & (NPTE2_IN_PG - 1)));
1884 	}
1885 	return (NULL);
1886 }
1887 
1888 /*
1889  *  Releases a pte2 that was obtained from pmap_pte2().
1890  *  Be prepared for the pte2p being NULL.
1891  */
1892 static __inline void
pmap_pte2_release(pt2_entry_t * pte2p)1893 pmap_pte2_release(pt2_entry_t *pte2p)
1894 {
1895 
1896 	if ((pt2_entry_t *)(trunc_page((vm_offset_t)pte2p)) == PADDR2) {
1897 		mtx_unlock(&PMAP2mutex);
1898 	}
1899 }
1900 
1901 /*
1902  *  Super fast pmap_pte2 routine best used when scanning
1903  *  the pv lists.  This eliminates many coarse-grained
1904  *  invltlb calls.  Note that many of the pv list
1905  *  scans are across different pmaps.  It is very wasteful
1906  *  to do an entire tlb flush for checking a single mapping.
1907  *
1908  *  If the given pmap is not the current pmap, pvh_global_lock
1909  *  must be held and curthread pinned to a CPU.
1910  */
1911 static pt2_entry_t *
pmap_pte2_quick(pmap_t pmap,vm_offset_t va)1912 pmap_pte2_quick(pmap_t pmap, vm_offset_t va)
1913 {
1914 	pt1_entry_t pte1;
1915 	vm_paddr_t pt2pg_pa;
1916 
1917 	pte1 = pte1_load(pmap_pte1(pmap, va));
1918 	if (pte1_is_section(pte1))
1919 		panic("%s: attempt to map PTE1", __func__);
1920 	if (pte1_is_link(pte1)) {
1921 		/* Are we current address space or kernel? */
1922 		if (pmap_is_current(pmap))
1923 			return (pt2map_entry(va));
1924 		rw_assert(&pvh_global_lock, RA_WLOCKED);
1925 		KASSERT(curthread->td_pinned > 0,
1926 		    ("%s: curthread not pinned", __func__));
1927 		/* Note that L2 page table size is not equal to PAGE_SIZE. */
1928 		pt2pg_pa = trunc_page(pte1_link_pa(pte1));
1929 		if (pte2_pa(pte2_load(PMAP1)) != pt2pg_pa) {
1930 			pte2_store(PMAP1, PTE2_KPT(pt2pg_pa));
1931 #ifdef SMP
1932 			PMAP1cpu = PCPU_GET(cpuid);
1933 #endif
1934 			tlb_flush_local((vm_offset_t)PADDR1);
1935 			PMAP1changed++;
1936 		} else
1937 #ifdef SMP
1938 		if (PMAP1cpu != PCPU_GET(cpuid)) {
1939 			PMAP1cpu = PCPU_GET(cpuid);
1940 			tlb_flush_local((vm_offset_t)PADDR1);
1941 			PMAP1changedcpu++;
1942 		} else
1943 #endif
1944 			PMAP1unchanged++;
1945 		return (PADDR1 + (arm32_btop(va) & (NPTE2_IN_PG - 1)));
1946 	}
1947 	return (NULL);
1948 }
1949 
1950 /*
1951  *  Routine: pmap_extract
1952  *  Function:
1953  * 	Extract the physical page address associated
1954  *	with the given map/virtual_address pair.
1955  */
1956 vm_paddr_t
pmap_extract(pmap_t pmap,vm_offset_t va)1957 pmap_extract(pmap_t pmap, vm_offset_t va)
1958 {
1959 	vm_paddr_t pa;
1960 	pt1_entry_t pte1;
1961 	pt2_entry_t *pte2p;
1962 
1963 	PMAP_LOCK(pmap);
1964 	pte1 = pte1_load(pmap_pte1(pmap, va));
1965 	if (pte1_is_section(pte1))
1966 		pa = pte1_pa(pte1) | (va & PTE1_OFFSET);
1967 	else if (pte1_is_link(pte1)) {
1968 		pte2p = pmap_pte2(pmap, va);
1969 		pa = pte2_pa(pte2_load(pte2p)) | (va & PTE2_OFFSET);
1970 		pmap_pte2_release(pte2p);
1971 	} else
1972 		pa = 0;
1973 	PMAP_UNLOCK(pmap);
1974 	return (pa);
1975 }
1976 
1977 /*
1978  *  Routine: pmap_extract_and_hold
1979  *  Function:
1980  *	Atomically extract and hold the physical page
1981  *	with the given pmap and virtual address pair
1982  *	if that mapping permits the given protection.
1983  */
1984 vm_page_t
pmap_extract_and_hold(pmap_t pmap,vm_offset_t va,vm_prot_t prot)1985 pmap_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
1986 {
1987 	vm_paddr_t pa, lockpa;
1988 	pt1_entry_t pte1;
1989 	pt2_entry_t pte2, *pte2p;
1990 	vm_page_t m;
1991 
1992 	lockpa = 0;
1993 	m = NULL;
1994 	PMAP_LOCK(pmap);
1995 retry:
1996 	pte1 = pte1_load(pmap_pte1(pmap, va));
1997 	if (pte1_is_section(pte1)) {
1998 		if (!(pte1 & PTE1_RO) || !(prot & VM_PROT_WRITE)) {
1999 			pa = pte1_pa(pte1) | (va & PTE1_OFFSET);
2000 			if (vm_page_pa_tryrelock(pmap, pa, &lockpa))
2001 				goto retry;
2002 			m = PHYS_TO_VM_PAGE(pa);
2003 			vm_page_hold(m);
2004 		}
2005 	} else if (pte1_is_link(pte1)) {
2006 		pte2p = pmap_pte2(pmap, va);
2007 		pte2 = pte2_load(pte2p);
2008 		pmap_pte2_release(pte2p);
2009 		if (pte2_is_valid(pte2) &&
2010 		    (!(pte2 & PTE2_RO) || !(prot & VM_PROT_WRITE))) {
2011 			pa = pte2_pa(pte2);
2012 			if (vm_page_pa_tryrelock(pmap, pa, &lockpa))
2013 				goto retry;
2014 			m = PHYS_TO_VM_PAGE(pa);
2015 			vm_page_hold(m);
2016 		}
2017 	}
2018 	PA_UNLOCK_COND(lockpa);
2019 	PMAP_UNLOCK(pmap);
2020 	return (m);
2021 }
2022 
2023 /*
2024  *  Grow the number of kernel L2 page table entries, if needed.
2025  */
2026 void
pmap_growkernel(vm_offset_t addr)2027 pmap_growkernel(vm_offset_t addr)
2028 {
2029 	vm_page_t m;
2030 	vm_paddr_t pt2pg_pa, pt2_pa;
2031 	pt1_entry_t pte1;
2032 	pt2_entry_t pte2;
2033 
2034 	PDEBUG(1, printf("%s: addr = %#x\n", __func__, addr));
2035 	/*
2036 	 * All the time kernel_vm_end is first KVA for which underlying
2037 	 * L2 page table is either not allocated or linked from L1 page table
2038 	 * (not considering sections). Except for two possible cases:
2039 	 *
2040 	 *   (1) in the very beginning as long as pmap_growkernel() was
2041 	 *       not called, it could be first unused KVA (which is not
2042 	 *       rounded up to PTE1_SIZE),
2043 	 *
2044 	 *   (2) when all KVA space is mapped and vm_map_max(kernel_map)
2045 	 *       address is not rounded up to PTE1_SIZE. (For example,
2046 	 *       it could be 0xFFFFFFFF.)
2047 	 */
2048 	kernel_vm_end = pte1_roundup(kernel_vm_end);
2049 	mtx_assert(&kernel_map->system_mtx, MA_OWNED);
2050 	addr = roundup2(addr, PTE1_SIZE);
2051 	if (addr - 1 >= vm_map_max(kernel_map))
2052 		addr = vm_map_max(kernel_map);
2053 	while (kernel_vm_end < addr) {
2054 		pte1 = pte1_load(kern_pte1(kernel_vm_end));
2055 		if (pte1_is_valid(pte1)) {
2056 			kernel_vm_end += PTE1_SIZE;
2057 			if (kernel_vm_end - 1 >= vm_map_max(kernel_map)) {
2058 				kernel_vm_end = vm_map_max(kernel_map);
2059 				break;
2060 			}
2061 			continue;
2062 		}
2063 
2064 		/*
2065 		 * kernel_vm_end_new is used in pmap_pinit() when kernel
2066 		 * mappings are entered to new pmap all at once to avoid race
2067 		 * between pmap_kenter_pte1() and kernel_vm_end increase.
2068 		 * The same aplies to pmap_kenter_pt2tab().
2069 		 */
2070 		kernel_vm_end_new = kernel_vm_end + PTE1_SIZE;
2071 
2072 		pte2 = pt2tab_load(kern_pt2tab_entry(kernel_vm_end));
2073 		if (!pte2_is_valid(pte2)) {
2074 			/*
2075 			 * Install new PT2s page into kernel PT2TAB.
2076 			 */
2077 			m = vm_page_alloc(NULL,
2078 			    pte1_index(kernel_vm_end) & ~PT2PG_MASK,
2079 			    VM_ALLOC_INTERRUPT | VM_ALLOC_NOOBJ |
2080 			    VM_ALLOC_WIRED | VM_ALLOC_ZERO);
2081 			if (m == NULL)
2082 				panic("%s: no memory to grow kernel", __func__);
2083 			/*
2084 			 * QQQ: To link all new L2 page tables from L1 page
2085 			 *      table now and so pmap_kenter_pte1() them
2086 			 *      at once together with pmap_kenter_pt2tab()
2087 			 *      could be nice speed up. However,
2088 			 *      pmap_growkernel() does not happen so often...
2089 			 * QQQ: The other TTBR is another option.
2090 			 */
2091 			pt2pg_pa = pmap_pt2pg_init(kernel_pmap, kernel_vm_end,
2092 			    m);
2093 		} else
2094 			pt2pg_pa = pte2_pa(pte2);
2095 
2096 		pt2_pa = page_pt2pa(pt2pg_pa, pte1_index(kernel_vm_end));
2097 		pmap_kenter_pte1(kernel_vm_end, PTE1_LINK(pt2_pa));
2098 
2099 		kernel_vm_end = kernel_vm_end_new;
2100 		if (kernel_vm_end - 1 >= vm_map_max(kernel_map)) {
2101 			kernel_vm_end = vm_map_max(kernel_map);
2102 			break;
2103 		}
2104 	}
2105 }
2106 
2107 static int
kvm_size(SYSCTL_HANDLER_ARGS)2108 kvm_size(SYSCTL_HANDLER_ARGS)
2109 {
2110 	unsigned long ksize = vm_max_kernel_address - KERNBASE;
2111 
2112 	return (sysctl_handle_long(oidp, &ksize, 0, req));
2113 }
2114 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD,
2115     0, 0, kvm_size, "IU", "Size of KVM");
2116 
2117 static int
kvm_free(SYSCTL_HANDLER_ARGS)2118 kvm_free(SYSCTL_HANDLER_ARGS)
2119 {
2120 	unsigned long kfree = vm_max_kernel_address - kernel_vm_end;
2121 
2122 	return (sysctl_handle_long(oidp, &kfree, 0, req));
2123 }
2124 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD,
2125     0, 0, kvm_free, "IU", "Amount of KVM free");
2126 
2127 /***********************************************
2128  *
2129  *  Pmap allocation/deallocation routines.
2130  *
2131  ***********************************************/
2132 
2133 /*
2134  *  Initialize the pmap for the swapper process.
2135  */
2136 void
pmap_pinit0(pmap_t pmap)2137 pmap_pinit0(pmap_t pmap)
2138 {
2139 	PDEBUG(1, printf("%s: pmap = %p\n", __func__, pmap));
2140 
2141 	PMAP_LOCK_INIT(pmap);
2142 
2143 	/*
2144 	 * Kernel page table directory and pmap stuff around is already
2145 	 * initialized, we are using it right now and here. So, finish
2146 	 * only PMAP structures initialization for process0 ...
2147 	 *
2148 	 * Since the L1 page table and PT2TAB is shared with the kernel pmap,
2149 	 * which is already included in the list "allpmaps", this pmap does
2150 	 * not need to be inserted into that list.
2151 	 */
2152 	pmap->pm_pt1 = kern_pt1;
2153 	pmap->pm_pt2tab = kern_pt2tab;
2154 	CPU_ZERO(&pmap->pm_active);
2155 	PCPU_SET(curpmap, pmap);
2156 	TAILQ_INIT(&pmap->pm_pvchunk);
2157 	bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
2158 	CPU_SET(0, &pmap->pm_active);
2159 }
2160 
2161 static __inline void
pte1_copy_nosync(pt1_entry_t * spte1p,pt1_entry_t * dpte1p,vm_offset_t sva,vm_offset_t eva)2162 pte1_copy_nosync(pt1_entry_t *spte1p, pt1_entry_t *dpte1p, vm_offset_t sva,
2163     vm_offset_t eva)
2164 {
2165 	u_int idx, count;
2166 
2167 	idx = pte1_index(sva);
2168 	count = (pte1_index(eva) - idx + 1) * sizeof(pt1_entry_t);
2169 	bcopy(spte1p + idx, dpte1p + idx, count);
2170 }
2171 
2172 static __inline void
pt2tab_copy_nosync(pt2_entry_t * spte2p,pt2_entry_t * dpte2p,vm_offset_t sva,vm_offset_t eva)2173 pt2tab_copy_nosync(pt2_entry_t *spte2p, pt2_entry_t *dpte2p, vm_offset_t sva,
2174     vm_offset_t eva)
2175 {
2176 	u_int idx, count;
2177 
2178 	idx = pt2tab_index(sva);
2179 	count = (pt2tab_index(eva) - idx + 1) * sizeof(pt2_entry_t);
2180 	bcopy(spte2p + idx, dpte2p + idx, count);
2181 }
2182 
2183 /*
2184  *  Initialize a preallocated and zeroed pmap structure,
2185  *  such as one in a vmspace structure.
2186  */
2187 int
pmap_pinit(pmap_t pmap)2188 pmap_pinit(pmap_t pmap)
2189 {
2190 	pt1_entry_t *pte1p;
2191 	pt2_entry_t *pte2p;
2192 	vm_paddr_t pa, pt2tab_pa;
2193 	u_int i;
2194 
2195 	PDEBUG(6, printf("%s: pmap = %p, pm_pt1 = %p\n", __func__, pmap,
2196 	    pmap->pm_pt1));
2197 
2198 	/*
2199 	 * No need to allocate L2 page table space yet but we do need
2200 	 * a valid L1 page table and PT2TAB table.
2201 	 *
2202 	 * Install shared kernel mappings to these tables. It's a little
2203 	 * tricky as some parts of KVA are reserved for vectors, devices,
2204 	 * and whatever else. These parts are supposed to be above
2205 	 * vm_max_kernel_address. Thus two regions should be installed:
2206 	 *
2207 	 *   (1) <KERNBASE, kernel_vm_end),
2208 	 *   (2) <vm_max_kernel_address, 0xFFFFFFFF>.
2209 	 *
2210 	 * QQQ: The second region should be stable enough to be installed
2211 	 *      only once in time when the tables are allocated.
2212 	 * QQQ: Maybe copy of both regions at once could be faster ...
2213 	 * QQQ: Maybe the other TTBR is an option.
2214 	 *
2215 	 * Finally, install own PT2TAB table to these tables.
2216 	 */
2217 
2218 	if (pmap->pm_pt1 == NULL) {
2219 		pmap->pm_pt1 = (pt1_entry_t *)kmem_alloc_contig(NB_IN_PT1,
2220 		    M_NOWAIT | M_ZERO, 0, -1UL, NB_IN_PT1, 0, pt_memattr);
2221 		if (pmap->pm_pt1 == NULL)
2222 			return (0);
2223 	}
2224 	if (pmap->pm_pt2tab == NULL) {
2225 		/*
2226 		 * QQQ: (1) PT2TAB must be contiguous. If PT2TAB is one page
2227 		 *      only, what should be the only size for 32 bit systems,
2228 		 *      then we could allocate it with vm_page_alloc() and all
2229 		 *      the stuff needed as other L2 page table pages.
2230 		 *      (2) Note that a process PT2TAB is special L2 page table
2231 		 *      page. Its mapping in kernel_arena is permanent and can
2232 		 *      be used no matter which process is current. Its mapping
2233 		 *      in PT2MAP can be used only for current process.
2234 		 */
2235 		pmap->pm_pt2tab = (pt2_entry_t *)kmem_alloc_attr(NB_IN_PT2TAB,
2236 		    M_NOWAIT | M_ZERO, 0, -1UL, pt_memattr);
2237 		if (pmap->pm_pt2tab == NULL) {
2238 			/*
2239 			 * QQQ: As struct pmap is allocated from UMA with
2240 			 *      UMA_ZONE_NOFREE flag, it's important to leave
2241 			 *      no allocation in pmap if initialization failed.
2242 			 */
2243 			kmem_free((vm_offset_t)pmap->pm_pt1, NB_IN_PT1);
2244 			pmap->pm_pt1 = NULL;
2245 			return (0);
2246 		}
2247 		/*
2248 		 * QQQ: Each L2 page table page vm_page_t has pindex set to
2249 		 *      pte1 index of virtual address mapped by this page.
2250 		 *      It's not valid for non kernel PT2TABs themselves.
2251 		 *      The pindex of these pages can not be altered because
2252 		 *      of the way how they are allocated now. However, it
2253 		 *      should not be a problem.
2254 		 */
2255 	}
2256 
2257 	mtx_lock_spin(&allpmaps_lock);
2258 	/*
2259 	 * To avoid race with pmap_kenter_pte1() and pmap_kenter_pt2tab(),
2260 	 * kernel_vm_end_new is used here instead of kernel_vm_end.
2261 	 */
2262 	pte1_copy_nosync(kern_pt1, pmap->pm_pt1, KERNBASE,
2263 	    kernel_vm_end_new - 1);
2264 	pte1_copy_nosync(kern_pt1, pmap->pm_pt1, vm_max_kernel_address,
2265 	    0xFFFFFFFF);
2266 	pt2tab_copy_nosync(kern_pt2tab, pmap->pm_pt2tab, KERNBASE,
2267 	    kernel_vm_end_new - 1);
2268 	pt2tab_copy_nosync(kern_pt2tab, pmap->pm_pt2tab, vm_max_kernel_address,
2269 	    0xFFFFFFFF);
2270 	LIST_INSERT_HEAD(&allpmaps, pmap, pm_list);
2271 	mtx_unlock_spin(&allpmaps_lock);
2272 
2273 	/*
2274 	 * Store PT2MAP PT2 pages (a.k.a. PT2TAB) in PT2TAB itself.
2275 	 * I.e. self reference mapping.  The PT2TAB is private, however mapped
2276 	 * into shared PT2MAP space, so the mapping should be not global.
2277 	 */
2278 	pt2tab_pa = vtophys(pmap->pm_pt2tab);
2279 	pte2p = pmap_pt2tab_entry(pmap, (vm_offset_t)PT2MAP);
2280 	for (pa = pt2tab_pa, i = 0; i < NPG_IN_PT2TAB; i++, pa += PTE2_SIZE) {
2281 		pt2tab_store(pte2p++, PTE2_KPT_NG(pa));
2282 	}
2283 
2284 	/* Insert PT2MAP PT2s into pmap PT1. */
2285 	pte1p = pmap_pte1(pmap, (vm_offset_t)PT2MAP);
2286 	for (pa = pt2tab_pa, i = 0; i < NPT2_IN_PT2TAB; i++, pa += NB_IN_PT2) {
2287 		pte1_store(pte1p++, PTE1_LINK(pa));
2288 	}
2289 
2290 	/*
2291 	 * Now synchronize new mapping which was made above.
2292 	 */
2293 	pte1_sync_range(pmap->pm_pt1, NB_IN_PT1);
2294 	pte2_sync_range(pmap->pm_pt2tab, NB_IN_PT2TAB);
2295 
2296 	CPU_ZERO(&pmap->pm_active);
2297 	TAILQ_INIT(&pmap->pm_pvchunk);
2298 	bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
2299 
2300 	return (1);
2301 }
2302 
2303 #ifdef INVARIANTS
2304 static boolean_t
pt2tab_user_is_empty(pt2_entry_t * tab)2305 pt2tab_user_is_empty(pt2_entry_t *tab)
2306 {
2307 	u_int i, end;
2308 
2309 	end = pt2tab_index(VM_MAXUSER_ADDRESS);
2310 	for (i = 0; i < end; i++)
2311 		if (tab[i] != 0) return (FALSE);
2312 	return (TRUE);
2313 }
2314 #endif
2315 /*
2316  *  Release any resources held by the given physical map.
2317  *  Called when a pmap initialized by pmap_pinit is being released.
2318  *  Should only be called if the map contains no valid mappings.
2319  */
2320 void
pmap_release(pmap_t pmap)2321 pmap_release(pmap_t pmap)
2322 {
2323 #ifdef INVARIANTS
2324 	vm_offset_t start, end;
2325 #endif
2326 	KASSERT(pmap->pm_stats.resident_count == 0,
2327 	    ("%s: pmap resident count %ld != 0", __func__,
2328 	    pmap->pm_stats.resident_count));
2329 	KASSERT(pt2tab_user_is_empty(pmap->pm_pt2tab),
2330 	    ("%s: has allocated user PT2(s)", __func__));
2331 	KASSERT(CPU_EMPTY(&pmap->pm_active),
2332 	    ("%s: pmap %p is active on some CPU(s)", __func__, pmap));
2333 
2334 	mtx_lock_spin(&allpmaps_lock);
2335 	LIST_REMOVE(pmap, pm_list);
2336 	mtx_unlock_spin(&allpmaps_lock);
2337 
2338 #ifdef INVARIANTS
2339 	start = pte1_index(KERNBASE) * sizeof(pt1_entry_t);
2340 	end = (pte1_index(0xFFFFFFFF) + 1) * sizeof(pt1_entry_t);
2341 	bzero((char *)pmap->pm_pt1 + start, end - start);
2342 
2343 	start = pt2tab_index(KERNBASE) * sizeof(pt2_entry_t);
2344 	end = (pt2tab_index(0xFFFFFFFF) + 1) * sizeof(pt2_entry_t);
2345 	bzero((char *)pmap->pm_pt2tab + start, end - start);
2346 #endif
2347 	/*
2348 	 * We are leaving PT1 and PT2TAB allocated on released pmap,
2349 	 * so hopefully UMA vmspace_zone will always be inited with
2350 	 * UMA_ZONE_NOFREE flag.
2351 	 */
2352 }
2353 
2354 /*********************************************************
2355  *
2356  *  L2 table pages and their pages management routines.
2357  *
2358  *********************************************************/
2359 
2360 /*
2361  *  Virtual interface for L2 page table wire counting.
2362  *
2363  *  Each L2 page table in a page has own counter which counts a number of
2364  *  valid mappings in a table. Global page counter counts mappings in all
2365  *  tables in a page plus a single itself mapping in PT2TAB.
2366  *
2367  *  During a promotion we leave the associated L2 page table counter
2368  *  untouched, so the table (strictly speaking a page which holds it)
2369  *  is never freed if promoted.
2370  *
2371  *  If a page m->wire_count == 1 then no valid mappings exist in any L2 page
2372  *  table in the page and the page itself is only mapped in PT2TAB.
2373  */
2374 
2375 static __inline void
pt2_wirecount_init(vm_page_t m)2376 pt2_wirecount_init(vm_page_t m)
2377 {
2378 	u_int i;
2379 
2380 	/*
2381 	 * Note: A page m is allocated with VM_ALLOC_WIRED flag and
2382 	 *       m->wire_count should be already set correctly.
2383 	 *       So, there is no need to set it again herein.
2384 	 */
2385 	for (i = 0; i < NPT2_IN_PG; i++)
2386 		m->md.pt2_wirecount[i] = 0;
2387 }
2388 
2389 static __inline void
pt2_wirecount_inc(vm_page_t m,uint32_t pte1_idx)2390 pt2_wirecount_inc(vm_page_t m, uint32_t pte1_idx)
2391 {
2392 
2393 	/*
2394 	 * Note: A just modificated pte2 (i.e. already allocated)
2395 	 *       is acquiring one extra reference which must be
2396 	 *       explicitly cleared. It influences the KASSERTs herein.
2397 	 *       All L2 page tables in a page always belong to the same
2398 	 *       pmap, so we allow only one extra reference for the page.
2399 	 */
2400 	KASSERT(m->md.pt2_wirecount[pte1_idx & PT2PG_MASK] < (NPTE2_IN_PT2 + 1),
2401 	    ("%s: PT2 is overflowing ...", __func__));
2402 	KASSERT(m->wire_count <= (NPTE2_IN_PG + 1),
2403 	    ("%s: PT2PG is overflowing ...", __func__));
2404 
2405 	m->wire_count++;
2406 	m->md.pt2_wirecount[pte1_idx & PT2PG_MASK]++;
2407 }
2408 
2409 static __inline void
pt2_wirecount_dec(vm_page_t m,uint32_t pte1_idx)2410 pt2_wirecount_dec(vm_page_t m, uint32_t pte1_idx)
2411 {
2412 
2413 	KASSERT(m->md.pt2_wirecount[pte1_idx & PT2PG_MASK] != 0,
2414 	    ("%s: PT2 is underflowing ...", __func__));
2415 	KASSERT(m->wire_count > 1,
2416 	    ("%s: PT2PG is underflowing ...", __func__));
2417 
2418 	m->wire_count--;
2419 	m->md.pt2_wirecount[pte1_idx & PT2PG_MASK]--;
2420 }
2421 
2422 static __inline void
pt2_wirecount_set(vm_page_t m,uint32_t pte1_idx,uint16_t count)2423 pt2_wirecount_set(vm_page_t m, uint32_t pte1_idx, uint16_t count)
2424 {
2425 
2426 	KASSERT(count <= NPTE2_IN_PT2,
2427 	    ("%s: invalid count %u", __func__, count));
2428 	KASSERT(m->wire_count >  m->md.pt2_wirecount[pte1_idx & PT2PG_MASK],
2429 	    ("%s: PT2PG corrupting (%u, %u) ...", __func__, m->wire_count,
2430 	    m->md.pt2_wirecount[pte1_idx & PT2PG_MASK]));
2431 
2432 	m->wire_count -= m->md.pt2_wirecount[pte1_idx & PT2PG_MASK];
2433 	m->wire_count += count;
2434 	m->md.pt2_wirecount[pte1_idx & PT2PG_MASK] = count;
2435 
2436 	KASSERT(m->wire_count <= (NPTE2_IN_PG + 1),
2437 	    ("%s: PT2PG is overflowed (%u) ...", __func__, m->wire_count));
2438 }
2439 
2440 static __inline uint32_t
pt2_wirecount_get(vm_page_t m,uint32_t pte1_idx)2441 pt2_wirecount_get(vm_page_t m, uint32_t pte1_idx)
2442 {
2443 
2444 	return (m->md.pt2_wirecount[pte1_idx & PT2PG_MASK]);
2445 }
2446 
2447 static __inline boolean_t
pt2_is_empty(vm_page_t m,vm_offset_t va)2448 pt2_is_empty(vm_page_t m, vm_offset_t va)
2449 {
2450 
2451 	return (m->md.pt2_wirecount[pte1_index(va) & PT2PG_MASK] == 0);
2452 }
2453 
2454 static __inline boolean_t
pt2_is_full(vm_page_t m,vm_offset_t va)2455 pt2_is_full(vm_page_t m, vm_offset_t va)
2456 {
2457 
2458 	return (m->md.pt2_wirecount[pte1_index(va) & PT2PG_MASK] ==
2459 	    NPTE2_IN_PT2);
2460 }
2461 
2462 static __inline boolean_t
pt2pg_is_empty(vm_page_t m)2463 pt2pg_is_empty(vm_page_t m)
2464 {
2465 
2466 	return (m->wire_count == 1);
2467 }
2468 
2469 /*
2470  *  This routine is called if the L2 page table
2471  *  is not mapped correctly.
2472  */
2473 static vm_page_t
_pmap_allocpte2(pmap_t pmap,vm_offset_t va,u_int flags)2474 _pmap_allocpte2(pmap_t pmap, vm_offset_t va, u_int flags)
2475 {
2476 	uint32_t pte1_idx;
2477 	pt1_entry_t *pte1p;
2478 	pt2_entry_t pte2;
2479 	vm_page_t  m;
2480 	vm_paddr_t pt2pg_pa, pt2_pa;
2481 
2482 	pte1_idx = pte1_index(va);
2483 	pte1p = pmap->pm_pt1 + pte1_idx;
2484 
2485 	KASSERT(pte1_load(pte1p) == 0,
2486 	    ("%s: pm_pt1[%#x] is not zero: %#x", __func__, pte1_idx,
2487 	    pte1_load(pte1p)));
2488 
2489 	pte2 = pt2tab_load(pmap_pt2tab_entry(pmap, va));
2490 	if (!pte2_is_valid(pte2)) {
2491 		/*
2492 		 * Install new PT2s page into pmap PT2TAB.
2493 		 */
2494 		m = vm_page_alloc(NULL, pte1_idx & ~PT2PG_MASK,
2495 		    VM_ALLOC_NOOBJ | VM_ALLOC_WIRED | VM_ALLOC_ZERO);
2496 		if (m == NULL) {
2497 			if ((flags & PMAP_ENTER_NOSLEEP) == 0) {
2498 				PMAP_UNLOCK(pmap);
2499 				rw_wunlock(&pvh_global_lock);
2500 				vm_wait(NULL);
2501 				rw_wlock(&pvh_global_lock);
2502 				PMAP_LOCK(pmap);
2503 			}
2504 
2505 			/*
2506 			 * Indicate the need to retry.  While waiting,
2507 			 * the L2 page table page may have been allocated.
2508 			 */
2509 			return (NULL);
2510 		}
2511 		pmap->pm_stats.resident_count++;
2512 		pt2pg_pa = pmap_pt2pg_init(pmap, va, m);
2513 	} else {
2514 		pt2pg_pa = pte2_pa(pte2);
2515 		m = PHYS_TO_VM_PAGE(pt2pg_pa);
2516 	}
2517 
2518 	pt2_wirecount_inc(m, pte1_idx);
2519 	pt2_pa = page_pt2pa(pt2pg_pa, pte1_idx);
2520 	pte1_store(pte1p, PTE1_LINK(pt2_pa));
2521 
2522 	return (m);
2523 }
2524 
2525 static vm_page_t
pmap_allocpte2(pmap_t pmap,vm_offset_t va,u_int flags)2526 pmap_allocpte2(pmap_t pmap, vm_offset_t va, u_int flags)
2527 {
2528 	u_int pte1_idx;
2529 	pt1_entry_t *pte1p, pte1;
2530 	vm_page_t m;
2531 
2532 	pte1_idx = pte1_index(va);
2533 retry:
2534 	pte1p = pmap->pm_pt1 + pte1_idx;
2535 	pte1 = pte1_load(pte1p);
2536 
2537 	/*
2538 	 * This supports switching from a 1MB page to a
2539 	 * normal 4K page.
2540 	 */
2541 	if (pte1_is_section(pte1)) {
2542 		(void)pmap_demote_pte1(pmap, pte1p, va);
2543 		/*
2544 		 * Reload pte1 after demotion.
2545 		 *
2546 		 * Note: Demotion can even fail as either PT2 is not find for
2547 		 *       the virtual address or PT2PG can not be allocated.
2548 		 */
2549 		pte1 = pte1_load(pte1p);
2550 	}
2551 
2552 	/*
2553 	 * If the L2 page table page is mapped, we just increment the
2554 	 * hold count, and activate it.
2555 	 */
2556 	if (pte1_is_link(pte1)) {
2557 		m = PHYS_TO_VM_PAGE(pte1_link_pa(pte1));
2558 		pt2_wirecount_inc(m, pte1_idx);
2559 	} else  {
2560 		/*
2561 		 * Here if the PT2 isn't mapped, or if it has
2562 		 * been deallocated.
2563 		 */
2564 		m = _pmap_allocpte2(pmap, va, flags);
2565 		if (m == NULL && (flags & PMAP_ENTER_NOSLEEP) == 0)
2566 			goto retry;
2567 	}
2568 
2569 	return (m);
2570 }
2571 
2572 /*
2573  *  Schedule the specified unused L2 page table page to be freed. Specifically,
2574  *  add the page to the specified list of pages that will be released to the
2575  *  physical memory manager after the TLB has been updated.
2576  */
2577 static __inline void
pmap_add_delayed_free_list(vm_page_t m,struct spglist * free)2578 pmap_add_delayed_free_list(vm_page_t m, struct spglist *free)
2579 {
2580 
2581 	/*
2582 	 * Put page on a list so that it is released after
2583 	 * *ALL* TLB shootdown is done
2584 	 */
2585 #ifdef PMAP_DEBUG
2586 	pmap_zero_page_check(m);
2587 #endif
2588 	m->flags |= PG_ZERO;
2589 	SLIST_INSERT_HEAD(free, m, plinks.s.ss);
2590 }
2591 
2592 /*
2593  *  Unwire L2 page tables page.
2594  */
2595 static void
pmap_unwire_pt2pg(pmap_t pmap,vm_offset_t va,vm_page_t m)2596 pmap_unwire_pt2pg(pmap_t pmap, vm_offset_t va, vm_page_t m)
2597 {
2598 	pt1_entry_t *pte1p, opte1 __unused;
2599 	pt2_entry_t *pte2p;
2600 	uint32_t i;
2601 
2602 	KASSERT(pt2pg_is_empty(m),
2603 	    ("%s: pmap %p PT2PG %p wired", __func__, pmap, m));
2604 
2605 	/*
2606 	 * Unmap all L2 page tables in the page from L1 page table.
2607 	 *
2608 	 * QQQ: Individual L2 page tables (except the last one) can be unmapped
2609 	 * earlier. However, we are doing that this way.
2610 	 */
2611 	KASSERT(m->pindex == (pte1_index(va) & ~PT2PG_MASK),
2612 	    ("%s: pmap %p va %#x PT2PG %p bad index", __func__, pmap, va, m));
2613 	pte1p = pmap->pm_pt1 + m->pindex;
2614 	for (i = 0; i < NPT2_IN_PG; i++, pte1p++) {
2615 		KASSERT(m->md.pt2_wirecount[i] == 0,
2616 		    ("%s: pmap %p PT2 %u (PG %p) wired", __func__, pmap, i, m));
2617 		opte1 = pte1_load(pte1p);
2618 		if (pte1_is_link(opte1)) {
2619 			pte1_clear(pte1p);
2620 			/*
2621 			 * Flush intermediate TLB cache.
2622 			 */
2623 			pmap_tlb_flush(pmap, (m->pindex + i) << PTE1_SHIFT);
2624 		}
2625 #ifdef INVARIANTS
2626 		else
2627 			KASSERT((opte1 == 0) || pte1_is_section(opte1),
2628 			    ("%s: pmap %p va %#x bad pte1 %x at %u", __func__,
2629 			    pmap, va, opte1, i));
2630 #endif
2631 	}
2632 
2633 	/*
2634 	 * Unmap the page from PT2TAB.
2635 	 */
2636 	pte2p = pmap_pt2tab_entry(pmap, va);
2637 	(void)pt2tab_load_clear(pte2p);
2638 	pmap_tlb_flush(pmap, pt2map_pt2pg(va));
2639 
2640 	m->wire_count = 0;
2641 	pmap->pm_stats.resident_count--;
2642 
2643 	/*
2644 	 * This barrier is so that the ordinary store unmapping
2645 	 * the L2 page table page is globally performed before TLB shoot-
2646 	 * down is begun.
2647 	 */
2648 	wmb();
2649 	vm_wire_sub(1);
2650 }
2651 
2652 /*
2653  *  Decrements a L2 page table page's wire count, which is used to record the
2654  *  number of valid page table entries within the page.  If the wire count
2655  *  drops to zero, then the page table page is unmapped.  Returns TRUE if the
2656  *  page table page was unmapped and FALSE otherwise.
2657  */
2658 static __inline boolean_t
pmap_unwire_pt2(pmap_t pmap,vm_offset_t va,vm_page_t m,struct spglist * free)2659 pmap_unwire_pt2(pmap_t pmap, vm_offset_t va, vm_page_t m, struct spglist *free)
2660 {
2661 	pt2_wirecount_dec(m, pte1_index(va));
2662 	if (pt2pg_is_empty(m)) {
2663 		/*
2664 		 * QQQ: Wire count is zero, so whole page should be zero and
2665 		 *      we can set PG_ZERO flag to it.
2666 		 *      Note that when promotion is enabled, it takes some
2667 		 *      more efforts. See pmap_unwire_pt2_all() below.
2668 		 */
2669 		pmap_unwire_pt2pg(pmap, va, m);
2670 		pmap_add_delayed_free_list(m, free);
2671 		return (TRUE);
2672 	} else
2673 		return (FALSE);
2674 }
2675 
2676 /*
2677  *  Drop a L2 page table page's wire count at once, which is used to record
2678  *  the number of valid L2 page table entries within the page. If the wire
2679  *  count drops to zero, then the L2 page table page is unmapped.
2680  */
2681 static __inline void
pmap_unwire_pt2_all(pmap_t pmap,vm_offset_t va,vm_page_t m,struct spglist * free)2682 pmap_unwire_pt2_all(pmap_t pmap, vm_offset_t va, vm_page_t m,
2683     struct spglist *free)
2684 {
2685 	u_int pte1_idx = pte1_index(va);
2686 
2687 	KASSERT(m->pindex == (pte1_idx & ~PT2PG_MASK),
2688 		("%s: PT2 page's pindex is wrong", __func__));
2689 	KASSERT(m->wire_count > pt2_wirecount_get(m, pte1_idx),
2690 	    ("%s: bad pt2 wire count %u > %u", __func__, m->wire_count,
2691 	    pt2_wirecount_get(m, pte1_idx)));
2692 
2693 	/*
2694 	 * It's possible that the L2 page table was never used.
2695 	 * It happened in case that a section was created without promotion.
2696 	 */
2697 	if (pt2_is_full(m, va)) {
2698 		pt2_wirecount_set(m, pte1_idx, 0);
2699 
2700 		/*
2701 		 * QQQ: We clear L2 page table now, so when L2 page table page
2702 		 *      is going to be freed, we can set it PG_ZERO flag ...
2703 		 *      This function is called only on section mappings, so
2704 		 *      hopefully it's not to big overload.
2705 		 *
2706 		 * XXX: If pmap is current, existing PT2MAP mapping could be
2707 		 *      used for zeroing.
2708 		 */
2709 		pmap_zero_page_area(m, page_pt2off(pte1_idx), NB_IN_PT2);
2710 	}
2711 #ifdef INVARIANTS
2712 	else
2713 		KASSERT(pt2_is_empty(m, va), ("%s: PT2 is not empty (%u)",
2714 		    __func__, pt2_wirecount_get(m, pte1_idx)));
2715 #endif
2716 	if (pt2pg_is_empty(m)) {
2717 		pmap_unwire_pt2pg(pmap, va, m);
2718 		pmap_add_delayed_free_list(m, free);
2719 	}
2720 }
2721 
2722 /*
2723  *  After removing a L2 page table entry, this routine is used to
2724  *  conditionally free the page, and manage the hold/wire counts.
2725  */
2726 static boolean_t
pmap_unuse_pt2(pmap_t pmap,vm_offset_t va,struct spglist * free)2727 pmap_unuse_pt2(pmap_t pmap, vm_offset_t va, struct spglist *free)
2728 {
2729 	pt1_entry_t pte1;
2730 	vm_page_t mpte;
2731 
2732 	if (va >= VM_MAXUSER_ADDRESS)
2733 		return (FALSE);
2734 	pte1 = pte1_load(pmap_pte1(pmap, va));
2735 	mpte = PHYS_TO_VM_PAGE(pte1_link_pa(pte1));
2736 	return (pmap_unwire_pt2(pmap, va, mpte, free));
2737 }
2738 
2739 /*************************************
2740  *
2741  *  Page management routines.
2742  *
2743  *************************************/
2744 
2745 CTASSERT(sizeof(struct pv_chunk) == PAGE_SIZE);
2746 CTASSERT(_NPCM == 11);
2747 CTASSERT(_NPCPV == 336);
2748 
2749 static __inline struct pv_chunk *
pv_to_chunk(pv_entry_t pv)2750 pv_to_chunk(pv_entry_t pv)
2751 {
2752 
2753 	return ((struct pv_chunk *)((uintptr_t)pv & ~(uintptr_t)PAGE_MASK));
2754 }
2755 
2756 #define PV_PMAP(pv) (pv_to_chunk(pv)->pc_pmap)
2757 
2758 #define	PC_FREE0_9	0xfffffffful	/* Free values for index 0 through 9 */
2759 #define	PC_FREE10	0x0000fffful	/* Free values for index 10 */
2760 
2761 static const uint32_t pc_freemask[_NPCM] = {
2762 	PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
2763 	PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
2764 	PC_FREE0_9, PC_FREE0_9, PC_FREE0_9,
2765 	PC_FREE0_9, PC_FREE10
2766 };
2767 
2768 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_count, CTLFLAG_RD, &pv_entry_count, 0,
2769 	"Current number of pv entries");
2770 
2771 #ifdef PV_STATS
2772 static int pc_chunk_count, pc_chunk_allocs, pc_chunk_frees, pc_chunk_tryfail;
2773 
2774 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_count, CTLFLAG_RD, &pc_chunk_count, 0,
2775     "Current number of pv entry chunks");
2776 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_allocs, CTLFLAG_RD, &pc_chunk_allocs, 0,
2777     "Current number of pv entry chunks allocated");
2778 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_frees, CTLFLAG_RD, &pc_chunk_frees, 0,
2779     "Current number of pv entry chunks frees");
2780 SYSCTL_INT(_vm_pmap, OID_AUTO, pc_chunk_tryfail, CTLFLAG_RD, &pc_chunk_tryfail,
2781     0, "Number of times tried to get a chunk page but failed.");
2782 
2783 static long pv_entry_frees, pv_entry_allocs;
2784 static int pv_entry_spare;
2785 
2786 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_frees, CTLFLAG_RD, &pv_entry_frees, 0,
2787     "Current number of pv entry frees");
2788 SYSCTL_LONG(_vm_pmap, OID_AUTO, pv_entry_allocs, CTLFLAG_RD, &pv_entry_allocs,
2789     0, "Current number of pv entry allocs");
2790 SYSCTL_INT(_vm_pmap, OID_AUTO, pv_entry_spare, CTLFLAG_RD, &pv_entry_spare, 0,
2791     "Current number of spare pv entries");
2792 #endif
2793 
2794 /*
2795  *  Is given page managed?
2796  */
2797 static __inline bool
is_managed(vm_paddr_t pa)2798 is_managed(vm_paddr_t pa)
2799 {
2800 	vm_page_t m;
2801 
2802 	m = PHYS_TO_VM_PAGE(pa);
2803 	if (m == NULL)
2804 		return (false);
2805 	return ((m->oflags & VPO_UNMANAGED) == 0);
2806 }
2807 
2808 static __inline bool
pte1_is_managed(pt1_entry_t pte1)2809 pte1_is_managed(pt1_entry_t pte1)
2810 {
2811 
2812 	return (is_managed(pte1_pa(pte1)));
2813 }
2814 
2815 static __inline bool
pte2_is_managed(pt2_entry_t pte2)2816 pte2_is_managed(pt2_entry_t pte2)
2817 {
2818 
2819 	return (is_managed(pte2_pa(pte2)));
2820 }
2821 
2822 /*
2823  *  We are in a serious low memory condition.  Resort to
2824  *  drastic measures to free some pages so we can allocate
2825  *  another pv entry chunk.
2826  */
2827 static vm_page_t
pmap_pv_reclaim(pmap_t locked_pmap)2828 pmap_pv_reclaim(pmap_t locked_pmap)
2829 {
2830 	struct pch newtail;
2831 	struct pv_chunk *pc;
2832 	struct md_page *pvh;
2833 	pt1_entry_t *pte1p;
2834 	pmap_t pmap;
2835 	pt2_entry_t *pte2p, tpte2;
2836 	pv_entry_t pv;
2837 	vm_offset_t va;
2838 	vm_page_t m, m_pc;
2839 	struct spglist free;
2840 	uint32_t inuse;
2841 	int bit, field, freed;
2842 
2843 	PMAP_LOCK_ASSERT(locked_pmap, MA_OWNED);
2844 	pmap = NULL;
2845 	m_pc = NULL;
2846 	SLIST_INIT(&free);
2847 	TAILQ_INIT(&newtail);
2848 	while ((pc = TAILQ_FIRST(&pv_chunks)) != NULL && (pv_vafree == 0 ||
2849 	    SLIST_EMPTY(&free))) {
2850 		TAILQ_REMOVE(&pv_chunks, pc, pc_lru);
2851 		if (pmap != pc->pc_pmap) {
2852 			if (pmap != NULL) {
2853 				if (pmap != locked_pmap)
2854 					PMAP_UNLOCK(pmap);
2855 			}
2856 			pmap = pc->pc_pmap;
2857 			/* Avoid deadlock and lock recursion. */
2858 			if (pmap > locked_pmap)
2859 				PMAP_LOCK(pmap);
2860 			else if (pmap != locked_pmap && !PMAP_TRYLOCK(pmap)) {
2861 				pmap = NULL;
2862 				TAILQ_INSERT_TAIL(&newtail, pc, pc_lru);
2863 				continue;
2864 			}
2865 		}
2866 
2867 		/*
2868 		 * Destroy every non-wired, 4 KB page mapping in the chunk.
2869 		 */
2870 		freed = 0;
2871 		for (field = 0; field < _NPCM; field++) {
2872 			for (inuse = ~pc->pc_map[field] & pc_freemask[field];
2873 			    inuse != 0; inuse &= ~(1UL << bit)) {
2874 				bit = ffs(inuse) - 1;
2875 				pv = &pc->pc_pventry[field * 32 + bit];
2876 				va = pv->pv_va;
2877 				pte1p = pmap_pte1(pmap, va);
2878 				if (pte1_is_section(pte1_load(pte1p)))
2879 					continue;
2880 				pte2p = pmap_pte2(pmap, va);
2881 				tpte2 = pte2_load(pte2p);
2882 				if ((tpte2 & PTE2_W) == 0)
2883 					tpte2 = pte2_load_clear(pte2p);
2884 				pmap_pte2_release(pte2p);
2885 				if ((tpte2 & PTE2_W) != 0)
2886 					continue;
2887 				KASSERT(tpte2 != 0,
2888 				    ("pmap_pv_reclaim: pmap %p va %#x zero pte",
2889 				    pmap, va));
2890 				pmap_tlb_flush(pmap, va);
2891 				m = PHYS_TO_VM_PAGE(pte2_pa(tpte2));
2892 				if (pte2_is_dirty(tpte2))
2893 					vm_page_dirty(m);
2894 				if ((tpte2 & PTE2_A) != 0)
2895 					vm_page_aflag_set(m, PGA_REFERENCED);
2896 				TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
2897 				if (TAILQ_EMPTY(&m->md.pv_list) &&
2898 				    (m->flags & PG_FICTITIOUS) == 0) {
2899 					pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
2900 					if (TAILQ_EMPTY(&pvh->pv_list)) {
2901 						vm_page_aflag_clear(m,
2902 						    PGA_WRITEABLE);
2903 					}
2904 				}
2905 				pc->pc_map[field] |= 1UL << bit;
2906 				pmap_unuse_pt2(pmap, va, &free);
2907 				freed++;
2908 			}
2909 		}
2910 		if (freed == 0) {
2911 			TAILQ_INSERT_TAIL(&newtail, pc, pc_lru);
2912 			continue;
2913 		}
2914 		/* Every freed mapping is for a 4 KB page. */
2915 		pmap->pm_stats.resident_count -= freed;
2916 		PV_STAT(pv_entry_frees += freed);
2917 		PV_STAT(pv_entry_spare += freed);
2918 		pv_entry_count -= freed;
2919 		TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
2920 		for (field = 0; field < _NPCM; field++)
2921 			if (pc->pc_map[field] != pc_freemask[field]) {
2922 				TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc,
2923 				    pc_list);
2924 				TAILQ_INSERT_TAIL(&newtail, pc, pc_lru);
2925 
2926 				/*
2927 				 * One freed pv entry in locked_pmap is
2928 				 * sufficient.
2929 				 */
2930 				if (pmap == locked_pmap)
2931 					goto out;
2932 				break;
2933 			}
2934 		if (field == _NPCM) {
2935 			PV_STAT(pv_entry_spare -= _NPCPV);
2936 			PV_STAT(pc_chunk_count--);
2937 			PV_STAT(pc_chunk_frees++);
2938 			/* Entire chunk is free; return it. */
2939 			m_pc = PHYS_TO_VM_PAGE(pmap_kextract((vm_offset_t)pc));
2940 			pmap_qremove((vm_offset_t)pc, 1);
2941 			pmap_pte2list_free(&pv_vafree, (vm_offset_t)pc);
2942 			break;
2943 		}
2944 	}
2945 out:
2946 	TAILQ_CONCAT(&pv_chunks, &newtail, pc_lru);
2947 	if (pmap != NULL) {
2948 		if (pmap != locked_pmap)
2949 			PMAP_UNLOCK(pmap);
2950 	}
2951 	if (m_pc == NULL && pv_vafree != 0 && SLIST_EMPTY(&free)) {
2952 		m_pc = SLIST_FIRST(&free);
2953 		SLIST_REMOVE_HEAD(&free, plinks.s.ss);
2954 		/* Recycle a freed page table page. */
2955 		m_pc->wire_count = 1;
2956 		vm_wire_add(1);
2957 	}
2958 	vm_page_free_pages_toq(&free, false);
2959 	return (m_pc);
2960 }
2961 
2962 static void
free_pv_chunk(struct pv_chunk * pc)2963 free_pv_chunk(struct pv_chunk *pc)
2964 {
2965 	vm_page_t m;
2966 
2967 	TAILQ_REMOVE(&pv_chunks, pc, pc_lru);
2968 	PV_STAT(pv_entry_spare -= _NPCPV);
2969 	PV_STAT(pc_chunk_count--);
2970 	PV_STAT(pc_chunk_frees++);
2971 	/* entire chunk is free, return it */
2972 	m = PHYS_TO_VM_PAGE(pmap_kextract((vm_offset_t)pc));
2973 	pmap_qremove((vm_offset_t)pc, 1);
2974 	vm_page_unwire_noq(m);
2975 	vm_page_free(m);
2976 	pmap_pte2list_free(&pv_vafree, (vm_offset_t)pc);
2977 }
2978 
2979 /*
2980  *  Free the pv_entry back to the free list.
2981  */
2982 static void
free_pv_entry(pmap_t pmap,pv_entry_t pv)2983 free_pv_entry(pmap_t pmap, pv_entry_t pv)
2984 {
2985 	struct pv_chunk *pc;
2986 	int idx, field, bit;
2987 
2988 	rw_assert(&pvh_global_lock, RA_WLOCKED);
2989 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
2990 	PV_STAT(pv_entry_frees++);
2991 	PV_STAT(pv_entry_spare++);
2992 	pv_entry_count--;
2993 	pc = pv_to_chunk(pv);
2994 	idx = pv - &pc->pc_pventry[0];
2995 	field = idx / 32;
2996 	bit = idx % 32;
2997 	pc->pc_map[field] |= 1ul << bit;
2998 	for (idx = 0; idx < _NPCM; idx++)
2999 		if (pc->pc_map[idx] != pc_freemask[idx]) {
3000 			/*
3001 			 * 98% of the time, pc is already at the head of the
3002 			 * list.  If it isn't already, move it to the head.
3003 			 */
3004 			if (__predict_false(TAILQ_FIRST(&pmap->pm_pvchunk) !=
3005 			    pc)) {
3006 				TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
3007 				TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc,
3008 				    pc_list);
3009 			}
3010 			return;
3011 		}
3012 	TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
3013 	free_pv_chunk(pc);
3014 }
3015 
3016 /*
3017  *  Get a new pv_entry, allocating a block from the system
3018  *  when needed.
3019  */
3020 static pv_entry_t
get_pv_entry(pmap_t pmap,boolean_t try)3021 get_pv_entry(pmap_t pmap, boolean_t try)
3022 {
3023 	static const struct timeval printinterval = { 60, 0 };
3024 	static struct timeval lastprint;
3025 	int bit, field;
3026 	pv_entry_t pv;
3027 	struct pv_chunk *pc;
3028 	vm_page_t m;
3029 
3030 	rw_assert(&pvh_global_lock, RA_WLOCKED);
3031 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3032 	PV_STAT(pv_entry_allocs++);
3033 	pv_entry_count++;
3034 	if (pv_entry_count > pv_entry_high_water)
3035 		if (ratecheck(&lastprint, &printinterval))
3036 			printf("Approaching the limit on PV entries, consider "
3037 			    "increasing either the vm.pmap.shpgperproc or the "
3038 			    "vm.pmap.pv_entry_max tunable.\n");
3039 retry:
3040 	pc = TAILQ_FIRST(&pmap->pm_pvchunk);
3041 	if (pc != NULL) {
3042 		for (field = 0; field < _NPCM; field++) {
3043 			if (pc->pc_map[field]) {
3044 				bit = ffs(pc->pc_map[field]) - 1;
3045 				break;
3046 			}
3047 		}
3048 		if (field < _NPCM) {
3049 			pv = &pc->pc_pventry[field * 32 + bit];
3050 			pc->pc_map[field] &= ~(1ul << bit);
3051 			/* If this was the last item, move it to tail */
3052 			for (field = 0; field < _NPCM; field++)
3053 				if (pc->pc_map[field] != 0) {
3054 					PV_STAT(pv_entry_spare--);
3055 					return (pv);	/* not full, return */
3056 				}
3057 			TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
3058 			TAILQ_INSERT_TAIL(&pmap->pm_pvchunk, pc, pc_list);
3059 			PV_STAT(pv_entry_spare--);
3060 			return (pv);
3061 		}
3062 	}
3063 	/*
3064 	 * Access to the pte2list "pv_vafree" is synchronized by the pvh
3065 	 * global lock.  If "pv_vafree" is currently non-empty, it will
3066 	 * remain non-empty until pmap_pte2list_alloc() completes.
3067 	 */
3068 	if (pv_vafree == 0 || (m = vm_page_alloc(NULL, 0, VM_ALLOC_NORMAL |
3069 	    VM_ALLOC_NOOBJ | VM_ALLOC_WIRED)) == NULL) {
3070 		if (try) {
3071 			pv_entry_count--;
3072 			PV_STAT(pc_chunk_tryfail++);
3073 			return (NULL);
3074 		}
3075 		m = pmap_pv_reclaim(pmap);
3076 		if (m == NULL)
3077 			goto retry;
3078 	}
3079 	PV_STAT(pc_chunk_count++);
3080 	PV_STAT(pc_chunk_allocs++);
3081 	pc = (struct pv_chunk *)pmap_pte2list_alloc(&pv_vafree);
3082 	pmap_qenter((vm_offset_t)pc, &m, 1);
3083 	pc->pc_pmap = pmap;
3084 	pc->pc_map[0] = pc_freemask[0] & ~1ul;	/* preallocated bit 0 */
3085 	for (field = 1; field < _NPCM; field++)
3086 		pc->pc_map[field] = pc_freemask[field];
3087 	TAILQ_INSERT_TAIL(&pv_chunks, pc, pc_lru);
3088 	pv = &pc->pc_pventry[0];
3089 	TAILQ_INSERT_HEAD(&pmap->pm_pvchunk, pc, pc_list);
3090 	PV_STAT(pv_entry_spare += _NPCPV - 1);
3091 	return (pv);
3092 }
3093 
3094 /*
3095  *  Create a pv entry for page at pa for
3096  *  (pmap, va).
3097  */
3098 static void
pmap_insert_entry(pmap_t pmap,vm_offset_t va,vm_page_t m)3099 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t m)
3100 {
3101 	pv_entry_t pv;
3102 
3103 	rw_assert(&pvh_global_lock, RA_WLOCKED);
3104 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3105 	pv = get_pv_entry(pmap, FALSE);
3106 	pv->pv_va = va;
3107 	TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
3108 }
3109 
3110 static __inline pv_entry_t
pmap_pvh_remove(struct md_page * pvh,pmap_t pmap,vm_offset_t va)3111 pmap_pvh_remove(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
3112 {
3113 	pv_entry_t pv;
3114 
3115 	rw_assert(&pvh_global_lock, RA_WLOCKED);
3116 	TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
3117 		if (pmap == PV_PMAP(pv) && va == pv->pv_va) {
3118 			TAILQ_REMOVE(&pvh->pv_list, pv, pv_next);
3119 			break;
3120 		}
3121 	}
3122 	return (pv);
3123 }
3124 
3125 static void
pmap_pvh_free(struct md_page * pvh,pmap_t pmap,vm_offset_t va)3126 pmap_pvh_free(struct md_page *pvh, pmap_t pmap, vm_offset_t va)
3127 {
3128 	pv_entry_t pv;
3129 
3130 	pv = pmap_pvh_remove(pvh, pmap, va);
3131 	KASSERT(pv != NULL, ("pmap_pvh_free: pv not found"));
3132 	free_pv_entry(pmap, pv);
3133 }
3134 
3135 static void
pmap_remove_entry(pmap_t pmap,vm_page_t m,vm_offset_t va)3136 pmap_remove_entry(pmap_t pmap, vm_page_t m, vm_offset_t va)
3137 {
3138 	struct md_page *pvh;
3139 
3140 	rw_assert(&pvh_global_lock, RA_WLOCKED);
3141 	pmap_pvh_free(&m->md, pmap, va);
3142 	if (TAILQ_EMPTY(&m->md.pv_list) && (m->flags & PG_FICTITIOUS) == 0) {
3143 		pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
3144 		if (TAILQ_EMPTY(&pvh->pv_list))
3145 			vm_page_aflag_clear(m, PGA_WRITEABLE);
3146 	}
3147 }
3148 
3149 static void
pmap_pv_demote_pte1(pmap_t pmap,vm_offset_t va,vm_paddr_t pa)3150 pmap_pv_demote_pte1(pmap_t pmap, vm_offset_t va, vm_paddr_t pa)
3151 {
3152 	struct md_page *pvh;
3153 	pv_entry_t pv;
3154 	vm_offset_t va_last;
3155 	vm_page_t m;
3156 
3157 	rw_assert(&pvh_global_lock, RA_WLOCKED);
3158 	KASSERT((pa & PTE1_OFFSET) == 0,
3159 	    ("pmap_pv_demote_pte1: pa is not 1mpage aligned"));
3160 
3161 	/*
3162 	 * Transfer the 1mpage's pv entry for this mapping to the first
3163 	 * page's pv list.
3164 	 */
3165 	pvh = pa_to_pvh(pa);
3166 	va = pte1_trunc(va);
3167 	pv = pmap_pvh_remove(pvh, pmap, va);
3168 	KASSERT(pv != NULL, ("pmap_pv_demote_pte1: pv not found"));
3169 	m = PHYS_TO_VM_PAGE(pa);
3170 	TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
3171 	/* Instantiate the remaining NPTE2_IN_PT2 - 1 pv entries. */
3172 	va_last = va + PTE1_SIZE - PAGE_SIZE;
3173 	do {
3174 		m++;
3175 		KASSERT((m->oflags & VPO_UNMANAGED) == 0,
3176 		    ("pmap_pv_demote_pte1: page %p is not managed", m));
3177 		va += PAGE_SIZE;
3178 		pmap_insert_entry(pmap, va, m);
3179 	} while (va < va_last);
3180 }
3181 
3182 #if VM_NRESERVLEVEL > 0
3183 static void
pmap_pv_promote_pte1(pmap_t pmap,vm_offset_t va,vm_paddr_t pa)3184 pmap_pv_promote_pte1(pmap_t pmap, vm_offset_t va, vm_paddr_t pa)
3185 {
3186 	struct md_page *pvh;
3187 	pv_entry_t pv;
3188 	vm_offset_t va_last;
3189 	vm_page_t m;
3190 
3191 	rw_assert(&pvh_global_lock, RA_WLOCKED);
3192 	KASSERT((pa & PTE1_OFFSET) == 0,
3193 	    ("pmap_pv_promote_pte1: pa is not 1mpage aligned"));
3194 
3195 	/*
3196 	 * Transfer the first page's pv entry for this mapping to the
3197 	 * 1mpage's pv list.  Aside from avoiding the cost of a call
3198 	 * to get_pv_entry(), a transfer avoids the possibility that
3199 	 * get_pv_entry() calls pmap_pv_reclaim() and that pmap_pv_reclaim()
3200 	 * removes one of the mappings that is being promoted.
3201 	 */
3202 	m = PHYS_TO_VM_PAGE(pa);
3203 	va = pte1_trunc(va);
3204 	pv = pmap_pvh_remove(&m->md, pmap, va);
3205 	KASSERT(pv != NULL, ("pmap_pv_promote_pte1: pv not found"));
3206 	pvh = pa_to_pvh(pa);
3207 	TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_next);
3208 	/* Free the remaining NPTE2_IN_PT2 - 1 pv entries. */
3209 	va_last = va + PTE1_SIZE - PAGE_SIZE;
3210 	do {
3211 		m++;
3212 		va += PAGE_SIZE;
3213 		pmap_pvh_free(&m->md, pmap, va);
3214 	} while (va < va_last);
3215 }
3216 #endif
3217 
3218 /*
3219  *  Conditionally create a pv entry.
3220  */
3221 static boolean_t
pmap_try_insert_pv_entry(pmap_t pmap,vm_offset_t va,vm_page_t m)3222 pmap_try_insert_pv_entry(pmap_t pmap, vm_offset_t va, vm_page_t m)
3223 {
3224 	pv_entry_t pv;
3225 
3226 	rw_assert(&pvh_global_lock, RA_WLOCKED);
3227 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3228 	if (pv_entry_count < pv_entry_high_water &&
3229 	    (pv = get_pv_entry(pmap, TRUE)) != NULL) {
3230 		pv->pv_va = va;
3231 		TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
3232 		return (TRUE);
3233 	} else
3234 		return (FALSE);
3235 }
3236 
3237 /*
3238  *  Create the pv entries for each of the pages within a section.
3239  */
3240 static bool
pmap_pv_insert_pte1(pmap_t pmap,vm_offset_t va,pt1_entry_t pte1,u_int flags)3241 pmap_pv_insert_pte1(pmap_t pmap, vm_offset_t va, pt1_entry_t pte1, u_int flags)
3242 {
3243 	struct md_page *pvh;
3244 	pv_entry_t pv;
3245 	bool noreclaim;
3246 
3247 	rw_assert(&pvh_global_lock, RA_WLOCKED);
3248 	noreclaim = (flags & PMAP_ENTER_NORECLAIM) != 0;
3249 	if ((noreclaim && pv_entry_count >= pv_entry_high_water) ||
3250 	    (pv = get_pv_entry(pmap, noreclaim)) == NULL)
3251 		return (false);
3252 	pv->pv_va = va;
3253 	pvh = pa_to_pvh(pte1_pa(pte1));
3254 	TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_next);
3255 	return (true);
3256 }
3257 
3258 static inline void
pmap_tlb_flush_pte1(pmap_t pmap,vm_offset_t va,pt1_entry_t npte1)3259 pmap_tlb_flush_pte1(pmap_t pmap, vm_offset_t va, pt1_entry_t npte1)
3260 {
3261 
3262 	/* Kill all the small mappings or the big one only. */
3263 	if (pte1_is_section(npte1))
3264 		pmap_tlb_flush_range(pmap, pte1_trunc(va), PTE1_SIZE);
3265 	else
3266 		pmap_tlb_flush(pmap, pte1_trunc(va));
3267 }
3268 
3269 /*
3270  *  Update kernel pte1 on all pmaps.
3271  *
3272  *  The following function is called only on one cpu with disabled interrupts.
3273  *  In SMP case, smp_rendezvous_cpus() is used to stop other cpus. This way
3274  *  nobody can invoke explicit hardware table walk during the update of pte1.
3275  *  Unsolicited hardware table walk can still happen, invoked by speculative
3276  *  data or instruction prefetch or even by speculative hardware table walk.
3277  *
3278  *  The break-before-make approach should be implemented here. However, it's
3279  *  not so easy to do that for kernel mappings as it would be unhappy to unmap
3280  *  itself unexpectedly but voluntarily.
3281  */
3282 static void
pmap_update_pte1_kernel(vm_offset_t va,pt1_entry_t npte1)3283 pmap_update_pte1_kernel(vm_offset_t va, pt1_entry_t npte1)
3284 {
3285 	pmap_t pmap;
3286 	pt1_entry_t *pte1p;
3287 
3288 	/*
3289 	 * Get current pmap. Interrupts should be disabled here
3290 	 * so PCPU_GET() is done atomically.
3291 	 */
3292 	pmap = PCPU_GET(curpmap);
3293 	if (pmap == NULL)
3294 		pmap = kernel_pmap;
3295 
3296 	/*
3297 	 * (1) Change pte1 on current pmap.
3298 	 * (2) Flush all obsolete TLB entries on current CPU.
3299 	 * (3) Change pte1 on all pmaps.
3300 	 * (4) Flush all obsolete TLB entries on all CPUs in SMP case.
3301 	 */
3302 
3303 	pte1p = pmap_pte1(pmap, va);
3304 	pte1_store(pte1p, npte1);
3305 
3306 	/* Kill all the small mappings or the big one only. */
3307 	if (pte1_is_section(npte1)) {
3308 		pmap_pte1_kern_promotions++;
3309 		tlb_flush_range_local(pte1_trunc(va), PTE1_SIZE);
3310 	} else {
3311 		pmap_pte1_kern_demotions++;
3312 		tlb_flush_local(pte1_trunc(va));
3313 	}
3314 
3315 	/*
3316 	 * In SMP case, this function is called when all cpus are at smp
3317 	 * rendezvous, so there is no need to use 'allpmaps_lock' lock here.
3318 	 * In UP case, the function is called with this lock locked.
3319 	 */
3320 	LIST_FOREACH(pmap, &allpmaps, pm_list) {
3321 		pte1p = pmap_pte1(pmap, va);
3322 		pte1_store(pte1p, npte1);
3323 	}
3324 
3325 #ifdef SMP
3326 	/* Kill all the small mappings or the big one only. */
3327 	if (pte1_is_section(npte1))
3328 		tlb_flush_range(pte1_trunc(va), PTE1_SIZE);
3329 	else
3330 		tlb_flush(pte1_trunc(va));
3331 #endif
3332 }
3333 
3334 #ifdef SMP
3335 struct pte1_action {
3336 	vm_offset_t va;
3337 	pt1_entry_t npte1;
3338 	u_int update;		/* CPU that updates the PTE1 */
3339 };
3340 
3341 static void
pmap_update_pte1_action(void * arg)3342 pmap_update_pte1_action(void *arg)
3343 {
3344 	struct pte1_action *act = arg;
3345 
3346 	if (act->update == PCPU_GET(cpuid))
3347 		pmap_update_pte1_kernel(act->va, act->npte1);
3348 }
3349 
3350 /*
3351  *  Change pte1 on current pmap.
3352  *  Note that kernel pte1 must be changed on all pmaps.
3353  *
3354  *  According to the architecture reference manual published by ARM,
3355  *  the behaviour is UNPREDICTABLE when two or more TLB entries map the same VA.
3356  *  According to this manual, UNPREDICTABLE behaviours must never happen in
3357  *  a viable system. In contrast, on x86 processors, it is not specified which
3358  *  TLB entry mapping the virtual address will be used, but the MMU doesn't
3359  *  generate a bogus translation the way it does on Cortex-A8 rev 2 (Beaglebone
3360  *  Black).
3361  *
3362  *  It's a problem when either promotion or demotion is being done. The pte1
3363  *  update and appropriate TLB flush must be done atomically in general.
3364  */
3365 static void
pmap_change_pte1(pmap_t pmap,pt1_entry_t * pte1p,vm_offset_t va,pt1_entry_t npte1)3366 pmap_change_pte1(pmap_t pmap, pt1_entry_t *pte1p, vm_offset_t va,
3367     pt1_entry_t npte1)
3368 {
3369 
3370 	if (pmap == kernel_pmap) {
3371 		struct pte1_action act;
3372 
3373 		sched_pin();
3374 		act.va = va;
3375 		act.npte1 = npte1;
3376 		act.update = PCPU_GET(cpuid);
3377 		smp_rendezvous_cpus(all_cpus, smp_no_rendezvous_barrier,
3378 		    pmap_update_pte1_action, NULL, &act);
3379 		sched_unpin();
3380 	} else {
3381 		register_t cspr;
3382 
3383 		/*
3384 		 * Use break-before-make approach for changing userland
3385 		 * mappings. It can cause L1 translation aborts on other
3386 		 * cores in SMP case. So, special treatment is implemented
3387 		 * in pmap_fault(). To reduce the likelihood that another core
3388 		 * will be affected by the broken mapping, disable interrupts
3389 		 * until the mapping change is completed.
3390 		 */
3391 		cspr = disable_interrupts(PSR_I | PSR_F);
3392 		pte1_clear(pte1p);
3393 		pmap_tlb_flush_pte1(pmap, va, npte1);
3394 		pte1_store(pte1p, npte1);
3395 		restore_interrupts(cspr);
3396 	}
3397 }
3398 #else
3399 static void
pmap_change_pte1(pmap_t pmap,pt1_entry_t * pte1p,vm_offset_t va,pt1_entry_t npte1)3400 pmap_change_pte1(pmap_t pmap, pt1_entry_t *pte1p, vm_offset_t va,
3401     pt1_entry_t npte1)
3402 {
3403 
3404 	if (pmap == kernel_pmap) {
3405 		mtx_lock_spin(&allpmaps_lock);
3406 		pmap_update_pte1_kernel(va, npte1);
3407 		mtx_unlock_spin(&allpmaps_lock);
3408 	} else {
3409 		register_t cspr;
3410 
3411 		/*
3412 		 * Use break-before-make approach for changing userland
3413 		 * mappings. It's absolutely safe in UP case when interrupts
3414 		 * are disabled.
3415 		 */
3416 		cspr = disable_interrupts(PSR_I | PSR_F);
3417 		pte1_clear(pte1p);
3418 		pmap_tlb_flush_pte1(pmap, va, npte1);
3419 		pte1_store(pte1p, npte1);
3420 		restore_interrupts(cspr);
3421 	}
3422 }
3423 #endif
3424 
3425 #if VM_NRESERVLEVEL > 0
3426 /*
3427  *  Tries to promote the NPTE2_IN_PT2, contiguous 4KB page mappings that are
3428  *  within a single page table page (PT2) to a single 1MB page mapping.
3429  *  For promotion to occur, two conditions must be met: (1) the 4KB page
3430  *  mappings must map aligned, contiguous physical memory and (2) the 4KB page
3431  *  mappings must have identical characteristics.
3432  *
3433  *  Managed (PG_MANAGED) mappings within the kernel address space are not
3434  *  promoted.  The reason is that kernel PTE1s are replicated in each pmap but
3435  *  pmap_remove_write(), pmap_clear_modify(), and pmap_clear_reference() only
3436  *  read the PTE1 from the kernel pmap.
3437  */
3438 static void
pmap_promote_pte1(pmap_t pmap,pt1_entry_t * pte1p,vm_offset_t va)3439 pmap_promote_pte1(pmap_t pmap, pt1_entry_t *pte1p, vm_offset_t va)
3440 {
3441 	pt1_entry_t npte1;
3442 	pt2_entry_t *fpte2p, fpte2, fpte2_fav;
3443 	pt2_entry_t *pte2p, pte2;
3444 	vm_offset_t pteva __unused;
3445 	vm_page_t m __unused;
3446 
3447 	PDEBUG(6, printf("%s(%p): try for va %#x pte1 %#x at %p\n", __func__,
3448 	    pmap, va, pte1_load(pte1p), pte1p));
3449 
3450 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3451 
3452 	/*
3453 	 * Examine the first PTE2 in the specified PT2. Abort if this PTE2 is
3454 	 * either invalid, unused, or does not map the first 4KB physical page
3455 	 * within a 1MB page.
3456 	 */
3457 	fpte2p = pmap_pte2_quick(pmap, pte1_trunc(va));
3458 	fpte2 = pte2_load(fpte2p);
3459 	if ((fpte2 & ((PTE2_FRAME & PTE1_OFFSET) | PTE2_A | PTE2_V)) !=
3460 	    (PTE2_A | PTE2_V)) {
3461 		pmap_pte1_p_failures++;
3462 		CTR3(KTR_PMAP, "%s: failure(1) for va %#x in pmap %p",
3463 		    __func__, va, pmap);
3464 		return;
3465 	}
3466 	if (pte2_is_managed(fpte2) && pmap == kernel_pmap) {
3467 		pmap_pte1_p_failures++;
3468 		CTR3(KTR_PMAP, "%s: failure(2) for va %#x in pmap %p",
3469 		    __func__, va, pmap);
3470 		return;
3471 	}
3472 	if ((fpte2 & (PTE2_NM | PTE2_RO)) == PTE2_NM) {
3473 		/*
3474 		 * When page is not modified, PTE2_RO can be set without
3475 		 * a TLB invalidation.
3476 		 */
3477 		fpte2 |= PTE2_RO;
3478 		pte2_store(fpte2p, fpte2);
3479 	}
3480 
3481 	/*
3482 	 * Examine each of the other PTE2s in the specified PT2. Abort if this
3483 	 * PTE2 maps an unexpected 4KB physical page or does not have identical
3484 	 * characteristics to the first PTE2.
3485 	 */
3486 	fpte2_fav = (fpte2 & (PTE2_FRAME | PTE2_A | PTE2_V));
3487 	fpte2_fav += PTE1_SIZE - PTE2_SIZE; /* examine from the end */
3488 	for (pte2p = fpte2p + NPTE2_IN_PT2 - 1; pte2p > fpte2p; pte2p--) {
3489 		pte2 = pte2_load(pte2p);
3490 		if ((pte2 & (PTE2_FRAME | PTE2_A | PTE2_V)) != fpte2_fav) {
3491 			pmap_pte1_p_failures++;
3492 			CTR3(KTR_PMAP, "%s: failure(3) for va %#x in pmap %p",
3493 			    __func__, va, pmap);
3494 			return;
3495 		}
3496 		if ((pte2 & (PTE2_NM | PTE2_RO)) == PTE2_NM) {
3497 			/*
3498 			 * When page is not modified, PTE2_RO can be set
3499 			 * without a TLB invalidation. See note above.
3500 			 */
3501 			pte2 |= PTE2_RO;
3502 			pte2_store(pte2p, pte2);
3503 			pteva = pte1_trunc(va) | (pte2 & PTE1_OFFSET &
3504 			    PTE2_FRAME);
3505 			CTR3(KTR_PMAP, "%s: protect for va %#x in pmap %p",
3506 			    __func__, pteva, pmap);
3507 		}
3508 		if ((pte2 & PTE2_PROMOTE) != (fpte2 & PTE2_PROMOTE)) {
3509 			pmap_pte1_p_failures++;
3510 			CTR3(KTR_PMAP, "%s: failure(4) for va %#x in pmap %p",
3511 			    __func__, va, pmap);
3512 			return;
3513 		}
3514 
3515 		fpte2_fav -= PTE2_SIZE;
3516 	}
3517 	/*
3518 	 * The page table page in its current state will stay in PT2TAB
3519 	 * until the PTE1 mapping the section is demoted by pmap_demote_pte1()
3520 	 * or destroyed by pmap_remove_pte1().
3521 	 *
3522 	 * Note that L2 page table size is not equal to PAGE_SIZE.
3523 	 */
3524 	m = PHYS_TO_VM_PAGE(trunc_page(pte1_link_pa(pte1_load(pte1p))));
3525 	KASSERT(m >= vm_page_array && m < &vm_page_array[vm_page_array_size],
3526 	    ("%s: PT2 page is out of range", __func__));
3527 	KASSERT(m->pindex == (pte1_index(va) & ~PT2PG_MASK),
3528 	    ("%s: PT2 page's pindex is wrong", __func__));
3529 
3530 	/*
3531 	 * Get pte1 from pte2 format.
3532 	 */
3533 	npte1 = (fpte2 & PTE1_FRAME) | ATTR_TO_L1(fpte2) | PTE1_V;
3534 
3535 	/*
3536 	 * Promote the pv entries.
3537 	 */
3538 	if (pte2_is_managed(fpte2))
3539 		pmap_pv_promote_pte1(pmap, va, pte1_pa(npte1));
3540 
3541 	/*
3542 	 * Promote the mappings.
3543 	 */
3544 	pmap_change_pte1(pmap, pte1p, va, npte1);
3545 
3546 	pmap_pte1_promotions++;
3547 	CTR3(KTR_PMAP, "%s: success for va %#x in pmap %p",
3548 	    __func__, va, pmap);
3549 
3550 	PDEBUG(6, printf("%s(%p): success for va %#x pte1 %#x(%#x) at %p\n",
3551 	    __func__, pmap, va, npte1, pte1_load(pte1p), pte1p));
3552 }
3553 #endif /* VM_NRESERVLEVEL > 0 */
3554 
3555 /*
3556  *  Zero L2 page table page.
3557  */
3558 static __inline void
pmap_clear_pt2(pt2_entry_t * fpte2p)3559 pmap_clear_pt2(pt2_entry_t *fpte2p)
3560 {
3561 	pt2_entry_t *pte2p;
3562 
3563 	for (pte2p = fpte2p; pte2p < fpte2p + NPTE2_IN_PT2; pte2p++)
3564 		pte2_clear(pte2p);
3565 
3566 }
3567 
3568 /*
3569  *  Removes a 1MB page mapping from the kernel pmap.
3570  */
3571 static void
pmap_remove_kernel_pte1(pmap_t pmap,pt1_entry_t * pte1p,vm_offset_t va)3572 pmap_remove_kernel_pte1(pmap_t pmap, pt1_entry_t *pte1p, vm_offset_t va)
3573 {
3574 	vm_page_t m;
3575 	uint32_t pte1_idx;
3576 	pt2_entry_t *fpte2p;
3577 	vm_paddr_t pt2_pa;
3578 
3579 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3580 	m = pmap_pt2_page(pmap, va);
3581 	if (m == NULL)
3582 		/*
3583 		 * QQQ: Is this function called only on promoted pte1?
3584 		 *      We certainly do section mappings directly
3585 		 *      (without promotion) in kernel !!!
3586 		 */
3587 		panic("%s: missing pt2 page", __func__);
3588 
3589 	pte1_idx = pte1_index(va);
3590 
3591 	/*
3592 	 * Initialize the L2 page table.
3593 	 */
3594 	fpte2p = page_pt2(pt2map_pt2pg(va), pte1_idx);
3595 	pmap_clear_pt2(fpte2p);
3596 
3597 	/*
3598 	 * Remove the mapping.
3599 	 */
3600 	pt2_pa = page_pt2pa(VM_PAGE_TO_PHYS(m), pte1_idx);
3601 	pmap_kenter_pte1(va, PTE1_LINK(pt2_pa));
3602 
3603 	/*
3604 	 * QQQ: We do not need to invalidate PT2MAP mapping
3605 	 * as we did not change it. I.e. the L2 page table page
3606 	 * was and still is mapped the same way.
3607 	 */
3608 }
3609 
3610 /*
3611  *  Do the things to unmap a section in a process
3612  */
3613 static void
pmap_remove_pte1(pmap_t pmap,pt1_entry_t * pte1p,vm_offset_t sva,struct spglist * free)3614 pmap_remove_pte1(pmap_t pmap, pt1_entry_t *pte1p, vm_offset_t sva,
3615     struct spglist *free)
3616 {
3617 	pt1_entry_t opte1;
3618 	struct md_page *pvh;
3619 	vm_offset_t eva, va;
3620 	vm_page_t m;
3621 
3622 	PDEBUG(6, printf("%s(%p): va %#x pte1 %#x at %p\n", __func__, pmap, sva,
3623 	    pte1_load(pte1p), pte1p));
3624 
3625 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3626 	KASSERT((sva & PTE1_OFFSET) == 0,
3627 	    ("%s: sva is not 1mpage aligned", __func__));
3628 
3629 	/*
3630 	 * Clear and invalidate the mapping. It should occupy one and only TLB
3631 	 * entry. So, pmap_tlb_flush() called with aligned address should be
3632 	 * sufficient.
3633 	 */
3634 	opte1 = pte1_load_clear(pte1p);
3635 	pmap_tlb_flush(pmap, sva);
3636 
3637 	if (pte1_is_wired(opte1))
3638 		pmap->pm_stats.wired_count -= PTE1_SIZE / PAGE_SIZE;
3639 	pmap->pm_stats.resident_count -= PTE1_SIZE / PAGE_SIZE;
3640 	if (pte1_is_managed(opte1)) {
3641 		pvh = pa_to_pvh(pte1_pa(opte1));
3642 		pmap_pvh_free(pvh, pmap, sva);
3643 		eva = sva + PTE1_SIZE;
3644 		for (va = sva, m = PHYS_TO_VM_PAGE(pte1_pa(opte1));
3645 		    va < eva; va += PAGE_SIZE, m++) {
3646 			if (pte1_is_dirty(opte1))
3647 				vm_page_dirty(m);
3648 			if (opte1 & PTE1_A)
3649 				vm_page_aflag_set(m, PGA_REFERENCED);
3650 			if (TAILQ_EMPTY(&m->md.pv_list) &&
3651 			    TAILQ_EMPTY(&pvh->pv_list))
3652 				vm_page_aflag_clear(m, PGA_WRITEABLE);
3653 		}
3654 	}
3655 	if (pmap == kernel_pmap) {
3656 		/*
3657 		 * L2 page table(s) can't be removed from kernel map as
3658 		 * kernel counts on it (stuff around pmap_growkernel()).
3659 		 */
3660 		 pmap_remove_kernel_pte1(pmap, pte1p, sva);
3661 	} else {
3662 		/*
3663 		 * Get associated L2 page table page.
3664 		 * It's possible that the page was never allocated.
3665 		 */
3666 		m = pmap_pt2_page(pmap, sva);
3667 		if (m != NULL)
3668 			pmap_unwire_pt2_all(pmap, sva, m, free);
3669 	}
3670 }
3671 
3672 /*
3673  *  Fills L2 page table page with mappings to consecutive physical pages.
3674  */
3675 static __inline void
pmap_fill_pt2(pt2_entry_t * fpte2p,pt2_entry_t npte2)3676 pmap_fill_pt2(pt2_entry_t *fpte2p, pt2_entry_t npte2)
3677 {
3678 	pt2_entry_t *pte2p;
3679 
3680 	for (pte2p = fpte2p; pte2p < fpte2p + NPTE2_IN_PT2; pte2p++) {
3681 		pte2_store(pte2p, npte2);
3682 		npte2 += PTE2_SIZE;
3683 	}
3684 }
3685 
3686 /*
3687  *  Tries to demote a 1MB page mapping. If demotion fails, the
3688  *  1MB page mapping is invalidated.
3689  */
3690 static boolean_t
pmap_demote_pte1(pmap_t pmap,pt1_entry_t * pte1p,vm_offset_t va)3691 pmap_demote_pte1(pmap_t pmap, pt1_entry_t *pte1p, vm_offset_t va)
3692 {
3693 	pt1_entry_t opte1, npte1;
3694 	pt2_entry_t *fpte2p, npte2;
3695 	vm_paddr_t pt2pg_pa, pt2_pa;
3696 	vm_page_t m;
3697 	struct spglist free;
3698 	uint32_t pte1_idx, isnew = 0;
3699 
3700 	PDEBUG(6, printf("%s(%p): try for va %#x pte1 %#x at %p\n", __func__,
3701 	    pmap, va, pte1_load(pte1p), pte1p));
3702 
3703 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
3704 
3705 	opte1 = pte1_load(pte1p);
3706 	KASSERT(pte1_is_section(opte1), ("%s: opte1 not a section", __func__));
3707 
3708 	if ((opte1 & PTE1_A) == 0 || (m = pmap_pt2_page(pmap, va)) == NULL) {
3709 		KASSERT(!pte1_is_wired(opte1),
3710 		    ("%s: PT2 page for a wired mapping is missing", __func__));
3711 
3712 		/*
3713 		 * Invalidate the 1MB page mapping and return
3714 		 * "failure" if the mapping was never accessed or the
3715 		 * allocation of the new page table page fails.
3716 		 */
3717 		if ((opte1 & PTE1_A) == 0 || (m = vm_page_alloc(NULL,
3718 		    pte1_index(va) & ~PT2PG_MASK, VM_ALLOC_NOOBJ |
3719 		    VM_ALLOC_NORMAL | VM_ALLOC_WIRED)) == NULL) {
3720 			SLIST_INIT(&free);
3721 			pmap_remove_pte1(pmap, pte1p, pte1_trunc(va), &free);
3722 			vm_page_free_pages_toq(&free, false);
3723 			CTR3(KTR_PMAP, "%s: failure for va %#x in pmap %p",
3724 			    __func__, va, pmap);
3725 			return (FALSE);
3726 		}
3727 		if (va < VM_MAXUSER_ADDRESS)
3728 			pmap->pm_stats.resident_count++;
3729 
3730 		isnew = 1;
3731 
3732 		/*
3733 		 * We init all L2 page tables in the page even if
3734 		 * we are going to change everything for one L2 page
3735 		 * table in a while.
3736 		 */
3737 		pt2pg_pa = pmap_pt2pg_init(pmap, va, m);
3738 	} else {
3739 		if (va < VM_MAXUSER_ADDRESS) {
3740 			if (pt2_is_empty(m, va))
3741 				isnew = 1; /* Demoting section w/o promotion. */
3742 #ifdef INVARIANTS
3743 			else
3744 				KASSERT(pt2_is_full(m, va), ("%s: bad PT2 wire"
3745 				    " count %u", __func__,
3746 				    pt2_wirecount_get(m, pte1_index(va))));
3747 #endif
3748 		}
3749 	}
3750 
3751 	pt2pg_pa = VM_PAGE_TO_PHYS(m);
3752 	pte1_idx = pte1_index(va);
3753 	/*
3754 	 * If the pmap is current, then the PT2MAP can provide access to
3755 	 * the page table page (promoted L2 page tables are not unmapped).
3756 	 * Otherwise, temporarily map the L2 page table page (m) into
3757 	 * the kernel's address space at either PADDR1 or PADDR2.
3758 	 *
3759 	 * Note that L2 page table size is not equal to PAGE_SIZE.
3760 	 */
3761 	if (pmap_is_current(pmap))
3762 		fpte2p = page_pt2(pt2map_pt2pg(va), pte1_idx);
3763 	else if (curthread->td_pinned > 0 && rw_wowned(&pvh_global_lock)) {
3764 		if (pte2_pa(pte2_load(PMAP1)) != pt2pg_pa) {
3765 			pte2_store(PMAP1, PTE2_KPT(pt2pg_pa));
3766 #ifdef SMP
3767 			PMAP1cpu = PCPU_GET(cpuid);
3768 #endif
3769 			tlb_flush_local((vm_offset_t)PADDR1);
3770 			PMAP1changed++;
3771 		} else
3772 #ifdef SMP
3773 		if (PMAP1cpu != PCPU_GET(cpuid)) {
3774 			PMAP1cpu = PCPU_GET(cpuid);
3775 			tlb_flush_local((vm_offset_t)PADDR1);
3776 			PMAP1changedcpu++;
3777 		} else
3778 #endif
3779 			PMAP1unchanged++;
3780 		fpte2p = page_pt2((vm_offset_t)PADDR1, pte1_idx);
3781 	} else {
3782 		mtx_lock(&PMAP2mutex);
3783 		if (pte2_pa(pte2_load(PMAP2)) != pt2pg_pa) {
3784 			pte2_store(PMAP2, PTE2_KPT(pt2pg_pa));
3785 			tlb_flush((vm_offset_t)PADDR2);
3786 		}
3787 		fpte2p = page_pt2((vm_offset_t)PADDR2, pte1_idx);
3788 	}
3789 	pt2_pa = page_pt2pa(pt2pg_pa, pte1_idx);
3790 	npte1 = PTE1_LINK(pt2_pa);
3791 
3792 	KASSERT((opte1 & PTE1_A) != 0,
3793 	    ("%s: opte1 is missing PTE1_A", __func__));
3794 	KASSERT((opte1 & (PTE1_NM | PTE1_RO)) != PTE1_NM,
3795 	    ("%s: opte1 has PTE1_NM", __func__));
3796 
3797 	/*
3798 	 *  Get pte2 from pte1 format.
3799 	*/
3800 	npte2 = pte1_pa(opte1) | ATTR_TO_L2(opte1) | PTE2_V;
3801 
3802 	/*
3803 	 * If the L2 page table page is new, initialize it. If the mapping
3804 	 * has changed attributes, update the page table entries.
3805 	 */
3806 	if (isnew != 0) {
3807 		pt2_wirecount_set(m, pte1_idx, NPTE2_IN_PT2);
3808 		pmap_fill_pt2(fpte2p, npte2);
3809 	} else if ((pte2_load(fpte2p) & PTE2_PROMOTE) !=
3810 		    (npte2 & PTE2_PROMOTE))
3811 		pmap_fill_pt2(fpte2p, npte2);
3812 
3813 	KASSERT(pte2_pa(pte2_load(fpte2p)) == pte2_pa(npte2),
3814 	    ("%s: fpte2p and npte2 map different physical addresses",
3815 	    __func__));
3816 
3817 	if (fpte2p == PADDR2)
3818 		mtx_unlock(&PMAP2mutex);
3819 
3820 	/*
3821 	 * Demote the mapping. This pmap is locked. The old PTE1 has
3822 	 * PTE1_A set. If the old PTE1 has not PTE1_RO set, it also
3823 	 * has not PTE1_NM set. Thus, there is no danger of a race with
3824 	 * another processor changing the setting of PTE1_A and/or PTE1_NM
3825 	 * between the read above and the store below.
3826 	 */
3827 	pmap_change_pte1(pmap, pte1p, va, npte1);
3828 
3829 	/*
3830 	 * Demote the pv entry. This depends on the earlier demotion
3831 	 * of the mapping. Specifically, the (re)creation of a per-
3832 	 * page pv entry might trigger the execution of pmap_pv_reclaim(),
3833 	 * which might reclaim a newly (re)created per-page pv entry
3834 	 * and destroy the associated mapping. In order to destroy
3835 	 * the mapping, the PTE1 must have already changed from mapping
3836 	 * the 1mpage to referencing the page table page.
3837 	 */
3838 	if (pte1_is_managed(opte1))
3839 		pmap_pv_demote_pte1(pmap, va, pte1_pa(opte1));
3840 
3841 	pmap_pte1_demotions++;
3842 	CTR3(KTR_PMAP, "%s: success for va %#x in pmap %p",
3843 	    __func__, va, pmap);
3844 
3845 	PDEBUG(6, printf("%s(%p): success for va %#x pte1 %#x(%#x) at %p\n",
3846 	    __func__, pmap, va, npte1, pte1_load(pte1p), pte1p));
3847 	return (TRUE);
3848 }
3849 
3850 /*
3851  *	Insert the given physical page (p) at
3852  *	the specified virtual address (v) in the
3853  *	target physical map with the protection requested.
3854  *
3855  *	If specified, the page will be wired down, meaning
3856  *	that the related pte can not be reclaimed.
3857  *
3858  *	NB:  This is the only routine which MAY NOT lazy-evaluate
3859  *	or lose information.  That is, this routine must actually
3860  *	insert this page into the given map NOW.
3861  */
3862 int
pmap_enter(pmap_t pmap,vm_offset_t va,vm_page_t m,vm_prot_t prot,u_int flags,int8_t psind)3863 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
3864     u_int flags, int8_t psind)
3865 {
3866 	pt1_entry_t *pte1p;
3867 	pt2_entry_t *pte2p;
3868 	pt2_entry_t npte2, opte2;
3869 	pv_entry_t pv;
3870 	vm_paddr_t opa, pa;
3871 	vm_page_t mpte2, om;
3872 	int rv;
3873 
3874 	va = trunc_page(va);
3875 	KASSERT(va <= vm_max_kernel_address, ("%s: toobig", __func__));
3876 	KASSERT(va < UPT2V_MIN_ADDRESS || va >= UPT2V_MAX_ADDRESS,
3877 	    ("%s: invalid to pmap_enter page table pages (va: 0x%x)", __func__,
3878 	    va));
3879 	KASSERT((m->oflags & VPO_UNMANAGED) != 0 || va < kmi.clean_sva ||
3880 	    va >= kmi.clean_eva,
3881 	    ("%s: managed mapping within the clean submap", __func__));
3882 	if ((m->oflags & VPO_UNMANAGED) == 0 && !vm_page_xbusied(m))
3883 		VM_OBJECT_ASSERT_LOCKED(m->object);
3884 	KASSERT((flags & PMAP_ENTER_RESERVED) == 0,
3885 	    ("%s: flags %u has reserved bits set", __func__, flags));
3886 	pa = VM_PAGE_TO_PHYS(m);
3887 	npte2 = PTE2(pa, PTE2_A, vm_page_pte2_attr(m));
3888 	if ((flags & VM_PROT_WRITE) == 0)
3889 		npte2 |= PTE2_NM;
3890 	if ((prot & VM_PROT_WRITE) == 0)
3891 		npte2 |= PTE2_RO;
3892 	KASSERT((npte2 & (PTE2_NM | PTE2_RO)) != PTE2_RO,
3893 	    ("%s: flags includes VM_PROT_WRITE but prot doesn't", __func__));
3894 	if ((prot & VM_PROT_EXECUTE) == 0)
3895 		npte2 |= PTE2_NX;
3896 	if ((flags & PMAP_ENTER_WIRED) != 0)
3897 		npte2 |= PTE2_W;
3898 	if (va < VM_MAXUSER_ADDRESS)
3899 		npte2 |= PTE2_U;
3900 	if (pmap != kernel_pmap)
3901 		npte2 |= PTE2_NG;
3902 
3903 	rw_wlock(&pvh_global_lock);
3904 	PMAP_LOCK(pmap);
3905 	sched_pin();
3906 	if (psind == 1) {
3907 		/* Assert the required virtual and physical alignment. */
3908 		KASSERT((va & PTE1_OFFSET) == 0,
3909 		    ("%s: va unaligned", __func__));
3910 		KASSERT(m->psind > 0, ("%s: m->psind < psind", __func__));
3911 		rv = pmap_enter_pte1(pmap, va, PTE1_PA(pa) | ATTR_TO_L1(npte2) |
3912 		    PTE1_V, flags, m);
3913 		goto out;
3914 	}
3915 
3916 	/*
3917 	 * In the case that a page table page is not
3918 	 * resident, we are creating it here.
3919 	 */
3920 	if (va < VM_MAXUSER_ADDRESS) {
3921 		mpte2 = pmap_allocpte2(pmap, va, flags);
3922 		if (mpte2 == NULL) {
3923 			KASSERT((flags & PMAP_ENTER_NOSLEEP) != 0,
3924 			    ("pmap_allocpte2 failed with sleep allowed"));
3925 			rv = KERN_RESOURCE_SHORTAGE;
3926 			goto out;
3927 		}
3928 	} else
3929 		mpte2 = NULL;
3930 	pte1p = pmap_pte1(pmap, va);
3931 	if (pte1_is_section(pte1_load(pte1p)))
3932 		panic("%s: attempted on 1MB page", __func__);
3933 	pte2p = pmap_pte2_quick(pmap, va);
3934 	if (pte2p == NULL)
3935 		panic("%s: invalid L1 page table entry va=%#x", __func__, va);
3936 
3937 	om = NULL;
3938 	opte2 = pte2_load(pte2p);
3939 	opa = pte2_pa(opte2);
3940 	/*
3941 	 * Mapping has not changed, must be protection or wiring change.
3942 	 */
3943 	if (pte2_is_valid(opte2) && (opa == pa)) {
3944 		/*
3945 		 * Wiring change, just update stats. We don't worry about
3946 		 * wiring PT2 pages as they remain resident as long as there
3947 		 * are valid mappings in them. Hence, if a user page is wired,
3948 		 * the PT2 page will be also.
3949 		 */
3950 		if (pte2_is_wired(npte2) && !pte2_is_wired(opte2))
3951 			pmap->pm_stats.wired_count++;
3952 		else if (!pte2_is_wired(npte2) && pte2_is_wired(opte2))
3953 			pmap->pm_stats.wired_count--;
3954 
3955 		/*
3956 		 * Remove extra pte2 reference
3957 		 */
3958 		if (mpte2)
3959 			pt2_wirecount_dec(mpte2, pte1_index(va));
3960 		if ((m->oflags & VPO_UNMANAGED) == 0)
3961 			om = m;
3962 		goto validate;
3963 	}
3964 
3965 	/*
3966 	 * QQQ: We think that changing physical address on writeable mapping
3967 	 *      is not safe. Well, maybe on kernel address space with correct
3968 	 *      locking, it can make a sense. However, we have no idea why
3969 	 *      anyone should do that on user address space. Are we wrong?
3970 	 */
3971 	KASSERT((opa == 0) || (opa == pa) ||
3972 	    !pte2_is_valid(opte2) || ((opte2 & PTE2_RO) != 0),
3973 	    ("%s: pmap %p va %#x(%#x) opa %#x pa %#x - gotcha %#x %#x!",
3974 	    __func__, pmap, va, opte2, opa, pa, flags, prot));
3975 
3976 	pv = NULL;
3977 
3978 	/*
3979 	 * Mapping has changed, invalidate old range and fall through to
3980 	 * handle validating new mapping.
3981 	 */
3982 	if (opa) {
3983 		if (pte2_is_wired(opte2))
3984 			pmap->pm_stats.wired_count--;
3985 		om = PHYS_TO_VM_PAGE(opa);
3986 		if (om != NULL && (om->oflags & VPO_UNMANAGED) != 0)
3987 			om = NULL;
3988 		if (om != NULL)
3989 			pv = pmap_pvh_remove(&om->md, pmap, va);
3990 
3991 		/*
3992 		 * Remove extra pte2 reference
3993 		 */
3994 		if (mpte2 != NULL)
3995 			pt2_wirecount_dec(mpte2, va >> PTE1_SHIFT);
3996 	} else
3997 		pmap->pm_stats.resident_count++;
3998 
3999 	/*
4000 	 * Enter on the PV list if part of our managed memory.
4001 	 */
4002 	if ((m->oflags & VPO_UNMANAGED) == 0) {
4003 		if (pv == NULL) {
4004 			pv = get_pv_entry(pmap, FALSE);
4005 			pv->pv_va = va;
4006 		}
4007 		TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
4008 	} else if (pv != NULL)
4009 		free_pv_entry(pmap, pv);
4010 
4011 	/*
4012 	 * Increment counters
4013 	 */
4014 	if (pte2_is_wired(npte2))
4015 		pmap->pm_stats.wired_count++;
4016 
4017 validate:
4018 	/*
4019 	 * Now validate mapping with desired protection/wiring.
4020 	 */
4021 	if (prot & VM_PROT_WRITE) {
4022 		if ((m->oflags & VPO_UNMANAGED) == 0)
4023 			vm_page_aflag_set(m, PGA_WRITEABLE);
4024 	}
4025 
4026 	/*
4027 	 * If the mapping or permission bits are different, we need
4028 	 * to update the pte2.
4029 	 *
4030 	 * QQQ: Think again and again what to do
4031 	 *      if the mapping is going to be changed!
4032 	 */
4033 	if ((opte2 & ~(PTE2_NM | PTE2_A)) != (npte2 & ~(PTE2_NM | PTE2_A))) {
4034 		/*
4035 		 * Sync icache if exec permission and attribute VM_MEMATTR_WB_WA
4036 		 * is set. Do it now, before the mapping is stored and made
4037 		 * valid for hardware table walk. If done later, there is a race
4038 		 * for other threads of current process in lazy loading case.
4039 		 * Don't do it for kernel memory which is mapped with exec
4040 		 * permission even if the memory isn't going to hold executable
4041 		 * code. The only time when icache sync is needed is after
4042 		 * kernel module is loaded and the relocation info is processed.
4043 		 * And it's done in elf_cpu_load_file().
4044 		 *
4045 		 * QQQ: (1) Does it exist any better way where
4046 		 *          or how to sync icache?
4047 		 *      (2) Now, we do it on a page basis.
4048 		 */
4049 		if ((prot & VM_PROT_EXECUTE) && pmap != kernel_pmap &&
4050 		    m->md.pat_mode == VM_MEMATTR_WB_WA &&
4051 		    (opa != pa || (opte2 & PTE2_NX)))
4052 			cache_icache_sync_fresh(va, pa, PAGE_SIZE);
4053 
4054 		if (opte2 & PTE2_V) {
4055 			/* Change mapping with break-before-make approach. */
4056 			opte2 = pte2_load_clear(pte2p);
4057 			pmap_tlb_flush(pmap, va);
4058 			pte2_store(pte2p, npte2);
4059 			if (om != NULL) {
4060 				KASSERT((om->oflags & VPO_UNMANAGED) == 0,
4061 				    ("%s: om %p unmanaged", __func__, om));
4062 				if ((opte2 & PTE2_A) != 0)
4063 					vm_page_aflag_set(om, PGA_REFERENCED);
4064 				if (pte2_is_dirty(opte2))
4065 					vm_page_dirty(om);
4066 				if (TAILQ_EMPTY(&om->md.pv_list) &&
4067 				    ((om->flags & PG_FICTITIOUS) != 0 ||
4068 				    TAILQ_EMPTY(&pa_to_pvh(opa)->pv_list)))
4069 					vm_page_aflag_clear(om, PGA_WRITEABLE);
4070 			}
4071 		} else
4072 			pte2_store(pte2p, npte2);
4073 	}
4074 #if 0
4075 	else {
4076 		/*
4077 		 * QQQ: In time when both access and not mofified bits are
4078 		 *      emulated by software, this should not happen. Some
4079 		 *      analysis is need, if this really happen. Missing
4080 		 *      tlb flush somewhere could be the reason.
4081 		 */
4082 		panic("%s: pmap %p va %#x opte2 %x npte2 %x !!", __func__, pmap,
4083 		    va, opte2, npte2);
4084 	}
4085 #endif
4086 
4087 #if VM_NRESERVLEVEL > 0
4088 	/*
4089 	 * If both the L2 page table page and the reservation are fully
4090 	 * populated, then attempt promotion.
4091 	 */
4092 	if ((mpte2 == NULL || pt2_is_full(mpte2, va)) &&
4093 	    sp_enabled && (m->flags & PG_FICTITIOUS) == 0 &&
4094 	    vm_reserv_level_iffullpop(m) == 0)
4095 		pmap_promote_pte1(pmap, pte1p, va);
4096 #endif
4097 
4098 	rv = KERN_SUCCESS;
4099 out:
4100 	sched_unpin();
4101 	rw_wunlock(&pvh_global_lock);
4102 	PMAP_UNLOCK(pmap);
4103 	return (rv);
4104 }
4105 
4106 /*
4107  *  Do the things to unmap a page in a process.
4108  */
4109 static int
pmap_remove_pte2(pmap_t pmap,pt2_entry_t * pte2p,vm_offset_t va,struct spglist * free)4110 pmap_remove_pte2(pmap_t pmap, pt2_entry_t *pte2p, vm_offset_t va,
4111     struct spglist *free)
4112 {
4113 	pt2_entry_t opte2;
4114 	vm_page_t m;
4115 
4116 	rw_assert(&pvh_global_lock, RA_WLOCKED);
4117 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4118 
4119 	/* Clear and invalidate the mapping. */
4120 	opte2 = pte2_load_clear(pte2p);
4121 	pmap_tlb_flush(pmap, va);
4122 
4123 	KASSERT(pte2_is_valid(opte2), ("%s: pmap %p va %#x not link pte2 %#x",
4124 	    __func__, pmap, va, opte2));
4125 
4126 	if (opte2 & PTE2_W)
4127 		pmap->pm_stats.wired_count -= 1;
4128 	pmap->pm_stats.resident_count -= 1;
4129 	if (pte2_is_managed(opte2)) {
4130 		m = PHYS_TO_VM_PAGE(pte2_pa(opte2));
4131 		if (pte2_is_dirty(opte2))
4132 			vm_page_dirty(m);
4133 		if (opte2 & PTE2_A)
4134 			vm_page_aflag_set(m, PGA_REFERENCED);
4135 		pmap_remove_entry(pmap, m, va);
4136 	}
4137 	return (pmap_unuse_pt2(pmap, va, free));
4138 }
4139 
4140 /*
4141  *  Remove a single page from a process address space.
4142  */
4143 static void
pmap_remove_page(pmap_t pmap,vm_offset_t va,struct spglist * free)4144 pmap_remove_page(pmap_t pmap, vm_offset_t va, struct spglist *free)
4145 {
4146 	pt2_entry_t *pte2p;
4147 
4148 	rw_assert(&pvh_global_lock, RA_WLOCKED);
4149 	KASSERT(curthread->td_pinned > 0,
4150 	    ("%s: curthread not pinned", __func__));
4151 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4152 	if ((pte2p = pmap_pte2_quick(pmap, va)) == NULL ||
4153 	    !pte2_is_valid(pte2_load(pte2p)))
4154 		return;
4155 	pmap_remove_pte2(pmap, pte2p, va, free);
4156 }
4157 
4158 /*
4159  *  Remove the given range of addresses from the specified map.
4160  *
4161  *  It is assumed that the start and end are properly
4162  *  rounded to the page size.
4163  */
4164 void
pmap_remove(pmap_t pmap,vm_offset_t sva,vm_offset_t eva)4165 pmap_remove(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
4166 {
4167 	vm_offset_t nextva;
4168 	pt1_entry_t *pte1p, pte1;
4169 	pt2_entry_t *pte2p, pte2;
4170 	struct spglist free;
4171 
4172 	/*
4173 	 * Perform an unsynchronized read. This is, however, safe.
4174 	 */
4175 	if (pmap->pm_stats.resident_count == 0)
4176 		return;
4177 
4178 	SLIST_INIT(&free);
4179 
4180 	rw_wlock(&pvh_global_lock);
4181 	sched_pin();
4182 	PMAP_LOCK(pmap);
4183 
4184 	/*
4185 	 * Special handling of removing one page. A very common
4186 	 * operation and easy to short circuit some code.
4187 	 */
4188 	if (sva + PAGE_SIZE == eva) {
4189 		pte1 = pte1_load(pmap_pte1(pmap, sva));
4190 		if (pte1_is_link(pte1)) {
4191 			pmap_remove_page(pmap, sva, &free);
4192 			goto out;
4193 		}
4194 	}
4195 
4196 	for (; sva < eva; sva = nextva) {
4197 		/*
4198 		 * Calculate address for next L2 page table.
4199 		 */
4200 		nextva = pte1_trunc(sva + PTE1_SIZE);
4201 		if (nextva < sva)
4202 			nextva = eva;
4203 		if (pmap->pm_stats.resident_count == 0)
4204 			break;
4205 
4206 		pte1p = pmap_pte1(pmap, sva);
4207 		pte1 = pte1_load(pte1p);
4208 
4209 		/*
4210 		 * Weed out invalid mappings. Note: we assume that the L1 page
4211 		 * table is always allocated, and in kernel virtual.
4212 		 */
4213 		if (pte1 == 0)
4214 			continue;
4215 
4216 		if (pte1_is_section(pte1)) {
4217 			/*
4218 			 * Are we removing the entire large page?  If not,
4219 			 * demote the mapping and fall through.
4220 			 */
4221 			if (sva + PTE1_SIZE == nextva && eva >= nextva) {
4222 				pmap_remove_pte1(pmap, pte1p, sva, &free);
4223 				continue;
4224 			} else if (!pmap_demote_pte1(pmap, pte1p, sva)) {
4225 				/* The large page mapping was destroyed. */
4226 				continue;
4227 			}
4228 #ifdef INVARIANTS
4229 			else {
4230 				/* Update pte1 after demotion. */
4231 				pte1 = pte1_load(pte1p);
4232 			}
4233 #endif
4234 		}
4235 
4236 		KASSERT(pte1_is_link(pte1), ("%s: pmap %p va %#x pte1 %#x at %p"
4237 		    " is not link", __func__, pmap, sva, pte1, pte1p));
4238 
4239 		/*
4240 		 * Limit our scan to either the end of the va represented
4241 		 * by the current L2 page table page, or to the end of the
4242 		 * range being removed.
4243 		 */
4244 		if (nextva > eva)
4245 			nextva = eva;
4246 
4247 		for (pte2p = pmap_pte2_quick(pmap, sva); sva != nextva;
4248 		    pte2p++, sva += PAGE_SIZE) {
4249 			pte2 = pte2_load(pte2p);
4250 			if (!pte2_is_valid(pte2))
4251 				continue;
4252 			if (pmap_remove_pte2(pmap, pte2p, sva, &free))
4253 				break;
4254 		}
4255 	}
4256 out:
4257 	sched_unpin();
4258 	rw_wunlock(&pvh_global_lock);
4259 	PMAP_UNLOCK(pmap);
4260 	vm_page_free_pages_toq(&free, false);
4261 }
4262 
4263 /*
4264  *	Routine:	pmap_remove_all
4265  *	Function:
4266  *		Removes this physical page from
4267  *		all physical maps in which it resides.
4268  *		Reflects back modify bits to the pager.
4269  *
4270  *	Notes:
4271  *		Original versions of this routine were very
4272  *		inefficient because they iteratively called
4273  *		pmap_remove (slow...)
4274  */
4275 
4276 void
pmap_remove_all(vm_page_t m)4277 pmap_remove_all(vm_page_t m)
4278 {
4279 	struct md_page *pvh;
4280 	pv_entry_t pv;
4281 	pmap_t pmap;
4282 	pt2_entry_t *pte2p, opte2;
4283 	pt1_entry_t *pte1p;
4284 	vm_offset_t va;
4285 	struct spglist free;
4286 
4287 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
4288 	    ("%s: page %p is not managed", __func__, m));
4289 	SLIST_INIT(&free);
4290 	rw_wlock(&pvh_global_lock);
4291 	sched_pin();
4292 	if ((m->flags & PG_FICTITIOUS) != 0)
4293 		goto small_mappings;
4294 	pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
4295 	while ((pv = TAILQ_FIRST(&pvh->pv_list)) != NULL) {
4296 		va = pv->pv_va;
4297 		pmap = PV_PMAP(pv);
4298 		PMAP_LOCK(pmap);
4299 		pte1p = pmap_pte1(pmap, va);
4300 		(void)pmap_demote_pte1(pmap, pte1p, va);
4301 		PMAP_UNLOCK(pmap);
4302 	}
4303 small_mappings:
4304 	while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
4305 		pmap = PV_PMAP(pv);
4306 		PMAP_LOCK(pmap);
4307 		pmap->pm_stats.resident_count--;
4308 		pte1p = pmap_pte1(pmap, pv->pv_va);
4309 		KASSERT(!pte1_is_section(pte1_load(pte1p)), ("%s: found "
4310 		    "a 1mpage in page %p's pv list", __func__, m));
4311 		pte2p = pmap_pte2_quick(pmap, pv->pv_va);
4312 		opte2 = pte2_load_clear(pte2p);
4313 		pmap_tlb_flush(pmap, pv->pv_va);
4314 		KASSERT(pte2_is_valid(opte2), ("%s: pmap %p va %x zero pte2",
4315 		    __func__, pmap, pv->pv_va));
4316 		if (pte2_is_wired(opte2))
4317 			pmap->pm_stats.wired_count--;
4318 		if (opte2 & PTE2_A)
4319 			vm_page_aflag_set(m, PGA_REFERENCED);
4320 
4321 		/*
4322 		 * Update the vm_page_t clean and reference bits.
4323 		 */
4324 		if (pte2_is_dirty(opte2))
4325 			vm_page_dirty(m);
4326 		pmap_unuse_pt2(pmap, pv->pv_va, &free);
4327 		TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
4328 		free_pv_entry(pmap, pv);
4329 		PMAP_UNLOCK(pmap);
4330 	}
4331 	vm_page_aflag_clear(m, PGA_WRITEABLE);
4332 	sched_unpin();
4333 	rw_wunlock(&pvh_global_lock);
4334 	vm_page_free_pages_toq(&free, false);
4335 }
4336 
4337 /*
4338  *  Just subroutine for pmap_remove_pages() to reasonably satisfy
4339  *  good coding style, a.k.a. 80 character line width limit hell.
4340  */
4341 static __inline void
pmap_remove_pte1_quick(pmap_t pmap,pt1_entry_t pte1,pv_entry_t pv,struct spglist * free)4342 pmap_remove_pte1_quick(pmap_t pmap, pt1_entry_t pte1, pv_entry_t pv,
4343     struct spglist *free)
4344 {
4345 	vm_paddr_t pa;
4346 	vm_page_t m, mt, mpt2pg;
4347 	struct md_page *pvh;
4348 
4349 	pa = pte1_pa(pte1);
4350 	m = PHYS_TO_VM_PAGE(pa);
4351 
4352 	KASSERT(m->phys_addr == pa, ("%s: vm_page_t %p addr mismatch %#x %#x",
4353 	    __func__, m, m->phys_addr, pa));
4354 	KASSERT((m->flags & PG_FICTITIOUS) != 0 ||
4355 	    m < &vm_page_array[vm_page_array_size],
4356 	    ("%s: bad pte1 %#x", __func__, pte1));
4357 
4358 	if (pte1_is_dirty(pte1)) {
4359 		for (mt = m; mt < &m[PTE1_SIZE / PAGE_SIZE]; mt++)
4360 			vm_page_dirty(mt);
4361 	}
4362 
4363 	pmap->pm_stats.resident_count -= PTE1_SIZE / PAGE_SIZE;
4364 	pvh = pa_to_pvh(pa);
4365 	TAILQ_REMOVE(&pvh->pv_list, pv, pv_next);
4366 	if (TAILQ_EMPTY(&pvh->pv_list)) {
4367 		for (mt = m; mt < &m[PTE1_SIZE / PAGE_SIZE]; mt++)
4368 			if (TAILQ_EMPTY(&mt->md.pv_list))
4369 				vm_page_aflag_clear(mt, PGA_WRITEABLE);
4370 	}
4371 	mpt2pg = pmap_pt2_page(pmap, pv->pv_va);
4372 	if (mpt2pg != NULL)
4373 		pmap_unwire_pt2_all(pmap, pv->pv_va, mpt2pg, free);
4374 }
4375 
4376 /*
4377  *  Just subroutine for pmap_remove_pages() to reasonably satisfy
4378  *  good coding style, a.k.a. 80 character line width limit hell.
4379  */
4380 static __inline void
pmap_remove_pte2_quick(pmap_t pmap,pt2_entry_t pte2,pv_entry_t pv,struct spglist * free)4381 pmap_remove_pte2_quick(pmap_t pmap, pt2_entry_t pte2, pv_entry_t pv,
4382     struct spglist *free)
4383 {
4384 	vm_paddr_t pa;
4385 	vm_page_t m;
4386 	struct md_page *pvh;
4387 
4388 	pa = pte2_pa(pte2);
4389 	m = PHYS_TO_VM_PAGE(pa);
4390 
4391 	KASSERT(m->phys_addr == pa, ("%s: vm_page_t %p addr mismatch %#x %#x",
4392 	    __func__, m, m->phys_addr, pa));
4393 	KASSERT((m->flags & PG_FICTITIOUS) != 0 ||
4394 	    m < &vm_page_array[vm_page_array_size],
4395 	    ("%s: bad pte2 %#x", __func__, pte2));
4396 
4397 	if (pte2_is_dirty(pte2))
4398 		vm_page_dirty(m);
4399 
4400 	pmap->pm_stats.resident_count--;
4401 	TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
4402 	if (TAILQ_EMPTY(&m->md.pv_list) && (m->flags & PG_FICTITIOUS) == 0) {
4403 		pvh = pa_to_pvh(pa);
4404 		if (TAILQ_EMPTY(&pvh->pv_list))
4405 			vm_page_aflag_clear(m, PGA_WRITEABLE);
4406 	}
4407 	pmap_unuse_pt2(pmap, pv->pv_va, free);
4408 }
4409 
4410 /*
4411  *  Remove all pages from specified address space this aids process
4412  *  exit speeds. Also, this code is special cased for current process
4413  *  only, but can have the more generic (and slightly slower) mode enabled.
4414  *  This is much faster than pmap_remove in the case of running down
4415  *  an entire address space.
4416  */
4417 void
pmap_remove_pages(pmap_t pmap)4418 pmap_remove_pages(pmap_t pmap)
4419 {
4420 	pt1_entry_t *pte1p, pte1;
4421 	pt2_entry_t *pte2p, pte2;
4422 	pv_entry_t pv;
4423 	struct pv_chunk *pc, *npc;
4424 	struct spglist free;
4425 	int field, idx;
4426 	int32_t bit;
4427 	uint32_t inuse, bitmask;
4428 	boolean_t allfree;
4429 
4430 	/*
4431 	 * Assert that the given pmap is only active on the current
4432 	 * CPU.  Unfortunately, we cannot block another CPU from
4433 	 * activating the pmap while this function is executing.
4434 	 */
4435 	KASSERT(pmap == vmspace_pmap(curthread->td_proc->p_vmspace),
4436 	    ("%s: non-current pmap %p", __func__, pmap));
4437 #if defined(SMP) && defined(INVARIANTS)
4438 	{
4439 		cpuset_t other_cpus;
4440 
4441 		sched_pin();
4442 		other_cpus = pmap->pm_active;
4443 		CPU_CLR(PCPU_GET(cpuid), &other_cpus);
4444 		sched_unpin();
4445 		KASSERT(CPU_EMPTY(&other_cpus),
4446 		    ("%s: pmap %p active on other cpus", __func__, pmap));
4447 	}
4448 #endif
4449 	SLIST_INIT(&free);
4450 	rw_wlock(&pvh_global_lock);
4451 	PMAP_LOCK(pmap);
4452 	sched_pin();
4453 	TAILQ_FOREACH_SAFE(pc, &pmap->pm_pvchunk, pc_list, npc) {
4454 		KASSERT(pc->pc_pmap == pmap, ("%s: wrong pmap %p %p",
4455 		    __func__, pmap, pc->pc_pmap));
4456 		allfree = TRUE;
4457 		for (field = 0; field < _NPCM; field++) {
4458 			inuse = (~(pc->pc_map[field])) & pc_freemask[field];
4459 			while (inuse != 0) {
4460 				bit = ffs(inuse) - 1;
4461 				bitmask = 1UL << bit;
4462 				idx = field * 32 + bit;
4463 				pv = &pc->pc_pventry[idx];
4464 				inuse &= ~bitmask;
4465 
4466 				/*
4467 				 * Note that we cannot remove wired pages
4468 				 * from a process' mapping at this time
4469 				 */
4470 				pte1p = pmap_pte1(pmap, pv->pv_va);
4471 				pte1 = pte1_load(pte1p);
4472 				if (pte1_is_section(pte1)) {
4473 					if (pte1_is_wired(pte1))  {
4474 						allfree = FALSE;
4475 						continue;
4476 					}
4477 					pte1_clear(pte1p);
4478 					pmap_remove_pte1_quick(pmap, pte1, pv,
4479 					    &free);
4480 				}
4481 				else if (pte1_is_link(pte1)) {
4482 					pte2p = pt2map_entry(pv->pv_va);
4483 					pte2 = pte2_load(pte2p);
4484 
4485 					if (!pte2_is_valid(pte2)) {
4486 						printf("%s: pmap %p va %#x "
4487 						    "pte2 %#x\n", __func__,
4488 						    pmap, pv->pv_va, pte2);
4489 						panic("bad pte2");
4490 					}
4491 
4492 					if (pte2_is_wired(pte2))   {
4493 						allfree = FALSE;
4494 						continue;
4495 					}
4496 					pte2_clear(pte2p);
4497 					pmap_remove_pte2_quick(pmap, pte2, pv,
4498 					    &free);
4499 				} else {
4500 					printf("%s: pmap %p va %#x pte1 %#x\n",
4501 					    __func__, pmap, pv->pv_va, pte1);
4502 					panic("bad pte1");
4503 				}
4504 
4505 				/* Mark free */
4506 				PV_STAT(pv_entry_frees++);
4507 				PV_STAT(pv_entry_spare++);
4508 				pv_entry_count--;
4509 				pc->pc_map[field] |= bitmask;
4510 			}
4511 		}
4512 		if (allfree) {
4513 			TAILQ_REMOVE(&pmap->pm_pvchunk, pc, pc_list);
4514 			free_pv_chunk(pc);
4515 		}
4516 	}
4517 	tlb_flush_all_ng_local();
4518 	sched_unpin();
4519 	rw_wunlock(&pvh_global_lock);
4520 	PMAP_UNLOCK(pmap);
4521 	vm_page_free_pages_toq(&free, false);
4522 }
4523 
4524 /*
4525  *  This code makes some *MAJOR* assumptions:
4526  *  1. Current pmap & pmap exists.
4527  *  2. Not wired.
4528  *  3. Read access.
4529  *  4. No L2 page table pages.
4530  *  but is *MUCH* faster than pmap_enter...
4531  */
4532 static vm_page_t
pmap_enter_quick_locked(pmap_t pmap,vm_offset_t va,vm_page_t m,vm_prot_t prot,vm_page_t mpt2pg)4533 pmap_enter_quick_locked(pmap_t pmap, vm_offset_t va, vm_page_t m,
4534     vm_prot_t prot, vm_page_t mpt2pg)
4535 {
4536 	pt2_entry_t *pte2p, pte2;
4537 	vm_paddr_t pa;
4538 	struct spglist free;
4539 	uint32_t l2prot;
4540 
4541 	KASSERT(va < kmi.clean_sva || va >= kmi.clean_eva ||
4542 	    (m->oflags & VPO_UNMANAGED) != 0,
4543 	    ("%s: managed mapping within the clean submap", __func__));
4544 	rw_assert(&pvh_global_lock, RA_WLOCKED);
4545 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4546 
4547 	/*
4548 	 * In the case that a L2 page table page is not
4549 	 * resident, we are creating it here.
4550 	 */
4551 	if (va < VM_MAXUSER_ADDRESS) {
4552 		u_int pte1_idx;
4553 		pt1_entry_t pte1, *pte1p;
4554 		vm_paddr_t pt2_pa;
4555 
4556 		/*
4557 		 * Get L1 page table things.
4558 		 */
4559 		pte1_idx = pte1_index(va);
4560 		pte1p = pmap_pte1(pmap, va);
4561 		pte1 = pte1_load(pte1p);
4562 
4563 		if (mpt2pg && (mpt2pg->pindex == (pte1_idx & ~PT2PG_MASK))) {
4564 			/*
4565 			 * Each of NPT2_IN_PG L2 page tables on the page can
4566 			 * come here. Make sure that associated L1 page table
4567 			 * link is established.
4568 			 *
4569 			 * QQQ: It comes that we don't establish all links to
4570 			 *      L2 page tables for newly allocated L2 page
4571 			 *      tables page.
4572 			 */
4573 			KASSERT(!pte1_is_section(pte1),
4574 			    ("%s: pte1 %#x is section", __func__, pte1));
4575 			if (!pte1_is_link(pte1)) {
4576 				pt2_pa = page_pt2pa(VM_PAGE_TO_PHYS(mpt2pg),
4577 				    pte1_idx);
4578 				pte1_store(pte1p, PTE1_LINK(pt2_pa));
4579 			}
4580 			pt2_wirecount_inc(mpt2pg, pte1_idx);
4581 		} else {
4582 			/*
4583 			 * If the L2 page table page is mapped, we just
4584 			 * increment the hold count, and activate it.
4585 			 */
4586 			if (pte1_is_section(pte1)) {
4587 				return (NULL);
4588 			} else if (pte1_is_link(pte1)) {
4589 				mpt2pg = PHYS_TO_VM_PAGE(pte1_link_pa(pte1));
4590 				pt2_wirecount_inc(mpt2pg, pte1_idx);
4591 			} else {
4592 				mpt2pg = _pmap_allocpte2(pmap, va,
4593 				    PMAP_ENTER_NOSLEEP);
4594 				if (mpt2pg == NULL)
4595 					return (NULL);
4596 			}
4597 		}
4598 	} else {
4599 		mpt2pg = NULL;
4600 	}
4601 
4602 	/*
4603 	 * This call to pt2map_entry() makes the assumption that we are
4604 	 * entering the page into the current pmap.  In order to support
4605 	 * quick entry into any pmap, one would likely use pmap_pte2_quick().
4606 	 * But that isn't as quick as pt2map_entry().
4607 	 */
4608 	pte2p = pt2map_entry(va);
4609 	pte2 = pte2_load(pte2p);
4610 	if (pte2_is_valid(pte2)) {
4611 		if (mpt2pg != NULL) {
4612 			/*
4613 			 * Remove extra pte2 reference
4614 			 */
4615 			pt2_wirecount_dec(mpt2pg, pte1_index(va));
4616 			mpt2pg = NULL;
4617 		}
4618 		return (NULL);
4619 	}
4620 
4621 	/*
4622 	 * Enter on the PV list if part of our managed memory.
4623 	 */
4624 	if ((m->oflags & VPO_UNMANAGED) == 0 &&
4625 	    !pmap_try_insert_pv_entry(pmap, va, m)) {
4626 		if (mpt2pg != NULL) {
4627 			SLIST_INIT(&free);
4628 			if (pmap_unwire_pt2(pmap, va, mpt2pg, &free)) {
4629 				pmap_tlb_flush(pmap, va);
4630 				vm_page_free_pages_toq(&free, false);
4631 			}
4632 
4633 			mpt2pg = NULL;
4634 		}
4635 		return (NULL);
4636 	}
4637 
4638 	/*
4639 	 * Increment counters
4640 	 */
4641 	pmap->pm_stats.resident_count++;
4642 
4643 	/*
4644 	 * Now validate mapping with RO protection
4645 	 */
4646 	pa = VM_PAGE_TO_PHYS(m);
4647 	l2prot = PTE2_RO | PTE2_NM;
4648 	if (va < VM_MAXUSER_ADDRESS)
4649 		l2prot |= PTE2_U | PTE2_NG;
4650 	if ((prot & VM_PROT_EXECUTE) == 0)
4651 		l2prot |= PTE2_NX;
4652 	else if (m->md.pat_mode == VM_MEMATTR_WB_WA && pmap != kernel_pmap) {
4653 		/*
4654 		 * Sync icache if exec permission and attribute VM_MEMATTR_WB_WA
4655 		 * is set. QQQ: For more info, see comments in pmap_enter().
4656 		 */
4657 		cache_icache_sync_fresh(va, pa, PAGE_SIZE);
4658 	}
4659 	pte2_store(pte2p, PTE2(pa, l2prot, vm_page_pte2_attr(m)));
4660 
4661 	return (mpt2pg);
4662 }
4663 
4664 void
pmap_enter_quick(pmap_t pmap,vm_offset_t va,vm_page_t m,vm_prot_t prot)4665 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
4666 {
4667 
4668 	rw_wlock(&pvh_global_lock);
4669 	PMAP_LOCK(pmap);
4670 	(void)pmap_enter_quick_locked(pmap, va, m, prot, NULL);
4671 	rw_wunlock(&pvh_global_lock);
4672 	PMAP_UNLOCK(pmap);
4673 }
4674 
4675 /*
4676  *  Tries to create a read- and/or execute-only 1 MB page mapping.  Returns
4677  *  true if successful.  Returns false if (1) a mapping already exists at the
4678  *  specified virtual address or (2) a PV entry cannot be allocated without
4679  *  reclaiming another PV entry.
4680  */
4681 static bool
pmap_enter_1mpage(pmap_t pmap,vm_offset_t va,vm_page_t m,vm_prot_t prot)4682 pmap_enter_1mpage(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
4683 {
4684 	pt1_entry_t pte1;
4685 	vm_paddr_t pa;
4686 
4687 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4688 	pa = VM_PAGE_TO_PHYS(m);
4689 	pte1 = PTE1(pa, PTE1_NM | PTE1_RO, ATTR_TO_L1(vm_page_pte2_attr(m)));
4690 	if ((prot & VM_PROT_EXECUTE) == 0)
4691 		pte1 |= PTE1_NX;
4692 	if (va < VM_MAXUSER_ADDRESS)
4693 		pte1 |= PTE1_U;
4694 	if (pmap != kernel_pmap)
4695 		pte1 |= PTE1_NG;
4696 	return (pmap_enter_pte1(pmap, va, pte1, PMAP_ENTER_NOSLEEP |
4697 	    PMAP_ENTER_NOREPLACE | PMAP_ENTER_NORECLAIM, m) == KERN_SUCCESS);
4698 }
4699 
4700 /*
4701  *  Tries to create the specified 1 MB page mapping.  Returns KERN_SUCCESS if
4702  *  the mapping was created, and either KERN_FAILURE or KERN_RESOURCE_SHORTAGE
4703  *  otherwise.  Returns KERN_FAILURE if PMAP_ENTER_NOREPLACE was specified and
4704  *  a mapping already exists at the specified virtual address.  Returns
4705  *  KERN_RESOURCE_SHORTAGE if PMAP_ENTER_NORECLAIM was specified and PV entry
4706  *  allocation failed.
4707  */
4708 static int
pmap_enter_pte1(pmap_t pmap,vm_offset_t va,pt1_entry_t pte1,u_int flags,vm_page_t m)4709 pmap_enter_pte1(pmap_t pmap, vm_offset_t va, pt1_entry_t pte1, u_int flags,
4710     vm_page_t m)
4711 {
4712 	struct spglist free;
4713 	pt1_entry_t opte1, *pte1p;
4714 	pt2_entry_t pte2, *pte2p;
4715 	vm_offset_t cur, end;
4716 	vm_page_t mt;
4717 
4718 	rw_assert(&pvh_global_lock, RA_WLOCKED);
4719 	KASSERT((pte1 & (PTE1_NM | PTE1_RO)) == 0 ||
4720 	    (pte1 & (PTE1_NM | PTE1_RO)) == (PTE1_NM | PTE1_RO),
4721 	    ("%s: pte1 has inconsistent NM and RO attributes", __func__));
4722 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4723 	pte1p = pmap_pte1(pmap, va);
4724 	opte1 = pte1_load(pte1p);
4725 	if (pte1_is_valid(opte1)) {
4726 		if ((flags & PMAP_ENTER_NOREPLACE) != 0) {
4727 			CTR3(KTR_PMAP, "%s: failure for va %#lx in pmap %p",
4728 			    __func__, va, pmap);
4729 			return (KERN_FAILURE);
4730 		}
4731 		/* Break the existing mapping(s). */
4732 		SLIST_INIT(&free);
4733 		if (pte1_is_section(opte1)) {
4734 			/*
4735 			 * If the section resulted from a promotion, then a
4736 			 * reserved PT page could be freed.
4737 			 */
4738 			pmap_remove_pte1(pmap, pte1p, va, &free);
4739 		} else {
4740 			sched_pin();
4741 			end = va + PTE1_SIZE;
4742 			for (cur = va, pte2p = pmap_pte2_quick(pmap, va);
4743 			    cur != end; cur += PAGE_SIZE, pte2p++) {
4744 				pte2 = pte2_load(pte2p);
4745 				if (!pte2_is_valid(pte2))
4746 					continue;
4747 				if (pmap_remove_pte2(pmap, pte2p, cur, &free))
4748 					break;
4749 			}
4750 			sched_unpin();
4751 		}
4752 		vm_page_free_pages_toq(&free, false);
4753 	}
4754 	if ((m->oflags & VPO_UNMANAGED) == 0) {
4755 		/*
4756 		 * Abort this mapping if its PV entry could not be created.
4757 		 */
4758 		if (!pmap_pv_insert_pte1(pmap, va, pte1, flags)) {
4759 			CTR3(KTR_PMAP, "%s: failure for va %#lx in pmap %p",
4760 			    __func__, va, pmap);
4761 			return (KERN_RESOURCE_SHORTAGE);
4762 		}
4763 		if ((pte1 & PTE1_RO) == 0) {
4764 			for (mt = m; mt < &m[PTE1_SIZE / PAGE_SIZE]; mt++)
4765 				vm_page_aflag_set(mt, PGA_WRITEABLE);
4766 		}
4767 	}
4768 
4769 	/*
4770 	 * Increment counters.
4771 	 */
4772 	if (pte1_is_wired(pte1))
4773 		pmap->pm_stats.wired_count += PTE1_SIZE / PAGE_SIZE;
4774 	pmap->pm_stats.resident_count += PTE1_SIZE / PAGE_SIZE;
4775 
4776 	/*
4777 	 * Sync icache if exec permission and attribute VM_MEMATTR_WB_WA
4778 	 * is set.  QQQ: For more info, see comments in pmap_enter().
4779 	 */
4780 	if ((pte1 & PTE1_NX) == 0 && m->md.pat_mode == VM_MEMATTR_WB_WA &&
4781 	    pmap != kernel_pmap && (!pte1_is_section(opte1) ||
4782 	    pte1_pa(opte1) != VM_PAGE_TO_PHYS(m) || (opte1 & PTE2_NX) != 0))
4783 		cache_icache_sync_fresh(va, VM_PAGE_TO_PHYS(m), PTE1_SIZE);
4784 
4785 	/*
4786 	 * Map the section.
4787 	 */
4788 	pte1_store(pte1p, pte1);
4789 
4790 	pmap_pte1_mappings++;
4791 	CTR3(KTR_PMAP, "%s: success for va %#lx in pmap %p", __func__, va,
4792 	    pmap);
4793 	return (KERN_SUCCESS);
4794 }
4795 
4796 /*
4797  *  Maps a sequence of resident pages belonging to the same object.
4798  *  The sequence begins with the given page m_start.  This page is
4799  *  mapped at the given virtual address start.  Each subsequent page is
4800  *  mapped at a virtual address that is offset from start by the same
4801  *  amount as the page is offset from m_start within the object.  The
4802  *  last page in the sequence is the page with the largest offset from
4803  *  m_start that can be mapped at a virtual address less than the given
4804  *  virtual address end.  Not every virtual page between start and end
4805  *  is mapped; only those for which a resident page exists with the
4806  *  corresponding offset from m_start are mapped.
4807  */
4808 void
pmap_enter_object(pmap_t pmap,vm_offset_t start,vm_offset_t end,vm_page_t m_start,vm_prot_t prot)4809 pmap_enter_object(pmap_t pmap, vm_offset_t start, vm_offset_t end,
4810     vm_page_t m_start, vm_prot_t prot)
4811 {
4812 	vm_offset_t va;
4813 	vm_page_t m, mpt2pg;
4814 	vm_pindex_t diff, psize;
4815 
4816 	PDEBUG(6, printf("%s: pmap %p start %#x end  %#x m %p prot %#x\n",
4817 	    __func__, pmap, start, end, m_start, prot));
4818 
4819 	VM_OBJECT_ASSERT_LOCKED(m_start->object);
4820 	psize = atop(end - start);
4821 	mpt2pg = NULL;
4822 	m = m_start;
4823 	rw_wlock(&pvh_global_lock);
4824 	PMAP_LOCK(pmap);
4825 	while (m != NULL && (diff = m->pindex - m_start->pindex) < psize) {
4826 		va = start + ptoa(diff);
4827 		if ((va & PTE1_OFFSET) == 0 && va + PTE1_SIZE <= end &&
4828 		    m->psind == 1 && sp_enabled &&
4829 		    pmap_enter_1mpage(pmap, va, m, prot))
4830 			m = &m[PTE1_SIZE / PAGE_SIZE - 1];
4831 		else
4832 			mpt2pg = pmap_enter_quick_locked(pmap, va, m, prot,
4833 			    mpt2pg);
4834 		m = TAILQ_NEXT(m, listq);
4835 	}
4836 	rw_wunlock(&pvh_global_lock);
4837 	PMAP_UNLOCK(pmap);
4838 }
4839 
4840 /*
4841  *  This code maps large physical mmap regions into the
4842  *  processor address space.  Note that some shortcuts
4843  *  are taken, but the code works.
4844  */
4845 void
pmap_object_init_pt(pmap_t pmap,vm_offset_t addr,vm_object_t object,vm_pindex_t pindex,vm_size_t size)4846 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_object_t object,
4847     vm_pindex_t pindex, vm_size_t size)
4848 {
4849 	pt1_entry_t *pte1p;
4850 	vm_paddr_t pa, pte2_pa;
4851 	vm_page_t p;
4852 	vm_memattr_t pat_mode;
4853 	u_int l1attr, l1prot;
4854 
4855 	VM_OBJECT_ASSERT_WLOCKED(object);
4856 	KASSERT(object->type == OBJT_DEVICE || object->type == OBJT_SG,
4857 	    ("%s: non-device object", __func__));
4858 	if ((addr & PTE1_OFFSET) == 0 && (size & PTE1_OFFSET) == 0) {
4859 		if (!vm_object_populate(object, pindex, pindex + atop(size)))
4860 			return;
4861 		p = vm_page_lookup(object, pindex);
4862 		KASSERT(p->valid == VM_PAGE_BITS_ALL,
4863 		    ("%s: invalid page %p", __func__, p));
4864 		pat_mode = p->md.pat_mode;
4865 
4866 		/*
4867 		 * Abort the mapping if the first page is not physically
4868 		 * aligned to a 1MB page boundary.
4869 		 */
4870 		pte2_pa = VM_PAGE_TO_PHYS(p);
4871 		if (pte2_pa & PTE1_OFFSET)
4872 			return;
4873 
4874 		/*
4875 		 * Skip the first page. Abort the mapping if the rest of
4876 		 * the pages are not physically contiguous or have differing
4877 		 * memory attributes.
4878 		 */
4879 		p = TAILQ_NEXT(p, listq);
4880 		for (pa = pte2_pa + PAGE_SIZE; pa < pte2_pa + size;
4881 		    pa += PAGE_SIZE) {
4882 			KASSERT(p->valid == VM_PAGE_BITS_ALL,
4883 			    ("%s: invalid page %p", __func__, p));
4884 			if (pa != VM_PAGE_TO_PHYS(p) ||
4885 			    pat_mode != p->md.pat_mode)
4886 				return;
4887 			p = TAILQ_NEXT(p, listq);
4888 		}
4889 
4890 		/*
4891 		 * Map using 1MB pages.
4892 		 *
4893 		 * QQQ: Well, we are mapping a section, so same condition must
4894 		 * be hold like during promotion. It looks that only RW mapping
4895 		 * is done here, so readonly mapping must be done elsewhere.
4896 		 */
4897 		l1prot = PTE1_U | PTE1_NG | PTE1_RW | PTE1_M | PTE1_A;
4898 		l1attr = ATTR_TO_L1(vm_memattr_to_pte2(pat_mode));
4899 		PMAP_LOCK(pmap);
4900 		for (pa = pte2_pa; pa < pte2_pa + size; pa += PTE1_SIZE) {
4901 			pte1p = pmap_pte1(pmap, addr);
4902 			if (!pte1_is_valid(pte1_load(pte1p))) {
4903 				pte1_store(pte1p, PTE1(pa, l1prot, l1attr));
4904 				pmap->pm_stats.resident_count += PTE1_SIZE /
4905 				    PAGE_SIZE;
4906 				pmap_pte1_mappings++;
4907 			}
4908 			/* Else continue on if the PTE1 is already valid. */
4909 			addr += PTE1_SIZE;
4910 		}
4911 		PMAP_UNLOCK(pmap);
4912 	}
4913 }
4914 
4915 /*
4916  *  Do the things to protect a 1mpage in a process.
4917  */
4918 static void
pmap_protect_pte1(pmap_t pmap,pt1_entry_t * pte1p,vm_offset_t sva,vm_prot_t prot)4919 pmap_protect_pte1(pmap_t pmap, pt1_entry_t *pte1p, vm_offset_t sva,
4920     vm_prot_t prot)
4921 {
4922 	pt1_entry_t npte1, opte1;
4923 	vm_offset_t eva, va;
4924 	vm_page_t m;
4925 
4926 	PMAP_LOCK_ASSERT(pmap, MA_OWNED);
4927 	KASSERT((sva & PTE1_OFFSET) == 0,
4928 	    ("%s: sva is not 1mpage aligned", __func__));
4929 
4930 	opte1 = npte1 = pte1_load(pte1p);
4931 	if (pte1_is_managed(opte1) && pte1_is_dirty(opte1)) {
4932 		eva = sva + PTE1_SIZE;
4933 		for (va = sva, m = PHYS_TO_VM_PAGE(pte1_pa(opte1));
4934 		    va < eva; va += PAGE_SIZE, m++)
4935 			vm_page_dirty(m);
4936 	}
4937 	if ((prot & VM_PROT_WRITE) == 0)
4938 		npte1 |= PTE1_RO | PTE1_NM;
4939 	if ((prot & VM_PROT_EXECUTE) == 0)
4940 		npte1 |= PTE1_NX;
4941 
4942 	/*
4943 	 * QQQ: Herein, execute permission is never set.
4944 	 *      It only can be cleared. So, no icache
4945 	 *      syncing is needed.
4946 	 */
4947 
4948 	if (npte1 != opte1) {
4949 		pte1_store(pte1p, npte1);
4950 		pmap_tlb_flush(pmap, sva);
4951 	}
4952 }
4953 
4954 /*
4955  *	Set the physical protection on the
4956  *	specified range of this map as requested.
4957  */
4958 void
pmap_protect(pmap_t pmap,vm_offset_t sva,vm_offset_t eva,vm_prot_t prot)4959 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
4960 {
4961 	boolean_t pv_lists_locked;
4962 	vm_offset_t nextva;
4963 	pt1_entry_t *pte1p, pte1;
4964 	pt2_entry_t *pte2p, opte2, npte2;
4965 
4966 	KASSERT((prot & ~VM_PROT_ALL) == 0, ("invalid prot %x", prot));
4967 	if (prot == VM_PROT_NONE) {
4968 		pmap_remove(pmap, sva, eva);
4969 		return;
4970 	}
4971 
4972 	if ((prot & (VM_PROT_WRITE | VM_PROT_EXECUTE)) ==
4973 	    (VM_PROT_WRITE | VM_PROT_EXECUTE))
4974 		return;
4975 
4976 	if (pmap_is_current(pmap))
4977 		pv_lists_locked = FALSE;
4978 	else {
4979 		pv_lists_locked = TRUE;
4980 resume:
4981 		rw_wlock(&pvh_global_lock);
4982 		sched_pin();
4983 	}
4984 
4985 	PMAP_LOCK(pmap);
4986 	for (; sva < eva; sva = nextva) {
4987 		/*
4988 		 * Calculate address for next L2 page table.
4989 		 */
4990 		nextva = pte1_trunc(sva + PTE1_SIZE);
4991 		if (nextva < sva)
4992 			nextva = eva;
4993 
4994 		pte1p = pmap_pte1(pmap, sva);
4995 		pte1 = pte1_load(pte1p);
4996 
4997 		/*
4998 		 * Weed out invalid mappings. Note: we assume that L1 page
4999 		 * page table is always allocated, and in kernel virtual.
5000 		 */
5001 		if (pte1 == 0)
5002 			continue;
5003 
5004 		if (pte1_is_section(pte1)) {
5005 			/*
5006 			 * Are we protecting the entire large page?  If not,
5007 			 * demote the mapping and fall through.
5008 			 */
5009 			if (sva + PTE1_SIZE == nextva && eva >= nextva) {
5010 				pmap_protect_pte1(pmap, pte1p, sva, prot);
5011 				continue;
5012 			} else {
5013 				if (!pv_lists_locked) {
5014 					pv_lists_locked = TRUE;
5015 					if (!rw_try_wlock(&pvh_global_lock)) {
5016 						PMAP_UNLOCK(pmap);
5017 						goto resume;
5018 					}
5019 					sched_pin();
5020 				}
5021 				if (!pmap_demote_pte1(pmap, pte1p, sva)) {
5022 					/*
5023 					 * The large page mapping
5024 					 * was destroyed.
5025 					 */
5026 					continue;
5027 				}
5028 #ifdef INVARIANTS
5029 				else {
5030 					/* Update pte1 after demotion */
5031 					pte1 = pte1_load(pte1p);
5032 				}
5033 #endif
5034 			}
5035 		}
5036 
5037 		KASSERT(pte1_is_link(pte1), ("%s: pmap %p va %#x pte1 %#x at %p"
5038 		    " is not link", __func__, pmap, sva, pte1, pte1p));
5039 
5040 		/*
5041 		 * Limit our scan to either the end of the va represented
5042 		 * by the current L2 page table page, or to the end of the
5043 		 * range being protected.
5044 		 */
5045 		if (nextva > eva)
5046 			nextva = eva;
5047 
5048 		for (pte2p = pmap_pte2_quick(pmap, sva); sva != nextva; pte2p++,
5049 		    sva += PAGE_SIZE) {
5050 			vm_page_t m;
5051 
5052 			opte2 = npte2 = pte2_load(pte2p);
5053 			if (!pte2_is_valid(opte2))
5054 				continue;
5055 
5056 			if ((prot & VM_PROT_WRITE) == 0) {
5057 				if (pte2_is_managed(opte2) &&
5058 				    pte2_is_dirty(opte2)) {
5059 					m = PHYS_TO_VM_PAGE(pte2_pa(opte2));
5060 					vm_page_dirty(m);
5061 				}
5062 				npte2 |= PTE2_RO | PTE2_NM;
5063 			}
5064 
5065 			if ((prot & VM_PROT_EXECUTE) == 0)
5066 				npte2 |= PTE2_NX;
5067 
5068 			/*
5069 			 * QQQ: Herein, execute permission is never set.
5070 			 *      It only can be cleared. So, no icache
5071 			 *      syncing is needed.
5072 			 */
5073 
5074 			if (npte2 != opte2) {
5075 				pte2_store(pte2p, npte2);
5076 				pmap_tlb_flush(pmap, sva);
5077 			}
5078 		}
5079 	}
5080 	if (pv_lists_locked) {
5081 		sched_unpin();
5082 		rw_wunlock(&pvh_global_lock);
5083 	}
5084 	PMAP_UNLOCK(pmap);
5085 }
5086 
5087 /*
5088  *	pmap_pvh_wired_mappings:
5089  *
5090  *	Return the updated number "count" of managed mappings that are wired.
5091  */
5092 static int
pmap_pvh_wired_mappings(struct md_page * pvh,int count)5093 pmap_pvh_wired_mappings(struct md_page *pvh, int count)
5094 {
5095 	pmap_t pmap;
5096 	pt1_entry_t pte1;
5097 	pt2_entry_t pte2;
5098 	pv_entry_t pv;
5099 
5100 	rw_assert(&pvh_global_lock, RA_WLOCKED);
5101 	sched_pin();
5102 	TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
5103 		pmap = PV_PMAP(pv);
5104 		PMAP_LOCK(pmap);
5105 		pte1 = pte1_load(pmap_pte1(pmap, pv->pv_va));
5106 		if (pte1_is_section(pte1)) {
5107 			if (pte1_is_wired(pte1))
5108 				count++;
5109 		} else {
5110 			KASSERT(pte1_is_link(pte1),
5111 			    ("%s: pte1 %#x is not link", __func__, pte1));
5112 			pte2 = pte2_load(pmap_pte2_quick(pmap, pv->pv_va));
5113 			if (pte2_is_wired(pte2))
5114 				count++;
5115 		}
5116 		PMAP_UNLOCK(pmap);
5117 	}
5118 	sched_unpin();
5119 	return (count);
5120 }
5121 
5122 /*
5123  *	pmap_page_wired_mappings:
5124  *
5125  *	Return the number of managed mappings to the given physical page
5126  *	that are wired.
5127  */
5128 int
pmap_page_wired_mappings(vm_page_t m)5129 pmap_page_wired_mappings(vm_page_t m)
5130 {
5131 	int count;
5132 
5133 	count = 0;
5134 	if ((m->oflags & VPO_UNMANAGED) != 0)
5135 		return (count);
5136 	rw_wlock(&pvh_global_lock);
5137 	count = pmap_pvh_wired_mappings(&m->md, count);
5138 	if ((m->flags & PG_FICTITIOUS) == 0) {
5139 		count = pmap_pvh_wired_mappings(pa_to_pvh(VM_PAGE_TO_PHYS(m)),
5140 		    count);
5141 	}
5142 	rw_wunlock(&pvh_global_lock);
5143 	return (count);
5144 }
5145 
5146 /*
5147  *  Returns TRUE if any of the given mappings were used to modify
5148  *  physical memory.  Otherwise, returns FALSE.  Both page and 1mpage
5149  *  mappings are supported.
5150  */
5151 static boolean_t
pmap_is_modified_pvh(struct md_page * pvh)5152 pmap_is_modified_pvh(struct md_page *pvh)
5153 {
5154 	pv_entry_t pv;
5155 	pt1_entry_t pte1;
5156 	pt2_entry_t pte2;
5157 	pmap_t pmap;
5158 	boolean_t rv;
5159 
5160 	rw_assert(&pvh_global_lock, RA_WLOCKED);
5161 	rv = FALSE;
5162 	sched_pin();
5163 	TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
5164 		pmap = PV_PMAP(pv);
5165 		PMAP_LOCK(pmap);
5166 		pte1 = pte1_load(pmap_pte1(pmap, pv->pv_va));
5167 		if (pte1_is_section(pte1)) {
5168 			rv = pte1_is_dirty(pte1);
5169 		} else {
5170 			KASSERT(pte1_is_link(pte1),
5171 			    ("%s: pte1 %#x is not link", __func__, pte1));
5172 			pte2 = pte2_load(pmap_pte2_quick(pmap, pv->pv_va));
5173 			rv = pte2_is_dirty(pte2);
5174 		}
5175 		PMAP_UNLOCK(pmap);
5176 		if (rv)
5177 			break;
5178 	}
5179 	sched_unpin();
5180 	return (rv);
5181 }
5182 
5183 /*
5184  *	pmap_is_modified:
5185  *
5186  *	Return whether or not the specified physical page was modified
5187  *	in any physical maps.
5188  */
5189 boolean_t
pmap_is_modified(vm_page_t m)5190 pmap_is_modified(vm_page_t m)
5191 {
5192 	boolean_t rv;
5193 
5194 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5195 	    ("%s: page %p is not managed", __func__, m));
5196 
5197 	/*
5198 	 * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
5199 	 * concurrently set while the object is locked.  Thus, if PGA_WRITEABLE
5200 	 * is clear, no PTE2s can have PG_M set.
5201 	 */
5202 	VM_OBJECT_ASSERT_WLOCKED(m->object);
5203 	if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
5204 		return (FALSE);
5205 	rw_wlock(&pvh_global_lock);
5206 	rv = pmap_is_modified_pvh(&m->md) ||
5207 	    ((m->flags & PG_FICTITIOUS) == 0 &&
5208 	    pmap_is_modified_pvh(pa_to_pvh(VM_PAGE_TO_PHYS(m))));
5209 	rw_wunlock(&pvh_global_lock);
5210 	return (rv);
5211 }
5212 
5213 /*
5214  *	pmap_is_prefaultable:
5215  *
5216  *	Return whether or not the specified virtual address is eligible
5217  *	for prefault.
5218  */
5219 boolean_t
pmap_is_prefaultable(pmap_t pmap,vm_offset_t addr)5220 pmap_is_prefaultable(pmap_t pmap, vm_offset_t addr)
5221 {
5222 	pt1_entry_t pte1;
5223 	pt2_entry_t pte2;
5224 	boolean_t rv;
5225 
5226 	rv = FALSE;
5227 	PMAP_LOCK(pmap);
5228 	pte1 = pte1_load(pmap_pte1(pmap, addr));
5229 	if (pte1_is_link(pte1)) {
5230 		pte2 = pte2_load(pt2map_entry(addr));
5231 		rv = !pte2_is_valid(pte2) ;
5232 	}
5233 	PMAP_UNLOCK(pmap);
5234 	return (rv);
5235 }
5236 
5237 /*
5238  *  Returns TRUE if any of the given mappings were referenced and FALSE
5239  *  otherwise. Both page and 1mpage mappings are supported.
5240  */
5241 static boolean_t
pmap_is_referenced_pvh(struct md_page * pvh)5242 pmap_is_referenced_pvh(struct md_page *pvh)
5243 {
5244 
5245 	pv_entry_t pv;
5246 	pt1_entry_t pte1;
5247 	pt2_entry_t pte2;
5248 	pmap_t pmap;
5249 	boolean_t rv;
5250 
5251 	rw_assert(&pvh_global_lock, RA_WLOCKED);
5252 	rv = FALSE;
5253 	sched_pin();
5254 	TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
5255 		pmap = PV_PMAP(pv);
5256 		PMAP_LOCK(pmap);
5257 		pte1 = pte1_load(pmap_pte1(pmap, pv->pv_va));
5258 		if (pte1_is_section(pte1)) {
5259 			rv = (pte1 & (PTE1_A | PTE1_V)) == (PTE1_A | PTE1_V);
5260 		} else {
5261 			pte2 = pte2_load(pmap_pte2_quick(pmap, pv->pv_va));
5262 			rv = (pte2 & (PTE2_A | PTE2_V)) == (PTE2_A | PTE2_V);
5263 		}
5264 		PMAP_UNLOCK(pmap);
5265 		if (rv)
5266 			break;
5267 	}
5268 	sched_unpin();
5269 	return (rv);
5270 }
5271 
5272 /*
5273  *	pmap_is_referenced:
5274  *
5275  *	Return whether or not the specified physical page was referenced
5276  *	in any physical maps.
5277  */
5278 boolean_t
pmap_is_referenced(vm_page_t m)5279 pmap_is_referenced(vm_page_t m)
5280 {
5281 	boolean_t rv;
5282 
5283 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5284 	    ("%s: page %p is not managed", __func__, m));
5285 	rw_wlock(&pvh_global_lock);
5286 	rv = pmap_is_referenced_pvh(&m->md) ||
5287 	    ((m->flags & PG_FICTITIOUS) == 0 &&
5288 	    pmap_is_referenced_pvh(pa_to_pvh(VM_PAGE_TO_PHYS(m))));
5289 	rw_wunlock(&pvh_global_lock);
5290 	return (rv);
5291 }
5292 
5293 /*
5294  *	pmap_ts_referenced:
5295  *
5296  *	Return a count of reference bits for a page, clearing those bits.
5297  *	It is not necessary for every reference bit to be cleared, but it
5298  *	is necessary that 0 only be returned when there are truly no
5299  *	reference bits set.
5300  *
5301  *	As an optimization, update the page's dirty field if a modified bit is
5302  *	found while counting reference bits.  This opportunistic update can be
5303  *	performed at low cost and can eliminate the need for some future calls
5304  *	to pmap_is_modified().  However, since this function stops after
5305  *	finding PMAP_TS_REFERENCED_MAX reference bits, it may not detect some
5306  *	dirty pages.  Those dirty pages will only be detected by a future call
5307  *	to pmap_is_modified().
5308  */
5309 int
pmap_ts_referenced(vm_page_t m)5310 pmap_ts_referenced(vm_page_t m)
5311 {
5312 	struct md_page *pvh;
5313 	pv_entry_t pv, pvf;
5314 	pmap_t pmap;
5315 	pt1_entry_t  *pte1p, opte1;
5316 	pt2_entry_t *pte2p, opte2;
5317 	vm_paddr_t pa;
5318 	int rtval = 0;
5319 
5320 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5321 	    ("%s: page %p is not managed", __func__, m));
5322 	pa = VM_PAGE_TO_PHYS(m);
5323 	pvh = pa_to_pvh(pa);
5324 	rw_wlock(&pvh_global_lock);
5325 	sched_pin();
5326 	if ((m->flags & PG_FICTITIOUS) != 0 ||
5327 	    (pvf = TAILQ_FIRST(&pvh->pv_list)) == NULL)
5328 		goto small_mappings;
5329 	pv = pvf;
5330 	do {
5331 		pmap = PV_PMAP(pv);
5332 		PMAP_LOCK(pmap);
5333 		pte1p = pmap_pte1(pmap, pv->pv_va);
5334 		opte1 = pte1_load(pte1p);
5335 		if (pte1_is_dirty(opte1)) {
5336 			/*
5337 			 * Although "opte1" is mapping a 1MB page, because
5338 			 * this function is called at a 4KB page granularity,
5339 			 * we only update the 4KB page under test.
5340 			 */
5341 			vm_page_dirty(m);
5342 		}
5343 		if ((opte1 & PTE1_A) != 0) {
5344 			/*
5345 			 * Since this reference bit is shared by 256 4KB pages,
5346 			 * it should not be cleared every time it is tested.
5347 			 * Apply a simple "hash" function on the physical page
5348 			 * number, the virtual section number, and the pmap
5349 			 * address to select one 4KB page out of the 256
5350 			 * on which testing the reference bit will result
5351 			 * in clearing that bit. This function is designed
5352 			 * to avoid the selection of the same 4KB page
5353 			 * for every 1MB page mapping.
5354 			 *
5355 			 * On demotion, a mapping that hasn't been referenced
5356 			 * is simply destroyed.  To avoid the possibility of a
5357 			 * subsequent page fault on a demoted wired mapping,
5358 			 * always leave its reference bit set.  Moreover,
5359 			 * since the section is wired, the current state of
5360 			 * its reference bit won't affect page replacement.
5361 			 */
5362 			 if ((((pa >> PAGE_SHIFT) ^ (pv->pv_va >> PTE1_SHIFT) ^
5363 			    (uintptr_t)pmap) & (NPTE2_IN_PG - 1)) == 0 &&
5364 			    !pte1_is_wired(opte1)) {
5365 				pte1_clear_bit(pte1p, PTE1_A);
5366 				pmap_tlb_flush(pmap, pv->pv_va);
5367 			}
5368 			rtval++;
5369 		}
5370 		PMAP_UNLOCK(pmap);
5371 		/* Rotate the PV list if it has more than one entry. */
5372 		if (TAILQ_NEXT(pv, pv_next) != NULL) {
5373 			TAILQ_REMOVE(&pvh->pv_list, pv, pv_next);
5374 			TAILQ_INSERT_TAIL(&pvh->pv_list, pv, pv_next);
5375 		}
5376 		if (rtval >= PMAP_TS_REFERENCED_MAX)
5377 			goto out;
5378 	} while ((pv = TAILQ_FIRST(&pvh->pv_list)) != pvf);
5379 small_mappings:
5380 	if ((pvf = TAILQ_FIRST(&m->md.pv_list)) == NULL)
5381 		goto out;
5382 	pv = pvf;
5383 	do {
5384 		pmap = PV_PMAP(pv);
5385 		PMAP_LOCK(pmap);
5386 		pte1p = pmap_pte1(pmap, pv->pv_va);
5387 		KASSERT(pte1_is_link(pte1_load(pte1p)),
5388 		    ("%s: not found a link in page %p's pv list", __func__, m));
5389 
5390 		pte2p = pmap_pte2_quick(pmap, pv->pv_va);
5391 		opte2 = pte2_load(pte2p);
5392 		if (pte2_is_dirty(opte2))
5393 			vm_page_dirty(m);
5394 		if ((opte2 & PTE2_A) != 0) {
5395 			pte2_clear_bit(pte2p, PTE2_A);
5396 			pmap_tlb_flush(pmap, pv->pv_va);
5397 			rtval++;
5398 		}
5399 		PMAP_UNLOCK(pmap);
5400 		/* Rotate the PV list if it has more than one entry. */
5401 		if (TAILQ_NEXT(pv, pv_next) != NULL) {
5402 			TAILQ_REMOVE(&m->md.pv_list, pv, pv_next);
5403 			TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_next);
5404 		}
5405 	} while ((pv = TAILQ_FIRST(&m->md.pv_list)) != pvf && rtval <
5406 	    PMAP_TS_REFERENCED_MAX);
5407 out:
5408 	sched_unpin();
5409 	rw_wunlock(&pvh_global_lock);
5410 	return (rtval);
5411 }
5412 
5413 /*
5414  *	Clear the wired attribute from the mappings for the specified range of
5415  *	addresses in the given pmap.  Every valid mapping within that range
5416  *	must have the wired attribute set.  In contrast, invalid mappings
5417  *	cannot have the wired attribute set, so they are ignored.
5418  *
5419  *	The wired attribute of the page table entry is not a hardware feature,
5420  *	so there is no need to invalidate any TLB entries.
5421  */
5422 void
pmap_unwire(pmap_t pmap,vm_offset_t sva,vm_offset_t eva)5423 pmap_unwire(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
5424 {
5425 	vm_offset_t nextva;
5426 	pt1_entry_t *pte1p, pte1;
5427 	pt2_entry_t *pte2p, pte2;
5428 	boolean_t pv_lists_locked;
5429 
5430 	if (pmap_is_current(pmap))
5431 		pv_lists_locked = FALSE;
5432 	else {
5433 		pv_lists_locked = TRUE;
5434 resume:
5435 		rw_wlock(&pvh_global_lock);
5436 		sched_pin();
5437 	}
5438 	PMAP_LOCK(pmap);
5439 	for (; sva < eva; sva = nextva) {
5440 		nextva = pte1_trunc(sva + PTE1_SIZE);
5441 		if (nextva < sva)
5442 			nextva = eva;
5443 
5444 		pte1p = pmap_pte1(pmap, sva);
5445 		pte1 = pte1_load(pte1p);
5446 
5447 		/*
5448 		 * Weed out invalid mappings. Note: we assume that L1 page
5449 		 * page table is always allocated, and in kernel virtual.
5450 		 */
5451 		if (pte1 == 0)
5452 			continue;
5453 
5454 		if (pte1_is_section(pte1)) {
5455 			if (!pte1_is_wired(pte1))
5456 				panic("%s: pte1 %#x not wired", __func__, pte1);
5457 
5458 			/*
5459 			 * Are we unwiring the entire large page?  If not,
5460 			 * demote the mapping and fall through.
5461 			 */
5462 			if (sva + PTE1_SIZE == nextva && eva >= nextva) {
5463 				pte1_clear_bit(pte1p, PTE1_W);
5464 				pmap->pm_stats.wired_count -= PTE1_SIZE /
5465 				    PAGE_SIZE;
5466 				continue;
5467 			} else {
5468 				if (!pv_lists_locked) {
5469 					pv_lists_locked = TRUE;
5470 					if (!rw_try_wlock(&pvh_global_lock)) {
5471 						PMAP_UNLOCK(pmap);
5472 						/* Repeat sva. */
5473 						goto resume;
5474 					}
5475 					sched_pin();
5476 				}
5477 				if (!pmap_demote_pte1(pmap, pte1p, sva))
5478 					panic("%s: demotion failed", __func__);
5479 #ifdef INVARIANTS
5480 				else {
5481 					/* Update pte1 after demotion */
5482 					pte1 = pte1_load(pte1p);
5483 				}
5484 #endif
5485 			}
5486 		}
5487 
5488 		KASSERT(pte1_is_link(pte1), ("%s: pmap %p va %#x pte1 %#x at %p"
5489 		    " is not link", __func__, pmap, sva, pte1, pte1p));
5490 
5491 		/*
5492 		 * Limit our scan to either the end of the va represented
5493 		 * by the current L2 page table page, or to the end of the
5494 		 * range being protected.
5495 		 */
5496 		if (nextva > eva)
5497 			nextva = eva;
5498 
5499 		for (pte2p = pmap_pte2_quick(pmap, sva); sva != nextva; pte2p++,
5500 		    sva += PAGE_SIZE) {
5501 			pte2 = pte2_load(pte2p);
5502 			if (!pte2_is_valid(pte2))
5503 				continue;
5504 			if (!pte2_is_wired(pte2))
5505 				panic("%s: pte2 %#x is missing PTE2_W",
5506 				    __func__, pte2);
5507 
5508 			/*
5509 			 * PTE2_W must be cleared atomically. Although the pmap
5510 			 * lock synchronizes access to PTE2_W, another processor
5511 			 * could be changing PTE2_NM and/or PTE2_A concurrently.
5512 			 */
5513 			pte2_clear_bit(pte2p, PTE2_W);
5514 			pmap->pm_stats.wired_count--;
5515 		}
5516 	}
5517 	if (pv_lists_locked) {
5518 		sched_unpin();
5519 		rw_wunlock(&pvh_global_lock);
5520 	}
5521 	PMAP_UNLOCK(pmap);
5522 }
5523 
5524 /*
5525  *  Clear the write and modified bits in each of the given page's mappings.
5526  */
5527 void
pmap_remove_write(vm_page_t m)5528 pmap_remove_write(vm_page_t m)
5529 {
5530 	struct md_page *pvh;
5531 	pv_entry_t next_pv, pv;
5532 	pmap_t pmap;
5533 	pt1_entry_t *pte1p;
5534 	pt2_entry_t *pte2p, opte2;
5535 	vm_offset_t va;
5536 
5537 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5538 	    ("%s: page %p is not managed", __func__, m));
5539 
5540 	/*
5541 	 * If the page is not exclusive busied, then PGA_WRITEABLE cannot be
5542 	 * set by another thread while the object is locked.  Thus,
5543 	 * if PGA_WRITEABLE is clear, no page table entries need updating.
5544 	 */
5545 	VM_OBJECT_ASSERT_WLOCKED(m->object);
5546 	if (!vm_page_xbusied(m) && (m->aflags & PGA_WRITEABLE) == 0)
5547 		return;
5548 	rw_wlock(&pvh_global_lock);
5549 	sched_pin();
5550 	if ((m->flags & PG_FICTITIOUS) != 0)
5551 		goto small_mappings;
5552 	pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
5553 	TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_next, next_pv) {
5554 		va = pv->pv_va;
5555 		pmap = PV_PMAP(pv);
5556 		PMAP_LOCK(pmap);
5557 		pte1p = pmap_pte1(pmap, va);
5558 		if (!(pte1_load(pte1p) & PTE1_RO))
5559 			(void)pmap_demote_pte1(pmap, pte1p, va);
5560 		PMAP_UNLOCK(pmap);
5561 	}
5562 small_mappings:
5563 	TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
5564 		pmap = PV_PMAP(pv);
5565 		PMAP_LOCK(pmap);
5566 		pte1p = pmap_pte1(pmap, pv->pv_va);
5567 		KASSERT(!pte1_is_section(pte1_load(pte1p)), ("%s: found"
5568 		    " a section in page %p's pv list", __func__, m));
5569 		pte2p = pmap_pte2_quick(pmap, pv->pv_va);
5570 		opte2 = pte2_load(pte2p);
5571 		if (!(opte2 & PTE2_RO)) {
5572 			pte2_store(pte2p, opte2 | PTE2_RO | PTE2_NM);
5573 			if (pte2_is_dirty(opte2))
5574 				vm_page_dirty(m);
5575 			pmap_tlb_flush(pmap, pv->pv_va);
5576 		}
5577 		PMAP_UNLOCK(pmap);
5578 	}
5579 	vm_page_aflag_clear(m, PGA_WRITEABLE);
5580 	sched_unpin();
5581 	rw_wunlock(&pvh_global_lock);
5582 }
5583 
5584 /*
5585  *	Apply the given advice to the specified range of addresses within the
5586  *	given pmap.  Depending on the advice, clear the referenced and/or
5587  *	modified flags in each mapping and set the mapped page's dirty field.
5588  */
5589 void
pmap_advise(pmap_t pmap,vm_offset_t sva,vm_offset_t eva,int advice)5590 pmap_advise(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, int advice)
5591 {
5592 	pt1_entry_t *pte1p, opte1;
5593 	pt2_entry_t *pte2p, pte2;
5594 	vm_offset_t pdnxt;
5595 	vm_page_t m;
5596 	boolean_t pv_lists_locked;
5597 
5598 	if (advice != MADV_DONTNEED && advice != MADV_FREE)
5599 		return;
5600 	if (pmap_is_current(pmap))
5601 		pv_lists_locked = FALSE;
5602 	else {
5603 		pv_lists_locked = TRUE;
5604 resume:
5605 		rw_wlock(&pvh_global_lock);
5606 		sched_pin();
5607 	}
5608 	PMAP_LOCK(pmap);
5609 	for (; sva < eva; sva = pdnxt) {
5610 		pdnxt = pte1_trunc(sva + PTE1_SIZE);
5611 		if (pdnxt < sva)
5612 			pdnxt = eva;
5613 		pte1p = pmap_pte1(pmap, sva);
5614 		opte1 = pte1_load(pte1p);
5615 		if (!pte1_is_valid(opte1)) /* XXX */
5616 			continue;
5617 		else if (pte1_is_section(opte1)) {
5618 			if (!pte1_is_managed(opte1))
5619 				continue;
5620 			if (!pv_lists_locked) {
5621 				pv_lists_locked = TRUE;
5622 				if (!rw_try_wlock(&pvh_global_lock)) {
5623 					PMAP_UNLOCK(pmap);
5624 					goto resume;
5625 				}
5626 				sched_pin();
5627 			}
5628 			if (!pmap_demote_pte1(pmap, pte1p, sva)) {
5629 				/*
5630 				 * The large page mapping was destroyed.
5631 				 */
5632 				continue;
5633 			}
5634 
5635 			/*
5636 			 * Unless the page mappings are wired, remove the
5637 			 * mapping to a single page so that a subsequent
5638 			 * access may repromote.  Since the underlying L2 page
5639 			 * table is fully populated, this removal never
5640 			 * frees a L2 page table page.
5641 			 */
5642 			if (!pte1_is_wired(opte1)) {
5643 				pte2p = pmap_pte2_quick(pmap, sva);
5644 				KASSERT(pte2_is_valid(pte2_load(pte2p)),
5645 				    ("%s: invalid PTE2", __func__));
5646 				pmap_remove_pte2(pmap, pte2p, sva, NULL);
5647 			}
5648 		}
5649 		if (pdnxt > eva)
5650 			pdnxt = eva;
5651 		for (pte2p = pmap_pte2_quick(pmap, sva); sva != pdnxt; pte2p++,
5652 		    sva += PAGE_SIZE) {
5653 			pte2 = pte2_load(pte2p);
5654 			if (!pte2_is_valid(pte2) || !pte2_is_managed(pte2))
5655 				continue;
5656 			else if (pte2_is_dirty(pte2)) {
5657 				if (advice == MADV_DONTNEED) {
5658 					/*
5659 					 * Future calls to pmap_is_modified()
5660 					 * can be avoided by making the page
5661 					 * dirty now.
5662 					 */
5663 					m = PHYS_TO_VM_PAGE(pte2_pa(pte2));
5664 					vm_page_dirty(m);
5665 				}
5666 				pte2_set_bit(pte2p, PTE2_NM);
5667 				pte2_clear_bit(pte2p, PTE2_A);
5668 			} else if ((pte2 & PTE2_A) != 0)
5669 				pte2_clear_bit(pte2p, PTE2_A);
5670 			else
5671 				continue;
5672 			pmap_tlb_flush(pmap, sva);
5673 		}
5674 	}
5675 	if (pv_lists_locked) {
5676 		sched_unpin();
5677 		rw_wunlock(&pvh_global_lock);
5678 	}
5679 	PMAP_UNLOCK(pmap);
5680 }
5681 
5682 /*
5683  *	Clear the modify bits on the specified physical page.
5684  */
5685 void
pmap_clear_modify(vm_page_t m)5686 pmap_clear_modify(vm_page_t m)
5687 {
5688 	struct md_page *pvh;
5689 	pv_entry_t next_pv, pv;
5690 	pmap_t pmap;
5691 	pt1_entry_t *pte1p, opte1;
5692 	pt2_entry_t *pte2p, opte2;
5693 	vm_offset_t va;
5694 
5695 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5696 	    ("%s: page %p is not managed", __func__, m));
5697 	VM_OBJECT_ASSERT_WLOCKED(m->object);
5698 	KASSERT(!vm_page_xbusied(m),
5699 	    ("%s: page %p is exclusive busy", __func__, m));
5700 
5701 	/*
5702 	 * If the page is not PGA_WRITEABLE, then no PTE2s can have PTE2_NM
5703 	 * cleared. If the object containing the page is locked and the page
5704 	 * is not exclusive busied, then PGA_WRITEABLE cannot be concurrently
5705 	 * set.
5706 	 */
5707 	if ((m->flags & PGA_WRITEABLE) == 0)
5708 		return;
5709 	rw_wlock(&pvh_global_lock);
5710 	sched_pin();
5711 	if ((m->flags & PG_FICTITIOUS) != 0)
5712 		goto small_mappings;
5713 	pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
5714 	TAILQ_FOREACH_SAFE(pv, &pvh->pv_list, pv_next, next_pv) {
5715 		va = pv->pv_va;
5716 		pmap = PV_PMAP(pv);
5717 		PMAP_LOCK(pmap);
5718 		pte1p = pmap_pte1(pmap, va);
5719 		opte1 = pte1_load(pte1p);
5720 		if (!(opte1 & PTE1_RO)) {
5721 			if (pmap_demote_pte1(pmap, pte1p, va) &&
5722 			    !pte1_is_wired(opte1)) {
5723 				/*
5724 				 * Write protect the mapping to a
5725 				 * single page so that a subsequent
5726 				 * write access may repromote.
5727 				 */
5728 				va += VM_PAGE_TO_PHYS(m) - pte1_pa(opte1);
5729 				pte2p = pmap_pte2_quick(pmap, va);
5730 				opte2 = pte2_load(pte2p);
5731 				if ((opte2 & PTE2_V)) {
5732 					pte2_set_bit(pte2p, PTE2_NM | PTE2_RO);
5733 					vm_page_dirty(m);
5734 					pmap_tlb_flush(pmap, va);
5735 				}
5736 			}
5737 		}
5738 		PMAP_UNLOCK(pmap);
5739 	}
5740 small_mappings:
5741 	TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
5742 		pmap = PV_PMAP(pv);
5743 		PMAP_LOCK(pmap);
5744 		pte1p = pmap_pte1(pmap, pv->pv_va);
5745 		KASSERT(!pte1_is_section(pte1_load(pte1p)), ("%s: found"
5746 		    " a section in page %p's pv list", __func__, m));
5747 		pte2p = pmap_pte2_quick(pmap, pv->pv_va);
5748 		if (pte2_is_dirty(pte2_load(pte2p))) {
5749 			pte2_set_bit(pte2p, PTE2_NM);
5750 			pmap_tlb_flush(pmap, pv->pv_va);
5751 		}
5752 		PMAP_UNLOCK(pmap);
5753 	}
5754 	sched_unpin();
5755 	rw_wunlock(&pvh_global_lock);
5756 }
5757 
5758 
5759 /*
5760  *  Sets the memory attribute for the specified page.
5761  */
5762 void
pmap_page_set_memattr(vm_page_t m,vm_memattr_t ma)5763 pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma)
5764 {
5765 	pt2_entry_t *cmap2_pte2p;
5766 	vm_memattr_t oma;
5767 	vm_paddr_t pa;
5768 	struct pcpu *pc;
5769 
5770 	oma = m->md.pat_mode;
5771 	m->md.pat_mode = ma;
5772 
5773 	CTR5(KTR_PMAP, "%s: page %p - 0x%08X oma: %d, ma: %d", __func__, m,
5774 	    VM_PAGE_TO_PHYS(m), oma, ma);
5775 	if ((m->flags & PG_FICTITIOUS) != 0)
5776 		return;
5777 #if 0
5778 	/*
5779 	 * If "m" is a normal page, flush it from the cache.
5780 	 *
5781 	 * First, try to find an existing mapping of the page by sf
5782 	 * buffer. sf_buf_invalidate_cache() modifies mapping and
5783 	 * flushes the cache.
5784 	 */
5785 	if (sf_buf_invalidate_cache(m, oma))
5786 		return;
5787 #endif
5788 	/*
5789 	 * If page is not mapped by sf buffer, map the page
5790 	 * transient and do invalidation.
5791 	 */
5792 	if (ma != oma) {
5793 		pa = VM_PAGE_TO_PHYS(m);
5794 		sched_pin();
5795 		pc = get_pcpu();
5796 		cmap2_pte2p = pc->pc_cmap2_pte2p;
5797 		mtx_lock(&pc->pc_cmap_lock);
5798 		if (pte2_load(cmap2_pte2p) != 0)
5799 			panic("%s: CMAP2 busy", __func__);
5800 		pte2_store(cmap2_pte2p, PTE2_KERN_NG(pa, PTE2_AP_KRW,
5801 		    vm_memattr_to_pte2(ma)));
5802 		dcache_wbinv_poc((vm_offset_t)pc->pc_cmap2_addr, pa, PAGE_SIZE);
5803 		pte2_clear(cmap2_pte2p);
5804 		tlb_flush((vm_offset_t)pc->pc_cmap2_addr);
5805 		sched_unpin();
5806 		mtx_unlock(&pc->pc_cmap_lock);
5807 	}
5808 }
5809 
5810 /*
5811  *  Miscellaneous support routines follow
5812  */
5813 
5814 /*
5815  *  Returns TRUE if the given page is mapped individually or as part of
5816  *  a 1mpage.  Otherwise, returns FALSE.
5817  */
5818 boolean_t
pmap_page_is_mapped(vm_page_t m)5819 pmap_page_is_mapped(vm_page_t m)
5820 {
5821 	boolean_t rv;
5822 
5823 	if ((m->oflags & VPO_UNMANAGED) != 0)
5824 		return (FALSE);
5825 	rw_wlock(&pvh_global_lock);
5826 	rv = !TAILQ_EMPTY(&m->md.pv_list) ||
5827 	    ((m->flags & PG_FICTITIOUS) == 0 &&
5828 	    !TAILQ_EMPTY(&pa_to_pvh(VM_PAGE_TO_PHYS(m))->pv_list));
5829 	rw_wunlock(&pvh_global_lock);
5830 	return (rv);
5831 }
5832 
5833 /*
5834  *  Returns true if the pmap's pv is one of the first
5835  *  16 pvs linked to from this page.  This count may
5836  *  be changed upwards or downwards in the future; it
5837  *  is only necessary that true be returned for a small
5838  *  subset of pmaps for proper page aging.
5839  */
5840 boolean_t
pmap_page_exists_quick(pmap_t pmap,vm_page_t m)5841 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
5842 {
5843 	struct md_page *pvh;
5844 	pv_entry_t pv;
5845 	int loops = 0;
5846 	boolean_t rv;
5847 
5848 	KASSERT((m->oflags & VPO_UNMANAGED) == 0,
5849 	    ("%s: page %p is not managed", __func__, m));
5850 	rv = FALSE;
5851 	rw_wlock(&pvh_global_lock);
5852 	TAILQ_FOREACH(pv, &m->md.pv_list, pv_next) {
5853 		if (PV_PMAP(pv) == pmap) {
5854 			rv = TRUE;
5855 			break;
5856 		}
5857 		loops++;
5858 		if (loops >= 16)
5859 			break;
5860 	}
5861 	if (!rv && loops < 16 && (m->flags & PG_FICTITIOUS) == 0) {
5862 		pvh = pa_to_pvh(VM_PAGE_TO_PHYS(m));
5863 		TAILQ_FOREACH(pv, &pvh->pv_list, pv_next) {
5864 			if (PV_PMAP(pv) == pmap) {
5865 				rv = TRUE;
5866 				break;
5867 			}
5868 			loops++;
5869 			if (loops >= 16)
5870 				break;
5871 		}
5872 	}
5873 	rw_wunlock(&pvh_global_lock);
5874 	return (rv);
5875 }
5876 
5877 /*
5878  *	pmap_zero_page zeros the specified hardware page by mapping
5879  *	the page into KVM and using bzero to clear its contents.
5880  */
5881 void
pmap_zero_page(vm_page_t m)5882 pmap_zero_page(vm_page_t m)
5883 {
5884 	pt2_entry_t *cmap2_pte2p;
5885 	struct pcpu *pc;
5886 
5887 	sched_pin();
5888 	pc = get_pcpu();
5889 	cmap2_pte2p = pc->pc_cmap2_pte2p;
5890 	mtx_lock(&pc->pc_cmap_lock);
5891 	if (pte2_load(cmap2_pte2p) != 0)
5892 		panic("%s: CMAP2 busy", __func__);
5893 	pte2_store(cmap2_pte2p, PTE2_KERN_NG(VM_PAGE_TO_PHYS(m), PTE2_AP_KRW,
5894 	    vm_page_pte2_attr(m)));
5895 	pagezero(pc->pc_cmap2_addr);
5896 	pte2_clear(cmap2_pte2p);
5897 	tlb_flush((vm_offset_t)pc->pc_cmap2_addr);
5898 	sched_unpin();
5899 	mtx_unlock(&pc->pc_cmap_lock);
5900 }
5901 
5902 /*
5903  *	pmap_zero_page_area zeros the specified hardware page by mapping
5904  *	the page into KVM and using bzero to clear its contents.
5905  *
5906  *	off and size may not cover an area beyond a single hardware page.
5907  */
5908 void
pmap_zero_page_area(vm_page_t m,int off,int size)5909 pmap_zero_page_area(vm_page_t m, int off, int size)
5910 {
5911 	pt2_entry_t *cmap2_pte2p;
5912 	struct pcpu *pc;
5913 
5914 	sched_pin();
5915 	pc = get_pcpu();
5916 	cmap2_pte2p = pc->pc_cmap2_pte2p;
5917 	mtx_lock(&pc->pc_cmap_lock);
5918 	if (pte2_load(cmap2_pte2p) != 0)
5919 		panic("%s: CMAP2 busy", __func__);
5920 	pte2_store(cmap2_pte2p, PTE2_KERN_NG(VM_PAGE_TO_PHYS(m), PTE2_AP_KRW,
5921 	    vm_page_pte2_attr(m)));
5922 	if (off == 0 && size == PAGE_SIZE)
5923 		pagezero(pc->pc_cmap2_addr);
5924 	else
5925 		bzero(pc->pc_cmap2_addr + off, size);
5926 	pte2_clear(cmap2_pte2p);
5927 	tlb_flush((vm_offset_t)pc->pc_cmap2_addr);
5928 	sched_unpin();
5929 	mtx_unlock(&pc->pc_cmap_lock);
5930 }
5931 
5932 /*
5933  *	pmap_copy_page copies the specified (machine independent)
5934  *	page by mapping the page into virtual memory and using
5935  *	bcopy to copy the page, one machine dependent page at a
5936  *	time.
5937  */
5938 void
pmap_copy_page(vm_page_t src,vm_page_t dst)5939 pmap_copy_page(vm_page_t src, vm_page_t dst)
5940 {
5941 	pt2_entry_t *cmap1_pte2p, *cmap2_pte2p;
5942 	struct pcpu *pc;
5943 
5944 	sched_pin();
5945 	pc = get_pcpu();
5946 	cmap1_pte2p = pc->pc_cmap1_pte2p;
5947 	cmap2_pte2p = pc->pc_cmap2_pte2p;
5948 	mtx_lock(&pc->pc_cmap_lock);
5949 	if (pte2_load(cmap1_pte2p) != 0)
5950 		panic("%s: CMAP1 busy", __func__);
5951 	if (pte2_load(cmap2_pte2p) != 0)
5952 		panic("%s: CMAP2 busy", __func__);
5953 	pte2_store(cmap1_pte2p, PTE2_KERN_NG(VM_PAGE_TO_PHYS(src),
5954 	    PTE2_AP_KR | PTE2_NM, vm_page_pte2_attr(src)));
5955 	pte2_store(cmap2_pte2p, PTE2_KERN_NG(VM_PAGE_TO_PHYS(dst),
5956 	    PTE2_AP_KRW, vm_page_pte2_attr(dst)));
5957 	bcopy(pc->pc_cmap1_addr, pc->pc_cmap2_addr, PAGE_SIZE);
5958 	pte2_clear(cmap1_pte2p);
5959 	tlb_flush((vm_offset_t)pc->pc_cmap1_addr);
5960 	pte2_clear(cmap2_pte2p);
5961 	tlb_flush((vm_offset_t)pc->pc_cmap2_addr);
5962 	sched_unpin();
5963 	mtx_unlock(&pc->pc_cmap_lock);
5964 }
5965 
5966 int unmapped_buf_allowed = 1;
5967 
5968 void
pmap_copy_pages(vm_page_t ma[],vm_offset_t a_offset,vm_page_t mb[],vm_offset_t b_offset,int xfersize)5969 pmap_copy_pages(vm_page_t ma[], vm_offset_t a_offset, vm_page_t mb[],
5970     vm_offset_t b_offset, int xfersize)
5971 {
5972 	pt2_entry_t *cmap1_pte2p, *cmap2_pte2p;
5973 	vm_page_t a_pg, b_pg;
5974 	char *a_cp, *b_cp;
5975 	vm_offset_t a_pg_offset, b_pg_offset;
5976 	struct pcpu *pc;
5977 	int cnt;
5978 
5979 	sched_pin();
5980 	pc = get_pcpu();
5981 	cmap1_pte2p = pc->pc_cmap1_pte2p;
5982 	cmap2_pte2p = pc->pc_cmap2_pte2p;
5983 	mtx_lock(&pc->pc_cmap_lock);
5984 	if (pte2_load(cmap1_pte2p) != 0)
5985 		panic("pmap_copy_pages: CMAP1 busy");
5986 	if (pte2_load(cmap2_pte2p) != 0)
5987 		panic("pmap_copy_pages: CMAP2 busy");
5988 	while (xfersize > 0) {
5989 		a_pg = ma[a_offset >> PAGE_SHIFT];
5990 		a_pg_offset = a_offset & PAGE_MASK;
5991 		cnt = min(xfersize, PAGE_SIZE - a_pg_offset);
5992 		b_pg = mb[b_offset >> PAGE_SHIFT];
5993 		b_pg_offset = b_offset & PAGE_MASK;
5994 		cnt = min(cnt, PAGE_SIZE - b_pg_offset);
5995 		pte2_store(cmap1_pte2p, PTE2_KERN_NG(VM_PAGE_TO_PHYS(a_pg),
5996 		    PTE2_AP_KR | PTE2_NM, vm_page_pte2_attr(a_pg)));
5997 		tlb_flush_local((vm_offset_t)pc->pc_cmap1_addr);
5998 		pte2_store(cmap2_pte2p, PTE2_KERN_NG(VM_PAGE_TO_PHYS(b_pg),
5999 		    PTE2_AP_KRW, vm_page_pte2_attr(b_pg)));
6000 		tlb_flush_local((vm_offset_t)pc->pc_cmap2_addr);
6001 		a_cp = pc->pc_cmap1_addr + a_pg_offset;
6002 		b_cp = pc->pc_cmap2_addr + b_pg_offset;
6003 		bcopy(a_cp, b_cp, cnt);
6004 		a_offset += cnt;
6005 		b_offset += cnt;
6006 		xfersize -= cnt;
6007 	}
6008 	pte2_clear(cmap1_pte2p);
6009 	tlb_flush((vm_offset_t)pc->pc_cmap1_addr);
6010 	pte2_clear(cmap2_pte2p);
6011 	tlb_flush((vm_offset_t)pc->pc_cmap2_addr);
6012 	sched_unpin();
6013 	mtx_unlock(&pc->pc_cmap_lock);
6014 }
6015 
6016 vm_offset_t
pmap_quick_enter_page(vm_page_t m)6017 pmap_quick_enter_page(vm_page_t m)
6018 {
6019 	struct pcpu *pc;
6020 	pt2_entry_t *pte2p;
6021 
6022 	critical_enter();
6023 	pc = get_pcpu();
6024 	pte2p = pc->pc_qmap_pte2p;
6025 
6026 	KASSERT(pte2_load(pte2p) == 0, ("%s: PTE2 busy", __func__));
6027 
6028 	pte2_store(pte2p, PTE2_KERN_NG(VM_PAGE_TO_PHYS(m), PTE2_AP_KRW,
6029 	    vm_page_pte2_attr(m)));
6030 	return (pc->pc_qmap_addr);
6031 }
6032 
6033 void
pmap_quick_remove_page(vm_offset_t addr)6034 pmap_quick_remove_page(vm_offset_t addr)
6035 {
6036 	struct pcpu *pc;
6037 	pt2_entry_t *pte2p;
6038 
6039 	pc = get_pcpu();
6040 	pte2p = pc->pc_qmap_pte2p;
6041 
6042 	KASSERT(addr == pc->pc_qmap_addr, ("%s: invalid address", __func__));
6043 	KASSERT(pte2_load(pte2p) != 0, ("%s: PTE2 not in use", __func__));
6044 
6045 	pte2_clear(pte2p);
6046 	tlb_flush(pc->pc_qmap_addr);
6047 	critical_exit();
6048 }
6049 
6050 /*
6051  *	Copy the range specified by src_addr/len
6052  *	from the source map to the range dst_addr/len
6053  *	in the destination map.
6054  *
6055  *	This routine is only advisory and need not do anything.
6056  */
6057 void
pmap_copy(pmap_t dst_pmap,pmap_t src_pmap,vm_offset_t dst_addr,vm_size_t len,vm_offset_t src_addr)6058 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, vm_size_t len,
6059     vm_offset_t src_addr)
6060 {
6061 	struct spglist free;
6062 	vm_offset_t addr;
6063 	vm_offset_t end_addr = src_addr + len;
6064 	vm_offset_t nextva;
6065 
6066 	if (dst_addr != src_addr)
6067 		return;
6068 
6069 	if (!pmap_is_current(src_pmap))
6070 		return;
6071 
6072 	rw_wlock(&pvh_global_lock);
6073 	if (dst_pmap < src_pmap) {
6074 		PMAP_LOCK(dst_pmap);
6075 		PMAP_LOCK(src_pmap);
6076 	} else {
6077 		PMAP_LOCK(src_pmap);
6078 		PMAP_LOCK(dst_pmap);
6079 	}
6080 	sched_pin();
6081 	for (addr = src_addr; addr < end_addr; addr = nextva) {
6082 		pt2_entry_t *src_pte2p, *dst_pte2p;
6083 		vm_page_t dst_mpt2pg, src_mpt2pg;
6084 		pt1_entry_t src_pte1;
6085 		u_int pte1_idx;
6086 
6087 		KASSERT(addr < VM_MAXUSER_ADDRESS,
6088 		    ("%s: invalid to pmap_copy page tables", __func__));
6089 
6090 		nextva = pte1_trunc(addr + PTE1_SIZE);
6091 		if (nextva < addr)
6092 			nextva = end_addr;
6093 
6094 		pte1_idx = pte1_index(addr);
6095 		src_pte1 = src_pmap->pm_pt1[pte1_idx];
6096 		if (pte1_is_section(src_pte1)) {
6097 			if ((addr & PTE1_OFFSET) != 0 ||
6098 			    (addr + PTE1_SIZE) > end_addr)
6099 				continue;
6100 			if (dst_pmap->pm_pt1[pte1_idx] == 0 &&
6101 			    (!pte1_is_managed(src_pte1) ||
6102 			    pmap_pv_insert_pte1(dst_pmap, addr, src_pte1,
6103 			    PMAP_ENTER_NORECLAIM))) {
6104 				dst_pmap->pm_pt1[pte1_idx] = src_pte1 &
6105 				    ~PTE1_W;
6106 				dst_pmap->pm_stats.resident_count +=
6107 				    PTE1_SIZE / PAGE_SIZE;
6108 				pmap_pte1_mappings++;
6109 			}
6110 			continue;
6111 		} else if (!pte1_is_link(src_pte1))
6112 			continue;
6113 
6114 		src_mpt2pg = PHYS_TO_VM_PAGE(pte1_link_pa(src_pte1));
6115 
6116 		/*
6117 		 * We leave PT2s to be linked from PT1 even if they are not
6118 		 * referenced until all PT2s in a page are without reference.
6119 		 *
6120 		 * QQQ: It could be changed ...
6121 		 */
6122 #if 0 /* single_pt2_link_is_cleared */
6123 		KASSERT(pt2_wirecount_get(src_mpt2pg, pte1_idx) > 0,
6124 		    ("%s: source page table page is unused", __func__));
6125 #else
6126 		if (pt2_wirecount_get(src_mpt2pg, pte1_idx) == 0)
6127 			continue;
6128 #endif
6129 		if (nextva > end_addr)
6130 			nextva = end_addr;
6131 
6132 		src_pte2p = pt2map_entry(addr);
6133 		while (addr < nextva) {
6134 			pt2_entry_t temp_pte2;
6135 			temp_pte2 = pte2_load(src_pte2p);
6136 			/*
6137 			 * we only virtual copy managed pages
6138 			 */
6139 			if (pte2_is_managed(temp_pte2)) {
6140 				dst_mpt2pg = pmap_allocpte2(dst_pmap, addr,
6141 				    PMAP_ENTER_NOSLEEP);
6142 				if (dst_mpt2pg == NULL)
6143 					goto out;
6144 				dst_pte2p = pmap_pte2_quick(dst_pmap, addr);
6145 				if (!pte2_is_valid(pte2_load(dst_pte2p)) &&
6146 				    pmap_try_insert_pv_entry(dst_pmap, addr,
6147 				    PHYS_TO_VM_PAGE(pte2_pa(temp_pte2)))) {
6148 					/*
6149 					 * Clear the wired, modified, and
6150 					 * accessed (referenced) bits
6151 					 * during the copy.
6152 					 */
6153 					temp_pte2 &=  ~(PTE2_W | PTE2_A);
6154 					temp_pte2 |= PTE2_NM;
6155 					pte2_store(dst_pte2p, temp_pte2);
6156 					dst_pmap->pm_stats.resident_count++;
6157 				} else {
6158 					SLIST_INIT(&free);
6159 					if (pmap_unwire_pt2(dst_pmap, addr,
6160 					    dst_mpt2pg, &free)) {
6161 						pmap_tlb_flush(dst_pmap, addr);
6162 						vm_page_free_pages_toq(&free,
6163 						    false);
6164 					}
6165 					goto out;
6166 				}
6167 				if (pt2_wirecount_get(dst_mpt2pg, pte1_idx) >=
6168 				    pt2_wirecount_get(src_mpt2pg, pte1_idx))
6169 					break;
6170 			}
6171 			addr += PAGE_SIZE;
6172 			src_pte2p++;
6173 		}
6174 	}
6175 out:
6176 	sched_unpin();
6177 	rw_wunlock(&pvh_global_lock);
6178 	PMAP_UNLOCK(src_pmap);
6179 	PMAP_UNLOCK(dst_pmap);
6180 }
6181 
6182 /*
6183  *	Increase the starting virtual address of the given mapping if a
6184  *	different alignment might result in more section mappings.
6185  */
6186 void
pmap_align_superpage(vm_object_t object,vm_ooffset_t offset,vm_offset_t * addr,vm_size_t size)6187 pmap_align_superpage(vm_object_t object, vm_ooffset_t offset,
6188     vm_offset_t *addr, vm_size_t size)
6189 {
6190 	vm_offset_t pte1_offset;
6191 
6192 	if (size < PTE1_SIZE)
6193 		return;
6194 	if (object != NULL && (object->flags & OBJ_COLORED) != 0)
6195 		offset += ptoa(object->pg_color);
6196 	pte1_offset = offset & PTE1_OFFSET;
6197 	if (size - ((PTE1_SIZE - pte1_offset) & PTE1_OFFSET) < PTE1_SIZE ||
6198 	    (*addr & PTE1_OFFSET) == pte1_offset)
6199 		return;
6200 	if ((*addr & PTE1_OFFSET) < pte1_offset)
6201 		*addr = pte1_trunc(*addr) + pte1_offset;
6202 	else
6203 		*addr = pte1_roundup(*addr) + pte1_offset;
6204 }
6205 
6206 void
pmap_activate(struct thread * td)6207 pmap_activate(struct thread *td)
6208 {
6209 	pmap_t pmap, oldpmap;
6210 	u_int cpuid, ttb;
6211 
6212 	PDEBUG(9, printf("%s: td = %08x\n", __func__, (uint32_t)td));
6213 
6214 	critical_enter();
6215 	pmap = vmspace_pmap(td->td_proc->p_vmspace);
6216 	oldpmap = PCPU_GET(curpmap);
6217 	cpuid = PCPU_GET(cpuid);
6218 
6219 #if defined(SMP)
6220 	CPU_CLR_ATOMIC(cpuid, &oldpmap->pm_active);
6221 	CPU_SET_ATOMIC(cpuid, &pmap->pm_active);
6222 #else
6223 	CPU_CLR(cpuid, &oldpmap->pm_active);
6224 	CPU_SET(cpuid, &pmap->pm_active);
6225 #endif
6226 
6227 	ttb = pmap_ttb_get(pmap);
6228 
6229 	/*
6230 	 * pmap_activate is for the current thread on the current cpu
6231 	 */
6232 	td->td_pcb->pcb_pagedir = ttb;
6233 	cp15_ttbr_set(ttb);
6234 	PCPU_SET(curpmap, pmap);
6235 	critical_exit();
6236 }
6237 
6238 /*
6239  *  Perform the pmap work for mincore.
6240  */
6241 int
pmap_mincore(pmap_t pmap,vm_offset_t addr,vm_paddr_t * locked_pa)6242 pmap_mincore(pmap_t pmap, vm_offset_t addr, vm_paddr_t *locked_pa)
6243 {
6244 	pt1_entry_t *pte1p, pte1;
6245 	pt2_entry_t *pte2p, pte2;
6246 	vm_paddr_t pa;
6247 	bool managed;
6248 	int val;
6249 
6250 	PMAP_LOCK(pmap);
6251 retry:
6252 	pte1p = pmap_pte1(pmap, addr);
6253 	pte1 = pte1_load(pte1p);
6254 	if (pte1_is_section(pte1)) {
6255 		pa = trunc_page(pte1_pa(pte1) | (addr & PTE1_OFFSET));
6256 		managed = pte1_is_managed(pte1);
6257 		val = MINCORE_SUPER | MINCORE_INCORE;
6258 		if (pte1_is_dirty(pte1))
6259 			val |= MINCORE_MODIFIED | MINCORE_MODIFIED_OTHER;
6260 		if (pte1 & PTE1_A)
6261 			val |= MINCORE_REFERENCED | MINCORE_REFERENCED_OTHER;
6262 	} else if (pte1_is_link(pte1)) {
6263 		pte2p = pmap_pte2(pmap, addr);
6264 		pte2 = pte2_load(pte2p);
6265 		pmap_pte2_release(pte2p);
6266 		pa = pte2_pa(pte2);
6267 		managed = pte2_is_managed(pte2);
6268 		val = MINCORE_INCORE;
6269 		if (pte2_is_dirty(pte2))
6270 			val |= MINCORE_MODIFIED | MINCORE_MODIFIED_OTHER;
6271 		if (pte2 & PTE2_A)
6272 			val |= MINCORE_REFERENCED | MINCORE_REFERENCED_OTHER;
6273 	} else {
6274 		managed = false;
6275 		val = 0;
6276 	}
6277 	if ((val & (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER)) !=
6278 	    (MINCORE_MODIFIED_OTHER | MINCORE_REFERENCED_OTHER) && managed) {
6279 		/* Ensure that "PHYS_TO_VM_PAGE(pa)->object" doesn't change. */
6280 		if (vm_page_pa_tryrelock(pmap, pa, locked_pa))
6281 			goto retry;
6282 	} else
6283 		PA_UNLOCK_COND(*locked_pa);
6284 	PMAP_UNLOCK(pmap);
6285 	return (val);
6286 }
6287 
6288 void
pmap_kenter_device(vm_offset_t va,vm_size_t size,vm_paddr_t pa)6289 pmap_kenter_device(vm_offset_t va, vm_size_t size, vm_paddr_t pa)
6290 {
6291 	vm_offset_t sva;
6292 	uint32_t l2attr;
6293 
6294 	KASSERT((size & PAGE_MASK) == 0,
6295 	    ("%s: device mapping not page-sized", __func__));
6296 
6297 	sva = va;
6298 	l2attr = vm_memattr_to_pte2(VM_MEMATTR_DEVICE);
6299 	while (size != 0) {
6300 		pmap_kenter_prot_attr(va, pa, PTE2_AP_KRW, l2attr);
6301 		va += PAGE_SIZE;
6302 		pa += PAGE_SIZE;
6303 		size -= PAGE_SIZE;
6304 	}
6305 	tlb_flush_range(sva, va - sva);
6306 }
6307 
6308 void
pmap_kremove_device(vm_offset_t va,vm_size_t size)6309 pmap_kremove_device(vm_offset_t va, vm_size_t size)
6310 {
6311 	vm_offset_t sva;
6312 
6313 	KASSERT((size & PAGE_MASK) == 0,
6314 	    ("%s: device mapping not page-sized", __func__));
6315 
6316 	sva = va;
6317 	while (size != 0) {
6318 		pmap_kremove(va);
6319 		va += PAGE_SIZE;
6320 		size -= PAGE_SIZE;
6321 	}
6322 	tlb_flush_range(sva, va - sva);
6323 }
6324 
6325 void
pmap_set_pcb_pagedir(pmap_t pmap,struct pcb * pcb)6326 pmap_set_pcb_pagedir(pmap_t pmap, struct pcb *pcb)
6327 {
6328 
6329 	pcb->pcb_pagedir = pmap_ttb_get(pmap);
6330 }
6331 
6332 
6333 /*
6334  *  Clean L1 data cache range by physical address.
6335  *  The range must be within a single page.
6336  */
6337 static void
pmap_dcache_wb_pou(vm_paddr_t pa,vm_size_t size,uint32_t attr)6338 pmap_dcache_wb_pou(vm_paddr_t pa, vm_size_t size, uint32_t attr)
6339 {
6340 	pt2_entry_t *cmap2_pte2p;
6341 	struct pcpu *pc;
6342 
6343 	KASSERT(((pa & PAGE_MASK) + size) <= PAGE_SIZE,
6344 	    ("%s: not on single page", __func__));
6345 
6346 	sched_pin();
6347 	pc = get_pcpu();
6348 	cmap2_pte2p = pc->pc_cmap2_pte2p;
6349 	mtx_lock(&pc->pc_cmap_lock);
6350 	if (pte2_load(cmap2_pte2p) != 0)
6351 		panic("%s: CMAP2 busy", __func__);
6352 	pte2_store(cmap2_pte2p, PTE2_KERN_NG(pa, PTE2_AP_KRW, attr));
6353 	dcache_wb_pou((vm_offset_t)pc->pc_cmap2_addr + (pa & PAGE_MASK), size);
6354 	pte2_clear(cmap2_pte2p);
6355 	tlb_flush((vm_offset_t)pc->pc_cmap2_addr);
6356 	sched_unpin();
6357 	mtx_unlock(&pc->pc_cmap_lock);
6358 }
6359 
6360 /*
6361  *  Sync instruction cache range which is not mapped yet.
6362  */
6363 void
cache_icache_sync_fresh(vm_offset_t va,vm_paddr_t pa,vm_size_t size)6364 cache_icache_sync_fresh(vm_offset_t va, vm_paddr_t pa, vm_size_t size)
6365 {
6366 	uint32_t len, offset;
6367 	vm_page_t m;
6368 
6369 	/* Write back d-cache on given address range. */
6370 	offset = pa & PAGE_MASK;
6371 	for ( ; size != 0; size -= len, pa += len, offset = 0) {
6372 		len = min(PAGE_SIZE - offset, size);
6373 		m = PHYS_TO_VM_PAGE(pa);
6374 		KASSERT(m != NULL, ("%s: vm_page_t is null for %#x",
6375 		  __func__, pa));
6376 		pmap_dcache_wb_pou(pa, len, vm_page_pte2_attr(m));
6377 	}
6378 	/*
6379 	 * I-cache is VIPT. Only way how to flush all virtual mappings
6380 	 * on given physical address is to invalidate all i-cache.
6381 	 */
6382 	icache_inv_all();
6383 }
6384 
6385 void
pmap_sync_icache(pmap_t pmap,vm_offset_t va,vm_size_t size)6386 pmap_sync_icache(pmap_t pmap, vm_offset_t va, vm_size_t size)
6387 {
6388 
6389 	/* Write back d-cache on given address range. */
6390 	if (va >= VM_MIN_KERNEL_ADDRESS) {
6391 		dcache_wb_pou(va, size);
6392 	} else {
6393 		uint32_t len, offset;
6394 		vm_paddr_t pa;
6395 		vm_page_t m;
6396 
6397 		offset = va & PAGE_MASK;
6398 		for ( ; size != 0; size -= len, va += len, offset = 0) {
6399 			pa = pmap_extract(pmap, va); /* offset is preserved */
6400 			len = min(PAGE_SIZE - offset, size);
6401 			m = PHYS_TO_VM_PAGE(pa);
6402 			KASSERT(m != NULL, ("%s: vm_page_t is null for %#x",
6403 				__func__, pa));
6404 			pmap_dcache_wb_pou(pa, len, vm_page_pte2_attr(m));
6405 		}
6406 	}
6407 	/*
6408 	 * I-cache is VIPT. Only way how to flush all virtual mappings
6409 	 * on given physical address is to invalidate all i-cache.
6410 	 */
6411 	icache_inv_all();
6412 }
6413 
6414 /*
6415  *  The implementation of pmap_fault() uses IN_RANGE2() macro which
6416  *  depends on the fact that given range size is a power of 2.
6417  */
6418 CTASSERT(powerof2(NB_IN_PT1));
6419 CTASSERT(powerof2(PT2MAP_SIZE));
6420 
6421 #define IN_RANGE2(addr, start, size)	\
6422     ((vm_offset_t)(start) == ((vm_offset_t)(addr) & ~((size) - 1)))
6423 
6424 /*
6425  *  Handle access and R/W emulation faults.
6426  */
6427 int
pmap_fault(pmap_t pmap,vm_offset_t far,uint32_t fsr,int idx,bool usermode)6428 pmap_fault(pmap_t pmap, vm_offset_t far, uint32_t fsr, int idx, bool usermode)
6429 {
6430 	pt1_entry_t *pte1p, pte1;
6431 	pt2_entry_t *pte2p, pte2;
6432 
6433 	if (pmap == NULL)
6434 		pmap = kernel_pmap;
6435 
6436 	/*
6437 	 * In kernel, we should never get abort with FAR which is in range of
6438 	 * pmap->pm_pt1 or PT2MAP address spaces. If it happens, stop here
6439 	 * and print out a useful abort message and even get to the debugger
6440 	 * otherwise it likely ends with never ending loop of aborts.
6441 	 */
6442 	if (__predict_false(IN_RANGE2(far, pmap->pm_pt1, NB_IN_PT1))) {
6443 		/*
6444 		 * All L1 tables should always be mapped and present.
6445 		 * However, we check only current one herein. For user mode,
6446 		 * only permission abort from malicious user is not fatal.
6447 		 * And alignment abort as it may have higher priority.
6448 		 */
6449 		if (!usermode || (idx != FAULT_ALIGN && idx != FAULT_PERM_L2)) {
6450 			CTR4(KTR_PMAP, "%s: pmap %#x pm_pt1 %#x far %#x",
6451 			    __func__, pmap, pmap->pm_pt1, far);
6452 			panic("%s: pm_pt1 abort", __func__);
6453 		}
6454 		return (KERN_INVALID_ADDRESS);
6455 	}
6456 	if (__predict_false(IN_RANGE2(far, PT2MAP, PT2MAP_SIZE))) {
6457 		/*
6458 		 * PT2MAP should be always mapped and present in current
6459 		 * L1 table. However, only existing L2 tables are mapped
6460 		 * in PT2MAP. For user mode, only L2 translation abort and
6461 		 * permission abort from malicious user is not fatal.
6462 		 * And alignment abort as it may have higher priority.
6463 		 */
6464 		if (!usermode || (idx != FAULT_ALIGN &&
6465 		    idx != FAULT_TRAN_L2 && idx != FAULT_PERM_L2)) {
6466 			CTR4(KTR_PMAP, "%s: pmap %#x PT2MAP %#x far %#x",
6467 			    __func__, pmap, PT2MAP, far);
6468 			panic("%s: PT2MAP abort", __func__);
6469 		}
6470 		return (KERN_INVALID_ADDRESS);
6471 	}
6472 
6473 	/*
6474 	 * A pmap lock is used below for handling of access and R/W emulation
6475 	 * aborts. They were handled by atomic operations before so some
6476 	 * analysis of new situation is needed to answer the following question:
6477 	 * Is it safe to use the lock even for these aborts?
6478 	 *
6479 	 * There may happen two cases in general:
6480 	 *
6481 	 * (1) Aborts while the pmap lock is locked already - this should not
6482 	 * happen as pmap lock is not recursive. However, under pmap lock only
6483 	 * internal kernel data should be accessed and such data should be
6484 	 * mapped with A bit set and NM bit cleared. If double abort happens,
6485 	 * then a mapping of data which has caused it must be fixed. Further,
6486 	 * all new mappings are always made with A bit set and the bit can be
6487 	 * cleared only on managed mappings.
6488 	 *
6489 	 * (2) Aborts while another lock(s) is/are locked - this already can
6490 	 * happen. However, there is no difference here if it's either access or
6491 	 * R/W emulation abort, or if it's some other abort.
6492 	 */
6493 
6494 	PMAP_LOCK(pmap);
6495 #ifdef INVARIANTS
6496 	pte1 = pte1_load(pmap_pte1(pmap, far));
6497 	if (pte1_is_link(pte1)) {
6498 		/*
6499 		 * Check in advance that associated L2 page table is mapped into
6500 		 * PT2MAP space. Note that faulty access to not mapped L2 page
6501 		 * table is caught in more general check above where "far" is
6502 		 * checked that it does not lay in PT2MAP space. Note also that
6503 		 * L1 page table and PT2TAB always exist and are mapped.
6504 		 */
6505 		pte2 = pt2tab_load(pmap_pt2tab_entry(pmap, far));
6506 		if (!pte2_is_valid(pte2))
6507 			panic("%s: missing L2 page table (%p, %#x)",
6508 			    __func__, pmap, far);
6509 	}
6510 #endif
6511 #ifdef SMP
6512 	/*
6513 	 * Special treatment is due to break-before-make approach done when
6514 	 * pte1 is updated for userland mapping during section promotion or
6515 	 * demotion. If not caught here, pmap_enter() can find a section
6516 	 * mapping on faulting address. That is not allowed.
6517 	 */
6518 	if (idx == FAULT_TRAN_L1 && usermode && cp15_ats1cur_check(far) == 0) {
6519 		PMAP_UNLOCK(pmap);
6520 		return (KERN_SUCCESS);
6521 	}
6522 #endif
6523 	/*
6524 	 * Accesss bits for page and section. Note that the entry
6525 	 * is not in TLB yet, so TLB flush is not necessary.
6526 	 *
6527 	 * QQQ: This is hardware emulation, we do not call userret()
6528 	 *      for aborts from user mode.
6529 	 */
6530 	if (idx == FAULT_ACCESS_L2) {
6531 		pte1 = pte1_load(pmap_pte1(pmap, far));
6532 		if (pte1_is_link(pte1)) {
6533 			/* L2 page table should exist and be mapped. */
6534 			pte2p = pt2map_entry(far);
6535 			pte2 = pte2_load(pte2p);
6536 			if (pte2_is_valid(pte2)) {
6537 				pte2_store(pte2p, pte2 | PTE2_A);
6538 				PMAP_UNLOCK(pmap);
6539 				return (KERN_SUCCESS);
6540 			}
6541 		} else {
6542 			/*
6543 			 * We got L2 access fault but PTE1 is not a link.
6544 			 * Probably some race happened, do nothing.
6545 			 */
6546 			CTR3(KTR_PMAP, "%s: FAULT_ACCESS_L2 - pmap %#x far %#x",
6547 			    __func__, pmap, far);
6548 			PMAP_UNLOCK(pmap);
6549 			return (KERN_SUCCESS);
6550 		}
6551 	}
6552 	if (idx == FAULT_ACCESS_L1) {
6553 		pte1p = pmap_pte1(pmap, far);
6554 		pte1 = pte1_load(pte1p);
6555 		if (pte1_is_section(pte1)) {
6556 			pte1_store(pte1p, pte1 | PTE1_A);
6557 			PMAP_UNLOCK(pmap);
6558 			return (KERN_SUCCESS);
6559 		} else {
6560 			/*
6561 			 * We got L1 access fault but PTE1 is not section
6562 			 * mapping. Probably some race happened, do nothing.
6563 			 */
6564 			CTR3(KTR_PMAP, "%s: FAULT_ACCESS_L1 - pmap %#x far %#x",
6565 			    __func__, pmap, far);
6566 			PMAP_UNLOCK(pmap);
6567 			return (KERN_SUCCESS);
6568 		}
6569 	}
6570 
6571 	/*
6572 	 * Handle modify bits for page and section. Note that the modify
6573 	 * bit is emulated by software. So PTEx_RO is software read only
6574 	 * bit and PTEx_NM flag is real hardware read only bit.
6575 	 *
6576 	 * QQQ: This is hardware emulation, we do not call userret()
6577 	 *      for aborts from user mode.
6578 	 */
6579 	if ((fsr & FSR_WNR) && (idx == FAULT_PERM_L2)) {
6580 		pte1 = pte1_load(pmap_pte1(pmap, far));
6581 		if (pte1_is_link(pte1)) {
6582 			/* L2 page table should exist and be mapped. */
6583 			pte2p = pt2map_entry(far);
6584 			pte2 = pte2_load(pte2p);
6585 			if (pte2_is_valid(pte2) && !(pte2 & PTE2_RO) &&
6586 			    (pte2 & PTE2_NM)) {
6587 				pte2_store(pte2p, pte2 & ~PTE2_NM);
6588 				tlb_flush(trunc_page(far));
6589 				PMAP_UNLOCK(pmap);
6590 				return (KERN_SUCCESS);
6591 			}
6592 		} else {
6593 			/*
6594 			 * We got L2 permission fault but PTE1 is not a link.
6595 			 * Probably some race happened, do nothing.
6596 			 */
6597 			CTR3(KTR_PMAP, "%s: FAULT_PERM_L2 - pmap %#x far %#x",
6598 			    __func__, pmap, far);
6599 			PMAP_UNLOCK(pmap);
6600 			return (KERN_SUCCESS);
6601 		}
6602 	}
6603 	if ((fsr & FSR_WNR) && (idx == FAULT_PERM_L1)) {
6604 		pte1p = pmap_pte1(pmap, far);
6605 		pte1 = pte1_load(pte1p);
6606 		if (pte1_is_section(pte1)) {
6607 			if (!(pte1 & PTE1_RO) && (pte1 & PTE1_NM)) {
6608 				pte1_store(pte1p, pte1 & ~PTE1_NM);
6609 				tlb_flush(pte1_trunc(far));
6610 				PMAP_UNLOCK(pmap);
6611 				return (KERN_SUCCESS);
6612 			}
6613 		} else {
6614 			/*
6615 			 * We got L1 permission fault but PTE1 is not section
6616 			 * mapping. Probably some race happened, do nothing.
6617 			 */
6618 			CTR3(KTR_PMAP, "%s: FAULT_PERM_L1 - pmap %#x far %#x",
6619 			    __func__, pmap, far);
6620 			PMAP_UNLOCK(pmap);
6621 			return (KERN_SUCCESS);
6622 		}
6623 	}
6624 
6625 	/*
6626 	 * QQQ: The previous code, mainly fast handling of access and
6627 	 *      modify bits aborts, could be moved to ASM. Now we are
6628 	 *      starting to deal with not fast aborts.
6629 	 */
6630 	PMAP_UNLOCK(pmap);
6631 	return (KERN_FAILURE);
6632 }
6633 
6634 #if defined(PMAP_DEBUG)
6635 /*
6636  *  Reusing of KVA used in pmap_zero_page function !!!
6637  */
6638 static void
pmap_zero_page_check(vm_page_t m)6639 pmap_zero_page_check(vm_page_t m)
6640 {
6641 	pt2_entry_t *cmap2_pte2p;
6642 	uint32_t *p, *end;
6643 	struct pcpu *pc;
6644 
6645 	sched_pin();
6646 	pc = get_pcpu();
6647 	cmap2_pte2p = pc->pc_cmap2_pte2p;
6648 	mtx_lock(&pc->pc_cmap_lock);
6649 	if (pte2_load(cmap2_pte2p) != 0)
6650 		panic("%s: CMAP2 busy", __func__);
6651 	pte2_store(cmap2_pte2p, PTE2_KERN_NG(VM_PAGE_TO_PHYS(m), PTE2_AP_KRW,
6652 	    vm_page_pte2_attr(m)));
6653 	end = (uint32_t*)(pc->pc_cmap2_addr + PAGE_SIZE);
6654 	for (p = (uint32_t*)pc->pc_cmap2_addr; p < end; p++)
6655 		if (*p != 0)
6656 			panic("%s: page %p not zero, va: %p", __func__, m,
6657 			    pc->pc_cmap2_addr);
6658 	pte2_clear(cmap2_pte2p);
6659 	tlb_flush((vm_offset_t)pc->pc_cmap2_addr);
6660 	sched_unpin();
6661 	mtx_unlock(&pc->pc_cmap_lock);
6662 }
6663 
6664 int
pmap_pid_dump(int pid)6665 pmap_pid_dump(int pid)
6666 {
6667 	pmap_t pmap;
6668 	struct proc *p;
6669 	int npte2 = 0;
6670 	int i, j, index;
6671 
6672 	sx_slock(&allproc_lock);
6673 	FOREACH_PROC_IN_SYSTEM(p) {
6674 		if (p->p_pid != pid || p->p_vmspace == NULL)
6675 			continue;
6676 		index = 0;
6677 		pmap = vmspace_pmap(p->p_vmspace);
6678 		for (i = 0; i < NPTE1_IN_PT1; i++) {
6679 			pt1_entry_t pte1;
6680 			pt2_entry_t *pte2p, pte2;
6681 			vm_offset_t base, va;
6682 			vm_paddr_t pa;
6683 			vm_page_t m;
6684 
6685 			base = i << PTE1_SHIFT;
6686 			pte1 = pte1_load(&pmap->pm_pt1[i]);
6687 
6688 			if (pte1_is_section(pte1)) {
6689 				/*
6690 				 * QQQ: Do something here!
6691 				 */
6692 			} else if (pte1_is_link(pte1)) {
6693 				for (j = 0; j < NPTE2_IN_PT2; j++) {
6694 					va = base + (j << PAGE_SHIFT);
6695 					if (va >= VM_MIN_KERNEL_ADDRESS) {
6696 						if (index) {
6697 							index = 0;
6698 							printf("\n");
6699 						}
6700 						sx_sunlock(&allproc_lock);
6701 						return (npte2);
6702 					}
6703 					pte2p = pmap_pte2(pmap, va);
6704 					pte2 = pte2_load(pte2p);
6705 					pmap_pte2_release(pte2p);
6706 					if (!pte2_is_valid(pte2))
6707 						continue;
6708 
6709 					pa = pte2_pa(pte2);
6710 					m = PHYS_TO_VM_PAGE(pa);
6711 					printf("va: 0x%x, pa: 0x%x, h: %d, w:"
6712 					    " %d, f: 0x%x", va, pa,
6713 					    m->hold_count, m->wire_count,
6714 					    m->flags);
6715 					npte2++;
6716 					index++;
6717 					if (index >= 2) {
6718 						index = 0;
6719 						printf("\n");
6720 					} else {
6721 						printf(" ");
6722 					}
6723 				}
6724 			}
6725 		}
6726 	}
6727 	sx_sunlock(&allproc_lock);
6728 	return (npte2);
6729 }
6730 
6731 #endif
6732 
6733 #ifdef DDB
6734 static pt2_entry_t *
pmap_pte2_ddb(pmap_t pmap,vm_offset_t va)6735 pmap_pte2_ddb(pmap_t pmap, vm_offset_t va)
6736 {
6737 	pt1_entry_t pte1;
6738 	vm_paddr_t pt2pg_pa;
6739 
6740 	pte1 = pte1_load(pmap_pte1(pmap, va));
6741 	if (!pte1_is_link(pte1))
6742 		return (NULL);
6743 
6744 	if (pmap_is_current(pmap))
6745 		return (pt2map_entry(va));
6746 
6747 	/* Note that L2 page table size is not equal to PAGE_SIZE. */
6748 	pt2pg_pa = trunc_page(pte1_link_pa(pte1));
6749 	if (pte2_pa(pte2_load(PMAP3)) != pt2pg_pa) {
6750 		pte2_store(PMAP3, PTE2_KPT(pt2pg_pa));
6751 #ifdef SMP
6752 		PMAP3cpu = PCPU_GET(cpuid);
6753 #endif
6754 		tlb_flush_local((vm_offset_t)PADDR3);
6755 	}
6756 #ifdef SMP
6757 	else if (PMAP3cpu != PCPU_GET(cpuid)) {
6758 		PMAP3cpu = PCPU_GET(cpuid);
6759 		tlb_flush_local((vm_offset_t)PADDR3);
6760 	}
6761 #endif
6762 	return (PADDR3 + (arm32_btop(va) & (NPTE2_IN_PG - 1)));
6763 }
6764 
6765 static void
dump_pmap(pmap_t pmap)6766 dump_pmap(pmap_t pmap)
6767 {
6768 
6769 	printf("pmap %p\n", pmap);
6770 	printf("  pm_pt1: %p\n", pmap->pm_pt1);
6771 	printf("  pm_pt2tab: %p\n", pmap->pm_pt2tab);
6772 	printf("  pm_active: 0x%08lX\n", pmap->pm_active.__bits[0]);
6773 }
6774 
DB_SHOW_COMMAND(pmaps,pmap_list_pmaps)6775 DB_SHOW_COMMAND(pmaps, pmap_list_pmaps)
6776 {
6777 
6778 	pmap_t pmap;
6779 	LIST_FOREACH(pmap, &allpmaps, pm_list) {
6780 		dump_pmap(pmap);
6781 	}
6782 }
6783 
6784 static int
pte2_class(pt2_entry_t pte2)6785 pte2_class(pt2_entry_t pte2)
6786 {
6787 	int cls;
6788 
6789 	cls = (pte2 >> 2) & 0x03;
6790 	cls |= (pte2 >> 4) & 0x04;
6791 	return (cls);
6792 }
6793 
6794 static void
dump_section(pmap_t pmap,uint32_t pte1_idx)6795 dump_section(pmap_t pmap, uint32_t pte1_idx)
6796 {
6797 }
6798 
6799 static void
dump_link(pmap_t pmap,uint32_t pte1_idx,boolean_t invalid_ok)6800 dump_link(pmap_t pmap, uint32_t pte1_idx, boolean_t invalid_ok)
6801 {
6802 	uint32_t i;
6803 	vm_offset_t va;
6804 	pt2_entry_t *pte2p, pte2;
6805 	vm_page_t m;
6806 
6807 	va = pte1_idx << PTE1_SHIFT;
6808 	pte2p = pmap_pte2_ddb(pmap, va);
6809 	for (i = 0; i < NPTE2_IN_PT2; i++, pte2p++, va += PAGE_SIZE) {
6810 		pte2 = pte2_load(pte2p);
6811 		if (pte2 == 0)
6812 			continue;
6813 		if (!pte2_is_valid(pte2)) {
6814 			printf(" 0x%08X: 0x%08X", va, pte2);
6815 			if (!invalid_ok)
6816 				printf(" - not valid !!!");
6817 			printf("\n");
6818 			continue;
6819 		}
6820 		m = PHYS_TO_VM_PAGE(pte2_pa(pte2));
6821 		printf(" 0x%08X: 0x%08X, TEX%d, s:%d, g:%d, m:%p", va , pte2,
6822 		    pte2_class(pte2), !!(pte2 & PTE2_S), !(pte2 & PTE2_NG), m);
6823 		if (m != NULL) {
6824 			printf(" v:%d h:%d w:%d f:0x%04X\n", m->valid,
6825 			    m->hold_count, m->wire_count, m->flags);
6826 		} else {
6827 			printf("\n");
6828 		}
6829 	}
6830 }
6831 
6832 static __inline boolean_t
is_pv_chunk_space(vm_offset_t va)6833 is_pv_chunk_space(vm_offset_t va)
6834 {
6835 
6836 	if ((((vm_offset_t)pv_chunkbase) <= va) &&
6837 	    (va < ((vm_offset_t)pv_chunkbase + PAGE_SIZE * pv_maxchunks)))
6838 		return (TRUE);
6839 	return (FALSE);
6840 }
6841 
DB_SHOW_COMMAND(pmap,pmap_pmap_print)6842 DB_SHOW_COMMAND(pmap, pmap_pmap_print)
6843 {
6844 	/* XXX convert args. */
6845 	pmap_t pmap = (pmap_t)addr;
6846 	pt1_entry_t pte1;
6847 	pt2_entry_t pte2;
6848 	vm_offset_t va, eva;
6849 	vm_page_t m;
6850 	uint32_t i;
6851 	boolean_t invalid_ok, dump_link_ok, dump_pv_chunk;
6852 
6853 	if (have_addr) {
6854 		pmap_t pm;
6855 
6856 		LIST_FOREACH(pm, &allpmaps, pm_list)
6857 			if (pm == pmap) break;
6858 		if (pm == NULL) {
6859 			printf("given pmap %p is not in allpmaps list\n", pmap);
6860 			return;
6861 		}
6862 	} else
6863 		pmap = PCPU_GET(curpmap);
6864 
6865 	eva = (modif[0] == 'u') ? VM_MAXUSER_ADDRESS : 0xFFFFFFFF;
6866 	dump_pv_chunk = FALSE; /* XXX evaluate from modif[] */
6867 
6868 	printf("pmap: 0x%08X\n", (uint32_t)pmap);
6869 	printf("PT2MAP: 0x%08X\n", (uint32_t)PT2MAP);
6870 	printf("pt2tab: 0x%08X\n", (uint32_t)pmap->pm_pt2tab);
6871 
6872 	for(i = 0; i < NPTE1_IN_PT1; i++) {
6873 		pte1 = pte1_load(&pmap->pm_pt1[i]);
6874 		if (pte1 == 0)
6875 			continue;
6876 		va = i << PTE1_SHIFT;
6877 		if (va >= eva)
6878 			break;
6879 
6880 		if (pte1_is_section(pte1)) {
6881 			printf("0x%08X: Section 0x%08X, s:%d g:%d\n", va, pte1,
6882 			    !!(pte1 & PTE1_S), !(pte1 & PTE1_NG));
6883 			dump_section(pmap, i);
6884 		} else if (pte1_is_link(pte1)) {
6885 			dump_link_ok = TRUE;
6886 			invalid_ok = FALSE;
6887 			pte2 = pte2_load(pmap_pt2tab_entry(pmap, va));
6888 			m = PHYS_TO_VM_PAGE(pte1_link_pa(pte1));
6889 			printf("0x%08X: Link 0x%08X, pt2tab: 0x%08X m: %p",
6890 			    va, pte1, pte2, m);
6891 			if (is_pv_chunk_space(va)) {
6892 				printf(" - pv_chunk space");
6893 				if (dump_pv_chunk)
6894 					invalid_ok = TRUE;
6895 				else
6896 					dump_link_ok = FALSE;
6897 			}
6898 			else if (m != NULL)
6899 				printf(" w:%d w2:%u", m->wire_count,
6900 				    pt2_wirecount_get(m, pte1_index(va)));
6901 			if (pte2 == 0)
6902 				printf(" !!! pt2tab entry is ZERO");
6903 			else if (pte2_pa(pte1) != pte2_pa(pte2))
6904 				printf(" !!! pt2tab entry is DIFFERENT - m: %p",
6905 				    PHYS_TO_VM_PAGE(pte2_pa(pte2)));
6906 			printf("\n");
6907 			if (dump_link_ok)
6908 				dump_link(pmap, i, invalid_ok);
6909 		} else
6910 			printf("0x%08X: Invalid entry 0x%08X\n", va, pte1);
6911 	}
6912 }
6913 
6914 static void
dump_pt2tab(pmap_t pmap)6915 dump_pt2tab(pmap_t pmap)
6916 {
6917 	uint32_t i;
6918 	pt2_entry_t pte2;
6919 	vm_offset_t va;
6920 	vm_paddr_t pa;
6921 	vm_page_t m;
6922 
6923 	printf("PT2TAB:\n");
6924 	for (i = 0; i < PT2TAB_ENTRIES; i++) {
6925 		pte2 = pte2_load(&pmap->pm_pt2tab[i]);
6926 		if (!pte2_is_valid(pte2))
6927 			continue;
6928 		va = i << PT2TAB_SHIFT;
6929 		pa = pte2_pa(pte2);
6930 		m = PHYS_TO_VM_PAGE(pa);
6931 		printf(" 0x%08X: 0x%08X, TEX%d, s:%d, m:%p", va, pte2,
6932 		    pte2_class(pte2), !!(pte2 & PTE2_S), m);
6933 		if (m != NULL)
6934 			printf(" , h: %d, w: %d, f: 0x%04X pidx: %lld",
6935 			    m->hold_count, m->wire_count, m->flags, m->pindex);
6936 		printf("\n");
6937 	}
6938 }
6939 
DB_SHOW_COMMAND(pmap_pt2tab,pmap_pt2tab_print)6940 DB_SHOW_COMMAND(pmap_pt2tab, pmap_pt2tab_print)
6941 {
6942 	/* XXX convert args. */
6943 	pmap_t pmap = (pmap_t)addr;
6944 	pt1_entry_t pte1;
6945 	pt2_entry_t pte2;
6946 	vm_offset_t va;
6947 	uint32_t i, start;
6948 
6949 	if (have_addr) {
6950 		printf("supported only on current pmap\n");
6951 		return;
6952 	}
6953 
6954 	pmap = PCPU_GET(curpmap);
6955 	printf("curpmap: 0x%08X\n", (uint32_t)pmap);
6956 	printf("PT2MAP: 0x%08X\n", (uint32_t)PT2MAP);
6957 	printf("pt2tab: 0x%08X\n", (uint32_t)pmap->pm_pt2tab);
6958 
6959 	start = pte1_index((vm_offset_t)PT2MAP);
6960 	for (i = start; i < (start + NPT2_IN_PT2TAB); i++) {
6961 		pte1 = pte1_load(&pmap->pm_pt1[i]);
6962 		if (pte1 == 0)
6963 			continue;
6964 		va = i << PTE1_SHIFT;
6965 		if (pte1_is_section(pte1)) {
6966 			printf("0x%08X: Section 0x%08X, s:%d\n", va, pte1,
6967 			    !!(pte1 & PTE1_S));
6968 			dump_section(pmap, i);
6969 		} else if (pte1_is_link(pte1)) {
6970 			pte2 = pte2_load(pmap_pt2tab_entry(pmap, va));
6971 			printf("0x%08X: Link 0x%08X, pt2tab: 0x%08X\n", va,
6972 			    pte1, pte2);
6973 			if (pte2 == 0)
6974 				printf("  !!! pt2tab entry is ZERO\n");
6975 		} else
6976 			printf("0x%08X: Invalid entry 0x%08X\n", va, pte1);
6977 	}
6978 	dump_pt2tab(pmap);
6979 }
6980 #endif
6981