xref: /openbmc/linux/arch/mips/kernel/signal_o32.c (revision aac5987a)
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 1991, 1992  Linus Torvalds
7  * Copyright (C) 1994 - 2000, 2006  Ralf Baechle
8  * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
9  * Copyright (C) 2016, Imagination Technologies Ltd.
10  */
11 #include <linux/compiler.h>
12 #include <linux/errno.h>
13 #include <linux/signal.h>
14 #include <linux/sched/signal.h>
15 #include <linux/uaccess.h>
16 
17 #include <asm/abi.h>
18 #include <asm/compat-signal.h>
19 #include <asm/dsp.h>
20 #include <asm/sim.h>
21 #include <asm/unistd.h>
22 
23 #include "signal-common.h"
24 
25 /*
26  * Including <asm/unistd.h> would give use the 64-bit syscall numbers ...
27  */
28 #define __NR_O32_restart_syscall	4253
29 
30 struct sigframe32 {
31 	u32 sf_ass[4];		/* argument save space for o32 */
32 	u32 sf_pad[2];		/* Was: signal trampoline */
33 	struct sigcontext32 sf_sc;
34 	compat_sigset_t sf_mask;
35 };
36 
37 struct ucontext32 {
38 	u32		    uc_flags;
39 	s32		    uc_link;
40 	compat_stack_t      uc_stack;
41 	struct sigcontext32 uc_mcontext;
42 	compat_sigset_t	    uc_sigmask;	  /* mask last for extensibility */
43 };
44 
45 struct rt_sigframe32 {
46 	u32 rs_ass[4];			/* argument save space for o32 */
47 	u32 rs_pad[2];			/* Was: signal trampoline */
48 	compat_siginfo_t rs_info;
49 	struct ucontext32 rs_uc;
50 };
51 
52 static int setup_sigcontext32(struct pt_regs *regs,
53 			      struct sigcontext32 __user *sc)
54 {
55 	int err = 0;
56 	int i;
57 
58 	err |= __put_user(regs->cp0_epc, &sc->sc_pc);
59 
60 	err |= __put_user(0, &sc->sc_regs[0]);
61 	for (i = 1; i < 32; i++)
62 		err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
63 
64 	err |= __put_user(regs->hi, &sc->sc_mdhi);
65 	err |= __put_user(regs->lo, &sc->sc_mdlo);
66 	if (cpu_has_dsp) {
67 		err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
68 		err |= __put_user(mfhi1(), &sc->sc_hi1);
69 		err |= __put_user(mflo1(), &sc->sc_lo1);
70 		err |= __put_user(mfhi2(), &sc->sc_hi2);
71 		err |= __put_user(mflo2(), &sc->sc_lo2);
72 		err |= __put_user(mfhi3(), &sc->sc_hi3);
73 		err |= __put_user(mflo3(), &sc->sc_lo3);
74 	}
75 
76 	/*
77 	 * Save FPU state to signal context.  Signal handler
78 	 * will "inherit" current FPU state.
79 	 */
80 	err |= protected_save_fp_context(sc);
81 
82 	return err;
83 }
84 
85 static int restore_sigcontext32(struct pt_regs *regs,
86 				struct sigcontext32 __user *sc)
87 {
88 	int err = 0;
89 	s32 treg;
90 	int i;
91 
92 	/* Always make any pending restarted system calls return -EINTR */
93 	current->restart_block.fn = do_no_restart_syscall;
94 
95 	err |= __get_user(regs->cp0_epc, &sc->sc_pc);
96 	err |= __get_user(regs->hi, &sc->sc_mdhi);
97 	err |= __get_user(regs->lo, &sc->sc_mdlo);
98 	if (cpu_has_dsp) {
99 		err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
100 		err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
101 		err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
102 		err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
103 		err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
104 		err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
105 		err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
106 	}
107 
108 	for (i = 1; i < 32; i++)
109 		err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
110 
111 	return err ?: protected_restore_fp_context(sc);
112 }
113 
114 static int setup_frame_32(void *sig_return, struct ksignal *ksig,
115 			  struct pt_regs *regs, sigset_t *set)
116 {
117 	struct sigframe32 __user *frame;
118 	int err = 0;
119 
120 	frame = get_sigframe(ksig, regs, sizeof(*frame));
121 	if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
122 		return -EFAULT;
123 
124 	err |= setup_sigcontext32(regs, &frame->sf_sc);
125 	err |= __copy_conv_sigset_to_user(&frame->sf_mask, set);
126 
127 	if (err)
128 		return -EFAULT;
129 
130 	/*
131 	 * Arguments to signal handler:
132 	 *
133 	 *   a0 = signal number
134 	 *   a1 = 0 (should be cause)
135 	 *   a2 = pointer to struct sigcontext
136 	 *
137 	 * $25 and c0_epc point to the signal handler, $29 points to the
138 	 * struct sigframe.
139 	 */
140 	regs->regs[ 4] = ksig->sig;
141 	regs->regs[ 5] = 0;
142 	regs->regs[ 6] = (unsigned long) &frame->sf_sc;
143 	regs->regs[29] = (unsigned long) frame;
144 	regs->regs[31] = (unsigned long) sig_return;
145 	regs->cp0_epc = regs->regs[25] = (unsigned long) ksig->ka.sa.sa_handler;
146 
147 	DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
148 	       current->comm, current->pid,
149 	       frame, regs->cp0_epc, regs->regs[31]);
150 
151 	return 0;
152 }
153 
154 asmlinkage void sys32_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
155 {
156 	struct rt_sigframe32 __user *frame;
157 	sigset_t set;
158 	int sig;
159 
160 	frame = (struct rt_sigframe32 __user *) regs.regs[29];
161 	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
162 		goto badframe;
163 	if (__copy_conv_sigset_from_user(&set, &frame->rs_uc.uc_sigmask))
164 		goto badframe;
165 
166 	set_current_blocked(&set);
167 
168 	sig = restore_sigcontext32(&regs, &frame->rs_uc.uc_mcontext);
169 	if (sig < 0)
170 		goto badframe;
171 	else if (sig)
172 		force_sig(sig, current);
173 
174 	if (compat_restore_altstack(&frame->rs_uc.uc_stack))
175 		goto badframe;
176 
177 	/*
178 	 * Don't let your children do this ...
179 	 */
180 	__asm__ __volatile__(
181 		"move\t$29, %0\n\t"
182 		"j\tsyscall_exit"
183 		:/* no outputs */
184 		:"r" (&regs));
185 	/* Unreached */
186 
187 badframe:
188 	force_sig(SIGSEGV, current);
189 }
190 
191 static int setup_rt_frame_32(void *sig_return, struct ksignal *ksig,
192 			     struct pt_regs *regs, sigset_t *set)
193 {
194 	struct rt_sigframe32 __user *frame;
195 	int err = 0;
196 
197 	frame = get_sigframe(ksig, regs, sizeof(*frame));
198 	if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
199 		return -EFAULT;
200 
201 	/* Convert (siginfo_t -> compat_siginfo_t) and copy to user. */
202 	err |= copy_siginfo_to_user32(&frame->rs_info, &ksig->info);
203 
204 	/* Create the ucontext.	 */
205 	err |= __put_user(0, &frame->rs_uc.uc_flags);
206 	err |= __put_user(0, &frame->rs_uc.uc_link);
207 	err |= __compat_save_altstack(&frame->rs_uc.uc_stack, regs->regs[29]);
208 	err |= setup_sigcontext32(regs, &frame->rs_uc.uc_mcontext);
209 	err |= __copy_conv_sigset_to_user(&frame->rs_uc.uc_sigmask, set);
210 
211 	if (err)
212 		return -EFAULT;
213 
214 	/*
215 	 * Arguments to signal handler:
216 	 *
217 	 *   a0 = signal number
218 	 *   a1 = 0 (should be cause)
219 	 *   a2 = pointer to ucontext
220 	 *
221 	 * $25 and c0_epc point to the signal handler, $29 points to
222 	 * the struct rt_sigframe32.
223 	 */
224 	regs->regs[ 4] = ksig->sig;
225 	regs->regs[ 5] = (unsigned long) &frame->rs_info;
226 	regs->regs[ 6] = (unsigned long) &frame->rs_uc;
227 	regs->regs[29] = (unsigned long) frame;
228 	regs->regs[31] = (unsigned long) sig_return;
229 	regs->cp0_epc = regs->regs[25] = (unsigned long) ksig->ka.sa.sa_handler;
230 
231 	DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
232 	       current->comm, current->pid,
233 	       frame, regs->cp0_epc, regs->regs[31]);
234 
235 	return 0;
236 }
237 
238 /*
239  * o32 compatibility on 64-bit kernels, without DSP ASE
240  */
241 struct mips_abi mips_abi_32 = {
242 	.setup_frame	= setup_frame_32,
243 	.setup_rt_frame = setup_rt_frame_32,
244 	.restart	= __NR_O32_restart_syscall,
245 
246 	.off_sc_fpregs = offsetof(struct sigcontext32, sc_fpregs),
247 	.off_sc_fpc_csr = offsetof(struct sigcontext32, sc_fpc_csr),
248 	.off_sc_used_math = offsetof(struct sigcontext32, sc_used_math),
249 
250 	.vdso		= &vdso_image_o32,
251 };
252 
253 
254 asmlinkage void sys32_sigreturn(nabi_no_regargs struct pt_regs regs)
255 {
256 	struct sigframe32 __user *frame;
257 	sigset_t blocked;
258 	int sig;
259 
260 	frame = (struct sigframe32 __user *) regs.regs[29];
261 	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
262 		goto badframe;
263 	if (__copy_conv_sigset_from_user(&blocked, &frame->sf_mask))
264 		goto badframe;
265 
266 	set_current_blocked(&blocked);
267 
268 	sig = restore_sigcontext32(&regs, &frame->sf_sc);
269 	if (sig < 0)
270 		goto badframe;
271 	else if (sig)
272 		force_sig(sig, current);
273 
274 	/*
275 	 * Don't let your children do this ...
276 	 */
277 	__asm__ __volatile__(
278 		"move\t$29, %0\n\t"
279 		"j\tsyscall_exit"
280 		:/* no outputs */
281 		:"r" (&regs));
282 	/* Unreached */
283 
284 badframe:
285 	force_sig(SIGSEGV, current);
286 }
287