blob: 763af1d0de64d8f8bbd323453e9ca7d266864c13 [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>
Dmitry Safonov68463512016-09-05 16:33:08 +030045#include <asm/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Tejun Heod9a89a22009-02-09 22:17:40 +090047#define COPY(x) do { \
48 get_user_ex(regs->x, &sc->x); \
49} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080050
Tejun Heod9a89a22009-02-09 22:17:40 +090051#define GET_SEG(seg) ({ \
52 unsigned short tmp; \
53 get_user_ex(tmp, &sc->seg); \
54 tmp; \
55})
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080056
Tejun Heod9a89a22009-02-09 22:17:40 +090057#define COPY_SEG(seg) do { \
58 regs->seg = GET_SEG(seg); \
59} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080060
Tejun Heod9a89a22009-02-09 22:17:40 +090061#define COPY_SEG_CPL3(seg) do { \
62 regs->seg = GET_SEG(seg) | 3; \
63} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080064
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -080065#ifdef CONFIG_X86_64
66/*
67 * If regs->ss will cause an IRET fault, change it. Otherwise leave it
68 * alone. Using this generally makes no sense unless
69 * user_64bit_mode(regs) would return true.
70 */
71static void force_valid_ss(struct pt_regs *regs)
72{
73 u32 ar;
74 asm volatile ("lar %[old_ss], %[ar]\n\t"
75 "jz 1f\n\t" /* If invalid: */
76 "xorl %[ar], %[ar]\n\t" /* set ar = 0 */
77 "1:"
78 : [ar] "=r" (ar)
79 : [old_ss] "rm" ((u16)regs->ss));
80
81 /*
82 * For a valid 64-bit user context, we need DPL 3, type
83 * read-write data or read-write exp-down data, and S and P
84 * set. We can't use VERW because VERW doesn't check the
85 * P bit.
86 */
87 ar &= AR_DPL_MASK | AR_S | AR_P | AR_TYPE_MASK;
88 if (ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA) &&
89 ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA_EXPDOWN))
90 regs->ss = __USER_DS;
91}
92#endif
93
Andy Lutomirski6c25da52016-02-16 15:09:03 -080094static int restore_sigcontext(struct pt_regs *regs,
95 struct sigcontext __user *sc,
96 unsigned long uc_flags)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080097{
Ingo Molnar530e5c82015-09-05 09:32:39 +020098 unsigned long buf_val;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080099 void __user *buf;
100 unsigned int tmpflags;
101 unsigned int err = 0;
102
103 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800104 current->restart_block.fn = do_no_restart_syscall;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800105
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800106 get_user_try {
107
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800108#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +0900109 set_user_gs(regs, GET_SEG(gs));
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800110 COPY_SEG(fs);
111 COPY_SEG(es);
112 COPY_SEG(ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800113#endif /* CONFIG_X86_32 */
114
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800115 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
Brian Gerst6a3713f2015-04-04 08:58:23 -0400116 COPY(dx); COPY(cx); COPY(ip); COPY(ax);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800117
118#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800119 COPY(r8);
120 COPY(r9);
121 COPY(r10);
122 COPY(r11);
123 COPY(r12);
124 COPY(r13);
125 COPY(r14);
126 COPY(r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800127#endif /* CONFIG_X86_64 */
128
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800129 COPY_SEG_CPL3(cs);
130 COPY_SEG_CPL3(ss);
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800131
132#ifdef CONFIG_X86_64
133 /*
134 * Fix up SS if needed for the benefit of old DOSEMU and
135 * CRIU.
136 */
137 if (unlikely(!(uc_flags & UC_STRICT_RESTORE_SS) &&
138 user_64bit_mode(regs)))
139 force_valid_ss(regs);
140#endif
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800141
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800142 get_user_ex(tmpflags, &sc->flags);
143 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
144 regs->orig_ax = -1; /* disable syscall checks */
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800145
Ingo Molnar530e5c82015-09-05 09:32:39 +0200146 get_user_ex(buf_val, &sc->fpstate);
147 buf = (void __user *)buf_val;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800148 } get_user_catch(err);
149
Masahiro Yamada97f26452016-08-03 13:45:50 -0700150 err |= fpu__restore_sig(buf, IS_ENABLED(CONFIG_X86_32));
H. Peter Anvin5e883532012-09-21 12:43:15 -0700151
Brian Gerst1daeaa32015-03-21 18:54:21 -0400152 force_iret();
153
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800154 return err;
155}
156
H. Peter Anvin85139422012-02-19 07:43:09 -0800157int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
158 struct pt_regs *regs, unsigned long mask)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800159{
160 int err = 0;
161
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800162 put_user_try {
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800163
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800164#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +0900165 put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800166 put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
167 put_user_ex(regs->es, (unsigned int __user *)&sc->es);
168 put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800169#endif /* CONFIG_X86_32 */
170
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800171 put_user_ex(regs->di, &sc->di);
172 put_user_ex(regs->si, &sc->si);
173 put_user_ex(regs->bp, &sc->bp);
174 put_user_ex(regs->sp, &sc->sp);
175 put_user_ex(regs->bx, &sc->bx);
176 put_user_ex(regs->dx, &sc->dx);
177 put_user_ex(regs->cx, &sc->cx);
178 put_user_ex(regs->ax, &sc->ax);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800179#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800180 put_user_ex(regs->r8, &sc->r8);
181 put_user_ex(regs->r9, &sc->r9);
182 put_user_ex(regs->r10, &sc->r10);
183 put_user_ex(regs->r11, &sc->r11);
184 put_user_ex(regs->r12, &sc->r12);
185 put_user_ex(regs->r13, &sc->r13);
186 put_user_ex(regs->r14, &sc->r14);
187 put_user_ex(regs->r15, &sc->r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800188#endif /* CONFIG_X86_64 */
189
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530190 put_user_ex(current->thread.trap_nr, &sc->trapno);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800191 put_user_ex(current->thread.error_code, &sc->err);
192 put_user_ex(regs->ip, &sc->ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800193#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800194 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
195 put_user_ex(regs->flags, &sc->flags);
196 put_user_ex(regs->sp, &sc->sp_at_signal);
197 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800198#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800199 put_user_ex(regs->flags, &sc->flags);
200 put_user_ex(regs->cs, &sc->cs);
Linus Torvaldsed596cd2015-08-13 08:25:20 -0700201 put_user_ex(0, &sc->gs);
202 put_user_ex(0, &sc->fs);
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800203 put_user_ex(regs->ss, &sc->ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800204#endif /* CONFIG_X86_32 */
205
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800206 put_user_ex(fpstate, &sc->fpstate);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800207
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800208 /* non-iBCS2 extensions.. */
209 put_user_ex(mask, &sc->oldmask);
210 put_user_ex(current->thread.cr2, &sc->cr2);
211 } put_user_catch(err);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800212
213 return err;
214}
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 * Set up a signal frame.
218 */
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800219
220/*
221 * Determine which stack to use..
222 */
Hiroshi Shimamoto1fae0272009-02-27 10:30:32 -0800223static unsigned long align_sigframe(unsigned long sp)
224{
225#ifdef CONFIG_X86_32
226 /*
227 * Align the stack pointer according to the i386 ABI,
228 * i.e. so that on function entry ((sp + 4) & 15) == 0.
229 */
230 sp = ((sp + 4) & -16ul) - 4;
231#else /* !CONFIG_X86_32 */
232 sp = round_down(sp, 16) - 8;
233#endif
234 return sp;
235}
236
Denys Vlasenkodae0f302015-09-28 14:23:57 +0200237static void __user *
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800238get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
239 void __user **fpstate)
240{
241 /* Default to using normal stack */
Suresh Siddha72a671c2012-07-24 16:05:29 -0700242 unsigned long math_size = 0;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800243 unsigned long sp = regs->sp;
Suresh Siddha72a671c2012-07-24 16:05:29 -0700244 unsigned long buf_fx = 0;
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700245 int onsigstack = on_sig_stack(sp);
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200246 struct fpu *fpu = &current->thread.fpu;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800247
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800248 /* redzone */
Masahiro Yamada97f26452016-08-03 13:45:50 -0700249 if (IS_ENABLED(CONFIG_X86_64))
Suresh Siddha050902c2012-07-24 16:05:27 -0700250 sp -= 128;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800251
Stas Sergeev0b4521e2016-04-14 23:20:02 +0300252 /* This is the X/Open sanctioned signal stack switching. */
253 if (ka->sa.sa_flags & SA_ONSTACK) {
254 if (sas_ss_flags(sp) == 0)
255 sp = current->sas_ss_sp + current->sas_ss_size;
Masahiro Yamada97f26452016-08-03 13:45:50 -0700256 } else if (IS_ENABLED(CONFIG_X86_32) &&
Stas Sergeev0b4521e2016-04-14 23:20:02 +0300257 !onsigstack &&
258 (regs->ss & 0xffff) != __USER_DS &&
259 !(ka->sa.sa_flags & SA_RESTORER) &&
260 ka->sa.sa_restorer) {
261 /* This is the legacy signal stack switching. */
262 sp = (unsigned long) ka->sa.sa_restorer;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800263 }
264
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200265 if (fpu->fpstate_active) {
Masahiro Yamada97f26452016-08-03 13:45:50 -0700266 sp = fpu__alloc_mathframe(sp, IS_ENABLED(CONFIG_X86_32),
Ingo Molnar82c0e452015-04-29 21:09:18 +0200267 &buf_fx, &math_size);
Hiroshi Shimamoto25051702009-03-02 17:20:01 -0800268 *fpstate = (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800269 }
270
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700271 sp = align_sigframe(sp - frame_size);
272
273 /*
274 * If we are on the alternate signal stack and would overflow it, don't.
275 * Return an always-bogus address instead so we will die with SIGSEGV.
276 */
277 if (onsigstack && !likely(on_sig_stack(sp)))
278 return (void __user *)-1L;
279
Suresh Siddha72a671c2012-07-24 16:05:29 -0700280 /* save i387 and extended state */
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200281 if (fpu->fpstate_active &&
Ingo Molnarc8e14042015-04-28 11:35:20 +0200282 copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size) < 0)
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700283 return (void __user *)-1L;
284
285 return (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800286}
287
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800288#ifdef CONFIG_X86_32
289static const struct {
290 u16 poplmovl;
291 u32 val;
292 u16 int80;
293} __attribute__((packed)) retcode = {
294 0xb858, /* popl %eax; movl $..., %eax */
295 __NR_sigreturn,
296 0x80cd, /* int $0x80 */
297};
298
299static const struct {
300 u8 movl;
301 u32 val;
302 u16 int80;
303 u8 pad;
304} __attribute__((packed)) rt_retcode = {
305 0xb8, /* movl $..., %eax */
306 __NR_rt_sigreturn,
307 0x80cd, /* int $0x80 */
308 0
309};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Ingo Molnar7e907f42008-03-06 10:33:08 +0100311static int
Al Viro235b8022012-11-09 23:51:47 -0500312__setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700313 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100316 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700318 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Al Viro235b8022012-11-09 23:51:47 -0500320 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700323 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700325 if (__put_user(sig, &frame->sig))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700326 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700328 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700329 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 if (_NSIG_WORDS > 1) {
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700332 if (__copy_to_user(&frame->extramask, &set->sig[1],
333 sizeof(frame->extramask)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700334 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700337 if (current->mm->context.vdso)
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700338 restorer = current->mm->context.vdso +
Andy Lutomirski0a6d1fa2015-10-05 17:47:56 -0700339 vdso_image_32.sym___kernel_sigreturn;
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100340 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100341 restorer = &frame->retcode;
Al Viro235b8022012-11-09 23:51:47 -0500342 if (ksig->ka.sa.sa_flags & SA_RESTORER)
343 restorer = ksig->ka.sa.sa_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 /* Set up to return from userspace. */
346 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100349 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 *
351 * WE DO NOT USE IT ANY MORE! It's only left here for historical
352 * reasons and because gdb uses it as a signature to notice
353 * signal handler stack frames.
354 */
Hiroshi Shimamoto4a612042008-11-11 19:09:29 -0800355 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700358 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100361 regs->sp = (unsigned long)frame;
Al Viro235b8022012-11-09 23:51:47 -0500362 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100363 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800364 regs->dx = 0;
365 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100367 regs->ds = __USER_DS;
368 regs->es = __USER_DS;
369 regs->ss = __USER_DS;
370 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
David Howells283828f2006-01-18 17:44:00 -0800372 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
Al Viro235b8022012-11-09 23:51:47 -0500375static int __setup_rt_frame(int sig, struct ksignal *ksig,
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700376 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100379 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700381 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Al Viro235b8022012-11-09 23:51:47 -0500383 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700386 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800388 put_user_try {
389 put_user_ex(sig, &frame->sig);
390 put_user_ex(&frame->info, &frame->pinfo);
391 put_user_ex(&frame->uc, &frame->puc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800393 /* Create the ucontext. */
Borislav Petkovd366bf72016-04-04 22:25:02 +0200394 if (boot_cpu_has(X86_FEATURE_XSAVE))
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800395 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
396 else
397 put_user_ex(0, &frame->uc.uc_flags);
398 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100399 save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800401 /* Set up to return from userspace. */
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700402 restorer = current->mm->context.vdso +
Andy Lutomirski0a6d1fa2015-10-05 17:47:56 -0700403 vdso_image_32.sym___kernel_rt_sigreturn;
Al Viro235b8022012-11-09 23:51:47 -0500404 if (ksig->ka.sa.sa_flags & SA_RESTORER)
405 restorer = ksig->ka.sa.sa_restorer;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800406 put_user_ex(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100407
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800408 /*
409 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
410 *
411 * WE DO NOT USE IT ANY MORE! It's only left here for historical
412 * reasons and because gdb uses it as a signature to notice
413 * signal handler stack frames.
414 */
415 put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
416 } put_user_catch(err);
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700417
Al Viro235b8022012-11-09 23:51:47 -0500418 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
H. Peter Anvin5e883532012-09-21 12:43:15 -0700419 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
420 regs, set->sig[0]);
421 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700424 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100427 regs->sp = (unsigned long)frame;
Al Viro235b8022012-11-09 23:51:47 -0500428 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700429 regs->ax = (unsigned long)sig;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100430 regs->dx = (unsigned long)&frame->info;
431 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100433 regs->ds = __USER_DS;
434 regs->es = __USER_DS;
435 regs->ss = __USER_DS;
436 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
David Howells283828f2006-01-18 17:44:00 -0800438 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800440#else /* !CONFIG_X86_32 */
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800441static unsigned long frame_uc_flags(struct pt_regs *regs)
442{
443 unsigned long flags;
444
Borislav Petkovd366bf72016-04-04 22:25:02 +0200445 if (boot_cpu_has(X86_FEATURE_XSAVE))
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800446 flags = UC_FP_XSTATE | UC_SIGCONTEXT_SS;
447 else
448 flags = UC_SIGCONTEXT_SS;
449
450 if (likely(user_64bit_mode(regs)))
451 flags |= UC_STRICT_RESTORE_SS;
452
453 return flags;
454}
455
Al Viro235b8022012-11-09 23:51:47 -0500456static int __setup_rt_frame(int sig, struct ksignal *ksig,
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800457 sigset_t *set, struct pt_regs *regs)
458{
459 struct rt_sigframe __user *frame;
460 void __user *fp = NULL;
461 int err = 0;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800462
Al Viro235b8022012-11-09 23:51:47 -0500463 frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800464
465 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
466 return -EFAULT;
467
Al Viro235b8022012-11-09 23:51:47 -0500468 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
469 if (copy_siginfo_to_user(&frame->info, &ksig->info))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800470 return -EFAULT;
471 }
472
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800473 put_user_try {
474 /* Create the ucontext. */
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800475 put_user_ex(frame_uc_flags(regs), &frame->uc.uc_flags);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800476 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100477 save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800478
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800479 /* Set up to return from userspace. If provided, use a stub
480 already in userspace. */
481 /* x86-64 should always use SA_RESTORER. */
Al Viro235b8022012-11-09 23:51:47 -0500482 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
483 put_user_ex(ksig->ka.sa.sa_restorer, &frame->pretcode);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800484 } else {
485 /* could use a vstub here */
486 err |= -EFAULT;
487 }
488 } put_user_catch(err);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800489
H. Peter Anvin5e883532012-09-21 12:43:15 -0700490 err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
491 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
492
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800493 if (err)
494 return -EFAULT;
495
496 /* Set up registers for signal handler */
497 regs->di = sig;
498 /* In case the signal handler was declared without prototypes */
499 regs->ax = 0;
500
501 /* This also works for non SA_SIGINFO handlers because they expect the
502 next argument after the signal number on the stack. */
503 regs->si = (unsigned long)&frame->info;
504 regs->dx = (unsigned long)&frame->uc;
Al Viro235b8022012-11-09 23:51:47 -0500505 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800506
507 regs->sp = (unsigned long)frame;
508
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -0800509 /*
510 * Set up the CS and SS registers to run signal handlers in
511 * 64-bit mode, even if the handler happens to be interrupting
512 * 32-bit or 16-bit code.
513 *
514 * SS is subtle. In 64-bit mode, we don't need any particular
515 * SS descriptor, but we do need SS to be valid. It's possible
516 * that the old SS is entirely bogus -- this can happen if the
517 * signal we're trying to deliver is #GP or #SS caused by a bad
518 * SS value. We also have a compatbility issue here: DOSEMU
519 * relies on the contents of the SS register indicating the
520 * SS value at the time of the signal, even though that code in
521 * DOSEMU predates sigreturn's ability to restore SS. (DOSEMU
522 * avoids relying on sigreturn to restore SS; instead it uses
523 * a trampoline.) So we do our best: if the old SS was valid,
524 * we keep it. Otherwise we replace it.
525 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800526 regs->cs = __USER_CS;
527
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -0800528 if (unlikely(regs->ss != __USER_DS))
529 force_valid_ss(regs);
530
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800531 return 0;
532}
533#endif /* CONFIG_X86_32 */
534
Al Viro235b8022012-11-09 23:51:47 -0500535static int x32_setup_rt_frame(struct ksignal *ksig,
536 compat_sigset_t *set,
Suresh Siddha050902c2012-07-24 16:05:27 -0700537 struct pt_regs *regs)
538{
539#ifdef CONFIG_X86_X32_ABI
540 struct rt_sigframe_x32 __user *frame;
541 void __user *restorer;
542 int err = 0;
543 void __user *fpstate = NULL;
544
Al Viro235b8022012-11-09 23:51:47 -0500545 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Suresh Siddha050902c2012-07-24 16:05:27 -0700546
547 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
548 return -EFAULT;
549
Al Viro235b8022012-11-09 23:51:47 -0500550 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
Dmitry Safonov68463512016-09-05 16:33:08 +0300551 if (__copy_siginfo_to_user32(&frame->info, &ksig->info, true))
Suresh Siddha050902c2012-07-24 16:05:27 -0700552 return -EFAULT;
553 }
554
555 put_user_try {
556 /* Create the ucontext. */
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800557 put_user_ex(frame_uc_flags(regs), &frame->uc.uc_flags);
Suresh Siddha050902c2012-07-24 16:05:27 -0700558 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100559 compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Suresh Siddha050902c2012-07-24 16:05:27 -0700560 put_user_ex(0, &frame->uc.uc__pad0);
Suresh Siddha050902c2012-07-24 16:05:27 -0700561
Al Viro235b8022012-11-09 23:51:47 -0500562 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
563 restorer = ksig->ka.sa.sa_restorer;
Suresh Siddha050902c2012-07-24 16:05:27 -0700564 } else {
565 /* could use a vstub here */
566 restorer = NULL;
567 err |= -EFAULT;
568 }
569 put_user_ex(restorer, &frame->pretcode);
570 } put_user_catch(err);
571
H. Peter Anvin49b8c692012-09-21 17:18:44 -0700572 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
573 regs, set->sig[0]);
574 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
575
Suresh Siddha050902c2012-07-24 16:05:27 -0700576 if (err)
577 return -EFAULT;
578
579 /* Set up registers for signal handler */
580 regs->sp = (unsigned long) frame;
Al Viro235b8022012-11-09 23:51:47 -0500581 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Suresh Siddha050902c2012-07-24 16:05:27 -0700582
583 /* We use the x32 calling convention here... */
Al Viro235b8022012-11-09 23:51:47 -0500584 regs->di = ksig->sig;
Suresh Siddha050902c2012-07-24 16:05:27 -0700585 regs->si = (unsigned long) &frame->info;
586 regs->dx = (unsigned long) &frame->uc;
587
588 loadsegment(ds, __USER_DS);
589 loadsegment(es, __USER_DS);
590
591 regs->cs = __USER_CS;
592 regs->ss = __USER_DS;
593#endif /* CONFIG_X86_X32_ABI */
594
595 return 0;
596}
597
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800598/*
599 * Do a signal return; undo the signal stack.
600 */
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800601#ifdef CONFIG_X86_32
Andi Kleenff491032013-08-05 15:02:40 -0700602asmlinkage unsigned long sys_sigreturn(void)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800603{
Al Viro3fe26fa2012-11-12 14:32:42 -0500604 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800605 struct sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800606 sigset_t set;
607
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800608 frame = (struct sigframe __user *)(regs->sp - 8);
609
610 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
611 goto badframe;
612 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
613 && __copy_from_user(&set.sig[1], &frame->extramask,
614 sizeof(frame->extramask))))
615 goto badframe;
616
Oleg Nesterov39822942011-07-10 21:27:27 +0200617 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800618
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800619 /*
620 * x86_32 has no uc_flags bits relevant to restore_sigcontext.
621 * Save a few cycles by skipping the __get_user.
622 */
623 if (restore_sigcontext(regs, &frame->sc, 0))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800624 goto badframe;
Brian Gerst6a3713f2015-04-04 08:58:23 -0400625 return regs->ax;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800626
627badframe:
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800628 signal_fault(regs, frame, "sigreturn");
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800629
630 return 0;
631}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800632#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800633
Andi Kleenff491032013-08-05 15:02:40 -0700634asmlinkage long sys_rt_sigreturn(void)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800635{
Al Viro3fe26fa2012-11-12 14:32:42 -0500636 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800637 struct rt_sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800638 sigset_t set;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800639 unsigned long uc_flags;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800640
641 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
642 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
643 goto badframe;
644 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
645 goto badframe;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800646 if (__get_user(uc_flags, &frame->uc.uc_flags))
647 goto badframe;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800648
Oleg Nesterove9bd3f02011-04-27 21:09:39 +0200649 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800650
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800651 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800652 goto badframe;
653
Al Viroc40702c2012-11-20 14:24:26 -0500654 if (restore_altstack(&frame->uc.uc_stack))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800655 goto badframe;
656
Brian Gerst6a3713f2015-04-04 08:58:23 -0400657 return regs->ax;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800658
659badframe:
660 signal_fault(regs, frame, "rt_sigreturn");
661 return 0;
662}
663
Dmitry Safonov68463512016-09-05 16:33:08 +0300664static inline int is_ia32_compat_frame(struct ksignal *ksig)
Ingo Molnar05012c12015-04-30 07:26:04 +0200665{
Masahiro Yamada97f26452016-08-03 13:45:50 -0700666 return IS_ENABLED(CONFIG_IA32_EMULATION) &&
Dmitry Safonov68463512016-09-05 16:33:08 +0300667 ksig->ka.sa.sa_flags & SA_IA32_ABI;
Ingo Molnar05012c12015-04-30 07:26:04 +0200668}
669
Dmitry Safonov68463512016-09-05 16:33:08 +0300670static inline int is_ia32_frame(struct ksignal *ksig)
Ingo Molnar05012c12015-04-30 07:26:04 +0200671{
Dmitry Safonov68463512016-09-05 16:33:08 +0300672 return IS_ENABLED(CONFIG_X86_32) || is_ia32_compat_frame(ksig);
Ingo Molnar05012c12015-04-30 07:26:04 +0200673}
674
Dmitry Safonov68463512016-09-05 16:33:08 +0300675static inline int is_x32_frame(struct ksignal *ksig)
Ingo Molnar05012c12015-04-30 07:26:04 +0200676{
Dmitry Safonov68463512016-09-05 16:33:08 +0300677 return IS_ENABLED(CONFIG_X86_X32_ABI) &&
678 ksig->ka.sa.sa_flags & SA_X32_ABI;
Ingo Molnar05012c12015-04-30 07:26:04 +0200679}
680
Roland McGrath7c1def12005-06-23 00:08:21 -0700681static int
Al Viro235b8022012-11-09 23:51:47 -0500682setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700683{
Richard Weinberger3050a352014-07-13 17:43:51 +0200684 int usig = ksig->sig;
Al Virob7f9a112012-05-02 09:59:21 -0400685 sigset_t *set = sigmask_to_save();
Suresh Siddha050902c2012-07-24 16:05:27 -0700686 compat_sigset_t *cset = (compat_sigset_t *) set;
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700687
688 /* Set up the stack frame */
Dmitry Safonov68463512016-09-05 16:33:08 +0300689 if (is_ia32_frame(ksig)) {
Al Viro235b8022012-11-09 23:51:47 -0500690 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
691 return ia32_setup_rt_frame(usig, ksig, cset, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700692 else
Al Viro235b8022012-11-09 23:51:47 -0500693 return ia32_setup_frame(usig, ksig, cset, regs);
Dmitry Safonov68463512016-09-05 16:33:08 +0300694 } else if (is_x32_frame(ksig)) {
Al Viro235b8022012-11-09 23:51:47 -0500695 return x32_setup_rt_frame(ksig, cset, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800696 } else {
Al Viro235b8022012-11-09 23:51:47 -0500697 return __setup_rt_frame(ksig->sig, ksig, set, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800698 }
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700699}
700
Al Viroa610d6e2012-05-21 23:42:15 -0400701static void
Al Viro235b8022012-11-09 23:51:47 -0500702handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700704 bool stepping, failed;
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200705 struct fpu *fpu = &current->thread.fpu;
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700706
Brian Gerst5ed92a82015-07-29 01:41:19 -0400707 if (v8086_mode(regs))
708 save_v86_state((struct kernel_vm86_regs *) regs, VM86_SIGNAL);
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /* Are we from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700711 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /* If so, check system call restarting.. */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700713 switch (syscall_get_error(current, regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800714 case -ERESTART_RESTARTBLOCK:
715 case -ERESTARTNOHAND:
716 regs->ax = -EINTR;
717 break;
718
719 case -ERESTARTSYS:
Al Viro235b8022012-11-09 23:51:47 -0500720 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100721 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800723 }
724 /* fallthrough */
725 case -ERESTARTNOINTR:
726 regs->ax = regs->orig_ax;
727 regs->ip -= 2;
728 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730 }
731
732 /*
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700733 * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now
734 * so that register information in the sigcontext is correct and
735 * then notify the tracer before entering the signal handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 */
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700737 stepping = test_thread_flag(TIF_SINGLESTEP);
738 if (stepping)
739 user_disable_single_step(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Al Viro235b8022012-11-09 23:51:47 -0500741 failed = (setup_rt_frame(ksig, regs) < 0);
742 if (!failed) {
743 /*
744 * Clear the direction flag as per the ABI for function entry.
Jiri Olsaddd40da2013-05-01 17:25:43 +0200745 *
Jiri Olsa24cda102013-05-01 17:25:42 +0200746 * Clear RF when entering the signal handler, because
747 * it might disable possible debug exception from the
748 * signal handler.
Jiri Olsaddd40da2013-05-01 17:25:43 +0200749 *
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700750 * Clear TF for the case when it wasn't set by debugger to
751 * avoid the recursive send_sigtrap() in SIGTRAP handler.
Al Viro235b8022012-11-09 23:51:47 -0500752 */
Jiri Olsaddd40da2013-05-01 17:25:43 +0200753 regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
Oleg Nesterov66463db2014-09-02 19:57:13 +0200754 /*
755 * Ensure the signal handler starts with the new fpu state.
756 */
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200757 if (fpu->fpstate_active)
Ingo Molnarfbce7782015-04-30 07:12:46 +0200758 fpu__clear(fpu);
Al Viroa610d6e2012-05-21 23:42:15 -0400759 }
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700760 signal_setup_done(failed, ksig, stepping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
Dmitry V. Levin22eab112015-12-01 00:54:36 +0300763static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs)
764{
Andy Lutomirski609c19a2016-07-26 23:12:22 -0700765 /*
766 * This function is fundamentally broken as currently
767 * implemented.
768 *
769 * The idea is that we want to trigger a call to the
770 * restart_block() syscall and that we want in_ia32_syscall(),
771 * in_x32_syscall(), etc. to match whatever they were in the
772 * syscall being restarted. We assume that the syscall
773 * instruction at (regs->ip - 2) matches whatever syscall
774 * instruction we used to enter in the first place.
775 *
776 * The problem is that we can get here when ptrace pokes
777 * syscall-like values into regs even if we're not in a syscall
778 * at all.
779 *
780 * For now, we maintain historical behavior and guess based on
781 * stored state. We could do better by saving the actual
782 * syscall arch in restart_block or (with caveats on x32) by
783 * checking if regs->ip points to 'int $0x80'. The current
784 * behavior is incorrect if a tracer has a different bitness
785 * than the tracee.
786 */
787#ifdef CONFIG_IA32_EMULATION
Andy Lutomirskib9d989c2016-09-13 14:29:21 -0700788 if (current->thread.status & (TS_COMPAT|TS_I386_REGS_POKED))
Dmitry V. Levin95d97ad2015-12-17 23:56:52 +0000789 return __NR_ia32_restart_syscall;
790#endif
791#ifdef CONFIG_X86_X32_ABI
792 return __NR_restart_syscall | (regs->orig_ax & __X32_SYSCALL_BIT);
793#else
Dmitry V. Levin22eab112015-12-01 00:54:36 +0300794 return __NR_restart_syscall;
Dmitry V. Levin95d97ad2015-12-17 23:56:52 +0000795#endif
Dmitry V. Levin22eab112015-12-01 00:54:36 +0300796}
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798/*
799 * Note that 'init' is a special process: it doesn't get signals it doesn't
800 * want to handle. Thus you cannot kill init even with a SIGKILL even by
801 * mistake.
802 */
Andy Lutomirski1f484aa2015-07-03 12:44:23 -0700803void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Al Viro235b8022012-11-09 23:51:47 -0500805 struct ksignal ksig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Al Viro235b8022012-11-09 23:51:47 -0500807 if (get_signal(&ksig)) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100808 /* Whee! Actually deliver the signal. */
Al Viro235b8022012-11-09 23:51:47 -0500809 handle_signal(&ksig, regs);
David Howells283828f2006-01-18 17:44:00 -0800810 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 /* Did we come from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700814 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 /* Restart the system call - no handlers present */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700816 switch (syscall_get_error(current, regs)) {
David Howells283828f2006-01-18 17:44:00 -0800817 case -ERESTARTNOHAND:
818 case -ERESTARTSYS:
819 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100820 regs->ax = regs->orig_ax;
821 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800822 break;
823
824 case -ERESTART_RESTARTBLOCK:
Dmitry V. Levin22eab112015-12-01 00:54:36 +0300825 regs->ax = get_nr_restart_syscall(regs);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100826 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800827 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829 }
David Howells283828f2006-01-18 17:44:00 -0800830
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800831 /*
832 * If there's no signal to deliver, we just put the saved sigmask
833 * back.
834 */
Al Viro51a7b442012-05-21 23:33:55 -0400835 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700838void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
839{
840 struct task_struct *me = current;
841
842 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800843 printk("%s"
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700844 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800845 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700846 me->comm, me->pid, where, frame,
847 regs->ip, regs->sp, regs->orig_ax);
848 print_vma_addr(" in ", regs->ip);
Joe Perchesc767a542012-05-21 19:50:07 -0700849 pr_cont("\n");
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700850 }
851
852 force_sig(SIGSEGV, me);
853}
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800854
855#ifdef CONFIG_X86_X32_ABI
Al Viro3fe26fa2012-11-12 14:32:42 -0500856asmlinkage long sys32_x32_rt_sigreturn(void)
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800857{
Al Viro3fe26fa2012-11-12 14:32:42 -0500858 struct pt_regs *regs = current_pt_regs();
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800859 struct rt_sigframe_x32 __user *frame;
860 sigset_t set;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800861 unsigned long uc_flags;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800862
863 frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
864
865 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
866 goto badframe;
867 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
868 goto badframe;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800869 if (__get_user(uc_flags, &frame->uc.uc_flags))
870 goto badframe;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800871
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800872 set_current_blocked(&set);
873
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800874 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800875 goto badframe;
876
Al Viro90268432012-12-14 14:47:53 -0500877 if (compat_restore_altstack(&frame->uc.uc_stack))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800878 goto badframe;
879
Brian Gerst6a3713f2015-04-04 08:58:23 -0400880 return regs->ax;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800881
882badframe:
883 signal_fault(regs, frame, "x32 rt_sigreturn");
884 return 0;
885}
886#endif