Alexander van Heukelum | 6fcbede | 2008-09-30 13:12:15 +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> |
| 8 | #include <linux/utsname.h> |
| 9 | #include <linux/hardirq.h> |
| 10 | #include <linux/kdebug.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/ptrace.h> |
| 13 | #include <linux/kexec.h> |
| 14 | #include <linux/bug.h> |
| 15 | #include <linux/nmi.h> |
Andrew Morton | ae87221 | 2007-08-24 16:11:54 -0700 | [diff] [blame] | 16 | #include <linux/sysfs.h> |
Alexander van Heukelum | 6fcbede | 2008-09-30 13:12:15 +0200 | [diff] [blame] | 17 | |
| 18 | #include <asm/stacktrace.h> |
| 19 | |
Neil Horman | 878719e | 2008-10-23 10:40:06 -0400 | [diff] [blame] | 20 | #include "dumpstack.h" |
Alexander van Heukelum | 6fcbede | 2008-09-30 13:12:15 +0200 | [diff] [blame] | 21 | |
| 22 | static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack, |
| 23 | unsigned *usedp, char **idp) |
| 24 | { |
| 25 | static char ids[][8] = { |
| 26 | [DEBUG_STACK - 1] = "#DB", |
| 27 | [NMI_STACK - 1] = "NMI", |
| 28 | [DOUBLEFAULT_STACK - 1] = "#DF", |
| 29 | [STACKFAULT_STACK - 1] = "#SS", |
| 30 | [MCE_STACK - 1] = "#MC", |
| 31 | #if DEBUG_STKSZ > EXCEPTION_STKSZ |
| 32 | [N_EXCEPTION_STACKS ... |
| 33 | N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]" |
| 34 | #endif |
| 35 | }; |
| 36 | unsigned k; |
| 37 | |
| 38 | /* |
| 39 | * Iterate over all exception stacks, and figure out whether |
| 40 | * 'stack' is in one of them: |
| 41 | */ |
| 42 | for (k = 0; k < N_EXCEPTION_STACKS; k++) { |
| 43 | unsigned long end = per_cpu(orig_ist, cpu).ist[k]; |
| 44 | /* |
| 45 | * Is 'stack' above this exception frame's end? |
| 46 | * If yes then skip to the next frame. |
| 47 | */ |
| 48 | if (stack >= end) |
| 49 | continue; |
| 50 | /* |
| 51 | * Is 'stack' above this exception frame's start address? |
| 52 | * If yes then we found the right frame. |
| 53 | */ |
| 54 | if (stack >= end - EXCEPTION_STKSZ) { |
| 55 | /* |
| 56 | * Make sure we only iterate through an exception |
| 57 | * stack once. If it comes up for the second time |
| 58 | * then there's something wrong going on - just |
| 59 | * break out and return NULL: |
| 60 | */ |
| 61 | if (*usedp & (1U << k)) |
| 62 | break; |
| 63 | *usedp |= 1U << k; |
| 64 | *idp = ids[k]; |
| 65 | return (unsigned long *)end; |
| 66 | } |
| 67 | /* |
| 68 | * If this is a debug stack, and if it has a larger size than |
| 69 | * the usual exception stacks, then 'stack' might still |
| 70 | * be within the lower portion of the debug stack: |
| 71 | */ |
| 72 | #if DEBUG_STKSZ > EXCEPTION_STKSZ |
| 73 | if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) { |
| 74 | unsigned j = N_EXCEPTION_STACKS - 1; |
| 75 | |
| 76 | /* |
| 77 | * Black magic. A large debug stack is composed of |
| 78 | * multiple exception stack entries, which we |
| 79 | * iterate through now. Dont look: |
| 80 | */ |
| 81 | do { |
| 82 | ++j; |
| 83 | end -= EXCEPTION_STKSZ; |
| 84 | ids[j][4] = '1' + (j - N_EXCEPTION_STACKS); |
| 85 | } while (stack < end - EXCEPTION_STKSZ); |
| 86 | if (*usedp & (1U << j)) |
| 87 | break; |
| 88 | *usedp |= 1U << j; |
| 89 | *idp = ids[j]; |
| 90 | return (unsigned long *)end; |
| 91 | } |
| 92 | #endif |
| 93 | } |
| 94 | return NULL; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * x86-64 can have up to three kernel stacks: |
| 99 | * process stack |
| 100 | * interrupt stack |
| 101 | * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack |
| 102 | */ |
| 103 | |
Alexander van Heukelum | 6fcbede | 2008-09-30 13:12:15 +0200 | [diff] [blame] | 104 | void dump_trace(struct task_struct *task, struct pt_regs *regs, |
| 105 | unsigned long *stack, unsigned long bp, |
| 106 | const struct stacktrace_ops *ops, void *data) |
| 107 | { |
| 108 | const unsigned cpu = get_cpu(); |
Brian Gerst | 26f80bd | 2009-01-19 00:38:58 +0900 | [diff] [blame] | 109 | unsigned long *irq_stack_end = |
| 110 | (unsigned long *)per_cpu(irq_stack_ptr, cpu); |
Alexander van Heukelum | 6fcbede | 2008-09-30 13:12:15 +0200 | [diff] [blame] | 111 | unsigned used = 0; |
| 112 | struct thread_info *tinfo; |
Steven Rostedt | 7ee991f | 2008-12-02 23:50:04 -0500 | [diff] [blame] | 113 | int graph = 0; |
Alexander van Heukelum | 6fcbede | 2008-09-30 13:12:15 +0200 | [diff] [blame] | 114 | |
| 115 | if (!task) |
| 116 | task = current; |
| 117 | |
| 118 | if (!stack) { |
| 119 | unsigned long dummy; |
| 120 | stack = &dummy; |
| 121 | if (task && task != current) |
| 122 | stack = (unsigned long *)task->thread.sp; |
| 123 | } |
| 124 | |
| 125 | #ifdef CONFIG_FRAME_POINTER |
| 126 | if (!bp) { |
| 127 | if (task == current) { |
| 128 | /* Grab bp right from our regs */ |
Alexander van Heukelum | 8a54166 | 2008-10-04 23:12:46 +0200 | [diff] [blame] | 129 | get_bp(bp); |
Alexander van Heukelum | 6fcbede | 2008-09-30 13:12:15 +0200 | [diff] [blame] | 130 | } else { |
| 131 | /* bp is the last reg pushed by switch_to */ |
| 132 | bp = *(unsigned long *) task->thread.sp; |
| 133 | } |
| 134 | } |
| 135 | #endif |
| 136 | |
| 137 | /* |
| 138 | * Print function call entries in all stacks, starting at the |
| 139 | * current stack address. If the stacks consist of nested |
| 140 | * exceptions |
| 141 | */ |
| 142 | tinfo = task_thread_info(task); |
| 143 | for (;;) { |
| 144 | char *id; |
| 145 | unsigned long *estack_end; |
| 146 | estack_end = in_exception_stack(cpu, (unsigned long)stack, |
| 147 | &used, &id); |
| 148 | |
| 149 | if (estack_end) { |
| 150 | if (ops->stack(data, id) < 0) |
| 151 | break; |
| 152 | |
| 153 | bp = print_context_stack(tinfo, stack, bp, ops, |
Steven Rostedt | 7ee991f | 2008-12-02 23:50:04 -0500 | [diff] [blame] | 154 | data, estack_end, &graph); |
Alexander van Heukelum | 6fcbede | 2008-09-30 13:12:15 +0200 | [diff] [blame] | 155 | ops->stack(data, "<EOE>"); |
| 156 | /* |
| 157 | * We link to the next stack via the |
| 158 | * second-to-last pointer (index -2 to end) in the |
| 159 | * exception stack: |
| 160 | */ |
| 161 | stack = (unsigned long *) estack_end[-2]; |
| 162 | continue; |
| 163 | } |
Brian Gerst | 26f80bd | 2009-01-19 00:38:58 +0900 | [diff] [blame] | 164 | if (irq_stack_end) { |
| 165 | unsigned long *irq_stack; |
| 166 | irq_stack = irq_stack_end - |
| 167 | (IRQ_STACK_SIZE - 64) / sizeof(*irq_stack); |
Alexander van Heukelum | 6fcbede | 2008-09-30 13:12:15 +0200 | [diff] [blame] | 168 | |
Brian Gerst | 26f80bd | 2009-01-19 00:38:58 +0900 | [diff] [blame] | 169 | if (stack >= irq_stack && stack < irq_stack_end) { |
Alexander van Heukelum | 6fcbede | 2008-09-30 13:12:15 +0200 | [diff] [blame] | 170 | if (ops->stack(data, "IRQ") < 0) |
| 171 | break; |
| 172 | bp = print_context_stack(tinfo, stack, bp, |
Brian Gerst | 26f80bd | 2009-01-19 00:38:58 +0900 | [diff] [blame] | 173 | ops, data, irq_stack_end, &graph); |
Alexander van Heukelum | 6fcbede | 2008-09-30 13:12:15 +0200 | [diff] [blame] | 174 | /* |
| 175 | * We link to the next stack (which would be |
| 176 | * the process stack normally) the last |
| 177 | * pointer (index -1 to end) in the IRQ stack: |
| 178 | */ |
Brian Gerst | 26f80bd | 2009-01-19 00:38:58 +0900 | [diff] [blame] | 179 | stack = (unsigned long *) (irq_stack_end[-1]); |
| 180 | irq_stack_end = NULL; |
Alexander van Heukelum | 6fcbede | 2008-09-30 13:12:15 +0200 | [diff] [blame] | 181 | ops->stack(data, "EOI"); |
| 182 | continue; |
| 183 | } |
| 184 | } |
| 185 | break; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * This handles the process stack: |
| 190 | */ |
Steven Rostedt | 7ee991f | 2008-12-02 23:50:04 -0500 | [diff] [blame] | 191 | bp = print_context_stack(tinfo, stack, bp, ops, data, NULL, &graph); |
Alexander van Heukelum | 6fcbede | 2008-09-30 13:12:15 +0200 | [diff] [blame] | 192 | put_cpu(); |
| 193 | } |
| 194 | EXPORT_SYMBOL(dump_trace); |
| 195 | |
Neil Horman | 878719e | 2008-10-23 10:40:06 -0400 | [diff] [blame] | 196 | void |
Alexander van Heukelum | 6fcbede | 2008-09-30 13:12:15 +0200 | [diff] [blame] | 197 | show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs, |
| 198 | unsigned long *sp, unsigned long bp, char *log_lvl) |
| 199 | { |
| 200 | unsigned long *stack; |
| 201 | int i; |
| 202 | const int cpu = smp_processor_id(); |
Brian Gerst | 26f80bd | 2009-01-19 00:38:58 +0900 | [diff] [blame] | 203 | unsigned long *irq_stack_end = |
| 204 | (unsigned long *)(per_cpu(irq_stack_ptr, cpu)); |
| 205 | unsigned long *irq_stack = |
| 206 | (unsigned long *)(per_cpu(irq_stack_ptr, cpu) - IRQ_STACK_SIZE); |
Alexander van Heukelum | 6fcbede | 2008-09-30 13:12:15 +0200 | [diff] [blame] | 207 | |
| 208 | /* |
| 209 | * debugging aid: "show_stack(NULL, NULL);" prints the |
| 210 | * back trace for this cpu. |
| 211 | */ |
| 212 | |
| 213 | if (sp == NULL) { |
| 214 | if (task) |
| 215 | sp = (unsigned long *)task->thread.sp; |
| 216 | else |
| 217 | sp = (unsigned long *)&sp; |
| 218 | } |
| 219 | |
| 220 | stack = sp; |
| 221 | for (i = 0; i < kstack_depth_to_print; i++) { |
Brian Gerst | 26f80bd | 2009-01-19 00:38:58 +0900 | [diff] [blame] | 222 | if (stack >= irq_stack && stack <= irq_stack_end) { |
| 223 | if (stack == irq_stack_end) { |
| 224 | stack = (unsigned long *) (irq_stack_end[-1]); |
Alexander van Heukelum | 6fcbede | 2008-09-30 13:12:15 +0200 | [diff] [blame] | 225 | printk(" <EOI> "); |
| 226 | } |
| 227 | } else { |
| 228 | if (((long) stack & (THREAD_SIZE-1)) == 0) |
| 229 | break; |
| 230 | } |
Alexander van Heukelum | 8a54166 | 2008-10-04 23:12:46 +0200 | [diff] [blame] | 231 | if (i && ((i % STACKSLOTS_PER_LINE) == 0)) |
Alexander van Heukelum | ca0a816 | 2008-10-04 23:12:44 +0200 | [diff] [blame] | 232 | printk("\n%s", log_lvl); |
Alexander van Heukelum | 6fcbede | 2008-09-30 13:12:15 +0200 | [diff] [blame] | 233 | printk(" %016lx", *stack++); |
| 234 | touch_nmi_watchdog(); |
| 235 | } |
| 236 | printk("\n"); |
| 237 | show_trace_log_lvl(task, regs, sp, bp, log_lvl); |
| 238 | } |
| 239 | |
Alexander van Heukelum | 6fcbede | 2008-09-30 13:12:15 +0200 | [diff] [blame] | 240 | void show_registers(struct pt_regs *regs) |
| 241 | { |
| 242 | int i; |
| 243 | unsigned long sp; |
| 244 | const int cpu = smp_processor_id(); |
Brian Gerst | c6f5e0a | 2009-01-19 00:38:58 +0900 | [diff] [blame] | 245 | struct task_struct *cur = current; |
Alexander van Heukelum | 6fcbede | 2008-09-30 13:12:15 +0200 | [diff] [blame] | 246 | |
| 247 | sp = regs->sp; |
| 248 | printk("CPU %d ", cpu); |
| 249 | __show_regs(regs, 1); |
| 250 | printk("Process %s (pid: %d, threadinfo %p, task %p)\n", |
| 251 | cur->comm, cur->pid, task_thread_info(cur), cur); |
| 252 | |
| 253 | /* |
| 254 | * When in-kernel, we also print out the stack and code at the |
| 255 | * time of the fault.. |
| 256 | */ |
| 257 | if (!user_mode(regs)) { |
| 258 | unsigned int code_prologue = code_bytes * 43 / 64; |
| 259 | unsigned int code_len = code_bytes; |
| 260 | unsigned char c; |
| 261 | u8 *ip; |
| 262 | |
Alexander van Heukelum | ca0a816 | 2008-10-04 23:12:44 +0200 | [diff] [blame] | 263 | printk(KERN_EMERG "Stack:\n"); |
Alexander van Heukelum | 6fcbede | 2008-09-30 13:12:15 +0200 | [diff] [blame] | 264 | show_stack_log_lvl(NULL, regs, (unsigned long *)sp, |
Alexander van Heukelum | ca0a816 | 2008-10-04 23:12:44 +0200 | [diff] [blame] | 265 | regs->bp, KERN_EMERG); |
Alexander van Heukelum | 6fcbede | 2008-09-30 13:12:15 +0200 | [diff] [blame] | 266 | |
| 267 | printk(KERN_EMERG "Code: "); |
| 268 | |
| 269 | ip = (u8 *)regs->ip - code_prologue; |
| 270 | if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) { |
Alexander van Heukelum | 8a54166 | 2008-10-04 23:12:46 +0200 | [diff] [blame] | 271 | /* try starting at IP */ |
Alexander van Heukelum | 6fcbede | 2008-09-30 13:12:15 +0200 | [diff] [blame] | 272 | ip = (u8 *)regs->ip; |
| 273 | code_len = code_len - code_prologue + 1; |
| 274 | } |
| 275 | for (i = 0; i < code_len; i++, ip++) { |
| 276 | if (ip < (u8 *)PAGE_OFFSET || |
| 277 | probe_kernel_address(ip, c)) { |
| 278 | printk(" Bad RIP value."); |
| 279 | break; |
| 280 | } |
| 281 | if (ip == (u8 *)regs->ip) |
| 282 | printk("<%02x> ", c); |
| 283 | else |
| 284 | printk("%02x ", c); |
| 285 | } |
| 286 | } |
| 287 | printk("\n"); |
| 288 | } |
| 289 | |
| 290 | int is_valid_bugaddr(unsigned long ip) |
| 291 | { |
| 292 | unsigned short ud2; |
| 293 | |
| 294 | if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2))) |
| 295 | return 0; |
| 296 | |
| 297 | return ud2 == 0x0b0f; |
| 298 | } |
| 299 | |