traps.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  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. /*
  11. * 'Traps.c' handles hardware traps and faults after we have saved some
  12. * state in 'entry.S'.
  13. */
  14. #include <linux/sched.h>
  15. #include <linux/kernel.h>
  16. #include <linux/string.h>
  17. #include <linux/errno.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/timer.h>
  20. #include <linux/mm.h>
  21. #include <linux/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/kallsyms.h>
  26. #include <linux/module.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/nmi.h>
  29. #include <linux/kprobes.h>
  30. #include <linux/kexec.h>
  31. #include <linux/unwind.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/bug.h>
  34. #include <asm/system.h>
  35. #include <asm/io.h>
  36. #include <asm/atomic.h>
  37. #include <asm/debugreg.h>
  38. #include <asm/desc.h>
  39. #include <asm/i387.h>
  40. #include <asm/kdebug.h>
  41. #include <asm/processor.h>
  42. #include <asm/unwind.h>
  43. #include <asm/smp.h>
  44. #include <asm/pgalloc.h>
  45. #include <asm/pda.h>
  46. #include <asm/proto.h>
  47. #include <asm/nmi.h>
  48. #include <asm/stacktrace.h>
  49. asmlinkage void divide_error(void);
  50. asmlinkage void debug(void);
  51. asmlinkage void nmi(void);
  52. asmlinkage void int3(void);
  53. asmlinkage void overflow(void);
  54. asmlinkage void bounds(void);
  55. asmlinkage void invalid_op(void);
  56. asmlinkage void device_not_available(void);
  57. asmlinkage void double_fault(void);
  58. asmlinkage void coprocessor_segment_overrun(void);
  59. asmlinkage void invalid_TSS(void);
  60. asmlinkage void segment_not_present(void);
  61. asmlinkage void stack_segment(void);
  62. asmlinkage void general_protection(void);
  63. asmlinkage void page_fault(void);
  64. asmlinkage void coprocessor_error(void);
  65. asmlinkage void simd_coprocessor_error(void);
  66. asmlinkage void reserved(void);
  67. asmlinkage void alignment_check(void);
  68. asmlinkage void machine_check(void);
  69. asmlinkage void spurious_interrupt_bug(void);
  70. ATOMIC_NOTIFIER_HEAD(die_chain);
  71. EXPORT_SYMBOL(die_chain);
  72. int register_die_notifier(struct notifier_block *nb)
  73. {
  74. vmalloc_sync_all();
  75. return atomic_notifier_chain_register(&die_chain, nb);
  76. }
  77. EXPORT_SYMBOL(register_die_notifier); /* used modular by kdb */
  78. int unregister_die_notifier(struct notifier_block *nb)
  79. {
  80. return atomic_notifier_chain_unregister(&die_chain, nb);
  81. }
  82. EXPORT_SYMBOL(unregister_die_notifier); /* used modular by kdb */
  83. static inline void conditional_sti(struct pt_regs *regs)
  84. {
  85. if (regs->eflags & X86_EFLAGS_IF)
  86. local_irq_enable();
  87. }
  88. static inline void preempt_conditional_sti(struct pt_regs *regs)
  89. {
  90. preempt_disable();
  91. if (regs->eflags & X86_EFLAGS_IF)
  92. local_irq_enable();
  93. }
  94. static inline void preempt_conditional_cli(struct pt_regs *regs)
  95. {
  96. if (regs->eflags & X86_EFLAGS_IF)
  97. local_irq_disable();
  98. /* Make sure to not schedule here because we could be running
  99. on an exception stack. */
  100. preempt_enable_no_resched();
  101. }
  102. int kstack_depth_to_print = 12;
  103. #ifdef CONFIG_KALLSYMS
  104. void printk_address(unsigned long address)
  105. {
  106. unsigned long offset = 0, symsize;
  107. const char *symname;
  108. char *modname;
  109. char *delim = ":";
  110. char namebuf[128];
  111. symname = kallsyms_lookup(address, &symsize, &offset,
  112. &modname, namebuf);
  113. if (!symname) {
  114. printk(" [<%016lx>]\n", address);
  115. return;
  116. }
  117. if (!modname)
  118. modname = delim = "";
  119. printk(" [<%016lx>] %s%s%s%s+0x%lx/0x%lx\n",
  120. address, delim, modname, delim, symname, offset, symsize);
  121. }
  122. #else
  123. void printk_address(unsigned long address)
  124. {
  125. printk(" [<%016lx>]\n", address);
  126. }
  127. #endif
  128. static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
  129. unsigned *usedp, char **idp)
  130. {
  131. static char ids[][8] = {
  132. [DEBUG_STACK - 1] = "#DB",
  133. [NMI_STACK - 1] = "NMI",
  134. [DOUBLEFAULT_STACK - 1] = "#DF",
  135. [STACKFAULT_STACK - 1] = "#SS",
  136. [MCE_STACK - 1] = "#MC",
  137. #if DEBUG_STKSZ > EXCEPTION_STKSZ
  138. [N_EXCEPTION_STACKS ... N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
  139. #endif
  140. };
  141. unsigned k;
  142. /*
  143. * Iterate over all exception stacks, and figure out whether
  144. * 'stack' is in one of them:
  145. */
  146. for (k = 0; k < N_EXCEPTION_STACKS; k++) {
  147. unsigned long end = per_cpu(orig_ist, cpu).ist[k];
  148. /*
  149. * Is 'stack' above this exception frame's end?
  150. * If yes then skip to the next frame.
  151. */
  152. if (stack >= end)
  153. continue;
  154. /*
  155. * Is 'stack' above this exception frame's start address?
  156. * If yes then we found the right frame.
  157. */
  158. if (stack >= end - EXCEPTION_STKSZ) {
  159. /*
  160. * Make sure we only iterate through an exception
  161. * stack once. If it comes up for the second time
  162. * then there's something wrong going on - just
  163. * break out and return NULL:
  164. */
  165. if (*usedp & (1U << k))
  166. break;
  167. *usedp |= 1U << k;
  168. *idp = ids[k];
  169. return (unsigned long *)end;
  170. }
  171. /*
  172. * If this is a debug stack, and if it has a larger size than
  173. * the usual exception stacks, then 'stack' might still
  174. * be within the lower portion of the debug stack:
  175. */
  176. #if DEBUG_STKSZ > EXCEPTION_STKSZ
  177. if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
  178. unsigned j = N_EXCEPTION_STACKS - 1;
  179. /*
  180. * Black magic. A large debug stack is composed of
  181. * multiple exception stack entries, which we
  182. * iterate through now. Dont look:
  183. */
  184. do {
  185. ++j;
  186. end -= EXCEPTION_STKSZ;
  187. ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
  188. } while (stack < end - EXCEPTION_STKSZ);
  189. if (*usedp & (1U << j))
  190. break;
  191. *usedp |= 1U << j;
  192. *idp = ids[j];
  193. return (unsigned long *)end;
  194. }
  195. #endif
  196. }
  197. return NULL;
  198. }
  199. #define MSG(txt) ops->warning(data, txt)
  200. /*
  201. * x86-64 can have upto three kernel stacks:
  202. * process stack
  203. * interrupt stack
  204. * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
  205. */
  206. static inline int valid_stack_ptr(struct thread_info *tinfo, void *p)
  207. {
  208. void *t = (void *)tinfo;
  209. return p > t && p < t + THREAD_SIZE - 3;
  210. }
  211. void dump_trace(struct task_struct *tsk, struct pt_regs *regs,
  212. unsigned long *stack,
  213. struct stacktrace_ops *ops, void *data)
  214. {
  215. const unsigned cpu = get_cpu();
  216. unsigned long *irqstack_end = (unsigned long*)cpu_pda(cpu)->irqstackptr;
  217. unsigned used = 0;
  218. struct thread_info *tinfo;
  219. if (!tsk)
  220. tsk = current;
  221. if (!stack) {
  222. unsigned long dummy;
  223. stack = &dummy;
  224. if (tsk && tsk != current)
  225. stack = (unsigned long *)tsk->thread.rsp;
  226. }
  227. /*
  228. * Print function call entries within a stack. 'cond' is the
  229. * "end of stackframe" condition, that the 'stack++'
  230. * iteration will eventually trigger.
  231. */
  232. #define HANDLE_STACK(cond) \
  233. do while (cond) { \
  234. unsigned long addr = *stack++; \
  235. /* Use unlocked access here because except for NMIs \
  236. we should be already protected against module unloads */ \
  237. if (__kernel_text_address(addr)) { \
  238. /* \
  239. * If the address is either in the text segment of the \
  240. * kernel, or in the region which contains vmalloc'ed \
  241. * memory, it *may* be the address of a calling \
  242. * routine; if so, print it so that someone tracing \
  243. * down the cause of the crash will be able to figure \
  244. * out the call path that was taken. \
  245. */ \
  246. ops->address(data, addr); \
  247. } \
  248. } while (0)
  249. /*
  250. * Print function call entries in all stacks, starting at the
  251. * current stack address. If the stacks consist of nested
  252. * exceptions
  253. */
  254. for (;;) {
  255. char *id;
  256. unsigned long *estack_end;
  257. estack_end = in_exception_stack(cpu, (unsigned long)stack,
  258. &used, &id);
  259. if (estack_end) {
  260. if (ops->stack(data, id) < 0)
  261. break;
  262. HANDLE_STACK (stack < estack_end);
  263. ops->stack(data, "<EOE>");
  264. /*
  265. * We link to the next stack via the
  266. * second-to-last pointer (index -2 to end) in the
  267. * exception stack:
  268. */
  269. stack = (unsigned long *) estack_end[-2];
  270. continue;
  271. }
  272. if (irqstack_end) {
  273. unsigned long *irqstack;
  274. irqstack = irqstack_end -
  275. (IRQSTACKSIZE - 64) / sizeof(*irqstack);
  276. if (stack >= irqstack && stack < irqstack_end) {
  277. if (ops->stack(data, "IRQ") < 0)
  278. break;
  279. HANDLE_STACK (stack < irqstack_end);
  280. /*
  281. * We link to the next stack (which would be
  282. * the process stack normally) the last
  283. * pointer (index -1 to end) in the IRQ stack:
  284. */
  285. stack = (unsigned long *) (irqstack_end[-1]);
  286. irqstack_end = NULL;
  287. ops->stack(data, "EOI");
  288. continue;
  289. }
  290. }
  291. break;
  292. }
  293. /*
  294. * This handles the process stack:
  295. */
  296. tinfo = task_thread_info(tsk);
  297. HANDLE_STACK (valid_stack_ptr(tinfo, stack));
  298. #undef HANDLE_STACK
  299. put_cpu();
  300. }
  301. EXPORT_SYMBOL(dump_trace);
  302. static void
  303. print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
  304. {
  305. print_symbol(msg, symbol);
  306. printk("\n");
  307. }
  308. static void print_trace_warning(void *data, char *msg)
  309. {
  310. printk("%s\n", msg);
  311. }
  312. static int print_trace_stack(void *data, char *name)
  313. {
  314. printk(" <%s> ", name);
  315. return 0;
  316. }
  317. static void print_trace_address(void *data, unsigned long addr)
  318. {
  319. printk_address(addr);
  320. }
  321. static struct stacktrace_ops print_trace_ops = {
  322. .warning = print_trace_warning,
  323. .warning_symbol = print_trace_warning_symbol,
  324. .stack = print_trace_stack,
  325. .address = print_trace_address,
  326. };
  327. void
  328. show_trace(struct task_struct *tsk, struct pt_regs *regs, unsigned long *stack)
  329. {
  330. printk("\nCall Trace:\n");
  331. dump_trace(tsk, regs, stack, &print_trace_ops, NULL);
  332. printk("\n");
  333. }
  334. static void
  335. _show_stack(struct task_struct *tsk, struct pt_regs *regs, unsigned long *rsp)
  336. {
  337. unsigned long *stack;
  338. int i;
  339. const int cpu = smp_processor_id();
  340. unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
  341. unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
  342. // debugging aid: "show_stack(NULL, NULL);" prints the
  343. // back trace for this cpu.
  344. if (rsp == NULL) {
  345. if (tsk)
  346. rsp = (unsigned long *)tsk->thread.rsp;
  347. else
  348. rsp = (unsigned long *)&rsp;
  349. }
  350. stack = rsp;
  351. for(i=0; i < kstack_depth_to_print; i++) {
  352. if (stack >= irqstack && stack <= irqstack_end) {
  353. if (stack == irqstack_end) {
  354. stack = (unsigned long *) (irqstack_end[-1]);
  355. printk(" <EOI> ");
  356. }
  357. } else {
  358. if (((long) stack & (THREAD_SIZE-1)) == 0)
  359. break;
  360. }
  361. if (i && ((i % 4) == 0))
  362. printk("\n");
  363. printk(" %016lx", *stack++);
  364. touch_nmi_watchdog();
  365. }
  366. show_trace(tsk, regs, rsp);
  367. }
  368. void show_stack(struct task_struct *tsk, unsigned long * rsp)
  369. {
  370. _show_stack(tsk, NULL, rsp);
  371. }
  372. /*
  373. * The architecture-independent dump_stack generator
  374. */
  375. void dump_stack(void)
  376. {
  377. unsigned long dummy;
  378. show_trace(NULL, NULL, &dummy);
  379. }
  380. EXPORT_SYMBOL(dump_stack);
  381. void show_registers(struct pt_regs *regs)
  382. {
  383. int i;
  384. int in_kernel = !user_mode(regs);
  385. unsigned long rsp;
  386. const int cpu = smp_processor_id();
  387. struct task_struct *cur = cpu_pda(cpu)->pcurrent;
  388. rsp = regs->rsp;
  389. printk("CPU %d ", cpu);
  390. __show_regs(regs);
  391. printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
  392. cur->comm, cur->pid, task_thread_info(cur), cur);
  393. /*
  394. * When in-kernel, we also print out the stack and code at the
  395. * time of the fault..
  396. */
  397. if (in_kernel) {
  398. printk("Stack: ");
  399. _show_stack(NULL, regs, (unsigned long*)rsp);
  400. printk("\nCode: ");
  401. if (regs->rip < PAGE_OFFSET)
  402. goto bad;
  403. for (i=0; i<20; i++) {
  404. unsigned char c;
  405. if (__get_user(c, &((unsigned char*)regs->rip)[i])) {
  406. bad:
  407. printk(" Bad RIP value.");
  408. break;
  409. }
  410. printk("%02x ", c);
  411. }
  412. }
  413. printk("\n");
  414. }
  415. int is_valid_bugaddr(unsigned long rip)
  416. {
  417. unsigned short ud2;
  418. if (__copy_from_user(&ud2, (const void __user *) rip, sizeof(ud2)))
  419. return 0;
  420. return ud2 == 0x0b0f;
  421. }
  422. #ifdef CONFIG_BUG
  423. void out_of_line_bug(void)
  424. {
  425. BUG();
  426. }
  427. EXPORT_SYMBOL(out_of_line_bug);
  428. #endif
  429. static DEFINE_SPINLOCK(die_lock);
  430. static int die_owner = -1;
  431. static unsigned int die_nest_count;
  432. unsigned __kprobes long oops_begin(void)
  433. {
  434. int cpu = smp_processor_id();
  435. unsigned long flags;
  436. oops_enter();
  437. /* racy, but better than risking deadlock. */
  438. local_irq_save(flags);
  439. if (!spin_trylock(&die_lock)) {
  440. if (cpu == die_owner)
  441. /* nested oops. should stop eventually */;
  442. else
  443. spin_lock(&die_lock);
  444. }
  445. die_nest_count++;
  446. die_owner = cpu;
  447. console_verbose();
  448. bust_spinlocks(1);
  449. return flags;
  450. }
  451. void __kprobes oops_end(unsigned long flags)
  452. {
  453. die_owner = -1;
  454. bust_spinlocks(0);
  455. die_nest_count--;
  456. if (die_nest_count)
  457. /* We still own the lock */
  458. local_irq_restore(flags);
  459. else
  460. /* Nest count reaches zero, release the lock. */
  461. spin_unlock_irqrestore(&die_lock, flags);
  462. if (panic_on_oops)
  463. panic("Fatal exception");
  464. oops_exit();
  465. }
  466. void __kprobes __die(const char * str, struct pt_regs * regs, long err)
  467. {
  468. static int die_counter;
  469. printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
  470. #ifdef CONFIG_PREEMPT
  471. printk("PREEMPT ");
  472. #endif
  473. #ifdef CONFIG_SMP
  474. printk("SMP ");
  475. #endif
  476. #ifdef CONFIG_DEBUG_PAGEALLOC
  477. printk("DEBUG_PAGEALLOC");
  478. #endif
  479. printk("\n");
  480. notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV);
  481. show_registers(regs);
  482. /* Executive summary in case the oops scrolled away */
  483. printk(KERN_ALERT "RIP ");
  484. printk_address(regs->rip);
  485. printk(" RSP <%016lx>\n", regs->rsp);
  486. if (kexec_should_crash(current))
  487. crash_kexec(regs);
  488. }
  489. void die(const char * str, struct pt_regs * regs, long err)
  490. {
  491. unsigned long flags = oops_begin();
  492. if (!user_mode(regs))
  493. report_bug(regs->rip);
  494. __die(str, regs, err);
  495. oops_end(flags);
  496. do_exit(SIGSEGV);
  497. }
  498. void __kprobes die_nmi(char *str, struct pt_regs *regs, int do_panic)
  499. {
  500. unsigned long flags = oops_begin();
  501. /*
  502. * We are in trouble anyway, lets at least try
  503. * to get a message out.
  504. */
  505. printk(str, smp_processor_id());
  506. show_registers(regs);
  507. if (kexec_should_crash(current))
  508. crash_kexec(regs);
  509. if (do_panic || panic_on_oops)
  510. panic("Non maskable interrupt");
  511. oops_end(flags);
  512. nmi_exit();
  513. local_irq_enable();
  514. do_exit(SIGSEGV);
  515. }
  516. static void __kprobes do_trap(int trapnr, int signr, char *str,
  517. struct pt_regs * regs, long error_code,
  518. siginfo_t *info)
  519. {
  520. struct task_struct *tsk = current;
  521. if (user_mode(regs)) {
  522. /*
  523. * We want error_code and trap_no set for userspace
  524. * faults and kernelspace faults which result in
  525. * die(), but not kernelspace faults which are fixed
  526. * up. die() gives the process no chance to handle
  527. * the signal and notice the kernel fault information,
  528. * so that won't result in polluting the information
  529. * about previously queued, but not yet delivered,
  530. * faults. See also do_general_protection below.
  531. */
  532. tsk->thread.error_code = error_code;
  533. tsk->thread.trap_no = trapnr;
  534. if (exception_trace && unhandled_signal(tsk, signr))
  535. printk(KERN_INFO
  536. "%s[%d] trap %s rip:%lx rsp:%lx error:%lx\n",
  537. tsk->comm, tsk->pid, str,
  538. regs->rip, regs->rsp, error_code);
  539. if (info)
  540. force_sig_info(signr, info, tsk);
  541. else
  542. force_sig(signr, tsk);
  543. return;
  544. }
  545. /* kernel trap */
  546. {
  547. const struct exception_table_entry *fixup;
  548. fixup = search_exception_tables(regs->rip);
  549. if (fixup)
  550. regs->rip = fixup->fixup;
  551. else {
  552. tsk->thread.error_code = error_code;
  553. tsk->thread.trap_no = trapnr;
  554. die(str, regs, error_code);
  555. }
  556. return;
  557. }
  558. }
  559. #define DO_ERROR(trapnr, signr, str, name) \
  560. asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
  561. { \
  562. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
  563. == NOTIFY_STOP) \
  564. return; \
  565. conditional_sti(regs); \
  566. do_trap(trapnr, signr, str, regs, error_code, NULL); \
  567. }
  568. #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
  569. asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
  570. { \
  571. siginfo_t info; \
  572. info.si_signo = signr; \
  573. info.si_errno = 0; \
  574. info.si_code = sicode; \
  575. info.si_addr = (void __user *)siaddr; \
  576. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
  577. == NOTIFY_STOP) \
  578. return; \
  579. conditional_sti(regs); \
  580. do_trap(trapnr, signr, str, regs, error_code, &info); \
  581. }
  582. DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->rip)
  583. DO_ERROR( 4, SIGSEGV, "overflow", overflow)
  584. DO_ERROR( 5, SIGSEGV, "bounds", bounds)
  585. DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->rip)
  586. DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
  587. DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
  588. DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
  589. DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
  590. DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
  591. DO_ERROR(18, SIGSEGV, "reserved", reserved)
  592. /* Runs on IST stack */
  593. asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
  594. {
  595. if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
  596. 12, SIGBUS) == NOTIFY_STOP)
  597. return;
  598. preempt_conditional_sti(regs);
  599. do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
  600. preempt_conditional_cli(regs);
  601. }
  602. asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
  603. {
  604. static const char str[] = "double fault";
  605. struct task_struct *tsk = current;
  606. /* Return not checked because double check cannot be ignored */
  607. notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
  608. tsk->thread.error_code = error_code;
  609. tsk->thread.trap_no = 8;
  610. /* This is always a kernel trap and never fixable (and thus must
  611. never return). */
  612. for (;;)
  613. die(str, regs, error_code);
  614. }
  615. asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
  616. long error_code)
  617. {
  618. struct task_struct *tsk = current;
  619. conditional_sti(regs);
  620. if (user_mode(regs)) {
  621. tsk->thread.error_code = error_code;
  622. tsk->thread.trap_no = 13;
  623. if (exception_trace && unhandled_signal(tsk, SIGSEGV))
  624. printk(KERN_INFO
  625. "%s[%d] general protection rip:%lx rsp:%lx error:%lx\n",
  626. tsk->comm, tsk->pid,
  627. regs->rip, regs->rsp, error_code);
  628. force_sig(SIGSEGV, tsk);
  629. return;
  630. }
  631. /* kernel gp */
  632. {
  633. const struct exception_table_entry *fixup;
  634. fixup = search_exception_tables(regs->rip);
  635. if (fixup) {
  636. regs->rip = fixup->fixup;
  637. return;
  638. }
  639. tsk->thread.error_code = error_code;
  640. tsk->thread.trap_no = 13;
  641. if (notify_die(DIE_GPF, "general protection fault", regs,
  642. error_code, 13, SIGSEGV) == NOTIFY_STOP)
  643. return;
  644. die("general protection fault", regs, error_code);
  645. }
  646. }
  647. static __kprobes void
  648. mem_parity_error(unsigned char reason, struct pt_regs * regs)
  649. {
  650. printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
  651. reason);
  652. printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
  653. if (panic_on_unrecovered_nmi)
  654. panic("NMI: Not continuing");
  655. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  656. /* Clear and disable the memory parity error line. */
  657. reason = (reason & 0xf) | 4;
  658. outb(reason, 0x61);
  659. }
  660. static __kprobes void
  661. io_check_error(unsigned char reason, struct pt_regs * regs)
  662. {
  663. printk("NMI: IOCK error (debug interrupt?)\n");
  664. show_registers(regs);
  665. /* Re-enable the IOCK line, wait for a few seconds */
  666. reason = (reason & 0xf) | 8;
  667. outb(reason, 0x61);
  668. mdelay(2000);
  669. reason &= ~8;
  670. outb(reason, 0x61);
  671. }
  672. static __kprobes void
  673. unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
  674. {
  675. printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
  676. reason);
  677. printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
  678. if (panic_on_unrecovered_nmi)
  679. panic("NMI: Not continuing");
  680. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  681. }
  682. /* Runs on IST stack. This code must keep interrupts off all the time.
  683. Nested NMIs are prevented by the CPU. */
  684. asmlinkage __kprobes void default_do_nmi(struct pt_regs *regs)
  685. {
  686. unsigned char reason = 0;
  687. int cpu;
  688. cpu = smp_processor_id();
  689. /* Only the BSP gets external NMIs from the system. */
  690. if (!cpu)
  691. reason = get_nmi_reason();
  692. if (!(reason & 0xc0)) {
  693. if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
  694. == NOTIFY_STOP)
  695. return;
  696. /*
  697. * Ok, so this is none of the documented NMI sources,
  698. * so it must be the NMI watchdog.
  699. */
  700. if (nmi_watchdog_tick(regs,reason))
  701. return;
  702. if (!do_nmi_callback(regs,cpu))
  703. unknown_nmi_error(reason, regs);
  704. return;
  705. }
  706. if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
  707. return;
  708. /* AK: following checks seem to be broken on modern chipsets. FIXME */
  709. if (reason & 0x80)
  710. mem_parity_error(reason, regs);
  711. if (reason & 0x40)
  712. io_check_error(reason, regs);
  713. }
  714. /* runs on IST stack. */
  715. asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code)
  716. {
  717. if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
  718. return;
  719. }
  720. preempt_conditional_sti(regs);
  721. do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
  722. preempt_conditional_cli(regs);
  723. }
  724. /* Help handler running on IST stack to switch back to user stack
  725. for scheduling or signal handling. The actual stack switch is done in
  726. entry.S */
  727. asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
  728. {
  729. struct pt_regs *regs = eregs;
  730. /* Did already sync */
  731. if (eregs == (struct pt_regs *)eregs->rsp)
  732. ;
  733. /* Exception from user space */
  734. else if (user_mode(eregs))
  735. regs = task_pt_regs(current);
  736. /* Exception from kernel and interrupts are enabled. Move to
  737. kernel process stack. */
  738. else if (eregs->eflags & X86_EFLAGS_IF)
  739. regs = (struct pt_regs *)(eregs->rsp -= sizeof(struct pt_regs));
  740. if (eregs != regs)
  741. *regs = *eregs;
  742. return regs;
  743. }
  744. /* runs on IST stack. */
  745. asmlinkage void __kprobes do_debug(struct pt_regs * regs,
  746. unsigned long error_code)
  747. {
  748. unsigned long condition;
  749. struct task_struct *tsk = current;
  750. siginfo_t info;
  751. get_debugreg(condition, 6);
  752. if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
  753. SIGTRAP) == NOTIFY_STOP)
  754. return;
  755. preempt_conditional_sti(regs);
  756. /* Mask out spurious debug traps due to lazy DR7 setting */
  757. if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
  758. if (!tsk->thread.debugreg7) {
  759. goto clear_dr7;
  760. }
  761. }
  762. tsk->thread.debugreg6 = condition;
  763. /* Mask out spurious TF errors due to lazy TF clearing */
  764. if (condition & DR_STEP) {
  765. /*
  766. * The TF error should be masked out only if the current
  767. * process is not traced and if the TRAP flag has been set
  768. * previously by a tracing process (condition detected by
  769. * the PT_DTRACE flag); remember that the i386 TRAP flag
  770. * can be modified by the process itself in user mode,
  771. * allowing programs to debug themselves without the ptrace()
  772. * interface.
  773. */
  774. if (!user_mode(regs))
  775. goto clear_TF_reenable;
  776. /*
  777. * Was the TF flag set by a debugger? If so, clear it now,
  778. * so that register information is correct.
  779. */
  780. if (tsk->ptrace & PT_DTRACE) {
  781. regs->eflags &= ~TF_MASK;
  782. tsk->ptrace &= ~PT_DTRACE;
  783. }
  784. }
  785. /* Ok, finally something we can handle */
  786. tsk->thread.trap_no = 1;
  787. tsk->thread.error_code = error_code;
  788. info.si_signo = SIGTRAP;
  789. info.si_errno = 0;
  790. info.si_code = TRAP_BRKPT;
  791. info.si_addr = user_mode(regs) ? (void __user *)regs->rip : NULL;
  792. force_sig_info(SIGTRAP, &info, tsk);
  793. clear_dr7:
  794. set_debugreg(0UL, 7);
  795. preempt_conditional_cli(regs);
  796. return;
  797. clear_TF_reenable:
  798. set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
  799. regs->eflags &= ~TF_MASK;
  800. preempt_conditional_cli(regs);
  801. }
  802. static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
  803. {
  804. const struct exception_table_entry *fixup;
  805. fixup = search_exception_tables(regs->rip);
  806. if (fixup) {
  807. regs->rip = fixup->fixup;
  808. return 1;
  809. }
  810. notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
  811. /* Illegal floating point operation in the kernel */
  812. current->thread.trap_no = trapnr;
  813. die(str, regs, 0);
  814. return 0;
  815. }
  816. /*
  817. * Note that we play around with the 'TS' bit in an attempt to get
  818. * the correct behaviour even in the presence of the asynchronous
  819. * IRQ13 behaviour
  820. */
  821. asmlinkage void do_coprocessor_error(struct pt_regs *regs)
  822. {
  823. void __user *rip = (void __user *)(regs->rip);
  824. struct task_struct * task;
  825. siginfo_t info;
  826. unsigned short cwd, swd;
  827. conditional_sti(regs);
  828. if (!user_mode(regs) &&
  829. kernel_math_error(regs, "kernel x87 math error", 16))
  830. return;
  831. /*
  832. * Save the info for the exception handler and clear the error.
  833. */
  834. task = current;
  835. save_init_fpu(task);
  836. task->thread.trap_no = 16;
  837. task->thread.error_code = 0;
  838. info.si_signo = SIGFPE;
  839. info.si_errno = 0;
  840. info.si_code = __SI_FAULT;
  841. info.si_addr = rip;
  842. /*
  843. * (~cwd & swd) will mask out exceptions that are not set to unmasked
  844. * status. 0x3f is the exception bits in these regs, 0x200 is the
  845. * C1 reg you need in case of a stack fault, 0x040 is the stack
  846. * fault bit. We should only be taking one exception at a time,
  847. * so if this combination doesn't produce any single exception,
  848. * then we have a bad program that isn't synchronizing its FPU usage
  849. * and it will suffer the consequences since we won't be able to
  850. * fully reproduce the context of the exception
  851. */
  852. cwd = get_fpu_cwd(task);
  853. swd = get_fpu_swd(task);
  854. switch (swd & ~cwd & 0x3f) {
  855. case 0x000:
  856. default:
  857. break;
  858. case 0x001: /* Invalid Op */
  859. /*
  860. * swd & 0x240 == 0x040: Stack Underflow
  861. * swd & 0x240 == 0x240: Stack Overflow
  862. * User must clear the SF bit (0x40) if set
  863. */
  864. info.si_code = FPE_FLTINV;
  865. break;
  866. case 0x002: /* Denormalize */
  867. case 0x010: /* Underflow */
  868. info.si_code = FPE_FLTUND;
  869. break;
  870. case 0x004: /* Zero Divide */
  871. info.si_code = FPE_FLTDIV;
  872. break;
  873. case 0x008: /* Overflow */
  874. info.si_code = FPE_FLTOVF;
  875. break;
  876. case 0x020: /* Precision */
  877. info.si_code = FPE_FLTRES;
  878. break;
  879. }
  880. force_sig_info(SIGFPE, &info, task);
  881. }
  882. asmlinkage void bad_intr(void)
  883. {
  884. printk("bad interrupt");
  885. }
  886. asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
  887. {
  888. void __user *rip = (void __user *)(regs->rip);
  889. struct task_struct * task;
  890. siginfo_t info;
  891. unsigned short mxcsr;
  892. conditional_sti(regs);
  893. if (!user_mode(regs) &&
  894. kernel_math_error(regs, "kernel simd math error", 19))
  895. return;
  896. /*
  897. * Save the info for the exception handler and clear the error.
  898. */
  899. task = current;
  900. save_init_fpu(task);
  901. task->thread.trap_no = 19;
  902. task->thread.error_code = 0;
  903. info.si_signo = SIGFPE;
  904. info.si_errno = 0;
  905. info.si_code = __SI_FAULT;
  906. info.si_addr = rip;
  907. /*
  908. * The SIMD FPU exceptions are handled a little differently, as there
  909. * is only a single status/control register. Thus, to determine which
  910. * unmasked exception was caught we must mask the exception mask bits
  911. * at 0x1f80, and then use these to mask the exception bits at 0x3f.
  912. */
  913. mxcsr = get_fpu_mxcsr(task);
  914. switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
  915. case 0x000:
  916. default:
  917. break;
  918. case 0x001: /* Invalid Op */
  919. info.si_code = FPE_FLTINV;
  920. break;
  921. case 0x002: /* Denormalize */
  922. case 0x010: /* Underflow */
  923. info.si_code = FPE_FLTUND;
  924. break;
  925. case 0x004: /* Zero Divide */
  926. info.si_code = FPE_FLTDIV;
  927. break;
  928. case 0x008: /* Overflow */
  929. info.si_code = FPE_FLTOVF;
  930. break;
  931. case 0x020: /* Precision */
  932. info.si_code = FPE_FLTRES;
  933. break;
  934. }
  935. force_sig_info(SIGFPE, &info, task);
  936. }
  937. asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
  938. {
  939. }
  940. asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
  941. {
  942. }
  943. asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
  944. {
  945. }
  946. /*
  947. * 'math_state_restore()' saves the current math information in the
  948. * old math state array, and gets the new ones from the current task
  949. *
  950. * Careful.. There are problems with IBM-designed IRQ13 behaviour.
  951. * Don't touch unless you *really* know how it works.
  952. */
  953. asmlinkage void math_state_restore(void)
  954. {
  955. struct task_struct *me = current;
  956. clts(); /* Allow maths ops (or we recurse) */
  957. if (!used_math())
  958. init_fpu(me);
  959. restore_fpu_checking(&me->thread.i387.fxsave);
  960. task_thread_info(me)->status |= TS_USEDFPU;
  961. me->fpu_counter++;
  962. }
  963. void __init trap_init(void)
  964. {
  965. set_intr_gate(0,&divide_error);
  966. set_intr_gate_ist(1,&debug,DEBUG_STACK);
  967. set_intr_gate_ist(2,&nmi,NMI_STACK);
  968. set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */
  969. set_system_gate(4,&overflow); /* int4 can be called from all */
  970. set_intr_gate(5,&bounds);
  971. set_intr_gate(6,&invalid_op);
  972. set_intr_gate(7,&device_not_available);
  973. set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
  974. set_intr_gate(9,&coprocessor_segment_overrun);
  975. set_intr_gate(10,&invalid_TSS);
  976. set_intr_gate(11,&segment_not_present);
  977. set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
  978. set_intr_gate(13,&general_protection);
  979. set_intr_gate(14,&page_fault);
  980. set_intr_gate(15,&spurious_interrupt_bug);
  981. set_intr_gate(16,&coprocessor_error);
  982. set_intr_gate(17,&alignment_check);
  983. #ifdef CONFIG_X86_MCE
  984. set_intr_gate_ist(18,&machine_check, MCE_STACK);
  985. #endif
  986. set_intr_gate(19,&simd_coprocessor_error);
  987. #ifdef CONFIG_IA32_EMULATION
  988. set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
  989. #endif
  990. /*
  991. * Should be a barrier for any external CPU state.
  992. */
  993. cpu_init();
  994. }
  995. static int __init oops_setup(char *s)
  996. {
  997. if (!s)
  998. return -EINVAL;
  999. if (!strcmp(s, "panic"))
  1000. panic_on_oops = 1;
  1001. return 0;
  1002. }
  1003. early_param("oops", oops_setup);
  1004. static int __init kstack_setup(char *s)
  1005. {
  1006. if (!s)
  1007. return -EINVAL;
  1008. kstack_depth_to_print = simple_strtoul(s,NULL,0);
  1009. return 0;
  1010. }
  1011. early_param("kstack", kstack_setup);