traps_64.c 28 KB

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