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