Merge tag 'soc-ep93xx-dt-6.12' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
[linux-2.6-block.git] / arch / sparc / kernel / signal32.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
5526b7e4 2/* arch/sparc64/kernel/signal32.c
1da177e4
LT
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
6 * Copyright (C) 1996 Miguel de Icaza (miguel@nuclecu.unam.mx)
7 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
8 * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
9 */
10
11#include <linux/sched.h>
12#include <linux/kernel.h>
13#include <linux/signal.h>
14#include <linux/errno.h>
15#include <linux/wait.h>
16#include <linux/ptrace.h>
17#include <linux/unistd.h>
18#include <linux/mm.h>
19#include <linux/tty.h>
1da177e4
LT
20#include <linux/binfmts.h>
21#include <linux/compat.h>
22#include <linux/bitops.h>
23
7c0f6ba6 24#include <linux/uaccess.h>
1da177e4 25#include <asm/ptrace.h>
1da177e4
LT
26#include <asm/psrcompat.h>
27#include <asm/fpumacro.h>
28#include <asm/visasm.h>
14cc6aba 29#include <asm/compat_signal.h>
d550bbd4 30#include <asm/switch_to.h>
1da177e4 31
5598473a 32#include "sigutil.h"
abaff455 33#include "kernel.h"
5598473a 34
1da177e4
LT
35/* This magic should be in g_upper[0] for all upper parts
36 * to be valid.
37 */
38#define SIGINFO_EXTRA_V8PLUS_MAGIC 0x130e269
39typedef struct {
40 unsigned int g_upper[8];
41 unsigned int o_upper[8];
42 unsigned int asi;
43} siginfo_extra_v8plus_t;
44
5526b7e4 45struct signal_frame32 {
1da177e4
LT
46 struct sparc_stackf32 ss;
47 __siginfo32_t info;
5598473a 48 /* __siginfo_fpu_t * */ u32 fpu_save;
1da177e4
LT
49 unsigned int insns[2];
50 unsigned int extramask[_COMPAT_NSIG_WORDS - 1];
51 unsigned int extra_size; /* Should be sizeof(siginfo_extra_v8plus_t) */
52 /* Only valid if (info.si_regs.psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS */
53 siginfo_extra_v8plus_t v8plus;
5598473a
DM
54 /* __siginfo_rwin_t * */u32 rwin_save;
55} __attribute__((aligned(8)));
1da177e4 56
1da177e4
LT
57struct rt_signal_frame32 {
58 struct sparc_stackf32 ss;
59 compat_siginfo_t info;
60 struct pt_regs32 regs;
61 compat_sigset_t mask;
5598473a 62 /* __siginfo_fpu_t * */ u32 fpu_save;
1da177e4 63 unsigned int insns[2];
99b06feb 64 compat_stack_t stack;
1da177e4
LT
65 unsigned int extra_size; /* Should be sizeof(siginfo_extra_v8plus_t) */
66 /* Only valid if (regs.psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS */
67 siginfo_extra_v8plus_t v8plus;
5598473a
DM
68 /* __siginfo_rwin_t * */u32 rwin_save;
69} __attribute__((aligned(8)));
1da177e4 70
d11c2a0d
DM
71/* Checks if the fp is valid. We always build signal frames which are
72 * 16-byte aligned, therefore we can always enforce that the restore
73 * frame has that property as well.
74 */
75static bool invalid_frame_pointer(void __user *fp, int fplen)
76{
77 if ((((unsigned long) fp) & 15) ||
78 ((unsigned long)fp) > 0x100000000ULL - fplen)
79 return true;
80 return false;
81}
82
5526b7e4 83void do_sigreturn32(struct pt_regs *regs)
1da177e4 84{
5526b7e4 85 struct signal_frame32 __user *sf;
5598473a
DM
86 compat_uptr_t fpu_save;
87 compat_uptr_t rwin_save;
d11c2a0d 88 unsigned int psr, ufp;
9ef595d8 89 unsigned int pc, npc;
1da177e4 90 sigset_t set;
c19ac326 91 compat_sigset_t seta;
1da177e4
LT
92 int err, i;
93
5526b7e4 94 /* Always make any pending restarted system calls return -EINTR */
f56141e3 95 current->restart_block.fn = do_no_restart_syscall;
5526b7e4
DM
96
97 synchronize_user_stack();
98
1da177e4 99 regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
5526b7e4 100 sf = (struct signal_frame32 __user *) regs->u_regs[UREG_FP];
1da177e4
LT
101
102 /* 1. Make sure we are not getting garbage from the user */
d11c2a0d
DM
103 if (invalid_frame_pointer(sf, sizeof(*sf)))
104 goto segv;
105
106 if (get_user(ufp, &sf->info.si_regs.u_regs[UREG_FP]))
107 goto segv;
108
109 if (ufp & 0x7)
1da177e4
LT
110 goto segv;
111
d11c2a0d 112 if (__get_user(pc, &sf->info.si_regs.pc) ||
187cd44e
AV
113 __get_user(npc, &sf->info.si_regs.npc))
114 goto segv;
1da177e4
LT
115
116 if ((pc | npc) & 3)
117 goto segv;
118
119 if (test_thread_flag(TIF_32BIT)) {
120 pc &= 0xffffffff;
121 npc &= 0xffffffff;
122 }
123 regs->tpc = pc;
124 regs->tnpc = npc;
125
126 /* 2. Restore the state */
127 err = __get_user(regs->y, &sf->info.si_regs.y);
128 err |= __get_user(psr, &sf->info.si_regs.psr);
129
130 for (i = UREG_G1; i <= UREG_I7; i++)
131 err |= __get_user(regs->u_regs[i], &sf->info.si_regs.u_regs[i]);
132 if ((psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS) {
133 err |= __get_user(i, &sf->v8plus.g_upper[0]);
134 if (i == SIGINFO_EXTRA_V8PLUS_MAGIC) {
135 unsigned long asi;
136
137 for (i = UREG_G1; i <= UREG_I7; i++)
138 err |= __get_user(((u32 *)regs->u_regs)[2*i], &sf->v8plus.g_upper[i]);
139 err |= __get_user(asi, &sf->v8plus.asi);
140 regs->tstate &= ~TSTATE_ASI;
141 regs->tstate |= ((asi & 0xffUL) << 24UL);
142 }
143 }
144
145 /* User can only change condition codes in %tstate. */
146 regs->tstate &= ~(TSTATE_ICC|TSTATE_XCC);
147 regs->tstate |= psr_to_tstate_icc(psr);
148
2678fefe 149 /* Prevent syscall restart. */
28e61036 150 pt_regs_clear_syscall(regs);
2678fefe 151
1da177e4 152 err |= __get_user(fpu_save, &sf->fpu_save);
5598473a
DM
153 if (!err && fpu_save)
154 err |= restore_fpu_state(regs, compat_ptr(fpu_save));
155 err |= __get_user(rwin_save, &sf->rwin_save);
156 if (!err && rwin_save) {
157 if (restore_rwin_state(compat_ptr(rwin_save)))
158 goto segv;
159 }
c19ac326
SR
160 err |= __get_user(seta.sig[0], &sf->info.si_mask);
161 err |= copy_from_user(&seta.sig[1], &sf->extramask,
1da177e4
LT
162 (_COMPAT_NSIG_WORDS - 1) * sizeof(unsigned int));
163 if (err)
164 goto segv;
c19ac326
SR
165
166 set.sig[0] = seta.sig[0] + (((long)seta.sig[1]) << 32);
faddf598 167 set_current_blocked(&set);
1da177e4
LT
168 return;
169
170segv:
3cf5d076 171 force_sig(SIGSEGV);
1da177e4
LT
172}
173
1da177e4
LT
174asmlinkage void do_rt_sigreturn32(struct pt_regs *regs)
175{
176 struct rt_signal_frame32 __user *sf;
d11c2a0d 177 unsigned int psr, pc, npc, ufp;
5598473a
DM
178 compat_uptr_t fpu_save;
179 compat_uptr_t rwin_save;
1da177e4 180 sigset_t set;
1da177e4
LT
181 int err, i;
182
183 /* Always make any pending restarted system calls return -EINTR */
f56141e3 184 current->restart_block.fn = do_no_restart_syscall;
1da177e4
LT
185
186 synchronize_user_stack();
187 regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
188 sf = (struct rt_signal_frame32 __user *) regs->u_regs[UREG_FP];
189
190 /* 1. Make sure we are not getting garbage from the user */
d11c2a0d 191 if (invalid_frame_pointer(sf, sizeof(*sf)))
1da177e4
LT
192 goto segv;
193
d11c2a0d
DM
194 if (get_user(ufp, &sf->regs.u_regs[UREG_FP]))
195 goto segv;
196
197 if (ufp & 0x7)
198 goto segv;
199
200 if (__get_user(pc, &sf->regs.pc) ||
187cd44e
AV
201 __get_user(npc, &sf->regs.npc))
202 goto segv;
1da177e4
LT
203
204 if ((pc | npc) & 3)
205 goto segv;
206
207 if (test_thread_flag(TIF_32BIT)) {
208 pc &= 0xffffffff;
209 npc &= 0xffffffff;
210 }
211 regs->tpc = pc;
212 regs->tnpc = npc;
213
214 /* 2. Restore the state */
215 err = __get_user(regs->y, &sf->regs.y);
216 err |= __get_user(psr, &sf->regs.psr);
217
218 for (i = UREG_G1; i <= UREG_I7; i++)
219 err |= __get_user(regs->u_regs[i], &sf->regs.u_regs[i]);
220 if ((psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS) {
221 err |= __get_user(i, &sf->v8plus.g_upper[0]);
222 if (i == SIGINFO_EXTRA_V8PLUS_MAGIC) {
223 unsigned long asi;
224
225 for (i = UREG_G1; i <= UREG_I7; i++)
226 err |= __get_user(((u32 *)regs->u_regs)[2*i], &sf->v8plus.g_upper[i]);
227 err |= __get_user(asi, &sf->v8plus.asi);
228 regs->tstate &= ~TSTATE_ASI;
229 regs->tstate |= ((asi & 0xffUL) << 24UL);
230 }
231 }
232
233 /* User can only change condition codes in %tstate. */
234 regs->tstate &= ~(TSTATE_ICC|TSTATE_XCC);
235 regs->tstate |= psr_to_tstate_icc(psr);
236
2678fefe 237 /* Prevent syscall restart. */
28e61036 238 pt_regs_clear_syscall(regs);
2678fefe 239
1da177e4 240 err |= __get_user(fpu_save, &sf->fpu_save);
5598473a
DM
241 if (!err && fpu_save)
242 err |= restore_fpu_state(regs, compat_ptr(fpu_save));
68c38fb6 243 err |= get_compat_sigset(&set, &sf->mask);
99b06feb 244 err |= compat_restore_altstack(&sf->stack);
1da177e4
LT
245 if (err)
246 goto segv;
247
5598473a
DM
248 err |= __get_user(rwin_save, &sf->rwin_save);
249 if (!err && rwin_save) {
250 if (restore_rwin_state(compat_ptr(rwin_save)))
251 goto segv;
252 }
253
faddf598 254 set_current_blocked(&set);
1da177e4
LT
255 return;
256segv:
3cf5d076 257 force_sig(SIGSEGV);
1da177e4
LT
258}
259
08f73957 260static void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs, unsigned long framesize)
1da177e4
LT
261{
262 unsigned long sp;
263
264 regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
265 sp = regs->u_regs[UREG_FP];
266
dc5dc7e6
DM
267 /*
268 * If we are on the alternate signal stack and would overflow it, don't.
269 * Return an always-bogus address instead so we will die with SIGSEGV.
270 */
271 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
272 return (void __user *) -1L;
273
1da177e4 274 /* This is the X/Open sanctioned signal stack switching. */
08f73957 275 sp = sigsp(sp, ksig) - framesize;
f036d9f3 276
dc5dc7e6
DM
277 /* Always align the stack frame. This handles two cases. First,
278 * sigaltstack need not be mindful of platform specific stack
279 * alignment. Second, if we took this signal because the stack
280 * is not aligned properly, we'd like to take the signal cleanly
281 * and report that.
282 */
f036d9f3 283 sp &= ~15UL;
dc5dc7e6 284
f036d9f3 285 return (void __user *) sp;
1da177e4
LT
286}
287
05c5e769
DM
288/* The I-cache flush instruction only works in the primary ASI, which
289 * right now is the nucleus, aka. kernel space.
290 *
291 * Therefore we have to kick the instructions out using the kernel
292 * side linear mapping of the physical address backing the user
293 * instructions.
294 */
295static void flush_signal_insns(unsigned long address)
296{
297 unsigned long pstate, paddr;
298 pte_t *ptep, pte;
299 pgd_t *pgdp;
5637bc50 300 p4d_t *p4dp;
05c5e769
DM
301 pud_t *pudp;
302 pmd_t *pmdp;
303
304 /* Commit all stores of the instructions we are about to flush. */
305 wmb();
306
307 /* Disable cross-call reception. In this way even a very wide
308 * munmap() on another cpu can't tear down the page table
309 * hierarchy from underneath us, since that can't complete
310 * until the IPI tlb flush returns.
311 */
312
313 __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
314 __asm__ __volatile__("wrpr %0, %1, %%pstate"
315 : : "r" (pstate), "i" (PSTATE_IE));
316
317 pgdp = pgd_offset(current->mm, address);
318 if (pgd_none(*pgdp))
319 goto out_irqs_on;
5637bc50
MR
320 p4dp = p4d_offset(pgdp, address);
321 if (p4d_none(*p4dp))
322 goto out_irqs_on;
323 pudp = pud_offset(p4dp, address);
05c5e769
DM
324 if (pud_none(*pudp))
325 goto out_irqs_on;
326 pmdp = pmd_offset(pudp, address);
327 if (pmd_none(*pmdp))
328 goto out_irqs_on;
329
330 ptep = pte_offset_map(pmdp, address);
4be14ec0
HD
331 if (!ptep)
332 goto out_irqs_on;
05c5e769
DM
333 pte = *ptep;
334 if (!pte_present(pte))
335 goto out_unmap;
336
337 paddr = (unsigned long) page_address(pte_page(pte));
338
339 __asm__ __volatile__("flush %0 + %1"
340 : /* no outputs */
341 : "r" (paddr),
342 "r" (address & (PAGE_SIZE - 1))
343 : "memory");
344
345out_unmap:
346 pte_unmap(ptep);
347out_irqs_on:
348 __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
349
350}
351
08f73957
AV
352static int setup_frame32(struct ksignal *ksig, struct pt_regs *regs,
353 sigset_t *oldset)
1da177e4 354{
5526b7e4 355 struct signal_frame32 __user *sf;
5598473a
DM
356 int i, err, wsaved;
357 void __user *tail;
1da177e4
LT
358 int sigframe_size;
359 u32 psr;
c19ac326 360 compat_sigset_t seta;
1da177e4
LT
361
362 /* 1. Make sure everything is clean */
363 synchronize_user_stack();
364 save_and_clear_fpu();
365
5598473a
DM
366 wsaved = get_thread_wsaved();
367
368 sigframe_size = sizeof(*sf);
369 if (current_thread_info()->fpsaved[0] & FPRS_FEF)
370 sigframe_size += sizeof(__siginfo_fpu_t);
371 if (wsaved)
372 sigframe_size += sizeof(__siginfo_rwin_t);
1da177e4 373
5526b7e4 374 sf = (struct signal_frame32 __user *)
08f73957 375 get_sigframe(ksig, regs, sigframe_size);
1da177e4 376
08f73957 377 if (invalid_frame_pointer(sf, sigframe_size)) {
5b4fc388
DM
378 if (show_unhandled_signals)
379 pr_info("%s[%d] bad frame in setup_frame32: %08lx TPC %08lx O7 %08lx\n",
380 current->comm, current->pid, (unsigned long)sf,
381 regs->tpc, regs->u_regs[UREG_I7]);
cb44c9a0 382 force_sigsegv(ksig->sig);
08f73957
AV
383 return -EINVAL;
384 }
1da177e4 385
5598473a 386 tail = (sf + 1);
1da177e4
LT
387
388 /* 2. Save the current process state */
389 if (test_thread_flag(TIF_32BIT)) {
390 regs->tpc &= 0xffffffff;
391 regs->tnpc &= 0xffffffff;
392 }
393 err = put_user(regs->tpc, &sf->info.si_regs.pc);
394 err |= __put_user(regs->tnpc, &sf->info.si_regs.npc);
395 err |= __put_user(regs->y, &sf->info.si_regs.y);
396 psr = tstate_to_psr(regs->tstate);
397 if (current_thread_info()->fpsaved[0] & FPRS_FEF)
398 psr |= PSR_EF;
399 err |= __put_user(psr, &sf->info.si_regs.psr);
400 for (i = 0; i < 16; i++)
401 err |= __put_user(regs->u_regs[i], &sf->info.si_regs.u_regs[i]);
402 err |= __put_user(sizeof(siginfo_extra_v8plus_t), &sf->extra_size);
403 err |= __put_user(SIGINFO_EXTRA_V8PLUS_MAGIC, &sf->v8plus.g_upper[0]);
404 for (i = 1; i < 16; i++)
405 err |= __put_user(((u32 *)regs->u_regs)[2*i],
406 &sf->v8plus.g_upper[i]);
407 err |= __put_user((regs->tstate & TSTATE_ASI) >> 24UL,
408 &sf->v8plus.asi);
409
410 if (psr & PSR_EF) {
5598473a
DM
411 __siginfo_fpu_t __user *fp = tail;
412 tail += sizeof(*fp);
413 err |= save_fpu_state(regs, fp);
414 err |= __put_user((u64)fp, &sf->fpu_save);
1da177e4
LT
415 } else {
416 err |= __put_user(0, &sf->fpu_save);
417 }
5598473a
DM
418 if (wsaved) {
419 __siginfo_rwin_t __user *rwp = tail;
420 tail += sizeof(*rwp);
421 err |= save_rwin_state(wsaved, rwp);
422 err |= __put_user((u64)rwp, &sf->rwin_save);
423 set_thread_wsaved(0);
424 } else {
425 err |= __put_user(0, &sf->rwin_save);
426 }
1da177e4 427
c19ac326
SR
428 /* If these change we need to know - assignments to seta relies on these sizes */
429 BUILD_BUG_ON(_NSIG_WORDS != 1);
430 BUILD_BUG_ON(_COMPAT_NSIG_WORDS != 2);
431 seta.sig[1] = (oldset->sig[0] >> 32);
432 seta.sig[0] = oldset->sig[0];
433
434 err |= __put_user(seta.sig[0], &sf->info.si_mask);
435 err |= __copy_to_user(sf->extramask, &seta.sig[1],
1da177e4
LT
436 (_COMPAT_NSIG_WORDS - 1) * sizeof(unsigned int));
437
5598473a 438 if (!wsaved) {
a7a08b27
AB
439 err |= raw_copy_in_user((u32 __user *)sf,
440 (u32 __user *)(regs->u_regs[UREG_FP]),
441 sizeof(struct reg_window32));
5598473a
DM
442 } else {
443 struct reg_window *rp;
444
445 rp = &current_thread_info()->reg_window[wsaved - 1];
446 for (i = 0; i < 8; i++)
447 err |= __put_user(rp->locals[i], &sf->ss.locals[i]);
448 for (i = 0; i < 6; i++)
449 err |= __put_user(rp->ins[i], &sf->ss.ins[i]);
450 err |= __put_user(rp->ins[6], &sf->ss.fp);
451 err |= __put_user(rp->ins[7], &sf->ss.callers_pc);
452 }
1da177e4 453 if (err)
08f73957 454 return err;
1da177e4
LT
455
456 /* 3. signal handler back-trampoline and parameters */
457 regs->u_regs[UREG_FP] = (unsigned long) sf;
08f73957 458 regs->u_regs[UREG_I0] = ksig->sig;
1da177e4
LT
459 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
460 regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
461
462 /* 4. signal handler */
08f73957 463 regs->tpc = (unsigned long) ksig->ka.sa.sa_handler;
1da177e4
LT
464 regs->tnpc = (regs->tpc + 4);
465 if (test_thread_flag(TIF_32BIT)) {
466 regs->tpc &= 0xffffffff;
467 regs->tnpc &= 0xffffffff;
468 }
469
470 /* 5. return to kernel instructions */
08f73957
AV
471 if (ksig->ka.ka_restorer) {
472 regs->u_regs[UREG_I7] = (unsigned long)ksig->ka.ka_restorer;
1da177e4 473 } else {
1da177e4 474 unsigned long address = ((unsigned long)&(sf->insns[0]));
1da177e4
LT
475
476 regs->u_regs[UREG_I7] = (unsigned long) (&(sf->insns[0]) - 2);
477
478 err = __put_user(0x821020d8, &sf->insns[0]); /*mov __NR_sigreturn, %g1*/
479 err |= __put_user(0x91d02010, &sf->insns[1]); /*t 0x10*/
480 if (err)
08f73957 481 return err;
05c5e769 482 flush_signal_insns(address);
1da177e4 483 }
c2785259 484 return 0;
1da177e4
LT
485}
486
08f73957
AV
487static int setup_rt_frame32(struct ksignal *ksig, struct pt_regs *regs,
488 sigset_t *oldset)
1da177e4
LT
489{
490 struct rt_signal_frame32 __user *sf;
5598473a
DM
491 int i, err, wsaved;
492 void __user *tail;
1da177e4
LT
493 int sigframe_size;
494 u32 psr;
1da177e4
LT
495
496 /* 1. Make sure everything is clean */
497 synchronize_user_stack();
498 save_and_clear_fpu();
499
5598473a
DM
500 wsaved = get_thread_wsaved();
501
502 sigframe_size = sizeof(*sf);
503 if (current_thread_info()->fpsaved[0] & FPRS_FEF)
504 sigframe_size += sizeof(__siginfo_fpu_t);
505 if (wsaved)
506 sigframe_size += sizeof(__siginfo_rwin_t);
1da177e4
LT
507
508 sf = (struct rt_signal_frame32 __user *)
08f73957 509 get_sigframe(ksig, regs, sigframe_size);
1da177e4 510
08f73957 511 if (invalid_frame_pointer(sf, sigframe_size)) {
5b4fc388
DM
512 if (show_unhandled_signals)
513 pr_info("%s[%d] bad frame in setup_rt_frame32: %08lx TPC %08lx O7 %08lx\n",
514 current->comm, current->pid, (unsigned long)sf,
515 regs->tpc, regs->u_regs[UREG_I7]);
cb44c9a0 516 force_sigsegv(ksig->sig);
08f73957
AV
517 return -EINVAL;
518 }
1da177e4 519
5598473a 520 tail = (sf + 1);
1da177e4
LT
521
522 /* 2. Save the current process state */
523 if (test_thread_flag(TIF_32BIT)) {
524 regs->tpc &= 0xffffffff;
525 regs->tnpc &= 0xffffffff;
526 }
527 err = put_user(regs->tpc, &sf->regs.pc);
528 err |= __put_user(regs->tnpc, &sf->regs.npc);
529 err |= __put_user(regs->y, &sf->regs.y);
530 psr = tstate_to_psr(regs->tstate);
531 if (current_thread_info()->fpsaved[0] & FPRS_FEF)
532 psr |= PSR_EF;
533 err |= __put_user(psr, &sf->regs.psr);
534 for (i = 0; i < 16; i++)
535 err |= __put_user(regs->u_regs[i], &sf->regs.u_regs[i]);
536 err |= __put_user(sizeof(siginfo_extra_v8plus_t), &sf->extra_size);
537 err |= __put_user(SIGINFO_EXTRA_V8PLUS_MAGIC, &sf->v8plus.g_upper[0]);
538 for (i = 1; i < 16; i++)
539 err |= __put_user(((u32 *)regs->u_regs)[2*i],
540 &sf->v8plus.g_upper[i]);
541 err |= __put_user((regs->tstate & TSTATE_ASI) >> 24UL,
542 &sf->v8plus.asi);
543
544 if (psr & PSR_EF) {
5598473a
DM
545 __siginfo_fpu_t __user *fp = tail;
546 tail += sizeof(*fp);
547 err |= save_fpu_state(regs, fp);
548 err |= __put_user((u64)fp, &sf->fpu_save);
1da177e4
LT
549 } else {
550 err |= __put_user(0, &sf->fpu_save);
551 }
5598473a
DM
552 if (wsaved) {
553 __siginfo_rwin_t __user *rwp = tail;
554 tail += sizeof(*rwp);
555 err |= save_rwin_state(wsaved, rwp);
556 err |= __put_user((u64)rwp, &sf->rwin_save);
557 set_thread_wsaved(0);
558 } else {
559 err |= __put_user(0, &sf->rwin_save);
560 }
1da177e4
LT
561
562 /* Update the siginfo structure. */
08f73957 563 err |= copy_siginfo_to_user32(&sf->info, &ksig->info);
1da177e4
LT
564
565 /* Setup sigaltstack */
99b06feb 566 err |= __compat_save_altstack(&sf->stack, regs->u_regs[UREG_FP]);
1da177e4 567
68c38fb6 568 err |= put_compat_sigset(&sf->mask, oldset, sizeof(compat_sigset_t));
1da177e4 569
5598473a 570 if (!wsaved) {
a7a08b27
AB
571 err |= raw_copy_in_user((u32 __user *)sf,
572 (u32 __user *)(regs->u_regs[UREG_FP]),
573 sizeof(struct reg_window32));
5598473a
DM
574 } else {
575 struct reg_window *rp;
576
577 rp = &current_thread_info()->reg_window[wsaved - 1];
578 for (i = 0; i < 8; i++)
579 err |= __put_user(rp->locals[i], &sf->ss.locals[i]);
580 for (i = 0; i < 6; i++)
581 err |= __put_user(rp->ins[i], &sf->ss.ins[i]);
582 err |= __put_user(rp->ins[6], &sf->ss.fp);
583 err |= __put_user(rp->ins[7], &sf->ss.callers_pc);
584 }
1da177e4 585 if (err)
08f73957 586 return err;
1da177e4
LT
587
588 /* 3. signal handler back-trampoline and parameters */
589 regs->u_regs[UREG_FP] = (unsigned long) sf;
08f73957 590 regs->u_regs[UREG_I0] = ksig->sig;
1da177e4
LT
591 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
592 regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
593
594 /* 4. signal handler */
08f73957 595 regs->tpc = (unsigned long) ksig->ka.sa.sa_handler;
1da177e4
LT
596 regs->tnpc = (regs->tpc + 4);
597 if (test_thread_flag(TIF_32BIT)) {
598 regs->tpc &= 0xffffffff;
599 regs->tnpc &= 0xffffffff;
600 }
601
602 /* 5. return to kernel instructions */
08f73957
AV
603 if (ksig->ka.ka_restorer)
604 regs->u_regs[UREG_I7] = (unsigned long)ksig->ka.ka_restorer;
1da177e4 605 else {
1da177e4 606 unsigned long address = ((unsigned long)&(sf->insns[0]));
1da177e4
LT
607
608 regs->u_regs[UREG_I7] = (unsigned long) (&(sf->insns[0]) - 2);
609
610 /* mov __NR_rt_sigreturn, %g1 */
611 err |= __put_user(0x82102065, &sf->insns[0]);
612
613 /* t 0x10 */
614 err |= __put_user(0x91d02010, &sf->insns[1]);
615 if (err)
08f73957 616 return err;
1da177e4 617
05c5e769 618 flush_signal_insns(address);
1da177e4 619 }
392c2180 620 return 0;
1da177e4
LT
621}
622
08f73957
AV
623static inline void handle_signal32(struct ksignal *ksig,
624 struct pt_regs *regs)
1da177e4 625{
08f73957 626 sigset_t *oldset = sigmask_to_save();
392c2180
DM
627 int err;
628
08f73957
AV
629 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
630 err = setup_rt_frame32(ksig, regs, oldset);
ec98c6b9 631 else
08f73957 632 err = setup_frame32(ksig, regs, oldset);
392c2180 633
08f73957 634 signal_setup_done(err, ksig, 0);
1da177e4
LT
635}
636
637static inline void syscall_restart32(unsigned long orig_i0, struct pt_regs *regs,
638 struct sigaction *sa)
639{
640 switch (regs->u_regs[UREG_I0]) {
641 case ERESTART_RESTARTBLOCK:
642 case ERESTARTNOHAND:
643 no_system_call_restart:
644 regs->u_regs[UREG_I0] = EINTR;
645 regs->tstate |= TSTATE_ICARRY;
646 break;
647 case ERESTARTSYS:
648 if (!(sa->sa_flags & SA_RESTART))
649 goto no_system_call_restart;
df561f66 650 fallthrough;
1da177e4
LT
651 case ERESTARTNOINTR:
652 regs->u_regs[UREG_I0] = orig_i0;
653 regs->tpc -= 4;
654 regs->tnpc -= 4;
655 }
656}
657
658/* Note that 'init' is a special process: it doesn't get signals it doesn't
659 * want to handle. Thus you cannot kill init even with a SIGKILL even by
660 * mistake.
661 */
dfbb83d3 662void do_signal32(struct pt_regs * regs)
1da177e4 663{
08f73957
AV
664 struct ksignal ksig;
665 unsigned long orig_i0 = 0;
666 int restart_syscall = 0;
667 bool has_handler = get_signal(&ksig);
28e61036 668
1d299bc7
DM
669 if (pt_regs_is_syscall(regs) &&
670 (regs->tstate & (TSTATE_XCARRY | TSTATE_ICARRY))) {
671 restart_syscall = 1;
e88d2468 672 orig_i0 = regs->u_regs[UREG_G6];
1d299bc7 673 }
28e61036 674
08f73957 675 if (has_handler) {
28e61036 676 if (restart_syscall)
08f73957
AV
677 syscall_restart32(orig_i0, regs, &ksig.ka.sa);
678 handle_signal32(&ksig, regs);
679 } else {
680 if (restart_syscall) {
681 switch (regs->u_regs[UREG_I0]) {
682 case ERESTARTNOHAND:
683 case ERESTARTSYS:
684 case ERESTARTNOINTR:
685 /* replay the system call when we are done */
686 regs->u_regs[UREG_I0] = orig_i0;
687 regs->tpc -= 4;
688 regs->tnpc -= 4;
689 pt_regs_clear_syscall(regs);
df561f66 690 fallthrough;
08f73957
AV
691 case ERESTART_RESTARTBLOCK:
692 regs->u_regs[UREG_G1] = __NR_restart_syscall;
693 regs->tpc -= 4;
694 regs->tnpc -= 4;
695 pt_regs_clear_syscall(regs);
696 }
697 }
698 restore_saved_sigmask();
1da177e4 699 }
1da177e4
LT
700}
701
702struct sigstack32 {
703 u32 the_stack;
704 int cur_status;
705};
706
707asmlinkage int do_sys32_sigstack(u32 u_ssptr, u32 u_ossptr, unsigned long sp)
708{
709 struct sigstack32 __user *ssptr =
710 (struct sigstack32 __user *)((unsigned long)(u_ssptr));
711 struct sigstack32 __user *ossptr =
712 (struct sigstack32 __user *)((unsigned long)(u_ossptr));
713 int ret = -EFAULT;
714
715 /* First see if old state is wanted. */
716 if (ossptr) {
717 if (put_user(current->sas_ss_sp + current->sas_ss_size,
718 &ossptr->the_stack) ||
719 __put_user(on_sig_stack(sp), &ossptr->cur_status))
720 goto out;
721 }
722
723 /* Now see if we want to update the new state. */
724 if (ssptr) {
725 u32 ss_sp;
726
727 if (get_user(ss_sp, &ssptr->the_stack))
728 goto out;
729
730 /* If the current stack was set with sigaltstack, don't
731 * swap stacks while we are on it.
732 */
733 ret = -EPERM;
734 if (current->sas_ss_sp && on_sig_stack(sp))
735 goto out;
736
737 /* Since we don't know the extent of the stack, and we don't
738 * track onstack-ness, but rather calculate it, we must
739 * presume a size. Ho hum this interface is lossy.
740 */
741 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
742 current->sas_ss_size = SIGSTKSZ;
743 }
744
745 ret = 0;
746out:
747 return ret;
748}
42365abd
ME
749
750/*
751 * Compile-time assertions for siginfo_t offsets. Check NSIG* as well, as
752 * changes likely come with new fields that should be added below.
753 */
754static_assert(NSIGILL == 11);
755static_assert(NSIGFPE == 15);
a5f6c2ac 756static_assert(NSIGSEGV == 10);
42365abd
ME
757static_assert(NSIGBUS == 5);
758static_assert(NSIGTRAP == 6);
759static_assert(NSIGCHLD == 6);
760static_assert(NSIGSYS == 2);
50ae8130
EB
761static_assert(sizeof(compat_siginfo_t) == 128);
762static_assert(__alignof__(compat_siginfo_t) == 4);
42365abd
ME
763static_assert(offsetof(compat_siginfo_t, si_signo) == 0x00);
764static_assert(offsetof(compat_siginfo_t, si_errno) == 0x04);
765static_assert(offsetof(compat_siginfo_t, si_code) == 0x08);
766static_assert(offsetof(compat_siginfo_t, si_pid) == 0x0c);
767static_assert(offsetof(compat_siginfo_t, si_uid) == 0x10);
768static_assert(offsetof(compat_siginfo_t, si_tid) == 0x0c);
769static_assert(offsetof(compat_siginfo_t, si_overrun) == 0x10);
770static_assert(offsetof(compat_siginfo_t, si_status) == 0x14);
771static_assert(offsetof(compat_siginfo_t, si_utime) == 0x18);
772static_assert(offsetof(compat_siginfo_t, si_stime) == 0x1c);
773static_assert(offsetof(compat_siginfo_t, si_value) == 0x14);
774static_assert(offsetof(compat_siginfo_t, si_int) == 0x14);
775static_assert(offsetof(compat_siginfo_t, si_ptr) == 0x14);
776static_assert(offsetof(compat_siginfo_t, si_addr) == 0x0c);
777static_assert(offsetof(compat_siginfo_t, si_trapno) == 0x10);
778static_assert(offsetof(compat_siginfo_t, si_addr_lsb) == 0x10);
779static_assert(offsetof(compat_siginfo_t, si_lower) == 0x14);
780static_assert(offsetof(compat_siginfo_t, si_upper) == 0x18);
781static_assert(offsetof(compat_siginfo_t, si_pkey) == 0x14);
782static_assert(offsetof(compat_siginfo_t, si_perf_data) == 0x10);
783static_assert(offsetof(compat_siginfo_t, si_perf_type) == 0x14);
78ed93d7 784static_assert(offsetof(compat_siginfo_t, si_perf_flags) == 0x18);
42365abd
ME
785static_assert(offsetof(compat_siginfo_t, si_band) == 0x0c);
786static_assert(offsetof(compat_siginfo_t, si_fd) == 0x10);