init.c 12 KB

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