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