traps.c 24 KB

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