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