blob: b67ae12503f30c3ba113f1d8c7a943d17e9db717 [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/ucontext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/unistd.h>
Imre Deak82c6f5a2010-04-11 15:58:27 +010024#include <asm/vfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Victor Kamensky574e2b52013-08-27 22:41:57 -070026extern const unsigned long sigreturn_codes[7];
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Russell King48be69a2013-07-24 00:29:18 +010028static unsigned long signal_return_offset;
29
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010030#ifdef CONFIG_CRUNCH
Hartley Sweeten65a50532009-08-04 23:20:45 +010031static int preserve_crunch_context(struct crunch_sigframe __user *frame)
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010032{
33 char kbuf[sizeof(*frame) + 8];
34 struct crunch_sigframe *kframe;
35
36 /* the crunch context must be 64 bit aligned */
37 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
38 kframe->magic = CRUNCH_MAGIC;
39 kframe->size = CRUNCH_STORAGE_SIZE;
40 crunch_task_copy(current_thread_info(), &kframe->storage);
41 return __copy_to_user(frame, kframe, sizeof(*frame));
42}
43
Dave Martince184a02017-06-30 18:56:59 +010044static int restore_crunch_context(char __user **auxp)
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010045{
Dave Martince184a02017-06-30 18:56:59 +010046 struct crunch_sigframe __user *frame =
47 (struct crunch_sigframe __user *)*auxp;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010048 char kbuf[sizeof(*frame) + 8];
49 struct crunch_sigframe *kframe;
50
51 /* the crunch context must be 64 bit aligned */
52 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
53 if (__copy_from_user(kframe, frame, sizeof(*frame)))
54 return -1;
55 if (kframe->magic != CRUNCH_MAGIC ||
56 kframe->size != CRUNCH_STORAGE_SIZE)
57 return -1;
Dave Martince184a02017-06-30 18:56:59 +010058 *auxp += CRUNCH_STORAGE_SIZE;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010059 crunch_task_restore(current_thread_info(), &kframe->storage);
60 return 0;
61}
62#endif
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#ifdef CONFIG_IWMMXT
65
Dave Martin26958352017-06-30 18:56:09 +010066static int preserve_iwmmxt_context(struct iwmmxt_sigframe __user *frame)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Hugh Dickins69b04752005-10-29 18:16:36 -070068 char kbuf[sizeof(*frame) + 8];
69 struct iwmmxt_sigframe *kframe;
Dave Martince184a02017-06-30 18:56:59 +010070 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 /* the iWMMXt context must be 64 bit aligned */
Hugh Dickins69b04752005-10-29 18:16:36 -070073 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
Dave Martince184a02017-06-30 18:56:59 +010074
75 if (test_thread_flag(TIF_USING_IWMMXT)) {
76 kframe->magic = IWMMXT_MAGIC;
77 kframe->size = IWMMXT_STORAGE_SIZE;
78 iwmmxt_task_copy(current_thread_info(), &kframe->storage);
79
80 err = __copy_to_user(frame, kframe, sizeof(*frame));
81 } else {
82 /*
83 * For bug-compatibility with older kernels, some space
84 * has to be reserved for iWMMXt even if it's not used.
85 * Set the magic and size appropriately so that properly
86 * written userspace can skip it reliably:
87 */
88 __put_user_error(DUMMY_MAGIC, &frame->magic, err);
89 __put_user_error(IWMMXT_STORAGE_SIZE, &frame->size, err);
90 }
91
92 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Dave Martince184a02017-06-30 18:56:59 +010095static int restore_iwmmxt_context(char __user **auxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Dave Martince184a02017-06-30 18:56:59 +010097 struct iwmmxt_sigframe __user *frame =
98 (struct iwmmxt_sigframe __user *)*auxp;
Hugh Dickins69b04752005-10-29 18:16:36 -070099 char kbuf[sizeof(*frame) + 8];
100 struct iwmmxt_sigframe *kframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Hugh Dickins69b04752005-10-29 18:16:36 -0700102 /* the iWMMXt context must be 64 bit aligned */
103 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
104 if (__copy_from_user(kframe, frame, sizeof(*frame)))
105 return -1;
Dave Martince184a02017-06-30 18:56:59 +0100106
107 /*
108 * For non-iWMMXt threads: a single iwmmxt_sigframe-sized dummy
109 * block is discarded for compatibility with setup_sigframe() if
110 * present, but we don't mandate its presence. If some other
111 * magic is here, it's not for us:
112 */
113 if (!test_thread_flag(TIF_USING_IWMMXT) &&
114 kframe->magic != DUMMY_MAGIC)
115 return 0;
116
117 if (kframe->size != IWMMXT_STORAGE_SIZE)
Hugh Dickins69b04752005-10-29 18:16:36 -0700118 return -1;
Dave Martince184a02017-06-30 18:56:59 +0100119
120 if (test_thread_flag(TIF_USING_IWMMXT)) {
121 if (kframe->magic != IWMMXT_MAGIC)
122 return -1;
123
124 iwmmxt_task_restore(current_thread_info(), &kframe->storage);
125 }
126
127 *auxp += IWMMXT_STORAGE_SIZE;
Hugh Dickins69b04752005-10-29 18:16:36 -0700128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
131#endif
132
Imre Deak82c6f5a2010-04-11 15:58:27 +0100133#ifdef CONFIG_VFP
134
135static int preserve_vfp_context(struct vfp_sigframe __user *frame)
136{
Imre Deak82c6f5a2010-04-11 15:58:27 +0100137 const unsigned long magic = VFP_MAGIC;
138 const unsigned long size = VFP_STORAGE_SIZE;
139 int err = 0;
140
Imre Deak82c6f5a2010-04-11 15:58:27 +0100141 __put_user_error(magic, &frame->magic, err);
142 __put_user_error(size, &frame->size, err);
143
Will Deacon2498814f2012-04-23 15:38:28 +0100144 if (err)
145 return -EFAULT;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100146
Will Deacon2498814f2012-04-23 15:38:28 +0100147 return vfp_preserve_user_clear_hwstate(&frame->ufp, &frame->ufp_exc);
Imre Deak82c6f5a2010-04-11 15:58:27 +0100148}
149
Dave Martince184a02017-06-30 18:56:59 +0100150static int restore_vfp_context(char __user **auxp)
Imre Deak82c6f5a2010-04-11 15:58:27 +0100151{
Dave Martince184a02017-06-30 18:56:59 +0100152 struct vfp_sigframe __user *frame =
153 (struct vfp_sigframe __user *)*auxp;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100154 unsigned long magic;
155 unsigned long size;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100156 int err = 0;
157
158 __get_user_error(magic, &frame->magic, err);
159 __get_user_error(size, &frame->size, err);
160
161 if (err)
162 return -EFAULT;
163 if (magic != VFP_MAGIC || size != VFP_STORAGE_SIZE)
164 return -EINVAL;
165
Dave Martince184a02017-06-30 18:56:59 +0100166 *auxp += size;
Will Deacon2498814f2012-04-23 15:38:28 +0100167 return vfp_restore_user_hwstate(&frame->ufp, &frame->ufp_exc);
Imre Deak82c6f5a2010-04-11 15:58:27 +0100168}
169
170#endif
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * Do a signal return; undo the signal stack. These are aligned to 64-bit.
174 */
175struct sigframe {
Russell King7d4fdc12006-06-15 20:13:25 +0100176 struct ucontext uc;
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000177 unsigned long retcode[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178};
179
180struct rt_sigframe {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 struct siginfo info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100182 struct sigframe sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183};
184
Russell King68071482006-06-15 20:23:02 +0100185static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Dave Martince184a02017-06-30 18:56:59 +0100187 char __user *aux;
Russell King68071482006-06-15 20:23:02 +0100188 sigset_t set;
189 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Russell King68071482006-06-15 20:23:02 +0100191 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
Al Viro77097ae2012-04-27 13:58:59 -0400192 if (err == 0)
Matt Fleming101d9b02012-03-05 15:05:34 -0800193 set_current_blocked(&set);
Russell King68071482006-06-15 20:23:02 +0100194
195 __get_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
196 __get_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
197 __get_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
198 __get_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
199 __get_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
200 __get_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
201 __get_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
202 __get_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
203 __get_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
204 __get_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
205 __get_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
206 __get_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
207 __get_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
208 __get_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
209 __get_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
210 __get_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
211 __get_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 err |= !valid_user_regs(regs);
214
Dave Martince184a02017-06-30 18:56:59 +0100215 aux = (char __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100216#ifdef CONFIG_CRUNCH
217 if (err == 0)
Dave Martince184a02017-06-30 18:56:59 +0100218 err |= restore_crunch_context(&aux);
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100219#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220#ifdef CONFIG_IWMMXT
Dave Martince184a02017-06-30 18:56:59 +0100221 if (err == 0)
222 err |= restore_iwmmxt_context(&aux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223#endif
224#ifdef CONFIG_VFP
Imre Deak82c6f5a2010-04-11 15:58:27 +0100225 if (err == 0)
Dave Martince184a02017-06-30 18:56:59 +0100226 err |= restore_vfp_context(&aux);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227#endif
228
229 return err;
230}
231
232asmlinkage int sys_sigreturn(struct pt_regs *regs)
233{
234 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800237 current->restart_block.fn = do_no_restart_syscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 /*
240 * Since we stacked the signal on a 64-bit boundary,
241 * then 'sp' should be word aligned here. If it's
242 * not, then the user is trying to mess with us.
243 */
244 if (regs->ARM_sp & 7)
245 goto badframe;
246
247 frame = (struct sigframe __user *)regs->ARM_sp;
248
249 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
250 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Russell King68071482006-06-15 20:23:02 +0100252 if (restore_sigframe(regs, frame))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 goto badframe;
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return regs->ARM_r0;
256
257badframe:
258 force_sig(SIGSEGV, current);
259 return 0;
260}
261
262asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
263{
264 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -0800267 current->restart_block.fn = do_no_restart_syscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 /*
270 * Since we stacked the signal on a 64-bit boundary,
271 * then 'sp' should be word aligned here. If it's
272 * not, then the user is trying to mess with us.
273 */
274 if (regs->ARM_sp & 7)
275 goto badframe;
276
277 frame = (struct rt_sigframe __user *)regs->ARM_sp;
278
279 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
280 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Russell King68071482006-06-15 20:23:02 +0100282 if (restore_sigframe(regs, &frame->sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 goto badframe;
284
Al Viroec93ac82012-12-23 01:52:54 -0500285 if (restore_altstack(&frame->sig.uc.uc_stack))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 goto badframe;
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return regs->ARM_r0;
289
290badframe:
291 force_sig(SIGSEGV, current);
292 return 0;
293}
294
295static int
Russell Kingaca6ca12006-06-15 20:28:03 +0100296setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs, sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100298 struct aux_sigframe __user *aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 int err = 0;
300
Russell Kingaca6ca12006-06-15 20:28:03 +0100301 __put_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
302 __put_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
303 __put_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
304 __put_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
305 __put_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
306 __put_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
307 __put_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
308 __put_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
309 __put_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
310 __put_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
311 __put_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
312 __put_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
313 __put_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
314 __put_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
315 __put_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
316 __put_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
317 __put_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Russell Kingaca6ca12006-06-15 20:28:03 +0100319 __put_user_error(current->thread.trap_no, &sf->uc.uc_mcontext.trap_no, err);
320 __put_user_error(current->thread.error_code, &sf->uc.uc_mcontext.error_code, err);
321 __put_user_error(current->thread.address, &sf->uc.uc_mcontext.fault_address, err);
322 __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
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
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100339 __put_user_error(0, &aux->end_magic, err);
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 */
358 if (!access_ok(VERIFY_WRITE, frame, framesize))
359 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 unsigned long retcode;
370 int thumb = 0;
Russell King53399052011-02-20 12:22:52 +0000371 unsigned long cpsr = regs->ARM_cpsr & ~(PSR_f | PSR_E_BIT);
372
373 cpsr |= PSR_ENDSTATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 /*
376 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
377 */
Al Viro7e243642012-11-07 17:53:13 -0500378 if (ksig->ka.sa.sa_flags & SA_THIRTYTWO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
380
381#ifdef CONFIG_ARM_THUMB
382 if (elf_hwcap & HWCAP_THUMB) {
383 /*
384 * The LSB of the handler determines if we're going to
385 * be using THUMB or ARM mode for this signal handler.
386 */
387 thumb = handler & 1;
388
T.J. Purtell6ecf8302013-11-06 18:38:05 +0100389 /*
Russell King9b556132015-09-11 16:44:02 +0100390 * Clear the If-Then Thumb-2 execution state. ARM spec
391 * requires this to be all 000s in ARM mode. Snapdragon
392 * S4/Krait misbehaves on a Thumb=>ARM signal transition
393 * without this.
394 *
395 * We must do this whenever we are running on a Thumb-2
396 * capable CPU, which includes ARMv6T2. However, we elect
Russell King12fc7302015-09-16 11:08:49 +0100397 * to always do this to simplify the code; this field is
398 * marked UNK/SBZP for older architectures.
T.J. Purtell6ecf8302013-11-06 18:38:05 +0100399 */
400 cpsr &= ~PSR_IT_MASK;
T.J. Purtell6ecf8302013-11-06 18:38:05 +0100401
Catalin Marinasd71e1352009-05-30 14:00:15 +0100402 if (thumb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 cpsr |= PSR_T_BIT;
Catalin Marinasd71e1352009-05-30 14:00:15 +0100404 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 cpsr &= ~PSR_T_BIT;
406 }
407#endif
408
Al Viro7e243642012-11-07 17:53:13 -0500409 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
410 retcode = (unsigned long)ksig->ka.sa.sa_restorer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 } else {
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000412 unsigned int idx = thumb << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Al Viro7e243642012-11-07 17:53:13 -0500414 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000415 idx += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Jonathan Austin9dfc28b2013-04-18 18:37:24 +0100417 /*
418 * Put the sigreturn code on the stack no matter which return
419 * mechanism we use in order to remain ABI compliant
420 */
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000421 if (__put_user(sigreturn_codes[idx], rc) ||
422 __put_user(sigreturn_codes[idx+1], rc+1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return 1;
424
Russell King8c0cc8a2013-08-03 10:39:51 +0100425#ifdef CONFIG_MMU
426 if (cpsr & MODE32_BIT) {
Russell King48be69a2013-07-24 00:29:18 +0100427 struct mm_struct *mm = current->mm;
428
Russell Kinge00d3492005-06-22 20:26:05 +0100429 /*
Russell King48be69a2013-07-24 00:29:18 +0100430 * 32-bit code can use the signal return page
431 * except when the MPU has protected the vectors
432 * page from PL0
Russell Kinge00d3492005-06-22 20:26:05 +0100433 */
Russell King48be69a2013-07-24 00:29:18 +0100434 retcode = mm->context.sigpage + signal_return_offset +
435 (idx << 2) + thumb;
Russell King8c0cc8a2013-08-03 10:39:51 +0100436 } else
437#endif
438 {
Russell Kinge00d3492005-06-22 20:26:05 +0100439 /*
440 * Ensure that the instruction cache sees
441 * the return code written onto the stack.
442 */
443 flush_icache_range((unsigned long)rc,
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000444 (unsigned long)(rc + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Russell Kinge00d3492005-06-22 20:26:05 +0100446 retcode = ((unsigned long)rc) + thumb;
447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449
Richard Weinbergera4980442014-07-13 15:24:03 +0200450 regs->ARM_r0 = ksig->sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 regs->ARM_sp = (unsigned long)frame;
452 regs->ARM_lr = retcode;
453 regs->ARM_pc = handler;
454 regs->ARM_cpsr = cpsr;
455
456 return 0;
457}
458
459static int
Al Viro7e243642012-11-07 17:53:13 -0500460setup_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Al Viro7e243642012-11-07 17:53:13 -0500462 struct sigframe __user *frame = get_sigframe(ksig, regs, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 int err = 0;
464
465 if (!frame)
466 return 1;
467
Russell Kingca195cf2006-06-24 22:41:09 +0100468 /*
469 * Set uc.uc_flags to a value which sc.trap_no would never have.
470 */
471 __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Russell Kingaca6ca12006-06-15 20:28:03 +0100473 err |= setup_sigframe(frame, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (err == 0)
Al Viro7e243642012-11-07 17:53:13 -0500475 err = setup_return(regs, ksig, frame->retcode, frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 return err;
478}
479
480static int
Al Viro7e243642012-11-07 17:53:13 -0500481setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Al Viro7e243642012-11-07 17:53:13 -0500483 struct rt_sigframe __user *frame = get_sigframe(ksig, regs, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 int err = 0;
485
486 if (!frame)
487 return 1;
488
Al Viro7e243642012-11-07 17:53:13 -0500489 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Russell Kingcb3504e2006-06-15 20:18:25 +0100491 __put_user_error(0, &frame->sig.uc.uc_flags, err);
492 __put_user_error(NULL, &frame->sig.uc.uc_link, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Al Viroec93ac82012-12-23 01:52:54 -0500494 err |= __save_altstack(&frame->sig.uc.uc_stack, regs->ARM_sp);
Russell Kingaca6ca12006-06-15 20:28:03 +0100495 err |= setup_sigframe(&frame->sig, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (err == 0)
Al Viro7e243642012-11-07 17:53:13 -0500497 err = setup_return(regs, ksig, frame->sig.retcode, frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 if (err == 0) {
500 /*
501 * For realtime signals we must also set the second and third
502 * arguments for the signal handler.
503 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
504 */
505 regs->ARM_r1 = (unsigned long)&frame->info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100506 regs->ARM_r2 = (unsigned long)&frame->sig.uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508
509 return err;
510}
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512/*
513 * OK, we're invoking a handler
514 */
Al Viro7e243642012-11-07 17:53:13 -0500515static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Al Virob7f9a112012-05-02 09:59:21 -0400517 sigset_t *oldset = sigmask_to_save();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 int ret;
519
520 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 * Set up the stack frame
522 */
Al Viro7e243642012-11-07 17:53:13 -0500523 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
524 ret = setup_rt_frame(ksig, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 else
Al Viro7e243642012-11-07 17:53:13 -0500526 ret = setup_frame(ksig, oldset, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 /*
529 * Check that the resulting registers are actually sane.
530 */
531 ret |= !valid_user_regs(regs);
532
Al Viro7e243642012-11-07 17:53:13 -0500533 signal_setup_done(ret, ksig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536/*
537 * Note that 'init' is a special process: it doesn't get signals it doesn't
538 * want to handle. Thus you cannot kill init even with a SIGKILL even by
539 * mistake.
540 *
541 * Note that we go through the signals twice: once to check the signals that
542 * the kernel can handle, and then we build all the user-level signal handling
543 * stack-frames in one go after that.
544 */
Al Viro81783782012-07-19 17:48:21 +0100545static int do_signal(struct pt_regs *regs, int syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100547 unsigned int retval = 0, continue_addr = 0, restart_addr = 0;
Al Viro7e243642012-11-07 17:53:13 -0500548 struct ksignal ksig;
Al Viro81783782012-07-19 17:48:21 +0100549 int restart = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /*
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100552 * If we were from a system call, check for system call restarting...
553 */
554 if (syscall) {
555 continue_addr = regs->ARM_pc;
556 restart_addr = continue_addr - (thumb_mode(regs) ? 2 : 4);
557 retval = regs->ARM_r0;
558
559 /*
560 * Prepare for system call restart. We do this here so that a
561 * debugger will see the already changed PSW.
562 */
563 switch (retval) {
Al Viro81783782012-07-19 17:48:21 +0100564 case -ERESTART_RESTARTBLOCK:
Al Viro66285212012-07-19 17:48:50 +0100565 restart -= 2;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100566 case -ERESTARTNOHAND:
567 case -ERESTARTSYS:
568 case -ERESTARTNOINTR:
Al Viro81783782012-07-19 17:48:21 +0100569 restart++;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100570 regs->ARM_r0 = regs->ARM_ORIG_r0;
571 regs->ARM_pc = restart_addr;
572 break;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100573 }
574 }
575
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100576 /*
577 * Get the signal to deliver. When running under ptrace, at this
578 * point the debugger may change all our registers ...
579 */
Al Viro81783782012-07-19 17:48:21 +0100580 /*
581 * Depending on the signal settings we may need to revert the
582 * decision to restart the system call. But skip this if a
583 * debugger has chosen to restart at a different PC.
584 */
Al Viro7e243642012-11-07 17:53:13 -0500585 if (get_signal(&ksig)) {
586 /* handler */
587 if (unlikely(restart) && regs->ARM_pc == restart_addr) {
Will Deaconad82cc02012-07-19 17:46:44 +0100588 if (retval == -ERESTARTNOHAND ||
589 retval == -ERESTART_RESTARTBLOCK
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100590 || (retval == -ERESTARTSYS
Al Viro7e243642012-11-07 17:53:13 -0500591 && !(ksig.ka.sa.sa_flags & SA_RESTART))) {
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100592 regs->ARM_r0 = -EINTR;
593 regs->ARM_pc = continue_addr;
594 }
595 }
Al Viro7e243642012-11-07 17:53:13 -0500596 handle_signal(&ksig, regs);
597 } else {
598 /* no handler */
599 restore_saved_sigmask();
600 if (unlikely(restart) && regs->ARM_pc == restart_addr) {
601 regs->ARM_pc = continue_addr;
602 return restart;
603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
Al Viro7e243642012-11-07 17:53:13 -0500605 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Al Viro81783782012-07-19 17:48:21 +0100608asmlinkage int
Al Viro0a267fa2012-07-19 17:47:55 +0100609do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Russell King3302cad2015-08-20 16:13:37 +0100611 /*
612 * The assembly code enters us with IRQs off, but it hasn't
613 * informed the tracing code of that for efficiency reasons.
614 * Update the trace code with the current status.
615 */
616 trace_hardirqs_off();
Al Viro0a267fa2012-07-19 17:47:55 +0100617 do {
618 if (likely(thread_flags & _TIF_NEED_RESCHED)) {
619 schedule();
620 } else {
621 if (unlikely(!user_mode(regs)))
Al Viro81783782012-07-19 17:48:21 +0100622 return 0;
Al Viro0a267fa2012-07-19 17:47:55 +0100623 local_irq_enable();
624 if (thread_flags & _TIF_SIGPENDING) {
Al Viro66285212012-07-19 17:48:50 +0100625 int restart = do_signal(regs, syscall);
626 if (unlikely(restart)) {
Al Viro81783782012-07-19 17:48:21 +0100627 /*
628 * Restart without handlers.
629 * Deal with it without leaving
630 * the kernel space.
631 */
Al Viro66285212012-07-19 17:48:50 +0100632 return restart;
Al Viro81783782012-07-19 17:48:21 +0100633 }
Al Viro0a267fa2012-07-19 17:47:55 +0100634 syscall = 0;
David A. Longc7edc9e2014-03-07 11:23:04 -0500635 } else if (thread_flags & _TIF_UPROBE) {
David A. Longc7edc9e2014-03-07 11:23:04 -0500636 uprobe_notify_resume(regs);
Al Viro0a267fa2012-07-19 17:47:55 +0100637 } else {
638 clear_thread_flag(TIF_NOTIFY_RESUME);
639 tracehook_notify_resume(regs);
640 }
641 }
642 local_irq_disable();
643 thread_flags = current_thread_info()->flags;
644 } while (thread_flags & _TIF_WORK_MASK);
Al Viro81783782012-07-19 17:48:21 +0100645 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
Russell King48be69a2013-07-24 00:29:18 +0100647
Russell King48be69a2013-07-24 00:29:18 +0100648struct page *get_signal_page(void)
649{
Russell Kinge0d40752013-08-03 10:30:05 +0100650 unsigned long ptr;
651 unsigned offset;
652 struct page *page;
653 void *addr;
Russell King48be69a2013-07-24 00:29:18 +0100654
Russell Kinge0d40752013-08-03 10:30:05 +0100655 page = alloc_pages(GFP_KERNEL, 0);
Russell King48be69a2013-07-24 00:29:18 +0100656
Russell Kinge0d40752013-08-03 10:30:05 +0100657 if (!page)
658 return NULL;
Russell King48be69a2013-07-24 00:29:18 +0100659
Russell Kinge0d40752013-08-03 10:30:05 +0100660 addr = page_address(page);
Russell King48be69a2013-07-24 00:29:18 +0100661
Russell Kinge0d40752013-08-03 10:30:05 +0100662 /* Give the signal return code some randomness */
663 offset = 0x200 + (get_random_int() & 0x7fc);
664 signal_return_offset = offset;
Russell King48be69a2013-07-24 00:29:18 +0100665
Russell Kinge0d40752013-08-03 10:30:05 +0100666 /*
667 * Copy signal return handlers into the vector page, and
668 * set sigreturn to be a pointer to these.
669 */
670 memcpy(addr + offset, sigreturn_codes, sizeof(sigreturn_codes));
Russell King48be69a2013-07-24 00:29:18 +0100671
Russell Kinge0d40752013-08-03 10:30:05 +0100672 ptr = (unsigned long)addr + offset;
673 flush_icache_range(ptr, ptr + sizeof(sigreturn_codes));
Russell King48be69a2013-07-24 00:29:18 +0100674
Russell Kinge0d40752013-08-03 10:30:05 +0100675 return page;
Russell King48be69a2013-07-24 00:29:18 +0100676}
Thomas Garniere33f8d322017-09-07 08:30:46 -0700677
678/* Defer to generic check */
679asmlinkage void addr_limit_check_failed(void)
680{
681 addr_limit_user_check();
682}