blob: a87e58a9e38fe17ffe37219d56d854ddf0be48b4 [file] [log] [blame]
Paul Mundt5a4f7c62007-11-20 18:08:06 +09001#include <linux/bug.h>
2#include <linux/io.h>
3#include <linux/types.h>
4#include <linux/kdebug.h>
Paul Mundt47a3eb92007-11-26 18:17:51 +09005#include <linux/signal.h>
6#include <linux/sched.h>
Paul Mundt9a33fc22008-05-19 19:32:07 +09007#include <linux/uaccess.h>
Paul Mundt1e1030d2009-09-01 17:38:32 +09008#include <linux/hardirq.h>
Paul Mundte115f2c2009-08-22 05:28:25 +09009#include <asm/unwinder.h>
David Howellse839ca52012-03-28 18:30:03 +010010#include <asm/traps.h>
Paul Mundt5a4f7c62007-11-20 18:08:06 +090011
Paul Mundt626ac8e2009-11-12 16:39:47 +090012#ifdef CONFIG_GENERIC_BUG
13static void handle_BUG(struct pt_regs *regs)
Paul Mundt5a4f7c62007-11-20 18:08:06 +090014{
Paul Mundte115f2c2009-08-22 05:28:25 +090015 const struct bug_entry *bug;
16 unsigned long bugaddr = regs->pc;
Paul Mundt5a4f7c62007-11-20 18:08:06 +090017 enum bug_trap_type tt;
Paul Mundte115f2c2009-08-22 05:28:25 +090018
19 if (!is_valid_bugaddr(bugaddr))
20 goto invalid;
21
22 bug = find_bug(bugaddr);
23
24 /* Switch unwinders when unwind_stack() is called */
25 if (bug->flags & BUGFLAG_UNWINDER)
26 unwinder_faulted = 1;
27
28 tt = report_bug(bugaddr, regs);
Paul Mundt5a4f7c62007-11-20 18:08:06 +090029 if (tt == BUG_TRAP_TYPE_WARN) {
Paul Mundte115f2c2009-08-22 05:28:25 +090030 regs->pc += instruction_size(bugaddr);
Paul Mundt5a4f7c62007-11-20 18:08:06 +090031 return;
32 }
33
Paul Mundte115f2c2009-08-22 05:28:25 +090034invalid:
Paul Mundt5a4f7c62007-11-20 18:08:06 +090035 die("Kernel BUG", regs, TRAPA_BUG_OPCODE & 0xff);
36}
37
38int is_valid_bugaddr(unsigned long addr)
39{
Paul Mundt2bcfffa2009-05-09 16:02:08 +090040 insn_size_t opcode;
Paul Mundt9a33fc22008-05-19 19:32:07 +090041
42 if (addr < PAGE_OFFSET)
43 return 0;
Paul Mundt2bcfffa2009-05-09 16:02:08 +090044 if (probe_kernel_address((insn_size_t *)addr, opcode))
Paul Mundt9a33fc22008-05-19 19:32:07 +090045 return 0;
Paul Mundte115f2c2009-08-22 05:28:25 +090046 if (opcode == TRAPA_BUG_OPCODE)
Matt Flemingb344e24a2009-08-16 21:54:48 +010047 return 1;
48
49 return 0;
Paul Mundt5a4f7c62007-11-20 18:08:06 +090050}
51#endif
52
53/*
54 * Generic trap handler.
55 */
56BUILD_TRAP_HANDLER(debug)
57{
58 TRAP_HANDLER_DECL;
59
60 /* Rewind */
Paul Mundt9d56dd32010-01-26 12:58:40 +090061 regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
Paul Mundt5a4f7c62007-11-20 18:08:06 +090062
63 if (notify_die(DIE_TRAP, "debug trap", regs, 0, vec & 0xff,
64 SIGTRAP) == NOTIFY_STOP)
65 return;
66
67 force_sig(SIGTRAP, current);
68}
69
70/*
71 * Special handler for BUG() traps.
72 */
73BUILD_TRAP_HANDLER(bug)
74{
75 TRAP_HANDLER_DECL;
76
77 /* Rewind */
Paul Mundt9d56dd32010-01-26 12:58:40 +090078 regs->pc -= instruction_size(__raw_readw(regs->pc - 4));
Paul Mundt5a4f7c62007-11-20 18:08:06 +090079
80 if (notify_die(DIE_TRAP, "bug trap", regs, 0, TRAPA_BUG_OPCODE & 0xff,
81 SIGTRAP) == NOTIFY_STOP)
82 return;
83
Paul Mundt626ac8e2009-11-12 16:39:47 +090084#ifdef CONFIG_GENERIC_BUG
Paul Mundt5a4f7c62007-11-20 18:08:06 +090085 if (__kernel_text_address(instruction_pointer(regs))) {
Paul Mundt2bcfffa2009-05-09 16:02:08 +090086 insn_size_t insn = *(insn_size_t *)instruction_pointer(regs);
Paul Mundt5a4f7c62007-11-20 18:08:06 +090087 if (insn == TRAPA_BUG_OPCODE)
88 handle_BUG(regs);
Magnus Damm0ec39882009-06-17 04:48:20 +000089 return;
Paul Mundt5a4f7c62007-11-20 18:08:06 +090090 }
91#endif
92
93 force_sig(SIGTRAP, current);
94}
Paul Mundt1e1030d2009-09-01 17:38:32 +090095
96BUILD_TRAP_HANDLER(nmi)
97{
Paul Mundt731ba332009-10-14 16:42:28 +090098 unsigned int cpu = smp_processor_id();
Paul Mundt1e1030d2009-09-01 17:38:32 +090099 TRAP_HANDLER_DECL;
100
101 nmi_enter();
Paul Mundt731ba332009-10-14 16:42:28 +0900102 nmi_count(cpu)++;
Paul Mundt1e1030d2009-09-01 17:38:32 +0900103
104 switch (notify_die(DIE_NMI, "NMI", regs, 0, vec & 0xff, SIGINT)) {
105 case NOTIFY_OK:
106 case NOTIFY_STOP:
107 break;
108 case NOTIFY_BAD:
109 die("Fatal Non-Maskable Interrupt", regs, SIGINT);
110 default:
111 printk(KERN_ALERT "Got NMI, but nobody cared. Ignoring...\n");
112 break;
113 }
114
115 nmi_exit();
116}