xref: /openbmc/linux/arch/x86/kernel/signal.c (revision 4f6cce39)
1 /*
2  *  Copyright (C) 1991, 1992  Linus Torvalds
3  *  Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
4  *
5  *  1997-11-28  Modified for POSIX.1b signals by Richard Henderson
6  *  2000-06-20  Pentium III FXSR, SSE support by Gareth Hughes
7  *  2000-2002   x86-64 support by Andi Kleen
8  */
9 
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 
12 #include <linux/sched.h>
13 #include <linux/sched/task_stack.h>
14 #include <linux/mm.h>
15 #include <linux/smp.h>
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/wait.h>
19 #include <linux/tracehook.h>
20 #include <linux/unistd.h>
21 #include <linux/stddef.h>
22 #include <linux/personality.h>
23 #include <linux/uaccess.h>
24 #include <linux/user-return-notifier.h>
25 #include <linux/uprobes.h>
26 #include <linux/context_tracking.h>
27 
28 #include <asm/processor.h>
29 #include <asm/ucontext.h>
30 #include <asm/fpu/internal.h>
31 #include <asm/fpu/signal.h>
32 #include <asm/vdso.h>
33 #include <asm/mce.h>
34 #include <asm/sighandling.h>
35 #include <asm/vm86.h>
36 
37 #ifdef CONFIG_X86_64
38 #include <asm/proto.h>
39 #include <asm/ia32_unistd.h>
40 #endif /* CONFIG_X86_64 */
41 
42 #include <asm/syscall.h>
43 #include <asm/syscalls.h>
44 
45 #include <asm/sigframe.h>
46 #include <asm/signal.h>
47 
48 #define COPY(x)			do {			\
49 	get_user_ex(regs->x, &sc->x);			\
50 } while (0)
51 
52 #define GET_SEG(seg)		({			\
53 	unsigned short tmp;				\
54 	get_user_ex(tmp, &sc->seg);			\
55 	tmp;						\
56 })
57 
58 #define COPY_SEG(seg)		do {			\
59 	regs->seg = GET_SEG(seg);			\
60 } while (0)
61 
62 #define COPY_SEG_CPL3(seg)	do {			\
63 	regs->seg = GET_SEG(seg) | 3;			\
64 } while (0)
65 
66 #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  */
72 static 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 
95 static int restore_sigcontext(struct pt_regs *regs,
96 			      struct sigcontext __user *sc,
97 			      unsigned long uc_flags)
98 {
99 	unsigned long buf_val;
100 	void __user *buf;
101 	unsigned int tmpflags;
102 	unsigned int err = 0;
103 
104 	/* Always make any pending restarted system calls return -EINTR */
105 	current->restart_block.fn = do_no_restart_syscall;
106 
107 	get_user_try {
108 
109 #ifdef CONFIG_X86_32
110 		set_user_gs(regs, GET_SEG(gs));
111 		COPY_SEG(fs);
112 		COPY_SEG(es);
113 		COPY_SEG(ds);
114 #endif /* CONFIG_X86_32 */
115 
116 		COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
117 		COPY(dx); COPY(cx); COPY(ip); COPY(ax);
118 
119 #ifdef CONFIG_X86_64
120 		COPY(r8);
121 		COPY(r9);
122 		COPY(r10);
123 		COPY(r11);
124 		COPY(r12);
125 		COPY(r13);
126 		COPY(r14);
127 		COPY(r15);
128 #endif /* CONFIG_X86_64 */
129 
130 		COPY_SEG_CPL3(cs);
131 		COPY_SEG_CPL3(ss);
132 
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
142 
143 		get_user_ex(tmpflags, &sc->flags);
144 		regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
145 		regs->orig_ax = -1;		/* disable syscall checks */
146 
147 		get_user_ex(buf_val, &sc->fpstate);
148 		buf = (void __user *)buf_val;
149 	} get_user_catch(err);
150 
151 	err |= fpu__restore_sig(buf, IS_ENABLED(CONFIG_X86_32));
152 
153 	force_iret();
154 
155 	return err;
156 }
157 
158 int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
159 		     struct pt_regs *regs, unsigned long mask)
160 {
161 	int err = 0;
162 
163 	put_user_try {
164 
165 #ifdef CONFIG_X86_32
166 		put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
167 		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);
170 #endif /* CONFIG_X86_32 */
171 
172 		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);
180 #ifdef CONFIG_X86_64
181 		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);
189 #endif /* CONFIG_X86_64 */
190 
191 		put_user_ex(current->thread.trap_nr, &sc->trapno);
192 		put_user_ex(current->thread.error_code, &sc->err);
193 		put_user_ex(regs->ip, &sc->ip);
194 #ifdef CONFIG_X86_32
195 		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);
199 #else /* !CONFIG_X86_32 */
200 		put_user_ex(regs->flags, &sc->flags);
201 		put_user_ex(regs->cs, &sc->cs);
202 		put_user_ex(0, &sc->gs);
203 		put_user_ex(0, &sc->fs);
204 		put_user_ex(regs->ss, &sc->ss);
205 #endif /* CONFIG_X86_32 */
206 
207 		put_user_ex(fpstate, &sc->fpstate);
208 
209 		/* non-iBCS2 extensions.. */
210 		put_user_ex(mask, &sc->oldmask);
211 		put_user_ex(current->thread.cr2, &sc->cr2);
212 	} put_user_catch(err);
213 
214 	return err;
215 }
216 
217 /*
218  * Set up a signal frame.
219  */
220 
221 /*
222  * Determine which stack to use..
223  */
224 static 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 
238 static void __user *
239 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
240 	     void __user **fpstate)
241 {
242 	/* Default to using normal stack */
243 	unsigned long math_size = 0;
244 	unsigned long sp = regs->sp;
245 	unsigned long buf_fx = 0;
246 	int onsigstack = on_sig_stack(sp);
247 	struct fpu *fpu = &current->thread.fpu;
248 
249 	/* redzone */
250 	if (IS_ENABLED(CONFIG_X86_64))
251 		sp -= 128;
252 
253 	/* 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;
257 	} else if (IS_ENABLED(CONFIG_X86_32) &&
258 		   !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;
264 	}
265 
266 	if (fpu->fpstate_active) {
267 		sp = fpu__alloc_mathframe(sp, IS_ENABLED(CONFIG_X86_32),
268 					  &buf_fx, &math_size);
269 		*fpstate = (void __user *)sp;
270 	}
271 
272 	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 
281 	/* save i387 and extended state */
282 	if (fpu->fpstate_active &&
283 	    copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size) < 0)
284 		return (void __user *)-1L;
285 
286 	return (void __user *)sp;
287 }
288 
289 #ifdef CONFIG_X86_32
290 static 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 
300 static 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 };
311 
312 static int
313 __setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
314 	      struct pt_regs *regs)
315 {
316 	struct sigframe __user *frame;
317 	void __user *restorer;
318 	int err = 0;
319 	void __user *fpstate = NULL;
320 
321 	frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
322 
323 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
324 		return -EFAULT;
325 
326 	if (__put_user(sig, &frame->sig))
327 		return -EFAULT;
328 
329 	if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
330 		return -EFAULT;
331 
332 	if (_NSIG_WORDS > 1) {
333 		if (__copy_to_user(&frame->extramask, &set->sig[1],
334 				   sizeof(frame->extramask)))
335 			return -EFAULT;
336 	}
337 
338 	if (current->mm->context.vdso)
339 		restorer = current->mm->context.vdso +
340 			vdso_image_32.sym___kernel_sigreturn;
341 	else
342 		restorer = &frame->retcode;
343 	if (ksig->ka.sa.sa_flags & SA_RESTORER)
344 		restorer = ksig->ka.sa.sa_restorer;
345 
346 	/* Set up to return from userspace.  */
347 	err |= __put_user(restorer, &frame->pretcode);
348 
349 	/*
350 	 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
351 	 *
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 	 */
356 	err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
357 
358 	if (err)
359 		return -EFAULT;
360 
361 	/* Set up registers for signal handler */
362 	regs->sp = (unsigned long)frame;
363 	regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
364 	regs->ax = (unsigned long)sig;
365 	regs->dx = 0;
366 	regs->cx = 0;
367 
368 	regs->ds = __USER_DS;
369 	regs->es = __USER_DS;
370 	regs->ss = __USER_DS;
371 	regs->cs = __USER_CS;
372 
373 	return 0;
374 }
375 
376 static int __setup_rt_frame(int sig, struct ksignal *ksig,
377 			    sigset_t *set, struct pt_regs *regs)
378 {
379 	struct rt_sigframe __user *frame;
380 	void __user *restorer;
381 	int err = 0;
382 	void __user *fpstate = NULL;
383 
384 	frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
385 
386 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
387 		return -EFAULT;
388 
389 	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);
393 
394 		/* Create the ucontext.  */
395 		if (boot_cpu_has(X86_FEATURE_XSAVE))
396 			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);
400 		save_altstack_ex(&frame->uc.uc_stack, regs->sp);
401 
402 		/* Set up to return from userspace.  */
403 		restorer = current->mm->context.vdso +
404 			vdso_image_32.sym___kernel_rt_sigreturn;
405 		if (ksig->ka.sa.sa_flags & SA_RESTORER)
406 			restorer = ksig->ka.sa.sa_restorer;
407 		put_user_ex(restorer, &frame->pretcode);
408 
409 		/*
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);
418 
419 	err |= copy_siginfo_to_user(&frame->info, &ksig->info);
420 	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));
423 
424 	if (err)
425 		return -EFAULT;
426 
427 	/* Set up registers for signal handler */
428 	regs->sp = (unsigned long)frame;
429 	regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
430 	regs->ax = (unsigned long)sig;
431 	regs->dx = (unsigned long)&frame->info;
432 	regs->cx = (unsigned long)&frame->uc;
433 
434 	regs->ds = __USER_DS;
435 	regs->es = __USER_DS;
436 	regs->ss = __USER_DS;
437 	regs->cs = __USER_CS;
438 
439 	return 0;
440 }
441 #else /* !CONFIG_X86_32 */
442 static unsigned long frame_uc_flags(struct pt_regs *regs)
443 {
444 	unsigned long flags;
445 
446 	if (boot_cpu_has(X86_FEATURE_XSAVE))
447 		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 
457 static int __setup_rt_frame(int sig, struct ksignal *ksig,
458 			    sigset_t *set, struct pt_regs *regs)
459 {
460 	struct rt_sigframe __user *frame;
461 	void __user *fp = NULL;
462 	int err = 0;
463 
464 	frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
465 
466 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
467 		return -EFAULT;
468 
469 	if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
470 		if (copy_siginfo_to_user(&frame->info, &ksig->info))
471 			return -EFAULT;
472 	}
473 
474 	put_user_try {
475 		/* Create the ucontext.  */
476 		put_user_ex(frame_uc_flags(regs), &frame->uc.uc_flags);
477 		put_user_ex(0, &frame->uc.uc_link);
478 		save_altstack_ex(&frame->uc.uc_stack, regs->sp);
479 
480 		/* Set up to return from userspace.  If provided, use a stub
481 		   already in userspace.  */
482 		/* x86-64 should always use SA_RESTORER. */
483 		if (ksig->ka.sa.sa_flags & SA_RESTORER) {
484 			put_user_ex(ksig->ka.sa.sa_restorer, &frame->pretcode);
485 		} else {
486 			/* could use a vstub here */
487 			err |= -EFAULT;
488 		}
489 	} put_user_catch(err);
490 
491 	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 
494 	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;
506 	regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
507 
508 	regs->sp = (unsigned long)frame;
509 
510 	/*
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 	 */
527 	regs->cs = __USER_CS;
528 
529 	if (unlikely(regs->ss != __USER_DS))
530 		force_valid_ss(regs);
531 
532 	return 0;
533 }
534 #endif /* CONFIG_X86_32 */
535 
536 static int x32_setup_rt_frame(struct ksignal *ksig,
537 			      compat_sigset_t *set,
538 			      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 
546 	frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
547 
548 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
549 		return -EFAULT;
550 
551 	if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
552 		if (__copy_siginfo_to_user32(&frame->info, &ksig->info, true))
553 			return -EFAULT;
554 	}
555 
556 	put_user_try {
557 		/* Create the ucontext.  */
558 		put_user_ex(frame_uc_flags(regs), &frame->uc.uc_flags);
559 		put_user_ex(0, &frame->uc.uc_link);
560 		compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
561 		put_user_ex(0, &frame->uc.uc__pad0);
562 
563 		if (ksig->ka.sa.sa_flags & SA_RESTORER) {
564 			restorer = ksig->ka.sa.sa_restorer;
565 		} 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 
573 	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 
577 	if (err)
578 		return -EFAULT;
579 
580 	/* Set up registers for signal handler */
581 	regs->sp = (unsigned long) frame;
582 	regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
583 
584 	/* We use the x32 calling convention here... */
585 	regs->di = ksig->sig;
586 	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 
599 /*
600  * Do a signal return; undo the signal stack.
601  */
602 #ifdef CONFIG_X86_32
603 asmlinkage unsigned long sys_sigreturn(void)
604 {
605 	struct pt_regs *regs = current_pt_regs();
606 	struct sigframe __user *frame;
607 	sigset_t set;
608 
609 	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 
618 	set_current_blocked(&set);
619 
620 	/*
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))
625 		goto badframe;
626 	return regs->ax;
627 
628 badframe:
629 	signal_fault(regs, frame, "sigreturn");
630 
631 	return 0;
632 }
633 #endif /* CONFIG_X86_32 */
634 
635 asmlinkage long sys_rt_sigreturn(void)
636 {
637 	struct pt_regs *regs = current_pt_regs();
638 	struct rt_sigframe __user *frame;
639 	sigset_t set;
640 	unsigned long uc_flags;
641 
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;
647 	if (__get_user(uc_flags, &frame->uc.uc_flags))
648 		goto badframe;
649 
650 	set_current_blocked(&set);
651 
652 	if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
653 		goto badframe;
654 
655 	if (restore_altstack(&frame->uc.uc_stack))
656 		goto badframe;
657 
658 	return regs->ax;
659 
660 badframe:
661 	signal_fault(regs, frame, "rt_sigreturn");
662 	return 0;
663 }
664 
665 static inline int is_ia32_compat_frame(struct ksignal *ksig)
666 {
667 	return IS_ENABLED(CONFIG_IA32_EMULATION) &&
668 		ksig->ka.sa.sa_flags & SA_IA32_ABI;
669 }
670 
671 static inline int is_ia32_frame(struct ksignal *ksig)
672 {
673 	return IS_ENABLED(CONFIG_X86_32) || is_ia32_compat_frame(ksig);
674 }
675 
676 static inline int is_x32_frame(struct ksignal *ksig)
677 {
678 	return IS_ENABLED(CONFIG_X86_X32_ABI) &&
679 		ksig->ka.sa.sa_flags & SA_X32_ABI;
680 }
681 
682 static int
683 setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
684 {
685 	int usig = ksig->sig;
686 	sigset_t *set = sigmask_to_save();
687 	compat_sigset_t *cset = (compat_sigset_t *) set;
688 
689 	/* Set up the stack frame */
690 	if (is_ia32_frame(ksig)) {
691 		if (ksig->ka.sa.sa_flags & SA_SIGINFO)
692 			return ia32_setup_rt_frame(usig, ksig, cset, regs);
693 		else
694 			return ia32_setup_frame(usig, ksig, cset, regs);
695 	} else if (is_x32_frame(ksig)) {
696 		return x32_setup_rt_frame(ksig, cset, regs);
697 	} else {
698 		return __setup_rt_frame(ksig->sig, ksig, set, regs);
699 	}
700 }
701 
702 static void
703 handle_signal(struct ksignal *ksig, struct pt_regs *regs)
704 {
705 	bool stepping, failed;
706 	struct fpu *fpu = &current->thread.fpu;
707 
708 	if (v8086_mode(regs))
709 		save_v86_state((struct kernel_vm86_regs *) regs, VM86_SIGNAL);
710 
711 	/* Are we from a system call? */
712 	if (syscall_get_nr(current, regs) >= 0) {
713 		/* If so, check system call restarting.. */
714 		switch (syscall_get_error(current, regs)) {
715 		case -ERESTART_RESTARTBLOCK:
716 		case -ERESTARTNOHAND:
717 			regs->ax = -EINTR;
718 			break;
719 
720 		case -ERESTARTSYS:
721 			if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
722 				regs->ax = -EINTR;
723 				break;
724 			}
725 		/* fallthrough */
726 		case -ERESTARTNOINTR:
727 			regs->ax = regs->orig_ax;
728 			regs->ip -= 2;
729 			break;
730 		}
731 	}
732 
733 	/*
734 	 * 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.
737 	 */
738 	stepping = test_thread_flag(TIF_SINGLESTEP);
739 	if (stepping)
740 		user_disable_single_step(current);
741 
742 	failed = (setup_rt_frame(ksig, regs) < 0);
743 	if (!failed) {
744 		/*
745 		 * Clear the direction flag as per the ABI for function entry.
746 		 *
747 		 * Clear RF when entering the signal handler, because
748 		 * it might disable possible debug exception from the
749 		 * signal handler.
750 		 *
751 		 * Clear TF for the case when it wasn't set by debugger to
752 		 * avoid the recursive send_sigtrap() in SIGTRAP handler.
753 		 */
754 		regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
755 		/*
756 		 * Ensure the signal handler starts with the new fpu state.
757 		 */
758 		if (fpu->fpstate_active)
759 			fpu__clear(fpu);
760 	}
761 	signal_setup_done(failed, ksig, stepping);
762 }
763 
764 static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs)
765 {
766 	/*
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
789 	if (current->thread.status & (TS_COMPAT|TS_I386_REGS_POKED))
790 		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
795 	return __NR_restart_syscall;
796 #endif
797 }
798 
799 /*
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  */
804 void do_signal(struct pt_regs *regs)
805 {
806 	struct ksignal ksig;
807 
808 	if (get_signal(&ksig)) {
809 		/* Whee! Actually deliver the signal.  */
810 		handle_signal(&ksig, regs);
811 		return;
812 	}
813 
814 	/* Did we come from a system call? */
815 	if (syscall_get_nr(current, regs) >= 0) {
816 		/* Restart the system call - no handlers present */
817 		switch (syscall_get_error(current, regs)) {
818 		case -ERESTARTNOHAND:
819 		case -ERESTARTSYS:
820 		case -ERESTARTNOINTR:
821 			regs->ax = regs->orig_ax;
822 			regs->ip -= 2;
823 			break;
824 
825 		case -ERESTART_RESTARTBLOCK:
826 			regs->ax = get_nr_restart_syscall(regs);
827 			regs->ip -= 2;
828 			break;
829 		}
830 	}
831 
832 	/*
833 	 * If there's no signal to deliver, we just put the saved sigmask
834 	 * back.
835 	 */
836 	restore_saved_sigmask();
837 }
838 
839 void 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()) {
844 		printk("%s"
845 		       "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
846 		       task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
847 		       me->comm, me->pid, where, frame,
848 		       regs->ip, regs->sp, regs->orig_ax);
849 		print_vma_addr(" in ", regs->ip);
850 		pr_cont("\n");
851 	}
852 
853 	force_sig(SIGSEGV, me);
854 }
855 
856 #ifdef CONFIG_X86_X32_ABI
857 asmlinkage long sys32_x32_rt_sigreturn(void)
858 {
859 	struct pt_regs *regs = current_pt_regs();
860 	struct rt_sigframe_x32 __user *frame;
861 	sigset_t set;
862 	unsigned long uc_flags;
863 
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;
870 	if (__get_user(uc_flags, &frame->uc.uc_flags))
871 		goto badframe;
872 
873 	set_current_blocked(&set);
874 
875 	if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
876 		goto badframe;
877 
878 	if (compat_restore_altstack(&frame->uc.uc_stack))
879 		goto badframe;
880 
881 	return regs->ax;
882 
883 badframe:
884 	signal_fault(regs, frame, "x32 rt_sigreturn");
885 	return 0;
886 }
887 #endif
888