traps.c 25 KB

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