traps.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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. * $Id: traps.c,v 1.36 2002/03/24 11:09:10 ak Exp $
  11. */
  12. /*
  13. * 'Traps.c' handles hardware traps and faults after we have saved some
  14. * state in 'entry.S'.
  15. */
  16. #include <linux/config.h>
  17. #include <linux/sched.h>
  18. #include <linux/kernel.h>
  19. #include <linux/string.h>
  20. #include <linux/errno.h>
  21. #include <linux/ptrace.h>
  22. #include <linux/timer.h>
  23. #include <linux/mm.h>
  24. #include <linux/init.h>
  25. #include <linux/delay.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/module.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/nmi.h>
  31. #include <linux/kprobes.h>
  32. #include <linux/kexec.h>
  33. #include <asm/system.h>
  34. #include <asm/uaccess.h>
  35. #include <asm/io.h>
  36. #include <asm/atomic.h>
  37. #include <asm/debugreg.h>
  38. #include <asm/desc.h>
  39. #include <asm/i387.h>
  40. #include <asm/kdebug.h>
  41. #include <asm/processor.h>
  42. #include <asm/smp.h>
  43. #include <asm/pgalloc.h>
  44. #include <asm/pda.h>
  45. #include <asm/proto.h>
  46. #include <asm/nmi.h>
  47. asmlinkage void divide_error(void);
  48. asmlinkage void debug(void);
  49. asmlinkage void nmi(void);
  50. asmlinkage void int3(void);
  51. asmlinkage void overflow(void);
  52. asmlinkage void bounds(void);
  53. asmlinkage void invalid_op(void);
  54. asmlinkage void device_not_available(void);
  55. asmlinkage void double_fault(void);
  56. asmlinkage void coprocessor_segment_overrun(void);
  57. asmlinkage void invalid_TSS(void);
  58. asmlinkage void segment_not_present(void);
  59. asmlinkage void stack_segment(void);
  60. asmlinkage void general_protection(void);
  61. asmlinkage void page_fault(void);
  62. asmlinkage void coprocessor_error(void);
  63. asmlinkage void simd_coprocessor_error(void);
  64. asmlinkage void reserved(void);
  65. asmlinkage void alignment_check(void);
  66. asmlinkage void machine_check(void);
  67. asmlinkage void spurious_interrupt_bug(void);
  68. ATOMIC_NOTIFIER_HEAD(die_chain);
  69. int register_die_notifier(struct notifier_block *nb)
  70. {
  71. vmalloc_sync_all();
  72. return atomic_notifier_chain_register(&die_chain, nb);
  73. }
  74. EXPORT_SYMBOL(register_die_notifier);
  75. int unregister_die_notifier(struct notifier_block *nb)
  76. {
  77. return atomic_notifier_chain_unregister(&die_chain, nb);
  78. }
  79. EXPORT_SYMBOL(unregister_die_notifier);
  80. static inline void conditional_sti(struct pt_regs *regs)
  81. {
  82. if (regs->eflags & X86_EFLAGS_IF)
  83. local_irq_enable();
  84. }
  85. static inline void preempt_conditional_sti(struct pt_regs *regs)
  86. {
  87. preempt_disable();
  88. if (regs->eflags & X86_EFLAGS_IF)
  89. local_irq_enable();
  90. }
  91. static inline void preempt_conditional_cli(struct pt_regs *regs)
  92. {
  93. if (regs->eflags & X86_EFLAGS_IF)
  94. local_irq_disable();
  95. 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. do_exit(SIGSEGV);
  426. }
  427. static void __kprobes do_trap(int trapnr, int signr, char *str,
  428. struct pt_regs * regs, long error_code,
  429. siginfo_t *info)
  430. {
  431. struct task_struct *tsk = current;
  432. conditional_sti(regs);
  433. tsk->thread.error_code = error_code;
  434. tsk->thread.trap_no = trapnr;
  435. if (user_mode(regs)) {
  436. if (exception_trace && unhandled_signal(tsk, signr))
  437. printk(KERN_INFO
  438. "%s[%d] trap %s rip:%lx rsp:%lx error:%lx\n",
  439. tsk->comm, tsk->pid, str,
  440. regs->rip, regs->rsp, error_code);
  441. if (info)
  442. force_sig_info(signr, info, tsk);
  443. else
  444. force_sig(signr, tsk);
  445. return;
  446. }
  447. /* kernel trap */
  448. {
  449. const struct exception_table_entry *fixup;
  450. fixup = search_exception_tables(regs->rip);
  451. if (fixup)
  452. regs->rip = fixup->fixup;
  453. else
  454. die(str, regs, error_code);
  455. return;
  456. }
  457. }
  458. #define DO_ERROR(trapnr, signr, str, name) \
  459. asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
  460. { \
  461. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
  462. == NOTIFY_STOP) \
  463. return; \
  464. do_trap(trapnr, signr, str, regs, error_code, NULL); \
  465. }
  466. #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
  467. asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
  468. { \
  469. siginfo_t info; \
  470. info.si_signo = signr; \
  471. info.si_errno = 0; \
  472. info.si_code = sicode; \
  473. info.si_addr = (void __user *)siaddr; \
  474. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
  475. == NOTIFY_STOP) \
  476. return; \
  477. do_trap(trapnr, signr, str, regs, error_code, &info); \
  478. }
  479. DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->rip)
  480. DO_ERROR( 4, SIGSEGV, "overflow", overflow)
  481. DO_ERROR( 5, SIGSEGV, "bounds", bounds)
  482. DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->rip)
  483. DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
  484. DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
  485. DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
  486. DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
  487. DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
  488. DO_ERROR(18, SIGSEGV, "reserved", reserved)
  489. DO_ERROR(12, SIGBUS, "stack segment", stack_segment)
  490. asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
  491. {
  492. static const char str[] = "double fault";
  493. struct task_struct *tsk = current;
  494. /* Return not checked because double check cannot be ignored */
  495. notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
  496. tsk->thread.error_code = error_code;
  497. tsk->thread.trap_no = 8;
  498. /* This is always a kernel trap and never fixable (and thus must
  499. never return). */
  500. for (;;)
  501. die(str, regs, error_code);
  502. }
  503. asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
  504. long error_code)
  505. {
  506. struct task_struct *tsk = current;
  507. conditional_sti(regs);
  508. tsk->thread.error_code = error_code;
  509. tsk->thread.trap_no = 13;
  510. if (user_mode(regs)) {
  511. if (exception_trace && unhandled_signal(tsk, SIGSEGV))
  512. printk(KERN_INFO
  513. "%s[%d] general protection rip:%lx rsp:%lx error:%lx\n",
  514. tsk->comm, tsk->pid,
  515. regs->rip, regs->rsp, error_code);
  516. force_sig(SIGSEGV, tsk);
  517. return;
  518. }
  519. /* kernel gp */
  520. {
  521. const struct exception_table_entry *fixup;
  522. fixup = search_exception_tables(regs->rip);
  523. if (fixup) {
  524. regs->rip = fixup->fixup;
  525. return;
  526. }
  527. if (notify_die(DIE_GPF, "general protection fault", regs,
  528. error_code, 13, SIGSEGV) == NOTIFY_STOP)
  529. return;
  530. die("general protection fault", regs, error_code);
  531. }
  532. }
  533. static __kprobes void
  534. mem_parity_error(unsigned char reason, struct pt_regs * regs)
  535. {
  536. printk("Uhhuh. NMI received. Dazed and confused, but trying to continue\n");
  537. printk("You probably have a hardware problem with your RAM chips\n");
  538. /* Clear and disable the memory parity error line. */
  539. reason = (reason & 0xf) | 4;
  540. outb(reason, 0x61);
  541. }
  542. static __kprobes void
  543. io_check_error(unsigned char reason, struct pt_regs * regs)
  544. {
  545. printk("NMI: IOCK error (debug interrupt?)\n");
  546. show_registers(regs);
  547. /* Re-enable the IOCK line, wait for a few seconds */
  548. reason = (reason & 0xf) | 8;
  549. outb(reason, 0x61);
  550. mdelay(2000);
  551. reason &= ~8;
  552. outb(reason, 0x61);
  553. }
  554. static __kprobes void
  555. unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
  556. { printk("Uhhuh. NMI received for unknown reason %02x.\n", reason);
  557. printk("Dazed and confused, but trying to continue\n");
  558. printk("Do you have a strange power saving mode enabled?\n");
  559. }
  560. /* Runs on IST stack. This code must keep interrupts off all the time.
  561. Nested NMIs are prevented by the CPU. */
  562. asmlinkage __kprobes void default_do_nmi(struct pt_regs *regs)
  563. {
  564. unsigned char reason = 0;
  565. int cpu;
  566. cpu = smp_processor_id();
  567. /* Only the BSP gets external NMIs from the system. */
  568. if (!cpu)
  569. reason = get_nmi_reason();
  570. if (!(reason & 0xc0)) {
  571. if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
  572. == NOTIFY_STOP)
  573. return;
  574. #ifdef CONFIG_X86_LOCAL_APIC
  575. /*
  576. * Ok, so this is none of the documented NMI sources,
  577. * so it must be the NMI watchdog.
  578. */
  579. if (nmi_watchdog > 0) {
  580. nmi_watchdog_tick(regs,reason);
  581. return;
  582. }
  583. #endif
  584. unknown_nmi_error(reason, regs);
  585. return;
  586. }
  587. if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
  588. return;
  589. /* AK: following checks seem to be broken on modern chipsets. FIXME */
  590. if (reason & 0x80)
  591. mem_parity_error(reason, regs);
  592. if (reason & 0x40)
  593. io_check_error(reason, regs);
  594. }
  595. /* runs on IST stack. */
  596. asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code)
  597. {
  598. if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
  599. return;
  600. }
  601. do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
  602. return;
  603. }
  604. /* Help handler running on IST stack to switch back to user stack
  605. for scheduling or signal handling. The actual stack switch is done in
  606. entry.S */
  607. asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
  608. {
  609. struct pt_regs *regs = eregs;
  610. /* Did already sync */
  611. if (eregs == (struct pt_regs *)eregs->rsp)
  612. ;
  613. /* Exception from user space */
  614. else if (user_mode(eregs))
  615. regs = task_pt_regs(current);
  616. /* Exception from kernel and interrupts are enabled. Move to
  617. kernel process stack. */
  618. else if (eregs->eflags & X86_EFLAGS_IF)
  619. regs = (struct pt_regs *)(eregs->rsp -= sizeof(struct pt_regs));
  620. if (eregs != regs)
  621. *regs = *eregs;
  622. return regs;
  623. }
  624. /* runs on IST stack. */
  625. asmlinkage void __kprobes do_debug(struct pt_regs * regs,
  626. unsigned long error_code)
  627. {
  628. unsigned long condition;
  629. struct task_struct *tsk = current;
  630. siginfo_t info;
  631. get_debugreg(condition, 6);
  632. if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
  633. SIGTRAP) == NOTIFY_STOP)
  634. return;
  635. preempt_conditional_sti(regs);
  636. /* Mask out spurious debug traps due to lazy DR7 setting */
  637. if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
  638. if (!tsk->thread.debugreg7) {
  639. goto clear_dr7;
  640. }
  641. }
  642. tsk->thread.debugreg6 = condition;
  643. /* Mask out spurious TF errors due to lazy TF clearing */
  644. if (condition & DR_STEP) {
  645. /*
  646. * The TF error should be masked out only if the current
  647. * process is not traced and if the TRAP flag has been set
  648. * previously by a tracing process (condition detected by
  649. * the PT_DTRACE flag); remember that the i386 TRAP flag
  650. * can be modified by the process itself in user mode,
  651. * allowing programs to debug themselves without the ptrace()
  652. * interface.
  653. */
  654. if (!user_mode(regs))
  655. goto clear_TF_reenable;
  656. /*
  657. * Was the TF flag set by a debugger? If so, clear it now,
  658. * so that register information is correct.
  659. */
  660. if (tsk->ptrace & PT_DTRACE) {
  661. regs->eflags &= ~TF_MASK;
  662. tsk->ptrace &= ~PT_DTRACE;
  663. }
  664. }
  665. /* Ok, finally something we can handle */
  666. tsk->thread.trap_no = 1;
  667. tsk->thread.error_code = error_code;
  668. info.si_signo = SIGTRAP;
  669. info.si_errno = 0;
  670. info.si_code = TRAP_BRKPT;
  671. info.si_addr = user_mode(regs) ? (void __user *)regs->rip : NULL;
  672. force_sig_info(SIGTRAP, &info, tsk);
  673. clear_dr7:
  674. set_debugreg(0UL, 7);
  675. preempt_conditional_cli(regs);
  676. return;
  677. clear_TF_reenable:
  678. set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
  679. regs->eflags &= ~TF_MASK;
  680. preempt_conditional_cli(regs);
  681. }
  682. static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
  683. {
  684. const struct exception_table_entry *fixup;
  685. fixup = search_exception_tables(regs->rip);
  686. if (fixup) {
  687. regs->rip = fixup->fixup;
  688. return 1;
  689. }
  690. notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
  691. /* Illegal floating point operation in the kernel */
  692. current->thread.trap_no = trapnr;
  693. die(str, regs, 0);
  694. return 0;
  695. }
  696. /*
  697. * Note that we play around with the 'TS' bit in an attempt to get
  698. * the correct behaviour even in the presence of the asynchronous
  699. * IRQ13 behaviour
  700. */
  701. asmlinkage void do_coprocessor_error(struct pt_regs *regs)
  702. {
  703. void __user *rip = (void __user *)(regs->rip);
  704. struct task_struct * task;
  705. siginfo_t info;
  706. unsigned short cwd, swd;
  707. conditional_sti(regs);
  708. if (!user_mode(regs) &&
  709. kernel_math_error(regs, "kernel x87 math error", 16))
  710. return;
  711. /*
  712. * Save the info for the exception handler and clear the error.
  713. */
  714. task = current;
  715. save_init_fpu(task);
  716. task->thread.trap_no = 16;
  717. task->thread.error_code = 0;
  718. info.si_signo = SIGFPE;
  719. info.si_errno = 0;
  720. info.si_code = __SI_FAULT;
  721. info.si_addr = rip;
  722. /*
  723. * (~cwd & swd) will mask out exceptions that are not set to unmasked
  724. * status. 0x3f is the exception bits in these regs, 0x200 is the
  725. * C1 reg you need in case of a stack fault, 0x040 is the stack
  726. * fault bit. We should only be taking one exception at a time,
  727. * so if this combination doesn't produce any single exception,
  728. * then we have a bad program that isn't synchronizing its FPU usage
  729. * and it will suffer the consequences since we won't be able to
  730. * fully reproduce the context of the exception
  731. */
  732. cwd = get_fpu_cwd(task);
  733. swd = get_fpu_swd(task);
  734. switch (swd & ~cwd & 0x3f) {
  735. case 0x000:
  736. default:
  737. break;
  738. case 0x001: /* Invalid Op */
  739. /*
  740. * swd & 0x240 == 0x040: Stack Underflow
  741. * swd & 0x240 == 0x240: Stack Overflow
  742. * User must clear the SF bit (0x40) if set
  743. */
  744. info.si_code = FPE_FLTINV;
  745. break;
  746. case 0x002: /* Denormalize */
  747. case 0x010: /* Underflow */
  748. info.si_code = FPE_FLTUND;
  749. break;
  750. case 0x004: /* Zero Divide */
  751. info.si_code = FPE_FLTDIV;
  752. break;
  753. case 0x008: /* Overflow */
  754. info.si_code = FPE_FLTOVF;
  755. break;
  756. case 0x020: /* Precision */
  757. info.si_code = FPE_FLTRES;
  758. break;
  759. }
  760. force_sig_info(SIGFPE, &info, task);
  761. }
  762. asmlinkage void bad_intr(void)
  763. {
  764. printk("bad interrupt");
  765. }
  766. asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
  767. {
  768. void __user *rip = (void __user *)(regs->rip);
  769. struct task_struct * task;
  770. siginfo_t info;
  771. unsigned short mxcsr;
  772. conditional_sti(regs);
  773. if (!user_mode(regs) &&
  774. kernel_math_error(regs, "kernel simd math error", 19))
  775. return;
  776. /*
  777. * Save the info for the exception handler and clear the error.
  778. */
  779. task = current;
  780. save_init_fpu(task);
  781. task->thread.trap_no = 19;
  782. task->thread.error_code = 0;
  783. info.si_signo = SIGFPE;
  784. info.si_errno = 0;
  785. info.si_code = __SI_FAULT;
  786. info.si_addr = rip;
  787. /*
  788. * The SIMD FPU exceptions are handled a little differently, as there
  789. * is only a single status/control register. Thus, to determine which
  790. * unmasked exception was caught we must mask the exception mask bits
  791. * at 0x1f80, and then use these to mask the exception bits at 0x3f.
  792. */
  793. mxcsr = get_fpu_mxcsr(task);
  794. switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
  795. case 0x000:
  796. default:
  797. break;
  798. case 0x001: /* Invalid Op */
  799. info.si_code = FPE_FLTINV;
  800. break;
  801. case 0x002: /* Denormalize */
  802. case 0x010: /* Underflow */
  803. info.si_code = FPE_FLTUND;
  804. break;
  805. case 0x004: /* Zero Divide */
  806. info.si_code = FPE_FLTDIV;
  807. break;
  808. case 0x008: /* Overflow */
  809. info.si_code = FPE_FLTOVF;
  810. break;
  811. case 0x020: /* Precision */
  812. info.si_code = FPE_FLTRES;
  813. break;
  814. }
  815. force_sig_info(SIGFPE, &info, task);
  816. }
  817. asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
  818. {
  819. }
  820. asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
  821. {
  822. }
  823. asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
  824. {
  825. }
  826. /*
  827. * 'math_state_restore()' saves the current math information in the
  828. * old math state array, and gets the new ones from the current task
  829. *
  830. * Careful.. There are problems with IBM-designed IRQ13 behaviour.
  831. * Don't touch unless you *really* know how it works.
  832. */
  833. asmlinkage void math_state_restore(void)
  834. {
  835. struct task_struct *me = current;
  836. clts(); /* Allow maths ops (or we recurse) */
  837. if (!used_math())
  838. init_fpu(me);
  839. restore_fpu_checking(&me->thread.i387.fxsave);
  840. task_thread_info(me)->status |= TS_USEDFPU;
  841. }
  842. void __init trap_init(void)
  843. {
  844. set_intr_gate(0,&divide_error);
  845. set_intr_gate_ist(1,&debug,DEBUG_STACK);
  846. set_intr_gate_ist(2,&nmi,NMI_STACK);
  847. set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */
  848. set_system_gate(4,&overflow); /* int4 can be called from all */
  849. set_intr_gate(5,&bounds);
  850. set_intr_gate(6,&invalid_op);
  851. set_intr_gate(7,&device_not_available);
  852. set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
  853. set_intr_gate(9,&coprocessor_segment_overrun);
  854. set_intr_gate(10,&invalid_TSS);
  855. set_intr_gate(11,&segment_not_present);
  856. set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
  857. set_intr_gate(13,&general_protection);
  858. set_intr_gate(14,&page_fault);
  859. set_intr_gate(15,&spurious_interrupt_bug);
  860. set_intr_gate(16,&coprocessor_error);
  861. set_intr_gate(17,&alignment_check);
  862. #ifdef CONFIG_X86_MCE
  863. set_intr_gate_ist(18,&machine_check, MCE_STACK);
  864. #endif
  865. set_intr_gate(19,&simd_coprocessor_error);
  866. #ifdef CONFIG_IA32_EMULATION
  867. set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
  868. #endif
  869. /*
  870. * Should be a barrier for any external CPU state.
  871. */
  872. cpu_init();
  873. }
  874. /* Actual parsing is done early in setup.c. */
  875. static int __init oops_dummy(char *s)
  876. {
  877. panic_on_oops = 1;
  878. return 1;
  879. }
  880. __setup("oops=", oops_dummy);
  881. static int __init kstack_setup(char *s)
  882. {
  883. kstack_depth_to_print = simple_strtoul(s,NULL,0);
  884. return 1;
  885. }
  886. __setup("kstack=", kstack_setup);