KEYS: Add missing linux/tracehook.h #inclusions
[linux-2.6.git] / arch / m32r / kernel / signal.c
1 /*
2  *  linux/arch/m32r/kernel/signal.c
3  *
4  *  Copyright (c) 2003  Hitoshi Yamamoto
5  *
6  *  Taken from i386 version.
7  *  Copyright (C) 1991, 1992  Linus Torvalds
8  *
9  *  1997-11-28  Modified for POSIX.1b signals by Richard Henderson
10  *  2000-06-20  Pentium III FXSR, SSE support by Gareth Hughes
11  */
12
13 #include <linux/sched.h>
14 #include <linux/mm.h>
15 #include <linux/smp.h>
16 #include <linux/kernel.h>
17 #include <linux/signal.h>
18 #include <linux/errno.h>
19 #include <linux/wait.h>
20 #include <linux/unistd.h>
21 #include <linux/stddef.h>
22 #include <linux/personality.h>
23 #include <linux/freezer.h>
24 #include <linux/tracehook.h>
25 #include <asm/cacheflush.h>
26 #include <asm/ucontext.h>
27 #include <asm/uaccess.h>
28
29 #define DEBUG_SIG 0
30
31 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
32
33 int do_signal(struct pt_regs *, sigset_t *);
34
35 asmlinkage int
36 sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize,
37                   unsigned long r2, unsigned long r3, unsigned long r4,
38                   unsigned long r5, unsigned long r6, struct pt_regs *regs)
39 {
40         sigset_t newset;
41
42         /* XXX: Don't preclude handling different sized sigset_t's.  */
43         if (sigsetsize != sizeof(sigset_t))
44                 return -EINVAL;
45
46         if (copy_from_user(&newset, unewset, sizeof(newset)))
47                 return -EFAULT;
48         sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
49
50         spin_lock_irq(&current->sighand->siglock);
51         current->saved_sigmask = current->blocked;
52         current->blocked = newset;
53         recalc_sigpending();
54         spin_unlock_irq(&current->sighand->siglock);
55
56         current->state = TASK_INTERRUPTIBLE;
57         schedule();
58         set_thread_flag(TIF_RESTORE_SIGMASK);
59         return -ERESTARTNOHAND;
60 }
61
62 asmlinkage int
63 sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
64                 unsigned long r2, unsigned long r3, unsigned long r4,
65                 unsigned long r5, unsigned long r6, struct pt_regs *regs)
66 {
67         return do_sigaltstack(uss, uoss, regs->spu);
68 }
69
70
71 /*
72  * Do a signal return; undo the signal stack.
73  */
74
75 struct rt_sigframe
76 {
77         int sig;
78         struct siginfo __user *pinfo;
79         void __user *puc;
80         struct siginfo info;
81         struct ucontext uc;
82 //      struct _fpstate fpstate;
83 };
84
85 static int
86 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
87                    int *r0_p)
88 {
89         unsigned int err = 0;
90
91         /* Always make any pending restarted system calls return -EINTR */
92         current_thread_info()->restart_block.fn = do_no_restart_syscall;
93
94 #define COPY(x)         err |= __get_user(regs->x, &sc->sc_##x)
95         COPY(r4);
96         COPY(r5);
97         COPY(r6);
98         COPY(pt_regs);
99         /* COPY(r0); Skip r0 */
100         COPY(r1);
101         COPY(r2);
102         COPY(r3);
103         COPY(r7);
104         COPY(r8);
105         COPY(r9);
106         COPY(r10);
107         COPY(r11);
108         COPY(r12);
109         COPY(acc0h);
110         COPY(acc0l);
111         COPY(acc1h);            /* ISA_DSP_LEVEL2 only */
112         COPY(acc1l);            /* ISA_DSP_LEVEL2 only */
113         COPY(psw);
114         COPY(bpc);
115         COPY(bbpsw);
116         COPY(bbpc);
117         COPY(spu);
118         COPY(fp);
119         COPY(lr);
120         COPY(spi);
121 #undef COPY
122
123         regs->syscall_nr = -1;  /* disable syscall checks */
124         err |= __get_user(*r0_p, &sc->sc_r0);
125
126         return err;
127 }
128
129 asmlinkage int
130 sys_rt_sigreturn(unsigned long r0, unsigned long r1,
131                  unsigned long r2, unsigned long r3, unsigned long r4,
132                  unsigned long r5, unsigned long r6, struct pt_regs *regs)
133 {
134         struct rt_sigframe __user *frame = (struct rt_sigframe __user *)regs->spu;
135         sigset_t set;
136         int result;
137
138         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
139                 goto badframe;
140         if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
141                 goto badframe;
142
143         sigdelsetmask(&set, ~_BLOCKABLE);
144         spin_lock_irq(&current->sighand->siglock);
145         current->blocked = set;
146         recalc_sigpending();
147         spin_unlock_irq(&current->sighand->siglock);
148
149         if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &result))
150                 goto badframe;
151
152         if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->spu) == -EFAULT)
153                 goto badframe;
154
155         return result;
156
157 badframe:
158         force_sig(SIGSEGV, current);
159         return 0;
160 }
161
162 /*
163  * Set up a signal frame.
164  */
165
166 static int
167 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
168                  unsigned long mask)
169 {
170         int err = 0;
171
172 #define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
173         COPY(r4);
174         COPY(r5);
175         COPY(r6);
176         COPY(pt_regs);
177         COPY(r0);
178         COPY(r1);
179         COPY(r2);
180         COPY(r3);
181         COPY(r7);
182         COPY(r8);
183         COPY(r9);
184         COPY(r10);
185         COPY(r11);
186         COPY(r12);
187         COPY(acc0h);
188         COPY(acc0l);
189         COPY(acc1h);            /* ISA_DSP_LEVEL2 only */
190         COPY(acc1l);            /* ISA_DSP_LEVEL2 only */
191         COPY(psw);
192         COPY(bpc);
193         COPY(bbpsw);
194         COPY(bbpc);
195         COPY(spu);
196         COPY(fp);
197         COPY(lr);
198         COPY(spi);
199 #undef COPY
200
201         err |= __put_user(mask, &sc->oldmask);
202
203         return err;
204 }
205
206 /*
207  * Determine which stack to use..
208  */
209 static inline void __user *
210 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
211 {
212         /* This is the X/Open sanctioned signal stack switching.  */
213         if (ka->sa.sa_flags & SA_ONSTACK) {
214                 if (sas_ss_flags(sp) == 0)
215                         sp = current->sas_ss_sp + current->sas_ss_size;
216         }
217
218         return (void __user *)((sp - frame_size) & -8ul);
219 }
220
221 static void setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
222                            sigset_t *set, struct pt_regs *regs)
223 {
224         struct rt_sigframe __user *frame;
225         int err = 0;
226         int signal;
227
228         frame = get_sigframe(ka, regs->spu, sizeof(*frame));
229
230         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
231                 goto give_sigsegv;
232
233         signal = current_thread_info()->exec_domain
234                 && current_thread_info()->exec_domain->signal_invmap
235                 && sig < 32
236                 ? current_thread_info()->exec_domain->signal_invmap[sig]
237                 : sig;
238
239         err |= __put_user(signal, &frame->sig);
240         if (err)
241                 goto give_sigsegv;
242
243         err |= __put_user(&frame->info, &frame->pinfo);
244         err |= __put_user(&frame->uc, &frame->puc);
245         err |= copy_siginfo_to_user(&frame->info, info);
246         if (err)
247                 goto give_sigsegv;
248
249         /* Create the ucontext.  */
250         err |= __put_user(0, &frame->uc.uc_flags);
251         err |= __put_user(0, &frame->uc.uc_link);
252         err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
253         err |= __put_user(sas_ss_flags(regs->spu),
254                           &frame->uc.uc_stack.ss_flags);
255         err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
256         err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
257         err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
258         if (err)
259                 goto give_sigsegv;
260
261         /* Set up to return from userspace.  */
262         regs->lr = (unsigned long)ka->sa.sa_restorer;
263
264         /* Set up registers for signal handler */
265         regs->spu = (unsigned long)frame;
266         regs->r0 = signal;      /* Arg for signal handler */
267         regs->r1 = (unsigned long)&frame->info;
268         regs->r2 = (unsigned long)&frame->uc;
269         regs->bpc = (unsigned long)ka->sa.sa_handler;
270
271         set_fs(USER_DS);
272
273 #if DEBUG_SIG
274         printk("SIG deliver (%s:%d): sp=%p pc=%p\n",
275                 current->comm, current->pid, frame, regs->pc);
276 #endif
277
278         return;
279
280 give_sigsegv:
281         force_sigsegv(sig, current);
282 }
283
284 /*
285  * OK, we're invoking a handler
286  */
287
288 static void
289 handle_signal(unsigned long sig, struct k_sigaction *ka, siginfo_t *info,
290               sigset_t *oldset, struct pt_regs *regs)
291 {
292         unsigned short inst;
293
294         /* Are we from a system call? */
295         if (regs->syscall_nr >= 0) {
296                 /* If so, check system call restarting.. */
297                 switch (regs->r0) {
298                         case -ERESTART_RESTARTBLOCK:
299                         case -ERESTARTNOHAND:
300                                 regs->r0 = -EINTR;
301                                 break;
302
303                         case -ERESTARTSYS:
304                                 if (!(ka->sa.sa_flags & SA_RESTART)) {
305                                         regs->r0 = -EINTR;
306                                         break;
307                                 }
308                         /* fallthrough */
309                         case -ERESTARTNOINTR:
310                                 regs->r0 = regs->orig_r0;
311                                 inst = *(unsigned short *)(regs->bpc - 2);
312                                 if ((inst & 0xfff0) == 0x10f0)  /* trap ? */
313                                         regs->bpc -= 2;
314                                 else
315                                         regs->bpc -= 4;
316                 }
317         }
318
319         /* Set up the stack frame */
320         setup_rt_frame(sig, ka, info, oldset, regs);
321
322         spin_lock_irq(&current->sighand->siglock);
323         sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
324         if (!(ka->sa.sa_flags & SA_NODEFER))
325                 sigaddset(&current->blocked,sig);
326         recalc_sigpending();
327         spin_unlock_irq(&current->sighand->siglock);
328 }
329
330 /*
331  * Note that 'init' is a special process: it doesn't get signals it doesn't
332  * want to handle. Thus you cannot kill init even with a SIGKILL even by
333  * mistake.
334  */
335 int do_signal(struct pt_regs *regs, sigset_t *oldset)
336 {
337         siginfo_t info;
338         int signr;
339         struct k_sigaction ka;
340         unsigned short inst;
341
342         /*
343          * We want the common case to go fast, which
344          * is why we may in certain cases get here from
345          * kernel mode. Just return without doing anything
346          * if so.
347          */
348         if (!user_mode(regs))
349                 return 1;
350
351         if (try_to_freeze()) 
352                 goto no_signal;
353
354         if (!oldset)
355                 oldset = &current->blocked;
356
357         signr = get_signal_to_deliver(&info, &ka, regs, NULL);
358         if (signr > 0) {
359                 /* Re-enable any watchpoints before delivering the
360                  * signal to user space. The processor register will
361                  * have been cleared if the watchpoint triggered
362                  * inside the kernel.
363                  */
364
365                 /* Whee!  Actually deliver the signal.  */
366                 handle_signal(signr, &ka, &info, oldset, regs);
367                 return 1;
368         }
369
370  no_signal:
371         /* Did we come from a system call? */
372         if (regs->syscall_nr >= 0) {
373                 /* Restart the system call - no handlers present */
374                 if (regs->r0 == -ERESTARTNOHAND ||
375                     regs->r0 == -ERESTARTSYS ||
376                     regs->r0 == -ERESTARTNOINTR) {
377                         regs->r0 = regs->orig_r0;
378                         inst = *(unsigned short *)(regs->bpc - 2);
379                         if ((inst & 0xfff0) == 0x10f0)  /* trap ? */
380                                 regs->bpc -= 2;
381                         else
382                                 regs->bpc -= 4;
383                 }
384                 if (regs->r0 == -ERESTART_RESTARTBLOCK){
385                         regs->r0 = regs->orig_r0;
386                         regs->r7 = __NR_restart_syscall;
387                         inst = *(unsigned short *)(regs->bpc - 2);
388                         if ((inst & 0xfff0) == 0x10f0)  /* trap ? */
389                                 regs->bpc -= 2;
390                         else
391                                 regs->bpc -= 4;
392                 }
393         }
394         return 0;
395 }
396
397 /*
398  * notification of userspace execution resumption
399  * - triggered by current->work.notify_resume
400  */
401 void do_notify_resume(struct pt_regs *regs, sigset_t *oldset,
402                       __u32 thread_info_flags)
403 {
404         /* Pending single-step? */
405         if (thread_info_flags & _TIF_SINGLESTEP)
406                 clear_thread_flag(TIF_SINGLESTEP);
407
408         /* deal with pending signal delivery */
409         if (thread_info_flags & _TIF_SIGPENDING)
410                 do_signal(regs,oldset);
411
412         if (thread_info_flags & _TIF_NOTIFY_RESUME) {
413                 clear_thread_flag(TIF_NOTIFY_RESUME);
414                 tracehook_notify_resume(regs);
415                 if (current->replacement_session_keyring)
416                         key_replace_session_keyring();
417         }
418
419         clear_thread_flag(TIF_IRET);
420 }