blob: 9c73acc1c86012c6694d931cc98bc7c340010d86 [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>
Ingo Molnar7e907f42008-03-06 10:33:08 +010021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/processor.h>
23#include <asm/ucontext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/i387.h>
Roland McGrath6c3652e2008-01-30 13:30:42 +010025#include <asm/vdso.h>
Andi Kleen4efc0672009-04-28 19:07:31 +020026#include <asm/mce.h>
H. Peter Anvinf28f0c22012-02-19 07:38:43 -080027#include <asm/sighandling.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080028
29#ifdef CONFIG_X86_64
30#include <asm/proto.h>
31#include <asm/ia32_unistd.h>
H. Peter Anvinc5a37392012-02-19 09:41:09 -080032#include <asm/sys_ia32.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
Harvey Harrison1a176802008-02-08 12:09:59 -080040#ifdef CONFIG_X86_32
41# define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF)
42#else
43# define FIX_EFLAGS __FIX_EFLAGS
44#endif
45
Tejun Heod9a89a22009-02-09 22:17:40 +090046#define COPY(x) do { \
47 get_user_ex(regs->x, &sc->x); \
48} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080049
Tejun Heod9a89a22009-02-09 22:17:40 +090050#define GET_SEG(seg) ({ \
51 unsigned short tmp; \
52 get_user_ex(tmp, &sc->seg); \
53 tmp; \
54})
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080055
Tejun Heod9a89a22009-02-09 22:17:40 +090056#define COPY_SEG(seg) do { \
57 regs->seg = GET_SEG(seg); \
58} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080059
Tejun Heod9a89a22009-02-09 22:17:40 +090060#define COPY_SEG_CPL3(seg) do { \
61 regs->seg = GET_SEG(seg) | 3; \
62} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080063
H. Peter Anvin85139422012-02-19 07:43:09 -080064int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
65 unsigned long *pax)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080066{
67 void __user *buf;
68 unsigned int tmpflags;
69 unsigned int err = 0;
70
71 /* Always make any pending restarted system calls return -EINTR */
72 current_thread_info()->restart_block.fn = do_no_restart_syscall;
73
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080074 get_user_try {
75
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080076#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +090077 set_user_gs(regs, GET_SEG(gs));
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080078 COPY_SEG(fs);
79 COPY_SEG(es);
80 COPY_SEG(ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080081#endif /* CONFIG_X86_32 */
82
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080083 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
84 COPY(dx); COPY(cx); COPY(ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080085
86#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080087 COPY(r8);
88 COPY(r9);
89 COPY(r10);
90 COPY(r11);
91 COPY(r12);
92 COPY(r13);
93 COPY(r14);
94 COPY(r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080095#endif /* CONFIG_X86_64 */
96
97#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080098 COPY_SEG_CPL3(cs);
99 COPY_SEG_CPL3(ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800100#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800101 /* Kernel saves and restores only the CS segment register on signals,
102 * which is the bare minimum needed to allow mixed 32/64-bit code.
103 * App's signal handler can save/restore other segments if needed. */
104 COPY_SEG_CPL3(cs);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800105#endif /* CONFIG_X86_32 */
106
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800107 get_user_ex(tmpflags, &sc->flags);
108 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
109 regs->orig_ax = -1; /* disable syscall checks */
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800110
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800111 get_user_ex(buf, &sc->fpstate);
112 err |= restore_i387_xstate(buf);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800113
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800114 get_user_ex(*pax, &sc->ax);
115 } get_user_catch(err);
116
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800117 return err;
118}
119
H. Peter Anvin85139422012-02-19 07:43:09 -0800120int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
121 struct pt_regs *regs, unsigned long mask)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800122{
123 int err = 0;
124
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800125 put_user_try {
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800126
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800127#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +0900128 put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800129 put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
130 put_user_ex(regs->es, (unsigned int __user *)&sc->es);
131 put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800132#endif /* CONFIG_X86_32 */
133
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800134 put_user_ex(regs->di, &sc->di);
135 put_user_ex(regs->si, &sc->si);
136 put_user_ex(regs->bp, &sc->bp);
137 put_user_ex(regs->sp, &sc->sp);
138 put_user_ex(regs->bx, &sc->bx);
139 put_user_ex(regs->dx, &sc->dx);
140 put_user_ex(regs->cx, &sc->cx);
141 put_user_ex(regs->ax, &sc->ax);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800142#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800143 put_user_ex(regs->r8, &sc->r8);
144 put_user_ex(regs->r9, &sc->r9);
145 put_user_ex(regs->r10, &sc->r10);
146 put_user_ex(regs->r11, &sc->r11);
147 put_user_ex(regs->r12, &sc->r12);
148 put_user_ex(regs->r13, &sc->r13);
149 put_user_ex(regs->r14, &sc->r14);
150 put_user_ex(regs->r15, &sc->r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800151#endif /* CONFIG_X86_64 */
152
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530153 put_user_ex(current->thread.trap_nr, &sc->trapno);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800154 put_user_ex(current->thread.error_code, &sc->err);
155 put_user_ex(regs->ip, &sc->ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800156#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800157 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
158 put_user_ex(regs->flags, &sc->flags);
159 put_user_ex(regs->sp, &sc->sp_at_signal);
160 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800161#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800162 put_user_ex(regs->flags, &sc->flags);
163 put_user_ex(regs->cs, &sc->cs);
164 put_user_ex(0, &sc->gs);
165 put_user_ex(0, &sc->fs);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800166#endif /* CONFIG_X86_32 */
167
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800168 put_user_ex(fpstate, &sc->fpstate);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800169
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800170 /* non-iBCS2 extensions.. */
171 put_user_ex(mask, &sc->oldmask);
172 put_user_ex(current->thread.cr2, &sc->cr2);
173 } put_user_catch(err);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800174
175 return err;
176}
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 * Set up a signal frame.
180 */
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800181
182/*
183 * Determine which stack to use..
184 */
Hiroshi Shimamoto1fae0272009-02-27 10:30:32 -0800185static unsigned long align_sigframe(unsigned long sp)
186{
187#ifdef CONFIG_X86_32
188 /*
189 * Align the stack pointer according to the i386 ABI,
190 * i.e. so that on function entry ((sp + 4) & 15) == 0.
191 */
192 sp = ((sp + 4) & -16ul) - 4;
193#else /* !CONFIG_X86_32 */
194 sp = round_down(sp, 16) - 8;
195#endif
196 return sp;
197}
198
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800199static inline void __user *
200get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
201 void __user **fpstate)
202{
203 /* Default to using normal stack */
204 unsigned long sp = regs->sp;
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700205 int onsigstack = on_sig_stack(sp);
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800206
207#ifdef CONFIG_X86_64
208 /* redzone */
209 sp -= 128;
210#endif /* CONFIG_X86_64 */
211
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700212 if (!onsigstack) {
213 /* This is the X/Open sanctioned signal stack switching. */
214 if (ka->sa.sa_flags & SA_ONSTACK) {
Hiroshi Shimamoto0f8f3082009-03-26 10:03:08 -0700215 if (current->sas_ss_size)
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700216 sp = current->sas_ss_sp + current->sas_ss_size;
217 } else {
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800218#ifdef CONFIG_X86_32
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700219 /* This is the legacy signal stack switching. */
220 if ((regs->ss & 0xffff) != __USER_DS &&
221 !(ka->sa.sa_flags & SA_RESTORER) &&
222 ka->sa.sa_restorer)
223 sp = (unsigned long) ka->sa.sa_restorer;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800224#endif /* CONFIG_X86_32 */
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700225 }
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800226 }
227
228 if (used_math()) {
229 sp -= sig_xstate_size;
Hiroshi Shimamoto25051702009-03-02 17:20:01 -0800230#ifdef CONFIG_X86_64
231 sp = round_down(sp, 64);
232#endif /* CONFIG_X86_64 */
233 *fpstate = (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800234 }
235
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700236 sp = align_sigframe(sp - frame_size);
237
238 /*
239 * If we are on the alternate signal stack and would overflow it, don't.
240 * Return an always-bogus address instead so we will die with SIGSEGV.
241 */
242 if (onsigstack && !likely(on_sig_stack(sp)))
243 return (void __user *)-1L;
244
245 /* save i387 state */
246 if (used_math() && save_i387_xstate(*fpstate) < 0)
247 return (void __user *)-1L;
248
249 return (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800250}
251
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800252#ifdef CONFIG_X86_32
253static const struct {
254 u16 poplmovl;
255 u32 val;
256 u16 int80;
257} __attribute__((packed)) retcode = {
258 0xb858, /* popl %eax; movl $..., %eax */
259 __NR_sigreturn,
260 0x80cd, /* int $0x80 */
261};
262
263static const struct {
264 u8 movl;
265 u32 val;
266 u16 int80;
267 u8 pad;
268} __attribute__((packed)) rt_retcode = {
269 0xb8, /* movl $..., %eax */
270 __NR_rt_sigreturn,
271 0x80cd, /* int $0x80 */
272 0
273};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Ingo Molnar7e907f42008-03-06 10:33:08 +0100275static int
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700276__setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
277 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100280 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700282 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700284 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700287 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700289 if (__put_user(sig, &frame->sig))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700290 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700292 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700293 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 if (_NSIG_WORDS > 1) {
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700296 if (__copy_to_user(&frame->extramask, &set->sig[1],
297 sizeof(frame->extramask)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700298 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700301 if (current->mm->context.vdso)
Roland McGrath6c3652e2008-01-30 13:30:42 +0100302 restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100303 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100304 restorer = &frame->retcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (ka->sa.sa_flags & SA_RESTORER)
306 restorer = ka->sa.sa_restorer;
307
308 /* Set up to return from userspace. */
309 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100312 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 *
314 * WE DO NOT USE IT ANY MORE! It's only left here for historical
315 * reasons and because gdb uses it as a signature to notice
316 * signal handler stack frames.
317 */
Hiroshi Shimamoto4a612042008-11-11 19:09:29 -0800318 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700321 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100324 regs->sp = (unsigned long)frame;
325 regs->ip = (unsigned long)ka->sa.sa_handler;
326 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800327 regs->dx = 0;
328 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100330 regs->ds = __USER_DS;
331 regs->es = __USER_DS;
332 regs->ss = __USER_DS;
333 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
David Howells283828f2006-01-18 17:44:00 -0800335 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700338static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
339 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100342 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700344 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Suresh Siddha3c1c7f12008-07-29 10:29:21 -0700346 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700349 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800351 put_user_try {
352 put_user_ex(sig, &frame->sig);
353 put_user_ex(&frame->info, &frame->pinfo);
354 put_user_ex(&frame->uc, &frame->puc);
355 err |= copy_siginfo_to_user(&frame->info, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800357 /* Create the ucontext. */
358 if (cpu_has_xsave)
359 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
360 else
361 put_user_ex(0, &frame->uc.uc_flags);
362 put_user_ex(0, &frame->uc.uc_link);
363 put_user_ex(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
364 put_user_ex(sas_ss_flags(regs->sp),
365 &frame->uc.uc_stack.ss_flags);
366 put_user_ex(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
367 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
368 regs, set->sig[0]);
369 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800371 /* Set up to return from userspace. */
372 restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
373 if (ka->sa.sa_flags & SA_RESTORER)
374 restorer = ka->sa.sa_restorer;
375 put_user_ex(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100376
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800377 /*
378 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
379 *
380 * WE DO NOT USE IT ANY MORE! It's only left here for historical
381 * reasons and because gdb uses it as a signature to notice
382 * signal handler stack frames.
383 */
384 put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
385 } put_user_catch(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700388 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100391 regs->sp = (unsigned long)frame;
392 regs->ip = (unsigned long)ka->sa.sa_handler;
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700393 regs->ax = (unsigned long)sig;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100394 regs->dx = (unsigned long)&frame->info;
395 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100397 regs->ds = __USER_DS;
398 regs->es = __USER_DS;
399 regs->ss = __USER_DS;
400 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
David Howells283828f2006-01-18 17:44:00 -0800402 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800404#else /* !CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800405static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
406 sigset_t *set, struct pt_regs *regs)
407{
408 struct rt_sigframe __user *frame;
409 void __user *fp = NULL;
410 int err = 0;
411 struct task_struct *me = current;
412
Hiroshi Shimamoto97286a22009-02-27 10:28:48 -0800413 frame = get_sigframe(ka, regs, sizeof(struct rt_sigframe), &fp);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800414
415 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
416 return -EFAULT;
417
418 if (ka->sa.sa_flags & SA_SIGINFO) {
419 if (copy_siginfo_to_user(&frame->info, info))
420 return -EFAULT;
421 }
422
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800423 put_user_try {
424 /* Create the ucontext. */
425 if (cpu_has_xsave)
426 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
427 else
428 put_user_ex(0, &frame->uc.uc_flags);
429 put_user_ex(0, &frame->uc.uc_link);
430 put_user_ex(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
431 put_user_ex(sas_ss_flags(regs->sp),
432 &frame->uc.uc_stack.ss_flags);
433 put_user_ex(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
434 err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
435 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800436
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800437 /* Set up to return from userspace. If provided, use a stub
438 already in userspace. */
439 /* x86-64 should always use SA_RESTORER. */
440 if (ka->sa.sa_flags & SA_RESTORER) {
441 put_user_ex(ka->sa.sa_restorer, &frame->pretcode);
442 } else {
443 /* could use a vstub here */
444 err |= -EFAULT;
445 }
446 } put_user_catch(err);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800447
448 if (err)
449 return -EFAULT;
450
451 /* Set up registers for signal handler */
452 regs->di = sig;
453 /* In case the signal handler was declared without prototypes */
454 regs->ax = 0;
455
456 /* This also works for non SA_SIGINFO handlers because they expect the
457 next argument after the signal number on the stack. */
458 regs->si = (unsigned long)&frame->info;
459 regs->dx = (unsigned long)&frame->uc;
460 regs->ip = (unsigned long) ka->sa.sa_handler;
461
462 regs->sp = (unsigned long)frame;
463
464 /* Set up the CS register to run signal handlers in 64-bit mode,
465 even if the handler happens to be interrupting 32-bit code. */
466 regs->cs = __USER_CS;
467
468 return 0;
469}
470#endif /* CONFIG_X86_32 */
471
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800472#ifdef CONFIG_X86_32
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800473/*
474 * Atomically swap in the new signal mask, and wait for a signal.
475 */
476asmlinkage int
477sys_sigsuspend(int history0, int history1, old_sigset_t mask)
478{
Oleg Nesterov39822942011-07-10 21:27:27 +0200479 sigset_t blocked;
480
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800481 current->saved_sigmask = current->blocked;
Oleg Nesterov39822942011-07-10 21:27:27 +0200482
483 mask &= _BLOCKABLE;
484 siginitset(&blocked, mask);
485 set_current_blocked(&blocked);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800486
487 current->state = TASK_INTERRUPTIBLE;
488 schedule();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800489
Oleg Nesterov39822942011-07-10 21:27:27 +0200490 set_restore_sigmask();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800491 return -ERESTARTNOHAND;
492}
493
494asmlinkage int
495sys_sigaction(int sig, const struct old_sigaction __user *act,
496 struct old_sigaction __user *oact)
497{
498 struct k_sigaction new_ka, old_ka;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800499 int ret = 0;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800500
501 if (act) {
502 old_sigset_t mask;
503
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800504 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800505 return -EFAULT;
506
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800507 get_user_try {
508 get_user_ex(new_ka.sa.sa_handler, &act->sa_handler);
509 get_user_ex(new_ka.sa.sa_flags, &act->sa_flags);
510 get_user_ex(mask, &act->sa_mask);
511 get_user_ex(new_ka.sa.sa_restorer, &act->sa_restorer);
512 } get_user_catch(ret);
513
514 if (ret)
515 return -EFAULT;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800516 siginitset(&new_ka.sa.sa_mask, mask);
517 }
518
519 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
520
521 if (!ret && oact) {
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800522 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800523 return -EFAULT;
524
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800525 put_user_try {
526 put_user_ex(old_ka.sa.sa_handler, &oact->sa_handler);
527 put_user_ex(old_ka.sa.sa_flags, &oact->sa_flags);
528 put_user_ex(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
529 put_user_ex(old_ka.sa.sa_restorer, &oact->sa_restorer);
530 } put_user_catch(ret);
531
532 if (ret)
533 return -EFAULT;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800534 }
535
536 return ret;
537}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800538#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800539
Brian Gerst052acad2009-12-09 19:01:54 -0500540long
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800541sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
542 struct pt_regs *regs)
543{
544 return do_sigaltstack(uss, uoss, regs->sp);
545}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800546
547/*
548 * Do a signal return; undo the signal stack.
549 */
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800550#ifdef CONFIG_X86_32
Brian Gerstb12bdaf2009-02-11 16:43:58 -0500551unsigned long sys_sigreturn(struct pt_regs *regs)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800552{
553 struct sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800554 unsigned long ax;
555 sigset_t set;
556
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800557 frame = (struct sigframe __user *)(regs->sp - 8);
558
559 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
560 goto badframe;
561 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
562 && __copy_from_user(&set.sig[1], &frame->extramask,
563 sizeof(frame->extramask))))
564 goto badframe;
565
566 sigdelsetmask(&set, ~_BLOCKABLE);
Oleg Nesterov39822942011-07-10 21:27:27 +0200567 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800568
569 if (restore_sigcontext(regs, &frame->sc, &ax))
570 goto badframe;
571 return ax;
572
573badframe:
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800574 signal_fault(regs, frame, "sigreturn");
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800575
576 return 0;
577}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800578#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800579
H. Peter Anvin74452502009-02-11 16:31:40 -0800580long sys_rt_sigreturn(struct pt_regs *regs)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800581{
582 struct rt_sigframe __user *frame;
583 unsigned long ax;
584 sigset_t set;
585
586 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
587 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
588 goto badframe;
589 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
590 goto badframe;
591
592 sigdelsetmask(&set, ~_BLOCKABLE);
Oleg Nesterove9bd3f02011-04-27 21:09:39 +0200593 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800594
595 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
596 goto badframe;
597
598 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
599 goto badframe;
600
601 return ax;
602
603badframe:
604 signal_fault(regs, frame, "rt_sigreturn");
605 return 0;
606}
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608/*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100609 * OK, we're invoking a handler:
610 */
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700611static int signr_convert(int sig)
612{
Hiroshi Shimamoto96bf84b2008-10-29 18:44:08 -0700613#ifdef CONFIG_X86_32
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700614 struct thread_info *info = current_thread_info();
615
616 if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
617 return info->exec_domain->signal_invmap[sig];
Hiroshi Shimamoto96bf84b2008-10-29 18:44:08 -0700618#endif /* CONFIG_X86_32 */
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700619 return sig;
620}
621
Hiroshi Shimamotocabf5032008-10-29 18:46:07 -0700622#ifdef CONFIG_X86_32
623
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700624#define is_ia32 1
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700625#define ia32_setup_frame __setup_frame
626#define ia32_setup_rt_frame __setup_rt_frame
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700627
Hiroshi Shimamotocabf5032008-10-29 18:46:07 -0700628#else /* !CONFIG_X86_32 */
629
630#ifdef CONFIG_IA32_EMULATION
631#define is_ia32 test_thread_flag(TIF_IA32)
632#else /* !CONFIG_IA32_EMULATION */
633#define is_ia32 0
634#endif /* CONFIG_IA32_EMULATION */
635
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800636#ifdef CONFIG_X86_X32_ABI
637#define is_x32 test_thread_flag(TIF_X32)
638
639static int x32_setup_rt_frame(int sig, struct k_sigaction *ka,
640 siginfo_t *info, compat_sigset_t *set,
641 struct pt_regs *regs);
642#else /* !CONFIG_X86_X32_ABI */
643#define is_x32 0
644#endif /* CONFIG_X86_X32_ABI */
645
Hiroshi Shimamotof5223762008-12-17 18:47:17 -0800646int ia32_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
647 sigset_t *set, struct pt_regs *regs);
648int ia32_setup_frame(int sig, struct k_sigaction *ka,
649 sigset_t *set, struct pt_regs *regs);
650
Hiroshi Shimamotocabf5032008-10-29 18:46:07 -0700651#endif /* CONFIG_X86_32 */
652
Roland McGrath7c1def12005-06-23 00:08:21 -0700653static int
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700654setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
Oleg Nesterov9b429622011-07-10 20:22:03 +0200655 struct pt_regs *regs)
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700656{
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700657 int usig = signr_convert(sig);
Oleg Nesterov9b429622011-07-10 20:22:03 +0200658 sigset_t *set = &current->blocked;
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700659 int ret;
660
Oleg Nesterov9b429622011-07-10 20:22:03 +0200661 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
662 set = &current->saved_sigmask;
663
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700664 /* Set up the stack frame */
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700665 if (is_ia32) {
666 if (ka->sa.sa_flags & SA_SIGINFO)
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700667 ret = ia32_setup_rt_frame(usig, ka, info, set, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700668 else
Hiroshi Shimamoto4694d232008-09-24 19:13:29 -0700669 ret = ia32_setup_frame(usig, ka, set, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800670#ifdef CONFIG_X86_X32_ABI
671 } else if (is_x32) {
672 ret = x32_setup_rt_frame(usig, ka, info,
673 (compat_sigset_t *)set, regs);
674#endif
675 } else {
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700676 ret = __setup_rt_frame(sig, ka, info, set, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800677 }
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700678
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700679 if (ret) {
680 force_sigsegv(sig, current);
681 return -EFAULT;
682 }
683
Oleg Nesterov9b429622011-07-10 20:22:03 +0200684 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700685 return ret;
686}
687
688static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
Oleg Nesterov9b429622011-07-10 20:22:03 +0200690 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Roland McGrath7c1def12005-06-23 00:08:21 -0700692 int ret;
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 /* Are we from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700695 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 /* If so, check system call restarting.. */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700697 switch (syscall_get_error(current, regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800698 case -ERESTART_RESTARTBLOCK:
699 case -ERESTARTNOHAND:
700 regs->ax = -EINTR;
701 break;
702
703 case -ERESTARTSYS:
704 if (!(ka->sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100705 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800707 }
708 /* fallthrough */
709 case -ERESTARTNOINTR:
710 regs->ax = regs->orig_ax;
711 regs->ip -= 2;
712 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
714 }
715
716 /*
Roland McGrathe1f28772008-01-30 13:30:50 +0100717 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
718 * flag so that register information in the sigcontext is correct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100720 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
Roland McGrathe1f28772008-01-30 13:30:50 +0100721 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100722 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Oleg Nesterov9b429622011-07-10 20:22:03 +0200724 ret = setup_rt_frame(sig, ka, info, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Ingo Molnar7e907f42008-03-06 10:33:08 +0100726 if (ret)
727 return ret;
Roland McGrath7c1def12005-06-23 00:08:21 -0700728
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
Matt Fleming5e6292c2012-01-10 15:11:17 -0800742 block_sigmask(ka, sig);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100743
Roland McGrath36a03302008-03-14 17:46:38 -0700744 tracehook_signal_handler(sig, info, ka, regs,
745 test_thread_flag(TIF_SINGLESTEP));
746
Ingo Molnar7e907f42008-03-06 10:33:08 +0100747 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700750#ifdef CONFIG_X86_32
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700751#define NR_restart_syscall __NR_restart_syscall
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700752#else /* !CONFIG_X86_32 */
753#define NR_restart_syscall \
754 test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
755#endif /* CONFIG_X86_32 */
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757/*
758 * Note that 'init' is a special process: it doesn't get signals it doesn't
759 * want to handle. Thus you cannot kill init even with a SIGKILL even by
760 * mistake.
761 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100762static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800764 struct k_sigaction ka;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 siginfo_t info;
766 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 /*
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800769 * We want the common case to go fast, which is why we may in certain
770 * cases get here from kernel mode. Just return without doing anything
771 * if so.
772 * X86_32: vm86 regs switched out by assembly code before reaching
773 * here, so testing against kernel CS suffices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 */
Vincent Hanquez717b5942005-06-23 00:08:45 -0700775 if (!user_mode(regs))
David Howells283828f2006-01-18 17:44:00 -0800776 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
779 if (signr > 0) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100780 /* Whee! Actually deliver the signal. */
Oleg Nesterov9b429622011-07-10 20:22:03 +0200781 handle_signal(signr, &info, &ka, regs);
David Howells283828f2006-01-18 17:44:00 -0800782 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 /* Did we come from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700786 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 /* Restart the system call - no handlers present */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700788 switch (syscall_get_error(current, regs)) {
David Howells283828f2006-01-18 17:44:00 -0800789 case -ERESTARTNOHAND:
790 case -ERESTARTSYS:
791 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100792 regs->ax = regs->orig_ax;
793 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800794 break;
795
796 case -ERESTART_RESTARTBLOCK:
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700797 regs->ax = NR_restart_syscall;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100798 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800799 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801 }
David Howells283828f2006-01-18 17:44:00 -0800802
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800803 /*
804 * If there's no signal to deliver, we just put the saved sigmask
805 * back.
806 */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700807 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
808 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
Oleg Nesterov9b429622011-07-10 20:22:03 +0200809 set_current_blocked(&current->saved_sigmask);
David Howells283828f2006-01-18 17:44:00 -0800810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
813/*
814 * notification of userspace execution resumption
David Howells283828f2006-01-18 17:44:00 -0800815 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100817void
818do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Andi Kleenc1ebf832009-07-09 00:31:41 +0200820#ifdef CONFIG_X86_MCE
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700821 /* notify userspace of pending MCEs */
822 if (thread_info_flags & _TIF_MCE_NOTIFY)
Andi Kleen9b1beaf2009-05-27 21:56:59 +0200823 mce_notify_process();
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700824#endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 /* deal with pending signal delivery */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700827 if (thread_info_flags & _TIF_SIGPENDING)
David Howells283828f2006-01-18 17:44:00 -0800828 do_signal(regs);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100829
Roland McGrath59e52132008-04-19 19:10:57 -0700830 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
831 clear_thread_flag(TIF_NOTIFY_RESUME);
832 tracehook_notify_resume(regs);
David Howellsee18d642009-09-02 09:14:21 +0100833 if (current->replacement_session_keyring)
834 key_replace_session_keyring();
Roland McGrath59e52132008-04-19 19:10:57 -0700835 }
Avi Kivity7c68af62009-09-19 09:40:22 +0300836 if (thread_info_flags & _TIF_USER_RETURN_NOTIFY)
837 fire_user_return_notifiers();
Roland McGrath59e52132008-04-19 19:10:57 -0700838
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700839#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 clear_thread_flag(TIF_IRET);
Hiroshi Shimamotoee847c52008-09-23 17:21:45 -0700841#endif /* CONFIG_X86_32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700843
844void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
845{
846 struct task_struct *me = current;
847
848 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800849 printk("%s"
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700850 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800851 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700852 me->comm, me->pid, where, frame,
853 regs->ip, regs->sp, regs->orig_ax);
854 print_vma_addr(" in ", regs->ip);
855 printk(KERN_CONT "\n");
856 }
857
858 force_sig(SIGSEGV, me);
859}
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800860
861#ifdef CONFIG_X86_X32_ABI
862static int x32_setup_rt_frame(int sig, struct k_sigaction *ka,
863 siginfo_t *info, compat_sigset_t *set,
864 struct pt_regs *regs)
865{
866 struct rt_sigframe_x32 __user *frame;
867 void __user *restorer;
868 int err = 0;
869 void __user *fpstate = NULL;
870
871 frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
872
873 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
874 return -EFAULT;
875
876 if (ka->sa.sa_flags & SA_SIGINFO) {
877 if (copy_siginfo_to_user32(&frame->info, info))
878 return -EFAULT;
879 }
880
881 put_user_try {
882 /* Create the ucontext. */
883 if (cpu_has_xsave)
884 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
885 else
886 put_user_ex(0, &frame->uc.uc_flags);
887 put_user_ex(0, &frame->uc.uc_link);
888 put_user_ex(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
889 put_user_ex(sas_ss_flags(regs->sp),
890 &frame->uc.uc_stack.ss_flags);
891 put_user_ex(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
892 put_user_ex(0, &frame->uc.uc__pad0);
893 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
894 regs, set->sig[0]);
895 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
896
897 if (ka->sa.sa_flags & SA_RESTORER) {
898 restorer = ka->sa.sa_restorer;
899 } else {
900 /* could use a vstub here */
901 restorer = NULL;
902 err |= -EFAULT;
903 }
904 put_user_ex(restorer, &frame->pretcode);
905 } put_user_catch(err);
906
907 if (err)
908 return -EFAULT;
909
910 /* Set up registers for signal handler */
911 regs->sp = (unsigned long) frame;
912 regs->ip = (unsigned long) ka->sa.sa_handler;
913
914 /* We use the x32 calling convention here... */
915 regs->di = sig;
916 regs->si = (unsigned long) &frame->info;
917 regs->dx = (unsigned long) &frame->uc;
918
919 loadsegment(ds, __USER_DS);
920 loadsegment(es, __USER_DS);
921
922 regs->cs = __USER_CS;
923 regs->ss = __USER_DS;
924
925 return 0;
926}
927
928asmlinkage long sys32_x32_rt_sigreturn(struct pt_regs *regs)
929{
930 struct rt_sigframe_x32 __user *frame;
931 sigset_t set;
932 unsigned long ax;
933 struct pt_regs tregs;
934
935 frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
936
937 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
938 goto badframe;
939 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
940 goto badframe;
941
942 sigdelsetmask(&set, ~_BLOCKABLE);
943 set_current_blocked(&set);
944
945 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
946 goto badframe;
947
948 tregs = *regs;
949 if (sys32_sigaltstack(&frame->uc.uc_stack, NULL, &tregs) == -EFAULT)
950 goto badframe;
951
952 return ax;
953
954badframe:
955 signal_fault(regs, frame, "x32 rt_sigreturn");
956 return 0;
957}
958#endif