module.c 11 KB

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