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