module.c 11 KB

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