1 /*
2 * Based on arch/arm/mm/mmu.c
3 *
4 * Copyright (C) 1995-2005 Russell King
5 * Copyright (C) 2012 ARM Ltd.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <linux/export.h>
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/libfdt.h>
25 #include <linux/mman.h>
26 #include <linux/nodemask.h>
27 #include <linux/memblock.h>
28 #include <linux/fs.h>
29 #include <linux/io.h>
30 #include <linux/slab.h>
31 #include <linux/stop_machine.h>
32 #include <linux/mm.h>
33
34 #include <asm/barrier.h>
35 #include <asm/cputype.h>
36 #include <asm/fixmap.h>
37 #include <asm/kasan.h>
38 #include <asm/kernel-pgtable.h>
39 #include <asm/sections.h>
40 #include <asm/setup.h>
41 #include <asm/sizes.h>
42 #include <asm/tlb.h>
43 #include <asm/memblock.h>
44 #include <asm/mmu_context.h>
45
46 #include "mm.h"
47
48 u64 idmap_t0sz = TCR_T0SZ(VA_BITS);
49
50 u64 kimage_voffset __read_mostly;
51 EXPORT_SYMBOL(kimage_voffset);
52
53 /*
54 * Empty_zero_page is a special page that is used for zero-initialized data
55 * and COW.
56 */
57 unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
58 EXPORT_SYMBOL(empty_zero_page);
59
60 static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss;
61 static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss __maybe_unused;
62 static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss __maybe_unused;
63
phys_mem_access_prot(struct file * file,unsigned long pfn,unsigned long size,pgprot_t vma_prot)64 pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
65 unsigned long size, pgprot_t vma_prot)
66 {
67 if (!pfn_valid(pfn))
68 return pgprot_noncached(vma_prot);
69 else if (file->f_flags & O_SYNC)
70 return pgprot_writecombine(vma_prot);
71 return vma_prot;
72 }
73 EXPORT_SYMBOL(phys_mem_access_prot);
74
early_pgtable_alloc(void)75 static phys_addr_t __init early_pgtable_alloc(void)
76 {
77 phys_addr_t phys;
78 void *ptr;
79
80 phys = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
81 BUG_ON(!phys);
82
83 /*
84 * The FIX_{PGD,PUD,PMD} slots may be in active use, but the FIX_PTE
85 * slot will be free, so we can (ab)use the FIX_PTE slot to initialise
86 * any level of table.
87 */
88 ptr = pte_set_fixmap(phys);
89
90 memset(ptr, 0, PAGE_SIZE);
91
92 /*
93 * Implicit barriers also ensure the zeroed page is visible to the page
94 * table walker
95 */
96 pte_clear_fixmap();
97
98 return phys;
99 }
100
101 /*
102 * remap a PMD into pages
103 */
split_pmd(pmd_t * pmd,pte_t * pte)104 static void split_pmd(pmd_t *pmd, pte_t *pte)
105 {
106 unsigned long pfn = pmd_pfn(*pmd);
107 int i = 0;
108
109 do {
110 /*
111 * Need to have the least restrictive permissions available
112 * permissions will be fixed up later
113 */
114 set_pte(pte, pfn_pte(pfn, PAGE_KERNEL_EXEC));
115 pfn++;
116 } while (pte++, i++, i < PTRS_PER_PTE);
117 }
118
alloc_init_pte(pmd_t * pmd,unsigned long addr,unsigned long end,unsigned long pfn,pgprot_t prot,phys_addr_t (* pgtable_alloc)(void))119 static void alloc_init_pte(pmd_t *pmd, unsigned long addr,
120 unsigned long end, unsigned long pfn,
121 pgprot_t prot,
122 phys_addr_t (*pgtable_alloc)(void))
123 {
124 pte_t *pte;
125
126 if (pmd_none(*pmd) || pmd_sect(*pmd)) {
127 phys_addr_t pte_phys;
128 BUG_ON(!pgtable_alloc);
129 pte_phys = pgtable_alloc();
130 pte = pte_set_fixmap(pte_phys);
131 if (pmd_sect(*pmd))
132 split_pmd(pmd, pte);
133 __pmd_populate(pmd, pte_phys, PMD_TYPE_TABLE);
134 flush_tlb_all();
135 pte_clear_fixmap();
136 }
137 BUG_ON(pmd_bad(*pmd));
138
139 pte = pte_set_fixmap_offset(pmd, addr);
140 do {
141 set_pte(pte, pfn_pte(pfn, prot));
142 pfn++;
143 } while (pte++, addr += PAGE_SIZE, addr != end);
144
145 pte_clear_fixmap();
146 }
147
split_pud(pud_t * old_pud,pmd_t * pmd)148 static void split_pud(pud_t *old_pud, pmd_t *pmd)
149 {
150 unsigned long addr = pud_pfn(*old_pud) << PAGE_SHIFT;
151 pgprot_t prot = __pgprot(pud_val(*old_pud) ^ addr);
152 int i = 0;
153
154 do {
155 set_pmd(pmd, __pmd(addr | pgprot_val(prot)));
156 addr += PMD_SIZE;
157 } while (pmd++, i++, i < PTRS_PER_PMD);
158 }
159
160 #ifdef CONFIG_DEBUG_PAGEALLOC
block_mappings_allowed(phys_addr_t (* pgtable_alloc)(void))161 static bool block_mappings_allowed(phys_addr_t (*pgtable_alloc)(void))
162 {
163
164 /*
165 * If debug_page_alloc is enabled we must map the linear map
166 * using pages. However, other mappings created by
167 * create_mapping_noalloc must use sections in some cases. Allow
168 * sections to be used in those cases, where no pgtable_alloc
169 * function is provided.
170 */
171 return !pgtable_alloc || !debug_pagealloc_enabled();
172 }
173 #else
block_mappings_allowed(phys_addr_t (* pgtable_alloc)(void))174 static bool block_mappings_allowed(phys_addr_t (*pgtable_alloc)(void))
175 {
176 return true;
177 }
178 #endif
179
alloc_init_pmd(pud_t * pud,unsigned long addr,unsigned long end,phys_addr_t phys,pgprot_t prot,phys_addr_t (* pgtable_alloc)(void))180 static void alloc_init_pmd(pud_t *pud, unsigned long addr, unsigned long end,
181 phys_addr_t phys, pgprot_t prot,
182 phys_addr_t (*pgtable_alloc)(void))
183 {
184 pmd_t *pmd;
185 unsigned long next;
186
187 /*
188 * Check for initial section mappings in the pgd/pud and remove them.
189 */
190 if (pud_none(*pud) || pud_sect(*pud)) {
191 phys_addr_t pmd_phys;
192 BUG_ON(!pgtable_alloc);
193 pmd_phys = pgtable_alloc();
194 pmd = pmd_set_fixmap(pmd_phys);
195 if (pud_sect(*pud)) {
196 /*
197 * need to have the 1G of mappings continue to be
198 * present
199 */
200 split_pud(pud, pmd);
201 }
202 __pud_populate(pud, pmd_phys, PUD_TYPE_TABLE);
203 flush_tlb_all();
204 pmd_clear_fixmap();
205 }
206 BUG_ON(pud_bad(*pud));
207
208 pmd = pmd_set_fixmap_offset(pud, addr);
209 do {
210 next = pmd_addr_end(addr, end);
211 /* try section mapping first */
212 if (((addr | next | phys) & ~SECTION_MASK) == 0 &&
213 block_mappings_allowed(pgtable_alloc)) {
214 pmd_t old_pmd =*pmd;
215 pmd_set_huge(pmd, phys, prot);
216 /*
217 * Check for previous table entries created during
218 * boot (__create_page_tables) and flush them.
219 */
220 if (!pmd_none(old_pmd)) {
221 flush_tlb_all();
222 if (pmd_table(old_pmd)) {
223 phys_addr_t table = pmd_page_paddr(old_pmd);
224 if (!WARN_ON_ONCE(slab_is_available()))
225 memblock_free(table, PAGE_SIZE);
226 }
227 }
228 } else {
229 alloc_init_pte(pmd, addr, next, __phys_to_pfn(phys),
230 prot, pgtable_alloc);
231 }
232 phys += next - addr;
233 } while (pmd++, addr = next, addr != end);
234
235 pmd_clear_fixmap();
236 }
237
use_1G_block(unsigned long addr,unsigned long next,unsigned long phys)238 static inline bool use_1G_block(unsigned long addr, unsigned long next,
239 unsigned long phys)
240 {
241 if (PAGE_SHIFT != 12)
242 return false;
243
244 if (((addr | next | phys) & ~PUD_MASK) != 0)
245 return false;
246
247 return true;
248 }
249
alloc_init_pud(pgd_t * pgd,unsigned long addr,unsigned long end,phys_addr_t phys,pgprot_t prot,phys_addr_t (* pgtable_alloc)(void))250 static void alloc_init_pud(pgd_t *pgd, unsigned long addr, unsigned long end,
251 phys_addr_t phys, pgprot_t prot,
252 phys_addr_t (*pgtable_alloc)(void))
253 {
254 pud_t *pud;
255 unsigned long next;
256
257 if (pgd_none(*pgd)) {
258 phys_addr_t pud_phys;
259 BUG_ON(!pgtable_alloc);
260 pud_phys = pgtable_alloc();
261 __pgd_populate(pgd, pud_phys, PUD_TYPE_TABLE);
262 }
263 BUG_ON(pgd_bad(*pgd));
264
265 pud = pud_set_fixmap_offset(pgd, addr);
266 do {
267 next = pud_addr_end(addr, end);
268
269 /*
270 * For 4K granule only, attempt to put down a 1GB block
271 */
272 if (use_1G_block(addr, next, phys) &&
273 block_mappings_allowed(pgtable_alloc)) {
274 pud_t old_pud = *pud;
275 pud_set_huge(pud, phys, prot);
276
277 /*
278 * If we have an old value for a pud, it will
279 * be pointing to a pmd table that we no longer
280 * need (from swapper_pg_dir).
281 *
282 * Look up the old pmd table and free it.
283 */
284 if (!pud_none(old_pud)) {
285 flush_tlb_all();
286 if (pud_table(old_pud)) {
287 phys_addr_t table = pud_page_paddr(old_pud);
288 if (!WARN_ON_ONCE(slab_is_available()))
289 memblock_free(table, PAGE_SIZE);
290 }
291 }
292 } else {
293 alloc_init_pmd(pud, addr, next, phys, prot,
294 pgtable_alloc);
295 }
296 phys += next - addr;
297 } while (pud++, addr = next, addr != end);
298
299 pud_clear_fixmap();
300 }
301
302 /*
303 * Create the page directory entries and any necessary page tables for the
304 * mapping specified by 'md'.
305 */
init_pgd(pgd_t * pgd,phys_addr_t phys,unsigned long virt,phys_addr_t size,pgprot_t prot,phys_addr_t (* pgtable_alloc)(void))306 static void init_pgd(pgd_t *pgd, phys_addr_t phys, unsigned long virt,
307 phys_addr_t size, pgprot_t prot,
308 phys_addr_t (*pgtable_alloc)(void))
309 {
310 unsigned long addr, length, end, next;
311
312 /*
313 * If the virtual and physical address don't have the same offset
314 * within a page, we cannot map the region as the caller expects.
315 */
316 if (WARN_ON((phys ^ virt) & ~PAGE_MASK))
317 return;
318
319 phys &= PAGE_MASK;
320 addr = virt & PAGE_MASK;
321 length = PAGE_ALIGN(size + (virt & ~PAGE_MASK));
322
323 end = addr + length;
324 do {
325 next = pgd_addr_end(addr, end);
326 alloc_init_pud(pgd, addr, next, phys, prot, pgtable_alloc);
327 phys += next - addr;
328 } while (pgd++, addr = next, addr != end);
329 }
330
late_pgtable_alloc(void)331 static phys_addr_t late_pgtable_alloc(void)
332 {
333 void *ptr = (void *)__get_free_page(PGALLOC_GFP);
334 BUG_ON(!ptr);
335
336 /* Ensure the zeroed page is visible to the page table walker */
337 dsb(ishst);
338 return __pa(ptr);
339 }
340
__create_pgd_mapping(pgd_t * pgdir,phys_addr_t phys,unsigned long virt,phys_addr_t size,pgprot_t prot,phys_addr_t (* alloc)(void))341 static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys,
342 unsigned long virt, phys_addr_t size,
343 pgprot_t prot,
344 phys_addr_t (*alloc)(void))
345 {
346 init_pgd(pgd_offset_raw(pgdir, virt), phys, virt, size, prot, alloc);
347 }
348
349 /*
350 * This function can only be used to modify existing table entries,
351 * without allocating new levels of table. Note that this permits the
352 * creation of new section or page entries.
353 */
create_mapping_noalloc(phys_addr_t phys,unsigned long virt,phys_addr_t size,pgprot_t prot)354 static void __init create_mapping_noalloc(phys_addr_t phys, unsigned long virt,
355 phys_addr_t size, pgprot_t prot)
356 {
357 if (virt < VMALLOC_START) {
358 pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
359 &phys, virt);
360 return;
361 }
362 __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot,
363 NULL);
364 }
365
create_pgd_mapping(struct mm_struct * mm,phys_addr_t phys,unsigned long virt,phys_addr_t size,pgprot_t prot)366 void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys,
367 unsigned long virt, phys_addr_t size,
368 pgprot_t prot)
369 {
370 __create_pgd_mapping(mm->pgd, phys, virt, size, prot,
371 late_pgtable_alloc);
372 }
373
create_mapping_late(phys_addr_t phys,unsigned long virt,phys_addr_t size,pgprot_t prot)374 static void create_mapping_late(phys_addr_t phys, unsigned long virt,
375 phys_addr_t size, pgprot_t prot)
376 {
377 if (virt < VMALLOC_START) {
378 pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
379 &phys, virt);
380 return;
381 }
382
383 __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot,
384 late_pgtable_alloc);
385 }
386
__map_memblock(pgd_t * pgd,phys_addr_t start,phys_addr_t end)387 static void __init __map_memblock(pgd_t *pgd, phys_addr_t start, phys_addr_t end)
388 {
389 unsigned long kernel_start = __pa_symbol(_stext);
390 unsigned long kernel_end = __pa_symbol(__init_begin);
391
392 /*
393 * Take care not to create a writable alias for the
394 * read-only text and rodata sections of the kernel image.
395 */
396
397 /* No overlap with the kernel text/rodata */
398 if (end < kernel_start || start >= kernel_end) {
399 __create_pgd_mapping(pgd, start, __phys_to_virt(start),
400 end - start, PAGE_KERNEL,
401 early_pgtable_alloc);
402 return;
403 }
404
405 /*
406 * This block overlaps the kernel text/rodata mapping.
407 * Map the portion(s) which don't overlap.
408 */
409 if (start < kernel_start)
410 __create_pgd_mapping(pgd, start,
411 __phys_to_virt(start),
412 kernel_start - start, PAGE_KERNEL,
413 early_pgtable_alloc);
414 if (kernel_end < end)
415 __create_pgd_mapping(pgd, kernel_end,
416 __phys_to_virt(kernel_end),
417 end - kernel_end, PAGE_KERNEL,
418 early_pgtable_alloc);
419
420 /*
421 * Map the linear alias of the [_stext, __init_begin) interval as
422 * read-only/non-executable. This makes the contents of the
423 * region accessible to subsystems such as hibernate, but
424 * protects it from inadvertent modification or execution.
425 */
426 __create_pgd_mapping(pgd, kernel_start, __phys_to_virt(kernel_start),
427 kernel_end - kernel_start, PAGE_KERNEL_RO,
428 early_pgtable_alloc);
429 }
430
map_mem(pgd_t * pgd)431 static void __init map_mem(pgd_t *pgd)
432 {
433 struct memblock_region *reg;
434
435 /* map all the memory banks */
436 for_each_memblock(memory, reg) {
437 phys_addr_t start = reg->base;
438 phys_addr_t end = start + reg->size;
439
440 if (start >= end)
441 break;
442 if (memblock_is_nomap(reg))
443 continue;
444
445 __map_memblock(pgd, start, end);
446 }
447 }
448
mark_rodata_ro(void)449 void mark_rodata_ro(void)
450 {
451 unsigned long section_size;
452
453 section_size = (unsigned long)_etext - (unsigned long)_stext;
454 create_mapping_late(__pa_symbol(_stext), (unsigned long)_stext,
455 section_size, PAGE_KERNEL_ROX);
456 /*
457 * mark .rodata as read only. Use __init_begin rather than __end_rodata
458 * to cover NOTES and EXCEPTION_TABLE.
459 */
460 section_size = (unsigned long)__init_begin - (unsigned long)__start_rodata;
461 create_mapping_late(__pa_symbol(__start_rodata),
462 (unsigned long)__start_rodata,
463 section_size, PAGE_KERNEL_RO);
464 }
465
fixup_init(void)466 void fixup_init(void)
467 {
468 /*
469 * Unmap the __init region but leave the VM area in place. This
470 * prevents the region from being reused for kernel modules, which
471 * is not supported by kallsyms.
472 */
473 unmap_kernel_range((u64)__init_begin, (u64)(__init_end - __init_begin));
474 }
475
map_kernel_chunk(pgd_t * pgd,void * va_start,void * va_end,pgprot_t prot,struct vm_struct * vma)476 static void __init map_kernel_chunk(pgd_t *pgd, void *va_start, void *va_end,
477 pgprot_t prot, struct vm_struct *vma)
478 {
479 phys_addr_t pa_start = __pa_symbol(va_start);
480 unsigned long size = va_end - va_start;
481
482 BUG_ON(!PAGE_ALIGNED(pa_start));
483 BUG_ON(!PAGE_ALIGNED(size));
484
485 __create_pgd_mapping(pgd, pa_start, (unsigned long)va_start, size, prot,
486 early_pgtable_alloc);
487
488 vma->addr = va_start;
489 vma->phys_addr = pa_start;
490 vma->size = size;
491 vma->flags = VM_MAP;
492 vma->caller = __builtin_return_address(0);
493
494 vm_area_add_early(vma);
495 }
496
497 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
map_entry_trampoline(void)498 static int __init map_entry_trampoline(void)
499 {
500 extern char __entry_tramp_text_start[];
501
502 pgprot_t prot = PAGE_KERNEL_ROX;
503 phys_addr_t pa_start = __pa_symbol(__entry_tramp_text_start);
504
505 /* The trampoline is always mapped and can therefore be global */
506 pgprot_val(prot) &= ~PTE_NG;
507
508 /* Map only the text into the trampoline page table */
509 memset(tramp_pg_dir, 0, PGD_SIZE);
510 __create_pgd_mapping(tramp_pg_dir, pa_start, TRAMP_VALIAS, PAGE_SIZE,
511 prot, late_pgtable_alloc);
512
513 /* Map both the text and data into the kernel page table */
514 __set_fixmap(FIX_ENTRY_TRAMP_TEXT, pa_start, prot);
515 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
516 extern char __entry_tramp_data_start[];
517
518 __set_fixmap(FIX_ENTRY_TRAMP_DATA,
519 __pa_symbol(__entry_tramp_data_start),
520 PAGE_KERNEL_RO);
521 }
522
523 return 0;
524 }
525 core_initcall(map_entry_trampoline);
526 #endif
527
528 /*
529 * Create fine-grained mappings for the kernel.
530 */
map_kernel(pgd_t * pgd)531 static void __init map_kernel(pgd_t *pgd)
532 {
533 static struct vm_struct vmlinux_text, vmlinux_rodata, vmlinux_init, vmlinux_data;
534
535 map_kernel_chunk(pgd, _stext, _etext, PAGE_KERNEL_EXEC, &vmlinux_text);
536 map_kernel_chunk(pgd, __start_rodata, __init_begin, PAGE_KERNEL, &vmlinux_rodata);
537 map_kernel_chunk(pgd, __init_begin, __init_end, PAGE_KERNEL_EXEC,
538 &vmlinux_init);
539 map_kernel_chunk(pgd, _data, _end, PAGE_KERNEL, &vmlinux_data);
540
541 if (!pgd_val(*pgd_offset_raw(pgd, FIXADDR_START))) {
542 /*
543 * The fixmap falls in a separate pgd to the kernel, and doesn't
544 * live in the carveout for the swapper_pg_dir. We can simply
545 * re-use the existing dir for the fixmap.
546 */
547 set_pgd(pgd_offset_raw(pgd, FIXADDR_START),
548 *pgd_offset_k(FIXADDR_START));
549 } else if (CONFIG_PGTABLE_LEVELS > 3) {
550 /*
551 * The fixmap shares its top level pgd entry with the kernel
552 * mapping. This can really only occur when we are running
553 * with 16k/4 levels, so we can simply reuse the pud level
554 * entry instead.
555 */
556 BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
557 set_pud(pud_set_fixmap_offset(pgd, FIXADDR_START),
558 __pud(__pa_symbol(bm_pmd) | PUD_TYPE_TABLE));
559 pud_clear_fixmap();
560 } else {
561 BUG();
562 }
563
564 kasan_copy_shadow(pgd);
565 }
566
567 /*
568 * paging_init() sets up the page tables, initialises the zone memory
569 * maps and sets up the zero page.
570 */
paging_init(void)571 void __init paging_init(void)
572 {
573 phys_addr_t pgd_phys = early_pgtable_alloc();
574 pgd_t *pgd = pgd_set_fixmap(pgd_phys);
575
576 map_kernel(pgd);
577 map_mem(pgd);
578
579 /*
580 * We want to reuse the original swapper_pg_dir so we don't have to
581 * communicate the new address to non-coherent secondaries in
582 * secondary_entry, and so cpu_switch_mm can generate the address with
583 * adrp+add rather than a load from some global variable.
584 *
585 * To do this we need to go via a temporary pgd.
586 */
587 cpu_replace_ttbr1(__va(pgd_phys));
588 memcpy(swapper_pg_dir, pgd, PAGE_SIZE);
589 cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
590
591 pgd_clear_fixmap();
592 memblock_free(pgd_phys, PAGE_SIZE);
593
594 /* Ensure the zero page is visible to the page table walker */
595 dsb(ishst);
596
597 /*
598 * We only reuse the PGD from the swapper_pg_dir, not the pud + pmd
599 * allocated with it.
600 */
601 memblock_free(__pa_symbol(swapper_pg_dir) + PAGE_SIZE,
602 SWAPPER_DIR_SIZE - PAGE_SIZE);
603
604 bootmem_init();
605 }
606
607 /*
608 * Check whether a kernel address is valid (derived from arch/x86/).
609 */
kern_addr_valid(unsigned long addr)610 int kern_addr_valid(unsigned long addr)
611 {
612 pgd_t *pgd;
613 pud_t *pud;
614 pmd_t *pmd;
615 pte_t *pte;
616
617 if ((((long)addr) >> VA_BITS) != -1UL)
618 return 0;
619
620 pgd = pgd_offset_k(addr);
621 if (pgd_none(*pgd))
622 return 0;
623
624 pud = pud_offset(pgd, addr);
625 if (pud_none(*pud))
626 return 0;
627
628 if (pud_sect(*pud))
629 return pfn_valid(pud_pfn(*pud));
630
631 pmd = pmd_offset(pud, addr);
632 if (pmd_none(*pmd))
633 return 0;
634
635 if (pmd_sect(*pmd))
636 return pfn_valid(pmd_pfn(*pmd));
637
638 pte = pte_offset_kernel(pmd, addr);
639 if (pte_none(*pte))
640 return 0;
641
642 return pfn_valid(pte_pfn(*pte));
643 }
644 #ifdef CONFIG_SPARSEMEM_VMEMMAP
645 #if !ARM64_SWAPPER_USES_SECTION_MAPS
vmemmap_populate(unsigned long start,unsigned long end,int node)646 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
647 {
648 return vmemmap_populate_basepages(start, end, node);
649 }
650 #else /* !ARM64_SWAPPER_USES_SECTION_MAPS */
vmemmap_populate(unsigned long start,unsigned long end,int node)651 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
652 {
653 unsigned long addr = start;
654 unsigned long next;
655 pgd_t *pgd;
656 pud_t *pud;
657 pmd_t *pmd;
658
659 do {
660 next = pmd_addr_end(addr, end);
661
662 pgd = vmemmap_pgd_populate(addr, node);
663 if (!pgd)
664 return -ENOMEM;
665
666 pud = vmemmap_pud_populate(pgd, addr, node);
667 if (!pud)
668 return -ENOMEM;
669
670 pmd = pmd_offset(pud, addr);
671 if (pmd_none(*pmd)) {
672 void *p = NULL;
673
674 p = vmemmap_alloc_block_buf(PMD_SIZE, node);
675 if (!p)
676 return -ENOMEM;
677
678 set_pmd(pmd, __pmd(__pa(p) | PROT_SECT_NORMAL));
679 } else
680 vmemmap_verify((pte_t *)pmd, node, addr, next);
681 } while (addr = next, addr != end);
682
683 return 0;
684 }
685 #endif /* CONFIG_ARM64_64K_PAGES */
vmemmap_free(unsigned long start,unsigned long end)686 void vmemmap_free(unsigned long start, unsigned long end)
687 {
688 }
689 #endif /* CONFIG_SPARSEMEM_VMEMMAP */
690
fixmap_pud(unsigned long addr)691 static inline pud_t * fixmap_pud(unsigned long addr)
692 {
693 pgd_t *pgd = pgd_offset_k(addr);
694
695 BUG_ON(pgd_none(*pgd) || pgd_bad(*pgd));
696
697 return pud_offset_kimg(pgd, addr);
698 }
699
fixmap_pmd(unsigned long addr)700 static inline pmd_t * fixmap_pmd(unsigned long addr)
701 {
702 pud_t *pud = fixmap_pud(addr);
703
704 BUG_ON(pud_none(*pud) || pud_bad(*pud));
705
706 return pmd_offset_kimg(pud, addr);
707 }
708
fixmap_pte(unsigned long addr)709 static inline pte_t * fixmap_pte(unsigned long addr)
710 {
711 return &bm_pte[pte_index(addr)];
712 }
713
714 /*
715 * The p*d_populate functions call virt_to_phys implicitly so they can't be used
716 * directly on kernel symbols (bm_p*d). This function is called too early to use
717 * lm_alias so __p*d_populate functions must be used to populate with the
718 * physical address from __pa_symbol.
719 */
early_fixmap_init(void)720 void __init early_fixmap_init(void)
721 {
722 pgd_t *pgd;
723 pud_t *pud;
724 pmd_t *pmd;
725 unsigned long addr = FIXADDR_START;
726
727 pgd = pgd_offset_k(addr);
728 if (CONFIG_PGTABLE_LEVELS > 3 &&
729 !(pgd_none(*pgd) || pgd_page_paddr(*pgd) == __pa_symbol(bm_pud))) {
730 /*
731 * We only end up here if the kernel mapping and the fixmap
732 * share the top level pgd entry, which should only happen on
733 * 16k/4 levels configurations.
734 */
735 BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
736 pud = pud_offset_kimg(pgd, addr);
737 } else {
738 if (pgd_none(*pgd))
739 __pgd_populate(pgd, __pa_symbol(bm_pud),
740 PUD_TYPE_TABLE);
741 pud = fixmap_pud(addr);
742 }
743 if (pud_none(*pud))
744 __pud_populate(pud, __pa_symbol(bm_pmd), PMD_TYPE_TABLE);
745 pmd = fixmap_pmd(addr);
746 __pmd_populate(pmd, __pa_symbol(bm_pte), PMD_TYPE_TABLE);
747
748 /*
749 * The boot-ioremap range spans multiple pmds, for which
750 * we are not prepared:
751 */
752 BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
753 != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
754
755 if ((pmd != fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)))
756 || pmd != fixmap_pmd(fix_to_virt(FIX_BTMAP_END))) {
757 WARN_ON(1);
758 pr_warn("pmd %p != %p, %p\n",
759 pmd, fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)),
760 fixmap_pmd(fix_to_virt(FIX_BTMAP_END)));
761 pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
762 fix_to_virt(FIX_BTMAP_BEGIN));
763 pr_warn("fix_to_virt(FIX_BTMAP_END): %08lx\n",
764 fix_to_virt(FIX_BTMAP_END));
765
766 pr_warn("FIX_BTMAP_END: %d\n", FIX_BTMAP_END);
767 pr_warn("FIX_BTMAP_BEGIN: %d\n", FIX_BTMAP_BEGIN);
768 }
769 }
770
__set_fixmap(enum fixed_addresses idx,phys_addr_t phys,pgprot_t flags)771 void __set_fixmap(enum fixed_addresses idx,
772 phys_addr_t phys, pgprot_t flags)
773 {
774 unsigned long addr = __fix_to_virt(idx);
775 pte_t *pte;
776
777 BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
778
779 pte = fixmap_pte(addr);
780
781 if (pgprot_val(flags)) {
782 set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
783 } else {
784 pte_clear(&init_mm, addr, pte);
785 flush_tlb_kernel_range(addr, addr+PAGE_SIZE);
786 }
787 }
788
__fixmap_remap_fdt(phys_addr_t dt_phys,int * size,pgprot_t prot)789 void *__init __fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot)
790 {
791 const u64 dt_virt_base = __fix_to_virt(FIX_FDT);
792 int offset;
793 void *dt_virt;
794
795 /*
796 * Check whether the physical FDT address is set and meets the minimum
797 * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be
798 * at least 8 bytes so that we can always access the magic and size
799 * fields of the FDT header after mapping the first chunk, double check
800 * here if that is indeed the case.
801 */
802 BUILD_BUG_ON(MIN_FDT_ALIGN < 8);
803 if (!dt_phys || dt_phys % MIN_FDT_ALIGN)
804 return NULL;
805
806 /*
807 * Make sure that the FDT region can be mapped without the need to
808 * allocate additional translation table pages, so that it is safe
809 * to call create_mapping_noalloc() this early.
810 *
811 * On 64k pages, the FDT will be mapped using PTEs, so we need to
812 * be in the same PMD as the rest of the fixmap.
813 * On 4k pages, we'll use section mappings for the FDT so we only
814 * have to be in the same PUD.
815 */
816 BUILD_BUG_ON(dt_virt_base % SZ_2M);
817
818 BUILD_BUG_ON(__fix_to_virt(FIX_FDT_END) >> SWAPPER_TABLE_SHIFT !=
819 __fix_to_virt(FIX_BTMAP_BEGIN) >> SWAPPER_TABLE_SHIFT);
820
821 offset = dt_phys % SWAPPER_BLOCK_SIZE;
822 dt_virt = (void *)dt_virt_base + offset;
823
824 /* map the first chunk so we can read the size from the header */
825 create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE),
826 dt_virt_base, SWAPPER_BLOCK_SIZE, prot);
827
828 if (fdt_magic(dt_virt) != FDT_MAGIC)
829 return NULL;
830
831 *size = fdt_totalsize(dt_virt);
832 if (*size > MAX_FDT_SIZE)
833 return NULL;
834
835 if (offset + *size > SWAPPER_BLOCK_SIZE)
836 create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE), dt_virt_base,
837 round_up(offset + *size, SWAPPER_BLOCK_SIZE), prot);
838
839 return dt_virt;
840 }
841
fixmap_remap_fdt(phys_addr_t dt_phys)842 void *__init fixmap_remap_fdt(phys_addr_t dt_phys)
843 {
844 void *dt_virt;
845 int size;
846
847 dt_virt = __fixmap_remap_fdt(dt_phys, &size, PAGE_KERNEL_RO);
848 if (!dt_virt)
849 return NULL;
850
851 memblock_reserve(dt_phys, size);
852 return dt_virt;
853 }
854
arch_ioremap_pud_supported(void)855 int __init arch_ioremap_pud_supported(void)
856 {
857 /*
858 * Only 4k granule supports level 1 block mappings.
859 * SW table walks can't handle removal of intermediate entries.
860 */
861 return IS_ENABLED(CONFIG_ARM64_4K_PAGES) &&
862 !IS_ENABLED(CONFIG_ARM64_PTDUMP_DEBUGFS);
863 }
864
arch_ioremap_pmd_supported(void)865 int __init arch_ioremap_pmd_supported(void)
866 {
867 /* See arch_ioremap_pud_supported() */
868 return !IS_ENABLED(CONFIG_ARM64_PTDUMP_DEBUGFS);
869 }
870
pud_set_huge(pud_t * pud,phys_addr_t phys,pgprot_t prot)871 int pud_set_huge(pud_t *pud, phys_addr_t phys, pgprot_t prot)
872 {
873 BUG_ON(phys & ~PUD_MASK);
874 set_pud(pud, __pud(phys | PUD_TYPE_SECT | pgprot_val(mk_sect_prot(prot))));
875 return 1;
876 }
877
pmd_set_huge(pmd_t * pmd,phys_addr_t phys,pgprot_t prot)878 int pmd_set_huge(pmd_t *pmd, phys_addr_t phys, pgprot_t prot)
879 {
880 BUG_ON(phys & ~PMD_MASK);
881 set_pmd(pmd, __pmd(phys | PMD_TYPE_SECT | pgprot_val(mk_sect_prot(prot))));
882 return 1;
883 }
884
pud_clear_huge(pud_t * pud)885 int pud_clear_huge(pud_t *pud)
886 {
887 if (!pud_sect(*pud))
888 return 0;
889 pud_clear(pud);
890 return 1;
891 }
892
pmd_clear_huge(pmd_t * pmd)893 int pmd_clear_huge(pmd_t *pmd)
894 {
895 if (!pmd_sect(*pmd))
896 return 0;
897 pmd_clear(pmd);
898 return 1;
899 }
900
901 #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
pud_free_pmd_page(pud_t * pud,unsigned long addr)902 int pud_free_pmd_page(pud_t *pud, unsigned long addr)
903 {
904 return pud_none(*pud);
905 }
906
pmd_free_pte_page(pmd_t * pmd,unsigned long addr)907 int pmd_free_pte_page(pmd_t *pmd, unsigned long addr)
908 {
909 return pmd_none(*pmd);
910 }
911 #endif
912