blob: dee275014e00884c45199621044c075a01ab1546 [file] [log] [blame]
Benjamin Herrenschmidt22e38f22007-06-04 15:15:49 +10001/*
2 * Common signal handling code for both 32 and 64 bits
3 *
4 * Copyright (c) 2007 Benjamin Herrenschmidt, IBM Coproration
5 * Extracted from signal_32.c and signal_64.c
6 *
7 * This file is subject to the terms and conditions of the GNU General
8 * Public License. See the file README.legal in the main directory of
9 * this archive for more details.
10 */
11
12#include <linux/ptrace.h>
13#include <linux/signal.h>
Benjamin Herrenschmidta3f61dc2007-06-04 17:22:48 +100014#include <asm/uaccess.h>
Benjamin Herrenschmidt22e38f22007-06-04 15:15:49 +100015#include <asm/unistd.h>
16
Christoph Hellwigdb277e92007-06-04 15:15:51 +100017#include "signal.h"
18
19
Christoph Hellwigf478f542007-06-04 15:15:52 +100020#ifdef CONFIG_PPC64
21static inline int is_32bit_task(void)
22{
23 return test_thread_flag(TIF_32BIT);
24}
25#else
26static inline int is_32bit_task(void)
27{
28 return 1;
29}
30#endif
31
Benjamin Herrenschmidta3f61dc2007-06-04 17:22:48 +100032/*
33 * Allocate space for the signal frame
34 */
35void __user * get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
36 size_t frame_size)
37{
38 unsigned long oldsp, newsp;
39
40 /* Default to using normal stack */
41 oldsp = regs->gpr[1];
42
43 /* Check for alt stack */
44 if ((ka->sa.sa_flags & SA_ONSTACK) &&
45 current->sas_ss_size && !on_sig_stack(oldsp))
46 oldsp = (current->sas_ss_sp + current->sas_ss_size);
47
48 /* Get aligned frame */
49 newsp = (oldsp - frame_size) & ~0xFUL;
50
51 /* Check access */
52 if (!access_ok(VERIFY_WRITE, (void __user *)newsp, oldsp - newsp))
53 return NULL;
54
55 return (void __user *)newsp;
56}
57
Christoph Hellwigf478f542007-06-04 15:15:52 +100058
Christoph Hellwigdb277e92007-06-04 15:15:51 +100059/*
60 * Restore the user process's signal mask
61 */
62void restore_sigmask(sigset_t *set)
63{
64 sigdelsetmask(set, ~_BLOCKABLE);
65 spin_lock_irq(&current->sighand->siglock);
66 current->blocked = *set;
67 recalc_sigpending();
68 spin_unlock_irq(&current->sighand->siglock);
69}
70
Christoph Hellwigf478f542007-06-04 15:15:52 +100071static void check_syscall_restart(struct pt_regs *regs, struct k_sigaction *ka,
72 int has_handler)
Benjamin Herrenschmidt22e38f22007-06-04 15:15:49 +100073{
74 unsigned long ret = regs->gpr[3];
75 int restart = 1;
76
77 /* syscall ? */
78 if (TRAP(regs) != 0x0C00)
79 return;
80
81 /* error signalled ? */
82 if (!(regs->ccr & 0x10000000))
83 return;
84
85 switch (ret) {
86 case ERESTART_RESTARTBLOCK:
87 case ERESTARTNOHAND:
88 /* ERESTARTNOHAND means that the syscall should only be
89 * restarted if there was no handler for the signal, and since
90 * we only get here if there is a handler, we dont restart.
91 */
92 restart = !has_handler;
93 break;
94 case ERESTARTSYS:
95 /* ERESTARTSYS means to restart the syscall if there is no
96 * handler or the handler was registered with SA_RESTART
97 */
98 restart = !has_handler || (ka->sa.sa_flags & SA_RESTART) != 0;
99 break;
100 case ERESTARTNOINTR:
101 /* ERESTARTNOINTR means that the syscall should be
102 * called again after the signal handler returns.
103 */
104 break;
105 default:
106 return;
107 }
108 if (restart) {
109 if (ret == ERESTART_RESTARTBLOCK)
110 regs->gpr[0] = __NR_restart_syscall;
111 else
112 regs->gpr[3] = regs->orig_gpr3;
113 regs->nip -= 4;
114 regs->result = 0;
115 } else {
116 regs->result = -EINTR;
117 regs->gpr[3] = EINTR;
118 regs->ccr |= 0x10000000;
119 }
120}
Christoph Hellwig69d15f62007-06-04 15:15:50 +1000121
Christoph Hellwigf478f542007-06-04 15:15:52 +1000122int do_signal(sigset_t *oldset, struct pt_regs *regs)
123{
124 siginfo_t info;
125 int signr;
126 struct k_sigaction ka;
127 int ret;
128 int is32 = is_32bit_task();
129
Christoph Hellwigf478f542007-06-04 15:15:52 +1000130 if (test_thread_flag(TIF_RESTORE_SIGMASK))
131 oldset = &current->saved_sigmask;
132 else if (!oldset)
133 oldset = &current->blocked;
134
135 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
136
Christoph Hellwigf478f542007-06-04 15:15:52 +1000137 /* Is there any syscall restart business here ? */
138 check_syscall_restart(regs, &ka, signr > 0);
139
140 if (signr <= 0) {
141 /* No signal to deliver -- put the saved sigmask back */
142 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
143 clear_thread_flag(TIF_RESTORE_SIGMASK);
144 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
145 }
146 return 0; /* no signals delivered */
147 }
148
149#ifdef CONFIG_PPC64
150 /*
151 * Reenable the DABR before delivering the signal to
152 * user space. The DABR will have been cleared if it
153 * triggered inside the kernel.
154 */
155 if (current->thread.dabr)
156 set_dabr(current->thread.dabr);
157#endif
158
159 if (is32) {
Christoph Hellwigf478f542007-06-04 15:15:52 +1000160 if (ka.sa.sa_flags & SA_SIGINFO)
161 ret = handle_rt_signal32(signr, &ka, &info, oldset,
Benjamin Herrenschmidta3f61dc2007-06-04 17:22:48 +1000162 regs);
Christoph Hellwigf478f542007-06-04 15:15:52 +1000163 else
164 ret = handle_signal32(signr, &ka, &info, oldset,
Benjamin Herrenschmidta3f61dc2007-06-04 17:22:48 +1000165 regs);
Christoph Hellwigf478f542007-06-04 15:15:52 +1000166#ifdef CONFIG_PPC64
167 } else {
168 ret = handle_rt_signal64(signr, &ka, &info, oldset, regs);
169#endif
170 }
171
172 if (ret) {
173 spin_lock_irq(&current->sighand->siglock);
174 sigorsets(&current->blocked, &current->blocked,
175 &ka.sa.sa_mask);
176 if (!(ka.sa.sa_flags & SA_NODEFER))
177 sigaddset(&current->blocked, signr);
178 recalc_sigpending();
179 spin_unlock_irq(&current->sighand->siglock);
180
181 /*
182 * A signal was successfully delivered; the saved sigmask is in
183 * its frame, and we can clear the TIF_RESTORE_SIGMASK flag.
184 */
185 if (test_thread_flag(TIF_RESTORE_SIGMASK))
186 clear_thread_flag(TIF_RESTORE_SIGMASK);
187 }
188
189 return ret;
190}
191
Christoph Hellwig69d15f62007-06-04 15:15:50 +1000192long sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss,
193 unsigned long r5, unsigned long r6, unsigned long r7,
194 unsigned long r8, struct pt_regs *regs)
195{
196 return do_sigaltstack(uss, uoss, regs->gpr[1]);
197}