blob: 3843d3bab2ddc4d2e7eb536cc8380ee3d420d5f5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/arm/kernel/signal.c
3 *
4 * Copyright (C) 1995-2002 Russell King
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/errno.h>
11#include <linux/signal.h>
12#include <linux/ptrace.h>
13#include <linux/personality.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080014#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Russell Kingee90dab2006-11-09 14:20:47 +000016#include <asm/elf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/cacheflush.h>
18#include <asm/ucontext.h>
19#include <asm/uaccess.h>
20#include <asm/unistd.h>
21
22#include "ptrace.h"
Russell Kinge00d3492005-06-22 20:26:05 +010023#include "signal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
26
27/*
28 * For ARM syscalls, we encode the syscall number into the instruction.
29 */
30#define SWI_SYS_SIGRETURN (0xef000000|(__NR_sigreturn))
31#define SWI_SYS_RT_SIGRETURN (0xef000000|(__NR_rt_sigreturn))
32
33/*
Nicolas Pitrefcca5382006-01-18 22:38:47 +000034 * With EABI, the syscall number has to be loaded into r7.
35 */
36#define MOV_R7_NR_SIGRETURN (0xe3a07000 | (__NR_sigreturn - __NR_SYSCALL_BASE))
37#define MOV_R7_NR_RT_SIGRETURN (0xe3a07000 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
38
39/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * For Thumb syscalls, we pass the syscall number via r7. We therefore
41 * need two 16-bit instructions.
42 */
43#define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (__NR_sigreturn - __NR_SYSCALL_BASE))
44#define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
45
Nicolas Pitrefcca5382006-01-18 22:38:47 +000046const unsigned long sigreturn_codes[7] = {
47 MOV_R7_NR_SIGRETURN, SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
48 MOV_R7_NR_RT_SIGRETURN, SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN,
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
50
51static int do_signal(sigset_t *oldset, struct pt_regs * regs, int syscall);
52
53/*
54 * atomically swap in the new signal mask, and wait for a signal.
55 */
56asmlinkage int sys_sigsuspend(int restart, unsigned long oldmask, old_sigset_t mask, struct pt_regs *regs)
57{
58 sigset_t saveset;
59
60 mask &= _BLOCKABLE;
61 spin_lock_irq(&current->sighand->siglock);
62 saveset = current->blocked;
63 siginitset(&current->blocked, mask);
64 recalc_sigpending();
65 spin_unlock_irq(&current->sighand->siglock);
66 regs->ARM_r0 = -EINTR;
67
68 while (1) {
69 current->state = TASK_INTERRUPTIBLE;
70 schedule();
71 if (do_signal(&saveset, regs, 0))
72 return regs->ARM_r0;
73 }
74}
75
76asmlinkage int
77sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize, struct pt_regs *regs)
78{
79 sigset_t saveset, newset;
80
81 /* XXX: Don't preclude handling different sized sigset_t's. */
82 if (sigsetsize != sizeof(sigset_t))
83 return -EINVAL;
84
85 if (copy_from_user(&newset, unewset, sizeof(newset)))
86 return -EFAULT;
87 sigdelsetmask(&newset, ~_BLOCKABLE);
88
89 spin_lock_irq(&current->sighand->siglock);
90 saveset = current->blocked;
91 current->blocked = newset;
92 recalc_sigpending();
93 spin_unlock_irq(&current->sighand->siglock);
94 regs->ARM_r0 = -EINTR;
95
96 while (1) {
97 current->state = TASK_INTERRUPTIBLE;
98 schedule();
99 if (do_signal(&saveset, regs, 0))
100 return regs->ARM_r0;
101 }
102}
103
104asmlinkage int
105sys_sigaction(int sig, const struct old_sigaction __user *act,
106 struct old_sigaction __user *oact)
107{
108 struct k_sigaction new_ka, old_ka;
109 int ret;
110
111 if (act) {
112 old_sigset_t mask;
113 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
114 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
115 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
116 return -EFAULT;
117 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
118 __get_user(mask, &act->sa_mask);
119 siginitset(&new_ka.sa.sa_mask, mask);
120 }
121
122 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
123
124 if (!ret && oact) {
125 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
126 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
127 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
128 return -EFAULT;
129 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
130 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
131 }
132
133 return ret;
134}
135
Lennert Buytenhek3bec6de2006-06-27 22:56:18 +0100136#ifdef CONFIG_CRUNCH
137static int preserve_crunch_context(struct crunch_sigframe *frame)
138{
139 char kbuf[sizeof(*frame) + 8];
140 struct crunch_sigframe *kframe;
141
142 /* the crunch context must be 64 bit aligned */
143 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
144 kframe->magic = CRUNCH_MAGIC;
145 kframe->size = CRUNCH_STORAGE_SIZE;
146 crunch_task_copy(current_thread_info(), &kframe->storage);
147 return __copy_to_user(frame, kframe, sizeof(*frame));
148}
149
150static int restore_crunch_context(struct crunch_sigframe *frame)
151{
152 char kbuf[sizeof(*frame) + 8];
153 struct crunch_sigframe *kframe;
154
155 /* the crunch context must be 64 bit aligned */
156 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
157 if (__copy_from_user(kframe, frame, sizeof(*frame)))
158 return -1;
159 if (kframe->magic != CRUNCH_MAGIC ||
160 kframe->size != CRUNCH_STORAGE_SIZE)
161 return -1;
162 crunch_task_restore(current_thread_info(), &kframe->storage);
163 return 0;
164}
165#endif
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#ifdef CONFIG_IWMMXT
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169static int preserve_iwmmxt_context(struct iwmmxt_sigframe *frame)
170{
Hugh Dickins69b04752005-10-29 18:16:36 -0700171 char kbuf[sizeof(*frame) + 8];
172 struct iwmmxt_sigframe *kframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 /* the iWMMXt context must be 64 bit aligned */
Hugh Dickins69b04752005-10-29 18:16:36 -0700175 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100176 kframe->magic = IWMMXT_MAGIC;
177 kframe->size = IWMMXT_STORAGE_SIZE;
Hugh Dickins69b04752005-10-29 18:16:36 -0700178 iwmmxt_task_copy(current_thread_info(), &kframe->storage);
179 return __copy_to_user(frame, kframe, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182static int restore_iwmmxt_context(struct iwmmxt_sigframe *frame)
183{
Hugh Dickins69b04752005-10-29 18:16:36 -0700184 char kbuf[sizeof(*frame) + 8];
185 struct iwmmxt_sigframe *kframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Hugh Dickins69b04752005-10-29 18:16:36 -0700187 /* the iWMMXt context must be 64 bit aligned */
188 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
189 if (__copy_from_user(kframe, frame, sizeof(*frame)))
190 return -1;
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100191 if (kframe->magic != IWMMXT_MAGIC ||
192 kframe->size != IWMMXT_STORAGE_SIZE)
Hugh Dickins69b04752005-10-29 18:16:36 -0700193 return -1;
194 iwmmxt_task_restore(current_thread_info(), &kframe->storage);
195 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
198#endif
199
200/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 * Do a signal return; undo the signal stack. These are aligned to 64-bit.
202 */
203struct sigframe {
Russell King7d4fdc12006-06-15 20:13:25 +0100204 struct ucontext uc;
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000205 unsigned long retcode[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206};
207
208struct rt_sigframe {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 struct siginfo info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100210 struct sigframe sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211};
212
Russell King68071482006-06-15 20:23:02 +0100213static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100215 struct aux_sigframe __user *aux;
Russell King68071482006-06-15 20:23:02 +0100216 sigset_t set;
217 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Russell King68071482006-06-15 20:23:02 +0100219 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
220 if (err == 0) {
221 sigdelsetmask(&set, ~_BLOCKABLE);
222 spin_lock_irq(&current->sighand->siglock);
223 current->blocked = set;
224 recalc_sigpending();
225 spin_unlock_irq(&current->sighand->siglock);
226 }
227
228 __get_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
229 __get_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
230 __get_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
231 __get_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
232 __get_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
233 __get_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
234 __get_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
235 __get_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
236 __get_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
237 __get_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
238 __get_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
239 __get_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
240 __get_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
241 __get_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
242 __get_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
243 __get_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
244 __get_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 err |= !valid_user_regs(regs);
247
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100248 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6de2006-06-27 22:56:18 +0100249#ifdef CONFIG_CRUNCH
250 if (err == 0)
251 err |= restore_crunch_context(&aux->crunch);
252#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253#ifdef CONFIG_IWMMXT
254 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
255 err |= restore_iwmmxt_context(&aux->iwmmxt);
256#endif
257#ifdef CONFIG_VFP
258// if (err == 0)
Russell King68071482006-06-15 20:23:02 +0100259// err |= vfp_restore_state(&sf->aux.vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260#endif
261
262 return err;
263}
264
265asmlinkage int sys_sigreturn(struct pt_regs *regs)
266{
267 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 /* Always make any pending restarted system calls return -EINTR */
270 current_thread_info()->restart_block.fn = do_no_restart_syscall;
271
272 /*
273 * Since we stacked the signal on a 64-bit boundary,
274 * then 'sp' should be word aligned here. If it's
275 * not, then the user is trying to mess with us.
276 */
277 if (regs->ARM_sp & 7)
278 goto badframe;
279
280 frame = (struct sigframe __user *)regs->ARM_sp;
281
282 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
283 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Russell King68071482006-06-15 20:23:02 +0100285 if (restore_sigframe(regs, frame))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 goto badframe;
287
288 /* Send SIGTRAP if we're single-stepping */
289 if (current->ptrace & PT_SINGLESTEP) {
290 ptrace_cancel_bpt(current);
291 send_sig(SIGTRAP, current, 1);
292 }
293
294 return regs->ARM_r0;
295
296badframe:
297 force_sig(SIGSEGV, current);
298 return 0;
299}
300
301asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
302{
303 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 /* Always make any pending restarted system calls return -EINTR */
306 current_thread_info()->restart_block.fn = do_no_restart_syscall;
307
308 /*
309 * Since we stacked the signal on a 64-bit boundary,
310 * then 'sp' should be word aligned here. If it's
311 * not, then the user is trying to mess with us.
312 */
313 if (regs->ARM_sp & 7)
314 goto badframe;
315
316 frame = (struct rt_sigframe __user *)regs->ARM_sp;
317
318 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
319 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Russell King68071482006-06-15 20:23:02 +0100321 if (restore_sigframe(regs, &frame->sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 goto badframe;
323
Russell Kingcb3504e2006-06-15 20:18:25 +0100324 if (do_sigaltstack(&frame->sig.uc.uc_stack, NULL, regs->ARM_sp) == -EFAULT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 goto badframe;
326
327 /* Send SIGTRAP if we're single-stepping */
328 if (current->ptrace & PT_SINGLESTEP) {
329 ptrace_cancel_bpt(current);
330 send_sig(SIGTRAP, current, 1);
331 }
332
333 return regs->ARM_r0;
334
335badframe:
336 force_sig(SIGSEGV, current);
337 return 0;
338}
339
340static int
Russell Kingaca6ca12006-06-15 20:28:03 +0100341setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs, sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100343 struct aux_sigframe __user *aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 int err = 0;
345
Russell Kingaca6ca12006-06-15 20:28:03 +0100346 __put_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
347 __put_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
348 __put_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
349 __put_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
350 __put_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
351 __put_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
352 __put_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
353 __put_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
354 __put_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
355 __put_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
356 __put_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
357 __put_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
358 __put_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
359 __put_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
360 __put_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
361 __put_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
362 __put_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Russell Kingaca6ca12006-06-15 20:28:03 +0100364 __put_user_error(current->thread.trap_no, &sf->uc.uc_mcontext.trap_no, err);
365 __put_user_error(current->thread.error_code, &sf->uc.uc_mcontext.error_code, err);
366 __put_user_error(current->thread.address, &sf->uc.uc_mcontext.fault_address, err);
367 __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Russell Kingaca6ca12006-06-15 20:28:03 +0100369 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100371 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6de2006-06-27 22:56:18 +0100372#ifdef CONFIG_CRUNCH
373 if (err == 0)
374 err |= preserve_crunch_context(&aux->crunch);
375#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376#ifdef CONFIG_IWMMXT
377 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
378 err |= preserve_iwmmxt_context(&aux->iwmmxt);
379#endif
380#ifdef CONFIG_VFP
381// if (err == 0)
Russell Kingaca6ca12006-06-15 20:28:03 +0100382// err |= vfp_save_state(&sf->aux.vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383#endif
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100384 __put_user_error(0, &aux->end_magic, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 return err;
387}
388
389static inline void __user *
390get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
391{
392 unsigned long sp = regs->ARM_sp;
393 void __user *frame;
394
395 /*
396 * This is the X/Open sanctioned signal stack switching.
397 */
398 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
399 sp = current->sas_ss_sp + current->sas_ss_size;
400
401 /*
402 * ATPCS B01 mandates 8-byte alignment
403 */
404 frame = (void __user *)((sp - framesize) & ~7);
405
406 /*
407 * Check that we can actually write to the signal frame.
408 */
409 if (!access_ok(VERIFY_WRITE, frame, framesize))
410 frame = NULL;
411
412 return frame;
413}
414
415static int
416setup_return(struct pt_regs *regs, struct k_sigaction *ka,
417 unsigned long __user *rc, void __user *frame, int usig)
418{
419 unsigned long handler = (unsigned long)ka->sa.sa_handler;
420 unsigned long retcode;
421 int thumb = 0;
422 unsigned long cpsr = regs->ARM_cpsr & ~PSR_f;
423
424 /*
425 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
426 */
427 if (ka->sa.sa_flags & SA_THIRTYTWO)
428 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
429
430#ifdef CONFIG_ARM_THUMB
431 if (elf_hwcap & HWCAP_THUMB) {
432 /*
433 * The LSB of the handler determines if we're going to
434 * be using THUMB or ARM mode for this signal handler.
435 */
436 thumb = handler & 1;
437
438 if (thumb)
439 cpsr |= PSR_T_BIT;
440 else
441 cpsr &= ~PSR_T_BIT;
442 }
443#endif
444
445 if (ka->sa.sa_flags & SA_RESTORER) {
446 retcode = (unsigned long)ka->sa.sa_restorer;
447 } else {
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000448 unsigned int idx = thumb << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 if (ka->sa.sa_flags & SA_SIGINFO)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000451 idx += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000453 if (__put_user(sigreturn_codes[idx], rc) ||
454 __put_user(sigreturn_codes[idx+1], rc+1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return 1;
456
Russell Kinge00d3492005-06-22 20:26:05 +0100457 if (cpsr & MODE32_BIT) {
458 /*
459 * 32-bit code can use the new high-page
460 * signal return code support.
461 */
462 retcode = KERN_SIGRETURN_CODE + (idx << 2) + thumb;
463 } else {
464 /*
465 * Ensure that the instruction cache sees
466 * the return code written onto the stack.
467 */
468 flush_icache_range((unsigned long)rc,
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000469 (unsigned long)(rc + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Russell Kinge00d3492005-06-22 20:26:05 +0100471 retcode = ((unsigned long)rc) + thumb;
472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474
475 regs->ARM_r0 = usig;
476 regs->ARM_sp = (unsigned long)frame;
477 regs->ARM_lr = retcode;
478 regs->ARM_pc = handler;
479 regs->ARM_cpsr = cpsr;
480
481 return 0;
482}
483
484static int
485setup_frame(int usig, struct k_sigaction *ka, sigset_t *set, struct pt_regs *regs)
486{
487 struct sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
488 int err = 0;
489
490 if (!frame)
491 return 1;
492
Russell Kingca195cf2006-06-24 22:41:09 +0100493 /*
494 * Set uc.uc_flags to a value which sc.trap_no would never have.
495 */
496 __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Russell Kingaca6ca12006-06-15 20:28:03 +0100498 err |= setup_sigframe(frame, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (err == 0)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000500 err = setup_return(regs, ka, frame->retcode, frame, usig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 return err;
503}
504
505static int
506setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
507 sigset_t *set, struct pt_regs *regs)
508{
509 struct rt_sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
510 stack_t stack;
511 int err = 0;
512
513 if (!frame)
514 return 1;
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 err |= copy_siginfo_to_user(&frame->info, info);
517
Russell Kingcb3504e2006-06-15 20:18:25 +0100518 __put_user_error(0, &frame->sig.uc.uc_flags, err);
519 __put_user_error(NULL, &frame->sig.uc.uc_link, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 memset(&stack, 0, sizeof(stack));
522 stack.ss_sp = (void __user *)current->sas_ss_sp;
523 stack.ss_flags = sas_ss_flags(regs->ARM_sp);
524 stack.ss_size = current->sas_ss_size;
Russell Kingcb3504e2006-06-15 20:18:25 +0100525 err |= __copy_to_user(&frame->sig.uc.uc_stack, &stack, sizeof(stack));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Russell Kingaca6ca12006-06-15 20:28:03 +0100527 err |= setup_sigframe(&frame->sig, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (err == 0)
Russell Kingcb3504e2006-06-15 20:18:25 +0100529 err = setup_return(regs, ka, frame->sig.retcode, frame, usig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 if (err == 0) {
532 /*
533 * For realtime signals we must also set the second and third
534 * arguments for the signal handler.
535 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
536 */
537 regs->ARM_r1 = (unsigned long)&frame->info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100538 regs->ARM_r2 = (unsigned long)&frame->sig.uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540
541 return err;
542}
543
544static inline void restart_syscall(struct pt_regs *regs)
545{
546 regs->ARM_r0 = regs->ARM_ORIG_r0;
547 regs->ARM_pc -= thumb_mode(regs) ? 2 : 4;
548}
549
550/*
551 * OK, we're invoking a handler
552 */
553static void
554handle_signal(unsigned long sig, struct k_sigaction *ka,
555 siginfo_t *info, sigset_t *oldset,
556 struct pt_regs * regs, int syscall)
557{
558 struct thread_info *thread = current_thread_info();
559 struct task_struct *tsk = current;
560 int usig = sig;
561 int ret;
562
563 /*
564 * If we were from a system call, check for system call restarting...
565 */
566 if (syscall) {
567 switch (regs->ARM_r0) {
568 case -ERESTART_RESTARTBLOCK:
569 case -ERESTARTNOHAND:
570 regs->ARM_r0 = -EINTR;
571 break;
572 case -ERESTARTSYS:
573 if (!(ka->sa.sa_flags & SA_RESTART)) {
574 regs->ARM_r0 = -EINTR;
575 break;
576 }
577 /* fallthrough */
578 case -ERESTARTNOINTR:
579 restart_syscall(regs);
580 }
581 }
582
583 /*
584 * translate the signal
585 */
586 if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
587 usig = thread->exec_domain->signal_invmap[usig];
588
589 /*
590 * Set up the stack frame
591 */
592 if (ka->sa.sa_flags & SA_SIGINFO)
593 ret = setup_rt_frame(usig, ka, info, oldset, regs);
594 else
595 ret = setup_frame(usig, ka, oldset, regs);
596
597 /*
598 * Check that the resulting registers are actually sane.
599 */
600 ret |= !valid_user_regs(regs);
601
Steven Rostedt69be8f12005-08-29 11:44:09 -0400602 if (ret != 0) {
Daniel Jacobowitza6c61e92005-11-19 10:01:07 +0000603 force_sigsegv(sig, tsk);
604 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
606
Daniel Jacobowitza6c61e92005-11-19 10:01:07 +0000607 /*
608 * Block the signal if we were successful.
609 */
610 spin_lock_irq(&tsk->sighand->siglock);
611 sigorsets(&tsk->blocked, &tsk->blocked,
612 &ka->sa.sa_mask);
613 if (!(ka->sa.sa_flags & SA_NODEFER))
614 sigaddset(&tsk->blocked, sig);
615 recalc_sigpending();
616 spin_unlock_irq(&tsk->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
620/*
621 * Note that 'init' is a special process: it doesn't get signals it doesn't
622 * want to handle. Thus you cannot kill init even with a SIGKILL even by
623 * mistake.
624 *
625 * Note that we go through the signals twice: once to check the signals that
626 * the kernel can handle, and then we build all the user-level signal handling
627 * stack-frames in one go after that.
628 */
629static int do_signal(sigset_t *oldset, struct pt_regs *regs, int syscall)
630{
631 struct k_sigaction ka;
632 siginfo_t info;
633 int signr;
634
635 /*
636 * We want the common case to go fast, which
637 * is why we may in certain cases get here from
638 * kernel mode. Just return without doing anything
639 * if so.
640 */
641 if (!user_mode(regs))
642 return 0;
643
Andrew Mortonbdb94f32005-06-26 03:27:21 -0700644 if (try_to_freeze())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 goto no_signal;
646
647 if (current->ptrace & PT_SINGLESTEP)
648 ptrace_cancel_bpt(current);
649
650 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
651 if (signr > 0) {
652 handle_signal(signr, &ka, &info, oldset, regs, syscall);
653 if (current->ptrace & PT_SINGLESTEP)
654 ptrace_set_bpt(current);
655 return 1;
656 }
657
658 no_signal:
659 /*
660 * No signal to deliver to the process - restart the syscall.
661 */
662 if (syscall) {
663 if (regs->ARM_r0 == -ERESTART_RESTARTBLOCK) {
664 if (thumb_mode(regs)) {
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100665 regs->ARM_r7 = __NR_restart_syscall - __NR_SYSCALL_BASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 regs->ARM_pc -= 2;
667 } else {
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100668#if defined(CONFIG_AEABI) && !defined(CONFIG_OABI_COMPAT)
669 regs->ARM_r7 = __NR_restart_syscall;
670 regs->ARM_pc -= 4;
671#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 u32 __user *usp;
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100673 u32 swival = __NR_restart_syscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 regs->ARM_sp -= 12;
676 usp = (u32 __user *)regs->ARM_sp;
677
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100678 /*
679 * Either we supports OABI only, or we have
680 * EABI with the OABI compat layer enabled.
681 * In the later case we don't know if user
682 * space is EABI or not, and if not we must
683 * not clobber r7. Always using the OABI
684 * syscall solves that issue and works for
685 * all those cases.
686 */
Nicolas Pitre95eaa5f2006-06-23 11:40:53 -0400687 swival = swival - __NR_SYSCALL_BASE + __NR_OABI_SYSCALL_BASE;
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 put_user(regs->ARM_pc, &usp[0]);
690 /* swi __NR_restart_syscall */
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100691 put_user(0xef000000 | swival, &usp[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /* ldr pc, [sp], #12 */
693 put_user(0xe49df00c, &usp[2]);
694
695 flush_icache_range((unsigned long)usp,
696 (unsigned long)(usp + 3));
697
698 regs->ARM_pc = regs->ARM_sp + 4;
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100699#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
701 }
702 if (regs->ARM_r0 == -ERESTARTNOHAND ||
703 regs->ARM_r0 == -ERESTARTSYS ||
704 regs->ARM_r0 == -ERESTARTNOINTR) {
705 restart_syscall(regs);
706 }
707 }
708 if (current->ptrace & PT_SINGLESTEP)
709 ptrace_set_bpt(current);
710 return 0;
711}
712
713asmlinkage void
714do_notify_resume(struct pt_regs *regs, unsigned int thread_flags, int syscall)
715{
716 if (thread_flags & _TIF_SIGPENDING)
717 do_signal(&current->blocked, regs, syscall);
718}