blob: d75337974ee9b8ef8b8adb223f82cb4e17598659 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Ralf Baechle
8 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
Leonid Yegoshinca750642013-12-12 16:57:19 +00009 * Copyright (C) 2014, Imagination Technologies Ltd.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
Ralf Baechle02416dc2005-06-15 13:00:12 +000011#include <linux/cache.h>
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +020012#include <linux/context_tracking.h>
Ralf Baechle1f717922011-07-27 11:44:47 +010013#include <linux/irqflags.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/sched.h>
15#include <linux/mm.h>
16#include <linux/personality.h>
17#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel.h>
19#include <linux/signal.h>
20#include <linux/errno.h>
21#include <linux/wait.h>
22#include <linux/ptrace.h>
23#include <linux/unistd.h>
Ralf Baechle40e084a2015-07-29 22:44:53 +020024#include <linux/uprobes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/compiler.h>
Ralf Baechledbda6ac2009-02-08 16:00:26 +000026#include <linux/syscalls.h>
Atsushi Nemotofaea6232007-04-16 23:19:44 +090027#include <linux/uaccess.h>
David Howells733e5e42009-09-09 08:30:21 +010028#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Ralf Baechlee50c0a82005-05-31 11:49:19 +000030#include <asm/abi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/asm.h>
32#include <linux/bitops.h>
33#include <asm/cacheflush.h>
34#include <asm/fpu.h>
35#include <asm/sim.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/ucontext.h>
37#include <asm/cpu-features.h>
Ralf Baechle02416dc2005-06-15 13:00:12 +000038#include <asm/war.h>
David Howellsb81947c2012-03-28 18:30:02 +010039#include <asm/dsp.h>
Douglas Leung01be0572013-03-25 13:21:11 -050040#include <asm/inst.h>
Paul Burtonbf82cb32015-07-27 12:58:22 -070041#include <asm/msa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include "signal-common.h"
44
Paul Burton2db9ca02015-07-27 12:58:15 -070045static int (*save_fp_context)(void __user *sc);
46static int (*restore_fp_context)(void __user *sc);
Ralf Baechle137f6f32009-11-24 19:35:41 +000047
Ralf Baechle66680582007-02-13 01:31:48 +000048struct sigframe {
49 u32 sf_ass[4]; /* argument save space for o32 */
David Daneyd814c282010-02-18 16:13:05 -080050 u32 sf_pad[2]; /* Was: signal trampoline */
Paul Burtonf1fe2d22015-07-27 12:58:21 -070051
52 /* Matches struct ucontext from its uc_mcontext field onwards */
Ralf Baechle66680582007-02-13 01:31:48 +000053 struct sigcontext sf_sc;
54 sigset_t sf_mask;
Paul Burtonf1fe2d22015-07-27 12:58:21 -070055 unsigned long long sf_extcontext[0];
Ralf Baechle66680582007-02-13 01:31:48 +000056};
57
Franck Bui-Huuc0b9bae2007-02-05 15:24:21 +010058struct rt_sigframe {
59 u32 rs_ass[4]; /* argument save space for o32 */
David Daneyd814c282010-02-18 16:13:05 -080060 u32 rs_pad[2]; /* Was: signal trampoline */
Franck Bui-Huuc0b9bae2007-02-05 15:24:21 +010061 struct siginfo rs_info;
62 struct ucontext rs_uc;
63};
64
Paul Burton4eec81d2018-11-07 23:14:08 +000065#ifdef CONFIG_MIPS_FP_SUPPORT
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
Paul Burtonb2ead522014-01-27 15:23:02 +000068 * Thread saved context copy to/from a signal context presumed to be on the
69 * user stack, and therefore accessed with appropriate macros from uaccess.h.
70 */
Paul Burton2db9ca02015-07-27 12:58:15 -070071static int copy_fp_to_sigcontext(void __user *sc)
Paul Burtonb2ead522014-01-27 15:23:02 +000072{
Paul Burton2db9ca02015-07-27 12:58:15 -070073 struct mips_abi *abi = current->thread.abi;
74 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
75 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
Paul Burtonb2ead522014-01-27 15:23:02 +000076 int i;
77 int err = 0;
Paul Burton6f0aba62015-07-27 12:58:17 -070078 int inc = test_thread_flag(TIF_32BIT_FPREGS) ? 2 : 1;
Paul Burtonb2ead522014-01-27 15:23:02 +000079
Paul Burton6f0aba62015-07-27 12:58:17 -070080 for (i = 0; i < NUM_FPU_REGS; i += inc) {
Paul Burtonb2ead522014-01-27 15:23:02 +000081 err |=
82 __put_user(get_fpr64(&current->thread.fpu.fpr[i], 0),
Paul Burton2db9ca02015-07-27 12:58:15 -070083 &fpregs[i]);
Paul Burtonb2ead522014-01-27 15:23:02 +000084 }
Paul Burton2db9ca02015-07-27 12:58:15 -070085 err |= __put_user(current->thread.fpu.fcr31, csr);
Paul Burtonb2ead522014-01-27 15:23:02 +000086
87 return err;
88}
89
Paul Burton2db9ca02015-07-27 12:58:15 -070090static int copy_fp_from_sigcontext(void __user *sc)
Paul Burtonb2ead522014-01-27 15:23:02 +000091{
Paul Burton2db9ca02015-07-27 12:58:15 -070092 struct mips_abi *abi = current->thread.abi;
93 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
94 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
Paul Burtonb2ead522014-01-27 15:23:02 +000095 int i;
96 int err = 0;
Paul Burton6f0aba62015-07-27 12:58:17 -070097 int inc = test_thread_flag(TIF_32BIT_FPREGS) ? 2 : 1;
Paul Burtonb2ead522014-01-27 15:23:02 +000098 u64 fpr_val;
99
Paul Burton6f0aba62015-07-27 12:58:17 -0700100 for (i = 0; i < NUM_FPU_REGS; i += inc) {
Paul Burton2db9ca02015-07-27 12:58:15 -0700101 err |= __get_user(fpr_val, &fpregs[i]);
Paul Burtonb2ead522014-01-27 15:23:02 +0000102 set_fpr64(&current->thread.fpu.fpr[i], 0, fpr_val);
103 }
Paul Burton2db9ca02015-07-27 12:58:15 -0700104 err |= __get_user(current->thread.fpu.fcr31, csr);
Paul Burtonb2ead522014-01-27 15:23:02 +0000105
106 return err;
107}
108
Paul Burton4eec81d2018-11-07 23:14:08 +0000109#else /* !CONFIG_MIPS_FP_SUPPORT */
110
111static int copy_fp_to_sigcontext(void __user *sc)
112{
113 return 0;
114}
115
116static int copy_fp_from_sigcontext(void __user *sc)
117{
118 return 0;
119}
120
121#endif /* !CONFIG_MIPS_FP_SUPPORT */
122
Paul Burtonb2ead522014-01-27 15:23:02 +0000123/*
Paul Burton2db9ca02015-07-27 12:58:15 -0700124 * Wrappers for the assembly _{save,restore}_fp_context functions.
125 */
126static int save_hw_fp_context(void __user *sc)
127{
128 struct mips_abi *abi = current->thread.abi;
129 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
130 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
131
132 return _save_fp_context(fpregs, csr);
133}
134
135static int restore_hw_fp_context(void __user *sc)
136{
137 struct mips_abi *abi = current->thread.abi;
138 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
139 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
140
141 return _restore_fp_context(fpregs, csr);
142}
143
144/*
Paul Burtonbf82cb32015-07-27 12:58:22 -0700145 * Extended context handling.
146 */
147
148static inline void __user *sc_to_extcontext(void __user *sc)
149{
150 struct ucontext __user *uc;
151
152 /*
153 * We can just pretend the sigcontext is always embedded in a struct
154 * ucontext here, because the offset from sigcontext to extended
155 * context is the same in the struct sigframe case.
156 */
157 uc = container_of(sc, struct ucontext, uc_mcontext);
158 return &uc->uc_extcontext;
159}
160
Paul Burton4eec81d2018-11-07 23:14:08 +0000161#ifdef CONFIG_CPU_HAS_MSA
162
Paul Burtonbf82cb32015-07-27 12:58:22 -0700163static int save_msa_extcontext(void __user *buf)
164{
165 struct msa_extcontext __user *msa = buf;
166 uint64_t val;
167 int i, err;
168
169 if (!thread_msa_context_live())
170 return 0;
171
172 /*
173 * Ensure that we can't lose the live MSA context between checking
174 * for it & writing it to memory.
175 */
176 preempt_disable();
177
178 if (is_msa_enabled()) {
179 /*
180 * There are no EVA versions of the vector register load/store
181 * instructions, so MSA context has to be saved to kernel memory
182 * and then copied to user memory. The save to kernel memory
183 * should already have been done when handling scalar FP
184 * context.
185 */
Masahiro Yamada97f26452016-08-03 13:45:50 -0700186 BUG_ON(IS_ENABLED(CONFIG_EVA));
Paul Burtonbf82cb32015-07-27 12:58:22 -0700187
188 err = __put_user(read_msa_csr(), &msa->csr);
189 err |= _save_msa_all_upper(&msa->wr);
190
191 preempt_enable();
192 } else {
193 preempt_enable();
194
195 err = __put_user(current->thread.fpu.msacsr, &msa->csr);
196
197 for (i = 0; i < NUM_FPU_REGS; i++) {
198 val = get_fpr64(&current->thread.fpu.fpr[i], 1);
199 err |= __put_user(val, &msa->wr[i]);
200 }
201 }
202
203 err |= __put_user(MSA_EXTCONTEXT_MAGIC, &msa->ext.magic);
204 err |= __put_user(sizeof(*msa), &msa->ext.size);
205
206 return err ? -EFAULT : sizeof(*msa);
207}
208
209static int restore_msa_extcontext(void __user *buf, unsigned int size)
210{
211 struct msa_extcontext __user *msa = buf;
212 unsigned long long val;
213 unsigned int csr;
214 int i, err;
215
216 if (size != sizeof(*msa))
217 return -EINVAL;
218
219 err = get_user(csr, &msa->csr);
220 if (err)
221 return err;
222
223 preempt_disable();
224
225 if (is_msa_enabled()) {
226 /*
227 * There are no EVA versions of the vector register load/store
228 * instructions, so MSA context has to be copied to kernel
229 * memory and later loaded to registers. The same is true of
230 * scalar FP context, so FPU & MSA should have already been
231 * disabled whilst handling scalar FP context.
232 */
Masahiro Yamada97f26452016-08-03 13:45:50 -0700233 BUG_ON(IS_ENABLED(CONFIG_EVA));
Paul Burtonbf82cb32015-07-27 12:58:22 -0700234
235 write_msa_csr(csr);
236 err |= _restore_msa_all_upper(&msa->wr);
237 preempt_enable();
238 } else {
239 preempt_enable();
240
241 current->thread.fpu.msacsr = csr;
242
243 for (i = 0; i < NUM_FPU_REGS; i++) {
244 err |= __get_user(val, &msa->wr[i]);
245 set_fpr64(&current->thread.fpu.fpr[i], 1, val);
246 }
247 }
248
249 return err;
250}
251
Paul Burton4eec81d2018-11-07 23:14:08 +0000252#else /* !CONFIG_CPU_HAS_MSA */
253
254static int save_msa_extcontext(void __user *buf)
255{
256 return 0;
257}
258
259static int restore_msa_extcontext(void __user *buf, unsigned int size)
260{
261 return SIGSYS;
262}
263
264#endif /* !CONFIG_CPU_HAS_MSA */
265
Paul Burtonbf82cb32015-07-27 12:58:22 -0700266static int save_extcontext(void __user *buf)
267{
268 int sz;
269
270 sz = save_msa_extcontext(buf);
271 if (sz < 0)
272 return sz;
273 buf += sz;
274
275 /* If no context was saved then trivially return */
276 if (!sz)
277 return 0;
278
279 /* Write the end marker */
280 if (__put_user(END_EXTCONTEXT_MAGIC, (u32 *)buf))
281 return -EFAULT;
282
283 sz += sizeof(((struct extcontext *)NULL)->magic);
284 return sz;
285}
286
287static int restore_extcontext(void __user *buf)
288{
289 struct extcontext ext;
290 int err;
291
292 while (1) {
293 err = __get_user(ext.magic, (unsigned int *)buf);
294 if (err)
295 return err;
296
297 if (ext.magic == END_EXTCONTEXT_MAGIC)
298 return 0;
299
300 err = __get_user(ext.size, (unsigned int *)(buf
301 + offsetof(struct extcontext, size)));
302 if (err)
303 return err;
304
305 switch (ext.magic) {
306 case MSA_EXTCONTEXT_MAGIC:
307 err = restore_msa_extcontext(buf, ext.size);
308 break;
309
310 default:
311 err = -EINVAL;
312 break;
313 }
314
315 if (err)
316 return err;
317
318 buf += ext.size;
319 }
320}
321
322/*
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100323 * Helper routines
324 */
Paul Burtond02a40a2015-07-27 12:58:18 -0700325int protected_save_fp_context(void __user *sc)
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900326{
Paul Burton2db9ca02015-07-27 12:58:15 -0700327 struct mips_abi *abi = current->thread.abi;
328 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
329 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
Paul Burton64243c22015-07-27 12:58:16 -0700330 uint32_t __user *used_math = sc + abi->off_sc_used_math;
Paul Burtonbf82cb32015-07-27 12:58:22 -0700331 unsigned int used, ext_sz;
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900332 int err;
Paul Burton689ee852015-07-27 12:58:13 -0700333
Paul Burton0d071fa2015-07-27 12:58:20 -0700334 used = used_math() ? USED_FP : 0;
Paul Burtonbf82cb32015-07-27 12:58:22 -0700335 if (!used)
336 goto fp_done;
Paul Burton0d071fa2015-07-27 12:58:20 -0700337
Paul Burtonbf82cb32015-07-27 12:58:22 -0700338 if (!test_thread_flag(TIF_32BIT_FPREGS))
339 used |= USED_FR1;
340 if (test_thread_flag(TIF_HYBRID_FPREGS))
341 used |= USED_HYBRID_FPRS;
Paul Burton64243c22015-07-27 12:58:16 -0700342
Paul Burton689ee852015-07-27 12:58:13 -0700343 /*
344 * EVA does not have userland equivalents of ldc1 or sdc1, so
345 * save to the kernel FP context & copy that to userland below.
346 */
Masahiro Yamada97f26452016-08-03 13:45:50 -0700347 if (IS_ENABLED(CONFIG_EVA))
Paul Burton689ee852015-07-27 12:58:13 -0700348 lose_fpu(1);
349
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900350 while (1) {
351 lock_fpu_owner();
Paul Burtonff3aa5f2014-01-27 15:23:03 +0000352 if (is_fpu_owner()) {
353 err = save_fp_context(sc);
354 unlock_fpu_owner();
355 } else {
356 unlock_fpu_owner();
357 err = copy_fp_to_sigcontext(sc);
358 }
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900359 if (likely(!err))
360 break;
361 /* touch the sigcontext and try again */
Paul Burton2db9ca02015-07-27 12:58:15 -0700362 err = __put_user(0, &fpregs[0]) |
363 __put_user(0, &fpregs[31]) |
364 __put_user(0, csr);
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900365 if (err)
Paul Burtonbf82cb32015-07-27 12:58:22 -0700366 return err; /* really bad sigcontext */
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900367 }
Paul Burton689ee852015-07-27 12:58:13 -0700368
Paul Burtonbf82cb32015-07-27 12:58:22 -0700369fp_done:
370 ext_sz = err = save_extcontext(sc_to_extcontext(sc));
371 if (err < 0)
372 return err;
373 used |= ext_sz ? USED_EXTCONTEXT : 0;
374
375 return __put_user(used, used_math);
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900376}
377
Paul Burtond02a40a2015-07-27 12:58:18 -0700378int protected_restore_fp_context(void __user *sc)
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900379{
Paul Burton2db9ca02015-07-27 12:58:15 -0700380 struct mips_abi *abi = current->thread.abi;
381 uint64_t __user *fpregs = sc + abi->off_sc_fpregs;
382 uint32_t __user *csr = sc + abi->off_sc_fpc_csr;
Paul Burton64243c22015-07-27 12:58:16 -0700383 uint32_t __user *used_math = sc + abi->off_sc_used_math;
384 unsigned int used;
Paul Burtonbf82cb32015-07-27 12:58:22 -0700385 int err, sig = 0, tmp __maybe_unused;
Paul Burton64243c22015-07-27 12:58:16 -0700386
387 err = __get_user(used, used_math);
Paul Burton0d071fa2015-07-27 12:58:20 -0700388 conditional_used_math(used & USED_FP);
Paul Burton64243c22015-07-27 12:58:16 -0700389
390 /*
391 * The signal handler may have used FPU; give it up if the program
392 * doesn't want it following sigreturn.
393 */
Paul Burtonbf82cb32015-07-27 12:58:22 -0700394 if (err || !(used & USED_FP))
Paul Burton64243c22015-07-27 12:58:16 -0700395 lose_fpu(0);
Paul Burtonbf82cb32015-07-27 12:58:22 -0700396 if (err)
Paul Burton64243c22015-07-27 12:58:16 -0700397 return err;
Paul Burtonbf82cb32015-07-27 12:58:22 -0700398 if (!(used & USED_FP))
399 goto fp_done;
Paul Burton64243c22015-07-27 12:58:16 -0700400
401 err = sig = fpcsr_pending(csr);
402 if (err < 0)
403 return err;
Paul Burton689ee852015-07-27 12:58:13 -0700404
405 /*
406 * EVA does not have userland equivalents of ldc1 or sdc1, so we
407 * disable the FPU here such that the code below simply copies to
408 * the kernel FP context.
409 */
Masahiro Yamada97f26452016-08-03 13:45:50 -0700410 if (IS_ENABLED(CONFIG_EVA))
Paul Burton689ee852015-07-27 12:58:13 -0700411 lose_fpu(0);
412
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900413 while (1) {
414 lock_fpu_owner();
Paul Burtonff3aa5f2014-01-27 15:23:03 +0000415 if (is_fpu_owner()) {
416 err = restore_fp_context(sc);
417 unlock_fpu_owner();
418 } else {
419 unlock_fpu_owner();
420 err = copy_fp_from_sigcontext(sc);
421 }
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900422 if (likely(!err))
423 break;
424 /* touch the sigcontext and try again */
Paul Burton2db9ca02015-07-27 12:58:15 -0700425 err = __get_user(tmp, &fpregs[0]) |
426 __get_user(tmp, &fpregs[31]) |
427 __get_user(tmp, csr);
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900428 if (err)
429 break; /* really bad sigcontext */
430 }
Paul Burton689ee852015-07-27 12:58:13 -0700431
Paul Burtonbf82cb32015-07-27 12:58:22 -0700432fp_done:
Paul Burton6533af42016-04-21 18:04:53 +0100433 if (!err && (used & USED_EXTCONTEXT))
434 err = restore_extcontext(sc_to_extcontext(sc));
Paul Burtonbf82cb32015-07-27 12:58:22 -0700435
Paul Burton64243c22015-07-27 12:58:16 -0700436 return err ?: sig;
Atsushi Nemotofaea6232007-04-16 23:19:44 +0900437}
438
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100439int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
440{
441 int err = 0;
442 int i;
443
444 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
445
446 err |= __put_user(0, &sc->sc_regs[0]);
447 for (i = 1; i < 32; i++)
448 err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
449
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100450#ifdef CONFIG_CPU_HAS_SMARTMIPS
451 err |= __put_user(regs->acx, &sc->sc_acx);
452#endif
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100453 err |= __put_user(regs->hi, &sc->sc_mdhi);
454 err |= __put_user(regs->lo, &sc->sc_mdlo);
455 if (cpu_has_dsp) {
456 err |= __put_user(mfhi1(), &sc->sc_hi1);
457 err |= __put_user(mflo1(), &sc->sc_lo1);
458 err |= __put_user(mfhi2(), &sc->sc_hi2);
459 err |= __put_user(mflo2(), &sc->sc_lo2);
460 err |= __put_user(mfhi3(), &sc->sc_hi3);
461 err |= __put_user(mflo3(), &sc->sc_lo3);
462 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
463 }
464
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100465
Paul Burton64243c22015-07-27 12:58:16 -0700466 /*
467 * Save FPU state to signal context. Signal handler
468 * will "inherit" current FPU state.
469 */
470 err |= protected_save_fp_context(sc);
471
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100472 return err;
473}
474
Paul Burtonbf82cb32015-07-27 12:58:22 -0700475static size_t extcontext_max_size(void)
476{
477 size_t sz = 0;
478
479 /*
480 * The assumption here is that between this point & the point at which
481 * the extended context is saved the size of the context should only
482 * ever be able to shrink (if the task is preempted), but never grow.
483 * That is, what this function returns is an upper bound on the size of
484 * the extended context for the current task at the current time.
485 */
486
487 if (thread_msa_context_live())
488 sz += sizeof(struct msa_extcontext);
489
490 /* If any context is saved then we'll append the end marker */
491 if (sz)
492 sz += sizeof(((struct extcontext *)NULL)->magic);
493
494 return sz;
495}
496
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900497int fpcsr_pending(unsigned int __user *fpcsr)
498{
499 int err, sig = 0;
500 unsigned int csr, enabled;
501
502 err = __get_user(csr, fpcsr);
503 enabled = FPU_CSR_UNI_X | ((csr & FPU_CSR_ALL_E) << 5);
504 /*
505 * If the signal handler set some FPU exceptions, clear it and
506 * send SIGFPE.
507 */
508 if (csr & enabled) {
509 csr &= ~enabled;
510 err |= __put_user(csr, fpcsr);
511 sig = SIGFPE;
512 }
513 return err ?: sig;
514}
515
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100516int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
517{
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100518 unsigned long treg;
519 int err = 0;
520 int i;
521
522 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800523 current->restart_block.fn = do_no_restart_syscall;
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100524
525 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100526
527#ifdef CONFIG_CPU_HAS_SMARTMIPS
528 err |= __get_user(regs->acx, &sc->sc_acx);
529#endif
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100530 err |= __get_user(regs->hi, &sc->sc_mdhi);
531 err |= __get_user(regs->lo, &sc->sc_mdlo);
532 if (cpu_has_dsp) {
533 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
534 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
535 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
536 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
537 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
538 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
539 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
540 }
541
542 for (i = 1; i < 32; i++)
543 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
544
Paul Burton64243c22015-07-27 12:58:16 -0700545 return err ?: protected_restore_fp_context(sc);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100546}
547
Richard Weinberger7c4f5632014-03-05 15:35:41 +0100548void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs,
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100549 size_t frame_size)
550{
551 unsigned long sp;
552
Paul Burtonbf82cb32015-07-27 12:58:22 -0700553 /* Leave space for potential extended context */
554 frame_size += extcontext_max_size();
555
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100556 /* Default to using normal stack */
557 sp = regs->regs[29];
558
559 /*
560 * FPU emulator may have it's own trampoline active just
561 * above the user stack, 16-bytes before the next lowest
562 * 16 byte boundary. Try to avoid trashing it.
563 */
564 sp -= 32;
565
Richard Weinberger7c4f5632014-03-05 15:35:41 +0100566 sp = sigsp(sp, ksig);
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100567
568 return (void __user *)((sp - frame_size) & (ICACHE_REFILLS_WORKAROUND_WAR ? ~(cpu_icache_line_size()-1) : ALMASK));
569}
570
Franck Bui-Huuc3fc4ab2007-02-05 15:24:20 +0100571/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 * Atomically swap in the new signal mask, and wait for a signal.
573 */
574
575#ifdef CONFIG_TRAD_SIGNALS
Al Viro1910f4a2012-12-25 16:25:18 -0500576SYSCALL_DEFINE1(sigsuspend, sigset_t __user *, uset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Al Viro1910f4a2012-12-25 16:25:18 -0500578 return sys_rt_sigsuspend(uset, sizeof(sigset_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580#endif
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582#ifdef CONFIG_TRAD_SIGNALS
Ralf Baechledbda6ac2009-02-08 16:00:26 +0000583SYSCALL_DEFINE3(sigaction, int, sig, const struct sigaction __user *, act,
584 struct sigaction __user *, oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 struct k_sigaction new_ka, old_ka;
587 int ret;
588 int err = 0;
589
590 if (act) {
591 old_sigset_t mask;
592
Linus Torvalds96d4f262019-01-03 18:57:57 -0800593 if (!access_ok(act, sizeof(*act)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return -EFAULT;
595 err |= __get_user(new_ka.sa.sa_handler, &act->sa_handler);
596 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
597 err |= __get_user(mask, &act->sa_mask.sig[0]);
598 if (err)
599 return -EFAULT;
600
601 siginitset(&new_ka.sa.sa_mask, mask);
602 }
603
604 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
605
606 if (!ret && oact) {
Linus Torvalds96d4f262019-01-03 18:57:57 -0800607 if (!access_ok(oact, sizeof(*oact)))
Ralf Baechlee0daad42007-02-05 00:10:11 +0000608 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
610 err |= __put_user(old_ka.sa.sa_handler, &oact->sa_handler);
611 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
612 err |= __put_user(0, &oact->sa_mask.sig[1]);
613 err |= __put_user(0, &oact->sa_mask.sig[2]);
614 err |= __put_user(0, &oact->sa_mask.sig[3]);
615 if (err)
616 return -EFAULT;
617 }
618
619 return ret;
620}
621#endif
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623#ifdef CONFIG_TRAD_SIGNALS
Paul Burton96a68b12018-08-01 13:12:42 -0700624asmlinkage void sys_sigreturn(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900626 struct sigframe __user *frame;
Paul Burton96a68b12018-08-01 13:12:42 -0700627 struct pt_regs *regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 sigset_t blocked;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900629 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Paul Burton96a68b12018-08-01 13:12:42 -0700631 regs = current_pt_regs();
632 frame = (struct sigframe __user *)regs->regs[29];
Linus Torvalds96d4f262019-01-03 18:57:57 -0800633 if (!access_ok(frame, sizeof(*frame)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 goto badframe;
635 if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
636 goto badframe;
637
Matt Fleming8598f3c2012-02-14 11:40:52 +0000638 set_current_blocked(&blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Paul Burton96a68b12018-08-01 13:12:42 -0700640 sig = restore_sigcontext(regs, &frame->sf_sc);
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900641 if (sig < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 goto badframe;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900643 else if (sig)
644 force_sig(sig, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 /*
647 * Don't let your children do this ...
648 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 __asm__ __volatile__(
650 "move\t$29, %0\n\t"
651 "j\tsyscall_exit"
Paul Burton96a68b12018-08-01 13:12:42 -0700652 : /* no outputs */
653 : "r" (regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /* Unreached */
655
656badframe:
657 force_sig(SIGSEGV, current);
658}
Ralf Baechlee50c0a82005-05-31 11:49:19 +0000659#endif /* CONFIG_TRAD_SIGNALS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Paul Burton96a68b12018-08-01 13:12:42 -0700661asmlinkage void sys_rt_sigreturn(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900663 struct rt_sigframe __user *frame;
Paul Burton96a68b12018-08-01 13:12:42 -0700664 struct pt_regs *regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 sigset_t set;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900666 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Paul Burton96a68b12018-08-01 13:12:42 -0700668 regs = current_pt_regs();
669 frame = (struct rt_sigframe __user *)regs->regs[29];
Linus Torvalds96d4f262019-01-03 18:57:57 -0800670 if (!access_ok(frame, sizeof(*frame)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 goto badframe;
672 if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
673 goto badframe;
674
Matt Fleming8598f3c2012-02-14 11:40:52 +0000675 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Paul Burton96a68b12018-08-01 13:12:42 -0700677 sig = restore_sigcontext(regs, &frame->rs_uc.uc_mcontext);
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900678 if (sig < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 goto badframe;
Atsushi Nemotoc6a2f462007-03-10 01:03:48 +0900680 else if (sig)
681 force_sig(sig, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Al Viroea536ad2012-12-23 03:13:40 -0500683 if (restore_altstack(&frame->rs_uc.uc_stack))
684 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 /*
687 * Don't let your children do this ...
688 */
689 __asm__ __volatile__(
690 "move\t$29, %0\n\t"
691 "j\tsyscall_exit"
Paul Burton96a68b12018-08-01 13:12:42 -0700692 : /* no outputs */
693 : "r" (regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 /* Unreached */
695
696badframe:
697 force_sig(SIGSEGV, current);
698}
699
700#ifdef CONFIG_TRAD_SIGNALS
Richard Weinberger81d103b2013-10-06 22:25:42 +0200701static int setup_frame(void *sig_return, struct ksignal *ksig,
702 struct pt_regs *regs, sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900704 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 int err = 0;
706
Richard Weinberger7c4f5632014-03-05 15:35:41 +0100707 frame = get_sigframe(ksig, regs, sizeof(*frame));
Linus Torvalds96d4f262019-01-03 18:57:57 -0800708 if (!access_ok(frame, sizeof (*frame)))
Richard Weinberger81d103b2013-10-06 22:25:42 +0200709 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 err |= setup_sigcontext(regs, &frame->sf_sc);
712 err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
713 if (err)
Richard Weinberger81d103b2013-10-06 22:25:42 +0200714 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 /*
717 * Arguments to signal handler:
718 *
719 * a0 = signal number
720 * a1 = 0 (should be cause)
721 * a2 = pointer to struct sigcontext
722 *
723 * $25 and c0_epc point to the signal handler, $29 points to the
724 * struct sigframe.
725 */
Richard Weinberger81d103b2013-10-06 22:25:42 +0200726 regs->regs[ 4] = ksig->sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 regs->regs[ 5] = 0;
728 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
729 regs->regs[29] = (unsigned long) frame;
David Daneyd814c282010-02-18 16:13:05 -0800730 regs->regs[31] = (unsigned long) sig_return;
Richard Weinberger81d103b2013-10-06 22:25:42 +0200731 regs->cp0_epc = regs->regs[25] = (unsigned long) ksig->ka.sa.sa_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100733 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 current->comm, current->pid,
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100735 frame, regs->cp0_epc, regs->regs[31]);
Ralf Baechlee0daad42007-02-05 00:10:11 +0000736 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738#endif
739
Richard Weinberger81d103b2013-10-06 22:25:42 +0200740static int setup_rt_frame(void *sig_return, struct ksignal *ksig,
741 struct pt_regs *regs, sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900743 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 int err = 0;
745
Richard Weinberger7c4f5632014-03-05 15:35:41 +0100746 frame = get_sigframe(ksig, regs, sizeof(*frame));
Linus Torvalds96d4f262019-01-03 18:57:57 -0800747 if (!access_ok(frame, sizeof (*frame)))
Richard Weinberger81d103b2013-10-06 22:25:42 +0200748 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 /* Create siginfo. */
Richard Weinberger81d103b2013-10-06 22:25:42 +0200751 err |= copy_siginfo_to_user(&frame->rs_info, &ksig->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Ralf Baechle70342282013-01-22 12:59:30 +0100753 /* Create the ucontext. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 err |= __put_user(0, &frame->rs_uc.uc_flags);
Atsushi Nemoto5665a0a2006-02-02 01:26:34 +0900755 err |= __put_user(NULL, &frame->rs_uc.uc_link);
Al Viroea536ad2012-12-23 03:13:40 -0500756 err |= __save_altstack(&frame->rs_uc.uc_stack, regs->regs[29]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
758 err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
759
760 if (err)
Richard Weinberger81d103b2013-10-06 22:25:42 +0200761 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 /*
764 * Arguments to signal handler:
765 *
766 * a0 = signal number
767 * a1 = 0 (should be cause)
768 * a2 = pointer to ucontext
769 *
770 * $25 and c0_epc point to the signal handler, $29 points to
771 * the struct rt_sigframe.
772 */
Richard Weinberger81d103b2013-10-06 22:25:42 +0200773 regs->regs[ 4] = ksig->sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 regs->regs[ 5] = (unsigned long) &frame->rs_info;
775 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
776 regs->regs[29] = (unsigned long) frame;
David Daneyd814c282010-02-18 16:13:05 -0800777 regs->regs[31] = (unsigned long) sig_return;
Richard Weinberger81d103b2013-10-06 22:25:42 +0200778 regs->cp0_epc = regs->regs[25] = (unsigned long) ksig->ka.sa.sa_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100780 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 current->comm, current->pid,
782 frame, regs->cp0_epc, regs->regs[31]);
Franck Bui-Huu722bb632007-02-05 15:24:24 +0100783
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000784 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000787struct mips_abi mips_abi = {
788#ifdef CONFIG_TRAD_SIGNALS
789 .setup_frame = setup_frame,
790#endif
Ralf Baechle70342282013-01-22 12:59:30 +0100791 .setup_rt_frame = setup_rt_frame,
Paul Burton77856102015-07-27 12:58:14 -0700792 .restart = __NR_restart_syscall,
793
794 .off_sc_fpregs = offsetof(struct sigcontext, sc_fpregs),
795 .off_sc_fpc_csr = offsetof(struct sigcontext, sc_fpc_csr),
796 .off_sc_used_math = offsetof(struct sigcontext, sc_used_math),
Alex Smithebb5e782015-10-21 09:54:38 +0100797
798 .vdso = &vdso_image,
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000799};
800
Richard Weinberger81d103b2013-10-06 22:25:42 +0200801static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Al Virob7f9a112012-05-02 09:59:21 -0400803 sigset_t *oldset = sigmask_to_save();
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000804 int ret;
David Daneyd814c282010-02-18 16:13:05 -0800805 struct mips_abi *abi = current->thread.abi;
806 void *vdso = current->mm->context.vdso;
Ralf Baechle129bc8f2005-07-11 20:45:51 +0000807
Paul Burton432c6ba2016-07-08 11:06:19 +0100808 /*
809 * If we were emulating a delay slot instruction, exit that frame such
810 * that addresses in the sigframe are as expected for userland and we
811 * don't have a problem if we reuse the thread's frame for an
812 * instruction within the signal handler.
813 */
814 dsemul_thread_rollback(regs);
815
Al Viro8f5a00eb2010-09-28 18:50:37 +0100816 if (regs->regs[0]) {
817 switch(regs->regs[2]) {
818 case ERESTART_RESTARTBLOCK:
819 case ERESTARTNOHAND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 regs->regs[2] = EINTR;
821 break;
Al Viro8f5a00eb2010-09-28 18:50:37 +0100822 case ERESTARTSYS:
Richard Weinberger81d103b2013-10-06 22:25:42 +0200823 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
Al Viro8f5a00eb2010-09-28 18:50:37 +0100824 regs->regs[2] = EINTR;
825 break;
826 }
827 /* fallthrough */
828 case ERESTARTNOINTR:
829 regs->regs[7] = regs->regs[26];
830 regs->regs[2] = regs->regs[0];
831 regs->cp0_epc -= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Ralf Baechle70342282013-01-22 12:59:30 +0100834 regs->regs[0] = 0; /* Don't deal with this again. */
Al Viro8f5a00eb2010-09-28 18:50:37 +0100835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Paul Burton662d8552018-06-24 09:33:22 -0700837 rseq_signal_deliver(ksig, regs);
Paul Burton9ea141a2018-06-14 10:13:53 -0700838
Leonid Yegoshin79394692015-10-19 11:39:55 -0700839 if (sig_uses_siginfo(&ksig->ka, abi))
Alex Smithebb5e782015-10-21 09:54:38 +0100840 ret = abi->setup_rt_frame(vdso + abi->vdso->off_rt_sigreturn,
Richard Weinberger81d103b2013-10-06 22:25:42 +0200841 ksig, regs, oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 else
Alex Smithebb5e782015-10-21 09:54:38 +0100843 ret = abi->setup_frame(vdso + abi->vdso->off_sigreturn,
844 ksig, regs, oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Richard Weinberger81d103b2013-10-06 22:25:42 +0200846 signal_setup_done(ret, ksig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000849static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Richard Weinberger81d103b2013-10-06 22:25:42 +0200851 struct ksignal ksig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Richard Weinberger81d103b2013-10-06 22:25:42 +0200853 if (get_signal(&ksig)) {
Ralf Baechle70342282013-01-22 12:59:30 +0100854 /* Whee! Actually deliver the signal. */
Richard Weinberger81d103b2013-10-06 22:25:42 +0200855 handle_signal(&ksig, regs);
Ralf Baechle45887e12006-08-03 21:54:13 +0100856 return;
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (regs->regs[0]) {
Ralf Baechle9ec9b5a2012-11-06 14:27:19 +0100860 switch (regs->regs[2]) {
861 case ERESTARTNOHAND:
862 case ERESTARTSYS:
863 case ERESTARTNOINTR:
Al Viro8f5a00eb2010-09-28 18:50:37 +0100864 regs->regs[2] = regs->regs[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 regs->regs[7] = regs->regs[26];
Al Viro8f5a00eb2010-09-28 18:50:37 +0100866 regs->cp0_epc -= 4;
Ralf Baechle9ec9b5a2012-11-06 14:27:19 +0100867 break;
868
869 case ERESTART_RESTARTBLOCK:
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000870 regs->regs[2] = current->thread.abi->restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 regs->regs[7] = regs->regs[26];
872 regs->cp0_epc -= 4;
Ralf Baechle9ec9b5a2012-11-06 14:27:19 +0100873 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
Ralf Baechle70342282013-01-22 12:59:30 +0100875 regs->regs[0] = 0; /* Don't deal with this again. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000877
878 /*
879 * If there's no signal to deliver, we just put the saved sigmask
880 * back
881 */
Al Viro51a7b442012-05-21 23:33:55 -0400882 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
885/*
886 * notification of userspace execution resumption
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000887 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 */
Ralf Baechle7b3e2fc2006-02-08 12:58:41 +0000889asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 __u32 thread_info_flags)
891{
Ralf Baechle1f717922011-07-27 11:44:47 +0100892 local_irq_enable();
893
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200894 user_exit();
895
Ralf Baechle40e084a2015-07-29 22:44:53 +0200896 if (thread_info_flags & _TIF_UPROBE)
897 uprobe_notify_resume(regs);
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 /* deal with pending signal delivery */
Al Viro6fd84c02012-05-23 15:28:58 -0400900 if (thread_info_flags & _TIF_SIGPENDING)
Ralf Baechle151fd6a2007-02-15 11:40:37 +0000901 do_signal(regs);
David Howellsd0420c82009-09-02 09:14:16 +0100902
903 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
904 clear_thread_flag(TIF_NOTIFY_RESUME);
905 tracehook_notify_resume(regs);
Paul Burton662d8552018-06-24 09:33:22 -0700906 rseq_handle_notify_resume(NULL, regs);
David Howellsd0420c82009-09-02 09:14:16 +0100907 }
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200908
909 user_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
Ralf Baechle137f6f32009-11-24 19:35:41 +0000911
Paul Burton4eec81d2018-11-07 23:14:08 +0000912#if defined(CONFIG_SMP) && defined(CONFIG_MIPS_FP_SUPPORT)
Paul Burton2db9ca02015-07-27 12:58:15 -0700913static int smp_save_fp_context(void __user *sc)
Ralf Baechle137f6f32009-11-24 19:35:41 +0000914{
915 return raw_cpu_has_fpu
Paul Burton2db9ca02015-07-27 12:58:15 -0700916 ? save_hw_fp_context(sc)
Paul Burtonb2ead522014-01-27 15:23:02 +0000917 : copy_fp_to_sigcontext(sc);
Ralf Baechle137f6f32009-11-24 19:35:41 +0000918}
919
Paul Burton2db9ca02015-07-27 12:58:15 -0700920static int smp_restore_fp_context(void __user *sc)
Ralf Baechle137f6f32009-11-24 19:35:41 +0000921{
922 return raw_cpu_has_fpu
Paul Burton2db9ca02015-07-27 12:58:15 -0700923 ? restore_hw_fp_context(sc)
Paul Burtonb2ead522014-01-27 15:23:02 +0000924 : copy_fp_from_sigcontext(sc);
Ralf Baechle137f6f32009-11-24 19:35:41 +0000925}
926#endif
927
928static int signal_setup(void)
929{
Paul Burtonf1fe2d22015-07-27 12:58:21 -0700930 /*
931 * The offset from sigcontext to extended context should be the same
932 * regardless of the type of signal, such that userland can always know
933 * where to look if it wishes to find the extended context structures.
934 */
935 BUILD_BUG_ON((offsetof(struct sigframe, sf_extcontext) -
936 offsetof(struct sigframe, sf_sc)) !=
937 (offsetof(struct rt_sigframe, rs_uc.uc_extcontext) -
938 offsetof(struct rt_sigframe, rs_uc.uc_mcontext)));
939
Paul Burton4eec81d2018-11-07 23:14:08 +0000940#if defined(CONFIG_SMP) && defined(CONFIG_MIPS_FP_SUPPORT)
Ralf Baechle137f6f32009-11-24 19:35:41 +0000941 /* For now just do the cpu_has_fpu check when the functions are invoked */
942 save_fp_context = smp_save_fp_context;
943 restore_fp_context = smp_restore_fp_context;
944#else
945 if (cpu_has_fpu) {
Paul Burton2db9ca02015-07-27 12:58:15 -0700946 save_fp_context = save_hw_fp_context;
947 restore_fp_context = restore_hw_fp_context;
Ralf Baechle137f6f32009-11-24 19:35:41 +0000948 } else {
Paul Burton14fa12d2014-10-28 11:25:51 +0000949 save_fp_context = copy_fp_to_sigcontext;
950 restore_fp_context = copy_fp_from_sigcontext;
Ralf Baechle137f6f32009-11-24 19:35:41 +0000951 }
Leonid Yegoshinca750642013-12-12 16:57:19 +0000952#endif /* CONFIG_SMP */
Ralf Baechle137f6f32009-11-24 19:35:41 +0000953
954 return 0;
955}
956
957arch_initcall(signal_setup);