1 // SPDX-License-Identifier: GPL-2.0-only
3 * AArch64 loadable module support.
5 * Copyright (C) 2012 ARM Limited
7 * Author: Will Deacon <will.deacon@arm.com>
10 #include <linux/bitops.h>
11 #include <linux/elf.h>
12 #include <linux/ftrace.h>
13 #include <linux/gfp.h>
14 #include <linux/kasan.h>
15 #include <linux/kernel.h>
17 #include <linux/moduleloader.h>
18 #include <linux/vmalloc.h>
19 #include <asm/alternative.h>
21 #include <asm/sections.h>
23 void *module_alloc(unsigned long size)
25 u64 module_alloc_end = module_alloc_base + MODULES_VSIZE;
26 gfp_t gfp_mask = GFP_KERNEL;
29 /* Silence the initial allocation */
30 if (IS_ENABLED(CONFIG_ARM64_MODULE_PLTS))
31 gfp_mask |= __GFP_NOWARN;
33 if (IS_ENABLED(CONFIG_KASAN_GENERIC) ||
34 IS_ENABLED(CONFIG_KASAN_SW_TAGS))
35 /* don't exceed the static module region - see below */
36 module_alloc_end = MODULES_END;
38 p = __vmalloc_node_range(size, MODULE_ALIGN, module_alloc_base,
39 module_alloc_end, gfp_mask, PAGE_KERNEL, 0,
40 NUMA_NO_NODE, __builtin_return_address(0));
42 if (!p && IS_ENABLED(CONFIG_ARM64_MODULE_PLTS) &&
43 (IS_ENABLED(CONFIG_KASAN_VMALLOC) ||
44 (!IS_ENABLED(CONFIG_KASAN_GENERIC) &&
45 !IS_ENABLED(CONFIG_KASAN_SW_TAGS))))
47 * KASAN without KASAN_VMALLOC can only deal with module
48 * allocations being served from the reserved module region,
49 * since the remainder of the vmalloc region is already
50 * backed by zero shadow pages, and punching holes into it
51 * is non-trivial. Since the module region is not randomized
52 * when KASAN is enabled without KASAN_VMALLOC, it is even
53 * less likely that the module region gets exhausted, so we
54 * can simply omit this fallback in that case.
56 p = __vmalloc_node_range(size, MODULE_ALIGN, module_alloc_base,
57 module_alloc_base + SZ_2G, GFP_KERNEL,
58 PAGE_KERNEL, 0, NUMA_NO_NODE,
59 __builtin_return_address(0));
61 if (p && (kasan_module_alloc(p, size) < 0)) {
69 enum aarch64_reloc_op {
76 static u64 do_reloc(enum aarch64_reloc_op reloc_op, __le32 *place, u64 val)
82 return val - (u64)place;
84 return (val & ~0xfff) - ((u64)place & ~0xfff);
89 pr_err("do_reloc: unknown relocation operation %d\n", reloc_op);
93 static int reloc_data(enum aarch64_reloc_op op, void *place, u64 val, int len)
95 s64 sval = do_reloc(op, place, val);
98 * The ELF psABI for AArch64 documents the 16-bit and 32-bit place
99 * relative and absolute relocations as having a range of [-2^15, 2^16)
100 * or [-2^31, 2^32), respectively. However, in order to be able to
101 * detect overflows reliably, we have to choose whether we interpret
102 * such quantities as signed or as unsigned, and stick with it.
103 * The way we organize our address space requires a signed
104 * interpretation of 32-bit relative references, so let's use that
105 * for all R_AARCH64_PRELxx relocations. This means our upper
106 * bound for overflow detection should be Sxx_MAX rather than Uxx_MAX.
111 *(s16 *)place = sval;
114 if (sval < 0 || sval > U16_MAX)
118 if (sval < S16_MIN || sval > S16_MAX)
122 pr_err("Invalid 16-bit data relocation (%d)\n", op);
127 *(s32 *)place = sval;
130 if (sval < 0 || sval > U32_MAX)
134 if (sval < S32_MIN || sval > S32_MAX)
138 pr_err("Invalid 32-bit data relocation (%d)\n", op);
143 *(s64 *)place = sval;
146 pr_err("Invalid length (%d) for data relocation\n", len);
152 enum aarch64_insn_movw_imm_type {
153 AARCH64_INSN_IMM_MOVNZ,
154 AARCH64_INSN_IMM_MOVKZ,
157 static int reloc_insn_movw(enum aarch64_reloc_op op, __le32 *place, u64 val,
158 int lsb, enum aarch64_insn_movw_imm_type imm_type)
162 u32 insn = le32_to_cpu(*place);
164 sval = do_reloc(op, place, val);
167 if (imm_type == AARCH64_INSN_IMM_MOVNZ) {
169 * For signed MOVW relocations, we have to manipulate the
170 * instruction encoding depending on whether or not the
171 * immediate is less than zero.
175 /* >=0: Set the instruction to MOVZ (opcode 10b). */
179 * <0: Set the instruction to MOVN (opcode 00b).
180 * Since we've masked the opcode already, we
181 * don't need to do anything other than
182 * inverting the new immediate field.
188 /* Update the instruction with the new encoding. */
189 insn = aarch64_insn_encode_immediate(AARCH64_INSN_IMM_16, insn, imm);
190 *place = cpu_to_le32(insn);
198 static int reloc_insn_imm(enum aarch64_reloc_op op, __le32 *place, u64 val,
199 int lsb, int len, enum aarch64_insn_imm_type imm_type)
203 u32 insn = le32_to_cpu(*place);
205 /* Calculate the relocation value. */
206 sval = do_reloc(op, place, val);
209 /* Extract the value bits and shift them to bit 0. */
210 imm_mask = (BIT(lsb + len) - 1) >> lsb;
211 imm = sval & imm_mask;
213 /* Update the instruction's immediate field. */
214 insn = aarch64_insn_encode_immediate(imm_type, insn, imm);
215 *place = cpu_to_le32(insn);
218 * Extract the upper value bits (including the sign bit) and
219 * shift them to bit 0.
221 sval = (s64)(sval & ~(imm_mask >> 1)) >> (len - 1);
224 * Overflow has occurred if the upper bits are not all equal to
225 * the sign bit of the value.
227 if ((u64)(sval + 1) >= 2)
233 static int reloc_insn_adrp(struct module *mod, Elf64_Shdr *sechdrs,
234 __le32 *place, u64 val)
238 if (!is_forbidden_offset_for_adrp(place))
239 return reloc_insn_imm(RELOC_OP_PAGE, place, val, 12, 21,
240 AARCH64_INSN_IMM_ADR);
242 /* patch ADRP to ADR if it is in range */
243 if (!reloc_insn_imm(RELOC_OP_PREL, place, val & ~0xfff, 0, 21,
244 AARCH64_INSN_IMM_ADR)) {
245 insn = le32_to_cpu(*place);
248 /* out of range for ADR -> emit a veneer */
249 val = module_emit_veneer_for_adrp(mod, sechdrs, place, val & ~0xfff);
252 insn = aarch64_insn_gen_branch_imm((u64)place, val,
253 AARCH64_INSN_BRANCH_NOLINK);
256 *place = cpu_to_le32(insn);
260 int apply_relocate_add(Elf64_Shdr *sechdrs,
262 unsigned int symindex,
272 Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
274 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
275 /* loc corresponds to P in the AArch64 ELF document. */
276 loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
279 /* sym is the ELF symbol we're referring to. */
280 sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
281 + ELF64_R_SYM(rel[i].r_info);
283 /* val corresponds to (S + A) in the AArch64 ELF document. */
284 val = sym->st_value + rel[i].r_addend;
286 /* Check for overflow by default. */
287 overflow_check = true;
289 /* Perform the static relocation. */
290 switch (ELF64_R_TYPE(rel[i].r_info)) {
291 /* Null relocations. */
297 /* Data relocations. */
298 case R_AARCH64_ABS64:
299 overflow_check = false;
300 ovf = reloc_data(RELOC_OP_ABS, loc, val, 64);
302 case R_AARCH64_ABS32:
303 ovf = reloc_data(RELOC_OP_ABS, loc, val, 32);
305 case R_AARCH64_ABS16:
306 ovf = reloc_data(RELOC_OP_ABS, loc, val, 16);
308 case R_AARCH64_PREL64:
309 overflow_check = false;
310 ovf = reloc_data(RELOC_OP_PREL, loc, val, 64);
312 case R_AARCH64_PREL32:
313 ovf = reloc_data(RELOC_OP_PREL, loc, val, 32);
315 case R_AARCH64_PREL16:
316 ovf = reloc_data(RELOC_OP_PREL, loc, val, 16);
319 /* MOVW instruction relocations. */
320 case R_AARCH64_MOVW_UABS_G0_NC:
321 overflow_check = false;
323 case R_AARCH64_MOVW_UABS_G0:
324 ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
325 AARCH64_INSN_IMM_MOVKZ);
327 case R_AARCH64_MOVW_UABS_G1_NC:
328 overflow_check = false;
330 case R_AARCH64_MOVW_UABS_G1:
331 ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
332 AARCH64_INSN_IMM_MOVKZ);
334 case R_AARCH64_MOVW_UABS_G2_NC:
335 overflow_check = false;
337 case R_AARCH64_MOVW_UABS_G2:
338 ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
339 AARCH64_INSN_IMM_MOVKZ);
341 case R_AARCH64_MOVW_UABS_G3:
342 /* We're using the top bits so we can't overflow. */
343 overflow_check = false;
344 ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 48,
345 AARCH64_INSN_IMM_MOVKZ);
347 case R_AARCH64_MOVW_SABS_G0:
348 ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0,
349 AARCH64_INSN_IMM_MOVNZ);
351 case R_AARCH64_MOVW_SABS_G1:
352 ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16,
353 AARCH64_INSN_IMM_MOVNZ);
355 case R_AARCH64_MOVW_SABS_G2:
356 ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32,
357 AARCH64_INSN_IMM_MOVNZ);
359 case R_AARCH64_MOVW_PREL_G0_NC:
360 overflow_check = false;
361 ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
362 AARCH64_INSN_IMM_MOVKZ);
364 case R_AARCH64_MOVW_PREL_G0:
365 ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0,
366 AARCH64_INSN_IMM_MOVNZ);
368 case R_AARCH64_MOVW_PREL_G1_NC:
369 overflow_check = false;
370 ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
371 AARCH64_INSN_IMM_MOVKZ);
373 case R_AARCH64_MOVW_PREL_G1:
374 ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16,
375 AARCH64_INSN_IMM_MOVNZ);
377 case R_AARCH64_MOVW_PREL_G2_NC:
378 overflow_check = false;
379 ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
380 AARCH64_INSN_IMM_MOVKZ);
382 case R_AARCH64_MOVW_PREL_G2:
383 ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32,
384 AARCH64_INSN_IMM_MOVNZ);
386 case R_AARCH64_MOVW_PREL_G3:
387 /* We're using the top bits so we can't overflow. */
388 overflow_check = false;
389 ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 48,
390 AARCH64_INSN_IMM_MOVNZ);
393 /* Immediate instruction relocations. */
394 case R_AARCH64_LD_PREL_LO19:
395 ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
396 AARCH64_INSN_IMM_19);
398 case R_AARCH64_ADR_PREL_LO21:
399 ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 0, 21,
400 AARCH64_INSN_IMM_ADR);
402 case R_AARCH64_ADR_PREL_PG_HI21_NC:
403 overflow_check = false;
405 case R_AARCH64_ADR_PREL_PG_HI21:
406 ovf = reloc_insn_adrp(me, sechdrs, loc, val);
407 if (ovf && ovf != -ERANGE)
410 case R_AARCH64_ADD_ABS_LO12_NC:
411 case R_AARCH64_LDST8_ABS_LO12_NC:
412 overflow_check = false;
413 ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 0, 12,
414 AARCH64_INSN_IMM_12);
416 case R_AARCH64_LDST16_ABS_LO12_NC:
417 overflow_check = false;
418 ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 1, 11,
419 AARCH64_INSN_IMM_12);
421 case R_AARCH64_LDST32_ABS_LO12_NC:
422 overflow_check = false;
423 ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 2, 10,
424 AARCH64_INSN_IMM_12);
426 case R_AARCH64_LDST64_ABS_LO12_NC:
427 overflow_check = false;
428 ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 3, 9,
429 AARCH64_INSN_IMM_12);
431 case R_AARCH64_LDST128_ABS_LO12_NC:
432 overflow_check = false;
433 ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 4, 8,
434 AARCH64_INSN_IMM_12);
436 case R_AARCH64_TSTBR14:
437 ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 14,
438 AARCH64_INSN_IMM_14);
440 case R_AARCH64_CONDBR19:
441 ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19,
442 AARCH64_INSN_IMM_19);
444 case R_AARCH64_JUMP26:
445 case R_AARCH64_CALL26:
446 ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 26,
447 AARCH64_INSN_IMM_26);
449 if (IS_ENABLED(CONFIG_ARM64_MODULE_PLTS) &&
451 val = module_emit_plt_entry(me, sechdrs, loc, &rel[i], sym);
454 ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2,
455 26, AARCH64_INSN_IMM_26);
460 pr_err("module %s: unsupported RELA relocation: %llu\n",
461 me->name, ELF64_R_TYPE(rel[i].r_info));
465 if (overflow_check && ovf == -ERANGE)
473 pr_err("module %s: overflow in relocation type %d val %Lx\n",
474 me->name, (int)ELF64_R_TYPE(rel[i].r_info), val);
478 static const Elf_Shdr *find_section(const Elf_Ehdr *hdr,
479 const Elf_Shdr *sechdrs,
482 const Elf_Shdr *s, *se;
483 const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
485 for (s = sechdrs, se = sechdrs + hdr->e_shnum; s < se; s++) {
486 if (strcmp(name, secstrs + s->sh_name) == 0)
493 static inline void __init_plt(struct plt_entry *plt, unsigned long addr)
495 *plt = get_plt_entry(addr, plt);
498 static int module_init_ftrace_plt(const Elf_Ehdr *hdr,
499 const Elf_Shdr *sechdrs,
502 #if defined(CONFIG_ARM64_MODULE_PLTS) && defined(CONFIG_DYNAMIC_FTRACE)
504 struct plt_entry *plts;
506 s = find_section(hdr, sechdrs, ".text.ftrace_trampoline");
510 plts = (void *)s->sh_addr;
512 __init_plt(&plts[FTRACE_PLT_IDX], FTRACE_ADDR);
514 if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS))
515 __init_plt(&plts[FTRACE_REGS_PLT_IDX], FTRACE_REGS_ADDR);
517 mod->arch.ftrace_trampolines = plts;
522 int module_finalize(const Elf_Ehdr *hdr,
523 const Elf_Shdr *sechdrs,
527 s = find_section(hdr, sechdrs, ".altinstructions");
529 apply_alternatives_module((void *)s->sh_addr, s->sh_size);
531 return module_init_ftrace_plt(hdr, sechdrs, me);