xref: /linux-6.15/arch/powerpc/kernel/module_64.c (revision 1f2aaed2)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
27568cb4eSPaul Mackerras /*  Kernel module help for PPC64.
37568cb4eSPaul Mackerras     Copyright (C) 2001, 2003 Rusty Russell IBM Corporation.
47568cb4eSPaul Mackerras 
57568cb4eSPaul Mackerras */
6c7d1f6afSAnton Blanchard 
7c7d1f6afSAnton Blanchard #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8c7d1f6afSAnton Blanchard 
97568cb4eSPaul Mackerras #include <linux/module.h>
107568cb4eSPaul Mackerras #include <linux/elf.h>
117568cb4eSPaul Mackerras #include <linux/moduleloader.h>
127568cb4eSPaul Mackerras #include <linux/err.h>
137568cb4eSPaul Mackerras #include <linux/vmalloc.h>
14f48cb8b4SSteven Rostedt #include <linux/ftrace.h>
1573c9ceabSJeremy Fitzhardinge #include <linux/bug.h>
1683775b85SAnton Blanchard #include <linux/uaccess.h>
177568cb4eSPaul Mackerras #include <asm/module.h>
1821c4ff80SBenjamin Herrenschmidt #include <asm/firmware.h>
19b7bcda63SMichael Ellerman #include <asm/code-patching.h>
20eda09fbdSEmil Medve #include <linux/sort.h>
21b88c4767SRobert Jennings #include <asm/setup.h>
22336a7b5dSMichael Ellerman #include <asm/sections.h>
2375346251SJordan Niethe #include <asm/inst.h>
247568cb4eSPaul Mackerras 
257568cb4eSPaul Mackerras /* FIXME: We don't do .init separately.  To do this, we'd need to have
267568cb4eSPaul Mackerras    a separate r2 value in the init and core section, and stub between
277568cb4eSPaul Mackerras    them, too.
287568cb4eSPaul Mackerras 
297568cb4eSPaul Mackerras    Using a magic allocator which places modules within 32MB solves
307568cb4eSPaul Mackerras    this, and makes other things simpler.  Anton?
317568cb4eSPaul Mackerras    --RR.  */
327568cb4eSPaul Mackerras 
33f55d9665SMichael Ellerman #ifdef PPC64_ELF_ABI_v2
34008d7a91SRusty Russell 
35008d7a91SRusty Russell /* An address is simply the address of the function. */
36008d7a91SRusty Russell typedef unsigned long func_desc_t;
37008d7a91SRusty Russell 
38008d7a91SRusty Russell static func_desc_t func_desc(unsigned long addr)
39008d7a91SRusty Russell {
40008d7a91SRusty Russell 	return addr;
41008d7a91SRusty Russell }
42008d7a91SRusty Russell static unsigned long func_addr(unsigned long addr)
43008d7a91SRusty Russell {
44008d7a91SRusty Russell 	return addr;
45008d7a91SRusty Russell }
46008d7a91SRusty Russell static unsigned long stub_func_addr(func_desc_t func)
47008d7a91SRusty Russell {
48008d7a91SRusty Russell 	return func;
49008d7a91SRusty Russell }
50008d7a91SRusty Russell 
51008d7a91SRusty Russell /* PowerPC64 specific values for the Elf64_Sym st_other field.  */
52008d7a91SRusty Russell #define STO_PPC64_LOCAL_BIT	5
53008d7a91SRusty Russell #define STO_PPC64_LOCAL_MASK	(7 << STO_PPC64_LOCAL_BIT)
54008d7a91SRusty Russell #define PPC64_LOCAL_ENTRY_OFFSET(other)					\
55008d7a91SRusty Russell  (((1 << (((other) & STO_PPC64_LOCAL_MASK) >> STO_PPC64_LOCAL_BIT)) >> 2) << 2)
56008d7a91SRusty Russell 
57008d7a91SRusty Russell static unsigned int local_entry_offset(const Elf64_Sym *sym)
58008d7a91SRusty Russell {
59008d7a91SRusty Russell 	/* sym->st_other indicates offset to local entry point
60008d7a91SRusty Russell 	 * (otherwise it will assume r12 is the address of the start
61008d7a91SRusty Russell 	 * of function and try to derive r2 from it). */
62008d7a91SRusty Russell 	return PPC64_LOCAL_ENTRY_OFFSET(sym->st_other);
63008d7a91SRusty Russell }
64d2fae548SRusty Russell #else
65008d7a91SRusty Russell 
66008d7a91SRusty Russell /* An address is address of the OPD entry, which contains address of fn. */
67008d7a91SRusty Russell typedef struct ppc64_opd_entry func_desc_t;
68008d7a91SRusty Russell 
69008d7a91SRusty Russell static func_desc_t func_desc(unsigned long addr)
70008d7a91SRusty Russell {
71008d7a91SRusty Russell 	return *(struct ppc64_opd_entry *)addr;
72008d7a91SRusty Russell }
73008d7a91SRusty Russell static unsigned long func_addr(unsigned long addr)
74008d7a91SRusty Russell {
75008d7a91SRusty Russell 	return func_desc(addr).funcaddr;
76008d7a91SRusty Russell }
77008d7a91SRusty Russell static unsigned long stub_func_addr(func_desc_t func)
78008d7a91SRusty Russell {
79008d7a91SRusty Russell 	return func.funcaddr;
80008d7a91SRusty Russell }
81008d7a91SRusty Russell static unsigned int local_entry_offset(const Elf64_Sym *sym)
82008d7a91SRusty Russell {
83008d7a91SRusty Russell 	return 0;
84008d7a91SRusty Russell }
855633e85bSSergey Senozhatsky 
865633e85bSSergey Senozhatsky void *dereference_module_function_descriptor(struct module *mod, void *ptr)
875633e85bSSergey Senozhatsky {
885633e85bSSergey Senozhatsky 	if (ptr < (void *)mod->arch.start_opd ||
895633e85bSSergey Senozhatsky 			ptr >= (void *)mod->arch.end_opd)
905633e85bSSergey Senozhatsky 		return ptr;
915633e85bSSergey Senozhatsky 
925633e85bSSergey Senozhatsky 	return dereference_function_descriptor(ptr);
935633e85bSSergey Senozhatsky }
94d2fae548SRusty Russell #endif
95d2fae548SRusty Russell 
96f17c4e01SMichael Ellerman #define STUB_MAGIC 0x73747562 /* stub */
97f17c4e01SMichael Ellerman 
987568cb4eSPaul Mackerras /* Like PPC32, we need little trampolines to do > 24-bit jumps (into
997568cb4eSPaul Mackerras    the kernel itself).  But on PPC64, these need to be used for every
1007568cb4eSPaul Mackerras    jump, actually, to reset r2 (TOC+0x8000). */
1017568cb4eSPaul Mackerras struct ppc64_stub_entry
1027568cb4eSPaul Mackerras {
10383775b85SAnton Blanchard 	/* 28 byte jump instruction sequence (7 instructions). We only
10483775b85SAnton Blanchard 	 * need 6 instructions on ABIv2 but we always allocate 7 so
10583775b85SAnton Blanchard 	 * so we don't have to modify the trampoline load instruction. */
1060e60e46eSRusty Russell 	u32 jump[7];
107f17c4e01SMichael Ellerman 	/* Used by ftrace to identify stubs */
108f17c4e01SMichael Ellerman 	u32 magic;
1097568cb4eSPaul Mackerras 	/* Data for the above code */
110008d7a91SRusty Russell 	func_desc_t funcdata;
1117568cb4eSPaul Mackerras };
1127568cb4eSPaul Mackerras 
1135c729a11SRusty Russell /*
1145c729a11SRusty Russell  * PPC64 uses 24 bit jumps, but we need to jump into other modules or
1155c729a11SRusty Russell  * the kernel which may be further.  So we jump to a stub.
1165c729a11SRusty Russell  *
1175c729a11SRusty Russell  * For ELFv1 we need to use this to set up the new r2 value (aka TOC
1185c729a11SRusty Russell  * pointer).  For ELFv2 it's the callee's responsibility to set up the
1195c729a11SRusty Russell  * new r2, but for both we need to save the old r2.
1205c729a11SRusty Russell  *
1215c729a11SRusty Russell  * We could simply patch the new r2 value and function pointer into
1225c729a11SRusty Russell  * the stub, but it's significantly shorter to put these values at the
1235c729a11SRusty Russell  * end of the stub code, and patch the stub address (32-bits relative
1245c729a11SRusty Russell  * to the TOC ptr, r2) into the stub.
125a2b6f26cSChristophe Leroy  *
126a2b6f26cSChristophe Leroy  * addis   r11,r2, <high>
127a2b6f26cSChristophe Leroy  * addi    r11,r11, <low>
128a2b6f26cSChristophe Leroy  * std     r2,R2_STACK_OFFSET(r1)
129a2b6f26cSChristophe Leroy  * ld      r12,32(r11)
130a2b6f26cSChristophe Leroy  * ld      r2,40(r11)
131a2b6f26cSChristophe Leroy  * mtctr   r12
132a2b6f26cSChristophe Leroy  * bctr
1335c729a11SRusty Russell  */
13483775b85SAnton Blanchard static u32 ppc64_stub_insns[] = {
135a2b6f26cSChristophe Leroy 	PPC_INST_ADDIS | __PPC_RT(R11) | __PPC_RA(R2),
136a2b6f26cSChristophe Leroy 	PPC_INST_ADDI | __PPC_RT(R11) | __PPC_RA(R11),
137fed8393eSEugene Surovegin 	/* Save current r2 value in magic place on the stack. */
138a2b6f26cSChristophe Leroy 	PPC_INST_STD | __PPC_RS(R2) | __PPC_RA(R1) | R2_STACK_OFFSET,
139a2b6f26cSChristophe Leroy 	PPC_INST_LD | __PPC_RT(R12) | __PPC_RA(R11) | 32,
140f55d9665SMichael Ellerman #ifdef PPC64_ELF_ABI_v1
1415c729a11SRusty Russell 	/* Set up new r2 from function descriptor */
142a2b6f26cSChristophe Leroy 	PPC_INST_LD | __PPC_RT(R2) | __PPC_RA(R11) | 40,
1435c729a11SRusty Russell #endif
144a2b6f26cSChristophe Leroy 	PPC_INST_MTCTR | __PPC_RS(R12),
145a2b6f26cSChristophe Leroy 	PPC_INST_BCTR,
14683775b85SAnton Blanchard };
14783775b85SAnton Blanchard 
1487568cb4eSPaul Mackerras /* Count how many different 24-bit relocations (different symbol,
1497568cb4eSPaul Mackerras    different addend) */
1507568cb4eSPaul Mackerras static unsigned int count_relocs(const Elf64_Rela *rela, unsigned int num)
1517568cb4eSPaul Mackerras {
152eda09fbdSEmil Medve 	unsigned int i, r_info, r_addend, _count_relocs;
1537568cb4eSPaul Mackerras 
1547568cb4eSPaul Mackerras 	/* FIXME: Only count external ones --RR */
155eda09fbdSEmil Medve 	_count_relocs = 0;
156eda09fbdSEmil Medve 	r_info = 0;
157eda09fbdSEmil Medve 	r_addend = 0;
158eda09fbdSEmil Medve 	for (i = 0; i < num; i++)
1597568cb4eSPaul Mackerras 		/* Only count 24-bit relocs, others don't need stubs */
160eda09fbdSEmil Medve 		if (ELF64_R_TYPE(rela[i].r_info) == R_PPC_REL24 &&
161eda09fbdSEmil Medve 		    (r_info != ELF64_R_SYM(rela[i].r_info) ||
162eda09fbdSEmil Medve 		     r_addend != rela[i].r_addend)) {
163eda09fbdSEmil Medve 			_count_relocs++;
164eda09fbdSEmil Medve 			r_info = ELF64_R_SYM(rela[i].r_info);
165eda09fbdSEmil Medve 			r_addend = rela[i].r_addend;
1667568cb4eSPaul Mackerras 		}
167eda09fbdSEmil Medve 
168eda09fbdSEmil Medve 	return _count_relocs;
1697568cb4eSPaul Mackerras }
1707568cb4eSPaul Mackerras 
171eda09fbdSEmil Medve static int relacmp(const void *_x, const void *_y)
172eda09fbdSEmil Medve {
173eda09fbdSEmil Medve 	const Elf64_Rela *x, *y;
174eda09fbdSEmil Medve 
175eda09fbdSEmil Medve 	y = (Elf64_Rela *)_x;
176eda09fbdSEmil Medve 	x = (Elf64_Rela *)_y;
177eda09fbdSEmil Medve 
178eda09fbdSEmil Medve 	/* Compare the entire r_info (as opposed to ELF64_R_SYM(r_info) only) to
179eda09fbdSEmil Medve 	 * make the comparison cheaper/faster. It won't affect the sorting or
180eda09fbdSEmil Medve 	 * the counting algorithms' performance
181eda09fbdSEmil Medve 	 */
182eda09fbdSEmil Medve 	if (x->r_info < y->r_info)
183eda09fbdSEmil Medve 		return -1;
184eda09fbdSEmil Medve 	else if (x->r_info > y->r_info)
185eda09fbdSEmil Medve 		return 1;
186eda09fbdSEmil Medve 	else if (x->r_addend < y->r_addend)
187eda09fbdSEmil Medve 		return -1;
188eda09fbdSEmil Medve 	else if (x->r_addend > y->r_addend)
189eda09fbdSEmil Medve 		return 1;
190eda09fbdSEmil Medve 	else
191eda09fbdSEmil Medve 		return 0;
192eda09fbdSEmil Medve }
193eda09fbdSEmil Medve 
1947568cb4eSPaul Mackerras /* Get size of potential trampolines required. */
1957568cb4eSPaul Mackerras static unsigned long get_stubs_size(const Elf64_Ehdr *hdr,
1967568cb4eSPaul Mackerras 				    const Elf64_Shdr *sechdrs)
1977568cb4eSPaul Mackerras {
1987568cb4eSPaul Mackerras 	/* One extra reloc so it's always 0-funcaddr terminated */
1997568cb4eSPaul Mackerras 	unsigned long relocs = 1;
2007568cb4eSPaul Mackerras 	unsigned i;
2017568cb4eSPaul Mackerras 
2027568cb4eSPaul Mackerras 	/* Every relocated section... */
2037568cb4eSPaul Mackerras 	for (i = 1; i < hdr->e_shnum; i++) {
2047568cb4eSPaul Mackerras 		if (sechdrs[i].sh_type == SHT_RELA) {
205c7d1f6afSAnton Blanchard 			pr_debug("Found relocations in section %u\n", i);
206c7d1f6afSAnton Blanchard 			pr_debug("Ptr: %p.  Number: %Lu\n",
2077568cb4eSPaul Mackerras 			       (void *)sechdrs[i].sh_addr,
2087568cb4eSPaul Mackerras 			       sechdrs[i].sh_size / sizeof(Elf64_Rela));
209eda09fbdSEmil Medve 
210eda09fbdSEmil Medve 			/* Sort the relocation information based on a symbol and
211eda09fbdSEmil Medve 			 * addend key. This is a stable O(n*log n) complexity
212eda09fbdSEmil Medve 			 * alogrithm but it will reduce the complexity of
213eda09fbdSEmil Medve 			 * count_relocs() to linear complexity O(n)
214eda09fbdSEmil Medve 			 */
215eda09fbdSEmil Medve 			sort((void *)sechdrs[i].sh_addr,
216eda09fbdSEmil Medve 			     sechdrs[i].sh_size / sizeof(Elf64_Rela),
217bac7ca7bSAndrey Abramov 			     sizeof(Elf64_Rela), relacmp, NULL);
218eda09fbdSEmil Medve 
2197568cb4eSPaul Mackerras 			relocs += count_relocs((void *)sechdrs[i].sh_addr,
2207568cb4eSPaul Mackerras 					       sechdrs[i].sh_size
2217568cb4eSPaul Mackerras 					       / sizeof(Elf64_Rela));
2227568cb4eSPaul Mackerras 		}
2237568cb4eSPaul Mackerras 	}
2247568cb4eSPaul Mackerras 
225f48cb8b4SSteven Rostedt #ifdef CONFIG_DYNAMIC_FTRACE
226f48cb8b4SSteven Rostedt 	/* make the trampoline to the ftrace_caller */
227f48cb8b4SSteven Rostedt 	relocs++;
228ae30cc05SNaveen N. Rao #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
229ae30cc05SNaveen N. Rao 	/* an additional one for ftrace_regs_caller */
230ae30cc05SNaveen N. Rao 	relocs++;
231ae30cc05SNaveen N. Rao #endif
232f48cb8b4SSteven Rostedt #endif
233f48cb8b4SSteven Rostedt 
234c7d1f6afSAnton Blanchard 	pr_debug("Looks like a total of %lu stubs, max\n", relocs);
2357568cb4eSPaul Mackerras 	return relocs * sizeof(struct ppc64_stub_entry);
2367568cb4eSPaul Mackerras }
2377568cb4eSPaul Mackerras 
2385b12c5c6SRusty Russell /* Still needed for ELFv2, for .TOC. */
2397568cb4eSPaul Mackerras static void dedotify_versions(struct modversion_info *vers,
2407568cb4eSPaul Mackerras 			      unsigned long size)
2417568cb4eSPaul Mackerras {
2427568cb4eSPaul Mackerras 	struct modversion_info *end;
2437568cb4eSPaul Mackerras 
2447568cb4eSPaul Mackerras 	for (end = (void *)vers + size; vers < end; vers++)
245c2cbcf53SLaurent Dufour 		if (vers->name[0] == '.') {
2467568cb4eSPaul Mackerras 			memmove(vers->name, vers->name+1, strlen(vers->name));
247c2cbcf53SLaurent Dufour 		}
2487568cb4eSPaul Mackerras }
2497568cb4eSPaul Mackerras 
250c153693dSAlan Modra /*
251c153693dSAlan Modra  * Undefined symbols which refer to .funcname, hack to funcname. Make .TOC.
252c153693dSAlan Modra  * seem to be defined (value set later).
253c153693dSAlan Modra  */
2547568cb4eSPaul Mackerras static void dedotify(Elf64_Sym *syms, unsigned int numsyms, char *strtab)
2557568cb4eSPaul Mackerras {
2567568cb4eSPaul Mackerras 	unsigned int i;
2577568cb4eSPaul Mackerras 
2587568cb4eSPaul Mackerras 	for (i = 1; i < numsyms; i++) {
2597568cb4eSPaul Mackerras 		if (syms[i].st_shndx == SHN_UNDEF) {
2607568cb4eSPaul Mackerras 			char *name = strtab + syms[i].st_name;
261c153693dSAlan Modra 			if (name[0] == '.') {
262c153693dSAlan Modra 				if (strcmp(name+1, "TOC.") == 0)
263c153693dSAlan Modra 					syms[i].st_shndx = SHN_ABS;
264f15838e9SAndreas Schwab 				syms[i].st_name++;
2657568cb4eSPaul Mackerras 			}
2667568cb4eSPaul Mackerras 		}
2677568cb4eSPaul Mackerras 	}
268c153693dSAlan Modra }
2697568cb4eSPaul Mackerras 
2704edebbeaSRusty Russell static Elf64_Sym *find_dot_toc(Elf64_Shdr *sechdrs,
2714edebbeaSRusty Russell 			       const char *strtab,
2724edebbeaSRusty Russell 			       unsigned int symindex)
2734edebbeaSRusty Russell {
2744edebbeaSRusty Russell 	unsigned int i, numsyms;
2754edebbeaSRusty Russell 	Elf64_Sym *syms;
2764edebbeaSRusty Russell 
2774edebbeaSRusty Russell 	syms = (Elf64_Sym *)sechdrs[symindex].sh_addr;
2784edebbeaSRusty Russell 	numsyms = sechdrs[symindex].sh_size / sizeof(Elf64_Sym);
2794edebbeaSRusty Russell 
2804edebbeaSRusty Russell 	for (i = 1; i < numsyms; i++) {
281c153693dSAlan Modra 		if (syms[i].st_shndx == SHN_ABS
282008d7a91SRusty Russell 		    && strcmp(strtab + syms[i].st_name, "TOC.") == 0)
2834edebbeaSRusty Russell 			return &syms[i];
2844edebbeaSRusty Russell 	}
2854edebbeaSRusty Russell 	return NULL;
2864edebbeaSRusty Russell }
2874edebbeaSRusty Russell 
2887568cb4eSPaul Mackerras int module_frob_arch_sections(Elf64_Ehdr *hdr,
2897568cb4eSPaul Mackerras 			      Elf64_Shdr *sechdrs,
2907568cb4eSPaul Mackerras 			      char *secstrings,
2917568cb4eSPaul Mackerras 			      struct module *me)
2927568cb4eSPaul Mackerras {
2937568cb4eSPaul Mackerras 	unsigned int i;
2947568cb4eSPaul Mackerras 
2957568cb4eSPaul Mackerras 	/* Find .toc and .stubs sections, symtab and strtab */
2967568cb4eSPaul Mackerras 	for (i = 1; i < hdr->e_shnum; i++) {
2977568cb4eSPaul Mackerras 		char *p;
2987568cb4eSPaul Mackerras 		if (strcmp(secstrings + sechdrs[i].sh_name, ".stubs") == 0)
2997568cb4eSPaul Mackerras 			me->arch.stubs_section = i;
3005c45b528SAlan Modra 		else if (strcmp(secstrings + sechdrs[i].sh_name, ".toc") == 0) {
3017568cb4eSPaul Mackerras 			me->arch.toc_section = i;
3025c45b528SAlan Modra 			if (sechdrs[i].sh_addralign < 8)
3035c45b528SAlan Modra 				sechdrs[i].sh_addralign = 8;
3045c45b528SAlan Modra 		}
3057568cb4eSPaul Mackerras 		else if (strcmp(secstrings+sechdrs[i].sh_name,"__versions")==0)
3067568cb4eSPaul Mackerras 			dedotify_versions((void *)hdr + sechdrs[i].sh_offset,
3077568cb4eSPaul Mackerras 					  sechdrs[i].sh_size);
3087568cb4eSPaul Mackerras 
3097568cb4eSPaul Mackerras 		/* We don't handle .init for the moment: rename to _init */
3107568cb4eSPaul Mackerras 		while ((p = strstr(secstrings + sechdrs[i].sh_name, ".init")))
3117568cb4eSPaul Mackerras 			p[0] = '_';
3127568cb4eSPaul Mackerras 
3137568cb4eSPaul Mackerras 		if (sechdrs[i].sh_type == SHT_SYMTAB)
3147568cb4eSPaul Mackerras 			dedotify((void *)hdr + sechdrs[i].sh_offset,
3157568cb4eSPaul Mackerras 				 sechdrs[i].sh_size / sizeof(Elf64_Sym),
3167568cb4eSPaul Mackerras 				 (void *)hdr
3177568cb4eSPaul Mackerras 				 + sechdrs[sechdrs[i].sh_link].sh_offset);
3187568cb4eSPaul Mackerras 	}
319f749edaeSAlan Modra 
320f749edaeSAlan Modra 	if (!me->arch.stubs_section) {
321c7d1f6afSAnton Blanchard 		pr_err("%s: doesn't contain .stubs.\n", me->name);
3227568cb4eSPaul Mackerras 		return -ENOEXEC;
3237568cb4eSPaul Mackerras 	}
3247568cb4eSPaul Mackerras 
325f749edaeSAlan Modra 	/* If we don't have a .toc, just use .stubs.  We need to set r2
326f749edaeSAlan Modra 	   to some reasonable value in case the module calls out to
327f749edaeSAlan Modra 	   other functions via a stub, or if a function pointer escapes
328f749edaeSAlan Modra 	   the module by some means.  */
329f749edaeSAlan Modra 	if (!me->arch.toc_section)
330f749edaeSAlan Modra 		me->arch.toc_section = me->arch.stubs_section;
331f749edaeSAlan Modra 
3327568cb4eSPaul Mackerras 	/* Override the stubs size */
3337568cb4eSPaul Mackerras 	sechdrs[me->arch.stubs_section].sh_size = get_stubs_size(hdr, sechdrs);
3347568cb4eSPaul Mackerras 	return 0;
3357568cb4eSPaul Mackerras }
3367568cb4eSPaul Mackerras 
3375c45b528SAlan Modra /*
3385c45b528SAlan Modra  * r2 is the TOC pointer: it actually points 0x8000 into the TOC (this gives the
3395c45b528SAlan Modra  * value maximum span in an instruction which uses a signed offset). Round down
3405c45b528SAlan Modra  * to a 256 byte boundary for the odd case where we are setting up r2 without a
3415c45b528SAlan Modra  * .toc section.
3425c45b528SAlan Modra  */
343136cd345SMichael Ellerman static inline unsigned long my_r2(const Elf64_Shdr *sechdrs, struct module *me)
3447568cb4eSPaul Mackerras {
3455c45b528SAlan Modra 	return (sechdrs[me->arch.toc_section].sh_addr & ~0xfful) + 0x8000;
3467568cb4eSPaul Mackerras }
3477568cb4eSPaul Mackerras 
3487568cb4eSPaul Mackerras /* Patch stub to reference function and correct r2 value. */
349136cd345SMichael Ellerman static inline int create_stub(const Elf64_Shdr *sechdrs,
3507568cb4eSPaul Mackerras 			      struct ppc64_stub_entry *entry,
351008d7a91SRusty Russell 			      unsigned long addr,
3527568cb4eSPaul Mackerras 			      struct module *me)
3537568cb4eSPaul Mackerras {
3547568cb4eSPaul Mackerras 	long reladdr;
3557568cb4eSPaul Mackerras 
35683775b85SAnton Blanchard 	memcpy(entry->jump, ppc64_stub_insns, sizeof(ppc64_stub_insns));
3577568cb4eSPaul Mackerras 
3587568cb4eSPaul Mackerras 	/* Stub uses address relative to r2. */
3597568cb4eSPaul Mackerras 	reladdr = (unsigned long)entry - my_r2(sechdrs, me);
3607568cb4eSPaul Mackerras 	if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) {
361c7d1f6afSAnton Blanchard 		pr_err("%s: Address %p of stub out of range of %p.\n",
3627568cb4eSPaul Mackerras 		       me->name, (void *)reladdr, (void *)my_r2);
3637568cb4eSPaul Mackerras 		return 0;
3647568cb4eSPaul Mackerras 	}
365c7d1f6afSAnton Blanchard 	pr_debug("Stub %p get data from reladdr %li\n", entry, reladdr);
3667568cb4eSPaul Mackerras 
3670e60e46eSRusty Russell 	entry->jump[0] |= PPC_HA(reladdr);
3680e60e46eSRusty Russell 	entry->jump[1] |= PPC_LO(reladdr);
369008d7a91SRusty Russell 	entry->funcdata = func_desc(addr);
370f17c4e01SMichael Ellerman 	entry->magic = STUB_MAGIC;
371f17c4e01SMichael Ellerman 
3727568cb4eSPaul Mackerras 	return 1;
3737568cb4eSPaul Mackerras }
3747568cb4eSPaul Mackerras 
375008d7a91SRusty Russell /* Create stub to jump to function described in this OPD/ptr: we need the
3767568cb4eSPaul Mackerras    stub to set up the TOC ptr (r2) for the function. */
377136cd345SMichael Ellerman static unsigned long stub_for_addr(const Elf64_Shdr *sechdrs,
378008d7a91SRusty Russell 				   unsigned long addr,
3797568cb4eSPaul Mackerras 				   struct module *me)
3807568cb4eSPaul Mackerras {
3817568cb4eSPaul Mackerras 	struct ppc64_stub_entry *stubs;
3827568cb4eSPaul Mackerras 	unsigned int i, num_stubs;
3837568cb4eSPaul Mackerras 
3847568cb4eSPaul Mackerras 	num_stubs = sechdrs[me->arch.stubs_section].sh_size / sizeof(*stubs);
3857568cb4eSPaul Mackerras 
3867568cb4eSPaul Mackerras 	/* Find this stub, or if that fails, the next avail. entry */
3877568cb4eSPaul Mackerras 	stubs = (void *)sechdrs[me->arch.stubs_section].sh_addr;
388008d7a91SRusty Russell 	for (i = 0; stub_func_addr(stubs[i].funcdata); i++) {
3891c0437afSKamalesh Babulal 		if (WARN_ON(i >= num_stubs))
3901c0437afSKamalesh Babulal 			return 0;
3917568cb4eSPaul Mackerras 
392008d7a91SRusty Russell 		if (stub_func_addr(stubs[i].funcdata) == func_addr(addr))
3937568cb4eSPaul Mackerras 			return (unsigned long)&stubs[i];
3947568cb4eSPaul Mackerras 	}
3957568cb4eSPaul Mackerras 
396008d7a91SRusty Russell 	if (!create_stub(sechdrs, &stubs[i], addr, me))
3977568cb4eSPaul Mackerras 		return 0;
3987568cb4eSPaul Mackerras 
3997568cb4eSPaul Mackerras 	return (unsigned long)&stubs[i];
4007568cb4eSPaul Mackerras }
4017568cb4eSPaul Mackerras 
402abba7597SNicholas Piggin #ifdef CONFIG_MPROFILE_KERNEL
403*1f2aaed2SNaveen N. Rao static bool is_mprofile_ftrace_call(const char *name)
40415308664STorsten Duwe {
405*1f2aaed2SNaveen N. Rao 	if (!strcmp("_mcount", name))
40615308664STorsten Duwe 		return true;
40715308664STorsten Duwe 
40815308664STorsten Duwe 	return false;
40915308664STorsten Duwe }
41015308664STorsten Duwe 
41115308664STorsten Duwe /*
41215308664STorsten Duwe  * In case of _mcount calls, do not save the current callee's TOC (in r2) into
41315308664STorsten Duwe  * the original caller's stack frame. If we did we would clobber the saved TOC
41415308664STorsten Duwe  * value of the original caller.
41515308664STorsten Duwe  */
41615308664STorsten Duwe static void squash_toc_save_inst(const char *name, unsigned long addr)
41715308664STorsten Duwe {
41815308664STorsten Duwe 	struct ppc64_stub_entry *stub = (struct ppc64_stub_entry *)addr;
41915308664STorsten Duwe 
42015308664STorsten Duwe 	/* Only for calls to _mcount */
42115308664STorsten Duwe 	if (strcmp("_mcount", name) != 0)
42215308664STorsten Duwe 		return;
42315308664STorsten Duwe 
42415308664STorsten Duwe 	stub->jump[2] = PPC_INST_NOP;
42515308664STorsten Duwe }
42615308664STorsten Duwe #else
42715308664STorsten Duwe static void squash_toc_save_inst(const char *name, unsigned long addr) { }
42815308664STorsten Duwe 
429*1f2aaed2SNaveen N. Rao static bool is_mprofile_ftrace_call(const char *name)
43015308664STorsten Duwe {
43115308664STorsten Duwe 	return false;
43215308664STorsten Duwe }
43315308664STorsten Duwe #endif
43415308664STorsten Duwe 
4357568cb4eSPaul Mackerras /* We expect a noop next: if it is, replace it with instruction to
4367568cb4eSPaul Mackerras    restore r2. */
437250122baSNaveen N. Rao static int restore_r2(const char *name, u32 *instruction, struct module *me)
4387568cb4eSPaul Mackerras {
439b9eab08dSJosh Poimboeuf 	u32 *prev_insn = instruction - 1;
440b9eab08dSJosh Poimboeuf 
441*1f2aaed2SNaveen N. Rao 	if (is_mprofile_ftrace_call(name))
442b9eab08dSJosh Poimboeuf 		return 1;
443b9eab08dSJosh Poimboeuf 
444b9eab08dSJosh Poimboeuf 	/*
445b9eab08dSJosh Poimboeuf 	 * Make sure the branch isn't a sibling call.  Sibling calls aren't
446b9eab08dSJosh Poimboeuf 	 * "link" branches and they don't return, so they don't need the r2
447b9eab08dSJosh Poimboeuf 	 * restore afterwards.
448b9eab08dSJosh Poimboeuf 	 */
44975346251SJordan Niethe 	if (!instr_is_relative_link_branch(ppc_inst(*prev_insn)))
45015308664STorsten Duwe 		return 1;
45131278b17SMichael Ellerman 
45231278b17SMichael Ellerman 	if (*instruction != PPC_INST_NOP) {
4531ea61ea2SJosh Poimboeuf 		pr_err("%s: Expected nop after call, got %08x at %pS\n",
4541ea61ea2SJosh Poimboeuf 			me->name, *instruction, instruction);
4557568cb4eSPaul Mackerras 		return 0;
4567568cb4eSPaul Mackerras 	}
457d2fae548SRusty Russell 	/* ld r2,R2_STACK_OFFSET(r1) */
45815308664STorsten Duwe 	*instruction = PPC_INST_LD_TOC;
4597568cb4eSPaul Mackerras 	return 1;
4607568cb4eSPaul Mackerras }
4617568cb4eSPaul Mackerras 
4627568cb4eSPaul Mackerras int apply_relocate_add(Elf64_Shdr *sechdrs,
4637568cb4eSPaul Mackerras 		       const char *strtab,
4647568cb4eSPaul Mackerras 		       unsigned int symindex,
4657568cb4eSPaul Mackerras 		       unsigned int relsec,
4667568cb4eSPaul Mackerras 		       struct module *me)
4677568cb4eSPaul Mackerras {
4687568cb4eSPaul Mackerras 	unsigned int i;
4697568cb4eSPaul Mackerras 	Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr;
4707568cb4eSPaul Mackerras 	Elf64_Sym *sym;
4717568cb4eSPaul Mackerras 	unsigned long *location;
4727568cb4eSPaul Mackerras 	unsigned long value;
4737568cb4eSPaul Mackerras 
474c7d1f6afSAnton Blanchard 	pr_debug("Applying ADD relocate section %u to %u\n", relsec,
4757568cb4eSPaul Mackerras 	       sechdrs[relsec].sh_info);
4764edebbeaSRusty Russell 
4774edebbeaSRusty Russell 	/* First time we're called, we can fix up .TOC. */
4784edebbeaSRusty Russell 	if (!me->arch.toc_fixed) {
4794edebbeaSRusty Russell 		sym = find_dot_toc(sechdrs, strtab, symindex);
4804edebbeaSRusty Russell 		/* It's theoretically possible that a module doesn't want a
4814edebbeaSRusty Russell 		 * .TOC. so don't fail it just for that. */
4824edebbeaSRusty Russell 		if (sym)
4834edebbeaSRusty Russell 			sym->st_value = my_r2(sechdrs, me);
4844edebbeaSRusty Russell 		me->arch.toc_fixed = true;
4854edebbeaSRusty Russell 	}
4864edebbeaSRusty Russell 
4877568cb4eSPaul Mackerras 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
4887568cb4eSPaul Mackerras 		/* This is where to make the change */
4897568cb4eSPaul Mackerras 		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
4907568cb4eSPaul Mackerras 			+ rela[i].r_offset;
4917568cb4eSPaul Mackerras 		/* This is the symbol it is referring to */
4927568cb4eSPaul Mackerras 		sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
4937568cb4eSPaul Mackerras 			+ ELF64_R_SYM(rela[i].r_info);
4947568cb4eSPaul Mackerras 
495c7d1f6afSAnton Blanchard 		pr_debug("RELOC at %p: %li-type as %s (0x%lx) + %li\n",
4967568cb4eSPaul Mackerras 		       location, (long)ELF64_R_TYPE(rela[i].r_info),
4977568cb4eSPaul Mackerras 		       strtab + sym->st_name, (unsigned long)sym->st_value,
4987568cb4eSPaul Mackerras 		       (long)rela[i].r_addend);
4997568cb4eSPaul Mackerras 
5007568cb4eSPaul Mackerras 		/* `Everything is relative'. */
5017568cb4eSPaul Mackerras 		value = sym->st_value + rela[i].r_addend;
5027568cb4eSPaul Mackerras 
5037568cb4eSPaul Mackerras 		switch (ELF64_R_TYPE(rela[i].r_info)) {
5047568cb4eSPaul Mackerras 		case R_PPC64_ADDR32:
5057568cb4eSPaul Mackerras 			/* Simply set it */
5067568cb4eSPaul Mackerras 			*(u32 *)location = value;
5077568cb4eSPaul Mackerras 			break;
5087568cb4eSPaul Mackerras 
5097568cb4eSPaul Mackerras 		case R_PPC64_ADDR64:
5107568cb4eSPaul Mackerras 			/* Simply set it */
5117568cb4eSPaul Mackerras 			*(unsigned long *)location = value;
5127568cb4eSPaul Mackerras 			break;
5137568cb4eSPaul Mackerras 
5147568cb4eSPaul Mackerras 		case R_PPC64_TOC:
5157568cb4eSPaul Mackerras 			*(unsigned long *)location = my_r2(sechdrs, me);
5167568cb4eSPaul Mackerras 			break;
5177568cb4eSPaul Mackerras 
5187568cb4eSPaul Mackerras 		case R_PPC64_TOC16:
519f749edaeSAlan Modra 			/* Subtract TOC pointer */
5207568cb4eSPaul Mackerras 			value -= my_r2(sechdrs, me);
5217568cb4eSPaul Mackerras 			if (value + 0x8000 > 0xffff) {
522c7d1f6afSAnton Blanchard 				pr_err("%s: bad TOC16 relocation (0x%lx)\n",
5237568cb4eSPaul Mackerras 				       me->name, value);
5247568cb4eSPaul Mackerras 				return -ENOEXEC;
5257568cb4eSPaul Mackerras 			}
5267568cb4eSPaul Mackerras 			*((uint16_t *) location)
5277568cb4eSPaul Mackerras 				= (*((uint16_t *) location) & ~0xffff)
5287568cb4eSPaul Mackerras 				| (value & 0xffff);
5297568cb4eSPaul Mackerras 			break;
5307568cb4eSPaul Mackerras 
5311fbe9cf2SAnton Blanchard 		case R_PPC64_TOC16_LO:
5321fbe9cf2SAnton Blanchard 			/* Subtract TOC pointer */
5331fbe9cf2SAnton Blanchard 			value -= my_r2(sechdrs, me);
5341fbe9cf2SAnton Blanchard 			*((uint16_t *) location)
5351fbe9cf2SAnton Blanchard 				= (*((uint16_t *) location) & ~0xffff)
5361fbe9cf2SAnton Blanchard 				| (value & 0xffff);
5371fbe9cf2SAnton Blanchard 			break;
5381fbe9cf2SAnton Blanchard 
5397568cb4eSPaul Mackerras 		case R_PPC64_TOC16_DS:
540f749edaeSAlan Modra 			/* Subtract TOC pointer */
5417568cb4eSPaul Mackerras 			value -= my_r2(sechdrs, me);
5427568cb4eSPaul Mackerras 			if ((value & 3) != 0 || value + 0x8000 > 0xffff) {
543c7d1f6afSAnton Blanchard 				pr_err("%s: bad TOC16_DS relocation (0x%lx)\n",
5447568cb4eSPaul Mackerras 				       me->name, value);
5457568cb4eSPaul Mackerras 				return -ENOEXEC;
5467568cb4eSPaul Mackerras 			}
5477568cb4eSPaul Mackerras 			*((uint16_t *) location)
5487568cb4eSPaul Mackerras 				= (*((uint16_t *) location) & ~0xfffc)
5497568cb4eSPaul Mackerras 				| (value & 0xfffc);
5507568cb4eSPaul Mackerras 			break;
5517568cb4eSPaul Mackerras 
5521fbe9cf2SAnton Blanchard 		case R_PPC64_TOC16_LO_DS:
5531fbe9cf2SAnton Blanchard 			/* Subtract TOC pointer */
5541fbe9cf2SAnton Blanchard 			value -= my_r2(sechdrs, me);
5551fbe9cf2SAnton Blanchard 			if ((value & 3) != 0) {
556c7d1f6afSAnton Blanchard 				pr_err("%s: bad TOC16_LO_DS relocation (0x%lx)\n",
5571fbe9cf2SAnton Blanchard 				       me->name, value);
5581fbe9cf2SAnton Blanchard 				return -ENOEXEC;
5591fbe9cf2SAnton Blanchard 			}
5601fbe9cf2SAnton Blanchard 			*((uint16_t *) location)
5611fbe9cf2SAnton Blanchard 				= (*((uint16_t *) location) & ~0xfffc)
5621fbe9cf2SAnton Blanchard 				| (value & 0xfffc);
5631fbe9cf2SAnton Blanchard 			break;
5641fbe9cf2SAnton Blanchard 
5651fbe9cf2SAnton Blanchard 		case R_PPC64_TOC16_HA:
5661fbe9cf2SAnton Blanchard 			/* Subtract TOC pointer */
5671fbe9cf2SAnton Blanchard 			value -= my_r2(sechdrs, me);
5681fbe9cf2SAnton Blanchard 			value = ((value + 0x8000) >> 16);
5691fbe9cf2SAnton Blanchard 			*((uint16_t *) location)
5701fbe9cf2SAnton Blanchard 				= (*((uint16_t *) location) & ~0xffff)
5711fbe9cf2SAnton Blanchard 				| (value & 0xffff);
5721fbe9cf2SAnton Blanchard 			break;
5731fbe9cf2SAnton Blanchard 
5747568cb4eSPaul Mackerras 		case R_PPC_REL24:
5757568cb4eSPaul Mackerras 			/* FIXME: Handle weak symbols here --RR */
576a443bf6eSKamalesh Babulal 			if (sym->st_shndx == SHN_UNDEF ||
577a443bf6eSKamalesh Babulal 			    sym->st_shndx == SHN_LIVEPATCH) {
5787568cb4eSPaul Mackerras 				/* External: go via stub */
5797568cb4eSPaul Mackerras 				value = stub_for_addr(sechdrs, value, me);
5807568cb4eSPaul Mackerras 				if (!value)
5817568cb4eSPaul Mackerras 					return -ENOENT;
582250122baSNaveen N. Rao 				if (!restore_r2(strtab + sym->st_name,
583250122baSNaveen N. Rao 							(u32 *)location + 1, me))
5847568cb4eSPaul Mackerras 					return -ENOEXEC;
58515308664STorsten Duwe 
58615308664STorsten Duwe 				squash_toc_save_inst(strtab + sym->st_name, value);
587008d7a91SRusty Russell 			} else
588008d7a91SRusty Russell 				value += local_entry_offset(sym);
5897568cb4eSPaul Mackerras 
5907568cb4eSPaul Mackerras 			/* Convert value to relative */
5917568cb4eSPaul Mackerras 			value -= (unsigned long)location;
5927568cb4eSPaul Mackerras 			if (value + 0x2000000 > 0x3ffffff || (value & 3) != 0){
593c7d1f6afSAnton Blanchard 				pr_err("%s: REL24 %li out of range!\n",
5947568cb4eSPaul Mackerras 				       me->name, (long int)value);
5957568cb4eSPaul Mackerras 				return -ENOEXEC;
5967568cb4eSPaul Mackerras 			}
5977568cb4eSPaul Mackerras 
5987568cb4eSPaul Mackerras 			/* Only replace bits 2 through 26 */
5997568cb4eSPaul Mackerras 			*(uint32_t *)location
6007568cb4eSPaul Mackerras 				= (*(uint32_t *)location & ~0x03fffffc)
6017568cb4eSPaul Mackerras 				| (value & 0x03fffffc);
6027568cb4eSPaul Mackerras 			break;
6037568cb4eSPaul Mackerras 
60421c4ff80SBenjamin Herrenschmidt 		case R_PPC64_REL64:
60521c4ff80SBenjamin Herrenschmidt 			/* 64 bits relative (used by features fixups) */
60621c4ff80SBenjamin Herrenschmidt 			*location = value - (unsigned long)location;
60721c4ff80SBenjamin Herrenschmidt 			break;
60821c4ff80SBenjamin Herrenschmidt 
6099f751b82SMichael Ellerman 		case R_PPC64_REL32:
6109f751b82SMichael Ellerman 			/* 32 bits relative (used by relative exception tables) */
611b851ba02SNicholas Piggin 			/* Convert value to relative */
612b851ba02SNicholas Piggin 			value -= (unsigned long)location;
613b851ba02SNicholas Piggin 			if (value + 0x80000000 > 0xffffffff) {
614b851ba02SNicholas Piggin 				pr_err("%s: REL32 %li out of range!\n",
615b851ba02SNicholas Piggin 				       me->name, (long int)value);
616b851ba02SNicholas Piggin 				return -ENOEXEC;
617b851ba02SNicholas Piggin 			}
618b851ba02SNicholas Piggin 			*(u32 *)location = value;
6199f751b82SMichael Ellerman 			break;
6209f751b82SMichael Ellerman 
621d247da0aSRusty Russell 		case R_PPC64_TOCSAVE:
622d247da0aSRusty Russell 			/*
623d247da0aSRusty Russell 			 * Marker reloc indicates we don't have to save r2.
624d247da0aSRusty Russell 			 * That would only save us one instruction, so ignore
625d247da0aSRusty Russell 			 * it.
626d247da0aSRusty Russell 			 */
627d247da0aSRusty Russell 			break;
628d247da0aSRusty Russell 
629a61674bdSUlrich Weigand 		case R_PPC64_ENTRY:
630a61674bdSUlrich Weigand 			/*
631a61674bdSUlrich Weigand 			 * Optimize ELFv2 large code model entry point if
632a61674bdSUlrich Weigand 			 * the TOC is within 2GB range of current location.
633a61674bdSUlrich Weigand 			 */
634a61674bdSUlrich Weigand 			value = my_r2(sechdrs, me) - (unsigned long)location;
635a61674bdSUlrich Weigand 			if (value + 0x80008000 > 0xffffffff)
636a61674bdSUlrich Weigand 				break;
637a61674bdSUlrich Weigand 			/*
638a61674bdSUlrich Weigand 			 * Check for the large code model prolog sequence:
639a61674bdSUlrich Weigand 		         *	ld r2, ...(r12)
640a61674bdSUlrich Weigand 			 *	add r2, r2, r12
641a61674bdSUlrich Weigand 			 */
642a2b6f26cSChristophe Leroy 			if ((((uint32_t *)location)[0] & ~0xfffc) !=
643a2b6f26cSChristophe Leroy 			    (PPC_INST_LD | __PPC_RT(R2) | __PPC_RA(R12)))
644a61674bdSUlrich Weigand 				break;
645a2b6f26cSChristophe Leroy 			if (((uint32_t *)location)[1] !=
646a2b6f26cSChristophe Leroy 			    (PPC_INST_ADD | __PPC_RT(R2) | __PPC_RA(R2) | __PPC_RB(R12)))
647a61674bdSUlrich Weigand 				break;
648a61674bdSUlrich Weigand 			/*
649a61674bdSUlrich Weigand 			 * If found, replace it with:
650a61674bdSUlrich Weigand 			 *	addis r2, r12, (.TOC.-func)@ha
6512fb0a2c9SMichael Ellerman 			 *	addi  r2,  r2, (.TOC.-func)@l
652a61674bdSUlrich Weigand 			 */
653a2b6f26cSChristophe Leroy 			((uint32_t *)location)[0] = PPC_INST_ADDIS | __PPC_RT(R2) |
654a2b6f26cSChristophe Leroy 						    __PPC_RA(R12) | PPC_HA(value);
655a2b6f26cSChristophe Leroy 			((uint32_t *)location)[1] = PPC_INST_ADDI | __PPC_RT(R2) |
656a2b6f26cSChristophe Leroy 						    __PPC_RA(R2) | PPC_LO(value);
657a61674bdSUlrich Weigand 			break;
658a61674bdSUlrich Weigand 
6590906584aSRusty Russell 		case R_PPC64_REL16_HA:
6600906584aSRusty Russell 			/* Subtract location pointer */
6610906584aSRusty Russell 			value -= (unsigned long)location;
6620906584aSRusty Russell 			value = ((value + 0x8000) >> 16);
6630906584aSRusty Russell 			*((uint16_t *) location)
6640906584aSRusty Russell 				= (*((uint16_t *) location) & ~0xffff)
6650906584aSRusty Russell 				| (value & 0xffff);
6660906584aSRusty Russell 			break;
6670906584aSRusty Russell 
6680906584aSRusty Russell 		case R_PPC64_REL16_LO:
6690906584aSRusty Russell 			/* Subtract location pointer */
6700906584aSRusty Russell 			value -= (unsigned long)location;
6710906584aSRusty Russell 			*((uint16_t *) location)
6720906584aSRusty Russell 				= (*((uint16_t *) location) & ~0xffff)
6730906584aSRusty Russell 				| (value & 0xffff);
6740906584aSRusty Russell 			break;
6750906584aSRusty Russell 
6767568cb4eSPaul Mackerras 		default:
677c7d1f6afSAnton Blanchard 			pr_err("%s: Unknown ADD relocation: %lu\n",
6787568cb4eSPaul Mackerras 			       me->name,
6797568cb4eSPaul Mackerras 			       (unsigned long)ELF64_R_TYPE(rela[i].r_info));
6807568cb4eSPaul Mackerras 			return -ENOEXEC;
6817568cb4eSPaul Mackerras 		}
6827568cb4eSPaul Mackerras 	}
6837568cb4eSPaul Mackerras 
684136cd345SMichael Ellerman 	return 0;
685136cd345SMichael Ellerman }
686136cd345SMichael Ellerman 
687f48cb8b4SSteven Rostedt #ifdef CONFIG_DYNAMIC_FTRACE
68803b51416SNaveen N. Rao int module_trampoline_target(struct module *mod, unsigned long addr,
68903b51416SNaveen N. Rao 			     unsigned long *target)
69003b51416SNaveen N. Rao {
69103b51416SNaveen N. Rao 	struct ppc64_stub_entry *stub;
69203b51416SNaveen N. Rao 	func_desc_t funcdata;
69303b51416SNaveen N. Rao 	u32 magic;
69403b51416SNaveen N. Rao 
69503b51416SNaveen N. Rao 	if (!within_module_core(addr, mod)) {
69603b51416SNaveen N. Rao 		pr_err("%s: stub %lx not in module %s\n", __func__, addr, mod->name);
69703b51416SNaveen N. Rao 		return -EFAULT;
69803b51416SNaveen N. Rao 	}
69903b51416SNaveen N. Rao 
70003b51416SNaveen N. Rao 	stub = (struct ppc64_stub_entry *)addr;
70103b51416SNaveen N. Rao 
70203b51416SNaveen N. Rao 	if (probe_kernel_read(&magic, &stub->magic, sizeof(magic))) {
70303b51416SNaveen N. Rao 		pr_err("%s: fault reading magic for stub %lx for %s\n", __func__, addr, mod->name);
70403b51416SNaveen N. Rao 		return -EFAULT;
70503b51416SNaveen N. Rao 	}
70603b51416SNaveen N. Rao 
70703b51416SNaveen N. Rao 	if (magic != STUB_MAGIC) {
70803b51416SNaveen N. Rao 		pr_err("%s: bad magic for stub %lx for %s\n", __func__, addr, mod->name);
70903b51416SNaveen N. Rao 		return -EFAULT;
71003b51416SNaveen N. Rao 	}
71103b51416SNaveen N. Rao 
71203b51416SNaveen N. Rao 	if (probe_kernel_read(&funcdata, &stub->funcdata, sizeof(funcdata))) {
71303b51416SNaveen N. Rao 		pr_err("%s: fault reading funcdata for stub %lx for %s\n", __func__, addr, mod->name);
71403b51416SNaveen N. Rao                 return -EFAULT;
71503b51416SNaveen N. Rao 	}
71603b51416SNaveen N. Rao 
71703b51416SNaveen N. Rao 	*target = stub_func_addr(funcdata);
71803b51416SNaveen N. Rao 
71903b51416SNaveen N. Rao 	return 0;
72003b51416SNaveen N. Rao }
721336a7b5dSMichael Ellerman 
722abba7597SNicholas Piggin #ifdef CONFIG_MPROFILE_KERNEL
723336a7b5dSMichael Ellerman 
724336a7b5dSMichael Ellerman #define PACATOC offsetof(struct paca_struct, kernel_toc)
725336a7b5dSMichael Ellerman 
726336a7b5dSMichael Ellerman /*
727336a7b5dSMichael Ellerman  * For mprofile-kernel we use a special stub for ftrace_caller() because we
728336a7b5dSMichael Ellerman  * can't rely on r2 containing this module's TOC when we enter the stub.
729336a7b5dSMichael Ellerman  *
730336a7b5dSMichael Ellerman  * That can happen if the function calling us didn't need to use the toc. In
731336a7b5dSMichael Ellerman  * that case it won't have setup r2, and the r2 value will be either the
732336a7b5dSMichael Ellerman  * kernel's toc, or possibly another modules toc.
733336a7b5dSMichael Ellerman  *
734336a7b5dSMichael Ellerman  * To deal with that this stub uses the kernel toc, which is always accessible
735336a7b5dSMichael Ellerman  * via the paca (in r13). The target (ftrace_caller()) is responsible for
736336a7b5dSMichael Ellerman  * saving and restoring the toc before returning.
737336a7b5dSMichael Ellerman  */
738ae30cc05SNaveen N. Rao static unsigned long create_ftrace_stub(const Elf64_Shdr *sechdrs,
739ae30cc05SNaveen N. Rao 				struct module *me, unsigned long addr)
740336a7b5dSMichael Ellerman {
741336a7b5dSMichael Ellerman 	struct ppc64_stub_entry *entry;
742336a7b5dSMichael Ellerman 	unsigned int i, num_stubs;
743a2b6f26cSChristophe Leroy 	/*
744a2b6f26cSChristophe Leroy 	 * ld      r12,PACATOC(r13)
745a2b6f26cSChristophe Leroy 	 * addis   r12,r12,<high>
746a2b6f26cSChristophe Leroy 	 * addi    r12,r12,<low>
747a2b6f26cSChristophe Leroy 	 * mtctr   r12
748a2b6f26cSChristophe Leroy 	 * bctr
749a2b6f26cSChristophe Leroy 	 */
750336a7b5dSMichael Ellerman 	static u32 stub_insns[] = {
751a2b6f26cSChristophe Leroy 		PPC_INST_LD | __PPC_RT(R12) | __PPC_RA(R13) | PACATOC,
752a2b6f26cSChristophe Leroy 		PPC_INST_ADDIS | __PPC_RT(R12) | __PPC_RA(R12),
753a2b6f26cSChristophe Leroy 		PPC_INST_ADDI | __PPC_RT(R12) | __PPC_RA(R12),
754a2b6f26cSChristophe Leroy 		PPC_INST_MTCTR | __PPC_RS(R12),
755a2b6f26cSChristophe Leroy 		PPC_INST_BCTR,
756336a7b5dSMichael Ellerman 	};
757336a7b5dSMichael Ellerman 	long reladdr;
758336a7b5dSMichael Ellerman 
759336a7b5dSMichael Ellerman 	num_stubs = sechdrs[me->arch.stubs_section].sh_size / sizeof(*entry);
760336a7b5dSMichael Ellerman 
761336a7b5dSMichael Ellerman 	/* Find the next available stub entry */
762336a7b5dSMichael Ellerman 	entry = (void *)sechdrs[me->arch.stubs_section].sh_addr;
763336a7b5dSMichael Ellerman 	for (i = 0; i < num_stubs && stub_func_addr(entry->funcdata); i++, entry++);
764336a7b5dSMichael Ellerman 
765336a7b5dSMichael Ellerman 	if (i >= num_stubs) {
766336a7b5dSMichael Ellerman 		pr_err("%s: Unable to find a free slot for ftrace stub.\n", me->name);
767336a7b5dSMichael Ellerman 		return 0;
768336a7b5dSMichael Ellerman 	}
769336a7b5dSMichael Ellerman 
770336a7b5dSMichael Ellerman 	memcpy(entry->jump, stub_insns, sizeof(stub_insns));
771336a7b5dSMichael Ellerman 
772336a7b5dSMichael Ellerman 	/* Stub uses address relative to kernel toc (from the paca) */
773ae30cc05SNaveen N. Rao 	reladdr = addr - kernel_toc_addr();
774336a7b5dSMichael Ellerman 	if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) {
775ae30cc05SNaveen N. Rao 		pr_err("%s: Address of %ps out of range of kernel_toc.\n",
776ae30cc05SNaveen N. Rao 							me->name, (void *)addr);
777336a7b5dSMichael Ellerman 		return 0;
778336a7b5dSMichael Ellerman 	}
779336a7b5dSMichael Ellerman 
780336a7b5dSMichael Ellerman 	entry->jump[1] |= PPC_HA(reladdr);
781336a7b5dSMichael Ellerman 	entry->jump[2] |= PPC_LO(reladdr);
782336a7b5dSMichael Ellerman 
783336a7b5dSMichael Ellerman 	/* Eventhough we don't use funcdata in the stub, it's needed elsewhere. */
784ae30cc05SNaveen N. Rao 	entry->funcdata = func_desc(addr);
785336a7b5dSMichael Ellerman 	entry->magic = STUB_MAGIC;
786336a7b5dSMichael Ellerman 
787336a7b5dSMichael Ellerman 	return (unsigned long)entry;
788336a7b5dSMichael Ellerman }
789336a7b5dSMichael Ellerman #else
790ae30cc05SNaveen N. Rao static unsigned long create_ftrace_stub(const Elf64_Shdr *sechdrs,
791ae30cc05SNaveen N. Rao 				struct module *me, unsigned long addr)
792336a7b5dSMichael Ellerman {
793ae30cc05SNaveen N. Rao 	return stub_for_addr(sechdrs, addr, me);
794336a7b5dSMichael Ellerman }
795f48cb8b4SSteven Rostedt #endif
796f48cb8b4SSteven Rostedt 
797136cd345SMichael Ellerman int module_finalize_ftrace(struct module *mod, const Elf_Shdr *sechdrs)
798136cd345SMichael Ellerman {
799ae30cc05SNaveen N. Rao 	mod->arch.tramp = create_ftrace_stub(sechdrs, mod,
800ae30cc05SNaveen N. Rao 					(unsigned long)ftrace_caller);
801ae30cc05SNaveen N. Rao #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
802ae30cc05SNaveen N. Rao 	mod->arch.tramp_regs = create_ftrace_stub(sechdrs, mod,
803ae30cc05SNaveen N. Rao 					(unsigned long)ftrace_regs_caller);
804ae30cc05SNaveen N. Rao 	if (!mod->arch.tramp_regs)
805ae30cc05SNaveen N. Rao 		return -ENOENT;
806ae30cc05SNaveen N. Rao #endif
807136cd345SMichael Ellerman 
808136cd345SMichael Ellerman 	if (!mod->arch.tramp)
809136cd345SMichael Ellerman 		return -ENOENT;
8107568cb4eSPaul Mackerras 
8117568cb4eSPaul Mackerras 	return 0;
8127568cb4eSPaul Mackerras }
813136cd345SMichael Ellerman #endif
814