traps_64.c 29 KB

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