module.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. * AArch64 loadable module support.
  3. *
  4. * Copyright (C) 2012 ARM Limited
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * Author: Will Deacon <will.deacon@arm.com>
  19. */
  20. #include <linux/bitops.h>
  21. #include <linux/elf.h>
  22. #include <linux/gfp.h>
  23. #include <linux/kasan.h>
  24. #include <linux/kernel.h>
  25. #include <linux/mm.h>
  26. #include <linux/moduleloader.h>
  27. #include <linux/vmalloc.h>
  28. #include <asm/alternative.h>
  29. #include <asm/insn.h>
  30. #include <asm/sections.h>
  31. void *module_alloc(unsigned long size)
  32. {
  33. u64 module_alloc_end = module_alloc_base + MODULES_VSIZE;
  34. gfp_t gfp_mask = GFP_KERNEL;
  35. void *p;
  36. /* Silence the initial allocation */
  37. if (IS_ENABLED(CONFIG_ARM64_MODULE_PLTS))
  38. gfp_mask |= __GFP_NOWARN;
  39. if (IS_ENABLED(CONFIG_KASAN))
  40. /* don't exceed the static module region - see below */
  41. module_alloc_end = MODULES_END;
  42. p = __vmalloc_node_range(size, MODULE_ALIGN, module_alloc_base,
  43. module_alloc_end, gfp_mask, PAGE_KERNEL_EXEC, 0,
  44. NUMA_NO_NODE, __builtin_return_address(0));
  45. if (!p && IS_ENABLED(CONFIG_ARM64_MODULE_PLTS) &&
  46. !IS_ENABLED(CONFIG_KASAN))
  47. /*
  48. * KASAN can only deal with module allocations being served
  49. * from the reserved module region, since the remainder of
  50. * the vmalloc region is already backed by zero shadow pages,
  51. * and punching holes into it is non-trivial. Since the module
  52. * region is not randomized when KASAN is enabled, it is even
  53. * less likely that the module region gets exhausted, so we
  54. * can simply omit this fallback in that case.
  55. */
  56. p = __vmalloc_node_range(size, MODULE_ALIGN, module_alloc_base,
  57. module_alloc_base + SZ_2G, GFP_KERNEL,
  58. PAGE_KERNEL_EXEC, 0, NUMA_NO_NODE,
  59. __builtin_return_address(0));
  60. if (p && (kasan_module_alloc(p, size) < 0)) {
  61. vfree(p);
  62. return NULL;
  63. }
  64. return p;
  65. }
  66. enum aarch64_reloc_op {
  67. RELOC_OP_NONE,
  68. RELOC_OP_ABS,
  69. RELOC_OP_PREL,
  70. RELOC_OP_PAGE,
  71. };
  72. static u64 do_reloc(enum aarch64_reloc_op reloc_op, __le32 *place, u64 val)
  73. {
  74. switch (reloc_op) {
  75. case RELOC_OP_ABS:
  76. return val;
  77. case RELOC_OP_PREL:
  78. return val - (u64)place;
  79. case RELOC_OP_PAGE:
  80. return (val & ~0xfff) - ((u64)place & ~0xfff);
  81. case RELOC_OP_NONE:
  82. return 0;
  83. }
  84. pr_err("do_reloc: unknown relocation operation %d\n", reloc_op);
  85. return 0;
  86. }
  87. static int reloc_data(enum aarch64_reloc_op op, void *place, u64 val, int len)
  88. {
  89. s64 sval = do_reloc(op, place, val);
  90. switch (len) {
  91. case 16:
  92. *(s16 *)place = sval;
  93. if (sval < S16_MIN || sval > U16_MAX)
  94. return -ERANGE;
  95. break;
  96. case 32:
  97. *(s32 *)place = sval;
  98. if (sval < S32_MIN || sval > U32_MAX)
  99. return -ERANGE;
  100. break;
  101. case 64:
  102. *(s64 *)place = sval;
  103. break;
  104. default:
  105. pr_err("Invalid length (%d) for data relocation\n", len);
  106. return 0;
  107. }
  108. return 0;
  109. }
  110. enum aarch64_insn_movw_imm_type {
  111. AARCH64_INSN_IMM_MOVNZ,
  112. AARCH64_INSN_IMM_MOVKZ,
  113. };
  114. static int reloc_insn_movw(enum aarch64_reloc_op op, __le32 *place, u64 val,
  115. int lsb, enum aarch64_insn_movw_imm_type imm_type)
  116. {
  117. u64 imm;
  118. s64 sval;
  119. u32 insn = le32_to_cpu(*place);
  120. sval = do_reloc(op, place, val);
  121. imm = sval >> lsb;
  122. if (imm_type == AARCH64_INSN_IMM_MOVNZ) {
  123. /*
  124. * For signed MOVW relocations, we have to manipulate the
  125. * instruction encoding depending on whether or not the
  126. * immediate is less than zero.
  127. */
  128. insn &= ~(3 << 29);
  129. if (sval >= 0) {
  130. /* >=0: Set the instruction to MOVZ (opcode 10b). */
  131. insn |= 2 << 29;
  132. } else {
  133. /*
  134. * <0: Set the instruction to MOVN (opcode 00b).
  135. * Since we've masked the opcode already, we
  136. * don't need to do anything other than
  137. * inverting the new immediate field.
  138. */
  139. imm = ~imm;
  140. }
  141. }
  142. /* Update the instruction with the new encoding. */
  143. insn = aarch64_insn_encode_immediate(AARCH64_INSN_IMM_16, insn, imm);
  144. *place = cpu_to_le32(insn);
  145. if (imm > U16_MAX)
  146. return -ERANGE;
  147. return 0;
  148. }
  149. static int reloc_insn_imm(enum aarch64_reloc_op op, __le32 *place, u64 val,
  150. int lsb, int len, enum aarch64_insn_imm_type imm_type)
  151. {
  152. u64 imm, imm_mask;
  153. s64 sval;
  154. u32 insn = le32_to_cpu(*place);
  155. /* Calculate the relocation value. */
  156. sval = do_reloc(op, place, val);
  157. sval >>= lsb;
  158. /* Extract the value bits and shift them to bit 0. */
  159. imm_mask = (BIT(lsb + len) - 1) >> lsb;
  160. imm = sval & imm_mask;
  161. /* Update the instruction's immediate field. */
  162. insn = aarch64_insn_encode_immediate(imm_type, insn, imm);
  163. *place = cpu_to_le32(insn);
  164. /*
  165. * Extract the upper value bits (including the sign bit) and
  166. * shift them to bit 0.
  167. */
  168. sval = (s64)(sval & ~(imm_mask >> 1)) >> (len - 1);
  169. /*
  170. * Overflow has occurred if the upper bits are not all equal to
  171. * the sign bit of the value.
  172. */
  173. if ((u64)(sval + 1) >= 2)
  174. return -ERANGE;
  175. return 0;
  176. }
  177. static int reloc_insn_adrp(struct module *mod, __le32 *place, u64 val)
  178. {
  179. u32 insn;
  180. if (!IS_ENABLED(CONFIG_ARM64_ERRATUM_843419) ||
  181. !cpus_have_const_cap(ARM64_WORKAROUND_843419) ||
  182. ((u64)place & 0xfff) < 0xff8)
  183. return reloc_insn_imm(RELOC_OP_PAGE, place, val, 12, 21,
  184. AARCH64_INSN_IMM_ADR);
  185. /* patch ADRP to ADR if it is in range */
  186. if (!reloc_insn_imm(RELOC_OP_PREL, place, val & ~0xfff, 0, 21,
  187. AARCH64_INSN_IMM_ADR)) {
  188. insn = le32_to_cpu(*place);
  189. insn &= ~BIT(31);
  190. } else {
  191. /* out of range for ADR -> emit a veneer */
  192. val = module_emit_veneer_for_adrp(mod, place, val & ~0xfff);
  193. if (!val)
  194. return -ENOEXEC;
  195. insn = aarch64_insn_gen_branch_imm((u64)place, val,
  196. AARCH64_INSN_BRANCH_NOLINK);
  197. }
  198. *place = cpu_to_le32(insn);
  199. return 0;
  200. }
  201. int apply_relocate_add(Elf64_Shdr *sechdrs,
  202. const char *strtab,
  203. unsigned int symindex,
  204. unsigned int relsec,
  205. struct module *me)
  206. {
  207. unsigned int i;
  208. int ovf;
  209. bool overflow_check;
  210. Elf64_Sym *sym;
  211. void *loc;
  212. u64 val;
  213. Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  214. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  215. /* loc corresponds to P in the AArch64 ELF document. */
  216. loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  217. + rel[i].r_offset;
  218. /* sym is the ELF symbol we're referring to. */
  219. sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
  220. + ELF64_R_SYM(rel[i].r_info);
  221. /* val corresponds to (S + A) in the AArch64 ELF document. */
  222. val = sym->st_value + rel[i].r_addend;
  223. /* Check for overflow by default. */
  224. overflow_check = true;
  225. /* Perform the static relocation. */
  226. switch (ELF64_R_TYPE(rel[i].r_info)) {
  227. /* Null relocations. */
  228. case R_ARM_NONE:
  229. case R_AARCH64_NONE:
  230. ovf = 0;
  231. break;
  232. /* Data relocations. */
  233. case R_AARCH64_ABS64:
  234. overflow_check = false;
  235. ovf = reloc_data(RELOC_OP_ABS, loc, val, 64);
  236. break;
  237. case R_AARCH64_ABS32:
  238. ovf = reloc_data(RELOC_OP_ABS, loc, val, 32);
  239. break;
  240. case R_AARCH64_ABS16:
  241. ovf = reloc_data(RELOC_OP_ABS, loc, val, 16);
  242. break;
  243. case R_AARCH64_PREL64:
  244. overflow_check = false;
  245. ovf = reloc_data(RELOC_OP_PREL, loc, val, 64);
  246. break;
  247. case R_AARCH64_PREL32:
  248. ovf = reloc_data(RELOC_OP_PREL, loc, val, 32);
  249. break;
  250. case R_AARCH64_PREL16:
  251. ovf = reloc_data(RELOC_OP_PREL, loc, val, 16);
  252. break;
  253. /* MOVW instruction relocations. */
  254. case R_AARCH64_MOVW_UABS_G0_NC:
  255. overflow_check = false;
  256. case R_AARCH64_MOVW_UABS_G0:
  257. ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
  258. AARCH64_INSN_IMM_MOVKZ);
  259. break;
  260. case R_AARCH64_MOVW_UABS_G1_NC:
  261. overflow_check = false;
  262. case R_AARCH64_MOVW_UABS_G1:
  263. ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
  264. AARCH64_INSN_IMM_MOVKZ);
  265. break;
  266. case R_AARCH64_MOVW_UABS_G2_NC:
  267. overflow_check = false;
  268. case R_AARCH64_MOVW_UABS_G2:
  269. ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
  270. AARCH64_INSN_IMM_MOVKZ);
  271. break;
  272. case R_AARCH64_MOVW_UABS_G3:
  273. /* We're using the top bits so we can't overflow. */
  274. overflow_check = false;
  275. ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 48,
  276. AARCH64_INSN_IMM_MOVKZ);
  277. break;
  278. case R_AARCH64_MOVW_SABS_G0:
  279. ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
  280. AARCH64_INSN_IMM_MOVNZ);
  281. break;
  282. case R_AARCH64_MOVW_SABS_G1:
  283. ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
  284. AARCH64_INSN_IMM_MOVNZ);
  285. break;
  286. case R_AARCH64_MOVW_SABS_G2:
  287. ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
  288. AARCH64_INSN_IMM_MOVNZ);
  289. break;
  290. case R_AARCH64_MOVW_PREL_G0_NC:
  291. overflow_check = false;
  292. ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
  293. AARCH64_INSN_IMM_MOVKZ);
  294. break;
  295. case R_AARCH64_MOVW_PREL_G0:
  296. ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
  297. AARCH64_INSN_IMM_MOVNZ);
  298. break;
  299. case R_AARCH64_MOVW_PREL_G1_NC:
  300. overflow_check = false;
  301. ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
  302. AARCH64_INSN_IMM_MOVKZ);
  303. break;
  304. case R_AARCH64_MOVW_PREL_G1:
  305. ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
  306. AARCH64_INSN_IMM_MOVNZ);
  307. break;
  308. case R_AARCH64_MOVW_PREL_G2_NC:
  309. overflow_check = false;
  310. ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
  311. AARCH64_INSN_IMM_MOVKZ);
  312. break;
  313. case R_AARCH64_MOVW_PREL_G2:
  314. ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
  315. AARCH64_INSN_IMM_MOVNZ);
  316. break;
  317. case R_AARCH64_MOVW_PREL_G3:
  318. /* We're using the top bits so we can't overflow. */
  319. overflow_check = false;
  320. ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 48,
  321. AARCH64_INSN_IMM_MOVNZ);
  322. break;
  323. /* Immediate instruction relocations. */
  324. case R_AARCH64_LD_PREL_LO19:
  325. ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
  326. AARCH64_INSN_IMM_19);
  327. break;
  328. case R_AARCH64_ADR_PREL_LO21:
  329. ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 0, 21,
  330. AARCH64_INSN_IMM_ADR);
  331. break;
  332. case R_AARCH64_ADR_PREL_PG_HI21_NC:
  333. overflow_check = false;
  334. case R_AARCH64_ADR_PREL_PG_HI21:
  335. ovf = reloc_insn_adrp(me, loc, val);
  336. if (ovf && ovf != -ERANGE)
  337. return ovf;
  338. break;
  339. case R_AARCH64_ADD_ABS_LO12_NC:
  340. case R_AARCH64_LDST8_ABS_LO12_NC:
  341. overflow_check = false;
  342. ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 0, 12,
  343. AARCH64_INSN_IMM_12);
  344. break;
  345. case R_AARCH64_LDST16_ABS_LO12_NC:
  346. overflow_check = false;
  347. ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 1, 11,
  348. AARCH64_INSN_IMM_12);
  349. break;
  350. case R_AARCH64_LDST32_ABS_LO12_NC:
  351. overflow_check = false;
  352. ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 2, 10,
  353. AARCH64_INSN_IMM_12);
  354. break;
  355. case R_AARCH64_LDST64_ABS_LO12_NC:
  356. overflow_check = false;
  357. ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 3, 9,
  358. AARCH64_INSN_IMM_12);
  359. break;
  360. case R_AARCH64_LDST128_ABS_LO12_NC:
  361. overflow_check = false;
  362. ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 4, 8,
  363. AARCH64_INSN_IMM_12);
  364. break;
  365. case R_AARCH64_TSTBR14:
  366. ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 14,
  367. AARCH64_INSN_IMM_14);
  368. break;
  369. case R_AARCH64_CONDBR19:
  370. ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
  371. AARCH64_INSN_IMM_19);
  372. break;
  373. case R_AARCH64_JUMP26:
  374. case R_AARCH64_CALL26:
  375. ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 26,
  376. AARCH64_INSN_IMM_26);
  377. if (IS_ENABLED(CONFIG_ARM64_MODULE_PLTS) &&
  378. ovf == -ERANGE) {
  379. val = module_emit_plt_entry(me, loc, &rel[i], sym);
  380. if (!val)
  381. return -ENOEXEC;
  382. ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2,
  383. 26, AARCH64_INSN_IMM_26);
  384. }
  385. break;
  386. default:
  387. pr_err("module %s: unsupported RELA relocation: %llu\n",
  388. me->name, ELF64_R_TYPE(rel[i].r_info));
  389. return -ENOEXEC;
  390. }
  391. if (overflow_check && ovf == -ERANGE)
  392. goto overflow;
  393. }
  394. return 0;
  395. overflow:
  396. pr_err("module %s: overflow in relocation type %d val %Lx\n",
  397. me->name, (int)ELF64_R_TYPE(rel[i].r_info), val);
  398. return -ENOEXEC;
  399. }
  400. int module_finalize(const Elf_Ehdr *hdr,
  401. const Elf_Shdr *sechdrs,
  402. struct module *me)
  403. {
  404. const Elf_Shdr *s, *se;
  405. const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  406. for (s = sechdrs, se = sechdrs + hdr->e_shnum; s < se; s++) {
  407. if (strcmp(".altinstructions", secstrs + s->sh_name) == 0)
  408. apply_alternatives_module((void *)s->sh_addr, s->sh_size);
  409. #ifdef CONFIG_ARM64_MODULE_PLTS
  410. if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE) &&
  411. !strcmp(".text.ftrace_trampoline", secstrs + s->sh_name))
  412. me->arch.ftrace_trampoline = (void *)s->sh_addr;
  413. #endif
  414. }
  415. return 0;
  416. }