traps.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  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 <linux/bug.h>
  34. #include <linux/kdebug.h>
  35. #if defined(CONFIG_EDAC)
  36. #include <linux/edac.h>
  37. #endif
  38. #include <asm/system.h>
  39. #include <asm/io.h>
  40. #include <asm/atomic.h>
  41. #include <asm/debugreg.h>
  42. #include <asm/desc.h>
  43. #include <asm/i387.h>
  44. #include <asm/processor.h>
  45. #include <asm/unwind.h>
  46. #include <asm/smp.h>
  47. #include <asm/pgalloc.h>
  48. #include <asm/pda.h>
  49. #include <asm/proto.h>
  50. #include <asm/nmi.h>
  51. #include <asm/stacktrace.h>
  52. asmlinkage void divide_error(void);
  53. asmlinkage void debug(void);
  54. asmlinkage void nmi(void);
  55. asmlinkage void int3(void);
  56. asmlinkage void overflow(void);
  57. asmlinkage void bounds(void);
  58. asmlinkage void invalid_op(void);
  59. asmlinkage void device_not_available(void);
  60. asmlinkage void double_fault(void);
  61. asmlinkage void coprocessor_segment_overrun(void);
  62. asmlinkage void invalid_TSS(void);
  63. asmlinkage void segment_not_present(void);
  64. asmlinkage void stack_segment(void);
  65. asmlinkage void general_protection(void);
  66. asmlinkage void page_fault(void);
  67. asmlinkage void coprocessor_error(void);
  68. asmlinkage void simd_coprocessor_error(void);
  69. asmlinkage void reserved(void);
  70. asmlinkage void alignment_check(void);
  71. asmlinkage void machine_check(void);
  72. asmlinkage void spurious_interrupt_bug(void);
  73. static inline void conditional_sti(struct pt_regs *regs)
  74. {
  75. if (regs->eflags & X86_EFLAGS_IF)
  76. local_irq_enable();
  77. }
  78. static inline void preempt_conditional_sti(struct pt_regs *regs)
  79. {
  80. preempt_disable();
  81. if (regs->eflags & X86_EFLAGS_IF)
  82. local_irq_enable();
  83. }
  84. static inline void preempt_conditional_cli(struct pt_regs *regs)
  85. {
  86. if (regs->eflags & X86_EFLAGS_IF)
  87. local_irq_disable();
  88. /* Make sure to not schedule here because we could be running
  89. on an exception stack. */
  90. preempt_enable_no_resched();
  91. }
  92. int kstack_depth_to_print = 12;
  93. #ifdef CONFIG_KALLSYMS
  94. void printk_address(unsigned long address)
  95. {
  96. unsigned long offset = 0, symsize;
  97. const char *symname;
  98. char *modname;
  99. char *delim = ":";
  100. char namebuf[128];
  101. symname = kallsyms_lookup(address, &symsize, &offset,
  102. &modname, namebuf);
  103. if (!symname) {
  104. printk(" [<%016lx>]\n", address);
  105. return;
  106. }
  107. if (!modname)
  108. modname = delim = "";
  109. printk(" [<%016lx>] %s%s%s%s+0x%lx/0x%lx\n",
  110. address, delim, modname, delim, symname, offset, symsize);
  111. }
  112. #else
  113. void printk_address(unsigned long address)
  114. {
  115. printk(" [<%016lx>]\n", address);
  116. }
  117. #endif
  118. static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
  119. unsigned *usedp, char **idp)
  120. {
  121. static char ids[][8] = {
  122. [DEBUG_STACK - 1] = "#DB",
  123. [NMI_STACK - 1] = "NMI",
  124. [DOUBLEFAULT_STACK - 1] = "#DF",
  125. [STACKFAULT_STACK - 1] = "#SS",
  126. [MCE_STACK - 1] = "#MC",
  127. #if DEBUG_STKSZ > EXCEPTION_STKSZ
  128. [N_EXCEPTION_STACKS ... N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
  129. #endif
  130. };
  131. unsigned k;
  132. /*
  133. * Iterate over all exception stacks, and figure out whether
  134. * 'stack' is in one of them:
  135. */
  136. for (k = 0; k < N_EXCEPTION_STACKS; k++) {
  137. unsigned long end = per_cpu(orig_ist, cpu).ist[k];
  138. /*
  139. * Is 'stack' above this exception frame's end?
  140. * If yes then skip to the next frame.
  141. */
  142. if (stack >= end)
  143. continue;
  144. /*
  145. * Is 'stack' above this exception frame's start address?
  146. * If yes then we found the right frame.
  147. */
  148. if (stack >= end - EXCEPTION_STKSZ) {
  149. /*
  150. * Make sure we only iterate through an exception
  151. * stack once. If it comes up for the second time
  152. * then there's something wrong going on - just
  153. * break out and return NULL:
  154. */
  155. if (*usedp & (1U << k))
  156. break;
  157. *usedp |= 1U << k;
  158. *idp = ids[k];
  159. return (unsigned long *)end;
  160. }
  161. /*
  162. * If this is a debug stack, and if it has a larger size than
  163. * the usual exception stacks, then 'stack' might still
  164. * be within the lower portion of the debug stack:
  165. */
  166. #if DEBUG_STKSZ > EXCEPTION_STKSZ
  167. if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
  168. unsigned j = N_EXCEPTION_STACKS - 1;
  169. /*
  170. * Black magic. A large debug stack is composed of
  171. * multiple exception stack entries, which we
  172. * iterate through now. Dont look:
  173. */
  174. do {
  175. ++j;
  176. end -= EXCEPTION_STKSZ;
  177. ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
  178. } while (stack < end - EXCEPTION_STKSZ);
  179. if (*usedp & (1U << j))
  180. break;
  181. *usedp |= 1U << j;
  182. *idp = ids[j];
  183. return (unsigned long *)end;
  184. }
  185. #endif
  186. }
  187. return NULL;
  188. }
  189. #define MSG(txt) ops->warning(data, txt)
  190. /*
  191. * x86-64 can have upto three kernel stacks:
  192. * process stack
  193. * interrupt stack
  194. * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
  195. */
  196. static inline int valid_stack_ptr(struct thread_info *tinfo, void *p)
  197. {
  198. void *t = (void *)tinfo;
  199. return p > t && p < t + THREAD_SIZE - 3;
  200. }
  201. void dump_trace(struct task_struct *tsk, struct pt_regs *regs,
  202. unsigned long *stack,
  203. struct stacktrace_ops *ops, void *data)
  204. {
  205. const unsigned cpu = get_cpu();
  206. unsigned long *irqstack_end = (unsigned long*)cpu_pda(cpu)->irqstackptr;
  207. unsigned used = 0;
  208. struct thread_info *tinfo;
  209. if (!tsk)
  210. tsk = current;
  211. if (!stack) {
  212. unsigned long dummy;
  213. stack = &dummy;
  214. if (tsk && tsk != current)
  215. stack = (unsigned long *)tsk->thread.rsp;
  216. }
  217. /*
  218. * Print function call entries within a stack. 'cond' is the
  219. * "end of stackframe" condition, that the 'stack++'
  220. * iteration will eventually trigger.
  221. */
  222. #define HANDLE_STACK(cond) \
  223. do while (cond) { \
  224. unsigned long addr = *stack++; \
  225. /* Use unlocked access here because except for NMIs \
  226. we should be already protected against module unloads */ \
  227. if (__kernel_text_address(addr)) { \
  228. /* \
  229. * If the address is either in the text segment of the \
  230. * kernel, or in the region which contains vmalloc'ed \
  231. * memory, it *may* be the address of a calling \
  232. * routine; if so, print it so that someone tracing \
  233. * down the cause of the crash will be able to figure \
  234. * out the call path that was taken. \
  235. */ \
  236. ops->address(data, addr); \
  237. } \
  238. } while (0)
  239. /*
  240. * Print function call entries in all stacks, starting at the
  241. * current stack address. If the stacks consist of nested
  242. * exceptions
  243. */
  244. for (;;) {
  245. char *id;
  246. unsigned long *estack_end;
  247. estack_end = in_exception_stack(cpu, (unsigned long)stack,
  248. &used, &id);
  249. if (estack_end) {
  250. if (ops->stack(data, id) < 0)
  251. break;
  252. HANDLE_STACK (stack < estack_end);
  253. ops->stack(data, "<EOE>");
  254. /*
  255. * We link to the next stack via the
  256. * second-to-last pointer (index -2 to end) in the
  257. * exception stack:
  258. */
  259. stack = (unsigned long *) estack_end[-2];
  260. continue;
  261. }
  262. if (irqstack_end) {
  263. unsigned long *irqstack;
  264. irqstack = irqstack_end -
  265. (IRQSTACKSIZE - 64) / sizeof(*irqstack);
  266. if (stack >= irqstack && stack < irqstack_end) {
  267. if (ops->stack(data, "IRQ") < 0)
  268. break;
  269. HANDLE_STACK (stack < irqstack_end);
  270. /*
  271. * We link to the next stack (which would be
  272. * the process stack normally) the last
  273. * pointer (index -1 to end) in the IRQ stack:
  274. */
  275. stack = (unsigned long *) (irqstack_end[-1]);
  276. irqstack_end = NULL;
  277. ops->stack(data, "EOI");
  278. continue;
  279. }
  280. }
  281. break;
  282. }
  283. /*
  284. * This handles the process stack:
  285. */
  286. tinfo = task_thread_info(tsk);
  287. HANDLE_STACK (valid_stack_ptr(tinfo, stack));
  288. #undef HANDLE_STACK
  289. put_cpu();
  290. }
  291. EXPORT_SYMBOL(dump_trace);
  292. static void
  293. print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
  294. {
  295. print_symbol(msg, symbol);
  296. printk("\n");
  297. }
  298. static void print_trace_warning(void *data, char *msg)
  299. {
  300. printk("%s\n", msg);
  301. }
  302. static int print_trace_stack(void *data, char *name)
  303. {
  304. printk(" <%s> ", name);
  305. return 0;
  306. }
  307. static void print_trace_address(void *data, unsigned long addr)
  308. {
  309. touch_nmi_watchdog();
  310. printk_address(addr);
  311. }
  312. static struct stacktrace_ops print_trace_ops = {
  313. .warning = print_trace_warning,
  314. .warning_symbol = print_trace_warning_symbol,
  315. .stack = print_trace_stack,
  316. .address = print_trace_address,
  317. };
  318. void
  319. show_trace(struct task_struct *tsk, struct pt_regs *regs, unsigned long *stack)
  320. {
  321. printk("\nCall Trace:\n");
  322. dump_trace(tsk, regs, stack, &print_trace_ops, NULL);
  323. printk("\n");
  324. }
  325. static void
  326. _show_stack(struct task_struct *tsk, struct pt_regs *regs, unsigned long *rsp)
  327. {
  328. unsigned long *stack;
  329. int i;
  330. const int cpu = smp_processor_id();
  331. unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
  332. unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
  333. // debugging aid: "show_stack(NULL, NULL);" prints the
  334. // back trace for this cpu.
  335. if (rsp == NULL) {
  336. if (tsk)
  337. rsp = (unsigned long *)tsk->thread.rsp;
  338. else
  339. rsp = (unsigned long *)&rsp;
  340. }
  341. stack = rsp;
  342. for(i=0; i < kstack_depth_to_print; i++) {
  343. if (stack >= irqstack && stack <= irqstack_end) {
  344. if (stack == irqstack_end) {
  345. stack = (unsigned long *) (irqstack_end[-1]);
  346. printk(" <EOI> ");
  347. }
  348. } else {
  349. if (((long) stack & (THREAD_SIZE-1)) == 0)
  350. break;
  351. }
  352. if (i && ((i % 4) == 0))
  353. printk("\n");
  354. printk(" %016lx", *stack++);
  355. touch_nmi_watchdog();
  356. }
  357. show_trace(tsk, regs, rsp);
  358. }
  359. void show_stack(struct task_struct *tsk, unsigned long * rsp)
  360. {
  361. _show_stack(tsk, NULL, rsp);
  362. }
  363. /*
  364. * The architecture-independent dump_stack generator
  365. */
  366. void dump_stack(void)
  367. {
  368. unsigned long dummy;
  369. show_trace(NULL, NULL, &dummy);
  370. }
  371. EXPORT_SYMBOL(dump_stack);
  372. void show_registers(struct pt_regs *regs)
  373. {
  374. int i;
  375. int in_kernel = !user_mode(regs);
  376. unsigned long rsp;
  377. const int cpu = smp_processor_id();
  378. struct task_struct *cur = cpu_pda(cpu)->pcurrent;
  379. rsp = regs->rsp;
  380. printk("CPU %d ", cpu);
  381. __show_regs(regs);
  382. printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
  383. cur->comm, cur->pid, task_thread_info(cur), cur);
  384. /*
  385. * When in-kernel, we also print out the stack and code at the
  386. * time of the fault..
  387. */
  388. if (in_kernel) {
  389. printk("Stack: ");
  390. _show_stack(NULL, regs, (unsigned long*)rsp);
  391. printk("\nCode: ");
  392. if (regs->rip < PAGE_OFFSET)
  393. goto bad;
  394. for (i=0; i<20; i++) {
  395. unsigned char c;
  396. if (__get_user(c, &((unsigned char*)regs->rip)[i])) {
  397. bad:
  398. printk(" Bad RIP value.");
  399. break;
  400. }
  401. printk("%02x ", c);
  402. }
  403. }
  404. printk("\n");
  405. }
  406. int is_valid_bugaddr(unsigned long rip)
  407. {
  408. unsigned short ud2;
  409. if (__copy_from_user(&ud2, (const void __user *) rip, sizeof(ud2)))
  410. return 0;
  411. return ud2 == 0x0b0f;
  412. }
  413. #ifdef CONFIG_BUG
  414. void out_of_line_bug(void)
  415. {
  416. BUG();
  417. }
  418. EXPORT_SYMBOL(out_of_line_bug);
  419. #endif
  420. static DEFINE_SPINLOCK(die_lock);
  421. static int die_owner = -1;
  422. static unsigned int die_nest_count;
  423. unsigned __kprobes long oops_begin(void)
  424. {
  425. int cpu;
  426. unsigned long flags;
  427. oops_enter();
  428. /* racy, but better than risking deadlock. */
  429. local_irq_save(flags);
  430. cpu = smp_processor_id();
  431. if (!spin_trylock(&die_lock)) {
  432. if (cpu == die_owner)
  433. /* nested oops. should stop eventually */;
  434. else
  435. spin_lock(&die_lock);
  436. }
  437. die_nest_count++;
  438. die_owner = cpu;
  439. console_verbose();
  440. bust_spinlocks(1);
  441. return flags;
  442. }
  443. void __kprobes oops_end(unsigned long flags)
  444. {
  445. die_owner = -1;
  446. bust_spinlocks(0);
  447. die_nest_count--;
  448. if (die_nest_count)
  449. /* We still own the lock */
  450. local_irq_restore(flags);
  451. else
  452. /* Nest count reaches zero, release the lock. */
  453. spin_unlock_irqrestore(&die_lock, flags);
  454. if (panic_on_oops)
  455. panic("Fatal exception");
  456. oops_exit();
  457. }
  458. void __kprobes __die(const char * str, struct pt_regs * regs, long err)
  459. {
  460. static int die_counter;
  461. printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
  462. #ifdef CONFIG_PREEMPT
  463. printk("PREEMPT ");
  464. #endif
  465. #ifdef CONFIG_SMP
  466. printk("SMP ");
  467. #endif
  468. #ifdef CONFIG_DEBUG_PAGEALLOC
  469. printk("DEBUG_PAGEALLOC");
  470. #endif
  471. printk("\n");
  472. notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV);
  473. show_registers(regs);
  474. add_taint(TAINT_DIE);
  475. /* Executive summary in case the oops scrolled away */
  476. printk(KERN_ALERT "RIP ");
  477. printk_address(regs->rip);
  478. printk(" RSP <%016lx>\n", regs->rsp);
  479. if (kexec_should_crash(current))
  480. crash_kexec(regs);
  481. }
  482. void die(const char * str, struct pt_regs * regs, long err)
  483. {
  484. unsigned long flags = oops_begin();
  485. if (!user_mode(regs))
  486. report_bug(regs->rip, regs);
  487. __die(str, regs, err);
  488. oops_end(flags);
  489. do_exit(SIGSEGV);
  490. }
  491. void __kprobes die_nmi(char *str, struct pt_regs *regs, int do_panic)
  492. {
  493. unsigned long flags = oops_begin();
  494. /*
  495. * We are in trouble anyway, lets at least try
  496. * to get a message out.
  497. */
  498. printk(str, smp_processor_id());
  499. show_registers(regs);
  500. if (kexec_should_crash(current))
  501. crash_kexec(regs);
  502. if (do_panic || panic_on_oops)
  503. panic("Non maskable interrupt");
  504. oops_end(flags);
  505. nmi_exit();
  506. local_irq_enable();
  507. do_exit(SIGSEGV);
  508. }
  509. static void __kprobes do_trap(int trapnr, int signr, char *str,
  510. struct pt_regs * regs, long error_code,
  511. siginfo_t *info)
  512. {
  513. struct task_struct *tsk = current;
  514. if (user_mode(regs)) {
  515. /*
  516. * We want error_code and trap_no set for userspace
  517. * faults and kernelspace faults which result in
  518. * die(), but not kernelspace faults which are fixed
  519. * up. die() gives the process no chance to handle
  520. * the signal and notice the kernel fault information,
  521. * so that won't result in polluting the information
  522. * about previously queued, but not yet delivered,
  523. * faults. See also do_general_protection below.
  524. */
  525. tsk->thread.error_code = error_code;
  526. tsk->thread.trap_no = trapnr;
  527. if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
  528. printk_ratelimit())
  529. printk(KERN_INFO
  530. "%s[%d] trap %s rip:%lx rsp:%lx error:%lx\n",
  531. tsk->comm, tsk->pid, str,
  532. regs->rip, regs->rsp, error_code);
  533. if (info)
  534. force_sig_info(signr, info, tsk);
  535. else
  536. force_sig(signr, tsk);
  537. return;
  538. }
  539. /* kernel trap */
  540. {
  541. const struct exception_table_entry *fixup;
  542. fixup = search_exception_tables(regs->rip);
  543. if (fixup)
  544. regs->rip = fixup->fixup;
  545. else {
  546. tsk->thread.error_code = error_code;
  547. tsk->thread.trap_no = trapnr;
  548. die(str, regs, error_code);
  549. }
  550. return;
  551. }
  552. }
  553. #define DO_ERROR(trapnr, signr, str, name) \
  554. asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
  555. { \
  556. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
  557. == NOTIFY_STOP) \
  558. return; \
  559. conditional_sti(regs); \
  560. do_trap(trapnr, signr, str, regs, error_code, NULL); \
  561. }
  562. #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
  563. asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
  564. { \
  565. siginfo_t info; \
  566. info.si_signo = signr; \
  567. info.si_errno = 0; \
  568. info.si_code = sicode; \
  569. info.si_addr = (void __user *)siaddr; \
  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, &info); \
  575. }
  576. DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->rip)
  577. DO_ERROR( 4, SIGSEGV, "overflow", overflow)
  578. DO_ERROR( 5, SIGSEGV, "bounds", bounds)
  579. DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->rip)
  580. DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
  581. DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
  582. DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
  583. DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
  584. DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
  585. DO_ERROR(18, SIGSEGV, "reserved", reserved)
  586. /* Runs on IST stack */
  587. asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
  588. {
  589. if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
  590. 12, SIGBUS) == NOTIFY_STOP)
  591. return;
  592. preempt_conditional_sti(regs);
  593. do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
  594. preempt_conditional_cli(regs);
  595. }
  596. asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
  597. {
  598. static const char str[] = "double fault";
  599. struct task_struct *tsk = current;
  600. /* Return not checked because double check cannot be ignored */
  601. notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
  602. tsk->thread.error_code = error_code;
  603. tsk->thread.trap_no = 8;
  604. /* This is always a kernel trap and never fixable (and thus must
  605. never return). */
  606. for (;;)
  607. die(str, regs, error_code);
  608. }
  609. asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
  610. long error_code)
  611. {
  612. struct task_struct *tsk = current;
  613. conditional_sti(regs);
  614. if (user_mode(regs)) {
  615. tsk->thread.error_code = error_code;
  616. tsk->thread.trap_no = 13;
  617. if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
  618. printk_ratelimit())
  619. printk(KERN_INFO
  620. "%s[%d] general protection rip:%lx rsp:%lx error:%lx\n",
  621. tsk->comm, tsk->pid,
  622. regs->rip, regs->rsp, error_code);
  623. force_sig(SIGSEGV, tsk);
  624. return;
  625. }
  626. /* kernel gp */
  627. {
  628. const struct exception_table_entry *fixup;
  629. fixup = search_exception_tables(regs->rip);
  630. if (fixup) {
  631. regs->rip = fixup->fixup;
  632. return;
  633. }
  634. tsk->thread.error_code = error_code;
  635. tsk->thread.trap_no = 13;
  636. if (notify_die(DIE_GPF, "general protection fault", regs,
  637. error_code, 13, SIGSEGV) == NOTIFY_STOP)
  638. return;
  639. die("general protection fault", regs, error_code);
  640. }
  641. }
  642. static __kprobes void
  643. mem_parity_error(unsigned char reason, struct pt_regs * regs)
  644. {
  645. printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
  646. reason);
  647. printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
  648. #if defined(CONFIG_EDAC)
  649. if(edac_handler_set()) {
  650. edac_atomic_assert_error();
  651. return;
  652. }
  653. #endif
  654. if (panic_on_unrecovered_nmi)
  655. panic("NMI: Not continuing");
  656. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  657. /* Clear and disable the memory parity error line. */
  658. reason = (reason & 0xf) | 4;
  659. outb(reason, 0x61);
  660. }
  661. static __kprobes void
  662. io_check_error(unsigned char reason, struct pt_regs * regs)
  663. {
  664. printk("NMI: IOCK error (debug interrupt?)\n");
  665. show_registers(regs);
  666. /* Re-enable the IOCK line, wait for a few seconds */
  667. reason = (reason & 0xf) | 8;
  668. outb(reason, 0x61);
  669. mdelay(2000);
  670. reason &= ~8;
  671. outb(reason, 0x61);
  672. }
  673. static __kprobes void
  674. unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
  675. {
  676. printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
  677. reason);
  678. printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
  679. if (panic_on_unrecovered_nmi)
  680. panic("NMI: Not continuing");
  681. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  682. }
  683. /* Runs on IST stack. This code must keep interrupts off all the time.
  684. Nested NMIs are prevented by the CPU. */
  685. asmlinkage __kprobes void default_do_nmi(struct pt_regs *regs)
  686. {
  687. unsigned char reason = 0;
  688. int cpu;
  689. cpu = smp_processor_id();
  690. /* Only the BSP gets external NMIs from the system. */
  691. if (!cpu)
  692. reason = get_nmi_reason();
  693. if (!(reason & 0xc0)) {
  694. if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
  695. == NOTIFY_STOP)
  696. return;
  697. /*
  698. * Ok, so this is none of the documented NMI sources,
  699. * so it must be the NMI watchdog.
  700. */
  701. if (nmi_watchdog_tick(regs,reason))
  702. return;
  703. if (!do_nmi_callback(regs,cpu))
  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. me->fpu_counter++;
  963. }
  964. void __init trap_init(void)
  965. {
  966. set_intr_gate(0,&divide_error);
  967. set_intr_gate_ist(1,&debug,DEBUG_STACK);
  968. set_intr_gate_ist(2,&nmi,NMI_STACK);
  969. set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */
  970. set_system_gate(4,&overflow); /* int4 can be called from all */
  971. set_intr_gate(5,&bounds);
  972. set_intr_gate(6,&invalid_op);
  973. set_intr_gate(7,&device_not_available);
  974. set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
  975. set_intr_gate(9,&coprocessor_segment_overrun);
  976. set_intr_gate(10,&invalid_TSS);
  977. set_intr_gate(11,&segment_not_present);
  978. set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
  979. set_intr_gate(13,&general_protection);
  980. set_intr_gate(14,&page_fault);
  981. set_intr_gate(15,&spurious_interrupt_bug);
  982. set_intr_gate(16,&coprocessor_error);
  983. set_intr_gate(17,&alignment_check);
  984. #ifdef CONFIG_X86_MCE
  985. set_intr_gate_ist(18,&machine_check, MCE_STACK);
  986. #endif
  987. set_intr_gate(19,&simd_coprocessor_error);
  988. #ifdef CONFIG_IA32_EMULATION
  989. set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
  990. #endif
  991. /*
  992. * Should be a barrier for any external CPU state.
  993. */
  994. cpu_init();
  995. }
  996. static int __init oops_setup(char *s)
  997. {
  998. if (!s)
  999. return -EINVAL;
  1000. if (!strcmp(s, "panic"))
  1001. panic_on_oops = 1;
  1002. return 0;
  1003. }
  1004. early_param("oops", oops_setup);
  1005. static int __init kstack_setup(char *s)
  1006. {
  1007. if (!s)
  1008. return -EINVAL;
  1009. kstack_depth_to_print = simple_strtoul(s,NULL,0);
  1010. return 0;
  1011. }
  1012. early_param("kstack", kstack_setup);