init.c 19 KB

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