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