blob: 29ad351804e9324d3aaf59c07c86d274958516b3 [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>
Ingo Molnar7e907f42008-03-06 10:33:08 +010025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/processor.h>
27#include <asm/ucontext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/i387.h>
Linus Torvalds1361b832012-02-21 13:19:22 -080029#include <asm/fpu-internal.h>
Roland McGrath6c3652e2008-01-30 13:30:42 +010030#include <asm/vdso.h>
Andi Kleen4efc0672009-04-28 19:07:31 +020031#include <asm/mce.h>
H. Peter Anvinf28f0c22012-02-19 07:38:43 -080032#include <asm/sighandling.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080033
34#ifdef CONFIG_X86_64
35#include <asm/proto.h>
36#include <asm/ia32_unistd.h>
H. Peter Anvinc5a37392012-02-19 09:41:09 -080037#include <asm/sys_ia32.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080038#endif /* CONFIG_X86_64 */
39
Hiroshi Shimamotobb579252008-09-05 16:26:55 -070040#include <asm/syscall.h>
Jaswinder Singhbbc1f692008-07-21 21:34:13 +053041#include <asm/syscalls.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010042
Hiroshi Shimamoto41af86f2008-12-17 18:50:32 -080043#include <asm/sigframe.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Harvey Harrison1a176802008-02-08 12:09:59 -080045#ifdef CONFIG_X86_32
46# define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
47#else
48# define FIX_EFLAGS __FIX_EFLAGS
49#endif
50
Tejun Heod9a89a22009-02-09 22:17:40 +090051#define COPY(x) do { \
52 get_user_ex(regs->x, &sc->x); \
53} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080054
Tejun Heod9a89a22009-02-09 22:17:40 +090055#define GET_SEG(seg) ({ \
56 unsigned short tmp; \
57 get_user_ex(tmp, &sc->seg); \
58 tmp; \
59})
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080060
Tejun Heod9a89a22009-02-09 22:17:40 +090061#define COPY_SEG(seg) do { \
62 regs->seg = GET_SEG(seg); \
63} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080064
Tejun Heod9a89a22009-02-09 22:17:40 +090065#define COPY_SEG_CPL3(seg) do { \
66 regs->seg = GET_SEG(seg) | 3; \
67} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080068
H. Peter Anvin85139422012-02-19 07:43:09 -080069int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
70 unsigned long *pax)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080071{
72 void __user *buf;
73 unsigned int tmpflags;
74 unsigned int err = 0;
75
76 /* Always make any pending restarted system calls return -EINTR */
77 current_thread_info()->restart_block.fn = do_no_restart_syscall;
78
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080079 get_user_try {
80
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080081#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +090082 set_user_gs(regs, GET_SEG(gs));
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080083 COPY_SEG(fs);
84 COPY_SEG(es);
85 COPY_SEG(ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080086#endif /* CONFIG_X86_32 */
87
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080088 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
89 COPY(dx); COPY(cx); COPY(ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080090
91#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080092 COPY(r8);
93 COPY(r9);
94 COPY(r10);
95 COPY(r11);
96 COPY(r12);
97 COPY(r13);
98 COPY(r14);
99 COPY(r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800100#endif /* CONFIG_X86_64 */
101
102#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800103 COPY_SEG_CPL3(cs);
104 COPY_SEG_CPL3(ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800105#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800106 /* Kernel saves and restores only the CS segment register on signals,
107 * which is the bare minimum needed to allow mixed 32/64-bit code.
108 * App's signal handler can save/restore other segments if needed. */
109 COPY_SEG_CPL3(cs);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800110#endif /* CONFIG_X86_32 */
111
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800112 get_user_ex(tmpflags, &sc->flags);
113 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
114 regs->orig_ax = -1; /* disable syscall checks */
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800115
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800116 get_user_ex(buf, &sc->fpstate);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800117
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800118 get_user_ex(*pax, &sc->ax);
119 } get_user_catch(err);
120
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700121 err |= restore_xstate_sig(buf, config_enabled(CONFIG_X86_32));
H. Peter Anvin5e883532012-09-21 12:43:15 -0700122
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800123 return err;
124}
125
H. Peter Anvin85139422012-02-19 07:43:09 -0800126int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
127 struct pt_regs *regs, unsigned long mask)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800128{
129 int err = 0;
130
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800131 put_user_try {
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800132
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800133#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +0900134 put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800135 put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
136 put_user_ex(regs->es, (unsigned int __user *)&sc->es);
137 put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800138#endif /* CONFIG_X86_32 */
139
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800140 put_user_ex(regs->di, &sc->di);
141 put_user_ex(regs->si, &sc->si);
142 put_user_ex(regs->bp, &sc->bp);
143 put_user_ex(regs->sp, &sc->sp);
144 put_user_ex(regs->bx, &sc->bx);
145 put_user_ex(regs->dx, &sc->dx);
146 put_user_ex(regs->cx, &sc->cx);
147 put_user_ex(regs->ax, &sc->ax);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800148#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800149 put_user_ex(regs->r8, &sc->r8);
150 put_user_ex(regs->r9, &sc->r9);
151 put_user_ex(regs->r10, &sc->r10);
152 put_user_ex(regs->r11, &sc->r11);
153 put_user_ex(regs->r12, &sc->r12);
154 put_user_ex(regs->r13, &sc->r13);
155 put_user_ex(regs->r14, &sc->r14);
156 put_user_ex(regs->r15, &sc->r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800157#endif /* CONFIG_X86_64 */
158
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530159 put_user_ex(current->thread.trap_nr, &sc->trapno);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800160 put_user_ex(current->thread.error_code, &sc->err);
161 put_user_ex(regs->ip, &sc->ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800162#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800163 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
164 put_user_ex(regs->flags, &sc->flags);
165 put_user_ex(regs->sp, &sc->sp_at_signal);
166 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800167#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800168 put_user_ex(regs->flags, &sc->flags);
169 put_user_ex(regs->cs, &sc->cs);
170 put_user_ex(0, &sc->gs);
171 put_user_ex(0, &sc->fs);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800172#endif /* CONFIG_X86_32 */
173
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800174 put_user_ex(fpstate, &sc->fpstate);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800175
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800176 /* non-iBCS2 extensions.. */
177 put_user_ex(mask, &sc->oldmask);
178 put_user_ex(current->thread.cr2, &sc->cr2);
179 } put_user_catch(err);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800180
181 return err;
182}
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * Set up a signal frame.
186 */
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800187
188/*
189 * Determine which stack to use..
190 */
Hiroshi Shimamoto1fae0272009-02-27 10:30:32 -0800191static unsigned long align_sigframe(unsigned long sp)
192{
193#ifdef CONFIG_X86_32
194 /*
195 * Align the stack pointer according to the i386 ABI,
196 * i.e. so that on function entry ((sp + 4) & 15) == 0.
197 */
198 sp = ((sp + 4) & -16ul) - 4;
199#else /* !CONFIG_X86_32 */
200 sp = round_down(sp, 16) - 8;
201#endif
202 return sp;
203}
204
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800205static inline void __user *
206get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
207 void __user **fpstate)
208{
209 /* Default to using normal stack */
Suresh Siddha72a671c2012-07-24 16:05:29 -0700210 unsigned long math_size = 0;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800211 unsigned long sp = regs->sp;
Suresh Siddha72a671c2012-07-24 16:05:29 -0700212 unsigned long buf_fx = 0;
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700213 int onsigstack = on_sig_stack(sp);
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800214
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800215 /* redzone */
Suresh Siddha050902c2012-07-24 16:05:27 -0700216 if (config_enabled(CONFIG_X86_64))
217 sp -= 128;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800218
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700219 if (!onsigstack) {
220 /* This is the X/Open sanctioned signal stack switching. */
221 if (ka->sa.sa_flags & SA_ONSTACK) {
Hiroshi Shimamoto0f8f3082009-03-26 10:03:08 -0700222 if (current->sas_ss_size)
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700223 sp = current->sas_ss_sp + current->sas_ss_size;
Suresh Siddha050902c2012-07-24 16:05:27 -0700224 } else if (config_enabled(CONFIG_X86_32) &&
225 (regs->ss & 0xffff) != __USER_DS &&
226 !(ka->sa.sa_flags & SA_RESTORER) &&
227 ka->sa.sa_restorer) {
228 /* This is the legacy signal stack switching. */
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700229 sp = (unsigned long) ka->sa.sa_restorer;
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700230 }
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800231 }
232
233 if (used_math()) {
Suresh Siddha72a671c2012-07-24 16:05:29 -0700234 sp = alloc_mathframe(sp, config_enabled(CONFIG_X86_32),
235 &buf_fx, &math_size);
Hiroshi Shimamoto25051702009-03-02 17:20:01 -0800236 *fpstate = (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800237 }
238
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700239 sp = align_sigframe(sp - frame_size);
240
241 /*
242 * If we are on the alternate signal stack and would overflow it, don't.
243 * Return an always-bogus address instead so we will die with SIGSEGV.
244 */
245 if (onsigstack && !likely(on_sig_stack(sp)))
246 return (void __user *)-1L;
247
Suresh Siddha72a671c2012-07-24 16:05:29 -0700248 /* save i387 and extended state */
249 if (used_math() &&
250 save_xstate_sig(*fpstate, (void __user *)buf_fx, math_size) < 0)
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700251 return (void __user *)-1L;
252
253 return (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800254}
255
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800256#ifdef CONFIG_X86_32
257static const struct {
258 u16 poplmovl;
259 u32 val;
260 u16 int80;
261} __attribute__((packed)) retcode = {
262 0xb858, /* popl %eax; movl $..., %eax */
263 __NR_sigreturn,
264 0x80cd, /* int $0x80 */
265};
266
267static const struct {
268 u8 movl;
269 u32 val;
270 u16 int80;
271 u8 pad;
272} __attribute__((packed)) rt_retcode = {
273 0xb8, /* movl $..., %eax */
274 __NR_rt_sigreturn,
275 0x80cd, /* int $0x80 */
276 0
277};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Ingo Molnar7e907f42008-03-06 10:33:08 +0100279static int
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700280__setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
281 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100284 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700286 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700288 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700291 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700293 if (__put_user(sig, &frame->sig))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700294 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700296 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700297 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 if (_NSIG_WORDS > 1) {
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700300 if (__copy_to_user(&frame->extramask, &set->sig[1],
301 sizeof(frame->extramask)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700302 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700305 if (current->mm->context.vdso)
Roland McGrath6c3652e2008-01-30 13:30:42 +0100306 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100307 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100308 restorer = &frame->retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (ka->sa.sa_flags & SA_RESTORER)
310 restorer = ka->sa.sa_restorer;
311
312 /* Set up to return from userspace. */
313 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100316 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 *
318 * WE DO NOT USE IT ANY MORE! It's only left here for historical
319 * reasons and because gdb uses it as a signature to notice
320 * signal handler stack frames.
321 */
Hiroshi Shimamoto4a612042008-11-11 19:09:29 -0800322 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700325 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100328 regs->sp = (unsigned long)frame;
329 regs->ip = (unsigned long)ka->sa.sa_handler;
330 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800331 regs->dx = 0;
332 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100334 regs->ds = __USER_DS;
335 regs->es = __USER_DS;
336 regs->ss = __USER_DS;
337 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
David Howells283828f2006-01-18 17:44:00 -0800339 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700342static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
343 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100346 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700348 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700350 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700353 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800355 put_user_try {
356 put_user_ex(sig, &frame->sig);
357 put_user_ex(&frame->info, &frame->pinfo);
358 put_user_ex(&frame->uc, &frame->puc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800360 /* Create the ucontext. */
361 if (cpu_has_xsave)
362 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
363 else
364 put_user_ex(0, &frame->uc.uc_flags);
365 put_user_ex(0, &frame->uc.uc_link);
366 put_user_ex(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
367 put_user_ex(sas_ss_flags(regs->sp),
368 &frame->uc.uc_stack.ss_flags);
369 put_user_ex(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800371 /* Set up to return from userspace. */
372 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
373 if (ka->sa.sa_flags & SA_RESTORER)
374 restorer = ka->sa.sa_restorer;
375 put_user_ex(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100376
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800377 /*
378 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
379 *
380 * WE DO NOT USE IT ANY MORE! It's only left here for historical
381 * reasons and because gdb uses it as a signature to notice
382 * signal handler stack frames.
383 */
384 put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
385 } put_user_catch(err);
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700386
H. Peter Anvin5e883532012-09-21 12:43:15 -0700387 err |= copy_siginfo_to_user(&frame->info, info);
388 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
389 regs, set->sig[0]);
390 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700393 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100396 regs->sp = (unsigned long)frame;
397 regs->ip = (unsigned long)ka->sa.sa_handler;
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700398 regs->ax = (unsigned long)sig;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100399 regs->dx = (unsigned long)&frame->info;
400 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100402 regs->ds = __USER_DS;
403 regs->es = __USER_DS;
404 regs->ss = __USER_DS;
405 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
David Howells283828f2006-01-18 17:44:00 -0800407 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800409#else /* !CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800410static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
411 sigset_t *set, struct pt_regs *regs)
412{
413 struct rt_sigframe __user *frame;
414 void __user *fp = NULL;
415 int err = 0;
416 struct task_struct *me = current;
417
Hiroshi Shimamoto97286a22009-02-27 10:28:48 -0800418 frame = get_sigframe(ka, regs, sizeof(struct rt_sigframe), &fp);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800419
420 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
421 return -EFAULT;
422
423 if (ka->sa.sa_flags & SA_SIGINFO) {
424 if (copy_siginfo_to_user(&frame->info, info))
425 return -EFAULT;
426 }
427
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800428 put_user_try {
429 /* Create the ucontext. */
430 if (cpu_has_xsave)
431 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
432 else
433 put_user_ex(0, &frame->uc.uc_flags);
434 put_user_ex(0, &frame->uc.uc_link);
435 put_user_ex(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
436 put_user_ex(sas_ss_flags(regs->sp),
437 &frame->uc.uc_stack.ss_flags);
438 put_user_ex(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800439
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800440 /* Set up to return from userspace. If provided, use a stub
441 already in userspace. */
442 /* x86-64 should always use SA_RESTORER. */
443 if (ka->sa.sa_flags & SA_RESTORER) {
444 put_user_ex(ka->sa.sa_restorer, &frame->pretcode);
445 } else {
446 /* could use a vstub here */
447 err |= -EFAULT;
448 }
449 } put_user_catch(err);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800450
H. Peter Anvin5e883532012-09-21 12:43:15 -0700451 err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
452 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
453
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800454 if (err)
455 return -EFAULT;
456
457 /* Set up registers for signal handler */
458 regs->di = sig;
459 /* In case the signal handler was declared without prototypes */
460 regs->ax = 0;
461
462 /* This also works for non SA_SIGINFO handlers because they expect the
463 next argument after the signal number on the stack. */
464 regs->si = (unsigned long)&frame->info;
465 regs->dx = (unsigned long)&frame->uc;
466 regs->ip = (unsigned long) ka->sa.sa_handler;
467
468 regs->sp = (unsigned long)frame;
469
470 /* Set up the CS register to run signal handlers in 64-bit mode,
471 even if the handler happens to be interrupting 32-bit code. */
472 regs->cs = __USER_CS;
473
474 return 0;
475}
476#endif /* CONFIG_X86_32 */
477
Suresh Siddha050902c2012-07-24 16:05:27 -0700478static int x32_setup_rt_frame(int sig, struct k_sigaction *ka,
479 siginfo_t *info, compat_sigset_t *set,
480 struct pt_regs *regs)
481{
482#ifdef CONFIG_X86_X32_ABI
483 struct rt_sigframe_x32 __user *frame;
484 void __user *restorer;
485 int err = 0;
486 void __user *fpstate = NULL;
487
488 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
489
490 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
491 return -EFAULT;
492
493 if (ka->sa.sa_flags & SA_SIGINFO) {
494 if (copy_siginfo_to_user32(&frame->info, info))
495 return -EFAULT;
496 }
497
498 put_user_try {
499 /* Create the ucontext. */
500 if (cpu_has_xsave)
501 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
502 else
503 put_user_ex(0, &frame->uc.uc_flags);
504 put_user_ex(0, &frame->uc.uc_link);
505 put_user_ex(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
506 put_user_ex(sas_ss_flags(regs->sp),
507 &frame->uc.uc_stack.ss_flags);
508 put_user_ex(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
509 put_user_ex(0, &frame->uc.uc__pad0);
Suresh Siddha050902c2012-07-24 16:05:27 -0700510
511 if (ka->sa.sa_flags & SA_RESTORER) {
512 restorer = ka->sa.sa_restorer;
513 } else {
514 /* could use a vstub here */
515 restorer = NULL;
516 err |= -EFAULT;
517 }
518 put_user_ex(restorer, &frame->pretcode);
519 } put_user_catch(err);
520
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700521 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
522 regs, set->sig[0]);
523 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
524
Suresh Siddha050902c2012-07-24 16:05:27 -0700525 if (err)
526 return -EFAULT;
527
528 /* Set up registers for signal handler */
529 regs->sp = (unsigned long) frame;
530 regs->ip = (unsigned long) ka->sa.sa_handler;
531
532 /* We use the x32 calling convention here... */
533 regs->di = sig;
534 regs->si = (unsigned long) &frame->info;
535 regs->dx = (unsigned long) &frame->uc;
536
537 loadsegment(ds, __USER_DS);
538 loadsegment(es, __USER_DS);
539
540 regs->cs = __USER_CS;
541 regs->ss = __USER_DS;
542#endif /* CONFIG_X86_X32_ABI */
543
544 return 0;
545}
546
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800547#ifdef CONFIG_X86_32
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800548/*
549 * Atomically swap in the new signal mask, and wait for a signal.
550 */
551asmlinkage int
552sys_sigsuspend(int history0, int history1, old_sigset_t mask)
553{
Oleg Nesterov39822942011-07-10 21:27:27 +0200554 sigset_t blocked;
Oleg Nesterov39822942011-07-10 21:27:27 +0200555 siginitset(&blocked, mask);
Al Viro68f3f162012-05-21 21:42:32 -0400556 return sigsuspend(&blocked);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800557}
558
559asmlinkage int
560sys_sigaction(int sig, const struct old_sigaction __user *act,
561 struct old_sigaction __user *oact)
562{
563 struct k_sigaction new_ka, old_ka;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800564 int ret = 0;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800565
566 if (act) {
567 old_sigset_t mask;
568
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800569 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800570 return -EFAULT;
571
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800572 get_user_try {
573 get_user_ex(new_ka.sa.sa_handler, &act->sa_handler);
574 get_user_ex(new_ka.sa.sa_flags, &act->sa_flags);
575 get_user_ex(mask, &act->sa_mask);
576 get_user_ex(new_ka.sa.sa_restorer, &act->sa_restorer);
577 } get_user_catch(ret);
578
579 if (ret)
580 return -EFAULT;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800581 siginitset(&new_ka.sa.sa_mask, mask);
582 }
583
584 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
585
586 if (!ret && oact) {
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800587 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800588 return -EFAULT;
589
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800590 put_user_try {
591 put_user_ex(old_ka.sa.sa_handler, &oact->sa_handler);
592 put_user_ex(old_ka.sa.sa_flags, &oact->sa_flags);
593 put_user_ex(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
594 put_user_ex(old_ka.sa.sa_restorer, &oact->sa_restorer);
595 } put_user_catch(ret);
596
597 if (ret)
598 return -EFAULT;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800599 }
600
601 return ret;
602}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800603#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800604
Brian Gerst052acad2009-12-09 19:01:54 -0500605long
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800606sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
607 struct pt_regs *regs)
608{
609 return do_sigaltstack(uss, uoss, regs->sp);
610}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800611
612/*
613 * Do a signal return; undo the signal stack.
614 */
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800615#ifdef CONFIG_X86_32
Brian Gerstb12bdaf2009-02-11 16:43:58 -0500616unsigned long sys_sigreturn(struct pt_regs *regs)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800617{
618 struct sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800619 unsigned long ax;
620 sigset_t set;
621
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800622 frame = (struct sigframe __user *)(regs->sp - 8);
623
624 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
625 goto badframe;
626 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
627 && __copy_from_user(&set.sig[1], &frame->extramask,
628 sizeof(frame->extramask))))
629 goto badframe;
630
Oleg Nesterov39822942011-07-10 21:27:27 +0200631 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800632
633 if (restore_sigcontext(regs, &frame->sc, &ax))
634 goto badframe;
635 return ax;
636
637badframe:
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800638 signal_fault(regs, frame, "sigreturn");
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800639
640 return 0;
641}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800642#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800643
H. Peter Anvin74452502009-02-11 16:31:40 -0800644long sys_rt_sigreturn(struct pt_regs *regs)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800645{
646 struct rt_sigframe __user *frame;
647 unsigned long ax;
648 sigset_t set;
649
650 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
651 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
652 goto badframe;
653 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
654 goto badframe;
655
Oleg Nesterove9bd3f02011-04-27 21:09:39 +0200656 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800657
658 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
659 goto badframe;
660
661 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
662 goto badframe;
663
664 return ax;
665
666badframe:
667 signal_fault(regs, frame, "rt_sigreturn");
668 return 0;
669}
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671/*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100672 * OK, we're invoking a handler:
673 */
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700674static int signr_convert(int sig)
675{
Hiroshi Shimamoto96bf84b2008-10-29 18:44:08 -0700676#ifdef CONFIG_X86_32
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700677 struct thread_info *info = current_thread_info();
678
679 if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
680 return info->exec_domain->signal_invmap[sig];
Hiroshi Shimamoto96bf84b2008-10-29 18:44:08 -0700681#endif /* CONFIG_X86_32 */
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700682 return sig;
683}
684
Roland McGrath7c1def12005-06-23 00:08:21 -0700685static int
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700686setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
Oleg Nesterov9b429622011-07-10 20:22:03 +0200687 struct pt_regs *regs)
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700688{
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700689 int usig = signr_convert(sig);
Al Virob7f9a112012-05-02 09:59:21 -0400690 sigset_t *set = sigmask_to_save();
Suresh Siddha050902c2012-07-24 16:05:27 -0700691 compat_sigset_t *cset = (compat_sigset_t *) set;
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700692
693 /* Set up the stack frame */
Suresh Siddha050902c2012-07-24 16:05:27 -0700694 if (is_ia32_frame()) {
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700695 if (ka->sa.sa_flags & SA_SIGINFO)
Suresh Siddha050902c2012-07-24 16:05:27 -0700696 return ia32_setup_rt_frame(usig, ka, info, cset, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700697 else
Suresh Siddha050902c2012-07-24 16:05:27 -0700698 return ia32_setup_frame(usig, ka, cset, regs);
699 } else if (is_x32_frame()) {
700 return x32_setup_rt_frame(usig, ka, info, cset, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800701 } else {
Al Viroa610d6e2012-05-21 23:42:15 -0400702 return __setup_rt_frame(sig, ka, info, set, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800703 }
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700704}
705
Al Viroa610d6e2012-05-21 23:42:15 -0400706static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
Oleg Nesterov9b429622011-07-10 20:22:03 +0200708 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
710 /* Are we from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700711 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /* If so, check system call restarting.. */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700713 switch (syscall_get_error(current, regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800714 case -ERESTART_RESTARTBLOCK:
715 case -ERESTARTNOHAND:
716 regs->ax = -EINTR;
717 break;
718
719 case -ERESTARTSYS:
720 if (!(ka->sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100721 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800723 }
724 /* fallthrough */
725 case -ERESTARTNOINTR:
726 regs->ax = regs->orig_ax;
727 regs->ip -= 2;
728 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730 }
731
732 /*
Roland McGrathe1f28772008-01-30 13:30:50 +0100733 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
734 * flag so that register information in the sigcontext is correct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100736 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
Roland McGrathe1f28772008-01-30 13:30:50 +0100737 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100738 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Al Viroa610d6e2012-05-21 23:42:15 -0400740 if (setup_rt_frame(sig, ka, info, regs) < 0) {
741 force_sigsegv(sig, current);
742 return;
743 }
Roland McGrath7c1def12005-06-23 00:08:21 -0700744
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700745 /*
746 * Clear the direction flag as per the ABI for function entry.
747 */
748 regs->flags &= ~X86_EFLAGS_DF;
749
750 /*
751 * Clear TF when entering the signal handler, but
752 * notify any tracer that was single-stepping it.
753 * The tracer may want to single-step inside the
754 * handler too.
755 */
756 regs->flags &= ~X86_EFLAGS_TF;
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700757
Al Viroefee9842012-04-28 02:04:15 -0400758 signal_delivered(sig, info, ka, regs,
759 test_thread_flag(TIF_SINGLESTEP));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700762#ifdef CONFIG_X86_32
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700763#define NR_restart_syscall __NR_restart_syscall
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700764#else /* !CONFIG_X86_32 */
765#define NR_restart_syscall \
766 test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
767#endif /* CONFIG_X86_32 */
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769/*
770 * Note that 'init' is a special process: it doesn't get signals it doesn't
771 * want to handle. Thus you cannot kill init even with a SIGKILL even by
772 * mistake.
773 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100774static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800776 struct k_sigaction ka;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 siginfo_t info;
778 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
781 if (signr > 0) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100782 /* Whee! Actually deliver the signal. */
Oleg Nesterov9b429622011-07-10 20:22:03 +0200783 handle_signal(signr, &info, &ka, regs);
David Howells283828f2006-01-18 17:44:00 -0800784 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 /* Did we come from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700788 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 /* Restart the system call - no handlers present */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700790 switch (syscall_get_error(current, regs)) {
David Howells283828f2006-01-18 17:44:00 -0800791 case -ERESTARTNOHAND:
792 case -ERESTARTSYS:
793 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100794 regs->ax = regs->orig_ax;
795 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800796 break;
797
798 case -ERESTART_RESTARTBLOCK:
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700799 regs->ax = NR_restart_syscall;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100800 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800801 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803 }
David Howells283828f2006-01-18 17:44:00 -0800804
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800805 /*
806 * If there's no signal to deliver, we just put the saved sigmask
807 * back.
808 */
Al Viro51a7b442012-05-21 23:33:55 -0400809 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
812/*
813 * notification of userspace execution resumption
David Howells283828f2006-01-18 17:44:00 -0800814 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100816void
817do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Frederic Weisbeckeredf55fd2012-07-11 20:26:39 +0200819 rcu_user_exit();
820
Andi Kleenc1ebf832009-07-09 00:31:41 +0200821#ifdef CONFIG_X86_MCE
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700822 /* notify userspace of pending MCEs */
823 if (thread_info_flags & _TIF_MCE_NOTIFY)
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200824 mce_notify_process();
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700825#endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
826
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530827 if (thread_info_flags & _TIF_UPROBE) {
828 clear_thread_flag(TIF_UPROBE);
829 uprobe_notify_resume(regs);
830 }
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 /* deal with pending signal delivery */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700833 if (thread_info_flags & _TIF_SIGPENDING)
David Howells283828f2006-01-18 17:44:00 -0800834 do_signal(regs);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100835
Roland McGrath59e52132008-04-19 19:10:57 -0700836 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
837 clear_thread_flag(TIF_NOTIFY_RESUME);
838 tracehook_notify_resume(regs);
839 }
Avi Kivity7c68af62009-09-19 09:40:22 +0300840 if (thread_info_flags & _TIF_USER_RETURN_NOTIFY)
841 fire_user_return_notifiers();
Roland McGrath59e52132008-04-19 19:10:57 -0700842
Frederic Weisbeckeredf55fd2012-07-11 20:26:39 +0200843 rcu_user_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700845
846void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
847{
848 struct task_struct *me = current;
849
850 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800851 printk("%s"
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700852 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800853 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700854 me->comm, me->pid, where, frame,
855 regs->ip, regs->sp, regs->orig_ax);
856 print_vma_addr(" in ", regs->ip);
Joe Perchesc767a542012-05-21 19:50:07 -0700857 pr_cont("\n");
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700858 }
859
860 force_sig(SIGSEGV, me);
861}
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800862
863#ifdef CONFIG_X86_X32_ABI
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800864asmlinkage long sys32_x32_rt_sigreturn(struct pt_regs *regs)
865{
866 struct rt_sigframe_x32 __user *frame;
867 sigset_t set;
868 unsigned long ax;
869 struct pt_regs tregs;
870
871 frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
872
873 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
874 goto badframe;
875 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
876 goto badframe;
877
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800878 set_current_blocked(&set);
879
880 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
881 goto badframe;
882
883 tregs = *regs;
884 if (sys32_sigaltstack(&frame->uc.uc_stack, NULL, &tregs) == -EFAULT)
885 goto badframe;
886
887 return ax;
888
889badframe:
890 signal_fault(regs, frame, "x32 rt_sigreturn");
891 return 0;
892}
893#endif