traps.c 24 KB

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