blob: 0ae74207e43edce0d5793c25fe200dec8e942fce [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>
Thomas Garniere33f8d322017-09-07 08:30:46 -070017#include <linux/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Russell Kingee90dab2006-11-09 14:20:47 +000019#include <asm/elf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/cacheflush.h>
Russell King48be69a2013-07-24 00:29:18 +010021#include <asm/traps.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
Nicolas Pitre5c165952017-08-09 23:42:51 -040025#include "signal.h"
26
27extern const unsigned long sigreturn_codes[17];
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Russell King48be69a2013-07-24 00:29:18 +010029static unsigned long signal_return_offset;
30
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010031#ifdef CONFIG_CRUNCH
Hartley Sweeten65a50532009-08-04 23:20:45 +010032static int preserve_crunch_context(struct crunch_sigframe __user *frame)
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010033{
34 char kbuf[sizeof(*frame) + 8];
35 struct crunch_sigframe *kframe;
36
37 /* the crunch context must be 64 bit aligned */
38 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
39 kframe->magic = CRUNCH_MAGIC;
40 kframe->size = CRUNCH_STORAGE_SIZE;
41 crunch_task_copy(current_thread_info(), &kframe->storage);
42 return __copy_to_user(frame, kframe, sizeof(*frame));
43}
44
Dave Martince184a02017-06-30 18:56:59 +010045static int restore_crunch_context(char __user **auxp)
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010046{
Dave Martince184a02017-06-30 18:56:59 +010047 struct crunch_sigframe __user *frame =
48 (struct crunch_sigframe __user *)*auxp;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010049 char kbuf[sizeof(*frame) + 8];
50 struct crunch_sigframe *kframe;
51
52 /* the crunch context must be 64 bit aligned */
53 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
54 if (__copy_from_user(kframe, frame, sizeof(*frame)))
55 return -1;
56 if (kframe->magic != CRUNCH_MAGIC ||
57 kframe->size != CRUNCH_STORAGE_SIZE)
58 return -1;
Dave Martince184a02017-06-30 18:56:59 +010059 *auxp += CRUNCH_STORAGE_SIZE;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010060 crunch_task_restore(current_thread_info(), &kframe->storage);
61 return 0;
62}
63#endif
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#ifdef CONFIG_IWMMXT
66
Dave Martin26958352017-06-30 18:56:09 +010067static int preserve_iwmmxt_context(struct iwmmxt_sigframe __user *frame)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Hugh Dickins69b04752005-10-29 18:16:36 -070069 char kbuf[sizeof(*frame) + 8];
70 struct iwmmxt_sigframe *kframe;
Dave Martince184a02017-06-30 18:56:59 +010071 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 /* the iWMMXt context must be 64 bit aligned */
Hugh Dickins69b04752005-10-29 18:16:36 -070074 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
Dave Martince184a02017-06-30 18:56:59 +010075
76 if (test_thread_flag(TIF_USING_IWMMXT)) {
77 kframe->magic = IWMMXT_MAGIC;
78 kframe->size = IWMMXT_STORAGE_SIZE;
79 iwmmxt_task_copy(current_thread_info(), &kframe->storage);
80
81 err = __copy_to_user(frame, kframe, sizeof(*frame));
82 } else {
83 /*
84 * For bug-compatibility with older kernels, some space
85 * has to be reserved for iWMMXt even if it's not used.
86 * Set the magic and size appropriately so that properly
87 * written userspace can skip it reliably:
88 */
89 __put_user_error(DUMMY_MAGIC, &frame->magic, err);
90 __put_user_error(IWMMXT_STORAGE_SIZE, &frame->size, err);
91 }
92
93 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
Dave Martince184a02017-06-30 18:56:59 +010096static int restore_iwmmxt_context(char __user **auxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Dave Martince184a02017-06-30 18:56:59 +010098 struct iwmmxt_sigframe __user *frame =
99 (struct iwmmxt_sigframe __user *)*auxp;
Hugh Dickins69b04752005-10-29 18:16:36 -0700100 char kbuf[sizeof(*frame) + 8];
101 struct iwmmxt_sigframe *kframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Hugh Dickins69b04752005-10-29 18:16:36 -0700103 /* the iWMMXt context must be 64 bit aligned */
104 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
105 if (__copy_from_user(kframe, frame, sizeof(*frame)))
106 return -1;
Dave Martince184a02017-06-30 18:56:59 +0100107
108 /*
109 * For non-iWMMXt threads: a single iwmmxt_sigframe-sized dummy
110 * block is discarded for compatibility with setup_sigframe() if
111 * present, but we don't mandate its presence. If some other
112 * magic is here, it's not for us:
113 */
114 if (!test_thread_flag(TIF_USING_IWMMXT) &&
115 kframe->magic != DUMMY_MAGIC)
116 return 0;
117
118 if (kframe->size != IWMMXT_STORAGE_SIZE)
Hugh Dickins69b04752005-10-29 18:16:36 -0700119 return -1;
Dave Martince184a02017-06-30 18:56:59 +0100120
121 if (test_thread_flag(TIF_USING_IWMMXT)) {
122 if (kframe->magic != IWMMXT_MAGIC)
123 return -1;
124
125 iwmmxt_task_restore(current_thread_info(), &kframe->storage);
126 }
127
128 *auxp += IWMMXT_STORAGE_SIZE;
Hugh Dickins69b04752005-10-29 18:16:36 -0700129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132#endif
133
Imre Deak82c6f5a2010-04-11 15:58:27 +0100134#ifdef CONFIG_VFP
135
136static int preserve_vfp_context(struct vfp_sigframe __user *frame)
137{
Imre Deak82c6f5a2010-04-11 15:58:27 +0100138 const unsigned long magic = VFP_MAGIC;
139 const unsigned long size = VFP_STORAGE_SIZE;
140 int err = 0;
141
Imre Deak82c6f5a2010-04-11 15:58:27 +0100142 __put_user_error(magic, &frame->magic, err);
143 __put_user_error(size, &frame->size, err);
144
Will Deacon2498814f2012-04-23 15:38:28 +0100145 if (err)
146 return -EFAULT;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100147
Will Deacon2498814f2012-04-23 15:38:28 +0100148 return vfp_preserve_user_clear_hwstate(&frame->ufp, &frame->ufp_exc);
Imre Deak82c6f5a2010-04-11 15:58:27 +0100149}
150
Dave Martince184a02017-06-30 18:56:59 +0100151static int restore_vfp_context(char __user **auxp)
Imre Deak82c6f5a2010-04-11 15:58:27 +0100152{
Dave Martince184a02017-06-30 18:56:59 +0100153 struct vfp_sigframe __user *frame =
154 (struct vfp_sigframe __user *)*auxp;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100155 unsigned long magic;
156 unsigned long size;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100157 int err = 0;
158
159 __get_user_error(magic, &frame->magic, err);
160 __get_user_error(size, &frame->size, err);
161
162 if (err)
163 return -EFAULT;
164 if (magic != VFP_MAGIC || size != VFP_STORAGE_SIZE)
165 return -EINVAL;
166
Dave Martince184a02017-06-30 18:56:59 +0100167 *auxp += size;
Will Deacon2498814f2012-04-23 15:38:28 +0100168 return vfp_restore_user_hwstate(&frame->ufp, &frame->ufp_exc);
Imre Deak82c6f5a2010-04-11 15:58:27 +0100169}
170
171#endif
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * Do a signal return; undo the signal stack. These are aligned to 64-bit.
175 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Russell King68071482006-06-15 20:23:02 +0100177static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Russell Kingc32cd412018-07-09 10:05:22 +0100179 struct sigcontext context;
Dave Martince184a02017-06-30 18:56:59 +0100180 char __user *aux;
Russell King68071482006-06-15 20:23:02 +0100181 sigset_t set;
182 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Russell King68071482006-06-15 20:23:02 +0100184 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
Al Viro77097ae2012-04-27 13:58:59 -0400185 if (err == 0)
Matt Fleming101d9b02012-03-05 15:05:34 -0800186 set_current_blocked(&set);
Russell King68071482006-06-15 20:23:02 +0100187
Russell Kingc32cd412018-07-09 10:05:22 +0100188 err |= __copy_from_user(&context, &sf->uc.uc_mcontext, sizeof(context));
189 if (err == 0) {
190 regs->ARM_r0 = context.arm_r0;
191 regs->ARM_r1 = context.arm_r1;
192 regs->ARM_r2 = context.arm_r2;
193 regs->ARM_r3 = context.arm_r3;
194 regs->ARM_r4 = context.arm_r4;
195 regs->ARM_r5 = context.arm_r5;
196 regs->ARM_r6 = context.arm_r6;
197 regs->ARM_r7 = context.arm_r7;
198 regs->ARM_r8 = context.arm_r8;
199 regs->ARM_r9 = context.arm_r9;
200 regs->ARM_r10 = context.arm_r10;
201 regs->ARM_fp = context.arm_fp;
202 regs->ARM_ip = context.arm_ip;
203 regs->ARM_sp = context.arm_sp;
204 regs->ARM_lr = context.arm_lr;
205 regs->ARM_pc = context.arm_pc;
206 regs->ARM_cpsr = context.arm_cpsr;
207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 err |= !valid_user_regs(regs);
210
Dave Martince184a02017-06-30 18:56:59 +0100211 aux = (char __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100212#ifdef CONFIG_CRUNCH
213 if (err == 0)
Dave Martince184a02017-06-30 18:56:59 +0100214 err |= restore_crunch_context(&aux);
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100215#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216#ifdef CONFIG_IWMMXT
Dave Martince184a02017-06-30 18:56:59 +0100217 if (err == 0)
218 err |= restore_iwmmxt_context(&aux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219#endif
220#ifdef CONFIG_VFP
Imre Deak82c6f5a2010-04-11 15:58:27 +0100221 if (err == 0)
Dave Martince184a02017-06-30 18:56:59 +0100222 err |= restore_vfp_context(&aux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223#endif
224
225 return err;
226}
227
228asmlinkage int sys_sigreturn(struct pt_regs *regs)
229{
230 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800233 current->restart_block.fn = do_no_restart_syscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 /*
236 * Since we stacked the signal on a 64-bit boundary,
237 * then 'sp' should be word aligned here. If it's
238 * not, then the user is trying to mess with us.
239 */
240 if (regs->ARM_sp & 7)
241 goto badframe;
242
243 frame = (struct sigframe __user *)regs->ARM_sp;
244
245 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
246 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Russell King68071482006-06-15 20:23:02 +0100248 if (restore_sigframe(regs, frame))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 goto badframe;
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return regs->ARM_r0;
252
253badframe:
254 force_sig(SIGSEGV, current);
255 return 0;
256}
257
258asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
259{
260 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800263 current->restart_block.fn = do_no_restart_syscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 /*
266 * Since we stacked the signal on a 64-bit boundary,
267 * then 'sp' should be word aligned here. If it's
268 * not, then the user is trying to mess with us.
269 */
270 if (regs->ARM_sp & 7)
271 goto badframe;
272
273 frame = (struct rt_sigframe __user *)regs->ARM_sp;
274
275 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
276 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Russell King68071482006-06-15 20:23:02 +0100278 if (restore_sigframe(regs, &frame->sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 goto badframe;
280
Al Viroec93ac82012-12-23 01:52:54 -0500281 if (restore_altstack(&frame->sig.uc.uc_stack))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 goto badframe;
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return regs->ARM_r0;
285
286badframe:
287 force_sig(SIGSEGV, current);
288 return 0;
289}
290
291static int
Russell Kingaca6ca12006-06-15 20:28:03 +0100292setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs, sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100294 struct aux_sigframe __user *aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 int err = 0;
296
Russell Kingaca6ca12006-06-15 20:28:03 +0100297 __put_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
298 __put_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
299 __put_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
300 __put_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
301 __put_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
302 __put_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
303 __put_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
304 __put_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
305 __put_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
306 __put_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
307 __put_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
308 __put_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
309 __put_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
310 __put_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
311 __put_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
312 __put_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
313 __put_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Russell Kingaca6ca12006-06-15 20:28:03 +0100315 __put_user_error(current->thread.trap_no, &sf->uc.uc_mcontext.trap_no, err);
316 __put_user_error(current->thread.error_code, &sf->uc.uc_mcontext.error_code, err);
317 __put_user_error(current->thread.address, &sf->uc.uc_mcontext.fault_address, err);
318 __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Russell Kingaca6ca12006-06-15 20:28:03 +0100320 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100322 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100323#ifdef CONFIG_CRUNCH
324 if (err == 0)
325 err |= preserve_crunch_context(&aux->crunch);
326#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327#ifdef CONFIG_IWMMXT
Dave Martince184a02017-06-30 18:56:59 +0100328 if (err == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 err |= preserve_iwmmxt_context(&aux->iwmmxt);
330#endif
331#ifdef CONFIG_VFP
Imre Deak82c6f5a2010-04-11 15:58:27 +0100332 if (err == 0)
333 err |= preserve_vfp_context(&aux->vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334#endif
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100335 __put_user_error(0, &aux->end_magic, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 return err;
338}
339
340static inline void __user *
Al Viro7e243642012-11-07 17:53:13 -0500341get_sigframe(struct ksignal *ksig, struct pt_regs *regs, int framesize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Al Viro7e243642012-11-07 17:53:13 -0500343 unsigned long sp = sigsp(regs->ARM_sp, ksig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 void __user *frame;
345
346 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 * ATPCS B01 mandates 8-byte alignment
348 */
349 frame = (void __user *)((sp - framesize) & ~7);
350
351 /*
352 * Check that we can actually write to the signal frame.
353 */
354 if (!access_ok(VERIFY_WRITE, frame, framesize))
355 frame = NULL;
356
357 return frame;
358}
359
Al Viro7e243642012-11-07 17:53:13 -0500360static int
361setup_return(struct pt_regs *regs, struct ksignal *ksig,
362 unsigned long __user *rc, void __user *frame)
363{
364 unsigned long handler = (unsigned long)ksig->ka.sa.sa_handler;
Nicolas Pitre5c165952017-08-09 23:42:51 -0400365 unsigned long handler_fdpic_GOT = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 unsigned long retcode;
Nicolas Pitre5c165952017-08-09 23:42:51 -0400367 unsigned int idx, thumb = 0;
Russell King53399052011-02-20 12:22:52 +0000368 unsigned long cpsr = regs->ARM_cpsr & ~(PSR_f | PSR_E_BIT);
Nicolas Pitre5c165952017-08-09 23:42:51 -0400369 bool fdpic = IS_ENABLED(CONFIG_BINFMT_ELF_FDPIC) &&
370 (current->personality & FDPIC_FUNCPTRS);
371
372 if (fdpic) {
373 unsigned long __user *fdpic_func_desc =
374 (unsigned long __user *)handler;
375 if (__get_user(handler, &fdpic_func_desc[0]) ||
376 __get_user(handler_fdpic_GOT, &fdpic_func_desc[1]))
377 return 1;
378 }
Russell King53399052011-02-20 12:22:52 +0000379
380 cpsr |= PSR_ENDSTATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 /*
383 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
384 */
Al Viro7e243642012-11-07 17:53:13 -0500385 if (ksig->ka.sa.sa_flags & SA_THIRTYTWO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
387
388#ifdef CONFIG_ARM_THUMB
389 if (elf_hwcap & HWCAP_THUMB) {
390 /*
391 * The LSB of the handler determines if we're going to
392 * be using THUMB or ARM mode for this signal handler.
393 */
394 thumb = handler & 1;
395
T.J. Purtell6ecf8302013-11-06 18:38:05 +0100396 /*
Russell King9b556132015-09-11 16:44:02 +0100397 * Clear the If-Then Thumb-2 execution state. ARM spec
398 * requires this to be all 000s in ARM mode. Snapdragon
399 * S4/Krait misbehaves on a Thumb=>ARM signal transition
400 * without this.
401 *
402 * We must do this whenever we are running on a Thumb-2
403 * capable CPU, which includes ARMv6T2. However, we elect
Russell King12fc7302015-09-16 11:08:49 +0100404 * to always do this to simplify the code; this field is
405 * marked UNK/SBZP for older architectures.
T.J. Purtell6ecf8302013-11-06 18:38:05 +0100406 */
407 cpsr &= ~PSR_IT_MASK;
T.J. Purtell6ecf8302013-11-06 18:38:05 +0100408
Catalin Marinasd71e1352009-05-30 14:00:15 +0100409 if (thumb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 cpsr |= PSR_T_BIT;
Catalin Marinasd71e1352009-05-30 14:00:15 +0100411 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 cpsr &= ~PSR_T_BIT;
413 }
414#endif
415
Al Viro7e243642012-11-07 17:53:13 -0500416 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
417 retcode = (unsigned long)ksig->ka.sa.sa_restorer;
Nicolas Pitre5c165952017-08-09 23:42:51 -0400418 if (fdpic) {
419 /*
420 * We need code to load the function descriptor.
421 * That code follows the standard sigreturn code
422 * (6 words), and is made of 3 + 2 words for each
423 * variant. The 4th copied word is the actual FD
424 * address that the assembly code expects.
425 */
426 idx = 6 + thumb * 3;
427 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
428 idx += 5;
429 if (__put_user(sigreturn_codes[idx], rc ) ||
430 __put_user(sigreturn_codes[idx+1], rc+1) ||
431 __put_user(sigreturn_codes[idx+2], rc+2) ||
432 __put_user(retcode, rc+3))
433 return 1;
434 goto rc_finish;
435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 } else {
Nicolas Pitre5c165952017-08-09 23:42:51 -0400437 idx = thumb << 1;
Al Viro7e243642012-11-07 17:53:13 -0500438 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000439 idx += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Jonathan Austin9dfc28b2013-04-18 18:37:24 +0100441 /*
442 * Put the sigreturn code on the stack no matter which return
443 * mechanism we use in order to remain ABI compliant
444 */
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000445 if (__put_user(sigreturn_codes[idx], rc) ||
446 __put_user(sigreturn_codes[idx+1], rc+1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return 1;
448
Nicolas Pitre5c165952017-08-09 23:42:51 -0400449rc_finish:
Russell King8c0cc8a2013-08-03 10:39:51 +0100450#ifdef CONFIG_MMU
451 if (cpsr & MODE32_BIT) {
Russell King48be69a2013-07-24 00:29:18 +0100452 struct mm_struct *mm = current->mm;
453
Russell Kinge00d3492005-06-22 20:26:05 +0100454 /*
Russell King48be69a2013-07-24 00:29:18 +0100455 * 32-bit code can use the signal return page
456 * except when the MPU has protected the vectors
457 * page from PL0
Russell Kinge00d3492005-06-22 20:26:05 +0100458 */
Russell King48be69a2013-07-24 00:29:18 +0100459 retcode = mm->context.sigpage + signal_return_offset +
460 (idx << 2) + thumb;
Russell King8c0cc8a2013-08-03 10:39:51 +0100461 } else
462#endif
463 {
Russell Kinge00d3492005-06-22 20:26:05 +0100464 /*
465 * Ensure that the instruction cache sees
466 * the return code written onto the stack.
467 */
468 flush_icache_range((unsigned long)rc,
Nicolas Pitre5c165952017-08-09 23:42:51 -0400469 (unsigned long)(rc + 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Russell Kinge00d3492005-06-22 20:26:05 +0100471 retcode = ((unsigned long)rc) + thumb;
472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474
Richard Weinbergera4980442014-07-13 15:24:03 +0200475 regs->ARM_r0 = ksig->sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 regs->ARM_sp = (unsigned long)frame;
477 regs->ARM_lr = retcode;
478 regs->ARM_pc = handler;
Nicolas Pitre5c165952017-08-09 23:42:51 -0400479 if (fdpic)
480 regs->ARM_r9 = handler_fdpic_GOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 regs->ARM_cpsr = cpsr;
482
483 return 0;
484}
485
486static int
Al Viro7e243642012-11-07 17:53:13 -0500487setup_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Al Viro7e243642012-11-07 17:53:13 -0500489 struct sigframe __user *frame = get_sigframe(ksig, regs, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 int err = 0;
491
492 if (!frame)
493 return 1;
494
Russell Kingca195cf2006-06-24 22:41:09 +0100495 /*
496 * Set uc.uc_flags to a value which sc.trap_no would never have.
497 */
498 __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Russell Kingaca6ca12006-06-15 20:28:03 +0100500 err |= setup_sigframe(frame, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (err == 0)
Al Viro7e243642012-11-07 17:53:13 -0500502 err = setup_return(regs, ksig, frame->retcode, frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 return err;
505}
506
507static int
Al Viro7e243642012-11-07 17:53:13 -0500508setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Al Viro7e243642012-11-07 17:53:13 -0500510 struct rt_sigframe __user *frame = get_sigframe(ksig, regs, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 int err = 0;
512
513 if (!frame)
514 return 1;
515
Al Viro7e243642012-11-07 17:53:13 -0500516 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Russell Kingcb3504e2006-06-15 20:18:25 +0100518 __put_user_error(0, &frame->sig.uc.uc_flags, err);
519 __put_user_error(NULL, &frame->sig.uc.uc_link, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Al Viroec93ac82012-12-23 01:52:54 -0500521 err |= __save_altstack(&frame->sig.uc.uc_stack, regs->ARM_sp);
Russell Kingaca6ca12006-06-15 20:28:03 +0100522 err |= setup_sigframe(&frame->sig, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (err == 0)
Al Viro7e243642012-11-07 17:53:13 -0500524 err = setup_return(regs, ksig, frame->sig.retcode, frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 if (err == 0) {
527 /*
528 * For realtime signals we must also set the second and third
529 * arguments for the signal handler.
530 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
531 */
532 regs->ARM_r1 = (unsigned long)&frame->info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100533 regs->ARM_r2 = (unsigned long)&frame->sig.uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535
536 return err;
537}
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539/*
540 * OK, we're invoking a handler
541 */
Al Viro7e243642012-11-07 17:53:13 -0500542static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Al Virob7f9a112012-05-02 09:59:21 -0400544 sigset_t *oldset = sigmask_to_save();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 int ret;
546
547 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 * Set up the stack frame
549 */
Al Viro7e243642012-11-07 17:53:13 -0500550 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
551 ret = setup_rt_frame(ksig, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 else
Al Viro7e243642012-11-07 17:53:13 -0500553 ret = setup_frame(ksig, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 /*
556 * Check that the resulting registers are actually sane.
557 */
558 ret |= !valid_user_regs(regs);
559
Al Viro7e243642012-11-07 17:53:13 -0500560 signal_setup_done(ret, ksig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
563/*
564 * Note that 'init' is a special process: it doesn't get signals it doesn't
565 * want to handle. Thus you cannot kill init even with a SIGKILL even by
566 * mistake.
567 *
568 * Note that we go through the signals twice: once to check the signals that
569 * the kernel can handle, and then we build all the user-level signal handling
570 * stack-frames in one go after that.
571 */
Al Viro81783782012-07-19 17:48:21 +0100572static int do_signal(struct pt_regs *regs, int syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100574 unsigned int retval = 0, continue_addr = 0, restart_addr = 0;
Al Viro7e243642012-11-07 17:53:13 -0500575 struct ksignal ksig;
Al Viro81783782012-07-19 17:48:21 +0100576 int restart = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 /*
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100579 * If we were from a system call, check for system call restarting...
580 */
581 if (syscall) {
582 continue_addr = regs->ARM_pc;
583 restart_addr = continue_addr - (thumb_mode(regs) ? 2 : 4);
584 retval = regs->ARM_r0;
585
586 /*
587 * Prepare for system call restart. We do this here so that a
588 * debugger will see the already changed PSW.
589 */
590 switch (retval) {
Al Viro81783782012-07-19 17:48:21 +0100591 case -ERESTART_RESTARTBLOCK:
Al Viro66285212012-07-19 17:48:50 +0100592 restart -= 2;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100593 case -ERESTARTNOHAND:
594 case -ERESTARTSYS:
595 case -ERESTARTNOINTR:
Al Viro81783782012-07-19 17:48:21 +0100596 restart++;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100597 regs->ARM_r0 = regs->ARM_ORIG_r0;
598 regs->ARM_pc = restart_addr;
599 break;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100600 }
601 }
602
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100603 /*
604 * Get the signal to deliver. When running under ptrace, at this
605 * point the debugger may change all our registers ...
606 */
Al Viro81783782012-07-19 17:48:21 +0100607 /*
608 * Depending on the signal settings we may need to revert the
609 * decision to restart the system call. But skip this if a
610 * debugger has chosen to restart at a different PC.
611 */
Al Viro7e243642012-11-07 17:53:13 -0500612 if (get_signal(&ksig)) {
613 /* handler */
614 if (unlikely(restart) && regs->ARM_pc == restart_addr) {
Will Deaconad82cc02012-07-19 17:46:44 +0100615 if (retval == -ERESTARTNOHAND ||
616 retval == -ERESTART_RESTARTBLOCK
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100617 || (retval == -ERESTARTSYS
Al Viro7e243642012-11-07 17:53:13 -0500618 && !(ksig.ka.sa.sa_flags & SA_RESTART))) {
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100619 regs->ARM_r0 = -EINTR;
620 regs->ARM_pc = continue_addr;
621 }
622 }
Al Viro7e243642012-11-07 17:53:13 -0500623 handle_signal(&ksig, regs);
624 } else {
625 /* no handler */
626 restore_saved_sigmask();
627 if (unlikely(restart) && regs->ARM_pc == restart_addr) {
628 regs->ARM_pc = continue_addr;
629 return restart;
630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
Al Viro7e243642012-11-07 17:53:13 -0500632 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
Al Viro81783782012-07-19 17:48:21 +0100635asmlinkage int
Al Viro0a267fa2012-07-19 17:47:55 +0100636do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Russell King3302cad2015-08-20 16:13:37 +0100638 /*
639 * The assembly code enters us with IRQs off, but it hasn't
640 * informed the tracing code of that for efficiency reasons.
641 * Update the trace code with the current status.
642 */
643 trace_hardirqs_off();
Al Viro0a267fa2012-07-19 17:47:55 +0100644 do {
645 if (likely(thread_flags & _TIF_NEED_RESCHED)) {
646 schedule();
647 } else {
648 if (unlikely(!user_mode(regs)))
Al Viro81783782012-07-19 17:48:21 +0100649 return 0;
Al Viro0a267fa2012-07-19 17:47:55 +0100650 local_irq_enable();
651 if (thread_flags & _TIF_SIGPENDING) {
Al Viro66285212012-07-19 17:48:50 +0100652 int restart = do_signal(regs, syscall);
653 if (unlikely(restart)) {
Al Viro81783782012-07-19 17:48:21 +0100654 /*
655 * Restart without handlers.
656 * Deal with it without leaving
657 * the kernel space.
658 */
Al Viro66285212012-07-19 17:48:50 +0100659 return restart;
Al Viro81783782012-07-19 17:48:21 +0100660 }
Al Viro0a267fa2012-07-19 17:47:55 +0100661 syscall = 0;
David A. Longc7edc9e2014-03-07 11:23:04 -0500662 } else if (thread_flags & _TIF_UPROBE) {
David A. Longc7edc9e2014-03-07 11:23:04 -0500663 uprobe_notify_resume(regs);
Al Viro0a267fa2012-07-19 17:47:55 +0100664 } else {
665 clear_thread_flag(TIF_NOTIFY_RESUME);
666 tracehook_notify_resume(regs);
667 }
668 }
669 local_irq_disable();
670 thread_flags = current_thread_info()->flags;
671 } while (thread_flags & _TIF_WORK_MASK);
Al Viro81783782012-07-19 17:48:21 +0100672 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
Russell King48be69a2013-07-24 00:29:18 +0100674
Russell King48be69a2013-07-24 00:29:18 +0100675struct page *get_signal_page(void)
676{
Russell Kinge0d40752013-08-03 10:30:05 +0100677 unsigned long ptr;
678 unsigned offset;
679 struct page *page;
680 void *addr;
Russell King48be69a2013-07-24 00:29:18 +0100681
Russell Kinge0d40752013-08-03 10:30:05 +0100682 page = alloc_pages(GFP_KERNEL, 0);
Russell King48be69a2013-07-24 00:29:18 +0100683
Russell Kinge0d40752013-08-03 10:30:05 +0100684 if (!page)
685 return NULL;
Russell King48be69a2013-07-24 00:29:18 +0100686
Russell Kinge0d40752013-08-03 10:30:05 +0100687 addr = page_address(page);
Russell King48be69a2013-07-24 00:29:18 +0100688
Russell Kinge0d40752013-08-03 10:30:05 +0100689 /* Give the signal return code some randomness */
690 offset = 0x200 + (get_random_int() & 0x7fc);
691 signal_return_offset = offset;
Russell King48be69a2013-07-24 00:29:18 +0100692
Russell Kinge0d40752013-08-03 10:30:05 +0100693 /*
694 * Copy signal return handlers into the vector page, and
695 * set sigreturn to be a pointer to these.
696 */
697 memcpy(addr + offset, sigreturn_codes, sizeof(sigreturn_codes));
Russell King48be69a2013-07-24 00:29:18 +0100698
Russell Kinge0d40752013-08-03 10:30:05 +0100699 ptr = (unsigned long)addr + offset;
700 flush_icache_range(ptr, ptr + sizeof(sigreturn_codes));
Russell King48be69a2013-07-24 00:29:18 +0100701
Russell Kinge0d40752013-08-03 10:30:05 +0100702 return page;
Russell King48be69a2013-07-24 00:29:18 +0100703}
Thomas Garniere33f8d322017-09-07 08:30:46 -0700704
705/* Defer to generic check */
706asmlinkage void addr_limit_check_failed(void)
707{
708 addr_limit_user_check();
709}