xref: /freebsd/sys/powerpc/powerpc/elf64_machdep.c (revision e3aa18ad)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright 1996-1998 John D. Polstra.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.h>
33 #include <sys/elf.h>
34 #include <sys/exec.h>
35 #include <sys/imgact.h>
36 #include <sys/malloc.h>
37 #include <sys/proc.h>
38 #include <sys/namei.h>
39 #include <sys/fcntl.h>
40 #include <sys/reg.h>
41 #include <sys/sysent.h>
42 #include <sys/imgact_elf.h>
43 #include <sys/jail.h>
44 #include <sys/smp.h>
45 #include <sys/syscall.h>
46 #include <sys/signalvar.h>
47 #include <sys/vnode.h>
48 #include <sys/linker.h>
49 
50 #include <vm/vm.h>
51 #include <vm/vm_param.h>
52 
53 #include <machine/altivec.h>
54 #include <machine/cpu.h>
55 #include <machine/fpu.h>
56 #include <machine/elf.h>
57 #include <machine/md_var.h>
58 
59 #include <powerpc/powerpc/elf_common.c>
60 
61 static void exec_setregs_funcdesc(struct thread *td, struct image_params *imgp,
62     uintptr_t stack);
63 
64 struct sysentvec elf64_freebsd_sysvec_v1 = {
65 	.sv_size	= SYS_MAXSYSCALL,
66 	.sv_table	= sysent,
67 	.sv_fixup	= __elfN(freebsd_fixup),
68 	.sv_sendsig	= sendsig,
69 	.sv_sigcode	= sigcode64,
70 	.sv_szsigcode	= &szsigcode64,
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_imgact_try	= NULL,
77 	.sv_minsigstksz	= MINSIGSTKSZ,
78 	.sv_minuser	= VM_MIN_ADDRESS,
79 	.sv_maxuser	= VM_MAXUSER_ADDRESS,
80 	.sv_usrstack	= USRSTACK,
81 	.sv_psstrings	= PS_STRINGS,
82 	.sv_psstringssz	= sizeof(struct ps_strings),
83 	.sv_stackprot	= VM_PROT_ALL,
84 	.sv_copyout_auxargs = __elfN(powerpc_copyout_auxargs),
85 	.sv_copyout_strings = exec_copyout_strings,
86 	.sv_setregs	= exec_setregs_funcdesc,
87 	.sv_fixlimit	= NULL,
88 	.sv_maxssiz	= NULL,
89 	.sv_flags	= SV_ABI_FREEBSD | SV_LP64 | SV_SHP | SV_ASLR |
90 			    SV_TIMEKEEP | SV_RNG_SEED_VER,
91 	.sv_set_syscall_retval = cpu_set_syscall_retval,
92 	.sv_fetch_syscall_args = cpu_fetch_syscall_args,
93 	.sv_syscallnames = syscallnames,
94 	.sv_shared_page_base = SHAREDPAGE,
95 	.sv_shared_page_len = PAGE_SIZE,
96 	.sv_schedtail	= NULL,
97 	.sv_thread_detach = NULL,
98 	.sv_trap	= NULL,
99 	.sv_hwcap	= &cpu_features,
100 	.sv_hwcap2	= &cpu_features2,
101 	.sv_onexec_old	= exec_onexec_old,
102 	.sv_onexit	= exit_onexit,
103 	.sv_regset_begin = SET_BEGIN(__elfN(regset)),
104 	.sv_regset_end  = SET_LIMIT(__elfN(regset)),
105 };
106 
107 struct sysentvec elf64_freebsd_sysvec_v2 = {
108 	.sv_size	= SYS_MAXSYSCALL,
109 	.sv_table	= sysent,
110 	.sv_fixup	= __elfN(freebsd_fixup),
111 	.sv_sendsig	= sendsig,
112 	.sv_sigcode	= sigcode64, /* Fixed up in ppc64_init_sysvecs(). */
113 	.sv_szsigcode	= &szsigcode64,
114 	.sv_name	= "FreeBSD ELF64 V2",
115 	.sv_coredump	= __elfN(coredump),
116 	.sv_elf_core_osabi = ELFOSABI_FREEBSD,
117 	.sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR,
118 	.sv_elf_core_prepare_notes = __elfN(prepare_notes),
119 	.sv_imgact_try	= NULL,
120 	.sv_minsigstksz	= MINSIGSTKSZ,
121 	.sv_minuser	= VM_MIN_ADDRESS,
122 	.sv_maxuser	= VM_MAXUSER_ADDRESS,
123 	.sv_usrstack	= USRSTACK,
124 	.sv_psstrings	= PS_STRINGS,
125 	.sv_psstringssz	= sizeof(struct ps_strings),
126 	.sv_stackprot	= VM_PROT_ALL,
127 	.sv_copyout_auxargs = __elfN(powerpc_copyout_auxargs),
128 	.sv_copyout_strings = exec_copyout_strings,
129 	.sv_setregs	= exec_setregs,
130 	.sv_fixlimit	= NULL,
131 	.sv_maxssiz	= NULL,
132 	.sv_flags	= SV_ABI_FREEBSD | SV_LP64 | SV_SHP |
133 			    SV_TIMEKEEP | SV_RNG_SEED_VER,
134 	.sv_set_syscall_retval = cpu_set_syscall_retval,
135 	.sv_fetch_syscall_args = cpu_fetch_syscall_args,
136 	.sv_syscallnames = syscallnames,
137 	.sv_shared_page_base = SHAREDPAGE,
138 	.sv_shared_page_len = PAGE_SIZE,
139 	.sv_schedtail	= NULL,
140 	.sv_thread_detach = NULL,
141 	.sv_trap	= NULL,
142 	.sv_hwcap	= &cpu_features,
143 	.sv_hwcap2	= &cpu_features2,
144 	.sv_onexec_old	= exec_onexec_old,
145 	.sv_onexit	= exit_onexit,
146 	.sv_regset_begin = SET_BEGIN(__elfN(regset)),
147 	.sv_regset_end  = SET_LIMIT(__elfN(regset)),
148 };
149 
150 static boolean_t ppc64_elfv1_header_match(struct image_params *params,
151     int32_t *, uint32_t *);
152 static boolean_t ppc64_elfv2_header_match(struct image_params *params,
153     int32_t *, uint32_t *);
154 
155 static Elf64_Brandinfo freebsd_brand_info_elfv1 = {
156 	.brand		= ELFOSABI_FREEBSD,
157 	.machine	= EM_PPC64,
158 	.compat_3_brand	= "FreeBSD",
159 	.emul_path	= NULL,
160 	.interp_path	= "/libexec/ld-elf.so.1",
161 	.sysvec		= &elf64_freebsd_sysvec_v1,
162 	.interp_newpath	= NULL,
163 	.brand_note	= &elf64_freebsd_brandnote,
164 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
165 	.header_supported = &ppc64_elfv1_header_match
166 };
167 
168 SYSINIT(elf64v1, SI_SUB_EXEC, SI_ORDER_ANY,
169     (sysinit_cfunc_t) elf64_insert_brand_entry,
170     &freebsd_brand_info_elfv1);
171 
172 static Elf64_Brandinfo freebsd_brand_info_elfv2 = {
173 	.brand		= ELFOSABI_FREEBSD,
174 	.machine	= EM_PPC64,
175 	.compat_3_brand	= "FreeBSD",
176 	.emul_path	= NULL,
177 	.interp_path	= "/libexec/ld-elf.so.1",
178 	.sysvec		= &elf64_freebsd_sysvec_v2,
179 	.interp_newpath	= NULL,
180 	.brand_note	= &elf64_freebsd_brandnote,
181 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
182 	.header_supported = &ppc64_elfv2_header_match
183 };
184 
185 SYSINIT(elf64v2, SI_SUB_EXEC, SI_ORDER_ANY,
186     (sysinit_cfunc_t) elf64_insert_brand_entry,
187     &freebsd_brand_info_elfv2);
188 
189 static Elf64_Brandinfo freebsd_brand_oinfo = {
190 	.brand		= ELFOSABI_FREEBSD,
191 	.machine	= EM_PPC64,
192 	.compat_3_brand	= "FreeBSD",
193 	.emul_path	= NULL,
194 	.interp_path	= "/usr/libexec/ld-elf.so.1",
195 	.sysvec		= &elf64_freebsd_sysvec_v1,
196 	.interp_newpath	= NULL,
197 	.brand_note	= &elf64_freebsd_brandnote,
198 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
199 	.header_supported = &ppc64_elfv1_header_match
200 };
201 
202 SYSINIT(oelf64, SI_SUB_EXEC, SI_ORDER_ANY,
203 	(sysinit_cfunc_t) elf64_insert_brand_entry,
204 	&freebsd_brand_oinfo);
205 
206 void elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase);
207 
208 static void
209 ppc64_init_sysvecs(void *arg)
210 {
211 	exec_sysvec_init(&elf64_freebsd_sysvec_v2);
212 	exec_sysvec_init_secondary(&elf64_freebsd_sysvec_v2,
213 	    &elf64_freebsd_sysvec_v1);
214 	/*
215 	 * Adjust elfv2 sigcode after elfv1 sysvec is initialized.
216 	 * exec_sysvec_init_secondary() assumes secondary sysvecs use
217 	 * identical signal code, and skips allocating a second copy.
218 	 * Since the ELFv2 trampoline is a strict subset of the ELFv1 code,
219 	 * we can work around this by adjusting the base address. This also
220 	 * avoids two copies of the trampoline code being allocated!
221 	 */
222 	elf64_freebsd_sysvec_v2.sv_sigcode_base +=
223 	    (uintptr_t)sigcode64_elfv2 - (uintptr_t)&sigcode64;
224 	elf64_freebsd_sysvec_v2.sv_szsigcode = &szsigcode64_elfv2;
225 }
226 SYSINIT(elf64_sysvec, SI_SUB_EXEC, SI_ORDER_ANY, ppc64_init_sysvecs, NULL);
227 
228 static boolean_t
229 ppc64_elfv1_header_match(struct image_params *params, int32_t *osrel __unused,
230     uint32_t *fctl0 __unused)
231 {
232 	const Elf64_Ehdr *hdr = (const Elf64_Ehdr *)params->image_header;
233 	int abi = (hdr->e_flags & 3);
234 
235 	return (abi == 0 || abi == 1);
236 }
237 
238 static boolean_t
239 ppc64_elfv2_header_match(struct image_params *params, int32_t *osrel __unused,
240     uint32_t *fctl0 __unused)
241 {
242 	const Elf64_Ehdr *hdr = (const Elf64_Ehdr *)params->image_header;
243 	int abi = (hdr->e_flags & 3);
244 
245 	return (abi == 2);
246 }
247 
248 static void
249 exec_setregs_funcdesc(struct thread *td, struct image_params *imgp,
250     uintptr_t stack)
251 {
252 	struct trapframe *tf;
253 	register_t entry_desc[3];
254 
255 	tf = trapframe(td);
256 	exec_setregs(td, imgp, stack);
257 
258 	/*
259 	 * For 64-bit ELFv1, we need to disentangle the function
260 	 * descriptor
261 	 *
262 	 * 0. entry point
263 	 * 1. TOC value (r2)
264 	 * 2. Environment pointer (r11)
265 	 */
266 
267 	(void)copyin((void *)imgp->entry_addr, entry_desc,
268 	    sizeof(entry_desc));
269 	tf->srr0 = entry_desc[0] + imgp->reloc_base;
270 	tf->fixreg[2] = entry_desc[1] + imgp->reloc_base;
271 	tf->fixreg[11] = entry_desc[2] + imgp->reloc_base;
272 }
273 
274 void
275 elf64_dump_thread(struct thread *td, void *dst, size_t *off)
276 {
277 	size_t len;
278 	struct pcb *pcb;
279 	uint64_t vshr[32];
280 	uint64_t *vsr_dw1;
281 	int vsr_idx;
282 
283 	len = 0;
284 	pcb = td->td_pcb;
285 
286 	if (pcb->pcb_flags & PCB_VEC) {
287 		save_vec_nodrop(td);
288 		if (dst != NULL) {
289 			len += elf64_populate_note(NT_PPC_VMX,
290 			    &pcb->pcb_vec, (char *)dst + len,
291 			    sizeof(pcb->pcb_vec), NULL);
292 		} else
293 			len += elf64_populate_note(NT_PPC_VMX, NULL, NULL,
294 			    sizeof(pcb->pcb_vec), NULL);
295 	}
296 
297 	if (pcb->pcb_flags & PCB_VSX) {
298 		save_fpu_nodrop(td);
299 		if (dst != NULL) {
300 			/*
301 			 * Doubleword 0 of VSR0-VSR31 overlap with FPR0-FPR31 and
302 			 * VSR32-VSR63 overlap with VR0-VR31, so we only copy
303 			 * the non-overlapping data, which is doubleword 1 of VSR0-VSR31.
304 			 */
305 			for (vsr_idx = 0; vsr_idx < nitems(vshr); vsr_idx++) {
306 				vsr_dw1 = (uint64_t *)&pcb->pcb_fpu.fpr[vsr_idx].vsr[2];
307 				vshr[vsr_idx] = *vsr_dw1;
308 			}
309 			len += elf64_populate_note(NT_PPC_VSX,
310 			    vshr, (char *)dst + len,
311 			    sizeof(vshr), NULL);
312 		} else
313 			len += elf64_populate_note(NT_PPC_VSX, NULL, NULL,
314 			    sizeof(vshr), NULL);
315 	}
316 
317 	*off = len;
318 }
319 
320 bool
321 elf_is_ifunc_reloc(Elf_Size r_info)
322 {
323 
324 	return (ELF_R_TYPE(r_info) == R_PPC_IRELATIVE);
325 }
326 
327 /* Process one elf relocation with addend. */
328 static int
329 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
330     int type, int local, elf_lookup_fn lookup)
331 {
332 	Elf_Addr *where;
333 	Elf_Addr addr;
334 	Elf_Addr addend, val;
335 	Elf_Word rtype, symidx;
336 	const Elf_Rela *rela;
337 	int error;
338 
339 	switch (type) {
340 	case ELF_RELOC_REL:
341 		panic("PPC only supports RELA relocations");
342 		break;
343 	case ELF_RELOC_RELA:
344 		rela = (const Elf_Rela *)data;
345 		where = (Elf_Addr *) (relocbase + rela->r_offset);
346 		addend = rela->r_addend;
347 		rtype = ELF_R_TYPE(rela->r_info);
348 		symidx = ELF_R_SYM(rela->r_info);
349 		break;
350 	default:
351 		panic("elf_reloc: unknown relocation mode %d\n", type);
352 	}
353 
354 	switch (rtype) {
355 	case R_PPC_NONE:
356 		break;
357 
358 	case R_PPC64_ADDR64:	/* doubleword64 S + A */
359 		error = lookup(lf, symidx, 1, &addr);
360 		if (error != 0)
361 			return (-1);
362 		addr += addend;
363 		*where = addr;
364 		break;
365 
366 	case R_PPC_RELATIVE:	/* doubleword64 B + A */
367 		*where = elf_relocaddr(lf, relocbase + addend);
368 		break;
369 
370 	case R_PPC_JMP_SLOT:	/* function descriptor copy */
371 		lookup(lf, symidx, 1, &addr);
372 #if !defined(_CALL_ELF) || _CALL_ELF == 1
373 		memcpy(where, (Elf_Addr *)addr, 3*sizeof(Elf_Addr));
374 #else
375 		*where = addr;
376 #endif
377 		__asm __volatile("dcbst 0,%0; sync" :: "r"(where) : "memory");
378 		break;
379 
380 	case R_PPC_IRELATIVE:
381 		addr = relocbase + addend;
382 		val = ((Elf64_Addr (*)(void))addr)();
383 		if (*where != val)
384 			*where = val;
385 		break;
386 
387 	default:
388 		printf("kldload: unexpected relocation type %d, "
389 		    "symbol index %d\n", (int)rtype, symidx);
390 		return (-1);
391 	}
392 	return (0);
393 }
394 
395 void
396 elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase)
397 {
398 	Elf_Rela *rela = NULL, *relalim;
399 	Elf_Addr relasz = 0;
400 	Elf_Addr *where;
401 
402 	/*
403 	 * Extract the rela/relasz values from the dynamic section
404 	 */
405 	for (; dynp->d_tag != DT_NULL; dynp++) {
406 		switch (dynp->d_tag) {
407 		case DT_RELA:
408 			rela = (Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
409 			break;
410 		case DT_RELASZ:
411 			relasz = dynp->d_un.d_val;
412 			break;
413 		}
414 	}
415 
416 	/*
417 	 * Relocate these values
418 	 */
419 	relalim = (Elf_Rela *)((caddr_t)rela + relasz);
420 	for (; rela < relalim; rela++) {
421 		if (ELF_R_TYPE(rela->r_info) != R_PPC_RELATIVE)
422 			continue;
423 		where = (Elf_Addr *)(relocbase + rela->r_offset);
424 		*where = (Elf_Addr)(relocbase + rela->r_addend);
425 	}
426 }
427 
428 int
429 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
430     elf_lookup_fn lookup)
431 {
432 
433 	return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
434 }
435 
436 int
437 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
438     int type, elf_lookup_fn lookup)
439 {
440 
441 	return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
442 }
443 
444 int
445 elf_cpu_load_file(linker_file_t lf)
446 {
447 	/* Only sync the cache for non-kernel modules */
448 	if (lf->id != 1)
449 		__syncicache(lf->address, lf->size);
450 	return (0);
451 }
452 
453 int
454 elf_cpu_unload_file(linker_file_t lf __unused)
455 {
456 
457 	return (0);
458 }
459 
460 int
461 elf_cpu_parse_dynamic(caddr_t loadbase __unused, Elf_Dyn *dynamic __unused)
462 {
463 
464 	return (0);
465 }
466