blob: 1aa5ecdd1b3da1824967e28031f15a419dec57e4 [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 King48be69a2013-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>
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>
Russell King48be69a2013-07-24 00:29:18 +010019#include <asm/traps.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/ucontext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/unistd.h>
Imre Deak82c6f5a2010-04-11 15:58:27 +010022#include <asm/vfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024/*
25 * For ARM syscalls, we encode the syscall number into the instruction.
26 */
janboeee4cd582008-03-19 03:34:23 +010027#define SWI_SYS_SIGRETURN (0xef000000|(__NR_sigreturn)|(__NR_OABI_SYSCALL_BASE))
28#define SWI_SYS_RT_SIGRETURN (0xef000000|(__NR_rt_sigreturn)|(__NR_OABI_SYSCALL_BASE))
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/*
Nicolas Pitrefcca5382006-01-18 22:38:47 +000031 * With EABI, the syscall number has to be loaded into r7.
32 */
33#define MOV_R7_NR_SIGRETURN (0xe3a07000 | (__NR_sigreturn - __NR_SYSCALL_BASE))
34#define MOV_R7_NR_RT_SIGRETURN (0xe3a07000 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
35
36/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * For Thumb syscalls, we pass the syscall number via r7. We therefore
38 * need two 16-bit instructions.
39 */
40#define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (__NR_sigreturn - __NR_SYSCALL_BASE))
41#define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
42
Russell King48be69a2013-07-24 00:29:18 +010043static const unsigned long sigreturn_codes[7] = {
Nicolas Pitrefcca5382006-01-18 22:38:47 +000044 MOV_R7_NR_SIGRETURN, SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
45 MOV_R7_NR_RT_SIGRETURN, SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN,
Linus Torvalds1da177e2005-04-16 15:20:36 -070046};
47
Russell King48be69a2013-07-24 00:29:18 +010048static unsigned long signal_return_offset;
49
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010050#ifdef CONFIG_CRUNCH
Hartley Sweeten65a50532009-08-04 23:20:45 +010051static int preserve_crunch_context(struct crunch_sigframe __user *frame)
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010052{
53 char kbuf[sizeof(*frame) + 8];
54 struct crunch_sigframe *kframe;
55
56 /* the crunch context must be 64 bit aligned */
57 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
58 kframe->magic = CRUNCH_MAGIC;
59 kframe->size = CRUNCH_STORAGE_SIZE;
60 crunch_task_copy(current_thread_info(), &kframe->storage);
61 return __copy_to_user(frame, kframe, sizeof(*frame));
62}
63
Hartley Sweeten65a50532009-08-04 23:20:45 +010064static int restore_crunch_context(struct crunch_sigframe __user *frame)
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010065{
66 char kbuf[sizeof(*frame) + 8];
67 struct crunch_sigframe *kframe;
68
69 /* the crunch context must be 64 bit aligned */
70 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
71 if (__copy_from_user(kframe, frame, sizeof(*frame)))
72 return -1;
73 if (kframe->magic != CRUNCH_MAGIC ||
74 kframe->size != CRUNCH_STORAGE_SIZE)
75 return -1;
76 crunch_task_restore(current_thread_info(), &kframe->storage);
77 return 0;
78}
79#endif
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#ifdef CONFIG_IWMMXT
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static int preserve_iwmmxt_context(struct iwmmxt_sigframe *frame)
84{
Hugh Dickins69b04752005-10-29 18:16:36 -070085 char kbuf[sizeof(*frame) + 8];
86 struct iwmmxt_sigframe *kframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 /* the iWMMXt context must be 64 bit aligned */
Hugh Dickins69b04752005-10-29 18:16:36 -070089 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +010090 kframe->magic = IWMMXT_MAGIC;
91 kframe->size = IWMMXT_STORAGE_SIZE;
Hugh Dickins69b04752005-10-29 18:16:36 -070092 iwmmxt_task_copy(current_thread_info(), &kframe->storage);
93 return __copy_to_user(frame, kframe, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
96static int restore_iwmmxt_context(struct iwmmxt_sigframe *frame)
97{
Hugh Dickins69b04752005-10-29 18:16:36 -070098 char kbuf[sizeof(*frame) + 8];
99 struct iwmmxt_sigframe *kframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Hugh Dickins69b04752005-10-29 18:16:36 -0700101 /* the iWMMXt context must be 64 bit aligned */
102 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
103 if (__copy_from_user(kframe, frame, sizeof(*frame)))
104 return -1;
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100105 if (kframe->magic != IWMMXT_MAGIC ||
106 kframe->size != IWMMXT_STORAGE_SIZE)
Hugh Dickins69b04752005-10-29 18:16:36 -0700107 return -1;
108 iwmmxt_task_restore(current_thread_info(), &kframe->storage);
109 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
112#endif
113
Imre Deak82c6f5a2010-04-11 15:58:27 +0100114#ifdef CONFIG_VFP
115
116static int preserve_vfp_context(struct vfp_sigframe __user *frame)
117{
Imre Deak82c6f5a2010-04-11 15:58:27 +0100118 const unsigned long magic = VFP_MAGIC;
119 const unsigned long size = VFP_STORAGE_SIZE;
120 int err = 0;
121
Imre Deak82c6f5a2010-04-11 15:58:27 +0100122 __put_user_error(magic, &frame->magic, err);
123 __put_user_error(size, &frame->size, err);
124
Will Deacon2498814f2012-04-23 15:38:28 +0100125 if (err)
126 return -EFAULT;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100127
Will Deacon2498814f2012-04-23 15:38:28 +0100128 return vfp_preserve_user_clear_hwstate(&frame->ufp, &frame->ufp_exc);
Imre Deak82c6f5a2010-04-11 15:58:27 +0100129}
130
131static int restore_vfp_context(struct vfp_sigframe __user *frame)
132{
Imre Deak82c6f5a2010-04-11 15:58:27 +0100133 unsigned long magic;
134 unsigned long size;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100135 int err = 0;
136
137 __get_user_error(magic, &frame->magic, err);
138 __get_user_error(size, &frame->size, err);
139
140 if (err)
141 return -EFAULT;
142 if (magic != VFP_MAGIC || size != VFP_STORAGE_SIZE)
143 return -EINVAL;
144
Will Deacon2498814f2012-04-23 15:38:28 +0100145 return vfp_restore_user_hwstate(&frame->ufp, &frame->ufp_exc);
Imre Deak82c6f5a2010-04-11 15:58:27 +0100146}
147
148#endif
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 * Do a signal return; undo the signal stack. These are aligned to 64-bit.
152 */
153struct sigframe {
Russell King7d4fdc12006-06-15 20:13:25 +0100154 struct ucontext uc;
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000155 unsigned long retcode[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156};
157
158struct rt_sigframe {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 struct siginfo info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100160 struct sigframe sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161};
162
Russell King68071482006-06-15 20:23:02 +0100163static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100165 struct aux_sigframe __user *aux;
Russell King68071482006-06-15 20:23:02 +0100166 sigset_t set;
167 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Russell King68071482006-06-15 20:23:02 +0100169 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
Al Viro77097ae2012-04-27 13:58:59 -0400170 if (err == 0)
Matt Fleming101d9b02012-03-05 15:05:34 -0800171 set_current_blocked(&set);
Russell King68071482006-06-15 20:23:02 +0100172
173 __get_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
174 __get_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
175 __get_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
176 __get_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
177 __get_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
178 __get_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
179 __get_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
180 __get_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
181 __get_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
182 __get_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
183 __get_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
184 __get_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
185 __get_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
186 __get_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
187 __get_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
188 __get_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
189 __get_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 err |= !valid_user_regs(regs);
192
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100193 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100194#ifdef CONFIG_CRUNCH
195 if (err == 0)
196 err |= restore_crunch_context(&aux->crunch);
197#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198#ifdef CONFIG_IWMMXT
199 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
200 err |= restore_iwmmxt_context(&aux->iwmmxt);
201#endif
202#ifdef CONFIG_VFP
Imre Deak82c6f5a2010-04-11 15:58:27 +0100203 if (err == 0)
204 err |= restore_vfp_context(&aux->vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205#endif
206
207 return err;
208}
209
210asmlinkage int sys_sigreturn(struct pt_regs *regs)
211{
212 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 /* Always make any pending restarted system calls return -EINTR */
215 current_thread_info()->restart_block.fn = do_no_restart_syscall;
216
217 /*
218 * Since we stacked the signal on a 64-bit boundary,
219 * then 'sp' should be word aligned here. If it's
220 * not, then the user is trying to mess with us.
221 */
222 if (regs->ARM_sp & 7)
223 goto badframe;
224
225 frame = (struct sigframe __user *)regs->ARM_sp;
226
227 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
228 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Russell King68071482006-06-15 20:23:02 +0100230 if (restore_sigframe(regs, frame))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 goto badframe;
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return regs->ARM_r0;
234
235badframe:
236 force_sig(SIGSEGV, current);
237 return 0;
238}
239
240asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
241{
242 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 /* Always make any pending restarted system calls return -EINTR */
245 current_thread_info()->restart_block.fn = do_no_restart_syscall;
246
247 /*
248 * Since we stacked the signal on a 64-bit boundary,
249 * then 'sp' should be word aligned here. If it's
250 * not, then the user is trying to mess with us.
251 */
252 if (regs->ARM_sp & 7)
253 goto badframe;
254
255 frame = (struct rt_sigframe __user *)regs->ARM_sp;
256
257 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
258 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Russell King68071482006-06-15 20:23:02 +0100260 if (restore_sigframe(regs, &frame->sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 goto badframe;
262
Al Viroec93ac82012-12-23 01:52:54 -0500263 if (restore_altstack(&frame->sig.uc.uc_stack))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 goto badframe;
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return regs->ARM_r0;
267
268badframe:
269 force_sig(SIGSEGV, current);
270 return 0;
271}
272
273static int
Russell Kingaca6ca12006-06-15 20:28:03 +0100274setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs, sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100276 struct aux_sigframe __user *aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 int err = 0;
278
Russell Kingaca6ca12006-06-15 20:28:03 +0100279 __put_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
280 __put_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
281 __put_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
282 __put_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
283 __put_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
284 __put_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
285 __put_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
286 __put_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
287 __put_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
288 __put_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
289 __put_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
290 __put_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
291 __put_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
292 __put_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
293 __put_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
294 __put_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
295 __put_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Russell Kingaca6ca12006-06-15 20:28:03 +0100297 __put_user_error(current->thread.trap_no, &sf->uc.uc_mcontext.trap_no, err);
298 __put_user_error(current->thread.error_code, &sf->uc.uc_mcontext.error_code, err);
299 __put_user_error(current->thread.address, &sf->uc.uc_mcontext.fault_address, err);
300 __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Russell Kingaca6ca12006-06-15 20:28:03 +0100302 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100304 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100305#ifdef CONFIG_CRUNCH
306 if (err == 0)
307 err |= preserve_crunch_context(&aux->crunch);
308#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309#ifdef CONFIG_IWMMXT
310 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
311 err |= preserve_iwmmxt_context(&aux->iwmmxt);
312#endif
313#ifdef CONFIG_VFP
Imre Deak82c6f5a2010-04-11 15:58:27 +0100314 if (err == 0)
315 err |= preserve_vfp_context(&aux->vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316#endif
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100317 __put_user_error(0, &aux->end_magic, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 return err;
320}
321
322static inline void __user *
Al Viro7e243642012-11-07 17:53:13 -0500323get_sigframe(struct ksignal *ksig, struct pt_regs *regs, int framesize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Al Viro7e243642012-11-07 17:53:13 -0500325 unsigned long sp = sigsp(regs->ARM_sp, ksig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 void __user *frame;
327
328 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 * ATPCS B01 mandates 8-byte alignment
330 */
331 frame = (void __user *)((sp - framesize) & ~7);
332
333 /*
334 * Check that we can actually write to the signal frame.
335 */
336 if (!access_ok(VERIFY_WRITE, frame, framesize))
337 frame = NULL;
338
339 return frame;
340}
341
Al Viro7e243642012-11-07 17:53:13 -0500342/*
343 * translate the signal
344 */
345static inline int map_sig(int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Al Viro7e243642012-11-07 17:53:13 -0500347 struct thread_info *thread = current_thread_info();
348 if (sig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
349 sig = thread->exec_domain->signal_invmap[sig];
350 return sig;
351}
352
353static int
354setup_return(struct pt_regs *regs, struct ksignal *ksig,
355 unsigned long __user *rc, void __user *frame)
356{
357 unsigned long handler = (unsigned long)ksig->ka.sa.sa_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 unsigned long retcode;
359 int thumb = 0;
Russell King53399052011-02-20 12:22:52 +0000360 unsigned long cpsr = regs->ARM_cpsr & ~(PSR_f | PSR_E_BIT);
361
362 cpsr |= PSR_ENDSTATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 /*
365 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
366 */
Al Viro7e243642012-11-07 17:53:13 -0500367 if (ksig->ka.sa.sa_flags & SA_THIRTYTWO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
369
370#ifdef CONFIG_ARM_THUMB
371 if (elf_hwcap & HWCAP_THUMB) {
372 /*
373 * The LSB of the handler determines if we're going to
374 * be using THUMB or ARM mode for this signal handler.
375 */
376 thumb = handler & 1;
377
T.J. Purtell6ecf8302013-11-06 18:38:05 +0100378#if __LINUX_ARM_ARCH__ >= 7
379 /*
380 * Clear the If-Then Thumb-2 execution state
381 * ARM spec requires this to be all 000s in ARM mode
382 * Snapdragon S4/Krait misbehaves on a Thumb=>ARM
383 * signal transition without this.
384 */
385 cpsr &= ~PSR_IT_MASK;
386#endif
387
Catalin Marinasd71e1352009-05-30 14:00:15 +0100388 if (thumb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 cpsr |= PSR_T_BIT;
Catalin Marinasd71e1352009-05-30 14:00:15 +0100390 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 cpsr &= ~PSR_T_BIT;
392 }
393#endif
394
Al Viro7e243642012-11-07 17:53:13 -0500395 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
396 retcode = (unsigned long)ksig->ka.sa.sa_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 } else {
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000398 unsigned int idx = thumb << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Al Viro7e243642012-11-07 17:53:13 -0500400 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000401 idx += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Jonathan Austin9dfc28b2013-04-18 18:37:24 +0100403 /*
404 * Put the sigreturn code on the stack no matter which return
405 * mechanism we use in order to remain ABI compliant
406 */
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000407 if (__put_user(sigreturn_codes[idx], rc) ||
408 __put_user(sigreturn_codes[idx+1], rc+1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return 1;
410
Russell King8c0cc8a2013-08-03 10:39:51 +0100411#ifdef CONFIG_MMU
412 if (cpsr & MODE32_BIT) {
Russell King48be69a2013-07-24 00:29:18 +0100413 struct mm_struct *mm = current->mm;
414
Russell Kinge00d3492005-06-22 20:26:05 +0100415 /*
Russell King48be69a2013-07-24 00:29:18 +0100416 * 32-bit code can use the signal return page
417 * except when the MPU has protected the vectors
418 * page from PL0
Russell Kinge00d3492005-06-22 20:26:05 +0100419 */
Russell King48be69a2013-07-24 00:29:18 +0100420 retcode = mm->context.sigpage + signal_return_offset +
421 (idx << 2) + thumb;
Russell King8c0cc8a2013-08-03 10:39:51 +0100422 } else
423#endif
424 {
Russell Kinge00d3492005-06-22 20:26:05 +0100425 /*
426 * Ensure that the instruction cache sees
427 * the return code written onto the stack.
428 */
429 flush_icache_range((unsigned long)rc,
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000430 (unsigned long)(rc + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Russell Kinge00d3492005-06-22 20:26:05 +0100432 retcode = ((unsigned long)rc) + thumb;
433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435
Al Viro7e243642012-11-07 17:53:13 -0500436 regs->ARM_r0 = map_sig(ksig->sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 regs->ARM_sp = (unsigned long)frame;
438 regs->ARM_lr = retcode;
439 regs->ARM_pc = handler;
440 regs->ARM_cpsr = cpsr;
441
442 return 0;
443}
444
445static int
Al Viro7e243642012-11-07 17:53:13 -0500446setup_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Al Viro7e243642012-11-07 17:53:13 -0500448 struct sigframe __user *frame = get_sigframe(ksig, regs, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 int err = 0;
450
451 if (!frame)
452 return 1;
453
Russell Kingca195cf2006-06-24 22:41:09 +0100454 /*
455 * Set uc.uc_flags to a value which sc.trap_no would never have.
456 */
457 __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Russell Kingaca6ca12006-06-15 20:28:03 +0100459 err |= setup_sigframe(frame, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (err == 0)
Al Viro7e243642012-11-07 17:53:13 -0500461 err = setup_return(regs, ksig, frame->retcode, frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 return err;
464}
465
466static int
Al Viro7e243642012-11-07 17:53:13 -0500467setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Al Viro7e243642012-11-07 17:53:13 -0500469 struct rt_sigframe __user *frame = get_sigframe(ksig, regs, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 int err = 0;
471
472 if (!frame)
473 return 1;
474
Al Viro7e243642012-11-07 17:53:13 -0500475 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Russell Kingcb3504e2006-06-15 20:18:25 +0100477 __put_user_error(0, &frame->sig.uc.uc_flags, err);
478 __put_user_error(NULL, &frame->sig.uc.uc_link, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Al Viroec93ac82012-12-23 01:52:54 -0500480 err |= __save_altstack(&frame->sig.uc.uc_stack, regs->ARM_sp);
Russell Kingaca6ca12006-06-15 20:28:03 +0100481 err |= setup_sigframe(&frame->sig, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (err == 0)
Al Viro7e243642012-11-07 17:53:13 -0500483 err = setup_return(regs, ksig, frame->sig.retcode, frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 if (err == 0) {
486 /*
487 * For realtime signals we must also set the second and third
488 * arguments for the signal handler.
489 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
490 */
491 regs->ARM_r1 = (unsigned long)&frame->info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100492 regs->ARM_r2 = (unsigned long)&frame->sig.uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494
495 return err;
496}
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498/*
499 * OK, we're invoking a handler
500 */
Al Viro7e243642012-11-07 17:53:13 -0500501static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Al Virob7f9a112012-05-02 09:59:21 -0400503 sigset_t *oldset = sigmask_to_save();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 int ret;
505
506 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 * Set up the stack frame
508 */
Al Viro7e243642012-11-07 17:53:13 -0500509 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
510 ret = setup_rt_frame(ksig, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 else
Al Viro7e243642012-11-07 17:53:13 -0500512 ret = setup_frame(ksig, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 /*
515 * Check that the resulting registers are actually sane.
516 */
517 ret |= !valid_user_regs(regs);
518
Al Viro7e243642012-11-07 17:53:13 -0500519 signal_setup_done(ret, ksig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
522/*
523 * Note that 'init' is a special process: it doesn't get signals it doesn't
524 * want to handle. Thus you cannot kill init even with a SIGKILL even by
525 * mistake.
526 *
527 * Note that we go through the signals twice: once to check the signals that
528 * the kernel can handle, and then we build all the user-level signal handling
529 * stack-frames in one go after that.
530 */
Al Viro81783782012-07-19 17:48:21 +0100531static int do_signal(struct pt_regs *regs, int syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100533 unsigned int retval = 0, continue_addr = 0, restart_addr = 0;
Al Viro7e243642012-11-07 17:53:13 -0500534 struct ksignal ksig;
Al Viro81783782012-07-19 17:48:21 +0100535 int restart = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 /*
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100538 * If we were from a system call, check for system call restarting...
539 */
540 if (syscall) {
541 continue_addr = regs->ARM_pc;
542 restart_addr = continue_addr - (thumb_mode(regs) ? 2 : 4);
543 retval = regs->ARM_r0;
544
545 /*
546 * Prepare for system call restart. We do this here so that a
547 * debugger will see the already changed PSW.
548 */
549 switch (retval) {
Al Viro81783782012-07-19 17:48:21 +0100550 case -ERESTART_RESTARTBLOCK:
Al Viro66285212012-07-19 17:48:50 +0100551 restart -= 2;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100552 case -ERESTARTNOHAND:
553 case -ERESTARTSYS:
554 case -ERESTARTNOINTR:
Al Viro81783782012-07-19 17:48:21 +0100555 restart++;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100556 regs->ARM_r0 = regs->ARM_ORIG_r0;
557 regs->ARM_pc = restart_addr;
558 break;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100559 }
560 }
561
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100562 /*
563 * Get the signal to deliver. When running under ptrace, at this
564 * point the debugger may change all our registers ...
565 */
Al Viro81783782012-07-19 17:48:21 +0100566 /*
567 * Depending on the signal settings we may need to revert the
568 * decision to restart the system call. But skip this if a
569 * debugger has chosen to restart at a different PC.
570 */
Al Viro7e243642012-11-07 17:53:13 -0500571 if (get_signal(&ksig)) {
572 /* handler */
573 if (unlikely(restart) && regs->ARM_pc == restart_addr) {
Will Deaconad82cc02012-07-19 17:46:44 +0100574 if (retval == -ERESTARTNOHAND ||
575 retval == -ERESTART_RESTARTBLOCK
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100576 || (retval == -ERESTARTSYS
Al Viro7e243642012-11-07 17:53:13 -0500577 && !(ksig.ka.sa.sa_flags & SA_RESTART))) {
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100578 regs->ARM_r0 = -EINTR;
579 regs->ARM_pc = continue_addr;
580 }
581 }
Al Viro7e243642012-11-07 17:53:13 -0500582 handle_signal(&ksig, regs);
583 } else {
584 /* no handler */
585 restore_saved_sigmask();
586 if (unlikely(restart) && regs->ARM_pc == restart_addr) {
587 regs->ARM_pc = continue_addr;
588 return restart;
589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
Al Viro7e243642012-11-07 17:53:13 -0500591 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
Al Viro81783782012-07-19 17:48:21 +0100594asmlinkage int
Al Viro0a267fa2012-07-19 17:47:55 +0100595do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Al Viro0a267fa2012-07-19 17:47:55 +0100597 do {
598 if (likely(thread_flags & _TIF_NEED_RESCHED)) {
599 schedule();
600 } else {
601 if (unlikely(!user_mode(regs)))
Al Viro81783782012-07-19 17:48:21 +0100602 return 0;
Al Viro0a267fa2012-07-19 17:47:55 +0100603 local_irq_enable();
604 if (thread_flags & _TIF_SIGPENDING) {
Al Viro66285212012-07-19 17:48:50 +0100605 int restart = do_signal(regs, syscall);
606 if (unlikely(restart)) {
Al Viro81783782012-07-19 17:48:21 +0100607 /*
608 * Restart without handlers.
609 * Deal with it without leaving
610 * the kernel space.
611 */
Al Viro66285212012-07-19 17:48:50 +0100612 return restart;
Al Viro81783782012-07-19 17:48:21 +0100613 }
Al Viro0a267fa2012-07-19 17:47:55 +0100614 syscall = 0;
615 } else {
616 clear_thread_flag(TIF_NOTIFY_RESUME);
617 tracehook_notify_resume(regs);
618 }
619 }
620 local_irq_disable();
621 thread_flags = current_thread_info()->flags;
622 } while (thread_flags & _TIF_WORK_MASK);
Al Viro81783782012-07-19 17:48:21 +0100623 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
Russell King48be69a2013-07-24 00:29:18 +0100625
Russell King48be69a2013-07-24 00:29:18 +0100626struct page *get_signal_page(void)
627{
Russell Kinge0d40752013-08-03 10:30:05 +0100628 unsigned long ptr;
629 unsigned offset;
630 struct page *page;
631 void *addr;
Russell King48be69a2013-07-24 00:29:18 +0100632
Russell Kinge0d40752013-08-03 10:30:05 +0100633 page = alloc_pages(GFP_KERNEL, 0);
Russell King48be69a2013-07-24 00:29:18 +0100634
Russell Kinge0d40752013-08-03 10:30:05 +0100635 if (!page)
636 return NULL;
Russell King48be69a2013-07-24 00:29:18 +0100637
Russell Kinge0d40752013-08-03 10:30:05 +0100638 addr = page_address(page);
Russell King48be69a2013-07-24 00:29:18 +0100639
Russell Kinge0d40752013-08-03 10:30:05 +0100640 /* Give the signal return code some randomness */
641 offset = 0x200 + (get_random_int() & 0x7fc);
642 signal_return_offset = offset;
Russell King48be69a2013-07-24 00:29:18 +0100643
Russell Kinge0d40752013-08-03 10:30:05 +0100644 /*
645 * Copy signal return handlers into the vector page, and
646 * set sigreturn to be a pointer to these.
647 */
648 memcpy(addr + offset, sigreturn_codes, sizeof(sigreturn_codes));
Russell King48be69a2013-07-24 00:29:18 +0100649
Russell Kinge0d40752013-08-03 10:30:05 +0100650 ptr = (unsigned long)addr + offset;
651 flush_icache_range(ptr, ptr + sizeof(sigreturn_codes));
Russell King48be69a2013-07-24 00:29:18 +0100652
Russell Kinge0d40752013-08-03 10:30:05 +0100653 return page;
Russell King48be69a2013-07-24 00:29:18 +0100654}