blob: 40a24932a8a152878abfbf8ff6210a28892e5e2d [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>
13#include <linux/signal.h>
14#include <linux/errno.h>
15#include <linux/wait.h>
16#include <linux/ptrace.h>
17#include <linux/tracehook.h>
18#include <linux/unistd.h>
19#include <linux/stddef.h>
20#include <linux/personality.h>
21#include <linux/uaccess.h>
Avi Kivity7c68af62009-09-19 09:40:22 +030022#include <linux/user-return-notifier.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/processor.h>
25#include <asm/ucontext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/i387.h>
Roland McGrath6c3652e2008-01-30 13:30:42 +010027#include <asm/vdso.h>
Andi Kleen4efc0672009-04-28 19:07:31 +020028#include <asm/mce.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080029
30#ifdef CONFIG_X86_64
31#include <asm/proto.h>
32#include <asm/ia32_unistd.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080033#endif /* CONFIG_X86_64 */
34
Hiroshi Shimamotobb579252008-09-05 16:26:55 -070035#include <asm/syscall.h>
Jaswinder Singhbbc1f692008-07-21 21:34:13 +053036#include <asm/syscalls.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010037
Hiroshi Shimamoto41af86f2008-12-17 18:50:32 -080038#include <asm/sigframe.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
41
Harvey Harrison1a176802008-02-08 12:09:59 -080042#define __FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | \
43 X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
44 X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
45 X86_EFLAGS_CF)
46
47#ifdef CONFIG_X86_32
48# define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
49#else
50# define FIX_EFLAGS __FIX_EFLAGS
51#endif
52
Tejun Heod9a89a22009-02-09 22:17:40 +090053#define COPY(x) do { \
54 get_user_ex(regs->x, &sc->x); \
55} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080056
Tejun Heod9a89a22009-02-09 22:17:40 +090057#define GET_SEG(seg) ({ \
58 unsigned short tmp; \
59 get_user_ex(tmp, &sc->seg); \
60 tmp; \
61})
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080062
Tejun Heod9a89a22009-02-09 22:17:40 +090063#define COPY_SEG(seg) do { \
64 regs->seg = GET_SEG(seg); \
65} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080066
Tejun Heod9a89a22009-02-09 22:17:40 +090067#define COPY_SEG_CPL3(seg) do { \
68 regs->seg = GET_SEG(seg) | 3; \
69} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080070
71static int
72restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
73 unsigned long *pax)
74{
75 void __user *buf;
76 unsigned int tmpflags;
77 unsigned int err = 0;
78
79 /* Always make any pending restarted system calls return -EINTR */
80 current_thread_info()->restart_block.fn = do_no_restart_syscall;
81
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080082 get_user_try {
83
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080084#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +090085 set_user_gs(regs, GET_SEG(gs));
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080086 COPY_SEG(fs);
87 COPY_SEG(es);
88 COPY_SEG(ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080089#endif /* CONFIG_X86_32 */
90
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080091 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
92 COPY(dx); COPY(cx); COPY(ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080093
94#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080095 COPY(r8);
96 COPY(r9);
97 COPY(r10);
98 COPY(r11);
99 COPY(r12);
100 COPY(r13);
101 COPY(r14);
102 COPY(r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800103#endif /* CONFIG_X86_64 */
104
105#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800106 COPY_SEG_CPL3(cs);
107 COPY_SEG_CPL3(ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800108#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800109 /* Kernel saves and restores only the CS segment register on signals,
110 * which is the bare minimum needed to allow mixed 32/64-bit code.
111 * App's signal handler can save/restore other segments if needed. */
112 COPY_SEG_CPL3(cs);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800113#endif /* CONFIG_X86_32 */
114
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800115 get_user_ex(tmpflags, &sc->flags);
116 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
117 regs->orig_ax = -1; /* disable syscall checks */
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800118
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800119 get_user_ex(buf, &sc->fpstate);
120 err |= restore_i387_xstate(buf);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800121
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800122 get_user_ex(*pax, &sc->ax);
123 } get_user_catch(err);
124
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800125 return err;
126}
127
128static int
129setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
130 struct pt_regs *regs, unsigned long mask)
131{
132 int err = 0;
133
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800134 put_user_try {
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800135
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800136#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +0900137 put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800138 put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
139 put_user_ex(regs->es, (unsigned int __user *)&sc->es);
140 put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800141#endif /* CONFIG_X86_32 */
142
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800143 put_user_ex(regs->di, &sc->di);
144 put_user_ex(regs->si, &sc->si);
145 put_user_ex(regs->bp, &sc->bp);
146 put_user_ex(regs->sp, &sc->sp);
147 put_user_ex(regs->bx, &sc->bx);
148 put_user_ex(regs->dx, &sc->dx);
149 put_user_ex(regs->cx, &sc->cx);
150 put_user_ex(regs->ax, &sc->ax);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800151#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800152 put_user_ex(regs->r8, &sc->r8);
153 put_user_ex(regs->r9, &sc->r9);
154 put_user_ex(regs->r10, &sc->r10);
155 put_user_ex(regs->r11, &sc->r11);
156 put_user_ex(regs->r12, &sc->r12);
157 put_user_ex(regs->r13, &sc->r13);
158 put_user_ex(regs->r14, &sc->r14);
159 put_user_ex(regs->r15, &sc->r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800160#endif /* CONFIG_X86_64 */
161
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800162 put_user_ex(current->thread.trap_no, &sc->trapno);
163 put_user_ex(current->thread.error_code, &sc->err);
164 put_user_ex(regs->ip, &sc->ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800165#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800166 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
167 put_user_ex(regs->flags, &sc->flags);
168 put_user_ex(regs->sp, &sc->sp_at_signal);
169 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800170#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800171 put_user_ex(regs->flags, &sc->flags);
172 put_user_ex(regs->cs, &sc->cs);
173 put_user_ex(0, &sc->gs);
174 put_user_ex(0, &sc->fs);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800175#endif /* CONFIG_X86_32 */
176
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800177 put_user_ex(fpstate, &sc->fpstate);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800178
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800179 /* non-iBCS2 extensions.. */
180 put_user_ex(mask, &sc->oldmask);
181 put_user_ex(current->thread.cr2, &sc->cr2);
182 } put_user_catch(err);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800183
184 return err;
185}
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 * Set up a signal frame.
189 */
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800190
191/*
192 * Determine which stack to use..
193 */
Hiroshi Shimamoto1fae0272009-02-27 10:30:32 -0800194static unsigned long align_sigframe(unsigned long sp)
195{
196#ifdef CONFIG_X86_32
197 /*
198 * Align the stack pointer according to the i386 ABI,
199 * i.e. so that on function entry ((sp + 4) & 15) == 0.
200 */
201 sp = ((sp + 4) & -16ul) - 4;
202#else /* !CONFIG_X86_32 */
203 sp = round_down(sp, 16) - 8;
204#endif
205 return sp;
206}
207
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800208static inline void __user *
209get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
210 void __user **fpstate)
211{
212 /* Default to using normal stack */
213 unsigned long sp = regs->sp;
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700214 int onsigstack = on_sig_stack(sp);
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800215
216#ifdef CONFIG_X86_64
217 /* redzone */
218 sp -= 128;
219#endif /* CONFIG_X86_64 */
220
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700221 if (!onsigstack) {
222 /* This is the X/Open sanctioned signal stack switching. */
223 if (ka->sa.sa_flags & SA_ONSTACK) {
Hiroshi Shimamoto0f8f3082009-03-26 10:03:08 -0700224 if (current->sas_ss_size)
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700225 sp = current->sas_ss_sp + current->sas_ss_size;
226 } else {
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800227#ifdef CONFIG_X86_32
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700228 /* This is the legacy signal stack switching. */
229 if ((regs->ss & 0xffff) != __USER_DS &&
230 !(ka->sa.sa_flags & SA_RESTORER) &&
231 ka->sa.sa_restorer)
232 sp = (unsigned long) ka->sa.sa_restorer;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800233#endif /* CONFIG_X86_32 */
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700234 }
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800235 }
236
237 if (used_math()) {
238 sp -= sig_xstate_size;
Hiroshi Shimamoto25051702009-03-02 17:20:01 -0800239#ifdef CONFIG_X86_64
240 sp = round_down(sp, 64);
241#endif /* CONFIG_X86_64 */
242 *fpstate = (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800243 }
244
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700245 sp = align_sigframe(sp - frame_size);
246
247 /*
248 * If we are on the alternate signal stack and would overflow it, don't.
249 * Return an always-bogus address instead so we will die with SIGSEGV.
250 */
251 if (onsigstack && !likely(on_sig_stack(sp)))
252 return (void __user *)-1L;
253
254 /* save i387 state */
255 if (used_math() && save_i387_xstate(*fpstate) < 0)
256 return (void __user *)-1L;
257
258 return (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800259}
260
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800261#ifdef CONFIG_X86_32
262static const struct {
263 u16 poplmovl;
264 u32 val;
265 u16 int80;
266} __attribute__((packed)) retcode = {
267 0xb858, /* popl %eax; movl $..., %eax */
268 __NR_sigreturn,
269 0x80cd, /* int $0x80 */
270};
271
272static const struct {
273 u8 movl;
274 u32 val;
275 u16 int80;
276 u8 pad;
277} __attribute__((packed)) rt_retcode = {
278 0xb8, /* movl $..., %eax */
279 __NR_rt_sigreturn,
280 0x80cd, /* int $0x80 */
281 0
282};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Ingo Molnar7e907f42008-03-06 10:33:08 +0100284static int
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700285__setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
286 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100289 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700291 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700293 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700296 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700298 if (__put_user(sig, &frame->sig))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700299 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700301 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700302 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 if (_NSIG_WORDS > 1) {
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700305 if (__copy_to_user(&frame->extramask, &set->sig[1],
306 sizeof(frame->extramask)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700307 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
309
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700310 if (current->mm->context.vdso)
Roland McGrath6c3652e2008-01-30 13:30:42 +0100311 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100312 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100313 restorer = &frame->retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (ka->sa.sa_flags & SA_RESTORER)
315 restorer = ka->sa.sa_restorer;
316
317 /* Set up to return from userspace. */
318 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100321 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 *
323 * WE DO NOT USE IT ANY MORE! It's only left here for historical
324 * reasons and because gdb uses it as a signature to notice
325 * signal handler stack frames.
326 */
Hiroshi Shimamoto4a612042008-11-11 19:09:29 -0800327 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700330 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100333 regs->sp = (unsigned long)frame;
334 regs->ip = (unsigned long)ka->sa.sa_handler;
335 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800336 regs->dx = 0;
337 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100339 regs->ds = __USER_DS;
340 regs->es = __USER_DS;
341 regs->ss = __USER_DS;
342 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
David Howells283828f2006-01-18 17:44:00 -0800344 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700347static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
348 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100351 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700353 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700355 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700358 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800360 put_user_try {
361 put_user_ex(sig, &frame->sig);
362 put_user_ex(&frame->info, &frame->pinfo);
363 put_user_ex(&frame->uc, &frame->puc);
364 err |= copy_siginfo_to_user(&frame->info, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800366 /* Create the ucontext. */
367 if (cpu_has_xsave)
368 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
369 else
370 put_user_ex(0, &frame->uc.uc_flags);
371 put_user_ex(0, &frame->uc.uc_link);
372 put_user_ex(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
373 put_user_ex(sas_ss_flags(regs->sp),
374 &frame->uc.uc_stack.ss_flags);
375 put_user_ex(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
376 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
377 regs, set->sig[0]);
378 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800380 /* Set up to return from userspace. */
381 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
382 if (ka->sa.sa_flags & SA_RESTORER)
383 restorer = ka->sa.sa_restorer;
384 put_user_ex(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100385
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800386 /*
387 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
388 *
389 * WE DO NOT USE IT ANY MORE! It's only left here for historical
390 * reasons and because gdb uses it as a signature to notice
391 * signal handler stack frames.
392 */
393 put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
394 } put_user_catch(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700397 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100400 regs->sp = (unsigned long)frame;
401 regs->ip = (unsigned long)ka->sa.sa_handler;
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700402 regs->ax = (unsigned long)sig;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100403 regs->dx = (unsigned long)&frame->info;
404 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100406 regs->ds = __USER_DS;
407 regs->es = __USER_DS;
408 regs->ss = __USER_DS;
409 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
David Howells283828f2006-01-18 17:44:00 -0800411 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800413#else /* !CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800414static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
415 sigset_t *set, struct pt_regs *regs)
416{
417 struct rt_sigframe __user *frame;
418 void __user *fp = NULL;
419 int err = 0;
420 struct task_struct *me = current;
421
Hiroshi Shimamoto97286a22009-02-27 10:28:48 -0800422 frame = get_sigframe(ka, regs, sizeof(struct rt_sigframe), &fp);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800423
424 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
425 return -EFAULT;
426
427 if (ka->sa.sa_flags & SA_SIGINFO) {
428 if (copy_siginfo_to_user(&frame->info, info))
429 return -EFAULT;
430 }
431
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800432 put_user_try {
433 /* Create the ucontext. */
434 if (cpu_has_xsave)
435 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
436 else
437 put_user_ex(0, &frame->uc.uc_flags);
438 put_user_ex(0, &frame->uc.uc_link);
439 put_user_ex(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
440 put_user_ex(sas_ss_flags(regs->sp),
441 &frame->uc.uc_stack.ss_flags);
442 put_user_ex(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
443 err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
444 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800445
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800446 /* Set up to return from userspace. If provided, use a stub
447 already in userspace. */
448 /* x86-64 should always use SA_RESTORER. */
449 if (ka->sa.sa_flags & SA_RESTORER) {
450 put_user_ex(ka->sa.sa_restorer, &frame->pretcode);
451 } else {
452 /* could use a vstub here */
453 err |= -EFAULT;
454 }
455 } put_user_catch(err);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800456
457 if (err)
458 return -EFAULT;
459
460 /* Set up registers for signal handler */
461 regs->di = sig;
462 /* In case the signal handler was declared without prototypes */
463 regs->ax = 0;
464
465 /* This also works for non SA_SIGINFO handlers because they expect the
466 next argument after the signal number on the stack. */
467 regs->si = (unsigned long)&frame->info;
468 regs->dx = (unsigned long)&frame->uc;
469 regs->ip = (unsigned long) ka->sa.sa_handler;
470
471 regs->sp = (unsigned long)frame;
472
473 /* Set up the CS register to run signal handlers in 64-bit mode,
474 even if the handler happens to be interrupting 32-bit code. */
475 regs->cs = __USER_CS;
476
477 return 0;
478}
479#endif /* CONFIG_X86_32 */
480
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800481#ifdef CONFIG_X86_32
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800482/*
483 * Atomically swap in the new signal mask, and wait for a signal.
484 */
485asmlinkage int
486sys_sigsuspend(int history0, int history1, old_sigset_t mask)
487{
488 mask &= _BLOCKABLE;
489 spin_lock_irq(&current->sighand->siglock);
490 current->saved_sigmask = current->blocked;
491 siginitset(&current->blocked, mask);
492 recalc_sigpending();
493 spin_unlock_irq(&current->sighand->siglock);
494
495 current->state = TASK_INTERRUPTIBLE;
496 schedule();
497 set_restore_sigmask();
498
499 return -ERESTARTNOHAND;
500}
501
502asmlinkage int
503sys_sigaction(int sig, const struct old_sigaction __user *act,
504 struct old_sigaction __user *oact)
505{
506 struct k_sigaction new_ka, old_ka;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800507 int ret = 0;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800508
509 if (act) {
510 old_sigset_t mask;
511
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800512 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800513 return -EFAULT;
514
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800515 get_user_try {
516 get_user_ex(new_ka.sa.sa_handler, &act->sa_handler);
517 get_user_ex(new_ka.sa.sa_flags, &act->sa_flags);
518 get_user_ex(mask, &act->sa_mask);
519 get_user_ex(new_ka.sa.sa_restorer, &act->sa_restorer);
520 } get_user_catch(ret);
521
522 if (ret)
523 return -EFAULT;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800524 siginitset(&new_ka.sa.sa_mask, mask);
525 }
526
527 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
528
529 if (!ret && oact) {
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800530 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800531 return -EFAULT;
532
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800533 put_user_try {
534 put_user_ex(old_ka.sa.sa_handler, &oact->sa_handler);
535 put_user_ex(old_ka.sa.sa_flags, &oact->sa_flags);
536 put_user_ex(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
537 put_user_ex(old_ka.sa.sa_restorer, &oact->sa_restorer);
538 } put_user_catch(ret);
539
540 if (ret)
541 return -EFAULT;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800542 }
543
544 return ret;
545}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800546#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800547
Brian Gerst052acad2009-12-09 19:01:54 -0500548long
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800549sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
550 struct pt_regs *regs)
551{
552 return do_sigaltstack(uss, uoss, regs->sp);
553}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800554
555/*
556 * Do a signal return; undo the signal stack.
557 */
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800558#ifdef CONFIG_X86_32
Brian Gerstb12bdaf2009-02-11 16:43:58 -0500559unsigned long sys_sigreturn(struct pt_regs *regs)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800560{
561 struct sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800562 unsigned long ax;
563 sigset_t set;
564
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800565 frame = (struct sigframe __user *)(regs->sp - 8);
566
567 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
568 goto badframe;
569 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
570 && __copy_from_user(&set.sig[1], &frame->extramask,
571 sizeof(frame->extramask))))
572 goto badframe;
573
574 sigdelsetmask(&set, ~_BLOCKABLE);
575 spin_lock_irq(&current->sighand->siglock);
576 current->blocked = set;
577 recalc_sigpending();
578 spin_unlock_irq(&current->sighand->siglock);
579
580 if (restore_sigcontext(regs, &frame->sc, &ax))
581 goto badframe;
582 return ax;
583
584badframe:
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800585 signal_fault(regs, frame, "sigreturn");
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800586
587 return 0;
588}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800589#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800590
H. Peter Anvin74452502009-02-11 16:31:40 -0800591long sys_rt_sigreturn(struct pt_regs *regs)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800592{
593 struct rt_sigframe __user *frame;
594 unsigned long ax;
595 sigset_t set;
596
597 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
598 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
599 goto badframe;
600 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
601 goto badframe;
602
603 sigdelsetmask(&set, ~_BLOCKABLE);
Oleg Nesterove9bd3f02011-04-27 21:09:39 +0200604 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800605
606 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
607 goto badframe;
608
609 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
610 goto badframe;
611
612 return ax;
613
614badframe:
615 signal_fault(regs, frame, "rt_sigreturn");
616 return 0;
617}
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619/*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100620 * OK, we're invoking a handler:
621 */
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700622static int signr_convert(int sig)
623{
Hiroshi Shimamoto96bf84b2008-10-29 18:44:08 -0700624#ifdef CONFIG_X86_32
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700625 struct thread_info *info = current_thread_info();
626
627 if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
628 return info->exec_domain->signal_invmap[sig];
Hiroshi Shimamoto96bf84b2008-10-29 18:44:08 -0700629#endif /* CONFIG_X86_32 */
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700630 return sig;
631}
632
Hiroshi Shimamotocabf5032008-10-29 18:46:07 -0700633#ifdef CONFIG_X86_32
634
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700635#define is_ia32 1
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700636#define ia32_setup_frame __setup_frame
637#define ia32_setup_rt_frame __setup_rt_frame
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700638
Hiroshi Shimamotocabf5032008-10-29 18:46:07 -0700639#else /* !CONFIG_X86_32 */
640
641#ifdef CONFIG_IA32_EMULATION
642#define is_ia32 test_thread_flag(TIF_IA32)
643#else /* !CONFIG_IA32_EMULATION */
644#define is_ia32 0
645#endif /* CONFIG_IA32_EMULATION */
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,
656 sigset_t *set, struct pt_regs *regs)
657{
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700658 int usig = signr_convert(sig);
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700659 int ret;
660
661 /* Set up the stack frame */
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700662 if (is_ia32) {
663 if (ka->sa.sa_flags & SA_SIGINFO)
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700664 ret = ia32_setup_rt_frame(usig, ka, info, set, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700665 else
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700666 ret = ia32_setup_frame(usig, ka, set, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700667 } else
668 ret = __setup_rt_frame(sig, ka, info, set, regs);
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700669
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700670 if (ret) {
671 force_sigsegv(sig, current);
672 return -EFAULT;
673 }
674
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700675 return ret;
676}
677
678static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800680 sigset_t *oldset, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Oleg Nesterove6a58582011-04-27 21:04:28 +0200682 sigset_t blocked;
Roland McGrath7c1def12005-06-23 00:08:21 -0700683 int ret;
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 /* Are we from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700686 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 /* If so, check system call restarting.. */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700688 switch (syscall_get_error(current, regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800689 case -ERESTART_RESTARTBLOCK:
690 case -ERESTARTNOHAND:
691 regs->ax = -EINTR;
692 break;
693
694 case -ERESTARTSYS:
695 if (!(ka->sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100696 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800698 }
699 /* fallthrough */
700 case -ERESTARTNOINTR:
701 regs->ax = regs->orig_ax;
702 regs->ip -= 2;
703 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
705 }
706
707 /*
Roland McGrathe1f28772008-01-30 13:30:50 +0100708 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
709 * flag so that register information in the sigcontext is correct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100711 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
Roland McGrathe1f28772008-01-30 13:30:50 +0100712 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100713 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700715 ret = setup_rt_frame(sig, ka, info, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Ingo Molnar7e907f42008-03-06 10:33:08 +0100717 if (ret)
718 return ret;
Roland McGrath7c1def12005-06-23 00:08:21 -0700719
Hiroshi Shimamoto86d32372008-09-23 17:22:32 -0700720#ifdef CONFIG_X86_64
721 /*
722 * This has nothing to do with segment registers,
723 * despite the name. This magic affects uaccess.h
724 * macros' behavior. Reset it to the normal setting.
725 */
726 set_fs(USER_DS);
727#endif
728
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700729 /*
730 * Clear the direction flag as per the ABI for function entry.
731 */
732 regs->flags &= ~X86_EFLAGS_DF;
733
734 /*
735 * Clear TF when entering the signal handler, but
736 * notify any tracer that was single-stepping it.
737 * The tracer may want to single-step inside the
738 * handler too.
739 */
740 regs->flags &= ~X86_EFLAGS_TF;
Roland McGrath8b9c5ff2008-04-19 14:26:54 -0700741
Oleg Nesterove6a58582011-04-27 21:04:28 +0200742 sigorsets(&blocked, &current->blocked, &ka->sa.sa_mask);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100743 if (!(ka->sa.sa_flags & SA_NODEFER))
Oleg Nesterove6a58582011-04-27 21:04:28 +0200744 sigaddset(&blocked, sig);
745 set_current_blocked(&blocked);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100746
Roland McGrath36a03302008-03-14 17:46:38 -0700747 tracehook_signal_handler(sig, info, ka, regs,
748 test_thread_flag(TIF_SINGLESTEP));
749
Ingo Molnar7e907f42008-03-06 10:33:08 +0100750 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700753#ifdef CONFIG_X86_32
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700754#define NR_restart_syscall __NR_restart_syscall
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700755#else /* !CONFIG_X86_32 */
756#define NR_restart_syscall \
757 test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
758#endif /* CONFIG_X86_32 */
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760/*
761 * Note that 'init' is a special process: it doesn't get signals it doesn't
762 * want to handle. Thus you cannot kill init even with a SIGKILL even by
763 * mistake.
764 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100765static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800767 struct k_sigaction ka;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 siginfo_t info;
769 int signr;
David Howells283828f2006-01-18 17:44:00 -0800770 sigset_t *oldset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 /*
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800773 * We want the common case to go fast, which is why we may in certain
774 * cases get here from kernel mode. Just return without doing anything
775 * if so.
776 * X86_32: vm86 regs switched out by assembly code before reaching
777 * here, so testing against kernel CS suffices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 */
Vincent Hanquez717b5942005-06-23 00:08:45 -0700779 if (!user_mode(regs))
David Howells283828f2006-01-18 17:44:00 -0800780 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700782 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
David Howells283828f2006-01-18 17:44:00 -0800783 oldset = &current->saved_sigmask;
784 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 oldset = &current->blocked;
786
787 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
788 if (signr > 0) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100789 /* Whee! Actually deliver the signal. */
David Howells283828f2006-01-18 17:44:00 -0800790 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100791 /*
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700792 * A signal was successfully delivered; the saved
David Howells283828f2006-01-18 17:44:00 -0800793 * sigmask will have been stored in the signal frame,
794 * and will be restored by sigreturn, so we can simply
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700795 * clear the TS_RESTORE_SIGMASK flag.
Ingo Molnar7e907f42008-03-06 10:33:08 +0100796 */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700797 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
David Howells283828f2006-01-18 17:44:00 -0800798 }
David Howells283828f2006-01-18 17:44:00 -0800799 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 /* Did we come from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700803 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 /* Restart the system call - no handlers present */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700805 switch (syscall_get_error(current, regs)) {
David Howells283828f2006-01-18 17:44:00 -0800806 case -ERESTARTNOHAND:
807 case -ERESTARTSYS:
808 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100809 regs->ax = regs->orig_ax;
810 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800811 break;
812
813 case -ERESTART_RESTARTBLOCK:
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700814 regs->ax = NR_restart_syscall;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100815 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800816 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818 }
David Howells283828f2006-01-18 17:44:00 -0800819
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800820 /*
821 * If there's no signal to deliver, we just put the saved sigmask
822 * back.
823 */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700824 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
825 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
David Howells283828f2006-01-18 17:44:00 -0800826 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
830/*
831 * notification of userspace execution resumption
David Howells283828f2006-01-18 17:44:00 -0800832 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100834void
835do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Andi Kleenc1ebf832009-07-09 00:31:41 +0200837#ifdef CONFIG_X86_MCE
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700838 /* notify userspace of pending MCEs */
839 if (thread_info_flags & _TIF_MCE_NOTIFY)
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200840 mce_notify_process();
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700841#endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 /* deal with pending signal delivery */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700844 if (thread_info_flags & _TIF_SIGPENDING)
David Howells283828f2006-01-18 17:44:00 -0800845 do_signal(regs);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100846
Roland McGrath59e52132008-04-19 19:10:57 -0700847 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
848 clear_thread_flag(TIF_NOTIFY_RESUME);
849 tracehook_notify_resume(regs);
David Howellsee18d642009-09-02 09:14:21 +0100850 if (current->replacement_session_keyring)
851 key_replace_session_keyring();
Roland McGrath59e52132008-04-19 19:10:57 -0700852 }
Avi Kivity7c68af62009-09-19 09:40:22 +0300853 if (thread_info_flags & _TIF_USER_RETURN_NOTIFY)
854 fire_user_return_notifiers();
Roland McGrath59e52132008-04-19 19:10:57 -0700855
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700856#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 clear_thread_flag(TIF_IRET);
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700858#endif /* CONFIG_X86_32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700860
861void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
862{
863 struct task_struct *me = current;
864
865 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800866 printk("%s"
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700867 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800868 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700869 me->comm, me->pid, where, frame,
870 regs->ip, regs->sp, regs->orig_ax);
871 print_vma_addr(" in ", regs->ip);
872 printk(KERN_CONT "\n");
873 }
874
875 force_sig(SIGSEGV, me);
876}