module.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Author: Hanlu Li <lihanlu@loongson.cn>
  4. * Huacai Chen <chenhuacai@loongson.cn>
  5. *
  6. * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
  7. */
  8. #define pr_fmt(fmt) "kmod: " fmt
  9. #include <linux/moduleloader.h>
  10. #include <linux/elf.h>
  11. #include <linux/mm.h>
  12. #include <linux/numa.h>
  13. #include <linux/vmalloc.h>
  14. #include <linux/slab.h>
  15. #include <linux/fs.h>
  16. #include <linux/ftrace.h>
  17. #include <linux/string.h>
  18. #include <linux/kernel.h>
  19. #include <asm/alternative.h>
  20. #include <asm/inst.h>
  21. #include <asm/unwind.h>
  22. static int rela_stack_push(s64 stack_value, s64 *rela_stack, size_t *rela_stack_top)
  23. {
  24. if (*rela_stack_top >= RELA_STACK_DEPTH)
  25. return -ENOEXEC;
  26. rela_stack[(*rela_stack_top)++] = stack_value;
  27. pr_debug("%s stack_value = 0x%llx\n", __func__, stack_value);
  28. return 0;
  29. }
  30. static int rela_stack_pop(s64 *stack_value, s64 *rela_stack, size_t *rela_stack_top)
  31. {
  32. if (*rela_stack_top == 0)
  33. return -ENOEXEC;
  34. *stack_value = rela_stack[--(*rela_stack_top)];
  35. pr_debug("%s stack_value = 0x%llx\n", __func__, *stack_value);
  36. return 0;
  37. }
  38. static int apply_r_larch_none(struct module *mod, u32 *location, Elf_Addr v,
  39. s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
  40. {
  41. return 0;
  42. }
  43. static int apply_r_larch_error(struct module *me, u32 *location, Elf_Addr v,
  44. s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
  45. {
  46. pr_err("%s: Unsupport relocation type %u, please add its support.\n", me->name, type);
  47. return -EINVAL;
  48. }
  49. static int apply_r_larch_32(struct module *mod, u32 *location, Elf_Addr v,
  50. s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
  51. {
  52. *location = v;
  53. return 0;
  54. }
  55. static int apply_r_larch_64(struct module *mod, u32 *location, Elf_Addr v,
  56. s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
  57. {
  58. *(Elf_Addr *)location = v;
  59. return 0;
  60. }
  61. static int apply_r_larch_sop_push_pcrel(struct module *mod, u32 *location, Elf_Addr v,
  62. s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
  63. {
  64. return rela_stack_push(v - (u64)location, rela_stack, rela_stack_top);
  65. }
  66. static int apply_r_larch_sop_push_absolute(struct module *mod, u32 *location, Elf_Addr v,
  67. s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
  68. {
  69. return rela_stack_push(v, rela_stack, rela_stack_top);
  70. }
  71. static int apply_r_larch_sop_push_dup(struct module *mod, u32 *location, Elf_Addr v,
  72. s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
  73. {
  74. int err = 0;
  75. s64 opr1;
  76. err = rela_stack_pop(&opr1, rela_stack, rela_stack_top);
  77. if (err)
  78. return err;
  79. err = rela_stack_push(opr1, rela_stack, rela_stack_top);
  80. if (err)
  81. return err;
  82. err = rela_stack_push(opr1, rela_stack, rela_stack_top);
  83. if (err)
  84. return err;
  85. return 0;
  86. }
  87. static int apply_r_larch_sop_push_plt_pcrel(struct module *mod,
  88. Elf_Shdr *sechdrs, u32 *location, Elf_Addr v,
  89. s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
  90. {
  91. ptrdiff_t offset = (void *)v - (void *)location;
  92. if (offset >= SZ_128M)
  93. v = module_emit_plt_entry(mod, sechdrs, v);
  94. if (offset < -SZ_128M)
  95. v = module_emit_plt_entry(mod, sechdrs, v);
  96. return apply_r_larch_sop_push_pcrel(mod, location, v, rela_stack, rela_stack_top, type);
  97. }
  98. static int apply_r_larch_sop(struct module *mod, u32 *location, Elf_Addr v,
  99. s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
  100. {
  101. int err = 0;
  102. s64 opr1, opr2, opr3;
  103. if (type == R_LARCH_SOP_IF_ELSE) {
  104. err = rela_stack_pop(&opr3, rela_stack, rela_stack_top);
  105. if (err)
  106. return err;
  107. }
  108. err = rela_stack_pop(&opr2, rela_stack, rela_stack_top);
  109. if (err)
  110. return err;
  111. err = rela_stack_pop(&opr1, rela_stack, rela_stack_top);
  112. if (err)
  113. return err;
  114. switch (type) {
  115. case R_LARCH_SOP_AND:
  116. err = rela_stack_push(opr1 & opr2, rela_stack, rela_stack_top);
  117. break;
  118. case R_LARCH_SOP_ADD:
  119. err = rela_stack_push(opr1 + opr2, rela_stack, rela_stack_top);
  120. break;
  121. case R_LARCH_SOP_SUB:
  122. err = rela_stack_push(opr1 - opr2, rela_stack, rela_stack_top);
  123. break;
  124. case R_LARCH_SOP_SL:
  125. err = rela_stack_push(opr1 << opr2, rela_stack, rela_stack_top);
  126. break;
  127. case R_LARCH_SOP_SR:
  128. err = rela_stack_push(opr1 >> opr2, rela_stack, rela_stack_top);
  129. break;
  130. case R_LARCH_SOP_IF_ELSE:
  131. err = rela_stack_push(opr1 ? opr2 : opr3, rela_stack, rela_stack_top);
  132. break;
  133. default:
  134. pr_err("%s: Unsupport relocation type %u\n", mod->name, type);
  135. return -EINVAL;
  136. }
  137. return err;
  138. }
  139. static int apply_r_larch_sop_imm_field(struct module *mod, u32 *location, Elf_Addr v,
  140. s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
  141. {
  142. int err = 0;
  143. s64 opr1;
  144. union loongarch_instruction *insn = (union loongarch_instruction *)location;
  145. err = rela_stack_pop(&opr1, rela_stack, rela_stack_top);
  146. if (err)
  147. return err;
  148. switch (type) {
  149. case R_LARCH_SOP_POP_32_U_10_12:
  150. if (!unsigned_imm_check(opr1, 12))
  151. goto overflow;
  152. /* (*(uint32_t *) PC) [21 ... 10] = opr [11 ... 0] */
  153. insn->reg2i12_format.immediate = opr1 & 0xfff;
  154. return 0;
  155. case R_LARCH_SOP_POP_32_S_10_12:
  156. if (!signed_imm_check(opr1, 12))
  157. goto overflow;
  158. insn->reg2i12_format.immediate = opr1 & 0xfff;
  159. return 0;
  160. case R_LARCH_SOP_POP_32_S_10_16:
  161. if (!signed_imm_check(opr1, 16))
  162. goto overflow;
  163. insn->reg2i16_format.immediate = opr1 & 0xffff;
  164. return 0;
  165. case R_LARCH_SOP_POP_32_S_10_16_S2:
  166. if (opr1 % 4)
  167. goto unaligned;
  168. if (!signed_imm_check(opr1, 18))
  169. goto overflow;
  170. insn->reg2i16_format.immediate = (opr1 >> 2) & 0xffff;
  171. return 0;
  172. case R_LARCH_SOP_POP_32_S_5_20:
  173. if (!signed_imm_check(opr1, 20))
  174. goto overflow;
  175. insn->reg1i20_format.immediate = (opr1) & 0xfffff;
  176. return 0;
  177. case R_LARCH_SOP_POP_32_S_0_5_10_16_S2:
  178. if (opr1 % 4)
  179. goto unaligned;
  180. if (!signed_imm_check(opr1, 23))
  181. goto overflow;
  182. opr1 >>= 2;
  183. insn->reg1i21_format.immediate_l = opr1 & 0xffff;
  184. insn->reg1i21_format.immediate_h = (opr1 >> 16) & 0x1f;
  185. return 0;
  186. case R_LARCH_SOP_POP_32_S_0_10_10_16_S2:
  187. if (opr1 % 4)
  188. goto unaligned;
  189. if (!signed_imm_check(opr1, 28))
  190. goto overflow;
  191. opr1 >>= 2;
  192. insn->reg0i26_format.immediate_l = opr1 & 0xffff;
  193. insn->reg0i26_format.immediate_h = (opr1 >> 16) & 0x3ff;
  194. return 0;
  195. case R_LARCH_SOP_POP_32_U:
  196. if (!unsigned_imm_check(opr1, 32))
  197. goto overflow;
  198. /* (*(uint32_t *) PC) = opr */
  199. *location = (u32)opr1;
  200. return 0;
  201. default:
  202. pr_err("%s: Unsupport relocation type %u\n", mod->name, type);
  203. return -EINVAL;
  204. }
  205. overflow:
  206. pr_err("module %s: opr1 = 0x%llx overflow! dangerous %s (%u) relocation\n",
  207. mod->name, opr1, __func__, type);
  208. return -ENOEXEC;
  209. unaligned:
  210. pr_err("module %s: opr1 = 0x%llx unaligned! dangerous %s (%u) relocation\n",
  211. mod->name, opr1, __func__, type);
  212. return -ENOEXEC;
  213. }
  214. static int apply_r_larch_add_sub(struct module *mod, u32 *location, Elf_Addr v,
  215. s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
  216. {
  217. switch (type) {
  218. case R_LARCH_ADD32:
  219. *(s32 *)location += v;
  220. return 0;
  221. case R_LARCH_ADD64:
  222. *(s64 *)location += v;
  223. return 0;
  224. case R_LARCH_SUB32:
  225. *(s32 *)location -= v;
  226. return 0;
  227. case R_LARCH_SUB64:
  228. *(s64 *)location -= v;
  229. return 0;
  230. default:
  231. pr_err("%s: Unsupport relocation type %u\n", mod->name, type);
  232. return -EINVAL;
  233. }
  234. }
  235. static int apply_r_larch_b26(struct module *mod,
  236. Elf_Shdr *sechdrs, u32 *location, Elf_Addr v,
  237. s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
  238. {
  239. ptrdiff_t offset = (void *)v - (void *)location;
  240. union loongarch_instruction *insn = (union loongarch_instruction *)location;
  241. if (offset >= SZ_128M)
  242. v = module_emit_plt_entry(mod, sechdrs, v);
  243. if (offset < -SZ_128M)
  244. v = module_emit_plt_entry(mod, sechdrs, v);
  245. offset = (void *)v - (void *)location;
  246. if (offset & 3) {
  247. pr_err("module %s: jump offset = 0x%llx unaligned! dangerous R_LARCH_B26 (%u) relocation\n",
  248. mod->name, (long long)offset, type);
  249. return -ENOEXEC;
  250. }
  251. if (!signed_imm_check(offset, 28)) {
  252. pr_err("module %s: jump offset = 0x%llx overflow! dangerous R_LARCH_B26 (%u) relocation\n",
  253. mod->name, (long long)offset, type);
  254. return -ENOEXEC;
  255. }
  256. offset >>= 2;
  257. insn->reg0i26_format.immediate_l = offset & 0xffff;
  258. insn->reg0i26_format.immediate_h = (offset >> 16) & 0x3ff;
  259. return 0;
  260. }
  261. static int apply_r_larch_pcala(struct module *mod, u32 *location, Elf_Addr v,
  262. s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
  263. {
  264. union loongarch_instruction *insn = (union loongarch_instruction *)location;
  265. /* Use s32 for a sign-extension deliberately. */
  266. s32 offset_hi20 = (void *)((v + 0x800) & ~0xfff) -
  267. (void *)((Elf_Addr)location & ~0xfff);
  268. Elf_Addr anchor = (((Elf_Addr)location) & ~0xfff) + offset_hi20;
  269. ptrdiff_t offset_rem = (void *)v - (void *)anchor;
  270. switch (type) {
  271. case R_LARCH_PCALA_LO12:
  272. insn->reg2i12_format.immediate = v & 0xfff;
  273. break;
  274. case R_LARCH_PCALA_HI20:
  275. v = offset_hi20 >> 12;
  276. insn->reg1i20_format.immediate = v & 0xfffff;
  277. break;
  278. case R_LARCH_PCALA64_LO20:
  279. v = offset_rem >> 32;
  280. insn->reg1i20_format.immediate = v & 0xfffff;
  281. break;
  282. case R_LARCH_PCALA64_HI12:
  283. v = offset_rem >> 52;
  284. insn->reg2i12_format.immediate = v & 0xfff;
  285. break;
  286. default:
  287. pr_err("%s: Unsupport relocation type %u\n", mod->name, type);
  288. return -EINVAL;
  289. }
  290. return 0;
  291. }
  292. static int apply_r_larch_got_pc(struct module *mod,
  293. Elf_Shdr *sechdrs, u32 *location, Elf_Addr v,
  294. s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
  295. {
  296. Elf_Addr got = module_emit_got_entry(mod, sechdrs, v);
  297. if (!got)
  298. return -EINVAL;
  299. switch (type) {
  300. case R_LARCH_GOT_PC_LO12:
  301. type = R_LARCH_PCALA_LO12;
  302. break;
  303. case R_LARCH_GOT_PC_HI20:
  304. type = R_LARCH_PCALA_HI20;
  305. break;
  306. default:
  307. pr_err("%s: Unsupport relocation type %u\n", mod->name, type);
  308. return -EINVAL;
  309. }
  310. return apply_r_larch_pcala(mod, location, got, rela_stack, rela_stack_top, type);
  311. }
  312. static int apply_r_larch_32_pcrel(struct module *mod, u32 *location, Elf_Addr v,
  313. s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
  314. {
  315. ptrdiff_t offset = (void *)v - (void *)location;
  316. *(u32 *)location = offset;
  317. return 0;
  318. }
  319. static int apply_r_larch_64_pcrel(struct module *mod, u32 *location, Elf_Addr v,
  320. s64 *rela_stack, size_t *rela_stack_top, unsigned int type)
  321. {
  322. ptrdiff_t offset = (void *)v - (void *)location;
  323. *(u64 *)location = offset;
  324. return 0;
  325. }
  326. /*
  327. * reloc_handlers_rela() - Apply a particular relocation to a module
  328. * @mod: the module to apply the reloc to
  329. * @location: the address at which the reloc is to be applied
  330. * @v: the value of the reloc, with addend for RELA-style
  331. * @rela_stack: the stack used for store relocation info, LOCAL to THIS module
  332. * @rela_stac_top: where the stack operation(pop/push) applies to
  333. *
  334. * Return: 0 upon success, else -ERRNO
  335. */
  336. typedef int (*reloc_rela_handler)(struct module *mod, u32 *location, Elf_Addr v,
  337. s64 *rela_stack, size_t *rela_stack_top, unsigned int type);
  338. /* The handlers for known reloc types */
  339. static reloc_rela_handler reloc_rela_handlers[] = {
  340. [R_LARCH_NONE ... R_LARCH_64_PCREL] = apply_r_larch_error,
  341. [R_LARCH_NONE] = apply_r_larch_none,
  342. [R_LARCH_32] = apply_r_larch_32,
  343. [R_LARCH_64] = apply_r_larch_64,
  344. [R_LARCH_MARK_LA] = apply_r_larch_none,
  345. [R_LARCH_MARK_PCREL] = apply_r_larch_none,
  346. [R_LARCH_SOP_PUSH_PCREL] = apply_r_larch_sop_push_pcrel,
  347. [R_LARCH_SOP_PUSH_ABSOLUTE] = apply_r_larch_sop_push_absolute,
  348. [R_LARCH_SOP_PUSH_DUP] = apply_r_larch_sop_push_dup,
  349. [R_LARCH_SOP_SUB ... R_LARCH_SOP_IF_ELSE] = apply_r_larch_sop,
  350. [R_LARCH_SOP_POP_32_S_10_5 ... R_LARCH_SOP_POP_32_U] = apply_r_larch_sop_imm_field,
  351. [R_LARCH_ADD32 ... R_LARCH_SUB64] = apply_r_larch_add_sub,
  352. [R_LARCH_PCALA_HI20...R_LARCH_PCALA64_HI12] = apply_r_larch_pcala,
  353. [R_LARCH_32_PCREL] = apply_r_larch_32_pcrel,
  354. [R_LARCH_64_PCREL] = apply_r_larch_64_pcrel,
  355. };
  356. int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
  357. unsigned int symindex, unsigned int relsec,
  358. struct module *mod)
  359. {
  360. int i, err;
  361. unsigned int type;
  362. s64 rela_stack[RELA_STACK_DEPTH];
  363. size_t rela_stack_top = 0;
  364. reloc_rela_handler handler;
  365. void *location;
  366. Elf_Addr v;
  367. Elf_Sym *sym;
  368. Elf_Rela *rel = (void *) sechdrs[relsec].sh_addr;
  369. pr_debug("%s: Applying relocate section %u to %u\n", __func__, relsec,
  370. sechdrs[relsec].sh_info);
  371. rela_stack_top = 0;
  372. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  373. /* This is where to make the change */
  374. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr + rel[i].r_offset;
  375. /* This is the symbol it is referring to */
  376. sym = (Elf_Sym *)sechdrs[symindex].sh_addr + ELF_R_SYM(rel[i].r_info);
  377. if (IS_ERR_VALUE(sym->st_value)) {
  378. /* Ignore unresolved weak symbol */
  379. if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
  380. continue;
  381. pr_warn("%s: Unknown symbol %s\n", mod->name, strtab + sym->st_name);
  382. return -ENOENT;
  383. }
  384. type = ELF_R_TYPE(rel[i].r_info);
  385. if (type < ARRAY_SIZE(reloc_rela_handlers))
  386. handler = reloc_rela_handlers[type];
  387. else
  388. handler = NULL;
  389. if (!handler) {
  390. pr_err("%s: Unknown relocation type %u\n", mod->name, type);
  391. return -EINVAL;
  392. }
  393. pr_debug("type %d st_value %llx r_addend %llx loc %llx\n",
  394. (int)ELF_R_TYPE(rel[i].r_info),
  395. sym->st_value, rel[i].r_addend, (u64)location);
  396. v = sym->st_value + rel[i].r_addend;
  397. switch (type) {
  398. case R_LARCH_B26:
  399. err = apply_r_larch_b26(mod, sechdrs, location,
  400. v, rela_stack, &rela_stack_top, type);
  401. break;
  402. case R_LARCH_GOT_PC_HI20...R_LARCH_GOT_PC_LO12:
  403. err = apply_r_larch_got_pc(mod, sechdrs, location,
  404. v, rela_stack, &rela_stack_top, type);
  405. break;
  406. case R_LARCH_SOP_PUSH_PLT_PCREL:
  407. err = apply_r_larch_sop_push_plt_pcrel(mod, sechdrs, location,
  408. v, rela_stack, &rela_stack_top, type);
  409. break;
  410. default:
  411. err = handler(mod, location, v, rela_stack, &rela_stack_top, type);
  412. }
  413. if (err)
  414. return err;
  415. }
  416. return 0;
  417. }
  418. static void module_init_ftrace_plt(const Elf_Ehdr *hdr,
  419. const Elf_Shdr *sechdrs, struct module *mod)
  420. {
  421. #ifdef CONFIG_DYNAMIC_FTRACE
  422. struct plt_entry *ftrace_plts;
  423. ftrace_plts = (void *)sechdrs->sh_addr;
  424. ftrace_plts[FTRACE_PLT_IDX] = emit_plt_entry(FTRACE_ADDR);
  425. if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS))
  426. ftrace_plts[FTRACE_REGS_PLT_IDX] = emit_plt_entry(FTRACE_REGS_ADDR);
  427. mod->arch.ftrace_trampolines = ftrace_plts;
  428. #endif
  429. }
  430. int module_finalize(const Elf_Ehdr *hdr,
  431. const Elf_Shdr *sechdrs, struct module *mod)
  432. {
  433. const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  434. const Elf_Shdr *s, *alt = NULL, *orc = NULL, *orc_ip = NULL, *ftrace = NULL;
  435. for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
  436. if (!strcmp(".altinstructions", secstrs + s->sh_name))
  437. alt = s;
  438. if (!strcmp(".orc_unwind", secstrs + s->sh_name))
  439. orc = s;
  440. if (!strcmp(".orc_unwind_ip", secstrs + s->sh_name))
  441. orc_ip = s;
  442. if (!strcmp(".ftrace_trampoline", secstrs + s->sh_name))
  443. ftrace = s;
  444. }
  445. if (alt)
  446. apply_alternatives((void *)alt->sh_addr, (void *)alt->sh_addr + alt->sh_size);
  447. if (orc && orc_ip)
  448. unwind_module_init(mod, (void *)orc_ip->sh_addr, orc_ip->sh_size, (void *)orc->sh_addr, orc->sh_size);
  449. if (ftrace)
  450. module_init_ftrace_plt(hdr, ftrace, mod);
  451. return 0;
  452. }