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