aboutsummaryrefslogtreecommitdiffstats
path: root/arch/arm26/kernel/signal.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/arm26/kernel/signal.c')
-rw-r--r--arch/arm26/kernel/signal.c540
1 files changed, 540 insertions, 0 deletions
diff --git a/arch/arm26/kernel/signal.c b/arch/arm26/kernel/signal.c
new file mode 100644
index 000000000000..356d9809cc0b
--- /dev/null
+++ b/arch/arm26/kernel/signal.c
@@ -0,0 +1,540 @@
1/*
2 * linux/arch/arm26/kernel/signal.c
3 *
4 * Copyright (C) 1995-2002 Russell King
5 * Copyright (C) 2003 Ian Molton (ARM26)
6 *
7 * FIXME!!! This is probably very broken (13/05/2003)
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13#include <linux/config.h>
14#include <linux/sched.h>
15#include <linux/mm.h>
16#include <linux/smp.h>
17#include <linux/smp_lock.h>
18#include <linux/kernel.h>
19#include <linux/errno.h>
20#include <linux/signal.h>
21#include <linux/wait.h>
22#include <linux/ptrace.h>
23#include <linux/personality.h>
24#include <linux/tty.h>
25#include <linux/binfmts.h>
26#include <linux/elf.h>
27
28#include <asm/pgalloc.h>
29#include <asm/ucontext.h>
30#include <asm/uaccess.h>
31#include <asm/unistd.h>
32
33#include "ptrace.h"
34
35#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
36
37/*
38 * For ARM syscalls, we encode the syscall number into the instruction.
39 */
40#define SWI_SYS_SIGRETURN (0xef000000|(__NR_sigreturn))
41#define SWI_SYS_RT_SIGRETURN (0xef000000|(__NR_rt_sigreturn))
42
43static int do_signal(sigset_t *oldset, struct pt_regs * regs, int syscall);
44
45/*
46 * atomically swap in the new signal mask, and wait for a signal.
47 */
48asmlinkage int sys_sigsuspend(int restart, unsigned long oldmask, old_sigset_t mask, struct pt_regs *regs)
49{
50 sigset_t saveset;
51
52 mask &= _BLOCKABLE;
53 spin_lock_irq(&current->sighand->siglock);
54 saveset = current->blocked;
55 siginitset(&current->blocked, mask);
56 recalc_sigpending();
57 spin_unlock_irq(&current->sighand->siglock);
58 regs->ARM_r0 = -EINTR;
59
60 while (1) {
61 current->state = TASK_INTERRUPTIBLE;
62 schedule();
63 if (do_signal(&saveset, regs, 0))
64 return regs->ARM_r0;
65 }
66}
67
68asmlinkage int
69sys_rt_sigsuspend(sigset_t *unewset, size_t sigsetsize, struct pt_regs *regs)
70{
71 sigset_t saveset, newset;
72
73 /* XXX: Don't preclude handling different sized sigset_t's. */
74 if (sigsetsize != sizeof(sigset_t))
75 return -EINVAL;
76
77 if (copy_from_user(&newset, unewset, sizeof(newset)))
78 return -EFAULT;
79 sigdelsetmask(&newset, ~_BLOCKABLE);
80
81 spin_lock_irq(&current->sighand->siglock);
82 saveset = current->blocked;
83 current->blocked = newset;
84 recalc_sigpending();
85 spin_unlock_irq(&current->sighand->siglock);
86 regs->ARM_r0 = -EINTR;
87
88 while (1) {
89 current->state = TASK_INTERRUPTIBLE;
90 schedule();
91 if (do_signal(&saveset, regs, 0))
92 return regs->ARM_r0;
93 }
94}
95
96asmlinkage int
97sys_sigaction(int sig, const struct old_sigaction *act,
98 struct old_sigaction *oact)
99{
100 struct k_sigaction new_ka, old_ka;
101 int ret;
102
103 if (act) {
104 old_sigset_t mask;
105 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
106 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
107 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
108 return -EFAULT;
109 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
110 __get_user(mask, &act->sa_mask);
111 siginitset(&new_ka.sa.sa_mask, mask);
112 }
113
114 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
115
116 if (!ret && oact) {
117 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
118 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
119 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
120 return -EFAULT;
121 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
122 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
123 }
124
125 return ret;
126}
127
128/*
129 * Do a signal return; undo the signal stack.
130 */
131struct sigframe
132{
133 struct sigcontext sc;
134 unsigned long extramask[_NSIG_WORDS-1];
135 unsigned long retcode;
136};
137
138struct rt_sigframe
139{
140 struct siginfo *pinfo;
141 void *puc;
142 struct siginfo info;
143 struct ucontext uc;
144 unsigned long retcode;
145};
146
147static int
148restore_sigcontext(struct pt_regs *regs, struct sigcontext *sc)
149{
150 int err = 0;
151
152 __get_user_error(regs->ARM_r0, &sc->arm_r0, err);
153 __get_user_error(regs->ARM_r1, &sc->arm_r1, err);
154 __get_user_error(regs->ARM_r2, &sc->arm_r2, err);
155 __get_user_error(regs->ARM_r3, &sc->arm_r3, err);
156 __get_user_error(regs->ARM_r4, &sc->arm_r4, err);
157 __get_user_error(regs->ARM_r5, &sc->arm_r5, err);
158 __get_user_error(regs->ARM_r6, &sc->arm_r6, err);
159 __get_user_error(regs->ARM_r7, &sc->arm_r7, err);
160 __get_user_error(regs->ARM_r8, &sc->arm_r8, err);
161 __get_user_error(regs->ARM_r9, &sc->arm_r9, err);
162 __get_user_error(regs->ARM_r10, &sc->arm_r10, err);
163 __get_user_error(regs->ARM_fp, &sc->arm_fp, err);
164 __get_user_error(regs->ARM_ip, &sc->arm_ip, err);
165 __get_user_error(regs->ARM_sp, &sc->arm_sp, err);
166 __get_user_error(regs->ARM_lr, &sc->arm_lr, err);
167 __get_user_error(regs->ARM_pc, &sc->arm_pc, err);
168
169 err |= !valid_user_regs(regs);
170
171 return err;
172}
173
174asmlinkage int sys_sigreturn(struct pt_regs *regs)
175{
176 struct sigframe *frame;
177 sigset_t set;
178
179 /*
180 * Since we stacked the signal on a 64-bit boundary,
181 * then 'sp' should be word aligned here. If it's
182 * not, then the user is trying to mess with us.
183 */
184 if (regs->ARM_sp & 7)
185 goto badframe;
186
187 frame = (struct sigframe *)regs->ARM_sp;
188
189 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
190 goto badframe;
191 if (__get_user(set.sig[0], &frame->sc.oldmask)
192 || (_NSIG_WORDS > 1
193 && __copy_from_user(&set.sig[1], &frame->extramask,
194 sizeof(frame->extramask))))
195 goto badframe;
196
197 sigdelsetmask(&set, ~_BLOCKABLE);
198 spin_lock_irq(&current->sighand->siglock);
199 current->blocked = set;
200 recalc_sigpending();
201 spin_unlock_irq(&current->sighand->siglock);
202
203 if (restore_sigcontext(regs, &frame->sc))
204 goto badframe;
205
206 /* Send SIGTRAP if we're single-stepping */
207 if (current->ptrace & PT_SINGLESTEP) {
208 ptrace_cancel_bpt(current);
209 send_sig(SIGTRAP, current, 1);
210 }
211
212 return regs->ARM_r0;
213
214badframe:
215 force_sig(SIGSEGV, current);
216 return 0;
217}
218
219asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
220{
221 struct rt_sigframe *frame;
222 sigset_t set;
223
224 /*
225 * Since we stacked the signal on a 64-bit boundary,
226 * then 'sp' should be word aligned here. If it's
227 * not, then the user is trying to mess with us.
228 */
229 if (regs->ARM_sp & 7)
230 goto badframe;
231
232 frame = (struct rt_sigframe *)regs->ARM_sp;
233
234 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
235 goto badframe;
236 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
237 goto badframe;
238
239 sigdelsetmask(&set, ~_BLOCKABLE);
240 spin_lock_irq(&current->sighand->siglock);
241 current->blocked = set;
242 recalc_sigpending();
243 spin_unlock_irq(&current->sighand->siglock);
244
245 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
246 goto badframe;
247
248 /* Send SIGTRAP if we're single-stepping */
249 if (current->ptrace & PT_SINGLESTEP) {
250 ptrace_cancel_bpt(current);
251 send_sig(SIGTRAP, current, 1);
252 }
253
254 return regs->ARM_r0;
255
256badframe:
257 force_sig(SIGSEGV, current);
258 return 0;
259}
260
261static int
262setup_sigcontext(struct sigcontext *sc, /*struct _fpstate *fpstate,*/
263 struct pt_regs *regs, unsigned long mask)
264{
265 int err = 0;
266
267 __put_user_error(regs->ARM_r0, &sc->arm_r0, err);
268 __put_user_error(regs->ARM_r1, &sc->arm_r1, err);
269 __put_user_error(regs->ARM_r2, &sc->arm_r2, err);
270 __put_user_error(regs->ARM_r3, &sc->arm_r3, err);
271 __put_user_error(regs->ARM_r4, &sc->arm_r4, err);
272 __put_user_error(regs->ARM_r5, &sc->arm_r5, err);
273 __put_user_error(regs->ARM_r6, &sc->arm_r6, err);
274 __put_user_error(regs->ARM_r7, &sc->arm_r7, err);
275 __put_user_error(regs->ARM_r8, &sc->arm_r8, err);
276 __put_user_error(regs->ARM_r9, &sc->arm_r9, err);
277 __put_user_error(regs->ARM_r10, &sc->arm_r10, err);
278 __put_user_error(regs->ARM_fp, &sc->arm_fp, err);
279 __put_user_error(regs->ARM_ip, &sc->arm_ip, err);
280 __put_user_error(regs->ARM_sp, &sc->arm_sp, err);
281 __put_user_error(regs->ARM_lr, &sc->arm_lr, err);
282 __put_user_error(regs->ARM_pc, &sc->arm_pc, err);
283
284 __put_user_error(current->thread.trap_no, &sc->trap_no, err);
285 __put_user_error(current->thread.error_code, &sc->error_code, err);
286 __put_user_error(current->thread.address, &sc->fault_address, err);
287 __put_user_error(mask, &sc->oldmask, err);
288
289 return err;
290}
291
292static inline void *
293get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
294{
295 unsigned long sp = regs->ARM_sp;
296
297 /*
298 * This is the X/Open sanctioned signal stack switching.
299 */
300 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
301 sp = current->sas_ss_sp + current->sas_ss_size;
302
303 /*
304 * ATPCS B01 mandates 8-byte alignment
305 */
306 return (void *)((sp - framesize) & ~7);
307}
308
309static int
310setup_return(struct pt_regs *regs, struct k_sigaction *ka,
311 unsigned long *rc, void *frame, int usig)
312{
313 unsigned long handler = (unsigned long)ka->sa.sa_handler;
314 unsigned long retcode;
315
316 if (ka->sa.sa_flags & SA_RESTORER) {
317 retcode = (unsigned long)ka->sa.sa_restorer;
318 } else {
319
320 if (__put_user((ka->sa.sa_flags & SA_SIGINFO)?SWI_SYS_RT_SIGRETURN:SWI_SYS_SIGRETURN, rc))
321 return 1;
322
323 retcode = ((unsigned long)rc);
324 }
325
326 regs->ARM_r0 = usig;
327 regs->ARM_sp = (unsigned long)frame;
328 regs->ARM_lr = retcode;
329 regs->ARM_pc = handler & ~3;
330
331 return 0;
332}
333
334static int
335setup_frame(int usig, struct k_sigaction *ka, sigset_t *set, struct pt_regs *regs)
336{
337 struct sigframe *frame = get_sigframe(ka, regs, sizeof(*frame));
338 int err = 0;
339
340 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
341 return 1;
342
343 err |= setup_sigcontext(&frame->sc, /*&frame->fpstate,*/ regs, set->sig[0]);
344
345 if (_NSIG_WORDS > 1) {
346 err |= __copy_to_user(frame->extramask, &set->sig[1],
347 sizeof(frame->extramask));
348 }
349
350 if (err == 0)
351 err = setup_return(regs, ka, &frame->retcode, frame, usig);
352
353 return err;
354}
355
356static int
357setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
358 sigset_t *set, struct pt_regs *regs)
359{
360 struct rt_sigframe *frame = get_sigframe(ka, regs, sizeof(*frame));
361 int err = 0;
362
363 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
364 return 1;
365
366 __put_user_error(&frame->info, &frame->pinfo, err);
367 __put_user_error(&frame->uc, &frame->puc, err);
368 err |= copy_siginfo_to_user(&frame->info, info);
369
370 /* Clear all the bits of the ucontext we don't use. */
371 err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
372
373 err |= setup_sigcontext(&frame->uc.uc_mcontext, /*&frame->fpstate,*/
374 regs, set->sig[0]);
375 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
376
377 if (err == 0)
378 err = setup_return(regs, ka, &frame->retcode, frame, usig);
379
380 if (err == 0) {
381 /*
382 * For realtime signals we must also set the second and third
383 * arguments for the signal handler.
384 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
385 */
386 regs->ARM_r1 = (unsigned long)frame->pinfo;
387 regs->ARM_r2 = (unsigned long)frame->puc;
388 }
389
390 return err;
391}
392
393static inline void restart_syscall(struct pt_regs *regs)
394{
395 regs->ARM_r0 = regs->ARM_ORIG_r0;
396 regs->ARM_pc -= 4;
397}
398
399/*
400 * OK, we're invoking a handler
401 */
402static void
403handle_signal(unsigned long sig, siginfo_t *info, sigset_t *oldset,
404 struct pt_regs * regs, int syscall)
405{
406 struct thread_info *thread = current_thread_info();
407 struct task_struct *tsk = current;
408 struct k_sigaction *ka = &tsk->sighand->action[sig-1];
409 int usig = sig;
410 int ret;
411
412 /*
413 * If we were from a system call, check for system call restarting...
414 */
415 if (syscall) {
416 switch (regs->ARM_r0) {
417 case -ERESTART_RESTARTBLOCK:
418 current_thread_info()->restart_block.fn =
419 do_no_restart_syscall;
420 case -ERESTARTNOHAND:
421 regs->ARM_r0 = -EINTR;
422 break;
423 case -ERESTARTSYS:
424 if (!(ka->sa.sa_flags & SA_RESTART)) {
425 regs->ARM_r0 = -EINTR;
426 break;
427 }
428 /* fallthrough */
429 case -ERESTARTNOINTR:
430 restart_syscall(regs);
431 }
432 }
433
434 /*
435 * translate the signal
436 */
437 if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
438 usig = thread->exec_domain->signal_invmap[usig];
439
440 /*
441 * Set up the stack frame
442 */
443 if (ka->sa.sa_flags & SA_SIGINFO)
444 ret = setup_rt_frame(usig, ka, info, oldset, regs);
445 else
446 ret = setup_frame(usig, ka, oldset, regs);
447
448 /*
449 * Check that the resulting registers are actually sane.
450 */
451 ret |= !valid_user_regs(regs);
452
453 if (ret == 0) {
454 if (ka->sa.sa_flags & SA_ONESHOT)
455 ka->sa.sa_handler = SIG_DFL;
456
457 if (!(ka->sa.sa_flags & SA_NODEFER)) {
458 spin_lock_irq(&tsk->sighand->siglock);
459 sigorsets(&tsk->blocked, &tsk->blocked,
460 &ka->sa.sa_mask);
461 sigaddset(&tsk->blocked, sig);
462 recalc_sigpending();
463 spin_unlock_irq(&tsk->sighand->siglock);
464 }
465 return;
466 }
467
468 force_sigsegv(sig, tsk);
469}
470
471/*
472 * Note that 'init' is a special process: it doesn't get signals it doesn't
473 * want to handle. Thus you cannot kill init even with a SIGKILL even by
474 * mistake.
475 *
476 * Note that we go through the signals twice: once to check the signals that
477 * the kernel can handle, and then we build all the user-level signal handling
478 * stack-frames in one go after that.
479 */
480static int do_signal(sigset_t *oldset, struct pt_regs *regs, int syscall)
481{
482 siginfo_t info;
483 int signr;
484
485 /*
486 * We want the common case to go fast, which
487 * is why we may in certain cases get here from
488 * kernel mode. Just return without doing anything
489 * if so.
490 */
491 if (!user_mode(regs))
492 return 0;
493
494 if (current->ptrace & PT_SINGLESTEP)
495 ptrace_cancel_bpt(current);
496
497 signr = get_signal_to_deliver(&info, regs, NULL);
498 if (signr > 0) {
499 handle_signal(signr, &info, oldset, regs, syscall);
500 if (current->ptrace & PT_SINGLESTEP)
501 ptrace_set_bpt(current);
502 return 1;
503 }
504
505 /*
506 * No signal to deliver to the process - restart the syscall.
507 */
508 if (syscall) {
509 if (regs->ARM_r0 == -ERESTART_RESTARTBLOCK) {
510 u32 *usp;
511
512 regs->ARM_sp -= 12;
513 usp = (u32 *)regs->ARM_sp;
514
515 put_user(regs->ARM_pc, &usp[0]);
516 /* swi __NR_restart_syscall */
517 put_user(0xef000000 | __NR_restart_syscall, &usp[1]);
518 /* ldr pc, [sp], #12 */
519// FIXME!!! is #12 correct there?
520 put_user(0xe49df00c, &usp[2]);
521
522 regs->ARM_pc = regs->ARM_sp + 4;
523 }
524 if (regs->ARM_r0 == -ERESTARTNOHAND ||
525 regs->ARM_r0 == -ERESTARTSYS ||
526 regs->ARM_r0 == -ERESTARTNOINTR) {
527 restart_syscall(regs);
528 }
529 }
530 if (current->ptrace & PT_SINGLESTEP)
531 ptrace_set_bpt(current);
532 return 0;
533}
534
535asmlinkage void
536do_notify_resume(struct pt_regs *regs, unsigned int thread_flags, int syscall)
537{
538 if (thread_flags & _TIF_SIGPENDING)
539 do_signal(&current->blocked, regs, syscall);
540}