traps.c 25 KB

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