11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/arch/arm/mm/init.c 31da177e4SLinus Torvalds * 490072059SRussell King * Copyright (C) 1995-2005 Russell King 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 71da177e4SLinus Torvalds * it under the terms of the GNU General Public License version 2 as 81da177e4SLinus Torvalds * published by the Free Software Foundation. 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds #include <linux/kernel.h> 111da177e4SLinus Torvalds #include <linux/errno.h> 121da177e4SLinus Torvalds #include <linux/swap.h> 131da177e4SLinus Torvalds #include <linux/init.h> 141da177e4SLinus Torvalds #include <linux/mman.h> 153f07c014SIngo Molnar #include <linux/sched/signal.h> 1629930025SIngo Molnar #include <linux/sched/task.h> 17dc28094bSPaul Gortmaker #include <linux/export.h> 181da177e4SLinus Torvalds #include <linux/nodemask.h> 191da177e4SLinus Torvalds #include <linux/initrd.h> 209eb8f674SGrant Likely #include <linux/of_fdt.h> 213835f6cbSNicolas Pitre #include <linux/highmem.h> 225a0e3ad6STejun Heo #include <linux/gfp.h> 232778f620SRussell King #include <linux/memblock.h> 24c7909509SMarek Szyprowski #include <linux/dma-contiguous.h> 25158e8bfeSAlessandro Rubini #include <linux/sizes.h> 2608925c2fSLaura Abbott #include <linux/stop_machine.h> 271da177e4SLinus Torvalds 28b4b20ad8SRussell King #include <asm/cp15.h> 291da177e4SLinus Torvalds #include <asm/mach-types.h> 30716a3dc2SRussell King #include <asm/memblock.h> 31d2ca5f24SAfzal Mohammed #include <asm/memory.h> 3293c02ab4SGrant Likely #include <asm/prom.h> 3337efe642SRussell King #include <asm/sections.h> 341da177e4SLinus Torvalds #include <asm/setup.h> 351e6b4811SKees Cook #include <asm/system_info.h> 361da177e4SLinus Torvalds #include <asm/tlb.h> 37db9ef1afSFenkart/Bostandzhyan #include <asm/fixmap.h> 38a8e53c15SJinbum Park #include <asm/ptdump.h> 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds #include <asm/mach/arch.h> 411da177e4SLinus Torvalds #include <asm/mach/map.h> 421da177e4SLinus Torvalds 431b2e2b73SRussell King #include "mm.h" 441b2e2b73SRussell King 45b4b20ad8SRussell King #ifdef CONFIG_CPU_CP15_MMU 46b4b20ad8SRussell King unsigned long __init __clear_cr(unsigned long mask) 47b4b20ad8SRussell King { 48b4b20ad8SRussell King cr_alignment = cr_alignment & ~mask; 49b4b20ad8SRussell King return cr_alignment; 50b4b20ad8SRussell King } 51b4b20ad8SRussell King #endif 52b4b20ad8SRussell King 53b1ab95c6SFlorian Fainelli #ifdef CONFIG_BLK_DEV_INITRD 54012d1f4aSRussell King static int __init parse_tag_initrd(const struct tag *tag) 55012d1f4aSRussell King { 564ed89f22SRussell King pr_warn("ATAG_INITRD is deprecated; " 57012d1f4aSRussell King "please update your bootloader.\n"); 58012d1f4aSRussell King phys_initrd_start = __virt_to_phys(tag->u.initrd.start); 59012d1f4aSRussell King phys_initrd_size = tag->u.initrd.size; 60012d1f4aSRussell King return 0; 61012d1f4aSRussell King } 62012d1f4aSRussell King 63012d1f4aSRussell King __tagtable(ATAG_INITRD, parse_tag_initrd); 64012d1f4aSRussell King 65012d1f4aSRussell King static int __init parse_tag_initrd2(const struct tag *tag) 66012d1f4aSRussell King { 67012d1f4aSRussell King phys_initrd_start = tag->u.initrd.start; 68012d1f4aSRussell King phys_initrd_size = tag->u.initrd.size; 69012d1f4aSRussell King return 0; 70012d1f4aSRussell King } 71012d1f4aSRussell King 72012d1f4aSRussell King __tagtable(ATAG_INITRD2, parse_tag_initrd2); 73b1ab95c6SFlorian Fainelli #endif 741da177e4SLinus Torvalds 75f25b4b4cSRussell King static void __init find_limits(unsigned long *min, unsigned long *max_low, 76f25b4b4cSRussell King unsigned long *max_high) 77dde5828fSRussell King { 781c2f87c2SLaura Abbott *max_low = PFN_DOWN(memblock_get_current_limit()); 791c2f87c2SLaura Abbott *min = PFN_UP(memblock_start_of_DRAM()); 801c2f87c2SLaura Abbott *max_high = PFN_DOWN(memblock_end_of_DRAM()); 81dde5828fSRussell King } 82dde5828fSRussell King 83be20902bSRussell King #ifdef CONFIG_ZONE_DMA 8465032018SNicolas Pitre 85364230b9SRob Herring phys_addr_t arm_dma_zone_size __read_mostly; 8665032018SNicolas Pitre EXPORT_SYMBOL(arm_dma_zone_size); 8765032018SNicolas Pitre 88022ae537SRussell King /* 89022ae537SRussell King * The DMA mask corresponding to the maximum bus address allocatable 90022ae537SRussell King * using GFP_DMA. The default here places no restriction on DMA 91022ae537SRussell King * allocations. This must be the smallest DMA mask in the system, 92022ae537SRussell King * so a successful GFP_DMA allocation will always satisfy this. 93022ae537SRussell King */ 944986e5c7SMarek Szyprowski phys_addr_t arm_dma_limit; 954dcfa600SRussell King unsigned long arm_dma_pfn_limit; 96022ae537SRussell King 97be20902bSRussell King static void __init arm_adjust_dma_zone(unsigned long *size, unsigned long *hole, 98be20902bSRussell King unsigned long dma_size) 99be20902bSRussell King { 100be20902bSRussell King if (size[0] <= dma_size) 101be20902bSRussell King return; 102be20902bSRussell King 103be20902bSRussell King size[ZONE_NORMAL] = size[0] - dma_size; 104be20902bSRussell King size[ZONE_DMA] = dma_size; 105be20902bSRussell King hole[ZONE_NORMAL] = hole[0]; 106be20902bSRussell King hole[ZONE_DMA] = 0; 107be20902bSRussell King } 108be20902bSRussell King #endif 109be20902bSRussell King 110ff69a4c8SRussell King void __init setup_dma_zone(const struct machine_desc *mdesc) 111c7909509SMarek Szyprowski { 112c7909509SMarek Szyprowski #ifdef CONFIG_ZONE_DMA 113c7909509SMarek Szyprowski if (mdesc->dma_zone_size) { 114c7909509SMarek Szyprowski arm_dma_zone_size = mdesc->dma_zone_size; 1156bcac805SRussell King arm_dma_limit = PHYS_OFFSET + arm_dma_zone_size - 1; 116c7909509SMarek Szyprowski } else 117c7909509SMarek Szyprowski arm_dma_limit = 0xffffffff; 1184dcfa600SRussell King arm_dma_pfn_limit = arm_dma_limit >> PAGE_SHIFT; 119c7909509SMarek Szyprowski #endif 120c7909509SMarek Szyprowski } 121c7909509SMarek Szyprowski 12284f452b1SSantosh Shilimkar static void __init zone_sizes_init(unsigned long min, unsigned long max_low, 123a2c54d2aSRussell King unsigned long max_high) 124b7a69ac3SRussell King { 125b7a69ac3SRussell King unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES]; 126a2c54d2aSRussell King struct memblock_region *reg; 127b7a69ac3SRussell King 12890072059SRussell King /* 129be370302SRussell King * initialise the zones. 13090072059SRussell King */ 13190072059SRussell King memset(zone_size, 0, sizeof(zone_size)); 13290072059SRussell King 13390072059SRussell King /* 134be370302SRussell King * The memory size has already been determined. If we need 135be370302SRussell King * to do anything fancy with the allocation of this memory 136be370302SRussell King * to the zones, now is the time to do it. 13790072059SRussell King */ 138dde5828fSRussell King zone_size[0] = max_low - min; 139dde5828fSRussell King #ifdef CONFIG_HIGHMEM 140dde5828fSRussell King zone_size[ZONE_HIGHMEM] = max_high - max_low; 141dde5828fSRussell King #endif 14290072059SRussell King 14390072059SRussell King /* 144be370302SRussell King * Calculate the size of the holes. 145be370302SRussell King * holes = node_size - sum(bank_sizes) 14690072059SRussell King */ 147dde5828fSRussell King memcpy(zhole_size, zone_size, sizeof(zhole_size)); 148a2c54d2aSRussell King for_each_memblock(memory, reg) { 149a2c54d2aSRussell King unsigned long start = memblock_region_memory_base_pfn(reg); 150a2c54d2aSRussell King unsigned long end = memblock_region_memory_end_pfn(reg); 151a2c54d2aSRussell King 152a2c54d2aSRussell King if (start < max_low) { 153a2c54d2aSRussell King unsigned long low_end = min(end, max_low); 154a2c54d2aSRussell King zhole_size[0] -= low_end - start; 155a2c54d2aSRussell King } 156dde5828fSRussell King #ifdef CONFIG_HIGHMEM 157a2c54d2aSRussell King if (end > max_low) { 158a2c54d2aSRussell King unsigned long high_start = max(start, max_low); 159a2c54d2aSRussell King zhole_size[ZONE_HIGHMEM] -= end - high_start; 160a2c54d2aSRussell King } 161dde5828fSRussell King #endif 162dde5828fSRussell King } 16390072059SRussell King 16465032018SNicolas Pitre #ifdef CONFIG_ZONE_DMA 16590072059SRussell King /* 16690072059SRussell King * Adjust the sizes according to any special requirements for 16790072059SRussell King * this machine type. 16890072059SRussell King */ 169c7909509SMarek Szyprowski if (arm_dma_zone_size) 170be20902bSRussell King arm_adjust_dma_zone(zone_size, zhole_size, 17165032018SNicolas Pitre arm_dma_zone_size >> PAGE_SHIFT); 172be20902bSRussell King #endif 17390072059SRussell King 174be370302SRussell King free_area_init_node(0, zone_size, min, zhole_size); 17590072059SRussell King } 17690072059SRussell King 1777b7bf499SWill Deacon #ifdef CONFIG_HAVE_ARCH_PFN_VALID 178b7cfda9fSRussell King int pfn_valid(unsigned long pfn) 179b7cfda9fSRussell King { 18009414d00SArd Biesheuvel return memblock_is_map_memory(__pfn_to_phys(pfn)); 181b7cfda9fSRussell King } 182b7cfda9fSRussell King EXPORT_SYMBOL(pfn_valid); 1837b7bf499SWill Deacon #endif 184657e12fdSRussell King 1857b7bf499SWill Deacon #ifndef CONFIG_SPARSEMEM 18614904927SStephen Boyd static void __init arm_memory_present(void) 187657e12fdSRussell King { 188657e12fdSRussell King } 189657e12fdSRussell King #else 19014904927SStephen Boyd static void __init arm_memory_present(void) 191657e12fdSRussell King { 192719c1514SBenjamin Herrenschmidt struct memblock_region *reg; 193719c1514SBenjamin Herrenschmidt 1947c996361SYinghai Lu for_each_memblock(memory, reg) 195c7fc2de0SYinghai Lu memory_present(0, memblock_region_memory_base_pfn(reg), 196c7fc2de0SYinghai Lu memblock_region_memory_end_pfn(reg)); 197657e12fdSRussell King } 198b7cfda9fSRussell King #endif 199b7cfda9fSRussell King 200716a3dc2SRussell King static bool arm_memblock_steal_permitted = true; 201716a3dc2SRussell King 202bc2827d0SRussell King phys_addr_t __init arm_memblock_steal(phys_addr_t size, phys_addr_t align) 203716a3dc2SRussell King { 204716a3dc2SRussell King phys_addr_t phys; 205716a3dc2SRussell King 206716a3dc2SRussell King BUG_ON(!arm_memblock_steal_permitted); 207716a3dc2SRussell King 208f240ec09SMike Rapoport phys = memblock_phys_alloc(size, align); 209*ecc3e771SMike Rapoport if (!phys) 210*ecc3e771SMike Rapoport panic("Failed to steal %pa bytes at %pS\n", 211*ecc3e771SMike Rapoport &size, (void *)_RET_IP_); 212*ecc3e771SMike Rapoport 213716a3dc2SRussell King memblock_free(phys, size); 214716a3dc2SRussell King memblock_remove(phys, size); 215716a3dc2SRussell King 216716a3dc2SRussell King return phys; 217716a3dc2SRussell King } 218716a3dc2SRussell King 21939286248SRussell King static void __init arm_initrd_init(void) 2202778f620SRussell King { 2212778f620SRussell King #ifdef CONFIG_BLK_DEV_INITRD 222cdcc5fa0SRussell King phys_addr_t start; 223cdcc5fa0SRussell King unsigned long size; 224cdcc5fa0SRussell King 2254c235cb9SBen Peddell initrd_start = initrd_end = 0; 22668b32f36SRussell King 22768b32f36SRussell King if (!phys_initrd_size) 22868b32f36SRussell King return; 22968b32f36SRussell King 230cdcc5fa0SRussell King /* 231cdcc5fa0SRussell King * Round the memory region to page boundaries as per free_initrd_mem() 232cdcc5fa0SRussell King * This allows us to detect whether the pages overlapping the initrd 233cdcc5fa0SRussell King * are in use, but more importantly, reserves the entire set of pages 234cdcc5fa0SRussell King * as we don't want these pages allocated for other purposes. 235cdcc5fa0SRussell King */ 236cdcc5fa0SRussell King start = round_down(phys_initrd_start, PAGE_SIZE); 237cdcc5fa0SRussell King size = phys_initrd_size + (phys_initrd_start - start); 238cdcc5fa0SRussell King size = round_up(size, PAGE_SIZE); 239cdcc5fa0SRussell King 240cdcc5fa0SRussell King if (!memblock_is_region_memory(start, size)) { 241de22cc6eSVitaly Andrianov pr_err("INITRD: 0x%08llx+0x%08lx is not a memory region - disabling initrd\n", 242cdcc5fa0SRussell King (u64)start, size); 24368b32f36SRussell King return; 2448f4b8c76SRussell King } 24568b32f36SRussell King 246cdcc5fa0SRussell King if (memblock_is_region_reserved(start, size)) { 247de22cc6eSVitaly Andrianov pr_err("INITRD: 0x%08llx+0x%08lx overlaps in-use memory region - disabling initrd\n", 248cdcc5fa0SRussell King (u64)start, size); 24968b32f36SRussell King return; 250b0a2679dSRussell King } 25168b32f36SRussell King 252cdcc5fa0SRussell King memblock_reserve(start, size); 2532778f620SRussell King 2542778f620SRussell King /* Now convert initrd to virtual addresses */ 2552778f620SRussell King initrd_start = __phys_to_virt(phys_initrd_start); 2562778f620SRussell King initrd_end = initrd_start + phys_initrd_size; 2572778f620SRussell King #endif 25839286248SRussell King } 25939286248SRussell King 26039286248SRussell King void __init arm_memblock_init(const struct machine_desc *mdesc) 26139286248SRussell King { 26239286248SRussell King /* Register the kernel text, kernel data and initrd with memblock. */ 26339286248SRussell King memblock_reserve(__pa(KERNEL_START), KERNEL_END - KERNEL_START); 26439286248SRussell King 26539286248SRussell King arm_initrd_init(); 2662778f620SRussell King 2672778f620SRussell King arm_mm_memblock_reserve(); 2682778f620SRussell King 2698d717a52SRussell King /* reserve any platform specific memblock areas */ 2708d717a52SRussell King if (mdesc->reserve) 2718d717a52SRussell King mdesc->reserve(); 2728d717a52SRussell King 27324bbd929SArd Biesheuvel early_init_fdt_reserve_self(); 274bcedb5f9SMarek Szyprowski early_init_fdt_scan_reserved_mem(); 275bcedb5f9SMarek Szyprowski 27699a468d7SGeorge G. Davis /* reserve memory for DMA contiguous allocations */ 27795b0e655SMarek Szyprowski dma_contiguous_reserve(arm_dma_limit); 278c7909509SMarek Szyprowski 279716a3dc2SRussell King arm_memblock_steal_permitted = false; 2802778f620SRussell King memblock_dump_all(); 2812778f620SRussell King } 2822778f620SRussell King 2838d717a52SRussell King void __init bootmem_init(void) 28490072059SRussell King { 285dde5828fSRussell King unsigned long min, max_low, max_high; 28690072059SRussell King 2878e58caefSGrygorii Strashko memblock_allow_resize(); 288dde5828fSRussell King max_low = max_high = 0; 289dde5828fSRussell King 290f25b4b4cSRussell King find_limits(&min, &max_low, &max_high); 291dde5828fSRussell King 292d30eae47SVladimir Murzin early_memtest((phys_addr_t)min << PAGE_SHIFT, 293d30eae47SVladimir Murzin (phys_addr_t)max_low << PAGE_SHIFT); 294d30eae47SVladimir Murzin 295b7a69ac3SRussell King /* 296657e12fdSRussell King * Sparsemem tries to allocate bootmem in memory_present(), 297657e12fdSRussell King * so must be done after the fixed reservations 298657e12fdSRussell King */ 299eda2e5dcSRussell King arm_memory_present(); 30090072059SRussell King 301b7a69ac3SRussell King /* 302b7a69ac3SRussell King * sparse_init() needs the bootmem allocator up and running. 303b7a69ac3SRussell King */ 304b7a69ac3SRussell King sparse_init(); 305b7a69ac3SRussell King 306b7a69ac3SRussell King /* 307be370302SRussell King * Now free the memory - free_area_init_node needs 308b7a69ac3SRussell King * the sparse mem_map arrays initialized by sparse_init() 309b7a69ac3SRussell King * for memmap_init_zone(), otherwise all PFNs are invalid. 310b7a69ac3SRussell King */ 31184f452b1SSantosh Shilimkar zone_sizes_init(min, max_low, max_high); 312b7a69ac3SRussell King 31390072059SRussell King /* 31490072059SRussell King * This doesn't seem to be used by the Linux memory manager any 31590072059SRussell King * more, but is used by ll_rw_block. If we can get rid of it, we 31690072059SRussell King * also get rid of some of the stuff above as well. 31790072059SRussell King */ 31826ba47b1SSantosh Shilimkar min_low_pfn = min; 31926ba47b1SSantosh Shilimkar max_low_pfn = max_low; 32026ba47b1SSantosh Shilimkar max_pfn = max_high; 32190072059SRussell King } 32290072059SRussell King 32354d52573SStephen Boyd /* 32454d52573SStephen Boyd * Poison init memory with an undefined instruction (ARM) or a branch to an 32554d52573SStephen Boyd * undefined instruction (Thumb). 32654d52573SStephen Boyd */ 32754d52573SStephen Boyd static inline void poison_init_mem(void *s, size_t count) 32854d52573SStephen Boyd { 32954d52573SStephen Boyd u32 *p = (u32 *)s; 330bf912d99SJamie Iles for (; count != 0; count -= 4) 33154d52573SStephen Boyd *p++ = 0xe7fddef0; 33254d52573SStephen Boyd } 33354d52573SStephen Boyd 334a013053dSRussell King static inline void 335be370302SRussell King free_memmap(unsigned long start_pfn, unsigned long end_pfn) 336a013053dSRussell King { 337a013053dSRussell King struct page *start_pg, *end_pg; 33856bc6286SVitaly Andrianov phys_addr_t pg, pgend; 339a013053dSRussell King 340a013053dSRussell King /* 341a013053dSRussell King * Convert start_pfn/end_pfn to a struct page pointer. 342a013053dSRussell King */ 3433257f43dSCatalin Marinas start_pg = pfn_to_page(start_pfn - 1) + 1; 3449af386c8SWill Deacon end_pg = pfn_to_page(end_pfn - 1) + 1; 345a013053dSRussell King 346a013053dSRussell King /* 347a013053dSRussell King * Convert to physical addresses, and 348a013053dSRussell King * round start upwards and end downwards. 349a013053dSRussell King */ 35056bc6286SVitaly Andrianov pg = PAGE_ALIGN(__pa(start_pg)); 35156bc6286SVitaly Andrianov pgend = __pa(end_pg) & PAGE_MASK; 352a013053dSRussell King 353a013053dSRussell King /* 354a013053dSRussell King * If there are free pages between these, 355a013053dSRussell King * free the section of the memmap array. 356a013053dSRussell King */ 357a013053dSRussell King if (pg < pgend) 358cfb66586SSantosh Shilimkar memblock_free_early(pg, pgend - pg); 359a013053dSRussell King } 360a013053dSRussell King 361a013053dSRussell King /* 362a013053dSRussell King * The mem_map array can get very big. Free the unused area of the memory map. 363a013053dSRussell King */ 3641c2f87c2SLaura Abbott static void __init free_unused_memmap(void) 365a013053dSRussell King { 3661c2f87c2SLaura Abbott unsigned long start, prev_end = 0; 3671c2f87c2SLaura Abbott struct memblock_region *reg; 368a013053dSRussell King 369a013053dSRussell King /* 3703260e529SMichael Bohan * This relies on each bank being in address order. 3713260e529SMichael Bohan * The banks are sorted previously in bootmem_init(). 372a013053dSRussell King */ 3731c2f87c2SLaura Abbott for_each_memblock(memory, reg) { 3741c2f87c2SLaura Abbott start = memblock_region_memory_base_pfn(reg); 375a013053dSRussell King 3769af386c8SWill Deacon #ifdef CONFIG_SPARSEMEM 3779af386c8SWill Deacon /* 3789af386c8SWill Deacon * Take care not to free memmap entries that don't exist 3799af386c8SWill Deacon * due to SPARSEMEM sections which aren't present. 3809af386c8SWill Deacon */ 3811c2f87c2SLaura Abbott start = min(start, 3821c2f87c2SLaura Abbott ALIGN(prev_end, PAGES_PER_SECTION)); 383002ea9eeSLinus Walleij #else 384002ea9eeSLinus Walleij /* 385002ea9eeSLinus Walleij * Align down here since the VM subsystem insists that the 386002ea9eeSLinus Walleij * memmap entries are valid from the bank start aligned to 387002ea9eeSLinus Walleij * MAX_ORDER_NR_PAGES. 388002ea9eeSLinus Walleij */ 3891c2f87c2SLaura Abbott start = round_down(start, MAX_ORDER_NR_PAGES); 3909af386c8SWill Deacon #endif 391a013053dSRussell King /* 392a013053dSRussell King * If we had a previous bank, and there is a space 393a013053dSRussell King * between the current bank and the previous, free it. 394a013053dSRussell King */ 3951c2f87c2SLaura Abbott if (prev_end && prev_end < start) 3961c2f87c2SLaura Abbott free_memmap(prev_end, start); 397a013053dSRussell King 3983260e529SMichael Bohan /* 3993260e529SMichael Bohan * Align up here since the VM subsystem insists that the 4003260e529SMichael Bohan * memmap entries are valid from the bank end aligned to 4013260e529SMichael Bohan * MAX_ORDER_NR_PAGES. 4023260e529SMichael Bohan */ 4031c2f87c2SLaura Abbott prev_end = ALIGN(memblock_region_memory_end_pfn(reg), 4041c2f87c2SLaura Abbott MAX_ORDER_NR_PAGES); 405a013053dSRussell King } 4069af386c8SWill Deacon 4079af386c8SWill Deacon #ifdef CONFIG_SPARSEMEM 4081c2f87c2SLaura Abbott if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION)) 4091c2f87c2SLaura Abbott free_memmap(prev_end, 4101c2f87c2SLaura Abbott ALIGN(prev_end, PAGES_PER_SECTION)); 4119af386c8SWill Deacon #endif 412a013053dSRussell King } 413a013053dSRussell King 41483db0384SJiang Liu #ifdef CONFIG_HIGHMEM 41583db0384SJiang Liu static inline void free_area_high(unsigned long pfn, unsigned long end) 41683db0384SJiang Liu { 417dd6911efSJiang Liu for (; pfn < end; pfn++) 418dd6911efSJiang Liu free_highmem_page(pfn_to_page(pfn)); 41983db0384SJiang Liu } 42083db0384SJiang Liu #endif 42183db0384SJiang Liu 422d0e775afSRussell King static void __init free_highpages(void) 423d0e775afSRussell King { 424d0e775afSRussell King #ifdef CONFIG_HIGHMEM 42526ba47b1SSantosh Shilimkar unsigned long max_low = max_low_pfn; 426df4f14c7SRussell King struct memblock_region *mem, *res; 427d0e775afSRussell King 428d0e775afSRussell King /* set highmem page free */ 429df4f14c7SRussell King for_each_memblock(memory, mem) { 430df4f14c7SRussell King unsigned long start = memblock_region_memory_base_pfn(mem); 431df4f14c7SRussell King unsigned long end = memblock_region_memory_end_pfn(mem); 432df4f14c7SRussell King 433df4f14c7SRussell King /* Ignore complete lowmem entries */ 434df4f14c7SRussell King if (end <= max_low) 435df4f14c7SRussell King continue; 436df4f14c7SRussell King 43709414d00SArd Biesheuvel if (memblock_is_nomap(mem)) 43809414d00SArd Biesheuvel continue; 43909414d00SArd Biesheuvel 440df4f14c7SRussell King /* Truncate partial highmem entries */ 441df4f14c7SRussell King if (start < max_low) 442df4f14c7SRussell King start = max_low; 443df4f14c7SRussell King 444df4f14c7SRussell King /* Find and exclude any reserved regions */ 445df4f14c7SRussell King for_each_memblock(reserved, res) { 446df4f14c7SRussell King unsigned long res_start, res_end; 447df4f14c7SRussell King 448df4f14c7SRussell King res_start = memblock_region_reserved_base_pfn(res); 449df4f14c7SRussell King res_end = memblock_region_reserved_end_pfn(res); 450df4f14c7SRussell King 451df4f14c7SRussell King if (res_end < start) 452df4f14c7SRussell King continue; 453df4f14c7SRussell King if (res_start < start) 454df4f14c7SRussell King res_start = start; 455df4f14c7SRussell King if (res_start > end) 456df4f14c7SRussell King res_start = end; 457df4f14c7SRussell King if (res_end > end) 458df4f14c7SRussell King res_end = end; 459df4f14c7SRussell King if (res_start != start) 46083db0384SJiang Liu free_area_high(start, res_start); 461df4f14c7SRussell King start = res_end; 462df4f14c7SRussell King if (start == end) 463df4f14c7SRussell King break; 464df4f14c7SRussell King } 465df4f14c7SRussell King 466df4f14c7SRussell King /* And now free anything which remains */ 467df4f14c7SRussell King if (start < end) 46883db0384SJiang Liu free_area_high(start, end); 469d0e775afSRussell King } 470d0e775afSRussell King #endif 471d0e775afSRussell King } 472d0e775afSRussell King 4731da177e4SLinus Torvalds /* 4741da177e4SLinus Torvalds * mem_init() marks the free areas in the mem_map and tells us how much 4751da177e4SLinus Torvalds * memory is free. This is done after various parts of the system have 4761da177e4SLinus Torvalds * claimed their memory after the kernel image. 4771da177e4SLinus Torvalds */ 4781da177e4SLinus Torvalds void __init mem_init(void) 4791da177e4SLinus Torvalds { 4801dbd30e9SLinus Walleij #ifdef CONFIG_HAVE_TCM 4811dbd30e9SLinus Walleij /* These pointers are filled in on TCM detection */ 4821dbd30e9SLinus Walleij extern u32 dtcm_end; 4831dbd30e9SLinus Walleij extern u32 itcm_end; 4841dbd30e9SLinus Walleij #endif 4851da177e4SLinus Torvalds 486b3ba41f2SSantosh Shilimkar set_max_mapnr(pfn_to_page(max_pfn) - mem_map); 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds /* this will put all unused low memory onto the freelists */ 4891c2f87c2SLaura Abbott free_unused_memmap(); 490c6ffc5caSMike Rapoport memblock_free_all(); 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds #ifdef CONFIG_SA1111 4931da177e4SLinus Torvalds /* now that our DMA memory is actually so designated, we can free it */ 494bfd65dd9SLinus Torvalds free_reserved_area(__va(PHYS_OFFSET), swapper_pg_dir, -1, NULL); 4951da177e4SLinus Torvalds #endif 4961da177e4SLinus Torvalds 497d0e775afSRussell King free_highpages(); 4983835f6cbSNicolas Pitre 4992450c973SJiang Liu mem_init_print_info(NULL); 5001da177e4SLinus Torvalds 501db9ef1afSFenkart/Bostandzhyan #define MLK(b, t) b, t, ((t) - (b)) >> 10 502db9ef1afSFenkart/Bostandzhyan #define MLM(b, t) b, t, ((t) - (b)) >> 20 503db9ef1afSFenkart/Bostandzhyan #define MLK_ROUNDUP(b, t) b, t, DIV_ROUND_UP(((t) - (b)), SZ_1K) 504db9ef1afSFenkart/Bostandzhyan 5054ed89f22SRussell King pr_notice("Virtual kernel memory layout:\n" 506db9ef1afSFenkart/Bostandzhyan " vector : 0x%08lx - 0x%08lx (%4ld kB)\n" 50707d2a5c7SLinus Walleij #ifdef CONFIG_HAVE_TCM 50807d2a5c7SLinus Walleij " DTCM : 0x%08lx - 0x%08lx (%4ld kB)\n" 50907d2a5c7SLinus Walleij " ITCM : 0x%08lx - 0x%08lx (%4ld kB)\n" 51007d2a5c7SLinus Walleij #endif 511db9ef1afSFenkart/Bostandzhyan " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n" 512db9ef1afSFenkart/Bostandzhyan " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n" 513db9ef1afSFenkart/Bostandzhyan " lowmem : 0x%08lx - 0x%08lx (%4ld MB)\n" 514db9ef1afSFenkart/Bostandzhyan #ifdef CONFIG_HIGHMEM 515db9ef1afSFenkart/Bostandzhyan " pkmap : 0x%08lx - 0x%08lx (%4ld MB)\n" 516db9ef1afSFenkart/Bostandzhyan #endif 517d9277d51SUwe Kleine-König #ifdef CONFIG_MODULES 518db9ef1afSFenkart/Bostandzhyan " modules : 0x%08lx - 0x%08lx (%4ld MB)\n" 519d9277d51SUwe Kleine-König #endif 520178c3dfeSRussell King " .text : 0x%p" " - 0x%p" " (%4td kB)\n" 521178c3dfeSRussell King " .init : 0x%p" " - 0x%p" " (%4td kB)\n" 522178c3dfeSRussell King " .data : 0x%p" " - 0x%p" " (%4td kB)\n" 523178c3dfeSRussell King " .bss : 0x%p" " - 0x%p" " (%4td kB)\n", 524db9ef1afSFenkart/Bostandzhyan 525d2ca5f24SAfzal Mohammed MLK(VECTORS_BASE, VECTORS_BASE + PAGE_SIZE), 52607d2a5c7SLinus Walleij #ifdef CONFIG_HAVE_TCM 5271dbd30e9SLinus Walleij MLK(DTCM_OFFSET, (unsigned long) dtcm_end), 5281dbd30e9SLinus Walleij MLK(ITCM_OFFSET, (unsigned long) itcm_end), 52907d2a5c7SLinus Walleij #endif 530b615bbbfSMark Salter MLK(FIXADDR_START, FIXADDR_END), 531c931b4f6SFenkart/Bostandzhyan MLM(VMALLOC_START, VMALLOC_END), 532db9ef1afSFenkart/Bostandzhyan MLM(PAGE_OFFSET, (unsigned long)high_memory), 533db9ef1afSFenkart/Bostandzhyan #ifdef CONFIG_HIGHMEM 534db9ef1afSFenkart/Bostandzhyan MLM(PKMAP_BASE, (PKMAP_BASE) + (LAST_PKMAP) * 535db9ef1afSFenkart/Bostandzhyan (PAGE_SIZE)), 536db9ef1afSFenkart/Bostandzhyan #endif 537d9277d51SUwe Kleine-König #ifdef CONFIG_MODULES 538db9ef1afSFenkart/Bostandzhyan MLM(MODULES_VADDR, MODULES_END), 539d9277d51SUwe Kleine-König #endif 540db9ef1afSFenkart/Bostandzhyan 541db9ef1afSFenkart/Bostandzhyan MLK_ROUNDUP(_text, _etext), 5423835d69aSRussell King MLK_ROUNDUP(__init_begin, __init_end), 54345f6d7e0SRabin Vincent MLK_ROUNDUP(_sdata, _edata), 54445f6d7e0SRabin Vincent MLK_ROUNDUP(__bss_start, __bss_stop)); 545db9ef1afSFenkart/Bostandzhyan 546db9ef1afSFenkart/Bostandzhyan #undef MLK 547db9ef1afSFenkart/Bostandzhyan #undef MLM 548db9ef1afSFenkart/Bostandzhyan #undef MLK_ROUNDUP 549db9ef1afSFenkart/Bostandzhyan 550a1839272SFenkart/Bostandzhyan /* 551a1839272SFenkart/Bostandzhyan * Check boundaries twice: Some fundamental inconsistencies can 552a1839272SFenkart/Bostandzhyan * be detected at build time already. 553a1839272SFenkart/Bostandzhyan */ 554a1839272SFenkart/Bostandzhyan #ifdef CONFIG_MMU 555a1839272SFenkart/Bostandzhyan BUILD_BUG_ON(TASK_SIZE > MODULES_VADDR); 556a1839272SFenkart/Bostandzhyan BUG_ON(TASK_SIZE > MODULES_VADDR); 557a1839272SFenkart/Bostandzhyan #endif 558a1839272SFenkart/Bostandzhyan 559a1839272SFenkart/Bostandzhyan #ifdef CONFIG_HIGHMEM 560a1839272SFenkart/Bostandzhyan BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET); 561a1839272SFenkart/Bostandzhyan BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET); 562a1839272SFenkart/Bostandzhyan #endif 5631da177e4SLinus Torvalds } 5641da177e4SLinus Torvalds 5650f5bf6d0SLaura Abbott #ifdef CONFIG_STRICT_KERNEL_RWX 5661e6b4811SKees Cook struct section_perm { 56725362dc4SKees Cook const char *name; 5681e6b4811SKees Cook unsigned long start; 5691e6b4811SKees Cook unsigned long end; 5701e6b4811SKees Cook pmdval_t mask; 5711e6b4811SKees Cook pmdval_t prot; 57280d6b0c2SKees Cook pmdval_t clear; 5731e6b4811SKees Cook }; 5741e6b4811SKees Cook 57564ac2e74SKees Cook /* First section-aligned location at or after __start_rodata. */ 57664ac2e74SKees Cook extern char __start_rodata_section_aligned[]; 57764ac2e74SKees Cook 57880d6b0c2SKees Cook static struct section_perm nx_perms[] = { 5791e6b4811SKees Cook /* Make pages tables, etc before _stext RW (set NX). */ 5801e6b4811SKees Cook { 58125362dc4SKees Cook .name = "pre-text NX", 5821e6b4811SKees Cook .start = PAGE_OFFSET, 5831e6b4811SKees Cook .end = (unsigned long)_stext, 5841e6b4811SKees Cook .mask = ~PMD_SECT_XN, 5851e6b4811SKees Cook .prot = PMD_SECT_XN, 5861e6b4811SKees Cook }, 5871e6b4811SKees Cook /* Make init RW (set NX). */ 5881e6b4811SKees Cook { 58925362dc4SKees Cook .name = "init NX", 5901e6b4811SKees Cook .start = (unsigned long)__init_begin, 5911e6b4811SKees Cook .end = (unsigned long)_sdata, 5921e6b4811SKees Cook .mask = ~PMD_SECT_XN, 5931e6b4811SKees Cook .prot = PMD_SECT_XN, 5941e6b4811SKees Cook }, 59580d6b0c2SKees Cook /* Make rodata NX (set RO in ro_perms below). */ 59680d6b0c2SKees Cook { 59725362dc4SKees Cook .name = "rodata NX", 59864ac2e74SKees Cook .start = (unsigned long)__start_rodata_section_aligned, 59980d6b0c2SKees Cook .end = (unsigned long)__init_begin, 60080d6b0c2SKees Cook .mask = ~PMD_SECT_XN, 60180d6b0c2SKees Cook .prot = PMD_SECT_XN, 60280d6b0c2SKees Cook }, 6031e6b4811SKees Cook }; 6041e6b4811SKees Cook 60580d6b0c2SKees Cook static struct section_perm ro_perms[] = { 60680d6b0c2SKees Cook /* Make kernel code and rodata RX (set RO). */ 60780d6b0c2SKees Cook { 60825362dc4SKees Cook .name = "text/rodata RO", 60980d6b0c2SKees Cook .start = (unsigned long)_stext, 61080d6b0c2SKees Cook .end = (unsigned long)__init_begin, 61180d6b0c2SKees Cook #ifdef CONFIG_ARM_LPAE 612400eeffaSPhilip Derrin .mask = ~(L_PMD_SECT_RDONLY | PMD_SECT_AP2), 613400eeffaSPhilip Derrin .prot = L_PMD_SECT_RDONLY | PMD_SECT_AP2, 61480d6b0c2SKees Cook #else 61580d6b0c2SKees Cook .mask = ~(PMD_SECT_APX | PMD_SECT_AP_WRITE), 61680d6b0c2SKees Cook .prot = PMD_SECT_APX | PMD_SECT_AP_WRITE, 61780d6b0c2SKees Cook .clear = PMD_SECT_AP_WRITE, 61880d6b0c2SKees Cook #endif 61980d6b0c2SKees Cook }, 62080d6b0c2SKees Cook }; 62180d6b0c2SKees Cook 6221e6b4811SKees Cook /* 6231e6b4811SKees Cook * Updates section permissions only for the current mm (sections are 6241e6b4811SKees Cook * copied into each mm). During startup, this is the init_mm. Is only 6251e6b4811SKees Cook * safe to be called with preemption disabled, as under stop_machine(). 6261e6b4811SKees Cook */ 6271e6b4811SKees Cook static inline void section_update(unsigned long addr, pmdval_t mask, 62808925c2fSLaura Abbott pmdval_t prot, struct mm_struct *mm) 6291e6b4811SKees Cook { 6301e6b4811SKees Cook pmd_t *pmd; 6311e6b4811SKees Cook 6321e6b4811SKees Cook pmd = pmd_offset(pud_offset(pgd_offset(mm, addr), addr), addr); 6331e6b4811SKees Cook 6341e6b4811SKees Cook #ifdef CONFIG_ARM_LPAE 6351e6b4811SKees Cook pmd[0] = __pmd((pmd_val(pmd[0]) & mask) | prot); 6361e6b4811SKees Cook #else 6371e6b4811SKees Cook if (addr & SECTION_SIZE) 6381e6b4811SKees Cook pmd[1] = __pmd((pmd_val(pmd[1]) & mask) | prot); 6391e6b4811SKees Cook else 6401e6b4811SKees Cook pmd[0] = __pmd((pmd_val(pmd[0]) & mask) | prot); 6411e6b4811SKees Cook #endif 6421e6b4811SKees Cook flush_pmd_entry(pmd); 6431e6b4811SKees Cook local_flush_tlb_kernel_range(addr, addr + SECTION_SIZE); 6441e6b4811SKees Cook } 6451e6b4811SKees Cook 6461e6b4811SKees Cook /* Make sure extended page tables are in use. */ 6471e6b4811SKees Cook static inline bool arch_has_strict_perms(void) 6481e6b4811SKees Cook { 6491e6b4811SKees Cook if (cpu_architecture() < CPU_ARCH_ARMv6) 6501e6b4811SKees Cook return false; 6511e6b4811SKees Cook 6521e6b4811SKees Cook return !!(get_cr() & CR_XP); 6531e6b4811SKees Cook } 6541e6b4811SKees Cook 65508925c2fSLaura Abbott void set_section_perms(struct section_perm *perms, int n, bool set, 65608925c2fSLaura Abbott struct mm_struct *mm) 65708925c2fSLaura Abbott { 65808925c2fSLaura Abbott size_t i; 65908925c2fSLaura Abbott unsigned long addr; 66008925c2fSLaura Abbott 66108925c2fSLaura Abbott if (!arch_has_strict_perms()) 66208925c2fSLaura Abbott return; 66308925c2fSLaura Abbott 66408925c2fSLaura Abbott for (i = 0; i < n; i++) { 66508925c2fSLaura Abbott if (!IS_ALIGNED(perms[i].start, SECTION_SIZE) || 66608925c2fSLaura Abbott !IS_ALIGNED(perms[i].end, SECTION_SIZE)) { 66725362dc4SKees Cook pr_err("BUG: %s section %lx-%lx not aligned to %lx\n", 66825362dc4SKees Cook perms[i].name, perms[i].start, perms[i].end, 66908925c2fSLaura Abbott SECTION_SIZE); 67008925c2fSLaura Abbott continue; 6711e6b4811SKees Cook } 6721e6b4811SKees Cook 67308925c2fSLaura Abbott for (addr = perms[i].start; 67408925c2fSLaura Abbott addr < perms[i].end; 67508925c2fSLaura Abbott addr += SECTION_SIZE) 67608925c2fSLaura Abbott section_update(addr, perms[i].mask, 67708925c2fSLaura Abbott set ? perms[i].prot : perms[i].clear, mm); 67808925c2fSLaura Abbott } 67908925c2fSLaura Abbott 68008925c2fSLaura Abbott } 68108925c2fSLaura Abbott 68211ce4b33SGrygorii Strashko /** 68311ce4b33SGrygorii Strashko * update_sections_early intended to be called only through stop_machine 68411ce4b33SGrygorii Strashko * framework and executed by only one CPU while all other CPUs will spin and 68511ce4b33SGrygorii Strashko * wait, so no locking is required in this function. 68611ce4b33SGrygorii Strashko */ 68708925c2fSLaura Abbott static void update_sections_early(struct section_perm perms[], int n) 6881e6b4811SKees Cook { 68908925c2fSLaura Abbott struct task_struct *t, *s; 69008925c2fSLaura Abbott 69108925c2fSLaura Abbott for_each_process(t) { 69208925c2fSLaura Abbott if (t->flags & PF_KTHREAD) 69308925c2fSLaura Abbott continue; 69408925c2fSLaura Abbott for_each_thread(t, s) 69508925c2fSLaura Abbott set_section_perms(perms, n, true, s->mm); 69608925c2fSLaura Abbott } 69708925c2fSLaura Abbott set_section_perms(perms, n, true, current->active_mm); 69808925c2fSLaura Abbott set_section_perms(perms, n, true, &init_mm); 69908925c2fSLaura Abbott } 70008925c2fSLaura Abbott 70111ce4b33SGrygorii Strashko static int __fix_kernmem_perms(void *unused) 70208925c2fSLaura Abbott { 70308925c2fSLaura Abbott update_sections_early(nx_perms, ARRAY_SIZE(nx_perms)); 70408925c2fSLaura Abbott return 0; 70508925c2fSLaura Abbott } 70608925c2fSLaura Abbott 70711ce4b33SGrygorii Strashko static void fix_kernmem_perms(void) 70808925c2fSLaura Abbott { 70908925c2fSLaura Abbott stop_machine(__fix_kernmem_perms, NULL, NULL); 7101e6b4811SKees Cook } 71180d6b0c2SKees Cook 71211ce4b33SGrygorii Strashko static int __mark_rodata_ro(void *unused) 71308925c2fSLaura Abbott { 71408925c2fSLaura Abbott update_sections_early(ro_perms, ARRAY_SIZE(ro_perms)); 71508925c2fSLaura Abbott return 0; 71608925c2fSLaura Abbott } 71708925c2fSLaura Abbott 718b4c7e2bdSSteven Rostedt (VMware) static int kernel_set_to_readonly __read_mostly; 719b4c7e2bdSSteven Rostedt (VMware) 72080d6b0c2SKees Cook void mark_rodata_ro(void) 72180d6b0c2SKees Cook { 722b4c7e2bdSSteven Rostedt (VMware) kernel_set_to_readonly = 1; 72308925c2fSLaura Abbott stop_machine(__mark_rodata_ro, NULL, NULL); 724a8e53c15SJinbum Park debug_checkwx(); 72580d6b0c2SKees Cook } 72680d6b0c2SKees Cook 72780d6b0c2SKees Cook void set_kernel_text_rw(void) 72880d6b0c2SKees Cook { 729b4c7e2bdSSteven Rostedt (VMware) if (!kernel_set_to_readonly) 730b4c7e2bdSSteven Rostedt (VMware) return; 731b4c7e2bdSSteven Rostedt (VMware) 73208925c2fSLaura Abbott set_section_perms(ro_perms, ARRAY_SIZE(ro_perms), false, 73308925c2fSLaura Abbott current->active_mm); 73480d6b0c2SKees Cook } 73580d6b0c2SKees Cook 73680d6b0c2SKees Cook void set_kernel_text_ro(void) 73780d6b0c2SKees Cook { 738b4c7e2bdSSteven Rostedt (VMware) if (!kernel_set_to_readonly) 739b4c7e2bdSSteven Rostedt (VMware) return; 740b4c7e2bdSSteven Rostedt (VMware) 74108925c2fSLaura Abbott set_section_perms(ro_perms, ARRAY_SIZE(ro_perms), true, 74208925c2fSLaura Abbott current->active_mm); 74380d6b0c2SKees Cook } 74480d6b0c2SKees Cook 7451e6b4811SKees Cook #else 7461e6b4811SKees Cook static inline void fix_kernmem_perms(void) { } 7470f5bf6d0SLaura Abbott #endif /* CONFIG_STRICT_KERNEL_RWX */ 7481e6b4811SKees Cook 7491e6b4811SKees Cook void free_initmem(void) 7501e6b4811SKees Cook { 7511e6b4811SKees Cook fix_kernmem_perms(); 752bc581770SLinus Walleij 75354d52573SStephen Boyd poison_init_mem(__init_begin, __init_end - __init_begin); 7546db015e4SNicolas Pitre if (!machine_is_integrator() && !machine_is_cintegrator()) 755dbe67df4SJiang Liu free_initmem_default(-1); 7561da177e4SLinus Torvalds } 7571da177e4SLinus Torvalds 7581da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_INITRD 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds static int keep_initrd; 7611da177e4SLinus Torvalds 7621da177e4SLinus Torvalds void free_initrd_mem(unsigned long start, unsigned long end) 7631da177e4SLinus Torvalds { 76454d52573SStephen Boyd if (!keep_initrd) { 765421520baSYalin Wang if (start == initrd_start) 766421520baSYalin Wang start = round_down(start, PAGE_SIZE); 767421520baSYalin Wang if (end == initrd_end) 768421520baSYalin Wang end = round_up(end, PAGE_SIZE); 769421520baSYalin Wang 77054d52573SStephen Boyd poison_init_mem((void *)start, PAGE_ALIGN(end) - start); 771dbe67df4SJiang Liu free_reserved_area((void *)start, (void *)end, -1, "initrd"); 7721da177e4SLinus Torvalds } 77354d52573SStephen Boyd } 7741da177e4SLinus Torvalds 7751da177e4SLinus Torvalds static int __init keepinitrd_setup(char *__unused) 7761da177e4SLinus Torvalds { 7771da177e4SLinus Torvalds keep_initrd = 1; 7781da177e4SLinus Torvalds return 1; 7791da177e4SLinus Torvalds } 7801da177e4SLinus Torvalds 7811da177e4SLinus Torvalds __setup("keepinitrd", keepinitrd_setup); 7821da177e4SLinus Torvalds #endif 783