init.c 20 KB

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