blob: 3e84ef16f65799459cd55e0c1bce50ae43a9195e [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
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -080064#ifdef CONFIG_X86_64
65/*
66 * If regs->ss will cause an IRET fault, change it. Otherwise leave it
67 * alone. Using this generally makes no sense unless
68 * user_64bit_mode(regs) would return true.
69 */
70static void force_valid_ss(struct pt_regs *regs)
71{
72 u32 ar;
73 asm volatile ("lar %[old_ss], %[ar]\n\t"
74 "jz 1f\n\t" /* If invalid: */
75 "xorl %[ar], %[ar]\n\t" /* set ar = 0 */
76 "1:"
77 : [ar] "=r" (ar)
78 : [old_ss] "rm" ((u16)regs->ss));
79
80 /*
81 * For a valid 64-bit user context, we need DPL 3, type
82 * read-write data or read-write exp-down data, and S and P
83 * set. We can't use VERW because VERW doesn't check the
84 * P bit.
85 */
86 ar &= AR_DPL_MASK | AR_S | AR_P | AR_TYPE_MASK;
87 if (ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA) &&
88 ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA_EXPDOWN))
89 regs->ss = __USER_DS;
90}
91#endif
92
Andy Lutomirski6c25da52016-02-16 15:09:03 -080093static int restore_sigcontext(struct pt_regs *regs,
94 struct sigcontext __user *sc,
95 unsigned long uc_flags)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080096{
Ingo Molnar530e5c82015-09-05 09:32:39 +020097 unsigned long buf_val;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080098 void __user *buf;
99 unsigned int tmpflags;
100 unsigned int err = 0;
101
102 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800103 current->restart_block.fn = do_no_restart_syscall;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800104
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800105 get_user_try {
106
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800107#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +0900108 set_user_gs(regs, GET_SEG(gs));
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800109 COPY_SEG(fs);
110 COPY_SEG(es);
111 COPY_SEG(ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800112#endif /* CONFIG_X86_32 */
113
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800114 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
Brian Gerst6a3713f2015-04-04 08:58:23 -0400115 COPY(dx); COPY(cx); COPY(ip); COPY(ax);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800116
117#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800118 COPY(r8);
119 COPY(r9);
120 COPY(r10);
121 COPY(r11);
122 COPY(r12);
123 COPY(r13);
124 COPY(r14);
125 COPY(r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800126#endif /* CONFIG_X86_64 */
127
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800128 COPY_SEG_CPL3(cs);
129 COPY_SEG_CPL3(ss);
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800130
131#ifdef CONFIG_X86_64
132 /*
133 * Fix up SS if needed for the benefit of old DOSEMU and
134 * CRIU.
135 */
136 if (unlikely(!(uc_flags & UC_STRICT_RESTORE_SS) &&
137 user_64bit_mode(regs)))
138 force_valid_ss(regs);
139#endif
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800140
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800141 get_user_ex(tmpflags, &sc->flags);
142 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
143 regs->orig_ax = -1; /* disable syscall checks */
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800144
Ingo Molnar530e5c82015-09-05 09:32:39 +0200145 get_user_ex(buf_val, &sc->fpstate);
146 buf = (void __user *)buf_val;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800147 } get_user_catch(err);
148
Ingo Molnar9dfe99b2015-04-29 20:55:19 +0200149 err |= fpu__restore_sig(buf, config_enabled(CONFIG_X86_32));
H. Peter Anvin5e883532012-09-21 12:43:15 -0700150
Brian Gerst1daeaa32015-03-21 18:54:21 -0400151 force_iret();
152
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800153 return err;
154}
155
H. Peter Anvin85139422012-02-19 07:43:09 -0800156int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
157 struct pt_regs *regs, unsigned long mask)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800158{
159 int err = 0;
160
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800161 put_user_try {
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800162
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800163#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +0900164 put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800165 put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
166 put_user_ex(regs->es, (unsigned int __user *)&sc->es);
167 put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800168#endif /* CONFIG_X86_32 */
169
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800170 put_user_ex(regs->di, &sc->di);
171 put_user_ex(regs->si, &sc->si);
172 put_user_ex(regs->bp, &sc->bp);
173 put_user_ex(regs->sp, &sc->sp);
174 put_user_ex(regs->bx, &sc->bx);
175 put_user_ex(regs->dx, &sc->dx);
176 put_user_ex(regs->cx, &sc->cx);
177 put_user_ex(regs->ax, &sc->ax);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800178#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800179 put_user_ex(regs->r8, &sc->r8);
180 put_user_ex(regs->r9, &sc->r9);
181 put_user_ex(regs->r10, &sc->r10);
182 put_user_ex(regs->r11, &sc->r11);
183 put_user_ex(regs->r12, &sc->r12);
184 put_user_ex(regs->r13, &sc->r13);
185 put_user_ex(regs->r14, &sc->r14);
186 put_user_ex(regs->r15, &sc->r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800187#endif /* CONFIG_X86_64 */
188
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530189 put_user_ex(current->thread.trap_nr, &sc->trapno);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800190 put_user_ex(current->thread.error_code, &sc->err);
191 put_user_ex(regs->ip, &sc->ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800192#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800193 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
194 put_user_ex(regs->flags, &sc->flags);
195 put_user_ex(regs->sp, &sc->sp_at_signal);
196 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800197#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800198 put_user_ex(regs->flags, &sc->flags);
199 put_user_ex(regs->cs, &sc->cs);
Linus Torvaldsed596cd2015-08-13 08:25:20 -0700200 put_user_ex(0, &sc->gs);
201 put_user_ex(0, &sc->fs);
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800202 put_user_ex(regs->ss, &sc->ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800203#endif /* CONFIG_X86_32 */
204
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800205 put_user_ex(fpstate, &sc->fpstate);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800206
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800207 /* non-iBCS2 extensions.. */
208 put_user_ex(mask, &sc->oldmask);
209 put_user_ex(current->thread.cr2, &sc->cr2);
210 } put_user_catch(err);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800211
212 return err;
213}
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 * Set up a signal frame.
217 */
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800218
219/*
220 * Determine which stack to use..
221 */
Hiroshi Shimamoto1fae0272009-02-27 10:30:32 -0800222static unsigned long align_sigframe(unsigned long sp)
223{
224#ifdef CONFIG_X86_32
225 /*
226 * Align the stack pointer according to the i386 ABI,
227 * i.e. so that on function entry ((sp + 4) & 15) == 0.
228 */
229 sp = ((sp + 4) & -16ul) - 4;
230#else /* !CONFIG_X86_32 */
231 sp = round_down(sp, 16) - 8;
232#endif
233 return sp;
234}
235
Denys Vlasenkodae0f302015-09-28 14:23:57 +0200236static void __user *
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800237get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
238 void __user **fpstate)
239{
240 /* Default to using normal stack */
Suresh Siddha72a671c2012-07-24 16:05:29 -0700241 unsigned long math_size = 0;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800242 unsigned long sp = regs->sp;
Suresh Siddha72a671c2012-07-24 16:05:29 -0700243 unsigned long buf_fx = 0;
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700244 int onsigstack = on_sig_stack(sp);
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200245 struct fpu *fpu = &current->thread.fpu;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800246
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800247 /* redzone */
Suresh Siddha050902c2012-07-24 16:05:27 -0700248 if (config_enabled(CONFIG_X86_64))
249 sp -= 128;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800250
Stas Sergeev0b4521e2016-04-14 23:20:02 +0300251 /* This is the X/Open sanctioned signal stack switching. */
252 if (ka->sa.sa_flags & SA_ONSTACK) {
253 if (sas_ss_flags(sp) == 0)
254 sp = current->sas_ss_sp + current->sas_ss_size;
255 } else if (config_enabled(CONFIG_X86_32) &&
256 !onsigstack &&
257 (regs->ss & 0xffff) != __USER_DS &&
258 !(ka->sa.sa_flags & SA_RESTORER) &&
259 ka->sa.sa_restorer) {
260 /* This is the legacy signal stack switching. */
261 sp = (unsigned long) ka->sa.sa_restorer;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800262 }
263
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200264 if (fpu->fpstate_active) {
Ingo Molnar82c0e452015-04-29 21:09:18 +0200265 sp = fpu__alloc_mathframe(sp, config_enabled(CONFIG_X86_32),
266 &buf_fx, &math_size);
Hiroshi Shimamoto25051702009-03-02 17:20:01 -0800267 *fpstate = (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800268 }
269
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700270 sp = align_sigframe(sp - frame_size);
271
272 /*
273 * If we are on the alternate signal stack and would overflow it, don't.
274 * Return an always-bogus address instead so we will die with SIGSEGV.
275 */
276 if (onsigstack && !likely(on_sig_stack(sp)))
277 return (void __user *)-1L;
278
Suresh Siddha72a671c2012-07-24 16:05:29 -0700279 /* save i387 and extended state */
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200280 if (fpu->fpstate_active &&
Ingo Molnarc8e140412015-04-28 11:35:20 +0200281 copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size) < 0)
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700282 return (void __user *)-1L;
283
284 return (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800285}
286
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800287#ifdef CONFIG_X86_32
288static const struct {
289 u16 poplmovl;
290 u32 val;
291 u16 int80;
292} __attribute__((packed)) retcode = {
293 0xb858, /* popl %eax; movl $..., %eax */
294 __NR_sigreturn,
295 0x80cd, /* int $0x80 */
296};
297
298static const struct {
299 u8 movl;
300 u32 val;
301 u16 int80;
302 u8 pad;
303} __attribute__((packed)) rt_retcode = {
304 0xb8, /* movl $..., %eax */
305 __NR_rt_sigreturn,
306 0x80cd, /* int $0x80 */
307 0
308};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Ingo Molnar7e907f42008-03-06 10:33:08 +0100310static int
Al Viro235b8022012-11-09 23:51:47 -0500311__setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700312 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100315 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700317 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Al Viro235b8022012-11-09 23:51:47 -0500319 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700322 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700324 if (__put_user(sig, &frame->sig))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700325 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700327 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700328 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 if (_NSIG_WORDS > 1) {
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700331 if (__copy_to_user(&frame->extramask, &set->sig[1],
332 sizeof(frame->extramask)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700333 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700336 if (current->mm->context.vdso)
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700337 restorer = current->mm->context.vdso +
Andy Lutomirski0a6d1fa2015-10-05 17:47:56 -0700338 vdso_image_32.sym___kernel_sigreturn;
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100339 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100340 restorer = &frame->retcode;
Al Viro235b8022012-11-09 23:51:47 -0500341 if (ksig->ka.sa.sa_flags & SA_RESTORER)
342 restorer = ksig->ka.sa.sa_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 /* Set up to return from userspace. */
345 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100348 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 *
350 * WE DO NOT USE IT ANY MORE! It's only left here for historical
351 * reasons and because gdb uses it as a signature to notice
352 * signal handler stack frames.
353 */
Hiroshi Shimamoto4a612042008-11-11 19:09:29 -0800354 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700357 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100360 regs->sp = (unsigned long)frame;
Al Viro235b8022012-11-09 23:51:47 -0500361 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100362 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800363 regs->dx = 0;
364 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100366 regs->ds = __USER_DS;
367 regs->es = __USER_DS;
368 regs->ss = __USER_DS;
369 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
David Howells283828f2006-01-18 17:44:00 -0800371 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
Al Viro235b8022012-11-09 23:51:47 -0500374static int __setup_rt_frame(int sig, struct ksignal *ksig,
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700375 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100378 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700380 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Al Viro235b8022012-11-09 23:51:47 -0500382 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700385 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800387 put_user_try {
388 put_user_ex(sig, &frame->sig);
389 put_user_ex(&frame->info, &frame->pinfo);
390 put_user_ex(&frame->uc, &frame->puc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800392 /* Create the ucontext. */
393 if (cpu_has_xsave)
394 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
395 else
396 put_user_ex(0, &frame->uc.uc_flags);
397 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100398 save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800400 /* Set up to return from userspace. */
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700401 restorer = current->mm->context.vdso +
Andy Lutomirski0a6d1fa2015-10-05 17:47:56 -0700402 vdso_image_32.sym___kernel_rt_sigreturn;
Al Viro235b8022012-11-09 23:51:47 -0500403 if (ksig->ka.sa.sa_flags & SA_RESTORER)
404 restorer = ksig->ka.sa.sa_restorer;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800405 put_user_ex(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100406
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800407 /*
408 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
409 *
410 * WE DO NOT USE IT ANY MORE! It's only left here for historical
411 * reasons and because gdb uses it as a signature to notice
412 * signal handler stack frames.
413 */
414 put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
415 } put_user_catch(err);
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700416
Al Viro235b8022012-11-09 23:51:47 -0500417 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
H. Peter Anvin5e883532012-09-21 12:43:15 -0700418 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
419 regs, set->sig[0]);
420 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700423 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100426 regs->sp = (unsigned long)frame;
Al Viro235b8022012-11-09 23:51:47 -0500427 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700428 regs->ax = (unsigned long)sig;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100429 regs->dx = (unsigned long)&frame->info;
430 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100432 regs->ds = __USER_DS;
433 regs->es = __USER_DS;
434 regs->ss = __USER_DS;
435 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
David Howells283828f2006-01-18 17:44:00 -0800437 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800439#else /* !CONFIG_X86_32 */
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800440static unsigned long frame_uc_flags(struct pt_regs *regs)
441{
442 unsigned long flags;
443
444 if (cpu_has_xsave)
445 flags = UC_FP_XSTATE | UC_SIGCONTEXT_SS;
446 else
447 flags = UC_SIGCONTEXT_SS;
448
449 if (likely(user_64bit_mode(regs)))
450 flags |= UC_STRICT_RESTORE_SS;
451
452 return flags;
453}
454
Al Viro235b8022012-11-09 23:51:47 -0500455static int __setup_rt_frame(int sig, struct ksignal *ksig,
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800456 sigset_t *set, struct pt_regs *regs)
457{
458 struct rt_sigframe __user *frame;
459 void __user *fp = NULL;
460 int err = 0;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800461
Al Viro235b8022012-11-09 23:51:47 -0500462 frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800463
464 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
465 return -EFAULT;
466
Al Viro235b8022012-11-09 23:51:47 -0500467 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
468 if (copy_siginfo_to_user(&frame->info, &ksig->info))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800469 return -EFAULT;
470 }
471
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800472 put_user_try {
473 /* Create the ucontext. */
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800474 put_user_ex(frame_uc_flags(regs), &frame->uc.uc_flags);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800475 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100476 save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800477
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800478 /* Set up to return from userspace. If provided, use a stub
479 already in userspace. */
480 /* x86-64 should always use SA_RESTORER. */
Al Viro235b8022012-11-09 23:51:47 -0500481 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
482 put_user_ex(ksig->ka.sa.sa_restorer, &frame->pretcode);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800483 } else {
484 /* could use a vstub here */
485 err |= -EFAULT;
486 }
487 } put_user_catch(err);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800488
H. Peter Anvin5e883532012-09-21 12:43:15 -0700489 err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
490 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
491
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800492 if (err)
493 return -EFAULT;
494
495 /* Set up registers for signal handler */
496 regs->di = sig;
497 /* In case the signal handler was declared without prototypes */
498 regs->ax = 0;
499
500 /* This also works for non SA_SIGINFO handlers because they expect the
501 next argument after the signal number on the stack. */
502 regs->si = (unsigned long)&frame->info;
503 regs->dx = (unsigned long)&frame->uc;
Al Viro235b8022012-11-09 23:51:47 -0500504 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800505
506 regs->sp = (unsigned long)frame;
507
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -0800508 /*
509 * Set up the CS and SS registers to run signal handlers in
510 * 64-bit mode, even if the handler happens to be interrupting
511 * 32-bit or 16-bit code.
512 *
513 * SS is subtle. In 64-bit mode, we don't need any particular
514 * SS descriptor, but we do need SS to be valid. It's possible
515 * that the old SS is entirely bogus -- this can happen if the
516 * signal we're trying to deliver is #GP or #SS caused by a bad
517 * SS value. We also have a compatbility issue here: DOSEMU
518 * relies on the contents of the SS register indicating the
519 * SS value at the time of the signal, even though that code in
520 * DOSEMU predates sigreturn's ability to restore SS. (DOSEMU
521 * avoids relying on sigreturn to restore SS; instead it uses
522 * a trampoline.) So we do our best: if the old SS was valid,
523 * we keep it. Otherwise we replace it.
524 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800525 regs->cs = __USER_CS;
526
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -0800527 if (unlikely(regs->ss != __USER_DS))
528 force_valid_ss(regs);
529
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800530 return 0;
531}
532#endif /* CONFIG_X86_32 */
533
Al Viro235b8022012-11-09 23:51:47 -0500534static int x32_setup_rt_frame(struct ksignal *ksig,
535 compat_sigset_t *set,
Suresh Siddha050902c2012-07-24 16:05:27 -0700536 struct pt_regs *regs)
537{
538#ifdef CONFIG_X86_X32_ABI
539 struct rt_sigframe_x32 __user *frame;
540 void __user *restorer;
541 int err = 0;
542 void __user *fpstate = NULL;
543
Al Viro235b8022012-11-09 23:51:47 -0500544 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Suresh Siddha050902c2012-07-24 16:05:27 -0700545
546 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
547 return -EFAULT;
548
Al Viro235b8022012-11-09 23:51:47 -0500549 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
550 if (copy_siginfo_to_user32(&frame->info, &ksig->info))
Suresh Siddha050902c2012-07-24 16:05:27 -0700551 return -EFAULT;
552 }
553
554 put_user_try {
555 /* Create the ucontext. */
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800556 put_user_ex(frame_uc_flags(regs), &frame->uc.uc_flags);
Suresh Siddha050902c2012-07-24 16:05:27 -0700557 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100558 compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Suresh Siddha050902c2012-07-24 16:05:27 -0700559 put_user_ex(0, &frame->uc.uc__pad0);
Suresh Siddha050902c2012-07-24 16:05:27 -0700560
Al Viro235b8022012-11-09 23:51:47 -0500561 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
562 restorer = ksig->ka.sa.sa_restorer;
Suresh Siddha050902c2012-07-24 16:05:27 -0700563 } else {
564 /* could use a vstub here */
565 restorer = NULL;
566 err |= -EFAULT;
567 }
568 put_user_ex(restorer, &frame->pretcode);
569 } put_user_catch(err);
570
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700571 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
572 regs, set->sig[0]);
573 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
574
Suresh Siddha050902c2012-07-24 16:05:27 -0700575 if (err)
576 return -EFAULT;
577
578 /* Set up registers for signal handler */
579 regs->sp = (unsigned long) frame;
Al Viro235b8022012-11-09 23:51:47 -0500580 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Suresh Siddha050902c2012-07-24 16:05:27 -0700581
582 /* We use the x32 calling convention here... */
Al Viro235b8022012-11-09 23:51:47 -0500583 regs->di = ksig->sig;
Suresh Siddha050902c2012-07-24 16:05:27 -0700584 regs->si = (unsigned long) &frame->info;
585 regs->dx = (unsigned long) &frame->uc;
586
587 loadsegment(ds, __USER_DS);
588 loadsegment(es, __USER_DS);
589
590 regs->cs = __USER_CS;
591 regs->ss = __USER_DS;
592#endif /* CONFIG_X86_X32_ABI */
593
594 return 0;
595}
596
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800597/*
598 * Do a signal return; undo the signal stack.
599 */
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800600#ifdef CONFIG_X86_32
Andi Kleenff491032013-08-05 15:02:40 -0700601asmlinkage unsigned long sys_sigreturn(void)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800602{
Al Viro3fe26fa2012-11-12 14:32:42 -0500603 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800604 struct sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800605 sigset_t set;
606
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800607 frame = (struct sigframe __user *)(regs->sp - 8);
608
609 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
610 goto badframe;
611 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
612 && __copy_from_user(&set.sig[1], &frame->extramask,
613 sizeof(frame->extramask))))
614 goto badframe;
615
Oleg Nesterov39822942011-07-10 21:27:27 +0200616 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800617
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800618 /*
619 * x86_32 has no uc_flags bits relevant to restore_sigcontext.
620 * Save a few cycles by skipping the __get_user.
621 */
622 if (restore_sigcontext(regs, &frame->sc, 0))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800623 goto badframe;
Brian Gerst6a3713f2015-04-04 08:58:23 -0400624 return regs->ax;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800625
626badframe:
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800627 signal_fault(regs, frame, "sigreturn");
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800628
629 return 0;
630}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800631#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800632
Andi Kleenff491032013-08-05 15:02:40 -0700633asmlinkage long sys_rt_sigreturn(void)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800634{
Al Viro3fe26fa2012-11-12 14:32:42 -0500635 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800636 struct rt_sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800637 sigset_t set;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800638 unsigned long uc_flags;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800639
640 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
641 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
642 goto badframe;
643 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
644 goto badframe;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800645 if (__get_user(uc_flags, &frame->uc.uc_flags))
646 goto badframe;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800647
Oleg Nesterove9bd3f02011-04-27 21:09:39 +0200648 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800649
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800650 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800651 goto badframe;
652
Al Viroc40702c2012-11-20 14:24:26 -0500653 if (restore_altstack(&frame->uc.uc_stack))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800654 goto badframe;
655
Brian Gerst6a3713f2015-04-04 08:58:23 -0400656 return regs->ax;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800657
658badframe:
659 signal_fault(regs, frame, "rt_sigreturn");
660 return 0;
661}
662
Ingo Molnar05012c12015-04-30 07:26:04 +0200663static inline int is_ia32_compat_frame(void)
664{
665 return config_enabled(CONFIG_IA32_EMULATION) &&
666 test_thread_flag(TIF_IA32);
667}
668
669static inline int is_ia32_frame(void)
670{
671 return config_enabled(CONFIG_X86_32) || is_ia32_compat_frame();
672}
673
674static inline int is_x32_frame(void)
675{
676 return config_enabled(CONFIG_X86_X32_ABI) && test_thread_flag(TIF_X32);
677}
678
Roland McGrath7c1def12005-06-23 00:08:21 -0700679static int
Al Viro235b8022012-11-09 23:51:47 -0500680setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700681{
Richard Weinberger3050a352014-07-13 17:43:51 +0200682 int usig = ksig->sig;
Al Virob7f9a112012-05-02 09:59:21 -0400683 sigset_t *set = sigmask_to_save();
Suresh Siddha050902c2012-07-24 16:05:27 -0700684 compat_sigset_t *cset = (compat_sigset_t *) set;
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700685
686 /* Set up the stack frame */
Suresh Siddha050902c2012-07-24 16:05:27 -0700687 if (is_ia32_frame()) {
Al Viro235b8022012-11-09 23:51:47 -0500688 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
689 return ia32_setup_rt_frame(usig, ksig, cset, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700690 else
Al Viro235b8022012-11-09 23:51:47 -0500691 return ia32_setup_frame(usig, ksig, cset, regs);
Suresh Siddha050902c2012-07-24 16:05:27 -0700692 } else if (is_x32_frame()) {
Al Viro235b8022012-11-09 23:51:47 -0500693 return x32_setup_rt_frame(ksig, cset, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800694 } else {
Al Viro235b8022012-11-09 23:51:47 -0500695 return __setup_rt_frame(ksig->sig, ksig, set, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800696 }
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700697}
698
Al Viroa610d6e2012-05-21 23:42:15 -0400699static void
Al Viro235b8022012-11-09 23:51:47 -0500700handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700702 bool stepping, failed;
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200703 struct fpu *fpu = &current->thread.fpu;
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700704
Brian Gerst5ed92a82015-07-29 01:41:19 -0400705 if (v8086_mode(regs))
706 save_v86_state((struct kernel_vm86_regs *) regs, VM86_SIGNAL);
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 /* Are we from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700709 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /* If so, check system call restarting.. */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700711 switch (syscall_get_error(current, regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800712 case -ERESTART_RESTARTBLOCK:
713 case -ERESTARTNOHAND:
714 regs->ax = -EINTR;
715 break;
716
717 case -ERESTARTSYS:
Al Viro235b8022012-11-09 23:51:47 -0500718 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100719 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800721 }
722 /* fallthrough */
723 case -ERESTARTNOINTR:
724 regs->ax = regs->orig_ax;
725 regs->ip -= 2;
726 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
728 }
729
730 /*
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700731 * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now
732 * so that register information in the sigcontext is correct and
733 * then notify the tracer before entering the signal handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 */
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700735 stepping = test_thread_flag(TIF_SINGLESTEP);
736 if (stepping)
737 user_disable_single_step(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Al Viro235b8022012-11-09 23:51:47 -0500739 failed = (setup_rt_frame(ksig, regs) < 0);
740 if (!failed) {
741 /*
742 * Clear the direction flag as per the ABI for function entry.
Jiri Olsaddd40da2013-05-01 17:25:43 +0200743 *
Jiri Olsa24cda102013-05-01 17:25:42 +0200744 * Clear RF when entering the signal handler, because
745 * it might disable possible debug exception from the
746 * signal handler.
Jiri Olsaddd40da2013-05-01 17:25:43 +0200747 *
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700748 * Clear TF for the case when it wasn't set by debugger to
749 * avoid the recursive send_sigtrap() in SIGTRAP handler.
Al Viro235b8022012-11-09 23:51:47 -0500750 */
Jiri Olsaddd40da2013-05-01 17:25:43 +0200751 regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
Oleg Nesterov66463db2014-09-02 19:57:13 +0200752 /*
753 * Ensure the signal handler starts with the new fpu state.
754 */
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200755 if (fpu->fpstate_active)
Ingo Molnarfbce7782015-04-30 07:12:46 +0200756 fpu__clear(fpu);
Al Viroa610d6e2012-05-21 23:42:15 -0400757 }
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700758 signal_setup_done(failed, ksig, stepping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300761static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs)
762{
Dmitry V. Levin95d97ad2015-12-17 23:56:52 +0000763#ifdef CONFIG_X86_64
764 if (is_ia32_task())
765 return __NR_ia32_restart_syscall;
766#endif
767#ifdef CONFIG_X86_X32_ABI
768 return __NR_restart_syscall | (regs->orig_ax & __X32_SYSCALL_BIT);
769#else
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300770 return __NR_restart_syscall;
Dmitry V. Levin95d97ad2015-12-17 23:56:52 +0000771#endif
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300772}
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774/*
775 * Note that 'init' is a special process: it doesn't get signals it doesn't
776 * want to handle. Thus you cannot kill init even with a SIGKILL even by
777 * mistake.
778 */
Andy Lutomirski1f484aa2015-07-03 12:44:23 -0700779void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Al Viro235b8022012-11-09 23:51:47 -0500781 struct ksignal ksig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Al Viro235b8022012-11-09 23:51:47 -0500783 if (get_signal(&ksig)) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100784 /* Whee! Actually deliver the signal. */
Al Viro235b8022012-11-09 23:51:47 -0500785 handle_signal(&ksig, regs);
David Howells283828f2006-01-18 17:44:00 -0800786 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 /* Did we come from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700790 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 /* Restart the system call - no handlers present */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700792 switch (syscall_get_error(current, regs)) {
David Howells283828f2006-01-18 17:44:00 -0800793 case -ERESTARTNOHAND:
794 case -ERESTARTSYS:
795 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100796 regs->ax = regs->orig_ax;
797 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800798 break;
799
800 case -ERESTART_RESTARTBLOCK:
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300801 regs->ax = get_nr_restart_syscall(regs);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100802 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800803 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
805 }
David Howells283828f2006-01-18 17:44:00 -0800806
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800807 /*
808 * If there's no signal to deliver, we just put the saved sigmask
809 * back.
810 */
Al Viro51a7b442012-05-21 23:33:55 -0400811 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700814void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
815{
816 struct task_struct *me = current;
817
818 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800819 printk("%s"
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700820 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800821 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700822 me->comm, me->pid, where, frame,
823 regs->ip, regs->sp, regs->orig_ax);
824 print_vma_addr(" in ", regs->ip);
Joe Perchesc767a542012-05-21 19:50:07 -0700825 pr_cont("\n");
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700826 }
827
828 force_sig(SIGSEGV, me);
829}
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800830
831#ifdef CONFIG_X86_X32_ABI
Al Viro3fe26fa2012-11-12 14:32:42 -0500832asmlinkage long sys32_x32_rt_sigreturn(void)
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800833{
Al Viro3fe26fa2012-11-12 14:32:42 -0500834 struct pt_regs *regs = current_pt_regs();
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800835 struct rt_sigframe_x32 __user *frame;
836 sigset_t set;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800837 unsigned long uc_flags;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800838
839 frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
840
841 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
842 goto badframe;
843 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
844 goto badframe;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800845 if (__get_user(uc_flags, &frame->uc.uc_flags))
846 goto badframe;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800847
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800848 set_current_blocked(&set);
849
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800850 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800851 goto badframe;
852
Al Viro90268432012-12-14 14:47:53 -0500853 if (compat_restore_altstack(&frame->uc.uc_stack))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800854 goto badframe;
855
Brian Gerst6a3713f2015-04-04 08:58:23 -0400856 return regs->ax;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800857
858badframe:
859 signal_fault(regs, frame, "x32 rt_sigreturn");
860 return 0;
861}
862#endif