traps.c 24 KB

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