traps.c 29 KB

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