1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
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
28 #include <sys/param.h>
29 #include <sys/kernel.h>
30 #include <sys/systm.h>
31 #include <sys/elf.h>
32 #include <sys/exec.h>
33 #include <sys/imgact.h>
34 #include <sys/malloc.h>
35 #include <sys/proc.h>
36 #include <sys/namei.h>
37 #include <sys/fcntl.h>
38 #include <sys/reg.h>
39 #include <sys/sysent.h>
40 #include <sys/imgact_elf.h>
41 #include <sys/jail.h>
42 #include <sys/smp.h>
43 #include <sys/syscall.h>
44 #include <sys/signalvar.h>
45 #include <sys/vnode.h>
46 #include <sys/linker.h>
47
48 #include <vm/vm.h>
49 #include <vm/vm_param.h>
50 #include <vm/pmap.h>
51 #include <vm/vm_map.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/md_var.h>
58
59 #include <powerpc/powerpc/elf_common.c>
60
61 static void exec_setregs_funcdesc(struct thread *td, struct image_params *imgp,
62 uintptr_t stack);
63
64 struct sysentvec elf64_freebsd_sysvec_v1 = {
65 .sv_size = SYS_MAXSYSCALL,
66 .sv_table = sysent,
67 .sv_fixup = __elfN(freebsd_fixup),
68 .sv_sendsig = sendsig,
69 .sv_sigcode = sigcode64,
70 .sv_szsigcode = &szsigcode64,
71 .sv_name = "FreeBSD ELF64",
72 .sv_coredump = __elfN(coredump),
73 .sv_elf_core_osabi = ELFOSABI_FREEBSD,
74 .sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR,
75 .sv_elf_core_prepare_notes = __elfN(prepare_notes),
76 .sv_minsigstksz = MINSIGSTKSZ,
77 .sv_minuser = VM_MIN_ADDRESS,
78 .sv_maxuser = VM_MAXUSER_ADDRESS,
79 .sv_usrstack = USRSTACK,
80 .sv_psstrings = PS_STRINGS,
81 .sv_psstringssz = sizeof(struct ps_strings),
82 .sv_stackprot = VM_PROT_ALL,
83 .sv_copyout_auxargs = __elfN(powerpc_copyout_auxargs),
84 .sv_copyout_strings = exec_copyout_strings,
85 .sv_setregs = exec_setregs_funcdesc,
86 .sv_fixlimit = NULL,
87 .sv_maxssiz = NULL,
88 .sv_flags = SV_ABI_FREEBSD | SV_LP64 | SV_SHP | SV_ASLR |
89 SV_TIMEKEEP | SV_RNG_SEED_VER | SV_SIGSYS,
90 .sv_set_syscall_retval = cpu_set_syscall_retval,
91 .sv_fetch_syscall_args = cpu_fetch_syscall_args,
92 .sv_syscallnames = syscallnames,
93 .sv_shared_page_base = SHAREDPAGE,
94 .sv_shared_page_len = PAGE_SIZE,
95 .sv_schedtail = NULL,
96 .sv_thread_detach = NULL,
97 .sv_trap = NULL,
98 .sv_hwcap = &cpu_features,
99 .sv_hwcap2 = &cpu_features2,
100 .sv_onexec_old = exec_onexec_old,
101 .sv_onexit = exit_onexit,
102 .sv_regset_begin = SET_BEGIN(__elfN(regset)),
103 .sv_regset_end = SET_LIMIT(__elfN(regset)),
104 };
105
106 struct sysentvec elf64_freebsd_sysvec_v2 = {
107 .sv_size = SYS_MAXSYSCALL,
108 .sv_table = sysent,
109 .sv_fixup = __elfN(freebsd_fixup),
110 .sv_sendsig = sendsig,
111 .sv_sigcode = sigcode64, /* Fixed up in ppc64_init_sysvecs(). */
112 .sv_szsigcode = &szsigcode64,
113 .sv_name = "FreeBSD ELF64 V2",
114 .sv_coredump = __elfN(coredump),
115 .sv_elf_core_osabi = ELFOSABI_FREEBSD,
116 .sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR,
117 .sv_elf_core_prepare_notes = __elfN(prepare_notes),
118 .sv_minsigstksz = MINSIGSTKSZ,
119 .sv_minuser = VM_MIN_ADDRESS,
120 .sv_maxuser = VM_MAXUSER_ADDRESS,
121 .sv_usrstack = USRSTACK,
122 .sv_psstrings = PS_STRINGS,
123 .sv_psstringssz = sizeof(struct ps_strings),
124 .sv_stackprot = VM_PROT_ALL,
125 .sv_copyout_auxargs = __elfN(powerpc_copyout_auxargs),
126 .sv_copyout_strings = exec_copyout_strings,
127 .sv_setregs = exec_setregs,
128 .sv_fixlimit = NULL,
129 .sv_maxssiz = NULL,
130 .sv_flags = SV_ABI_FREEBSD | SV_LP64 | SV_SHP |
131 SV_TIMEKEEP | SV_RNG_SEED_VER | SV_SIGSYS,
132 .sv_set_syscall_retval = cpu_set_syscall_retval,
133 .sv_fetch_syscall_args = cpu_fetch_syscall_args,
134 .sv_syscallnames = syscallnames,
135 .sv_shared_page_base = SHAREDPAGE,
136 .sv_shared_page_len = PAGE_SIZE,
137 .sv_schedtail = NULL,
138 .sv_thread_detach = NULL,
139 .sv_trap = NULL,
140 .sv_hwcap = &cpu_features,
141 .sv_hwcap2 = &cpu_features2,
142 .sv_onexec_old = exec_onexec_old,
143 .sv_onexit = exit_onexit,
144 .sv_regset_begin = SET_BEGIN(__elfN(regset)),
145 .sv_regset_end = SET_LIMIT(__elfN(regset)),
146 };
147
148 static bool ppc64_elfv1_header_match(const struct image_params *params,
149 const int32_t *, const uint32_t *);
150 static bool ppc64_elfv2_header_match(const struct image_params *params,
151 const int32_t *, const uint32_t *);
152
153 static Elf64_Brandinfo freebsd_brand_info_elfv1 = {
154 .brand = ELFOSABI_FREEBSD,
155 .machine = EM_PPC64,
156 .compat_3_brand = "FreeBSD",
157 .interp_path = "/libexec/ld-elf.so.1",
158 .sysvec = &elf64_freebsd_sysvec_v1,
159 .interp_newpath = NULL,
160 .brand_note = &elf64_freebsd_brandnote,
161 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
162 .header_supported = &ppc64_elfv1_header_match
163 };
164
165 SYSINIT(elf64v1, SI_SUB_EXEC, SI_ORDER_ANY,
166 (sysinit_cfunc_t) elf64_insert_brand_entry,
167 &freebsd_brand_info_elfv1);
168
169 static Elf64_Brandinfo freebsd_brand_info_elfv2 = {
170 .brand = ELFOSABI_FREEBSD,
171 .machine = EM_PPC64,
172 .compat_3_brand = "FreeBSD",
173 .interp_path = "/libexec/ld-elf.so.1",
174 .sysvec = &elf64_freebsd_sysvec_v2,
175 .interp_newpath = NULL,
176 .brand_note = &elf64_freebsd_brandnote,
177 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
178 .header_supported = &ppc64_elfv2_header_match
179 };
180
181 SYSINIT(elf64v2, SI_SUB_EXEC, SI_ORDER_ANY,
182 (sysinit_cfunc_t) elf64_insert_brand_entry,
183 &freebsd_brand_info_elfv2);
184
185 static Elf64_Brandinfo freebsd_brand_oinfo = {
186 .brand = ELFOSABI_FREEBSD,
187 .machine = EM_PPC64,
188 .compat_3_brand = "FreeBSD",
189 .interp_path = "/usr/libexec/ld-elf.so.1",
190 .sysvec = &elf64_freebsd_sysvec_v1,
191 .interp_newpath = NULL,
192 .brand_note = &elf64_freebsd_brandnote,
193 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
194 .header_supported = &ppc64_elfv1_header_match
195 };
196
197 SYSINIT(oelf64, SI_SUB_EXEC, SI_ORDER_ANY,
198 (sysinit_cfunc_t) elf64_insert_brand_entry,
199 &freebsd_brand_oinfo);
200
201 void elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase);
202
203 static void
ppc64_init_sysvecs(void * arg)204 ppc64_init_sysvecs(void *arg)
205 {
206 exec_sysvec_init(&elf64_freebsd_sysvec_v2);
207 exec_sysvec_init_secondary(&elf64_freebsd_sysvec_v2,
208 &elf64_freebsd_sysvec_v1);
209 /*
210 * Adjust elfv2 sigcode after elfv1 sysvec is initialized.
211 * exec_sysvec_init_secondary() assumes secondary sysvecs use
212 * identical signal code, and skips allocating a second copy.
213 * Since the ELFv2 trampoline is a strict subset of the ELFv1 code,
214 * we can work around this by adjusting the offset. This also
215 * avoids two copies of the trampoline code being allocated!
216 */
217 elf64_freebsd_sysvec_v2.sv_sigcode_offset +=
218 (uintptr_t)sigcode64_elfv2 - (uintptr_t)&sigcode64;
219 elf64_freebsd_sysvec_v2.sv_szsigcode = &szsigcode64_elfv2;
220 }
221 SYSINIT(elf64_sysvec, SI_SUB_EXEC, SI_ORDER_ANY, ppc64_init_sysvecs, NULL);
222
223 static bool
ppc64_elfv1_header_match(const struct image_params * params,const int32_t * osrel __unused,const uint32_t * fctl0 __unused)224 ppc64_elfv1_header_match(const struct image_params *params,
225 const int32_t *osrel __unused, const uint32_t *fctl0 __unused)
226 {
227 const Elf64_Ehdr *hdr = (const Elf64_Ehdr *)params->image_header;
228 int abi = (hdr->e_flags & 3);
229
230 return (abi == 0 || abi == 1);
231 }
232
233 static bool
ppc64_elfv2_header_match(const struct image_params * params,const int32_t * osrel __unused,const uint32_t * fctl0 __unused)234 ppc64_elfv2_header_match(const struct image_params *params,
235 const int32_t *osrel __unused, const uint32_t *fctl0 __unused)
236 {
237 const Elf64_Ehdr *hdr = (const Elf64_Ehdr *)params->image_header;
238 int abi = (hdr->e_flags & 3);
239
240 return (abi == 2);
241 }
242
243 static void
exec_setregs_funcdesc(struct thread * td,struct image_params * imgp,uintptr_t stack)244 exec_setregs_funcdesc(struct thread *td, struct image_params *imgp,
245 uintptr_t stack)
246 {
247 struct trapframe *tf;
248 register_t entry_desc[3];
249
250 tf = trapframe(td);
251 exec_setregs(td, imgp, stack);
252
253 /*
254 * For 64-bit ELFv1, we need to disentangle the function
255 * descriptor
256 *
257 * 0. entry point
258 * 1. TOC value (r2)
259 * 2. Environment pointer (r11)
260 */
261
262 (void)copyin((void *)imgp->entry_addr, entry_desc,
263 sizeof(entry_desc));
264 tf->srr0 = entry_desc[0] + imgp->reloc_base;
265 tf->fixreg[2] = entry_desc[1] + imgp->reloc_base;
266 tf->fixreg[11] = entry_desc[2] + imgp->reloc_base;
267 }
268
269 void
elf64_dump_thread(struct thread * td,void * dst,size_t * off)270 elf64_dump_thread(struct thread *td, void *dst, size_t *off)
271 {
272 size_t len;
273 struct pcb *pcb;
274 uint64_t vshr[32];
275 uint64_t *vsr_dw1;
276 int vsr_idx;
277
278 len = 0;
279 pcb = td->td_pcb;
280
281 if (pcb->pcb_flags & PCB_VEC) {
282 if (dst != NULL) {
283 len += elf64_populate_note(NT_PPC_VMX,
284 &pcb->pcb_vec, (char *)dst + len,
285 sizeof(pcb->pcb_vec), NULL);
286 } else
287 len += elf64_populate_note(NT_PPC_VMX, NULL, NULL,
288 sizeof(pcb->pcb_vec), NULL);
289 }
290
291 if (pcb->pcb_flags & PCB_VSX) {
292 if (dst != NULL) {
293 /*
294 * Doubleword 0 of VSR0-VSR31 overlap with FPR0-FPR31 and
295 * VSR32-VSR63 overlap with VR0-VR31, so we only copy
296 * the non-overlapping data, which is doubleword 1 of VSR0-VSR31.
297 */
298 for (vsr_idx = 0; vsr_idx < nitems(vshr); vsr_idx++) {
299 vsr_dw1 = (uint64_t *)&pcb->pcb_fpu.fpr[vsr_idx].vsr[2];
300 vshr[vsr_idx] = *vsr_dw1;
301 }
302 len += elf64_populate_note(NT_PPC_VSX,
303 vshr, (char *)dst + len,
304 sizeof(vshr), NULL);
305 } else
306 len += elf64_populate_note(NT_PPC_VSX, NULL, NULL,
307 sizeof(vshr), NULL);
308 }
309
310 *off = len;
311 }
312
313 bool
elf_is_ifunc_reloc(Elf_Size r_info)314 elf_is_ifunc_reloc(Elf_Size r_info)
315 {
316
317 return (ELF_R_TYPE(r_info) == R_PPC_IRELATIVE);
318 }
319
320 /* Process one elf relocation with addend. */
321 static int
elf_reloc_internal(linker_file_t lf,Elf_Addr relocbase,const void * data,int type,int local,elf_lookup_fn lookup)322 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
323 int type, int local, elf_lookup_fn lookup)
324 {
325 Elf_Addr *where;
326 Elf_Addr addr;
327 Elf_Addr addend, val;
328 Elf_Word rtype, symidx;
329 const Elf_Rela *rela;
330 int error;
331
332 switch (type) {
333 case ELF_RELOC_REL:
334 panic("PPC only supports RELA relocations");
335 break;
336 case ELF_RELOC_RELA:
337 rela = (const Elf_Rela *)data;
338 where = (Elf_Addr *) (relocbase + rela->r_offset);
339 addend = rela->r_addend;
340 rtype = ELF_R_TYPE(rela->r_info);
341 symidx = ELF_R_SYM(rela->r_info);
342 break;
343 default:
344 panic("elf_reloc: unknown relocation mode %d\n", type);
345 }
346
347 switch (rtype) {
348 case R_PPC_NONE:
349 break;
350
351 case R_PPC64_ADDR64: /* doubleword64 S + A */
352 error = lookup(lf, symidx, 1, &addr);
353 if (error != 0)
354 return (-1);
355 addr += addend;
356 *where = addr;
357 break;
358
359 case R_PPC_RELATIVE: /* doubleword64 B + A */
360 *where = elf_relocaddr(lf, relocbase + addend);
361 break;
362
363 case R_PPC_JMP_SLOT: /* function descriptor copy */
364 lookup(lf, symidx, 1, &addr);
365 #if !defined(_CALL_ELF) || _CALL_ELF == 1
366 memcpy(where, (Elf_Addr *)addr, 3*sizeof(Elf_Addr));
367 #else
368 *where = addr;
369 #endif
370 __asm __volatile("dcbst 0,%0; sync" :: "r"(where) : "memory");
371 break;
372
373 case R_PPC_IRELATIVE:
374 addr = relocbase + addend;
375 val = ((Elf64_Addr (*)(void))addr)();
376 if (*where != val)
377 *where = val;
378 break;
379
380 default:
381 printf("kldload: unexpected relocation type %d, "
382 "symbol index %d\n", (int)rtype, symidx);
383 return (-1);
384 }
385 return (0);
386 }
387
388 void
elf_reloc_self(Elf_Dyn * dynp,Elf_Addr relocbase)389 elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase)
390 {
391 Elf_Rela *rela = NULL, *relalim;
392 Elf_Addr relasz = 0;
393 Elf_Addr *where;
394
395 /*
396 * Extract the rela/relasz values from the dynamic section
397 */
398 for (; dynp->d_tag != DT_NULL; dynp++) {
399 switch (dynp->d_tag) {
400 case DT_RELA:
401 rela = (Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
402 break;
403 case DT_RELASZ:
404 relasz = dynp->d_un.d_val;
405 break;
406 }
407 }
408
409 /*
410 * Relocate these values
411 */
412 relalim = (Elf_Rela *)((caddr_t)rela + relasz);
413 for (; rela < relalim; rela++) {
414 if (ELF_R_TYPE(rela->r_info) != R_PPC_RELATIVE)
415 continue;
416 where = (Elf_Addr *)(relocbase + rela->r_offset);
417 *where = (Elf_Addr)(relocbase + rela->r_addend);
418 }
419 }
420
421 int
elf_reloc(linker_file_t lf,Elf_Addr relocbase,const void * data,int type,elf_lookup_fn lookup)422 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
423 elf_lookup_fn lookup)
424 {
425
426 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
427 }
428
429 int
elf_reloc_local(linker_file_t lf,Elf_Addr relocbase,const void * data,int type,elf_lookup_fn lookup)430 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
431 int type, elf_lookup_fn lookup)
432 {
433
434 return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
435 }
436
437 int
elf_cpu_load_file(linker_file_t lf)438 elf_cpu_load_file(linker_file_t lf)
439 {
440 /* Only sync the cache for non-kernel modules */
441 if (lf->id != 1)
442 __syncicache(lf->address, lf->size);
443 return (0);
444 }
445
446 int
elf_cpu_unload_file(linker_file_t lf __unused)447 elf_cpu_unload_file(linker_file_t lf __unused)
448 {
449
450 return (0);
451 }
452
453 int
elf_cpu_parse_dynamic(caddr_t loadbase __unused,Elf_Dyn * dynamic __unused)454 elf_cpu_parse_dynamic(caddr_t loadbase __unused, Elf_Dyn *dynamic __unused)
455 {
456
457 return (0);
458 }
459