blob: c126eba8411d06dce594ebffe7a68c57827c435f [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/*
26 * For ARM syscalls, we encode the syscall number into the instruction.
27 */
janboeee4cd582008-03-19 03:34:23 +010028#define SWI_SYS_SIGRETURN (0xef000000|(__NR_sigreturn)|(__NR_OABI_SYSCALL_BASE))
29#define SWI_SYS_RT_SIGRETURN (0xef000000|(__NR_rt_sigreturn)|(__NR_OABI_SYSCALL_BASE))
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/*
Nicolas Pitrefcca5382006-01-18 22:38:47 +000032 * With EABI, the syscall number has to be loaded into r7.
33 */
34#define MOV_R7_NR_SIGRETURN (0xe3a07000 | (__NR_sigreturn - __NR_SYSCALL_BASE))
35#define MOV_R7_NR_RT_SIGRETURN (0xe3a07000 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
36
37/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * For Thumb syscalls, we pass the syscall number via r7. We therefore
39 * need two 16-bit instructions.
40 */
41#define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (__NR_sigreturn - __NR_SYSCALL_BASE))
42#define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
43
Nicolas Pitrefcca5382006-01-18 22:38:47 +000044const unsigned long sigreturn_codes[7] = {
45 MOV_R7_NR_SIGRETURN, SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
46 MOV_R7_NR_RT_SIGRETURN, SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047};
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/*
50 * atomically swap in the new signal mask, and wait for a signal.
51 */
Mikael Pettersson36984262009-08-15 12:58:11 +010052asmlinkage int sys_sigsuspend(int restart, unsigned long oldmask, old_sigset_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Matt Fleming101d9b02012-03-05 15:05:34 -080054 sigset_t blocked;
Matt Fleming101d9b02012-03-05 15:05:34 -080055 siginitset(&blocked, mask);
Al Viro68f3f162012-05-21 21:42:32 -040056 return sigsuspend(&blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
59asmlinkage int
60sys_sigaction(int sig, const struct old_sigaction __user *act,
61 struct old_sigaction __user *oact)
62{
63 struct k_sigaction new_ka, old_ka;
64 int ret;
65
66 if (act) {
67 old_sigset_t mask;
68 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
69 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
Al Viro7f1b5a92012-04-22 17:15:42 -040070 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
71 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
72 __get_user(mask, &act->sa_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 siginitset(&new_ka.sa.sa_mask, mask);
75 }
76
77 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
78
79 if (!ret && oact) {
80 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
81 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
Al Viro7f1b5a92012-04-22 17:15:42 -040082 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
83 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
84 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
87
88 return ret;
89}
90
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010091#ifdef CONFIG_CRUNCH
Hartley Sweeten65a50532009-08-04 23:20:45 +010092static int preserve_crunch_context(struct crunch_sigframe __user *frame)
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010093{
94 char kbuf[sizeof(*frame) + 8];
95 struct crunch_sigframe *kframe;
96
97 /* the crunch context must be 64 bit aligned */
98 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
99 kframe->magic = CRUNCH_MAGIC;
100 kframe->size = CRUNCH_STORAGE_SIZE;
101 crunch_task_copy(current_thread_info(), &kframe->storage);
102 return __copy_to_user(frame, kframe, sizeof(*frame));
103}
104
Hartley Sweeten65a50532009-08-04 23:20:45 +0100105static int restore_crunch_context(struct crunch_sigframe __user *frame)
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100106{
107 char kbuf[sizeof(*frame) + 8];
108 struct crunch_sigframe *kframe;
109
110 /* the crunch context must be 64 bit aligned */
111 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
112 if (__copy_from_user(kframe, frame, sizeof(*frame)))
113 return -1;
114 if (kframe->magic != CRUNCH_MAGIC ||
115 kframe->size != CRUNCH_STORAGE_SIZE)
116 return -1;
117 crunch_task_restore(current_thread_info(), &kframe->storage);
118 return 0;
119}
120#endif
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#ifdef CONFIG_IWMMXT
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124static int preserve_iwmmxt_context(struct iwmmxt_sigframe *frame)
125{
Hugh Dickins69b04752005-10-29 18:16:36 -0700126 char kbuf[sizeof(*frame) + 8];
127 struct iwmmxt_sigframe *kframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 /* the iWMMXt context must be 64 bit aligned */
Hugh Dickins69b04752005-10-29 18:16:36 -0700130 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100131 kframe->magic = IWMMXT_MAGIC;
132 kframe->size = IWMMXT_STORAGE_SIZE;
Hugh Dickins69b04752005-10-29 18:16:36 -0700133 iwmmxt_task_copy(current_thread_info(), &kframe->storage);
134 return __copy_to_user(frame, kframe, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
137static int restore_iwmmxt_context(struct iwmmxt_sigframe *frame)
138{
Hugh Dickins69b04752005-10-29 18:16:36 -0700139 char kbuf[sizeof(*frame) + 8];
140 struct iwmmxt_sigframe *kframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Hugh Dickins69b04752005-10-29 18:16:36 -0700142 /* the iWMMXt context must be 64 bit aligned */
143 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
144 if (__copy_from_user(kframe, frame, sizeof(*frame)))
145 return -1;
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100146 if (kframe->magic != IWMMXT_MAGIC ||
147 kframe->size != IWMMXT_STORAGE_SIZE)
Hugh Dickins69b04752005-10-29 18:16:36 -0700148 return -1;
149 iwmmxt_task_restore(current_thread_info(), &kframe->storage);
150 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153#endif
154
Imre Deak82c6f5a2010-04-11 15:58:27 +0100155#ifdef CONFIG_VFP
156
157static int preserve_vfp_context(struct vfp_sigframe __user *frame)
158{
Imre Deak82c6f5a2010-04-11 15:58:27 +0100159 const unsigned long magic = VFP_MAGIC;
160 const unsigned long size = VFP_STORAGE_SIZE;
161 int err = 0;
162
Imre Deak82c6f5a2010-04-11 15:58:27 +0100163 __put_user_error(magic, &frame->magic, err);
164 __put_user_error(size, &frame->size, err);
165
Will Deacon2498814f2012-04-23 15:38:28 +0100166 if (err)
167 return -EFAULT;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100168
Will Deacon2498814f2012-04-23 15:38:28 +0100169 return vfp_preserve_user_clear_hwstate(&frame->ufp, &frame->ufp_exc);
Imre Deak82c6f5a2010-04-11 15:58:27 +0100170}
171
172static int restore_vfp_context(struct vfp_sigframe __user *frame)
173{
Imre Deak82c6f5a2010-04-11 15:58:27 +0100174 unsigned long magic;
175 unsigned long size;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100176 int err = 0;
177
178 __get_user_error(magic, &frame->magic, err);
179 __get_user_error(size, &frame->size, err);
180
181 if (err)
182 return -EFAULT;
183 if (magic != VFP_MAGIC || size != VFP_STORAGE_SIZE)
184 return -EINVAL;
185
Will Deacon2498814f2012-04-23 15:38:28 +0100186 return vfp_restore_user_hwstate(&frame->ufp, &frame->ufp_exc);
Imre Deak82c6f5a2010-04-11 15:58:27 +0100187}
188
189#endif
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 * Do a signal return; undo the signal stack. These are aligned to 64-bit.
193 */
194struct sigframe {
Russell King7d4fdc12006-06-15 20:13:25 +0100195 struct ucontext uc;
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000196 unsigned long retcode[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197};
198
199struct rt_sigframe {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 struct siginfo info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100201 struct sigframe sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202};
203
Russell King68071482006-06-15 20:23:02 +0100204static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100206 struct aux_sigframe __user *aux;
Russell King68071482006-06-15 20:23:02 +0100207 sigset_t set;
208 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Russell King68071482006-06-15 20:23:02 +0100210 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
Al Viro77097ae2012-04-27 13:58:59 -0400211 if (err == 0)
Matt Fleming101d9b02012-03-05 15:05:34 -0800212 set_current_blocked(&set);
Russell King68071482006-06-15 20:23:02 +0100213
214 __get_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
215 __get_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
216 __get_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
217 __get_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
218 __get_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
219 __get_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
220 __get_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
221 __get_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
222 __get_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
223 __get_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
224 __get_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
225 __get_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
226 __get_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
227 __get_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
228 __get_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
229 __get_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
230 __get_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 err |= !valid_user_regs(regs);
233
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100234 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100235#ifdef CONFIG_CRUNCH
236 if (err == 0)
237 err |= restore_crunch_context(&aux->crunch);
238#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239#ifdef CONFIG_IWMMXT
240 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
241 err |= restore_iwmmxt_context(&aux->iwmmxt);
242#endif
243#ifdef CONFIG_VFP
Imre Deak82c6f5a2010-04-11 15:58:27 +0100244 if (err == 0)
245 err |= restore_vfp_context(&aux->vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246#endif
247
248 return err;
249}
250
251asmlinkage int sys_sigreturn(struct pt_regs *regs)
252{
253 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 /* Always make any pending restarted system calls return -EINTR */
256 current_thread_info()->restart_block.fn = do_no_restart_syscall;
257
258 /*
259 * Since we stacked the signal on a 64-bit boundary,
260 * then 'sp' should be word aligned here. If it's
261 * not, then the user is trying to mess with us.
262 */
263 if (regs->ARM_sp & 7)
264 goto badframe;
265
266 frame = (struct sigframe __user *)regs->ARM_sp;
267
268 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
269 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Russell King68071482006-06-15 20:23:02 +0100271 if (restore_sigframe(regs, frame))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 goto badframe;
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return regs->ARM_r0;
275
276badframe:
277 force_sig(SIGSEGV, current);
278 return 0;
279}
280
281asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
282{
283 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 /* Always make any pending restarted system calls return -EINTR */
286 current_thread_info()->restart_block.fn = do_no_restart_syscall;
287
288 /*
289 * Since we stacked the signal on a 64-bit boundary,
290 * then 'sp' should be word aligned here. If it's
291 * not, then the user is trying to mess with us.
292 */
293 if (regs->ARM_sp & 7)
294 goto badframe;
295
296 frame = (struct rt_sigframe __user *)regs->ARM_sp;
297
298 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
299 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Russell King68071482006-06-15 20:23:02 +0100301 if (restore_sigframe(regs, &frame->sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 goto badframe;
303
Russell Kingcb3504e2006-06-15 20:18:25 +0100304 if (do_sigaltstack(&frame->sig.uc.uc_stack, NULL, regs->ARM_sp) == -EFAULT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 goto badframe;
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return regs->ARM_r0;
308
309badframe:
310 force_sig(SIGSEGV, current);
311 return 0;
312}
313
314static int
Russell Kingaca6ca12006-06-15 20:28:03 +0100315setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs, sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100317 struct aux_sigframe __user *aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 int err = 0;
319
Russell Kingaca6ca12006-06-15 20:28:03 +0100320 __put_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
321 __put_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
322 __put_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
323 __put_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
324 __put_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
325 __put_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
326 __put_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
327 __put_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
328 __put_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
329 __put_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
330 __put_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
331 __put_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
332 __put_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
333 __put_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
334 __put_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
335 __put_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
336 __put_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Russell Kingaca6ca12006-06-15 20:28:03 +0100338 __put_user_error(current->thread.trap_no, &sf->uc.uc_mcontext.trap_no, err);
339 __put_user_error(current->thread.error_code, &sf->uc.uc_mcontext.error_code, err);
340 __put_user_error(current->thread.address, &sf->uc.uc_mcontext.fault_address, err);
341 __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Russell Kingaca6ca12006-06-15 20:28:03 +0100343 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100345 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100346#ifdef CONFIG_CRUNCH
347 if (err == 0)
348 err |= preserve_crunch_context(&aux->crunch);
349#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350#ifdef CONFIG_IWMMXT
351 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
352 err |= preserve_iwmmxt_context(&aux->iwmmxt);
353#endif
354#ifdef CONFIG_VFP
Imre Deak82c6f5a2010-04-11 15:58:27 +0100355 if (err == 0)
356 err |= preserve_vfp_context(&aux->vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357#endif
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100358 __put_user_error(0, &aux->end_magic, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 return err;
361}
362
363static inline void __user *
364get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
365{
366 unsigned long sp = regs->ARM_sp;
367 void __user *frame;
368
369 /*
370 * This is the X/Open sanctioned signal stack switching.
371 */
372 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
373 sp = current->sas_ss_sp + current->sas_ss_size;
374
375 /*
376 * ATPCS B01 mandates 8-byte alignment
377 */
378 frame = (void __user *)((sp - framesize) & ~7);
379
380 /*
381 * Check that we can actually write to the signal frame.
382 */
383 if (!access_ok(VERIFY_WRITE, frame, framesize))
384 frame = NULL;
385
386 return frame;
387}
388
389static int
390setup_return(struct pt_regs *regs, struct k_sigaction *ka,
391 unsigned long __user *rc, void __user *frame, int usig)
392{
393 unsigned long handler = (unsigned long)ka->sa.sa_handler;
394 unsigned long retcode;
395 int thumb = 0;
Russell King53399052011-02-20 12:22:52 +0000396 unsigned long cpsr = regs->ARM_cpsr & ~(PSR_f | PSR_E_BIT);
397
398 cpsr |= PSR_ENDSTATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 /*
401 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
402 */
403 if (ka->sa.sa_flags & SA_THIRTYTWO)
404 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
405
406#ifdef CONFIG_ARM_THUMB
407 if (elf_hwcap & HWCAP_THUMB) {
408 /*
409 * The LSB of the handler determines if we're going to
410 * be using THUMB or ARM mode for this signal handler.
411 */
412 thumb = handler & 1;
413
Catalin Marinasd71e1352009-05-30 14:00:15 +0100414 if (thumb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 cpsr |= PSR_T_BIT;
Catalin Marinasd71e1352009-05-30 14:00:15 +0100416#if __LINUX_ARM_ARCH__ >= 7
417 /* clear the If-Then Thumb-2 execution state */
418 cpsr &= ~PSR_IT_MASK;
419#endif
420 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 cpsr &= ~PSR_T_BIT;
422 }
423#endif
424
425 if (ka->sa.sa_flags & SA_RESTORER) {
426 retcode = (unsigned long)ka->sa.sa_restorer;
427 } else {
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000428 unsigned int idx = thumb << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 if (ka->sa.sa_flags & SA_SIGINFO)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000431 idx += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000433 if (__put_user(sigreturn_codes[idx], rc) ||
434 __put_user(sigreturn_codes[idx+1], rc+1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return 1;
436
Russell Kinge00d3492005-06-22 20:26:05 +0100437 if (cpsr & MODE32_BIT) {
438 /*
439 * 32-bit code can use the new high-page
440 * signal return code support.
441 */
442 retcode = KERN_SIGRETURN_CODE + (idx << 2) + thumb;
443 } else {
444 /*
445 * Ensure that the instruction cache sees
446 * the return code written onto the stack.
447 */
448 flush_icache_range((unsigned long)rc,
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000449 (unsigned long)(rc + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Russell Kinge00d3492005-06-22 20:26:05 +0100451 retcode = ((unsigned long)rc) + thumb;
452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454
455 regs->ARM_r0 = usig;
456 regs->ARM_sp = (unsigned long)frame;
457 regs->ARM_lr = retcode;
458 regs->ARM_pc = handler;
459 regs->ARM_cpsr = cpsr;
460
461 return 0;
462}
463
464static int
465setup_frame(int usig, struct k_sigaction *ka, sigset_t *set, struct pt_regs *regs)
466{
467 struct sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
468 int err = 0;
469
470 if (!frame)
471 return 1;
472
Russell Kingca195cf2006-06-24 22:41:09 +0100473 /*
474 * Set uc.uc_flags to a value which sc.trap_no would never have.
475 */
476 __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Russell Kingaca6ca12006-06-15 20:28:03 +0100478 err |= setup_sigframe(frame, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (err == 0)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000480 err = setup_return(regs, ka, frame->retcode, frame, usig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 return err;
483}
484
485static int
486setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
487 sigset_t *set, struct pt_regs *regs)
488{
489 struct rt_sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
490 stack_t stack;
491 int err = 0;
492
493 if (!frame)
494 return 1;
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 err |= copy_siginfo_to_user(&frame->info, info);
497
Russell Kingcb3504e2006-06-15 20:18:25 +0100498 __put_user_error(0, &frame->sig.uc.uc_flags, err);
499 __put_user_error(NULL, &frame->sig.uc.uc_link, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 memset(&stack, 0, sizeof(stack));
502 stack.ss_sp = (void __user *)current->sas_ss_sp;
503 stack.ss_flags = sas_ss_flags(regs->ARM_sp);
504 stack.ss_size = current->sas_ss_size;
Russell Kingcb3504e2006-06-15 20:18:25 +0100505 err |= __copy_to_user(&frame->sig.uc.uc_stack, &stack, sizeof(stack));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Russell Kingaca6ca12006-06-15 20:28:03 +0100507 err |= setup_sigframe(&frame->sig, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (err == 0)
Russell Kingcb3504e2006-06-15 20:18:25 +0100509 err = setup_return(regs, ka, frame->sig.retcode, frame, usig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 if (err == 0) {
512 /*
513 * For realtime signals we must also set the second and third
514 * arguments for the signal handler.
515 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
516 */
517 regs->ARM_r1 = (unsigned long)&frame->info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100518 regs->ARM_r2 = (unsigned long)&frame->sig.uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520
521 return err;
522}
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524/*
525 * OK, we're invoking a handler
526 */
Al Viroa610d6e2012-05-21 23:42:15 -0400527static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528handle_signal(unsigned long sig, struct k_sigaction *ka,
Al Virob7f9a112012-05-02 09:59:21 -0400529 siginfo_t *info, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
531 struct thread_info *thread = current_thread_info();
532 struct task_struct *tsk = current;
Al Virob7f9a112012-05-02 09:59:21 -0400533 sigset_t *oldset = sigmask_to_save();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 int usig = sig;
535 int ret;
536
537 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 * translate the signal
539 */
540 if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
541 usig = thread->exec_domain->signal_invmap[usig];
542
543 /*
544 * Set up the stack frame
545 */
546 if (ka->sa.sa_flags & SA_SIGINFO)
547 ret = setup_rt_frame(usig, ka, info, oldset, regs);
548 else
549 ret = setup_frame(usig, ka, oldset, regs);
550
551 /*
552 * Check that the resulting registers are actually sane.
553 */
554 ret |= !valid_user_regs(regs);
555
Steven Rostedt69be8f12005-08-29 11:44:09 -0400556 if (ret != 0) {
Daniel Jacobowitza6c61e92005-11-19 10:01:07 +0000557 force_sigsegv(sig, tsk);
Al Viroa610d6e2012-05-21 23:42:15 -0400558 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
560
Daniel Jacobowitza6c61e92005-11-19 10:01:07 +0000561 /*
562 * Block the signal if we were successful.
563 */
Matt Fleming101d9b02012-03-05 15:05:34 -0800564 block_sigmask(ka, sig);
Wade Farnsworth0693bf62012-04-04 16:19:47 +0100565 tracehook_signal_handler(sig, info, ka, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
568/*
569 * Note that 'init' is a special process: it doesn't get signals it doesn't
570 * want to handle. Thus you cannot kill init even with a SIGKILL even by
571 * mistake.
572 *
573 * Note that we go through the signals twice: once to check the signals that
574 * the kernel can handle, and then we build all the user-level signal handling
575 * stack-frames in one go after that.
576 */
Mikael Pettersson36984262009-08-15 12:58:11 +0100577static void do_signal(struct pt_regs *regs, int syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100579 unsigned int retval = 0, continue_addr = 0, restart_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 struct k_sigaction ka;
581 siginfo_t info;
582 int signr;
583
584 /*
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100585 * If we were from a system call, check for system call restarting...
586 */
587 if (syscall) {
588 continue_addr = regs->ARM_pc;
589 restart_addr = continue_addr - (thumb_mode(regs) ? 2 : 4);
590 retval = regs->ARM_r0;
591
592 /*
593 * Prepare for system call restart. We do this here so that a
594 * debugger will see the already changed PSW.
595 */
596 switch (retval) {
597 case -ERESTARTNOHAND:
598 case -ERESTARTSYS:
599 case -ERESTARTNOINTR:
Al Viro6b5c8042012-05-02 21:02:03 -0400600 case -ERESTART_RESTARTBLOCK:
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100601 regs->ARM_r0 = regs->ARM_ORIG_r0;
602 regs->ARM_pc = restart_addr;
603 break;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100604 }
605 }
606
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100607 /*
608 * Get the signal to deliver. When running under ptrace, at this
609 * point the debugger may change all our registers ...
610 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
612 if (signr > 0) {
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100613 /*
614 * Depending on the signal settings we may need to revert the
615 * decision to restart the system call. But skip this if a
616 * debugger has chosen to restart at a different PC.
617 */
618 if (regs->ARM_pc == restart_addr) {
Al Viro6b5c8042012-05-02 21:02:03 -0400619 if (retval == -ERESTARTNOHAND ||
620 retval == -ERESTART_RESTARTBLOCK
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100621 || (retval == -ERESTARTSYS
622 && !(ka.sa.sa_flags & SA_RESTART))) {
623 regs->ARM_r0 = -EINTR;
624 regs->ARM_pc = continue_addr;
625 }
Al Viro6b5c8042012-05-02 21:02:03 -0400626 clear_thread_flag(TIF_SYSCALL_RESTARTSYS);
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100627 }
628
Al Viroa610d6e2012-05-21 23:42:15 -0400629 handle_signal(signr, &ka, &info, regs);
Mikael Pettersson36984262009-08-15 12:58:11 +0100630 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (syscall) {
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100634 /*
635 * Handle restarting a different system call. As above,
636 * if a debugger has chosen to restart at a different PC,
637 * ignore the restart.
638 */
639 if (retval == -ERESTART_RESTARTBLOCK
Al Viro6b5c8042012-05-02 21:02:03 -0400640 && regs->ARM_pc == restart_addr)
641 set_thread_flag(TIF_SYSCALL_RESTARTSYS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
Al Viro7dfae722012-04-26 18:15:11 -0400643
Al Viro51a7b442012-05-21 23:33:55 -0400644 restore_saved_sigmask();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
647asmlinkage void
648do_notify_resume(struct pt_regs *regs, unsigned int thread_flags, int syscall)
649{
650 if (thread_flags & _TIF_SIGPENDING)
Mikael Pettersson36984262009-08-15 12:58:11 +0100651 do_signal(regs, syscall);
David Howellsd0420c82009-09-02 09:14:16 +0100652
653 if (thread_flags & _TIF_NOTIFY_RESUME) {
654 clear_thread_flag(TIF_NOTIFY_RESUME);
655 tracehook_notify_resume(regs);
656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}