aboutsummaryrefslogtreecommitdiffstats
path: root/arch/score/kernel/signal.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/score/kernel/signal.c')
-rw-r--r--arch/score/kernel/signal.c355
1 files changed, 355 insertions, 0 deletions
diff --git a/arch/score/kernel/signal.c b/arch/score/kernel/signal.c
new file mode 100644
index 00000000000..b4ed1b3f807
--- /dev/null
+++ b/arch/score/kernel/signal.c
@@ -0,0 +1,355 @@
1/*
2 * arch/score/kernel/signal.c
3 *
4 * Score Processor version.
5 *
6 * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
7 * Chen Liqin <liqin.chen@sunplusct.com>
8 * Lennox Wu <lennox.wu@sunplusct.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see the file COPYING, or write
22 * to the Free Software Foundation, Inc.,
23 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#include <linux/errno.h>
27#include <linux/signal.h>
28#include <linux/unistd.h>
29#include <linux/uaccess.h>
30#include <asm-generic/ucontext.h>
31
32#include <asm/cacheflush.h>
33
34#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
35
36struct rt_sigframe {
37 u32 rs_ass[4]; /* argument save space */
38 u32 rs_code[2]; /* signal trampoline */
39 struct siginfo rs_info;
40 struct ucontext rs_uc;
41};
42
43int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
44{
45 int err = 0;
46 unsigned long reg;
47
48 reg = regs->cp0_epc; err |= __put_user(reg, &sc->sc_pc);
49 err |= __put_user(regs->cp0_psr, &sc->sc_psr);
50 err |= __put_user(regs->cp0_condition, &sc->sc_condition);
51
52
53#define save_gp_reg(i) { \
54 reg = regs->regs[i]; \
55 err |= __put_user(reg, &sc->sc_regs[i]); \
56} while (0)
57 save_gp_reg(0); save_gp_reg(1); save_gp_reg(2);
58 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5);
59 save_gp_reg(6); save_gp_reg(7); save_gp_reg(8);
60 save_gp_reg(9); save_gp_reg(10); save_gp_reg(11);
61 save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
62 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17);
63 save_gp_reg(18); save_gp_reg(19); save_gp_reg(20);
64 save_gp_reg(21); save_gp_reg(22); save_gp_reg(23);
65 save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
66 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29);
67#undef save_gp_reg
68
69 reg = regs->ceh; err |= __put_user(reg, &sc->sc_mdceh);
70 reg = regs->cel; err |= __put_user(reg, &sc->sc_mdcel);
71 err |= __put_user(regs->cp0_ecr, &sc->sc_ecr);
72 err |= __put_user(regs->cp0_ema, &sc->sc_ema);
73
74 return err;
75}
76
77int restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
78{
79 int err = 0;
80 u32 reg;
81
82 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
83 err |= __get_user(regs->cp0_condition, &sc->sc_condition);
84
85 err |= __get_user(reg, &sc->sc_mdceh);
86 regs->ceh = (int) reg;
87 err |= __get_user(reg, &sc->sc_mdcel);
88 regs->cel = (int) reg;
89
90 err |= __get_user(reg, &sc->sc_psr);
91 regs->cp0_psr = (int) reg;
92 err |= __get_user(reg, &sc->sc_ecr);
93 regs->cp0_ecr = (int) reg;
94 err |= __get_user(reg, &sc->sc_ema);
95 regs->cp0_ema = (int) reg;
96
97#define restore_gp_reg(i) do { \
98 err |= __get_user(reg, &sc->sc_regs[i]); \
99 regs->regs[i] = reg; \
100} while (0)
101 restore_gp_reg(0); restore_gp_reg(1); restore_gp_reg(2);
102 restore_gp_reg(3); restore_gp_reg(4); restore_gp_reg(5);
103 restore_gp_reg(6); restore_gp_reg(7); restore_gp_reg(8);
104 restore_gp_reg(9); restore_gp_reg(10); restore_gp_reg(11);
105 restore_gp_reg(12); restore_gp_reg(13); restore_gp_reg(14);
106 restore_gp_reg(15); restore_gp_reg(16); restore_gp_reg(17);
107 restore_gp_reg(18); restore_gp_reg(19); restore_gp_reg(20);
108 restore_gp_reg(21); restore_gp_reg(22); restore_gp_reg(23);
109 restore_gp_reg(24); restore_gp_reg(25); restore_gp_reg(26);
110 restore_gp_reg(27); restore_gp_reg(28); restore_gp_reg(29);
111#undef restore_gp_reg
112
113 return err;
114}
115
116/*
117 * Determine which stack to use..
118 */
119void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
120 size_t frame_size)
121{
122 unsigned long sp;
123
124 /* Default to using normal stack */
125 sp = regs->regs[0];
126 sp -= 32;
127
128 /* This is the X/Open sanctioned signal stack switching. */
129 if ((ka->sa.sa_flags & SA_ONSTACK) && (!on_sig_stack(sp)))
130 sp = current->sas_ss_sp + current->sas_ss_size;
131
132 return (void *)((sp - frame_size) & ~7);
133}
134
135asmlinkage int score_sigaltstack(struct pt_regs *regs)
136{
137 const stack_t *uss = (const stack_t *) regs->regs[4];
138 stack_t *uoss = (stack_t *) regs->regs[5];
139 unsigned long usp = regs->regs[0];
140
141 return do_sigaltstack(uss, uoss, usp);
142}
143
144asmlinkage void score_rt_sigreturn(struct pt_regs *regs)
145{
146 struct rt_sigframe __user *frame;
147 sigset_t set;
148 stack_t st;
149 int sig;
150
151 frame = (struct rt_sigframe __user *) regs->regs[0];
152 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
153 goto badframe;
154 if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
155 goto badframe;
156
157 sigdelsetmask(&set, ~_BLOCKABLE);
158 spin_lock_irq(&current->sighand->siglock);
159 current->blocked = set;
160 recalc_sigpending();
161 spin_unlock_irq(&current->sighand->siglock);
162
163 sig = restore_sigcontext(regs, &frame->rs_uc.uc_mcontext);
164 if (sig < 0)
165 goto badframe;
166 else if (sig)
167 force_sig(sig, current);
168
169 if (__copy_from_user(&st, &frame->rs_uc.uc_stack, sizeof(st)))
170 goto badframe;
171
172 /* It is more difficult to avoid calling this function than to
173 call it and ignore errors. */
174 do_sigaltstack((stack_t __user *)&st, NULL, regs->regs[0]);
175
176 __asm__ __volatile__(
177 "mv\tr0, %0\n\t"
178 "la\tr8, syscall_exit\n\t"
179 "br\tr8\n\t"
180 : : "r" (regs) : "r8");
181
182badframe:
183 force_sig(SIGSEGV, current);
184}
185
186int setup_rt_frame(struct k_sigaction *ka, struct pt_regs *regs,
187 int signr, sigset_t *set, siginfo_t *info)
188{
189 struct rt_sigframe *frame;
190 int err = 0;
191
192 frame = get_sigframe(ka, regs, sizeof(*frame));
193 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
194 goto give_sigsegv;
195
196 /*
197 * Set up the return code ...
198 *
199 * li v0, __NR_rt_sigreturn
200 * syscall
201 */
202 err |= __put_user(0x87788000 + __NR_rt_sigreturn*2,
203 frame->rs_code + 0);
204 err |= __put_user(0x80008002, frame->rs_code + 1);
205 flush_cache_sigtramp((unsigned long) frame->rs_code);
206
207 err |= copy_siginfo_to_user(&frame->rs_info, info);
208 err |= __put_user(0, &frame->rs_uc.uc_flags);
209 err |= __put_user(0, &frame->rs_uc.uc_link);
210 err |= __put_user((void *)current->sas_ss_sp,
211 &frame->rs_uc.uc_stack.ss_sp);
212 err |= __put_user(sas_ss_flags(regs->regs[0]),
213 &frame->rs_uc.uc_stack.ss_flags);
214 err |= __put_user(current->sas_ss_size,
215 &frame->rs_uc.uc_stack.ss_size);
216 err |= setup_sigcontext(regs, &frame->rs_uc.uc_mcontext);
217 err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
218
219 if (err)
220 goto give_sigsegv;
221
222 regs->regs[0] = (unsigned long) frame;
223 regs->regs[3] = (unsigned long) frame->rs_code;
224 regs->regs[4] = signr;
225 regs->regs[5] = (unsigned long) &frame->rs_info;
226 regs->regs[6] = (unsigned long) &frame->rs_uc;
227 regs->regs[29] = (unsigned long) ka->sa.sa_handler;
228 regs->cp0_epc = (unsigned long) ka->sa.sa_handler;
229
230 return 0;
231
232give_sigsegv:
233 if (signr == SIGSEGV)
234 ka->sa.sa_handler = SIG_DFL;
235 force_sig(SIGSEGV, current);
236 return -EFAULT;
237}
238
239int handle_signal(unsigned long sig, siginfo_t *info,
240 struct k_sigaction *ka, sigset_t *oldset, struct pt_regs *regs)
241{
242 int ret;
243
244 if (regs->is_syscall) {
245 switch (regs->regs[4]) {
246 case ERESTART_RESTARTBLOCK:
247 case ERESTARTNOHAND:
248 regs->regs[4] = EINTR;
249 break;
250 case ERESTARTSYS:
251 if (!(ka->sa.sa_flags & SA_RESTART)) {
252 regs->regs[4] = EINTR;
253 break;
254 }
255 case ERESTARTNOINTR:
256 regs->regs[4] = regs->orig_r4;
257 regs->regs[7] = regs->orig_r7;
258 regs->cp0_epc -= 8;
259 }
260
261 regs->is_syscall = 0;
262 }
263
264 /*
265 * Set up the stack frame
266 */
267 ret = setup_rt_frame(ka, regs, sig, oldset, info);
268
269 spin_lock_irq(&current->sighand->siglock);
270 sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);
271 if (!(ka->sa.sa_flags & SA_NODEFER))
272 sigaddset(&current->blocked, sig);
273 recalc_sigpending();
274 spin_unlock_irq(&current->sighand->siglock);
275
276 return ret;
277}
278
279asmlinkage void do_signal(struct pt_regs *regs)
280{
281 struct k_sigaction ka;
282 sigset_t *oldset;
283 siginfo_t info;
284 int signr;
285
286 /*
287 * We want the common case to go fast, which is why we may in certain
288 * cases get here from kernel mode. Just return without doing anything
289 * if so.
290 */
291 if (!user_mode(regs))
292 return;
293
294 if (test_thread_flag(TIF_RESTORE_SIGMASK))
295 oldset = &current->saved_sigmask;
296 else
297 oldset = &current->blocked;
298
299 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
300 if (signr > 0) {
301 /* Actually deliver the signal. */
302 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
303 /*
304 * A signal was successfully delivered; the saved
305 * sigmask will have been stored in the signal frame,
306 * and will be restored by sigreturn, so we can simply
307 * clear the TIF_RESTORE_SIGMASK flag.
308 */
309 if (test_thread_flag(TIF_RESTORE_SIGMASK))
310 clear_thread_flag(TIF_RESTORE_SIGMASK);
311 }
312
313 return;
314 }
315
316 if (regs->is_syscall) {
317 if (regs->regs[4] == ERESTARTNOHAND ||
318 regs->regs[4] == ERESTARTSYS ||
319 regs->regs[4] == ERESTARTNOINTR) {
320 regs->regs[4] = regs->orig_r4;
321 regs->regs[7] = regs->orig_r7;
322 regs->cp0_epc -= 8;
323 }
324
325 if (regs->regs[4] == ERESTART_RESTARTBLOCK) {
326 regs->regs[27] = __NR_restart_syscall;
327 regs->regs[4] = regs->orig_r4;
328 regs->regs[7] = regs->orig_r7;
329 regs->cp0_epc -= 8;
330 }
331
332 regs->is_syscall = 0; /* Don't deal with this again. */
333 }
334
335 /*
336 * If there's no signal to deliver, we just put the saved sigmask
337 * back
338 */
339 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
340 clear_thread_flag(TIF_RESTORE_SIGMASK);
341 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
342 }
343}
344
345/*
346 * notification of userspace execution resumption
347 * - triggered by the TIF_WORK_MASK flags
348 */
349asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
350 __u32 thread_info_flags)
351{
352 /* deal with pending signal delivery */
353 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_RESTORE_SIGMASK))
354 do_signal(regs);
355}