2 * Copyright (c) 2018 Olivier Houchard
3 * Copyright (c) 2017 Nuxi, https://nuxi.nl/
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/param.h>
31 #include <sys/mutex.h>
32 #include <sys/syscallsubr.h>
34 #include <sys/sysctl.h>
35 #include <sys/sysent.h>
36 #include <sys/sysproto.h>
37 #include <machine/armreg.h>
38 #include <machine/pcb.h>
40 #include <machine/vfp.h>
42 #include <compat/freebsd32/freebsd32_proto.h>
43 #include <compat/freebsd32/freebsd32_signal.h>
46 #include <vm/vm_param.h>
48 #include <vm/vm_map.h>
50 _Static_assert(sizeof(mcontext32_t) == 208, "mcontext32_t size incorrect");
51 _Static_assert(sizeof(ucontext32_t) == 260, "ucontext32_t size incorrect");
52 _Static_assert(sizeof(struct __siginfo32) == 64,
53 "struct __siginfo32 size incorrect");
55 extern void freebsd32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);
57 SYSCTL_NODE(_compat, OID_AUTO, arm, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
61 * The first two fields of a ucontext_t are the signal mask and the machine
62 * context. The next field is uc_link; we want to avoid destroying the link
63 * when copying out contexts.
65 #define UC32_COPY_SIZE offsetof(ucontext32_t, uc_link)
68 * Stubs for machine dependent 32-bits system calls.
72 freebsd32_sysarch(struct thread *td, struct freebsd32_sysarch_args *uap)
76 #define ARM_SYNC_ICACHE 0
77 #define ARM_DRAIN_WRITEBUF 1
80 #define ARM_GET_VFPSTATE 4
84 WRITE_SPECIALREG(tpidr_el0, uap->parms);
85 WRITE_SPECIALREG(tpidrro_el0, uap->parms);
94 if ((error = copyin(uap->parms, &args, sizeof(args))) != 0)
96 if ((uint64_t)args.addr + (uint64_t)args.size > 0xffffffff)
98 cpu_icache_sync_range_checked(
99 (void *)(uintptr_t)args.addr, args.size);
102 case ARM_GET_VFPSTATE:
104 mcontext32_vfp_t mcontext_vfp;
107 uint32_t mc_vfp_size;
110 if ((error = copyin(uap->parms, &args, sizeof(args))) != 0)
112 if (args.mc_vfp_size != sizeof(mcontext_vfp))
115 get_fpcontext32(td, &mcontext_vfp);
117 bzero(&mcontext_vfp, sizeof(mcontext_vfp));
119 error = copyout(&mcontext_vfp,
120 (void *)(uintptr_t)args.mc_vfp,
121 sizeof(mcontext_vfp));
131 get_fpcontext32(struct thread *td, mcontext32_vfp_t *mcp)
136 KASSERT(td == curthread || TD_IS_SUSPENDED(td) ||
137 P_SHOULDSTOP(td->td_proc),
138 ("not suspended thread %p", td));
140 memset(mcp, 0, sizeof(*mcp));
143 if ((pcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
145 * If we have just been running VFP instructions we will
146 * need to save the state to memcpy it below.
149 vfp_save_state(td, pcb);
151 KASSERT(pcb->pcb_fpusaved == &pcb->pcb_fpustate,
152 ("Called get_fpcontext32 while the kernel is using the VFP"));
153 KASSERT((pcb->pcb_fpflags & ~PCB_FP_USERMASK) == 0,
154 ("Non-userspace FPU flags set in get_fpcontext32"));
155 for (i = 0; i < 16; i++) {
156 uint64_t *tmpreg = (uint64_t *)&pcb->pcb_fpustate.vfp_regs[i];
158 mcp->mcv_reg[i * 2] = tmpreg[0];
159 mcp->mcv_reg[i * 2 + 1] = tmpreg[1];
161 mcp->mcv_fpscr = VFP_FPSCR_FROM_SRCR(pcb->pcb_fpustate.vfp_fpcr,
162 pcb->pcb_fpustate.vfp_fpsr);
167 set_fpcontext32(struct thread *td, mcontext32_vfp_t *mcp)
176 for (i = 0; i < 16; i++) {
177 uint64_t *tmpreg = (uint64_t *)&pcb->pcb_fpustate.vfp_regs[i];
179 tmpreg[0] = mcp->mcv_reg[i * 2];
180 tmpreg[1] = mcp->mcv_reg[i * 2 + 1];
182 pcb->pcb_fpustate.vfp_fpsr = VFP_FPSR_FROM_FPSCR(mcp->mcv_fpscr);
183 pcb->pcb_fpustate.vfp_fpcr = VFP_FPSR_FROM_FPSCR(mcp->mcv_fpscr);
189 get_mcontext32(struct thread *td, mcontext32_t *mcp, int flags)
191 struct trapframe *tf;
196 if ((flags & GET_MC_CLEAR_RET) != 0) {
197 mcp->mc_gregset[0] = 0;
198 mcp->mc_gregset[16] = tf->tf_spsr & ~PSR_C;
200 mcp->mc_gregset[0] = tf->tf_x[0];
201 mcp->mc_gregset[16] = tf->tf_spsr;
203 for (i = 1; i < 15; i++)
204 mcp->mc_gregset[i] = tf->tf_x[i];
205 mcp->mc_gregset[15] = tf->tf_elr;
207 mcp->mc_vfp_size = 0;
210 memset(mcp->mc_spare, 0, sizeof(mcp->mc_spare));
214 set_mcontext32(struct thread *td, mcontext32_t *mcp)
216 struct trapframe *tf;
217 mcontext32_vfp_t mc_vfp;
223 spsr = mcp->mc_gregset[16];
225 * There is no PSR_SS in the 32-bit kernel so ignore it if it's set
226 * as we will set it later if needed.
228 if ((spsr & ~(PSR_SETTABLE_32 | PSR_SS)) !=
229 (tf->tf_spsr & ~(PSR_SETTABLE_32 | PSR_SS)))
232 spsr &= PSR_SETTABLE_32;
233 spsr |= tf->tf_spsr & ~PSR_SETTABLE_32;
235 if ((td->td_dbgflags & TDB_STEP) != 0) {
237 td->td_pcb->pcb_flags |= PCB_SINGLE_STEP;
238 WRITE_SPECIALREG(mdscr_el1,
239 READ_SPECIALREG(mdscr_el1) | MDSCR_SS);
242 for (i = 0; i < 15; i++)
243 tf->tf_x[i] = mcp->mc_gregset[i];
244 tf->tf_elr = mcp->mc_gregset[15];
247 if (mcp->mc_vfp_size == sizeof(mc_vfp) && mcp->mc_vfp_ptr != 0) {
248 if (copyin((void *)(uintptr_t)mcp->mc_vfp_ptr, &mc_vfp,
249 sizeof(mc_vfp)) != 0)
251 set_fpcontext32(td, &mc_vfp);
258 #define UC_COPY_SIZE offsetof(ucontext32_t, uc_link)
261 freebsd32_getcontext(struct thread *td, struct freebsd32_getcontext_args *uap)
266 if (uap->ucp == NULL)
269 memset(&uc, 0, sizeof(uc));
270 get_mcontext32(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
271 PROC_LOCK(td->td_proc);
272 uc.uc_sigmask = td->td_sigmask;
273 PROC_UNLOCK(td->td_proc);
274 ret = copyout(&uc, uap->ucp, UC_COPY_SIZE);
280 freebsd32_setcontext(struct thread *td, struct freebsd32_setcontext_args *uap)
285 if (uap->ucp == NULL)
288 ret = copyin(uap->ucp, &uc, UC_COPY_SIZE);
290 ret = set_mcontext32(td, &uc.uc_mcontext);
292 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask,
300 freebsd32_sigreturn(struct thread *td, struct freebsd32_sigreturn_args *uap)
307 if (copyin(uap->sigcntxp, &uc, sizeof(uc)))
309 error = set_mcontext32(td, &uc.uc_mcontext);
313 /* Restore signal mask. */
314 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
316 return (EJUSTRETURN);
321 freebsd32_swapcontext(struct thread *td, struct freebsd32_swapcontext_args *uap)
326 if (uap->oucp == NULL || uap->ucp == NULL)
329 bzero(&uc, sizeof(uc));
330 get_mcontext32(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
331 PROC_LOCK(td->td_proc);
332 uc.uc_sigmask = td->td_sigmask;
333 PROC_UNLOCK(td->td_proc);
334 ret = copyout(&uc, uap->oucp, UC32_COPY_SIZE);
336 ret = copyin(uap->ucp, &uc, UC32_COPY_SIZE);
338 ret = set_mcontext32(td, &uc.uc_mcontext);
339 kern_sigprocmask(td, SIG_SETMASK,
340 &uc.uc_sigmask, NULL, 0);
348 freebsd32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
352 struct trapframe *tf;
353 struct sigframe32 *fp, frame;
355 struct __siginfo32 siginfo;
356 struct sysentvec *sysent;
360 siginfo_to_siginfo32(&ksi->ksi_info, &siginfo);
363 PROC_LOCK_ASSERT(p, MA_OWNED);
364 sig = ksi->ksi_signo;
366 mtx_assert(&psp->ps_mtx, MA_OWNED);
368 onstack = sigonstack(tf->tf_x[13]);
370 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
373 /* Allocate and validate space for the signal handler context. */
374 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !(onstack) &&
375 SIGISMEMBER(psp->ps_sigonstack, sig)) {
376 fp = (struct sigframe32 *)((uintptr_t)td->td_sigstk.ss_sp +
377 td->td_sigstk.ss_size);
378 #if defined(COMPAT_43)
379 td->td_sigstk.ss_flags |= SS_ONSTACK;
382 fp = (struct sigframe32 *)td->td_frame->tf_x[13];
384 /* make room on the stack */
387 /* make the stack aligned */
388 fp = (struct sigframe32 *)((unsigned long)(fp) &~ (8 - 1));
389 /* Populate the siginfo frame. */
390 get_mcontext32(td, &frame.sf_uc.uc_mcontext, 0);
392 get_fpcontext32(td, &frame.sf_vfp);
393 frame.sf_uc.uc_mcontext.mc_vfp_size = sizeof(fp->sf_vfp);
394 frame.sf_uc.uc_mcontext.mc_vfp_ptr = (uint32_t)(uintptr_t)&fp->sf_vfp;
396 frame.sf_uc.uc_mcontext.mc_vfp_size = 0;
397 frame.sf_uc.uc_mcontext.mc_vfp_ptr = (uint32_t)NULL;
399 frame.sf_si = siginfo;
400 frame.sf_uc.uc_sigmask = *mask;
401 frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK )
402 ? ((onstack) ? SS_ONSTACK : 0) : SS_DISABLE;
403 frame.sf_uc.uc_stack.ss_sp = (uintptr_t)td->td_sigstk.ss_sp;
404 frame.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
406 mtx_unlock(&psp->ps_mtx);
407 PROC_UNLOCK(td->td_proc);
409 /* Copy the sigframe out to the user's stack. */
410 if (copyout(&frame, fp, sizeof(*fp)) != 0) {
411 /* Process has trashed its stack. Kill it. */
412 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
418 * Build context to run handler in. We invoke the handler
419 * directly, only returning via the trampoline. Note the
420 * trampoline version numbers are coordinated with machine-
421 * dependent code in libc.
425 tf->tf_x[1] = (register_t)&fp->sf_si;
426 tf->tf_x[2] = (register_t)&fp->sf_uc;
428 /* the trampoline uses r5 as the uc address */
429 tf->tf_x[5] = (register_t)&fp->sf_uc;
430 tf->tf_elr = (register_t)catcher;
431 tf->tf_x[13] = (register_t)fp;
432 sysent = p->p_sysent;
434 tf->tf_x[14] = (register_t)PROC_SIGCODE(p);
436 tf->tf_x[14] = (register_t)(PROC_PS_STRINGS(p) -
437 *(sysent->sv_szsigcode));
438 /* Set the mode to enter in the signal handler */
439 if ((register_t)catcher & 1)
440 tf->tf_spsr |= PSR_T;
442 tf->tf_spsr &= ~PSR_T;
444 /* Clear the single step flag while in the signal handler */
445 if ((td->td_pcb->pcb_flags & PCB_SINGLE_STEP) != 0) {
446 td->td_pcb->pcb_flags &= ~PCB_SINGLE_STEP;
447 WRITE_SPECIALREG(mdscr_el1,
448 READ_SPECIALREG(mdscr_el1) & ~MDSCR_SS);
452 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_x[14],
456 mtx_lock(&psp->ps_mtx);
462 * Mirror the osigreturn definition in kern_sig.c for !i386 platforms. This
463 * mirrors what's connected to the FreeBSD/arm syscall.
466 ofreebsd32_sigreturn(struct thread *td, struct ofreebsd32_sigreturn_args *uap)
469 return (nosys(td, (struct nosys_args *)uap));