]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/riscv/riscv/elf_machdep.c
unbount: Vendor import 1.14.0rc1
[FreeBSD/FreeBSD.git] / sys / riscv / riscv / elf_machdep.c
1 /*-
2  * Copyright 1996-1998 John D. Polstra.
3  * Copyright (c) 2015 Ruslan Bukin <br@bsdpad.com>
4  * Copyright (c) 2016 Yukishige Shibata <y-shibat@mtd.biglobe.ne.jp>
5  * All rights reserved.
6  *
7  * Portions of this software were developed by SRI International and the
8  * University of Cambridge Computer Laboratory under DARPA/AFRL contract
9  * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
10  *
11  * Portions of this software were developed by the University of Cambridge
12  * Computer Laboratory as part of the CTSRD Project, with support from the
13  * UK Higher Education Innovation Fund (HEIF).
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/systm.h>
43 #include <sys/exec.h>
44 #include <sys/imgact.h>
45 #include <sys/linker.h>
46 #include <sys/proc.h>
47 #include <sys/sysctl.h>
48 #include <sys/sysent.h>
49 #include <sys/imgact_elf.h>
50 #include <sys/syscall.h>
51 #include <sys/signalvar.h>
52 #include <sys/vnode.h>
53
54 #include <vm/vm.h>
55 #include <vm/pmap.h>
56 #include <vm/vm_param.h>
57
58 #include <machine/elf.h>
59 #include <machine/md_var.h>
60
61 static const char *riscv_machine_arch(struct proc *p);
62
63 u_long elf_hwcap;
64
65 struct sysentvec elf64_freebsd_sysvec = {
66         .sv_size        = SYS_MAXSYSCALL,
67         .sv_table       = sysent,
68         .sv_transtrap   = NULL,
69         .sv_fixup       = __elfN(freebsd_fixup),
70         .sv_sendsig     = sendsig,
71         .sv_sigcode     = sigcode,
72         .sv_szsigcode   = &szsigcode,
73         .sv_name        = "FreeBSD ELF64",
74         .sv_coredump    = __elfN(coredump),
75         .sv_elf_core_osabi = ELFOSABI_FREEBSD,
76         .sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR,
77         .sv_elf_core_prepare_notes = __elfN(prepare_notes),
78         .sv_imgact_try  = NULL,
79         .sv_minsigstksz = MINSIGSTKSZ,
80         .sv_minuser     = VM_MIN_ADDRESS,
81         .sv_maxuser     = VM_MAXUSER_ADDRESS,
82         .sv_usrstack    = USRSTACK,
83         .sv_psstrings   = PS_STRINGS,
84         .sv_stackprot   = VM_PROT_READ | VM_PROT_WRITE,
85         .sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs),
86         .sv_copyout_strings     = exec_copyout_strings,
87         .sv_setregs     = exec_setregs,
88         .sv_fixlimit    = NULL,
89         .sv_maxssiz     = NULL,
90         .sv_flags       = SV_ABI_FREEBSD | SV_LP64 | SV_SHP | SV_TIMEKEEP |
91             SV_ASLR | SV_RNG_SEED_VER,
92         .sv_set_syscall_retval = cpu_set_syscall_retval,
93         .sv_fetch_syscall_args = cpu_fetch_syscall_args,
94         .sv_syscallnames = syscallnames,
95         .sv_shared_page_base = SHAREDPAGE,
96         .sv_shared_page_len = PAGE_SIZE,
97         .sv_schedtail   = NULL,
98         .sv_thread_detach = NULL,
99         .sv_trap        = NULL,
100         .sv_hwcap       = &elf_hwcap,
101         .sv_machine_arch = riscv_machine_arch,
102         .sv_onexec_old  = exec_onexec_old,
103         .sv_onexit      = exit_onexit,
104 };
105 INIT_SYSENTVEC(elf64_sysvec, &elf64_freebsd_sysvec);
106
107 static const char *
108 riscv_machine_arch(struct proc *p)
109 {
110
111         if ((p->p_elf_flags & EF_RISCV_FLOAT_ABI_MASK) ==
112             EF_RISCV_FLOAT_ABI_SOFT)
113                 return (MACHINE_ARCH "sf");
114         return (MACHINE_ARCH);
115 }
116
117 static Elf64_Brandinfo freebsd_brand_info = {
118         .brand          = ELFOSABI_FREEBSD,
119         .machine        = EM_RISCV,
120         .compat_3_brand = "FreeBSD",
121         .emul_path      = NULL,
122         .interp_path    = "/libexec/ld-elf.so.1",
123         .sysvec         = &elf64_freebsd_sysvec,
124         .interp_newpath = NULL,
125         .brand_note     = &elf64_freebsd_brandnote,
126         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
127 };
128
129 SYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_FIRST,
130     (sysinit_cfunc_t)elf64_insert_brand_entry, &freebsd_brand_info);
131
132 static bool debug_kld;
133 SYSCTL_BOOL(_debug, OID_AUTO, kld_reloc, CTLFLAG_RW, &debug_kld, 0,
134     "Activate debug prints in elf_reloc_internal()");
135
136 struct type2str_ent {
137         int type;
138         const char *str;
139 };
140
141 void
142 elf64_dump_thread(struct thread *td, void *dst, size_t *off)
143 {
144
145 }
146
147 /*
148  * Following 4 functions are used to manupilate bits on 32bit interger value.
149  * FIXME: I implemetend for ease-to-understand rather than for well-optimized.
150  */
151 static uint32_t
152 gen_bitmask(int msb, int lsb)
153 {
154         uint32_t mask;
155
156         if (msb == sizeof(mask) * 8 - 1)
157                 mask = ~0;
158         else
159                 mask = (1U << (msb + 1)) - 1;
160
161         if (lsb > 0)
162                 mask &= ~((1U << lsb) - 1);
163
164         return (mask);
165 }
166
167 static uint32_t
168 extract_bits(uint32_t x, int msb, int lsb)
169 {
170         uint32_t mask;
171
172         mask = gen_bitmask(msb, lsb);
173
174         x &= mask;
175         x >>= lsb;
176
177         return (x);
178 }
179
180 static uint32_t
181 insert_bits(uint32_t d, uint32_t s, int msb, int lsb)
182 {
183         uint32_t mask;
184
185         mask = gen_bitmask(msb, lsb);
186
187         d &= ~mask;
188
189         s <<= lsb;
190         s &= mask;
191
192         return (d | s);
193 }
194
195 static uint32_t
196 insert_imm(uint32_t insn, uint32_t imm, int imm_msb, int imm_lsb,
197     int insn_lsb)
198 {
199         int insn_msb;
200         uint32_t v;
201
202         v = extract_bits(imm, imm_msb, imm_lsb);
203         insn_msb = (imm_msb - imm_lsb) + insn_lsb;
204
205         return (insert_bits(insn, v, insn_msb, insn_lsb));
206 }
207
208 /*
209  * The RISC-V ISA is designed so that all of immediate values are
210  * sign-extended.
211  * An immediate value is sometimes generated at runtime by adding
212  * 12bit sign integer and 20bit signed integer. This requests 20bit
213  * immediate value to be ajusted if the MSB of the 12bit immediate
214  * value is asserted (sign-extended value is treated as negative value).
215  *
216  * For example, 0x123800 can be calculated by adding upper 20 bit of
217  * 0x124000 and sign-extended 12bit immediate whose bit pattern is
218  * 0x800 as follows:
219  *   0x123800
220  *     = 0x123000 + 0x800
221  *     = (0x123000 + 0x1000) + (-0x1000 + 0x800)
222  *     = (0x123000 + 0x1000) + (0xff...ff800)
223  *     = 0x124000            + sign-extention(0x800)
224  */
225 static uint32_t
226 calc_hi20_imm(uint32_t value)
227 {
228         /*
229          * There is the arithmetical hack that can remove conditional
230          * statement. But I implement it in straightforward way.
231          */
232         if ((value & 0x800) != 0)
233                 value += 0x1000;
234         return (value & ~0xfff);
235 }
236
237 static const struct type2str_ent t2s[] = {
238         { R_RISCV_NONE,         "R_RISCV_NONE"          },
239         { R_RISCV_64,           "R_RISCV_64"            },
240         { R_RISCV_JUMP_SLOT,    "R_RISCV_JUMP_SLOT"     },
241         { R_RISCV_RELATIVE,     "R_RISCV_RELATIVE"      },
242         { R_RISCV_JAL,          "R_RISCV_JAL"           },
243         { R_RISCV_CALL,         "R_RISCV_CALL"          },
244         { R_RISCV_PCREL_HI20,   "R_RISCV_PCREL_HI20"    },
245         { R_RISCV_PCREL_LO12_I, "R_RISCV_PCREL_LO12_I"  },
246         { R_RISCV_PCREL_LO12_S, "R_RISCV_PCREL_LO12_S"  },
247         { R_RISCV_HI20,         "R_RISCV_HI20"          },
248         { R_RISCV_LO12_I,       "R_RISCV_LO12_I"        },
249         { R_RISCV_LO12_S,       "R_RISCV_LO12_S"        },
250 };
251
252 static const char *
253 reloctype_to_str(int type)
254 {
255         int i;
256
257         for (i = 0; i < sizeof(t2s) / sizeof(t2s[0]); ++i) {
258                 if (type == t2s[i].type)
259                         return t2s[i].str;
260         }
261
262         return "*unknown*";
263 }
264
265 bool
266 elf_is_ifunc_reloc(Elf_Size r_info __unused)
267 {
268
269         return (false);
270 }
271
272 /*
273  * Currently kernel loadable module for RISCV is compiled with -fPIC option.
274  * (see also additional CFLAGS definition for RISCV in sys/conf/kmod.mk)
275  * Only R_RISCV_64, R_RISCV_JUMP_SLOT and RISCV_RELATIVE are emitted in
276  * the module. Other relocations will be processed when kernel loadable
277  * modules are built in non-PIC.
278  *
279  * FIXME: only RISCV64 is supported.
280  */
281 static int
282 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
283     int type, int local, elf_lookup_fn lookup)
284 {
285         Elf_Size rtype, symidx;
286         const Elf_Rela *rela;
287         Elf_Addr val, addr;
288         Elf64_Addr *where;
289         Elf_Addr addend;
290         uint32_t before32_1;
291         uint32_t before32;
292         uint64_t before64;
293         uint32_t *insn32p;
294         uint32_t imm20;
295         int error;
296
297         switch (type) {
298         case ELF_RELOC_RELA:
299                 rela = (const Elf_Rela *)data;
300                 where = (Elf_Addr *)(relocbase + rela->r_offset);
301                 insn32p = (uint32_t *)where;
302                 addend = rela->r_addend;
303                 rtype = ELF_R_TYPE(rela->r_info);
304                 symidx = ELF_R_SYM(rela->r_info);
305                 break;
306         default:
307                 printf("%s:%d unknown reloc type %d\n",
308                     __FUNCTION__, __LINE__, type);
309                 return (-1);
310         }
311
312         switch (rtype) {
313         case R_RISCV_NONE:
314                 break;
315
316         case R_RISCV_64:
317         case R_RISCV_JUMP_SLOT:
318                 error = lookup(lf, symidx, 1, &addr);
319                 if (error != 0)
320                         return (-1);
321
322                 val = addr;
323                 before64 = *where;
324                 if (*where != val)
325                         *where = val;
326                 if (debug_kld)
327                         printf("%p %c %-24s %016lx -> %016lx\n", where,
328                             (local ? 'l' : 'g'), reloctype_to_str(rtype),
329                             before64, *where);
330                 break;
331
332         case R_RISCV_RELATIVE:
333                 before64 = *where;
334                 *where = elf_relocaddr(lf, relocbase + addend);
335                 if (debug_kld)
336                         printf("%p %c %-24s %016lx -> %016lx\n", where,
337                             (local ? 'l' : 'g'), reloctype_to_str(rtype),
338                             before64, *where);
339                 break;
340
341         case R_RISCV_JAL:
342                 error = lookup(lf, symidx, 1, &addr);
343                 if (error != 0)
344                         return (-1);
345
346                 val = addr - (Elf_Addr)where;
347                 if (val <= -(1UL << 20) || (1UL << 20) <= val) {
348                         printf("kldload: huge offset against R_RISCV_JAL\n");
349                         return (-1);
350                 }
351
352                 before32 = *insn32p;
353                 *insn32p = insert_imm(*insn32p, val, 20, 20, 31);
354                 *insn32p = insert_imm(*insn32p, val, 10,  1, 21);
355                 *insn32p = insert_imm(*insn32p, val, 11, 11, 20);
356                 *insn32p = insert_imm(*insn32p, val, 19, 12, 12);
357                 if (debug_kld)
358                         printf("%p %c %-24s %08x -> %08x\n", where,
359                             (local ? 'l' : 'g'), reloctype_to_str(rtype),
360                             before32, *insn32p);
361                 break;
362
363         case R_RISCV_CALL:
364                 /*
365                  * R_RISCV_CALL relocates 8-byte region that consists
366                  * of the sequence of AUIPC and JALR.
367                  */
368                 /* Calculate and check the pc relative offset. */
369                 error = lookup(lf, symidx, 1, &addr);
370                 if (error != 0)
371                         return (-1);
372
373                 val = addr - (Elf_Addr)where;
374                 if (val <= -(1UL << 32) || (1UL << 32) <= val) {
375                         printf("kldload: huge offset against R_RISCV_CALL\n");
376                         return (-1);
377                 }
378
379                 /* Relocate AUIPC. */
380                 before32 = insn32p[0];
381                 imm20 = calc_hi20_imm(val);
382                 insn32p[0] = insert_imm(insn32p[0], imm20, 31, 12, 12);
383
384                 /* Relocate JALR. */
385                 before32_1 = insn32p[1];
386                 insn32p[1] = insert_imm(insn32p[1], val, 11,  0, 20);
387                 if (debug_kld)
388                         printf("%p %c %-24s %08x %08x -> %08x %08x\n", where,
389                             (local ? 'l' : 'g'), reloctype_to_str(rtype),
390                             before32, insn32p[0], before32_1, insn32p[1]);
391                 break;
392
393         case R_RISCV_PCREL_HI20:
394                 error = lookup(lf, symidx, 1, &addr);
395                 if (error != 0)
396                         return (-1);
397
398                 val = addr - (Elf_Addr)where;
399                 insn32p = (uint32_t *)where;
400                 before32 = *insn32p;
401                 imm20 = calc_hi20_imm(val);
402                 *insn32p = insert_imm(*insn32p, imm20, 31, 12, 12);
403                 if (debug_kld)
404                         printf("%p %c %-24s %08x -> %08x\n", where,
405                             (local ? 'l' : 'g'), reloctype_to_str(rtype),
406                             before32, *insn32p);
407                 break;
408
409         case R_RISCV_PCREL_LO12_I:
410                 error = lookup(lf, symidx, 1, &addr);
411                 if (error != 0)
412                         return (-1);
413
414                 val = addr - (Elf_Addr)where;
415                 insn32p = (uint32_t *)where;
416                 before32 = *insn32p;
417                 *insn32p = insert_imm(*insn32p, addr, 11,  0, 20);
418                 if (debug_kld)
419                         printf("%p %c %-24s %08x -> %08x\n", where,
420                             (local ? 'l' : 'g'), reloctype_to_str(rtype),
421                             before32, *insn32p);
422                 break;
423
424         case R_RISCV_PCREL_LO12_S:
425                 error = lookup(lf, symidx, 1, &addr);
426                 if (error != 0)
427                         return (-1);
428
429                 val = addr - (Elf_Addr)where;
430                 insn32p = (uint32_t *)where;
431                 before32 = *insn32p;
432                 *insn32p = insert_imm(*insn32p, addr, 11,  5, 25);
433                 *insn32p = insert_imm(*insn32p, addr,  4,  0,  7);
434                 if (debug_kld)
435                         printf("%p %c %-24s %08x -> %08x\n", where,
436                             (local ? 'l' : 'g'), reloctype_to_str(rtype),
437                             before32, *insn32p);
438                 break;
439
440         case R_RISCV_HI20:
441                 error = lookup(lf, symidx, 1, &addr);
442                 if (error != 0)
443                         return (-1);
444
445                 val = addr;
446                 insn32p = (uint32_t *)where;
447                 before32 = *insn32p;
448                 imm20 = calc_hi20_imm(val);
449                 *insn32p = insert_imm(*insn32p, imm20, 31, 12, 12);
450                 if (debug_kld)
451                         printf("%p %c %-24s %08x -> %08x\n", where,
452                             (local ? 'l' : 'g'), reloctype_to_str(rtype),
453                             before32, *insn32p);
454                 break;
455
456         case R_RISCV_LO12_I:
457                 error = lookup(lf, symidx, 1, &addr);
458                 if (error != 0)
459                         return (-1);
460
461                 val = addr;
462                 insn32p = (uint32_t *)where;
463                 before32 = *insn32p;
464                 *insn32p = insert_imm(*insn32p, addr, 11,  0, 20);
465                 if (debug_kld)
466                         printf("%p %c %-24s %08x -> %08x\n", where,
467                             (local ? 'l' : 'g'), reloctype_to_str(rtype),
468                             before32, *insn32p);
469                 break;
470
471         case R_RISCV_LO12_S:
472                 error = lookup(lf, symidx, 1, &addr);
473                 if (error != 0)
474                         return (-1);
475
476                 val = addr;
477                 insn32p = (uint32_t *)where;
478                 before32 = *insn32p;
479                 *insn32p = insert_imm(*insn32p, addr, 11,  5, 25);
480                 *insn32p = insert_imm(*insn32p, addr,  4,  0,  7);
481                 if (debug_kld)
482                         printf("%p %c %-24s %08x -> %08x\n", where,
483                             (local ? 'l' : 'g'), reloctype_to_str(rtype),
484                             before32, *insn32p);
485                 break;
486
487         default:
488                 printf("kldload: unexpected relocation type %ld, "
489                     "symbol index %ld\n", rtype, symidx);
490                 return (-1);
491         }
492
493         return (0);
494 }
495
496 int
497 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
498     elf_lookup_fn lookup)
499 {
500
501         return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
502 }
503
504 int
505 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
506     int type, elf_lookup_fn lookup)
507 {
508
509         return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
510 }
511
512 int
513 elf_cpu_load_file(linker_file_t lf __unused)
514 {
515
516         return (0);
517 }
518
519 int
520 elf_cpu_unload_file(linker_file_t lf __unused)
521 {
522
523         return (0);
524 }
525
526 int
527 elf_cpu_parse_dynamic(caddr_t loadbase __unused, Elf_Dyn *dynamic __unused)
528 {
529
530         return (0);
531 }