blob: 35b283d3df0c33ea4e3a6c3003b145fc714c89e1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Al Virof67aa2f2011-08-18 20:10:39 +01002 * Copyright (C) 2003 PathScale, Inc.
3 * Copyright (C) 2003 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Licensed under the GPL
5 */
6
Al Virof67aa2f2011-08-18 20:10:39 +01007
8#include <linux/personality.h>
Jeff Dike77969312008-01-30 13:31:08 +01009#include <linux/ptrace.h>
Al Virof67aa2f2011-08-18 20:10:39 +010010#include <linux/kernel.h>
Jeff Dike77969312008-01-30 13:31:08 +010011#include <asm/unistd.h>
12#include <asm/uaccess.h>
13#include <asm/ucontext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "frame_kern.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "skas.h"
16
Al Virof67aa2f2011-08-18 20:10:39 +010017#ifdef CONFIG_X86_32
18
Jeff Dikea5f6096c2007-10-16 01:27:15 -070019/*
20 * FPU tag word conversions.
21 */
22
23static inline unsigned short twd_i387_to_fxsr(unsigned short twd)
24{
25 unsigned int tmp; /* to avoid 16 bit prefixes in the code */
26
27 /* Transform each pair of bits into 01 (valid) or 00 (empty) */
28 tmp = ~twd;
29 tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
30 /* and move the valid bits to the lower byte. */
31 tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
32 tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
33 tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
34 return tmp;
35}
36
37static inline unsigned long twd_fxsr_to_i387(struct user_fxsr_struct *fxsave)
38{
39 struct _fpxreg *st = NULL;
40 unsigned long twd = (unsigned long) fxsave->twd;
41 unsigned long tag;
42 unsigned long ret = 0xffff0000;
43 int i;
44
Phil Carmody497888c2011-07-14 15:07:13 +030045#define FPREG_ADDR(f, n) ((char *)&(f)->st_space + (n) * 16)
Jeff Dikea5f6096c2007-10-16 01:27:15 -070046
47 for (i = 0; i < 8; i++) {
48 if (twd & 0x1) {
49 st = (struct _fpxreg *) FPREG_ADDR(fxsave, i);
50
51 switch (st->exponent & 0x7fff) {
52 case 0x7fff:
53 tag = 2; /* Special */
54 break;
55 case 0x0000:
56 if ( !st->significand[0] &&
57 !st->significand[1] &&
58 !st->significand[2] &&
59 !st->significand[3] ) {
60 tag = 1; /* Zero */
61 } else {
62 tag = 2; /* Special */
63 }
64 break;
65 default:
66 if (st->significand[3] & 0x8000) {
67 tag = 0; /* Valid */
68 } else {
69 tag = 2; /* Special */
70 }
71 break;
72 }
73 } else {
74 tag = 3; /* Empty */
75 }
76 ret |= (tag << (2 * i));
77 twd = twd >> 1;
78 }
79 return ret;
80}
81
82static int convert_fxsr_to_user(struct _fpstate __user *buf,
83 struct user_fxsr_struct *fxsave)
84{
85 unsigned long env[7];
86 struct _fpreg __user *to;
87 struct _fpxreg *from;
88 int i;
89
90 env[0] = (unsigned long)fxsave->cwd | 0xffff0000ul;
91 env[1] = (unsigned long)fxsave->swd | 0xffff0000ul;
92 env[2] = twd_fxsr_to_i387(fxsave);
93 env[3] = fxsave->fip;
94 env[4] = fxsave->fcs | ((unsigned long)fxsave->fop << 16);
95 env[5] = fxsave->foo;
96 env[6] = fxsave->fos;
97
98 if (__copy_to_user(buf, env, 7 * sizeof(unsigned long)))
99 return 1;
100
101 to = &buf->_st[0];
102 from = (struct _fpxreg *) &fxsave->st_space[0];
103 for (i = 0; i < 8; i++, to++, from++) {
104 unsigned long __user *t = (unsigned long __user *)to;
105 unsigned long *f = (unsigned long *)from;
106
107 if (__put_user(*f, t) ||
108 __put_user(*(f + 1), t + 1) ||
109 __put_user(from->exponent, &to->exponent))
110 return 1;
111 }
112 return 0;
113}
114
115static int convert_fxsr_from_user(struct user_fxsr_struct *fxsave,
116 struct _fpstate __user *buf)
117{
118 unsigned long env[7];
119 struct _fpxreg *to;
120 struct _fpreg __user *from;
121 int i;
122
123 if (copy_from_user( env, buf, 7 * sizeof(long)))
124 return 1;
125
126 fxsave->cwd = (unsigned short)(env[0] & 0xffff);
127 fxsave->swd = (unsigned short)(env[1] & 0xffff);
128 fxsave->twd = twd_i387_to_fxsr((unsigned short)(env[2] & 0xffff));
129 fxsave->fip = env[3];
130 fxsave->fop = (unsigned short)((env[4] & 0xffff0000ul) >> 16);
131 fxsave->fcs = (env[4] & 0xffff);
132 fxsave->foo = env[5];
133 fxsave->fos = env[6];
134
135 to = (struct _fpxreg *) &fxsave->st_space[0];
136 from = &buf->_st[0];
137 for (i = 0; i < 8; i++, to++, from++) {
138 unsigned long *t = (unsigned long *)to;
139 unsigned long __user *f = (unsigned long __user *)from;
140
141 if (__get_user(*t, f) ||
142 __get_user(*(t + 1), f + 1) ||
143 __get_user(to->exponent, &from->exponent))
144 return 1;
145 }
146 return 0;
147}
148
149extern int have_fpx_regs;
150
Al Virof67aa2f2011-08-18 20:10:39 +0100151#endif
152
Jeff Dike77bf4402007-10-16 01:26:58 -0700153static int copy_sc_from_user(struct pt_regs *regs,
154 struct sigcontext __user *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Jeff Dikea5f6096c2007-10-16 01:27:15 -0700156 struct sigcontext sc;
Jeff Dikea5a678c2008-02-04 22:30:54 -0800157 int err, pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Al Viro3b7d15b2012-04-22 03:27:28 -0400159 /* Always make any pending restarted system calls return -EINTR */
160 current_thread_info()->restart_block.fn = do_no_restart_syscall;
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 err = copy_from_user(&sc, from, sizeof(sc));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700163 if (err)
Jeff Dike5d864562007-05-06 14:51:24 -0700164 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Al Viroa955bd62011-08-18 20:00:49 +0100166#define GETREG(regno, regname) regs->regs.gp[HOST_##regno] = sc.regname
167
Al Virof67aa2f2011-08-18 20:10:39 +0100168#ifdef CONFIG_X86_32
Al Viroa955bd62011-08-18 20:00:49 +0100169 GETREG(GS, gs);
170 GETREG(FS, fs);
171 GETREG(ES, es);
172 GETREG(DS, ds);
Al Virof67aa2f2011-08-18 20:10:39 +0100173#endif
Al Viro3579a382011-08-18 20:10:09 +0100174 GETREG(DI, di);
175 GETREG(SI, si);
176 GETREG(BP, bp);
Al Viroa955bd62011-08-18 20:00:49 +0100177 GETREG(SP, sp);
Al Viro3579a382011-08-18 20:10:09 +0100178 GETREG(BX, bx);
179 GETREG(DX, dx);
180 GETREG(CX, cx);
181 GETREG(AX, ax);
Al Viroa955bd62011-08-18 20:00:49 +0100182 GETREG(IP, ip);
Al Virof67aa2f2011-08-18 20:10:39 +0100183
184#ifdef CONFIG_X86_64
185 GETREG(R8, r8);
186 GETREG(R9, r9);
187 GETREG(R10, r10);
188 GETREG(R11, r11);
189 GETREG(R12, r12);
190 GETREG(R13, r13);
191 GETREG(R14, r14);
192 GETREG(R15, r15);
193#endif
194
Al Viroa955bd62011-08-18 20:00:49 +0100195 GETREG(CS, cs);
196 GETREG(EFLAGS, flags);
Al Virof67aa2f2011-08-18 20:10:39 +0100197#ifdef CONFIG_X86_32
Al Viroa955bd62011-08-18 20:00:49 +0100198 GETREG(SS, ss);
Al Virof67aa2f2011-08-18 20:10:39 +0100199#endif
Al Viroa955bd62011-08-18 20:00:49 +0100200
201#undef GETREG
Al Virof67aa2f2011-08-18 20:10:39 +0100202
203 pid = userspace_pid[current_thread_info()->cpu];
204#ifdef CONFIG_X86_32
Jeff Dikea5f6096c2007-10-16 01:27:15 -0700205 if (have_fpx_regs) {
206 struct user_fxsr_struct fpx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
WANG Cong966c8072008-10-18 20:27:26 -0700208 err = copy_from_user(&fpx,
209 &((struct _fpstate __user *)sc.fpstate)->_fxsr_env[0],
Jeff Dikea5f6096c2007-10-16 01:27:15 -0700210 sizeof(struct user_fxsr_struct));
211 if (err)
212 return 1;
213
214 err = convert_fxsr_from_user(&fpx, sc.fpstate);
215 if (err)
216 return 1;
217
Jeff Dikea5a678c2008-02-04 22:30:54 -0800218 err = restore_fpx_registers(pid, (unsigned long *) &fpx);
Jeff Dikea5f6096c2007-10-16 01:27:15 -0700219 if (err < 0) {
220 printk(KERN_ERR "copy_sc_from_user - "
221 "restore_fpx_registers failed, errno = %d\n",
222 -err);
223 return 1;
224 }
Al Virof67aa2f2011-08-18 20:10:39 +0100225 } else
226#endif
227 {
Jeff Dikea5f6096c2007-10-16 01:27:15 -0700228 struct user_i387_struct fp;
229
230 err = copy_from_user(&fp, sc.fpstate,
231 sizeof(struct user_i387_struct));
232 if (err)
233 return 1;
234
Jeff Dikea5a678c2008-02-04 22:30:54 -0800235 err = restore_fp_registers(pid, (unsigned long *) &fp);
Jeff Dikea5f6096c2007-10-16 01:27:15 -0700236 if (err < 0) {
237 printk(KERN_ERR "copy_sc_from_user - "
238 "restore_fp_registers failed, errno = %d\n",
239 -err);
240 return 1;
241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
Jeff Dike5d864562007-05-06 14:51:24 -0700243 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
Jeff Dike77bf4402007-10-16 01:26:58 -0700246static int copy_sc_to_user(struct sigcontext __user *to,
Al Virof67aa2f2011-08-18 20:10:39 +0100247 struct _fpstate __user *to_fp, struct pt_regs *regs,
248 unsigned long mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Jeff Dikeba180fd2007-10-16 01:27:00 -0700250 struct sigcontext sc;
Bodo Stroesserc5784552005-05-05 16:15:31 -0700251 struct faultinfo * fi = &current->thread.arch.faultinfo;
Jeff Dikea5a678c2008-02-04 22:30:54 -0800252 int err, pid;
Al Viroa955bd62011-08-18 20:00:49 +0100253 memset(&sc, 0, sizeof(struct sigcontext));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Al Virof67aa2f2011-08-18 20:10:39 +0100255#define PUTREG(regno, regname) sc.regname = regs->regs.gp[HOST_##regno]
256
257#ifdef CONFIG_X86_32
258 PUTREG(GS, gs);
259 PUTREG(FS, fs);
260 PUTREG(ES, es);
261 PUTREG(DS, ds);
262#endif
263 PUTREG(DI, di);
264 PUTREG(SI, si);
265 PUTREG(BP, bp);
266 PUTREG(SP, sp);
267 PUTREG(BX, bx);
268 PUTREG(DX, dx);
269 PUTREG(CX, cx);
270 PUTREG(AX, ax);
271#ifdef CONFIG_X86_64
272 PUTREG(R8, r8);
273 PUTREG(R9, r9);
274 PUTREG(R10, r10);
275 PUTREG(R11, r11);
276 PUTREG(R12, r12);
277 PUTREG(R13, r13);
278 PUTREG(R14, r14);
279 PUTREG(R15, r15);
280#endif
281
Jeff Dikeba180fd2007-10-16 01:27:00 -0700282 sc.cr2 = fi->cr2;
283 sc.err = fi->error_code;
284 sc.trapno = fi->trap_no;
Al Virof67aa2f2011-08-18 20:10:39 +0100285 PUTREG(IP, ip);
286 PUTREG(CS, cs);
287 PUTREG(EFLAGS, flags);
288#ifdef CONFIG_X86_32
289 PUTREG(SP, sp_at_signal);
290 PUTREG(SS, ss);
291#endif
292#undef PUTREG
293 sc.oldmask = mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 sc.fpstate = to_fp;
295
Al Virof67aa2f2011-08-18 20:10:39 +0100296 err = copy_to_user(to, &sc, sizeof(struct sigcontext));
297 if (err)
298 return 1;
299
Jeff Dikea5a678c2008-02-04 22:30:54 -0800300 pid = userspace_pid[current_thread_info()->cpu];
Al Virof67aa2f2011-08-18 20:10:39 +0100301
302#ifdef CONFIG_X86_32
Jeff Dikea5f6096c2007-10-16 01:27:15 -0700303 if (have_fpx_regs) {
304 struct user_fxsr_struct fpx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Jeff Dikea5a678c2008-02-04 22:30:54 -0800306 err = save_fpx_registers(pid, (unsigned long *) &fpx);
Jeff Dikea5f6096c2007-10-16 01:27:15 -0700307 if (err < 0){
308 printk(KERN_ERR "copy_sc_to_user - save_fpx_registers "
309 "failed, errno = %d\n", err);
310 return 1;
311 }
312
313 err = convert_fxsr_to_user(to_fp, &fpx);
314 if (err)
315 return 1;
316
317 err |= __put_user(fpx.swd, &to_fp->status);
318 err |= __put_user(X86_FXSR_MAGIC, &to_fp->magic);
319 if (err)
320 return 1;
321
322 if (copy_to_user(&to_fp->_fxsr_env[0], &fpx,
323 sizeof(struct user_fxsr_struct)))
324 return 1;
Al Virof67aa2f2011-08-18 20:10:39 +0100325 } else
326#endif
327 {
Jeff Dikea5f6096c2007-10-16 01:27:15 -0700328 struct user_i387_struct fp;
329
Jeff Dikea5a678c2008-02-04 22:30:54 -0800330 err = save_fp_registers(pid, (unsigned long *) &fp);
Jeff Dikea5f6096c2007-10-16 01:27:15 -0700331 if (copy_to_user(to_fp, &fp, sizeof(struct user_i387_struct)))
332 return 1;
333 }
334
Al Virof67aa2f2011-08-18 20:10:39 +0100335 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Al Virof67aa2f2011-08-18 20:10:39 +0100338#ifdef CONFIG_X86_32
Jeff Dikeba180fd2007-10-16 01:27:00 -0700339static int copy_ucontext_to_user(struct ucontext __user *uc,
340 struct _fpstate __user *fp, sigset_t *set,
341 unsigned long sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 int err = 0;
344
345 err |= put_user(current->sas_ss_sp, &uc->uc_stack.ss_sp);
346 err |= put_user(sas_ss_flags(sp), &uc->uc_stack.ss_flags);
347 err |= put_user(current->sas_ss_size, &uc->uc_stack.ss_size);
Al Virof67aa2f2011-08-18 20:10:39 +0100348 err |= copy_sc_to_user(&uc->uc_mcontext, fp, &current->thread.regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 err |= copy_to_user(&uc->uc_sigmask, set, sizeof(*set));
Jeff Dike5d864562007-05-06 14:51:24 -0700350 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353struct sigframe
354{
Al Viro4d338e12006-03-31 02:30:15 -0800355 char __user *pretcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 int sig;
357 struct sigcontext sc;
358 struct _fpstate fpstate;
359 unsigned long extramask[_NSIG_WORDS-1];
360 char retcode[8];
361};
362
363struct rt_sigframe
364{
Al Viro4d338e12006-03-31 02:30:15 -0800365 char __user *pretcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 int sig;
Al Viro4d338e12006-03-31 02:30:15 -0800367 struct siginfo __user *pinfo;
368 void __user *puc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 struct siginfo info;
370 struct ucontext uc;
371 struct _fpstate fpstate;
372 char retcode[8];
373};
374
375int setup_signal_stack_sc(unsigned long stack_top, int sig,
376 struct k_sigaction *ka, struct pt_regs *regs,
377 sigset_t *mask)
378{
379 struct sigframe __user *frame;
Al Viro4d338e12006-03-31 02:30:15 -0800380 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 int err = 0;
382
Jeff Dike38966252007-01-30 14:36:17 -0800383 /* This is the same calculation as i386 - ((sp + 4) & 15) == 0 */
384 stack_top = ((stack_top + 4) & -16UL) - 4;
Al Viro4d338e12006-03-31 02:30:15 -0800385 frame = (struct sigframe __user *) stack_top - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
387 return 1;
388
Al Viro4d338e12006-03-31 02:30:15 -0800389 restorer = frame->retcode;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700390 if (ka->sa.sa_flags & SA_RESTORER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 restorer = ka->sa.sa_restorer;
392
393 err |= __put_user(restorer, &frame->pretcode);
394 err |= __put_user(sig, &frame->sig);
Al Virof67aa2f2011-08-18 20:10:39 +0100395 err |= copy_sc_to_user(&frame->sc, &frame->fpstate, regs, mask->sig[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (_NSIG_WORDS > 1)
397 err |= __copy_to_user(&frame->extramask, &mask->sig[1],
398 sizeof(frame->extramask));
399
400 /*
401 * This is popl %eax ; movl $,%eax ; int $0x80
402 *
403 * WE DO NOT USE IT ANY MORE! It's only left here for historical
404 * reasons and because gdb uses it as a signature to notice
405 * signal handler stack frames.
406 */
407 err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
408 err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
409 err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
410
Jeff Dikeba180fd2007-10-16 01:27:00 -0700411 if (err)
Al Virofbe98682011-08-18 20:10:29 +0100412 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 PT_REGS_SP(regs) = (unsigned long) frame;
415 PT_REGS_IP(regs) = (unsigned long) ka->sa.sa_handler;
Al Viro243412b2012-05-20 00:05:58 -0400416 PT_REGS_AX(regs) = (unsigned long) sig;
417 PT_REGS_DX(regs) = (unsigned long) 0;
418 PT_REGS_CX(regs) = (unsigned long) 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
421 ptrace_notify(SIGTRAP);
Jeff Dike98c18232006-03-27 01:14:38 -0800422 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
425int setup_signal_stack_si(unsigned long stack_top, int sig,
426 struct k_sigaction *ka, struct pt_regs *regs,
427 siginfo_t *info, sigset_t *mask)
428{
429 struct rt_sigframe __user *frame;
Al Viro4d338e12006-03-31 02:30:15 -0800430 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 int err = 0;
432
433 stack_top &= -8UL;
Al Viro4d338e12006-03-31 02:30:15 -0800434 frame = (struct rt_sigframe __user *) stack_top - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
436 return 1;
437
Al Viro4d338e12006-03-31 02:30:15 -0800438 restorer = frame->retcode;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700439 if (ka->sa.sa_flags & SA_RESTORER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 restorer = ka->sa.sa_restorer;
441
442 err |= __put_user(restorer, &frame->pretcode);
443 err |= __put_user(sig, &frame->sig);
444 err |= __put_user(&frame->info, &frame->pinfo);
445 err |= __put_user(&frame->uc, &frame->puc);
446 err |= copy_siginfo_to_user(&frame->info, info);
447 err |= copy_ucontext_to_user(&frame->uc, &frame->fpstate, mask,
Al Virofbe98682011-08-18 20:10:29 +0100448 PT_REGS_SP(regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 /*
451 * This is movl $,%eax ; int $0x80
452 *
453 * WE DO NOT USE IT ANY MORE! It's only left here for historical
454 * reasons and because gdb uses it as a signature to notice
455 * signal handler stack frames.
456 */
457 err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
458 err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
459 err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
460
Jeff Dikeba180fd2007-10-16 01:27:00 -0700461 if (err)
Al Virofbe98682011-08-18 20:10:29 +0100462 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Al Virofbe98682011-08-18 20:10:29 +0100464 PT_REGS_SP(regs) = (unsigned long) frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 PT_REGS_IP(regs) = (unsigned long) ka->sa.sa_handler;
Al Viro243412b2012-05-20 00:05:58 -0400466 PT_REGS_AX(regs) = (unsigned long) sig;
467 PT_REGS_DX(regs) = (unsigned long) &frame->info;
468 PT_REGS_CX(regs) = (unsigned long) &frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 if ((current->ptrace & PT_DTRACE) && (current->ptrace & PT_PTRACED))
471 ptrace_notify(SIGTRAP);
Jeff Dike98c18232006-03-27 01:14:38 -0800472 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Al Virof67aa2f2011-08-18 20:10:39 +0100475long sys_sigreturn(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 unsigned long sp = PT_REGS_SP(&current->thread.regs);
Al Viro4d338e12006-03-31 02:30:15 -0800478 struct sigframe __user *frame = (struct sigframe __user *)(sp - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 sigset_t set;
480 struct sigcontext __user *sc = &frame->sc;
481 unsigned long __user *oldmask = &sc->oldmask;
482 unsigned long __user *extramask = frame->extramask;
483 int sig_size = (_NSIG_WORDS - 1) * sizeof(unsigned long);
484
Jeff Dikeba180fd2007-10-16 01:27:00 -0700485 if (copy_from_user(&set.sig[0], oldmask, sizeof(set.sig[0])) ||
486 copy_from_user(&set.sig[1], extramask, sig_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 goto segfault;
488
489 sigdelsetmask(&set, ~_BLOCKABLE);
Al Viro78722a42011-08-18 20:00:19 +0100490 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Jeff Dikeba180fd2007-10-16 01:27:00 -0700492 if (copy_sc_from_user(&current->thread.regs, sc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 goto segfault;
494
495 /* Avoid ERESTART handling */
496 PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
Jeff Dike5d864562007-05-06 14:51:24 -0700497 return PT_REGS_SYSCALL_RET(&current->thread.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 segfault:
500 force_sig(SIGSEGV, current);
501 return 0;
502}
503
Al Virof67aa2f2011-08-18 20:10:39 +0100504#else
505
506struct rt_sigframe
507{
508 char __user *pretcode;
509 struct ucontext uc;
510 struct siginfo info;
511 struct _fpstate fpstate;
512};
513
514int setup_signal_stack_si(unsigned long stack_top, int sig,
515 struct k_sigaction *ka, struct pt_regs * regs,
516 siginfo_t *info, sigset_t *set)
517{
518 struct rt_sigframe __user *frame;
519 int err = 0;
520 struct task_struct *me = current;
521
522 frame = (struct rt_sigframe __user *)
523 round_down(stack_top - sizeof(struct rt_sigframe), 16);
524 /* Subtract 128 for a red zone and 8 for proper alignment */
525 frame = (struct rt_sigframe __user *) ((unsigned long) frame - 128 - 8);
526
527 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
528 goto out;
529
530 if (ka->sa.sa_flags & SA_SIGINFO) {
531 err |= copy_siginfo_to_user(&frame->info, info);
532 if (err)
533 goto out;
534 }
535
536 /* Create the ucontext. */
537 err |= __put_user(0, &frame->uc.uc_flags);
538 err |= __put_user(0, &frame->uc.uc_link);
539 err |= __put_user(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
540 err |= __put_user(sas_ss_flags(PT_REGS_SP(regs)),
541 &frame->uc.uc_stack.ss_flags);
542 err |= __put_user(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
543 err |= copy_sc_to_user(&frame->uc.uc_mcontext, &frame->fpstate, regs,
544 set->sig[0]);
545 err |= __put_user(&frame->fpstate, &frame->uc.uc_mcontext.fpstate);
546 if (sizeof(*set) == 16) {
547 __put_user(set->sig[0], &frame->uc.uc_sigmask.sig[0]);
548 __put_user(set->sig[1], &frame->uc.uc_sigmask.sig[1]);
549 }
550 else
551 err |= __copy_to_user(&frame->uc.uc_sigmask, set,
552 sizeof(*set));
553
554 /*
555 * Set up to return from userspace. If provided, use a stub
556 * already in userspace.
557 */
558 /* x86-64 should always use SA_RESTORER. */
559 if (ka->sa.sa_flags & SA_RESTORER)
560 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
561 else
562 /* could use a vstub here */
563 return err;
564
565 if (err)
566 return err;
567
568 /* Set up registers for signal handler */
569 {
570 struct exec_domain *ed = current_thread_info()->exec_domain;
571 if (unlikely(ed && ed->signal_invmap && sig < 32))
572 sig = ed->signal_invmap[sig];
573 }
574
575 PT_REGS_SP(regs) = (unsigned long) frame;
Al Viro243412b2012-05-20 00:05:58 -0400576 PT_REGS_DI(regs) = sig;
Al Virof67aa2f2011-08-18 20:10:39 +0100577 /* In case the signal handler was declared without prototypes */
Al Viro243412b2012-05-20 00:05:58 -0400578 PT_REGS_AX(regs) = 0;
Al Virof67aa2f2011-08-18 20:10:39 +0100579
580 /*
581 * This also works for non SA_SIGINFO handlers because they expect the
582 * next argument after the signal number on the stack.
583 */
Al Viro243412b2012-05-20 00:05:58 -0400584 PT_REGS_SI(regs) = (unsigned long) &frame->info;
585 PT_REGS_DX(regs) = (unsigned long) &frame->uc;
586 PT_REGS_IP(regs) = (unsigned long) ka->sa.sa_handler;
Al Virof67aa2f2011-08-18 20:10:39 +0100587 out:
588 return err;
589}
590#endif
591
592long sys_rt_sigreturn(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Al Viro4d338e12006-03-31 02:30:15 -0800594 unsigned long sp = PT_REGS_SP(&current->thread.regs);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700595 struct rt_sigframe __user *frame =
Al Virof67aa2f2011-08-18 20:10:39 +0100596 (struct rt_sigframe __user *)(sp - sizeof(long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 struct ucontext __user *uc = &frame->uc;
Al Virof67aa2f2011-08-18 20:10:39 +0100598 sigset_t set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Al Virof67aa2f2011-08-18 20:10:39 +0100600 if (copy_from_user(&set, &uc->uc_sigmask, sizeof(set)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 goto segfault;
602
603 sigdelsetmask(&set, ~_BLOCKABLE);
Al Viro78722a42011-08-18 20:00:19 +0100604 set_current_blocked(&set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Jeff Dikeba180fd2007-10-16 01:27:00 -0700606 if (copy_sc_from_user(&current->thread.regs, &uc->uc_mcontext))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 goto segfault;
608
609 /* Avoid ERESTART handling */
610 PT_REGS_SYSCALL_NR(&current->thread.regs) = -1;
Jeff Dike5d864562007-05-06 14:51:24 -0700611 return PT_REGS_SYSCALL_RET(&current->thread.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 segfault:
614 force_sig(SIGSEGV, current);
615 return 0;
616}
Al Virof67aa2f2011-08-18 20:10:39 +0100617
618#ifdef CONFIG_X86_32
619long ptregs_sigreturn(void)
620{
621 return sys_sigreturn(NULL);
622}
623long ptregs_rt_sigreturn(void)
624{
625 return sys_rt_sigreturn(NULL);
626}
627#endif