blob: e60990c0a1f01ef91cf85d5dd693ad98e018a4b8 [file] [log] [blame]
Bryan Wu1394f032007-05-06 14:50:22 -07001/*
Mike Frysingere8f263d2010-01-26 07:33:53 +00002 * Copyright 2004-2010 Analog Devices Inc.
Bryan Wu1394f032007-05-06 14:50:22 -07003 *
Robin Getz96f10502009-09-24 14:11:24 +00004 * Licensed under the GPL-2 or later
Bryan Wu1394f032007-05-06 14:50:22 -07005 */
6
7#include <linux/signal.h>
8#include <linux/syscalls.h>
9#include <linux/ptrace.h>
10#include <linux/tty.h>
11#include <linux/personality.h>
12#include <linux/binfmts.h>
13#include <linux/freezer.h>
Mike Frysinger1f83b8f2007-07-12 22:58:21 +080014#include <linux/uaccess.h>
Barry Songd1be2e42009-12-08 09:55:37 +000015#include <linux/tracehook.h>
Bryan Wu1394f032007-05-06 14:50:22 -070016
Bryan Wu1394f032007-05-06 14:50:22 -070017#include <asm/cacheflush.h>
18#include <asm/ucontext.h>
Bernd Schmidt697a9d62008-04-24 02:51:36 +080019#include <asm/fixed_code.h>
Bryan Wu1394f032007-05-06 14:50:22 -070020
21#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
22
Bernd Schmidt8513c422008-05-07 11:41:26 +080023/* Location of the trace bit in SYSCFG. */
24#define TRACE_BITS 0x0001
25
Bryan Wu1394f032007-05-06 14:50:22 -070026struct fdpic_func_descriptor {
27 unsigned long text;
28 unsigned long GOT;
29};
30
31struct rt_sigframe {
32 int sig;
33 struct siginfo *pinfo;
34 void *puc;
Bernd Schmidt697a9d62008-04-24 02:51:36 +080035 /* This is no longer needed by the kernel, but unfortunately userspace
36 * code expects it to be there. */
Bryan Wu1394f032007-05-06 14:50:22 -070037 char retcode[8];
38 struct siginfo info;
39 struct ucontext uc;
40};
41
Mike Frysinger0ddeeca2008-03-07 02:37:41 +080042asmlinkage int sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
Bryan Wu1394f032007-05-06 14:50:22 -070043{
44 return do_sigaltstack(uss, uoss, rdusp());
45}
46
47static inline int
Mike Frysinger0ddeeca2008-03-07 02:37:41 +080048rt_restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc, int *pr0)
Bryan Wu1394f032007-05-06 14:50:22 -070049{
50 unsigned long usp = 0;
51 int err = 0;
52
53#define RESTORE(x) err |= __get_user(regs->x, &sc->sc_##x)
54
55 /* restore passed registers */
56 RESTORE(r0); RESTORE(r1); RESTORE(r2); RESTORE(r3);
57 RESTORE(r4); RESTORE(r5); RESTORE(r6); RESTORE(r7);
58 RESTORE(p0); RESTORE(p1); RESTORE(p2); RESTORE(p3);
59 RESTORE(p4); RESTORE(p5);
60 err |= __get_user(usp, &sc->sc_usp);
61 wrusp(usp);
62 RESTORE(a0w); RESTORE(a1w);
63 RESTORE(a0x); RESTORE(a1x);
64 RESTORE(astat);
65 RESTORE(rets);
66 RESTORE(pc);
67 RESTORE(retx);
68 RESTORE(fp);
69 RESTORE(i0); RESTORE(i1); RESTORE(i2); RESTORE(i3);
70 RESTORE(m0); RESTORE(m1); RESTORE(m2); RESTORE(m3);
71 RESTORE(l0); RESTORE(l1); RESTORE(l2); RESTORE(l3);
72 RESTORE(b0); RESTORE(b1); RESTORE(b2); RESTORE(b3);
73 RESTORE(lc0); RESTORE(lc1);
74 RESTORE(lt0); RESTORE(lt1);
75 RESTORE(lb0); RESTORE(lb1);
76 RESTORE(seqstat);
77
78 regs->orig_p0 = -1; /* disable syscall checks */
79
80 *pr0 = regs->r0;
81 return err;
82}
83
84asmlinkage int do_rt_sigreturn(unsigned long __unused)
85{
86 struct pt_regs *regs = (struct pt_regs *)__unused;
87 unsigned long usp = rdusp();
88 struct rt_sigframe *frame = (struct rt_sigframe *)(usp);
89 sigset_t set;
90 int r0;
91
92 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
93 goto badframe;
94 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
95 goto badframe;
96
97 sigdelsetmask(&set, ~_BLOCKABLE);
98 spin_lock_irq(&current->sighand->siglock);
99 current->blocked = set;
100 recalc_sigpending();
101 spin_unlock_irq(&current->sighand->siglock);
102
103 if (rt_restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
104 goto badframe;
105
106 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->usp) == -EFAULT)
107 goto badframe;
108
109 return r0;
110
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800111 badframe:
Bryan Wu1394f032007-05-06 14:50:22 -0700112 force_sig(SIGSEGV, current);
113 return 0;
114}
115
116static inline int rt_setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs)
117{
118 int err = 0;
119
120#define SETUP(x) err |= __put_user(regs->x, &sc->sc_##x)
121
122 SETUP(r0); SETUP(r1); SETUP(r2); SETUP(r3);
123 SETUP(r4); SETUP(r5); SETUP(r6); SETUP(r7);
124 SETUP(p0); SETUP(p1); SETUP(p2); SETUP(p3);
125 SETUP(p4); SETUP(p5);
126 err |= __put_user(rdusp(), &sc->sc_usp);
127 SETUP(a0w); SETUP(a1w);
128 SETUP(a0x); SETUP(a1x);
129 SETUP(astat);
130 SETUP(rets);
131 SETUP(pc);
132 SETUP(retx);
133 SETUP(fp);
134 SETUP(i0); SETUP(i1); SETUP(i2); SETUP(i3);
135 SETUP(m0); SETUP(m1); SETUP(m2); SETUP(m3);
136 SETUP(l0); SETUP(l1); SETUP(l2); SETUP(l3);
137 SETUP(b0); SETUP(b1); SETUP(b2); SETUP(b3);
138 SETUP(lc0); SETUP(lc1);
139 SETUP(lt0); SETUP(lt1);
140 SETUP(lb0); SETUP(lb1);
141 SETUP(seqstat);
142
143 return err;
144}
145
Bryan Wu1394f032007-05-06 14:50:22 -0700146static inline void *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
147 size_t frame_size)
148{
149 unsigned long usp;
150
151 /* Default to using normal stack. */
152 usp = rdusp();
153
154 /* This is the X/Open sanctioned signal stack switching. */
155 if (ka->sa.sa_flags & SA_ONSTACK) {
156 if (!on_sig_stack(usp))
157 usp = current->sas_ss_sp + current->sas_ss_size;
158 }
159 return (void *)((usp - frame_size) & -8UL);
160}
161
162static int
163setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t * info,
164 sigset_t * set, struct pt_regs *regs)
165{
166 struct rt_sigframe *frame;
167 int err = 0;
168
169 frame = get_sigframe(ka, regs, sizeof(*frame));
170
171 err |= __put_user((current_thread_info()->exec_domain
172 && current_thread_info()->exec_domain->signal_invmap
173 && sig < 32
174 ? current_thread_info()->exec_domain->
175 signal_invmap[sig] : sig), &frame->sig);
176
177 err |= __put_user(&frame->info, &frame->pinfo);
178 err |= __put_user(&frame->uc, &frame->puc);
179 err |= copy_siginfo_to_user(&frame->info, info);
180
181 /* Create the ucontext. */
182 err |= __put_user(0, &frame->uc.uc_flags);
183 err |= __put_user(0, &frame->uc.uc_link);
184 err |=
185 __put_user((void *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
186 err |= __put_user(sas_ss_flags(rdusp()), &frame->uc.uc_stack.ss_flags);
187 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
188 err |= rt_setup_sigcontext(&frame->uc.uc_mcontext, regs);
189 err |= copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
190
Bryan Wu1394f032007-05-06 14:50:22 -0700191 if (err)
192 goto give_sigsegv;
193
Bryan Wu1394f032007-05-06 14:50:22 -0700194 /* Set up registers for signal handler */
195 wrusp((unsigned long)frame);
WANG Congecd0fa92008-04-29 00:59:15 -0700196 if (current->personality & FDPIC_FUNCPTRS) {
Bryan Wu1394f032007-05-06 14:50:22 -0700197 struct fdpic_func_descriptor __user *funcptr =
198 (struct fdpic_func_descriptor *) ka->sa.sa_handler;
199 __get_user(regs->pc, &funcptr->text);
200 __get_user(regs->p3, &funcptr->GOT);
201 } else
202 regs->pc = (unsigned long)ka->sa.sa_handler;
Bernd Schmidt697a9d62008-04-24 02:51:36 +0800203 regs->rets = SIGRETURN_STUB;
Bryan Wu1394f032007-05-06 14:50:22 -0700204
205 regs->r0 = frame->sig;
206 regs->r1 = (unsigned long)(&frame->info);
207 regs->r2 = (unsigned long)(&frame->uc);
208
209 return 0;
210
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800211 give_sigsegv:
Bryan Wu1394f032007-05-06 14:50:22 -0700212 if (sig == SIGSEGV)
213 ka->sa.sa_handler = SIG_DFL;
214 force_sig(SIGSEGV, current);
215 return -EFAULT;
216}
217
218static inline void
219handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
220{
221 switch (regs->r0) {
222 case -ERESTARTNOHAND:
223 if (!has_handler)
224 goto do_restart;
225 regs->r0 = -EINTR;
226 break;
227
228 case -ERESTARTSYS:
229 if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
230 regs->r0 = -EINTR;
231 break;
232 }
233 /* fallthrough */
234 case -ERESTARTNOINTR:
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800235 do_restart:
Bryan Wu1394f032007-05-06 14:50:22 -0700236 regs->p0 = regs->orig_p0;
237 regs->r0 = regs->orig_r0;
238 regs->pc -= 2;
239 break;
240 }
241}
242
243/*
244 * OK, we're invoking a handler
245 */
246static int
247handle_signal(int sig, siginfo_t *info, struct k_sigaction *ka,
248 sigset_t *oldset, struct pt_regs *regs)
249{
250 int ret;
251
252 /* are we from a system call? to see pt_regs->orig_p0 */
253 if (regs->orig_p0 >= 0)
254 /* If so, check system call restarting.. */
255 handle_restart(regs, ka, 1);
256
257 /* set up the stack frame */
258 ret = setup_rt_frame(sig, ka, info, oldset, regs);
259
260 if (ret == 0) {
261 spin_lock_irq(&current->sighand->siglock);
262 sigorsets(&current->blocked, &current->blocked,
263 &ka->sa.sa_mask);
264 if (!(ka->sa.sa_flags & SA_NODEFER))
265 sigaddset(&current->blocked, sig);
266 recalc_sigpending();
267 spin_unlock_irq(&current->sighand->siglock);
268 }
269 return ret;
270}
271
272/*
273 * Note that 'init' is a special process: it doesn't get signals it doesn't
274 * want to handle. Thus you cannot kill init even with a SIGKILL even by
275 * mistake.
276 *
277 * Note that we go through the signals twice: once to check the signals
278 * that the kernel can handle, and then we build all the user-level signal
279 * handling stack-frames in one go after that.
280 */
281asmlinkage void do_signal(struct pt_regs *regs)
282{
283 siginfo_t info;
284 int signr;
285 struct k_sigaction ka;
286 sigset_t *oldset;
287
288 current->thread.esp0 = (unsigned long)regs;
289
290 if (try_to_freeze())
291 goto no_signal;
292
293 if (test_thread_flag(TIF_RESTORE_SIGMASK))
294 oldset = &current->saved_sigmask;
295 else
296 oldset = &current->blocked;
297
298 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
299 if (signr > 0) {
300 /* Whee! Actually deliver the signal. */
301 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
302 /* a signal was successfully delivered; the saved
303 * sigmask will have been stored in the signal frame,
304 * and will be restored by sigreturn, so we can simply
305 * clear the TIF_RESTORE_SIGMASK flag */
306 if (test_thread_flag(TIF_RESTORE_SIGMASK))
307 clear_thread_flag(TIF_RESTORE_SIGMASK);
Mike Frysingere8f263d2010-01-26 07:33:53 +0000308
309 tracehook_signal_handler(signr, &info, &ka, regs, 1);
Bryan Wu1394f032007-05-06 14:50:22 -0700310 }
311
312 return;
313 }
314
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800315 no_signal:
Bryan Wu1394f032007-05-06 14:50:22 -0700316 /* Did we come from a system call? */
317 if (regs->orig_p0 >= 0)
318 /* Restart the system call - no handlers present */
319 handle_restart(regs, NULL, 0);
320
321 /* if there's no signal to deliver, we just put the saved sigmask
322 * back */
323 if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
324 clear_thread_flag(TIF_RESTORE_SIGMASK);
325 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
326 }
327}
Barry Songd1be2e42009-12-08 09:55:37 +0000328
329/*
330 * notification of userspace execution resumption
331 */
332asmlinkage void do_notify_resume(struct pt_regs *regs)
333{
334 if (test_thread_flag(TIF_SIGPENDING) || test_thread_flag(TIF_RESTORE_SIGMASK))
335 do_signal(regs);
336
337 if (test_thread_flag(TIF_NOTIFY_RESUME)) {
338 clear_thread_flag(TIF_NOTIFY_RESUME);
339 tracehook_notify_resume(regs);
340 if (current->replacement_session_keyring)
341 key_replace_session_keyring();
342 }
343}
344