xref: /freebsd/sys/powerpc/powerpc/elf64_machdep.c (revision 85007872d1227006adf2ce119fe30de856cbe12d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 
28 #include <sys/param.h>
29 #include <sys/kernel.h>
30 #include <sys/systm.h>
31 #include <sys/elf.h>
32 #include <sys/exec.h>
33 #include <sys/imgact.h>
34 #include <sys/malloc.h>
35 #include <sys/proc.h>
36 #include <sys/namei.h>
37 #include <sys/fcntl.h>
38 #include <sys/reg.h>
39 #include <sys/sysent.h>
40 #include <sys/imgact_elf.h>
41 #include <sys/jail.h>
42 #include <sys/smp.h>
43 #include <sys/syscall.h>
44 #include <sys/signalvar.h>
45 #include <sys/vnode.h>
46 #include <sys/linker.h>
47 
48 #include <vm/vm.h>
49 #include <vm/vm_param.h>
50 #include <vm/pmap.h>
51 #include <vm/vm_map.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_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_psstringssz	= sizeof(struct ps_strings),
82 	.sv_stackprot	= VM_PROT_ALL,
83 	.sv_copyout_auxargs = __elfN(powerpc_copyout_auxargs),
84 	.sv_copyout_strings = exec_copyout_strings,
85 	.sv_setregs	= exec_setregs_funcdesc,
86 	.sv_fixlimit	= NULL,
87 	.sv_maxssiz	= NULL,
88 	.sv_flags	= SV_ABI_FREEBSD | SV_LP64 | SV_SHP | SV_ASLR |
89 			    SV_TIMEKEEP | SV_RNG_SEED_VER | SV_SIGSYS,
90 	.sv_set_syscall_retval = cpu_set_syscall_retval,
91 	.sv_fetch_syscall_args = cpu_fetch_syscall_args,
92 	.sv_syscallnames = syscallnames,
93 	.sv_shared_page_base = SHAREDPAGE,
94 	.sv_shared_page_len = PAGE_SIZE,
95 	.sv_schedtail	= NULL,
96 	.sv_thread_detach = NULL,
97 	.sv_trap	= NULL,
98 	.sv_hwcap	= &cpu_features,
99 	.sv_hwcap2	= &cpu_features2,
100 	.sv_hwcap3	= NULL,
101 	.sv_hwcap4	= NULL,
102 	.sv_onexec_old	= exec_onexec_old,
103 	.sv_onexit	= exit_onexit,
104 	.sv_regset_begin = SET_BEGIN(__elfN(regset)),
105 	.sv_regset_end  = SET_LIMIT(__elfN(regset)),
106 };
107 
108 struct sysentvec elf64_freebsd_sysvec_v2 = {
109 	.sv_size	= SYS_MAXSYSCALL,
110 	.sv_table	= sysent,
111 	.sv_fixup	= __elfN(freebsd_fixup),
112 	.sv_sendsig	= sendsig,
113 	.sv_sigcode	= sigcode64, /* Fixed up in ppc64_init_sysvecs(). */
114 	.sv_szsigcode	= &szsigcode64,
115 	.sv_name	= "FreeBSD ELF64 V2",
116 	.sv_coredump	= __elfN(coredump),
117 	.sv_elf_core_osabi = ELFOSABI_FREEBSD,
118 	.sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR,
119 	.sv_elf_core_prepare_notes = __elfN(prepare_notes),
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 | SV_SIGSYS,
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_hwcap3	= NULL,
145 	.sv_hwcap4	= NULL,
146 	.sv_onexec_old	= exec_onexec_old,
147 	.sv_onexit	= exit_onexit,
148 	.sv_regset_begin = SET_BEGIN(__elfN(regset)),
149 	.sv_regset_end  = SET_LIMIT(__elfN(regset)),
150 };
151 
152 static bool ppc64_elfv1_header_match(const struct image_params *params,
153     const int32_t *, const uint32_t *);
154 static bool ppc64_elfv2_header_match(const struct image_params *params,
155     const int32_t *, const uint32_t *);
156 
157 static Elf64_Brandinfo freebsd_brand_info_elfv1 = {
158 	.brand		= ELFOSABI_FREEBSD,
159 	.machine	= EM_PPC64,
160 	.compat_3_brand	= "FreeBSD",
161 	.interp_path	= "/libexec/ld-elf.so.1",
162 	.sysvec		= &elf64_freebsd_sysvec_v1,
163 	.interp_newpath	= NULL,
164 	.brand_note	= &elf64_freebsd_brandnote,
165 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
166 	.header_supported = &ppc64_elfv1_header_match
167 };
168 
169 SYSINIT(elf64v1, SI_SUB_EXEC, SI_ORDER_ANY,
170     (sysinit_cfunc_t) elf64_insert_brand_entry,
171     &freebsd_brand_info_elfv1);
172 
173 static Elf64_Brandinfo freebsd_brand_info_elfv2 = {
174 	.brand		= ELFOSABI_FREEBSD,
175 	.machine	= EM_PPC64,
176 	.compat_3_brand	= "FreeBSD",
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 	.interp_path	= "/usr/libexec/ld-elf.so.1",
194 	.sysvec		= &elf64_freebsd_sysvec_v1,
195 	.interp_newpath	= NULL,
196 	.brand_note	= &elf64_freebsd_brandnote,
197 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
198 	.header_supported = &ppc64_elfv1_header_match
199 };
200 
201 SYSINIT(oelf64, SI_SUB_EXEC, SI_ORDER_ANY,
202 	(sysinit_cfunc_t) elf64_insert_brand_entry,
203 	&freebsd_brand_oinfo);
204 
205 void elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase);
206 
207 static void
ppc64_init_sysvecs(void * arg)208 ppc64_init_sysvecs(void *arg)
209 {
210 	exec_sysvec_init(&elf64_freebsd_sysvec_v2);
211 	exec_sysvec_init_secondary(&elf64_freebsd_sysvec_v2,
212 	    &elf64_freebsd_sysvec_v1);
213 	/*
214 	 * Adjust elfv2 sigcode after elfv1 sysvec is initialized.
215 	 * exec_sysvec_init_secondary() assumes secondary sysvecs use
216 	 * identical signal code, and skips allocating a second copy.
217 	 * Since the ELFv2 trampoline is a strict subset of the ELFv1 code,
218 	 * we can work around this by adjusting the offset. This also
219 	 * avoids two copies of the trampoline code being allocated!
220 	 */
221 	elf64_freebsd_sysvec_v2.sv_sigcode_offset +=
222 	    (uintptr_t)sigcode64_elfv2 - (uintptr_t)&sigcode64;
223 	elf64_freebsd_sysvec_v2.sv_szsigcode = &szsigcode64_elfv2;
224 }
225 SYSINIT(elf64_sysvec, SI_SUB_EXEC, SI_ORDER_ANY, ppc64_init_sysvecs, NULL);
226 
227 static bool
ppc64_elfv1_header_match(const struct image_params * params,const int32_t * osrel __unused,const uint32_t * fctl0 __unused)228 ppc64_elfv1_header_match(const struct image_params *params,
229     const int32_t *osrel __unused, const uint32_t *fctl0 __unused)
230 {
231 	const Elf64_Ehdr *hdr = (const Elf64_Ehdr *)params->image_header;
232 	int abi = (hdr->e_flags & 3);
233 
234 	return (abi == 0 || abi == 1);
235 }
236 
237 static bool
ppc64_elfv2_header_match(const struct image_params * params,const int32_t * osrel __unused,const uint32_t * fctl0 __unused)238 ppc64_elfv2_header_match(const struct image_params *params,
239     const int32_t *osrel __unused, const uint32_t *fctl0 __unused)
240 {
241 	const Elf64_Ehdr *hdr = (const Elf64_Ehdr *)params->image_header;
242 	int abi = (hdr->e_flags & 3);
243 
244 	return (abi == 2);
245 }
246 
247 static void
exec_setregs_funcdesc(struct thread * td,struct image_params * imgp,uintptr_t stack)248 exec_setregs_funcdesc(struct thread *td, struct image_params *imgp,
249     uintptr_t stack)
250 {
251 	struct trapframe *tf;
252 	register_t entry_desc[3];
253 
254 	tf = trapframe(td);
255 	exec_setregs(td, imgp, stack);
256 
257 	/*
258 	 * For 64-bit ELFv1, we need to disentangle the function
259 	 * descriptor
260 	 *
261 	 * 0. entry point
262 	 * 1. TOC value (r2)
263 	 * 2. Environment pointer (r11)
264 	 */
265 
266 	(void)copyin((void *)imgp->entry_addr, entry_desc,
267 	    sizeof(entry_desc));
268 	tf->srr0 = entry_desc[0] + imgp->reloc_base;
269 	tf->fixreg[2] = entry_desc[1] + imgp->reloc_base;
270 	tf->fixreg[11] = entry_desc[2] + imgp->reloc_base;
271 }
272 
273 void
elf64_dump_thread(struct thread * td,void * dst,size_t * off)274 elf64_dump_thread(struct thread *td, void *dst, size_t *off)
275 {
276 	size_t len;
277 	struct pcb *pcb;
278 	uint64_t vshr[32];
279 	uint64_t *vsr_dw1;
280 	int vsr_idx;
281 
282 	len = 0;
283 	pcb = td->td_pcb;
284 
285 	if (pcb->pcb_flags & PCB_VEC) {
286 		if (dst != NULL) {
287 			len += elf64_populate_note(NT_PPC_VMX,
288 			    &pcb->pcb_vec, (char *)dst + len,
289 			    sizeof(pcb->pcb_vec), NULL);
290 		} else
291 			len += elf64_populate_note(NT_PPC_VMX, NULL, NULL,
292 			    sizeof(pcb->pcb_vec), NULL);
293 	}
294 
295 	if (pcb->pcb_flags & PCB_VSX) {
296 		if (dst != NULL) {
297 			/*
298 			 * Doubleword 0 of VSR0-VSR31 overlap with FPR0-FPR31 and
299 			 * VSR32-VSR63 overlap with VR0-VR31, so we only copy
300 			 * the non-overlapping data, which is doubleword 1 of VSR0-VSR31.
301 			 */
302 			for (vsr_idx = 0; vsr_idx < nitems(vshr); vsr_idx++) {
303 				vsr_dw1 = (uint64_t *)&pcb->pcb_fpu.fpr[vsr_idx].vsr[2];
304 				vshr[vsr_idx] = *vsr_dw1;
305 			}
306 			len += elf64_populate_note(NT_PPC_VSX,
307 			    vshr, (char *)dst + len,
308 			    sizeof(vshr), NULL);
309 		} else
310 			len += elf64_populate_note(NT_PPC_VSX, NULL, NULL,
311 			    sizeof(vshr), NULL);
312 	}
313 
314 	*off = len;
315 }
316 
317 bool
elf_is_ifunc_reloc(Elf_Size r_info)318 elf_is_ifunc_reloc(Elf_Size r_info)
319 {
320 
321 	return (ELF_R_TYPE(r_info) == R_PPC_IRELATIVE);
322 }
323 
324 /* Process one elf relocation with addend. */
325 static int
elf_reloc_internal(linker_file_t lf,Elf_Addr relocbase,const void * data,int type,int local,elf_lookup_fn lookup)326 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
327     int type, int local, elf_lookup_fn lookup)
328 {
329 	Elf_Addr *where;
330 	Elf_Addr addr;
331 	Elf_Addr addend, val;
332 	Elf_Word rtype, symidx;
333 	const Elf_Rela *rela;
334 	int error;
335 
336 	switch (type) {
337 	case ELF_RELOC_REL:
338 		panic("PPC only supports RELA relocations");
339 		break;
340 	case ELF_RELOC_RELA:
341 		rela = (const Elf_Rela *)data;
342 		where = (Elf_Addr *) (relocbase + rela->r_offset);
343 		addend = rela->r_addend;
344 		rtype = ELF_R_TYPE(rela->r_info);
345 		symidx = ELF_R_SYM(rela->r_info);
346 		break;
347 	default:
348 		panic("elf_reloc: unknown relocation mode %d\n", type);
349 	}
350 
351 	switch (rtype) {
352 	case R_PPC_NONE:
353 		break;
354 
355 	case R_PPC64_ADDR64:	/* doubleword64 S + A */
356 		error = lookup(lf, symidx, 1, &addr);
357 		if (error != 0)
358 			return (-1);
359 		addr += addend;
360 		*where = addr;
361 		break;
362 
363 	case R_PPC_RELATIVE:	/* doubleword64 B + A */
364 		*where = elf_relocaddr(lf, relocbase + addend);
365 		break;
366 
367 	case R_PPC_JMP_SLOT:	/* function descriptor copy */
368 		lookup(lf, symidx, 1, &addr);
369 #if !defined(_CALL_ELF) || _CALL_ELF == 1
370 		memcpy(where, (Elf_Addr *)addr, 3*sizeof(Elf_Addr));
371 #else
372 		*where = addr;
373 #endif
374 		__asm __volatile("dcbst 0,%0; sync" :: "r"(where) : "memory");
375 		break;
376 
377 	case R_PPC_IRELATIVE:
378 		addr = relocbase + addend;
379 		val = ((Elf64_Addr (*)(void))addr)();
380 		if (*where != val)
381 			*where = val;
382 		break;
383 
384 	default:
385 		printf("kldload: unexpected relocation type %d, "
386 		    "symbol index %d\n", (int)rtype, symidx);
387 		return (-1);
388 	}
389 	return (0);
390 }
391 
392 void
elf_reloc_self(Elf_Dyn * dynp,Elf_Addr relocbase)393 elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase)
394 {
395 	Elf_Rela *rela = NULL, *relalim;
396 	Elf_Addr relasz = 0;
397 	Elf_Addr *where;
398 
399 	/*
400 	 * Extract the rela/relasz values from the dynamic section
401 	 */
402 	for (; dynp->d_tag != DT_NULL; dynp++) {
403 		switch (dynp->d_tag) {
404 		case DT_RELA:
405 			rela = (Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
406 			break;
407 		case DT_RELASZ:
408 			relasz = dynp->d_un.d_val;
409 			break;
410 		}
411 	}
412 
413 	/*
414 	 * Relocate these values
415 	 */
416 	relalim = (Elf_Rela *)((caddr_t)rela + relasz);
417 	for (; rela < relalim; rela++) {
418 		if (ELF_R_TYPE(rela->r_info) != R_PPC_RELATIVE)
419 			continue;
420 		where = (Elf_Addr *)(relocbase + rela->r_offset);
421 		*where = (Elf_Addr)(relocbase + rela->r_addend);
422 	}
423 }
424 
425 int
elf_reloc(linker_file_t lf,Elf_Addr relocbase,const void * data,int type,elf_lookup_fn lookup)426 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
427     elf_lookup_fn lookup)
428 {
429 
430 	return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
431 }
432 
433 int
elf_reloc_local(linker_file_t lf,Elf_Addr relocbase,const void * data,int type,elf_lookup_fn lookup)434 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
435     int type, elf_lookup_fn lookup)
436 {
437 
438 	return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
439 }
440 
441 int
elf_cpu_load_file(linker_file_t lf)442 elf_cpu_load_file(linker_file_t lf)
443 {
444 	/* Only sync the cache for non-kernel modules */
445 	if (lf->id != 1)
446 		__syncicache(lf->address, lf->size);
447 	return (0);
448 }
449 
450 int
elf_cpu_unload_file(linker_file_t lf __unused)451 elf_cpu_unload_file(linker_file_t lf __unused)
452 {
453 
454 	return (0);
455 }
456 
457 int
elf_cpu_parse_dynamic(caddr_t loadbase __unused,Elf_Dyn * dynamic __unused)458 elf_cpu_parse_dynamic(caddr_t loadbase __unused, Elf_Dyn *dynamic __unused)
459 {
460 
461 	return (0);
462 }
463