module_64.c 30 KB

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