xref: /openbmc/linux/arch/powerpc/kernel/module.c (revision 5f66f73b)
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/bug.h>
12 #include <asm/module.h>
13 #include <linux/uaccess.h>
14 #include <asm/firmware.h>
15 #include <linux/sort.h>
16 #include <asm/setup.h>
17 #include <asm/sections.h>
18 
19 static LIST_HEAD(module_bug_list);
20 
21 static const Elf_Shdr *find_section(const Elf_Ehdr *hdr,
22 				    const Elf_Shdr *sechdrs,
23 				    const char *name)
24 {
25 	char *secstrings;
26 	unsigned int i;
27 
28 	secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
29 	for (i = 1; i < hdr->e_shnum; i++)
30 		if (strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
31 			return &sechdrs[i];
32 	return NULL;
33 }
34 
35 int module_finalize(const Elf_Ehdr *hdr,
36 		const Elf_Shdr *sechdrs, struct module *me)
37 {
38 	const Elf_Shdr *sect;
39 	int rc;
40 
41 	rc = module_finalize_ftrace(me, sechdrs);
42 	if (rc)
43 		return rc;
44 
45 	/* Apply feature fixups */
46 	sect = find_section(hdr, sechdrs, "__ftr_fixup");
47 	if (sect != NULL)
48 		do_feature_fixups(cur_cpu_spec->cpu_features,
49 				  (void *)sect->sh_addr,
50 				  (void *)sect->sh_addr + sect->sh_size);
51 
52 	sect = find_section(hdr, sechdrs, "__mmu_ftr_fixup");
53 	if (sect != NULL)
54 		do_feature_fixups(cur_cpu_spec->mmu_features,
55 				  (void *)sect->sh_addr,
56 				  (void *)sect->sh_addr + sect->sh_size);
57 
58 #ifdef CONFIG_PPC64
59 	sect = find_section(hdr, sechdrs, "__fw_ftr_fixup");
60 	if (sect != NULL)
61 		do_feature_fixups(powerpc_firmware_features,
62 				  (void *)sect->sh_addr,
63 				  (void *)sect->sh_addr + sect->sh_size);
64 #endif /* CONFIG_PPC64 */
65 
66 #ifdef PPC64_ELF_ABI_v1
67 	sect = find_section(hdr, sechdrs, ".opd");
68 	if (sect != NULL) {
69 		me->arch.start_opd = sect->sh_addr;
70 		me->arch.end_opd = sect->sh_addr + sect->sh_size;
71 	}
72 #endif /* PPC64_ELF_ABI_v1 */
73 
74 #ifdef CONFIG_PPC_BARRIER_NOSPEC
75 	sect = find_section(hdr, sechdrs, "__spec_barrier_fixup");
76 	if (sect != NULL)
77 		do_barrier_nospec_fixups_range(barrier_nospec_enabled,
78 				  (void *)sect->sh_addr,
79 				  (void *)sect->sh_addr + sect->sh_size);
80 #endif /* CONFIG_PPC_BARRIER_NOSPEC */
81 
82 	sect = find_section(hdr, sechdrs, "__lwsync_fixup");
83 	if (sect != NULL)
84 		do_lwsync_fixups(cur_cpu_spec->cpu_features,
85 				 (void *)sect->sh_addr,
86 				 (void *)sect->sh_addr + sect->sh_size);
87 
88 	return 0;
89 }
90 
91 #ifdef MODULES_VADDR
92 static __always_inline void *
93 __module_alloc(unsigned long size, unsigned long start, unsigned long end)
94 {
95 	return __vmalloc_node_range(size, 1, start, end, GFP_KERNEL,
96 				    PAGE_KERNEL_EXEC, VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
97 				    __builtin_return_address(0));
98 }
99 
100 void *module_alloc(unsigned long size)
101 {
102 	unsigned long limit = (unsigned long)_etext - SZ_32M;
103 	void *ptr = NULL;
104 
105 	BUILD_BUG_ON(TASK_SIZE > MODULES_VADDR);
106 
107 	/* First try within 32M limit from _etext to avoid branch trampolines */
108 	if (MODULES_VADDR < PAGE_OFFSET && MODULES_END > limit)
109 		ptr = __module_alloc(size, limit, MODULES_END);
110 
111 	if (!ptr)
112 		ptr = __module_alloc(size, MODULES_VADDR, MODULES_END);
113 
114 	return ptr;
115 }
116 #endif
117