ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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/sim.h>
33 #include <asm/uaccess.h>
34 #include <asm/ucontext.h>
35 #include <asm/system.h>
36 #include <asm/fpu.h>
37
38 /*
39  * Including <asm/unistd.h would give use the 64-bit syscall numbers ...
40  */
41 #define __NR_N32_rt_sigreturn           6211
42 #define __NR_N32_restart_syscall        6214
43
44 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
45
46 /* IRIX compatible stack_t  */
47 typedef struct sigaltstack32 {
48         s32 ss_sp;
49         compat_size_t ss_size;
50         int ss_flags;
51 } stack32_t;
52
53 struct ucontextn32 {
54         u32                 uc_flags;
55         s32                 uc_link;
56         stack32_t           uc_stack;
57         struct sigcontext   uc_mcontext;
58         sigset_t            uc_sigmask;   /* mask last for extensibility */
59 };
60
61 struct rt_sigframe_n32 {
62         u32 rs_ass[4];                  /* argument save space for o32 */
63         u32 rs_code[2];                 /* signal trampoline */
64         struct siginfo rs_info;
65         struct ucontextn32 rs_uc;
66 };
67
68 extern asmlinkage int restore_sigcontext(struct pt_regs *regs, struct sigcontext *sc);
69 extern int inline setup_sigcontext(struct pt_regs *regs, struct sigcontext *sc);
70
71 asmlinkage void sysn32_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
72 {
73         struct rt_sigframe_n32 *frame;
74         sigset_t set;
75         stack_t st;
76         s32 sp;
77
78         frame = (struct rt_sigframe_n32 *) regs.regs[29];
79         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
80                 goto badframe;
81         if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
82                 goto badframe;
83
84         sigdelsetmask(&set, ~_BLOCKABLE);
85         spin_lock_irq(&current->sighand->siglock);
86         current->blocked = set;
87         recalc_sigpending();
88         spin_unlock_irq(&current->sighand->siglock);
89
90         if (restore_sigcontext(&regs, &frame->rs_uc.uc_mcontext))
91                 goto badframe;
92
93         /* The ucontext contains a stack32_t, so we must convert!  */
94         if (__get_user(sp, &frame->rs_uc.uc_stack.ss_sp))
95                 goto badframe;
96         st.ss_size = (long) sp;
97         if (__get_user(st.ss_size, &frame->rs_uc.uc_stack.ss_size))
98                 goto badframe;
99         if (__get_user(st.ss_flags, &frame->rs_uc.uc_stack.ss_flags))
100                 goto badframe;
101
102         /* It is more difficult to avoid calling this function than to
103            call it and ignore errors.  */
104         do_sigaltstack(&st, NULL, regs.regs[29]);
105
106         /*
107          * Don't let your children do this ...
108          */
109         __asm__ __volatile__(
110                 "move\t$29, %0\n\t"
111                 "j\tsyscall_exit"
112                 :/* no outputs */
113                 :"r" (&regs));
114         /* Unreached */
115
116 badframe:
117         force_sig(SIGSEGV, current);
118 }
119
120 /*
121  * Determine which stack to use..
122  */
123 static inline void *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
124         size_t frame_size)
125 {
126         unsigned long sp;
127
128         /* Default to using normal stack */
129         sp = regs->regs[29];
130
131         /*
132          * FPU emulator may have it's own trampoline active just
133          * above the user stack, 16-bytes before the next lowest
134          * 16 byte boundary.  Try to avoid trashing it.
135          */
136         sp -= 32;
137
138         /* This is the X/Open sanctioned signal stack switching.  */
139         if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags (sp) == 0))
140                 sp = current->sas_ss_sp + current->sas_ss_size;
141
142         return (void *)((sp - frame_size) & ALMASK);
143 }
144
145 void setup_rt_frame_n32(struct k_sigaction * ka,
146         struct pt_regs *regs, int signr, sigset_t *set, siginfo_t *info)
147 {
148         struct rt_sigframe_n32 *frame;
149         int err = 0;
150         s32 sp;
151
152         frame = get_sigframe(ka, regs, sizeof(*frame));
153         if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
154                 goto give_sigsegv;
155
156         /*
157          * Set up the return code ...
158          *
159          *         li      v0, __NR_rt_sigreturn
160          *         syscall
161          */
162         err |= __put_user(0x24020000 + __NR_N32_rt_sigreturn, frame->rs_code + 0);
163         err |= __put_user(0x0000000c                    , frame->rs_code + 1);
164         flush_cache_sigtramp((unsigned long) frame->rs_code);
165
166         /* Create siginfo.  */
167         err |= copy_siginfo_to_user(&frame->rs_info, info);
168
169         /* Create the ucontext.  */
170         err |= __put_user(0, &frame->rs_uc.uc_flags);
171         err |= __put_user(0, &frame->rs_uc.uc_link);
172         sp = (int) (long) current->sas_ss_sp;
173         err |= __put_user(sp,
174                           &frame->rs_uc.uc_stack.ss_sp);
175         err |= __put_user(sas_ss_flags(regs->regs[29]),
176                           &frame->rs_uc.uc_stack.ss_flags);
177         err |= __put_user(current->sas_ss_size,
178                           &frame->rs_uc.uc_stack.ss_size);
179         err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
180         err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
181
182         if (err)
183                 goto give_sigsegv;
184
185         /*
186          * Arguments to signal handler:
187          *
188          *   a0 = signal number
189          *   a1 = 0 (should be cause)
190          *   a2 = pointer to ucontext
191          *
192          * $25 and c0_epc point to the signal handler, $29 points to
193          * the struct rt_sigframe.
194          */
195         regs->regs[ 4] = signr;
196         regs->regs[ 5] = (unsigned long) &frame->rs_info;
197         regs->regs[ 6] = (unsigned long) &frame->rs_uc;
198         regs->regs[29] = (unsigned long) frame;
199         regs->regs[31] = (unsigned long) frame->rs_code;
200         regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
201
202 #if DEBUG_SIG
203         printk("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%p\n",
204                current->comm, current->pid,
205                frame, regs->cp0_epc, regs->regs[31]);
206 #endif
207         return;
208
209 give_sigsegv:
210         if (signr == SIGSEGV)
211                 ka->sa.sa_handler = SIG_DFL;
212         force_sig(SIGSEGV, current);
213 }