Alexander van Heukelum | 2bc5f92 | 2008-09-30 13:12:14 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 3 | * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs |
| 4 | */ |
| 5 | #include <linux/kallsyms.h> |
| 6 | #include <linux/kprobes.h> |
| 7 | #include <linux/uaccess.h> |
Alexander van Heukelum | 2bc5f92 | 2008-09-30 13:12:14 +0200 | [diff] [blame] | 8 | #include <linux/hardirq.h> |
| 9 | #include <linux/kdebug.h> |
Paul Gortmaker | 186f436 | 2016-07-13 20:18:56 -0400 | [diff] [blame] | 10 | #include <linux/export.h> |
Alexander van Heukelum | 2bc5f92 | 2008-09-30 13:12:14 +0200 | [diff] [blame] | 11 | #include <linux/ptrace.h> |
| 12 | #include <linux/kexec.h> |
Ingo Molnar | b803090 | 2009-11-26 08:17:31 +0100 | [diff] [blame] | 13 | #include <linux/sysfs.h> |
Alexander van Heukelum | 2bc5f92 | 2008-09-30 13:12:14 +0200 | [diff] [blame] | 14 | #include <linux/bug.h> |
| 15 | #include <linux/nmi.h> |
| 16 | |
| 17 | #include <asm/stacktrace.h> |
| 18 | |
Josh Poimboeuf | cb76c93 | 2016-09-14 21:07:42 -0500 | [diff] [blame] | 19 | void stack_type_str(enum stack_type type, const char **begin, const char **end) |
Steven Rostedt | 198d208 | 2014-02-06 09:41:31 -0500 | [diff] [blame] | 20 | { |
Josh Poimboeuf | cb76c93 | 2016-09-14 21:07:42 -0500 | [diff] [blame] | 21 | switch (type) { |
| 22 | case STACK_TYPE_IRQ: |
| 23 | case STACK_TYPE_SOFTIRQ: |
| 24 | *begin = "IRQ"; |
| 25 | *end = "EOI"; |
| 26 | break; |
| 27 | default: |
| 28 | *begin = NULL; |
| 29 | *end = NULL; |
| 30 | } |
Steven Rostedt | 198d208 | 2014-02-06 09:41:31 -0500 | [diff] [blame] | 31 | } |
| 32 | |
Josh Poimboeuf | cb76c93 | 2016-09-14 21:07:42 -0500 | [diff] [blame] | 33 | static bool in_hardirq_stack(unsigned long *stack, struct stack_info *info) |
Steven Rostedt | 198d208 | 2014-02-06 09:41:31 -0500 | [diff] [blame] | 34 | { |
Josh Poimboeuf | cb76c93 | 2016-09-14 21:07:42 -0500 | [diff] [blame] | 35 | unsigned long *begin = (unsigned long *)this_cpu_read(hardirq_stack); |
| 36 | unsigned long *end = begin + (THREAD_SIZE / sizeof(long)); |
Steven Rostedt | 198d208 | 2014-02-06 09:41:31 -0500 | [diff] [blame] | 37 | |
Josh Poimboeuf | 5fe599e | 2016-09-14 21:07:43 -0500 | [diff] [blame] | 38 | /* |
| 39 | * This is a software stack, so 'end' can be a valid stack pointer. |
| 40 | * It just means the stack is empty. |
| 41 | */ |
| 42 | if (stack < begin || stack > end) |
Josh Poimboeuf | cb76c93 | 2016-09-14 21:07:42 -0500 | [diff] [blame] | 43 | return false; |
| 44 | |
| 45 | info->type = STACK_TYPE_IRQ; |
| 46 | info->begin = begin; |
| 47 | info->end = end; |
| 48 | |
| 49 | /* |
| 50 | * See irq_32.c -- the next stack pointer is stored at the beginning of |
| 51 | * the stack. |
| 52 | */ |
| 53 | info->next_sp = (unsigned long *)*begin; |
| 54 | |
| 55 | return true; |
Steven Rostedt | 198d208 | 2014-02-06 09:41:31 -0500 | [diff] [blame] | 56 | } |
| 57 | |
Josh Poimboeuf | cb76c93 | 2016-09-14 21:07:42 -0500 | [diff] [blame] | 58 | static bool in_softirq_stack(unsigned long *stack, struct stack_info *info) |
Steven Rostedt | 198d208 | 2014-02-06 09:41:31 -0500 | [diff] [blame] | 59 | { |
Josh Poimboeuf | cb76c93 | 2016-09-14 21:07:42 -0500 | [diff] [blame] | 60 | unsigned long *begin = (unsigned long *)this_cpu_read(softirq_stack); |
| 61 | unsigned long *end = begin + (THREAD_SIZE / sizeof(long)); |
Steven Rostedt | 198d208 | 2014-02-06 09:41:31 -0500 | [diff] [blame] | 62 | |
Josh Poimboeuf | 5fe599e | 2016-09-14 21:07:43 -0500 | [diff] [blame] | 63 | /* |
| 64 | * This is a software stack, so 'end' can be a valid stack pointer. |
| 65 | * It just means the stack is empty. |
| 66 | */ |
| 67 | if (stack < begin || stack > end) |
Josh Poimboeuf | cb76c93 | 2016-09-14 21:07:42 -0500 | [diff] [blame] | 68 | return false; |
| 69 | |
| 70 | info->type = STACK_TYPE_SOFTIRQ; |
| 71 | info->begin = begin; |
| 72 | info->end = end; |
| 73 | |
| 74 | /* |
| 75 | * The next stack pointer is stored at the beginning of the stack. |
| 76 | * See irq_32.c. |
| 77 | */ |
| 78 | info->next_sp = (unsigned long *)*begin; |
| 79 | |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | int get_stack_info(unsigned long *stack, struct task_struct *task, |
| 84 | struct stack_info *info, unsigned long *visit_mask) |
| 85 | { |
| 86 | if (!stack) |
| 87 | goto unknown; |
| 88 | |
| 89 | task = task ? : current; |
| 90 | |
| 91 | if (in_task_stack(stack, task, info)) |
Josh Poimboeuf | fcd709e | 2016-09-14 21:07:44 -0500 | [diff] [blame] | 92 | goto recursion_check; |
Josh Poimboeuf | cb76c93 | 2016-09-14 21:07:42 -0500 | [diff] [blame] | 93 | |
| 94 | if (task != current) |
| 95 | goto unknown; |
| 96 | |
| 97 | if (in_hardirq_stack(stack, info)) |
Josh Poimboeuf | fcd709e | 2016-09-14 21:07:44 -0500 | [diff] [blame] | 98 | goto recursion_check; |
Josh Poimboeuf | cb76c93 | 2016-09-14 21:07:42 -0500 | [diff] [blame] | 99 | |
| 100 | if (in_softirq_stack(stack, info)) |
Josh Poimboeuf | fcd709e | 2016-09-14 21:07:44 -0500 | [diff] [blame] | 101 | goto recursion_check; |
| 102 | |
| 103 | goto unknown; |
| 104 | |
| 105 | recursion_check: |
| 106 | /* |
| 107 | * Make sure we don't iterate through any given stack more than once. |
| 108 | * If it comes up a second time then there's something wrong going on: |
| 109 | * just break out and report an unknown stack type. |
| 110 | */ |
| 111 | if (visit_mask) { |
| 112 | if (*visit_mask & (1UL << info->type)) |
| 113 | goto unknown; |
| 114 | *visit_mask |= 1UL << info->type; |
| 115 | } |
| 116 | |
| 117 | return 0; |
Josh Poimboeuf | cb76c93 | 2016-09-14 21:07:42 -0500 | [diff] [blame] | 118 | |
| 119 | unknown: |
| 120 | info->type = STACK_TYPE_UNKNOWN; |
| 121 | return -EINVAL; |
Steven Rostedt | 198d208 | 2014-02-06 09:41:31 -0500 | [diff] [blame] | 122 | } |
Frederic Weisbecker | 0406ca6 | 2009-07-01 21:02:09 +0200 | [diff] [blame] | 123 | |
Jan Beulich | 57da8b9 | 2012-05-09 08:47:37 +0100 | [diff] [blame] | 124 | void show_regs(struct pt_regs *regs) |
Alexander van Heukelum | 2bc5f92 | 2008-09-30 13:12:14 +0200 | [diff] [blame] | 125 | { |
| 126 | int i; |
| 127 | |
Tejun Heo | a43cb95 | 2013-04-30 15:27:17 -0700 | [diff] [blame] | 128 | show_regs_print_info(KERN_EMERG); |
Andy Lutomirski | f39b6f0 | 2015-03-18 18:33:33 -0700 | [diff] [blame] | 129 | __show_regs(regs, !user_mode(regs)); |
Alexander van Heukelum | 2bc5f92 | 2008-09-30 13:12:14 +0200 | [diff] [blame] | 130 | |
Alexander van Heukelum | 2bc5f92 | 2008-09-30 13:12:14 +0200 | [diff] [blame] | 131 | /* |
| 132 | * When in-kernel, we also print out the stack and code at the |
| 133 | * time of the fault.. |
| 134 | */ |
Andy Lutomirski | f39b6f0 | 2015-03-18 18:33:33 -0700 | [diff] [blame] | 135 | if (!user_mode(regs)) { |
Alexander van Heukelum | 2bc5f92 | 2008-09-30 13:12:14 +0200 | [diff] [blame] | 136 | unsigned int code_prologue = code_bytes * 43 / 64; |
| 137 | unsigned int code_len = code_bytes; |
| 138 | unsigned char c; |
| 139 | u8 *ip; |
| 140 | |
Josh Poimboeuf | 0ee1dd9 | 2016-10-25 09:51:13 -0500 | [diff] [blame^] | 141 | show_trace_log_lvl(current, regs, NULL, KERN_EMERG); |
Alexander van Heukelum | 2bc5f92 | 2008-09-30 13:12:14 +0200 | [diff] [blame] | 142 | |
Joe Perches | c767a54 | 2012-05-21 19:50:07 -0700 | [diff] [blame] | 143 | pr_emerg("Code:"); |
Alexander van Heukelum | 2bc5f92 | 2008-09-30 13:12:14 +0200 | [diff] [blame] | 144 | |
| 145 | ip = (u8 *)regs->ip - code_prologue; |
| 146 | if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) { |
Alexander van Heukelum | 8a54166 | 2008-10-04 23:12:46 +0200 | [diff] [blame] | 147 | /* try starting at IP */ |
Alexander van Heukelum | 2bc5f92 | 2008-09-30 13:12:14 +0200 | [diff] [blame] | 148 | ip = (u8 *)regs->ip; |
| 149 | code_len = code_len - code_prologue + 1; |
| 150 | } |
| 151 | for (i = 0; i < code_len; i++, ip++) { |
| 152 | if (ip < (u8 *)PAGE_OFFSET || |
| 153 | probe_kernel_address(ip, c)) { |
Joe Perches | c767a54 | 2012-05-21 19:50:07 -0700 | [diff] [blame] | 154 | pr_cont(" Bad EIP value."); |
Alexander van Heukelum | 2bc5f92 | 2008-09-30 13:12:14 +0200 | [diff] [blame] | 155 | break; |
| 156 | } |
| 157 | if (ip == (u8 *)regs->ip) |
Joe Perches | c767a54 | 2012-05-21 19:50:07 -0700 | [diff] [blame] | 158 | pr_cont(" <%02x>", c); |
Alexander van Heukelum | 2bc5f92 | 2008-09-30 13:12:14 +0200 | [diff] [blame] | 159 | else |
Joe Perches | c767a54 | 2012-05-21 19:50:07 -0700 | [diff] [blame] | 160 | pr_cont(" %02x", c); |
Alexander van Heukelum | 2bc5f92 | 2008-09-30 13:12:14 +0200 | [diff] [blame] | 161 | } |
| 162 | } |
Joe Perches | c767a54 | 2012-05-21 19:50:07 -0700 | [diff] [blame] | 163 | pr_cont("\n"); |
Alexander van Heukelum | 2bc5f92 | 2008-09-30 13:12:14 +0200 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | int is_valid_bugaddr(unsigned long ip) |
| 167 | { |
| 168 | unsigned short ud2; |
| 169 | |
| 170 | if (ip < PAGE_OFFSET) |
| 171 | return 0; |
| 172 | if (probe_kernel_address((unsigned short *)ip, ud2)) |
| 173 | return 0; |
| 174 | |
| 175 | return ud2 == 0x0b0f; |
| 176 | } |