blob: 206996c1669db344aba7ff072f734552723e7938 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -08003 * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
6 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -08007 * 2000-2002 x86-64 support by Andi Kleen
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
Joe Perchesc767a542012-05-21 19:50:07 -07009
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
Ingo Molnar7e907f42008-03-06 10:33:08 +010012#include <linux/sched.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010013#include <linux/mm.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080014#include <linux/smp.h>
15#include <linux/kernel.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080016#include <linux/errno.h>
17#include <linux/wait.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080018#include <linux/tracehook.h>
19#include <linux/unistd.h>
20#include <linux/stddef.h>
21#include <linux/personality.h>
22#include <linux/uaccess.h>
Avi Kivity7c68af62009-09-19 09:40:22 +030023#include <linux/user-return-notifier.h>
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +053024#include <linux/uprobes.h>
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010025#include <linux/context_tracking.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/processor.h>
28#include <asm/ucontext.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020029#include <asm/fpu/internal.h>
Ingo Molnarfcbc99c2015-04-30 08:45:02 +020030#include <asm/fpu/signal.h>
Roland McGrath6c3652e2008-01-30 13:30:42 +010031#include <asm/vdso.h>
Andi Kleen4efc0672009-04-28 19:07:31 +020032#include <asm/mce.h>
H. Peter Anvinf28f0c22012-02-19 07:38:43 -080033#include <asm/sighandling.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080034
35#ifdef CONFIG_X86_64
36#include <asm/proto.h>
37#include <asm/ia32_unistd.h>
H. Peter Anvinc5a37392012-02-19 09:41:09 -080038#include <asm/sys_ia32.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080039#endif /* CONFIG_X86_64 */
40
Hiroshi Shimamotobb579252008-09-05 16:26:55 -070041#include <asm/syscall.h>
Jaswinder Singhbbc1f692008-07-21 21:34:13 +053042#include <asm/syscalls.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010043
Hiroshi Shimamoto41af86f2008-12-17 18:50:32 -080044#include <asm/sigframe.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
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
Brian Gerst6a3713f2015-04-04 08:58:23 -040064int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080065{
66 void __user *buf;
67 unsigned int tmpflags;
68 unsigned int err = 0;
69
70 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -080071 current->restart_block.fn = do_no_restart_syscall;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080072
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080073 get_user_try {
74
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080075#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +090076 set_user_gs(regs, GET_SEG(gs));
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080077 COPY_SEG(fs);
78 COPY_SEG(es);
79 COPY_SEG(ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080080#endif /* CONFIG_X86_32 */
81
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080082 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
Brian Gerst6a3713f2015-04-04 08:58:23 -040083 COPY(dx); COPY(cx); COPY(ip); COPY(ax);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080084
85#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080086 COPY(r8);
87 COPY(r9);
88 COPY(r10);
89 COPY(r11);
90 COPY(r12);
91 COPY(r13);
92 COPY(r14);
93 COPY(r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080094#endif /* CONFIG_X86_64 */
95
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080096 COPY_SEG_CPL3(cs);
97 COPY_SEG_CPL3(ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080098
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080099 get_user_ex(tmpflags, &sc->flags);
100 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
101 regs->orig_ax = -1; /* disable syscall checks */
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800102
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800103 get_user_ex(buf, &sc->fpstate);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800104 } get_user_catch(err);
105
Ingo Molnar9dfe99b2015-04-29 20:55:19 +0200106 err |= fpu__restore_sig(buf, config_enabled(CONFIG_X86_32));
H. Peter Anvin5e883532012-09-21 12:43:15 -0700107
Brian Gerst1daeaa32015-03-21 18:54:21 -0400108 force_iret();
109
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800110 return err;
111}
112
H. Peter Anvin85139422012-02-19 07:43:09 -0800113int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
114 struct pt_regs *regs, unsigned long mask)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800115{
116 int err = 0;
117
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800118 put_user_try {
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800119
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800120#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +0900121 put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800122 put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
123 put_user_ex(regs->es, (unsigned int __user *)&sc->es);
124 put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800125#endif /* CONFIG_X86_32 */
126
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800127 put_user_ex(regs->di, &sc->di);
128 put_user_ex(regs->si, &sc->si);
129 put_user_ex(regs->bp, &sc->bp);
130 put_user_ex(regs->sp, &sc->sp);
131 put_user_ex(regs->bx, &sc->bx);
132 put_user_ex(regs->dx, &sc->dx);
133 put_user_ex(regs->cx, &sc->cx);
134 put_user_ex(regs->ax, &sc->ax);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800135#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800136 put_user_ex(regs->r8, &sc->r8);
137 put_user_ex(regs->r9, &sc->r9);
138 put_user_ex(regs->r10, &sc->r10);
139 put_user_ex(regs->r11, &sc->r11);
140 put_user_ex(regs->r12, &sc->r12);
141 put_user_ex(regs->r13, &sc->r13);
142 put_user_ex(regs->r14, &sc->r14);
143 put_user_ex(regs->r15, &sc->r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800144#endif /* CONFIG_X86_64 */
145
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530146 put_user_ex(current->thread.trap_nr, &sc->trapno);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800147 put_user_ex(current->thread.error_code, &sc->err);
148 put_user_ex(regs->ip, &sc->ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800149#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800150 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
151 put_user_ex(regs->flags, &sc->flags);
152 put_user_ex(regs->sp, &sc->sp_at_signal);
153 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800154#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800155 put_user_ex(regs->flags, &sc->flags);
156 put_user_ex(regs->cs, &sc->cs);
Andy Lutomirski9a036b932015-03-12 13:57:52 -0700157 put_user_ex(0, &sc->__pad2);
158 put_user_ex(0, &sc->__pad1);
Andy Lutomirskic6f20622015-03-12 13:57:51 -0700159 put_user_ex(regs->ss, &sc->ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800160#endif /* CONFIG_X86_32 */
161
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800162 put_user_ex(fpstate, &sc->fpstate);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800163
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800164 /* non-iBCS2 extensions.. */
165 put_user_ex(mask, &sc->oldmask);
166 put_user_ex(current->thread.cr2, &sc->cr2);
167 } put_user_catch(err);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800168
169 return err;
170}
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * Set up a signal frame.
174 */
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800175
176/*
177 * Determine which stack to use..
178 */
Hiroshi Shimamoto1fae0272009-02-27 10:30:32 -0800179static unsigned long align_sigframe(unsigned long sp)
180{
181#ifdef CONFIG_X86_32
182 /*
183 * Align the stack pointer according to the i386 ABI,
184 * i.e. so that on function entry ((sp + 4) & 15) == 0.
185 */
186 sp = ((sp + 4) & -16ul) - 4;
187#else /* !CONFIG_X86_32 */
188 sp = round_down(sp, 16) - 8;
189#endif
190 return sp;
191}
192
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800193static inline void __user *
194get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
195 void __user **fpstate)
196{
197 /* Default to using normal stack */
Suresh Siddha72a671c2012-07-24 16:05:29 -0700198 unsigned long math_size = 0;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800199 unsigned long sp = regs->sp;
Suresh Siddha72a671c2012-07-24 16:05:29 -0700200 unsigned long buf_fx = 0;
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700201 int onsigstack = on_sig_stack(sp);
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200202 struct fpu *fpu = &current->thread.fpu;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800203
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800204 /* redzone */
Suresh Siddha050902c2012-07-24 16:05:27 -0700205 if (config_enabled(CONFIG_X86_64))
206 sp -= 128;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800207
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700208 if (!onsigstack) {
209 /* This is the X/Open sanctioned signal stack switching. */
210 if (ka->sa.sa_flags & SA_ONSTACK) {
Hiroshi Shimamoto0f8f3082009-03-26 10:03:08 -0700211 if (current->sas_ss_size)
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700212 sp = current->sas_ss_sp + current->sas_ss_size;
Suresh Siddha050902c2012-07-24 16:05:27 -0700213 } else if (config_enabled(CONFIG_X86_32) &&
214 (regs->ss & 0xffff) != __USER_DS &&
215 !(ka->sa.sa_flags & SA_RESTORER) &&
216 ka->sa.sa_restorer) {
217 /* This is the legacy signal stack switching. */
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700218 sp = (unsigned long) ka->sa.sa_restorer;
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700219 }
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800220 }
221
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200222 if (fpu->fpstate_active) {
Ingo Molnar82c0e452015-04-29 21:09:18 +0200223 sp = fpu__alloc_mathframe(sp, config_enabled(CONFIG_X86_32),
224 &buf_fx, &math_size);
Hiroshi Shimamoto25051702009-03-02 17:20:01 -0800225 *fpstate = (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800226 }
227
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700228 sp = align_sigframe(sp - frame_size);
229
230 /*
231 * If we are on the alternate signal stack and would overflow it, don't.
232 * Return an always-bogus address instead so we will die with SIGSEGV.
233 */
234 if (onsigstack && !likely(on_sig_stack(sp)))
235 return (void __user *)-1L;
236
Suresh Siddha72a671c2012-07-24 16:05:29 -0700237 /* save i387 and extended state */
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200238 if (fpu->fpstate_active &&
Ingo Molnarc8e14042015-04-28 11:35:20 +0200239 copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size) < 0)
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700240 return (void __user *)-1L;
241
242 return (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800243}
244
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800245#ifdef CONFIG_X86_32
246static const struct {
247 u16 poplmovl;
248 u32 val;
249 u16 int80;
250} __attribute__((packed)) retcode = {
251 0xb858, /* popl %eax; movl $..., %eax */
252 __NR_sigreturn,
253 0x80cd, /* int $0x80 */
254};
255
256static const struct {
257 u8 movl;
258 u32 val;
259 u16 int80;
260 u8 pad;
261} __attribute__((packed)) rt_retcode = {
262 0xb8, /* movl $..., %eax */
263 __NR_rt_sigreturn,
264 0x80cd, /* int $0x80 */
265 0
266};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Ingo Molnar7e907f42008-03-06 10:33:08 +0100268static int
Al Viro235b8022012-11-09 23:51:47 -0500269__setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700270 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100273 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700275 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Al Viro235b8022012-11-09 23:51:47 -0500277 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700280 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700282 if (__put_user(sig, &frame->sig))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700283 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700285 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700286 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 if (_NSIG_WORDS > 1) {
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700289 if (__copy_to_user(&frame->extramask, &set->sig[1],
290 sizeof(frame->extramask)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700291 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700294 if (current->mm->context.vdso)
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700295 restorer = current->mm->context.vdso +
296 selected_vdso32->sym___kernel_sigreturn;
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100297 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100298 restorer = &frame->retcode;
Al Viro235b8022012-11-09 23:51:47 -0500299 if (ksig->ka.sa.sa_flags & SA_RESTORER)
300 restorer = ksig->ka.sa.sa_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 /* Set up to return from userspace. */
303 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100306 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 *
308 * WE DO NOT USE IT ANY MORE! It's only left here for historical
309 * reasons and because gdb uses it as a signature to notice
310 * signal handler stack frames.
311 */
Hiroshi Shimamoto4a612042008-11-11 19:09:29 -0800312 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700315 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100318 regs->sp = (unsigned long)frame;
Al Viro235b8022012-11-09 23:51:47 -0500319 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100320 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800321 regs->dx = 0;
322 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100324 regs->ds = __USER_DS;
325 regs->es = __USER_DS;
326 regs->ss = __USER_DS;
327 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
David Howells283828f2006-01-18 17:44:00 -0800329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Al Viro235b8022012-11-09 23:51:47 -0500332static int __setup_rt_frame(int sig, struct ksignal *ksig,
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700333 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100336 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700338 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Al Viro235b8022012-11-09 23:51:47 -0500340 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700343 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800345 put_user_try {
346 put_user_ex(sig, &frame->sig);
347 put_user_ex(&frame->info, &frame->pinfo);
348 put_user_ex(&frame->uc, &frame->puc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800350 /* Create the ucontext. */
351 if (cpu_has_xsave)
352 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
353 else
354 put_user_ex(0, &frame->uc.uc_flags);
355 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100356 save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800358 /* Set up to return from userspace. */
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700359 restorer = current->mm->context.vdso +
Andy Lutomirski6ba19a62014-06-21 07:31:55 -0700360 selected_vdso32->sym___kernel_rt_sigreturn;
Al Viro235b8022012-11-09 23:51:47 -0500361 if (ksig->ka.sa.sa_flags & SA_RESTORER)
362 restorer = ksig->ka.sa.sa_restorer;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800363 put_user_ex(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100364
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800365 /*
366 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
367 *
368 * WE DO NOT USE IT ANY MORE! It's only left here for historical
369 * reasons and because gdb uses it as a signature to notice
370 * signal handler stack frames.
371 */
372 put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
373 } put_user_catch(err);
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700374
Al Viro235b8022012-11-09 23:51:47 -0500375 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
H. Peter Anvin5e883532012-09-21 12:43:15 -0700376 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
380 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700381 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100384 regs->sp = (unsigned long)frame;
Al Viro235b8022012-11-09 23:51:47 -0500385 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700386 regs->ax = (unsigned long)sig;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100387 regs->dx = (unsigned long)&frame->info;
388 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100390 regs->ds = __USER_DS;
391 regs->es = __USER_DS;
392 regs->ss = __USER_DS;
393 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
David Howells283828f2006-01-18 17:44:00 -0800395 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800397#else /* !CONFIG_X86_32 */
Al Viro235b8022012-11-09 23:51:47 -0500398static int __setup_rt_frame(int sig, struct ksignal *ksig,
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800399 sigset_t *set, struct pt_regs *regs)
400{
401 struct rt_sigframe __user *frame;
402 void __user *fp = NULL;
403 int err = 0;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800404
Al Viro235b8022012-11-09 23:51:47 -0500405 frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800406
407 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
408 return -EFAULT;
409
Al Viro235b8022012-11-09 23:51:47 -0500410 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
411 if (copy_siginfo_to_user(&frame->info, &ksig->info))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800412 return -EFAULT;
413 }
414
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800415 put_user_try {
416 /* Create the ucontext. */
417 if (cpu_has_xsave)
418 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
419 else
420 put_user_ex(0, &frame->uc.uc_flags);
421 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100422 save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800423
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800424 /* Set up to return from userspace. If provided, use a stub
425 already in userspace. */
426 /* x86-64 should always use SA_RESTORER. */
Al Viro235b8022012-11-09 23:51:47 -0500427 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
428 put_user_ex(ksig->ka.sa.sa_restorer, &frame->pretcode);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800429 } else {
430 /* could use a vstub here */
431 err |= -EFAULT;
432 }
433 } put_user_catch(err);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800434
H. Peter Anvin5e883532012-09-21 12:43:15 -0700435 err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
436 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
437
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800438 if (err)
439 return -EFAULT;
440
441 /* Set up registers for signal handler */
442 regs->di = sig;
443 /* In case the signal handler was declared without prototypes */
444 regs->ax = 0;
445
446 /* This also works for non SA_SIGINFO handlers because they expect the
447 next argument after the signal number on the stack. */
448 regs->si = (unsigned long)&frame->info;
449 regs->dx = (unsigned long)&frame->uc;
Al Viro235b8022012-11-09 23:51:47 -0500450 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800451
452 regs->sp = (unsigned long)frame;
453
Andy Lutomirskic6f20622015-03-12 13:57:51 -0700454 /*
455 * Set up the CS and SS registers to run signal handlers in
456 * 64-bit mode, even if the handler happens to be interrupting
457 * 32-bit or 16-bit code.
458 *
459 * SS is subtle. In 64-bit mode, we don't need any particular
460 * SS descriptor, but we do need SS to be valid. It's possible
461 * that the old SS is entirely bogus -- this can happen if the
462 * signal we're trying to deliver is #GP or #SS caused by a bad
463 * SS value.
464 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800465 regs->cs = __USER_CS;
Andy Lutomirskic6f20622015-03-12 13:57:51 -0700466 regs->ss = __USER_DS;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800467
468 return 0;
469}
470#endif /* CONFIG_X86_32 */
471
Al Viro235b8022012-11-09 23:51:47 -0500472static int x32_setup_rt_frame(struct ksignal *ksig,
473 compat_sigset_t *set,
Suresh Siddha050902c2012-07-24 16:05:27 -0700474 struct pt_regs *regs)
475{
476#ifdef CONFIG_X86_X32_ABI
477 struct rt_sigframe_x32 __user *frame;
478 void __user *restorer;
479 int err = 0;
480 void __user *fpstate = NULL;
481
Al Viro235b8022012-11-09 23:51:47 -0500482 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Suresh Siddha050902c2012-07-24 16:05:27 -0700483
484 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
485 return -EFAULT;
486
Al Viro235b8022012-11-09 23:51:47 -0500487 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
488 if (copy_siginfo_to_user32(&frame->info, &ksig->info))
Suresh Siddha050902c2012-07-24 16:05:27 -0700489 return -EFAULT;
490 }
491
492 put_user_try {
493 /* Create the ucontext. */
494 if (cpu_has_xsave)
495 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
496 else
497 put_user_ex(0, &frame->uc.uc_flags);
498 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100499 compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Suresh Siddha050902c2012-07-24 16:05:27 -0700500 put_user_ex(0, &frame->uc.uc__pad0);
Suresh Siddha050902c2012-07-24 16:05:27 -0700501
Al Viro235b8022012-11-09 23:51:47 -0500502 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
503 restorer = ksig->ka.sa.sa_restorer;
Suresh Siddha050902c2012-07-24 16:05:27 -0700504 } else {
505 /* could use a vstub here */
506 restorer = NULL;
507 err |= -EFAULT;
508 }
509 put_user_ex(restorer, &frame->pretcode);
510 } put_user_catch(err);
511
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700512 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
513 regs, set->sig[0]);
514 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
515
Suresh Siddha050902c2012-07-24 16:05:27 -0700516 if (err)
517 return -EFAULT;
518
519 /* Set up registers for signal handler */
520 regs->sp = (unsigned long) frame;
Al Viro235b8022012-11-09 23:51:47 -0500521 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Suresh Siddha050902c2012-07-24 16:05:27 -0700522
523 /* We use the x32 calling convention here... */
Al Viro235b8022012-11-09 23:51:47 -0500524 regs->di = ksig->sig;
Suresh Siddha050902c2012-07-24 16:05:27 -0700525 regs->si = (unsigned long) &frame->info;
526 regs->dx = (unsigned long) &frame->uc;
527
528 loadsegment(ds, __USER_DS);
529 loadsegment(es, __USER_DS);
530
531 regs->cs = __USER_CS;
532 regs->ss = __USER_DS;
533#endif /* CONFIG_X86_X32_ABI */
534
535 return 0;
536}
537
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800538/*
539 * Do a signal return; undo the signal stack.
540 */
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800541#ifdef CONFIG_X86_32
Andi Kleenff491032013-08-05 15:02:40 -0700542asmlinkage unsigned long sys_sigreturn(void)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800543{
Al Viro3fe26fa2012-11-12 14:32:42 -0500544 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800545 struct sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800546 sigset_t set;
547
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800548 frame = (struct sigframe __user *)(regs->sp - 8);
549
550 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
551 goto badframe;
552 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
553 && __copy_from_user(&set.sig[1], &frame->extramask,
554 sizeof(frame->extramask))))
555 goto badframe;
556
Oleg Nesterov39822942011-07-10 21:27:27 +0200557 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800558
Brian Gerst6a3713f2015-04-04 08:58:23 -0400559 if (restore_sigcontext(regs, &frame->sc))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800560 goto badframe;
Brian Gerst6a3713f2015-04-04 08:58:23 -0400561 return regs->ax;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800562
563badframe:
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800564 signal_fault(regs, frame, "sigreturn");
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800565
566 return 0;
567}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800568#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800569
Andi Kleenff491032013-08-05 15:02:40 -0700570asmlinkage long sys_rt_sigreturn(void)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800571{
Al Viro3fe26fa2012-11-12 14:32:42 -0500572 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800573 struct rt_sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800574 sigset_t set;
575
576 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
577 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
578 goto badframe;
579 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
580 goto badframe;
581
Oleg Nesterove9bd3f02011-04-27 21:09:39 +0200582 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800583
Brian Gerst6a3713f2015-04-04 08:58:23 -0400584 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800585 goto badframe;
586
Al Viroc40702c2012-11-20 14:24:26 -0500587 if (restore_altstack(&frame->uc.uc_stack))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800588 goto badframe;
589
Brian Gerst6a3713f2015-04-04 08:58:23 -0400590 return regs->ax;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800591
592badframe:
593 signal_fault(regs, frame, "rt_sigreturn");
594 return 0;
595}
596
Ingo Molnar05012c12015-04-30 07:26:04 +0200597static inline int is_ia32_compat_frame(void)
598{
599 return config_enabled(CONFIG_IA32_EMULATION) &&
600 test_thread_flag(TIF_IA32);
601}
602
603static inline int is_ia32_frame(void)
604{
605 return config_enabled(CONFIG_X86_32) || is_ia32_compat_frame();
606}
607
608static inline int is_x32_frame(void)
609{
610 return config_enabled(CONFIG_X86_X32_ABI) && test_thread_flag(TIF_X32);
611}
612
Roland McGrath7c1def12005-06-23 00:08:21 -0700613static int
Al Viro235b8022012-11-09 23:51:47 -0500614setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700615{
Richard Weinberger3050a352014-07-13 17:43:51 +0200616 int usig = ksig->sig;
Al Virob7f9a112012-05-02 09:59:21 -0400617 sigset_t *set = sigmask_to_save();
Suresh Siddha050902c2012-07-24 16:05:27 -0700618 compat_sigset_t *cset = (compat_sigset_t *) set;
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700619
620 /* Set up the stack frame */
Suresh Siddha050902c2012-07-24 16:05:27 -0700621 if (is_ia32_frame()) {
Al Viro235b8022012-11-09 23:51:47 -0500622 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
623 return ia32_setup_rt_frame(usig, ksig, cset, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700624 else
Al Viro235b8022012-11-09 23:51:47 -0500625 return ia32_setup_frame(usig, ksig, cset, regs);
Suresh Siddha050902c2012-07-24 16:05:27 -0700626 } else if (is_x32_frame()) {
Al Viro235b8022012-11-09 23:51:47 -0500627 return x32_setup_rt_frame(ksig, cset, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800628 } else {
Al Viro235b8022012-11-09 23:51:47 -0500629 return __setup_rt_frame(ksig->sig, ksig, set, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800630 }
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700631}
632
Al Viroa610d6e2012-05-21 23:42:15 -0400633static void
Al Viro235b8022012-11-09 23:51:47 -0500634handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700636 bool stepping, failed;
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200637 struct fpu *fpu = &current->thread.fpu;
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 /* Are we from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700640 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 /* If so, check system call restarting.. */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700642 switch (syscall_get_error(current, regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800643 case -ERESTART_RESTARTBLOCK:
644 case -ERESTARTNOHAND:
645 regs->ax = -EINTR;
646 break;
647
648 case -ERESTARTSYS:
Al Viro235b8022012-11-09 23:51:47 -0500649 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100650 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800652 }
653 /* fallthrough */
654 case -ERESTARTNOINTR:
655 regs->ax = regs->orig_ax;
656 regs->ip -= 2;
657 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659 }
660
661 /*
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700662 * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now
663 * so that register information in the sigcontext is correct and
664 * then notify the tracer before entering the signal handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 */
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700666 stepping = test_thread_flag(TIF_SINGLESTEP);
667 if (stepping)
668 user_disable_single_step(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Al Viro235b8022012-11-09 23:51:47 -0500670 failed = (setup_rt_frame(ksig, regs) < 0);
671 if (!failed) {
672 /*
673 * Clear the direction flag as per the ABI for function entry.
Jiri Olsaddd40da2013-05-01 17:25:43 +0200674 *
Jiri Olsa24cda102013-05-01 17:25:42 +0200675 * Clear RF when entering the signal handler, because
676 * it might disable possible debug exception from the
677 * signal handler.
Jiri Olsaddd40da2013-05-01 17:25:43 +0200678 *
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700679 * Clear TF for the case when it wasn't set by debugger to
680 * avoid the recursive send_sigtrap() in SIGTRAP handler.
Al Viro235b8022012-11-09 23:51:47 -0500681 */
Jiri Olsaddd40da2013-05-01 17:25:43 +0200682 regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
Oleg Nesterov66463db2014-09-02 19:57:13 +0200683 /*
684 * Ensure the signal handler starts with the new fpu state.
685 */
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200686 if (fpu->fpstate_active)
Ingo Molnarfbce7782015-04-30 07:12:46 +0200687 fpu__clear(fpu);
Al Viroa610d6e2012-05-21 23:42:15 -0400688 }
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700689 signal_setup_done(failed, ksig, stepping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700692#ifdef CONFIG_X86_32
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700693#define NR_restart_syscall __NR_restart_syscall
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700694#else /* !CONFIG_X86_32 */
695#define NR_restart_syscall \
696 test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
697#endif /* CONFIG_X86_32 */
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699/*
700 * Note that 'init' is a special process: it doesn't get signals it doesn't
701 * want to handle. Thus you cannot kill init even with a SIGKILL even by
702 * mistake.
703 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100704static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Al Viro235b8022012-11-09 23:51:47 -0500706 struct ksignal ksig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Al Viro235b8022012-11-09 23:51:47 -0500708 if (get_signal(&ksig)) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100709 /* Whee! Actually deliver the signal. */
Al Viro235b8022012-11-09 23:51:47 -0500710 handle_signal(&ksig, regs);
David Howells283828f2006-01-18 17:44:00 -0800711 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 /* Did we come from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700715 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 /* Restart the system call - no handlers present */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700717 switch (syscall_get_error(current, regs)) {
David Howells283828f2006-01-18 17:44:00 -0800718 case -ERESTARTNOHAND:
719 case -ERESTARTSYS:
720 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100721 regs->ax = regs->orig_ax;
722 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800723 break;
724
725 case -ERESTART_RESTARTBLOCK:
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700726 regs->ax = NR_restart_syscall;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100727 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800728 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730 }
David Howells283828f2006-01-18 17:44:00 -0800731
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800732 /*
733 * If there's no signal to deliver, we just put the saved sigmask
734 * back.
735 */
Al Viro51a7b442012-05-21 23:33:55 -0400736 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
739/*
740 * notification of userspace execution resumption
David Howells283828f2006-01-18 17:44:00 -0800741 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 */
Andi Kleen277d5b42013-08-05 15:02:43 -0700743__visible void
Ingo Molnar7e907f42008-03-06 10:33:08 +0100744do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +0100746 user_exit();
Frederic Weisbeckeredf55fd2012-07-11 20:26:39 +0200747
Oleg Nesterovdb023ea2012-09-14 19:05:46 +0200748 if (thread_info_flags & _TIF_UPROBE)
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530749 uprobe_notify_resume(regs);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 /* deal with pending signal delivery */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700752 if (thread_info_flags & _TIF_SIGPENDING)
David Howells283828f2006-01-18 17:44:00 -0800753 do_signal(regs);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100754
Roland McGrath59e52132008-04-19 19:10:57 -0700755 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
756 clear_thread_flag(TIF_NOTIFY_RESUME);
757 tracehook_notify_resume(regs);
758 }
Avi Kivity7c68af62009-09-19 09:40:22 +0300759 if (thread_info_flags & _TIF_USER_RETURN_NOTIFY)
760 fire_user_return_notifiers();
Roland McGrath59e52132008-04-19 19:10:57 -0700761
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +0100762 user_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700764
765void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
766{
767 struct task_struct *me = current;
768
769 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800770 printk("%s"
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700771 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800772 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700773 me->comm, me->pid, where, frame,
774 regs->ip, regs->sp, regs->orig_ax);
775 print_vma_addr(" in ", regs->ip);
Joe Perchesc767a542012-05-21 19:50:07 -0700776 pr_cont("\n");
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700777 }
778
779 force_sig(SIGSEGV, me);
780}
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800781
782#ifdef CONFIG_X86_X32_ABI
Al Viro3fe26fa2012-11-12 14:32:42 -0500783asmlinkage long sys32_x32_rt_sigreturn(void)
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800784{
Al Viro3fe26fa2012-11-12 14:32:42 -0500785 struct pt_regs *regs = current_pt_regs();
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800786 struct rt_sigframe_x32 __user *frame;
787 sigset_t set;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800788
789 frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
790
791 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
792 goto badframe;
793 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
794 goto badframe;
795
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800796 set_current_blocked(&set);
797
Brian Gerst6a3713f2015-04-04 08:58:23 -0400798 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800799 goto badframe;
800
Al Viro90268432012-12-14 14:47:53 -0500801 if (compat_restore_altstack(&frame->uc.uc_stack))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800802 goto badframe;
803
Brian Gerst6a3713f2015-04-04 08:58:23 -0400804 return regs->ax;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800805
806badframe:
807 signal_fault(regs, frame, "x32 rt_sigreturn");
808 return 0;
809}
810#endif