blob: 5814298ef0b701e61e4de018aa216009c9445d30 [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>
David A. Longc7edc9e2014-03-07 11:23:04 -050016#include <linux/uprobes.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 King48be69a2013-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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Victor Kamensky574e2b52013-08-27 22:41:57 -070025extern const unsigned long sigreturn_codes[7];
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Russell King48be69a2013-07-24 00:29:18 +010027static unsigned long signal_return_offset;
28
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010029#ifdef CONFIG_CRUNCH
Hartley Sweeten65a50532009-08-04 23:20:45 +010030static int preserve_crunch_context(struct crunch_sigframe __user *frame)
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010031{
32 char kbuf[sizeof(*frame) + 8];
33 struct crunch_sigframe *kframe;
34
35 /* the crunch context must be 64 bit aligned */
36 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
37 kframe->magic = CRUNCH_MAGIC;
38 kframe->size = CRUNCH_STORAGE_SIZE;
39 crunch_task_copy(current_thread_info(), &kframe->storage);
40 return __copy_to_user(frame, kframe, sizeof(*frame));
41}
42
Dave Martince184a02017-06-30 18:56:59 +010043static int restore_crunch_context(char __user **auxp)
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010044{
Dave Martince184a02017-06-30 18:56:59 +010045 struct crunch_sigframe __user *frame =
46 (struct crunch_sigframe __user *)*auxp;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010047 char kbuf[sizeof(*frame) + 8];
48 struct crunch_sigframe *kframe;
49
50 /* the crunch context must be 64 bit aligned */
51 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
52 if (__copy_from_user(kframe, frame, sizeof(*frame)))
53 return -1;
54 if (kframe->magic != CRUNCH_MAGIC ||
55 kframe->size != CRUNCH_STORAGE_SIZE)
56 return -1;
Dave Martince184a02017-06-30 18:56:59 +010057 *auxp += CRUNCH_STORAGE_SIZE;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010058 crunch_task_restore(current_thread_info(), &kframe->storage);
59 return 0;
60}
61#endif
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#ifdef CONFIG_IWMMXT
64
Dave Martin26958352017-06-30 18:56:09 +010065static int preserve_iwmmxt_context(struct iwmmxt_sigframe __user *frame)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Hugh Dickins69b04752005-10-29 18:16:36 -070067 char kbuf[sizeof(*frame) + 8];
68 struct iwmmxt_sigframe *kframe;
Dave Martince184a02017-06-30 18:56:59 +010069 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 /* the iWMMXt context must be 64 bit aligned */
Hugh Dickins69b04752005-10-29 18:16:36 -070072 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
Dave Martince184a02017-06-30 18:56:59 +010073
74 if (test_thread_flag(TIF_USING_IWMMXT)) {
75 kframe->magic = IWMMXT_MAGIC;
76 kframe->size = IWMMXT_STORAGE_SIZE;
77 iwmmxt_task_copy(current_thread_info(), &kframe->storage);
78
79 err = __copy_to_user(frame, kframe, sizeof(*frame));
80 } else {
81 /*
82 * For bug-compatibility with older kernels, some space
83 * has to be reserved for iWMMXt even if it's not used.
84 * Set the magic and size appropriately so that properly
85 * written userspace can skip it reliably:
86 */
87 __put_user_error(DUMMY_MAGIC, &frame->magic, err);
88 __put_user_error(IWMMXT_STORAGE_SIZE, &frame->size, err);
89 }
90
91 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Dave Martince184a02017-06-30 18:56:59 +010094static int restore_iwmmxt_context(char __user **auxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Dave Martince184a02017-06-30 18:56:59 +010096 struct iwmmxt_sigframe __user *frame =
97 (struct iwmmxt_sigframe __user *)*auxp;
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;
Dave Martince184a02017-06-30 18:56:59 +0100105
106 /*
107 * For non-iWMMXt threads: a single iwmmxt_sigframe-sized dummy
108 * block is discarded for compatibility with setup_sigframe() if
109 * present, but we don't mandate its presence. If some other
110 * magic is here, it's not for us:
111 */
112 if (!test_thread_flag(TIF_USING_IWMMXT) &&
113 kframe->magic != DUMMY_MAGIC)
114 return 0;
115
116 if (kframe->size != IWMMXT_STORAGE_SIZE)
Hugh Dickins69b04752005-10-29 18:16:36 -0700117 return -1;
Dave Martince184a02017-06-30 18:56:59 +0100118
119 if (test_thread_flag(TIF_USING_IWMMXT)) {
120 if (kframe->magic != IWMMXT_MAGIC)
121 return -1;
122
123 iwmmxt_task_restore(current_thread_info(), &kframe->storage);
124 }
125
126 *auxp += IWMMXT_STORAGE_SIZE;
Hugh Dickins69b04752005-10-29 18:16:36 -0700127 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130#endif
131
Imre Deak82c6f5a2010-04-11 15:58:27 +0100132#ifdef CONFIG_VFP
133
134static int preserve_vfp_context(struct vfp_sigframe __user *frame)
135{
Imre Deak82c6f5a2010-04-11 15:58:27 +0100136 const unsigned long magic = VFP_MAGIC;
137 const unsigned long size = VFP_STORAGE_SIZE;
138 int err = 0;
139
Imre Deak82c6f5a2010-04-11 15:58:27 +0100140 __put_user_error(magic, &frame->magic, err);
141 __put_user_error(size, &frame->size, err);
142
Will Deacon2498814f2012-04-23 15:38:28 +0100143 if (err)
144 return -EFAULT;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100145
Will Deacon2498814f2012-04-23 15:38:28 +0100146 return vfp_preserve_user_clear_hwstate(&frame->ufp, &frame->ufp_exc);
Imre Deak82c6f5a2010-04-11 15:58:27 +0100147}
148
Dave Martince184a02017-06-30 18:56:59 +0100149static int restore_vfp_context(char __user **auxp)
Imre Deak82c6f5a2010-04-11 15:58:27 +0100150{
Dave Martince184a02017-06-30 18:56:59 +0100151 struct vfp_sigframe __user *frame =
152 (struct vfp_sigframe __user *)*auxp;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100153 unsigned long magic;
154 unsigned long size;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100155 int err = 0;
156
157 __get_user_error(magic, &frame->magic, err);
158 __get_user_error(size, &frame->size, err);
159
160 if (err)
161 return -EFAULT;
162 if (magic != VFP_MAGIC || size != VFP_STORAGE_SIZE)
163 return -EINVAL;
164
Dave Martince184a02017-06-30 18:56:59 +0100165 *auxp += size;
Will Deacon2498814f2012-04-23 15:38:28 +0100166 return vfp_restore_user_hwstate(&frame->ufp, &frame->ufp_exc);
Imre Deak82c6f5a2010-04-11 15:58:27 +0100167}
168
169#endif
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 * Do a signal return; undo the signal stack. These are aligned to 64-bit.
173 */
174struct sigframe {
Russell King7d4fdc12006-06-15 20:13:25 +0100175 struct ucontext uc;
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000176 unsigned long retcode[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177};
178
179struct rt_sigframe {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 struct siginfo info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100181 struct sigframe sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182};
183
Russell King68071482006-06-15 20:23:02 +0100184static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Dave Martince184a02017-06-30 18:56:59 +0100186 char __user *aux;
Russell King68071482006-06-15 20:23:02 +0100187 sigset_t set;
188 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Russell King68071482006-06-15 20:23:02 +0100190 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
Al Viro77097ae2012-04-27 13:58:59 -0400191 if (err == 0)
Matt Fleming101d9b02012-03-05 15:05:34 -0800192 set_current_blocked(&set);
Russell King68071482006-06-15 20:23:02 +0100193
194 __get_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
195 __get_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
196 __get_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
197 __get_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
198 __get_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
199 __get_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
200 __get_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
201 __get_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
202 __get_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
203 __get_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
204 __get_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
205 __get_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
206 __get_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
207 __get_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
208 __get_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
209 __get_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
210 __get_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 err |= !valid_user_regs(regs);
213
Dave Martince184a02017-06-30 18:56:59 +0100214 aux = (char __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100215#ifdef CONFIG_CRUNCH
216 if (err == 0)
Dave Martince184a02017-06-30 18:56:59 +0100217 err |= restore_crunch_context(&aux);
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100218#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219#ifdef CONFIG_IWMMXT
Dave Martince184a02017-06-30 18:56:59 +0100220 if (err == 0)
221 err |= restore_iwmmxt_context(&aux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222#endif
223#ifdef CONFIG_VFP
Imre Deak82c6f5a2010-04-11 15:58:27 +0100224 if (err == 0)
Dave Martince184a02017-06-30 18:56:59 +0100225 err |= restore_vfp_context(&aux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226#endif
227
228 return err;
229}
230
231asmlinkage int sys_sigreturn(struct pt_regs *regs)
232{
233 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800236 current->restart_block.fn = do_no_restart_syscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 /*
239 * Since we stacked the signal on a 64-bit boundary,
240 * then 'sp' should be word aligned here. If it's
241 * not, then the user is trying to mess with us.
242 */
243 if (regs->ARM_sp & 7)
244 goto badframe;
245
246 frame = (struct sigframe __user *)regs->ARM_sp;
247
248 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
249 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Russell King68071482006-06-15 20:23:02 +0100251 if (restore_sigframe(regs, frame))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 goto badframe;
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return regs->ARM_r0;
255
256badframe:
257 force_sig(SIGSEGV, current);
258 return 0;
259}
260
261asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
262{
263 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800266 current->restart_block.fn = do_no_restart_syscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 /*
269 * Since we stacked the signal on a 64-bit boundary,
270 * then 'sp' should be word aligned here. If it's
271 * not, then the user is trying to mess with us.
272 */
273 if (regs->ARM_sp & 7)
274 goto badframe;
275
276 frame = (struct rt_sigframe __user *)regs->ARM_sp;
277
278 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
279 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Russell King68071482006-06-15 20:23:02 +0100281 if (restore_sigframe(regs, &frame->sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 goto badframe;
283
Al Viroec93ac82012-12-23 01:52:54 -0500284 if (restore_altstack(&frame->sig.uc.uc_stack))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 goto badframe;
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return regs->ARM_r0;
288
289badframe:
290 force_sig(SIGSEGV, current);
291 return 0;
292}
293
294static int
Russell Kingaca6ca12006-06-15 20:28:03 +0100295setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs, sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100297 struct aux_sigframe __user *aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 int err = 0;
299
Russell Kingaca6ca12006-06-15 20:28:03 +0100300 __put_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
301 __put_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
302 __put_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
303 __put_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
304 __put_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
305 __put_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
306 __put_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
307 __put_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
308 __put_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
309 __put_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
310 __put_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
311 __put_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
312 __put_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
313 __put_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
314 __put_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
315 __put_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
316 __put_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Russell Kingaca6ca12006-06-15 20:28:03 +0100318 __put_user_error(current->thread.trap_no, &sf->uc.uc_mcontext.trap_no, err);
319 __put_user_error(current->thread.error_code, &sf->uc.uc_mcontext.error_code, err);
320 __put_user_error(current->thread.address, &sf->uc.uc_mcontext.fault_address, err);
321 __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Russell Kingaca6ca12006-06-15 20:28:03 +0100323 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100325 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100326#ifdef CONFIG_CRUNCH
327 if (err == 0)
328 err |= preserve_crunch_context(&aux->crunch);
329#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330#ifdef CONFIG_IWMMXT
Dave Martince184a02017-06-30 18:56:59 +0100331 if (err == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 err |= preserve_iwmmxt_context(&aux->iwmmxt);
333#endif
334#ifdef CONFIG_VFP
Imre Deak82c6f5a2010-04-11 15:58:27 +0100335 if (err == 0)
336 err |= preserve_vfp_context(&aux->vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337#endif
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100338 __put_user_error(0, &aux->end_magic, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 return err;
341}
342
343static inline void __user *
Al Viro7e243642012-11-07 17:53:13 -0500344get_sigframe(struct ksignal *ksig, struct pt_regs *regs, int framesize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Al Viro7e243642012-11-07 17:53:13 -0500346 unsigned long sp = sigsp(regs->ARM_sp, ksig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 void __user *frame;
348
349 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 * ATPCS B01 mandates 8-byte alignment
351 */
352 frame = (void __user *)((sp - framesize) & ~7);
353
354 /*
355 * Check that we can actually write to the signal frame.
356 */
357 if (!access_ok(VERIFY_WRITE, frame, framesize))
358 frame = NULL;
359
360 return frame;
361}
362
Al Viro7e243642012-11-07 17:53:13 -0500363static int
364setup_return(struct pt_regs *regs, struct ksignal *ksig,
365 unsigned long __user *rc, void __user *frame)
366{
367 unsigned long handler = (unsigned long)ksig->ka.sa.sa_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 unsigned long retcode;
369 int thumb = 0;
Russell King53399052011-02-20 12:22:52 +0000370 unsigned long cpsr = regs->ARM_cpsr & ~(PSR_f | PSR_E_BIT);
371
372 cpsr |= PSR_ENDSTATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 /*
375 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
376 */
Al Viro7e243642012-11-07 17:53:13 -0500377 if (ksig->ka.sa.sa_flags & SA_THIRTYTWO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
379
380#ifdef CONFIG_ARM_THUMB
381 if (elf_hwcap & HWCAP_THUMB) {
382 /*
383 * The LSB of the handler determines if we're going to
384 * be using THUMB or ARM mode for this signal handler.
385 */
386 thumb = handler & 1;
387
T.J. Purtell6ecf8302013-11-06 18:38:05 +0100388 /*
Russell King9b556132015-09-11 16:44:02 +0100389 * Clear the If-Then Thumb-2 execution state. ARM spec
390 * requires this to be all 000s in ARM mode. Snapdragon
391 * S4/Krait misbehaves on a Thumb=>ARM signal transition
392 * without this.
393 *
394 * We must do this whenever we are running on a Thumb-2
395 * capable CPU, which includes ARMv6T2. However, we elect
Russell King12fc7302015-09-16 11:08:49 +0100396 * to always do this to simplify the code; this field is
397 * marked UNK/SBZP for older architectures.
T.J. Purtell6ecf8302013-11-06 18:38:05 +0100398 */
399 cpsr &= ~PSR_IT_MASK;
T.J. Purtell6ecf8302013-11-06 18:38:05 +0100400
Catalin Marinasd71e1352009-05-30 14:00:15 +0100401 if (thumb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 cpsr |= PSR_T_BIT;
Catalin Marinasd71e1352009-05-30 14:00:15 +0100403 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 cpsr &= ~PSR_T_BIT;
405 }
406#endif
407
Al Viro7e243642012-11-07 17:53:13 -0500408 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
409 retcode = (unsigned long)ksig->ka.sa.sa_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 } else {
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000411 unsigned int idx = thumb << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Al Viro7e243642012-11-07 17:53:13 -0500413 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000414 idx += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Jonathan Austin9dfc28b2013-04-18 18:37:24 +0100416 /*
417 * Put the sigreturn code on the stack no matter which return
418 * mechanism we use in order to remain ABI compliant
419 */
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000420 if (__put_user(sigreturn_codes[idx], rc) ||
421 __put_user(sigreturn_codes[idx+1], rc+1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return 1;
423
Russell King8c0cc8a2013-08-03 10:39:51 +0100424#ifdef CONFIG_MMU
425 if (cpsr & MODE32_BIT) {
Russell King48be69a2013-07-24 00:29:18 +0100426 struct mm_struct *mm = current->mm;
427
Russell Kinge00d3492005-06-22 20:26:05 +0100428 /*
Russell King48be69a2013-07-24 00:29:18 +0100429 * 32-bit code can use the signal return page
430 * except when the MPU has protected the vectors
431 * page from PL0
Russell Kinge00d3492005-06-22 20:26:05 +0100432 */
Russell King48be69a2013-07-24 00:29:18 +0100433 retcode = mm->context.sigpage + signal_return_offset +
434 (idx << 2) + thumb;
Russell King8c0cc8a2013-08-03 10:39:51 +0100435 } else
436#endif
437 {
Russell Kinge00d3492005-06-22 20:26:05 +0100438 /*
439 * Ensure that the instruction cache sees
440 * the return code written onto the stack.
441 */
442 flush_icache_range((unsigned long)rc,
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000443 (unsigned long)(rc + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Russell Kinge00d3492005-06-22 20:26:05 +0100445 retcode = ((unsigned long)rc) + thumb;
446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448
Richard Weinbergera4980442014-07-13 15:24:03 +0200449 regs->ARM_r0 = ksig->sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 regs->ARM_sp = (unsigned long)frame;
451 regs->ARM_lr = retcode;
452 regs->ARM_pc = handler;
453 regs->ARM_cpsr = cpsr;
454
455 return 0;
456}
457
458static int
Al Viro7e243642012-11-07 17:53:13 -0500459setup_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Al Viro7e243642012-11-07 17:53:13 -0500461 struct sigframe __user *frame = get_sigframe(ksig, regs, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 int err = 0;
463
464 if (!frame)
465 return 1;
466
Russell Kingca195cf2006-06-24 22:41:09 +0100467 /*
468 * Set uc.uc_flags to a value which sc.trap_no would never have.
469 */
470 __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Russell Kingaca6ca12006-06-15 20:28:03 +0100472 err |= setup_sigframe(frame, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (err == 0)
Al Viro7e243642012-11-07 17:53:13 -0500474 err = setup_return(regs, ksig, frame->retcode, frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 return err;
477}
478
479static int
Al Viro7e243642012-11-07 17:53:13 -0500480setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Al Viro7e243642012-11-07 17:53:13 -0500482 struct rt_sigframe __user *frame = get_sigframe(ksig, regs, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 int err = 0;
484
485 if (!frame)
486 return 1;
487
Al Viro7e243642012-11-07 17:53:13 -0500488 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Russell Kingcb3504e2006-06-15 20:18:25 +0100490 __put_user_error(0, &frame->sig.uc.uc_flags, err);
491 __put_user_error(NULL, &frame->sig.uc.uc_link, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Al Viroec93ac82012-12-23 01:52:54 -0500493 err |= __save_altstack(&frame->sig.uc.uc_stack, regs->ARM_sp);
Russell Kingaca6ca12006-06-15 20:28:03 +0100494 err |= setup_sigframe(&frame->sig, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (err == 0)
Al Viro7e243642012-11-07 17:53:13 -0500496 err = setup_return(regs, ksig, frame->sig.retcode, frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 if (err == 0) {
499 /*
500 * For realtime signals we must also set the second and third
501 * arguments for the signal handler.
502 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
503 */
504 regs->ARM_r1 = (unsigned long)&frame->info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100505 regs->ARM_r2 = (unsigned long)&frame->sig.uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507
508 return err;
509}
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511/*
512 * OK, we're invoking a handler
513 */
Al Viro7e243642012-11-07 17:53:13 -0500514static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Al Virob7f9a112012-05-02 09:59:21 -0400516 sigset_t *oldset = sigmask_to_save();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 int ret;
518
519 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 * Set up the stack frame
521 */
Al Viro7e243642012-11-07 17:53:13 -0500522 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
523 ret = setup_rt_frame(ksig, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 else
Al Viro7e243642012-11-07 17:53:13 -0500525 ret = setup_frame(ksig, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 /*
528 * Check that the resulting registers are actually sane.
529 */
530 ret |= !valid_user_regs(regs);
531
Al Viro7e243642012-11-07 17:53:13 -0500532 signal_setup_done(ret, ksig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
535/*
536 * Note that 'init' is a special process: it doesn't get signals it doesn't
537 * want to handle. Thus you cannot kill init even with a SIGKILL even by
538 * mistake.
539 *
540 * Note that we go through the signals twice: once to check the signals that
541 * the kernel can handle, and then we build all the user-level signal handling
542 * stack-frames in one go after that.
543 */
Al Viro81783782012-07-19 17:48:21 +0100544static int do_signal(struct pt_regs *regs, int syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100546 unsigned int retval = 0, continue_addr = 0, restart_addr = 0;
Al Viro7e243642012-11-07 17:53:13 -0500547 struct ksignal ksig;
Al Viro81783782012-07-19 17:48:21 +0100548 int restart = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 /*
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100551 * If we were from a system call, check for system call restarting...
552 */
553 if (syscall) {
554 continue_addr = regs->ARM_pc;
555 restart_addr = continue_addr - (thumb_mode(regs) ? 2 : 4);
556 retval = regs->ARM_r0;
557
558 /*
559 * Prepare for system call restart. We do this here so that a
560 * debugger will see the already changed PSW.
561 */
562 switch (retval) {
Al Viro81783782012-07-19 17:48:21 +0100563 case -ERESTART_RESTARTBLOCK:
Al Viro66285212012-07-19 17:48:50 +0100564 restart -= 2;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100565 case -ERESTARTNOHAND:
566 case -ERESTARTSYS:
567 case -ERESTARTNOINTR:
Al Viro81783782012-07-19 17:48:21 +0100568 restart++;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100569 regs->ARM_r0 = regs->ARM_ORIG_r0;
570 regs->ARM_pc = restart_addr;
571 break;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100572 }
573 }
574
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100575 /*
576 * Get the signal to deliver. When running under ptrace, at this
577 * point the debugger may change all our registers ...
578 */
Al Viro81783782012-07-19 17:48:21 +0100579 /*
580 * Depending on the signal settings we may need to revert the
581 * decision to restart the system call. But skip this if a
582 * debugger has chosen to restart at a different PC.
583 */
Al Viro7e243642012-11-07 17:53:13 -0500584 if (get_signal(&ksig)) {
585 /* handler */
586 if (unlikely(restart) && regs->ARM_pc == restart_addr) {
Will Deaconad82cc02012-07-19 17:46:44 +0100587 if (retval == -ERESTARTNOHAND ||
588 retval == -ERESTART_RESTARTBLOCK
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100589 || (retval == -ERESTARTSYS
Al Viro7e243642012-11-07 17:53:13 -0500590 && !(ksig.ka.sa.sa_flags & SA_RESTART))) {
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100591 regs->ARM_r0 = -EINTR;
592 regs->ARM_pc = continue_addr;
593 }
594 }
Al Viro7e243642012-11-07 17:53:13 -0500595 handle_signal(&ksig, regs);
596 } else {
597 /* no handler */
598 restore_saved_sigmask();
599 if (unlikely(restart) && regs->ARM_pc == restart_addr) {
600 regs->ARM_pc = continue_addr;
601 return restart;
602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
Al Viro7e243642012-11-07 17:53:13 -0500604 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
Al Viro81783782012-07-19 17:48:21 +0100607asmlinkage int
Al Viro0a267fa2012-07-19 17:47:55 +0100608do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Russell King3302cad2015-08-20 16:13:37 +0100610 /*
611 * The assembly code enters us with IRQs off, but it hasn't
612 * informed the tracing code of that for efficiency reasons.
613 * Update the trace code with the current status.
614 */
615 trace_hardirqs_off();
Al Viro0a267fa2012-07-19 17:47:55 +0100616 do {
617 if (likely(thread_flags & _TIF_NEED_RESCHED)) {
618 schedule();
619 } else {
620 if (unlikely(!user_mode(regs)))
Al Viro81783782012-07-19 17:48:21 +0100621 return 0;
Al Viro0a267fa2012-07-19 17:47:55 +0100622 local_irq_enable();
623 if (thread_flags & _TIF_SIGPENDING) {
Al Viro66285212012-07-19 17:48:50 +0100624 int restart = do_signal(regs, syscall);
625 if (unlikely(restart)) {
Al Viro81783782012-07-19 17:48:21 +0100626 /*
627 * Restart without handlers.
628 * Deal with it without leaving
629 * the kernel space.
630 */
Al Viro66285212012-07-19 17:48:50 +0100631 return restart;
Al Viro81783782012-07-19 17:48:21 +0100632 }
Al Viro0a267fa2012-07-19 17:47:55 +0100633 syscall = 0;
David A. Longc7edc9e2014-03-07 11:23:04 -0500634 } else if (thread_flags & _TIF_UPROBE) {
David A. Longc7edc9e2014-03-07 11:23:04 -0500635 uprobe_notify_resume(regs);
Al Viro0a267fa2012-07-19 17:47:55 +0100636 } else {
637 clear_thread_flag(TIF_NOTIFY_RESUME);
638 tracehook_notify_resume(regs);
639 }
640 }
641 local_irq_disable();
642 thread_flags = current_thread_info()->flags;
643 } while (thread_flags & _TIF_WORK_MASK);
Al Viro81783782012-07-19 17:48:21 +0100644 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
Russell King48be69a2013-07-24 00:29:18 +0100646
Russell King48be69a2013-07-24 00:29:18 +0100647struct page *get_signal_page(void)
648{
Russell Kinge0d40752013-08-03 10:30:05 +0100649 unsigned long ptr;
650 unsigned offset;
651 struct page *page;
652 void *addr;
Russell King48be69a2013-07-24 00:29:18 +0100653
Russell Kinge0d40752013-08-03 10:30:05 +0100654 page = alloc_pages(GFP_KERNEL, 0);
Russell King48be69a2013-07-24 00:29:18 +0100655
Russell Kinge0d40752013-08-03 10:30:05 +0100656 if (!page)
657 return NULL;
Russell King48be69a2013-07-24 00:29:18 +0100658
Russell Kinge0d40752013-08-03 10:30:05 +0100659 addr = page_address(page);
Russell King48be69a2013-07-24 00:29:18 +0100660
Russell Kinge0d40752013-08-03 10:30:05 +0100661 /* Give the signal return code some randomness */
662 offset = 0x200 + (get_random_int() & 0x7fc);
663 signal_return_offset = offset;
Russell King48be69a2013-07-24 00:29:18 +0100664
Russell Kinge0d40752013-08-03 10:30:05 +0100665 /*
666 * Copy signal return handlers into the vector page, and
667 * set sigreturn to be a pointer to these.
668 */
669 memcpy(addr + offset, sigreturn_codes, sizeof(sigreturn_codes));
Russell King48be69a2013-07-24 00:29:18 +0100670
Russell Kinge0d40752013-08-03 10:30:05 +0100671 ptr = (unsigned long)addr + offset;
672 flush_icache_range(ptr, ptr + sizeof(sigreturn_codes));
Russell King48be69a2013-07-24 00:29:18 +0100673
Russell Kinge0d40752013-08-03 10:30:05 +0100674 return page;
Russell King48be69a2013-07-24 00:29:18 +0100675}