module_32.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 (location >= mod->core_layout.base
  145. && location < mod->core_layout.base + mod->core_layout.size)
  146. entry = (void *)sechdrs[mod->arch.core_plt_section].sh_addr;
  147. else
  148. entry = (void *)sechdrs[mod->arch.init_plt_section].sh_addr;
  149. /* Find this entry, or if that fails, the next avail. entry */
  150. while (entry->jump[0]) {
  151. if (entry_matches(entry, val)) return (uint32_t)entry;
  152. entry++;
  153. }
  154. if (patch_instruction(&entry->jump[0], ppc_inst(PPC_RAW_LIS(_R12, PPC_HA(val)))))
  155. return 0;
  156. if (patch_instruction(&entry->jump[1], ppc_inst(PPC_RAW_ADDI(_R12, _R12, PPC_LO(val)))))
  157. return 0;
  158. if (patch_instruction(&entry->jump[2], ppc_inst(PPC_RAW_MTCTR(_R12))))
  159. return 0;
  160. if (patch_instruction(&entry->jump[3], ppc_inst(PPC_RAW_BCTR())))
  161. return 0;
  162. pr_debug("Initialized plt for 0x%x at %p\n", val, entry);
  163. return (uint32_t)entry;
  164. }
  165. static int patch_location_16(uint32_t *loc, u16 value)
  166. {
  167. loc = PTR_ALIGN_DOWN(loc, sizeof(u32));
  168. return patch_instruction(loc, ppc_inst((*loc & 0xffff0000) | value));
  169. }
  170. int apply_relocate_add(Elf32_Shdr *sechdrs,
  171. const char *strtab,
  172. unsigned int symindex,
  173. unsigned int relsec,
  174. struct module *module)
  175. {
  176. unsigned int i;
  177. Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
  178. Elf32_Sym *sym;
  179. uint32_t *location;
  180. uint32_t value;
  181. pr_debug("Applying ADD relocate section %u to %u\n", relsec,
  182. sechdrs[relsec].sh_info);
  183. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
  184. /* This is where to make the change */
  185. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  186. + rela[i].r_offset;
  187. /* This is the symbol it is referring to. Note that all
  188. undefined symbols have been resolved. */
  189. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  190. + ELF32_R_SYM(rela[i].r_info);
  191. /* `Everything is relative'. */
  192. value = sym->st_value + rela[i].r_addend;
  193. switch (ELF32_R_TYPE(rela[i].r_info)) {
  194. case R_PPC_ADDR32:
  195. /* Simply set it */
  196. *(uint32_t *)location = value;
  197. break;
  198. case R_PPC_ADDR16_LO:
  199. /* Low half of the symbol */
  200. if (patch_location_16(location, PPC_LO(value)))
  201. return -EFAULT;
  202. break;
  203. case R_PPC_ADDR16_HI:
  204. /* Higher half of the symbol */
  205. if (patch_location_16(location, PPC_HI(value)))
  206. return -EFAULT;
  207. break;
  208. case R_PPC_ADDR16_HA:
  209. if (patch_location_16(location, PPC_HA(value)))
  210. return -EFAULT;
  211. break;
  212. case R_PPC_REL24:
  213. if ((int)(value - (uint32_t)location) < -0x02000000
  214. || (int)(value - (uint32_t)location) >= 0x02000000) {
  215. value = do_plt_call(location, value,
  216. sechdrs, module);
  217. if (!value)
  218. return -EFAULT;
  219. }
  220. /* Only replace bits 2 through 26 */
  221. pr_debug("REL24 value = %08X. location = %08X\n",
  222. value, (uint32_t)location);
  223. pr_debug("Location before: %08X.\n",
  224. *(uint32_t *)location);
  225. value = (*(uint32_t *)location & ~PPC_LI_MASK) |
  226. PPC_LI(value - (uint32_t)location);
  227. if (patch_instruction(location, ppc_inst(value)))
  228. return -EFAULT;
  229. pr_debug("Location after: %08X.\n",
  230. *(uint32_t *)location);
  231. pr_debug("ie. jump to %08X+%08X = %08X\n",
  232. *(uint32_t *)PPC_LI((uint32_t)location), (uint32_t)location,
  233. (*(uint32_t *)PPC_LI((uint32_t)location)) + (uint32_t)location);
  234. break;
  235. case R_PPC_REL32:
  236. /* 32-bit relative jump. */
  237. *(uint32_t *)location = value - (uint32_t)location;
  238. break;
  239. default:
  240. pr_err("%s: unknown ADD relocation: %u\n",
  241. module->name,
  242. ELF32_R_TYPE(rela[i].r_info));
  243. return -ENOEXEC;
  244. }
  245. }
  246. return 0;
  247. }
  248. #ifdef CONFIG_DYNAMIC_FTRACE
  249. notrace int module_trampoline_target(struct module *mod, unsigned long addr,
  250. unsigned long *target)
  251. {
  252. ppc_inst_t jmp[4];
  253. /* Find where the trampoline jumps to */
  254. if (copy_inst_from_kernel_nofault(jmp, (void *)addr))
  255. return -EFAULT;
  256. if (__copy_inst_from_kernel_nofault(jmp + 1, (void *)addr + 4))
  257. return -EFAULT;
  258. if (__copy_inst_from_kernel_nofault(jmp + 2, (void *)addr + 8))
  259. return -EFAULT;
  260. if (__copy_inst_from_kernel_nofault(jmp + 3, (void *)addr + 12))
  261. return -EFAULT;
  262. /* verify that this is what we expect it to be */
  263. if ((ppc_inst_val(jmp[0]) & 0xffff0000) != PPC_RAW_LIS(_R12, 0))
  264. return -EINVAL;
  265. if ((ppc_inst_val(jmp[1]) & 0xffff0000) != PPC_RAW_ADDI(_R12, _R12, 0))
  266. return -EINVAL;
  267. if (ppc_inst_val(jmp[2]) != PPC_RAW_MTCTR(_R12))
  268. return -EINVAL;
  269. if (ppc_inst_val(jmp[3]) != PPC_RAW_BCTR())
  270. return -EINVAL;
  271. addr = (ppc_inst_val(jmp[1]) & 0xffff) | ((ppc_inst_val(jmp[0]) & 0xffff) << 16);
  272. if (addr & 0x8000)
  273. addr -= 0x10000;
  274. *target = addr;
  275. return 0;
  276. }
  277. int module_finalize_ftrace(struct module *module, const Elf_Shdr *sechdrs)
  278. {
  279. module->arch.tramp = do_plt_call(module->core_layout.base,
  280. (unsigned long)ftrace_caller,
  281. sechdrs, module);
  282. if (!module->arch.tramp)
  283. return -ENOENT;
  284. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  285. module->arch.tramp_regs = do_plt_call(module->core_layout.base,
  286. (unsigned long)ftrace_regs_caller,
  287. sechdrs, module);
  288. if (!module->arch.tramp_regs)
  289. return -ENOENT;
  290. #endif
  291. return 0;
  292. }
  293. #endif