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