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