blob: cb6282c3638ffbd32bcb33663d8cefc17eac8a8e [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>
Brian Gerstba3e1272015-07-29 01:41:21 -040034#include <asm/vm86.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080035
36#ifdef CONFIG_X86_64
37#include <asm/proto.h>
38#include <asm/ia32_unistd.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{
Ingo Molnar530e5c82015-09-05 09:32:39 +020066 unsigned long buf_val;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080067 void __user *buf;
68 unsigned int tmpflags;
69 unsigned int err = 0;
70
71 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -080072 current->restart_block.fn = do_no_restart_syscall;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080073
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);
Brian Gerst6a3713f2015-04-04 08:58:23 -040084 COPY(dx); COPY(cx); COPY(ip); COPY(ax);
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
Linus Torvaldsed596cd2015-08-13 08:25:20 -070097#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080098 COPY_SEG_CPL3(cs);
99 COPY_SEG_CPL3(ss);
Linus Torvaldsed596cd2015-08-13 08:25:20 -0700100#else /* !CONFIG_X86_32 */
101 /* 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);
105#endif /* CONFIG_X86_32 */
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800106
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
Ingo Molnar530e5c82015-09-05 09:32:39 +0200111 get_user_ex(buf_val, &sc->fpstate);
112 buf = (void __user *)buf_val;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800113 } get_user_catch(err);
114
Ingo Molnar9dfe99b2015-04-29 20:55:19 +0200115 err |= fpu__restore_sig(buf, config_enabled(CONFIG_X86_32));
H. Peter Anvin5e883532012-09-21 12:43:15 -0700116
Brian Gerst1daeaa32015-03-21 18:54:21 -0400117 force_iret();
118
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800119 return err;
120}
121
H. Peter Anvin85139422012-02-19 07:43:09 -0800122int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
123 struct pt_regs *regs, unsigned long mask)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800124{
125 int err = 0;
126
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800127 put_user_try {
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800128
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800129#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +0900130 put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800131 put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
132 put_user_ex(regs->es, (unsigned int __user *)&sc->es);
133 put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800134#endif /* CONFIG_X86_32 */
135
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800136 put_user_ex(regs->di, &sc->di);
137 put_user_ex(regs->si, &sc->si);
138 put_user_ex(regs->bp, &sc->bp);
139 put_user_ex(regs->sp, &sc->sp);
140 put_user_ex(regs->bx, &sc->bx);
141 put_user_ex(regs->dx, &sc->dx);
142 put_user_ex(regs->cx, &sc->cx);
143 put_user_ex(regs->ax, &sc->ax);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800144#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800145 put_user_ex(regs->r8, &sc->r8);
146 put_user_ex(regs->r9, &sc->r9);
147 put_user_ex(regs->r10, &sc->r10);
148 put_user_ex(regs->r11, &sc->r11);
149 put_user_ex(regs->r12, &sc->r12);
150 put_user_ex(regs->r13, &sc->r13);
151 put_user_ex(regs->r14, &sc->r14);
152 put_user_ex(regs->r15, &sc->r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800153#endif /* CONFIG_X86_64 */
154
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530155 put_user_ex(current->thread.trap_nr, &sc->trapno);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800156 put_user_ex(current->thread.error_code, &sc->err);
157 put_user_ex(regs->ip, &sc->ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800158#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800159 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
160 put_user_ex(regs->flags, &sc->flags);
161 put_user_ex(regs->sp, &sc->sp_at_signal);
162 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800163#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800164 put_user_ex(regs->flags, &sc->flags);
165 put_user_ex(regs->cs, &sc->cs);
Linus Torvaldsed596cd2015-08-13 08:25:20 -0700166 put_user_ex(0, &sc->gs);
167 put_user_ex(0, &sc->fs);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800168#endif /* CONFIG_X86_32 */
169
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800170 put_user_ex(fpstate, &sc->fpstate);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800171
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800172 /* non-iBCS2 extensions.. */
173 put_user_ex(mask, &sc->oldmask);
174 put_user_ex(current->thread.cr2, &sc->cr2);
175 } put_user_catch(err);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800176
177 return err;
178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 * Set up a signal frame.
182 */
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800183
184/*
185 * Determine which stack to use..
186 */
Hiroshi Shimamoto1fae0272009-02-27 10:30:32 -0800187static unsigned long align_sigframe(unsigned long sp)
188{
189#ifdef CONFIG_X86_32
190 /*
191 * Align the stack pointer according to the i386 ABI,
192 * i.e. so that on function entry ((sp + 4) & 15) == 0.
193 */
194 sp = ((sp + 4) & -16ul) - 4;
195#else /* !CONFIG_X86_32 */
196 sp = round_down(sp, 16) - 8;
197#endif
198 return sp;
199}
200
Denys Vlasenkodae0f302015-09-28 14:23:57 +0200201static void __user *
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800202get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
203 void __user **fpstate)
204{
205 /* Default to using normal stack */
Suresh Siddha72a671c2012-07-24 16:05:29 -0700206 unsigned long math_size = 0;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800207 unsigned long sp = regs->sp;
Suresh Siddha72a671c2012-07-24 16:05:29 -0700208 unsigned long buf_fx = 0;
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700209 int onsigstack = on_sig_stack(sp);
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200210 struct fpu *fpu = &current->thread.fpu;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800211
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800212 /* redzone */
Suresh Siddha050902c2012-07-24 16:05:27 -0700213 if (config_enabled(CONFIG_X86_64))
214 sp -= 128;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800215
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700216 if (!onsigstack) {
217 /* This is the X/Open sanctioned signal stack switching. */
218 if (ka->sa.sa_flags & SA_ONSTACK) {
Hiroshi Shimamoto0f8f3082009-03-26 10:03:08 -0700219 if (current->sas_ss_size)
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700220 sp = current->sas_ss_sp + current->sas_ss_size;
Suresh Siddha050902c2012-07-24 16:05:27 -0700221 } else if (config_enabled(CONFIG_X86_32) &&
222 (regs->ss & 0xffff) != __USER_DS &&
223 !(ka->sa.sa_flags & SA_RESTORER) &&
224 ka->sa.sa_restorer) {
225 /* This is the legacy signal stack switching. */
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700226 sp = (unsigned long) ka->sa.sa_restorer;
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700227 }
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800228 }
229
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200230 if (fpu->fpstate_active) {
Ingo Molnar82c0e452015-04-29 21:09:18 +0200231 sp = fpu__alloc_mathframe(sp, config_enabled(CONFIG_X86_32),
232 &buf_fx, &math_size);
Hiroshi Shimamoto25051702009-03-02 17:20:01 -0800233 *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
Suresh Siddha72a671c2012-07-24 16:05:29 -0700245 /* save i387 and extended state */
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200246 if (fpu->fpstate_active &&
Ingo Molnarc8e140412015-04-28 11:35:20 +0200247 copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size) < 0)
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700248 return (void __user *)-1L;
249
250 return (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800251}
252
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800253#ifdef CONFIG_X86_32
254static const struct {
255 u16 poplmovl;
256 u32 val;
257 u16 int80;
258} __attribute__((packed)) retcode = {
259 0xb858, /* popl %eax; movl $..., %eax */
260 __NR_sigreturn,
261 0x80cd, /* int $0x80 */
262};
263
264static const struct {
265 u8 movl;
266 u32 val;
267 u16 int80;
268 u8 pad;
269} __attribute__((packed)) rt_retcode = {
270 0xb8, /* movl $..., %eax */
271 __NR_rt_sigreturn,
272 0x80cd, /* int $0x80 */
273 0
274};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Ingo Molnar7e907f42008-03-06 10:33:08 +0100276static int
Al Viro235b8022012-11-09 23:51:47 -0500277__setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700278 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100281 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700283 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Al Viro235b8022012-11-09 23:51:47 -0500285 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700288 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700290 if (__put_user(sig, &frame->sig))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700291 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700293 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700294 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 if (_NSIG_WORDS > 1) {
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700297 if (__copy_to_user(&frame->extramask, &set->sig[1],
298 sizeof(frame->extramask)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700299 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700302 if (current->mm->context.vdso)
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700303 restorer = current->mm->context.vdso +
Andy Lutomirski0a6d1fa2015-10-05 17:47:56 -0700304 vdso_image_32.sym___kernel_sigreturn;
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100305 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100306 restorer = &frame->retcode;
Al Viro235b8022012-11-09 23:51:47 -0500307 if (ksig->ka.sa.sa_flags & SA_RESTORER)
308 restorer = ksig->ka.sa.sa_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 /* Set up to return from userspace. */
311 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100314 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 *
316 * WE DO NOT USE IT ANY MORE! It's only left here for historical
317 * reasons and because gdb uses it as a signature to notice
318 * signal handler stack frames.
319 */
Hiroshi Shimamoto4a612042008-11-11 19:09:29 -0800320 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700323 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100326 regs->sp = (unsigned long)frame;
Al Viro235b8022012-11-09 23:51:47 -0500327 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100328 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800329 regs->dx = 0;
330 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100332 regs->ds = __USER_DS;
333 regs->es = __USER_DS;
334 regs->ss = __USER_DS;
335 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
David Howells283828f2006-01-18 17:44:00 -0800337 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
Al Viro235b8022012-11-09 23:51:47 -0500340static int __setup_rt_frame(int sig, struct ksignal *ksig,
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700341 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100344 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700346 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Al Viro235b8022012-11-09 23:51:47 -0500348 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700351 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800353 put_user_try {
354 put_user_ex(sig, &frame->sig);
355 put_user_ex(&frame->info, &frame->pinfo);
356 put_user_ex(&frame->uc, &frame->puc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800358 /* Create the ucontext. */
359 if (cpu_has_xsave)
360 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
361 else
362 put_user_ex(0, &frame->uc.uc_flags);
363 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100364 save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800366 /* Set up to return from userspace. */
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700367 restorer = current->mm->context.vdso +
Andy Lutomirski0a6d1fa2015-10-05 17:47:56 -0700368 vdso_image_32.sym___kernel_rt_sigreturn;
Al Viro235b8022012-11-09 23:51:47 -0500369 if (ksig->ka.sa.sa_flags & SA_RESTORER)
370 restorer = ksig->ka.sa.sa_restorer;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800371 put_user_ex(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100372
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800373 /*
374 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
375 *
376 * WE DO NOT USE IT ANY MORE! It's only left here for historical
377 * reasons and because gdb uses it as a signature to notice
378 * signal handler stack frames.
379 */
380 put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
381 } put_user_catch(err);
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700382
Al Viro235b8022012-11-09 23:51:47 -0500383 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
H. Peter Anvin5e883532012-09-21 12:43:15 -0700384 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
385 regs, set->sig[0]);
386 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700389 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100392 regs->sp = (unsigned long)frame;
Al Viro235b8022012-11-09 23:51:47 -0500393 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700394 regs->ax = (unsigned long)sig;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100395 regs->dx = (unsigned long)&frame->info;
396 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100398 regs->ds = __USER_DS;
399 regs->es = __USER_DS;
400 regs->ss = __USER_DS;
401 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
David Howells283828f2006-01-18 17:44:00 -0800403 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800405#else /* !CONFIG_X86_32 */
Al Viro235b8022012-11-09 23:51:47 -0500406static int __setup_rt_frame(int sig, struct ksignal *ksig,
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800407 sigset_t *set, struct pt_regs *regs)
408{
409 struct rt_sigframe __user *frame;
410 void __user *fp = NULL;
411 int err = 0;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800412
Al Viro235b8022012-11-09 23:51:47 -0500413 frame = get_sigframe(&ksig->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
Al Viro235b8022012-11-09 23:51:47 -0500418 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
419 if (copy_siginfo_to_user(&frame->info, &ksig->info))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800420 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);
Al Virobd1c149a2013-09-01 20:35:01 +0100430 save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800431
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800432 /* Set up to return from userspace. If provided, use a stub
433 already in userspace. */
434 /* x86-64 should always use SA_RESTORER. */
Al Viro235b8022012-11-09 23:51:47 -0500435 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
436 put_user_ex(ksig->ka.sa.sa_restorer, &frame->pretcode);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800437 } else {
438 /* could use a vstub here */
439 err |= -EFAULT;
440 }
441 } put_user_catch(err);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800442
H. Peter Anvin5e883532012-09-21 12:43:15 -0700443 err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
444 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
445
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800446 if (err)
447 return -EFAULT;
448
449 /* Set up registers for signal handler */
450 regs->di = sig;
451 /* In case the signal handler was declared without prototypes */
452 regs->ax = 0;
453
454 /* This also works for non SA_SIGINFO handlers because they expect the
455 next argument after the signal number on the stack. */
456 regs->si = (unsigned long)&frame->info;
457 regs->dx = (unsigned long)&frame->uc;
Al Viro235b8022012-11-09 23:51:47 -0500458 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800459
460 regs->sp = (unsigned long)frame;
461
Linus Torvaldsed596cd2015-08-13 08:25:20 -0700462 /* Set up the CS register to run signal handlers in 64-bit mode,
463 even if the handler happens to be interrupting 32-bit code. */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800464 regs->cs = __USER_CS;
465
466 return 0;
467}
468#endif /* CONFIG_X86_32 */
469
Al Viro235b8022012-11-09 23:51:47 -0500470static int x32_setup_rt_frame(struct ksignal *ksig,
471 compat_sigset_t *set,
Suresh Siddha050902c2012-07-24 16:05:27 -0700472 struct pt_regs *regs)
473{
474#ifdef CONFIG_X86_X32_ABI
475 struct rt_sigframe_x32 __user *frame;
476 void __user *restorer;
477 int err = 0;
478 void __user *fpstate = NULL;
479
Al Viro235b8022012-11-09 23:51:47 -0500480 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Suresh Siddha050902c2012-07-24 16:05:27 -0700481
482 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
483 return -EFAULT;
484
Al Viro235b8022012-11-09 23:51:47 -0500485 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
486 if (copy_siginfo_to_user32(&frame->info, &ksig->info))
Suresh Siddha050902c2012-07-24 16:05:27 -0700487 return -EFAULT;
488 }
489
490 put_user_try {
491 /* Create the ucontext. */
492 if (cpu_has_xsave)
493 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
494 else
495 put_user_ex(0, &frame->uc.uc_flags);
496 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100497 compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Suresh Siddha050902c2012-07-24 16:05:27 -0700498 put_user_ex(0, &frame->uc.uc__pad0);
Suresh Siddha050902c2012-07-24 16:05:27 -0700499
Al Viro235b8022012-11-09 23:51:47 -0500500 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
501 restorer = ksig->ka.sa.sa_restorer;
Suresh Siddha050902c2012-07-24 16:05:27 -0700502 } else {
503 /* could use a vstub here */
504 restorer = NULL;
505 err |= -EFAULT;
506 }
507 put_user_ex(restorer, &frame->pretcode);
508 } put_user_catch(err);
509
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700510 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
511 regs, set->sig[0]);
512 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
513
Suresh Siddha050902c2012-07-24 16:05:27 -0700514 if (err)
515 return -EFAULT;
516
517 /* Set up registers for signal handler */
518 regs->sp = (unsigned long) frame;
Al Viro235b8022012-11-09 23:51:47 -0500519 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Suresh Siddha050902c2012-07-24 16:05:27 -0700520
521 /* We use the x32 calling convention here... */
Al Viro235b8022012-11-09 23:51:47 -0500522 regs->di = ksig->sig;
Suresh Siddha050902c2012-07-24 16:05:27 -0700523 regs->si = (unsigned long) &frame->info;
524 regs->dx = (unsigned long) &frame->uc;
525
526 loadsegment(ds, __USER_DS);
527 loadsegment(es, __USER_DS);
528
529 regs->cs = __USER_CS;
530 regs->ss = __USER_DS;
531#endif /* CONFIG_X86_X32_ABI */
532
533 return 0;
534}
535
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800536/*
537 * Do a signal return; undo the signal stack.
538 */
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800539#ifdef CONFIG_X86_32
Andi Kleenff491032013-08-05 15:02:40 -0700540asmlinkage unsigned long sys_sigreturn(void)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800541{
Al Viro3fe26fa2012-11-12 14:32:42 -0500542 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800543 struct sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800544 sigset_t set;
545
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800546 frame = (struct sigframe __user *)(regs->sp - 8);
547
548 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
549 goto badframe;
550 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
551 && __copy_from_user(&set.sig[1], &frame->extramask,
552 sizeof(frame->extramask))))
553 goto badframe;
554
Oleg Nesterov39822942011-07-10 21:27:27 +0200555 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800556
Brian Gerst6a3713f2015-04-04 08:58:23 -0400557 if (restore_sigcontext(regs, &frame->sc))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800558 goto badframe;
Brian Gerst6a3713f2015-04-04 08:58:23 -0400559 return regs->ax;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800560
561badframe:
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800562 signal_fault(regs, frame, "sigreturn");
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800563
564 return 0;
565}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800566#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800567
Andi Kleenff491032013-08-05 15:02:40 -0700568asmlinkage long sys_rt_sigreturn(void)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800569{
Al Viro3fe26fa2012-11-12 14:32:42 -0500570 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800571 struct rt_sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800572 sigset_t set;
573
574 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
575 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
576 goto badframe;
577 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
578 goto badframe;
579
Oleg Nesterove9bd3f02011-04-27 21:09:39 +0200580 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800581
Brian Gerst6a3713f2015-04-04 08:58:23 -0400582 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800583 goto badframe;
584
Al Viroc40702c2012-11-20 14:24:26 -0500585 if (restore_altstack(&frame->uc.uc_stack))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800586 goto badframe;
587
Brian Gerst6a3713f2015-04-04 08:58:23 -0400588 return regs->ax;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800589
590badframe:
591 signal_fault(regs, frame, "rt_sigreturn");
592 return 0;
593}
594
Ingo Molnar05012c12015-04-30 07:26:04 +0200595static inline int is_ia32_compat_frame(void)
596{
597 return config_enabled(CONFIG_IA32_EMULATION) &&
598 test_thread_flag(TIF_IA32);
599}
600
601static inline int is_ia32_frame(void)
602{
603 return config_enabled(CONFIG_X86_32) || is_ia32_compat_frame();
604}
605
606static inline int is_x32_frame(void)
607{
608 return config_enabled(CONFIG_X86_X32_ABI) && test_thread_flag(TIF_X32);
609}
610
Roland McGrath7c1def12005-06-23 00:08:21 -0700611static int
Al Viro235b8022012-11-09 23:51:47 -0500612setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700613{
Richard Weinberger3050a352014-07-13 17:43:51 +0200614 int usig = ksig->sig;
Al Virob7f9a112012-05-02 09:59:21 -0400615 sigset_t *set = sigmask_to_save();
Suresh Siddha050902c2012-07-24 16:05:27 -0700616 compat_sigset_t *cset = (compat_sigset_t *) set;
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700617
618 /* Set up the stack frame */
Suresh Siddha050902c2012-07-24 16:05:27 -0700619 if (is_ia32_frame()) {
Al Viro235b8022012-11-09 23:51:47 -0500620 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
621 return ia32_setup_rt_frame(usig, ksig, cset, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700622 else
Al Viro235b8022012-11-09 23:51:47 -0500623 return ia32_setup_frame(usig, ksig, cset, regs);
Suresh Siddha050902c2012-07-24 16:05:27 -0700624 } else if (is_x32_frame()) {
Al Viro235b8022012-11-09 23:51:47 -0500625 return x32_setup_rt_frame(ksig, cset, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800626 } else {
Al Viro235b8022012-11-09 23:51:47 -0500627 return __setup_rt_frame(ksig->sig, ksig, set, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800628 }
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700629}
630
Al Viroa610d6e2012-05-21 23:42:15 -0400631static void
Al Viro235b8022012-11-09 23:51:47 -0500632handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700634 bool stepping, failed;
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200635 struct fpu *fpu = &current->thread.fpu;
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700636
Brian Gerst5ed92a82015-07-29 01:41:19 -0400637 if (v8086_mode(regs))
638 save_v86_state((struct kernel_vm86_regs *) regs, VM86_SIGNAL);
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 /* Are we from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700641 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 /* If so, check system call restarting.. */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700643 switch (syscall_get_error(current, regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800644 case -ERESTART_RESTARTBLOCK:
645 case -ERESTARTNOHAND:
646 regs->ax = -EINTR;
647 break;
648
649 case -ERESTARTSYS:
Al Viro235b8022012-11-09 23:51:47 -0500650 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100651 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800653 }
654 /* fallthrough */
655 case -ERESTARTNOINTR:
656 regs->ax = regs->orig_ax;
657 regs->ip -= 2;
658 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660 }
661
662 /*
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700663 * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now
664 * so that register information in the sigcontext is correct and
665 * then notify the tracer before entering the signal handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 */
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700667 stepping = test_thread_flag(TIF_SINGLESTEP);
668 if (stepping)
669 user_disable_single_step(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Al Viro235b8022012-11-09 23:51:47 -0500671 failed = (setup_rt_frame(ksig, regs) < 0);
672 if (!failed) {
673 /*
674 * Clear the direction flag as per the ABI for function entry.
Jiri Olsaddd40da2013-05-01 17:25:43 +0200675 *
Jiri Olsa24cda102013-05-01 17:25:42 +0200676 * Clear RF when entering the signal handler, because
677 * it might disable possible debug exception from the
678 * signal handler.
Jiri Olsaddd40da2013-05-01 17:25:43 +0200679 *
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700680 * Clear TF for the case when it wasn't set by debugger to
681 * avoid the recursive send_sigtrap() in SIGTRAP handler.
Al Viro235b8022012-11-09 23:51:47 -0500682 */
Jiri Olsaddd40da2013-05-01 17:25:43 +0200683 regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
Oleg Nesterov66463db2014-09-02 19:57:13 +0200684 /*
685 * Ensure the signal handler starts with the new fpu state.
686 */
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200687 if (fpu->fpstate_active)
Ingo Molnarfbce7782015-04-30 07:12:46 +0200688 fpu__clear(fpu);
Al Viroa610d6e2012-05-21 23:42:15 -0400689 }
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700690 signal_setup_done(failed, ksig, stepping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300693static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs)
694{
695#if defined(CONFIG_X86_32) || !defined(CONFIG_X86_64)
696 return __NR_restart_syscall;
697#else /* !CONFIG_X86_32 && CONFIG_X86_64 */
698 return test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall :
699 __NR_restart_syscall | (regs->orig_ax & __X32_SYSCALL_BIT);
700#endif /* CONFIG_X86_32 || !CONFIG_X86_64 */
701}
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703/*
704 * Note that 'init' is a special process: it doesn't get signals it doesn't
705 * want to handle. Thus you cannot kill init even with a SIGKILL even by
706 * mistake.
707 */
Andy Lutomirski1f484aa2015-07-03 12:44:23 -0700708void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Al Viro235b8022012-11-09 23:51:47 -0500710 struct ksignal ksig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Al Viro235b8022012-11-09 23:51:47 -0500712 if (get_signal(&ksig)) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100713 /* Whee! Actually deliver the signal. */
Al Viro235b8022012-11-09 23:51:47 -0500714 handle_signal(&ksig, regs);
David Howells283828f2006-01-18 17:44:00 -0800715 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 /* Did we come from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700719 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 /* Restart the system call - no handlers present */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700721 switch (syscall_get_error(current, regs)) {
David Howells283828f2006-01-18 17:44:00 -0800722 case -ERESTARTNOHAND:
723 case -ERESTARTSYS:
724 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100725 regs->ax = regs->orig_ax;
726 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800727 break;
728
729 case -ERESTART_RESTARTBLOCK:
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300730 regs->ax = get_nr_restart_syscall(regs);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100731 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800732 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734 }
David Howells283828f2006-01-18 17:44:00 -0800735
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800736 /*
737 * If there's no signal to deliver, we just put the saved sigmask
738 * back.
739 */
Al Viro51a7b442012-05-21 23:33:55 -0400740 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700743void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
744{
745 struct task_struct *me = current;
746
747 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800748 printk("%s"
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700749 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800750 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700751 me->comm, me->pid, where, frame,
752 regs->ip, regs->sp, regs->orig_ax);
753 print_vma_addr(" in ", regs->ip);
Joe Perchesc767a542012-05-21 19:50:07 -0700754 pr_cont("\n");
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700755 }
756
757 force_sig(SIGSEGV, me);
758}
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800759
760#ifdef CONFIG_X86_X32_ABI
Al Viro3fe26fa2012-11-12 14:32:42 -0500761asmlinkage long sys32_x32_rt_sigreturn(void)
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800762{
Al Viro3fe26fa2012-11-12 14:32:42 -0500763 struct pt_regs *regs = current_pt_regs();
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800764 struct rt_sigframe_x32 __user *frame;
765 sigset_t set;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800766
767 frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
768
769 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
770 goto badframe;
771 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
772 goto badframe;
773
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800774 set_current_blocked(&set);
775
Brian Gerst6a3713f2015-04-04 08:58:23 -0400776 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800777 goto badframe;
778
Al Viro90268432012-12-14 14:47:53 -0500779 if (compat_restore_altstack(&frame->uc.uc_stack))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800780 goto badframe;
781
Brian Gerst6a3713f2015-04-04 08:58:23 -0400782 return regs->ax;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800783
784badframe:
785 signal_fault(regs, frame, "x32 rt_sigreturn");
786 return 0;
787}
788#endif