module_32.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Kernel module help for PPC.
  3. Copyright (C) 2001 Rusty Russell.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/module.h>
  7. #include <linux/moduleloader.h>
  8. #include <linux/elf.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/fs.h>
  11. #include <linux/string.h>
  12. #include <linux/kernel.h>
  13. #include <linux/ftrace.h>
  14. #include <linux/cache.h>
  15. #include <linux/bug.h>
  16. #include <linux/sort.h>
  17. #include <asm/setup.h>
  18. #include <asm/code-patching.h>
  19. /* Count how many different relocations (different symbol, different
  20. addend) */
  21. static unsigned int count_relocs(const Elf32_Rela *rela, unsigned int num)
  22. {
  23. unsigned int i, r_info, r_addend, _count_relocs;
  24. _count_relocs = 0;
  25. r_info = 0;
  26. r_addend = 0;
  27. for (i = 0; i < num; i++)
  28. /* Only count 24-bit relocs, others don't need stubs */
  29. if (ELF32_R_TYPE(rela[i].r_info) == R_PPC_REL24 &&
  30. (r_info != ELF32_R_SYM(rela[i].r_info) ||
  31. r_addend != rela[i].r_addend)) {
  32. _count_relocs++;
  33. r_info = ELF32_R_SYM(rela[i].r_info);
  34. r_addend = rela[i].r_addend;
  35. }
  36. #ifdef CONFIG_DYNAMIC_FTRACE
  37. _count_relocs++; /* add one for ftrace_caller */
  38. #endif
  39. return _count_relocs;
  40. }
  41. static int relacmp(const void *_x, const void *_y)
  42. {
  43. const Elf32_Rela *x, *y;
  44. y = (Elf32_Rela *)_x;
  45. x = (Elf32_Rela *)_y;
  46. /* Compare the entire r_info (as opposed to ELF32_R_SYM(r_info) only) to
  47. * make the comparison cheaper/faster. It won't affect the sorting or
  48. * the counting algorithms' performance
  49. */
  50. if (x->r_info < y->r_info)
  51. return -1;
  52. else if (x->r_info > y->r_info)
  53. return 1;
  54. else if (x->r_addend < y->r_addend)
  55. return -1;
  56. else if (x->r_addend > y->r_addend)
  57. return 1;
  58. else
  59. return 0;
  60. }
  61. /* Get the potential trampolines size required of the init and
  62. non-init sections */
  63. static unsigned long get_plt_size(const Elf32_Ehdr *hdr,
  64. const Elf32_Shdr *sechdrs,
  65. const char *secstrings,
  66. int is_init)
  67. {
  68. unsigned long ret = 0;
  69. unsigned i;
  70. /* Everything marked ALLOC (this includes the exported
  71. symbols) */
  72. for (i = 1; i < hdr->e_shnum; i++) {
  73. /* If it's called *.init*, and we're not init, we're
  74. not interested */
  75. if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != NULL)
  76. != is_init)
  77. continue;
  78. /* We don't want to look at debug sections. */
  79. if (strstr(secstrings + sechdrs[i].sh_name, ".debug"))
  80. continue;
  81. if (sechdrs[i].sh_type == SHT_RELA) {
  82. pr_debug("Found relocations in section %u\n", i);
  83. pr_debug("Ptr: %p. Number: %u\n",
  84. (void *)hdr + sechdrs[i].sh_offset,
  85. sechdrs[i].sh_size / sizeof(Elf32_Rela));
  86. /* Sort the relocation information based on a symbol and
  87. * addend key. This is a stable O(n*log n) complexity
  88. * algorithm but it will reduce the complexity of
  89. * count_relocs() to linear complexity O(n)
  90. */
  91. sort((void *)hdr + sechdrs[i].sh_offset,
  92. sechdrs[i].sh_size / sizeof(Elf32_Rela),
  93. sizeof(Elf32_Rela), relacmp, NULL);
  94. ret += count_relocs((void *)hdr
  95. + sechdrs[i].sh_offset,
  96. sechdrs[i].sh_size
  97. / sizeof(Elf32_Rela))
  98. * sizeof(struct ppc_plt_entry);
  99. }
  100. }
  101. return ret;
  102. }
  103. int module_frob_arch_sections(Elf32_Ehdr *hdr,
  104. Elf32_Shdr *sechdrs,
  105. char *secstrings,
  106. struct module *me)
  107. {
  108. unsigned int i;
  109. /* Find .plt and .init.plt sections */
  110. for (i = 0; i < hdr->e_shnum; i++) {
  111. if (strcmp(secstrings + sechdrs[i].sh_name, ".init.plt") == 0)
  112. me->arch.init_plt_section = i;
  113. else if (strcmp(secstrings + sechdrs[i].sh_name, ".plt") == 0)
  114. me->arch.core_plt_section = i;
  115. }
  116. if (!me->arch.core_plt_section || !me->arch.init_plt_section) {
  117. pr_err("Module doesn't contain .plt or .init.plt sections.\n");
  118. return -ENOEXEC;
  119. }
  120. /* Override their sizes */
  121. sechdrs[me->arch.core_plt_section].sh_size
  122. = get_plt_size(hdr, sechdrs, secstrings, 0);
  123. sechdrs[me->arch.init_plt_section].sh_size
  124. = get_plt_size(hdr, sechdrs, secstrings, 1);
  125. return 0;
  126. }
  127. static inline int entry_matches(struct ppc_plt_entry *entry, Elf32_Addr val)
  128. {
  129. if (entry->jump[0] != PPC_RAW_LIS(_R12, PPC_HA(val)))
  130. return 0;
  131. if (entry->jump[1] != PPC_RAW_ADDI(_R12, _R12, PPC_LO(val)))
  132. return 0;
  133. return 1;
  134. }
  135. /* Set up a trampoline in the PLT to bounce us to the distant function */
  136. static uint32_t do_plt_call(void *location,
  137. Elf32_Addr val,
  138. const Elf32_Shdr *sechdrs,
  139. struct module *mod)
  140. {
  141. struct ppc_plt_entry *entry;
  142. pr_debug("Doing plt for call to 0x%x at 0x%x\n", val, (unsigned int)location);
  143. /* Init, or core PLT? */
  144. if (within_module_core((unsigned long)location, mod))
  145. entry = (void *)sechdrs[mod->arch.core_plt_section].sh_addr;
  146. else
  147. entry = (void *)sechdrs[mod->arch.init_plt_section].sh_addr;
  148. /* Find this entry, or if that fails, the next avail. entry */
  149. while (entry->jump[0]) {
  150. if (entry_matches(entry, val)) return (uint32_t)entry;
  151. entry++;
  152. }
  153. if (patch_instruction(&entry->jump[0], ppc_inst(PPC_RAW_LIS(_R12, PPC_HA(val)))))
  154. return 0;
  155. if (patch_instruction(&entry->jump[1], ppc_inst(PPC_RAW_ADDI(_R12, _R12, PPC_LO(val)))))
  156. return 0;
  157. if (patch_instruction(&entry->jump[2], ppc_inst(PPC_RAW_MTCTR(_R12))))
  158. return 0;
  159. if (patch_instruction(&entry->jump[3], ppc_inst(PPC_RAW_BCTR())))
  160. return 0;
  161. pr_debug("Initialized plt for 0x%x at %p\n", val, entry);
  162. return (uint32_t)entry;
  163. }
  164. static int patch_location_16(uint32_t *loc, u16 value)
  165. {
  166. loc = PTR_ALIGN_DOWN(loc, sizeof(u32));
  167. return patch_instruction(loc, ppc_inst((*loc & 0xffff0000) | value));
  168. }
  169. int apply_relocate_add(Elf32_Shdr *sechdrs,
  170. const char *strtab,
  171. unsigned int symindex,
  172. unsigned int relsec,
  173. struct module *module)
  174. {
  175. unsigned int i;
  176. Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
  177. Elf32_Sym *sym;
  178. uint32_t *location;
  179. uint32_t value;
  180. pr_debug("Applying ADD relocate section %u to %u\n", relsec,
  181. sechdrs[relsec].sh_info);
  182. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
  183. /* This is where to make the change */
  184. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  185. + rela[i].r_offset;
  186. /* This is the symbol it is referring to. Note that all
  187. undefined symbols have been resolved. */
  188. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  189. + ELF32_R_SYM(rela[i].r_info);
  190. /* `Everything is relative'. */
  191. value = sym->st_value + rela[i].r_addend;
  192. switch (ELF32_R_TYPE(rela[i].r_info)) {
  193. case R_PPC_ADDR32:
  194. /* Simply set it */
  195. *(uint32_t *)location = value;
  196. break;
  197. case R_PPC_ADDR16_LO:
  198. /* Low half of the symbol */
  199. if (patch_location_16(location, PPC_LO(value)))
  200. return -EFAULT;
  201. break;
  202. case R_PPC_ADDR16_HI:
  203. /* Higher half of the symbol */
  204. if (patch_location_16(location, PPC_HI(value)))
  205. return -EFAULT;
  206. break;
  207. case R_PPC_ADDR16_HA:
  208. if (patch_location_16(location, PPC_HA(value)))
  209. return -EFAULT;
  210. break;
  211. case R_PPC_REL24:
  212. if ((int)(value - (uint32_t)location) < -0x02000000
  213. || (int)(value - (uint32_t)location) >= 0x02000000) {
  214. value = do_plt_call(location, value,
  215. sechdrs, module);
  216. if (!value)
  217. return -EFAULT;
  218. }
  219. /* Only replace bits 2 through 26 */
  220. pr_debug("REL24 value = %08X. location = %08X\n",
  221. value, (uint32_t)location);
  222. pr_debug("Location before: %08X.\n",
  223. *(uint32_t *)location);
  224. value = (*(uint32_t *)location & ~PPC_LI_MASK) |
  225. PPC_LI(value - (uint32_t)location);
  226. if (patch_instruction(location, ppc_inst(value)))
  227. return -EFAULT;
  228. pr_debug("Location after: %08X.\n",
  229. *(uint32_t *)location);
  230. pr_debug("ie. jump to %08X+%08X = %08X\n",
  231. *(uint32_t *)PPC_LI((uint32_t)location), (uint32_t)location,
  232. (*(uint32_t *)PPC_LI((uint32_t)location)) + (uint32_t)location);
  233. break;
  234. case R_PPC_REL32:
  235. /* 32-bit relative jump. */
  236. *(uint32_t *)location = value - (uint32_t)location;
  237. break;
  238. default:
  239. pr_err("%s: unknown ADD relocation: %u\n",
  240. module->name,
  241. ELF32_R_TYPE(rela[i].r_info));
  242. return -ENOEXEC;
  243. }
  244. }
  245. return 0;
  246. }
  247. #ifdef CONFIG_DYNAMIC_FTRACE
  248. notrace int module_trampoline_target(struct module *mod, unsigned long addr,
  249. unsigned long *target)
  250. {
  251. ppc_inst_t jmp[4];
  252. /* Find where the trampoline jumps to */
  253. if (copy_inst_from_kernel_nofault(jmp, (void *)addr))
  254. return -EFAULT;
  255. if (__copy_inst_from_kernel_nofault(jmp + 1, (void *)addr + 4))
  256. return -EFAULT;
  257. if (__copy_inst_from_kernel_nofault(jmp + 2, (void *)addr + 8))
  258. return -EFAULT;
  259. if (__copy_inst_from_kernel_nofault(jmp + 3, (void *)addr + 12))
  260. return -EFAULT;
  261. /* verify that this is what we expect it to be */
  262. if ((ppc_inst_val(jmp[0]) & 0xffff0000) != PPC_RAW_LIS(_R12, 0))
  263. return -EINVAL;
  264. if ((ppc_inst_val(jmp[1]) & 0xffff0000) != PPC_RAW_ADDI(_R12, _R12, 0))
  265. return -EINVAL;
  266. if (ppc_inst_val(jmp[2]) != PPC_RAW_MTCTR(_R12))
  267. return -EINVAL;
  268. if (ppc_inst_val(jmp[3]) != PPC_RAW_BCTR())
  269. return -EINVAL;
  270. addr = (ppc_inst_val(jmp[1]) & 0xffff) | ((ppc_inst_val(jmp[0]) & 0xffff) << 16);
  271. if (addr & 0x8000)
  272. addr -= 0x10000;
  273. *target = addr;
  274. return 0;
  275. }
  276. int module_finalize_ftrace(struct module *module, const Elf_Shdr *sechdrs)
  277. {
  278. module->arch.tramp = do_plt_call(module->mem[MOD_TEXT].base,
  279. (unsigned long)ftrace_caller,
  280. sechdrs, module);
  281. if (!module->arch.tramp)
  282. return -ENOENT;
  283. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  284. module->arch.tramp_regs = do_plt_call(module->mem[MOD_TEXT].base,
  285. (unsigned long)ftrace_regs_caller,
  286. sechdrs, module);
  287. if (!module->arch.tramp_regs)
  288. return -ENOENT;
  289. #endif
  290. return 0;
  291. }
  292. #endif