xref: /openbmc/linux/arch/x86/kernel/signal.c (revision 78c99ba1)
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 #include <linux/sched.h>
10 #include <linux/mm.h>
11 #include <linux/smp.h>
12 #include <linux/kernel.h>
13 #include <linux/signal.h>
14 #include <linux/errno.h>
15 #include <linux/wait.h>
16 #include <linux/ptrace.h>
17 #include <linux/tracehook.h>
18 #include <linux/unistd.h>
19 #include <linux/stddef.h>
20 #include <linux/personality.h>
21 #include <linux/uaccess.h>
22 
23 #include <asm/processor.h>
24 #include <asm/ucontext.h>
25 #include <asm/i387.h>
26 #include <asm/vdso.h>
27 
28 #ifdef CONFIG_X86_64
29 #include <asm/proto.h>
30 #include <asm/ia32_unistd.h>
31 #include <asm/mce.h>
32 #endif /* CONFIG_X86_64 */
33 
34 #include <asm/syscall.h>
35 #include <asm/syscalls.h>
36 
37 #include <asm/sigframe.h>
38 
39 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
40 
41 #define __FIX_EFLAGS	(X86_EFLAGS_AC | X86_EFLAGS_OF | \
42 			 X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
43 			 X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
44 			 X86_EFLAGS_CF)
45 
46 #ifdef CONFIG_X86_32
47 # define FIX_EFLAGS	(__FIX_EFLAGS | X86_EFLAGS_RF)
48 #else
49 # define FIX_EFLAGS	__FIX_EFLAGS
50 #endif
51 
52 #define COPY(x)			do {			\
53 	get_user_ex(regs->x, &sc->x);			\
54 } while (0)
55 
56 #define GET_SEG(seg)		({			\
57 	unsigned short tmp;				\
58 	get_user_ex(tmp, &sc->seg);			\
59 	tmp;						\
60 })
61 
62 #define COPY_SEG(seg)		do {			\
63 	regs->seg = GET_SEG(seg);			\
64 } while (0)
65 
66 #define COPY_SEG_CPL3(seg)	do {			\
67 	regs->seg = GET_SEG(seg) | 3;			\
68 } while (0)
69 
70 static int
71 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
72 		   unsigned long *pax)
73 {
74 	void __user *buf;
75 	unsigned int tmpflags;
76 	unsigned int err = 0;
77 
78 	/* Always make any pending restarted system calls return -EINTR */
79 	current_thread_info()->restart_block.fn = do_no_restart_syscall;
80 
81 	get_user_try {
82 
83 #ifdef CONFIG_X86_32
84 		set_user_gs(regs, GET_SEG(gs));
85 		COPY_SEG(fs);
86 		COPY_SEG(es);
87 		COPY_SEG(ds);
88 #endif /* CONFIG_X86_32 */
89 
90 		COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
91 		COPY(dx); COPY(cx); COPY(ip);
92 
93 #ifdef CONFIG_X86_64
94 		COPY(r8);
95 		COPY(r9);
96 		COPY(r10);
97 		COPY(r11);
98 		COPY(r12);
99 		COPY(r13);
100 		COPY(r14);
101 		COPY(r15);
102 #endif /* CONFIG_X86_64 */
103 
104 #ifdef CONFIG_X86_32
105 		COPY_SEG_CPL3(cs);
106 		COPY_SEG_CPL3(ss);
107 #else /* !CONFIG_X86_32 */
108 		/* Kernel saves and restores only the CS segment register on signals,
109 		 * which is the bare minimum needed to allow mixed 32/64-bit code.
110 		 * App's signal handler can save/restore other segments if needed. */
111 		COPY_SEG_CPL3(cs);
112 #endif /* CONFIG_X86_32 */
113 
114 		get_user_ex(tmpflags, &sc->flags);
115 		regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
116 		regs->orig_ax = -1;		/* disable syscall checks */
117 
118 		get_user_ex(buf, &sc->fpstate);
119 		err |= restore_i387_xstate(buf);
120 
121 		get_user_ex(*pax, &sc->ax);
122 	} get_user_catch(err);
123 
124 	return err;
125 }
126 
127 static int
128 setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
129 		 struct pt_regs *regs, unsigned long mask)
130 {
131 	int err = 0;
132 
133 	put_user_try {
134 
135 #ifdef CONFIG_X86_32
136 		put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
137 		put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
138 		put_user_ex(regs->es, (unsigned int __user *)&sc->es);
139 		put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
140 #endif /* CONFIG_X86_32 */
141 
142 		put_user_ex(regs->di, &sc->di);
143 		put_user_ex(regs->si, &sc->si);
144 		put_user_ex(regs->bp, &sc->bp);
145 		put_user_ex(regs->sp, &sc->sp);
146 		put_user_ex(regs->bx, &sc->bx);
147 		put_user_ex(regs->dx, &sc->dx);
148 		put_user_ex(regs->cx, &sc->cx);
149 		put_user_ex(regs->ax, &sc->ax);
150 #ifdef CONFIG_X86_64
151 		put_user_ex(regs->r8, &sc->r8);
152 		put_user_ex(regs->r9, &sc->r9);
153 		put_user_ex(regs->r10, &sc->r10);
154 		put_user_ex(regs->r11, &sc->r11);
155 		put_user_ex(regs->r12, &sc->r12);
156 		put_user_ex(regs->r13, &sc->r13);
157 		put_user_ex(regs->r14, &sc->r14);
158 		put_user_ex(regs->r15, &sc->r15);
159 #endif /* CONFIG_X86_64 */
160 
161 		put_user_ex(current->thread.trap_no, &sc->trapno);
162 		put_user_ex(current->thread.error_code, &sc->err);
163 		put_user_ex(regs->ip, &sc->ip);
164 #ifdef CONFIG_X86_32
165 		put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
166 		put_user_ex(regs->flags, &sc->flags);
167 		put_user_ex(regs->sp, &sc->sp_at_signal);
168 		put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
169 #else /* !CONFIG_X86_32 */
170 		put_user_ex(regs->flags, &sc->flags);
171 		put_user_ex(regs->cs, &sc->cs);
172 		put_user_ex(0, &sc->gs);
173 		put_user_ex(0, &sc->fs);
174 #endif /* CONFIG_X86_32 */
175 
176 		put_user_ex(fpstate, &sc->fpstate);
177 
178 		/* non-iBCS2 extensions.. */
179 		put_user_ex(mask, &sc->oldmask);
180 		put_user_ex(current->thread.cr2, &sc->cr2);
181 	} put_user_catch(err);
182 
183 	return err;
184 }
185 
186 /*
187  * Set up a signal frame.
188  */
189 
190 /*
191  * Determine which stack to use..
192  */
193 static unsigned long align_sigframe(unsigned long sp)
194 {
195 #ifdef CONFIG_X86_32
196 	/*
197 	 * Align the stack pointer according to the i386 ABI,
198 	 * i.e. so that on function entry ((sp + 4) & 15) == 0.
199 	 */
200 	sp = ((sp + 4) & -16ul) - 4;
201 #else /* !CONFIG_X86_32 */
202 	sp = round_down(sp, 16) - 8;
203 #endif
204 	return sp;
205 }
206 
207 static inline void __user *
208 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
209 	     void __user **fpstate)
210 {
211 	/* Default to using normal stack */
212 	unsigned long sp = regs->sp;
213 	int onsigstack = on_sig_stack(sp);
214 
215 #ifdef CONFIG_X86_64
216 	/* redzone */
217 	sp -= 128;
218 #endif /* CONFIG_X86_64 */
219 
220 	if (!onsigstack) {
221 		/* This is the X/Open sanctioned signal stack switching.  */
222 		if (ka->sa.sa_flags & SA_ONSTACK) {
223 			if (current->sas_ss_size)
224 				sp = current->sas_ss_sp + current->sas_ss_size;
225 		} else {
226 #ifdef CONFIG_X86_32
227 			/* This is the legacy signal stack switching. */
228 			if ((regs->ss & 0xffff) != __USER_DS &&
229 				!(ka->sa.sa_flags & SA_RESTORER) &&
230 					ka->sa.sa_restorer)
231 				sp = (unsigned long) ka->sa.sa_restorer;
232 #endif /* CONFIG_X86_32 */
233 		}
234 	}
235 
236 	if (used_math()) {
237 		sp -= sig_xstate_size;
238 #ifdef CONFIG_X86_64
239 		sp = round_down(sp, 64);
240 #endif /* CONFIG_X86_64 */
241 		*fpstate = (void __user *)sp;
242 	}
243 
244 	sp = align_sigframe(sp - frame_size);
245 
246 	/*
247 	 * If we are on the alternate signal stack and would overflow it, don't.
248 	 * Return an always-bogus address instead so we will die with SIGSEGV.
249 	 */
250 	if (onsigstack && !likely(on_sig_stack(sp)))
251 		return (void __user *)-1L;
252 
253 	/* save i387 state */
254 	if (used_math() && save_i387_xstate(*fpstate) < 0)
255 		return (void __user *)-1L;
256 
257 	return (void __user *)sp;
258 }
259 
260 #ifdef CONFIG_X86_32
261 static const struct {
262 	u16 poplmovl;
263 	u32 val;
264 	u16 int80;
265 } __attribute__((packed)) retcode = {
266 	0xb858,		/* popl %eax; movl $..., %eax */
267 	__NR_sigreturn,
268 	0x80cd,		/* int $0x80 */
269 };
270 
271 static const struct {
272 	u8  movl;
273 	u32 val;
274 	u16 int80;
275 	u8  pad;
276 } __attribute__((packed)) rt_retcode = {
277 	0xb8,		/* movl $..., %eax */
278 	__NR_rt_sigreturn,
279 	0x80cd,		/* int $0x80 */
280 	0
281 };
282 
283 static int
284 __setup_frame(int sig, struct k_sigaction *ka, sigset_t *set,
285 	      struct pt_regs *regs)
286 {
287 	struct sigframe __user *frame;
288 	void __user *restorer;
289 	int err = 0;
290 	void __user *fpstate = NULL;
291 
292 	frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
293 
294 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
295 		return -EFAULT;
296 
297 	if (__put_user(sig, &frame->sig))
298 		return -EFAULT;
299 
300 	if (setup_sigcontext(&frame->sc, fpstate, regs, set->sig[0]))
301 		return -EFAULT;
302 
303 	if (_NSIG_WORDS > 1) {
304 		if (__copy_to_user(&frame->extramask, &set->sig[1],
305 				   sizeof(frame->extramask)))
306 			return -EFAULT;
307 	}
308 
309 	if (current->mm->context.vdso)
310 		restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn);
311 	else
312 		restorer = &frame->retcode;
313 	if (ka->sa.sa_flags & SA_RESTORER)
314 		restorer = ka->sa.sa_restorer;
315 
316 	/* Set up to return from userspace.  */
317 	err |= __put_user(restorer, &frame->pretcode);
318 
319 	/*
320 	 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
321 	 *
322 	 * WE DO NOT USE IT ANY MORE! It's only left here for historical
323 	 * reasons and because gdb uses it as a signature to notice
324 	 * signal handler stack frames.
325 	 */
326 	err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
327 
328 	if (err)
329 		return -EFAULT;
330 
331 	/* Set up registers for signal handler */
332 	regs->sp = (unsigned long)frame;
333 	regs->ip = (unsigned long)ka->sa.sa_handler;
334 	regs->ax = (unsigned long)sig;
335 	regs->dx = 0;
336 	regs->cx = 0;
337 
338 	regs->ds = __USER_DS;
339 	regs->es = __USER_DS;
340 	regs->ss = __USER_DS;
341 	regs->cs = __USER_CS;
342 
343 	return 0;
344 }
345 
346 static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
347 			    sigset_t *set, struct pt_regs *regs)
348 {
349 	struct rt_sigframe __user *frame;
350 	void __user *restorer;
351 	int err = 0;
352 	void __user *fpstate = NULL;
353 
354 	frame = get_sigframe(ka, regs, sizeof(*frame), &fpstate);
355 
356 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
357 		return -EFAULT;
358 
359 	put_user_try {
360 		put_user_ex(sig, &frame->sig);
361 		put_user_ex(&frame->info, &frame->pinfo);
362 		put_user_ex(&frame->uc, &frame->puc);
363 		err |= copy_siginfo_to_user(&frame->info, info);
364 
365 		/* Create the ucontext.  */
366 		if (cpu_has_xsave)
367 			put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
368 		else
369 			put_user_ex(0, &frame->uc.uc_flags);
370 		put_user_ex(0, &frame->uc.uc_link);
371 		put_user_ex(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
372 		put_user_ex(sas_ss_flags(regs->sp),
373 			    &frame->uc.uc_stack.ss_flags);
374 		put_user_ex(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
375 		err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
376 					regs, set->sig[0]);
377 		err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
378 
379 		/* Set up to return from userspace.  */
380 		restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn);
381 		if (ka->sa.sa_flags & SA_RESTORER)
382 			restorer = ka->sa.sa_restorer;
383 		put_user_ex(restorer, &frame->pretcode);
384 
385 		/*
386 		 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
387 		 *
388 		 * WE DO NOT USE IT ANY MORE! It's only left here for historical
389 		 * reasons and because gdb uses it as a signature to notice
390 		 * signal handler stack frames.
391 		 */
392 		put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
393 	} put_user_catch(err);
394 
395 	if (err)
396 		return -EFAULT;
397 
398 	/* Set up registers for signal handler */
399 	regs->sp = (unsigned long)frame;
400 	regs->ip = (unsigned long)ka->sa.sa_handler;
401 	regs->ax = (unsigned long)sig;
402 	regs->dx = (unsigned long)&frame->info;
403 	regs->cx = (unsigned long)&frame->uc;
404 
405 	regs->ds = __USER_DS;
406 	regs->es = __USER_DS;
407 	regs->ss = __USER_DS;
408 	regs->cs = __USER_CS;
409 
410 	return 0;
411 }
412 #else /* !CONFIG_X86_32 */
413 static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
414 			    sigset_t *set, struct pt_regs *regs)
415 {
416 	struct rt_sigframe __user *frame;
417 	void __user *fp = NULL;
418 	int err = 0;
419 	struct task_struct *me = current;
420 
421 	frame = get_sigframe(ka, regs, sizeof(struct rt_sigframe), &fp);
422 
423 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
424 		return -EFAULT;
425 
426 	if (ka->sa.sa_flags & SA_SIGINFO) {
427 		if (copy_siginfo_to_user(&frame->info, info))
428 			return -EFAULT;
429 	}
430 
431 	put_user_try {
432 		/* Create the ucontext.  */
433 		if (cpu_has_xsave)
434 			put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
435 		else
436 			put_user_ex(0, &frame->uc.uc_flags);
437 		put_user_ex(0, &frame->uc.uc_link);
438 		put_user_ex(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
439 		put_user_ex(sas_ss_flags(regs->sp),
440 			    &frame->uc.uc_stack.ss_flags);
441 		put_user_ex(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
442 		err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
443 		err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
444 
445 		/* Set up to return from userspace.  If provided, use a stub
446 		   already in userspace.  */
447 		/* x86-64 should always use SA_RESTORER. */
448 		if (ka->sa.sa_flags & SA_RESTORER) {
449 			put_user_ex(ka->sa.sa_restorer, &frame->pretcode);
450 		} else {
451 			/* could use a vstub here */
452 			err |= -EFAULT;
453 		}
454 	} put_user_catch(err);
455 
456 	if (err)
457 		return -EFAULT;
458 
459 	/* Set up registers for signal handler */
460 	regs->di = sig;
461 	/* In case the signal handler was declared without prototypes */
462 	regs->ax = 0;
463 
464 	/* This also works for non SA_SIGINFO handlers because they expect the
465 	   next argument after the signal number on the stack. */
466 	regs->si = (unsigned long)&frame->info;
467 	regs->dx = (unsigned long)&frame->uc;
468 	regs->ip = (unsigned long) ka->sa.sa_handler;
469 
470 	regs->sp = (unsigned long)frame;
471 
472 	/* Set up the CS register to run signal handlers in 64-bit mode,
473 	   even if the handler happens to be interrupting 32-bit code. */
474 	regs->cs = __USER_CS;
475 
476 	return 0;
477 }
478 #endif /* CONFIG_X86_32 */
479 
480 #ifdef CONFIG_X86_32
481 /*
482  * Atomically swap in the new signal mask, and wait for a signal.
483  */
484 asmlinkage int
485 sys_sigsuspend(int history0, int history1, old_sigset_t mask)
486 {
487 	mask &= _BLOCKABLE;
488 	spin_lock_irq(&current->sighand->siglock);
489 	current->saved_sigmask = current->blocked;
490 	siginitset(&current->blocked, mask);
491 	recalc_sigpending();
492 	spin_unlock_irq(&current->sighand->siglock);
493 
494 	current->state = TASK_INTERRUPTIBLE;
495 	schedule();
496 	set_restore_sigmask();
497 
498 	return -ERESTARTNOHAND;
499 }
500 
501 asmlinkage int
502 sys_sigaction(int sig, const struct old_sigaction __user *act,
503 	      struct old_sigaction __user *oact)
504 {
505 	struct k_sigaction new_ka, old_ka;
506 	int ret = 0;
507 
508 	if (act) {
509 		old_sigset_t mask;
510 
511 		if (!access_ok(VERIFY_READ, act, sizeof(*act)))
512 			return -EFAULT;
513 
514 		get_user_try {
515 			get_user_ex(new_ka.sa.sa_handler, &act->sa_handler);
516 			get_user_ex(new_ka.sa.sa_flags, &act->sa_flags);
517 			get_user_ex(mask, &act->sa_mask);
518 			get_user_ex(new_ka.sa.sa_restorer, &act->sa_restorer);
519 		} get_user_catch(ret);
520 
521 		if (ret)
522 			return -EFAULT;
523 		siginitset(&new_ka.sa.sa_mask, mask);
524 	}
525 
526 	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
527 
528 	if (!ret && oact) {
529 		if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
530 			return -EFAULT;
531 
532 		put_user_try {
533 			put_user_ex(old_ka.sa.sa_handler, &oact->sa_handler);
534 			put_user_ex(old_ka.sa.sa_flags, &oact->sa_flags);
535 			put_user_ex(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
536 			put_user_ex(old_ka.sa.sa_restorer, &oact->sa_restorer);
537 		} put_user_catch(ret);
538 
539 		if (ret)
540 			return -EFAULT;
541 	}
542 
543 	return ret;
544 }
545 #endif /* CONFIG_X86_32 */
546 
547 #ifdef CONFIG_X86_32
548 int sys_sigaltstack(struct pt_regs *regs)
549 {
550 	const stack_t __user *uss = (const stack_t __user *)regs->bx;
551 	stack_t __user *uoss = (stack_t __user *)regs->cx;
552 
553 	return do_sigaltstack(uss, uoss, regs->sp);
554 }
555 #else /* !CONFIG_X86_32 */
556 asmlinkage long
557 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
558 		struct pt_regs *regs)
559 {
560 	return do_sigaltstack(uss, uoss, regs->sp);
561 }
562 #endif /* CONFIG_X86_32 */
563 
564 /*
565  * Do a signal return; undo the signal stack.
566  */
567 #ifdef CONFIG_X86_32
568 unsigned long sys_sigreturn(struct pt_regs *regs)
569 {
570 	struct sigframe __user *frame;
571 	unsigned long ax;
572 	sigset_t set;
573 
574 	frame = (struct sigframe __user *)(regs->sp - 8);
575 
576 	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
577 		goto badframe;
578 	if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
579 		&& __copy_from_user(&set.sig[1], &frame->extramask,
580 				    sizeof(frame->extramask))))
581 		goto badframe;
582 
583 	sigdelsetmask(&set, ~_BLOCKABLE);
584 	spin_lock_irq(&current->sighand->siglock);
585 	current->blocked = set;
586 	recalc_sigpending();
587 	spin_unlock_irq(&current->sighand->siglock);
588 
589 	if (restore_sigcontext(regs, &frame->sc, &ax))
590 		goto badframe;
591 	return ax;
592 
593 badframe:
594 	signal_fault(regs, frame, "sigreturn");
595 
596 	return 0;
597 }
598 #endif /* CONFIG_X86_32 */
599 
600 long sys_rt_sigreturn(struct pt_regs *regs)
601 {
602 	struct rt_sigframe __user *frame;
603 	unsigned long ax;
604 	sigset_t set;
605 
606 	frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
607 	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
608 		goto badframe;
609 	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
610 		goto badframe;
611 
612 	sigdelsetmask(&set, ~_BLOCKABLE);
613 	spin_lock_irq(&current->sighand->siglock);
614 	current->blocked = set;
615 	recalc_sigpending();
616 	spin_unlock_irq(&current->sighand->siglock);
617 
618 	if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
619 		goto badframe;
620 
621 	if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
622 		goto badframe;
623 
624 	return ax;
625 
626 badframe:
627 	signal_fault(regs, frame, "rt_sigreturn");
628 	return 0;
629 }
630 
631 /*
632  * OK, we're invoking a handler:
633  */
634 static int signr_convert(int sig)
635 {
636 #ifdef CONFIG_X86_32
637 	struct thread_info *info = current_thread_info();
638 
639 	if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
640 		return info->exec_domain->signal_invmap[sig];
641 #endif /* CONFIG_X86_32 */
642 	return sig;
643 }
644 
645 #ifdef CONFIG_X86_32
646 
647 #define is_ia32	1
648 #define ia32_setup_frame	__setup_frame
649 #define ia32_setup_rt_frame	__setup_rt_frame
650 
651 #else /* !CONFIG_X86_32 */
652 
653 #ifdef CONFIG_IA32_EMULATION
654 #define is_ia32	test_thread_flag(TIF_IA32)
655 #else /* !CONFIG_IA32_EMULATION */
656 #define is_ia32	0
657 #endif /* CONFIG_IA32_EMULATION */
658 
659 int ia32_setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
660 		sigset_t *set, struct pt_regs *regs);
661 int ia32_setup_frame(int sig, struct k_sigaction *ka,
662 		sigset_t *set, struct pt_regs *regs);
663 
664 #endif /* CONFIG_X86_32 */
665 
666 static int
667 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
668 	       sigset_t *set, struct pt_regs *regs)
669 {
670 	int usig = signr_convert(sig);
671 	int ret;
672 
673 	/* Set up the stack frame */
674 	if (is_ia32) {
675 		if (ka->sa.sa_flags & SA_SIGINFO)
676 			ret = ia32_setup_rt_frame(usig, ka, info, set, regs);
677 		else
678 			ret = ia32_setup_frame(usig, ka, set, regs);
679 	} else
680 		ret = __setup_rt_frame(sig, ka, info, set, regs);
681 
682 	if (ret) {
683 		force_sigsegv(sig, current);
684 		return -EFAULT;
685 	}
686 
687 	return ret;
688 }
689 
690 static int
691 handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
692 	      sigset_t *oldset, struct pt_regs *regs)
693 {
694 	int ret;
695 
696 	/* Are we from a system call? */
697 	if (syscall_get_nr(current, regs) >= 0) {
698 		/* If so, check system call restarting.. */
699 		switch (syscall_get_error(current, regs)) {
700 		case -ERESTART_RESTARTBLOCK:
701 		case -ERESTARTNOHAND:
702 			regs->ax = -EINTR;
703 			break;
704 
705 		case -ERESTARTSYS:
706 			if (!(ka->sa.sa_flags & SA_RESTART)) {
707 				regs->ax = -EINTR;
708 				break;
709 			}
710 		/* fallthrough */
711 		case -ERESTARTNOINTR:
712 			regs->ax = regs->orig_ax;
713 			regs->ip -= 2;
714 			break;
715 		}
716 	}
717 
718 	/*
719 	 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
720 	 * flag so that register information in the sigcontext is correct.
721 	 */
722 	if (unlikely(regs->flags & X86_EFLAGS_TF) &&
723 	    likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
724 		regs->flags &= ~X86_EFLAGS_TF;
725 
726 	ret = setup_rt_frame(sig, ka, info, oldset, regs);
727 
728 	if (ret)
729 		return ret;
730 
731 #ifdef CONFIG_X86_64
732 	/*
733 	 * This has nothing to do with segment registers,
734 	 * despite the name.  This magic affects uaccess.h
735 	 * macros' behavior.  Reset it to the normal setting.
736 	 */
737 	set_fs(USER_DS);
738 #endif
739 
740 	/*
741 	 * Clear the direction flag as per the ABI for function entry.
742 	 */
743 	regs->flags &= ~X86_EFLAGS_DF;
744 
745 	/*
746 	 * Clear TF when entering the signal handler, but
747 	 * notify any tracer that was single-stepping it.
748 	 * The tracer may want to single-step inside the
749 	 * handler too.
750 	 */
751 	regs->flags &= ~X86_EFLAGS_TF;
752 
753 	spin_lock_irq(&current->sighand->siglock);
754 	sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
755 	if (!(ka->sa.sa_flags & SA_NODEFER))
756 		sigaddset(&current->blocked, sig);
757 	recalc_sigpending();
758 	spin_unlock_irq(&current->sighand->siglock);
759 
760 	tracehook_signal_handler(sig, info, ka, regs,
761 				 test_thread_flag(TIF_SINGLESTEP));
762 
763 	return 0;
764 }
765 
766 #ifdef CONFIG_X86_32
767 #define NR_restart_syscall	__NR_restart_syscall
768 #else /* !CONFIG_X86_32 */
769 #define NR_restart_syscall	\
770 	test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
771 #endif /* CONFIG_X86_32 */
772 
773 /*
774  * Note that 'init' is a special process: it doesn't get signals it doesn't
775  * want to handle. Thus you cannot kill init even with a SIGKILL even by
776  * mistake.
777  */
778 static void do_signal(struct pt_regs *regs)
779 {
780 	struct k_sigaction ka;
781 	siginfo_t info;
782 	int signr;
783 	sigset_t *oldset;
784 
785 	/*
786 	 * We want the common case to go fast, which is why we may in certain
787 	 * cases get here from kernel mode. Just return without doing anything
788 	 * if so.
789 	 * X86_32: vm86 regs switched out by assembly code before reaching
790 	 * here, so testing against kernel CS suffices.
791 	 */
792 	if (!user_mode(regs))
793 		return;
794 
795 	if (current_thread_info()->status & TS_RESTORE_SIGMASK)
796 		oldset = &current->saved_sigmask;
797 	else
798 		oldset = &current->blocked;
799 
800 	signr = get_signal_to_deliver(&info, &ka, regs, NULL);
801 	if (signr > 0) {
802 		/*
803 		 * Re-enable any watchpoints before delivering the
804 		 * signal to user space. The processor register will
805 		 * have been cleared if the watchpoint triggered
806 		 * inside the kernel.
807 		 */
808 		if (current->thread.debugreg7)
809 			set_debugreg(current->thread.debugreg7, 7);
810 
811 		/* Whee! Actually deliver the signal.  */
812 		if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
813 			/*
814 			 * A signal was successfully delivered; the saved
815 			 * sigmask will have been stored in the signal frame,
816 			 * and will be restored by sigreturn, so we can simply
817 			 * clear the TS_RESTORE_SIGMASK flag.
818 			 */
819 			current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
820 		}
821 		return;
822 	}
823 
824 	/* Did we come from a system call? */
825 	if (syscall_get_nr(current, regs) >= 0) {
826 		/* Restart the system call - no handlers present */
827 		switch (syscall_get_error(current, regs)) {
828 		case -ERESTARTNOHAND:
829 		case -ERESTARTSYS:
830 		case -ERESTARTNOINTR:
831 			regs->ax = regs->orig_ax;
832 			regs->ip -= 2;
833 			break;
834 
835 		case -ERESTART_RESTARTBLOCK:
836 			regs->ax = NR_restart_syscall;
837 			regs->ip -= 2;
838 			break;
839 		}
840 	}
841 
842 	/*
843 	 * If there's no signal to deliver, we just put the saved sigmask
844 	 * back.
845 	 */
846 	if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
847 		current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
848 		sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
849 	}
850 }
851 
852 /*
853  * notification of userspace execution resumption
854  * - triggered by the TIF_WORK_MASK flags
855  */
856 void
857 do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
858 {
859 #if defined(CONFIG_X86_64) && defined(CONFIG_X86_MCE)
860 	/* notify userspace of pending MCEs */
861 	if (thread_info_flags & _TIF_MCE_NOTIFY)
862 		mce_notify_user();
863 #endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
864 
865 	/* deal with pending signal delivery */
866 	if (thread_info_flags & _TIF_SIGPENDING)
867 		do_signal(regs);
868 
869 	if (thread_info_flags & _TIF_NOTIFY_RESUME) {
870 		clear_thread_flag(TIF_NOTIFY_RESUME);
871 		tracehook_notify_resume(regs);
872 	}
873 
874 #ifdef CONFIG_X86_32
875 	clear_thread_flag(TIF_IRET);
876 #endif /* CONFIG_X86_32 */
877 }
878 
879 void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
880 {
881 	struct task_struct *me = current;
882 
883 	if (show_unhandled_signals && printk_ratelimit()) {
884 		printk("%s"
885 		       "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
886 		       task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
887 		       me->comm, me->pid, where, frame,
888 		       regs->ip, regs->sp, regs->orig_ax);
889 		print_vma_addr(" in ", regs->ip);
890 		printk(KERN_CONT "\n");
891 	}
892 
893 	force_sig(SIGSEGV, me);
894 }
895