]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/powerpc/powerpc/elf32_machdep.c
Update bmake to version 20180919
[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/syscall.h>
45 #include <sys/sysctl.h>
46 #include <sys/signalvar.h>
47 #include <sys/vnode.h>
48 #include <sys/linker.h>
49
50 #include <vm/vm.h>
51 #include <vm/vm_param.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/reg.h>
58 #include <machine/md_var.h>
59
60 #ifdef __powerpc64__
61 #include <compat/freebsd32/freebsd32_proto.h>
62 #include <compat/freebsd32/freebsd32_util.h>
63
64 extern const char *freebsd32_syscallnames[];
65 static void ppc32_fixlimit(struct rlimit *rl, int which);
66
67 static SYSCTL_NODE(_compat, OID_AUTO, ppc32, CTLFLAG_RW, 0, "32-bit mode");
68
69 #define PPC32_MAXDSIZ (1024*1024*1024)
70 static u_long ppc32_maxdsiz = PPC32_MAXDSIZ;
71 SYSCTL_ULONG(_compat_ppc32, OID_AUTO, maxdsiz, CTLFLAG_RWTUN, &ppc32_maxdsiz,
72              0, "");
73 #define PPC32_MAXSSIZ (64*1024*1024)
74 u_long ppc32_maxssiz = PPC32_MAXSSIZ;
75 SYSCTL_ULONG(_compat_ppc32, OID_AUTO, maxssiz, CTLFLAG_RWTUN, &ppc32_maxssiz,
76              0, "");
77 #endif
78
79 struct sysentvec elf32_freebsd_sysvec = {
80         .sv_size        = SYS_MAXSYSCALL,
81 #ifdef __powerpc64__
82         .sv_table       = freebsd32_sysent,
83 #else
84         .sv_table       = sysent,
85 #endif
86         .sv_mask        = 0,
87         .sv_errsize     = 0,
88         .sv_errtbl      = NULL,
89         .sv_transtrap   = NULL,
90         .sv_fixup       = __elfN(freebsd_fixup),
91         .sv_sendsig     = sendsig,
92         .sv_sigcode     = sigcode32,
93         .sv_szsigcode   = &szsigcode32,
94         .sv_name        = "FreeBSD ELF32",
95         .sv_coredump    = __elfN(coredump),
96         .sv_imgact_try  = NULL,
97         .sv_minsigstksz = MINSIGSTKSZ,
98         .sv_pagesize    = PAGE_SIZE,
99         .sv_minuser     = VM_MIN_ADDRESS,
100         .sv_stackprot   = VM_PROT_ALL,
101 #ifdef __powerpc64__
102         .sv_maxuser     = VM_MAXUSER_ADDRESS,
103         .sv_usrstack    = FREEBSD32_USRSTACK,
104         .sv_psstrings   = FREEBSD32_PS_STRINGS,
105         .sv_copyout_strings = freebsd32_copyout_strings,
106         .sv_setregs     = ppc32_setregs,
107         .sv_syscallnames = freebsd32_syscallnames,
108         .sv_fixlimit    = ppc32_fixlimit,
109 #else
110         .sv_maxuser     = VM_MAXUSER_ADDRESS,
111         .sv_usrstack    = USRSTACK,
112         .sv_psstrings   = PS_STRINGS,
113         .sv_copyout_strings = exec_copyout_strings,
114         .sv_setregs     = exec_setregs,
115         .sv_syscallnames = syscallnames,
116         .sv_fixlimit    = NULL,
117 #endif
118         .sv_maxssiz     = NULL,
119         .sv_flags       = SV_ABI_FREEBSD | SV_ILP32 | SV_SHP,
120         .sv_set_syscall_retval = cpu_set_syscall_retval,
121         .sv_fetch_syscall_args = cpu_fetch_syscall_args,
122         .sv_shared_page_base = FREEBSD32_SHAREDPAGE,
123         .sv_shared_page_len = PAGE_SIZE,
124         .sv_schedtail   = NULL,
125         .sv_thread_detach = NULL,
126         .sv_trap        = NULL,
127         .sv_hwcap       = &cpu_features,
128         .sv_hwcap2      = &cpu_features2,
129 };
130 INIT_SYSENTVEC(elf32_sysvec, &elf32_freebsd_sysvec);
131
132 static Elf32_Brandinfo freebsd_brand_info = {
133         .brand          = ELFOSABI_FREEBSD,
134         .machine        = EM_PPC,
135         .compat_3_brand = "FreeBSD",
136         .emul_path      = NULL,
137         .interp_path    = "/libexec/ld-elf.so.1",
138         .sysvec         = &elf32_freebsd_sysvec,
139 #ifdef __powerpc64__
140         .interp_newpath = "/libexec/ld-elf32.so.1",
141 #else
142         .interp_newpath = NULL,
143 #endif
144         .brand_note     = &elf32_freebsd_brandnote,
145         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
146 };
147
148 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST,
149     (sysinit_cfunc_t) elf32_insert_brand_entry,
150     &freebsd_brand_info);
151
152 static Elf32_Brandinfo freebsd_brand_oinfo = {
153         .brand          = ELFOSABI_FREEBSD,
154         .machine        = EM_PPC,
155         .compat_3_brand = "FreeBSD",
156         .emul_path      = NULL,
157         .interp_path    = "/usr/libexec/ld-elf.so.1",
158         .sysvec         = &elf32_freebsd_sysvec,
159         .interp_newpath = NULL,
160         .brand_note     = &elf32_freebsd_brandnote,
161         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
162 };
163
164 SYSINIT(oelf32, SI_SUB_EXEC, SI_ORDER_ANY,
165         (sysinit_cfunc_t) elf32_insert_brand_entry,
166         &freebsd_brand_oinfo);
167
168 void elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase);
169
170 void
171 elf32_dump_thread(struct thread *td, void *dst, size_t *off)
172 {
173         size_t len;
174         struct pcb *pcb;
175         uint64_t vshr[32];
176         uint64_t *vsr_dw1;
177         int vsr_idx;
178
179         len = 0;
180         pcb = td->td_pcb;
181
182         if (pcb->pcb_flags & PCB_VEC) {
183                 save_vec_nodrop(td);
184                 if (dst != NULL) {
185                         len += elf32_populate_note(NT_PPC_VMX,
186                             &pcb->pcb_vec, (char *)dst + len,
187                             sizeof(pcb->pcb_vec), NULL);
188                 } else
189                         len += elf32_populate_note(NT_PPC_VMX, NULL, NULL,
190                             sizeof(pcb->pcb_vec), NULL);
191         }
192
193         if (pcb->pcb_flags & PCB_VSX) {
194                 save_fpu_nodrop(td);
195                 if (dst != NULL) {
196                         /*
197                          * Doubleword 0 of VSR0-VSR31 overlap with FPR0-FPR31 and
198                          * VSR32-VSR63 overlap with VR0-VR31, so we only copy
199                          * the non-overlapping data, which is doubleword 1 of VSR0-VSR31.
200                          */
201                         for (vsr_idx = 0; vsr_idx < nitems(vshr); vsr_idx++) {
202                                 vsr_dw1 = (uint64_t *)&pcb->pcb_fpu.fpr[vsr_idx].vsr[2];
203                                 vshr[vsr_idx] = *vsr_dw1;
204                         }
205                         len += elf32_populate_note(NT_PPC_VSX,
206                             vshr, (char *)dst + len,
207                             sizeof(vshr), NULL);
208                 } else
209                         len += elf32_populate_note(NT_PPC_VSX, NULL, NULL,
210                             sizeof(vshr), NULL);
211         }
212
213         *off = len;
214 }
215
216 #ifndef __powerpc64__
217 bool
218 elf_is_ifunc_reloc(Elf_Size r_info __unused)
219 {
220
221         return (false);
222 }
223
224 /* Process one elf relocation with addend. */
225 static int
226 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
227     int type, int local, elf_lookup_fn lookup)
228 {
229         Elf_Addr *where;
230         Elf_Half *hwhere;
231         Elf_Addr addr;
232         Elf_Addr addend;
233         Elf_Word rtype, symidx;
234         const Elf_Rela *rela;
235         int error;
236
237         switch (type) {
238         case ELF_RELOC_REL:
239                 panic("PPC only supports RELA relocations");
240                 break;
241         case ELF_RELOC_RELA:
242                 rela = (const Elf_Rela *)data;
243                 where = (Elf_Addr *) ((uintptr_t)relocbase + rela->r_offset);
244                 hwhere = (Elf_Half *) ((uintptr_t)relocbase + rela->r_offset);
245                 addend = rela->r_addend;
246                 rtype = ELF_R_TYPE(rela->r_info);
247                 symidx = ELF_R_SYM(rela->r_info);
248                 break;
249         default:
250                 panic("elf_reloc: unknown relocation mode %d\n", type);
251         }
252
253         switch (rtype) {
254
255         case R_PPC_NONE:
256                 break;
257
258         case R_PPC_ADDR32: /* word32 S + A */
259                 error = lookup(lf, symidx, 1, &addr);
260                 if (error != 0)
261                         return -1;
262                 *where = elf_relocaddr(lf, addr + addend);
263                         break;
264
265         case R_PPC_ADDR16_LO: /* #lo(S) */
266                 error = lookup(lf, symidx, 1, &addr);
267                 if (error != 0)
268                         return -1;
269                 /*
270                  * addend values are sometimes relative to sections
271                  * (i.e. .rodata) in rela, where in reality they
272                  * are relative to relocbase. Detect this condition.
273                  */
274                 if (addr > relocbase && addr <= (relocbase + addend))
275                         addr = relocbase;
276                 addr = elf_relocaddr(lf, addr + addend);
277                 *hwhere = addr & 0xffff;
278                 break;
279
280         case R_PPC_ADDR16_HA: /* #ha(S) */
281                 error = lookup(lf, symidx, 1, &addr);
282                 if (error != 0)
283                         return -1;
284                 /*
285                  * addend values are sometimes relative to sections
286                  * (i.e. .rodata) in rela, where in reality they
287                  * are relative to relocbase. Detect this condition.
288                  */
289                 if (addr > relocbase && addr <= (relocbase + addend))
290                         addr = relocbase;
291                 addr = elf_relocaddr(lf, addr + addend);
292                 *hwhere = ((addr >> 16) + ((addr & 0x8000) ? 1 : 0))
293                     & 0xffff;
294                 break;
295
296         case R_PPC_RELATIVE: /* word32 B + A */
297                 *where = elf_relocaddr(lf, relocbase + addend);
298                 break;
299
300         default:
301                 printf("kldload: unexpected relocation type %d\n",
302                     (int) rtype);
303                 return -1;
304         }
305         return(0);
306 }
307
308 void
309 elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase)
310 {
311         Elf_Rela *rela = NULL, *relalim;
312         Elf_Addr relasz = 0;
313         Elf_Addr *where;
314
315         /*
316          * Extract the rela/relasz values from the dynamic section
317          */
318         for (; dynp->d_tag != DT_NULL; dynp++) {
319                 switch (dynp->d_tag) {
320                 case DT_RELA:
321                         rela = (Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
322                         break;
323                 case DT_RELASZ:
324                         relasz = dynp->d_un.d_val;
325                         break;
326                 }
327         }
328
329         /*
330          * Relocate these values
331          */
332         relalim = (Elf_Rela *)((caddr_t)rela + relasz);
333         for (; rela < relalim; rela++) {
334                 if (ELF_R_TYPE(rela->r_info) != R_PPC_RELATIVE)
335                         continue;
336                 where = (Elf_Addr *)(relocbase + rela->r_offset);
337                 *where = (Elf_Addr)(relocbase + rela->r_addend);
338         }
339 }
340
341 int
342 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
343     elf_lookup_fn lookup)
344 {
345
346         return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
347 }
348
349 int
350 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
351     int type, elf_lookup_fn lookup)
352 {
353
354         return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
355 }
356
357 int
358 elf_cpu_load_file(linker_file_t lf)
359 {
360         /* Only sync the cache for non-kernel modules */
361         if (lf->id != 1)
362                 __syncicache(lf->address, lf->size);
363         return (0);
364 }
365
366 int
367 elf_cpu_unload_file(linker_file_t lf __unused)
368 {
369
370         return (0);
371 }
372 #endif
373
374 #ifdef __powerpc64__
375 static void
376 ppc32_fixlimit(struct rlimit *rl, int which)
377 {
378         switch (which) {
379         case RLIMIT_DATA:
380                 if (ppc32_maxdsiz != 0) {
381                         if (rl->rlim_cur > ppc32_maxdsiz)
382                                 rl->rlim_cur = ppc32_maxdsiz;
383                         if (rl->rlim_max > ppc32_maxdsiz)
384                                 rl->rlim_max = ppc32_maxdsiz;
385                 }
386                 break;
387         case RLIMIT_STACK:
388                 if (ppc32_maxssiz != 0) {
389                         if (rl->rlim_cur > ppc32_maxssiz)
390                                 rl->rlim_cur = ppc32_maxssiz;
391                         if (rl->rlim_max > ppc32_maxssiz)
392                                 rl->rlim_max = ppc32_maxssiz;
393                 }
394                 break;
395         }
396 }
397 #endif