patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / mips / kernel / signal_n32.c
1 /*
2  * Copyright (C) 2003 Broadcom Corporation
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18 #include <linux/sched.h>
19 #include <linux/mm.h>
20 #include <linux/smp.h>
21 #include <linux/smp_lock.h>
22 #include <linux/kernel.h>
23 #include <linux/signal.h>
24 #include <linux/errno.h>
25 #include <linux/wait.h>
26 #include <linux/ptrace.h>
27 #include <linux/unistd.h>
28 #include <linux/compat.h>
29
30 #include <asm/asm.h>
31 #include <asm/bitops.h>
32 #include <asm/cacheflush.h>
33 #include <asm/sim.h>
34 #include <asm/uaccess.h>
35 #include <asm/ucontext.h>
36 #include <asm/system.h>
37 #include <asm/fpu.h>
38
39 /*
40  * Including <asm/unistd.h would give use the 64-bit syscall numbers ...
41  */
42 #define __NR_N32_rt_sigreturn           6211
43 #define __NR_N32_restart_syscall        6214
44
45 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
46
47 /* IRIX compatible stack_t  */
48 typedef struct sigaltstack32 {
49         s32 ss_sp;
50         compat_size_t ss_size;
51         int ss_flags;
52 } stack32_t;
53
54 struct ucontextn32 {
55         u32                 uc_flags;
56         s32                 uc_link;
57         stack32_t           uc_stack;
58         struct sigcontext   uc_mcontext;
59         sigset_t            uc_sigmask;   /* mask last for extensibility */
60 };
61
62 struct rt_sigframe_n32 {
63         u32 rs_ass[4];                  /* argument save space for o32 */
64         u32 rs_code[2];                 /* signal trampoline */
65         struct siginfo rs_info;
66         struct ucontextn32 rs_uc;
67 };
68
69 extern asmlinkage int restore_sigcontext(struct pt_regs *regs, struct sigcontext *sc);
70 extern int inline setup_sigcontext(struct pt_regs *regs, struct sigcontext *sc);
71
72 asmlinkage void sysn32_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
73 {
74         struct rt_sigframe_n32 *frame;
75         sigset_t set;
76         stack_t st;
77         s32 sp;
78
79         frame = (struct rt_sigframe_n32 *) regs.regs[29];
80         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
81                 goto badframe;
82         if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
83                 goto badframe;
84
85         sigdelsetmask(&set, ~_BLOCKABLE);
86         spin_lock_irq(&current->sighand->siglock);
87         current->blocked = set;
88         recalc_sigpending();
89         spin_unlock_irq(&current->sighand->siglock);
90
91         if (restore_sigcontext(&regs, &frame->rs_uc.uc_mcontext))
92                 goto badframe;
93
94         /* The ucontext contains a stack32_t, so we must convert!  */
95         if (__get_user(sp, &frame->rs_uc.uc_stack.ss_sp))
96                 goto badframe;
97         st.ss_size = (long) sp;
98         if (__get_user(st.ss_size, &frame->rs_uc.uc_stack.ss_size))
99                 goto badframe;
100         if (__get_user(st.ss_flags, &frame->rs_uc.uc_stack.ss_flags))
101                 goto badframe;
102
103         /* It is more difficult to avoid calling this function than to
104            call it and ignore errors.  */
105         do_sigaltstack(&st, NULL, regs.regs[29]);
106
107         /*
108          * Don't let your children do this ...
109          */
110         __asm__ __volatile__(
111                 "move\t$29, %0\n\t"
112                 "j\tsyscall_exit"
113                 :/* no outputs */
114                 :"r" (&regs));
115         /* Unreached */
116
117 badframe:
118         force_sig(SIGSEGV, current);
119 }
120
121 /*
122  * Determine which stack to use..
123  */
124 static inline void *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
125         size_t frame_size)
126 {
127         unsigned long sp;
128
129         /* Default to using normal stack */
130         sp = regs->regs[29];
131
132         /*
133          * FPU emulator may have it's own trampoline active just
134          * above the user stack, 16-bytes before the next lowest
135          * 16 byte boundary.  Try to avoid trashing it.
136          */
137         sp -= 32;
138
139         /* This is the X/Open sanctioned signal stack switching.  */
140         if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags (sp) == 0))
141                 sp = current->sas_ss_sp + current->sas_ss_size;
142
143         return (void *)((sp - frame_size) & ALMASK);
144 }
145
146 void setup_rt_frame_n32(struct k_sigaction * ka,
147         struct pt_regs *regs, int signr, sigset_t *set, siginfo_t *info)
148 {
149         struct rt_sigframe_n32 *frame;
150         int err = 0;
151         s32 sp;
152
153         frame = get_sigframe(ka, regs, sizeof(*frame));
154         if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
155                 goto give_sigsegv;
156
157         /*
158          * Set up the return code ...
159          *
160          *         li      v0, __NR_rt_sigreturn
161          *         syscall
162          */
163         err |= __put_user(0x24020000 + __NR_N32_rt_sigreturn, frame->rs_code + 0);
164         err |= __put_user(0x0000000c                    , frame->rs_code + 1);
165         flush_cache_sigtramp((unsigned long) frame->rs_code);
166
167         /* Create siginfo.  */
168         err |= copy_siginfo_to_user(&frame->rs_info, info);
169
170         /* Create the ucontext.  */
171         err |= __put_user(0, &frame->rs_uc.uc_flags);
172         err |= __put_user(0, &frame->rs_uc.uc_link);
173         sp = (int) (long) current->sas_ss_sp;
174         err |= __put_user(sp,
175                           &frame->rs_uc.uc_stack.ss_sp);
176         err |= __put_user(sas_ss_flags(regs->regs[29]),
177                           &frame->rs_uc.uc_stack.ss_flags);
178         err |= __put_user(current->sas_ss_size,
179                           &frame->rs_uc.uc_stack.ss_size);
180         err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
181         err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
182
183         if (err)
184                 goto give_sigsegv;
185
186         /*
187          * Arguments to signal handler:
188          *
189          *   a0 = signal number
190          *   a1 = 0 (should be cause)
191          *   a2 = pointer to ucontext
192          *
193          * $25 and c0_epc point to the signal handler, $29 points to
194          * the struct rt_sigframe.
195          */
196         regs->regs[ 4] = signr;
197         regs->regs[ 5] = (unsigned long) &frame->rs_info;
198         regs->regs[ 6] = (unsigned long) &frame->rs_uc;
199         regs->regs[29] = (unsigned long) frame;
200         regs->regs[31] = (unsigned long) frame->rs_code;
201         regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
202
203 #if DEBUG_SIG
204         printk("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%p\n",
205                current->comm, current->pid,
206                frame, regs->cp0_epc, regs->regs[31]);
207 #endif
208         return;
209
210 give_sigsegv:
211         if (signr == SIGSEGV)
212                 ka->sa.sa_handler = SIG_DFL;
213         force_sig(SIGSEGV, current);
214 }