blob: e27ee1f701d5c75140148d771761a624a374698b [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>
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>
20
21#include "ptrace.h"
Russell Kinge00d3492005-06-22 20:26:05 +010022#include "signal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
25
26/*
27 * For ARM syscalls, we encode the syscall number into the instruction.
28 */
janboeee4cd582008-03-19 03:34:23 +010029#define SWI_SYS_SIGRETURN (0xef000000|(__NR_sigreturn)|(__NR_OABI_SYSCALL_BASE))
30#define SWI_SYS_RT_SIGRETURN (0xef000000|(__NR_rt_sigreturn)|(__NR_OABI_SYSCALL_BASE))
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32/*
Nicolas Pitrefcca5382006-01-18 22:38:47 +000033 * With EABI, the syscall number has to be loaded into r7.
34 */
35#define MOV_R7_NR_SIGRETURN (0xe3a07000 | (__NR_sigreturn - __NR_SYSCALL_BASE))
36#define MOV_R7_NR_RT_SIGRETURN (0xe3a07000 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
37
38/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * For Thumb syscalls, we pass the syscall number via r7. We therefore
40 * need two 16-bit instructions.
41 */
42#define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (__NR_sigreturn - __NR_SYSCALL_BASE))
43#define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
44
Nicolas Pitrefcca5382006-01-18 22:38:47 +000045const unsigned long sigreturn_codes[7] = {
46 MOV_R7_NR_SIGRETURN, SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
47 MOV_R7_NR_RT_SIGRETURN, SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN,
Linus Torvalds1da177e2005-04-16 15:20:36 -070048};
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/*
51 * atomically swap in the new signal mask, and wait for a signal.
52 */
Mikael Pettersson36984262009-08-15 12:58:11 +010053asmlinkage int sys_sigsuspend(int restart, unsigned long oldmask, old_sigset_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 mask &= _BLOCKABLE;
56 spin_lock_irq(&current->sighand->siglock);
Mikael Pettersson36984262009-08-15 12:58:11 +010057 current->saved_sigmask = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 siginitset(&current->blocked, mask);
59 recalc_sigpending();
60 spin_unlock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Mikael Pettersson36984262009-08-15 12:58:11 +010062 current->state = TASK_INTERRUPTIBLE;
63 schedule();
64 set_restore_sigmask();
65 return -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
68asmlinkage int
69sys_sigaction(int sig, const struct old_sigaction __user *act,
70 struct old_sigaction __user *oact)
71{
72 struct k_sigaction new_ka, old_ka;
73 int ret;
74
75 if (act) {
76 old_sigset_t mask;
77 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
78 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
79 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
80 return -EFAULT;
81 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
82 __get_user(mask, &act->sa_mask);
83 siginitset(&new_ka.sa.sa_mask, mask);
84 }
85
86 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
87
88 if (!ret && oact) {
89 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
90 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
91 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
92 return -EFAULT;
93 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
94 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
95 }
96
97 return ret;
98}
99
Lennert Buytenhek3bec6de2006-06-27 22:56:18 +0100100#ifdef CONFIG_CRUNCH
101static int preserve_crunch_context(struct crunch_sigframe *frame)
102{
103 char kbuf[sizeof(*frame) + 8];
104 struct crunch_sigframe *kframe;
105
106 /* the crunch context must be 64 bit aligned */
107 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
108 kframe->magic = CRUNCH_MAGIC;
109 kframe->size = CRUNCH_STORAGE_SIZE;
110 crunch_task_copy(current_thread_info(), &kframe->storage);
111 return __copy_to_user(frame, kframe, sizeof(*frame));
112}
113
114static int restore_crunch_context(struct crunch_sigframe *frame)
115{
116 char kbuf[sizeof(*frame) + 8];
117 struct crunch_sigframe *kframe;
118
119 /* the crunch context must be 64 bit aligned */
120 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
121 if (__copy_from_user(kframe, frame, sizeof(*frame)))
122 return -1;
123 if (kframe->magic != CRUNCH_MAGIC ||
124 kframe->size != CRUNCH_STORAGE_SIZE)
125 return -1;
126 crunch_task_restore(current_thread_info(), &kframe->storage);
127 return 0;
128}
129#endif
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#ifdef CONFIG_IWMMXT
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133static int preserve_iwmmxt_context(struct iwmmxt_sigframe *frame)
134{
Hugh Dickins69b04752005-10-29 18:16:36 -0700135 char kbuf[sizeof(*frame) + 8];
136 struct iwmmxt_sigframe *kframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 /* the iWMMXt context must be 64 bit aligned */
Hugh Dickins69b04752005-10-29 18:16:36 -0700139 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100140 kframe->magic = IWMMXT_MAGIC;
141 kframe->size = IWMMXT_STORAGE_SIZE;
Hugh Dickins69b04752005-10-29 18:16:36 -0700142 iwmmxt_task_copy(current_thread_info(), &kframe->storage);
143 return __copy_to_user(frame, kframe, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
146static int restore_iwmmxt_context(struct iwmmxt_sigframe *frame)
147{
Hugh Dickins69b04752005-10-29 18:16:36 -0700148 char kbuf[sizeof(*frame) + 8];
149 struct iwmmxt_sigframe *kframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Hugh Dickins69b04752005-10-29 18:16:36 -0700151 /* the iWMMXt context must be 64 bit aligned */
152 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
153 if (__copy_from_user(kframe, frame, sizeof(*frame)))
154 return -1;
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100155 if (kframe->magic != IWMMXT_MAGIC ||
156 kframe->size != IWMMXT_STORAGE_SIZE)
Hugh Dickins69b04752005-10-29 18:16:36 -0700157 return -1;
158 iwmmxt_task_restore(current_thread_info(), &kframe->storage);
159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
162#endif
163
164/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 * Do a signal return; undo the signal stack. These are aligned to 64-bit.
166 */
167struct sigframe {
Russell King7d4fdc12006-06-15 20:13:25 +0100168 struct ucontext uc;
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000169 unsigned long retcode[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170};
171
172struct rt_sigframe {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 struct siginfo info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100174 struct sigframe sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175};
176
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{
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100179 struct aux_sigframe __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));
184 if (err == 0) {
185 sigdelsetmask(&set, ~_BLOCKABLE);
186 spin_lock_irq(&current->sighand->siglock);
187 current->blocked = set;
188 recalc_sigpending();
189 spin_unlock_irq(&current->sighand->siglock);
190 }
191
192 __get_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
193 __get_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
194 __get_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
195 __get_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
196 __get_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
197 __get_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
198 __get_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
199 __get_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
200 __get_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
201 __get_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
202 __get_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
203 __get_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
204 __get_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
205 __get_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
206 __get_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
207 __get_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
208 __get_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 err |= !valid_user_regs(regs);
211
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100212 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6de2006-06-27 22:56:18 +0100213#ifdef CONFIG_CRUNCH
214 if (err == 0)
215 err |= restore_crunch_context(&aux->crunch);
216#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217#ifdef CONFIG_IWMMXT
218 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
219 err |= restore_iwmmxt_context(&aux->iwmmxt);
220#endif
221#ifdef CONFIG_VFP
222// if (err == 0)
Russell King68071482006-06-15 20:23:02 +0100223// err |= vfp_restore_state(&sf->aux.vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224#endif
225
226 return err;
227}
228
229asmlinkage int sys_sigreturn(struct pt_regs *regs)
230{
231 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 /* Always make any pending restarted system calls return -EINTR */
234 current_thread_info()->restart_block.fn = do_no_restart_syscall;
235
236 /*
237 * Since we stacked the signal on a 64-bit boundary,
238 * then 'sp' should be word aligned here. If it's
239 * not, then the user is trying to mess with us.
240 */
241 if (regs->ARM_sp & 7)
242 goto badframe;
243
244 frame = (struct sigframe __user *)regs->ARM_sp;
245
246 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
247 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Russell King68071482006-06-15 20:23:02 +0100249 if (restore_sigframe(regs, frame))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 goto badframe;
251
Russell Kingb2a0d362007-03-04 09:50:28 +0000252 single_step_trap(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 return regs->ARM_r0;
255
256badframe:
257 force_sig(SIGSEGV, current);
258 return 0;
259}
260
261asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
262{
263 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 /* Always make any pending restarted system calls return -EINTR */
266 current_thread_info()->restart_block.fn = do_no_restart_syscall;
267
268 /*
269 * Since we stacked the signal on a 64-bit boundary,
270 * then 'sp' should be word aligned here. If it's
271 * not, then the user is trying to mess with us.
272 */
273 if (regs->ARM_sp & 7)
274 goto badframe;
275
276 frame = (struct rt_sigframe __user *)regs->ARM_sp;
277
278 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
279 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Russell King68071482006-06-15 20:23:02 +0100281 if (restore_sigframe(regs, &frame->sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 goto badframe;
283
Russell Kingcb3504e2006-06-15 20:18:25 +0100284 if (do_sigaltstack(&frame->sig.uc.uc_stack, NULL, regs->ARM_sp) == -EFAULT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 goto badframe;
286
Russell Kingb2a0d362007-03-04 09:50:28 +0000287 single_step_trap(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 return regs->ARM_r0;
290
291badframe:
292 force_sig(SIGSEGV, current);
293 return 0;
294}
295
296static int
Russell Kingaca6ca12006-06-15 20:28:03 +0100297setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs, sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100299 struct aux_sigframe __user *aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 int err = 0;
301
Russell Kingaca6ca12006-06-15 20:28:03 +0100302 __put_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
303 __put_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
304 __put_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
305 __put_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
306 __put_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
307 __put_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
308 __put_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
309 __put_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
310 __put_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
311 __put_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
312 __put_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
313 __put_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
314 __put_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
315 __put_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
316 __put_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
317 __put_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
318 __put_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Russell Kingaca6ca12006-06-15 20:28:03 +0100320 __put_user_error(current->thread.trap_no, &sf->uc.uc_mcontext.trap_no, err);
321 __put_user_error(current->thread.error_code, &sf->uc.uc_mcontext.error_code, err);
322 __put_user_error(current->thread.address, &sf->uc.uc_mcontext.fault_address, err);
323 __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Russell Kingaca6ca12006-06-15 20:28:03 +0100325 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100327 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6de2006-06-27 22:56:18 +0100328#ifdef CONFIG_CRUNCH
329 if (err == 0)
330 err |= preserve_crunch_context(&aux->crunch);
331#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332#ifdef CONFIG_IWMMXT
333 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
334 err |= preserve_iwmmxt_context(&aux->iwmmxt);
335#endif
336#ifdef CONFIG_VFP
337// if (err == 0)
Russell Kingaca6ca12006-06-15 20:28:03 +0100338// err |= vfp_save_state(&sf->aux.vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339#endif
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100340 __put_user_error(0, &aux->end_magic, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 return err;
343}
344
345static inline void __user *
346get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
347{
348 unsigned long sp = regs->ARM_sp;
349 void __user *frame;
350
351 /*
352 * This is the X/Open sanctioned signal stack switching.
353 */
354 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
355 sp = current->sas_ss_sp + current->sas_ss_size;
356
357 /*
358 * ATPCS B01 mandates 8-byte alignment
359 */
360 frame = (void __user *)((sp - framesize) & ~7);
361
362 /*
363 * Check that we can actually write to the signal frame.
364 */
365 if (!access_ok(VERIFY_WRITE, frame, framesize))
366 frame = NULL;
367
368 return frame;
369}
370
371static int
372setup_return(struct pt_regs *regs, struct k_sigaction *ka,
373 unsigned long __user *rc, void __user *frame, int usig)
374{
375 unsigned long handler = (unsigned long)ka->sa.sa_handler;
376 unsigned long retcode;
377 int thumb = 0;
378 unsigned long cpsr = regs->ARM_cpsr & ~PSR_f;
379
380 /*
381 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
382 */
383 if (ka->sa.sa_flags & SA_THIRTYTWO)
384 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
385
386#ifdef CONFIG_ARM_THUMB
387 if (elf_hwcap & HWCAP_THUMB) {
388 /*
389 * The LSB of the handler determines if we're going to
390 * be using THUMB or ARM mode for this signal handler.
391 */
392 thumb = handler & 1;
393
Catalin Marinasd71e1352009-05-30 14:00:15 +0100394 if (thumb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 cpsr |= PSR_T_BIT;
Catalin Marinasd71e1352009-05-30 14:00:15 +0100396#if __LINUX_ARM_ARCH__ >= 7
397 /* clear the If-Then Thumb-2 execution state */
398 cpsr &= ~PSR_IT_MASK;
399#endif
400 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 cpsr &= ~PSR_T_BIT;
402 }
403#endif
404
405 if (ka->sa.sa_flags & SA_RESTORER) {
406 retcode = (unsigned long)ka->sa.sa_restorer;
407 } else {
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000408 unsigned int idx = thumb << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 if (ka->sa.sa_flags & SA_SIGINFO)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000411 idx += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000413 if (__put_user(sigreturn_codes[idx], rc) ||
414 __put_user(sigreturn_codes[idx+1], rc+1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return 1;
416
Russell Kinge00d3492005-06-22 20:26:05 +0100417 if (cpsr & MODE32_BIT) {
418 /*
419 * 32-bit code can use the new high-page
420 * signal return code support.
421 */
422 retcode = KERN_SIGRETURN_CODE + (idx << 2) + thumb;
423 } else {
424 /*
425 * Ensure that the instruction cache sees
426 * the return code written onto the stack.
427 */
428 flush_icache_range((unsigned long)rc,
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000429 (unsigned long)(rc + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Russell Kinge00d3492005-06-22 20:26:05 +0100431 retcode = ((unsigned long)rc) + thumb;
432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434
435 regs->ARM_r0 = usig;
436 regs->ARM_sp = (unsigned long)frame;
437 regs->ARM_lr = retcode;
438 regs->ARM_pc = handler;
439 regs->ARM_cpsr = cpsr;
440
441 return 0;
442}
443
444static int
445setup_frame(int usig, struct k_sigaction *ka, sigset_t *set, struct pt_regs *regs)
446{
447 struct sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
448 int err = 0;
449
450 if (!frame)
451 return 1;
452
Russell Kingca195cf2006-06-24 22:41:09 +0100453 /*
454 * Set uc.uc_flags to a value which sc.trap_no would never have.
455 */
456 __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Russell Kingaca6ca12006-06-15 20:28:03 +0100458 err |= setup_sigframe(frame, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (err == 0)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000460 err = setup_return(regs, ka, frame->retcode, frame, usig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 return err;
463}
464
465static int
466setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
467 sigset_t *set, struct pt_regs *regs)
468{
469 struct rt_sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
470 stack_t stack;
471 int err = 0;
472
473 if (!frame)
474 return 1;
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 err |= copy_siginfo_to_user(&frame->info, info);
477
Russell Kingcb3504e2006-06-15 20:18:25 +0100478 __put_user_error(0, &frame->sig.uc.uc_flags, err);
479 __put_user_error(NULL, &frame->sig.uc.uc_link, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 memset(&stack, 0, sizeof(stack));
482 stack.ss_sp = (void __user *)current->sas_ss_sp;
483 stack.ss_flags = sas_ss_flags(regs->ARM_sp);
484 stack.ss_size = current->sas_ss_size;
Russell Kingcb3504e2006-06-15 20:18:25 +0100485 err |= __copy_to_user(&frame->sig.uc.uc_stack, &stack, sizeof(stack));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Russell Kingaca6ca12006-06-15 20:28:03 +0100487 err |= setup_sigframe(&frame->sig, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (err == 0)
Russell Kingcb3504e2006-06-15 20:18:25 +0100489 err = setup_return(regs, ka, frame->sig.retcode, frame, usig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 if (err == 0) {
492 /*
493 * For realtime signals we must also set the second and third
494 * arguments for the signal handler.
495 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
496 */
497 regs->ARM_r1 = (unsigned long)&frame->info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100498 regs->ARM_r2 = (unsigned long)&frame->sig.uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
500
501 return err;
502}
503
Eric W. Biederman288ddad2009-05-20 15:52:40 -0700504static inline void setup_syscall_restart(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 regs->ARM_r0 = regs->ARM_ORIG_r0;
507 regs->ARM_pc -= thumb_mode(regs) ? 2 : 4;
508}
509
510/*
511 * OK, we're invoking a handler
512 */
Mikael Pettersson36984262009-08-15 12:58:11 +0100513static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514handle_signal(unsigned long sig, struct k_sigaction *ka,
515 siginfo_t *info, sigset_t *oldset,
516 struct pt_regs * regs, int syscall)
517{
518 struct thread_info *thread = current_thread_info();
519 struct task_struct *tsk = current;
520 int usig = sig;
521 int ret;
522
523 /*
524 * If we were from a system call, check for system call restarting...
525 */
526 if (syscall) {
527 switch (regs->ARM_r0) {
528 case -ERESTART_RESTARTBLOCK:
529 case -ERESTARTNOHAND:
530 regs->ARM_r0 = -EINTR;
531 break;
532 case -ERESTARTSYS:
533 if (!(ka->sa.sa_flags & SA_RESTART)) {
534 regs->ARM_r0 = -EINTR;
535 break;
536 }
537 /* fallthrough */
538 case -ERESTARTNOINTR:
Eric W. Biederman288ddad2009-05-20 15:52:40 -0700539 setup_syscall_restart(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
541 }
542
543 /*
544 * translate the signal
545 */
546 if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
547 usig = thread->exec_domain->signal_invmap[usig];
548
549 /*
550 * Set up the stack frame
551 */
552 if (ka->sa.sa_flags & SA_SIGINFO)
553 ret = setup_rt_frame(usig, ka, info, oldset, regs);
554 else
555 ret = setup_frame(usig, ka, oldset, regs);
556
557 /*
558 * Check that the resulting registers are actually sane.
559 */
560 ret |= !valid_user_regs(regs);
561
Steven Rostedt69be8f12005-08-29 11:44:09 -0400562 if (ret != 0) {
Daniel Jacobowitza6c61e92005-11-19 10:01:07 +0000563 force_sigsegv(sig, tsk);
Mikael Pettersson36984262009-08-15 12:58:11 +0100564 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566
Daniel Jacobowitza6c61e92005-11-19 10:01:07 +0000567 /*
568 * Block the signal if we were successful.
569 */
570 spin_lock_irq(&tsk->sighand->siglock);
571 sigorsets(&tsk->blocked, &tsk->blocked,
572 &ka->sa.sa_mask);
573 if (!(ka->sa.sa_flags & SA_NODEFER))
574 sigaddset(&tsk->blocked, sig);
575 recalc_sigpending();
576 spin_unlock_irq(&tsk->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Mikael Pettersson36984262009-08-15 12:58:11 +0100578 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
581/*
582 * Note that 'init' is a special process: it doesn't get signals it doesn't
583 * want to handle. Thus you cannot kill init even with a SIGKILL even by
584 * mistake.
585 *
586 * Note that we go through the signals twice: once to check the signals that
587 * the kernel can handle, and then we build all the user-level signal handling
588 * stack-frames in one go after that.
589 */
Mikael Pettersson36984262009-08-15 12:58:11 +0100590static void do_signal(struct pt_regs *regs, int syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 struct k_sigaction ka;
593 siginfo_t info;
594 int signr;
595
596 /*
597 * We want the common case to go fast, which
598 * is why we may in certain cases get here from
599 * kernel mode. Just return without doing anything
600 * if so.
601 */
602 if (!user_mode(regs))
Mikael Pettersson36984262009-08-15 12:58:11 +0100603 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Andrew Mortonbdb94f32005-06-26 03:27:21 -0700605 if (try_to_freeze())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 goto no_signal;
607
Russell Kingb2a0d362007-03-04 09:50:28 +0000608 single_step_clear(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
611 if (signr > 0) {
Mikael Pettersson36984262009-08-15 12:58:11 +0100612 sigset_t *oldset;
613
614 if (test_thread_flag(TIF_RESTORE_SIGMASK))
615 oldset = &current->saved_sigmask;
616 else
617 oldset = &current->blocked;
618 if (handle_signal(signr, &ka, &info, oldset, regs, syscall) == 0) {
619 /*
620 * A signal was successfully delivered; the saved
621 * sigmask will have been stored in the signal frame,
622 * and will be restored by sigreturn, so we can simply
623 * clear the TIF_RESTORE_SIGMASK flag.
624 */
625 if (test_thread_flag(TIF_RESTORE_SIGMASK))
626 clear_thread_flag(TIF_RESTORE_SIGMASK);
627 }
Russell Kingb2a0d362007-03-04 09:50:28 +0000628 single_step_set(current);
Mikael Pettersson36984262009-08-15 12:58:11 +0100629 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
631
632 no_signal:
633 /*
634 * No signal to deliver to the process - restart the syscall.
635 */
636 if (syscall) {
637 if (regs->ARM_r0 == -ERESTART_RESTARTBLOCK) {
638 if (thumb_mode(regs)) {
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100639 regs->ARM_r7 = __NR_restart_syscall - __NR_SYSCALL_BASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 regs->ARM_pc -= 2;
641 } else {
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100642#if defined(CONFIG_AEABI) && !defined(CONFIG_OABI_COMPAT)
643 regs->ARM_r7 = __NR_restart_syscall;
644 regs->ARM_pc -= 4;
645#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 u32 __user *usp;
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100647 u32 swival = __NR_restart_syscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 regs->ARM_sp -= 12;
650 usp = (u32 __user *)regs->ARM_sp;
651
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100652 /*
653 * Either we supports OABI only, or we have
654 * EABI with the OABI compat layer enabled.
655 * In the later case we don't know if user
656 * space is EABI or not, and if not we must
657 * not clobber r7. Always using the OABI
658 * syscall solves that issue and works for
659 * all those cases.
660 */
Nicolas Pitre95eaa5f2006-06-23 11:40:53 -0400661 swival = swival - __NR_SYSCALL_BASE + __NR_OABI_SYSCALL_BASE;
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 put_user(regs->ARM_pc, &usp[0]);
664 /* swi __NR_restart_syscall */
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100665 put_user(0xef000000 | swival, &usp[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 /* ldr pc, [sp], #12 */
667 put_user(0xe49df00c, &usp[2]);
668
669 flush_icache_range((unsigned long)usp,
670 (unsigned long)(usp + 3));
671
672 regs->ARM_pc = regs->ARM_sp + 4;
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100673#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
675 }
676 if (regs->ARM_r0 == -ERESTARTNOHAND ||
677 regs->ARM_r0 == -ERESTARTSYS ||
678 regs->ARM_r0 == -ERESTARTNOINTR) {
Eric W. Biederman288ddad2009-05-20 15:52:40 -0700679 setup_syscall_restart(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
Mikael Pettersson36984262009-08-15 12:58:11 +0100681
682 /* If there's no signal to deliver, we just put the saved sigmask
683 * back.
684 */
685 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
686 clear_thread_flag(TIF_RESTORE_SIGMASK);
687 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
Russell Kingb2a0d362007-03-04 09:50:28 +0000690 single_step_set(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
693asmlinkage void
694do_notify_resume(struct pt_regs *regs, unsigned int thread_flags, int syscall)
695{
696 if (thread_flags & _TIF_SIGPENDING)
Mikael Pettersson36984262009-08-15 12:58:11 +0100697 do_signal(regs, syscall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}