xref: /netbsd-src/sys/arch/sparc64/sparc64/sunos_machdep.c (revision 1dc652ef5a0bffbd0917f95e0797bad8c6fc8efd)
1 /*	$NetBSD: sunos_machdep.c,v 1.35 2023/12/20 05:33:59 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 1995 Matthew R. Green
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: sunos_machdep.c,v 1.35 2023/12/20 05:33:59 thorpej Exp $");
31 
32 #ifdef _KERNEL_OPT
33 #include "opt_ddb.h"
34 #endif
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/proc.h>
39 #include <sys/namei.h>
40 #include <sys/filedesc.h>
41 #include <sys/ioctl.h>
42 #include <sys/mount.h>
43 #include <sys/kernel.h>
44 #include <sys/signal.h>
45 #include <sys/signalvar.h>
46 
47 #include <compat/sys/signal.h>
48 #include <compat/sys/signalvar.h>
49 
50 #include <sys/syscallargs.h>
51 #include <compat/sunos/sunos.h>
52 #include <compat/sunos/sunos_syscallargs.h>
53 
54 #include <machine/frame.h>
55 #include <machine/cpu.h>
56 
57 #ifdef DEBUG
58 #include <sparc64/sparc64/sigdebug.h>
59 #endif
60 
61 struct sunos_sigcontext {
62 	int	sc_onstack;		/* sigstack state to restore */
63 	int	sc_mask;		/* signal mask to restore (old style) */
64 	/* begin machine dependent portion */
65 	int	sc_sp;			/* %sp to restore */
66 	int	sc_pc;			/* pc to restore */
67 	int	sc_npc;			/* npc to restore */
68 	int	sc_psr;			/* pstate to restore */
69 	int	sc_g1;			/* %g1 to restore */
70 	int	sc_o0;			/* %o0 to restore */
71 };
72 
73 struct sunos_sigframe {
74 	int	sf_signo;		/* signal number */
75 	int	sf_code;		/* code */
76 	uint32_t	sf_scp;			/* SunOS user addr of sigcontext */
77 	int	sf_addr;		/* SunOS compat, always 0 for now */
78 	struct	sunos_sigcontext sf_sc;	/* actual sigcontext */
79 };
80 
81 void
sunos_sendsig(const ksiginfo_t * ksi,const sigset_t * mask)82 sunos_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
83 {
84 	register struct lwp *l = curlwp;
85 	struct proc *p = l->l_proc;
86 	register struct sunos_sigframe *fp;
87 	register struct trapframe64 *tf;
88 	register int addr, onstack;
89 	struct rwindow32 *oldsp, *newsp;
90 	register32_t sp;
91 	int sig = ksi->ksi_signo, error;
92 	sig_t catcher = SIGACTION(p, sig).sa_handler;
93 	struct sunos_sigframe sf;
94 
95 	tf = (struct trapframe64 *)l->l_md.md_tf;
96 	/* Need to attempt to zero extend this 32-bit pointer */
97 	oldsp = (struct rwindow32 *)(u_long)(u_int)tf->tf_out[6];
98 	/*
99 	 * Compute new user stack addresses, subtract off
100 	 * one signal frame, and align.
101 	 */
102 	onstack =
103 	    (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
104 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
105 
106 	if (onstack)
107 		fp = (struct sunos_sigframe *)((char *)l->l_sigstk.ss_sp +
108 					l->l_sigstk.ss_size);
109 	else
110 		fp = (struct sunos_sigframe *)oldsp;
111 	fp = (struct sunos_sigframe *)((long)(fp - 1) & ~7);
112 
113 #ifdef DEBUG
114 	sigpid = p->p_pid;
115 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid) {
116 		printf("sunos_sendsig: %s[%d] sig %d newusp %p scp %p oldsp %p\n",
117 		    p->p_comm, p->p_pid, sig, fp, &fp->sf_sc, oldsp);
118 #ifdef DDB
119 		if (sigdebug & SDB_DDB) Debugger();
120 #endif
121 	}
122 #endif
123 	/*
124 	 * Now set up the signal frame.  We build it in kernel space
125 	 * and then copy it out.  We probably ought to just build it
126 	 * directly in user space....
127 	 */
128 	sf.sf_signo = sig;
129 	sf.sf_code = ksi->ksi_trap;
130 	sf.sf_scp = (u_long)&fp->sf_sc;
131 	sf.sf_addr = 0;			/* XXX */
132 
133 	/*
134 	 * Build the signal context to be used by sigreturn.
135 	 */
136 	sf.sf_sc.sc_onstack = l->l_sigstk.ss_flags & SS_ONSTACK;
137 	native_sigset_to_sigset13(mask, &sf.sf_sc.sc_mask);
138 	sf.sf_sc.sc_sp = (long)oldsp;
139 	sf.sf_sc.sc_pc = tf->tf_pc;
140 	sf.sf_sc.sc_npc = tf->tf_npc;
141 	sf.sf_sc.sc_psr = TSTATECCR_TO_PSR(tf->tf_tstate); /* XXX */
142 	sf.sf_sc.sc_g1 = tf->tf_global[1];
143 	sf.sf_sc.sc_o0 = tf->tf_out[0];
144 
145 	/*
146 	 * Put the stack in a consistent state before we whack away
147 	 * at it.  Note that write_user_windows may just dump the
148 	 * registers into the pcb; we need them in the process's memory.
149 	 * We also need to make sure that when we start the signal handler,
150 	 * its %i6 (%fp), which is loaded from the newly allocated stack area,
151 	 * joins seamlessly with the frame it was in when the signal occurred,
152 	 * so that the debugger and _longjmp code can back up through it.
153 	 */
154 	sendsig_reset(l, sig);
155 	mutex_exit(p->p_lock);
156 	newsp = (struct rwindow32 *)((long)fp - sizeof(struct rwindow32));
157 	write_user_windows();
158 #ifdef DEBUG
159 	if ((sigdebug & SDB_KSTACK))
160 	    printf("sunos_sendsig: saving sf to %p, setting stack pointer %p to %p\n",
161 		   fp, &(((struct rwindow32 *)newsp)->rw_in[6]), oldsp);
162 #endif
163 	sp = (register32_t)(uintptr_t)oldsp;
164 	error = (rwindow_save(l) ||
165 	    copyout((void *)&sf, (void *)fp, sizeof sf) ||
166 	    copyout(&sp, &(((struct rwindow32 *)newsp)->rw_in[6]),
167 	        sizeof(sp)));
168 	mutex_enter(p->p_lock);
169 
170 	if (error) {
171 		/*
172 		 * Process has trashed its stack; give it an illegal
173 		 * instruction to halt it in its tracks.
174 		 */
175 #ifdef DEBUG
176 		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
177 			printf("sunos_sendsig: window save or copyout error\n");
178 		printf("sunos_sendsig: stack was trashed trying to send sig %d, sending SIGILL\n", sig);
179 #ifdef DDB
180 		if (sigdebug & SDB_DDB) Debugger();
181 #endif
182 #endif
183 		sigexit(l, SIGILL);
184 		/* NOTREACHED */
185 	}
186 
187 #ifdef DEBUG
188 	if (sigdebug & SDB_FOLLOW) {
189 		printf("sunos_sendsig: %s[%d] sig %d scp %p\n",
190 		       p->p_comm, p->p_pid, sig, &fp->sf_sc);
191 	}
192 #endif
193 	/*
194 	 * Arrange to continue execution at the code copied out in exec().
195 	 * It needs the function to call in %g1, and a new stack pointer.
196 	 */
197 	addr = (long)catcher;	/* user does his own trampolining */
198 	tf->tf_pc = addr;
199 	tf->tf_npc = addr + 4;
200 	tf->tf_out[6] = (uint64_t)(u_int)(u_long)newsp;
201 #ifdef DEBUG
202 	if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid) {
203 		printf("sunos_sendsig: about to return to catcher %p thru %p\n",
204 		       catcher, (void *)(u_long)addr);
205 #ifdef DDB
206 		if (sigdebug & SDB_DDB) Debugger();
207 #endif
208 	}
209 #endif
210 }
211 
212 int
sunos_sys_sigreturn(register struct lwp * l,const struct sunos_sys_sigreturn_args * uap,register_t * retval)213 sunos_sys_sigreturn(register struct lwp *l, const struct sunos_sys_sigreturn_args *uap, register_t *retval)
214 {
215 	struct proc *p = l->l_proc;
216 	struct sunos_sigcontext sc, *scp;
217 	sigset_t mask;
218 	struct trapframe64 *tf;
219 
220 	/* First ensure consistent stack state (see sendsig). */
221 	write_user_windows();
222 	if (rwindow_save(l)) {
223 		mutex_enter(p->p_lock);
224 		sigexit(l, SIGILL);
225 	}
226 #ifdef DEBUG
227 	if (sigdebug & SDB_FOLLOW) {
228 		printf("sunos_sigreturn: %s[%d], sigcntxp %p\n",
229 		    p->p_comm, p->p_pid, SCARG(uap, sigcntxp));
230 #ifdef DDB
231 		if (sigdebug & SDB_DDB) Debugger();
232 #endif
233 	}
234 #endif
235 
236 	scp = (struct sunos_sigcontext *)SCARG(uap, sigcntxp);
237 	if ((vaddr_t)scp & 3 || (copyin((void *)scp, &sc, sizeof sc) != 0))
238 		return (EFAULT);
239 	scp = &sc;
240 
241 	tf = (struct trapframe64 *)l->l_md.md_tf;
242 	/*
243 	 * Only the icc bits in the psr are used, so it need not be
244 	 * verified.  pc and npc must be multiples of 4.  This is all
245 	 * that is required; if it holds, just do it.
246 	 */
247 	if (((scp->sc_pc | scp->sc_npc) & 3) != 0 || scp->sc_pc == 0 || scp->sc_npc == 0)
248 #ifdef DEBUG
249 	{
250 		printf("sunos_sigreturn: pc %p or npc %p invalid\n", (void *)(u_long)scp->sc_pc, (void *)(u_long)scp->sc_npc);
251 #ifdef DDB
252 		Debugger();
253 #endif
254 		return (EINVAL);
255 	}
256 #endif
257 		return (EINVAL);
258 	/* take only psr ICC field */
259 	tf->tf_tstate = (int64_t)(tf->tf_tstate & ~TSTATE_CCR) | PSRCC_TO_TSTATE(scp->sc_psr);
260 	tf->tf_pc = scp->sc_pc;
261 	tf->tf_npc = scp->sc_npc;
262 	tf->tf_global[1] = scp->sc_g1;
263 	tf->tf_out[0] = scp->sc_o0;
264 	tf->tf_out[6] = scp->sc_sp;
265 #ifdef DEBUG
266 	if (sigdebug & SDB_FOLLOW) {
267 		printf("sunos_sigreturn: return trapframe pc=%p sp=%p tstate=%llx\n",
268 		       (void *)(u_long)tf->tf_pc, (void *)(u_long)tf->tf_out[6], (unsigned long long)tf->tf_tstate);
269 #ifdef DDB
270 		if (sigdebug & SDB_DDB) Debugger();
271 #endif
272 	}
273 #endif
274 
275 	mutex_enter(p->p_lock);
276 	if (scp->sc_onstack & SS_ONSTACK)
277 		l->l_sigstk.ss_flags |= SS_ONSTACK;
278 	else
279 		l->l_sigstk.ss_flags &= ~SS_ONSTACK;
280 	/* Restore signal mask */
281 	native_sigset13_to_sigset(&scp->sc_mask, &mask);
282 	(void) sigprocmask1(l, SIG_SETMASK, &mask, 0);
283 	mutex_exit(p->p_lock);
284 
285 	return (EJUSTRETURN);
286 }
287