traps.c 30 KB

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