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