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