1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* Kernel module help for PPC64. 3 Copyright (C) 2001, 2003 Rusty Russell IBM Corporation. 4 5 */ 6 7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 8 9 #include <linux/module.h> 10 #include <linux/elf.h> 11 #include <linux/moduleloader.h> 12 #include <linux/err.h> 13 #include <linux/vmalloc.h> 14 #include <linux/ftrace.h> 15 #include <linux/bug.h> 16 #include <linux/uaccess.h> 17 #include <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> 23 24 /* FIXME: We don't do .init separately. To do this, we'd need to have 25 a separate r2 value in the init and core section, and stub between 26 them, too. 27 28 Using a magic allocator which places modules within 32MB solves 29 this, and makes other things simpler. Anton? 30 --RR. */ 31 32 #ifdef PPC64_ELF_ABI_v2 33 34 /* An address is simply the address of the function. */ 35 typedef unsigned long func_desc_t; 36 37 static func_desc_t func_desc(unsigned long addr) 38 { 39 return addr; 40 } 41 static unsigned long func_addr(unsigned long addr) 42 { 43 return addr; 44 } 45 static unsigned long stub_func_addr(func_desc_t func) 46 { 47 return func; 48 } 49 50 /* PowerPC64 specific values for the Elf64_Sym st_other field. */ 51 #define STO_PPC64_LOCAL_BIT 5 52 #define STO_PPC64_LOCAL_MASK (7 << STO_PPC64_LOCAL_BIT) 53 #define PPC64_LOCAL_ENTRY_OFFSET(other) \ 54 (((1 << (((other) & STO_PPC64_LOCAL_MASK) >> STO_PPC64_LOCAL_BIT)) >> 2) << 2) 55 56 static unsigned int local_entry_offset(const Elf64_Sym *sym) 57 { 58 /* sym->st_other indicates offset to local entry point 59 * (otherwise it will assume r12 is the address of the start 60 * of function and try to derive r2 from it). */ 61 return PPC64_LOCAL_ENTRY_OFFSET(sym->st_other); 62 } 63 #else 64 65 /* An address is address of the OPD entry, which contains address of fn. */ 66 typedef struct ppc64_opd_entry func_desc_t; 67 68 static func_desc_t func_desc(unsigned long addr) 69 { 70 return *(struct ppc64_opd_entry *)addr; 71 } 72 static unsigned long func_addr(unsigned long addr) 73 { 74 return func_desc(addr).funcaddr; 75 } 76 static unsigned long stub_func_addr(func_desc_t func) 77 { 78 return func.funcaddr; 79 } 80 static unsigned int local_entry_offset(const Elf64_Sym *sym) 81 { 82 return 0; 83 } 84 85 void *dereference_module_function_descriptor(struct module *mod, void *ptr) 86 { 87 if (ptr < (void *)mod->arch.start_opd || 88 ptr >= (void *)mod->arch.end_opd) 89 return ptr; 90 91 return dereference_function_descriptor(ptr); 92 } 93 #endif 94 95 #define STUB_MAGIC 0x73747562 /* stub */ 96 97 /* Like PPC32, we need little trampolines to do > 24-bit jumps (into 98 the kernel itself). But on PPC64, these need to be used for every 99 jump, actually, to reset r2 (TOC+0x8000). */ 100 struct ppc64_stub_entry 101 { 102 /* 28 byte jump instruction sequence (7 instructions). We only 103 * need 6 instructions on ABIv2 but we always allocate 7 so 104 * so we don't have to modify the trampoline load instruction. */ 105 u32 jump[7]; 106 /* Used by ftrace to identify stubs */ 107 u32 magic; 108 /* Data for the above code */ 109 func_desc_t funcdata; 110 }; 111 112 /* 113 * PPC64 uses 24 bit jumps, but we need to jump into other modules or 114 * the kernel which may be further. So we jump to a stub. 115 * 116 * For ELFv1 we need to use this to set up the new r2 value (aka TOC 117 * pointer). For ELFv2 it's the callee's responsibility to set up the 118 * new r2, but for both we need to save the old r2. 119 * 120 * We could simply patch the new r2 value and function pointer into 121 * the stub, but it's significantly shorter to put these values at the 122 * end of the stub code, and patch the stub address (32-bits relative 123 * to the TOC ptr, r2) into the stub. 124 * 125 * addis r11,r2, <high> 126 * addi r11,r11, <low> 127 * std r2,R2_STACK_OFFSET(r1) 128 * ld r12,32(r11) 129 * ld r2,40(r11) 130 * mtctr r12 131 * bctr 132 */ 133 static u32 ppc64_stub_insns[] = { 134 PPC_INST_ADDIS | __PPC_RT(R11) | __PPC_RA(R2), 135 PPC_INST_ADDI | __PPC_RT(R11) | __PPC_RA(R11), 136 /* Save current r2 value in magic place on the stack. */ 137 PPC_INST_STD | __PPC_RS(R2) | __PPC_RA(R1) | R2_STACK_OFFSET, 138 PPC_INST_LD | __PPC_RT(R12) | __PPC_RA(R11) | 32, 139 #ifdef PPC64_ELF_ABI_v1 140 /* Set up new r2 from function descriptor */ 141 PPC_INST_LD | __PPC_RT(R2) | __PPC_RA(R11) | 40, 142 #endif 143 PPC_INST_MTCTR | __PPC_RS(R12), 144 PPC_INST_BCTR, 145 }; 146 147 #ifdef CONFIG_DYNAMIC_FTRACE 148 int module_trampoline_target(struct module *mod, unsigned long addr, 149 unsigned long *target) 150 { 151 struct ppc64_stub_entry *stub; 152 func_desc_t funcdata; 153 u32 magic; 154 155 if (!within_module_core(addr, mod)) { 156 pr_err("%s: stub %lx not in module %s\n", __func__, addr, mod->name); 157 return -EFAULT; 158 } 159 160 stub = (struct ppc64_stub_entry *)addr; 161 162 if (probe_kernel_read(&magic, &stub->magic, sizeof(magic))) { 163 pr_err("%s: fault reading magic for stub %lx for %s\n", __func__, addr, mod->name); 164 return -EFAULT; 165 } 166 167 if (magic != STUB_MAGIC) { 168 pr_err("%s: bad magic for stub %lx for %s\n", __func__, addr, mod->name); 169 return -EFAULT; 170 } 171 172 if (probe_kernel_read(&funcdata, &stub->funcdata, sizeof(funcdata))) { 173 pr_err("%s: fault reading funcdata for stub %lx for %s\n", __func__, addr, mod->name); 174 return -EFAULT; 175 } 176 177 *target = stub_func_addr(funcdata); 178 179 return 0; 180 } 181 #endif 182 183 /* Count how many different 24-bit relocations (different symbol, 184 different addend) */ 185 static unsigned int count_relocs(const Elf64_Rela *rela, unsigned int num) 186 { 187 unsigned int i, r_info, r_addend, _count_relocs; 188 189 /* FIXME: Only count external ones --RR */ 190 _count_relocs = 0; 191 r_info = 0; 192 r_addend = 0; 193 for (i = 0; i < num; i++) 194 /* Only count 24-bit relocs, others don't need stubs */ 195 if (ELF64_R_TYPE(rela[i].r_info) == R_PPC_REL24 && 196 (r_info != ELF64_R_SYM(rela[i].r_info) || 197 r_addend != rela[i].r_addend)) { 198 _count_relocs++; 199 r_info = ELF64_R_SYM(rela[i].r_info); 200 r_addend = rela[i].r_addend; 201 } 202 203 return _count_relocs; 204 } 205 206 static int relacmp(const void *_x, const void *_y) 207 { 208 const Elf64_Rela *x, *y; 209 210 y = (Elf64_Rela *)_x; 211 x = (Elf64_Rela *)_y; 212 213 /* Compare the entire r_info (as opposed to ELF64_R_SYM(r_info) only) to 214 * make the comparison cheaper/faster. It won't affect the sorting or 215 * the counting algorithms' performance 216 */ 217 if (x->r_info < y->r_info) 218 return -1; 219 else if (x->r_info > y->r_info) 220 return 1; 221 else if (x->r_addend < y->r_addend) 222 return -1; 223 else if (x->r_addend > y->r_addend) 224 return 1; 225 else 226 return 0; 227 } 228 229 /* Get size of potential trampolines required. */ 230 static unsigned long get_stubs_size(const Elf64_Ehdr *hdr, 231 const Elf64_Shdr *sechdrs) 232 { 233 /* One extra reloc so it's always 0-funcaddr terminated */ 234 unsigned long relocs = 1; 235 unsigned i; 236 237 /* Every relocated section... */ 238 for (i = 1; i < hdr->e_shnum; i++) { 239 if (sechdrs[i].sh_type == SHT_RELA) { 240 pr_debug("Found relocations in section %u\n", i); 241 pr_debug("Ptr: %p. Number: %Lu\n", 242 (void *)sechdrs[i].sh_addr, 243 sechdrs[i].sh_size / sizeof(Elf64_Rela)); 244 245 /* Sort the relocation information based on a symbol and 246 * addend key. This is a stable O(n*log n) complexity 247 * alogrithm but it will reduce the complexity of 248 * count_relocs() to linear complexity O(n) 249 */ 250 sort((void *)sechdrs[i].sh_addr, 251 sechdrs[i].sh_size / sizeof(Elf64_Rela), 252 sizeof(Elf64_Rela), relacmp, NULL); 253 254 relocs += count_relocs((void *)sechdrs[i].sh_addr, 255 sechdrs[i].sh_size 256 / sizeof(Elf64_Rela)); 257 } 258 } 259 260 #ifdef CONFIG_DYNAMIC_FTRACE 261 /* make the trampoline to the ftrace_caller */ 262 relocs++; 263 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS 264 /* an additional one for ftrace_regs_caller */ 265 relocs++; 266 #endif 267 #endif 268 269 pr_debug("Looks like a total of %lu stubs, max\n", relocs); 270 return relocs * sizeof(struct ppc64_stub_entry); 271 } 272 273 /* Still needed for ELFv2, for .TOC. */ 274 static void dedotify_versions(struct modversion_info *vers, 275 unsigned long size) 276 { 277 struct modversion_info *end; 278 279 for (end = (void *)vers + size; vers < end; vers++) 280 if (vers->name[0] == '.') { 281 memmove(vers->name, vers->name+1, strlen(vers->name)); 282 } 283 } 284 285 /* 286 * Undefined symbols which refer to .funcname, hack to funcname. Make .TOC. 287 * seem to be defined (value set later). 288 */ 289 static void dedotify(Elf64_Sym *syms, unsigned int numsyms, char *strtab) 290 { 291 unsigned int i; 292 293 for (i = 1; i < numsyms; i++) { 294 if (syms[i].st_shndx == SHN_UNDEF) { 295 char *name = strtab + syms[i].st_name; 296 if (name[0] == '.') { 297 if (strcmp(name+1, "TOC.") == 0) 298 syms[i].st_shndx = SHN_ABS; 299 syms[i].st_name++; 300 } 301 } 302 } 303 } 304 305 static Elf64_Sym *find_dot_toc(Elf64_Shdr *sechdrs, 306 const char *strtab, 307 unsigned int symindex) 308 { 309 unsigned int i, numsyms; 310 Elf64_Sym *syms; 311 312 syms = (Elf64_Sym *)sechdrs[symindex].sh_addr; 313 numsyms = sechdrs[symindex].sh_size / sizeof(Elf64_Sym); 314 315 for (i = 1; i < numsyms; i++) { 316 if (syms[i].st_shndx == SHN_ABS 317 && strcmp(strtab + syms[i].st_name, "TOC.") == 0) 318 return &syms[i]; 319 } 320 return NULL; 321 } 322 323 int module_frob_arch_sections(Elf64_Ehdr *hdr, 324 Elf64_Shdr *sechdrs, 325 char *secstrings, 326 struct module *me) 327 { 328 unsigned int i; 329 330 /* Find .toc and .stubs sections, symtab and strtab */ 331 for (i = 1; i < hdr->e_shnum; i++) { 332 char *p; 333 if (strcmp(secstrings + sechdrs[i].sh_name, ".stubs") == 0) 334 me->arch.stubs_section = i; 335 else if (strcmp(secstrings + sechdrs[i].sh_name, ".toc") == 0) { 336 me->arch.toc_section = i; 337 if (sechdrs[i].sh_addralign < 8) 338 sechdrs[i].sh_addralign = 8; 339 } 340 else if (strcmp(secstrings+sechdrs[i].sh_name,"__versions")==0) 341 dedotify_versions((void *)hdr + sechdrs[i].sh_offset, 342 sechdrs[i].sh_size); 343 344 /* We don't handle .init for the moment: rename to _init */ 345 while ((p = strstr(secstrings + sechdrs[i].sh_name, ".init"))) 346 p[0] = '_'; 347 348 if (sechdrs[i].sh_type == SHT_SYMTAB) 349 dedotify((void *)hdr + sechdrs[i].sh_offset, 350 sechdrs[i].sh_size / sizeof(Elf64_Sym), 351 (void *)hdr 352 + sechdrs[sechdrs[i].sh_link].sh_offset); 353 } 354 355 if (!me->arch.stubs_section) { 356 pr_err("%s: doesn't contain .stubs.\n", me->name); 357 return -ENOEXEC; 358 } 359 360 /* If we don't have a .toc, just use .stubs. We need to set r2 361 to some reasonable value in case the module calls out to 362 other functions via a stub, or if a function pointer escapes 363 the module by some means. */ 364 if (!me->arch.toc_section) 365 me->arch.toc_section = me->arch.stubs_section; 366 367 /* Override the stubs size */ 368 sechdrs[me->arch.stubs_section].sh_size = get_stubs_size(hdr, sechdrs); 369 return 0; 370 } 371 372 /* 373 * r2 is the TOC pointer: it actually points 0x8000 into the TOC (this gives the 374 * value maximum span in an instruction which uses a signed offset). Round down 375 * to a 256 byte boundary for the odd case where we are setting up r2 without a 376 * .toc section. 377 */ 378 static inline unsigned long my_r2(const Elf64_Shdr *sechdrs, struct module *me) 379 { 380 return (sechdrs[me->arch.toc_section].sh_addr & ~0xfful) + 0x8000; 381 } 382 383 /* Patch stub to reference function and correct r2 value. */ 384 static inline int create_stub(const Elf64_Shdr *sechdrs, 385 struct ppc64_stub_entry *entry, 386 unsigned long addr, 387 struct module *me) 388 { 389 long reladdr; 390 391 memcpy(entry->jump, ppc64_stub_insns, sizeof(ppc64_stub_insns)); 392 393 /* Stub uses address relative to r2. */ 394 reladdr = (unsigned long)entry - my_r2(sechdrs, me); 395 if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) { 396 pr_err("%s: Address %p of stub out of range of %p.\n", 397 me->name, (void *)reladdr, (void *)my_r2); 398 return 0; 399 } 400 pr_debug("Stub %p get data from reladdr %li\n", entry, reladdr); 401 402 entry->jump[0] |= PPC_HA(reladdr); 403 entry->jump[1] |= PPC_LO(reladdr); 404 entry->funcdata = func_desc(addr); 405 entry->magic = STUB_MAGIC; 406 407 return 1; 408 } 409 410 /* Create stub to jump to function described in this OPD/ptr: we need the 411 stub to set up the TOC ptr (r2) for the function. */ 412 static unsigned long stub_for_addr(const Elf64_Shdr *sechdrs, 413 unsigned long addr, 414 struct module *me) 415 { 416 struct ppc64_stub_entry *stubs; 417 unsigned int i, num_stubs; 418 419 num_stubs = sechdrs[me->arch.stubs_section].sh_size / sizeof(*stubs); 420 421 /* Find this stub, or if that fails, the next avail. entry */ 422 stubs = (void *)sechdrs[me->arch.stubs_section].sh_addr; 423 for (i = 0; stub_func_addr(stubs[i].funcdata); i++) { 424 if (WARN_ON(i >= num_stubs)) 425 return 0; 426 427 if (stub_func_addr(stubs[i].funcdata) == func_addr(addr)) 428 return (unsigned long)&stubs[i]; 429 } 430 431 if (!create_stub(sechdrs, &stubs[i], addr, me)) 432 return 0; 433 434 return (unsigned long)&stubs[i]; 435 } 436 437 #ifdef CONFIG_MPROFILE_KERNEL 438 static bool is_mprofile_mcount_callsite(const char *name, u32 *instruction) 439 { 440 if (strcmp("_mcount", name)) 441 return false; 442 443 /* 444 * Check if this is one of the -mprofile-kernel sequences. 445 */ 446 if (instruction[-1] == PPC_INST_STD_LR && 447 instruction[-2] == PPC_INST_MFLR) 448 return true; 449 450 if (instruction[-1] == PPC_INST_MFLR) 451 return true; 452 453 return false; 454 } 455 456 /* 457 * In case of _mcount calls, do not save the current callee's TOC (in r2) into 458 * the original caller's stack frame. If we did we would clobber the saved TOC 459 * value of the original caller. 460 */ 461 static void squash_toc_save_inst(const char *name, unsigned long addr) 462 { 463 struct ppc64_stub_entry *stub = (struct ppc64_stub_entry *)addr; 464 465 /* Only for calls to _mcount */ 466 if (strcmp("_mcount", name) != 0) 467 return; 468 469 stub->jump[2] = PPC_INST_NOP; 470 } 471 #else 472 static void squash_toc_save_inst(const char *name, unsigned long addr) { } 473 474 static bool is_mprofile_mcount_callsite(const char *name, u32 *instruction) 475 { 476 return false; 477 } 478 #endif 479 480 /* We expect a noop next: if it is, replace it with instruction to 481 restore r2. */ 482 static int restore_r2(const char *name, u32 *instruction, struct module *me) 483 { 484 u32 *prev_insn = instruction - 1; 485 486 if (is_mprofile_mcount_callsite(name, prev_insn)) 487 return 1; 488 489 /* 490 * Make sure the branch isn't a sibling call. Sibling calls aren't 491 * "link" branches and they don't return, so they don't need the r2 492 * restore afterwards. 493 */ 494 if (!instr_is_relative_link_branch(*prev_insn)) 495 return 1; 496 497 if (*instruction != PPC_INST_NOP) { 498 pr_err("%s: Expected nop after call, got %08x at %pS\n", 499 me->name, *instruction, instruction); 500 return 0; 501 } 502 /* ld r2,R2_STACK_OFFSET(r1) */ 503 *instruction = PPC_INST_LD_TOC; 504 return 1; 505 } 506 507 int apply_relocate_add(Elf64_Shdr *sechdrs, 508 const char *strtab, 509 unsigned int symindex, 510 unsigned int relsec, 511 struct module *me) 512 { 513 unsigned int i; 514 Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr; 515 Elf64_Sym *sym; 516 unsigned long *location; 517 unsigned long value; 518 519 pr_debug("Applying ADD relocate section %u to %u\n", relsec, 520 sechdrs[relsec].sh_info); 521 522 /* First time we're called, we can fix up .TOC. */ 523 if (!me->arch.toc_fixed) { 524 sym = find_dot_toc(sechdrs, strtab, symindex); 525 /* It's theoretically possible that a module doesn't want a 526 * .TOC. so don't fail it just for that. */ 527 if (sym) 528 sym->st_value = my_r2(sechdrs, me); 529 me->arch.toc_fixed = true; 530 } 531 532 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) { 533 /* This is where to make the change */ 534 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr 535 + rela[i].r_offset; 536 /* This is the symbol it is referring to */ 537 sym = (Elf64_Sym *)sechdrs[symindex].sh_addr 538 + ELF64_R_SYM(rela[i].r_info); 539 540 pr_debug("RELOC at %p: %li-type as %s (0x%lx) + %li\n", 541 location, (long)ELF64_R_TYPE(rela[i].r_info), 542 strtab + sym->st_name, (unsigned long)sym->st_value, 543 (long)rela[i].r_addend); 544 545 /* `Everything is relative'. */ 546 value = sym->st_value + rela[i].r_addend; 547 548 switch (ELF64_R_TYPE(rela[i].r_info)) { 549 case R_PPC64_ADDR32: 550 /* Simply set it */ 551 *(u32 *)location = value; 552 break; 553 554 case R_PPC64_ADDR64: 555 /* Simply set it */ 556 *(unsigned long *)location = value; 557 break; 558 559 case R_PPC64_TOC: 560 *(unsigned long *)location = my_r2(sechdrs, me); 561 break; 562 563 case R_PPC64_TOC16: 564 /* Subtract TOC pointer */ 565 value -= my_r2(sechdrs, me); 566 if (value + 0x8000 > 0xffff) { 567 pr_err("%s: bad TOC16 relocation (0x%lx)\n", 568 me->name, value); 569 return -ENOEXEC; 570 } 571 *((uint16_t *) location) 572 = (*((uint16_t *) location) & ~0xffff) 573 | (value & 0xffff); 574 break; 575 576 case R_PPC64_TOC16_LO: 577 /* Subtract TOC pointer */ 578 value -= my_r2(sechdrs, me); 579 *((uint16_t *) location) 580 = (*((uint16_t *) location) & ~0xffff) 581 | (value & 0xffff); 582 break; 583 584 case R_PPC64_TOC16_DS: 585 /* Subtract TOC pointer */ 586 value -= my_r2(sechdrs, me); 587 if ((value & 3) != 0 || value + 0x8000 > 0xffff) { 588 pr_err("%s: bad TOC16_DS relocation (0x%lx)\n", 589 me->name, value); 590 return -ENOEXEC; 591 } 592 *((uint16_t *) location) 593 = (*((uint16_t *) location) & ~0xfffc) 594 | (value & 0xfffc); 595 break; 596 597 case R_PPC64_TOC16_LO_DS: 598 /* Subtract TOC pointer */ 599 value -= my_r2(sechdrs, me); 600 if ((value & 3) != 0) { 601 pr_err("%s: bad TOC16_LO_DS relocation (0x%lx)\n", 602 me->name, value); 603 return -ENOEXEC; 604 } 605 *((uint16_t *) location) 606 = (*((uint16_t *) location) & ~0xfffc) 607 | (value & 0xfffc); 608 break; 609 610 case R_PPC64_TOC16_HA: 611 /* Subtract TOC pointer */ 612 value -= my_r2(sechdrs, me); 613 value = ((value + 0x8000) >> 16); 614 *((uint16_t *) location) 615 = (*((uint16_t *) location) & ~0xffff) 616 | (value & 0xffff); 617 break; 618 619 case R_PPC_REL24: 620 /* FIXME: Handle weak symbols here --RR */ 621 if (sym->st_shndx == SHN_UNDEF || 622 sym->st_shndx == SHN_LIVEPATCH) { 623 /* External: go via stub */ 624 value = stub_for_addr(sechdrs, value, me); 625 if (!value) 626 return -ENOENT; 627 if (!restore_r2(strtab + sym->st_name, 628 (u32 *)location + 1, me)) 629 return -ENOEXEC; 630 631 squash_toc_save_inst(strtab + sym->st_name, value); 632 } else 633 value += local_entry_offset(sym); 634 635 /* Convert value to relative */ 636 value -= (unsigned long)location; 637 if (value + 0x2000000 > 0x3ffffff || (value & 3) != 0){ 638 pr_err("%s: REL24 %li out of range!\n", 639 me->name, (long int)value); 640 return -ENOEXEC; 641 } 642 643 /* Only replace bits 2 through 26 */ 644 *(uint32_t *)location 645 = (*(uint32_t *)location & ~0x03fffffc) 646 | (value & 0x03fffffc); 647 break; 648 649 case R_PPC64_REL64: 650 /* 64 bits relative (used by features fixups) */ 651 *location = value - (unsigned long)location; 652 break; 653 654 case R_PPC64_REL32: 655 /* 32 bits relative (used by relative exception tables) */ 656 /* Convert value to relative */ 657 value -= (unsigned long)location; 658 if (value + 0x80000000 > 0xffffffff) { 659 pr_err("%s: REL32 %li out of range!\n", 660 me->name, (long int)value); 661 return -ENOEXEC; 662 } 663 *(u32 *)location = value; 664 break; 665 666 case R_PPC64_TOCSAVE: 667 /* 668 * Marker reloc indicates we don't have to save r2. 669 * That would only save us one instruction, so ignore 670 * it. 671 */ 672 break; 673 674 case R_PPC64_ENTRY: 675 /* 676 * Optimize ELFv2 large code model entry point if 677 * the TOC is within 2GB range of current location. 678 */ 679 value = my_r2(sechdrs, me) - (unsigned long)location; 680 if (value + 0x80008000 > 0xffffffff) 681 break; 682 /* 683 * Check for the large code model prolog sequence: 684 * ld r2, ...(r12) 685 * add r2, r2, r12 686 */ 687 if ((((uint32_t *)location)[0] & ~0xfffc) != 688 (PPC_INST_LD | __PPC_RT(R2) | __PPC_RA(R12))) 689 break; 690 if (((uint32_t *)location)[1] != 691 (PPC_INST_ADD | __PPC_RT(R2) | __PPC_RA(R2) | __PPC_RB(R12))) 692 break; 693 /* 694 * If found, replace it with: 695 * addis r2, r12, (.TOC.-func)@ha 696 * addi r2, r2, (.TOC.-func)@l 697 */ 698 ((uint32_t *)location)[0] = PPC_INST_ADDIS | __PPC_RT(R2) | 699 __PPC_RA(R12) | PPC_HA(value); 700 ((uint32_t *)location)[1] = PPC_INST_ADDI | __PPC_RT(R2) | 701 __PPC_RA(R2) | PPC_LO(value); 702 break; 703 704 case R_PPC64_REL16_HA: 705 /* Subtract location pointer */ 706 value -= (unsigned long)location; 707 value = ((value + 0x8000) >> 16); 708 *((uint16_t *) location) 709 = (*((uint16_t *) location) & ~0xffff) 710 | (value & 0xffff); 711 break; 712 713 case R_PPC64_REL16_LO: 714 /* Subtract location pointer */ 715 value -= (unsigned long)location; 716 *((uint16_t *) location) 717 = (*((uint16_t *) location) & ~0xffff) 718 | (value & 0xffff); 719 break; 720 721 default: 722 pr_err("%s: Unknown ADD relocation: %lu\n", 723 me->name, 724 (unsigned long)ELF64_R_TYPE(rela[i].r_info)); 725 return -ENOEXEC; 726 } 727 } 728 729 return 0; 730 } 731 732 #ifdef CONFIG_DYNAMIC_FTRACE 733 734 #ifdef CONFIG_MPROFILE_KERNEL 735 736 #define PACATOC offsetof(struct paca_struct, kernel_toc) 737 738 /* 739 * For mprofile-kernel we use a special stub for ftrace_caller() because we 740 * can't rely on r2 containing this module's TOC when we enter the stub. 741 * 742 * That can happen if the function calling us didn't need to use the toc. In 743 * that case it won't have setup r2, and the r2 value will be either the 744 * kernel's toc, or possibly another modules toc. 745 * 746 * To deal with that this stub uses the kernel toc, which is always accessible 747 * via the paca (in r13). The target (ftrace_caller()) is responsible for 748 * saving and restoring the toc before returning. 749 */ 750 static unsigned long create_ftrace_stub(const Elf64_Shdr *sechdrs, 751 struct module *me, unsigned long addr) 752 { 753 struct ppc64_stub_entry *entry; 754 unsigned int i, num_stubs; 755 /* 756 * ld r12,PACATOC(r13) 757 * addis r12,r12,<high> 758 * addi r12,r12,<low> 759 * mtctr r12 760 * bctr 761 */ 762 static u32 stub_insns[] = { 763 PPC_INST_LD | __PPC_RT(R12) | __PPC_RA(R13) | PACATOC, 764 PPC_INST_ADDIS | __PPC_RT(R12) | __PPC_RA(R12), 765 PPC_INST_ADDI | __PPC_RT(R12) | __PPC_RA(R12), 766 PPC_INST_MTCTR | __PPC_RS(R12), 767 PPC_INST_BCTR, 768 }; 769 long reladdr; 770 771 num_stubs = sechdrs[me->arch.stubs_section].sh_size / sizeof(*entry); 772 773 /* Find the next available stub entry */ 774 entry = (void *)sechdrs[me->arch.stubs_section].sh_addr; 775 for (i = 0; i < num_stubs && stub_func_addr(entry->funcdata); i++, entry++); 776 777 if (i >= num_stubs) { 778 pr_err("%s: Unable to find a free slot for ftrace stub.\n", me->name); 779 return 0; 780 } 781 782 memcpy(entry->jump, stub_insns, sizeof(stub_insns)); 783 784 /* Stub uses address relative to kernel toc (from the paca) */ 785 reladdr = addr - kernel_toc_addr(); 786 if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) { 787 pr_err("%s: Address of %ps out of range of kernel_toc.\n", 788 me->name, (void *)addr); 789 return 0; 790 } 791 792 entry->jump[1] |= PPC_HA(reladdr); 793 entry->jump[2] |= PPC_LO(reladdr); 794 795 /* Eventhough we don't use funcdata in the stub, it's needed elsewhere. */ 796 entry->funcdata = func_desc(addr); 797 entry->magic = STUB_MAGIC; 798 799 return (unsigned long)entry; 800 } 801 #else 802 static unsigned long create_ftrace_stub(const Elf64_Shdr *sechdrs, 803 struct module *me, unsigned long addr) 804 { 805 return stub_for_addr(sechdrs, addr, me); 806 } 807 #endif 808 809 int module_finalize_ftrace(struct module *mod, const Elf_Shdr *sechdrs) 810 { 811 mod->arch.tramp = create_ftrace_stub(sechdrs, mod, 812 (unsigned long)ftrace_caller); 813 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS 814 mod->arch.tramp_regs = create_ftrace_stub(sechdrs, mod, 815 (unsigned long)ftrace_regs_caller); 816 if (!mod->arch.tramp_regs) 817 return -ENOENT; 818 #endif 819 820 if (!mod->arch.tramp) 821 return -ENOENT; 822 823 return 0; 824 } 825 #endif 826