xref: /linux/arch/powerpc/kernel/module_64.c (revision f15d97df5afae16f40ecef942031235d1c6ba14f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*  Kernel module help for PPC64.
3     Copyright (C) 2001, 2003 Rusty Russell IBM Corporation.
4 
5 */
6 
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 
9 #include <linux/module.h>
10 #include <linux/elf.h>
11 #include <linux/moduleloader.h>
12 #include <linux/err.h>
13 #include <linux/vmalloc.h>
14 #include <linux/ftrace.h>
15 #include <linux/bug.h>
16 #include <linux/uaccess.h>
17 #include <linux/kernel.h>
18 #include <asm/module.h>
19 #include <asm/firmware.h>
20 #include <asm/text-patching.h>
21 #include <linux/sort.h>
22 #include <asm/setup.h>
23 #include <asm/sections.h>
24 #include <asm/inst.h>
25 
26 /* FIXME: We don't do .init separately.  To do this, we'd need to have
27    a separate r2 value in the init and core section, and stub between
28    them, too.
29 
30    Using a magic allocator which places modules within 32MB solves
31    this, and makes other things simpler.  Anton?
32    --RR.  */
33 
module_elf_check_arch(Elf_Ehdr * hdr)34 bool module_elf_check_arch(Elf_Ehdr *hdr)
35 {
36 	unsigned long abi_level = hdr->e_flags & 0x3;
37 
38 	if (IS_ENABLED(CONFIG_PPC64_ELF_ABI_V2))
39 		return abi_level == 2;
40 	else
41 		return abi_level < 2;
42 }
43 
44 #ifdef CONFIG_PPC64_ELF_ABI_V2
45 
func_desc(unsigned long addr)46 static func_desc_t func_desc(unsigned long addr)
47 {
48 	func_desc_t desc = {
49 		.addr = addr,
50 	};
51 
52 	return desc;
53 }
54 
55 /* PowerPC64 specific values for the Elf64_Sym st_other field.  */
56 #define STO_PPC64_LOCAL_BIT	5
57 #define STO_PPC64_LOCAL_MASK	(7 << STO_PPC64_LOCAL_BIT)
58 #define PPC64_LOCAL_ENTRY_OFFSET(other)					\
59  (((1 << (((other) & STO_PPC64_LOCAL_MASK) >> STO_PPC64_LOCAL_BIT)) >> 2) << 2)
60 
local_entry_offset(const Elf64_Sym * sym)61 static unsigned int local_entry_offset(const Elf64_Sym *sym)
62 {
63 	/* sym->st_other indicates offset to local entry point
64 	 * (otherwise it will assume r12 is the address of the start
65 	 * of function and try to derive r2 from it). */
66 	return PPC64_LOCAL_ENTRY_OFFSET(sym->st_other);
67 }
68 #else
69 
func_desc(unsigned long addr)70 static func_desc_t func_desc(unsigned long addr)
71 {
72 	return *(struct func_desc *)addr;
73 }
local_entry_offset(const Elf64_Sym * sym)74 static unsigned int local_entry_offset(const Elf64_Sym *sym)
75 {
76 	return 0;
77 }
78 
dereference_module_function_descriptor(struct module * mod,void * ptr)79 void *dereference_module_function_descriptor(struct module *mod, void *ptr)
80 {
81 	if (ptr < (void *)mod->arch.start_opd ||
82 			ptr >= (void *)mod->arch.end_opd)
83 		return ptr;
84 
85 	return dereference_function_descriptor(ptr);
86 }
87 #endif
88 
func_addr(unsigned long addr)89 static unsigned long func_addr(unsigned long addr)
90 {
91 	return func_desc(addr).addr;
92 }
93 
stub_func_addr(func_desc_t func)94 static unsigned long stub_func_addr(func_desc_t func)
95 {
96 	return func.addr;
97 }
98 
99 #define STUB_MAGIC 0x73747562 /* stub */
100 
101 /* Like PPC32, we need little trampolines to do > 24-bit jumps (into
102    the kernel itself).  But on PPC64, these need to be used for every
103    jump, actually, to reset r2 (TOC+0x8000). */
104 struct ppc64_stub_entry {
105 	/*
106 	 * 28 byte jump instruction sequence (7 instructions) that can
107 	 * hold ppc64_stub_insns or stub_insns. Must be 8-byte aligned
108 	 * with PCREL kernels that use prefix instructions in the stub.
109 	 */
110 	u32 jump[7];
111 	/* Used by ftrace to identify stubs */
112 	u32 magic;
113 	/* Data for the above code */
114 	func_desc_t funcdata;
115 } __aligned(8);
116 
117 struct ppc64_got_entry {
118 	u64 addr;
119 };
120 
121 /*
122  * PPC64 uses 24 bit jumps, but we need to jump into other modules or
123  * the kernel which may be further.  So we jump to a stub.
124  *
125  * Target address and TOC are loaded from function descriptor in the
126  * ppc64_stub_entry.
127  *
128  * r12 is used to generate the target address, which is required for the
129  * ELFv2 global entry point calling convention.
130  *
131  * TOC handling:
132  * - PCREL does not have a TOC.
133  * - ELFv2 non-PCREL just has to save r2, the callee is responsible for
134  *   setting its own TOC pointer at the global entry address.
135  * - ELFv1 must load the new TOC pointer from the function descriptor.
136  */
137 static u32 ppc64_stub_insns[] = {
138 #ifdef CONFIG_PPC_KERNEL_PCREL
139 	/* pld r12,addr */
140 	PPC_PREFIX_8LS | __PPC_PRFX_R(1),
141 	PPC_INST_PLD | ___PPC_RT(_R12),
142 #else
143 	PPC_RAW_ADDIS(_R11, _R2, 0),
144 	PPC_RAW_ADDI(_R11, _R11, 0),
145 	/* Save current r2 value in magic place on the stack. */
146 	PPC_RAW_STD(_R2, _R1, R2_STACK_OFFSET),
147 	PPC_RAW_LD(_R12, _R11, 32),
148 #ifdef CONFIG_PPC64_ELF_ABI_V1
149 	/* Set up new r2 from function descriptor */
150 	PPC_RAW_LD(_R2, _R11, 40),
151 #endif
152 #endif
153 	PPC_RAW_MTCTR(_R12),
154 	PPC_RAW_BCTR(),
155 };
156 
157 /*
158  * Count how many different r_type relocations (different symbol,
159  * different addend).
160  */
count_relocs(const Elf64_Rela * rela,unsigned int num,unsigned long r_type)161 static unsigned int count_relocs(const Elf64_Rela *rela, unsigned int num,
162 				 unsigned long r_type)
163 {
164 	unsigned int i, r_info, r_addend, _count_relocs;
165 
166 	/* FIXME: Only count external ones --RR */
167 	_count_relocs = 0;
168 	r_info = 0;
169 	r_addend = 0;
170 	for (i = 0; i < num; i++)
171 		/* Only count r_type relocs, others don't need stubs */
172 		if (ELF64_R_TYPE(rela[i].r_info) == r_type &&
173 		    (r_info != ELF64_R_SYM(rela[i].r_info) ||
174 		     r_addend != rela[i].r_addend)) {
175 			_count_relocs++;
176 			r_info = ELF64_R_SYM(rela[i].r_info);
177 			r_addend = rela[i].r_addend;
178 		}
179 
180 	return _count_relocs;
181 }
182 
relacmp(const void * _x,const void * _y)183 static int relacmp(const void *_x, const void *_y)
184 {
185 	const Elf64_Rela *x, *y;
186 
187 	y = (Elf64_Rela *)_x;
188 	x = (Elf64_Rela *)_y;
189 
190 	/* Compare the entire r_info (as opposed to ELF64_R_SYM(r_info) only) to
191 	 * make the comparison cheaper/faster. It won't affect the sorting or
192 	 * the counting algorithms' performance
193 	 */
194 	if (x->r_info < y->r_info)
195 		return -1;
196 	else if (x->r_info > y->r_info)
197 		return 1;
198 	else if (x->r_addend < y->r_addend)
199 		return -1;
200 	else if (x->r_addend > y->r_addend)
201 		return 1;
202 	else
203 		return 0;
204 }
205 
206 /* Get size of potential trampolines required. */
get_stubs_size(const Elf64_Ehdr * hdr,const Elf64_Shdr * sechdrs,char * secstrings,struct module * me)207 static unsigned long get_stubs_size(const Elf64_Ehdr *hdr,
208 				    const Elf64_Shdr *sechdrs,
209 				    char *secstrings,
210 				    struct module *me)
211 {
212 	/* One extra reloc so it's always 0-addr terminated */
213 	unsigned long relocs = 1;
214 	unsigned i;
215 
216 	/* Every relocated section... */
217 	for (i = 1; i < hdr->e_shnum; i++) {
218 		if (sechdrs[i].sh_type == SHT_RELA) {
219 			pr_debug("Found relocations in section %u\n", i);
220 			pr_debug("Ptr: %p.  Number: %Lu\n",
221 			       (void *)sechdrs[i].sh_addr,
222 			       sechdrs[i].sh_size / sizeof(Elf64_Rela));
223 
224 			/* Sort the relocation information based on a symbol and
225 			 * addend key. This is a stable O(n*log n) complexity
226 			 * algorithm but it will reduce the complexity of
227 			 * count_relocs() to linear complexity O(n)
228 			 */
229 			sort((void *)sechdrs[i].sh_addr,
230 			     sechdrs[i].sh_size / sizeof(Elf64_Rela),
231 			     sizeof(Elf64_Rela), relacmp, NULL);
232 
233 			relocs += count_relocs((void *)sechdrs[i].sh_addr,
234 					       sechdrs[i].sh_size
235 					       / sizeof(Elf64_Rela),
236 					       R_PPC_REL24);
237 #ifdef CONFIG_PPC_KERNEL_PCREL
238 			relocs += count_relocs((void *)sechdrs[i].sh_addr,
239 					       sechdrs[i].sh_size
240 					       / sizeof(Elf64_Rela),
241 					       R_PPC64_REL24_NOTOC);
242 #endif
243 		}
244 	}
245 
246 	/* stubs for ftrace_caller and ftrace_regs_caller */
247 	relocs += IS_ENABLED(CONFIG_DYNAMIC_FTRACE) + IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS);
248 
249 #ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE
250 	/* stubs for the function tracer */
251 	for (i = 1; i < hdr->e_shnum; i++) {
252 		if (!strcmp(secstrings + sechdrs[i].sh_name, "__patchable_function_entries")) {
253 			me->arch.ool_stub_count = sechdrs[i].sh_size / sizeof(unsigned long);
254 			me->arch.ool_stub_index = 0;
255 			relocs += roundup(me->arch.ool_stub_count * sizeof(struct ftrace_ool_stub),
256 					  sizeof(struct ppc64_stub_entry)) /
257 				  sizeof(struct ppc64_stub_entry);
258 			break;
259 		}
260 	}
261 #endif
262 
263 	pr_debug("Looks like a total of %lu stubs, max\n", relocs);
264 	return relocs * sizeof(struct ppc64_stub_entry);
265 }
266 
267 #ifdef CONFIG_PPC_KERNEL_PCREL
count_pcpu_relocs(const Elf64_Shdr * sechdrs,const Elf64_Rela * rela,unsigned int num,unsigned int symindex,unsigned int pcpu)268 static int count_pcpu_relocs(const Elf64_Shdr *sechdrs,
269 			     const Elf64_Rela *rela, unsigned int num,
270 			     unsigned int symindex, unsigned int pcpu)
271 {
272 	unsigned int i, r_info, r_addend, _count_relocs;
273 
274 	_count_relocs = 0;
275 	r_info = 0;
276 	r_addend = 0;
277 
278 	for (i = 0; i < num; i++) {
279 		Elf64_Sym *sym;
280 
281 		/* This is the symbol it is referring to */
282 		sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
283 			+ ELF64_R_SYM(rela[i].r_info);
284 
285 		if (sym->st_shndx == pcpu &&
286 		    (r_info != ELF64_R_SYM(rela[i].r_info) ||
287 		     r_addend != rela[i].r_addend)) {
288 			_count_relocs++;
289 			r_info = ELF64_R_SYM(rela[i].r_info);
290 			r_addend = rela[i].r_addend;
291 		}
292 	}
293 
294 	return _count_relocs;
295 }
296 
297 /* Get size of potential GOT required. */
get_got_size(const Elf64_Ehdr * hdr,const Elf64_Shdr * sechdrs,struct module * me)298 static unsigned long get_got_size(const Elf64_Ehdr *hdr,
299 				  const Elf64_Shdr *sechdrs,
300 				  struct module *me)
301 {
302 	/* One extra reloc so it's always 0-addr terminated */
303 	unsigned long relocs = 1;
304 	unsigned int i, symindex = 0;
305 
306 	for (i = 1; i < hdr->e_shnum; i++) {
307 		if (sechdrs[i].sh_type == SHT_SYMTAB) {
308 			symindex = i;
309 			break;
310 		}
311 	}
312 	WARN_ON_ONCE(!symindex);
313 
314 	/* Every relocated section... */
315 	for (i = 1; i < hdr->e_shnum; i++) {
316 		if (sechdrs[i].sh_type == SHT_RELA) {
317 			pr_debug("Found relocations in section %u\n", i);
318 			pr_debug("Ptr: %p.  Number: %llu\n", (void *)sechdrs[i].sh_addr,
319 				 sechdrs[i].sh_size / sizeof(Elf64_Rela));
320 
321 			/*
322 			 * Sort the relocation information based on a symbol and
323 			 * addend key. This is a stable O(n*log n) complexity
324 			 * algorithm but it will reduce the complexity of
325 			 * count_relocs() to linear complexity O(n)
326 			 */
327 			sort((void *)sechdrs[i].sh_addr,
328 			     sechdrs[i].sh_size / sizeof(Elf64_Rela),
329 			     sizeof(Elf64_Rela), relacmp, NULL);
330 
331 			relocs += count_relocs((void *)sechdrs[i].sh_addr,
332 					       sechdrs[i].sh_size
333 					       / sizeof(Elf64_Rela),
334 					       R_PPC64_GOT_PCREL34);
335 
336 			/*
337 			 * Percpu data access typically gets linked with
338 			 * REL34 relocations, but the percpu section gets
339 			 * moved at load time and requires that to be
340 			 * converted to GOT linkage.
341 			 */
342 			if (IS_ENABLED(CONFIG_SMP) && symindex)
343 				relocs += count_pcpu_relocs(sechdrs,
344 						(void *)sechdrs[i].sh_addr,
345 					       sechdrs[i].sh_size
346 					       / sizeof(Elf64_Rela),
347 					       symindex, me->arch.pcpu_section);
348 		}
349 	}
350 
351 	pr_debug("Looks like a total of %lu GOT entries, max\n", relocs);
352 	return relocs * sizeof(struct ppc64_got_entry);
353 }
354 #else /* CONFIG_PPC_KERNEL_PCREL */
355 
356 /* Still needed for ELFv2, for .TOC. */
dedotify_versions(struct modversion_info * vers,unsigned long size)357 static void dedotify_versions(struct modversion_info *vers,
358 			      unsigned long size)
359 {
360 	struct modversion_info *end;
361 
362 	for (end = (void *)vers + size; vers < end; vers++)
363 		if (vers->name[0] == '.') {
364 			memmove(vers->name, vers->name+1, strlen(vers->name));
365 		}
366 }
367 
368 /* Same as normal versions, remove a leading dot if present. */
dedotify_ext_version_names(char * str_seq,unsigned long size)369 static void dedotify_ext_version_names(char *str_seq, unsigned long size)
370 {
371 	unsigned long out = 0;
372 	unsigned long in;
373 	char last = '\0';
374 
375 	for (in = 0; in < size; in++) {
376 		/* Skip one leading dot */
377 		if (last == '\0' && str_seq[in] == '.')
378 			in++;
379 		last = str_seq[in];
380 		str_seq[out++] = last;
381 	}
382 	/* Zero the trailing portion of the names table for robustness */
383 	memset(&str_seq[out], 0, size - out);
384 }
385 
386 /*
387  * Undefined symbols which refer to .funcname, hack to funcname. Make .TOC.
388  * seem to be defined (value set later).
389  */
dedotify(Elf64_Sym * syms,unsigned int numsyms,char * strtab)390 static void dedotify(Elf64_Sym *syms, unsigned int numsyms, char *strtab)
391 {
392 	unsigned int i;
393 
394 	for (i = 1; i < numsyms; i++) {
395 		if (syms[i].st_shndx == SHN_UNDEF) {
396 			char *name = strtab + syms[i].st_name;
397 			if (name[0] == '.') {
398 				if (strcmp(name+1, "TOC.") == 0)
399 					syms[i].st_shndx = SHN_ABS;
400 				syms[i].st_name++;
401 			}
402 		}
403 	}
404 }
405 
find_dot_toc(Elf64_Shdr * sechdrs,const char * strtab,unsigned int symindex)406 static Elf64_Sym *find_dot_toc(Elf64_Shdr *sechdrs,
407 			       const char *strtab,
408 			       unsigned int symindex)
409 {
410 	unsigned int i, numsyms;
411 	Elf64_Sym *syms;
412 
413 	syms = (Elf64_Sym *)sechdrs[symindex].sh_addr;
414 	numsyms = sechdrs[symindex].sh_size / sizeof(Elf64_Sym);
415 
416 	for (i = 1; i < numsyms; i++) {
417 		if (syms[i].st_shndx == SHN_ABS
418 		    && strcmp(strtab + syms[i].st_name, "TOC.") == 0)
419 			return &syms[i];
420 	}
421 	return NULL;
422 }
423 #endif /* CONFIG_PPC_KERNEL_PCREL */
424 
module_init_section(const char * name)425 bool module_init_section(const char *name)
426 {
427 	/* We don't handle .init for the moment: always return false. */
428 	return false;
429 }
430 
module_frob_arch_sections(Elf64_Ehdr * hdr,Elf64_Shdr * sechdrs,char * secstrings,struct module * me)431 int module_frob_arch_sections(Elf64_Ehdr *hdr,
432 			      Elf64_Shdr *sechdrs,
433 			      char *secstrings,
434 			      struct module *me)
435 {
436 	unsigned int i;
437 
438 	/* Find .toc and .stubs sections, symtab and strtab */
439 	for (i = 1; i < hdr->e_shnum; i++) {
440 		if (strcmp(secstrings + sechdrs[i].sh_name, ".stubs") == 0)
441 			me->arch.stubs_section = i;
442 #ifdef CONFIG_PPC_KERNEL_PCREL
443 		else if (strcmp(secstrings + sechdrs[i].sh_name, ".data..percpu") == 0)
444 			me->arch.pcpu_section = i;
445 		else if (strcmp(secstrings + sechdrs[i].sh_name, ".mygot") == 0) {
446 			me->arch.got_section = i;
447 			if (sechdrs[i].sh_addralign < 8)
448 				sechdrs[i].sh_addralign = 8;
449 		}
450 #else
451 		else if (strcmp(secstrings + sechdrs[i].sh_name, ".toc") == 0) {
452 			me->arch.toc_section = i;
453 			if (sechdrs[i].sh_addralign < 8)
454 				sechdrs[i].sh_addralign = 8;
455 		} else if (strcmp(secstrings + sechdrs[i].sh_name, "__versions") == 0)
456 			dedotify_versions((void *)hdr + sechdrs[i].sh_offset,
457 					  sechdrs[i].sh_size);
458 		else if (strcmp(secstrings + sechdrs[i].sh_name, "__version_ext_names") == 0)
459 			dedotify_ext_version_names((void *)hdr + sechdrs[i].sh_offset,
460 						   sechdrs[i].sh_size);
461 
462 		if (sechdrs[i].sh_type == SHT_SYMTAB)
463 			dedotify((void *)hdr + sechdrs[i].sh_offset,
464 				 sechdrs[i].sh_size / sizeof(Elf64_Sym),
465 				 (void *)hdr
466 				 + sechdrs[sechdrs[i].sh_link].sh_offset);
467 #endif
468 	}
469 
470 	if (!me->arch.stubs_section) {
471 		pr_err("%s: doesn't contain .stubs.\n", me->name);
472 		return -ENOEXEC;
473 	}
474 
475 #ifdef CONFIG_PPC_KERNEL_PCREL
476 	if (!me->arch.got_section) {
477 		pr_err("%s: doesn't contain .mygot.\n", me->name);
478 		return -ENOEXEC;
479 	}
480 
481 	/* Override the got size */
482 	sechdrs[me->arch.got_section].sh_size = get_got_size(hdr, sechdrs, me);
483 #else
484 	/* If we don't have a .toc, just use .stubs.  We need to set r2
485 	   to some reasonable value in case the module calls out to
486 	   other functions via a stub, or if a function pointer escapes
487 	   the module by some means.  */
488 	if (!me->arch.toc_section)
489 		me->arch.toc_section = me->arch.stubs_section;
490 #endif
491 
492 	/* Override the stubs size */
493 	sechdrs[me->arch.stubs_section].sh_size = get_stubs_size(hdr, sechdrs, secstrings, me);
494 
495 	return 0;
496 }
497 
498 #if defined(CONFIG_MPROFILE_KERNEL) || defined(CONFIG_ARCH_USING_PATCHABLE_FUNCTION_ENTRY)
499 
500 static u32 stub_insns[] = {
501 #ifdef CONFIG_PPC_KERNEL_PCREL
502 	PPC_RAW_LD(_R12, _R13, offsetof(struct paca_struct, kernelbase)),
503 	PPC_RAW_NOP(), /* align the prefix insn */
504 	/* paddi r12,r12,addr */
505 	PPC_PREFIX_MLS | __PPC_PRFX_R(0),
506 	PPC_INST_PADDI | ___PPC_RT(_R12) | ___PPC_RA(_R12),
507 	PPC_RAW_MTCTR(_R12),
508 	PPC_RAW_BCTR(),
509 #else
510 	PPC_RAW_LD(_R12, _R13, offsetof(struct paca_struct, kernel_toc)),
511 	PPC_RAW_ADDIS(_R12, _R12, 0),
512 	PPC_RAW_ADDI(_R12, _R12, 0),
513 	PPC_RAW_MTCTR(_R12),
514 	PPC_RAW_BCTR(),
515 #endif
516 };
517 
518 /*
519  * For mprofile-kernel we use a special stub for ftrace_caller() because we
520  * can't rely on r2 containing this module's TOC when we enter the stub.
521  *
522  * That can happen if the function calling us didn't need to use the toc. In
523  * that case it won't have setup r2, and the r2 value will be either the
524  * kernel's toc, or possibly another modules toc.
525  *
526  * To deal with that this stub uses the kernel toc, which is always accessible
527  * via the paca (in r13). The target (ftrace_caller()) is responsible for
528  * saving and restoring the toc before returning.
529  */
create_ftrace_stub(struct ppc64_stub_entry * entry,unsigned long addr,struct module * me)530 static inline int create_ftrace_stub(struct ppc64_stub_entry *entry,
531 					unsigned long addr,
532 					struct module *me)
533 {
534 	long reladdr;
535 
536 	if ((unsigned long)entry->jump % 8 != 0) {
537 		pr_err("%s: Address of stub entry is not 8-byte aligned\n", me->name);
538 		return 0;
539 	}
540 
541 	BUILD_BUG_ON(sizeof(stub_insns) > sizeof(entry->jump));
542 	memcpy(entry->jump, stub_insns, sizeof(stub_insns));
543 
544 	if (IS_ENABLED(CONFIG_PPC_KERNEL_PCREL)) {
545 		/* Stub uses address relative to kernel base (from the paca) */
546 		reladdr = addr - local_paca->kernelbase;
547 		if (reladdr > 0x1FFFFFFFFL || reladdr < -0x200000000L) {
548 			pr_err("%s: Address of %ps out of range of 34-bit relative address.\n",
549 				me->name, (void *)addr);
550 			return 0;
551 		}
552 
553 		entry->jump[2] |= IMM_H18(reladdr);
554 		entry->jump[3] |= IMM_L(reladdr);
555 	} else {
556 		/* Stub uses address relative to kernel toc (from the paca) */
557 		reladdr = addr - kernel_toc_addr();
558 		if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) {
559 			pr_err("%s: Address of %ps out of range of kernel_toc.\n",
560 				me->name, (void *)addr);
561 			return 0;
562 		}
563 
564 		entry->jump[1] |= PPC_HA(reladdr);
565 		entry->jump[2] |= PPC_LO(reladdr);
566 	}
567 
568 	/* Even though we don't use funcdata in the stub, it's needed elsewhere. */
569 	entry->funcdata = func_desc(addr);
570 	entry->magic = STUB_MAGIC;
571 
572 	return 1;
573 }
574 
is_mprofile_ftrace_call(const char * name)575 static bool is_mprofile_ftrace_call(const char *name)
576 {
577 	if (!strcmp("_mcount", name))
578 		return true;
579 #ifdef CONFIG_DYNAMIC_FTRACE
580 	if (!strcmp("ftrace_caller", name))
581 		return true;
582 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
583 	if (!strcmp("ftrace_regs_caller", name))
584 		return true;
585 #endif
586 #endif
587 
588 	return false;
589 }
590 #else
create_ftrace_stub(struct ppc64_stub_entry * entry,unsigned long addr,struct module * me)591 static inline int create_ftrace_stub(struct ppc64_stub_entry *entry,
592 					unsigned long addr,
593 					struct module *me)
594 {
595 	return 0;
596 }
597 
is_mprofile_ftrace_call(const char * name)598 static bool is_mprofile_ftrace_call(const char *name)
599 {
600 	return false;
601 }
602 #endif
603 
604 /*
605  * r2 is the TOC pointer: it actually points 0x8000 into the TOC (this gives the
606  * value maximum span in an instruction which uses a signed offset). Round down
607  * to a 256 byte boundary for the odd case where we are setting up r2 without a
608  * .toc section.
609  */
my_r2(const Elf64_Shdr * sechdrs,struct module * me)610 static inline unsigned long my_r2(const Elf64_Shdr *sechdrs, struct module *me)
611 {
612 #ifndef CONFIG_PPC_KERNEL_PCREL
613 	return (sechdrs[me->arch.toc_section].sh_addr & ~0xfful) + 0x8000;
614 #else
615 	return -1;
616 #endif
617 }
618 
619 /* Patch stub to reference function and correct r2 value. */
create_stub(const Elf64_Shdr * sechdrs,struct ppc64_stub_entry * entry,unsigned long addr,struct module * me,const char * name)620 static inline int create_stub(const Elf64_Shdr *sechdrs,
621 			      struct ppc64_stub_entry *entry,
622 			      unsigned long addr,
623 			      struct module *me,
624 			      const char *name)
625 {
626 	long reladdr;
627 	func_desc_t desc;
628 	int i;
629 
630 	if (is_mprofile_ftrace_call(name))
631 		return create_ftrace_stub(entry, addr, me);
632 
633 	if ((unsigned long)entry->jump % 8 != 0) {
634 		pr_err("%s: Address of stub entry is not 8-byte aligned\n", me->name);
635 		return 0;
636 	}
637 
638 	BUILD_BUG_ON(sizeof(ppc64_stub_insns) > sizeof(entry->jump));
639 	for (i = 0; i < ARRAY_SIZE(ppc64_stub_insns); i++) {
640 		if (patch_instruction(&entry->jump[i],
641 				      ppc_inst(ppc64_stub_insns[i])))
642 			return 0;
643 	}
644 
645 	if (IS_ENABLED(CONFIG_PPC_KERNEL_PCREL)) {
646 		/* Stub uses address relative to itself! */
647 		reladdr = 0 + offsetof(struct ppc64_stub_entry, funcdata);
648 		BUILD_BUG_ON(reladdr != 32);
649 		if (reladdr > 0x1FFFFFFFFL || reladdr < -0x200000000L) {
650 			pr_err("%s: Address of %p out of range of 34-bit relative address.\n",
651 				me->name, (void *)reladdr);
652 			return 0;
653 		}
654 		pr_debug("Stub %p get data from reladdr %li\n", entry, reladdr);
655 
656 		/* May not even need this if we're relative to 0 */
657 		if (patch_instruction(&entry->jump[0],
658 		    ppc_inst_prefix(entry->jump[0] | IMM_H18(reladdr),
659 				    entry->jump[1] | IMM_L(reladdr))))
660 			return 0;
661 
662 	} else {
663 		/* Stub uses address relative to r2. */
664 		reladdr = (unsigned long)entry - my_r2(sechdrs, me);
665 		if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) {
666 			pr_err("%s: Address %p of stub out of range of %p.\n",
667 			       me->name, (void *)reladdr, (void *)my_r2);
668 			return 0;
669 		}
670 		pr_debug("Stub %p get data from reladdr %li\n", entry, reladdr);
671 
672 		if (patch_instruction(&entry->jump[0],
673 				      ppc_inst(entry->jump[0] | PPC_HA(reladdr))))
674 			return 0;
675 
676 		if (patch_instruction(&entry->jump[1],
677 				      ppc_inst(entry->jump[1] | PPC_LO(reladdr))))
678 			return 0;
679 	}
680 
681 	// func_desc_t is 8 bytes if ABIv2, else 16 bytes
682 	desc = func_desc(addr);
683 	for (i = 0; i < sizeof(func_desc_t) / sizeof(u32); i++) {
684 		if (patch_u32(((u32 *)&entry->funcdata) + i, ((u32 *)&desc)[i]))
685 			return 0;
686 	}
687 
688 	if (patch_u32(&entry->magic, STUB_MAGIC))
689 		return 0;
690 
691 	return 1;
692 }
693 
694 /* Create stub to jump to function described in this OPD/ptr: we need the
695    stub to set up the TOC ptr (r2) for the function. */
stub_for_addr(const Elf64_Shdr * sechdrs,unsigned long addr,struct module * me,const char * name)696 static unsigned long stub_for_addr(const Elf64_Shdr *sechdrs,
697 				   unsigned long addr,
698 				   struct module *me,
699 				   const char *name)
700 {
701 	struct ppc64_stub_entry *stubs;
702 	unsigned int i, num_stubs;
703 
704 	num_stubs = sechdrs[me->arch.stubs_section].sh_size / sizeof(*stubs);
705 
706 	/* Find this stub, or if that fails, the next avail. entry */
707 	stubs = (void *)sechdrs[me->arch.stubs_section].sh_addr;
708 	for (i = 0; stub_func_addr(stubs[i].funcdata); i++) {
709 		if (WARN_ON(i >= num_stubs))
710 			return 0;
711 
712 		if (stub_func_addr(stubs[i].funcdata) == func_addr(addr))
713 			return (unsigned long)&stubs[i];
714 	}
715 
716 	if (!create_stub(sechdrs, &stubs[i], addr, me, name))
717 		return 0;
718 
719 	return (unsigned long)&stubs[i];
720 }
721 
722 #ifdef CONFIG_PPC_KERNEL_PCREL
723 /* Create GOT to load the location described in this ptr */
got_for_addr(const Elf64_Shdr * sechdrs,unsigned long addr,struct module * me,const char * name)724 static unsigned long got_for_addr(const Elf64_Shdr *sechdrs,
725 				  unsigned long addr,
726 				  struct module *me,
727 				  const char *name)
728 {
729 	struct ppc64_got_entry *got;
730 	unsigned int i, num_got;
731 
732 	if (!IS_ENABLED(CONFIG_PPC_KERNEL_PCREL))
733 		return addr;
734 
735 	num_got = sechdrs[me->arch.got_section].sh_size / sizeof(*got);
736 
737 	/* Find this stub, or if that fails, the next avail. entry */
738 	got = (void *)sechdrs[me->arch.got_section].sh_addr;
739 	for (i = 0; got[i].addr; i++) {
740 		if (WARN_ON(i >= num_got))
741 			return 0;
742 
743 		if (got[i].addr == addr)
744 			return (unsigned long)&got[i];
745 	}
746 
747 	got[i].addr = addr;
748 
749 	return (unsigned long)&got[i];
750 }
751 #endif
752 
753 /* We expect a noop next: if it is, replace it with instruction to
754    restore r2. */
restore_r2(const char * name,u32 * instruction,struct module * me)755 static int restore_r2(const char *name, u32 *instruction, struct module *me)
756 {
757 	u32 *prev_insn = instruction - 1;
758 	u32 insn_val = *instruction;
759 
760 	if (IS_ENABLED(CONFIG_PPC_KERNEL_PCREL))
761 		return 0;
762 
763 	if (is_mprofile_ftrace_call(name))
764 		return 0;
765 
766 	/*
767 	 * Make sure the branch isn't a sibling call.  Sibling calls aren't
768 	 * "link" branches and they don't return, so they don't need the r2
769 	 * restore afterwards.
770 	 */
771 	if (!instr_is_relative_link_branch(ppc_inst(*prev_insn)))
772 		return 0;
773 
774 	/*
775 	 * For livepatch, the restore r2 instruction might have already been
776 	 * written previously, if the referenced symbol is in a previously
777 	 * unloaded module which is now being loaded again.  In that case, skip
778 	 * the warning and the instruction write.
779 	 */
780 	if (insn_val == PPC_INST_LD_TOC)
781 		return 0;
782 
783 	if (insn_val != PPC_RAW_NOP()) {
784 		pr_err("%s: Expected nop after call, got %08x at %pS\n",
785 			me->name, insn_val, instruction);
786 		return -ENOEXEC;
787 	}
788 
789 	/* ld r2,R2_STACK_OFFSET(r1) */
790 	return patch_instruction(instruction, ppc_inst(PPC_INST_LD_TOC));
791 }
792 
apply_relocate_add(Elf64_Shdr * sechdrs,const char * strtab,unsigned int symindex,unsigned int relsec,struct module * me)793 int apply_relocate_add(Elf64_Shdr *sechdrs,
794 		       const char *strtab,
795 		       unsigned int symindex,
796 		       unsigned int relsec,
797 		       struct module *me)
798 {
799 	unsigned int i;
800 	Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr;
801 	Elf64_Sym *sym;
802 	unsigned long *location;
803 	unsigned long value;
804 
805 	pr_debug("Applying ADD relocate section %u to %u\n", relsec,
806 	       sechdrs[relsec].sh_info);
807 
808 #ifndef CONFIG_PPC_KERNEL_PCREL
809 	/* First time we're called, we can fix up .TOC. */
810 	if (!me->arch.toc_fixed) {
811 		sym = find_dot_toc(sechdrs, strtab, symindex);
812 		/* It's theoretically possible that a module doesn't want a
813 		 * .TOC. so don't fail it just for that. */
814 		if (sym)
815 			sym->st_value = my_r2(sechdrs, me);
816 		me->arch.toc_fixed = true;
817 	}
818 #endif
819 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
820 		/* This is where to make the change */
821 		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
822 			+ rela[i].r_offset;
823 		/* This is the symbol it is referring to */
824 		sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
825 			+ ELF64_R_SYM(rela[i].r_info);
826 
827 		pr_debug("RELOC at %p: %li-type as %s (0x%lx) + %li\n",
828 		       location, (long)ELF64_R_TYPE(rela[i].r_info),
829 		       strtab + sym->st_name, (unsigned long)sym->st_value,
830 		       (long)rela[i].r_addend);
831 
832 		/* `Everything is relative'. */
833 		value = sym->st_value + rela[i].r_addend;
834 
835 		switch (ELF64_R_TYPE(rela[i].r_info)) {
836 		case R_PPC64_ADDR32:
837 			/* Simply set it */
838 			*(u32 *)location = value;
839 			break;
840 
841 		case R_PPC64_ADDR64:
842 			/* Simply set it */
843 			*(unsigned long *)location = value;
844 			break;
845 
846 #ifndef CONFIG_PPC_KERNEL_PCREL
847 		case R_PPC64_TOC:
848 			*(unsigned long *)location = my_r2(sechdrs, me);
849 			break;
850 
851 		case R_PPC64_TOC16:
852 			/* Subtract TOC pointer */
853 			value -= my_r2(sechdrs, me);
854 			if (value + 0x8000 > 0xffff) {
855 				pr_err("%s: bad TOC16 relocation (0x%lx)\n",
856 				       me->name, value);
857 				return -ENOEXEC;
858 			}
859 			*((uint16_t *) location)
860 				= (*((uint16_t *) location) & ~0xffff)
861 				| (value & 0xffff);
862 			break;
863 
864 		case R_PPC64_TOC16_LO:
865 			/* Subtract TOC pointer */
866 			value -= my_r2(sechdrs, me);
867 			*((uint16_t *) location)
868 				= (*((uint16_t *) location) & ~0xffff)
869 				| (value & 0xffff);
870 			break;
871 
872 		case R_PPC64_TOC16_DS:
873 			/* Subtract TOC pointer */
874 			value -= my_r2(sechdrs, me);
875 			if ((value & 3) != 0 || value + 0x8000 > 0xffff) {
876 				pr_err("%s: bad TOC16_DS relocation (0x%lx)\n",
877 				       me->name, value);
878 				return -ENOEXEC;
879 			}
880 			*((uint16_t *) location)
881 				= (*((uint16_t *) location) & ~0xfffc)
882 				| (value & 0xfffc);
883 			break;
884 
885 		case R_PPC64_TOC16_LO_DS:
886 			/* Subtract TOC pointer */
887 			value -= my_r2(sechdrs, me);
888 			if ((value & 3) != 0) {
889 				pr_err("%s: bad TOC16_LO_DS relocation (0x%lx)\n",
890 				       me->name, value);
891 				return -ENOEXEC;
892 			}
893 			*((uint16_t *) location)
894 				= (*((uint16_t *) location) & ~0xfffc)
895 				| (value & 0xfffc);
896 			break;
897 
898 		case R_PPC64_TOC16_HA:
899 			/* Subtract TOC pointer */
900 			value -= my_r2(sechdrs, me);
901 			value = ((value + 0x8000) >> 16);
902 			*((uint16_t *) location)
903 				= (*((uint16_t *) location) & ~0xffff)
904 				| (value & 0xffff);
905 			break;
906 #endif
907 
908 		case R_PPC_REL24:
909 #ifdef CONFIG_PPC_KERNEL_PCREL
910 		/* PCREL still generates REL24 for mcount */
911 		case R_PPC64_REL24_NOTOC:
912 #endif
913 			/* FIXME: Handle weak symbols here --RR */
914 			if (sym->st_shndx == SHN_UNDEF ||
915 			    sym->st_shndx == SHN_LIVEPATCH) {
916 				/* External: go via stub */
917 				value = stub_for_addr(sechdrs, value, me,
918 						strtab + sym->st_name);
919 				if (!value)
920 					return -ENOENT;
921 				if (restore_r2(strtab + sym->st_name,
922 					       (u32 *)location + 1, me))
923 					return -ENOEXEC;
924 			} else
925 				value += local_entry_offset(sym);
926 
927 			/* Convert value to relative */
928 			value -= (unsigned long)location;
929 			if (value + 0x2000000 > 0x3ffffff || (value & 3) != 0){
930 				pr_err("%s: REL24 %li out of range!\n",
931 				       me->name, (long int)value);
932 				return -ENOEXEC;
933 			}
934 
935 			/* Only replace bits 2 through 26 */
936 			value = (*(uint32_t *)location & ~PPC_LI_MASK) | PPC_LI(value);
937 
938 			if (patch_instruction((u32 *)location, ppc_inst(value)))
939 				return -EFAULT;
940 
941 			break;
942 
943 		case R_PPC64_REL64:
944 			/* 64 bits relative (used by features fixups) */
945 			*location = value - (unsigned long)location;
946 			break;
947 
948 		case R_PPC64_REL32:
949 			/* 32 bits relative (used by relative exception tables) */
950 			/* Convert value to relative */
951 			value -= (unsigned long)location;
952 			if (value + 0x80000000 > 0xffffffff) {
953 				pr_err("%s: REL32 %li out of range!\n",
954 				       me->name, (long int)value);
955 				return -ENOEXEC;
956 			}
957 			*(u32 *)location = value;
958 			break;
959 
960 #ifdef CONFIG_PPC_KERNEL_PCREL
961 		case R_PPC64_PCREL34: {
962 			unsigned long absvalue = value;
963 
964 			/* Convert value to relative */
965 			value -= (unsigned long)location;
966 
967 			if (value + 0x200000000 > 0x3ffffffff) {
968 				if (sym->st_shndx != me->arch.pcpu_section) {
969 					pr_err("%s: REL34 %li out of range!\n",
970 					       me->name, (long)value);
971 					return -ENOEXEC;
972 				}
973 
974 				/*
975 				 * per-cpu section is special cased because
976 				 * it is moved during loading, so has to be
977 				 * converted to use GOT.
978 				 */
979 				value = got_for_addr(sechdrs, absvalue, me,
980 						     strtab + sym->st_name);
981 				if (!value)
982 					return -ENOENT;
983 				value -= (unsigned long)location;
984 
985 				/* Turn pla into pld */
986 				if (patch_instruction((u32 *)location,
987 				    ppc_inst_prefix((*(u32 *)location & ~0x02000000),
988 						    (*((u32 *)location + 1) & ~0xf8000000) | 0xe4000000)))
989 					return -EFAULT;
990 			}
991 
992 			if (patch_instruction((u32 *)location,
993 			    ppc_inst_prefix((*(u32 *)location & ~0x3ffff) | IMM_H18(value),
994 					    (*((u32 *)location + 1) & ~0xffff) | IMM_L(value))))
995 				return -EFAULT;
996 
997 			break;
998 		}
999 
1000 #else
1001 		case R_PPC64_TOCSAVE:
1002 			/*
1003 			 * Marker reloc indicates we don't have to save r2.
1004 			 * That would only save us one instruction, so ignore
1005 			 * it.
1006 			 */
1007 			break;
1008 #endif
1009 
1010 		case R_PPC64_ENTRY:
1011 			if (IS_ENABLED(CONFIG_PPC_KERNEL_PCREL))
1012 				break;
1013 
1014 			/*
1015 			 * Optimize ELFv2 large code model entry point if
1016 			 * the TOC is within 2GB range of current location.
1017 			 */
1018 			value = my_r2(sechdrs, me) - (unsigned long)location;
1019 			if (value + 0x80008000 > 0xffffffff)
1020 				break;
1021 			/*
1022 			 * Check for the large code model prolog sequence:
1023 		         *	ld r2, ...(r12)
1024 			 *	add r2, r2, r12
1025 			 */
1026 			if ((((uint32_t *)location)[0] & ~0xfffc) != PPC_RAW_LD(_R2, _R12, 0))
1027 				break;
1028 			if (((uint32_t *)location)[1] != PPC_RAW_ADD(_R2, _R2, _R12))
1029 				break;
1030 			/*
1031 			 * If found, replace it with:
1032 			 *	addis r2, r12, (.TOC.-func)@ha
1033 			 *	addi  r2,  r2, (.TOC.-func)@l
1034 			 */
1035 			((uint32_t *)location)[0] = PPC_RAW_ADDIS(_R2, _R12, PPC_HA(value));
1036 			((uint32_t *)location)[1] = PPC_RAW_ADDI(_R2, _R2, PPC_LO(value));
1037 			break;
1038 
1039 		case R_PPC64_REL16_HA:
1040 			/* Subtract location pointer */
1041 			value -= (unsigned long)location;
1042 			value = ((value + 0x8000) >> 16);
1043 			*((uint16_t *) location)
1044 				= (*((uint16_t *) location) & ~0xffff)
1045 				| (value & 0xffff);
1046 			break;
1047 
1048 		case R_PPC64_REL16_LO:
1049 			/* Subtract location pointer */
1050 			value -= (unsigned long)location;
1051 			*((uint16_t *) location)
1052 				= (*((uint16_t *) location) & ~0xffff)
1053 				| (value & 0xffff);
1054 			break;
1055 
1056 #ifdef CONFIG_PPC_KERNEL_PCREL
1057 		case R_PPC64_GOT_PCREL34:
1058 			value = got_for_addr(sechdrs, value, me,
1059 					     strtab + sym->st_name);
1060 			if (!value)
1061 				return -ENOENT;
1062 			value -= (unsigned long)location;
1063 			((uint32_t *)location)[0] = (((uint32_t *)location)[0] & ~0x3ffff) |
1064 						    ((value >> 16) & 0x3ffff);
1065 			((uint32_t *)location)[1] = (((uint32_t *)location)[1] & ~0xffff) |
1066 						    (value & 0xffff);
1067 			break;
1068 #endif
1069 
1070 		default:
1071 			pr_err("%s: Unknown ADD relocation: %lu\n",
1072 			       me->name,
1073 			       (unsigned long)ELF64_R_TYPE(rela[i].r_info));
1074 			return -ENOEXEC;
1075 		}
1076 	}
1077 
1078 	return 0;
1079 }
1080 
1081 #ifdef CONFIG_DYNAMIC_FTRACE
module_trampoline_target(struct module * mod,unsigned long addr,unsigned long * target)1082 int module_trampoline_target(struct module *mod, unsigned long addr,
1083 			     unsigned long *target)
1084 {
1085 	struct ppc64_stub_entry *stub;
1086 	func_desc_t funcdata;
1087 	u32 magic;
1088 
1089 	if (!within_module_core(addr, mod)) {
1090 		pr_err("%s: stub %lx not in module %s\n", __func__, addr, mod->name);
1091 		return -EFAULT;
1092 	}
1093 
1094 	stub = (struct ppc64_stub_entry *)addr;
1095 
1096 	if (copy_from_kernel_nofault(&magic, &stub->magic,
1097 			sizeof(magic))) {
1098 		pr_err("%s: fault reading magic for stub %lx for %s\n", __func__, addr, mod->name);
1099 		return -EFAULT;
1100 	}
1101 
1102 	if (magic != STUB_MAGIC) {
1103 		pr_err("%s: bad magic for stub %lx for %s\n", __func__, addr, mod->name);
1104 		return -EFAULT;
1105 	}
1106 
1107 	if (copy_from_kernel_nofault(&funcdata, &stub->funcdata,
1108 			sizeof(funcdata))) {
1109 		pr_err("%s: fault reading funcdata for stub %lx for %s\n", __func__, addr, mod->name);
1110                 return -EFAULT;
1111 	}
1112 
1113 	*target = stub_func_addr(funcdata);
1114 
1115 	return 0;
1116 }
1117 
setup_ftrace_ool_stubs(const Elf64_Shdr * sechdrs,unsigned long addr,struct module * me)1118 static int setup_ftrace_ool_stubs(const Elf64_Shdr *sechdrs, unsigned long addr, struct module *me)
1119 {
1120 #ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE
1121 	unsigned int i, total_stubs, num_stubs;
1122 	struct ppc64_stub_entry *stub;
1123 
1124 	total_stubs = sechdrs[me->arch.stubs_section].sh_size / sizeof(*stub);
1125 	num_stubs = roundup(me->arch.ool_stub_count * sizeof(struct ftrace_ool_stub),
1126 			    sizeof(struct ppc64_stub_entry)) / sizeof(struct ppc64_stub_entry);
1127 
1128 	/* Find the next available entry */
1129 	stub = (void *)sechdrs[me->arch.stubs_section].sh_addr;
1130 	for (i = 0; stub_func_addr(stub[i].funcdata); i++)
1131 		if (WARN_ON(i >= total_stubs))
1132 			return -1;
1133 
1134 	if (WARN_ON(i + num_stubs > total_stubs))
1135 		return -1;
1136 
1137 	stub += i;
1138 	me->arch.ool_stubs = (struct ftrace_ool_stub *)stub;
1139 
1140 	/* reserve stubs */
1141 	for (i = 0; i < num_stubs; i++)
1142 		if (patch_u32((void *)&stub->funcdata, PPC_RAW_NOP()))
1143 			return -1;
1144 #endif
1145 
1146 	return 0;
1147 }
1148 
module_finalize_ftrace(struct module * mod,const Elf_Shdr * sechdrs)1149 int module_finalize_ftrace(struct module *mod, const Elf_Shdr *sechdrs)
1150 {
1151 	mod->arch.tramp = stub_for_addr(sechdrs,
1152 					(unsigned long)ftrace_caller,
1153 					mod,
1154 					"ftrace_caller");
1155 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1156 	mod->arch.tramp_regs = stub_for_addr(sechdrs,
1157 					(unsigned long)ftrace_regs_caller,
1158 					mod,
1159 					"ftrace_regs_caller");
1160 	if (!mod->arch.tramp_regs)
1161 		return -ENOENT;
1162 #endif
1163 
1164 	if (!mod->arch.tramp)
1165 		return -ENOENT;
1166 
1167 	if (setup_ftrace_ool_stubs(sechdrs, mod->arch.tramp, mod))
1168 		return -ENOENT;
1169 
1170 	return 0;
1171 }
1172 #endif
1173