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