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 static inline int 105 in_irq_stack(unsigned long *stack, unsigned long *irq_stack, 106 unsigned long *irq_stack_end) 107 { 108 return (stack >= irq_stack && stack < irq_stack_end); 109 } 110 111 /* 112 * We are returning from the irq stack and go to the previous one. 113 * If the previous stack is also in the irq stack, then bp in the first 114 * frame of the irq stack points to the previous, interrupted one. 115 * Otherwise we have another level of indirection: We first save 116 * the bp of the previous stack, then we switch the stack to the irq one 117 * and save a new bp that links to the previous one. 118 * (See save_args()) 119 */ 120 static inline unsigned long 121 fixup_bp_irq_link(unsigned long bp, unsigned long *stack, 122 unsigned long *irq_stack, unsigned long *irq_stack_end) 123 { 124 #ifdef CONFIG_FRAME_POINTER 125 struct stack_frame *frame = (struct stack_frame *)bp; 126 127 if (!in_irq_stack(stack, irq_stack, irq_stack_end)) 128 return (unsigned long)frame->next_frame; 129 #endif 130 return bp; 131 } 132 133 /* 134 * x86-64 can have up to three kernel stacks: 135 * process stack 136 * interrupt stack 137 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack 138 */ 139 140 void dump_trace(struct task_struct *task, struct pt_regs *regs, 141 unsigned long *stack, unsigned long bp, 142 const struct stacktrace_ops *ops, void *data) 143 { 144 const unsigned cpu = get_cpu(); 145 unsigned long *irq_stack_end = 146 (unsigned long *)per_cpu(irq_stack_ptr, cpu); 147 unsigned used = 0; 148 struct thread_info *tinfo; 149 int graph = 0; 150 151 if (!task) 152 task = current; 153 154 if (!stack) { 155 unsigned long dummy; 156 stack = &dummy; 157 if (task && task != current) 158 stack = (unsigned long *)task->thread.sp; 159 } 160 161 #ifdef CONFIG_FRAME_POINTER 162 if (!bp) { 163 if (task == current) { 164 /* Grab bp right from our regs */ 165 get_bp(bp); 166 } else { 167 /* bp is the last reg pushed by switch_to */ 168 bp = *(unsigned long *) task->thread.sp; 169 } 170 } 171 #endif 172 173 /* 174 * Print function call entries in all stacks, starting at the 175 * current stack address. If the stacks consist of nested 176 * exceptions 177 */ 178 tinfo = task_thread_info(task); 179 for (;;) { 180 char *id; 181 unsigned long *estack_end; 182 estack_end = in_exception_stack(cpu, (unsigned long)stack, 183 &used, &id); 184 185 if (estack_end) { 186 if (ops->stack(data, id) < 0) 187 break; 188 189 bp = print_context_stack(tinfo, stack, bp, ops, 190 data, estack_end, &graph); 191 ops->stack(data, "<EOE>"); 192 /* 193 * We link to the next stack via the 194 * second-to-last pointer (index -2 to end) in the 195 * exception stack: 196 */ 197 stack = (unsigned long *) estack_end[-2]; 198 continue; 199 } 200 if (irq_stack_end) { 201 unsigned long *irq_stack; 202 irq_stack = irq_stack_end - 203 (IRQ_STACK_SIZE - 64) / sizeof(*irq_stack); 204 205 if (in_irq_stack(stack, irq_stack, irq_stack_end)) { 206 if (ops->stack(data, "IRQ") < 0) 207 break; 208 bp = print_context_stack(tinfo, stack, bp, 209 ops, data, irq_stack_end, &graph); 210 /* 211 * We link to the next stack (which would be 212 * the process stack normally) the last 213 * pointer (index -1 to end) in the IRQ stack: 214 */ 215 stack = (unsigned long *) (irq_stack_end[-1]); 216 bp = fixup_bp_irq_link(bp, stack, irq_stack, 217 irq_stack_end); 218 irq_stack_end = NULL; 219 ops->stack(data, "EOI"); 220 continue; 221 } 222 } 223 break; 224 } 225 226 /* 227 * This handles the process stack: 228 */ 229 bp = print_context_stack(tinfo, stack, bp, ops, data, NULL, &graph); 230 put_cpu(); 231 } 232 EXPORT_SYMBOL(dump_trace); 233 234 void 235 show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs, 236 unsigned long *sp, unsigned long bp, char *log_lvl) 237 { 238 unsigned long *stack; 239 int i; 240 const int cpu = smp_processor_id(); 241 unsigned long *irq_stack_end = 242 (unsigned long *)(per_cpu(irq_stack_ptr, cpu)); 243 unsigned long *irq_stack = 244 (unsigned long *)(per_cpu(irq_stack_ptr, cpu) - IRQ_STACK_SIZE); 245 246 /* 247 * debugging aid: "show_stack(NULL, NULL);" prints the 248 * back trace for this cpu. 249 */ 250 251 if (sp == NULL) { 252 if (task) 253 sp = (unsigned long *)task->thread.sp; 254 else 255 sp = (unsigned long *)&sp; 256 } 257 258 stack = sp; 259 for (i = 0; i < kstack_depth_to_print; i++) { 260 if (stack >= irq_stack && stack <= irq_stack_end) { 261 if (stack == irq_stack_end) { 262 stack = (unsigned long *) (irq_stack_end[-1]); 263 printk(" <EOI> "); 264 } 265 } else { 266 if (((long) stack & (THREAD_SIZE-1)) == 0) 267 break; 268 } 269 if (i && ((i % STACKSLOTS_PER_LINE) == 0)) 270 printk("\n%s", log_lvl); 271 printk(" %016lx", *stack++); 272 touch_nmi_watchdog(); 273 } 274 printk("\n"); 275 show_trace_log_lvl(task, regs, sp, bp, log_lvl); 276 } 277 278 void show_registers(struct pt_regs *regs) 279 { 280 int i; 281 unsigned long sp; 282 const int cpu = smp_processor_id(); 283 struct task_struct *cur = current; 284 285 sp = regs->sp; 286 printk("CPU %d ", cpu); 287 __show_regs(regs, 1); 288 printk("Process %s (pid: %d, threadinfo %p, task %p)\n", 289 cur->comm, cur->pid, task_thread_info(cur), cur); 290 291 /* 292 * When in-kernel, we also print out the stack and code at the 293 * time of the fault.. 294 */ 295 if (!user_mode(regs)) { 296 unsigned int code_prologue = code_bytes * 43 / 64; 297 unsigned int code_len = code_bytes; 298 unsigned char c; 299 u8 *ip; 300 301 printk(KERN_EMERG "Stack:\n"); 302 show_stack_log_lvl(NULL, regs, (unsigned long *)sp, 303 regs->bp, KERN_EMERG); 304 305 printk(KERN_EMERG "Code: "); 306 307 ip = (u8 *)regs->ip - code_prologue; 308 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) { 309 /* try starting at IP */ 310 ip = (u8 *)regs->ip; 311 code_len = code_len - code_prologue + 1; 312 } 313 for (i = 0; i < code_len; i++, ip++) { 314 if (ip < (u8 *)PAGE_OFFSET || 315 probe_kernel_address(ip, c)) { 316 printk(" Bad RIP value."); 317 break; 318 } 319 if (ip == (u8 *)regs->ip) 320 printk("<%02x> ", c); 321 else 322 printk("%02x ", c); 323 } 324 } 325 printk("\n"); 326 } 327 328 int is_valid_bugaddr(unsigned long ip) 329 { 330 unsigned short ud2; 331 332 if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2))) 333 return 0; 334 335 return ud2 == 0x0b0f; 336 } 337 338