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