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