| 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 <linux/kernel.h> |
| 18 | #include <asm/module.h> |
| 19 | #include <asm/firmware.h> |
| 20 | #include <asm/text-patching.h> |
| 21 | #include <linux/sort.h> |
| 22 | #include <asm/setup.h> |
| 23 | #include <asm/sections.h> |
| 24 | #include <asm/inst.h> |
| 25 | |
| 26 | /* FIXME: We don't do .init separately. To do this, we'd need to have |
| 27 | a separate r2 value in the init and core section, and stub between |
| 28 | them, too. |
| 29 | |
| 30 | Using a magic allocator which places modules within 32MB solves |
| 31 | this, and makes other things simpler. Anton? |
| 32 | --RR. */ |
| 33 | |
| 34 | bool module_elf_check_arch(Elf_Ehdr *hdr) |
| 35 | { |
| 36 | unsigned long abi_level = hdr->e_flags & 0x3; |
| 37 | |
| 38 | if (IS_ENABLED(CONFIG_PPC64_ELF_ABI_V2)) |
| 39 | return abi_level == 2; |
| 40 | else |
| 41 | return abi_level < 2; |
| 42 | } |
| 43 | |
| 44 | #ifdef CONFIG_PPC64_ELF_ABI_V2 |
| 45 | |
| 46 | static func_desc_t func_desc(unsigned long addr) |
| 47 | { |
| 48 | func_desc_t desc = { |
| 49 | .addr = addr, |
| 50 | }; |
| 51 | |
| 52 | return desc; |
| 53 | } |
| 54 | |
| 55 | /* PowerPC64 specific values for the Elf64_Sym st_other field. */ |
| 56 | #define STO_PPC64_LOCAL_BIT 5 |
| 57 | #define STO_PPC64_LOCAL_MASK (7 << STO_PPC64_LOCAL_BIT) |
| 58 | #define PPC64_LOCAL_ENTRY_OFFSET(other) \ |
| 59 | (((1 << (((other) & STO_PPC64_LOCAL_MASK) >> STO_PPC64_LOCAL_BIT)) >> 2) << 2) |
| 60 | |
| 61 | static unsigned int local_entry_offset(const Elf64_Sym *sym) |
| 62 | { |
| 63 | /* sym->st_other indicates offset to local entry point |
| 64 | * (otherwise it will assume r12 is the address of the start |
| 65 | * of function and try to derive r2 from it). */ |
| 66 | return PPC64_LOCAL_ENTRY_OFFSET(sym->st_other); |
| 67 | } |
| 68 | #else |
| 69 | |
| 70 | static func_desc_t func_desc(unsigned long addr) |
| 71 | { |
| 72 | return *(struct func_desc *)addr; |
| 73 | } |
| 74 | static unsigned int local_entry_offset(const Elf64_Sym *sym) |
| 75 | { |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | void *dereference_module_function_descriptor(struct module *mod, void *ptr) |
| 80 | { |
| 81 | if (ptr < (void *)mod->arch.start_opd || |
| 82 | ptr >= (void *)mod->arch.end_opd) |
| 83 | return ptr; |
| 84 | |
| 85 | return dereference_function_descriptor(ptr); |
| 86 | } |
| 87 | #endif |
| 88 | |
| 89 | static unsigned long func_addr(unsigned long addr) |
| 90 | { |
| 91 | return func_desc(addr).addr; |
| 92 | } |
| 93 | |
| 94 | static unsigned long stub_func_addr(func_desc_t func) |
| 95 | { |
| 96 | return func.addr; |
| 97 | } |
| 98 | |
| 99 | #define STUB_MAGIC 0x73747562 /* stub */ |
| 100 | |
| 101 | /* Like PPC32, we need little trampolines to do > 24-bit jumps (into |
| 102 | the kernel itself). But on PPC64, these need to be used for every |
| 103 | jump, actually, to reset r2 (TOC+0x8000). */ |
| 104 | struct ppc64_stub_entry { |
| 105 | /* |
| 106 | * 28 byte jump instruction sequence (7 instructions) that can |
| 107 | * hold ppc64_stub_insns or stub_insns. Must be 8-byte aligned |
| 108 | * with PCREL kernels that use prefix instructions in the stub. |
| 109 | */ |
| 110 | u32 jump[7]; |
| 111 | /* Used by ftrace to identify stubs */ |
| 112 | u32 magic; |
| 113 | /* Data for the above code */ |
| 114 | func_desc_t funcdata; |
| 115 | } __aligned(8); |
| 116 | |
| 117 | struct ppc64_got_entry { |
| 118 | u64 addr; |
| 119 | }; |
| 120 | |
| 121 | /* |
| 122 | * PPC64 uses 24 bit jumps, but we need to jump into other modules or |
| 123 | * the kernel which may be further. So we jump to a stub. |
| 124 | * |
| 125 | * Target address and TOC are loaded from function descriptor in the |
| 126 | * ppc64_stub_entry. |
| 127 | * |
| 128 | * r12 is used to generate the target address, which is required for the |
| 129 | * ELFv2 global entry point calling convention. |
| 130 | * |
| 131 | * TOC handling: |
| 132 | * - PCREL does not have a TOC. |
| 133 | * - ELFv2 non-PCREL just has to save r2, the callee is responsible for |
| 134 | * setting its own TOC pointer at the global entry address. |
| 135 | * - ELFv1 must load the new TOC pointer from the function descriptor. |
| 136 | */ |
| 137 | static u32 ppc64_stub_insns[] = { |
| 138 | #ifdef CONFIG_PPC_KERNEL_PCREL |
| 139 | /* pld r12,addr */ |
| 140 | PPC_PREFIX_8LS | __PPC_PRFX_R(1), |
| 141 | PPC_INST_PLD | ___PPC_RT(_R12), |
| 142 | #else |
| 143 | PPC_RAW_ADDIS(_R11, _R2, 0), |
| 144 | PPC_RAW_ADDI(_R11, _R11, 0), |
| 145 | /* Save current r2 value in magic place on the stack. */ |
| 146 | PPC_RAW_STD(_R2, _R1, R2_STACK_OFFSET), |
| 147 | PPC_RAW_LD(_R12, _R11, 32), |
| 148 | #ifdef CONFIG_PPC64_ELF_ABI_V1 |
| 149 | /* Set up new r2 from function descriptor */ |
| 150 | PPC_RAW_LD(_R2, _R11, 40), |
| 151 | #endif |
| 152 | #endif |
| 153 | PPC_RAW_MTCTR(_R12), |
| 154 | PPC_RAW_BCTR(), |
| 155 | }; |
| 156 | |
| 157 | /* |
| 158 | * Count how many different r_type relocations (different symbol, |
| 159 | * different addend). |
| 160 | */ |
| 161 | static unsigned int count_relocs(const Elf64_Rela *rela, unsigned int num, |
| 162 | unsigned long r_type) |
| 163 | { |
| 164 | unsigned int i, r_info, r_addend, _count_relocs; |
| 165 | |
| 166 | /* FIXME: Only count external ones --RR */ |
| 167 | _count_relocs = 0; |
| 168 | r_info = 0; |
| 169 | r_addend = 0; |
| 170 | for (i = 0; i < num; i++) |
| 171 | /* Only count r_type relocs, others don't need stubs */ |
| 172 | if (ELF64_R_TYPE(rela[i].r_info) == r_type && |
| 173 | (r_info != ELF64_R_SYM(rela[i].r_info) || |
| 174 | r_addend != rela[i].r_addend)) { |
| 175 | _count_relocs++; |
| 176 | r_info = ELF64_R_SYM(rela[i].r_info); |
| 177 | r_addend = rela[i].r_addend; |
| 178 | } |
| 179 | |
| 180 | return _count_relocs; |
| 181 | } |
| 182 | |
| 183 | static int relacmp(const void *_x, const void *_y) |
| 184 | { |
| 185 | const Elf64_Rela *x, *y; |
| 186 | |
| 187 | y = (Elf64_Rela *)_x; |
| 188 | x = (Elf64_Rela *)_y; |
| 189 | |
| 190 | /* Compare the entire r_info (as opposed to ELF64_R_SYM(r_info) only) to |
| 191 | * make the comparison cheaper/faster. It won't affect the sorting or |
| 192 | * the counting algorithms' performance |
| 193 | */ |
| 194 | if (x->r_info < y->r_info) |
| 195 | return -1; |
| 196 | else if (x->r_info > y->r_info) |
| 197 | return 1; |
| 198 | else if (x->r_addend < y->r_addend) |
| 199 | return -1; |
| 200 | else if (x->r_addend > y->r_addend) |
| 201 | return 1; |
| 202 | else |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | /* Get size of potential trampolines required. */ |
| 207 | static unsigned long get_stubs_size(const Elf64_Ehdr *hdr, |
| 208 | const Elf64_Shdr *sechdrs, |
| 209 | char *secstrings, |
| 210 | struct module *me) |
| 211 | { |
| 212 | unsigned long relocs = 0; |
| 213 | unsigned i; |
| 214 | |
| 215 | /* Every relocated section... */ |
| 216 | for (i = 1; i < hdr->e_shnum; i++) { |
| 217 | if (sechdrs[i].sh_type == SHT_RELA) { |
| 218 | pr_debug("Found relocations in section %u\n" , i); |
| 219 | pr_debug("Ptr: %p. Number: %Lu\n" , |
| 220 | (void *)sechdrs[i].sh_addr, |
| 221 | sechdrs[i].sh_size / sizeof(Elf64_Rela)); |
| 222 | |
| 223 | /* Sort the relocation information based on a symbol and |
| 224 | * addend key. This is a stable O(n*log n) complexity |
| 225 | * algorithm but it will reduce the complexity of |
| 226 | * count_relocs() to linear complexity O(n) |
| 227 | */ |
| 228 | sort(base: (void *)sechdrs[i].sh_addr, |
| 229 | num: sechdrs[i].sh_size / sizeof(Elf64_Rela), |
| 230 | size: sizeof(Elf64_Rela), cmp_func: relacmp, NULL); |
| 231 | |
| 232 | relocs += count_relocs(rela: (void *)sechdrs[i].sh_addr, |
| 233 | num: sechdrs[i].sh_size |
| 234 | / sizeof(Elf64_Rela), |
| 235 | r_type: R_PPC_REL24); |
| 236 | #ifdef CONFIG_PPC_KERNEL_PCREL |
| 237 | relocs += count_relocs((void *)sechdrs[i].sh_addr, |
| 238 | sechdrs[i].sh_size |
| 239 | / sizeof(Elf64_Rela), |
| 240 | R_PPC64_REL24_NOTOC); |
| 241 | #endif |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /* stubs for ftrace_caller and ftrace_regs_caller */ |
| 246 | relocs += IS_ENABLED(CONFIG_DYNAMIC_FTRACE) + IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS); |
| 247 | |
| 248 | #ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE |
| 249 | /* stubs for the function tracer */ |
| 250 | for (i = 1; i < hdr->e_shnum; i++) { |
| 251 | if (!strcmp(secstrings + sechdrs[i].sh_name, "__patchable_function_entries" )) { |
| 252 | me->arch.ool_stub_count = sechdrs[i].sh_size / sizeof(unsigned long); |
| 253 | me->arch.ool_stub_index = 0; |
| 254 | relocs += roundup(me->arch.ool_stub_count * sizeof(struct ftrace_ool_stub), |
| 255 | sizeof(struct ppc64_stub_entry)) / |
| 256 | sizeof(struct ppc64_stub_entry); |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | #endif |
| 261 | |
| 262 | pr_debug("Looks like a total of %lu stubs, max\n" , relocs); |
| 263 | return relocs * sizeof(struct ppc64_stub_entry); |
| 264 | } |
| 265 | |
| 266 | #ifdef CONFIG_PPC_KERNEL_PCREL |
| 267 | static int count_pcpu_relocs(const Elf64_Shdr *sechdrs, |
| 268 | const Elf64_Rela *rela, unsigned int num, |
| 269 | unsigned int symindex, unsigned int pcpu) |
| 270 | { |
| 271 | unsigned int i, r_info, r_addend, _count_relocs; |
| 272 | |
| 273 | _count_relocs = 0; |
| 274 | r_info = 0; |
| 275 | r_addend = 0; |
| 276 | |
| 277 | for (i = 0; i < num; i++) { |
| 278 | Elf64_Sym *sym; |
| 279 | |
| 280 | /* This is the symbol it is referring to */ |
| 281 | sym = (Elf64_Sym *)sechdrs[symindex].sh_addr |
| 282 | + ELF64_R_SYM(rela[i].r_info); |
| 283 | |
| 284 | if (sym->st_shndx == pcpu && |
| 285 | (r_info != ELF64_R_SYM(rela[i].r_info) || |
| 286 | r_addend != rela[i].r_addend)) { |
| 287 | _count_relocs++; |
| 288 | r_info = ELF64_R_SYM(rela[i].r_info); |
| 289 | r_addend = rela[i].r_addend; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | return _count_relocs; |
| 294 | } |
| 295 | |
| 296 | /* Get size of potential GOT required. */ |
| 297 | static unsigned long get_got_size(const Elf64_Ehdr *hdr, |
| 298 | const Elf64_Shdr *sechdrs, |
| 299 | struct module *me) |
| 300 | { |
| 301 | /* One extra reloc so it's always 0-addr terminated */ |
| 302 | unsigned long relocs = 1; |
| 303 | unsigned int i, symindex = 0; |
| 304 | |
| 305 | for (i = 1; i < hdr->e_shnum; i++) { |
| 306 | if (sechdrs[i].sh_type == SHT_SYMTAB) { |
| 307 | symindex = i; |
| 308 | break; |
| 309 | } |
| 310 | } |
| 311 | WARN_ON_ONCE(!symindex); |
| 312 | |
| 313 | /* Every relocated section... */ |
| 314 | for (i = 1; i < hdr->e_shnum; i++) { |
| 315 | if (sechdrs[i].sh_type == SHT_RELA) { |
| 316 | pr_debug("Found relocations in section %u\n" , i); |
| 317 | pr_debug("Ptr: %p. Number: %llu\n" , (void *)sechdrs[i].sh_addr, |
| 318 | sechdrs[i].sh_size / sizeof(Elf64_Rela)); |
| 319 | |
| 320 | /* |
| 321 | * Sort the relocation information based on a symbol and |
| 322 | * addend key. This is a stable O(n*log n) complexity |
| 323 | * algorithm but it will reduce the complexity of |
| 324 | * count_relocs() to linear complexity O(n) |
| 325 | */ |
| 326 | sort((void *)sechdrs[i].sh_addr, |
| 327 | sechdrs[i].sh_size / sizeof(Elf64_Rela), |
| 328 | sizeof(Elf64_Rela), relacmp, NULL); |
| 329 | |
| 330 | relocs += count_relocs((void *)sechdrs[i].sh_addr, |
| 331 | sechdrs[i].sh_size |
| 332 | / sizeof(Elf64_Rela), |
| 333 | R_PPC64_GOT_PCREL34); |
| 334 | |
| 335 | /* |
| 336 | * Percpu data access typically gets linked with |
| 337 | * REL34 relocations, but the percpu section gets |
| 338 | * moved at load time and requires that to be |
| 339 | * converted to GOT linkage. |
| 340 | */ |
| 341 | if (IS_ENABLED(CONFIG_SMP) && symindex) |
| 342 | relocs += count_pcpu_relocs(sechdrs, |
| 343 | (void *)sechdrs[i].sh_addr, |
| 344 | sechdrs[i].sh_size |
| 345 | / sizeof(Elf64_Rela), |
| 346 | symindex, me->arch.pcpu_section); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | pr_debug("Looks like a total of %lu GOT entries, max\n" , relocs); |
| 351 | return relocs * sizeof(struct ppc64_got_entry); |
| 352 | } |
| 353 | #else /* CONFIG_PPC_KERNEL_PCREL */ |
| 354 | |
| 355 | /* Still needed for ELFv2, for .TOC. */ |
| 356 | static void dedotify_versions(struct modversion_info *vers, |
| 357 | unsigned long size) |
| 358 | { |
| 359 | struct modversion_info *end; |
| 360 | |
| 361 | for (end = (void *)vers + size; vers < end; vers++) |
| 362 | if (vers->name[0] == '.') { |
| 363 | memmove(vers->name, vers->name+1, strlen(vers->name)); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | /* Same as normal versions, remove a leading dot if present. */ |
| 368 | static void dedotify_ext_version_names(char *str_seq, unsigned long size) |
| 369 | { |
| 370 | unsigned long out = 0; |
| 371 | unsigned long in; |
| 372 | char last = '\0'; |
| 373 | |
| 374 | for (in = 0; in < size; in++) { |
| 375 | /* Skip one leading dot */ |
| 376 | if (last == '\0' && str_seq[in] == '.') |
| 377 | in++; |
| 378 | last = str_seq[in]; |
| 379 | str_seq[out++] = last; |
| 380 | } |
| 381 | /* Zero the trailing portion of the names table for robustness */ |
| 382 | memset(&str_seq[out], 0, size - out); |
| 383 | } |
| 384 | |
| 385 | /* |
| 386 | * Undefined symbols which refer to .funcname, hack to funcname. Make .TOC. |
| 387 | * seem to be defined (value set later). |
| 388 | */ |
| 389 | static void dedotify(Elf64_Sym *syms, unsigned int numsyms, char *strtab) |
| 390 | { |
| 391 | unsigned int i; |
| 392 | |
| 393 | for (i = 1; i < numsyms; i++) { |
| 394 | if (syms[i].st_shndx == SHN_UNDEF) { |
| 395 | char *name = strtab + syms[i].st_name; |
| 396 | if (name[0] == '.') { |
| 397 | if (strcmp(name+1, "TOC." ) == 0) |
| 398 | syms[i].st_shndx = SHN_ABS; |
| 399 | syms[i].st_name++; |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | static Elf64_Sym *find_dot_toc(Elf64_Shdr *sechdrs, |
| 406 | const char *strtab, |
| 407 | unsigned int symindex) |
| 408 | { |
| 409 | unsigned int i, numsyms; |
| 410 | Elf64_Sym *syms; |
| 411 | |
| 412 | syms = (Elf64_Sym *)sechdrs[symindex].sh_addr; |
| 413 | numsyms = sechdrs[symindex].sh_size / sizeof(Elf64_Sym); |
| 414 | |
| 415 | for (i = 1; i < numsyms; i++) { |
| 416 | if (syms[i].st_shndx == SHN_ABS |
| 417 | && strcmp(strtab + syms[i].st_name, "TOC." ) == 0) |
| 418 | return &syms[i]; |
| 419 | } |
| 420 | return NULL; |
| 421 | } |
| 422 | #endif /* CONFIG_PPC_KERNEL_PCREL */ |
| 423 | |
| 424 | bool module_init_section(const char *name) |
| 425 | { |
| 426 | /* We don't handle .init for the moment: always return false. */ |
| 427 | return false; |
| 428 | } |
| 429 | |
| 430 | int module_frob_arch_sections(Elf64_Ehdr *hdr, |
| 431 | Elf64_Shdr *sechdrs, |
| 432 | char *secstrings, |
| 433 | struct module *me) |
| 434 | { |
| 435 | unsigned int i; |
| 436 | |
| 437 | /* Find .toc and .stubs sections, symtab and strtab */ |
| 438 | for (i = 1; i < hdr->e_shnum; i++) { |
| 439 | if (strcmp(secstrings + sechdrs[i].sh_name, ".stubs" ) == 0) |
| 440 | me->arch.stubs_section = i; |
| 441 | #ifdef CONFIG_PPC_KERNEL_PCREL |
| 442 | else if (strcmp(secstrings + sechdrs[i].sh_name, ".data..percpu" ) == 0) |
| 443 | me->arch.pcpu_section = i; |
| 444 | else if (strcmp(secstrings + sechdrs[i].sh_name, ".mygot" ) == 0) { |
| 445 | me->arch.got_section = i; |
| 446 | if (sechdrs[i].sh_addralign < 8) |
| 447 | sechdrs[i].sh_addralign = 8; |
| 448 | } |
| 449 | #else |
| 450 | else if (strcmp(secstrings + sechdrs[i].sh_name, ".toc" ) == 0) { |
| 451 | me->arch.toc_section = i; |
| 452 | if (sechdrs[i].sh_addralign < 8) |
| 453 | sechdrs[i].sh_addralign = 8; |
| 454 | } else if (strcmp(secstrings + sechdrs[i].sh_name, "__versions" ) == 0) |
| 455 | dedotify_versions(vers: (void *)hdr + sechdrs[i].sh_offset, |
| 456 | size: sechdrs[i].sh_size); |
| 457 | else if (strcmp(secstrings + sechdrs[i].sh_name, "__version_ext_names" ) == 0) |
| 458 | dedotify_ext_version_names(str_seq: (void *)hdr + sechdrs[i].sh_offset, |
| 459 | size: sechdrs[i].sh_size); |
| 460 | |
| 461 | if (sechdrs[i].sh_type == SHT_SYMTAB) |
| 462 | dedotify(syms: (void *)hdr + sechdrs[i].sh_offset, |
| 463 | numsyms: sechdrs[i].sh_size / sizeof(Elf64_Sym), |
| 464 | strtab: (void *)hdr |
| 465 | + sechdrs[sechdrs[i].sh_link].sh_offset); |
| 466 | #endif |
| 467 | } |
| 468 | |
| 469 | if (!me->arch.stubs_section) { |
| 470 | pr_err("%s: doesn't contain .stubs.\n" , me->name); |
| 471 | return -ENOEXEC; |
| 472 | } |
| 473 | |
| 474 | #ifdef CONFIG_PPC_KERNEL_PCREL |
| 475 | if (!me->arch.got_section) { |
| 476 | pr_err("%s: doesn't contain .mygot.\n" , me->name); |
| 477 | return -ENOEXEC; |
| 478 | } |
| 479 | |
| 480 | /* Override the got size */ |
| 481 | sechdrs[me->arch.got_section].sh_size = get_got_size(hdr, sechdrs, me); |
| 482 | #else |
| 483 | /* If we don't have a .toc, just use .stubs. We need to set r2 |
| 484 | to some reasonable value in case the module calls out to |
| 485 | other functions via a stub, or if a function pointer escapes |
| 486 | the module by some means. */ |
| 487 | if (!me->arch.toc_section) |
| 488 | me->arch.toc_section = me->arch.stubs_section; |
| 489 | #endif |
| 490 | |
| 491 | /* Override the stubs size */ |
| 492 | sechdrs[me->arch.stubs_section].sh_size = get_stubs_size(hdr, sechdrs, secstrings, me); |
| 493 | |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | #if defined(CONFIG_MPROFILE_KERNEL) || defined(CONFIG_ARCH_USING_PATCHABLE_FUNCTION_ENTRY) |
| 498 | |
| 499 | static u32 stub_insns[] = { |
| 500 | #ifdef CONFIG_PPC_KERNEL_PCREL |
| 501 | PPC_RAW_LD(_R12, _R13, offsetof(struct paca_struct, kernelbase)), |
| 502 | PPC_RAW_NOP(), /* align the prefix insn */ |
| 503 | /* paddi r12,r12,addr */ |
| 504 | PPC_PREFIX_MLS | __PPC_PRFX_R(0), |
| 505 | PPC_INST_PADDI | ___PPC_RT(_R12) | ___PPC_RA(_R12), |
| 506 | PPC_RAW_MTCTR(_R12), |
| 507 | PPC_RAW_BCTR(), |
| 508 | #else |
| 509 | PPC_RAW_LD(_R12, _R13, offsetof(struct paca_struct, kernel_toc)), |
| 510 | PPC_RAW_ADDIS(_R12, _R12, 0), |
| 511 | PPC_RAW_ADDI(_R12, _R12, 0), |
| 512 | PPC_RAW_MTCTR(_R12), |
| 513 | PPC_RAW_BCTR(), |
| 514 | #endif |
| 515 | }; |
| 516 | |
| 517 | /* |
| 518 | * For mprofile-kernel we use a special stub for ftrace_caller() because we |
| 519 | * can't rely on r2 containing this module's TOC when we enter the stub. |
| 520 | * |
| 521 | * That can happen if the function calling us didn't need to use the toc. In |
| 522 | * that case it won't have setup r2, and the r2 value will be either the |
| 523 | * kernel's toc, or possibly another modules toc. |
| 524 | * |
| 525 | * To deal with that this stub uses the kernel toc, which is always accessible |
| 526 | * via the paca (in r13). The target (ftrace_caller()) is responsible for |
| 527 | * saving and restoring the toc before returning. |
| 528 | */ |
| 529 | static inline int create_ftrace_stub(struct ppc64_stub_entry *entry, |
| 530 | unsigned long addr, |
| 531 | struct module *me) |
| 532 | { |
| 533 | long reladdr; |
| 534 | |
| 535 | if ((unsigned long)entry->jump % 8 != 0) { |
| 536 | pr_err("%s: Address of stub entry is not 8-byte aligned\n" , me->name); |
| 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | BUILD_BUG_ON(sizeof(stub_insns) > sizeof(entry->jump)); |
| 541 | memcpy(entry->jump, stub_insns, sizeof(stub_insns)); |
| 542 | |
| 543 | if (IS_ENABLED(CONFIG_PPC_KERNEL_PCREL)) { |
| 544 | /* Stub uses address relative to kernel base (from the paca) */ |
| 545 | reladdr = addr - local_paca->kernelbase; |
| 546 | if (reladdr > 0x1FFFFFFFFL || reladdr < -0x200000000L) { |
| 547 | pr_err("%s: Address of %ps out of range of 34-bit relative address.\n" , |
| 548 | me->name, (void *)addr); |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | entry->jump[2] |= IMM_H18(reladdr); |
| 553 | entry->jump[3] |= IMM_L(reladdr); |
| 554 | } else { |
| 555 | /* Stub uses address relative to kernel toc (from the paca) */ |
| 556 | reladdr = addr - kernel_toc_addr(); |
| 557 | if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) { |
| 558 | pr_err("%s: Address of %ps out of range of kernel_toc.\n" , |
| 559 | me->name, (void *)addr); |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | entry->jump[1] |= PPC_HA(reladdr); |
| 564 | entry->jump[2] |= PPC_LO(reladdr); |
| 565 | } |
| 566 | |
| 567 | /* Even though we don't use funcdata in the stub, it's needed elsewhere. */ |
| 568 | entry->funcdata = func_desc(addr); |
| 569 | entry->magic = STUB_MAGIC; |
| 570 | |
| 571 | return 1; |
| 572 | } |
| 573 | |
| 574 | static bool is_mprofile_ftrace_call(const char *name) |
| 575 | { |
| 576 | if (!strcmp("_mcount" , name)) |
| 577 | return true; |
| 578 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 579 | if (!strcmp("ftrace_caller" , name)) |
| 580 | return true; |
| 581 | #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS |
| 582 | if (!strcmp("ftrace_regs_caller" , name)) |
| 583 | return true; |
| 584 | #endif |
| 585 | #endif |
| 586 | |
| 587 | return false; |
| 588 | } |
| 589 | #else |
| 590 | static inline int create_ftrace_stub(struct ppc64_stub_entry *entry, |
| 591 | unsigned long addr, |
| 592 | struct module *me) |
| 593 | { |
| 594 | return 0; |
| 595 | } |
| 596 | |
| 597 | static bool is_mprofile_ftrace_call(const char *name) |
| 598 | { |
| 599 | return false; |
| 600 | } |
| 601 | #endif |
| 602 | |
| 603 | /* |
| 604 | * r2 is the TOC pointer: it actually points 0x8000 into the TOC (this gives the |
| 605 | * value maximum span in an instruction which uses a signed offset). Round down |
| 606 | * to a 256 byte boundary for the odd case where we are setting up r2 without a |
| 607 | * .toc section. |
| 608 | */ |
| 609 | static inline unsigned long my_r2(const Elf64_Shdr *sechdrs, struct module *me) |
| 610 | { |
| 611 | #ifndef CONFIG_PPC_KERNEL_PCREL |
| 612 | return (sechdrs[me->arch.toc_section].sh_addr & ~0xfful) + 0x8000; |
| 613 | #else |
| 614 | return -1; |
| 615 | #endif |
| 616 | } |
| 617 | |
| 618 | /* Patch stub to reference function and correct r2 value. */ |
| 619 | static inline int create_stub(const Elf64_Shdr *sechdrs, |
| 620 | struct ppc64_stub_entry *entry, |
| 621 | unsigned long addr, |
| 622 | struct module *me, |
| 623 | const char *name) |
| 624 | { |
| 625 | long reladdr; |
| 626 | func_desc_t desc; |
| 627 | int i; |
| 628 | |
| 629 | if (is_mprofile_ftrace_call(name)) |
| 630 | return create_ftrace_stub(entry, addr, me); |
| 631 | |
| 632 | if ((unsigned long)entry->jump % 8 != 0) { |
| 633 | pr_err("%s: Address of stub entry is not 8-byte aligned\n" , me->name); |
| 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | BUILD_BUG_ON(sizeof(ppc64_stub_insns) > sizeof(entry->jump)); |
| 638 | for (i = 0; i < ARRAY_SIZE(ppc64_stub_insns); i++) { |
| 639 | if (patch_instruction(&entry->jump[i], |
| 640 | ppc_inst(ppc64_stub_insns[i]))) |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | if (IS_ENABLED(CONFIG_PPC_KERNEL_PCREL)) { |
| 645 | /* Stub uses address relative to itself! */ |
| 646 | reladdr = 0 + offsetof(struct ppc64_stub_entry, funcdata); |
| 647 | BUILD_BUG_ON(reladdr != 32); |
| 648 | if (reladdr > 0x1FFFFFFFFL || reladdr < -0x200000000L) { |
| 649 | pr_err("%s: Address of %p out of range of 34-bit relative address.\n" , |
| 650 | me->name, (void *)reladdr); |
| 651 | return 0; |
| 652 | } |
| 653 | pr_debug("Stub %p get data from reladdr %li\n" , entry, reladdr); |
| 654 | |
| 655 | /* May not even need this if we're relative to 0 */ |
| 656 | if (patch_instruction(&entry->jump[0], |
| 657 | ppc_inst_prefix(entry->jump[0] | IMM_H18(reladdr), |
| 658 | entry->jump[1] | IMM_L(reladdr)))) |
| 659 | return 0; |
| 660 | |
| 661 | } else { |
| 662 | /* Stub uses address relative to r2. */ |
| 663 | reladdr = (unsigned long)entry - my_r2(sechdrs, me); |
| 664 | if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) { |
| 665 | pr_err("%s: Address %p of stub out of range of %p.\n" , |
| 666 | me->name, (void *)reladdr, (void *)my_r2); |
| 667 | return 0; |
| 668 | } |
| 669 | pr_debug("Stub %p get data from reladdr %li\n" , entry, reladdr); |
| 670 | |
| 671 | if (patch_instruction(&entry->jump[0], |
| 672 | ppc_inst(entry->jump[0] | PPC_HA(reladdr)))) |
| 673 | return 0; |
| 674 | |
| 675 | if (patch_instruction(&entry->jump[1], |
| 676 | ppc_inst(entry->jump[1] | PPC_LO(reladdr)))) |
| 677 | return 0; |
| 678 | } |
| 679 | |
| 680 | // func_desc_t is 8 bytes if ABIv2, else 16 bytes |
| 681 | desc = func_desc(addr); |
| 682 | for (i = 0; i < sizeof(func_desc_t) / sizeof(u32); i++) { |
| 683 | if (patch_u32(((u32 *)&entry->funcdata) + i, ((u32 *)&desc)[i])) |
| 684 | return 0; |
| 685 | } |
| 686 | |
| 687 | if (patch_u32(&entry->magic, STUB_MAGIC)) |
| 688 | return 0; |
| 689 | |
| 690 | return 1; |
| 691 | } |
| 692 | |
| 693 | /* Create stub to jump to function described in this OPD/ptr: we need the |
| 694 | stub to set up the TOC ptr (r2) for the function. */ |
| 695 | static unsigned long stub_for_addr(const Elf64_Shdr *sechdrs, |
| 696 | unsigned long addr, |
| 697 | struct module *me, |
| 698 | const char *name) |
| 699 | { |
| 700 | struct ppc64_stub_entry *stubs; |
| 701 | unsigned int i, num_stubs; |
| 702 | |
| 703 | num_stubs = sechdrs[me->arch.stubs_section].sh_size / sizeof(*stubs); |
| 704 | |
| 705 | /* Find this stub, or if that fails, the next avail. entry */ |
| 706 | stubs = (void *)sechdrs[me->arch.stubs_section].sh_addr; |
| 707 | for (i = 0; i < me->arch.stub_count; i++) { |
| 708 | if (WARN_ON(i >= num_stubs)) |
| 709 | return 0; |
| 710 | |
| 711 | if (stub_func_addr(func: stubs[i].funcdata) == func_addr(addr)) |
| 712 | return (unsigned long)&stubs[i]; |
| 713 | } |
| 714 | |
| 715 | if (!create_stub(sechdrs, entry: &stubs[i], addr, me, name)) |
| 716 | return 0; |
| 717 | |
| 718 | me->arch.stub_count++; |
| 719 | return (unsigned long)&stubs[i]; |
| 720 | } |
| 721 | |
| 722 | #ifdef CONFIG_PPC_KERNEL_PCREL |
| 723 | /* Create GOT to load the location described in this ptr */ |
| 724 | static unsigned long got_for_addr(const Elf64_Shdr *sechdrs, |
| 725 | unsigned long addr, |
| 726 | struct module *me, |
| 727 | const char *name) |
| 728 | { |
| 729 | struct ppc64_got_entry *got; |
| 730 | unsigned int i, num_got; |
| 731 | |
| 732 | if (!IS_ENABLED(CONFIG_PPC_KERNEL_PCREL)) |
| 733 | return addr; |
| 734 | |
| 735 | num_got = sechdrs[me->arch.got_section].sh_size / sizeof(*got); |
| 736 | |
| 737 | /* Find this stub, or if that fails, the next avail. entry */ |
| 738 | got = (void *)sechdrs[me->arch.got_section].sh_addr; |
| 739 | for (i = 0; got[i].addr; i++) { |
| 740 | if (WARN_ON(i >= num_got)) |
| 741 | return 0; |
| 742 | |
| 743 | if (got[i].addr == addr) |
| 744 | return (unsigned long)&got[i]; |
| 745 | } |
| 746 | |
| 747 | got[i].addr = addr; |
| 748 | |
| 749 | return (unsigned long)&got[i]; |
| 750 | } |
| 751 | #endif |
| 752 | |
| 753 | /* We expect a noop next: if it is, replace it with instruction to |
| 754 | restore r2. */ |
| 755 | static int restore_r2(const char *name, u32 *instruction, struct module *me) |
| 756 | { |
| 757 | u32 *prev_insn = instruction - 1; |
| 758 | u32 insn_val = *instruction; |
| 759 | |
| 760 | if (IS_ENABLED(CONFIG_PPC_KERNEL_PCREL)) |
| 761 | return 0; |
| 762 | |
| 763 | if (is_mprofile_ftrace_call(name)) |
| 764 | return 0; |
| 765 | |
| 766 | /* |
| 767 | * Make sure the branch isn't a sibling call. Sibling calls aren't |
| 768 | * "link" branches and they don't return, so they don't need the r2 |
| 769 | * restore afterwards. |
| 770 | */ |
| 771 | if (!instr_is_relative_link_branch(ppc_inst(*prev_insn))) |
| 772 | return 0; |
| 773 | |
| 774 | /* |
| 775 | * For livepatch, the restore r2 instruction might have already been |
| 776 | * written previously, if the referenced symbol is in a previously |
| 777 | * unloaded module which is now being loaded again. In that case, skip |
| 778 | * the warning and the instruction write. |
| 779 | */ |
| 780 | if (insn_val == PPC_INST_LD_TOC) |
| 781 | return 0; |
| 782 | |
| 783 | if (insn_val != PPC_RAW_NOP()) { |
| 784 | pr_err("%s: Expected nop after call, got %08x at %pS\n" , |
| 785 | me->name, insn_val, instruction); |
| 786 | return -ENOEXEC; |
| 787 | } |
| 788 | |
| 789 | /* ld r2,R2_STACK_OFFSET(r1) */ |
| 790 | return patch_instruction(instruction, ppc_inst(PPC_INST_LD_TOC)); |
| 791 | } |
| 792 | |
| 793 | int apply_relocate_add(Elf64_Shdr *sechdrs, |
| 794 | const char *strtab, |
| 795 | unsigned int symindex, |
| 796 | unsigned int relsec, |
| 797 | struct module *me) |
| 798 | { |
| 799 | unsigned int i; |
| 800 | Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr; |
| 801 | Elf64_Sym *sym; |
| 802 | unsigned long *location; |
| 803 | unsigned long value; |
| 804 | |
| 805 | pr_debug("Applying ADD relocate section %u to %u\n" , relsec, |
| 806 | sechdrs[relsec].sh_info); |
| 807 | |
| 808 | #ifndef CONFIG_PPC_KERNEL_PCREL |
| 809 | /* First time we're called, we can fix up .TOC. */ |
| 810 | if (!me->arch.toc_fixed) { |
| 811 | sym = find_dot_toc(sechdrs, strtab, symindex); |
| 812 | /* It's theoretically possible that a module doesn't want a |
| 813 | * .TOC. so don't fail it just for that. */ |
| 814 | if (sym) |
| 815 | sym->st_value = my_r2(sechdrs, me); |
| 816 | me->arch.toc_fixed = true; |
| 817 | } |
| 818 | #endif |
| 819 | for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) { |
| 820 | /* This is where to make the change */ |
| 821 | location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr |
| 822 | + rela[i].r_offset; |
| 823 | /* This is the symbol it is referring to */ |
| 824 | sym = (Elf64_Sym *)sechdrs[symindex].sh_addr |
| 825 | + ELF64_R_SYM(rela[i].r_info); |
| 826 | |
| 827 | pr_debug("RELOC at %p: %li-type as %s (0x%lx) + %li\n" , |
| 828 | location, (long)ELF64_R_TYPE(rela[i].r_info), |
| 829 | strtab + sym->st_name, (unsigned long)sym->st_value, |
| 830 | (long)rela[i].r_addend); |
| 831 | |
| 832 | /* `Everything is relative'. */ |
| 833 | value = sym->st_value + rela[i].r_addend; |
| 834 | |
| 835 | switch (ELF64_R_TYPE(rela[i].r_info)) { |
| 836 | case R_PPC64_ADDR32: |
| 837 | /* Simply set it */ |
| 838 | *(u32 *)location = value; |
| 839 | break; |
| 840 | |
| 841 | case R_PPC64_ADDR64: |
| 842 | /* Simply set it */ |
| 843 | *(unsigned long *)location = value; |
| 844 | break; |
| 845 | |
| 846 | #ifndef CONFIG_PPC_KERNEL_PCREL |
| 847 | case R_PPC64_TOC: |
| 848 | *(unsigned long *)location = my_r2(sechdrs, me); |
| 849 | break; |
| 850 | |
| 851 | case R_PPC64_TOC16: |
| 852 | /* Subtract TOC pointer */ |
| 853 | value -= my_r2(sechdrs, me); |
| 854 | if (value + 0x8000 > 0xffff) { |
| 855 | pr_err("%s: bad TOC16 relocation (0x%lx)\n" , |
| 856 | me->name, value); |
| 857 | return -ENOEXEC; |
| 858 | } |
| 859 | *((uint16_t *) location) |
| 860 | = (*((uint16_t *) location) & ~0xffff) |
| 861 | | (value & 0xffff); |
| 862 | break; |
| 863 | |
| 864 | case R_PPC64_TOC16_LO: |
| 865 | /* Subtract TOC pointer */ |
| 866 | value -= my_r2(sechdrs, me); |
| 867 | *((uint16_t *) location) |
| 868 | = (*((uint16_t *) location) & ~0xffff) |
| 869 | | (value & 0xffff); |
| 870 | break; |
| 871 | |
| 872 | case R_PPC64_TOC16_DS: |
| 873 | /* Subtract TOC pointer */ |
| 874 | value -= my_r2(sechdrs, me); |
| 875 | if ((value & 3) != 0 || value + 0x8000 > 0xffff) { |
| 876 | pr_err("%s: bad TOC16_DS relocation (0x%lx)\n" , |
| 877 | me->name, value); |
| 878 | return -ENOEXEC; |
| 879 | } |
| 880 | *((uint16_t *) location) |
| 881 | = (*((uint16_t *) location) & ~0xfffc) |
| 882 | | (value & 0xfffc); |
| 883 | break; |
| 884 | |
| 885 | case R_PPC64_TOC16_LO_DS: |
| 886 | /* Subtract TOC pointer */ |
| 887 | value -= my_r2(sechdrs, me); |
| 888 | if ((value & 3) != 0) { |
| 889 | pr_err("%s: bad TOC16_LO_DS relocation (0x%lx)\n" , |
| 890 | me->name, value); |
| 891 | return -ENOEXEC; |
| 892 | } |
| 893 | *((uint16_t *) location) |
| 894 | = (*((uint16_t *) location) & ~0xfffc) |
| 895 | | (value & 0xfffc); |
| 896 | break; |
| 897 | |
| 898 | case R_PPC64_TOC16_HA: |
| 899 | /* Subtract TOC pointer */ |
| 900 | value -= my_r2(sechdrs, me); |
| 901 | value = ((value + 0x8000) >> 16); |
| 902 | *((uint16_t *) location) |
| 903 | = (*((uint16_t *) location) & ~0xffff) |
| 904 | | (value & 0xffff); |
| 905 | break; |
| 906 | #endif |
| 907 | |
| 908 | case R_PPC_REL24: |
| 909 | #ifdef CONFIG_PPC_KERNEL_PCREL |
| 910 | /* PCREL still generates REL24 for mcount */ |
| 911 | case R_PPC64_REL24_NOTOC: |
| 912 | #endif |
| 913 | /* FIXME: Handle weak symbols here --RR */ |
| 914 | if (sym->st_shndx == SHN_UNDEF || |
| 915 | sym->st_shndx == SHN_LIVEPATCH) { |
| 916 | /* External: go via stub */ |
| 917 | value = stub_for_addr(sechdrs, addr: value, me, |
| 918 | name: strtab + sym->st_name); |
| 919 | if (!value) |
| 920 | return -ENOENT; |
| 921 | if (restore_r2(name: strtab + sym->st_name, |
| 922 | instruction: (u32 *)location + 1, me)) |
| 923 | return -ENOEXEC; |
| 924 | } else |
| 925 | value += local_entry_offset(sym); |
| 926 | |
| 927 | /* Convert value to relative */ |
| 928 | value -= (unsigned long)location; |
| 929 | if (value + 0x2000000 > 0x3ffffff || (value & 3) != 0){ |
| 930 | pr_err("%s: REL24 %li out of range!\n" , |
| 931 | me->name, (long int)value); |
| 932 | return -ENOEXEC; |
| 933 | } |
| 934 | |
| 935 | /* Only replace bits 2 through 26 */ |
| 936 | value = (*(uint32_t *)location & ~PPC_LI_MASK) | PPC_LI(value); |
| 937 | |
| 938 | if (patch_instruction((u32 *)location, ppc_inst(value))) |
| 939 | return -EFAULT; |
| 940 | |
| 941 | break; |
| 942 | |
| 943 | case R_PPC64_REL64: |
| 944 | /* 64 bits relative (used by features fixups) */ |
| 945 | *location = value - (unsigned long)location; |
| 946 | break; |
| 947 | |
| 948 | case R_PPC64_REL32: |
| 949 | /* 32 bits relative (used by relative exception tables) */ |
| 950 | /* Convert value to relative */ |
| 951 | value -= (unsigned long)location; |
| 952 | if (value + 0x80000000 > 0xffffffff) { |
| 953 | pr_err("%s: REL32 %li out of range!\n" , |
| 954 | me->name, (long int)value); |
| 955 | return -ENOEXEC; |
| 956 | } |
| 957 | *(u32 *)location = value; |
| 958 | break; |
| 959 | |
| 960 | #ifdef CONFIG_PPC_KERNEL_PCREL |
| 961 | case R_PPC64_PCREL34: { |
| 962 | unsigned long absvalue = value; |
| 963 | |
| 964 | /* Convert value to relative */ |
| 965 | value -= (unsigned long)location; |
| 966 | |
| 967 | if (value + 0x200000000 > 0x3ffffffff) { |
| 968 | if (sym->st_shndx != me->arch.pcpu_section) { |
| 969 | pr_err("%s: REL34 %li out of range!\n" , |
| 970 | me->name, (long)value); |
| 971 | return -ENOEXEC; |
| 972 | } |
| 973 | |
| 974 | /* |
| 975 | * per-cpu section is special cased because |
| 976 | * it is moved during loading, so has to be |
| 977 | * converted to use GOT. |
| 978 | */ |
| 979 | value = got_for_addr(sechdrs, absvalue, me, |
| 980 | strtab + sym->st_name); |
| 981 | if (!value) |
| 982 | return -ENOENT; |
| 983 | value -= (unsigned long)location; |
| 984 | |
| 985 | /* Turn pla into pld */ |
| 986 | if (patch_instruction((u32 *)location, |
| 987 | ppc_inst_prefix((*(u32 *)location & ~0x02000000), |
| 988 | (*((u32 *)location + 1) & ~0xf8000000) | 0xe4000000))) |
| 989 | return -EFAULT; |
| 990 | } |
| 991 | |
| 992 | if (patch_instruction((u32 *)location, |
| 993 | ppc_inst_prefix((*(u32 *)location & ~0x3ffff) | IMM_H18(value), |
| 994 | (*((u32 *)location + 1) & ~0xffff) | IMM_L(value)))) |
| 995 | return -EFAULT; |
| 996 | |
| 997 | break; |
| 998 | } |
| 999 | |
| 1000 | #else |
| 1001 | case R_PPC64_TOCSAVE: |
| 1002 | /* |
| 1003 | * Marker reloc indicates we don't have to save r2. |
| 1004 | * That would only save us one instruction, so ignore |
| 1005 | * it. |
| 1006 | */ |
| 1007 | break; |
| 1008 | #endif |
| 1009 | |
| 1010 | case R_PPC64_ENTRY: |
| 1011 | if (IS_ENABLED(CONFIG_PPC_KERNEL_PCREL)) |
| 1012 | break; |
| 1013 | |
| 1014 | /* |
| 1015 | * Optimize ELFv2 large code model entry point if |
| 1016 | * the TOC is within 2GB range of current location. |
| 1017 | */ |
| 1018 | value = my_r2(sechdrs, me) - (unsigned long)location; |
| 1019 | if (value + 0x80008000 > 0xffffffff) |
| 1020 | break; |
| 1021 | /* |
| 1022 | * Check for the large code model prolog sequence: |
| 1023 | * ld r2, ...(r12) |
| 1024 | * add r2, r2, r12 |
| 1025 | */ |
| 1026 | if ((((uint32_t *)location)[0] & ~0xfffc) != PPC_RAW_LD(_R2, _R12, 0)) |
| 1027 | break; |
| 1028 | if (((uint32_t *)location)[1] != PPC_RAW_ADD(_R2, _R2, _R12)) |
| 1029 | break; |
| 1030 | /* |
| 1031 | * If found, replace it with: |
| 1032 | * addis r2, r12, (.TOC.-func)@ha |
| 1033 | * addi r2, r2, (.TOC.-func)@l |
| 1034 | */ |
| 1035 | ((uint32_t *)location)[0] = PPC_RAW_ADDIS(_R2, _R12, PPC_HA(value)); |
| 1036 | ((uint32_t *)location)[1] = PPC_RAW_ADDI(_R2, _R2, PPC_LO(value)); |
| 1037 | break; |
| 1038 | |
| 1039 | case R_PPC64_REL16_HA: |
| 1040 | /* Subtract location pointer */ |
| 1041 | value -= (unsigned long)location; |
| 1042 | value = ((value + 0x8000) >> 16); |
| 1043 | *((uint16_t *) location) |
| 1044 | = (*((uint16_t *) location) & ~0xffff) |
| 1045 | | (value & 0xffff); |
| 1046 | break; |
| 1047 | |
| 1048 | case R_PPC64_REL16_LO: |
| 1049 | /* Subtract location pointer */ |
| 1050 | value -= (unsigned long)location; |
| 1051 | *((uint16_t *) location) |
| 1052 | = (*((uint16_t *) location) & ~0xffff) |
| 1053 | | (value & 0xffff); |
| 1054 | break; |
| 1055 | |
| 1056 | #ifdef CONFIG_PPC_KERNEL_PCREL |
| 1057 | case R_PPC64_GOT_PCREL34: |
| 1058 | value = got_for_addr(sechdrs, value, me, |
| 1059 | strtab + sym->st_name); |
| 1060 | if (!value) |
| 1061 | return -ENOENT; |
| 1062 | value -= (unsigned long)location; |
| 1063 | ((uint32_t *)location)[0] = (((uint32_t *)location)[0] & ~0x3ffff) | |
| 1064 | ((value >> 16) & 0x3ffff); |
| 1065 | ((uint32_t *)location)[1] = (((uint32_t *)location)[1] & ~0xffff) | |
| 1066 | (value & 0xffff); |
| 1067 | break; |
| 1068 | #endif |
| 1069 | |
| 1070 | default: |
| 1071 | pr_err("%s: Unknown ADD relocation: %lu\n" , |
| 1072 | me->name, |
| 1073 | (unsigned long)ELF64_R_TYPE(rela[i].r_info)); |
| 1074 | return -ENOEXEC; |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | return 0; |
| 1079 | } |
| 1080 | |
| 1081 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 1082 | int module_trampoline_target(struct module *mod, unsigned long addr, |
| 1083 | unsigned long *target) |
| 1084 | { |
| 1085 | struct ppc64_stub_entry *stub; |
| 1086 | func_desc_t funcdata; |
| 1087 | u32 magic; |
| 1088 | |
| 1089 | if (!within_module_core(addr, mod)) { |
| 1090 | pr_err("%s: stub %lx not in module %s\n" , __func__, addr, mod->name); |
| 1091 | return -EFAULT; |
| 1092 | } |
| 1093 | |
| 1094 | stub = (struct ppc64_stub_entry *)addr; |
| 1095 | |
| 1096 | if (copy_from_kernel_nofault(dst: &magic, src: &stub->magic, |
| 1097 | size: sizeof(magic))) { |
| 1098 | pr_err("%s: fault reading magic for stub %lx for %s\n" , __func__, addr, mod->name); |
| 1099 | return -EFAULT; |
| 1100 | } |
| 1101 | |
| 1102 | if (magic != STUB_MAGIC) { |
| 1103 | pr_err("%s: bad magic for stub %lx for %s\n" , __func__, addr, mod->name); |
| 1104 | return -EFAULT; |
| 1105 | } |
| 1106 | |
| 1107 | if (copy_from_kernel_nofault(dst: &funcdata, src: &stub->funcdata, |
| 1108 | size: sizeof(funcdata))) { |
| 1109 | pr_err("%s: fault reading funcdata for stub %lx for %s\n" , __func__, addr, mod->name); |
| 1110 | return -EFAULT; |
| 1111 | } |
| 1112 | |
| 1113 | *target = stub_func_addr(func: funcdata); |
| 1114 | |
| 1115 | return 0; |
| 1116 | } |
| 1117 | |
| 1118 | static int setup_ftrace_ool_stubs(const Elf64_Shdr *sechdrs, unsigned long addr, struct module *me) |
| 1119 | { |
| 1120 | #ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE |
| 1121 | unsigned int total_stubs, num_stubs; |
| 1122 | struct ppc64_stub_entry *stub; |
| 1123 | |
| 1124 | total_stubs = sechdrs[me->arch.stubs_section].sh_size / sizeof(*stub); |
| 1125 | num_stubs = roundup(me->arch.ool_stub_count * sizeof(struct ftrace_ool_stub), |
| 1126 | sizeof(struct ppc64_stub_entry)) / sizeof(struct ppc64_stub_entry); |
| 1127 | |
| 1128 | if (WARN_ON(me->arch.stub_count + num_stubs > total_stubs)) |
| 1129 | return -1; |
| 1130 | |
| 1131 | stub = (void *)sechdrs[me->arch.stubs_section].sh_addr; |
| 1132 | me->arch.ool_stubs = (struct ftrace_ool_stub *)(stub + me->arch.stub_count); |
| 1133 | me->arch.stub_count += num_stubs; |
| 1134 | #endif |
| 1135 | |
| 1136 | return 0; |
| 1137 | } |
| 1138 | |
| 1139 | int module_finalize_ftrace(struct module *mod, const Elf_Shdr *sechdrs) |
| 1140 | { |
| 1141 | mod->arch.tramp = stub_for_addr(sechdrs, |
| 1142 | addr: (unsigned long)ftrace_caller, |
| 1143 | me: mod, |
| 1144 | name: "ftrace_caller" ); |
| 1145 | #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS |
| 1146 | mod->arch.tramp_regs = stub_for_addr(sechdrs, |
| 1147 | addr: (unsigned long)ftrace_regs_caller, |
| 1148 | me: mod, |
| 1149 | name: "ftrace_regs_caller" ); |
| 1150 | if (!mod->arch.tramp_regs) |
| 1151 | return -ENOENT; |
| 1152 | #endif |
| 1153 | |
| 1154 | if (!mod->arch.tramp) |
| 1155 | return -ENOENT; |
| 1156 | |
| 1157 | if (setup_ftrace_ool_stubs(sechdrs, addr: mod->arch.tramp, me: mod)) |
| 1158 | return -ENOENT; |
| 1159 | |
| 1160 | return 0; |
| 1161 | } |
| 1162 | #endif |
| 1163 | |