blob: f2bf557bb005f93ac2b030078684cb790d9be6ed [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>
Ingo Molnarb17b0152017-02-08 18:51:35 +010017#include <linux/sched/debug.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010018#include <linux/sched/task_stack.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040019#include <linux/mm.h>
20#include <linux/smp.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040021#include <linux/kernel.h>
22#include <linux/signal.h>
23#include <linux/errno.h>
24#include <linux/wait.h>
25#include <linux/unistd.h>
26#include <linux/stddef.h>
27#include <linux/personality.h>
28#include <linux/suspend.h>
29#include <linux/ptrace.h>
30#include <linux/elf.h>
31#include <linux/compat.h>
32#include <linux/syscalls.h>
33#include <linux/uaccess.h>
34#include <asm/processor.h>
35#include <asm/ucontext.h>
36#include <asm/sigframe.h>
Chris Metcalf0707ad32010-06-25 17:04:17 -040037#include <asm/syscalls.h>
Chris Metcalf4a556f42013-08-07 15:33:32 -040038#include <asm/vdso.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040039#include <arch/interrupts.h>
40
41#define DEBUG_SIG 0
42
Chris Metcalf867e3592010-05-28 23:09:12 -040043/*
44 * Do a signal return; undo the signal stack.
45 */
46
47int restore_sigcontext(struct pt_regs *regs,
Chris Metcalf81711ce2010-12-14 16:07:25 -050048 struct sigcontext __user *sc)
Chris Metcalf867e3592010-05-28 23:09:12 -040049{
Chen Gang01f7ae02014-11-02 10:13:11 +080050 int err;
Chris Metcalf867e3592010-05-28 23:09:12 -040051
52 /* Always make any pending restarted system calls return -EINTR */
Andy Lutomirskif56141e2015-02-12 15:01:14 -080053 current->restart_block.fn = do_no_restart_syscall;
Chris Metcalf867e3592010-05-28 23:09:12 -040054
Chris Metcalf74fca9d2010-09-15 11:16:08 -040055 /*
56 * Enforce that sigcontext is like pt_regs, and doesn't mess
57 * up our stack alignment rules.
58 */
59 BUILD_BUG_ON(sizeof(struct sigcontext) != sizeof(struct pt_regs));
60 BUILD_BUG_ON(sizeof(struct sigcontext) % 8 != 0);
Chen Gang01f7ae02014-11-02 10:13:11 +080061 err = __copy_from_user(regs, sc, sizeof(*regs));
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{
Chen Gang01f7ae02014-11-02 10:13:11 +0800112 return __copy_to_user(sc, regs, sizeof(*regs));
Chris Metcalf867e3592010-05-28 23:09:12 -0400113}
114
115/*
116 * Determine which stack to use..
117 */
118static inline void __user *get_sigframe(struct k_sigaction *ka,
119 struct pt_regs *regs,
120 size_t frame_size)
121{
122 unsigned long sp;
123
124 /* Default to using normal stack */
125 sp = regs->sp;
126
127 /*
128 * If we are on the alternate signal stack and would overflow
129 * it, don't. Return an always-bogus address instead so we
130 * will die with SIGSEGV.
131 */
132 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
Chris Metcalf0707ad32010-06-25 17:04:17 -0400133 return (void __user __force *)-1UL;
Chris Metcalf867e3592010-05-28 23:09:12 -0400134
135 /* This is the X/Open sanctioned signal stack switching. */
136 if (ka->sa.sa_flags & SA_ONSTACK) {
137 if (sas_ss_flags(sp) == 0)
138 sp = current->sas_ss_sp + current->sas_ss_size;
139 }
140
141 sp -= frame_size;
142 /*
143 * Align the stack pointer according to the TILE ABI,
144 * i.e. so that on function entry (sp & 15) == 0.
145 */
146 sp &= -16UL;
147 return (void __user *) sp;
148}
149
Richard Weinbergerb3707c72013-10-07 15:01:08 +0200150static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
151 struct pt_regs *regs)
Chris Metcalf867e3592010-05-28 23:09:12 -0400152{
153 unsigned long restorer;
154 struct rt_sigframe __user *frame;
Richard Weinbergerb3707c72013-10-07 15:01:08 +0200155 int err = 0, sig = ksig->sig;
Chris Metcalf867e3592010-05-28 23:09:12 -0400156
Richard Weinbergerb3707c72013-10-07 15:01:08 +0200157 frame = get_sigframe(&ksig->ka, regs, sizeof(*frame));
Chris Metcalf867e3592010-05-28 23:09:12 -0400158
159 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
Richard Weinbergerb3707c72013-10-07 15:01:08 +0200160 goto err;
Chris Metcalf867e3592010-05-28 23:09:12 -0400161
Chris Metcalf867e3592010-05-28 23:09:12 -0400162 /* Always write at least the signal number for the stack backtracer. */
Richard Weinbergerb3707c72013-10-07 15:01:08 +0200163 if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
Chris Metcalf867e3592010-05-28 23:09:12 -0400164 /* At sigreturn time, restore the callee-save registers too. */
Richard Weinbergerb3707c72013-10-07 15:01:08 +0200165 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
Chris Metcalf867e3592010-05-28 23:09:12 -0400166 regs->flags |= PT_FLAGS_RESTORE_REGS;
167 } else {
Richard Weinbergerb3707c72013-10-07 15:01:08 +0200168 err |= __put_user(ksig->info.si_signo, &frame->info.si_signo);
Chris Metcalf867e3592010-05-28 23:09:12 -0400169 }
170
171 /* Create the ucontext. */
172 err |= __clear_user(&frame->save_area, sizeof(frame->save_area));
173 err |= __put_user(0, &frame->uc.uc_flags);
Chris Metcalf0707ad32010-06-25 17:04:17 -0400174 err |= __put_user(NULL, &frame->uc.uc_link);
Al Viro47669ab2012-12-23 03:50:34 -0500175 err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
Chris Metcalf867e3592010-05-28 23:09:12 -0400176 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs);
177 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
178 if (err)
Richard Weinbergerb3707c72013-10-07 15:01:08 +0200179 goto err;
Chris Metcalf867e3592010-05-28 23:09:12 -0400180
Chris Metcalf4a556f42013-08-07 15:33:32 -0400181 restorer = VDSO_SYM(&__vdso_rt_sigreturn);
Richard Weinbergerb3707c72013-10-07 15:01:08 +0200182 if (ksig->ka.sa.sa_flags & SA_RESTORER)
183 restorer = (unsigned long) ksig->ka.sa.sa_restorer;
Chris Metcalf867e3592010-05-28 23:09:12 -0400184
185 /*
186 * Set up registers for signal handler.
187 * Registers that we don't modify keep the value they had from
188 * user-space at the time we took the signal.
Chris Metcalf74fca9d2010-09-15 11:16:08 -0400189 * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
190 * since some things rely on this (e.g. glibc's debug/segfault.c).
Chris Metcalf867e3592010-05-28 23:09:12 -0400191 */
Richard Weinbergerb3707c72013-10-07 15:01:08 +0200192 regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
Chris Metcalf867e3592010-05-28 23:09:12 -0400193 regs->ex1 = PL_ICS_EX1(USER_PL, 1); /* set crit sec in handler */
194 regs->sp = (unsigned long) frame;
195 regs->lr = restorer;
Richard Weinberger89f191b2014-07-13 17:40:49 +0200196 regs->regs[0] = (unsigned long) sig;
Chris Metcalf74fca9d2010-09-15 11:16:08 -0400197 regs->regs[1] = (unsigned long) &frame->info;
198 regs->regs[2] = (unsigned long) &frame->uc;
199 regs->flags |= PT_FLAGS_CALLER_SAVES;
Chris Metcalf867e3592010-05-28 23:09:12 -0400200 return 0;
201
Richard Weinbergerb3707c72013-10-07 15:01:08 +0200202err:
203 trace_unhandled_signal("bad sigreturn frame", regs,
204 (unsigned long)frame, SIGSEGV);
Chris Metcalf867e3592010-05-28 23:09:12 -0400205 return -EFAULT;
206}
207
208/*
209 * OK, we're invoking a handler
210 */
211
Richard Weinbergerb3707c72013-10-07 15:01:08 +0200212static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
Chris Metcalf867e3592010-05-28 23:09:12 -0400213{
Al Virob7f9a112012-05-02 09:59:21 -0400214 sigset_t *oldset = sigmask_to_save();
Chris Metcalf867e3592010-05-28 23:09:12 -0400215 int ret;
216
Chris Metcalf867e3592010-05-28 23:09:12 -0400217 /* Are we from a system call? */
218 if (regs->faultnum == INT_SWINT_1) {
219 /* If so, check system call restarting.. */
220 switch (regs->regs[0]) {
221 case -ERESTART_RESTARTBLOCK:
222 case -ERESTARTNOHAND:
223 regs->regs[0] = -EINTR;
224 break;
225
226 case -ERESTARTSYS:
Richard Weinbergerb3707c72013-10-07 15:01:08 +0200227 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
Chris Metcalf867e3592010-05-28 23:09:12 -0400228 regs->regs[0] = -EINTR;
229 break;
230 }
231 /* fallthrough */
232 case -ERESTARTNOINTR:
233 /* Reload caller-saves to restore r0..r5 and r10. */
234 regs->flags |= PT_FLAGS_CALLER_SAVES;
235 regs->regs[0] = regs->orig_r0;
236 regs->pc -= 8;
237 }
238 }
239
240 /* Set up the stack frame */
241#ifdef CONFIG_COMPAT
242 if (is_compat_task())
Richard Weinbergerb3707c72013-10-07 15:01:08 +0200243 ret = compat_setup_rt_frame(ksig, oldset, regs);
Chris Metcalf867e3592010-05-28 23:09:12 -0400244 else
245#endif
Richard Weinbergerb3707c72013-10-07 15:01:08 +0200246 ret = setup_rt_frame(ksig, oldset, regs);
247
248 signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
Chris Metcalf867e3592010-05-28 23:09:12 -0400249}
250
251/*
252 * Note that 'init' is a special process: it doesn't get signals it doesn't
253 * want to handle. Thus you cannot kill init even with a SIGKILL even by
254 * mistake.
255 */
256void do_signal(struct pt_regs *regs)
257{
Richard Weinbergerb3707c72013-10-07 15:01:08 +0200258 struct ksignal ksig;
Chris Metcalf867e3592010-05-28 23:09:12 -0400259
260 /*
261 * i386 will check if we're coming from kernel mode and bail out
262 * here. In my experience this just turns weird crashes into
263 * weird spin-hangs. But if we find a case where this seems
264 * helpful, we can reinstate the check on "!user_mode(regs)".
265 */
266
Richard Weinbergerb3707c72013-10-07 15:01:08 +0200267 if (get_signal(&ksig)) {
Chris Metcalf867e3592010-05-28 23:09:12 -0400268 /* Whee! Actually deliver the signal. */
Richard Weinbergerb3707c72013-10-07 15:01:08 +0200269 handle_signal(&ksig, regs);
Chris Metcalf34a89d22010-10-28 15:03:30 -0400270 goto done;
Chris Metcalf867e3592010-05-28 23:09:12 -0400271 }
272
273 /* Did we come from a system call? */
274 if (regs->faultnum == INT_SWINT_1) {
275 /* Restart the system call - no handlers present */
276 switch (regs->regs[0]) {
277 case -ERESTARTNOHAND:
278 case -ERESTARTSYS:
279 case -ERESTARTNOINTR:
280 regs->flags |= PT_FLAGS_CALLER_SAVES;
281 regs->regs[0] = regs->orig_r0;
282 regs->pc -= 8;
283 break;
284
285 case -ERESTART_RESTARTBLOCK:
286 regs->flags |= PT_FLAGS_CALLER_SAVES;
287 regs->regs[TREG_SYSCALL_NR] = __NR_restart_syscall;
288 regs->pc -= 8;
289 break;
290 }
291 }
292
293 /* If there's no signal to deliver, just put the saved sigmask back. */
Al Viro51a7b442012-05-21 23:33:55 -0400294 restore_saved_sigmask();
Chris Metcalf34a89d22010-10-28 15:03:30 -0400295
296done:
297 /* Avoid double syscall restart if there are nested signals. */
298 regs->faultnum = INT_SWINT_1_SIGRETURN;
Chris Metcalf867e3592010-05-28 23:09:12 -0400299}
Chris Metcalf571d76a2011-05-16 14:23:44 -0400300
301int show_unhandled_signals = 1;
302
303static int __init crashinfo(char *str)
304{
Chris Metcalf571d76a2011-05-16 14:23:44 -0400305 const char *word;
306
307 if (*str == '\0')
Daniel Walterb2dfa042014-05-26 22:59:32 +0100308 show_unhandled_signals = 2;
309 else if (*str != '=' || kstrtoint(++str, 0, &show_unhandled_signals) != 0)
Chris Metcalf571d76a2011-05-16 14:23:44 -0400310 return 0;
Daniel Walterb2dfa042014-05-26 22:59:32 +0100311
Chris Metcalf571d76a2011-05-16 14:23:44 -0400312 switch (show_unhandled_signals) {
313 case 0:
314 word = "No";
315 break;
316 case 1:
317 word = "One-line";
318 break;
319 default:
320 word = "Detailed";
321 break;
322 }
323 pr_info("%s crash reports will be generated on the console\n", word);
324 return 1;
325}
326__setup("crashinfo", crashinfo);
327
328static void dump_mem(void __user *address)
329{
330 void __user *addr;
331 enum { region_size = 256, bytes_per_line = 16 };
332 int i, j, k;
333 int found_readable_mem = 0;
334
Chris Metcalf571d76a2011-05-16 14:23:44 -0400335 if (!access_ok(VERIFY_READ, address, 1)) {
336 pr_err("Not dumping at address 0x%lx (kernel address)\n",
337 (unsigned long)address);
338 return;
339 }
340
341 addr = (void __user *)
342 (((unsigned long)address & -bytes_per_line) - region_size/2);
343 if (addr > address)
344 addr = NULL;
345 for (i = 0; i < region_size;
346 addr += bytes_per_line, i += bytes_per_line) {
347 unsigned char buf[bytes_per_line];
348 char line[100];
349 if (copy_from_user(buf, addr, bytes_per_line))
350 continue;
351 if (!found_readable_mem) {
352 pr_err("Dumping memory around address 0x%lx:\n",
353 (unsigned long)address);
354 found_readable_mem = 1;
355 }
Joe Perchesf4743672014-10-31 10:50:46 -0700356 j = sprintf(line, REGFMT ":", (unsigned long)addr);
Chris Metcalf571d76a2011-05-16 14:23:44 -0400357 for (k = 0; k < bytes_per_line; ++k)
358 j += sprintf(&line[j], " %02x", buf[k]);
359 pr_err("%s\n", line);
360 }
361 if (!found_readable_mem)
362 pr_err("No readable memory around address 0x%lx\n",
363 (unsigned long)address);
364}
365
366void trace_unhandled_signal(const char *type, struct pt_regs *regs,
367 unsigned long address, int sig)
368{
369 struct task_struct *tsk = current;
370
371 if (show_unhandled_signals == 0)
372 return;
373
374 /* If the signal is handled, don't show it here. */
375 if (!is_global_init(tsk)) {
376 void __user *handler =
377 tsk->sighand->action[sig-1].sa.sa_handler;
378 if (handler != SIG_IGN && handler != SIG_DFL)
379 return;
380 }
381
382 /* Rate-limit the one-line output, not the detailed output. */
383 if (show_unhandled_signals <= 1 && !printk_ratelimit())
384 return;
385
386 printk("%s%s[%d]: %s at %lx pc "REGFMT" signal %d",
387 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
388 tsk->comm, task_pid_nr(tsk), type, address, regs->pc, sig);
389
390 print_vma_addr(KERN_CONT " in ", regs->pc);
391
392 printk(KERN_CONT "\n");
393
394 if (show_unhandled_signals > 1) {
395 switch (sig) {
396 case SIGILL:
397 case SIGFPE:
398 case SIGSEGV:
399 case SIGBUS:
Joe Perchesf4743672014-10-31 10:50:46 -0700400 pr_err("User crash: signal %d, trap %ld, address 0x%lx\n",
Chris Metcalf571d76a2011-05-16 14:23:44 -0400401 sig, regs->faultnum, address);
402 show_regs(regs);
403 dump_mem((void __user *)address);
404 break;
405 default:
406 pr_err("User crash: signal %d, trap %ld\n",
407 sig, regs->faultnum);
408 break;
409 }
410 }
411}