xref: /freebsd/sys/powerpc/powerpc/elf32_machdep.c (revision 206b73d0)
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/syscall.h>
45 #include <sys/sysctl.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/reg.h>
58 #include <machine/md_var.h>
59 
60 #ifdef __powerpc64__
61 #include <compat/freebsd32/freebsd32_proto.h>
62 #include <compat/freebsd32/freebsd32_util.h>
63 
64 extern const char *freebsd32_syscallnames[];
65 static void ppc32_fixlimit(struct rlimit *rl, int which);
66 
67 static SYSCTL_NODE(_compat, OID_AUTO, ppc32, CTLFLAG_RW, 0, "32-bit mode");
68 
69 #define PPC32_MAXDSIZ (1024*1024*1024)
70 static u_long ppc32_maxdsiz = PPC32_MAXDSIZ;
71 SYSCTL_ULONG(_compat_ppc32, OID_AUTO, maxdsiz, CTLFLAG_RWTUN, &ppc32_maxdsiz,
72              0, "");
73 #define PPC32_MAXSSIZ (64*1024*1024)
74 u_long ppc32_maxssiz = PPC32_MAXSSIZ;
75 SYSCTL_ULONG(_compat_ppc32, OID_AUTO, maxssiz, CTLFLAG_RWTUN, &ppc32_maxssiz,
76              0, "");
77 #endif
78 
79 struct sysentvec elf32_freebsd_sysvec = {
80 	.sv_size	= SYS_MAXSYSCALL,
81 #ifdef __powerpc64__
82 	.sv_table	= freebsd32_sysent,
83 #else
84 	.sv_table	= sysent,
85 #endif
86 	.sv_errsize	= 0,
87 	.sv_errtbl	= NULL,
88 	.sv_transtrap	= NULL,
89 	.sv_fixup	= __elfN(freebsd_fixup),
90 	.sv_sendsig	= sendsig,
91 	.sv_sigcode	= sigcode32,
92 	.sv_szsigcode	= &szsigcode32,
93 	.sv_name	= "FreeBSD ELF32",
94 	.sv_coredump	= __elfN(coredump),
95 	.sv_imgact_try	= NULL,
96 	.sv_minsigstksz	= MINSIGSTKSZ,
97 	.sv_minuser	= VM_MIN_ADDRESS,
98 	.sv_stackprot	= VM_PROT_ALL,
99 #ifdef __powerpc64__
100 	.sv_maxuser	= VM_MAXUSER_ADDRESS32,
101 	.sv_usrstack	= FREEBSD32_USRSTACK,
102 	.sv_psstrings	= FREEBSD32_PS_STRINGS,
103 	.sv_copyout_strings = freebsd32_copyout_strings,
104 	.sv_setregs	= ppc32_setregs,
105 	.sv_syscallnames = freebsd32_syscallnames,
106 	.sv_fixlimit	= ppc32_fixlimit,
107 #else
108 	.sv_maxuser	= VM_MAXUSER_ADDRESS,
109 	.sv_usrstack	= USRSTACK,
110 	.sv_psstrings	= PS_STRINGS,
111 	.sv_copyout_strings = exec_copyout_strings,
112 	.sv_setregs	= exec_setregs,
113 	.sv_syscallnames = syscallnames,
114 	.sv_fixlimit	= NULL,
115 #endif
116 	.sv_maxssiz	= NULL,
117 	.sv_flags	= SV_ABI_FREEBSD | SV_ILP32 | SV_SHP | SV_ASLR,
118 	.sv_set_syscall_retval = cpu_set_syscall_retval,
119 	.sv_fetch_syscall_args = cpu_fetch_syscall_args,
120 	.sv_shared_page_base = FREEBSD32_SHAREDPAGE,
121 	.sv_shared_page_len = PAGE_SIZE,
122 	.sv_schedtail	= NULL,
123 	.sv_thread_detach = NULL,
124 	.sv_trap	= NULL,
125 	.sv_hwcap	= &cpu_features,
126 	.sv_hwcap2	= &cpu_features2,
127 };
128 INIT_SYSENTVEC(elf32_sysvec, &elf32_freebsd_sysvec);
129 
130 static Elf32_Brandinfo freebsd_brand_info = {
131 	.brand		= ELFOSABI_FREEBSD,
132 	.machine	= EM_PPC,
133 	.compat_3_brand	= "FreeBSD",
134 	.emul_path	= NULL,
135 	.interp_path	= "/libexec/ld-elf.so.1",
136 	.sysvec		= &elf32_freebsd_sysvec,
137 #ifdef __powerpc64__
138 	.interp_newpath	= "/libexec/ld-elf32.so.1",
139 #else
140 	.interp_newpath	= NULL,
141 #endif
142 	.brand_note	= &elf32_freebsd_brandnote,
143 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
144 };
145 
146 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST,
147     (sysinit_cfunc_t) elf32_insert_brand_entry,
148     &freebsd_brand_info);
149 
150 static Elf32_Brandinfo freebsd_brand_oinfo = {
151 	.brand		= ELFOSABI_FREEBSD,
152 	.machine	= EM_PPC,
153 	.compat_3_brand	= "FreeBSD",
154 	.emul_path	= NULL,
155 	.interp_path	= "/usr/libexec/ld-elf.so.1",
156 	.sysvec		= &elf32_freebsd_sysvec,
157 	.interp_newpath	= NULL,
158 	.brand_note	= &elf32_freebsd_brandnote,
159 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
160 };
161 
162 SYSINIT(oelf32, SI_SUB_EXEC, SI_ORDER_ANY,
163 	(sysinit_cfunc_t) elf32_insert_brand_entry,
164 	&freebsd_brand_oinfo);
165 
166 void elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase);
167 
168 void
169 elf32_dump_thread(struct thread *td, void *dst, size_t *off)
170 {
171 	size_t len;
172 	struct pcb *pcb;
173 	uint64_t vshr[32];
174 	uint64_t *vsr_dw1;
175 	int vsr_idx;
176 
177 	len = 0;
178 	pcb = td->td_pcb;
179 
180 	if (pcb->pcb_flags & PCB_VEC) {
181 		save_vec_nodrop(td);
182 		if (dst != NULL) {
183 			len += elf32_populate_note(NT_PPC_VMX,
184 			    &pcb->pcb_vec, (char *)dst + len,
185 			    sizeof(pcb->pcb_vec), NULL);
186 		} else
187 			len += elf32_populate_note(NT_PPC_VMX, NULL, NULL,
188 			    sizeof(pcb->pcb_vec), NULL);
189 	}
190 
191 	if (pcb->pcb_flags & PCB_VSX) {
192 		save_fpu_nodrop(td);
193 		if (dst != NULL) {
194 			/*
195 			 * Doubleword 0 of VSR0-VSR31 overlap with FPR0-FPR31 and
196 			 * VSR32-VSR63 overlap with VR0-VR31, so we only copy
197 			 * the non-overlapping data, which is doubleword 1 of VSR0-VSR31.
198 			 */
199 			for (vsr_idx = 0; vsr_idx < nitems(vshr); vsr_idx++) {
200 				vsr_dw1 = (uint64_t *)&pcb->pcb_fpu.fpr[vsr_idx].vsr[2];
201 				vshr[vsr_idx] = *vsr_dw1;
202 			}
203 			len += elf32_populate_note(NT_PPC_VSX,
204 			    vshr, (char *)dst + len,
205 			    sizeof(vshr), NULL);
206 		} else
207 			len += elf32_populate_note(NT_PPC_VSX, NULL, NULL,
208 			    sizeof(vshr), NULL);
209 	}
210 
211 	*off = len;
212 }
213 
214 #ifndef __powerpc64__
215 bool
216 elf_is_ifunc_reloc(Elf_Size r_info __unused)
217 {
218 
219 	return (false);
220 }
221 
222 /* Process one elf relocation with addend. */
223 static int
224 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
225     int type, int local, elf_lookup_fn lookup)
226 {
227 	Elf_Addr *where;
228 	Elf_Half *hwhere;
229 	Elf_Addr addr;
230 	Elf_Addr addend;
231 	Elf_Word rtype, symidx;
232 	const Elf_Rela *rela;
233 	int error;
234 
235 	switch (type) {
236 	case ELF_RELOC_REL:
237 		panic("PPC only supports RELA relocations");
238 		break;
239 	case ELF_RELOC_RELA:
240 		rela = (const Elf_Rela *)data;
241 		where = (Elf_Addr *) ((uintptr_t)relocbase + rela->r_offset);
242 		hwhere = (Elf_Half *) ((uintptr_t)relocbase + rela->r_offset);
243 		addend = rela->r_addend;
244 		rtype = ELF_R_TYPE(rela->r_info);
245 		symidx = ELF_R_SYM(rela->r_info);
246 		break;
247 	default:
248 		panic("elf_reloc: unknown relocation mode %d\n", type);
249 	}
250 
251 	switch (rtype) {
252 
253 	case R_PPC_NONE:
254 		break;
255 
256 	case R_PPC_ADDR32: /* word32 S + A */
257 		error = lookup(lf, symidx, 1, &addr);
258 		if (error != 0)
259 			return -1;
260 		*where = elf_relocaddr(lf, addr + addend);
261 			break;
262 
263 	case R_PPC_ADDR16_LO: /* #lo(S) */
264 		error = lookup(lf, symidx, 1, &addr);
265 		if (error != 0)
266 			return -1;
267 		/*
268 		 * addend values are sometimes relative to sections
269 		 * (i.e. .rodata) in rela, where in reality they
270 		 * are relative to relocbase. Detect this condition.
271 		 */
272 		if (addr > relocbase && addr <= (relocbase + addend))
273 			addr = relocbase;
274 		addr = elf_relocaddr(lf, addr + addend);
275 		*hwhere = addr & 0xffff;
276 		break;
277 
278 	case R_PPC_ADDR16_HA: /* #ha(S) */
279 		error = lookup(lf, symidx, 1, &addr);
280 		if (error != 0)
281 			return -1;
282 		/*
283 		 * addend values are sometimes relative to sections
284 		 * (i.e. .rodata) in rela, where in reality they
285 		 * are relative to relocbase. Detect this condition.
286 		 */
287 		if (addr > relocbase && addr <= (relocbase + addend))
288 			addr = relocbase;
289 		addr = elf_relocaddr(lf, addr + addend);
290 		*hwhere = ((addr >> 16) + ((addr & 0x8000) ? 1 : 0))
291 		    & 0xffff;
292 		break;
293 
294 	case R_PPC_RELATIVE: /* word32 B + A */
295 		*where = elf_relocaddr(lf, relocbase + addend);
296 		break;
297 
298 	default:
299 		printf("kldload: unexpected relocation type %d\n",
300 		    (int) rtype);
301 		return -1;
302 	}
303 	return(0);
304 }
305 
306 void
307 elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase)
308 {
309 	Elf_Rela *rela = NULL, *relalim;
310 	Elf_Addr relasz = 0;
311 	Elf_Addr *where;
312 
313 	/*
314 	 * Extract the rela/relasz values from the dynamic section
315 	 */
316 	for (; dynp->d_tag != DT_NULL; dynp++) {
317 		switch (dynp->d_tag) {
318 		case DT_RELA:
319 			rela = (Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
320 			break;
321 		case DT_RELASZ:
322 			relasz = dynp->d_un.d_val;
323 			break;
324 		}
325 	}
326 
327 	/*
328 	 * Relocate these values
329 	 */
330 	relalim = (Elf_Rela *)((caddr_t)rela + relasz);
331 	for (; rela < relalim; rela++) {
332 		if (ELF_R_TYPE(rela->r_info) != R_PPC_RELATIVE)
333 			continue;
334 		where = (Elf_Addr *)(relocbase + rela->r_offset);
335 		*where = (Elf_Addr)(relocbase + rela->r_addend);
336 	}
337 }
338 
339 int
340 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
341     elf_lookup_fn lookup)
342 {
343 
344 	return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
345 }
346 
347 int
348 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
349     int type, elf_lookup_fn lookup)
350 {
351 
352 	return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
353 }
354 
355 int
356 elf_cpu_load_file(linker_file_t lf)
357 {
358 	/* Only sync the cache for non-kernel modules */
359 	if (lf->id != 1)
360 		__syncicache(lf->address, lf->size);
361 	return (0);
362 }
363 
364 int
365 elf_cpu_unload_file(linker_file_t lf __unused)
366 {
367 
368 	return (0);
369 }
370 #endif
371 
372 #ifdef __powerpc64__
373 static void
374 ppc32_fixlimit(struct rlimit *rl, int which)
375 {
376 	switch (which) {
377 	case RLIMIT_DATA:
378 		if (ppc32_maxdsiz != 0) {
379 			if (rl->rlim_cur > ppc32_maxdsiz)
380 				rl->rlim_cur = ppc32_maxdsiz;
381 			if (rl->rlim_max > ppc32_maxdsiz)
382 				rl->rlim_max = ppc32_maxdsiz;
383 		}
384 		break;
385 	case RLIMIT_STACK:
386 		if (ppc32_maxssiz != 0) {
387 			if (rl->rlim_cur > ppc32_maxssiz)
388 				rl->rlim_cur = ppc32_maxssiz;
389 			if (rl->rlim_max > ppc32_maxssiz)
390 				rl->rlim_max = ppc32_maxssiz;
391 		}
392 		break;
393 	}
394 }
395 #endif
396