blob: edb73b4aff7468ddec8f8b8709ebda1fa7e8df4d [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>
Russell King10d98aa2013-07-24 00:29:18 +010011#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/personality.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080014#include <linux/freezer.h>
Russell King33fa9b12008-09-06 11:35:55 +010015#include <linux/uaccess.h>
David Howells733e5e42009-09-09 08:30:21 +010016#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Russell Kingee90dab2006-11-09 14:20:47 +000018#include <asm/elf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/cacheflush.h>
Russell King10d98aa2013-07-24 00:29:18 +010020#include <asm/traps.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/ucontext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/unistd.h>
Imre Deak82c6f5a2010-04-11 15:58:27 +010023#include <asm/vfp.h>
Russell Kinge00d3492005-06-22 20:26:05 +010024#include "signal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
27
28/*
29 * For ARM syscalls, we encode the syscall number into the instruction.
30 */
janboeee4cd582008-03-19 03:34:23 +010031#define SWI_SYS_SIGRETURN (0xef000000|(__NR_sigreturn)|(__NR_OABI_SYSCALL_BASE))
32#define SWI_SYS_RT_SIGRETURN (0xef000000|(__NR_rt_sigreturn)|(__NR_OABI_SYSCALL_BASE))
Russell Kingab72b002009-10-25 15:39:37 +000033#define SWI_SYS_RESTART (0xef000000|__NR_restart_syscall|__NR_OABI_SYSCALL_BASE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35/*
Nicolas Pitrefcca5382006-01-18 22:38:47 +000036 * With EABI, the syscall number has to be loaded into r7.
37 */
38#define MOV_R7_NR_SIGRETURN (0xe3a07000 | (__NR_sigreturn - __NR_SYSCALL_BASE))
39#define MOV_R7_NR_RT_SIGRETURN (0xe3a07000 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
40
41/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * For Thumb syscalls, we pass the syscall number via r7. We therefore
43 * need two 16-bit instructions.
44 */
45#define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (__NR_sigreturn - __NR_SYSCALL_BASE))
46#define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
47
Russell King10d98aa2013-07-24 00:29:18 +010048static const unsigned long sigreturn_codes[7] = {
Nicolas Pitrefcca5382006-01-18 22:38:47 +000049 MOV_R7_NR_SIGRETURN, SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
50 MOV_R7_NR_RT_SIGRETURN, SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN,
Linus Torvalds1da177e2005-04-16 15:20:36 -070051};
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/*
Russell Kingab72b002009-10-25 15:39:37 +000054 * Either we support OABI only, or we have EABI with the OABI
55 * compat layer enabled. In the later case we don't know if
56 * user space is EABI or not, and if not we must not clobber r7.
57 * Always using the OABI syscall solves that issue and works for
58 * all those cases.
59 */
60const unsigned long syscall_restart_code[2] = {
61 SWI_SYS_RESTART, /* swi __NR_restart_syscall */
62 0xe49df004, /* ldr pc, [sp], #4 */
63};
64
65/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 * atomically swap in the new signal mask, and wait for a signal.
67 */
Mikael Pettersson36984262009-08-15 12:58:11 +010068asmlinkage int sys_sigsuspend(int restart, unsigned long oldmask, old_sigset_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Matt Fleming101d9b02012-03-05 15:05:34 -080070 sigset_t blocked;
71
Mikael Pettersson36984262009-08-15 12:58:11 +010072 current->saved_sigmask = current->blocked;
Matt Fleming101d9b02012-03-05 15:05:34 -080073
74 mask &= _BLOCKABLE;
75 siginitset(&blocked, mask);
76 set_current_blocked(&blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Mikael Pettersson36984262009-08-15 12:58:11 +010078 current->state = TASK_INTERRUPTIBLE;
79 schedule();
80 set_restore_sigmask();
81 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
84asmlinkage int
85sys_sigaction(int sig, const struct old_sigaction __user *act,
86 struct old_sigaction __user *oact)
87{
88 struct k_sigaction new_ka, old_ka;
89 int ret;
90
91 if (act) {
92 old_sigset_t mask;
93 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
94 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
95 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
96 return -EFAULT;
97 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
98 __get_user(mask, &act->sa_mask);
99 siginitset(&new_ka.sa.sa_mask, mask);
100 }
101
102 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
103
104 if (!ret && oact) {
105 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
106 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
107 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
108 return -EFAULT;
109 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
110 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
111 }
112
113 return ret;
114}
115
Russell King10d98aa2013-07-24 00:29:18 +0100116static unsigned long signal_return_offset;
117
Lennert Buytenhek3bec6de2006-06-27 22:56:18 +0100118#ifdef CONFIG_CRUNCH
Hartley Sweeten65a50532009-08-04 23:20:45 +0100119static int preserve_crunch_context(struct crunch_sigframe __user *frame)
Lennert Buytenhek3bec6de2006-06-27 22:56:18 +0100120{
121 char kbuf[sizeof(*frame) + 8];
122 struct crunch_sigframe *kframe;
123
124 /* the crunch context must be 64 bit aligned */
125 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
126 kframe->magic = CRUNCH_MAGIC;
127 kframe->size = CRUNCH_STORAGE_SIZE;
128 crunch_task_copy(current_thread_info(), &kframe->storage);
129 return __copy_to_user(frame, kframe, sizeof(*frame));
130}
131
Hartley Sweeten65a50532009-08-04 23:20:45 +0100132static int restore_crunch_context(struct crunch_sigframe __user *frame)
Lennert Buytenhek3bec6de2006-06-27 22:56:18 +0100133{
134 char kbuf[sizeof(*frame) + 8];
135 struct crunch_sigframe *kframe;
136
137 /* the crunch context must be 64 bit aligned */
138 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
139 if (__copy_from_user(kframe, frame, sizeof(*frame)))
140 return -1;
141 if (kframe->magic != CRUNCH_MAGIC ||
142 kframe->size != CRUNCH_STORAGE_SIZE)
143 return -1;
144 crunch_task_restore(current_thread_info(), &kframe->storage);
145 return 0;
146}
147#endif
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#ifdef CONFIG_IWMMXT
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static int preserve_iwmmxt_context(struct iwmmxt_sigframe *frame)
152{
Hugh Dickins69b04752005-10-29 18:16:36 -0700153 char kbuf[sizeof(*frame) + 8];
154 struct iwmmxt_sigframe *kframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 /* the iWMMXt context must be 64 bit aligned */
Hugh Dickins69b04752005-10-29 18:16:36 -0700157 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100158 kframe->magic = IWMMXT_MAGIC;
159 kframe->size = IWMMXT_STORAGE_SIZE;
Hugh Dickins69b04752005-10-29 18:16:36 -0700160 iwmmxt_task_copy(current_thread_info(), &kframe->storage);
161 return __copy_to_user(frame, kframe, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
164static int restore_iwmmxt_context(struct iwmmxt_sigframe *frame)
165{
Hugh Dickins69b04752005-10-29 18:16:36 -0700166 char kbuf[sizeof(*frame) + 8];
167 struct iwmmxt_sigframe *kframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Hugh Dickins69b04752005-10-29 18:16:36 -0700169 /* the iWMMXt context must be 64 bit aligned */
170 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
171 if (__copy_from_user(kframe, frame, sizeof(*frame)))
172 return -1;
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100173 if (kframe->magic != IWMMXT_MAGIC ||
174 kframe->size != IWMMXT_STORAGE_SIZE)
Hugh Dickins69b04752005-10-29 18:16:36 -0700175 return -1;
176 iwmmxt_task_restore(current_thread_info(), &kframe->storage);
177 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180#endif
181
Imre Deak82c6f5a2010-04-11 15:58:27 +0100182#ifdef CONFIG_VFP
183
184static int preserve_vfp_context(struct vfp_sigframe __user *frame)
185{
Imre Deak82c6f5a2010-04-11 15:58:27 +0100186 const unsigned long magic = VFP_MAGIC;
187 const unsigned long size = VFP_STORAGE_SIZE;
188 int err = 0;
189
Imre Deak82c6f5a2010-04-11 15:58:27 +0100190 __put_user_error(magic, &frame->magic, err);
191 __put_user_error(size, &frame->size, err);
192
Will Deacon24988142012-04-23 15:38:28 +0100193 if (err)
194 return -EFAULT;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100195
Will Deacon24988142012-04-23 15:38:28 +0100196 return vfp_preserve_user_clear_hwstate(&frame->ufp, &frame->ufp_exc);
Imre Deak82c6f5a2010-04-11 15:58:27 +0100197}
198
199static int restore_vfp_context(struct vfp_sigframe __user *frame)
200{
Imre Deak82c6f5a2010-04-11 15:58:27 +0100201 unsigned long magic;
202 unsigned long size;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100203 int err = 0;
204
205 __get_user_error(magic, &frame->magic, err);
206 __get_user_error(size, &frame->size, err);
207
208 if (err)
209 return -EFAULT;
210 if (magic != VFP_MAGIC || size != VFP_STORAGE_SIZE)
211 return -EINVAL;
212
Will Deacon24988142012-04-23 15:38:28 +0100213 return vfp_restore_user_hwstate(&frame->ufp, &frame->ufp_exc);
Imre Deak82c6f5a2010-04-11 15:58:27 +0100214}
215
216#endif
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * Do a signal return; undo the signal stack. These are aligned to 64-bit.
220 */
221struct sigframe {
Russell King7d4fdc12006-06-15 20:13:25 +0100222 struct ucontext uc;
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000223 unsigned long retcode[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224};
225
226struct rt_sigframe {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 struct siginfo info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100228 struct sigframe sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229};
230
Russell King68071482006-06-15 20:23:02 +0100231static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100233 struct aux_sigframe __user *aux;
Russell King68071482006-06-15 20:23:02 +0100234 sigset_t set;
235 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Russell King68071482006-06-15 20:23:02 +0100237 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
238 if (err == 0) {
239 sigdelsetmask(&set, ~_BLOCKABLE);
Matt Fleming101d9b02012-03-05 15:05:34 -0800240 set_current_blocked(&set);
Russell King68071482006-06-15 20:23:02 +0100241 }
242
243 __get_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
244 __get_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
245 __get_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
246 __get_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
247 __get_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
248 __get_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
249 __get_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
250 __get_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
251 __get_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
252 __get_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
253 __get_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
254 __get_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
255 __get_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
256 __get_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
257 __get_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
258 __get_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
259 __get_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 err |= !valid_user_regs(regs);
262
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100263 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6de2006-06-27 22:56:18 +0100264#ifdef CONFIG_CRUNCH
265 if (err == 0)
266 err |= restore_crunch_context(&aux->crunch);
267#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268#ifdef CONFIG_IWMMXT
269 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
270 err |= restore_iwmmxt_context(&aux->iwmmxt);
271#endif
272#ifdef CONFIG_VFP
Imre Deak82c6f5a2010-04-11 15:58:27 +0100273 if (err == 0)
274 err |= restore_vfp_context(&aux->vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275#endif
276
277 return err;
278}
279
280asmlinkage int sys_sigreturn(struct pt_regs *regs)
281{
282 struct 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 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))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 goto badframe;
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return regs->ARM_r0;
304
305badframe:
306 force_sig(SIGSEGV, current);
307 return 0;
308}
309
310asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
311{
312 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 /* Always make any pending restarted system calls return -EINTR */
315 current_thread_info()->restart_block.fn = do_no_restart_syscall;
316
317 /*
318 * Since we stacked the signal on a 64-bit boundary,
319 * then 'sp' should be word aligned here. If it's
320 * not, then the user is trying to mess with us.
321 */
322 if (regs->ARM_sp & 7)
323 goto badframe;
324
325 frame = (struct rt_sigframe __user *)regs->ARM_sp;
326
327 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
328 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Russell King68071482006-06-15 20:23:02 +0100330 if (restore_sigframe(regs, &frame->sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 goto badframe;
332
Russell Kingcb3504e2006-06-15 20:18:25 +0100333 if (do_sigaltstack(&frame->sig.uc.uc_stack, NULL, regs->ARM_sp) == -EFAULT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 goto badframe;
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return regs->ARM_r0;
337
338badframe:
339 force_sig(SIGSEGV, current);
340 return 0;
341}
342
343static int
Russell Kingaca6ca12006-06-15 20:28:03 +0100344setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs, sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100346 struct aux_sigframe __user *aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 int err = 0;
348
Russell Kingaca6ca12006-06-15 20:28:03 +0100349 __put_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
350 __put_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
351 __put_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
352 __put_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
353 __put_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
354 __put_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
355 __put_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
356 __put_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
357 __put_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
358 __put_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
359 __put_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
360 __put_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
361 __put_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
362 __put_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
363 __put_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
364 __put_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
365 __put_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Russell Kingaca6ca12006-06-15 20:28:03 +0100367 __put_user_error(current->thread.trap_no, &sf->uc.uc_mcontext.trap_no, err);
368 __put_user_error(current->thread.error_code, &sf->uc.uc_mcontext.error_code, err);
369 __put_user_error(current->thread.address, &sf->uc.uc_mcontext.fault_address, err);
370 __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Russell Kingaca6ca12006-06-15 20:28:03 +0100372 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100374 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6de2006-06-27 22:56:18 +0100375#ifdef CONFIG_CRUNCH
376 if (err == 0)
377 err |= preserve_crunch_context(&aux->crunch);
378#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379#ifdef CONFIG_IWMMXT
380 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
381 err |= preserve_iwmmxt_context(&aux->iwmmxt);
382#endif
383#ifdef CONFIG_VFP
Imre Deak82c6f5a2010-04-11 15:58:27 +0100384 if (err == 0)
385 err |= preserve_vfp_context(&aux->vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386#endif
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100387 __put_user_error(0, &aux->end_magic, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 return err;
390}
391
392static inline void __user *
393get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
394{
395 unsigned long sp = regs->ARM_sp;
396 void __user *frame;
397
398 /*
399 * This is the X/Open sanctioned signal stack switching.
400 */
401 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
402 sp = current->sas_ss_sp + current->sas_ss_size;
403
404 /*
405 * ATPCS B01 mandates 8-byte alignment
406 */
407 frame = (void __user *)((sp - framesize) & ~7);
408
409 /*
410 * Check that we can actually write to the signal frame.
411 */
412 if (!access_ok(VERIFY_WRITE, frame, framesize))
413 frame = NULL;
414
415 return frame;
416}
417
418static int
419setup_return(struct pt_regs *regs, struct k_sigaction *ka,
420 unsigned long __user *rc, void __user *frame, int usig)
421{
422 unsigned long handler = (unsigned long)ka->sa.sa_handler;
423 unsigned long retcode;
424 int thumb = 0;
Russell King53399052011-02-20 12:22:52 +0000425 unsigned long cpsr = regs->ARM_cpsr & ~(PSR_f | PSR_E_BIT);
426
427 cpsr |= PSR_ENDSTATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 /*
430 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
431 */
432 if (ka->sa.sa_flags & SA_THIRTYTWO)
433 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
434
435#ifdef CONFIG_ARM_THUMB
436 if (elf_hwcap & HWCAP_THUMB) {
437 /*
438 * The LSB of the handler determines if we're going to
439 * be using THUMB or ARM mode for this signal handler.
440 */
441 thumb = handler & 1;
442
T.J. Purtell3bd32602013-11-06 18:38:05 +0100443#if __LINUX_ARM_ARCH__ >= 7
444 /*
445 * Clear the If-Then Thumb-2 execution state
446 * ARM spec requires this to be all 000s in ARM mode
447 * Snapdragon S4/Krait misbehaves on a Thumb=>ARM
448 * signal transition without this.
449 */
450 cpsr &= ~PSR_IT_MASK;
451#endif
452
Catalin Marinasd71e1352009-05-30 14:00:15 +0100453 if (thumb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 cpsr |= PSR_T_BIT;
Catalin Marinasd71e1352009-05-30 14:00:15 +0100455 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 cpsr &= ~PSR_T_BIT;
457 }
458#endif
459
460 if (ka->sa.sa_flags & SA_RESTORER) {
461 retcode = (unsigned long)ka->sa.sa_restorer;
462 } else {
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000463 unsigned int idx = thumb << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 if (ka->sa.sa_flags & SA_SIGINFO)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000466 idx += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000468 if (__put_user(sigreturn_codes[idx], rc) ||
469 __put_user(sigreturn_codes[idx+1], rc+1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return 1;
471
Russell King0a4baba2013-08-03 10:39:51 +0100472#ifdef CONFIG_MMU
Russell Kinge00d3492005-06-22 20:26:05 +0100473 if (cpsr & MODE32_BIT) {
Russell King10d98aa2013-07-24 00:29:18 +0100474 struct mm_struct *mm = current->mm;
475
Russell Kinge00d3492005-06-22 20:26:05 +0100476 /*
Russell King10d98aa2013-07-24 00:29:18 +0100477 * 32-bit code can use the signal return page
478 * except when the MPU has protected the vectors
479 * page from PL0
Russell Kinge00d3492005-06-22 20:26:05 +0100480 */
Russell King10d98aa2013-07-24 00:29:18 +0100481 retcode = mm->context.sigpage + signal_return_offset +
482 (idx << 2) + thumb;
Russell King0a4baba2013-08-03 10:39:51 +0100483 } else
484#endif
485 {
Russell Kinge00d3492005-06-22 20:26:05 +0100486 /*
487 * Ensure that the instruction cache sees
488 * the return code written onto the stack.
489 */
490 flush_icache_range((unsigned long)rc,
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000491 (unsigned long)(rc + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Russell Kinge00d3492005-06-22 20:26:05 +0100493 retcode = ((unsigned long)rc) + thumb;
494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496
497 regs->ARM_r0 = usig;
498 regs->ARM_sp = (unsigned long)frame;
499 regs->ARM_lr = retcode;
500 regs->ARM_pc = handler;
501 regs->ARM_cpsr = cpsr;
502
503 return 0;
504}
505
506static int
507setup_frame(int usig, struct k_sigaction *ka, sigset_t *set, struct pt_regs *regs)
508{
509 struct sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
510 int err = 0;
511
512 if (!frame)
513 return 1;
514
Russell Kingca195cf2006-06-24 22:41:09 +0100515 /*
516 * Set uc.uc_flags to a value which sc.trap_no would never have.
517 */
518 __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Russell Kingaca6ca12006-06-15 20:28:03 +0100520 err |= setup_sigframe(frame, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (err == 0)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000522 err = setup_return(regs, ka, frame->retcode, frame, usig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 return err;
525}
526
527static int
528setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
529 sigset_t *set, struct pt_regs *regs)
530{
531 struct rt_sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
532 stack_t stack;
533 int err = 0;
534
535 if (!frame)
536 return 1;
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 err |= copy_siginfo_to_user(&frame->info, info);
539
Russell Kingcb3504e2006-06-15 20:18:25 +0100540 __put_user_error(0, &frame->sig.uc.uc_flags, err);
541 __put_user_error(NULL, &frame->sig.uc.uc_link, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 memset(&stack, 0, sizeof(stack));
544 stack.ss_sp = (void __user *)current->sas_ss_sp;
545 stack.ss_flags = sas_ss_flags(regs->ARM_sp);
546 stack.ss_size = current->sas_ss_size;
Russell Kingcb3504e2006-06-15 20:18:25 +0100547 err |= __copy_to_user(&frame->sig.uc.uc_stack, &stack, sizeof(stack));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Russell Kingaca6ca12006-06-15 20:28:03 +0100549 err |= setup_sigframe(&frame->sig, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (err == 0)
Russell Kingcb3504e2006-06-15 20:18:25 +0100551 err = setup_return(regs, ka, frame->sig.retcode, frame, usig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 if (err == 0) {
554 /*
555 * For realtime signals we must also set the second and third
556 * arguments for the signal handler.
557 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
558 */
559 regs->ARM_r1 = (unsigned long)&frame->info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100560 regs->ARM_r2 = (unsigned long)&frame->sig.uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562
563 return err;
564}
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566/*
567 * OK, we're invoking a handler
568 */
Mikael Pettersson36984262009-08-15 12:58:11 +0100569static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570handle_signal(unsigned long sig, struct k_sigaction *ka,
571 siginfo_t *info, sigset_t *oldset,
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100572 struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 struct thread_info *thread = current_thread_info();
575 struct task_struct *tsk = current;
576 int usig = sig;
577 int ret;
578
579 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 * translate the signal
581 */
582 if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
583 usig = thread->exec_domain->signal_invmap[usig];
584
585 /*
586 * Set up the stack frame
587 */
588 if (ka->sa.sa_flags & SA_SIGINFO)
589 ret = setup_rt_frame(usig, ka, info, oldset, regs);
590 else
591 ret = setup_frame(usig, ka, oldset, regs);
592
593 /*
594 * Check that the resulting registers are actually sane.
595 */
596 ret |= !valid_user_regs(regs);
597
Steven Rostedt69be8f12005-08-29 11:44:09 -0400598 if (ret != 0) {
Daniel Jacobowitza6c61e92005-11-19 10:01:07 +0000599 force_sigsegv(sig, tsk);
Mikael Pettersson36984262009-08-15 12:58:11 +0100600 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602
Daniel Jacobowitza6c61e92005-11-19 10:01:07 +0000603 /*
604 * Block the signal if we were successful.
605 */
Matt Fleming101d9b02012-03-05 15:05:34 -0800606 block_sigmask(ka, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Mikael Pettersson36984262009-08-15 12:58:11 +0100608 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
611/*
612 * Note that 'init' is a special process: it doesn't get signals it doesn't
613 * want to handle. Thus you cannot kill init even with a SIGKILL even by
614 * mistake.
615 *
616 * Note that we go through the signals twice: once to check the signals that
617 * the kernel can handle, and then we build all the user-level signal handling
618 * stack-frames in one go after that.
619 */
Mikael Pettersson36984262009-08-15 12:58:11 +0100620static void do_signal(struct pt_regs *regs, int syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100622 unsigned int retval = 0, continue_addr = 0, restart_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 struct k_sigaction ka;
624 siginfo_t info;
625 int signr;
626
627 /*
628 * We want the common case to go fast, which
629 * is why we may in certain cases get here from
630 * kernel mode. Just return without doing anything
631 * if so.
632 */
633 if (!user_mode(regs))
Mikael Pettersson36984262009-08-15 12:58:11 +0100634 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100636 /*
637 * If we were from a system call, check for system call restarting...
638 */
639 if (syscall) {
640 continue_addr = regs->ARM_pc;
641 restart_addr = continue_addr - (thumb_mode(regs) ? 2 : 4);
642 retval = regs->ARM_r0;
643
644 /*
645 * Prepare for system call restart. We do this here so that a
646 * debugger will see the already changed PSW.
647 */
648 switch (retval) {
649 case -ERESTARTNOHAND:
650 case -ERESTARTSYS:
651 case -ERESTARTNOINTR:
652 regs->ARM_r0 = regs->ARM_ORIG_r0;
653 regs->ARM_pc = restart_addr;
654 break;
655 case -ERESTART_RESTARTBLOCK:
656 regs->ARM_r0 = -EINTR;
657 break;
658 }
659 }
660
Andrew Mortonbdb94f32005-06-26 03:27:21 -0700661 if (try_to_freeze())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 goto no_signal;
663
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100664 /*
665 * Get the signal to deliver. When running under ptrace, at this
666 * point the debugger may change all our registers ...
667 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
669 if (signr > 0) {
Mikael Pettersson36984262009-08-15 12:58:11 +0100670 sigset_t *oldset;
671
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100672 /*
673 * Depending on the signal settings we may need to revert the
674 * decision to restart the system call. But skip this if a
675 * debugger has chosen to restart at a different PC.
676 */
677 if (regs->ARM_pc == restart_addr) {
678 if (retval == -ERESTARTNOHAND
679 || (retval == -ERESTARTSYS
680 && !(ka.sa.sa_flags & SA_RESTART))) {
681 regs->ARM_r0 = -EINTR;
682 regs->ARM_pc = continue_addr;
683 }
684 }
685
Mikael Pettersson36984262009-08-15 12:58:11 +0100686 if (test_thread_flag(TIF_RESTORE_SIGMASK))
687 oldset = &current->saved_sigmask;
688 else
689 oldset = &current->blocked;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100690 if (handle_signal(signr, &ka, &info, oldset, regs) == 0) {
Mikael Pettersson36984262009-08-15 12:58:11 +0100691 /*
692 * A signal was successfully delivered; the saved
693 * sigmask will have been stored in the signal frame,
694 * and will be restored by sigreturn, so we can simply
695 * clear the TIF_RESTORE_SIGMASK flag.
696 */
697 if (test_thread_flag(TIF_RESTORE_SIGMASK))
698 clear_thread_flag(TIF_RESTORE_SIGMASK);
699 }
Mikael Pettersson36984262009-08-15 12:58:11 +0100700 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702
703 no_signal:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (syscall) {
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100705 /*
706 * Handle restarting a different system call. As above,
707 * if a debugger has chosen to restart at a different PC,
708 * ignore the restart.
709 */
710 if (retval == -ERESTART_RESTARTBLOCK
711 && regs->ARM_pc == continue_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (thumb_mode(regs)) {
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100713 regs->ARM_r7 = __NR_restart_syscall - __NR_SYSCALL_BASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 regs->ARM_pc -= 2;
715 } else {
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100716#if defined(CONFIG_AEABI) && !defined(CONFIG_OABI_COMPAT)
717 regs->ARM_r7 = __NR_restart_syscall;
718 regs->ARM_pc -= 4;
719#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 u32 __user *usp;
721
Russell Kingab72b002009-10-25 15:39:37 +0000722 regs->ARM_sp -= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 usp = (u32 __user *)regs->ARM_sp;
724
Jean PIHET3336f4f2009-11-23 17:03:32 +0100725 if (put_user(regs->ARM_pc, usp) == 0) {
726 regs->ARM_pc = KERN_RESTART_CODE;
727 } else {
728 regs->ARM_sp += 4;
729 force_sigsegv(0, current);
730 }
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100731#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
733 }
Mikael Pettersson36984262009-08-15 12:58:11 +0100734
735 /* If there's no signal to deliver, we just put the saved sigmask
736 * back.
737 */
738 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
739 clear_thread_flag(TIF_RESTORE_SIGMASK);
740 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
745asmlinkage void
746do_notify_resume(struct pt_regs *regs, unsigned int thread_flags, int syscall)
747{
748 if (thread_flags & _TIF_SIGPENDING)
Mikael Pettersson36984262009-08-15 12:58:11 +0100749 do_signal(regs, syscall);
David Howellsd0420c82009-09-02 09:14:16 +0100750
751 if (thread_flags & _TIF_NOTIFY_RESUME) {
752 clear_thread_flag(TIF_NOTIFY_RESUME);
753 tracehook_notify_resume(regs);
David Howellsee18d642009-09-02 09:14:21 +0100754 if (current->replacement_session_keyring)
755 key_replace_session_keyring();
David Howellsd0420c82009-09-02 09:14:16 +0100756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
Russell King10d98aa2013-07-24 00:29:18 +0100758
Russell King10d98aa2013-07-24 00:29:18 +0100759struct page *get_signal_page(void)
760{
Russell King76c1e372013-08-03 10:30:05 +0100761 unsigned long ptr;
762 unsigned offset;
763 struct page *page;
764 void *addr;
Russell King10d98aa2013-07-24 00:29:18 +0100765
Russell King76c1e372013-08-03 10:30:05 +0100766 page = alloc_pages(GFP_KERNEL, 0);
Russell King10d98aa2013-07-24 00:29:18 +0100767
Russell King76c1e372013-08-03 10:30:05 +0100768 if (!page)
769 return NULL;
Russell King10d98aa2013-07-24 00:29:18 +0100770
Russell King76c1e372013-08-03 10:30:05 +0100771 addr = page_address(page);
Russell King10d98aa2013-07-24 00:29:18 +0100772
Russell King76c1e372013-08-03 10:30:05 +0100773 /* Give the signal return code some randomness */
774 offset = 0x200 + (get_random_int() & 0x7fc);
775 signal_return_offset = offset;
Russell King10d98aa2013-07-24 00:29:18 +0100776
Russell King76c1e372013-08-03 10:30:05 +0100777 /*
778 * Copy signal return handlers into the vector page, and
779 * set sigreturn to be a pointer to these.
780 */
781 memcpy(addr + offset, sigreturn_codes, sizeof(sigreturn_codes));
Russell King10d98aa2013-07-24 00:29:18 +0100782
Russell King76c1e372013-08-03 10:30:05 +0100783 ptr = (unsigned long)addr + offset;
784 flush_icache_range(ptr, ptr + sizeof(sigreturn_codes));
Russell King10d98aa2013-07-24 00:29:18 +0100785
Russell King76c1e372013-08-03 10:30:05 +0100786 return page;
Russell King10d98aa2013-07-24 00:29:18 +0100787}