blob: 5879ab3f53c18393741e71488b2880ad768895cb [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{
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
187 __get_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
188 __get_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
189 __get_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
190 __get_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
191 __get_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
192 __get_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
193 __get_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
194 __get_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
195 __get_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
196 __get_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
197 __get_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
198 __get_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
199 __get_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
200 __get_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
201 __get_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
202 __get_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
203 __get_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
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
241 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
242 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
271 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
272 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 int err = 0;
292
Russell Kingaca6ca12006-06-15 20:28:03 +0100293 __put_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
294 __put_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
295 __put_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
296 __put_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
297 __put_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
298 __put_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
299 __put_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
300 __put_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
301 __put_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
302 __put_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
303 __put_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
304 __put_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
305 __put_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
306 __put_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
307 __put_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
308 __put_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
309 __put_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Russell Kingaca6ca12006-06-15 20:28:03 +0100311 __put_user_error(current->thread.trap_no, &sf->uc.uc_mcontext.trap_no, err);
312 __put_user_error(current->thread.error_code, &sf->uc.uc_mcontext.error_code, err);
313 __put_user_error(current->thread.address, &sf->uc.uc_mcontext.fault_address, err);
314 __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Russell Kingaca6ca12006-06-15 20:28:03 +0100316 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100318 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100319#ifdef CONFIG_CRUNCH
320 if (err == 0)
321 err |= preserve_crunch_context(&aux->crunch);
322#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323#ifdef CONFIG_IWMMXT
Dave Martince184a02017-06-30 18:56:59 +0100324 if (err == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 err |= preserve_iwmmxt_context(&aux->iwmmxt);
326#endif
327#ifdef CONFIG_VFP
Imre Deak82c6f5a2010-04-11 15:58:27 +0100328 if (err == 0)
329 err |= preserve_vfp_context(&aux->vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330#endif
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100331 __put_user_error(0, &aux->end_magic, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 return err;
334}
335
336static inline void __user *
Al Viro7e243642012-11-07 17:53:13 -0500337get_sigframe(struct ksignal *ksig, struct pt_regs *regs, int framesize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Al Viro7e243642012-11-07 17:53:13 -0500339 unsigned long sp = sigsp(regs->ARM_sp, ksig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 void __user *frame;
341
342 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 * ATPCS B01 mandates 8-byte alignment
344 */
345 frame = (void __user *)((sp - framesize) & ~7);
346
347 /*
348 * Check that we can actually write to the signal frame.
349 */
350 if (!access_ok(VERIFY_WRITE, frame, framesize))
351 frame = NULL;
352
353 return frame;
354}
355
Al Viro7e243642012-11-07 17:53:13 -0500356static int
357setup_return(struct pt_regs *regs, struct ksignal *ksig,
358 unsigned long __user *rc, void __user *frame)
359{
360 unsigned long handler = (unsigned long)ksig->ka.sa.sa_handler;
Nicolas Pitre5c165952017-08-09 23:42:51 -0400361 unsigned long handler_fdpic_GOT = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 unsigned long retcode;
Nicolas Pitre5c165952017-08-09 23:42:51 -0400363 unsigned int idx, thumb = 0;
Russell King53399052011-02-20 12:22:52 +0000364 unsigned long cpsr = regs->ARM_cpsr & ~(PSR_f | PSR_E_BIT);
Nicolas Pitre5c165952017-08-09 23:42:51 -0400365 bool fdpic = IS_ENABLED(CONFIG_BINFMT_ELF_FDPIC) &&
366 (current->personality & FDPIC_FUNCPTRS);
367
368 if (fdpic) {
369 unsigned long __user *fdpic_func_desc =
370 (unsigned long __user *)handler;
371 if (__get_user(handler, &fdpic_func_desc[0]) ||
372 __get_user(handler_fdpic_GOT, &fdpic_func_desc[1]))
373 return 1;
374 }
Russell King53399052011-02-20 12:22:52 +0000375
376 cpsr |= PSR_ENDSTATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 /*
379 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
380 */
Al Viro7e243642012-11-07 17:53:13 -0500381 if (ksig->ka.sa.sa_flags & SA_THIRTYTWO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
383
384#ifdef CONFIG_ARM_THUMB
385 if (elf_hwcap & HWCAP_THUMB) {
386 /*
387 * The LSB of the handler determines if we're going to
388 * be using THUMB or ARM mode for this signal handler.
389 */
390 thumb = handler & 1;
391
T.J. Purtell6ecf8302013-11-06 18:38:05 +0100392 /*
Russell King9b556132015-09-11 16:44:02 +0100393 * Clear the If-Then Thumb-2 execution state. ARM spec
394 * requires this to be all 000s in ARM mode. Snapdragon
395 * S4/Krait misbehaves on a Thumb=>ARM signal transition
396 * without this.
397 *
398 * We must do this whenever we are running on a Thumb-2
399 * capable CPU, which includes ARMv6T2. However, we elect
Russell King12fc7302015-09-16 11:08:49 +0100400 * to always do this to simplify the code; this field is
401 * marked UNK/SBZP for older architectures.
T.J. Purtell6ecf8302013-11-06 18:38:05 +0100402 */
403 cpsr &= ~PSR_IT_MASK;
T.J. Purtell6ecf8302013-11-06 18:38:05 +0100404
Catalin Marinasd71e1352009-05-30 14:00:15 +0100405 if (thumb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 cpsr |= PSR_T_BIT;
Catalin Marinasd71e1352009-05-30 14:00:15 +0100407 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 cpsr &= ~PSR_T_BIT;
409 }
410#endif
411
Al Viro7e243642012-11-07 17:53:13 -0500412 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
413 retcode = (unsigned long)ksig->ka.sa.sa_restorer;
Nicolas Pitre5c165952017-08-09 23:42:51 -0400414 if (fdpic) {
415 /*
416 * We need code to load the function descriptor.
417 * That code follows the standard sigreturn code
418 * (6 words), and is made of 3 + 2 words for each
419 * variant. The 4th copied word is the actual FD
420 * address that the assembly code expects.
421 */
422 idx = 6 + thumb * 3;
423 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
424 idx += 5;
425 if (__put_user(sigreturn_codes[idx], rc ) ||
426 __put_user(sigreturn_codes[idx+1], rc+1) ||
427 __put_user(sigreturn_codes[idx+2], rc+2) ||
428 __put_user(retcode, rc+3))
429 return 1;
430 goto rc_finish;
431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 } else {
Nicolas Pitre5c165952017-08-09 23:42:51 -0400433 idx = thumb << 1;
Al Viro7e243642012-11-07 17:53:13 -0500434 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000435 idx += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Jonathan Austin9dfc28b2013-04-18 18:37:24 +0100437 /*
438 * Put the sigreturn code on the stack no matter which return
439 * mechanism we use in order to remain ABI compliant
440 */
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000441 if (__put_user(sigreturn_codes[idx], rc) ||
442 __put_user(sigreturn_codes[idx+1], rc+1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return 1;
444
Nicolas Pitre5c165952017-08-09 23:42:51 -0400445rc_finish:
Russell King8c0cc8a2013-08-03 10:39:51 +0100446#ifdef CONFIG_MMU
447 if (cpsr & MODE32_BIT) {
Russell King48be69a2013-07-24 00:29:18 +0100448 struct mm_struct *mm = current->mm;
449
Russell Kinge00d3492005-06-22 20:26:05 +0100450 /*
Russell King48be69a2013-07-24 00:29:18 +0100451 * 32-bit code can use the signal return page
452 * except when the MPU has protected the vectors
453 * page from PL0
Russell Kinge00d3492005-06-22 20:26:05 +0100454 */
Russell King48be69a2013-07-24 00:29:18 +0100455 retcode = mm->context.sigpage + signal_return_offset +
456 (idx << 2) + thumb;
Russell King8c0cc8a2013-08-03 10:39:51 +0100457 } else
458#endif
459 {
Russell Kinge00d3492005-06-22 20:26:05 +0100460 /*
461 * Ensure that the instruction cache sees
462 * the return code written onto the stack.
463 */
464 flush_icache_range((unsigned long)rc,
Nicolas Pitre5c165952017-08-09 23:42:51 -0400465 (unsigned long)(rc + 3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Russell Kinge00d3492005-06-22 20:26:05 +0100467 retcode = ((unsigned long)rc) + thumb;
468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470
Richard Weinbergera4980442014-07-13 15:24:03 +0200471 regs->ARM_r0 = ksig->sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 regs->ARM_sp = (unsigned long)frame;
473 regs->ARM_lr = retcode;
474 regs->ARM_pc = handler;
Nicolas Pitre5c165952017-08-09 23:42:51 -0400475 if (fdpic)
476 regs->ARM_r9 = handler_fdpic_GOT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 regs->ARM_cpsr = cpsr;
478
479 return 0;
480}
481
482static int
Al Viro7e243642012-11-07 17:53:13 -0500483setup_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Al Viro7e243642012-11-07 17:53:13 -0500485 struct sigframe __user *frame = get_sigframe(ksig, regs, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 int err = 0;
487
488 if (!frame)
489 return 1;
490
Russell Kingca195cf2006-06-24 22:41:09 +0100491 /*
492 * Set uc.uc_flags to a value which sc.trap_no would never have.
493 */
494 __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Russell Kingaca6ca12006-06-15 20:28:03 +0100496 err |= setup_sigframe(frame, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (err == 0)
Al Viro7e243642012-11-07 17:53:13 -0500498 err = setup_return(regs, ksig, frame->retcode, frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 return err;
501}
502
503static int
Al Viro7e243642012-11-07 17:53:13 -0500504setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Al Viro7e243642012-11-07 17:53:13 -0500506 struct rt_sigframe __user *frame = get_sigframe(ksig, regs, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 int err = 0;
508
509 if (!frame)
510 return 1;
511
Al Viro7e243642012-11-07 17:53:13 -0500512 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Russell Kingcb3504e2006-06-15 20:18:25 +0100514 __put_user_error(0, &frame->sig.uc.uc_flags, err);
515 __put_user_error(NULL, &frame->sig.uc.uc_link, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Al Viroec93ac82012-12-23 01:52:54 -0500517 err |= __save_altstack(&frame->sig.uc.uc_stack, regs->ARM_sp);
Russell Kingaca6ca12006-06-15 20:28:03 +0100518 err |= setup_sigframe(&frame->sig, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (err == 0)
Al Viro7e243642012-11-07 17:53:13 -0500520 err = setup_return(regs, ksig, frame->sig.retcode, frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 if (err == 0) {
523 /*
524 * For realtime signals we must also set the second and third
525 * arguments for the signal handler.
526 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
527 */
528 regs->ARM_r1 = (unsigned long)&frame->info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100529 regs->ARM_r2 = (unsigned long)&frame->sig.uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531
532 return err;
533}
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535/*
536 * OK, we're invoking a handler
537 */
Al Viro7e243642012-11-07 17:53:13 -0500538static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Al Virob7f9a112012-05-02 09:59:21 -0400540 sigset_t *oldset = sigmask_to_save();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 int ret;
542
543 /*
Mathieu Desnoyers9800b9d2018-06-02 08:43:55 -0400544 * Increment event counter and perform fixup for the pre-signal
545 * frame.
546 */
547 rseq_signal_deliver(regs);
548
549 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 * Set up the stack frame
551 */
Al Viro7e243642012-11-07 17:53:13 -0500552 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
553 ret = setup_rt_frame(ksig, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 else
Al Viro7e243642012-11-07 17:53:13 -0500555 ret = setup_frame(ksig, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 /*
558 * Check that the resulting registers are actually sane.
559 */
560 ret |= !valid_user_regs(regs);
561
Al Viro7e243642012-11-07 17:53:13 -0500562 signal_setup_done(ret, ksig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
565/*
566 * Note that 'init' is a special process: it doesn't get signals it doesn't
567 * want to handle. Thus you cannot kill init even with a SIGKILL even by
568 * mistake.
569 *
570 * Note that we go through the signals twice: once to check the signals that
571 * the kernel can handle, and then we build all the user-level signal handling
572 * stack-frames in one go after that.
573 */
Al Viro81783782012-07-19 17:48:21 +0100574static int do_signal(struct pt_regs *regs, int syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100576 unsigned int retval = 0, continue_addr = 0, restart_addr = 0;
Al Viro7e243642012-11-07 17:53:13 -0500577 struct ksignal ksig;
Al Viro81783782012-07-19 17:48:21 +0100578 int restart = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 /*
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100581 * If we were from a system call, check for system call restarting...
582 */
583 if (syscall) {
584 continue_addr = regs->ARM_pc;
585 restart_addr = continue_addr - (thumb_mode(regs) ? 2 : 4);
586 retval = regs->ARM_r0;
587
588 /*
589 * Prepare for system call restart. We do this here so that a
590 * debugger will see the already changed PSW.
591 */
592 switch (retval) {
Al Viro81783782012-07-19 17:48:21 +0100593 case -ERESTART_RESTARTBLOCK:
Al Viro66285212012-07-19 17:48:50 +0100594 restart -= 2;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100595 case -ERESTARTNOHAND:
596 case -ERESTARTSYS:
597 case -ERESTARTNOINTR:
Al Viro81783782012-07-19 17:48:21 +0100598 restart++;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100599 regs->ARM_r0 = regs->ARM_ORIG_r0;
600 regs->ARM_pc = restart_addr;
601 break;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100602 }
603 }
604
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100605 /*
606 * Get the signal to deliver. When running under ptrace, at this
607 * point the debugger may change all our registers ...
608 */
Al Viro81783782012-07-19 17:48:21 +0100609 /*
610 * Depending on the signal settings we may need to revert the
611 * decision to restart the system call. But skip this if a
612 * debugger has chosen to restart at a different PC.
613 */
Al Viro7e243642012-11-07 17:53:13 -0500614 if (get_signal(&ksig)) {
615 /* handler */
616 if (unlikely(restart) && regs->ARM_pc == restart_addr) {
Will Deaconad82cc02012-07-19 17:46:44 +0100617 if (retval == -ERESTARTNOHAND ||
618 retval == -ERESTART_RESTARTBLOCK
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100619 || (retval == -ERESTARTSYS
Al Viro7e243642012-11-07 17:53:13 -0500620 && !(ksig.ka.sa.sa_flags & SA_RESTART))) {
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100621 regs->ARM_r0 = -EINTR;
622 regs->ARM_pc = continue_addr;
623 }
624 }
Al Viro7e243642012-11-07 17:53:13 -0500625 handle_signal(&ksig, regs);
626 } else {
627 /* no handler */
628 restore_saved_sigmask();
629 if (unlikely(restart) && regs->ARM_pc == restart_addr) {
630 regs->ARM_pc = continue_addr;
631 return restart;
632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
Al Viro7e243642012-11-07 17:53:13 -0500634 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
Al Viro81783782012-07-19 17:48:21 +0100637asmlinkage int
Al Viro0a267fa2012-07-19 17:47:55 +0100638do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Russell King3302cad2015-08-20 16:13:37 +0100640 /*
641 * The assembly code enters us with IRQs off, but it hasn't
642 * informed the tracing code of that for efficiency reasons.
643 * Update the trace code with the current status.
644 */
645 trace_hardirqs_off();
Al Viro0a267fa2012-07-19 17:47:55 +0100646 do {
647 if (likely(thread_flags & _TIF_NEED_RESCHED)) {
648 schedule();
649 } else {
650 if (unlikely(!user_mode(regs)))
Al Viro81783782012-07-19 17:48:21 +0100651 return 0;
Al Viro0a267fa2012-07-19 17:47:55 +0100652 local_irq_enable();
653 if (thread_flags & _TIF_SIGPENDING) {
Al Viro66285212012-07-19 17:48:50 +0100654 int restart = do_signal(regs, syscall);
655 if (unlikely(restart)) {
Al Viro81783782012-07-19 17:48:21 +0100656 /*
657 * Restart without handlers.
658 * Deal with it without leaving
659 * the kernel space.
660 */
Al Viro66285212012-07-19 17:48:50 +0100661 return restart;
Al Viro81783782012-07-19 17:48:21 +0100662 }
Al Viro0a267fa2012-07-19 17:47:55 +0100663 syscall = 0;
David A. Longc7edc9e2014-03-07 11:23:04 -0500664 } else if (thread_flags & _TIF_UPROBE) {
David A. Longc7edc9e2014-03-07 11:23:04 -0500665 uprobe_notify_resume(regs);
Al Viro0a267fa2012-07-19 17:47:55 +0100666 } else {
667 clear_thread_flag(TIF_NOTIFY_RESUME);
668 tracehook_notify_resume(regs);
Mathieu Desnoyers9800b9d2018-06-02 08:43:55 -0400669 rseq_handle_notify_resume(regs);
Al Viro0a267fa2012-07-19 17:47:55 +0100670 }
671 }
672 local_irq_disable();
673 thread_flags = current_thread_info()->flags;
674 } while (thread_flags & _TIF_WORK_MASK);
Al Viro81783782012-07-19 17:48:21 +0100675 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
Russell King48be69a2013-07-24 00:29:18 +0100677
Russell King48be69a2013-07-24 00:29:18 +0100678struct page *get_signal_page(void)
679{
Russell Kinge0d40752013-08-03 10:30:05 +0100680 unsigned long ptr;
681 unsigned offset;
682 struct page *page;
683 void *addr;
Russell King48be69a2013-07-24 00:29:18 +0100684
Russell Kinge0d40752013-08-03 10:30:05 +0100685 page = alloc_pages(GFP_KERNEL, 0);
Russell King48be69a2013-07-24 00:29:18 +0100686
Russell Kinge0d40752013-08-03 10:30:05 +0100687 if (!page)
688 return NULL;
Russell King48be69a2013-07-24 00:29:18 +0100689
Russell Kinge0d40752013-08-03 10:30:05 +0100690 addr = page_address(page);
Russell King48be69a2013-07-24 00:29:18 +0100691
Russell Kinge0d40752013-08-03 10:30:05 +0100692 /* Give the signal return code some randomness */
693 offset = 0x200 + (get_random_int() & 0x7fc);
694 signal_return_offset = offset;
Russell King48be69a2013-07-24 00:29:18 +0100695
Russell Kinge0d40752013-08-03 10:30:05 +0100696 /*
697 * Copy signal return handlers into the vector page, and
698 * set sigreturn to be a pointer to these.
699 */
700 memcpy(addr + offset, sigreturn_codes, sizeof(sigreturn_codes));
Russell King48be69a2013-07-24 00:29:18 +0100701
Russell Kinge0d40752013-08-03 10:30:05 +0100702 ptr = (unsigned long)addr + offset;
703 flush_icache_range(ptr, ptr + sizeof(sigreturn_codes));
Russell King48be69a2013-07-24 00:29:18 +0100704
Russell Kinge0d40752013-08-03 10:30:05 +0100705 return page;
Russell King48be69a2013-07-24 00:29:18 +0100706}
Thomas Garniere33f8d322017-09-07 08:30:46 -0700707
708/* Defer to generic check */
709asmlinkage void addr_limit_check_failed(void)
710{
711 addr_limit_user_check();
712}