1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/arch/arm/mm/init.c
4 *
5 * Copyright (C) 1995-2005 Russell King
6 */
7 #include <linux/kernel.h>
8 #include <linux/errno.h>
9 #include <linux/swap.h>
10 #include <linux/init.h>
11 #include <linux/mman.h>
12 #include <linux/sched/signal.h>
13 #include <linux/sched/task.h>
14 #include <linux/export.h>
15 #include <linux/nodemask.h>
16 #include <linux/initrd.h>
17 #include <linux/of_fdt.h>
18 #include <linux/highmem.h>
19 #include <linux/gfp.h>
20 #include <linux/memblock.h>
21 #include <linux/dma-map-ops.h>
22 #include <linux/sizes.h>
23 #include <linux/stop_machine.h>
24 #include <linux/swiotlb.h>
25 #include <linux/rk-dma-heap.h>
26
27 #include <asm/cp15.h>
28 #include <asm/mach-types.h>
29 #include <asm/memblock.h>
30 #include <asm/memory.h>
31 #include <asm/prom.h>
32 #include <asm/sections.h>
33 #include <asm/setup.h>
34 #include <asm/set_memory.h>
35 #include <asm/system_info.h>
36 #include <asm/tlb.h>
37 #include <asm/fixmap.h>
38 #include <asm/ptdump.h>
39
40 #include <asm/mach/arch.h>
41 #include <asm/mach/map.h>
42
43 #include "mm.h"
44
45 #ifdef CONFIG_CPU_CP15_MMU
__clear_cr(unsigned long mask)46 unsigned long __init __clear_cr(unsigned long mask)
47 {
48 cr_alignment = cr_alignment & ~mask;
49 return cr_alignment;
50 }
51 #endif
52
53 #ifdef CONFIG_BLK_DEV_INITRD
parse_tag_initrd(const struct tag * tag)54 static int __init parse_tag_initrd(const struct tag *tag)
55 {
56 pr_warn("ATAG_INITRD is deprecated; "
57 "please update your bootloader.\n");
58 phys_initrd_start = __virt_to_phys(tag->u.initrd.start);
59 phys_initrd_size = tag->u.initrd.size;
60 return 0;
61 }
62
63 __tagtable(ATAG_INITRD, parse_tag_initrd);
64
parse_tag_initrd2(const struct tag * tag)65 static int __init parse_tag_initrd2(const struct tag *tag)
66 {
67 phys_initrd_start = tag->u.initrd.start;
68 phys_initrd_size = tag->u.initrd.size;
69 return 0;
70 }
71
72 __tagtable(ATAG_INITRD2, parse_tag_initrd2);
73 #endif
74
find_limits(unsigned long * min,unsigned long * max_low,unsigned long * max_high)75 static void __init find_limits(unsigned long *min, unsigned long *max_low,
76 unsigned long *max_high)
77 {
78 *max_low = PFN_DOWN(memblock_get_current_limit());
79 *min = PFN_UP(memblock_start_of_DRAM());
80 *max_high = PFN_DOWN(memblock_end_of_DRAM());
81 }
82
83 #ifdef CONFIG_ZONE_DMA
84
85 phys_addr_t arm_dma_zone_size __read_mostly;
86 EXPORT_SYMBOL(arm_dma_zone_size);
87
88 /*
89 * The DMA mask corresponding to the maximum bus address allocatable
90 * using GFP_DMA. The default here places no restriction on DMA
91 * allocations. This must be the smallest DMA mask in the system,
92 * so a successful GFP_DMA allocation will always satisfy this.
93 */
94 phys_addr_t arm_dma_limit;
95 unsigned long arm_dma_pfn_limit;
96 #endif
97
setup_dma_zone(const struct machine_desc * mdesc)98 void __init setup_dma_zone(const struct machine_desc *mdesc)
99 {
100 #ifdef CONFIG_ZONE_DMA
101 if (mdesc->dma_zone_size) {
102 arm_dma_zone_size = mdesc->dma_zone_size;
103 arm_dma_limit = PHYS_OFFSET + arm_dma_zone_size - 1;
104 } else
105 arm_dma_limit = 0xffffffff;
106 arm_dma_pfn_limit = arm_dma_limit >> PAGE_SHIFT;
107 #endif
108 }
109
zone_sizes_init(unsigned long min,unsigned long max_low,unsigned long max_high)110 static void __init zone_sizes_init(unsigned long min, unsigned long max_low,
111 unsigned long max_high)
112 {
113 unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 };
114
115 #ifdef CONFIG_ZONE_DMA
116 max_zone_pfn[ZONE_DMA] = min(arm_dma_pfn_limit, max_low);
117 #endif
118 max_zone_pfn[ZONE_NORMAL] = max_low;
119 #ifdef CONFIG_HIGHMEM
120 max_zone_pfn[ZONE_HIGHMEM] = max_high;
121 #endif
122 free_area_init(max_zone_pfn);
123 }
124
125 #ifdef CONFIG_HAVE_ARCH_PFN_VALID
pfn_valid(unsigned long pfn)126 int pfn_valid(unsigned long pfn)
127 {
128 phys_addr_t addr = __pfn_to_phys(pfn);
129 unsigned long pageblock_size = PAGE_SIZE * pageblock_nr_pages;
130
131 if (__phys_to_pfn(addr) != pfn)
132 return 0;
133
134 /*
135 * If address less than pageblock_size bytes away from a present
136 * memory chunk there still will be a memory map entry for it
137 * because we round freed memory map to the pageblock boundaries.
138 */
139 if (memblock_overlaps_region(&memblock.memory,
140 ALIGN_DOWN(addr, pageblock_size),
141 pageblock_size))
142 return 1;
143
144 return 0;
145 }
146 EXPORT_SYMBOL(pfn_valid);
147 #endif
148
149 static bool arm_memblock_steal_permitted = true;
150
arm_memblock_steal(phys_addr_t size,phys_addr_t align)151 phys_addr_t __init arm_memblock_steal(phys_addr_t size, phys_addr_t align)
152 {
153 phys_addr_t phys;
154
155 BUG_ON(!arm_memblock_steal_permitted);
156
157 phys = memblock_phys_alloc(size, align);
158 if (!phys)
159 panic("Failed to steal %pa bytes at %pS\n",
160 &size, (void *)_RET_IP_);
161
162 memblock_free(phys, size);
163 memblock_remove(phys, size);
164
165 return phys;
166 }
167
arm_initrd_init(void)168 static void __init arm_initrd_init(void)
169 {
170 #ifdef CONFIG_BLK_DEV_INITRD
171 phys_addr_t start;
172 unsigned long size;
173
174 initrd_start = initrd_end = 0;
175
176 if (!phys_initrd_size)
177 return;
178
179 /*
180 * Round the memory region to page boundaries as per free_initrd_mem()
181 * This allows us to detect whether the pages overlapping the initrd
182 * are in use, but more importantly, reserves the entire set of pages
183 * as we don't want these pages allocated for other purposes.
184 */
185 start = round_down(phys_initrd_start, PAGE_SIZE);
186 size = phys_initrd_size + (phys_initrd_start - start);
187 size = round_up(size, PAGE_SIZE);
188
189 if (!memblock_is_region_memory(start, size)) {
190 pr_err("INITRD: 0x%08llx+0x%08lx is not a memory region - disabling initrd\n",
191 (u64)start, size);
192 return;
193 }
194
195 if (memblock_is_region_reserved(start, size)) {
196 pr_err("INITRD: 0x%08llx+0x%08lx overlaps in-use memory region - disabling initrd\n",
197 (u64)start, size);
198 return;
199 }
200
201 memblock_reserve(start, size);
202
203 /* Now convert initrd to virtual addresses */
204 initrd_start = __phys_to_virt(phys_initrd_start);
205 initrd_end = initrd_start + phys_initrd_size;
206 #endif
207 }
208
209 #ifdef CONFIG_CPU_ICACHE_MISMATCH_WORKAROUND
check_cpu_icache_size(int cpuid)210 void check_cpu_icache_size(int cpuid)
211 {
212 u32 size, ctr;
213
214 asm("mrc p15, 0, %0, c0, c0, 1" : "=r" (ctr));
215
216 size = 1 << ((ctr & 0xf) + 2);
217 if (cpuid != 0 && icache_size != size)
218 pr_info("CPU%u: detected I-Cache line size mismatch, workaround enabled\n",
219 cpuid);
220 if (icache_size > size)
221 icache_size = size;
222 }
223 #endif
224
arm_memblock_init(const struct machine_desc * mdesc)225 void __init arm_memblock_init(const struct machine_desc *mdesc)
226 {
227 /* Register the kernel text, kernel data and initrd with memblock. */
228 memblock_reserve(__pa(KERNEL_START), KERNEL_END - KERNEL_START);
229
230 arm_initrd_init();
231
232 arm_mm_memblock_reserve();
233
234 /* reserve any platform specific memblock areas */
235 if (mdesc->reserve)
236 mdesc->reserve();
237
238 early_init_fdt_scan_reserved_mem();
239
240 /* reserve memory for DMA contiguous allocations */
241 dma_contiguous_reserve(arm_dma_limit);
242 rk_dma_heap_cma_setup();
243
244 arm_memblock_steal_permitted = false;
245 memblock_dump_all();
246 }
247
bootmem_init(void)248 void __init bootmem_init(void)
249 {
250 memblock_allow_resize();
251
252 find_limits(&min_low_pfn, &max_low_pfn, &max_pfn);
253
254 early_memtest((phys_addr_t)min_low_pfn << PAGE_SHIFT,
255 (phys_addr_t)max_low_pfn << PAGE_SHIFT);
256
257 /*
258 * sparse_init() tries to allocate memory from memblock, so must be
259 * done after the fixed reservations
260 */
261 sparse_init();
262
263 /*
264 * Now free the memory - free_area_init needs
265 * the sparse mem_map arrays initialized by sparse_init()
266 * for memmap_init_zone(), otherwise all PFNs are invalid.
267 */
268 zone_sizes_init(min_low_pfn, max_low_pfn, max_pfn);
269 }
270
271 /*
272 * Poison init memory with an undefined instruction (ARM) or a branch to an
273 * undefined instruction (Thumb).
274 */
poison_init_mem(void * s,size_t count)275 static inline void poison_init_mem(void *s, size_t count)
276 {
277 u32 *p = (u32 *)s;
278 for (; count != 0; count -= 4)
279 *p++ = 0xe7fddef0;
280 }
281
282 static inline void __init
free_memmap(unsigned long start_pfn,unsigned long end_pfn)283 free_memmap(unsigned long start_pfn, unsigned long end_pfn)
284 {
285 struct page *start_pg, *end_pg;
286 phys_addr_t pg, pgend;
287
288 /*
289 * Convert start_pfn/end_pfn to a struct page pointer.
290 */
291 start_pg = pfn_to_page(start_pfn - 1) + 1;
292 end_pg = pfn_to_page(end_pfn - 1) + 1;
293
294 /*
295 * Convert to physical addresses, and
296 * round start upwards and end downwards.
297 */
298 pg = PAGE_ALIGN(__pa(start_pg));
299 pgend = __pa(end_pg) & PAGE_MASK;
300
301 /*
302 * If there are free pages between these,
303 * free the section of the memmap array.
304 */
305 if (pg < pgend)
306 memblock_free_early(pg, pgend - pg);
307 }
308
309 /*
310 * The mem_map array can get very big. Free the unused area of the memory map.
311 */
free_unused_memmap(void)312 static void __init free_unused_memmap(void)
313 {
314 unsigned long start, end, prev_end = 0;
315 int i;
316
317 /*
318 * This relies on each bank being in address order.
319 * The banks are sorted previously in bootmem_init().
320 */
321 for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, NULL) {
322 #ifdef CONFIG_SPARSEMEM
323 /*
324 * Take care not to free memmap entries that don't exist
325 * due to SPARSEMEM sections which aren't present.
326 */
327 start = min(start,
328 ALIGN(prev_end, PAGES_PER_SECTION));
329 #endif
330 /*
331 * Align down here since many operations in VM subsystem
332 * presume that there are no holes in the memory map inside
333 * a pageblock
334 */
335 start = round_down(start, pageblock_nr_pages);
336
337 /*
338 * If we had a previous bank, and there is a space
339 * between the current bank and the previous, free it.
340 */
341 if (prev_end && prev_end < start)
342 free_memmap(prev_end, start);
343
344 /*
345 * Align up here since many operations in VM subsystem
346 * presume that there are no holes in the memory map inside
347 * a pageblock
348 */
349 prev_end = ALIGN(end, pageblock_nr_pages);
350 }
351
352 #ifdef CONFIG_SPARSEMEM
353 if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION)) {
354 prev_end = ALIGN(end, pageblock_nr_pages);
355 free_memmap(prev_end,
356 ALIGN(prev_end, PAGES_PER_SECTION));
357 }
358 #endif
359 }
360
free_highpages(void)361 static void __init free_highpages(void)
362 {
363 #ifdef CONFIG_HIGHMEM
364 unsigned long max_low = max_low_pfn;
365 phys_addr_t range_start, range_end;
366 u64 i;
367
368 /* set highmem page free */
369 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE,
370 &range_start, &range_end, NULL) {
371 unsigned long start = PFN_UP(range_start);
372 unsigned long end = PFN_DOWN(range_end);
373
374 /* Ignore complete lowmem entries */
375 if (end <= max_low)
376 continue;
377
378 /* Truncate partial highmem entries */
379 if (start < max_low)
380 start = max_low;
381
382 for (; start < end; start++)
383 free_highmem_page(pfn_to_page(start));
384 }
385 #endif
386 }
387
388 /*
389 * mem_init() marks the free areas in the mem_map and tells us how much
390 * memory is free. This is done after various parts of the system have
391 * claimed their memory after the kernel image.
392 */
mem_init(void)393 void __init mem_init(void)
394 {
395 #ifdef CONFIG_ARM_LPAE
396 if (swiotlb_force == SWIOTLB_FORCE ||
397 max_pfn > arm_dma_pfn_limit)
398 swiotlb_init(1);
399 else
400 swiotlb_force = SWIOTLB_NO_FORCE;
401 #endif
402
403 set_max_mapnr(pfn_to_page(max_pfn) - mem_map);
404
405 /* this will put all unused low memory onto the freelists */
406 free_unused_memmap();
407 memblock_free_all();
408
409 #ifdef CONFIG_SA1111
410 /* now that our DMA memory is actually so designated, we can free it */
411 free_reserved_area(__va(PHYS_OFFSET), swapper_pg_dir, -1, NULL);
412 #endif
413
414 free_highpages();
415
416 mem_init_print_info(NULL);
417
418 /*
419 * Check boundaries twice: Some fundamental inconsistencies can
420 * be detected at build time already.
421 */
422 #ifdef CONFIG_MMU
423 BUILD_BUG_ON(TASK_SIZE > MODULES_VADDR);
424 BUG_ON(TASK_SIZE > MODULES_VADDR);
425 #endif
426
427 #ifdef CONFIG_HIGHMEM
428 BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET);
429 BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET);
430 #endif
431 }
432
433 #ifdef CONFIG_STRICT_KERNEL_RWX
434 struct section_perm {
435 const char *name;
436 unsigned long start;
437 unsigned long end;
438 pmdval_t mask;
439 pmdval_t prot;
440 pmdval_t clear;
441 };
442
443 /* First section-aligned location at or after __start_rodata. */
444 extern char __start_rodata_section_aligned[];
445
446 static struct section_perm nx_perms[] = {
447 /* Make pages tables, etc before _stext RW (set NX). */
448 {
449 .name = "pre-text NX",
450 .start = PAGE_OFFSET,
451 .end = (unsigned long)_stext,
452 .mask = ~PMD_SECT_XN,
453 .prot = PMD_SECT_XN,
454 },
455 /* Make init RW (set NX). */
456 {
457 .name = "init NX",
458 .start = (unsigned long)__init_begin,
459 .end = (unsigned long)_sdata,
460 .mask = ~PMD_SECT_XN,
461 .prot = PMD_SECT_XN,
462 },
463 /* Make rodata NX (set RO in ro_perms below). */
464 {
465 .name = "rodata NX",
466 .start = (unsigned long)__start_rodata_section_aligned,
467 .end = (unsigned long)__init_begin,
468 .mask = ~PMD_SECT_XN,
469 .prot = PMD_SECT_XN,
470 },
471 };
472
473 static struct section_perm ro_perms[] = {
474 /* Make kernel code and rodata RX (set RO). */
475 {
476 .name = "text/rodata RO",
477 .start = (unsigned long)_stext,
478 .end = (unsigned long)__init_begin,
479 #ifdef CONFIG_ARM_LPAE
480 .mask = ~(L_PMD_SECT_RDONLY | PMD_SECT_AP2),
481 .prot = L_PMD_SECT_RDONLY | PMD_SECT_AP2,
482 #else
483 .mask = ~(PMD_SECT_APX | PMD_SECT_AP_WRITE),
484 .prot = PMD_SECT_APX | PMD_SECT_AP_WRITE,
485 .clear = PMD_SECT_AP_WRITE,
486 #endif
487 },
488 };
489
490 /*
491 * Updates section permissions only for the current mm (sections are
492 * copied into each mm). During startup, this is the init_mm. Is only
493 * safe to be called with preemption disabled, as under stop_machine().
494 */
section_update(unsigned long addr,pmdval_t mask,pmdval_t prot,struct mm_struct * mm)495 static inline void section_update(unsigned long addr, pmdval_t mask,
496 pmdval_t prot, struct mm_struct *mm)
497 {
498 pmd_t *pmd;
499
500 pmd = pmd_offset(pud_offset(p4d_offset(pgd_offset(mm, addr), addr), addr), addr);
501
502 #ifdef CONFIG_ARM_LPAE
503 pmd[0] = __pmd((pmd_val(pmd[0]) & mask) | prot);
504 #else
505 if (addr & SECTION_SIZE)
506 pmd[1] = __pmd((pmd_val(pmd[1]) & mask) | prot);
507 else
508 pmd[0] = __pmd((pmd_val(pmd[0]) & mask) | prot);
509 #endif
510 flush_pmd_entry(pmd);
511 local_flush_tlb_kernel_range(addr, addr + SECTION_SIZE);
512 }
513
514 /* Make sure extended page tables are in use. */
arch_has_strict_perms(void)515 static inline bool arch_has_strict_perms(void)
516 {
517 if (cpu_architecture() < CPU_ARCH_ARMv6)
518 return false;
519
520 return !!(get_cr() & CR_XP);
521 }
522
set_section_perms(struct section_perm * perms,int n,bool set,struct mm_struct * mm)523 static void set_section_perms(struct section_perm *perms, int n, bool set,
524 struct mm_struct *mm)
525 {
526 size_t i;
527 unsigned long addr;
528
529 if (!arch_has_strict_perms())
530 return;
531
532 for (i = 0; i < n; i++) {
533 if (!IS_ALIGNED(perms[i].start, SECTION_SIZE) ||
534 !IS_ALIGNED(perms[i].end, SECTION_SIZE)) {
535 pr_err("BUG: %s section %lx-%lx not aligned to %lx\n",
536 perms[i].name, perms[i].start, perms[i].end,
537 SECTION_SIZE);
538 continue;
539 }
540
541 for (addr = perms[i].start;
542 addr < perms[i].end;
543 addr += SECTION_SIZE)
544 section_update(addr, perms[i].mask,
545 set ? perms[i].prot : perms[i].clear, mm);
546 }
547
548 }
549
550 /**
551 * update_sections_early intended to be called only through stop_machine
552 * framework and executed by only one CPU while all other CPUs will spin and
553 * wait, so no locking is required in this function.
554 */
update_sections_early(struct section_perm perms[],int n)555 static void update_sections_early(struct section_perm perms[], int n)
556 {
557 struct task_struct *t, *s;
558
559 for_each_process(t) {
560 if (t->flags & PF_KTHREAD)
561 continue;
562 for_each_thread(t, s)
563 if (s->mm)
564 set_section_perms(perms, n, true, s->mm);
565 }
566 set_section_perms(perms, n, true, current->active_mm);
567 set_section_perms(perms, n, true, &init_mm);
568 }
569
__fix_kernmem_perms(void * unused)570 static int __fix_kernmem_perms(void *unused)
571 {
572 update_sections_early(nx_perms, ARRAY_SIZE(nx_perms));
573 return 0;
574 }
575
fix_kernmem_perms(void)576 static void fix_kernmem_perms(void)
577 {
578 stop_machine(__fix_kernmem_perms, NULL, NULL);
579 }
580
__mark_rodata_ro(void * unused)581 static int __mark_rodata_ro(void *unused)
582 {
583 update_sections_early(ro_perms, ARRAY_SIZE(ro_perms));
584 return 0;
585 }
586
587 static int kernel_set_to_readonly __read_mostly;
588
mark_rodata_ro(void)589 void mark_rodata_ro(void)
590 {
591 kernel_set_to_readonly = 1;
592 stop_machine(__mark_rodata_ro, NULL, NULL);
593 debug_checkwx();
594 }
595
set_kernel_text_rw(void)596 void set_kernel_text_rw(void)
597 {
598 if (!kernel_set_to_readonly)
599 return;
600
601 set_section_perms(ro_perms, ARRAY_SIZE(ro_perms), false,
602 current->active_mm);
603 }
604
set_kernel_text_ro(void)605 void set_kernel_text_ro(void)
606 {
607 if (!kernel_set_to_readonly)
608 return;
609
610 set_section_perms(ro_perms, ARRAY_SIZE(ro_perms), true,
611 current->active_mm);
612 }
613
614 #else
fix_kernmem_perms(void)615 static inline void fix_kernmem_perms(void) { }
616 #endif /* CONFIG_STRICT_KERNEL_RWX */
617
free_initmem(void)618 void free_initmem(void)
619 {
620 fix_kernmem_perms();
621
622 poison_init_mem(__init_begin, __init_end - __init_begin);
623 if (!machine_is_integrator() && !machine_is_cintegrator())
624 free_initmem_default(-1);
625 }
626
627 #ifdef CONFIG_BLK_DEV_INITRD
free_initrd_mem(unsigned long start,unsigned long end)628 void free_initrd_mem(unsigned long start, unsigned long end)
629 {
630 if (start == initrd_start)
631 start = round_down(start, PAGE_SIZE);
632 if (end == initrd_end)
633 end = round_up(end, PAGE_SIZE);
634
635 poison_init_mem((void *)start, PAGE_ALIGN(end) - start);
636 free_reserved_area((void *)start, (void *)end, -1, "initrd");
637 }
638 #endif
639