blob: 48cf7fffddf2add8d663fc4db96b019ed3eaea04 [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>
12#include <linux/ptrace.h>
13#include <linux/personality.h>
14
15#include <asm/cacheflush.h>
16#include <asm/ucontext.h>
17#include <asm/uaccess.h>
18#include <asm/unistd.h>
19
20#include "ptrace.h"
Russell Kinge00d3492005-06-22 20:26:05 +010021#include "signal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
24
25/*
26 * For ARM syscalls, we encode the syscall number into the instruction.
27 */
28#define SWI_SYS_SIGRETURN (0xef000000|(__NR_sigreturn))
29#define SWI_SYS_RT_SIGRETURN (0xef000000|(__NR_rt_sigreturn))
30
31/*
Nicolas Pitrefcca5382006-01-18 22:38:47 +000032 * With EABI, the syscall number has to be loaded into r7.
33 */
34#define MOV_R7_NR_SIGRETURN (0xe3a07000 | (__NR_sigreturn - __NR_SYSCALL_BASE))
35#define MOV_R7_NR_RT_SIGRETURN (0xe3a07000 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
36
37/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * For Thumb syscalls, we pass the syscall number via r7. We therefore
39 * need two 16-bit instructions.
40 */
41#define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (__NR_sigreturn - __NR_SYSCALL_BASE))
42#define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (__NR_rt_sigreturn - __NR_SYSCALL_BASE))
43
Nicolas Pitrefcca5382006-01-18 22:38:47 +000044const unsigned long sigreturn_codes[7] = {
45 MOV_R7_NR_SIGRETURN, SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
46 MOV_R7_NR_RT_SIGRETURN, SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047};
48
49static int do_signal(sigset_t *oldset, struct pt_regs * regs, int syscall);
50
51/*
52 * atomically swap in the new signal mask, and wait for a signal.
53 */
54asmlinkage int sys_sigsuspend(int restart, unsigned long oldmask, old_sigset_t mask, struct pt_regs *regs)
55{
56 sigset_t saveset;
57
58 mask &= _BLOCKABLE;
59 spin_lock_irq(&current->sighand->siglock);
60 saveset = current->blocked;
61 siginitset(&current->blocked, mask);
62 recalc_sigpending();
63 spin_unlock_irq(&current->sighand->siglock);
64 regs->ARM_r0 = -EINTR;
65
66 while (1) {
67 current->state = TASK_INTERRUPTIBLE;
68 schedule();
69 if (do_signal(&saveset, regs, 0))
70 return regs->ARM_r0;
71 }
72}
73
74asmlinkage int
75sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize, struct pt_regs *regs)
76{
77 sigset_t saveset, newset;
78
79 /* XXX: Don't preclude handling different sized sigset_t's. */
80 if (sigsetsize != sizeof(sigset_t))
81 return -EINVAL;
82
83 if (copy_from_user(&newset, unewset, sizeof(newset)))
84 return -EFAULT;
85 sigdelsetmask(&newset, ~_BLOCKABLE);
86
87 spin_lock_irq(&current->sighand->siglock);
88 saveset = current->blocked;
89 current->blocked = newset;
90 recalc_sigpending();
91 spin_unlock_irq(&current->sighand->siglock);
92 regs->ARM_r0 = -EINTR;
93
94 while (1) {
95 current->state = TASK_INTERRUPTIBLE;
96 schedule();
97 if (do_signal(&saveset, regs, 0))
98 return regs->ARM_r0;
99 }
100}
101
102asmlinkage int
103sys_sigaction(int sig, const struct old_sigaction __user *act,
104 struct old_sigaction __user *oact)
105{
106 struct k_sigaction new_ka, old_ka;
107 int ret;
108
109 if (act) {
110 old_sigset_t mask;
111 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
112 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
113 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer))
114 return -EFAULT;
115 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
116 __get_user(mask, &act->sa_mask);
117 siginitset(&new_ka.sa.sa_mask, mask);
118 }
119
120 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
121
122 if (!ret && oact) {
123 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
124 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
125 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer))
126 return -EFAULT;
127 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
128 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
129 }
130
131 return ret;
132}
133
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100134#ifdef CONFIG_CRUNCH
135static int preserve_crunch_context(struct crunch_sigframe *frame)
136{
137 char kbuf[sizeof(*frame) + 8];
138 struct crunch_sigframe *kframe;
139
140 /* the crunch context must be 64 bit aligned */
141 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
142 kframe->magic = CRUNCH_MAGIC;
143 kframe->size = CRUNCH_STORAGE_SIZE;
144 crunch_task_copy(current_thread_info(), &kframe->storage);
145 return __copy_to_user(frame, kframe, sizeof(*frame));
146}
147
148static int restore_crunch_context(struct crunch_sigframe *frame)
149{
150 char kbuf[sizeof(*frame) + 8];
151 struct crunch_sigframe *kframe;
152
153 /* the crunch context must be 64 bit aligned */
154 kframe = (struct crunch_sigframe *)((unsigned long)(kbuf + 8) & ~7);
155 if (__copy_from_user(kframe, frame, sizeof(*frame)))
156 return -1;
157 if (kframe->magic != CRUNCH_MAGIC ||
158 kframe->size != CRUNCH_STORAGE_SIZE)
159 return -1;
160 crunch_task_restore(current_thread_info(), &kframe->storage);
161 return 0;
162}
163#endif
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#ifdef CONFIG_IWMMXT
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static int preserve_iwmmxt_context(struct iwmmxt_sigframe *frame)
168{
Hugh Dickins69b04752005-10-29 18:16:36 -0700169 char kbuf[sizeof(*frame) + 8];
170 struct iwmmxt_sigframe *kframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 /* the iWMMXt context must be 64 bit aligned */
Hugh Dickins69b04752005-10-29 18:16:36 -0700173 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100174 kframe->magic = IWMMXT_MAGIC;
175 kframe->size = IWMMXT_STORAGE_SIZE;
Hugh Dickins69b04752005-10-29 18:16:36 -0700176 iwmmxt_task_copy(current_thread_info(), &kframe->storage);
177 return __copy_to_user(frame, kframe, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180static int restore_iwmmxt_context(struct iwmmxt_sigframe *frame)
181{
Hugh Dickins69b04752005-10-29 18:16:36 -0700182 char kbuf[sizeof(*frame) + 8];
183 struct iwmmxt_sigframe *kframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Hugh Dickins69b04752005-10-29 18:16:36 -0700185 /* the iWMMXt context must be 64 bit aligned */
186 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
187 if (__copy_from_user(kframe, frame, sizeof(*frame)))
188 return -1;
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100189 if (kframe->magic != IWMMXT_MAGIC ||
190 kframe->size != IWMMXT_STORAGE_SIZE)
Hugh Dickins69b04752005-10-29 18:16:36 -0700191 return -1;
192 iwmmxt_task_restore(current_thread_info(), &kframe->storage);
193 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196#endif
197
198/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 * Do a signal return; undo the signal stack. These are aligned to 64-bit.
200 */
201struct sigframe {
Russell King7d4fdc12006-06-15 20:13:25 +0100202 struct ucontext uc;
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000203 unsigned long retcode[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
206struct rt_sigframe {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 struct siginfo info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100208 struct sigframe sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209};
210
Russell King68071482006-06-15 20:23:02 +0100211static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100213 struct aux_sigframe __user *aux;
Russell King68071482006-06-15 20:23:02 +0100214 sigset_t set;
215 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Russell King68071482006-06-15 20:23:02 +0100217 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
218 if (err == 0) {
219 sigdelsetmask(&set, ~_BLOCKABLE);
220 spin_lock_irq(&current->sighand->siglock);
221 current->blocked = set;
222 recalc_sigpending();
223 spin_unlock_irq(&current->sighand->siglock);
224 }
225
226 __get_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
227 __get_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
228 __get_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
229 __get_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
230 __get_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
231 __get_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
232 __get_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
233 __get_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
234 __get_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
235 __get_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
236 __get_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
237 __get_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
238 __get_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
239 __get_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
240 __get_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
241 __get_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
242 __get_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 err |= !valid_user_regs(regs);
245
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100246 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100247#ifdef CONFIG_CRUNCH
248 if (err == 0)
249 err |= restore_crunch_context(&aux->crunch);
250#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251#ifdef CONFIG_IWMMXT
252 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
253 err |= restore_iwmmxt_context(&aux->iwmmxt);
254#endif
255#ifdef CONFIG_VFP
256// if (err == 0)
Russell King68071482006-06-15 20:23:02 +0100257// err |= vfp_restore_state(&sf->aux.vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258#endif
259
260 return err;
261}
262
263asmlinkage int sys_sigreturn(struct pt_regs *regs)
264{
265 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 /* Always make any pending restarted system calls return -EINTR */
268 current_thread_info()->restart_block.fn = do_no_restart_syscall;
269
270 /*
271 * Since we stacked the signal on a 64-bit boundary,
272 * then 'sp' should be word aligned here. If it's
273 * not, then the user is trying to mess with us.
274 */
275 if (regs->ARM_sp & 7)
276 goto badframe;
277
278 frame = (struct sigframe __user *)regs->ARM_sp;
279
280 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
281 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Russell King68071482006-06-15 20:23:02 +0100283 if (restore_sigframe(regs, frame))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 goto badframe;
285
286 /* Send SIGTRAP if we're single-stepping */
287 if (current->ptrace & PT_SINGLESTEP) {
288 ptrace_cancel_bpt(current);
289 send_sig(SIGTRAP, current, 1);
290 }
291
292 return regs->ARM_r0;
293
294badframe:
295 force_sig(SIGSEGV, current);
296 return 0;
297}
298
299asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
300{
301 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 /* Always make any pending restarted system calls return -EINTR */
304 current_thread_info()->restart_block.fn = do_no_restart_syscall;
305
306 /*
307 * Since we stacked the signal on a 64-bit boundary,
308 * then 'sp' should be word aligned here. If it's
309 * not, then the user is trying to mess with us.
310 */
311 if (regs->ARM_sp & 7)
312 goto badframe;
313
314 frame = (struct rt_sigframe __user *)regs->ARM_sp;
315
316 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
317 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Russell King68071482006-06-15 20:23:02 +0100319 if (restore_sigframe(regs, &frame->sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 goto badframe;
321
Russell Kingcb3504e2006-06-15 20:18:25 +0100322 if (do_sigaltstack(&frame->sig.uc.uc_stack, NULL, regs->ARM_sp) == -EFAULT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 goto badframe;
324
325 /* Send SIGTRAP if we're single-stepping */
326 if (current->ptrace & PT_SINGLESTEP) {
327 ptrace_cancel_bpt(current);
328 send_sig(SIGTRAP, current, 1);
329 }
330
331 return regs->ARM_r0;
332
333badframe:
334 force_sig(SIGSEGV, current);
335 return 0;
336}
337
338static int
Russell Kingaca6ca12006-06-15 20:28:03 +0100339setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs, sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100341 struct aux_sigframe __user *aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 int err = 0;
343
Russell Kingaca6ca12006-06-15 20:28:03 +0100344 __put_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
345 __put_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
346 __put_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
347 __put_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
348 __put_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
349 __put_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
350 __put_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
351 __put_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
352 __put_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
353 __put_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
354 __put_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
355 __put_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
356 __put_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
357 __put_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
358 __put_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
359 __put_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
360 __put_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Russell Kingaca6ca12006-06-15 20:28:03 +0100362 __put_user_error(current->thread.trap_no, &sf->uc.uc_mcontext.trap_no, err);
363 __put_user_error(current->thread.error_code, &sf->uc.uc_mcontext.error_code, err);
364 __put_user_error(current->thread.address, &sf->uc.uc_mcontext.fault_address, err);
365 __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Russell Kingaca6ca12006-06-15 20:28:03 +0100367 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100369 aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
Lennert Buytenhek3bec6ded2006-06-27 22:56:18 +0100370#ifdef CONFIG_CRUNCH
371 if (err == 0)
372 err |= preserve_crunch_context(&aux->crunch);
373#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374#ifdef CONFIG_IWMMXT
375 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
376 err |= preserve_iwmmxt_context(&aux->iwmmxt);
377#endif
378#ifdef CONFIG_VFP
379// if (err == 0)
Russell Kingaca6ca12006-06-15 20:28:03 +0100380// err |= vfp_save_state(&sf->aux.vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381#endif
Daniel Jacobowitz85fe0682006-06-24 23:46:21 +0100382 __put_user_error(0, &aux->end_magic, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 return err;
385}
386
387static inline void __user *
388get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
389{
390 unsigned long sp = regs->ARM_sp;
391 void __user *frame;
392
393 /*
394 * This is the X/Open sanctioned signal stack switching.
395 */
396 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
397 sp = current->sas_ss_sp + current->sas_ss_size;
398
399 /*
400 * ATPCS B01 mandates 8-byte alignment
401 */
402 frame = (void __user *)((sp - framesize) & ~7);
403
404 /*
405 * Check that we can actually write to the signal frame.
406 */
407 if (!access_ok(VERIFY_WRITE, frame, framesize))
408 frame = NULL;
409
410 return frame;
411}
412
413static int
414setup_return(struct pt_regs *regs, struct k_sigaction *ka,
415 unsigned long __user *rc, void __user *frame, int usig)
416{
417 unsigned long handler = (unsigned long)ka->sa.sa_handler;
418 unsigned long retcode;
419 int thumb = 0;
420 unsigned long cpsr = regs->ARM_cpsr & ~PSR_f;
421
422 /*
423 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
424 */
425 if (ka->sa.sa_flags & SA_THIRTYTWO)
426 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
427
428#ifdef CONFIG_ARM_THUMB
429 if (elf_hwcap & HWCAP_THUMB) {
430 /*
431 * The LSB of the handler determines if we're going to
432 * be using THUMB or ARM mode for this signal handler.
433 */
434 thumb = handler & 1;
435
436 if (thumb)
437 cpsr |= PSR_T_BIT;
438 else
439 cpsr &= ~PSR_T_BIT;
440 }
441#endif
442
443 if (ka->sa.sa_flags & SA_RESTORER) {
444 retcode = (unsigned long)ka->sa.sa_restorer;
445 } else {
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000446 unsigned int idx = thumb << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 if (ka->sa.sa_flags & SA_SIGINFO)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000449 idx += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000451 if (__put_user(sigreturn_codes[idx], rc) ||
452 __put_user(sigreturn_codes[idx+1], rc+1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return 1;
454
Russell Kinge00d3492005-06-22 20:26:05 +0100455 if (cpsr & MODE32_BIT) {
456 /*
457 * 32-bit code can use the new high-page
458 * signal return code support.
459 */
460 retcode = KERN_SIGRETURN_CODE + (idx << 2) + thumb;
461 } else {
462 /*
463 * Ensure that the instruction cache sees
464 * the return code written onto the stack.
465 */
466 flush_icache_range((unsigned long)rc,
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000467 (unsigned long)(rc + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Russell Kinge00d3492005-06-22 20:26:05 +0100469 retcode = ((unsigned long)rc) + thumb;
470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472
473 regs->ARM_r0 = usig;
474 regs->ARM_sp = (unsigned long)frame;
475 regs->ARM_lr = retcode;
476 regs->ARM_pc = handler;
477 regs->ARM_cpsr = cpsr;
478
479 return 0;
480}
481
482static int
483setup_frame(int usig, struct k_sigaction *ka, sigset_t *set, struct pt_regs *regs)
484{
485 struct sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
486 int err = 0;
487
488 if (!frame)
489 return 1;
490
Russell Kingca195cf2006-06-24 22:41:09 +0100491 /*
492 * Set uc.uc_flags to a value which sc.trap_no would never have.
493 */
494 __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Russell Kingaca6ca12006-06-15 20:28:03 +0100496 err |= setup_sigframe(frame, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (err == 0)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000498 err = setup_return(regs, ka, frame->retcode, frame, usig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 return err;
501}
502
503static int
504setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
505 sigset_t *set, struct pt_regs *regs)
506{
507 struct rt_sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
508 stack_t stack;
509 int err = 0;
510
511 if (!frame)
512 return 1;
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 err |= copy_siginfo_to_user(&frame->info, info);
515
Russell Kingcb3504e2006-06-15 20:18:25 +0100516 __put_user_error(0, &frame->sig.uc.uc_flags, err);
517 __put_user_error(NULL, &frame->sig.uc.uc_link, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 memset(&stack, 0, sizeof(stack));
520 stack.ss_sp = (void __user *)current->sas_ss_sp;
521 stack.ss_flags = sas_ss_flags(regs->ARM_sp);
522 stack.ss_size = current->sas_ss_size;
Russell Kingcb3504e2006-06-15 20:18:25 +0100523 err |= __copy_to_user(&frame->sig.uc.uc_stack, &stack, sizeof(stack));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Russell Kingaca6ca12006-06-15 20:28:03 +0100525 err |= setup_sigframe(&frame->sig, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (err == 0)
Russell Kingcb3504e2006-06-15 20:18:25 +0100527 err = setup_return(regs, ka, frame->sig.retcode, frame, usig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 if (err == 0) {
530 /*
531 * For realtime signals we must also set the second and third
532 * arguments for the signal handler.
533 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
534 */
535 regs->ARM_r1 = (unsigned long)&frame->info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100536 regs->ARM_r2 = (unsigned long)&frame->sig.uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
538
539 return err;
540}
541
542static inline void restart_syscall(struct pt_regs *regs)
543{
544 regs->ARM_r0 = regs->ARM_ORIG_r0;
545 regs->ARM_pc -= thumb_mode(regs) ? 2 : 4;
546}
547
548/*
549 * OK, we're invoking a handler
550 */
551static void
552handle_signal(unsigned long sig, struct k_sigaction *ka,
553 siginfo_t *info, sigset_t *oldset,
554 struct pt_regs * regs, int syscall)
555{
556 struct thread_info *thread = current_thread_info();
557 struct task_struct *tsk = current;
558 int usig = sig;
559 int ret;
560
561 /*
562 * If we were from a system call, check for system call restarting...
563 */
564 if (syscall) {
565 switch (regs->ARM_r0) {
566 case -ERESTART_RESTARTBLOCK:
567 case -ERESTARTNOHAND:
568 regs->ARM_r0 = -EINTR;
569 break;
570 case -ERESTARTSYS:
571 if (!(ka->sa.sa_flags & SA_RESTART)) {
572 regs->ARM_r0 = -EINTR;
573 break;
574 }
575 /* fallthrough */
576 case -ERESTARTNOINTR:
577 restart_syscall(regs);
578 }
579 }
580
581 /*
582 * translate the signal
583 */
584 if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
585 usig = thread->exec_domain->signal_invmap[usig];
586
587 /*
588 * Set up the stack frame
589 */
590 if (ka->sa.sa_flags & SA_SIGINFO)
591 ret = setup_rt_frame(usig, ka, info, oldset, regs);
592 else
593 ret = setup_frame(usig, ka, oldset, regs);
594
595 /*
596 * Check that the resulting registers are actually sane.
597 */
598 ret |= !valid_user_regs(regs);
599
Steven Rostedt69be8f12005-08-29 11:44:09 -0400600 if (ret != 0) {
Daniel Jacobowitza6c61e92005-11-19 10:01:07 +0000601 force_sigsegv(sig, tsk);
602 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604
Daniel Jacobowitza6c61e92005-11-19 10:01:07 +0000605 /*
606 * Block the signal if we were successful.
607 */
608 spin_lock_irq(&tsk->sighand->siglock);
609 sigorsets(&tsk->blocked, &tsk->blocked,
610 &ka->sa.sa_mask);
611 if (!(ka->sa.sa_flags & SA_NODEFER))
612 sigaddset(&tsk->blocked, sig);
613 recalc_sigpending();
614 spin_unlock_irq(&tsk->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
618/*
619 * Note that 'init' is a special process: it doesn't get signals it doesn't
620 * want to handle. Thus you cannot kill init even with a SIGKILL even by
621 * mistake.
622 *
623 * Note that we go through the signals twice: once to check the signals that
624 * the kernel can handle, and then we build all the user-level signal handling
625 * stack-frames in one go after that.
626 */
627static int do_signal(sigset_t *oldset, struct pt_regs *regs, int syscall)
628{
629 struct k_sigaction ka;
630 siginfo_t info;
631 int signr;
632
633 /*
634 * We want the common case to go fast, which
635 * is why we may in certain cases get here from
636 * kernel mode. Just return without doing anything
637 * if so.
638 */
639 if (!user_mode(regs))
640 return 0;
641
Andrew Mortonbdb94f32005-06-26 03:27:21 -0700642 if (try_to_freeze())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 goto no_signal;
644
645 if (current->ptrace & PT_SINGLESTEP)
646 ptrace_cancel_bpt(current);
647
648 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
649 if (signr > 0) {
650 handle_signal(signr, &ka, &info, oldset, regs, syscall);
651 if (current->ptrace & PT_SINGLESTEP)
652 ptrace_set_bpt(current);
653 return 1;
654 }
655
656 no_signal:
657 /*
658 * No signal to deliver to the process - restart the syscall.
659 */
660 if (syscall) {
661 if (regs->ARM_r0 == -ERESTART_RESTARTBLOCK) {
662 if (thumb_mode(regs)) {
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100663 regs->ARM_r7 = __NR_restart_syscall - __NR_SYSCALL_BASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 regs->ARM_pc -= 2;
665 } else {
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100666#if defined(CONFIG_AEABI) && !defined(CONFIG_OABI_COMPAT)
667 regs->ARM_r7 = __NR_restart_syscall;
668 regs->ARM_pc -= 4;
669#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 u32 __user *usp;
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100671 u32 swival = __NR_restart_syscall;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 regs->ARM_sp -= 12;
674 usp = (u32 __user *)regs->ARM_sp;
675
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100676 /*
677 * Either we supports OABI only, or we have
678 * EABI with the OABI compat layer enabled.
679 * In the later case we don't know if user
680 * space is EABI or not, and if not we must
681 * not clobber r7. Always using the OABI
682 * syscall solves that issue and works for
683 * all those cases.
684 */
Nicolas Pitre95eaa5f2006-06-23 11:40:53 -0400685 swival = swival - __NR_SYSCALL_BASE + __NR_OABI_SYSCALL_BASE;
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 put_user(regs->ARM_pc, &usp[0]);
688 /* swi __NR_restart_syscall */
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100689 put_user(0xef000000 | swival, &usp[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 /* ldr pc, [sp], #12 */
691 put_user(0xe49df00c, &usp[2]);
692
693 flush_icache_range((unsigned long)usp,
694 (unsigned long)(usp + 3));
695
696 regs->ARM_pc = regs->ARM_sp + 4;
Nicolas Pitref606a6f2006-06-22 22:18:45 +0100697#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699 }
700 if (regs->ARM_r0 == -ERESTARTNOHAND ||
701 regs->ARM_r0 == -ERESTARTSYS ||
702 regs->ARM_r0 == -ERESTARTNOINTR) {
703 restart_syscall(regs);
704 }
705 }
706 if (current->ptrace & PT_SINGLESTEP)
707 ptrace_set_bpt(current);
708 return 0;
709}
710
711asmlinkage void
712do_notify_resume(struct pt_regs *regs, unsigned int thread_flags, int syscall)
713{
714 if (thread_flags & _TIF_SIGPENDING)
715 do_signal(&current->blocked, regs, syscall);
716}