module.c 10 KB

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