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