blob: 55b820ee0ac9b6e02e22a24c3144affd390ede0d [file] [log] [blame]
Adrian Bunkb00dc832008-05-19 16:52:27 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * arch/sparc64/kernel/signal.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
David S. Miller0a4949c42008-07-31 20:40:46 -07005 * Copyright (C) 1995, 2008 David S. Miller (davem@davemloft.net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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
David S. Miller09337f52008-04-26 03:17:12 -070011#ifdef CONFIG_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/compat.h> /* for compat_old_sigset_t */
13#endif
14#include <linux/sched.h>
15#include <linux/kernel.h>
16#include <linux/signal.h>
17#include <linux/errno.h>
18#include <linux/wait.h>
19#include <linux/ptrace.h>
Roland McGrathe35a8922008-04-20 15:06:49 -070020#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/unistd.h>
22#include <linux/mm.h>
23#include <linux/tty.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/binfmts.h>
25#include <linux/bitops.h>
26
27#include <asm/uaccess.h>
28#include <asm/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/pgtable.h>
30#include <asm/fpumacro.h>
31#include <asm/uctx.h>
32#include <asm/siginfo.h>
33#include <asm/visasm.h>
David Howellsd550bbd2012-03-28 18:30:03 +010034#include <asm/switch_to.h>
35#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
David S. Miller062ea6d2008-03-26 01:52:18 -070037#include "entry.h"
38#include "systbls.h"
David S. Miller55984732011-08-20 17:14:54 -070039#include "sigutil.h"
David S. Miller062ea6d2008-03-26 01:52:18 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/* {set, get}context() needed for 64-bit SparcLinux userland. */
44asmlinkage void sparc64_set_context(struct pt_regs *regs)
45{
46 struct ucontext __user *ucp = (struct ucontext __user *)
47 regs->u_regs[UREG_I0];
48 mc_gregset_t __user *grp;
49 unsigned long pc, npc, tstate;
50 unsigned long fp, i7;
51 unsigned char fenab;
52 int err;
53
54 flush_user_windows();
55 if (get_thread_wsaved() ||
56 (((unsigned long)ucp) & (sizeof(unsigned long)-1)) ||
57 (!__access_ok(ucp, sizeof(*ucp))))
58 goto do_sigsegv;
59 grp = &ucp->uc_mcontext.mc_gregs;
60 err = __get_user(pc, &((*grp)[MC_PC]));
61 err |= __get_user(npc, &((*grp)[MC_NPC]));
62 if (err || ((pc | npc) & 3))
63 goto do_sigsegv;
64 if (regs->u_regs[UREG_I1]) {
65 sigset_t set;
66
67 if (_NSIG_WORDS == 1) {
68 if (__get_user(set.sig[0], &ucp->uc_sigmask.sig[0]))
69 goto do_sigsegv;
70 } else {
71 if (__copy_from_user(&set, &ucp->uc_sigmask, sizeof(sigset_t)))
72 goto do_sigsegv;
73 }
74 sigdelsetmask(&set, ~_BLOCKABLE);
Matt Flemingfaddf592011-08-11 14:57:02 +010075 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
77 if (test_thread_flag(TIF_32BIT)) {
78 pc &= 0xffffffff;
79 npc &= 0xffffffff;
80 }
81 regs->tpc = pc;
82 regs->tnpc = npc;
83 err |= __get_user(regs->y, &((*grp)[MC_Y]));
84 err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
85 regs->tstate &= ~(TSTATE_ASI | TSTATE_ICC | TSTATE_XCC);
86 regs->tstate |= (tstate & (TSTATE_ASI | TSTATE_ICC | TSTATE_XCC));
87 err |= __get_user(regs->u_regs[UREG_G1], (&(*grp)[MC_G1]));
88 err |= __get_user(regs->u_regs[UREG_G2], (&(*grp)[MC_G2]));
89 err |= __get_user(regs->u_regs[UREG_G3], (&(*grp)[MC_G3]));
90 err |= __get_user(regs->u_regs[UREG_G4], (&(*grp)[MC_G4]));
91 err |= __get_user(regs->u_regs[UREG_G5], (&(*grp)[MC_G5]));
92 err |= __get_user(regs->u_regs[UREG_G6], (&(*grp)[MC_G6]));
David S. Miller0a4949c42008-07-31 20:40:46 -070093
94 /* Skip %g7 as that's the thread register in userspace. */
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 err |= __get_user(regs->u_regs[UREG_I0], (&(*grp)[MC_O0]));
97 err |= __get_user(regs->u_regs[UREG_I1], (&(*grp)[MC_O1]));
98 err |= __get_user(regs->u_regs[UREG_I2], (&(*grp)[MC_O2]));
99 err |= __get_user(regs->u_regs[UREG_I3], (&(*grp)[MC_O3]));
100 err |= __get_user(regs->u_regs[UREG_I4], (&(*grp)[MC_O4]));
101 err |= __get_user(regs->u_regs[UREG_I5], (&(*grp)[MC_O5]));
102 err |= __get_user(regs->u_regs[UREG_I6], (&(*grp)[MC_O6]));
103 err |= __get_user(regs->u_regs[UREG_I7], (&(*grp)[MC_O7]));
104
105 err |= __get_user(fp, &(ucp->uc_mcontext.mc_fp));
106 err |= __get_user(i7, &(ucp->uc_mcontext.mc_i7));
107 err |= __put_user(fp,
108 (&(((struct reg_window __user *)(STACK_BIAS+regs->u_regs[UREG_I6]))->ins[6])));
109 err |= __put_user(i7,
110 (&(((struct reg_window __user *)(STACK_BIAS+regs->u_regs[UREG_I6]))->ins[7])));
111
112 err |= __get_user(fenab, &(ucp->uc_mcontext.mc_fpregs.mcfpu_enab));
113 if (fenab) {
114 unsigned long *fpregs = current_thread_info()->fpregs;
115 unsigned long fprs;
116
117 fprs_write(0);
118 err |= __get_user(fprs, &(ucp->uc_mcontext.mc_fpregs.mcfpu_fprs));
119 if (fprs & FPRS_DL)
120 err |= copy_from_user(fpregs,
121 &(ucp->uc_mcontext.mc_fpregs.mcfpu_fregs),
122 (sizeof(unsigned int) * 32));
123 if (fprs & FPRS_DU)
124 err |= copy_from_user(fpregs+16,
125 ((unsigned long __user *)&(ucp->uc_mcontext.mc_fpregs.mcfpu_fregs))+16,
126 (sizeof(unsigned int) * 32));
127 err |= __get_user(current_thread_info()->xfsr[0],
128 &(ucp->uc_mcontext.mc_fpregs.mcfpu_fsr));
129 err |= __get_user(current_thread_info()->gsr[0],
130 &(ucp->uc_mcontext.mc_fpregs.mcfpu_gsr));
131 regs->tstate &= ~TSTATE_PEF;
132 }
133 if (err)
134 goto do_sigsegv;
135
136 return;
137do_sigsegv:
138 force_sig(SIGSEGV, current);
139}
140
141asmlinkage void sparc64_get_context(struct pt_regs *regs)
142{
143 struct ucontext __user *ucp = (struct ucontext __user *)
144 regs->u_regs[UREG_I0];
145 mc_gregset_t __user *grp;
146 mcontext_t __user *mcp;
147 unsigned long fp, i7;
148 unsigned char fenab;
149 int err;
150
151 synchronize_user_stack();
152 if (get_thread_wsaved() || clear_user(ucp, sizeof(*ucp)))
153 goto do_sigsegv;
154
155#if 1
156 fenab = 0; /* IMO get_context is like any other system call, thus modifies FPU state -jj */
157#else
158 fenab = (current_thread_info()->fpsaved[0] & FPRS_FEF);
159#endif
160
161 mcp = &ucp->uc_mcontext;
162 grp = &mcp->mc_gregs;
163
164 /* Skip over the trap instruction, first. */
165 if (test_thread_flag(TIF_32BIT)) {
166 regs->tpc = (regs->tnpc & 0xffffffff);
167 regs->tnpc = (regs->tnpc + 4) & 0xffffffff;
168 } else {
169 regs->tpc = regs->tnpc;
170 regs->tnpc += 4;
171 }
172 err = 0;
173 if (_NSIG_WORDS == 1)
174 err |= __put_user(current->blocked.sig[0],
175 (unsigned long __user *)&ucp->uc_sigmask);
176 else
177 err |= __copy_to_user(&ucp->uc_sigmask, &current->blocked,
178 sizeof(sigset_t));
179
180 err |= __put_user(regs->tstate, &((*grp)[MC_TSTATE]));
181 err |= __put_user(regs->tpc, &((*grp)[MC_PC]));
182 err |= __put_user(regs->tnpc, &((*grp)[MC_NPC]));
183 err |= __put_user(regs->y, &((*grp)[MC_Y]));
184 err |= __put_user(regs->u_regs[UREG_G1], &((*grp)[MC_G1]));
185 err |= __put_user(regs->u_regs[UREG_G2], &((*grp)[MC_G2]));
186 err |= __put_user(regs->u_regs[UREG_G3], &((*grp)[MC_G3]));
187 err |= __put_user(regs->u_regs[UREG_G4], &((*grp)[MC_G4]));
188 err |= __put_user(regs->u_regs[UREG_G5], &((*grp)[MC_G5]));
189 err |= __put_user(regs->u_regs[UREG_G6], &((*grp)[MC_G6]));
190 err |= __put_user(regs->u_regs[UREG_G7], &((*grp)[MC_G7]));
191 err |= __put_user(regs->u_regs[UREG_I0], &((*grp)[MC_O0]));
192 err |= __put_user(regs->u_regs[UREG_I1], &((*grp)[MC_O1]));
193 err |= __put_user(regs->u_regs[UREG_I2], &((*grp)[MC_O2]));
194 err |= __put_user(regs->u_regs[UREG_I3], &((*grp)[MC_O3]));
195 err |= __put_user(regs->u_regs[UREG_I4], &((*grp)[MC_O4]));
196 err |= __put_user(regs->u_regs[UREG_I5], &((*grp)[MC_O5]));
197 err |= __put_user(regs->u_regs[UREG_I6], &((*grp)[MC_O6]));
198 err |= __put_user(regs->u_regs[UREG_I7], &((*grp)[MC_O7]));
199
200 err |= __get_user(fp,
201 (&(((struct reg_window __user *)(STACK_BIAS+regs->u_regs[UREG_I6]))->ins[6])));
202 err |= __get_user(i7,
203 (&(((struct reg_window __user *)(STACK_BIAS+regs->u_regs[UREG_I6]))->ins[7])));
204 err |= __put_user(fp, &(mcp->mc_fp));
205 err |= __put_user(i7, &(mcp->mc_i7));
206
207 err |= __put_user(fenab, &(mcp->mc_fpregs.mcfpu_enab));
208 if (fenab) {
209 unsigned long *fpregs = current_thread_info()->fpregs;
210 unsigned long fprs;
211
212 fprs = current_thread_info()->fpsaved[0];
213 if (fprs & FPRS_DL)
214 err |= copy_to_user(&(mcp->mc_fpregs.mcfpu_fregs), fpregs,
215 (sizeof(unsigned int) * 32));
216 if (fprs & FPRS_DU)
217 err |= copy_to_user(
218 ((unsigned long __user *)&(mcp->mc_fpregs.mcfpu_fregs))+16, fpregs+16,
219 (sizeof(unsigned int) * 32));
220 err |= __put_user(current_thread_info()->xfsr[0], &(mcp->mc_fpregs.mcfpu_fsr));
221 err |= __put_user(current_thread_info()->gsr[0], &(mcp->mc_fpregs.mcfpu_gsr));
222 err |= __put_user(fprs, &(mcp->mc_fpregs.mcfpu_fprs));
223 }
224 if (err)
225 goto do_sigsegv;
226
227 return;
228do_sigsegv:
229 force_sig(SIGSEGV, current);
230}
231
232struct rt_signal_frame {
233 struct sparc_stackf ss;
234 siginfo_t info;
235 struct pt_regs regs;
236 __siginfo_fpu_t __user *fpu_save;
237 stack_t stack;
238 sigset_t mask;
David S. Miller55984732011-08-20 17:14:54 -0700239 __siginfo_rwin_t *rwin_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240};
241
David S. Miller2d7d5f02006-01-19 02:42:49 -0800242static long _sigpause_common(old_sigset_t set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Matt Flemingfaddf592011-08-11 14:57:02 +0100244 sigset_t blocked;
Matt Flemingfaddf592011-08-11 14:57:02 +0100245 siginitset(&blocked, set);
Al Viro68f3f162012-05-21 21:42:32 -0400246 return sigsuspend(&blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
David S. Miller2d7d5f02006-01-19 02:42:49 -0800249asmlinkage long sys_sigpause(unsigned int set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
David S. Miller2d7d5f02006-01-19 02:42:49 -0800251 return _sigpause_common(set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
David S. Miller2d7d5f02006-01-19 02:42:49 -0800254asmlinkage long sys_sigsuspend(old_sigset_t set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
David S. Miller2d7d5f02006-01-19 02:42:49 -0800256 return _sigpause_common(set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259void do_rt_sigreturn(struct pt_regs *regs)
260{
261 struct rt_signal_frame __user *sf;
262 unsigned long tpc, tnpc, tstate;
263 __siginfo_fpu_t __user *fpu_save;
David S. Miller55984732011-08-20 17:14:54 -0700264 __siginfo_rwin_t __user *rwin_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 sigset_t set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 int err;
267
268 /* Always make any pending restarted system calls return -EINTR */
269 current_thread_info()->restart_block.fn = do_no_restart_syscall;
270
271 synchronize_user_stack ();
272 sf = (struct rt_signal_frame __user *)
273 (regs->u_regs [UREG_FP] + STACK_BIAS);
274
275 /* 1. Make sure we are not getting garbage from the user */
276 if (((unsigned long) sf) & 3)
277 goto segv;
278
279 err = get_user(tpc, &sf->regs.tpc);
280 err |= __get_user(tnpc, &sf->regs.tnpc);
281 if (test_thread_flag(TIF_32BIT)) {
282 tpc &= 0xffffffff;
283 tnpc &= 0xffffffff;
284 }
285 err |= ((tpc | tnpc) & 3);
286
287 /* 2. Restore the state */
288 err |= __get_user(regs->y, &sf->regs.y);
289 err |= __get_user(tstate, &sf->regs.tstate);
290 err |= copy_from_user(regs->u_regs, sf->regs.u_regs, sizeof(regs->u_regs));
291
292 /* User can only change condition codes and %asi in %tstate. */
293 regs->tstate &= ~(TSTATE_ASI | TSTATE_ICC | TSTATE_XCC);
294 regs->tstate |= (tstate & (TSTATE_ASI | TSTATE_ICC | TSTATE_XCC));
295
296 err |= __get_user(fpu_save, &sf->fpu_save);
David S. Miller55984732011-08-20 17:14:54 -0700297 if (!err && fpu_save)
298 err |= restore_fpu_state(regs, fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 err |= __copy_from_user(&set, &sf->mask, sizeof(sigset_t));
Oleg Nesterov62715ec2007-07-17 14:37:54 -0700301 err |= do_sigaltstack(&sf->stack, NULL, (unsigned long)sf);
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (err)
304 goto segv;
Oleg Nesterov62715ec2007-07-17 14:37:54 -0700305
David S. Miller55984732011-08-20 17:14:54 -0700306 err |= __get_user(rwin_save, &sf->rwin_save);
307 if (!err && rwin_save) {
308 if (restore_rwin_state(rwin_save))
309 goto segv;
310 }
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 regs->tpc = tpc;
313 regs->tnpc = tnpc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
David S. Miller2678fef2008-05-01 03:30:22 -0700315 /* Prevent syscall restart. */
David S. Miller28e61032008-05-11 02:07:19 -0700316 pt_regs_clear_syscall(regs);
David S. Miller2678fef2008-05-01 03:30:22 -0700317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 sigdelsetmask(&set, ~_BLOCKABLE);
Matt Flemingfaddf592011-08-11 14:57:02 +0100319 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return;
321segv:
322 force_sig(SIGSEGV, current);
323}
324
325/* Checks if the fp is valid */
David S. Miller55984732011-08-20 17:14:54 -0700326static int invalid_frame_pointer(void __user *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
David S. Millerf036d9f2010-02-09 16:18:40 -0800328 if (((unsigned long) fp) & 15)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return 1;
330 return 0;
331}
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333static inline void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, unsigned long framesize)
334{
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700335 unsigned long sp = regs->u_regs[UREG_FP] + STACK_BIAS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700337 /*
338 * If we are on the alternate signal stack and would overflow it, don't.
339 * Return an always-bogus address instead so we will die with SIGSEGV.
340 */
341 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
342 return (void __user *) -1L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 /* This is the X/Open sanctioned signal stack switching. */
345 if (ka->sa.sa_flags & SA_ONSTACK) {
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700346 if (sas_ss_flags(sp) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 sp = current->sas_ss_sp + current->sas_ss_size;
348 }
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700349
David S. Millerf036d9f2010-02-09 16:18:40 -0800350 sp -= framesize;
351
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700352 /* Always align the stack frame. This handles two cases. First,
353 * sigaltstack need not be mindful of platform specific stack
354 * alignment. Second, if we took this signal because the stack
355 * is not aligned properly, we'd like to take the signal cleanly
356 * and report that.
357 */
David S. Millerf036d9f2010-02-09 16:18:40 -0800358 sp &= ~15UL;
David S. Millerdc5dc7e2008-05-07 18:54:05 -0700359
David S. Millerf036d9f2010-02-09 16:18:40 -0800360 return (void __user *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
David S. Miller392c2182010-09-21 21:41:12 -0700363static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs,
365 int signo, sigset_t *oldset, siginfo_t *info)
366{
367 struct rt_signal_frame __user *sf;
David S. Miller55984732011-08-20 17:14:54 -0700368 int wsaved, err, sf_size;
369 void __user *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 /* 1. Make sure everything is clean */
372 synchronize_user_stack();
373 save_and_clear_fpu();
374
David S. Miller55984732011-08-20 17:14:54 -0700375 wsaved = get_thread_wsaved();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
David S. Miller55984732011-08-20 17:14:54 -0700377 sf_size = sizeof(struct rt_signal_frame);
378 if (current_thread_info()->fpsaved[0] & FPRS_FEF)
379 sf_size += sizeof(__siginfo_fpu_t);
380 if (wsaved)
381 sf_size += sizeof(__siginfo_rwin_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 sf = (struct rt_signal_frame __user *)
David S. Miller55984732011-08-20 17:14:54 -0700383 get_sigframe(ka, regs, sf_size);
384
385 if (invalid_frame_pointer (sf))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 goto sigill;
387
David S. Miller55984732011-08-20 17:14:54 -0700388 tail = (sf + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 /* 2. Save the current process state */
391 err = copy_to_user(&sf->regs, regs, sizeof (*regs));
392
393 if (current_thread_info()->fpsaved[0] & FPRS_FEF) {
David S. Miller55984732011-08-20 17:14:54 -0700394 __siginfo_fpu_t __user *fpu_save = tail;
395 tail += sizeof(__siginfo_fpu_t);
396 err |= save_fpu_state(regs, fpu_save);
397 err |= __put_user((u64)fpu_save, &sf->fpu_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 } else {
399 err |= __put_user(0, &sf->fpu_save);
400 }
David S. Miller55984732011-08-20 17:14:54 -0700401 if (wsaved) {
402 __siginfo_rwin_t __user *rwin_save = tail;
403 tail += sizeof(__siginfo_rwin_t);
404 err |= save_rwin_state(wsaved, rwin_save);
405 err |= __put_user((u64)rwin_save, &sf->rwin_save);
406 set_thread_wsaved(0);
407 } else {
408 err |= __put_user(0, &sf->rwin_save);
409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 /* Setup sigaltstack */
412 err |= __put_user(current->sas_ss_sp, &sf->stack.ss_sp);
413 err |= __put_user(sas_ss_flags(regs->u_regs[UREG_FP]), &sf->stack.ss_flags);
414 err |= __put_user(current->sas_ss_size, &sf->stack.ss_size);
415
416 err |= copy_to_user(&sf->mask, oldset, sizeof(sigset_t));
417
David S. Miller55984732011-08-20 17:14:54 -0700418 if (!wsaved) {
419 err |= copy_in_user((u64 __user *)sf,
420 (u64 __user *)(regs->u_regs[UREG_FP] +
421 STACK_BIAS),
422 sizeof(struct reg_window));
423 } else {
424 struct reg_window *rp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
David S. Miller55984732011-08-20 17:14:54 -0700426 rp = &current_thread_info()->reg_window[wsaved - 1];
427 err |= copy_to_user(sf, rp, sizeof(struct reg_window));
428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (info)
430 err |= copy_siginfo_to_user(&sf->info, info);
431 else {
432 err |= __put_user(signo, &sf->info.si_signo);
433 err |= __put_user(SI_NOINFO, &sf->info.si_code);
434 }
435 if (err)
436 goto sigsegv;
437
438 /* 3. signal handler back-trampoline and parameters */
439 regs->u_regs[UREG_FP] = ((unsigned long) sf) - STACK_BIAS;
440 regs->u_regs[UREG_I0] = signo;
441 regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
442
443 /* The sigcontext is passed in this way because of how it
444 * is defined in GLIBC's /usr/include/bits/sigcontext.h
445 * for sparc64. It includes the 128 bytes of siginfo_t.
446 */
447 regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
448
449 /* 5. signal handler */
450 regs->tpc = (unsigned long) ka->sa.sa_handler;
451 regs->tnpc = (regs->tpc + 4);
452 if (test_thread_flag(TIF_32BIT)) {
453 regs->tpc &= 0xffffffff;
454 regs->tnpc &= 0xffffffff;
455 }
456 /* 4. return to kernel instructions */
457 regs->u_regs[UREG_I7] = (unsigned long)ka->ka_restorer;
David S. Miller392c2182010-09-21 21:41:12 -0700458 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460sigill:
461 do_exit(SIGILL);
David S. Miller392c2182010-09-21 21:41:12 -0700462 return -EINVAL;
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464sigsegv:
465 force_sigsegv(signo, current);
David S. Miller392c2182010-09-21 21:41:12 -0700466 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
David S. Miller392c2182010-09-21 21:41:12 -0700469static inline int handle_signal(unsigned long signr, struct k_sigaction *ka,
470 siginfo_t *info,
471 sigset_t *oldset, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
David S. Miller392c2182010-09-21 21:41:12 -0700473 int err;
474
475 err = setup_rt_frame(ka, regs, signr, oldset,
476 (ka->sa.sa_flags & SA_SIGINFO) ? info : NULL);
477 if (err)
478 return err;
David S. Miller392c2182010-09-21 21:41:12 -0700479
Matt Flemingce24d8a2012-03-21 16:33:46 -0700480 block_sigmask(ka, signr);
David S. Miller392c2182010-09-21 21:41:12 -0700481 tracehook_signal_handler(signr, info, ka, regs, 0);
482
483 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
486static inline void syscall_restart(unsigned long orig_i0, struct pt_regs *regs,
David S. Miller28e61032008-05-11 02:07:19 -0700487 struct sigaction *sa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 switch (regs->u_regs[UREG_I0]) {
490 case ERESTART_RESTARTBLOCK:
491 case ERESTARTNOHAND:
492 no_system_call_restart:
493 regs->u_regs[UREG_I0] = EINTR;
494 regs->tstate |= (TSTATE_ICARRY|TSTATE_XCARRY);
495 break;
496 case ERESTARTSYS:
497 if (!(sa->sa_flags & SA_RESTART))
498 goto no_system_call_restart;
499 /* fallthrough */
500 case ERESTARTNOINTR:
501 regs->u_regs[UREG_I0] = orig_i0;
502 regs->tpc -= 4;
503 regs->tnpc -= 4;
504 }
505}
506
507/* Note that 'init' is a special process: it doesn't get signals it doesn't
508 * want to handle. Thus you cannot kill init even with a SIGKILL even by
509 * mistake.
510 */
David S. Miller238468b2008-04-24 03:01:48 -0700511static void do_signal(struct pt_regs *regs, unsigned long orig_i0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 struct k_sigaction ka;
David S. Miller28e61032008-05-11 02:07:19 -0700514 int restart_syscall;
David S. Miller2d7d5f02006-01-19 02:42:49 -0800515 sigset_t *oldset;
David S. Miller238468b2008-04-24 03:01:48 -0700516 siginfo_t info;
David S. Miller90888812008-04-27 14:52:51 -0700517 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
David S. Miller1d299bc2011-11-14 20:32:16 -0800519 /* It's a lot of work and synchronization to add a new ptrace
520 * register for GDB to save and restore in order to get
521 * orig_i0 correct for syscall restarts when debugging.
522 *
David S. Millere88d2462011-11-15 12:57:00 -0800523 * Although it should be the case that most of the global
524 * registers are volatile across a system call, glibc already
525 * depends upon that fact that we preserve them. So we can't
526 * just use any global register to save away the orig_i0 value.
527 *
528 * In particular %g2, %g3, %g4, and %g5 are all assumed to be
529 * preserved across a system call trap by various pieces of
530 * code in glibc.
531 *
532 * %g7 is used as the "thread register". %g6 is not used in
533 * any fixed manner. %g6 is used as a scratch register and
534 * a compiler temporary, but it's value is never used across
535 * a system call. Therefore %g6 is usable for orig_i0 storage.
David S. Miller1d299bc2011-11-14 20:32:16 -0800536 */
David S. Miller2678fef2008-05-01 03:30:22 -0700537 if (pt_regs_is_syscall(regs) &&
David S. Miller1d299bc2011-11-14 20:32:16 -0800538 (regs->tstate & (TSTATE_XCARRY | TSTATE_ICARRY)))
David S. Millere88d2462011-11-15 12:57:00 -0800539 regs->u_regs[UREG_G6] = orig_i0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
David S. Miller9a28dbf2008-05-12 22:45:15 -0700541 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
David S. Miller2d7d5f02006-01-19 02:42:49 -0800542 oldset = &current->saved_sigmask;
543 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 oldset = &current->blocked;
545
David S. Miller09337f52008-04-26 03:17:12 -0700546#ifdef CONFIG_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (test_thread_flag(TIF_32BIT)) {
David S. Miller1d299bc2011-11-14 20:32:16 -0800548 extern void do_signal32(sigset_t *, struct pt_regs *);
549 do_signal32(oldset, regs);
David S. Miller2d7d5f02006-01-19 02:42:49 -0800550 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552#endif
553
David S. Miller28e61032008-05-11 02:07:19 -0700554 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
555
David S. Miller1d299bc2011-11-14 20:32:16 -0800556 restart_syscall = 0;
557 if (pt_regs_is_syscall(regs) &&
558 (regs->tstate & (TSTATE_XCARRY | TSTATE_ICARRY))) {
559 restart_syscall = 1;
David S. Millere88d2462011-11-15 12:57:00 -0800560 orig_i0 = regs->u_regs[UREG_G6];
David S. Miller1d299bc2011-11-14 20:32:16 -0800561 }
David S. Miller28e61032008-05-11 02:07:19 -0700562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (signr > 0) {
David S. Miller28e61032008-05-11 02:07:19 -0700564 if (restart_syscall)
565 syscall_restart(orig_i0, regs, &ka.sa);
David S. Miller392c2182010-09-21 21:41:12 -0700566 if (handle_signal(signr, &ka, &info, oldset, regs) == 0) {
567 /* A signal was successfully delivered; the saved
568 * sigmask will have been stored in the signal frame,
569 * and will be restored by sigreturn, so we can simply
570 * clear the TS_RESTORE_SIGMASK flag.
571 */
572 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
573 }
David S. Miller2d7d5f02006-01-19 02:42:49 -0800574 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
David S. Miller28e61032008-05-11 02:07:19 -0700576 if (restart_syscall &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 (regs->u_regs[UREG_I0] == ERESTARTNOHAND ||
578 regs->u_regs[UREG_I0] == ERESTARTSYS ||
579 regs->u_regs[UREG_I0] == ERESTARTNOINTR)) {
580 /* replay the system call when we are done */
David S. Miller28e61032008-05-11 02:07:19 -0700581 regs->u_regs[UREG_I0] = orig_i0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 regs->tpc -= 4;
583 regs->tnpc -= 4;
David S. Millerc2785252010-09-21 22:30:13 -0700584 pt_regs_clear_syscall(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
David S. Miller28e61032008-05-11 02:07:19 -0700586 if (restart_syscall &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 regs->u_regs[UREG_I0] == ERESTART_RESTARTBLOCK) {
588 regs->u_regs[UREG_G1] = __NR_restart_syscall;
589 regs->tpc -= 4;
590 regs->tnpc -= 4;
David S. Millerc2785252010-09-21 22:30:13 -0700591 pt_regs_clear_syscall(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
David S. Miller2d7d5f02006-01-19 02:42:49 -0800593
David S. Miller9a28dbf2008-05-12 22:45:15 -0700594 /* If there's no signal to deliver, we just put the saved sigmask
David S. Miller2d7d5f02006-01-19 02:42:49 -0800595 * back
596 */
Al Viro51a7b442012-05-21 23:33:55 -0400597 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
David S. Miller7697daa2008-04-24 03:15:22 -0700600void do_notify_resume(struct pt_regs *regs, unsigned long orig_i0, unsigned long thread_info_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
David S. Miller9a28dbf2008-05-12 22:45:15 -0700602 if (thread_info_flags & _TIF_SIGPENDING)
David S. Miller238468b2008-04-24 03:01:48 -0700603 do_signal(regs, orig_i0);
Roland McGrathe35a8922008-04-20 15:06:49 -0700604 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
605 clear_thread_flag(TIF_NOTIFY_RESUME);
606 tracehook_notify_resume(regs);
607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
David Howellsee18d642009-09-02 09:14:21 +0100609