blob: 1ba95aff5d5958cdeb1142a24f18ea0c63717722 [file] [log] [blame]
David S. Miller5526b7e2008-04-27 02:26:36 -07001/* arch/sparc64/kernel/signal32.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1996 Miguel de Icaza (miguel@nuclecu.unam.mx)
6 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
7 * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
8 */
9
10#include <linux/sched.h>
11#include <linux/kernel.h>
12#include <linux/signal.h>
13#include <linux/errno.h>
14#include <linux/wait.h>
15#include <linux/ptrace.h>
16#include <linux/unistd.h>
17#include <linux/mm.h>
18#include <linux/tty.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/binfmts.h>
20#include <linux/compat.h>
21#include <linux/bitops.h>
Roland McGrath95698462008-07-27 01:08:02 -070022#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <asm/uaccess.h>
25#include <asm/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/pgtable.h>
27#include <asm/psrcompat.h>
28#include <asm/fpumacro.h>
29#include <asm/visasm.h>
David S. Miller14cc6ab2006-10-02 14:17:57 -070030#include <asm/compat_signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
David S. Miller55984732011-08-20 17:14:54 -070032#include "sigutil.h"
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/* This magic should be in g_upper[0] for all upper parts
37 * to be valid.
38 */
39#define SIGINFO_EXTRA_V8PLUS_MAGIC 0x130e269
40typedef struct {
41 unsigned int g_upper[8];
42 unsigned int o_upper[8];
43 unsigned int asi;
44} siginfo_extra_v8plus_t;
45
David S. Miller5526b7e2008-04-27 02:26:36 -070046struct signal_frame32 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 struct sparc_stackf32 ss;
48 __siginfo32_t info;
David S. Miller55984732011-08-20 17:14:54 -070049 /* __siginfo_fpu_t * */ u32 fpu_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 unsigned int insns[2];
51 unsigned int extramask[_COMPAT_NSIG_WORDS - 1];
52 unsigned int extra_size; /* Should be sizeof(siginfo_extra_v8plus_t) */
53 /* Only valid if (info.si_regs.psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS */
54 siginfo_extra_v8plus_t v8plus;
David S. Miller55984732011-08-20 17:14:54 -070055 /* __siginfo_rwin_t * */u32 rwin_save;
56} __attribute__((aligned(8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58typedef struct compat_siginfo{
59 int si_signo;
60 int si_errno;
61 int si_code;
62
63 union {
64 int _pad[SI_PAD_SIZE32];
65
66 /* kill() */
67 struct {
68 compat_pid_t _pid; /* sender's pid */
69 unsigned int _uid; /* sender's uid */
70 } _kill;
71
72 /* POSIX.1b timers */
73 struct {
Stephen Rothwell0d77e5a2005-06-23 00:10:14 -070074 compat_timer_t _tid; /* timer id */
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 int _overrun; /* overrun count */
76 compat_sigval_t _sigval; /* same as below */
77 int _sys_private; /* not to be passed to user */
78 } _timer;
79
80 /* POSIX.1b signals */
81 struct {
82 compat_pid_t _pid; /* sender's pid */
83 unsigned int _uid; /* sender's uid */
84 compat_sigval_t _sigval;
85 } _rt;
86
87 /* SIGCHLD */
88 struct {
89 compat_pid_t _pid; /* which child */
90 unsigned int _uid; /* sender's uid */
91 int _status; /* exit code */
92 compat_clock_t _utime;
93 compat_clock_t _stime;
94 } _sigchld;
95
96 /* SIGILL, SIGFPE, SIGSEGV, SIGBUS, SIGEMT */
97 struct {
98 u32 _addr; /* faulting insn/memory ref. */
99 int _trapno;
100 } _sigfault;
101
102 /* SIGPOLL */
103 struct {
104 int _band; /* POLL_IN, POLL_OUT, POLL_MSG */
105 int _fd;
106 } _sigpoll;
107 } _sifields;
108}compat_siginfo_t;
109
110struct rt_signal_frame32 {
111 struct sparc_stackf32 ss;
112 compat_siginfo_t info;
113 struct pt_regs32 regs;
114 compat_sigset_t mask;
David S. Miller55984732011-08-20 17:14:54 -0700115 /* __siginfo_fpu_t * */ u32 fpu_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 unsigned int insns[2];
117 stack_t32 stack;
118 unsigned int extra_size; /* Should be sizeof(siginfo_extra_v8plus_t) */
119 /* Only valid if (regs.psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS */
120 siginfo_extra_v8plus_t v8plus;
David S. Miller55984732011-08-20 17:14:54 -0700121 /* __siginfo_rwin_t * */u32 rwin_save;
122} __attribute__((aligned(8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124int copy_siginfo_to_user32(compat_siginfo_t __user *to, siginfo_t *from)
125{
126 int err;
127
128 if (!access_ok(VERIFY_WRITE, to, sizeof(compat_siginfo_t)))
129 return -EFAULT;
130
131 /* If you change siginfo_t structure, please be sure
132 this code is fixed accordingly.
133 It should never copy any pad contained in the structure
134 to avoid security leaks, but must copy the generic
135 3 ints plus the relevant union member.
136 This routine must convert siginfo from 64bit to 32bit as well
137 at the same time. */
138 err = __put_user(from->si_signo, &to->si_signo);
139 err |= __put_user(from->si_errno, &to->si_errno);
140 err |= __put_user((short)from->si_code, &to->si_code);
141 if (from->si_code < 0)
142 err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
143 else {
144 switch (from->si_code >> 16) {
145 case __SI_TIMER >> 16:
146 err |= __put_user(from->si_tid, &to->si_tid);
147 err |= __put_user(from->si_overrun, &to->si_overrun);
148 err |= __put_user(from->si_int, &to->si_int);
149 break;
150 case __SI_CHLD >> 16:
151 err |= __put_user(from->si_utime, &to->si_utime);
152 err |= __put_user(from->si_stime, &to->si_stime);
153 err |= __put_user(from->si_status, &to->si_status);
154 default:
155 err |= __put_user(from->si_pid, &to->si_pid);
156 err |= __put_user(from->si_uid, &to->si_uid);
157 break;
158 case __SI_FAULT >> 16:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 err |= __put_user(from->si_trapno, &to->si_trapno);
160 err |= __put_user((unsigned long)from->si_addr, &to->si_addr);
161 break;
Jurij Smakov9c7d3b32005-04-17 18:03:12 -0700162 case __SI_POLL >> 16:
163 err |= __put_user(from->si_band, &to->si_band);
164 err |= __put_user(from->si_fd, &to->si_fd);
165 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 case __SI_RT >> 16: /* This is not generated by the kernel as of now. */
167 case __SI_MESGQ >> 16:
168 err |= __put_user(from->si_pid, &to->si_pid);
169 err |= __put_user(from->si_uid, &to->si_uid);
170 err |= __put_user(from->si_int, &to->si_int);
171 break;
172 }
173 }
174 return err;
175}
176
177/* CAUTION: This is just a very minimalist implementation for the
178 * sake of compat_sys_rt_sigqueueinfo()
179 */
180int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from)
181{
182 if (!access_ok(VERIFY_WRITE, from, sizeof(compat_siginfo_t)))
183 return -EFAULT;
184
185 if (copy_from_user(to, from, 3*sizeof(int)) ||
186 copy_from_user(to->_sifields._pad, from->_sifields._pad,
187 SI_PAD_SIZE))
188 return -EFAULT;
189
190 return 0;
191}
192
David S. Miller5526b7e2008-04-27 02:26:36 -0700193void do_sigreturn32(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
David S. Miller5526b7e2008-04-27 02:26:36 -0700195 struct signal_frame32 __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700196 compat_uptr_t fpu_save;
197 compat_uptr_t rwin_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 unsigned int psr;
David S. Miller55984732011-08-20 17:14:54 -0700199 unsigned pc, npc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 sigset_t set;
201 unsigned seta[_COMPAT_NSIG_WORDS];
202 int err, i;
203
David S. Miller5526b7e2008-04-27 02:26:36 -0700204 /* Always make any pending restarted system calls return -EINTR */
205 current_thread_info()->restart_block.fn = do_no_restart_syscall;
206
207 synchronize_user_stack();
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
David S. Miller5526b7e2008-04-27 02:26:36 -0700210 sf = (struct signal_frame32 __user *) regs->u_regs[UREG_FP];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 /* 1. Make sure we are not getting garbage from the user */
213 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
214 (((unsigned long) sf) & 3))
215 goto segv;
216
217 get_user(pc, &sf->info.si_regs.pc);
218 __get_user(npc, &sf->info.si_regs.npc);
219
220 if ((pc | npc) & 3)
221 goto segv;
222
223 if (test_thread_flag(TIF_32BIT)) {
224 pc &= 0xffffffff;
225 npc &= 0xffffffff;
226 }
227 regs->tpc = pc;
228 regs->tnpc = npc;
229
230 /* 2. Restore the state */
231 err = __get_user(regs->y, &sf->info.si_regs.y);
232 err |= __get_user(psr, &sf->info.si_regs.psr);
233
234 for (i = UREG_G1; i <= UREG_I7; i++)
235 err |= __get_user(regs->u_regs[i], &sf->info.si_regs.u_regs[i]);
236 if ((psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS) {
237 err |= __get_user(i, &sf->v8plus.g_upper[0]);
238 if (i == SIGINFO_EXTRA_V8PLUS_MAGIC) {
239 unsigned long asi;
240
241 for (i = UREG_G1; i <= UREG_I7; i++)
242 err |= __get_user(((u32 *)regs->u_regs)[2*i], &sf->v8plus.g_upper[i]);
243 err |= __get_user(asi, &sf->v8plus.asi);
244 regs->tstate &= ~TSTATE_ASI;
245 regs->tstate |= ((asi & 0xffUL) << 24UL);
246 }
247 }
248
249 /* User can only change condition codes in %tstate. */
250 regs->tstate &= ~(TSTATE_ICC|TSTATE_XCC);
251 regs->tstate |= psr_to_tstate_icc(psr);
252
David S. Miller2678fef2008-05-01 03:30:22 -0700253 /* Prevent syscall restart. */
David S. Miller28e61032008-05-11 02:07:19 -0700254 pt_regs_clear_syscall(regs);
David S. Miller2678fef2008-05-01 03:30:22 -0700255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 err |= __get_user(fpu_save, &sf->fpu_save);
David S. Miller55984732011-08-20 17:14:54 -0700257 if (!err && fpu_save)
258 err |= restore_fpu_state(regs, compat_ptr(fpu_save));
259 err |= __get_user(rwin_save, &sf->rwin_save);
260 if (!err && rwin_save) {
261 if (restore_rwin_state(compat_ptr(rwin_save)))
262 goto segv;
263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 err |= __get_user(seta[0], &sf->info.si_mask);
265 err |= copy_from_user(seta+1, &sf->extramask,
266 (_COMPAT_NSIG_WORDS - 1) * sizeof(unsigned int));
267 if (err)
268 goto segv;
269 switch (_NSIG_WORDS) {
270 case 4: set.sig[3] = seta[6] + (((long)seta[7]) << 32);
271 case 3: set.sig[2] = seta[4] + (((long)seta[5]) << 32);
272 case 2: set.sig[1] = seta[2] + (((long)seta[3]) << 32);
273 case 1: set.sig[0] = seta[0] + (((long)seta[1]) << 32);
274 }
275 sigdelsetmask(&set, ~_BLOCKABLE);
276 spin_lock_irq(&current->sighand->siglock);
277 current->blocked = set;
278 recalc_sigpending();
279 spin_unlock_irq(&current->sighand->siglock);
280 return;
281
282segv:
283 force_sig(SIGSEGV, current);
284}
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286asmlinkage void do_rt_sigreturn32(struct pt_regs *regs)
287{
288 struct rt_signal_frame32 __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700289 unsigned int psr, pc, npc, u_ss_sp;
290 compat_uptr_t fpu_save;
291 compat_uptr_t rwin_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 mm_segment_t old_fs;
293 sigset_t set;
294 compat_sigset_t seta;
295 stack_t st;
296 int err, i;
297
298 /* Always make any pending restarted system calls return -EINTR */
299 current_thread_info()->restart_block.fn = do_no_restart_syscall;
300
301 synchronize_user_stack();
302 regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
303 sf = (struct rt_signal_frame32 __user *) regs->u_regs[UREG_FP];
304
305 /* 1. Make sure we are not getting garbage from the user */
306 if (!access_ok(VERIFY_READ, sf, sizeof(*sf)) ||
307 (((unsigned long) sf) & 3))
308 goto segv;
309
310 get_user(pc, &sf->regs.pc);
311 __get_user(npc, &sf->regs.npc);
312
313 if ((pc | npc) & 3)
314 goto segv;
315
316 if (test_thread_flag(TIF_32BIT)) {
317 pc &= 0xffffffff;
318 npc &= 0xffffffff;
319 }
320 regs->tpc = pc;
321 regs->tnpc = npc;
322
323 /* 2. Restore the state */
324 err = __get_user(regs->y, &sf->regs.y);
325 err |= __get_user(psr, &sf->regs.psr);
326
327 for (i = UREG_G1; i <= UREG_I7; i++)
328 err |= __get_user(regs->u_regs[i], &sf->regs.u_regs[i]);
329 if ((psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS) {
330 err |= __get_user(i, &sf->v8plus.g_upper[0]);
331 if (i == SIGINFO_EXTRA_V8PLUS_MAGIC) {
332 unsigned long asi;
333
334 for (i = UREG_G1; i <= UREG_I7; i++)
335 err |= __get_user(((u32 *)regs->u_regs)[2*i], &sf->v8plus.g_upper[i]);
336 err |= __get_user(asi, &sf->v8plus.asi);
337 regs->tstate &= ~TSTATE_ASI;
338 regs->tstate |= ((asi & 0xffUL) << 24UL);
339 }
340 }
341
342 /* User can only change condition codes in %tstate. */
343 regs->tstate &= ~(TSTATE_ICC|TSTATE_XCC);
344 regs->tstate |= psr_to_tstate_icc(psr);
345
David S. Miller2678fef2008-05-01 03:30:22 -0700346 /* Prevent syscall restart. */
David S. Miller28e61032008-05-11 02:07:19 -0700347 pt_regs_clear_syscall(regs);
David S. Miller2678fef2008-05-01 03:30:22 -0700348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 err |= __get_user(fpu_save, &sf->fpu_save);
David S. Miller55984732011-08-20 17:14:54 -0700350 if (!err && fpu_save)
351 err |= restore_fpu_state(regs, compat_ptr(fpu_save));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 err |= copy_from_user(&seta, &sf->mask, sizeof(compat_sigset_t));
353 err |= __get_user(u_ss_sp, &sf->stack.ss_sp);
354 st.ss_sp = compat_ptr(u_ss_sp);
355 err |= __get_user(st.ss_flags, &sf->stack.ss_flags);
356 err |= __get_user(st.ss_size, &sf->stack.ss_size);
357 if (err)
358 goto segv;
359
360 /* It is more difficult to avoid calling this function than to
361 call it and ignore errors. */
362 old_fs = get_fs();
363 set_fs(KERNEL_DS);
364 do_sigaltstack((stack_t __user *) &st, NULL, (unsigned long)sf);
365 set_fs(old_fs);
366
David S. Miller55984732011-08-20 17:14:54 -0700367 err |= __get_user(rwin_save, &sf->rwin_save);
368 if (!err && rwin_save) {
369 if (restore_rwin_state(compat_ptr(rwin_save)))
370 goto segv;
371 }
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 switch (_NSIG_WORDS) {
374 case 4: set.sig[3] = seta.sig[6] + (((long)seta.sig[7]) << 32);
375 case 3: set.sig[2] = seta.sig[4] + (((long)seta.sig[5]) << 32);
376 case 2: set.sig[1] = seta.sig[2] + (((long)seta.sig[3]) << 32);
377 case 1: set.sig[0] = seta.sig[0] + (((long)seta.sig[1]) << 32);
378 }
379 sigdelsetmask(&set, ~_BLOCKABLE);
380 spin_lock_irq(&current->sighand->siglock);
381 current->blocked = set;
382 recalc_sigpending();
383 spin_unlock_irq(&current->sighand->siglock);
384 return;
385segv:
386 force_sig(SIGSEGV, current);
387}
388
389/* Checks if the fp is valid */
390static int invalid_frame_pointer(void __user *fp, int fplen)
391{
392 if ((((unsigned long) fp) & 7) || ((unsigned long)fp) > 0x100000000ULL - fplen)
393 return 1;
394 return 0;
395}
396
397static void __user *get_sigframe(struct sigaction *sa, struct pt_regs *regs, unsigned long framesize)
398{
399 unsigned long sp;
400
401 regs->u_regs[UREG_FP] &= 0x00000000ffffffffUL;
402 sp = regs->u_regs[UREG_FP];
403
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700404 /*
405 * If we are on the alternate signal stack and would overflow it, don't.
406 * Return an always-bogus address instead so we will die with SIGSEGV.
407 */
408 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
409 return (void __user *) -1L;
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /* This is the X/Open sanctioned signal stack switching. */
412 if (sa->sa_flags & SA_ONSTACK) {
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700413 if (sas_ss_flags(sp) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 sp = current->sas_ss_sp + current->sas_ss_size;
415 }
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700416
David S. Millerf036d9f2010-02-09 16:18:40 -0800417 sp -= framesize;
418
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700419 /* Always align the stack frame. This handles two cases. First,
420 * sigaltstack need not be mindful of platform specific stack
421 * alignment. Second, if we took this signal because the stack
422 * is not aligned properly, we'd like to take the signal cleanly
423 * and report that.
424 */
David S. Millerf036d9f2010-02-09 16:18:40 -0800425 sp &= ~15UL;
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700426
David S. Millerf036d9f2010-02-09 16:18:40 -0800427 return (void __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
David S. Miller05c5e762010-09-20 23:24:52 -0700430/* The I-cache flush instruction only works in the primary ASI, which
431 * right now is the nucleus, aka. kernel space.
432 *
433 * Therefore we have to kick the instructions out using the kernel
434 * side linear mapping of the physical address backing the user
435 * instructions.
436 */
437static void flush_signal_insns(unsigned long address)
438{
439 unsigned long pstate, paddr;
440 pte_t *ptep, pte;
441 pgd_t *pgdp;
442 pud_t *pudp;
443 pmd_t *pmdp;
444
445 /* Commit all stores of the instructions we are about to flush. */
446 wmb();
447
448 /* Disable cross-call reception. In this way even a very wide
449 * munmap() on another cpu can't tear down the page table
450 * hierarchy from underneath us, since that can't complete
451 * until the IPI tlb flush returns.
452 */
453
454 __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
455 __asm__ __volatile__("wrpr %0, %1, %%pstate"
456 : : "r" (pstate), "i" (PSTATE_IE));
457
458 pgdp = pgd_offset(current->mm, address);
459 if (pgd_none(*pgdp))
460 goto out_irqs_on;
461 pudp = pud_offset(pgdp, address);
462 if (pud_none(*pudp))
463 goto out_irqs_on;
464 pmdp = pmd_offset(pudp, address);
465 if (pmd_none(*pmdp))
466 goto out_irqs_on;
467
468 ptep = pte_offset_map(pmdp, address);
469 pte = *ptep;
470 if (!pte_present(pte))
471 goto out_unmap;
472
473 paddr = (unsigned long) page_address(pte_page(pte));
474
475 __asm__ __volatile__("flush %0 + %1"
476 : /* no outputs */
477 : "r" (paddr),
478 "r" (address & (PAGE_SIZE - 1))
479 : "memory");
480
481out_unmap:
482 pte_unmap(ptep);
483out_irqs_on:
484 __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
485
486}
487
David S. Miller392c2182010-09-21 21:41:12 -0700488static int setup_frame32(struct k_sigaction *ka, struct pt_regs *regs,
489 int signo, sigset_t *oldset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
David S. Miller5526b7e2008-04-27 02:26:36 -0700491 struct signal_frame32 __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700492 int i, err, wsaved;
493 void __user *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 int sigframe_size;
495 u32 psr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 unsigned int seta[_COMPAT_NSIG_WORDS];
497
498 /* 1. Make sure everything is clean */
499 synchronize_user_stack();
500 save_and_clear_fpu();
501
David S. Miller55984732011-08-20 17:14:54 -0700502 wsaved = get_thread_wsaved();
503
504 sigframe_size = sizeof(*sf);
505 if (current_thread_info()->fpsaved[0] & FPRS_FEF)
506 sigframe_size += sizeof(__siginfo_fpu_t);
507 if (wsaved)
508 sigframe_size += sizeof(__siginfo_rwin_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
David S. Miller5526b7e2008-04-27 02:26:36 -0700510 sf = (struct signal_frame32 __user *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 get_sigframe(&ka->sa, regs, sigframe_size);
512
513 if (invalid_frame_pointer(sf, sigframe_size))
514 goto sigill;
515
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->info.si_regs.pc);
524 err |= __put_user(regs->tnpc, &sf->info.si_regs.npc);
525 err |= __put_user(regs->y, &sf->info.si_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->info.si_regs.psr);
530 for (i = 0; i < 16; i++)
531 err |= __put_user(regs->u_regs[i], &sf->info.si_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 switch (_NSIG_WORDS) {
559 case 4: seta[7] = (oldset->sig[3] >> 32);
560 seta[6] = oldset->sig[3];
561 case 3: seta[5] = (oldset->sig[2] >> 32);
562 seta[4] = oldset->sig[2];
563 case 2: seta[3] = (oldset->sig[1] >> 32);
564 seta[2] = oldset->sig[1];
565 case 1: seta[1] = (oldset->sig[0] >> 32);
566 seta[0] = oldset->sig[0];
567 }
568 err |= __put_user(seta[0], &sf->info.si_mask);
569 err |= __copy_to_user(sf->extramask, seta + 1,
570 (_COMPAT_NSIG_WORDS - 1) * sizeof(unsigned int));
571
David S. Miller55984732011-08-20 17:14:54 -0700572 if (!wsaved) {
573 err |= copy_in_user((u32 __user *)sf,
574 (u32 __user *)(regs->u_regs[UREG_FP]),
575 sizeof(struct reg_window32));
576 } else {
577 struct reg_window *rp;
578
579 rp = &current_thread_info()->reg_window[wsaved - 1];
580 for (i = 0; i < 8; i++)
581 err |= __put_user(rp->locals[i], &sf->ss.locals[i]);
582 for (i = 0; i < 6; i++)
583 err |= __put_user(rp->ins[i], &sf->ss.ins[i]);
584 err |= __put_user(rp->ins[6], &sf->ss.fp);
585 err |= __put_user(rp->ins[7], &sf->ss.callers_pc);
586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (err)
588 goto sigsegv;
589
590 /* 3. signal handler back-trampoline and parameters */
591 regs->u_regs[UREG_FP] = (unsigned long) sf;
592 regs->u_regs[UREG_I0] = signo;
593 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
594 regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
595
596 /* 4. signal handler */
597 regs->tpc = (unsigned long) ka->sa.sa_handler;
598 regs->tnpc = (regs->tpc + 4);
599 if (test_thread_flag(TIF_32BIT)) {
600 regs->tpc &= 0xffffffff;
601 regs->tnpc &= 0xffffffff;
602 }
603
604 /* 5. return to kernel instructions */
605 if (ka->ka_restorer) {
606 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
607 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 unsigned long address = ((unsigned long)&(sf->insns[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 regs->u_regs[UREG_I7] = (unsigned long) (&(sf->insns[0]) - 2);
611
612 err = __put_user(0x821020d8, &sf->insns[0]); /*mov __NR_sigreturn, %g1*/
613 err |= __put_user(0x91d02010, &sf->insns[1]); /*t 0x10*/
614 if (err)
615 goto sigsegv;
David S. Miller05c5e762010-09-20 23:24:52 -0700616 flush_signal_insns(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
David S. Millerc2785252010-09-21 22:30:13 -0700618 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620sigill:
621 do_exit(SIGILL);
David S. Miller392c2182010-09-21 21:41:12 -0700622 return -EINVAL;
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624sigsegv:
625 force_sigsegv(signo, current);
David S. Miller392c2182010-09-21 21:41:12 -0700626 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
David S. Miller392c2182010-09-21 21:41:12 -0700629static int setup_rt_frame32(struct k_sigaction *ka, struct pt_regs *regs,
630 unsigned long signr, sigset_t *oldset,
631 siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 struct rt_signal_frame32 __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700634 int i, err, wsaved;
635 void __user *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 int sigframe_size;
637 u32 psr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 compat_sigset_t seta;
639
640 /* 1. Make sure everything is clean */
641 synchronize_user_stack();
642 save_and_clear_fpu();
643
David S. Miller55984732011-08-20 17:14:54 -0700644 wsaved = get_thread_wsaved();
645
646 sigframe_size = sizeof(*sf);
647 if (current_thread_info()->fpsaved[0] & FPRS_FEF)
648 sigframe_size += sizeof(__siginfo_fpu_t);
649 if (wsaved)
650 sigframe_size += sizeof(__siginfo_rwin_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 sf = (struct rt_signal_frame32 __user *)
653 get_sigframe(&ka->sa, regs, sigframe_size);
654
655 if (invalid_frame_pointer(sf, sigframe_size))
656 goto sigill;
657
David S. Miller55984732011-08-20 17:14:54 -0700658 tail = (sf + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 /* 2. Save the current process state */
661 if (test_thread_flag(TIF_32BIT)) {
662 regs->tpc &= 0xffffffff;
663 regs->tnpc &= 0xffffffff;
664 }
665 err = put_user(regs->tpc, &sf->regs.pc);
666 err |= __put_user(regs->tnpc, &sf->regs.npc);
667 err |= __put_user(regs->y, &sf->regs.y);
668 psr = tstate_to_psr(regs->tstate);
669 if (current_thread_info()->fpsaved[0] & FPRS_FEF)
670 psr |= PSR_EF;
671 err |= __put_user(psr, &sf->regs.psr);
672 for (i = 0; i < 16; i++)
673 err |= __put_user(regs->u_regs[i], &sf->regs.u_regs[i]);
674 err |= __put_user(sizeof(siginfo_extra_v8plus_t), &sf->extra_size);
675 err |= __put_user(SIGINFO_EXTRA_V8PLUS_MAGIC, &sf->v8plus.g_upper[0]);
676 for (i = 1; i < 16; i++)
677 err |= __put_user(((u32 *)regs->u_regs)[2*i],
678 &sf->v8plus.g_upper[i]);
679 err |= __put_user((regs->tstate & TSTATE_ASI) >> 24UL,
680 &sf->v8plus.asi);
681
682 if (psr & PSR_EF) {
David S. Miller55984732011-08-20 17:14:54 -0700683 __siginfo_fpu_t __user *fp = tail;
684 tail += sizeof(*fp);
685 err |= save_fpu_state(regs, fp);
686 err |= __put_user((u64)fp, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 } else {
688 err |= __put_user(0, &sf->fpu_save);
689 }
David S. Miller55984732011-08-20 17:14:54 -0700690 if (wsaved) {
691 __siginfo_rwin_t __user *rwp = tail;
692 tail += sizeof(*rwp);
693 err |= save_rwin_state(wsaved, rwp);
694 err |= __put_user((u64)rwp, &sf->rwin_save);
695 set_thread_wsaved(0);
696 } else {
697 err |= __put_user(0, &sf->rwin_save);
698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 /* Update the siginfo structure. */
701 err |= copy_siginfo_to_user32(&sf->info, info);
702
703 /* Setup sigaltstack */
704 err |= __put_user(current->sas_ss_sp, &sf->stack.ss_sp);
705 err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &sf->stack.ss_flags);
706 err |= __put_user(current->sas_ss_size, &sf->stack.ss_size);
707
708 switch (_NSIG_WORDS) {
709 case 4: seta.sig[7] = (oldset->sig[3] >> 32);
710 seta.sig[6] = oldset->sig[3];
711 case 3: seta.sig[5] = (oldset->sig[2] >> 32);
712 seta.sig[4] = oldset->sig[2];
713 case 2: seta.sig[3] = (oldset->sig[1] >> 32);
714 seta.sig[2] = oldset->sig[1];
715 case 1: seta.sig[1] = (oldset->sig[0] >> 32);
716 seta.sig[0] = oldset->sig[0];
717 }
718 err |= __copy_to_user(&sf->mask, &seta, sizeof(compat_sigset_t));
719
David S. Miller55984732011-08-20 17:14:54 -0700720 if (!wsaved) {
721 err |= copy_in_user((u32 __user *)sf,
722 (u32 __user *)(regs->u_regs[UREG_FP]),
723 sizeof(struct reg_window32));
724 } else {
725 struct reg_window *rp;
726
727 rp = &current_thread_info()->reg_window[wsaved - 1];
728 for (i = 0; i < 8; i++)
729 err |= __put_user(rp->locals[i], &sf->ss.locals[i]);
730 for (i = 0; i < 6; i++)
731 err |= __put_user(rp->ins[i], &sf->ss.ins[i]);
732 err |= __put_user(rp->ins[6], &sf->ss.fp);
733 err |= __put_user(rp->ins[7], &sf->ss.callers_pc);
734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (err)
736 goto sigsegv;
737
738 /* 3. signal handler back-trampoline and parameters */
739 regs->u_regs[UREG_FP] = (unsigned long) sf;
740 regs->u_regs[UREG_I0] = signr;
741 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
742 regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
743
744 /* 4. signal handler */
745 regs->tpc = (unsigned long) ka->sa.sa_handler;
746 regs->tnpc = (regs->tpc + 4);
747 if (test_thread_flag(TIF_32BIT)) {
748 regs->tpc &= 0xffffffff;
749 regs->tnpc &= 0xffffffff;
750 }
751
752 /* 5. return to kernel instructions */
753 if (ka->ka_restorer)
754 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
755 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 unsigned long address = ((unsigned long)&(sf->insns[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 regs->u_regs[UREG_I7] = (unsigned long) (&(sf->insns[0]) - 2);
759
760 /* mov __NR_rt_sigreturn, %g1 */
761 err |= __put_user(0x82102065, &sf->insns[0]);
762
763 /* t 0x10 */
764 err |= __put_user(0x91d02010, &sf->insns[1]);
765 if (err)
766 goto sigsegv;
767
David S. Miller05c5e762010-09-20 23:24:52 -0700768 flush_signal_insns(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
David S. Miller392c2182010-09-21 21:41:12 -0700770 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772sigill:
773 do_exit(SIGILL);
David S. Miller392c2182010-09-21 21:41:12 -0700774 return -EINVAL;
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776sigsegv:
777 force_sigsegv(signr, current);
David S. Miller392c2182010-09-21 21:41:12 -0700778 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
David S. Miller392c2182010-09-21 21:41:12 -0700781static inline int handle_signal32(unsigned long signr, struct k_sigaction *ka,
782 siginfo_t *info,
783 sigset_t *oldset, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
David S. Miller392c2182010-09-21 21:41:12 -0700785 int err;
786
David S. Millerec98c6b2008-04-20 02:14:23 -0700787 if (ka->sa.sa_flags & SA_SIGINFO)
David S. Miller392c2182010-09-21 21:41:12 -0700788 err = setup_rt_frame32(ka, regs, signr, oldset, info);
David S. Millerec98c6b2008-04-20 02:14:23 -0700789 else
David S. Miller392c2182010-09-21 21:41:12 -0700790 err = setup_frame32(ka, regs, signr, oldset);
791
792 if (err)
793 return err;
David S. Miller5526b7e2008-04-27 02:26:36 -0700794
Steven Rostedt69be8f12005-08-29 11:44:09 -0400795 spin_lock_irq(&current->sighand->siglock);
796 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
797 if (!(ka->sa.sa_flags & SA_NOMASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 sigaddset(&current->blocked,signr);
Steven Rostedt69be8f12005-08-29 11:44:09 -0400799 recalc_sigpending();
800 spin_unlock_irq(&current->sighand->siglock);
David S. Miller392c2182010-09-21 21:41:12 -0700801
802 tracehook_signal_handler(signr, info, ka, regs, 0);
803
804 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
807static inline void syscall_restart32(unsigned long orig_i0, struct pt_regs *regs,
808 struct sigaction *sa)
809{
810 switch (regs->u_regs[UREG_I0]) {
811 case ERESTART_RESTARTBLOCK:
812 case ERESTARTNOHAND:
813 no_system_call_restart:
814 regs->u_regs[UREG_I0] = EINTR;
815 regs->tstate |= TSTATE_ICARRY;
816 break;
817 case ERESTARTSYS:
818 if (!(sa->sa_flags & SA_RESTART))
819 goto no_system_call_restart;
820 /* fallthrough */
821 case ERESTARTNOINTR:
822 regs->u_regs[UREG_I0] = orig_i0;
823 regs->tpc -= 4;
824 regs->tnpc -= 4;
825 }
826}
827
828/* Note that 'init' is a special process: it doesn't get signals it doesn't
829 * want to handle. Thus you cannot kill init even with a SIGKILL even by
830 * mistake.
831 */
David S. Miller2d7d5f02006-01-19 02:42:49 -0800832void do_signal32(sigset_t *oldset, struct pt_regs * regs,
David S. Miller28e61032008-05-11 02:07:19 -0700833 int restart_syscall, unsigned long orig_i0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 struct k_sigaction ka;
David S. Miller238468b2008-04-24 03:01:48 -0700836 siginfo_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
David S. Miller28e61032008-05-11 02:07:19 -0700839 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
840
841 /* If the debugger messes with the program counter, it clears
842 * the "in syscall" bit, directing us to not perform a syscall
843 * restart.
844 */
845 if (restart_syscall && !pt_regs_is_syscall(regs))
846 restart_syscall = 0;
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (signr > 0) {
David S. Miller28e61032008-05-11 02:07:19 -0700849 if (restart_syscall)
850 syscall_restart32(orig_i0, regs, &ka.sa);
David S. Miller392c2182010-09-21 21:41:12 -0700851 if (handle_signal32(signr, &ka, &info, oldset, regs) == 0) {
852 /* A signal was successfully delivered; the saved
853 * sigmask will have been stored in the signal frame,
854 * and will be restored by sigreturn, so we can simply
855 * clear the TS_RESTORE_SIGMASK flag.
856 */
857 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
858 }
David S. Miller2d7d5f02006-01-19 02:42:49 -0800859 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
David S. Miller28e61032008-05-11 02:07:19 -0700861 if (restart_syscall &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
863 regs->u_regs[UREG_I0] == ERESTARTSYS ||
864 regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
865 /* replay the system call when we are done */
David S. Miller28e61032008-05-11 02:07:19 -0700866 regs->u_regs[UREG_I0] = orig_i0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 regs->tpc -= 4;
868 regs->tnpc -= 4;
David S. Millerc2785252010-09-21 22:30:13 -0700869 pt_regs_clear_syscall(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
David S. Miller28e61032008-05-11 02:07:19 -0700871 if (restart_syscall &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
873 regs->u_regs[UREG_G1] = __NR_restart_syscall;
874 regs->tpc -= 4;
875 regs->tnpc -= 4;
David S. Millerc2785252010-09-21 22:30:13 -0700876 pt_regs_clear_syscall(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
David S. Miller2d7d5f02006-01-19 02:42:49 -0800878
David S. Miller9a28dbf2008-05-12 22:45:15 -0700879 /* If there's no signal to deliver, we just put the saved sigmask
David S. Miller2d7d5f02006-01-19 02:42:49 -0800880 * back
881 */
David S. Miller9a28dbf2008-05-12 22:45:15 -0700882 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
883 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
David S. Miller2d7d5f02006-01-19 02:42:49 -0800884 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
888struct sigstack32 {
889 u32 the_stack;
890 int cur_status;
891};
892
893asmlinkage int do_sys32_sigstack(u32 u_ssptr, u32 u_ossptr, unsigned long sp)
894{
895 struct sigstack32 __user *ssptr =
896 (struct sigstack32 __user *)((unsigned long)(u_ssptr));
897 struct sigstack32 __user *ossptr =
898 (struct sigstack32 __user *)((unsigned long)(u_ossptr));
899 int ret = -EFAULT;
900
901 /* First see if old state is wanted. */
902 if (ossptr) {
903 if (put_user(current->sas_ss_sp + current->sas_ss_size,
904 &ossptr->the_stack) ||
905 __put_user(on_sig_stack(sp), &ossptr->cur_status))
906 goto out;
907 }
908
909 /* Now see if we want to update the new state. */
910 if (ssptr) {
911 u32 ss_sp;
912
913 if (get_user(ss_sp, &ssptr->the_stack))
914 goto out;
915
916 /* If the current stack was set with sigaltstack, don't
917 * swap stacks while we are on it.
918 */
919 ret = -EPERM;
920 if (current->sas_ss_sp && on_sig_stack(sp))
921 goto out;
922
923 /* Since we don't know the extent of the stack, and we don't
924 * track onstack-ness, but rather calculate it, we must
925 * presume a size. Ho hum this interface is lossy.
926 */
927 current->sas_ss_sp = (unsigned long)ss_sp - SIGSTKSZ;
928 current->sas_ss_size = SIGSTKSZ;
929 }
930
931 ret = 0;
932out:
933 return ret;
934}
935
936asmlinkage long do_sys32_sigaltstack(u32 ussa, u32 uossa, unsigned long sp)
937{
938 stack_t uss, uoss;
939 u32 u_ss_sp = 0;
940 int ret;
941 mm_segment_t old_fs;
942 stack_t32 __user *uss32 = compat_ptr(ussa);
943 stack_t32 __user *uoss32 = compat_ptr(uossa);
944
945 if (ussa && (get_user(u_ss_sp, &uss32->ss_sp) ||
946 __get_user(uss.ss_flags, &uss32->ss_flags) ||
947 __get_user(uss.ss_size, &uss32->ss_size)))
948 return -EFAULT;
949 uss.ss_sp = compat_ptr(u_ss_sp);
950 old_fs = get_fs();
951 set_fs(KERNEL_DS);
952 ret = do_sigaltstack(ussa ? (stack_t __user *) &uss : NULL,
953 uossa ? (stack_t __user *) &uoss : NULL, sp);
954 set_fs(old_fs);
955 if (!ret && uossa && (put_user(ptr_to_compat(uoss.ss_sp), &uoss32->ss_sp) ||
956 __put_user(uoss.ss_flags, &uoss32->ss_flags) ||
957 __put_user(uoss.ss_size, &uoss32->ss_size)))
958 return -EFAULT;
959 return ret;
960}