traps.c 30 KB

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