GNU Linux-libre 4.19.264-gnu1
[releases.git] / arch / riscv / kernel / module.c
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or
5  *  (at your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful,
8  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *  GNU General Public License for more details.
11  *
12  *  Copyright (C) 2017 Zihao Yu
13  */
14
15 #include <linux/elf.h>
16 #include <linux/err.h>
17 #include <linux/errno.h>
18 #include <linux/moduleloader.h>
19 #include <linux/vmalloc.h>
20 #include <linux/sizes.h>
21 #include <asm/pgtable.h>
22 #include <asm/sections.h>
23
24 /*
25  * The auipc+jalr instruction pair can reach any PC-relative offset
26  * in the range [-2^31 - 2^11, 2^31 - 2^11)
27  */
28 static bool riscv_insn_valid_32bit_offset(ptrdiff_t val)
29 {
30 #ifdef CONFIG_32BIT
31         return true;
32 #else
33         return (-(1L << 31) - (1L << 11)) <= val && val < ((1L << 31) - (1L << 11));
34 #endif
35 }
36
37 static int apply_r_riscv_32_rela(struct module *me, u32 *location, Elf_Addr v)
38 {
39         if (v != (u32)v) {
40                 pr_err("%s: value %016llx out of range for 32-bit field\n",
41                        me->name, (long long)v);
42                 return -EINVAL;
43         }
44         *location = v;
45         return 0;
46 }
47
48 static int apply_r_riscv_64_rela(struct module *me, u32 *location, Elf_Addr v)
49 {
50         *(u64 *)location = v;
51         return 0;
52 }
53
54 static int apply_r_riscv_branch_rela(struct module *me, u32 *location,
55                                      Elf_Addr v)
56 {
57         ptrdiff_t offset = (void *)v - (void *)location;
58         u32 imm12 = (offset & 0x1000) << (31 - 12);
59         u32 imm11 = (offset & 0x800) >> (11 - 7);
60         u32 imm10_5 = (offset & 0x7e0) << (30 - 10);
61         u32 imm4_1 = (offset & 0x1e) << (11 - 4);
62
63         *location = (*location & 0x1fff07f) | imm12 | imm11 | imm10_5 | imm4_1;
64         return 0;
65 }
66
67 static int apply_r_riscv_jal_rela(struct module *me, u32 *location,
68                                   Elf_Addr v)
69 {
70         ptrdiff_t offset = (void *)v - (void *)location;
71         u32 imm20 = (offset & 0x100000) << (31 - 20);
72         u32 imm19_12 = (offset & 0xff000);
73         u32 imm11 = (offset & 0x800) << (20 - 11);
74         u32 imm10_1 = (offset & 0x7fe) << (30 - 10);
75
76         *location = (*location & 0xfff) | imm20 | imm19_12 | imm11 | imm10_1;
77         return 0;
78 }
79
80 static int apply_r_riscv_rcv_branch_rela(struct module *me, u32 *location,
81                                          Elf_Addr v)
82 {
83         ptrdiff_t offset = (void *)v - (void *)location;
84         u16 imm8 = (offset & 0x100) << (12 - 8);
85         u16 imm7_6 = (offset & 0xc0) >> (6 - 5);
86         u16 imm5 = (offset & 0x20) >> (5 - 2);
87         u16 imm4_3 = (offset & 0x18) << (12 - 5);
88         u16 imm2_1 = (offset & 0x6) << (12 - 10);
89
90         *(u16 *)location = (*(u16 *)location & 0xe383) |
91                     imm8 | imm7_6 | imm5 | imm4_3 | imm2_1;
92         return 0;
93 }
94
95 static int apply_r_riscv_rvc_jump_rela(struct module *me, u32 *location,
96                                        Elf_Addr v)
97 {
98         ptrdiff_t offset = (void *)v - (void *)location;
99         u16 imm11 = (offset & 0x800) << (12 - 11);
100         u16 imm10 = (offset & 0x400) >> (10 - 8);
101         u16 imm9_8 = (offset & 0x300) << (12 - 11);
102         u16 imm7 = (offset & 0x80) >> (7 - 6);
103         u16 imm6 = (offset & 0x40) << (12 - 11);
104         u16 imm5 = (offset & 0x20) >> (5 - 2);
105         u16 imm4 = (offset & 0x10) << (12 - 5);
106         u16 imm3_1 = (offset & 0xe) << (12 - 10);
107
108         *(u16 *)location = (*(u16 *)location & 0xe003) |
109                     imm11 | imm10 | imm9_8 | imm7 | imm6 | imm5 | imm4 | imm3_1;
110         return 0;
111 }
112
113 static int apply_r_riscv_pcrel_hi20_rela(struct module *me, u32 *location,
114                                          Elf_Addr v)
115 {
116         ptrdiff_t offset = (void *)v - (void *)location;
117         s32 hi20;
118
119         if (!riscv_insn_valid_32bit_offset(offset)) {
120                 pr_err(
121                   "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
122                   me->name, (long long)v, location);
123                 return -EINVAL;
124         }
125
126         hi20 = (offset + 0x800) & 0xfffff000;
127         *location = (*location & 0xfff) | hi20;
128         return 0;
129 }
130
131 static int apply_r_riscv_pcrel_lo12_i_rela(struct module *me, u32 *location,
132                                            Elf_Addr v)
133 {
134         /*
135          * v is the lo12 value to fill. It is calculated before calling this
136          * handler.
137          */
138         *location = (*location & 0xfffff) | ((v & 0xfff) << 20);
139         return 0;
140 }
141
142 static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, u32 *location,
143                                            Elf_Addr v)
144 {
145         /*
146          * v is the lo12 value to fill. It is calculated before calling this
147          * handler.
148          */
149         u32 imm11_5 = (v & 0xfe0) << (31 - 11);
150         u32 imm4_0 = (v & 0x1f) << (11 - 4);
151
152         *location = (*location & 0x1fff07f) | imm11_5 | imm4_0;
153         return 0;
154 }
155
156 static int apply_r_riscv_hi20_rela(struct module *me, u32 *location,
157                                    Elf_Addr v)
158 {
159         s32 hi20;
160
161         if (IS_ENABLED(CMODEL_MEDLOW)) {
162                 pr_err(
163                   "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
164                   me->name, (long long)v, location);
165                 return -EINVAL;
166         }
167
168         hi20 = ((s32)v + 0x800) & 0xfffff000;
169         *location = (*location & 0xfff) | hi20;
170         return 0;
171 }
172
173 static int apply_r_riscv_lo12_i_rela(struct module *me, u32 *location,
174                                      Elf_Addr v)
175 {
176         /* Skip medlow checking because of filtering by HI20 already */
177         s32 hi20 = ((s32)v + 0x800) & 0xfffff000;
178         s32 lo12 = ((s32)v - hi20);
179         *location = (*location & 0xfffff) | ((lo12 & 0xfff) << 20);
180         return 0;
181 }
182
183 static int apply_r_riscv_lo12_s_rela(struct module *me, u32 *location,
184                                      Elf_Addr v)
185 {
186         /* Skip medlow checking because of filtering by HI20 already */
187         s32 hi20 = ((s32)v + 0x800) & 0xfffff000;
188         s32 lo12 = ((s32)v - hi20);
189         u32 imm11_5 = (lo12 & 0xfe0) << (31 - 11);
190         u32 imm4_0 = (lo12 & 0x1f) << (11 - 4);
191         *location = (*location & 0x1fff07f) | imm11_5 | imm4_0;
192         return 0;
193 }
194
195 static int apply_r_riscv_got_hi20_rela(struct module *me, u32 *location,
196                                        Elf_Addr v)
197 {
198         ptrdiff_t offset = (void *)v - (void *)location;
199         s32 hi20;
200
201         /* Always emit the got entry */
202         if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
203                 offset = module_emit_got_entry(me, v);
204                 offset = (void *)offset - (void *)location;
205         } else {
206                 pr_err(
207                   "%s: can not generate the GOT entry for symbol = %016llx from PC = %p\n",
208                   me->name, (long long)v, location);
209                 return -EINVAL;
210         }
211
212         hi20 = (offset + 0x800) & 0xfffff000;
213         *location = (*location & 0xfff) | hi20;
214         return 0;
215 }
216
217 static int apply_r_riscv_call_plt_rela(struct module *me, u32 *location,
218                                        Elf_Addr v)
219 {
220         ptrdiff_t offset = (void *)v - (void *)location;
221         u32 hi20, lo12;
222
223         if (!riscv_insn_valid_32bit_offset(offset)) {
224                 /* Only emit the plt entry if offset over 32-bit range */
225                 if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
226                         offset = module_emit_plt_entry(me, v);
227                         offset = (void *)offset - (void *)location;
228                 } else {
229                         pr_err(
230                           "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
231                           me->name, (long long)v, location);
232                         return -EINVAL;
233                 }
234         }
235
236         hi20 = (offset + 0x800) & 0xfffff000;
237         lo12 = (offset - hi20) & 0xfff;
238         *location = (*location & 0xfff) | hi20;
239         *(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20);
240         return 0;
241 }
242
243 static int apply_r_riscv_call_rela(struct module *me, u32 *location,
244                                    Elf_Addr v)
245 {
246         ptrdiff_t offset = (void *)v - (void *)location;
247         u32 hi20, lo12;
248
249         if (!riscv_insn_valid_32bit_offset(offset)) {
250                 pr_err(
251                   "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
252                   me->name, (long long)v, location);
253                 return -EINVAL;
254         }
255
256         hi20 = (offset + 0x800) & 0xfffff000;
257         lo12 = (offset - hi20) & 0xfff;
258         *location = (*location & 0xfff) | hi20;
259         *(location + 1) = (*(location + 1) & 0xfffff) | (lo12 << 20);
260         return 0;
261 }
262
263 static int apply_r_riscv_relax_rela(struct module *me, u32 *location,
264                                     Elf_Addr v)
265 {
266         return 0;
267 }
268
269 static int apply_r_riscv_align_rela(struct module *me, u32 *location,
270                                     Elf_Addr v)
271 {
272         pr_err(
273           "%s: The unexpected relocation type 'R_RISCV_ALIGN' from PC = %p\n",
274           me->name, location);
275         return -EINVAL;
276 }
277
278 static int apply_r_riscv_add32_rela(struct module *me, u32 *location,
279                                     Elf_Addr v)
280 {
281         *(u32 *)location += (u32)v;
282         return 0;
283 }
284
285 static int apply_r_riscv_sub32_rela(struct module *me, u32 *location,
286                                     Elf_Addr v)
287 {
288         *(u32 *)location -= (u32)v;
289         return 0;
290 }
291
292 static int (*reloc_handlers_rela[]) (struct module *me, u32 *location,
293                                 Elf_Addr v) = {
294         [R_RISCV_32]                    = apply_r_riscv_32_rela,
295         [R_RISCV_64]                    = apply_r_riscv_64_rela,
296         [R_RISCV_BRANCH]                = apply_r_riscv_branch_rela,
297         [R_RISCV_JAL]                   = apply_r_riscv_jal_rela,
298         [R_RISCV_RVC_BRANCH]            = apply_r_riscv_rcv_branch_rela,
299         [R_RISCV_RVC_JUMP]              = apply_r_riscv_rvc_jump_rela,
300         [R_RISCV_PCREL_HI20]            = apply_r_riscv_pcrel_hi20_rela,
301         [R_RISCV_PCREL_LO12_I]          = apply_r_riscv_pcrel_lo12_i_rela,
302         [R_RISCV_PCREL_LO12_S]          = apply_r_riscv_pcrel_lo12_s_rela,
303         [R_RISCV_HI20]                  = apply_r_riscv_hi20_rela,
304         [R_RISCV_LO12_I]                = apply_r_riscv_lo12_i_rela,
305         [R_RISCV_LO12_S]                = apply_r_riscv_lo12_s_rela,
306         [R_RISCV_GOT_HI20]              = apply_r_riscv_got_hi20_rela,
307         [R_RISCV_CALL_PLT]              = apply_r_riscv_call_plt_rela,
308         [R_RISCV_CALL]                  = apply_r_riscv_call_rela,
309         [R_RISCV_RELAX]                 = apply_r_riscv_relax_rela,
310         [R_RISCV_ALIGN]                 = apply_r_riscv_align_rela,
311         [R_RISCV_ADD32]                 = apply_r_riscv_add32_rela,
312         [R_RISCV_SUB32]                 = apply_r_riscv_sub32_rela,
313 };
314
315 int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
316                        unsigned int symindex, unsigned int relsec,
317                        struct module *me)
318 {
319         Elf_Rela *rel = (void *) sechdrs[relsec].sh_addr;
320         int (*handler)(struct module *me, u32 *location, Elf_Addr v);
321         Elf_Sym *sym;
322         u32 *location;
323         unsigned int i, type;
324         Elf_Addr v;
325         int res;
326
327         pr_debug("Applying relocate section %u to %u\n", relsec,
328                sechdrs[relsec].sh_info);
329
330         for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
331                 /* This is where to make the change */
332                 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
333                         + rel[i].r_offset;
334                 /* This is the symbol it is referring to */
335                 sym = (Elf_Sym *)sechdrs[symindex].sh_addr
336                         + ELF_RISCV_R_SYM(rel[i].r_info);
337                 if (IS_ERR_VALUE(sym->st_value)) {
338                         /* Ignore unresolved weak symbol */
339                         if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
340                                 continue;
341                         pr_warning("%s: Unknown symbol %s\n",
342                                    me->name, strtab + sym->st_name);
343                         return -ENOENT;
344                 }
345
346                 type = ELF_RISCV_R_TYPE(rel[i].r_info);
347
348                 if (type < ARRAY_SIZE(reloc_handlers_rela))
349                         handler = reloc_handlers_rela[type];
350                 else
351                         handler = NULL;
352
353                 if (!handler) {
354                         pr_err("%s: Unknown relocation type %u\n",
355                                me->name, type);
356                         return -EINVAL;
357                 }
358
359                 v = sym->st_value + rel[i].r_addend;
360
361                 if (type == R_RISCV_PCREL_LO12_I || type == R_RISCV_PCREL_LO12_S) {
362                         unsigned int j;
363
364                         for (j = 0; j < sechdrs[relsec].sh_size / sizeof(*rel); j++) {
365                                 unsigned long hi20_loc =
366                                         sechdrs[sechdrs[relsec].sh_info].sh_addr
367                                         + rel[j].r_offset;
368                                 u32 hi20_type = ELF_RISCV_R_TYPE(rel[j].r_info);
369
370                                 /* Find the corresponding HI20 relocation entry */
371                                 if (hi20_loc == sym->st_value
372                                     && (hi20_type == R_RISCV_PCREL_HI20
373                                         || hi20_type == R_RISCV_GOT_HI20)) {
374                                         s32 hi20, lo12;
375                                         Elf_Sym *hi20_sym =
376                                                 (Elf_Sym *)sechdrs[symindex].sh_addr
377                                                 + ELF_RISCV_R_SYM(rel[j].r_info);
378                                         unsigned long hi20_sym_val =
379                                                 hi20_sym->st_value
380                                                 + rel[j].r_addend;
381
382                                         /* Calculate lo12 */
383                                         size_t offset = hi20_sym_val - hi20_loc;
384                                         if (IS_ENABLED(CONFIG_MODULE_SECTIONS)
385                                             && hi20_type == R_RISCV_GOT_HI20) {
386                                                 offset = module_emit_got_entry(
387                                                          me, hi20_sym_val);
388                                                 offset = offset - hi20_loc;
389                                         }
390                                         hi20 = (offset + 0x800) & 0xfffff000;
391                                         lo12 = offset - hi20;
392                                         v = lo12;
393
394                                         break;
395                                 }
396                         }
397                         if (j == sechdrs[relsec].sh_size / sizeof(*rel)) {
398                                 pr_err(
399                                   "%s: Can not find HI20 relocation information\n",
400                                   me->name);
401                                 return -EINVAL;
402                         }
403                 }
404
405                 res = handler(me, location, v);
406                 if (res)
407                         return res;
408         }
409
410         return 0;
411 }
412
413 #if defined(CONFIG_MMU) && defined(CONFIG_64BIT)
414 #define VMALLOC_MODULE_START \
415          max(PFN_ALIGN((unsigned long)&_end - SZ_2G), VMALLOC_START)
416 void *module_alloc(unsigned long size)
417 {
418         return __vmalloc_node_range(size, 1, VMALLOC_MODULE_START,
419                                     VMALLOC_END, GFP_KERNEL,
420                                     PAGE_KERNEL_EXEC, 0, NUMA_NO_NODE,
421                                     __builtin_return_address(0));
422 }
423 #endif