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