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