signal.c 22.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 *  Copyright (C) 1991, 1992  Linus Torvalds
3
 *  Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
L
Linus Torvalds 已提交
4 5 6
 *
 *  1997-11-28  Modified for POSIX.1b signals by Richard Henderson
 *  2000-06-20  Pentium III FXSR, SSE support by Gareth Hughes
7
 *  2000-2002   x86-64 support by Andi Kleen
L
Linus Torvalds 已提交
8
 */
9 10 11

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

12 13 14
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/smp.h>
L
Linus Torvalds 已提交
15 16 17
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/wait.h>
R
Roland McGrath 已提交
18
#include <linux/tracehook.h>
19 20 21 22
#include <linux/unistd.h>
#include <linux/stddef.h>
#include <linux/personality.h>
#include <linux/uaccess.h>
A
Avi Kivity 已提交
23
#include <linux/user-return-notifier.h>
24
#include <linux/uprobes.h>
25
#include <linux/context_tracking.h>
26

L
Linus Torvalds 已提交
27 28
#include <asm/processor.h>
#include <asm/ucontext.h>
29
#include <asm/fpu/internal.h>
30
#include <asm/fpu/signal.h>
R
Roland McGrath 已提交
31
#include <asm/vdso.h>
32
#include <asm/mce.h>
33
#include <asm/sighandling.h>
B
Brian Gerst 已提交
34
#include <asm/vm86.h>
35 36 37 38 39 40

#ifdef CONFIG_X86_64
#include <asm/proto.h>
#include <asm/ia32_unistd.h>
#endif /* CONFIG_X86_64 */

41
#include <asm/syscall.h>
42
#include <asm/syscalls.h>
43

44
#include <asm/sigframe.h>
L
Linus Torvalds 已提交
45

T
Tejun Heo 已提交
46 47 48
#define COPY(x)			do {			\
	get_user_ex(regs->x, &sc->x);			\
} while (0)
49

T
Tejun Heo 已提交
50 51 52 53 54
#define GET_SEG(seg)		({			\
	unsigned short tmp;				\
	get_user_ex(tmp, &sc->seg);			\
	tmp;						\
})
55

T
Tejun Heo 已提交
56 57 58
#define COPY_SEG(seg)		do {			\
	regs->seg = GET_SEG(seg);			\
} while (0)
59

T
Tejun Heo 已提交
60 61 62
#define COPY_SEG_CPL3(seg)	do {			\
	regs->seg = GET_SEG(seg) | 3;			\
} while (0)
63

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
#ifdef CONFIG_X86_64
/*
 * If regs->ss will cause an IRET fault, change it.  Otherwise leave it
 * alone.  Using this generally makes no sense unless
 * user_64bit_mode(regs) would return true.
 */
static void force_valid_ss(struct pt_regs *regs)
{
	u32 ar;
	asm volatile ("lar %[old_ss], %[ar]\n\t"
		      "jz 1f\n\t"		/* If invalid: */
		      "xorl %[ar], %[ar]\n\t"	/* set ar = 0 */
		      "1:"
		      : [ar] "=r" (ar)
		      : [old_ss] "rm" ((u16)regs->ss));

	/*
	 * For a valid 64-bit user context, we need DPL 3, type
	 * read-write data or read-write exp-down data, and S and P
	 * set.  We can't use VERW because VERW doesn't check the
	 * P bit.
	 */
	ar &= AR_DPL_MASK | AR_S | AR_P | AR_TYPE_MASK;
	if (ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA) &&
	    ar != (AR_DPL3 | AR_S | AR_P | AR_TYPE_RWDATA_EXPDOWN))
		regs->ss = __USER_DS;
}
#endif

93 94 95
static int restore_sigcontext(struct pt_regs *regs,
			      struct sigcontext __user *sc,
			      unsigned long uc_flags)
96
{
97
	unsigned long buf_val;
98 99 100 101 102
	void __user *buf;
	unsigned int tmpflags;
	unsigned int err = 0;

	/* Always make any pending restarted system calls return -EINTR */
103
	current->restart_block.fn = do_no_restart_syscall;
104

105 106
	get_user_try {

107
#ifdef CONFIG_X86_32
T
Tejun Heo 已提交
108
		set_user_gs(regs, GET_SEG(gs));
109 110 111
		COPY_SEG(fs);
		COPY_SEG(es);
		COPY_SEG(ds);
112 113
#endif /* CONFIG_X86_32 */

114
		COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
115
		COPY(dx); COPY(cx); COPY(ip); COPY(ax);
116 117

#ifdef CONFIG_X86_64
118 119 120 121 122 123 124 125
		COPY(r8);
		COPY(r9);
		COPY(r10);
		COPY(r11);
		COPY(r12);
		COPY(r13);
		COPY(r14);
		COPY(r15);
126 127
#endif /* CONFIG_X86_64 */

128 129
		COPY_SEG_CPL3(cs);
		COPY_SEG_CPL3(ss);
130 131 132 133 134 135 136 137 138 139

#ifdef CONFIG_X86_64
		/*
		 * Fix up SS if needed for the benefit of old DOSEMU and
		 * CRIU.
		 */
		if (unlikely(!(uc_flags & UC_STRICT_RESTORE_SS) &&
			     user_64bit_mode(regs)))
			force_valid_ss(regs);
#endif
140

141 142 143 144
		get_user_ex(tmpflags, &sc->flags);
		regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
		regs->orig_ax = -1;		/* disable syscall checks */

145 146
		get_user_ex(buf_val, &sc->fpstate);
		buf = (void __user *)buf_val;
147
	} get_user_catch(err);
148

149
	err |= fpu__restore_sig(buf, config_enabled(CONFIG_X86_32));
150

151 152
	force_iret();

153 154 155
	return err;
}

156 157
int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
		     struct pt_regs *regs, unsigned long mask)
158 159 160
{
	int err = 0;

161
	put_user_try {
162

163
#ifdef CONFIG_X86_32
T
Tejun Heo 已提交
164
		put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
165 166 167
		put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
		put_user_ex(regs->es, (unsigned int __user *)&sc->es);
		put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
168 169
#endif /* CONFIG_X86_32 */

170 171 172 173 174 175 176 177
		put_user_ex(regs->di, &sc->di);
		put_user_ex(regs->si, &sc->si);
		put_user_ex(regs->bp, &sc->bp);
		put_user_ex(regs->sp, &sc->sp);
		put_user_ex(regs->bx, &sc->bx);
		put_user_ex(regs->dx, &sc->dx);
		put_user_ex(regs->cx, &sc->cx);
		put_user_ex(regs->ax, &sc->ax);
178
#ifdef CONFIG_X86_64
179 180 181 182 183 184 185 186
		put_user_ex(regs->r8, &sc->r8);
		put_user_ex(regs->r9, &sc->r9);
		put_user_ex(regs->r10, &sc->r10);
		put_user_ex(regs->r11, &sc->r11);
		put_user_ex(regs->r12, &sc->r12);
		put_user_ex(regs->r13, &sc->r13);
		put_user_ex(regs->r14, &sc->r14);
		put_user_ex(regs->r15, &sc->r15);
187 188
#endif /* CONFIG_X86_64 */

189
		put_user_ex(current->thread.trap_nr, &sc->trapno);
190 191
		put_user_ex(current->thread.error_code, &sc->err);
		put_user_ex(regs->ip, &sc->ip);
192
#ifdef CONFIG_X86_32
193 194 195 196
		put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
		put_user_ex(regs->flags, &sc->flags);
		put_user_ex(regs->sp, &sc->sp_at_signal);
		put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
197
#else /* !CONFIG_X86_32 */
198 199
		put_user_ex(regs->flags, &sc->flags);
		put_user_ex(regs->cs, &sc->cs);
L
Linus Torvalds 已提交
200 201
		put_user_ex(0, &sc->gs);
		put_user_ex(0, &sc->fs);
202
		put_user_ex(regs->ss, &sc->ss);
203 204
#endif /* CONFIG_X86_32 */

205
		put_user_ex(fpstate, &sc->fpstate);
206

207 208 209 210
		/* non-iBCS2 extensions.. */
		put_user_ex(mask, &sc->oldmask);
		put_user_ex(current->thread.cr2, &sc->cr2);
	} put_user_catch(err);
211 212 213 214

	return err;
}

L
Linus Torvalds 已提交
215
/*
216
 * Set up a signal frame.
L
Linus Torvalds 已提交
217 218 219 220 221
 */

/*
 * Determine which stack to use..
 */
222 223 224 225 226 227 228 229 230 231 232 233 234 235
static unsigned long align_sigframe(unsigned long sp)
{
#ifdef CONFIG_X86_32
	/*
	 * Align the stack pointer according to the i386 ABI,
	 * i.e. so that on function entry ((sp + 4) & 15) == 0.
	 */
	sp = ((sp + 4) & -16ul) - 4;
#else /* !CONFIG_X86_32 */
	sp = round_down(sp, 16) - 8;
#endif
	return sp;
}

236
static void __user *
237
get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
238
	     void __user **fpstate)
L
Linus Torvalds 已提交
239 240
{
	/* Default to using normal stack */
241
	unsigned long math_size = 0;
242
	unsigned long sp = regs->sp;
243
	unsigned long buf_fx = 0;
244
	int onsigstack = on_sig_stack(sp);
245
	struct fpu *fpu = &current->thread.fpu;
246 247

	/* redzone */
248 249
	if (config_enabled(CONFIG_X86_64))
		sp -= 128;
L
Linus Torvalds 已提交
250

251 252 253 254 255 256 257 258 259 260 261
	/* This is the X/Open sanctioned signal stack switching.  */
	if (ka->sa.sa_flags & SA_ONSTACK) {
		if (sas_ss_flags(sp) == 0)
			sp = current->sas_ss_sp + current->sas_ss_size;
	} else if (config_enabled(CONFIG_X86_32) &&
		   !onsigstack &&
		   (regs->ss & 0xffff) != __USER_DS &&
		   !(ka->sa.sa_flags & SA_RESTORER) &&
		   ka->sa.sa_restorer) {
		/* This is the legacy signal stack switching. */
		sp = (unsigned long) ka->sa.sa_restorer;
L
Linus Torvalds 已提交
262 263
	}

264
	if (fpu->fpstate_active) {
265 266
		sp = fpu__alloc_mathframe(sp, config_enabled(CONFIG_X86_32),
					  &buf_fx, &math_size);
267
		*fpstate = (void __user *)sp;
268 269
	}

270 271 272 273 274 275 276 277 278
	sp = align_sigframe(sp - frame_size);

	/*
	 * If we are on the alternate signal stack and would overflow it, don't.
	 * Return an always-bogus address instead so we will die with SIGSEGV.
	 */
	if (onsigstack && !likely(on_sig_stack(sp)))
		return (void __user *)-1L;

279
	/* save i387 and extended state */
280
	if (fpu->fpstate_active &&
281
	    copy_fpstate_to_sigframe(*fpstate, (void __user *)buf_fx, math_size) < 0)
282 283 284
		return (void __user *)-1L;

	return (void __user *)sp;
L
Linus Torvalds 已提交
285 286
}

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
#ifdef CONFIG_X86_32
static const struct {
	u16 poplmovl;
	u32 val;
	u16 int80;
} __attribute__((packed)) retcode = {
	0xb858,		/* popl %eax; movl $..., %eax */
	__NR_sigreturn,
	0x80cd,		/* int $0x80 */
};

static const struct {
	u8  movl;
	u32 val;
	u16 int80;
	u8  pad;
} __attribute__((packed)) rt_retcode = {
	0xb8,		/* movl $..., %eax */
	__NR_rt_sigreturn,
	0x80cd,		/* int $0x80 */
	0
};

310
static int
A
Al Viro 已提交
311
__setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
312
	      struct pt_regs *regs)
L
Linus Torvalds 已提交
313 314
{
	struct sigframe __user *frame;
315
	void __user *restorer;
L
Linus Torvalds 已提交
316
	int err = 0;
317
	void __user *fpstate = NULL;
L
Linus Torvalds 已提交
318

A
Al Viro 已提交
319
	frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
L
Linus Torvalds 已提交
320 321

	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
322
		return -EFAULT;
L
Linus Torvalds 已提交
323

324
	if (__put_user(sig, &frame->sig))
325
		return -EFAULT;
L
Linus Torvalds 已提交
326

327
	if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
328
		return -EFAULT;
L
Linus Torvalds 已提交
329 330

	if (_NSIG_WORDS > 1) {
331 332
		if (__copy_to_user(&frame->extramask, &set->sig[1],
				   sizeof(frame->extramask)))
333
			return -EFAULT;
L
Linus Torvalds 已提交
334 335
	}

336
	if (current->mm->context.vdso)
337
		restorer = current->mm->context.vdso +
338
			vdso_image_32.sym___kernel_sigreturn;
339
	else
J
Jan Engelhardt 已提交
340
		restorer = &frame->retcode;
A
Al Viro 已提交
341 342
	if (ksig->ka.sa.sa_flags & SA_RESTORER)
		restorer = ksig->ka.sa.sa_restorer;
L
Linus Torvalds 已提交
343 344 345

	/* Set up to return from userspace.  */
	err |= __put_user(restorer, &frame->pretcode);
346

L
Linus Torvalds 已提交
347
	/*
348
	 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
L
Linus Torvalds 已提交
349 350 351 352 353
	 *
	 * WE DO NOT USE IT ANY MORE! It's only left here for historical
	 * reasons and because gdb uses it as a signature to notice
	 * signal handler stack frames.
	 */
354
	err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
L
Linus Torvalds 已提交
355 356

	if (err)
357
		return -EFAULT;
L
Linus Torvalds 已提交
358 359

	/* Set up registers for signal handler */
360
	regs->sp = (unsigned long)frame;
A
Al Viro 已提交
361
	regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
362
	regs->ax = (unsigned long)sig;
363 364
	regs->dx = 0;
	regs->cx = 0;
L
Linus Torvalds 已提交
365

366 367 368 369
	regs->ds = __USER_DS;
	regs->es = __USER_DS;
	regs->ss = __USER_DS;
	regs->cs = __USER_CS;
L
Linus Torvalds 已提交
370

371
	return 0;
L
Linus Torvalds 已提交
372 373
}

A
Al Viro 已提交
374
static int __setup_rt_frame(int sig, struct ksignal *ksig,
375
			    sigset_t *set, struct pt_regs *regs)
L
Linus Torvalds 已提交
376 377
{
	struct rt_sigframe __user *frame;
378
	void __user *restorer;
L
Linus Torvalds 已提交
379
	int err = 0;
380
	void __user *fpstate = NULL;
L
Linus Torvalds 已提交
381

A
Al Viro 已提交
382
	frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
L
Linus Torvalds 已提交
383 384

	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
385
		return -EFAULT;
L
Linus Torvalds 已提交
386

387 388 389 390 391 392
	put_user_try {
		put_user_ex(sig, &frame->sig);
		put_user_ex(&frame->info, &frame->pinfo);
		put_user_ex(&frame->uc, &frame->puc);

		/* Create the ucontext.  */
393
		if (boot_cpu_has(X86_FEATURE_XSAVE))
394 395 396 397
			put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
		else
			put_user_ex(0, &frame->uc.uc_flags);
		put_user_ex(0, &frame->uc.uc_link);
398
		save_altstack_ex(&frame->uc.uc_stack, regs->sp);
399 400

		/* Set up to return from userspace.  */
401
		restorer = current->mm->context.vdso +
402
			vdso_image_32.sym___kernel_rt_sigreturn;
A
Al Viro 已提交
403 404
		if (ksig->ka.sa.sa_flags & SA_RESTORER)
			restorer = ksig->ka.sa.sa_restorer;
405 406 407 408 409 410 411 412 413 414 415
		put_user_ex(restorer, &frame->pretcode);

		/*
		 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
		 *
		 * WE DO NOT USE IT ANY MORE! It's only left here for historical
		 * reasons and because gdb uses it as a signature to notice
		 * signal handler stack frames.
		 */
		put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
	} put_user_catch(err);
416
	
A
Al Viro 已提交
417
	err |= copy_siginfo_to_user(&frame->info, &ksig->info);
418 419 420
	err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
				regs, set->sig[0]);
	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
L
Linus Torvalds 已提交
421 422

	if (err)
423
		return -EFAULT;
L
Linus Torvalds 已提交
424 425

	/* Set up registers for signal handler */
426
	regs->sp = (unsigned long)frame;
A
Al Viro 已提交
427
	regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
428
	regs->ax = (unsigned long)sig;
429 430
	regs->dx = (unsigned long)&frame->info;
	regs->cx = (unsigned long)&frame->uc;
L
Linus Torvalds 已提交
431

432 433 434 435
	regs->ds = __USER_DS;
	regs->es = __USER_DS;
	regs->ss = __USER_DS;
	regs->cs = __USER_CS;
L
Linus Torvalds 已提交
436

437
	return 0;
L
Linus Torvalds 已提交
438
}
439
#else /* !CONFIG_X86_32 */
440 441 442 443
static unsigned long frame_uc_flags(struct pt_regs *regs)
{
	unsigned long flags;

444
	if (boot_cpu_has(X86_FEATURE_XSAVE))
445 446 447 448 449 450 451 452 453 454
		flags = UC_FP_XSTATE | UC_SIGCONTEXT_SS;
	else
		flags = UC_SIGCONTEXT_SS;

	if (likely(user_64bit_mode(regs)))
		flags |= UC_STRICT_RESTORE_SS;

	return flags;
}

A
Al Viro 已提交
455
static int __setup_rt_frame(int sig, struct ksignal *ksig,
456 457 458 459 460 461
			    sigset_t *set, struct pt_regs *regs)
{
	struct rt_sigframe __user *frame;
	void __user *fp = NULL;
	int err = 0;

A
Al Viro 已提交
462
	frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
463 464 465 466

	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
		return -EFAULT;

A
Al Viro 已提交
467 468
	if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
		if (copy_siginfo_to_user(&frame->info, &ksig->info))
469 470 471
			return -EFAULT;
	}

472 473
	put_user_try {
		/* Create the ucontext.  */
474
		put_user_ex(frame_uc_flags(regs), &frame->uc.uc_flags);
475
		put_user_ex(0, &frame->uc.uc_link);
476
		save_altstack_ex(&frame->uc.uc_stack, regs->sp);
477 478 479 480

		/* Set up to return from userspace.  If provided, use a stub
		   already in userspace.  */
		/* x86-64 should always use SA_RESTORER. */
A
Al Viro 已提交
481 482
		if (ksig->ka.sa.sa_flags & SA_RESTORER) {
			put_user_ex(ksig->ka.sa.sa_restorer, &frame->pretcode);
483 484 485 486 487
		} else {
			/* could use a vstub here */
			err |= -EFAULT;
		}
	} put_user_catch(err);
488

489 490 491
	err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));

492 493 494 495 496 497 498 499 500 501 502 503
	if (err)
		return -EFAULT;

	/* Set up registers for signal handler */
	regs->di = sig;
	/* In case the signal handler was declared without prototypes */
	regs->ax = 0;

	/* This also works for non SA_SIGINFO handlers because they expect the
	   next argument after the signal number on the stack. */
	regs->si = (unsigned long)&frame->info;
	regs->dx = (unsigned long)&frame->uc;
A
Al Viro 已提交
504
	regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
505 506 507

	regs->sp = (unsigned long)frame;

508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
	/*
	 * Set up the CS and SS registers to run signal handlers in
	 * 64-bit mode, even if the handler happens to be interrupting
	 * 32-bit or 16-bit code.
	 *
	 * SS is subtle.  In 64-bit mode, we don't need any particular
	 * SS descriptor, but we do need SS to be valid.  It's possible
	 * that the old SS is entirely bogus -- this can happen if the
	 * signal we're trying to deliver is #GP or #SS caused by a bad
	 * SS value.  We also have a compatbility issue here: DOSEMU
	 * relies on the contents of the SS register indicating the
	 * SS value at the time of the signal, even though that code in
	 * DOSEMU predates sigreturn's ability to restore SS.  (DOSEMU
	 * avoids relying on sigreturn to restore SS; instead it uses
	 * a trampoline.)  So we do our best: if the old SS was valid,
	 * we keep it.  Otherwise we replace it.
	 */
525 526
	regs->cs = __USER_CS;

527 528 529
	if (unlikely(regs->ss != __USER_DS))
		force_valid_ss(regs);

530 531 532 533
	return 0;
}
#endif /* CONFIG_X86_32 */

A
Al Viro 已提交
534 535
static int x32_setup_rt_frame(struct ksignal *ksig,
			      compat_sigset_t *set,
536 537 538 539 540 541 542 543
			      struct pt_regs *regs)
{
#ifdef CONFIG_X86_X32_ABI
	struct rt_sigframe_x32 __user *frame;
	void __user *restorer;
	int err = 0;
	void __user *fpstate = NULL;

A
Al Viro 已提交
544
	frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
545 546 547 548

	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
		return -EFAULT;

A
Al Viro 已提交
549 550
	if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
		if (copy_siginfo_to_user32(&frame->info, &ksig->info))
551 552 553 554 555
			return -EFAULT;
	}

	put_user_try {
		/* Create the ucontext.  */
556
		put_user_ex(frame_uc_flags(regs), &frame->uc.uc_flags);
557
		put_user_ex(0, &frame->uc.uc_link);
558
		compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
559 560
		put_user_ex(0, &frame->uc.uc__pad0);

A
Al Viro 已提交
561 562
		if (ksig->ka.sa.sa_flags & SA_RESTORER) {
			restorer = ksig->ka.sa.sa_restorer;
563 564 565 566 567 568 569 570
		} else {
			/* could use a vstub here */
			restorer = NULL;
			err |= -EFAULT;
		}
		put_user_ex(restorer, &frame->pretcode);
	} put_user_catch(err);

571 572 573 574
	err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
				regs, set->sig[0]);
	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));

575 576 577 578 579
	if (err)
		return -EFAULT;

	/* Set up registers for signal handler */
	regs->sp = (unsigned long) frame;
A
Al Viro 已提交
580
	regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
581 582

	/* We use the x32 calling convention here... */
A
Al Viro 已提交
583
	regs->di = ksig->sig;
584 585 586 587 588 589 590 591 592 593 594 595 596
	regs->si = (unsigned long) &frame->info;
	regs->dx = (unsigned long) &frame->uc;

	loadsegment(ds, __USER_DS);
	loadsegment(es, __USER_DS);

	regs->cs = __USER_CS;
	regs->ss = __USER_DS;
#endif	/* CONFIG_X86_X32_ABI */

	return 0;
}

597 598 599
/*
 * Do a signal return; undo the signal stack.
 */
600
#ifdef CONFIG_X86_32
601
asmlinkage unsigned long sys_sigreturn(void)
602
{
603
	struct pt_regs *regs = current_pt_regs();
604 605 606 607 608 609 610 611 612 613 614 615
	struct sigframe __user *frame;
	sigset_t set;

	frame = (struct sigframe __user *)(regs->sp - 8);

	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
		goto badframe;
	if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
		&& __copy_from_user(&set.sig[1], &frame->extramask,
				    sizeof(frame->extramask))))
		goto badframe;

616
	set_current_blocked(&set);
617

618 619 620 621 622
	/*
	 * x86_32 has no uc_flags bits relevant to restore_sigcontext.
	 * Save a few cycles by skipping the __get_user.
	 */
	if (restore_sigcontext(regs, &frame->sc, 0))
623
		goto badframe;
624
	return regs->ax;
625 626

badframe:
627
	signal_fault(regs, frame, "sigreturn");
628 629 630

	return 0;
}
631
#endif /* CONFIG_X86_32 */
632

633
asmlinkage long sys_rt_sigreturn(void)
634
{
635
	struct pt_regs *regs = current_pt_regs();
636 637
	struct rt_sigframe __user *frame;
	sigset_t set;
638
	unsigned long uc_flags;
639 640 641 642 643 644

	frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
		goto badframe;
	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
		goto badframe;
645 646
	if (__get_user(uc_flags, &frame->uc.uc_flags))
		goto badframe;
647

648
	set_current_blocked(&set);
649

650
	if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
651 652
		goto badframe;

653
	if (restore_altstack(&frame->uc.uc_stack))
654 655
		goto badframe;

656
	return regs->ax;
657 658 659 660 661 662

badframe:
	signal_fault(regs, frame, "rt_sigreturn");
	return 0;
}

663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
static inline int is_ia32_compat_frame(void)
{
	return config_enabled(CONFIG_IA32_EMULATION) &&
	       test_thread_flag(TIF_IA32);
}

static inline int is_ia32_frame(void)
{
	return config_enabled(CONFIG_X86_32) || is_ia32_compat_frame();
}

static inline int is_x32_frame(void)
{
	return config_enabled(CONFIG_X86_X32_ABI) && test_thread_flag(TIF_X32);
}

679
static int
A
Al Viro 已提交
680
setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
681
{
682
	int usig = ksig->sig;
A
Al Viro 已提交
683
	sigset_t *set = sigmask_to_save();
684
	compat_sigset_t *cset = (compat_sigset_t *) set;
685 686

	/* Set up the stack frame */
687
	if (is_ia32_frame()) {
A
Al Viro 已提交
688 689
		if (ksig->ka.sa.sa_flags & SA_SIGINFO)
			return ia32_setup_rt_frame(usig, ksig, cset, regs);
690
		else
A
Al Viro 已提交
691
			return ia32_setup_frame(usig, ksig, cset, regs);
692
	} else if (is_x32_frame()) {
A
Al Viro 已提交
693
		return x32_setup_rt_frame(ksig, cset, regs);
694
	} else {
A
Al Viro 已提交
695
		return __setup_rt_frame(ksig->sig, ksig, set, regs);
696
	}
697 698
}

699
static void
A
Al Viro 已提交
700
handle_signal(struct ksignal *ksig, struct pt_regs *regs)
L
Linus Torvalds 已提交
701
{
702
	bool stepping, failed;
703
	struct fpu *fpu = &current->thread.fpu;
704

705 706 707
	if (v8086_mode(regs))
		save_v86_state((struct kernel_vm86_regs *) regs, VM86_SIGNAL);

L
Linus Torvalds 已提交
708
	/* Are we from a system call? */
709
	if (syscall_get_nr(current, regs) >= 0) {
L
Linus Torvalds 已提交
710
		/* If so, check system call restarting.. */
711
		switch (syscall_get_error(current, regs)) {
712 713 714 715 716 717
		case -ERESTART_RESTARTBLOCK:
		case -ERESTARTNOHAND:
			regs->ax = -EINTR;
			break;

		case -ERESTARTSYS:
A
Al Viro 已提交
718
			if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
719
				regs->ax = -EINTR;
L
Linus Torvalds 已提交
720
				break;
721 722 723 724 725 726
			}
		/* fallthrough */
		case -ERESTARTNOINTR:
			regs->ax = regs->orig_ax;
			regs->ip -= 2;
			break;
L
Linus Torvalds 已提交
727 728 729 730
		}
	}

	/*
731 732 733
	 * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now
	 * so that register information in the sigcontext is correct and
	 * then notify the tracer before entering the signal handler.
L
Linus Torvalds 已提交
734
	 */
735 736 737
	stepping = test_thread_flag(TIF_SINGLESTEP);
	if (stepping)
		user_disable_single_step(current);
L
Linus Torvalds 已提交
738

A
Al Viro 已提交
739 740 741 742
	failed = (setup_rt_frame(ksig, regs) < 0);
	if (!failed) {
		/*
		 * Clear the direction flag as per the ABI for function entry.
743
		 *
744 745 746
		 * Clear RF when entering the signal handler, because
		 * it might disable possible debug exception from the
		 * signal handler.
747
		 *
748 749
		 * Clear TF for the case when it wasn't set by debugger to
		 * avoid the recursive send_sigtrap() in SIGTRAP handler.
A
Al Viro 已提交
750
		 */
751
		regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
752 753 754
		/*
		 * Ensure the signal handler starts with the new fpu state.
		 */
755
		if (fpu->fpstate_active)
756
			fpu__clear(fpu);
757
	}
758
	signal_setup_done(failed, ksig, stepping);
L
Linus Torvalds 已提交
759 760
}

761 762
static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs)
{
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
	/*
	 * This function is fundamentally broken as currently
	 * implemented.
	 *
	 * The idea is that we want to trigger a call to the
	 * restart_block() syscall and that we want in_ia32_syscall(),
	 * in_x32_syscall(), etc. to match whatever they were in the
	 * syscall being restarted.  We assume that the syscall
	 * instruction at (regs->ip - 2) matches whatever syscall
	 * instruction we used to enter in the first place.
	 *
	 * The problem is that we can get here when ptrace pokes
	 * syscall-like values into regs even if we're not in a syscall
	 * at all.
	 *
	 * For now, we maintain historical behavior and guess based on
	 * stored state.  We could do better by saving the actual
	 * syscall arch in restart_block or (with caveats on x32) by
	 * checking if regs->ip points to 'int $0x80'.  The current
	 * behavior is incorrect if a tracer has a different bitness
	 * than the tracee.
	 */
#ifdef CONFIG_IA32_EMULATION
	if (current_thread_info()->status & (TS_COMPAT|TS_I386_REGS_POKED))
787 788 789 790 791
		return __NR_ia32_restart_syscall;
#endif
#ifdef CONFIG_X86_X32_ABI
	return __NR_restart_syscall | (regs->orig_ax & __X32_SYSCALL_BIT);
#else
792
	return __NR_restart_syscall;
793
#endif
794
}
795

L
Linus Torvalds 已提交
796 797 798 799 800
/*
 * Note that 'init' is a special process: it doesn't get signals it doesn't
 * want to handle. Thus you cannot kill init even with a SIGKILL even by
 * mistake.
 */
801
void do_signal(struct pt_regs *regs)
L
Linus Torvalds 已提交
802
{
A
Al Viro 已提交
803
	struct ksignal ksig;
L
Linus Torvalds 已提交
804

A
Al Viro 已提交
805
	if (get_signal(&ksig)) {
806
		/* Whee! Actually deliver the signal.  */
A
Al Viro 已提交
807
		handle_signal(&ksig, regs);
808
		return;
L
Linus Torvalds 已提交
809 810 811
	}

	/* Did we come from a system call? */
812
	if (syscall_get_nr(current, regs) >= 0) {
L
Linus Torvalds 已提交
813
		/* Restart the system call - no handlers present */
814
		switch (syscall_get_error(current, regs)) {
815 816 817
		case -ERESTARTNOHAND:
		case -ERESTARTSYS:
		case -ERESTARTNOINTR:
818 819
			regs->ax = regs->orig_ax;
			regs->ip -= 2;
820 821 822
			break;

		case -ERESTART_RESTARTBLOCK:
823
			regs->ax = get_nr_restart_syscall(regs);
824
			regs->ip -= 2;
825
			break;
L
Linus Torvalds 已提交
826 827
		}
	}
828

829 830 831 832
	/*
	 * If there's no signal to deliver, we just put the saved sigmask
	 * back.
	 */
A
Al Viro 已提交
833
	restore_saved_sigmask();
L
Linus Torvalds 已提交
834 835
}

836 837 838 839 840
void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
{
	struct task_struct *me = current;

	if (show_unhandled_signals && printk_ratelimit()) {
841
		printk("%s"
842
		       "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
843
		       task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
844 845 846
		       me->comm, me->pid, where, frame,
		       regs->ip, regs->sp, regs->orig_ax);
		print_vma_addr(" in ", regs->ip);
847
		pr_cont("\n");
848 849 850 851
	}

	force_sig(SIGSEGV, me);
}
852 853

#ifdef CONFIG_X86_X32_ABI
854
asmlinkage long sys32_x32_rt_sigreturn(void)
855
{
856
	struct pt_regs *regs = current_pt_regs();
857 858
	struct rt_sigframe_x32 __user *frame;
	sigset_t set;
859
	unsigned long uc_flags;
860 861 862 863 864 865 866

	frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);

	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
		goto badframe;
	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
		goto badframe;
867 868
	if (__get_user(uc_flags, &frame->uc.uc_flags))
		goto badframe;
869 870 871

	set_current_blocked(&set);

872
	if (restore_sigcontext(regs, &frame->uc.uc_mcontext, uc_flags))
873 874
		goto badframe;

A
Al Viro 已提交
875
	if (compat_restore_altstack(&frame->uc.uc_stack))
876 877
		goto badframe;

878
	return regs->ax;
879 880 881 882 883 884

badframe:
	signal_fault(regs, frame, "x32 rt_sigreturn");
	return 0;
}
#endif