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.
8 #include <linux/moduleloader.h>
10 #include <linux/vmalloc.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>
20 static LIST_HEAD(module_bug_list);
22 static const Elf_Shdr *find_section(const Elf_Ehdr *hdr,
23 const Elf_Shdr *sechdrs,
29 secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
30 for (i = 1; i < hdr->e_shnum; i++)
31 if (strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
36 int module_finalize(const Elf_Ehdr *hdr,
37 const Elf_Shdr *sechdrs, struct module *me)
42 rc = module_finalize_ftrace(me, sechdrs);
46 /* Apply feature fixups */
47 sect = find_section(hdr, sechdrs, "__ftr_fixup");
49 do_feature_fixups(cur_cpu_spec->cpu_features,
50 (void *)sect->sh_addr,
51 (void *)sect->sh_addr + sect->sh_size);
53 sect = find_section(hdr, sechdrs, "__mmu_ftr_fixup");
55 do_feature_fixups(cur_cpu_spec->mmu_features,
56 (void *)sect->sh_addr,
57 (void *)sect->sh_addr + sect->sh_size);
60 sect = find_section(hdr, sechdrs, "__fw_ftr_fixup");
62 do_feature_fixups(powerpc_firmware_features,
63 (void *)sect->sh_addr,
64 (void *)sect->sh_addr + sect->sh_size);
65 #endif /* CONFIG_PPC64 */
67 #ifdef PPC64_ELF_ABI_v1
68 sect = find_section(hdr, sechdrs, ".opd");
70 me->arch.start_opd = sect->sh_addr;
71 me->arch.end_opd = sect->sh_addr + sect->sh_size;
73 #endif /* PPC64_ELF_ABI_v1 */
75 #ifdef CONFIG_PPC_BARRIER_NOSPEC
76 sect = find_section(hdr, sechdrs, "__spec_barrier_fixup");
78 do_barrier_nospec_fixups_range(barrier_nospec_enabled,
79 (void *)sect->sh_addr,
80 (void *)sect->sh_addr + sect->sh_size);
81 #endif /* CONFIG_PPC_BARRIER_NOSPEC */
83 sect = find_section(hdr, sechdrs, "__lwsync_fixup");
85 do_lwsync_fixups(cur_cpu_spec->cpu_features,
86 (void *)sect->sh_addr,
87 (void *)sect->sh_addr + sect->sh_size);
92 static __always_inline void *
93 __module_alloc(unsigned long size, unsigned long start, unsigned long end, bool nowarn)
95 pgprot_t prot = strict_module_rwx_enabled() ? PAGE_KERNEL : PAGE_KERNEL_EXEC;
96 gfp_t gfp = GFP_KERNEL | (nowarn ? __GFP_NOWARN : 0);
99 * Don't do huge page allocations for modules yet until more testing
100 * is done. STRICT_MODULE_RWX may require extra work to support this
103 return __vmalloc_node_range(size, 1, start, end, gfp, prot,
104 VM_FLUSH_RESET_PERMS | VM_NO_HUGE_VMAP,
105 NUMA_NO_NODE, __builtin_return_address(0));
108 void *module_alloc(unsigned long size)
111 unsigned long limit = (unsigned long)_etext - SZ_32M;
114 BUILD_BUG_ON(TASK_SIZE > MODULES_VADDR);
116 /* First try within 32M limit from _etext to avoid branch trampolines */
117 if (MODULES_VADDR < PAGE_OFFSET && MODULES_END > limit)
118 ptr = __module_alloc(size, limit, MODULES_END, true);
121 ptr = __module_alloc(size, MODULES_VADDR, MODULES_END, false);
125 return __module_alloc(size, VMALLOC_START, VMALLOC_END, false);