traps.c 30 KB

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