module.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. void *p;
  34. p = __vmalloc_node_range(size, MODULE_ALIGN, module_alloc_base,
  35. module_alloc_base + MODULES_VSIZE,
  36. GFP_KERNEL, PAGE_KERNEL_EXEC, 0,
  37. NUMA_NO_NODE, __builtin_return_address(0));
  38. if (!p && IS_ENABLED(CONFIG_ARM64_MODULE_PLTS) &&
  39. !IS_ENABLED(CONFIG_KASAN))
  40. /*
  41. * KASAN can only deal with module allocations being served
  42. * from the reserved module region, since the remainder of
  43. * the vmalloc region is already backed by zero shadow pages,
  44. * and punching holes into it is non-trivial. Since the module
  45. * region is not randomized when KASAN is enabled, it is even
  46. * less likely that the module region gets exhausted, so we
  47. * can simply omit this fallback in that case.
  48. */
  49. p = __vmalloc_node_range(size, MODULE_ALIGN, VMALLOC_START,
  50. VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_EXEC, 0,
  51. NUMA_NO_NODE, __builtin_return_address(0));
  52. if (p && (kasan_module_alloc(p, size) < 0)) {
  53. vfree(p);
  54. return NULL;
  55. }
  56. return p;
  57. }
  58. enum aarch64_reloc_op {
  59. RELOC_OP_NONE,
  60. RELOC_OP_ABS,
  61. RELOC_OP_PREL,
  62. RELOC_OP_PAGE,
  63. };
  64. static u64 do_reloc(enum aarch64_reloc_op reloc_op, void *place, u64 val)
  65. {
  66. switch (reloc_op) {
  67. case RELOC_OP_ABS:
  68. return val;
  69. case RELOC_OP_PREL:
  70. return val - (u64)place;
  71. case RELOC_OP_PAGE:
  72. return (val & ~0xfff) - ((u64)place & ~0xfff);
  73. case RELOC_OP_NONE:
  74. return 0;
  75. }
  76. pr_err("do_reloc: unknown relocation operation %d\n", reloc_op);
  77. return 0;
  78. }
  79. static int reloc_data(enum aarch64_reloc_op op, void *place, u64 val, int len)
  80. {
  81. s64 sval = do_reloc(op, place, val);
  82. switch (len) {
  83. case 16:
  84. *(s16 *)place = sval;
  85. if (sval < S16_MIN || sval > U16_MAX)
  86. return -ERANGE;
  87. break;
  88. case 32:
  89. *(s32 *)place = sval;
  90. if (sval < S32_MIN || sval > U32_MAX)
  91. return -ERANGE;
  92. break;
  93. case 64:
  94. *(s64 *)place = sval;
  95. break;
  96. default:
  97. pr_err("Invalid length (%d) for data relocation\n", len);
  98. return 0;
  99. }
  100. return 0;
  101. }
  102. enum aarch64_insn_movw_imm_type {
  103. AARCH64_INSN_IMM_MOVNZ,
  104. AARCH64_INSN_IMM_MOVKZ,
  105. };
  106. static int reloc_insn_movw(enum aarch64_reloc_op op, void *place, u64 val,
  107. int lsb, enum aarch64_insn_movw_imm_type imm_type)
  108. {
  109. u64 imm;
  110. s64 sval;
  111. u32 insn = le32_to_cpu(*(u32 *)place);
  112. sval = do_reloc(op, place, val);
  113. imm = sval >> lsb;
  114. if (imm_type == AARCH64_INSN_IMM_MOVNZ) {
  115. /*
  116. * For signed MOVW relocations, we have to manipulate the
  117. * instruction encoding depending on whether or not the
  118. * immediate is less than zero.
  119. */
  120. insn &= ~(3 << 29);
  121. if (sval >= 0) {
  122. /* >=0: Set the instruction to MOVZ (opcode 10b). */
  123. insn |= 2 << 29;
  124. } else {
  125. /*
  126. * <0: Set the instruction to MOVN (opcode 00b).
  127. * Since we've masked the opcode already, we
  128. * don't need to do anything other than
  129. * inverting the new immediate field.
  130. */
  131. imm = ~imm;
  132. }
  133. }
  134. /* Update the instruction with the new encoding. */
  135. insn = aarch64_insn_encode_immediate(AARCH64_INSN_IMM_16, insn, imm);
  136. *(u32 *)place = cpu_to_le32(insn);
  137. if (imm > U16_MAX)
  138. return -ERANGE;
  139. return 0;
  140. }
  141. static int reloc_insn_imm(enum aarch64_reloc_op op, void *place, u64 val,
  142. int lsb, int len, enum aarch64_insn_imm_type imm_type)
  143. {
  144. u64 imm, imm_mask;
  145. s64 sval;
  146. u32 insn = le32_to_cpu(*(u32 *)place);
  147. /* Calculate the relocation value. */
  148. sval = do_reloc(op, place, val);
  149. sval >>= lsb;
  150. /* Extract the value bits and shift them to bit 0. */
  151. imm_mask = (BIT(lsb + len) - 1) >> lsb;
  152. imm = sval & imm_mask;
  153. /* Update the instruction's immediate field. */
  154. insn = aarch64_insn_encode_immediate(imm_type, insn, imm);
  155. *(u32 *)place = cpu_to_le32(insn);
  156. /*
  157. * Extract the upper value bits (including the sign bit) and
  158. * shift them to bit 0.
  159. */
  160. sval = (s64)(sval & ~(imm_mask >> 1)) >> (len - 1);
  161. /*
  162. * Overflow has occurred if the upper bits are not all equal to
  163. * the sign bit of the value.
  164. */
  165. if ((u64)(sval + 1) >= 2)
  166. return -ERANGE;
  167. return 0;
  168. }
  169. int apply_relocate_add(Elf64_Shdr *sechdrs,
  170. const char *strtab,
  171. unsigned int symindex,
  172. unsigned int relsec,
  173. struct module *me)
  174. {
  175. unsigned int i;
  176. int ovf;
  177. bool overflow_check;
  178. Elf64_Sym *sym;
  179. void *loc;
  180. u64 val;
  181. Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  182. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  183. /* loc corresponds to P in the AArch64 ELF document. */
  184. loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  185. + rel[i].r_offset;
  186. /* sym is the ELF symbol we're referring to. */
  187. sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
  188. + ELF64_R_SYM(rel[i].r_info);
  189. /* val corresponds to (S + A) in the AArch64 ELF document. */
  190. val = sym->st_value + rel[i].r_addend;
  191. /* Check for overflow by default. */
  192. overflow_check = true;
  193. /* Perform the static relocation. */
  194. switch (ELF64_R_TYPE(rel[i].r_info)) {
  195. /* Null relocations. */
  196. case R_ARM_NONE:
  197. case R_AARCH64_NONE:
  198. ovf = 0;
  199. break;
  200. /* Data relocations. */
  201. case R_AARCH64_ABS64:
  202. overflow_check = false;
  203. ovf = reloc_data(RELOC_OP_ABS, loc, val, 64);
  204. break;
  205. case R_AARCH64_ABS32:
  206. ovf = reloc_data(RELOC_OP_ABS, loc, val, 32);
  207. break;
  208. case R_AARCH64_ABS16:
  209. ovf = reloc_data(RELOC_OP_ABS, loc, val, 16);
  210. break;
  211. case R_AARCH64_PREL64:
  212. overflow_check = false;
  213. ovf = reloc_data(RELOC_OP_PREL, loc, val, 64);
  214. break;
  215. case R_AARCH64_PREL32:
  216. ovf = reloc_data(RELOC_OP_PREL, loc, val, 32);
  217. break;
  218. case R_AARCH64_PREL16:
  219. ovf = reloc_data(RELOC_OP_PREL, loc, val, 16);
  220. break;
  221. /* MOVW instruction relocations. */
  222. case R_AARCH64_MOVW_UABS_G0_NC:
  223. overflow_check = false;
  224. case R_AARCH64_MOVW_UABS_G0:
  225. ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
  226. AARCH64_INSN_IMM_MOVKZ);
  227. break;
  228. case R_AARCH64_MOVW_UABS_G1_NC:
  229. overflow_check = false;
  230. case R_AARCH64_MOVW_UABS_G1:
  231. ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
  232. AARCH64_INSN_IMM_MOVKZ);
  233. break;
  234. case R_AARCH64_MOVW_UABS_G2_NC:
  235. overflow_check = false;
  236. case R_AARCH64_MOVW_UABS_G2:
  237. ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
  238. AARCH64_INSN_IMM_MOVKZ);
  239. break;
  240. case R_AARCH64_MOVW_UABS_G3:
  241. /* We're using the top bits so we can't overflow. */
  242. overflow_check = false;
  243. ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 48,
  244. AARCH64_INSN_IMM_MOVKZ);
  245. break;
  246. case R_AARCH64_MOVW_SABS_G0:
  247. ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
  248. AARCH64_INSN_IMM_MOVNZ);
  249. break;
  250. case R_AARCH64_MOVW_SABS_G1:
  251. ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
  252. AARCH64_INSN_IMM_MOVNZ);
  253. break;
  254. case R_AARCH64_MOVW_SABS_G2:
  255. ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
  256. AARCH64_INSN_IMM_MOVNZ);
  257. break;
  258. case R_AARCH64_MOVW_PREL_G0_NC:
  259. overflow_check = false;
  260. ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
  261. AARCH64_INSN_IMM_MOVKZ);
  262. break;
  263. case R_AARCH64_MOVW_PREL_G0:
  264. ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
  265. AARCH64_INSN_IMM_MOVNZ);
  266. break;
  267. case R_AARCH64_MOVW_PREL_G1_NC:
  268. overflow_check = false;
  269. ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
  270. AARCH64_INSN_IMM_MOVKZ);
  271. break;
  272. case R_AARCH64_MOVW_PREL_G1:
  273. ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
  274. AARCH64_INSN_IMM_MOVNZ);
  275. break;
  276. case R_AARCH64_MOVW_PREL_G2_NC:
  277. overflow_check = false;
  278. ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
  279. AARCH64_INSN_IMM_MOVKZ);
  280. break;
  281. case R_AARCH64_MOVW_PREL_G2:
  282. ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
  283. AARCH64_INSN_IMM_MOVNZ);
  284. break;
  285. case R_AARCH64_MOVW_PREL_G3:
  286. /* We're using the top bits so we can't overflow. */
  287. overflow_check = false;
  288. ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 48,
  289. AARCH64_INSN_IMM_MOVNZ);
  290. break;
  291. /* Immediate instruction relocations. */
  292. case R_AARCH64_LD_PREL_LO19:
  293. ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
  294. AARCH64_INSN_IMM_19);
  295. break;
  296. case R_AARCH64_ADR_PREL_LO21:
  297. ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 0, 21,
  298. AARCH64_INSN_IMM_ADR);
  299. break;
  300. #ifndef CONFIG_ARM64_ERRATUM_843419
  301. case R_AARCH64_ADR_PREL_PG_HI21_NC:
  302. overflow_check = false;
  303. case R_AARCH64_ADR_PREL_PG_HI21:
  304. ovf = reloc_insn_imm(RELOC_OP_PAGE, loc, val, 12, 21,
  305. AARCH64_INSN_IMM_ADR);
  306. break;
  307. #endif
  308. case R_AARCH64_ADD_ABS_LO12_NC:
  309. case R_AARCH64_LDST8_ABS_LO12_NC:
  310. overflow_check = false;
  311. ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 0, 12,
  312. AARCH64_INSN_IMM_12);
  313. break;
  314. case R_AARCH64_LDST16_ABS_LO12_NC:
  315. overflow_check = false;
  316. ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 1, 11,
  317. AARCH64_INSN_IMM_12);
  318. break;
  319. case R_AARCH64_LDST32_ABS_LO12_NC:
  320. overflow_check = false;
  321. ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 2, 10,
  322. AARCH64_INSN_IMM_12);
  323. break;
  324. case R_AARCH64_LDST64_ABS_LO12_NC:
  325. overflow_check = false;
  326. ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 3, 9,
  327. AARCH64_INSN_IMM_12);
  328. break;
  329. case R_AARCH64_LDST128_ABS_LO12_NC:
  330. overflow_check = false;
  331. ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 4, 8,
  332. AARCH64_INSN_IMM_12);
  333. break;
  334. case R_AARCH64_TSTBR14:
  335. ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 14,
  336. AARCH64_INSN_IMM_14);
  337. break;
  338. case R_AARCH64_CONDBR19:
  339. ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
  340. AARCH64_INSN_IMM_19);
  341. break;
  342. case R_AARCH64_JUMP26:
  343. case R_AARCH64_CALL26:
  344. ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 26,
  345. AARCH64_INSN_IMM_26);
  346. if (IS_ENABLED(CONFIG_ARM64_MODULE_PLTS) &&
  347. ovf == -ERANGE) {
  348. val = module_emit_plt_entry(me, &rel[i], sym);
  349. ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2,
  350. 26, AARCH64_INSN_IMM_26);
  351. }
  352. break;
  353. default:
  354. pr_err("module %s: unsupported RELA relocation: %llu\n",
  355. me->name, ELF64_R_TYPE(rel[i].r_info));
  356. return -ENOEXEC;
  357. }
  358. if (overflow_check && ovf == -ERANGE)
  359. goto overflow;
  360. }
  361. return 0;
  362. overflow:
  363. pr_err("module %s: overflow in relocation type %d val %Lx\n",
  364. me->name, (int)ELF64_R_TYPE(rel[i].r_info), val);
  365. return -ENOEXEC;
  366. }
  367. int module_finalize(const Elf_Ehdr *hdr,
  368. const Elf_Shdr *sechdrs,
  369. struct module *me)
  370. {
  371. const Elf_Shdr *s, *se;
  372. const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  373. for (s = sechdrs, se = sechdrs + hdr->e_shnum; s < se; s++) {
  374. if (strcmp(".altinstructions", secstrs + s->sh_name) == 0) {
  375. apply_alternatives((void *)s->sh_addr, s->sh_size);
  376. return 0;
  377. }
  378. }
  379. return 0;
  380. }