module.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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/alternative.h>
  28. #include <asm/insn.h>
  29. #include <asm/sections.h>
  30. #define AARCH64_INSN_IMM_MOVNZ AARCH64_INSN_IMM_MAX
  31. #define AARCH64_INSN_IMM_MOVK AARCH64_INSN_IMM_16
  32. void *module_alloc(unsigned long size)
  33. {
  34. return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END,
  35. GFP_KERNEL, PAGE_KERNEL_EXEC, 0,
  36. NUMA_NO_NODE, __builtin_return_address(0));
  37. }
  38. enum aarch64_reloc_op {
  39. RELOC_OP_NONE,
  40. RELOC_OP_ABS,
  41. RELOC_OP_PREL,
  42. RELOC_OP_PAGE,
  43. };
  44. static u64 do_reloc(enum aarch64_reloc_op reloc_op, void *place, u64 val)
  45. {
  46. switch (reloc_op) {
  47. case RELOC_OP_ABS:
  48. return val;
  49. case RELOC_OP_PREL:
  50. return val - (u64)place;
  51. case RELOC_OP_PAGE:
  52. return (val & ~0xfff) - ((u64)place & ~0xfff);
  53. case RELOC_OP_NONE:
  54. return 0;
  55. }
  56. pr_err("do_reloc: unknown relocation operation %d\n", reloc_op);
  57. return 0;
  58. }
  59. static int reloc_data(enum aarch64_reloc_op op, void *place, u64 val, int len)
  60. {
  61. u64 imm_mask = (1 << len) - 1;
  62. s64 sval = do_reloc(op, place, val);
  63. switch (len) {
  64. case 16:
  65. *(s16 *)place = sval;
  66. break;
  67. case 32:
  68. *(s32 *)place = sval;
  69. break;
  70. case 64:
  71. *(s64 *)place = sval;
  72. break;
  73. default:
  74. pr_err("Invalid length (%d) for data relocation\n", len);
  75. return 0;
  76. }
  77. /*
  78. * Extract the upper value bits (including the sign bit) and
  79. * shift them to bit 0.
  80. */
  81. sval = (s64)(sval & ~(imm_mask >> 1)) >> (len - 1);
  82. /*
  83. * Overflow has occurred if the value is not representable in
  84. * len bits (i.e the bottom len bits are not sign-extended and
  85. * the top bits are not all zero).
  86. */
  87. if ((u64)(sval + 1) > 2)
  88. return -ERANGE;
  89. return 0;
  90. }
  91. static int reloc_insn_movw(enum aarch64_reloc_op op, void *place, u64 val,
  92. int lsb, enum aarch64_insn_imm_type imm_type)
  93. {
  94. u64 imm, limit = 0;
  95. s64 sval;
  96. u32 insn = le32_to_cpu(*(u32 *)place);
  97. sval = do_reloc(op, place, val);
  98. sval >>= lsb;
  99. imm = sval & 0xffff;
  100. if (imm_type == AARCH64_INSN_IMM_MOVNZ) {
  101. /*
  102. * For signed MOVW relocations, we have to manipulate the
  103. * instruction encoding depending on whether or not the
  104. * immediate is less than zero.
  105. */
  106. insn &= ~(3 << 29);
  107. if ((s64)imm >= 0) {
  108. /* >=0: Set the instruction to MOVZ (opcode 10b). */
  109. insn |= 2 << 29;
  110. } else {
  111. /*
  112. * <0: Set the instruction to MOVN (opcode 00b).
  113. * Since we've masked the opcode already, we
  114. * don't need to do anything other than
  115. * inverting the new immediate field.
  116. */
  117. imm = ~imm;
  118. }
  119. imm_type = AARCH64_INSN_IMM_MOVK;
  120. }
  121. /* Update the instruction with the new encoding. */
  122. insn = aarch64_insn_encode_immediate(imm_type, insn, imm);
  123. *(u32 *)place = cpu_to_le32(insn);
  124. /* Shift out the immediate field. */
  125. sval >>= 16;
  126. /*
  127. * For unsigned immediates, the overflow check is straightforward.
  128. * For signed immediates, the sign bit is actually the bit past the
  129. * most significant bit of the field.
  130. * The AARCH64_INSN_IMM_16 immediate type is unsigned.
  131. */
  132. if (imm_type != AARCH64_INSN_IMM_16) {
  133. sval++;
  134. limit++;
  135. }
  136. /* Check the upper bits depending on the sign of the immediate. */
  137. if ((u64)sval > limit)
  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_16);
  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_16);
  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_16);
  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_16);
  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_MOVK);
  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_MOVK);
  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_MOVK);
  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. case R_AARCH64_ADR_PREL_PG_HI21_NC:
  301. overflow_check = false;
  302. case R_AARCH64_ADR_PREL_PG_HI21:
  303. ovf = reloc_insn_imm(RELOC_OP_PAGE, loc, val, 12, 21,
  304. AARCH64_INSN_IMM_ADR);
  305. break;
  306. case R_AARCH64_ADD_ABS_LO12_NC:
  307. case R_AARCH64_LDST8_ABS_LO12_NC:
  308. overflow_check = false;
  309. ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 0, 12,
  310. AARCH64_INSN_IMM_12);
  311. break;
  312. case R_AARCH64_LDST16_ABS_LO12_NC:
  313. overflow_check = false;
  314. ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 1, 11,
  315. AARCH64_INSN_IMM_12);
  316. break;
  317. case R_AARCH64_LDST32_ABS_LO12_NC:
  318. overflow_check = false;
  319. ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 2, 10,
  320. AARCH64_INSN_IMM_12);
  321. break;
  322. case R_AARCH64_LDST64_ABS_LO12_NC:
  323. overflow_check = false;
  324. ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 3, 9,
  325. AARCH64_INSN_IMM_12);
  326. break;
  327. case R_AARCH64_LDST128_ABS_LO12_NC:
  328. overflow_check = false;
  329. ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 4, 8,
  330. AARCH64_INSN_IMM_12);
  331. break;
  332. case R_AARCH64_TSTBR14:
  333. ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 14,
  334. AARCH64_INSN_IMM_14);
  335. break;
  336. case R_AARCH64_CONDBR19:
  337. ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
  338. AARCH64_INSN_IMM_19);
  339. break;
  340. case R_AARCH64_JUMP26:
  341. case R_AARCH64_CALL26:
  342. ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 26,
  343. AARCH64_INSN_IMM_26);
  344. break;
  345. default:
  346. pr_err("module %s: unsupported RELA relocation: %llu\n",
  347. me->name, ELF64_R_TYPE(rel[i].r_info));
  348. return -ENOEXEC;
  349. }
  350. if (overflow_check && ovf == -ERANGE)
  351. goto overflow;
  352. }
  353. return 0;
  354. overflow:
  355. pr_err("module %s: overflow in relocation type %d val %Lx\n",
  356. me->name, (int)ELF64_R_TYPE(rel[i].r_info), val);
  357. return -ENOEXEC;
  358. }
  359. int module_finalize(const Elf_Ehdr *hdr,
  360. const Elf_Shdr *sechdrs,
  361. struct module *me)
  362. {
  363. const Elf_Shdr *s, *se;
  364. const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  365. for (s = sechdrs, se = sechdrs + hdr->e_shnum; s < se; s++) {
  366. if (strcmp(".altinstructions", secstrs + s->sh_name) == 0) {
  367. apply_alternatives((void *)s->sh_addr, s->sh_size);
  368. return 0;
  369. }
  370. }
  371. return 0;
  372. }