blob: be5edfdde558d600494c6720850a7ed0c0e0b153 [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);
Dave Martince184a02017-06-30 18:56:59 +010080 } 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 */
Julien Thierry73839792018-09-11 10:12:05 +010087 *kframe = (struct iwmmxt_sigframe) {
88 .magic = DUMMY_MAGIC,
89 .size = IWMMXT_STORAGE_SIZE,
90 };
Dave Martince184a02017-06-30 18:56:59 +010091 }
92
Julien Thierry73839792018-09-11 10:12:05 +010093 err = __copy_to_user(frame, kframe, sizeof(*kframe));
94
Dave Martince184a02017-06-30 18:56:59 +010095 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Dave Martince184a02017-06-30 18:56:59 +010098static int restore_iwmmxt_context(char __user **auxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Dave Martince184a02017-06-30 18:56:59 +0100100 struct iwmmxt_sigframe __user *frame =
101 (struct iwmmxt_sigframe __user *)*auxp;
Hugh Dickins69b04752005-10-29 18:16:36 -0700102 char kbuf[sizeof(*frame) + 8];
103 struct iwmmxt_sigframe *kframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Hugh Dickins69b04752005-10-29 18:16:36 -0700105 /* the iWMMXt context must be 64 bit aligned */
106 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
107 if (__copy_from_user(kframe, frame, sizeof(*frame)))
108 return -1;
Dave Martince184a02017-06-30 18:56:59 +0100109
110 /*
111 * For non-iWMMXt threads: a single iwmmxt_sigframe-sized dummy
112 * block is discarded for compatibility with setup_sigframe() if
113 * present, but we don't mandate its presence. If some other
114 * magic is here, it's not for us:
115 */
116 if (!test_thread_flag(TIF_USING_IWMMXT) &&
117 kframe->magic != DUMMY_MAGIC)
118 return 0;
119
120 if (kframe->size != IWMMXT_STORAGE_SIZE)
Hugh Dickins69b04752005-10-29 18:16:36 -0700121 return -1;
Dave Martince184a02017-06-30 18:56:59 +0100122
123 if (test_thread_flag(TIF_USING_IWMMXT)) {
124 if (kframe->magic != IWMMXT_MAGIC)
125 return -1;
126
127 iwmmxt_task_restore(current_thread_info(), &kframe->storage);
128 }
129
130 *auxp += IWMMXT_STORAGE_SIZE;
Hugh Dickins69b04752005-10-29 18:16:36 -0700131 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134#endif
135
Imre Deak82c6f5a2010-04-11 15:58:27 +0100136#ifdef CONFIG_VFP
137
138static int preserve_vfp_context(struct vfp_sigframe __user *frame)
139{
Julien Thierry3aa2df62018-09-11 10:12:18 +0100140 struct vfp_sigframe kframe;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100141 int err = 0;
142
Julien Thierry3aa2df62018-09-11 10:12:18 +0100143 memset(&kframe, 0, sizeof(kframe));
144 kframe.magic = VFP_MAGIC;
145 kframe.size = VFP_STORAGE_SIZE;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100146
Julien Thierry3aa2df62018-09-11 10:12:18 +0100147 err = vfp_preserve_user_clear_hwstate(&kframe.ufp, &kframe.ufp_exc);
Will Deacon24988142012-04-23 15:38:28 +0100148 if (err)
Julien Thierry3aa2df62018-09-11 10:12:18 +0100149 return err;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100150
Julien Thierry3aa2df62018-09-11 10:12:18 +0100151 return __copy_to_user(frame, &kframe, sizeof(kframe));
Imre Deak82c6f5a2010-04-11 15:58:27 +0100152}
153
Dave Martince184a02017-06-30 18:56:59 +0100154static int restore_vfp_context(char __user **auxp)
Imre Deak82c6f5a2010-04-11 15:58:27 +0100155{
Russell King42019fc2018-07-09 10:13:36 +0100156 struct vfp_sigframe frame;
157 int err;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100158
Russell King42019fc2018-07-09 10:13:36 +0100159 err = __copy_from_user(&frame, *auxp, sizeof(frame));
Imre Deak82c6f5a2010-04-11 15:58:27 +0100160 if (err)
Russell King42019fc2018-07-09 10:13:36 +0100161 return err;
162
163 if (frame.magic != VFP_MAGIC || frame.size != VFP_STORAGE_SIZE)
Imre Deak82c6f5a2010-04-11 15:58:27 +0100164 return -EINVAL;
165
Russell King42019fc2018-07-09 10:13:36 +0100166 *auxp += sizeof(frame);
167 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{
Russell Kingc32cd412018-07-09 10:05:22 +0100178 struct sigcontext context;
Dave Martince184a02017-06-30 18:56:59 +0100179 char __user *aux;
Russell King68071482006-06-15 20:23:02 +0100180 sigset_t set;
181 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Russell King68071482006-06-15 20:23:02 +0100183 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
Al Viro77097ae2012-04-27 13:58:59 -0400184 if (err == 0)
Matt Fleming101d9b02012-03-05 15:05:34 -0800185 set_current_blocked(&set);
Russell King68071482006-06-15 20:23:02 +0100186
Russell Kingc32cd412018-07-09 10:05:22 +0100187 err |= __copy_from_user(&context, &sf->uc.uc_mcontext, sizeof(context));
188 if (err == 0) {
189 regs->ARM_r0 = context.arm_r0;
190 regs->ARM_r1 = context.arm_r1;
191 regs->ARM_r2 = context.arm_r2;
192 regs->ARM_r3 = context.arm_r3;
193 regs->ARM_r4 = context.arm_r4;
194 regs->ARM_r5 = context.arm_r5;
195 regs->ARM_r6 = context.arm_r6;
196 regs->ARM_r7 = context.arm_r7;
197 regs->ARM_r8 = context.arm_r8;
198 regs->ARM_r9 = context.arm_r9;
199 regs->ARM_r10 = context.arm_r10;
200 regs->ARM_fp = context.arm_fp;
201 regs->ARM_ip = context.arm_ip;
202 regs->ARM_sp = context.arm_sp;
203 regs->ARM_lr = context.arm_lr;
204 regs->ARM_pc = context.arm_pc;
205 regs->ARM_cpsr = context.arm_cpsr;
206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 err |= !valid_user_regs(regs);
209
Dave Martince184a02017-06-30 18:56:59 +0100210 aux = (char __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100211#ifdef CONFIG_CRUNCH
212 if (err == 0)
Dave Martince184a02017-06-30 18:56:59 +0100213 err |= restore_crunch_context(&aux);
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100214#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215#ifdef CONFIG_IWMMXT
Dave Martince184a02017-06-30 18:56:59 +0100216 if (err == 0)
217 err |= restore_iwmmxt_context(&aux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218#endif
219#ifdef CONFIG_VFP
Imre Deak82c6f5a2010-04-11 15:58:27 +0100220 if (err == 0)
Dave Martince184a02017-06-30 18:56:59 +0100221 err |= restore_vfp_context(&aux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222#endif
223
224 return err;
225}
226
227asmlinkage int sys_sigreturn(struct pt_regs *regs)
228{
229 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800232 current->restart_block.fn = do_no_restart_syscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 /*
235 * Since we stacked the signal on a 64-bit boundary,
236 * then 'sp' should be word aligned here. If it's
237 * not, then the user is trying to mess with us.
238 */
239 if (regs->ARM_sp & 7)
240 goto badframe;
241
242 frame = (struct sigframe __user *)regs->ARM_sp;
243
Linus Torvalds96d4f262019-01-03 18:57:57 -0800244 if (!access_ok(frame, sizeof (*frame)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Russell King68071482006-06-15 20:23:02 +0100247 if (restore_sigframe(regs, frame))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 goto badframe;
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return regs->ARM_r0;
251
252badframe:
253 force_sig(SIGSEGV, current);
254 return 0;
255}
256
257asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
258{
259 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800262 current->restart_block.fn = do_no_restart_syscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 /*
265 * Since we stacked the signal on a 64-bit boundary,
266 * then 'sp' should be word aligned here. If it's
267 * not, then the user is trying to mess with us.
268 */
269 if (regs->ARM_sp & 7)
270 goto badframe;
271
272 frame = (struct rt_sigframe __user *)regs->ARM_sp;
273
Linus Torvalds96d4f262019-01-03 18:57:57 -0800274 if (!access_ok(frame, sizeof (*frame)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Russell King68071482006-06-15 20:23:02 +0100277 if (restore_sigframe(regs, &frame->sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 goto badframe;
279
Al Viroec93ac82012-12-23 01:52:54 -0500280 if (restore_altstack(&frame->sig.uc.uc_stack))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 goto badframe;
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return regs->ARM_r0;
284
285badframe:
286 force_sig(SIGSEGV, current);
287 return 0;
288}
289
290static int
Russell Kingaca6ca12006-06-15 20:28:03 +0100291setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs, sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100293 struct aux_sigframe __user *aux;
Julien Thierry5ca451c2018-09-11 10:11:06 +0100294 struct sigcontext context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 int err = 0;
296
Julien Thierry5ca451c2018-09-11 10:11:06 +0100297 context = (struct sigcontext) {
298 .arm_r0 = regs->ARM_r0,
299 .arm_r1 = regs->ARM_r1,
300 .arm_r2 = regs->ARM_r2,
301 .arm_r3 = regs->ARM_r3,
302 .arm_r4 = regs->ARM_r4,
303 .arm_r5 = regs->ARM_r5,
304 .arm_r6 = regs->ARM_r6,
305 .arm_r7 = regs->ARM_r7,
306 .arm_r8 = regs->ARM_r8,
307 .arm_r9 = regs->ARM_r9,
308 .arm_r10 = regs->ARM_r10,
309 .arm_fp = regs->ARM_fp,
310 .arm_ip = regs->ARM_ip,
311 .arm_sp = regs->ARM_sp,
312 .arm_lr = regs->ARM_lr,
313 .arm_pc = regs->ARM_pc,
314 .arm_cpsr = regs->ARM_cpsr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Julien Thierry5ca451c2018-09-11 10:11:06 +0100316 .trap_no = current->thread.trap_no,
317 .error_code = current->thread.error_code,
318 .fault_address = current->thread.address,
319 .oldmask = set->sig[0],
320 };
321
322 err |= __copy_to_user(&sf->uc.uc_mcontext, &context, sizeof(context));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Russell Kingaca6ca12006-06-15 20:28:03 +0100324 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100326 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100327#ifdef CONFIG_CRUNCH
328 if (err == 0)
329 err |= preserve_crunch_context(&aux->crunch);
330#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331#ifdef CONFIG_IWMMXT
Dave Martince184a02017-06-30 18:56:59 +0100332 if (err == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 err |= preserve_iwmmxt_context(&aux->iwmmxt);
334#endif
335#ifdef CONFIG_VFP
Imre Deak82c6f5a2010-04-11 15:58:27 +0100336 if (err == 0)
337 err |= preserve_vfp_context(&aux->vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338#endif
Julien Thierry18ea66b2018-09-11 10:13:11 +0100339 err |= __put_user(0, &aux->end_magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 return err;
342}
343
344static inline void __user *
Al Viro7e243642012-11-07 17:53:13 -0500345get_sigframe(struct ksignal *ksig, struct pt_regs *regs, int framesize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Al Viro7e243642012-11-07 17:53:13 -0500347 unsigned long sp = sigsp(regs->ARM_sp, ksig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 void __user *frame;
349
350 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 * ATPCS B01 mandates 8-byte alignment
352 */
353 frame = (void __user *)((sp - framesize) & ~7);
354
355 /*
356 * Check that we can actually write to the signal frame.
357 */
Linus Torvalds96d4f262019-01-03 18:57:57 -0800358 if (!access_ok(frame, framesize))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 frame = NULL;
360
361 return frame;
362}
363
Al Viro7e243642012-11-07 17:53:13 -0500364static int
365setup_return(struct pt_regs *regs, struct ksignal *ksig,
366 unsigned long __user *rc, void __user *frame)
367{
368 unsigned long handler = (unsigned long)ksig->ka.sa.sa_handler;
Nicolas Pitre5c165952017-08-09 23:42:51 -0400369 unsigned long handler_fdpic_GOT = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 unsigned long retcode;
Nicolas Pitre5c165952017-08-09 23:42:51 -0400371 unsigned int idx, thumb = 0;
Russell King53399052011-02-20 12:22:52 +0000372 unsigned long cpsr = regs->ARM_cpsr & ~(PSR_f | PSR_E_BIT);
Nicolas Pitre5c165952017-08-09 23:42:51 -0400373 bool fdpic = IS_ENABLED(CONFIG_BINFMT_ELF_FDPIC) &&
374 (current->personality & FDPIC_FUNCPTRS);
375
376 if (fdpic) {
377 unsigned long __user *fdpic_func_desc =
378 (unsigned long __user *)handler;
379 if (__get_user(handler, &fdpic_func_desc[0]) ||
380 __get_user(handler_fdpic_GOT, &fdpic_func_desc[1]))
381 return 1;
382 }
Russell King53399052011-02-20 12:22:52 +0000383
384 cpsr |= PSR_ENDSTATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 /*
387 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
388 */
Al Viro7e243642012-11-07 17:53:13 -0500389 if (ksig->ka.sa.sa_flags & SA_THIRTYTWO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
391
392#ifdef CONFIG_ARM_THUMB
393 if (elf_hwcap & HWCAP_THUMB) {
394 /*
395 * The LSB of the handler determines if we're going to
396 * be using THUMB or ARM mode for this signal handler.
397 */
398 thumb = handler & 1;
399
T.J. Purtell6ecf8302013-11-06 18:38:05 +0100400 /*
Russell King9b556132015-09-11 16:44:02 +0100401 * Clear the If-Then Thumb-2 execution state. ARM spec
402 * requires this to be all 000s in ARM mode. Snapdragon
403 * S4/Krait misbehaves on a Thumb=>ARM signal transition
404 * without this.
405 *
406 * We must do this whenever we are running on a Thumb-2
407 * capable CPU, which includes ARMv6T2. However, we elect
Russell King12fc7302015-09-16 11:08:49 +0100408 * to always do this to simplify the code; this field is
409 * marked UNK/SBZP for older architectures.
T.J. Purtell6ecf8302013-11-06 18:38:05 +0100410 */
411 cpsr &= ~PSR_IT_MASK;
T.J. Purtell6ecf8302013-11-06 18:38:05 +0100412
Catalin Marinasd71e1352009-05-30 14:00:15 +0100413 if (thumb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 cpsr |= PSR_T_BIT;
Catalin Marinasd71e1352009-05-30 14:00:15 +0100415 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 cpsr &= ~PSR_T_BIT;
417 }
418#endif
419
Al Viro7e243642012-11-07 17:53:13 -0500420 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
421 retcode = (unsigned long)ksig->ka.sa.sa_restorer;
Nicolas Pitre5c165952017-08-09 23:42:51 -0400422 if (fdpic) {
423 /*
424 * We need code to load the function descriptor.
425 * That code follows the standard sigreturn code
426 * (6 words), and is made of 3 + 2 words for each
427 * variant. The 4th copied word is the actual FD
428 * address that the assembly code expects.
429 */
430 idx = 6 + thumb * 3;
431 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
432 idx += 5;
433 if (__put_user(sigreturn_codes[idx], rc ) ||
434 __put_user(sigreturn_codes[idx+1], rc+1) ||
435 __put_user(sigreturn_codes[idx+2], rc+2) ||
436 __put_user(retcode, rc+3))
437 return 1;
438 goto rc_finish;
439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 } else {
Nicolas Pitre5c165952017-08-09 23:42:51 -0400441 idx = thumb << 1;
Al Viro7e243642012-11-07 17:53:13 -0500442 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000443 idx += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Jonathan Austin9dfc28b2013-04-18 18:37:24 +0100445 /*
446 * Put the sigreturn code on the stack no matter which return
447 * mechanism we use in order to remain ABI compliant
448 */
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000449 if (__put_user(sigreturn_codes[idx], rc) ||
450 __put_user(sigreturn_codes[idx+1], rc+1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return 1;
452
Nicolas Pitre5c165952017-08-09 23:42:51 -0400453rc_finish:
Russell King8c0cc8a2013-08-03 10:39:51 +0100454#ifdef CONFIG_MMU
455 if (cpsr & MODE32_BIT) {
Russell King48be69a2013-07-24 00:29:18 +0100456 struct mm_struct *mm = current->mm;
457
Russell Kinge00d3492005-06-22 20:26:05 +0100458 /*
Russell King48be69a2013-07-24 00:29:18 +0100459 * 32-bit code can use the signal return page
460 * except when the MPU has protected the vectors
461 * page from PL0
Russell Kinge00d3492005-06-22 20:26:05 +0100462 */
Russell King48be69a2013-07-24 00:29:18 +0100463 retcode = mm->context.sigpage + signal_return_offset +
464 (idx << 2) + thumb;
Russell King8c0cc8a2013-08-03 10:39:51 +0100465 } else
466#endif
467 {
Russell Kinge00d3492005-06-22 20:26:05 +0100468 /*
469 * Ensure that the instruction cache sees
470 * the return code written onto the stack.
471 */
472 flush_icache_range((unsigned long)rc,
Nicolas Pitre5c165952017-08-09 23:42:51 -0400473 (unsigned long)(rc + 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Russell Kinge00d3492005-06-22 20:26:05 +0100475 retcode = ((unsigned long)rc) + thumb;
476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478
Richard Weinbergera4980442014-07-13 15:24:03 +0200479 regs->ARM_r0 = ksig->sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 regs->ARM_sp = (unsigned long)frame;
481 regs->ARM_lr = retcode;
482 regs->ARM_pc = handler;
Nicolas Pitre5c165952017-08-09 23:42:51 -0400483 if (fdpic)
484 regs->ARM_r9 = handler_fdpic_GOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 regs->ARM_cpsr = cpsr;
486
487 return 0;
488}
489
490static int
Al Viro7e243642012-11-07 17:53:13 -0500491setup_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Al Viro7e243642012-11-07 17:53:13 -0500493 struct sigframe __user *frame = get_sigframe(ksig, regs, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 int err = 0;
495
496 if (!frame)
497 return 1;
498
Russell Kingca195cf2006-06-24 22:41:09 +0100499 /*
500 * Set uc.uc_flags to a value which sc.trap_no would never have.
501 */
Julien Thierry18ea66b2018-09-11 10:13:11 +0100502 err = __put_user(0x5ac3c35a, &frame->uc.uc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Russell Kingaca6ca12006-06-15 20:28:03 +0100504 err |= setup_sigframe(frame, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (err == 0)
Al Viro7e243642012-11-07 17:53:13 -0500506 err = setup_return(regs, ksig, frame->retcode, frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 return err;
509}
510
511static int
Al Viro7e243642012-11-07 17:53:13 -0500512setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Al Viro7e243642012-11-07 17:53:13 -0500514 struct rt_sigframe __user *frame = get_sigframe(ksig, regs, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 int err = 0;
516
517 if (!frame)
518 return 1;
519
Al Viro7e243642012-11-07 17:53:13 -0500520 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Julien Thierry18ea66b2018-09-11 10:13:11 +0100522 err |= __put_user(0, &frame->sig.uc.uc_flags);
523 err |= __put_user(NULL, &frame->sig.uc.uc_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Al Viroec93ac82012-12-23 01:52:54 -0500525 err |= __save_altstack(&frame->sig.uc.uc_stack, regs->ARM_sp);
Russell Kingaca6ca12006-06-15 20:28:03 +0100526 err |= setup_sigframe(&frame->sig, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (err == 0)
Al Viro7e243642012-11-07 17:53:13 -0500528 err = setup_return(regs, ksig, frame->sig.retcode, frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 if (err == 0) {
531 /*
532 * For realtime signals we must also set the second and third
533 * arguments for the signal handler.
534 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
535 */
536 regs->ARM_r1 = (unsigned long)&frame->info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100537 regs->ARM_r2 = (unsigned long)&frame->sig.uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539
540 return err;
541}
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543/*
544 * OK, we're invoking a handler
545 */
Al Viro7e243642012-11-07 17:53:13 -0500546static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Al Virob7f9a112012-05-02 09:59:21 -0400548 sigset_t *oldset = sigmask_to_save();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 int ret;
550
551 /*
Mathieu Desnoyersbff95042019-03-05 14:47:53 -0500552 * Perform fixup for the pre-signal frame.
Mathieu Desnoyers9800b9d2018-06-02 08:43:55 -0400553 */
Will Deacon784e0302018-06-22 11:45:07 +0100554 rseq_signal_deliver(ksig, regs);
Mathieu Desnoyers9800b9d2018-06-02 08:43:55 -0400555
556 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 * Set up the stack frame
558 */
Al Viro7e243642012-11-07 17:53:13 -0500559 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
560 ret = setup_rt_frame(ksig, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 else
Al Viro7e243642012-11-07 17:53:13 -0500562 ret = setup_frame(ksig, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 /*
565 * Check that the resulting registers are actually sane.
566 */
567 ret |= !valid_user_regs(regs);
568
Al Viro7e243642012-11-07 17:53:13 -0500569 signal_setup_done(ret, ksig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
572/*
573 * Note that 'init' is a special process: it doesn't get signals it doesn't
574 * want to handle. Thus you cannot kill init even with a SIGKILL even by
575 * mistake.
576 *
577 * Note that we go through the signals twice: once to check the signals that
578 * the kernel can handle, and then we build all the user-level signal handling
579 * stack-frames in one go after that.
580 */
Al Viro81783782012-07-19 17:48:21 +0100581static int do_signal(struct pt_regs *regs, int syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100583 unsigned int retval = 0, continue_addr = 0, restart_addr = 0;
Al Viro7e243642012-11-07 17:53:13 -0500584 struct ksignal ksig;
Al Viro81783782012-07-19 17:48:21 +0100585 int restart = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 /*
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100588 * If we were from a system call, check for system call restarting...
589 */
590 if (syscall) {
591 continue_addr = regs->ARM_pc;
592 restart_addr = continue_addr - (thumb_mode(regs) ? 2 : 4);
593 retval = regs->ARM_r0;
594
595 /*
596 * Prepare for system call restart. We do this here so that a
597 * debugger will see the already changed PSW.
598 */
599 switch (retval) {
Al Viro81783782012-07-19 17:48:21 +0100600 case -ERESTART_RESTARTBLOCK:
Al Viro66285212012-07-19 17:48:50 +0100601 restart -= 2;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100602 case -ERESTARTNOHAND:
603 case -ERESTARTSYS:
604 case -ERESTARTNOINTR:
Al Viro81783782012-07-19 17:48:21 +0100605 restart++;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100606 regs->ARM_r0 = regs->ARM_ORIG_r0;
607 regs->ARM_pc = restart_addr;
608 break;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100609 }
610 }
611
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100612 /*
613 * Get the signal to deliver. When running under ptrace, at this
614 * point the debugger may change all our registers ...
615 */
Al Viro81783782012-07-19 17:48:21 +0100616 /*
617 * Depending on the signal settings we may need to revert the
618 * decision to restart the system call. But skip this if a
619 * debugger has chosen to restart at a different PC.
620 */
Al Viro7e243642012-11-07 17:53:13 -0500621 if (get_signal(&ksig)) {
622 /* handler */
623 if (unlikely(restart) && regs->ARM_pc == restart_addr) {
Will Deaconad82cc02012-07-19 17:46:44 +0100624 if (retval == -ERESTARTNOHAND ||
625 retval == -ERESTART_RESTARTBLOCK
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100626 || (retval == -ERESTARTSYS
Al Viro7e243642012-11-07 17:53:13 -0500627 && !(ksig.ka.sa.sa_flags & SA_RESTART))) {
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100628 regs->ARM_r0 = -EINTR;
629 regs->ARM_pc = continue_addr;
630 }
631 }
Al Viro7e243642012-11-07 17:53:13 -0500632 handle_signal(&ksig, regs);
633 } else {
634 /* no handler */
635 restore_saved_sigmask();
636 if (unlikely(restart) && regs->ARM_pc == restart_addr) {
637 regs->ARM_pc = continue_addr;
638 return restart;
639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
Al Viro7e243642012-11-07 17:53:13 -0500641 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Al Viro81783782012-07-19 17:48:21 +0100644asmlinkage int
Al Viro0a267fa2012-07-19 17:47:55 +0100645do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Russell King3302cad2015-08-20 16:13:37 +0100647 /*
648 * The assembly code enters us with IRQs off, but it hasn't
649 * informed the tracing code of that for efficiency reasons.
650 * Update the trace code with the current status.
651 */
652 trace_hardirqs_off();
Al Viro0a267fa2012-07-19 17:47:55 +0100653 do {
654 if (likely(thread_flags & _TIF_NEED_RESCHED)) {
655 schedule();
656 } else {
657 if (unlikely(!user_mode(regs)))
Al Viro81783782012-07-19 17:48:21 +0100658 return 0;
Al Viro0a267fa2012-07-19 17:47:55 +0100659 local_irq_enable();
660 if (thread_flags & _TIF_SIGPENDING) {
Al Viro66285212012-07-19 17:48:50 +0100661 int restart = do_signal(regs, syscall);
662 if (unlikely(restart)) {
Al Viro81783782012-07-19 17:48:21 +0100663 /*
664 * Restart without handlers.
665 * Deal with it without leaving
666 * the kernel space.
667 */
Al Viro66285212012-07-19 17:48:50 +0100668 return restart;
Al Viro81783782012-07-19 17:48:21 +0100669 }
Al Viro0a267fa2012-07-19 17:47:55 +0100670 syscall = 0;
David A. Longc7edc9e2014-03-07 11:23:04 -0500671 } else if (thread_flags & _TIF_UPROBE) {
David A. Longc7edc9e2014-03-07 11:23:04 -0500672 uprobe_notify_resume(regs);
Al Viro0a267fa2012-07-19 17:47:55 +0100673 } else {
674 clear_thread_flag(TIF_NOTIFY_RESUME);
675 tracehook_notify_resume(regs);
Will Deacon784e0302018-06-22 11:45:07 +0100676 rseq_handle_notify_resume(NULL, regs);
Al Viro0a267fa2012-07-19 17:47:55 +0100677 }
678 }
679 local_irq_disable();
680 thread_flags = current_thread_info()->flags;
681 } while (thread_flags & _TIF_WORK_MASK);
Al Viro81783782012-07-19 17:48:21 +0100682 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
Russell King48be69a2013-07-24 00:29:18 +0100684
Russell King48be69a2013-07-24 00:29:18 +0100685struct page *get_signal_page(void)
686{
Russell Kinge0d40752013-08-03 10:30:05 +0100687 unsigned long ptr;
688 unsigned offset;
689 struct page *page;
690 void *addr;
Russell King48be69a2013-07-24 00:29:18 +0100691
Russell Kinge0d40752013-08-03 10:30:05 +0100692 page = alloc_pages(GFP_KERNEL, 0);
Russell King48be69a2013-07-24 00:29:18 +0100693
Russell Kinge0d40752013-08-03 10:30:05 +0100694 if (!page)
695 return NULL;
Russell King48be69a2013-07-24 00:29:18 +0100696
Russell Kinge0d40752013-08-03 10:30:05 +0100697 addr = page_address(page);
Russell King48be69a2013-07-24 00:29:18 +0100698
Russell Kinge0d40752013-08-03 10:30:05 +0100699 /* Give the signal return code some randomness */
700 offset = 0x200 + (get_random_int() & 0x7fc);
701 signal_return_offset = offset;
Russell King48be69a2013-07-24 00:29:18 +0100702
Russell Kinge0d40752013-08-03 10:30:05 +0100703 /*
704 * Copy signal return handlers into the vector page, and
705 * set sigreturn to be a pointer to these.
706 */
707 memcpy(addr + offset, sigreturn_codes, sizeof(sigreturn_codes));
Russell King48be69a2013-07-24 00:29:18 +0100708
Russell Kinge0d40752013-08-03 10:30:05 +0100709 ptr = (unsigned long)addr + offset;
710 flush_icache_range(ptr, ptr + sizeof(sigreturn_codes));
Russell King48be69a2013-07-24 00:29:18 +0100711
Russell Kinge0d40752013-08-03 10:30:05 +0100712 return page;
Russell King48be69a2013-07-24 00:29:18 +0100713}
Thomas Garniere33f8d322017-09-07 08:30:46 -0700714
715/* Defer to generic check */
716asmlinkage void addr_limit_check_failed(void)
717{
718 addr_limit_user_check();
719}
Mathieu Desnoyersb74406f2018-06-02 08:43:56 -0400720
721#ifdef CONFIG_DEBUG_RSEQ
722asmlinkage void do_rseq_syscall(struct pt_regs *regs)
723{
724 rseq_syscall(regs);
725}
726#endif