blob: a9dde97f5ca58a6b6ccb248c3b7ff0cfe43d3556 [file] [log] [blame]
Will Deacon257cb252012-03-05 11:49:33 +00001/*
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
21#include <linux/bitops.h>
22#include <linux/elf.h>
23#include <linux/gfp.h>
Andrey Ryabinin39d114d2015-10-12 18:52:58 +030024#include <linux/kasan.h>
Will Deacon257cb252012-03-05 11:49:33 +000025#include <linux/kernel.h>
26#include <linux/mm.h>
27#include <linux/moduleloader.h>
28#include <linux/vmalloc.h>
Paul Walmsley2c2b2822015-01-05 17:38:41 -070029#include <asm/alternative.h>
Jiang Liuc84fced2014-01-07 22:17:10 +080030#include <asm/insn.h>
Andre Przywara932ded42014-11-28 13:40:45 +000031#include <asm/sections.h>
Jiang Liuc84fced2014-01-07 22:17:10 +080032
Will Deacon257cb252012-03-05 11:49:33 +000033void *module_alloc(unsigned long size)
34{
Andrey Ryabinin39d114d2015-10-12 18:52:58 +030035 void *p;
36
37 p = __vmalloc_node_range(size, MODULE_ALIGN, MODULES_VADDR, MODULES_END,
38 GFP_KERNEL, PAGE_KERNEL_EXEC, 0,
39 NUMA_NO_NODE, __builtin_return_address(0));
40
Ard Biesheuvelfd045f62015-11-24 12:37:35 +010041 if (!p && IS_ENABLED(CONFIG_ARM64_MODULE_PLTS) &&
42 !IS_ENABLED(CONFIG_KASAN))
43 /*
44 * KASAN can only deal with module allocations being served
45 * from the reserved module region, since the remainder of
46 * the vmalloc region is already backed by zero shadow pages,
47 * and punching holes into it is non-trivial. Since the module
48 * region is not randomized when KASAN is enabled, it is even
49 * less likely that the module region gets exhausted, so we
50 * can simply omit this fallback in that case.
51 */
52 p = __vmalloc_node_range(size, MODULE_ALIGN, VMALLOC_START,
53 VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_EXEC, 0,
54 NUMA_NO_NODE, __builtin_return_address(0));
55
Andrey Ryabinin39d114d2015-10-12 18:52:58 +030056 if (p && (kasan_module_alloc(p, size) < 0)) {
57 vfree(p);
58 return NULL;
59 }
60
61 return p;
Will Deacon257cb252012-03-05 11:49:33 +000062}
63
64enum aarch64_reloc_op {
65 RELOC_OP_NONE,
66 RELOC_OP_ABS,
67 RELOC_OP_PREL,
68 RELOC_OP_PAGE,
69};
70
71static u64 do_reloc(enum aarch64_reloc_op reloc_op, void *place, u64 val)
72{
73 switch (reloc_op) {
74 case RELOC_OP_ABS:
75 return val;
76 case RELOC_OP_PREL:
77 return val - (u64)place;
78 case RELOC_OP_PAGE:
79 return (val & ~0xfff) - ((u64)place & ~0xfff);
80 case RELOC_OP_NONE:
81 return 0;
82 }
83
84 pr_err("do_reloc: unknown relocation operation %d\n", reloc_op);
85 return 0;
86}
87
88static int reloc_data(enum aarch64_reloc_op op, void *place, u64 val, int len)
89{
Will Deacon257cb252012-03-05 11:49:33 +000090 s64 sval = do_reloc(op, place, val);
91
92 switch (len) {
93 case 16:
94 *(s16 *)place = sval;
Ard Biesheuvelf9308962016-01-05 10:18:52 +010095 if (sval < S16_MIN || sval > U16_MAX)
96 return -ERANGE;
Will Deacon257cb252012-03-05 11:49:33 +000097 break;
98 case 32:
99 *(s32 *)place = sval;
Ard Biesheuvelf9308962016-01-05 10:18:52 +0100100 if (sval < S32_MIN || sval > U32_MAX)
101 return -ERANGE;
Will Deacon257cb252012-03-05 11:49:33 +0000102 break;
103 case 64:
104 *(s64 *)place = sval;
105 break;
106 default:
107 pr_err("Invalid length (%d) for data relocation\n", len);
108 return 0;
109 }
Will Deacon257cb252012-03-05 11:49:33 +0000110 return 0;
111}
112
Ard Biesheuvelb24a5572016-01-05 10:18:51 +0100113enum aarch64_insn_movw_imm_type {
114 AARCH64_INSN_IMM_MOVNZ,
115 AARCH64_INSN_IMM_MOVKZ,
116};
117
Jiang Liuc84fced2014-01-07 22:17:10 +0800118static int reloc_insn_movw(enum aarch64_reloc_op op, void *place, u64 val,
Ard Biesheuvelb24a5572016-01-05 10:18:51 +0100119 int lsb, enum aarch64_insn_movw_imm_type imm_type)
Will Deacon257cb252012-03-05 11:49:33 +0000120{
Ard Biesheuvelb24a5572016-01-05 10:18:51 +0100121 u64 imm;
Jiang Liuc84fced2014-01-07 22:17:10 +0800122 s64 sval;
123 u32 insn = le32_to_cpu(*(u32 *)place);
Will Deacon257cb252012-03-05 11:49:33 +0000124
Jiang Liuc84fced2014-01-07 22:17:10 +0800125 sval = do_reloc(op, place, val);
Ard Biesheuvelb24a5572016-01-05 10:18:51 +0100126 imm = sval >> lsb;
Will Deacon122e2fa2013-11-05 10:16:52 +0000127
Jiang Liuc84fced2014-01-07 22:17:10 +0800128 if (imm_type == AARCH64_INSN_IMM_MOVNZ) {
Will Deacon257cb252012-03-05 11:49:33 +0000129 /*
130 * For signed MOVW relocations, we have to manipulate the
131 * instruction encoding depending on whether or not the
132 * immediate is less than zero.
133 */
134 insn &= ~(3 << 29);
Ard Biesheuvelb24a5572016-01-05 10:18:51 +0100135 if (sval >= 0) {
Will Deacon257cb252012-03-05 11:49:33 +0000136 /* >=0: Set the instruction to MOVZ (opcode 10b). */
137 insn |= 2 << 29;
138 } else {
139 /*
140 * <0: Set the instruction to MOVN (opcode 00b).
141 * Since we've masked the opcode already, we
142 * don't need to do anything other than
143 * inverting the new immediate field.
144 */
145 imm = ~imm;
146 }
Will Deacon257cb252012-03-05 11:49:33 +0000147 }
148
Will Deacon257cb252012-03-05 11:49:33 +0000149 /* Update the instruction with the new encoding. */
Ard Biesheuvelb24a5572016-01-05 10:18:51 +0100150 insn = aarch64_insn_encode_immediate(AARCH64_INSN_IMM_16, insn, imm);
Jiang Liuc84fced2014-01-07 22:17:10 +0800151 *(u32 *)place = cpu_to_le32(insn);
Will Deacon257cb252012-03-05 11:49:33 +0000152
Ard Biesheuvelb24a5572016-01-05 10:18:51 +0100153 if (imm > U16_MAX)
Will Deacon257cb252012-03-05 11:49:33 +0000154 return -ERANGE;
155
156 return 0;
157}
158
159static int reloc_insn_imm(enum aarch64_reloc_op op, void *place, u64 val,
Jiang Liuc84fced2014-01-07 22:17:10 +0800160 int lsb, int len, enum aarch64_insn_imm_type imm_type)
Will Deacon257cb252012-03-05 11:49:33 +0000161{
162 u64 imm, imm_mask;
163 s64 sval;
Jiang Liuc84fced2014-01-07 22:17:10 +0800164 u32 insn = le32_to_cpu(*(u32 *)place);
Will Deacon257cb252012-03-05 11:49:33 +0000165
166 /* Calculate the relocation value. */
167 sval = do_reloc(op, place, val);
168 sval >>= lsb;
169
170 /* Extract the value bits and shift them to bit 0. */
171 imm_mask = (BIT(lsb + len) - 1) >> lsb;
172 imm = sval & imm_mask;
173
174 /* Update the instruction's immediate field. */
Jiang Liuc84fced2014-01-07 22:17:10 +0800175 insn = aarch64_insn_encode_immediate(imm_type, insn, imm);
176 *(u32 *)place = cpu_to_le32(insn);
Will Deacon257cb252012-03-05 11:49:33 +0000177
178 /*
179 * Extract the upper value bits (including the sign bit) and
180 * shift them to bit 0.
181 */
182 sval = (s64)(sval & ~(imm_mask >> 1)) >> (len - 1);
183
184 /*
185 * Overflow has occurred if the upper bits are not all equal to
186 * the sign bit of the value.
187 */
188 if ((u64)(sval + 1) >= 2)
189 return -ERANGE;
190
191 return 0;
192}
193
194int apply_relocate_add(Elf64_Shdr *sechdrs,
195 const char *strtab,
196 unsigned int symindex,
197 unsigned int relsec,
198 struct module *me)
199{
200 unsigned int i;
201 int ovf;
202 bool overflow_check;
203 Elf64_Sym *sym;
204 void *loc;
205 u64 val;
206 Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
207
208 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
209 /* loc corresponds to P in the AArch64 ELF document. */
210 loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
211 + rel[i].r_offset;
212
213 /* sym is the ELF symbol we're referring to. */
214 sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
215 + ELF64_R_SYM(rel[i].r_info);
216
217 /* val corresponds to (S + A) in the AArch64 ELF document. */
218 val = sym->st_value + rel[i].r_addend;
219
220 /* Check for overflow by default. */
221 overflow_check = true;
222
223 /* Perform the static relocation. */
224 switch (ELF64_R_TYPE(rel[i].r_info)) {
225 /* Null relocations. */
226 case R_ARM_NONE:
227 case R_AARCH64_NONE:
228 ovf = 0;
229 break;
230
231 /* Data relocations. */
232 case R_AARCH64_ABS64:
233 overflow_check = false;
234 ovf = reloc_data(RELOC_OP_ABS, loc, val, 64);
235 break;
236 case R_AARCH64_ABS32:
237 ovf = reloc_data(RELOC_OP_ABS, loc, val, 32);
238 break;
239 case R_AARCH64_ABS16:
240 ovf = reloc_data(RELOC_OP_ABS, loc, val, 16);
241 break;
242 case R_AARCH64_PREL64:
243 overflow_check = false;
244 ovf = reloc_data(RELOC_OP_PREL, loc, val, 64);
245 break;
246 case R_AARCH64_PREL32:
247 ovf = reloc_data(RELOC_OP_PREL, loc, val, 32);
248 break;
249 case R_AARCH64_PREL16:
250 ovf = reloc_data(RELOC_OP_PREL, loc, val, 16);
251 break;
252
253 /* MOVW instruction relocations. */
254 case R_AARCH64_MOVW_UABS_G0_NC:
255 overflow_check = false;
256 case R_AARCH64_MOVW_UABS_G0:
257 ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
Ard Biesheuvelb24a5572016-01-05 10:18:51 +0100258 AARCH64_INSN_IMM_MOVKZ);
Will Deacon257cb252012-03-05 11:49:33 +0000259 break;
260 case R_AARCH64_MOVW_UABS_G1_NC:
261 overflow_check = false;
262 case R_AARCH64_MOVW_UABS_G1:
263 ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
Ard Biesheuvelb24a5572016-01-05 10:18:51 +0100264 AARCH64_INSN_IMM_MOVKZ);
Will Deacon257cb252012-03-05 11:49:33 +0000265 break;
266 case R_AARCH64_MOVW_UABS_G2_NC:
267 overflow_check = false;
268 case R_AARCH64_MOVW_UABS_G2:
269 ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
Ard Biesheuvelb24a5572016-01-05 10:18:51 +0100270 AARCH64_INSN_IMM_MOVKZ);
Will Deacon257cb252012-03-05 11:49:33 +0000271 break;
272 case R_AARCH64_MOVW_UABS_G3:
273 /* We're using the top bits so we can't overflow. */
274 overflow_check = false;
275 ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 48,
Ard Biesheuvelb24a5572016-01-05 10:18:51 +0100276 AARCH64_INSN_IMM_MOVKZ);
Will Deacon257cb252012-03-05 11:49:33 +0000277 break;
278 case R_AARCH64_MOVW_SABS_G0:
279 ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
Jiang Liuc84fced2014-01-07 22:17:10 +0800280 AARCH64_INSN_IMM_MOVNZ);
Will Deacon257cb252012-03-05 11:49:33 +0000281 break;
282 case R_AARCH64_MOVW_SABS_G1:
283 ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
Jiang Liuc84fced2014-01-07 22:17:10 +0800284 AARCH64_INSN_IMM_MOVNZ);
Will Deacon257cb252012-03-05 11:49:33 +0000285 break;
286 case R_AARCH64_MOVW_SABS_G2:
287 ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
Jiang Liuc84fced2014-01-07 22:17:10 +0800288 AARCH64_INSN_IMM_MOVNZ);
Will Deacon257cb252012-03-05 11:49:33 +0000289 break;
290 case R_AARCH64_MOVW_PREL_G0_NC:
291 overflow_check = false;
292 ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
Ard Biesheuvelb24a5572016-01-05 10:18:51 +0100293 AARCH64_INSN_IMM_MOVKZ);
Will Deacon257cb252012-03-05 11:49:33 +0000294 break;
295 case R_AARCH64_MOVW_PREL_G0:
296 ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
Jiang Liuc84fced2014-01-07 22:17:10 +0800297 AARCH64_INSN_IMM_MOVNZ);
Will Deacon257cb252012-03-05 11:49:33 +0000298 break;
299 case R_AARCH64_MOVW_PREL_G1_NC:
300 overflow_check = false;
301 ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
Ard Biesheuvelb24a5572016-01-05 10:18:51 +0100302 AARCH64_INSN_IMM_MOVKZ);
Will Deacon257cb252012-03-05 11:49:33 +0000303 break;
304 case R_AARCH64_MOVW_PREL_G1:
305 ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
Jiang Liuc84fced2014-01-07 22:17:10 +0800306 AARCH64_INSN_IMM_MOVNZ);
Will Deacon257cb252012-03-05 11:49:33 +0000307 break;
308 case R_AARCH64_MOVW_PREL_G2_NC:
309 overflow_check = false;
310 ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
Ard Biesheuvelb24a5572016-01-05 10:18:51 +0100311 AARCH64_INSN_IMM_MOVKZ);
Will Deacon257cb252012-03-05 11:49:33 +0000312 break;
313 case R_AARCH64_MOVW_PREL_G2:
314 ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
Jiang Liuc84fced2014-01-07 22:17:10 +0800315 AARCH64_INSN_IMM_MOVNZ);
Will Deacon257cb252012-03-05 11:49:33 +0000316 break;
317 case R_AARCH64_MOVW_PREL_G3:
318 /* We're using the top bits so we can't overflow. */
319 overflow_check = false;
320 ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 48,
Jiang Liuc84fced2014-01-07 22:17:10 +0800321 AARCH64_INSN_IMM_MOVNZ);
Will Deacon257cb252012-03-05 11:49:33 +0000322 break;
323
324 /* Immediate instruction relocations. */
325 case R_AARCH64_LD_PREL_LO19:
326 ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
Jiang Liuc84fced2014-01-07 22:17:10 +0800327 AARCH64_INSN_IMM_19);
Will Deacon257cb252012-03-05 11:49:33 +0000328 break;
329 case R_AARCH64_ADR_PREL_LO21:
330 ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 0, 21,
Jiang Liuc84fced2014-01-07 22:17:10 +0800331 AARCH64_INSN_IMM_ADR);
Will Deacon257cb252012-03-05 11:49:33 +0000332 break;
Will Deacondf057cc2015-03-17 12:15:02 +0000333#ifndef CONFIG_ARM64_ERRATUM_843419
Will Deacon257cb252012-03-05 11:49:33 +0000334 case R_AARCH64_ADR_PREL_PG_HI21_NC:
335 overflow_check = false;
336 case R_AARCH64_ADR_PREL_PG_HI21:
337 ovf = reloc_insn_imm(RELOC_OP_PAGE, loc, val, 12, 21,
Jiang Liuc84fced2014-01-07 22:17:10 +0800338 AARCH64_INSN_IMM_ADR);
Will Deacon257cb252012-03-05 11:49:33 +0000339 break;
Will Deacondf057cc2015-03-17 12:15:02 +0000340#endif
Will Deacon257cb252012-03-05 11:49:33 +0000341 case R_AARCH64_ADD_ABS_LO12_NC:
342 case R_AARCH64_LDST8_ABS_LO12_NC:
343 overflow_check = false;
344 ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 0, 12,
Jiang Liuc84fced2014-01-07 22:17:10 +0800345 AARCH64_INSN_IMM_12);
Will Deacon257cb252012-03-05 11:49:33 +0000346 break;
347 case R_AARCH64_LDST16_ABS_LO12_NC:
348 overflow_check = false;
349 ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 1, 11,
Jiang Liuc84fced2014-01-07 22:17:10 +0800350 AARCH64_INSN_IMM_12);
Will Deacon257cb252012-03-05 11:49:33 +0000351 break;
352 case R_AARCH64_LDST32_ABS_LO12_NC:
353 overflow_check = false;
354 ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 2, 10,
Jiang Liuc84fced2014-01-07 22:17:10 +0800355 AARCH64_INSN_IMM_12);
Will Deacon257cb252012-03-05 11:49:33 +0000356 break;
357 case R_AARCH64_LDST64_ABS_LO12_NC:
358 overflow_check = false;
359 ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 3, 9,
Jiang Liuc84fced2014-01-07 22:17:10 +0800360 AARCH64_INSN_IMM_12);
Will Deacon257cb252012-03-05 11:49:33 +0000361 break;
362 case R_AARCH64_LDST128_ABS_LO12_NC:
363 overflow_check = false;
364 ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 4, 8,
Jiang Liuc84fced2014-01-07 22:17:10 +0800365 AARCH64_INSN_IMM_12);
Will Deacon257cb252012-03-05 11:49:33 +0000366 break;
367 case R_AARCH64_TSTBR14:
368 ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 14,
Jiang Liuc84fced2014-01-07 22:17:10 +0800369 AARCH64_INSN_IMM_14);
Will Deacon257cb252012-03-05 11:49:33 +0000370 break;
371 case R_AARCH64_CONDBR19:
372 ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
Jiang Liuc84fced2014-01-07 22:17:10 +0800373 AARCH64_INSN_IMM_19);
Will Deacon257cb252012-03-05 11:49:33 +0000374 break;
375 case R_AARCH64_JUMP26:
376 case R_AARCH64_CALL26:
377 ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 26,
Jiang Liuc84fced2014-01-07 22:17:10 +0800378 AARCH64_INSN_IMM_26);
Ard Biesheuvelfd045f62015-11-24 12:37:35 +0100379
380 if (IS_ENABLED(CONFIG_ARM64_MODULE_PLTS) &&
381 ovf == -ERANGE) {
382 val = module_emit_plt_entry(me, &rel[i], sym);
383 ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2,
384 26, AARCH64_INSN_IMM_26);
385 }
Will Deacon257cb252012-03-05 11:49:33 +0000386 break;
387
388 default:
389 pr_err("module %s: unsupported RELA relocation: %llu\n",
390 me->name, ELF64_R_TYPE(rel[i].r_info));
391 return -ENOEXEC;
392 }
393
394 if (overflow_check && ovf == -ERANGE)
395 goto overflow;
396
397 }
398
399 return 0;
400
401overflow:
402 pr_err("module %s: overflow in relocation type %d val %Lx\n",
403 me->name, (int)ELF64_R_TYPE(rel[i].r_info), val);
404 return -ENOEXEC;
405}
Andre Przywara932ded42014-11-28 13:40:45 +0000406
407int module_finalize(const Elf_Ehdr *hdr,
408 const Elf_Shdr *sechdrs,
409 struct module *me)
410{
411 const Elf_Shdr *s, *se;
412 const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
413
414 for (s = sechdrs, se = sechdrs + hdr->e_shnum; s < se; s++) {
415 if (strcmp(".altinstructions", secstrs + s->sh_name) == 0) {
416 apply_alternatives((void *)s->sh_addr, s->sh_size);
417 return 0;
418 }
419 }
420
421 return 0;
422}