xref: /freebsd/sys/powerpc/powerpc/elf64_machdep.c (revision 4f52dfbb)
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/elf.h>
52 #include <machine/md_var.h>
53 
54 static void exec_setregs_funcdesc(struct thread *td, struct image_params *imgp,
55     u_long stack);
56 
57 struct sysentvec elf64_freebsd_sysvec_v1 = {
58 	.sv_size	= SYS_MAXSYSCALL,
59 	.sv_table	= sysent,
60 	.sv_mask	= 0,
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,
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_mask	= 0,
100 	.sv_errsize	= 0,
101 	.sv_errtbl	= NULL,
102 	.sv_transtrap	= NULL,
103 	.sv_fixup	= __elfN(freebsd_fixup),
104 	.sv_sendsig	= sendsig,
105 	.sv_sigcode	= sigcode64_elfv2,
106 	.sv_szsigcode	= &szsigcode64_elfv2,
107 	.sv_name	= "FreeBSD ELF64 V2",
108 	.sv_coredump	= __elfN(coredump),
109 	.sv_imgact_try	= NULL,
110 	.sv_minsigstksz	= MINSIGSTKSZ,
111 	.sv_pagesize	= PAGE_SIZE,
112 	.sv_minuser	= VM_MIN_ADDRESS,
113 	.sv_maxuser	= VM_MAXUSER_ADDRESS,
114 	.sv_usrstack	= USRSTACK,
115 	.sv_psstrings	= PS_STRINGS,
116 	.sv_stackprot	= VM_PROT_ALL,
117 	.sv_copyout_strings = exec_copyout_strings,
118 	.sv_setregs	= exec_setregs,
119 	.sv_fixlimit	= NULL,
120 	.sv_maxssiz	= NULL,
121 	.sv_flags	= SV_ABI_FREEBSD | SV_LP64 | SV_SHP,
122 	.sv_set_syscall_retval = cpu_set_syscall_retval,
123 	.sv_fetch_syscall_args = cpu_fetch_syscall_args,
124 	.sv_syscallnames = syscallnames,
125 	.sv_shared_page_base = SHAREDPAGE,
126 	.sv_shared_page_len = PAGE_SIZE,
127 	.sv_schedtail	= NULL,
128 	.sv_thread_detach = NULL,
129 };
130 INIT_SYSENTVEC(elf64_sysvec_v2, &elf64_freebsd_sysvec_v2);
131 
132 static boolean_t ppc64_elfv1_header_match(struct image_params *params);
133 static boolean_t ppc64_elfv2_header_match(struct image_params *params);
134 
135 static Elf64_Brandinfo freebsd_brand_info_elfv1 = {
136 	.brand		= ELFOSABI_FREEBSD,
137 	.machine	= EM_PPC64,
138 	.compat_3_brand	= "FreeBSD",
139 	.emul_path	= NULL,
140 	.interp_path	= "/libexec/ld-elf.so.1",
141 	.sysvec		= &elf64_freebsd_sysvec_v1,
142 	.interp_newpath	= NULL,
143 	.brand_note	= &elf64_freebsd_brandnote,
144 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
145 	.header_supported = &ppc64_elfv1_header_match
146 };
147 
148 SYSINIT(elf64v1, SI_SUB_EXEC, SI_ORDER_ANY,
149     (sysinit_cfunc_t) elf64_insert_brand_entry,
150     &freebsd_brand_info_elfv1);
151 
152 static Elf64_Brandinfo freebsd_brand_info_elfv2 = {
153 	.brand		= ELFOSABI_FREEBSD,
154 	.machine	= EM_PPC64,
155 	.compat_3_brand	= "FreeBSD",
156 	.emul_path	= NULL,
157 	.interp_path	= "/libexec/ld-elf.so.1",
158 	.sysvec		= &elf64_freebsd_sysvec_v2,
159 	.interp_newpath	= NULL,
160 	.brand_note	= &elf64_freebsd_brandnote,
161 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
162 	.header_supported = &ppc64_elfv2_header_match
163 };
164 
165 SYSINIT(elf64v2, SI_SUB_EXEC, SI_ORDER_ANY,
166     (sysinit_cfunc_t) elf64_insert_brand_entry,
167     &freebsd_brand_info_elfv2);
168 
169 static Elf64_Brandinfo freebsd_brand_oinfo = {
170 	.brand		= ELFOSABI_FREEBSD,
171 	.machine	= EM_PPC64,
172 	.compat_3_brand	= "FreeBSD",
173 	.emul_path	= NULL,
174 	.interp_path	= "/usr/libexec/ld-elf.so.1",
175 	.sysvec		= &elf64_freebsd_sysvec_v1,
176 	.interp_newpath	= NULL,
177 	.brand_note	= &elf64_freebsd_brandnote,
178 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
179 	.header_supported = &ppc64_elfv1_header_match
180 };
181 
182 SYSINIT(oelf64, SI_SUB_EXEC, SI_ORDER_ANY,
183 	(sysinit_cfunc_t) elf64_insert_brand_entry,
184 	&freebsd_brand_oinfo);
185 
186 void elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase);
187 
188 static boolean_t
189 ppc64_elfv1_header_match(struct image_params *params)
190 {
191 	const Elf64_Ehdr *hdr = (const Elf64_Ehdr *)params->image_header;
192 	int abi = (hdr->e_flags & 3);
193 
194 	return (abi == 0 || abi == 1);
195 }
196 
197 static boolean_t
198 ppc64_elfv2_header_match(struct image_params *params)
199 {
200 	const Elf64_Ehdr *hdr = (const Elf64_Ehdr *)params->image_header;
201 	int abi = (hdr->e_flags & 3);
202 
203 	return (abi == 2);
204 }
205 
206 static void
207 exec_setregs_funcdesc(struct thread *td, struct image_params *imgp,
208     u_long stack)
209 {
210 	struct trapframe *tf;
211 	register_t entry_desc[3];
212 
213 	tf = trapframe(td);
214 	exec_setregs(td, imgp, stack);
215 
216 	/*
217 	 * For 64-bit ELFv1, we need to disentangle the function
218 	 * descriptor
219 	 *
220 	 * 0. entry point
221 	 * 1. TOC value (r2)
222 	 * 2. Environment pointer (r11)
223 	 */
224 
225 	(void)copyin((void *)imgp->entry_addr, entry_desc,
226 	    sizeof(entry_desc));
227 	tf->srr0 = entry_desc[0] + imgp->reloc_base;
228 	tf->fixreg[2] = entry_desc[1] + imgp->reloc_base;
229 	tf->fixreg[11] = entry_desc[2] + imgp->reloc_base;
230 }
231 
232 void
233 elf64_dump_thread(struct thread *td, void *dst, size_t *off)
234 {
235 	size_t len;
236 	struct pcb *pcb;
237 
238 	len = 0;
239 	pcb = td->td_pcb;
240 	if (pcb->pcb_flags & PCB_VEC) {
241 		save_vec_nodrop(td);
242 		if (dst != NULL) {
243 			len += elf64_populate_note(NT_PPC_VMX,
244 			    &pcb->pcb_vec, dst,
245 			    sizeof(pcb->pcb_vec), NULL);
246 		} else
247 			len += elf64_populate_note(NT_PPC_VMX, NULL, NULL,
248 			    sizeof(pcb->pcb_vec), NULL);
249 	}
250 	*off = len;
251 }
252 
253 
254 /* Process one elf relocation with addend. */
255 static int
256 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
257     int type, int local, elf_lookup_fn lookup)
258 {
259 	Elf_Addr *where;
260 	Elf_Addr addr;
261 	Elf_Addr addend;
262 	Elf_Word rtype, symidx;
263 	const Elf_Rela *rela;
264 	int error;
265 
266 	switch (type) {
267 	case ELF_RELOC_REL:
268 		panic("PPC only supports RELA relocations");
269 		break;
270 	case ELF_RELOC_RELA:
271 		rela = (const Elf_Rela *)data;
272 		where = (Elf_Addr *) (relocbase + rela->r_offset);
273 		addend = rela->r_addend;
274 		rtype = ELF_R_TYPE(rela->r_info);
275 		symidx = ELF_R_SYM(rela->r_info);
276 		break;
277 	default:
278 		panic("elf_reloc: unknown relocation mode %d\n", type);
279 	}
280 
281 	switch (rtype) {
282 
283 	case R_PPC_NONE:
284 		break;
285 
286 	case R_PPC64_ADDR64:	/* doubleword64 S + A */
287 		error = lookup(lf, symidx, 1, &addr);
288 		if (error != 0)
289 			return -1;
290 		addr += addend;
291 		*where = addr;
292 		break;
293 
294 	case R_PPC_RELATIVE:	/* doubleword64 B + A */
295 		*where = elf_relocaddr(lf, relocbase + addend);
296 		break;
297 
298 	case R_PPC_JMP_SLOT:	/* function descriptor copy */
299 		lookup(lf, symidx, 1, &addr);
300 #if !defined(_CALL_ELF) || _CALL_ELF == 1
301 		memcpy(where, (Elf_Addr *)addr, 3*sizeof(Elf_Addr));
302 #else
303 		memcpy(where, (Elf_Addr *)addr, sizeof(Elf_Addr));
304 #endif
305 		__asm __volatile("dcbst 0,%0; sync" :: "r"(where) : "memory");
306 		break;
307 
308 	default:
309 		printf("kldload: unexpected relocation type %d\n",
310 		    (int) rtype);
311 		return -1;
312 	}
313 	return(0);
314 }
315 
316 void
317 elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase)
318 {
319 	Elf_Rela *rela = NULL, *relalim;
320 	Elf_Addr relasz = 0;
321 	Elf_Addr *where;
322 
323 	/*
324 	 * Extract the rela/relasz values from the dynamic section
325 	 */
326 	for (; dynp->d_tag != DT_NULL; dynp++) {
327 		switch (dynp->d_tag) {
328 		case DT_RELA:
329 			rela = (Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
330 			break;
331 		case DT_RELASZ:
332 			relasz = dynp->d_un.d_val;
333 			break;
334 		}
335 	}
336 
337 	/*
338 	 * Relocate these values
339 	 */
340 	relalim = (Elf_Rela *)((caddr_t)rela + relasz);
341 	for (; rela < relalim; rela++) {
342 		if (ELF_R_TYPE(rela->r_info) != R_PPC_RELATIVE)
343 			continue;
344 		where = (Elf_Addr *)(relocbase + rela->r_offset);
345 		*where = (Elf_Addr)(relocbase + rela->r_addend);
346 	}
347 }
348 
349 int
350 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
351     elf_lookup_fn lookup)
352 {
353 
354 	return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
355 }
356 
357 int
358 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
359     int type, elf_lookup_fn lookup)
360 {
361 
362 	return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
363 }
364 
365 int
366 elf_cpu_load_file(linker_file_t lf)
367 {
368 	/* Only sync the cache for non-kernel modules */
369 	if (lf->id != 1)
370 		__syncicache(lf->address, lf->size);
371 	return (0);
372 }
373 
374 int
375 elf_cpu_unload_file(linker_file_t lf __unused)
376 {
377 
378 	return (0);
379 }
380