blob: 53cc4085c3d71b14243628a4ca1113f58dc39a5b [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/i387.h>
Linus Torvalds1361b832012-02-21 13:19:22 -080030#include <asm/fpu-internal.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
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700106 err |= restore_xstate_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);
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800202
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800203 /* redzone */
Suresh Siddha050902c2012-07-24 16:05:27 -0700204 if (config_enabled(CONFIG_X86_64))
205 sp -= 128;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800206
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700207 if (!onsigstack) {
208 /* This is the X/Open sanctioned signal stack switching. */
209 if (ka->sa.sa_flags & SA_ONSTACK) {
Hiroshi Shimamoto0f8f3082009-03-26 10:03:08 -0700210 if (current->sas_ss_size)
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700211 sp = current->sas_ss_sp + current->sas_ss_size;
Suresh Siddha050902c2012-07-24 16:05:27 -0700212 } else if (config_enabled(CONFIG_X86_32) &&
213 (regs->ss & 0xffff) != __USER_DS &&
214 !(ka->sa.sa_flags & SA_RESTORER) &&
215 ka->sa.sa_restorer) {
216 /* This is the legacy signal stack switching. */
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700217 sp = (unsigned long) ka->sa.sa_restorer;
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700218 }
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800219 }
220
221 if (used_math()) {
Suresh Siddha72a671c2012-07-24 16:05:29 -0700222 sp = alloc_mathframe(sp, config_enabled(CONFIG_X86_32),
223 &buf_fx, &math_size);
Hiroshi Shimamoto25051702009-03-02 17:20:01 -0800224 *fpstate = (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800225 }
226
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700227 sp = align_sigframe(sp - frame_size);
228
229 /*
230 * If we are on the alternate signal stack and would overflow it, don't.
231 * Return an always-bogus address instead so we will die with SIGSEGV.
232 */
233 if (onsigstack && !likely(on_sig_stack(sp)))
234 return (void __user *)-1L;
235
Suresh Siddha72a671c2012-07-24 16:05:29 -0700236 /* save i387 and extended state */
237 if (used_math() &&
238 save_xstate_sig(*fpstate, (void __user *)buf_fx, math_size) < 0)
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700239 return (void __user *)-1L;
240
241 return (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800242}
243
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800244#ifdef CONFIG_X86_32
245static const struct {
246 u16 poplmovl;
247 u32 val;
248 u16 int80;
249} __attribute__((packed)) retcode = {
250 0xb858, /* popl %eax; movl $..., %eax */
251 __NR_sigreturn,
252 0x80cd, /* int $0x80 */
253};
254
255static const struct {
256 u8 movl;
257 u32 val;
258 u16 int80;
259 u8 pad;
260} __attribute__((packed)) rt_retcode = {
261 0xb8, /* movl $..., %eax */
262 __NR_rt_sigreturn,
263 0x80cd, /* int $0x80 */
264 0
265};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Ingo Molnar7e907f42008-03-06 10:33:08 +0100267static int
Al Viro235b8022012-11-09 23:51:47 -0500268__setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700269 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100272 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700274 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Al Viro235b8022012-11-09 23:51:47 -0500276 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700279 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700281 if (__put_user(sig, &frame->sig))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700282 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700284 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700285 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 if (_NSIG_WORDS > 1) {
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700288 if (__copy_to_user(&frame->extramask, &set->sig[1],
289 sizeof(frame->extramask)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700290 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700293 if (current->mm->context.vdso)
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700294 restorer = current->mm->context.vdso +
295 selected_vdso32->sym___kernel_sigreturn;
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100296 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100297 restorer = &frame->retcode;
Al Viro235b8022012-11-09 23:51:47 -0500298 if (ksig->ka.sa.sa_flags & SA_RESTORER)
299 restorer = ksig->ka.sa.sa_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 /* Set up to return from userspace. */
302 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100305 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 *
307 * WE DO NOT USE IT ANY MORE! It's only left here for historical
308 * reasons and because gdb uses it as a signature to notice
309 * signal handler stack frames.
310 */
Hiroshi Shimamoto4a612042008-11-11 19:09:29 -0800311 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700314 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100317 regs->sp = (unsigned long)frame;
Al Viro235b8022012-11-09 23:51:47 -0500318 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100319 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800320 regs->dx = 0;
321 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100323 regs->ds = __USER_DS;
324 regs->es = __USER_DS;
325 regs->ss = __USER_DS;
326 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
David Howells283828f2006-01-18 17:44:00 -0800328 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Al Viro235b8022012-11-09 23:51:47 -0500331static int __setup_rt_frame(int sig, struct ksignal *ksig,
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700332 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100335 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700337 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Al Viro235b8022012-11-09 23:51:47 -0500339 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700342 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800344 put_user_try {
345 put_user_ex(sig, &frame->sig);
346 put_user_ex(&frame->info, &frame->pinfo);
347 put_user_ex(&frame->uc, &frame->puc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800349 /* Create the ucontext. */
350 if (cpu_has_xsave)
351 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
352 else
353 put_user_ex(0, &frame->uc.uc_flags);
354 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100355 save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800357 /* Set up to return from userspace. */
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700358 restorer = current->mm->context.vdso +
Andy Lutomirski6ba19a62014-06-21 07:31:55 -0700359 selected_vdso32->sym___kernel_rt_sigreturn;
Al Viro235b8022012-11-09 23:51:47 -0500360 if (ksig->ka.sa.sa_flags & SA_RESTORER)
361 restorer = ksig->ka.sa.sa_restorer;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800362 put_user_ex(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100363
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800364 /*
365 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
366 *
367 * WE DO NOT USE IT ANY MORE! It's only left here for historical
368 * reasons and because gdb uses it as a signature to notice
369 * signal handler stack frames.
370 */
371 put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
372 } put_user_catch(err);
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700373
Al Viro235b8022012-11-09 23:51:47 -0500374 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
H. Peter Anvin5e883532012-09-21 12:43:15 -0700375 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
376 regs, set->sig[0]);
377 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700380 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100383 regs->sp = (unsigned long)frame;
Al Viro235b8022012-11-09 23:51:47 -0500384 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700385 regs->ax = (unsigned long)sig;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100386 regs->dx = (unsigned long)&frame->info;
387 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100389 regs->ds = __USER_DS;
390 regs->es = __USER_DS;
391 regs->ss = __USER_DS;
392 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
David Howells283828f2006-01-18 17:44:00 -0800394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800396#else /* !CONFIG_X86_32 */
Al Viro235b8022012-11-09 23:51:47 -0500397static int __setup_rt_frame(int sig, struct ksignal *ksig,
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800398 sigset_t *set, struct pt_regs *regs)
399{
400 struct rt_sigframe __user *frame;
401 void __user *fp = NULL;
402 int err = 0;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800403
Al Viro235b8022012-11-09 23:51:47 -0500404 frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800405
406 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
407 return -EFAULT;
408
Al Viro235b8022012-11-09 23:51:47 -0500409 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
410 if (copy_siginfo_to_user(&frame->info, &ksig->info))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800411 return -EFAULT;
412 }
413
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800414 put_user_try {
415 /* Create the ucontext. */
416 if (cpu_has_xsave)
417 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
418 else
419 put_user_ex(0, &frame->uc.uc_flags);
420 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100421 save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800422
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800423 /* Set up to return from userspace. If provided, use a stub
424 already in userspace. */
425 /* x86-64 should always use SA_RESTORER. */
Al Viro235b8022012-11-09 23:51:47 -0500426 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
427 put_user_ex(ksig->ka.sa.sa_restorer, &frame->pretcode);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800428 } else {
429 /* could use a vstub here */
430 err |= -EFAULT;
431 }
432 } put_user_catch(err);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800433
H. Peter Anvin5e883532012-09-21 12:43:15 -0700434 err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
435 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
436
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800437 if (err)
438 return -EFAULT;
439
440 /* Set up registers for signal handler */
441 regs->di = sig;
442 /* In case the signal handler was declared without prototypes */
443 regs->ax = 0;
444
445 /* This also works for non SA_SIGINFO handlers because they expect the
446 next argument after the signal number on the stack. */
447 regs->si = (unsigned long)&frame->info;
448 regs->dx = (unsigned long)&frame->uc;
Al Viro235b8022012-11-09 23:51:47 -0500449 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800450
451 regs->sp = (unsigned long)frame;
452
Andy Lutomirskic6f20622015-03-12 13:57:51 -0700453 /*
454 * Set up the CS and SS registers to run signal handlers in
455 * 64-bit mode, even if the handler happens to be interrupting
456 * 32-bit or 16-bit code.
457 *
458 * SS is subtle. In 64-bit mode, we don't need any particular
459 * SS descriptor, but we do need SS to be valid. It's possible
460 * that the old SS is entirely bogus -- this can happen if the
461 * signal we're trying to deliver is #GP or #SS caused by a bad
462 * SS value.
463 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800464 regs->cs = __USER_CS;
Andy Lutomirskic6f20622015-03-12 13:57:51 -0700465 regs->ss = __USER_DS;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800466
467 return 0;
468}
469#endif /* CONFIG_X86_32 */
470
Al Viro235b8022012-11-09 23:51:47 -0500471static int x32_setup_rt_frame(struct ksignal *ksig,
472 compat_sigset_t *set,
Suresh Siddha050902c2012-07-24 16:05:27 -0700473 struct pt_regs *regs)
474{
475#ifdef CONFIG_X86_X32_ABI
476 struct rt_sigframe_x32 __user *frame;
477 void __user *restorer;
478 int err = 0;
479 void __user *fpstate = NULL;
480
Al Viro235b8022012-11-09 23:51:47 -0500481 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Suresh Siddha050902c2012-07-24 16:05:27 -0700482
483 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
484 return -EFAULT;
485
Al Viro235b8022012-11-09 23:51:47 -0500486 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
487 if (copy_siginfo_to_user32(&frame->info, &ksig->info))
Suresh Siddha050902c2012-07-24 16:05:27 -0700488 return -EFAULT;
489 }
490
491 put_user_try {
492 /* Create the ucontext. */
493 if (cpu_has_xsave)
494 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
495 else
496 put_user_ex(0, &frame->uc.uc_flags);
497 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100498 compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Suresh Siddha050902c2012-07-24 16:05:27 -0700499 put_user_ex(0, &frame->uc.uc__pad0);
Suresh Siddha050902c2012-07-24 16:05:27 -0700500
Al Viro235b8022012-11-09 23:51:47 -0500501 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
502 restorer = ksig->ka.sa.sa_restorer;
Suresh Siddha050902c2012-07-24 16:05:27 -0700503 } else {
504 /* could use a vstub here */
505 restorer = NULL;
506 err |= -EFAULT;
507 }
508 put_user_ex(restorer, &frame->pretcode);
509 } put_user_catch(err);
510
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700511 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
512 regs, set->sig[0]);
513 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
514
Suresh Siddha050902c2012-07-24 16:05:27 -0700515 if (err)
516 return -EFAULT;
517
518 /* Set up registers for signal handler */
519 regs->sp = (unsigned long) frame;
Al Viro235b8022012-11-09 23:51:47 -0500520 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Suresh Siddha050902c2012-07-24 16:05:27 -0700521
522 /* We use the x32 calling convention here... */
Al Viro235b8022012-11-09 23:51:47 -0500523 regs->di = ksig->sig;
Suresh Siddha050902c2012-07-24 16:05:27 -0700524 regs->si = (unsigned long) &frame->info;
525 regs->dx = (unsigned long) &frame->uc;
526
527 loadsegment(ds, __USER_DS);
528 loadsegment(es, __USER_DS);
529
530 regs->cs = __USER_CS;
531 regs->ss = __USER_DS;
532#endif /* CONFIG_X86_X32_ABI */
533
534 return 0;
535}
536
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800537/*
538 * Do a signal return; undo the signal stack.
539 */
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800540#ifdef CONFIG_X86_32
Andi Kleenff491032013-08-05 15:02:40 -0700541asmlinkage unsigned long sys_sigreturn(void)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800542{
Al Viro3fe26fa2012-11-12 14:32:42 -0500543 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800544 struct sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800545 sigset_t set;
546
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800547 frame = (struct sigframe __user *)(regs->sp - 8);
548
549 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
550 goto badframe;
551 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
552 && __copy_from_user(&set.sig[1], &frame->extramask,
553 sizeof(frame->extramask))))
554 goto badframe;
555
Oleg Nesterov39822942011-07-10 21:27:27 +0200556 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800557
Brian Gerst6a3713f2015-04-04 08:58:23 -0400558 if (restore_sigcontext(regs, &frame->sc))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800559 goto badframe;
Brian Gerst6a3713f2015-04-04 08:58:23 -0400560 return regs->ax;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800561
562badframe:
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800563 signal_fault(regs, frame, "sigreturn");
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800564
565 return 0;
566}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800567#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800568
Andi Kleenff491032013-08-05 15:02:40 -0700569asmlinkage long sys_rt_sigreturn(void)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800570{
Al Viro3fe26fa2012-11-12 14:32:42 -0500571 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800572 struct rt_sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800573 sigset_t set;
574
575 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
576 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
577 goto badframe;
578 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
579 goto badframe;
580
Oleg Nesterove9bd3f02011-04-27 21:09:39 +0200581 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800582
Brian Gerst6a3713f2015-04-04 08:58:23 -0400583 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800584 goto badframe;
585
Al Viroc40702c2012-11-20 14:24:26 -0500586 if (restore_altstack(&frame->uc.uc_stack))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800587 goto badframe;
588
Brian Gerst6a3713f2015-04-04 08:58:23 -0400589 return regs->ax;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800590
591badframe:
592 signal_fault(regs, frame, "rt_sigreturn");
593 return 0;
594}
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596/*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100597 * OK, we're invoking a handler:
598 */
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700599static int signr_convert(int sig)
600{
Hiroshi Shimamoto96bf84b2008-10-29 18:44:08 -0700601#ifdef CONFIG_X86_32
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700602 struct thread_info *info = current_thread_info();
603
604 if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
605 return info->exec_domain->signal_invmap[sig];
Hiroshi Shimamoto96bf84b2008-10-29 18:44:08 -0700606#endif /* CONFIG_X86_32 */
Hiroshi Shimamoto8d8c13b2008-09-24 19:10:29 -0700607 return sig;
608}
609
Roland McGrath7c1def12005-06-23 00:08:21 -0700610static int
Al Viro235b8022012-11-09 23:51:47 -0500611setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700612{
Al Viro235b8022012-11-09 23:51:47 -0500613 int usig = signr_convert(ksig->sig);
Al Virob7f9a112012-05-02 09:59:21 -0400614 sigset_t *set = sigmask_to_save();
Suresh Siddha050902c2012-07-24 16:05:27 -0700615 compat_sigset_t *cset = (compat_sigset_t *) set;
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700616
617 /* Set up the stack frame */
Suresh Siddha050902c2012-07-24 16:05:27 -0700618 if (is_ia32_frame()) {
Al Viro235b8022012-11-09 23:51:47 -0500619 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
620 return ia32_setup_rt_frame(usig, ksig, cset, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700621 else
Al Viro235b8022012-11-09 23:51:47 -0500622 return ia32_setup_frame(usig, ksig, cset, regs);
Suresh Siddha050902c2012-07-24 16:05:27 -0700623 } else if (is_x32_frame()) {
Al Viro235b8022012-11-09 23:51:47 -0500624 return x32_setup_rt_frame(ksig, cset, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800625 } else {
Al Viro235b8022012-11-09 23:51:47 -0500626 return __setup_rt_frame(ksig->sig, ksig, set, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800627 }
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700628}
629
Al Viroa610d6e2012-05-21 23:42:15 -0400630static void
Al Viro235b8022012-11-09 23:51:47 -0500631handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Al Viro235b8022012-11-09 23:51:47 -0500633 bool failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 /* Are we from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700635 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 /* If so, check system call restarting.. */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700637 switch (syscall_get_error(current, regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800638 case -ERESTART_RESTARTBLOCK:
639 case -ERESTARTNOHAND:
640 regs->ax = -EINTR;
641 break;
642
643 case -ERESTARTSYS:
Al Viro235b8022012-11-09 23:51:47 -0500644 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100645 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800647 }
648 /* fallthrough */
649 case -ERESTARTNOINTR:
650 regs->ax = regs->orig_ax;
651 regs->ip -= 2;
652 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654 }
655
656 /*
Roland McGrathe1f28772008-01-30 13:30:50 +0100657 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
658 * flag so that register information in the sigcontext is correct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100660 if (unlikely(regs->flags & X86_EFLAGS_TF) &&
Roland McGrathe1f28772008-01-30 13:30:50 +0100661 likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100662 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Al Viro235b8022012-11-09 23:51:47 -0500664 failed = (setup_rt_frame(ksig, regs) < 0);
665 if (!failed) {
666 /*
667 * Clear the direction flag as per the ABI for function entry.
Jiri Olsaddd40da2013-05-01 17:25:43 +0200668 *
Jiri Olsa24cda102013-05-01 17:25:42 +0200669 * Clear RF when entering the signal handler, because
670 * it might disable possible debug exception from the
671 * signal handler.
Jiri Olsaddd40da2013-05-01 17:25:43 +0200672 *
Al Viro235b8022012-11-09 23:51:47 -0500673 * Clear TF when entering the signal handler, but
674 * notify any tracer that was single-stepping it.
675 * The tracer may want to single-step inside the
676 * handler too.
677 */
Jiri Olsaddd40da2013-05-01 17:25:43 +0200678 regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
Oleg Nesterov66463db2014-09-02 19:57:13 +0200679 /*
680 * Ensure the signal handler starts with the new fpu state.
681 */
682 if (used_math())
683 drop_init_fpu(current);
Al Viroa610d6e2012-05-21 23:42:15 -0400684 }
Al Viro235b8022012-11-09 23:51:47 -0500685 signal_setup_done(failed, ksig, test_thread_flag(TIF_SINGLESTEP));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700688#ifdef CONFIG_X86_32
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700689#define NR_restart_syscall __NR_restart_syscall
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700690#else /* !CONFIG_X86_32 */
691#define NR_restart_syscall \
692 test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
693#endif /* CONFIG_X86_32 */
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695/*
696 * Note that 'init' is a special process: it doesn't get signals it doesn't
697 * want to handle. Thus you cannot kill init even with a SIGKILL even by
698 * mistake.
699 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100700static void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Al Viro235b8022012-11-09 23:51:47 -0500702 struct ksignal ksig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Al Viro235b8022012-11-09 23:51:47 -0500704 if (get_signal(&ksig)) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100705 /* Whee! Actually deliver the signal. */
Al Viro235b8022012-11-09 23:51:47 -0500706 handle_signal(&ksig, regs);
David Howells283828f2006-01-18 17:44:00 -0800707 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /* Did we come from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700711 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /* Restart the system call - no handlers present */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700713 switch (syscall_get_error(current, regs)) {
David Howells283828f2006-01-18 17:44:00 -0800714 case -ERESTARTNOHAND:
715 case -ERESTARTSYS:
716 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100717 regs->ax = regs->orig_ax;
718 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800719 break;
720
721 case -ERESTART_RESTARTBLOCK:
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700722 regs->ax = NR_restart_syscall;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100723 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800724 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726 }
David Howells283828f2006-01-18 17:44:00 -0800727
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800728 /*
729 * If there's no signal to deliver, we just put the saved sigmask
730 * back.
731 */
Al Viro51a7b442012-05-21 23:33:55 -0400732 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
735/*
736 * notification of userspace execution resumption
David Howells283828f2006-01-18 17:44:00 -0800737 * - triggered by the TIF_WORK_MASK flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 */
Andi Kleen277d5b42013-08-05 15:02:43 -0700739__visible void
Ingo Molnar7e907f42008-03-06 10:33:08 +0100740do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +0100742 user_exit();
Frederic Weisbeckeredf55fd2012-07-11 20:26:39 +0200743
Oleg Nesterovdb023ea2012-09-14 19:05:46 +0200744 if (thread_info_flags & _TIF_UPROBE)
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530745 uprobe_notify_resume(regs);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 /* deal with pending signal delivery */
Roland McGrath5a8da0e2008-04-30 00:53:10 -0700748 if (thread_info_flags & _TIF_SIGPENDING)
David Howells283828f2006-01-18 17:44:00 -0800749 do_signal(regs);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100750
Roland McGrath59e52132008-04-19 19:10:57 -0700751 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
752 clear_thread_flag(TIF_NOTIFY_RESUME);
753 tracehook_notify_resume(regs);
754 }
Avi Kivity7c68af62009-09-19 09:40:22 +0300755 if (thread_info_flags & _TIF_USER_RETURN_NOTIFY)
756 fire_user_return_notifiers();
Roland McGrath59e52132008-04-19 19:10:57 -0700757
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +0100758 user_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700760
761void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
762{
763 struct task_struct *me = current;
764
765 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800766 printk("%s"
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700767 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800768 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700769 me->comm, me->pid, where, frame,
770 regs->ip, regs->sp, regs->orig_ax);
771 print_vma_addr(" in ", regs->ip);
Joe Perchesc767a542012-05-21 19:50:07 -0700772 pr_cont("\n");
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700773 }
774
775 force_sig(SIGSEGV, me);
776}
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800777
778#ifdef CONFIG_X86_X32_ABI
Al Viro3fe26fa2012-11-12 14:32:42 -0500779asmlinkage long sys32_x32_rt_sigreturn(void)
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800780{
Al Viro3fe26fa2012-11-12 14:32:42 -0500781 struct pt_regs *regs = current_pt_regs();
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800782 struct rt_sigframe_x32 __user *frame;
783 sigset_t set;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800784
785 frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
786
787 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
788 goto badframe;
789 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
790 goto badframe;
791
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800792 set_current_blocked(&set);
793
Brian Gerst6a3713f2015-04-04 08:58:23 -0400794 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800795 goto badframe;
796
Al Viro90268432012-12-14 14:47:53 -0500797 if (compat_restore_altstack(&frame->uc.uc_stack))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800798 goto badframe;
799
Brian Gerst6a3713f2015-04-04 08:58:23 -0400800 return regs->ax;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800801
802badframe:
803 signal_fault(regs, frame, "x32 rt_sigreturn");
804 return 0;
805}
806#endif