traps.c 25 KB

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