blob: 4c5b3fcbed94c376a2a44bb0c4fdaab139b3a169 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
David S. Miller5526b7e2008-04-27 02:26:36 -07002/* arch/sparc64/kernel/signal32.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/binfmts.h>
21#include <linux/compat.h>
22#include <linux/bitops.h>
Roland McGrath95698462008-07-27 01:08:02 -070023#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080025#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/pgtable.h>
28#include <asm/psrcompat.h>
29#include <asm/fpumacro.h>
30#include <asm/visasm.h>
David S. Miller14cc6ab2006-10-02 14:17:57 -070031#include <asm/compat_signal.h>
David Howellsd550bbd2012-03-28 18:30:03 +010032#include <asm/switch_to.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
David S. Miller55984732011-08-20 17:14:54 -070034#include "sigutil.h"
Sam Ravnborgabaff452014-05-16 23:26:00 +020035#include "kernel.h"
David S. Miller55984732011-08-20 17:14:54 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/* This magic should be in g_upper[0] for all upper parts
38 * to be valid.
39 */
40#define SIGINFO_EXTRA_V8PLUS_MAGIC 0x130e269
41typedef struct {
42 unsigned int g_upper[8];
43 unsigned int o_upper[8];
44 unsigned int asi;
45} siginfo_extra_v8plus_t;
46
David S. Miller5526b7e2008-04-27 02:26:36 -070047struct signal_frame32 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 struct sparc_stackf32 ss;
49 __siginfo32_t info;
David S. Miller55984732011-08-20 17:14:54 -070050 /* __siginfo_fpu_t * */ u32 fpu_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 unsigned int insns[2];
52 unsigned int extramask[_COMPAT_NSIG_WORDS - 1];
53 unsigned int extra_size; /* Should be sizeof(siginfo_extra_v8plus_t) */
54 /* Only valid if (info.si_regs.psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS */
55 siginfo_extra_v8plus_t v8plus;
David S. Miller55984732011-08-20 17:14:54 -070056 /* __siginfo_rwin_t * */u32 rwin_save;
57} __attribute__((aligned(8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059struct rt_signal_frame32 {
60 struct sparc_stackf32 ss;
61 compat_siginfo_t info;
62 struct pt_regs32 regs;
63 compat_sigset_t mask;
David S. Miller55984732011-08-20 17:14:54 -070064 /* __siginfo_fpu_t * */ u32 fpu_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 unsigned int insns[2];
Al Viro99b06fe2012-12-23 03:41:17 -050066 compat_stack_t stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 unsigned int extra_size; /* Should be sizeof(siginfo_extra_v8plus_t) */
68 /* Only valid if (regs.psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS */
69 siginfo_extra_v8plus_t v8plus;
David S. Miller55984732011-08-20 17:14:54 -070070 /* __siginfo_rwin_t * */u32 rwin_save;
71} __attribute__((aligned(8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
David S. Millerd11c2a02016-05-28 21:21:31 -070073/* Checks if the fp is valid. We always build signal frames which are
74 * 16-byte aligned, therefore we can always enforce that the restore
75 * frame has that property as well.
76 */
77static bool invalid_frame_pointer(void __user *fp, int fplen)
78{
79 if ((((unsigned long) fp) & 15) ||
80 ((unsigned long)fp) > 0x100000000ULL - fplen)
81 return true;
82 return false;
83}
84
David S. Miller5526b7e2008-04-27 02:26:36 -070085void do_sigreturn32(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
David S. Miller5526b7e2008-04-27 02:26:36 -070087 struct signal_frame32 __user *sf;
David S. Miller55984732011-08-20 17:14:54 -070088 compat_uptr_t fpu_save;
89 compat_uptr_t rwin_save;
David S. Millerd11c2a02016-05-28 21:21:31 -070090 unsigned int psr, ufp;
Joe Perches9ef595d2016-03-10 15:21:43 -080091 unsigned int pc, npc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 sigset_t set;
Sam Ravnborgc19ac322014-05-16 23:26:01 +020093 compat_sigset_t seta;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 int err, i;
95
David S. Miller5526b7e2008-04-27 02:26:36 -070096 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -080097 current->restart_block.fn = do_no_restart_syscall;
David S. Miller5526b7e2008-04-27 02:26:36 -070098
99 synchronize_user_stack();
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
David S. Miller5526b7e2008-04-27 02:26:36 -0700102 sf = (struct signal_frame32 __user *) regs->u_regs[UREG_FP];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 /* 1. Make sure we are not getting garbage from the user */
David S. Millerd11c2a02016-05-28 21:21:31 -0700105 if (invalid_frame_pointer(sf, sizeof(*sf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 goto segv;
107
David S. Millerd11c2a02016-05-28 21:21:31 -0700108 if (get_user(ufp, &sf->info.si_regs.u_regs[UREG_FP]))
109 goto segv;
110
111 if (ufp & 0x7)
112 goto segv;
113
114 if (__get_user(pc, &sf->info.si_regs.pc) ||
Al Viro187cd442012-04-22 16:51:36 -0400115 __get_user(npc, &sf->info.si_regs.npc))
116 goto segv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 if ((pc | npc) & 3)
119 goto segv;
120
121 if (test_thread_flag(TIF_32BIT)) {
122 pc &= 0xffffffff;
123 npc &= 0xffffffff;
124 }
125 regs->tpc = pc;
126 regs->tnpc = npc;
127
128 /* 2. Restore the state */
129 err = __get_user(regs->y, &sf->info.si_regs.y);
130 err |= __get_user(psr, &sf->info.si_regs.psr);
131
132 for (i = UREG_G1; i <= UREG_I7; i++)
133 err |= __get_user(regs->u_regs[i], &sf->info.si_regs.u_regs[i]);
134 if ((psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS) {
135 err |= __get_user(i, &sf->v8plus.g_upper[0]);
136 if (i == SIGINFO_EXTRA_V8PLUS_MAGIC) {
137 unsigned long asi;
138
139 for (i = UREG_G1; i <= UREG_I7; i++)
140 err |= __get_user(((u32 *)regs->u_regs)[2*i], &sf->v8plus.g_upper[i]);
141 err |= __get_user(asi, &sf->v8plus.asi);
142 regs->tstate &= ~TSTATE_ASI;
143 regs->tstate |= ((asi & 0xffUL) << 24UL);
144 }
145 }
146
147 /* User can only change condition codes in %tstate. */
148 regs->tstate &= ~(TSTATE_ICC|TSTATE_XCC);
149 regs->tstate |= psr_to_tstate_icc(psr);
150
David S. Miller2678fef2008-05-01 03:30:22 -0700151 /* Prevent syscall restart. */
David S. Miller28e61032008-05-11 02:07:19 -0700152 pt_regs_clear_syscall(regs);
David S. Miller2678fef2008-05-01 03:30:22 -0700153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 err |= __get_user(fpu_save, &sf->fpu_save);
David S. Miller55984732011-08-20 17:14:54 -0700155 if (!err && fpu_save)
156 err |= restore_fpu_state(regs, compat_ptr(fpu_save));
157 err |= __get_user(rwin_save, &sf->rwin_save);
158 if (!err && rwin_save) {
159 if (restore_rwin_state(compat_ptr(rwin_save)))
160 goto segv;
161 }
Sam Ravnborgc19ac322014-05-16 23:26:01 +0200162 err |= __get_user(seta.sig[0], &sf->info.si_mask);
163 err |= copy_from_user(&seta.sig[1], &sf->extramask,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 (_COMPAT_NSIG_WORDS - 1) * sizeof(unsigned int));
165 if (err)
166 goto segv;
Sam Ravnborgc19ac322014-05-16 23:26:01 +0200167
168 set.sig[0] = seta.sig[0] + (((long)seta.sig[1]) << 32);
Matt Flemingfaddf592011-08-11 14:57:02 +0100169 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return;
171
172segv:
173 force_sig(SIGSEGV, current);
174}
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176asmlinkage void do_rt_sigreturn32(struct pt_regs *regs)
177{
178 struct rt_signal_frame32 __user *sf;
David S. Millerd11c2a02016-05-28 21:21:31 -0700179 unsigned int psr, pc, npc, ufp;
David S. Miller55984732011-08-20 17:14:54 -0700180 compat_uptr_t fpu_save;
181 compat_uptr_t rwin_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 sigset_t set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 int err, i;
184
185 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800186 current->restart_block.fn = do_no_restart_syscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 synchronize_user_stack();
189 regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
190 sf = (struct rt_signal_frame32 __user *) regs->u_regs[UREG_FP];
191
192 /* 1. Make sure we are not getting garbage from the user */
David S. Millerd11c2a02016-05-28 21:21:31 -0700193 if (invalid_frame_pointer(sf, sizeof(*sf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 goto segv;
195
David S. Millerd11c2a02016-05-28 21:21:31 -0700196 if (get_user(ufp, &sf->regs.u_regs[UREG_FP]))
197 goto segv;
198
199 if (ufp & 0x7)
200 goto segv;
201
202 if (__get_user(pc, &sf->regs.pc) ||
Al Viro187cd442012-04-22 16:51:36 -0400203 __get_user(npc, &sf->regs.npc))
204 goto segv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 if ((pc | npc) & 3)
207 goto segv;
208
209 if (test_thread_flag(TIF_32BIT)) {
210 pc &= 0xffffffff;
211 npc &= 0xffffffff;
212 }
213 regs->tpc = pc;
214 regs->tnpc = npc;
215
216 /* 2. Restore the state */
217 err = __get_user(regs->y, &sf->regs.y);
218 err |= __get_user(psr, &sf->regs.psr);
219
220 for (i = UREG_G1; i <= UREG_I7; i++)
221 err |= __get_user(regs->u_regs[i], &sf->regs.u_regs[i]);
222 if ((psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS) {
223 err |= __get_user(i, &sf->v8plus.g_upper[0]);
224 if (i == SIGINFO_EXTRA_V8PLUS_MAGIC) {
225 unsigned long asi;
226
227 for (i = UREG_G1; i <= UREG_I7; i++)
228 err |= __get_user(((u32 *)regs->u_regs)[2*i], &sf->v8plus.g_upper[i]);
229 err |= __get_user(asi, &sf->v8plus.asi);
230 regs->tstate &= ~TSTATE_ASI;
231 regs->tstate |= ((asi & 0xffUL) << 24UL);
232 }
233 }
234
235 /* User can only change condition codes in %tstate. */
236 regs->tstate &= ~(TSTATE_ICC|TSTATE_XCC);
237 regs->tstate |= psr_to_tstate_icc(psr);
238
David S. Miller2678fef2008-05-01 03:30:22 -0700239 /* Prevent syscall restart. */
David S. Miller28e61032008-05-11 02:07:19 -0700240 pt_regs_clear_syscall(regs);
David S. Miller2678fef2008-05-01 03:30:22 -0700241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 err |= __get_user(fpu_save, &sf->fpu_save);
David S. Miller55984732011-08-20 17:14:54 -0700243 if (!err && fpu_save)
244 err |= restore_fpu_state(regs, compat_ptr(fpu_save));
Al Viro68c38fb2017-09-04 12:28:06 -0400245 err |= get_compat_sigset(&set, &sf->mask);
Al Viro99b06fe2012-12-23 03:41:17 -0500246 err |= compat_restore_altstack(&sf->stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (err)
248 goto segv;
249
David S. Miller55984732011-08-20 17:14:54 -0700250 err |= __get_user(rwin_save, &sf->rwin_save);
251 if (!err && rwin_save) {
252 if (restore_rwin_state(compat_ptr(rwin_save)))
253 goto segv;
254 }
255
Matt Flemingfaddf592011-08-11 14:57:02 +0100256 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return;
258segv:
259 force_sig(SIGSEGV, current);
260}
261
Al Viro08f739572012-11-07 23:48:13 -0500262static void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs, unsigned long framesize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 unsigned long sp;
265
266 regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
267 sp = regs->u_regs[UREG_FP];
268
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700269 /*
270 * If we are on the alternate signal stack and would overflow it, don't.
271 * Return an always-bogus address instead so we will die with SIGSEGV.
272 */
273 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
274 return (void __user *) -1L;
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 /* This is the X/Open sanctioned signal stack switching. */
Al Viro08f739572012-11-07 23:48:13 -0500277 sp = sigsp(sp, ksig) - framesize;
David S. Millerf036d9f2010-02-09 16:18:40 -0800278
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700279 /* Always align the stack frame. This handles two cases. First,
280 * sigaltstack need not be mindful of platform specific stack
281 * alignment. Second, if we took this signal because the stack
282 * is not aligned properly, we'd like to take the signal cleanly
283 * and report that.
284 */
David S. Millerf036d9f2010-02-09 16:18:40 -0800285 sp &= ~15UL;
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700286
David S. Millerf036d9f2010-02-09 16:18:40 -0800287 return (void __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
David S. Miller05c5e762010-09-20 23:24:52 -0700290/* The I-cache flush instruction only works in the primary ASI, which
291 * right now is the nucleus, aka. kernel space.
292 *
293 * Therefore we have to kick the instructions out using the kernel
294 * side linear mapping of the physical address backing the user
295 * instructions.
296 */
297static void flush_signal_insns(unsigned long address)
298{
299 unsigned long pstate, paddr;
300 pte_t *ptep, pte;
301 pgd_t *pgdp;
302 pud_t *pudp;
303 pmd_t *pmdp;
304
305 /* Commit all stores of the instructions we are about to flush. */
306 wmb();
307
308 /* Disable cross-call reception. In this way even a very wide
309 * munmap() on another cpu can't tear down the page table
310 * hierarchy from underneath us, since that can't complete
311 * until the IPI tlb flush returns.
312 */
313
314 __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
315 __asm__ __volatile__("wrpr %0, %1, %%pstate"
316 : : "r" (pstate), "i" (PSTATE_IE));
317
318 pgdp = pgd_offset(current->mm, address);
319 if (pgd_none(*pgdp))
320 goto out_irqs_on;
321 pudp = pud_offset(pgdp, address);
322 if (pud_none(*pudp))
323 goto out_irqs_on;
324 pmdp = pmd_offset(pudp, address);
325 if (pmd_none(*pmdp))
326 goto out_irqs_on;
327
328 ptep = pte_offset_map(pmdp, address);
329 pte = *ptep;
330 if (!pte_present(pte))
331 goto out_unmap;
332
333 paddr = (unsigned long) page_address(pte_page(pte));
334
335 __asm__ __volatile__("flush %0 + %1"
336 : /* no outputs */
337 : "r" (paddr),
338 "r" (address & (PAGE_SIZE - 1))
339 : "memory");
340
341out_unmap:
342 pte_unmap(ptep);
343out_irqs_on:
344 __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
345
346}
347
Al Viro08f739572012-11-07 23:48:13 -0500348static int setup_frame32(struct ksignal *ksig, struct pt_regs *regs,
349 sigset_t *oldset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
David S. Miller5526b7e2008-04-27 02:26:36 -0700351 struct signal_frame32 __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700352 int i, err, wsaved;
353 void __user *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 int sigframe_size;
355 u32 psr;
Sam Ravnborgc19ac322014-05-16 23:26:01 +0200356 compat_sigset_t seta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 /* 1. Make sure everything is clean */
359 synchronize_user_stack();
360 save_and_clear_fpu();
361
David S. Miller55984732011-08-20 17:14:54 -0700362 wsaved = get_thread_wsaved();
363
364 sigframe_size = sizeof(*sf);
365 if (current_thread_info()->fpsaved[0] & FPRS_FEF)
366 sigframe_size += sizeof(__siginfo_fpu_t);
367 if (wsaved)
368 sigframe_size += sizeof(__siginfo_rwin_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
David S. Miller5526b7e2008-04-27 02:26:36 -0700370 sf = (struct signal_frame32 __user *)
Al Viro08f739572012-11-07 23:48:13 -0500371 get_sigframe(ksig, regs, sigframe_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Al Viro08f739572012-11-07 23:48:13 -0500373 if (invalid_frame_pointer(sf, sigframe_size)) {
David Miller5b4fc382018-10-25 20:36:46 -0700374 if (show_unhandled_signals)
375 pr_info("%s[%d] bad frame in setup_frame32: %08lx TPC %08lx O7 %08lx\n",
376 current->comm, current->pid, (unsigned long)sf,
377 regs->tpc, regs->u_regs[UREG_I7]);
378 force_sigsegv(ksig->sig, current);
Al Viro08f739572012-11-07 23:48:13 -0500379 return -EINVAL;
380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
David S. Miller55984732011-08-20 17:14:54 -0700382 tail = (sf + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 /* 2. Save the current process state */
385 if (test_thread_flag(TIF_32BIT)) {
386 regs->tpc &= 0xffffffff;
387 regs->tnpc &= 0xffffffff;
388 }
389 err = put_user(regs->tpc, &sf->info.si_regs.pc);
390 err |= __put_user(regs->tnpc, &sf->info.si_regs.npc);
391 err |= __put_user(regs->y, &sf->info.si_regs.y);
392 psr = tstate_to_psr(regs->tstate);
393 if (current_thread_info()->fpsaved[0] & FPRS_FEF)
394 psr |= PSR_EF;
395 err |= __put_user(psr, &sf->info.si_regs.psr);
396 for (i = 0; i < 16; i++)
397 err |= __put_user(regs->u_regs[i], &sf->info.si_regs.u_regs[i]);
398 err |= __put_user(sizeof(siginfo_extra_v8plus_t), &sf->extra_size);
399 err |= __put_user(SIGINFO_EXTRA_V8PLUS_MAGIC, &sf->v8plus.g_upper[0]);
400 for (i = 1; i < 16; i++)
401 err |= __put_user(((u32 *)regs->u_regs)[2*i],
402 &sf->v8plus.g_upper[i]);
403 err |= __put_user((regs->tstate & TSTATE_ASI) >> 24UL,
404 &sf->v8plus.asi);
405
406 if (psr & PSR_EF) {
David S. Miller55984732011-08-20 17:14:54 -0700407 __siginfo_fpu_t __user *fp = tail;
408 tail += sizeof(*fp);
409 err |= save_fpu_state(regs, fp);
410 err |= __put_user((u64)fp, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 } else {
412 err |= __put_user(0, &sf->fpu_save);
413 }
David S. Miller55984732011-08-20 17:14:54 -0700414 if (wsaved) {
415 __siginfo_rwin_t __user *rwp = tail;
416 tail += sizeof(*rwp);
417 err |= save_rwin_state(wsaved, rwp);
418 err |= __put_user((u64)rwp, &sf->rwin_save);
419 set_thread_wsaved(0);
420 } else {
421 err |= __put_user(0, &sf->rwin_save);
422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Sam Ravnborgc19ac322014-05-16 23:26:01 +0200424 /* If these change we need to know - assignments to seta relies on these sizes */
425 BUILD_BUG_ON(_NSIG_WORDS != 1);
426 BUILD_BUG_ON(_COMPAT_NSIG_WORDS != 2);
427 seta.sig[1] = (oldset->sig[0] >> 32);
428 seta.sig[0] = oldset->sig[0];
429
430 err |= __put_user(seta.sig[0], &sf->info.si_mask);
431 err |= __copy_to_user(sf->extramask, &seta.sig[1],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 (_COMPAT_NSIG_WORDS - 1) * sizeof(unsigned int));
433
David S. Miller55984732011-08-20 17:14:54 -0700434 if (!wsaved) {
435 err |= copy_in_user((u32 __user *)sf,
436 (u32 __user *)(regs->u_regs[UREG_FP]),
437 sizeof(struct reg_window32));
438 } else {
439 struct reg_window *rp;
440
441 rp = &current_thread_info()->reg_window[wsaved - 1];
442 for (i = 0; i < 8; i++)
443 err |= __put_user(rp->locals[i], &sf->ss.locals[i]);
444 for (i = 0; i < 6; i++)
445 err |= __put_user(rp->ins[i], &sf->ss.ins[i]);
446 err |= __put_user(rp->ins[6], &sf->ss.fp);
447 err |= __put_user(rp->ins[7], &sf->ss.callers_pc);
448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (err)
Al Viro08f739572012-11-07 23:48:13 -0500450 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 /* 3. signal handler back-trampoline and parameters */
453 regs->u_regs[UREG_FP] = (unsigned long) sf;
Al Viro08f739572012-11-07 23:48:13 -0500454 regs->u_regs[UREG_I0] = ksig->sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
456 regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
457
458 /* 4. signal handler */
Al Viro08f739572012-11-07 23:48:13 -0500459 regs->tpc = (unsigned long) ksig->ka.sa.sa_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 regs->tnpc = (regs->tpc + 4);
461 if (test_thread_flag(TIF_32BIT)) {
462 regs->tpc &= 0xffffffff;
463 regs->tnpc &= 0xffffffff;
464 }
465
466 /* 5. return to kernel instructions */
Al Viro08f739572012-11-07 23:48:13 -0500467 if (ksig->ka.ka_restorer) {
468 regs->u_regs[UREG_I7] = (unsigned long)ksig->ka.ka_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 unsigned long address = ((unsigned long)&(sf->insns[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 regs->u_regs[UREG_I7] = (unsigned long) (&(sf->insns[0]) - 2);
473
474 err = __put_user(0x821020d8, &sf->insns[0]); /*mov __NR_sigreturn, %g1*/
475 err |= __put_user(0x91d02010, &sf->insns[1]); /*t 0x10*/
476 if (err)
Al Viro08f739572012-11-07 23:48:13 -0500477 return err;
David S. Miller05c5e762010-09-20 23:24:52 -0700478 flush_signal_insns(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
David S. Millerc2785252010-09-21 22:30:13 -0700480 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
Al Viro08f739572012-11-07 23:48:13 -0500483static int setup_rt_frame32(struct ksignal *ksig, struct pt_regs *regs,
484 sigset_t *oldset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 struct rt_signal_frame32 __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700487 int i, err, wsaved;
488 void __user *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 int sigframe_size;
490 u32 psr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 /* 1. Make sure everything is clean */
493 synchronize_user_stack();
494 save_and_clear_fpu();
495
David S. Miller55984732011-08-20 17:14:54 -0700496 wsaved = get_thread_wsaved();
497
498 sigframe_size = sizeof(*sf);
499 if (current_thread_info()->fpsaved[0] & FPRS_FEF)
500 sigframe_size += sizeof(__siginfo_fpu_t);
501 if (wsaved)
502 sigframe_size += sizeof(__siginfo_rwin_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 sf = (struct rt_signal_frame32 __user *)
Al Viro08f739572012-11-07 23:48:13 -0500505 get_sigframe(ksig, regs, sigframe_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Al Viro08f739572012-11-07 23:48:13 -0500507 if (invalid_frame_pointer(sf, sigframe_size)) {
David Miller5b4fc382018-10-25 20:36:46 -0700508 if (show_unhandled_signals)
509 pr_info("%s[%d] bad frame in setup_rt_frame32: %08lx TPC %08lx O7 %08lx\n",
510 current->comm, current->pid, (unsigned long)sf,
511 regs->tpc, regs->u_regs[UREG_I7]);
512 force_sigsegv(ksig->sig, current);
Al Viro08f739572012-11-07 23:48:13 -0500513 return -EINVAL;
514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
David S. Miller55984732011-08-20 17:14:54 -0700516 tail = (sf + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 /* 2. Save the current process state */
519 if (test_thread_flag(TIF_32BIT)) {
520 regs->tpc &= 0xffffffff;
521 regs->tnpc &= 0xffffffff;
522 }
523 err = put_user(regs->tpc, &sf->regs.pc);
524 err |= __put_user(regs->tnpc, &sf->regs.npc);
525 err |= __put_user(regs->y, &sf->regs.y);
526 psr = tstate_to_psr(regs->tstate);
527 if (current_thread_info()->fpsaved[0] & FPRS_FEF)
528 psr |= PSR_EF;
529 err |= __put_user(psr, &sf->regs.psr);
530 for (i = 0; i < 16; i++)
531 err |= __put_user(regs->u_regs[i], &sf->regs.u_regs[i]);
532 err |= __put_user(sizeof(siginfo_extra_v8plus_t), &sf->extra_size);
533 err |= __put_user(SIGINFO_EXTRA_V8PLUS_MAGIC, &sf->v8plus.g_upper[0]);
534 for (i = 1; i < 16; i++)
535 err |= __put_user(((u32 *)regs->u_regs)[2*i],
536 &sf->v8plus.g_upper[i]);
537 err |= __put_user((regs->tstate & TSTATE_ASI) >> 24UL,
538 &sf->v8plus.asi);
539
540 if (psr & PSR_EF) {
David S. Miller55984732011-08-20 17:14:54 -0700541 __siginfo_fpu_t __user *fp = tail;
542 tail += sizeof(*fp);
543 err |= save_fpu_state(regs, fp);
544 err |= __put_user((u64)fp, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 } else {
546 err |= __put_user(0, &sf->fpu_save);
547 }
David S. Miller55984732011-08-20 17:14:54 -0700548 if (wsaved) {
549 __siginfo_rwin_t __user *rwp = tail;
550 tail += sizeof(*rwp);
551 err |= save_rwin_state(wsaved, rwp);
552 err |= __put_user((u64)rwp, &sf->rwin_save);
553 set_thread_wsaved(0);
554 } else {
555 err |= __put_user(0, &sf->rwin_save);
556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 /* Update the siginfo structure. */
Al Viro08f739572012-11-07 23:48:13 -0500559 err |= copy_siginfo_to_user32(&sf->info, &ksig->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 /* Setup sigaltstack */
Al Viro99b06fe2012-12-23 03:41:17 -0500562 err |= __compat_save_altstack(&sf->stack, regs->u_regs[UREG_FP]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Al Viro68c38fb2017-09-04 12:28:06 -0400564 err |= put_compat_sigset(&sf->mask, oldset, sizeof(compat_sigset_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
David S. Miller55984732011-08-20 17:14:54 -0700566 if (!wsaved) {
567 err |= copy_in_user((u32 __user *)sf,
568 (u32 __user *)(regs->u_regs[UREG_FP]),
569 sizeof(struct reg_window32));
570 } else {
571 struct reg_window *rp;
572
573 rp = &current_thread_info()->reg_window[wsaved - 1];
574 for (i = 0; i < 8; i++)
575 err |= __put_user(rp->locals[i], &sf->ss.locals[i]);
576 for (i = 0; i < 6; i++)
577 err |= __put_user(rp->ins[i], &sf->ss.ins[i]);
578 err |= __put_user(rp->ins[6], &sf->ss.fp);
579 err |= __put_user(rp->ins[7], &sf->ss.callers_pc);
580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (err)
Al Viro08f739572012-11-07 23:48:13 -0500582 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 /* 3. signal handler back-trampoline and parameters */
585 regs->u_regs[UREG_FP] = (unsigned long) sf;
Al Viro08f739572012-11-07 23:48:13 -0500586 regs->u_regs[UREG_I0] = ksig->sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
588 regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
589
590 /* 4. signal handler */
Al Viro08f739572012-11-07 23:48:13 -0500591 regs->tpc = (unsigned long) ksig->ka.sa.sa_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 regs->tnpc = (regs->tpc + 4);
593 if (test_thread_flag(TIF_32BIT)) {
594 regs->tpc &= 0xffffffff;
595 regs->tnpc &= 0xffffffff;
596 }
597
598 /* 5. return to kernel instructions */
Al Viro08f739572012-11-07 23:48:13 -0500599 if (ksig->ka.ka_restorer)
600 regs->u_regs[UREG_I7] = (unsigned long)ksig->ka.ka_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 unsigned long address = ((unsigned long)&(sf->insns[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 regs->u_regs[UREG_I7] = (unsigned long) (&(sf->insns[0]) - 2);
605
606 /* mov __NR_rt_sigreturn, %g1 */
607 err |= __put_user(0x82102065, &sf->insns[0]);
608
609 /* t 0x10 */
610 err |= __put_user(0x91d02010, &sf->insns[1]);
611 if (err)
Al Viro08f739572012-11-07 23:48:13 -0500612 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
David S. Miller05c5e762010-09-20 23:24:52 -0700614 flush_signal_insns(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
David S. Miller392c2182010-09-21 21:41:12 -0700616 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
Al Viro08f739572012-11-07 23:48:13 -0500619static inline void handle_signal32(struct ksignal *ksig,
620 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Al Viro08f739572012-11-07 23:48:13 -0500622 sigset_t *oldset = sigmask_to_save();
David S. Miller392c2182010-09-21 21:41:12 -0700623 int err;
624
Al Viro08f739572012-11-07 23:48:13 -0500625 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
626 err = setup_rt_frame32(ksig, regs, oldset);
David S. Millerec98c6b2008-04-20 02:14:23 -0700627 else
Al Viro08f739572012-11-07 23:48:13 -0500628 err = setup_frame32(ksig, regs, oldset);
David S. Miller392c2182010-09-21 21:41:12 -0700629
Al Viro08f739572012-11-07 23:48:13 -0500630 signal_setup_done(err, ksig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
633static inline void syscall_restart32(unsigned long orig_i0, struct pt_regs *regs,
634 struct sigaction *sa)
635{
636 switch (regs->u_regs[UREG_I0]) {
637 case ERESTART_RESTARTBLOCK:
638 case ERESTARTNOHAND:
639 no_system_call_restart:
640 regs->u_regs[UREG_I0] = EINTR;
641 regs->tstate |= TSTATE_ICARRY;
642 break;
643 case ERESTARTSYS:
644 if (!(sa->sa_flags & SA_RESTART))
645 goto no_system_call_restart;
646 /* fallthrough */
647 case ERESTARTNOINTR:
648 regs->u_regs[UREG_I0] = orig_i0;
649 regs->tpc -= 4;
650 regs->tnpc -= 4;
651 }
652}
653
654/* Note that 'init' is a special process: it doesn't get signals it doesn't
655 * want to handle. Thus you cannot kill init even with a SIGKILL even by
656 * mistake.
657 */
Al Virodfbb83d2013-03-02 02:55:16 -0500658void do_signal32(struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Al Viro08f739572012-11-07 23:48:13 -0500660 struct ksignal ksig;
661 unsigned long orig_i0 = 0;
662 int restart_syscall = 0;
663 bool has_handler = get_signal(&ksig);
David S. Miller28e61032008-05-11 02:07:19 -0700664
David S. Miller1d299bc2011-11-14 20:32:16 -0800665 if (pt_regs_is_syscall(regs) &&
666 (regs->tstate & (TSTATE_XCARRY | TSTATE_ICARRY))) {
667 restart_syscall = 1;
David S. Millere88d2462011-11-15 12:57:00 -0800668 orig_i0 = regs->u_regs[UREG_G6];
David S. Miller1d299bc2011-11-14 20:32:16 -0800669 }
David S. Miller28e61032008-05-11 02:07:19 -0700670
Al Viro08f739572012-11-07 23:48:13 -0500671 if (has_handler) {
David S. Miller28e61032008-05-11 02:07:19 -0700672 if (restart_syscall)
Al Viro08f739572012-11-07 23:48:13 -0500673 syscall_restart32(orig_i0, regs, &ksig.ka.sa);
674 handle_signal32(&ksig, regs);
675 } else {
676 if (restart_syscall) {
677 switch (regs->u_regs[UREG_I0]) {
678 case ERESTARTNOHAND:
679 case ERESTARTSYS:
680 case ERESTARTNOINTR:
681 /* replay the system call when we are done */
682 regs->u_regs[UREG_I0] = orig_i0;
683 regs->tpc -= 4;
684 regs->tnpc -= 4;
685 pt_regs_clear_syscall(regs);
686 case ERESTART_RESTARTBLOCK:
687 regs->u_regs[UREG_G1] = __NR_restart_syscall;
688 regs->tpc -= 4;
689 regs->tnpc -= 4;
690 pt_regs_clear_syscall(regs);
691 }
692 }
693 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
697struct sigstack32 {
698 u32 the_stack;
699 int cur_status;
700};
701
702asmlinkage int do_sys32_sigstack(u32 u_ssptr, u32 u_ossptr, unsigned long sp)
703{
704 struct sigstack32 __user *ssptr =
705 (struct sigstack32 __user *)((unsigned long)(u_ssptr));
706 struct sigstack32 __user *ossptr =
707 (struct sigstack32 __user *)((unsigned long)(u_ossptr));
708 int ret = -EFAULT;
709
710 /* First see if old state is wanted. */
711 if (ossptr) {
712 if (put_user(current->sas_ss_sp + current->sas_ss_size,
713 &ossptr->the_stack) ||
714 __put_user(on_sig_stack(sp), &ossptr->cur_status))
715 goto out;
716 }
717
718 /* Now see if we want to update the new state. */
719 if (ssptr) {
720 u32 ss_sp;
721
722 if (get_user(ss_sp, &ssptr->the_stack))
723 goto out;
724
725 /* If the current stack was set with sigaltstack, don't
726 * swap stacks while we are on it.
727 */
728 ret = -EPERM;
729 if (current->sas_ss_sp && on_sig_stack(sp))
730 goto out;
731
732 /* Since we don't know the extent of the stack, and we don't
733 * track onstack-ness, but rather calculate it, we must
734 * presume a size. Ho hum this interface is lossy.
735 */
736 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
737 current->sas_ss_size = SIGSTKSZ;
738 }
739
740 ret = 0;
741out:
742 return ret;
743}