init.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. void __init arch_zone_limits_init(unsigned long *max_zone_pfn)
  97. {
  98. #ifdef CONFIG_ZONE_DMA
  99. max_zone_pfn[ZONE_DMA] = min(arm_dma_pfn_limit, max_low_pfn);
  100. #endif
  101. max_zone_pfn[ZONE_NORMAL] = max_low_pfn;
  102. #ifdef CONFIG_HIGHMEM
  103. max_zone_pfn[ZONE_HIGHMEM] = max_pfn;
  104. #endif
  105. }
  106. #ifdef CONFIG_HAVE_ARCH_PFN_VALID
  107. int pfn_valid(unsigned long pfn)
  108. {
  109. phys_addr_t addr = __pfn_to_phys(pfn);
  110. unsigned long pageblock_size = PAGE_SIZE * pageblock_nr_pages;
  111. if (__phys_to_pfn(addr) != pfn)
  112. return 0;
  113. /*
  114. * If address less than pageblock_size bytes away from a present
  115. * memory chunk there still will be a memory map entry for it
  116. * because we round freed memory map to the pageblock boundaries.
  117. */
  118. if (memblock_overlaps_region(&memblock.memory,
  119. ALIGN_DOWN(addr, pageblock_size),
  120. pageblock_size))
  121. return 1;
  122. return 0;
  123. }
  124. EXPORT_SYMBOL(pfn_valid);
  125. #endif
  126. static bool arm_memblock_steal_permitted = true;
  127. phys_addr_t __init arm_memblock_steal(phys_addr_t size, phys_addr_t align)
  128. {
  129. phys_addr_t phys;
  130. BUG_ON(!arm_memblock_steal_permitted);
  131. phys = memblock_phys_alloc(size, align);
  132. if (!phys)
  133. panic("Failed to steal %pa bytes at %pS\n",
  134. &size, (void *)_RET_IP_);
  135. memblock_phys_free(phys, size);
  136. memblock_remove(phys, size);
  137. return phys;
  138. }
  139. #ifdef CONFIG_CPU_ICACHE_MISMATCH_WORKAROUND
  140. void check_cpu_icache_size(int cpuid)
  141. {
  142. u32 size, ctr;
  143. asm("mrc p15, 0, %0, c0, c0, 1" : "=r" (ctr));
  144. size = 1 << ((ctr & 0xf) + 2);
  145. if (cpuid != 0 && icache_size != size)
  146. pr_info("CPU%u: detected I-Cache line size mismatch, workaround enabled\n",
  147. cpuid);
  148. if (icache_size > size)
  149. icache_size = size;
  150. }
  151. #endif
  152. void __init arm_memblock_init(const struct machine_desc *mdesc)
  153. {
  154. /* Register the kernel text, kernel data and initrd with memblock. */
  155. memblock_reserve(__pa(KERNEL_START), KERNEL_END - KERNEL_START);
  156. reserve_initrd_mem();
  157. arm_mm_memblock_reserve();
  158. /* reserve any platform specific memblock areas */
  159. if (mdesc->reserve)
  160. mdesc->reserve();
  161. early_init_fdt_scan_reserved_mem();
  162. /* reserve memory for DMA contiguous allocations */
  163. dma_contiguous_reserve(arm_dma_limit);
  164. arm_memblock_steal_permitted = false;
  165. memblock_dump_all();
  166. }
  167. void __init bootmem_init(void)
  168. {
  169. memblock_allow_resize();
  170. find_limits(&min_low_pfn, &max_low_pfn, &max_pfn);
  171. early_memtest((phys_addr_t)min_low_pfn << PAGE_SHIFT,
  172. (phys_addr_t)max_low_pfn << PAGE_SHIFT);
  173. }
  174. /*
  175. * Poison init memory with an undefined instruction (ARM) or a branch to an
  176. * undefined instruction (Thumb).
  177. */
  178. static inline void poison_init_mem(void *s, size_t count)
  179. {
  180. u32 *p = (u32 *)s;
  181. for (; count != 0; count -= 4)
  182. *p++ = 0xe7fddef0;
  183. }
  184. void __init arch_mm_preinit(void)
  185. {
  186. #ifdef CONFIG_ARM_LPAE
  187. swiotlb_init(max_pfn > arm_dma_pfn_limit, SWIOTLB_VERBOSE);
  188. #endif
  189. #ifdef CONFIG_SA1111
  190. /* now that our DMA memory is actually so designated, we can free it */
  191. memblock_phys_free(PHYS_OFFSET, __pa(swapper_pg_dir) - PHYS_OFFSET);
  192. #endif
  193. /*
  194. * Check boundaries twice: Some fundamental inconsistencies can
  195. * be detected at build time already.
  196. */
  197. #ifdef CONFIG_MMU
  198. BUILD_BUG_ON(TASK_SIZE > MODULES_VADDR);
  199. BUG_ON(TASK_SIZE > MODULES_VADDR);
  200. #endif
  201. #ifdef CONFIG_HIGHMEM
  202. BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET);
  203. BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET);
  204. #endif
  205. }
  206. #ifdef CONFIG_STRICT_KERNEL_RWX
  207. struct section_perm {
  208. const char *name;
  209. unsigned long start;
  210. unsigned long end;
  211. pmdval_t mask;
  212. pmdval_t prot;
  213. pmdval_t clear;
  214. };
  215. /* First section-aligned location at or after __start_rodata. */
  216. extern char __start_rodata_section_aligned[];
  217. static struct section_perm nx_perms[] = {
  218. /* Make pages tables, etc before _stext RW (set NX). */
  219. {
  220. .name = "pre-text NX",
  221. .start = PAGE_OFFSET,
  222. .end = (unsigned long)_stext,
  223. .mask = ~PMD_SECT_XN,
  224. .prot = PMD_SECT_XN,
  225. },
  226. /* Make init RW (set NX). */
  227. {
  228. .name = "init NX",
  229. .start = (unsigned long)__init_begin,
  230. .end = (unsigned long)_sdata,
  231. .mask = ~PMD_SECT_XN,
  232. .prot = PMD_SECT_XN,
  233. },
  234. /* Make rodata NX (set RO in ro_perms below). */
  235. {
  236. .name = "rodata NX",
  237. .start = (unsigned long)__start_rodata_section_aligned,
  238. .end = (unsigned long)__init_begin,
  239. .mask = ~PMD_SECT_XN,
  240. .prot = PMD_SECT_XN,
  241. },
  242. };
  243. static struct section_perm ro_perms[] = {
  244. /* Make kernel code and rodata RX (set RO). */
  245. {
  246. .name = "text/rodata RO",
  247. .start = (unsigned long)_stext,
  248. .end = (unsigned long)__init_begin,
  249. #ifdef CONFIG_ARM_LPAE
  250. .mask = ~(L_PMD_SECT_RDONLY | PMD_SECT_AP2),
  251. .prot = L_PMD_SECT_RDONLY | PMD_SECT_AP2,
  252. #else
  253. .mask = ~(PMD_SECT_APX | PMD_SECT_AP_WRITE),
  254. .prot = PMD_SECT_APX | PMD_SECT_AP_WRITE,
  255. .clear = PMD_SECT_AP_WRITE,
  256. #endif
  257. },
  258. };
  259. /*
  260. * Updates section permissions only for the current mm (sections are
  261. * copied into each mm). During startup, this is the init_mm. Is only
  262. * safe to be called with preemption disabled, as under stop_machine().
  263. */
  264. static inline void section_update(unsigned long addr, pmdval_t mask,
  265. pmdval_t prot, struct mm_struct *mm)
  266. {
  267. pmd_t *pmd;
  268. pmd = pmd_offset(pud_offset(p4d_offset(pgd_offset(mm, addr), addr), addr), addr);
  269. #ifdef CONFIG_ARM_LPAE
  270. pmd[0] = __pmd((pmd_val(pmd[0]) & mask) | prot);
  271. #else
  272. if (addr & SECTION_SIZE)
  273. pmd[1] = __pmd((pmd_val(pmd[1]) & mask) | prot);
  274. else
  275. pmd[0] = __pmd((pmd_val(pmd[0]) & mask) | prot);
  276. #endif
  277. flush_pmd_entry(pmd);
  278. local_flush_tlb_kernel_range(addr, addr + SECTION_SIZE);
  279. }
  280. /* Make sure extended page tables are in use. */
  281. static inline bool arch_has_strict_perms(void)
  282. {
  283. if (cpu_architecture() < CPU_ARCH_ARMv6)
  284. return false;
  285. return !!(get_cr() & CR_XP);
  286. }
  287. static void set_section_perms(struct section_perm *perms, int n, bool set,
  288. struct mm_struct *mm)
  289. {
  290. size_t i;
  291. unsigned long addr;
  292. if (!arch_has_strict_perms())
  293. return;
  294. for (i = 0; i < n; i++) {
  295. if (!IS_ALIGNED(perms[i].start, SECTION_SIZE) ||
  296. !IS_ALIGNED(perms[i].end, SECTION_SIZE)) {
  297. pr_err("BUG: %s section %lx-%lx not aligned to %lx\n",
  298. perms[i].name, perms[i].start, perms[i].end,
  299. SECTION_SIZE);
  300. continue;
  301. }
  302. for (addr = perms[i].start;
  303. addr < perms[i].end;
  304. addr += SECTION_SIZE)
  305. section_update(addr, perms[i].mask,
  306. set ? perms[i].prot : perms[i].clear, mm);
  307. }
  308. }
  309. /*
  310. * update_sections_early intended to be called only through stop_machine
  311. * framework and executed by only one CPU while all other CPUs will spin and
  312. * wait, so no locking is required in this function.
  313. */
  314. static void update_sections_early(struct section_perm perms[], int n)
  315. {
  316. struct task_struct *t, *s;
  317. for_each_process(t) {
  318. if (t->flags & PF_KTHREAD)
  319. continue;
  320. for_each_thread(t, s)
  321. if (s->mm)
  322. set_section_perms(perms, n, true, s->mm);
  323. }
  324. set_section_perms(perms, n, true, current->active_mm);
  325. set_section_perms(perms, n, true, &init_mm);
  326. }
  327. static int __fix_kernmem_perms(void *unused)
  328. {
  329. update_sections_early(nx_perms, ARRAY_SIZE(nx_perms));
  330. return 0;
  331. }
  332. static void fix_kernmem_perms(void)
  333. {
  334. stop_machine(__fix_kernmem_perms, NULL, NULL);
  335. }
  336. static int __mark_rodata_ro(void *unused)
  337. {
  338. update_sections_early(ro_perms, ARRAY_SIZE(ro_perms));
  339. return 0;
  340. }
  341. void mark_rodata_ro(void)
  342. {
  343. stop_machine(__mark_rodata_ro, NULL, NULL);
  344. arm_debug_checkwx();
  345. }
  346. #else
  347. static inline void fix_kernmem_perms(void) { }
  348. #endif /* CONFIG_STRICT_KERNEL_RWX */
  349. void free_initmem(void)
  350. {
  351. fix_kernmem_perms();
  352. poison_init_mem(__init_begin, __init_end - __init_begin);
  353. if (!machine_is_integrator() && !machine_is_cintegrator())
  354. free_initmem_default(-1);
  355. }
  356. #ifdef CONFIG_BLK_DEV_INITRD
  357. void free_initrd_mem(unsigned long start, unsigned long end)
  358. {
  359. if (start == initrd_start)
  360. start = round_down(start, PAGE_SIZE);
  361. if (end == initrd_end)
  362. end = round_up(end, PAGE_SIZE);
  363. poison_init_mem((void *)start, PAGE_ALIGN(end) - start);
  364. free_reserved_area((void *)start, (void *)end, -1, "initrd");
  365. }
  366. #endif
  367. #ifdef CONFIG_EXECMEM
  368. #ifdef CONFIG_XIP_KERNEL
  369. /*
  370. * The XIP kernel text is mapped in the module area for modules and
  371. * some other stuff to work without any indirect relocations.
  372. * MODULES_VADDR is redefined here and not in asm/memory.h to avoid
  373. * recompiling the whole kernel when CONFIG_XIP_KERNEL is turned on/off.
  374. */
  375. #undef MODULES_VADDR
  376. #define MODULES_VADDR (((unsigned long)_exiprom + ~PMD_MASK) & PMD_MASK)
  377. #endif
  378. #ifdef CONFIG_MMU
  379. static struct execmem_info execmem_info __ro_after_init;
  380. struct execmem_info __init *execmem_arch_setup(void)
  381. {
  382. unsigned long fallback_start = 0, fallback_end = 0;
  383. if (IS_ENABLED(CONFIG_ARM_MODULE_PLTS)) {
  384. fallback_start = VMALLOC_START;
  385. fallback_end = VMALLOC_END;
  386. }
  387. execmem_info = (struct execmem_info){
  388. .ranges = {
  389. [EXECMEM_DEFAULT] = {
  390. .start = MODULES_VADDR,
  391. .end = MODULES_END,
  392. .pgprot = PAGE_KERNEL_EXEC,
  393. .alignment = 1,
  394. .fallback_start = fallback_start,
  395. .fallback_end = fallback_end,
  396. },
  397. },
  398. };
  399. return &execmem_info;
  400. }
  401. #endif /* CONFIG_MMU */
  402. #endif /* CONFIG_EXECMEM */