traps.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  1. /*
  2. * linux/arch/x86-64/traps.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
  6. *
  7. * Pentium III FXSR, SSE support
  8. * Gareth Hughes <gareth@valinux.com>, May 2000
  9. */
  10. /*
  11. * 'Traps.c' handles hardware traps and faults after we have saved some
  12. * state in 'entry.S'.
  13. */
  14. #include <linux/sched.h>
  15. #include <linux/kernel.h>
  16. #include <linux/string.h>
  17. #include <linux/errno.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/timer.h>
  20. #include <linux/mm.h>
  21. #include <linux/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/module.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/nmi.h>
  28. #include <linux/kprobes.h>
  29. #include <linux/kexec.h>
  30. #include <linux/unwind.h>
  31. #include <asm/system.h>
  32. #include <asm/uaccess.h>
  33. #include <asm/io.h>
  34. #include <asm/atomic.h>
  35. #include <asm/debugreg.h>
  36. #include <asm/desc.h>
  37. #include <asm/i387.h>
  38. #include <asm/kdebug.h>
  39. #include <asm/processor.h>
  40. #include <asm/unwind.h>
  41. #include <asm/smp.h>
  42. #include <asm/pgalloc.h>
  43. #include <asm/pda.h>
  44. #include <asm/proto.h>
  45. #include <asm/nmi.h>
  46. #include <asm/stacktrace.h>
  47. asmlinkage void divide_error(void);
  48. asmlinkage void debug(void);
  49. asmlinkage void nmi(void);
  50. asmlinkage void int3(void);
  51. asmlinkage void overflow(void);
  52. asmlinkage void bounds(void);
  53. asmlinkage void invalid_op(void);
  54. asmlinkage void device_not_available(void);
  55. asmlinkage void double_fault(void);
  56. asmlinkage void coprocessor_segment_overrun(void);
  57. asmlinkage void invalid_TSS(void);
  58. asmlinkage void segment_not_present(void);
  59. asmlinkage void stack_segment(void);
  60. asmlinkage void general_protection(void);
  61. asmlinkage void page_fault(void);
  62. asmlinkage void coprocessor_error(void);
  63. asmlinkage void simd_coprocessor_error(void);
  64. asmlinkage void reserved(void);
  65. asmlinkage void alignment_check(void);
  66. asmlinkage void machine_check(void);
  67. asmlinkage void spurious_interrupt_bug(void);
  68. ATOMIC_NOTIFIER_HEAD(die_chain);
  69. EXPORT_SYMBOL(die_chain);
  70. int register_die_notifier(struct notifier_block *nb)
  71. {
  72. vmalloc_sync_all();
  73. return atomic_notifier_chain_register(&die_chain, nb);
  74. }
  75. EXPORT_SYMBOL(register_die_notifier); /* used modular by kdb */
  76. int unregister_die_notifier(struct notifier_block *nb)
  77. {
  78. return atomic_notifier_chain_unregister(&die_chain, nb);
  79. }
  80. EXPORT_SYMBOL(unregister_die_notifier); /* used modular by kdb */
  81. static inline void conditional_sti(struct pt_regs *regs)
  82. {
  83. if (regs->eflags & X86_EFLAGS_IF)
  84. local_irq_enable();
  85. }
  86. static inline void preempt_conditional_sti(struct pt_regs *regs)
  87. {
  88. preempt_disable();
  89. if (regs->eflags & X86_EFLAGS_IF)
  90. local_irq_enable();
  91. }
  92. static inline void preempt_conditional_cli(struct pt_regs *regs)
  93. {
  94. if (regs->eflags & X86_EFLAGS_IF)
  95. local_irq_disable();
  96. /* Make sure to not schedule here because we could be running
  97. on an exception stack. */
  98. preempt_enable_no_resched();
  99. }
  100. static int kstack_depth_to_print = 12;
  101. #ifdef CONFIG_STACK_UNWIND
  102. static int call_trace = 1;
  103. #else
  104. #define call_trace (-1)
  105. #endif
  106. #ifdef CONFIG_KALLSYMS
  107. # include <linux/kallsyms.h>
  108. void printk_address(unsigned long address)
  109. {
  110. unsigned long offset = 0, symsize;
  111. const char *symname;
  112. char *modname;
  113. char *delim = ":";
  114. char namebuf[128];
  115. symname = kallsyms_lookup(address, &symsize, &offset,
  116. &modname, namebuf);
  117. if (!symname) {
  118. printk(" [<%016lx>]\n", address);
  119. return;
  120. }
  121. if (!modname)
  122. modname = delim = "";
  123. printk(" [<%016lx>] %s%s%s%s+0x%lx/0x%lx\n",
  124. address, delim, modname, delim, symname, offset, symsize);
  125. }
  126. #else
  127. void printk_address(unsigned long address)
  128. {
  129. printk(" [<%016lx>]\n", address);
  130. }
  131. #endif
  132. static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
  133. unsigned *usedp, char **idp)
  134. {
  135. static char ids[][8] = {
  136. [DEBUG_STACK - 1] = "#DB",
  137. [NMI_STACK - 1] = "NMI",
  138. [DOUBLEFAULT_STACK - 1] = "#DF",
  139. [STACKFAULT_STACK - 1] = "#SS",
  140. [MCE_STACK - 1] = "#MC",
  141. #if DEBUG_STKSZ > EXCEPTION_STKSZ
  142. [N_EXCEPTION_STACKS ... N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
  143. #endif
  144. };
  145. unsigned k;
  146. /*
  147. * Iterate over all exception stacks, and figure out whether
  148. * 'stack' is in one of them:
  149. */
  150. for (k = 0; k < N_EXCEPTION_STACKS; k++) {
  151. unsigned long end;
  152. /*
  153. * set 'end' to the end of the exception stack.
  154. */
  155. switch (k + 1) {
  156. /*
  157. * TODO: this block is not needed i think, because
  158. * setup64.c:cpu_init() sets up t->ist[DEBUG_STACK]
  159. * properly too.
  160. */
  161. #if DEBUG_STKSZ > EXCEPTION_STKSZ
  162. case DEBUG_STACK:
  163. end = cpu_pda(cpu)->debugstack + DEBUG_STKSZ;
  164. break;
  165. #endif
  166. default:
  167. end = per_cpu(orig_ist, cpu).ist[k];
  168. break;
  169. }
  170. /*
  171. * Is 'stack' above this exception frame's end?
  172. * If yes then skip to the next frame.
  173. */
  174. if (stack >= end)
  175. continue;
  176. /*
  177. * Is 'stack' above this exception frame's start address?
  178. * If yes then we found the right frame.
  179. */
  180. if (stack >= end - EXCEPTION_STKSZ) {
  181. /*
  182. * Make sure we only iterate through an exception
  183. * stack once. If it comes up for the second time
  184. * then there's something wrong going on - just
  185. * break out and return NULL:
  186. */
  187. if (*usedp & (1U << k))
  188. break;
  189. *usedp |= 1U << k;
  190. *idp = ids[k];
  191. return (unsigned long *)end;
  192. }
  193. /*
  194. * If this is a debug stack, and if it has a larger size than
  195. * the usual exception stacks, then 'stack' might still
  196. * be within the lower portion of the debug stack:
  197. */
  198. #if DEBUG_STKSZ > EXCEPTION_STKSZ
  199. if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
  200. unsigned j = N_EXCEPTION_STACKS - 1;
  201. /*
  202. * Black magic. A large debug stack is composed of
  203. * multiple exception stack entries, which we
  204. * iterate through now. Dont look:
  205. */
  206. do {
  207. ++j;
  208. end -= EXCEPTION_STKSZ;
  209. ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
  210. } while (stack < end - EXCEPTION_STKSZ);
  211. if (*usedp & (1U << j))
  212. break;
  213. *usedp |= 1U << j;
  214. *idp = ids[j];
  215. return (unsigned long *)end;
  216. }
  217. #endif
  218. }
  219. return NULL;
  220. }
  221. struct ops_and_data {
  222. struct stacktrace_ops *ops;
  223. void *data;
  224. };
  225. static int dump_trace_unwind(struct unwind_frame_info *info, void *context)
  226. {
  227. struct ops_and_data *oad = (struct ops_and_data *)context;
  228. int n = 0;
  229. while (unwind(info) == 0 && UNW_PC(info)) {
  230. n++;
  231. oad->ops->address(oad->data, UNW_PC(info));
  232. if (arch_unw_user_mode(info))
  233. break;
  234. }
  235. return n;
  236. }
  237. /*
  238. * x86-64 can have upto three kernel stacks:
  239. * process stack
  240. * interrupt stack
  241. * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
  242. */
  243. void dump_trace(struct task_struct *tsk, struct pt_regs *regs, unsigned long * stack,
  244. struct stacktrace_ops *ops, void *data)
  245. {
  246. const unsigned cpu = safe_smp_processor_id();
  247. unsigned long *irqstack_end = (unsigned long *)cpu_pda(cpu)->irqstackptr;
  248. unsigned used = 0;
  249. if (!tsk)
  250. tsk = current;
  251. if (call_trace >= 0) {
  252. int unw_ret = 0;
  253. struct unwind_frame_info info;
  254. struct ops_and_data oad = { .ops = ops, .data = data };
  255. if (regs) {
  256. if (unwind_init_frame_info(&info, tsk, regs) == 0)
  257. unw_ret = dump_trace_unwind(&info, &oad);
  258. } else if (tsk == current)
  259. unw_ret = unwind_init_running(&info, dump_trace_unwind, &oad);
  260. else {
  261. if (unwind_init_blocked(&info, tsk) == 0)
  262. unw_ret = dump_trace_unwind(&info, &oad);
  263. }
  264. if (unw_ret > 0) {
  265. if (call_trace == 1 && !arch_unw_user_mode(&info)) {
  266. ops->warning_symbol(data, "DWARF2 unwinder stuck at %s\n",
  267. UNW_PC(&info));
  268. if ((long)UNW_SP(&info) < 0) {
  269. ops->warning(data, "Leftover inexact backtrace:\n");
  270. stack = (unsigned long *)UNW_SP(&info);
  271. if (!stack)
  272. return;
  273. } else
  274. ops->warning(data, "Full inexact backtrace again:\n");
  275. } else if (call_trace >= 1)
  276. return;
  277. else
  278. ops->warning(data, "Full inexact backtrace again:\n");
  279. } else
  280. ops->warning(data, "Inexact backtrace:\n");
  281. }
  282. if (!stack) {
  283. unsigned long dummy;
  284. stack = &dummy;
  285. if (tsk && tsk != current)
  286. stack = (unsigned long *)tsk->thread.rsp;
  287. }
  288. /*
  289. * Print function call entries within a stack. 'cond' is the
  290. * "end of stackframe" condition, that the 'stack++'
  291. * iteration will eventually trigger.
  292. */
  293. #define HANDLE_STACK(cond) \
  294. do while (cond) { \
  295. unsigned long addr = *stack++; \
  296. if (kernel_text_address(addr)) { \
  297. /* \
  298. * If the address is either in the text segment of the \
  299. * kernel, or in the region which contains vmalloc'ed \
  300. * memory, it *may* be the address of a calling \
  301. * routine; if so, print it so that someone tracing \
  302. * down the cause of the crash will be able to figure \
  303. * out the call path that was taken. \
  304. */ \
  305. ops->address(data, addr); \
  306. } \
  307. } while (0)
  308. /*
  309. * Print function call entries in all stacks, starting at the
  310. * current stack address. If the stacks consist of nested
  311. * exceptions
  312. */
  313. for (;;) {
  314. char *id;
  315. unsigned long *estack_end;
  316. estack_end = in_exception_stack(cpu, (unsigned long)stack,
  317. &used, &id);
  318. if (estack_end) {
  319. if (ops->stack(data, id) < 0)
  320. break;
  321. HANDLE_STACK (stack < estack_end);
  322. ops->stack(data, "<EOE>");
  323. /*
  324. * We link to the next stack via the
  325. * second-to-last pointer (index -2 to end) in the
  326. * exception stack:
  327. */
  328. stack = (unsigned long *) estack_end[-2];
  329. continue;
  330. }
  331. if (irqstack_end) {
  332. unsigned long *irqstack;
  333. irqstack = irqstack_end -
  334. (IRQSTACKSIZE - 64) / sizeof(*irqstack);
  335. if (stack >= irqstack && stack < irqstack_end) {
  336. if (ops->stack(data, "IRQ") < 0)
  337. break;
  338. HANDLE_STACK (stack < irqstack_end);
  339. /*
  340. * We link to the next stack (which would be
  341. * the process stack normally) the last
  342. * pointer (index -1 to end) in the IRQ stack:
  343. */
  344. stack = (unsigned long *) (irqstack_end[-1]);
  345. irqstack_end = NULL;
  346. ops->stack(data, "EOI");
  347. continue;
  348. }
  349. }
  350. break;
  351. }
  352. /*
  353. * This handles the process stack:
  354. */
  355. HANDLE_STACK (((long) stack & (THREAD_SIZE-1)) != 0);
  356. #undef HANDLE_STACK
  357. }
  358. EXPORT_SYMBOL(dump_trace);
  359. static void
  360. print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
  361. {
  362. print_symbol(msg, symbol);
  363. printk("\n");
  364. }
  365. static void print_trace_warning(void *data, char *msg)
  366. {
  367. printk("%s\n", msg);
  368. }
  369. static int print_trace_stack(void *data, char *name)
  370. {
  371. printk(" <%s> ", name);
  372. return 0;
  373. }
  374. static void print_trace_address(void *data, unsigned long addr)
  375. {
  376. printk_address(addr);
  377. }
  378. static struct stacktrace_ops print_trace_ops = {
  379. .warning = print_trace_warning,
  380. .warning_symbol = print_trace_warning_symbol,
  381. .stack = print_trace_stack,
  382. .address = print_trace_address,
  383. };
  384. void
  385. show_trace(struct task_struct *tsk, struct pt_regs *regs, unsigned long *stack)
  386. {
  387. printk("\nCall Trace:\n");
  388. dump_trace(tsk, regs, stack, &print_trace_ops, NULL);
  389. printk("\n");
  390. }
  391. static void
  392. _show_stack(struct task_struct *tsk, struct pt_regs *regs, unsigned long *rsp)
  393. {
  394. unsigned long *stack;
  395. int i;
  396. const int cpu = safe_smp_processor_id();
  397. unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
  398. unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
  399. // debugging aid: "show_stack(NULL, NULL);" prints the
  400. // back trace for this cpu.
  401. if (rsp == NULL) {
  402. if (tsk)
  403. rsp = (unsigned long *)tsk->thread.rsp;
  404. else
  405. rsp = (unsigned long *)&rsp;
  406. }
  407. stack = rsp;
  408. for(i=0; i < kstack_depth_to_print; i++) {
  409. if (stack >= irqstack && stack <= irqstack_end) {
  410. if (stack == irqstack_end) {
  411. stack = (unsigned long *) (irqstack_end[-1]);
  412. printk(" <EOI> ");
  413. }
  414. } else {
  415. if (((long) stack & (THREAD_SIZE-1)) == 0)
  416. break;
  417. }
  418. if (i && ((i % 4) == 0))
  419. printk("\n");
  420. printk(" %016lx", *stack++);
  421. touch_nmi_watchdog();
  422. }
  423. show_trace(tsk, regs, rsp);
  424. }
  425. void show_stack(struct task_struct *tsk, unsigned long * rsp)
  426. {
  427. _show_stack(tsk, NULL, rsp);
  428. }
  429. /*
  430. * The architecture-independent dump_stack generator
  431. */
  432. void dump_stack(void)
  433. {
  434. unsigned long dummy;
  435. show_trace(NULL, NULL, &dummy);
  436. }
  437. EXPORT_SYMBOL(dump_stack);
  438. void show_registers(struct pt_regs *regs)
  439. {
  440. int i;
  441. int in_kernel = !user_mode(regs);
  442. unsigned long rsp;
  443. const int cpu = safe_smp_processor_id();
  444. struct task_struct *cur = cpu_pda(cpu)->pcurrent;
  445. rsp = regs->rsp;
  446. printk("CPU %d ", cpu);
  447. __show_regs(regs);
  448. printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
  449. cur->comm, cur->pid, task_thread_info(cur), cur);
  450. /*
  451. * When in-kernel, we also print out the stack and code at the
  452. * time of the fault..
  453. */
  454. if (in_kernel) {
  455. printk("Stack: ");
  456. _show_stack(NULL, regs, (unsigned long*)rsp);
  457. printk("\nCode: ");
  458. if (regs->rip < PAGE_OFFSET)
  459. goto bad;
  460. for (i=0; i<20; i++) {
  461. unsigned char c;
  462. if (__get_user(c, &((unsigned char*)regs->rip)[i])) {
  463. bad:
  464. printk(" Bad RIP value.");
  465. break;
  466. }
  467. printk("%02x ", c);
  468. }
  469. }
  470. printk("\n");
  471. }
  472. void handle_BUG(struct pt_regs *regs)
  473. {
  474. struct bug_frame f;
  475. long len;
  476. const char *prefix = "";
  477. if (user_mode(regs))
  478. return;
  479. if (__copy_from_user(&f, (const void __user *) regs->rip,
  480. sizeof(struct bug_frame)))
  481. return;
  482. if (f.filename >= 0 ||
  483. f.ud2[0] != 0x0f || f.ud2[1] != 0x0b)
  484. return;
  485. len = __strnlen_user((char *)(long)f.filename, PATH_MAX) - 1;
  486. if (len < 0 || len >= PATH_MAX)
  487. f.filename = (int)(long)"unmapped filename";
  488. else if (len > 50) {
  489. f.filename += len - 50;
  490. prefix = "...";
  491. }
  492. printk("----------- [cut here ] --------- [please bite here ] ---------\n");
  493. printk(KERN_ALERT "Kernel BUG at %s%.50s:%d\n", prefix, (char *)(long)f.filename, f.line);
  494. }
  495. #ifdef CONFIG_BUG
  496. void out_of_line_bug(void)
  497. {
  498. BUG();
  499. }
  500. EXPORT_SYMBOL(out_of_line_bug);
  501. #endif
  502. static DEFINE_SPINLOCK(die_lock);
  503. static int die_owner = -1;
  504. static unsigned int die_nest_count;
  505. unsigned __kprobes long oops_begin(void)
  506. {
  507. int cpu = safe_smp_processor_id();
  508. unsigned long flags;
  509. oops_enter();
  510. /* racy, but better than risking deadlock. */
  511. local_irq_save(flags);
  512. if (!spin_trylock(&die_lock)) {
  513. if (cpu == die_owner)
  514. /* nested oops. should stop eventually */;
  515. else
  516. spin_lock(&die_lock);
  517. }
  518. die_nest_count++;
  519. die_owner = cpu;
  520. console_verbose();
  521. bust_spinlocks(1);
  522. return flags;
  523. }
  524. void __kprobes oops_end(unsigned long flags)
  525. {
  526. die_owner = -1;
  527. bust_spinlocks(0);
  528. die_nest_count--;
  529. if (die_nest_count)
  530. /* We still own the lock */
  531. local_irq_restore(flags);
  532. else
  533. /* Nest count reaches zero, release the lock. */
  534. spin_unlock_irqrestore(&die_lock, flags);
  535. if (panic_on_oops)
  536. panic("Fatal exception");
  537. oops_exit();
  538. }
  539. void __kprobes __die(const char * str, struct pt_regs * regs, long err)
  540. {
  541. static int die_counter;
  542. printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
  543. #ifdef CONFIG_PREEMPT
  544. printk("PREEMPT ");
  545. #endif
  546. #ifdef CONFIG_SMP
  547. printk("SMP ");
  548. #endif
  549. #ifdef CONFIG_DEBUG_PAGEALLOC
  550. printk("DEBUG_PAGEALLOC");
  551. #endif
  552. printk("\n");
  553. notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV);
  554. show_registers(regs);
  555. /* Executive summary in case the oops scrolled away */
  556. printk(KERN_ALERT "RIP ");
  557. printk_address(regs->rip);
  558. printk(" RSP <%016lx>\n", regs->rsp);
  559. if (kexec_should_crash(current))
  560. crash_kexec(regs);
  561. }
  562. void die(const char * str, struct pt_regs * regs, long err)
  563. {
  564. unsigned long flags = oops_begin();
  565. handle_BUG(regs);
  566. __die(str, regs, err);
  567. oops_end(flags);
  568. do_exit(SIGSEGV);
  569. }
  570. void __kprobes die_nmi(char *str, struct pt_regs *regs, int do_panic)
  571. {
  572. unsigned long flags = oops_begin();
  573. /*
  574. * We are in trouble anyway, lets at least try
  575. * to get a message out.
  576. */
  577. printk(str, safe_smp_processor_id());
  578. show_registers(regs);
  579. if (kexec_should_crash(current))
  580. crash_kexec(regs);
  581. if (do_panic || panic_on_oops)
  582. panic("Non maskable interrupt");
  583. oops_end(flags);
  584. nmi_exit();
  585. local_irq_enable();
  586. do_exit(SIGSEGV);
  587. }
  588. static void __kprobes do_trap(int trapnr, int signr, char *str,
  589. struct pt_regs * regs, long error_code,
  590. siginfo_t *info)
  591. {
  592. struct task_struct *tsk = current;
  593. tsk->thread.error_code = error_code;
  594. tsk->thread.trap_no = trapnr;
  595. if (user_mode(regs)) {
  596. if (exception_trace && unhandled_signal(tsk, signr))
  597. printk(KERN_INFO
  598. "%s[%d] trap %s rip:%lx rsp:%lx error:%lx\n",
  599. tsk->comm, tsk->pid, str,
  600. regs->rip, regs->rsp, error_code);
  601. if (info)
  602. force_sig_info(signr, info, tsk);
  603. else
  604. force_sig(signr, tsk);
  605. return;
  606. }
  607. /* kernel trap */
  608. {
  609. const struct exception_table_entry *fixup;
  610. fixup = search_exception_tables(regs->rip);
  611. if (fixup)
  612. regs->rip = fixup->fixup;
  613. else
  614. die(str, regs, error_code);
  615. return;
  616. }
  617. }
  618. #define DO_ERROR(trapnr, signr, str, name) \
  619. asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
  620. { \
  621. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
  622. == NOTIFY_STOP) \
  623. return; \
  624. conditional_sti(regs); \
  625. do_trap(trapnr, signr, str, regs, error_code, NULL); \
  626. }
  627. #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
  628. asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
  629. { \
  630. siginfo_t info; \
  631. info.si_signo = signr; \
  632. info.si_errno = 0; \
  633. info.si_code = sicode; \
  634. info.si_addr = (void __user *)siaddr; \
  635. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
  636. == NOTIFY_STOP) \
  637. return; \
  638. conditional_sti(regs); \
  639. do_trap(trapnr, signr, str, regs, error_code, &info); \
  640. }
  641. DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->rip)
  642. DO_ERROR( 4, SIGSEGV, "overflow", overflow)
  643. DO_ERROR( 5, SIGSEGV, "bounds", bounds)
  644. DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->rip)
  645. DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
  646. DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
  647. DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
  648. DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
  649. DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
  650. DO_ERROR(18, SIGSEGV, "reserved", reserved)
  651. /* Runs on IST stack */
  652. asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
  653. {
  654. if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
  655. 12, SIGBUS) == NOTIFY_STOP)
  656. return;
  657. preempt_conditional_sti(regs);
  658. do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
  659. preempt_conditional_cli(regs);
  660. }
  661. asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
  662. {
  663. static const char str[] = "double fault";
  664. struct task_struct *tsk = current;
  665. /* Return not checked because double check cannot be ignored */
  666. notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
  667. tsk->thread.error_code = error_code;
  668. tsk->thread.trap_no = 8;
  669. /* This is always a kernel trap and never fixable (and thus must
  670. never return). */
  671. for (;;)
  672. die(str, regs, error_code);
  673. }
  674. asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
  675. long error_code)
  676. {
  677. struct task_struct *tsk = current;
  678. conditional_sti(regs);
  679. tsk->thread.error_code = error_code;
  680. tsk->thread.trap_no = 13;
  681. if (user_mode(regs)) {
  682. if (exception_trace && unhandled_signal(tsk, SIGSEGV))
  683. printk(KERN_INFO
  684. "%s[%d] general protection rip:%lx rsp:%lx error:%lx\n",
  685. tsk->comm, tsk->pid,
  686. regs->rip, regs->rsp, error_code);
  687. force_sig(SIGSEGV, tsk);
  688. return;
  689. }
  690. /* kernel gp */
  691. {
  692. const struct exception_table_entry *fixup;
  693. fixup = search_exception_tables(regs->rip);
  694. if (fixup) {
  695. regs->rip = fixup->fixup;
  696. return;
  697. }
  698. if (notify_die(DIE_GPF, "general protection fault", regs,
  699. error_code, 13, SIGSEGV) == NOTIFY_STOP)
  700. return;
  701. die("general protection fault", regs, error_code);
  702. }
  703. }
  704. static __kprobes void
  705. mem_parity_error(unsigned char reason, struct pt_regs * regs)
  706. {
  707. printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
  708. reason);
  709. printk(KERN_EMERG "You probably have a hardware problem with your "
  710. "RAM chips\n");
  711. if (panic_on_unrecovered_nmi)
  712. panic("NMI: Not continuing");
  713. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  714. /* Clear and disable the memory parity error line. */
  715. reason = (reason & 0xf) | 4;
  716. outb(reason, 0x61);
  717. }
  718. static __kprobes void
  719. io_check_error(unsigned char reason, struct pt_regs * regs)
  720. {
  721. printk("NMI: IOCK error (debug interrupt?)\n");
  722. show_registers(regs);
  723. /* Re-enable the IOCK line, wait for a few seconds */
  724. reason = (reason & 0xf) | 8;
  725. outb(reason, 0x61);
  726. mdelay(2000);
  727. reason &= ~8;
  728. outb(reason, 0x61);
  729. }
  730. static __kprobes void
  731. unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
  732. {
  733. printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
  734. reason);
  735. printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
  736. if (panic_on_unrecovered_nmi)
  737. panic("NMI: Not continuing");
  738. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  739. }
  740. /* Runs on IST stack. This code must keep interrupts off all the time.
  741. Nested NMIs are prevented by the CPU. */
  742. asmlinkage __kprobes void default_do_nmi(struct pt_regs *regs)
  743. {
  744. unsigned char reason = 0;
  745. int cpu;
  746. cpu = smp_processor_id();
  747. /* Only the BSP gets external NMIs from the system. */
  748. if (!cpu)
  749. reason = get_nmi_reason();
  750. if (!(reason & 0xc0)) {
  751. if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
  752. == NOTIFY_STOP)
  753. return;
  754. /*
  755. * Ok, so this is none of the documented NMI sources,
  756. * so it must be the NMI watchdog.
  757. */
  758. if (nmi_watchdog_tick(regs,reason))
  759. return;
  760. if (!do_nmi_callback(regs,cpu))
  761. unknown_nmi_error(reason, regs);
  762. return;
  763. }
  764. if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
  765. return;
  766. /* AK: following checks seem to be broken on modern chipsets. FIXME */
  767. if (reason & 0x80)
  768. mem_parity_error(reason, regs);
  769. if (reason & 0x40)
  770. io_check_error(reason, regs);
  771. }
  772. /* runs on IST stack. */
  773. asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code)
  774. {
  775. if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
  776. return;
  777. }
  778. preempt_conditional_sti(regs);
  779. do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
  780. preempt_conditional_cli(regs);
  781. }
  782. /* Help handler running on IST stack to switch back to user stack
  783. for scheduling or signal handling. The actual stack switch is done in
  784. entry.S */
  785. asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
  786. {
  787. struct pt_regs *regs = eregs;
  788. /* Did already sync */
  789. if (eregs == (struct pt_regs *)eregs->rsp)
  790. ;
  791. /* Exception from user space */
  792. else if (user_mode(eregs))
  793. regs = task_pt_regs(current);
  794. /* Exception from kernel and interrupts are enabled. Move to
  795. kernel process stack. */
  796. else if (eregs->eflags & X86_EFLAGS_IF)
  797. regs = (struct pt_regs *)(eregs->rsp -= sizeof(struct pt_regs));
  798. if (eregs != regs)
  799. *regs = *eregs;
  800. return regs;
  801. }
  802. /* runs on IST stack. */
  803. asmlinkage void __kprobes do_debug(struct pt_regs * regs,
  804. unsigned long error_code)
  805. {
  806. unsigned long condition;
  807. struct task_struct *tsk = current;
  808. siginfo_t info;
  809. get_debugreg(condition, 6);
  810. if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
  811. SIGTRAP) == NOTIFY_STOP)
  812. return;
  813. preempt_conditional_sti(regs);
  814. /* Mask out spurious debug traps due to lazy DR7 setting */
  815. if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
  816. if (!tsk->thread.debugreg7) {
  817. goto clear_dr7;
  818. }
  819. }
  820. tsk->thread.debugreg6 = condition;
  821. /* Mask out spurious TF errors due to lazy TF clearing */
  822. if (condition & DR_STEP) {
  823. /*
  824. * The TF error should be masked out only if the current
  825. * process is not traced and if the TRAP flag has been set
  826. * previously by a tracing process (condition detected by
  827. * the PT_DTRACE flag); remember that the i386 TRAP flag
  828. * can be modified by the process itself in user mode,
  829. * allowing programs to debug themselves without the ptrace()
  830. * interface.
  831. */
  832. if (!user_mode(regs))
  833. goto clear_TF_reenable;
  834. /*
  835. * Was the TF flag set by a debugger? If so, clear it now,
  836. * so that register information is correct.
  837. */
  838. if (tsk->ptrace & PT_DTRACE) {
  839. regs->eflags &= ~TF_MASK;
  840. tsk->ptrace &= ~PT_DTRACE;
  841. }
  842. }
  843. /* Ok, finally something we can handle */
  844. tsk->thread.trap_no = 1;
  845. tsk->thread.error_code = error_code;
  846. info.si_signo = SIGTRAP;
  847. info.si_errno = 0;
  848. info.si_code = TRAP_BRKPT;
  849. info.si_addr = user_mode(regs) ? (void __user *)regs->rip : NULL;
  850. force_sig_info(SIGTRAP, &info, tsk);
  851. clear_dr7:
  852. set_debugreg(0UL, 7);
  853. preempt_conditional_cli(regs);
  854. return;
  855. clear_TF_reenable:
  856. set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
  857. regs->eflags &= ~TF_MASK;
  858. preempt_conditional_cli(regs);
  859. }
  860. static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
  861. {
  862. const struct exception_table_entry *fixup;
  863. fixup = search_exception_tables(regs->rip);
  864. if (fixup) {
  865. regs->rip = fixup->fixup;
  866. return 1;
  867. }
  868. notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
  869. /* Illegal floating point operation in the kernel */
  870. current->thread.trap_no = trapnr;
  871. die(str, regs, 0);
  872. return 0;
  873. }
  874. /*
  875. * Note that we play around with the 'TS' bit in an attempt to get
  876. * the correct behaviour even in the presence of the asynchronous
  877. * IRQ13 behaviour
  878. */
  879. asmlinkage void do_coprocessor_error(struct pt_regs *regs)
  880. {
  881. void __user *rip = (void __user *)(regs->rip);
  882. struct task_struct * task;
  883. siginfo_t info;
  884. unsigned short cwd, swd;
  885. conditional_sti(regs);
  886. if (!user_mode(regs) &&
  887. kernel_math_error(regs, "kernel x87 math error", 16))
  888. return;
  889. /*
  890. * Save the info for the exception handler and clear the error.
  891. */
  892. task = current;
  893. save_init_fpu(task);
  894. task->thread.trap_no = 16;
  895. task->thread.error_code = 0;
  896. info.si_signo = SIGFPE;
  897. info.si_errno = 0;
  898. info.si_code = __SI_FAULT;
  899. info.si_addr = rip;
  900. /*
  901. * (~cwd & swd) will mask out exceptions that are not set to unmasked
  902. * status. 0x3f is the exception bits in these regs, 0x200 is the
  903. * C1 reg you need in case of a stack fault, 0x040 is the stack
  904. * fault bit. We should only be taking one exception at a time,
  905. * so if this combination doesn't produce any single exception,
  906. * then we have a bad program that isn't synchronizing its FPU usage
  907. * and it will suffer the consequences since we won't be able to
  908. * fully reproduce the context of the exception
  909. */
  910. cwd = get_fpu_cwd(task);
  911. swd = get_fpu_swd(task);
  912. switch (swd & ~cwd & 0x3f) {
  913. case 0x000:
  914. default:
  915. break;
  916. case 0x001: /* Invalid Op */
  917. /*
  918. * swd & 0x240 == 0x040: Stack Underflow
  919. * swd & 0x240 == 0x240: Stack Overflow
  920. * User must clear the SF bit (0x40) if set
  921. */
  922. info.si_code = FPE_FLTINV;
  923. break;
  924. case 0x002: /* Denormalize */
  925. case 0x010: /* Underflow */
  926. info.si_code = FPE_FLTUND;
  927. break;
  928. case 0x004: /* Zero Divide */
  929. info.si_code = FPE_FLTDIV;
  930. break;
  931. case 0x008: /* Overflow */
  932. info.si_code = FPE_FLTOVF;
  933. break;
  934. case 0x020: /* Precision */
  935. info.si_code = FPE_FLTRES;
  936. break;
  937. }
  938. force_sig_info(SIGFPE, &info, task);
  939. }
  940. asmlinkage void bad_intr(void)
  941. {
  942. printk("bad interrupt");
  943. }
  944. asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
  945. {
  946. void __user *rip = (void __user *)(regs->rip);
  947. struct task_struct * task;
  948. siginfo_t info;
  949. unsigned short mxcsr;
  950. conditional_sti(regs);
  951. if (!user_mode(regs) &&
  952. kernel_math_error(regs, "kernel simd math error", 19))
  953. return;
  954. /*
  955. * Save the info for the exception handler and clear the error.
  956. */
  957. task = current;
  958. save_init_fpu(task);
  959. task->thread.trap_no = 19;
  960. task->thread.error_code = 0;
  961. info.si_signo = SIGFPE;
  962. info.si_errno = 0;
  963. info.si_code = __SI_FAULT;
  964. info.si_addr = rip;
  965. /*
  966. * The SIMD FPU exceptions are handled a little differently, as there
  967. * is only a single status/control register. Thus, to determine which
  968. * unmasked exception was caught we must mask the exception mask bits
  969. * at 0x1f80, and then use these to mask the exception bits at 0x3f.
  970. */
  971. mxcsr = get_fpu_mxcsr(task);
  972. switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
  973. case 0x000:
  974. default:
  975. break;
  976. case 0x001: /* Invalid Op */
  977. info.si_code = FPE_FLTINV;
  978. break;
  979. case 0x002: /* Denormalize */
  980. case 0x010: /* Underflow */
  981. info.si_code = FPE_FLTUND;
  982. break;
  983. case 0x004: /* Zero Divide */
  984. info.si_code = FPE_FLTDIV;
  985. break;
  986. case 0x008: /* Overflow */
  987. info.si_code = FPE_FLTOVF;
  988. break;
  989. case 0x020: /* Precision */
  990. info.si_code = FPE_FLTRES;
  991. break;
  992. }
  993. force_sig_info(SIGFPE, &info, task);
  994. }
  995. asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
  996. {
  997. }
  998. asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
  999. {
  1000. }
  1001. asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
  1002. {
  1003. }
  1004. /*
  1005. * 'math_state_restore()' saves the current math information in the
  1006. * old math state array, and gets the new ones from the current task
  1007. *
  1008. * Careful.. There are problems with IBM-designed IRQ13 behaviour.
  1009. * Don't touch unless you *really* know how it works.
  1010. */
  1011. asmlinkage void math_state_restore(void)
  1012. {
  1013. struct task_struct *me = current;
  1014. clts(); /* Allow maths ops (or we recurse) */
  1015. if (!used_math())
  1016. init_fpu(me);
  1017. restore_fpu_checking(&me->thread.i387.fxsave);
  1018. task_thread_info(me)->status |= TS_USEDFPU;
  1019. me->fpu_counter++;
  1020. }
  1021. void __init trap_init(void)
  1022. {
  1023. set_intr_gate(0,&divide_error);
  1024. set_intr_gate_ist(1,&debug,DEBUG_STACK);
  1025. set_intr_gate_ist(2,&nmi,NMI_STACK);
  1026. set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */
  1027. set_system_gate(4,&overflow); /* int4 can be called from all */
  1028. set_intr_gate(5,&bounds);
  1029. set_intr_gate(6,&invalid_op);
  1030. set_intr_gate(7,&device_not_available);
  1031. set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
  1032. set_intr_gate(9,&coprocessor_segment_overrun);
  1033. set_intr_gate(10,&invalid_TSS);
  1034. set_intr_gate(11,&segment_not_present);
  1035. set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
  1036. set_intr_gate(13,&general_protection);
  1037. set_intr_gate(14,&page_fault);
  1038. set_intr_gate(15,&spurious_interrupt_bug);
  1039. set_intr_gate(16,&coprocessor_error);
  1040. set_intr_gate(17,&alignment_check);
  1041. #ifdef CONFIG_X86_MCE
  1042. set_intr_gate_ist(18,&machine_check, MCE_STACK);
  1043. #endif
  1044. set_intr_gate(19,&simd_coprocessor_error);
  1045. #ifdef CONFIG_IA32_EMULATION
  1046. set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
  1047. #endif
  1048. /*
  1049. * Should be a barrier for any external CPU state.
  1050. */
  1051. cpu_init();
  1052. }
  1053. static int __init oops_setup(char *s)
  1054. {
  1055. if (!s)
  1056. return -EINVAL;
  1057. if (!strcmp(s, "panic"))
  1058. panic_on_oops = 1;
  1059. return 0;
  1060. }
  1061. early_param("oops", oops_setup);
  1062. static int __init kstack_setup(char *s)
  1063. {
  1064. if (!s)
  1065. return -EINVAL;
  1066. kstack_depth_to_print = simple_strtoul(s,NULL,0);
  1067. return 0;
  1068. }
  1069. early_param("kstack", kstack_setup);
  1070. #ifdef CONFIG_STACK_UNWIND
  1071. static int __init call_trace_setup(char *s)
  1072. {
  1073. if (!s)
  1074. return -EINVAL;
  1075. if (strcmp(s, "old") == 0)
  1076. call_trace = -1;
  1077. else if (strcmp(s, "both") == 0)
  1078. call_trace = 0;
  1079. else if (strcmp(s, "newfallback") == 0)
  1080. call_trace = 1;
  1081. else if (strcmp(s, "new") == 0)
  1082. call_trace = 2;
  1083. return 0;
  1084. }
  1085. early_param("call_trace", call_trace_setup);
  1086. #endif