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