init.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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/execmem.h>
  26. #include <asm/cp15.h>
  27. #include <asm/mach-types.h>
  28. #include <asm/memblock.h>
  29. #include <asm/page.h>
  30. #include <asm/prom.h>
  31. #include <asm/sections.h>
  32. #include <asm/setup.h>
  33. #include <asm/set_memory.h>
  34. #include <asm/system_info.h>
  35. #include <asm/tlb.h>
  36. #include <asm/fixmap.h>
  37. #include <asm/ptdump.h>
  38. #include <asm/mach/arch.h>
  39. #include <asm/mach/map.h>
  40. #include "mm.h"
  41. #ifdef CONFIG_CPU_CP15_MMU
  42. unsigned long __init __clear_cr(unsigned long mask)
  43. {
  44. cr_alignment = cr_alignment & ~mask;
  45. return cr_alignment;
  46. }
  47. #endif
  48. #ifdef CONFIG_BLK_DEV_INITRD
  49. static int __init parse_tag_initrd(const struct tag *tag)
  50. {
  51. pr_warn("ATAG_INITRD is deprecated; "
  52. "please update your bootloader.\n");
  53. phys_initrd_start = __virt_to_phys(tag->u.initrd.start);
  54. phys_initrd_size = tag->u.initrd.size;
  55. return 0;
  56. }
  57. __tagtable(ATAG_INITRD, parse_tag_initrd);
  58. static int __init parse_tag_initrd2(const struct tag *tag)
  59. {
  60. phys_initrd_start = tag->u.initrd.start;
  61. phys_initrd_size = tag->u.initrd.size;
  62. return 0;
  63. }
  64. __tagtable(ATAG_INITRD2, parse_tag_initrd2);
  65. #endif
  66. static void __init find_limits(unsigned long *min, unsigned long *max_low,
  67. unsigned long *max_high)
  68. {
  69. *max_low = PFN_DOWN(memblock_get_current_limit());
  70. *min = PFN_UP(memblock_start_of_DRAM());
  71. *max_high = PFN_DOWN(memblock_end_of_DRAM());
  72. }
  73. #ifdef CONFIG_ZONE_DMA
  74. phys_addr_t arm_dma_zone_size __read_mostly;
  75. EXPORT_SYMBOL(arm_dma_zone_size);
  76. /*
  77. * The DMA mask corresponding to the maximum bus address allocatable
  78. * using GFP_DMA. The default here places no restriction on DMA
  79. * allocations. This must be the smallest DMA mask in the system,
  80. * so a successful GFP_DMA allocation will always satisfy this.
  81. */
  82. phys_addr_t arm_dma_limit;
  83. unsigned long arm_dma_pfn_limit;
  84. #endif
  85. void __init setup_dma_zone(const struct machine_desc *mdesc)
  86. {
  87. #ifdef CONFIG_ZONE_DMA
  88. if (mdesc->dma_zone_size) {
  89. arm_dma_zone_size = mdesc->dma_zone_size;
  90. arm_dma_limit = PHYS_OFFSET + arm_dma_zone_size - 1;
  91. } else
  92. arm_dma_limit = 0xffffffff;
  93. arm_dma_pfn_limit = arm_dma_limit >> PAGE_SHIFT;
  94. #endif
  95. }
  96. static void __init zone_sizes_init(unsigned long min, unsigned long max_low,
  97. unsigned long max_high)
  98. {
  99. unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 };
  100. #ifdef CONFIG_ZONE_DMA
  101. max_zone_pfn[ZONE_DMA] = min(arm_dma_pfn_limit, max_low);
  102. #endif
  103. max_zone_pfn[ZONE_NORMAL] = max_low;
  104. #ifdef CONFIG_HIGHMEM
  105. max_zone_pfn[ZONE_HIGHMEM] = max_high;
  106. #endif
  107. free_area_init(max_zone_pfn);
  108. }
  109. #ifdef CONFIG_HAVE_ARCH_PFN_VALID
  110. int pfn_valid(unsigned long pfn)
  111. {
  112. phys_addr_t addr = __pfn_to_phys(pfn);
  113. unsigned long pageblock_size = PAGE_SIZE * pageblock_nr_pages;
  114. if (__phys_to_pfn(addr) != pfn)
  115. return 0;
  116. /*
  117. * If address less than pageblock_size bytes away from a present
  118. * memory chunk there still will be a memory map entry for it
  119. * because we round freed memory map to the pageblock boundaries.
  120. */
  121. if (memblock_overlaps_region(&memblock.memory,
  122. ALIGN_DOWN(addr, pageblock_size),
  123. pageblock_size))
  124. return 1;
  125. return 0;
  126. }
  127. EXPORT_SYMBOL(pfn_valid);
  128. #endif
  129. static bool arm_memblock_steal_permitted = true;
  130. phys_addr_t __init arm_memblock_steal(phys_addr_t size, phys_addr_t align)
  131. {
  132. phys_addr_t phys;
  133. BUG_ON(!arm_memblock_steal_permitted);
  134. phys = memblock_phys_alloc(size, align);
  135. if (!phys)
  136. panic("Failed to steal %pa bytes at %pS\n",
  137. &size, (void *)_RET_IP_);
  138. memblock_phys_free(phys, size);
  139. memblock_remove(phys, size);
  140. return phys;
  141. }
  142. #ifdef CONFIG_CPU_ICACHE_MISMATCH_WORKAROUND
  143. void check_cpu_icache_size(int cpuid)
  144. {
  145. u32 size, ctr;
  146. asm("mrc p15, 0, %0, c0, c0, 1" : "=r" (ctr));
  147. size = 1 << ((ctr & 0xf) + 2);
  148. if (cpuid != 0 && icache_size != size)
  149. pr_info("CPU%u: detected I-Cache line size mismatch, workaround enabled\n",
  150. cpuid);
  151. if (icache_size > size)
  152. icache_size = size;
  153. }
  154. #endif
  155. void __init arm_memblock_init(const struct machine_desc *mdesc)
  156. {
  157. /* Register the kernel text, kernel data and initrd with memblock. */
  158. memblock_reserve(__pa(KERNEL_START), KERNEL_END - KERNEL_START);
  159. reserve_initrd_mem();
  160. arm_mm_memblock_reserve();
  161. /* reserve any platform specific memblock areas */
  162. if (mdesc->reserve)
  163. mdesc->reserve();
  164. early_init_fdt_scan_reserved_mem();
  165. /* reserve memory for DMA contiguous allocations */
  166. dma_contiguous_reserve(arm_dma_limit);
  167. arm_memblock_steal_permitted = false;
  168. memblock_dump_all();
  169. }
  170. void __init bootmem_init(void)
  171. {
  172. memblock_allow_resize();
  173. find_limits(&min_low_pfn, &max_low_pfn, &max_pfn);
  174. early_memtest((phys_addr_t)min_low_pfn << PAGE_SHIFT,
  175. (phys_addr_t)max_low_pfn << PAGE_SHIFT);
  176. /*
  177. * sparse_init() tries to allocate memory from memblock, so must be
  178. * done after the fixed reservations
  179. */
  180. sparse_init();
  181. /*
  182. * Now free the memory - free_area_init needs
  183. * the sparse mem_map arrays initialized by sparse_init()
  184. * for memmap_init_zone(), otherwise all PFNs are invalid.
  185. */
  186. zone_sizes_init(min_low_pfn, max_low_pfn, max_pfn);
  187. }
  188. /*
  189. * Poison init memory with an undefined instruction (ARM) or a branch to an
  190. * undefined instruction (Thumb).
  191. */
  192. static inline void poison_init_mem(void *s, size_t count)
  193. {
  194. u32 *p = (u32 *)s;
  195. for (; count != 0; count -= 4)
  196. *p++ = 0xe7fddef0;
  197. }
  198. static void __init free_highpages(void)
  199. {
  200. #ifdef CONFIG_HIGHMEM
  201. unsigned long max_low = max_low_pfn;
  202. phys_addr_t range_start, range_end;
  203. u64 i;
  204. /* set highmem page free */
  205. for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE,
  206. &range_start, &range_end, NULL) {
  207. unsigned long start = PFN_UP(range_start);
  208. unsigned long end = PFN_DOWN(range_end);
  209. /* Ignore complete lowmem entries */
  210. if (end <= max_low)
  211. continue;
  212. /* Truncate partial highmem entries */
  213. if (start < max_low)
  214. start = max_low;
  215. for (; start < end; start++)
  216. free_highmem_page(pfn_to_page(start));
  217. }
  218. #endif
  219. }
  220. /*
  221. * mem_init() marks the free areas in the mem_map and tells us how much
  222. * memory is free. This is done after various parts of the system have
  223. * claimed their memory after the kernel image.
  224. */
  225. void __init mem_init(void)
  226. {
  227. #ifdef CONFIG_ARM_LPAE
  228. swiotlb_init(max_pfn > arm_dma_pfn_limit, SWIOTLB_VERBOSE);
  229. #endif
  230. set_max_mapnr(pfn_to_page(max_pfn) - mem_map);
  231. /* this will put all unused low memory onto the freelists */
  232. memblock_free_all();
  233. #ifdef CONFIG_SA1111
  234. /* now that our DMA memory is actually so designated, we can free it */
  235. free_reserved_area(__va(PHYS_OFFSET), swapper_pg_dir, -1, NULL);
  236. #endif
  237. free_highpages();
  238. /*
  239. * Check boundaries twice: Some fundamental inconsistencies can
  240. * be detected at build time already.
  241. */
  242. #ifdef CONFIG_MMU
  243. BUILD_BUG_ON(TASK_SIZE > MODULES_VADDR);
  244. BUG_ON(TASK_SIZE > MODULES_VADDR);
  245. #endif
  246. #ifdef CONFIG_HIGHMEM
  247. BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET);
  248. BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET);
  249. #endif
  250. }
  251. #ifdef CONFIG_STRICT_KERNEL_RWX
  252. struct section_perm {
  253. const char *name;
  254. unsigned long start;
  255. unsigned long end;
  256. pmdval_t mask;
  257. pmdval_t prot;
  258. pmdval_t clear;
  259. };
  260. /* First section-aligned location at or after __start_rodata. */
  261. extern char __start_rodata_section_aligned[];
  262. static struct section_perm nx_perms[] = {
  263. /* Make pages tables, etc before _stext RW (set NX). */
  264. {
  265. .name = "pre-text NX",
  266. .start = PAGE_OFFSET,
  267. .end = (unsigned long)_stext,
  268. .mask = ~PMD_SECT_XN,
  269. .prot = PMD_SECT_XN,
  270. },
  271. /* Make init RW (set NX). */
  272. {
  273. .name = "init NX",
  274. .start = (unsigned long)__init_begin,
  275. .end = (unsigned long)_sdata,
  276. .mask = ~PMD_SECT_XN,
  277. .prot = PMD_SECT_XN,
  278. },
  279. /* Make rodata NX (set RO in ro_perms below). */
  280. {
  281. .name = "rodata NX",
  282. .start = (unsigned long)__start_rodata_section_aligned,
  283. .end = (unsigned long)__init_begin,
  284. .mask = ~PMD_SECT_XN,
  285. .prot = PMD_SECT_XN,
  286. },
  287. };
  288. static struct section_perm ro_perms[] = {
  289. /* Make kernel code and rodata RX (set RO). */
  290. {
  291. .name = "text/rodata RO",
  292. .start = (unsigned long)_stext,
  293. .end = (unsigned long)__init_begin,
  294. #ifdef CONFIG_ARM_LPAE
  295. .mask = ~(L_PMD_SECT_RDONLY | PMD_SECT_AP2),
  296. .prot = L_PMD_SECT_RDONLY | PMD_SECT_AP2,
  297. #else
  298. .mask = ~(PMD_SECT_APX | PMD_SECT_AP_WRITE),
  299. .prot = PMD_SECT_APX | PMD_SECT_AP_WRITE,
  300. .clear = PMD_SECT_AP_WRITE,
  301. #endif
  302. },
  303. };
  304. /*
  305. * Updates section permissions only for the current mm (sections are
  306. * copied into each mm). During startup, this is the init_mm. Is only
  307. * safe to be called with preemption disabled, as under stop_machine().
  308. */
  309. static inline void section_update(unsigned long addr, pmdval_t mask,
  310. pmdval_t prot, struct mm_struct *mm)
  311. {
  312. pmd_t *pmd;
  313. pmd = pmd_offset(pud_offset(p4d_offset(pgd_offset(mm, addr), addr), addr), addr);
  314. #ifdef CONFIG_ARM_LPAE
  315. pmd[0] = __pmd((pmd_val(pmd[0]) & mask) | prot);
  316. #else
  317. if (addr & SECTION_SIZE)
  318. pmd[1] = __pmd((pmd_val(pmd[1]) & mask) | prot);
  319. else
  320. pmd[0] = __pmd((pmd_val(pmd[0]) & mask) | prot);
  321. #endif
  322. flush_pmd_entry(pmd);
  323. local_flush_tlb_kernel_range(addr, addr + SECTION_SIZE);
  324. }
  325. /* Make sure extended page tables are in use. */
  326. static inline bool arch_has_strict_perms(void)
  327. {
  328. if (cpu_architecture() < CPU_ARCH_ARMv6)
  329. return false;
  330. return !!(get_cr() & CR_XP);
  331. }
  332. static void set_section_perms(struct section_perm *perms, int n, bool set,
  333. struct mm_struct *mm)
  334. {
  335. size_t i;
  336. unsigned long addr;
  337. if (!arch_has_strict_perms())
  338. return;
  339. for (i = 0; i < n; i++) {
  340. if (!IS_ALIGNED(perms[i].start, SECTION_SIZE) ||
  341. !IS_ALIGNED(perms[i].end, SECTION_SIZE)) {
  342. pr_err("BUG: %s section %lx-%lx not aligned to %lx\n",
  343. perms[i].name, perms[i].start, perms[i].end,
  344. SECTION_SIZE);
  345. continue;
  346. }
  347. for (addr = perms[i].start;
  348. addr < perms[i].end;
  349. addr += SECTION_SIZE)
  350. section_update(addr, perms[i].mask,
  351. set ? perms[i].prot : perms[i].clear, mm);
  352. }
  353. }
  354. /*
  355. * update_sections_early intended to be called only through stop_machine
  356. * framework and executed by only one CPU while all other CPUs will spin and
  357. * wait, so no locking is required in this function.
  358. */
  359. static void update_sections_early(struct section_perm perms[], int n)
  360. {
  361. struct task_struct *t, *s;
  362. for_each_process(t) {
  363. if (t->flags & PF_KTHREAD)
  364. continue;
  365. for_each_thread(t, s)
  366. if (s->mm)
  367. set_section_perms(perms, n, true, s->mm);
  368. }
  369. set_section_perms(perms, n, true, current->active_mm);
  370. set_section_perms(perms, n, true, &init_mm);
  371. }
  372. static int __fix_kernmem_perms(void *unused)
  373. {
  374. update_sections_early(nx_perms, ARRAY_SIZE(nx_perms));
  375. return 0;
  376. }
  377. static void fix_kernmem_perms(void)
  378. {
  379. stop_machine(__fix_kernmem_perms, NULL, NULL);
  380. }
  381. static int __mark_rodata_ro(void *unused)
  382. {
  383. update_sections_early(ro_perms, ARRAY_SIZE(ro_perms));
  384. return 0;
  385. }
  386. void mark_rodata_ro(void)
  387. {
  388. stop_machine(__mark_rodata_ro, NULL, NULL);
  389. arm_debug_checkwx();
  390. }
  391. #else
  392. static inline void fix_kernmem_perms(void) { }
  393. #endif /* CONFIG_STRICT_KERNEL_RWX */
  394. void free_initmem(void)
  395. {
  396. fix_kernmem_perms();
  397. poison_init_mem(__init_begin, __init_end - __init_begin);
  398. if (!machine_is_integrator() && !machine_is_cintegrator())
  399. free_initmem_default(-1);
  400. }
  401. #ifdef CONFIG_BLK_DEV_INITRD
  402. void free_initrd_mem(unsigned long start, unsigned long end)
  403. {
  404. if (start == initrd_start)
  405. start = round_down(start, PAGE_SIZE);
  406. if (end == initrd_end)
  407. end = round_up(end, PAGE_SIZE);
  408. poison_init_mem((void *)start, PAGE_ALIGN(end) - start);
  409. free_reserved_area((void *)start, (void *)end, -1, "initrd");
  410. }
  411. #endif
  412. #ifdef CONFIG_EXECMEM
  413. #ifdef CONFIG_XIP_KERNEL
  414. /*
  415. * The XIP kernel text is mapped in the module area for modules and
  416. * some other stuff to work without any indirect relocations.
  417. * MODULES_VADDR is redefined here and not in asm/memory.h to avoid
  418. * recompiling the whole kernel when CONFIG_XIP_KERNEL is turned on/off.
  419. */
  420. #undef MODULES_VADDR
  421. #define MODULES_VADDR (((unsigned long)_exiprom + ~PMD_MASK) & PMD_MASK)
  422. #endif
  423. #ifdef CONFIG_MMU
  424. static struct execmem_info execmem_info __ro_after_init;
  425. struct execmem_info __init *execmem_arch_setup(void)
  426. {
  427. unsigned long fallback_start = 0, fallback_end = 0;
  428. if (IS_ENABLED(CONFIG_ARM_MODULE_PLTS)) {
  429. fallback_start = VMALLOC_START;
  430. fallback_end = VMALLOC_END;
  431. }
  432. execmem_info = (struct execmem_info){
  433. .ranges = {
  434. [EXECMEM_DEFAULT] = {
  435. .start = MODULES_VADDR,
  436. .end = MODULES_END,
  437. .pgprot = PAGE_KERNEL_EXEC,
  438. .alignment = 1,
  439. .fallback_start = fallback_start,
  440. .fallback_end = fallback_end,
  441. },
  442. },
  443. };
  444. return &execmem_info;
  445. }
  446. #endif /* CONFIG_MMU */
  447. #endif /* CONFIG_EXECMEM */