2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 Juli Mallett <jmallett@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Based on nwhitehorn's COMPAT_FREEBSD32 support code for PowerPC64.
35 #include "opt_compat.h"
37 #define __ELF_WORD_SIZE 32
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/systm.h>
43 #include <sys/sysent.h>
45 #include <sys/imgact.h>
46 #include <sys/malloc.h>
48 #include <sys/namei.h>
49 #include <sys/fcntl.h>
50 #include <sys/sysent.h>
51 #include <sys/imgact_elf.h>
52 #include <sys/syscall.h>
53 #include <sys/syscallsubr.h>
54 #include <sys/sysproto.h>
55 #include <sys/signalvar.h>
56 #include <sys/vnode.h>
57 #include <sys/linker.h>
60 #include <vm/vm_param.h>
62 #include <machine/md_var.h>
63 #include <machine/reg.h>
64 #include <machine/sigframe.h>
65 #include <machine/sysarch.h>
66 #include <machine/tls.h>
68 #include <compat/freebsd32/freebsd32_signal.h>
69 #include <compat/freebsd32/freebsd32_util.h>
70 #include <compat/freebsd32/freebsd32_proto.h>
72 static void freebsd32_exec_setregs(struct thread *, struct image_params *, u_long);
73 static int get_mcontext32(struct thread *, mcontext32_t *, int);
74 static int set_mcontext32(struct thread *, mcontext32_t *);
75 static void freebsd32_sendsig(sig_t, ksiginfo_t *, sigset_t *);
77 extern const char *freebsd32_syscallnames[];
79 struct sysentvec elf32_freebsd_sysvec = {
80 .sv_size = SYS_MAXSYSCALL,
81 .sv_table = freebsd32_sysent,
86 .sv_fixup = __elfN(freebsd_fixup),
87 .sv_sendsig = freebsd32_sendsig,
88 .sv_sigcode = sigcode32,
89 .sv_szsigcode = &szsigcode32,
90 .sv_name = "FreeBSD ELF32",
91 .sv_coredump = __elfN(coredump),
92 .sv_imgact_try = NULL,
93 .sv_minsigstksz = MINSIGSTKSZ,
94 .sv_pagesize = PAGE_SIZE,
95 .sv_minuser = VM_MIN_ADDRESS,
96 .sv_maxuser = ((vm_offset_t)0x80000000),
97 .sv_usrstack = FREEBSD32_USRSTACK,
98 .sv_psstrings = FREEBSD32_PS_STRINGS,
99 .sv_stackprot = VM_PROT_ALL,
100 .sv_copyout_strings = freebsd32_copyout_strings,
101 .sv_setregs = freebsd32_exec_setregs,
104 .sv_flags = SV_ABI_FREEBSD | SV_ILP32,
105 .sv_set_syscall_retval = cpu_set_syscall_retval,
106 .sv_fetch_syscall_args = cpu_fetch_syscall_args,
107 .sv_syscallnames = freebsd32_syscallnames,
108 .sv_schedtail = NULL,
109 .sv_thread_detach = NULL,
112 INIT_SYSENTVEC(elf32_sysvec, &elf32_freebsd_sysvec);
114 static Elf32_Brandinfo freebsd_brand_info = {
115 .brand = ELFOSABI_FREEBSD,
117 .compat_3_brand = "FreeBSD",
119 .interp_path = "/libexec/ld-elf.so.1",
120 .sysvec = &elf32_freebsd_sysvec,
121 .interp_newpath = "/libexec/ld-elf32.so.1",
122 .brand_note = &elf32_freebsd_brandnote,
123 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
126 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST,
127 (sysinit_cfunc_t) elf32_insert_brand_entry,
128 &freebsd_brand_info);
131 freebsd32_exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
133 exec_setregs(td, imgp, stack);
136 * See comment in exec_setregs about running 32-bit binaries with 64-bit
139 td->td_frame->sp -= 65536;
142 * Clear extended address space bit for userland.
144 td->td_frame->sr &= ~MIPS_SR_UX;
146 td->td_md.md_tls_tcb_offset = TLS_TP_OFFSET + TLS_TCB_SIZE32;
150 set_regs32(struct thread *td, struct reg32 *regs)
155 for (i = 0; i < NUMSAVEREGS; i++)
156 r.r_regs[i] = regs->r_regs[i];
158 return (set_regs(td, &r));
162 fill_regs32(struct thread *td, struct reg32 *regs)
168 error = fill_regs(td, &r);
172 for (i = 0; i < NUMSAVEREGS; i++)
173 regs->r_regs[i] = r.r_regs[i];
179 set_fpregs32(struct thread *td, struct fpreg32 *fpregs)
184 for (i = 0; i < NUMFPREGS; i++)
185 fp.r_regs[i] = fpregs->r_regs[i];
187 return (set_fpregs(td, &fp));
191 fill_fpregs32(struct thread *td, struct fpreg32 *fpregs)
197 error = fill_fpregs(td, &fp);
201 for (i = 0; i < NUMFPREGS; i++)
202 fpregs->r_regs[i] = fp.r_regs[i];
208 get_mcontext32(struct thread *td, mcontext32_t *mcp, int flags)
214 error = get_mcontext(td, &mcp64, flags);
218 mcp->mc_onstack = mcp64.mc_onstack;
219 mcp->mc_pc = mcp64.mc_pc;
220 for (i = 0; i < 32; i++)
221 mcp->mc_regs[i] = mcp64.mc_regs[i];
223 mcp->mullo = mcp64.mullo;
224 mcp->mulhi = mcp64.mulhi;
225 mcp->mc_fpused = mcp64.mc_fpused;
226 for (i = 0; i < 33; i++)
227 mcp->mc_fpregs[i] = mcp64.mc_fpregs[i];
228 mcp->mc_fpc_eir = mcp64.mc_fpc_eir;
229 mcp->mc_tls = (int32_t)(intptr_t)mcp64.mc_tls;
235 set_mcontext32(struct thread *td, mcontext32_t *mcp)
240 mcp64.mc_onstack = mcp->mc_onstack;
241 mcp64.mc_pc = mcp->mc_pc;
242 for (i = 0; i < 32; i++)
243 mcp64.mc_regs[i] = mcp->mc_regs[i];
245 mcp64.mullo = mcp->mullo;
246 mcp64.mulhi = mcp->mulhi;
247 mcp64.mc_fpused = mcp->mc_fpused;
248 for (i = 0; i < 33; i++)
249 mcp64.mc_fpregs[i] = mcp->mc_fpregs[i];
250 mcp64.mc_fpc_eir = mcp->mc_fpc_eir;
251 mcp64.mc_tls = (void *)(intptr_t)mcp->mc_tls;
253 return (set_mcontext(td, &mcp64));
257 freebsd32_sigreturn(struct thread *td, struct freebsd32_sigreturn_args *uap)
262 CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
264 if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
265 CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
269 error = set_mcontext32(td, &uc.uc_mcontext);
273 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
276 CTR3(KTR_SIG, "sigreturn: return td=%p pc=%#x sp=%#x",
277 td, uc.uc_mcontext.mc_srr0, uc.uc_mcontext.mc_gpr[1]);
280 return (EJUSTRETURN);
284 * The first two fields of a ucontext_t are the signal mask and the machine
285 * context. The next field is uc_link; we want to avoid destroying the link
286 * when copying out contexts.
288 #define UC32_COPY_SIZE offsetof(ucontext32_t, uc_link)
291 freebsd32_getcontext(struct thread *td, struct freebsd32_getcontext_args *uap)
296 if (uap->ucp == NULL)
299 get_mcontext32(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
300 PROC_LOCK(td->td_proc);
301 uc.uc_sigmask = td->td_sigmask;
302 PROC_UNLOCK(td->td_proc);
303 ret = copyout(&uc, uap->ucp, UC32_COPY_SIZE);
309 freebsd32_setcontext(struct thread *td, struct freebsd32_setcontext_args *uap)
314 if (uap->ucp == NULL)
317 ret = copyin(uap->ucp, &uc, UC32_COPY_SIZE);
319 ret = set_mcontext32(td, &uc.uc_mcontext);
321 kern_sigprocmask(td, SIG_SETMASK,
322 &uc.uc_sigmask, NULL, 0);
326 return (ret == 0 ? EJUSTRETURN : ret);
330 freebsd32_swapcontext(struct thread *td, struct freebsd32_swapcontext_args *uap)
335 if (uap->oucp == NULL || uap->ucp == NULL)
338 get_mcontext32(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
339 PROC_LOCK(td->td_proc);
340 uc.uc_sigmask = td->td_sigmask;
341 PROC_UNLOCK(td->td_proc);
342 ret = copyout(&uc, uap->oucp, UC32_COPY_SIZE);
344 ret = copyin(uap->ucp, &uc, UC32_COPY_SIZE);
346 ret = set_mcontext32(td, &uc.uc_mcontext);
348 kern_sigprocmask(td, SIG_SETMASK,
349 &uc.uc_sigmask, NULL, 0);
354 return (ret == 0 ? EJUSTRETURN : ret);
357 #define UCONTEXT_MAGIC 0xACEDBADE
360 * Send an interrupt to process.
362 * Stack is set up to allow sigcode stored
363 * at top to call routine, followed by kcall
364 * to sigreturn routine below. After sigreturn
365 * resets the signal mask, the stack, and the
366 * frame pointer, it returns to the user
370 freebsd32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
374 struct fpreg32 fpregs;
377 struct sigframe32 sf, *sfp;
384 PROC_LOCK_ASSERT(p, MA_OWNED);
385 sig = ksi->ksi_signo;
387 mtx_assert(&psp->ps_mtx, MA_OWNED);
389 fill_regs32(td, ®s);
390 oonstack = sigonstack(td->td_frame->sp);
392 /* save user context */
393 bzero(&sf, sizeof sf);
394 sf.sf_uc.uc_sigmask = *mask;
395 sf.sf_uc.uc_stack.ss_sp = (int32_t)(intptr_t)td->td_sigstk.ss_sp;
396 sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
397 sf.sf_uc.uc_stack.ss_flags = td->td_sigstk.ss_flags;
398 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
399 sf.sf_uc.uc_mcontext.mc_pc = regs.r_regs[PC];
400 sf.sf_uc.uc_mcontext.mullo = regs.r_regs[MULLO];
401 sf.sf_uc.uc_mcontext.mulhi = regs.r_regs[MULHI];
402 sf.sf_uc.uc_mcontext.mc_tls = (int32_t)(intptr_t)td->td_md.md_tls;
403 sf.sf_uc.uc_mcontext.mc_regs[0] = UCONTEXT_MAGIC; /* magic number */
404 for (i = 1; i < 32; i++)
405 sf.sf_uc.uc_mcontext.mc_regs[i] = regs.r_regs[i];
406 sf.sf_uc.uc_mcontext.mc_fpused = td->td_md.md_flags & MDTD_FPUSED;
407 if (sf.sf_uc.uc_mcontext.mc_fpused) {
408 /* if FPU has current state, save it first */
409 if (td == PCPU_GET(fpcurthread))
410 MipsSaveCurFPState(td);
411 fill_fpregs32(td, &fpregs);
412 for (i = 0; i < 33; i++)
413 sf.sf_uc.uc_mcontext.mc_fpregs[i] = fpregs.r_regs[i];
416 /* Allocate and validate space for the signal handler context. */
417 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
418 SIGISMEMBER(psp->ps_sigonstack, sig)) {
419 sfp = (struct sigframe32 *)(((uintptr_t)td->td_sigstk.ss_sp +
420 td->td_sigstk.ss_size - sizeof(struct sigframe32))
421 & ~(sizeof(__int64_t) - 1));
423 sfp = (struct sigframe32 *)((vm_offset_t)(td->td_frame->sp -
424 sizeof(struct sigframe32)) & ~(sizeof(__int64_t) - 1));
426 /* Build the argument list for the signal handler. */
427 td->td_frame->a0 = sig;
428 td->td_frame->a2 = (register_t)(intptr_t)&sfp->sf_uc;
429 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
430 /* Signal handler installed with SA_SIGINFO. */
431 td->td_frame->a1 = (register_t)(intptr_t)&sfp->sf_si;
432 /* sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher; */
434 /* fill siginfo structure */
435 sf.sf_si.si_signo = sig;
436 sf.sf_si.si_code = ksi->ksi_code;
437 sf.sf_si.si_addr = td->td_frame->badvaddr;
439 /* Old FreeBSD-style arguments. */
440 td->td_frame->a1 = ksi->ksi_code;
441 td->td_frame->a3 = td->td_frame->badvaddr;
442 /* sf.sf_ahu.sf_handler = catcher; */
445 mtx_unlock(&psp->ps_mtx);
449 * Copy the sigframe out to the user's stack.
451 if (copyout(&sf, sfp, sizeof(struct sigframe32)) != 0) {
453 * Something is wrong with the stack pointer.
454 * ...Kill the process.
460 td->td_frame->pc = (register_t)(intptr_t)catcher;
461 td->td_frame->t9 = (register_t)(intptr_t)catcher;
462 td->td_frame->sp = (register_t)(intptr_t)sfp;
464 * Signal trampoline code is at base of user stack.
466 td->td_frame->ra = (register_t)(intptr_t)FREEBSD32_PS_STRINGS - *(p->p_sysent->sv_szsigcode);
468 mtx_lock(&psp->ps_mtx);
472 freebsd32_sysarch(struct thread *td, struct freebsd32_sysarch_args *uap)
479 td->td_md.md_tls = (void *)(intptr_t)uap->parms;
482 tlsbase = (int32_t)(intptr_t)td->td_md.md_tls;
483 error = copyout(&tlsbase, uap->parms, sizeof(tlsbase));
492 elf32_dump_thread(struct thread *td __unused, void *dst __unused,
493 size_t *off __unused)