sig_machdep.c revision 1.35
1/*	$NetBSD: sig_machdep.c,v 1.35 2009/11/21 17:40:29 rmind 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.35 2009/11/21 17:40:29 rmind Exp $");
36
37#include "opt_ppcarch.h"
38#include "opt_altivec.h"
39
40#include <sys/param.h>
41#include <sys/mount.h>
42#include <sys/proc.h>
43#include <sys/syscallargs.h>
44#include <sys/systm.h>
45#include <sys/ucontext.h>
46
47#include <powerpc/fpu.h>
48#include <powerpc/altivec.h>
49
50/*
51 * Send a signal to process.
52 */
53void
54sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
55{
56	struct lwp * const l = curlwp;
57	struct proc * const p = l->l_proc;
58	struct trapframe * const tf = trapframe(l);
59	struct sigaltstack *ss = &l->l_sigstk;
60	const struct sigact_sigdesc *sd =
61	    &p->p_sigacts->sa_sigdesc[ksi->ksi_signo];
62	ucontext_t uc;
63	vaddr_t sp, sip, ucp;
64	int onstack, error;
65
66	/* Do we need to jump onto the signal stack? */
67	onstack = (ss->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
68	    (sd->sd_sigact.sa_flags & SA_ONSTACK) != 0;
69
70	/* Find top of stack.  */
71	sp = (onstack ? (vaddr_t)ss->ss_sp + ss->ss_size : tf->fixreg[1]);
72	sp &= ~(CALLFRAMELEN-1);
73
74	/* Allocate space for the ucontext.  */
75	sp -= sizeof(ucontext_t);
76	ucp = sp;
77
78	/* Allocate space for the siginfo.  */
79	sp -= sizeof(siginfo_t);
80	sip = sp;
81
82	sp &= ~(CALLFRAMELEN-1);
83
84	/* Save register context. */
85	uc.uc_flags = _UC_SIGMASK;
86	uc.uc_sigmask = *mask;
87	uc.uc_link = l->l_ctxlink;
88	memset(&uc.uc_stack, 0, sizeof(uc.uc_stack));
89	sendsig_reset(l, ksi->ksi_signo);
90	mutex_exit(p->p_lock);
91	cpu_getmcontext(l, &uc.uc_mcontext, &uc.uc_flags);
92
93	/*
94	 * Copy the siginfo and ucontext onto the user's stack.
95	 */
96	error = (copyout(&ksi->ksi_info, (void *)sip, sizeof(ksi->ksi_info)) != 0 ||
97	    copyout(&uc, (void *)ucp, sizeof(uc)) != 0);
98	mutex_enter(p->p_lock);
99
100	if (error) {
101		/*
102		 * Process has trashed its stack; give it an illegal
103		 * instruction to halt it in its tracks.
104		 */
105		sigexit(l, SIGILL);
106		/* NOTREACHED */
107	}
108
109	/*
110	 * Build context to run handler in.  Note the trampoline version
111	 * numbers are coordinated with machine-dependent code in libc.
112	 */
113	switch (sd->sd_vers) {
114	case 2:		/* siginfo sigtramp */
115		tf->fixreg[1]  = (register_t)sp - CALLFRAMELEN;
116		tf->fixreg[3]  = (register_t)ksi->ksi_signo;
117		tf->fixreg[4]  = (register_t)sip;
118		tf->fixreg[5]  = (register_t)ucp;
119		/* Preserve ucp across call to signal function */
120		tf->fixreg[30] = (register_t)ucp;
121		tf->lr         = (register_t)sd->sd_tramp;
122		tf->srr0       = (register_t)sd->sd_sigact.sa_handler;
123		break;
124
125	default:
126		goto nosupport;
127	}
128
129	/* Remember that we're now on the signal stack. */
130	if (onstack)
131		ss->ss_flags |= SS_ONSTACK;
132	return;
133
134 nosupport:
135	/* Don't know what trampoline version; kill it. */
136	printf("sendsig_siginfo(sig %d): bad version %d\n",
137	    ksi->ksi_signo, sd->sd_vers);
138	sigexit(l, SIGILL);
139	/* NOTREACHED */
140}
141
142void
143cpu_getmcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flagp)
144{
145	const struct trapframe *tf = trapframe(l);
146	__greg_t *gr = mcp->__gregs;
147#if defined(PPC_HAVE_FPU) || defined(ALTIVEC)
148	struct pcb *pcb = lwp_getpcb(l);
149#endif
150
151	/* Save GPR context. */
152	(void)memcpy(gr, &tf->fixreg, 32 * sizeof (gr[0])); /* GR0-31 */
153	gr[_REG_CR]  = tf->cr;
154	gr[_REG_LR]  = tf->lr;
155	gr[_REG_PC]  = tf->srr0;
156	gr[_REG_MSR] = tf->srr1 & PSL_USERSRR1;
157#ifdef PPC_HAVE_FPU
158	gr[_REG_MSR] |= pcb->pcb_flags & (PCB_FE0|PCB_FE1);
159#endif
160#ifdef ALTIVEC
161	gr[_REG_MSR] |= pcb->pcb_flags & PCB_ALTIVEC ? PSL_VEC : 0;
162#endif
163	gr[_REG_CTR] = tf->ctr;
164	gr[_REG_XER] = tf->xer;
165#ifdef PPC_OEA
166	gr[_REG_MQ]  = tf->tf_xtra[TF_MQ];
167#else
168	gr[_REG_MQ]  = 0;
169#endif
170	*flagp |= _UC_CPU;
171
172#ifdef PPC_HAVE_FPU
173	/* Save FPR context, if any. */
174	if ((pcb->pcb_flags & PCB_FPU) != 0) {
175		/* If we're the FPU owner, dump its context to the PCB first. */
176		if (pcb->pcb_fpcpu)
177			save_fpu_lwp(l, FPU_SAVE);
178		(void)memcpy(mcp->__fpregs.__fpu_regs, pcb->pcb_fpu.fpreg,
179		    sizeof (mcp->__fpregs.__fpu_regs));
180		mcp->__fpregs.__fpu_fpscr =
181		    ((int *)&pcb->pcb_fpu.fpscr)[_QUAD_LOWWORD];
182		mcp->__fpregs.__fpu_valid = 1;
183		*flagp |= _UC_FPU;
184	} else
185#endif
186		memset(&mcp->__fpregs, 0, sizeof(mcp->__fpregs));
187
188#ifdef ALTIVEC
189	/* Save AltiVec context, if any. */
190	if ((pcb->pcb_flags & PCB_ALTIVEC) != 0) {
191		/*
192		 * If we're the AltiVec owner, dump its context
193		 * to the PCB first.
194		 */
195		if (pcb->pcb_veccpu)
196			save_vec_lwp(l, ALTIVEC_SAVE);
197		(void)memcpy(mcp->__vrf.__vrs, pcb->pcb_vr.vreg,
198		    sizeof (mcp->__vrf.__vrs));
199		mcp->__vrf.__vscr = pcb->pcb_vr.vscr;
200		mcp->__vrf.__vrsave = pcb->pcb_vr.vrsave;
201		*flagp |= _UC_POWERPC_VEC;
202	} else
203#endif
204		memset(&mcp->__vrf, 0, sizeof (mcp->__vrf));
205}
206
207int
208cpu_setmcontext(struct lwp *l, const mcontext_t *mcp, unsigned int flags)
209{
210	struct trapframe *tf = trapframe(l);
211	const __greg_t *gr = mcp->__gregs;
212#ifdef PPC_HAVE_FPU
213	struct pcb *pcb = lwp_getpcb(l);
214#endif
215
216	/* Restore GPR context, if any. */
217	if (flags & _UC_CPU) {
218#ifdef PPC_HAVE_FPU
219		/*
220		 * Always save the FP exception mode in the PCB.
221		 */
222		pcb->pcb_flags &= ~(PCB_FE0|PCB_FE1);
223		pcb->pcb_flags |= gr[_REG_MSR] & (PCB_FE0|PCB_FE1);
224#endif
225
226		(void)memcpy(&tf->fixreg, gr, 32 * sizeof (gr[0]));
227		tf->cr   = gr[_REG_CR];
228		tf->lr   = gr[_REG_LR];
229		tf->srr0 = gr[_REG_PC];
230		/*
231		 * Accept all user-settable bits without complaint;
232		 * userland should not need to know the machine-specific
233		 * MSR value.
234		 */
235		tf->srr1 = (gr[_REG_MSR] & PSL_USERMOD) | PSL_USERSET;
236		tf->ctr  = gr[_REG_CTR];
237		tf->xer  = gr[_REG_XER];
238#ifdef PPC_OEA
239		tf->tf_xtra[TF_MQ] = gr[_REG_MQ];
240#endif
241	}
242
243#ifdef PPC_HAVE_FPU /* Restore FPR context, if any. */
244	if ((flags & _UC_FPU) && mcp->__fpregs.__fpu_valid != 0) {
245		/* we don't need to save the state, just drop it */
246		save_fpu_lwp(l, FPU_DISCARD);
247		(void)memcpy(&pcb->pcb_fpu.fpreg, &mcp->__fpregs.__fpu_regs,
248		    sizeof (pcb->pcb_fpu.fpreg));
249		((int *)&pcb->pcb_fpu.fpscr)[_QUAD_LOWWORD] =
250		    mcp->__fpregs.__fpu_fpscr;
251	}
252#endif
253
254#ifdef ALTIVEC
255	/* Restore AltiVec context, if any. */
256	if (flags & _UC_POWERPC_VEC) {
257		/* we don't need to save the state, just drop it */
258		save_vec_lwp(l, ALTIVEC_DISCARD);
259		(void)memcpy(pcb->pcb_vr.vreg, &mcp->__vrf.__vrs,
260		    sizeof (pcb->pcb_vr.vreg));
261		pcb->pcb_vr.vscr = mcp->__vrf.__vscr;
262		pcb->pcb_vr.vrsave = mcp->__vrf.__vrsave;
263	}
264#endif
265
266	return (0);
267}
268