]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/arm64/elf32_machdep.c
libarchive: merge from vendor branch
[FreeBSD/FreeBSD.git] / sys / arm64 / arm64 / elf32_machdep.c
1 /*-
2  * Copyright (c) 2014, 2015 The FreeBSD Foundation.
3  * Copyright (c) 2014, 2017 Andrew Turner.
4  * Copyright (c) 2018 Olivier Houchard
5  * All rights reserved.
6  *
7  * This software was developed by Andrew Turner under
8  * sponsorship from the FreeBSD Foundation.
9  *
10  * Portions of this software were developed by Konstantin Belousov
11  * under sponsorship from the FreeBSD Foundation.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #define __ELF_WORD_SIZE 32
39
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/systm.h>
43 #include <sys/exec.h>
44 #include <sys/imgact.h>
45 #include <sys/linker.h>
46 #include <sys/proc.h>
47 #include <sys/reg.h>
48 #include <sys/sysent.h>
49 #include <sys/imgact_elf.h>
50 #include <sys/syscall.h>
51 #include <sys/signalvar.h>
52 #include <sys/vnode.h>
53
54 #include <machine/elf.h>
55 #include <machine/pcb.h>
56 #ifdef VFP
57 #include <machine/vfp.h>
58 #endif
59
60 #include <compat/freebsd32/freebsd32_util.h>
61
62 #define FREEBSD32_MINUSER       0x00001000
63 #define FREEBSD32_MAXUSER       ((1ul << 32) - PAGE_SIZE)
64 #define FREEBSD32_SHAREDPAGE    (FREEBSD32_MAXUSER - PAGE_SIZE)
65 #define FREEBSD32_USRSTACK      FREEBSD32_SHAREDPAGE
66
67 extern const char *freebsd32_syscallnames[];
68
69 extern char aarch32_sigcode[];
70 extern int sz_aarch32_sigcode;
71
72 static int freebsd32_fetch_syscall_args(struct thread *td);
73 static void freebsd32_setregs(struct thread *td, struct image_params *imgp,
74     u_long stack);
75 static void freebsd32_set_syscall_retval(struct thread *, int);
76
77 static bool elf32_arm_abi_supported(struct image_params *, int32_t *,
78     uint32_t *);
79
80 extern void freebsd32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);
81
82 u_long __read_frequently elf32_hwcap;
83 u_long __read_frequently elf32_hwcap2;
84
85 static struct sysentvec elf32_freebsd_sysvec = {
86         .sv_size        = SYS_MAXSYSCALL,
87         .sv_table       = freebsd32_sysent,
88         .sv_fixup       = elf32_freebsd_fixup,
89         .sv_sendsig     = freebsd32_sendsig,
90         .sv_sigcode     = aarch32_sigcode,
91         .sv_szsigcode   = &sz_aarch32_sigcode,
92         .sv_name        = "FreeBSD ELF32",
93         .sv_coredump    = elf32_coredump,
94         .sv_elf_core_osabi = ELFOSABI_FREEBSD,
95         .sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR,
96         .sv_elf_core_prepare_notes = elf32_prepare_notes,
97         .sv_minsigstksz = MINSIGSTKSZ,
98         .sv_minuser     = FREEBSD32_MINUSER,
99         .sv_maxuser     = FREEBSD32_MAXUSER,
100         .sv_usrstack    = FREEBSD32_USRSTACK,
101         .sv_psstrings   = FREEBSD32_PS_STRINGS,
102         .sv_psstringssz = sizeof(struct freebsd32_ps_strings),
103         .sv_stackprot   = VM_PROT_READ | VM_PROT_WRITE,
104         .sv_copyout_auxargs = elf32_freebsd_copyout_auxargs,
105         .sv_copyout_strings = freebsd32_copyout_strings,
106         .sv_setregs     = freebsd32_setregs,
107         .sv_fixlimit    = NULL, // XXX
108         .sv_maxssiz     = NULL,
109         .sv_flags       = SV_ABI_FREEBSD | SV_ILP32 | SV_SHP | SV_TIMEKEEP |
110             SV_RNG_SEED_VER,
111         .sv_set_syscall_retval = freebsd32_set_syscall_retval,
112         .sv_fetch_syscall_args = freebsd32_fetch_syscall_args,
113         .sv_syscallnames = freebsd32_syscallnames,
114         .sv_shared_page_base = FREEBSD32_SHAREDPAGE,
115         .sv_shared_page_len = PAGE_SIZE,
116         .sv_schedtail   = NULL,
117         .sv_thread_detach = NULL,
118         .sv_trap        = NULL,
119         .sv_hwcap       = &elf32_hwcap,
120         .sv_hwcap2      = &elf32_hwcap2,
121         .sv_onexec_old  = exec_onexec_old,
122         .sv_onexit      = exit_onexit,
123         .sv_regset_begin = SET_BEGIN(__elfN(regset)),
124         .sv_regset_end  = SET_LIMIT(__elfN(regset)),
125 };
126 INIT_SYSENTVEC(elf32_sysvec, &elf32_freebsd_sysvec);
127
128 static Elf32_Brandinfo freebsd32_brand_info = {
129         .brand          = ELFOSABI_FREEBSD,
130         .machine        = EM_ARM,
131         .compat_3_brand = "FreeBSD",
132         .interp_path    = "/libexec/ld-elf.so.1",
133         .sysvec         = &elf32_freebsd_sysvec,
134         .interp_newpath = "/libexec/ld-elf32.so.1",
135         .brand_note     = &elf32_freebsd_brandnote,
136         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
137         .header_supported= elf32_arm_abi_supported,
138 };
139
140 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST,
141     (sysinit_cfunc_t)elf32_insert_brand_entry, &freebsd32_brand_info);
142
143 static bool
144 elf32_arm_abi_supported(struct image_params *imgp, int32_t *osrel __unused,
145     uint32_t *fctl0 __unused)
146 {
147         const Elf32_Ehdr *hdr;
148
149         /* Check if we support AArch32 */
150         if (ID_AA64PFR0_EL0_VAL(READ_SPECIALREG(id_aa64pfr0_el1)) !=
151             ID_AA64PFR0_EL0_64_32)
152                 return (false);
153
154 #define EF_ARM_EABI_FREEBSD_MIN EF_ARM_EABI_VER4
155         hdr = (const Elf32_Ehdr *)imgp->image_header;
156         if (EF_ARM_EABI_VERSION(hdr->e_flags) < EF_ARM_EABI_FREEBSD_MIN) {
157                 if (bootverbose)
158                         uprintf("Attempting to execute non EABI binary "
159                             "(rev %d) image %s",
160                             EF_ARM_EABI_VERSION(hdr->e_flags),
161                             imgp->args->fname);
162                 return (false);
163         }
164
165         return (true);
166 }
167
168 static int
169 freebsd32_fetch_syscall_args(struct thread *td)
170 {
171         struct proc *p;
172         register_t *ap;
173         struct syscall_args *sa;
174         int error, i, nap, narg;
175         unsigned int args[4];
176
177         nap = 4;
178         p = td->td_proc;
179         ap = td->td_frame->tf_x;
180         sa = &td->td_sa;
181
182         /* r7 is the syscall id */
183         sa->code = td->td_frame->tf_x[7];
184         sa->original_code = sa->code;
185
186         if (sa->code == SYS_syscall) {
187                 sa->code = *ap++;
188                 nap--;
189         } else if (sa->code == SYS___syscall) {
190                 sa->code = ap[1];
191                 nap -= 2;
192                 ap += 2;
193         }
194
195         if (sa->code >= p->p_sysent->sv_size)
196                 sa->callp = &p->p_sysent->sv_table[0];
197         else
198                 sa->callp = &p->p_sysent->sv_table[sa->code];
199
200         narg = sa->callp->sy_narg;
201         for (i = 0; i < nap; i++)
202                 sa->args[i] = ap[i];
203         if (narg > nap) {
204                 if (narg - nap > nitems(args))
205                         panic("Too many system call arguiments");
206                 error = copyin((void *)td->td_frame->tf_x[13], args,
207                     (narg - nap) * sizeof(int));
208                 if (error != 0)
209                         return (error);
210                 for (i = 0; i < (narg - nap); i++)
211                         sa->args[i + nap] = args[i];
212         }
213
214         td->td_retval[0] = 0;
215         td->td_retval[1] = 0;
216
217         return (0);
218 }
219
220 static void
221 freebsd32_set_syscall_retval(struct thread *td, int error)
222 {
223         struct trapframe *frame;
224
225         frame = td->td_frame;
226         switch (error) {
227         case 0:
228                 frame->tf_x[0] = td->td_retval[0];
229                 frame->tf_x[1] = td->td_retval[1];
230                 frame->tf_spsr &= ~PSR_C;
231                 break;
232         case ERESTART:
233                 /*
234                  * Reconstruct the pc to point at the swi.
235                  */
236                 if ((frame->tf_spsr & PSR_T) != 0)
237                         frame->tf_elr -= 2; //THUMB_INSN_SIZE;
238                 else
239                         frame->tf_elr -= 4; //INSN_SIZE;
240                 break;
241         case EJUSTRETURN:
242                 /* nothing to do */
243                 break;
244         default:
245                 frame->tf_x[0] = error;
246                 frame->tf_spsr |= PSR_C;
247                 break;
248         }
249 }
250
251 static void
252 freebsd32_setregs(struct thread *td, struct image_params *imgp,
253    uintptr_t stack)
254 {
255         struct trapframe *tf = td->td_frame;
256         struct pcb *pcb = td->td_pcb;
257
258         memset(tf, 0, sizeof(struct trapframe));
259
260         /*
261          * We need to set x0 for init as it doesn't call
262          * cpu_set_syscall_retval to copy the value. We also
263          * need to set td_retval for the cases where we do.
264          */
265         tf->tf_x[0] = stack;
266         /* SP_usr is mapped to x13 */
267         tf->tf_x[13] = stack;
268         /* LR_usr is mapped to x14 */
269         tf->tf_x[14] = imgp->entry_addr;
270         tf->tf_elr = imgp->entry_addr;
271         tf->tf_spsr = PSR_M_32;
272         if ((uint32_t)imgp->entry_addr & 1)
273                 tf->tf_spsr |= PSR_T;
274
275 #ifdef VFP
276         vfp_reset_state(td, pcb);
277 #endif
278
279         /*
280          * Clear debug register state. It is not applicable to the new process.
281          */
282         bzero(&pcb->pcb_dbg_regs, sizeof(pcb->pcb_dbg_regs));
283 }
284
285 void
286 elf32_dump_thread(struct thread *td, void *dst, size_t *off)
287 {
288 }