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