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