sig_machdep.c revision 1.11 1 /* $NetBSD: sig_machdep.c,v 1.11 2004/07/24 19:04:53 chs Exp $ */
2
3 /*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matthew Fredette.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1982, 1986, 1990, 1993
41 * The Regents of the University of California. All rights reserved.
42 *
43 * This code is derived from software contributed to Berkeley by
44 * the Systems Programming Group of the University of Utah Computer
45 * Science Department.
46 *
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
49 * are met:
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
55 * 3. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 * from: Utah Hdr: machdep.c 1.74 92/12/20
72 * from: @(#)machdep.c 8.10 (Berkeley) 4/20/94
73 */
74 /*
75 * Copyright (c) 1988 University of Utah.
76 *
77 * This code is derived from software contributed to Berkeley by
78 * the Systems Programming Group of the University of Utah Computer
79 * Science Department.
80 *
81 * Redistribution and use in source and binary forms, with or without
82 * modification, are permitted provided that the following conditions
83 * are met:
84 * 1. Redistributions of source code must retain the above copyright
85 * notice, this list of conditions and the following disclaimer.
86 * 2. Redistributions in binary form must reproduce the above copyright
87 * notice, this list of conditions and the following disclaimer in the
88 * documentation and/or other materials provided with the distribution.
89 * 3. All advertising materials mentioning features or use of this software
90 * must display the following acknowledgement:
91 * This product includes software developed by the University of
92 * California, Berkeley and its contributors.
93 * 4. Neither the name of the University nor the names of its contributors
94 * may be used to endorse or promote products derived from this software
95 * without specific prior written permission.
96 *
97 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
98 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
99 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
100 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
101 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
102 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
103 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
104 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
105 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
106 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
107 * SUCH DAMAGE.
108 *
109 * from: Utah Hdr: machdep.c 1.74 92/12/20
110 * from: @(#)machdep.c 8.10 (Berkeley) 4/20/94
111 */
112
113 #include <sys/cdefs.h>
114 __KERNEL_RCSID(0, "$NetBSD: sig_machdep.c,v 1.11 2004/07/24 19:04:53 chs Exp $");
115
116 #include "opt_compat_netbsd.h"
117
118 #define __HPPA_SIGNAL_PRIVATE
119
120 #include <sys/param.h>
121 #include <sys/systm.h>
122 #include <sys/kernel.h>
123 #include <sys/proc.h>
124 #include <sys/user.h>
125 #include <sys/signal.h>
126 #include <sys/signalvar.h>
127
128 #include <sys/mount.h>
129 #include <sys/sa.h>
130 #include <sys/syscallargs.h>
131
132 #include <machine/cpu.h>
133 #include <machine/reg.h>
134 #include <machine/frame.h>
135
136 #ifdef DEBUG
137 int sigdebug = 0;
138 int sigpid = 0;
139 #define SDB_FOLLOW 0x01
140 #define SDB_KSTACK 0x02
141 #define SDB_FPSTATE 0x04
142 #endif
143
144 void sendsig_sigcontext(const struct ksiginfo *, const sigset_t *);
145
146 /*
147 * Send an interrupt to process.
148 */
149 void
150 sendsig_sigcontext(const struct ksiginfo *ksi, const sigset_t *mask)
151 {
152 int sig = ksi->ksi_signo;
153 u_long code = ksi->ksi_trap;
154
155 struct lwp *l = curlwp;
156 struct proc *p = l->l_proc;
157 struct sigacts *ps = p->p_sigacts;
158 struct sigframe *fp, kf;
159 caddr_t sp;
160 struct trapframe *tf;
161 int onstack, fsize;
162 sig_t catcher = SIGACTION(p, sig).sa_handler;
163
164 tf = (struct trapframe *)l->l_md.md_regs;
165
166 /* Do we need to jump onto the signal stack? */
167 onstack =
168 (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
169 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
170
171 /*
172 * Allocate space for the signal handler context.
173 * The PA-RISC calling convention mandates that
174 * the stack pointer must always be 64-byte aligned,
175 * and points to the first *unused* byte.
176 */
177 fsize = sizeof(struct sigframe);
178 sp = (onstack ?
179 (caddr_t)p->p_sigctx.ps_sigstk.ss_sp :
180 (caddr_t)tf->tf_sp);
181 sp = (caddr_t)(((u_int)(sp + fsize + 63)) & ~63);
182 fp = (struct sigframe *) (sp - fsize);
183
184 #ifdef DEBUG
185 if ((sigdebug & SDB_FOLLOW) && (!sigpid || p->p_pid == sigpid))
186 printf("sendsig: %s[%d] sig %d catcher %p\n",
187 p->p_comm, p->p_pid, sig, catcher);
188 #endif
189
190 /*
191 * Save necessary hardware state. Currently this includes:
192 * - original exception frame
193 * - FP coprocessor state
194 */
195 kf.sf_state.ss_flags = SS_USERREGS;
196 memcpy(&kf.sf_state.ss_frame, tf, sizeof(*tf));
197 /* XXX FP state */
198
199 /* Build the signal context to be used by sigreturn. */
200 kf.sf_sc.sc_sp = tf->tf_sp;
201 kf.sf_sc.sc_fp = tf->tf_sp; /* XXX fredette - is this right? */
202 kf.sf_sc.sc_ap = (int)&fp->sf_state;
203 kf.sf_sc.sc_pcsqh = tf->tf_iisq_head;
204 kf.sf_sc.sc_pcoqh = tf->tf_iioq_head;
205 kf.sf_sc.sc_pcsqt = tf->tf_iisq_tail;
206 kf.sf_sc.sc_pcoqt = tf->tf_iioq_tail;
207 kf.sf_sc.sc_ps = tf->tf_ipsw;
208
209 /* Save signal stack. */
210 kf.sf_sc.sc_onstack = p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK;
211
212 /* Save signal mask. */
213 kf.sf_sc.sc_mask = *mask;
214
215 /* Fill the calling convention part of the signal frame. */
216 kf.sf_psp = 0;
217 kf.sf_clup = 0; /* XXX fredette - is this right? */
218 kf.sf_sl = 0; /* XXX fredette - is this right? */
219 kf.sf_edp = 0; /* XXX fredette - is this right? */
220
221 /* Copy out the signal frame. */
222 if (copyout(&kf, fp, fsize)) {
223 #ifdef DEBUG
224 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
225 printf("sendsig(%d): copyout failed on sig %d\n",
226 p->p_pid, sig);
227 #endif
228 /*
229 * Process has trashed its stack; give it an illegal
230 * instruction to halt it in its tracks.
231 */
232 sigexit(l, SIGILL);
233 /* NOTREACHED */
234 }
235 #ifdef DEBUG
236 if (sigdebug & SDB_FOLLOW)
237 printf("sendsig(%d): sig %d scp %p fp %p sc_sp %x sc_ap %x\n",
238 p->p_pid, sig, &fp->sf_sc, fp,
239 kf.sf_sc.sc_sp, kf.sf_sc.sc_ap);
240 #endif
241
242 /* Set up the registers to return to sigcode. */
243 switch (ps->sa_sigdesc[sig].sd_vers) {
244 #if 1 /* COMPAT_16 */
245 case 0: /* legacy on-stack sigtramp */
246 tf->tf_iioq_head =
247 (int)p->p_sigctx.ps_sigcode | HPPA_PC_PRIV_USER;
248 tf->tf_iioq_tail = tf->tf_iioq_head + 4;
249 break;
250 #endif
251
252 case 1:
253 tf->tf_iioq_head =
254 (int)ps->sa_sigdesc[sig].sd_tramp | HPPA_PC_PRIV_USER;
255 tf->tf_iioq_tail = tf->tf_iioq_head + 4;
256 break;
257
258 default:
259 /* Don't know what trampoline version; kill it. */
260 sigexit(l, SIGILL);
261 }
262
263 tf->tf_sp = (int)sp;
264 tf->tf_r3 = (int)&fp->sf_sc;
265 tf->tf_arg0 = sig;
266 tf->tf_arg1 = code;
267 tf->tf_arg2 = (int)&fp->sf_sc;
268 tf->tf_arg3 = (int)catcher;
269
270 /* Remember that we're now on the signal stack. */
271 if (onstack)
272 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
273
274 #ifdef DEBUG
275 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
276 printf("sendsig(%d): sig %d returns\n",
277 p->p_pid, sig);
278 #endif
279 }
280
281 void *getframe(struct lwp *, int, int *);
282 void sendsig_siginfo(const struct ksiginfo *, const sigset_t *);
283
284 void *
285 getframe(struct lwp *l, int sig, int *onstack)
286 {
287 struct proc *p = l->l_proc;
288 struct sigctx *ctx = &p->p_sigctx;
289 struct trapframe *tf = l->l_md.md_regs;
290
291 /* Do we need to jump onto the signal stack? */
292 *onstack = (ctx->ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0
293 && (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
294 if (*onstack)
295 return (void *)ctx->ps_sigstk.ss_sp;
296 else
297 return (void *)tf->tf_sp;
298 }
299
300 struct sigframe_siginfo {
301 siginfo_t sf_si;
302 ucontext_t sf_uc;
303 };
304
305 void
306 sendsig_siginfo(const struct ksiginfo *ksi, const sigset_t *mask)
307 {
308 struct lwp *l = curlwp;
309 struct proc *p = l->l_proc;
310 struct sigacts *ps = p->p_sigacts;
311 struct sigframe_siginfo *fp, frame;
312 struct trapframe *tf;
313 int sig = ksi->ksi_signo;
314 sig_t catcher = SIGACTION(p, sig).sa_handler;
315 int onstack;
316
317 fp = getframe(l, sig, &onstack);
318 tf = (struct trapframe *)l->l_md.md_regs;
319
320 /* Build stack frame for signal trampoline. */
321 switch (ps->sa_sigdesc[sig].sd_vers) {
322 default:
323 printf("sendsig_siginfo: bad version %d\n",
324 ps->sa_sigdesc[sig].sd_vers);
325 sigexit(l, SIGILL);
326 case 2:
327 break;
328 }
329
330 frame.sf_si._info = ksi->ksi_info;
331 frame.sf_uc.uc_flags = _UC_SIGMASK |
332 ((p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) ?
333 _UC_SETSTACK : _UC_CLRSTACK);
334 frame.sf_uc.uc_sigmask = *mask;
335 frame.sf_uc.uc_link = NULL;
336 memset(&frame.sf_uc.uc_stack, 0, sizeof(frame.sf_uc.uc_stack));
337 cpu_getmcontext(l, &frame.sf_uc.uc_mcontext, &frame.sf_uc.uc_flags);
338
339 if (copyout(&frame, fp, sizeof(frame)) != 0) {
340
341 /*
342 * Process has trashed its stack; give it an illegal
343 * instruction to halt it in its tracks.
344 */
345 sigexit(l, SIGILL);
346 /* NOTREACHED */
347 }
348
349 /*
350 * Set up the registers to invoke the signal trampoline.
351 */
352 tf->tf_arg0 = sig;
353 tf->tf_arg1 = (__greg_t)&fp->sf_si;
354 tf->tf_arg2 = (__greg_t)&fp->sf_uc;
355 tf->tf_r3 = (__greg_t)&fp->sf_uc;
356
357 tf->tf_iioq_head =
358 (__greg_t)ps->sa_sigdesc[sig].sd_tramp | HPPA_PC_PRIV_USER;
359 tf->tf_iioq_tail = tf->tf_iioq_head + 4;
360 tf->tf_arg3 = (__greg_t)catcher;
361 tf->tf_sp = HPPA_FRAME_ROUND((uintptr_t)fp + sizeof(*fp) +
362 HPPA_FRAME_SIZE);
363
364 /* Remember that we're now on the signal stack. */
365 if (onstack)
366 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
367 }
368
369 void
370 sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
371 {
372 if (curproc->p_sigacts->sa_sigdesc[ksi->ksi_signo].sd_vers < 2)
373 sendsig_sigcontext(ksi, mask);
374 else
375 sendsig_siginfo(ksi, mask);
376 }
377
378 int
379 compat_16_sys___sigreturn14(struct lwp *l, void *v, register_t *retval)
380 {
381 struct compat_16_sys___sigreturn14_args /* {
382 syscallarg(struct sigcontext *) sigcntxp;
383 } */ *uap = v;
384 struct proc *p = l->l_proc;
385 struct sigcontext *scp;
386 struct trapframe *tf;
387 struct sigcontext tsigc;
388 struct sigstate tstate;
389 int rf, flags;
390
391 /*
392 * The trampoline code hands us the context.
393 * It is unsafe to keep track of it ourselves, in the event that a
394 * program jumps out of a signal handler.
395 */
396 scp = SCARG(uap, sigcntxp);
397 #ifdef DEBUG
398 if (sigdebug & SDB_FOLLOW)
399 printf("sigreturn: pid %d, scp %p\n", p->p_pid, scp);
400 #endif
401 if ((int)scp & 3)
402 return (EINVAL);
403
404 if (copyin(scp, &tsigc, sizeof(tsigc)) != 0)
405 return (EFAULT);
406 scp = &tsigc;
407
408 /* Make sure the user isn't pulling a fast one on us! */
409 /* XXX fredette - until this is done, huge security hole here. */
410 /* XXX fredette - requiring that PSL_R be zero will hurt debuggers. */
411 if ((scp->sc_ps & (PSW_MBS|PSW_MBZ)) != PSW_MBS)
412 return (EINVAL);
413
414 /* Restore register context. */
415 tf = (struct trapframe *)l->l_md.md_regs;
416
417 /*
418 * Grab pointer to hardware state information.
419 * If zero, the user is probably doing a longjmp.
420 */
421 if ((rf = scp->sc_ap) == 0)
422 goto restore;
423
424 /*
425 * See if there is anything to do before we go to the
426 * expense of copying in the trapframe
427 */
428 flags = fuword((caddr_t)rf);
429 #ifdef DEBUG
430 if (sigdebug & SDB_FOLLOW)
431 printf("sigreturn(%d): sc_ap %x flags %x\n",
432 p->p_pid, rf, flags);
433 #endif
434 /* fuword failed (bogus sc_ap value). */
435 if (flags == -1)
436 return (EINVAL);
437
438 if (flags == 0 || copyin((caddr_t)rf, &tstate, sizeof(tstate)) != 0)
439 goto restore;
440 #ifdef DEBUG
441 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
442 printf("sigreturn(%d): ssp %p usp %x scp %p\n",
443 p->p_pid, &flags, scp->sc_sp, SCARG(uap, sigcntxp));
444 #endif
445
446 /*
447 * Restore most of the users registers except for those
448 * in the sigcontext; they will be handled below.
449 */
450 if (flags & SS_USERREGS) {
451
452 /*
453 * There are more registers that the user can tell
454 * us to bash than registers that, for security
455 * or other reasons, we must protect. So it's
456 * easier (but not faster), to copy these sensitive
457 * register values into the user-provided frame,
458 * then bulk-copy the user-provided frame into
459 * the process' frame.
460 */
461 #define SIG_PROTECT(r) tstate.ss_frame.r = tf->r
462 /* SRs 5,6,7 must be protected. */
463 SIG_PROTECT(tf_sr5);
464 SIG_PROTECT(tf_sr6);
465 SIG_PROTECT(tf_sr7);
466
467 /* all CRs except CR11 must be protected. */
468 SIG_PROTECT(tf_rctr); /* CR0 */
469 /* CRs 1-8 are reserved */
470 SIG_PROTECT(tf_pidr1); /* CR8 */
471 SIG_PROTECT(tf_pidr2); /* CR9 */
472 SIG_PROTECT(tf_ccr); /* CR10 */
473 SIG_PROTECT(tf_pidr3); /* CR12 */
474 SIG_PROTECT(tf_pidr4); /* CR14 */
475 SIG_PROTECT(tf_eiem); /* CR15 */
476 /* CR17 is the IISQ head */
477 /* CR18 is the IIOQ head */
478 SIG_PROTECT(tf_iir); /* CR19 */
479 SIG_PROTECT(tf_isr); /* CR20 */
480 SIG_PROTECT(tf_ior); /* CR21 */
481 /* CR22 is the IPSW */
482 SIG_PROTECT(tf_eirr); /* CR23 */
483 SIG_PROTECT(tf_hptm); /* CR24 */
484 SIG_PROTECT(tf_vtop); /* CR25 */
485 /* XXX where are CR26, CR27, CR29, CR31? */
486 SIG_PROTECT(tf_cr28); /* CR28 */
487 SIG_PROTECT(tf_cr30); /* CR30 */
488 #undef SIG_PROTECT
489
490 /* The bulk copy. */
491 *tf = tstate.ss_frame;
492 }
493
494 /*
495 * Restore the original FP context
496 */
497 /* XXX fredette */
498
499 restore:
500 /*
501 * Restore the user supplied information.
502 * This should be at the last so that the error (EINVAL)
503 * is reported to the sigreturn caller, not to the
504 * jump destination.
505 */
506
507 tf->tf_sp = scp->sc_sp;
508 /* XXX should we be doing the space registers? */
509 tf->tf_iisq_head = scp->sc_pcsqh;
510 tf->tf_iioq_head = scp->sc_pcoqh | HPPA_PC_PRIV_USER;
511 tf->tf_iisq_tail = scp->sc_pcsqt;
512 tf->tf_iioq_tail = scp->sc_pcoqt | HPPA_PC_PRIV_USER;
513 tf->tf_ipsw = scp->sc_ps;
514
515 /* Restore signal stack. */
516 if (scp->sc_onstack & SS_ONSTACK)
517 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
518 else
519 p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
520
521 /* Restore signal mask. */
522 (void) sigprocmask1(p, SIG_SETMASK, &scp->sc_mask, 0);
523
524 #ifdef DEBUG
525 #if 0 /* XXX FP state */
526 if ((sigdebug & SDB_FPSTATE) && *(char *)&tstate.ss_fpstate)
527 printf("sigreturn(%d): copied in FP state (%x) at %p\n",
528 p->p_pid, *(u_int *)&tstate.ss_fpstate,
529 &tstate.ss_fpstate);
530 #endif
531 if ((sigdebug & SDB_FOLLOW) ||
532 ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid))
533 printf("sigreturn(%d): returns\n", p->p_pid);
534 #endif
535 return (EJUSTRETURN);
536 }
537
538