traps.c 24 KB

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