blob: 1a65c31d1bfd2477f40f6a8950c9dda8e9a628b6 [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 Kinge00d3492005-06-22 20:26:05 +0100472 if (cpsr & MODE32_BIT) {
Russell King10d98aa2013-07-24 00:29:18 +0100473 struct mm_struct *mm = current->mm;
474
Russell Kinge00d3492005-06-22 20:26:05 +0100475 /*
Russell King10d98aa2013-07-24 00:29:18 +0100476 * 32-bit code can use the signal return page
477 * except when the MPU has protected the vectors
478 * page from PL0
Russell Kinge00d3492005-06-22 20:26:05 +0100479 */
Russell King10d98aa2013-07-24 00:29:18 +0100480 retcode = mm->context.sigpage + signal_return_offset +
481 (idx << 2) + thumb;
Russell Kinge00d3492005-06-22 20:26:05 +0100482 } else {
483 /*
484 * Ensure that the instruction cache sees
485 * the return code written onto the stack.
486 */
487 flush_icache_range((unsigned long)rc,
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000488 (unsigned long)(rc + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Russell Kinge00d3492005-06-22 20:26:05 +0100490 retcode = ((unsigned long)rc) + thumb;
491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493
494 regs->ARM_r0 = usig;
495 regs->ARM_sp = (unsigned long)frame;
496 regs->ARM_lr = retcode;
497 regs->ARM_pc = handler;
498 regs->ARM_cpsr = cpsr;
499
500 return 0;
501}
502
503static int
504setup_frame(int usig, struct k_sigaction *ka, sigset_t *set, struct pt_regs *regs)
505{
506 struct sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
507 int err = 0;
508
509 if (!frame)
510 return 1;
511
Russell Kingca195cf2006-06-24 22:41:09 +0100512 /*
513 * Set uc.uc_flags to a value which sc.trap_no would never have.
514 */
515 __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Russell Kingaca6ca12006-06-15 20:28:03 +0100517 err |= setup_sigframe(frame, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (err == 0)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000519 err = setup_return(regs, ka, frame->retcode, frame, usig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 return err;
522}
523
524static int
525setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
526 sigset_t *set, struct pt_regs *regs)
527{
528 struct rt_sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
529 stack_t stack;
530 int err = 0;
531
532 if (!frame)
533 return 1;
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 err |= copy_siginfo_to_user(&frame->info, info);
536
Russell Kingcb3504e2006-06-15 20:18:25 +0100537 __put_user_error(0, &frame->sig.uc.uc_flags, err);
538 __put_user_error(NULL, &frame->sig.uc.uc_link, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 memset(&stack, 0, sizeof(stack));
541 stack.ss_sp = (void __user *)current->sas_ss_sp;
542 stack.ss_flags = sas_ss_flags(regs->ARM_sp);
543 stack.ss_size = current->sas_ss_size;
Russell Kingcb3504e2006-06-15 20:18:25 +0100544 err |= __copy_to_user(&frame->sig.uc.uc_stack, &stack, sizeof(stack));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Russell Kingaca6ca12006-06-15 20:28:03 +0100546 err |= setup_sigframe(&frame->sig, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (err == 0)
Russell Kingcb3504e2006-06-15 20:18:25 +0100548 err = setup_return(regs, ka, frame->sig.retcode, frame, usig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 if (err == 0) {
551 /*
552 * For realtime signals we must also set the second and third
553 * arguments for the signal handler.
554 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
555 */
556 regs->ARM_r1 = (unsigned long)&frame->info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100557 regs->ARM_r2 = (unsigned long)&frame->sig.uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559
560 return err;
561}
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563/*
564 * OK, we're invoking a handler
565 */
Mikael Pettersson36984262009-08-15 12:58:11 +0100566static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567handle_signal(unsigned long sig, struct k_sigaction *ka,
568 siginfo_t *info, sigset_t *oldset,
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100569 struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
571 struct thread_info *thread = current_thread_info();
572 struct task_struct *tsk = current;
573 int usig = sig;
574 int ret;
575
576 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 * translate the signal
578 */
579 if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
580 usig = thread->exec_domain->signal_invmap[usig];
581
582 /*
583 * Set up the stack frame
584 */
585 if (ka->sa.sa_flags & SA_SIGINFO)
586 ret = setup_rt_frame(usig, ka, info, oldset, regs);
587 else
588 ret = setup_frame(usig, ka, oldset, regs);
589
590 /*
591 * Check that the resulting registers are actually sane.
592 */
593 ret |= !valid_user_regs(regs);
594
Steven Rostedt69be8f12005-08-29 11:44:09 -0400595 if (ret != 0) {
Daniel Jacobowitza6c61e92005-11-19 10:01:07 +0000596 force_sigsegv(sig, tsk);
Mikael Pettersson36984262009-08-15 12:58:11 +0100597 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
599
Daniel Jacobowitza6c61e92005-11-19 10:01:07 +0000600 /*
601 * Block the signal if we were successful.
602 */
Matt Fleming101d9b02012-03-05 15:05:34 -0800603 block_sigmask(ka, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Mikael Pettersson36984262009-08-15 12:58:11 +0100605 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
608/*
609 * Note that 'init' is a special process: it doesn't get signals it doesn't
610 * want to handle. Thus you cannot kill init even with a SIGKILL even by
611 * mistake.
612 *
613 * Note that we go through the signals twice: once to check the signals that
614 * the kernel can handle, and then we build all the user-level signal handling
615 * stack-frames in one go after that.
616 */
Mikael Pettersson36984262009-08-15 12:58:11 +0100617static void do_signal(struct pt_regs *regs, int syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100619 unsigned int retval = 0, continue_addr = 0, restart_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 struct k_sigaction ka;
621 siginfo_t info;
622 int signr;
623
624 /*
625 * We want the common case to go fast, which
626 * is why we may in certain cases get here from
627 * kernel mode. Just return without doing anything
628 * if so.
629 */
630 if (!user_mode(regs))
Mikael Pettersson36984262009-08-15 12:58:11 +0100631 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100633 /*
634 * If we were from a system call, check for system call restarting...
635 */
636 if (syscall) {
637 continue_addr = regs->ARM_pc;
638 restart_addr = continue_addr - (thumb_mode(regs) ? 2 : 4);
639 retval = regs->ARM_r0;
640
641 /*
642 * Prepare for system call restart. We do this here so that a
643 * debugger will see the already changed PSW.
644 */
645 switch (retval) {
646 case -ERESTARTNOHAND:
647 case -ERESTARTSYS:
648 case -ERESTARTNOINTR:
649 regs->ARM_r0 = regs->ARM_ORIG_r0;
650 regs->ARM_pc = restart_addr;
651 break;
652 case -ERESTART_RESTARTBLOCK:
653 regs->ARM_r0 = -EINTR;
654 break;
655 }
656 }
657
Andrew Mortonbdb94f32005-06-26 03:27:21 -0700658 if (try_to_freeze())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 goto no_signal;
660
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100661 /*
662 * Get the signal to deliver. When running under ptrace, at this
663 * point the debugger may change all our registers ...
664 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
666 if (signr > 0) {
Mikael Pettersson36984262009-08-15 12:58:11 +0100667 sigset_t *oldset;
668
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100669 /*
670 * Depending on the signal settings we may need to revert the
671 * decision to restart the system call. But skip this if a
672 * debugger has chosen to restart at a different PC.
673 */
674 if (regs->ARM_pc == restart_addr) {
675 if (retval == -ERESTARTNOHAND
676 || (retval == -ERESTARTSYS
677 && !(ka.sa.sa_flags & SA_RESTART))) {
678 regs->ARM_r0 = -EINTR;
679 regs->ARM_pc = continue_addr;
680 }
681 }
682
Mikael Pettersson36984262009-08-15 12:58:11 +0100683 if (test_thread_flag(TIF_RESTORE_SIGMASK))
684 oldset = &current->saved_sigmask;
685 else
686 oldset = &current->blocked;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100687 if (handle_signal(signr, &ka, &info, oldset, regs) == 0) {
Mikael Pettersson36984262009-08-15 12:58:11 +0100688 /*
689 * A signal was successfully delivered; the saved
690 * sigmask will have been stored in the signal frame,
691 * and will be restored by sigreturn, so we can simply
692 * clear the TIF_RESTORE_SIGMASK flag.
693 */
694 if (test_thread_flag(TIF_RESTORE_SIGMASK))
695 clear_thread_flag(TIF_RESTORE_SIGMASK);
696 }
Mikael Pettersson36984262009-08-15 12:58:11 +0100697 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699
700 no_signal:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (syscall) {
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100702 /*
703 * Handle restarting a different system call. As above,
704 * if a debugger has chosen to restart at a different PC,
705 * ignore the restart.
706 */
707 if (retval == -ERESTART_RESTARTBLOCK
708 && regs->ARM_pc == continue_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (thumb_mode(regs)) {
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100710 regs->ARM_r7 = __NR_restart_syscall - __NR_SYSCALL_BASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 regs->ARM_pc -= 2;
712 } else {
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100713#if defined(CONFIG_AEABI) && !defined(CONFIG_OABI_COMPAT)
714 regs->ARM_r7 = __NR_restart_syscall;
715 regs->ARM_pc -= 4;
716#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 u32 __user *usp;
718
Russell Kingab72b002009-10-25 15:39:37 +0000719 regs->ARM_sp -= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 usp = (u32 __user *)regs->ARM_sp;
721
Jean PIHET3336f4f2009-11-23 17:03:32 +0100722 if (put_user(regs->ARM_pc, usp) == 0) {
723 regs->ARM_pc = KERN_RESTART_CODE;
724 } else {
725 regs->ARM_sp += 4;
726 force_sigsegv(0, current);
727 }
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100728#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730 }
Mikael Pettersson36984262009-08-15 12:58:11 +0100731
732 /* If there's no signal to deliver, we just put the saved sigmask
733 * back.
734 */
735 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
736 clear_thread_flag(TIF_RESTORE_SIGMASK);
737 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
742asmlinkage void
743do_notify_resume(struct pt_regs *regs, unsigned int thread_flags, int syscall)
744{
745 if (thread_flags & _TIF_SIGPENDING)
Mikael Pettersson36984262009-08-15 12:58:11 +0100746 do_signal(regs, syscall);
David Howellsd0420c82009-09-02 09:14:16 +0100747
748 if (thread_flags & _TIF_NOTIFY_RESUME) {
749 clear_thread_flag(TIF_NOTIFY_RESUME);
750 tracehook_notify_resume(regs);
David Howellsee18d642009-09-02 09:14:21 +0100751 if (current->replacement_session_keyring)
752 key_replace_session_keyring();
David Howellsd0420c82009-09-02 09:14:16 +0100753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
Russell King10d98aa2013-07-24 00:29:18 +0100755
756static struct page *signal_page;
757
758struct page *get_signal_page(void)
759{
760 if (!signal_page) {
761 unsigned long ptr;
762 unsigned offset;
763 void *addr;
764
765 signal_page = alloc_pages(GFP_KERNEL, 0);
766
767 if (!signal_page)
768 return NULL;
769
770 addr = page_address(signal_page);
771
772 /* Give the signal return code some randomness */
773 offset = 0x200 + (get_random_int() & 0x7fc);
774 signal_return_offset = offset;
775
776 /*
777 * Copy signal return handlers into the vector page, and
778 * set sigreturn to be a pointer to these.
779 */
780 memcpy(addr + offset, sigreturn_codes, sizeof(sigreturn_codes));
781
782 ptr = (unsigned long)addr + offset;
783 flush_icache_range(ptr, ptr + sizeof(sigreturn_codes));
784 }
785
786 return signal_page;
787}