blob: 9531845bf6611e9a42bb78328b080168e482bd99 [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
Chris Metcalf867e3592010-05-28 23:09:12 -040040/*
41 * Do a signal return; undo the signal stack.
42 */
43
44int restore_sigcontext(struct pt_regs *regs,
Chris Metcalf81711ce2010-12-14 16:07:25 -050045 struct sigcontext __user *sc)
Chris Metcalf867e3592010-05-28 23:09:12 -040046{
47 int err = 0;
48 int i;
49
50 /* Always make any pending restarted system calls return -EINTR */
51 current_thread_info()->restart_block.fn = do_no_restart_syscall;
52
Chris Metcalf74fca9d2010-09-15 11:16:08 -040053 /*
54 * Enforce that sigcontext is like pt_regs, and doesn't mess
55 * up our stack alignment rules.
56 */
57 BUILD_BUG_ON(sizeof(struct sigcontext) != sizeof(struct pt_regs));
58 BUILD_BUG_ON(sizeof(struct sigcontext) % 8 != 0);
59
Chris Metcalf867e3592010-05-28 23:09:12 -040060 for (i = 0; i < sizeof(struct pt_regs)/sizeof(long); ++i)
Chris Metcalf74fca9d2010-09-15 11:16:08 -040061 err |= __get_user(regs->regs[i], &sc->gregs[i]);
Chris Metcalf867e3592010-05-28 23:09:12 -040062
Chris Metcalf1deb9c52010-10-28 15:47:06 -040063 /* Ensure that the PL is always set to USER_PL. */
64 regs->ex1 = PL_ICS_EX1(USER_PL, EX1_ICS(regs->ex1));
65
Chris Metcalf867e3592010-05-28 23:09:12 -040066 regs->faultnum = INT_SWINT_1_SIGRETURN;
67
Chris Metcalf867e3592010-05-28 23:09:12 -040068 return err;
69}
70
Chris Metcalf571d76a2011-05-16 14:23:44 -040071void signal_fault(const char *type, struct pt_regs *regs,
72 void __user *frame, int sig)
73{
74 trace_unhandled_signal(type, regs, (unsigned long)frame, SIGSEGV);
75 force_sigsegv(sig, current);
76}
77
Chris Metcalf81711ce2010-12-14 16:07:25 -050078/* The assembly shim for this function arranges to ignore the return value. */
Chris Metcalf6b14e412012-10-23 13:30:54 -040079SYSCALL_DEFINE0(rt_sigreturn)
Chris Metcalf867e3592010-05-28 23:09:12 -040080{
Chris Metcalf6b14e412012-10-23 13:30:54 -040081 struct pt_regs *regs = current_pt_regs();
Chris Metcalf867e3592010-05-28 23:09:12 -040082 struct rt_sigframe __user *frame =
83 (struct rt_sigframe __user *)(regs->sp);
84 sigset_t set;
Chris Metcalf867e3592010-05-28 23:09:12 -040085
86 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
87 goto badframe;
88 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
89 goto badframe;
90
Matt Flemingad092332012-02-14 11:41:06 +000091 set_current_blocked(&set);
Chris Metcalf867e3592010-05-28 23:09:12 -040092
Chris Metcalf81711ce2010-12-14 16:07:25 -050093 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
Chris Metcalf867e3592010-05-28 23:09:12 -040094 goto badframe;
95
Al Viro47669ab2012-12-23 03:50:34 -050096 if (restore_altstack(&frame->uc.uc_stack))
Chris Metcalf867e3592010-05-28 23:09:12 -040097 goto badframe;
98
Chris Metcalf81711ce2010-12-14 16:07:25 -050099 return 0;
Chris Metcalf867e3592010-05-28 23:09:12 -0400100
101badframe:
Chris Metcalf571d76a2011-05-16 14:23:44 -0400102 signal_fault("bad sigreturn frame", regs, frame, 0);
Chris Metcalf867e3592010-05-28 23:09:12 -0400103 return 0;
104}
105
106/*
107 * Set up a signal frame.
108 */
109
110int setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
111{
112 int i, err = 0;
113
114 for (i = 0; i < sizeof(struct pt_regs)/sizeof(long); ++i)
Chris Metcalf74fca9d2010-09-15 11:16:08 -0400115 err |= __put_user(regs->regs[i], &sc->gregs[i]);
Chris Metcalf867e3592010-05-28 23:09:12 -0400116
117 return err;
118}
119
120/*
121 * Determine which stack to use..
122 */
123static inline void __user *get_sigframe(struct k_sigaction *ka,
124 struct pt_regs *regs,
125 size_t frame_size)
126{
127 unsigned long sp;
128
129 /* Default to using normal stack */
130 sp = regs->sp;
131
132 /*
133 * If we are on the alternate signal stack and would overflow
134 * it, don't. Return an always-bogus address instead so we
135 * will die with SIGSEGV.
136 */
137 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
Chris Metcalf0707ad32010-06-25 17:04:17 -0400138 return (void __user __force *)-1UL;
Chris Metcalf867e3592010-05-28 23:09:12 -0400139
140 /* This is the X/Open sanctioned signal stack switching. */
141 if (ka->sa.sa_flags & SA_ONSTACK) {
142 if (sas_ss_flags(sp) == 0)
143 sp = current->sas_ss_sp + current->sas_ss_size;
144 }
145
146 sp -= frame_size;
147 /*
148 * Align the stack pointer according to the TILE ABI,
149 * i.e. so that on function entry (sp & 15) == 0.
150 */
151 sp &= -16UL;
152 return (void __user *) sp;
153}
154
155static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
156 sigset_t *set, struct pt_regs *regs)
157{
158 unsigned long restorer;
159 struct rt_sigframe __user *frame;
160 int err = 0;
161 int usig;
162
163 frame = get_sigframe(ka, regs, sizeof(*frame));
164
165 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
166 goto give_sigsegv;
167
168 usig = current_thread_info()->exec_domain
169 && current_thread_info()->exec_domain->signal_invmap
170 && sig < 32
171 ? current_thread_info()->exec_domain->signal_invmap[sig]
172 : sig;
173
174 /* Always write at least the signal number for the stack backtracer. */
175 if (ka->sa.sa_flags & SA_SIGINFO) {
176 /* At sigreturn time, restore the callee-save registers too. */
177 err |= copy_siginfo_to_user(&frame->info, info);
178 regs->flags |= PT_FLAGS_RESTORE_REGS;
179 } else {
180 err |= __put_user(info->si_signo, &frame->info.si_signo);
181 }
182
183 /* Create the ucontext. */
184 err |= __clear_user(&frame->save_area, sizeof(frame->save_area));
185 err |= __put_user(0, &frame->uc.uc_flags);
Chris Metcalf0707ad32010-06-25 17:04:17 -0400186 err |= __put_user(NULL, &frame->uc.uc_link);
Al Viro47669ab2012-12-23 03:50:34 -0500187 err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
Chris Metcalf867e3592010-05-28 23:09:12 -0400188 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs);
189 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
190 if (err)
191 goto give_sigsegv;
192
193 restorer = VDSO_BASE;
194 if (ka->sa.sa_flags & SA_RESTORER)
195 restorer = (unsigned long) ka->sa.sa_restorer;
196
197 /*
198 * Set up registers for signal handler.
199 * Registers that we don't modify keep the value they had from
200 * user-space at the time we took the signal.
Chris Metcalf74fca9d2010-09-15 11:16:08 -0400201 * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
202 * since some things rely on this (e.g. glibc's debug/segfault.c).
Chris Metcalf867e3592010-05-28 23:09:12 -0400203 */
204 regs->pc = (unsigned long) ka->sa.sa_handler;
205 regs->ex1 = PL_ICS_EX1(USER_PL, 1); /* set crit sec in handler */
206 regs->sp = (unsigned long) frame;
207 regs->lr = restorer;
208 regs->regs[0] = (unsigned long) usig;
Chris Metcalf74fca9d2010-09-15 11:16:08 -0400209 regs->regs[1] = (unsigned long) &frame->info;
210 regs->regs[2] = (unsigned long) &frame->uc;
211 regs->flags |= PT_FLAGS_CALLER_SAVES;
Chris Metcalf867e3592010-05-28 23:09:12 -0400212 return 0;
213
214give_sigsegv:
Chris Metcalf571d76a2011-05-16 14:23:44 -0400215 signal_fault("bad setup frame", regs, frame, sig);
Chris Metcalf867e3592010-05-28 23:09:12 -0400216 return -EFAULT;
217}
218
219/*
220 * OK, we're invoking a handler
221 */
222
Al Viroa610d6e2012-05-21 23:42:15 -0400223static void handle_signal(unsigned long sig, siginfo_t *info,
Al Virob7f9a112012-05-02 09:59:21 -0400224 struct k_sigaction *ka,
Chris Metcalf867e3592010-05-28 23:09:12 -0400225 struct pt_regs *regs)
226{
Al Virob7f9a112012-05-02 09:59:21 -0400227 sigset_t *oldset = sigmask_to_save();
Chris Metcalf867e3592010-05-28 23:09:12 -0400228 int ret;
229
Chris Metcalf867e3592010-05-28 23:09:12 -0400230 /* Are we from a system call? */
231 if (regs->faultnum == INT_SWINT_1) {
232 /* If so, check system call restarting.. */
233 switch (regs->regs[0]) {
234 case -ERESTART_RESTARTBLOCK:
235 case -ERESTARTNOHAND:
236 regs->regs[0] = -EINTR;
237 break;
238
239 case -ERESTARTSYS:
240 if (!(ka->sa.sa_flags & SA_RESTART)) {
241 regs->regs[0] = -EINTR;
242 break;
243 }
244 /* fallthrough */
245 case -ERESTARTNOINTR:
246 /* Reload caller-saves to restore r0..r5 and r10. */
247 regs->flags |= PT_FLAGS_CALLER_SAVES;
248 regs->regs[0] = regs->orig_r0;
249 regs->pc -= 8;
250 }
251 }
252
253 /* Set up the stack frame */
254#ifdef CONFIG_COMPAT
255 if (is_compat_task())
256 ret = compat_setup_rt_frame(sig, ka, info, oldset, regs);
257 else
258#endif
259 ret = setup_rt_frame(sig, ka, info, oldset, regs);
Al Viroa610d6e2012-05-21 23:42:15 -0400260 if (ret)
261 return;
Al Viroa9253282012-05-26 22:22:53 -0400262 signal_delivered(sig, info, ka, regs,
263 test_thread_flag(TIF_SINGLESTEP));
Chris Metcalf867e3592010-05-28 23:09:12 -0400264}
265
266/*
267 * Note that 'init' is a special process: it doesn't get signals it doesn't
268 * want to handle. Thus you cannot kill init even with a SIGKILL even by
269 * mistake.
270 */
271void do_signal(struct pt_regs *regs)
272{
273 siginfo_t info;
274 int signr;
275 struct k_sigaction ka;
Chris Metcalf867e3592010-05-28 23:09:12 -0400276
277 /*
278 * i386 will check if we're coming from kernel mode and bail out
279 * here. In my experience this just turns weird crashes into
280 * weird spin-hangs. But if we find a case where this seems
281 * helpful, we can reinstate the check on "!user_mode(regs)".
282 */
283
Chris Metcalf867e3592010-05-28 23:09:12 -0400284 signr = get_signal_to_deliver(&info, &ka, regs, NULL);
285 if (signr > 0) {
286 /* Whee! Actually deliver the signal. */
Al Viroa610d6e2012-05-21 23:42:15 -0400287 handle_signal(signr, &info, &ka, regs);
Chris Metcalf34a89d22010-10-28 15:03:30 -0400288 goto done;
Chris Metcalf867e3592010-05-28 23:09:12 -0400289 }
290
291 /* Did we come from a system call? */
292 if (regs->faultnum == INT_SWINT_1) {
293 /* Restart the system call - no handlers present */
294 switch (regs->regs[0]) {
295 case -ERESTARTNOHAND:
296 case -ERESTARTSYS:
297 case -ERESTARTNOINTR:
298 regs->flags |= PT_FLAGS_CALLER_SAVES;
299 regs->regs[0] = regs->orig_r0;
300 regs->pc -= 8;
301 break;
302
303 case -ERESTART_RESTARTBLOCK:
304 regs->flags |= PT_FLAGS_CALLER_SAVES;
305 regs->regs[TREG_SYSCALL_NR] = __NR_restart_syscall;
306 regs->pc -= 8;
307 break;
308 }
309 }
310
311 /* If there's no signal to deliver, just put the saved sigmask back. */
Al Viro51a7b442012-05-21 23:33:55 -0400312 restore_saved_sigmask();
Chris Metcalf34a89d22010-10-28 15:03:30 -0400313
314done:
315 /* Avoid double syscall restart if there are nested signals. */
316 regs->faultnum = INT_SWINT_1_SIGRETURN;
Chris Metcalf867e3592010-05-28 23:09:12 -0400317}
Chris Metcalf571d76a2011-05-16 14:23:44 -0400318
319int show_unhandled_signals = 1;
320
321static int __init crashinfo(char *str)
322{
323 unsigned long val;
324 const char *word;
325
326 if (*str == '\0')
327 val = 2;
328 else if (*str != '=' || strict_strtoul(++str, 0, &val) != 0)
329 return 0;
330 show_unhandled_signals = val;
331 switch (show_unhandled_signals) {
332 case 0:
333 word = "No";
334 break;
335 case 1:
336 word = "One-line";
337 break;
338 default:
339 word = "Detailed";
340 break;
341 }
342 pr_info("%s crash reports will be generated on the console\n", word);
343 return 1;
344}
345__setup("crashinfo", crashinfo);
346
347static void dump_mem(void __user *address)
348{
349 void __user *addr;
350 enum { region_size = 256, bytes_per_line = 16 };
351 int i, j, k;
352 int found_readable_mem = 0;
353
354 pr_err("\n");
355 if (!access_ok(VERIFY_READ, address, 1)) {
356 pr_err("Not dumping at address 0x%lx (kernel address)\n",
357 (unsigned long)address);
358 return;
359 }
360
361 addr = (void __user *)
362 (((unsigned long)address & -bytes_per_line) - region_size/2);
363 if (addr > address)
364 addr = NULL;
365 for (i = 0; i < region_size;
366 addr += bytes_per_line, i += bytes_per_line) {
367 unsigned char buf[bytes_per_line];
368 char line[100];
369 if (copy_from_user(buf, addr, bytes_per_line))
370 continue;
371 if (!found_readable_mem) {
372 pr_err("Dumping memory around address 0x%lx:\n",
373 (unsigned long)address);
374 found_readable_mem = 1;
375 }
376 j = sprintf(line, REGFMT":", (unsigned long)addr);
377 for (k = 0; k < bytes_per_line; ++k)
378 j += sprintf(&line[j], " %02x", buf[k]);
379 pr_err("%s\n", line);
380 }
381 if (!found_readable_mem)
382 pr_err("No readable memory around address 0x%lx\n",
383 (unsigned long)address);
384}
385
386void trace_unhandled_signal(const char *type, struct pt_regs *regs,
387 unsigned long address, int sig)
388{
389 struct task_struct *tsk = current;
390
391 if (show_unhandled_signals == 0)
392 return;
393
394 /* If the signal is handled, don't show it here. */
395 if (!is_global_init(tsk)) {
396 void __user *handler =
397 tsk->sighand->action[sig-1].sa.sa_handler;
398 if (handler != SIG_IGN && handler != SIG_DFL)
399 return;
400 }
401
402 /* Rate-limit the one-line output, not the detailed output. */
403 if (show_unhandled_signals <= 1 && !printk_ratelimit())
404 return;
405
406 printk("%s%s[%d]: %s at %lx pc "REGFMT" signal %d",
407 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
408 tsk->comm, task_pid_nr(tsk), type, address, regs->pc, sig);
409
410 print_vma_addr(KERN_CONT " in ", regs->pc);
411
412 printk(KERN_CONT "\n");
413
414 if (show_unhandled_signals > 1) {
415 switch (sig) {
416 case SIGILL:
417 case SIGFPE:
418 case SIGSEGV:
419 case SIGBUS:
420 pr_err("User crash: signal %d,"
421 " trap %ld, address 0x%lx\n",
422 sig, regs->faultnum, address);
423 show_regs(regs);
424 dump_mem((void __user *)address);
425 break;
426 default:
427 pr_err("User crash: signal %d, trap %ld\n",
428 sig, regs->faultnum);
429 break;
430 }
431 }
432}