Commit | Line | Data |
---|---|---|
867e359b CM |
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/mm.h> | |
18 | #include <linux/smp.h> | |
867e359b CM |
19 | #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> | |
0707ad30 | 35 | #include <asm/syscalls.h> |
4a556f4f | 36 | #include <asm/vdso.h> |
867e359b CM |
37 | #include <arch/interrupts.h> |
38 | ||
39 | #define DEBUG_SIG 0 | |
40 | ||
867e359b CM |
41 | /* |
42 | * Do a signal return; undo the signal stack. | |
43 | */ | |
44 | ||
45 | int restore_sigcontext(struct pt_regs *regs, | |
81711cee | 46 | struct sigcontext __user *sc) |
867e359b CM |
47 | { |
48 | int err = 0; | |
49 | int i; | |
50 | ||
51 | /* Always make any pending restarted system calls return -EINTR */ | |
52 | current_thread_info()->restart_block.fn = do_no_restart_syscall; | |
53 | ||
74fca9da CM |
54 | /* |
55 | * Enforce that sigcontext is like pt_regs, and doesn't mess | |
56 | * up our stack alignment rules. | |
57 | */ | |
58 | BUILD_BUG_ON(sizeof(struct sigcontext) != sizeof(struct pt_regs)); | |
59 | BUILD_BUG_ON(sizeof(struct sigcontext) % 8 != 0); | |
60 | ||
867e359b | 61 | for (i = 0; i < sizeof(struct pt_regs)/sizeof(long); ++i) |
74fca9da | 62 | err |= __get_user(regs->regs[i], &sc->gregs[i]); |
867e359b | 63 | |
1deb9c5d CM |
64 | /* Ensure that the PL is always set to USER_PL. */ |
65 | regs->ex1 = PL_ICS_EX1(USER_PL, EX1_ICS(regs->ex1)); | |
66 | ||
867e359b CM |
67 | regs->faultnum = INT_SWINT_1_SIGRETURN; |
68 | ||
867e359b CM |
69 | return err; |
70 | } | |
71 | ||
571d76ac CM |
72 | void signal_fault(const char *type, struct pt_regs *regs, |
73 | void __user *frame, int sig) | |
74 | { | |
75 | trace_unhandled_signal(type, regs, (unsigned long)frame, SIGSEGV); | |
76 | force_sigsegv(sig, current); | |
77 | } | |
78 | ||
81711cee | 79 | /* The assembly shim for this function arranges to ignore the return value. */ |
6b14e419 | 80 | SYSCALL_DEFINE0(rt_sigreturn) |
867e359b | 81 | { |
6b14e419 | 82 | struct pt_regs *regs = current_pt_regs(); |
867e359b CM |
83 | struct rt_sigframe __user *frame = |
84 | (struct rt_sigframe __user *)(regs->sp); | |
85 | sigset_t set; | |
867e359b CM |
86 | |
87 | if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) | |
88 | goto badframe; | |
89 | if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set))) | |
90 | goto badframe; | |
91 | ||
ad092338 | 92 | set_current_blocked(&set); |
867e359b | 93 | |
81711cee | 94 | if (restore_sigcontext(regs, &frame->uc.uc_mcontext)) |
867e359b CM |
95 | goto badframe; |
96 | ||
47669ab0 | 97 | if (restore_altstack(&frame->uc.uc_stack)) |
867e359b CM |
98 | goto badframe; |
99 | ||
81711cee | 100 | return 0; |
867e359b CM |
101 | |
102 | badframe: | |
571d76ac | 103 | signal_fault("bad sigreturn frame", regs, frame, 0); |
867e359b CM |
104 | return 0; |
105 | } | |
106 | ||
107 | /* | |
108 | * Set up a signal frame. | |
109 | */ | |
110 | ||
111 | int setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs) | |
112 | { | |
113 | int i, err = 0; | |
114 | ||
115 | for (i = 0; i < sizeof(struct pt_regs)/sizeof(long); ++i) | |
74fca9da | 116 | err |= __put_user(regs->regs[i], &sc->gregs[i]); |
867e359b CM |
117 | |
118 | return err; | |
119 | } | |
120 | ||
121 | /* | |
122 | * Determine which stack to use.. | |
123 | */ | |
124 | static inline void __user *get_sigframe(struct k_sigaction *ka, | |
125 | struct pt_regs *regs, | |
126 | size_t frame_size) | |
127 | { | |
128 | unsigned long sp; | |
129 | ||
130 | /* Default to using normal stack */ | |
131 | sp = regs->sp; | |
132 | ||
133 | /* | |
134 | * If we are on the alternate signal stack and would overflow | |
135 | * it, don't. Return an always-bogus address instead so we | |
136 | * will die with SIGSEGV. | |
137 | */ | |
138 | if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size))) | |
0707ad30 | 139 | return (void __user __force *)-1UL; |
867e359b CM |
140 | |
141 | /* This is the X/Open sanctioned signal stack switching. */ | |
142 | if (ka->sa.sa_flags & SA_ONSTACK) { | |
143 | if (sas_ss_flags(sp) == 0) | |
144 | sp = current->sas_ss_sp + current->sas_ss_size; | |
145 | } | |
146 | ||
147 | sp -= frame_size; | |
148 | /* | |
149 | * Align the stack pointer according to the TILE ABI, | |
150 | * i.e. so that on function entry (sp & 15) == 0. | |
151 | */ | |
152 | sp &= -16UL; | |
153 | return (void __user *) sp; | |
154 | } | |
155 | ||
b3707c7e RW |
156 | static int setup_rt_frame(struct ksignal *ksig, sigset_t *set, |
157 | struct pt_regs *regs) | |
867e359b CM |
158 | { |
159 | unsigned long restorer; | |
160 | struct rt_sigframe __user *frame; | |
b3707c7e | 161 | int err = 0, sig = ksig->sig; |
867e359b CM |
162 | int usig; |
163 | ||
b3707c7e | 164 | frame = get_sigframe(&ksig->ka, regs, sizeof(*frame)); |
867e359b CM |
165 | |
166 | if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) | |
b3707c7e | 167 | goto err; |
867e359b CM |
168 | |
169 | usig = current_thread_info()->exec_domain | |
170 | && current_thread_info()->exec_domain->signal_invmap | |
171 | && sig < 32 | |
172 | ? current_thread_info()->exec_domain->signal_invmap[sig] | |
173 | : sig; | |
174 | ||
175 | /* Always write at least the signal number for the stack backtracer. */ | |
b3707c7e | 176 | if (ksig->ka.sa.sa_flags & SA_SIGINFO) { |
867e359b | 177 | /* At sigreturn time, restore the callee-save registers too. */ |
b3707c7e | 178 | err |= copy_siginfo_to_user(&frame->info, &ksig->info); |
867e359b CM |
179 | regs->flags |= PT_FLAGS_RESTORE_REGS; |
180 | } else { | |
b3707c7e | 181 | err |= __put_user(ksig->info.si_signo, &frame->info.si_signo); |
867e359b CM |
182 | } |
183 | ||
184 | /* Create the ucontext. */ | |
185 | err |= __clear_user(&frame->save_area, sizeof(frame->save_area)); | |
186 | err |= __put_user(0, &frame->uc.uc_flags); | |
0707ad30 | 187 | err |= __put_user(NULL, &frame->uc.uc_link); |
47669ab0 | 188 | err |= __save_altstack(&frame->uc.uc_stack, regs->sp); |
867e359b CM |
189 | err |= setup_sigcontext(&frame->uc.uc_mcontext, regs); |
190 | err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)); | |
191 | if (err) | |
b3707c7e | 192 | goto err; |
867e359b | 193 | |
4a556f4f | 194 | restorer = VDSO_SYM(&__vdso_rt_sigreturn); |
b3707c7e RW |
195 | if (ksig->ka.sa.sa_flags & SA_RESTORER) |
196 | restorer = (unsigned long) ksig->ka.sa.sa_restorer; | |
867e359b CM |
197 | |
198 | /* | |
199 | * Set up registers for signal handler. | |
200 | * Registers that we don't modify keep the value they had from | |
201 | * user-space at the time we took the signal. | |
74fca9da CM |
202 | * We always pass siginfo and mcontext, regardless of SA_SIGINFO, |
203 | * since some things rely on this (e.g. glibc's debug/segfault.c). | |
867e359b | 204 | */ |
b3707c7e | 205 | regs->pc = (unsigned long) ksig->ka.sa.sa_handler; |
867e359b CM |
206 | regs->ex1 = PL_ICS_EX1(USER_PL, 1); /* set crit sec in handler */ |
207 | regs->sp = (unsigned long) frame; | |
208 | regs->lr = restorer; | |
209 | regs->regs[0] = (unsigned long) usig; | |
74fca9da CM |
210 | regs->regs[1] = (unsigned long) &frame->info; |
211 | regs->regs[2] = (unsigned long) &frame->uc; | |
212 | regs->flags |= PT_FLAGS_CALLER_SAVES; | |
867e359b CM |
213 | return 0; |
214 | ||
b3707c7e RW |
215 | err: |
216 | trace_unhandled_signal("bad sigreturn frame", regs, | |
217 | (unsigned long)frame, SIGSEGV); | |
867e359b CM |
218 | return -EFAULT; |
219 | } | |
220 | ||
221 | /* | |
222 | * OK, we're invoking a handler | |
223 | */ | |
224 | ||
b3707c7e | 225 | static void handle_signal(struct ksignal *ksig, struct pt_regs *regs) |
867e359b | 226 | { |
b7f9a11a | 227 | sigset_t *oldset = sigmask_to_save(); |
867e359b CM |
228 | int ret; |
229 | ||
867e359b CM |
230 | /* 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: | |
b3707c7e | 240 | if (!(ksig->ka.sa.sa_flags & SA_RESTART)) { |
867e359b CM |
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()) | |
b3707c7e | 256 | ret = compat_setup_rt_frame(ksig, oldset, regs); |
867e359b CM |
257 | else |
258 | #endif | |
b3707c7e RW |
259 | ret = setup_rt_frame(ksig, oldset, regs); |
260 | ||
261 | signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP)); | |
867e359b CM |
262 | } |
263 | ||
264 | /* | |
265 | * Note that 'init' is a special process: it doesn't get signals it doesn't | |
266 | * want to handle. Thus you cannot kill init even with a SIGKILL even by | |
267 | * mistake. | |
268 | */ | |
269 | void do_signal(struct pt_regs *regs) | |
270 | { | |
b3707c7e | 271 | struct ksignal ksig; |
867e359b CM |
272 | |
273 | /* | |
274 | * i386 will check if we're coming from kernel mode and bail out | |
275 | * here. In my experience this just turns weird crashes into | |
276 | * weird spin-hangs. But if we find a case where this seems | |
277 | * helpful, we can reinstate the check on "!user_mode(regs)". | |
278 | */ | |
279 | ||
b3707c7e | 280 | if (get_signal(&ksig)) { |
867e359b | 281 | /* Whee! Actually deliver the signal. */ |
b3707c7e | 282 | handle_signal(&ksig, regs); |
34a89d26 | 283 | goto done; |
867e359b CM |
284 | } |
285 | ||
286 | /* Did we come from a system call? */ | |
287 | if (regs->faultnum == INT_SWINT_1) { | |
288 | /* Restart the system call - no handlers present */ | |
289 | switch (regs->regs[0]) { | |
290 | case -ERESTARTNOHAND: | |
291 | case -ERESTARTSYS: | |
292 | case -ERESTARTNOINTR: | |
293 | regs->flags |= PT_FLAGS_CALLER_SAVES; | |
294 | regs->regs[0] = regs->orig_r0; | |
295 | regs->pc -= 8; | |
296 | break; | |
297 | ||
298 | case -ERESTART_RESTARTBLOCK: | |
299 | regs->flags |= PT_FLAGS_CALLER_SAVES; | |
300 | regs->regs[TREG_SYSCALL_NR] = __NR_restart_syscall; | |
301 | regs->pc -= 8; | |
302 | break; | |
303 | } | |
304 | } | |
305 | ||
306 | /* If there's no signal to deliver, just put the saved sigmask back. */ | |
51a7b448 | 307 | restore_saved_sigmask(); |
34a89d26 CM |
308 | |
309 | done: | |
310 | /* Avoid double syscall restart if there are nested signals. */ | |
311 | regs->faultnum = INT_SWINT_1_SIGRETURN; | |
867e359b | 312 | } |
571d76ac CM |
313 | |
314 | int show_unhandled_signals = 1; | |
315 | ||
316 | static int __init crashinfo(char *str) | |
317 | { | |
571d76ac CM |
318 | const char *word; |
319 | ||
320 | if (*str == '\0') | |
b2dfa048 DW |
321 | show_unhandled_signals = 2; |
322 | else if (*str != '=' || kstrtoint(++str, 0, &show_unhandled_signals) != 0) | |
571d76ac | 323 | return 0; |
b2dfa048 | 324 | |
571d76ac CM |
325 | switch (show_unhandled_signals) { |
326 | case 0: | |
327 | word = "No"; | |
328 | break; | |
329 | case 1: | |
330 | word = "One-line"; | |
331 | break; | |
332 | default: | |
333 | word = "Detailed"; | |
334 | break; | |
335 | } | |
336 | pr_info("%s crash reports will be generated on the console\n", word); | |
337 | return 1; | |
338 | } | |
339 | __setup("crashinfo", crashinfo); | |
340 | ||
341 | static void dump_mem(void __user *address) | |
342 | { | |
343 | void __user *addr; | |
344 | enum { region_size = 256, bytes_per_line = 16 }; | |
345 | int i, j, k; | |
346 | int found_readable_mem = 0; | |
347 | ||
348 | pr_err("\n"); | |
349 | if (!access_ok(VERIFY_READ, address, 1)) { | |
350 | pr_err("Not dumping at address 0x%lx (kernel address)\n", | |
351 | (unsigned long)address); | |
352 | return; | |
353 | } | |
354 | ||
355 | addr = (void __user *) | |
356 | (((unsigned long)address & -bytes_per_line) - region_size/2); | |
357 | if (addr > address) | |
358 | addr = NULL; | |
359 | for (i = 0; i < region_size; | |
360 | addr += bytes_per_line, i += bytes_per_line) { | |
361 | unsigned char buf[bytes_per_line]; | |
362 | char line[100]; | |
363 | if (copy_from_user(buf, addr, bytes_per_line)) | |
364 | continue; | |
365 | if (!found_readable_mem) { | |
366 | pr_err("Dumping memory around address 0x%lx:\n", | |
367 | (unsigned long)address); | |
368 | found_readable_mem = 1; | |
369 | } | |
370 | j = sprintf(line, REGFMT":", (unsigned long)addr); | |
371 | for (k = 0; k < bytes_per_line; ++k) | |
372 | j += sprintf(&line[j], " %02x", buf[k]); | |
373 | pr_err("%s\n", line); | |
374 | } | |
375 | if (!found_readable_mem) | |
376 | pr_err("No readable memory around address 0x%lx\n", | |
377 | (unsigned long)address); | |
378 | } | |
379 | ||
380 | void trace_unhandled_signal(const char *type, struct pt_regs *regs, | |
381 | unsigned long address, int sig) | |
382 | { | |
383 | struct task_struct *tsk = current; | |
384 | ||
385 | if (show_unhandled_signals == 0) | |
386 | return; | |
387 | ||
388 | /* If the signal is handled, don't show it here. */ | |
389 | if (!is_global_init(tsk)) { | |
390 | void __user *handler = | |
391 | tsk->sighand->action[sig-1].sa.sa_handler; | |
392 | if (handler != SIG_IGN && handler != SIG_DFL) | |
393 | return; | |
394 | } | |
395 | ||
396 | /* Rate-limit the one-line output, not the detailed output. */ | |
397 | if (show_unhandled_signals <= 1 && !printk_ratelimit()) | |
398 | return; | |
399 | ||
400 | printk("%s%s[%d]: %s at %lx pc "REGFMT" signal %d", | |
401 | task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG, | |
402 | tsk->comm, task_pid_nr(tsk), type, address, regs->pc, sig); | |
403 | ||
404 | print_vma_addr(KERN_CONT " in ", regs->pc); | |
405 | ||
406 | printk(KERN_CONT "\n"); | |
407 | ||
408 | if (show_unhandled_signals > 1) { | |
409 | switch (sig) { | |
410 | case SIGILL: | |
411 | case SIGFPE: | |
412 | case SIGSEGV: | |
413 | case SIGBUS: | |
414 | pr_err("User crash: signal %d," | |
415 | " trap %ld, address 0x%lx\n", | |
416 | sig, regs->faultnum, address); | |
417 | show_regs(regs); | |
418 | dump_mem((void __user *)address); | |
419 | break; | |
420 | default: | |
421 | pr_err("User crash: signal %d, trap %ld\n", | |
422 | sig, regs->faultnum); | |
423 | break; | |
424 | } | |
425 | } | |
426 | } |