blob: 56f72d257ebdfcdfd8607fcc770eeecc61c24806 [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>
Russell King33fa9b12008-09-06 11:35:55 +010013#include <linux/uaccess.h>
David Howells733e5e42009-09-09 08:30:21 +010014#include <linux/tracehook.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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/unistd.h>
Imre Deak82c6f5a2010-04-11 15:58:27 +010020#include <asm/vfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Russell Kinge00d3492005-06-22 20:26:05 +010022#include "signal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024/*
25 * For ARM syscalls, we encode the syscall number into the instruction.
26 */
janboeee4cd582008-03-19 03:34:23 +010027#define SWI_SYS_SIGRETURN (0xef000000|(__NR_sigreturn)|(__NR_OABI_SYSCALL_BASE))
28#define SWI_SYS_RT_SIGRETURN (0xef000000|(__NR_rt_sigreturn)|(__NR_OABI_SYSCALL_BASE))
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/*
Nicolas Pitrefcca5382006-01-18 22:38:47 +000031 * With EABI, the syscall number has to be loaded into r7.
32 */
33#define MOV_R7_NR_SIGRETURN (0xe3a07000 | (__NR_sigreturn - __NR_SYSCALL_BASE))
34#define MOV_R7_NR_RT_SIGRETURN (0xe3a07000 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
35
36/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * For Thumb syscalls, we pass the syscall number via r7. We therefore
38 * need two 16-bit instructions.
39 */
40#define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (__NR_sigreturn - __NR_SYSCALL_BASE))
41#define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
42
Nicolas Pitrefcca5382006-01-18 22:38:47 +000043const unsigned long sigreturn_codes[7] = {
44 MOV_R7_NR_SIGRETURN, SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
45 MOV_R7_NR_RT_SIGRETURN, SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN,
Linus Torvalds1da177e2005-04-16 15:20:36 -070046};
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/*
49 * atomically swap in the new signal mask, and wait for a signal.
50 */
Mikael Pettersson36984262009-08-15 12:58:11 +010051asmlinkage int sys_sigsuspend(int restart, unsigned long oldmask, old_sigset_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Matt Fleming101d9b02012-03-05 15:05:34 -080053 sigset_t blocked;
Matt Fleming101d9b02012-03-05 15:05:34 -080054 siginitset(&blocked, mask);
Al Viro68f3f162012-05-21 21:42:32 -040055 return sigsuspend(&blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
58asmlinkage int
59sys_sigaction(int sig, const struct old_sigaction __user *act,
60 struct old_sigaction __user *oact)
61{
62 struct k_sigaction new_ka, old_ka;
63 int ret;
64
65 if (act) {
66 old_sigset_t mask;
67 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
68 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
Al Viro7f1b5a92012-04-22 17:15:42 -040069 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
70 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
71 __get_user(mask, &act->sa_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 siginitset(&new_ka.sa.sa_mask, mask);
74 }
75
76 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
77
78 if (!ret && oact) {
79 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
80 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
Al Viro7f1b5a92012-04-22 17:15:42 -040081 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
82 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
83 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86
87 return ret;
88}
89
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010090#ifdef CONFIG_CRUNCH
Hartley Sweeten65a50532009-08-04 23:20:45 +010091static int preserve_crunch_context(struct crunch_sigframe __user *frame)
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010092{
93 char kbuf[sizeof(*frame) + 8];
94 struct crunch_sigframe *kframe;
95
96 /* the crunch context must be 64 bit aligned */
97 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
98 kframe->magic = CRUNCH_MAGIC;
99 kframe->size = CRUNCH_STORAGE_SIZE;
100 crunch_task_copy(current_thread_info(), &kframe->storage);
101 return __copy_to_user(frame, kframe, sizeof(*frame));
102}
103
Hartley Sweeten65a50532009-08-04 23:20:45 +0100104static int restore_crunch_context(struct crunch_sigframe __user *frame)
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100105{
106 char kbuf[sizeof(*frame) + 8];
107 struct crunch_sigframe *kframe;
108
109 /* the crunch context must be 64 bit aligned */
110 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
111 if (__copy_from_user(kframe, frame, sizeof(*frame)))
112 return -1;
113 if (kframe->magic != CRUNCH_MAGIC ||
114 kframe->size != CRUNCH_STORAGE_SIZE)
115 return -1;
116 crunch_task_restore(current_thread_info(), &kframe->storage);
117 return 0;
118}
119#endif
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#ifdef CONFIG_IWMMXT
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static int preserve_iwmmxt_context(struct iwmmxt_sigframe *frame)
124{
Hugh Dickins69b04752005-10-29 18:16:36 -0700125 char kbuf[sizeof(*frame) + 8];
126 struct iwmmxt_sigframe *kframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 /* the iWMMXt context must be 64 bit aligned */
Hugh Dickins69b04752005-10-29 18:16:36 -0700129 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100130 kframe->magic = IWMMXT_MAGIC;
131 kframe->size = IWMMXT_STORAGE_SIZE;
Hugh Dickins69b04752005-10-29 18:16:36 -0700132 iwmmxt_task_copy(current_thread_info(), &kframe->storage);
133 return __copy_to_user(frame, kframe, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
136static int restore_iwmmxt_context(struct iwmmxt_sigframe *frame)
137{
Hugh Dickins69b04752005-10-29 18:16:36 -0700138 char kbuf[sizeof(*frame) + 8];
139 struct iwmmxt_sigframe *kframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Hugh Dickins69b04752005-10-29 18:16:36 -0700141 /* the iWMMXt context must be 64 bit aligned */
142 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
143 if (__copy_from_user(kframe, frame, sizeof(*frame)))
144 return -1;
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100145 if (kframe->magic != IWMMXT_MAGIC ||
146 kframe->size != IWMMXT_STORAGE_SIZE)
Hugh Dickins69b04752005-10-29 18:16:36 -0700147 return -1;
148 iwmmxt_task_restore(current_thread_info(), &kframe->storage);
149 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
152#endif
153
Imre Deak82c6f5a2010-04-11 15:58:27 +0100154#ifdef CONFIG_VFP
155
156static int preserve_vfp_context(struct vfp_sigframe __user *frame)
157{
Imre Deak82c6f5a2010-04-11 15:58:27 +0100158 const unsigned long magic = VFP_MAGIC;
159 const unsigned long size = VFP_STORAGE_SIZE;
160 int err = 0;
161
Imre Deak82c6f5a2010-04-11 15:58:27 +0100162 __put_user_error(magic, &frame->magic, err);
163 __put_user_error(size, &frame->size, err);
164
Will Deacon2498814f2012-04-23 15:38:28 +0100165 if (err)
166 return -EFAULT;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100167
Will Deacon2498814f2012-04-23 15:38:28 +0100168 return vfp_preserve_user_clear_hwstate(&frame->ufp, &frame->ufp_exc);
Imre Deak82c6f5a2010-04-11 15:58:27 +0100169}
170
171static int restore_vfp_context(struct vfp_sigframe __user *frame)
172{
Imre Deak82c6f5a2010-04-11 15:58:27 +0100173 unsigned long magic;
174 unsigned long size;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100175 int err = 0;
176
177 __get_user_error(magic, &frame->magic, err);
178 __get_user_error(size, &frame->size, err);
179
180 if (err)
181 return -EFAULT;
182 if (magic != VFP_MAGIC || size != VFP_STORAGE_SIZE)
183 return -EINVAL;
184
Will Deacon2498814f2012-04-23 15:38:28 +0100185 return vfp_restore_user_hwstate(&frame->ufp, &frame->ufp_exc);
Imre Deak82c6f5a2010-04-11 15:58:27 +0100186}
187
188#endif
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * Do a signal return; undo the signal stack. These are aligned to 64-bit.
192 */
193struct sigframe {
Russell King7d4fdc12006-06-15 20:13:25 +0100194 struct ucontext uc;
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000195 unsigned long retcode[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196};
197
198struct rt_sigframe {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 struct siginfo info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100200 struct sigframe sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201};
202
Russell King68071482006-06-15 20:23:02 +0100203static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100205 struct aux_sigframe __user *aux;
Russell King68071482006-06-15 20:23:02 +0100206 sigset_t set;
207 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Russell King68071482006-06-15 20:23:02 +0100209 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
Al Viro77097ae2012-04-27 13:58:59 -0400210 if (err == 0)
Matt Fleming101d9b02012-03-05 15:05:34 -0800211 set_current_blocked(&set);
Russell King68071482006-06-15 20:23:02 +0100212
213 __get_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
214 __get_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
215 __get_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
216 __get_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
217 __get_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
218 __get_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
219 __get_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
220 __get_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
221 __get_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
222 __get_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
223 __get_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
224 __get_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
225 __get_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
226 __get_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
227 __get_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
228 __get_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
229 __get_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 err |= !valid_user_regs(regs);
232
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100233 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100234#ifdef CONFIG_CRUNCH
235 if (err == 0)
236 err |= restore_crunch_context(&aux->crunch);
237#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238#ifdef CONFIG_IWMMXT
239 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
240 err |= restore_iwmmxt_context(&aux->iwmmxt);
241#endif
242#ifdef CONFIG_VFP
Imre Deak82c6f5a2010-04-11 15:58:27 +0100243 if (err == 0)
244 err |= restore_vfp_context(&aux->vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245#endif
246
247 return err;
248}
249
250asmlinkage int sys_sigreturn(struct pt_regs *regs)
251{
252 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 /* Always make any pending restarted system calls return -EINTR */
255 current_thread_info()->restart_block.fn = do_no_restart_syscall;
256
257 /*
258 * Since we stacked the signal on a 64-bit boundary,
259 * then 'sp' should be word aligned here. If it's
260 * not, then the user is trying to mess with us.
261 */
262 if (regs->ARM_sp & 7)
263 goto badframe;
264
265 frame = (struct sigframe __user *)regs->ARM_sp;
266
267 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
268 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Russell King68071482006-06-15 20:23:02 +0100270 if (restore_sigframe(regs, frame))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 goto badframe;
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return regs->ARM_r0;
274
275badframe:
276 force_sig(SIGSEGV, current);
277 return 0;
278}
279
280asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
281{
282 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 /* Always make any pending restarted system calls return -EINTR */
285 current_thread_info()->restart_block.fn = do_no_restart_syscall;
286
287 /*
288 * Since we stacked the signal on a 64-bit boundary,
289 * then 'sp' should be word aligned here. If it's
290 * not, then the user is trying to mess with us.
291 */
292 if (regs->ARM_sp & 7)
293 goto badframe;
294
295 frame = (struct rt_sigframe __user *)regs->ARM_sp;
296
297 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
298 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Russell King68071482006-06-15 20:23:02 +0100300 if (restore_sigframe(regs, &frame->sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 goto badframe;
302
Russell Kingcb3504e2006-06-15 20:18:25 +0100303 if (do_sigaltstack(&frame->sig.uc.uc_stack, NULL, regs->ARM_sp) == -EFAULT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 goto badframe;
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return regs->ARM_r0;
307
308badframe:
309 force_sig(SIGSEGV, current);
310 return 0;
311}
312
313static int
Russell Kingaca6ca12006-06-15 20:28:03 +0100314setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs, sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100316 struct aux_sigframe __user *aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 int err = 0;
318
Russell Kingaca6ca12006-06-15 20:28:03 +0100319 __put_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
320 __put_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
321 __put_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
322 __put_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
323 __put_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
324 __put_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
325 __put_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
326 __put_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
327 __put_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
328 __put_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
329 __put_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
330 __put_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
331 __put_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
332 __put_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
333 __put_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
334 __put_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
335 __put_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Russell Kingaca6ca12006-06-15 20:28:03 +0100337 __put_user_error(current->thread.trap_no, &sf->uc.uc_mcontext.trap_no, err);
338 __put_user_error(current->thread.error_code, &sf->uc.uc_mcontext.error_code, err);
339 __put_user_error(current->thread.address, &sf->uc.uc_mcontext.fault_address, err);
340 __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Russell Kingaca6ca12006-06-15 20:28:03 +0100342 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100344 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100345#ifdef CONFIG_CRUNCH
346 if (err == 0)
347 err |= preserve_crunch_context(&aux->crunch);
348#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349#ifdef CONFIG_IWMMXT
350 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
351 err |= preserve_iwmmxt_context(&aux->iwmmxt);
352#endif
353#ifdef CONFIG_VFP
Imre Deak82c6f5a2010-04-11 15:58:27 +0100354 if (err == 0)
355 err |= preserve_vfp_context(&aux->vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356#endif
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100357 __put_user_error(0, &aux->end_magic, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 return err;
360}
361
362static inline void __user *
363get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
364{
365 unsigned long sp = regs->ARM_sp;
366 void __user *frame;
367
368 /*
369 * This is the X/Open sanctioned signal stack switching.
370 */
371 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
372 sp = current->sas_ss_sp + current->sas_ss_size;
373
374 /*
375 * ATPCS B01 mandates 8-byte alignment
376 */
377 frame = (void __user *)((sp - framesize) & ~7);
378
379 /*
380 * Check that we can actually write to the signal frame.
381 */
382 if (!access_ok(VERIFY_WRITE, frame, framesize))
383 frame = NULL;
384
385 return frame;
386}
387
388static int
389setup_return(struct pt_regs *regs, struct k_sigaction *ka,
390 unsigned long __user *rc, void __user *frame, int usig)
391{
392 unsigned long handler = (unsigned long)ka->sa.sa_handler;
393 unsigned long retcode;
394 int thumb = 0;
Russell King53399052011-02-20 12:22:52 +0000395 unsigned long cpsr = regs->ARM_cpsr & ~(PSR_f | PSR_E_BIT);
396
397 cpsr |= PSR_ENDSTATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 /*
400 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
401 */
402 if (ka->sa.sa_flags & SA_THIRTYTWO)
403 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
404
405#ifdef CONFIG_ARM_THUMB
406 if (elf_hwcap & HWCAP_THUMB) {
407 /*
408 * The LSB of the handler determines if we're going to
409 * be using THUMB or ARM mode for this signal handler.
410 */
411 thumb = handler & 1;
412
Catalin Marinasd71e1352009-05-30 14:00:15 +0100413 if (thumb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 cpsr |= PSR_T_BIT;
Catalin Marinasd71e1352009-05-30 14:00:15 +0100415#if __LINUX_ARM_ARCH__ >= 7
416 /* clear the If-Then Thumb-2 execution state */
417 cpsr &= ~PSR_IT_MASK;
418#endif
419 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 cpsr &= ~PSR_T_BIT;
421 }
422#endif
423
424 if (ka->sa.sa_flags & SA_RESTORER) {
425 retcode = (unsigned long)ka->sa.sa_restorer;
426 } else {
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000427 unsigned int idx = thumb << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 if (ka->sa.sa_flags & SA_SIGINFO)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000430 idx += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000432 if (__put_user(sigreturn_codes[idx], rc) ||
433 __put_user(sigreturn_codes[idx+1], rc+1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return 1;
435
Russell Kinge00d3492005-06-22 20:26:05 +0100436 if (cpsr & MODE32_BIT) {
437 /*
438 * 32-bit code can use the new high-page
439 * signal return code support.
440 */
441 retcode = KERN_SIGRETURN_CODE + (idx << 2) + thumb;
442 } else {
443 /*
444 * Ensure that the instruction cache sees
445 * the return code written onto the stack.
446 */
447 flush_icache_range((unsigned long)rc,
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000448 (unsigned long)(rc + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Russell Kinge00d3492005-06-22 20:26:05 +0100450 retcode = ((unsigned long)rc) + thumb;
451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453
454 regs->ARM_r0 = usig;
455 regs->ARM_sp = (unsigned long)frame;
456 regs->ARM_lr = retcode;
457 regs->ARM_pc = handler;
458 regs->ARM_cpsr = cpsr;
459
460 return 0;
461}
462
463static int
464setup_frame(int usig, struct k_sigaction *ka, sigset_t *set, struct pt_regs *regs)
465{
466 struct sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
467 int err = 0;
468
469 if (!frame)
470 return 1;
471
Russell Kingca195cf2006-06-24 22:41:09 +0100472 /*
473 * Set uc.uc_flags to a value which sc.trap_no would never have.
474 */
475 __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Russell Kingaca6ca12006-06-15 20:28:03 +0100477 err |= setup_sigframe(frame, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (err == 0)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000479 err = setup_return(regs, ka, frame->retcode, frame, usig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 return err;
482}
483
484static int
485setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
486 sigset_t *set, struct pt_regs *regs)
487{
488 struct rt_sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
489 stack_t stack;
490 int err = 0;
491
492 if (!frame)
493 return 1;
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 err |= copy_siginfo_to_user(&frame->info, info);
496
Russell Kingcb3504e2006-06-15 20:18:25 +0100497 __put_user_error(0, &frame->sig.uc.uc_flags, err);
498 __put_user_error(NULL, &frame->sig.uc.uc_link, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 memset(&stack, 0, sizeof(stack));
501 stack.ss_sp = (void __user *)current->sas_ss_sp;
502 stack.ss_flags = sas_ss_flags(regs->ARM_sp);
503 stack.ss_size = current->sas_ss_size;
Russell Kingcb3504e2006-06-15 20:18:25 +0100504 err |= __copy_to_user(&frame->sig.uc.uc_stack, &stack, sizeof(stack));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Russell Kingaca6ca12006-06-15 20:28:03 +0100506 err |= setup_sigframe(&frame->sig, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (err == 0)
Russell Kingcb3504e2006-06-15 20:18:25 +0100508 err = setup_return(regs, ka, frame->sig.retcode, frame, usig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 if (err == 0) {
511 /*
512 * For realtime signals we must also set the second and third
513 * arguments for the signal handler.
514 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
515 */
516 regs->ARM_r1 = (unsigned long)&frame->info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100517 regs->ARM_r2 = (unsigned long)&frame->sig.uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519
520 return err;
521}
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523/*
524 * OK, we're invoking a handler
525 */
Al Viroa610d6e2012-05-21 23:42:15 -0400526static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527handle_signal(unsigned long sig, struct k_sigaction *ka,
Al Virob7f9a112012-05-02 09:59:21 -0400528 siginfo_t *info, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 struct thread_info *thread = current_thread_info();
531 struct task_struct *tsk = current;
Al Virob7f9a112012-05-02 09:59:21 -0400532 sigset_t *oldset = sigmask_to_save();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 int usig = sig;
534 int ret;
535
536 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 * translate the signal
538 */
539 if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
540 usig = thread->exec_domain->signal_invmap[usig];
541
542 /*
543 * Set up the stack frame
544 */
545 if (ka->sa.sa_flags & SA_SIGINFO)
546 ret = setup_rt_frame(usig, ka, info, oldset, regs);
547 else
548 ret = setup_frame(usig, ka, oldset, regs);
549
550 /*
551 * Check that the resulting registers are actually sane.
552 */
553 ret |= !valid_user_regs(regs);
554
Steven Rostedt69be8f12005-08-29 11:44:09 -0400555 if (ret != 0) {
Daniel Jacobowitza6c61e92005-11-19 10:01:07 +0000556 force_sigsegv(sig, tsk);
Al Viroa610d6e2012-05-21 23:42:15 -0400557 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
Al Viroefee9842012-04-28 02:04:15 -0400559 signal_delivered(sig, info, ka, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
562/*
563 * Note that 'init' is a special process: it doesn't get signals it doesn't
564 * want to handle. Thus you cannot kill init even with a SIGKILL even by
565 * mistake.
566 *
567 * Note that we go through the signals twice: once to check the signals that
568 * the kernel can handle, and then we build all the user-level signal handling
569 * stack-frames in one go after that.
570 */
Al Viro81783782012-07-19 17:48:21 +0100571static int do_signal(struct pt_regs *regs, int syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100573 unsigned int retval = 0, continue_addr = 0, restart_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 struct k_sigaction ka;
575 siginfo_t info;
576 int signr;
Al Viro81783782012-07-19 17:48:21 +0100577 int restart = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 /*
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100580 * If we were from a system call, check for system call restarting...
581 */
582 if (syscall) {
583 continue_addr = regs->ARM_pc;
584 restart_addr = continue_addr - (thumb_mode(regs) ? 2 : 4);
585 retval = regs->ARM_r0;
586
587 /*
588 * Prepare for system call restart. We do this here so that a
589 * debugger will see the already changed PSW.
590 */
591 switch (retval) {
Al Viro81783782012-07-19 17:48:21 +0100592 case -ERESTART_RESTARTBLOCK:
Al Viro66285212012-07-19 17:48:50 +0100593 restart -= 2;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100594 case -ERESTARTNOHAND:
595 case -ERESTARTSYS:
596 case -ERESTARTNOINTR:
Al Viro81783782012-07-19 17:48:21 +0100597 restart++;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100598 regs->ARM_r0 = regs->ARM_ORIG_r0;
599 regs->ARM_pc = restart_addr;
600 break;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100601 }
602 }
603
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100604 /*
605 * Get the signal to deliver. When running under ptrace, at this
606 * point the debugger may change all our registers ...
607 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
Al Viro81783782012-07-19 17:48:21 +0100609 /*
610 * Depending on the signal settings we may need to revert the
611 * decision to restart the system call. But skip this if a
612 * debugger has chosen to restart at a different PC.
613 */
614 if (regs->ARM_pc != restart_addr)
615 restart = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (signr > 0) {
Al Viro81783782012-07-19 17:48:21 +0100617 if (unlikely(restart)) {
Will Deaconad82cc02012-07-19 17:46:44 +0100618 if (retval == -ERESTARTNOHAND ||
619 retval == -ERESTART_RESTARTBLOCK
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100620 || (retval == -ERESTARTSYS
621 && !(ka.sa.sa_flags & SA_RESTART))) {
622 regs->ARM_r0 = -EINTR;
623 regs->ARM_pc = continue_addr;
624 }
625 }
626
Al Viroa610d6e2012-05-21 23:42:15 -0400627 handle_signal(signr, &ka, &info, regs);
Al Viro81783782012-07-19 17:48:21 +0100628 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
630
Al Viro51a7b442012-05-21 23:33:55 -0400631 restore_saved_sigmask();
Al Viro66285212012-07-19 17:48:50 +0100632 if (unlikely(restart))
633 regs->ARM_pc = continue_addr;
Al Viro81783782012-07-19 17:48:21 +0100634 return restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
Al Viro81783782012-07-19 17:48:21 +0100637asmlinkage int
Al Viro0a267fa2012-07-19 17:47:55 +0100638do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Al Viro0a267fa2012-07-19 17:47:55 +0100640 do {
641 if (likely(thread_flags & _TIF_NEED_RESCHED)) {
642 schedule();
643 } else {
644 if (unlikely(!user_mode(regs)))
Al Viro81783782012-07-19 17:48:21 +0100645 return 0;
Al Viro0a267fa2012-07-19 17:47:55 +0100646 local_irq_enable();
647 if (thread_flags & _TIF_SIGPENDING) {
Al Viro66285212012-07-19 17:48:50 +0100648 int restart = do_signal(regs, syscall);
649 if (unlikely(restart)) {
Al Viro81783782012-07-19 17:48:21 +0100650 /*
651 * Restart without handlers.
652 * Deal with it without leaving
653 * the kernel space.
654 */
Al Viro66285212012-07-19 17:48:50 +0100655 return restart;
Al Viro81783782012-07-19 17:48:21 +0100656 }
Al Viro0a267fa2012-07-19 17:47:55 +0100657 syscall = 0;
658 } else {
659 clear_thread_flag(TIF_NOTIFY_RESUME);
660 tracehook_notify_resume(regs);
661 }
662 }
663 local_irq_disable();
664 thread_flags = current_thread_info()->flags;
665 } while (thread_flags & _TIF_WORK_MASK);
Al Viro81783782012-07-19 17:48:21 +0100666 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}