xref: /freebsd/sys/arm64/arm64/exec_machdep.c (revision 535af610)
1 /*-
2  * Copyright (c) 2014 Andrew Turner
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  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/exec.h>
34 #include <sys/imgact.h>
35 #include <sys/kdb.h>
36 #include <sys/kernel.h>
37 #include <sys/ktr.h>
38 #include <sys/limits.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/proc.h>
42 #include <sys/ptrace.h>
43 #include <sys/reg.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>
50 
51 #include <vm/vm.h>
52 #include <vm/vm_param.h>
53 #include <vm/pmap.h>
54 #include <vm/vm_map.h>
55 
56 #include <machine/armreg.h>
57 #include <machine/kdb.h>
58 #include <machine/md_var.h>
59 #include <machine/pcb.h>
60 
61 #ifdef VFP
62 #include <machine/vfp.h>
63 #endif
64 
65 _Static_assert(sizeof(mcontext_t) == 880, "mcontext_t size incorrect");
66 _Static_assert(sizeof(ucontext_t) == 960, "ucontext_t size incorrect");
67 _Static_assert(sizeof(siginfo_t) == 80, "siginfo_t size incorrect");
68 
69 static void get_fpcontext(struct thread *td, mcontext_t *mcp);
70 static void set_fpcontext(struct thread *td, mcontext_t *mcp);
71 
72 int
73 fill_regs(struct thread *td, struct reg *regs)
74 {
75 	struct trapframe *frame;
76 
77 	frame = td->td_frame;
78 	regs->sp = frame->tf_sp;
79 	regs->lr = frame->tf_lr;
80 	regs->elr = frame->tf_elr;
81 	regs->spsr = frame->tf_spsr;
82 
83 	memcpy(regs->x, frame->tf_x, sizeof(regs->x));
84 
85 #ifdef COMPAT_FREEBSD32
86 	/*
87 	 * We may be called here for a 32bits process, if we're using a
88 	 * 64bits debugger. If so, put PC and SPSR where it expects it.
89 	 */
90 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
91 		regs->x[15] = frame->tf_elr;
92 		regs->x[16] = frame->tf_spsr;
93 	}
94 #endif
95 	return (0);
96 }
97 
98 int
99 set_regs(struct thread *td, struct reg *regs)
100 {
101 	struct trapframe *frame;
102 
103 	frame = td->td_frame;
104 	frame->tf_sp = regs->sp;
105 	frame->tf_lr = regs->lr;
106 
107 	memcpy(frame->tf_x, regs->x, sizeof(frame->tf_x));
108 
109 #ifdef COMPAT_FREEBSD32
110 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
111 		/*
112 		 * We may be called for a 32bits process if we're using
113 		 * a 64bits debugger. If so, get PC and SPSR from where
114 		 * it put it.
115 		 */
116 		frame->tf_elr = regs->x[15];
117 		frame->tf_spsr &= ~PSR_SETTABLE_32;
118 		frame->tf_spsr |= regs->x[16] & PSR_SETTABLE_32;
119 		/* Don't allow userspace to ask to continue single stepping.
120 		 * The SPSR.SS field doesn't exist when the EL1 is AArch32.
121 		 * As the SPSR.DIT field has moved in its place don't
122 		 * allow userspace to set the SPSR.SS field.
123 		 */
124 	} else
125 #endif
126 	{
127 		frame->tf_elr = regs->elr;
128 		/*
129 		 * frame->tf_spsr and regs->spsr on FreeBSD 13 was 32-bit
130 		 * where from 14 they are 64 bit. As PSR_SETTABLE_64 clears
131 		 * the upper 32 bits no compatibility handling is needed,
132 		 * however if this is ever not the case we will need to add
133 		 * these, similar to how it is done in set_mcontext.
134 		 */
135 		frame->tf_spsr &= ~PSR_SETTABLE_64;
136 		frame->tf_spsr |= regs->spsr & PSR_SETTABLE_64;
137 		/* Enable single stepping if userspace asked fot it */
138 		if ((frame->tf_spsr & PSR_SS) != 0) {
139 			td->td_pcb->pcb_flags |= PCB_SINGLE_STEP;
140 
141 			WRITE_SPECIALREG(mdscr_el1,
142 			    READ_SPECIALREG(mdscr_el1) | MDSCR_SS);
143 			isb();
144 		}
145 	}
146 	return (0);
147 }
148 
149 int
150 fill_fpregs(struct thread *td, struct fpreg *regs)
151 {
152 #ifdef VFP
153 	struct pcb *pcb;
154 
155 	pcb = td->td_pcb;
156 	if ((pcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
157 		/*
158 		 * If we have just been running VFP instructions we will
159 		 * need to save the state to memcpy it below.
160 		 */
161 		if (td == curthread)
162 			vfp_save_state(td, pcb);
163 	}
164 
165 	KASSERT(pcb->pcb_fpusaved == &pcb->pcb_fpustate,
166 	    ("Called fill_fpregs while the kernel is using the VFP"));
167 	memcpy(regs->fp_q, pcb->pcb_fpustate.vfp_regs,
168 	    sizeof(regs->fp_q));
169 	regs->fp_cr = pcb->pcb_fpustate.vfp_fpcr;
170 	regs->fp_sr = pcb->pcb_fpustate.vfp_fpsr;
171 #else
172 	memset(regs, 0, sizeof(*regs));
173 #endif
174 	return (0);
175 }
176 
177 int
178 set_fpregs(struct thread *td, struct fpreg *regs)
179 {
180 #ifdef VFP
181 	struct pcb *pcb;
182 
183 	pcb = td->td_pcb;
184 	KASSERT(pcb->pcb_fpusaved == &pcb->pcb_fpustate,
185 	    ("Called set_fpregs while the kernel is using the VFP"));
186 	memcpy(pcb->pcb_fpustate.vfp_regs, regs->fp_q, sizeof(regs->fp_q));
187 	pcb->pcb_fpustate.vfp_fpcr = regs->fp_cr;
188 	pcb->pcb_fpustate.vfp_fpsr = regs->fp_sr;
189 #endif
190 	return (0);
191 }
192 
193 int
194 fill_dbregs(struct thread *td, struct dbreg *regs)
195 {
196 	struct debug_monitor_state *monitor;
197 	int i;
198 	uint8_t debug_ver, nbkpts, nwtpts;
199 
200 	memset(regs, 0, sizeof(*regs));
201 
202 	extract_user_id_field(ID_AA64DFR0_EL1, ID_AA64DFR0_DebugVer_SHIFT,
203 	    &debug_ver);
204 	extract_user_id_field(ID_AA64DFR0_EL1, ID_AA64DFR0_BRPs_SHIFT,
205 	    &nbkpts);
206 	extract_user_id_field(ID_AA64DFR0_EL1, ID_AA64DFR0_WRPs_SHIFT,
207 	    &nwtpts);
208 
209 	/*
210 	 * The BRPs field contains the number of breakpoints - 1. Armv8-A
211 	 * allows the hardware to provide 2-16 breakpoints so this won't
212 	 * overflow an 8 bit value. The same applies to the WRPs field.
213 	 */
214 	nbkpts++;
215 	nwtpts++;
216 
217 	regs->db_debug_ver = debug_ver;
218 	regs->db_nbkpts = nbkpts;
219 	regs->db_nwtpts = nwtpts;
220 
221 	monitor = &td->td_pcb->pcb_dbg_regs;
222 	if ((monitor->dbg_flags & DBGMON_ENABLED) != 0) {
223 		for (i = 0; i < nbkpts; i++) {
224 			regs->db_breakregs[i].dbr_addr = monitor->dbg_bvr[i];
225 			regs->db_breakregs[i].dbr_ctrl = monitor->dbg_bcr[i];
226 		}
227 		for (i = 0; i < nwtpts; i++) {
228 			regs->db_watchregs[i].dbw_addr = monitor->dbg_wvr[i];
229 			regs->db_watchregs[i].dbw_ctrl = monitor->dbg_wcr[i];
230 		}
231 	}
232 
233 	return (0);
234 }
235 
236 int
237 set_dbregs(struct thread *td, struct dbreg *regs)
238 {
239 	struct debug_monitor_state *monitor;
240 	uint64_t addr;
241 	uint32_t ctrl;
242 	int i;
243 
244 	monitor = &td->td_pcb->pcb_dbg_regs;
245 	monitor->dbg_enable_count = 0;
246 
247 	for (i = 0; i < DBG_BRP_MAX; i++) {
248 		addr = regs->db_breakregs[i].dbr_addr;
249 		ctrl = regs->db_breakregs[i].dbr_ctrl;
250 
251 		/*
252 		 * Don't let the user set a breakpoint on a kernel or
253 		 * non-canonical user address.
254 		 */
255 		if (addr >= VM_MAXUSER_ADDRESS)
256 			return (EINVAL);
257 
258 		/*
259 		 * The lowest 2 bits are ignored, so record the effective
260 		 * address.
261 		 */
262 		addr = rounddown2(addr, 4);
263 
264 		/*
265 		 * Some control fields are ignored, and other bits reserved.
266 		 * Only unlinked, address-matching breakpoints are supported.
267 		 *
268 		 * XXX: fields that appear unvalidated, such as BAS, have
269 		 * constrained undefined behaviour. If the user mis-programs
270 		 * these, there is no risk to the system.
271 		 */
272 		ctrl &= DBGBCR_EN | DBGBCR_PMC | DBGBCR_BAS;
273 		if ((ctrl & DBGBCR_EN) != 0) {
274 			/* Only target EL0. */
275 			if ((ctrl & DBGBCR_PMC) != DBGBCR_PMC_EL0)
276 				return (EINVAL);
277 
278 			monitor->dbg_enable_count++;
279 		}
280 
281 		monitor->dbg_bvr[i] = addr;
282 		monitor->dbg_bcr[i] = ctrl;
283 	}
284 
285 	for (i = 0; i < DBG_WRP_MAX; i++) {
286 		addr = regs->db_watchregs[i].dbw_addr;
287 		ctrl = regs->db_watchregs[i].dbw_ctrl;
288 
289 		/*
290 		 * Don't let the user set a watchpoint on a kernel or
291 		 * non-canonical user address.
292 		 */
293 		if (addr >= VM_MAXUSER_ADDRESS)
294 			return (EINVAL);
295 
296 		/*
297 		 * Some control fields are ignored, and other bits reserved.
298 		 * Only unlinked watchpoints are supported.
299 		 */
300 		ctrl &= DBGWCR_EN | DBGWCR_PAC | DBGWCR_LSC | DBGWCR_BAS |
301 		    DBGWCR_MASK;
302 
303 		if ((ctrl & DBGWCR_EN) != 0) {
304 			/* Only target EL0. */
305 			if ((ctrl & DBGWCR_PAC) != DBGWCR_PAC_EL0)
306 				return (EINVAL);
307 
308 			/* Must set at least one of the load/store bits. */
309 			if ((ctrl & DBGWCR_LSC) == 0)
310 				return (EINVAL);
311 
312 			/*
313 			 * When specifying the address range with BAS, the MASK
314 			 * field must be zero.
315 			 */
316 			if ((ctrl & DBGWCR_BAS) != DBGWCR_BAS &&
317 			    (ctrl & DBGWCR_MASK) != 0)
318 				return (EINVAL);
319 
320 			monitor->dbg_enable_count++;
321 		}
322 		monitor->dbg_wvr[i] = addr;
323 		monitor->dbg_wcr[i] = ctrl;
324 	}
325 
326 	if (monitor->dbg_enable_count > 0)
327 		monitor->dbg_flags |= DBGMON_ENABLED;
328 
329 	return (0);
330 }
331 
332 #ifdef COMPAT_FREEBSD32
333 int
334 fill_regs32(struct thread *td, struct reg32 *regs)
335 {
336 	int i;
337 	struct trapframe *tf;
338 
339 	tf = td->td_frame;
340 	for (i = 0; i < 13; i++)
341 		regs->r[i] = tf->tf_x[i];
342 	/* For arm32, SP is r13 and LR is r14 */
343 	regs->r_sp = tf->tf_x[13];
344 	regs->r_lr = tf->tf_x[14];
345 	regs->r_pc = tf->tf_elr;
346 	regs->r_cpsr = tf->tf_spsr;
347 
348 	return (0);
349 }
350 
351 int
352 set_regs32(struct thread *td, struct reg32 *regs)
353 {
354 	int i;
355 	struct trapframe *tf;
356 
357 	tf = td->td_frame;
358 	for (i = 0; i < 13; i++)
359 		tf->tf_x[i] = regs->r[i];
360 	/* For arm 32, SP is r13 an LR is r14 */
361 	tf->tf_x[13] = regs->r_sp;
362 	tf->tf_x[14] = regs->r_lr;
363 	tf->tf_elr = regs->r_pc;
364 	tf->tf_spsr &= ~PSR_SETTABLE_32;
365 	tf->tf_spsr |= regs->r_cpsr & PSR_SETTABLE_32;
366 
367 	return (0);
368 }
369 
370 /* XXX fill/set dbregs/fpregs are stubbed on 32-bit arm. */
371 int
372 fill_fpregs32(struct thread *td, struct fpreg32 *regs)
373 {
374 
375 	memset(regs, 0, sizeof(*regs));
376 	return (0);
377 }
378 
379 int
380 set_fpregs32(struct thread *td, struct fpreg32 *regs)
381 {
382 
383 	return (0);
384 }
385 
386 int
387 fill_dbregs32(struct thread *td, struct dbreg32 *regs)
388 {
389 
390 	memset(regs, 0, sizeof(*regs));
391 	return (0);
392 }
393 
394 int
395 set_dbregs32(struct thread *td, struct dbreg32 *regs)
396 {
397 
398 	return (0);
399 }
400 #endif
401 
402 void
403 exec_setregs(struct thread *td, struct image_params *imgp, uintptr_t stack)
404 {
405 	struct trapframe *tf = td->td_frame;
406 	struct pcb *pcb = td->td_pcb;
407 
408 	memset(tf, 0, sizeof(struct trapframe));
409 
410 	tf->tf_x[0] = stack;
411 	tf->tf_sp = STACKALIGN(stack);
412 	tf->tf_lr = imgp->entry_addr;
413 	tf->tf_elr = imgp->entry_addr;
414 
415 	td->td_pcb->pcb_tpidr_el0 = 0;
416 	td->td_pcb->pcb_tpidrro_el0 = 0;
417 	WRITE_SPECIALREG(tpidrro_el0, 0);
418 	WRITE_SPECIALREG(tpidr_el0, 0);
419 
420 #ifdef VFP
421 	vfp_reset_state(td, pcb);
422 #endif
423 
424 	/*
425 	 * Clear debug register state. It is not applicable to the new process.
426 	 */
427 	bzero(&pcb->pcb_dbg_regs, sizeof(pcb->pcb_dbg_regs));
428 
429 	/* Generate new pointer authentication keys */
430 	ptrauth_exec(td);
431 }
432 
433 /* Sanity check these are the same size, they will be memcpy'd to and from */
434 CTASSERT(sizeof(((struct trapframe *)0)->tf_x) ==
435     sizeof((struct gpregs *)0)->gp_x);
436 CTASSERT(sizeof(((struct trapframe *)0)->tf_x) ==
437     sizeof((struct reg *)0)->x);
438 
439 int
440 get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
441 {
442 	struct trapframe *tf = td->td_frame;
443 
444 	if (clear_ret & GET_MC_CLEAR_RET) {
445 		mcp->mc_gpregs.gp_x[0] = 0;
446 		mcp->mc_gpregs.gp_spsr = tf->tf_spsr & ~PSR_C;
447 	} else {
448 		mcp->mc_gpregs.gp_x[0] = tf->tf_x[0];
449 		mcp->mc_gpregs.gp_spsr = tf->tf_spsr;
450 	}
451 
452 	memcpy(&mcp->mc_gpregs.gp_x[1], &tf->tf_x[1],
453 	    sizeof(mcp->mc_gpregs.gp_x[1]) * (nitems(mcp->mc_gpregs.gp_x) - 1));
454 
455 	mcp->mc_gpregs.gp_sp = tf->tf_sp;
456 	mcp->mc_gpregs.gp_lr = tf->tf_lr;
457 	mcp->mc_gpregs.gp_elr = tf->tf_elr;
458 	get_fpcontext(td, mcp);
459 
460 	return (0);
461 }
462 
463 int
464 set_mcontext(struct thread *td, mcontext_t *mcp)
465 {
466 #define	PSR_13_MASK	0xfffffffful
467 	struct trapframe *tf = td->td_frame;
468 	uint64_t spsr;
469 
470 	spsr = mcp->mc_gpregs.gp_spsr;
471 #ifdef COMPAT_FREEBSD13
472 	if (td->td_proc->p_osrel < P_OSREL_ARM64_SPSR) {
473 		/*
474 		 * Before FreeBSD 14 gp_spsr was 32 bit. The size of mc_gpregs
475 		 * was identical because of padding so mask of the upper bits
476 		 * that may be invalid on earlier releases.
477 		 */
478 		spsr &= PSR_13_MASK;
479 	}
480 #endif
481 
482 	if ((spsr & PSR_M_MASK) != PSR_M_EL0t ||
483 	    (spsr & PSR_AARCH32) != 0 ||
484 	    (spsr & PSR_DAIF) != (td->td_frame->tf_spsr & PSR_DAIF))
485 		return (EINVAL);
486 
487 	memcpy(tf->tf_x, mcp->mc_gpregs.gp_x, sizeof(tf->tf_x));
488 
489 	tf->tf_sp = mcp->mc_gpregs.gp_sp;
490 	tf->tf_lr = mcp->mc_gpregs.gp_lr;
491 	tf->tf_elr = mcp->mc_gpregs.gp_elr;
492 #ifdef COMPAT_FREEBSD13
493 	if (td->td_proc->p_osrel < P_OSREL_ARM64_SPSR) {
494 		/* Keep the upper 32 bits of spsr on older releases */
495 		tf->tf_spsr &= ~PSR_13_MASK;
496 		tf->tf_spsr |= spsr;
497 	} else
498 #endif
499 		tf->tf_spsr = spsr;
500 	if ((tf->tf_spsr & PSR_SS) != 0) {
501 		td->td_pcb->pcb_flags |= PCB_SINGLE_STEP;
502 
503 		WRITE_SPECIALREG(mdscr_el1,
504 		    READ_SPECIALREG(mdscr_el1) | MDSCR_SS);
505 		isb();
506 	}
507 	set_fpcontext(td, mcp);
508 
509 	return (0);
510 #undef PSR_13_MASK
511 }
512 
513 static void
514 get_fpcontext(struct thread *td, mcontext_t *mcp)
515 {
516 #ifdef VFP
517 	struct pcb *curpcb;
518 
519 	MPASS(td == curthread);
520 
521 	curpcb = curthread->td_pcb;
522 	if ((curpcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
523 		/*
524 		 * If we have just been running VFP instructions we will
525 		 * need to save the state to memcpy it below.
526 		 */
527 		vfp_save_state(td, curpcb);
528 	}
529 
530 	KASSERT(curpcb->pcb_fpusaved == &curpcb->pcb_fpustate,
531 	    ("Called get_fpcontext while the kernel is using the VFP"));
532 	KASSERT((curpcb->pcb_fpflags & ~PCB_FP_USERMASK) == 0,
533 	    ("Non-userspace FPU flags set in get_fpcontext"));
534 	memcpy(mcp->mc_fpregs.fp_q, curpcb->pcb_fpustate.vfp_regs,
535 	    sizeof(mcp->mc_fpregs.fp_q));
536 	mcp->mc_fpregs.fp_cr = curpcb->pcb_fpustate.vfp_fpcr;
537 	mcp->mc_fpregs.fp_sr = curpcb->pcb_fpustate.vfp_fpsr;
538 	mcp->mc_fpregs.fp_flags = curpcb->pcb_fpflags;
539 	mcp->mc_flags |= _MC_FP_VALID;
540 #endif
541 }
542 
543 static void
544 set_fpcontext(struct thread *td, mcontext_t *mcp)
545 {
546 #ifdef VFP
547 	struct pcb *curpcb;
548 
549 	MPASS(td == curthread);
550 	if ((mcp->mc_flags & _MC_FP_VALID) != 0) {
551 		curpcb = curthread->td_pcb;
552 
553 		/*
554 		 * Discard any vfp state for the current thread, we
555 		 * are about to override it.
556 		 */
557 		critical_enter();
558 		vfp_discard(td);
559 		critical_exit();
560 
561 		KASSERT(curpcb->pcb_fpusaved == &curpcb->pcb_fpustate,
562 		    ("Called set_fpcontext while the kernel is using the VFP"));
563 		memcpy(curpcb->pcb_fpustate.vfp_regs, mcp->mc_fpregs.fp_q,
564 		    sizeof(mcp->mc_fpregs.fp_q));
565 		curpcb->pcb_fpustate.vfp_fpcr = mcp->mc_fpregs.fp_cr;
566 		curpcb->pcb_fpustate.vfp_fpsr = mcp->mc_fpregs.fp_sr;
567 		curpcb->pcb_fpflags = mcp->mc_fpregs.fp_flags & PCB_FP_USERMASK;
568 	}
569 #endif
570 }
571 
572 int
573 sys_sigreturn(struct thread *td, struct sigreturn_args *uap)
574 {
575 	ucontext_t uc;
576 	int error;
577 
578 	if (copyin(uap->sigcntxp, &uc, sizeof(uc)))
579 		return (EFAULT);
580 
581 	error = set_mcontext(td, &uc.uc_mcontext);
582 	if (error != 0)
583 		return (error);
584 
585 	/* Restore signal mask. */
586 	kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
587 
588 	return (EJUSTRETURN);
589 }
590 
591 void
592 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
593 {
594 	struct thread *td;
595 	struct proc *p;
596 	struct trapframe *tf;
597 	struct sigframe *fp, frame;
598 	struct sigacts *psp;
599 	int onstack, sig;
600 
601 	td = curthread;
602 	p = td->td_proc;
603 	PROC_LOCK_ASSERT(p, MA_OWNED);
604 
605 	sig = ksi->ksi_signo;
606 	psp = p->p_sigacts;
607 	mtx_assert(&psp->ps_mtx, MA_OWNED);
608 
609 	tf = td->td_frame;
610 	onstack = sigonstack(tf->tf_sp);
611 
612 	CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
613 	    catcher, sig);
614 
615 	/* Allocate and validate space for the signal handler context. */
616 	if ((td->td_pflags & TDP_ALTSTACK) != 0 && !onstack &&
617 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
618 		fp = (struct sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
619 		    td->td_sigstk.ss_size);
620 #if defined(COMPAT_43)
621 		td->td_sigstk.ss_flags |= SS_ONSTACK;
622 #endif
623 	} else {
624 		fp = (struct sigframe *)td->td_frame->tf_sp;
625 	}
626 
627 	/* Make room, keeping the stack aligned */
628 	fp--;
629 	fp = (struct sigframe *)STACKALIGN(fp);
630 
631 	/* Fill in the frame to copy out */
632 	bzero(&frame, sizeof(frame));
633 	get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
634 	frame.sf_si = ksi->ksi_info;
635 	frame.sf_uc.uc_sigmask = *mask;
636 	frame.sf_uc.uc_stack = td->td_sigstk;
637 	frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ?
638 	    (onstack ? SS_ONSTACK : 0) : SS_DISABLE;
639 	mtx_unlock(&psp->ps_mtx);
640 	PROC_UNLOCK(td->td_proc);
641 
642 	/* Copy the sigframe out to the user's stack. */
643 	if (copyout(&frame, fp, sizeof(*fp)) != 0) {
644 		/* Process has trashed its stack. Kill it. */
645 		CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
646 		PROC_LOCK(p);
647 		sigexit(td, SIGILL);
648 	}
649 
650 	tf->tf_x[0] = sig;
651 	tf->tf_x[1] = (register_t)&fp->sf_si;
652 	tf->tf_x[2] = (register_t)&fp->sf_uc;
653 	tf->tf_x[8] = (register_t)catcher;
654 	tf->tf_sp = (register_t)fp;
655 	tf->tf_elr = (register_t)PROC_SIGCODE(p);
656 
657 	/* Clear the single step flag while in the signal handler */
658 	if ((td->td_pcb->pcb_flags & PCB_SINGLE_STEP) != 0) {
659 		td->td_pcb->pcb_flags &= ~PCB_SINGLE_STEP;
660 		WRITE_SPECIALREG(mdscr_el1,
661 		    READ_SPECIALREG(mdscr_el1) & ~MDSCR_SS);
662 		isb();
663 	}
664 
665 	CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_elr,
666 	    tf->tf_sp);
667 
668 	PROC_LOCK(p);
669 	mtx_lock(&psp->ps_mtx);
670 }
671