blob: b280908a376e20efc6773b76c6e80353ef8680f4 [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);
117 err |= restore_i387_xstate(buf);
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
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800122 return err;
123}
124
H. Peter Anvin85139422012-02-19 07:43:09 -0800125int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
126 struct pt_regs *regs, unsigned long mask)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800127{
128 int err = 0;
129
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800130 put_user_try {
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800131
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800132#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +0900133 put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800134 put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
135 put_user_ex(regs->es, (unsigned int __user *)&sc->es);
136 put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800137#endif /* CONFIG_X86_32 */
138
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800139 put_user_ex(regs->di, &sc->di);
140 put_user_ex(regs->si, &sc->si);
141 put_user_ex(regs->bp, &sc->bp);
142 put_user_ex(regs->sp, &sc->sp);
143 put_user_ex(regs->bx, &sc->bx);
144 put_user_ex(regs->dx, &sc->dx);
145 put_user_ex(regs->cx, &sc->cx);
146 put_user_ex(regs->ax, &sc->ax);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800147#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800148 put_user_ex(regs->r8, &sc->r8);
149 put_user_ex(regs->r9, &sc->r9);
150 put_user_ex(regs->r10, &sc->r10);
151 put_user_ex(regs->r11, &sc->r11);
152 put_user_ex(regs->r12, &sc->r12);
153 put_user_ex(regs->r13, &sc->r13);
154 put_user_ex(regs->r14, &sc->r14);
155 put_user_ex(regs->r15, &sc->r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800156#endif /* CONFIG_X86_64 */
157
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530158 put_user_ex(current->thread.trap_nr, &sc->trapno);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800159 put_user_ex(current->thread.error_code, &sc->err);
160 put_user_ex(regs->ip, &sc->ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800161#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800162 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
163 put_user_ex(regs->flags, &sc->flags);
164 put_user_ex(regs->sp, &sc->sp_at_signal);
165 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800166#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800167 put_user_ex(regs->flags, &sc->flags);
168 put_user_ex(regs->cs, &sc->cs);
169 put_user_ex(0, &sc->gs);
170 put_user_ex(0, &sc->fs);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800171#endif /* CONFIG_X86_32 */
172
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800173 put_user_ex(fpstate, &sc->fpstate);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800174
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800175 /* non-iBCS2 extensions.. */
176 put_user_ex(mask, &sc->oldmask);
177 put_user_ex(current->thread.cr2, &sc->cr2);
178 } put_user_catch(err);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800179
180 return err;
181}
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 * Set up a signal frame.
185 */
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800186
187/*
188 * Determine which stack to use..
189 */
Hiroshi Shimamoto1fae0272009-02-27 10:30:32 -0800190static unsigned long align_sigframe(unsigned long sp)
191{
192#ifdef CONFIG_X86_32
193 /*
194 * Align the stack pointer according to the i386 ABI,
195 * i.e. so that on function entry ((sp + 4) & 15) == 0.
196 */
197 sp = ((sp + 4) & -16ul) - 4;
198#else /* !CONFIG_X86_32 */
199 sp = round_down(sp, 16) - 8;
200#endif
201 return sp;
202}
203
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800204static inline void __user *
205get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
206 void __user **fpstate)
207{
208 /* Default to using normal stack */
209 unsigned long sp = regs->sp;
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700210 int onsigstack = on_sig_stack(sp);
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800211
212#ifdef CONFIG_X86_64
213 /* redzone */
214 sp -= 128;
215#endif /* CONFIG_X86_64 */
216
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700217 if (!onsigstack) {
218 /* This is the X/Open sanctioned signal stack switching. */
219 if (ka->sa.sa_flags & SA_ONSTACK) {
Hiroshi Shimamoto0f8f3082009-03-26 10:03:08 -0700220 if (current->sas_ss_size)
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700221 sp = current->sas_ss_sp + current->sas_ss_size;
222 } else {
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800223#ifdef CONFIG_X86_32
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700224 /* This is the legacy signal stack switching. */
225 if ((regs->ss & 0xffff) != __USER_DS &&
226 !(ka->sa.sa_flags & SA_RESTORER) &&
227 ka->sa.sa_restorer)
228 sp = (unsigned long) ka->sa.sa_restorer;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800229#endif /* CONFIG_X86_32 */
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700230 }
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800231 }
232
233 if (used_math()) {
234 sp -= sig_xstate_size;
Hiroshi Shimamoto25051702009-03-02 17:20:01 -0800235#ifdef CONFIG_X86_64
236 sp = round_down(sp, 64);
237#endif /* CONFIG_X86_64 */
238 *fpstate = (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800239 }
240
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700241 sp = align_sigframe(sp - frame_size);
242
243 /*
244 * If we are on the alternate signal stack and would overflow it, don't.
245 * Return an always-bogus address instead so we will die with SIGSEGV.
246 */
247 if (onsigstack && !likely(on_sig_stack(sp)))
248 return (void __user *)-1L;
249
250 /* save i387 state */
251 if (used_math() && save_i387_xstate(*fpstate) < 0)
252 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);
360 err |= copy_siginfo_to_user(&frame->info, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800362 /* Create the ucontext. */
363 if (cpu_has_xsave)
364 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
365 else
366 put_user_ex(0, &frame->uc.uc_flags);
367 put_user_ex(0, &frame->uc.uc_link);
368 put_user_ex(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
369 put_user_ex(sas_ss_flags(regs->sp),
370 &frame->uc.uc_stack.ss_flags);
371 put_user_ex(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
372 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
373 regs, set->sig[0]);
374 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800376 /* Set up to return from userspace. */
377 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
378 if (ka->sa.sa_flags & SA_RESTORER)
379 restorer = ka->sa.sa_restorer;
380 put_user_ex(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100381
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800382 /*
383 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
384 *
385 * WE DO NOT USE IT ANY MORE! It's only left here for historical
386 * reasons and because gdb uses it as a signature to notice
387 * signal handler stack frames.
388 */
389 put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
390 } put_user_catch(err);
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);
439 err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
440 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800441
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800442 /* Set up to return from userspace. If provided, use a stub
443 already in userspace. */
444 /* x86-64 should always use SA_RESTORER. */
445 if (ka->sa.sa_flags & SA_RESTORER) {
446 put_user_ex(ka->sa.sa_restorer, &frame->pretcode);
447 } else {
448 /* could use a vstub here */
449 err |= -EFAULT;
450 }
451 } put_user_catch(err);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800452
453 if (err)
454 return -EFAULT;
455
456 /* Set up registers for signal handler */
457 regs->di = sig;
458 /* In case the signal handler was declared without prototypes */
459 regs->ax = 0;
460
461 /* This also works for non SA_SIGINFO handlers because they expect the
462 next argument after the signal number on the stack. */
463 regs->si = (unsigned long)&frame->info;
464 regs->dx = (unsigned long)&frame->uc;
465 regs->ip = (unsigned long) ka->sa.sa_handler;
466
467 regs->sp = (unsigned long)frame;
468
469 /* Set up the CS register to run signal handlers in 64-bit mode,
470 even if the handler happens to be interrupting 32-bit code. */
471 regs->cs = __USER_CS;
472
473 return 0;
474}
475#endif /* CONFIG_X86_32 */
476
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800477#ifdef CONFIG_X86_32
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800478/*
479 * Atomically swap in the new signal mask, and wait for a signal.
480 */
481asmlinkage int
482sys_sigsuspend(int history0, int history1, old_sigset_t mask)
483{
Oleg Nesterov39822942011-07-10 21:27:27 +0200484 sigset_t blocked;
Oleg Nesterov39822942011-07-10 21:27:27 +0200485 siginitset(&blocked, mask);
Al Viro68f3f162012-05-21 21:42:32 -0400486 return sigsuspend(&blocked);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800487}
488
489asmlinkage int
490sys_sigaction(int sig, const struct old_sigaction __user *act,
491 struct old_sigaction __user *oact)
492{
493 struct k_sigaction new_ka, old_ka;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800494 int ret = 0;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800495
496 if (act) {
497 old_sigset_t mask;
498
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800499 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800500 return -EFAULT;
501
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800502 get_user_try {
503 get_user_ex(new_ka.sa.sa_handler, &act->sa_handler);
504 get_user_ex(new_ka.sa.sa_flags, &act->sa_flags);
505 get_user_ex(mask, &act->sa_mask);
506 get_user_ex(new_ka.sa.sa_restorer, &act->sa_restorer);
507 } get_user_catch(ret);
508
509 if (ret)
510 return -EFAULT;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800511 siginitset(&new_ka.sa.sa_mask, mask);
512 }
513
514 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
515
516 if (!ret && oact) {
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800517 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800518 return -EFAULT;
519
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800520 put_user_try {
521 put_user_ex(old_ka.sa.sa_handler, &oact->sa_handler);
522 put_user_ex(old_ka.sa.sa_flags, &oact->sa_flags);
523 put_user_ex(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
524 put_user_ex(old_ka.sa.sa_restorer, &oact->sa_restorer);
525 } put_user_catch(ret);
526
527 if (ret)
528 return -EFAULT;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800529 }
530
531 return ret;
532}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800533#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800534
Brian Gerst052acad2009-12-09 19:01:54 -0500535long
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800536sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
537 struct pt_regs *regs)
538{
539 return do_sigaltstack(uss, uoss, regs->sp);
540}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800541
542/*
543 * Do a signal return; undo the signal stack.
544 */
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800545#ifdef CONFIG_X86_32
Brian Gerstb12bdaf2009-02-11 16:43:58 -0500546unsigned long sys_sigreturn(struct pt_regs *regs)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800547{
548 struct sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800549 unsigned long ax;
550 sigset_t set;
551
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800552 frame = (struct sigframe __user *)(regs->sp - 8);
553
554 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
555 goto badframe;
556 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
557 && __copy_from_user(&set.sig[1], &frame->extramask,
558 sizeof(frame->extramask))))
559 goto badframe;
560
Oleg Nesterov39822942011-07-10 21:27:27 +0200561 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800562
563 if (restore_sigcontext(regs, &frame->sc, &ax))
564 goto badframe;
565 return ax;
566
567badframe:
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800568 signal_fault(regs, frame, "sigreturn");
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800569
570 return 0;
571}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800572#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800573
H. Peter Anvin74452502009-02-11 16:31:40 -0800574long sys_rt_sigreturn(struct pt_regs *regs)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800575{
576 struct rt_sigframe __user *frame;
577 unsigned long ax;
578 sigset_t set;
579
580 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
581 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
582 goto badframe;
583 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
584 goto badframe;
585
Oleg Nesterove9bd3f02011-04-27 21:09:39 +0200586 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800587
588 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
589 goto badframe;
590
591 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
592 goto badframe;
593
594 return ax;
595
596badframe:
597 signal_fault(regs, frame, "rt_sigreturn");
598 return 0;
599}
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601/*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100602 * OK, we're invoking a handler:
603 */
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700604static int signr_convert(int sig)
605{
Hiroshi Shimamoto96bf84b2008-10-29 18:44:08 -0700606#ifdef CONFIG_X86_32
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700607 struct thread_info *info = current_thread_info();
608
609 if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
610 return info->exec_domain->signal_invmap[sig];
Hiroshi Shimamoto96bf84b2008-10-29 18:44:08 -0700611#endif /* CONFIG_X86_32 */
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700612 return sig;
613}
614
Hiroshi Shimamotocabf5032008-10-29 18:46:07 -0700615#ifdef CONFIG_X86_32
616
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700617#define is_ia32 1
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700618#define ia32_setup_frame __setup_frame
619#define ia32_setup_rt_frame __setup_rt_frame
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700620
Hiroshi Shimamotocabf5032008-10-29 18:46:07 -0700621#else /* !CONFIG_X86_32 */
622
623#ifdef CONFIG_IA32_EMULATION
624#define is_ia32 test_thread_flag(TIF_IA32)
625#else /* !CONFIG_IA32_EMULATION */
626#define is_ia32 0
627#endif /* CONFIG_IA32_EMULATION */
628
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800629#ifdef CONFIG_X86_X32_ABI
630#define is_x32 test_thread_flag(TIF_X32)
631
632static int x32_setup_rt_frame(int sig, struct k_sigaction *ka,
633 siginfo_t *info, compat_sigset_t *set,
634 struct pt_regs *regs);
635#else /* !CONFIG_X86_X32_ABI */
636#define is_x32 0
637#endif /* CONFIG_X86_X32_ABI */
638
Hiroshi Shimamotof5223762008-12-17 18:47:17 -0800639int ia32_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
640 sigset_t *set, struct pt_regs *regs);
641int ia32_setup_frame(int sig, struct k_sigaction *ka,
642 sigset_t *set, struct pt_regs *regs);
643
Hiroshi Shimamotocabf5032008-10-29 18:46:07 -0700644#endif /* CONFIG_X86_32 */
645
Roland McGrath7c1def12005-06-23 00:08:21 -0700646static int
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700647setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
Oleg Nesterov9b429622011-07-10 20:22:03 +0200648 struct pt_regs *regs)
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700649{
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700650 int usig = signr_convert(sig);
Al Virob7f9a112012-05-02 09:59:21 -0400651 sigset_t *set = sigmask_to_save();
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700652
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)
Al Viroa610d6e2012-05-21 23:42:15 -0400656 return ia32_setup_rt_frame(usig, ka, info, set, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700657 else
Al Viroa610d6e2012-05-21 23:42:15 -0400658 return 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) {
Al Viroa610d6e2012-05-21 23:42:15 -0400661 return x32_setup_rt_frame(usig, ka, info,
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800662 (compat_sigset_t *)set, regs);
663#endif
664 } else {
Al Viroa610d6e2012-05-21 23:42:15 -0400665 return __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}
668
Al Viroa610d6e2012-05-21 23:42:15 -0400669static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
Oleg Nesterov9b429622011-07-10 20:22:03 +0200671 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 /* Are we from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700674 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 /* If so, check system call restarting.. */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700676 switch (syscall_get_error(current, regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800677 case -ERESTART_RESTARTBLOCK:
678 case -ERESTARTNOHAND:
679 regs->ax = -EINTR;
680 break;
681
682 case -ERESTARTSYS:
683 if (!(ka->sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100684 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800686 }
687 /* fallthrough */
688 case -ERESTARTNOINTR:
689 regs->ax = regs->orig_ax;
690 regs->ip -= 2;
691 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693 }
694
695 /*
Roland McGrathe1f28772008-01-30 13:30:50 +0100696 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
697 * flag so that register information in the sigcontext is correct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100699 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
Roland McGrathe1f28772008-01-30 13:30:50 +0100700 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100701 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Al Viroa610d6e2012-05-21 23:42:15 -0400703 if (setup_rt_frame(sig, ka, info, regs) < 0) {
704 force_sigsegv(sig, current);
705 return;
706 }
Roland McGrath7c1def12005-06-23 00:08:21 -0700707
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700708 /*
709 * Clear the direction flag as per the ABI for function entry.
710 */
711 regs->flags &= ~X86_EFLAGS_DF;
712
713 /*
714 * Clear TF when entering the signal handler, but
715 * notify any tracer that was single-stepping it.
716 * The tracer may want to single-step inside the
717 * handler too.
718 */
719 regs->flags &= ~X86_EFLAGS_TF;
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700720
Al Viroefee9842012-04-28 02:04:15 -0400721 signal_delivered(sig, info, ka, regs,
722 test_thread_flag(TIF_SINGLESTEP));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700725#ifdef CONFIG_X86_32
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700726#define NR_restart_syscall __NR_restart_syscall
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700727#else /* !CONFIG_X86_32 */
728#define NR_restart_syscall \
729 test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
730#endif /* CONFIG_X86_32 */
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732/*
733 * Note that 'init' is a special process: it doesn't get signals it doesn't
734 * want to handle. Thus you cannot kill init even with a SIGKILL even by
735 * mistake.
736 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100737static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800739 struct k_sigaction ka;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 siginfo_t info;
741 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
744 if (signr > 0) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100745 /* Whee! Actually deliver the signal. */
Oleg Nesterov9b429622011-07-10 20:22:03 +0200746 handle_signal(signr, &info, &ka, regs);
David Howells283828f2006-01-18 17:44:00 -0800747 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 /* Did we come from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700751 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 /* Restart the system call - no handlers present */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700753 switch (syscall_get_error(current, regs)) {
David Howells283828f2006-01-18 17:44:00 -0800754 case -ERESTARTNOHAND:
755 case -ERESTARTSYS:
756 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100757 regs->ax = regs->orig_ax;
758 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800759 break;
760
761 case -ERESTART_RESTARTBLOCK:
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700762 regs->ax = NR_restart_syscall;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100763 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800764 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766 }
David Howells283828f2006-01-18 17:44:00 -0800767
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800768 /*
769 * If there's no signal to deliver, we just put the saved sigmask
770 * back.
771 */
Al Viro51a7b442012-05-21 23:33:55 -0400772 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
775/*
776 * notification of userspace execution resumption
David Howells283828f2006-01-18 17:44:00 -0800777 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100779void
780do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Andi Kleenc1ebf832009-07-09 00:31:41 +0200782#ifdef CONFIG_X86_MCE
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700783 /* notify userspace of pending MCEs */
784 if (thread_info_flags & _TIF_MCE_NOTIFY)
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200785 mce_notify_process();
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700786#endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
787
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530788 if (thread_info_flags & _TIF_UPROBE) {
789 clear_thread_flag(TIF_UPROBE);
790 uprobe_notify_resume(regs);
791 }
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 /* deal with pending signal delivery */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700794 if (thread_info_flags & _TIF_SIGPENDING)
David Howells283828f2006-01-18 17:44:00 -0800795 do_signal(regs);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100796
Roland McGrath59e52132008-04-19 19:10:57 -0700797 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
798 clear_thread_flag(TIF_NOTIFY_RESUME);
799 tracehook_notify_resume(regs);
800 }
Avi Kivity7c68af62009-09-19 09:40:22 +0300801 if (thread_info_flags & _TIF_USER_RETURN_NOTIFY)
802 fire_user_return_notifiers();
Roland McGrath59e52132008-04-19 19:10:57 -0700803
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700804#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 clear_thread_flag(TIF_IRET);
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700806#endif /* CONFIG_X86_32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700808
809void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
810{
811 struct task_struct *me = current;
812
813 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800814 printk("%s"
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700815 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800816 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700817 me->comm, me->pid, where, frame,
818 regs->ip, regs->sp, regs->orig_ax);
819 print_vma_addr(" in ", regs->ip);
Joe Perchesc767a542012-05-21 19:50:07 -0700820 pr_cont("\n");
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700821 }
822
823 force_sig(SIGSEGV, me);
824}
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800825
826#ifdef CONFIG_X86_X32_ABI
827static int x32_setup_rt_frame(int sig, struct k_sigaction *ka,
828 siginfo_t *info, compat_sigset_t *set,
829 struct pt_regs *regs)
830{
831 struct rt_sigframe_x32 __user *frame;
832 void __user *restorer;
833 int err = 0;
834 void __user *fpstate = NULL;
835
836 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
837
838 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
839 return -EFAULT;
840
841 if (ka->sa.sa_flags & SA_SIGINFO) {
842 if (copy_siginfo_to_user32(&frame->info, info))
843 return -EFAULT;
844 }
845
846 put_user_try {
847 /* Create the ucontext. */
848 if (cpu_has_xsave)
849 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
850 else
851 put_user_ex(0, &frame->uc.uc_flags);
852 put_user_ex(0, &frame->uc.uc_link);
853 put_user_ex(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
854 put_user_ex(sas_ss_flags(regs->sp),
855 &frame->uc.uc_stack.ss_flags);
856 put_user_ex(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
857 put_user_ex(0, &frame->uc.uc__pad0);
858 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
859 regs, set->sig[0]);
860 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
861
862 if (ka->sa.sa_flags & SA_RESTORER) {
863 restorer = ka->sa.sa_restorer;
864 } else {
865 /* could use a vstub here */
866 restorer = NULL;
867 err |= -EFAULT;
868 }
869 put_user_ex(restorer, &frame->pretcode);
870 } put_user_catch(err);
871
872 if (err)
873 return -EFAULT;
874
875 /* Set up registers for signal handler */
876 regs->sp = (unsigned long) frame;
877 regs->ip = (unsigned long) ka->sa.sa_handler;
878
879 /* We use the x32 calling convention here... */
880 regs->di = sig;
881 regs->si = (unsigned long) &frame->info;
882 regs->dx = (unsigned long) &frame->uc;
883
884 loadsegment(ds, __USER_DS);
885 loadsegment(es, __USER_DS);
886
887 regs->cs = __USER_CS;
888 regs->ss = __USER_DS;
889
890 return 0;
891}
892
893asmlinkage long sys32_x32_rt_sigreturn(struct pt_regs *regs)
894{
895 struct rt_sigframe_x32 __user *frame;
896 sigset_t set;
897 unsigned long ax;
898 struct pt_regs tregs;
899
900 frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
901
902 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
903 goto badframe;
904 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
905 goto badframe;
906
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800907 set_current_blocked(&set);
908
909 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
910 goto badframe;
911
912 tregs = *regs;
913 if (sys32_sigaltstack(&frame->uc.uc_stack, NULL, &tregs) == -EFAULT)
914 goto badframe;
915
916 return ax;
917
918badframe:
919 signal_fault(regs, frame, "x32 rt_sigreturn");
920 return 0;
921}
922#endif