traps.c 29 KB

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