1 /*	$NetBSD: sunos32_machdep.c,v 1.8 2002/07/04 23:32:07 thorpej Exp $	*/
2 /* from: NetBSD: sunos_machdep.c,v 1.14 2001/01/29 01:37:56 mrg Exp 	*/
3 
4 /*
5  * Copyright (c) 1995, 2001 Matthew R. Green
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. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifdef _KERNEL_OPT
33 #include "opt_ddb.h"
34 #endif
35 
36 #include <sys/param.h>
37 #include <sys/exec.h>
38 #include <sys/systm.h>
39 #include <sys/proc.h>
40 #include <sys/namei.h>
41 #include <sys/user.h>
42 #include <sys/filedesc.h>
43 #include <sys/ioctl.h>
44 #include <sys/mount.h>
45 #include <sys/kernel.h>
46 #include <sys/signal.h>
47 #include <sys/signalvar.h>
48 #include <sys/malloc.h>
49 #include <sys/select.h>
50 
51 #include <sys/syscallargs.h>
52 #include <compat/sunos/sunos.h>
53 #include <compat/sunos/sunos_syscallargs.h>
54 #include <compat/netbsd32/netbsd32.h>
55 #include <compat/sunos32/sunos32.h>
56 #include <compat/sunos32/sunos32_syscallargs.h>
57 #include <compat/sunos32/sunos32_exec.h>
58 
59 #include <machine/frame.h>
60 #include <machine/cpu.h>
61 #include <machine/vuid_event.h>
62 #include <machine/reg.h>
63 
64 #include <dev/sun/event_var.h>
65 
66 #ifdef DEBUG
67 #include <sparc64/sparc64/sigdebug.h>
68 #endif
69 
70 struct sunos32_sigcontext {
71 	u_int32_t	sc_onstack;		/* sigstack state to restore */
72 	u_int32_t	sc_mask;		/* signal mask to restore (old style) */
73 	/* begin machine dependent portion */
74 	u_int32_t	sc_sp;			/* %sp to restore */
75 	u_int32_t	sc_pc;			/* pc to restore */
76 	u_int32_t	sc_npc;			/* npc to restore */
77 	u_int32_t	sc_psr;			/* pstate to restore */
78 	u_int32_t	sc_g1;			/* %g1 to restore */
79 	u_int32_t	sc_o0;			/* %o0 to restore */
80 };
81 
82 struct sunos32_sigframe {
83 	u_int32_t	sf_signo;		/* signal number */
84 	u_int32_t	sf_code;		/* code */
85 	u_int32_t	sf_scp;			/* SunOS user addr of sigcontext */
86 	u_int32_t	sf_addr;		/* SunOS compat, always 0 for now */
87 	struct	sunos32_sigcontext sf_sc;	/* actual sigcontext */
88 };
89 
90 static int ev_out32 __P((struct firm_event *, int, struct uio *));
91 
92 /*
93  * Set up registers on exec.
94  *
95  * XXX this entire mess must be fixed
96  */
97 /* ARGSUSED */
98 void
99 sunos32_setregs(p, pack, stack)
100 	struct proc *p;
101 	struct exec_package *pack;
102 	u_long stack; /* XXX */
103 {
104 	register struct trapframe64 *tf = p->p_md.md_tf;
105 	register struct fpstate64 *fs;
106 	register int64_t tstate;
107 
108 	/* Don't allow misaligned code by default */
109 	p->p_md.md_flags &= ~MDP_FIXALIGN;
110 
111 	/* Mark this as a 32-bit emulation */
112 	p->p_flag |= P_32;
113 
114 	/* Setup the ev_out32 hook */
115 	if (ev_out32_hook == NULL)
116 		ev_out32_hook = ev_out32;
117 
118 	/*
119 	 * Set the registers to 0 except for:
120 	 *	%o6: stack pointer, built in exec())
121 	 *	%tstate: (retain icc and xcc and cwp bits)
122 	 *	%g1: address of p->p_psstr (used by crt0)
123 	 *	%tpc,%tnpc: entry point of program
124 	 */
125 	tstate = ((PSTATE_USER32)<<TSTATE_PSTATE_SHIFT)
126 		| (tf->tf_tstate & TSTATE_CWP);
127 	if ((fs = p->p_md.md_fpstate) != NULL) {
128 		/*
129 		 * We hold an FPU state.  If we own *the* FPU chip state
130 		 * we must get rid of it, and the only way to do that is
131 		 * to save it.  In any case, get rid of our FPU state.
132 		 */
133 		if (p == fpproc) {
134 			savefpstate(fs);
135 			fpproc = NULL;
136 		}
137 		free((void *)fs, M_SUBPROC);
138 		p->p_md.md_fpstate = NULL;
139 	}
140 	bzero((caddr_t)tf, sizeof *tf);
141 	tf->tf_tstate = tstate;
142 	tf->tf_global[1] = (u_int)(u_long)p->p_psstr;
143 	tf->tf_pc = pack->ep_entry & ~3;
144 	tf->tf_npc = tf->tf_pc + 4;
145 
146 	stack -= sizeof(struct rwindow32);
147 	tf->tf_out[6] = stack;
148 	tf->tf_out[7] = NULL;
149 }
150 
151 void
152 sunos32_sendsig(sig, mask, code)
153 	int sig;
154 	sigset_t *mask;
155 	u_long code;
156 {
157 	struct proc *p = curproc;	/* XXX */
158 	struct sunos32_sigframe *fp;
159 	struct trapframe64 *tf;
160 	struct rwindow32 *oldsp, *newsp;
161 	struct sunos32_sigframe sf;
162 	struct sunos32_sigcontext *scp;
163 	u_int32_t addr, oldsp32;
164 	int onstack;
165 	sig_t catcher = SIGACTION(p, sig).sa_handler;
166 
167 	tf = p->p_md.md_tf;
168 	/* Need to attempt to zero extend this 32-bit pointer */
169 	oldsp = (struct rwindow32 *)(u_long)(u_int)tf->tf_out[6];
170 	oldsp32 = (u_int32_t)(u_long)oldsp;
171 
172 	/*
173 	 * Compute new user stack addresses, subtract off
174 	 * one signal frame, and align.
175 	 */
176 	onstack =
177 	    (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
178 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
179 
180 	if (onstack)
181 		fp = (struct sunos32_sigframe *)
182 		     ((caddr_t)p->p_sigctx.ps_sigstk.ss_sp + p->p_sigctx.ps_sigstk.ss_size);
183 	else
184 		fp = (struct sunos32_sigframe *)oldsp;
185 
186 	fp = (struct sunos32_sigframe *)((u_long)(fp - 1) & ~7);
187 
188 #ifdef DEBUG
189 	sigpid = p->p_pid;
190 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid) {
191 		printf("sunos32_sendsig: %s[%d] sig %d newusp %p scp %p oldsp %p\n",
192 		    p->p_comm, p->p_pid, sig, fp, &fp->sf_sc, oldsp);
193 #ifdef DDB
194 		if (sigdebug & SDB_DDB) Debugger();
195 #endif
196 	}
197 #endif
198 	/*
199 	 * Now set up the signal frame.  We build it in kernel space
200 	 * and then copy it out.  We probably ought to just build it
201 	 * directly in user space....
202 	 */
203 	sf.sf_signo = sig;
204 	sf.sf_code = (u_int32_t)code;
205 	scp = &fp->sf_sc;
206 	if ((u_long)scp >= 0x100000000)
207 		printf("sunos32_sendsig: sf_scp overflow %p > 0x100000000\n", scp);
208 	sf.sf_scp = (u_int32_t)(u_long)scp;
209 	sf.sf_addr = 0;			/* XXX */
210 
211 	/*
212 	 * Build the signal context to be used by sigreturn.
213 	 */
214 	sf.sf_sc.sc_onstack = p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK;
215 	native_sigset_to_sigset13(mask, &sf.sf_sc.sc_mask);
216 	sf.sf_sc.sc_sp = (u_int)(u_long)oldsp;
217 	sf.sf_sc.sc_pc = tf->tf_pc;
218 	sf.sf_sc.sc_npc = tf->tf_npc;
219 	sf.sf_sc.sc_psr = TSTATECCR_TO_PSR(tf->tf_tstate); /* XXX */
220 	sf.sf_sc.sc_g1 = tf->tf_global[1];
221 	sf.sf_sc.sc_o0 = tf->tf_out[0];
222 
223 	/*
224 	 * Put the stack in a consistent state before we whack away
225 	 * at it.  Note that write_user_windows may just dump the
226 	 * registers into the pcb; we need them in the process's memory.
227 	 * We also need to make sure that when we start the signal handler,
228 	 * its %i6 (%fp), which is loaded from the newly allocated stack area,
229 	 * joins seamlessly with the frame it was in when the signal occurred,
230 	 * so that the debugger and _longjmp code can back up through it.
231 	 */
232 	newsp = (struct rwindow32 *)((long)fp - sizeof(struct rwindow32));
233 	write_user_windows();
234 #ifdef DEBUG
235 	if ((sigdebug & SDB_KSTACK))
236 	    printf("sunos32_sendsig: saving sf to %p, setting stack pointer %p to %p\n",
237 		   fp, &(((struct rwindow32 *)newsp)->rw_in[6]), oldsp);
238 #endif
239 	if (rwindow_save(p) || copyout((caddr_t)&sf, (caddr_t)fp, sizeof sf) ||
240 	    copyout((caddr_t)&oldsp32, &(((struct rwindow32 *)newsp)->rw_in[6]), sizeof oldsp32)) {
241 		/*
242 		 * Process has trashed its stack; give it an illegal
243 		 * instruction to halt it in its tracks.
244 		 */
245 #ifdef DEBUG
246 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
247 			printf("sunos32_sendsig: window save or copyout error\n");
248 		printf("sunos32_sendsig: stack was trashed trying to send sig %d, sending SIGILL\n", sig);
249 #ifdef DDB
250 		if (sigdebug & SDB_DDB) Debugger();
251 #endif
252 #endif
253 		sigexit(p, SIGILL);
254 		/* NOTREACHED */
255 	}
256 
257 #ifdef DEBUG
258 	if ((sigdebug & SDB_FOLLOW)) {
259 		printf("sunos32_sendsig: %s[%d] sig %d scp %p\n",
260 		       p->p_comm, p->p_pid, sig, &fp->sf_sc);
261 	}
262 #endif
263 	/*
264 	 * Arrange to continue execution at the code copied out in exec().
265 	 * It needs the function to call in %g1, and a new stack pointer.
266 	 */
267 	addr = (u_int32_t)(u_long)catcher;	/* user does his own trampolining */
268 	tf->tf_pc = addr;
269 	tf->tf_npc = addr + 4;
270 	tf->tf_out[6] = (u_int64_t)(u_int)(u_long)newsp;
271 #ifdef DEBUG
272 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid) {
273 		printf("sunos32_sendsig: about to return to catcher %p thru %p\n",
274 		       catcher, (void *)(u_long)addr);
275 #ifdef DDB
276 		if (sigdebug & SDB_DDB) Debugger();
277 #endif
278 	}
279 #endif
280 }
281 
282 int
283 sunos32_sys_sigreturn(p, v, retval)
284         register struct proc *p;
285 	void *v;
286 	register_t *retval;
287 {
288 	struct sunos32_sys_sigreturn_args /*
289 		syscallarg(netbsd32_sigcontextp_t) sigcntxp;
290 	} */ *uap = v;
291 	struct sunos32_sigcontext sc, *scp;
292 	sigset_t mask;
293 	struct trapframe64 *tf;
294 
295 	/* First ensure consistent stack state (see sendsig). */
296 	write_user_windows();
297 	if (rwindow_save(p))
298 		sigexit(p, SIGILL);
299 #ifdef DEBUG
300 	if (sigdebug & SDB_FOLLOW) {
301 		printf("sunos32_sigreturn: %s[%d], sigcntxp %p\n",
302 		    p->p_comm, p->p_pid, (void *)(u_long)SCARG(uap, sigcntxp));
303 #ifdef DDB
304 		if (sigdebug & SDB_DDB) Debugger();
305 #endif
306 	}
307 #endif
308 
309 	scp = (struct sunos32_sigcontext *)(u_long)SCARG(uap, sigcntxp);
310 	if ((vaddr_t)scp & 3 || (copyin((caddr_t)scp, &sc, sizeof sc) != 0))
311 		return (EFAULT);
312 	scp = &sc;
313 
314 	tf = p->p_md.md_tf;
315 	/*
316 	 * Only the icc bits in the psr are used, so it need not be
317 	 * verified.  pc and npc must be multiples of 4.  This is all
318 	 * that is required; if it holds, just do it.
319 	 */
320 	if (((scp->sc_pc | scp->sc_npc) & 3) != 0 || scp->sc_pc == 0 || scp->sc_npc == 0)
321 	{
322 #ifdef DEBUG
323 		printf("sunos32_sigreturn: pc %x or npc %x invalid\n", scp->sc_pc, scp->sc_npc);
324 #ifdef DDB
325 		Debugger();
326 #endif
327 #endif
328 		return (EINVAL);
329 	}
330 	/* take only psr ICC field */
331 	tf->tf_tstate = (int64_t)(tf->tf_tstate & ~TSTATE_CCR) | PSRCC_TO_TSTATE(scp->sc_psr);
332 	tf->tf_pc = scp->sc_pc;
333 	tf->tf_npc = scp->sc_npc;
334 	tf->tf_global[1] = scp->sc_g1;
335 	tf->tf_out[0] = scp->sc_o0;
336 	tf->tf_out[6] = scp->sc_sp;
337 #ifdef DEBUG
338 	if (sigdebug & SDB_FOLLOW) {
339 		printf("sunos32_sigreturn: return trapframe pc=%p sp=%p tstate=%llx\n",
340 		       (void *)(u_long)tf->tf_pc, (void *)(u_long)tf->tf_out[6], (unsigned long long)tf->tf_tstate);
341 #ifdef DDB
342 		if (sigdebug & SDB_DDB) Debugger();
343 #endif
344 	}
345 #endif
346 
347 	if (scp->sc_onstack & SS_ONSTACK)
348 		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
349 	else
350 		p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
351 
352 	/* Restore signal mask */
353 	native_sigset13_to_sigset(&scp->sc_mask, &mask);
354 	(void) sigprocmask1(p, SIG_SETMASK, &mask, 0);
355 
356 	return (EJUSTRETURN);
357 }
358 
359 /*
360  * Write out a series of 32-bit firm_events.
361  */
362 static int
363 ev_out32(e, n, uio)
364 	struct firm_event *e;
365 	int n;
366 	struct uio *uio;
367 {
368 	struct firm_event32 e32;
369 	int error = 0;
370 
371 	while (n-- && error == 0) {
372 		e32.id = e->id;
373 		e32.value = e->value;
374 		e32.time.tv_sec = e->time.tv_sec;
375 		e32.time.tv_usec = e->time.tv_usec;
376 		error = uiomove((caddr_t)&e32, sizeof(e32), uio);
377 		e++;
378 	}
379 	return (error);
380 }
381