blob: cc30a74e4adb2c3499b52488c49498dea1851ae7 [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 Molnar68db0cf2017-02-08 18:51:37 +010013#include <linux/sched/task_stack.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010014#include <linux/mm.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080015#include <linux/smp.h>
16#include <linux/kernel.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080017#include <linux/errno.h>
18#include <linux/wait.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080019#include <linux/tracehook.h>
20#include <linux/unistd.h>
21#include <linux/stddef.h>
22#include <linux/personality.h>
23#include <linux/uaccess.h>
Avi Kivity7c68af62009-09-19 09:40:22 +030024#include <linux/user-return-notifier.h>
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +053025#include <linux/uprobes.h>
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010026#include <linux/context_tracking.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/processor.h>
29#include <asm/ucontext.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020030#include <asm/fpu/internal.h>
Ingo Molnarfcbc99c2015-04-30 08:45:02 +020031#include <asm/fpu/signal.h>
Roland McGrath6c3652e2008-01-30 13:30:42 +010032#include <asm/vdso.h>
Andi Kleen4efc0672009-04-28 19:07:31 +020033#include <asm/mce.h>
H. Peter Anvinf28f0c22012-02-19 07:38:43 -080034#include <asm/sighandling.h>
Brian Gerstba3e1272015-07-29 01:41:21 -040035#include <asm/vm86.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080036
37#ifdef CONFIG_X86_64
38#include <asm/proto.h>
39#include <asm/ia32_unistd.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080040#endif /* CONFIG_X86_64 */
41
Hiroshi Shimamotobb579252008-09-05 16:26:55 -070042#include <asm/syscall.h>
Jaswinder Singhbbc1f692008-07-21 21:34:13 +053043#include <asm/syscalls.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010044
Hiroshi Shimamoto41af86f2008-12-17 18:50:32 -080045#include <asm/sigframe.h>
Dmitry Safonov68463512016-09-05 16:33:08 +030046#include <asm/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Tejun Heod9a89a22009-02-09 22:17:40 +090048#define COPY(x) do { \
49 get_user_ex(regs->x, &sc->x); \
50} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080051
Tejun Heod9a89a22009-02-09 22:17:40 +090052#define GET_SEG(seg) ({ \
53 unsigned short tmp; \
54 get_user_ex(tmp, &sc->seg); \
55 tmp; \
56})
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080057
Tejun Heod9a89a22009-02-09 22:17:40 +090058#define COPY_SEG(seg) do { \
59 regs->seg = GET_SEG(seg); \
60} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080061
Tejun Heod9a89a22009-02-09 22:17:40 +090062#define COPY_SEG_CPL3(seg) do { \
63 regs->seg = GET_SEG(seg) | 3; \
64} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080065
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -080066#ifdef CONFIG_X86_64
67/*
68 * If regs->ss will cause an IRET fault, change it. Otherwise leave it
69 * alone. Using this generally makes no sense unless
70 * user_64bit_mode(regs) would return true.
71 */
72static void force_valid_ss(struct pt_regs *regs)
73{
74 u32 ar;
75 asm volatile ("lar %[old_ss], %[ar]\n\t"
76 "jz 1f\n\t" /* If invalid: */
77 "xorl %[ar], %[ar]\n\t" /* set ar = 0 */
78 "1:"
79 : [ar] "=r" (ar)
80 : [old_ss] "rm" ((u16)regs->ss));
81
82 /*
83 * For a valid 64-bit user context, we need DPL 3, type
84 * read-write data or read-write exp-down data, and S and P
85 * set. We can't use VERW because VERW doesn't check the
86 * P bit.
87 */
88 ar &= AR_DPL_MASK | AR_S | AR_P | AR_TYPE_MASK;
89 if (ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA) &&
90 ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA_EXPDOWN))
91 regs->ss = __USER_DS;
92}
93#endif
94
Andy Lutomirski6c25da52016-02-16 15:09:03 -080095static int restore_sigcontext(struct pt_regs *regs,
96 struct sigcontext __user *sc,
97 unsigned long uc_flags)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080098{
Ingo Molnar530e5c82015-09-05 09:32:39 +020099 unsigned long buf_val;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800100 void __user *buf;
101 unsigned int tmpflags;
102 unsigned int err = 0;
103
104 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800105 current->restart_block.fn = do_no_restart_syscall;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800106
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800107 get_user_try {
108
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800109#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +0900110 set_user_gs(regs, GET_SEG(gs));
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800111 COPY_SEG(fs);
112 COPY_SEG(es);
113 COPY_SEG(ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800114#endif /* CONFIG_X86_32 */
115
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800116 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
Brian Gerst6a3713f2015-04-04 08:58:23 -0400117 COPY(dx); COPY(cx); COPY(ip); COPY(ax);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800118
119#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800120 COPY(r8);
121 COPY(r9);
122 COPY(r10);
123 COPY(r11);
124 COPY(r12);
125 COPY(r13);
126 COPY(r14);
127 COPY(r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800128#endif /* CONFIG_X86_64 */
129
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800130 COPY_SEG_CPL3(cs);
131 COPY_SEG_CPL3(ss);
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800132
133#ifdef CONFIG_X86_64
134 /*
135 * Fix up SS if needed for the benefit of old DOSEMU and
136 * CRIU.
137 */
138 if (unlikely(!(uc_flags & UC_STRICT_RESTORE_SS) &&
139 user_64bit_mode(regs)))
140 force_valid_ss(regs);
141#endif
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800142
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800143 get_user_ex(tmpflags, &sc->flags);
144 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
145 regs->orig_ax = -1; /* disable syscall checks */
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800146
Ingo Molnar530e5c82015-09-05 09:32:39 +0200147 get_user_ex(buf_val, &sc->fpstate);
148 buf = (void __user *)buf_val;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800149 } get_user_catch(err);
150
Masahiro Yamada97f26452016-08-03 13:45:50 -0700151 err |= fpu__restore_sig(buf, IS_ENABLED(CONFIG_X86_32));
H. Peter Anvin5e883532012-09-21 12:43:15 -0700152
Brian Gerst1daeaa32015-03-21 18:54:21 -0400153 force_iret();
154
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800155 return err;
156}
157
H. Peter Anvin85139422012-02-19 07:43:09 -0800158int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
159 struct pt_regs *regs, unsigned long mask)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800160{
161 int err = 0;
162
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800163 put_user_try {
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800164
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800165#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +0900166 put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800167 put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
168 put_user_ex(regs->es, (unsigned int __user *)&sc->es);
169 put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800170#endif /* CONFIG_X86_32 */
171
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800172 put_user_ex(regs->di, &sc->di);
173 put_user_ex(regs->si, &sc->si);
174 put_user_ex(regs->bp, &sc->bp);
175 put_user_ex(regs->sp, &sc->sp);
176 put_user_ex(regs->bx, &sc->bx);
177 put_user_ex(regs->dx, &sc->dx);
178 put_user_ex(regs->cx, &sc->cx);
179 put_user_ex(regs->ax, &sc->ax);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800180#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800181 put_user_ex(regs->r8, &sc->r8);
182 put_user_ex(regs->r9, &sc->r9);
183 put_user_ex(regs->r10, &sc->r10);
184 put_user_ex(regs->r11, &sc->r11);
185 put_user_ex(regs->r12, &sc->r12);
186 put_user_ex(regs->r13, &sc->r13);
187 put_user_ex(regs->r14, &sc->r14);
188 put_user_ex(regs->r15, &sc->r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800189#endif /* CONFIG_X86_64 */
190
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530191 put_user_ex(current->thread.trap_nr, &sc->trapno);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800192 put_user_ex(current->thread.error_code, &sc->err);
193 put_user_ex(regs->ip, &sc->ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800194#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800195 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
196 put_user_ex(regs->flags, &sc->flags);
197 put_user_ex(regs->sp, &sc->sp_at_signal);
198 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800199#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800200 put_user_ex(regs->flags, &sc->flags);
201 put_user_ex(regs->cs, &sc->cs);
Linus Torvaldsed596cd2015-08-13 08:25:20 -0700202 put_user_ex(0, &sc->gs);
203 put_user_ex(0, &sc->fs);
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800204 put_user_ex(regs->ss, &sc->ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800205#endif /* CONFIG_X86_32 */
206
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800207 put_user_ex(fpstate, &sc->fpstate);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800208
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800209 /* non-iBCS2 extensions.. */
210 put_user_ex(mask, &sc->oldmask);
211 put_user_ex(current->thread.cr2, &sc->cr2);
212 } put_user_catch(err);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800213
214 return err;
215}
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * Set up a signal frame.
219 */
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800220
221/*
222 * Determine which stack to use..
223 */
Hiroshi Shimamoto1fae0272009-02-27 10:30:32 -0800224static unsigned long align_sigframe(unsigned long sp)
225{
226#ifdef CONFIG_X86_32
227 /*
228 * Align the stack pointer according to the i386 ABI,
229 * i.e. so that on function entry ((sp + 4) & 15) == 0.
230 */
231 sp = ((sp + 4) & -16ul) - 4;
232#else /* !CONFIG_X86_32 */
233 sp = round_down(sp, 16) - 8;
234#endif
235 return sp;
236}
237
Denys Vlasenkodae0f302015-09-28 14:23:57 +0200238static void __user *
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800239get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
240 void __user **fpstate)
241{
242 /* Default to using normal stack */
Suresh Siddha72a671c2012-07-24 16:05:29 -0700243 unsigned long math_size = 0;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800244 unsigned long sp = regs->sp;
Suresh Siddha72a671c2012-07-24 16:05:29 -0700245 unsigned long buf_fx = 0;
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700246 int onsigstack = on_sig_stack(sp);
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200247 struct fpu *fpu = &current->thread.fpu;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800248
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800249 /* redzone */
Masahiro Yamada97f26452016-08-03 13:45:50 -0700250 if (IS_ENABLED(CONFIG_X86_64))
Suresh Siddha050902c2012-07-24 16:05:27 -0700251 sp -= 128;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800252
Stas Sergeev0b4521e2016-04-14 23:20:02 +0300253 /* This is the X/Open sanctioned signal stack switching. */
254 if (ka->sa.sa_flags & SA_ONSTACK) {
255 if (sas_ss_flags(sp) == 0)
256 sp = current->sas_ss_sp + current->sas_ss_size;
Masahiro Yamada97f26452016-08-03 13:45:50 -0700257 } else if (IS_ENABLED(CONFIG_X86_32) &&
Stas Sergeev0b4521e2016-04-14 23:20:02 +0300258 !onsigstack &&
259 (regs->ss & 0xffff) != __USER_DS &&
260 !(ka->sa.sa_flags & SA_RESTORER) &&
261 ka->sa.sa_restorer) {
262 /* This is the legacy signal stack switching. */
263 sp = (unsigned long) ka->sa.sa_restorer;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800264 }
265
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200266 if (fpu->fpstate_active) {
Masahiro Yamada97f26452016-08-03 13:45:50 -0700267 sp = fpu__alloc_mathframe(sp, IS_ENABLED(CONFIG_X86_32),
Ingo Molnar82c0e452015-04-29 21:09:18 +0200268 &buf_fx, &math_size);
Hiroshi Shimamoto25051702009-03-02 17:20:01 -0800269 *fpstate = (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800270 }
271
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700272 sp = align_sigframe(sp - frame_size);
273
274 /*
275 * If we are on the alternate signal stack and would overflow it, don't.
276 * Return an always-bogus address instead so we will die with SIGSEGV.
277 */
278 if (onsigstack && !likely(on_sig_stack(sp)))
279 return (void __user *)-1L;
280
Suresh Siddha72a671c2012-07-24 16:05:29 -0700281 /* save i387 and extended state */
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200282 if (fpu->fpstate_active &&
Ingo Molnarc8e140412015-04-28 11:35:20 +0200283 copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size) < 0)
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700284 return (void __user *)-1L;
285
286 return (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800287}
288
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800289#ifdef CONFIG_X86_32
290static const struct {
291 u16 poplmovl;
292 u32 val;
293 u16 int80;
294} __attribute__((packed)) retcode = {
295 0xb858, /* popl %eax; movl $..., %eax */
296 __NR_sigreturn,
297 0x80cd, /* int $0x80 */
298};
299
300static const struct {
301 u8 movl;
302 u32 val;
303 u16 int80;
304 u8 pad;
305} __attribute__((packed)) rt_retcode = {
306 0xb8, /* movl $..., %eax */
307 __NR_rt_sigreturn,
308 0x80cd, /* int $0x80 */
309 0
310};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Ingo Molnar7e907f42008-03-06 10:33:08 +0100312static int
Al Viro235b8022012-11-09 23:51:47 -0500313__setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700314 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100317 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700319 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Al Viro235b8022012-11-09 23:51:47 -0500321 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700324 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700326 if (__put_user(sig, &frame->sig))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700327 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700329 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700330 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 if (_NSIG_WORDS > 1) {
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700333 if (__copy_to_user(&frame->extramask, &set->sig[1],
334 sizeof(frame->extramask)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700335 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
337
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700338 if (current->mm->context.vdso)
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700339 restorer = current->mm->context.vdso +
Andy Lutomirski0a6d1fa2015-10-05 17:47:56 -0700340 vdso_image_32.sym___kernel_sigreturn;
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100341 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100342 restorer = &frame->retcode;
Al Viro235b8022012-11-09 23:51:47 -0500343 if (ksig->ka.sa.sa_flags & SA_RESTORER)
344 restorer = ksig->ka.sa.sa_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 /* Set up to return from userspace. */
347 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100350 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 *
352 * WE DO NOT USE IT ANY MORE! It's only left here for historical
353 * reasons and because gdb uses it as a signature to notice
354 * signal handler stack frames.
355 */
Hiroshi Shimamoto4a612042008-11-11 19:09:29 -0800356 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700359 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100362 regs->sp = (unsigned long)frame;
Al Viro235b8022012-11-09 23:51:47 -0500363 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100364 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800365 regs->dx = 0;
366 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100368 regs->ds = __USER_DS;
369 regs->es = __USER_DS;
370 regs->ss = __USER_DS;
371 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
David Howells283828f2006-01-18 17:44:00 -0800373 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
Al Viro235b8022012-11-09 23:51:47 -0500376static int __setup_rt_frame(int sig, struct ksignal *ksig,
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700377 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100380 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700382 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Al Viro235b8022012-11-09 23:51:47 -0500384 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700387 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800389 put_user_try {
390 put_user_ex(sig, &frame->sig);
391 put_user_ex(&frame->info, &frame->pinfo);
392 put_user_ex(&frame->uc, &frame->puc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800394 /* Create the ucontext. */
Borislav Petkovd366bf72016-04-04 22:25:02 +0200395 if (boot_cpu_has(X86_FEATURE_XSAVE))
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800396 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
397 else
398 put_user_ex(0, &frame->uc.uc_flags);
399 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100400 save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800402 /* Set up to return from userspace. */
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700403 restorer = current->mm->context.vdso +
Andy Lutomirski0a6d1fa2015-10-05 17:47:56 -0700404 vdso_image_32.sym___kernel_rt_sigreturn;
Al Viro235b8022012-11-09 23:51:47 -0500405 if (ksig->ka.sa.sa_flags & SA_RESTORER)
406 restorer = ksig->ka.sa.sa_restorer;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800407 put_user_ex(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100408
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800409 /*
410 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
411 *
412 * WE DO NOT USE IT ANY MORE! It's only left here for historical
413 * reasons and because gdb uses it as a signature to notice
414 * signal handler stack frames.
415 */
416 put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
417 } put_user_catch(err);
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700418
Al Viro235b8022012-11-09 23:51:47 -0500419 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
H. Peter Anvin5e883532012-09-21 12:43:15 -0700420 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
421 regs, set->sig[0]);
422 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700425 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100428 regs->sp = (unsigned long)frame;
Al Viro235b8022012-11-09 23:51:47 -0500429 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700430 regs->ax = (unsigned long)sig;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100431 regs->dx = (unsigned long)&frame->info;
432 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100434 regs->ds = __USER_DS;
435 regs->es = __USER_DS;
436 regs->ss = __USER_DS;
437 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
David Howells283828f2006-01-18 17:44:00 -0800439 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800441#else /* !CONFIG_X86_32 */
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800442static unsigned long frame_uc_flags(struct pt_regs *regs)
443{
444 unsigned long flags;
445
Borislav Petkovd366bf72016-04-04 22:25:02 +0200446 if (boot_cpu_has(X86_FEATURE_XSAVE))
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800447 flags = UC_FP_XSTATE | UC_SIGCONTEXT_SS;
448 else
449 flags = UC_SIGCONTEXT_SS;
450
451 if (likely(user_64bit_mode(regs)))
452 flags |= UC_STRICT_RESTORE_SS;
453
454 return flags;
455}
456
Al Viro235b8022012-11-09 23:51:47 -0500457static int __setup_rt_frame(int sig, struct ksignal *ksig,
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800458 sigset_t *set, struct pt_regs *regs)
459{
460 struct rt_sigframe __user *frame;
461 void __user *fp = NULL;
462 int err = 0;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800463
Al Viro235b8022012-11-09 23:51:47 -0500464 frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800465
466 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
467 return -EFAULT;
468
Al Viro235b8022012-11-09 23:51:47 -0500469 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
470 if (copy_siginfo_to_user(&frame->info, &ksig->info))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800471 return -EFAULT;
472 }
473
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800474 put_user_try {
475 /* Create the ucontext. */
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800476 put_user_ex(frame_uc_flags(regs), &frame->uc.uc_flags);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800477 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100478 save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800479
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800480 /* Set up to return from userspace. If provided, use a stub
481 already in userspace. */
482 /* x86-64 should always use SA_RESTORER. */
Al Viro235b8022012-11-09 23:51:47 -0500483 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
484 put_user_ex(ksig->ka.sa.sa_restorer, &frame->pretcode);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800485 } else {
486 /* could use a vstub here */
487 err |= -EFAULT;
488 }
489 } put_user_catch(err);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800490
H. Peter Anvin5e883532012-09-21 12:43:15 -0700491 err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
492 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
493
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800494 if (err)
495 return -EFAULT;
496
497 /* Set up registers for signal handler */
498 regs->di = sig;
499 /* In case the signal handler was declared without prototypes */
500 regs->ax = 0;
501
502 /* This also works for non SA_SIGINFO handlers because they expect the
503 next argument after the signal number on the stack. */
504 regs->si = (unsigned long)&frame->info;
505 regs->dx = (unsigned long)&frame->uc;
Al Viro235b8022012-11-09 23:51:47 -0500506 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800507
508 regs->sp = (unsigned long)frame;
509
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -0800510 /*
511 * Set up the CS and SS registers to run signal handlers in
512 * 64-bit mode, even if the handler happens to be interrupting
513 * 32-bit or 16-bit code.
514 *
515 * SS is subtle. In 64-bit mode, we don't need any particular
516 * SS descriptor, but we do need SS to be valid. It's possible
517 * that the old SS is entirely bogus -- this can happen if the
518 * signal we're trying to deliver is #GP or #SS caused by a bad
519 * SS value. We also have a compatbility issue here: DOSEMU
520 * relies on the contents of the SS register indicating the
521 * SS value at the time of the signal, even though that code in
522 * DOSEMU predates sigreturn's ability to restore SS. (DOSEMU
523 * avoids relying on sigreturn to restore SS; instead it uses
524 * a trampoline.) So we do our best: if the old SS was valid,
525 * we keep it. Otherwise we replace it.
526 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800527 regs->cs = __USER_CS;
528
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -0800529 if (unlikely(regs->ss != __USER_DS))
530 force_valid_ss(regs);
531
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800532 return 0;
533}
534#endif /* CONFIG_X86_32 */
535
Al Viro235b8022012-11-09 23:51:47 -0500536static int x32_setup_rt_frame(struct ksignal *ksig,
537 compat_sigset_t *set,
Suresh Siddha050902c2012-07-24 16:05:27 -0700538 struct pt_regs *regs)
539{
540#ifdef CONFIG_X86_X32_ABI
541 struct rt_sigframe_x32 __user *frame;
542 void __user *restorer;
543 int err = 0;
544 void __user *fpstate = NULL;
545
Al Viro235b8022012-11-09 23:51:47 -0500546 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Suresh Siddha050902c2012-07-24 16:05:27 -0700547
548 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
549 return -EFAULT;
550
Al Viro235b8022012-11-09 23:51:47 -0500551 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
Dmitry Safonov68463512016-09-05 16:33:08 +0300552 if (__copy_siginfo_to_user32(&frame->info, &ksig->info, true))
Suresh Siddha050902c2012-07-24 16:05:27 -0700553 return -EFAULT;
554 }
555
556 put_user_try {
557 /* Create the ucontext. */
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800558 put_user_ex(frame_uc_flags(regs), &frame->uc.uc_flags);
Suresh Siddha050902c2012-07-24 16:05:27 -0700559 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100560 compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Suresh Siddha050902c2012-07-24 16:05:27 -0700561 put_user_ex(0, &frame->uc.uc__pad0);
Suresh Siddha050902c2012-07-24 16:05:27 -0700562
Al Viro235b8022012-11-09 23:51:47 -0500563 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
564 restorer = ksig->ka.sa.sa_restorer;
Suresh Siddha050902c2012-07-24 16:05:27 -0700565 } else {
566 /* could use a vstub here */
567 restorer = NULL;
568 err |= -EFAULT;
569 }
570 put_user_ex(restorer, &frame->pretcode);
571 } put_user_catch(err);
572
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700573 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
574 regs, set->sig[0]);
575 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
576
Suresh Siddha050902c2012-07-24 16:05:27 -0700577 if (err)
578 return -EFAULT;
579
580 /* Set up registers for signal handler */
581 regs->sp = (unsigned long) frame;
Al Viro235b8022012-11-09 23:51:47 -0500582 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Suresh Siddha050902c2012-07-24 16:05:27 -0700583
584 /* We use the x32 calling convention here... */
Al Viro235b8022012-11-09 23:51:47 -0500585 regs->di = ksig->sig;
Suresh Siddha050902c2012-07-24 16:05:27 -0700586 regs->si = (unsigned long) &frame->info;
587 regs->dx = (unsigned long) &frame->uc;
588
589 loadsegment(ds, __USER_DS);
590 loadsegment(es, __USER_DS);
591
592 regs->cs = __USER_CS;
593 regs->ss = __USER_DS;
594#endif /* CONFIG_X86_X32_ABI */
595
596 return 0;
597}
598
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800599/*
600 * Do a signal return; undo the signal stack.
601 */
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800602#ifdef CONFIG_X86_32
Andi Kleenff491032013-08-05 15:02:40 -0700603asmlinkage unsigned long sys_sigreturn(void)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800604{
Al Viro3fe26fa2012-11-12 14:32:42 -0500605 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800606 struct sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800607 sigset_t set;
608
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800609 frame = (struct sigframe __user *)(regs->sp - 8);
610
611 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
612 goto badframe;
613 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
614 && __copy_from_user(&set.sig[1], &frame->extramask,
615 sizeof(frame->extramask))))
616 goto badframe;
617
Oleg Nesterov39822942011-07-10 21:27:27 +0200618 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800619
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800620 /*
621 * x86_32 has no uc_flags bits relevant to restore_sigcontext.
622 * Save a few cycles by skipping the __get_user.
623 */
624 if (restore_sigcontext(regs, &frame->sc, 0))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800625 goto badframe;
Brian Gerst6a3713f2015-04-04 08:58:23 -0400626 return regs->ax;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800627
628badframe:
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800629 signal_fault(regs, frame, "sigreturn");
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800630
631 return 0;
632}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800633#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800634
Andi Kleenff491032013-08-05 15:02:40 -0700635asmlinkage long sys_rt_sigreturn(void)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800636{
Al Viro3fe26fa2012-11-12 14:32:42 -0500637 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800638 struct rt_sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800639 sigset_t set;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800640 unsigned long uc_flags;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800641
642 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
643 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
644 goto badframe;
645 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
646 goto badframe;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800647 if (__get_user(uc_flags, &frame->uc.uc_flags))
648 goto badframe;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800649
Oleg Nesterove9bd3f02011-04-27 21:09:39 +0200650 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800651
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800652 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800653 goto badframe;
654
Al Viroc40702c2012-11-20 14:24:26 -0500655 if (restore_altstack(&frame->uc.uc_stack))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800656 goto badframe;
657
Brian Gerst6a3713f2015-04-04 08:58:23 -0400658 return regs->ax;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800659
660badframe:
661 signal_fault(regs, frame, "rt_sigreturn");
662 return 0;
663}
664
Dmitry Safonov68463512016-09-05 16:33:08 +0300665static inline int is_ia32_compat_frame(struct ksignal *ksig)
Ingo Molnar05012c12015-04-30 07:26:04 +0200666{
Masahiro Yamada97f26452016-08-03 13:45:50 -0700667 return IS_ENABLED(CONFIG_IA32_EMULATION) &&
Dmitry Safonov68463512016-09-05 16:33:08 +0300668 ksig->ka.sa.sa_flags & SA_IA32_ABI;
Ingo Molnar05012c12015-04-30 07:26:04 +0200669}
670
Dmitry Safonov68463512016-09-05 16:33:08 +0300671static inline int is_ia32_frame(struct ksignal *ksig)
Ingo Molnar05012c12015-04-30 07:26:04 +0200672{
Dmitry Safonov68463512016-09-05 16:33:08 +0300673 return IS_ENABLED(CONFIG_X86_32) || is_ia32_compat_frame(ksig);
Ingo Molnar05012c12015-04-30 07:26:04 +0200674}
675
Dmitry Safonov68463512016-09-05 16:33:08 +0300676static inline int is_x32_frame(struct ksignal *ksig)
Ingo Molnar05012c12015-04-30 07:26:04 +0200677{
Dmitry Safonov68463512016-09-05 16:33:08 +0300678 return IS_ENABLED(CONFIG_X86_X32_ABI) &&
679 ksig->ka.sa.sa_flags & SA_X32_ABI;
Ingo Molnar05012c12015-04-30 07:26:04 +0200680}
681
Roland McGrath7c1def12005-06-23 00:08:21 -0700682static int
Al Viro235b8022012-11-09 23:51:47 -0500683setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700684{
Richard Weinberger3050a352014-07-13 17:43:51 +0200685 int usig = ksig->sig;
Al Virob7f9a112012-05-02 09:59:21 -0400686 sigset_t *set = sigmask_to_save();
Suresh Siddha050902c2012-07-24 16:05:27 -0700687 compat_sigset_t *cset = (compat_sigset_t *) set;
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700688
689 /* Set up the stack frame */
Dmitry Safonov68463512016-09-05 16:33:08 +0300690 if (is_ia32_frame(ksig)) {
Al Viro235b8022012-11-09 23:51:47 -0500691 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
692 return ia32_setup_rt_frame(usig, ksig, cset, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700693 else
Al Viro235b8022012-11-09 23:51:47 -0500694 return ia32_setup_frame(usig, ksig, cset, regs);
Dmitry Safonov68463512016-09-05 16:33:08 +0300695 } else if (is_x32_frame(ksig)) {
Al Viro235b8022012-11-09 23:51:47 -0500696 return x32_setup_rt_frame(ksig, cset, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800697 } else {
Al Viro235b8022012-11-09 23:51:47 -0500698 return __setup_rt_frame(ksig->sig, ksig, set, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800699 }
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700700}
701
Al Viroa610d6e2012-05-21 23:42:15 -0400702static void
Al Viro235b8022012-11-09 23:51:47 -0500703handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700705 bool stepping, failed;
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200706 struct fpu *fpu = &current->thread.fpu;
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700707
Brian Gerst5ed92a82015-07-29 01:41:19 -0400708 if (v8086_mode(regs))
709 save_v86_state((struct kernel_vm86_regs *) regs, VM86_SIGNAL);
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 /* Are we from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700712 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 /* If so, check system call restarting.. */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700714 switch (syscall_get_error(current, regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800715 case -ERESTART_RESTARTBLOCK:
716 case -ERESTARTNOHAND:
717 regs->ax = -EINTR;
718 break;
719
720 case -ERESTARTSYS:
Al Viro235b8022012-11-09 23:51:47 -0500721 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100722 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800724 }
725 /* fallthrough */
726 case -ERESTARTNOINTR:
727 regs->ax = regs->orig_ax;
728 regs->ip -= 2;
729 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731 }
732
733 /*
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700734 * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now
735 * so that register information in the sigcontext is correct and
736 * then notify the tracer before entering the signal handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 */
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700738 stepping = test_thread_flag(TIF_SINGLESTEP);
739 if (stepping)
740 user_disable_single_step(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Al Viro235b8022012-11-09 23:51:47 -0500742 failed = (setup_rt_frame(ksig, regs) < 0);
743 if (!failed) {
744 /*
745 * Clear the direction flag as per the ABI for function entry.
Jiri Olsaddd40da2013-05-01 17:25:43 +0200746 *
Jiri Olsa24cda102013-05-01 17:25:42 +0200747 * Clear RF when entering the signal handler, because
748 * it might disable possible debug exception from the
749 * signal handler.
Jiri Olsaddd40da2013-05-01 17:25:43 +0200750 *
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700751 * Clear TF for the case when it wasn't set by debugger to
752 * avoid the recursive send_sigtrap() in SIGTRAP handler.
Al Viro235b8022012-11-09 23:51:47 -0500753 */
Jiri Olsaddd40da2013-05-01 17:25:43 +0200754 regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
Oleg Nesterov66463db2014-09-02 19:57:13 +0200755 /*
756 * Ensure the signal handler starts with the new fpu state.
757 */
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200758 if (fpu->fpstate_active)
Ingo Molnarfbce7782015-04-30 07:12:46 +0200759 fpu__clear(fpu);
Al Viroa610d6e2012-05-21 23:42:15 -0400760 }
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700761 signal_setup_done(failed, ksig, stepping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300764static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs)
765{
Andy Lutomirski609c19a2016-07-26 23:12:22 -0700766 /*
767 * This function is fundamentally broken as currently
768 * implemented.
769 *
770 * The idea is that we want to trigger a call to the
771 * restart_block() syscall and that we want in_ia32_syscall(),
772 * in_x32_syscall(), etc. to match whatever they were in the
773 * syscall being restarted. We assume that the syscall
774 * instruction at (regs->ip - 2) matches whatever syscall
775 * instruction we used to enter in the first place.
776 *
777 * The problem is that we can get here when ptrace pokes
778 * syscall-like values into regs even if we're not in a syscall
779 * at all.
780 *
781 * For now, we maintain historical behavior and guess based on
782 * stored state. We could do better by saving the actual
783 * syscall arch in restart_block or (with caveats on x32) by
784 * checking if regs->ip points to 'int $0x80'. The current
785 * behavior is incorrect if a tracer has a different bitness
786 * than the tracee.
787 */
788#ifdef CONFIG_IA32_EMULATION
Andy Lutomirskib9d989c2016-09-13 14:29:21 -0700789 if (current->thread.status & (TS_COMPAT|TS_I386_REGS_POKED))
Dmitry V. Levin95d97ad2015-12-17 23:56:52 +0000790 return __NR_ia32_restart_syscall;
791#endif
792#ifdef CONFIG_X86_X32_ABI
793 return __NR_restart_syscall | (regs->orig_ax & __X32_SYSCALL_BIT);
794#else
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300795 return __NR_restart_syscall;
Dmitry V. Levin95d97ad2015-12-17 23:56:52 +0000796#endif
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300797}
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799/*
800 * Note that 'init' is a special process: it doesn't get signals it doesn't
801 * want to handle. Thus you cannot kill init even with a SIGKILL even by
802 * mistake.
803 */
Andy Lutomirski1f484aa2015-07-03 12:44:23 -0700804void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Al Viro235b8022012-11-09 23:51:47 -0500806 struct ksignal ksig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Al Viro235b8022012-11-09 23:51:47 -0500808 if (get_signal(&ksig)) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100809 /* Whee! Actually deliver the signal. */
Al Viro235b8022012-11-09 23:51:47 -0500810 handle_signal(&ksig, regs);
David Howells283828f2006-01-18 17:44:00 -0800811 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 /* Did we come from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700815 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /* Restart the system call - no handlers present */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700817 switch (syscall_get_error(current, regs)) {
David Howells283828f2006-01-18 17:44:00 -0800818 case -ERESTARTNOHAND:
819 case -ERESTARTSYS:
820 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100821 regs->ax = regs->orig_ax;
822 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800823 break;
824
825 case -ERESTART_RESTARTBLOCK:
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300826 regs->ax = get_nr_restart_syscall(regs);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100827 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800828 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830 }
David Howells283828f2006-01-18 17:44:00 -0800831
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800832 /*
833 * If there's no signal to deliver, we just put the saved sigmask
834 * back.
835 */
Al Viro51a7b442012-05-21 23:33:55 -0400836 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700839void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
840{
841 struct task_struct *me = current;
842
843 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800844 printk("%s"
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700845 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800846 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700847 me->comm, me->pid, where, frame,
848 regs->ip, regs->sp, regs->orig_ax);
Markus Trippelsdorf1c99a682017-04-07 14:09:04 +0200849 print_vma_addr(KERN_CONT " in ", regs->ip);
Joe Perchesc767a542012-05-21 19:50:07 -0700850 pr_cont("\n");
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700851 }
852
853 force_sig(SIGSEGV, me);
854}
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800855
856#ifdef CONFIG_X86_X32_ABI
Al Viro3fe26fa2012-11-12 14:32:42 -0500857asmlinkage long sys32_x32_rt_sigreturn(void)
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800858{
Al Viro3fe26fa2012-11-12 14:32:42 -0500859 struct pt_regs *regs = current_pt_regs();
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800860 struct rt_sigframe_x32 __user *frame;
861 sigset_t set;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800862 unsigned long uc_flags;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800863
864 frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
865
866 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
867 goto badframe;
868 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
869 goto badframe;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800870 if (__get_user(uc_flags, &frame->uc.uc_flags))
871 goto badframe;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800872
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800873 set_current_blocked(&set);
874
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800875 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800876 goto badframe;
877
Al Viro90268432012-12-14 14:47:53 -0500878 if (compat_restore_altstack(&frame->uc.uc_stack))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800879 goto badframe;
880
Brian Gerst6a3713f2015-04-04 08:58:23 -0400881 return regs->ax;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800882
883badframe:
884 signal_fault(regs, frame, "x32 rt_sigreturn");
885 return 0;
886}
887#endif