1/*-
2 * Copyright (c) 2012 Juli Mallett <jmallett@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: stable/11/sys/mips/mips/freebsd32_machdep.c 341165 2018-11-28 21:19:58Z vangyzen $
27 */
28
29/*
30 * Based on nwhitehorn's COMPAT_FREEBSD32 support code for PowerPC64.
31 */
32
33#include "opt_compat.h"
34
35#define __ELF_WORD_SIZE 32
36
37#include <sys/types.h>
38#include <sys/param.h>
39#include <sys/kernel.h>
40#include <sys/systm.h>
41#include <sys/sysent.h>
42#include <sys/exec.h>
43#include <sys/imgact.h>
44#include <sys/malloc.h>
45#include <sys/proc.h>
46#include <sys/namei.h>
47#include <sys/fcntl.h>
48#include <sys/sysent.h>
49#include <sys/imgact_elf.h>
50#include <sys/syscall.h>
51#include <sys/syscallsubr.h>
52#include <sys/sysproto.h>
53#include <sys/signalvar.h>
54#include <sys/vnode.h>
55#include <sys/linker.h>
56
57#include <vm/vm.h>
58#include <vm/vm_param.h>
59
60#include <machine/md_var.h>
61#include <machine/reg.h>
62#include <machine/sigframe.h>
63#include <machine/sysarch.h>
64
65#include <compat/freebsd32/freebsd32_signal.h>
66#include <compat/freebsd32/freebsd32_util.h>
67#include <compat/freebsd32/freebsd32_proto.h>
68
69static void freebsd32_exec_setregs(struct thread *, struct image_params *, u_long);
70static int get_mcontext32(struct thread *, mcontext32_t *, int);
71static int set_mcontext32(struct thread *, mcontext32_t *);
72static void freebsd32_sendsig(sig_t, ksiginfo_t *, sigset_t *);
73
74extern const char *freebsd32_syscallnames[];
75
76struct sysentvec elf32_freebsd_sysvec = {
77	.sv_size	= SYS_MAXSYSCALL,
78	.sv_table	= freebsd32_sysent,
79	.sv_mask	= 0,
80	.sv_errsize	= 0,
81	.sv_errtbl	= NULL,
82	.sv_transtrap	= NULL,
83	.sv_fixup	= __elfN(freebsd_fixup),
84	.sv_sendsig	= freebsd32_sendsig,
85	.sv_sigcode	= sigcode32,
86	.sv_szsigcode	= &szsigcode32,
87	.sv_name	= "FreeBSD ELF32",
88	.sv_coredump	= __elfN(coredump),
89	.sv_imgact_try	= NULL,
90	.sv_minsigstksz	= MINSIGSTKSZ,
91	.sv_pagesize	= PAGE_SIZE,
92	.sv_minuser	= VM_MIN_ADDRESS,
93	.sv_maxuser	= ((vm_offset_t)0x80000000),
94	.sv_usrstack	= FREEBSD32_USRSTACK,
95	.sv_psstrings	= FREEBSD32_PS_STRINGS,
96	.sv_stackprot	= VM_PROT_ALL,
97	.sv_copyout_strings = freebsd32_copyout_strings,
98	.sv_setregs	= freebsd32_exec_setregs,
99	.sv_fixlimit	= NULL,
100	.sv_maxssiz	= NULL,
101	.sv_flags	= SV_ABI_FREEBSD | SV_ILP32,
102	.sv_set_syscall_retval = cpu_set_syscall_retval,
103	.sv_fetch_syscall_args = cpu_fetch_syscall_args,
104	.sv_syscallnames = freebsd32_syscallnames,
105	.sv_schedtail	= NULL,
106	.sv_thread_detach = NULL,
107	.sv_trap	= NULL,
108};
109INIT_SYSENTVEC(elf32_sysvec, &elf32_freebsd_sysvec);
110
111static Elf32_Brandinfo freebsd_brand_info = {
112	.brand		= ELFOSABI_FREEBSD,
113	.machine	= EM_MIPS,
114	.compat_3_brand	= "FreeBSD",
115	.emul_path	= NULL,
116	.interp_path	= "/libexec/ld-elf.so.1",
117	.sysvec		= &elf32_freebsd_sysvec,
118	.interp_newpath	= "/libexec/ld-elf32.so.1",
119	.flags		= 0
120};
121
122SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST,
123    (sysinit_cfunc_t) elf32_insert_brand_entry,
124    &freebsd_brand_info);
125
126static void
127freebsd32_exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
128{
129	exec_setregs(td, imgp, stack);
130
131	/*
132	 * See comment in exec_setregs about running 32-bit binaries with 64-bit
133	 * registers.
134	 */
135	td->td_frame->sp -= 65536;
136
137	/*
138	 * Clear extended address space bit for userland.
139	 */
140	td->td_frame->sr &= ~MIPS_SR_UX;
141}
142
143int
144set_regs32(struct thread *td, struct reg32 *regs)
145{
146	struct reg r;
147	unsigned i;
148
149	for (i = 0; i < NUMSAVEREGS; i++)
150		r.r_regs[i] = regs->r_regs[i];
151
152	return (set_regs(td, &r));
153}
154
155int
156fill_regs32(struct thread *td, struct reg32 *regs)
157{
158	struct reg r;
159	unsigned i;
160	int error;
161
162	error = fill_regs(td, &r);
163	if (error != 0)
164		return (error);
165
166	for (i = 0; i < NUMSAVEREGS; i++)
167		regs->r_regs[i] = r.r_regs[i];
168
169	return (0);
170}
171
172int
173set_fpregs32(struct thread *td, struct fpreg32 *fpregs)
174{
175	struct fpreg fp;
176	unsigned i;
177
178	for (i = 0; i < NUMFPREGS; i++)
179		fp.r_regs[i] = fpregs->r_regs[i];
180
181	return (set_fpregs(td, &fp));
182}
183
184int
185fill_fpregs32(struct thread *td, struct fpreg32 *fpregs)
186{
187	struct fpreg fp;
188	unsigned i;
189	int error;
190
191	error = fill_fpregs(td, &fp);
192	if (error != 0)
193		return (error);
194
195	for (i = 0; i < NUMFPREGS; i++)
196		fpregs->r_regs[i] = fp.r_regs[i];
197
198	return (0);
199}
200
201static int
202get_mcontext32(struct thread *td, mcontext32_t *mcp, int flags)
203{
204	mcontext_t mcp64;
205	unsigned i;
206	int error;
207
208	error = get_mcontext(td, &mcp64, flags);
209	if (error != 0)
210		return (error);
211
212	mcp->mc_onstack = mcp64.mc_onstack;
213	mcp->mc_pc = mcp64.mc_pc;
214	for (i = 0; i < 32; i++)
215		mcp->mc_regs[i] = mcp64.mc_regs[i];
216	mcp->sr = mcp64.sr;
217	mcp->mullo = mcp64.mullo;
218	mcp->mulhi = mcp64.mulhi;
219	mcp->mc_fpused = mcp64.mc_fpused;
220	for (i = 0; i < 33; i++)
221		mcp->mc_fpregs[i] = mcp64.mc_fpregs[i];
222	mcp->mc_fpc_eir = mcp64.mc_fpc_eir;
223	mcp->mc_tls = (int32_t)(intptr_t)mcp64.mc_tls;
224
225	return (0);
226}
227
228static int
229set_mcontext32(struct thread *td, mcontext32_t *mcp)
230{
231	mcontext_t mcp64;
232	unsigned i;
233
234	mcp64.mc_onstack = mcp->mc_onstack;
235	mcp64.mc_pc = mcp->mc_pc;
236	for (i = 0; i < 32; i++)
237		mcp64.mc_regs[i] = mcp->mc_regs[i];
238	mcp64.sr = mcp->sr;
239	mcp64.mullo = mcp->mullo;
240	mcp64.mulhi = mcp->mulhi;
241	mcp64.mc_fpused = mcp->mc_fpused;
242	for (i = 0; i < 33; i++)
243		mcp64.mc_fpregs[i] = mcp->mc_fpregs[i];
244	mcp64.mc_fpc_eir = mcp->mc_fpc_eir;
245	mcp64.mc_tls = (void *)(intptr_t)mcp->mc_tls;
246
247	return (set_mcontext(td, &mcp64));
248}
249
250int
251freebsd32_sigreturn(struct thread *td, struct freebsd32_sigreturn_args *uap)
252{
253	ucontext32_t uc;
254	int error;
255
256	CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
257
258	if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
259		CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
260		return (EFAULT);
261	}
262
263	error = set_mcontext32(td, &uc.uc_mcontext);
264	if (error != 0)
265		return (error);
266
267	kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
268
269#if 0
270	CTR3(KTR_SIG, "sigreturn: return td=%p pc=%#x sp=%#x",
271	     td, uc.uc_mcontext.mc_srr0, uc.uc_mcontext.mc_gpr[1]);
272#endif
273
274	return (EJUSTRETURN);
275}
276
277/*
278 * The first two fields of a ucontext_t are the signal mask and the machine
279 * context.  The next field is uc_link; we want to avoid destroying the link
280 * when copying out contexts.
281 */
282#define	UC32_COPY_SIZE	offsetof(ucontext32_t, uc_link)
283
284int
285freebsd32_getcontext(struct thread *td, struct freebsd32_getcontext_args *uap)
286{
287	ucontext32_t uc;
288	int ret;
289
290	if (uap->ucp == NULL)
291		ret = EINVAL;
292	else {
293		bzero(&uc, sizeof(uc));
294		get_mcontext32(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
295		PROC_LOCK(td->td_proc);
296		uc.uc_sigmask = td->td_sigmask;
297		PROC_UNLOCK(td->td_proc);
298		ret = copyout(&uc, uap->ucp, UC32_COPY_SIZE);
299	}
300	return (ret);
301}
302
303int
304freebsd32_setcontext(struct thread *td, struct freebsd32_setcontext_args *uap)
305{
306	ucontext32_t uc;
307	int ret;
308
309	if (uap->ucp == NULL)
310		ret = EINVAL;
311	else {
312		ret = copyin(uap->ucp, &uc, UC32_COPY_SIZE);
313		if (ret == 0) {
314			ret = set_mcontext32(td, &uc.uc_mcontext);
315			if (ret == 0) {
316				kern_sigprocmask(td, SIG_SETMASK,
317				    &uc.uc_sigmask, NULL, 0);
318			}
319		}
320	}
321	return (ret == 0 ? EJUSTRETURN : ret);
322}
323
324int
325freebsd32_swapcontext(struct thread *td, struct freebsd32_swapcontext_args *uap)
326{
327	ucontext32_t uc;
328	int ret;
329
330	if (uap->oucp == NULL || uap->ucp == NULL)
331		ret = EINVAL;
332	else {
333		bzero(&uc, sizeof(uc));
334		get_mcontext32(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
335		PROC_LOCK(td->td_proc);
336		uc.uc_sigmask = td->td_sigmask;
337		PROC_UNLOCK(td->td_proc);
338		ret = copyout(&uc, uap->oucp, UC32_COPY_SIZE);
339		if (ret == 0) {
340			ret = copyin(uap->ucp, &uc, UC32_COPY_SIZE);
341			if (ret == 0) {
342				ret = set_mcontext32(td, &uc.uc_mcontext);
343				if (ret == 0) {
344					kern_sigprocmask(td, SIG_SETMASK,
345					    &uc.uc_sigmask, NULL, 0);
346				}
347			}
348		}
349	}
350	return (ret == 0 ? EJUSTRETURN : ret);
351}
352
353#define	UCONTEXT_MAGIC	0xACEDBADE
354
355/*
356 * Send an interrupt to process.
357 *
358 * Stack is set up to allow sigcode stored
359 * at top to call routine, followed by kcall
360 * to sigreturn routine below.	After sigreturn
361 * resets the signal mask, the stack, and the
362 * frame pointer, it returns to the user
363 * specified pc, psl.
364 */
365static void
366freebsd32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
367{
368	struct proc *p;
369	struct thread *td;
370	struct fpreg32 fpregs;
371	struct reg32 regs;
372	struct sigacts *psp;
373	struct sigframe32 sf, *sfp;
374	int sig;
375	int oonstack;
376	unsigned i;
377
378	td = curthread;
379	p = td->td_proc;
380	PROC_LOCK_ASSERT(p, MA_OWNED);
381	sig = ksi->ksi_signo;
382	psp = p->p_sigacts;
383	mtx_assert(&psp->ps_mtx, MA_OWNED);
384
385	fill_regs32(td, &regs);
386	oonstack = sigonstack(td->td_frame->sp);
387
388	/* save user context */
389	bzero(&sf, sizeof sf);
390	sf.sf_uc.uc_sigmask = *mask;
391	sf.sf_uc.uc_stack.ss_sp = (int32_t)(intptr_t)td->td_sigstk.ss_sp;
392	sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
393	sf.sf_uc.uc_stack.ss_flags = td->td_sigstk.ss_flags;
394	sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
395	sf.sf_uc.uc_mcontext.mc_pc = regs.r_regs[PC];
396	sf.sf_uc.uc_mcontext.mullo = regs.r_regs[MULLO];
397	sf.sf_uc.uc_mcontext.mulhi = regs.r_regs[MULHI];
398	sf.sf_uc.uc_mcontext.mc_tls = (int32_t)(intptr_t)td->td_md.md_tls;
399	sf.sf_uc.uc_mcontext.mc_regs[0] = UCONTEXT_MAGIC;  /* magic number */
400	for (i = 1; i < 32; i++)
401		sf.sf_uc.uc_mcontext.mc_regs[i] = regs.r_regs[i];
402	sf.sf_uc.uc_mcontext.mc_fpused = td->td_md.md_flags & MDTD_FPUSED;
403	if (sf.sf_uc.uc_mcontext.mc_fpused) {
404		/* if FPU has current state, save it first */
405		if (td == PCPU_GET(fpcurthread))
406			MipsSaveCurFPState(td);
407		fill_fpregs32(td, &fpregs);
408		for (i = 0; i < 33; i++)
409			sf.sf_uc.uc_mcontext.mc_fpregs[i] = fpregs.r_regs[i];
410	}
411
412	/* Allocate and validate space for the signal handler context. */
413	if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
414	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
415		sfp = (struct sigframe32 *)(((uintptr_t)td->td_sigstk.ss_sp +
416		    td->td_sigstk.ss_size - sizeof(struct sigframe32))
417		    & ~(sizeof(__int64_t) - 1));
418	} else
419		sfp = (struct sigframe32 *)((vm_offset_t)(td->td_frame->sp -
420		    sizeof(struct sigframe32)) & ~(sizeof(__int64_t) - 1));
421
422	/* Build the argument list for the signal handler. */
423	td->td_frame->a0 = sig;
424	td->td_frame->a2 = (register_t)(intptr_t)&sfp->sf_uc;
425	if (SIGISMEMBER(psp->ps_siginfo, sig)) {
426		/* Signal handler installed with SA_SIGINFO. */
427		td->td_frame->a1 = (register_t)(intptr_t)&sfp->sf_si;
428		/* sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher; */
429
430		/* fill siginfo structure */
431		sf.sf_si.si_signo = sig;
432		sf.sf_si.si_code = ksi->ksi_code;
433		sf.sf_si.si_addr = td->td_frame->badvaddr;
434	} else {
435		/* Old FreeBSD-style arguments. */
436		td->td_frame->a1 = ksi->ksi_code;
437		td->td_frame->a3 = td->td_frame->badvaddr;
438		/* sf.sf_ahu.sf_handler = catcher; */
439	}
440
441	mtx_unlock(&psp->ps_mtx);
442	PROC_UNLOCK(p);
443
444	/*
445	 * Copy the sigframe out to the user's stack.
446	 */
447	if (copyout(&sf, sfp, sizeof(struct sigframe32)) != 0) {
448		/*
449		 * Something is wrong with the stack pointer.
450		 * ...Kill the process.
451		 */
452		PROC_LOCK(p);
453		sigexit(td, SIGILL);
454	}
455
456	td->td_frame->pc = (register_t)(intptr_t)catcher;
457	td->td_frame->t9 = (register_t)(intptr_t)catcher;
458	td->td_frame->sp = (register_t)(intptr_t)sfp;
459	/*
460	 * Signal trampoline code is at base of user stack.
461	 */
462	td->td_frame->ra = (register_t)(intptr_t)FREEBSD32_PS_STRINGS - *(p->p_sysent->sv_szsigcode);
463	PROC_LOCK(p);
464	mtx_lock(&psp->ps_mtx);
465}
466
467int
468freebsd32_sysarch(struct thread *td, struct freebsd32_sysarch_args *uap)
469{
470	int error;
471	int32_t tlsbase;
472
473	switch (uap->op) {
474	case MIPS_SET_TLS:
475		td->td_md.md_tls = (void *)(intptr_t)uap->parms;
476		return (0);
477	case MIPS_GET_TLS:
478		tlsbase = (int32_t)(intptr_t)td->td_md.md_tls;
479		error = copyout(&tlsbase, uap->parms, sizeof(tlsbase));
480		return (error);
481	default:
482		break;
483	}
484	return (EINVAL);
485}
486
487void
488elf32_dump_thread(struct thread *td __unused, void *dst __unused,
489    size_t *off __unused)
490{
491}
492