traps_64.c 28 KB

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