xref: /freebsd/sys/powerpc/powerpc/elf64_machdep.c (revision 0957b409)
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/exec.h>
34 #include <sys/imgact.h>
35 #include <sys/malloc.h>
36 #include <sys/proc.h>
37 #include <sys/namei.h>
38 #include <sys/fcntl.h>
39 #include <sys/sysent.h>
40 #include <sys/imgact_elf.h>
41 #include <sys/syscall.h>
42 #include <sys/signalvar.h>
43 #include <sys/vnode.h>
44 #include <sys/linker.h>
45 
46 #include <vm/vm.h>
47 #include <vm/vm_param.h>
48 
49 #include <machine/altivec.h>
50 #include <machine/cpu.h>
51 #include <machine/fpu.h>
52 #include <machine/elf.h>
53 #include <machine/md_var.h>
54 
55 static void exec_setregs_funcdesc(struct thread *td, struct image_params *imgp,
56     u_long stack);
57 
58 struct sysentvec elf64_freebsd_sysvec_v1 = {
59 	.sv_size	= SYS_MAXSYSCALL,
60 	.sv_table	= sysent,
61 	.sv_errsize	= 0,
62 	.sv_errtbl	= NULL,
63 	.sv_transtrap	= NULL,
64 	.sv_fixup	= __elfN(freebsd_fixup),
65 	.sv_sendsig	= sendsig,
66 	.sv_sigcode	= sigcode64,
67 	.sv_szsigcode	= &szsigcode64,
68 	.sv_name	= "FreeBSD ELF64",
69 	.sv_coredump	= __elfN(coredump),
70 	.sv_imgact_try	= NULL,
71 	.sv_minsigstksz	= MINSIGSTKSZ,
72 	.sv_pagesize	= PAGE_SIZE,
73 	.sv_minuser	= VM_MIN_ADDRESS,
74 	.sv_maxuser	= VM_MAXUSER_ADDRESS,
75 	.sv_usrstack	= USRSTACK,
76 	.sv_psstrings	= PS_STRINGS,
77 	.sv_stackprot	= VM_PROT_ALL,
78 	.sv_copyout_strings = exec_copyout_strings,
79 	.sv_setregs	= exec_setregs_funcdesc,
80 	.sv_fixlimit	= NULL,
81 	.sv_maxssiz	= NULL,
82 	.sv_flags	= SV_ABI_FREEBSD | SV_LP64 | SV_SHP | SV_ASLR,
83 	.sv_set_syscall_retval = cpu_set_syscall_retval,
84 	.sv_fetch_syscall_args = cpu_fetch_syscall_args,
85 	.sv_syscallnames = syscallnames,
86 	.sv_shared_page_base = SHAREDPAGE,
87 	.sv_shared_page_len = PAGE_SIZE,
88 	.sv_schedtail	= NULL,
89 	.sv_thread_detach = NULL,
90 	.sv_trap	= NULL,
91 	.sv_hwcap	= &cpu_features,
92 	.sv_hwcap2	= &cpu_features2,
93 };
94 INIT_SYSENTVEC(elf64_sysvec_v1, &elf64_freebsd_sysvec_v1);
95 
96 struct sysentvec elf64_freebsd_sysvec_v2 = {
97 	.sv_size	= SYS_MAXSYSCALL,
98 	.sv_table	= sysent,
99 	.sv_errsize	= 0,
100 	.sv_errtbl	= NULL,
101 	.sv_transtrap	= NULL,
102 	.sv_fixup	= __elfN(freebsd_fixup),
103 	.sv_sendsig	= sendsig,
104 	.sv_sigcode	= sigcode64_elfv2,
105 	.sv_szsigcode	= &szsigcode64_elfv2,
106 	.sv_name	= "FreeBSD ELF64 V2",
107 	.sv_coredump	= __elfN(coredump),
108 	.sv_imgact_try	= NULL,
109 	.sv_minsigstksz	= MINSIGSTKSZ,
110 	.sv_pagesize	= PAGE_SIZE,
111 	.sv_minuser	= VM_MIN_ADDRESS,
112 	.sv_maxuser	= VM_MAXUSER_ADDRESS,
113 	.sv_usrstack	= USRSTACK,
114 	.sv_psstrings	= PS_STRINGS,
115 	.sv_stackprot	= VM_PROT_ALL,
116 	.sv_copyout_strings = exec_copyout_strings,
117 	.sv_setregs	= exec_setregs,
118 	.sv_fixlimit	= NULL,
119 	.sv_maxssiz	= NULL,
120 	.sv_flags	= SV_ABI_FREEBSD | SV_LP64 | SV_SHP,
121 	.sv_set_syscall_retval = cpu_set_syscall_retval,
122 	.sv_fetch_syscall_args = cpu_fetch_syscall_args,
123 	.sv_syscallnames = syscallnames,
124 	.sv_shared_page_base = SHAREDPAGE,
125 	.sv_shared_page_len = PAGE_SIZE,
126 	.sv_schedtail	= NULL,
127 	.sv_thread_detach = NULL,
128 	.sv_trap	= NULL,
129 	.sv_hwcap	= &cpu_features,
130 	.sv_hwcap2	= &cpu_features2,
131 };
132 INIT_SYSENTVEC(elf64_sysvec_v2, &elf64_freebsd_sysvec_v2);
133 
134 static boolean_t ppc64_elfv1_header_match(struct image_params *params);
135 static boolean_t ppc64_elfv2_header_match(struct image_params *params);
136 
137 static Elf64_Brandinfo freebsd_brand_info_elfv1 = {
138 	.brand		= ELFOSABI_FREEBSD,
139 	.machine	= EM_PPC64,
140 	.compat_3_brand	= "FreeBSD",
141 	.emul_path	= NULL,
142 	.interp_path	= "/libexec/ld-elf.so.1",
143 	.sysvec		= &elf64_freebsd_sysvec_v1,
144 	.interp_newpath	= NULL,
145 	.brand_note	= &elf64_freebsd_brandnote,
146 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
147 	.header_supported = &ppc64_elfv1_header_match
148 };
149 
150 SYSINIT(elf64v1, SI_SUB_EXEC, SI_ORDER_ANY,
151     (sysinit_cfunc_t) elf64_insert_brand_entry,
152     &freebsd_brand_info_elfv1);
153 
154 static Elf64_Brandinfo freebsd_brand_info_elfv2 = {
155 	.brand		= ELFOSABI_FREEBSD,
156 	.machine	= EM_PPC64,
157 	.compat_3_brand	= "FreeBSD",
158 	.emul_path	= NULL,
159 	.interp_path	= "/libexec/ld-elf.so.1",
160 	.sysvec		= &elf64_freebsd_sysvec_v2,
161 	.interp_newpath	= NULL,
162 	.brand_note	= &elf64_freebsd_brandnote,
163 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
164 	.header_supported = &ppc64_elfv2_header_match
165 };
166 
167 SYSINIT(elf64v2, SI_SUB_EXEC, SI_ORDER_ANY,
168     (sysinit_cfunc_t) elf64_insert_brand_entry,
169     &freebsd_brand_info_elfv2);
170 
171 static Elf64_Brandinfo freebsd_brand_oinfo = {
172 	.brand		= ELFOSABI_FREEBSD,
173 	.machine	= EM_PPC64,
174 	.compat_3_brand	= "FreeBSD",
175 	.emul_path	= NULL,
176 	.interp_path	= "/usr/libexec/ld-elf.so.1",
177 	.sysvec		= &elf64_freebsd_sysvec_v1,
178 	.interp_newpath	= NULL,
179 	.brand_note	= &elf64_freebsd_brandnote,
180 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
181 	.header_supported = &ppc64_elfv1_header_match
182 };
183 
184 SYSINIT(oelf64, SI_SUB_EXEC, SI_ORDER_ANY,
185 	(sysinit_cfunc_t) elf64_insert_brand_entry,
186 	&freebsd_brand_oinfo);
187 
188 void elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase);
189 
190 static boolean_t
191 ppc64_elfv1_header_match(struct image_params *params)
192 {
193 	const Elf64_Ehdr *hdr = (const Elf64_Ehdr *)params->image_header;
194 	int abi = (hdr->e_flags & 3);
195 
196 	return (abi == 0 || abi == 1);
197 }
198 
199 static boolean_t
200 ppc64_elfv2_header_match(struct image_params *params)
201 {
202 	const Elf64_Ehdr *hdr = (const Elf64_Ehdr *)params->image_header;
203 	int abi = (hdr->e_flags & 3);
204 
205 	return (abi == 2);
206 }
207 
208 static void
209 exec_setregs_funcdesc(struct thread *td, struct image_params *imgp,
210     u_long stack)
211 {
212 	struct trapframe *tf;
213 	register_t entry_desc[3];
214 
215 	tf = trapframe(td);
216 	exec_setregs(td, imgp, stack);
217 
218 	/*
219 	 * For 64-bit ELFv1, we need to disentangle the function
220 	 * descriptor
221 	 *
222 	 * 0. entry point
223 	 * 1. TOC value (r2)
224 	 * 2. Environment pointer (r11)
225 	 */
226 
227 	(void)copyin((void *)imgp->entry_addr, entry_desc,
228 	    sizeof(entry_desc));
229 	tf->srr0 = entry_desc[0] + imgp->reloc_base;
230 	tf->fixreg[2] = entry_desc[1] + imgp->reloc_base;
231 	tf->fixreg[11] = entry_desc[2] + imgp->reloc_base;
232 }
233 
234 void
235 elf64_dump_thread(struct thread *td, void *dst, size_t *off)
236 {
237 	size_t len;
238 	struct pcb *pcb;
239 	uint64_t vshr[32];
240 	uint64_t *vsr_dw1;
241 	int vsr_idx;
242 
243 	len = 0;
244 	pcb = td->td_pcb;
245 
246 	if (pcb->pcb_flags & PCB_VEC) {
247 		save_vec_nodrop(td);
248 		if (dst != NULL) {
249 			len += elf64_populate_note(NT_PPC_VMX,
250 			    &pcb->pcb_vec, (char *)dst + len,
251 			    sizeof(pcb->pcb_vec), NULL);
252 		} else
253 			len += elf64_populate_note(NT_PPC_VMX, NULL, NULL,
254 			    sizeof(pcb->pcb_vec), NULL);
255 	}
256 
257 	if (pcb->pcb_flags & PCB_VSX) {
258 		save_fpu_nodrop(td);
259 		if (dst != NULL) {
260 			/*
261 			 * Doubleword 0 of VSR0-VSR31 overlap with FPR0-FPR31 and
262 			 * VSR32-VSR63 overlap with VR0-VR31, so we only copy
263 			 * the non-overlapping data, which is doubleword 1 of VSR0-VSR31.
264 			 */
265 			for (vsr_idx = 0; vsr_idx < nitems(vshr); vsr_idx++) {
266 				vsr_dw1 = (uint64_t *)&pcb->pcb_fpu.fpr[vsr_idx].vsr[2];
267 				vshr[vsr_idx] = *vsr_dw1;
268 			}
269 			len += elf64_populate_note(NT_PPC_VSX,
270 			    vshr, (char *)dst + len,
271 			    sizeof(vshr), NULL);
272 		} else
273 			len += elf64_populate_note(NT_PPC_VSX, NULL, NULL,
274 			    sizeof(vshr), NULL);
275 	}
276 
277 	*off = len;
278 }
279 
280 bool
281 elf_is_ifunc_reloc(Elf_Size r_info __unused)
282 {
283 
284 	return (false);
285 }
286 
287 /* Process one elf relocation with addend. */
288 static int
289 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
290     int type, int local, elf_lookup_fn lookup)
291 {
292 	Elf_Addr *where;
293 	Elf_Addr addr;
294 	Elf_Addr addend;
295 	Elf_Word rtype, symidx;
296 	const Elf_Rela *rela;
297 	int error;
298 
299 	switch (type) {
300 	case ELF_RELOC_REL:
301 		panic("PPC only supports RELA relocations");
302 		break;
303 	case ELF_RELOC_RELA:
304 		rela = (const Elf_Rela *)data;
305 		where = (Elf_Addr *) (relocbase + rela->r_offset);
306 		addend = rela->r_addend;
307 		rtype = ELF_R_TYPE(rela->r_info);
308 		symidx = ELF_R_SYM(rela->r_info);
309 		break;
310 	default:
311 		panic("elf_reloc: unknown relocation mode %d\n", type);
312 	}
313 
314 	switch (rtype) {
315 
316 	case R_PPC_NONE:
317 		break;
318 
319 	case R_PPC64_ADDR64:	/* doubleword64 S + A */
320 		error = lookup(lf, symidx, 1, &addr);
321 		if (error != 0)
322 			return -1;
323 		addr += addend;
324 		*where = addr;
325 		break;
326 
327 	case R_PPC_RELATIVE:	/* doubleword64 B + A */
328 		*where = elf_relocaddr(lf, relocbase + addend);
329 		break;
330 
331 	case R_PPC_JMP_SLOT:	/* function descriptor copy */
332 		lookup(lf, symidx, 1, &addr);
333 #if !defined(_CALL_ELF) || _CALL_ELF == 1
334 		memcpy(where, (Elf_Addr *)addr, 3*sizeof(Elf_Addr));
335 #else
336 		*where = addr;
337 #endif
338 		__asm __volatile("dcbst 0,%0; sync" :: "r"(where) : "memory");
339 		break;
340 
341 	default:
342 		printf("kldload: unexpected relocation type %d\n",
343 		    (int) rtype);
344 		return -1;
345 	}
346 	return(0);
347 }
348 
349 void
350 elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase)
351 {
352 	Elf_Rela *rela = NULL, *relalim;
353 	Elf_Addr relasz = 0;
354 	Elf_Addr *where;
355 
356 	/*
357 	 * Extract the rela/relasz values from the dynamic section
358 	 */
359 	for (; dynp->d_tag != DT_NULL; dynp++) {
360 		switch (dynp->d_tag) {
361 		case DT_RELA:
362 			rela = (Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
363 			break;
364 		case DT_RELASZ:
365 			relasz = dynp->d_un.d_val;
366 			break;
367 		}
368 	}
369 
370 	/*
371 	 * Relocate these values
372 	 */
373 	relalim = (Elf_Rela *)((caddr_t)rela + relasz);
374 	for (; rela < relalim; rela++) {
375 		if (ELF_R_TYPE(rela->r_info) != R_PPC_RELATIVE)
376 			continue;
377 		where = (Elf_Addr *)(relocbase + rela->r_offset);
378 		*where = (Elf_Addr)(relocbase + rela->r_addend);
379 	}
380 }
381 
382 int
383 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
384     elf_lookup_fn lookup)
385 {
386 
387 	return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
388 }
389 
390 int
391 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
392     int type, elf_lookup_fn lookup)
393 {
394 
395 	return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
396 }
397 
398 int
399 elf_cpu_load_file(linker_file_t lf)
400 {
401 	/* Only sync the cache for non-kernel modules */
402 	if (lf->id != 1)
403 		__syncicache(lf->address, lf->size);
404 	return (0);
405 }
406 
407 int
408 elf_cpu_unload_file(linker_file_t lf __unused)
409 {
410 
411 	return (0);
412 }
413