| Paul Mundt | 5a4f7c6 | 2007-11-20 18:08:06 +0900 | [diff] [blame] | 1 | #include <linux/bug.h> | 
|  | 2 | #include <linux/io.h> | 
|  | 3 | #include <linux/types.h> | 
|  | 4 | #include <linux/kdebug.h> | 
| Paul Mundt | 47a3eb9 | 2007-11-26 18:17:51 +0900 | [diff] [blame] | 5 | #include <linux/signal.h> | 
|  | 6 | #include <linux/sched.h> | 
| Paul Mundt | 9a33fc2 | 2008-05-19 19:32:07 +0900 | [diff] [blame] | 7 | #include <linux/uaccess.h> | 
| Paul Mundt | 1e1030d | 2009-09-01 17:38:32 +0900 | [diff] [blame] | 8 | #include <linux/hardirq.h> | 
| Paul Mundt | e115f2c | 2009-08-22 05:28:25 +0900 | [diff] [blame] | 9 | #include <asm/unwinder.h> | 
| Paul Mundt | 5a4f7c6 | 2007-11-20 18:08:06 +0900 | [diff] [blame] | 10 | #include <asm/system.h> | 
|  | 11 |  | 
|  | 12 | #ifdef CONFIG_BUG | 
| Matt Fleming | b344e24 | 2009-08-16 21:54:48 +0100 | [diff] [blame] | 13 | void handle_BUG(struct pt_regs *regs) | 
| Paul Mundt | 5a4f7c6 | 2007-11-20 18:08:06 +0900 | [diff] [blame] | 14 | { | 
| Paul Mundt | e115f2c | 2009-08-22 05:28:25 +0900 | [diff] [blame] | 15 | const struct bug_entry *bug; | 
|  | 16 | unsigned long bugaddr = regs->pc; | 
| Paul Mundt | 5a4f7c6 | 2007-11-20 18:08:06 +0900 | [diff] [blame] | 17 | enum bug_trap_type tt; | 
| Paul Mundt | e115f2c | 2009-08-22 05:28:25 +0900 | [diff] [blame] | 18 |  | 
|  | 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 Mundt | 5a4f7c6 | 2007-11-20 18:08:06 +0900 | [diff] [blame] | 29 | if (tt == BUG_TRAP_TYPE_WARN) { | 
| Paul Mundt | e115f2c | 2009-08-22 05:28:25 +0900 | [diff] [blame] | 30 | regs->pc += instruction_size(bugaddr); | 
| Paul Mundt | 5a4f7c6 | 2007-11-20 18:08:06 +0900 | [diff] [blame] | 31 | return; | 
|  | 32 | } | 
|  | 33 |  | 
| Paul Mundt | e115f2c | 2009-08-22 05:28:25 +0900 | [diff] [blame] | 34 | invalid: | 
| Paul Mundt | 5a4f7c6 | 2007-11-20 18:08:06 +0900 | [diff] [blame] | 35 | die("Kernel BUG", regs, TRAPA_BUG_OPCODE & 0xff); | 
|  | 36 | } | 
|  | 37 |  | 
|  | 38 | int is_valid_bugaddr(unsigned long addr) | 
|  | 39 | { | 
| Paul Mundt | 2bcfffa | 2009-05-09 16:02:08 +0900 | [diff] [blame] | 40 | insn_size_t opcode; | 
| Paul Mundt | 9a33fc2 | 2008-05-19 19:32:07 +0900 | [diff] [blame] | 41 |  | 
|  | 42 | if (addr < PAGE_OFFSET) | 
|  | 43 | return 0; | 
| Paul Mundt | 2bcfffa | 2009-05-09 16:02:08 +0900 | [diff] [blame] | 44 | if (probe_kernel_address((insn_size_t *)addr, opcode)) | 
| Paul Mundt | 9a33fc2 | 2008-05-19 19:32:07 +0900 | [diff] [blame] | 45 | return 0; | 
| Paul Mundt | e115f2c | 2009-08-22 05:28:25 +0900 | [diff] [blame] | 46 | if (opcode == TRAPA_BUG_OPCODE) | 
| Matt Fleming | b344e24 | 2009-08-16 21:54:48 +0100 | [diff] [blame] | 47 | return 1; | 
|  | 48 |  | 
|  | 49 | return 0; | 
| Paul Mundt | 5a4f7c6 | 2007-11-20 18:08:06 +0900 | [diff] [blame] | 50 | } | 
|  | 51 | #endif | 
|  | 52 |  | 
|  | 53 | /* | 
|  | 54 | * Generic trap handler. | 
|  | 55 | */ | 
|  | 56 | BUILD_TRAP_HANDLER(debug) | 
|  | 57 | { | 
|  | 58 | TRAP_HANDLER_DECL; | 
|  | 59 |  | 
|  | 60 | /* Rewind */ | 
|  | 61 | regs->pc -= instruction_size(ctrl_inw(regs->pc - 4)); | 
|  | 62 |  | 
|  | 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 | */ | 
|  | 73 | BUILD_TRAP_HANDLER(bug) | 
|  | 74 | { | 
|  | 75 | TRAP_HANDLER_DECL; | 
|  | 76 |  | 
|  | 77 | /* Rewind */ | 
|  | 78 | regs->pc -= instruction_size(ctrl_inw(regs->pc - 4)); | 
|  | 79 |  | 
|  | 80 | if (notify_die(DIE_TRAP, "bug trap", regs, 0, TRAPA_BUG_OPCODE & 0xff, | 
|  | 81 | SIGTRAP) == NOTIFY_STOP) | 
|  | 82 | return; | 
|  | 83 |  | 
|  | 84 | #ifdef CONFIG_BUG | 
|  | 85 | if (__kernel_text_address(instruction_pointer(regs))) { | 
| Paul Mundt | 2bcfffa | 2009-05-09 16:02:08 +0900 | [diff] [blame] | 86 | insn_size_t insn = *(insn_size_t *)instruction_pointer(regs); | 
| Paul Mundt | 5a4f7c6 | 2007-11-20 18:08:06 +0900 | [diff] [blame] | 87 | if (insn == TRAPA_BUG_OPCODE) | 
|  | 88 | handle_BUG(regs); | 
| Magnus Damm | 0ec3988 | 2009-06-17 04:48:20 +0000 | [diff] [blame] | 89 | return; | 
| Paul Mundt | 5a4f7c6 | 2007-11-20 18:08:06 +0900 | [diff] [blame] | 90 | } | 
|  | 91 | #endif | 
|  | 92 |  | 
|  | 93 | force_sig(SIGTRAP, current); | 
|  | 94 | } | 
| Paul Mundt | 1e1030d | 2009-09-01 17:38:32 +0900 | [diff] [blame] | 95 |  | 
|  | 96 | BUILD_TRAP_HANDLER(nmi) | 
|  | 97 | { | 
|  | 98 | TRAP_HANDLER_DECL; | 
|  | 99 |  | 
|  | 100 | nmi_enter(); | 
|  | 101 |  | 
|  | 102 | switch (notify_die(DIE_NMI, "NMI", regs, 0, vec & 0xff, SIGINT)) { | 
|  | 103 | case NOTIFY_OK: | 
|  | 104 | case NOTIFY_STOP: | 
|  | 105 | break; | 
|  | 106 | case NOTIFY_BAD: | 
|  | 107 | die("Fatal Non-Maskable Interrupt", regs, SIGINT); | 
|  | 108 | default: | 
|  | 109 | printk(KERN_ALERT "Got NMI, but nobody cared. Ignoring...\n"); | 
|  | 110 | break; | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 | nmi_exit(); | 
|  | 114 | } |