- patches.fixes/patch-2.6.11-rc1: 2.6.11-rc1.
[linux-flexiantxendom0-3.2.10.git] / arch / frv / kernel / signal.c
1 /* signal.c: FRV specific bits of signal handling
2  *
3  * Copyright (C) 2003-5 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  * - Derived from arch/m68k/kernel/signal.c
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version
10  * 2 of the License, or (at your option) any later version.
11  */
12
13 #include <linux/sched.h>
14 #include <linux/mm.h>
15 #include <linux/smp.h>
16 #include <linux/smp_lock.h>
17 #include <linux/kernel.h>
18 #include <linux/signal.h>
19 #include <linux/errno.h>
20 #include <linux/wait.h>
21 #include <linux/ptrace.h>
22 #include <linux/unistd.h>
23 #include <linux/personality.h>
24 #include <linux/suspend.h>
25 #include <asm/ucontext.h>
26 #include <asm/uaccess.h>
27 #include <asm/cacheflush.h>
28
29 #define DEBUG_SIG 0
30
31 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
32
33 struct fdpic_func_descriptor {
34         unsigned long   text;
35         unsigned long   GOT;
36 };
37
38 asmlinkage int do_signal(struct pt_regs *regs, sigset_t *oldset);
39
40 /*
41  * Atomically swap in the new signal mask, and wait for a signal.
42  */
43 asmlinkage int sys_sigsuspend(int history0, int history1, old_sigset_t mask)
44 {
45         sigset_t saveset;
46
47         mask &= _BLOCKABLE;
48         spin_lock_irq(&current->sighand->siglock);
49         saveset = current->blocked;
50         siginitset(&current->blocked, mask);
51         recalc_sigpending();
52         spin_unlock_irq(&current->sighand->siglock);
53
54         __frame->gr8 = -EINTR;
55         while (1) {
56                 current->state = TASK_INTERRUPTIBLE;
57                 schedule();
58                 if (do_signal(__frame, &saveset))
59                         /* return the signal number as the return value of this function
60                          * - this is an utterly evil hack. syscalls should not invoke do_signal()
61                          *   as entry.S sets regs->gr8 to the return value of the system call
62                          * - we can't just use sigpending() as we'd have to discard SIG_IGN signals
63                          *   and call waitpid() if SIGCHLD needed discarding
64                          * - this only works on the i386 because it passes arguments to the signal
65                          *   handler on the stack, and the return value in EAX is effectively
66                          *   discarded
67                          */
68                         return __frame->gr8;
69         }
70 }
71
72 asmlinkage int sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize)
73 {
74         sigset_t saveset, newset;
75
76         /* XXX: Don't preclude handling different sized sigset_t's.  */
77         if (sigsetsize != sizeof(sigset_t))
78                 return -EINVAL;
79
80         if (copy_from_user(&newset, unewset, sizeof(newset)))
81                 return -EFAULT;
82         sigdelsetmask(&newset, ~_BLOCKABLE);
83
84         spin_lock_irq(&current->sighand->siglock);
85         saveset = current->blocked;
86         current->blocked = newset;
87         recalc_sigpending();
88         spin_unlock_irq(&current->sighand->siglock);
89
90         __frame->gr8 = -EINTR;
91         while (1) {
92                 current->state = TASK_INTERRUPTIBLE;
93                 schedule();
94                 if (do_signal(__frame, &saveset))
95                         /* return the signal number as the return value of this function
96                          * - this is an utterly evil hack. syscalls should not invoke do_signal()
97                          *   as entry.S sets regs->gr8 to the return value of the system call
98                          * - we can't just use sigpending() as we'd have to discard SIG_IGN signals
99                          *   and call waitpid() if SIGCHLD needed discarding
100                          * - this only works on the i386 because it passes arguments to the signal
101                          *   handler on the stack, and the return value in EAX is effectively
102                          *   discarded
103                          */
104                         return __frame->gr8;
105         }
106 }
107
108 asmlinkage int sys_sigaction(int sig,
109                              const struct old_sigaction __user *act,
110                              struct old_sigaction __user *oact)
111 {
112         struct k_sigaction new_ka, old_ka;
113         int ret;
114
115         if (act) {
116                 old_sigset_t mask;
117                 if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
118                     __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
119                     __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
120                         return -EFAULT;
121                 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
122                 __get_user(mask, &act->sa_mask);
123                 siginitset(&new_ka.sa.sa_mask, mask);
124         }
125
126         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
127
128         if (!ret && oact) {
129                 if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
130                     __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
131                     __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
132                         return -EFAULT;
133                 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
134                 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
135         }
136
137         return ret;
138 }
139
140 asmlinkage
141 int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
142 {
143         return do_sigaltstack(uss, uoss, __frame->sp);
144 }
145
146
147 /*
148  * Do a signal return; undo the signal stack.
149  */
150
151 struct sigframe
152 {
153         void (*pretcode)(void);
154         int sig;
155         struct sigcontext sc;
156         unsigned long extramask[_NSIG_WORDS-1];
157         uint32_t retcode[2];
158 };
159
160 struct rt_sigframe
161 {
162         void (*pretcode)(void);
163         int sig;
164         struct siginfo *pinfo;
165         void *puc;
166         struct siginfo info;
167         struct ucontext uc;
168         uint32_t retcode[2];
169 };
170
171 static int restore_sigcontext(struct sigcontext __user *sc, int *_gr8)
172 {
173         struct user_context *user = current->thread.user;
174         unsigned long tbr, psr;
175
176         tbr = user->i.tbr;
177         psr = user->i.psr;
178         if (copy_from_user(user, &sc->sc_context, sizeof(sc->sc_context)))
179                 goto badframe;
180         user->i.tbr = tbr;
181         user->i.psr = psr;
182
183         restore_user_regs(user);
184
185         user->i.syscallno = -1;         /* disable syscall checks */
186
187         *_gr8 = user->i.gr[8];
188         return 0;
189
190  badframe:
191         return 1;
192 }
193
194 asmlinkage int sys_sigreturn(void)
195 {
196         struct sigframe __user *frame = (struct sigframe __user *) __frame->sp;
197         sigset_t set;
198         int gr8;
199
200         if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
201                 goto badframe;
202         if (__get_user(set.sig[0], &frame->sc.sc_oldmask))
203                 goto badframe;
204
205         if (_NSIG_WORDS > 1 &&
206             __copy_from_user(&set.sig[1], &frame->extramask, sizeof(frame->extramask)))
207                 goto badframe;
208
209         sigdelsetmask(&set, ~_BLOCKABLE);
210         spin_lock_irq(&current->sighand->siglock);
211         current->blocked = set;
212         recalc_sigpending();
213         spin_unlock_irq(&current->sighand->siglock);
214
215         if (restore_sigcontext(&frame->sc, &gr8))
216                 goto badframe;
217         return gr8;
218
219  badframe:
220         force_sig(SIGSEGV, current);
221         return 0;
222 }
223
224 asmlinkage int sys_rt_sigreturn(void)
225 {
226         struct rt_sigframe __user *frame = (struct rt_sigframe __user *) __frame->sp;
227         sigset_t set;
228         stack_t st;
229         int gr8;
230
231         if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
232                 goto badframe;
233         if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
234                 goto badframe;
235
236         sigdelsetmask(&set, ~_BLOCKABLE);
237         spin_lock_irq(&current->sighand->siglock);
238         current->blocked = set;
239         recalc_sigpending();
240         spin_unlock_irq(&current->sighand->siglock);
241
242         if (restore_sigcontext(&frame->uc.uc_mcontext, &gr8))
243                 goto badframe;
244
245         if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
246                 goto badframe;
247
248         /* It is more difficult to avoid calling this function than to
249          * call it and ignore errors.  */
250         /*
251          * THIS CANNOT WORK! "&st" is a kernel address, and "do_sigaltstack()"
252          * takes a user address (and verifies that it is a user address). End
253          * result: it does exactly _nothing_.
254          */
255         do_sigaltstack(&st, NULL, __frame->sp);
256
257         return gr8;
258
259 badframe:
260         force_sig(SIGSEGV, current);
261         return 0;
262 }
263
264 /*
265  * Set up a signal frame
266  */
267 static int setup_sigcontext(struct sigcontext __user *sc, unsigned long mask)
268 {
269         save_user_regs(current->thread.user);
270
271         if (copy_to_user(&sc->sc_context, current->thread.user, sizeof(sc->sc_context)) != 0)
272                 goto badframe;
273
274         /* non-iBCS2 extensions.. */
275         if (__put_user(mask, &sc->sc_oldmask) < 0)
276                 goto badframe;
277
278         return 0;
279
280  badframe:
281         return 1;
282 }
283
284 /*****************************************************************************/
285 /*
286  * Determine which stack to use..
287  */
288 static inline void __user *get_sigframe(struct k_sigaction *ka,
289                                         struct pt_regs *regs,
290                                         size_t frame_size)
291 {
292         unsigned long sp;
293
294         /* Default to using normal stack */
295         sp = regs->sp;
296
297         /* This is the X/Open sanctioned signal stack switching.  */
298         if (ka->sa.sa_flags & SA_ONSTACK) {
299                 if (! on_sig_stack(sp))
300                         sp = current->sas_ss_sp + current->sas_ss_size;
301         }
302
303         return (void __user *) ((sp - frame_size) & ~7UL);
304 } /* end get_sigframe() */
305
306 /*****************************************************************************/
307 /*
308  *
309  */
310 static void setup_frame(int sig, struct k_sigaction *ka, sigset_t *set, struct pt_regs * regs)
311 {
312         struct sigframe __user *frame;
313         int rsig;
314
315         frame = get_sigframe(ka, regs, sizeof(*frame));
316
317         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
318                 goto give_sigsegv;
319
320         rsig = sig;
321         if (sig < 32 &&
322             __current_thread_info->exec_domain &&
323             __current_thread_info->exec_domain->signal_invmap)
324                 rsig = __current_thread_info->exec_domain->signal_invmap[sig];
325
326         if (__put_user(rsig, &frame->sig) < 0)
327                 goto give_sigsegv;
328
329         if (setup_sigcontext(&frame->sc, set->sig[0]))
330                 goto give_sigsegv;
331
332         if (_NSIG_WORDS > 1) {
333                 if (__copy_to_user(frame->extramask, &set->sig[1],
334                                    sizeof(frame->extramask)))
335                         goto give_sigsegv;
336         }
337
338         /* Set up to return from userspace.  If provided, use a stub
339          * already in userspace.  */
340         if (ka->sa.sa_flags & SA_RESTORER) {
341                 if (__put_user(ka->sa.sa_restorer, &frame->pretcode) < 0)
342                         goto give_sigsegv;
343         }
344         else {
345                 /* Set up the following code on the stack:
346                  *      setlos  #__NR_sigreturn,gr7
347                  *      tira    gr0,0
348                  */
349                 if (__put_user((void (*)(void))frame->retcode, &frame->pretcode) ||
350                     __put_user(0x8efc0000|__NR_sigreturn, &frame->retcode[0]) ||
351                     __put_user(0xc0700000, &frame->retcode[1]))
352                         goto give_sigsegv;
353
354                 flush_icache_range((unsigned long) frame->retcode,
355                                    (unsigned long) (frame->retcode + 2));
356         }
357
358         /* set up registers for signal handler */
359         regs->sp   = (unsigned long) frame;
360         regs->lr   = (unsigned long) &frame->retcode;
361         regs->gr8  = sig;
362
363         if (get_personality & FDPIC_FUNCPTRS) {
364                 struct fdpic_func_descriptor __user *funcptr =
365                         (struct fdpic_func_descriptor *) ka->sa.sa_handler;
366                 __get_user(regs->pc, &funcptr->text);
367                 __get_user(regs->gr15, &funcptr->GOT);
368         } else {
369                 regs->pc   = (unsigned long) ka->sa.sa_handler;
370                 regs->gr15 = 0;
371         }
372
373         set_fs(USER_DS);
374
375 #if DEBUG_SIG
376         printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
377                 sig, current->comm, current->pid, frame, regs->pc, frame->pretcode);
378 #endif
379
380         return;
381
382 give_sigsegv:
383         if (sig == SIGSEGV)
384                 ka->sa.sa_handler = SIG_DFL;
385
386         force_sig(SIGSEGV, current);
387 } /* end setup_frame() */
388
389 /*****************************************************************************/
390 /*
391  *
392  */
393 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
394                            sigset_t *set, struct pt_regs * regs)
395 {
396         struct rt_sigframe __user *frame;
397         int rsig;
398
399         frame = get_sigframe(ka, regs, sizeof(*frame));
400
401         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
402                 goto give_sigsegv;
403
404         rsig = sig;
405         if (sig < 32 &&
406             __current_thread_info->exec_domain &&
407             __current_thread_info->exec_domain->signal_invmap)
408                 rsig = __current_thread_info->exec_domain->signal_invmap[sig];
409
410         if (__put_user(rsig,            &frame->sig) ||
411             __put_user(&frame->info,    &frame->pinfo) ||
412             __put_user(&frame->uc,      &frame->puc))
413                 goto give_sigsegv;
414
415         if (copy_siginfo_to_user(&frame->info, info))
416                 goto give_sigsegv;
417
418         /* Create the ucontext.  */
419         if (__put_user(0, &frame->uc.uc_flags) ||
420             __put_user(0, &frame->uc.uc_link) ||
421             __put_user((void*)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp) ||
422             __put_user(sas_ss_flags(regs->sp), &frame->uc.uc_stack.ss_flags) ||
423             __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size))
424                 goto give_sigsegv;
425
426         if (setup_sigcontext(&frame->uc.uc_mcontext, set->sig[0]))
427                 goto give_sigsegv;
428
429         if (__copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)))
430                 goto give_sigsegv;
431
432         /* Set up to return from userspace.  If provided, use a stub
433          * already in userspace.  */
434         if (ka->sa.sa_flags & SA_RESTORER) {
435                 if (__put_user(ka->sa.sa_restorer, &frame->pretcode))
436                         goto give_sigsegv;
437         }
438         else {
439                 /* Set up the following code on the stack:
440                  *      setlos  #__NR_sigreturn,gr7
441                  *      tira    gr0,0
442                  */
443                 if (__put_user((void (*)(void))frame->retcode, &frame->pretcode) ||
444                     __put_user(0x8efc0000|__NR_rt_sigreturn, &frame->retcode[0]) ||
445                     __put_user(0xc0700000, &frame->retcode[1]))
446                         goto give_sigsegv;
447
448                 flush_icache_range((unsigned long) frame->retcode,
449                                    (unsigned long) (frame->retcode + 2));
450         }
451
452         /* Set up registers for signal handler */
453         regs->sp  = (unsigned long) frame;
454         regs->lr   = (unsigned long) &frame->retcode;
455         regs->gr8 = sig;
456         regs->gr9 = (unsigned long) &frame->info;
457
458         if (get_personality & FDPIC_FUNCPTRS) {
459                 struct fdpic_func_descriptor *funcptr =
460                         (struct fdpic_func_descriptor __user *) ka->sa.sa_handler;
461                 __get_user(regs->pc, &funcptr->text);
462                 __get_user(regs->gr15, &funcptr->GOT);
463         } else {
464                 regs->pc   = (unsigned long) ka->sa.sa_handler;
465                 regs->gr15 = 0;
466         }
467
468         set_fs(USER_DS);
469
470 #if DEBUG_SIG
471         printk("SIG deliver %d (%s:%d): sp=%p pc=%lx ra=%p\n",
472                 sig, current->comm, current->pid, frame, regs->pc, frame->pretcode);
473 #endif
474
475         return;
476
477 give_sigsegv:
478         if (sig == SIGSEGV)
479                 ka->sa.sa_handler = SIG_DFL;
480         force_sig(SIGSEGV, current);
481
482 } /* end setup_rt_frame() */
483
484 /*****************************************************************************/
485 /*
486  * OK, we're invoking a handler
487  */
488 static void handle_signal(unsigned long sig, siginfo_t *info,
489                           struct k_sigaction *ka, sigset_t *oldset,
490                           struct pt_regs *regs)
491 {
492         /* Are we from a system call? */
493         if (in_syscall(regs)) {
494                 /* If so, check system call restarting.. */
495                 switch (regs->gr8) {
496                 case -ERESTART_RESTARTBLOCK:
497                 case -ERESTARTNOHAND:
498                         regs->gr8 = -EINTR;
499                         break;
500
501                 case -ERESTARTSYS:
502                         if (!(ka->sa.sa_flags & SA_RESTART)) {
503                                 regs->gr8 = -EINTR;
504                                 break;
505                         }
506                         /* fallthrough */
507                 case -ERESTARTNOINTR:
508                         regs->gr8 = regs->orig_gr8;
509                         regs->pc -= 4;
510                 }
511         }
512
513         /* Set up the stack frame */
514         if (ka->sa.sa_flags & SA_SIGINFO)
515                 setup_rt_frame(sig, ka, info, oldset, regs);
516         else
517                 setup_frame(sig, ka, oldset, regs);
518
519         if (!(ka->sa.sa_flags & SA_NODEFER)) {
520                 spin_lock_irq(&current->sighand->siglock);
521                 sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
522                 sigaddset(&current->blocked, sig);
523                 recalc_sigpending();
524                 spin_unlock_irq(&current->sighand->siglock);
525         }
526 } /* end handle_signal() */
527
528 /*****************************************************************************/
529 /*
530  * Note that 'init' is a special process: it doesn't get signals it doesn't
531  * want to handle. Thus you cannot kill init even with a SIGKILL even by
532  * mistake.
533  */
534 int do_signal(struct pt_regs *regs, sigset_t *oldset)
535 {
536         struct k_sigaction ka;
537         siginfo_t info;
538         int signr;
539
540         /*
541          * We want the common case to go fast, which
542          * is why we may in certain cases get here from
543          * kernel mode. Just return without doing anything
544          * if so.
545          */
546         if (!user_mode(regs))
547                 return 1;
548
549         if (current->flags & PF_FREEZE) {
550                 refrigerator(0);
551                 goto no_signal;
552         }
553
554         if (!oldset)
555                 oldset = &current->blocked;
556
557         signr = get_signal_to_deliver(&info, &ka, regs, NULL);
558         if (signr > 0) {
559                 handle_signal(signr, &info, &ka, oldset, regs);
560                 return 1;
561         }
562
563  no_signal:
564         /* Did we come from a system call? */
565         if (regs->syscallno >= 0) {
566                 /* Restart the system call - no handlers present */
567                 if (regs->gr8 == -ERESTARTNOHAND ||
568                     regs->gr8 == -ERESTARTSYS ||
569                     regs->gr8 == -ERESTARTNOINTR) {
570                         regs->gr8 = regs->orig_gr8;
571                         regs->pc -= 4;
572                 }
573
574                 if (regs->gr8 == -ERESTART_RESTARTBLOCK){
575                         regs->gr8 = __NR_restart_syscall;
576                         regs->pc -= 4;
577                 }
578         }
579
580         return 0;
581 } /* end do_signal() */
582
583 /*****************************************************************************/
584 /*
585  * notification of userspace execution resumption
586  * - triggered by current->work.notify_resume
587  */
588 asmlinkage void do_notify_resume(__u32 thread_info_flags)
589 {
590         /* pending single-step? */
591         if (thread_info_flags & _TIF_SINGLESTEP)
592                 clear_thread_flag(TIF_SINGLESTEP);
593
594         /* deal with pending signal delivery */
595         if (thread_info_flags & _TIF_SIGPENDING)
596                 do_signal(__frame, NULL);
597
598 } /* end do_notify_resume() */