blob: 92a3b312a53c465bbde5f006b5707b62671a49ae [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) 1991, 1992 Linus Torvalds
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -08004 * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
7 * 2000-06-20 Pentium III FXSR, SSE support by Gareth Hughes
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -08008 * 2000-2002 x86-64 support by Andi Kleen
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
Joe Perchesc767a542012-05-21 19:50:07 -070010
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Ingo Molnar7e907f42008-03-06 10:33:08 +010013#include <linux/sched.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010014#include <linux/sched/task_stack.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010015#include <linux/mm.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080016#include <linux/smp.h>
17#include <linux/kernel.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080018#include <linux/errno.h>
19#include <linux/wait.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080020#include <linux/tracehook.h>
21#include <linux/unistd.h>
22#include <linux/stddef.h>
23#include <linux/personality.h>
24#include <linux/uaccess.h>
Avi Kivity7c68af62009-09-19 09:40:22 +030025#include <linux/user-return-notifier.h>
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +053026#include <linux/uprobes.h>
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010027#include <linux/context_tracking.h>
Tautschnig, Michael4c8ca512018-03-14 09:41:42 +000028#include <linux/syscalls.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/processor.h>
31#include <asm/ucontext.h>
Ingo Molnar78f7f1e2015-04-24 02:54:44 +020032#include <asm/fpu/internal.h>
Ingo Molnarfcbc99c2015-04-30 08:45:02 +020033#include <asm/fpu/signal.h>
Roland McGrath6c3652e2008-01-30 13:30:42 +010034#include <asm/vdso.h>
Andi Kleen4efc0672009-04-28 19:07:31 +020035#include <asm/mce.h>
H. Peter Anvinf28f0c22012-02-19 07:38:43 -080036#include <asm/sighandling.h>
Brian Gerstba3e1272015-07-29 01:41:21 -040037#include <asm/vm86.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080038
39#ifdef CONFIG_X86_64
40#include <asm/proto.h>
41#include <asm/ia32_unistd.h>
Hiroshi Shimamoto5c9b3a02008-11-21 17:36:41 -080042#endif /* CONFIG_X86_64 */
43
Hiroshi Shimamotobb579252008-09-05 16:26:55 -070044#include <asm/syscall.h>
Jaswinder Singhbbc1f692008-07-21 21:34:13 +053045#include <asm/syscalls.h>
Ingo Molnar7e907f42008-03-06 10:33:08 +010046
Hiroshi Shimamoto41af86f2008-12-17 18:50:32 -080047#include <asm/sigframe.h>
Dmitry Safonov68463512016-09-05 16:33:08 +030048#include <asm/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Tejun Heod9a89a22009-02-09 22:17:40 +090050#define COPY(x) do { \
51 get_user_ex(regs->x, &sc->x); \
52} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080053
Tejun Heod9a89a22009-02-09 22:17:40 +090054#define GET_SEG(seg) ({ \
55 unsigned short tmp; \
56 get_user_ex(tmp, &sc->seg); \
57 tmp; \
58})
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080059
Tejun Heod9a89a22009-02-09 22:17:40 +090060#define COPY_SEG(seg) do { \
61 regs->seg = GET_SEG(seg); \
62} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080063
Tejun Heod9a89a22009-02-09 22:17:40 +090064#define COPY_SEG_CPL3(seg) do { \
65 regs->seg = GET_SEG(seg) | 3; \
66} while (0)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -080067
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -080068#ifdef CONFIG_X86_64
69/*
70 * If regs->ss will cause an IRET fault, change it. Otherwise leave it
71 * alone. Using this generally makes no sense unless
72 * user_64bit_mode(regs) would return true.
73 */
74static void force_valid_ss(struct pt_regs *regs)
75{
76 u32 ar;
77 asm volatile ("lar %[old_ss], %[ar]\n\t"
78 "jz 1f\n\t" /* If invalid: */
79 "xorl %[ar], %[ar]\n\t" /* set ar = 0 */
80 "1:"
81 : [ar] "=r" (ar)
82 : [old_ss] "rm" ((u16)regs->ss));
83
84 /*
85 * For a valid 64-bit user context, we need DPL 3, type
86 * read-write data or read-write exp-down data, and S and P
87 * set. We can't use VERW because VERW doesn't check the
88 * P bit.
89 */
90 ar &= AR_DPL_MASK | AR_S | AR_P | AR_TYPE_MASK;
91 if (ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA) &&
92 ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA_EXPDOWN))
93 regs->ss = __USER_DS;
94}
95#endif
96
Andy Lutomirski6c25da52016-02-16 15:09:03 -080097static int restore_sigcontext(struct pt_regs *regs,
98 struct sigcontext __user *sc,
99 unsigned long uc_flags)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800100{
Ingo Molnar530e5c82015-09-05 09:32:39 +0200101 unsigned long buf_val;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800102 void __user *buf;
103 unsigned int tmpflags;
104 unsigned int err = 0;
105
106 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800107 current->restart_block.fn = do_no_restart_syscall;
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800108
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800109 get_user_try {
110
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800111#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +0900112 set_user_gs(regs, GET_SEG(gs));
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800113 COPY_SEG(fs);
114 COPY_SEG(es);
115 COPY_SEG(ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800116#endif /* CONFIG_X86_32 */
117
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800118 COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
Brian Gerst6a3713f2015-04-04 08:58:23 -0400119 COPY(dx); COPY(cx); COPY(ip); COPY(ax);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800120
121#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800122 COPY(r8);
123 COPY(r9);
124 COPY(r10);
125 COPY(r11);
126 COPY(r12);
127 COPY(r13);
128 COPY(r14);
129 COPY(r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800130#endif /* CONFIG_X86_64 */
131
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800132 COPY_SEG_CPL3(cs);
133 COPY_SEG_CPL3(ss);
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800134
135#ifdef CONFIG_X86_64
136 /*
137 * Fix up SS if needed for the benefit of old DOSEMU and
138 * CRIU.
139 */
140 if (unlikely(!(uc_flags & UC_STRICT_RESTORE_SS) &&
141 user_64bit_mode(regs)))
142 force_valid_ss(regs);
143#endif
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800144
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800145 get_user_ex(tmpflags, &sc->flags);
146 regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
147 regs->orig_ax = -1; /* disable syscall checks */
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800148
Ingo Molnar530e5c82015-09-05 09:32:39 +0200149 get_user_ex(buf_val, &sc->fpstate);
150 buf = (void __user *)buf_val;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800151 } get_user_catch(err);
152
Masahiro Yamada97f26452016-08-03 13:45:50 -0700153 err |= fpu__restore_sig(buf, IS_ENABLED(CONFIG_X86_32));
H. Peter Anvin5e883532012-09-21 12:43:15 -0700154
Brian Gerst1daeaa32015-03-21 18:54:21 -0400155 force_iret();
156
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800157 return err;
158}
159
H. Peter Anvin85139422012-02-19 07:43:09 -0800160int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
161 struct pt_regs *regs, unsigned long mask)
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800162{
163 int err = 0;
164
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800165 put_user_try {
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800166
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800167#ifdef CONFIG_X86_32
Tejun Heod9a89a22009-02-09 22:17:40 +0900168 put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800169 put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
170 put_user_ex(regs->es, (unsigned int __user *)&sc->es);
171 put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800172#endif /* CONFIG_X86_32 */
173
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800174 put_user_ex(regs->di, &sc->di);
175 put_user_ex(regs->si, &sc->si);
176 put_user_ex(regs->bp, &sc->bp);
177 put_user_ex(regs->sp, &sc->sp);
178 put_user_ex(regs->bx, &sc->bx);
179 put_user_ex(regs->dx, &sc->dx);
180 put_user_ex(regs->cx, &sc->cx);
181 put_user_ex(regs->ax, &sc->ax);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800182#ifdef CONFIG_X86_64
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800183 put_user_ex(regs->r8, &sc->r8);
184 put_user_ex(regs->r9, &sc->r9);
185 put_user_ex(regs->r10, &sc->r10);
186 put_user_ex(regs->r11, &sc->r11);
187 put_user_ex(regs->r12, &sc->r12);
188 put_user_ex(regs->r13, &sc->r13);
189 put_user_ex(regs->r14, &sc->r14);
190 put_user_ex(regs->r15, &sc->r15);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800191#endif /* CONFIG_X86_64 */
192
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530193 put_user_ex(current->thread.trap_nr, &sc->trapno);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800194 put_user_ex(current->thread.error_code, &sc->err);
195 put_user_ex(regs->ip, &sc->ip);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800196#ifdef CONFIG_X86_32
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800197 put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
198 put_user_ex(regs->flags, &sc->flags);
199 put_user_ex(regs->sp, &sc->sp_at_signal);
200 put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800201#else /* !CONFIG_X86_32 */
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800202 put_user_ex(regs->flags, &sc->flags);
203 put_user_ex(regs->cs, &sc->cs);
Linus Torvaldsed596cd2015-08-13 08:25:20 -0700204 put_user_ex(0, &sc->gs);
205 put_user_ex(0, &sc->fs);
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800206 put_user_ex(regs->ss, &sc->ss);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800207#endif /* CONFIG_X86_32 */
208
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800209 put_user_ex(fpstate, &sc->fpstate);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800210
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800211 /* non-iBCS2 extensions.. */
212 put_user_ex(mask, &sc->oldmask);
213 put_user_ex(current->thread.cr2, &sc->cr2);
214 } put_user_catch(err);
Hiroshi Shimamoto26016572008-11-24 18:21:37 -0800215
216 return err;
217}
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 * Set up a signal frame.
221 */
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800222
223/*
224 * Determine which stack to use..
225 */
Hiroshi Shimamoto1fae0272009-02-27 10:30:32 -0800226static unsigned long align_sigframe(unsigned long sp)
227{
228#ifdef CONFIG_X86_32
229 /*
230 * Align the stack pointer according to the i386 ABI,
231 * i.e. so that on function entry ((sp + 4) & 15) == 0.
232 */
233 sp = ((sp + 4) & -16ul) - 4;
234#else /* !CONFIG_X86_32 */
235 sp = round_down(sp, 16) - 8;
236#endif
237 return sp;
238}
239
Denys Vlasenkodae0f302015-09-28 14:23:57 +0200240static void __user *
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800241get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
242 void __user **fpstate)
243{
244 /* Default to using normal stack */
Suresh Siddha72a671c2012-07-24 16:05:29 -0700245 unsigned long math_size = 0;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800246 unsigned long sp = regs->sp;
Suresh Siddha72a671c2012-07-24 16:05:29 -0700247 unsigned long buf_fx = 0;
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700248 int onsigstack = on_sig_stack(sp);
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200249 struct fpu *fpu = &current->thread.fpu;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800250
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800251 /* redzone */
Masahiro Yamada97f26452016-08-03 13:45:50 -0700252 if (IS_ENABLED(CONFIG_X86_64))
Suresh Siddha050902c2012-07-24 16:05:27 -0700253 sp -= 128;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800254
Stas Sergeev0b4521e2016-04-14 23:20:02 +0300255 /* This is the X/Open sanctioned signal stack switching. */
256 if (ka->sa.sa_flags & SA_ONSTACK) {
257 if (sas_ss_flags(sp) == 0)
258 sp = current->sas_ss_sp + current->sas_ss_size;
Masahiro Yamada97f26452016-08-03 13:45:50 -0700259 } else if (IS_ENABLED(CONFIG_X86_32) &&
Stas Sergeev0b4521e2016-04-14 23:20:02 +0300260 !onsigstack &&
Andy Lutomirski99504812017-07-28 06:00:32 -0700261 regs->ss != __USER_DS &&
Stas Sergeev0b4521e2016-04-14 23:20:02 +0300262 !(ka->sa.sa_flags & SA_RESTORER) &&
263 ka->sa.sa_restorer) {
264 /* This is the legacy signal stack switching. */
265 sp = (unsigned long) ka->sa.sa_restorer;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800266 }
267
Ingo Molnare4a81bf2017-09-26 09:43:36 +0200268 if (fpu->initialized) {
Masahiro Yamada97f26452016-08-03 13:45:50 -0700269 sp = fpu__alloc_mathframe(sp, IS_ENABLED(CONFIG_X86_32),
Ingo Molnar82c0e452015-04-29 21:09:18 +0200270 &buf_fx, &math_size);
Hiroshi Shimamoto25051702009-03-02 17:20:01 -0800271 *fpstate = (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800272 }
273
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700274 sp = align_sigframe(sp - frame_size);
275
276 /*
277 * If we are on the alternate signal stack and would overflow it, don't.
278 * Return an always-bogus address instead so we will die with SIGSEGV.
279 */
280 if (onsigstack && !likely(on_sig_stack(sp)))
281 return (void __user *)-1L;
282
Suresh Siddha72a671c2012-07-24 16:05:29 -0700283 /* save i387 and extended state */
Ingo Molnare4a81bf2017-09-26 09:43:36 +0200284 if (fpu->initialized &&
Ingo Molnarc8e140412015-04-28 11:35:20 +0200285 copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size) < 0)
Hiroshi Shimamoto14fc9fb2009-03-19 10:56:29 -0700286 return (void __user *)-1L;
287
288 return (void __user *)sp;
Hiroshi Shimamoto75779f02009-02-27 10:29:57 -0800289}
290
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800291#ifdef CONFIG_X86_32
292static const struct {
293 u16 poplmovl;
294 u32 val;
295 u16 int80;
296} __attribute__((packed)) retcode = {
297 0xb858, /* popl %eax; movl $..., %eax */
298 __NR_sigreturn,
299 0x80cd, /* int $0x80 */
300};
301
302static const struct {
303 u8 movl;
304 u32 val;
305 u16 int80;
306 u8 pad;
307} __attribute__((packed)) rt_retcode = {
308 0xb8, /* movl $..., %eax */
309 __NR_rt_sigreturn,
310 0x80cd, /* int $0x80 */
311 0
312};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Ingo Molnar7e907f42008-03-06 10:33:08 +0100314static int
Al Viro235b8022012-11-09 23:51:47 -0500315__setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700316 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 struct sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100319 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700321 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Al Viro235b8022012-11-09 23:51:47 -0500323 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
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 (__put_user(sig, &frame->sig))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700329 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700331 if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700332 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 if (_NSIG_WORDS > 1) {
Hiroshi Shimamoto2ba48e12008-09-12 17:02:53 -0700335 if (__copy_to_user(&frame->extramask, &set->sig[1],
336 sizeof(frame->extramask)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700337 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339
Roland McGrath1a3e4ca2008-04-09 01:29:27 -0700340 if (current->mm->context.vdso)
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700341 restorer = current->mm->context.vdso +
Andy Lutomirski0a6d1fa2015-10-05 17:47:56 -0700342 vdso_image_32.sym___kernel_sigreturn;
Andi Kleen9fbbd4d2007-02-13 13:26:26 +0100343 else
Jan Engelhardtade1af72008-01-30 13:33:23 +0100344 restorer = &frame->retcode;
Al Viro235b8022012-11-09 23:51:47 -0500345 if (ksig->ka.sa.sa_flags & SA_RESTORER)
346 restorer = ksig->ka.sa.sa_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 /* Set up to return from userspace. */
349 err |= __put_user(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 /*
Ingo Molnar7e907f42008-03-06 10:33:08 +0100352 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 *
354 * WE DO NOT USE IT ANY MORE! It's only left here for historical
355 * reasons and because gdb uses it as a signature to notice
356 * signal handler stack frames.
357 */
Hiroshi Shimamoto4a612042008-11-11 19:09:29 -0800358 err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700361 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100364 regs->sp = (unsigned long)frame;
Al Viro235b8022012-11-09 23:51:47 -0500365 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100366 regs->ax = (unsigned long)sig;
Harvey Harrison92bc2052008-02-08 12:09:56 -0800367 regs->dx = 0;
368 regs->cx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100370 regs->ds = __USER_DS;
371 regs->es = __USER_DS;
372 regs->ss = __USER_DS;
373 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
David Howells283828f2006-01-18 17:44:00 -0800375 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
Al Viro235b8022012-11-09 23:51:47 -0500378static int __setup_rt_frame(int sig, struct ksignal *ksig,
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700379 sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 struct rt_sigframe __user *frame;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100382 void __user *restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 int err = 0;
Suresh Siddhaab513702008-07-29 10:29:22 -0700384 void __user *fpstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Al Viro235b8022012-11-09 23:51:47 -0500386 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700389 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800391 put_user_try {
392 put_user_ex(sig, &frame->sig);
393 put_user_ex(&frame->info, &frame->pinfo);
394 put_user_ex(&frame->uc, &frame->puc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800396 /* Create the ucontext. */
Borislav Petkovd366bf72016-04-04 22:25:02 +0200397 if (boot_cpu_has(X86_FEATURE_XSAVE))
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800398 put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
399 else
400 put_user_ex(0, &frame->uc.uc_flags);
401 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100402 save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800404 /* Set up to return from userspace. */
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700405 restorer = current->mm->context.vdso +
Andy Lutomirski0a6d1fa2015-10-05 17:47:56 -0700406 vdso_image_32.sym___kernel_rt_sigreturn;
Al Viro235b8022012-11-09 23:51:47 -0500407 if (ksig->ka.sa.sa_flags & SA_RESTORER)
408 restorer = ksig->ka.sa.sa_restorer;
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800409 put_user_ex(restorer, &frame->pretcode);
Ingo Molnar7e907f42008-03-06 10:33:08 +0100410
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800411 /*
412 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
413 *
414 * WE DO NOT USE IT ANY MORE! It's only left here for historical
415 * reasons and because gdb uses it as a signature to notice
416 * signal handler stack frames.
417 */
418 put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
419 } put_user_catch(err);
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700420
Al Viro235b8022012-11-09 23:51:47 -0500421 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
H. Peter Anvin5e883532012-09-21 12:43:15 -0700422 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
423 regs, set->sig[0]);
424 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 if (err)
Hiroshi Shimamoto3d0aedd2008-09-12 17:01:09 -0700427 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 /* Set up registers for signal handler */
Ingo Molnar7e907f42008-03-06 10:33:08 +0100430 regs->sp = (unsigned long)frame;
Al Viro235b8022012-11-09 23:51:47 -0500431 regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
Hiroshi Shimamoto13ad7722008-09-05 16:28:38 -0700432 regs->ax = (unsigned long)sig;
Ingo Molnar7e907f42008-03-06 10:33:08 +0100433 regs->dx = (unsigned long)&frame->info;
434 regs->cx = (unsigned long)&frame->uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100436 regs->ds = __USER_DS;
437 regs->es = __USER_DS;
438 regs->ss = __USER_DS;
439 regs->cs = __USER_CS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
David Howells283828f2006-01-18 17:44:00 -0800441 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800443#else /* !CONFIG_X86_32 */
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800444static unsigned long frame_uc_flags(struct pt_regs *regs)
445{
446 unsigned long flags;
447
Borislav Petkovd366bf72016-04-04 22:25:02 +0200448 if (boot_cpu_has(X86_FEATURE_XSAVE))
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800449 flags = UC_FP_XSTATE | UC_SIGCONTEXT_SS;
450 else
451 flags = UC_SIGCONTEXT_SS;
452
453 if (likely(user_64bit_mode(regs)))
454 flags |= UC_STRICT_RESTORE_SS;
455
456 return flags;
457}
458
Al Viro235b8022012-11-09 23:51:47 -0500459static int __setup_rt_frame(int sig, struct ksignal *ksig,
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800460 sigset_t *set, struct pt_regs *regs)
461{
462 struct rt_sigframe __user *frame;
463 void __user *fp = NULL;
464 int err = 0;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800465
Al Viro235b8022012-11-09 23:51:47 -0500466 frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800467
468 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
469 return -EFAULT;
470
Al Viro235b8022012-11-09 23:51:47 -0500471 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
472 if (copy_siginfo_to_user(&frame->info, &ksig->info))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800473 return -EFAULT;
474 }
475
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800476 put_user_try {
477 /* Create the ucontext. */
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800478 put_user_ex(frame_uc_flags(regs), &frame->uc.uc_flags);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800479 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100480 save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800481
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800482 /* Set up to return from userspace. If provided, use a stub
483 already in userspace. */
484 /* x86-64 should always use SA_RESTORER. */
Al Viro235b8022012-11-09 23:51:47 -0500485 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
486 put_user_ex(ksig->ka.sa.sa_restorer, &frame->pretcode);
Hiroshi Shimamoto98e3d452009-01-23 15:50:10 -0800487 } else {
488 /* could use a vstub here */
489 err |= -EFAULT;
490 }
491 } put_user_catch(err);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800492
H. Peter Anvin5e883532012-09-21 12:43:15 -0700493 err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
494 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
495
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800496 if (err)
497 return -EFAULT;
498
499 /* Set up registers for signal handler */
500 regs->di = sig;
501 /* In case the signal handler was declared without prototypes */
502 regs->ax = 0;
503
504 /* This also works for non SA_SIGINFO handlers because they expect the
505 next argument after the signal number on the stack. */
506 regs->si = (unsigned long)&frame->info;
507 regs->dx = (unsigned long)&frame->uc;
Al Viro235b8022012-11-09 23:51:47 -0500508 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800509
510 regs->sp = (unsigned long)frame;
511
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -0800512 /*
513 * Set up the CS and SS registers to run signal handlers in
514 * 64-bit mode, even if the handler happens to be interrupting
515 * 32-bit or 16-bit code.
516 *
517 * SS is subtle. In 64-bit mode, we don't need any particular
518 * SS descriptor, but we do need SS to be valid. It's possible
519 * that the old SS is entirely bogus -- this can happen if the
520 * signal we're trying to deliver is #GP or #SS caused by a bad
521 * SS value. We also have a compatbility issue here: DOSEMU
522 * relies on the contents of the SS register indicating the
523 * SS value at the time of the signal, even though that code in
524 * DOSEMU predates sigreturn's ability to restore SS. (DOSEMU
525 * avoids relying on sigreturn to restore SS; instead it uses
526 * a trampoline.) So we do our best: if the old SS was valid,
527 * we keep it. Otherwise we replace it.
528 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800529 regs->cs = __USER_CS;
530
Andy Lutomirski8ff5bd22016-02-16 15:09:02 -0800531 if (unlikely(regs->ss != __USER_DS))
532 force_valid_ss(regs);
533
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800534 return 0;
535}
536#endif /* CONFIG_X86_32 */
537
Al Viro235b8022012-11-09 23:51:47 -0500538static int x32_setup_rt_frame(struct ksignal *ksig,
539 compat_sigset_t *set,
Suresh Siddha050902c2012-07-24 16:05:27 -0700540 struct pt_regs *regs)
541{
542#ifdef CONFIG_X86_X32_ABI
543 struct rt_sigframe_x32 __user *frame;
544 void __user *restorer;
545 int err = 0;
546 void __user *fpstate = NULL;
547
Al Viro235b8022012-11-09 23:51:47 -0500548 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
Suresh Siddha050902c2012-07-24 16:05:27 -0700549
550 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
551 return -EFAULT;
552
Al Viro235b8022012-11-09 23:51:47 -0500553 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
Dmitry Safonov68463512016-09-05 16:33:08 +0300554 if (__copy_siginfo_to_user32(&frame->info, &ksig->info, true))
Suresh Siddha050902c2012-07-24 16:05:27 -0700555 return -EFAULT;
556 }
557
558 put_user_try {
559 /* Create the ucontext. */
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800560 put_user_ex(frame_uc_flags(regs), &frame->uc.uc_flags);
Suresh Siddha050902c2012-07-24 16:05:27 -0700561 put_user_ex(0, &frame->uc.uc_link);
Al Virobd1c149a2013-09-01 20:35:01 +0100562 compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
Suresh Siddha050902c2012-07-24 16:05:27 -0700563 put_user_ex(0, &frame->uc.uc__pad0);
Suresh Siddha050902c2012-07-24 16:05:27 -0700564
Al Viro235b8022012-11-09 23:51:47 -0500565 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
566 restorer = ksig->ka.sa.sa_restorer;
Suresh Siddha050902c2012-07-24 16:05:27 -0700567 } else {
568 /* could use a vstub here */
569 restorer = NULL;
570 err |= -EFAULT;
571 }
572 put_user_ex(restorer, &frame->pretcode);
573 } put_user_catch(err);
574
H. Peter Anvin49b8c6952012-09-21 17:18:44 -0700575 err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
576 regs, set->sig[0]);
577 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
578
Suresh Siddha050902c2012-07-24 16:05:27 -0700579 if (err)
580 return -EFAULT;
581
582 /* Set up registers for signal handler */
583 regs->sp = (unsigned long) frame;
Al Viro235b8022012-11-09 23:51:47 -0500584 regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
Suresh Siddha050902c2012-07-24 16:05:27 -0700585
586 /* We use the x32 calling convention here... */
Al Viro235b8022012-11-09 23:51:47 -0500587 regs->di = ksig->sig;
Suresh Siddha050902c2012-07-24 16:05:27 -0700588 regs->si = (unsigned long) &frame->info;
589 regs->dx = (unsigned long) &frame->uc;
590
591 loadsegment(ds, __USER_DS);
592 loadsegment(es, __USER_DS);
593
594 regs->cs = __USER_CS;
595 regs->ss = __USER_DS;
596#endif /* CONFIG_X86_X32_ABI */
597
598 return 0;
599}
600
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800601/*
602 * Do a signal return; undo the signal stack.
603 */
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800604#ifdef CONFIG_X86_32
Tautschnig, Michael4c8ca512018-03-14 09:41:42 +0000605SYSCALL_DEFINE0(sigreturn)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800606{
Al Viro3fe26fa2012-11-12 14:32:42 -0500607 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800608 struct sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800609 sigset_t set;
610
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800611 frame = (struct sigframe __user *)(regs->sp - 8);
612
613 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
614 goto badframe;
615 if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
616 && __copy_from_user(&set.sig[1], &frame->extramask,
617 sizeof(frame->extramask))))
618 goto badframe;
619
Oleg Nesterov39822942011-07-10 21:27:27 +0200620 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800621
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800622 /*
623 * x86_32 has no uc_flags bits relevant to restore_sigcontext.
624 * Save a few cycles by skipping the __get_user.
625 */
626 if (restore_sigcontext(regs, &frame->sc, 0))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800627 goto badframe;
Brian Gerst6a3713f2015-04-04 08:58:23 -0400628 return regs->ax;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800629
630badframe:
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800631 signal_fault(regs, frame, "sigreturn");
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800632
633 return 0;
634}
Hiroshi Shimamotoe5fa2d02008-11-24 18:24:11 -0800635#endif /* CONFIG_X86_32 */
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800636
Tautschnig, Michael4c8ca512018-03-14 09:41:42 +0000637SYSCALL_DEFINE0(rt_sigreturn)
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800638{
Al Viro3fe26fa2012-11-12 14:32:42 -0500639 struct pt_regs *regs = current_pt_regs();
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800640 struct rt_sigframe __user *frame;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800641 sigset_t set;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800642 unsigned long uc_flags;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800643
644 frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
645 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
646 goto badframe;
647 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
648 goto badframe;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800649 if (__get_user(uc_flags, &frame->uc.uc_flags))
650 goto badframe;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800651
Oleg Nesterove9bd3f02011-04-27 21:09:39 +0200652 set_current_blocked(&set);
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800653
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800654 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800655 goto badframe;
656
Al Viroc40702c2012-11-20 14:24:26 -0500657 if (restore_altstack(&frame->uc.uc_stack))
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800658 goto badframe;
659
Brian Gerst6a3713f2015-04-04 08:58:23 -0400660 return regs->ax;
Hiroshi Shimamotobfeb91a2008-11-24 18:23:12 -0800661
662badframe:
663 signal_fault(regs, frame, "rt_sigreturn");
664 return 0;
665}
666
Dmitry Safonov68463512016-09-05 16:33:08 +0300667static inline int is_ia32_compat_frame(struct ksignal *ksig)
Ingo Molnar05012c12015-04-30 07:26:04 +0200668{
Masahiro Yamada97f26452016-08-03 13:45:50 -0700669 return IS_ENABLED(CONFIG_IA32_EMULATION) &&
Dmitry Safonov68463512016-09-05 16:33:08 +0300670 ksig->ka.sa.sa_flags & SA_IA32_ABI;
Ingo Molnar05012c12015-04-30 07:26:04 +0200671}
672
Dmitry Safonov68463512016-09-05 16:33:08 +0300673static inline int is_ia32_frame(struct ksignal *ksig)
Ingo Molnar05012c12015-04-30 07:26:04 +0200674{
Dmitry Safonov68463512016-09-05 16:33:08 +0300675 return IS_ENABLED(CONFIG_X86_32) || is_ia32_compat_frame(ksig);
Ingo Molnar05012c12015-04-30 07:26:04 +0200676}
677
Dmitry Safonov68463512016-09-05 16:33:08 +0300678static inline int is_x32_frame(struct ksignal *ksig)
Ingo Molnar05012c12015-04-30 07:26:04 +0200679{
Dmitry Safonov68463512016-09-05 16:33:08 +0300680 return IS_ENABLED(CONFIG_X86_X32_ABI) &&
681 ksig->ka.sa.sa_flags & SA_X32_ABI;
Ingo Molnar05012c12015-04-30 07:26:04 +0200682}
683
Roland McGrath7c1def12005-06-23 00:08:21 -0700684static int
Al Viro235b8022012-11-09 23:51:47 -0500685setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700686{
Richard Weinberger3050a352014-07-13 17:43:51 +0200687 int usig = ksig->sig;
Al Virob7f9a112012-05-02 09:59:21 -0400688 sigset_t *set = sigmask_to_save();
Suresh Siddha050902c2012-07-24 16:05:27 -0700689 compat_sigset_t *cset = (compat_sigset_t *) set;
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700690
Mathieu Desnoyersd6761b82018-06-02 08:43:58 -0400691 /*
692 * Increment event counter and perform fixup for the pre-signal
693 * frame.
694 */
Will Deacon784e0302018-06-22 11:45:07 +0100695 rseq_signal_deliver(ksig, regs);
Mathieu Desnoyersd6761b82018-06-02 08:43:58 -0400696
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700697 /* Set up the stack frame */
Dmitry Safonov68463512016-09-05 16:33:08 +0300698 if (is_ia32_frame(ksig)) {
Al Viro235b8022012-11-09 23:51:47 -0500699 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
700 return ia32_setup_rt_frame(usig, ksig, cset, regs);
Hiroshi Shimamoto455edbc2008-09-24 19:13:11 -0700701 else
Al Viro235b8022012-11-09 23:51:47 -0500702 return ia32_setup_frame(usig, ksig, cset, regs);
Dmitry Safonov68463512016-09-05 16:33:08 +0300703 } else if (is_x32_frame(ksig)) {
Al Viro235b8022012-11-09 23:51:47 -0500704 return x32_setup_rt_frame(ksig, cset, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800705 } else {
Al Viro235b8022012-11-09 23:51:47 -0500706 return __setup_rt_frame(ksig->sig, ksig, set, regs);
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800707 }
Hiroshi Shimamoto1d130242008-09-05 16:28:06 -0700708}
709
Al Viroa610d6e2012-05-21 23:42:15 -0400710static void
Al Viro235b8022012-11-09 23:51:47 -0500711handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700713 bool stepping, failed;
Ingo Molnarc5bedc62015-04-23 12:49:20 +0200714 struct fpu *fpu = &current->thread.fpu;
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700715
Brian Gerst5ed92a82015-07-29 01:41:19 -0400716 if (v8086_mode(regs))
717 save_v86_state((struct kernel_vm86_regs *) regs, VM86_SIGNAL);
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 /* Are we from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700720 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 /* If so, check system call restarting.. */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700722 switch (syscall_get_error(current, regs)) {
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800723 case -ERESTART_RESTARTBLOCK:
724 case -ERESTARTNOHAND:
725 regs->ax = -EINTR;
726 break;
727
728 case -ERESTARTSYS:
Al Viro235b8022012-11-09 23:51:47 -0500729 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100730 regs->ax = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 break;
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800732 }
733 /* fallthrough */
734 case -ERESTARTNOINTR:
735 regs->ax = regs->orig_ax;
736 regs->ip -= 2;
737 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739 }
740
741 /*
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700742 * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now
743 * so that register information in the sigcontext is correct and
744 * then notify the tracer before entering the signal handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 */
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700746 stepping = test_thread_flag(TIF_SINGLESTEP);
747 if (stepping)
748 user_disable_single_step(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Al Viro235b8022012-11-09 23:51:47 -0500750 failed = (setup_rt_frame(ksig, regs) < 0);
751 if (!failed) {
752 /*
753 * Clear the direction flag as per the ABI for function entry.
Jiri Olsaddd40da2013-05-01 17:25:43 +0200754 *
Jiri Olsa24cda102013-05-01 17:25:42 +0200755 * Clear RF when entering the signal handler, because
756 * it might disable possible debug exception from the
757 * signal handler.
Jiri Olsaddd40da2013-05-01 17:25:43 +0200758 *
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700759 * Clear TF for the case when it wasn't set by debugger to
760 * avoid the recursive send_sigtrap() in SIGTRAP handler.
Al Viro235b8022012-11-09 23:51:47 -0500761 */
Jiri Olsaddd40da2013-05-01 17:25:43 +0200762 regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
Oleg Nesterov66463db2014-09-02 19:57:13 +0200763 /*
764 * Ensure the signal handler starts with the new fpu state.
765 */
Ingo Molnare4a81bf2017-09-26 09:43:36 +0200766 if (fpu->initialized)
Ingo Molnarfbce7782015-04-30 07:12:46 +0200767 fpu__clear(fpu);
Al Viroa610d6e2012-05-21 23:42:15 -0400768 }
Oleg Nesterovfd0f86b2015-04-16 00:40:25 -0700769 signal_setup_done(failed, ksig, stepping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300772static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs)
773{
Andy Lutomirski609c19a2016-07-26 23:12:22 -0700774 /*
775 * This function is fundamentally broken as currently
776 * implemented.
777 *
778 * The idea is that we want to trigger a call to the
779 * restart_block() syscall and that we want in_ia32_syscall(),
780 * in_x32_syscall(), etc. to match whatever they were in the
781 * syscall being restarted. We assume that the syscall
782 * instruction at (regs->ip - 2) matches whatever syscall
783 * instruction we used to enter in the first place.
784 *
785 * The problem is that we can get here when ptrace pokes
786 * syscall-like values into regs even if we're not in a syscall
787 * at all.
788 *
789 * For now, we maintain historical behavior and guess based on
790 * stored state. We could do better by saving the actual
791 * syscall arch in restart_block or (with caveats on x32) by
792 * checking if regs->ip points to 'int $0x80'. The current
793 * behavior is incorrect if a tracer has a different bitness
794 * than the tracee.
795 */
796#ifdef CONFIG_IA32_EMULATION
Andy Lutomirski37a8f7c2018-01-28 10:38:50 -0800797 if (current_thread_info()->status & (TS_COMPAT|TS_I386_REGS_POKED))
Dmitry V. Levin95d97ad2015-12-17 23:56:52 +0000798 return __NR_ia32_restart_syscall;
799#endif
800#ifdef CONFIG_X86_X32_ABI
801 return __NR_restart_syscall | (regs->orig_ax & __X32_SYSCALL_BIT);
802#else
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300803 return __NR_restart_syscall;
Dmitry V. Levin95d97ad2015-12-17 23:56:52 +0000804#endif
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300805}
Hiroshi Shimamoto57917752008-10-29 18:46:40 -0700806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807/*
808 * Note that 'init' is a special process: it doesn't get signals it doesn't
809 * want to handle. Thus you cannot kill init even with a SIGKILL even by
810 * mistake.
811 */
Andy Lutomirski1f484aa2015-07-03 12:44:23 -0700812void do_signal(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Al Viro235b8022012-11-09 23:51:47 -0500814 struct ksignal ksig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Al Viro235b8022012-11-09 23:51:47 -0500816 if (get_signal(&ksig)) {
Ingo Molnar7e907f42008-03-06 10:33:08 +0100817 /* Whee! Actually deliver the signal. */
Al Viro235b8022012-11-09 23:51:47 -0500818 handle_signal(&ksig, regs);
David Howells283828f2006-01-18 17:44:00 -0800819 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 /* Did we come from a system call? */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700823 if (syscall_get_nr(current, regs) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 /* Restart the system call - no handlers present */
Hiroshi Shimamotobb579252008-09-05 16:26:55 -0700825 switch (syscall_get_error(current, regs)) {
David Howells283828f2006-01-18 17:44:00 -0800826 case -ERESTARTNOHAND:
827 case -ERESTARTSYS:
828 case -ERESTARTNOINTR:
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100829 regs->ax = regs->orig_ax;
830 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800831 break;
832
833 case -ERESTART_RESTARTBLOCK:
Dmitry V. Levin22eab1102015-12-01 00:54:36 +0300834 regs->ax = get_nr_restart_syscall(regs);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100835 regs->ip -= 2;
David Howells283828f2006-01-18 17:44:00 -0800836 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838 }
David Howells283828f2006-01-18 17:44:00 -0800839
Harvey Harrisonac66f3f2008-02-08 12:09:58 -0800840 /*
841 * If there's no signal to deliver, we just put the saved sigmask
842 * back.
843 */
Al Viro51a7b442012-05-21 23:33:55 -0400844 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700847void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
848{
849 struct task_struct *me = current;
850
851 if (show_unhandled_signals && printk_ratelimit()) {
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800852 printk("%s"
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700853 "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
Hiroshi Shimamotoae417bb2008-12-16 14:02:16 -0800854 task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700855 me->comm, me->pid, where, frame,
856 regs->ip, regs->sp, regs->orig_ax);
Markus Trippelsdorf1c99a682017-04-07 14:09:04 +0200857 print_vma_addr(KERN_CONT " in ", regs->ip);
Joe Perchesc767a542012-05-21 19:50:07 -0700858 pr_cont("\n");
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700859 }
860
861 force_sig(SIGSEGV, me);
862}
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800863
864#ifdef CONFIG_X86_X32_ABI
Al Viro3fe26fa2012-11-12 14:32:42 -0500865asmlinkage long sys32_x32_rt_sigreturn(void)
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800866{
Al Viro3fe26fa2012-11-12 14:32:42 -0500867 struct pt_regs *regs = current_pt_regs();
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800868 struct rt_sigframe_x32 __user *frame;
869 sigset_t set;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800870 unsigned long uc_flags;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800871
872 frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
873
874 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
875 goto badframe;
876 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
877 goto badframe;
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800878 if (__get_user(uc_flags, &frame->uc.uc_flags))
879 goto badframe;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800880
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800881 set_current_blocked(&set);
882
Andy Lutomirski6c25da52016-02-16 15:09:03 -0800883 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800884 goto badframe;
885
Al Viro90268432012-12-14 14:47:53 -0500886 if (compat_restore_altstack(&frame->uc.uc_stack))
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800887 goto badframe;
888
Brian Gerst6a3713f2015-04-04 08:58:23 -0400889 return regs->ax;
H. Peter Anvinc5a37392012-02-19 09:41:09 -0800890
891badframe:
892 signal_fault(regs, frame, "x32 rt_sigreturn");
893 return 0;
894}
895#endif