blob: 1f3574ec4fad823d560a644b53a1f9c871240d89 [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/unistd.h>
Imre Deak82c6f5a2010-04-11 15:58:27 +010022#include <asm/vfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Nicolas Pitre5c165952017-08-09 23:42:51 -040024#include "signal.h"
25
26extern const unsigned long sigreturn_codes[17];
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Russell King48be69a2013-07-24 00:29:18 +010028static unsigned long signal_return_offset;
29
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010030#ifdef CONFIG_CRUNCH
Hartley Sweeten65a50532009-08-04 23:20:45 +010031static int preserve_crunch_context(struct crunch_sigframe __user *frame)
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010032{
33 char kbuf[sizeof(*frame) + 8];
34 struct crunch_sigframe *kframe;
35
36 /* the crunch context must be 64 bit aligned */
37 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
38 kframe->magic = CRUNCH_MAGIC;
39 kframe->size = CRUNCH_STORAGE_SIZE;
40 crunch_task_copy(current_thread_info(), &kframe->storage);
41 return __copy_to_user(frame, kframe, sizeof(*frame));
42}
43
Dave Martince184a02017-06-30 18:56:59 +010044static int restore_crunch_context(char __user **auxp)
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010045{
Dave Martince184a02017-06-30 18:56:59 +010046 struct crunch_sigframe __user *frame =
47 (struct crunch_sigframe __user *)*auxp;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010048 char kbuf[sizeof(*frame) + 8];
49 struct crunch_sigframe *kframe;
50
51 /* the crunch context must be 64 bit aligned */
52 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
53 if (__copy_from_user(kframe, frame, sizeof(*frame)))
54 return -1;
55 if (kframe->magic != CRUNCH_MAGIC ||
56 kframe->size != CRUNCH_STORAGE_SIZE)
57 return -1;
Dave Martince184a02017-06-30 18:56:59 +010058 *auxp += CRUNCH_STORAGE_SIZE;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010059 crunch_task_restore(current_thread_info(), &kframe->storage);
60 return 0;
61}
62#endif
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#ifdef CONFIG_IWMMXT
65
Dave Martin26958352017-06-30 18:56:09 +010066static int preserve_iwmmxt_context(struct iwmmxt_sigframe __user *frame)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Hugh Dickins69b04752005-10-29 18:16:36 -070068 char kbuf[sizeof(*frame) + 8];
69 struct iwmmxt_sigframe *kframe;
Dave Martince184a02017-06-30 18:56:59 +010070 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 /* the iWMMXt context must be 64 bit aligned */
Hugh Dickins69b04752005-10-29 18:16:36 -070073 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
Dave Martince184a02017-06-30 18:56:59 +010074
75 if (test_thread_flag(TIF_USING_IWMMXT)) {
76 kframe->magic = IWMMXT_MAGIC;
77 kframe->size = IWMMXT_STORAGE_SIZE;
78 iwmmxt_task_copy(current_thread_info(), &kframe->storage);
79
80 err = __copy_to_user(frame, kframe, sizeof(*frame));
81 } else {
82 /*
83 * For bug-compatibility with older kernels, some space
84 * has to be reserved for iWMMXt even if it's not used.
85 * Set the magic and size appropriately so that properly
86 * written userspace can skip it reliably:
87 */
88 __put_user_error(DUMMY_MAGIC, &frame->magic, err);
89 __put_user_error(IWMMXT_STORAGE_SIZE, &frame->size, err);
90 }
91
92 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Dave Martince184a02017-06-30 18:56:59 +010095static int restore_iwmmxt_context(char __user **auxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Dave Martince184a02017-06-30 18:56:59 +010097 struct iwmmxt_sigframe __user *frame =
98 (struct iwmmxt_sigframe __user *)*auxp;
Hugh Dickins69b04752005-10-29 18:16:36 -070099 char kbuf[sizeof(*frame) + 8];
100 struct iwmmxt_sigframe *kframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Hugh Dickins69b04752005-10-29 18:16:36 -0700102 /* the iWMMXt context must be 64 bit aligned */
103 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
104 if (__copy_from_user(kframe, frame, sizeof(*frame)))
105 return -1;
Dave Martince184a02017-06-30 18:56:59 +0100106
107 /*
108 * For non-iWMMXt threads: a single iwmmxt_sigframe-sized dummy
109 * block is discarded for compatibility with setup_sigframe() if
110 * present, but we don't mandate its presence. If some other
111 * magic is here, it's not for us:
112 */
113 if (!test_thread_flag(TIF_USING_IWMMXT) &&
114 kframe->magic != DUMMY_MAGIC)
115 return 0;
116
117 if (kframe->size != IWMMXT_STORAGE_SIZE)
Hugh Dickins69b04752005-10-29 18:16:36 -0700118 return -1;
Dave Martince184a02017-06-30 18:56:59 +0100119
120 if (test_thread_flag(TIF_USING_IWMMXT)) {
121 if (kframe->magic != IWMMXT_MAGIC)
122 return -1;
123
124 iwmmxt_task_restore(current_thread_info(), &kframe->storage);
125 }
126
127 *auxp += IWMMXT_STORAGE_SIZE;
Hugh Dickins69b04752005-10-29 18:16:36 -0700128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
131#endif
132
Imre Deak82c6f5a2010-04-11 15:58:27 +0100133#ifdef CONFIG_VFP
134
135static int preserve_vfp_context(struct vfp_sigframe __user *frame)
136{
Imre Deak82c6f5a2010-04-11 15:58:27 +0100137 const unsigned long magic = VFP_MAGIC;
138 const unsigned long size = VFP_STORAGE_SIZE;
139 int err = 0;
140
Imre Deak82c6f5a2010-04-11 15:58:27 +0100141 __put_user_error(magic, &frame->magic, err);
142 __put_user_error(size, &frame->size, err);
143
Will Deacon2498814f2012-04-23 15:38:28 +0100144 if (err)
145 return -EFAULT;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100146
Will Deacon2498814f2012-04-23 15:38:28 +0100147 return vfp_preserve_user_clear_hwstate(&frame->ufp, &frame->ufp_exc);
Imre Deak82c6f5a2010-04-11 15:58:27 +0100148}
149
Dave Martince184a02017-06-30 18:56:59 +0100150static int restore_vfp_context(char __user **auxp)
Imre Deak82c6f5a2010-04-11 15:58:27 +0100151{
Dave Martince184a02017-06-30 18:56:59 +0100152 struct vfp_sigframe __user *frame =
153 (struct vfp_sigframe __user *)*auxp;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100154 unsigned long magic;
155 unsigned long size;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100156 int err = 0;
157
158 __get_user_error(magic, &frame->magic, err);
159 __get_user_error(size, &frame->size, err);
160
161 if (err)
162 return -EFAULT;
163 if (magic != VFP_MAGIC || size != VFP_STORAGE_SIZE)
164 return -EINVAL;
165
Dave Martince184a02017-06-30 18:56:59 +0100166 *auxp += size;
Will Deacon2498814f2012-04-23 15:38:28 +0100167 return vfp_restore_user_hwstate(&frame->ufp, &frame->ufp_exc);
Imre Deak82c6f5a2010-04-11 15:58:27 +0100168}
169
170#endif
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * Do a signal return; undo the signal stack. These are aligned to 64-bit.
174 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Russell King68071482006-06-15 20:23:02 +0100176static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Dave Martince184a02017-06-30 18:56:59 +0100178 char __user *aux;
Russell King68071482006-06-15 20:23:02 +0100179 sigset_t set;
180 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Russell King68071482006-06-15 20:23:02 +0100182 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
Al Viro77097ae2012-04-27 13:58:59 -0400183 if (err == 0)
Matt Fleming101d9b02012-03-05 15:05:34 -0800184 set_current_blocked(&set);
Russell King68071482006-06-15 20:23:02 +0100185
186 __get_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
187 __get_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
188 __get_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
189 __get_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
190 __get_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
191 __get_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
192 __get_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
193 __get_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
194 __get_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
195 __get_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
196 __get_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
197 __get_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
198 __get_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
199 __get_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
200 __get_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
201 __get_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
202 __get_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 err |= !valid_user_regs(regs);
205
Dave Martince184a02017-06-30 18:56:59 +0100206 aux = (char __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100207#ifdef CONFIG_CRUNCH
208 if (err == 0)
Dave Martince184a02017-06-30 18:56:59 +0100209 err |= restore_crunch_context(&aux);
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100210#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211#ifdef CONFIG_IWMMXT
Dave Martince184a02017-06-30 18:56:59 +0100212 if (err == 0)
213 err |= restore_iwmmxt_context(&aux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214#endif
215#ifdef CONFIG_VFP
Imre Deak82c6f5a2010-04-11 15:58:27 +0100216 if (err == 0)
Dave Martince184a02017-06-30 18:56:59 +0100217 err |= restore_vfp_context(&aux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218#endif
219
220 return err;
221}
222
223asmlinkage int sys_sigreturn(struct pt_regs *regs)
224{
225 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800228 current->restart_block.fn = do_no_restart_syscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 /*
231 * Since we stacked the signal on a 64-bit boundary,
232 * then 'sp' should be word aligned here. If it's
233 * not, then the user is trying to mess with us.
234 */
235 if (regs->ARM_sp & 7)
236 goto badframe;
237
238 frame = (struct sigframe __user *)regs->ARM_sp;
239
240 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
241 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Russell King68071482006-06-15 20:23:02 +0100243 if (restore_sigframe(regs, frame))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 goto badframe;
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return regs->ARM_r0;
247
248badframe:
249 force_sig(SIGSEGV, current);
250 return 0;
251}
252
253asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
254{
255 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800258 current->restart_block.fn = do_no_restart_syscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 /*
261 * Since we stacked the signal on a 64-bit boundary,
262 * then 'sp' should be word aligned here. If it's
263 * not, then the user is trying to mess with us.
264 */
265 if (regs->ARM_sp & 7)
266 goto badframe;
267
268 frame = (struct rt_sigframe __user *)regs->ARM_sp;
269
270 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
271 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Russell King68071482006-06-15 20:23:02 +0100273 if (restore_sigframe(regs, &frame->sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 goto badframe;
275
Al Viroec93ac82012-12-23 01:52:54 -0500276 if (restore_altstack(&frame->sig.uc.uc_stack))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 goto badframe;
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return regs->ARM_r0;
280
281badframe:
282 force_sig(SIGSEGV, current);
283 return 0;
284}
285
286static int
Russell Kingaca6ca12006-06-15 20:28:03 +0100287setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs, sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100289 struct aux_sigframe __user *aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 int err = 0;
291
Russell Kingaca6ca12006-06-15 20:28:03 +0100292 __put_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
293 __put_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
294 __put_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
295 __put_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
296 __put_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
297 __put_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
298 __put_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
299 __put_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
300 __put_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
301 __put_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
302 __put_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
303 __put_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
304 __put_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
305 __put_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
306 __put_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
307 __put_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
308 __put_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Russell Kingaca6ca12006-06-15 20:28:03 +0100310 __put_user_error(current->thread.trap_no, &sf->uc.uc_mcontext.trap_no, err);
311 __put_user_error(current->thread.error_code, &sf->uc.uc_mcontext.error_code, err);
312 __put_user_error(current->thread.address, &sf->uc.uc_mcontext.fault_address, err);
313 __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Russell Kingaca6ca12006-06-15 20:28:03 +0100315 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100317 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100318#ifdef CONFIG_CRUNCH
319 if (err == 0)
320 err |= preserve_crunch_context(&aux->crunch);
321#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322#ifdef CONFIG_IWMMXT
Dave Martince184a02017-06-30 18:56:59 +0100323 if (err == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 err |= preserve_iwmmxt_context(&aux->iwmmxt);
325#endif
326#ifdef CONFIG_VFP
Imre Deak82c6f5a2010-04-11 15:58:27 +0100327 if (err == 0)
328 err |= preserve_vfp_context(&aux->vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329#endif
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100330 __put_user_error(0, &aux->end_magic, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 return err;
333}
334
335static inline void __user *
Al Viro7e243642012-11-07 17:53:13 -0500336get_sigframe(struct ksignal *ksig, struct pt_regs *regs, int framesize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Al Viro7e243642012-11-07 17:53:13 -0500338 unsigned long sp = sigsp(regs->ARM_sp, ksig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 void __user *frame;
340
341 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 * ATPCS B01 mandates 8-byte alignment
343 */
344 frame = (void __user *)((sp - framesize) & ~7);
345
346 /*
347 * Check that we can actually write to the signal frame.
348 */
349 if (!access_ok(VERIFY_WRITE, frame, framesize))
350 frame = NULL;
351
352 return frame;
353}
354
Al Viro7e243642012-11-07 17:53:13 -0500355static int
356setup_return(struct pt_regs *regs, struct ksignal *ksig,
357 unsigned long __user *rc, void __user *frame)
358{
359 unsigned long handler = (unsigned long)ksig->ka.sa.sa_handler;
Nicolas Pitre5c165952017-08-09 23:42:51 -0400360 unsigned long handler_fdpic_GOT = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 unsigned long retcode;
Nicolas Pitre5c165952017-08-09 23:42:51 -0400362 unsigned int idx, thumb = 0;
Russell King53399052011-02-20 12:22:52 +0000363 unsigned long cpsr = regs->ARM_cpsr & ~(PSR_f | PSR_E_BIT);
Nicolas Pitre5c165952017-08-09 23:42:51 -0400364 bool fdpic = IS_ENABLED(CONFIG_BINFMT_ELF_FDPIC) &&
365 (current->personality & FDPIC_FUNCPTRS);
366
367 if (fdpic) {
368 unsigned long __user *fdpic_func_desc =
369 (unsigned long __user *)handler;
370 if (__get_user(handler, &fdpic_func_desc[0]) ||
371 __get_user(handler_fdpic_GOT, &fdpic_func_desc[1]))
372 return 1;
373 }
Russell King53399052011-02-20 12:22:52 +0000374
375 cpsr |= PSR_ENDSTATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 /*
378 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
379 */
Al Viro7e243642012-11-07 17:53:13 -0500380 if (ksig->ka.sa.sa_flags & SA_THIRTYTWO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
382
383#ifdef CONFIG_ARM_THUMB
384 if (elf_hwcap & HWCAP_THUMB) {
385 /*
386 * The LSB of the handler determines if we're going to
387 * be using THUMB or ARM mode for this signal handler.
388 */
389 thumb = handler & 1;
390
T.J. Purtell6ecf8302013-11-06 18:38:05 +0100391 /*
Russell King9b556132015-09-11 16:44:02 +0100392 * Clear the If-Then Thumb-2 execution state. ARM spec
393 * requires this to be all 000s in ARM mode. Snapdragon
394 * S4/Krait misbehaves on a Thumb=>ARM signal transition
395 * without this.
396 *
397 * We must do this whenever we are running on a Thumb-2
398 * capable CPU, which includes ARMv6T2. However, we elect
Russell King12fc7302015-09-16 11:08:49 +0100399 * to always do this to simplify the code; this field is
400 * marked UNK/SBZP for older architectures.
T.J. Purtell6ecf8302013-11-06 18:38:05 +0100401 */
402 cpsr &= ~PSR_IT_MASK;
T.J. Purtell6ecf8302013-11-06 18:38:05 +0100403
Catalin Marinasd71e1352009-05-30 14:00:15 +0100404 if (thumb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 cpsr |= PSR_T_BIT;
Catalin Marinasd71e1352009-05-30 14:00:15 +0100406 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 cpsr &= ~PSR_T_BIT;
408 }
409#endif
410
Al Viro7e243642012-11-07 17:53:13 -0500411 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
412 retcode = (unsigned long)ksig->ka.sa.sa_restorer;
Nicolas Pitre5c165952017-08-09 23:42:51 -0400413 if (fdpic) {
414 /*
415 * We need code to load the function descriptor.
416 * That code follows the standard sigreturn code
417 * (6 words), and is made of 3 + 2 words for each
418 * variant. The 4th copied word is the actual FD
419 * address that the assembly code expects.
420 */
421 idx = 6 + thumb * 3;
422 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
423 idx += 5;
424 if (__put_user(sigreturn_codes[idx], rc ) ||
425 __put_user(sigreturn_codes[idx+1], rc+1) ||
426 __put_user(sigreturn_codes[idx+2], rc+2) ||
427 __put_user(retcode, rc+3))
428 return 1;
429 goto rc_finish;
430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 } else {
Nicolas Pitre5c165952017-08-09 23:42:51 -0400432 idx = thumb << 1;
Al Viro7e243642012-11-07 17:53:13 -0500433 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000434 idx += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Jonathan Austin9dfc28b2013-04-18 18:37:24 +0100436 /*
437 * Put the sigreturn code on the stack no matter which return
438 * mechanism we use in order to remain ABI compliant
439 */
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000440 if (__put_user(sigreturn_codes[idx], rc) ||
441 __put_user(sigreturn_codes[idx+1], rc+1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return 1;
443
Nicolas Pitre5c165952017-08-09 23:42:51 -0400444rc_finish:
Russell King8c0cc8a2013-08-03 10:39:51 +0100445#ifdef CONFIG_MMU
446 if (cpsr & MODE32_BIT) {
Russell King48be69a2013-07-24 00:29:18 +0100447 struct mm_struct *mm = current->mm;
448
Russell Kinge00d3492005-06-22 20:26:05 +0100449 /*
Russell King48be69a2013-07-24 00:29:18 +0100450 * 32-bit code can use the signal return page
451 * except when the MPU has protected the vectors
452 * page from PL0
Russell Kinge00d3492005-06-22 20:26:05 +0100453 */
Russell King48be69a2013-07-24 00:29:18 +0100454 retcode = mm->context.sigpage + signal_return_offset +
455 (idx << 2) + thumb;
Russell King8c0cc8a2013-08-03 10:39:51 +0100456 } else
457#endif
458 {
Russell Kinge00d3492005-06-22 20:26:05 +0100459 /*
460 * Ensure that the instruction cache sees
461 * the return code written onto the stack.
462 */
463 flush_icache_range((unsigned long)rc,
Nicolas Pitre5c165952017-08-09 23:42:51 -0400464 (unsigned long)(rc + 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Russell Kinge00d3492005-06-22 20:26:05 +0100466 retcode = ((unsigned long)rc) + thumb;
467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469
Richard Weinbergera4980442014-07-13 15:24:03 +0200470 regs->ARM_r0 = ksig->sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 regs->ARM_sp = (unsigned long)frame;
472 regs->ARM_lr = retcode;
473 regs->ARM_pc = handler;
Nicolas Pitre5c165952017-08-09 23:42:51 -0400474 if (fdpic)
475 regs->ARM_r9 = handler_fdpic_GOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 regs->ARM_cpsr = cpsr;
477
478 return 0;
479}
480
481static int
Al Viro7e243642012-11-07 17:53:13 -0500482setup_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Al Viro7e243642012-11-07 17:53:13 -0500484 struct sigframe __user *frame = get_sigframe(ksig, regs, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 int err = 0;
486
487 if (!frame)
488 return 1;
489
Russell Kingca195cf2006-06-24 22:41:09 +0100490 /*
491 * Set uc.uc_flags to a value which sc.trap_no would never have.
492 */
493 __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Russell Kingaca6ca12006-06-15 20:28:03 +0100495 err |= setup_sigframe(frame, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (err == 0)
Al Viro7e243642012-11-07 17:53:13 -0500497 err = setup_return(regs, ksig, frame->retcode, frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 return err;
500}
501
502static int
Al Viro7e243642012-11-07 17:53:13 -0500503setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Al Viro7e243642012-11-07 17:53:13 -0500505 struct rt_sigframe __user *frame = get_sigframe(ksig, regs, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 int err = 0;
507
508 if (!frame)
509 return 1;
510
Al Viro7e243642012-11-07 17:53:13 -0500511 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Russell Kingcb3504e2006-06-15 20:18:25 +0100513 __put_user_error(0, &frame->sig.uc.uc_flags, err);
514 __put_user_error(NULL, &frame->sig.uc.uc_link, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Al Viroec93ac82012-12-23 01:52:54 -0500516 err |= __save_altstack(&frame->sig.uc.uc_stack, regs->ARM_sp);
Russell Kingaca6ca12006-06-15 20:28:03 +0100517 err |= setup_sigframe(&frame->sig, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (err == 0)
Al Viro7e243642012-11-07 17:53:13 -0500519 err = setup_return(regs, ksig, frame->sig.retcode, frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 if (err == 0) {
522 /*
523 * For realtime signals we must also set the second and third
524 * arguments for the signal handler.
525 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
526 */
527 regs->ARM_r1 = (unsigned long)&frame->info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100528 regs->ARM_r2 = (unsigned long)&frame->sig.uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530
531 return err;
532}
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534/*
535 * OK, we're invoking a handler
536 */
Al Viro7e243642012-11-07 17:53:13 -0500537static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Al Virob7f9a112012-05-02 09:59:21 -0400539 sigset_t *oldset = sigmask_to_save();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 int ret;
541
542 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 * Set up the stack frame
544 */
Al Viro7e243642012-11-07 17:53:13 -0500545 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
546 ret = setup_rt_frame(ksig, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 else
Al Viro7e243642012-11-07 17:53:13 -0500548 ret = setup_frame(ksig, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 /*
551 * Check that the resulting registers are actually sane.
552 */
553 ret |= !valid_user_regs(regs);
554
Al Viro7e243642012-11-07 17:53:13 -0500555 signal_setup_done(ret, ksig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
558/*
559 * Note that 'init' is a special process: it doesn't get signals it doesn't
560 * want to handle. Thus you cannot kill init even with a SIGKILL even by
561 * mistake.
562 *
563 * Note that we go through the signals twice: once to check the signals that
564 * the kernel can handle, and then we build all the user-level signal handling
565 * stack-frames in one go after that.
566 */
Al Viro81783782012-07-19 17:48:21 +0100567static int do_signal(struct pt_regs *regs, int syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100569 unsigned int retval = 0, continue_addr = 0, restart_addr = 0;
Al Viro7e243642012-11-07 17:53:13 -0500570 struct ksignal ksig;
Al Viro81783782012-07-19 17:48:21 +0100571 int restart = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 /*
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100574 * If we were from a system call, check for system call restarting...
575 */
576 if (syscall) {
577 continue_addr = regs->ARM_pc;
578 restart_addr = continue_addr - (thumb_mode(regs) ? 2 : 4);
579 retval = regs->ARM_r0;
580
581 /*
582 * Prepare for system call restart. We do this here so that a
583 * debugger will see the already changed PSW.
584 */
585 switch (retval) {
Al Viro81783782012-07-19 17:48:21 +0100586 case -ERESTART_RESTARTBLOCK:
Al Viro66285212012-07-19 17:48:50 +0100587 restart -= 2;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100588 case -ERESTARTNOHAND:
589 case -ERESTARTSYS:
590 case -ERESTARTNOINTR:
Al Viro81783782012-07-19 17:48:21 +0100591 restart++;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100592 regs->ARM_r0 = regs->ARM_ORIG_r0;
593 regs->ARM_pc = restart_addr;
594 break;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100595 }
596 }
597
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100598 /*
599 * Get the signal to deliver. When running under ptrace, at this
600 * point the debugger may change all our registers ...
601 */
Al Viro81783782012-07-19 17:48:21 +0100602 /*
603 * Depending on the signal settings we may need to revert the
604 * decision to restart the system call. But skip this if a
605 * debugger has chosen to restart at a different PC.
606 */
Al Viro7e243642012-11-07 17:53:13 -0500607 if (get_signal(&ksig)) {
608 /* handler */
609 if (unlikely(restart) && regs->ARM_pc == restart_addr) {
Will Deaconad82cc02012-07-19 17:46:44 +0100610 if (retval == -ERESTARTNOHAND ||
611 retval == -ERESTART_RESTARTBLOCK
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100612 || (retval == -ERESTARTSYS
Al Viro7e243642012-11-07 17:53:13 -0500613 && !(ksig.ka.sa.sa_flags & SA_RESTART))) {
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100614 regs->ARM_r0 = -EINTR;
615 regs->ARM_pc = continue_addr;
616 }
617 }
Al Viro7e243642012-11-07 17:53:13 -0500618 handle_signal(&ksig, regs);
619 } else {
620 /* no handler */
621 restore_saved_sigmask();
622 if (unlikely(restart) && regs->ARM_pc == restart_addr) {
623 regs->ARM_pc = continue_addr;
624 return restart;
625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
Al Viro7e243642012-11-07 17:53:13 -0500627 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
Al Viro81783782012-07-19 17:48:21 +0100630asmlinkage int
Al Viro0a267fa2012-07-19 17:47:55 +0100631do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Russell King3302cad2015-08-20 16:13:37 +0100633 /*
634 * The assembly code enters us with IRQs off, but it hasn't
635 * informed the tracing code of that for efficiency reasons.
636 * Update the trace code with the current status.
637 */
638 trace_hardirqs_off();
Al Viro0a267fa2012-07-19 17:47:55 +0100639 do {
640 if (likely(thread_flags & _TIF_NEED_RESCHED)) {
641 schedule();
642 } else {
643 if (unlikely(!user_mode(regs)))
Al Viro81783782012-07-19 17:48:21 +0100644 return 0;
Al Viro0a267fa2012-07-19 17:47:55 +0100645 local_irq_enable();
646 if (thread_flags & _TIF_SIGPENDING) {
Al Viro66285212012-07-19 17:48:50 +0100647 int restart = do_signal(regs, syscall);
648 if (unlikely(restart)) {
Al Viro81783782012-07-19 17:48:21 +0100649 /*
650 * Restart without handlers.
651 * Deal with it without leaving
652 * the kernel space.
653 */
Al Viro66285212012-07-19 17:48:50 +0100654 return restart;
Al Viro81783782012-07-19 17:48:21 +0100655 }
Al Viro0a267fa2012-07-19 17:47:55 +0100656 syscall = 0;
David A. Longc7edc9e2014-03-07 11:23:04 -0500657 } else if (thread_flags & _TIF_UPROBE) {
David A. Longc7edc9e2014-03-07 11:23:04 -0500658 uprobe_notify_resume(regs);
Al Viro0a267fa2012-07-19 17:47:55 +0100659 } else {
660 clear_thread_flag(TIF_NOTIFY_RESUME);
661 tracehook_notify_resume(regs);
662 }
663 }
664 local_irq_disable();
665 thread_flags = current_thread_info()->flags;
666 } while (thread_flags & _TIF_WORK_MASK);
Al Viro81783782012-07-19 17:48:21 +0100667 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
Russell King48be69a2013-07-24 00:29:18 +0100669
Russell King48be69a2013-07-24 00:29:18 +0100670struct page *get_signal_page(void)
671{
Russell Kinge0d40752013-08-03 10:30:05 +0100672 unsigned long ptr;
673 unsigned offset;
674 struct page *page;
675 void *addr;
Russell King48be69a2013-07-24 00:29:18 +0100676
Russell Kinge0d40752013-08-03 10:30:05 +0100677 page = alloc_pages(GFP_KERNEL, 0);
Russell King48be69a2013-07-24 00:29:18 +0100678
Russell Kinge0d40752013-08-03 10:30:05 +0100679 if (!page)
680 return NULL;
Russell King48be69a2013-07-24 00:29:18 +0100681
Russell Kinge0d40752013-08-03 10:30:05 +0100682 addr = page_address(page);
Russell King48be69a2013-07-24 00:29:18 +0100683
Russell Kinge0d40752013-08-03 10:30:05 +0100684 /* Give the signal return code some randomness */
685 offset = 0x200 + (get_random_int() & 0x7fc);
686 signal_return_offset = offset;
Russell King48be69a2013-07-24 00:29:18 +0100687
Russell Kinge0d40752013-08-03 10:30:05 +0100688 /*
689 * Copy signal return handlers into the vector page, and
690 * set sigreturn to be a pointer to these.
691 */
692 memcpy(addr + offset, sigreturn_codes, sizeof(sigreturn_codes));
Russell King48be69a2013-07-24 00:29:18 +0100693
Russell Kinge0d40752013-08-03 10:30:05 +0100694 ptr = (unsigned long)addr + offset;
695 flush_icache_range(ptr, ptr + sizeof(sigreturn_codes));
Russell King48be69a2013-07-24 00:29:18 +0100696
Russell Kinge0d40752013-08-03 10:30:05 +0100697 return page;
Russell King48be69a2013-07-24 00:29:18 +0100698}