traps.c 24 KB

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