1 /*        $NetBSD: compat_16_machdep.c,v 1.5 2018/12/29 11:30:12 maxv Exp $     */
2 
3 /*
4  * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * Changed for the VAX port (and for readability) /IC
8  *
9  * This code is derived from software contributed to Berkeley by the Systems
10  * Programming Group of the University of Utah Computer 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 /*
42  * Copyright (c) 2002, Hugh Graham.
43  * Copyright (c) 1994, 1998 Ludd, University of Lule}, Sweden.
44  * Copyright (c) 1993 Adam Glass
45  * Copyright (c) 1988 University of Utah.
46  *
47  * Changed for the VAX port (and for readability) /IC
48  *
49  * This code is derived from software contributed to Berkeley by the Systems
50  * Programming Group of the University of Utah Computer Science Department.
51  *
52  * Redistribution and use in source and binary forms, with or without
53  * modification, are permitted provided that the following conditions
54  * are met:
55  * 1. Redistributions of source code must retain the above copyright
56  *    notice, this list of conditions and the following disclaimer.
57  * 2. Redistributions in binary form must reproduce the above copyright
58  *    notice, this list of conditions and the following disclaimer in the
59  *    documentation and/or other materials provided with the distribution.
60  * 3. All advertising materials mentioning features or use of this software
61  *    must display the following acknowledgement:
62  *        This product includes software developed by the University of
63  *        California, Berkeley and its contributors.
64  * 4. Neither the name of the University nor the names of its contributors
65  *    may be used to endorse or promote products derived from this software
66  *    without specific prior written permission.
67  *
68  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
69  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
70  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
71  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
72  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
73  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
74  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
75  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
76  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
77  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
78  * SUCH DAMAGE.
79  *
80  * from: Utah Hdr: machdep.c 1.63 91/04/24
81  *
82  * @(#)machdep.c    7.16 (Berkeley) 6/3/91
83  */
84 
85 #include <sys/cdefs.h>
86 __KERNEL_RCSID(0, "$NetBSD: compat_16_machdep.c,v 1.5 2018/12/29 11:30:12 maxv Exp $");
87 
88 #ifdef _KERNEL_OPT
89 #include "opt_ddb.h"
90 #include "opt_compat_netbsd.h"
91 #include "opt_compat_ultrix.h"
92 #include "opt_multiprocessor.h"
93 #include "opt_lockdebug.h"
94 #endif
95 
96 #include <sys/param.h>
97 #include <sys/systm.h>
98 #include <sys/cpu.h>
99 #include <sys/device.h>
100 #include <sys/proc.h>
101 #include <sys/signal.h>
102 #include <sys/syscallargs.h>
103 #include <sys/sysctl.h>
104 #include <sys/time.h>
105 
106 #include <dev/cons.h>
107 
108 #include <machine/macros.h>
109 #include <machine/reg.h>
110 #include <vax/vax/gencons.h>
111 
112 /*
113  * This code was moved here from sys/arch/vax/vax/sig_machdep.c
114  */
115 
116 int
compat_16_sys___sigreturn14(struct lwp * l,const struct compat_16_sys___sigreturn14_args * uap,register_t * retval)117 compat_16_sys___sigreturn14(struct lwp *l, const struct compat_16_sys___sigreturn14_args *uap, register_t *retval)
118 {
119           /* {
120                     syscallarg(struct sigcontext *) sigcntxp;
121           } */
122           struct proc * const p = l->l_proc;
123           struct trapframe * const tf = l->l_md.md_utf;
124           struct sigcontext * const ucntx = SCARG(uap, sigcntxp);
125           struct sigcontext ksc;
126 
127 
128           if (copyin((void *)ucntx, (void *)&ksc, sizeof(struct sigcontext)))
129                     return EINVAL;
130           /* Compatibility mode? */
131           if ((ksc.sc_ps & (PSL_IPL | PSL_IS)) ||
132               ((ksc.sc_ps & (PSL_U | PSL_PREVU)) != (PSL_U | PSL_PREVU)) ||
133               (ksc.sc_ps & PSL_CM)) {
134                     return (EINVAL);
135           }
136 
137           mutex_enter(p->p_lock);
138           if (ksc.sc_onstack & SS_ONSTACK)
139                     l->l_sigstk.ss_flags |= SS_ONSTACK;
140           else
141                     l->l_sigstk.ss_flags &= ~SS_ONSTACK;
142 
143           /* Restore signal mask. */
144           (void) sigprocmask1(l, SIG_SETMASK, &ksc.sc_mask, 0);
145           mutex_exit(p->p_lock);
146 
147           tf->tf_fp = ksc.sc_fp;
148           tf->tf_ap = ksc.sc_ap;
149           tf->tf_pc = ksc.sc_pc;
150           tf->tf_sp = ksc.sc_sp;
151           tf->tf_psl = ksc.sc_ps;
152           return (EJUSTRETURN);
153 }
154 
155 /*
156  * Brief description of how sendsig() works:
157  * A struct sigcontext is allocated on the user stack. The relevant
158  * registers are saved in it. Below it is a struct trampframe constructed, it
159  * is actually an argument list for callg. The user
160  * stack pointer is put below all structs.
161  *
162  * The registers will contain when the signal handler is called:
163  * pc, psl          - Obvious
164  * sp               - An address below all structs
165  * fp               - The address of the signal handler
166  * ap               - The address to the callg frame
167  *
168  * The trampoline code will save r0-r5 before doing anything else.
169  */
170 vaddr_t
setupstack_sigcontext2(const struct ksiginfo * ksi,const sigset_t * mask,int vers,struct lwp * l,struct trapframe * tf,vaddr_t sp,int onstack,vaddr_t handler)171 setupstack_sigcontext2(const struct ksiginfo *ksi, const sigset_t *mask,
172           int vers, struct lwp *l, struct trapframe *tf, vaddr_t sp, int onstack,
173           vaddr_t handler)
174 {
175           struct trampoline2 tramp;
176           struct sigcontext sigctx;
177           struct proc * const p = l->l_proc;
178           bool error;
179 
180           /* The sigcontext struct will be passed back to sigreturn().  */
181           sigctx.sc_pc = tf->tf_pc;
182           sigctx.sc_ps = tf->tf_psl;
183           sigctx.sc_ap = tf->tf_ap;
184           sigctx.sc_fp = tf->tf_fp;
185           sigctx.sc_sp = tf->tf_sp;
186           sigctx.sc_onstack = onstack ? SS_ONSTACK : 0;
187           sigctx.sc_mask = *mask;
188           sp -= sizeof(struct sigcontext);
189 
190           /* Arguments given to the signal handler.  */
191           tramp.narg = 3;
192           tramp.sig = ksi->ksi_signo;
193           tramp.code = (register_t)ksi->ksi_addr;
194           tramp.scp = sp;
195           sp -= sizeof(tramp);
196           sendsig_reset(l, ksi->ksi_signo);
197           mutex_exit(p->p_lock);
198 
199           /* Store the handler in the trapframe.  */
200           tf->tf_fp = handler;
201 
202           /* Copy out the sigcontext and trampoline.  */
203           error = (copyout(&sigctx, (char *)tramp.scp, sizeof(sigctx)) != 0 ||
204               copyout(&tramp, (char *)sp, sizeof(tramp)) != 0);
205 
206           mutex_enter(p->p_lock);
207           if (error)
208                     return 0;
209 
210           /* return updated stack pointer */
211           return sp;
212 }
213