xref: /linux/arch/arm64/mm/mmu.c (revision 24172e0d79900908cf5ebf366600616d29c9b417)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Based on arch/arm/mm/mmu.c
4  *
5  * Copyright (C) 1995-2005 Russell King
6  * Copyright (C) 2012 ARM Ltd.
7  */
8 
9 #include <linux/cache.h>
10 #include <linux/export.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/init.h>
14 #include <linux/ioport.h>
15 #include <linux/kexec.h>
16 #include <linux/libfdt.h>
17 #include <linux/mman.h>
18 #include <linux/nodemask.h>
19 #include <linux/memblock.h>
20 #include <linux/memremap.h>
21 #include <linux/memory.h>
22 #include <linux/fs.h>
23 #include <linux/io.h>
24 #include <linux/mm.h>
25 #include <linux/vmalloc.h>
26 #include <linux/set_memory.h>
27 #include <linux/kfence.h>
28 #include <linux/pkeys.h>
29 #include <linux/mm_inline.h>
30 #include <linux/pagewalk.h>
31 #include <linux/stop_machine.h>
32 
33 #include <asm/barrier.h>
34 #include <asm/cputype.h>
35 #include <asm/fixmap.h>
36 #include <asm/kasan.h>
37 #include <asm/kernel-pgtable.h>
38 #include <asm/sections.h>
39 #include <asm/setup.h>
40 #include <linux/sizes.h>
41 #include <asm/tlb.h>
42 #include <asm/mmu_context.h>
43 #include <asm/ptdump.h>
44 #include <asm/tlbflush.h>
45 #include <asm/pgalloc.h>
46 #include <asm/kfence.h>
47 
48 #define NO_BLOCK_MAPPINGS	BIT(0)
49 #define NO_CONT_MAPPINGS	BIT(1)
50 #define NO_EXEC_MAPPINGS	BIT(2)	/* assumes FEAT_HPDS is not used */
51 
52 DEFINE_STATIC_KEY_FALSE(arm64_ptdump_lock_key);
53 
54 u64 kimage_voffset __ro_after_init;
55 EXPORT_SYMBOL(kimage_voffset);
56 
57 u32 __boot_cpu_mode[] = { BOOT_CPU_MODE_EL2, BOOT_CPU_MODE_EL1 };
58 
59 static bool rodata_is_rw __ro_after_init = true;
60 
61 /*
62  * The booting CPU updates the failed status @__early_cpu_boot_status,
63  * with MMU turned off.
64  */
65 long __section(".mmuoff.data.write") __early_cpu_boot_status;
66 
67 /*
68  * Empty_zero_page is a special page that is used for zero-initialized data
69  * and COW.
70  */
71 unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
72 EXPORT_SYMBOL(empty_zero_page);
73 
74 static DEFINE_SPINLOCK(swapper_pgdir_lock);
75 static DEFINE_MUTEX(fixmap_lock);
76 
set_swapper_pgd(pgd_t * pgdp,pgd_t pgd)77 void noinstr set_swapper_pgd(pgd_t *pgdp, pgd_t pgd)
78 {
79 	pgd_t *fixmap_pgdp;
80 
81 	/*
82 	 * Don't bother with the fixmap if swapper_pg_dir is still mapped
83 	 * writable in the kernel mapping.
84 	 */
85 	if (rodata_is_rw) {
86 		WRITE_ONCE(*pgdp, pgd);
87 		dsb(ishst);
88 		isb();
89 		return;
90 	}
91 
92 	spin_lock(&swapper_pgdir_lock);
93 	fixmap_pgdp = pgd_set_fixmap(__pa_symbol(pgdp));
94 	WRITE_ONCE(*fixmap_pgdp, pgd);
95 	/*
96 	 * We need dsb(ishst) here to ensure the page-table-walker sees
97 	 * our new entry before set_p?d() returns. The fixmap's
98 	 * flush_tlb_kernel_range() via clear_fixmap() does this for us.
99 	 */
100 	pgd_clear_fixmap();
101 	spin_unlock(&swapper_pgdir_lock);
102 }
103 
phys_mem_access_prot(struct file * file,unsigned long pfn,unsigned long size,pgprot_t vma_prot)104 pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
105 			      unsigned long size, pgprot_t vma_prot)
106 {
107 	if (!pfn_is_map_memory(pfn))
108 		return pgprot_noncached(vma_prot);
109 	else if (file->f_flags & O_SYNC)
110 		return pgprot_writecombine(vma_prot);
111 	return vma_prot;
112 }
113 EXPORT_SYMBOL(phys_mem_access_prot);
114 
early_pgtable_alloc(enum pgtable_type pgtable_type)115 static phys_addr_t __init early_pgtable_alloc(enum pgtable_type pgtable_type)
116 {
117 	phys_addr_t phys;
118 
119 	phys = memblock_phys_alloc_range(PAGE_SIZE, PAGE_SIZE, 0,
120 					 MEMBLOCK_ALLOC_NOLEAKTRACE);
121 	if (!phys)
122 		panic("Failed to allocate page table page\n");
123 
124 	return phys;
125 }
126 
pgattr_change_is_safe(pteval_t old,pteval_t new)127 bool pgattr_change_is_safe(pteval_t old, pteval_t new)
128 {
129 	/*
130 	 * The following mapping attributes may be updated in live
131 	 * kernel mappings without the need for break-before-make.
132 	 */
133 	pteval_t mask = PTE_PXN | PTE_RDONLY | PTE_WRITE | PTE_NG |
134 			PTE_SWBITS_MASK;
135 
136 	/* creating or taking down mappings is always safe */
137 	if (!pte_valid(__pte(old)) || !pte_valid(__pte(new)))
138 		return true;
139 
140 	/* A live entry's pfn should not change */
141 	if (pte_pfn(__pte(old)) != pte_pfn(__pte(new)))
142 		return false;
143 
144 	/* live contiguous mappings may not be manipulated at all */
145 	if ((old | new) & PTE_CONT)
146 		return false;
147 
148 	/* Transitioning from Non-Global to Global is unsafe */
149 	if (old & ~new & PTE_NG)
150 		return false;
151 
152 	/*
153 	 * Changing the memory type between Normal and Normal-Tagged is safe
154 	 * since Tagged is considered a permission attribute from the
155 	 * mismatched attribute aliases perspective.
156 	 */
157 	if (((old & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL) ||
158 	     (old & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL_TAGGED)) &&
159 	    ((new & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL) ||
160 	     (new & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL_TAGGED)))
161 		mask |= PTE_ATTRINDX_MASK;
162 
163 	return ((old ^ new) & ~mask) == 0;
164 }
165 
init_clear_pgtable(void * table)166 static void init_clear_pgtable(void *table)
167 {
168 	clear_page(table);
169 
170 	/* Ensure the zeroing is observed by page table walks. */
171 	dsb(ishst);
172 }
173 
init_pte(pte_t * ptep,unsigned long addr,unsigned long end,phys_addr_t phys,pgprot_t prot)174 static void init_pte(pte_t *ptep, unsigned long addr, unsigned long end,
175 		     phys_addr_t phys, pgprot_t prot)
176 {
177 	do {
178 		pte_t old_pte = __ptep_get(ptep);
179 
180 		/*
181 		 * Required barriers to make this visible to the table walker
182 		 * are deferred to the end of alloc_init_cont_pte().
183 		 */
184 		__set_pte_nosync(ptep, pfn_pte(__phys_to_pfn(phys), prot));
185 
186 		/*
187 		 * After the PTE entry has been populated once, we
188 		 * only allow updates to the permission attributes.
189 		 */
190 		BUG_ON(!pgattr_change_is_safe(pte_val(old_pte),
191 					      pte_val(__ptep_get(ptep))));
192 
193 		phys += PAGE_SIZE;
194 	} while (ptep++, addr += PAGE_SIZE, addr != end);
195 }
196 
alloc_init_cont_pte(pmd_t * pmdp,unsigned long addr,unsigned long end,phys_addr_t phys,pgprot_t prot,phys_addr_t (* pgtable_alloc)(enum pgtable_type),int flags)197 static void alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr,
198 				unsigned long end, phys_addr_t phys,
199 				pgprot_t prot,
200 				phys_addr_t (*pgtable_alloc)(enum pgtable_type),
201 				int flags)
202 {
203 	unsigned long next;
204 	pmd_t pmd = READ_ONCE(*pmdp);
205 	pte_t *ptep;
206 
207 	BUG_ON(pmd_sect(pmd));
208 	if (pmd_none(pmd)) {
209 		pmdval_t pmdval = PMD_TYPE_TABLE | PMD_TABLE_UXN | PMD_TABLE_AF;
210 		phys_addr_t pte_phys;
211 
212 		if (flags & NO_EXEC_MAPPINGS)
213 			pmdval |= PMD_TABLE_PXN;
214 		BUG_ON(!pgtable_alloc);
215 		pte_phys = pgtable_alloc(TABLE_PTE);
216 		ptep = pte_set_fixmap(pte_phys);
217 		init_clear_pgtable(ptep);
218 		ptep += pte_index(addr);
219 		__pmd_populate(pmdp, pte_phys, pmdval);
220 	} else {
221 		BUG_ON(pmd_bad(pmd));
222 		ptep = pte_set_fixmap_offset(pmdp, addr);
223 	}
224 
225 	do {
226 		pgprot_t __prot = prot;
227 
228 		next = pte_cont_addr_end(addr, end);
229 
230 		/* use a contiguous mapping if the range is suitably aligned */
231 		if ((((addr | next | phys) & ~CONT_PTE_MASK) == 0) &&
232 		    (flags & NO_CONT_MAPPINGS) == 0)
233 			__prot = __pgprot(pgprot_val(prot) | PTE_CONT);
234 
235 		init_pte(ptep, addr, next, phys, __prot);
236 
237 		ptep += pte_index(next) - pte_index(addr);
238 		phys += next - addr;
239 	} while (addr = next, addr != end);
240 
241 	/*
242 	 * Note: barriers and maintenance necessary to clear the fixmap slot
243 	 * ensure that all previous pgtable writes are visible to the table
244 	 * walker.
245 	 */
246 	pte_clear_fixmap();
247 }
248 
init_pmd(pmd_t * pmdp,unsigned long addr,unsigned long end,phys_addr_t phys,pgprot_t prot,phys_addr_t (* pgtable_alloc)(enum pgtable_type),int flags)249 static void init_pmd(pmd_t *pmdp, unsigned long addr, unsigned long end,
250 		     phys_addr_t phys, pgprot_t prot,
251 		     phys_addr_t (*pgtable_alloc)(enum pgtable_type), int flags)
252 {
253 	unsigned long next;
254 
255 	do {
256 		pmd_t old_pmd = READ_ONCE(*pmdp);
257 
258 		next = pmd_addr_end(addr, end);
259 
260 		/* try section mapping first */
261 		if (((addr | next | phys) & ~PMD_MASK) == 0 &&
262 		    (flags & NO_BLOCK_MAPPINGS) == 0) {
263 			pmd_set_huge(pmdp, phys, prot);
264 
265 			/*
266 			 * After the PMD entry has been populated once, we
267 			 * only allow updates to the permission attributes.
268 			 */
269 			BUG_ON(!pgattr_change_is_safe(pmd_val(old_pmd),
270 						      READ_ONCE(pmd_val(*pmdp))));
271 		} else {
272 			alloc_init_cont_pte(pmdp, addr, next, phys, prot,
273 					    pgtable_alloc, flags);
274 
275 			BUG_ON(pmd_val(old_pmd) != 0 &&
276 			       pmd_val(old_pmd) != READ_ONCE(pmd_val(*pmdp)));
277 		}
278 		phys += next - addr;
279 	} while (pmdp++, addr = next, addr != end);
280 }
281 
alloc_init_cont_pmd(pud_t * pudp,unsigned long addr,unsigned long end,phys_addr_t phys,pgprot_t prot,phys_addr_t (* pgtable_alloc)(enum pgtable_type),int flags)282 static void alloc_init_cont_pmd(pud_t *pudp, unsigned long addr,
283 				unsigned long end, phys_addr_t phys,
284 				pgprot_t prot,
285 				phys_addr_t (*pgtable_alloc)(enum pgtable_type),
286 				int flags)
287 {
288 	unsigned long next;
289 	pud_t pud = READ_ONCE(*pudp);
290 	pmd_t *pmdp;
291 
292 	/*
293 	 * Check for initial section mappings in the pgd/pud.
294 	 */
295 	BUG_ON(pud_sect(pud));
296 	if (pud_none(pud)) {
297 		pudval_t pudval = PUD_TYPE_TABLE | PUD_TABLE_UXN | PUD_TABLE_AF;
298 		phys_addr_t pmd_phys;
299 
300 		if (flags & NO_EXEC_MAPPINGS)
301 			pudval |= PUD_TABLE_PXN;
302 		BUG_ON(!pgtable_alloc);
303 		pmd_phys = pgtable_alloc(TABLE_PMD);
304 		pmdp = pmd_set_fixmap(pmd_phys);
305 		init_clear_pgtable(pmdp);
306 		pmdp += pmd_index(addr);
307 		__pud_populate(pudp, pmd_phys, pudval);
308 	} else {
309 		BUG_ON(pud_bad(pud));
310 		pmdp = pmd_set_fixmap_offset(pudp, addr);
311 	}
312 
313 	do {
314 		pgprot_t __prot = prot;
315 
316 		next = pmd_cont_addr_end(addr, end);
317 
318 		/* use a contiguous mapping if the range is suitably aligned */
319 		if ((((addr | next | phys) & ~CONT_PMD_MASK) == 0) &&
320 		    (flags & NO_CONT_MAPPINGS) == 0)
321 			__prot = __pgprot(pgprot_val(prot) | PTE_CONT);
322 
323 		init_pmd(pmdp, addr, next, phys, __prot, pgtable_alloc, flags);
324 
325 		pmdp += pmd_index(next) - pmd_index(addr);
326 		phys += next - addr;
327 	} while (addr = next, addr != end);
328 
329 	pmd_clear_fixmap();
330 }
331 
alloc_init_pud(p4d_t * p4dp,unsigned long addr,unsigned long end,phys_addr_t phys,pgprot_t prot,phys_addr_t (* pgtable_alloc)(enum pgtable_type),int flags)332 static void alloc_init_pud(p4d_t *p4dp, unsigned long addr, unsigned long end,
333 			   phys_addr_t phys, pgprot_t prot,
334 			   phys_addr_t (*pgtable_alloc)(enum pgtable_type),
335 			   int flags)
336 {
337 	unsigned long next;
338 	p4d_t p4d = READ_ONCE(*p4dp);
339 	pud_t *pudp;
340 
341 	if (p4d_none(p4d)) {
342 		p4dval_t p4dval = P4D_TYPE_TABLE | P4D_TABLE_UXN | P4D_TABLE_AF;
343 		phys_addr_t pud_phys;
344 
345 		if (flags & NO_EXEC_MAPPINGS)
346 			p4dval |= P4D_TABLE_PXN;
347 		BUG_ON(!pgtable_alloc);
348 		pud_phys = pgtable_alloc(TABLE_PUD);
349 		pudp = pud_set_fixmap(pud_phys);
350 		init_clear_pgtable(pudp);
351 		pudp += pud_index(addr);
352 		__p4d_populate(p4dp, pud_phys, p4dval);
353 	} else {
354 		BUG_ON(p4d_bad(p4d));
355 		pudp = pud_set_fixmap_offset(p4dp, addr);
356 	}
357 
358 	do {
359 		pud_t old_pud = READ_ONCE(*pudp);
360 
361 		next = pud_addr_end(addr, end);
362 
363 		/*
364 		 * For 4K granule only, attempt to put down a 1GB block
365 		 */
366 		if (pud_sect_supported() &&
367 		   ((addr | next | phys) & ~PUD_MASK) == 0 &&
368 		    (flags & NO_BLOCK_MAPPINGS) == 0) {
369 			pud_set_huge(pudp, phys, prot);
370 
371 			/*
372 			 * After the PUD entry has been populated once, we
373 			 * only allow updates to the permission attributes.
374 			 */
375 			BUG_ON(!pgattr_change_is_safe(pud_val(old_pud),
376 						      READ_ONCE(pud_val(*pudp))));
377 		} else {
378 			alloc_init_cont_pmd(pudp, addr, next, phys, prot,
379 					    pgtable_alloc, flags);
380 
381 			BUG_ON(pud_val(old_pud) != 0 &&
382 			       pud_val(old_pud) != READ_ONCE(pud_val(*pudp)));
383 		}
384 		phys += next - addr;
385 	} while (pudp++, addr = next, addr != end);
386 
387 	pud_clear_fixmap();
388 }
389 
alloc_init_p4d(pgd_t * pgdp,unsigned long addr,unsigned long end,phys_addr_t phys,pgprot_t prot,phys_addr_t (* pgtable_alloc)(enum pgtable_type),int flags)390 static void alloc_init_p4d(pgd_t *pgdp, unsigned long addr, unsigned long end,
391 			   phys_addr_t phys, pgprot_t prot,
392 			   phys_addr_t (*pgtable_alloc)(enum pgtable_type),
393 			   int flags)
394 {
395 	unsigned long next;
396 	pgd_t pgd = READ_ONCE(*pgdp);
397 	p4d_t *p4dp;
398 
399 	if (pgd_none(pgd)) {
400 		pgdval_t pgdval = PGD_TYPE_TABLE | PGD_TABLE_UXN | PGD_TABLE_AF;
401 		phys_addr_t p4d_phys;
402 
403 		if (flags & NO_EXEC_MAPPINGS)
404 			pgdval |= PGD_TABLE_PXN;
405 		BUG_ON(!pgtable_alloc);
406 		p4d_phys = pgtable_alloc(TABLE_P4D);
407 		p4dp = p4d_set_fixmap(p4d_phys);
408 		init_clear_pgtable(p4dp);
409 		p4dp += p4d_index(addr);
410 		__pgd_populate(pgdp, p4d_phys, pgdval);
411 	} else {
412 		BUG_ON(pgd_bad(pgd));
413 		p4dp = p4d_set_fixmap_offset(pgdp, addr);
414 	}
415 
416 	do {
417 		p4d_t old_p4d = READ_ONCE(*p4dp);
418 
419 		next = p4d_addr_end(addr, end);
420 
421 		alloc_init_pud(p4dp, addr, next, phys, prot,
422 			       pgtable_alloc, flags);
423 
424 		BUG_ON(p4d_val(old_p4d) != 0 &&
425 		       p4d_val(old_p4d) != READ_ONCE(p4d_val(*p4dp)));
426 
427 		phys += next - addr;
428 	} while (p4dp++, addr = next, addr != end);
429 
430 	p4d_clear_fixmap();
431 }
432 
__create_pgd_mapping_locked(pgd_t * pgdir,phys_addr_t phys,unsigned long virt,phys_addr_t size,pgprot_t prot,phys_addr_t (* pgtable_alloc)(enum pgtable_type),int flags)433 static void __create_pgd_mapping_locked(pgd_t *pgdir, phys_addr_t phys,
434 					unsigned long virt, phys_addr_t size,
435 					pgprot_t prot,
436 					phys_addr_t (*pgtable_alloc)(enum pgtable_type),
437 					int flags)
438 {
439 	unsigned long addr, end, next;
440 	pgd_t *pgdp = pgd_offset_pgd(pgdir, virt);
441 
442 	/*
443 	 * If the virtual and physical address don't have the same offset
444 	 * within a page, we cannot map the region as the caller expects.
445 	 */
446 	if (WARN_ON((phys ^ virt) & ~PAGE_MASK))
447 		return;
448 
449 	phys &= PAGE_MASK;
450 	addr = virt & PAGE_MASK;
451 	end = PAGE_ALIGN(virt + size);
452 
453 	do {
454 		next = pgd_addr_end(addr, end);
455 		alloc_init_p4d(pgdp, addr, next, phys, prot, pgtable_alloc,
456 			       flags);
457 		phys += next - addr;
458 	} while (pgdp++, addr = next, addr != end);
459 }
460 
__create_pgd_mapping(pgd_t * pgdir,phys_addr_t phys,unsigned long virt,phys_addr_t size,pgprot_t prot,phys_addr_t (* pgtable_alloc)(enum pgtable_type),int flags)461 static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys,
462 				 unsigned long virt, phys_addr_t size,
463 				 pgprot_t prot,
464 				 phys_addr_t (*pgtable_alloc)(enum pgtable_type),
465 				 int flags)
466 {
467 	mutex_lock(&fixmap_lock);
468 	__create_pgd_mapping_locked(pgdir, phys, virt, size, prot,
469 				    pgtable_alloc, flags);
470 	mutex_unlock(&fixmap_lock);
471 }
472 
473 #define INVALID_PHYS_ADDR	(-1ULL)
474 
__pgd_pgtable_alloc(struct mm_struct * mm,gfp_t gfp,enum pgtable_type pgtable_type)475 static phys_addr_t __pgd_pgtable_alloc(struct mm_struct *mm, gfp_t gfp,
476 				       enum pgtable_type pgtable_type)
477 {
478 	/* Page is zeroed by init_clear_pgtable() so don't duplicate effort. */
479 	struct ptdesc *ptdesc = pagetable_alloc(gfp & ~__GFP_ZERO, 0);
480 	phys_addr_t pa;
481 
482 	if (!ptdesc)
483 		return INVALID_PHYS_ADDR;
484 
485 	pa = page_to_phys(ptdesc_page(ptdesc));
486 
487 	switch (pgtable_type) {
488 	case TABLE_PTE:
489 		BUG_ON(!pagetable_pte_ctor(mm, ptdesc));
490 		break;
491 	case TABLE_PMD:
492 		BUG_ON(!pagetable_pmd_ctor(mm, ptdesc));
493 		break;
494 	case TABLE_PUD:
495 		pagetable_pud_ctor(ptdesc);
496 		break;
497 	case TABLE_P4D:
498 		pagetable_p4d_ctor(ptdesc);
499 		break;
500 	}
501 
502 	return pa;
503 }
504 
505 static phys_addr_t
try_pgd_pgtable_alloc_init_mm(enum pgtable_type pgtable_type,gfp_t gfp)506 try_pgd_pgtable_alloc_init_mm(enum pgtable_type pgtable_type, gfp_t gfp)
507 {
508 	return __pgd_pgtable_alloc(&init_mm, gfp, pgtable_type);
509 }
510 
511 static phys_addr_t __maybe_unused
pgd_pgtable_alloc_init_mm(enum pgtable_type pgtable_type)512 pgd_pgtable_alloc_init_mm(enum pgtable_type pgtable_type)
513 {
514 	phys_addr_t pa;
515 
516 	pa = __pgd_pgtable_alloc(&init_mm, GFP_PGTABLE_KERNEL, pgtable_type);
517 	BUG_ON(pa == INVALID_PHYS_ADDR);
518 	return pa;
519 }
520 
521 static phys_addr_t
pgd_pgtable_alloc_special_mm(enum pgtable_type pgtable_type)522 pgd_pgtable_alloc_special_mm(enum pgtable_type pgtable_type)
523 {
524 	phys_addr_t pa;
525 
526 	pa = __pgd_pgtable_alloc(NULL, GFP_PGTABLE_KERNEL, pgtable_type);
527 	BUG_ON(pa == INVALID_PHYS_ADDR);
528 	return pa;
529 }
530 
split_contpte(pte_t * ptep)531 static void split_contpte(pte_t *ptep)
532 {
533 	int i;
534 
535 	ptep = PTR_ALIGN_DOWN(ptep, sizeof(*ptep) * CONT_PTES);
536 	for (i = 0; i < CONT_PTES; i++, ptep++)
537 		__set_pte(ptep, pte_mknoncont(__ptep_get(ptep)));
538 }
539 
split_pmd(pmd_t * pmdp,pmd_t pmd,gfp_t gfp,bool to_cont)540 static int split_pmd(pmd_t *pmdp, pmd_t pmd, gfp_t gfp, bool to_cont)
541 {
542 	pmdval_t tableprot = PMD_TYPE_TABLE | PMD_TABLE_UXN | PMD_TABLE_AF;
543 	unsigned long pfn = pmd_pfn(pmd);
544 	pgprot_t prot = pmd_pgprot(pmd);
545 	phys_addr_t pte_phys;
546 	pte_t *ptep;
547 	int i;
548 
549 	pte_phys = try_pgd_pgtable_alloc_init_mm(TABLE_PTE, gfp);
550 	if (pte_phys == INVALID_PHYS_ADDR)
551 		return -ENOMEM;
552 	ptep = (pte_t *)phys_to_virt(pte_phys);
553 
554 	if (pgprot_val(prot) & PMD_SECT_PXN)
555 		tableprot |= PMD_TABLE_PXN;
556 
557 	prot = __pgprot((pgprot_val(prot) & ~PTE_TYPE_MASK) | PTE_TYPE_PAGE);
558 	prot = __pgprot(pgprot_val(prot) & ~PTE_CONT);
559 	if (to_cont)
560 		prot = __pgprot(pgprot_val(prot) | PTE_CONT);
561 
562 	for (i = 0; i < PTRS_PER_PTE; i++, ptep++, pfn++)
563 		__set_pte(ptep, pfn_pte(pfn, prot));
564 
565 	/*
566 	 * Ensure the pte entries are visible to the table walker by the time
567 	 * the pmd entry that points to the ptes is visible.
568 	 */
569 	dsb(ishst);
570 	__pmd_populate(pmdp, pte_phys, tableprot);
571 
572 	return 0;
573 }
574 
split_contpmd(pmd_t * pmdp)575 static void split_contpmd(pmd_t *pmdp)
576 {
577 	int i;
578 
579 	pmdp = PTR_ALIGN_DOWN(pmdp, sizeof(*pmdp) * CONT_PMDS);
580 	for (i = 0; i < CONT_PMDS; i++, pmdp++)
581 		set_pmd(pmdp, pmd_mknoncont(pmdp_get(pmdp)));
582 }
583 
split_pud(pud_t * pudp,pud_t pud,gfp_t gfp,bool to_cont)584 static int split_pud(pud_t *pudp, pud_t pud, gfp_t gfp, bool to_cont)
585 {
586 	pudval_t tableprot = PUD_TYPE_TABLE | PUD_TABLE_UXN | PUD_TABLE_AF;
587 	unsigned int step = PMD_SIZE >> PAGE_SHIFT;
588 	unsigned long pfn = pud_pfn(pud);
589 	pgprot_t prot = pud_pgprot(pud);
590 	phys_addr_t pmd_phys;
591 	pmd_t *pmdp;
592 	int i;
593 
594 	pmd_phys = try_pgd_pgtable_alloc_init_mm(TABLE_PMD, gfp);
595 	if (pmd_phys == INVALID_PHYS_ADDR)
596 		return -ENOMEM;
597 	pmdp = (pmd_t *)phys_to_virt(pmd_phys);
598 
599 	if (pgprot_val(prot) & PMD_SECT_PXN)
600 		tableprot |= PUD_TABLE_PXN;
601 
602 	prot = __pgprot((pgprot_val(prot) & ~PMD_TYPE_MASK) | PMD_TYPE_SECT);
603 	prot = __pgprot(pgprot_val(prot) & ~PTE_CONT);
604 	if (to_cont)
605 		prot = __pgprot(pgprot_val(prot) | PTE_CONT);
606 
607 	for (i = 0; i < PTRS_PER_PMD; i++, pmdp++, pfn += step)
608 		set_pmd(pmdp, pfn_pmd(pfn, prot));
609 
610 	/*
611 	 * Ensure the pmd entries are visible to the table walker by the time
612 	 * the pud entry that points to the pmds is visible.
613 	 */
614 	dsb(ishst);
615 	__pud_populate(pudp, pmd_phys, tableprot);
616 
617 	return 0;
618 }
619 
split_kernel_leaf_mapping_locked(unsigned long addr)620 static int split_kernel_leaf_mapping_locked(unsigned long addr)
621 {
622 	pgd_t *pgdp, pgd;
623 	p4d_t *p4dp, p4d;
624 	pud_t *pudp, pud;
625 	pmd_t *pmdp, pmd;
626 	pte_t *ptep, pte;
627 	int ret = 0;
628 
629 	/*
630 	 * PGD: If addr is PGD aligned then addr already describes a leaf
631 	 * boundary. If not present then there is nothing to split.
632 	 */
633 	if (ALIGN_DOWN(addr, PGDIR_SIZE) == addr)
634 		goto out;
635 	pgdp = pgd_offset_k(addr);
636 	pgd = pgdp_get(pgdp);
637 	if (!pgd_present(pgd))
638 		goto out;
639 
640 	/*
641 	 * P4D: If addr is P4D aligned then addr already describes a leaf
642 	 * boundary. If not present then there is nothing to split.
643 	 */
644 	if (ALIGN_DOWN(addr, P4D_SIZE) == addr)
645 		goto out;
646 	p4dp = p4d_offset(pgdp, addr);
647 	p4d = p4dp_get(p4dp);
648 	if (!p4d_present(p4d))
649 		goto out;
650 
651 	/*
652 	 * PUD: If addr is PUD aligned then addr already describes a leaf
653 	 * boundary. If not present then there is nothing to split. Otherwise,
654 	 * if we have a pud leaf, split to contpmd.
655 	 */
656 	if (ALIGN_DOWN(addr, PUD_SIZE) == addr)
657 		goto out;
658 	pudp = pud_offset(p4dp, addr);
659 	pud = pudp_get(pudp);
660 	if (!pud_present(pud))
661 		goto out;
662 	if (pud_leaf(pud)) {
663 		ret = split_pud(pudp, pud, GFP_PGTABLE_KERNEL, true);
664 		if (ret)
665 			goto out;
666 	}
667 
668 	/*
669 	 * CONTPMD: If addr is CONTPMD aligned then addr already describes a
670 	 * leaf boundary. If not present then there is nothing to split.
671 	 * Otherwise, if we have a contpmd leaf, split to pmd.
672 	 */
673 	if (ALIGN_DOWN(addr, CONT_PMD_SIZE) == addr)
674 		goto out;
675 	pmdp = pmd_offset(pudp, addr);
676 	pmd = pmdp_get(pmdp);
677 	if (!pmd_present(pmd))
678 		goto out;
679 	if (pmd_leaf(pmd)) {
680 		if (pmd_cont(pmd))
681 			split_contpmd(pmdp);
682 		/*
683 		 * PMD: If addr is PMD aligned then addr already describes a
684 		 * leaf boundary. Otherwise, split to contpte.
685 		 */
686 		if (ALIGN_DOWN(addr, PMD_SIZE) == addr)
687 			goto out;
688 		ret = split_pmd(pmdp, pmd, GFP_PGTABLE_KERNEL, true);
689 		if (ret)
690 			goto out;
691 	}
692 
693 	/*
694 	 * CONTPTE: If addr is CONTPTE aligned then addr already describes a
695 	 * leaf boundary. If not present then there is nothing to split.
696 	 * Otherwise, if we have a contpte leaf, split to pte.
697 	 */
698 	if (ALIGN_DOWN(addr, CONT_PTE_SIZE) == addr)
699 		goto out;
700 	ptep = pte_offset_kernel(pmdp, addr);
701 	pte = __ptep_get(ptep);
702 	if (!pte_present(pte))
703 		goto out;
704 	if (pte_cont(pte))
705 		split_contpte(ptep);
706 
707 out:
708 	return ret;
709 }
710 
force_pte_mapping(void)711 static inline bool force_pte_mapping(void)
712 {
713 	const bool bbml2 = system_capabilities_finalized() ?
714 		system_supports_bbml2_noabort() : cpu_supports_bbml2_noabort();
715 
716 	if (debug_pagealloc_enabled())
717 		return true;
718 	if (bbml2)
719 		return false;
720 	return rodata_full || arm64_kfence_can_set_direct_map() || is_realm_world();
721 }
722 
split_leaf_mapping_possible(void)723 static inline bool split_leaf_mapping_possible(void)
724 {
725 	/*
726 	 * !BBML2_NOABORT systems should never run into scenarios where we would
727 	 * have to split. So exit early and let calling code detect it and raise
728 	 * a warning.
729 	 */
730 	if (!system_supports_bbml2_noabort())
731 		return false;
732 	return !force_pte_mapping();
733 }
734 
735 static DEFINE_MUTEX(pgtable_split_lock);
736 
split_kernel_leaf_mapping(unsigned long start,unsigned long end)737 int split_kernel_leaf_mapping(unsigned long start, unsigned long end)
738 {
739 	int ret;
740 
741 	/*
742 	 * Exit early if the region is within a pte-mapped area or if we can't
743 	 * split. For the latter case, the permission change code will raise a
744 	 * warning if not already pte-mapped.
745 	 */
746 	if (!split_leaf_mapping_possible() || is_kfence_address((void *)start))
747 		return 0;
748 
749 	/*
750 	 * Ensure start and end are at least page-aligned since this is the
751 	 * finest granularity we can split to.
752 	 */
753 	if (start != PAGE_ALIGN(start) || end != PAGE_ALIGN(end))
754 		return -EINVAL;
755 
756 	mutex_lock(&pgtable_split_lock);
757 	arch_enter_lazy_mmu_mode();
758 
759 	/*
760 	 * The split_kernel_leaf_mapping_locked() may sleep, it is not a
761 	 * problem for ARM64 since ARM64's lazy MMU implementation allows
762 	 * sleeping.
763 	 *
764 	 * Optimize for the common case of splitting out a single page from a
765 	 * larger mapping. Here we can just split on the "least aligned" of
766 	 * start and end and this will guarantee that there must also be a split
767 	 * on the more aligned address since the both addresses must be in the
768 	 * same contpte block and it must have been split to ptes.
769 	 */
770 	if (end - start == PAGE_SIZE) {
771 		start = __ffs(start) < __ffs(end) ? start : end;
772 		ret = split_kernel_leaf_mapping_locked(start);
773 	} else {
774 		ret = split_kernel_leaf_mapping_locked(start);
775 		if (!ret)
776 			ret = split_kernel_leaf_mapping_locked(end);
777 	}
778 
779 	arch_leave_lazy_mmu_mode();
780 	mutex_unlock(&pgtable_split_lock);
781 	return ret;
782 }
783 
split_to_ptes_pud_entry(pud_t * pudp,unsigned long addr,unsigned long next,struct mm_walk * walk)784 static int split_to_ptes_pud_entry(pud_t *pudp, unsigned long addr,
785 				   unsigned long next, struct mm_walk *walk)
786 {
787 	gfp_t gfp = *(gfp_t *)walk->private;
788 	pud_t pud = pudp_get(pudp);
789 	int ret = 0;
790 
791 	if (pud_leaf(pud))
792 		ret = split_pud(pudp, pud, gfp, false);
793 
794 	return ret;
795 }
796 
split_to_ptes_pmd_entry(pmd_t * pmdp,unsigned long addr,unsigned long next,struct mm_walk * walk)797 static int split_to_ptes_pmd_entry(pmd_t *pmdp, unsigned long addr,
798 				   unsigned long next, struct mm_walk *walk)
799 {
800 	gfp_t gfp = *(gfp_t *)walk->private;
801 	pmd_t pmd = pmdp_get(pmdp);
802 	int ret = 0;
803 
804 	if (pmd_leaf(pmd)) {
805 		if (pmd_cont(pmd))
806 			split_contpmd(pmdp);
807 		ret = split_pmd(pmdp, pmd, gfp, false);
808 
809 		/*
810 		 * We have split the pmd directly to ptes so there is no need to
811 		 * visit each pte to check if they are contpte.
812 		 */
813 		walk->action = ACTION_CONTINUE;
814 	}
815 
816 	return ret;
817 }
818 
split_to_ptes_pte_entry(pte_t * ptep,unsigned long addr,unsigned long next,struct mm_walk * walk)819 static int split_to_ptes_pte_entry(pte_t *ptep, unsigned long addr,
820 				   unsigned long next, struct mm_walk *walk)
821 {
822 	pte_t pte = __ptep_get(ptep);
823 
824 	if (pte_cont(pte))
825 		split_contpte(ptep);
826 
827 	return 0;
828 }
829 
830 static const struct mm_walk_ops split_to_ptes_ops = {
831 	.pud_entry	= split_to_ptes_pud_entry,
832 	.pmd_entry	= split_to_ptes_pmd_entry,
833 	.pte_entry	= split_to_ptes_pte_entry,
834 };
835 
range_split_to_ptes(unsigned long start,unsigned long end,gfp_t gfp)836 static int range_split_to_ptes(unsigned long start, unsigned long end, gfp_t gfp)
837 {
838 	int ret;
839 
840 	arch_enter_lazy_mmu_mode();
841 	ret = walk_kernel_page_table_range_lockless(start, end,
842 					&split_to_ptes_ops, NULL, &gfp);
843 	arch_leave_lazy_mmu_mode();
844 
845 	return ret;
846 }
847 
848 static bool linear_map_requires_bbml2 __initdata;
849 
850 u32 idmap_kpti_bbml2_flag;
851 
init_idmap_kpti_bbml2_flag(void)852 static void __init init_idmap_kpti_bbml2_flag(void)
853 {
854 	WRITE_ONCE(idmap_kpti_bbml2_flag, 1);
855 	/* Must be visible to other CPUs before stop_machine() is called. */
856 	smp_mb();
857 }
858 
linear_map_split_to_ptes(void * __unused)859 static int __init linear_map_split_to_ptes(void *__unused)
860 {
861 	/*
862 	 * Repainting the linear map must be done by CPU0 (the boot CPU) because
863 	 * that's the only CPU that we know supports BBML2. The other CPUs will
864 	 * be held in a waiting area with the idmap active.
865 	 */
866 	if (!smp_processor_id()) {
867 		unsigned long lstart = _PAGE_OFFSET(vabits_actual);
868 		unsigned long lend = PAGE_END;
869 		unsigned long kstart = (unsigned long)lm_alias(_stext);
870 		unsigned long kend = (unsigned long)lm_alias(__init_begin);
871 		int ret;
872 
873 		/*
874 		 * Wait for all secondary CPUs to be put into the waiting area.
875 		 */
876 		smp_cond_load_acquire(&idmap_kpti_bbml2_flag, VAL == num_online_cpus());
877 
878 		/*
879 		 * Walk all of the linear map [lstart, lend), except the kernel
880 		 * linear map alias [kstart, kend), and split all mappings to
881 		 * PTE. The kernel alias remains static throughout runtime so
882 		 * can continue to be safely mapped with large mappings.
883 		 */
884 		ret = range_split_to_ptes(lstart, kstart, GFP_ATOMIC);
885 		if (!ret)
886 			ret = range_split_to_ptes(kend, lend, GFP_ATOMIC);
887 		if (ret)
888 			panic("Failed to split linear map\n");
889 		flush_tlb_kernel_range(lstart, lend);
890 
891 		/*
892 		 * Relies on dsb in flush_tlb_kernel_range() to avoid reordering
893 		 * before any page table split operations.
894 		 */
895 		WRITE_ONCE(idmap_kpti_bbml2_flag, 0);
896 	} else {
897 		typedef void (wait_split_fn)(void);
898 		extern wait_split_fn wait_linear_map_split_to_ptes;
899 		wait_split_fn *wait_fn;
900 
901 		wait_fn = (void *)__pa_symbol(wait_linear_map_split_to_ptes);
902 
903 		/*
904 		 * At least one secondary CPU doesn't support BBML2 so cannot
905 		 * tolerate the size of the live mappings changing. So have the
906 		 * secondary CPUs wait for the boot CPU to make the changes
907 		 * with the idmap active and init_mm inactive.
908 		 */
909 		cpu_install_idmap();
910 		wait_fn();
911 		cpu_uninstall_idmap();
912 	}
913 
914 	return 0;
915 }
916 
linear_map_maybe_split_to_ptes(void)917 void __init linear_map_maybe_split_to_ptes(void)
918 {
919 	if (linear_map_requires_bbml2 && !system_supports_bbml2_noabort()) {
920 		init_idmap_kpti_bbml2_flag();
921 		stop_machine(linear_map_split_to_ptes, NULL, cpu_online_mask);
922 	}
923 }
924 
925 /*
926  * This function can only be used to modify existing table entries,
927  * without allocating new levels of table. Note that this permits the
928  * creation of new section or page entries.
929  */
create_mapping_noalloc(phys_addr_t phys,unsigned long virt,phys_addr_t size,pgprot_t prot)930 void __init create_mapping_noalloc(phys_addr_t phys, unsigned long virt,
931 				   phys_addr_t size, pgprot_t prot)
932 {
933 	if (virt < PAGE_OFFSET) {
934 		pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
935 			&phys, virt);
936 		return;
937 	}
938 	__create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL,
939 			     NO_CONT_MAPPINGS);
940 }
941 
create_pgd_mapping(struct mm_struct * mm,phys_addr_t phys,unsigned long virt,phys_addr_t size,pgprot_t prot,bool page_mappings_only)942 void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys,
943 			       unsigned long virt, phys_addr_t size,
944 			       pgprot_t prot, bool page_mappings_only)
945 {
946 	int flags = 0;
947 
948 	BUG_ON(mm == &init_mm);
949 
950 	if (page_mappings_only)
951 		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
952 
953 	__create_pgd_mapping(mm->pgd, phys, virt, size, prot,
954 			     pgd_pgtable_alloc_special_mm, flags);
955 }
956 
update_mapping_prot(phys_addr_t phys,unsigned long virt,phys_addr_t size,pgprot_t prot)957 static void update_mapping_prot(phys_addr_t phys, unsigned long virt,
958 				phys_addr_t size, pgprot_t prot)
959 {
960 	if (virt < PAGE_OFFSET) {
961 		pr_warn("BUG: not updating mapping for %pa at 0x%016lx - outside kernel range\n",
962 			&phys, virt);
963 		return;
964 	}
965 
966 	__create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL,
967 			     NO_CONT_MAPPINGS);
968 
969 	/* flush the TLBs after updating live kernel mappings */
970 	flush_tlb_kernel_range(virt, virt + size);
971 }
972 
__map_memblock(pgd_t * pgdp,phys_addr_t start,phys_addr_t end,pgprot_t prot,int flags)973 static void __init __map_memblock(pgd_t *pgdp, phys_addr_t start,
974 				  phys_addr_t end, pgprot_t prot, int flags)
975 {
976 	__create_pgd_mapping(pgdp, start, __phys_to_virt(start), end - start,
977 			     prot, early_pgtable_alloc, flags);
978 }
979 
mark_linear_text_alias_ro(void)980 void __init mark_linear_text_alias_ro(void)
981 {
982 	/*
983 	 * Remove the write permissions from the linear alias of .text/.rodata
984 	 */
985 	update_mapping_prot(__pa_symbol(_text), (unsigned long)lm_alias(_text),
986 			    (unsigned long)__init_begin - (unsigned long)_text,
987 			    PAGE_KERNEL_RO);
988 }
989 
990 #ifdef CONFIG_KFENCE
991 
992 bool __ro_after_init kfence_early_init = !!CONFIG_KFENCE_SAMPLE_INTERVAL;
993 
994 /* early_param() will be parsed before map_mem() below. */
parse_kfence_early_init(char * arg)995 static int __init parse_kfence_early_init(char *arg)
996 {
997 	int val;
998 
999 	if (get_option(&arg, &val))
1000 		kfence_early_init = !!val;
1001 	return 0;
1002 }
1003 early_param("kfence.sample_interval", parse_kfence_early_init);
1004 
arm64_kfence_alloc_pool(void)1005 static phys_addr_t __init arm64_kfence_alloc_pool(void)
1006 {
1007 	phys_addr_t kfence_pool;
1008 
1009 	if (!kfence_early_init)
1010 		return 0;
1011 
1012 	kfence_pool = memblock_phys_alloc(KFENCE_POOL_SIZE, PAGE_SIZE);
1013 	if (!kfence_pool) {
1014 		pr_err("failed to allocate kfence pool\n");
1015 		kfence_early_init = false;
1016 		return 0;
1017 	}
1018 
1019 	/* Temporarily mark as NOMAP. */
1020 	memblock_mark_nomap(kfence_pool, KFENCE_POOL_SIZE);
1021 
1022 	return kfence_pool;
1023 }
1024 
arm64_kfence_map_pool(phys_addr_t kfence_pool,pgd_t * pgdp)1025 static void __init arm64_kfence_map_pool(phys_addr_t kfence_pool, pgd_t *pgdp)
1026 {
1027 	if (!kfence_pool)
1028 		return;
1029 
1030 	/* KFENCE pool needs page-level mapping. */
1031 	__map_memblock(pgdp, kfence_pool, kfence_pool + KFENCE_POOL_SIZE,
1032 			pgprot_tagged(PAGE_KERNEL),
1033 			NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS);
1034 	memblock_clear_nomap(kfence_pool, KFENCE_POOL_SIZE);
1035 	__kfence_pool = phys_to_virt(kfence_pool);
1036 }
1037 
arch_kfence_init_pool(void)1038 bool arch_kfence_init_pool(void)
1039 {
1040 	unsigned long start = (unsigned long)__kfence_pool;
1041 	unsigned long end = start + KFENCE_POOL_SIZE;
1042 	int ret;
1043 
1044 	/* Exit early if we know the linear map is already pte-mapped. */
1045 	if (!split_leaf_mapping_possible())
1046 		return true;
1047 
1048 	/* Kfence pool is already pte-mapped for the early init case. */
1049 	if (kfence_early_init)
1050 		return true;
1051 
1052 	mutex_lock(&pgtable_split_lock);
1053 	ret = range_split_to_ptes(start, end, GFP_PGTABLE_KERNEL);
1054 	mutex_unlock(&pgtable_split_lock);
1055 
1056 	/*
1057 	 * Since the system supports bbml2_noabort, tlb invalidation is not
1058 	 * required here; the pgtable mappings have been split to pte but larger
1059 	 * entries may safely linger in the TLB.
1060 	 */
1061 
1062 	return !ret;
1063 }
1064 #else /* CONFIG_KFENCE */
1065 
arm64_kfence_alloc_pool(void)1066 static inline phys_addr_t arm64_kfence_alloc_pool(void) { return 0; }
arm64_kfence_map_pool(phys_addr_t kfence_pool,pgd_t * pgdp)1067 static inline void arm64_kfence_map_pool(phys_addr_t kfence_pool, pgd_t *pgdp) { }
1068 
1069 #endif /* CONFIG_KFENCE */
1070 
map_mem(pgd_t * pgdp)1071 static void __init map_mem(pgd_t *pgdp)
1072 {
1073 	static const u64 direct_map_end = _PAGE_END(VA_BITS_MIN);
1074 	phys_addr_t kernel_start = __pa_symbol(_text);
1075 	phys_addr_t kernel_end = __pa_symbol(__init_begin);
1076 	phys_addr_t start, end;
1077 	phys_addr_t early_kfence_pool;
1078 	int flags = NO_EXEC_MAPPINGS;
1079 	u64 i;
1080 
1081 	/*
1082 	 * Setting hierarchical PXNTable attributes on table entries covering
1083 	 * the linear region is only possible if it is guaranteed that no table
1084 	 * entries at any level are being shared between the linear region and
1085 	 * the vmalloc region. Check whether this is true for the PGD level, in
1086 	 * which case it is guaranteed to be true for all other levels as well.
1087 	 * (Unless we are running with support for LPA2, in which case the
1088 	 * entire reduced VA space is covered by a single pgd_t which will have
1089 	 * been populated without the PXNTable attribute by the time we get here.)
1090 	 */
1091 	BUILD_BUG_ON(pgd_index(direct_map_end - 1) == pgd_index(direct_map_end) &&
1092 		     pgd_index(_PAGE_OFFSET(VA_BITS_MIN)) != PTRS_PER_PGD - 1);
1093 
1094 	early_kfence_pool = arm64_kfence_alloc_pool();
1095 
1096 	linear_map_requires_bbml2 = !force_pte_mapping() && can_set_direct_map();
1097 
1098 	if (force_pte_mapping())
1099 		flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
1100 
1101 	/*
1102 	 * Take care not to create a writable alias for the
1103 	 * read-only text and rodata sections of the kernel image.
1104 	 * So temporarily mark them as NOMAP to skip mappings in
1105 	 * the following for-loop
1106 	 */
1107 	memblock_mark_nomap(kernel_start, kernel_end - kernel_start);
1108 
1109 	/* map all the memory banks */
1110 	for_each_mem_range(i, &start, &end) {
1111 		if (start >= end)
1112 			break;
1113 		/*
1114 		 * The linear map must allow allocation tags reading/writing
1115 		 * if MTE is present. Otherwise, it has the same attributes as
1116 		 * PAGE_KERNEL.
1117 		 */
1118 		__map_memblock(pgdp, start, end, pgprot_tagged(PAGE_KERNEL),
1119 			       flags);
1120 	}
1121 
1122 	/*
1123 	 * Map the linear alias of the [_text, __init_begin) interval
1124 	 * as non-executable now, and remove the write permission in
1125 	 * mark_linear_text_alias_ro() below (which will be called after
1126 	 * alternative patching has completed). This makes the contents
1127 	 * of the region accessible to subsystems such as hibernate,
1128 	 * but protects it from inadvertent modification or execution.
1129 	 * Note that contiguous mappings cannot be remapped in this way,
1130 	 * so we should avoid them here.
1131 	 */
1132 	__map_memblock(pgdp, kernel_start, kernel_end,
1133 		       PAGE_KERNEL, NO_CONT_MAPPINGS);
1134 	memblock_clear_nomap(kernel_start, kernel_end - kernel_start);
1135 	arm64_kfence_map_pool(early_kfence_pool, pgdp);
1136 }
1137 
mark_rodata_ro(void)1138 void mark_rodata_ro(void)
1139 {
1140 	unsigned long section_size;
1141 
1142 	/*
1143 	 * mark .rodata as read only. Use __init_begin rather than __end_rodata
1144 	 * to cover NOTES and EXCEPTION_TABLE.
1145 	 */
1146 	section_size = (unsigned long)__init_begin - (unsigned long)__start_rodata;
1147 	WRITE_ONCE(rodata_is_rw, false);
1148 	update_mapping_prot(__pa_symbol(__start_rodata), (unsigned long)__start_rodata,
1149 			    section_size, PAGE_KERNEL_RO);
1150 	/* mark the range between _text and _stext as read only. */
1151 	update_mapping_prot(__pa_symbol(_text), (unsigned long)_text,
1152 			    (unsigned long)_stext - (unsigned long)_text,
1153 			    PAGE_KERNEL_RO);
1154 }
1155 
declare_vma(struct vm_struct * vma,void * va_start,void * va_end,unsigned long vm_flags)1156 static void __init declare_vma(struct vm_struct *vma,
1157 			       void *va_start, void *va_end,
1158 			       unsigned long vm_flags)
1159 {
1160 	phys_addr_t pa_start = __pa_symbol(va_start);
1161 	unsigned long size = va_end - va_start;
1162 
1163 	BUG_ON(!PAGE_ALIGNED(pa_start));
1164 	BUG_ON(!PAGE_ALIGNED(size));
1165 
1166 	if (!(vm_flags & VM_NO_GUARD))
1167 		size += PAGE_SIZE;
1168 
1169 	vma->addr	= va_start;
1170 	vma->phys_addr	= pa_start;
1171 	vma->size	= size;
1172 	vma->flags	= VM_MAP | vm_flags;
1173 	vma->caller	= __builtin_return_address(0);
1174 
1175 	vm_area_add_early(vma);
1176 }
1177 
1178 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
1179 #define KPTI_NG_TEMP_VA		(-(1UL << PMD_SHIFT))
1180 
1181 static phys_addr_t kpti_ng_temp_alloc __initdata;
1182 
kpti_ng_pgd_alloc(enum pgtable_type type)1183 static phys_addr_t __init kpti_ng_pgd_alloc(enum pgtable_type type)
1184 {
1185 	kpti_ng_temp_alloc -= PAGE_SIZE;
1186 	return kpti_ng_temp_alloc;
1187 }
1188 
__kpti_install_ng_mappings(void * __unused)1189 static int __init __kpti_install_ng_mappings(void *__unused)
1190 {
1191 	typedef void (kpti_remap_fn)(int, int, phys_addr_t, unsigned long);
1192 	extern kpti_remap_fn idmap_kpti_install_ng_mappings;
1193 	kpti_remap_fn *remap_fn;
1194 
1195 	int cpu = smp_processor_id();
1196 	int levels = CONFIG_PGTABLE_LEVELS;
1197 	int order = order_base_2(levels);
1198 	u64 kpti_ng_temp_pgd_pa = 0;
1199 	pgd_t *kpti_ng_temp_pgd;
1200 	u64 alloc = 0;
1201 
1202 	if (levels == 5 && !pgtable_l5_enabled())
1203 		levels = 4;
1204 	else if (levels == 4 && !pgtable_l4_enabled())
1205 		levels = 3;
1206 
1207 	remap_fn = (void *)__pa_symbol(idmap_kpti_install_ng_mappings);
1208 
1209 	if (!cpu) {
1210 		alloc = __get_free_pages(GFP_ATOMIC | __GFP_ZERO, order);
1211 		kpti_ng_temp_pgd = (pgd_t *)(alloc + (levels - 1) * PAGE_SIZE);
1212 		kpti_ng_temp_alloc = kpti_ng_temp_pgd_pa = __pa(kpti_ng_temp_pgd);
1213 
1214 		//
1215 		// Create a minimal page table hierarchy that permits us to map
1216 		// the swapper page tables temporarily as we traverse them.
1217 		//
1218 		// The physical pages are laid out as follows:
1219 		//
1220 		// +--------+-/-------+-/------ +-/------ +-\\\--------+
1221 		// :  PTE[] : | PMD[] : | PUD[] : | P4D[] : ||| PGD[]  :
1222 		// +--------+-\-------+-\------ +-\------ +-///--------+
1223 		//      ^
1224 		// The first page is mapped into this hierarchy at a PMD_SHIFT
1225 		// aligned virtual address, so that we can manipulate the PTE
1226 		// level entries while the mapping is active. The first entry
1227 		// covers the PTE[] page itself, the remaining entries are free
1228 		// to be used as a ad-hoc fixmap.
1229 		//
1230 		__create_pgd_mapping_locked(kpti_ng_temp_pgd, __pa(alloc),
1231 					    KPTI_NG_TEMP_VA, PAGE_SIZE, PAGE_KERNEL,
1232 					    kpti_ng_pgd_alloc, 0);
1233 	}
1234 
1235 	cpu_install_idmap();
1236 	remap_fn(cpu, num_online_cpus(), kpti_ng_temp_pgd_pa, KPTI_NG_TEMP_VA);
1237 	cpu_uninstall_idmap();
1238 
1239 	if (!cpu) {
1240 		free_pages(alloc, order);
1241 		arm64_use_ng_mappings = true;
1242 	}
1243 
1244 	return 0;
1245 }
1246 
kpti_install_ng_mappings(void)1247 void __init kpti_install_ng_mappings(void)
1248 {
1249 	/* Check whether KPTI is going to be used */
1250 	if (!arm64_kernel_unmapped_at_el0())
1251 		return;
1252 
1253 	/*
1254 	 * We don't need to rewrite the page-tables if either we've done
1255 	 * it already or we have KASLR enabled and therefore have not
1256 	 * created any global mappings at all.
1257 	 */
1258 	if (arm64_use_ng_mappings)
1259 		return;
1260 
1261 	init_idmap_kpti_bbml2_flag();
1262 	stop_machine(__kpti_install_ng_mappings, NULL, cpu_online_mask);
1263 }
1264 
kernel_exec_prot(void)1265 static pgprot_t __init kernel_exec_prot(void)
1266 {
1267 	return rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;
1268 }
1269 
map_entry_trampoline(void)1270 static int __init map_entry_trampoline(void)
1271 {
1272 	int i;
1273 
1274 	if (!arm64_kernel_unmapped_at_el0())
1275 		return 0;
1276 
1277 	pgprot_t prot = kernel_exec_prot();
1278 	phys_addr_t pa_start = __pa_symbol(__entry_tramp_text_start);
1279 
1280 	/* The trampoline is always mapped and can therefore be global */
1281 	pgprot_val(prot) &= ~PTE_NG;
1282 
1283 	/* Map only the text into the trampoline page table */
1284 	memset(tramp_pg_dir, 0, PGD_SIZE);
1285 	__create_pgd_mapping(tramp_pg_dir, pa_start, TRAMP_VALIAS,
1286 			     entry_tramp_text_size(), prot,
1287 			     pgd_pgtable_alloc_init_mm, NO_BLOCK_MAPPINGS);
1288 
1289 	/* Map both the text and data into the kernel page table */
1290 	for (i = 0; i < DIV_ROUND_UP(entry_tramp_text_size(), PAGE_SIZE); i++)
1291 		__set_fixmap(FIX_ENTRY_TRAMP_TEXT1 - i,
1292 			     pa_start + i * PAGE_SIZE, prot);
1293 
1294 	if (IS_ENABLED(CONFIG_RELOCATABLE))
1295 		__set_fixmap(FIX_ENTRY_TRAMP_TEXT1 - i,
1296 			     pa_start + i * PAGE_SIZE, PAGE_KERNEL_RO);
1297 
1298 	return 0;
1299 }
1300 core_initcall(map_entry_trampoline);
1301 #endif
1302 
1303 /*
1304  * Declare the VMA areas for the kernel
1305  */
declare_kernel_vmas(void)1306 static void __init declare_kernel_vmas(void)
1307 {
1308 	static struct vm_struct vmlinux_seg[KERNEL_SEGMENT_COUNT];
1309 
1310 	declare_vma(&vmlinux_seg[0], _text, _etext, VM_NO_GUARD);
1311 	declare_vma(&vmlinux_seg[1], __start_rodata, __inittext_begin, VM_NO_GUARD);
1312 	declare_vma(&vmlinux_seg[2], __inittext_begin, __inittext_end, VM_NO_GUARD);
1313 	declare_vma(&vmlinux_seg[3], __initdata_begin, __initdata_end, VM_NO_GUARD);
1314 	declare_vma(&vmlinux_seg[4], _data, _end, 0);
1315 }
1316 
1317 void __pi_map_range(phys_addr_t *pte, u64 start, u64 end, phys_addr_t pa,
1318 		    pgprot_t prot, int level, pte_t *tbl, bool may_use_cont,
1319 		    u64 va_offset);
1320 
1321 static u8 idmap_ptes[IDMAP_LEVELS - 1][PAGE_SIZE] __aligned(PAGE_SIZE) __ro_after_init,
1322 	  kpti_bbml2_ptes[IDMAP_LEVELS - 1][PAGE_SIZE] __aligned(PAGE_SIZE) __ro_after_init;
1323 
create_idmap(void)1324 static void __init create_idmap(void)
1325 {
1326 	phys_addr_t start = __pa_symbol(__idmap_text_start);
1327 	phys_addr_t end   = __pa_symbol(__idmap_text_end);
1328 	phys_addr_t ptep  = __pa_symbol(idmap_ptes);
1329 
1330 	__pi_map_range(&ptep, start, end, start, PAGE_KERNEL_ROX,
1331 		       IDMAP_ROOT_LEVEL, (pte_t *)idmap_pg_dir, false,
1332 		       __phys_to_virt(ptep) - ptep);
1333 
1334 	if (linear_map_requires_bbml2 ||
1335 	    (IS_ENABLED(CONFIG_UNMAP_KERNEL_AT_EL0) && !arm64_use_ng_mappings)) {
1336 		phys_addr_t pa = __pa_symbol(&idmap_kpti_bbml2_flag);
1337 
1338 		/*
1339 		 * The KPTI G-to-nG conversion code needs a read-write mapping
1340 		 * of its synchronization flag in the ID map. This is also used
1341 		 * when splitting the linear map to ptes if a secondary CPU
1342 		 * doesn't support bbml2.
1343 		 */
1344 		ptep = __pa_symbol(kpti_bbml2_ptes);
1345 		__pi_map_range(&ptep, pa, pa + sizeof(u32), pa, PAGE_KERNEL,
1346 			       IDMAP_ROOT_LEVEL, (pte_t *)idmap_pg_dir, false,
1347 			       __phys_to_virt(ptep) - ptep);
1348 	}
1349 }
1350 
paging_init(void)1351 void __init paging_init(void)
1352 {
1353 	map_mem(swapper_pg_dir);
1354 
1355 	memblock_allow_resize();
1356 
1357 	create_idmap();
1358 	declare_kernel_vmas();
1359 }
1360 
1361 #ifdef CONFIG_MEMORY_HOTPLUG
free_hotplug_page_range(struct page * page,size_t size,struct vmem_altmap * altmap)1362 static void free_hotplug_page_range(struct page *page, size_t size,
1363 				    struct vmem_altmap *altmap)
1364 {
1365 	if (altmap) {
1366 		vmem_altmap_free(altmap, size >> PAGE_SHIFT);
1367 	} else {
1368 		WARN_ON(PageReserved(page));
1369 		__free_pages(page, get_order(size));
1370 	}
1371 }
1372 
free_hotplug_pgtable_page(struct page * page)1373 static void free_hotplug_pgtable_page(struct page *page)
1374 {
1375 	free_hotplug_page_range(page, PAGE_SIZE, NULL);
1376 }
1377 
pgtable_range_aligned(unsigned long start,unsigned long end,unsigned long floor,unsigned long ceiling,unsigned long mask)1378 static bool pgtable_range_aligned(unsigned long start, unsigned long end,
1379 				  unsigned long floor, unsigned long ceiling,
1380 				  unsigned long mask)
1381 {
1382 	start &= mask;
1383 	if (start < floor)
1384 		return false;
1385 
1386 	if (ceiling) {
1387 		ceiling &= mask;
1388 		if (!ceiling)
1389 			return false;
1390 	}
1391 
1392 	if (end - 1 > ceiling - 1)
1393 		return false;
1394 	return true;
1395 }
1396 
unmap_hotplug_pte_range(pmd_t * pmdp,unsigned long addr,unsigned long end,bool free_mapped,struct vmem_altmap * altmap)1397 static void unmap_hotplug_pte_range(pmd_t *pmdp, unsigned long addr,
1398 				    unsigned long end, bool free_mapped,
1399 				    struct vmem_altmap *altmap)
1400 {
1401 	pte_t *ptep, pte;
1402 
1403 	do {
1404 		ptep = pte_offset_kernel(pmdp, addr);
1405 		pte = __ptep_get(ptep);
1406 		if (pte_none(pte))
1407 			continue;
1408 
1409 		WARN_ON(!pte_present(pte));
1410 		__pte_clear(&init_mm, addr, ptep);
1411 		flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
1412 		if (free_mapped)
1413 			free_hotplug_page_range(pte_page(pte),
1414 						PAGE_SIZE, altmap);
1415 	} while (addr += PAGE_SIZE, addr < end);
1416 }
1417 
unmap_hotplug_pmd_range(pud_t * pudp,unsigned long addr,unsigned long end,bool free_mapped,struct vmem_altmap * altmap)1418 static void unmap_hotplug_pmd_range(pud_t *pudp, unsigned long addr,
1419 				    unsigned long end, bool free_mapped,
1420 				    struct vmem_altmap *altmap)
1421 {
1422 	unsigned long next;
1423 	pmd_t *pmdp, pmd;
1424 
1425 	do {
1426 		next = pmd_addr_end(addr, end);
1427 		pmdp = pmd_offset(pudp, addr);
1428 		pmd = READ_ONCE(*pmdp);
1429 		if (pmd_none(pmd))
1430 			continue;
1431 
1432 		WARN_ON(!pmd_present(pmd));
1433 		if (pmd_sect(pmd)) {
1434 			pmd_clear(pmdp);
1435 
1436 			/*
1437 			 * One TLBI should be sufficient here as the PMD_SIZE
1438 			 * range is mapped with a single block entry.
1439 			 */
1440 			flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
1441 			if (free_mapped)
1442 				free_hotplug_page_range(pmd_page(pmd),
1443 							PMD_SIZE, altmap);
1444 			continue;
1445 		}
1446 		WARN_ON(!pmd_table(pmd));
1447 		unmap_hotplug_pte_range(pmdp, addr, next, free_mapped, altmap);
1448 	} while (addr = next, addr < end);
1449 }
1450 
unmap_hotplug_pud_range(p4d_t * p4dp,unsigned long addr,unsigned long end,bool free_mapped,struct vmem_altmap * altmap)1451 static void unmap_hotplug_pud_range(p4d_t *p4dp, unsigned long addr,
1452 				    unsigned long end, bool free_mapped,
1453 				    struct vmem_altmap *altmap)
1454 {
1455 	unsigned long next;
1456 	pud_t *pudp, pud;
1457 
1458 	do {
1459 		next = pud_addr_end(addr, end);
1460 		pudp = pud_offset(p4dp, addr);
1461 		pud = READ_ONCE(*pudp);
1462 		if (pud_none(pud))
1463 			continue;
1464 
1465 		WARN_ON(!pud_present(pud));
1466 		if (pud_sect(pud)) {
1467 			pud_clear(pudp);
1468 
1469 			/*
1470 			 * One TLBI should be sufficient here as the PUD_SIZE
1471 			 * range is mapped with a single block entry.
1472 			 */
1473 			flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
1474 			if (free_mapped)
1475 				free_hotplug_page_range(pud_page(pud),
1476 							PUD_SIZE, altmap);
1477 			continue;
1478 		}
1479 		WARN_ON(!pud_table(pud));
1480 		unmap_hotplug_pmd_range(pudp, addr, next, free_mapped, altmap);
1481 	} while (addr = next, addr < end);
1482 }
1483 
unmap_hotplug_p4d_range(pgd_t * pgdp,unsigned long addr,unsigned long end,bool free_mapped,struct vmem_altmap * altmap)1484 static void unmap_hotplug_p4d_range(pgd_t *pgdp, unsigned long addr,
1485 				    unsigned long end, bool free_mapped,
1486 				    struct vmem_altmap *altmap)
1487 {
1488 	unsigned long next;
1489 	p4d_t *p4dp, p4d;
1490 
1491 	do {
1492 		next = p4d_addr_end(addr, end);
1493 		p4dp = p4d_offset(pgdp, addr);
1494 		p4d = READ_ONCE(*p4dp);
1495 		if (p4d_none(p4d))
1496 			continue;
1497 
1498 		WARN_ON(!p4d_present(p4d));
1499 		unmap_hotplug_pud_range(p4dp, addr, next, free_mapped, altmap);
1500 	} while (addr = next, addr < end);
1501 }
1502 
unmap_hotplug_range(unsigned long addr,unsigned long end,bool free_mapped,struct vmem_altmap * altmap)1503 static void unmap_hotplug_range(unsigned long addr, unsigned long end,
1504 				bool free_mapped, struct vmem_altmap *altmap)
1505 {
1506 	unsigned long next;
1507 	pgd_t *pgdp, pgd;
1508 
1509 	/*
1510 	 * altmap can only be used as vmemmap mapping backing memory.
1511 	 * In case the backing memory itself is not being freed, then
1512 	 * altmap is irrelevant. Warn about this inconsistency when
1513 	 * encountered.
1514 	 */
1515 	WARN_ON(!free_mapped && altmap);
1516 
1517 	do {
1518 		next = pgd_addr_end(addr, end);
1519 		pgdp = pgd_offset_k(addr);
1520 		pgd = READ_ONCE(*pgdp);
1521 		if (pgd_none(pgd))
1522 			continue;
1523 
1524 		WARN_ON(!pgd_present(pgd));
1525 		unmap_hotplug_p4d_range(pgdp, addr, next, free_mapped, altmap);
1526 	} while (addr = next, addr < end);
1527 }
1528 
free_empty_pte_table(pmd_t * pmdp,unsigned long addr,unsigned long end,unsigned long floor,unsigned long ceiling)1529 static void free_empty_pte_table(pmd_t *pmdp, unsigned long addr,
1530 				 unsigned long end, unsigned long floor,
1531 				 unsigned long ceiling)
1532 {
1533 	pte_t *ptep, pte;
1534 	unsigned long i, start = addr;
1535 
1536 	do {
1537 		ptep = pte_offset_kernel(pmdp, addr);
1538 		pte = __ptep_get(ptep);
1539 
1540 		/*
1541 		 * This is just a sanity check here which verifies that
1542 		 * pte clearing has been done by earlier unmap loops.
1543 		 */
1544 		WARN_ON(!pte_none(pte));
1545 	} while (addr += PAGE_SIZE, addr < end);
1546 
1547 	if (!pgtable_range_aligned(start, end, floor, ceiling, PMD_MASK))
1548 		return;
1549 
1550 	/*
1551 	 * Check whether we can free the pte page if the rest of the
1552 	 * entries are empty. Overlap with other regions have been
1553 	 * handled by the floor/ceiling check.
1554 	 */
1555 	ptep = pte_offset_kernel(pmdp, 0UL);
1556 	for (i = 0; i < PTRS_PER_PTE; i++) {
1557 		if (!pte_none(__ptep_get(&ptep[i])))
1558 			return;
1559 	}
1560 
1561 	pmd_clear(pmdp);
1562 	__flush_tlb_kernel_pgtable(start);
1563 	free_hotplug_pgtable_page(virt_to_page(ptep));
1564 }
1565 
free_empty_pmd_table(pud_t * pudp,unsigned long addr,unsigned long end,unsigned long floor,unsigned long ceiling)1566 static void free_empty_pmd_table(pud_t *pudp, unsigned long addr,
1567 				 unsigned long end, unsigned long floor,
1568 				 unsigned long ceiling)
1569 {
1570 	pmd_t *pmdp, pmd;
1571 	unsigned long i, next, start = addr;
1572 
1573 	do {
1574 		next = pmd_addr_end(addr, end);
1575 		pmdp = pmd_offset(pudp, addr);
1576 		pmd = READ_ONCE(*pmdp);
1577 		if (pmd_none(pmd))
1578 			continue;
1579 
1580 		WARN_ON(!pmd_present(pmd) || !pmd_table(pmd) || pmd_sect(pmd));
1581 		free_empty_pte_table(pmdp, addr, next, floor, ceiling);
1582 	} while (addr = next, addr < end);
1583 
1584 	if (CONFIG_PGTABLE_LEVELS <= 2)
1585 		return;
1586 
1587 	if (!pgtable_range_aligned(start, end, floor, ceiling, PUD_MASK))
1588 		return;
1589 
1590 	/*
1591 	 * Check whether we can free the pmd page if the rest of the
1592 	 * entries are empty. Overlap with other regions have been
1593 	 * handled by the floor/ceiling check.
1594 	 */
1595 	pmdp = pmd_offset(pudp, 0UL);
1596 	for (i = 0; i < PTRS_PER_PMD; i++) {
1597 		if (!pmd_none(READ_ONCE(pmdp[i])))
1598 			return;
1599 	}
1600 
1601 	pud_clear(pudp);
1602 	__flush_tlb_kernel_pgtable(start);
1603 	free_hotplug_pgtable_page(virt_to_page(pmdp));
1604 }
1605 
free_empty_pud_table(p4d_t * p4dp,unsigned long addr,unsigned long end,unsigned long floor,unsigned long ceiling)1606 static void free_empty_pud_table(p4d_t *p4dp, unsigned long addr,
1607 				 unsigned long end, unsigned long floor,
1608 				 unsigned long ceiling)
1609 {
1610 	pud_t *pudp, pud;
1611 	unsigned long i, next, start = addr;
1612 
1613 	do {
1614 		next = pud_addr_end(addr, end);
1615 		pudp = pud_offset(p4dp, addr);
1616 		pud = READ_ONCE(*pudp);
1617 		if (pud_none(pud))
1618 			continue;
1619 
1620 		WARN_ON(!pud_present(pud) || !pud_table(pud) || pud_sect(pud));
1621 		free_empty_pmd_table(pudp, addr, next, floor, ceiling);
1622 	} while (addr = next, addr < end);
1623 
1624 	if (!pgtable_l4_enabled())
1625 		return;
1626 
1627 	if (!pgtable_range_aligned(start, end, floor, ceiling, P4D_MASK))
1628 		return;
1629 
1630 	/*
1631 	 * Check whether we can free the pud page if the rest of the
1632 	 * entries are empty. Overlap with other regions have been
1633 	 * handled by the floor/ceiling check.
1634 	 */
1635 	pudp = pud_offset(p4dp, 0UL);
1636 	for (i = 0; i < PTRS_PER_PUD; i++) {
1637 		if (!pud_none(READ_ONCE(pudp[i])))
1638 			return;
1639 	}
1640 
1641 	p4d_clear(p4dp);
1642 	__flush_tlb_kernel_pgtable(start);
1643 	free_hotplug_pgtable_page(virt_to_page(pudp));
1644 }
1645 
free_empty_p4d_table(pgd_t * pgdp,unsigned long addr,unsigned long end,unsigned long floor,unsigned long ceiling)1646 static void free_empty_p4d_table(pgd_t *pgdp, unsigned long addr,
1647 				 unsigned long end, unsigned long floor,
1648 				 unsigned long ceiling)
1649 {
1650 	p4d_t *p4dp, p4d;
1651 	unsigned long i, next, start = addr;
1652 
1653 	do {
1654 		next = p4d_addr_end(addr, end);
1655 		p4dp = p4d_offset(pgdp, addr);
1656 		p4d = READ_ONCE(*p4dp);
1657 		if (p4d_none(p4d))
1658 			continue;
1659 
1660 		WARN_ON(!p4d_present(p4d));
1661 		free_empty_pud_table(p4dp, addr, next, floor, ceiling);
1662 	} while (addr = next, addr < end);
1663 
1664 	if (!pgtable_l5_enabled())
1665 		return;
1666 
1667 	if (!pgtable_range_aligned(start, end, floor, ceiling, PGDIR_MASK))
1668 		return;
1669 
1670 	/*
1671 	 * Check whether we can free the p4d page if the rest of the
1672 	 * entries are empty. Overlap with other regions have been
1673 	 * handled by the floor/ceiling check.
1674 	 */
1675 	p4dp = p4d_offset(pgdp, 0UL);
1676 	for (i = 0; i < PTRS_PER_P4D; i++) {
1677 		if (!p4d_none(READ_ONCE(p4dp[i])))
1678 			return;
1679 	}
1680 
1681 	pgd_clear(pgdp);
1682 	__flush_tlb_kernel_pgtable(start);
1683 	free_hotplug_pgtable_page(virt_to_page(p4dp));
1684 }
1685 
free_empty_tables(unsigned long addr,unsigned long end,unsigned long floor,unsigned long ceiling)1686 static void free_empty_tables(unsigned long addr, unsigned long end,
1687 			      unsigned long floor, unsigned long ceiling)
1688 {
1689 	unsigned long next;
1690 	pgd_t *pgdp, pgd;
1691 
1692 	do {
1693 		next = pgd_addr_end(addr, end);
1694 		pgdp = pgd_offset_k(addr);
1695 		pgd = READ_ONCE(*pgdp);
1696 		if (pgd_none(pgd))
1697 			continue;
1698 
1699 		WARN_ON(!pgd_present(pgd));
1700 		free_empty_p4d_table(pgdp, addr, next, floor, ceiling);
1701 	} while (addr = next, addr < end);
1702 }
1703 #endif
1704 
vmemmap_set_pmd(pmd_t * pmdp,void * p,int node,unsigned long addr,unsigned long next)1705 void __meminit vmemmap_set_pmd(pmd_t *pmdp, void *p, int node,
1706 			       unsigned long addr, unsigned long next)
1707 {
1708 	pmd_set_huge(pmdp, __pa(p), __pgprot(PROT_SECT_NORMAL));
1709 }
1710 
vmemmap_check_pmd(pmd_t * pmdp,int node,unsigned long addr,unsigned long next)1711 int __meminit vmemmap_check_pmd(pmd_t *pmdp, int node,
1712 				unsigned long addr, unsigned long next)
1713 {
1714 	vmemmap_verify((pte_t *)pmdp, node, addr, next);
1715 
1716 	return pmd_sect(READ_ONCE(*pmdp));
1717 }
1718 
vmemmap_populate(unsigned long start,unsigned long end,int node,struct vmem_altmap * altmap)1719 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
1720 		struct vmem_altmap *altmap)
1721 {
1722 	WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END));
1723 	/* [start, end] should be within one section */
1724 	WARN_ON_ONCE(end - start > PAGES_PER_SECTION * sizeof(struct page));
1725 
1726 	if (!IS_ENABLED(CONFIG_ARM64_4K_PAGES) ||
1727 	    (end - start < PAGES_PER_SECTION * sizeof(struct page)))
1728 		return vmemmap_populate_basepages(start, end, node, altmap);
1729 	else
1730 		return vmemmap_populate_hugepages(start, end, node, altmap);
1731 }
1732 
1733 #ifdef CONFIG_MEMORY_HOTPLUG
vmemmap_free(unsigned long start,unsigned long end,struct vmem_altmap * altmap)1734 void vmemmap_free(unsigned long start, unsigned long end,
1735 		struct vmem_altmap *altmap)
1736 {
1737 	WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END));
1738 
1739 	unmap_hotplug_range(start, end, true, altmap);
1740 	free_empty_tables(start, end, VMEMMAP_START, VMEMMAP_END);
1741 }
1742 #endif /* CONFIG_MEMORY_HOTPLUG */
1743 
pud_set_huge(pud_t * pudp,phys_addr_t phys,pgprot_t prot)1744 int pud_set_huge(pud_t *pudp, phys_addr_t phys, pgprot_t prot)
1745 {
1746 	pud_t new_pud = pfn_pud(__phys_to_pfn(phys), mk_pud_sect_prot(prot));
1747 
1748 	/* Only allow permission changes for now */
1749 	if (!pgattr_change_is_safe(READ_ONCE(pud_val(*pudp)),
1750 				   pud_val(new_pud)))
1751 		return 0;
1752 
1753 	VM_BUG_ON(phys & ~PUD_MASK);
1754 	set_pud(pudp, new_pud);
1755 	return 1;
1756 }
1757 
pmd_set_huge(pmd_t * pmdp,phys_addr_t phys,pgprot_t prot)1758 int pmd_set_huge(pmd_t *pmdp, phys_addr_t phys, pgprot_t prot)
1759 {
1760 	pmd_t new_pmd = pfn_pmd(__phys_to_pfn(phys), mk_pmd_sect_prot(prot));
1761 
1762 	/* Only allow permission changes for now */
1763 	if (!pgattr_change_is_safe(READ_ONCE(pmd_val(*pmdp)),
1764 				   pmd_val(new_pmd)))
1765 		return 0;
1766 
1767 	VM_BUG_ON(phys & ~PMD_MASK);
1768 	set_pmd(pmdp, new_pmd);
1769 	return 1;
1770 }
1771 
1772 #ifndef __PAGETABLE_P4D_FOLDED
p4d_clear_huge(p4d_t * p4dp)1773 void p4d_clear_huge(p4d_t *p4dp)
1774 {
1775 }
1776 #endif
1777 
pud_clear_huge(pud_t * pudp)1778 int pud_clear_huge(pud_t *pudp)
1779 {
1780 	if (!pud_sect(READ_ONCE(*pudp)))
1781 		return 0;
1782 	pud_clear(pudp);
1783 	return 1;
1784 }
1785 
pmd_clear_huge(pmd_t * pmdp)1786 int pmd_clear_huge(pmd_t *pmdp)
1787 {
1788 	if (!pmd_sect(READ_ONCE(*pmdp)))
1789 		return 0;
1790 	pmd_clear(pmdp);
1791 	return 1;
1792 }
1793 
__pmd_free_pte_page(pmd_t * pmdp,unsigned long addr,bool acquire_mmap_lock)1794 static int __pmd_free_pte_page(pmd_t *pmdp, unsigned long addr,
1795 			       bool acquire_mmap_lock)
1796 {
1797 	pte_t *table;
1798 	pmd_t pmd;
1799 
1800 	pmd = READ_ONCE(*pmdp);
1801 
1802 	if (!pmd_table(pmd)) {
1803 		VM_WARN_ON(1);
1804 		return 1;
1805 	}
1806 
1807 	/* See comment in pud_free_pmd_page for static key logic */
1808 	table = pte_offset_kernel(pmdp, addr);
1809 	pmd_clear(pmdp);
1810 	__flush_tlb_kernel_pgtable(addr);
1811 	if (static_branch_unlikely(&arm64_ptdump_lock_key) && acquire_mmap_lock) {
1812 		mmap_read_lock(&init_mm);
1813 		mmap_read_unlock(&init_mm);
1814 	}
1815 
1816 	pte_free_kernel(NULL, table);
1817 	return 1;
1818 }
1819 
pmd_free_pte_page(pmd_t * pmdp,unsigned long addr)1820 int pmd_free_pte_page(pmd_t *pmdp, unsigned long addr)
1821 {
1822 	/* If ptdump is walking the pagetables, acquire init_mm.mmap_lock */
1823 	return __pmd_free_pte_page(pmdp, addr, /* acquire_mmap_lock = */ true);
1824 }
1825 
pud_free_pmd_page(pud_t * pudp,unsigned long addr)1826 int pud_free_pmd_page(pud_t *pudp, unsigned long addr)
1827 {
1828 	pmd_t *table;
1829 	pmd_t *pmdp;
1830 	pud_t pud;
1831 	unsigned long next, end;
1832 
1833 	pud = READ_ONCE(*pudp);
1834 
1835 	if (!pud_table(pud)) {
1836 		VM_WARN_ON(1);
1837 		return 1;
1838 	}
1839 
1840 	table = pmd_offset(pudp, addr);
1841 
1842 	/*
1843 	 * Our objective is to prevent ptdump from reading a PMD table which has
1844 	 * been freed. In this race, if pud_free_pmd_page observes the key on
1845 	 * (which got flipped by ptdump) then the mmap lock sequence here will,
1846 	 * as a result of the mmap write lock/unlock sequence in ptdump, give
1847 	 * us the correct synchronization. If not, this means that ptdump has
1848 	 * yet not started walking the pagetables - the sequence of barriers
1849 	 * issued by __flush_tlb_kernel_pgtable() guarantees that ptdump will
1850 	 * observe an empty PUD.
1851 	 */
1852 	pud_clear(pudp);
1853 	__flush_tlb_kernel_pgtable(addr);
1854 	if (static_branch_unlikely(&arm64_ptdump_lock_key)) {
1855 		mmap_read_lock(&init_mm);
1856 		mmap_read_unlock(&init_mm);
1857 	}
1858 
1859 	pmdp = table;
1860 	next = addr;
1861 	end = addr + PUD_SIZE;
1862 	do {
1863 		if (pmd_present(pmdp_get(pmdp)))
1864 			/*
1865 			 * PMD has been isolated, so ptdump won't see it. No
1866 			 * need to acquire init_mm.mmap_lock.
1867 			 */
1868 			__pmd_free_pte_page(pmdp, next, /* acquire_mmap_lock = */ false);
1869 	} while (pmdp++, next += PMD_SIZE, next != end);
1870 
1871 	pmd_free(NULL, table);
1872 	return 1;
1873 }
1874 
1875 #ifdef CONFIG_MEMORY_HOTPLUG
__remove_pgd_mapping(pgd_t * pgdir,unsigned long start,u64 size)1876 static void __remove_pgd_mapping(pgd_t *pgdir, unsigned long start, u64 size)
1877 {
1878 	unsigned long end = start + size;
1879 
1880 	WARN_ON(pgdir != init_mm.pgd);
1881 	WARN_ON((start < PAGE_OFFSET) || (end > PAGE_END));
1882 
1883 	unmap_hotplug_range(start, end, false, NULL);
1884 	free_empty_tables(start, end, PAGE_OFFSET, PAGE_END);
1885 }
1886 
arch_get_mappable_range(void)1887 struct range arch_get_mappable_range(void)
1888 {
1889 	struct range mhp_range;
1890 	phys_addr_t start_linear_pa = __pa(_PAGE_OFFSET(vabits_actual));
1891 	phys_addr_t end_linear_pa = __pa(PAGE_END - 1);
1892 
1893 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
1894 		/*
1895 		 * Check for a wrap, it is possible because of randomized linear
1896 		 * mapping the start physical address is actually bigger than
1897 		 * the end physical address. In this case set start to zero
1898 		 * because [0, end_linear_pa] range must still be able to cover
1899 		 * all addressable physical addresses.
1900 		 */
1901 		if (start_linear_pa > end_linear_pa)
1902 			start_linear_pa = 0;
1903 	}
1904 
1905 	WARN_ON(start_linear_pa > end_linear_pa);
1906 
1907 	/*
1908 	 * Linear mapping region is the range [PAGE_OFFSET..(PAGE_END - 1)]
1909 	 * accommodating both its ends but excluding PAGE_END. Max physical
1910 	 * range which can be mapped inside this linear mapping range, must
1911 	 * also be derived from its end points.
1912 	 */
1913 	mhp_range.start = start_linear_pa;
1914 	mhp_range.end =  end_linear_pa;
1915 
1916 	return mhp_range;
1917 }
1918 
arch_add_memory(int nid,u64 start,u64 size,struct mhp_params * params)1919 int arch_add_memory(int nid, u64 start, u64 size,
1920 		    struct mhp_params *params)
1921 {
1922 	int ret, flags = NO_EXEC_MAPPINGS;
1923 
1924 	VM_BUG_ON(!mhp_range_allowed(start, size, true));
1925 
1926 	if (force_pte_mapping())
1927 		flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
1928 
1929 	__create_pgd_mapping(swapper_pg_dir, start, __phys_to_virt(start),
1930 			     size, params->pgprot, pgd_pgtable_alloc_init_mm,
1931 			     flags);
1932 
1933 	memblock_clear_nomap(start, size);
1934 
1935 	ret = __add_pages(nid, start >> PAGE_SHIFT, size >> PAGE_SHIFT,
1936 			   params);
1937 	if (ret)
1938 		__remove_pgd_mapping(swapper_pg_dir,
1939 				     __phys_to_virt(start), size);
1940 	else {
1941 		/* Address of hotplugged memory can be smaller */
1942 		max_pfn = max(max_pfn, PFN_UP(start + size));
1943 		max_low_pfn = max_pfn;
1944 	}
1945 
1946 	return ret;
1947 }
1948 
arch_remove_memory(u64 start,u64 size,struct vmem_altmap * altmap)1949 void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap)
1950 {
1951 	unsigned long start_pfn = start >> PAGE_SHIFT;
1952 	unsigned long nr_pages = size >> PAGE_SHIFT;
1953 
1954 	__remove_pages(start_pfn, nr_pages, altmap);
1955 	__remove_pgd_mapping(swapper_pg_dir, __phys_to_virt(start), size);
1956 }
1957 
1958 /*
1959  * This memory hotplug notifier helps prevent boot memory from being
1960  * inadvertently removed as it blocks pfn range offlining process in
1961  * __offline_pages(). Hence this prevents both offlining as well as
1962  * removal process for boot memory which is initially always online.
1963  * In future if and when boot memory could be removed, this notifier
1964  * should be dropped and free_hotplug_page_range() should handle any
1965  * reserved pages allocated during boot.
1966  */
prevent_bootmem_remove_notifier(struct notifier_block * nb,unsigned long action,void * data)1967 static int prevent_bootmem_remove_notifier(struct notifier_block *nb,
1968 					   unsigned long action, void *data)
1969 {
1970 	struct mem_section *ms;
1971 	struct memory_notify *arg = data;
1972 	unsigned long end_pfn = arg->start_pfn + arg->nr_pages;
1973 	unsigned long pfn = arg->start_pfn;
1974 
1975 	if ((action != MEM_GOING_OFFLINE) && (action != MEM_OFFLINE))
1976 		return NOTIFY_OK;
1977 
1978 	for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1979 		unsigned long start = PFN_PHYS(pfn);
1980 		unsigned long end = start + (1UL << PA_SECTION_SHIFT);
1981 
1982 		ms = __pfn_to_section(pfn);
1983 		if (!early_section(ms))
1984 			continue;
1985 
1986 		if (action == MEM_GOING_OFFLINE) {
1987 			/*
1988 			 * Boot memory removal is not supported. Prevent
1989 			 * it via blocking any attempted offline request
1990 			 * for the boot memory and just report it.
1991 			 */
1992 			pr_warn("Boot memory [%lx %lx] offlining attempted\n", start, end);
1993 			return NOTIFY_BAD;
1994 		} else if (action == MEM_OFFLINE) {
1995 			/*
1996 			 * This should have never happened. Boot memory
1997 			 * offlining should have been prevented by this
1998 			 * very notifier. Probably some memory removal
1999 			 * procedure might have changed which would then
2000 			 * require further debug.
2001 			 */
2002 			pr_err("Boot memory [%lx %lx] offlined\n", start, end);
2003 
2004 			/*
2005 			 * Core memory hotplug does not process a return
2006 			 * code from the notifier for MEM_OFFLINE events.
2007 			 * The error condition has been reported. Return
2008 			 * from here as if ignored.
2009 			 */
2010 			return NOTIFY_DONE;
2011 		}
2012 	}
2013 	return NOTIFY_OK;
2014 }
2015 
2016 static struct notifier_block prevent_bootmem_remove_nb = {
2017 	.notifier_call = prevent_bootmem_remove_notifier,
2018 };
2019 
2020 /*
2021  * This ensures that boot memory sections on the platform are online
2022  * from early boot. Memory sections could not be prevented from being
2023  * offlined, unless for some reason they are not online to begin with.
2024  * This helps validate the basic assumption on which the above memory
2025  * event notifier works to prevent boot memory section offlining and
2026  * its possible removal.
2027  */
validate_bootmem_online(void)2028 static void validate_bootmem_online(void)
2029 {
2030 	phys_addr_t start, end, addr;
2031 	struct mem_section *ms;
2032 	u64 i;
2033 
2034 	/*
2035 	 * Scanning across all memblock might be expensive
2036 	 * on some big memory systems. Hence enable this
2037 	 * validation only with DEBUG_VM.
2038 	 */
2039 	if (!IS_ENABLED(CONFIG_DEBUG_VM))
2040 		return;
2041 
2042 	for_each_mem_range(i, &start, &end) {
2043 		for (addr = start; addr < end; addr += (1UL << PA_SECTION_SHIFT)) {
2044 			ms = __pfn_to_section(PHYS_PFN(addr));
2045 
2046 			/*
2047 			 * All memory ranges in the system at this point
2048 			 * should have been marked as early sections.
2049 			 */
2050 			WARN_ON(!early_section(ms));
2051 
2052 			/*
2053 			 * Memory notifier mechanism here to prevent boot
2054 			 * memory offlining depends on the fact that each
2055 			 * early section memory on the system is initially
2056 			 * online. Otherwise a given memory section which
2057 			 * is already offline will be overlooked and can
2058 			 * be removed completely. Call out such sections.
2059 			 */
2060 			if (!online_section(ms))
2061 				pr_err("Boot memory [%llx %llx] is offline, can be removed\n",
2062 					addr, addr + (1UL << PA_SECTION_SHIFT));
2063 		}
2064 	}
2065 }
2066 
prevent_bootmem_remove_init(void)2067 static int __init prevent_bootmem_remove_init(void)
2068 {
2069 	int ret = 0;
2070 
2071 	if (!IS_ENABLED(CONFIG_MEMORY_HOTREMOVE))
2072 		return ret;
2073 
2074 	validate_bootmem_online();
2075 	ret = register_memory_notifier(&prevent_bootmem_remove_nb);
2076 	if (ret)
2077 		pr_err("%s: Notifier registration failed %d\n", __func__, ret);
2078 
2079 	return ret;
2080 }
2081 early_initcall(prevent_bootmem_remove_init);
2082 #endif
2083 
modify_prot_start_ptes(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep,unsigned int nr)2084 pte_t modify_prot_start_ptes(struct vm_area_struct *vma, unsigned long addr,
2085 			     pte_t *ptep, unsigned int nr)
2086 {
2087 	pte_t pte = get_and_clear_ptes(vma->vm_mm, addr, ptep, nr);
2088 
2089 	if (alternative_has_cap_unlikely(ARM64_WORKAROUND_2645198)) {
2090 		/*
2091 		 * Break-before-make (BBM) is required for all user space mappings
2092 		 * when the permission changes from executable to non-executable
2093 		 * in cases where cpu is affected with errata #2645198.
2094 		 */
2095 		if (pte_accessible(vma->vm_mm, pte) && pte_user_exec(pte))
2096 			__flush_tlb_range(vma, addr, nr * PAGE_SIZE,
2097 					  PAGE_SIZE, true, 3);
2098 	}
2099 
2100 	return pte;
2101 }
2102 
ptep_modify_prot_start(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep)2103 pte_t ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep)
2104 {
2105 	return modify_prot_start_ptes(vma, addr, ptep, 1);
2106 }
2107 
modify_prot_commit_ptes(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep,pte_t old_pte,pte_t pte,unsigned int nr)2108 void modify_prot_commit_ptes(struct vm_area_struct *vma, unsigned long addr,
2109 			     pte_t *ptep, pte_t old_pte, pte_t pte,
2110 			     unsigned int nr)
2111 {
2112 	set_ptes(vma->vm_mm, addr, ptep, pte, nr);
2113 }
2114 
ptep_modify_prot_commit(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep,pte_t old_pte,pte_t pte)2115 void ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep,
2116 			     pte_t old_pte, pte_t pte)
2117 {
2118 	modify_prot_commit_ptes(vma, addr, ptep, old_pte, pte, 1);
2119 }
2120 
2121 /*
2122  * Atomically replaces the active TTBR1_EL1 PGD with a new VA-compatible PGD,
2123  * avoiding the possibility of conflicting TLB entries being allocated.
2124  */
__cpu_replace_ttbr1(pgd_t * pgdp,bool cnp)2125 void __cpu_replace_ttbr1(pgd_t *pgdp, bool cnp)
2126 {
2127 	typedef void (ttbr_replace_func)(phys_addr_t);
2128 	extern ttbr_replace_func idmap_cpu_replace_ttbr1;
2129 	ttbr_replace_func *replace_phys;
2130 	unsigned long daif;
2131 
2132 	/* phys_to_ttbr() zeros lower 2 bits of ttbr with 52-bit PA */
2133 	phys_addr_t ttbr1 = phys_to_ttbr(virt_to_phys(pgdp));
2134 
2135 	if (cnp)
2136 		ttbr1 |= TTBR_CNP_BIT;
2137 
2138 	replace_phys = (void *)__pa_symbol(idmap_cpu_replace_ttbr1);
2139 
2140 	cpu_install_idmap();
2141 
2142 	/*
2143 	 * We really don't want to take *any* exceptions while TTBR1 is
2144 	 * in the process of being replaced so mask everything.
2145 	 */
2146 	daif = local_daif_save();
2147 	replace_phys(ttbr1);
2148 	local_daif_restore(daif);
2149 
2150 	cpu_uninstall_idmap();
2151 }
2152 
2153 #ifdef CONFIG_ARCH_HAS_PKEYS
arch_set_user_pkey_access(struct task_struct * tsk,int pkey,unsigned long init_val)2154 int arch_set_user_pkey_access(struct task_struct *tsk, int pkey, unsigned long init_val)
2155 {
2156 	u64 new_por;
2157 	u64 old_por;
2158 
2159 	if (!system_supports_poe())
2160 		return -ENOSPC;
2161 
2162 	/*
2163 	 * This code should only be called with valid 'pkey'
2164 	 * values originating from in-kernel users.  Complain
2165 	 * if a bad value is observed.
2166 	 */
2167 	if (WARN_ON_ONCE(pkey >= arch_max_pkey()))
2168 		return -EINVAL;
2169 
2170 	/* Set the bits we need in POR:  */
2171 	new_por = POE_RWX;
2172 	if (init_val & PKEY_DISABLE_WRITE)
2173 		new_por &= ~POE_W;
2174 	if (init_val & PKEY_DISABLE_ACCESS)
2175 		new_por &= ~POE_RW;
2176 	if (init_val & PKEY_DISABLE_READ)
2177 		new_por &= ~POE_R;
2178 	if (init_val & PKEY_DISABLE_EXECUTE)
2179 		new_por &= ~POE_X;
2180 
2181 	/* Shift the bits in to the correct place in POR for pkey: */
2182 	new_por = POR_ELx_PERM_PREP(pkey, new_por);
2183 
2184 	/* Get old POR and mask off any old bits in place: */
2185 	old_por = read_sysreg_s(SYS_POR_EL0);
2186 	old_por &= ~(POE_MASK << POR_ELx_PERM_SHIFT(pkey));
2187 
2188 	/* Write old part along with new part: */
2189 	write_sysreg_s(old_por | new_por, SYS_POR_EL0);
2190 
2191 	return 0;
2192 }
2193 #endif
2194