32718f5e4f61c24099bad5fbee57586d6bd57732
[sfrench/cifs-2.6.git] / arch / x86 / kernel / signal_64.c
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 #include <linux/sched.h>
11 #include <linux/mm.h>
12 #include <linux/smp.h>
13 #include <linux/kernel.h>
14 #include <linux/signal.h>
15 #include <linux/errno.h>
16 #include <linux/wait.h>
17 #include <linux/ptrace.h>
18 #include <linux/tracehook.h>
19 #include <linux/unistd.h>
20 #include <linux/stddef.h>
21 #include <linux/personality.h>
22 #include <linux/uaccess.h>
23
24 #include <asm/processor.h>
25 #include <asm/ucontext.h>
26 #include <asm/i387.h>
27 #include <asm/vdso.h>
28
29 #ifdef CONFIG_X86_64
30 #include <asm/proto.h>
31 #include <asm/ia32_unistd.h>
32 #include <asm/mce.h>
33 #endif /* CONFIG_X86_64 */
34
35 #include <asm/syscall.h>
36 #include <asm/syscalls.h>
37
38 #include "sigframe.h"
39
40 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
41
42 #define __FIX_EFLAGS    (X86_EFLAGS_AC | X86_EFLAGS_OF | \
43                          X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \
44                          X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \
45                          X86_EFLAGS_CF)
46
47 #ifdef CONFIG_X86_32
48 # define FIX_EFLAGS     (__FIX_EFLAGS | X86_EFLAGS_RF)
49 #else
50 # define FIX_EFLAGS     __FIX_EFLAGS
51 #endif
52
53 #ifdef CONFIG_X86_32
54 asmlinkage int sys_sigaltstack(unsigned long bx)
55 {
56         /*
57          * This is needed to make gcc realize it doesn't own the
58          * "struct pt_regs"
59          */
60         struct pt_regs *regs = (struct pt_regs *)&bx;
61         const stack_t __user *uss = (const stack_t __user *)bx;
62         stack_t __user *uoss = (stack_t __user *)regs->cx;
63
64         return do_sigaltstack(uss, uoss, regs->sp);
65 }
66 #else /* !CONFIG_X86_32 */
67 asmlinkage long
68 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
69                 struct pt_regs *regs)
70 {
71         return do_sigaltstack(uss, uoss, regs->sp);
72 }
73 #endif /* CONFIG_X86_32 */
74
75 #define COPY(x)                 {               \
76         err |= __get_user(regs->x, &sc->x);     \
77 }
78
79 #define COPY_SEG_CPL3(seg)      {                       \
80                 unsigned short tmp;                     \
81                 err |= __get_user(tmp, &sc->seg);       \
82                 regs->seg = tmp | 3;                    \
83 }
84
85 /*
86  * Do a signal return; undo the signal stack.
87  */
88 static int
89 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
90                    unsigned long *pax)
91 {
92         void __user *buf;
93         unsigned int tmpflags;
94         unsigned int err = 0;
95
96         /* Always make any pending restarted system calls return -EINTR */
97         current_thread_info()->restart_block.fn = do_no_restart_syscall;
98
99 #ifdef CONFIG_X86_32
100         GET_SEG(gs);
101         COPY_SEG(fs);
102         COPY_SEG(es);
103         COPY_SEG(ds);
104 #endif /* CONFIG_X86_32 */
105
106         COPY(di); COPY(si); COPY(bp); COPY(sp); COPY(bx);
107         COPY(dx); COPY(cx); COPY(ip);
108
109 #ifdef CONFIG_X86_64
110         COPY(r8);
111         COPY(r9);
112         COPY(r10);
113         COPY(r11);
114         COPY(r12);
115         COPY(r13);
116         COPY(r14);
117         COPY(r15);
118 #endif /* CONFIG_X86_64 */
119
120 #ifdef CONFIG_X86_32
121         COPY_SEG_CPL3(cs);
122         COPY_SEG_CPL3(ss);
123 #else /* !CONFIG_X86_32 */
124         /* Kernel saves and restores only the CS segment register on signals,
125          * which is the bare minimum needed to allow mixed 32/64-bit code.
126          * App's signal handler can save/restore other segments if needed. */
127         COPY_SEG_CPL3(cs);
128 #endif /* CONFIG_X86_32 */
129
130         err |= __get_user(tmpflags, &sc->flags);
131         regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
132         regs->orig_ax = -1;             /* disable syscall checks */
133
134         err |= __get_user(buf, &sc->fpstate);
135         err |= restore_i387_xstate(buf);
136
137         err |= __get_user(*pax, &sc->ax);
138         return err;
139 }
140
141 static long do_rt_sigreturn(struct pt_regs *regs)
142 {
143         struct rt_sigframe __user *frame;
144         unsigned long ax;
145         sigset_t set;
146
147         frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
148         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
149                 goto badframe;
150         if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
151                 goto badframe;
152
153         sigdelsetmask(&set, ~_BLOCKABLE);
154         spin_lock_irq(&current->sighand->siglock);
155         current->blocked = set;
156         recalc_sigpending();
157         spin_unlock_irq(&current->sighand->siglock);
158
159         if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
160                 goto badframe;
161
162         if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
163                 goto badframe;
164
165         return ax;
166
167 badframe:
168         signal_fault(regs, frame, "rt_sigreturn");
169         return 0;
170 }
171
172 #ifdef CONFIG_X86_32
173 asmlinkage int sys_rt_sigreturn(unsigned long __unused)
174 {
175         struct pt_regs *regs = (struct pt_regs *)&__unused;
176
177         return do_rt_sigreturn(regs);
178 }
179 #else /* !CONFIG_X86_32 */
180 asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
181 {
182         return do_rt_sigreturn(regs);
183 }
184 #endif /* CONFIG_X86_32 */
185
186 /*
187  * Set up a signal frame.
188  */
189 static int
190 setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
191                  struct pt_regs *regs, unsigned long mask)
192 {
193         int err = 0;
194
195 #ifdef CONFIG_X86_32
196         {
197                 unsigned int tmp;
198
199                 savesegment(gs, tmp);
200                 err |= __put_user(tmp, (unsigned int __user *)&sc->gs);
201         }
202         err |= __put_user(regs->fs, (unsigned int __user *)&sc->fs);
203         err |= __put_user(regs->es, (unsigned int __user *)&sc->es);
204         err |= __put_user(regs->ds, (unsigned int __user *)&sc->ds);
205 #endif /* CONFIG_X86_32 */
206
207         err |= __put_user(regs->di, &sc->di);
208         err |= __put_user(regs->si, &sc->si);
209         err |= __put_user(regs->bp, &sc->bp);
210         err |= __put_user(regs->sp, &sc->sp);
211         err |= __put_user(regs->bx, &sc->bx);
212         err |= __put_user(regs->dx, &sc->dx);
213         err |= __put_user(regs->cx, &sc->cx);
214         err |= __put_user(regs->ax, &sc->ax);
215 #ifdef CONFIG_X86_64
216         err |= __put_user(regs->r8, &sc->r8);
217         err |= __put_user(regs->r9, &sc->r9);
218         err |= __put_user(regs->r10, &sc->r10);
219         err |= __put_user(regs->r11, &sc->r11);
220         err |= __put_user(regs->r12, &sc->r12);
221         err |= __put_user(regs->r13, &sc->r13);
222         err |= __put_user(regs->r14, &sc->r14);
223         err |= __put_user(regs->r15, &sc->r15);
224 #endif /* CONFIG_X86_64 */
225
226         err |= __put_user(current->thread.trap_no, &sc->trapno);
227         err |= __put_user(current->thread.error_code, &sc->err);
228         err |= __put_user(regs->ip, &sc->ip);
229 #ifdef CONFIG_X86_32
230         err |= __put_user(regs->cs, (unsigned int __user *)&sc->cs);
231         err |= __put_user(regs->flags, &sc->flags);
232         err |= __put_user(regs->sp, &sc->sp_at_signal);
233         err |= __put_user(regs->ss, (unsigned int __user *)&sc->ss);
234 #else /* !CONFIG_X86_32 */
235         err |= __put_user(regs->flags, &sc->flags);
236         err |= __put_user(regs->cs, &sc->cs);
237         err |= __put_user(0, &sc->gs);
238         err |= __put_user(0, &sc->fs);
239 #endif /* CONFIG_X86_32 */
240
241         err |= __put_user(fpstate, &sc->fpstate);
242
243         /* non-iBCS2 extensions.. */
244         err |= __put_user(mask, &sc->oldmask);
245         err |= __put_user(current->thread.cr2, &sc->cr2);
246
247         return err;
248 }
249
250 /*
251  * Determine which stack to use..
252  */
253
254 static void __user *
255 get_stack(struct k_sigaction *ka, unsigned long sp, unsigned long size)
256 {
257         /* Default to using normal stack - redzone*/
258         sp -= 128;
259
260         /* This is the X/Open sanctioned signal stack switching.  */
261         if (ka->sa.sa_flags & SA_ONSTACK) {
262                 if (sas_ss_flags(sp) == 0)
263                         sp = current->sas_ss_sp + current->sas_ss_size;
264         }
265
266         return (void __user *)round_down(sp - size, 64);
267 }
268
269 static int __setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
270                             sigset_t *set, struct pt_regs *regs)
271 {
272         struct rt_sigframe __user *frame;
273         void __user *fp = NULL;
274         int err = 0;
275         struct task_struct *me = current;
276
277         if (used_math()) {
278                 fp = get_stack(ka, regs->sp, sig_xstate_size);
279                 frame = (void __user *)round_down(
280                         (unsigned long)fp - sizeof(struct rt_sigframe), 16) - 8;
281
282                 if (save_i387_xstate(fp) < 0)
283                         return -EFAULT;
284         } else
285                 frame = get_stack(ka, regs->sp, sizeof(struct rt_sigframe)) - 8;
286
287         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
288                 return -EFAULT;
289
290         if (ka->sa.sa_flags & SA_SIGINFO) {
291                 if (copy_siginfo_to_user(&frame->info, info))
292                         return -EFAULT;
293         }
294
295         /* Create the ucontext.  */
296         if (cpu_has_xsave)
297                 err |= __put_user(UC_FP_XSTATE, &frame->uc.uc_flags);
298         else
299                 err |= __put_user(0, &frame->uc.uc_flags);
300         err |= __put_user(0, &frame->uc.uc_link);
301         err |= __put_user(me->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
302         err |= __put_user(sas_ss_flags(regs->sp),
303                           &frame->uc.uc_stack.ss_flags);
304         err |= __put_user(me->sas_ss_size, &frame->uc.uc_stack.ss_size);
305         err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
306         err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
307
308         /* Set up to return from userspace.  If provided, use a stub
309            already in userspace.  */
310         /* x86-64 should always use SA_RESTORER. */
311         if (ka->sa.sa_flags & SA_RESTORER) {
312                 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
313         } else {
314                 /* could use a vstub here */
315                 return -EFAULT;
316         }
317
318         if (err)
319                 return -EFAULT;
320
321         /* Set up registers for signal handler */
322         regs->di = sig;
323         /* In case the signal handler was declared without prototypes */
324         regs->ax = 0;
325
326         /* This also works for non SA_SIGINFO handlers because they expect the
327            next argument after the signal number on the stack. */
328         regs->si = (unsigned long)&frame->info;
329         regs->dx = (unsigned long)&frame->uc;
330         regs->ip = (unsigned long) ka->sa.sa_handler;
331
332         regs->sp = (unsigned long)frame;
333
334         /* Set up the CS register to run signal handlers in 64-bit mode,
335            even if the handler happens to be interrupting 32-bit code. */
336         regs->cs = __USER_CS;
337
338         return 0;
339 }
340
341 /*
342  * OK, we're invoking a handler
343  */
344 static int signr_convert(int sig)
345 {
346 #ifdef CONFIG_X86_32
347         struct thread_info *info = current_thread_info();
348
349         if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
350                 return info->exec_domain->signal_invmap[sig];
351 #endif /* CONFIG_X86_32 */
352         return sig;
353 }
354
355 #ifdef CONFIG_X86_32
356
357 #define is_ia32 1
358 #define ia32_setup_frame        __setup_frame
359 #define ia32_setup_rt_frame     __setup_rt_frame
360
361 #else /* !CONFIG_X86_32 */
362
363 #ifdef CONFIG_IA32_EMULATION
364 #define is_ia32 test_thread_flag(TIF_IA32)
365 #else /* !CONFIG_IA32_EMULATION */
366 #define is_ia32 0
367 #endif /* CONFIG_IA32_EMULATION */
368
369 #endif /* CONFIG_X86_32 */
370
371 static int
372 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
373                sigset_t *set, struct pt_regs *regs)
374 {
375         int usig = signr_convert(sig);
376         int ret;
377
378         /* Set up the stack frame */
379         if (is_ia32) {
380                 if (ka->sa.sa_flags & SA_SIGINFO)
381                         ret = ia32_setup_rt_frame(usig, ka, info, set, regs);
382                 else
383                         ret = ia32_setup_frame(usig, ka, set, regs);
384         } else
385                 ret = __setup_rt_frame(sig, ka, info, set, regs);
386
387         if (ret) {
388                 force_sigsegv(sig, current);
389                 return -EFAULT;
390         }
391
392         return ret;
393 }
394
395 static int
396 handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
397               sigset_t *oldset, struct pt_regs *regs)
398 {
399         int ret;
400
401         /* Are we from a system call? */
402         if (syscall_get_nr(current, regs) >= 0) {
403                 /* If so, check system call restarting.. */
404                 switch (syscall_get_error(current, regs)) {
405                 case -ERESTART_RESTARTBLOCK:
406                 case -ERESTARTNOHAND:
407                         regs->ax = -EINTR;
408                         break;
409
410                 case -ERESTARTSYS:
411                         if (!(ka->sa.sa_flags & SA_RESTART)) {
412                                 regs->ax = -EINTR;
413                                 break;
414                         }
415                 /* fallthrough */
416                 case -ERESTARTNOINTR:
417                         regs->ax = regs->orig_ax;
418                         regs->ip -= 2;
419                         break;
420                 }
421         }
422
423         /*
424          * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
425          * flag so that register information in the sigcontext is correct.
426          */
427         if (unlikely(regs->flags & X86_EFLAGS_TF) &&
428             likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
429                 regs->flags &= ~X86_EFLAGS_TF;
430
431         ret = setup_rt_frame(sig, ka, info, oldset, regs);
432
433         if (ret)
434                 return ret;
435
436 #ifdef CONFIG_X86_64
437         /*
438          * This has nothing to do with segment registers,
439          * despite the name.  This magic affects uaccess.h
440          * macros' behavior.  Reset it to the normal setting.
441          */
442         set_fs(USER_DS);
443 #endif
444
445         /*
446          * Clear the direction flag as per the ABI for function entry.
447          */
448         regs->flags &= ~X86_EFLAGS_DF;
449
450         /*
451          * Clear TF when entering the signal handler, but
452          * notify any tracer that was single-stepping it.
453          * The tracer may want to single-step inside the
454          * handler too.
455          */
456         regs->flags &= ~X86_EFLAGS_TF;
457
458         spin_lock_irq(&current->sighand->siglock);
459         sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
460         if (!(ka->sa.sa_flags & SA_NODEFER))
461                 sigaddset(&current->blocked, sig);
462         recalc_sigpending();
463         spin_unlock_irq(&current->sighand->siglock);
464
465         tracehook_signal_handler(sig, info, ka, regs,
466                                  test_thread_flag(TIF_SINGLESTEP));
467
468         return 0;
469 }
470
471 #ifdef CONFIG_X86_32
472 #define NR_restart_syscall      __NR_restart_syscall
473 #else /* !CONFIG_X86_32 */
474 #define NR_restart_syscall      \
475         test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
476 #endif /* CONFIG_X86_32 */
477
478 /*
479  * Note that 'init' is a special process: it doesn't get signals it doesn't
480  * want to handle. Thus you cannot kill init even with a SIGKILL even by
481  * mistake.
482  */
483 static void do_signal(struct pt_regs *regs)
484 {
485         struct k_sigaction ka;
486         siginfo_t info;
487         int signr;
488         sigset_t *oldset;
489
490         /*
491          * We want the common case to go fast, which is why we may in certain
492          * cases get here from kernel mode. Just return without doing anything
493          * if so.
494          * X86_32: vm86 regs switched out by assembly code before reaching
495          * here, so testing against kernel CS suffices.
496          */
497         if (!user_mode(regs))
498                 return;
499
500         if (current_thread_info()->status & TS_RESTORE_SIGMASK)
501                 oldset = &current->saved_sigmask;
502         else
503                 oldset = &current->blocked;
504
505         signr = get_signal_to_deliver(&info, &ka, regs, NULL);
506         if (signr > 0) {
507                 /*
508                  * Re-enable any watchpoints before delivering the
509                  * signal to user space. The processor register will
510                  * have been cleared if the watchpoint triggered
511                  * inside the kernel.
512                  */
513                 if (current->thread.debugreg7)
514                         set_debugreg(current->thread.debugreg7, 7);
515
516                 /* Whee! Actually deliver the signal.  */
517                 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
518                         /*
519                          * A signal was successfully delivered; the saved
520                          * sigmask will have been stored in the signal frame,
521                          * and will be restored by sigreturn, so we can simply
522                          * clear the TS_RESTORE_SIGMASK flag.
523                          */
524                         current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
525                 }
526                 return;
527         }
528
529         /* Did we come from a system call? */
530         if (syscall_get_nr(current, regs) >= 0) {
531                 /* Restart the system call - no handlers present */
532                 switch (syscall_get_error(current, regs)) {
533                 case -ERESTARTNOHAND:
534                 case -ERESTARTSYS:
535                 case -ERESTARTNOINTR:
536                         regs->ax = regs->orig_ax;
537                         regs->ip -= 2;
538                         break;
539
540                 case -ERESTART_RESTARTBLOCK:
541                         regs->ax = NR_restart_syscall;
542                         regs->ip -= 2;
543                         break;
544                 }
545         }
546
547         /*
548          * If there's no signal to deliver, we just put the saved sigmask
549          * back.
550          */
551         if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
552                 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
553                 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
554         }
555 }
556
557 /*
558  * notification of userspace execution resumption
559  * - triggered by the TIF_WORK_MASK flags
560  */
561 void
562 do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
563 {
564 #if defined(CONFIG_X86_64) && defined(CONFIG_X86_MCE)
565         /* notify userspace of pending MCEs */
566         if (thread_info_flags & _TIF_MCE_NOTIFY)
567                 mce_notify_user();
568 #endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
569
570         /* deal with pending signal delivery */
571         if (thread_info_flags & _TIF_SIGPENDING)
572                 do_signal(regs);
573
574         if (thread_info_flags & _TIF_NOTIFY_RESUME) {
575                 clear_thread_flag(TIF_NOTIFY_RESUME);
576                 tracehook_notify_resume(regs);
577         }
578
579 #ifdef CONFIG_X86_32
580         clear_thread_flag(TIF_IRET);
581 #endif /* CONFIG_X86_32 */
582 }
583
584 void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
585 {
586         struct task_struct *me = current;
587
588         if (show_unhandled_signals && printk_ratelimit()) {
589                 printk(KERN_INFO
590                        "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
591                        me->comm, me->pid, where, frame,
592                        regs->ip, regs->sp, regs->orig_ax);
593                 print_vma_addr(" in ", regs->ip);
594                 printk(KERN_CONT "\n");
595         }
596
597         force_sig(SIGSEGV, me);
598 }