signal_32.c 17.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6
/*
 *  Copyright (C) 1991, 1992  Linus Torvalds
 *
 *  1997-11-28  Modified for POSIX.1b signals by Richard Henderson
 *  2000-06-20  Pentium III FXSR, SSE support by Gareth Hughes
 */
7
#include <linux/list.h>
L
Linus Torvalds 已提交
8

9 10 11
#include <linux/personality.h>
#include <linux/binfmts.h>
#include <linux/suspend.h>
L
Linus Torvalds 已提交
12
#include <linux/kernel.h>
13
#include <linux/ptrace.h>
L
Linus Torvalds 已提交
14
#include <linux/signal.h>
15 16
#include <linux/stddef.h>
#include <linux/unistd.h>
L
Linus Torvalds 已提交
17
#include <linux/errno.h>
18
#include <linux/sched.h>
L
Linus Torvalds 已提交
19 20
#include <linux/wait.h>
#include <linux/elf.h>
21 22 23
#include <linux/smp.h>
#include <linux/mm.h>

L
Linus Torvalds 已提交
24 25 26 27
#include <asm/processor.h>
#include <asm/ucontext.h>
#include <asm/uaccess.h>
#include <asm/i387.h>
R
Roland McGrath 已提交
28
#include <asm/vdso.h>
29

30
#include "sigframe.h"
L
Linus Torvalds 已提交
31 32 33

#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))

34 35 36 37 38 39 40 41 42 43 44
#define __FIX_EFLAGS	(X86_EFLAGS_AC | X86_EFLAGS_OF | \
			 X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
			 X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
			 X86_EFLAGS_CF)

#ifdef CONFIG_X86_32
# define FIX_EFLAGS	(__FIX_EFLAGS | X86_EFLAGS_RF)
#else
# define FIX_EFLAGS	__FIX_EFLAGS
#endif

L
Linus Torvalds 已提交
45 46 47 48 49 50 51 52
/*
 * Atomically swap in the new signal mask, and wait for a signal.
 */
asmlinkage int
sys_sigsuspend(int history0, int history1, old_sigset_t mask)
{
	mask &= _BLOCKABLE;
	spin_lock_irq(&current->sighand->siglock);
53
	current->saved_sigmask = current->blocked;
L
Linus Torvalds 已提交
54 55 56 57
	siginitset(&current->blocked, mask);
	recalc_sigpending();
	spin_unlock_irq(&current->sighand->siglock);

58 59
	current->state = TASK_INTERRUPTIBLE;
	schedule();
R
Roland McGrath 已提交
60
	set_restore_sigmask();
61

62
	return -ERESTARTNOHAND;
L
Linus Torvalds 已提交
63 64
}

65
asmlinkage int
L
Linus Torvalds 已提交
66 67 68 69 70 71 72 73
sys_sigaction(int sig, const struct old_sigaction __user *act,
	      struct old_sigaction __user *oact)
{
	struct k_sigaction new_ka, old_ka;
	int ret;

	if (act) {
		old_sigset_t mask;
74

L
Linus Torvalds 已提交
75 76 77 78
		if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
		    __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
		    __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
			return -EFAULT;
79

L
Linus Torvalds 已提交
80 81 82 83 84 85 86 87 88 89 90 91
		__get_user(new_ka.sa.sa_flags, &act->sa_flags);
		__get_user(mask, &act->sa_mask);
		siginitset(&new_ka.sa.sa_mask, mask);
	}

	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);

	if (!ret && oact) {
		if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
		    __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
		    __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
			return -EFAULT;
92

L
Linus Torvalds 已提交
93 94 95 96 97 98 99
		__put_user(old_ka.sa.sa_flags, &oact->sa_flags);
		__put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
	}

	return ret;
}

100
asmlinkage int sys_sigaltstack(unsigned long bx)
L
Linus Torvalds 已提交
101
{
102 103 104 105
	/*
	 * This is needed to make gcc realize it doesn't own the
	 * "struct pt_regs"
	 */
106 107 108
	struct pt_regs *regs = (struct pt_regs *)&bx;
	const stack_t __user *uss = (const stack_t __user *)bx;
	stack_t __user *uoss = (stack_t __user *)regs->cx;
L
Linus Torvalds 已提交
109

110
	return do_sigaltstack(uss, uoss, regs->sp);
L
Linus Torvalds 已提交
111 112 113 114 115 116 117
}


/*
 * Do a signal return; undo the signal stack.
 */
static int
118 119
restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
		   unsigned long *pax)
L
Linus Torvalds 已提交
120 121 122 123 124 125
{
	unsigned int err = 0;

	/* Always make any pending restarted system calls return -EINTR */
	current_thread_info()->restart_block.fn = do_no_restart_syscall;

126
#define COPY(x)		err |= __get_user(regs->x, &sc->x)
L
Linus Torvalds 已提交
127 128 129 130

#define COPY_SEG(seg)							\
	{ unsigned short tmp;						\
	  err |= __get_user(tmp, &sc->seg);				\
131
	  regs->seg = tmp; }
L
Linus Torvalds 已提交
132 133 134 135

#define COPY_SEG_STRICT(seg)						\
	{ unsigned short tmp;						\
	  err |= __get_user(tmp, &sc->seg);				\
136
	  regs->seg = tmp|3; }
L
Linus Torvalds 已提交
137 138 139 140

#define GET_SEG(seg)							\
	{ unsigned short tmp;						\
	  err |= __get_user(tmp, &sc->seg);				\
141
	  loadsegment(seg, tmp); }
L
Linus Torvalds 已提交
142

143 144
	GET_SEG(gs);
	COPY_SEG(fs);
L
Linus Torvalds 已提交
145 146
	COPY_SEG(es);
	COPY_SEG(ds);
147 148
	COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
	COPY(dx); COPY(cx); COPY(ip);
L
Linus Torvalds 已提交
149 150
	COPY_SEG_STRICT(cs);
	COPY_SEG_STRICT(ss);
151

L
Linus Torvalds 已提交
152 153
	{
		unsigned int tmpflags;
154

155
		err |= __get_user(tmpflags, &sc->flags);
156 157
		regs->flags = (regs->flags & ~FIX_EFLAGS) |
						(tmpflags & FIX_EFLAGS);
158
		regs->orig_ax = -1;		/* disable syscall checks */
L
Linus Torvalds 已提交
159 160 161
	}

	{
162 163
		struct _fpstate __user *buf;

L
Linus Torvalds 已提交
164 165 166 167 168 169 170
		err |= __get_user(buf, &sc->fpstate);
		if (buf) {
			if (!access_ok(VERIFY_READ, buf, sizeof(*buf)))
				goto badframe;
			err |= restore_i387(buf);
		} else {
			struct task_struct *me = current;
171

L
Linus Torvalds 已提交
172 173 174 175 176 177 178
			if (used_math()) {
				clear_fpu(me);
				clear_used_math();
			}
		}
	}

179
	err |= __get_user(*pax, &sc->ax);
L
Linus Torvalds 已提交
180 181 182 183 184 185
	return err;

badframe:
	return 1;
}

186
asmlinkage unsigned long sys_sigreturn(unsigned long __unused)
L
Linus Torvalds 已提交
187
{
188 189
	struct sigframe __user *frame;
	struct pt_regs *regs;
190
	unsigned long ax;
191 192 193 194
	sigset_t set;

	regs = (struct pt_regs *) &__unused;
	frame = (struct sigframe __user *)(regs->sp - 8);
L
Linus Torvalds 已提交
195 196 197

	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
		goto badframe;
198
	if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
L
Linus Torvalds 已提交
199 200 201 202 203 204 205 206 207
		&& __copy_from_user(&set.sig[1], &frame->extramask,
				    sizeof(frame->extramask))))
		goto badframe;

	sigdelsetmask(&set, ~_BLOCKABLE);
	spin_lock_irq(&current->sighand->siglock);
	current->blocked = set;
	recalc_sigpending();
	spin_unlock_irq(&current->sighand->siglock);
208

209
	if (restore_sigcontext(regs, &frame->sc, &ax))
L
Linus Torvalds 已提交
210
		goto badframe;
211
	return ax;
L
Linus Torvalds 已提交
212 213

badframe:
214
	if (show_unhandled_signals && printk_ratelimit()) {
215
		printk("%s%s[%d] bad frame in sigreturn frame:"
216
			"%p ip:%lx sp:%lx oeax:%lx",
217
		    task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
218 219
		    current->comm, task_pid_nr(current), frame, regs->ip,
		    regs->sp, regs->orig_ax);
220
		print_vma_addr(" in ", regs->ip);
221
		printk(KERN_CONT "\n");
222
	}
223

L
Linus Torvalds 已提交
224
	force_sig(SIGSEGV, current);
225

L
Linus Torvalds 已提交
226
	return 0;
227
}
L
Linus Torvalds 已提交
228 229 230

asmlinkage int sys_rt_sigreturn(unsigned long __unused)
{
231 232
	struct pt_regs *regs = (struct pt_regs *)&__unused;
	struct rt_sigframe __user *frame;
233
	unsigned long ax;
L
Linus Torvalds 已提交
234 235
	sigset_t set;

236
	frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
L
Linus Torvalds 已提交
237 238 239 240 241 242 243 244 245 246
	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
		goto badframe;
	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
		goto badframe;

	sigdelsetmask(&set, ~_BLOCKABLE);
	spin_lock_irq(&current->sighand->siglock);
	current->blocked = set;
	recalc_sigpending();
	spin_unlock_irq(&current->sighand->siglock);
247

248
	if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
L
Linus Torvalds 已提交
249 250
		goto badframe;

251
	if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
L
Linus Torvalds 已提交
252 253
		goto badframe;

254
	return ax;
L
Linus Torvalds 已提交
255 256 257 258

badframe:
	force_sig(SIGSEGV, current);
	return 0;
259
}
L
Linus Torvalds 已提交
260 261 262 263 264 265 266 267 268 269

/*
 * Set up a signal frame.
 */
static int
setup_sigcontext(struct sigcontext __user *sc, struct _fpstate __user *fpstate,
		 struct pt_regs *regs, unsigned long mask)
{
	int tmp, err = 0;

270
	err |= __put_user(regs->fs, (unsigned int __user *)&sc->fs);
271 272
	savesegment(gs, tmp);
	err |= __put_user(tmp, (unsigned int __user *)&sc->gs);
L
Linus Torvalds 已提交
273

274 275
	err |= __put_user(regs->es, (unsigned int __user *)&sc->es);
	err |= __put_user(regs->ds, (unsigned int __user *)&sc->ds);
276 277 278 279 280 281 282 283
	err |= __put_user(regs->di, &sc->di);
	err |= __put_user(regs->si, &sc->si);
	err |= __put_user(regs->bp, &sc->bp);
	err |= __put_user(regs->sp, &sc->sp);
	err |= __put_user(regs->bx, &sc->bx);
	err |= __put_user(regs->dx, &sc->dx);
	err |= __put_user(regs->cx, &sc->cx);
	err |= __put_user(regs->ax, &sc->ax);
L
Linus Torvalds 已提交
284 285
	err |= __put_user(current->thread.trap_no, &sc->trapno);
	err |= __put_user(current->thread.error_code, &sc->err);
286
	err |= __put_user(regs->ip, &sc->ip);
287
	err |= __put_user(regs->cs, (unsigned int __user *)&sc->cs);
288 289
	err |= __put_user(regs->flags, &sc->flags);
	err |= __put_user(regs->sp, &sc->sp_at_signal);
290
	err |= __put_user(regs->ss, (unsigned int __user *)&sc->ss);
L
Linus Torvalds 已提交
291 292 293

	tmp = save_i387(fpstate);
	if (tmp < 0)
294
		err = 1;
L
Linus Torvalds 已提交
295
	else
296
		err |= __put_user(tmp ? fpstate : NULL, &sc->fpstate);
L
Linus Torvalds 已提交
297 298 299 300 301 302 303 304 305 306 307 308

	/* non-iBCS2 extensions.. */
	err |= __put_user(mask, &sc->oldmask);
	err |= __put_user(current->thread.cr2, &sc->cr2);

	return err;
}

/*
 * Determine which stack to use..
 */
static inline void __user *
309
get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
L
Linus Torvalds 已提交
310
{
311
	unsigned long sp;
L
Linus Torvalds 已提交
312 313

	/* Default to using normal stack */
314
	sp = regs->sp;
L
Linus Torvalds 已提交
315

316 317 318 319
	/*
	 * 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.
	 */
320
	if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
321 322
		return (void __user *) -1L;

L
Linus Torvalds 已提交
323 324
	/* This is the X/Open sanctioned signal stack switching.  */
	if (ka->sa.sa_flags & SA_ONSTACK) {
325 326
		if (sas_ss_flags(sp) == 0)
			sp = current->sas_ss_sp + current->sas_ss_size;
327 328 329 330 331 332
	} else {
		/* This is the legacy signal stack switching. */
		if ((regs->ss & 0xffff) != __USER_DS &&
			!(ka->sa.sa_flags & SA_RESTORER) &&
				ka->sa.sa_restorer)
			sp = (unsigned long) ka->sa.sa_restorer;
L
Linus Torvalds 已提交
333 334
	}

335
	sp -= frame_size;
336 337 338 339
	/*
	 * Align the stack pointer according to the i386 ABI,
	 * i.e. so that on function entry ((sp + 4) & 15) == 0.
	 */
340
	sp = ((sp + 4) & -16ul) - 4;
341

342
	return (void __user *) sp;
L
Linus Torvalds 已提交
343 344
}

345 346 347
static int
setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
	    struct pt_regs *regs)
L
Linus Torvalds 已提交
348 349
{
	struct sigframe __user *frame;
350
	void __user *restorer;
L
Linus Torvalds 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
	int err = 0;
	int usig;

	frame = get_sigframe(ka, regs, sizeof(*frame));

	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
		goto give_sigsegv;

	usig = current_thread_info()->exec_domain
		&& current_thread_info()->exec_domain->signal_invmap
		&& sig < 32
		? current_thread_info()->exec_domain->signal_invmap[sig]
		: sig;

	err = __put_user(usig, &frame->sig);
	if (err)
		goto give_sigsegv;

	err = setup_sigcontext(&frame->sc, &frame->fpstate, regs, set->sig[0]);
	if (err)
		goto give_sigsegv;

	if (_NSIG_WORDS > 1) {
		err = __copy_to_user(&frame->extramask, &set->sig[1],
				      sizeof(frame->extramask));
		if (err)
			goto give_sigsegv;
	}

380
	if (current->mm->context.vdso)
R
Roland McGrath 已提交
381
		restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
382
	else
J
Jan Engelhardt 已提交
383
		restorer = &frame->retcode;
L
Linus Torvalds 已提交
384 385 386 387 388
	if (ka->sa.sa_flags & SA_RESTORER)
		restorer = ka->sa.sa_restorer;

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

L
Linus Torvalds 已提交
390
	/*
391
	 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
L
Linus Torvalds 已提交
392 393 394 395 396 397 398 399 400 401 402 403 404
	 *
	 * 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.
	 */
	err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
	err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
	err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));

	if (err)
		goto give_sigsegv;

	/* Set up registers for signal handler */
405 406 407
	regs->sp = (unsigned long)frame;
	regs->ip = (unsigned long)ka->sa.sa_handler;
	regs->ax = (unsigned long)sig;
408 409
	regs->dx = 0;
	regs->cx = 0;
L
Linus Torvalds 已提交
410

411 412 413 414
	regs->ds = __USER_DS;
	regs->es = __USER_DS;
	regs->ss = __USER_DS;
	regs->cs = __USER_CS;
L
Linus Torvalds 已提交
415

416
	return 0;
L
Linus Torvalds 已提交
417 418 419

give_sigsegv:
	force_sigsegv(sig, current);
420
	return -EFAULT;
L
Linus Torvalds 已提交
421 422
}

423
static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
424
			  sigset_t *set, struct pt_regs *regs)
L
Linus Torvalds 已提交
425 426
{
	struct rt_sigframe __user *frame;
427
	void __user *restorer;
L
Linus Torvalds 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
	int err = 0;
	int usig;

	frame = get_sigframe(ka, regs, sizeof(*frame));

	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
		goto give_sigsegv;

	usig = current_thread_info()->exec_domain
		&& current_thread_info()->exec_domain->signal_invmap
		&& sig < 32
		? current_thread_info()->exec_domain->signal_invmap[sig]
		: sig;

	err |= __put_user(usig, &frame->sig);
	err |= __put_user(&frame->info, &frame->pinfo);
	err |= __put_user(&frame->uc, &frame->puc);
	err |= copy_siginfo_to_user(&frame->info, info);
	if (err)
		goto give_sigsegv;

	/* Create the ucontext.  */
	err |= __put_user(0, &frame->uc.uc_flags);
	err |= __put_user(0, &frame->uc.uc_link);
	err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
453
	err |= __put_user(sas_ss_flags(regs->sp),
L
Linus Torvalds 已提交
454 455 456
			  &frame->uc.uc_stack.ss_flags);
	err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
	err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->fpstate,
457
				regs, set->sig[0]);
L
Linus Torvalds 已提交
458 459 460 461 462
	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
	if (err)
		goto give_sigsegv;

	/* Set up to return from userspace.  */
R
Roland McGrath 已提交
463
	restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
L
Linus Torvalds 已提交
464 465 466
	if (ka->sa.sa_flags & SA_RESTORER)
		restorer = ka->sa.sa_restorer;
	err |= __put_user(restorer, &frame->pretcode);
467

L
Linus Torvalds 已提交
468
	/*
469
	 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
L
Linus Torvalds 已提交
470 471 472 473 474 475 476 477 478 479 480 481 482
	 *
	 * 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.
	 */
	err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
	err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
	err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));

	if (err)
		goto give_sigsegv;

	/* Set up registers for signal handler */
483 484 485 486 487
	regs->sp = (unsigned long)frame;
	regs->ip = (unsigned long)ka->sa.sa_handler;
	regs->ax = (unsigned long)usig;
	regs->dx = (unsigned long)&frame->info;
	regs->cx = (unsigned long)&frame->uc;
L
Linus Torvalds 已提交
488

489 490 491 492
	regs->ds = __USER_DS;
	regs->es = __USER_DS;
	regs->ss = __USER_DS;
	regs->cs = __USER_CS;
L
Linus Torvalds 已提交
493

494
	return 0;
L
Linus Torvalds 已提交
495 496 497

give_sigsegv:
	force_sigsegv(sig, current);
498
	return -EFAULT;
L
Linus Torvalds 已提交
499 500 501
}

/*
502 503
 * OK, we're invoking a handler:
 */
504
static int
L
Linus Torvalds 已提交
505
handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
506
	      sigset_t *oldset, struct pt_regs *regs)
L
Linus Torvalds 已提交
507
{
508 509
	int ret;

L
Linus Torvalds 已提交
510
	/* Are we from a system call? */
511
	if ((long)regs->orig_ax >= 0) {
L
Linus Torvalds 已提交
512
		/* If so, check system call restarting.. */
513
		switch (regs->ax) {
514 515 516 517 518 519 520
		case -ERESTART_RESTARTBLOCK:
		case -ERESTARTNOHAND:
			regs->ax = -EINTR;
			break;

		case -ERESTARTSYS:
			if (!(ka->sa.sa_flags & SA_RESTART)) {
521
				regs->ax = -EINTR;
L
Linus Torvalds 已提交
522
				break;
523 524 525 526 527 528
			}
		/* fallthrough */
		case -ERESTARTNOINTR:
			regs->ax = regs->orig_ax;
			regs->ip -= 2;
			break;
L
Linus Torvalds 已提交
529 530 531 532
		}
	}

	/*
R
Roland McGrath 已提交
533 534
	 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
	 * flag so that register information in the sigcontext is correct.
L
Linus Torvalds 已提交
535
	 */
536
	if (unlikely(regs->flags & X86_EFLAGS_TF) &&
R
Roland McGrath 已提交
537
	    likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
538
		regs->flags &= ~X86_EFLAGS_TF;
L
Linus Torvalds 已提交
539 540 541

	/* Set up the stack frame */
	if (ka->sa.sa_flags & SA_SIGINFO)
542
		ret = setup_rt_frame(sig, ka, info, oldset, regs);
L
Linus Torvalds 已提交
543
	else
544
		ret = setup_frame(sig, ka, oldset, regs);
L
Linus Torvalds 已提交
545

546 547
	if (ret)
		return ret;
548

549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
	/*
	 * Clear the direction flag as per the ABI for function entry.
	 */
	regs->flags &= ~X86_EFLAGS_DF;

	/*
	 * Clear TF when entering the signal handler, but
	 * notify any tracer that was single-stepping it.
	 * The tracer may want to single-step inside the
	 * handler too.
	 */
	regs->flags &= ~X86_EFLAGS_TF;
	if (test_thread_flag(TIF_SINGLESTEP))
		ptrace_notify(SIGTRAP);

564 565 566 567 568 569 570 571
	spin_lock_irq(&current->sighand->siglock);
	sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
	if (!(ka->sa.sa_flags & SA_NODEFER))
		sigaddset(&current->blocked, sig);
	recalc_sigpending();
	spin_unlock_irq(&current->sighand->siglock);

	return 0;
L
Linus Torvalds 已提交
572 573 574 575 576 577 578
}

/*
 * 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.
 */
579
static void do_signal(struct pt_regs *regs)
L
Linus Torvalds 已提交
580
{
581
	struct k_sigaction ka;
L
Linus Torvalds 已提交
582 583
	siginfo_t info;
	int signr;
584
	sigset_t *oldset;
L
Linus Torvalds 已提交
585 586

	/*
587 588 589 590 591
	 * We want the common case to go fast, which is why we may in certain
	 * cases get here from kernel mode. Just return without doing anything
	 * if so.
	 * X86_32: vm86 regs switched out by assembly code before reaching
	 * here, so testing against kernel CS suffices.
L
Linus Torvalds 已提交
592
	 */
593
	if (!user_mode(regs))
594
		return;
L
Linus Torvalds 已提交
595

R
Roland McGrath 已提交
596
	if (current_thread_info()->status & TS_RESTORE_SIGMASK)
597 598
		oldset = &current->saved_sigmask;
	else
L
Linus Torvalds 已提交
599 600 601 602
		oldset = &current->blocked;

	signr = get_signal_to_deliver(&info, &ka, regs, NULL);
	if (signr > 0) {
603 604
		/*
		 * Re-enable any watchpoints before delivering the
L
Linus Torvalds 已提交
605 606 607 608
		 * signal to user space. The processor register will
		 * have been cleared if the watchpoint triggered
		 * inside the kernel.
		 */
609
		if (current->thread.debugreg7)
610
			set_debugreg(current->thread.debugreg7, 7);
L
Linus Torvalds 已提交
611

612
		/* Whee! Actually deliver the signal.  */
613
		if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
614
			/*
R
Roland McGrath 已提交
615
			 * A signal was successfully delivered; the saved
616 617
			 * sigmask will have been stored in the signal frame,
			 * and will be restored by sigreturn, so we can simply
R
Roland McGrath 已提交
618
			 * clear the TS_RESTORE_SIGMASK flag.
619
			 */
R
Roland McGrath 已提交
620
			current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
621 622
		}
		return;
L
Linus Torvalds 已提交
623 624 625
	}

	/* Did we come from a system call? */
626
	if ((long)regs->orig_ax >= 0) {
L
Linus Torvalds 已提交
627
		/* Restart the system call - no handlers present */
628
		switch (regs->ax) {
629 630 631
		case -ERESTARTNOHAND:
		case -ERESTARTSYS:
		case -ERESTARTNOINTR:
632 633
			regs->ax = regs->orig_ax;
			regs->ip -= 2;
634 635 636
			break;

		case -ERESTART_RESTARTBLOCK:
637 638
			regs->ax = __NR_restart_syscall;
			regs->ip -= 2;
639
			break;
L
Linus Torvalds 已提交
640 641
		}
	}
642

643 644 645 646
	/*
	 * If there's no signal to deliver, we just put the saved sigmask
	 * back.
	 */
R
Roland McGrath 已提交
647 648
	if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
		current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
649 650
		sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
	}
L
Linus Torvalds 已提交
651 652 653 654
}

/*
 * notification of userspace execution resumption
655
 * - triggered by the TIF_WORK_MASK flags
L
Linus Torvalds 已提交
656
 */
657 658
void
do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
L
Linus Torvalds 已提交
659 660 661
{
	/* Pending single-step? */
	if (thread_info_flags & _TIF_SINGLESTEP) {
662
		regs->flags |= X86_EFLAGS_TF;
L
Linus Torvalds 已提交
663 664
		clear_thread_flag(TIF_SINGLESTEP);
	}
665

L
Linus Torvalds 已提交
666
	/* deal with pending signal delivery */
R
Roland McGrath 已提交
667
	if (thread_info_flags & _TIF_SIGPENDING)
668
		do_signal(regs);
P
Peter Zijlstra 已提交
669 670 671

	if (thread_info_flags & _TIF_HRTICK_RESCHED)
		hrtick_resched();
672

L
Linus Torvalds 已提交
673 674
	clear_thread_flag(TIF_IRET);
}