traps.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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. #define DO_ERROR_STACK(trapnr, signr, str, name) \
  438. asmlinkage void *do_##name(struct pt_regs * regs, long error_code) \
  439. { \
  440. struct pt_regs *pr = ((struct pt_regs *)(current->thread.rsp0))-1; \
  441. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
  442. == NOTIFY_STOP) \
  443. return regs; \
  444. if (regs->cs & 3) { \
  445. memcpy(pr, regs, sizeof(struct pt_regs)); \
  446. regs = pr; \
  447. } \
  448. do_trap(trapnr, signr, str, regs, error_code, NULL); \
  449. return regs; \
  450. }
  451. DO_ERROR_STACK(12, SIGBUS, "stack segment", stack_segment)
  452. DO_ERROR_STACK( 8, SIGSEGV, "double fault", double_fault)
  453. asmlinkage void do_general_protection(struct pt_regs * regs, long error_code)
  454. {
  455. conditional_sti(regs);
  456. #ifdef CONFIG_CHECKING
  457. {
  458. unsigned long gs;
  459. struct x8664_pda *pda = cpu_pda + safe_smp_processor_id();
  460. rdmsrl(MSR_GS_BASE, gs);
  461. if (gs != (unsigned long)pda) {
  462. wrmsrl(MSR_GS_BASE, pda);
  463. oops_in_progress++;
  464. printk("general protection handler: wrong gs %lx expected %p\n", gs, pda);
  465. oops_in_progress--;
  466. }
  467. }
  468. #endif
  469. if ((regs->cs & 3)!=0) {
  470. struct task_struct *tsk = current;
  471. if (exception_trace && unhandled_signal(tsk, SIGSEGV))
  472. printk(KERN_INFO
  473. "%s[%d] general protection rip:%lx rsp:%lx error:%lx\n",
  474. tsk->comm, tsk->pid,
  475. regs->rip,regs->rsp,error_code);
  476. tsk->thread.error_code = error_code;
  477. tsk->thread.trap_no = 13;
  478. force_sig(SIGSEGV, tsk);
  479. return;
  480. }
  481. /* kernel gp */
  482. {
  483. const struct exception_table_entry *fixup;
  484. fixup = search_exception_tables(regs->rip);
  485. if (fixup) {
  486. regs->rip = fixup->fixup;
  487. return;
  488. }
  489. if (notify_die(DIE_GPF, "general protection fault", regs,
  490. error_code, 13, SIGSEGV) == NOTIFY_STOP)
  491. return;
  492. die("general protection fault", regs, error_code);
  493. }
  494. }
  495. static void mem_parity_error(unsigned char reason, struct pt_regs * regs)
  496. {
  497. printk("Uhhuh. NMI received. Dazed and confused, but trying to continue\n");
  498. printk("You probably have a hardware problem with your RAM chips\n");
  499. /* Clear and disable the memory parity error line. */
  500. reason = (reason & 0xf) | 4;
  501. outb(reason, 0x61);
  502. }
  503. static void io_check_error(unsigned char reason, struct pt_regs * regs)
  504. {
  505. printk("NMI: IOCK error (debug interrupt?)\n");
  506. show_registers(regs);
  507. /* Re-enable the IOCK line, wait for a few seconds */
  508. reason = (reason & 0xf) | 8;
  509. outb(reason, 0x61);
  510. mdelay(2000);
  511. reason &= ~8;
  512. outb(reason, 0x61);
  513. }
  514. static void unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
  515. { printk("Uhhuh. NMI received for unknown reason %02x.\n", reason);
  516. printk("Dazed and confused, but trying to continue\n");
  517. printk("Do you have a strange power saving mode enabled?\n");
  518. }
  519. asmlinkage void default_do_nmi(struct pt_regs *regs)
  520. {
  521. unsigned char reason = 0;
  522. /* Only the BSP gets external NMIs from the system. */
  523. if (!smp_processor_id())
  524. reason = get_nmi_reason();
  525. if (!(reason & 0xc0)) {
  526. if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 0, SIGINT)
  527. == NOTIFY_STOP)
  528. return;
  529. #ifdef CONFIG_X86_LOCAL_APIC
  530. /*
  531. * Ok, so this is none of the documented NMI sources,
  532. * so it must be the NMI watchdog.
  533. */
  534. if (nmi_watchdog > 0) {
  535. nmi_watchdog_tick(regs,reason);
  536. return;
  537. }
  538. #endif
  539. unknown_nmi_error(reason, regs);
  540. return;
  541. }
  542. if (notify_die(DIE_NMI, "nmi", regs, reason, 0, SIGINT) == NOTIFY_STOP)
  543. return;
  544. /* AK: following checks seem to be broken on modern chipsets. FIXME */
  545. if (reason & 0x80)
  546. mem_parity_error(reason, regs);
  547. if (reason & 0x40)
  548. io_check_error(reason, regs);
  549. }
  550. asmlinkage void do_int3(struct pt_regs * regs, long error_code)
  551. {
  552. if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
  553. return;
  554. }
  555. do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
  556. return;
  557. }
  558. /* runs on IST stack. */
  559. asmlinkage void *do_debug(struct pt_regs * regs, unsigned long error_code)
  560. {
  561. struct pt_regs *pr;
  562. unsigned long condition;
  563. struct task_struct *tsk = current;
  564. siginfo_t info;
  565. pr = (struct pt_regs *)(current->thread.rsp0)-1;
  566. if (regs->cs & 3) {
  567. memcpy(pr, regs, sizeof(struct pt_regs));
  568. regs = pr;
  569. }
  570. #ifdef CONFIG_CHECKING
  571. {
  572. /* RED-PEN interaction with debugger - could destroy gs */
  573. unsigned long gs;
  574. struct x8664_pda *pda = cpu_pda + safe_smp_processor_id();
  575. rdmsrl(MSR_GS_BASE, gs);
  576. if (gs != (unsigned long)pda) {
  577. wrmsrl(MSR_GS_BASE, pda);
  578. printk("debug handler: wrong gs %lx expected %p\n", gs, pda);
  579. }
  580. }
  581. #endif
  582. asm("movq %%db6,%0" : "=r" (condition));
  583. if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
  584. SIGTRAP) == NOTIFY_STOP) {
  585. return regs;
  586. }
  587. conditional_sti(regs);
  588. /* Mask out spurious debug traps due to lazy DR7 setting */
  589. if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
  590. if (!tsk->thread.debugreg7) {
  591. goto clear_dr7;
  592. }
  593. }
  594. tsk->thread.debugreg6 = condition;
  595. /* Mask out spurious TF errors due to lazy TF clearing */
  596. if ((condition & DR_STEP) &&
  597. (notify_die(DIE_DEBUGSTEP, "debugstep", regs, condition,
  598. 1, SIGTRAP) != NOTIFY_STOP)) {
  599. /*
  600. * The TF error should be masked out only if the current
  601. * process is not traced and if the TRAP flag has been set
  602. * previously by a tracing process (condition detected by
  603. * the PT_DTRACE flag); remember that the i386 TRAP flag
  604. * can be modified by the process itself in user mode,
  605. * allowing programs to debug themselves without the ptrace()
  606. * interface.
  607. */
  608. if ((regs->cs & 3) == 0)
  609. goto clear_TF_reenable;
  610. if ((tsk->ptrace & (PT_DTRACE|PT_PTRACED)) == PT_DTRACE)
  611. goto clear_TF;
  612. }
  613. /* Ok, finally something we can handle */
  614. tsk->thread.trap_no = 1;
  615. tsk->thread.error_code = error_code;
  616. info.si_signo = SIGTRAP;
  617. info.si_errno = 0;
  618. info.si_code = TRAP_BRKPT;
  619. if ((regs->cs & 3) == 0)
  620. goto clear_dr7;
  621. info.si_addr = (void __user *)regs->rip;
  622. force_sig_info(SIGTRAP, &info, tsk);
  623. clear_dr7:
  624. asm volatile("movq %0,%%db7"::"r"(0UL));
  625. notify_die(DIE_DEBUG, "debug", regs, condition, 1, SIGTRAP);
  626. return regs;
  627. clear_TF_reenable:
  628. set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
  629. clear_TF:
  630. /* RED-PEN could cause spurious errors */
  631. if (notify_die(DIE_DEBUG, "debug2", regs, condition, 1, SIGTRAP)
  632. != NOTIFY_STOP)
  633. regs->eflags &= ~TF_MASK;
  634. return regs;
  635. }
  636. static int kernel_math_error(struct pt_regs *regs, char *str)
  637. {
  638. const struct exception_table_entry *fixup;
  639. fixup = search_exception_tables(regs->rip);
  640. if (fixup) {
  641. regs->rip = fixup->fixup;
  642. return 1;
  643. }
  644. notify_die(DIE_GPF, str, regs, 0, 16, SIGFPE);
  645. #if 0
  646. /* This should be a die, but warn only for now */
  647. die(str, regs, 0);
  648. #else
  649. printk(KERN_DEBUG "%s: %s at ", current->comm, str);
  650. printk_address(regs->rip);
  651. printk("\n");
  652. #endif
  653. return 0;
  654. }
  655. /*
  656. * Note that we play around with the 'TS' bit in an attempt to get
  657. * the correct behaviour even in the presence of the asynchronous
  658. * IRQ13 behaviour
  659. */
  660. asmlinkage void do_coprocessor_error(struct pt_regs *regs)
  661. {
  662. void __user *rip = (void __user *)(regs->rip);
  663. struct task_struct * task;
  664. siginfo_t info;
  665. unsigned short cwd, swd;
  666. conditional_sti(regs);
  667. if ((regs->cs & 3) == 0 &&
  668. kernel_math_error(regs, "kernel x87 math error"))
  669. return;
  670. /*
  671. * Save the info for the exception handler and clear the error.
  672. */
  673. task = current;
  674. save_init_fpu(task);
  675. task->thread.trap_no = 16;
  676. task->thread.error_code = 0;
  677. info.si_signo = SIGFPE;
  678. info.si_errno = 0;
  679. info.si_code = __SI_FAULT;
  680. info.si_addr = rip;
  681. /*
  682. * (~cwd & swd) will mask out exceptions that are not set to unmasked
  683. * status. 0x3f is the exception bits in these regs, 0x200 is the
  684. * C1 reg you need in case of a stack fault, 0x040 is the stack
  685. * fault bit. We should only be taking one exception at a time,
  686. * so if this combination doesn't produce any single exception,
  687. * then we have a bad program that isn't synchronizing its FPU usage
  688. * and it will suffer the consequences since we won't be able to
  689. * fully reproduce the context of the exception
  690. */
  691. cwd = get_fpu_cwd(task);
  692. swd = get_fpu_swd(task);
  693. switch (((~cwd) & swd & 0x3f) | (swd & 0x240)) {
  694. case 0x000:
  695. default:
  696. break;
  697. case 0x001: /* Invalid Op */
  698. case 0x041: /* Stack Fault */
  699. case 0x241: /* Stack Fault | Direction */
  700. info.si_code = FPE_FLTINV;
  701. break;
  702. case 0x002: /* Denormalize */
  703. case 0x010: /* Underflow */
  704. info.si_code = FPE_FLTUND;
  705. break;
  706. case 0x004: /* Zero Divide */
  707. info.si_code = FPE_FLTDIV;
  708. break;
  709. case 0x008: /* Overflow */
  710. info.si_code = FPE_FLTOVF;
  711. break;
  712. case 0x020: /* Precision */
  713. info.si_code = FPE_FLTRES;
  714. break;
  715. }
  716. force_sig_info(SIGFPE, &info, task);
  717. }
  718. asmlinkage void bad_intr(void)
  719. {
  720. printk("bad interrupt");
  721. }
  722. asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
  723. {
  724. void __user *rip = (void __user *)(regs->rip);
  725. struct task_struct * task;
  726. siginfo_t info;
  727. unsigned short mxcsr;
  728. conditional_sti(regs);
  729. if ((regs->cs & 3) == 0 &&
  730. kernel_math_error(regs, "simd math error"))
  731. return;
  732. /*
  733. * Save the info for the exception handler and clear the error.
  734. */
  735. task = current;
  736. save_init_fpu(task);
  737. task->thread.trap_no = 19;
  738. task->thread.error_code = 0;
  739. info.si_signo = SIGFPE;
  740. info.si_errno = 0;
  741. info.si_code = __SI_FAULT;
  742. info.si_addr = rip;
  743. /*
  744. * The SIMD FPU exceptions are handled a little differently, as there
  745. * is only a single status/control register. Thus, to determine which
  746. * unmasked exception was caught we must mask the exception mask bits
  747. * at 0x1f80, and then use these to mask the exception bits at 0x3f.
  748. */
  749. mxcsr = get_fpu_mxcsr(task);
  750. switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
  751. case 0x000:
  752. default:
  753. break;
  754. case 0x001: /* Invalid Op */
  755. info.si_code = FPE_FLTINV;
  756. break;
  757. case 0x002: /* Denormalize */
  758. case 0x010: /* Underflow */
  759. info.si_code = FPE_FLTUND;
  760. break;
  761. case 0x004: /* Zero Divide */
  762. info.si_code = FPE_FLTDIV;
  763. break;
  764. case 0x008: /* Overflow */
  765. info.si_code = FPE_FLTOVF;
  766. break;
  767. case 0x020: /* Precision */
  768. info.si_code = FPE_FLTRES;
  769. break;
  770. }
  771. force_sig_info(SIGFPE, &info, task);
  772. }
  773. asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
  774. {
  775. }
  776. asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
  777. {
  778. }
  779. /*
  780. * 'math_state_restore()' saves the current math information in the
  781. * old math state array, and gets the new ones from the current task
  782. *
  783. * Careful.. There are problems with IBM-designed IRQ13 behaviour.
  784. * Don't touch unless you *really* know how it works.
  785. */
  786. asmlinkage void math_state_restore(void)
  787. {
  788. struct task_struct *me = current;
  789. clts(); /* Allow maths ops (or we recurse) */
  790. if (!used_math())
  791. init_fpu(me);
  792. restore_fpu_checking(&me->thread.i387.fxsave);
  793. me->thread_info->status |= TS_USEDFPU;
  794. }
  795. void do_call_debug(struct pt_regs *regs)
  796. {
  797. notify_die(DIE_CALL, "debug call", regs, 0, 255, SIGINT);
  798. }
  799. void __init trap_init(void)
  800. {
  801. set_intr_gate(0,&divide_error);
  802. set_intr_gate_ist(1,&debug,DEBUG_STACK);
  803. set_intr_gate_ist(2,&nmi,NMI_STACK);
  804. set_system_gate(3,&int3);
  805. set_system_gate(4,&overflow); /* int4-5 can be called from all */
  806. set_system_gate(5,&bounds);
  807. set_intr_gate(6,&invalid_op);
  808. set_intr_gate(7,&device_not_available);
  809. set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
  810. set_intr_gate(9,&coprocessor_segment_overrun);
  811. set_intr_gate(10,&invalid_TSS);
  812. set_intr_gate(11,&segment_not_present);
  813. set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
  814. set_intr_gate(13,&general_protection);
  815. set_intr_gate(14,&page_fault);
  816. set_intr_gate(15,&spurious_interrupt_bug);
  817. set_intr_gate(16,&coprocessor_error);
  818. set_intr_gate(17,&alignment_check);
  819. #ifdef CONFIG_X86_MCE
  820. set_intr_gate_ist(18,&machine_check, MCE_STACK);
  821. #endif
  822. set_intr_gate(19,&simd_coprocessor_error);
  823. #ifdef CONFIG_IA32_EMULATION
  824. set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
  825. #endif
  826. set_intr_gate(KDB_VECTOR, call_debug);
  827. /*
  828. * Should be a barrier for any external CPU state.
  829. */
  830. cpu_init();
  831. }
  832. /* Actual parsing is done early in setup.c. */
  833. static int __init oops_dummy(char *s)
  834. {
  835. panic_on_oops = 1;
  836. return -1;
  837. }
  838. __setup("oops=", oops_dummy);
  839. static int __init kstack_setup(char *s)
  840. {
  841. kstack_depth_to_print = simple_strtoul(s,NULL,0);
  842. return 0;
  843. }
  844. __setup("kstack=", kstack_setup);