blob: 80b8b5c7e07a1a472b1b18458c41d32412e642e6 [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
50static int do_signal(sigset_t *oldset, struct pt_regs * regs, int syscall);
51
52/*
53 * atomically swap in the new signal mask, and wait for a signal.
54 */
55asmlinkage int sys_sigsuspend(int restart, unsigned long oldmask, old_sigset_t mask, struct pt_regs *regs)
56{
57 sigset_t saveset;
58
59 mask &= _BLOCKABLE;
60 spin_lock_irq(&current->sighand->siglock);
61 saveset = current->blocked;
62 siginitset(&current->blocked, mask);
63 recalc_sigpending();
64 spin_unlock_irq(&current->sighand->siglock);
65 regs->ARM_r0 = -EINTR;
66
67 while (1) {
68 current->state = TASK_INTERRUPTIBLE;
69 schedule();
70 if (do_signal(&saveset, regs, 0))
71 return regs->ARM_r0;
72 }
73}
74
75asmlinkage int
76sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize, struct pt_regs *regs)
77{
78 sigset_t saveset, newset;
79
80 /* XXX: Don't preclude handling different sized sigset_t's. */
81 if (sigsetsize != sizeof(sigset_t))
82 return -EINVAL;
83
84 if (copy_from_user(&newset, unewset, sizeof(newset)))
85 return -EFAULT;
86 sigdelsetmask(&newset, ~_BLOCKABLE);
87
88 spin_lock_irq(&current->sighand->siglock);
89 saveset = current->blocked;
90 current->blocked = newset;
91 recalc_sigpending();
92 spin_unlock_irq(&current->sighand->siglock);
93 regs->ARM_r0 = -EINTR;
94
95 while (1) {
96 current->state = TASK_INTERRUPTIBLE;
97 schedule();
98 if (do_signal(&saveset, regs, 0))
99 return regs->ARM_r0;
100 }
101}
102
103asmlinkage int
104sys_sigaction(int sig, const struct old_sigaction __user *act,
105 struct old_sigaction __user *oact)
106{
107 struct k_sigaction new_ka, old_ka;
108 int ret;
109
110 if (act) {
111 old_sigset_t mask;
112 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
113 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
114 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
115 return -EFAULT;
116 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
117 __get_user(mask, &act->sa_mask);
118 siginitset(&new_ka.sa.sa_mask, mask);
119 }
120
121 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
122
123 if (!ret && oact) {
124 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
125 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
126 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
127 return -EFAULT;
128 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
129 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
130 }
131
132 return ret;
133}
134
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100135#ifdef CONFIG_CRUNCH
136static int preserve_crunch_context(struct crunch_sigframe *frame)
137{
138 char kbuf[sizeof(*frame) + 8];
139 struct crunch_sigframe *kframe;
140
141 /* the crunch context must be 64 bit aligned */
142 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
143 kframe->magic = CRUNCH_MAGIC;
144 kframe->size = CRUNCH_STORAGE_SIZE;
145 crunch_task_copy(current_thread_info(), &kframe->storage);
146 return __copy_to_user(frame, kframe, sizeof(*frame));
147}
148
149static int restore_crunch_context(struct crunch_sigframe *frame)
150{
151 char kbuf[sizeof(*frame) + 8];
152 struct crunch_sigframe *kframe;
153
154 /* the crunch context must be 64 bit aligned */
155 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
156 if (__copy_from_user(kframe, frame, sizeof(*frame)))
157 return -1;
158 if (kframe->magic != CRUNCH_MAGIC ||
159 kframe->size != CRUNCH_STORAGE_SIZE)
160 return -1;
161 crunch_task_restore(current_thread_info(), &kframe->storage);
162 return 0;
163}
164#endif
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#ifdef CONFIG_IWMMXT
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168static int preserve_iwmmxt_context(struct iwmmxt_sigframe *frame)
169{
Hugh Dickins69b04752005-10-29 18:16:36 -0700170 char kbuf[sizeof(*frame) + 8];
171 struct iwmmxt_sigframe *kframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 /* the iWMMXt context must be 64 bit aligned */
Hugh Dickins69b04752005-10-29 18:16:36 -0700174 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100175 kframe->magic = IWMMXT_MAGIC;
176 kframe->size = IWMMXT_STORAGE_SIZE;
Hugh Dickins69b04752005-10-29 18:16:36 -0700177 iwmmxt_task_copy(current_thread_info(), &kframe->storage);
178 return __copy_to_user(frame, kframe, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181static int restore_iwmmxt_context(struct iwmmxt_sigframe *frame)
182{
Hugh Dickins69b04752005-10-29 18:16:36 -0700183 char kbuf[sizeof(*frame) + 8];
184 struct iwmmxt_sigframe *kframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Hugh Dickins69b04752005-10-29 18:16:36 -0700186 /* the iWMMXt context must be 64 bit aligned */
187 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
188 if (__copy_from_user(kframe, frame, sizeof(*frame)))
189 return -1;
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100190 if (kframe->magic != IWMMXT_MAGIC ||
191 kframe->size != IWMMXT_STORAGE_SIZE)
Hugh Dickins69b04752005-10-29 18:16:36 -0700192 return -1;
193 iwmmxt_task_restore(current_thread_info(), &kframe->storage);
194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
197#endif
198
199/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 * Do a signal return; undo the signal stack. These are aligned to 64-bit.
201 */
202struct sigframe {
Russell King7d4fdc12006-06-15 20:13:25 +0100203 struct ucontext uc;
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000204 unsigned long retcode[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205};
206
207struct rt_sigframe {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 struct siginfo info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100209 struct sigframe sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210};
211
Russell King68071482006-06-15 20:23:02 +0100212static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100214 struct aux_sigframe __user *aux;
Russell King68071482006-06-15 20:23:02 +0100215 sigset_t set;
216 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Russell King68071482006-06-15 20:23:02 +0100218 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
219 if (err == 0) {
220 sigdelsetmask(&set, ~_BLOCKABLE);
221 spin_lock_irq(&current->sighand->siglock);
222 current->blocked = set;
223 recalc_sigpending();
224 spin_unlock_irq(&current->sighand->siglock);
225 }
226
227 __get_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
228 __get_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
229 __get_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
230 __get_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
231 __get_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
232 __get_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
233 __get_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
234 __get_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
235 __get_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
236 __get_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
237 __get_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
238 __get_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
239 __get_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
240 __get_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
241 __get_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
242 __get_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
243 __get_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 err |= !valid_user_regs(regs);
246
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100247 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100248#ifdef CONFIG_CRUNCH
249 if (err == 0)
250 err |= restore_crunch_context(&aux->crunch);
251#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252#ifdef CONFIG_IWMMXT
253 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
254 err |= restore_iwmmxt_context(&aux->iwmmxt);
255#endif
256#ifdef CONFIG_VFP
257// if (err == 0)
Russell King68071482006-06-15 20:23:02 +0100258// err |= vfp_restore_state(&sf->aux.vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259#endif
260
261 return err;
262}
263
264asmlinkage int sys_sigreturn(struct pt_regs *regs)
265{
266 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 /* Always make any pending restarted system calls return -EINTR */
269 current_thread_info()->restart_block.fn = do_no_restart_syscall;
270
271 /*
272 * Since we stacked the signal on a 64-bit boundary,
273 * then 'sp' should be word aligned here. If it's
274 * not, then the user is trying to mess with us.
275 */
276 if (regs->ARM_sp & 7)
277 goto badframe;
278
279 frame = (struct sigframe __user *)regs->ARM_sp;
280
281 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
282 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Russell King68071482006-06-15 20:23:02 +0100284 if (restore_sigframe(regs, frame))
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
296asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
297{
298 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 /* Always make any pending restarted system calls return -EINTR */
301 current_thread_info()->restart_block.fn = do_no_restart_syscall;
302
303 /*
304 * Since we stacked the signal on a 64-bit boundary,
305 * then 'sp' should be word aligned here. If it's
306 * not, then the user is trying to mess with us.
307 */
308 if (regs->ARM_sp & 7)
309 goto badframe;
310
311 frame = (struct rt_sigframe __user *)regs->ARM_sp;
312
313 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
314 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Russell King68071482006-06-15 20:23:02 +0100316 if (restore_sigframe(regs, &frame->sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 goto badframe;
318
Russell Kingcb3504e2006-06-15 20:18:25 +0100319 if (do_sigaltstack(&frame->sig.uc.uc_stack, NULL, regs->ARM_sp) == -EFAULT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 goto badframe;
321
Russell Kingb2a0d362007-03-04 09:50:28 +0000322 single_step_trap(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 return regs->ARM_r0;
325
326badframe:
327 force_sig(SIGSEGV, current);
328 return 0;
329}
330
331static int
Russell Kingaca6ca12006-06-15 20:28:03 +0100332setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs, sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100334 struct aux_sigframe __user *aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 int err = 0;
336
Russell Kingaca6ca12006-06-15 20:28:03 +0100337 __put_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
338 __put_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
339 __put_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
340 __put_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
341 __put_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
342 __put_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
343 __put_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
344 __put_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
345 __put_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
346 __put_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
347 __put_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
348 __put_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
349 __put_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
350 __put_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
351 __put_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
352 __put_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
353 __put_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Russell Kingaca6ca12006-06-15 20:28:03 +0100355 __put_user_error(current->thread.trap_no, &sf->uc.uc_mcontext.trap_no, err);
356 __put_user_error(current->thread.error_code, &sf->uc.uc_mcontext.error_code, err);
357 __put_user_error(current->thread.address, &sf->uc.uc_mcontext.fault_address, err);
358 __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Russell Kingaca6ca12006-06-15 20:28:03 +0100360 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100362 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100363#ifdef CONFIG_CRUNCH
364 if (err == 0)
365 err |= preserve_crunch_context(&aux->crunch);
366#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367#ifdef CONFIG_IWMMXT
368 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
369 err |= preserve_iwmmxt_context(&aux->iwmmxt);
370#endif
371#ifdef CONFIG_VFP
372// if (err == 0)
Russell Kingaca6ca12006-06-15 20:28:03 +0100373// err |= vfp_save_state(&sf->aux.vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374#endif
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100375 __put_user_error(0, &aux->end_magic, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 return err;
378}
379
380static inline void __user *
381get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
382{
383 unsigned long sp = regs->ARM_sp;
384 void __user *frame;
385
386 /*
387 * This is the X/Open sanctioned signal stack switching.
388 */
389 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
390 sp = current->sas_ss_sp + current->sas_ss_size;
391
392 /*
393 * ATPCS B01 mandates 8-byte alignment
394 */
395 frame = (void __user *)((sp - framesize) & ~7);
396
397 /*
398 * Check that we can actually write to the signal frame.
399 */
400 if (!access_ok(VERIFY_WRITE, frame, framesize))
401 frame = NULL;
402
403 return frame;
404}
405
406static int
407setup_return(struct pt_regs *regs, struct k_sigaction *ka,
408 unsigned long __user *rc, void __user *frame, int usig)
409{
410 unsigned long handler = (unsigned long)ka->sa.sa_handler;
411 unsigned long retcode;
412 int thumb = 0;
413 unsigned long cpsr = regs->ARM_cpsr & ~PSR_f;
414
415 /*
416 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
417 */
418 if (ka->sa.sa_flags & SA_THIRTYTWO)
419 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
420
421#ifdef CONFIG_ARM_THUMB
422 if (elf_hwcap & HWCAP_THUMB) {
423 /*
424 * The LSB of the handler determines if we're going to
425 * be using THUMB or ARM mode for this signal handler.
426 */
427 thumb = handler & 1;
428
429 if (thumb)
430 cpsr |= PSR_T_BIT;
431 else
432 cpsr &= ~PSR_T_BIT;
433 }
434#endif
435
436 if (ka->sa.sa_flags & SA_RESTORER) {
437 retcode = (unsigned long)ka->sa.sa_restorer;
438 } else {
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000439 unsigned int idx = thumb << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 if (ka->sa.sa_flags & SA_SIGINFO)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000442 idx += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000444 if (__put_user(sigreturn_codes[idx], rc) ||
445 __put_user(sigreturn_codes[idx+1], rc+1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return 1;
447
Russell Kinge00d3492005-06-22 20:26:05 +0100448 if (cpsr & MODE32_BIT) {
449 /*
450 * 32-bit code can use the new high-page
451 * signal return code support.
452 */
453 retcode = KERN_SIGRETURN_CODE + (idx << 2) + thumb;
454 } else {
455 /*
456 * Ensure that the instruction cache sees
457 * the return code written onto the stack.
458 */
459 flush_icache_range((unsigned long)rc,
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000460 (unsigned long)(rc + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Russell Kinge00d3492005-06-22 20:26:05 +0100462 retcode = ((unsigned long)rc) + thumb;
463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465
466 regs->ARM_r0 = usig;
467 regs->ARM_sp = (unsigned long)frame;
468 regs->ARM_lr = retcode;
469 regs->ARM_pc = handler;
470 regs->ARM_cpsr = cpsr;
471
472 return 0;
473}
474
475static int
476setup_frame(int usig, struct k_sigaction *ka, sigset_t *set, struct pt_regs *regs)
477{
478 struct sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
479 int err = 0;
480
481 if (!frame)
482 return 1;
483
Russell Kingca195cf2006-06-24 22:41:09 +0100484 /*
485 * Set uc.uc_flags to a value which sc.trap_no would never have.
486 */
487 __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Russell Kingaca6ca12006-06-15 20:28:03 +0100489 err |= setup_sigframe(frame, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (err == 0)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000491 err = setup_return(regs, ka, frame->retcode, frame, usig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 return err;
494}
495
496static int
497setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
498 sigset_t *set, struct pt_regs *regs)
499{
500 struct rt_sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
501 stack_t stack;
502 int err = 0;
503
504 if (!frame)
505 return 1;
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 err |= copy_siginfo_to_user(&frame->info, info);
508
Russell Kingcb3504e2006-06-15 20:18:25 +0100509 __put_user_error(0, &frame->sig.uc.uc_flags, err);
510 __put_user_error(NULL, &frame->sig.uc.uc_link, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 memset(&stack, 0, sizeof(stack));
513 stack.ss_sp = (void __user *)current->sas_ss_sp;
514 stack.ss_flags = sas_ss_flags(regs->ARM_sp);
515 stack.ss_size = current->sas_ss_size;
Russell Kingcb3504e2006-06-15 20:18:25 +0100516 err |= __copy_to_user(&frame->sig.uc.uc_stack, &stack, sizeof(stack));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Russell Kingaca6ca12006-06-15 20:28:03 +0100518 err |= setup_sigframe(&frame->sig, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (err == 0)
Russell Kingcb3504e2006-06-15 20:18:25 +0100520 err = setup_return(regs, ka, frame->sig.retcode, frame, usig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 if (err == 0) {
523 /*
524 * For realtime signals we must also set the second and third
525 * arguments for the signal handler.
526 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
527 */
528 regs->ARM_r1 = (unsigned long)&frame->info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100529 regs->ARM_r2 = (unsigned long)&frame->sig.uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531
532 return err;
533}
534
535static inline void restart_syscall(struct pt_regs *regs)
536{
537 regs->ARM_r0 = regs->ARM_ORIG_r0;
538 regs->ARM_pc -= thumb_mode(regs) ? 2 : 4;
539}
540
541/*
542 * OK, we're invoking a handler
543 */
544static void
545handle_signal(unsigned long sig, struct k_sigaction *ka,
546 siginfo_t *info, sigset_t *oldset,
547 struct pt_regs * regs, int syscall)
548{
549 struct thread_info *thread = current_thread_info();
550 struct task_struct *tsk = current;
551 int usig = sig;
552 int ret;
553
554 /*
555 * If we were from a system call, check for system call restarting...
556 */
557 if (syscall) {
558 switch (regs->ARM_r0) {
559 case -ERESTART_RESTARTBLOCK:
560 case -ERESTARTNOHAND:
561 regs->ARM_r0 = -EINTR;
562 break;
563 case -ERESTARTSYS:
564 if (!(ka->sa.sa_flags & SA_RESTART)) {
565 regs->ARM_r0 = -EINTR;
566 break;
567 }
568 /* fallthrough */
569 case -ERESTARTNOINTR:
570 restart_syscall(regs);
571 }
572 }
573
574 /*
575 * translate the signal
576 */
577 if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
578 usig = thread->exec_domain->signal_invmap[usig];
579
580 /*
581 * Set up the stack frame
582 */
583 if (ka->sa.sa_flags & SA_SIGINFO)
584 ret = setup_rt_frame(usig, ka, info, oldset, regs);
585 else
586 ret = setup_frame(usig, ka, oldset, regs);
587
588 /*
589 * Check that the resulting registers are actually sane.
590 */
591 ret |= !valid_user_regs(regs);
592
Steven Rostedt69be8f12005-08-29 11:44:09 -0400593 if (ret != 0) {
Daniel Jacobowitza6c61e92005-11-19 10:01:07 +0000594 force_sigsegv(sig, tsk);
595 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
597
Daniel Jacobowitza6c61e92005-11-19 10:01:07 +0000598 /*
599 * Block the signal if we were successful.
600 */
601 spin_lock_irq(&tsk->sighand->siglock);
602 sigorsets(&tsk->blocked, &tsk->blocked,
603 &ka->sa.sa_mask);
604 if (!(ka->sa.sa_flags & SA_NODEFER))
605 sigaddset(&tsk->blocked, sig);
606 recalc_sigpending();
607 spin_unlock_irq(&tsk->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
611/*
612 * Note that 'init' is a special process: it doesn't get signals it doesn't
613 * want to handle. Thus you cannot kill init even with a SIGKILL even by
614 * mistake.
615 *
616 * Note that we go through the signals twice: once to check the signals that
617 * the kernel can handle, and then we build all the user-level signal handling
618 * stack-frames in one go after that.
619 */
620static int do_signal(sigset_t *oldset, struct pt_regs *regs, int syscall)
621{
622 struct k_sigaction ka;
623 siginfo_t info;
624 int signr;
625
626 /*
627 * We want the common case to go fast, which
628 * is why we may in certain cases get here from
629 * kernel mode. Just return without doing anything
630 * if so.
631 */
632 if (!user_mode(regs))
633 return 0;
634
Andrew Mortonbdb94f32005-06-26 03:27:21 -0700635 if (try_to_freeze())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 goto no_signal;
637
Russell Kingb2a0d362007-03-04 09:50:28 +0000638 single_step_clear(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
641 if (signr > 0) {
642 handle_signal(signr, &ka, &info, oldset, regs, syscall);
Russell Kingb2a0d362007-03-04 09:50:28 +0000643 single_step_set(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return 1;
645 }
646
647 no_signal:
648 /*
649 * No signal to deliver to the process - restart the syscall.
650 */
651 if (syscall) {
652 if (regs->ARM_r0 == -ERESTART_RESTARTBLOCK) {
653 if (thumb_mode(regs)) {
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100654 regs->ARM_r7 = __NR_restart_syscall - __NR_SYSCALL_BASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 regs->ARM_pc -= 2;
656 } else {
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100657#if defined(CONFIG_AEABI) && !defined(CONFIG_OABI_COMPAT)
658 regs->ARM_r7 = __NR_restart_syscall;
659 regs->ARM_pc -= 4;
660#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 u32 __user *usp;
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100662 u32 swival = __NR_restart_syscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 regs->ARM_sp -= 12;
665 usp = (u32 __user *)regs->ARM_sp;
666
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100667 /*
668 * Either we supports OABI only, or we have
669 * EABI with the OABI compat layer enabled.
670 * In the later case we don't know if user
671 * space is EABI or not, and if not we must
672 * not clobber r7. Always using the OABI
673 * syscall solves that issue and works for
674 * all those cases.
675 */
Nicolas Pitre95eaa5f2006-06-23 11:40:53 -0400676 swival = swival - __NR_SYSCALL_BASE + __NR_OABI_SYSCALL_BASE;
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 put_user(regs->ARM_pc, &usp[0]);
679 /* swi __NR_restart_syscall */
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100680 put_user(0xef000000 | swival, &usp[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 /* ldr pc, [sp], #12 */
682 put_user(0xe49df00c, &usp[2]);
683
684 flush_icache_range((unsigned long)usp,
685 (unsigned long)(usp + 3));
686
687 regs->ARM_pc = regs->ARM_sp + 4;
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100688#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690 }
691 if (regs->ARM_r0 == -ERESTARTNOHAND ||
692 regs->ARM_r0 == -ERESTARTSYS ||
693 regs->ARM_r0 == -ERESTARTNOINTR) {
694 restart_syscall(regs);
695 }
696 }
Russell Kingb2a0d362007-03-04 09:50:28 +0000697 single_step_set(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return 0;
699}
700
701asmlinkage void
702do_notify_resume(struct pt_regs *regs, unsigned int thread_flags, int syscall)
703{
704 if (thread_flags & _TIF_SIGPENDING)
705 do_signal(&current->blocked, regs, syscall);
706}