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