blob: 757407e36696688d59810453b1ffca8da8522a62 [file] [log] [blame]
Chris Metcalf867e3592010-05-28 23:09:12 -04001/*
2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright 2010 Tilera Corporation. All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation, version 2.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
12 * NON INFRINGEMENT. See the GNU General Public License for
13 * more details.
14 */
15
16#include <linux/sched.h>
17#include <linux/mm.h>
18#include <linux/smp.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040019#include <linux/kernel.h>
20#include <linux/signal.h>
21#include <linux/errno.h>
22#include <linux/wait.h>
23#include <linux/unistd.h>
24#include <linux/stddef.h>
25#include <linux/personality.h>
26#include <linux/suspend.h>
27#include <linux/ptrace.h>
28#include <linux/elf.h>
29#include <linux/compat.h>
30#include <linux/syscalls.h>
31#include <linux/uaccess.h>
32#include <asm/processor.h>
33#include <asm/ucontext.h>
34#include <asm/sigframe.h>
Chris Metcalf0707ad32010-06-25 17:04:17 -040035#include <asm/syscalls.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040036#include <arch/interrupts.h>
37
38#define DEBUG_SIG 0
39
40#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
41
42
Chris Metcalfd929b6a2010-10-14 14:34:33 -040043SYSCALL_DEFINE3(sigaltstack, const stack_t __user *, uss,
44 stack_t __user *, uoss, struct pt_regs *, regs)
Chris Metcalf867e3592010-05-28 23:09:12 -040045{
46 return do_sigaltstack(uss, uoss, regs->sp);
47}
48
49
50/*
51 * Do a signal return; undo the signal stack.
52 */
53
54int restore_sigcontext(struct pt_regs *regs,
55 struct sigcontext __user *sc, long *pr0)
56{
57 int err = 0;
58 int i;
59
60 /* Always make any pending restarted system calls return -EINTR */
61 current_thread_info()->restart_block.fn = do_no_restart_syscall;
62
Chris Metcalf74fca9d2010-09-15 11:16:08 -040063 /*
64 * Enforce that sigcontext is like pt_regs, and doesn't mess
65 * up our stack alignment rules.
66 */
67 BUILD_BUG_ON(sizeof(struct sigcontext) != sizeof(struct pt_regs));
68 BUILD_BUG_ON(sizeof(struct sigcontext) % 8 != 0);
69
Chris Metcalf867e3592010-05-28 23:09:12 -040070 for (i = 0; i < sizeof(struct pt_regs)/sizeof(long); ++i)
Chris Metcalf74fca9d2010-09-15 11:16:08 -040071 err |= __get_user(regs->regs[i], &sc->gregs[i]);
Chris Metcalf867e3592010-05-28 23:09:12 -040072
Chris Metcalf1deb9c52010-10-28 15:47:06 -040073 /* Ensure that the PL is always set to USER_PL. */
74 regs->ex1 = PL_ICS_EX1(USER_PL, EX1_ICS(regs->ex1));
75
Chris Metcalf867e3592010-05-28 23:09:12 -040076 regs->faultnum = INT_SWINT_1_SIGRETURN;
77
Chris Metcalf74fca9d2010-09-15 11:16:08 -040078 err |= __get_user(*pr0, &sc->gregs[0]);
Chris Metcalf867e3592010-05-28 23:09:12 -040079 return err;
80}
81
Chris Metcalf0707ad32010-06-25 17:04:17 -040082/* sigreturn() returns long since it restores r0 in the interrupted code. */
Chris Metcalfd929b6a2010-10-14 14:34:33 -040083SYSCALL_DEFINE1(rt_sigreturn, struct pt_regs *, regs)
Chris Metcalf867e3592010-05-28 23:09:12 -040084{
85 struct rt_sigframe __user *frame =
86 (struct rt_sigframe __user *)(regs->sp);
87 sigset_t set;
88 long r0;
89
90 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
91 goto badframe;
92 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
93 goto badframe;
94
95 sigdelsetmask(&set, ~_BLOCKABLE);
96 spin_lock_irq(&current->sighand->siglock);
97 current->blocked = set;
98 recalc_sigpending();
99 spin_unlock_irq(&current->sighand->siglock);
100
101 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
102 goto badframe;
103
104 if (do_sigaltstack(&frame->uc.uc_stack, NULL, regs->sp) == -EFAULT)
105 goto badframe;
106
107 return r0;
108
109badframe:
110 force_sig(SIGSEGV, current);
111 return 0;
112}
113
114/*
115 * Set up a signal frame.
116 */
117
118int setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
119{
120 int i, err = 0;
121
122 for (i = 0; i < sizeof(struct pt_regs)/sizeof(long); ++i)
Chris Metcalf74fca9d2010-09-15 11:16:08 -0400123 err |= __put_user(regs->regs[i], &sc->gregs[i]);
Chris Metcalf867e3592010-05-28 23:09:12 -0400124
125 return err;
126}
127
128/*
129 * Determine which stack to use..
130 */
131static inline void __user *get_sigframe(struct k_sigaction *ka,
132 struct pt_regs *regs,
133 size_t frame_size)
134{
135 unsigned long sp;
136
137 /* Default to using normal stack */
138 sp = regs->sp;
139
140 /*
141 * If we are on the alternate signal stack and would overflow
142 * it, don't. Return an always-bogus address instead so we
143 * will die with SIGSEGV.
144 */
145 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
Chris Metcalf0707ad32010-06-25 17:04:17 -0400146 return (void __user __force *)-1UL;
Chris Metcalf867e3592010-05-28 23:09:12 -0400147
148 /* This is the X/Open sanctioned signal stack switching. */
149 if (ka->sa.sa_flags & SA_ONSTACK) {
150 if (sas_ss_flags(sp) == 0)
151 sp = current->sas_ss_sp + current->sas_ss_size;
152 }
153
154 sp -= frame_size;
155 /*
156 * Align the stack pointer according to the TILE ABI,
157 * i.e. so that on function entry (sp & 15) == 0.
158 */
159 sp &= -16UL;
160 return (void __user *) sp;
161}
162
163static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
164 sigset_t *set, struct pt_regs *regs)
165{
166 unsigned long restorer;
167 struct rt_sigframe __user *frame;
168 int err = 0;
169 int usig;
170
171 frame = get_sigframe(ka, regs, sizeof(*frame));
172
173 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
174 goto give_sigsegv;
175
176 usig = current_thread_info()->exec_domain
177 && current_thread_info()->exec_domain->signal_invmap
178 && sig < 32
179 ? current_thread_info()->exec_domain->signal_invmap[sig]
180 : sig;
181
182 /* Always write at least the signal number for the stack backtracer. */
183 if (ka->sa.sa_flags & SA_SIGINFO) {
184 /* At sigreturn time, restore the callee-save registers too. */
185 err |= copy_siginfo_to_user(&frame->info, info);
186 regs->flags |= PT_FLAGS_RESTORE_REGS;
187 } else {
188 err |= __put_user(info->si_signo, &frame->info.si_signo);
189 }
190
191 /* Create the ucontext. */
192 err |= __clear_user(&frame->save_area, sizeof(frame->save_area));
193 err |= __put_user(0, &frame->uc.uc_flags);
Chris Metcalf0707ad32010-06-25 17:04:17 -0400194 err |= __put_user(NULL, &frame->uc.uc_link);
195 err |= __put_user((void __user *)(current->sas_ss_sp),
Chris Metcalf867e3592010-05-28 23:09:12 -0400196 &frame->uc.uc_stack.ss_sp);
197 err |= __put_user(sas_ss_flags(regs->sp),
198 &frame->uc.uc_stack.ss_flags);
199 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
200 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs);
201 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
202 if (err)
203 goto give_sigsegv;
204
205 restorer = VDSO_BASE;
206 if (ka->sa.sa_flags & SA_RESTORER)
207 restorer = (unsigned long) ka->sa.sa_restorer;
208
209 /*
210 * Set up registers for signal handler.
211 * Registers that we don't modify keep the value they had from
212 * user-space at the time we took the signal.
Chris Metcalf74fca9d2010-09-15 11:16:08 -0400213 * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
214 * since some things rely on this (e.g. glibc's debug/segfault.c).
Chris Metcalf867e3592010-05-28 23:09:12 -0400215 */
216 regs->pc = (unsigned long) ka->sa.sa_handler;
217 regs->ex1 = PL_ICS_EX1(USER_PL, 1); /* set crit sec in handler */
218 regs->sp = (unsigned long) frame;
219 regs->lr = restorer;
220 regs->regs[0] = (unsigned long) usig;
Chris Metcalf74fca9d2010-09-15 11:16:08 -0400221 regs->regs[1] = (unsigned long) &frame->info;
222 regs->regs[2] = (unsigned long) &frame->uc;
223 regs->flags |= PT_FLAGS_CALLER_SAVES;
Chris Metcalf867e3592010-05-28 23:09:12 -0400224
225 /*
226 * Notify any tracer that was single-stepping it.
227 * The tracer may want to single-step inside the
228 * handler too.
229 */
230 if (test_thread_flag(TIF_SINGLESTEP))
231 ptrace_notify(SIGTRAP);
232
233 return 0;
234
235give_sigsegv:
236 force_sigsegv(sig, current);
237 return -EFAULT;
238}
239
240/*
241 * OK, we're invoking a handler
242 */
243
244static int handle_signal(unsigned long sig, siginfo_t *info,
245 struct k_sigaction *ka, sigset_t *oldset,
246 struct pt_regs *regs)
247{
248 int ret;
249
250
251 /* Are we from a system call? */
252 if (regs->faultnum == INT_SWINT_1) {
253 /* If so, check system call restarting.. */
254 switch (regs->regs[0]) {
255 case -ERESTART_RESTARTBLOCK:
256 case -ERESTARTNOHAND:
257 regs->regs[0] = -EINTR;
258 break;
259
260 case -ERESTARTSYS:
261 if (!(ka->sa.sa_flags & SA_RESTART)) {
262 regs->regs[0] = -EINTR;
263 break;
264 }
265 /* fallthrough */
266 case -ERESTARTNOINTR:
267 /* Reload caller-saves to restore r0..r5 and r10. */
268 regs->flags |= PT_FLAGS_CALLER_SAVES;
269 regs->regs[0] = regs->orig_r0;
270 regs->pc -= 8;
271 }
272 }
273
274 /* Set up the stack frame */
275#ifdef CONFIG_COMPAT
276 if (is_compat_task())
277 ret = compat_setup_rt_frame(sig, ka, info, oldset, regs);
278 else
279#endif
280 ret = setup_rt_frame(sig, ka, info, oldset, regs);
281 if (ret == 0) {
282 /* This code is only called from system calls or from
283 * the work_pending path in the return-to-user code, and
284 * either way we can re-enable interrupts unconditionally.
285 */
286 spin_lock_irq(&current->sighand->siglock);
287 sigorsets(&current->blocked,
288 &current->blocked, &ka->sa.sa_mask);
289 if (!(ka->sa.sa_flags & SA_NODEFER))
290 sigaddset(&current->blocked, sig);
291 recalc_sigpending();
292 spin_unlock_irq(&current->sighand->siglock);
293 }
294
295 return ret;
296}
297
298/*
299 * Note that 'init' is a special process: it doesn't get signals it doesn't
300 * want to handle. Thus you cannot kill init even with a SIGKILL even by
301 * mistake.
302 */
303void do_signal(struct pt_regs *regs)
304{
305 siginfo_t info;
306 int signr;
307 struct k_sigaction ka;
308 sigset_t *oldset;
309
310 /*
311 * i386 will check if we're coming from kernel mode and bail out
312 * here. In my experience this just turns weird crashes into
313 * weird spin-hangs. But if we find a case where this seems
314 * helpful, we can reinstate the check on "!user_mode(regs)".
315 */
316
317 if (current_thread_info()->status & TS_RESTORE_SIGMASK)
318 oldset = &current->saved_sigmask;
319 else
320 oldset = &current->blocked;
321
322 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
323 if (signr > 0) {
324 /* Whee! Actually deliver the signal. */
325 if (handle_signal(signr, &info, &ka, oldset, regs) == 0) {
326 /*
327 * A signal was successfully delivered; the saved
328 * sigmask will have been stored in the signal frame,
329 * and will be restored by sigreturn, so we can simply
330 * clear the TS_RESTORE_SIGMASK flag.
331 */
332 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
333 }
334
Chris Metcalf34a89d22010-10-28 15:03:30 -0400335 goto done;
Chris Metcalf867e3592010-05-28 23:09:12 -0400336 }
337
338 /* Did we come from a system call? */
339 if (regs->faultnum == INT_SWINT_1) {
340 /* Restart the system call - no handlers present */
341 switch (regs->regs[0]) {
342 case -ERESTARTNOHAND:
343 case -ERESTARTSYS:
344 case -ERESTARTNOINTR:
345 regs->flags |= PT_FLAGS_CALLER_SAVES;
346 regs->regs[0] = regs->orig_r0;
347 regs->pc -= 8;
348 break;
349
350 case -ERESTART_RESTARTBLOCK:
351 regs->flags |= PT_FLAGS_CALLER_SAVES;
352 regs->regs[TREG_SYSCALL_NR] = __NR_restart_syscall;
353 regs->pc -= 8;
354 break;
355 }
356 }
357
358 /* If there's no signal to deliver, just put the saved sigmask back. */
359 if (current_thread_info()->status & TS_RESTORE_SIGMASK) {
360 current_thread_info()->status &= ~TS_RESTORE_SIGMASK;
361 sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
362 }
Chris Metcalf34a89d22010-10-28 15:03:30 -0400363
364done:
365 /* Avoid double syscall restart if there are nested signals. */
366 regs->faultnum = INT_SWINT_1_SIGRETURN;
Chris Metcalf867e3592010-05-28 23:09:12 -0400367}