traps.c 24 KB

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