This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / arch / i386 / kernel / signal.c
1 /*
2  *  linux/arch/i386/kernel/signal.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  *
6  *  1997-11-28  Modified for POSIX.1b signals by Richard Henderson
7  *  2000-06-20  Pentium III FXSR, SSE support by Gareth Hughes
8  */
9
10 #include <linux/sched.h>
11 #include <linux/mm.h>
12 #include <linux/smp.h>
13 #include <linux/smp_lock.h>
14 #include <linux/kernel.h>
15 #include <linux/signal.h>
16 #include <linux/errno.h>
17 #include <linux/wait.h>
18 #include <linux/unistd.h>
19 #include <linux/stddef.h>
20 #include <linux/personality.h>
21 #include <linux/suspend.h>
22 #include <linux/elf.h>
23 #include <asm/processor.h>
24 #include <asm/ucontext.h>
25 #include <asm/uaccess.h>
26 #include <asm/i387.h>
27 #include "sigframe.h"
28
29 #define DEBUG_SIG 0
30
31 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
32
33 /*
34  * Atomically swap in the new signal mask, and wait for a signal.
35  */
36 asmlinkage int
37 sys_sigsuspend(int history0, int history1, old_sigset_t mask)
38 {
39         struct pt_regs * regs = (struct pt_regs *) &history0;
40         sigset_t saveset;
41
42         mask &= _BLOCKABLE;
43         spin_lock_irq(&current->sighand->siglock);
44         saveset = current->blocked;
45         siginitset(&current->blocked, mask);
46         recalc_sigpending();
47         spin_unlock_irq(&current->sighand->siglock);
48
49         regs->eax = -EINTR;
50         while (1) {
51                 current->state = TASK_INTERRUPTIBLE;
52                 schedule();
53                 if (do_signal(regs, &saveset))
54                         return -EINTR;
55         }
56 }
57
58 asmlinkage int
59 sys_rt_sigsuspend(struct pt_regs regs)
60 {
61         sigset_t saveset, newset;
62
63         /* XXX: Don't preclude handling different sized sigset_t's.  */
64         if (regs.ecx != sizeof(sigset_t))
65                 return -EINVAL;
66
67         if (copy_from_user(&newset, (sigset_t __user *)regs.ebx, sizeof(newset)))
68                 return -EFAULT;
69         sigdelsetmask(&newset, ~_BLOCKABLE);
70
71         spin_lock_irq(&current->sighand->siglock);
72         saveset = current->blocked;
73         current->blocked = newset;
74         recalc_sigpending();
75         spin_unlock_irq(&current->sighand->siglock);
76
77         regs.eax = -EINTR;
78         while (1) {
79                 current->state = TASK_INTERRUPTIBLE;
80                 schedule();
81                 if (do_signal(&regs, &saveset))
82                         return -EINTR;
83         }
84 }
85
86 asmlinkage int 
87 sys_sigaction(int sig, const struct old_sigaction __user *act,
88               struct old_sigaction __user *oact)
89 {
90         struct k_sigaction new_ka, old_ka;
91         int ret;
92
93         if (act) {
94                 old_sigset_t mask;
95                 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
96                     __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
97                     __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
98                         return -EFAULT;
99                 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
100                 __get_user(mask, &act->sa_mask);
101                 siginitset(&new_ka.sa.sa_mask, mask);
102         }
103
104         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
105
106         if (!ret && oact) {
107                 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
108                     __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
109                     __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
110                         return -EFAULT;
111                 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
112                 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
113         }
114
115         return ret;
116 }
117
118 asmlinkage int
119 sys_sigaltstack(struct pt_regs regs)
120 {
121         const stack_t __user *uss = (const stack_t __user *)regs.ebx;
122         stack_t __user *uoss = (stack_t __user *)regs.ecx;
123         return do_sigaltstack(uss, uoss, regs.esp);
124 }
125
126
127 /*
128  * Do a signal return; undo the signal stack.
129  */
130
131 static int
132 restore_sigcontext(struct pt_regs *regs,
133                 struct sigcontext __user *__sc, int *peax)
134 {
135         struct sigcontext scratch; /* 88 bytes of scratch area */
136
137         /* Always make any pending restarted system calls return -EINTR */
138         current_thread_info()->restart_block.fn = do_no_restart_syscall;
139
140         if (copy_from_user(&scratch, __sc, sizeof(scratch)))
141                 return -EFAULT;
142
143 #define COPY(x)         regs->x = scratch.x
144
145 #define COPY_SEG(seg)                                                   \
146         { unsigned short tmp = scratch.seg;                             \
147           regs->x##seg = tmp; }
148
149 #define COPY_SEG_STRICT(seg)                                            \
150         { unsigned short tmp = scratch.seg;                             \
151           regs->x##seg = tmp|3; }
152
153 #define GET_SEG(seg)                                                    \
154         { unsigned short tmp = scratch.seg;                             \
155           loadsegment(seg,tmp); }
156
157 #define FIX_EFLAGS      (X86_EFLAGS_AC | X86_EFLAGS_OF | X86_EFLAGS_DF | \
158                          X86_EFLAGS_TF | X86_EFLAGS_SF | X86_EFLAGS_ZF | \
159                          X86_EFLAGS_AF | X86_EFLAGS_PF | X86_EFLAGS_CF)
160
161         GET_SEG(gs);
162         GET_SEG(fs);
163         COPY_SEG(es);
164         COPY_SEG(ds);
165         COPY(edi);
166         COPY(esi);
167         COPY(ebp);
168         COPY(esp);
169         COPY(ebx);
170         COPY(edx);
171         COPY(ecx);
172         COPY(eip);
173         COPY_SEG_STRICT(cs);
174         COPY_SEG_STRICT(ss);
175         
176         {
177                 unsigned int tmpflags = scratch.eflags;
178                 regs->eflags = (regs->eflags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
179                 regs->orig_eax = -1;            /* disable syscall checks */
180         }
181
182         {
183                 struct _fpstate * buf = scratch.fpstate;
184                 if (buf) {
185                         if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
186                                 return -EFAULT;
187                         if (restore_i387(buf))
188                                 return -EFAULT;
189                 }
190         }
191
192         *peax = scratch.eax;
193         return 0;
194 }
195
196 asmlinkage int sys_sigreturn(unsigned long __unused)
197 {
198         struct pt_regs *regs = (struct pt_regs *) &__unused;
199         struct sigframe __user *frame = (struct sigframe __user *)(regs->esp - 8);
200         sigset_t set;
201         int eax;
202
203         if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
204                 goto badframe;
205         if (__get_user(set.sig[0], &frame->sc.oldmask)
206             || (_NSIG_WORDS > 1
207                 && __copy_from_user(&set.sig[1], &frame->extramask,
208                                     sizeof(frame->extramask))))
209                 goto badframe;
210
211         sigdelsetmask(&set, ~_BLOCKABLE);
212         spin_lock_irq(&current->sighand->siglock);
213         current->blocked = set;
214         recalc_sigpending();
215         spin_unlock_irq(&current->sighand->siglock);
216         
217         if (restore_sigcontext(regs, &frame->sc, &eax))
218                 goto badframe;
219         return eax;
220
221 badframe:
222         force_sig(SIGSEGV, current);
223         return 0;
224 }       
225
226 asmlinkage int sys_rt_sigreturn(unsigned long __unused)
227 {
228         struct pt_regs *regs = (struct pt_regs *) &__unused;
229         struct rt_sigframe __user *frame = (struct rt_sigframe __user *)(regs->esp - 4);
230         sigset_t set;
231         int eax;
232
233         if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
234                 goto badframe;
235         if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
236                 goto badframe;
237
238         sigdelsetmask(&set, ~_BLOCKABLE);
239         spin_lock_irq(&current->sighand->siglock);
240         current->blocked = set;
241         recalc_sigpending();
242         spin_unlock_irq(&current->sighand->siglock);
243         
244         if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &eax))
245                 goto badframe;
246
247         if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->esp) == -EFAULT)
248                 goto badframe;
249
250         return eax;
251
252 badframe:
253         force_sig(SIGSEGV, current);
254         return 0;
255 }       
256
257 /*
258  * Set up a signal frame.
259  */
260
261 static int
262 setup_sigcontext(struct sigcontext __user *__sc, struct _fpstate __user *fpstate,
263                  struct pt_regs *regs, unsigned long mask)
264 {
265         struct sigcontext sc; /* 88 bytes of scratch area */
266         int tmp;
267
268         tmp = 0;
269         __asm__("movl %%gs,%0" : "=r"(tmp): "0"(tmp));
270         *(unsigned int *)&sc.gs = tmp;
271         __asm__("movl %%fs,%0" : "=r"(tmp): "0"(tmp));
272         *(unsigned int *)&sc.fs = tmp;
273         *(unsigned int *)&sc.es = regs->xes;
274         *(unsigned int *)&sc.ds = regs->xds;
275         sc.edi = regs->edi;
276         sc.esi = regs->esi;
277         sc.ebp = regs->ebp;
278         sc.esp = regs->esp;
279         sc.ebx = regs->ebx;
280         sc.edx = regs->edx;
281         sc.ecx = regs->ecx;
282         sc.eax = regs->eax;
283         sc.trapno = current->thread.trap_no;
284         sc.err = current->thread.error_code;
285         sc.eip = regs->eip;
286         *(unsigned int *)&sc.cs = regs->xcs;
287         sc.eflags = regs->eflags;
288         sc.esp_at_signal = regs->esp;
289         *(unsigned int *)&sc.ss = regs->xss;
290
291         tmp = save_i387(fpstate);
292         if (tmp < 0)
293                 return 1;
294         sc.fpstate = tmp ? fpstate : NULL;
295
296         /* non-iBCS2 extensions.. */
297         sc.oldmask = mask;
298         sc.cr2 = current->thread.cr2;
299
300         if (copy_to_user(__sc, &sc, sizeof(sc)))
301                 return 1;
302         return 0;
303 }
304
305 /*
306  * Determine which stack to use..
307  */
308 static inline void __user *
309 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
310 {
311         unsigned long esp;
312
313         /* Default to using normal stack */
314         esp = regs->esp;
315
316         /* This is the X/Open sanctioned signal stack switching.  */
317         if (ka->sa.sa_flags & SA_ONSTACK) {
318                 if (sas_ss_flags(esp) == 0)
319                         esp = current->sas_ss_sp + current->sas_ss_size;
320         }
321
322         /* This is the legacy signal stack switching. */
323         else if ((regs->xss & 0xffff) != __USER_DS &&
324                  !(ka->sa.sa_flags & SA_RESTORER) &&
325                  ka->sa.sa_restorer) {
326                 esp = (unsigned long) ka->sa.sa_restorer;
327         }
328
329         return (void __user *)((esp - frame_size) & -8ul);
330 }
331
332 /* These symbols are defined with the addresses in the vsyscall page.
333    See vsyscall-sigreturn.S.  */
334 extern char __kernel_sigreturn, __kernel_rt_sigreturn, SYSENTER_RETURN;
335
336 static void setup_frame(int sig, struct k_sigaction *ka,
337                         sigset_t *set, struct pt_regs * regs)
338 {
339         void *restorer;
340         struct sigframe __user *frame;
341         int err = 0;
342
343         frame = get_sigframe(ka, regs, sizeof(*frame));
344
345         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
346                 goto give_sigsegv;
347
348         err |= __put_user((current_thread_info()->exec_domain
349                            && current_thread_info()->exec_domain->signal_invmap
350                            && sig < 32
351                            ? current_thread_info()->exec_domain->signal_invmap[sig]
352                            : sig),
353                           &frame->sig);
354         if (err)
355                 goto give_sigsegv;
356
357         err |= setup_sigcontext(&frame->sc, &frame->fpstate, regs, set->sig[0]);
358         if (err)
359                 goto give_sigsegv;
360
361         if (_NSIG_WORDS > 1) {
362                 err |= __copy_to_user(&frame->extramask, &set->sig[1],
363                                       sizeof(frame->extramask));
364         }
365         if (err)
366                 goto give_sigsegv;
367
368         restorer = current->mm->context.vdso + (long)&__kernel_sigreturn;
369         if (ka->sa.sa_flags & SA_RESTORER)
370                 restorer = ka->sa.sa_restorer;
371
372         /* Set up to return from userspace.  */
373         err |= __put_user(restorer, &frame->pretcode);
374          
375         /*
376          * This is popl %eax ; movl $,%eax ; int $0x80
377          *
378          * WE DO NOT USE IT ANY MORE! It's only left here for historical
379          * reasons and because gdb uses it as a signature to notice
380          * signal handler stack frames.
381          */
382         err |= __put_user(0xb858, (short __user *)(frame->retcode+0));
383         err |= __put_user(__NR_sigreturn, (int __user *)(frame->retcode+2));
384         err |= __put_user(0x80cd, (short __user *)(frame->retcode+6));
385
386         if (err)
387                 goto give_sigsegv;
388
389         /* Set up registers for signal handler */
390         regs->esp = (unsigned long) frame;
391         regs->eip = (unsigned long) ka->sa.sa_handler;
392
393         set_fs(USER_DS);
394         regs->xds = __USER_DS;
395         regs->xes = __USER_DS;
396         regs->xss = __USER_DS;
397         regs->xcs = __USER_CS;
398         regs->eflags &= ~TF_MASK;
399
400 #if DEBUG_SIG
401         printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
402                 current->comm, current->pid, frame, regs->eip, frame->pretcode);
403 #endif
404
405         return;
406
407 give_sigsegv:
408         if (sig == SIGSEGV)
409                 ka->sa.sa_handler = SIG_DFL;
410         force_sig(SIGSEGV, current);
411 }
412
413 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
414                            sigset_t *set, struct pt_regs * regs)
415 {
416         void *restorer;
417         struct rt_sigframe __user *frame;
418         int err = 0;
419
420         frame = get_sigframe(ka, regs, sizeof(*frame));
421
422         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
423                 goto give_sigsegv;
424
425         err |= __put_user((current_thread_info()->exec_domain
426                            && current_thread_info()->exec_domain->signal_invmap
427                            && sig < 32
428                            ? current_thread_info()->exec_domain->signal_invmap[sig]
429                            : sig),
430                           &frame->sig);
431         err |= __put_user(&frame->info, &frame->pinfo);
432         err |= __put_user(&frame->uc, &frame->puc);
433         err |= copy_siginfo_to_user(&frame->info, info);
434         if (err)
435                 goto give_sigsegv;
436
437         /* Create the ucontext.  */
438         err |= __put_user(0, &frame->uc.uc_flags);
439         err |= __put_user(0, &frame->uc.uc_link);
440         err |= __put_user(current->sas_ss_sp, (unsigned long *)&frame->uc.uc_stack.ss_sp);
441         err |= __put_user(sas_ss_flags(regs->esp),
442                           &frame->uc.uc_stack.ss_flags);
443         err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
444         err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->fpstate,
445                                 regs, set->sig[0]);
446         err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
447         if (err)
448                 goto give_sigsegv;
449
450         /* Set up to return from userspace.  */
451         restorer = current->mm->context.vdso + (long)&__kernel_rt_sigreturn;
452         if (ka->sa.sa_flags & SA_RESTORER)
453                 restorer = ka->sa.sa_restorer;
454
455         err |= __put_user(restorer, &frame->pretcode);
456          
457         /*
458          * This is movl $,%eax ; int $0x80
459          *
460          * WE DO NOT USE IT ANY MORE! It's only left here for historical
461          * reasons and because gdb uses it as a signature to notice
462          * signal handler stack frames.
463          */
464         err |= __put_user(0xb8, (char __user *)(frame->retcode+0));
465         err |= __put_user(__NR_rt_sigreturn, (int __user *)(frame->retcode+1));
466         err |= __put_user(0x80cd, (short __user *)(frame->retcode+5));
467
468         if (err)
469                 goto give_sigsegv;
470
471         /* Set up registers for signal handler */
472         regs->esp = (unsigned long) frame;
473         regs->eip = (unsigned long) ka->sa.sa_handler;
474
475         set_fs(USER_DS);
476         regs->xds = __USER_DS;
477         regs->xes = __USER_DS;
478         regs->xss = __USER_DS;
479         regs->xcs = __USER_CS;
480         regs->eflags &= ~TF_MASK;
481
482 #if DEBUG_SIG
483         printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
484                 current->comm, current->pid, frame, regs->eip, frame->pretcode);
485 #endif
486
487         return;
488
489 give_sigsegv:
490         if (sig == SIGSEGV)
491                 ka->sa.sa_handler = SIG_DFL;
492         force_sig(SIGSEGV, current);
493 }
494
495 /*
496  * OK, we're invoking a handler
497  */     
498
499 static void
500 handle_signal(unsigned long sig, siginfo_t *info, sigset_t *oldset,
501         struct pt_regs * regs)
502 {
503         struct k_sigaction *ka = &current->sighand->action[sig-1];
504
505         /* Are we from a system call? */
506         if (regs->orig_eax >= 0) {
507                 /* If so, check system call restarting.. */
508                 switch (regs->eax) {
509                         case -ERESTART_RESTARTBLOCK:
510                         case -ERESTARTNOHAND:
511                                 regs->eax = -EINTR;
512                                 break;
513
514                         case -ERESTARTSYS:
515                                 if (!(ka->sa.sa_flags & SA_RESTART)) {
516                                         regs->eax = -EINTR;
517                                         break;
518                                 }
519                         /* fallthrough */
520                         case -ERESTARTNOINTR:
521                                 regs->eax = regs->orig_eax;
522                                 regs->eip -= 2;
523                 }
524         }
525
526         /* Set up the stack frame */
527         if (ka->sa.sa_flags & SA_SIGINFO)
528                 setup_rt_frame(sig, ka, info, oldset, regs);
529         else
530                 setup_frame(sig, ka, oldset, regs);
531
532         if (ka->sa.sa_flags & SA_ONESHOT)
533                 ka->sa.sa_handler = SIG_DFL;
534
535         if (!(ka->sa.sa_flags & SA_NODEFER)) {
536                 spin_lock_irq(&current->sighand->siglock);
537                 sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
538                 sigaddset(&current->blocked,sig);
539                 recalc_sigpending();
540                 spin_unlock_irq(&current->sighand->siglock);
541         }
542 }
543
544 /*
545  * Note that 'init' is a special process: it doesn't get signals it doesn't
546  * want to handle. Thus you cannot kill init even with a SIGKILL even by
547  * mistake.
548  */
549 int fastcall do_signal(struct pt_regs *regs, sigset_t *oldset)
550 {
551         siginfo_t info;
552         int signr;
553
554         /*
555          * We want the common case to go fast, which
556          * is why we may in certain cases get here from
557          * kernel mode. Just return without doing anything
558          * if so.
559          */
560         if ((regs->xcs & 3) != 3)
561                 return 1;
562
563         if (current->flags & PF_FREEZE) {
564                 refrigerator(0);
565                 goto no_signal;
566         }
567
568         if (!oldset)
569                 oldset = &current->blocked;
570
571         signr = get_signal_to_deliver(&info, regs, NULL);
572         if (signr > 0) {
573                 /* Reenable any watchpoints before delivering the
574                  * signal to user space. The processor register will
575                  * have been cleared if the watchpoint triggered
576                  * inside the kernel.
577                  */
578                 __asm__("movl %0,%%db7" : : "r" (current->thread.debugreg[7]));
579
580                 /* Whee!  Actually deliver the signal.  */
581                 handle_signal(signr, &info, oldset, regs);
582                 return 1;
583         }
584
585  no_signal:
586         /* Did we come from a system call? */
587         if (regs->orig_eax >= 0) {
588                 /* Restart the system call - no handlers present */
589                 if (regs->eax == -ERESTARTNOHAND ||
590                     regs->eax == -ERESTARTSYS ||
591                     regs->eax == -ERESTARTNOINTR) {
592                         regs->eax = regs->orig_eax;
593                         regs->eip -= 2;
594                 }
595                 if (regs->eax == -ERESTART_RESTARTBLOCK){
596                         regs->eax = __NR_restart_syscall;
597                         regs->eip -= 2;
598                 }
599         }
600         return 0;
601 }
602
603 /*
604  * notification of userspace execution resumption
605  * - triggered by current->work.notify_resume
606  */
607 __attribute__((regparm(3)))
608 void do_notify_resume(struct pt_regs *regs, sigset_t *oldset,
609                       __u32 thread_info_flags)
610 {
611         /* Pending single-step? */
612         if (thread_info_flags & _TIF_SINGLESTEP) {
613                 regs->eflags |= TF_MASK;
614                 clear_thread_flag(TIF_SINGLESTEP);
615         }
616         /* deal with pending signal delivery */
617         if (thread_info_flags & _TIF_SIGPENDING)
618                 do_signal(regs,oldset);
619         
620         clear_thread_flag(TIF_IRET);
621 }