traps_64.c 29 KB

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