xref: /netbsd/sys/arch/m68k/m68k/compat_13_machdep.c (revision 6550d01e)
1 /*	$NetBSD: compat_13_machdep.c,v 1.14 2009/11/23 00:11:44 rmind Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * the Systems Programming Group of the University of Utah Computer
9  * Science Department.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	from: Utah Hdr: machdep.c 1.74 92/12/20
36  *	from: @(#)machdep.c	8.10 (Berkeley) 4/20/94
37  */
38 /*
39  * Copyright (c) 1988 University of Utah.
40  *
41  * This code is derived from software contributed to Berkeley by
42  * the Systems Programming Group of the University of Utah Computer
43  * Science Department.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  * 1. Redistributions of source code must retain the above copyright
49  *    notice, this list of conditions and the following disclaimer.
50  * 2. Redistributions in binary form must reproduce the above copyright
51  *    notice, this list of conditions and the following disclaimer in the
52  *    documentation and/or other materials provided with the distribution.
53  * 3. All advertising materials mentioning features or use of this software
54  *    must display the following acknowledgement:
55  *	This product includes software developed by the University of
56  *	California, Berkeley and its contributors.
57  * 4. Neither the name of the University nor the names of its contributors
58  *    may be used to endorse or promote products derived from this software
59  *    without specific prior written permission.
60  *
61  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71  * SUCH DAMAGE.
72  *
73  *	from: Utah Hdr: machdep.c 1.74 92/12/20
74  *	from: @(#)machdep.c	8.10 (Berkeley) 4/20/94
75  */
76 
77 #include <sys/cdefs.h>
78 __KERNEL_RCSID(0, "$NetBSD: compat_13_machdep.c,v 1.14 2009/11/23 00:11:44 rmind Exp $");
79 
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/kernel.h>
83 #include <sys/proc.h>
84 #include <sys/signal.h>
85 #include <sys/signalvar.h>
86 
87 #include <sys/mount.h>
88 #include <sys/syscallargs.h>
89 
90 #include <compat/sys/signal.h>
91 #include <compat/sys/signalvar.h>
92 
93 #include <machine/cpu.h>
94 #include <machine/reg.h>
95 
96 /*
97  * System call to cleanup state after a signal
98  * has been taken.  Reset signal mask and
99  * stack state from context left by sendsig (above).
100  * Return to previous pc and psl as specified by
101  * context left by sendsig. Check carefully to
102  * make sure that the user has not modified the
103  * psl to gain improper privileges or to cause
104  * a machine fault.
105  */
106 int
107 compat_13_sys_sigreturn(struct lwp *l, const struct compat_13_sys_sigreturn_args *uap, register_t *retval)
108 {
109 	/* {
110 		syscallarg(struct sigcontext13 *) sigcntxp;
111 	} */
112 	struct proc *p = l->l_proc;
113 	struct sigcontext13 *scp;
114 	struct frame *frame;
115 	struct sigcontext13 tsigc;
116 	sigset_t mask;
117 
118 	/*
119 	 * The trampoline code hands us the context.
120 	 * It is unsafe to keep track of it ourselves, in the event that a
121 	 * program jumps out of a signal handler.
122 	 */
123 	scp = SCARG(uap, sigcntxp);
124 	if ((int)scp & 1)
125 		return EINVAL;
126 
127 	if (copyin(scp, &tsigc, sizeof(tsigc)) != 0)
128 		return EFAULT;
129 	scp = &tsigc;
130 
131 	/* Make sure the user isn't pulling a fast one on us! */
132 	if ((scp->sc_ps & (PSL_MBZ|PSL_IPL|PSL_S)) != 0)
133 		return EINVAL;
134 
135 	/* Restore register context. */
136 	frame = (struct frame *)l->l_md.md_regs;
137 
138 	/*
139 	 * We only support restoring the sigcontext13 in this call.
140 	 * We are not called from the sigcode (per sendsig()), so
141 	 * we will not have a sigstate to restore.
142 	 */
143 	if (scp->sc_ap != 0)
144 		return EINVAL;
145 
146 	/*
147 	 * Restore the user supplied information.
148 	 * This should be at the last so that the error (EINVAL)
149 	 * is reported to the sigreturn caller, not to the
150 	 * jump destination.
151 	 */
152 
153 	frame->f_regs[SP] = scp->sc_sp;
154 	frame->f_regs[A6] = scp->sc_fp;
155 	frame->f_pc = scp->sc_pc;
156 	frame->f_sr = scp->sc_ps;
157 
158 	mutex_enter(p->p_lock);
159 
160 	/* Restore signal stack. */
161 	if (scp->sc_onstack & SS_ONSTACK)
162 		l->l_sigstk.ss_flags |= SS_ONSTACK;
163 	else
164 		l->l_sigstk.ss_flags &= ~SS_ONSTACK;
165 
166 	/* Restore signal mask. */
167 	native_sigset13_to_sigset(&scp->sc_mask, &mask);
168 	(void)sigprocmask1(l, SIG_SETMASK, &mask, 0);
169 
170 	mutex_exit(p->p_lock);
171 
172 	return EJUSTRETURN;
173 }
174