blob: 07429a23c7207885b1b3366a94a99411fff62e18 [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>
11#include <linux/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/personality.h>
Russell King33fa9b12008-09-06 11:35:55 +010013#include <linux/uaccess.h>
David Howells733e5e42009-09-09 08:30:21 +010014#include <linux/tracehook.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>
18#include <asm/ucontext.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
Russell Kinge00d3492005-06-22 20:26:05 +010022#include "signal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024/*
25 * For ARM syscalls, we encode the syscall number into the instruction.
26 */
janboeee4cd582008-03-19 03:34:23 +010027#define SWI_SYS_SIGRETURN (0xef000000|(__NR_sigreturn)|(__NR_OABI_SYSCALL_BASE))
28#define SWI_SYS_RT_SIGRETURN (0xef000000|(__NR_rt_sigreturn)|(__NR_OABI_SYSCALL_BASE))
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/*
Nicolas Pitrefcca5382006-01-18 22:38:47 +000031 * With EABI, the syscall number has to be loaded into r7.
32 */
33#define MOV_R7_NR_SIGRETURN (0xe3a07000 | (__NR_sigreturn - __NR_SYSCALL_BASE))
34#define MOV_R7_NR_RT_SIGRETURN (0xe3a07000 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
35
36/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * For Thumb syscalls, we pass the syscall number via r7. We therefore
38 * need two 16-bit instructions.
39 */
40#define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (__NR_sigreturn - __NR_SYSCALL_BASE))
41#define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
42
Nicolas Pitrefcca5382006-01-18 22:38:47 +000043const unsigned long sigreturn_codes[7] = {
44 MOV_R7_NR_SIGRETURN, SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
45 MOV_R7_NR_RT_SIGRETURN, SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN,
Linus Torvalds1da177e2005-04-16 15:20:36 -070046};
47
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010048#ifdef CONFIG_CRUNCH
Hartley Sweeten65a50532009-08-04 23:20:45 +010049static int preserve_crunch_context(struct crunch_sigframe __user *frame)
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010050{
51 char kbuf[sizeof(*frame) + 8];
52 struct crunch_sigframe *kframe;
53
54 /* the crunch context must be 64 bit aligned */
55 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
56 kframe->magic = CRUNCH_MAGIC;
57 kframe->size = CRUNCH_STORAGE_SIZE;
58 crunch_task_copy(current_thread_info(), &kframe->storage);
59 return __copy_to_user(frame, kframe, sizeof(*frame));
60}
61
Hartley Sweeten65a50532009-08-04 23:20:45 +010062static int restore_crunch_context(struct crunch_sigframe __user *frame)
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +010063{
64 char kbuf[sizeof(*frame) + 8];
65 struct crunch_sigframe *kframe;
66
67 /* the crunch context must be 64 bit aligned */
68 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
69 if (__copy_from_user(kframe, frame, sizeof(*frame)))
70 return -1;
71 if (kframe->magic != CRUNCH_MAGIC ||
72 kframe->size != CRUNCH_STORAGE_SIZE)
73 return -1;
74 crunch_task_restore(current_thread_info(), &kframe->storage);
75 return 0;
76}
77#endif
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#ifdef CONFIG_IWMMXT
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static int preserve_iwmmxt_context(struct iwmmxt_sigframe *frame)
82{
Hugh Dickins69b04752005-10-29 18:16:36 -070083 char kbuf[sizeof(*frame) + 8];
84 struct iwmmxt_sigframe *kframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 /* the iWMMXt context must be 64 bit aligned */
Hugh Dickins69b04752005-10-29 18:16:36 -070087 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +010088 kframe->magic = IWMMXT_MAGIC;
89 kframe->size = IWMMXT_STORAGE_SIZE;
Hugh Dickins69b04752005-10-29 18:16:36 -070090 iwmmxt_task_copy(current_thread_info(), &kframe->storage);
91 return __copy_to_user(frame, kframe, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
94static int restore_iwmmxt_context(struct iwmmxt_sigframe *frame)
95{
Hugh Dickins69b04752005-10-29 18:16:36 -070096 char kbuf[sizeof(*frame) + 8];
97 struct iwmmxt_sigframe *kframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Hugh Dickins69b04752005-10-29 18:16:36 -070099 /* the iWMMXt context must be 64 bit aligned */
100 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
101 if (__copy_from_user(kframe, frame, sizeof(*frame)))
102 return -1;
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100103 if (kframe->magic != IWMMXT_MAGIC ||
104 kframe->size != IWMMXT_STORAGE_SIZE)
Hugh Dickins69b04752005-10-29 18:16:36 -0700105 return -1;
106 iwmmxt_task_restore(current_thread_info(), &kframe->storage);
107 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
110#endif
111
Imre Deak82c6f5a2010-04-11 15:58:27 +0100112#ifdef CONFIG_VFP
113
114static int preserve_vfp_context(struct vfp_sigframe __user *frame)
115{
Imre Deak82c6f5a2010-04-11 15:58:27 +0100116 const unsigned long magic = VFP_MAGIC;
117 const unsigned long size = VFP_STORAGE_SIZE;
118 int err = 0;
119
Imre Deak82c6f5a2010-04-11 15:58:27 +0100120 __put_user_error(magic, &frame->magic, err);
121 __put_user_error(size, &frame->size, err);
122
Will Deacon2498814f2012-04-23 15:38:28 +0100123 if (err)
124 return -EFAULT;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100125
Will Deacon2498814f2012-04-23 15:38:28 +0100126 return vfp_preserve_user_clear_hwstate(&frame->ufp, &frame->ufp_exc);
Imre Deak82c6f5a2010-04-11 15:58:27 +0100127}
128
129static int restore_vfp_context(struct vfp_sigframe __user *frame)
130{
Imre Deak82c6f5a2010-04-11 15:58:27 +0100131 unsigned long magic;
132 unsigned long size;
Imre Deak82c6f5a2010-04-11 15:58:27 +0100133 int err = 0;
134
135 __get_user_error(magic, &frame->magic, err);
136 __get_user_error(size, &frame->size, err);
137
138 if (err)
139 return -EFAULT;
140 if (magic != VFP_MAGIC || size != VFP_STORAGE_SIZE)
141 return -EINVAL;
142
Will Deacon2498814f2012-04-23 15:38:28 +0100143 return vfp_restore_user_hwstate(&frame->ufp, &frame->ufp_exc);
Imre Deak82c6f5a2010-04-11 15:58:27 +0100144}
145
146#endif
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 * Do a signal return; undo the signal stack. These are aligned to 64-bit.
150 */
151struct sigframe {
Russell King7d4fdc12006-06-15 20:13:25 +0100152 struct ucontext uc;
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000153 unsigned long retcode[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154};
155
156struct rt_sigframe {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 struct siginfo info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100158 struct sigframe sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159};
160
Russell King68071482006-06-15 20:23:02 +0100161static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100163 struct aux_sigframe __user *aux;
Russell King68071482006-06-15 20:23:02 +0100164 sigset_t set;
165 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Russell King68071482006-06-15 20:23:02 +0100167 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
Al Viro77097ae2012-04-27 13:58:59 -0400168 if (err == 0)
Matt Fleming101d9b02012-03-05 15:05:34 -0800169 set_current_blocked(&set);
Russell King68071482006-06-15 20:23:02 +0100170
171 __get_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
172 __get_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
173 __get_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
174 __get_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
175 __get_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
176 __get_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
177 __get_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
178 __get_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
179 __get_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
180 __get_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
181 __get_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
182 __get_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
183 __get_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
184 __get_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
185 __get_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
186 __get_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
187 __get_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 err |= !valid_user_regs(regs);
190
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100191 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100192#ifdef CONFIG_CRUNCH
193 if (err == 0)
194 err |= restore_crunch_context(&aux->crunch);
195#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196#ifdef CONFIG_IWMMXT
197 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
198 err |= restore_iwmmxt_context(&aux->iwmmxt);
199#endif
200#ifdef CONFIG_VFP
Imre Deak82c6f5a2010-04-11 15:58:27 +0100201 if (err == 0)
202 err |= restore_vfp_context(&aux->vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203#endif
204
205 return err;
206}
207
208asmlinkage int sys_sigreturn(struct pt_regs *regs)
209{
210 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 /* Always make any pending restarted system calls return -EINTR */
213 current_thread_info()->restart_block.fn = do_no_restart_syscall;
214
215 /*
216 * Since we stacked the signal on a 64-bit boundary,
217 * then 'sp' should be word aligned here. If it's
218 * not, then the user is trying to mess with us.
219 */
220 if (regs->ARM_sp & 7)
221 goto badframe;
222
223 frame = (struct sigframe __user *)regs->ARM_sp;
224
225 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
226 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Russell King68071482006-06-15 20:23:02 +0100228 if (restore_sigframe(regs, frame))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 goto badframe;
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return regs->ARM_r0;
232
233badframe:
234 force_sig(SIGSEGV, current);
235 return 0;
236}
237
238asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
239{
240 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 /* Always make any pending restarted system calls return -EINTR */
243 current_thread_info()->restart_block.fn = do_no_restart_syscall;
244
245 /*
246 * Since we stacked the signal on a 64-bit boundary,
247 * then 'sp' should be word aligned here. If it's
248 * not, then the user is trying to mess with us.
249 */
250 if (regs->ARM_sp & 7)
251 goto badframe;
252
253 frame = (struct rt_sigframe __user *)regs->ARM_sp;
254
255 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
256 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Russell King68071482006-06-15 20:23:02 +0100258 if (restore_sigframe(regs, &frame->sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 goto badframe;
260
Al Viroec93ac82012-12-23 01:52:54 -0500261 if (restore_altstack(&frame->sig.uc.uc_stack))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 goto badframe;
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return regs->ARM_r0;
265
266badframe:
267 force_sig(SIGSEGV, current);
268 return 0;
269}
270
271static int
Russell Kingaca6ca12006-06-15 20:28:03 +0100272setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs, sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100274 struct aux_sigframe __user *aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 int err = 0;
276
Russell Kingaca6ca12006-06-15 20:28:03 +0100277 __put_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
278 __put_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
279 __put_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
280 __put_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
281 __put_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
282 __put_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
283 __put_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
284 __put_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
285 __put_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
286 __put_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
287 __put_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
288 __put_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
289 __put_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
290 __put_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
291 __put_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
292 __put_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
293 __put_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Russell Kingaca6ca12006-06-15 20:28:03 +0100295 __put_user_error(current->thread.trap_no, &sf->uc.uc_mcontext.trap_no, err);
296 __put_user_error(current->thread.error_code, &sf->uc.uc_mcontext.error_code, err);
297 __put_user_error(current->thread.address, &sf->uc.uc_mcontext.fault_address, err);
298 __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Russell Kingaca6ca12006-06-15 20:28:03 +0100300 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100302 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100303#ifdef CONFIG_CRUNCH
304 if (err == 0)
305 err |= preserve_crunch_context(&aux->crunch);
306#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307#ifdef CONFIG_IWMMXT
308 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
309 err |= preserve_iwmmxt_context(&aux->iwmmxt);
310#endif
311#ifdef CONFIG_VFP
Imre Deak82c6f5a2010-04-11 15:58:27 +0100312 if (err == 0)
313 err |= preserve_vfp_context(&aux->vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314#endif
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100315 __put_user_error(0, &aux->end_magic, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 return err;
318}
319
320static inline void __user *
321get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
322{
323 unsigned long sp = regs->ARM_sp;
324 void __user *frame;
325
326 /*
327 * This is the X/Open sanctioned signal stack switching.
328 */
329 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
330 sp = current->sas_ss_sp + current->sas_ss_size;
331
332 /*
333 * ATPCS B01 mandates 8-byte alignment
334 */
335 frame = (void __user *)((sp - framesize) & ~7);
336
337 /*
338 * Check that we can actually write to the signal frame.
339 */
340 if (!access_ok(VERIFY_WRITE, frame, framesize))
341 frame = NULL;
342
343 return frame;
344}
345
346static int
347setup_return(struct pt_regs *regs, struct k_sigaction *ka,
348 unsigned long __user *rc, void __user *frame, int usig)
349{
350 unsigned long handler = (unsigned long)ka->sa.sa_handler;
351 unsigned long retcode;
352 int thumb = 0;
Russell King53399052011-02-20 12:22:52 +0000353 unsigned long cpsr = regs->ARM_cpsr & ~(PSR_f | PSR_E_BIT);
354
355 cpsr |= PSR_ENDSTATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 /*
358 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
359 */
360 if (ka->sa.sa_flags & SA_THIRTYTWO)
361 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
362
363#ifdef CONFIG_ARM_THUMB
364 if (elf_hwcap & HWCAP_THUMB) {
365 /*
366 * The LSB of the handler determines if we're going to
367 * be using THUMB or ARM mode for this signal handler.
368 */
369 thumb = handler & 1;
370
Catalin Marinasd71e1352009-05-30 14:00:15 +0100371 if (thumb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 cpsr |= PSR_T_BIT;
Catalin Marinasd71e1352009-05-30 14:00:15 +0100373#if __LINUX_ARM_ARCH__ >= 7
374 /* clear the If-Then Thumb-2 execution state */
375 cpsr &= ~PSR_IT_MASK;
376#endif
377 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 cpsr &= ~PSR_T_BIT;
379 }
380#endif
381
382 if (ka->sa.sa_flags & SA_RESTORER) {
383 retcode = (unsigned long)ka->sa.sa_restorer;
384 } else {
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000385 unsigned int idx = thumb << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 if (ka->sa.sa_flags & SA_SIGINFO)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000388 idx += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000390 if (__put_user(sigreturn_codes[idx], rc) ||
391 __put_user(sigreturn_codes[idx+1], rc+1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return 1;
393
Russell Kinge00d3492005-06-22 20:26:05 +0100394 if (cpsr & MODE32_BIT) {
395 /*
396 * 32-bit code can use the new high-page
397 * signal return code support.
398 */
399 retcode = KERN_SIGRETURN_CODE + (idx << 2) + thumb;
400 } else {
401 /*
402 * Ensure that the instruction cache sees
403 * the return code written onto the stack.
404 */
405 flush_icache_range((unsigned long)rc,
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000406 (unsigned long)(rc + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Russell Kinge00d3492005-06-22 20:26:05 +0100408 retcode = ((unsigned long)rc) + thumb;
409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411
412 regs->ARM_r0 = usig;
413 regs->ARM_sp = (unsigned long)frame;
414 regs->ARM_lr = retcode;
415 regs->ARM_pc = handler;
416 regs->ARM_cpsr = cpsr;
417
418 return 0;
419}
420
421static int
422setup_frame(int usig, struct k_sigaction *ka, sigset_t *set, struct pt_regs *regs)
423{
424 struct sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
425 int err = 0;
426
427 if (!frame)
428 return 1;
429
Russell Kingca195cf2006-06-24 22:41:09 +0100430 /*
431 * Set uc.uc_flags to a value which sc.trap_no would never have.
432 */
433 __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Russell Kingaca6ca12006-06-15 20:28:03 +0100435 err |= setup_sigframe(frame, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (err == 0)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000437 err = setup_return(regs, ka, frame->retcode, frame, usig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 return err;
440}
441
442static int
443setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
444 sigset_t *set, struct pt_regs *regs)
445{
446 struct rt_sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 int err = 0;
448
449 if (!frame)
450 return 1;
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 err |= copy_siginfo_to_user(&frame->info, info);
453
Russell Kingcb3504e2006-06-15 20:18:25 +0100454 __put_user_error(0, &frame->sig.uc.uc_flags, err);
455 __put_user_error(NULL, &frame->sig.uc.uc_link, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Al Viroec93ac82012-12-23 01:52:54 -0500457 err |= __save_altstack(&frame->sig.uc.uc_stack, regs->ARM_sp);
Russell Kingaca6ca12006-06-15 20:28:03 +0100458 err |= setup_sigframe(&frame->sig, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (err == 0)
Russell Kingcb3504e2006-06-15 20:18:25 +0100460 err = setup_return(regs, ka, frame->sig.retcode, frame, usig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 if (err == 0) {
463 /*
464 * For realtime signals we must also set the second and third
465 * arguments for the signal handler.
466 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
467 */
468 regs->ARM_r1 = (unsigned long)&frame->info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100469 regs->ARM_r2 = (unsigned long)&frame->sig.uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471
472 return err;
473}
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475/*
476 * OK, we're invoking a handler
477 */
Al Viroa610d6e2012-05-21 23:42:15 -0400478static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479handle_signal(unsigned long sig, struct k_sigaction *ka,
Al Virob7f9a112012-05-02 09:59:21 -0400480 siginfo_t *info, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 struct thread_info *thread = current_thread_info();
483 struct task_struct *tsk = current;
Al Virob7f9a112012-05-02 09:59:21 -0400484 sigset_t *oldset = sigmask_to_save();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 int usig = sig;
486 int ret;
487
488 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 * translate the signal
490 */
491 if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
492 usig = thread->exec_domain->signal_invmap[usig];
493
494 /*
495 * Set up the stack frame
496 */
497 if (ka->sa.sa_flags & SA_SIGINFO)
498 ret = setup_rt_frame(usig, ka, info, oldset, regs);
499 else
500 ret = setup_frame(usig, ka, oldset, regs);
501
502 /*
503 * Check that the resulting registers are actually sane.
504 */
505 ret |= !valid_user_regs(regs);
506
Steven Rostedt69be8f12005-08-29 11:44:09 -0400507 if (ret != 0) {
Daniel Jacobowitza6c61e92005-11-19 10:01:07 +0000508 force_sigsegv(sig, tsk);
Al Viroa610d6e2012-05-21 23:42:15 -0400509 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
Al Viroefee9842012-04-28 02:04:15 -0400511 signal_delivered(sig, info, ka, regs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
514/*
515 * Note that 'init' is a special process: it doesn't get signals it doesn't
516 * want to handle. Thus you cannot kill init even with a SIGKILL even by
517 * mistake.
518 *
519 * Note that we go through the signals twice: once to check the signals that
520 * the kernel can handle, and then we build all the user-level signal handling
521 * stack-frames in one go after that.
522 */
Al Viro81783782012-07-19 17:48:21 +0100523static int do_signal(struct pt_regs *regs, int syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100525 unsigned int retval = 0, continue_addr = 0, restart_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 struct k_sigaction ka;
527 siginfo_t info;
528 int signr;
Al Viro81783782012-07-19 17:48:21 +0100529 int restart = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 /*
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100532 * If we were from a system call, check for system call restarting...
533 */
534 if (syscall) {
535 continue_addr = regs->ARM_pc;
536 restart_addr = continue_addr - (thumb_mode(regs) ? 2 : 4);
537 retval = regs->ARM_r0;
538
539 /*
540 * Prepare for system call restart. We do this here so that a
541 * debugger will see the already changed PSW.
542 */
543 switch (retval) {
Al Viro81783782012-07-19 17:48:21 +0100544 case -ERESTART_RESTARTBLOCK:
Al Viro66285212012-07-19 17:48:50 +0100545 restart -= 2;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100546 case -ERESTARTNOHAND:
547 case -ERESTARTSYS:
548 case -ERESTARTNOINTR:
Al Viro81783782012-07-19 17:48:21 +0100549 restart++;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100550 regs->ARM_r0 = regs->ARM_ORIG_r0;
551 regs->ARM_pc = restart_addr;
552 break;
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100553 }
554 }
555
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100556 /*
557 * Get the signal to deliver. When running under ptrace, at this
558 * point the debugger may change all our registers ...
559 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
Al Viro81783782012-07-19 17:48:21 +0100561 /*
562 * Depending on the signal settings we may need to revert the
563 * decision to restart the system call. But skip this if a
564 * debugger has chosen to restart at a different PC.
565 */
566 if (regs->ARM_pc != restart_addr)
567 restart = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (signr > 0) {
Al Viro81783782012-07-19 17:48:21 +0100569 if (unlikely(restart)) {
Will Deaconad82cc02012-07-19 17:46:44 +0100570 if (retval == -ERESTARTNOHAND ||
571 retval == -ERESTART_RESTARTBLOCK
Arnd Bergmann2af68df2011-05-03 18:32:55 +0100572 || (retval == -ERESTARTSYS
573 && !(ka.sa.sa_flags & SA_RESTART))) {
574 regs->ARM_r0 = -EINTR;
575 regs->ARM_pc = continue_addr;
576 }
577 }
578
Al Viroa610d6e2012-05-21 23:42:15 -0400579 handle_signal(signr, &ka, &info, regs);
Al Viro81783782012-07-19 17:48:21 +0100580 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
582
Al Viro51a7b442012-05-21 23:33:55 -0400583 restore_saved_sigmask();
Al Viro66285212012-07-19 17:48:50 +0100584 if (unlikely(restart))
585 regs->ARM_pc = continue_addr;
Al Viro81783782012-07-19 17:48:21 +0100586 return restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
Al Viro81783782012-07-19 17:48:21 +0100589asmlinkage int
Al Viro0a267fa2012-07-19 17:47:55 +0100590do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Al Viro0a267fa2012-07-19 17:47:55 +0100592 do {
593 if (likely(thread_flags & _TIF_NEED_RESCHED)) {
594 schedule();
595 } else {
596 if (unlikely(!user_mode(regs)))
Al Viro81783782012-07-19 17:48:21 +0100597 return 0;
Al Viro0a267fa2012-07-19 17:47:55 +0100598 local_irq_enable();
599 if (thread_flags & _TIF_SIGPENDING) {
Al Viro66285212012-07-19 17:48:50 +0100600 int restart = do_signal(regs, syscall);
601 if (unlikely(restart)) {
Al Viro81783782012-07-19 17:48:21 +0100602 /*
603 * Restart without handlers.
604 * Deal with it without leaving
605 * the kernel space.
606 */
Al Viro66285212012-07-19 17:48:50 +0100607 return restart;
Al Viro81783782012-07-19 17:48:21 +0100608 }
Al Viro0a267fa2012-07-19 17:47:55 +0100609 syscall = 0;
610 } else {
611 clear_thread_flag(TIF_NOTIFY_RESUME);
612 tracehook_notify_resume(regs);
613 }
614 }
615 local_irq_disable();
616 thread_flags = current_thread_info()->flags;
617 } while (thread_flags & _TIF_WORK_MASK);
Al Viro81783782012-07-19 17:48:21 +0100618 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}