sig_machdep.c revision 1.52
1/*	$NetBSD: sig_machdep.c,v 1.52 2020/07/06 09:34:18 rin Exp $	*/
2
3/*
4 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 * Copyright (C) 1995, 1996 TooLs GmbH.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by TooLs GmbH.
19 * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 *    derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include <sys/cdefs.h>
35__KERNEL_RCSID(0, "$NetBSD: sig_machdep.c,v 1.52 2020/07/06 09:34:18 rin Exp $");
36
37#ifdef _KERNEL_OPT
38#include "opt_altivec.h"
39#include "opt_ppcarch.h"
40#endif
41
42#include <sys/param.h>
43#include <sys/mount.h>
44#include <sys/proc.h>
45#include <sys/syscallargs.h>
46#include <sys/systm.h>
47#include <sys/ucontext.h>
48#include <sys/cpu.h>
49
50#include <uvm/uvm_extern.h>
51
52#include <powerpc/fpu.h>
53#include <powerpc/altivec.h>
54#include <powerpc/pcb.h>
55#include <powerpc/psl.h>
56
57/*
58 * Send a signal to process.
59 */
60void
61sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
62{
63	struct lwp * const l = curlwp;
64	struct proc * const p = l->l_proc;
65	struct trapframe * const tf = l->l_md.md_utf;
66	struct sigaltstack * const ss = &l->l_sigstk;
67	const struct sigact_sigdesc * const sd =
68	    &p->p_sigacts->sa_sigdesc[ksi->ksi_signo];
69	/* save handler before sendsig_reset trashes it! */
70	const void * const handler = sd->sd_sigact.sa_handler;
71	ucontext_t uc;
72	vaddr_t sp, sip, ucp;
73	int onstack, error;
74
75	/* Do we need to jump onto the signal stack? */
76	onstack = (ss->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
77	    (sd->sd_sigact.sa_flags & SA_ONSTACK) != 0;
78
79	/* Find top of stack.  */
80	sp = (onstack ? (vaddr_t)ss->ss_sp + ss->ss_size : tf->tf_fixreg[1]);
81	sp &= ~(CALLFRAMELEN-1);
82
83	/* Allocate space for the ucontext.  */
84	sp -= sizeof(ucontext_t);
85	ucp = sp;
86
87	/* Allocate space for the siginfo.  */
88	sp -= sizeof(siginfo_t);
89	sip = sp;
90
91	sp &= ~(CALLFRAMELEN-1);
92
93	/* Save register context. */
94	memset(&uc, 0, sizeof(uc));
95	uc.uc_flags = _UC_SIGMASK;
96	uc.uc_flags |= (ss->ss_flags & SS_ONSTACK) ?
97	    _UC_SETSTACK : _UC_CLRSTACK;
98	uc.uc_sigmask = *mask;
99	uc.uc_link = l->l_ctxlink;
100	sendsig_reset(l, ksi->ksi_signo);
101	mutex_exit(p->p_lock);
102	cpu_getmcontext(l, &uc.uc_mcontext, &uc.uc_flags);
103
104	/*
105	 * Copy the siginfo and ucontext onto the user's stack.
106	 */
107	error = (copyout(&ksi->ksi_info, (void *)sip, sizeof(ksi->ksi_info)) != 0 ||
108	    copyout(&uc, (void *)ucp, sizeof(uc)) != 0);
109	mutex_enter(p->p_lock);
110
111	if (error) {
112		/*
113		 * Process has trashed its stack; give it an illegal
114		 * instruction to halt it in its tracks.
115		 */
116		sigexit(l, SIGILL);
117		/* NOTREACHED */
118	}
119
120	/*
121	 * Build context to run handler in.  Note the trampoline version
122	 * numbers are coordinated with machine-dependent code in libc.
123	 */
124	switch (sd->sd_vers) {
125	case 2:		/* siginfo sigtramp */
126		tf->tf_fixreg[1]  = (register_t)sp - CALLFRAMELEN;
127		tf->tf_fixreg[3]  = (register_t)ksi->ksi_signo;
128		tf->tf_fixreg[4]  = (register_t)sip;
129		tf->tf_fixreg[5]  = (register_t)ucp;
130		/* Preserve ucp across call to signal function */
131		tf->tf_fixreg[30] = (register_t)ucp;
132		tf->tf_lr         = (register_t)sd->sd_tramp;
133		tf->tf_srr0       = (register_t)handler;
134		break;
135
136	default:
137		goto nosupport;
138	}
139
140	/* Remember that we're now on the signal stack. */
141	if (onstack)
142		ss->ss_flags |= SS_ONSTACK;
143	return;
144
145 nosupport:
146	/* Don't know what trampoline version; kill it. */
147	printf("sendsig_siginfo(sig %d): bad version %d\n",
148	    ksi->ksi_signo, sd->sd_vers);
149	sigexit(l, SIGILL);
150	/* NOTREACHED */
151}
152
153void
154cpu_getmcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flagp)
155{
156	const struct trapframe * const tf = l->l_md.md_utf;
157	__greg_t * const gr = mcp->__gregs;
158#if defined(PPC_HAVE_FPU)
159	struct pcb * const pcb = lwp_getpcb(l);
160#endif
161
162	/* Save GPR context. */
163	(void)memcpy(gr, &tf->tf_fixreg, 32 * sizeof (gr[0])); /* GR0-31 */
164	gr[_REG_CR]  = tf->tf_cr;
165	gr[_REG_LR]  = tf->tf_lr;
166	gr[_REG_PC]  = tf->tf_srr0;
167	gr[_REG_MSR] = tf->tf_srr1 & PSL_USERSRR1;
168#ifdef PPC_HAVE_FPU
169	gr[_REG_MSR] |= pcb->pcb_flags & (PCB_FE0|PCB_FE1);
170#endif
171	gr[_REG_CTR] = tf->tf_ctr;
172	gr[_REG_XER] = tf->tf_xer;
173#ifdef PPC_OEA
174	gr[_REG_MQ]  = tf->tf_mq;
175#else
176	gr[_REG_MQ]  = 0;
177#endif
178
179	*flagp |= _UC_CPU;
180	*flagp |= _UC_TLSBASE;
181
182#ifdef PPC_HAVE_FPU
183	/* Save FPU context, if any. */
184	if (!fpu_save_to_mcontext(l, mcp, flagp))
185#endif
186		memset(&mcp->__fpregs, 0, sizeof(mcp->__fpregs));
187
188#if defined(ALTIVEC) || defined(PPC_HAVE_SPE)
189	/* Save vector context, if any. */
190	if (!vec_save_to_mcontext(l, mcp, flagp))
191#endif
192		memset(&mcp->__vrf, 0, sizeof (mcp->__vrf));
193}
194
195int
196cpu_mcontext_validate(struct lwp *l, const mcontext_t *mcp)
197{
198	return 0;
199}
200
201int
202cpu_setmcontext(struct lwp *l, const mcontext_t *mcp, unsigned int flags)
203{
204	struct trapframe * const tf = l->l_md.md_utf;
205	const __greg_t * const gr = mcp->__gregs;
206	struct proc * const p = l->l_proc;
207	int error;
208
209	/* Restore GPR context, if any. */
210	if (flags & _UC_CPU) {
211		error = cpu_mcontext_validate(l, mcp);
212		if (error)
213			return error;
214
215#ifdef PPC_HAVE_FPU
216		/*
217		 * Always save the FP exception mode in the PCB.
218		 */
219		struct pcb * const pcb = lwp_getpcb(l);
220		pcb->pcb_flags &= ~(PCB_FE0|PCB_FE1);
221		pcb->pcb_flags |= gr[_REG_MSR] & (PCB_FE0|PCB_FE1);
222#endif
223
224		/*
225		 * R2 is the TLS register so avoid updating it here.
226		 */
227
228		__greg_t save_r2 = tf->tf_fixreg[_REG_R2];
229		(void)memcpy(&tf->tf_fixreg, gr, 32 * sizeof (gr[0]));
230		tf->tf_fixreg[_REG_R2] = save_r2;
231		tf->tf_cr   = gr[_REG_CR];
232		tf->tf_lr   = gr[_REG_LR];
233		tf->tf_srr0 = gr[_REG_PC];
234
235		/*
236		 * Accept all user-settable bits without complaint;
237		 * userland should not need to know the machine-specific
238		 * MSR value.
239		 */
240		tf->tf_srr1 = (gr[_REG_MSR] & PSL_USERMOD) | PSL_USERSET;
241		tf->tf_ctr  = gr[_REG_CTR];
242		tf->tf_xer  = gr[_REG_XER];
243#ifdef PPC_OEA
244		tf->tf_mq = gr[_REG_MQ];
245#endif
246	}
247
248	if (flags & _UC_TLSBASE)
249		lwp_setprivate(l, (void *)(uintptr_t)gr[_REG_R2]);
250
251#ifdef PPC_HAVE_FPU
252	/* Restore FPU context, if any. */
253	if (flags & _UC_FPU)
254		fpu_restore_from_mcontext(l, mcp);
255#endif
256
257#ifdef ALTIVEC
258	/* Restore AltiVec context, if any. */
259	if (flags & _UC_POWERPC_VEC)
260		vec_restore_from_mcontext(l, mcp);
261#endif
262
263#ifdef PPC_HAVE_SPE
264	/* Restore SPE context, if any. */
265	if (flags & _UC_POWERPC_SPE)
266		vec_restore_from_mcontext(l, mcp);
267#endif
268
269	mutex_enter(p->p_lock);
270	if (flags & _UC_SETSTACK)
271		l->l_sigstk.ss_flags |= SS_ONSTACK;
272	if (flags & _UC_CLRSTACK)
273		l->l_sigstk.ss_flags &= ~SS_ONSTACK;
274	mutex_exit(p->p_lock);
275
276	return (0);
277}
278
279int
280cpu_lwp_setprivate(lwp_t *l, void *addr)
281{
282	struct trapframe * const tf = l->l_md.md_utf;
283
284	tf->tf_fixreg[_REG_R2] = (register_t)addr;
285
286	return 0;
287}
288