2 * Copyright (c) 2014 Andrew Turner
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
34 #include <sys/imgact.h>
36 #include <sys/kernel.h>
38 #include <sys/limits.h>
40 #include <sys/mutex.h>
42 #include <sys/ptrace.h>
44 #include <sys/rwlock.h>
45 #include <sys/signalvar.h>
46 #include <sys/syscallsubr.h>
47 #include <sys/sysent.h>
48 #include <sys/sysproto.h>
49 #include <sys/ucontext.h>
52 #include <vm/vm_param.h>
54 #include <machine/armreg.h>
55 #include <machine/kdb.h>
56 #include <machine/md_var.h>
57 #include <machine/pcb.h>
60 #include <machine/vfp.h>
63 static void get_fpcontext(struct thread *td, mcontext_t *mcp);
64 static void set_fpcontext(struct thread *td, mcontext_t *mcp);
67 fill_regs(struct thread *td, struct reg *regs)
69 struct trapframe *frame;
72 regs->sp = frame->tf_sp;
73 regs->lr = frame->tf_lr;
74 regs->elr = frame->tf_elr;
75 regs->spsr = frame->tf_spsr;
77 memcpy(regs->x, frame->tf_x, sizeof(regs->x));
79 #ifdef COMPAT_FREEBSD32
81 * We may be called here for a 32bits process, if we're using a
82 * 64bits debugger. If so, put PC and SPSR where it expects it.
84 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
85 regs->x[15] = frame->tf_elr;
86 regs->x[16] = frame->tf_spsr;
93 set_regs(struct thread *td, struct reg *regs)
95 struct trapframe *frame;
98 frame->tf_sp = regs->sp;
99 frame->tf_lr = regs->lr;
101 memcpy(frame->tf_x, regs->x, sizeof(frame->tf_x));
103 #ifdef COMPAT_FREEBSD32
104 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
106 * We may be called for a 32bits process if we're using
107 * a 64bits debugger. If so, get PC and SPSR from where
110 frame->tf_elr = regs->x[15];
111 frame->tf_spsr &= ~PSR_SETTABLE_32;
112 frame->tf_spsr |= regs->x[16] & PSR_SETTABLE_32;
113 /* Don't allow userspace to ask to continue single stepping.
114 * The SPSR.SS field doesn't exist when the EL1 is AArch32.
115 * As the SPSR.DIT field has moved in its place don't
116 * allow userspace to set the SPSR.SS field.
121 frame->tf_elr = regs->elr;
122 frame->tf_spsr &= ~PSR_SETTABLE_64;
123 frame->tf_spsr |= regs->spsr & PSR_SETTABLE_64;
124 /* Enable single stepping if userspace asked fot it */
125 if ((frame->tf_spsr & PSR_SS) != 0) {
126 td->td_pcb->pcb_flags |= PCB_SINGLE_STEP;
128 WRITE_SPECIALREG(mdscr_el1,
129 READ_SPECIALREG(mdscr_el1) | MDSCR_SS);
137 fill_fpregs(struct thread *td, struct fpreg *regs)
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);
152 KASSERT(pcb->pcb_fpusaved == &pcb->pcb_fpustate,
153 ("Called fill_fpregs while the kernel is using the VFP"));
154 memcpy(regs->fp_q, pcb->pcb_fpustate.vfp_regs,
156 regs->fp_cr = pcb->pcb_fpustate.vfp_fpcr;
157 regs->fp_sr = pcb->pcb_fpustate.vfp_fpsr;
159 memset(regs, 0, sizeof(*regs));
165 set_fpregs(struct thread *td, struct fpreg *regs)
171 KASSERT(pcb->pcb_fpusaved == &pcb->pcb_fpustate,
172 ("Called set_fpregs while the kernel is using the VFP"));
173 memcpy(pcb->pcb_fpustate.vfp_regs, regs->fp_q, sizeof(regs->fp_q));
174 pcb->pcb_fpustate.vfp_fpcr = regs->fp_cr;
175 pcb->pcb_fpustate.vfp_fpsr = regs->fp_sr;
181 fill_dbregs(struct thread *td, struct dbreg *regs)
183 struct debug_monitor_state *monitor;
185 uint8_t debug_ver, nbkpts, nwtpts;
187 memset(regs, 0, sizeof(*regs));
189 extract_user_id_field(ID_AA64DFR0_EL1, ID_AA64DFR0_DebugVer_SHIFT,
191 extract_user_id_field(ID_AA64DFR0_EL1, ID_AA64DFR0_BRPs_SHIFT,
193 extract_user_id_field(ID_AA64DFR0_EL1, ID_AA64DFR0_WRPs_SHIFT,
197 * The BRPs field contains the number of breakpoints - 1. Armv8-A
198 * allows the hardware to provide 2-16 breakpoints so this won't
199 * overflow an 8 bit value. The same applies to the WRPs field.
204 regs->db_debug_ver = debug_ver;
205 regs->db_nbkpts = nbkpts;
206 regs->db_nwtpts = nwtpts;
208 monitor = &td->td_pcb->pcb_dbg_regs;
209 if ((monitor->dbg_flags & DBGMON_ENABLED) != 0) {
210 for (i = 0; i < nbkpts; i++) {
211 regs->db_breakregs[i].dbr_addr = monitor->dbg_bvr[i];
212 regs->db_breakregs[i].dbr_ctrl = monitor->dbg_bcr[i];
214 for (i = 0; i < nwtpts; i++) {
215 regs->db_watchregs[i].dbw_addr = monitor->dbg_wvr[i];
216 regs->db_watchregs[i].dbw_ctrl = monitor->dbg_wcr[i];
224 set_dbregs(struct thread *td, struct dbreg *regs)
226 struct debug_monitor_state *monitor;
232 monitor = &td->td_pcb->pcb_dbg_regs;
234 monitor->dbg_enable_count = 0;
236 for (i = 0; i < DBG_BRP_MAX; i++) {
237 addr = regs->db_breakregs[i].dbr_addr;
238 ctrl = regs->db_breakregs[i].dbr_ctrl;
241 * Don't let the user set a breakpoint on a kernel or
242 * non-canonical user address.
244 if (addr >= VM_MAXUSER_ADDRESS)
248 * The lowest 2 bits are ignored, so record the effective
251 addr = rounddown2(addr, 4);
254 * Some control fields are ignored, and other bits reserved.
255 * Only unlinked, address-matching breakpoints are supported.
257 * XXX: fields that appear unvalidated, such as BAS, have
258 * constrained undefined behaviour. If the user mis-programs
259 * these, there is no risk to the system.
261 ctrl &= DBGBCR_EN | DBGBCR_PMC | DBGBCR_BAS;
262 if ((ctrl & DBGBCR_EN) != 0) {
263 /* Only target EL0. */
264 if ((ctrl & DBGBCR_PMC) != DBGBCR_PMC_EL0)
267 monitor->dbg_enable_count++;
270 monitor->dbg_bvr[i] = addr;
271 monitor->dbg_bcr[i] = ctrl;
274 for (i = 0; i < DBG_WRP_MAX; i++) {
275 addr = regs->db_watchregs[i].dbw_addr;
276 ctrl = regs->db_watchregs[i].dbw_ctrl;
279 * Don't let the user set a watchpoint on a kernel or
280 * non-canonical user address.
282 if (addr >= VM_MAXUSER_ADDRESS)
286 * Some control fields are ignored, and other bits reserved.
287 * Only unlinked watchpoints are supported.
289 ctrl &= DBGWCR_EN | DBGWCR_PAC | DBGWCR_LSC | DBGWCR_BAS |
292 if ((ctrl & DBGWCR_EN) != 0) {
293 /* Only target EL0. */
294 if ((ctrl & DBGWCR_PAC) != DBGWCR_PAC_EL0)
297 /* Must set at least one of the load/store bits. */
298 if ((ctrl & DBGWCR_LSC) == 0)
302 * When specifying the address range with BAS, the MASK
303 * field must be zero.
305 if ((ctrl & DBGWCR_BAS) != DBGWCR_BAS &&
306 (ctrl & DBGWCR_MASK) != 0)
309 monitor->dbg_enable_count++;
311 monitor->dbg_wvr[i] = addr;
312 monitor->dbg_wcr[i] = ctrl;
315 if (monitor->dbg_enable_count > 0)
316 monitor->dbg_flags |= DBGMON_ENABLED;
321 #ifdef COMPAT_FREEBSD32
323 fill_regs32(struct thread *td, struct reg32 *regs)
326 struct trapframe *tf;
329 for (i = 0; i < 13; i++)
330 regs->r[i] = tf->tf_x[i];
331 /* For arm32, SP is r13 and LR is r14 */
332 regs->r_sp = tf->tf_x[13];
333 regs->r_lr = tf->tf_x[14];
334 regs->r_pc = tf->tf_elr;
335 regs->r_cpsr = tf->tf_spsr;
341 set_regs32(struct thread *td, struct reg32 *regs)
344 struct trapframe *tf;
347 for (i = 0; i < 13; i++)
348 tf->tf_x[i] = regs->r[i];
349 /* For arm 32, SP is r13 an LR is r14 */
350 tf->tf_x[13] = regs->r_sp;
351 tf->tf_x[14] = regs->r_lr;
352 tf->tf_elr = regs->r_pc;
353 tf->tf_spsr &= ~PSR_SETTABLE_32;
354 tf->tf_spsr |= regs->r_cpsr & PSR_SETTABLE_32;
359 /* XXX fill/set dbregs/fpregs are stubbed on 32-bit arm. */
361 fill_fpregs32(struct thread *td, struct fpreg32 *regs)
364 memset(regs, 0, sizeof(*regs));
369 set_fpregs32(struct thread *td, struct fpreg32 *regs)
376 fill_dbregs32(struct thread *td, struct dbreg32 *regs)
379 memset(regs, 0, sizeof(*regs));
384 set_dbregs32(struct thread *td, struct dbreg32 *regs)
392 exec_setregs(struct thread *td, struct image_params *imgp, uintptr_t stack)
394 struct trapframe *tf = td->td_frame;
395 struct pcb *pcb = td->td_pcb;
397 memset(tf, 0, sizeof(struct trapframe));
400 tf->tf_sp = STACKALIGN(stack);
401 tf->tf_lr = imgp->entry_addr;
402 tf->tf_elr = imgp->entry_addr;
404 td->td_pcb->pcb_tpidr_el0 = 0;
405 td->td_pcb->pcb_tpidrro_el0 = 0;
406 WRITE_SPECIALREG(tpidrro_el0, 0);
407 WRITE_SPECIALREG(tpidr_el0, 0);
410 vfp_reset_state(td, pcb);
414 * Clear debug register state. It is not applicable to the new process.
416 bzero(&pcb->pcb_dbg_regs, sizeof(pcb->pcb_dbg_regs));
419 /* Sanity check these are the same size, they will be memcpy'd to and from */
420 CTASSERT(sizeof(((struct trapframe *)0)->tf_x) ==
421 sizeof((struct gpregs *)0)->gp_x);
422 CTASSERT(sizeof(((struct trapframe *)0)->tf_x) ==
423 sizeof((struct reg *)0)->x);
426 get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
428 struct trapframe *tf = td->td_frame;
430 if (clear_ret & GET_MC_CLEAR_RET) {
431 mcp->mc_gpregs.gp_x[0] = 0;
432 mcp->mc_gpregs.gp_spsr = tf->tf_spsr & ~PSR_C;
434 mcp->mc_gpregs.gp_x[0] = tf->tf_x[0];
435 mcp->mc_gpregs.gp_spsr = tf->tf_spsr;
438 memcpy(&mcp->mc_gpregs.gp_x[1], &tf->tf_x[1],
439 sizeof(mcp->mc_gpregs.gp_x[1]) * (nitems(mcp->mc_gpregs.gp_x) - 1));
441 mcp->mc_gpregs.gp_sp = tf->tf_sp;
442 mcp->mc_gpregs.gp_lr = tf->tf_lr;
443 mcp->mc_gpregs.gp_elr = tf->tf_elr;
444 get_fpcontext(td, mcp);
450 set_mcontext(struct thread *td, mcontext_t *mcp)
452 struct trapframe *tf = td->td_frame;
455 spsr = mcp->mc_gpregs.gp_spsr;
456 if ((spsr & PSR_M_MASK) != PSR_M_EL0t ||
457 (spsr & PSR_AARCH32) != 0 ||
458 (spsr & PSR_DAIF) != (td->td_frame->tf_spsr & PSR_DAIF))
461 memcpy(tf->tf_x, mcp->mc_gpregs.gp_x, sizeof(tf->tf_x));
463 tf->tf_sp = mcp->mc_gpregs.gp_sp;
464 tf->tf_lr = mcp->mc_gpregs.gp_lr;
465 tf->tf_elr = mcp->mc_gpregs.gp_elr;
466 tf->tf_spsr = mcp->mc_gpregs.gp_spsr;
467 if ((tf->tf_spsr & PSR_SS) != 0) {
468 td->td_pcb->pcb_flags |= PCB_SINGLE_STEP;
470 WRITE_SPECIALREG(mdscr_el1,
471 READ_SPECIALREG(mdscr_el1) | MDSCR_SS);
474 set_fpcontext(td, mcp);
480 get_fpcontext(struct thread *td, mcontext_t *mcp)
485 MPASS(td == curthread);
487 curpcb = curthread->td_pcb;
488 if ((curpcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
490 * If we have just been running VFP instructions we will
491 * need to save the state to memcpy it below.
493 vfp_save_state(td, curpcb);
496 KASSERT(curpcb->pcb_fpusaved == &curpcb->pcb_fpustate,
497 ("Called get_fpcontext while the kernel is using the VFP"));
498 KASSERT((curpcb->pcb_fpflags & ~PCB_FP_USERMASK) == 0,
499 ("Non-userspace FPU flags set in get_fpcontext"));
500 memcpy(mcp->mc_fpregs.fp_q, curpcb->pcb_fpustate.vfp_regs,
501 sizeof(mcp->mc_fpregs.fp_q));
502 mcp->mc_fpregs.fp_cr = curpcb->pcb_fpustate.vfp_fpcr;
503 mcp->mc_fpregs.fp_sr = curpcb->pcb_fpustate.vfp_fpsr;
504 mcp->mc_fpregs.fp_flags = curpcb->pcb_fpflags;
505 mcp->mc_flags |= _MC_FP_VALID;
510 set_fpcontext(struct thread *td, mcontext_t *mcp)
515 MPASS(td == curthread);
516 if ((mcp->mc_flags & _MC_FP_VALID) != 0) {
517 curpcb = curthread->td_pcb;
520 * Discard any vfp state for the current thread, we
521 * are about to override it.
527 KASSERT(curpcb->pcb_fpusaved == &curpcb->pcb_fpustate,
528 ("Called set_fpcontext while the kernel is using the VFP"));
529 memcpy(curpcb->pcb_fpustate.vfp_regs, mcp->mc_fpregs.fp_q,
530 sizeof(mcp->mc_fpregs.fp_q));
531 curpcb->pcb_fpustate.vfp_fpcr = mcp->mc_fpregs.fp_cr;
532 curpcb->pcb_fpustate.vfp_fpsr = mcp->mc_fpregs.fp_sr;
533 curpcb->pcb_fpflags = mcp->mc_fpregs.fp_flags & PCB_FP_USERMASK;
539 sys_sigreturn(struct thread *td, struct sigreturn_args *uap)
544 if (copyin(uap->sigcntxp, &uc, sizeof(uc)))
547 error = set_mcontext(td, &uc.uc_mcontext);
551 /* Restore signal mask. */
552 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
554 return (EJUSTRETURN);
558 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
562 struct trapframe *tf;
563 struct sigframe *fp, frame;
569 PROC_LOCK_ASSERT(p, MA_OWNED);
571 sig = ksi->ksi_signo;
573 mtx_assert(&psp->ps_mtx, MA_OWNED);
576 onstack = sigonstack(tf->tf_sp);
578 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
581 /* Allocate and validate space for the signal handler context. */
582 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !onstack &&
583 SIGISMEMBER(psp->ps_sigonstack, sig)) {
584 fp = (struct sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
585 td->td_sigstk.ss_size);
586 #if defined(COMPAT_43)
587 td->td_sigstk.ss_flags |= SS_ONSTACK;
590 fp = (struct sigframe *)td->td_frame->tf_sp;
593 /* Make room, keeping the stack aligned */
595 fp = (struct sigframe *)STACKALIGN(fp);
597 /* Fill in the frame to copy out */
598 bzero(&frame, sizeof(frame));
599 get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
600 frame.sf_si = ksi->ksi_info;
601 frame.sf_uc.uc_sigmask = *mask;
602 frame.sf_uc.uc_stack = td->td_sigstk;
603 frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ?
604 (onstack ? SS_ONSTACK : 0) : SS_DISABLE;
605 mtx_unlock(&psp->ps_mtx);
606 PROC_UNLOCK(td->td_proc);
608 /* Copy the sigframe out to the user's stack. */
609 if (copyout(&frame, fp, sizeof(*fp)) != 0) {
610 /* Process has trashed its stack. Kill it. */
611 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
617 tf->tf_x[1] = (register_t)&fp->sf_si;
618 tf->tf_x[2] = (register_t)&fp->sf_uc;
620 tf->tf_elr = (register_t)catcher;
621 tf->tf_sp = (register_t)fp;
622 tf->tf_lr = (register_t)p->p_sysent->sv_sigcode_base;
624 /* Clear the single step flag while in the signal handler */
625 if ((td->td_pcb->pcb_flags & PCB_SINGLE_STEP) != 0) {
626 td->td_pcb->pcb_flags &= ~PCB_SINGLE_STEP;
627 WRITE_SPECIALREG(mdscr_el1,
628 READ_SPECIALREG(mdscr_el1) & ~MDSCR_SS);
632 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_elr,
636 mtx_lock(&psp->ps_mtx);