blob: 1423a3419789c0d2ddaacbb7ae763da8c17e55c9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/arm/kernel/signal.c
3 *
4 * Copyright (C) 1995-2002 Russell King
5 *
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>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080013#include <linux/freezer.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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Russell Kingee90dab2006-11-09 14:20:47 +000017#include <asm/elf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/cacheflush.h>
19#include <asm/ucontext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/unistd.h>
21
22#include "ptrace.h"
Russell Kinge00d3492005-06-22 20:26:05 +010023#include "signal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
26
27/*
28 * For ARM syscalls, we encode the syscall number into the instruction.
29 */
janboeee4cd582008-03-19 03:34:23 +010030#define SWI_SYS_SIGRETURN (0xef000000|(__NR_sigreturn)|(__NR_OABI_SYSCALL_BASE))
31#define SWI_SYS_RT_SIGRETURN (0xef000000|(__NR_rt_sigreturn)|(__NR_OABI_SYSCALL_BASE))
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33/*
Nicolas Pitrefcca5382006-01-18 22:38:47 +000034 * With EABI, the syscall number has to be loaded into r7.
35 */
36#define MOV_R7_NR_SIGRETURN (0xe3a07000 | (__NR_sigreturn - __NR_SYSCALL_BASE))
37#define MOV_R7_NR_RT_SIGRETURN (0xe3a07000 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
38
39/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * For Thumb syscalls, we pass the syscall number via r7. We therefore
41 * need two 16-bit instructions.
42 */
43#define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (__NR_sigreturn - __NR_SYSCALL_BASE))
44#define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
45
Nicolas Pitrefcca5382006-01-18 22:38:47 +000046const unsigned long sigreturn_codes[7] = {
47 MOV_R7_NR_SIGRETURN, SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
48 MOV_R7_NR_RT_SIGRETURN, SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN,
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * atomically swap in the new signal mask, and wait for a signal.
53 */
Mikael Pettersson36984262009-08-15 12:58:11 +010054asmlinkage int sys_sigsuspend(int restart, unsigned long oldmask, old_sigset_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 mask &= _BLOCKABLE;
57 spin_lock_irq(&current->sighand->siglock);
Mikael Pettersson36984262009-08-15 12:58:11 +010058 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 siginitset(&current->blocked, mask);
60 recalc_sigpending();
61 spin_unlock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Mikael Pettersson36984262009-08-15 12:58:11 +010063 current->state = TASK_INTERRUPTIBLE;
64 schedule();
65 set_restore_sigmask();
66 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
69asmlinkage int
70sys_sigaction(int sig, const struct old_sigaction __user *act,
71 struct old_sigaction __user *oact)
72{
73 struct k_sigaction new_ka, old_ka;
74 int ret;
75
76 if (act) {
77 old_sigset_t mask;
78 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
79 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
80 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
81 return -EFAULT;
82 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
83 __get_user(mask, &act->sa_mask);
84 siginitset(&new_ka.sa.sa_mask, mask);
85 }
86
87 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
88
89 if (!ret && oact) {
90 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
91 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
92 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
93 return -EFAULT;
94 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
95 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
96 }
97
98 return ret;
99}
100
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100101#ifdef CONFIG_CRUNCH
Hartley Sweeten65a50532009-08-04 23:20:45 +0100102static int preserve_crunch_context(struct crunch_sigframe __user *frame)
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100103{
104 char kbuf[sizeof(*frame) + 8];
105 struct crunch_sigframe *kframe;
106
107 /* the crunch context must be 64 bit aligned */
108 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
109 kframe->magic = CRUNCH_MAGIC;
110 kframe->size = CRUNCH_STORAGE_SIZE;
111 crunch_task_copy(current_thread_info(), &kframe->storage);
112 return __copy_to_user(frame, kframe, sizeof(*frame));
113}
114
Hartley Sweeten65a50532009-08-04 23:20:45 +0100115static int restore_crunch_context(struct crunch_sigframe __user *frame)
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100116{
117 char kbuf[sizeof(*frame) + 8];
118 struct crunch_sigframe *kframe;
119
120 /* the crunch context must be 64 bit aligned */
121 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
122 if (__copy_from_user(kframe, frame, sizeof(*frame)))
123 return -1;
124 if (kframe->magic != CRUNCH_MAGIC ||
125 kframe->size != CRUNCH_STORAGE_SIZE)
126 return -1;
127 crunch_task_restore(current_thread_info(), &kframe->storage);
128 return 0;
129}
130#endif
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#ifdef CONFIG_IWMMXT
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134static int preserve_iwmmxt_context(struct iwmmxt_sigframe *frame)
135{
Hugh Dickins69b04752005-10-29 18:16:36 -0700136 char kbuf[sizeof(*frame) + 8];
137 struct iwmmxt_sigframe *kframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 /* the iWMMXt context must be 64 bit aligned */
Hugh Dickins69b04752005-10-29 18:16:36 -0700140 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100141 kframe->magic = IWMMXT_MAGIC;
142 kframe->size = IWMMXT_STORAGE_SIZE;
Hugh Dickins69b04752005-10-29 18:16:36 -0700143 iwmmxt_task_copy(current_thread_info(), &kframe->storage);
144 return __copy_to_user(frame, kframe, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147static int restore_iwmmxt_context(struct iwmmxt_sigframe *frame)
148{
Hugh Dickins69b04752005-10-29 18:16:36 -0700149 char kbuf[sizeof(*frame) + 8];
150 struct iwmmxt_sigframe *kframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Hugh Dickins69b04752005-10-29 18:16:36 -0700152 /* the iWMMXt context must be 64 bit aligned */
153 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
154 if (__copy_from_user(kframe, frame, sizeof(*frame)))
155 return -1;
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100156 if (kframe->magic != IWMMXT_MAGIC ||
157 kframe->size != IWMMXT_STORAGE_SIZE)
Hugh Dickins69b04752005-10-29 18:16:36 -0700158 return -1;
159 iwmmxt_task_restore(current_thread_info(), &kframe->storage);
160 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
163#endif
164
165/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 * Do a signal return; undo the signal stack. These are aligned to 64-bit.
167 */
168struct sigframe {
Russell King7d4fdc12006-06-15 20:13:25 +0100169 struct ucontext uc;
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000170 unsigned long retcode[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171};
172
173struct rt_sigframe {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 struct siginfo info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100175 struct sigframe sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176};
177
Russell King68071482006-06-15 20:23:02 +0100178static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100180 struct aux_sigframe __user *aux;
Russell King68071482006-06-15 20:23:02 +0100181 sigset_t set;
182 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Russell King68071482006-06-15 20:23:02 +0100184 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
185 if (err == 0) {
186 sigdelsetmask(&set, ~_BLOCKABLE);
187 spin_lock_irq(&current->sighand->siglock);
188 current->blocked = set;
189 recalc_sigpending();
190 spin_unlock_irq(&current->sighand->siglock);
191 }
192
193 __get_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
194 __get_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
195 __get_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
196 __get_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
197 __get_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
198 __get_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
199 __get_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
200 __get_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
201 __get_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
202 __get_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
203 __get_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
204 __get_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
205 __get_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
206 __get_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
207 __get_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
208 __get_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
209 __get_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 err |= !valid_user_regs(regs);
212
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100213 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100214#ifdef CONFIG_CRUNCH
215 if (err == 0)
216 err |= restore_crunch_context(&aux->crunch);
217#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218#ifdef CONFIG_IWMMXT
219 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
220 err |= restore_iwmmxt_context(&aux->iwmmxt);
221#endif
222#ifdef CONFIG_VFP
223// if (err == 0)
Russell King68071482006-06-15 20:23:02 +0100224// err |= vfp_restore_state(&sf->aux.vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225#endif
226
227 return err;
228}
229
230asmlinkage int sys_sigreturn(struct pt_regs *regs)
231{
232 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 /* Always make any pending restarted system calls return -EINTR */
235 current_thread_info()->restart_block.fn = do_no_restart_syscall;
236
237 /*
238 * Since we stacked the signal on a 64-bit boundary,
239 * then 'sp' should be word aligned here. If it's
240 * not, then the user is trying to mess with us.
241 */
242 if (regs->ARM_sp & 7)
243 goto badframe;
244
245 frame = (struct sigframe __user *)regs->ARM_sp;
246
247 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
248 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Russell King68071482006-06-15 20:23:02 +0100250 if (restore_sigframe(regs, frame))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 goto badframe;
252
Russell Kingb2a0d362007-03-04 09:50:28 +0000253 single_step_trap(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 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 */
267 current_thread_info()->restart_block.fn = do_no_restart_syscall;
268
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
Russell Kingcb3504e2006-06-15 20:18:25 +0100285 if (do_sigaltstack(&frame->sig.uc.uc_stack, NULL, regs->ARM_sp) == -EFAULT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 goto badframe;
287
Russell Kingb2a0d362007-03-04 09:50:28 +0000288 single_step_trap(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 return regs->ARM_r0;
291
292badframe:
293 force_sig(SIGSEGV, current);
294 return 0;
295}
296
297static int
Russell Kingaca6ca12006-06-15 20:28:03 +0100298setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs, sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100300 struct aux_sigframe __user *aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 int err = 0;
302
Russell Kingaca6ca12006-06-15 20:28:03 +0100303 __put_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
304 __put_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
305 __put_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
306 __put_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
307 __put_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
308 __put_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
309 __put_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
310 __put_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
311 __put_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
312 __put_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
313 __put_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
314 __put_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
315 __put_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
316 __put_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
317 __put_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
318 __put_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
319 __put_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Russell Kingaca6ca12006-06-15 20:28:03 +0100321 __put_user_error(current->thread.trap_no, &sf->uc.uc_mcontext.trap_no, err);
322 __put_user_error(current->thread.error_code, &sf->uc.uc_mcontext.error_code, err);
323 __put_user_error(current->thread.address, &sf->uc.uc_mcontext.fault_address, err);
324 __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Russell Kingaca6ca12006-06-15 20:28:03 +0100326 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100328 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100329#ifdef CONFIG_CRUNCH
330 if (err == 0)
331 err |= preserve_crunch_context(&aux->crunch);
332#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333#ifdef CONFIG_IWMMXT
334 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
335 err |= preserve_iwmmxt_context(&aux->iwmmxt);
336#endif
337#ifdef CONFIG_VFP
338// if (err == 0)
Russell Kingaca6ca12006-06-15 20:28:03 +0100339// err |= vfp_save_state(&sf->aux.vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340#endif
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100341 __put_user_error(0, &aux->end_magic, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 return err;
344}
345
346static inline void __user *
347get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
348{
349 unsigned long sp = regs->ARM_sp;
350 void __user *frame;
351
352 /*
353 * This is the X/Open sanctioned signal stack switching.
354 */
355 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
356 sp = current->sas_ss_sp + current->sas_ss_size;
357
358 /*
359 * ATPCS B01 mandates 8-byte alignment
360 */
361 frame = (void __user *)((sp - framesize) & ~7);
362
363 /*
364 * Check that we can actually write to the signal frame.
365 */
366 if (!access_ok(VERIFY_WRITE, frame, framesize))
367 frame = NULL;
368
369 return frame;
370}
371
372static int
373setup_return(struct pt_regs *regs, struct k_sigaction *ka,
374 unsigned long __user *rc, void __user *frame, int usig)
375{
376 unsigned long handler = (unsigned long)ka->sa.sa_handler;
377 unsigned long retcode;
378 int thumb = 0;
379 unsigned long cpsr = regs->ARM_cpsr & ~PSR_f;
380
381 /*
382 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
383 */
384 if (ka->sa.sa_flags & SA_THIRTYTWO)
385 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
386
387#ifdef CONFIG_ARM_THUMB
388 if (elf_hwcap & HWCAP_THUMB) {
389 /*
390 * The LSB of the handler determines if we're going to
391 * be using THUMB or ARM mode for this signal handler.
392 */
393 thumb = handler & 1;
394
Catalin Marinasd71e1352009-05-30 14:00:15 +0100395 if (thumb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 cpsr |= PSR_T_BIT;
Catalin Marinasd71e1352009-05-30 14:00:15 +0100397#if __LINUX_ARM_ARCH__ >= 7
398 /* clear the If-Then Thumb-2 execution state */
399 cpsr &= ~PSR_IT_MASK;
400#endif
401 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 cpsr &= ~PSR_T_BIT;
403 }
404#endif
405
406 if (ka->sa.sa_flags & SA_RESTORER) {
407 retcode = (unsigned long)ka->sa.sa_restorer;
408 } else {
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000409 unsigned int idx = thumb << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 if (ka->sa.sa_flags & SA_SIGINFO)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000412 idx += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000414 if (__put_user(sigreturn_codes[idx], rc) ||
415 __put_user(sigreturn_codes[idx+1], rc+1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return 1;
417
Russell Kinge00d3492005-06-22 20:26:05 +0100418 if (cpsr & MODE32_BIT) {
419 /*
420 * 32-bit code can use the new high-page
421 * signal return code support.
422 */
423 retcode = KERN_SIGRETURN_CODE + (idx << 2) + thumb;
424 } else {
425 /*
426 * Ensure that the instruction cache sees
427 * the return code written onto the stack.
428 */
429 flush_icache_range((unsigned long)rc,
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000430 (unsigned long)(rc + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Russell Kinge00d3492005-06-22 20:26:05 +0100432 retcode = ((unsigned long)rc) + thumb;
433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435
436 regs->ARM_r0 = usig;
437 regs->ARM_sp = (unsigned long)frame;
438 regs->ARM_lr = retcode;
439 regs->ARM_pc = handler;
440 regs->ARM_cpsr = cpsr;
441
442 return 0;
443}
444
445static int
446setup_frame(int usig, struct k_sigaction *ka, sigset_t *set, struct pt_regs *regs)
447{
448 struct sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
449 int err = 0;
450
451 if (!frame)
452 return 1;
453
Russell Kingca195cf2006-06-24 22:41:09 +0100454 /*
455 * Set uc.uc_flags to a value which sc.trap_no would never have.
456 */
457 __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Russell Kingaca6ca12006-06-15 20:28:03 +0100459 err |= setup_sigframe(frame, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (err == 0)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000461 err = setup_return(regs, ka, frame->retcode, frame, usig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 return err;
464}
465
466static int
467setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
468 sigset_t *set, struct pt_regs *regs)
469{
470 struct rt_sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
471 stack_t stack;
472 int err = 0;
473
474 if (!frame)
475 return 1;
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 err |= copy_siginfo_to_user(&frame->info, info);
478
Russell Kingcb3504e2006-06-15 20:18:25 +0100479 __put_user_error(0, &frame->sig.uc.uc_flags, err);
480 __put_user_error(NULL, &frame->sig.uc.uc_link, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 memset(&stack, 0, sizeof(stack));
483 stack.ss_sp = (void __user *)current->sas_ss_sp;
484 stack.ss_flags = sas_ss_flags(regs->ARM_sp);
485 stack.ss_size = current->sas_ss_size;
Russell Kingcb3504e2006-06-15 20:18:25 +0100486 err |= __copy_to_user(&frame->sig.uc.uc_stack, &stack, sizeof(stack));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Russell Kingaca6ca12006-06-15 20:28:03 +0100488 err |= setup_sigframe(&frame->sig, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (err == 0)
Russell Kingcb3504e2006-06-15 20:18:25 +0100490 err = setup_return(regs, ka, frame->sig.retcode, frame, usig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 if (err == 0) {
493 /*
494 * For realtime signals we must also set the second and third
495 * arguments for the signal handler.
496 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
497 */
498 regs->ARM_r1 = (unsigned long)&frame->info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100499 regs->ARM_r2 = (unsigned long)&frame->sig.uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501
502 return err;
503}
504
Eric W. Biederman288ddad2009-05-20 15:52:40 -0700505static inline void setup_syscall_restart(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 regs->ARM_r0 = regs->ARM_ORIG_r0;
508 regs->ARM_pc -= thumb_mode(regs) ? 2 : 4;
509}
510
511/*
512 * OK, we're invoking a handler
513 */
Mikael Pettersson36984262009-08-15 12:58:11 +0100514static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515handle_signal(unsigned long sig, struct k_sigaction *ka,
516 siginfo_t *info, sigset_t *oldset,
517 struct pt_regs * regs, int syscall)
518{
519 struct thread_info *thread = current_thread_info();
520 struct task_struct *tsk = current;
521 int usig = sig;
522 int ret;
523
524 /*
525 * If we were from a system call, check for system call restarting...
526 */
527 if (syscall) {
528 switch (regs->ARM_r0) {
529 case -ERESTART_RESTARTBLOCK:
530 case -ERESTARTNOHAND:
531 regs->ARM_r0 = -EINTR;
532 break;
533 case -ERESTARTSYS:
534 if (!(ka->sa.sa_flags & SA_RESTART)) {
535 regs->ARM_r0 = -EINTR;
536 break;
537 }
538 /* fallthrough */
539 case -ERESTARTNOINTR:
Eric W. Biederman288ddad2009-05-20 15:52:40 -0700540 setup_syscall_restart(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
542 }
543
544 /*
545 * translate the signal
546 */
547 if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
548 usig = thread->exec_domain->signal_invmap[usig];
549
550 /*
551 * Set up the stack frame
552 */
553 if (ka->sa.sa_flags & SA_SIGINFO)
554 ret = setup_rt_frame(usig, ka, info, oldset, regs);
555 else
556 ret = setup_frame(usig, ka, oldset, regs);
557
558 /*
559 * Check that the resulting registers are actually sane.
560 */
561 ret |= !valid_user_regs(regs);
562
Steven Rostedt69be8f12005-08-29 11:44:09 -0400563 if (ret != 0) {
Daniel Jacobowitza6c61e92005-11-19 10:01:07 +0000564 force_sigsegv(sig, tsk);
Mikael Pettersson36984262009-08-15 12:58:11 +0100565 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567
Daniel Jacobowitza6c61e92005-11-19 10:01:07 +0000568 /*
569 * Block the signal if we were successful.
570 */
571 spin_lock_irq(&tsk->sighand->siglock);
572 sigorsets(&tsk->blocked, &tsk->blocked,
573 &ka->sa.sa_mask);
574 if (!(ka->sa.sa_flags & SA_NODEFER))
575 sigaddset(&tsk->blocked, sig);
576 recalc_sigpending();
577 spin_unlock_irq(&tsk->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Mikael Pettersson36984262009-08-15 12:58:11 +0100579 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
582/*
583 * Note that 'init' is a special process: it doesn't get signals it doesn't
584 * want to handle. Thus you cannot kill init even with a SIGKILL even by
585 * mistake.
586 *
587 * Note that we go through the signals twice: once to check the signals that
588 * the kernel can handle, and then we build all the user-level signal handling
589 * stack-frames in one go after that.
590 */
Mikael Pettersson36984262009-08-15 12:58:11 +0100591static void do_signal(struct pt_regs *regs, int syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 struct k_sigaction ka;
594 siginfo_t info;
595 int signr;
596
597 /*
598 * We want the common case to go fast, which
599 * is why we may in certain cases get here from
600 * kernel mode. Just return without doing anything
601 * if so.
602 */
603 if (!user_mode(regs))
Mikael Pettersson36984262009-08-15 12:58:11 +0100604 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Andrew Mortonbdb94f32005-06-26 03:27:21 -0700606 if (try_to_freeze())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 goto no_signal;
608
Russell Kingb2a0d362007-03-04 09:50:28 +0000609 single_step_clear(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
612 if (signr > 0) {
Mikael Pettersson36984262009-08-15 12:58:11 +0100613 sigset_t *oldset;
614
615 if (test_thread_flag(TIF_RESTORE_SIGMASK))
616 oldset = &current->saved_sigmask;
617 else
618 oldset = &current->blocked;
619 if (handle_signal(signr, &ka, &info, oldset, regs, syscall) == 0) {
620 /*
621 * A signal was successfully delivered; the saved
622 * sigmask will have been stored in the signal frame,
623 * and will be restored by sigreturn, so we can simply
624 * clear the TIF_RESTORE_SIGMASK flag.
625 */
626 if (test_thread_flag(TIF_RESTORE_SIGMASK))
627 clear_thread_flag(TIF_RESTORE_SIGMASK);
628 }
Russell Kingb2a0d362007-03-04 09:50:28 +0000629 single_step_set(current);
Mikael Pettersson36984262009-08-15 12:58:11 +0100630 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632
633 no_signal:
634 /*
635 * No signal to deliver to the process - restart the syscall.
636 */
637 if (syscall) {
638 if (regs->ARM_r0 == -ERESTART_RESTARTBLOCK) {
639 if (thumb_mode(regs)) {
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100640 regs->ARM_r7 = __NR_restart_syscall - __NR_SYSCALL_BASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 regs->ARM_pc -= 2;
642 } else {
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100643#if defined(CONFIG_AEABI) && !defined(CONFIG_OABI_COMPAT)
644 regs->ARM_r7 = __NR_restart_syscall;
645 regs->ARM_pc -= 4;
646#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 u32 __user *usp;
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100648 u32 swival = __NR_restart_syscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 regs->ARM_sp -= 12;
651 usp = (u32 __user *)regs->ARM_sp;
652
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100653 /*
654 * Either we supports OABI only, or we have
655 * EABI with the OABI compat layer enabled.
656 * In the later case we don't know if user
657 * space is EABI or not, and if not we must
658 * not clobber r7. Always using the OABI
659 * syscall solves that issue and works for
660 * all those cases.
661 */
Nicolas Pitre95eaa5f2006-06-23 11:40:53 -0400662 swival = swival - __NR_SYSCALL_BASE + __NR_OABI_SYSCALL_BASE;
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 put_user(regs->ARM_pc, &usp[0]);
665 /* swi __NR_restart_syscall */
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100666 put_user(0xef000000 | swival, &usp[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 /* ldr pc, [sp], #12 */
668 put_user(0xe49df00c, &usp[2]);
669
670 flush_icache_range((unsigned long)usp,
671 (unsigned long)(usp + 3));
672
673 regs->ARM_pc = regs->ARM_sp + 4;
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100674#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676 }
677 if (regs->ARM_r0 == -ERESTARTNOHAND ||
678 regs->ARM_r0 == -ERESTARTSYS ||
679 regs->ARM_r0 == -ERESTARTNOINTR) {
Eric W. Biederman288ddad2009-05-20 15:52:40 -0700680 setup_syscall_restart(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
Mikael Pettersson36984262009-08-15 12:58:11 +0100682
683 /* If there's no signal to deliver, we just put the saved sigmask
684 * back.
685 */
686 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
687 clear_thread_flag(TIF_RESTORE_SIGMASK);
688 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
Russell Kingb2a0d362007-03-04 09:50:28 +0000691 single_step_set(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
694asmlinkage void
695do_notify_resume(struct pt_regs *regs, unsigned int thread_flags, int syscall)
696{
697 if (thread_flags & _TIF_SIGPENDING)
Mikael Pettersson36984262009-08-15 12:58:11 +0100698 do_signal(regs, syscall);
David Howellsd0420c82009-09-02 09:14:16 +0100699
700 if (thread_flags & _TIF_NOTIFY_RESUME) {
701 clear_thread_flag(TIF_NOTIFY_RESUME);
702 tracehook_notify_resume(regs);
David Howellsee18d642009-09-02 09:14:21 +0100703 if (current->replacement_session_keyring)
704 key_replace_session_keyring();
David Howellsd0420c82009-09-02 09:14:16 +0100705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}