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> 237568cb4eSPaul Mackerras 247568cb4eSPaul Mackerras /* FIXME: We don't do .init separately. To do this, we'd need to have 257568cb4eSPaul Mackerras a separate r2 value in the init and core section, and stub between 267568cb4eSPaul Mackerras them, too. 277568cb4eSPaul Mackerras 287568cb4eSPaul Mackerras Using a magic allocator which places modules within 32MB solves 297568cb4eSPaul Mackerras this, and makes other things simpler. Anton? 307568cb4eSPaul Mackerras --RR. */ 317568cb4eSPaul Mackerras 32f55d9665SMichael Ellerman #ifdef PPC64_ELF_ABI_v2 33008d7a91SRusty Russell 34008d7a91SRusty Russell /* An address is simply the address of the function. */ 35008d7a91SRusty Russell typedef unsigned long func_desc_t; 36008d7a91SRusty Russell 37008d7a91SRusty Russell static func_desc_t func_desc(unsigned long addr) 38008d7a91SRusty Russell { 39008d7a91SRusty Russell return addr; 40008d7a91SRusty Russell } 41008d7a91SRusty Russell static unsigned long func_addr(unsigned long addr) 42008d7a91SRusty Russell { 43008d7a91SRusty Russell return addr; 44008d7a91SRusty Russell } 45008d7a91SRusty Russell static unsigned long stub_func_addr(func_desc_t func) 46008d7a91SRusty Russell { 47008d7a91SRusty Russell return func; 48008d7a91SRusty Russell } 49008d7a91SRusty Russell 50008d7a91SRusty Russell /* PowerPC64 specific values for the Elf64_Sym st_other field. */ 51008d7a91SRusty Russell #define STO_PPC64_LOCAL_BIT 5 52008d7a91SRusty Russell #define STO_PPC64_LOCAL_MASK (7 << STO_PPC64_LOCAL_BIT) 53008d7a91SRusty Russell #define PPC64_LOCAL_ENTRY_OFFSET(other) \ 54008d7a91SRusty Russell (((1 << (((other) & STO_PPC64_LOCAL_MASK) >> STO_PPC64_LOCAL_BIT)) >> 2) << 2) 55008d7a91SRusty Russell 56008d7a91SRusty Russell static unsigned int local_entry_offset(const Elf64_Sym *sym) 57008d7a91SRusty Russell { 58008d7a91SRusty Russell /* sym->st_other indicates offset to local entry point 59008d7a91SRusty Russell * (otherwise it will assume r12 is the address of the start 60008d7a91SRusty Russell * of function and try to derive r2 from it). */ 61008d7a91SRusty Russell return PPC64_LOCAL_ENTRY_OFFSET(sym->st_other); 62008d7a91SRusty Russell } 63d2fae548SRusty Russell #else 64008d7a91SRusty Russell 65008d7a91SRusty Russell /* An address is address of the OPD entry, which contains address of fn. */ 66008d7a91SRusty Russell typedef struct ppc64_opd_entry func_desc_t; 67008d7a91SRusty Russell 68008d7a91SRusty Russell static func_desc_t func_desc(unsigned long addr) 69008d7a91SRusty Russell { 70008d7a91SRusty Russell return *(struct ppc64_opd_entry *)addr; 71008d7a91SRusty Russell } 72008d7a91SRusty Russell static unsigned long func_addr(unsigned long addr) 73008d7a91SRusty Russell { 74008d7a91SRusty Russell return func_desc(addr).funcaddr; 75008d7a91SRusty Russell } 76008d7a91SRusty Russell static unsigned long stub_func_addr(func_desc_t func) 77008d7a91SRusty Russell { 78008d7a91SRusty Russell return func.funcaddr; 79008d7a91SRusty Russell } 80008d7a91SRusty Russell static unsigned int local_entry_offset(const Elf64_Sym *sym) 81008d7a91SRusty Russell { 82008d7a91SRusty Russell return 0; 83008d7a91SRusty Russell } 845633e85bSSergey Senozhatsky 855633e85bSSergey Senozhatsky void *dereference_module_function_descriptor(struct module *mod, void *ptr) 865633e85bSSergey Senozhatsky { 875633e85bSSergey Senozhatsky if (ptr < (void *)mod->arch.start_opd || 885633e85bSSergey Senozhatsky ptr >= (void *)mod->arch.end_opd) 895633e85bSSergey Senozhatsky return ptr; 905633e85bSSergey Senozhatsky 915633e85bSSergey Senozhatsky return dereference_function_descriptor(ptr); 925633e85bSSergey Senozhatsky } 93d2fae548SRusty Russell #endif 94d2fae548SRusty Russell 95f17c4e01SMichael Ellerman #define STUB_MAGIC 0x73747562 /* stub */ 96f17c4e01SMichael Ellerman 977568cb4eSPaul Mackerras /* Like PPC32, we need little trampolines to do > 24-bit jumps (into 987568cb4eSPaul Mackerras the kernel itself). But on PPC64, these need to be used for every 997568cb4eSPaul Mackerras jump, actually, to reset r2 (TOC+0x8000). */ 1007568cb4eSPaul Mackerras struct ppc64_stub_entry 1017568cb4eSPaul Mackerras { 10283775b85SAnton Blanchard /* 28 byte jump instruction sequence (7 instructions). We only 10383775b85SAnton Blanchard * need 6 instructions on ABIv2 but we always allocate 7 so 10483775b85SAnton Blanchard * so we don't have to modify the trampoline load instruction. */ 1050e60e46eSRusty Russell u32 jump[7]; 106f17c4e01SMichael Ellerman /* Used by ftrace to identify stubs */ 107f17c4e01SMichael Ellerman u32 magic; 1087568cb4eSPaul Mackerras /* Data for the above code */ 109008d7a91SRusty Russell func_desc_t funcdata; 1107568cb4eSPaul Mackerras }; 1117568cb4eSPaul Mackerras 1125c729a11SRusty Russell /* 1135c729a11SRusty Russell * PPC64 uses 24 bit jumps, but we need to jump into other modules or 1145c729a11SRusty Russell * the kernel which may be further. So we jump to a stub. 1155c729a11SRusty Russell * 1165c729a11SRusty Russell * For ELFv1 we need to use this to set up the new r2 value (aka TOC 1175c729a11SRusty Russell * pointer). For ELFv2 it's the callee's responsibility to set up the 1185c729a11SRusty Russell * new r2, but for both we need to save the old r2. 1195c729a11SRusty Russell * 1205c729a11SRusty Russell * We could simply patch the new r2 value and function pointer into 1215c729a11SRusty Russell * the stub, but it's significantly shorter to put these values at the 1225c729a11SRusty Russell * end of the stub code, and patch the stub address (32-bits relative 1235c729a11SRusty Russell * to the TOC ptr, r2) into the stub. 124a2b6f26cSChristophe Leroy * 125a2b6f26cSChristophe Leroy * addis r11,r2, <high> 126a2b6f26cSChristophe Leroy * addi r11,r11, <low> 127a2b6f26cSChristophe Leroy * std r2,R2_STACK_OFFSET(r1) 128a2b6f26cSChristophe Leroy * ld r12,32(r11) 129a2b6f26cSChristophe Leroy * ld r2,40(r11) 130a2b6f26cSChristophe Leroy * mtctr r12 131a2b6f26cSChristophe Leroy * bctr 1325c729a11SRusty Russell */ 13383775b85SAnton Blanchard static u32 ppc64_stub_insns[] = { 134a2b6f26cSChristophe Leroy PPC_INST_ADDIS | __PPC_RT(R11) | __PPC_RA(R2), 135a2b6f26cSChristophe Leroy PPC_INST_ADDI | __PPC_RT(R11) | __PPC_RA(R11), 136fed8393eSEugene Surovegin /* Save current r2 value in magic place on the stack. */ 137a2b6f26cSChristophe Leroy PPC_INST_STD | __PPC_RS(R2) | __PPC_RA(R1) | R2_STACK_OFFSET, 138a2b6f26cSChristophe Leroy PPC_INST_LD | __PPC_RT(R12) | __PPC_RA(R11) | 32, 139f55d9665SMichael Ellerman #ifdef PPC64_ELF_ABI_v1 1405c729a11SRusty Russell /* Set up new r2 from function descriptor */ 141a2b6f26cSChristophe Leroy PPC_INST_LD | __PPC_RT(R2) | __PPC_RA(R11) | 40, 1425c729a11SRusty Russell #endif 143a2b6f26cSChristophe Leroy PPC_INST_MTCTR | __PPC_RS(R12), 144a2b6f26cSChristophe Leroy PPC_INST_BCTR, 14583775b85SAnton Blanchard }; 14683775b85SAnton Blanchard 14783775b85SAnton Blanchard #ifdef CONFIG_DYNAMIC_FTRACE 148f17c4e01SMichael Ellerman int module_trampoline_target(struct module *mod, unsigned long addr, 149dd9fa162SAnton Blanchard unsigned long *target) 150dd9fa162SAnton Blanchard { 151f17c4e01SMichael Ellerman struct ppc64_stub_entry *stub; 152f17c4e01SMichael Ellerman func_desc_t funcdata; 153f17c4e01SMichael Ellerman u32 magic; 154dd9fa162SAnton Blanchard 155f17c4e01SMichael Ellerman if (!within_module_core(addr, mod)) { 156f17c4e01SMichael Ellerman pr_err("%s: stub %lx not in module %s\n", __func__, addr, mod->name); 157dd9fa162SAnton Blanchard return -EFAULT; 158f17c4e01SMichael Ellerman } 159dd9fa162SAnton Blanchard 160f17c4e01SMichael Ellerman stub = (struct ppc64_stub_entry *)addr; 161dd9fa162SAnton Blanchard 162f17c4e01SMichael Ellerman if (probe_kernel_read(&magic, &stub->magic, sizeof(magic))) { 163f17c4e01SMichael Ellerman pr_err("%s: fault reading magic for stub %lx for %s\n", __func__, addr, mod->name); 164dd9fa162SAnton Blanchard return -EFAULT; 165f17c4e01SMichael Ellerman } 166f17c4e01SMichael Ellerman 167f17c4e01SMichael Ellerman if (magic != STUB_MAGIC) { 168f17c4e01SMichael Ellerman pr_err("%s: bad magic for stub %lx for %s\n", __func__, addr, mod->name); 169f17c4e01SMichael Ellerman return -EFAULT; 170f17c4e01SMichael Ellerman } 171f17c4e01SMichael Ellerman 172f17c4e01SMichael Ellerman if (probe_kernel_read(&funcdata, &stub->funcdata, sizeof(funcdata))) { 173f17c4e01SMichael Ellerman pr_err("%s: fault reading funcdata for stub %lx for %s\n", __func__, addr, mod->name); 174f17c4e01SMichael Ellerman return -EFAULT; 175f17c4e01SMichael Ellerman } 176f17c4e01SMichael Ellerman 177f17c4e01SMichael Ellerman *target = stub_func_addr(funcdata); 178dd9fa162SAnton Blanchard 179dd9fa162SAnton Blanchard return 0; 180dd9fa162SAnton Blanchard } 18183775b85SAnton Blanchard #endif 1827568cb4eSPaul Mackerras 1837568cb4eSPaul Mackerras /* Count how many different 24-bit relocations (different symbol, 1847568cb4eSPaul Mackerras different addend) */ 1857568cb4eSPaul Mackerras static unsigned int count_relocs(const Elf64_Rela *rela, unsigned int num) 1867568cb4eSPaul Mackerras { 187eda09fbdSEmil Medve unsigned int i, r_info, r_addend, _count_relocs; 1887568cb4eSPaul Mackerras 1897568cb4eSPaul Mackerras /* FIXME: Only count external ones --RR */ 190eda09fbdSEmil Medve _count_relocs = 0; 191eda09fbdSEmil Medve r_info = 0; 192eda09fbdSEmil Medve r_addend = 0; 193eda09fbdSEmil Medve for (i = 0; i < num; i++) 1947568cb4eSPaul Mackerras /* Only count 24-bit relocs, others don't need stubs */ 195eda09fbdSEmil Medve if (ELF64_R_TYPE(rela[i].r_info) == R_PPC_REL24 && 196eda09fbdSEmil Medve (r_info != ELF64_R_SYM(rela[i].r_info) || 197eda09fbdSEmil Medve r_addend != rela[i].r_addend)) { 198eda09fbdSEmil Medve _count_relocs++; 199eda09fbdSEmil Medve r_info = ELF64_R_SYM(rela[i].r_info); 200eda09fbdSEmil Medve r_addend = rela[i].r_addend; 2017568cb4eSPaul Mackerras } 202eda09fbdSEmil Medve 203eda09fbdSEmil Medve return _count_relocs; 2047568cb4eSPaul Mackerras } 2057568cb4eSPaul Mackerras 206eda09fbdSEmil Medve static int relacmp(const void *_x, const void *_y) 207eda09fbdSEmil Medve { 208eda09fbdSEmil Medve const Elf64_Rela *x, *y; 209eda09fbdSEmil Medve 210eda09fbdSEmil Medve y = (Elf64_Rela *)_x; 211eda09fbdSEmil Medve x = (Elf64_Rela *)_y; 212eda09fbdSEmil Medve 213eda09fbdSEmil Medve /* Compare the entire r_info (as opposed to ELF64_R_SYM(r_info) only) to 214eda09fbdSEmil Medve * make the comparison cheaper/faster. It won't affect the sorting or 215eda09fbdSEmil Medve * the counting algorithms' performance 216eda09fbdSEmil Medve */ 217eda09fbdSEmil Medve if (x->r_info < y->r_info) 218eda09fbdSEmil Medve return -1; 219eda09fbdSEmil Medve else if (x->r_info > y->r_info) 220eda09fbdSEmil Medve return 1; 221eda09fbdSEmil Medve else if (x->r_addend < y->r_addend) 222eda09fbdSEmil Medve return -1; 223eda09fbdSEmil Medve else if (x->r_addend > y->r_addend) 224eda09fbdSEmil Medve return 1; 225eda09fbdSEmil Medve else 226eda09fbdSEmil Medve return 0; 227eda09fbdSEmil Medve } 228eda09fbdSEmil Medve 2297568cb4eSPaul Mackerras /* Get size of potential trampolines required. */ 2307568cb4eSPaul Mackerras static unsigned long get_stubs_size(const Elf64_Ehdr *hdr, 2317568cb4eSPaul Mackerras const Elf64_Shdr *sechdrs) 2327568cb4eSPaul Mackerras { 2337568cb4eSPaul Mackerras /* One extra reloc so it's always 0-funcaddr terminated */ 2347568cb4eSPaul Mackerras unsigned long relocs = 1; 2357568cb4eSPaul Mackerras unsigned i; 2367568cb4eSPaul Mackerras 2377568cb4eSPaul Mackerras /* Every relocated section... */ 2387568cb4eSPaul Mackerras for (i = 1; i < hdr->e_shnum; i++) { 2397568cb4eSPaul Mackerras if (sechdrs[i].sh_type == SHT_RELA) { 240c7d1f6afSAnton Blanchard pr_debug("Found relocations in section %u\n", i); 241c7d1f6afSAnton Blanchard pr_debug("Ptr: %p. Number: %Lu\n", 2427568cb4eSPaul Mackerras (void *)sechdrs[i].sh_addr, 2437568cb4eSPaul Mackerras sechdrs[i].sh_size / sizeof(Elf64_Rela)); 244eda09fbdSEmil Medve 245eda09fbdSEmil Medve /* Sort the relocation information based on a symbol and 246eda09fbdSEmil Medve * addend key. This is a stable O(n*log n) complexity 247eda09fbdSEmil Medve * alogrithm but it will reduce the complexity of 248eda09fbdSEmil Medve * count_relocs() to linear complexity O(n) 249eda09fbdSEmil Medve */ 250eda09fbdSEmil Medve sort((void *)sechdrs[i].sh_addr, 251eda09fbdSEmil Medve sechdrs[i].sh_size / sizeof(Elf64_Rela), 252*bac7ca7bSAndrey Abramov sizeof(Elf64_Rela), relacmp, NULL); 253eda09fbdSEmil Medve 2547568cb4eSPaul Mackerras relocs += count_relocs((void *)sechdrs[i].sh_addr, 2557568cb4eSPaul Mackerras sechdrs[i].sh_size 2567568cb4eSPaul Mackerras / sizeof(Elf64_Rela)); 2577568cb4eSPaul Mackerras } 2587568cb4eSPaul Mackerras } 2597568cb4eSPaul Mackerras 260f48cb8b4SSteven Rostedt #ifdef CONFIG_DYNAMIC_FTRACE 261f48cb8b4SSteven Rostedt /* make the trampoline to the ftrace_caller */ 262f48cb8b4SSteven Rostedt relocs++; 263ae30cc05SNaveen N. Rao #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS 264ae30cc05SNaveen N. Rao /* an additional one for ftrace_regs_caller */ 265ae30cc05SNaveen N. Rao relocs++; 266ae30cc05SNaveen N. Rao #endif 267f48cb8b4SSteven Rostedt #endif 268f48cb8b4SSteven Rostedt 269c7d1f6afSAnton Blanchard pr_debug("Looks like a total of %lu stubs, max\n", relocs); 2707568cb4eSPaul Mackerras return relocs * sizeof(struct ppc64_stub_entry); 2717568cb4eSPaul Mackerras } 2727568cb4eSPaul Mackerras 2735b12c5c6SRusty Russell /* Still needed for ELFv2, for .TOC. */ 2747568cb4eSPaul Mackerras static void dedotify_versions(struct modversion_info *vers, 2757568cb4eSPaul Mackerras unsigned long size) 2767568cb4eSPaul Mackerras { 2777568cb4eSPaul Mackerras struct modversion_info *end; 2787568cb4eSPaul Mackerras 2797568cb4eSPaul Mackerras for (end = (void *)vers + size; vers < end; vers++) 280c2cbcf53SLaurent Dufour if (vers->name[0] == '.') { 2817568cb4eSPaul Mackerras memmove(vers->name, vers->name+1, strlen(vers->name)); 282c2cbcf53SLaurent Dufour } 2837568cb4eSPaul Mackerras } 2847568cb4eSPaul Mackerras 285c153693dSAlan Modra /* 286c153693dSAlan Modra * Undefined symbols which refer to .funcname, hack to funcname. Make .TOC. 287c153693dSAlan Modra * seem to be defined (value set later). 288c153693dSAlan Modra */ 2897568cb4eSPaul Mackerras static void dedotify(Elf64_Sym *syms, unsigned int numsyms, char *strtab) 2907568cb4eSPaul Mackerras { 2917568cb4eSPaul Mackerras unsigned int i; 2927568cb4eSPaul Mackerras 2937568cb4eSPaul Mackerras for (i = 1; i < numsyms; i++) { 2947568cb4eSPaul Mackerras if (syms[i].st_shndx == SHN_UNDEF) { 2957568cb4eSPaul Mackerras char *name = strtab + syms[i].st_name; 296c153693dSAlan Modra if (name[0] == '.') { 297c153693dSAlan Modra if (strcmp(name+1, "TOC.") == 0) 298c153693dSAlan Modra syms[i].st_shndx = SHN_ABS; 299f15838e9SAndreas Schwab syms[i].st_name++; 3007568cb4eSPaul Mackerras } 3017568cb4eSPaul Mackerras } 3027568cb4eSPaul Mackerras } 303c153693dSAlan Modra } 3047568cb4eSPaul Mackerras 3054edebbeaSRusty Russell static Elf64_Sym *find_dot_toc(Elf64_Shdr *sechdrs, 3064edebbeaSRusty Russell const char *strtab, 3074edebbeaSRusty Russell unsigned int symindex) 3084edebbeaSRusty Russell { 3094edebbeaSRusty Russell unsigned int i, numsyms; 3104edebbeaSRusty Russell Elf64_Sym *syms; 3114edebbeaSRusty Russell 3124edebbeaSRusty Russell syms = (Elf64_Sym *)sechdrs[symindex].sh_addr; 3134edebbeaSRusty Russell numsyms = sechdrs[symindex].sh_size / sizeof(Elf64_Sym); 3144edebbeaSRusty Russell 3154edebbeaSRusty Russell for (i = 1; i < numsyms; i++) { 316c153693dSAlan Modra if (syms[i].st_shndx == SHN_ABS 317008d7a91SRusty Russell && strcmp(strtab + syms[i].st_name, "TOC.") == 0) 3184edebbeaSRusty Russell return &syms[i]; 3194edebbeaSRusty Russell } 3204edebbeaSRusty Russell return NULL; 3214edebbeaSRusty Russell } 3224edebbeaSRusty Russell 3237568cb4eSPaul Mackerras int module_frob_arch_sections(Elf64_Ehdr *hdr, 3247568cb4eSPaul Mackerras Elf64_Shdr *sechdrs, 3257568cb4eSPaul Mackerras char *secstrings, 3267568cb4eSPaul Mackerras struct module *me) 3277568cb4eSPaul Mackerras { 3287568cb4eSPaul Mackerras unsigned int i; 3297568cb4eSPaul Mackerras 3307568cb4eSPaul Mackerras /* Find .toc and .stubs sections, symtab and strtab */ 3317568cb4eSPaul Mackerras for (i = 1; i < hdr->e_shnum; i++) { 3327568cb4eSPaul Mackerras char *p; 3337568cb4eSPaul Mackerras if (strcmp(secstrings + sechdrs[i].sh_name, ".stubs") == 0) 3347568cb4eSPaul Mackerras me->arch.stubs_section = i; 3355c45b528SAlan Modra else if (strcmp(secstrings + sechdrs[i].sh_name, ".toc") == 0) { 3367568cb4eSPaul Mackerras me->arch.toc_section = i; 3375c45b528SAlan Modra if (sechdrs[i].sh_addralign < 8) 3385c45b528SAlan Modra sechdrs[i].sh_addralign = 8; 3395c45b528SAlan Modra } 3407568cb4eSPaul Mackerras else if (strcmp(secstrings+sechdrs[i].sh_name,"__versions")==0) 3417568cb4eSPaul Mackerras dedotify_versions((void *)hdr + sechdrs[i].sh_offset, 3427568cb4eSPaul Mackerras sechdrs[i].sh_size); 3437568cb4eSPaul Mackerras 3447568cb4eSPaul Mackerras /* We don't handle .init for the moment: rename to _init */ 3457568cb4eSPaul Mackerras while ((p = strstr(secstrings + sechdrs[i].sh_name, ".init"))) 3467568cb4eSPaul Mackerras p[0] = '_'; 3477568cb4eSPaul Mackerras 3487568cb4eSPaul Mackerras if (sechdrs[i].sh_type == SHT_SYMTAB) 3497568cb4eSPaul Mackerras dedotify((void *)hdr + sechdrs[i].sh_offset, 3507568cb4eSPaul Mackerras sechdrs[i].sh_size / sizeof(Elf64_Sym), 3517568cb4eSPaul Mackerras (void *)hdr 3527568cb4eSPaul Mackerras + sechdrs[sechdrs[i].sh_link].sh_offset); 3537568cb4eSPaul Mackerras } 354f749edaeSAlan Modra 355f749edaeSAlan Modra if (!me->arch.stubs_section) { 356c7d1f6afSAnton Blanchard pr_err("%s: doesn't contain .stubs.\n", me->name); 3577568cb4eSPaul Mackerras return -ENOEXEC; 3587568cb4eSPaul Mackerras } 3597568cb4eSPaul Mackerras 360f749edaeSAlan Modra /* If we don't have a .toc, just use .stubs. We need to set r2 361f749edaeSAlan Modra to some reasonable value in case the module calls out to 362f749edaeSAlan Modra other functions via a stub, or if a function pointer escapes 363f749edaeSAlan Modra the module by some means. */ 364f749edaeSAlan Modra if (!me->arch.toc_section) 365f749edaeSAlan Modra me->arch.toc_section = me->arch.stubs_section; 366f749edaeSAlan Modra 3677568cb4eSPaul Mackerras /* Override the stubs size */ 3687568cb4eSPaul Mackerras sechdrs[me->arch.stubs_section].sh_size = get_stubs_size(hdr, sechdrs); 3697568cb4eSPaul Mackerras return 0; 3707568cb4eSPaul Mackerras } 3717568cb4eSPaul Mackerras 3725c45b528SAlan Modra /* 3735c45b528SAlan Modra * r2 is the TOC pointer: it actually points 0x8000 into the TOC (this gives the 3745c45b528SAlan Modra * value maximum span in an instruction which uses a signed offset). Round down 3755c45b528SAlan Modra * to a 256 byte boundary for the odd case where we are setting up r2 without a 3765c45b528SAlan Modra * .toc section. 3775c45b528SAlan Modra */ 378136cd345SMichael Ellerman static inline unsigned long my_r2(const Elf64_Shdr *sechdrs, struct module *me) 3797568cb4eSPaul Mackerras { 3805c45b528SAlan Modra return (sechdrs[me->arch.toc_section].sh_addr & ~0xfful) + 0x8000; 3817568cb4eSPaul Mackerras } 3827568cb4eSPaul Mackerras 3837568cb4eSPaul Mackerras /* Patch stub to reference function and correct r2 value. */ 384136cd345SMichael Ellerman static inline int create_stub(const Elf64_Shdr *sechdrs, 3857568cb4eSPaul Mackerras struct ppc64_stub_entry *entry, 386008d7a91SRusty Russell unsigned long addr, 3877568cb4eSPaul Mackerras struct module *me) 3887568cb4eSPaul Mackerras { 3897568cb4eSPaul Mackerras long reladdr; 3907568cb4eSPaul Mackerras 39183775b85SAnton Blanchard memcpy(entry->jump, ppc64_stub_insns, sizeof(ppc64_stub_insns)); 3927568cb4eSPaul Mackerras 3937568cb4eSPaul Mackerras /* Stub uses address relative to r2. */ 3947568cb4eSPaul Mackerras reladdr = (unsigned long)entry - my_r2(sechdrs, me); 3957568cb4eSPaul Mackerras if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) { 396c7d1f6afSAnton Blanchard pr_err("%s: Address %p of stub out of range of %p.\n", 3977568cb4eSPaul Mackerras me->name, (void *)reladdr, (void *)my_r2); 3987568cb4eSPaul Mackerras return 0; 3997568cb4eSPaul Mackerras } 400c7d1f6afSAnton Blanchard pr_debug("Stub %p get data from reladdr %li\n", entry, reladdr); 4017568cb4eSPaul Mackerras 4020e60e46eSRusty Russell entry->jump[0] |= PPC_HA(reladdr); 4030e60e46eSRusty Russell entry->jump[1] |= PPC_LO(reladdr); 404008d7a91SRusty Russell entry->funcdata = func_desc(addr); 405f17c4e01SMichael Ellerman entry->magic = STUB_MAGIC; 406f17c4e01SMichael Ellerman 4077568cb4eSPaul Mackerras return 1; 4087568cb4eSPaul Mackerras } 4097568cb4eSPaul Mackerras 410008d7a91SRusty Russell /* Create stub to jump to function described in this OPD/ptr: we need the 4117568cb4eSPaul Mackerras stub to set up the TOC ptr (r2) for the function. */ 412136cd345SMichael Ellerman static unsigned long stub_for_addr(const Elf64_Shdr *sechdrs, 413008d7a91SRusty Russell unsigned long addr, 4147568cb4eSPaul Mackerras struct module *me) 4157568cb4eSPaul Mackerras { 4167568cb4eSPaul Mackerras struct ppc64_stub_entry *stubs; 4177568cb4eSPaul Mackerras unsigned int i, num_stubs; 4187568cb4eSPaul Mackerras 4197568cb4eSPaul Mackerras num_stubs = sechdrs[me->arch.stubs_section].sh_size / sizeof(*stubs); 4207568cb4eSPaul Mackerras 4217568cb4eSPaul Mackerras /* Find this stub, or if that fails, the next avail. entry */ 4227568cb4eSPaul Mackerras stubs = (void *)sechdrs[me->arch.stubs_section].sh_addr; 423008d7a91SRusty Russell for (i = 0; stub_func_addr(stubs[i].funcdata); i++) { 4241c0437afSKamalesh Babulal if (WARN_ON(i >= num_stubs)) 4251c0437afSKamalesh Babulal return 0; 4267568cb4eSPaul Mackerras 427008d7a91SRusty Russell if (stub_func_addr(stubs[i].funcdata) == func_addr(addr)) 4287568cb4eSPaul Mackerras return (unsigned long)&stubs[i]; 4297568cb4eSPaul Mackerras } 4307568cb4eSPaul Mackerras 431008d7a91SRusty Russell if (!create_stub(sechdrs, &stubs[i], addr, me)) 4327568cb4eSPaul Mackerras return 0; 4337568cb4eSPaul Mackerras 4347568cb4eSPaul Mackerras return (unsigned long)&stubs[i]; 4357568cb4eSPaul Mackerras } 4367568cb4eSPaul Mackerras 437abba7597SNicholas Piggin #ifdef CONFIG_MPROFILE_KERNEL 438250122baSNaveen N. Rao static bool is_mprofile_mcount_callsite(const char *name, u32 *instruction) 43915308664STorsten Duwe { 440250122baSNaveen N. Rao if (strcmp("_mcount", name)) 441250122baSNaveen N. Rao return false; 442250122baSNaveen N. Rao 44315308664STorsten Duwe /* 44415308664STorsten Duwe * Check if this is one of the -mprofile-kernel sequences. 44515308664STorsten Duwe */ 44615308664STorsten Duwe if (instruction[-1] == PPC_INST_STD_LR && 44715308664STorsten Duwe instruction[-2] == PPC_INST_MFLR) 44815308664STorsten Duwe return true; 44915308664STorsten Duwe 45015308664STorsten Duwe if (instruction[-1] == PPC_INST_MFLR) 45115308664STorsten Duwe return true; 45215308664STorsten Duwe 45315308664STorsten Duwe return false; 45415308664STorsten Duwe } 45515308664STorsten Duwe 45615308664STorsten Duwe /* 45715308664STorsten Duwe * In case of _mcount calls, do not save the current callee's TOC (in r2) into 45815308664STorsten Duwe * the original caller's stack frame. If we did we would clobber the saved TOC 45915308664STorsten Duwe * value of the original caller. 46015308664STorsten Duwe */ 46115308664STorsten Duwe static void squash_toc_save_inst(const char *name, unsigned long addr) 46215308664STorsten Duwe { 46315308664STorsten Duwe struct ppc64_stub_entry *stub = (struct ppc64_stub_entry *)addr; 46415308664STorsten Duwe 46515308664STorsten Duwe /* Only for calls to _mcount */ 46615308664STorsten Duwe if (strcmp("_mcount", name) != 0) 46715308664STorsten Duwe return; 46815308664STorsten Duwe 46915308664STorsten Duwe stub->jump[2] = PPC_INST_NOP; 47015308664STorsten Duwe } 47115308664STorsten Duwe #else 47215308664STorsten Duwe static void squash_toc_save_inst(const char *name, unsigned long addr) { } 47315308664STorsten Duwe 474250122baSNaveen N. Rao static bool is_mprofile_mcount_callsite(const char *name, u32 *instruction) 47515308664STorsten Duwe { 47615308664STorsten Duwe return false; 47715308664STorsten Duwe } 47815308664STorsten Duwe #endif 47915308664STorsten Duwe 4807568cb4eSPaul Mackerras /* We expect a noop next: if it is, replace it with instruction to 4817568cb4eSPaul Mackerras restore r2. */ 482250122baSNaveen N. Rao static int restore_r2(const char *name, u32 *instruction, struct module *me) 4837568cb4eSPaul Mackerras { 484b9eab08dSJosh Poimboeuf u32 *prev_insn = instruction - 1; 485b9eab08dSJosh Poimboeuf 486250122baSNaveen N. Rao if (is_mprofile_mcount_callsite(name, prev_insn)) 487b9eab08dSJosh Poimboeuf return 1; 488b9eab08dSJosh Poimboeuf 489b9eab08dSJosh Poimboeuf /* 490b9eab08dSJosh Poimboeuf * Make sure the branch isn't a sibling call. Sibling calls aren't 491b9eab08dSJosh Poimboeuf * "link" branches and they don't return, so they don't need the r2 492b9eab08dSJosh Poimboeuf * restore afterwards. 493b9eab08dSJosh Poimboeuf */ 494b9eab08dSJosh Poimboeuf if (!instr_is_relative_link_branch(*prev_insn)) 49515308664STorsten Duwe return 1; 49631278b17SMichael Ellerman 49731278b17SMichael Ellerman if (*instruction != PPC_INST_NOP) { 4981ea61ea2SJosh Poimboeuf pr_err("%s: Expected nop after call, got %08x at %pS\n", 4991ea61ea2SJosh Poimboeuf me->name, *instruction, instruction); 5007568cb4eSPaul Mackerras return 0; 5017568cb4eSPaul Mackerras } 502d2fae548SRusty Russell /* ld r2,R2_STACK_OFFSET(r1) */ 50315308664STorsten Duwe *instruction = PPC_INST_LD_TOC; 5047568cb4eSPaul Mackerras return 1; 5057568cb4eSPaul Mackerras } 5067568cb4eSPaul Mackerras 5077568cb4eSPaul Mackerras int apply_relocate_add(Elf64_Shdr *sechdrs, 5087568cb4eSPaul Mackerras const char *strtab, 5097568cb4eSPaul Mackerras unsigned int symindex, 5107568cb4eSPaul Mackerras unsigned int relsec, 5117568cb4eSPaul Mackerras struct module *me) 5127568cb4eSPaul Mackerras { 5137568cb4eSPaul Mackerras unsigned int i; 5147568cb4eSPaul Mackerras Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr; 5157568cb4eSPaul Mackerras Elf64_Sym *sym; 5167568cb4eSPaul Mackerras unsigned long *location; 5177568cb4eSPaul Mackerras unsigned long value; 5187568cb4eSPaul Mackerras 519c7d1f6afSAnton Blanchard pr_debug("Applying ADD relocate section %u to %u\n", relsec, 5207568cb4eSPaul Mackerras sechdrs[relsec].sh_info); 5214edebbeaSRusty Russell 5224edebbeaSRusty Russell /* First time we're called, we can fix up .TOC. */ 5234edebbeaSRusty Russell if (!me->arch.toc_fixed) { 5244edebbeaSRusty Russell sym = find_dot_toc(sechdrs, strtab, symindex); 5254edebbeaSRusty Russell /* It's theoretically possible that a module doesn't want a 5264edebbeaSRusty Russell * .TOC. so don't fail it just for that. */ 5274edebbeaSRusty Russell if (sym) 5284edebbeaSRusty Russell sym->st_value = my_r2(sechdrs, me); 5294edebbeaSRusty Russell me->arch.toc_fixed = true; 5304edebbeaSRusty Russell } 5314edebbeaSRusty Russell 5327568cb4eSPaul Mackerras for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) { 5337568cb4eSPaul Mackerras /* This is where to make the change */ 5347568cb4eSPaul Mackerras location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr 5357568cb4eSPaul Mackerras + rela[i].r_offset; 5367568cb4eSPaul Mackerras /* This is the symbol it is referring to */ 5377568cb4eSPaul Mackerras sym = (Elf64_Sym *)sechdrs[symindex].sh_addr 5387568cb4eSPaul Mackerras + ELF64_R_SYM(rela[i].r_info); 5397568cb4eSPaul Mackerras 540c7d1f6afSAnton Blanchard pr_debug("RELOC at %p: %li-type as %s (0x%lx) + %li\n", 5417568cb4eSPaul Mackerras location, (long)ELF64_R_TYPE(rela[i].r_info), 5427568cb4eSPaul Mackerras strtab + sym->st_name, (unsigned long)sym->st_value, 5437568cb4eSPaul Mackerras (long)rela[i].r_addend); 5447568cb4eSPaul Mackerras 5457568cb4eSPaul Mackerras /* `Everything is relative'. */ 5467568cb4eSPaul Mackerras value = sym->st_value + rela[i].r_addend; 5477568cb4eSPaul Mackerras 5487568cb4eSPaul Mackerras switch (ELF64_R_TYPE(rela[i].r_info)) { 5497568cb4eSPaul Mackerras case R_PPC64_ADDR32: 5507568cb4eSPaul Mackerras /* Simply set it */ 5517568cb4eSPaul Mackerras *(u32 *)location = value; 5527568cb4eSPaul Mackerras break; 5537568cb4eSPaul Mackerras 5547568cb4eSPaul Mackerras case R_PPC64_ADDR64: 5557568cb4eSPaul Mackerras /* Simply set it */ 5567568cb4eSPaul Mackerras *(unsigned long *)location = value; 5577568cb4eSPaul Mackerras break; 5587568cb4eSPaul Mackerras 5597568cb4eSPaul Mackerras case R_PPC64_TOC: 5607568cb4eSPaul Mackerras *(unsigned long *)location = my_r2(sechdrs, me); 5617568cb4eSPaul Mackerras break; 5627568cb4eSPaul Mackerras 5637568cb4eSPaul Mackerras case R_PPC64_TOC16: 564f749edaeSAlan Modra /* Subtract TOC pointer */ 5657568cb4eSPaul Mackerras value -= my_r2(sechdrs, me); 5667568cb4eSPaul Mackerras if (value + 0x8000 > 0xffff) { 567c7d1f6afSAnton Blanchard pr_err("%s: bad TOC16 relocation (0x%lx)\n", 5687568cb4eSPaul Mackerras me->name, value); 5697568cb4eSPaul Mackerras return -ENOEXEC; 5707568cb4eSPaul Mackerras } 5717568cb4eSPaul Mackerras *((uint16_t *) location) 5727568cb4eSPaul Mackerras = (*((uint16_t *) location) & ~0xffff) 5737568cb4eSPaul Mackerras | (value & 0xffff); 5747568cb4eSPaul Mackerras break; 5757568cb4eSPaul Mackerras 5761fbe9cf2SAnton Blanchard case R_PPC64_TOC16_LO: 5771fbe9cf2SAnton Blanchard /* Subtract TOC pointer */ 5781fbe9cf2SAnton Blanchard value -= my_r2(sechdrs, me); 5791fbe9cf2SAnton Blanchard *((uint16_t *) location) 5801fbe9cf2SAnton Blanchard = (*((uint16_t *) location) & ~0xffff) 5811fbe9cf2SAnton Blanchard | (value & 0xffff); 5821fbe9cf2SAnton Blanchard break; 5831fbe9cf2SAnton Blanchard 5847568cb4eSPaul Mackerras case R_PPC64_TOC16_DS: 585f749edaeSAlan Modra /* Subtract TOC pointer */ 5867568cb4eSPaul Mackerras value -= my_r2(sechdrs, me); 5877568cb4eSPaul Mackerras if ((value & 3) != 0 || value + 0x8000 > 0xffff) { 588c7d1f6afSAnton Blanchard pr_err("%s: bad TOC16_DS relocation (0x%lx)\n", 5897568cb4eSPaul Mackerras me->name, value); 5907568cb4eSPaul Mackerras return -ENOEXEC; 5917568cb4eSPaul Mackerras } 5927568cb4eSPaul Mackerras *((uint16_t *) location) 5937568cb4eSPaul Mackerras = (*((uint16_t *) location) & ~0xfffc) 5947568cb4eSPaul Mackerras | (value & 0xfffc); 5957568cb4eSPaul Mackerras break; 5967568cb4eSPaul Mackerras 5971fbe9cf2SAnton Blanchard case R_PPC64_TOC16_LO_DS: 5981fbe9cf2SAnton Blanchard /* Subtract TOC pointer */ 5991fbe9cf2SAnton Blanchard value -= my_r2(sechdrs, me); 6001fbe9cf2SAnton Blanchard if ((value & 3) != 0) { 601c7d1f6afSAnton Blanchard pr_err("%s: bad TOC16_LO_DS relocation (0x%lx)\n", 6021fbe9cf2SAnton Blanchard me->name, value); 6031fbe9cf2SAnton Blanchard return -ENOEXEC; 6041fbe9cf2SAnton Blanchard } 6051fbe9cf2SAnton Blanchard *((uint16_t *) location) 6061fbe9cf2SAnton Blanchard = (*((uint16_t *) location) & ~0xfffc) 6071fbe9cf2SAnton Blanchard | (value & 0xfffc); 6081fbe9cf2SAnton Blanchard break; 6091fbe9cf2SAnton Blanchard 6101fbe9cf2SAnton Blanchard case R_PPC64_TOC16_HA: 6111fbe9cf2SAnton Blanchard /* Subtract TOC pointer */ 6121fbe9cf2SAnton Blanchard value -= my_r2(sechdrs, me); 6131fbe9cf2SAnton Blanchard value = ((value + 0x8000) >> 16); 6141fbe9cf2SAnton Blanchard *((uint16_t *) location) 6151fbe9cf2SAnton Blanchard = (*((uint16_t *) location) & ~0xffff) 6161fbe9cf2SAnton Blanchard | (value & 0xffff); 6171fbe9cf2SAnton Blanchard break; 6181fbe9cf2SAnton Blanchard 6197568cb4eSPaul Mackerras case R_PPC_REL24: 6207568cb4eSPaul Mackerras /* FIXME: Handle weak symbols here --RR */ 621a443bf6eSKamalesh Babulal if (sym->st_shndx == SHN_UNDEF || 622a443bf6eSKamalesh Babulal sym->st_shndx == SHN_LIVEPATCH) { 6237568cb4eSPaul Mackerras /* External: go via stub */ 6247568cb4eSPaul Mackerras value = stub_for_addr(sechdrs, value, me); 6257568cb4eSPaul Mackerras if (!value) 6267568cb4eSPaul Mackerras return -ENOENT; 627250122baSNaveen N. Rao if (!restore_r2(strtab + sym->st_name, 628250122baSNaveen N. Rao (u32 *)location + 1, me)) 6297568cb4eSPaul Mackerras return -ENOEXEC; 63015308664STorsten Duwe 63115308664STorsten Duwe squash_toc_save_inst(strtab + sym->st_name, value); 632008d7a91SRusty Russell } else 633008d7a91SRusty Russell value += local_entry_offset(sym); 6347568cb4eSPaul Mackerras 6357568cb4eSPaul Mackerras /* Convert value to relative */ 6367568cb4eSPaul Mackerras value -= (unsigned long)location; 6377568cb4eSPaul Mackerras if (value + 0x2000000 > 0x3ffffff || (value & 3) != 0){ 638c7d1f6afSAnton Blanchard pr_err("%s: REL24 %li out of range!\n", 6397568cb4eSPaul Mackerras me->name, (long int)value); 6407568cb4eSPaul Mackerras return -ENOEXEC; 6417568cb4eSPaul Mackerras } 6427568cb4eSPaul Mackerras 6437568cb4eSPaul Mackerras /* Only replace bits 2 through 26 */ 6447568cb4eSPaul Mackerras *(uint32_t *)location 6457568cb4eSPaul Mackerras = (*(uint32_t *)location & ~0x03fffffc) 6467568cb4eSPaul Mackerras | (value & 0x03fffffc); 6477568cb4eSPaul Mackerras break; 6487568cb4eSPaul Mackerras 64921c4ff80SBenjamin Herrenschmidt case R_PPC64_REL64: 65021c4ff80SBenjamin Herrenschmidt /* 64 bits relative (used by features fixups) */ 65121c4ff80SBenjamin Herrenschmidt *location = value - (unsigned long)location; 65221c4ff80SBenjamin Herrenschmidt break; 65321c4ff80SBenjamin Herrenschmidt 6549f751b82SMichael Ellerman case R_PPC64_REL32: 6559f751b82SMichael Ellerman /* 32 bits relative (used by relative exception tables) */ 656b851ba02SNicholas Piggin /* Convert value to relative */ 657b851ba02SNicholas Piggin value -= (unsigned long)location; 658b851ba02SNicholas Piggin if (value + 0x80000000 > 0xffffffff) { 659b851ba02SNicholas Piggin pr_err("%s: REL32 %li out of range!\n", 660b851ba02SNicholas Piggin me->name, (long int)value); 661b851ba02SNicholas Piggin return -ENOEXEC; 662b851ba02SNicholas Piggin } 663b851ba02SNicholas Piggin *(u32 *)location = value; 6649f751b82SMichael Ellerman break; 6659f751b82SMichael Ellerman 666d247da0aSRusty Russell case R_PPC64_TOCSAVE: 667d247da0aSRusty Russell /* 668d247da0aSRusty Russell * Marker reloc indicates we don't have to save r2. 669d247da0aSRusty Russell * That would only save us one instruction, so ignore 670d247da0aSRusty Russell * it. 671d247da0aSRusty Russell */ 672d247da0aSRusty Russell break; 673d247da0aSRusty Russell 674a61674bdSUlrich Weigand case R_PPC64_ENTRY: 675a61674bdSUlrich Weigand /* 676a61674bdSUlrich Weigand * Optimize ELFv2 large code model entry point if 677a61674bdSUlrich Weigand * the TOC is within 2GB range of current location. 678a61674bdSUlrich Weigand */ 679a61674bdSUlrich Weigand value = my_r2(sechdrs, me) - (unsigned long)location; 680a61674bdSUlrich Weigand if (value + 0x80008000 > 0xffffffff) 681a61674bdSUlrich Weigand break; 682a61674bdSUlrich Weigand /* 683a61674bdSUlrich Weigand * Check for the large code model prolog sequence: 684a61674bdSUlrich Weigand * ld r2, ...(r12) 685a61674bdSUlrich Weigand * add r2, r2, r12 686a61674bdSUlrich Weigand */ 687a2b6f26cSChristophe Leroy if ((((uint32_t *)location)[0] & ~0xfffc) != 688a2b6f26cSChristophe Leroy (PPC_INST_LD | __PPC_RT(R2) | __PPC_RA(R12))) 689a61674bdSUlrich Weigand break; 690a2b6f26cSChristophe Leroy if (((uint32_t *)location)[1] != 691a2b6f26cSChristophe Leroy (PPC_INST_ADD | __PPC_RT(R2) | __PPC_RA(R2) | __PPC_RB(R12))) 692a61674bdSUlrich Weigand break; 693a61674bdSUlrich Weigand /* 694a61674bdSUlrich Weigand * If found, replace it with: 695a61674bdSUlrich Weigand * addis r2, r12, (.TOC.-func)@ha 6962fb0a2c9SMichael Ellerman * addi r2, r2, (.TOC.-func)@l 697a61674bdSUlrich Weigand */ 698a2b6f26cSChristophe Leroy ((uint32_t *)location)[0] = PPC_INST_ADDIS | __PPC_RT(R2) | 699a2b6f26cSChristophe Leroy __PPC_RA(R12) | PPC_HA(value); 700a2b6f26cSChristophe Leroy ((uint32_t *)location)[1] = PPC_INST_ADDI | __PPC_RT(R2) | 701a2b6f26cSChristophe Leroy __PPC_RA(R2) | PPC_LO(value); 702a61674bdSUlrich Weigand break; 703a61674bdSUlrich Weigand 7040906584aSRusty Russell case R_PPC64_REL16_HA: 7050906584aSRusty Russell /* Subtract location pointer */ 7060906584aSRusty Russell value -= (unsigned long)location; 7070906584aSRusty Russell value = ((value + 0x8000) >> 16); 7080906584aSRusty Russell *((uint16_t *) location) 7090906584aSRusty Russell = (*((uint16_t *) location) & ~0xffff) 7100906584aSRusty Russell | (value & 0xffff); 7110906584aSRusty Russell break; 7120906584aSRusty Russell 7130906584aSRusty Russell case R_PPC64_REL16_LO: 7140906584aSRusty Russell /* Subtract location pointer */ 7150906584aSRusty Russell value -= (unsigned long)location; 7160906584aSRusty Russell *((uint16_t *) location) 7170906584aSRusty Russell = (*((uint16_t *) location) & ~0xffff) 7180906584aSRusty Russell | (value & 0xffff); 7190906584aSRusty Russell break; 7200906584aSRusty Russell 7217568cb4eSPaul Mackerras default: 722c7d1f6afSAnton Blanchard pr_err("%s: Unknown ADD relocation: %lu\n", 7237568cb4eSPaul Mackerras me->name, 7247568cb4eSPaul Mackerras (unsigned long)ELF64_R_TYPE(rela[i].r_info)); 7257568cb4eSPaul Mackerras return -ENOEXEC; 7267568cb4eSPaul Mackerras } 7277568cb4eSPaul Mackerras } 7287568cb4eSPaul Mackerras 729136cd345SMichael Ellerman return 0; 730136cd345SMichael Ellerman } 731136cd345SMichael Ellerman 732f48cb8b4SSteven Rostedt #ifdef CONFIG_DYNAMIC_FTRACE 733336a7b5dSMichael Ellerman 734abba7597SNicholas Piggin #ifdef CONFIG_MPROFILE_KERNEL 735336a7b5dSMichael Ellerman 736336a7b5dSMichael Ellerman #define PACATOC offsetof(struct paca_struct, kernel_toc) 737336a7b5dSMichael Ellerman 738336a7b5dSMichael Ellerman /* 739336a7b5dSMichael Ellerman * For mprofile-kernel we use a special stub for ftrace_caller() because we 740336a7b5dSMichael Ellerman * can't rely on r2 containing this module's TOC when we enter the stub. 741336a7b5dSMichael Ellerman * 742336a7b5dSMichael Ellerman * That can happen if the function calling us didn't need to use the toc. In 743336a7b5dSMichael Ellerman * that case it won't have setup r2, and the r2 value will be either the 744336a7b5dSMichael Ellerman * kernel's toc, or possibly another modules toc. 745336a7b5dSMichael Ellerman * 746336a7b5dSMichael Ellerman * To deal with that this stub uses the kernel toc, which is always accessible 747336a7b5dSMichael Ellerman * via the paca (in r13). The target (ftrace_caller()) is responsible for 748336a7b5dSMichael Ellerman * saving and restoring the toc before returning. 749336a7b5dSMichael Ellerman */ 750ae30cc05SNaveen N. Rao static unsigned long create_ftrace_stub(const Elf64_Shdr *sechdrs, 751ae30cc05SNaveen N. Rao struct module *me, unsigned long addr) 752336a7b5dSMichael Ellerman { 753336a7b5dSMichael Ellerman struct ppc64_stub_entry *entry; 754336a7b5dSMichael Ellerman unsigned int i, num_stubs; 755a2b6f26cSChristophe Leroy /* 756a2b6f26cSChristophe Leroy * ld r12,PACATOC(r13) 757a2b6f26cSChristophe Leroy * addis r12,r12,<high> 758a2b6f26cSChristophe Leroy * addi r12,r12,<low> 759a2b6f26cSChristophe Leroy * mtctr r12 760a2b6f26cSChristophe Leroy * bctr 761a2b6f26cSChristophe Leroy */ 762336a7b5dSMichael Ellerman static u32 stub_insns[] = { 763a2b6f26cSChristophe Leroy PPC_INST_LD | __PPC_RT(R12) | __PPC_RA(R13) | PACATOC, 764a2b6f26cSChristophe Leroy PPC_INST_ADDIS | __PPC_RT(R12) | __PPC_RA(R12), 765a2b6f26cSChristophe Leroy PPC_INST_ADDI | __PPC_RT(R12) | __PPC_RA(R12), 766a2b6f26cSChristophe Leroy PPC_INST_MTCTR | __PPC_RS(R12), 767a2b6f26cSChristophe Leroy PPC_INST_BCTR, 768336a7b5dSMichael Ellerman }; 769336a7b5dSMichael Ellerman long reladdr; 770336a7b5dSMichael Ellerman 771336a7b5dSMichael Ellerman num_stubs = sechdrs[me->arch.stubs_section].sh_size / sizeof(*entry); 772336a7b5dSMichael Ellerman 773336a7b5dSMichael Ellerman /* Find the next available stub entry */ 774336a7b5dSMichael Ellerman entry = (void *)sechdrs[me->arch.stubs_section].sh_addr; 775336a7b5dSMichael Ellerman for (i = 0; i < num_stubs && stub_func_addr(entry->funcdata); i++, entry++); 776336a7b5dSMichael Ellerman 777336a7b5dSMichael Ellerman if (i >= num_stubs) { 778336a7b5dSMichael Ellerman pr_err("%s: Unable to find a free slot for ftrace stub.\n", me->name); 779336a7b5dSMichael Ellerman return 0; 780336a7b5dSMichael Ellerman } 781336a7b5dSMichael Ellerman 782336a7b5dSMichael Ellerman memcpy(entry->jump, stub_insns, sizeof(stub_insns)); 783336a7b5dSMichael Ellerman 784336a7b5dSMichael Ellerman /* Stub uses address relative to kernel toc (from the paca) */ 785ae30cc05SNaveen N. Rao reladdr = addr - kernel_toc_addr(); 786336a7b5dSMichael Ellerman if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) { 787ae30cc05SNaveen N. Rao pr_err("%s: Address of %ps out of range of kernel_toc.\n", 788ae30cc05SNaveen N. Rao me->name, (void *)addr); 789336a7b5dSMichael Ellerman return 0; 790336a7b5dSMichael Ellerman } 791336a7b5dSMichael Ellerman 792336a7b5dSMichael Ellerman entry->jump[1] |= PPC_HA(reladdr); 793336a7b5dSMichael Ellerman entry->jump[2] |= PPC_LO(reladdr); 794336a7b5dSMichael Ellerman 795336a7b5dSMichael Ellerman /* Eventhough we don't use funcdata in the stub, it's needed elsewhere. */ 796ae30cc05SNaveen N. Rao entry->funcdata = func_desc(addr); 797336a7b5dSMichael Ellerman entry->magic = STUB_MAGIC; 798336a7b5dSMichael Ellerman 799336a7b5dSMichael Ellerman return (unsigned long)entry; 800336a7b5dSMichael Ellerman } 801336a7b5dSMichael Ellerman #else 802ae30cc05SNaveen N. Rao static unsigned long create_ftrace_stub(const Elf64_Shdr *sechdrs, 803ae30cc05SNaveen N. Rao struct module *me, unsigned long addr) 804336a7b5dSMichael Ellerman { 805ae30cc05SNaveen N. Rao return stub_for_addr(sechdrs, addr, me); 806336a7b5dSMichael Ellerman } 807f48cb8b4SSteven Rostedt #endif 808f48cb8b4SSteven Rostedt 809136cd345SMichael Ellerman int module_finalize_ftrace(struct module *mod, const Elf_Shdr *sechdrs) 810136cd345SMichael Ellerman { 811ae30cc05SNaveen N. Rao mod->arch.tramp = create_ftrace_stub(sechdrs, mod, 812ae30cc05SNaveen N. Rao (unsigned long)ftrace_caller); 813ae30cc05SNaveen N. Rao #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS 814ae30cc05SNaveen N. Rao mod->arch.tramp_regs = create_ftrace_stub(sechdrs, mod, 815ae30cc05SNaveen N. Rao (unsigned long)ftrace_regs_caller); 816ae30cc05SNaveen N. Rao if (!mod->arch.tramp_regs) 817ae30cc05SNaveen N. Rao return -ENOENT; 818ae30cc05SNaveen N. Rao #endif 819136cd345SMichael Ellerman 820136cd345SMichael Ellerman if (!mod->arch.tramp) 821136cd345SMichael Ellerman return -ENOENT; 8227568cb4eSPaul Mackerras 8237568cb4eSPaul Mackerras return 0; 8247568cb4eSPaul Mackerras } 825136cd345SMichael Ellerman #endif 826