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