blob: b3cd6913ceeaa0aac511729218c2f950c4272244 [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>
Roland McGrath6c3652e2008-01-30 13:30:42 +010026#include <asm/vdso.h>
Andi Kleen4efc0672009-04-28 19:07:31 +020027#include <asm/mce.h>
H. Peter Anvinf28f0c22012-02-19 07:38:43 -080028#include <asm/sighandling.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080029
30#ifdef CONFIG_X86_64
31#include <asm/proto.h>
32#include <asm/ia32_unistd.h>
H. Peter Anvinc5a37392012-02-19 09:41:09 -080033#include <asm/sys_ia32.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080034#endif /* CONFIG_X86_64 */
35
Hiroshi Shimamotobb579252008-09-05 16:26:55 -070036#include <asm/syscall.h>
Jaswinder Singhbbc1f692008-07-21 21:34:13 +053037#include <asm/syscalls.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010038
Hiroshi Shimamoto41af86f2008-12-17 18:50:32 -080039#include <asm/sigframe.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Harvey Harrison1a176802008-02-08 12:09:59 -080041#ifdef CONFIG_X86_32
42# define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
43#else
44# define FIX_EFLAGS __FIX_EFLAGS
45#endif
46
Tejun Heod9a89a22009-02-09 22:17:40 +090047#define COPY(x) do { \
48 get_user_ex(regs->x, &sc->x); \
49} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080050
Tejun Heod9a89a22009-02-09 22:17:40 +090051#define GET_SEG(seg) ({ \
52 unsigned short tmp; \
53 get_user_ex(tmp, &sc->seg); \
54 tmp; \
55})
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080056
Tejun Heod9a89a22009-02-09 22:17:40 +090057#define COPY_SEG(seg) do { \
58 regs->seg = GET_SEG(seg); \
59} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080060
Tejun Heod9a89a22009-02-09 22:17:40 +090061#define COPY_SEG_CPL3(seg) do { \
62 regs->seg = GET_SEG(seg) | 3; \
63} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080064
H. Peter Anvin85139422012-02-19 07:43:09 -080065int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
66 unsigned long *pax)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080067{
68 void __user *buf;
69 unsigned int tmpflags;
70 unsigned int err = 0;
71
72 /* Always make any pending restarted system calls return -EINTR */
73 current_thread_info()->restart_block.fn = do_no_restart_syscall;
74
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080075 get_user_try {
76
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080077#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +090078 set_user_gs(regs, GET_SEG(gs));
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080079 COPY_SEG(fs);
80 COPY_SEG(es);
81 COPY_SEG(ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080082#endif /* CONFIG_X86_32 */
83
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080084 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
85 COPY(dx); COPY(cx); COPY(ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080086
87#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080088 COPY(r8);
89 COPY(r9);
90 COPY(r10);
91 COPY(r11);
92 COPY(r12);
93 COPY(r13);
94 COPY(r14);
95 COPY(r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080096#endif /* CONFIG_X86_64 */
97
98#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080099 COPY_SEG_CPL3(cs);
100 COPY_SEG_CPL3(ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800101#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800102 /* Kernel saves and restores only the CS segment register on signals,
103 * which is the bare minimum needed to allow mixed 32/64-bit code.
104 * App's signal handler can save/restore other segments if needed. */
105 COPY_SEG_CPL3(cs);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800106#endif /* CONFIG_X86_32 */
107
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800108 get_user_ex(tmpflags, &sc->flags);
109 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
110 regs->orig_ax = -1; /* disable syscall checks */
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800111
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800112 get_user_ex(buf, &sc->fpstate);
113 err |= restore_i387_xstate(buf);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800114
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800115 get_user_ex(*pax, &sc->ax);
116 } get_user_catch(err);
117
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800118 return err;
119}
120
H. Peter Anvin85139422012-02-19 07:43:09 -0800121int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
122 struct pt_regs *regs, unsigned long mask)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800123{
124 int err = 0;
125
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800126 put_user_try {
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800127
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800128#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +0900129 put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800130 put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
131 put_user_ex(regs->es, (unsigned int __user *)&sc->es);
132 put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800133#endif /* CONFIG_X86_32 */
134
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800135 put_user_ex(regs->di, &sc->di);
136 put_user_ex(regs->si, &sc->si);
137 put_user_ex(regs->bp, &sc->bp);
138 put_user_ex(regs->sp, &sc->sp);
139 put_user_ex(regs->bx, &sc->bx);
140 put_user_ex(regs->dx, &sc->dx);
141 put_user_ex(regs->cx, &sc->cx);
142 put_user_ex(regs->ax, &sc->ax);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800143#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800144 put_user_ex(regs->r8, &sc->r8);
145 put_user_ex(regs->r9, &sc->r9);
146 put_user_ex(regs->r10, &sc->r10);
147 put_user_ex(regs->r11, &sc->r11);
148 put_user_ex(regs->r12, &sc->r12);
149 put_user_ex(regs->r13, &sc->r13);
150 put_user_ex(regs->r14, &sc->r14);
151 put_user_ex(regs->r15, &sc->r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800152#endif /* CONFIG_X86_64 */
153
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530154 put_user_ex(current->thread.trap_nr, &sc->trapno);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800155 put_user_ex(current->thread.error_code, &sc->err);
156 put_user_ex(regs->ip, &sc->ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800157#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800158 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
159 put_user_ex(regs->flags, &sc->flags);
160 put_user_ex(regs->sp, &sc->sp_at_signal);
161 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800162#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800163 put_user_ex(regs->flags, &sc->flags);
164 put_user_ex(regs->cs, &sc->cs);
165 put_user_ex(0, &sc->gs);
166 put_user_ex(0, &sc->fs);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800167#endif /* CONFIG_X86_32 */
168
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800169 put_user_ex(fpstate, &sc->fpstate);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800170
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800171 /* non-iBCS2 extensions.. */
172 put_user_ex(mask, &sc->oldmask);
173 put_user_ex(current->thread.cr2, &sc->cr2);
174 } put_user_catch(err);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800175
176 return err;
177}
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * Set up a signal frame.
181 */
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800182
183/*
184 * Determine which stack to use..
185 */
Hiroshi Shimamoto1fae0272009-02-27 10:30:32 -0800186static unsigned long align_sigframe(unsigned long sp)
187{
188#ifdef CONFIG_X86_32
189 /*
190 * Align the stack pointer according to the i386 ABI,
191 * i.e. so that on function entry ((sp + 4) & 15) == 0.
192 */
193 sp = ((sp + 4) & -16ul) - 4;
194#else /* !CONFIG_X86_32 */
195 sp = round_down(sp, 16) - 8;
196#endif
197 return sp;
198}
199
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800200static inline void __user *
201get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
202 void __user **fpstate)
203{
204 /* Default to using normal stack */
205 unsigned long sp = regs->sp;
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700206 int onsigstack = on_sig_stack(sp);
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800207
208#ifdef CONFIG_X86_64
209 /* redzone */
210 sp -= 128;
211#endif /* CONFIG_X86_64 */
212
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700213 if (!onsigstack) {
214 /* This is the X/Open sanctioned signal stack switching. */
215 if (ka->sa.sa_flags & SA_ONSTACK) {
Hiroshi Shimamoto0f8f3082009-03-26 10:03:08 -0700216 if (current->sas_ss_size)
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700217 sp = current->sas_ss_sp + current->sas_ss_size;
218 } else {
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800219#ifdef CONFIG_X86_32
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700220 /* This is the legacy signal stack switching. */
221 if ((regs->ss & 0xffff) != __USER_DS &&
222 !(ka->sa.sa_flags & SA_RESTORER) &&
223 ka->sa.sa_restorer)
224 sp = (unsigned long) ka->sa.sa_restorer;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800225#endif /* CONFIG_X86_32 */
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700226 }
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800227 }
228
229 if (used_math()) {
230 sp -= sig_xstate_size;
Hiroshi Shimamoto25051702009-03-02 17:20:01 -0800231#ifdef CONFIG_X86_64
232 sp = round_down(sp, 64);
233#endif /* CONFIG_X86_64 */
234 *fpstate = (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800235 }
236
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700237 sp = align_sigframe(sp - frame_size);
238
239 /*
240 * If we are on the alternate signal stack and would overflow it, don't.
241 * Return an always-bogus address instead so we will die with SIGSEGV.
242 */
243 if (onsigstack && !likely(on_sig_stack(sp)))
244 return (void __user *)-1L;
245
246 /* save i387 state */
247 if (used_math() && save_i387_xstate(*fpstate) < 0)
248 return (void __user *)-1L;
249
250 return (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800251}
252
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800253#ifdef CONFIG_X86_32
254static const struct {
255 u16 poplmovl;
256 u32 val;
257 u16 int80;
258} __attribute__((packed)) retcode = {
259 0xb858, /* popl %eax; movl $..., %eax */
260 __NR_sigreturn,
261 0x80cd, /* int $0x80 */
262};
263
264static const struct {
265 u8 movl;
266 u32 val;
267 u16 int80;
268 u8 pad;
269} __attribute__((packed)) rt_retcode = {
270 0xb8, /* movl $..., %eax */
271 __NR_rt_sigreturn,
272 0x80cd, /* int $0x80 */
273 0
274};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Ingo Molnar7e907f42008-03-06 10:33:08 +0100276static int
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700277__setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
278 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100281 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700283 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700285 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700288 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700290 if (__put_user(sig, &frame->sig))
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 (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700294 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 if (_NSIG_WORDS > 1) {
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700297 if (__copy_to_user(&frame->extramask, &set->sig[1],
298 sizeof(frame->extramask)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700299 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700302 if (current->mm->context.vdso)
Roland McGrath6c3652e2008-01-30 13:30:42 +0100303 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100304 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100305 restorer = &frame->retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (ka->sa.sa_flags & SA_RESTORER)
307 restorer = ka->sa.sa_restorer;
308
309 /* Set up to return from userspace. */
310 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100313 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 *
315 * WE DO NOT USE IT ANY MORE! It's only left here for historical
316 * reasons and because gdb uses it as a signature to notice
317 * signal handler stack frames.
318 */
Hiroshi Shimamoto4a612042008-11-11 19:09:29 -0800319 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700322 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100325 regs->sp = (unsigned long)frame;
326 regs->ip = (unsigned long)ka->sa.sa_handler;
327 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800328 regs->dx = 0;
329 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100331 regs->ds = __USER_DS;
332 regs->es = __USER_DS;
333 regs->ss = __USER_DS;
334 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
David Howells283828f2006-01-18 17:44:00 -0800336 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700339static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
340 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100343 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700345 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700347 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700350 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800352 put_user_try {
353 put_user_ex(sig, &frame->sig);
354 put_user_ex(&frame->info, &frame->pinfo);
355 put_user_ex(&frame->uc, &frame->puc);
356 err |= copy_siginfo_to_user(&frame->info, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800358 /* Create the ucontext. */
359 if (cpu_has_xsave)
360 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
361 else
362 put_user_ex(0, &frame->uc.uc_flags);
363 put_user_ex(0, &frame->uc.uc_link);
364 put_user_ex(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
365 put_user_ex(sas_ss_flags(regs->sp),
366 &frame->uc.uc_stack.ss_flags);
367 put_user_ex(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
368 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
369 regs, set->sig[0]);
370 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800372 /* Set up to return from userspace. */
373 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
374 if (ka->sa.sa_flags & SA_RESTORER)
375 restorer = ka->sa.sa_restorer;
376 put_user_ex(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100377
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800378 /*
379 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
380 *
381 * WE DO NOT USE IT ANY MORE! It's only left here for historical
382 * reasons and because gdb uses it as a signature to notice
383 * signal handler stack frames.
384 */
385 put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
386 } put_user_catch(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700389 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100392 regs->sp = (unsigned long)frame;
393 regs->ip = (unsigned long)ka->sa.sa_handler;
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700394 regs->ax = (unsigned long)sig;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100395 regs->dx = (unsigned long)&frame->info;
396 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100398 regs->ds = __USER_DS;
399 regs->es = __USER_DS;
400 regs->ss = __USER_DS;
401 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
David Howells283828f2006-01-18 17:44:00 -0800403 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800405#else /* !CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800406static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
407 sigset_t *set, struct pt_regs *regs)
408{
409 struct rt_sigframe __user *frame;
410 void __user *fp = NULL;
411 int err = 0;
412 struct task_struct *me = current;
413
Hiroshi Shimamoto97286a22009-02-27 10:28:48 -0800414 frame = get_sigframe(ka, regs, sizeof(struct rt_sigframe), &fp);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800415
416 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
417 return -EFAULT;
418
419 if (ka->sa.sa_flags & SA_SIGINFO) {
420 if (copy_siginfo_to_user(&frame->info, info))
421 return -EFAULT;
422 }
423
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800424 put_user_try {
425 /* Create the ucontext. */
426 if (cpu_has_xsave)
427 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
428 else
429 put_user_ex(0, &frame->uc.uc_flags);
430 put_user_ex(0, &frame->uc.uc_link);
431 put_user_ex(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
432 put_user_ex(sas_ss_flags(regs->sp),
433 &frame->uc.uc_stack.ss_flags);
434 put_user_ex(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
435 err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
436 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800437
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800438 /* Set up to return from userspace. If provided, use a stub
439 already in userspace. */
440 /* x86-64 should always use SA_RESTORER. */
441 if (ka->sa.sa_flags & SA_RESTORER) {
442 put_user_ex(ka->sa.sa_restorer, &frame->pretcode);
443 } else {
444 /* could use a vstub here */
445 err |= -EFAULT;
446 }
447 } put_user_catch(err);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800448
449 if (err)
450 return -EFAULT;
451
452 /* Set up registers for signal handler */
453 regs->di = sig;
454 /* In case the signal handler was declared without prototypes */
455 regs->ax = 0;
456
457 /* This also works for non SA_SIGINFO handlers because they expect the
458 next argument after the signal number on the stack. */
459 regs->si = (unsigned long)&frame->info;
460 regs->dx = (unsigned long)&frame->uc;
461 regs->ip = (unsigned long) ka->sa.sa_handler;
462
463 regs->sp = (unsigned long)frame;
464
465 /* Set up the CS register to run signal handlers in 64-bit mode,
466 even if the handler happens to be interrupting 32-bit code. */
467 regs->cs = __USER_CS;
468
469 return 0;
470}
471#endif /* CONFIG_X86_32 */
472
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800473#ifdef CONFIG_X86_32
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800474/*
475 * Atomically swap in the new signal mask, and wait for a signal.
476 */
477asmlinkage int
478sys_sigsuspend(int history0, int history1, old_sigset_t mask)
479{
Oleg Nesterov39822942011-07-10 21:27:27 +0200480 sigset_t blocked;
481
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800482 current->saved_sigmask = current->blocked;
Oleg Nesterov39822942011-07-10 21:27:27 +0200483
484 mask &= _BLOCKABLE;
485 siginitset(&blocked, mask);
486 set_current_blocked(&blocked);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800487
488 current->state = TASK_INTERRUPTIBLE;
489 schedule();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800490
Oleg Nesterov39822942011-07-10 21:27:27 +0200491 set_restore_sigmask();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800492 return -ERESTARTNOHAND;
493}
494
495asmlinkage int
496sys_sigaction(int sig, const struct old_sigaction __user *act,
497 struct old_sigaction __user *oact)
498{
499 struct k_sigaction new_ka, old_ka;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800500 int ret = 0;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800501
502 if (act) {
503 old_sigset_t mask;
504
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800505 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800506 return -EFAULT;
507
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800508 get_user_try {
509 get_user_ex(new_ka.sa.sa_handler, &act->sa_handler);
510 get_user_ex(new_ka.sa.sa_flags, &act->sa_flags);
511 get_user_ex(mask, &act->sa_mask);
512 get_user_ex(new_ka.sa.sa_restorer, &act->sa_restorer);
513 } get_user_catch(ret);
514
515 if (ret)
516 return -EFAULT;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800517 siginitset(&new_ka.sa.sa_mask, mask);
518 }
519
520 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
521
522 if (!ret && oact) {
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800523 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800524 return -EFAULT;
525
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800526 put_user_try {
527 put_user_ex(old_ka.sa.sa_handler, &oact->sa_handler);
528 put_user_ex(old_ka.sa.sa_flags, &oact->sa_flags);
529 put_user_ex(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
530 put_user_ex(old_ka.sa.sa_restorer, &oact->sa_restorer);
531 } put_user_catch(ret);
532
533 if (ret)
534 return -EFAULT;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800535 }
536
537 return ret;
538}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800539#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800540
Brian Gerst052acad2009-12-09 19:01:54 -0500541long
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800542sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
543 struct pt_regs *regs)
544{
545 return do_sigaltstack(uss, uoss, regs->sp);
546}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800547
548/*
549 * Do a signal return; undo the signal stack.
550 */
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800551#ifdef CONFIG_X86_32
Brian Gerstb12bdaf2009-02-11 16:43:58 -0500552unsigned long sys_sigreturn(struct pt_regs *regs)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800553{
554 struct sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800555 unsigned long ax;
556 sigset_t set;
557
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800558 frame = (struct sigframe __user *)(regs->sp - 8);
559
560 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
561 goto badframe;
562 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
563 && __copy_from_user(&set.sig[1], &frame->extramask,
564 sizeof(frame->extramask))))
565 goto badframe;
566
567 sigdelsetmask(&set, ~_BLOCKABLE);
Oleg Nesterov39822942011-07-10 21:27:27 +0200568 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800569
570 if (restore_sigcontext(regs, &frame->sc, &ax))
571 goto badframe;
572 return ax;
573
574badframe:
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800575 signal_fault(regs, frame, "sigreturn");
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800576
577 return 0;
578}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800579#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800580
H. Peter Anvin74452502009-02-11 16:31:40 -0800581long sys_rt_sigreturn(struct pt_regs *regs)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800582{
583 struct rt_sigframe __user *frame;
584 unsigned long ax;
585 sigset_t set;
586
587 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
588 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
589 goto badframe;
590 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
591 goto badframe;
592
593 sigdelsetmask(&set, ~_BLOCKABLE);
Oleg Nesterove9bd3f02011-04-27 21:09:39 +0200594 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800595
596 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
597 goto badframe;
598
599 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
600 goto badframe;
601
602 return ax;
603
604badframe:
605 signal_fault(regs, frame, "rt_sigreturn");
606 return 0;
607}
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609/*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100610 * OK, we're invoking a handler:
611 */
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700612static int signr_convert(int sig)
613{
Hiroshi Shimamoto96bf84b2008-10-29 18:44:08 -0700614#ifdef CONFIG_X86_32
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700615 struct thread_info *info = current_thread_info();
616
617 if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
618 return info->exec_domain->signal_invmap[sig];
Hiroshi Shimamoto96bf84b2008-10-29 18:44:08 -0700619#endif /* CONFIG_X86_32 */
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700620 return sig;
621}
622
Hiroshi Shimamotocabf5032008-10-29 18:46:07 -0700623#ifdef CONFIG_X86_32
624
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700625#define is_ia32 1
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700626#define ia32_setup_frame __setup_frame
627#define ia32_setup_rt_frame __setup_rt_frame
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700628
Hiroshi Shimamotocabf5032008-10-29 18:46:07 -0700629#else /* !CONFIG_X86_32 */
630
631#ifdef CONFIG_IA32_EMULATION
632#define is_ia32 test_thread_flag(TIF_IA32)
633#else /* !CONFIG_IA32_EMULATION */
634#define is_ia32 0
635#endif /* CONFIG_IA32_EMULATION */
636
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800637#ifdef CONFIG_X86_X32_ABI
638#define is_x32 test_thread_flag(TIF_X32)
639
640static int x32_setup_rt_frame(int sig, struct k_sigaction *ka,
641 siginfo_t *info, compat_sigset_t *set,
642 struct pt_regs *regs);
643#else /* !CONFIG_X86_X32_ABI */
644#define is_x32 0
645#endif /* CONFIG_X86_X32_ABI */
646
Hiroshi Shimamotof5223762008-12-17 18:47:17 -0800647int ia32_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
648 sigset_t *set, struct pt_regs *regs);
649int ia32_setup_frame(int sig, struct k_sigaction *ka,
650 sigset_t *set, struct pt_regs *regs);
651
Hiroshi Shimamotocabf5032008-10-29 18:46:07 -0700652#endif /* CONFIG_X86_32 */
653
Roland McGrath7c1def12005-06-23 00:08:21 -0700654static int
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700655setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
Oleg Nesterov9b429622011-07-10 20:22:03 +0200656 struct pt_regs *regs)
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700657{
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700658 int usig = signr_convert(sig);
Oleg Nesterov9b429622011-07-10 20:22:03 +0200659 sigset_t *set = &current->blocked;
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700660 int ret;
661
Oleg Nesterov9b429622011-07-10 20:22:03 +0200662 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
663 set = &current->saved_sigmask;
664
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700665 /* Set up the stack frame */
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700666 if (is_ia32) {
667 if (ka->sa.sa_flags & SA_SIGINFO)
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700668 ret = ia32_setup_rt_frame(usig, ka, info, set, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700669 else
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700670 ret = ia32_setup_frame(usig, ka, set, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800671#ifdef CONFIG_X86_X32_ABI
672 } else if (is_x32) {
673 ret = x32_setup_rt_frame(usig, ka, info,
674 (compat_sigset_t *)set, regs);
675#endif
676 } else {
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700677 ret = __setup_rt_frame(sig, ka, info, set, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800678 }
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700679
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700680 if (ret) {
681 force_sigsegv(sig, current);
682 return -EFAULT;
683 }
684
Oleg Nesterov9b429622011-07-10 20:22:03 +0200685 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700686 return ret;
687}
688
689static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
Oleg Nesterov9b429622011-07-10 20:22:03 +0200691 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Roland McGrath7c1def12005-06-23 00:08:21 -0700693 int ret;
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /* Are we from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700696 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 /* If so, check system call restarting.. */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700698 switch (syscall_get_error(current, regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800699 case -ERESTART_RESTARTBLOCK:
700 case -ERESTARTNOHAND:
701 regs->ax = -EINTR;
702 break;
703
704 case -ERESTARTSYS:
705 if (!(ka->sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100706 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800708 }
709 /* fallthrough */
710 case -ERESTARTNOINTR:
711 regs->ax = regs->orig_ax;
712 regs->ip -= 2;
713 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715 }
716
717 /*
Roland McGrathe1f28772008-01-30 13:30:50 +0100718 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
719 * flag so that register information in the sigcontext is correct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100721 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
Roland McGrathe1f28772008-01-30 13:30:50 +0100722 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100723 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Oleg Nesterov9b429622011-07-10 20:22:03 +0200725 ret = setup_rt_frame(sig, ka, info, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Ingo Molnar7e907f42008-03-06 10:33:08 +0100727 if (ret)
728 return ret;
Roland McGrath7c1def12005-06-23 00:08:21 -0700729
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700730 /*
731 * Clear the direction flag as per the ABI for function entry.
732 */
733 regs->flags &= ~X86_EFLAGS_DF;
734
735 /*
736 * Clear TF when entering the signal handler, but
737 * notify any tracer that was single-stepping it.
738 * The tracer may want to single-step inside the
739 * handler too.
740 */
741 regs->flags &= ~X86_EFLAGS_TF;
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700742
Matt Fleming5e6292c2012-01-10 15:11:17 -0800743 block_sigmask(ka, sig);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100744
Roland McGrath36a03302008-03-14 17:46:38 -0700745 tracehook_signal_handler(sig, info, ka, regs,
746 test_thread_flag(TIF_SINGLESTEP));
747
Ingo Molnar7e907f42008-03-06 10:33:08 +0100748 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700751#ifdef CONFIG_X86_32
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700752#define NR_restart_syscall __NR_restart_syscall
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700753#else /* !CONFIG_X86_32 */
754#define NR_restart_syscall \
755 test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
756#endif /* CONFIG_X86_32 */
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758/*
759 * Note that 'init' is a special process: it doesn't get signals it doesn't
760 * want to handle. Thus you cannot kill init even with a SIGKILL even by
761 * mistake.
762 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100763static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800765 struct k_sigaction ka;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 siginfo_t info;
767 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 /*
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800770 * We want the common case to go fast, which is why we may in certain
771 * cases get here from kernel mode. Just return without doing anything
772 * if so.
773 * X86_32: vm86 regs switched out by assembly code before reaching
774 * here, so testing against kernel CS suffices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 */
Vincent Hanquez717b5942005-06-23 00:08:45 -0700776 if (!user_mode(regs))
David Howells283828f2006-01-18 17:44:00 -0800777 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
780 if (signr > 0) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100781 /* Whee! Actually deliver the signal. */
Oleg Nesterov9b429622011-07-10 20:22:03 +0200782 handle_signal(signr, &info, &ka, regs);
David Howells283828f2006-01-18 17:44:00 -0800783 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 /* Did we come from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700787 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 /* Restart the system call - no handlers present */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700789 switch (syscall_get_error(current, regs)) {
David Howells283828f2006-01-18 17:44:00 -0800790 case -ERESTARTNOHAND:
791 case -ERESTARTSYS:
792 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100793 regs->ax = regs->orig_ax;
794 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800795 break;
796
797 case -ERESTART_RESTARTBLOCK:
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700798 regs->ax = NR_restart_syscall;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100799 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800800 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802 }
David Howells283828f2006-01-18 17:44:00 -0800803
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800804 /*
805 * If there's no signal to deliver, we just put the saved sigmask
806 * back.
807 */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700808 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
809 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
Oleg Nesterov9b429622011-07-10 20:22:03 +0200810 set_current_blocked(&current->saved_sigmask);
David Howells283828f2006-01-18 17:44:00 -0800811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
814/*
815 * notification of userspace execution resumption
David Howells283828f2006-01-18 17:44:00 -0800816 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100818void
819do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
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);
David Howellsee18d642009-09-02 09:14:21 +0100839 if (current->replacement_session_keyring)
840 key_replace_session_keyring();
Roland McGrath59e52132008-04-19 19:10:57 -0700841 }
Avi Kivity7c68af62009-09-19 09:40:22 +0300842 if (thread_info_flags & _TIF_USER_RETURN_NOTIFY)
843 fire_user_return_notifiers();
Roland McGrath59e52132008-04-19 19:10:57 -0700844
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700845#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 clear_thread_flag(TIF_IRET);
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700847#endif /* CONFIG_X86_32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700849
850void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
851{
852 struct task_struct *me = current;
853
854 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800855 printk("%s"
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700856 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800857 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700858 me->comm, me->pid, where, frame,
859 regs->ip, regs->sp, regs->orig_ax);
860 print_vma_addr(" in ", regs->ip);
861 printk(KERN_CONT "\n");
862 }
863
864 force_sig(SIGSEGV, me);
865}
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800866
867#ifdef CONFIG_X86_X32_ABI
868static int x32_setup_rt_frame(int sig, struct k_sigaction *ka,
869 siginfo_t *info, compat_sigset_t *set,
870 struct pt_regs *regs)
871{
872 struct rt_sigframe_x32 __user *frame;
873 void __user *restorer;
874 int err = 0;
875 void __user *fpstate = NULL;
876
877 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
878
879 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
880 return -EFAULT;
881
882 if (ka->sa.sa_flags & SA_SIGINFO) {
883 if (copy_siginfo_to_user32(&frame->info, info))
884 return -EFAULT;
885 }
886
887 put_user_try {
888 /* Create the ucontext. */
889 if (cpu_has_xsave)
890 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
891 else
892 put_user_ex(0, &frame->uc.uc_flags);
893 put_user_ex(0, &frame->uc.uc_link);
894 put_user_ex(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
895 put_user_ex(sas_ss_flags(regs->sp),
896 &frame->uc.uc_stack.ss_flags);
897 put_user_ex(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
898 put_user_ex(0, &frame->uc.uc__pad0);
899 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
900 regs, set->sig[0]);
901 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
902
903 if (ka->sa.sa_flags & SA_RESTORER) {
904 restorer = ka->sa.sa_restorer;
905 } else {
906 /* could use a vstub here */
907 restorer = NULL;
908 err |= -EFAULT;
909 }
910 put_user_ex(restorer, &frame->pretcode);
911 } put_user_catch(err);
912
913 if (err)
914 return -EFAULT;
915
916 /* Set up registers for signal handler */
917 regs->sp = (unsigned long) frame;
918 regs->ip = (unsigned long) ka->sa.sa_handler;
919
920 /* We use the x32 calling convention here... */
921 regs->di = sig;
922 regs->si = (unsigned long) &frame->info;
923 regs->dx = (unsigned long) &frame->uc;
924
925 loadsegment(ds, __USER_DS);
926 loadsegment(es, __USER_DS);
927
928 regs->cs = __USER_CS;
929 regs->ss = __USER_DS;
930
931 return 0;
932}
933
934asmlinkage long sys32_x32_rt_sigreturn(struct pt_regs *regs)
935{
936 struct rt_sigframe_x32 __user *frame;
937 sigset_t set;
938 unsigned long ax;
939 struct pt_regs tregs;
940
941 frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
942
943 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
944 goto badframe;
945 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
946 goto badframe;
947
948 sigdelsetmask(&set, ~_BLOCKABLE);
949 set_current_blocked(&set);
950
951 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
952 goto badframe;
953
954 tregs = *regs;
955 if (sys32_sigaltstack(&frame->uc.uc_stack, NULL, &tregs) == -EFAULT)
956 goto badframe;
957
958 return ax;
959
960badframe:
961 signal_fault(regs, frame, "x32 rt_sigreturn");
962 return 0;
963}
964#endif