xref: /freebsd/sys/arm64/arm64/exec_machdep.c (revision 7e6437c0)
1b4ae6b16SKonstantin Belousov /*-
2b4ae6b16SKonstantin Belousov  * Copyright (c) 2014 Andrew Turner
3b4ae6b16SKonstantin Belousov  * All rights reserved.
4b4ae6b16SKonstantin Belousov  *
5b4ae6b16SKonstantin Belousov  * Redistribution and use in source and binary forms, with or without
6b4ae6b16SKonstantin Belousov  * modification, are permitted provided that the following conditions
7b4ae6b16SKonstantin Belousov  * are met:
8b4ae6b16SKonstantin Belousov  * 1. Redistributions of source code must retain the above copyright
9b4ae6b16SKonstantin Belousov  *    notice, this list of conditions and the following disclaimer.
10b4ae6b16SKonstantin Belousov  * 2. Redistributions in binary form must reproduce the above copyright
11b4ae6b16SKonstantin Belousov  *    notice, this list of conditions and the following disclaimer in the
12b4ae6b16SKonstantin Belousov  *    documentation and/or other materials provided with the distribution.
13b4ae6b16SKonstantin Belousov  *
14b4ae6b16SKonstantin Belousov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15b4ae6b16SKonstantin Belousov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16b4ae6b16SKonstantin Belousov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17b4ae6b16SKonstantin Belousov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18b4ae6b16SKonstantin Belousov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19b4ae6b16SKonstantin Belousov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20b4ae6b16SKonstantin Belousov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21b4ae6b16SKonstantin Belousov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22b4ae6b16SKonstantin Belousov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23b4ae6b16SKonstantin Belousov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24b4ae6b16SKonstantin Belousov  * SUCH DAMAGE.
25b4ae6b16SKonstantin Belousov  *
26b4ae6b16SKonstantin Belousov  */
27b4ae6b16SKonstantin Belousov 
28b4ae6b16SKonstantin Belousov #include <sys/param.h>
29b4ae6b16SKonstantin Belousov #include <sys/systm.h>
30b4ae6b16SKonstantin Belousov #include <sys/exec.h>
31b4ae6b16SKonstantin Belousov #include <sys/imgact.h>
32b4ae6b16SKonstantin Belousov #include <sys/kdb.h>
33b4ae6b16SKonstantin Belousov #include <sys/kernel.h>
34b4ae6b16SKonstantin Belousov #include <sys/ktr.h>
35b4ae6b16SKonstantin Belousov #include <sys/limits.h>
36b4ae6b16SKonstantin Belousov #include <sys/lock.h>
37b4ae6b16SKonstantin Belousov #include <sys/mutex.h>
38b4ae6b16SKonstantin Belousov #include <sys/proc.h>
39b4ae6b16SKonstantin Belousov #include <sys/ptrace.h>
40b4ae6b16SKonstantin Belousov #include <sys/reg.h>
41b4ae6b16SKonstantin Belousov #include <sys/rwlock.h>
42b4ae6b16SKonstantin Belousov #include <sys/signalvar.h>
43b4ae6b16SKonstantin Belousov #include <sys/syscallsubr.h>
44b4ae6b16SKonstantin Belousov #include <sys/sysent.h>
45b4ae6b16SKonstantin Belousov #include <sys/sysproto.h>
46b4ae6b16SKonstantin Belousov #include <sys/ucontext.h>
47b4ae6b16SKonstantin Belousov 
48b4ae6b16SKonstantin Belousov #include <vm/vm.h>
49b4ae6b16SKonstantin Belousov #include <vm/vm_param.h>
50361971fbSKornel Dulęba #include <vm/pmap.h>
51361971fbSKornel Dulęba #include <vm/vm_map.h>
52b4ae6b16SKonstantin Belousov 
53b4ae6b16SKonstantin Belousov #include <machine/armreg.h>
54b4ae6b16SKonstantin Belousov #include <machine/kdb.h>
55b4ae6b16SKonstantin Belousov #include <machine/md_var.h>
56b4ae6b16SKonstantin Belousov #include <machine/pcb.h>
57b4ae6b16SKonstantin Belousov 
58b4ae6b16SKonstantin Belousov #ifdef VFP
59b4ae6b16SKonstantin Belousov #include <machine/vfp.h>
60b4ae6b16SKonstantin Belousov #endif
61b4ae6b16SKonstantin Belousov 
623988ca5aSWarner Losh _Static_assert(sizeof(mcontext_t) == 880, "mcontext_t size incorrect");
633988ca5aSWarner Losh _Static_assert(sizeof(ucontext_t) == 960, "ucontext_t size incorrect");
643988ca5aSWarner Losh _Static_assert(sizeof(siginfo_t) == 80, "siginfo_t size incorrect");
653988ca5aSWarner Losh 
66b4ae6b16SKonstantin Belousov static void get_fpcontext(struct thread *td, mcontext_t *mcp);
67b4ae6b16SKonstantin Belousov static void set_fpcontext(struct thread *td, mcontext_t *mcp);
68b4ae6b16SKonstantin Belousov 
69b4ae6b16SKonstantin Belousov int
fill_regs(struct thread * td,struct reg * regs)70b4ae6b16SKonstantin Belousov fill_regs(struct thread *td, struct reg *regs)
71b4ae6b16SKonstantin Belousov {
72b4ae6b16SKonstantin Belousov 	struct trapframe *frame;
73b4ae6b16SKonstantin Belousov 
74b4ae6b16SKonstantin Belousov 	frame = td->td_frame;
75b4ae6b16SKonstantin Belousov 	regs->sp = frame->tf_sp;
76b4ae6b16SKonstantin Belousov 	regs->lr = frame->tf_lr;
77b4ae6b16SKonstantin Belousov 	regs->elr = frame->tf_elr;
78b4ae6b16SKonstantin Belousov 	regs->spsr = frame->tf_spsr;
79b4ae6b16SKonstantin Belousov 
80b4ae6b16SKonstantin Belousov 	memcpy(regs->x, frame->tf_x, sizeof(regs->x));
81b4ae6b16SKonstantin Belousov 
82b4ae6b16SKonstantin Belousov #ifdef COMPAT_FREEBSD32
83b4ae6b16SKonstantin Belousov 	/*
84b4ae6b16SKonstantin Belousov 	 * We may be called here for a 32bits process, if we're using a
85b4ae6b16SKonstantin Belousov 	 * 64bits debugger. If so, put PC and SPSR where it expects it.
86b4ae6b16SKonstantin Belousov 	 */
87b4ae6b16SKonstantin Belousov 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
88b4ae6b16SKonstantin Belousov 		regs->x[15] = frame->tf_elr;
89b4ae6b16SKonstantin Belousov 		regs->x[16] = frame->tf_spsr;
90b4ae6b16SKonstantin Belousov 	}
91b4ae6b16SKonstantin Belousov #endif
92b4ae6b16SKonstantin Belousov 	return (0);
93b4ae6b16SKonstantin Belousov }
94b4ae6b16SKonstantin Belousov 
95b4ae6b16SKonstantin Belousov int
set_regs(struct thread * td,struct reg * regs)96b4ae6b16SKonstantin Belousov set_regs(struct thread *td, struct reg *regs)
97b4ae6b16SKonstantin Belousov {
98b4ae6b16SKonstantin Belousov 	struct trapframe *frame;
99b4ae6b16SKonstantin Belousov 
100b4ae6b16SKonstantin Belousov 	frame = td->td_frame;
101b4ae6b16SKonstantin Belousov 	frame->tf_sp = regs->sp;
102b4ae6b16SKonstantin Belousov 	frame->tf_lr = regs->lr;
103b4ae6b16SKonstantin Belousov 
104b4ae6b16SKonstantin Belousov 	memcpy(frame->tf_x, regs->x, sizeof(frame->tf_x));
105b4ae6b16SKonstantin Belousov 
106b4ae6b16SKonstantin Belousov #ifdef COMPAT_FREEBSD32
107b4ae6b16SKonstantin Belousov 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
108b4ae6b16SKonstantin Belousov 		/*
109b4ae6b16SKonstantin Belousov 		 * We may be called for a 32bits process if we're using
110b4ae6b16SKonstantin Belousov 		 * a 64bits debugger. If so, get PC and SPSR from where
111b4ae6b16SKonstantin Belousov 		 * it put it.
112b4ae6b16SKonstantin Belousov 		 */
113b4ae6b16SKonstantin Belousov 		frame->tf_elr = regs->x[15];
11431cf95ceSAndrew Turner 		frame->tf_spsr &= ~PSR_SETTABLE_32;
11531cf95ceSAndrew Turner 		frame->tf_spsr |= regs->x[16] & PSR_SETTABLE_32;
11631cf95ceSAndrew Turner 		/* Don't allow userspace to ask to continue single stepping.
11731cf95ceSAndrew Turner 		 * The SPSR.SS field doesn't exist when the EL1 is AArch32.
11831cf95ceSAndrew Turner 		 * As the SPSR.DIT field has moved in its place don't
11931cf95ceSAndrew Turner 		 * allow userspace to set the SPSR.SS field.
12031cf95ceSAndrew Turner 		 */
121b4ae6b16SKonstantin Belousov 	} else
122b4ae6b16SKonstantin Belousov #endif
123b4ae6b16SKonstantin Belousov 	{
124b4ae6b16SKonstantin Belousov 		frame->tf_elr = regs->elr;
1254a06b28aSAndrew Turner 		/*
1264a06b28aSAndrew Turner 		 * frame->tf_spsr and regs->spsr on FreeBSD 13 was 32-bit
1274a06b28aSAndrew Turner 		 * where from 14 they are 64 bit. As PSR_SETTABLE_64 clears
1284a06b28aSAndrew Turner 		 * the upper 32 bits no compatibility handling is needed,
1294a06b28aSAndrew Turner 		 * however if this is ever not the case we will need to add
1304a06b28aSAndrew Turner 		 * these, similar to how it is done in set_mcontext.
1314a06b28aSAndrew Turner 		 */
13231cf95ceSAndrew Turner 		frame->tf_spsr &= ~PSR_SETTABLE_64;
13331cf95ceSAndrew Turner 		frame->tf_spsr |= regs->spsr & PSR_SETTABLE_64;
13431cf95ceSAndrew Turner 		/* Enable single stepping if userspace asked fot it */
13531cf95ceSAndrew Turner 		if ((frame->tf_spsr & PSR_SS) != 0) {
13631cf95ceSAndrew Turner 			td->td_pcb->pcb_flags |= PCB_SINGLE_STEP;
13731cf95ceSAndrew Turner 
13831cf95ceSAndrew Turner 			WRITE_SPECIALREG(mdscr_el1,
13931cf95ceSAndrew Turner 			    READ_SPECIALREG(mdscr_el1) | MDSCR_SS);
14031cf95ceSAndrew Turner 			isb();
14131cf95ceSAndrew Turner 		}
142b4ae6b16SKonstantin Belousov 	}
143b4ae6b16SKonstantin Belousov 	return (0);
144b4ae6b16SKonstantin Belousov }
145b4ae6b16SKonstantin Belousov 
146b4ae6b16SKonstantin Belousov int
fill_fpregs(struct thread * td,struct fpreg * regs)147b4ae6b16SKonstantin Belousov fill_fpregs(struct thread *td, struct fpreg *regs)
148b4ae6b16SKonstantin Belousov {
149b4ae6b16SKonstantin Belousov #ifdef VFP
150b4ae6b16SKonstantin Belousov 	struct pcb *pcb;
151b4ae6b16SKonstantin Belousov 
152b4ae6b16SKonstantin Belousov 	pcb = td->td_pcb;
153b4ae6b16SKonstantin Belousov 	if ((pcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
154b4ae6b16SKonstantin Belousov 		/*
155b4ae6b16SKonstantin Belousov 		 * If we have just been running VFP instructions we will
156b4ae6b16SKonstantin Belousov 		 * need to save the state to memcpy it below.
157b4ae6b16SKonstantin Belousov 		 */
158b4ae6b16SKonstantin Belousov 		if (td == curthread)
159b4ae6b16SKonstantin Belousov 			vfp_save_state(td, pcb);
16095dd6974SAndrew Turner 	}
161b4ae6b16SKonstantin Belousov 
162b4ae6b16SKonstantin Belousov 	KASSERT(pcb->pcb_fpusaved == &pcb->pcb_fpustate,
163b4ae6b16SKonstantin Belousov 	    ("Called fill_fpregs while the kernel is using the VFP"));
164b4ae6b16SKonstantin Belousov 	memcpy(regs->fp_q, pcb->pcb_fpustate.vfp_regs,
165b4ae6b16SKonstantin Belousov 	    sizeof(regs->fp_q));
166b4ae6b16SKonstantin Belousov 	regs->fp_cr = pcb->pcb_fpustate.vfp_fpcr;
167b4ae6b16SKonstantin Belousov 	regs->fp_sr = pcb->pcb_fpustate.vfp_fpsr;
16895dd6974SAndrew Turner #else
169b4ae6b16SKonstantin Belousov 	memset(regs, 0, sizeof(*regs));
17095dd6974SAndrew Turner #endif
171b4ae6b16SKonstantin Belousov 	return (0);
172b4ae6b16SKonstantin Belousov }
173b4ae6b16SKonstantin Belousov 
174b4ae6b16SKonstantin Belousov int
set_fpregs(struct thread * td,struct fpreg * regs)175b4ae6b16SKonstantin Belousov set_fpregs(struct thread *td, struct fpreg *regs)
176b4ae6b16SKonstantin Belousov {
177b4ae6b16SKonstantin Belousov #ifdef VFP
178b4ae6b16SKonstantin Belousov 	struct pcb *pcb;
179b4ae6b16SKonstantin Belousov 
180b4ae6b16SKonstantin Belousov 	pcb = td->td_pcb;
181b4ae6b16SKonstantin Belousov 	KASSERT(pcb->pcb_fpusaved == &pcb->pcb_fpustate,
182b4ae6b16SKonstantin Belousov 	    ("Called set_fpregs while the kernel is using the VFP"));
183b4ae6b16SKonstantin Belousov 	memcpy(pcb->pcb_fpustate.vfp_regs, regs->fp_q, sizeof(regs->fp_q));
184b4ae6b16SKonstantin Belousov 	pcb->pcb_fpustate.vfp_fpcr = regs->fp_cr;
185b4ae6b16SKonstantin Belousov 	pcb->pcb_fpustate.vfp_fpsr = regs->fp_sr;
186b4ae6b16SKonstantin Belousov #endif
187b4ae6b16SKonstantin Belousov 	return (0);
188b4ae6b16SKonstantin Belousov }
189b4ae6b16SKonstantin Belousov 
190b4ae6b16SKonstantin Belousov int
fill_dbregs(struct thread * td,struct dbreg * regs)191b4ae6b16SKonstantin Belousov fill_dbregs(struct thread *td, struct dbreg *regs)
192b4ae6b16SKonstantin Belousov {
193b4ae6b16SKonstantin Belousov 	struct debug_monitor_state *monitor;
194b4ae6b16SKonstantin Belousov 	int i;
195b4ae6b16SKonstantin Belousov 	uint8_t debug_ver, nbkpts, nwtpts;
196b4ae6b16SKonstantin Belousov 
197b4ae6b16SKonstantin Belousov 	memset(regs, 0, sizeof(*regs));
198b4ae6b16SKonstantin Belousov 
199b4ae6b16SKonstantin Belousov 	extract_user_id_field(ID_AA64DFR0_EL1, ID_AA64DFR0_DebugVer_SHIFT,
200b4ae6b16SKonstantin Belousov 	    &debug_ver);
201b4ae6b16SKonstantin Belousov 	extract_user_id_field(ID_AA64DFR0_EL1, ID_AA64DFR0_BRPs_SHIFT,
202b4ae6b16SKonstantin Belousov 	    &nbkpts);
203b4ae6b16SKonstantin Belousov 	extract_user_id_field(ID_AA64DFR0_EL1, ID_AA64DFR0_WRPs_SHIFT,
204b4ae6b16SKonstantin Belousov 	    &nwtpts);
205b4ae6b16SKonstantin Belousov 
206b4ae6b16SKonstantin Belousov 	/*
207b4ae6b16SKonstantin Belousov 	 * The BRPs field contains the number of breakpoints - 1. Armv8-A
208b4ae6b16SKonstantin Belousov 	 * allows the hardware to provide 2-16 breakpoints so this won't
209b4ae6b16SKonstantin Belousov 	 * overflow an 8 bit value. The same applies to the WRPs field.
210b4ae6b16SKonstantin Belousov 	 */
211b4ae6b16SKonstantin Belousov 	nbkpts++;
212b4ae6b16SKonstantin Belousov 	nwtpts++;
213b4ae6b16SKonstantin Belousov 
214b4ae6b16SKonstantin Belousov 	regs->db_debug_ver = debug_ver;
215b4ae6b16SKonstantin Belousov 	regs->db_nbkpts = nbkpts;
216b4ae6b16SKonstantin Belousov 	regs->db_nwtpts = nwtpts;
217b4ae6b16SKonstantin Belousov 
218b4ae6b16SKonstantin Belousov 	monitor = &td->td_pcb->pcb_dbg_regs;
219b4ae6b16SKonstantin Belousov 	if ((monitor->dbg_flags & DBGMON_ENABLED) != 0) {
220b4ae6b16SKonstantin Belousov 		for (i = 0; i < nbkpts; i++) {
221b4ae6b16SKonstantin Belousov 			regs->db_breakregs[i].dbr_addr = monitor->dbg_bvr[i];
222b4ae6b16SKonstantin Belousov 			regs->db_breakregs[i].dbr_ctrl = monitor->dbg_bcr[i];
223b4ae6b16SKonstantin Belousov 		}
224b4ae6b16SKonstantin Belousov 		for (i = 0; i < nwtpts; i++) {
225b4ae6b16SKonstantin Belousov 			regs->db_watchregs[i].dbw_addr = monitor->dbg_wvr[i];
226b4ae6b16SKonstantin Belousov 			regs->db_watchregs[i].dbw_ctrl = monitor->dbg_wcr[i];
227b4ae6b16SKonstantin Belousov 		}
228b4ae6b16SKonstantin Belousov 	}
229b4ae6b16SKonstantin Belousov 
230b4ae6b16SKonstantin Belousov 	return (0);
231b4ae6b16SKonstantin Belousov }
232b4ae6b16SKonstantin Belousov 
233b4ae6b16SKonstantin Belousov int
set_dbregs(struct thread * td,struct dbreg * regs)234b4ae6b16SKonstantin Belousov set_dbregs(struct thread *td, struct dbreg *regs)
235b4ae6b16SKonstantin Belousov {
236b4ae6b16SKonstantin Belousov 	struct debug_monitor_state *monitor;
237b4ae6b16SKonstantin Belousov 	uint64_t addr;
238b4ae6b16SKonstantin Belousov 	uint32_t ctrl;
239b4ae6b16SKonstantin Belousov 	int i;
240b4ae6b16SKonstantin Belousov 
241b4ae6b16SKonstantin Belousov 	monitor = &td->td_pcb->pcb_dbg_regs;
242b4ae6b16SKonstantin Belousov 	monitor->dbg_enable_count = 0;
243b4ae6b16SKonstantin Belousov 
244b4ae6b16SKonstantin Belousov 	for (i = 0; i < DBG_BRP_MAX; i++) {
245b4ae6b16SKonstantin Belousov 		addr = regs->db_breakregs[i].dbr_addr;
246b4ae6b16SKonstantin Belousov 		ctrl = regs->db_breakregs[i].dbr_ctrl;
247b4ae6b16SKonstantin Belousov 
248b4ae6b16SKonstantin Belousov 		/*
249b4ae6b16SKonstantin Belousov 		 * Don't let the user set a breakpoint on a kernel or
250b4ae6b16SKonstantin Belousov 		 * non-canonical user address.
251b4ae6b16SKonstantin Belousov 		 */
252b4ae6b16SKonstantin Belousov 		if (addr >= VM_MAXUSER_ADDRESS)
253b4ae6b16SKonstantin Belousov 			return (EINVAL);
254b4ae6b16SKonstantin Belousov 
255b4ae6b16SKonstantin Belousov 		/*
256b4ae6b16SKonstantin Belousov 		 * The lowest 2 bits are ignored, so record the effective
257b4ae6b16SKonstantin Belousov 		 * address.
258b4ae6b16SKonstantin Belousov 		 */
259b4ae6b16SKonstantin Belousov 		addr = rounddown2(addr, 4);
260b4ae6b16SKonstantin Belousov 
261b4ae6b16SKonstantin Belousov 		/*
262b4ae6b16SKonstantin Belousov 		 * Some control fields are ignored, and other bits reserved.
263b4ae6b16SKonstantin Belousov 		 * Only unlinked, address-matching breakpoints are supported.
264b4ae6b16SKonstantin Belousov 		 *
265b4ae6b16SKonstantin Belousov 		 * XXX: fields that appear unvalidated, such as BAS, have
266b4ae6b16SKonstantin Belousov 		 * constrained undefined behaviour. If the user mis-programs
267b4ae6b16SKonstantin Belousov 		 * these, there is no risk to the system.
268b4ae6b16SKonstantin Belousov 		 */
269664640baSAndrew Turner 		ctrl &= DBGBCR_EN | DBGBCR_PMC | DBGBCR_BAS;
270664640baSAndrew Turner 		if ((ctrl & DBGBCR_EN) != 0) {
271b4ae6b16SKonstantin Belousov 			/* Only target EL0. */
272664640baSAndrew Turner 			if ((ctrl & DBGBCR_PMC) != DBGBCR_PMC_EL0)
273b4ae6b16SKonstantin Belousov 				return (EINVAL);
274b4ae6b16SKonstantin Belousov 
275b4ae6b16SKonstantin Belousov 			monitor->dbg_enable_count++;
276b4ae6b16SKonstantin Belousov 		}
277b4ae6b16SKonstantin Belousov 
278b4ae6b16SKonstantin Belousov 		monitor->dbg_bvr[i] = addr;
279b4ae6b16SKonstantin Belousov 		monitor->dbg_bcr[i] = ctrl;
280b4ae6b16SKonstantin Belousov 	}
281b4ae6b16SKonstantin Belousov 
282b4ae6b16SKonstantin Belousov 	for (i = 0; i < DBG_WRP_MAX; i++) {
283b4ae6b16SKonstantin Belousov 		addr = regs->db_watchregs[i].dbw_addr;
284b4ae6b16SKonstantin Belousov 		ctrl = regs->db_watchregs[i].dbw_ctrl;
285b4ae6b16SKonstantin Belousov 
286b4ae6b16SKonstantin Belousov 		/*
287b4ae6b16SKonstantin Belousov 		 * Don't let the user set a watchpoint on a kernel or
288b4ae6b16SKonstantin Belousov 		 * non-canonical user address.
289b4ae6b16SKonstantin Belousov 		 */
290b4ae6b16SKonstantin Belousov 		if (addr >= VM_MAXUSER_ADDRESS)
291b4ae6b16SKonstantin Belousov 			return (EINVAL);
292b4ae6b16SKonstantin Belousov 
293b4ae6b16SKonstantin Belousov 		/*
294b4ae6b16SKonstantin Belousov 		 * Some control fields are ignored, and other bits reserved.
295b4ae6b16SKonstantin Belousov 		 * Only unlinked watchpoints are supported.
296b4ae6b16SKonstantin Belousov 		 */
297664640baSAndrew Turner 		ctrl &= DBGWCR_EN | DBGWCR_PAC | DBGWCR_LSC | DBGWCR_BAS |
298664640baSAndrew Turner 		    DBGWCR_MASK;
299b4ae6b16SKonstantin Belousov 
300664640baSAndrew Turner 		if ((ctrl & DBGWCR_EN) != 0) {
301b4ae6b16SKonstantin Belousov 			/* Only target EL0. */
302664640baSAndrew Turner 			if ((ctrl & DBGWCR_PAC) != DBGWCR_PAC_EL0)
303b4ae6b16SKonstantin Belousov 				return (EINVAL);
304b4ae6b16SKonstantin Belousov 
305b4ae6b16SKonstantin Belousov 			/* Must set at least one of the load/store bits. */
306664640baSAndrew Turner 			if ((ctrl & DBGWCR_LSC) == 0)
307b4ae6b16SKonstantin Belousov 				return (EINVAL);
308b4ae6b16SKonstantin Belousov 
309b4ae6b16SKonstantin Belousov 			/*
310b4ae6b16SKonstantin Belousov 			 * When specifying the address range with BAS, the MASK
311b4ae6b16SKonstantin Belousov 			 * field must be zero.
312b4ae6b16SKonstantin Belousov 			 */
313664640baSAndrew Turner 			if ((ctrl & DBGWCR_BAS) != DBGWCR_BAS &&
314664640baSAndrew Turner 			    (ctrl & DBGWCR_MASK) != 0)
315b4ae6b16SKonstantin Belousov 				return (EINVAL);
316b4ae6b16SKonstantin Belousov 
317b4ae6b16SKonstantin Belousov 			monitor->dbg_enable_count++;
318b4ae6b16SKonstantin Belousov 		}
319b4ae6b16SKonstantin Belousov 		monitor->dbg_wvr[i] = addr;
320b4ae6b16SKonstantin Belousov 		monitor->dbg_wcr[i] = ctrl;
321b4ae6b16SKonstantin Belousov 	}
322b4ae6b16SKonstantin Belousov 
323b4ae6b16SKonstantin Belousov 	if (monitor->dbg_enable_count > 0)
324b4ae6b16SKonstantin Belousov 		monitor->dbg_flags |= DBGMON_ENABLED;
325b4ae6b16SKonstantin Belousov 
326b4ae6b16SKonstantin Belousov 	return (0);
327b4ae6b16SKonstantin Belousov }
328b4ae6b16SKonstantin Belousov 
329b4ae6b16SKonstantin Belousov #ifdef COMPAT_FREEBSD32
330b4ae6b16SKonstantin Belousov int
fill_regs32(struct thread * td,struct reg32 * regs)331b4ae6b16SKonstantin Belousov fill_regs32(struct thread *td, struct reg32 *regs)
332b4ae6b16SKonstantin Belousov {
333b4ae6b16SKonstantin Belousov 	int i;
334b4ae6b16SKonstantin Belousov 	struct trapframe *tf;
335b4ae6b16SKonstantin Belousov 
336b4ae6b16SKonstantin Belousov 	tf = td->td_frame;
337b4ae6b16SKonstantin Belousov 	for (i = 0; i < 13; i++)
338b4ae6b16SKonstantin Belousov 		regs->r[i] = tf->tf_x[i];
339b4ae6b16SKonstantin Belousov 	/* For arm32, SP is r13 and LR is r14 */
340b4ae6b16SKonstantin Belousov 	regs->r_sp = tf->tf_x[13];
341b4ae6b16SKonstantin Belousov 	regs->r_lr = tf->tf_x[14];
342b4ae6b16SKonstantin Belousov 	regs->r_pc = tf->tf_elr;
343b4ae6b16SKonstantin Belousov 	regs->r_cpsr = tf->tf_spsr;
344b4ae6b16SKonstantin Belousov 
345b4ae6b16SKonstantin Belousov 	return (0);
346b4ae6b16SKonstantin Belousov }
347b4ae6b16SKonstantin Belousov 
348b4ae6b16SKonstantin Belousov int
set_regs32(struct thread * td,struct reg32 * regs)349b4ae6b16SKonstantin Belousov set_regs32(struct thread *td, struct reg32 *regs)
350b4ae6b16SKonstantin Belousov {
351b4ae6b16SKonstantin Belousov 	int i;
352b4ae6b16SKonstantin Belousov 	struct trapframe *tf;
353b4ae6b16SKonstantin Belousov 
354b4ae6b16SKonstantin Belousov 	tf = td->td_frame;
355b4ae6b16SKonstantin Belousov 	for (i = 0; i < 13; i++)
356b4ae6b16SKonstantin Belousov 		tf->tf_x[i] = regs->r[i];
357b4ae6b16SKonstantin Belousov 	/* For arm 32, SP is r13 an LR is r14 */
358b4ae6b16SKonstantin Belousov 	tf->tf_x[13] = regs->r_sp;
359b4ae6b16SKonstantin Belousov 	tf->tf_x[14] = regs->r_lr;
360b4ae6b16SKonstantin Belousov 	tf->tf_elr = regs->r_pc;
36131cf95ceSAndrew Turner 	tf->tf_spsr &= ~PSR_SETTABLE_32;
36231cf95ceSAndrew Turner 	tf->tf_spsr |= regs->r_cpsr & PSR_SETTABLE_32;
363b4ae6b16SKonstantin Belousov 
364b4ae6b16SKonstantin Belousov 	return (0);
365b4ae6b16SKonstantin Belousov }
366b4ae6b16SKonstantin Belousov 
367b4ae6b16SKonstantin Belousov /* XXX fill/set dbregs/fpregs are stubbed on 32-bit arm. */
368b4ae6b16SKonstantin Belousov int
fill_fpregs32(struct thread * td,struct fpreg32 * regs)369b4ae6b16SKonstantin Belousov fill_fpregs32(struct thread *td, struct fpreg32 *regs)
370b4ae6b16SKonstantin Belousov {
371b4ae6b16SKonstantin Belousov 
372b4ae6b16SKonstantin Belousov 	memset(regs, 0, sizeof(*regs));
373b4ae6b16SKonstantin Belousov 	return (0);
374b4ae6b16SKonstantin Belousov }
375b4ae6b16SKonstantin Belousov 
376b4ae6b16SKonstantin Belousov int
set_fpregs32(struct thread * td,struct fpreg32 * regs)377b4ae6b16SKonstantin Belousov set_fpregs32(struct thread *td, struct fpreg32 *regs)
378b4ae6b16SKonstantin Belousov {
379b4ae6b16SKonstantin Belousov 
380b4ae6b16SKonstantin Belousov 	return (0);
381b4ae6b16SKonstantin Belousov }
382b4ae6b16SKonstantin Belousov 
383b4ae6b16SKonstantin Belousov int
fill_dbregs32(struct thread * td,struct dbreg32 * regs)384b4ae6b16SKonstantin Belousov fill_dbregs32(struct thread *td, struct dbreg32 *regs)
385b4ae6b16SKonstantin Belousov {
386b4ae6b16SKonstantin Belousov 
387b4ae6b16SKonstantin Belousov 	memset(regs, 0, sizeof(*regs));
388b4ae6b16SKonstantin Belousov 	return (0);
389b4ae6b16SKonstantin Belousov }
390b4ae6b16SKonstantin Belousov 
391b4ae6b16SKonstantin Belousov int
set_dbregs32(struct thread * td,struct dbreg32 * regs)392b4ae6b16SKonstantin Belousov set_dbregs32(struct thread *td, struct dbreg32 *regs)
393b4ae6b16SKonstantin Belousov {
394b4ae6b16SKonstantin Belousov 
395b4ae6b16SKonstantin Belousov 	return (0);
396b4ae6b16SKonstantin Belousov }
397b4ae6b16SKonstantin Belousov #endif
398b4ae6b16SKonstantin Belousov 
399b4ae6b16SKonstantin Belousov void
exec_setregs(struct thread * td,struct image_params * imgp,uintptr_t stack)400b4ae6b16SKonstantin Belousov exec_setregs(struct thread *td, struct image_params *imgp, uintptr_t stack)
401b4ae6b16SKonstantin Belousov {
402b4ae6b16SKonstantin Belousov 	struct trapframe *tf = td->td_frame;
403b4ae6b16SKonstantin Belousov 	struct pcb *pcb = td->td_pcb;
404b4ae6b16SKonstantin Belousov 
405b4ae6b16SKonstantin Belousov 	memset(tf, 0, sizeof(struct trapframe));
406b4ae6b16SKonstantin Belousov 
407b4ae6b16SKonstantin Belousov 	tf->tf_x[0] = stack;
408b4ae6b16SKonstantin Belousov 	tf->tf_sp = STACKALIGN(stack);
409b4ae6b16SKonstantin Belousov 	tf->tf_lr = imgp->entry_addr;
410b4ae6b16SKonstantin Belousov 	tf->tf_elr = imgp->entry_addr;
411b4ae6b16SKonstantin Belousov 
412b4ae6b16SKonstantin Belousov 	td->td_pcb->pcb_tpidr_el0 = 0;
413b4ae6b16SKonstantin Belousov 	td->td_pcb->pcb_tpidrro_el0 = 0;
414b4ae6b16SKonstantin Belousov 	WRITE_SPECIALREG(tpidrro_el0, 0);
415b4ae6b16SKonstantin Belousov 	WRITE_SPECIALREG(tpidr_el0, 0);
416b4ae6b16SKonstantin Belousov 
417b4ae6b16SKonstantin Belousov #ifdef VFP
418b4ae6b16SKonstantin Belousov 	vfp_reset_state(td, pcb);
419b4ae6b16SKonstantin Belousov #endif
420b4ae6b16SKonstantin Belousov 
421b4ae6b16SKonstantin Belousov 	/*
422b4ae6b16SKonstantin Belousov 	 * Clear debug register state. It is not applicable to the new process.
423b4ae6b16SKonstantin Belousov 	 */
424b4ae6b16SKonstantin Belousov 	bzero(&pcb->pcb_dbg_regs, sizeof(pcb->pcb_dbg_regs));
42585b7c566SAndrew Turner 
42685b7c566SAndrew Turner 	/* Generate new pointer authentication keys */
42785b7c566SAndrew Turner 	ptrauth_exec(td);
428b4ae6b16SKonstantin Belousov }
429b4ae6b16SKonstantin Belousov 
430b4ae6b16SKonstantin Belousov /* Sanity check these are the same size, they will be memcpy'd to and from */
431b4ae6b16SKonstantin Belousov CTASSERT(sizeof(((struct trapframe *)0)->tf_x) ==
432b4ae6b16SKonstantin Belousov     sizeof((struct gpregs *)0)->gp_x);
433b4ae6b16SKonstantin Belousov CTASSERT(sizeof(((struct trapframe *)0)->tf_x) ==
434b4ae6b16SKonstantin Belousov     sizeof((struct reg *)0)->x);
435b4ae6b16SKonstantin Belousov 
436b4ae6b16SKonstantin Belousov int
get_mcontext(struct thread * td,mcontext_t * mcp,int clear_ret)437b4ae6b16SKonstantin Belousov get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
438b4ae6b16SKonstantin Belousov {
439b4ae6b16SKonstantin Belousov 	struct trapframe *tf = td->td_frame;
440b4ae6b16SKonstantin Belousov 
441b4ae6b16SKonstantin Belousov 	if (clear_ret & GET_MC_CLEAR_RET) {
442b4ae6b16SKonstantin Belousov 		mcp->mc_gpregs.gp_x[0] = 0;
443b4ae6b16SKonstantin Belousov 		mcp->mc_gpregs.gp_spsr = tf->tf_spsr & ~PSR_C;
444b4ae6b16SKonstantin Belousov 	} else {
445b4ae6b16SKonstantin Belousov 		mcp->mc_gpregs.gp_x[0] = tf->tf_x[0];
446b4ae6b16SKonstantin Belousov 		mcp->mc_gpregs.gp_spsr = tf->tf_spsr;
447b4ae6b16SKonstantin Belousov 	}
448b4ae6b16SKonstantin Belousov 
449b4ae6b16SKonstantin Belousov 	memcpy(&mcp->mc_gpregs.gp_x[1], &tf->tf_x[1],
450b4ae6b16SKonstantin Belousov 	    sizeof(mcp->mc_gpregs.gp_x[1]) * (nitems(mcp->mc_gpregs.gp_x) - 1));
451b4ae6b16SKonstantin Belousov 
452b4ae6b16SKonstantin Belousov 	mcp->mc_gpregs.gp_sp = tf->tf_sp;
453b4ae6b16SKonstantin Belousov 	mcp->mc_gpregs.gp_lr = tf->tf_lr;
454b4ae6b16SKonstantin Belousov 	mcp->mc_gpregs.gp_elr = tf->tf_elr;
455b4ae6b16SKonstantin Belousov 	get_fpcontext(td, mcp);
456b4ae6b16SKonstantin Belousov 
457b4ae6b16SKonstantin Belousov 	return (0);
458b4ae6b16SKonstantin Belousov }
459b4ae6b16SKonstantin Belousov 
460b4ae6b16SKonstantin Belousov int
set_mcontext(struct thread * td,mcontext_t * mcp)461b4ae6b16SKonstantin Belousov set_mcontext(struct thread *td, mcontext_t *mcp)
462b4ae6b16SKonstantin Belousov {
4634a06b28aSAndrew Turner #define	PSR_13_MASK	0xfffffffful
4647e6437c0SAndrew Turner 	struct arm64_reg_context ctx;
465b4ae6b16SKonstantin Belousov 	struct trapframe *tf = td->td_frame;
4662ecbbcc7SZachary Leaf 	uint64_t spsr;
4677e6437c0SAndrew Turner 	vm_offset_t addr;
4687e6437c0SAndrew Turner 	int error;
4697e6437c0SAndrew Turner 	bool done;
470b4ae6b16SKonstantin Belousov 
471b4ae6b16SKonstantin Belousov 	spsr = mcp->mc_gpregs.gp_spsr;
4724a06b28aSAndrew Turner #ifdef COMPAT_FREEBSD13
4734a06b28aSAndrew Turner 	if (td->td_proc->p_osrel < P_OSREL_ARM64_SPSR) {
4744a06b28aSAndrew Turner 		/*
4754a06b28aSAndrew Turner 		 * Before FreeBSD 14 gp_spsr was 32 bit. The size of mc_gpregs
4764a06b28aSAndrew Turner 		 * was identical because of padding so mask of the upper bits
4774a06b28aSAndrew Turner 		 * that may be invalid on earlier releases.
4784a06b28aSAndrew Turner 		 */
4794a06b28aSAndrew Turner 		spsr &= PSR_13_MASK;
4804a06b28aSAndrew Turner 	}
4814a06b28aSAndrew Turner #endif
4824a06b28aSAndrew Turner 
483b4ae6b16SKonstantin Belousov 	if ((spsr & PSR_M_MASK) != PSR_M_EL0t ||
484b4ae6b16SKonstantin Belousov 	    (spsr & PSR_AARCH32) != 0 ||
485b4ae6b16SKonstantin Belousov 	    (spsr & PSR_DAIF) != (td->td_frame->tf_spsr & PSR_DAIF))
486b4ae6b16SKonstantin Belousov 		return (EINVAL);
487b4ae6b16SKonstantin Belousov 
488b4ae6b16SKonstantin Belousov 	memcpy(tf->tf_x, mcp->mc_gpregs.gp_x, sizeof(tf->tf_x));
489b4ae6b16SKonstantin Belousov 
490b4ae6b16SKonstantin Belousov 	tf->tf_sp = mcp->mc_gpregs.gp_sp;
491b4ae6b16SKonstantin Belousov 	tf->tf_lr = mcp->mc_gpregs.gp_lr;
492b4ae6b16SKonstantin Belousov 	tf->tf_elr = mcp->mc_gpregs.gp_elr;
4934a06b28aSAndrew Turner #ifdef COMPAT_FREEBSD13
4944a06b28aSAndrew Turner 	if (td->td_proc->p_osrel < P_OSREL_ARM64_SPSR) {
4954a06b28aSAndrew Turner 		/* Keep the upper 32 bits of spsr on older releases */
4964a06b28aSAndrew Turner 		tf->tf_spsr &= ~PSR_13_MASK;
4974a06b28aSAndrew Turner 		tf->tf_spsr |= spsr;
4984a06b28aSAndrew Turner 	} else
4994a06b28aSAndrew Turner #endif
5004a06b28aSAndrew Turner 		tf->tf_spsr = spsr;
50131cf95ceSAndrew Turner 	if ((tf->tf_spsr & PSR_SS) != 0) {
50231cf95ceSAndrew Turner 		td->td_pcb->pcb_flags |= PCB_SINGLE_STEP;
50331cf95ceSAndrew Turner 
50431cf95ceSAndrew Turner 		WRITE_SPECIALREG(mdscr_el1,
50531cf95ceSAndrew Turner 		    READ_SPECIALREG(mdscr_el1) | MDSCR_SS);
50631cf95ceSAndrew Turner 		isb();
50731cf95ceSAndrew Turner 	}
5087e6437c0SAndrew Turner 
509b4ae6b16SKonstantin Belousov 	set_fpcontext(td, mcp);
510b4ae6b16SKonstantin Belousov 
5117e6437c0SAndrew Turner 	/* Read any register contexts we find */
5127e6437c0SAndrew Turner 	if (mcp->mc_ptr != 0) {
5137e6437c0SAndrew Turner 		addr = mcp->mc_ptr;
5147e6437c0SAndrew Turner 
5157e6437c0SAndrew Turner 		done = false;
5167e6437c0SAndrew Turner 		do {
5177e6437c0SAndrew Turner 			if (!__is_aligned(addr,
5187e6437c0SAndrew Turner 			    _Alignof(struct arm64_reg_context)))
5197e6437c0SAndrew Turner 				return (EINVAL);
5207e6437c0SAndrew Turner 
5217e6437c0SAndrew Turner 			error = copyin((const void *)addr, &ctx, sizeof(ctx));
5227e6437c0SAndrew Turner 			if (error != 0)
5237e6437c0SAndrew Turner 				return (error);
5247e6437c0SAndrew Turner 
5257e6437c0SAndrew Turner 			switch (ctx.ctx_id) {
5267e6437c0SAndrew Turner 			case ARM64_CTX_END:
5277e6437c0SAndrew Turner 				done = true;
5287e6437c0SAndrew Turner 				break;
5297e6437c0SAndrew Turner 			default:
5307e6437c0SAndrew Turner 				return (EINVAL);
5317e6437c0SAndrew Turner 			}
5327e6437c0SAndrew Turner 
5337e6437c0SAndrew Turner 			addr += ctx.ctx_size;
5347e6437c0SAndrew Turner 		} while (!done);
5357e6437c0SAndrew Turner 	}
5367e6437c0SAndrew Turner 
537b4ae6b16SKonstantin Belousov 	return (0);
5384a06b28aSAndrew Turner #undef PSR_13_MASK
539b4ae6b16SKonstantin Belousov }
540b4ae6b16SKonstantin Belousov 
541b4ae6b16SKonstantin Belousov static void
get_fpcontext(struct thread * td,mcontext_t * mcp)542b4ae6b16SKonstantin Belousov get_fpcontext(struct thread *td, mcontext_t *mcp)
543b4ae6b16SKonstantin Belousov {
544b4ae6b16SKonstantin Belousov #ifdef VFP
545b4ae6b16SKonstantin Belousov 	struct pcb *curpcb;
546b4ae6b16SKonstantin Belousov 
54761f5462fSAndrew Turner 	MPASS(td == curthread);
548b4ae6b16SKonstantin Belousov 
549b4ae6b16SKonstantin Belousov 	curpcb = curthread->td_pcb;
550b4ae6b16SKonstantin Belousov 	if ((curpcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
551b4ae6b16SKonstantin Belousov 		/*
552b4ae6b16SKonstantin Belousov 		 * If we have just been running VFP instructions we will
553b4ae6b16SKonstantin Belousov 		 * need to save the state to memcpy it below.
554b4ae6b16SKonstantin Belousov 		 */
555b4ae6b16SKonstantin Belousov 		vfp_save_state(td, curpcb);
55661f5462fSAndrew Turner 	}
557b4ae6b16SKonstantin Belousov 
558b4ae6b16SKonstantin Belousov 	KASSERT(curpcb->pcb_fpusaved == &curpcb->pcb_fpustate,
559b4ae6b16SKonstantin Belousov 	    ("Called get_fpcontext while the kernel is using the VFP"));
560b4ae6b16SKonstantin Belousov 	KASSERT((curpcb->pcb_fpflags & ~PCB_FP_USERMASK) == 0,
561b4ae6b16SKonstantin Belousov 	    ("Non-userspace FPU flags set in get_fpcontext"));
562b4ae6b16SKonstantin Belousov 	memcpy(mcp->mc_fpregs.fp_q, curpcb->pcb_fpustate.vfp_regs,
563b4ae6b16SKonstantin Belousov 	    sizeof(mcp->mc_fpregs.fp_q));
564b4ae6b16SKonstantin Belousov 	mcp->mc_fpregs.fp_cr = curpcb->pcb_fpustate.vfp_fpcr;
565b4ae6b16SKonstantin Belousov 	mcp->mc_fpregs.fp_sr = curpcb->pcb_fpustate.vfp_fpsr;
566b4ae6b16SKonstantin Belousov 	mcp->mc_fpregs.fp_flags = curpcb->pcb_fpflags;
567b4ae6b16SKonstantin Belousov 	mcp->mc_flags |= _MC_FP_VALID;
568b4ae6b16SKonstantin Belousov #endif
569b4ae6b16SKonstantin Belousov }
570b4ae6b16SKonstantin Belousov 
571b4ae6b16SKonstantin Belousov static void
set_fpcontext(struct thread * td,mcontext_t * mcp)572b4ae6b16SKonstantin Belousov set_fpcontext(struct thread *td, mcontext_t *mcp)
573b4ae6b16SKonstantin Belousov {
574b4ae6b16SKonstantin Belousov #ifdef VFP
575b4ae6b16SKonstantin Belousov 	struct pcb *curpcb;
576b4ae6b16SKonstantin Belousov 
577a85cf421SAndrew Turner 	MPASS(td == curthread);
578b4ae6b16SKonstantin Belousov 	if ((mcp->mc_flags & _MC_FP_VALID) != 0) {
579b4ae6b16SKonstantin Belousov 		curpcb = curthread->td_pcb;
580b4ae6b16SKonstantin Belousov 
581b4ae6b16SKonstantin Belousov 		/*
582b4ae6b16SKonstantin Belousov 		 * Discard any vfp state for the current thread, we
583b4ae6b16SKonstantin Belousov 		 * are about to override it.
584b4ae6b16SKonstantin Belousov 		 */
585a85cf421SAndrew Turner 		critical_enter();
586b4ae6b16SKonstantin Belousov 		vfp_discard(td);
587a85cf421SAndrew Turner 		critical_exit();
588b4ae6b16SKonstantin Belousov 
589b4ae6b16SKonstantin Belousov 		KASSERT(curpcb->pcb_fpusaved == &curpcb->pcb_fpustate,
590b4ae6b16SKonstantin Belousov 		    ("Called set_fpcontext while the kernel is using the VFP"));
591b4ae6b16SKonstantin Belousov 		memcpy(curpcb->pcb_fpustate.vfp_regs, mcp->mc_fpregs.fp_q,
592b4ae6b16SKonstantin Belousov 		    sizeof(mcp->mc_fpregs.fp_q));
593b4ae6b16SKonstantin Belousov 		curpcb->pcb_fpustate.vfp_fpcr = mcp->mc_fpregs.fp_cr;
594b4ae6b16SKonstantin Belousov 		curpcb->pcb_fpustate.vfp_fpsr = mcp->mc_fpregs.fp_sr;
595b4ae6b16SKonstantin Belousov 		curpcb->pcb_fpflags = mcp->mc_fpregs.fp_flags & PCB_FP_USERMASK;
596b4ae6b16SKonstantin Belousov 	}
597b4ae6b16SKonstantin Belousov #endif
598b4ae6b16SKonstantin Belousov }
599b4ae6b16SKonstantin Belousov 
600b4ae6b16SKonstantin Belousov int
sys_sigreturn(struct thread * td,struct sigreturn_args * uap)601b4ae6b16SKonstantin Belousov sys_sigreturn(struct thread *td, struct sigreturn_args *uap)
602b4ae6b16SKonstantin Belousov {
603b4ae6b16SKonstantin Belousov 	ucontext_t uc;
604b4ae6b16SKonstantin Belousov 	int error;
605b4ae6b16SKonstantin Belousov 
606b4ae6b16SKonstantin Belousov 	if (copyin(uap->sigcntxp, &uc, sizeof(uc)))
607b4ae6b16SKonstantin Belousov 		return (EFAULT);
608b4ae6b16SKonstantin Belousov 
609b4ae6b16SKonstantin Belousov 	error = set_mcontext(td, &uc.uc_mcontext);
610b4ae6b16SKonstantin Belousov 	if (error != 0)
611b4ae6b16SKonstantin Belousov 		return (error);
612b4ae6b16SKonstantin Belousov 
613b4ae6b16SKonstantin Belousov 	/* Restore signal mask. */
614b4ae6b16SKonstantin Belousov 	kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
615b4ae6b16SKonstantin Belousov 
616b4ae6b16SKonstantin Belousov 	return (EJUSTRETURN);
617b4ae6b16SKonstantin Belousov }
618b4ae6b16SKonstantin Belousov 
6197e6437c0SAndrew Turner static bool
sendsig_ctx_end(struct thread * td,vm_offset_t * addrp)6207e6437c0SAndrew Turner sendsig_ctx_end(struct thread *td, vm_offset_t *addrp)
6217e6437c0SAndrew Turner {
6227e6437c0SAndrew Turner 	struct arm64_reg_context end_ctx;
6237e6437c0SAndrew Turner 	vm_offset_t ctx_addr;
6247e6437c0SAndrew Turner 
6257e6437c0SAndrew Turner 	*addrp -= sizeof(end_ctx);
6267e6437c0SAndrew Turner 	ctx_addr = *addrp;
6277e6437c0SAndrew Turner 
6287e6437c0SAndrew Turner 	memset(&end_ctx, 0, sizeof(end_ctx));
6297e6437c0SAndrew Turner 	end_ctx.ctx_id = ARM64_CTX_END;
6307e6437c0SAndrew Turner 	end_ctx.ctx_size = sizeof(end_ctx);
6317e6437c0SAndrew Turner 
6327e6437c0SAndrew Turner 	if (copyout(&end_ctx, (void *)ctx_addr, sizeof(end_ctx)) != 0)
6337e6437c0SAndrew Turner 		return (false);
6347e6437c0SAndrew Turner 
6357e6437c0SAndrew Turner 	return (true);
6367e6437c0SAndrew Turner }
6377e6437c0SAndrew Turner 
6387e6437c0SAndrew Turner typedef bool(*ctx_func)(struct thread *, vm_offset_t *);
6397e6437c0SAndrew Turner static const ctx_func ctx_funcs[] = {
6407e6437c0SAndrew Turner 	sendsig_ctx_end,	/* Must be first to end the linked list */
6417e6437c0SAndrew Turner 	NULL,
6427e6437c0SAndrew Turner };
6437e6437c0SAndrew Turner 
644b4ae6b16SKonstantin Belousov void
sendsig(sig_t catcher,ksiginfo_t * ksi,sigset_t * mask)645b4ae6b16SKonstantin Belousov sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
646b4ae6b16SKonstantin Belousov {
647b4ae6b16SKonstantin Belousov 	struct thread *td;
648b4ae6b16SKonstantin Belousov 	struct proc *p;
649b4ae6b16SKonstantin Belousov 	struct trapframe *tf;
650b4ae6b16SKonstantin Belousov 	struct sigframe *fp, frame;
651b4ae6b16SKonstantin Belousov 	struct sigacts *psp;
6527e6437c0SAndrew Turner 	vm_offset_t addr;
653b4ae6b16SKonstantin Belousov 	int onstack, sig;
654b4ae6b16SKonstantin Belousov 
655b4ae6b16SKonstantin Belousov 	td = curthread;
656b4ae6b16SKonstantin Belousov 	p = td->td_proc;
657b4ae6b16SKonstantin Belousov 	PROC_LOCK_ASSERT(p, MA_OWNED);
658b4ae6b16SKonstantin Belousov 
659b4ae6b16SKonstantin Belousov 	sig = ksi->ksi_signo;
660b4ae6b16SKonstantin Belousov 	psp = p->p_sigacts;
661b4ae6b16SKonstantin Belousov 	mtx_assert(&psp->ps_mtx, MA_OWNED);
662b4ae6b16SKonstantin Belousov 
663b4ae6b16SKonstantin Belousov 	tf = td->td_frame;
664b4ae6b16SKonstantin Belousov 	onstack = sigonstack(tf->tf_sp);
665b4ae6b16SKonstantin Belousov 
666b4ae6b16SKonstantin Belousov 	CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
667b4ae6b16SKonstantin Belousov 	    catcher, sig);
668b4ae6b16SKonstantin Belousov 
669b4ae6b16SKonstantin Belousov 	/* Allocate and validate space for the signal handler context. */
670b4ae6b16SKonstantin Belousov 	if ((td->td_pflags & TDP_ALTSTACK) != 0 && !onstack &&
671b4ae6b16SKonstantin Belousov 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
6727e6437c0SAndrew Turner 		addr = ((uintptr_t)td->td_sigstk.ss_sp +
673b4ae6b16SKonstantin Belousov 		    td->td_sigstk.ss_size);
674b4ae6b16SKonstantin Belousov #if defined(COMPAT_43)
675b4ae6b16SKonstantin Belousov 		td->td_sigstk.ss_flags |= SS_ONSTACK;
676b4ae6b16SKonstantin Belousov #endif
677b4ae6b16SKonstantin Belousov 	} else {
6787e6437c0SAndrew Turner 		addr = td->td_frame->tf_sp;
679b4ae6b16SKonstantin Belousov 	}
680b4ae6b16SKonstantin Belousov 
681b4ae6b16SKonstantin Belousov 	/* Fill in the frame to copy out */
682b4ae6b16SKonstantin Belousov 	bzero(&frame, sizeof(frame));
683b4ae6b16SKonstantin Belousov 	get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
684b4ae6b16SKonstantin Belousov 	frame.sf_si = ksi->ksi_info;
685b4ae6b16SKonstantin Belousov 	frame.sf_uc.uc_sigmask = *mask;
686b4ae6b16SKonstantin Belousov 	frame.sf_uc.uc_stack = td->td_sigstk;
687b4ae6b16SKonstantin Belousov 	frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ?
688b4ae6b16SKonstantin Belousov 	    (onstack ? SS_ONSTACK : 0) : SS_DISABLE;
689b4ae6b16SKonstantin Belousov 	mtx_unlock(&psp->ps_mtx);
690b4ae6b16SKonstantin Belousov 	PROC_UNLOCK(td->td_proc);
691b4ae6b16SKonstantin Belousov 
6927e6437c0SAndrew Turner 	for (int i = 0; ctx_funcs[i] != NULL; i++) {
6937e6437c0SAndrew Turner 		if (!ctx_funcs[i](td, &addr)) {
6947e6437c0SAndrew Turner 			/* Process has trashed its stack. Kill it. */
6957e6437c0SAndrew Turner 			CTR4(KTR_SIG,
6967e6437c0SAndrew Turner 			    "sendsig: frame sigexit td=%p fp=%#lx func[%d]=%p",
6977e6437c0SAndrew Turner 			    td, addr, i, ctx_funcs[i]);
6987e6437c0SAndrew Turner 			PROC_LOCK(p);
6997e6437c0SAndrew Turner 			sigexit(td, SIGILL);
7007e6437c0SAndrew Turner 			/* NOTREACHED */
7017e6437c0SAndrew Turner 		}
7027e6437c0SAndrew Turner 	}
7037e6437c0SAndrew Turner 
7047e6437c0SAndrew Turner 	/* Point at the first context */
7057e6437c0SAndrew Turner 	frame.sf_uc.uc_mcontext.mc_ptr = addr;
7067e6437c0SAndrew Turner 
7077e6437c0SAndrew Turner 	/* Make room, keeping the stack aligned */
7087e6437c0SAndrew Turner 	fp = (struct sigframe *)addr;
7097e6437c0SAndrew Turner 	fp--;
7107e6437c0SAndrew Turner 	fp = (struct sigframe *)STACKALIGN(fp);
7117e6437c0SAndrew Turner 
712b4ae6b16SKonstantin Belousov 	/* Copy the sigframe out to the user's stack. */
713b4ae6b16SKonstantin Belousov 	if (copyout(&frame, fp, sizeof(*fp)) != 0) {
714b4ae6b16SKonstantin Belousov 		/* Process has trashed its stack. Kill it. */
715b4ae6b16SKonstantin Belousov 		CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
716b4ae6b16SKonstantin Belousov 		PROC_LOCK(p);
717b4ae6b16SKonstantin Belousov 		sigexit(td, SIGILL);
718b4ae6b16SKonstantin Belousov 	}
719b4ae6b16SKonstantin Belousov 
720b4ae6b16SKonstantin Belousov 	tf->tf_x[0] = sig;
721b4ae6b16SKonstantin Belousov 	tf->tf_x[1] = (register_t)&fp->sf_si;
722b4ae6b16SKonstantin Belousov 	tf->tf_x[2] = (register_t)&fp->sf_uc;
723db3a1eecSAndrew Turner 	tf->tf_x[8] = (register_t)catcher;
724b4ae6b16SKonstantin Belousov 	tf->tf_sp = (register_t)fp;
725f6ac79fbSKornel Dulęba 	tf->tf_elr = (register_t)PROC_SIGCODE(p);
726b4ae6b16SKonstantin Belousov 
72731cf95ceSAndrew Turner 	/* Clear the single step flag while in the signal handler */
72831cf95ceSAndrew Turner 	if ((td->td_pcb->pcb_flags & PCB_SINGLE_STEP) != 0) {
72931cf95ceSAndrew Turner 		td->td_pcb->pcb_flags &= ~PCB_SINGLE_STEP;
73031cf95ceSAndrew Turner 		WRITE_SPECIALREG(mdscr_el1,
73131cf95ceSAndrew Turner 		    READ_SPECIALREG(mdscr_el1) & ~MDSCR_SS);
73231cf95ceSAndrew Turner 		isb();
73331cf95ceSAndrew Turner 	}
73431cf95ceSAndrew Turner 
735b4ae6b16SKonstantin Belousov 	CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_elr,
736b4ae6b16SKonstantin Belousov 	    tf->tf_sp);
737b4ae6b16SKonstantin Belousov 
738b4ae6b16SKonstantin Belousov 	PROC_LOCK(p);
739b4ae6b16SKonstantin Belousov 	mtx_lock(&psp->ps_mtx);
740b4ae6b16SKonstantin Belousov }
741