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