xref: /netbsd/sys/arch/m68k/m68k/sunos_machdep.c (revision 8ac3875a)
1 /*	$NetBSD: sunos_machdep.c,v 1.38 2011/02/08 20:20:16 rmind 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.38 2011/02/08 20:20:16 rmind 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/malloc.h>
55 #include <sys/buf.h>
56 
57 #include <sys/syscallargs.h>
58 #include <compat/sunos/sunos.h>
59 #include <compat/sunos/sunos_syscallargs.h>
60 #include <compat/sys/signal.h>
61 #include <compat/sys/signalvar.h>
62 
63 #include <machine/reg.h>
64 
65 #ifdef DEBUG
66 extern int sigdebug;
67 extern int sigpid;
68 #define SDB_FOLLOW      0x01
69 #define SDB_KSTACK      0x02
70 #define SDB_FPSTATE     0x04
71 #endif
72 
73 /* sigh.. I guess it's too late to change now, but "our" sigcontext
74    is plain vax, not very 68000 (ap, for example..) */
75 struct sunos_sigcontext {
76 	int 	sc_onstack;		/* sigstack state to restore */
77 	int	sc_mask;		/* signal mask to restore */
78 	int	sc_sp;			/* sp to restore */
79 	int	sc_pc;			/* pc to restore */
80 	int	sc_ps;			/* psl to restore */
81 };
82 struct sunos_sigframe {
83 	int	sf_signum;		/* signo for handler */
84 	int	sf_code;		/* additional info for handler */
85 	struct sunos_sigcontext *sf_scp;/* context pointer for handler */
86 	u_int	sf_addr;		/* even more info for handler */
87 	struct sunos_sigcontext sf_sc;	/* I don't know if that's what
88 					   comes here */
89 };
90 /*
91  * much simpler sendsig() for SunOS processes, as SunOS does the whole
92  * context-saving in usermode. For now, no hardware information (ie.
93  * frames for buserror etc) is saved. This could be fatal, so I take
94  * SIG_DFL for "dangerous" signals.
95  */
96 void
sunos_sendsig(const ksiginfo_t * ksi,const sigset_t * mask)97 sunos_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
98 {
99 	u_long code = KSI_TRAPCODE(ksi);
100 	int sig = ksi->ksi_signo;
101 	struct lwp *l = curlwp;
102 	struct proc *p = l->l_proc;
103 	struct frame *frame = (struct frame *)l->l_md.md_regs;
104 	int onstack, error;
105 	struct sunos_sigframe *fp = getframe(l, sig, &onstack), kf;
106 	sig_t catcher = SIGACTION(p, sig).sa_handler;
107 	short ft = frame->f_format;
108 
109 	/*
110 	 * if this is a hardware fault (ft >= FMT9), sunos_sendsig
111 	 * can't currently handle it. Reset signal actions and
112 	 * have the process die unconditionally.
113 	 */
114 	if (ft >= FMT9) {
115 		SIGACTION(p, sig).sa_handler = SIG_DFL;
116 		sigdelset(&p->p_sigctx.ps_sigignore, sig);
117 		sigdelset(&p->p_sigctx.ps_sigcatch, sig);
118 		sigdelset(&l->l_sigmask, sig);
119 		mutex_exit(p->p_lock);
120 		psignal(p, sig);
121 		mutex_enter(p->p_lock);
122 		return;
123 	}
124 
125 	fp--;
126 
127 #ifdef DEBUG
128 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
129 		printf("sunos_sendsig(%d): sig %d ssp %p usp %p scp %p ft %d\n",
130 		       p->p_pid, sig, &onstack, fp, &fp->sf_sc, ft);
131 #endif
132 
133 	/* Build stack frame for signal trampoline. */
134 	kf.sf_signum = sig;
135 	kf.sf_code = code;
136 	kf.sf_scp = &fp->sf_sc;
137 	kf.sf_addr = ~0;		/* means: not computable */
138 
139 	/* Build the signal context to be used by sigreturn. */
140 	kf.sf_sc.sc_sp = frame->f_regs[SP];
141 	kf.sf_sc.sc_pc = frame->f_pc;
142 	kf.sf_sc.sc_ps = frame->f_sr;
143 
144 	/* Save signal stack. */
145 	kf.sf_sc.sc_onstack = l->l_sigstk.ss_flags & SS_ONSTACK;
146 
147 	/* Save signal mask. */
148 	native_sigset_to_sigset13(mask, &kf.sf_sc.sc_mask);
149 
150 	sendsig_reset(l, sig);
151 	mutex_exit(p->p_lock);
152 	error = copyout(&kf, fp, sizeof(kf));
153 	mutex_enter(p->p_lock);
154 
155 	if (error != 0) {
156 #ifdef DEBUG
157 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
158 			printf("sendsig(%d): copyout failed on sig %d\n",
159 			    p->p_pid, sig);
160 #endif
161 		/*
162 		 * Process has trashed its stack; give it an illegal
163 		 * instruction to halt it in its tracks.
164 		 */
165 		sigexit(l, SIGILL);
166 		/* NOTREACHED */
167 	}
168 #ifdef DEBUG
169 	if (sigdebug & SDB_FOLLOW)
170 		printf("sunos_sendsig(%d): sig %d scp %p sc_sp %x\n",
171 		       p->p_pid, sig, &fp->sf_sc,kf.sf_sc.sc_sp);
172 #endif
173 
174 	buildcontext(l, catcher, fp);
175 
176 	/* Remember that we're now on the signal stack. */
177 	if (onstack)
178 		l->l_sigstk.ss_flags |= SS_ONSTACK;
179 
180 #ifdef DEBUG
181 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
182 		printf("sunos_sendsig(%d): sig %d returns\n",
183 		       p->p_pid, sig);
184 #endif
185 }
186 
187 
188 /*
189  * System call to cleanup state after a signal
190  * has been taken.  Reset signal mask and
191  * stack state from context left by sendsig (above).
192  * Return to previous pc and psl as specified by
193  * context left by sendsig. Check carefully to
194  * make sure that the user has not modified the
195  * psl to gain improper privileges or to cause
196  * a machine fault.
197  */
198 int
sunos_sys_sigreturn(struct lwp * l,const struct sunos_sys_sigreturn_args * uap,register_t * retval)199 sunos_sys_sigreturn(struct lwp *l, const struct sunos_sys_sigreturn_args *uap, register_t *retval)
200 {
201 	struct proc *p = l->l_proc;
202 	struct sunos_sigcontext *scp;
203 	struct frame *frame;
204 	struct sunos_sigcontext tsigc;
205 	sigset_t mask;
206 
207 	scp = (struct sunos_sigcontext *) SCARG(uap, sigcntxp);
208 #ifdef DEBUG
209 	if (sigdebug & SDB_FOLLOW)
210 		printf("sunos_sigreturn: pid %d, scp %p\n", p->p_pid, scp);
211 #endif
212 	if ((int)scp & 1)
213 		return EINVAL;
214 	if (copyin((void *)scp, (void *)&tsigc, sizeof(tsigc)) != 0)
215 		return EFAULT;
216 	scp = &tsigc;
217 
218 	/* Make sure the user isn't pulling a fast one on us! */
219 	if ((scp->sc_ps & (PSL_MBZ|PSL_IPL|PSL_S)) != 0)
220 		return EINVAL;
221 
222 	/*
223 	 * Restore the user supplied information
224 	 */
225 
226 	frame = (struct frame *) l->l_md.md_regs;
227 	frame->f_regs[SP] = scp->sc_sp;
228 	frame->f_pc = scp->sc_pc;
229 	frame->f_sr = scp->sc_ps;
230 
231 	mutex_enter(p->p_lock);
232 
233 	/* Restore signal stack. */
234 	if (scp->sc_onstack & SS_ONSTACK)
235 		l->l_sigstk.ss_flags |= SS_ONSTACK;
236 	else
237 		l->l_sigstk.ss_flags &= ~SS_ONSTACK;
238 
239 	/* Restore signal mask. */
240 	native_sigset13_to_sigset(&scp->sc_mask, &mask);
241 	(void)sigprocmask1(l, SIG_SETMASK, &mask, 0);
242 
243 	mutex_exit(p->p_lock);
244 
245 	return EJUSTRETURN;
246 }
247