xref: /netbsd-src/sys/arch/powerpc/powerpc/compat_16_machdep.c (revision 9a8fceddf4f4d459b5022c7eb01310e8e0a8cfab)
1 /*	$NetBSD: compat_16_machdep.c,v 1.26 2024/06/19 15:19:22 rin Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5  * Copyright (C) 1995, 1996 TooLs GmbH.
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. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by TooLs GmbH.
19  * 4. The name of TooLs GmbH may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: compat_16_machdep.c,v 1.26 2024/06/19 15:19:22 rin Exp $");
36 
37 #ifdef _KERNEL_OPT
38 #include "opt_altivec.h"
39 #include "opt_compat_netbsd.h"
40 #include "opt_ppcarch.h"
41 #endif
42 
43 #include <sys/param.h>
44 #include <sys/mount.h>
45 #include <sys/proc.h>
46 #include <sys/syscallargs.h>
47 #include <sys/systm.h>
48 #include <sys/ucontext.h>
49 
50 #include <uvm/uvm_extern.h>
51 
52 #include <compat/sys/signal.h>
53 #include <compat/sys/signalvar.h>
54 
55 #include <powerpc/frame.h>
56 #include <powerpc/pcb.h>
57 #include <powerpc/psl.h>
58 #include <powerpc/fpu.h>
59 #if defined(ALTIVEC) || defined(PPC_HAVE_SPE)
60 #include <powerpc/altivec.h>
61 #endif
62 
63 #ifdef _LP64
64 
65 /*
66  * COMPAT_16 is useful only with COMPAT_NETBSD32.
67  */
68 void
sendsig_sigcontext(const ksiginfo_t * ksi,const sigset_t * mask)69 sendsig_sigcontext(const ksiginfo_t *ksi, const sigset_t *mask)
70 {
71 
72 	printf("sendsig_sigcontext: illegal\n");
73 	sigexit(curlwp, SIGILL);
74 }
75 
76 int
compat_16_sys___sigreturn14(struct lwp * l,const struct compat_16_sys___sigreturn14_args * uap,register_t * retval)77 compat_16_sys___sigreturn14(struct lwp *l,
78     const struct compat_16_sys___sigreturn14_args *uap, register_t *retval)
79 {
80 
81 	return ENOSYS;
82 }
83 
84 #else
85 
86 /*
87  * Send a signal to process.
88  */
89 void
sendsig_sigcontext(const ksiginfo_t * ksi,const sigset_t * mask)90 sendsig_sigcontext(const ksiginfo_t *ksi, const sigset_t *mask)
91 {
92 	struct lwp * const l = curlwp;
93 	struct proc * const p = l->l_proc;
94 	struct sigacts * const ps = p->p_sigacts;
95 	struct sigcontext *fp, frame;
96 	struct trapframe * const tf = l->l_md.md_utf;
97 	struct utrapframe * const utf = &frame.sc_frame;
98 	int onstack, error;
99 	int sig = ksi->ksi_signo;
100 	u_long code = KSI_TRAPCODE(ksi);
101 	sig_t catcher = SIGACTION(p, sig).sa_handler;
102 
103 
104 	/* Do we need to jump onto the signal stack? */
105 	onstack =
106 	    (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
107 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
108 
109 	/* Allocate space for the signal handler context. */
110 	if (onstack)
111 		fp = (struct sigcontext *)((char *)l->l_sigstk.ss_sp +
112 						l->l_sigstk.ss_size);
113 	else
114 		fp = (struct sigcontext *)tf->tf_fixreg[1];
115 	fp = (struct sigcontext *)((uintptr_t)(fp - 1) & -CALLFRAMELEN);
116 
117 	/* Save register context. */
118 	memcpy(utf->fixreg, tf->tf_fixreg, sizeof(utf->fixreg));
119 	utf->lr   = tf->tf_lr;
120 	utf->cr   = tf->tf_cr;
121 	utf->xer  = tf->tf_xer;
122 	utf->ctr  = tf->tf_ctr;
123 	utf->srr0 = tf->tf_srr0;
124 	utf->srr1 = tf->tf_srr1 & PSL_USERSRR1;
125 
126 #ifdef PPC_HAVE_FPU
127 	const struct pcb * const pcb = lwp_getpcb(l);
128 	utf->srr1 |= pcb->pcb_flags & (PCB_FE0|PCB_FE1);
129 #endif
130 #if defined(ALTIVEC) || defined(PPC_HAVE_SPE)
131 	/*
132 	 * We can't round-trip the vector unit registers with a
133 	 * sigcontext, so at least get them saved into the PCB.
134 	 * XXX vec_save_to_mcontext() has a special hack for this.
135 	 */
136 	vec_save_to_mcontext(l, NULL, NULL);
137 #endif
138 #ifdef PPC_OEA
139 	utf->vrsave = tf->tf_vrsave;
140 	utf->mq = tf->tf_mq;
141 #endif
142 
143 	/* Save signal stack. */
144 	frame.sc_onstack = l->l_sigstk.ss_flags & SS_ONSTACK;
145 
146 	/* Save signal mask. */
147 	frame.sc_mask = *mask;
148 
149 #ifdef COMPAT_13
150 	/*
151 	 * XXX We always have to save an old style signal mask because
152 	 * XXX we might be delivering a signal to a process which will
153 	 * XXX escape from the signal in a non-standard way and invoke
154 	 * XXX sigreturn() directly.
155 	 */
156 	native_sigset_to_sigset13(mask, &frame.__sc_mask13);
157 #endif
158 	sendsig_reset(l, sig);
159 	mutex_exit(p->p_lock);
160 	error = copyout(&frame, fp, sizeof frame);
161 	mutex_enter(p->p_lock);
162 
163 	if (error != 0) {
164 		/*
165 		 * Process has trashed its stack; give it an illegal
166 		 * instruction to halt it in its tracks.
167 		 */
168 		sigexit(l, SIGILL);
169 		/* NOTREACHED */
170 	}
171 
172 	/*
173 	 * Build context to run handler in.  Note the trampoline version
174 	 * numbers are coordinated with machine-dependent code in libc.
175 	 */
176 	switch (ps->sa_sigdesc[sig].sd_vers) {
177 #if 1 /* COMPAT_16 */
178 	case __SIGTRAMP_SIGCODE_VERSION:	/* legacy on-stack sigtramp */
179 		tf->tf_fixreg[1] = (register_t)fp;
180 		tf->tf_lr = (register_t)catcher;
181 		tf->tf_fixreg[3] = (register_t)sig;
182 		tf->tf_fixreg[4] = (register_t)code;
183 		tf->tf_fixreg[5] = (register_t)fp;
184 		tf->tf_srr0 = (register_t)p->p_sigctx.ps_sigcode;
185 		break;
186 #endif /* COMPAT_16 */
187 
188 	case __SIGTRAMP_SIGCONTEXT_VERSION:
189 		tf->tf_fixreg[1] = (register_t)fp;
190 		tf->tf_lr = (register_t)catcher;
191 		tf->tf_fixreg[3] = (register_t)sig;
192 		tf->tf_fixreg[4] = (register_t)code;
193 		tf->tf_fixreg[5] = (register_t)fp;
194 		tf->tf_srr0 = (register_t)ps->sa_sigdesc[sig].sd_tramp;
195 		break;
196 
197 	default:
198 		/* Don't know what trampoline version; kill it. */
199 		sigexit(l, SIGILL);
200 	}
201 
202 	/* Remember that we're now on the signal stack. */
203 	if (onstack)
204 		l->l_sigstk.ss_flags |= SS_ONSTACK;
205 }
206 
207 /*
208  * System call to cleanup state after a signal handler returns.
209  */
210 int
compat_16_sys___sigreturn14(struct lwp * l,const struct compat_16_sys___sigreturn14_args * uap,register_t * retval)211 compat_16_sys___sigreturn14(struct lwp *l,
212     const struct compat_16_sys___sigreturn14_args *uap, register_t *retval)
213 {
214 	/* {
215 		syscallarg(struct sigcontext *) sigcntxp;
216 	} */
217 	struct proc * const p = l->l_proc;
218 	struct sigcontext sc;
219 	struct utrapframe * const utf = &sc.sc_frame;
220 	int error;
221 
222 	/*
223 	 * The trampoline hands us the context.
224 	 * It is unsafe to keep track of it ourselves, in the event that a
225 	 * program jumps out of a signal handler.
226 	 */
227 	if ((error = copyin(SCARG(uap, sigcntxp), &sc, sizeof sc)) != 0)
228 		return (error);
229 
230 	/* Restore the register context. */
231 	struct trapframe * const tf = l->l_md.md_utf;
232 
233 	/*
234 	 * Make sure SRR1 hasn't been maliciously tampered with.
235 	 */
236 	if (!PSL_USEROK_P(sc.sc_frame.srr1))
237 		return (EINVAL);
238 
239 	/* Restore register context. */
240 	memcpy(tf->tf_fixreg, utf->fixreg, sizeof(tf->tf_fixreg));
241 	tf->tf_lr   = utf->lr;
242 	tf->tf_cr   = utf->cr;
243 	tf->tf_xer  = utf->xer;
244 	tf->tf_ctr  = utf->ctr;
245 	tf->tf_srr0 = utf->srr0;
246 	tf->tf_srr1 = utf->srr1;
247 
248 #ifdef PPC_HAVE_FPU
249 	struct pcb * const pcb = lwp_getpcb(l);
250 	pcb->pcb_flags &= ~(PCB_FE0|PCB_FE1);
251 	pcb->pcb_flags |= utf->srr1 & (PCB_FE0|PCB_FE1);
252 #endif
253 #if defined(ALTIVEC) || defined(PPC_HAVE_SPE)
254 	/*
255 	 * We can't round-trip the vector unit registers with a
256 	 * sigcontext, so at least force them to get reloaded from
257 	 * the PCB (we saved them into the PCB in sendsig_sigcontext()).
258 	 * XXX vec_restore_from_mcontext() has a special hack for this.
259 	 */
260 	vec_restore_from_mcontext(l, NULL);
261 #endif
262 #ifdef PPC_OEA
263 	tf->tf_vrsave = utf->vrsave;
264 	tf->tf_mq = utf->mq;
265 #endif
266 
267 	mutex_enter(p->p_lock);
268 	/* Restore signal stack. */
269 	if (sc.sc_onstack & SS_ONSTACK)
270 		l->l_sigstk.ss_flags |= SS_ONSTACK;
271 	else
272 		l->l_sigstk.ss_flags &= ~SS_ONSTACK;
273 	/* Restore signal mask. */
274 	(void) sigprocmask1(l, SIG_SETMASK, &sc.sc_mask, 0);
275 	mutex_exit(p->p_lock);
276 
277 	return (EJUSTRETURN);
278 }
279 
280 #endif /* !_LP64 */
281