blob: 3ca71d679aecd04636396bafe27f8e3d3d785d58 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/arch/arm/kernel/signal.c
4 *
Russell Kingab72b002009-10-25 15:39:37 +00005 * Copyright (C) 1995-2009 Russell King
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/errno.h>
Russell King48be69a2013-07-24 00:29:18 +01008#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/personality.h>
Russell King33fa9b12008-09-06 11:35:55 +010011#include <linux/uaccess.h>
David Howells733e5e42009-09-09 08:30:21 +010012#include <linux/tracehook.h>
David A. Longc7edc9e2014-03-07 11:23:04 -050013#include <linux/uprobes.h>
Thomas Garniere33f8d322017-09-07 08:30:46 -070014#include <linux/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Russell Kingee90dab2006-11-09 14:20:47 +000016#include <asm/elf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/cacheflush.h>
Russell King48be69a2013-07-24 00:29:18 +010018#include <asm/traps.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/unistd.h>
Imre Deak82c6f5a2010-04-11 15:58:27 +010020#include <asm/vfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Nicolas Pitre5c165952017-08-09 23:42:51 -040022#include "signal.h"
23
24extern const unsigned long sigreturn_codes[17];
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Russell King48be69a2013-07-24 00:29:18 +010026static unsigned long signal_return_offset;
27
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010028#ifdef CONFIG_CRUNCH
Hartley Sweeten65a50532009-08-04 23:20:45 +010029static int preserve_crunch_context(struct crunch_sigframe __user *frame)
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010030{
31 char kbuf[sizeof(*frame) + 8];
32 struct crunch_sigframe *kframe;
33
34 /* the crunch context must be 64 bit aligned */
35 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
36 kframe->magic = CRUNCH_MAGIC;
37 kframe->size = CRUNCH_STORAGE_SIZE;
38 crunch_task_copy(current_thread_info(), &kframe->storage);
39 return __copy_to_user(frame, kframe, sizeof(*frame));
40}
41
Dave Martince184a02017-06-30 18:56:59 +010042static int restore_crunch_context(char __user **auxp)
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010043{
Dave Martince184a02017-06-30 18:56:59 +010044 struct crunch_sigframe __user *frame =
45 (struct crunch_sigframe __user *)*auxp;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010046 char kbuf[sizeof(*frame) + 8];
47 struct crunch_sigframe *kframe;
48
49 /* the crunch context must be 64 bit aligned */
50 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
51 if (__copy_from_user(kframe, frame, sizeof(*frame)))
52 return -1;
53 if (kframe->magic != CRUNCH_MAGIC ||
54 kframe->size != CRUNCH_STORAGE_SIZE)
55 return -1;
Dave Martince184a02017-06-30 18:56:59 +010056 *auxp += CRUNCH_STORAGE_SIZE;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010057 crunch_task_restore(current_thread_info(), &kframe->storage);
58 return 0;
59}
60#endif
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#ifdef CONFIG_IWMMXT
63
Dave Martin26958352017-06-30 18:56:09 +010064static int preserve_iwmmxt_context(struct iwmmxt_sigframe __user *frame)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Hugh Dickins69b04752005-10-29 18:16:36 -070066 char kbuf[sizeof(*frame) + 8];
67 struct iwmmxt_sigframe *kframe;
Dave Martince184a02017-06-30 18:56:59 +010068 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 /* the iWMMXt context must be 64 bit aligned */
Hugh Dickins69b04752005-10-29 18:16:36 -070071 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
Dave Martince184a02017-06-30 18:56:59 +010072
73 if (test_thread_flag(TIF_USING_IWMMXT)) {
74 kframe->magic = IWMMXT_MAGIC;
75 kframe->size = IWMMXT_STORAGE_SIZE;
76 iwmmxt_task_copy(current_thread_info(), &kframe->storage);
Dave Martince184a02017-06-30 18:56:59 +010077 } else {
78 /*
79 * For bug-compatibility with older kernels, some space
80 * has to be reserved for iWMMXt even if it's not used.
81 * Set the magic and size appropriately so that properly
82 * written userspace can skip it reliably:
83 */
Julien Thierry73839792018-09-11 10:12:05 +010084 *kframe = (struct iwmmxt_sigframe) {
85 .magic = DUMMY_MAGIC,
86 .size = IWMMXT_STORAGE_SIZE,
87 };
Dave Martince184a02017-06-30 18:56:59 +010088 }
89
Julien Thierry73839792018-09-11 10:12:05 +010090 err = __copy_to_user(frame, kframe, sizeof(*kframe));
91
Dave Martince184a02017-06-30 18:56:59 +010092 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{
Julien Thierry3aa2df62018-09-11 10:12:18 +0100137 struct vfp_sigframe kframe;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100138 int err = 0;
139
Julien Thierry3aa2df62018-09-11 10:12:18 +0100140 memset(&kframe, 0, sizeof(kframe));
141 kframe.magic = VFP_MAGIC;
142 kframe.size = VFP_STORAGE_SIZE;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100143
Julien Thierry3aa2df62018-09-11 10:12:18 +0100144 err = vfp_preserve_user_clear_hwstate(&kframe.ufp, &kframe.ufp_exc);
Will Deacon2498814f2012-04-23 15:38:28 +0100145 if (err)
Julien Thierry3aa2df62018-09-11 10:12:18 +0100146 return err;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100147
Julien Thierry3aa2df62018-09-11 10:12:18 +0100148 return __copy_to_user(frame, &kframe, sizeof(kframe));
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{
Russell King42019fc2018-07-09 10:13:36 +0100153 struct vfp_sigframe frame;
154 int err;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100155
Russell King42019fc2018-07-09 10:13:36 +0100156 err = __copy_from_user(&frame, *auxp, sizeof(frame));
Imre Deak82c6f5a2010-04-11 15:58:27 +0100157 if (err)
Russell King42019fc2018-07-09 10:13:36 +0100158 return err;
159
160 if (frame.magic != VFP_MAGIC || frame.size != VFP_STORAGE_SIZE)
Imre Deak82c6f5a2010-04-11 15:58:27 +0100161 return -EINVAL;
162
Russell King42019fc2018-07-09 10:13:36 +0100163 *auxp += sizeof(frame);
164 return vfp_restore_user_hwstate(&frame.ufp, &frame.ufp_exc);
Imre Deak82c6f5a2010-04-11 15:58:27 +0100165}
166
167#endif
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * Do a signal return; undo the signal stack. These are aligned to 64-bit.
171 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Russell King68071482006-06-15 20:23:02 +0100173static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Russell Kingc32cd412018-07-09 10:05:22 +0100175 struct sigcontext context;
Dave Martince184a02017-06-30 18:56:59 +0100176 char __user *aux;
Russell King68071482006-06-15 20:23:02 +0100177 sigset_t set;
178 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Russell King68071482006-06-15 20:23:02 +0100180 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
Al Viro77097ae2012-04-27 13:58:59 -0400181 if (err == 0)
Matt Fleming101d9b02012-03-05 15:05:34 -0800182 set_current_blocked(&set);
Russell King68071482006-06-15 20:23:02 +0100183
Russell Kingc32cd412018-07-09 10:05:22 +0100184 err |= __copy_from_user(&context, &sf->uc.uc_mcontext, sizeof(context));
185 if (err == 0) {
186 regs->ARM_r0 = context.arm_r0;
187 regs->ARM_r1 = context.arm_r1;
188 regs->ARM_r2 = context.arm_r2;
189 regs->ARM_r3 = context.arm_r3;
190 regs->ARM_r4 = context.arm_r4;
191 regs->ARM_r5 = context.arm_r5;
192 regs->ARM_r6 = context.arm_r6;
193 regs->ARM_r7 = context.arm_r7;
194 regs->ARM_r8 = context.arm_r8;
195 regs->ARM_r9 = context.arm_r9;
196 regs->ARM_r10 = context.arm_r10;
197 regs->ARM_fp = context.arm_fp;
198 regs->ARM_ip = context.arm_ip;
199 regs->ARM_sp = context.arm_sp;
200 regs->ARM_lr = context.arm_lr;
201 regs->ARM_pc = context.arm_pc;
202 regs->ARM_cpsr = context.arm_cpsr;
203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 err |= !valid_user_regs(regs);
206
Dave Martince184a02017-06-30 18:56:59 +0100207 aux = (char __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100208#ifdef CONFIG_CRUNCH
209 if (err == 0)
Dave Martince184a02017-06-30 18:56:59 +0100210 err |= restore_crunch_context(&aux);
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100211#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212#ifdef CONFIG_IWMMXT
Dave Martince184a02017-06-30 18:56:59 +0100213 if (err == 0)
214 err |= restore_iwmmxt_context(&aux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215#endif
216#ifdef CONFIG_VFP
Imre Deak82c6f5a2010-04-11 15:58:27 +0100217 if (err == 0)
Dave Martince184a02017-06-30 18:56:59 +0100218 err |= restore_vfp_context(&aux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219#endif
220
221 return err;
222}
223
224asmlinkage int sys_sigreturn(struct pt_regs *regs)
225{
226 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800229 current->restart_block.fn = do_no_restart_syscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 /*
232 * Since we stacked the signal on a 64-bit boundary,
233 * then 'sp' should be word aligned here. If it's
234 * not, then the user is trying to mess with us.
235 */
236 if (regs->ARM_sp & 7)
237 goto badframe;
238
239 frame = (struct sigframe __user *)regs->ARM_sp;
240
Linus Torvalds96d4f262019-01-03 18:57:57 -0800241 if (!access_ok(frame, sizeof (*frame)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Russell King68071482006-06-15 20:23:02 +0100244 if (restore_sigframe(regs, frame))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 goto badframe;
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return regs->ARM_r0;
248
249badframe:
250 force_sig(SIGSEGV, current);
251 return 0;
252}
253
254asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
255{
256 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800259 current->restart_block.fn = do_no_restart_syscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 /*
262 * Since we stacked the signal on a 64-bit boundary,
263 * then 'sp' should be word aligned here. If it's
264 * not, then the user is trying to mess with us.
265 */
266 if (regs->ARM_sp & 7)
267 goto badframe;
268
269 frame = (struct rt_sigframe __user *)regs->ARM_sp;
270
Linus Torvalds96d4f262019-01-03 18:57:57 -0800271 if (!access_ok(frame, sizeof (*frame)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Russell King68071482006-06-15 20:23:02 +0100274 if (restore_sigframe(regs, &frame->sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 goto badframe;
276
Al Viroec93ac82012-12-23 01:52:54 -0500277 if (restore_altstack(&frame->sig.uc.uc_stack))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 goto badframe;
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return regs->ARM_r0;
281
282badframe:
283 force_sig(SIGSEGV, current);
284 return 0;
285}
286
287static int
Russell Kingaca6ca12006-06-15 20:28:03 +0100288setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs, sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100290 struct aux_sigframe __user *aux;
Julien Thierry5ca451c2018-09-11 10:11:06 +0100291 struct sigcontext context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 int err = 0;
293
Julien Thierry5ca451c2018-09-11 10:11:06 +0100294 context = (struct sigcontext) {
295 .arm_r0 = regs->ARM_r0,
296 .arm_r1 = regs->ARM_r1,
297 .arm_r2 = regs->ARM_r2,
298 .arm_r3 = regs->ARM_r3,
299 .arm_r4 = regs->ARM_r4,
300 .arm_r5 = regs->ARM_r5,
301 .arm_r6 = regs->ARM_r6,
302 .arm_r7 = regs->ARM_r7,
303 .arm_r8 = regs->ARM_r8,
304 .arm_r9 = regs->ARM_r9,
305 .arm_r10 = regs->ARM_r10,
306 .arm_fp = regs->ARM_fp,
307 .arm_ip = regs->ARM_ip,
308 .arm_sp = regs->ARM_sp,
309 .arm_lr = regs->ARM_lr,
310 .arm_pc = regs->ARM_pc,
311 .arm_cpsr = regs->ARM_cpsr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Julien Thierry5ca451c2018-09-11 10:11:06 +0100313 .trap_no = current->thread.trap_no,
314 .error_code = current->thread.error_code,
315 .fault_address = current->thread.address,
316 .oldmask = set->sig[0],
317 };
318
319 err |= __copy_to_user(&sf->uc.uc_mcontext, &context, sizeof(context));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Russell Kingaca6ca12006-06-15 20:28:03 +0100321 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100323 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100324#ifdef CONFIG_CRUNCH
325 if (err == 0)
326 err |= preserve_crunch_context(&aux->crunch);
327#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328#ifdef CONFIG_IWMMXT
Dave Martince184a02017-06-30 18:56:59 +0100329 if (err == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 err |= preserve_iwmmxt_context(&aux->iwmmxt);
331#endif
332#ifdef CONFIG_VFP
Imre Deak82c6f5a2010-04-11 15:58:27 +0100333 if (err == 0)
334 err |= preserve_vfp_context(&aux->vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335#endif
Julien Thierry18ea66b2018-09-11 10:13:11 +0100336 err |= __put_user(0, &aux->end_magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 return err;
339}
340
341static inline void __user *
Al Viro7e243642012-11-07 17:53:13 -0500342get_sigframe(struct ksignal *ksig, struct pt_regs *regs, int framesize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Al Viro7e243642012-11-07 17:53:13 -0500344 unsigned long sp = sigsp(regs->ARM_sp, ksig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 void __user *frame;
346
347 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 * ATPCS B01 mandates 8-byte alignment
349 */
350 frame = (void __user *)((sp - framesize) & ~7);
351
352 /*
353 * Check that we can actually write to the signal frame.
354 */
Linus Torvalds96d4f262019-01-03 18:57:57 -0800355 if (!access_ok(frame, framesize))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 frame = NULL;
357
358 return frame;
359}
360
Al Viro7e243642012-11-07 17:53:13 -0500361static int
362setup_return(struct pt_regs *regs, struct ksignal *ksig,
363 unsigned long __user *rc, void __user *frame)
364{
365 unsigned long handler = (unsigned long)ksig->ka.sa.sa_handler;
Nicolas Pitre5c165952017-08-09 23:42:51 -0400366 unsigned long handler_fdpic_GOT = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 unsigned long retcode;
Nicolas Pitre5c165952017-08-09 23:42:51 -0400368 unsigned int idx, thumb = 0;
Russell King53399052011-02-20 12:22:52 +0000369 unsigned long cpsr = regs->ARM_cpsr & ~(PSR_f | PSR_E_BIT);
Nicolas Pitre5c165952017-08-09 23:42:51 -0400370 bool fdpic = IS_ENABLED(CONFIG_BINFMT_ELF_FDPIC) &&
371 (current->personality & FDPIC_FUNCPTRS);
372
373 if (fdpic) {
374 unsigned long __user *fdpic_func_desc =
375 (unsigned long __user *)handler;
376 if (__get_user(handler, &fdpic_func_desc[0]) ||
377 __get_user(handler_fdpic_GOT, &fdpic_func_desc[1]))
378 return 1;
379 }
Russell King53399052011-02-20 12:22:52 +0000380
381 cpsr |= PSR_ENDSTATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 /*
384 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
385 */
Al Viro7e243642012-11-07 17:53:13 -0500386 if (ksig->ka.sa.sa_flags & SA_THIRTYTWO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
388
389#ifdef CONFIG_ARM_THUMB
390 if (elf_hwcap & HWCAP_THUMB) {
391 /*
392 * The LSB of the handler determines if we're going to
393 * be using THUMB or ARM mode for this signal handler.
394 */
395 thumb = handler & 1;
396
T.J. Purtell6ecf8302013-11-06 18:38:05 +0100397 /*
Russell King9b556132015-09-11 16:44:02 +0100398 * Clear the If-Then Thumb-2 execution state. ARM spec
399 * requires this to be all 000s in ARM mode. Snapdragon
400 * S4/Krait misbehaves on a Thumb=>ARM signal transition
401 * without this.
402 *
403 * We must do this whenever we are running on a Thumb-2
404 * capable CPU, which includes ARMv6T2. However, we elect
Russell King12fc7302015-09-16 11:08:49 +0100405 * to always do this to simplify the code; this field is
406 * marked UNK/SBZP for older architectures.
T.J. Purtell6ecf8302013-11-06 18:38:05 +0100407 */
408 cpsr &= ~PSR_IT_MASK;
T.J. Purtell6ecf8302013-11-06 18:38:05 +0100409
Catalin Marinasd71e1352009-05-30 14:00:15 +0100410 if (thumb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 cpsr |= PSR_T_BIT;
Catalin Marinasd71e1352009-05-30 14:00:15 +0100412 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 cpsr &= ~PSR_T_BIT;
414 }
415#endif
416
Al Viro7e243642012-11-07 17:53:13 -0500417 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
418 retcode = (unsigned long)ksig->ka.sa.sa_restorer;
Nicolas Pitre5c165952017-08-09 23:42:51 -0400419 if (fdpic) {
420 /*
421 * We need code to load the function descriptor.
422 * That code follows the standard sigreturn code
423 * (6 words), and is made of 3 + 2 words for each
424 * variant. The 4th copied word is the actual FD
425 * address that the assembly code expects.
426 */
427 idx = 6 + thumb * 3;
428 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
429 idx += 5;
430 if (__put_user(sigreturn_codes[idx], rc ) ||
431 __put_user(sigreturn_codes[idx+1], rc+1) ||
432 __put_user(sigreturn_codes[idx+2], rc+2) ||
433 __put_user(retcode, rc+3))
434 return 1;
435 goto rc_finish;
436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 } else {
Nicolas Pitre5c165952017-08-09 23:42:51 -0400438 idx = thumb << 1;
Al Viro7e243642012-11-07 17:53:13 -0500439 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000440 idx += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Jonathan Austin9dfc28b2013-04-18 18:37:24 +0100442 /*
443 * Put the sigreturn code on the stack no matter which return
444 * mechanism we use in order to remain ABI compliant
445 */
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000446 if (__put_user(sigreturn_codes[idx], rc) ||
447 __put_user(sigreturn_codes[idx+1], rc+1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return 1;
449
Nicolas Pitre5c165952017-08-09 23:42:51 -0400450rc_finish:
Russell King8c0cc8a2013-08-03 10:39:51 +0100451#ifdef CONFIG_MMU
452 if (cpsr & MODE32_BIT) {
Russell King48be69a2013-07-24 00:29:18 +0100453 struct mm_struct *mm = current->mm;
454
Russell Kinge00d3492005-06-22 20:26:05 +0100455 /*
Russell King48be69a2013-07-24 00:29:18 +0100456 * 32-bit code can use the signal return page
457 * except when the MPU has protected the vectors
458 * page from PL0
Russell Kinge00d3492005-06-22 20:26:05 +0100459 */
Russell King48be69a2013-07-24 00:29:18 +0100460 retcode = mm->context.sigpage + signal_return_offset +
461 (idx << 2) + thumb;
Russell King8c0cc8a2013-08-03 10:39:51 +0100462 } else
463#endif
464 {
Russell Kinge00d3492005-06-22 20:26:05 +0100465 /*
466 * Ensure that the instruction cache sees
467 * the return code written onto the stack.
468 */
469 flush_icache_range((unsigned long)rc,
Nicolas Pitre5c165952017-08-09 23:42:51 -0400470 (unsigned long)(rc + 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Russell Kinge00d3492005-06-22 20:26:05 +0100472 retcode = ((unsigned long)rc) + thumb;
473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
475
Richard Weinbergera4980442014-07-13 15:24:03 +0200476 regs->ARM_r0 = ksig->sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 regs->ARM_sp = (unsigned long)frame;
478 regs->ARM_lr = retcode;
479 regs->ARM_pc = handler;
Nicolas Pitre5c165952017-08-09 23:42:51 -0400480 if (fdpic)
481 regs->ARM_r9 = handler_fdpic_GOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 regs->ARM_cpsr = cpsr;
483
484 return 0;
485}
486
487static int
Al Viro7e243642012-11-07 17:53:13 -0500488setup_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Al Viro7e243642012-11-07 17:53:13 -0500490 struct sigframe __user *frame = get_sigframe(ksig, regs, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 int err = 0;
492
493 if (!frame)
494 return 1;
495
Russell Kingca195cf2006-06-24 22:41:09 +0100496 /*
497 * Set uc.uc_flags to a value which sc.trap_no would never have.
498 */
Julien Thierry18ea66b2018-09-11 10:13:11 +0100499 err = __put_user(0x5ac3c35a, &frame->uc.uc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Russell Kingaca6ca12006-06-15 20:28:03 +0100501 err |= setup_sigframe(frame, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (err == 0)
Al Viro7e243642012-11-07 17:53:13 -0500503 err = setup_return(regs, ksig, frame->retcode, frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 return err;
506}
507
508static int
Al Viro7e243642012-11-07 17:53:13 -0500509setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Al Viro7e243642012-11-07 17:53:13 -0500511 struct rt_sigframe __user *frame = get_sigframe(ksig, regs, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 int err = 0;
513
514 if (!frame)
515 return 1;
516
Al Viro7e243642012-11-07 17:53:13 -0500517 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Julien Thierry18ea66b2018-09-11 10:13:11 +0100519 err |= __put_user(0, &frame->sig.uc.uc_flags);
520 err |= __put_user(NULL, &frame->sig.uc.uc_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Al Viroec93ac82012-12-23 01:52:54 -0500522 err |= __save_altstack(&frame->sig.uc.uc_stack, regs->ARM_sp);
Russell Kingaca6ca12006-06-15 20:28:03 +0100523 err |= setup_sigframe(&frame->sig, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (err == 0)
Al Viro7e243642012-11-07 17:53:13 -0500525 err = setup_return(regs, ksig, frame->sig.retcode, frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 if (err == 0) {
528 /*
529 * For realtime signals we must also set the second and third
530 * arguments for the signal handler.
531 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
532 */
533 regs->ARM_r1 = (unsigned long)&frame->info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100534 regs->ARM_r2 = (unsigned long)&frame->sig.uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
536
537 return err;
538}
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540/*
541 * OK, we're invoking a handler
542 */
Al Viro7e243642012-11-07 17:53:13 -0500543static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Al Virob7f9a112012-05-02 09:59:21 -0400545 sigset_t *oldset = sigmask_to_save();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 int ret;
547
548 /*
Mathieu Desnoyersbff95042019-03-05 14:47:53 -0500549 * Perform fixup for the pre-signal frame.
Mathieu Desnoyers9800b9d2018-06-02 08:43:55 -0400550 */
Will Deacon784e0302018-06-22 11:45:07 +0100551 rseq_signal_deliver(ksig, regs);
Mathieu Desnoyers9800b9d2018-06-02 08:43:55 -0400552
553 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 * Set up the stack frame
555 */
Al Viro7e243642012-11-07 17:53:13 -0500556 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
557 ret = setup_rt_frame(ksig, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 else
Al Viro7e243642012-11-07 17:53:13 -0500559 ret = setup_frame(ksig, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 /*
562 * Check that the resulting registers are actually sane.
563 */
564 ret |= !valid_user_regs(regs);
565
Al Viro7e243642012-11-07 17:53:13 -0500566 signal_setup_done(ret, ksig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
569/*
570 * Note that 'init' is a special process: it doesn't get signals it doesn't
571 * want to handle. Thus you cannot kill init even with a SIGKILL even by
572 * mistake.
573 *
574 * Note that we go through the signals twice: once to check the signals that
575 * the kernel can handle, and then we build all the user-level signal handling
576 * stack-frames in one go after that.
577 */
Al Viro81783782012-07-19 17:48:21 +0100578static int do_signal(struct pt_regs *regs, int syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100580 unsigned int retval = 0, continue_addr = 0, restart_addr = 0;
Al Viro7e243642012-11-07 17:53:13 -0500581 struct ksignal ksig;
Al Viro81783782012-07-19 17:48:21 +0100582 int restart = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 /*
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100585 * If we were from a system call, check for system call restarting...
586 */
587 if (syscall) {
588 continue_addr = regs->ARM_pc;
589 restart_addr = continue_addr - (thumb_mode(regs) ? 2 : 4);
590 retval = regs->ARM_r0;
591
592 /*
593 * Prepare for system call restart. We do this here so that a
594 * debugger will see the already changed PSW.
595 */
596 switch (retval) {
Al Viro81783782012-07-19 17:48:21 +0100597 case -ERESTART_RESTARTBLOCK:
Al Viro66285212012-07-19 17:48:50 +0100598 restart -= 2;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100599 case -ERESTARTNOHAND:
600 case -ERESTARTSYS:
601 case -ERESTARTNOINTR:
Al Viro81783782012-07-19 17:48:21 +0100602 restart++;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100603 regs->ARM_r0 = regs->ARM_ORIG_r0;
604 regs->ARM_pc = restart_addr;
605 break;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100606 }
607 }
608
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100609 /*
610 * Get the signal to deliver. When running under ptrace, at this
611 * point the debugger may change all our registers ...
612 */
Al Viro81783782012-07-19 17:48:21 +0100613 /*
614 * Depending on the signal settings we may need to revert the
615 * decision to restart the system call. But skip this if a
616 * debugger has chosen to restart at a different PC.
617 */
Al Viro7e243642012-11-07 17:53:13 -0500618 if (get_signal(&ksig)) {
619 /* handler */
620 if (unlikely(restart) && regs->ARM_pc == restart_addr) {
Will Deaconad82cc02012-07-19 17:46:44 +0100621 if (retval == -ERESTARTNOHAND ||
622 retval == -ERESTART_RESTARTBLOCK
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100623 || (retval == -ERESTARTSYS
Al Viro7e243642012-11-07 17:53:13 -0500624 && !(ksig.ka.sa.sa_flags & SA_RESTART))) {
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100625 regs->ARM_r0 = -EINTR;
626 regs->ARM_pc = continue_addr;
627 }
628 }
Al Viro7e243642012-11-07 17:53:13 -0500629 handle_signal(&ksig, regs);
630 } else {
631 /* no handler */
632 restore_saved_sigmask();
633 if (unlikely(restart) && regs->ARM_pc == restart_addr) {
634 regs->ARM_pc = continue_addr;
635 return restart;
636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
Al Viro7e243642012-11-07 17:53:13 -0500638 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
Al Viro81783782012-07-19 17:48:21 +0100641asmlinkage int
Al Viro0a267fa2012-07-19 17:47:55 +0100642do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Russell King3302cad2015-08-20 16:13:37 +0100644 /*
645 * The assembly code enters us with IRQs off, but it hasn't
646 * informed the tracing code of that for efficiency reasons.
647 * Update the trace code with the current status.
648 */
649 trace_hardirqs_off();
Al Viro0a267fa2012-07-19 17:47:55 +0100650 do {
651 if (likely(thread_flags & _TIF_NEED_RESCHED)) {
652 schedule();
653 } else {
654 if (unlikely(!user_mode(regs)))
Al Viro81783782012-07-19 17:48:21 +0100655 return 0;
Al Viro0a267fa2012-07-19 17:47:55 +0100656 local_irq_enable();
657 if (thread_flags & _TIF_SIGPENDING) {
Al Viro66285212012-07-19 17:48:50 +0100658 int restart = do_signal(regs, syscall);
659 if (unlikely(restart)) {
Al Viro81783782012-07-19 17:48:21 +0100660 /*
661 * Restart without handlers.
662 * Deal with it without leaving
663 * the kernel space.
664 */
Al Viro66285212012-07-19 17:48:50 +0100665 return restart;
Al Viro81783782012-07-19 17:48:21 +0100666 }
Al Viro0a267fa2012-07-19 17:47:55 +0100667 syscall = 0;
David A. Longc7edc9e2014-03-07 11:23:04 -0500668 } else if (thread_flags & _TIF_UPROBE) {
David A. Longc7edc9e2014-03-07 11:23:04 -0500669 uprobe_notify_resume(regs);
Al Viro0a267fa2012-07-19 17:47:55 +0100670 } else {
671 clear_thread_flag(TIF_NOTIFY_RESUME);
672 tracehook_notify_resume(regs);
Will Deacon784e0302018-06-22 11:45:07 +0100673 rseq_handle_notify_resume(NULL, regs);
Al Viro0a267fa2012-07-19 17:47:55 +0100674 }
675 }
676 local_irq_disable();
677 thread_flags = current_thread_info()->flags;
678 } while (thread_flags & _TIF_WORK_MASK);
Al Viro81783782012-07-19 17:48:21 +0100679 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
Russell King48be69a2013-07-24 00:29:18 +0100681
Russell King48be69a2013-07-24 00:29:18 +0100682struct page *get_signal_page(void)
683{
Russell Kinge0d40752013-08-03 10:30:05 +0100684 unsigned long ptr;
685 unsigned offset;
686 struct page *page;
687 void *addr;
Russell King48be69a2013-07-24 00:29:18 +0100688
Russell Kinge0d40752013-08-03 10:30:05 +0100689 page = alloc_pages(GFP_KERNEL, 0);
Russell King48be69a2013-07-24 00:29:18 +0100690
Russell Kinge0d40752013-08-03 10:30:05 +0100691 if (!page)
692 return NULL;
Russell King48be69a2013-07-24 00:29:18 +0100693
Russell Kinge0d40752013-08-03 10:30:05 +0100694 addr = page_address(page);
Russell King48be69a2013-07-24 00:29:18 +0100695
Russell Kinge0d40752013-08-03 10:30:05 +0100696 /* Give the signal return code some randomness */
697 offset = 0x200 + (get_random_int() & 0x7fc);
698 signal_return_offset = offset;
Russell King48be69a2013-07-24 00:29:18 +0100699
Russell Kinge0d40752013-08-03 10:30:05 +0100700 /*
701 * Copy signal return handlers into the vector page, and
702 * set sigreturn to be a pointer to these.
703 */
704 memcpy(addr + offset, sigreturn_codes, sizeof(sigreturn_codes));
Russell King48be69a2013-07-24 00:29:18 +0100705
Russell Kinge0d40752013-08-03 10:30:05 +0100706 ptr = (unsigned long)addr + offset;
707 flush_icache_range(ptr, ptr + sizeof(sigreturn_codes));
Russell King48be69a2013-07-24 00:29:18 +0100708
Russell Kinge0d40752013-08-03 10:30:05 +0100709 return page;
Russell King48be69a2013-07-24 00:29:18 +0100710}
Thomas Garniere33f8d322017-09-07 08:30:46 -0700711
712/* Defer to generic check */
713asmlinkage void addr_limit_check_failed(void)
714{
715 addr_limit_user_check();
716}
Mathieu Desnoyersb74406f2018-06-02 08:43:56 -0400717
718#ifdef CONFIG_DEBUG_RSEQ
719asmlinkage void do_rseq_syscall(struct pt_regs *regs)
720{
721 rseq_syscall(regs);
722}
723#endif