1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Kernel module help for PPC64.
3 Copyright (C) 2001, 2003 Rusty Russell IBM Corporation.
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/module.h>
10 #include <linux/elf.h>
11 #include <linux/moduleloader.h>
12 #include <linux/err.h>
13 #include <linux/vmalloc.h>
14 #include <linux/ftrace.h>
15 #include <linux/bug.h>
16 #include <linux/uaccess.h>
17 #include <asm/module.h>
18 #include <asm/firmware.h>
19 #include <asm/code-patching.h>
20 #include <linux/sort.h>
21 #include <asm/setup.h>
22 #include <asm/sections.h>
25 /* FIXME: We don't do .init separately. To do this, we'd need to have
26 a separate r2 value in the init and core section, and stub between
29 Using a magic allocator which places modules within 32MB solves
30 this, and makes other things simpler. Anton?
33 #ifdef PPC64_ELF_ABI_v2
35 /* An address is simply the address of the function. */
36 typedef unsigned long func_desc_t;
38 static func_desc_t func_desc(unsigned long addr)
42 static unsigned long func_addr(unsigned long addr)
46 static unsigned long stub_func_addr(func_desc_t func)
51 /* PowerPC64 specific values for the Elf64_Sym st_other field. */
52 #define STO_PPC64_LOCAL_BIT 5
53 #define STO_PPC64_LOCAL_MASK (7 << STO_PPC64_LOCAL_BIT)
54 #define PPC64_LOCAL_ENTRY_OFFSET(other) \
55 (((1 << (((other) & STO_PPC64_LOCAL_MASK) >> STO_PPC64_LOCAL_BIT)) >> 2) << 2)
57 static unsigned int local_entry_offset(const Elf64_Sym *sym)
59 /* sym->st_other indicates offset to local entry point
60 * (otherwise it will assume r12 is the address of the start
61 * of function and try to derive r2 from it). */
62 return PPC64_LOCAL_ENTRY_OFFSET(sym->st_other);
66 /* An address is address of the OPD entry, which contains address of fn. */
67 typedef struct ppc64_opd_entry func_desc_t;
69 static func_desc_t func_desc(unsigned long addr)
71 return *(struct ppc64_opd_entry *)addr;
73 static unsigned long func_addr(unsigned long addr)
75 return func_desc(addr).funcaddr;
77 static unsigned long stub_func_addr(func_desc_t func)
81 static unsigned int local_entry_offset(const Elf64_Sym *sym)
86 void *dereference_module_function_descriptor(struct module *mod, void *ptr)
88 if (ptr < (void *)mod->arch.start_opd ||
89 ptr >= (void *)mod->arch.end_opd)
92 return dereference_function_descriptor(ptr);
96 #define STUB_MAGIC 0x73747562 /* stub */
98 /* Like PPC32, we need little trampolines to do > 24-bit jumps (into
99 the kernel itself). But on PPC64, these need to be used for every
100 jump, actually, to reset r2 (TOC+0x8000). */
101 struct ppc64_stub_entry
103 /* 28 byte jump instruction sequence (7 instructions). We only
104 * need 6 instructions on ABIv2 but we always allocate 7 so
105 * so we don't have to modify the trampoline load instruction. */
107 /* Used by ftrace to identify stubs */
109 /* Data for the above code */
110 func_desc_t funcdata;
114 * PPC64 uses 24 bit jumps, but we need to jump into other modules or
115 * the kernel which may be further. So we jump to a stub.
117 * For ELFv1 we need to use this to set up the new r2 value (aka TOC
118 * pointer). For ELFv2 it's the callee's responsibility to set up the
119 * new r2, but for both we need to save the old r2.
121 * We could simply patch the new r2 value and function pointer into
122 * the stub, but it's significantly shorter to put these values at the
123 * end of the stub code, and patch the stub address (32-bits relative
124 * to the TOC ptr, r2) into the stub.
126 static u32 ppc64_stub_insns[] = {
127 PPC_RAW_ADDIS(_R11, _R2, 0),
128 PPC_RAW_ADDI(_R11, _R11, 0),
129 /* Save current r2 value in magic place on the stack. */
130 PPC_RAW_STD(_R2, _R1, R2_STACK_OFFSET),
131 PPC_RAW_LD(_R12, _R11, 32),
132 #ifdef PPC64_ELF_ABI_v1
133 /* Set up new r2 from function descriptor */
134 PPC_RAW_LD(_R2, _R11, 40),
140 /* Count how many different 24-bit relocations (different symbol,
142 static unsigned int count_relocs(const Elf64_Rela *rela, unsigned int num)
144 unsigned int i, r_info, r_addend, _count_relocs;
146 /* FIXME: Only count external ones --RR */
150 for (i = 0; i < num; i++)
151 /* Only count 24-bit relocs, others don't need stubs */
152 if (ELF64_R_TYPE(rela[i].r_info) == R_PPC_REL24 &&
153 (r_info != ELF64_R_SYM(rela[i].r_info) ||
154 r_addend != rela[i].r_addend)) {
156 r_info = ELF64_R_SYM(rela[i].r_info);
157 r_addend = rela[i].r_addend;
160 return _count_relocs;
163 static int relacmp(const void *_x, const void *_y)
165 const Elf64_Rela *x, *y;
167 y = (Elf64_Rela *)_x;
168 x = (Elf64_Rela *)_y;
170 /* Compare the entire r_info (as opposed to ELF64_R_SYM(r_info) only) to
171 * make the comparison cheaper/faster. It won't affect the sorting or
172 * the counting algorithms' performance
174 if (x->r_info < y->r_info)
176 else if (x->r_info > y->r_info)
178 else if (x->r_addend < y->r_addend)
180 else if (x->r_addend > y->r_addend)
186 /* Get size of potential trampolines required. */
187 static unsigned long get_stubs_size(const Elf64_Ehdr *hdr,
188 const Elf64_Shdr *sechdrs)
190 /* One extra reloc so it's always 0-funcaddr terminated */
191 unsigned long relocs = 1;
194 /* Every relocated section... */
195 for (i = 1; i < hdr->e_shnum; i++) {
196 if (sechdrs[i].sh_type == SHT_RELA) {
197 pr_debug("Found relocations in section %u\n", i);
198 pr_debug("Ptr: %p. Number: %Lu\n",
199 (void *)sechdrs[i].sh_addr,
200 sechdrs[i].sh_size / sizeof(Elf64_Rela));
202 /* Sort the relocation information based on a symbol and
203 * addend key. This is a stable O(n*log n) complexity
204 * alogrithm but it will reduce the complexity of
205 * count_relocs() to linear complexity O(n)
207 sort((void *)sechdrs[i].sh_addr,
208 sechdrs[i].sh_size / sizeof(Elf64_Rela),
209 sizeof(Elf64_Rela), relacmp, NULL);
211 relocs += count_relocs((void *)sechdrs[i].sh_addr,
213 / sizeof(Elf64_Rela));
217 #ifdef CONFIG_DYNAMIC_FTRACE
218 /* make the trampoline to the ftrace_caller */
220 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
221 /* an additional one for ftrace_regs_caller */
226 pr_debug("Looks like a total of %lu stubs, max\n", relocs);
227 return relocs * sizeof(struct ppc64_stub_entry);
230 /* Still needed for ELFv2, for .TOC. */
231 static void dedotify_versions(struct modversion_info *vers,
234 struct modversion_info *end;
236 for (end = (void *)vers + size; vers < end; vers++)
237 if (vers->name[0] == '.') {
238 memmove(vers->name, vers->name+1, strlen(vers->name));
243 * Undefined symbols which refer to .funcname, hack to funcname. Make .TOC.
244 * seem to be defined (value set later).
246 static void dedotify(Elf64_Sym *syms, unsigned int numsyms, char *strtab)
250 for (i = 1; i < numsyms; i++) {
251 if (syms[i].st_shndx == SHN_UNDEF) {
252 char *name = strtab + syms[i].st_name;
253 if (name[0] == '.') {
254 if (strcmp(name+1, "TOC.") == 0)
255 syms[i].st_shndx = SHN_ABS;
262 static Elf64_Sym *find_dot_toc(Elf64_Shdr *sechdrs,
264 unsigned int symindex)
266 unsigned int i, numsyms;
269 syms = (Elf64_Sym *)sechdrs[symindex].sh_addr;
270 numsyms = sechdrs[symindex].sh_size / sizeof(Elf64_Sym);
272 for (i = 1; i < numsyms; i++) {
273 if (syms[i].st_shndx == SHN_ABS
274 && strcmp(strtab + syms[i].st_name, "TOC.") == 0)
280 int module_frob_arch_sections(Elf64_Ehdr *hdr,
287 /* Find .toc and .stubs sections, symtab and strtab */
288 for (i = 1; i < hdr->e_shnum; i++) {
290 if (strcmp(secstrings + sechdrs[i].sh_name, ".stubs") == 0)
291 me->arch.stubs_section = i;
292 else if (strcmp(secstrings + sechdrs[i].sh_name, ".toc") == 0) {
293 me->arch.toc_section = i;
294 if (sechdrs[i].sh_addralign < 8)
295 sechdrs[i].sh_addralign = 8;
297 else if (strcmp(secstrings+sechdrs[i].sh_name,"__versions")==0)
298 dedotify_versions((void *)hdr + sechdrs[i].sh_offset,
301 /* We don't handle .init for the moment: rename to _init */
302 while ((p = strstr(secstrings + sechdrs[i].sh_name, ".init")))
305 if (sechdrs[i].sh_type == SHT_SYMTAB)
306 dedotify((void *)hdr + sechdrs[i].sh_offset,
307 sechdrs[i].sh_size / sizeof(Elf64_Sym),
309 + sechdrs[sechdrs[i].sh_link].sh_offset);
312 if (!me->arch.stubs_section) {
313 pr_err("%s: doesn't contain .stubs.\n", me->name);
317 /* If we don't have a .toc, just use .stubs. We need to set r2
318 to some reasonable value in case the module calls out to
319 other functions via a stub, or if a function pointer escapes
320 the module by some means. */
321 if (!me->arch.toc_section)
322 me->arch.toc_section = me->arch.stubs_section;
324 /* Override the stubs size */
325 sechdrs[me->arch.stubs_section].sh_size = get_stubs_size(hdr, sechdrs);
329 #ifdef CONFIG_MPROFILE_KERNEL
331 static u32 stub_insns[] = {
332 PPC_RAW_LD(_R12, _R13, offsetof(struct paca_struct, kernel_toc)),
333 PPC_RAW_ADDIS(_R12, _R12, 0),
334 PPC_RAW_ADDI(_R12, _R12, 0),
340 * For mprofile-kernel we use a special stub for ftrace_caller() because we
341 * can't rely on r2 containing this module's TOC when we enter the stub.
343 * That can happen if the function calling us didn't need to use the toc. In
344 * that case it won't have setup r2, and the r2 value will be either the
345 * kernel's toc, or possibly another modules toc.
347 * To deal with that this stub uses the kernel toc, which is always accessible
348 * via the paca (in r13). The target (ftrace_caller()) is responsible for
349 * saving and restoring the toc before returning.
351 static inline int create_ftrace_stub(struct ppc64_stub_entry *entry,
357 memcpy(entry->jump, stub_insns, sizeof(stub_insns));
359 /* Stub uses address relative to kernel toc (from the paca) */
360 reladdr = addr - kernel_toc_addr();
361 if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) {
362 pr_err("%s: Address of %ps out of range of kernel_toc.\n",
363 me->name, (void *)addr);
367 entry->jump[1] |= PPC_HA(reladdr);
368 entry->jump[2] |= PPC_LO(reladdr);
370 /* Eventhough we don't use funcdata in the stub, it's needed elsewhere. */
371 entry->funcdata = func_desc(addr);
372 entry->magic = STUB_MAGIC;
377 static bool is_mprofile_ftrace_call(const char *name)
379 if (!strcmp("_mcount", name))
381 #ifdef CONFIG_DYNAMIC_FTRACE
382 if (!strcmp("ftrace_caller", name))
384 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
385 if (!strcmp("ftrace_regs_caller", name))
393 static inline int create_ftrace_stub(struct ppc64_stub_entry *entry,
400 static bool is_mprofile_ftrace_call(const char *name)
407 * r2 is the TOC pointer: it actually points 0x8000 into the TOC (this gives the
408 * value maximum span in an instruction which uses a signed offset). Round down
409 * to a 256 byte boundary for the odd case where we are setting up r2 without a
412 static inline unsigned long my_r2(const Elf64_Shdr *sechdrs, struct module *me)
414 return (sechdrs[me->arch.toc_section].sh_addr & ~0xfful) + 0x8000;
417 /* Patch stub to reference function and correct r2 value. */
418 static inline int create_stub(const Elf64_Shdr *sechdrs,
419 struct ppc64_stub_entry *entry,
428 if (is_mprofile_ftrace_call(name))
429 return create_ftrace_stub(entry, addr, me);
431 for (i = 0; i < sizeof(ppc64_stub_insns) / sizeof(u32); i++) {
432 if (patch_instruction(&entry->jump[i],
433 ppc_inst(ppc64_stub_insns[i])))
437 /* Stub uses address relative to r2. */
438 reladdr = (unsigned long)entry - my_r2(sechdrs, me);
439 if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) {
440 pr_err("%s: Address %p of stub out of range of %p.\n",
441 me->name, (void *)reladdr, (void *)my_r2);
444 pr_debug("Stub %p get data from reladdr %li\n", entry, reladdr);
446 if (patch_instruction(&entry->jump[0],
447 ppc_inst(entry->jump[0] | PPC_HA(reladdr))))
450 if (patch_instruction(&entry->jump[1],
451 ppc_inst(entry->jump[1] | PPC_LO(reladdr))))
454 // func_desc_t is 8 bytes if ABIv2, else 16 bytes
455 desc = func_desc(addr);
456 for (i = 0; i < sizeof(func_desc_t) / sizeof(u32); i++) {
457 if (patch_instruction(((u32 *)&entry->funcdata) + i,
458 ppc_inst(((u32 *)(&desc))[i])))
462 if (patch_instruction(&entry->magic, ppc_inst(STUB_MAGIC)))
468 /* Create stub to jump to function described in this OPD/ptr: we need the
469 stub to set up the TOC ptr (r2) for the function. */
470 static unsigned long stub_for_addr(const Elf64_Shdr *sechdrs,
475 struct ppc64_stub_entry *stubs;
476 unsigned int i, num_stubs;
478 num_stubs = sechdrs[me->arch.stubs_section].sh_size / sizeof(*stubs);
480 /* Find this stub, or if that fails, the next avail. entry */
481 stubs = (void *)sechdrs[me->arch.stubs_section].sh_addr;
482 for (i = 0; stub_func_addr(stubs[i].funcdata); i++) {
483 if (WARN_ON(i >= num_stubs))
486 if (stub_func_addr(stubs[i].funcdata) == func_addr(addr))
487 return (unsigned long)&stubs[i];
490 if (!create_stub(sechdrs, &stubs[i], addr, me, name))
493 return (unsigned long)&stubs[i];
496 /* We expect a noop next: if it is, replace it with instruction to
498 static int restore_r2(const char *name, u32 *instruction, struct module *me)
500 u32 *prev_insn = instruction - 1;
502 if (is_mprofile_ftrace_call(name))
506 * Make sure the branch isn't a sibling call. Sibling calls aren't
507 * "link" branches and they don't return, so they don't need the r2
508 * restore afterwards.
510 if (!instr_is_relative_link_branch(ppc_inst(*prev_insn)))
513 if (*instruction != PPC_RAW_NOP()) {
514 pr_err("%s: Expected nop after call, got %08x at %pS\n",
515 me->name, *instruction, instruction);
519 /* ld r2,R2_STACK_OFFSET(r1) */
520 if (patch_instruction(instruction, ppc_inst(PPC_INST_LD_TOC)))
526 int apply_relocate_add(Elf64_Shdr *sechdrs,
528 unsigned int symindex,
533 Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr;
535 unsigned long *location;
538 pr_debug("Applying ADD relocate section %u to %u\n", relsec,
539 sechdrs[relsec].sh_info);
541 /* First time we're called, we can fix up .TOC. */
542 if (!me->arch.toc_fixed) {
543 sym = find_dot_toc(sechdrs, strtab, symindex);
544 /* It's theoretically possible that a module doesn't want a
545 * .TOC. so don't fail it just for that. */
547 sym->st_value = my_r2(sechdrs, me);
548 me->arch.toc_fixed = true;
551 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
552 /* This is where to make the change */
553 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
555 /* This is the symbol it is referring to */
556 sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
557 + ELF64_R_SYM(rela[i].r_info);
559 pr_debug("RELOC at %p: %li-type as %s (0x%lx) + %li\n",
560 location, (long)ELF64_R_TYPE(rela[i].r_info),
561 strtab + sym->st_name, (unsigned long)sym->st_value,
562 (long)rela[i].r_addend);
564 /* `Everything is relative'. */
565 value = sym->st_value + rela[i].r_addend;
567 switch (ELF64_R_TYPE(rela[i].r_info)) {
570 *(u32 *)location = value;
575 *(unsigned long *)location = value;
579 *(unsigned long *)location = my_r2(sechdrs, me);
583 /* Subtract TOC pointer */
584 value -= my_r2(sechdrs, me);
585 if (value + 0x8000 > 0xffff) {
586 pr_err("%s: bad TOC16 relocation (0x%lx)\n",
590 *((uint16_t *) location)
591 = (*((uint16_t *) location) & ~0xffff)
595 case R_PPC64_TOC16_LO:
596 /* Subtract TOC pointer */
597 value -= my_r2(sechdrs, me);
598 *((uint16_t *) location)
599 = (*((uint16_t *) location) & ~0xffff)
603 case R_PPC64_TOC16_DS:
604 /* Subtract TOC pointer */
605 value -= my_r2(sechdrs, me);
606 if ((value & 3) != 0 || value + 0x8000 > 0xffff) {
607 pr_err("%s: bad TOC16_DS relocation (0x%lx)\n",
611 *((uint16_t *) location)
612 = (*((uint16_t *) location) & ~0xfffc)
616 case R_PPC64_TOC16_LO_DS:
617 /* Subtract TOC pointer */
618 value -= my_r2(sechdrs, me);
619 if ((value & 3) != 0) {
620 pr_err("%s: bad TOC16_LO_DS relocation (0x%lx)\n",
624 *((uint16_t *) location)
625 = (*((uint16_t *) location) & ~0xfffc)
629 case R_PPC64_TOC16_HA:
630 /* Subtract TOC pointer */
631 value -= my_r2(sechdrs, me);
632 value = ((value + 0x8000) >> 16);
633 *((uint16_t *) location)
634 = (*((uint16_t *) location) & ~0xffff)
639 /* FIXME: Handle weak symbols here --RR */
640 if (sym->st_shndx == SHN_UNDEF ||
641 sym->st_shndx == SHN_LIVEPATCH) {
642 /* External: go via stub */
643 value = stub_for_addr(sechdrs, value, me,
644 strtab + sym->st_name);
647 if (!restore_r2(strtab + sym->st_name,
648 (u32 *)location + 1, me))
651 value += local_entry_offset(sym);
653 /* Convert value to relative */
654 value -= (unsigned long)location;
655 if (value + 0x2000000 > 0x3ffffff || (value & 3) != 0){
656 pr_err("%s: REL24 %li out of range!\n",
657 me->name, (long int)value);
661 /* Only replace bits 2 through 26 */
662 value = (*(uint32_t *)location & ~0x03fffffc)
663 | (value & 0x03fffffc);
665 if (patch_instruction((u32 *)location, ppc_inst(value)))
671 /* 64 bits relative (used by features fixups) */
672 *location = value - (unsigned long)location;
676 /* 32 bits relative (used by relative exception tables) */
677 /* Convert value to relative */
678 value -= (unsigned long)location;
679 if (value + 0x80000000 > 0xffffffff) {
680 pr_err("%s: REL32 %li out of range!\n",
681 me->name, (long int)value);
684 *(u32 *)location = value;
687 case R_PPC64_TOCSAVE:
689 * Marker reloc indicates we don't have to save r2.
690 * That would only save us one instruction, so ignore
697 * Optimize ELFv2 large code model entry point if
698 * the TOC is within 2GB range of current location.
700 value = my_r2(sechdrs, me) - (unsigned long)location;
701 if (value + 0x80008000 > 0xffffffff)
704 * Check for the large code model prolog sequence:
708 if ((((uint32_t *)location)[0] & ~0xfffc) != PPC_RAW_LD(_R2, _R12, 0))
710 if (((uint32_t *)location)[1] != PPC_RAW_ADD(_R2, _R2, _R12))
713 * If found, replace it with:
714 * addis r2, r12, (.TOC.-func)@ha
715 * addi r2, r2, (.TOC.-func)@l
717 ((uint32_t *)location)[0] = PPC_RAW_ADDIS(_R2, _R12, PPC_HA(value));
718 ((uint32_t *)location)[1] = PPC_RAW_ADDI(_R2, _R2, PPC_LO(value));
721 case R_PPC64_REL16_HA:
722 /* Subtract location pointer */
723 value -= (unsigned long)location;
724 value = ((value + 0x8000) >> 16);
725 *((uint16_t *) location)
726 = (*((uint16_t *) location) & ~0xffff)
730 case R_PPC64_REL16_LO:
731 /* Subtract location pointer */
732 value -= (unsigned long)location;
733 *((uint16_t *) location)
734 = (*((uint16_t *) location) & ~0xffff)
739 pr_err("%s: Unknown ADD relocation: %lu\n",
741 (unsigned long)ELF64_R_TYPE(rela[i].r_info));
749 #ifdef CONFIG_DYNAMIC_FTRACE
750 int module_trampoline_target(struct module *mod, unsigned long addr,
751 unsigned long *target)
753 struct ppc64_stub_entry *stub;
754 func_desc_t funcdata;
757 if (!within_module_core(addr, mod)) {
758 pr_err("%s: stub %lx not in module %s\n", __func__, addr, mod->name);
762 stub = (struct ppc64_stub_entry *)addr;
764 if (copy_from_kernel_nofault(&magic, &stub->magic,
766 pr_err("%s: fault reading magic for stub %lx for %s\n", __func__, addr, mod->name);
770 if (magic != STUB_MAGIC) {
771 pr_err("%s: bad magic for stub %lx for %s\n", __func__, addr, mod->name);
775 if (copy_from_kernel_nofault(&funcdata, &stub->funcdata,
777 pr_err("%s: fault reading funcdata for stub %lx for %s\n", __func__, addr, mod->name);
781 *target = stub_func_addr(funcdata);
786 int module_finalize_ftrace(struct module *mod, const Elf_Shdr *sechdrs)
788 mod->arch.tramp = stub_for_addr(sechdrs,
789 (unsigned long)ftrace_caller,
792 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
793 mod->arch.tramp_regs = stub_for_addr(sechdrs,
794 (unsigned long)ftrace_regs_caller,
796 "ftrace_regs_caller");
797 if (!mod->arch.tramp_regs)
801 if (!mod->arch.tramp)