traps_64.c 29 KB

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