xref: /freebsd/sys/powerpc/powerpc/elf32_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 
34 #define __ELF_WORD_SIZE 32
35 
36 #include <sys/exec.h>
37 #include <sys/imgact.h>
38 #include <sys/malloc.h>
39 #include <sys/proc.h>
40 #include <sys/namei.h>
41 #include <sys/fcntl.h>
42 #include <sys/sysent.h>
43 #include <sys/imgact_elf.h>
44 #include <sys/jail.h>
45 #include <sys/reg.h>
46 #include <sys/smp.h>
47 #include <sys/syscall.h>
48 #include <sys/sysctl.h>
49 #include <sys/signalvar.h>
50 #include <sys/vnode.h>
51 #include <sys/linker.h>
52 
53 #include <vm/vm.h>
54 #include <vm/vm_param.h>
55 
56 #include <machine/altivec.h>
57 #include <machine/cpu.h>
58 #include <machine/fpu.h>
59 #include <machine/elf.h>
60 #include <machine/md_var.h>
61 
62 #include <powerpc/powerpc/elf_common.c>
63 
64 #ifdef __powerpc64__
65 #include <compat/freebsd32/freebsd32_proto.h>
66 #include <compat/freebsd32/freebsd32_util.h>
67 
68 extern const char *freebsd32_syscallnames[];
69 static void ppc32_fixlimit(struct rlimit *rl, int which);
70 
71 static SYSCTL_NODE(_compat, OID_AUTO, ppc32, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
72     "32-bit mode");
73 
74 #define PPC32_MAXDSIZ (1024*1024*1024)
75 static u_long ppc32_maxdsiz = PPC32_MAXDSIZ;
76 SYSCTL_ULONG(_compat_ppc32, OID_AUTO, maxdsiz, CTLFLAG_RWTUN, &ppc32_maxdsiz,
77              0, "");
78 #define PPC32_MAXSSIZ (64*1024*1024)
79 u_long ppc32_maxssiz = PPC32_MAXSSIZ;
80 SYSCTL_ULONG(_compat_ppc32, OID_AUTO, maxssiz, CTLFLAG_RWTUN, &ppc32_maxssiz,
81              0, "");
82 #else
83 static void ppc32_runtime_resolve(void);
84 #endif
85 
86 struct sysentvec elf32_freebsd_sysvec = {
87 	.sv_size	= SYS_MAXSYSCALL,
88 #ifdef __powerpc64__
89 	.sv_table	= freebsd32_sysent,
90 #else
91 	.sv_table	= sysent,
92 #endif
93 	.sv_fixup	= __elfN(freebsd_fixup),
94 	.sv_copyout_auxargs = __elfN(powerpc_copyout_auxargs),
95 	.sv_sendsig	= sendsig,
96 	.sv_sigcode	= sigcode32,
97 	.sv_szsigcode	= &szsigcode32,
98 	.sv_name	= "FreeBSD ELF32",
99 	.sv_coredump	= __elfN(coredump),
100 	.sv_elf_core_osabi = ELFOSABI_FREEBSD,
101 	.sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR,
102 	.sv_elf_core_prepare_notes = __elfN(prepare_notes),
103 	.sv_imgact_try	= NULL,
104 	.sv_minsigstksz	= MINSIGSTKSZ,
105 	.sv_minuser	= VM_MIN_ADDRESS,
106 	.sv_stackprot	= VM_PROT_ALL,
107 #ifdef __powerpc64__
108 	.sv_maxuser	= VM_MAXUSER_ADDRESS32,
109 	.sv_usrstack	= FREEBSD32_USRSTACK,
110 	.sv_psstrings	= FREEBSD32_PS_STRINGS,
111 	.sv_psstringssz	= sizeof(struct freebsd32_ps_strings),
112 	.sv_copyout_strings = freebsd32_copyout_strings,
113 	.sv_setregs	= ppc32_setregs,
114 	.sv_syscallnames = freebsd32_syscallnames,
115 	.sv_fixlimit	= ppc32_fixlimit,
116 #else
117 	.sv_maxuser	= VM_MAXUSER_ADDRESS,
118 	.sv_usrstack	= USRSTACK,
119 	.sv_psstrings	= PS_STRINGS,
120 	.sv_psstringssz	= sizeof(struct ps_strings),
121 	.sv_copyout_strings = exec_copyout_strings,
122 	.sv_setregs	= exec_setregs,
123 	.sv_syscallnames = syscallnames,
124 	.sv_fixlimit	= NULL,
125 #endif
126 	.sv_maxssiz	= NULL,
127 	.sv_flags	= SV_ABI_FREEBSD | SV_ILP32 | SV_SHP | SV_ASLR |
128 			    SV_TIMEKEEP | SV_RNG_SEED_VER,
129 	.sv_set_syscall_retval = cpu_set_syscall_retval,
130 	.sv_fetch_syscall_args = cpu_fetch_syscall_args,
131 	.sv_shared_page_base = FREEBSD32_SHAREDPAGE,
132 	.sv_shared_page_len = PAGE_SIZE,
133 	.sv_schedtail	= NULL,
134 	.sv_thread_detach = NULL,
135 	.sv_trap	= NULL,
136 	.sv_hwcap	= &cpu_features,
137 	.sv_hwcap2	= &cpu_features2,
138 	.sv_onexec_old	= exec_onexec_old,
139 	.sv_onexit	= exit_onexit,
140 	.sv_regset_begin = SET_BEGIN(__elfN(regset)),
141 	.sv_regset_end  = SET_LIMIT(__elfN(regset)),
142 };
143 INIT_SYSENTVEC(elf32_sysvec, &elf32_freebsd_sysvec);
144 
145 static Elf32_Brandinfo freebsd_brand_info = {
146 	.brand		= ELFOSABI_FREEBSD,
147 	.machine	= EM_PPC,
148 	.compat_3_brand	= "FreeBSD",
149 	.emul_path	= NULL,
150 	.interp_path	= "/libexec/ld-elf.so.1",
151 	.sysvec		= &elf32_freebsd_sysvec,
152 #ifdef __powerpc64__
153 	.interp_newpath	= "/libexec/ld-elf32.so.1",
154 #else
155 	.interp_newpath	= NULL,
156 #endif
157 	.brand_note	= &elf32_freebsd_brandnote,
158 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
159 };
160 
161 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST,
162     (sysinit_cfunc_t) elf32_insert_brand_entry,
163     &freebsd_brand_info);
164 
165 static Elf32_Brandinfo freebsd_brand_oinfo = {
166 	.brand		= ELFOSABI_FREEBSD,
167 	.machine	= EM_PPC,
168 	.compat_3_brand	= "FreeBSD",
169 	.emul_path	= NULL,
170 	.interp_path	= "/usr/libexec/ld-elf.so.1",
171 	.sysvec		= &elf32_freebsd_sysvec,
172 	.interp_newpath	= NULL,
173 	.brand_note	= &elf32_freebsd_brandnote,
174 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
175 };
176 
177 SYSINIT(oelf32, SI_SUB_EXEC, SI_ORDER_ANY,
178 	(sysinit_cfunc_t) elf32_insert_brand_entry,
179 	&freebsd_brand_oinfo);
180 
181 void elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase);
182 
183 void
184 elf32_dump_thread(struct thread *td, void *dst, size_t *off)
185 {
186 	size_t len;
187 	struct pcb *pcb;
188 	uint64_t vshr[32];
189 	uint64_t *vsr_dw1;
190 	int vsr_idx;
191 
192 	len = 0;
193 	pcb = td->td_pcb;
194 
195 	if (pcb->pcb_flags & PCB_VEC) {
196 		save_vec_nodrop(td);
197 		if (dst != NULL) {
198 			len += elf32_populate_note(NT_PPC_VMX,
199 			    &pcb->pcb_vec, (char *)dst + len,
200 			    sizeof(pcb->pcb_vec), NULL);
201 		} else
202 			len += elf32_populate_note(NT_PPC_VMX, NULL, NULL,
203 			    sizeof(pcb->pcb_vec), NULL);
204 	}
205 
206 	if (pcb->pcb_flags & PCB_VSX) {
207 		save_fpu_nodrop(td);
208 		if (dst != NULL) {
209 			/*
210 			 * Doubleword 0 of VSR0-VSR31 overlap with FPR0-FPR31 and
211 			 * VSR32-VSR63 overlap with VR0-VR31, so we only copy
212 			 * the non-overlapping data, which is doubleword 1 of VSR0-VSR31.
213 			 */
214 			for (vsr_idx = 0; vsr_idx < nitems(vshr); vsr_idx++) {
215 				vsr_dw1 = (uint64_t *)&pcb->pcb_fpu.fpr[vsr_idx].vsr[2];
216 				vshr[vsr_idx] = *vsr_dw1;
217 			}
218 			len += elf32_populate_note(NT_PPC_VSX,
219 			    vshr, (char *)dst + len,
220 			    sizeof(vshr), NULL);
221 		} else
222 			len += elf32_populate_note(NT_PPC_VSX, NULL, NULL,
223 			    sizeof(vshr), NULL);
224 	}
225 
226 	*off = len;
227 }
228 
229 #ifndef __powerpc64__
230 bool
231 elf_is_ifunc_reloc(Elf_Size r_info)
232 {
233 
234 	return (ELF_R_TYPE(r_info) == R_PPC_IRELATIVE);
235 }
236 
237 /* Process one elf relocation with addend. */
238 static int
239 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
240     int type, int local, elf_lookup_fn lookup)
241 {
242 	Elf_Addr *where;
243 	Elf_Half *hwhere;
244 	Elf_Addr addr;
245 	Elf_Addr addend, val;
246 	Elf_Word rtype, symidx;
247 	const Elf_Rela *rela;
248 	int error;
249 
250 	switch (type) {
251 	case ELF_RELOC_REL:
252 		panic("PPC only supports RELA relocations");
253 		break;
254 	case ELF_RELOC_RELA:
255 		rela = (const Elf_Rela *)data;
256 		where = (Elf_Addr *) ((uintptr_t)relocbase + rela->r_offset);
257 		hwhere = (Elf_Half *) ((uintptr_t)relocbase + rela->r_offset);
258 		addend = rela->r_addend;
259 		rtype = ELF_R_TYPE(rela->r_info);
260 		symidx = ELF_R_SYM(rela->r_info);
261 		break;
262 	default:
263 		panic("elf_reloc: unknown relocation mode %d\n", type);
264 	}
265 
266 	switch (rtype) {
267 	case R_PPC_NONE:
268 		break;
269 
270 	case R_PPC_ADDR32: /* word32 S + A */
271 		error = lookup(lf, symidx, 1, &addr);
272 		if (error != 0)
273 			return (-1);
274 		*where = elf_relocaddr(lf, addr + addend);
275 			break;
276 
277 	case R_PPC_ADDR16_LO: /* #lo(S) */
278 		error = lookup(lf, symidx, 1, &addr);
279 		if (error != 0)
280 			return (-1);
281 		/*
282 		 * addend values are sometimes relative to sections
283 		 * (i.e. .rodata) in rela, where in reality they
284 		 * are relative to relocbase. Detect this condition.
285 		 */
286 		if (addr > relocbase && addr <= (relocbase + addend))
287 			addr = relocbase;
288 		addr = elf_relocaddr(lf, addr + addend);
289 		*hwhere = addr & 0xffff;
290 		break;
291 
292 	case R_PPC_ADDR16_HA: /* #ha(S) */
293 		error = lookup(lf, symidx, 1, &addr);
294 		if (error != 0)
295 			return (-1);
296 		/*
297 		 * addend values are sometimes relative to sections
298 		 * (i.e. .rodata) in rela, where in reality they
299 		 * are relative to relocbase. Detect this condition.
300 		 */
301 		if (addr > relocbase && addr <= (relocbase + addend))
302 			addr = relocbase;
303 		addr = elf_relocaddr(lf, addr + addend);
304 		*hwhere = ((addr >> 16) + ((addr & 0x8000) ? 1 : 0))
305 		    & 0xffff;
306 		break;
307 
308 	case R_PPC_RELATIVE: /* word32 B + A */
309 		*where = elf_relocaddr(lf, relocbase + addend);
310 		break;
311 
312 	case R_PPC_JMP_SLOT: /* PLT jump slot entry */
313 		/*
314 		 * We currently only support Secure-PLT jump slots.
315 		 * Given that we reject BSS-PLT modules during load, we
316 		 * don't need to check again.
317 		 * The method we are using here is equivilent to
318 		 * LD_BIND_NOW.
319 		 */
320 		error = lookup(lf, symidx, 1, &addr);
321 		if (error != 0)
322 			return (-1);
323 		*where = elf_relocaddr(lf, addr + addend);
324 		break;
325 
326 	case R_PPC_IRELATIVE:
327 		addr = relocbase + addend;
328 		val = ((Elf32_Addr (*)(void))addr)();
329 		if (*where != val)
330 			*where = val;
331 		break;
332 
333 	default:
334 		printf("kldload: unexpected relocation type %d, "
335 		    "symbol index %d\n", (int)rtype, symidx);
336 		return (-1);
337 	}
338 	return (0);
339 }
340 
341 void
342 elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase)
343 {
344 	Elf_Rela *rela = NULL, *relalim;
345 	Elf_Addr relasz = 0;
346 	Elf_Addr *where;
347 
348 	/*
349 	 * Extract the rela/relasz values from the dynamic section
350 	 */
351 	for (; dynp->d_tag != DT_NULL; dynp++) {
352 		switch (dynp->d_tag) {
353 		case DT_RELA:
354 			rela = (Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
355 			break;
356 		case DT_RELASZ:
357 			relasz = dynp->d_un.d_val;
358 			break;
359 		}
360 	}
361 
362 	/*
363 	 * Relocate these values
364 	 */
365 	relalim = (Elf_Rela *)((caddr_t)rela + relasz);
366 	for (; rela < relalim; rela++) {
367 		if (ELF_R_TYPE(rela->r_info) != R_PPC_RELATIVE)
368 			continue;
369 		where = (Elf_Addr *)(relocbase + rela->r_offset);
370 		*where = (Elf_Addr)(relocbase + rela->r_addend);
371 	}
372 }
373 
374 int
375 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
376     elf_lookup_fn lookup)
377 {
378 
379 	return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
380 }
381 
382 int
383 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
384     int type, elf_lookup_fn lookup)
385 {
386 
387 	return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
388 }
389 
390 int
391 elf_cpu_load_file(linker_file_t lf)
392 {
393 
394 	/* Only sync the cache for non-kernel modules */
395 	if (lf->id != 1)
396 		__syncicache(lf->address, lf->size);
397 	return (0);
398 }
399 
400 int
401 elf_cpu_unload_file(linker_file_t lf __unused)
402 {
403 
404 	return (0);
405 }
406 
407 static void
408 ppc32_runtime_resolve()
409 {
410 
411 	/*
412 	 * Since we don't support lazy binding, panic immediately if anyone
413 	 * manages to call the runtime resolver.
414 	 */
415 	panic("kldload: Runtime resolver was called unexpectedly!");
416 }
417 
418 int
419 elf_cpu_parse_dynamic(caddr_t loadbase, Elf_Dyn *dynamic)
420 {
421 	Elf_Dyn *dp;
422 	bool has_plt = false;
423 	bool secure_plt = false;
424 	Elf_Addr *got;
425 
426 	for (dp = dynamic; dp->d_tag != DT_NULL; dp++) {
427 		switch (dp->d_tag) {
428 		case DT_PPC_GOT:
429 			secure_plt = true;
430 			got = (Elf_Addr *)(loadbase + dp->d_un.d_ptr);
431 			/* Install runtime resolver canary. */
432 			got[1] = (Elf_Addr)ppc32_runtime_resolve;
433 			got[2] = (Elf_Addr)0;
434 			break;
435 		case DT_PLTGOT:
436 			has_plt = true;
437 			break;
438 		}
439 	}
440 
441 	if (has_plt && !secure_plt) {
442 		printf("kldload: BSS-PLT modules are not supported.\n");
443 		return (-1);
444 	}
445 	return (0);
446 }
447 #endif
448 
449 #ifdef __powerpc64__
450 static void
451 ppc32_fixlimit(struct rlimit *rl, int which)
452 {
453 	switch (which) {
454 	case RLIMIT_DATA:
455 		if (ppc32_maxdsiz != 0) {
456 			if (rl->rlim_cur > ppc32_maxdsiz)
457 				rl->rlim_cur = ppc32_maxdsiz;
458 			if (rl->rlim_max > ppc32_maxdsiz)
459 				rl->rlim_max = ppc32_maxdsiz;
460 		}
461 		break;
462 	case RLIMIT_STACK:
463 		if (ppc32_maxssiz != 0) {
464 			if (rl->rlim_cur > ppc32_maxssiz)
465 				rl->rlim_cur = ppc32_maxssiz;
466 			if (rl->rlim_max > ppc32_maxssiz)
467 				rl->rlim_max = ppc32_maxssiz;
468 		}
469 		break;
470 	}
471 }
472 #endif
473