module.c 11 KB

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