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