1 /* $NetBSD: sunos_machdep.c,v 1.40 2023/12/20 00:40:43 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
10 * Science Department.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * from: Utah $Hdr: machdep.c 1.63 91/04/24$
37 *
38 * @(#)machdep.c 7.16 (Berkeley) 6/3/91
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: sunos_machdep.c,v 1.40 2023/12/20 00:40:43 thorpej Exp $");
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/namei.h>
47 #include <sys/proc.h>
48 #include <sys/filedesc.h>
49 #include <sys/ioctl.h>
50 #include <sys/mount.h>
51 #include <sys/kernel.h>
52 #include <sys/signal.h>
53 #include <sys/signalvar.h>
54 #include <sys/buf.h>
55
56 #include <sys/syscallargs.h>
57 #include <compat/sunos/sunos.h>
58 #include <compat/sunos/sunos_syscallargs.h>
59 #include <compat/sys/signal.h>
60 #include <compat/sys/signalvar.h>
61
62 #include <machine/reg.h>
63
64 #ifdef DEBUG
65 extern int sigdebug;
66 extern int sigpid;
67 #define SDB_FOLLOW 0x01
68 #define SDB_KSTACK 0x02
69 #define SDB_FPSTATE 0x04
70 #endif
71
72 /* sigh.. I guess it's too late to change now, but "our" sigcontext
73 is plain vax, not very 68000 (ap, for example..) */
74 struct sunos_sigcontext {
75 int sc_onstack; /* sigstack state to restore */
76 int sc_mask; /* signal mask to restore */
77 int sc_sp; /* sp to restore */
78 int sc_pc; /* pc to restore */
79 int sc_ps; /* psl to restore */
80 };
81 struct sunos_sigframe {
82 int sf_signum; /* signo for handler */
83 int sf_code; /* additional info for handler */
84 struct sunos_sigcontext *sf_scp;/* context pointer for handler */
85 u_int sf_addr; /* even more info for handler */
86 struct sunos_sigcontext sf_sc; /* I don't know if that's what
87 comes here */
88 };
89 /*
90 * much simpler sendsig() for SunOS processes, as SunOS does the whole
91 * context-saving in usermode. For now, no hardware information (ie.
92 * frames for buserror etc) is saved. This could be fatal, so I take
93 * SIG_DFL for "dangerous" signals.
94 */
95 void
sunos_sendsig(const ksiginfo_t * ksi,const sigset_t * mask)96 sunos_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
97 {
98 u_long code = KSI_TRAPCODE(ksi);
99 int sig = ksi->ksi_signo;
100 struct lwp *l = curlwp;
101 struct proc *p = l->l_proc;
102 struct frame *frame = (struct frame *)l->l_md.md_regs;
103 int onstack, error;
104 struct sunos_sigframe *fp = getframe(l, sig, &onstack), kf;
105 sig_t catcher = SIGACTION(p, sig).sa_handler;
106 short ft = frame->f_format;
107
108 /*
109 * if this is a hardware fault (ft >= FMT9), sunos_sendsig
110 * can't currently handle it. Reset signal actions and
111 * have the process die unconditionally.
112 */
113 if (ft >= FMT9) {
114 SIGACTION(p, sig).sa_handler = SIG_DFL;
115 sigdelset(&p->p_sigctx.ps_sigignore, sig);
116 sigdelset(&p->p_sigctx.ps_sigcatch, sig);
117 sigdelset(&l->l_sigmask, sig);
118 mutex_exit(p->p_lock);
119 psignal(p, sig);
120 mutex_enter(p->p_lock);
121 return;
122 }
123
124 fp--;
125
126 #ifdef DEBUG
127 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
128 printf("sunos_sendsig(%d): sig %d ssp %p usp %p scp %p ft %d\n",
129 p->p_pid, sig, &onstack, fp, &fp->sf_sc, ft);
130 #endif
131
132 /* Build stack frame for signal trampoline. */
133 kf.sf_signum = sig;
134 kf.sf_code = code;
135 kf.sf_scp = &fp->sf_sc;
136 kf.sf_addr = ~0; /* means: not computable */
137
138 /* Build the signal context to be used by sigreturn. */
139 kf.sf_sc.sc_sp = frame->f_regs[SP];
140 kf.sf_sc.sc_pc = frame->f_pc;
141 kf.sf_sc.sc_ps = frame->f_sr;
142
143 /* Save signal stack. */
144 kf.sf_sc.sc_onstack = l->l_sigstk.ss_flags & SS_ONSTACK;
145
146 /* Save signal mask. */
147 native_sigset_to_sigset13(mask, &kf.sf_sc.sc_mask);
148
149 sendsig_reset(l, sig);
150 mutex_exit(p->p_lock);
151 error = copyout(&kf, fp, sizeof(kf));
152 mutex_enter(p->p_lock);
153
154 if (error != 0) {
155 #ifdef DEBUG
156 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
157 printf("sendsig(%d): copyout failed on sig %d\n",
158 p->p_pid, sig);
159 #endif
160 /*
161 * Process has trashed its stack; give it an illegal
162 * instruction to halt it in its tracks.
163 */
164 sigexit(l, SIGILL);
165 /* NOTREACHED */
166 }
167 #ifdef DEBUG
168 if (sigdebug & SDB_FOLLOW)
169 printf("sunos_sendsig(%d): sig %d scp %p sc_sp %x\n",
170 p->p_pid, sig, &fp->sf_sc,kf.sf_sc.sc_sp);
171 #endif
172
173 buildcontext(l, catcher, fp);
174
175 /* Remember that we're now on the signal stack. */
176 if (onstack)
177 l->l_sigstk.ss_flags |= SS_ONSTACK;
178
179 #ifdef DEBUG
180 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
181 printf("sunos_sendsig(%d): sig %d returns\n",
182 p->p_pid, sig);
183 #endif
184 }
185
186
187 /*
188 * System call to cleanup state after a signal
189 * has been taken. Reset signal mask and
190 * stack state from context left by sendsig (above).
191 * Return to previous pc and psl as specified by
192 * context left by sendsig. Check carefully to
193 * make sure that the user has not modified the
194 * psl to gain improper privileges or to cause
195 * a machine fault.
196 */
197 int
sunos_sys_sigreturn(struct lwp * l,const struct sunos_sys_sigreturn_args * uap,register_t * retval)198 sunos_sys_sigreturn(struct lwp *l, const struct sunos_sys_sigreturn_args *uap, register_t *retval)
199 {
200 struct proc *p = l->l_proc;
201 struct sunos_sigcontext *scp;
202 struct frame *frame;
203 struct sunos_sigcontext tsigc;
204 sigset_t mask;
205
206 scp = (struct sunos_sigcontext *) SCARG(uap, sigcntxp);
207 #ifdef DEBUG
208 if (sigdebug & SDB_FOLLOW)
209 printf("sunos_sigreturn: pid %d, scp %p\n", p->p_pid, scp);
210 #endif
211 if ((int)scp & 1)
212 return EINVAL;
213 if (copyin((void *)scp, (void *)&tsigc, sizeof(tsigc)) != 0)
214 return EFAULT;
215 scp = &tsigc;
216
217 /* Make sure the user isn't pulling a fast one on us! */
218 if ((scp->sc_ps & (PSL_MBZ|PSL_IPL|PSL_S)) != 0)
219 return EINVAL;
220
221 /*
222 * Restore the user supplied information
223 */
224
225 frame = (struct frame *) l->l_md.md_regs;
226 frame->f_regs[SP] = scp->sc_sp;
227 frame->f_pc = scp->sc_pc;
228 frame->f_sr = scp->sc_ps;
229
230 mutex_enter(p->p_lock);
231
232 /* Restore signal stack. */
233 if (scp->sc_onstack & SS_ONSTACK)
234 l->l_sigstk.ss_flags |= SS_ONSTACK;
235 else
236 l->l_sigstk.ss_flags &= ~SS_ONSTACK;
237
238 /* Restore signal mask. */
239 native_sigset13_to_sigset(&scp->sc_mask, &mask);
240 (void)sigprocmask1(l, SIG_SETMASK, &mask, 0);
241
242 mutex_exit(p->p_lock);
243
244 return EJUSTRETURN;
245 }
246