1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/arch/arm/mm/init.c 41da177e4SLinus Torvalds * 590072059SRussell King * Copyright (C) 1995-2005 Russell King 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds #include <linux/kernel.h> 81da177e4SLinus Torvalds #include <linux/errno.h> 91da177e4SLinus Torvalds #include <linux/swap.h> 101da177e4SLinus Torvalds #include <linux/init.h> 111da177e4SLinus Torvalds #include <linux/mman.h> 123f07c014SIngo Molnar #include <linux/sched/signal.h> 1329930025SIngo Molnar #include <linux/sched/task.h> 14dc28094bSPaul Gortmaker #include <linux/export.h> 151da177e4SLinus Torvalds #include <linux/nodemask.h> 161da177e4SLinus Torvalds #include <linux/initrd.h> 179eb8f674SGrant Likely #include <linux/of_fdt.h> 183835f6cbSNicolas Pitre #include <linux/highmem.h> 195a0e3ad6STejun Heo #include <linux/gfp.h> 202778f620SRussell King #include <linux/memblock.h> 210b1abd1fSChristoph Hellwig #include <linux/dma-map-ops.h> 22158e8bfeSAlessandro Rubini #include <linux/sizes.h> 2308925c2fSLaura Abbott #include <linux/stop_machine.h> 24ad3c7b18SChristoph Hellwig #include <linux/swiotlb.h> 251da177e4SLinus Torvalds 26b4b20ad8SRussell King #include <asm/cp15.h> 271da177e4SLinus Torvalds #include <asm/mach-types.h> 28716a3dc2SRussell King #include <asm/memblock.h> 29*a9ff6961SLinus Walleij #include <asm/page.h> 3093c02ab4SGrant Likely #include <asm/prom.h> 3137efe642SRussell King #include <asm/sections.h> 321da177e4SLinus Torvalds #include <asm/setup.h> 339110f3e7SBen Dooks (Codethink) #include <asm/set_memory.h> 341e6b4811SKees Cook #include <asm/system_info.h> 351da177e4SLinus Torvalds #include <asm/tlb.h> 36db9ef1afSFenkart/Bostandzhyan #include <asm/fixmap.h> 37a8e53c15SJinbum Park #include <asm/ptdump.h> 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds #include <asm/mach/arch.h> 401da177e4SLinus Torvalds #include <asm/mach/map.h> 411da177e4SLinus Torvalds 421b2e2b73SRussell King #include "mm.h" 431b2e2b73SRussell King 44b4b20ad8SRussell King #ifdef CONFIG_CPU_CP15_MMU 45b4b20ad8SRussell King unsigned long __init __clear_cr(unsigned long mask) 46b4b20ad8SRussell King { 47b4b20ad8SRussell King cr_alignment = cr_alignment & ~mask; 48b4b20ad8SRussell King return cr_alignment; 49b4b20ad8SRussell King } 50b4b20ad8SRussell King #endif 51b4b20ad8SRussell King 52b1ab95c6SFlorian Fainelli #ifdef CONFIG_BLK_DEV_INITRD 53012d1f4aSRussell King static int __init parse_tag_initrd(const struct tag *tag) 54012d1f4aSRussell King { 554ed89f22SRussell King pr_warn("ATAG_INITRD is deprecated; " 56012d1f4aSRussell King "please update your bootloader.\n"); 57012d1f4aSRussell King phys_initrd_start = __virt_to_phys(tag->u.initrd.start); 58012d1f4aSRussell King phys_initrd_size = tag->u.initrd.size; 59012d1f4aSRussell King return 0; 60012d1f4aSRussell King } 61012d1f4aSRussell King 62012d1f4aSRussell King __tagtable(ATAG_INITRD, parse_tag_initrd); 63012d1f4aSRussell King 64012d1f4aSRussell King static int __init parse_tag_initrd2(const struct tag *tag) 65012d1f4aSRussell King { 66012d1f4aSRussell King phys_initrd_start = tag->u.initrd.start; 67012d1f4aSRussell King phys_initrd_size = tag->u.initrd.size; 68012d1f4aSRussell King return 0; 69012d1f4aSRussell King } 70012d1f4aSRussell King 71012d1f4aSRussell King __tagtable(ATAG_INITRD2, parse_tag_initrd2); 72b1ab95c6SFlorian Fainelli #endif 731da177e4SLinus Torvalds 74f25b4b4cSRussell King static void __init find_limits(unsigned long *min, unsigned long *max_low, 75f25b4b4cSRussell King unsigned long *max_high) 76dde5828fSRussell King { 771c2f87c2SLaura Abbott *max_low = PFN_DOWN(memblock_get_current_limit()); 781c2f87c2SLaura Abbott *min = PFN_UP(memblock_start_of_DRAM()); 791c2f87c2SLaura Abbott *max_high = PFN_DOWN(memblock_end_of_DRAM()); 80dde5828fSRussell King } 81dde5828fSRussell King 82be20902bSRussell King #ifdef CONFIG_ZONE_DMA 8365032018SNicolas Pitre 84364230b9SRob Herring phys_addr_t arm_dma_zone_size __read_mostly; 8565032018SNicolas Pitre EXPORT_SYMBOL(arm_dma_zone_size); 8665032018SNicolas Pitre 87022ae537SRussell King /* 88022ae537SRussell King * The DMA mask corresponding to the maximum bus address allocatable 89022ae537SRussell King * using GFP_DMA. The default here places no restriction on DMA 90022ae537SRussell King * allocations. This must be the smallest DMA mask in the system, 91022ae537SRussell King * so a successful GFP_DMA allocation will always satisfy this. 92022ae537SRussell King */ 934986e5c7SMarek Szyprowski phys_addr_t arm_dma_limit; 944dcfa600SRussell King unsigned long arm_dma_pfn_limit; 95be20902bSRussell King #endif 96be20902bSRussell King 97ff69a4c8SRussell King void __init setup_dma_zone(const struct machine_desc *mdesc) 98c7909509SMarek Szyprowski { 99c7909509SMarek Szyprowski #ifdef CONFIG_ZONE_DMA 100c7909509SMarek Szyprowski if (mdesc->dma_zone_size) { 101c7909509SMarek Szyprowski arm_dma_zone_size = mdesc->dma_zone_size; 1026bcac805SRussell King arm_dma_limit = PHYS_OFFSET + arm_dma_zone_size - 1; 103c7909509SMarek Szyprowski } else 104c7909509SMarek Szyprowski arm_dma_limit = 0xffffffff; 1054dcfa600SRussell King arm_dma_pfn_limit = arm_dma_limit >> PAGE_SHIFT; 106c7909509SMarek Szyprowski #endif 107c7909509SMarek Szyprowski } 108c7909509SMarek Szyprowski 10984f452b1SSantosh Shilimkar static void __init zone_sizes_init(unsigned long min, unsigned long max_low, 110a2c54d2aSRussell King unsigned long max_high) 111b7a69ac3SRussell King { 112a32c1c61SMike Rapoport unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 }; 11390072059SRussell King 11465032018SNicolas Pitre #ifdef CONFIG_ZONE_DMA 115a32c1c61SMike Rapoport max_zone_pfn[ZONE_DMA] = min(arm_dma_pfn_limit, max_low); 116be20902bSRussell King #endif 117a32c1c61SMike Rapoport max_zone_pfn[ZONE_NORMAL] = max_low; 118a32c1c61SMike Rapoport #ifdef CONFIG_HIGHMEM 119a32c1c61SMike Rapoport max_zone_pfn[ZONE_HIGHMEM] = max_high; 120a32c1c61SMike Rapoport #endif 121a32c1c61SMike Rapoport free_area_init(max_zone_pfn); 12290072059SRussell King } 12390072059SRussell King 1247b7bf499SWill Deacon #ifdef CONFIG_HAVE_ARCH_PFN_VALID 125b7cfda9fSRussell King int pfn_valid(unsigned long pfn) 126b7cfda9fSRussell King { 1275b3efa4fSzhaoyang phys_addr_t addr = __pfn_to_phys(pfn); 128a4d5613cSMike Rapoport unsigned long pageblock_size = PAGE_SIZE * pageblock_nr_pages; 1295b3efa4fSzhaoyang 1305b3efa4fSzhaoyang if (__phys_to_pfn(addr) != pfn) 1315b3efa4fSzhaoyang return 0; 1325b3efa4fSzhaoyang 133a4d5613cSMike Rapoport /* 134a4d5613cSMike Rapoport * If address less than pageblock_size bytes away from a present 135a4d5613cSMike Rapoport * memory chunk there still will be a memory map entry for it 136a4d5613cSMike Rapoport * because we round freed memory map to the pageblock boundaries. 137a4d5613cSMike Rapoport */ 138a4d5613cSMike Rapoport if (memblock_overlaps_region(&memblock.memory, 139a4d5613cSMike Rapoport ALIGN_DOWN(addr, pageblock_size), 140a4d5613cSMike Rapoport pageblock_size)) 141a4d5613cSMike Rapoport return 1; 142a4d5613cSMike Rapoport 143a4d5613cSMike Rapoport return 0; 144b7cfda9fSRussell King } 145b7cfda9fSRussell King EXPORT_SYMBOL(pfn_valid); 1467b7bf499SWill Deacon #endif 147657e12fdSRussell King 148716a3dc2SRussell King static bool arm_memblock_steal_permitted = true; 149716a3dc2SRussell King 150bc2827d0SRussell King phys_addr_t __init arm_memblock_steal(phys_addr_t size, phys_addr_t align) 151716a3dc2SRussell King { 152716a3dc2SRussell King phys_addr_t phys; 153716a3dc2SRussell King 154716a3dc2SRussell King BUG_ON(!arm_memblock_steal_permitted); 155716a3dc2SRussell King 156f240ec09SMike Rapoport phys = memblock_phys_alloc(size, align); 157ecc3e771SMike Rapoport if (!phys) 158ecc3e771SMike Rapoport panic("Failed to steal %pa bytes at %pS\n", 159ecc3e771SMike Rapoport &size, (void *)_RET_IP_); 160ecc3e771SMike Rapoport 1613ecc6834SMike Rapoport memblock_phys_free(phys, size); 162716a3dc2SRussell King memblock_remove(phys, size); 163716a3dc2SRussell King 164716a3dc2SRussell King return phys; 165716a3dc2SRussell King } 166716a3dc2SRussell King 1675f41f919SMarek Szyprowski #ifdef CONFIG_CPU_ICACHE_MISMATCH_WORKAROUND 1685f41f919SMarek Szyprowski void check_cpu_icache_size(int cpuid) 1695f41f919SMarek Szyprowski { 1705f41f919SMarek Szyprowski u32 size, ctr; 1715f41f919SMarek Szyprowski 1725f41f919SMarek Szyprowski asm("mrc p15, 0, %0, c0, c0, 1" : "=r" (ctr)); 1735f41f919SMarek Szyprowski 1745f41f919SMarek Szyprowski size = 1 << ((ctr & 0xf) + 2); 1755f41f919SMarek Szyprowski if (cpuid != 0 && icache_size != size) 1765f41f919SMarek Szyprowski pr_info("CPU%u: detected I-Cache line size mismatch, workaround enabled\n", 1775f41f919SMarek Szyprowski cpuid); 1785f41f919SMarek Szyprowski if (icache_size > size) 1795f41f919SMarek Szyprowski icache_size = size; 1805f41f919SMarek Szyprowski } 1815f41f919SMarek Szyprowski #endif 1825f41f919SMarek Szyprowski 18339286248SRussell King void __init arm_memblock_init(const struct machine_desc *mdesc) 18439286248SRussell King { 18539286248SRussell King /* Register the kernel text, kernel data and initrd with memblock. */ 18639286248SRussell King memblock_reserve(__pa(KERNEL_START), KERNEL_END - KERNEL_START); 18739286248SRussell King 188e46e45f0SWang Kefeng reserve_initrd_mem(); 1892778f620SRussell King 1902778f620SRussell King arm_mm_memblock_reserve(); 1912778f620SRussell King 1928d717a52SRussell King /* reserve any platform specific memblock areas */ 1938d717a52SRussell King if (mdesc->reserve) 1948d717a52SRussell King mdesc->reserve(); 1958d717a52SRussell King 196bcedb5f9SMarek Szyprowski early_init_fdt_scan_reserved_mem(); 197bcedb5f9SMarek Szyprowski 19899a468d7SGeorge G. Davis /* reserve memory for DMA contiguous allocations */ 19995b0e655SMarek Szyprowski dma_contiguous_reserve(arm_dma_limit); 200c7909509SMarek Szyprowski 201716a3dc2SRussell King arm_memblock_steal_permitted = false; 2022778f620SRussell King memblock_dump_all(); 2032778f620SRussell King } 2042778f620SRussell King 2058d717a52SRussell King void __init bootmem_init(void) 20690072059SRussell King { 2078e58caefSGrygorii Strashko memblock_allow_resize(); 208dde5828fSRussell King 209071d184aSDoug Berger find_limits(&min_low_pfn, &max_low_pfn, &max_pfn); 210dde5828fSRussell King 211071d184aSDoug Berger early_memtest((phys_addr_t)min_low_pfn << PAGE_SHIFT, 212071d184aSDoug Berger (phys_addr_t)max_low_pfn << PAGE_SHIFT); 213d30eae47SVladimir Murzin 214b7a69ac3SRussell King /* 215c89ab04fSMike Rapoport * sparse_init() tries to allocate memory from memblock, so must be 216c89ab04fSMike Rapoport * done after the fixed reservations 217b7a69ac3SRussell King */ 218b7a69ac3SRussell King sparse_init(); 219b7a69ac3SRussell King 220b7a69ac3SRussell King /* 221a32c1c61SMike Rapoport * Now free the memory - free_area_init needs 222b7a69ac3SRussell King * the sparse mem_map arrays initialized by sparse_init() 223b7a69ac3SRussell King * for memmap_init_zone(), otherwise all PFNs are invalid. 224b7a69ac3SRussell King */ 225071d184aSDoug Berger zone_sizes_init(min_low_pfn, max_low_pfn, max_pfn); 22690072059SRussell King } 22790072059SRussell King 22854d52573SStephen Boyd /* 22954d52573SStephen Boyd * Poison init memory with an undefined instruction (ARM) or a branch to an 23054d52573SStephen Boyd * undefined instruction (Thumb). 23154d52573SStephen Boyd */ 23254d52573SStephen Boyd static inline void poison_init_mem(void *s, size_t count) 23354d52573SStephen Boyd { 23454d52573SStephen Boyd u32 *p = (u32 *)s; 235bf912d99SJamie Iles for (; count != 0; count -= 4) 23654d52573SStephen Boyd *p++ = 0xe7fddef0; 23754d52573SStephen Boyd } 23854d52573SStephen Boyd 239d0e775afSRussell King static void __init free_highpages(void) 240d0e775afSRussell King { 241d0e775afSRussell King #ifdef CONFIG_HIGHMEM 24226ba47b1SSantosh Shilimkar unsigned long max_low = max_low_pfn; 243cddb5ddfSMike Rapoport phys_addr_t range_start, range_end; 244cddb5ddfSMike Rapoport u64 i; 245d0e775afSRussell King 246d0e775afSRussell King /* set highmem page free */ 247cddb5ddfSMike Rapoport for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, 248cddb5ddfSMike Rapoport &range_start, &range_end, NULL) { 249b9bc3670SArd Biesheuvel unsigned long start = PFN_UP(range_start); 250b9bc3670SArd Biesheuvel unsigned long end = PFN_DOWN(range_end); 251df4f14c7SRussell King 252df4f14c7SRussell King /* Ignore complete lowmem entries */ 253df4f14c7SRussell King if (end <= max_low) 254df4f14c7SRussell King continue; 255df4f14c7SRussell King 256df4f14c7SRussell King /* Truncate partial highmem entries */ 257df4f14c7SRussell King if (start < max_low) 258df4f14c7SRussell King start = max_low; 259df4f14c7SRussell King 260cddb5ddfSMike Rapoport for (; start < end; start++) 261cddb5ddfSMike Rapoport free_highmem_page(pfn_to_page(start)); 262d0e775afSRussell King } 263d0e775afSRussell King #endif 264d0e775afSRussell King } 265d0e775afSRussell King 2661da177e4SLinus Torvalds /* 2671da177e4SLinus Torvalds * mem_init() marks the free areas in the mem_map and tells us how much 2681da177e4SLinus Torvalds * memory is free. This is done after various parts of the system have 2691da177e4SLinus Torvalds * claimed their memory after the kernel image. 2701da177e4SLinus Torvalds */ 2711da177e4SLinus Torvalds void __init mem_init(void) 2721da177e4SLinus Torvalds { 273ad3c7b18SChristoph Hellwig #ifdef CONFIG_ARM_LPAE 274c6af2aa9SChristoph Hellwig swiotlb_init(max_pfn > arm_dma_pfn_limit, SWIOTLB_VERBOSE); 275ad3c7b18SChristoph Hellwig #endif 276ad3c7b18SChristoph Hellwig 277b3ba41f2SSantosh Shilimkar set_max_mapnr(pfn_to_page(max_pfn) - mem_map); 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds /* this will put all unused low memory onto the freelists */ 280c6ffc5caSMike Rapoport memblock_free_all(); 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds #ifdef CONFIG_SA1111 2831da177e4SLinus Torvalds /* now that our DMA memory is actually so designated, we can free it */ 284bfd65dd9SLinus Torvalds free_reserved_area(__va(PHYS_OFFSET), swapper_pg_dir, -1, NULL); 2851da177e4SLinus Torvalds #endif 2861da177e4SLinus Torvalds 287d0e775afSRussell King free_highpages(); 2883835f6cbSNicolas Pitre 289a1839272SFenkart/Bostandzhyan /* 290a1839272SFenkart/Bostandzhyan * Check boundaries twice: Some fundamental inconsistencies can 291a1839272SFenkart/Bostandzhyan * be detected at build time already. 292a1839272SFenkart/Bostandzhyan */ 293a1839272SFenkart/Bostandzhyan #ifdef CONFIG_MMU 294a1839272SFenkart/Bostandzhyan BUILD_BUG_ON(TASK_SIZE > MODULES_VADDR); 295a1839272SFenkart/Bostandzhyan BUG_ON(TASK_SIZE > MODULES_VADDR); 296a1839272SFenkart/Bostandzhyan #endif 297a1839272SFenkart/Bostandzhyan 298a1839272SFenkart/Bostandzhyan #ifdef CONFIG_HIGHMEM 299a1839272SFenkart/Bostandzhyan BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET); 300a1839272SFenkart/Bostandzhyan BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET); 301a1839272SFenkart/Bostandzhyan #endif 3021da177e4SLinus Torvalds } 3031da177e4SLinus Torvalds 3040f5bf6d0SLaura Abbott #ifdef CONFIG_STRICT_KERNEL_RWX 3051e6b4811SKees Cook struct section_perm { 30625362dc4SKees Cook const char *name; 3071e6b4811SKees Cook unsigned long start; 3081e6b4811SKees Cook unsigned long end; 3091e6b4811SKees Cook pmdval_t mask; 3101e6b4811SKees Cook pmdval_t prot; 31180d6b0c2SKees Cook pmdval_t clear; 3121e6b4811SKees Cook }; 3131e6b4811SKees Cook 31464ac2e74SKees Cook /* First section-aligned location at or after __start_rodata. */ 31564ac2e74SKees Cook extern char __start_rodata_section_aligned[]; 31664ac2e74SKees Cook 31780d6b0c2SKees Cook static struct section_perm nx_perms[] = { 3181e6b4811SKees Cook /* Make pages tables, etc before _stext RW (set NX). */ 3191e6b4811SKees Cook { 32025362dc4SKees Cook .name = "pre-text NX", 3211e6b4811SKees Cook .start = PAGE_OFFSET, 3221e6b4811SKees Cook .end = (unsigned long)_stext, 3231e6b4811SKees Cook .mask = ~PMD_SECT_XN, 3241e6b4811SKees Cook .prot = PMD_SECT_XN, 3251e6b4811SKees Cook }, 3261e6b4811SKees Cook /* Make init RW (set NX). */ 3271e6b4811SKees Cook { 32825362dc4SKees Cook .name = "init NX", 3291e6b4811SKees Cook .start = (unsigned long)__init_begin, 3301e6b4811SKees Cook .end = (unsigned long)_sdata, 3311e6b4811SKees Cook .mask = ~PMD_SECT_XN, 3321e6b4811SKees Cook .prot = PMD_SECT_XN, 3331e6b4811SKees Cook }, 33480d6b0c2SKees Cook /* Make rodata NX (set RO in ro_perms below). */ 33580d6b0c2SKees Cook { 33625362dc4SKees Cook .name = "rodata NX", 33764ac2e74SKees Cook .start = (unsigned long)__start_rodata_section_aligned, 33880d6b0c2SKees Cook .end = (unsigned long)__init_begin, 33980d6b0c2SKees Cook .mask = ~PMD_SECT_XN, 34080d6b0c2SKees Cook .prot = PMD_SECT_XN, 34180d6b0c2SKees Cook }, 3421e6b4811SKees Cook }; 3431e6b4811SKees Cook 34480d6b0c2SKees Cook static struct section_perm ro_perms[] = { 34580d6b0c2SKees Cook /* Make kernel code and rodata RX (set RO). */ 34680d6b0c2SKees Cook { 34725362dc4SKees Cook .name = "text/rodata RO", 34880d6b0c2SKees Cook .start = (unsigned long)_stext, 34980d6b0c2SKees Cook .end = (unsigned long)__init_begin, 35080d6b0c2SKees Cook #ifdef CONFIG_ARM_LPAE 351400eeffaSPhilip Derrin .mask = ~(L_PMD_SECT_RDONLY | PMD_SECT_AP2), 352400eeffaSPhilip Derrin .prot = L_PMD_SECT_RDONLY | PMD_SECT_AP2, 35380d6b0c2SKees Cook #else 35480d6b0c2SKees Cook .mask = ~(PMD_SECT_APX | PMD_SECT_AP_WRITE), 35580d6b0c2SKees Cook .prot = PMD_SECT_APX | PMD_SECT_AP_WRITE, 35680d6b0c2SKees Cook .clear = PMD_SECT_AP_WRITE, 35780d6b0c2SKees Cook #endif 35880d6b0c2SKees Cook }, 35980d6b0c2SKees Cook }; 36080d6b0c2SKees Cook 3611e6b4811SKees Cook /* 3621e6b4811SKees Cook * Updates section permissions only for the current mm (sections are 3631e6b4811SKees Cook * copied into each mm). During startup, this is the init_mm. Is only 3641e6b4811SKees Cook * safe to be called with preemption disabled, as under stop_machine(). 3651e6b4811SKees Cook */ 3661e6b4811SKees Cook static inline void section_update(unsigned long addr, pmdval_t mask, 36708925c2fSLaura Abbott pmdval_t prot, struct mm_struct *mm) 3681e6b4811SKees Cook { 3691e6b4811SKees Cook pmd_t *pmd; 3701e6b4811SKees Cook 37184e6ffb2SMike Rapoport pmd = pmd_offset(pud_offset(p4d_offset(pgd_offset(mm, addr), addr), addr), addr); 3721e6b4811SKees Cook 3731e6b4811SKees Cook #ifdef CONFIG_ARM_LPAE 3741e6b4811SKees Cook pmd[0] = __pmd((pmd_val(pmd[0]) & mask) | prot); 3751e6b4811SKees Cook #else 3761e6b4811SKees Cook if (addr & SECTION_SIZE) 3771e6b4811SKees Cook pmd[1] = __pmd((pmd_val(pmd[1]) & mask) | prot); 3781e6b4811SKees Cook else 3791e6b4811SKees Cook pmd[0] = __pmd((pmd_val(pmd[0]) & mask) | prot); 3801e6b4811SKees Cook #endif 3811e6b4811SKees Cook flush_pmd_entry(pmd); 3821e6b4811SKees Cook local_flush_tlb_kernel_range(addr, addr + SECTION_SIZE); 3831e6b4811SKees Cook } 3841e6b4811SKees Cook 3851e6b4811SKees Cook /* Make sure extended page tables are in use. */ 3861e6b4811SKees Cook static inline bool arch_has_strict_perms(void) 3871e6b4811SKees Cook { 3881e6b4811SKees Cook if (cpu_architecture() < CPU_ARCH_ARMv6) 3891e6b4811SKees Cook return false; 3901e6b4811SKees Cook 3911e6b4811SKees Cook return !!(get_cr() & CR_XP); 3921e6b4811SKees Cook } 3931e6b4811SKees Cook 394ea5379beSBen Dooks (Codethink) static void set_section_perms(struct section_perm *perms, int n, bool set, 39508925c2fSLaura Abbott struct mm_struct *mm) 39608925c2fSLaura Abbott { 39708925c2fSLaura Abbott size_t i; 39808925c2fSLaura Abbott unsigned long addr; 39908925c2fSLaura Abbott 40008925c2fSLaura Abbott if (!arch_has_strict_perms()) 40108925c2fSLaura Abbott return; 40208925c2fSLaura Abbott 40308925c2fSLaura Abbott for (i = 0; i < n; i++) { 40408925c2fSLaura Abbott if (!IS_ALIGNED(perms[i].start, SECTION_SIZE) || 40508925c2fSLaura Abbott !IS_ALIGNED(perms[i].end, SECTION_SIZE)) { 40625362dc4SKees Cook pr_err("BUG: %s section %lx-%lx not aligned to %lx\n", 40725362dc4SKees Cook perms[i].name, perms[i].start, perms[i].end, 40808925c2fSLaura Abbott SECTION_SIZE); 40908925c2fSLaura Abbott continue; 4101e6b4811SKees Cook } 4111e6b4811SKees Cook 41208925c2fSLaura Abbott for (addr = perms[i].start; 41308925c2fSLaura Abbott addr < perms[i].end; 41408925c2fSLaura Abbott addr += SECTION_SIZE) 41508925c2fSLaura Abbott section_update(addr, perms[i].mask, 41608925c2fSLaura Abbott set ? perms[i].prot : perms[i].clear, mm); 41708925c2fSLaura Abbott } 41808925c2fSLaura Abbott 41908925c2fSLaura Abbott } 42008925c2fSLaura Abbott 42111ce4b33SGrygorii Strashko /** 42211ce4b33SGrygorii Strashko * update_sections_early intended to be called only through stop_machine 42311ce4b33SGrygorii Strashko * framework and executed by only one CPU while all other CPUs will spin and 42411ce4b33SGrygorii Strashko * wait, so no locking is required in this function. 42511ce4b33SGrygorii Strashko */ 42608925c2fSLaura Abbott static void update_sections_early(struct section_perm perms[], int n) 4271e6b4811SKees Cook { 42808925c2fSLaura Abbott struct task_struct *t, *s; 42908925c2fSLaura Abbott 43008925c2fSLaura Abbott for_each_process(t) { 43108925c2fSLaura Abbott if (t->flags & PF_KTHREAD) 43208925c2fSLaura Abbott continue; 43308925c2fSLaura Abbott for_each_thread(t, s) 434c51bc12dSDoug Berger if (s->mm) 43508925c2fSLaura Abbott set_section_perms(perms, n, true, s->mm); 43608925c2fSLaura Abbott } 43708925c2fSLaura Abbott set_section_perms(perms, n, true, current->active_mm); 43808925c2fSLaura Abbott set_section_perms(perms, n, true, &init_mm); 43908925c2fSLaura Abbott } 44008925c2fSLaura Abbott 44111ce4b33SGrygorii Strashko static int __fix_kernmem_perms(void *unused) 44208925c2fSLaura Abbott { 44308925c2fSLaura Abbott update_sections_early(nx_perms, ARRAY_SIZE(nx_perms)); 44408925c2fSLaura Abbott return 0; 44508925c2fSLaura Abbott } 44608925c2fSLaura Abbott 44711ce4b33SGrygorii Strashko static void fix_kernmem_perms(void) 44808925c2fSLaura Abbott { 44908925c2fSLaura Abbott stop_machine(__fix_kernmem_perms, NULL, NULL); 4501e6b4811SKees Cook } 45180d6b0c2SKees Cook 45211ce4b33SGrygorii Strashko static int __mark_rodata_ro(void *unused) 45308925c2fSLaura Abbott { 45408925c2fSLaura Abbott update_sections_early(ro_perms, ARRAY_SIZE(ro_perms)); 45508925c2fSLaura Abbott return 0; 45608925c2fSLaura Abbott } 45708925c2fSLaura Abbott 45880d6b0c2SKees Cook void mark_rodata_ro(void) 45980d6b0c2SKees Cook { 46008925c2fSLaura Abbott stop_machine(__mark_rodata_ro, NULL, NULL); 461a8e53c15SJinbum Park debug_checkwx(); 46280d6b0c2SKees Cook } 46380d6b0c2SKees Cook 4641e6b4811SKees Cook #else 4651e6b4811SKees Cook static inline void fix_kernmem_perms(void) { } 4660f5bf6d0SLaura Abbott #endif /* CONFIG_STRICT_KERNEL_RWX */ 4671e6b4811SKees Cook 4681e6b4811SKees Cook void free_initmem(void) 4691e6b4811SKees Cook { 4701e6b4811SKees Cook fix_kernmem_perms(); 471bc581770SLinus Walleij 47254d52573SStephen Boyd poison_init_mem(__init_begin, __init_end - __init_begin); 4736db015e4SNicolas Pitre if (!machine_is_integrator() && !machine_is_cintegrator()) 474dbe67df4SJiang Liu free_initmem_default(-1); 4751da177e4SLinus Torvalds } 4761da177e4SLinus Torvalds 4771da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_INITRD 4781da177e4SLinus Torvalds void free_initrd_mem(unsigned long start, unsigned long end) 4791da177e4SLinus Torvalds { 480421520baSYalin Wang if (start == initrd_start) 481421520baSYalin Wang start = round_down(start, PAGE_SIZE); 482421520baSYalin Wang if (end == initrd_end) 483421520baSYalin Wang end = round_up(end, PAGE_SIZE); 484421520baSYalin Wang 48554d52573SStephen Boyd poison_init_mem((void *)start, PAGE_ALIGN(end) - start); 486dbe67df4SJiang Liu free_reserved_area((void *)start, (void *)end, -1, "initrd"); 4871da177e4SLinus Torvalds } 4881da177e4SLinus Torvalds #endif 489