module_64.c 22 KB

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