1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* Kernel module help for powerpc. 3 Copyright (C) 2001, 2003 Rusty Russell IBM Corporation. 4 Copyright (C) 2008 Freescale Semiconductor, Inc. 5 6 */ 7 #include <linux/elf.h> 8 #include <linux/moduleloader.h> 9 #include <linux/err.h> 10 #include <linux/vmalloc.h> 11 #include <linux/mm.h> 12 #include <linux/bug.h> 13 #include <asm/module.h> 14 #include <linux/uaccess.h> 15 #include <asm/firmware.h> 16 #include <linux/sort.h> 17 #include <asm/setup.h> 18 #include <asm/sections.h> 19 20 static const Elf_Shdr *find_section(const Elf_Ehdr *hdr, 21 const Elf_Shdr *sechdrs, 22 const char *name) 23 { 24 char *secstrings; 25 unsigned int i; 26 27 secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; 28 for (i = 1; i < hdr->e_shnum; i++) 29 if (strcmp(secstrings+sechdrs[i].sh_name, name) == 0) 30 return &sechdrs[i]; 31 return NULL; 32 } 33 34 int module_finalize(const Elf_Ehdr *hdr, 35 const Elf_Shdr *sechdrs, struct module *me) 36 { 37 const Elf_Shdr *sect; 38 int rc; 39 40 rc = module_finalize_ftrace(me, sechdrs); 41 if (rc) 42 return rc; 43 44 /* Apply feature fixups */ 45 sect = find_section(hdr, sechdrs, "__ftr_fixup"); 46 if (sect != NULL) 47 do_feature_fixups(cur_cpu_spec->cpu_features, 48 (void *)sect->sh_addr, 49 (void *)sect->sh_addr + sect->sh_size); 50 51 sect = find_section(hdr, sechdrs, "__mmu_ftr_fixup"); 52 if (sect != NULL) 53 do_feature_fixups(cur_cpu_spec->mmu_features, 54 (void *)sect->sh_addr, 55 (void *)sect->sh_addr + sect->sh_size); 56 57 #ifdef CONFIG_PPC64 58 sect = find_section(hdr, sechdrs, "__fw_ftr_fixup"); 59 if (sect != NULL) 60 do_feature_fixups(powerpc_firmware_features, 61 (void *)sect->sh_addr, 62 (void *)sect->sh_addr + sect->sh_size); 63 #endif /* CONFIG_PPC64 */ 64 65 #ifdef CONFIG_PPC64_ELF_ABI_V1 66 sect = find_section(hdr, sechdrs, ".opd"); 67 if (sect != NULL) { 68 me->arch.start_opd = sect->sh_addr; 69 me->arch.end_opd = sect->sh_addr + sect->sh_size; 70 } 71 #endif /* CONFIG_PPC64_ELF_ABI_V1 */ 72 73 #ifdef CONFIG_PPC_BARRIER_NOSPEC 74 sect = find_section(hdr, sechdrs, "__spec_barrier_fixup"); 75 if (sect != NULL) 76 do_barrier_nospec_fixups_range(barrier_nospec_enabled, 77 (void *)sect->sh_addr, 78 (void *)sect->sh_addr + sect->sh_size); 79 #endif /* CONFIG_PPC_BARRIER_NOSPEC */ 80 81 sect = find_section(hdr, sechdrs, "__lwsync_fixup"); 82 if (sect != NULL) 83 do_lwsync_fixups(cur_cpu_spec->cpu_features, 84 (void *)sect->sh_addr, 85 (void *)sect->sh_addr + sect->sh_size); 86 87 return 0; 88 } 89 90 static __always_inline void * 91 __module_alloc(unsigned long size, unsigned long start, unsigned long end, bool nowarn) 92 { 93 pgprot_t prot = strict_module_rwx_enabled() ? PAGE_KERNEL : PAGE_KERNEL_EXEC; 94 gfp_t gfp = GFP_KERNEL | (nowarn ? __GFP_NOWARN : 0); 95 96 /* 97 * Don't do huge page allocations for modules yet until more testing 98 * is done. STRICT_MODULE_RWX may require extra work to support this 99 * too. 100 */ 101 return __vmalloc_node_range(size, 1, start, end, gfp, prot, 102 VM_FLUSH_RESET_PERMS, 103 NUMA_NO_NODE, __builtin_return_address(0)); 104 } 105 106 void *module_alloc(unsigned long size) 107 { 108 #ifdef MODULES_VADDR 109 unsigned long limit = (unsigned long)_etext - SZ_32M; 110 void *ptr = NULL; 111 112 BUILD_BUG_ON(TASK_SIZE > MODULES_VADDR); 113 114 /* First try within 32M limit from _etext to avoid branch trampolines */ 115 if (MODULES_VADDR < PAGE_OFFSET && MODULES_END > limit) 116 ptr = __module_alloc(size, limit, MODULES_END, true); 117 118 if (!ptr) 119 ptr = __module_alloc(size, MODULES_VADDR, MODULES_END, false); 120 121 return ptr; 122 #else 123 return __module_alloc(size, VMALLOC_START, VMALLOC_END, false); 124 #endif 125 } 126