xref: /linux-6.15/arch/powerpc/kernel/module_64.c (revision 03b51416)
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
403250122baSNaveen N. Rao static bool is_mprofile_mcount_callsite(const char *name, u32 *instruction)
40415308664STorsten Duwe {
405250122baSNaveen N. Rao 	if (strcmp("_mcount", name))
406250122baSNaveen N. Rao 		return false;
407250122baSNaveen N. Rao 
40815308664STorsten Duwe 	/*
40915308664STorsten Duwe 	 * Check if this is one of the -mprofile-kernel sequences.
41015308664STorsten Duwe 	 */
41115308664STorsten Duwe 	if (instruction[-1] == PPC_INST_STD_LR &&
41215308664STorsten Duwe 	    instruction[-2] == PPC_INST_MFLR)
41315308664STorsten Duwe 		return true;
41415308664STorsten Duwe 
41515308664STorsten Duwe 	if (instruction[-1] == PPC_INST_MFLR)
41615308664STorsten Duwe 		return true;
41715308664STorsten Duwe 
41815308664STorsten Duwe 	return false;
41915308664STorsten Duwe }
42015308664STorsten Duwe 
42115308664STorsten Duwe /*
42215308664STorsten Duwe  * In case of _mcount calls, do not save the current callee's TOC (in r2) into
42315308664STorsten Duwe  * the original caller's stack frame. If we did we would clobber the saved TOC
42415308664STorsten Duwe  * value of the original caller.
42515308664STorsten Duwe  */
42615308664STorsten Duwe static void squash_toc_save_inst(const char *name, unsigned long addr)
42715308664STorsten Duwe {
42815308664STorsten Duwe 	struct ppc64_stub_entry *stub = (struct ppc64_stub_entry *)addr;
42915308664STorsten Duwe 
43015308664STorsten Duwe 	/* Only for calls to _mcount */
43115308664STorsten Duwe 	if (strcmp("_mcount", name) != 0)
43215308664STorsten Duwe 		return;
43315308664STorsten Duwe 
43415308664STorsten Duwe 	stub->jump[2] = PPC_INST_NOP;
43515308664STorsten Duwe }
43615308664STorsten Duwe #else
43715308664STorsten Duwe static void squash_toc_save_inst(const char *name, unsigned long addr) { }
43815308664STorsten Duwe 
439250122baSNaveen N. Rao static bool is_mprofile_mcount_callsite(const char *name, u32 *instruction)
44015308664STorsten Duwe {
44115308664STorsten Duwe 	return false;
44215308664STorsten Duwe }
44315308664STorsten Duwe #endif
44415308664STorsten Duwe 
4457568cb4eSPaul Mackerras /* We expect a noop next: if it is, replace it with instruction to
4467568cb4eSPaul Mackerras    restore r2. */
447250122baSNaveen N. Rao static int restore_r2(const char *name, u32 *instruction, struct module *me)
4487568cb4eSPaul Mackerras {
449b9eab08dSJosh Poimboeuf 	u32 *prev_insn = instruction - 1;
450b9eab08dSJosh Poimboeuf 
451250122baSNaveen N. Rao 	if (is_mprofile_mcount_callsite(name, prev_insn))
452b9eab08dSJosh Poimboeuf 		return 1;
453b9eab08dSJosh Poimboeuf 
454b9eab08dSJosh Poimboeuf 	/*
455b9eab08dSJosh Poimboeuf 	 * Make sure the branch isn't a sibling call.  Sibling calls aren't
456b9eab08dSJosh Poimboeuf 	 * "link" branches and they don't return, so they don't need the r2
457b9eab08dSJosh Poimboeuf 	 * restore afterwards.
458b9eab08dSJosh Poimboeuf 	 */
45975346251SJordan Niethe 	if (!instr_is_relative_link_branch(ppc_inst(*prev_insn)))
46015308664STorsten Duwe 		return 1;
46131278b17SMichael Ellerman 
46231278b17SMichael Ellerman 	if (*instruction != PPC_INST_NOP) {
4631ea61ea2SJosh Poimboeuf 		pr_err("%s: Expected nop after call, got %08x at %pS\n",
4641ea61ea2SJosh Poimboeuf 			me->name, *instruction, instruction);
4657568cb4eSPaul Mackerras 		return 0;
4667568cb4eSPaul Mackerras 	}
467d2fae548SRusty Russell 	/* ld r2,R2_STACK_OFFSET(r1) */
46815308664STorsten Duwe 	*instruction = PPC_INST_LD_TOC;
4697568cb4eSPaul Mackerras 	return 1;
4707568cb4eSPaul Mackerras }
4717568cb4eSPaul Mackerras 
4727568cb4eSPaul Mackerras int apply_relocate_add(Elf64_Shdr *sechdrs,
4737568cb4eSPaul Mackerras 		       const char *strtab,
4747568cb4eSPaul Mackerras 		       unsigned int symindex,
4757568cb4eSPaul Mackerras 		       unsigned int relsec,
4767568cb4eSPaul Mackerras 		       struct module *me)
4777568cb4eSPaul Mackerras {
4787568cb4eSPaul Mackerras 	unsigned int i;
4797568cb4eSPaul Mackerras 	Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr;
4807568cb4eSPaul Mackerras 	Elf64_Sym *sym;
4817568cb4eSPaul Mackerras 	unsigned long *location;
4827568cb4eSPaul Mackerras 	unsigned long value;
4837568cb4eSPaul Mackerras 
484c7d1f6afSAnton Blanchard 	pr_debug("Applying ADD relocate section %u to %u\n", relsec,
4857568cb4eSPaul Mackerras 	       sechdrs[relsec].sh_info);
4864edebbeaSRusty Russell 
4874edebbeaSRusty Russell 	/* First time we're called, we can fix up .TOC. */
4884edebbeaSRusty Russell 	if (!me->arch.toc_fixed) {
4894edebbeaSRusty Russell 		sym = find_dot_toc(sechdrs, strtab, symindex);
4904edebbeaSRusty Russell 		/* It's theoretically possible that a module doesn't want a
4914edebbeaSRusty Russell 		 * .TOC. so don't fail it just for that. */
4924edebbeaSRusty Russell 		if (sym)
4934edebbeaSRusty Russell 			sym->st_value = my_r2(sechdrs, me);
4944edebbeaSRusty Russell 		me->arch.toc_fixed = true;
4954edebbeaSRusty Russell 	}
4964edebbeaSRusty Russell 
4977568cb4eSPaul Mackerras 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
4987568cb4eSPaul Mackerras 		/* This is where to make the change */
4997568cb4eSPaul Mackerras 		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
5007568cb4eSPaul Mackerras 			+ rela[i].r_offset;
5017568cb4eSPaul Mackerras 		/* This is the symbol it is referring to */
5027568cb4eSPaul Mackerras 		sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
5037568cb4eSPaul Mackerras 			+ ELF64_R_SYM(rela[i].r_info);
5047568cb4eSPaul Mackerras 
505c7d1f6afSAnton Blanchard 		pr_debug("RELOC at %p: %li-type as %s (0x%lx) + %li\n",
5067568cb4eSPaul Mackerras 		       location, (long)ELF64_R_TYPE(rela[i].r_info),
5077568cb4eSPaul Mackerras 		       strtab + sym->st_name, (unsigned long)sym->st_value,
5087568cb4eSPaul Mackerras 		       (long)rela[i].r_addend);
5097568cb4eSPaul Mackerras 
5107568cb4eSPaul Mackerras 		/* `Everything is relative'. */
5117568cb4eSPaul Mackerras 		value = sym->st_value + rela[i].r_addend;
5127568cb4eSPaul Mackerras 
5137568cb4eSPaul Mackerras 		switch (ELF64_R_TYPE(rela[i].r_info)) {
5147568cb4eSPaul Mackerras 		case R_PPC64_ADDR32:
5157568cb4eSPaul Mackerras 			/* Simply set it */
5167568cb4eSPaul Mackerras 			*(u32 *)location = value;
5177568cb4eSPaul Mackerras 			break;
5187568cb4eSPaul Mackerras 
5197568cb4eSPaul Mackerras 		case R_PPC64_ADDR64:
5207568cb4eSPaul Mackerras 			/* Simply set it */
5217568cb4eSPaul Mackerras 			*(unsigned long *)location = value;
5227568cb4eSPaul Mackerras 			break;
5237568cb4eSPaul Mackerras 
5247568cb4eSPaul Mackerras 		case R_PPC64_TOC:
5257568cb4eSPaul Mackerras 			*(unsigned long *)location = my_r2(sechdrs, me);
5267568cb4eSPaul Mackerras 			break;
5277568cb4eSPaul Mackerras 
5287568cb4eSPaul Mackerras 		case R_PPC64_TOC16:
529f749edaeSAlan Modra 			/* Subtract TOC pointer */
5307568cb4eSPaul Mackerras 			value -= my_r2(sechdrs, me);
5317568cb4eSPaul Mackerras 			if (value + 0x8000 > 0xffff) {
532c7d1f6afSAnton Blanchard 				pr_err("%s: bad TOC16 relocation (0x%lx)\n",
5337568cb4eSPaul Mackerras 				       me->name, value);
5347568cb4eSPaul Mackerras 				return -ENOEXEC;
5357568cb4eSPaul Mackerras 			}
5367568cb4eSPaul Mackerras 			*((uint16_t *) location)
5377568cb4eSPaul Mackerras 				= (*((uint16_t *) location) & ~0xffff)
5387568cb4eSPaul Mackerras 				| (value & 0xffff);
5397568cb4eSPaul Mackerras 			break;
5407568cb4eSPaul Mackerras 
5411fbe9cf2SAnton Blanchard 		case R_PPC64_TOC16_LO:
5421fbe9cf2SAnton Blanchard 			/* Subtract TOC pointer */
5431fbe9cf2SAnton Blanchard 			value -= my_r2(sechdrs, me);
5441fbe9cf2SAnton Blanchard 			*((uint16_t *) location)
5451fbe9cf2SAnton Blanchard 				= (*((uint16_t *) location) & ~0xffff)
5461fbe9cf2SAnton Blanchard 				| (value & 0xffff);
5471fbe9cf2SAnton Blanchard 			break;
5481fbe9cf2SAnton Blanchard 
5497568cb4eSPaul Mackerras 		case R_PPC64_TOC16_DS:
550f749edaeSAlan Modra 			/* Subtract TOC pointer */
5517568cb4eSPaul Mackerras 			value -= my_r2(sechdrs, me);
5527568cb4eSPaul Mackerras 			if ((value & 3) != 0 || value + 0x8000 > 0xffff) {
553c7d1f6afSAnton Blanchard 				pr_err("%s: bad TOC16_DS relocation (0x%lx)\n",
5547568cb4eSPaul Mackerras 				       me->name, value);
5557568cb4eSPaul Mackerras 				return -ENOEXEC;
5567568cb4eSPaul Mackerras 			}
5577568cb4eSPaul Mackerras 			*((uint16_t *) location)
5587568cb4eSPaul Mackerras 				= (*((uint16_t *) location) & ~0xfffc)
5597568cb4eSPaul Mackerras 				| (value & 0xfffc);
5607568cb4eSPaul Mackerras 			break;
5617568cb4eSPaul Mackerras 
5621fbe9cf2SAnton Blanchard 		case R_PPC64_TOC16_LO_DS:
5631fbe9cf2SAnton Blanchard 			/* Subtract TOC pointer */
5641fbe9cf2SAnton Blanchard 			value -= my_r2(sechdrs, me);
5651fbe9cf2SAnton Blanchard 			if ((value & 3) != 0) {
566c7d1f6afSAnton Blanchard 				pr_err("%s: bad TOC16_LO_DS relocation (0x%lx)\n",
5671fbe9cf2SAnton Blanchard 				       me->name, value);
5681fbe9cf2SAnton Blanchard 				return -ENOEXEC;
5691fbe9cf2SAnton Blanchard 			}
5701fbe9cf2SAnton Blanchard 			*((uint16_t *) location)
5711fbe9cf2SAnton Blanchard 				= (*((uint16_t *) location) & ~0xfffc)
5721fbe9cf2SAnton Blanchard 				| (value & 0xfffc);
5731fbe9cf2SAnton Blanchard 			break;
5741fbe9cf2SAnton Blanchard 
5751fbe9cf2SAnton Blanchard 		case R_PPC64_TOC16_HA:
5761fbe9cf2SAnton Blanchard 			/* Subtract TOC pointer */
5771fbe9cf2SAnton Blanchard 			value -= my_r2(sechdrs, me);
5781fbe9cf2SAnton Blanchard 			value = ((value + 0x8000) >> 16);
5791fbe9cf2SAnton Blanchard 			*((uint16_t *) location)
5801fbe9cf2SAnton Blanchard 				= (*((uint16_t *) location) & ~0xffff)
5811fbe9cf2SAnton Blanchard 				| (value & 0xffff);
5821fbe9cf2SAnton Blanchard 			break;
5831fbe9cf2SAnton Blanchard 
5847568cb4eSPaul Mackerras 		case R_PPC_REL24:
5857568cb4eSPaul Mackerras 			/* FIXME: Handle weak symbols here --RR */
586a443bf6eSKamalesh Babulal 			if (sym->st_shndx == SHN_UNDEF ||
587a443bf6eSKamalesh Babulal 			    sym->st_shndx == SHN_LIVEPATCH) {
5887568cb4eSPaul Mackerras 				/* External: go via stub */
5897568cb4eSPaul Mackerras 				value = stub_for_addr(sechdrs, value, me);
5907568cb4eSPaul Mackerras 				if (!value)
5917568cb4eSPaul Mackerras 					return -ENOENT;
592250122baSNaveen N. Rao 				if (!restore_r2(strtab + sym->st_name,
593250122baSNaveen N. Rao 							(u32 *)location + 1, me))
5947568cb4eSPaul Mackerras 					return -ENOEXEC;
59515308664STorsten Duwe 
59615308664STorsten Duwe 				squash_toc_save_inst(strtab + sym->st_name, value);
597008d7a91SRusty Russell 			} else
598008d7a91SRusty Russell 				value += local_entry_offset(sym);
5997568cb4eSPaul Mackerras 
6007568cb4eSPaul Mackerras 			/* Convert value to relative */
6017568cb4eSPaul Mackerras 			value -= (unsigned long)location;
6027568cb4eSPaul Mackerras 			if (value + 0x2000000 > 0x3ffffff || (value & 3) != 0){
603c7d1f6afSAnton Blanchard 				pr_err("%s: REL24 %li out of range!\n",
6047568cb4eSPaul Mackerras 				       me->name, (long int)value);
6057568cb4eSPaul Mackerras 				return -ENOEXEC;
6067568cb4eSPaul Mackerras 			}
6077568cb4eSPaul Mackerras 
6087568cb4eSPaul Mackerras 			/* Only replace bits 2 through 26 */
6097568cb4eSPaul Mackerras 			*(uint32_t *)location
6107568cb4eSPaul Mackerras 				= (*(uint32_t *)location & ~0x03fffffc)
6117568cb4eSPaul Mackerras 				| (value & 0x03fffffc);
6127568cb4eSPaul Mackerras 			break;
6137568cb4eSPaul Mackerras 
61421c4ff80SBenjamin Herrenschmidt 		case R_PPC64_REL64:
61521c4ff80SBenjamin Herrenschmidt 			/* 64 bits relative (used by features fixups) */
61621c4ff80SBenjamin Herrenschmidt 			*location = value - (unsigned long)location;
61721c4ff80SBenjamin Herrenschmidt 			break;
61821c4ff80SBenjamin Herrenschmidt 
6199f751b82SMichael Ellerman 		case R_PPC64_REL32:
6209f751b82SMichael Ellerman 			/* 32 bits relative (used by relative exception tables) */
621b851ba02SNicholas Piggin 			/* Convert value to relative */
622b851ba02SNicholas Piggin 			value -= (unsigned long)location;
623b851ba02SNicholas Piggin 			if (value + 0x80000000 > 0xffffffff) {
624b851ba02SNicholas Piggin 				pr_err("%s: REL32 %li out of range!\n",
625b851ba02SNicholas Piggin 				       me->name, (long int)value);
626b851ba02SNicholas Piggin 				return -ENOEXEC;
627b851ba02SNicholas Piggin 			}
628b851ba02SNicholas Piggin 			*(u32 *)location = value;
6299f751b82SMichael Ellerman 			break;
6309f751b82SMichael Ellerman 
631d247da0aSRusty Russell 		case R_PPC64_TOCSAVE:
632d247da0aSRusty Russell 			/*
633d247da0aSRusty Russell 			 * Marker reloc indicates we don't have to save r2.
634d247da0aSRusty Russell 			 * That would only save us one instruction, so ignore
635d247da0aSRusty Russell 			 * it.
636d247da0aSRusty Russell 			 */
637d247da0aSRusty Russell 			break;
638d247da0aSRusty Russell 
639a61674bdSUlrich Weigand 		case R_PPC64_ENTRY:
640a61674bdSUlrich Weigand 			/*
641a61674bdSUlrich Weigand 			 * Optimize ELFv2 large code model entry point if
642a61674bdSUlrich Weigand 			 * the TOC is within 2GB range of current location.
643a61674bdSUlrich Weigand 			 */
644a61674bdSUlrich Weigand 			value = my_r2(sechdrs, me) - (unsigned long)location;
645a61674bdSUlrich Weigand 			if (value + 0x80008000 > 0xffffffff)
646a61674bdSUlrich Weigand 				break;
647a61674bdSUlrich Weigand 			/*
648a61674bdSUlrich Weigand 			 * Check for the large code model prolog sequence:
649a61674bdSUlrich Weigand 		         *	ld r2, ...(r12)
650a61674bdSUlrich Weigand 			 *	add r2, r2, r12
651a61674bdSUlrich Weigand 			 */
652a2b6f26cSChristophe Leroy 			if ((((uint32_t *)location)[0] & ~0xfffc) !=
653a2b6f26cSChristophe Leroy 			    (PPC_INST_LD | __PPC_RT(R2) | __PPC_RA(R12)))
654a61674bdSUlrich Weigand 				break;
655a2b6f26cSChristophe Leroy 			if (((uint32_t *)location)[1] !=
656a2b6f26cSChristophe Leroy 			    (PPC_INST_ADD | __PPC_RT(R2) | __PPC_RA(R2) | __PPC_RB(R12)))
657a61674bdSUlrich Weigand 				break;
658a61674bdSUlrich Weigand 			/*
659a61674bdSUlrich Weigand 			 * If found, replace it with:
660a61674bdSUlrich Weigand 			 *	addis r2, r12, (.TOC.-func)@ha
6612fb0a2c9SMichael Ellerman 			 *	addi  r2,  r2, (.TOC.-func)@l
662a61674bdSUlrich Weigand 			 */
663a2b6f26cSChristophe Leroy 			((uint32_t *)location)[0] = PPC_INST_ADDIS | __PPC_RT(R2) |
664a2b6f26cSChristophe Leroy 						    __PPC_RA(R12) | PPC_HA(value);
665a2b6f26cSChristophe Leroy 			((uint32_t *)location)[1] = PPC_INST_ADDI | __PPC_RT(R2) |
666a2b6f26cSChristophe Leroy 						    __PPC_RA(R2) | PPC_LO(value);
667a61674bdSUlrich Weigand 			break;
668a61674bdSUlrich Weigand 
6690906584aSRusty Russell 		case R_PPC64_REL16_HA:
6700906584aSRusty Russell 			/* Subtract location pointer */
6710906584aSRusty Russell 			value -= (unsigned long)location;
6720906584aSRusty Russell 			value = ((value + 0x8000) >> 16);
6730906584aSRusty Russell 			*((uint16_t *) location)
6740906584aSRusty Russell 				= (*((uint16_t *) location) & ~0xffff)
6750906584aSRusty Russell 				| (value & 0xffff);
6760906584aSRusty Russell 			break;
6770906584aSRusty Russell 
6780906584aSRusty Russell 		case R_PPC64_REL16_LO:
6790906584aSRusty Russell 			/* Subtract location pointer */
6800906584aSRusty Russell 			value -= (unsigned long)location;
6810906584aSRusty Russell 			*((uint16_t *) location)
6820906584aSRusty Russell 				= (*((uint16_t *) location) & ~0xffff)
6830906584aSRusty Russell 				| (value & 0xffff);
6840906584aSRusty Russell 			break;
6850906584aSRusty Russell 
6867568cb4eSPaul Mackerras 		default:
687c7d1f6afSAnton Blanchard 			pr_err("%s: Unknown ADD relocation: %lu\n",
6887568cb4eSPaul Mackerras 			       me->name,
6897568cb4eSPaul Mackerras 			       (unsigned long)ELF64_R_TYPE(rela[i].r_info));
6907568cb4eSPaul Mackerras 			return -ENOEXEC;
6917568cb4eSPaul Mackerras 		}
6927568cb4eSPaul Mackerras 	}
6937568cb4eSPaul Mackerras 
694136cd345SMichael Ellerman 	return 0;
695136cd345SMichael Ellerman }
696136cd345SMichael Ellerman 
697f48cb8b4SSteven Rostedt #ifdef CONFIG_DYNAMIC_FTRACE
698*03b51416SNaveen N. Rao int module_trampoline_target(struct module *mod, unsigned long addr,
699*03b51416SNaveen N. Rao 			     unsigned long *target)
700*03b51416SNaveen N. Rao {
701*03b51416SNaveen N. Rao 	struct ppc64_stub_entry *stub;
702*03b51416SNaveen N. Rao 	func_desc_t funcdata;
703*03b51416SNaveen N. Rao 	u32 magic;
704*03b51416SNaveen N. Rao 
705*03b51416SNaveen N. Rao 	if (!within_module_core(addr, mod)) {
706*03b51416SNaveen N. Rao 		pr_err("%s: stub %lx not in module %s\n", __func__, addr, mod->name);
707*03b51416SNaveen N. Rao 		return -EFAULT;
708*03b51416SNaveen N. Rao 	}
709*03b51416SNaveen N. Rao 
710*03b51416SNaveen N. Rao 	stub = (struct ppc64_stub_entry *)addr;
711*03b51416SNaveen N. Rao 
712*03b51416SNaveen N. Rao 	if (probe_kernel_read(&magic, &stub->magic, sizeof(magic))) {
713*03b51416SNaveen N. Rao 		pr_err("%s: fault reading magic for stub %lx for %s\n", __func__, addr, mod->name);
714*03b51416SNaveen N. Rao 		return -EFAULT;
715*03b51416SNaveen N. Rao 	}
716*03b51416SNaveen N. Rao 
717*03b51416SNaveen N. Rao 	if (magic != STUB_MAGIC) {
718*03b51416SNaveen N. Rao 		pr_err("%s: bad magic for stub %lx for %s\n", __func__, addr, mod->name);
719*03b51416SNaveen N. Rao 		return -EFAULT;
720*03b51416SNaveen N. Rao 	}
721*03b51416SNaveen N. Rao 
722*03b51416SNaveen N. Rao 	if (probe_kernel_read(&funcdata, &stub->funcdata, sizeof(funcdata))) {
723*03b51416SNaveen N. Rao 		pr_err("%s: fault reading funcdata for stub %lx for %s\n", __func__, addr, mod->name);
724*03b51416SNaveen N. Rao                 return -EFAULT;
725*03b51416SNaveen N. Rao 	}
726*03b51416SNaveen N. Rao 
727*03b51416SNaveen N. Rao 	*target = stub_func_addr(funcdata);
728*03b51416SNaveen N. Rao 
729*03b51416SNaveen N. Rao 	return 0;
730*03b51416SNaveen N. Rao }
731336a7b5dSMichael Ellerman 
732abba7597SNicholas Piggin #ifdef CONFIG_MPROFILE_KERNEL
733336a7b5dSMichael Ellerman 
734336a7b5dSMichael Ellerman #define PACATOC offsetof(struct paca_struct, kernel_toc)
735336a7b5dSMichael Ellerman 
736336a7b5dSMichael Ellerman /*
737336a7b5dSMichael Ellerman  * For mprofile-kernel we use a special stub for ftrace_caller() because we
738336a7b5dSMichael Ellerman  * can't rely on r2 containing this module's TOC when we enter the stub.
739336a7b5dSMichael Ellerman  *
740336a7b5dSMichael Ellerman  * That can happen if the function calling us didn't need to use the toc. In
741336a7b5dSMichael Ellerman  * that case it won't have setup r2, and the r2 value will be either the
742336a7b5dSMichael Ellerman  * kernel's toc, or possibly another modules toc.
743336a7b5dSMichael Ellerman  *
744336a7b5dSMichael Ellerman  * To deal with that this stub uses the kernel toc, which is always accessible
745336a7b5dSMichael Ellerman  * via the paca (in r13). The target (ftrace_caller()) is responsible for
746336a7b5dSMichael Ellerman  * saving and restoring the toc before returning.
747336a7b5dSMichael Ellerman  */
748ae30cc05SNaveen N. Rao static unsigned long create_ftrace_stub(const Elf64_Shdr *sechdrs,
749ae30cc05SNaveen N. Rao 				struct module *me, unsigned long addr)
750336a7b5dSMichael Ellerman {
751336a7b5dSMichael Ellerman 	struct ppc64_stub_entry *entry;
752336a7b5dSMichael Ellerman 	unsigned int i, num_stubs;
753a2b6f26cSChristophe Leroy 	/*
754a2b6f26cSChristophe Leroy 	 * ld      r12,PACATOC(r13)
755a2b6f26cSChristophe Leroy 	 * addis   r12,r12,<high>
756a2b6f26cSChristophe Leroy 	 * addi    r12,r12,<low>
757a2b6f26cSChristophe Leroy 	 * mtctr   r12
758a2b6f26cSChristophe Leroy 	 * bctr
759a2b6f26cSChristophe Leroy 	 */
760336a7b5dSMichael Ellerman 	static u32 stub_insns[] = {
761a2b6f26cSChristophe Leroy 		PPC_INST_LD | __PPC_RT(R12) | __PPC_RA(R13) | PACATOC,
762a2b6f26cSChristophe Leroy 		PPC_INST_ADDIS | __PPC_RT(R12) | __PPC_RA(R12),
763a2b6f26cSChristophe Leroy 		PPC_INST_ADDI | __PPC_RT(R12) | __PPC_RA(R12),
764a2b6f26cSChristophe Leroy 		PPC_INST_MTCTR | __PPC_RS(R12),
765a2b6f26cSChristophe Leroy 		PPC_INST_BCTR,
766336a7b5dSMichael Ellerman 	};
767336a7b5dSMichael Ellerman 	long reladdr;
768336a7b5dSMichael Ellerman 
769336a7b5dSMichael Ellerman 	num_stubs = sechdrs[me->arch.stubs_section].sh_size / sizeof(*entry);
770336a7b5dSMichael Ellerman 
771336a7b5dSMichael Ellerman 	/* Find the next available stub entry */
772336a7b5dSMichael Ellerman 	entry = (void *)sechdrs[me->arch.stubs_section].sh_addr;
773336a7b5dSMichael Ellerman 	for (i = 0; i < num_stubs && stub_func_addr(entry->funcdata); i++, entry++);
774336a7b5dSMichael Ellerman 
775336a7b5dSMichael Ellerman 	if (i >= num_stubs) {
776336a7b5dSMichael Ellerman 		pr_err("%s: Unable to find a free slot for ftrace stub.\n", me->name);
777336a7b5dSMichael Ellerman 		return 0;
778336a7b5dSMichael Ellerman 	}
779336a7b5dSMichael Ellerman 
780336a7b5dSMichael Ellerman 	memcpy(entry->jump, stub_insns, sizeof(stub_insns));
781336a7b5dSMichael Ellerman 
782336a7b5dSMichael Ellerman 	/* Stub uses address relative to kernel toc (from the paca) */
783ae30cc05SNaveen N. Rao 	reladdr = addr - kernel_toc_addr();
784336a7b5dSMichael Ellerman 	if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) {
785ae30cc05SNaveen N. Rao 		pr_err("%s: Address of %ps out of range of kernel_toc.\n",
786ae30cc05SNaveen N. Rao 							me->name, (void *)addr);
787336a7b5dSMichael Ellerman 		return 0;
788336a7b5dSMichael Ellerman 	}
789336a7b5dSMichael Ellerman 
790336a7b5dSMichael Ellerman 	entry->jump[1] |= PPC_HA(reladdr);
791336a7b5dSMichael Ellerman 	entry->jump[2] |= PPC_LO(reladdr);
792336a7b5dSMichael Ellerman 
793336a7b5dSMichael Ellerman 	/* Eventhough we don't use funcdata in the stub, it's needed elsewhere. */
794ae30cc05SNaveen N. Rao 	entry->funcdata = func_desc(addr);
795336a7b5dSMichael Ellerman 	entry->magic = STUB_MAGIC;
796336a7b5dSMichael Ellerman 
797336a7b5dSMichael Ellerman 	return (unsigned long)entry;
798336a7b5dSMichael Ellerman }
799336a7b5dSMichael Ellerman #else
800ae30cc05SNaveen N. Rao static unsigned long create_ftrace_stub(const Elf64_Shdr *sechdrs,
801ae30cc05SNaveen N. Rao 				struct module *me, unsigned long addr)
802336a7b5dSMichael Ellerman {
803ae30cc05SNaveen N. Rao 	return stub_for_addr(sechdrs, addr, me);
804336a7b5dSMichael Ellerman }
805f48cb8b4SSteven Rostedt #endif
806f48cb8b4SSteven Rostedt 
807136cd345SMichael Ellerman int module_finalize_ftrace(struct module *mod, const Elf_Shdr *sechdrs)
808136cd345SMichael Ellerman {
809ae30cc05SNaveen N. Rao 	mod->arch.tramp = create_ftrace_stub(sechdrs, mod,
810ae30cc05SNaveen N. Rao 					(unsigned long)ftrace_caller);
811ae30cc05SNaveen N. Rao #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
812ae30cc05SNaveen N. Rao 	mod->arch.tramp_regs = create_ftrace_stub(sechdrs, mod,
813ae30cc05SNaveen N. Rao 					(unsigned long)ftrace_regs_caller);
814ae30cc05SNaveen N. Rao 	if (!mod->arch.tramp_regs)
815ae30cc05SNaveen N. Rao 		return -ENOENT;
816ae30cc05SNaveen N. Rao #endif
817136cd345SMichael Ellerman 
818136cd345SMichael Ellerman 	if (!mod->arch.tramp)
819136cd345SMichael Ellerman 		return -ENOENT;
8207568cb4eSPaul Mackerras 
8217568cb4eSPaul Mackerras 	return 0;
8227568cb4eSPaul Mackerras }
823136cd345SMichael Ellerman #endif
824