blob: d6bf1f34a6e90b396c11d457dec9600eb6a9af87 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -08003 * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
6 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -08007 * 2000-2002 x86-64 support by Andi Kleen
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
Joe Perchesc767a542012-05-21 19:50:07 -07009
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
Ingo Molnar7e907f42008-03-06 10:33:08 +010012#include <linux/sched.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010013#include <linux/mm.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080014#include <linux/smp.h>
15#include <linux/kernel.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080016#include <linux/errno.h>
17#include <linux/wait.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080018#include <linux/tracehook.h>
19#include <linux/unistd.h>
20#include <linux/stddef.h>
21#include <linux/personality.h>
22#include <linux/uaccess.h>
Avi Kivity7c68af62009-09-19 09:40:22 +030023#include <linux/user-return-notifier.h>
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +053024#include <linux/uprobes.h>
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010025#include <linux/context_tracking.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/processor.h>
28#include <asm/ucontext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/i387.h>
Linus Torvalds1361b832012-02-21 13:19:22 -080030#include <asm/fpu-internal.h>
Roland McGrath6c3652e2008-01-30 13:30:42 +010031#include <asm/vdso.h>
Andi Kleen4efc0672009-04-28 19:07:31 +020032#include <asm/mce.h>
H. Peter Anvinf28f0c22012-02-19 07:38:43 -080033#include <asm/sighandling.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080034
35#ifdef CONFIG_X86_64
36#include <asm/proto.h>
37#include <asm/ia32_unistd.h>
H. Peter Anvinc5a37392012-02-19 09:41:09 -080038#include <asm/sys_ia32.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080039#endif /* CONFIG_X86_64 */
40
Hiroshi Shimamotobb579252008-09-05 16:26:55 -070041#include <asm/syscall.h>
Jaswinder Singhbbc1f692008-07-21 21:34:13 +053042#include <asm/syscalls.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010043
Hiroshi Shimamoto41af86f2008-12-17 18:50:32 -080044#include <asm/sigframe.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Harvey Harrison1a176802008-02-08 12:09:59 -080046#ifdef CONFIG_X86_32
47# define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
48#else
49# define FIX_EFLAGS __FIX_EFLAGS
50#endif
51
Tejun Heod9a89a22009-02-09 22:17:40 +090052#define COPY(x) do { \
53 get_user_ex(regs->x, &sc->x); \
54} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080055
Tejun Heod9a89a22009-02-09 22:17:40 +090056#define GET_SEG(seg) ({ \
57 unsigned short tmp; \
58 get_user_ex(tmp, &sc->seg); \
59 tmp; \
60})
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080061
Tejun Heod9a89a22009-02-09 22:17:40 +090062#define COPY_SEG(seg) do { \
63 regs->seg = GET_SEG(seg); \
64} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080065
Tejun Heod9a89a22009-02-09 22:17:40 +090066#define COPY_SEG_CPL3(seg) do { \
67 regs->seg = GET_SEG(seg) | 3; \
68} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080069
H. Peter Anvin85139422012-02-19 07:43:09 -080070int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
71 unsigned long *pax)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080072{
73 void __user *buf;
74 unsigned int tmpflags;
75 unsigned int err = 0;
76
77 /* Always make any pending restarted system calls return -EINTR */
78 current_thread_info()->restart_block.fn = do_no_restart_syscall;
79
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080080 get_user_try {
81
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080082#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +090083 set_user_gs(regs, GET_SEG(gs));
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080084 COPY_SEG(fs);
85 COPY_SEG(es);
86 COPY_SEG(ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080087#endif /* CONFIG_X86_32 */
88
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080089 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
90 COPY(dx); COPY(cx); COPY(ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080091
92#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080093 COPY(r8);
94 COPY(r9);
95 COPY(r10);
96 COPY(r11);
97 COPY(r12);
98 COPY(r13);
99 COPY(r14);
100 COPY(r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800101#endif /* CONFIG_X86_64 */
102
103#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800104 COPY_SEG_CPL3(cs);
105 COPY_SEG_CPL3(ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800106#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800107 /* Kernel saves and restores only the CS segment register on signals,
108 * which is the bare minimum needed to allow mixed 32/64-bit code.
109 * App's signal handler can save/restore other segments if needed. */
110 COPY_SEG_CPL3(cs);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800111#endif /* CONFIG_X86_32 */
112
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800113 get_user_ex(tmpflags, &sc->flags);
114 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
115 regs->orig_ax = -1; /* disable syscall checks */
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800116
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800117 get_user_ex(buf, &sc->fpstate);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800118
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800119 get_user_ex(*pax, &sc->ax);
120 } get_user_catch(err);
121
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700122 err |= restore_xstate_sig(buf, config_enabled(CONFIG_X86_32));
H. Peter Anvin5e883532012-09-21 12:43:15 -0700123
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800124 return err;
125}
126
H. Peter Anvin85139422012-02-19 07:43:09 -0800127int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
128 struct pt_regs *regs, unsigned long mask)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800129{
130 int err = 0;
131
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800132 put_user_try {
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800133
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800134#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +0900135 put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800136 put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
137 put_user_ex(regs->es, (unsigned int __user *)&sc->es);
138 put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800139#endif /* CONFIG_X86_32 */
140
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800141 put_user_ex(regs->di, &sc->di);
142 put_user_ex(regs->si, &sc->si);
143 put_user_ex(regs->bp, &sc->bp);
144 put_user_ex(regs->sp, &sc->sp);
145 put_user_ex(regs->bx, &sc->bx);
146 put_user_ex(regs->dx, &sc->dx);
147 put_user_ex(regs->cx, &sc->cx);
148 put_user_ex(regs->ax, &sc->ax);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800149#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800150 put_user_ex(regs->r8, &sc->r8);
151 put_user_ex(regs->r9, &sc->r9);
152 put_user_ex(regs->r10, &sc->r10);
153 put_user_ex(regs->r11, &sc->r11);
154 put_user_ex(regs->r12, &sc->r12);
155 put_user_ex(regs->r13, &sc->r13);
156 put_user_ex(regs->r14, &sc->r14);
157 put_user_ex(regs->r15, &sc->r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800158#endif /* CONFIG_X86_64 */
159
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530160 put_user_ex(current->thread.trap_nr, &sc->trapno);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800161 put_user_ex(current->thread.error_code, &sc->err);
162 put_user_ex(regs->ip, &sc->ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800163#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800164 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
165 put_user_ex(regs->flags, &sc->flags);
166 put_user_ex(regs->sp, &sc->sp_at_signal);
167 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800168#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800169 put_user_ex(regs->flags, &sc->flags);
170 put_user_ex(regs->cs, &sc->cs);
171 put_user_ex(0, &sc->gs);
172 put_user_ex(0, &sc->fs);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800173#endif /* CONFIG_X86_32 */
174
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800175 put_user_ex(fpstate, &sc->fpstate);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800176
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800177 /* non-iBCS2 extensions.. */
178 put_user_ex(mask, &sc->oldmask);
179 put_user_ex(current->thread.cr2, &sc->cr2);
180 } put_user_catch(err);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800181
182 return err;
183}
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 * Set up a signal frame.
187 */
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800188
189/*
190 * Determine which stack to use..
191 */
Hiroshi Shimamoto1fae0272009-02-27 10:30:32 -0800192static unsigned long align_sigframe(unsigned long sp)
193{
194#ifdef CONFIG_X86_32
195 /*
196 * Align the stack pointer according to the i386 ABI,
197 * i.e. so that on function entry ((sp + 4) & 15) == 0.
198 */
199 sp = ((sp + 4) & -16ul) - 4;
200#else /* !CONFIG_X86_32 */
201 sp = round_down(sp, 16) - 8;
202#endif
203 return sp;
204}
205
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800206static inline void __user *
207get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
208 void __user **fpstate)
209{
210 /* Default to using normal stack */
Suresh Siddha72a671c2012-07-24 16:05:29 -0700211 unsigned long math_size = 0;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800212 unsigned long sp = regs->sp;
Suresh Siddha72a671c2012-07-24 16:05:29 -0700213 unsigned long buf_fx = 0;
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700214 int onsigstack = on_sig_stack(sp);
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800215
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800216 /* redzone */
Suresh Siddha050902c2012-07-24 16:05:27 -0700217 if (config_enabled(CONFIG_X86_64))
218 sp -= 128;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800219
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700220 if (!onsigstack) {
221 /* This is the X/Open sanctioned signal stack switching. */
222 if (ka->sa.sa_flags & SA_ONSTACK) {
Hiroshi Shimamoto0f8f3082009-03-26 10:03:08 -0700223 if (current->sas_ss_size)
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700224 sp = current->sas_ss_sp + current->sas_ss_size;
Suresh Siddha050902c2012-07-24 16:05:27 -0700225 } else if (config_enabled(CONFIG_X86_32) &&
226 (regs->ss & 0xffff) != __USER_DS &&
227 !(ka->sa.sa_flags & SA_RESTORER) &&
228 ka->sa.sa_restorer) {
229 /* This is the legacy signal stack switching. */
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700230 sp = (unsigned long) ka->sa.sa_restorer;
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700231 }
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800232 }
233
234 if (used_math()) {
Suresh Siddha72a671c2012-07-24 16:05:29 -0700235 sp = alloc_mathframe(sp, config_enabled(CONFIG_X86_32),
236 &buf_fx, &math_size);
Hiroshi Shimamoto25051702009-03-02 17:20:01 -0800237 *fpstate = (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800238 }
239
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700240 sp = align_sigframe(sp - frame_size);
241
242 /*
243 * If we are on the alternate signal stack and would overflow it, don't.
244 * Return an always-bogus address instead so we will die with SIGSEGV.
245 */
246 if (onsigstack && !likely(on_sig_stack(sp)))
247 return (void __user *)-1L;
248
Suresh Siddha72a671c2012-07-24 16:05:29 -0700249 /* save i387 and extended state */
250 if (used_math() &&
251 save_xstate_sig(*fpstate, (void __user *)buf_fx, math_size) < 0)
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700252 return (void __user *)-1L;
253
254 return (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800255}
256
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800257#ifdef CONFIG_X86_32
258static const struct {
259 u16 poplmovl;
260 u32 val;
261 u16 int80;
262} __attribute__((packed)) retcode = {
263 0xb858, /* popl %eax; movl $..., %eax */
264 __NR_sigreturn,
265 0x80cd, /* int $0x80 */
266};
267
268static const struct {
269 u8 movl;
270 u32 val;
271 u16 int80;
272 u8 pad;
273} __attribute__((packed)) rt_retcode = {
274 0xb8, /* movl $..., %eax */
275 __NR_rt_sigreturn,
276 0x80cd, /* int $0x80 */
277 0
278};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Ingo Molnar7e907f42008-03-06 10:33:08 +0100280static int
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700281__setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
282 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100285 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700287 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700289 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700292 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700294 if (__put_user(sig, &frame->sig))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700295 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700297 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700298 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 if (_NSIG_WORDS > 1) {
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700301 if (__copy_to_user(&frame->extramask, &set->sig[1],
302 sizeof(frame->extramask)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700303 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700306 if (current->mm->context.vdso)
Roland McGrath6c3652e2008-01-30 13:30:42 +0100307 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100308 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100309 restorer = &frame->retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (ka->sa.sa_flags & SA_RESTORER)
311 restorer = ka->sa.sa_restorer;
312
313 /* Set up to return from userspace. */
314 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100317 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 *
319 * WE DO NOT USE IT ANY MORE! It's only left here for historical
320 * reasons and because gdb uses it as a signature to notice
321 * signal handler stack frames.
322 */
Hiroshi Shimamoto4a612042008-11-11 19:09:29 -0800323 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700326 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100329 regs->sp = (unsigned long)frame;
330 regs->ip = (unsigned long)ka->sa.sa_handler;
331 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800332 regs->dx = 0;
333 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100335 regs->ds = __USER_DS;
336 regs->es = __USER_DS;
337 regs->ss = __USER_DS;
338 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
David Howells283828f2006-01-18 17:44:00 -0800340 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700343static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
344 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100347 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700349 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700351 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700354 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800356 put_user_try {
357 put_user_ex(sig, &frame->sig);
358 put_user_ex(&frame->info, &frame->pinfo);
359 put_user_ex(&frame->uc, &frame->puc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800361 /* Create the ucontext. */
362 if (cpu_has_xsave)
363 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
364 else
365 put_user_ex(0, &frame->uc.uc_flags);
366 put_user_ex(0, &frame->uc.uc_link);
Al Viroc40702c2012-11-20 14:24:26 -0500367 err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800369 /* Set up to return from userspace. */
370 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
371 if (ka->sa.sa_flags & SA_RESTORER)
372 restorer = ka->sa.sa_restorer;
373 put_user_ex(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100374
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800375 /*
376 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
377 *
378 * WE DO NOT USE IT ANY MORE! It's only left here for historical
379 * reasons and because gdb uses it as a signature to notice
380 * signal handler stack frames.
381 */
382 put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
383 } put_user_catch(err);
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700384
H. Peter Anvin5e883532012-09-21 12:43:15 -0700385 err |= copy_siginfo_to_user(&frame->info, info);
386 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
387 regs, set->sig[0]);
388 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700391 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100394 regs->sp = (unsigned long)frame;
395 regs->ip = (unsigned long)ka->sa.sa_handler;
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700396 regs->ax = (unsigned long)sig;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100397 regs->dx = (unsigned long)&frame->info;
398 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100400 regs->ds = __USER_DS;
401 regs->es = __USER_DS;
402 regs->ss = __USER_DS;
403 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
David Howells283828f2006-01-18 17:44:00 -0800405 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800407#else /* !CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800408static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
409 sigset_t *set, struct pt_regs *regs)
410{
411 struct rt_sigframe __user *frame;
412 void __user *fp = NULL;
413 int err = 0;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800414
Hiroshi Shimamoto97286a22009-02-27 10:28:48 -0800415 frame = get_sigframe(ka, regs, sizeof(struct rt_sigframe), &fp);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800416
417 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
418 return -EFAULT;
419
420 if (ka->sa.sa_flags & SA_SIGINFO) {
421 if (copy_siginfo_to_user(&frame->info, info))
422 return -EFAULT;
423 }
424
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800425 put_user_try {
426 /* Create the ucontext. */
427 if (cpu_has_xsave)
428 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
429 else
430 put_user_ex(0, &frame->uc.uc_flags);
431 put_user_ex(0, &frame->uc.uc_link);
Al Viroc40702c2012-11-20 14:24:26 -0500432 err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800433
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800434 /* Set up to return from userspace. If provided, use a stub
435 already in userspace. */
436 /* x86-64 should always use SA_RESTORER. */
437 if (ka->sa.sa_flags & SA_RESTORER) {
438 put_user_ex(ka->sa.sa_restorer, &frame->pretcode);
439 } else {
440 /* could use a vstub here */
441 err |= -EFAULT;
442 }
443 } put_user_catch(err);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800444
H. Peter Anvin5e883532012-09-21 12:43:15 -0700445 err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
446 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
447
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800448 if (err)
449 return -EFAULT;
450
451 /* Set up registers for signal handler */
452 regs->di = sig;
453 /* In case the signal handler was declared without prototypes */
454 regs->ax = 0;
455
456 /* This also works for non SA_SIGINFO handlers because they expect the
457 next argument after the signal number on the stack. */
458 regs->si = (unsigned long)&frame->info;
459 regs->dx = (unsigned long)&frame->uc;
460 regs->ip = (unsigned long) ka->sa.sa_handler;
461
462 regs->sp = (unsigned long)frame;
463
464 /* Set up the CS register to run signal handlers in 64-bit mode,
465 even if the handler happens to be interrupting 32-bit code. */
466 regs->cs = __USER_CS;
467
468 return 0;
469}
470#endif /* CONFIG_X86_32 */
471
Suresh Siddha050902c2012-07-24 16:05:27 -0700472static int x32_setup_rt_frame(int sig, struct k_sigaction *ka,
473 siginfo_t *info, compat_sigset_t *set,
474 struct pt_regs *regs)
475{
476#ifdef CONFIG_X86_X32_ABI
477 struct rt_sigframe_x32 __user *frame;
478 void __user *restorer;
479 int err = 0;
480 void __user *fpstate = NULL;
481
482 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
483
484 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
485 return -EFAULT;
486
487 if (ka->sa.sa_flags & SA_SIGINFO) {
488 if (copy_siginfo_to_user32(&frame->info, info))
489 return -EFAULT;
490 }
491
492 put_user_try {
493 /* Create the ucontext. */
494 if (cpu_has_xsave)
495 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
496 else
497 put_user_ex(0, &frame->uc.uc_flags);
498 put_user_ex(0, &frame->uc.uc_link);
Al Viroc40702c2012-11-20 14:24:26 -0500499 err |= __compat_save_altstack(&frame->uc.uc_stack, regs->sp);
Suresh Siddha050902c2012-07-24 16:05:27 -0700500 put_user_ex(0, &frame->uc.uc__pad0);
Suresh Siddha050902c2012-07-24 16:05:27 -0700501
502 if (ka->sa.sa_flags & SA_RESTORER) {
503 restorer = ka->sa.sa_restorer;
504 } else {
505 /* could use a vstub here */
506 restorer = NULL;
507 err |= -EFAULT;
508 }
509 put_user_ex(restorer, &frame->pretcode);
510 } put_user_catch(err);
511
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700512 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
513 regs, set->sig[0]);
514 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
515
Suresh Siddha050902c2012-07-24 16:05:27 -0700516 if (err)
517 return -EFAULT;
518
519 /* Set up registers for signal handler */
520 regs->sp = (unsigned long) frame;
521 regs->ip = (unsigned long) ka->sa.sa_handler;
522
523 /* We use the x32 calling convention here... */
524 regs->di = sig;
525 regs->si = (unsigned long) &frame->info;
526 regs->dx = (unsigned long) &frame->uc;
527
528 loadsegment(ds, __USER_DS);
529 loadsegment(es, __USER_DS);
530
531 regs->cs = __USER_CS;
532 regs->ss = __USER_DS;
533#endif /* CONFIG_X86_X32_ABI */
534
535 return 0;
536}
537
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800538#ifdef CONFIG_X86_32
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800539/*
540 * Atomically swap in the new signal mask, and wait for a signal.
541 */
542asmlinkage int
543sys_sigsuspend(int history0, int history1, old_sigset_t mask)
544{
Oleg Nesterov39822942011-07-10 21:27:27 +0200545 sigset_t blocked;
Oleg Nesterov39822942011-07-10 21:27:27 +0200546 siginitset(&blocked, mask);
Al Viro68f3f162012-05-21 21:42:32 -0400547 return sigsuspend(&blocked);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800548}
549
550asmlinkage int
551sys_sigaction(int sig, const struct old_sigaction __user *act,
552 struct old_sigaction __user *oact)
553{
554 struct k_sigaction new_ka, old_ka;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800555 int ret = 0;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800556
557 if (act) {
558 old_sigset_t mask;
559
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800560 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800561 return -EFAULT;
562
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800563 get_user_try {
564 get_user_ex(new_ka.sa.sa_handler, &act->sa_handler);
565 get_user_ex(new_ka.sa.sa_flags, &act->sa_flags);
566 get_user_ex(mask, &act->sa_mask);
567 get_user_ex(new_ka.sa.sa_restorer, &act->sa_restorer);
568 } get_user_catch(ret);
569
570 if (ret)
571 return -EFAULT;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800572 siginitset(&new_ka.sa.sa_mask, mask);
573 }
574
575 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
576
577 if (!ret && oact) {
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800578 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800579 return -EFAULT;
580
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800581 put_user_try {
582 put_user_ex(old_ka.sa.sa_handler, &oact->sa_handler);
583 put_user_ex(old_ka.sa.sa_flags, &oact->sa_flags);
584 put_user_ex(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
585 put_user_ex(old_ka.sa.sa_restorer, &oact->sa_restorer);
586 } put_user_catch(ret);
587
588 if (ret)
589 return -EFAULT;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800590 }
591
592 return ret;
593}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800594#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800595
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800596/*
597 * Do a signal return; undo the signal stack.
598 */
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800599#ifdef CONFIG_X86_32
Brian Gerstb12bdaf2009-02-11 16:43:58 -0500600unsigned long sys_sigreturn(struct pt_regs *regs)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800601{
602 struct sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800603 unsigned long ax;
604 sigset_t set;
605
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800606 frame = (struct sigframe __user *)(regs->sp - 8);
607
608 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
609 goto badframe;
610 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
611 && __copy_from_user(&set.sig[1], &frame->extramask,
612 sizeof(frame->extramask))))
613 goto badframe;
614
Oleg Nesterov39822942011-07-10 21:27:27 +0200615 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800616
617 if (restore_sigcontext(regs, &frame->sc, &ax))
618 goto badframe;
619 return ax;
620
621badframe:
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800622 signal_fault(regs, frame, "sigreturn");
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800623
624 return 0;
625}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800626#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800627
H. Peter Anvin74452502009-02-11 16:31:40 -0800628long sys_rt_sigreturn(struct pt_regs *regs)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800629{
630 struct rt_sigframe __user *frame;
631 unsigned long ax;
632 sigset_t set;
633
634 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
635 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
636 goto badframe;
637 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
638 goto badframe;
639
Oleg Nesterove9bd3f02011-04-27 21:09:39 +0200640 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800641
642 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
643 goto badframe;
644
Al Viroc40702c2012-11-20 14:24:26 -0500645 if (restore_altstack(&frame->uc.uc_stack))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800646 goto badframe;
647
648 return ax;
649
650badframe:
651 signal_fault(regs, frame, "rt_sigreturn");
652 return 0;
653}
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655/*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100656 * OK, we're invoking a handler:
657 */
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700658static int signr_convert(int sig)
659{
Hiroshi Shimamoto96bf84b2008-10-29 18:44:08 -0700660#ifdef CONFIG_X86_32
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700661 struct thread_info *info = current_thread_info();
662
663 if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
664 return info->exec_domain->signal_invmap[sig];
Hiroshi Shimamoto96bf84b2008-10-29 18:44:08 -0700665#endif /* CONFIG_X86_32 */
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700666 return sig;
667}
668
Roland McGrath7c1def12005-06-23 00:08:21 -0700669static int
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700670setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
Oleg Nesterov9b429622011-07-10 20:22:03 +0200671 struct pt_regs *regs)
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700672{
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700673 int usig = signr_convert(sig);
Al Virob7f9a112012-05-02 09:59:21 -0400674 sigset_t *set = sigmask_to_save();
Suresh Siddha050902c2012-07-24 16:05:27 -0700675 compat_sigset_t *cset = (compat_sigset_t *) set;
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700676
677 /* Set up the stack frame */
Suresh Siddha050902c2012-07-24 16:05:27 -0700678 if (is_ia32_frame()) {
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700679 if (ka->sa.sa_flags & SA_SIGINFO)
Suresh Siddha050902c2012-07-24 16:05:27 -0700680 return ia32_setup_rt_frame(usig, ka, info, cset, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700681 else
Suresh Siddha050902c2012-07-24 16:05:27 -0700682 return ia32_setup_frame(usig, ka, cset, regs);
683 } else if (is_x32_frame()) {
684 return x32_setup_rt_frame(usig, ka, info, cset, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800685 } else {
Al Viroa610d6e2012-05-21 23:42:15 -0400686 return __setup_rt_frame(sig, ka, info, set, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800687 }
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700688}
689
Al Viroa610d6e2012-05-21 23:42:15 -0400690static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
Oleg Nesterov9b429622011-07-10 20:22:03 +0200692 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 /* Are we from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700695 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 /* If so, check system call restarting.. */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700697 switch (syscall_get_error(current, regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800698 case -ERESTART_RESTARTBLOCK:
699 case -ERESTARTNOHAND:
700 regs->ax = -EINTR;
701 break;
702
703 case -ERESTARTSYS:
704 if (!(ka->sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100705 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800707 }
708 /* fallthrough */
709 case -ERESTARTNOINTR:
710 regs->ax = regs->orig_ax;
711 regs->ip -= 2;
712 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
714 }
715
716 /*
Roland McGrathe1f28772008-01-30 13:30:50 +0100717 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
718 * flag so that register information in the sigcontext is correct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100720 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
Roland McGrathe1f28772008-01-30 13:30:50 +0100721 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100722 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Al Viroa610d6e2012-05-21 23:42:15 -0400724 if (setup_rt_frame(sig, ka, info, regs) < 0) {
725 force_sigsegv(sig, current);
726 return;
727 }
Roland McGrath7c1def12005-06-23 00:08:21 -0700728
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700729 /*
730 * Clear the direction flag as per the ABI for function entry.
731 */
732 regs->flags &= ~X86_EFLAGS_DF;
733
734 /*
735 * Clear TF when entering the signal handler, but
736 * notify any tracer that was single-stepping it.
737 * The tracer may want to single-step inside the
738 * handler too.
739 */
740 regs->flags &= ~X86_EFLAGS_TF;
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700741
Al Viroefee9842012-04-28 02:04:15 -0400742 signal_delivered(sig, info, ka, regs,
743 test_thread_flag(TIF_SINGLESTEP));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700746#ifdef CONFIG_X86_32
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700747#define NR_restart_syscall __NR_restart_syscall
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700748#else /* !CONFIG_X86_32 */
749#define NR_restart_syscall \
750 test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
751#endif /* CONFIG_X86_32 */
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753/*
754 * Note that 'init' is a special process: it doesn't get signals it doesn't
755 * want to handle. Thus you cannot kill init even with a SIGKILL even by
756 * mistake.
757 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100758static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800760 struct k_sigaction ka;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 siginfo_t info;
762 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
765 if (signr > 0) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100766 /* Whee! Actually deliver the signal. */
Oleg Nesterov9b429622011-07-10 20:22:03 +0200767 handle_signal(signr, &info, &ka, regs);
David Howells283828f2006-01-18 17:44:00 -0800768 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 /* Did we come from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700772 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 /* Restart the system call - no handlers present */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700774 switch (syscall_get_error(current, regs)) {
David Howells283828f2006-01-18 17:44:00 -0800775 case -ERESTARTNOHAND:
776 case -ERESTARTSYS:
777 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100778 regs->ax = regs->orig_ax;
779 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800780 break;
781
782 case -ERESTART_RESTARTBLOCK:
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700783 regs->ax = NR_restart_syscall;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100784 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800785 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787 }
David Howells283828f2006-01-18 17:44:00 -0800788
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800789 /*
790 * If there's no signal to deliver, we just put the saved sigmask
791 * back.
792 */
Al Viro51a7b442012-05-21 23:33:55 -0400793 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
796/*
797 * notification of userspace execution resumption
David Howells283828f2006-01-18 17:44:00 -0800798 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100800void
801do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +0100803 user_exit();
Frederic Weisbeckeredf55fd2012-07-11 20:26:39 +0200804
Andi Kleenc1ebf832009-07-09 00:31:41 +0200805#ifdef CONFIG_X86_MCE
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700806 /* notify userspace of pending MCEs */
807 if (thread_info_flags & _TIF_MCE_NOTIFY)
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200808 mce_notify_process();
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700809#endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
810
Oleg Nesterovdb023ea2012-09-14 19:05:46 +0200811 if (thread_info_flags & _TIF_UPROBE)
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530812 uprobe_notify_resume(regs);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 /* deal with pending signal delivery */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700815 if (thread_info_flags & _TIF_SIGPENDING)
David Howells283828f2006-01-18 17:44:00 -0800816 do_signal(regs);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100817
Roland McGrath59e52132008-04-19 19:10:57 -0700818 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
819 clear_thread_flag(TIF_NOTIFY_RESUME);
820 tracehook_notify_resume(regs);
821 }
Avi Kivity7c68af62009-09-19 09:40:22 +0300822 if (thread_info_flags & _TIF_USER_RETURN_NOTIFY)
823 fire_user_return_notifiers();
Roland McGrath59e52132008-04-19 19:10:57 -0700824
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +0100825 user_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700827
828void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
829{
830 struct task_struct *me = current;
831
832 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800833 printk("%s"
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700834 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800835 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700836 me->comm, me->pid, where, frame,
837 regs->ip, regs->sp, regs->orig_ax);
838 print_vma_addr(" in ", regs->ip);
Joe Perchesc767a542012-05-21 19:50:07 -0700839 pr_cont("\n");
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700840 }
841
842 force_sig(SIGSEGV, me);
843}
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800844
845#ifdef CONFIG_X86_X32_ABI
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800846asmlinkage long sys32_x32_rt_sigreturn(struct pt_regs *regs)
847{
848 struct rt_sigframe_x32 __user *frame;
849 sigset_t set;
850 unsigned long ax;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800851
852 frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
853
854 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
855 goto badframe;
856 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
857 goto badframe;
858
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800859 set_current_blocked(&set);
860
861 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
862 goto badframe;
863
Al Viro90268432012-12-14 14:47:53 -0500864 if (compat_restore_altstack(&frame->uc.uc_stack))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800865 goto badframe;
866
867 return ax;
868
869badframe:
870 signal_fault(regs, frame, "x32 rt_sigreturn");
871 return 0;
872}
873#endif