blob: c0ba8afee42fd4b3c984c02a3b0b68d36716ca79 [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 */
10#include <linux/config.h>
11#include <linux/errno.h>
12#include <linux/signal.h>
13#include <linux/ptrace.h>
14#include <linux/personality.h>
15
16#include <asm/cacheflush.h>
17#include <asm/ucontext.h>
18#include <asm/uaccess.h>
19#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 */
29#define SWI_SYS_SIGRETURN (0xef000000|(__NR_sigreturn))
30#define SWI_SYS_RT_SIGRETURN (0xef000000|(__NR_rt_sigreturn))
31
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
135#ifdef CONFIG_IWMMXT
136
137/* iwmmxt_area is 0x98 bytes long, preceeded by 8 bytes of signature */
138#define IWMMXT_STORAGE_SIZE (0x98 + 8)
139#define IWMMXT_MAGIC0 0x12ef842a
140#define IWMMXT_MAGIC1 0x1c07ca71
141
142struct iwmmxt_sigframe {
143 unsigned long magic0;
144 unsigned long magic1;
145 unsigned long storage[0x98/4];
146};
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148static int preserve_iwmmxt_context(struct iwmmxt_sigframe *frame)
149{
Hugh Dickins69b04752005-10-29 18:16:36 -0700150 char kbuf[sizeof(*frame) + 8];
151 struct iwmmxt_sigframe *kframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 /* the iWMMXt context must be 64 bit aligned */
Hugh Dickins69b04752005-10-29 18:16:36 -0700154 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
155 kframe->magic0 = IWMMXT_MAGIC0;
156 kframe->magic1 = IWMMXT_MAGIC1;
157 iwmmxt_task_copy(current_thread_info(), &kframe->storage);
158 return __copy_to_user(frame, kframe, sizeof(*frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161static int restore_iwmmxt_context(struct iwmmxt_sigframe *frame)
162{
Hugh Dickins69b04752005-10-29 18:16:36 -0700163 char kbuf[sizeof(*frame) + 8];
164 struct iwmmxt_sigframe *kframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Hugh Dickins69b04752005-10-29 18:16:36 -0700166 /* the iWMMXt context must be 64 bit aligned */
167 kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
168 if (__copy_from_user(kframe, frame, sizeof(*frame)))
169 return -1;
170 if (kframe->magic0 != IWMMXT_MAGIC0 ||
171 kframe->magic1 != IWMMXT_MAGIC1)
172 return -1;
173 iwmmxt_task_restore(current_thread_info(), &kframe->storage);
174 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
177#endif
178
179/*
180 * Auxiliary signal frame. This saves stuff like FP state.
181 * The layout of this structure is not part of the user ABI.
182 */
183struct aux_sigframe {
184#ifdef CONFIG_IWMMXT
185 struct iwmmxt_sigframe iwmmxt;
186#endif
187#ifdef CONFIG_VFP
188 union vfp_state vfp;
189#endif
190};
191
192/*
193 * Do a signal return; undo the signal stack. These are aligned to 64-bit.
194 */
195struct sigframe {
Russell King7d4fdc12006-06-15 20:13:25 +0100196 struct ucontext uc;
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000197 unsigned long retcode[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 struct aux_sigframe aux __attribute__((aligned(8)));
199};
200
201struct rt_sigframe {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 struct siginfo info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100203 struct sigframe sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
Russell King68071482006-06-15 20:23:02 +0100206static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Russell King68071482006-06-15 20:23:02 +0100208 sigset_t set;
209 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Russell King68071482006-06-15 20:23:02 +0100211 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
212 if (err == 0) {
213 sigdelsetmask(&set, ~_BLOCKABLE);
214 spin_lock_irq(&current->sighand->siglock);
215 current->blocked = set;
216 recalc_sigpending();
217 spin_unlock_irq(&current->sighand->siglock);
218 }
219
220 __get_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
221 __get_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
222 __get_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
223 __get_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
224 __get_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
225 __get_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
226 __get_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
227 __get_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
228 __get_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
229 __get_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
230 __get_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
231 __get_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
232 __get_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
233 __get_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
234 __get_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
235 __get_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
236 __get_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 err |= !valid_user_regs(regs);
239
240#ifdef CONFIG_IWMMXT
241 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
Russell King68071482006-06-15 20:23:02 +0100242 err |= restore_iwmmxt_context(&sf->aux.iwmmxt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243#endif
244#ifdef CONFIG_VFP
245// if (err == 0)
Russell King68071482006-06-15 20:23:02 +0100246// err |= vfp_restore_state(&sf->aux.vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247#endif
248
249 return err;
250}
251
252asmlinkage int sys_sigreturn(struct pt_regs *regs)
253{
254 struct sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 /* Always make any pending restarted system calls return -EINTR */
257 current_thread_info()->restart_block.fn = do_no_restart_syscall;
258
259 /*
260 * Since we stacked the signal on a 64-bit boundary,
261 * then 'sp' should be word aligned here. If it's
262 * not, then the user is trying to mess with us.
263 */
264 if (regs->ARM_sp & 7)
265 goto badframe;
266
267 frame = (struct sigframe __user *)regs->ARM_sp;
268
269 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
270 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Russell King68071482006-06-15 20:23:02 +0100272 if (restore_sigframe(regs, frame))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 goto badframe;
274
275 /* Send SIGTRAP if we're single-stepping */
276 if (current->ptrace & PT_SINGLESTEP) {
277 ptrace_cancel_bpt(current);
278 send_sig(SIGTRAP, current, 1);
279 }
280
281 return regs->ARM_r0;
282
283badframe:
284 force_sig(SIGSEGV, current);
285 return 0;
286}
287
288asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
289{
290 struct rt_sigframe __user *frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 /* Always make any pending restarted system calls return -EINTR */
293 current_thread_info()->restart_block.fn = do_no_restart_syscall;
294
295 /*
296 * Since we stacked the signal on a 64-bit boundary,
297 * then 'sp' should be word aligned here. If it's
298 * not, then the user is trying to mess with us.
299 */
300 if (regs->ARM_sp & 7)
301 goto badframe;
302
303 frame = (struct rt_sigframe __user *)regs->ARM_sp;
304
305 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
306 goto badframe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Russell King68071482006-06-15 20:23:02 +0100308 if (restore_sigframe(regs, &frame->sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 goto badframe;
310
Russell Kingcb3504e2006-06-15 20:18:25 +0100311 if (do_sigaltstack(&frame->sig.uc.uc_stack, NULL, regs->ARM_sp) == -EFAULT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 goto badframe;
313
314 /* Send SIGTRAP if we're single-stepping */
315 if (current->ptrace & PT_SINGLESTEP) {
316 ptrace_cancel_bpt(current);
317 send_sig(SIGTRAP, current, 1);
318 }
319
320 return regs->ARM_r0;
321
322badframe:
323 force_sig(SIGSEGV, current);
324 return 0;
325}
326
327static int
Russell Kingaca6ca12006-06-15 20:28:03 +0100328setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs, sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 int err = 0;
331
Russell Kingaca6ca12006-06-15 20:28:03 +0100332 __put_user_error(regs->ARM_r0, &sf->uc.uc_mcontext.arm_r0, err);
333 __put_user_error(regs->ARM_r1, &sf->uc.uc_mcontext.arm_r1, err);
334 __put_user_error(regs->ARM_r2, &sf->uc.uc_mcontext.arm_r2, err);
335 __put_user_error(regs->ARM_r3, &sf->uc.uc_mcontext.arm_r3, err);
336 __put_user_error(regs->ARM_r4, &sf->uc.uc_mcontext.arm_r4, err);
337 __put_user_error(regs->ARM_r5, &sf->uc.uc_mcontext.arm_r5, err);
338 __put_user_error(regs->ARM_r6, &sf->uc.uc_mcontext.arm_r6, err);
339 __put_user_error(regs->ARM_r7, &sf->uc.uc_mcontext.arm_r7, err);
340 __put_user_error(regs->ARM_r8, &sf->uc.uc_mcontext.arm_r8, err);
341 __put_user_error(regs->ARM_r9, &sf->uc.uc_mcontext.arm_r9, err);
342 __put_user_error(regs->ARM_r10, &sf->uc.uc_mcontext.arm_r10, err);
343 __put_user_error(regs->ARM_fp, &sf->uc.uc_mcontext.arm_fp, err);
344 __put_user_error(regs->ARM_ip, &sf->uc.uc_mcontext.arm_ip, err);
345 __put_user_error(regs->ARM_sp, &sf->uc.uc_mcontext.arm_sp, err);
346 __put_user_error(regs->ARM_lr, &sf->uc.uc_mcontext.arm_lr, err);
347 __put_user_error(regs->ARM_pc, &sf->uc.uc_mcontext.arm_pc, err);
348 __put_user_error(regs->ARM_cpsr, &sf->uc.uc_mcontext.arm_cpsr, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Russell Kingaca6ca12006-06-15 20:28:03 +0100350 __put_user_error(current->thread.trap_no, &sf->uc.uc_mcontext.trap_no, err);
351 __put_user_error(current->thread.error_code, &sf->uc.uc_mcontext.error_code, err);
352 __put_user_error(current->thread.address, &sf->uc.uc_mcontext.fault_address, err);
353 __put_user_error(set->sig[0], &sf->uc.uc_mcontext.oldmask, err);
354
355 err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357#ifdef CONFIG_IWMMXT
358 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
Russell Kingaca6ca12006-06-15 20:28:03 +0100359 err |= preserve_iwmmxt_context(&sf->aux.iwmmxt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360#endif
361#ifdef CONFIG_VFP
362// if (err == 0)
Russell Kingaca6ca12006-06-15 20:28:03 +0100363// err |= vfp_save_state(&sf->aux.vfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364#endif
365
366 return err;
367}
368
369static inline void __user *
370get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
371{
372 unsigned long sp = regs->ARM_sp;
373 void __user *frame;
374
375 /*
376 * This is the X/Open sanctioned signal stack switching.
377 */
378 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
379 sp = current->sas_ss_sp + current->sas_ss_size;
380
381 /*
382 * ATPCS B01 mandates 8-byte alignment
383 */
384 frame = (void __user *)((sp - framesize) & ~7);
385
386 /*
387 * Check that we can actually write to the signal frame.
388 */
389 if (!access_ok(VERIFY_WRITE, frame, framesize))
390 frame = NULL;
391
392 return frame;
393}
394
395static int
396setup_return(struct pt_regs *regs, struct k_sigaction *ka,
397 unsigned long __user *rc, void __user *frame, int usig)
398{
399 unsigned long handler = (unsigned long)ka->sa.sa_handler;
400 unsigned long retcode;
401 int thumb = 0;
402 unsigned long cpsr = regs->ARM_cpsr & ~PSR_f;
403
404 /*
405 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
406 */
407 if (ka->sa.sa_flags & SA_THIRTYTWO)
408 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
409
410#ifdef CONFIG_ARM_THUMB
411 if (elf_hwcap & HWCAP_THUMB) {
412 /*
413 * The LSB of the handler determines if we're going to
414 * be using THUMB or ARM mode for this signal handler.
415 */
416 thumb = handler & 1;
417
418 if (thumb)
419 cpsr |= PSR_T_BIT;
420 else
421 cpsr &= ~PSR_T_BIT;
422 }
423#endif
424
425 if (ka->sa.sa_flags & SA_RESTORER) {
426 retcode = (unsigned long)ka->sa.sa_restorer;
427 } else {
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000428 unsigned int idx = thumb << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 if (ka->sa.sa_flags & SA_SIGINFO)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000431 idx += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000433 if (__put_user(sigreturn_codes[idx], rc) ||
434 __put_user(sigreturn_codes[idx+1], rc+1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return 1;
436
Russell Kinge00d3492005-06-22 20:26:05 +0100437 if (cpsr & MODE32_BIT) {
438 /*
439 * 32-bit code can use the new high-page
440 * signal return code support.
441 */
442 retcode = KERN_SIGRETURN_CODE + (idx << 2) + thumb;
443 } else {
444 /*
445 * Ensure that the instruction cache sees
446 * the return code written onto the stack.
447 */
448 flush_icache_range((unsigned long)rc,
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000449 (unsigned long)(rc + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Russell Kinge00d3492005-06-22 20:26:05 +0100451 retcode = ((unsigned long)rc) + thumb;
452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454
455 regs->ARM_r0 = usig;
456 regs->ARM_sp = (unsigned long)frame;
457 regs->ARM_lr = retcode;
458 regs->ARM_pc = handler;
459 regs->ARM_cpsr = cpsr;
460
461 return 0;
462}
463
464static int
465setup_frame(int usig, struct k_sigaction *ka, sigset_t *set, struct pt_regs *regs)
466{
467 struct sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
468 int err = 0;
469
470 if (!frame)
471 return 1;
472
Russell Kingca195cf2006-06-24 22:41:09 +0100473 /*
474 * Set uc.uc_flags to a value which sc.trap_no would never have.
475 */
476 __put_user_error(0x5ac3c35a, &frame->uc.uc_flags, err);
477
Russell Kingaca6ca12006-06-15 20:28:03 +0100478 err |= setup_sigframe(frame, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (err == 0)
Nicolas Pitrefcca5382006-01-18 22:38:47 +0000480 err = setup_return(regs, ka, frame->retcode, frame, usig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 return err;
483}
484
485static int
486setup_rt_frame(int usig, struct k_sigaction *ka, siginfo_t *info,
487 sigset_t *set, struct pt_regs *regs)
488{
489 struct rt_sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
490 stack_t stack;
491 int err = 0;
492
493 if (!frame)
494 return 1;
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 err |= copy_siginfo_to_user(&frame->info, info);
497
Russell Kingcb3504e2006-06-15 20:18:25 +0100498 __put_user_error(0, &frame->sig.uc.uc_flags, err);
499 __put_user_error(NULL, &frame->sig.uc.uc_link, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 memset(&stack, 0, sizeof(stack));
502 stack.ss_sp = (void __user *)current->sas_ss_sp;
503 stack.ss_flags = sas_ss_flags(regs->ARM_sp);
504 stack.ss_size = current->sas_ss_size;
Russell Kingcb3504e2006-06-15 20:18:25 +0100505 err |= __copy_to_user(&frame->sig.uc.uc_stack, &stack, sizeof(stack));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Russell Kingaca6ca12006-06-15 20:28:03 +0100507 err |= setup_sigframe(&frame->sig, regs, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (err == 0)
Russell Kingcb3504e2006-06-15 20:18:25 +0100509 err = setup_return(regs, ka, frame->sig.retcode, frame, usig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 if (err == 0) {
512 /*
513 * For realtime signals we must also set the second and third
514 * arguments for the signal handler.
515 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
516 */
517 regs->ARM_r1 = (unsigned long)&frame->info;
Russell Kingcb3504e2006-06-15 20:18:25 +0100518 regs->ARM_r2 = (unsigned long)&frame->sig.uc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520
521 return err;
522}
523
524static inline void restart_syscall(struct pt_regs *regs)
525{
526 regs->ARM_r0 = regs->ARM_ORIG_r0;
527 regs->ARM_pc -= thumb_mode(regs) ? 2 : 4;
528}
529
530/*
531 * OK, we're invoking a handler
532 */
533static void
534handle_signal(unsigned long sig, struct k_sigaction *ka,
535 siginfo_t *info, sigset_t *oldset,
536 struct pt_regs * regs, int syscall)
537{
538 struct thread_info *thread = current_thread_info();
539 struct task_struct *tsk = current;
540 int usig = sig;
541 int ret;
542
543 /*
544 * If we were from a system call, check for system call restarting...
545 */
546 if (syscall) {
547 switch (regs->ARM_r0) {
548 case -ERESTART_RESTARTBLOCK:
549 case -ERESTARTNOHAND:
550 regs->ARM_r0 = -EINTR;
551 break;
552 case -ERESTARTSYS:
553 if (!(ka->sa.sa_flags & SA_RESTART)) {
554 regs->ARM_r0 = -EINTR;
555 break;
556 }
557 /* fallthrough */
558 case -ERESTARTNOINTR:
559 restart_syscall(regs);
560 }
561 }
562
563 /*
564 * translate the signal
565 */
566 if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
567 usig = thread->exec_domain->signal_invmap[usig];
568
569 /*
570 * Set up the stack frame
571 */
572 if (ka->sa.sa_flags & SA_SIGINFO)
573 ret = setup_rt_frame(usig, ka, info, oldset, regs);
574 else
575 ret = setup_frame(usig, ka, oldset, regs);
576
577 /*
578 * Check that the resulting registers are actually sane.
579 */
580 ret |= !valid_user_regs(regs);
581
Steven Rostedt69be8f12005-08-29 11:44:09 -0400582 if (ret != 0) {
Daniel Jacobowitza6c61e92005-11-19 10:01:07 +0000583 force_sigsegv(sig, tsk);
584 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586
Daniel Jacobowitza6c61e92005-11-19 10:01:07 +0000587 /*
588 * Block the signal if we were successful.
589 */
590 spin_lock_irq(&tsk->sighand->siglock);
591 sigorsets(&tsk->blocked, &tsk->blocked,
592 &ka->sa.sa_mask);
593 if (!(ka->sa.sa_flags & SA_NODEFER))
594 sigaddset(&tsk->blocked, sig);
595 recalc_sigpending();
596 spin_unlock_irq(&tsk->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
600/*
601 * Note that 'init' is a special process: it doesn't get signals it doesn't
602 * want to handle. Thus you cannot kill init even with a SIGKILL even by
603 * mistake.
604 *
605 * Note that we go through the signals twice: once to check the signals that
606 * the kernel can handle, and then we build all the user-level signal handling
607 * stack-frames in one go after that.
608 */
609static int do_signal(sigset_t *oldset, struct pt_regs *regs, int syscall)
610{
611 struct k_sigaction ka;
612 siginfo_t info;
613 int signr;
614
615 /*
616 * We want the common case to go fast, which
617 * is why we may in certain cases get here from
618 * kernel mode. Just return without doing anything
619 * if so.
620 */
621 if (!user_mode(regs))
622 return 0;
623
Andrew Mortonbdb94f32005-06-26 03:27:21 -0700624 if (try_to_freeze())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 goto no_signal;
626
627 if (current->ptrace & PT_SINGLESTEP)
628 ptrace_cancel_bpt(current);
629
630 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
631 if (signr > 0) {
632 handle_signal(signr, &ka, &info, oldset, regs, syscall);
633 if (current->ptrace & PT_SINGLESTEP)
634 ptrace_set_bpt(current);
635 return 1;
636 }
637
638 no_signal:
639 /*
640 * No signal to deliver to the process - restart the syscall.
641 */
642 if (syscall) {
643 if (regs->ARM_r0 == -ERESTART_RESTARTBLOCK) {
644 if (thumb_mode(regs)) {
645 regs->ARM_r7 = __NR_restart_syscall;
646 regs->ARM_pc -= 2;
647 } else {
648 u32 __user *usp;
649
650 regs->ARM_sp -= 12;
651 usp = (u32 __user *)regs->ARM_sp;
652
653 put_user(regs->ARM_pc, &usp[0]);
654 /* swi __NR_restart_syscall */
655 put_user(0xef000000 | __NR_restart_syscall, &usp[1]);
656 /* ldr pc, [sp], #12 */
657 put_user(0xe49df00c, &usp[2]);
658
659 flush_icache_range((unsigned long)usp,
660 (unsigned long)(usp + 3));
661
662 regs->ARM_pc = regs->ARM_sp + 4;
663 }
664 }
665 if (regs->ARM_r0 == -ERESTARTNOHAND ||
666 regs->ARM_r0 == -ERESTARTSYS ||
667 regs->ARM_r0 == -ERESTARTNOINTR) {
668 restart_syscall(regs);
669 }
670 }
671 if (current->ptrace & PT_SINGLESTEP)
672 ptrace_set_bpt(current);
673 return 0;
674}
675
676asmlinkage void
677do_notify_resume(struct pt_regs *regs, unsigned int thread_flags, int syscall)
678{
679 if (thread_flags & _TIF_SIGPENDING)
680 do_signal(&current->blocked, regs, syscall);
681}