• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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/sched/debug.h>
18 #include <linux/sched/task_stack.h>
19 #include <linux/mm.h>
20 #include <linux/smp.h>
21 #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>
37 #include <asm/syscalls.h>
38 #include <asm/vdso.h>
39 #include <arch/interrupts.h>
40 
41 #define DEBUG_SIG 0
42 
43 /*
44  * Do a signal return; undo the signal stack.
45  */
46 
restore_sigcontext(struct pt_regs * regs,struct sigcontext __user * sc)47 int restore_sigcontext(struct pt_regs *regs,
48 		       struct sigcontext __user *sc)
49 {
50 	int err;
51 
52 	/* Always make any pending restarted system calls return -EINTR */
53 	current->restart_block.fn = do_no_restart_syscall;
54 
55 	/*
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);
61 	err = __copy_from_user(regs, sc, sizeof(*regs));
62 
63 	/* Ensure that the PL is always set to USER_PL. */
64 	regs->ex1 = PL_ICS_EX1(USER_PL, EX1_ICS(regs->ex1));
65 
66 	regs->faultnum = INT_SWINT_1_SIGRETURN;
67 
68 	return err;
69 }
70 
signal_fault(const char * type,struct pt_regs * regs,void __user * frame,int sig)71 void 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 
78 /* The assembly shim for this function arranges to ignore the return value. */
SYSCALL_DEFINE0(rt_sigreturn)79 SYSCALL_DEFINE0(rt_sigreturn)
80 {
81 	struct pt_regs *regs = current_pt_regs();
82 	struct rt_sigframe __user *frame =
83 		(struct rt_sigframe __user *)(regs->sp);
84 	sigset_t set;
85 
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 
91 	set_current_blocked(&set);
92 
93 	if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
94 		goto badframe;
95 
96 	if (restore_altstack(&frame->uc.uc_stack))
97 		goto badframe;
98 
99 	return 0;
100 
101 badframe:
102 	signal_fault("bad sigreturn frame", regs, frame, 0);
103 	return 0;
104 }
105 
106 /*
107  * Set up a signal frame.
108  */
109 
setup_sigcontext(struct sigcontext __user * sc,struct pt_regs * regs)110 int setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
111 {
112 	return  __copy_to_user(sc, regs, sizeof(*regs));
113 }
114 
115 /*
116  * Determine which stack to use..
117  */
get_sigframe(struct k_sigaction * ka,struct pt_regs * regs,size_t frame_size)118 static 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)))
133 		return (void __user __force *)-1UL;
134 
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 
setup_rt_frame(struct ksignal * ksig,sigset_t * set,struct pt_regs * regs)150 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
151 			  struct pt_regs *regs)
152 {
153 	unsigned long restorer;
154 	struct rt_sigframe __user *frame;
155 	int err = 0, sig = ksig->sig;
156 
157 	frame = get_sigframe(&ksig->ka, regs, sizeof(*frame));
158 
159 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
160 		goto err;
161 
162 	/* Always write at least the signal number for the stack backtracer. */
163 	if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
164 		/* At sigreturn time, restore the callee-save registers too. */
165 		err |= copy_siginfo_to_user(&frame->info, &ksig->info);
166 		regs->flags |= PT_FLAGS_RESTORE_REGS;
167 	} else {
168 		err |= __put_user(ksig->info.si_signo, &frame->info.si_signo);
169 	}
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);
174 	err |= __put_user(NULL, &frame->uc.uc_link);
175 	err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
176 	err |= setup_sigcontext(&frame->uc.uc_mcontext, regs);
177 	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
178 	if (err)
179 		goto err;
180 
181 	restorer = VDSO_SYM(&__vdso_rt_sigreturn);
182 	if (ksig->ka.sa.sa_flags & SA_RESTORER)
183 		restorer = (unsigned long) ksig->ka.sa.sa_restorer;
184 
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.
189 	 * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
190 	 * since some things rely on this (e.g. glibc's debug/segfault.c).
191 	 */
192 	regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
193 	regs->ex1 = PL_ICS_EX1(USER_PL, 1); /* set crit sec in handler */
194 	regs->sp = (unsigned long) frame;
195 	regs->lr = restorer;
196 	regs->regs[0] = (unsigned long) sig;
197 	regs->regs[1] = (unsigned long) &frame->info;
198 	regs->regs[2] = (unsigned long) &frame->uc;
199 	regs->flags |= PT_FLAGS_CALLER_SAVES;
200 	return 0;
201 
202 err:
203 	trace_unhandled_signal("bad sigreturn frame", regs,
204 			      (unsigned long)frame, SIGSEGV);
205 	return -EFAULT;
206 }
207 
208 /*
209  * OK, we're invoking a handler
210  */
211 
handle_signal(struct ksignal * ksig,struct pt_regs * regs)212 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
213 {
214 	sigset_t *oldset = sigmask_to_save();
215 	int ret;
216 
217 	/* 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:
227 			if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
228 				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())
243 		ret = compat_setup_rt_frame(ksig, oldset, regs);
244 	else
245 #endif
246 		ret = setup_rt_frame(ksig, oldset, regs);
247 
248 	signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
249 }
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  */
do_signal(struct pt_regs * regs)256 void do_signal(struct pt_regs *regs)
257 {
258 	struct ksignal ksig;
259 
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 
267 	if (get_signal(&ksig)) {
268 		/* Whee! Actually deliver the signal.  */
269 		handle_signal(&ksig, regs);
270 		goto done;
271 	}
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. */
294 	restore_saved_sigmask();
295 
296 done:
297 	/* Avoid double syscall restart if there are nested signals. */
298 	regs->faultnum = INT_SWINT_1_SIGRETURN;
299 }
300 
301 int show_unhandled_signals = 1;
302 
crashinfo(char * str)303 static int __init crashinfo(char *str)
304 {
305 	const char *word;
306 
307 	if (*str == '\0')
308 		show_unhandled_signals = 2;
309 	else if (*str != '=' || kstrtoint(++str, 0, &show_unhandled_signals) != 0)
310 		return 0;
311 
312 	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 
dump_mem(void __user * address)328 static 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 
335 	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 		}
356 		j = sprintf(line, REGFMT ":", (unsigned long)addr);
357 		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 
trace_unhandled_signal(const char * type,struct pt_regs * regs,unsigned long address,int sig)366 void 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:
400 			pr_err("User crash: signal %d, trap %ld, address 0x%lx\n",
401 			       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 }
412