blob: d87ce92d340448fe90f6fb34a297c161cdc36f7c [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{
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
Linus Torvaldsed596cd2015-08-13 08:25:20 -070096#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -080097 COPY_SEG_CPL3(cs);
98 COPY_SEG_CPL3(ss);
Linus Torvaldsed596cd2015-08-13 08:25:20 -070099#else /* !CONFIG_X86_32 */
100 /* Kernel saves and restores only the CS segment register on signals,
101 * which is the bare minimum needed to allow mixed 32/64-bit code.
102 * App's signal handler can save/restore other segments if needed. */
103 COPY_SEG_CPL3(cs);
104#endif /* CONFIG_X86_32 */
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800105
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800106 get_user_ex(tmpflags, &sc->flags);
107 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
108 regs->orig_ax = -1; /* disable syscall checks */
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800109
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800110 get_user_ex(buf, &sc->fpstate);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800111 } get_user_catch(err);
112
Ingo Molnar9dfe99b2015-04-29 20:55:19 +0200113 err |= fpu__restore_sig(buf, config_enabled(CONFIG_X86_32));
H. Peter Anvin5e883532012-09-21 12:43:15 -0700114
Brian Gerst1daeaa32015-03-21 18:54:21 -0400115 force_iret();
116
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800117 return err;
118}
119
H. Peter Anvin85139422012-02-19 07:43:09 -0800120int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
121 struct pt_regs *regs, unsigned long mask)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800122{
123 int err = 0;
124
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800125 put_user_try {
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800126
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800127#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +0900128 put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800129 put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
130 put_user_ex(regs->es, (unsigned int __user *)&sc->es);
131 put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800132#endif /* CONFIG_X86_32 */
133
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800134 put_user_ex(regs->di, &sc->di);
135 put_user_ex(regs->si, &sc->si);
136 put_user_ex(regs->bp, &sc->bp);
137 put_user_ex(regs->sp, &sc->sp);
138 put_user_ex(regs->bx, &sc->bx);
139 put_user_ex(regs->dx, &sc->dx);
140 put_user_ex(regs->cx, &sc->cx);
141 put_user_ex(regs->ax, &sc->ax);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800142#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800143 put_user_ex(regs->r8, &sc->r8);
144 put_user_ex(regs->r9, &sc->r9);
145 put_user_ex(regs->r10, &sc->r10);
146 put_user_ex(regs->r11, &sc->r11);
147 put_user_ex(regs->r12, &sc->r12);
148 put_user_ex(regs->r13, &sc->r13);
149 put_user_ex(regs->r14, &sc->r14);
150 put_user_ex(regs->r15, &sc->r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800151#endif /* CONFIG_X86_64 */
152
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530153 put_user_ex(current->thread.trap_nr, &sc->trapno);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800154 put_user_ex(current->thread.error_code, &sc->err);
155 put_user_ex(regs->ip, &sc->ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800156#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800157 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
158 put_user_ex(regs->flags, &sc->flags);
159 put_user_ex(regs->sp, &sc->sp_at_signal);
160 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800161#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800162 put_user_ex(regs->flags, &sc->flags);
163 put_user_ex(regs->cs, &sc->cs);
Linus Torvaldsed596cd2015-08-13 08:25:20 -0700164 put_user_ex(0, &sc->gs);
165 put_user_ex(0, &sc->fs);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800166#endif /* CONFIG_X86_32 */
167
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800168 put_user_ex(fpstate, &sc->fpstate);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800169
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800170 /* non-iBCS2 extensions.. */
171 put_user_ex(mask, &sc->oldmask);
172 put_user_ex(current->thread.cr2, &sc->cr2);
173 } put_user_catch(err);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800174
175 return err;
176}
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 * Set up a signal frame.
180 */
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800181
182/*
183 * Determine which stack to use..
184 */
Hiroshi Shimamoto1fae0272009-02-27 10:30:32 -0800185static unsigned long align_sigframe(unsigned long sp)
186{
187#ifdef CONFIG_X86_32
188 /*
189 * Align the stack pointer according to the i386 ABI,
190 * i.e. so that on function entry ((sp + 4) & 15) == 0.
191 */
192 sp = ((sp + 4) & -16ul) - 4;
193#else /* !CONFIG_X86_32 */
194 sp = round_down(sp, 16) - 8;
195#endif
196 return sp;
197}
198
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800199static inline void __user *
200get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
201 void __user **fpstate)
202{
203 /* Default to using normal stack */
Suresh Siddha72a671c2012-07-24 16:05:29 -0700204 unsigned long math_size = 0;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800205 unsigned long sp = regs->sp;
Suresh Siddha72a671c2012-07-24 16:05:29 -0700206 unsigned long buf_fx = 0;
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700207 int onsigstack = on_sig_stack(sp);
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200208 struct fpu *fpu = &current->thread.fpu;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800209
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800210 /* redzone */
Suresh Siddha050902c2012-07-24 16:05:27 -0700211 if (config_enabled(CONFIG_X86_64))
212 sp -= 128;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800213
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700214 if (!onsigstack) {
215 /* This is the X/Open sanctioned signal stack switching. */
216 if (ka->sa.sa_flags & SA_ONSTACK) {
Hiroshi Shimamoto0f8f3082009-03-26 10:03:08 -0700217 if (current->sas_ss_size)
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700218 sp = current->sas_ss_sp + current->sas_ss_size;
Suresh Siddha050902c2012-07-24 16:05:27 -0700219 } else if (config_enabled(CONFIG_X86_32) &&
220 (regs->ss & 0xffff) != __USER_DS &&
221 !(ka->sa.sa_flags & SA_RESTORER) &&
222 ka->sa.sa_restorer) {
223 /* This is the legacy signal stack switching. */
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700224 sp = (unsigned long) ka->sa.sa_restorer;
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700225 }
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800226 }
227
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200228 if (fpu->fpstate_active) {
Ingo Molnar82c0e452015-04-29 21:09:18 +0200229 sp = fpu__alloc_mathframe(sp, config_enabled(CONFIG_X86_32),
230 &buf_fx, &math_size);
Hiroshi Shimamoto25051702009-03-02 17:20:01 -0800231 *fpstate = (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800232 }
233
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700234 sp = align_sigframe(sp - frame_size);
235
236 /*
237 * If we are on the alternate signal stack and would overflow it, don't.
238 * Return an always-bogus address instead so we will die with SIGSEGV.
239 */
240 if (onsigstack && !likely(on_sig_stack(sp)))
241 return (void __user *)-1L;
242
Suresh Siddha72a671c2012-07-24 16:05:29 -0700243 /* save i387 and extended state */
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200244 if (fpu->fpstate_active &&
Ingo Molnarc8e140412015-04-28 11:35:20 +0200245 copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size) < 0)
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700246 return (void __user *)-1L;
247
248 return (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800249}
250
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800251#ifdef CONFIG_X86_32
252static const struct {
253 u16 poplmovl;
254 u32 val;
255 u16 int80;
256} __attribute__((packed)) retcode = {
257 0xb858, /* popl %eax; movl $..., %eax */
258 __NR_sigreturn,
259 0x80cd, /* int $0x80 */
260};
261
262static const struct {
263 u8 movl;
264 u32 val;
265 u16 int80;
266 u8 pad;
267} __attribute__((packed)) rt_retcode = {
268 0xb8, /* movl $..., %eax */
269 __NR_rt_sigreturn,
270 0x80cd, /* int $0x80 */
271 0
272};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Ingo Molnar7e907f42008-03-06 10:33:08 +0100274static int
Al Viro235b8022012-11-09 23:51:47 -0500275__setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700276 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100279 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700281 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Al Viro235b8022012-11-09 23:51:47 -0500283 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700286 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700288 if (__put_user(sig, &frame->sig))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700289 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700291 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700292 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 if (_NSIG_WORDS > 1) {
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700295 if (__copy_to_user(&frame->extramask, &set->sig[1],
296 sizeof(frame->extramask)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700297 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700300 if (current->mm->context.vdso)
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700301 restorer = current->mm->context.vdso +
Andy Lutomirski0a6d1fa2015-10-05 17:47:56 -0700302 vdso_image_32.sym___kernel_sigreturn;
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100303 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100304 restorer = &frame->retcode;
Al Viro235b8022012-11-09 23:51:47 -0500305 if (ksig->ka.sa.sa_flags & SA_RESTORER)
306 restorer = ksig->ka.sa.sa_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 /* Set up to return from userspace. */
309 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100312 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 *
314 * WE DO NOT USE IT ANY MORE! It's only left here for historical
315 * reasons and because gdb uses it as a signature to notice
316 * signal handler stack frames.
317 */
Hiroshi Shimamoto4a612042008-11-11 19:09:29 -0800318 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700321 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100324 regs->sp = (unsigned long)frame;
Al Viro235b8022012-11-09 23:51:47 -0500325 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100326 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800327 regs->dx = 0;
328 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100330 regs->ds = __USER_DS;
331 regs->es = __USER_DS;
332 regs->ss = __USER_DS;
333 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
David Howells283828f2006-01-18 17:44:00 -0800335 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
Al Viro235b8022012-11-09 23:51:47 -0500338static int __setup_rt_frame(int sig, struct ksignal *ksig,
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700339 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100342 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700344 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Al Viro235b8022012-11-09 23:51:47 -0500346 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700349 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800351 put_user_try {
352 put_user_ex(sig, &frame->sig);
353 put_user_ex(&frame->info, &frame->pinfo);
354 put_user_ex(&frame->uc, &frame->puc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800356 /* Create the ucontext. */
357 if (cpu_has_xsave)
358 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
359 else
360 put_user_ex(0, &frame->uc.uc_flags);
361 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100362 save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800364 /* Set up to return from userspace. */
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700365 restorer = current->mm->context.vdso +
Andy Lutomirski0a6d1fa2015-10-05 17:47:56 -0700366 vdso_image_32.sym___kernel_rt_sigreturn;
Al Viro235b8022012-11-09 23:51:47 -0500367 if (ksig->ka.sa.sa_flags & SA_RESTORER)
368 restorer = ksig->ka.sa.sa_restorer;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800369 put_user_ex(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100370
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800371 /*
372 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
373 *
374 * WE DO NOT USE IT ANY MORE! It's only left here for historical
375 * reasons and because gdb uses it as a signature to notice
376 * signal handler stack frames.
377 */
378 put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
379 } put_user_catch(err);
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700380
Al Viro235b8022012-11-09 23:51:47 -0500381 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
H. Peter Anvin5e883532012-09-21 12:43:15 -0700382 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
383 regs, set->sig[0]);
384 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700387 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100390 regs->sp = (unsigned long)frame;
Al Viro235b8022012-11-09 23:51:47 -0500391 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700392 regs->ax = (unsigned long)sig;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100393 regs->dx = (unsigned long)&frame->info;
394 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100396 regs->ds = __USER_DS;
397 regs->es = __USER_DS;
398 regs->ss = __USER_DS;
399 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
David Howells283828f2006-01-18 17:44:00 -0800401 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800403#else /* !CONFIG_X86_32 */
Al Viro235b8022012-11-09 23:51:47 -0500404static int __setup_rt_frame(int sig, struct ksignal *ksig,
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800405 sigset_t *set, struct pt_regs *regs)
406{
407 struct rt_sigframe __user *frame;
408 void __user *fp = NULL;
409 int err = 0;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800410
Al Viro235b8022012-11-09 23:51:47 -0500411 frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800412
413 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
414 return -EFAULT;
415
Al Viro235b8022012-11-09 23:51:47 -0500416 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
417 if (copy_siginfo_to_user(&frame->info, &ksig->info))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800418 return -EFAULT;
419 }
420
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800421 put_user_try {
422 /* Create the ucontext. */
423 if (cpu_has_xsave)
424 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
425 else
426 put_user_ex(0, &frame->uc.uc_flags);
427 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100428 save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800429
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800430 /* Set up to return from userspace. If provided, use a stub
431 already in userspace. */
432 /* x86-64 should always use SA_RESTORER. */
Al Viro235b8022012-11-09 23:51:47 -0500433 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
434 put_user_ex(ksig->ka.sa.sa_restorer, &frame->pretcode);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800435 } else {
436 /* could use a vstub here */
437 err |= -EFAULT;
438 }
439 } put_user_catch(err);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800440
H. Peter Anvin5e883532012-09-21 12:43:15 -0700441 err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
442 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
443
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800444 if (err)
445 return -EFAULT;
446
447 /* Set up registers for signal handler */
448 regs->di = sig;
449 /* In case the signal handler was declared without prototypes */
450 regs->ax = 0;
451
452 /* This also works for non SA_SIGINFO handlers because they expect the
453 next argument after the signal number on the stack. */
454 regs->si = (unsigned long)&frame->info;
455 regs->dx = (unsigned long)&frame->uc;
Al Viro235b8022012-11-09 23:51:47 -0500456 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800457
458 regs->sp = (unsigned long)frame;
459
Linus Torvaldsed596cd2015-08-13 08:25:20 -0700460 /* Set up the CS register to run signal handlers in 64-bit mode,
461 even if the handler happens to be interrupting 32-bit code. */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800462 regs->cs = __USER_CS;
463
464 return 0;
465}
466#endif /* CONFIG_X86_32 */
467
Al Viro235b8022012-11-09 23:51:47 -0500468static int x32_setup_rt_frame(struct ksignal *ksig,
469 compat_sigset_t *set,
Suresh Siddha050902c2012-07-24 16:05:27 -0700470 struct pt_regs *regs)
471{
472#ifdef CONFIG_X86_X32_ABI
473 struct rt_sigframe_x32 __user *frame;
474 void __user *restorer;
475 int err = 0;
476 void __user *fpstate = NULL;
477
Al Viro235b8022012-11-09 23:51:47 -0500478 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Suresh Siddha050902c2012-07-24 16:05:27 -0700479
480 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
481 return -EFAULT;
482
Al Viro235b8022012-11-09 23:51:47 -0500483 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
484 if (copy_siginfo_to_user32(&frame->info, &ksig->info))
Suresh Siddha050902c2012-07-24 16:05:27 -0700485 return -EFAULT;
486 }
487
488 put_user_try {
489 /* Create the ucontext. */
490 if (cpu_has_xsave)
491 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
492 else
493 put_user_ex(0, &frame->uc.uc_flags);
494 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100495 compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Suresh Siddha050902c2012-07-24 16:05:27 -0700496 put_user_ex(0, &frame->uc.uc__pad0);
Suresh Siddha050902c2012-07-24 16:05:27 -0700497
Al Viro235b8022012-11-09 23:51:47 -0500498 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
499 restorer = ksig->ka.sa.sa_restorer;
Suresh Siddha050902c2012-07-24 16:05:27 -0700500 } else {
501 /* could use a vstub here */
502 restorer = NULL;
503 err |= -EFAULT;
504 }
505 put_user_ex(restorer, &frame->pretcode);
506 } put_user_catch(err);
507
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700508 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
509 regs, set->sig[0]);
510 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
511
Suresh Siddha050902c2012-07-24 16:05:27 -0700512 if (err)
513 return -EFAULT;
514
515 /* Set up registers for signal handler */
516 regs->sp = (unsigned long) frame;
Al Viro235b8022012-11-09 23:51:47 -0500517 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Suresh Siddha050902c2012-07-24 16:05:27 -0700518
519 /* We use the x32 calling convention here... */
Al Viro235b8022012-11-09 23:51:47 -0500520 regs->di = ksig->sig;
Suresh Siddha050902c2012-07-24 16:05:27 -0700521 regs->si = (unsigned long) &frame->info;
522 regs->dx = (unsigned long) &frame->uc;
523
524 loadsegment(ds, __USER_DS);
525 loadsegment(es, __USER_DS);
526
527 regs->cs = __USER_CS;
528 regs->ss = __USER_DS;
529#endif /* CONFIG_X86_X32_ABI */
530
531 return 0;
532}
533
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800534/*
535 * Do a signal return; undo the signal stack.
536 */
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800537#ifdef CONFIG_X86_32
Andi Kleenff491032013-08-05 15:02:40 -0700538asmlinkage unsigned long sys_sigreturn(void)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800539{
Al Viro3fe26fa2012-11-12 14:32:42 -0500540 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800541 struct sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800542 sigset_t set;
543
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800544 frame = (struct sigframe __user *)(regs->sp - 8);
545
546 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
547 goto badframe;
548 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
549 && __copy_from_user(&set.sig[1], &frame->extramask,
550 sizeof(frame->extramask))))
551 goto badframe;
552
Oleg Nesterov39822942011-07-10 21:27:27 +0200553 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800554
Brian Gerst6a3713f2015-04-04 08:58:23 -0400555 if (restore_sigcontext(regs, &frame->sc))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800556 goto badframe;
Brian Gerst6a3713f2015-04-04 08:58:23 -0400557 return regs->ax;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800558
559badframe:
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800560 signal_fault(regs, frame, "sigreturn");
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800561
562 return 0;
563}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800564#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800565
Andi Kleenff491032013-08-05 15:02:40 -0700566asmlinkage long sys_rt_sigreturn(void)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800567{
Al Viro3fe26fa2012-11-12 14:32:42 -0500568 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800569 struct rt_sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800570 sigset_t set;
571
572 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
573 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
574 goto badframe;
575 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
576 goto badframe;
577
Oleg Nesterove9bd3f02011-04-27 21:09:39 +0200578 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800579
Brian Gerst6a3713f2015-04-04 08:58:23 -0400580 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800581 goto badframe;
582
Al Viroc40702c2012-11-20 14:24:26 -0500583 if (restore_altstack(&frame->uc.uc_stack))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800584 goto badframe;
585
Brian Gerst6a3713f2015-04-04 08:58:23 -0400586 return regs->ax;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800587
588badframe:
589 signal_fault(regs, frame, "rt_sigreturn");
590 return 0;
591}
592
Ingo Molnar05012c12015-04-30 07:26:04 +0200593static inline int is_ia32_compat_frame(void)
594{
595 return config_enabled(CONFIG_IA32_EMULATION) &&
596 test_thread_flag(TIF_IA32);
597}
598
599static inline int is_ia32_frame(void)
600{
601 return config_enabled(CONFIG_X86_32) || is_ia32_compat_frame();
602}
603
604static inline int is_x32_frame(void)
605{
606 return config_enabled(CONFIG_X86_X32_ABI) && test_thread_flag(TIF_X32);
607}
608
Roland McGrath7c1def12005-06-23 00:08:21 -0700609static int
Al Viro235b8022012-11-09 23:51:47 -0500610setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700611{
Richard Weinberger3050a352014-07-13 17:43:51 +0200612 int usig = ksig->sig;
Al Virob7f9a112012-05-02 09:59:21 -0400613 sigset_t *set = sigmask_to_save();
Suresh Siddha050902c2012-07-24 16:05:27 -0700614 compat_sigset_t *cset = (compat_sigset_t *) set;
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700615
616 /* Set up the stack frame */
Suresh Siddha050902c2012-07-24 16:05:27 -0700617 if (is_ia32_frame()) {
Al Viro235b8022012-11-09 23:51:47 -0500618 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
619 return ia32_setup_rt_frame(usig, ksig, cset, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700620 else
Al Viro235b8022012-11-09 23:51:47 -0500621 return ia32_setup_frame(usig, ksig, cset, regs);
Suresh Siddha050902c2012-07-24 16:05:27 -0700622 } else if (is_x32_frame()) {
Al Viro235b8022012-11-09 23:51:47 -0500623 return x32_setup_rt_frame(ksig, cset, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800624 } else {
Al Viro235b8022012-11-09 23:51:47 -0500625 return __setup_rt_frame(ksig->sig, ksig, set, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800626 }
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700627}
628
Al Viroa610d6e2012-05-21 23:42:15 -0400629static void
Al Viro235b8022012-11-09 23:51:47 -0500630handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700632 bool stepping, failed;
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200633 struct fpu *fpu = &current->thread.fpu;
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700634
Brian Gerst5ed92a82015-07-29 01:41:19 -0400635 if (v8086_mode(regs))
636 save_v86_state((struct kernel_vm86_regs *) regs, VM86_SIGNAL);
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 /* Are we from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700639 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 /* If so, check system call restarting.. */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700641 switch (syscall_get_error(current, regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800642 case -ERESTART_RESTARTBLOCK:
643 case -ERESTARTNOHAND:
644 regs->ax = -EINTR;
645 break;
646
647 case -ERESTARTSYS:
Al Viro235b8022012-11-09 23:51:47 -0500648 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100649 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800651 }
652 /* fallthrough */
653 case -ERESTARTNOINTR:
654 regs->ax = regs->orig_ax;
655 regs->ip -= 2;
656 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658 }
659
660 /*
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700661 * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now
662 * so that register information in the sigcontext is correct and
663 * then notify the tracer before entering the signal handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 */
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700665 stepping = test_thread_flag(TIF_SINGLESTEP);
666 if (stepping)
667 user_disable_single_step(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Al Viro235b8022012-11-09 23:51:47 -0500669 failed = (setup_rt_frame(ksig, regs) < 0);
670 if (!failed) {
671 /*
672 * Clear the direction flag as per the ABI for function entry.
Jiri Olsaddd40da2013-05-01 17:25:43 +0200673 *
Jiri Olsa24cda102013-05-01 17:25:42 +0200674 * Clear RF when entering the signal handler, because
675 * it might disable possible debug exception from the
676 * signal handler.
Jiri Olsaddd40da2013-05-01 17:25:43 +0200677 *
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700678 * Clear TF for the case when it wasn't set by debugger to
679 * avoid the recursive send_sigtrap() in SIGTRAP handler.
Al Viro235b8022012-11-09 23:51:47 -0500680 */
Jiri Olsaddd40da2013-05-01 17:25:43 +0200681 regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
Oleg Nesterov66463db2014-09-02 19:57:13 +0200682 /*
683 * Ensure the signal handler starts with the new fpu state.
684 */
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200685 if (fpu->fpstate_active)
Ingo Molnarfbce7782015-04-30 07:12:46 +0200686 fpu__clear(fpu);
Al Viroa610d6e2012-05-21 23:42:15 -0400687 }
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700688 signal_setup_done(failed, ksig, stepping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700691#ifdef CONFIG_X86_32
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700692#define NR_restart_syscall __NR_restart_syscall
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700693#else /* !CONFIG_X86_32 */
694#define NR_restart_syscall \
695 test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
696#endif /* CONFIG_X86_32 */
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698/*
699 * Note that 'init' is a special process: it doesn't get signals it doesn't
700 * want to handle. Thus you cannot kill init even with a SIGKILL even by
701 * mistake.
702 */
Andy Lutomirski1f484aa2015-07-03 12:44:23 -0700703void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Al Viro235b8022012-11-09 23:51:47 -0500705 struct ksignal ksig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Al Viro235b8022012-11-09 23:51:47 -0500707 if (get_signal(&ksig)) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100708 /* Whee! Actually deliver the signal. */
Al Viro235b8022012-11-09 23:51:47 -0500709 handle_signal(&ksig, regs);
David Howells283828f2006-01-18 17:44:00 -0800710 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 /* Did we come from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700714 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 /* Restart the system call - no handlers present */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700716 switch (syscall_get_error(current, regs)) {
David Howells283828f2006-01-18 17:44:00 -0800717 case -ERESTARTNOHAND:
718 case -ERESTARTSYS:
719 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100720 regs->ax = regs->orig_ax;
721 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800722 break;
723
724 case -ERESTART_RESTARTBLOCK:
Hiroshi Shimamoto8fcd8e22008-09-05 16:27:39 -0700725 regs->ax = NR_restart_syscall;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100726 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800727 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
729 }
David Howells283828f2006-01-18 17:44:00 -0800730
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800731 /*
732 * If there's no signal to deliver, we just put the saved sigmask
733 * back.
734 */
Al Viro51a7b442012-05-21 23:33:55 -0400735 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700738void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
739{
740 struct task_struct *me = current;
741
742 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800743 printk("%s"
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700744 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800745 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700746 me->comm, me->pid, where, frame,
747 regs->ip, regs->sp, regs->orig_ax);
748 print_vma_addr(" in ", regs->ip);
Joe Perchesc767a542012-05-21 19:50:07 -0700749 pr_cont("\n");
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700750 }
751
752 force_sig(SIGSEGV, me);
753}
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800754
755#ifdef CONFIG_X86_X32_ABI
Al Viro3fe26fa2012-11-12 14:32:42 -0500756asmlinkage long sys32_x32_rt_sigreturn(void)
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800757{
Al Viro3fe26fa2012-11-12 14:32:42 -0500758 struct pt_regs *regs = current_pt_regs();
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800759 struct rt_sigframe_x32 __user *frame;
760 sigset_t set;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800761
762 frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
763
764 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
765 goto badframe;
766 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
767 goto badframe;
768
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800769 set_current_blocked(&set);
770
Brian Gerst6a3713f2015-04-04 08:58:23 -0400771 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800772 goto badframe;
773
Al Viro90268432012-12-14 14:47:53 -0500774 if (compat_restore_altstack(&frame->uc.uc_stack))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800775 goto badframe;
776
Brian Gerst6a3713f2015-04-04 08:58:23 -0400777 return regs->ax;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800778
779badframe:
780 signal_fault(regs, frame, "x32 rt_sigreturn");
781 return 0;
782}
783#endif