traps_64.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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("\nCall Trace:\n");
  311. dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
  312. printk("\n");
  313. }
  314. void show_trace(struct task_struct *task, struct pt_regs *regs,
  315. unsigned long *stack, unsigned long bp)
  316. {
  317. show_trace_log_lvl(task, regs, stack, bp, "");
  318. }
  319. static void
  320. show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
  321. unsigned long *sp, unsigned long bp, char *log_lvl)
  322. {
  323. unsigned long *stack;
  324. int i;
  325. const int cpu = smp_processor_id();
  326. unsigned long *irqstack_end =
  327. (unsigned long *) (cpu_pda(cpu)->irqstackptr);
  328. unsigned long *irqstack =
  329. (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
  330. /*
  331. * debugging aid: "show_stack(NULL, NULL);" prints the
  332. * back trace for this cpu.
  333. */
  334. if (sp == NULL) {
  335. if (task)
  336. sp = (unsigned long *)task->thread.sp;
  337. else
  338. sp = (unsigned long *)&sp;
  339. }
  340. stack = sp;
  341. for (i = 0; i < kstack_depth_to_print; i++) {
  342. if (stack >= irqstack && stack <= irqstack_end) {
  343. if (stack == irqstack_end) {
  344. stack = (unsigned long *) (irqstack_end[-1]);
  345. printk(" <EOI> ");
  346. }
  347. } else {
  348. if (((long) stack & (THREAD_SIZE-1)) == 0)
  349. break;
  350. }
  351. if (i && ((i % 4) == 0))
  352. printk("\n");
  353. printk(" %016lx", *stack++);
  354. touch_nmi_watchdog();
  355. }
  356. show_trace_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("\n");
  405. printk(KERN_EMERG "Code: ");
  406. ip = (u8 *)regs->ip - code_prologue;
  407. if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
  408. /* try starting at RIP */
  409. ip = (u8 *)regs->ip;
  410. code_len = code_len - code_prologue + 1;
  411. }
  412. for (i = 0; i < code_len; i++, ip++) {
  413. if (ip < (u8 *)PAGE_OFFSET ||
  414. probe_kernel_address(ip, c)) {
  415. printk(" Bad RIP value.");
  416. break;
  417. }
  418. if (ip == (u8 *)regs->ip)
  419. printk("<%02x> ", c);
  420. else
  421. printk("%02x ", c);
  422. }
  423. }
  424. printk("\n");
  425. }
  426. int is_valid_bugaddr(unsigned long ip)
  427. {
  428. unsigned short ud2;
  429. if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
  430. return 0;
  431. return ud2 == 0x0b0f;
  432. }
  433. static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
  434. static int die_owner = -1;
  435. static unsigned int die_nest_count;
  436. unsigned __kprobes long oops_begin(void)
  437. {
  438. int cpu;
  439. unsigned long flags;
  440. oops_enter();
  441. /* racy, but better than risking deadlock. */
  442. raw_local_irq_save(flags);
  443. cpu = smp_processor_id();
  444. if (!__raw_spin_trylock(&die_lock)) {
  445. if (cpu == die_owner)
  446. /* nested oops. should stop eventually */;
  447. else
  448. __raw_spin_lock(&die_lock);
  449. }
  450. die_nest_count++;
  451. die_owner = cpu;
  452. console_verbose();
  453. bust_spinlocks(1);
  454. return flags;
  455. }
  456. void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
  457. {
  458. die_owner = -1;
  459. bust_spinlocks(0);
  460. die_nest_count--;
  461. if (!die_nest_count)
  462. /* Nest count reaches zero, release the lock. */
  463. __raw_spin_unlock(&die_lock);
  464. raw_local_irq_restore(flags);
  465. if (!regs) {
  466. oops_exit();
  467. return;
  468. }
  469. if (panic_on_oops)
  470. panic("Fatal exception");
  471. oops_exit();
  472. do_exit(signr);
  473. }
  474. int __kprobes __die(const char *str, struct pt_regs *regs, long err)
  475. {
  476. printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff, ++die_counter);
  477. #ifdef CONFIG_PREEMPT
  478. printk("PREEMPT ");
  479. #endif
  480. #ifdef CONFIG_SMP
  481. printk("SMP ");
  482. #endif
  483. #ifdef CONFIG_DEBUG_PAGEALLOC
  484. printk("DEBUG_PAGEALLOC");
  485. #endif
  486. printk("\n");
  487. if (notify_die(DIE_OOPS, str, regs, err,
  488. current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
  489. return 1;
  490. show_registers(regs);
  491. add_taint(TAINT_DIE);
  492. /* Executive summary in case the oops scrolled away */
  493. printk(KERN_ALERT "RIP ");
  494. printk_address(regs->ip, 1);
  495. printk(" RSP <%016lx>\n", regs->sp);
  496. if (kexec_should_crash(current))
  497. crash_kexec(regs);
  498. return 0;
  499. }
  500. void die(const char *str, struct pt_regs *regs, long err)
  501. {
  502. unsigned long flags = oops_begin();
  503. if (!user_mode(regs))
  504. report_bug(regs->ip, regs);
  505. if (__die(str, regs, err))
  506. regs = NULL;
  507. oops_end(flags, regs, SIGSEGV);
  508. }
  509. notrace __kprobes void
  510. die_nmi(char *str, struct pt_regs *regs, int do_panic)
  511. {
  512. unsigned long flags;
  513. if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
  514. return;
  515. flags = oops_begin();
  516. /*
  517. * We are in trouble anyway, lets at least try
  518. * to get a message out.
  519. */
  520. printk(KERN_EMERG "%s", str);
  521. printk(" on CPU%d, ip %08lx, registers:\n",
  522. smp_processor_id(), regs->ip);
  523. show_registers(regs);
  524. if (kexec_should_crash(current))
  525. crash_kexec(regs);
  526. if (do_panic || panic_on_oops)
  527. panic("Non maskable interrupt");
  528. oops_end(flags, NULL, SIGBUS);
  529. nmi_exit();
  530. local_irq_enable();
  531. do_exit(SIGBUS);
  532. }
  533. static void __kprobes
  534. do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
  535. long error_code, siginfo_t *info)
  536. {
  537. struct task_struct *tsk = current;
  538. if (!user_mode(regs))
  539. goto kernel_trap;
  540. /*
  541. * We want error_code and trap_no set for userspace faults and
  542. * kernelspace faults which result in die(), but not
  543. * kernelspace faults which are fixed up. die() gives the
  544. * process no chance to handle the signal and notice the
  545. * kernel fault information, so that won't result in polluting
  546. * the information about previously queued, but not yet
  547. * delivered, faults. See also do_general_protection below.
  548. */
  549. tsk->thread.error_code = error_code;
  550. tsk->thread.trap_no = trapnr;
  551. if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
  552. printk_ratelimit()) {
  553. printk(KERN_INFO
  554. "%s[%d] trap %s ip:%lx sp:%lx error:%lx",
  555. tsk->comm, tsk->pid, str,
  556. regs->ip, regs->sp, error_code);
  557. print_vma_addr(" in ", regs->ip);
  558. printk("\n");
  559. }
  560. if (info)
  561. force_sig_info(signr, info, tsk);
  562. else
  563. force_sig(signr, tsk);
  564. return;
  565. kernel_trap:
  566. if (!fixup_exception(regs)) {
  567. tsk->thread.error_code = error_code;
  568. tsk->thread.trap_no = trapnr;
  569. die(str, regs, error_code);
  570. }
  571. return;
  572. }
  573. #define DO_ERROR(trapnr, signr, str, name) \
  574. asmlinkage void do_##name(struct pt_regs *regs, long error_code) \
  575. { \
  576. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
  577. == NOTIFY_STOP) \
  578. return; \
  579. conditional_sti(regs); \
  580. do_trap(trapnr, signr, str, regs, error_code, NULL); \
  581. }
  582. #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
  583. asmlinkage void do_##name(struct pt_regs *regs, long error_code) \
  584. { \
  585. siginfo_t info; \
  586. info.si_signo = signr; \
  587. info.si_errno = 0; \
  588. info.si_code = sicode; \
  589. info.si_addr = (void __user *)siaddr; \
  590. trace_hardirqs_fixup(); \
  591. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
  592. == NOTIFY_STOP) \
  593. return; \
  594. conditional_sti(regs); \
  595. do_trap(trapnr, signr, str, regs, error_code, &info); \
  596. }
  597. DO_ERROR_INFO(0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
  598. DO_ERROR(4, SIGSEGV, "overflow", overflow)
  599. DO_ERROR(5, SIGSEGV, "bounds", bounds)
  600. DO_ERROR_INFO(6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
  601. DO_ERROR(9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
  602. DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
  603. DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
  604. DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
  605. /* Runs on IST stack */
  606. asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
  607. {
  608. if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
  609. 12, SIGBUS) == NOTIFY_STOP)
  610. return;
  611. preempt_conditional_sti(regs);
  612. do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
  613. preempt_conditional_cli(regs);
  614. }
  615. asmlinkage void do_double_fault(struct pt_regs *regs, long error_code)
  616. {
  617. static const char str[] = "double fault";
  618. struct task_struct *tsk = current;
  619. /* Return not checked because double check cannot be ignored */
  620. notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
  621. tsk->thread.error_code = error_code;
  622. tsk->thread.trap_no = 8;
  623. /* This is always a kernel trap and never fixable (and thus must
  624. never return). */
  625. for (;;)
  626. die(str, regs, error_code);
  627. }
  628. asmlinkage void __kprobes
  629. do_general_protection(struct pt_regs *regs, long error_code)
  630. {
  631. struct task_struct *tsk;
  632. conditional_sti(regs);
  633. tsk = current;
  634. if (!user_mode(regs))
  635. goto gp_in_kernel;
  636. tsk->thread.error_code = error_code;
  637. tsk->thread.trap_no = 13;
  638. if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
  639. printk_ratelimit()) {
  640. printk(KERN_INFO
  641. "%s[%d] general protection ip:%lx sp:%lx error:%lx",
  642. tsk->comm, tsk->pid,
  643. regs->ip, regs->sp, error_code);
  644. print_vma_addr(" in ", regs->ip);
  645. printk("\n");
  646. }
  647. force_sig(SIGSEGV, tsk);
  648. return;
  649. gp_in_kernel:
  650. if (fixup_exception(regs))
  651. return;
  652. tsk->thread.error_code = error_code;
  653. tsk->thread.trap_no = 13;
  654. if (notify_die(DIE_GPF, "general protection fault", regs,
  655. error_code, 13, SIGSEGV) == NOTIFY_STOP)
  656. return;
  657. die("general protection fault", regs, error_code);
  658. }
  659. static notrace __kprobes void
  660. mem_parity_error(unsigned char reason, struct pt_regs *regs)
  661. {
  662. printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
  663. reason);
  664. printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
  665. #if defined(CONFIG_EDAC)
  666. if (edac_handler_set()) {
  667. edac_atomic_assert_error();
  668. return;
  669. }
  670. #endif
  671. if (panic_on_unrecovered_nmi)
  672. panic("NMI: Not continuing");
  673. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  674. /* Clear and disable the memory parity error line. */
  675. reason = (reason & 0xf) | 4;
  676. outb(reason, 0x61);
  677. }
  678. static notrace __kprobes void
  679. io_check_error(unsigned char reason, struct pt_regs *regs)
  680. {
  681. printk("NMI: IOCK error (debug interrupt?)\n");
  682. show_registers(regs);
  683. /* Re-enable the IOCK line, wait for a few seconds */
  684. reason = (reason & 0xf) | 8;
  685. outb(reason, 0x61);
  686. mdelay(2000);
  687. reason &= ~8;
  688. outb(reason, 0x61);
  689. }
  690. static notrace __kprobes void
  691. unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
  692. {
  693. if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) ==
  694. NOTIFY_STOP)
  695. return;
  696. printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
  697. reason);
  698. printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
  699. if (panic_on_unrecovered_nmi)
  700. panic("NMI: Not continuing");
  701. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  702. }
  703. /* Runs on IST stack. This code must keep interrupts off all the time.
  704. Nested NMIs are prevented by the CPU. */
  705. asmlinkage notrace __kprobes void default_do_nmi(struct pt_regs *regs)
  706. {
  707. unsigned char reason = 0;
  708. int cpu;
  709. cpu = smp_processor_id();
  710. /* Only the BSP gets external NMIs from the system. */
  711. if (!cpu)
  712. reason = get_nmi_reason();
  713. if (!(reason & 0xc0)) {
  714. if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
  715. == NOTIFY_STOP)
  716. return;
  717. /*
  718. * Ok, so this is none of the documented NMI sources,
  719. * so it must be the NMI watchdog.
  720. */
  721. if (nmi_watchdog_tick(regs, reason))
  722. return;
  723. if (!do_nmi_callback(regs, cpu))
  724. unknown_nmi_error(reason, regs);
  725. return;
  726. }
  727. if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
  728. return;
  729. /* AK: following checks seem to be broken on modern chipsets. FIXME */
  730. if (reason & 0x80)
  731. mem_parity_error(reason, regs);
  732. if (reason & 0x40)
  733. io_check_error(reason, regs);
  734. }
  735. asmlinkage notrace __kprobes void
  736. do_nmi(struct pt_regs *regs, long error_code)
  737. {
  738. nmi_enter();
  739. add_pda(__nmi_count, 1);
  740. if (!ignore_nmis)
  741. default_do_nmi(regs);
  742. nmi_exit();
  743. }
  744. void stop_nmi(void)
  745. {
  746. acpi_nmi_disable();
  747. ignore_nmis++;
  748. }
  749. void restart_nmi(void)
  750. {
  751. ignore_nmis--;
  752. acpi_nmi_enable();
  753. }
  754. /* runs on IST stack. */
  755. asmlinkage void __kprobes do_int3(struct pt_regs *regs, long error_code)
  756. {
  757. trace_hardirqs_fixup();
  758. if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
  759. == NOTIFY_STOP)
  760. return;
  761. preempt_conditional_sti(regs);
  762. do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
  763. preempt_conditional_cli(regs);
  764. }
  765. /* Help handler running on IST stack to switch back to user stack
  766. for scheduling or signal handling. The actual stack switch is done in
  767. entry.S */
  768. asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
  769. {
  770. struct pt_regs *regs = eregs;
  771. /* Did already sync */
  772. if (eregs == (struct pt_regs *)eregs->sp)
  773. ;
  774. /* Exception from user space */
  775. else if (user_mode(eregs))
  776. regs = task_pt_regs(current);
  777. /* Exception from kernel and interrupts are enabled. Move to
  778. kernel process stack. */
  779. else if (eregs->flags & X86_EFLAGS_IF)
  780. regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
  781. if (eregs != regs)
  782. *regs = *eregs;
  783. return regs;
  784. }
  785. /* runs on IST stack. */
  786. asmlinkage void __kprobes do_debug(struct pt_regs *regs,
  787. unsigned long error_code)
  788. {
  789. struct task_struct *tsk = current;
  790. unsigned long condition;
  791. siginfo_t info;
  792. trace_hardirqs_fixup();
  793. get_debugreg(condition, 6);
  794. /*
  795. * The processor cleared BTF, so don't mark that we need it set.
  796. */
  797. clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
  798. tsk->thread.debugctlmsr = 0;
  799. if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
  800. SIGTRAP) == NOTIFY_STOP)
  801. return;
  802. preempt_conditional_sti(regs);
  803. /* Mask out spurious debug traps due to lazy DR7 setting */
  804. if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
  805. if (!tsk->thread.debugreg7)
  806. goto clear_dr7;
  807. }
  808. tsk->thread.debugreg6 = condition;
  809. /*
  810. * Single-stepping through TF: make sure we ignore any events in
  811. * kernel space (but re-enable TF when returning to user mode).
  812. */
  813. if (condition & DR_STEP) {
  814. if (!user_mode(regs))
  815. goto clear_TF_reenable;
  816. }
  817. /* Ok, finally something we can handle */
  818. tsk->thread.trap_no = 1;
  819. tsk->thread.error_code = error_code;
  820. info.si_signo = SIGTRAP;
  821. info.si_errno = 0;
  822. info.si_code = TRAP_BRKPT;
  823. info.si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
  824. force_sig_info(SIGTRAP, &info, tsk);
  825. clear_dr7:
  826. set_debugreg(0, 7);
  827. preempt_conditional_cli(regs);
  828. return;
  829. clear_TF_reenable:
  830. set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
  831. regs->flags &= ~X86_EFLAGS_TF;
  832. preempt_conditional_cli(regs);
  833. return;
  834. }
  835. static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
  836. {
  837. if (fixup_exception(regs))
  838. return 1;
  839. notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
  840. /* Illegal floating point operation in the kernel */
  841. current->thread.trap_no = trapnr;
  842. die(str, regs, 0);
  843. return 0;
  844. }
  845. /*
  846. * Note that we play around with the 'TS' bit in an attempt to get
  847. * the correct behaviour even in the presence of the asynchronous
  848. * IRQ13 behaviour
  849. */
  850. asmlinkage void do_coprocessor_error(struct pt_regs *regs)
  851. {
  852. void __user *ip = (void __user *)(regs->ip);
  853. struct task_struct *task;
  854. siginfo_t info;
  855. unsigned short cwd, swd;
  856. conditional_sti(regs);
  857. if (!user_mode(regs) &&
  858. kernel_math_error(regs, "kernel x87 math error", 16))
  859. return;
  860. /*
  861. * Save the info for the exception handler and clear the error.
  862. */
  863. task = current;
  864. save_init_fpu(task);
  865. task->thread.trap_no = 16;
  866. task->thread.error_code = 0;
  867. info.si_signo = SIGFPE;
  868. info.si_errno = 0;
  869. info.si_code = __SI_FAULT;
  870. info.si_addr = ip;
  871. /*
  872. * (~cwd & swd) will mask out exceptions that are not set to unmasked
  873. * status. 0x3f is the exception bits in these regs, 0x200 is the
  874. * C1 reg you need in case of a stack fault, 0x040 is the stack
  875. * fault bit. We should only be taking one exception at a time,
  876. * so if this combination doesn't produce any single exception,
  877. * then we have a bad program that isn't synchronizing its FPU usage
  878. * and it will suffer the consequences since we won't be able to
  879. * fully reproduce the context of the exception
  880. */
  881. cwd = get_fpu_cwd(task);
  882. swd = get_fpu_swd(task);
  883. switch (swd & ~cwd & 0x3f) {
  884. case 0x000: /* No unmasked exception */
  885. default: /* Multiple exceptions */
  886. break;
  887. case 0x001: /* Invalid Op */
  888. /*
  889. * swd & 0x240 == 0x040: Stack Underflow
  890. * swd & 0x240 == 0x240: Stack Overflow
  891. * User must clear the SF bit (0x40) if set
  892. */
  893. info.si_code = FPE_FLTINV;
  894. break;
  895. case 0x002: /* Denormalize */
  896. case 0x010: /* Underflow */
  897. info.si_code = FPE_FLTUND;
  898. break;
  899. case 0x004: /* Zero Divide */
  900. info.si_code = FPE_FLTDIV;
  901. break;
  902. case 0x008: /* Overflow */
  903. info.si_code = FPE_FLTOVF;
  904. break;
  905. case 0x020: /* Precision */
  906. info.si_code = FPE_FLTRES;
  907. break;
  908. }
  909. force_sig_info(SIGFPE, &info, task);
  910. }
  911. asmlinkage void bad_intr(void)
  912. {
  913. printk("bad interrupt");
  914. }
  915. asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
  916. {
  917. void __user *ip = (void __user *)(regs->ip);
  918. struct task_struct *task;
  919. siginfo_t info;
  920. unsigned short mxcsr;
  921. conditional_sti(regs);
  922. if (!user_mode(regs) &&
  923. kernel_math_error(regs, "kernel simd math error", 19))
  924. return;
  925. /*
  926. * Save the info for the exception handler and clear the error.
  927. */
  928. task = current;
  929. save_init_fpu(task);
  930. task->thread.trap_no = 19;
  931. task->thread.error_code = 0;
  932. info.si_signo = SIGFPE;
  933. info.si_errno = 0;
  934. info.si_code = __SI_FAULT;
  935. info.si_addr = ip;
  936. /*
  937. * The SIMD FPU exceptions are handled a little differently, as there
  938. * is only a single status/control register. Thus, to determine which
  939. * unmasked exception was caught we must mask the exception mask bits
  940. * at 0x1f80, and then use these to mask the exception bits at 0x3f.
  941. */
  942. mxcsr = get_fpu_mxcsr(task);
  943. switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
  944. case 0x000:
  945. default:
  946. break;
  947. case 0x001: /* Invalid Op */
  948. info.si_code = FPE_FLTINV;
  949. break;
  950. case 0x002: /* Denormalize */
  951. case 0x010: /* Underflow */
  952. info.si_code = FPE_FLTUND;
  953. break;
  954. case 0x004: /* Zero Divide */
  955. info.si_code = FPE_FLTDIV;
  956. break;
  957. case 0x008: /* Overflow */
  958. info.si_code = FPE_FLTOVF;
  959. break;
  960. case 0x020: /* Precision */
  961. info.si_code = FPE_FLTRES;
  962. break;
  963. }
  964. force_sig_info(SIGFPE, &info, task);
  965. }
  966. asmlinkage void do_spurious_interrupt_bug(struct pt_regs *regs)
  967. {
  968. }
  969. asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
  970. {
  971. }
  972. asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
  973. {
  974. }
  975. /*
  976. * 'math_state_restore()' saves the current math information in the
  977. * old math state array, and gets the new ones from the current task
  978. *
  979. * Careful.. There are problems with IBM-designed IRQ13 behaviour.
  980. * Don't touch unless you *really* know how it works.
  981. */
  982. asmlinkage void math_state_restore(void)
  983. {
  984. struct task_struct *me = current;
  985. if (!used_math()) {
  986. local_irq_enable();
  987. /*
  988. * does a slab alloc which can sleep
  989. */
  990. if (init_fpu(me)) {
  991. /*
  992. * ran out of memory!
  993. */
  994. do_group_exit(SIGKILL);
  995. return;
  996. }
  997. local_irq_disable();
  998. }
  999. clts(); /* Allow maths ops (or we recurse) */
  1000. /*
  1001. * Paranoid restore. send a SIGSEGV if we fail to restore the state.
  1002. */
  1003. if (unlikely(restore_fpu_checking(me))) {
  1004. stts();
  1005. force_sig(SIGSEGV, me);
  1006. return;
  1007. }
  1008. task_thread_info(me)->status |= TS_USEDFPU;
  1009. me->fpu_counter++;
  1010. }
  1011. EXPORT_SYMBOL_GPL(math_state_restore);
  1012. void __init trap_init(void)
  1013. {
  1014. set_intr_gate(0, &divide_error);
  1015. set_intr_gate_ist(1, &debug, DEBUG_STACK);
  1016. set_intr_gate_ist(2, &nmi, NMI_STACK);
  1017. /* int3 can be called from all */
  1018. set_system_gate_ist(3, &int3, DEBUG_STACK);
  1019. /* int4 can be called from all */
  1020. set_system_gate(4, &overflow);
  1021. set_intr_gate(5, &bounds);
  1022. set_intr_gate(6, &invalid_op);
  1023. set_intr_gate(7, &device_not_available);
  1024. set_intr_gate_ist(8, &double_fault, DOUBLEFAULT_STACK);
  1025. set_intr_gate(9, &coprocessor_segment_overrun);
  1026. set_intr_gate(10, &invalid_TSS);
  1027. set_intr_gate(11, &segment_not_present);
  1028. set_intr_gate_ist(12, &stack_segment, STACKFAULT_STACK);
  1029. set_intr_gate(13, &general_protection);
  1030. set_intr_gate(14, &page_fault);
  1031. set_intr_gate(15, &spurious_interrupt_bug);
  1032. set_intr_gate(16, &coprocessor_error);
  1033. set_intr_gate(17, &alignment_check);
  1034. #ifdef CONFIG_X86_MCE
  1035. set_intr_gate_ist(18, &machine_check, MCE_STACK);
  1036. #endif
  1037. set_intr_gate(19, &simd_coprocessor_error);
  1038. #ifdef CONFIG_IA32_EMULATION
  1039. set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
  1040. #endif
  1041. /*
  1042. * Should be a barrier for any external CPU state:
  1043. */
  1044. cpu_init();
  1045. }
  1046. static int __init oops_setup(char *s)
  1047. {
  1048. if (!s)
  1049. return -EINVAL;
  1050. if (!strcmp(s, "panic"))
  1051. panic_on_oops = 1;
  1052. return 0;
  1053. }
  1054. early_param("oops", oops_setup);
  1055. static int __init kstack_setup(char *s)
  1056. {
  1057. if (!s)
  1058. return -EINVAL;
  1059. kstack_depth_to_print = simple_strtoul(s, NULL, 0);
  1060. return 0;
  1061. }
  1062. early_param("kstack", kstack_setup);
  1063. static int __init code_bytes_setup(char *s)
  1064. {
  1065. code_bytes = simple_strtoul(s, NULL, 0);
  1066. if (code_bytes > 8192)
  1067. code_bytes = 8192;
  1068. return 1;
  1069. }
  1070. __setup("code_bytes=", code_bytes_setup);