init.c 18 KB

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