blob: 56f3062c5111aea4708ed7ebddafabeaefb650b5 [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 */
Ingo Molnar7e907f42008-03-06 10:33:08 +01009#include <linux/sched.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010010#include <linux/mm.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080011#include <linux/smp.h>
12#include <linux/kernel.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080013#include <linux/errno.h>
14#include <linux/wait.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080015#include <linux/tracehook.h>
16#include <linux/unistd.h>
17#include <linux/stddef.h>
18#include <linux/personality.h>
19#include <linux/uaccess.h>
Avi Kivity7c68af62009-09-19 09:40:22 +030020#include <linux/user-return-notifier.h>
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +053021#include <linux/uprobes.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/processor.h>
24#include <asm/ucontext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/i387.h>
Linus Torvalds1361b832012-02-21 13:19:22 -080026#include <asm/fpu-internal.h>
Roland McGrath6c3652e2008-01-30 13:30:42 +010027#include <asm/vdso.h>
Andi Kleen4efc0672009-04-28 19:07:31 +020028#include <asm/mce.h>
H. Peter Anvinf28f0c22012-02-19 07:38:43 -080029#include <asm/sighandling.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080030
31#ifdef CONFIG_X86_64
32#include <asm/proto.h>
33#include <asm/ia32_unistd.h>
H. Peter Anvinc5a37392012-02-19 09:41:09 -080034#include <asm/sys_ia32.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080035#endif /* CONFIG_X86_64 */
36
Hiroshi Shimamotobb579252008-09-05 16:26:55 -070037#include <asm/syscall.h>
Jaswinder Singhbbc1f692008-07-21 21:34:13 +053038#include <asm/syscalls.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010039
Hiroshi Shimamoto41af86f2008-12-17 18:50:32 -080040#include <asm/sigframe.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Harvey Harrison1a176802008-02-08 12:09:59 -080042#ifdef CONFIG_X86_32
43# define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
44#else
45# define FIX_EFLAGS __FIX_EFLAGS
46#endif
47
Tejun Heod9a89a22009-02-09 22:17:40 +090048#define COPY(x) do { \
49 get_user_ex(regs->x, &sc->x); \
50} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080051
Tejun Heod9a89a22009-02-09 22:17:40 +090052#define GET_SEG(seg) ({ \
53 unsigned short tmp; \
54 get_user_ex(tmp, &sc->seg); \
55 tmp; \
56})
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080057
Tejun Heod9a89a22009-02-09 22:17:40 +090058#define COPY_SEG(seg) do { \
59 regs->seg = GET_SEG(seg); \
60} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080061
Tejun Heod9a89a22009-02-09 22:17:40 +090062#define COPY_SEG_CPL3(seg) do { \
63 regs->seg = GET_SEG(seg) | 3; \
64} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080065
H. Peter Anvin85139422012-02-19 07:43:09 -080066int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
67 unsigned long *pax)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080068{
69 void __user *buf;
70 unsigned int tmpflags;
71 unsigned int err = 0;
72
73 /* Always make any pending restarted system calls return -EINTR */
74 current_thread_info()->restart_block.fn = do_no_restart_syscall;
75
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080076 get_user_try {
77
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080078#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +090079 set_user_gs(regs, GET_SEG(gs));
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080080 COPY_SEG(fs);
81 COPY_SEG(es);
82 COPY_SEG(ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080083#endif /* CONFIG_X86_32 */
84
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080085 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
86 COPY(dx); COPY(cx); COPY(ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080087
88#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080089 COPY(r8);
90 COPY(r9);
91 COPY(r10);
92 COPY(r11);
93 COPY(r12);
94 COPY(r13);
95 COPY(r14);
96 COPY(r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080097#endif /* CONFIG_X86_64 */
98
99#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800100 COPY_SEG_CPL3(cs);
101 COPY_SEG_CPL3(ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800102#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800103 /* Kernel saves and restores only the CS segment register on signals,
104 * which is the bare minimum needed to allow mixed 32/64-bit code.
105 * App's signal handler can save/restore other segments if needed. */
106 COPY_SEG_CPL3(cs);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800107#endif /* CONFIG_X86_32 */
108
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800109 get_user_ex(tmpflags, &sc->flags);
110 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
111 regs->orig_ax = -1; /* disable syscall checks */
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800112
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800113 get_user_ex(buf, &sc->fpstate);
114 err |= restore_i387_xstate(buf);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800115
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800116 get_user_ex(*pax, &sc->ax);
117 } get_user_catch(err);
118
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800119 return err;
120}
121
H. Peter Anvin85139422012-02-19 07:43:09 -0800122int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
123 struct pt_regs *regs, unsigned long mask)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800124{
125 int err = 0;
126
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800127 put_user_try {
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800128
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800129#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +0900130 put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800131 put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
132 put_user_ex(regs->es, (unsigned int __user *)&sc->es);
133 put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800134#endif /* CONFIG_X86_32 */
135
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800136 put_user_ex(regs->di, &sc->di);
137 put_user_ex(regs->si, &sc->si);
138 put_user_ex(regs->bp, &sc->bp);
139 put_user_ex(regs->sp, &sc->sp);
140 put_user_ex(regs->bx, &sc->bx);
141 put_user_ex(regs->dx, &sc->dx);
142 put_user_ex(regs->cx, &sc->cx);
143 put_user_ex(regs->ax, &sc->ax);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800144#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800145 put_user_ex(regs->r8, &sc->r8);
146 put_user_ex(regs->r9, &sc->r9);
147 put_user_ex(regs->r10, &sc->r10);
148 put_user_ex(regs->r11, &sc->r11);
149 put_user_ex(regs->r12, &sc->r12);
150 put_user_ex(regs->r13, &sc->r13);
151 put_user_ex(regs->r14, &sc->r14);
152 put_user_ex(regs->r15, &sc->r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800153#endif /* CONFIG_X86_64 */
154
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530155 put_user_ex(current->thread.trap_nr, &sc->trapno);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800156 put_user_ex(current->thread.error_code, &sc->err);
157 put_user_ex(regs->ip, &sc->ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800158#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800159 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
160 put_user_ex(regs->flags, &sc->flags);
161 put_user_ex(regs->sp, &sc->sp_at_signal);
162 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800163#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800164 put_user_ex(regs->flags, &sc->flags);
165 put_user_ex(regs->cs, &sc->cs);
166 put_user_ex(0, &sc->gs);
167 put_user_ex(0, &sc->fs);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800168#endif /* CONFIG_X86_32 */
169
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800170 put_user_ex(fpstate, &sc->fpstate);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800171
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800172 /* non-iBCS2 extensions.. */
173 put_user_ex(mask, &sc->oldmask);
174 put_user_ex(current->thread.cr2, &sc->cr2);
175 } put_user_catch(err);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800176
177 return err;
178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 * Set up a signal frame.
182 */
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800183
184/*
185 * Determine which stack to use..
186 */
Hiroshi Shimamoto1fae0272009-02-27 10:30:32 -0800187static unsigned long align_sigframe(unsigned long sp)
188{
189#ifdef CONFIG_X86_32
190 /*
191 * Align the stack pointer according to the i386 ABI,
192 * i.e. so that on function entry ((sp + 4) & 15) == 0.
193 */
194 sp = ((sp + 4) & -16ul) - 4;
195#else /* !CONFIG_X86_32 */
196 sp = round_down(sp, 16) - 8;
197#endif
198 return sp;
199}
200
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800201static inline void __user *
202get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
203 void __user **fpstate)
204{
205 /* Default to using normal stack */
206 unsigned long sp = regs->sp;
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700207 int onsigstack = on_sig_stack(sp);
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800208
209#ifdef CONFIG_X86_64
210 /* redzone */
211 sp -= 128;
212#endif /* CONFIG_X86_64 */
213
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700214 if (!onsigstack) {
215 /* This is the X/Open sanctioned signal stack switching. */
216 if (ka->sa.sa_flags & SA_ONSTACK) {
Hiroshi Shimamoto0f8f3082009-03-26 10:03:08 -0700217 if (current->sas_ss_size)
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700218 sp = current->sas_ss_sp + current->sas_ss_size;
219 } else {
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800220#ifdef CONFIG_X86_32
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700221 /* This is the legacy signal stack switching. */
222 if ((regs->ss & 0xffff) != __USER_DS &&
223 !(ka->sa.sa_flags & SA_RESTORER) &&
224 ka->sa.sa_restorer)
225 sp = (unsigned long) ka->sa.sa_restorer;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800226#endif /* CONFIG_X86_32 */
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700227 }
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800228 }
229
230 if (used_math()) {
231 sp -= sig_xstate_size;
Hiroshi Shimamoto25051702009-03-02 17:20:01 -0800232#ifdef CONFIG_X86_64
233 sp = round_down(sp, 64);
234#endif /* CONFIG_X86_64 */
235 *fpstate = (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800236 }
237
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700238 sp = align_sigframe(sp - frame_size);
239
240 /*
241 * If we are on the alternate signal stack and would overflow it, don't.
242 * Return an always-bogus address instead so we will die with SIGSEGV.
243 */
244 if (onsigstack && !likely(on_sig_stack(sp)))
245 return (void __user *)-1L;
246
247 /* save i387 state */
248 if (used_math() && save_i387_xstate(*fpstate) < 0)
249 return (void __user *)-1L;
250
251 return (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800252}
253
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800254#ifdef CONFIG_X86_32
255static const struct {
256 u16 poplmovl;
257 u32 val;
258 u16 int80;
259} __attribute__((packed)) retcode = {
260 0xb858, /* popl %eax; movl $..., %eax */
261 __NR_sigreturn,
262 0x80cd, /* int $0x80 */
263};
264
265static const struct {
266 u8 movl;
267 u32 val;
268 u16 int80;
269 u8 pad;
270} __attribute__((packed)) rt_retcode = {
271 0xb8, /* movl $..., %eax */
272 __NR_rt_sigreturn,
273 0x80cd, /* int $0x80 */
274 0
275};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Ingo Molnar7e907f42008-03-06 10:33:08 +0100277static int
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700278__setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
279 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100282 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700284 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700286 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700289 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700291 if (__put_user(sig, &frame->sig))
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 (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700295 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 if (_NSIG_WORDS > 1) {
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700298 if (__copy_to_user(&frame->extramask, &set->sig[1],
299 sizeof(frame->extramask)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700300 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700303 if (current->mm->context.vdso)
Roland McGrath6c3652e2008-01-30 13:30:42 +0100304 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100305 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100306 restorer = &frame->retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (ka->sa.sa_flags & SA_RESTORER)
308 restorer = ka->sa.sa_restorer;
309
310 /* Set up to return from userspace. */
311 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100314 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 *
316 * WE DO NOT USE IT ANY MORE! It's only left here for historical
317 * reasons and because gdb uses it as a signature to notice
318 * signal handler stack frames.
319 */
Hiroshi Shimamoto4a612042008-11-11 19:09:29 -0800320 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700323 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100326 regs->sp = (unsigned long)frame;
327 regs->ip = (unsigned long)ka->sa.sa_handler;
328 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800329 regs->dx = 0;
330 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100332 regs->ds = __USER_DS;
333 regs->es = __USER_DS;
334 regs->ss = __USER_DS;
335 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
David Howells283828f2006-01-18 17:44:00 -0800337 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700340static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
341 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100344 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700346 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700348 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700351 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800353 put_user_try {
354 put_user_ex(sig, &frame->sig);
355 put_user_ex(&frame->info, &frame->pinfo);
356 put_user_ex(&frame->uc, &frame->puc);
357 err |= copy_siginfo_to_user(&frame->info, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800359 /* Create the ucontext. */
360 if (cpu_has_xsave)
361 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
362 else
363 put_user_ex(0, &frame->uc.uc_flags);
364 put_user_ex(0, &frame->uc.uc_link);
365 put_user_ex(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
366 put_user_ex(sas_ss_flags(regs->sp),
367 &frame->uc.uc_stack.ss_flags);
368 put_user_ex(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
369 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
370 regs, set->sig[0]);
371 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800373 /* Set up to return from userspace. */
374 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
375 if (ka->sa.sa_flags & SA_RESTORER)
376 restorer = ka->sa.sa_restorer;
377 put_user_ex(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100378
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800379 /*
380 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
381 *
382 * WE DO NOT USE IT ANY MORE! It's only left here for historical
383 * reasons and because gdb uses it as a signature to notice
384 * signal handler stack frames.
385 */
386 put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
387 } put_user_catch(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700390 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100393 regs->sp = (unsigned long)frame;
394 regs->ip = (unsigned long)ka->sa.sa_handler;
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700395 regs->ax = (unsigned long)sig;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100396 regs->dx = (unsigned long)&frame->info;
397 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100399 regs->ds = __USER_DS;
400 regs->es = __USER_DS;
401 regs->ss = __USER_DS;
402 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
David Howells283828f2006-01-18 17:44:00 -0800404 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800406#else /* !CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800407static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
408 sigset_t *set, struct pt_regs *regs)
409{
410 struct rt_sigframe __user *frame;
411 void __user *fp = NULL;
412 int err = 0;
413 struct task_struct *me = current;
414
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);
432 put_user_ex(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
433 put_user_ex(sas_ss_flags(regs->sp),
434 &frame->uc.uc_stack.ss_flags);
435 put_user_ex(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
436 err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
437 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800438
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800439 /* Set up to return from userspace. If provided, use a stub
440 already in userspace. */
441 /* x86-64 should always use SA_RESTORER. */
442 if (ka->sa.sa_flags & SA_RESTORER) {
443 put_user_ex(ka->sa.sa_restorer, &frame->pretcode);
444 } else {
445 /* could use a vstub here */
446 err |= -EFAULT;
447 }
448 } put_user_catch(err);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800449
450 if (err)
451 return -EFAULT;
452
453 /* Set up registers for signal handler */
454 regs->di = sig;
455 /* In case the signal handler was declared without prototypes */
456 regs->ax = 0;
457
458 /* This also works for non SA_SIGINFO handlers because they expect the
459 next argument after the signal number on the stack. */
460 regs->si = (unsigned long)&frame->info;
461 regs->dx = (unsigned long)&frame->uc;
462 regs->ip = (unsigned long) ka->sa.sa_handler;
463
464 regs->sp = (unsigned long)frame;
465
466 /* Set up the CS register to run signal handlers in 64-bit mode,
467 even if the handler happens to be interrupting 32-bit code. */
468 regs->cs = __USER_CS;
469
470 return 0;
471}
472#endif /* CONFIG_X86_32 */
473
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800474#ifdef CONFIG_X86_32
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800475/*
476 * Atomically swap in the new signal mask, and wait for a signal.
477 */
478asmlinkage int
479sys_sigsuspend(int history0, int history1, old_sigset_t mask)
480{
Oleg Nesterov39822942011-07-10 21:27:27 +0200481 sigset_t blocked;
Oleg Nesterov39822942011-07-10 21:27:27 +0200482 siginitset(&blocked, mask);
Al Viro68f3f162012-05-21 21:42:32 -0400483 return sigsuspend(&blocked);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800484}
485
486asmlinkage int
487sys_sigaction(int sig, const struct old_sigaction __user *act,
488 struct old_sigaction __user *oact)
489{
490 struct k_sigaction new_ka, old_ka;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800491 int ret = 0;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800492
493 if (act) {
494 old_sigset_t mask;
495
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800496 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800497 return -EFAULT;
498
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800499 get_user_try {
500 get_user_ex(new_ka.sa.sa_handler, &act->sa_handler);
501 get_user_ex(new_ka.sa.sa_flags, &act->sa_flags);
502 get_user_ex(mask, &act->sa_mask);
503 get_user_ex(new_ka.sa.sa_restorer, &act->sa_restorer);
504 } get_user_catch(ret);
505
506 if (ret)
507 return -EFAULT;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800508 siginitset(&new_ka.sa.sa_mask, mask);
509 }
510
511 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
512
513 if (!ret && oact) {
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800514 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800515 return -EFAULT;
516
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800517 put_user_try {
518 put_user_ex(old_ka.sa.sa_handler, &oact->sa_handler);
519 put_user_ex(old_ka.sa.sa_flags, &oact->sa_flags);
520 put_user_ex(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
521 put_user_ex(old_ka.sa.sa_restorer, &oact->sa_restorer);
522 } put_user_catch(ret);
523
524 if (ret)
525 return -EFAULT;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800526 }
527
528 return ret;
529}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800530#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800531
Brian Gerst052acad2009-12-09 19:01:54 -0500532long
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800533sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
534 struct pt_regs *regs)
535{
536 return do_sigaltstack(uss, uoss, regs->sp);
537}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800538
539/*
540 * Do a signal return; undo the signal stack.
541 */
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800542#ifdef CONFIG_X86_32
Brian Gerstb12bdaf2009-02-11 16:43:58 -0500543unsigned long sys_sigreturn(struct pt_regs *regs)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800544{
545 struct sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800546 unsigned long ax;
547 sigset_t set;
548
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800549 frame = (struct sigframe __user *)(regs->sp - 8);
550
551 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
552 goto badframe;
553 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
554 && __copy_from_user(&set.sig[1], &frame->extramask,
555 sizeof(frame->extramask))))
556 goto badframe;
557
558 sigdelsetmask(&set, ~_BLOCKABLE);
Oleg Nesterov39822942011-07-10 21:27:27 +0200559 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800560
561 if (restore_sigcontext(regs, &frame->sc, &ax))
562 goto badframe;
563 return ax;
564
565badframe:
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800566 signal_fault(regs, frame, "sigreturn");
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800567
568 return 0;
569}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800570#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800571
H. Peter Anvin74452502009-02-11 16:31:40 -0800572long sys_rt_sigreturn(struct pt_regs *regs)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800573{
574 struct rt_sigframe __user *frame;
575 unsigned long ax;
576 sigset_t set;
577
578 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
579 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
580 goto badframe;
581 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
582 goto badframe;
583
584 sigdelsetmask(&set, ~_BLOCKABLE);
Oleg Nesterove9bd3f02011-04-27 21:09:39 +0200585 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800586
587 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
588 goto badframe;
589
590 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
591 goto badframe;
592
593 return ax;
594
595badframe:
596 signal_fault(regs, frame, "rt_sigreturn");
597 return 0;
598}
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600/*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100601 * OK, we're invoking a handler:
602 */
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700603static int signr_convert(int sig)
604{
Hiroshi Shimamoto96bf84b2008-10-29 18:44:08 -0700605#ifdef CONFIG_X86_32
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700606 struct thread_info *info = current_thread_info();
607
608 if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
609 return info->exec_domain->signal_invmap[sig];
Hiroshi Shimamoto96bf84b2008-10-29 18:44:08 -0700610#endif /* CONFIG_X86_32 */
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700611 return sig;
612}
613
Hiroshi Shimamotocabf5032008-10-29 18:46:07 -0700614#ifdef CONFIG_X86_32
615
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700616#define is_ia32 1
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700617#define ia32_setup_frame __setup_frame
618#define ia32_setup_rt_frame __setup_rt_frame
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700619
Hiroshi Shimamotocabf5032008-10-29 18:46:07 -0700620#else /* !CONFIG_X86_32 */
621
622#ifdef CONFIG_IA32_EMULATION
623#define is_ia32 test_thread_flag(TIF_IA32)
624#else /* !CONFIG_IA32_EMULATION */
625#define is_ia32 0
626#endif /* CONFIG_IA32_EMULATION */
627
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800628#ifdef CONFIG_X86_X32_ABI
629#define is_x32 test_thread_flag(TIF_X32)
630
631static int x32_setup_rt_frame(int sig, struct k_sigaction *ka,
632 siginfo_t *info, compat_sigset_t *set,
633 struct pt_regs *regs);
634#else /* !CONFIG_X86_X32_ABI */
635#define is_x32 0
636#endif /* CONFIG_X86_X32_ABI */
637
Hiroshi Shimamotof5223762008-12-17 18:47:17 -0800638int ia32_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
639 sigset_t *set, struct pt_regs *regs);
640int ia32_setup_frame(int sig, struct k_sigaction *ka,
641 sigset_t *set, struct pt_regs *regs);
642
Hiroshi Shimamotocabf5032008-10-29 18:46:07 -0700643#endif /* CONFIG_X86_32 */
644
Roland McGrath7c1def12005-06-23 00:08:21 -0700645static int
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700646setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
Oleg Nesterov9b429622011-07-10 20:22:03 +0200647 struct pt_regs *regs)
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700648{
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700649 int usig = signr_convert(sig);
Al Virob7f9a112012-05-02 09:59:21 -0400650 sigset_t *set = sigmask_to_save();
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700651 int ret;
652
653 /* Set up the stack frame */
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700654 if (is_ia32) {
655 if (ka->sa.sa_flags & SA_SIGINFO)
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700656 ret = ia32_setup_rt_frame(usig, ka, info, set, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700657 else
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700658 ret = ia32_setup_frame(usig, ka, set, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800659#ifdef CONFIG_X86_X32_ABI
660 } else if (is_x32) {
661 ret = x32_setup_rt_frame(usig, ka, info,
662 (compat_sigset_t *)set, regs);
663#endif
664 } else {
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700665 ret = __setup_rt_frame(sig, ka, info, set, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800666 }
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700667
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700668 if (ret) {
669 force_sigsegv(sig, current);
670 return -EFAULT;
671 }
672
Oleg Nesterov9b429622011-07-10 20:22:03 +0200673 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700674 return ret;
675}
676
677static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
Oleg Nesterov9b429622011-07-10 20:22:03 +0200679 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Roland McGrath7c1def12005-06-23 00:08:21 -0700681 int ret;
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 /* Are we from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700684 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 /* If so, check system call restarting.. */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700686 switch (syscall_get_error(current, regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800687 case -ERESTART_RESTARTBLOCK:
688 case -ERESTARTNOHAND:
689 regs->ax = -EINTR;
690 break;
691
692 case -ERESTARTSYS:
693 if (!(ka->sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100694 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800696 }
697 /* fallthrough */
698 case -ERESTARTNOINTR:
699 regs->ax = regs->orig_ax;
700 regs->ip -= 2;
701 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
703 }
704
705 /*
Roland McGrathe1f28772008-01-30 13:30:50 +0100706 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
707 * flag so that register information in the sigcontext is correct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100709 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
Roland McGrathe1f28772008-01-30 13:30:50 +0100710 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100711 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Oleg Nesterov9b429622011-07-10 20:22:03 +0200713 ret = setup_rt_frame(sig, ka, info, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Ingo Molnar7e907f42008-03-06 10:33:08 +0100715 if (ret)
716 return ret;
Roland McGrath7c1def12005-06-23 00:08:21 -0700717
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700718 /*
719 * Clear the direction flag as per the ABI for function entry.
720 */
721 regs->flags &= ~X86_EFLAGS_DF;
722
723 /*
724 * Clear TF when entering the signal handler, but
725 * notify any tracer that was single-stepping it.
726 * The tracer may want to single-step inside the
727 * handler too.
728 */
729 regs->flags &= ~X86_EFLAGS_TF;
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700730
Matt Fleming5e6292c2012-01-10 15:11:17 -0800731 block_sigmask(ka, sig);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100732
Roland McGrath36a03302008-03-14 17:46:38 -0700733 tracehook_signal_handler(sig, info, ka, regs,
734 test_thread_flag(TIF_SINGLESTEP));
735
Ingo Molnar7e907f42008-03-06 10:33:08 +0100736 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700739#ifdef CONFIG_X86_32
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700740#define NR_restart_syscall __NR_restart_syscall
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700741#else /* !CONFIG_X86_32 */
742#define NR_restart_syscall \
743 test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
744#endif /* CONFIG_X86_32 */
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746/*
747 * Note that 'init' is a special process: it doesn't get signals it doesn't
748 * want to handle. Thus you cannot kill init even with a SIGKILL even by
749 * mistake.
750 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100751static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800753 struct k_sigaction ka;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 siginfo_t info;
755 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 /*
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800758 * We want the common case to go fast, which is why we may in certain
759 * cases get here from kernel mode. Just return without doing anything
760 * if so.
761 * X86_32: vm86 regs switched out by assembly code before reaching
762 * here, so testing against kernel CS suffices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 */
Vincent Hanquez717b5942005-06-23 00:08:45 -0700764 if (!user_mode(regs))
David Howells283828f2006-01-18 17:44:00 -0800765 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
768 if (signr > 0) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100769 /* Whee! Actually deliver the signal. */
Oleg Nesterov9b429622011-07-10 20:22:03 +0200770 handle_signal(signr, &info, &ka, regs);
David Howells283828f2006-01-18 17:44:00 -0800771 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 /* Did we come from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700775 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 /* Restart the system call - no handlers present */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700777 switch (syscall_get_error(current, regs)) {
David Howells283828f2006-01-18 17:44:00 -0800778 case -ERESTARTNOHAND:
779 case -ERESTARTSYS:
780 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100781 regs->ax = regs->orig_ax;
782 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800783 break;
784
785 case -ERESTART_RESTARTBLOCK:
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700786 regs->ax = NR_restart_syscall;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100787 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800788 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
790 }
David Howells283828f2006-01-18 17:44:00 -0800791
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800792 /*
793 * If there's no signal to deliver, we just put the saved sigmask
794 * back.
795 */
Al Viro51a7b442012-05-21 23:33:55 -0400796 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
799/*
800 * notification of userspace execution resumption
David Howells283828f2006-01-18 17:44:00 -0800801 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100803void
804do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Andi Kleenc1ebf832009-07-09 00:31:41 +0200806#ifdef CONFIG_X86_MCE
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700807 /* notify userspace of pending MCEs */
808 if (thread_info_flags & _TIF_MCE_NOTIFY)
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200809 mce_notify_process();
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700810#endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
811
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530812 if (thread_info_flags & _TIF_UPROBE) {
813 clear_thread_flag(TIF_UPROBE);
814 uprobe_notify_resume(regs);
815 }
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 /* deal with pending signal delivery */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700818 if (thread_info_flags & _TIF_SIGPENDING)
David Howells283828f2006-01-18 17:44:00 -0800819 do_signal(regs);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100820
Roland McGrath59e52132008-04-19 19:10:57 -0700821 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
822 clear_thread_flag(TIF_NOTIFY_RESUME);
823 tracehook_notify_resume(regs);
824 }
Avi Kivity7c68af62009-09-19 09:40:22 +0300825 if (thread_info_flags & _TIF_USER_RETURN_NOTIFY)
826 fire_user_return_notifiers();
Roland McGrath59e52132008-04-19 19:10:57 -0700827
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700828#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 clear_thread_flag(TIF_IRET);
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700830#endif /* CONFIG_X86_32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700832
833void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
834{
835 struct task_struct *me = current;
836
837 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800838 printk("%s"
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700839 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800840 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700841 me->comm, me->pid, where, frame,
842 regs->ip, regs->sp, regs->orig_ax);
843 print_vma_addr(" in ", regs->ip);
844 printk(KERN_CONT "\n");
845 }
846
847 force_sig(SIGSEGV, me);
848}
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800849
850#ifdef CONFIG_X86_X32_ABI
851static int x32_setup_rt_frame(int sig, struct k_sigaction *ka,
852 siginfo_t *info, compat_sigset_t *set,
853 struct pt_regs *regs)
854{
855 struct rt_sigframe_x32 __user *frame;
856 void __user *restorer;
857 int err = 0;
858 void __user *fpstate = NULL;
859
860 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
861
862 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
863 return -EFAULT;
864
865 if (ka->sa.sa_flags & SA_SIGINFO) {
866 if (copy_siginfo_to_user32(&frame->info, info))
867 return -EFAULT;
868 }
869
870 put_user_try {
871 /* Create the ucontext. */
872 if (cpu_has_xsave)
873 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
874 else
875 put_user_ex(0, &frame->uc.uc_flags);
876 put_user_ex(0, &frame->uc.uc_link);
877 put_user_ex(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
878 put_user_ex(sas_ss_flags(regs->sp),
879 &frame->uc.uc_stack.ss_flags);
880 put_user_ex(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
881 put_user_ex(0, &frame->uc.uc__pad0);
882 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
883 regs, set->sig[0]);
884 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
885
886 if (ka->sa.sa_flags & SA_RESTORER) {
887 restorer = ka->sa.sa_restorer;
888 } else {
889 /* could use a vstub here */
890 restorer = NULL;
891 err |= -EFAULT;
892 }
893 put_user_ex(restorer, &frame->pretcode);
894 } put_user_catch(err);
895
896 if (err)
897 return -EFAULT;
898
899 /* Set up registers for signal handler */
900 regs->sp = (unsigned long) frame;
901 regs->ip = (unsigned long) ka->sa.sa_handler;
902
903 /* We use the x32 calling convention here... */
904 regs->di = sig;
905 regs->si = (unsigned long) &frame->info;
906 regs->dx = (unsigned long) &frame->uc;
907
908 loadsegment(ds, __USER_DS);
909 loadsegment(es, __USER_DS);
910
911 regs->cs = __USER_CS;
912 regs->ss = __USER_DS;
913
914 return 0;
915}
916
917asmlinkage long sys32_x32_rt_sigreturn(struct pt_regs *regs)
918{
919 struct rt_sigframe_x32 __user *frame;
920 sigset_t set;
921 unsigned long ax;
922 struct pt_regs tregs;
923
924 frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
925
926 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
927 goto badframe;
928 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
929 goto badframe;
930
931 sigdelsetmask(&set, ~_BLOCKABLE);
932 set_current_blocked(&set);
933
934 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
935 goto badframe;
936
937 tregs = *regs;
938 if (sys32_sigaltstack(&frame->uc.uc_stack, NULL, &tregs) == -EFAULT)
939 goto badframe;
940
941 return ax;
942
943badframe:
944 signal_fault(regs, frame, "x32 rt_sigreturn");
945 return 0;
946}
947#endif