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> 21c7909509SMarek Szyprowski #include <linux/dma-contiguous.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> 29d2ca5f24SAfzal Mohammed #include <asm/memory.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); 1285b3efa4fSzhaoyang 1295b3efa4fSzhaoyang if (__phys_to_pfn(addr) != pfn) 1305b3efa4fSzhaoyang return 0; 1315b3efa4fSzhaoyang 132032be728SClemens Gruber return memblock_is_map_memory(addr); 133b7cfda9fSRussell King } 134b7cfda9fSRussell King EXPORT_SYMBOL(pfn_valid); 1357b7bf499SWill Deacon #endif 136657e12fdSRussell King 137716a3dc2SRussell King static bool arm_memblock_steal_permitted = true; 138716a3dc2SRussell King 139bc2827d0SRussell King phys_addr_t __init arm_memblock_steal(phys_addr_t size, phys_addr_t align) 140716a3dc2SRussell King { 141716a3dc2SRussell King phys_addr_t phys; 142716a3dc2SRussell King 143716a3dc2SRussell King BUG_ON(!arm_memblock_steal_permitted); 144716a3dc2SRussell King 145f240ec09SMike Rapoport phys = memblock_phys_alloc(size, align); 146ecc3e771SMike Rapoport if (!phys) 147ecc3e771SMike Rapoport panic("Failed to steal %pa bytes at %pS\n", 148ecc3e771SMike Rapoport &size, (void *)_RET_IP_); 149ecc3e771SMike Rapoport 150716a3dc2SRussell King memblock_free(phys, size); 151716a3dc2SRussell King memblock_remove(phys, size); 152716a3dc2SRussell King 153716a3dc2SRussell King return phys; 154716a3dc2SRussell King } 155716a3dc2SRussell King 15639286248SRussell King static void __init arm_initrd_init(void) 1572778f620SRussell King { 1582778f620SRussell King #ifdef CONFIG_BLK_DEV_INITRD 159cdcc5fa0SRussell King phys_addr_t start; 160cdcc5fa0SRussell King unsigned long size; 161cdcc5fa0SRussell King 1624c235cb9SBen Peddell initrd_start = initrd_end = 0; 16368b32f36SRussell King 16468b32f36SRussell King if (!phys_initrd_size) 16568b32f36SRussell King return; 16668b32f36SRussell King 167cdcc5fa0SRussell King /* 168cdcc5fa0SRussell King * Round the memory region to page boundaries as per free_initrd_mem() 169cdcc5fa0SRussell King * This allows us to detect whether the pages overlapping the initrd 170cdcc5fa0SRussell King * are in use, but more importantly, reserves the entire set of pages 171cdcc5fa0SRussell King * as we don't want these pages allocated for other purposes. 172cdcc5fa0SRussell King */ 173cdcc5fa0SRussell King start = round_down(phys_initrd_start, PAGE_SIZE); 174cdcc5fa0SRussell King size = phys_initrd_size + (phys_initrd_start - start); 175cdcc5fa0SRussell King size = round_up(size, PAGE_SIZE); 176cdcc5fa0SRussell King 177cdcc5fa0SRussell King if (!memblock_is_region_memory(start, size)) { 178de22cc6eSVitaly Andrianov pr_err("INITRD: 0x%08llx+0x%08lx is not a memory region - disabling initrd\n", 179cdcc5fa0SRussell King (u64)start, size); 18068b32f36SRussell King return; 1818f4b8c76SRussell King } 18268b32f36SRussell King 183cdcc5fa0SRussell King if (memblock_is_region_reserved(start, size)) { 184de22cc6eSVitaly Andrianov pr_err("INITRD: 0x%08llx+0x%08lx overlaps in-use memory region - disabling initrd\n", 185cdcc5fa0SRussell King (u64)start, size); 18668b32f36SRussell King return; 187b0a2679dSRussell King } 18868b32f36SRussell King 189cdcc5fa0SRussell King memblock_reserve(start, size); 1902778f620SRussell King 1912778f620SRussell King /* Now convert initrd to virtual addresses */ 1922778f620SRussell King initrd_start = __phys_to_virt(phys_initrd_start); 1932778f620SRussell King initrd_end = initrd_start + phys_initrd_size; 1942778f620SRussell King #endif 19539286248SRussell King } 19639286248SRussell King 1975f41f919SMarek Szyprowski #ifdef CONFIG_CPU_ICACHE_MISMATCH_WORKAROUND 1985f41f919SMarek Szyprowski void check_cpu_icache_size(int cpuid) 1995f41f919SMarek Szyprowski { 2005f41f919SMarek Szyprowski u32 size, ctr; 2015f41f919SMarek Szyprowski 2025f41f919SMarek Szyprowski asm("mrc p15, 0, %0, c0, c0, 1" : "=r" (ctr)); 2035f41f919SMarek Szyprowski 2045f41f919SMarek Szyprowski size = 1 << ((ctr & 0xf) + 2); 2055f41f919SMarek Szyprowski if (cpuid != 0 && icache_size != size) 2065f41f919SMarek Szyprowski pr_info("CPU%u: detected I-Cache line size mismatch, workaround enabled\n", 2075f41f919SMarek Szyprowski cpuid); 2085f41f919SMarek Szyprowski if (icache_size > size) 2095f41f919SMarek Szyprowski icache_size = size; 2105f41f919SMarek Szyprowski } 2115f41f919SMarek Szyprowski #endif 2125f41f919SMarek Szyprowski 21339286248SRussell King void __init arm_memblock_init(const struct machine_desc *mdesc) 21439286248SRussell King { 21539286248SRussell King /* Register the kernel text, kernel data and initrd with memblock. */ 21639286248SRussell King memblock_reserve(__pa(KERNEL_START), KERNEL_END - KERNEL_START); 21739286248SRussell King 21839286248SRussell King arm_initrd_init(); 2192778f620SRussell King 2202778f620SRussell King arm_mm_memblock_reserve(); 2212778f620SRussell King 2228d717a52SRussell King /* reserve any platform specific memblock areas */ 2238d717a52SRussell King if (mdesc->reserve) 2248d717a52SRussell King mdesc->reserve(); 2258d717a52SRussell King 22624bbd929SArd Biesheuvel early_init_fdt_reserve_self(); 227bcedb5f9SMarek Szyprowski early_init_fdt_scan_reserved_mem(); 228bcedb5f9SMarek Szyprowski 22999a468d7SGeorge G. Davis /* reserve memory for DMA contiguous allocations */ 23095b0e655SMarek Szyprowski dma_contiguous_reserve(arm_dma_limit); 231c7909509SMarek Szyprowski 232716a3dc2SRussell King arm_memblock_steal_permitted = false; 2332778f620SRussell King memblock_dump_all(); 2342778f620SRussell King } 2352778f620SRussell King 2368d717a52SRussell King void __init bootmem_init(void) 23790072059SRussell King { 2388e58caefSGrygorii Strashko memblock_allow_resize(); 239dde5828fSRussell King 240071d184aSDoug Berger find_limits(&min_low_pfn, &max_low_pfn, &max_pfn); 241dde5828fSRussell King 242071d184aSDoug Berger early_memtest((phys_addr_t)min_low_pfn << PAGE_SHIFT, 243071d184aSDoug Berger (phys_addr_t)max_low_pfn << PAGE_SHIFT); 244d30eae47SVladimir Murzin 245b7a69ac3SRussell King /* 246c89ab04fSMike Rapoport * sparse_init() tries to allocate memory from memblock, so must be 247c89ab04fSMike Rapoport * done after the fixed reservations 248b7a69ac3SRussell King */ 249b7a69ac3SRussell King sparse_init(); 250b7a69ac3SRussell King 251b7a69ac3SRussell King /* 252a32c1c61SMike Rapoport * Now free the memory - free_area_init needs 253b7a69ac3SRussell King * the sparse mem_map arrays initialized by sparse_init() 254b7a69ac3SRussell King * for memmap_init_zone(), otherwise all PFNs are invalid. 255b7a69ac3SRussell King */ 256071d184aSDoug Berger zone_sizes_init(min_low_pfn, max_low_pfn, max_pfn); 25790072059SRussell King } 25890072059SRussell King 25954d52573SStephen Boyd /* 26054d52573SStephen Boyd * Poison init memory with an undefined instruction (ARM) or a branch to an 26154d52573SStephen Boyd * undefined instruction (Thumb). 26254d52573SStephen Boyd */ 26354d52573SStephen Boyd static inline void poison_init_mem(void *s, size_t count) 26454d52573SStephen Boyd { 26554d52573SStephen Boyd u32 *p = (u32 *)s; 266bf912d99SJamie Iles for (; count != 0; count -= 4) 26754d52573SStephen Boyd *p++ = 0xe7fddef0; 26854d52573SStephen Boyd } 26954d52573SStephen Boyd 27031f3010eSOlof Johansson static inline void __init 271be370302SRussell King free_memmap(unsigned long start_pfn, unsigned long end_pfn) 272a013053dSRussell King { 273a013053dSRussell King struct page *start_pg, *end_pg; 27456bc6286SVitaly Andrianov phys_addr_t pg, pgend; 275a013053dSRussell King 276a013053dSRussell King /* 277a013053dSRussell King * Convert start_pfn/end_pfn to a struct page pointer. 278a013053dSRussell King */ 2793257f43dSCatalin Marinas start_pg = pfn_to_page(start_pfn - 1) + 1; 2809af386c8SWill Deacon end_pg = pfn_to_page(end_pfn - 1) + 1; 281a013053dSRussell King 282a013053dSRussell King /* 283a013053dSRussell King * Convert to physical addresses, and 284a013053dSRussell King * round start upwards and end downwards. 285a013053dSRussell King */ 28656bc6286SVitaly Andrianov pg = PAGE_ALIGN(__pa(start_pg)); 28756bc6286SVitaly Andrianov pgend = __pa(end_pg) & PAGE_MASK; 288a013053dSRussell King 289a013053dSRussell King /* 290a013053dSRussell King * If there are free pages between these, 291a013053dSRussell King * free the section of the memmap array. 292a013053dSRussell King */ 293a013053dSRussell King if (pg < pgend) 294cfb66586SSantosh Shilimkar memblock_free_early(pg, pgend - pg); 295a013053dSRussell King } 296a013053dSRussell King 297a013053dSRussell King /* 298a013053dSRussell King * The mem_map array can get very big. Free the unused area of the memory map. 299a013053dSRussell King */ 3001c2f87c2SLaura Abbott static void __init free_unused_memmap(void) 301a013053dSRussell King { 3021c2f87c2SLaura Abbott unsigned long start, prev_end = 0; 3031c2f87c2SLaura Abbott struct memblock_region *reg; 304a013053dSRussell King 305a013053dSRussell King /* 3063260e529SMichael Bohan * This relies on each bank being in address order. 3073260e529SMichael Bohan * The banks are sorted previously in bootmem_init(). 308a013053dSRussell King */ 3091c2f87c2SLaura Abbott for_each_memblock(memory, reg) { 3101c2f87c2SLaura Abbott start = memblock_region_memory_base_pfn(reg); 311a013053dSRussell King 3129af386c8SWill Deacon #ifdef CONFIG_SPARSEMEM 3139af386c8SWill Deacon /* 3149af386c8SWill Deacon * Take care not to free memmap entries that don't exist 3159af386c8SWill Deacon * due to SPARSEMEM sections which aren't present. 3169af386c8SWill Deacon */ 3171c2f87c2SLaura Abbott start = min(start, 3181c2f87c2SLaura Abbott ALIGN(prev_end, PAGES_PER_SECTION)); 319002ea9eeSLinus Walleij #else 320002ea9eeSLinus Walleij /* 321002ea9eeSLinus Walleij * Align down here since the VM subsystem insists that the 322002ea9eeSLinus Walleij * memmap entries are valid from the bank start aligned to 323002ea9eeSLinus Walleij * MAX_ORDER_NR_PAGES. 324002ea9eeSLinus Walleij */ 3251c2f87c2SLaura Abbott start = round_down(start, MAX_ORDER_NR_PAGES); 3269af386c8SWill Deacon #endif 327a013053dSRussell King /* 328a013053dSRussell King * If we had a previous bank, and there is a space 329a013053dSRussell King * between the current bank and the previous, free it. 330a013053dSRussell King */ 3311c2f87c2SLaura Abbott if (prev_end && prev_end < start) 3321c2f87c2SLaura Abbott free_memmap(prev_end, start); 333a013053dSRussell King 3343260e529SMichael Bohan /* 3353260e529SMichael Bohan * Align up here since the VM subsystem insists that the 3363260e529SMichael Bohan * memmap entries are valid from the bank end aligned to 3373260e529SMichael Bohan * MAX_ORDER_NR_PAGES. 3383260e529SMichael Bohan */ 3391c2f87c2SLaura Abbott prev_end = ALIGN(memblock_region_memory_end_pfn(reg), 3401c2f87c2SLaura Abbott MAX_ORDER_NR_PAGES); 341a013053dSRussell King } 3429af386c8SWill Deacon 3439af386c8SWill Deacon #ifdef CONFIG_SPARSEMEM 3441c2f87c2SLaura Abbott if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION)) 3451c2f87c2SLaura Abbott free_memmap(prev_end, 3461c2f87c2SLaura Abbott ALIGN(prev_end, PAGES_PER_SECTION)); 3479af386c8SWill Deacon #endif 348a013053dSRussell King } 349a013053dSRussell King 350d0e775afSRussell King static void __init free_highpages(void) 351d0e775afSRussell King { 352d0e775afSRussell King #ifdef CONFIG_HIGHMEM 35326ba47b1SSantosh Shilimkar unsigned long max_low = max_low_pfn; 354*cddb5ddfSMike Rapoport phys_addr_t range_start, range_end; 355*cddb5ddfSMike Rapoport u64 i; 356d0e775afSRussell King 357d0e775afSRussell King /* set highmem page free */ 358*cddb5ddfSMike Rapoport for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, 359*cddb5ddfSMike Rapoport &range_start, &range_end, NULL) { 360*cddb5ddfSMike Rapoport unsigned long start = PHYS_PFN(range_start); 361*cddb5ddfSMike Rapoport unsigned long end = PHYS_PFN(range_end); 362df4f14c7SRussell King 363df4f14c7SRussell King /* Ignore complete lowmem entries */ 364df4f14c7SRussell King if (end <= max_low) 365df4f14c7SRussell King continue; 366df4f14c7SRussell King 367df4f14c7SRussell King /* Truncate partial highmem entries */ 368df4f14c7SRussell King if (start < max_low) 369df4f14c7SRussell King start = max_low; 370df4f14c7SRussell King 371*cddb5ddfSMike Rapoport for (; start < end; start++) 372*cddb5ddfSMike Rapoport free_highmem_page(pfn_to_page(start)); 373d0e775afSRussell King } 374d0e775afSRussell King #endif 375d0e775afSRussell King } 376d0e775afSRussell King 3771da177e4SLinus Torvalds /* 3781da177e4SLinus Torvalds * mem_init() marks the free areas in the mem_map and tells us how much 3791da177e4SLinus Torvalds * memory is free. This is done after various parts of the system have 3801da177e4SLinus Torvalds * claimed their memory after the kernel image. 3811da177e4SLinus Torvalds */ 3821da177e4SLinus Torvalds void __init mem_init(void) 3831da177e4SLinus Torvalds { 384ad3c7b18SChristoph Hellwig #ifdef CONFIG_ARM_LPAE 385ad3c7b18SChristoph Hellwig swiotlb_init(1); 386ad3c7b18SChristoph Hellwig #endif 387ad3c7b18SChristoph Hellwig 388b3ba41f2SSantosh Shilimkar set_max_mapnr(pfn_to_page(max_pfn) - mem_map); 3891da177e4SLinus Torvalds 3901da177e4SLinus Torvalds /* this will put all unused low memory onto the freelists */ 3911c2f87c2SLaura Abbott free_unused_memmap(); 392c6ffc5caSMike Rapoport memblock_free_all(); 3931da177e4SLinus Torvalds 3941da177e4SLinus Torvalds #ifdef CONFIG_SA1111 3951da177e4SLinus Torvalds /* now that our DMA memory is actually so designated, we can free it */ 396bfd65dd9SLinus Torvalds free_reserved_area(__va(PHYS_OFFSET), swapper_pg_dir, -1, NULL); 3971da177e4SLinus Torvalds #endif 3981da177e4SLinus Torvalds 399d0e775afSRussell King free_highpages(); 4003835f6cbSNicolas Pitre 4012450c973SJiang Liu mem_init_print_info(NULL); 4021da177e4SLinus Torvalds 403a1839272SFenkart/Bostandzhyan /* 404a1839272SFenkart/Bostandzhyan * Check boundaries twice: Some fundamental inconsistencies can 405a1839272SFenkart/Bostandzhyan * be detected at build time already. 406a1839272SFenkart/Bostandzhyan */ 407a1839272SFenkart/Bostandzhyan #ifdef CONFIG_MMU 408a1839272SFenkart/Bostandzhyan BUILD_BUG_ON(TASK_SIZE > MODULES_VADDR); 409a1839272SFenkart/Bostandzhyan BUG_ON(TASK_SIZE > MODULES_VADDR); 410a1839272SFenkart/Bostandzhyan #endif 411a1839272SFenkart/Bostandzhyan 412a1839272SFenkart/Bostandzhyan #ifdef CONFIG_HIGHMEM 413a1839272SFenkart/Bostandzhyan BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET); 414a1839272SFenkart/Bostandzhyan BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET); 415a1839272SFenkart/Bostandzhyan #endif 4161da177e4SLinus Torvalds } 4171da177e4SLinus Torvalds 4180f5bf6d0SLaura Abbott #ifdef CONFIG_STRICT_KERNEL_RWX 4191e6b4811SKees Cook struct section_perm { 42025362dc4SKees Cook const char *name; 4211e6b4811SKees Cook unsigned long start; 4221e6b4811SKees Cook unsigned long end; 4231e6b4811SKees Cook pmdval_t mask; 4241e6b4811SKees Cook pmdval_t prot; 42580d6b0c2SKees Cook pmdval_t clear; 4261e6b4811SKees Cook }; 4271e6b4811SKees Cook 42864ac2e74SKees Cook /* First section-aligned location at or after __start_rodata. */ 42964ac2e74SKees Cook extern char __start_rodata_section_aligned[]; 43064ac2e74SKees Cook 43180d6b0c2SKees Cook static struct section_perm nx_perms[] = { 4321e6b4811SKees Cook /* Make pages tables, etc before _stext RW (set NX). */ 4331e6b4811SKees Cook { 43425362dc4SKees Cook .name = "pre-text NX", 4351e6b4811SKees Cook .start = PAGE_OFFSET, 4361e6b4811SKees Cook .end = (unsigned long)_stext, 4371e6b4811SKees Cook .mask = ~PMD_SECT_XN, 4381e6b4811SKees Cook .prot = PMD_SECT_XN, 4391e6b4811SKees Cook }, 4401e6b4811SKees Cook /* Make init RW (set NX). */ 4411e6b4811SKees Cook { 44225362dc4SKees Cook .name = "init NX", 4431e6b4811SKees Cook .start = (unsigned long)__init_begin, 4441e6b4811SKees Cook .end = (unsigned long)_sdata, 4451e6b4811SKees Cook .mask = ~PMD_SECT_XN, 4461e6b4811SKees Cook .prot = PMD_SECT_XN, 4471e6b4811SKees Cook }, 44880d6b0c2SKees Cook /* Make rodata NX (set RO in ro_perms below). */ 44980d6b0c2SKees Cook { 45025362dc4SKees Cook .name = "rodata NX", 45164ac2e74SKees Cook .start = (unsigned long)__start_rodata_section_aligned, 45280d6b0c2SKees Cook .end = (unsigned long)__init_begin, 45380d6b0c2SKees Cook .mask = ~PMD_SECT_XN, 45480d6b0c2SKees Cook .prot = PMD_SECT_XN, 45580d6b0c2SKees Cook }, 4561e6b4811SKees Cook }; 4571e6b4811SKees Cook 45880d6b0c2SKees Cook static struct section_perm ro_perms[] = { 45980d6b0c2SKees Cook /* Make kernel code and rodata RX (set RO). */ 46080d6b0c2SKees Cook { 46125362dc4SKees Cook .name = "text/rodata RO", 46280d6b0c2SKees Cook .start = (unsigned long)_stext, 46380d6b0c2SKees Cook .end = (unsigned long)__init_begin, 46480d6b0c2SKees Cook #ifdef CONFIG_ARM_LPAE 465400eeffaSPhilip Derrin .mask = ~(L_PMD_SECT_RDONLY | PMD_SECT_AP2), 466400eeffaSPhilip Derrin .prot = L_PMD_SECT_RDONLY | PMD_SECT_AP2, 46780d6b0c2SKees Cook #else 46880d6b0c2SKees Cook .mask = ~(PMD_SECT_APX | PMD_SECT_AP_WRITE), 46980d6b0c2SKees Cook .prot = PMD_SECT_APX | PMD_SECT_AP_WRITE, 47080d6b0c2SKees Cook .clear = PMD_SECT_AP_WRITE, 47180d6b0c2SKees Cook #endif 47280d6b0c2SKees Cook }, 47380d6b0c2SKees Cook }; 47480d6b0c2SKees Cook 4751e6b4811SKees Cook /* 4761e6b4811SKees Cook * Updates section permissions only for the current mm (sections are 4771e6b4811SKees Cook * copied into each mm). During startup, this is the init_mm. Is only 4781e6b4811SKees Cook * safe to be called with preemption disabled, as under stop_machine(). 4791e6b4811SKees Cook */ 4801e6b4811SKees Cook static inline void section_update(unsigned long addr, pmdval_t mask, 48108925c2fSLaura Abbott pmdval_t prot, struct mm_struct *mm) 4821e6b4811SKees Cook { 4831e6b4811SKees Cook pmd_t *pmd; 4841e6b4811SKees Cook 48584e6ffb2SMike Rapoport pmd = pmd_offset(pud_offset(p4d_offset(pgd_offset(mm, addr), addr), addr), addr); 4861e6b4811SKees Cook 4871e6b4811SKees Cook #ifdef CONFIG_ARM_LPAE 4881e6b4811SKees Cook pmd[0] = __pmd((pmd_val(pmd[0]) & mask) | prot); 4891e6b4811SKees Cook #else 4901e6b4811SKees Cook if (addr & SECTION_SIZE) 4911e6b4811SKees Cook pmd[1] = __pmd((pmd_val(pmd[1]) & mask) | prot); 4921e6b4811SKees Cook else 4931e6b4811SKees Cook pmd[0] = __pmd((pmd_val(pmd[0]) & mask) | prot); 4941e6b4811SKees Cook #endif 4951e6b4811SKees Cook flush_pmd_entry(pmd); 4961e6b4811SKees Cook local_flush_tlb_kernel_range(addr, addr + SECTION_SIZE); 4971e6b4811SKees Cook } 4981e6b4811SKees Cook 4991e6b4811SKees Cook /* Make sure extended page tables are in use. */ 5001e6b4811SKees Cook static inline bool arch_has_strict_perms(void) 5011e6b4811SKees Cook { 5021e6b4811SKees Cook if (cpu_architecture() < CPU_ARCH_ARMv6) 5031e6b4811SKees Cook return false; 5041e6b4811SKees Cook 5051e6b4811SKees Cook return !!(get_cr() & CR_XP); 5061e6b4811SKees Cook } 5071e6b4811SKees Cook 508ea5379beSBen Dooks (Codethink) static void set_section_perms(struct section_perm *perms, int n, bool set, 50908925c2fSLaura Abbott struct mm_struct *mm) 51008925c2fSLaura Abbott { 51108925c2fSLaura Abbott size_t i; 51208925c2fSLaura Abbott unsigned long addr; 51308925c2fSLaura Abbott 51408925c2fSLaura Abbott if (!arch_has_strict_perms()) 51508925c2fSLaura Abbott return; 51608925c2fSLaura Abbott 51708925c2fSLaura Abbott for (i = 0; i < n; i++) { 51808925c2fSLaura Abbott if (!IS_ALIGNED(perms[i].start, SECTION_SIZE) || 51908925c2fSLaura Abbott !IS_ALIGNED(perms[i].end, SECTION_SIZE)) { 52025362dc4SKees Cook pr_err("BUG: %s section %lx-%lx not aligned to %lx\n", 52125362dc4SKees Cook perms[i].name, perms[i].start, perms[i].end, 52208925c2fSLaura Abbott SECTION_SIZE); 52308925c2fSLaura Abbott continue; 5241e6b4811SKees Cook } 5251e6b4811SKees Cook 52608925c2fSLaura Abbott for (addr = perms[i].start; 52708925c2fSLaura Abbott addr < perms[i].end; 52808925c2fSLaura Abbott addr += SECTION_SIZE) 52908925c2fSLaura Abbott section_update(addr, perms[i].mask, 53008925c2fSLaura Abbott set ? perms[i].prot : perms[i].clear, mm); 53108925c2fSLaura Abbott } 53208925c2fSLaura Abbott 53308925c2fSLaura Abbott } 53408925c2fSLaura Abbott 53511ce4b33SGrygorii Strashko /** 53611ce4b33SGrygorii Strashko * update_sections_early intended to be called only through stop_machine 53711ce4b33SGrygorii Strashko * framework and executed by only one CPU while all other CPUs will spin and 53811ce4b33SGrygorii Strashko * wait, so no locking is required in this function. 53911ce4b33SGrygorii Strashko */ 54008925c2fSLaura Abbott static void update_sections_early(struct section_perm perms[], int n) 5411e6b4811SKees Cook { 54208925c2fSLaura Abbott struct task_struct *t, *s; 54308925c2fSLaura Abbott 54408925c2fSLaura Abbott for_each_process(t) { 54508925c2fSLaura Abbott if (t->flags & PF_KTHREAD) 54608925c2fSLaura Abbott continue; 54708925c2fSLaura Abbott for_each_thread(t, s) 548c51bc12dSDoug Berger if (s->mm) 54908925c2fSLaura Abbott set_section_perms(perms, n, true, s->mm); 55008925c2fSLaura Abbott } 55108925c2fSLaura Abbott set_section_perms(perms, n, true, current->active_mm); 55208925c2fSLaura Abbott set_section_perms(perms, n, true, &init_mm); 55308925c2fSLaura Abbott } 55408925c2fSLaura Abbott 55511ce4b33SGrygorii Strashko static int __fix_kernmem_perms(void *unused) 55608925c2fSLaura Abbott { 55708925c2fSLaura Abbott update_sections_early(nx_perms, ARRAY_SIZE(nx_perms)); 55808925c2fSLaura Abbott return 0; 55908925c2fSLaura Abbott } 56008925c2fSLaura Abbott 56111ce4b33SGrygorii Strashko static void fix_kernmem_perms(void) 56208925c2fSLaura Abbott { 56308925c2fSLaura Abbott stop_machine(__fix_kernmem_perms, NULL, NULL); 5641e6b4811SKees Cook } 56580d6b0c2SKees Cook 56611ce4b33SGrygorii Strashko static int __mark_rodata_ro(void *unused) 56708925c2fSLaura Abbott { 56808925c2fSLaura Abbott update_sections_early(ro_perms, ARRAY_SIZE(ro_perms)); 56908925c2fSLaura Abbott return 0; 57008925c2fSLaura Abbott } 57108925c2fSLaura Abbott 572b4c7e2bdSSteven Rostedt (VMware) static int kernel_set_to_readonly __read_mostly; 573b4c7e2bdSSteven Rostedt (VMware) 57480d6b0c2SKees Cook void mark_rodata_ro(void) 57580d6b0c2SKees Cook { 576b4c7e2bdSSteven Rostedt (VMware) kernel_set_to_readonly = 1; 57708925c2fSLaura Abbott stop_machine(__mark_rodata_ro, NULL, NULL); 578a8e53c15SJinbum Park debug_checkwx(); 57980d6b0c2SKees Cook } 58080d6b0c2SKees Cook 58180d6b0c2SKees Cook void set_kernel_text_rw(void) 58280d6b0c2SKees Cook { 583b4c7e2bdSSteven Rostedt (VMware) if (!kernel_set_to_readonly) 584b4c7e2bdSSteven Rostedt (VMware) return; 585b4c7e2bdSSteven Rostedt (VMware) 58608925c2fSLaura Abbott set_section_perms(ro_perms, ARRAY_SIZE(ro_perms), false, 58708925c2fSLaura Abbott current->active_mm); 58880d6b0c2SKees Cook } 58980d6b0c2SKees Cook 59080d6b0c2SKees Cook void set_kernel_text_ro(void) 59180d6b0c2SKees Cook { 592b4c7e2bdSSteven Rostedt (VMware) if (!kernel_set_to_readonly) 593b4c7e2bdSSteven Rostedt (VMware) return; 594b4c7e2bdSSteven Rostedt (VMware) 59508925c2fSLaura Abbott set_section_perms(ro_perms, ARRAY_SIZE(ro_perms), true, 59608925c2fSLaura Abbott current->active_mm); 59780d6b0c2SKees Cook } 59880d6b0c2SKees Cook 5991e6b4811SKees Cook #else 6001e6b4811SKees Cook static inline void fix_kernmem_perms(void) { } 6010f5bf6d0SLaura Abbott #endif /* CONFIG_STRICT_KERNEL_RWX */ 6021e6b4811SKees Cook 6031e6b4811SKees Cook void free_initmem(void) 6041e6b4811SKees Cook { 6051e6b4811SKees Cook fix_kernmem_perms(); 606bc581770SLinus Walleij 60754d52573SStephen Boyd poison_init_mem(__init_begin, __init_end - __init_begin); 6086db015e4SNicolas Pitre if (!machine_is_integrator() && !machine_is_cintegrator()) 609dbe67df4SJiang Liu free_initmem_default(-1); 6101da177e4SLinus Torvalds } 6111da177e4SLinus Torvalds 6121da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_INITRD 6131da177e4SLinus Torvalds void free_initrd_mem(unsigned long start, unsigned long end) 6141da177e4SLinus Torvalds { 615421520baSYalin Wang if (start == initrd_start) 616421520baSYalin Wang start = round_down(start, PAGE_SIZE); 617421520baSYalin Wang if (end == initrd_end) 618421520baSYalin Wang end = round_up(end, PAGE_SIZE); 619421520baSYalin Wang 62054d52573SStephen Boyd poison_init_mem((void *)start, PAGE_ALIGN(end) - start); 621dbe67df4SJiang Liu free_reserved_area((void *)start, (void *)end, -1, "initrd"); 6221da177e4SLinus Torvalds } 6231da177e4SLinus Torvalds #endif 624