blob: 2e66c93973c3253b1194de47103cc210dc21d9d7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/arm/kernel/signal.c
3 *
Russell Kingab72b002009-10-25 15:39:37 +00004 * Copyright (C) 1995-2009 Russell King
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/personality.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080013#include <linux/freezer.h>
Russell King33fa9b12008-09-06 11:35:55 +010014#include <linux/uaccess.h>
David Howells733e5e42009-09-09 08:30:21 +010015#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Russell Kingee90dab2006-11-09 14:20:47 +000017#include <asm/elf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/cacheflush.h>
19#include <asm/ucontext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/unistd.h>
Imre Deak82c6f5a2010-04-11 15:58:27 +010021#include <asm/vfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
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 */
janboeee4cd582008-03-19 03:34:23 +010030#define SWI_SYS_SIGRETURN (0xef000000|(__NR_sigreturn)|(__NR_OABI_SYSCALL_BASE))
31#define SWI_SYS_RT_SIGRETURN (0xef000000|(__NR_rt_sigreturn)|(__NR_OABI_SYSCALL_BASE))
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * atomically swap in the new signal mask, and wait for a signal.
53 */
Mikael Pettersson36984262009-08-15 12:58:11 +010054asmlinkage int sys_sigsuspend(int restart, unsigned long oldmask, old_sigset_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Matt Fleming101d9b02012-03-05 15:05:34 -080056 sigset_t blocked;
Matt Fleming101d9b02012-03-05 15:05:34 -080057 siginitset(&blocked, mask);
Al Viro68f3f162012-05-21 21:42:32 -040058 return sigsuspend(&blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
61asmlinkage int
62sys_sigaction(int sig, const struct old_sigaction __user *act,
63 struct old_sigaction __user *oact)
64{
65 struct k_sigaction new_ka, old_ka;
66 int ret;
67
68 if (act) {
69 old_sigset_t mask;
70 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
71 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
Al Viro7f1b5a92012-04-22 17:15:42 -040072 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
73 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
74 __get_user(mask, &act->sa_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 siginitset(&new_ka.sa.sa_mask, mask);
77 }
78
79 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
80
81 if (!ret && oact) {
82 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
83 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
Al Viro7f1b5a92012-04-22 17:15:42 -040084 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
85 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
86 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 }
89
90 return ret;
91}
92
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010093#ifdef CONFIG_CRUNCH
Hartley Sweeten65a50532009-08-04 23:20:45 +010094static int preserve_crunch_context(struct crunch_sigframe __user *frame)
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010095{
96 char kbuf[sizeof(*frame) + 8];
97 struct crunch_sigframe *kframe;
98
99 /* the crunch context must be 64 bit aligned */
100 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
101 kframe->magic = CRUNCH_MAGIC;
102 kframe->size = CRUNCH_STORAGE_SIZE;
103 crunch_task_copy(current_thread_info(), &kframe->storage);
104 return __copy_to_user(frame, kframe, sizeof(*frame));
105}
106
Hartley Sweeten65a50532009-08-04 23:20:45 +0100107static int restore_crunch_context(struct crunch_sigframe __user *frame)
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100108{
109 char kbuf[sizeof(*frame) + 8];
110 struct crunch_sigframe *kframe;
111
112 /* the crunch context must be 64 bit aligned */
113 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
114 if (__copy_from_user(kframe, frame, sizeof(*frame)))
115 return -1;
116 if (kframe->magic != CRUNCH_MAGIC ||
117 kframe->size != CRUNCH_STORAGE_SIZE)
118 return -1;
119 crunch_task_restore(current_thread_info(), &kframe->storage);
120 return 0;
121}
122#endif
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#ifdef CONFIG_IWMMXT
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126static int preserve_iwmmxt_context(struct iwmmxt_sigframe *frame)
127{
Hugh Dickins69b04752005-10-29 18:16:36 -0700128 char kbuf[sizeof(*frame) + 8];
129 struct iwmmxt_sigframe *kframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 /* the iWMMXt context must be 64 bit aligned */
Hugh Dickins69b04752005-10-29 18:16:36 -0700132 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100133 kframe->magic = IWMMXT_MAGIC;
134 kframe->size = IWMMXT_STORAGE_SIZE;
Hugh Dickins69b04752005-10-29 18:16:36 -0700135 iwmmxt_task_copy(current_thread_info(), &kframe->storage);
136 return __copy_to_user(frame, kframe, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
139static int restore_iwmmxt_context(struct iwmmxt_sigframe *frame)
140{
Hugh Dickins69b04752005-10-29 18:16:36 -0700141 char kbuf[sizeof(*frame) + 8];
142 struct iwmmxt_sigframe *kframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Hugh Dickins69b04752005-10-29 18:16:36 -0700144 /* the iWMMXt context must be 64 bit aligned */
145 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
146 if (__copy_from_user(kframe, frame, sizeof(*frame)))
147 return -1;
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100148 if (kframe->magic != IWMMXT_MAGIC ||
149 kframe->size != IWMMXT_STORAGE_SIZE)
Hugh Dickins69b04752005-10-29 18:16:36 -0700150 return -1;
151 iwmmxt_task_restore(current_thread_info(), &kframe->storage);
152 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
155#endif
156
Imre Deak82c6f5a2010-04-11 15:58:27 +0100157#ifdef CONFIG_VFP
158
159static int preserve_vfp_context(struct vfp_sigframe __user *frame)
160{
Imre Deak82c6f5a2010-04-11 15:58:27 +0100161 const unsigned long magic = VFP_MAGIC;
162 const unsigned long size = VFP_STORAGE_SIZE;
163 int err = 0;
164
Imre Deak82c6f5a2010-04-11 15:58:27 +0100165 __put_user_error(magic, &frame->magic, err);
166 __put_user_error(size, &frame->size, err);
167
Will Deacon2498814f2012-04-23 15:38:28 +0100168 if (err)
169 return -EFAULT;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100170
Will Deacon2498814f2012-04-23 15:38:28 +0100171 return vfp_preserve_user_clear_hwstate(&frame->ufp, &frame->ufp_exc);
Imre Deak82c6f5a2010-04-11 15:58:27 +0100172}
173
174static int restore_vfp_context(struct vfp_sigframe __user *frame)
175{
Imre Deak82c6f5a2010-04-11 15:58:27 +0100176 unsigned long magic;
177 unsigned long size;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100178 int err = 0;
179
180 __get_user_error(magic, &frame->magic, err);
181 __get_user_error(size, &frame->size, err);
182
183 if (err)
184 return -EFAULT;
185 if (magic != VFP_MAGIC || size != VFP_STORAGE_SIZE)
186 return -EINVAL;
187
Will Deacon2498814f2012-04-23 15:38:28 +0100188 return vfp_restore_user_hwstate(&frame->ufp, &frame->ufp_exc);
Imre Deak82c6f5a2010-04-11 15:58:27 +0100189}
190
191#endif
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 * Do a signal return; undo the signal stack. These are aligned to 64-bit.
195 */
196struct sigframe {
Russell King7d4fdc12006-06-15 20:13:25 +0100197 struct ucontext uc;
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000198 unsigned long retcode[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199};
200
201struct rt_sigframe {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 struct siginfo info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100203 struct sigframe sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
Russell King68071482006-06-15 20:23:02 +0100206static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100208 struct aux_sigframe __user *aux;
Russell King68071482006-06-15 20:23:02 +0100209 sigset_t set;
210 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Russell King68071482006-06-15 20:23:02 +0100212 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
213 if (err == 0) {
214 sigdelsetmask(&set, ~_BLOCKABLE);
Matt Fleming101d9b02012-03-05 15:05:34 -0800215 set_current_blocked(&set);
Russell King68071482006-06-15 20:23:02 +0100216 }
217
218 __get_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
219 __get_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
220 __get_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
221 __get_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
222 __get_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
223 __get_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
224 __get_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
225 __get_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
226 __get_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
227 __get_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
228 __get_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
229 __get_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
230 __get_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
231 __get_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
232 __get_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
233 __get_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
234 __get_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 err |= !valid_user_regs(regs);
237
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100238 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100239#ifdef CONFIG_CRUNCH
240 if (err == 0)
241 err |= restore_crunch_context(&aux->crunch);
242#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243#ifdef CONFIG_IWMMXT
244 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
245 err |= restore_iwmmxt_context(&aux->iwmmxt);
246#endif
247#ifdef CONFIG_VFP
Imre Deak82c6f5a2010-04-11 15:58:27 +0100248 if (err == 0)
249 err |= restore_vfp_context(&aux->vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250#endif
251
252 return err;
253}
254
255asmlinkage int sys_sigreturn(struct pt_regs *regs)
256{
257 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 /* Always make any pending restarted system calls return -EINTR */
260 current_thread_info()->restart_block.fn = do_no_restart_syscall;
261
262 /*
263 * Since we stacked the signal on a 64-bit boundary,
264 * then 'sp' should be word aligned here. If it's
265 * not, then the user is trying to mess with us.
266 */
267 if (regs->ARM_sp & 7)
268 goto badframe;
269
270 frame = (struct sigframe __user *)regs->ARM_sp;
271
272 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
273 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Russell King68071482006-06-15 20:23:02 +0100275 if (restore_sigframe(regs, frame))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 goto badframe;
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return regs->ARM_r0;
279
280badframe:
281 force_sig(SIGSEGV, current);
282 return 0;
283}
284
285asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
286{
287 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 /* Always make any pending restarted system calls return -EINTR */
290 current_thread_info()->restart_block.fn = do_no_restart_syscall;
291
292 /*
293 * Since we stacked the signal on a 64-bit boundary,
294 * then 'sp' should be word aligned here. If it's
295 * not, then the user is trying to mess with us.
296 */
297 if (regs->ARM_sp & 7)
298 goto badframe;
299
300 frame = (struct rt_sigframe __user *)regs->ARM_sp;
301
302 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
303 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Russell King68071482006-06-15 20:23:02 +0100305 if (restore_sigframe(regs, &frame->sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 goto badframe;
307
Russell Kingcb3504e2006-06-15 20:18:25 +0100308 if (do_sigaltstack(&frame->sig.uc.uc_stack, NULL, regs->ARM_sp) == -EFAULT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 goto badframe;
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return regs->ARM_r0;
312
313badframe:
314 force_sig(SIGSEGV, current);
315 return 0;
316}
317
318static int
Russell Kingaca6ca12006-06-15 20:28:03 +0100319setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs, sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100321 struct aux_sigframe __user *aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 int err = 0;
323
Russell Kingaca6ca12006-06-15 20:28:03 +0100324 __put_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
325 __put_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
326 __put_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
327 __put_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
328 __put_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
329 __put_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
330 __put_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
331 __put_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
332 __put_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
333 __put_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
334 __put_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
335 __put_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
336 __put_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
337 __put_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
338 __put_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
339 __put_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
340 __put_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Russell Kingaca6ca12006-06-15 20:28:03 +0100342 __put_user_error(current->thread.trap_no, &sf->uc.uc_mcontext.trap_no, err);
343 __put_user_error(current->thread.error_code, &sf->uc.uc_mcontext.error_code, err);
344 __put_user_error(current->thread.address, &sf->uc.uc_mcontext.fault_address, err);
345 __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Russell Kingaca6ca12006-06-15 20:28:03 +0100347 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100349 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100350#ifdef CONFIG_CRUNCH
351 if (err == 0)
352 err |= preserve_crunch_context(&aux->crunch);
353#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354#ifdef CONFIG_IWMMXT
355 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
356 err |= preserve_iwmmxt_context(&aux->iwmmxt);
357#endif
358#ifdef CONFIG_VFP
Imre Deak82c6f5a2010-04-11 15:58:27 +0100359 if (err == 0)
360 err |= preserve_vfp_context(&aux->vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361#endif
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100362 __put_user_error(0, &aux->end_magic, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 return err;
365}
366
367static inline void __user *
368get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
369{
370 unsigned long sp = regs->ARM_sp;
371 void __user *frame;
372
373 /*
374 * This is the X/Open sanctioned signal stack switching.
375 */
376 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
377 sp = current->sas_ss_sp + current->sas_ss_size;
378
379 /*
380 * ATPCS B01 mandates 8-byte alignment
381 */
382 frame = (void __user *)((sp - framesize) & ~7);
383
384 /*
385 * Check that we can actually write to the signal frame.
386 */
387 if (!access_ok(VERIFY_WRITE, frame, framesize))
388 frame = NULL;
389
390 return frame;
391}
392
393static int
394setup_return(struct pt_regs *regs, struct k_sigaction *ka,
395 unsigned long __user *rc, void __user *frame, int usig)
396{
397 unsigned long handler = (unsigned long)ka->sa.sa_handler;
398 unsigned long retcode;
399 int thumb = 0;
Russell King53399052011-02-20 12:22:52 +0000400 unsigned long cpsr = regs->ARM_cpsr & ~(PSR_f | PSR_E_BIT);
401
402 cpsr |= PSR_ENDSTATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 /*
405 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
406 */
407 if (ka->sa.sa_flags & SA_THIRTYTWO)
408 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
409
410#ifdef CONFIG_ARM_THUMB
411 if (elf_hwcap & HWCAP_THUMB) {
412 /*
413 * The LSB of the handler determines if we're going to
414 * be using THUMB or ARM mode for this signal handler.
415 */
416 thumb = handler & 1;
417
Catalin Marinasd71e1352009-05-30 14:00:15 +0100418 if (thumb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 cpsr |= PSR_T_BIT;
Catalin Marinasd71e1352009-05-30 14:00:15 +0100420#if __LINUX_ARM_ARCH__ >= 7
421 /* clear the If-Then Thumb-2 execution state */
422 cpsr &= ~PSR_IT_MASK;
423#endif
424 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 cpsr &= ~PSR_T_BIT;
426 }
427#endif
428
429 if (ka->sa.sa_flags & SA_RESTORER) {
430 retcode = (unsigned long)ka->sa.sa_restorer;
431 } else {
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000432 unsigned int idx = thumb << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 if (ka->sa.sa_flags & SA_SIGINFO)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000435 idx += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000437 if (__put_user(sigreturn_codes[idx], rc) ||
438 __put_user(sigreturn_codes[idx+1], rc+1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return 1;
440
Russell Kinge00d3492005-06-22 20:26:05 +0100441 if (cpsr & MODE32_BIT) {
442 /*
443 * 32-bit code can use the new high-page
444 * signal return code support.
445 */
446 retcode = KERN_SIGRETURN_CODE + (idx << 2) + thumb;
447 } else {
448 /*
449 * Ensure that the instruction cache sees
450 * the return code written onto the stack.
451 */
452 flush_icache_range((unsigned long)rc,
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000453 (unsigned long)(rc + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Russell Kinge00d3492005-06-22 20:26:05 +0100455 retcode = ((unsigned long)rc) + thumb;
456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458
459 regs->ARM_r0 = usig;
460 regs->ARM_sp = (unsigned long)frame;
461 regs->ARM_lr = retcode;
462 regs->ARM_pc = handler;
463 regs->ARM_cpsr = cpsr;
464
465 return 0;
466}
467
468static int
469setup_frame(int usig, struct k_sigaction *ka, sigset_t *set, struct pt_regs *regs)
470{
471 struct sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
472 int err = 0;
473
474 if (!frame)
475 return 1;
476
Russell Kingca195cf2006-06-24 22:41:09 +0100477 /*
478 * Set uc.uc_flags to a value which sc.trap_no would never have.
479 */
480 __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Russell Kingaca6ca12006-06-15 20:28:03 +0100482 err |= setup_sigframe(frame, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (err == 0)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000484 err = setup_return(regs, ka, frame->retcode, frame, usig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 return err;
487}
488
489static int
490setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
491 sigset_t *set, struct pt_regs *regs)
492{
493 struct rt_sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
494 stack_t stack;
495 int err = 0;
496
497 if (!frame)
498 return 1;
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 err |= copy_siginfo_to_user(&frame->info, info);
501
Russell Kingcb3504e2006-06-15 20:18:25 +0100502 __put_user_error(0, &frame->sig.uc.uc_flags, err);
503 __put_user_error(NULL, &frame->sig.uc.uc_link, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 memset(&stack, 0, sizeof(stack));
506 stack.ss_sp = (void __user *)current->sas_ss_sp;
507 stack.ss_flags = sas_ss_flags(regs->ARM_sp);
508 stack.ss_size = current->sas_ss_size;
Russell Kingcb3504e2006-06-15 20:18:25 +0100509 err |= __copy_to_user(&frame->sig.uc.uc_stack, &stack, sizeof(stack));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Russell Kingaca6ca12006-06-15 20:28:03 +0100511 err |= setup_sigframe(&frame->sig, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (err == 0)
Russell Kingcb3504e2006-06-15 20:18:25 +0100513 err = setup_return(regs, ka, frame->sig.retcode, frame, usig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 if (err == 0) {
516 /*
517 * For realtime signals we must also set the second and third
518 * arguments for the signal handler.
519 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
520 */
521 regs->ARM_r1 = (unsigned long)&frame->info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100522 regs->ARM_r2 = (unsigned long)&frame->sig.uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
524
525 return err;
526}
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528/*
529 * OK, we're invoking a handler
530 */
Mikael Pettersson36984262009-08-15 12:58:11 +0100531static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532handle_signal(unsigned long sig, struct k_sigaction *ka,
Al Virob7f9a112012-05-02 09:59:21 -0400533 siginfo_t *info, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 struct thread_info *thread = current_thread_info();
536 struct task_struct *tsk = current;
Al Virob7f9a112012-05-02 09:59:21 -0400537 sigset_t *oldset = sigmask_to_save();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 int usig = sig;
539 int ret;
540
541 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 * translate the signal
543 */
544 if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
545 usig = thread->exec_domain->signal_invmap[usig];
546
547 /*
548 * Set up the stack frame
549 */
550 if (ka->sa.sa_flags & SA_SIGINFO)
551 ret = setup_rt_frame(usig, ka, info, oldset, regs);
552 else
553 ret = setup_frame(usig, ka, oldset, regs);
554
555 /*
556 * Check that the resulting registers are actually sane.
557 */
558 ret |= !valid_user_regs(regs);
559
Steven Rostedt69be8f12005-08-29 11:44:09 -0400560 if (ret != 0) {
Daniel Jacobowitza6c61e92005-11-19 10:01:07 +0000561 force_sigsegv(sig, tsk);
Mikael Pettersson36984262009-08-15 12:58:11 +0100562 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564
Daniel Jacobowitza6c61e92005-11-19 10:01:07 +0000565 /*
566 * Block the signal if we were successful.
567 */
Matt Fleming101d9b02012-03-05 15:05:34 -0800568 block_sigmask(ka, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Wade Farnsworth0693bf62012-04-04 16:19:47 +0100570 tracehook_signal_handler(sig, info, ka, regs, 0);
571
Mikael Pettersson36984262009-08-15 12:58:11 +0100572 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
575/*
576 * Note that 'init' is a special process: it doesn't get signals it doesn't
577 * want to handle. Thus you cannot kill init even with a SIGKILL even by
578 * mistake.
579 *
580 * Note that we go through the signals twice: once to check the signals that
581 * the kernel can handle, and then we build all the user-level signal handling
582 * stack-frames in one go after that.
583 */
Mikael Pettersson36984262009-08-15 12:58:11 +0100584static void do_signal(struct pt_regs *regs, int syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100586 unsigned int retval = 0, continue_addr = 0, restart_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 struct k_sigaction ka;
588 siginfo_t info;
589 int signr;
590
591 /*
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100592 * If we were from a system call, check for system call restarting...
593 */
594 if (syscall) {
595 continue_addr = regs->ARM_pc;
596 restart_addr = continue_addr - (thumb_mode(regs) ? 2 : 4);
597 retval = regs->ARM_r0;
598
599 /*
600 * Prepare for system call restart. We do this here so that a
601 * debugger will see the already changed PSW.
602 */
603 switch (retval) {
604 case -ERESTARTNOHAND:
605 case -ERESTARTSYS:
606 case -ERESTARTNOINTR:
Al Viro6b5c8042012-05-02 21:02:03 -0400607 case -ERESTART_RESTARTBLOCK:
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100608 regs->ARM_r0 = regs->ARM_ORIG_r0;
609 regs->ARM_pc = restart_addr;
610 break;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100611 }
612 }
613
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100614 /*
615 * Get the signal to deliver. When running under ptrace, at this
616 * point the debugger may change all our registers ...
617 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
619 if (signr > 0) {
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100620 /*
621 * Depending on the signal settings we may need to revert the
622 * decision to restart the system call. But skip this if a
623 * debugger has chosen to restart at a different PC.
624 */
625 if (regs->ARM_pc == restart_addr) {
Al Viro6b5c8042012-05-02 21:02:03 -0400626 if (retval == -ERESTARTNOHAND ||
627 retval == -ERESTART_RESTARTBLOCK
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100628 || (retval == -ERESTARTSYS
629 && !(ka.sa.sa_flags & SA_RESTART))) {
630 regs->ARM_r0 = -EINTR;
631 regs->ARM_pc = continue_addr;
632 }
Al Viro6b5c8042012-05-02 21:02:03 -0400633 clear_thread_flag(TIF_SYSCALL_RESTARTSYS);
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100634 }
635
Al Virob7f9a112012-05-02 09:59:21 -0400636 if (handle_signal(signr, &ka, &info, regs) == 0) {
Mikael Pettersson36984262009-08-15 12:58:11 +0100637 /*
638 * A signal was successfully delivered; the saved
639 * sigmask will have been stored in the signal frame,
640 * and will be restored by sigreturn, so we can simply
641 * clear the TIF_RESTORE_SIGMASK flag.
642 */
643 if (test_thread_flag(TIF_RESTORE_SIGMASK))
644 clear_thread_flag(TIF_RESTORE_SIGMASK);
645 }
Mikael Pettersson36984262009-08-15 12:58:11 +0100646 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (syscall) {
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100650 /*
651 * Handle restarting a different system call. As above,
652 * if a debugger has chosen to restart at a different PC,
653 * ignore the restart.
654 */
655 if (retval == -ERESTART_RESTARTBLOCK
Al Viro6b5c8042012-05-02 21:02:03 -0400656 && regs->ARM_pc == restart_addr)
657 set_thread_flag(TIF_SYSCALL_RESTARTSYS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
Al Viro7dfae722012-04-26 18:15:11 -0400659
Al Viro51a7b442012-05-21 23:33:55 -0400660 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
663asmlinkage void
664do_notify_resume(struct pt_regs *regs, unsigned int thread_flags, int syscall)
665{
666 if (thread_flags & _TIF_SIGPENDING)
Mikael Pettersson36984262009-08-15 12:58:11 +0100667 do_signal(regs, syscall);
David Howellsd0420c82009-09-02 09:14:16 +0100668
669 if (thread_flags & _TIF_NOTIFY_RESUME) {
670 clear_thread_flag(TIF_NOTIFY_RESUME);
671 tracehook_notify_resume(regs);
672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}