traps_64.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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->eflags & 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->eflags & X86_EFLAGS_IF)
  81. local_irq_enable();
  82. }
  83. static inline void preempt_conditional_cli(struct pt_regs *regs)
  84. {
  85. if (regs->eflags & 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.rsp;
  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 *rsp)
  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 (rsp == NULL) {
  335. if (tsk)
  336. rsp = (unsigned long *)tsk->thread.rsp;
  337. else
  338. rsp = (unsigned long *)&rsp;
  339. }
  340. stack = rsp;
  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, rsp);
  357. }
  358. void show_stack(struct task_struct *tsk, unsigned long * rsp)
  359. {
  360. _show_stack(tsk, NULL, rsp);
  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 rsp;
  381. const int cpu = smp_processor_id();
  382. struct task_struct *cur = cpu_pda(cpu)->pcurrent;
  383. rsp = regs->rsp;
  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*)rsp);
  395. printk("\nCode: ");
  396. if (regs->rip < PAGE_OFFSET)
  397. goto bad;
  398. for (i=0; i<20; i++) {
  399. unsigned char c;
  400. if (__get_user(c, &((unsigned char*)regs->rip)[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 rip)
  411. {
  412. unsigned short ud2;
  413. if (__copy_from_user(&ud2, (const void __user *) rip, sizeof(ud2)))
  414. return 0;
  415. return ud2 == 0x0b0f;
  416. }
  417. #ifdef CONFIG_BUG
  418. void out_of_line_bug(void)
  419. {
  420. BUG();
  421. }
  422. EXPORT_SYMBOL(out_of_line_bug);
  423. #endif
  424. static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
  425. static int die_owner = -1;
  426. static unsigned int die_nest_count;
  427. unsigned __kprobes long oops_begin(void)
  428. {
  429. int cpu;
  430. unsigned long flags;
  431. oops_enter();
  432. /* racy, but better than risking deadlock. */
  433. raw_local_irq_save(flags);
  434. cpu = smp_processor_id();
  435. if (!__raw_spin_trylock(&die_lock)) {
  436. if (cpu == die_owner)
  437. /* nested oops. should stop eventually */;
  438. else
  439. __raw_spin_lock(&die_lock);
  440. }
  441. die_nest_count++;
  442. die_owner = cpu;
  443. console_verbose();
  444. bust_spinlocks(1);
  445. return flags;
  446. }
  447. void __kprobes oops_end(unsigned long flags)
  448. {
  449. die_owner = -1;
  450. bust_spinlocks(0);
  451. die_nest_count--;
  452. if (!die_nest_count)
  453. /* Nest count reaches zero, release the lock. */
  454. __raw_spin_unlock(&die_lock);
  455. raw_local_irq_restore(flags);
  456. if (panic_on_oops)
  457. panic("Fatal exception");
  458. oops_exit();
  459. }
  460. void __kprobes __die(const char * str, struct pt_regs * regs, long err)
  461. {
  462. static int die_counter;
  463. printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
  464. #ifdef CONFIG_PREEMPT
  465. printk("PREEMPT ");
  466. #endif
  467. #ifdef CONFIG_SMP
  468. printk("SMP ");
  469. #endif
  470. #ifdef CONFIG_DEBUG_PAGEALLOC
  471. printk("DEBUG_PAGEALLOC");
  472. #endif
  473. printk("\n");
  474. notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV);
  475. show_registers(regs);
  476. add_taint(TAINT_DIE);
  477. /* Executive summary in case the oops scrolled away */
  478. printk(KERN_ALERT "RIP ");
  479. printk_address(regs->rip);
  480. printk(" RSP <%016lx>\n", regs->rsp);
  481. if (kexec_should_crash(current))
  482. crash_kexec(regs);
  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->rip, regs);
  489. __die(str, regs, err);
  490. oops_end(flags);
  491. do_exit(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);
  507. nmi_exit();
  508. local_irq_enable();
  509. do_exit(SIGSEGV);
  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 rip:%lx rsp:%lx error:%lx\n",
  533. tsk->comm, tsk->pid, str,
  534. regs->rip, regs->rsp, 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->rip);
  545. if (fixup)
  546. regs->rip = 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. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
  573. == NOTIFY_STOP) \
  574. return; \
  575. conditional_sti(regs); \
  576. do_trap(trapnr, signr, str, regs, error_code, &info); \
  577. }
  578. DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->rip)
  579. DO_ERROR( 4, SIGSEGV, "overflow", overflow)
  580. DO_ERROR( 5, SIGSEGV, "bounds", bounds)
  581. DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->rip)
  582. DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
  583. DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
  584. DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
  585. DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
  586. DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
  587. DO_ERROR(18, SIGSEGV, "reserved", reserved)
  588. /* Runs on IST stack */
  589. asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
  590. {
  591. if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
  592. 12, SIGBUS) == NOTIFY_STOP)
  593. return;
  594. preempt_conditional_sti(regs);
  595. do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
  596. preempt_conditional_cli(regs);
  597. }
  598. asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
  599. {
  600. static const char str[] = "double fault";
  601. struct task_struct *tsk = current;
  602. /* Return not checked because double check cannot be ignored */
  603. notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
  604. tsk->thread.error_code = error_code;
  605. tsk->thread.trap_no = 8;
  606. /* This is always a kernel trap and never fixable (and thus must
  607. never return). */
  608. for (;;)
  609. die(str, regs, error_code);
  610. }
  611. asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
  612. long error_code)
  613. {
  614. struct task_struct *tsk = current;
  615. conditional_sti(regs);
  616. if (user_mode(regs)) {
  617. tsk->thread.error_code = error_code;
  618. tsk->thread.trap_no = 13;
  619. if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
  620. printk_ratelimit())
  621. printk(KERN_INFO
  622. "%s[%d] general protection rip:%lx rsp:%lx error:%lx\n",
  623. tsk->comm, tsk->pid,
  624. regs->rip, regs->rsp, error_code);
  625. force_sig(SIGSEGV, tsk);
  626. return;
  627. }
  628. /* kernel gp */
  629. {
  630. const struct exception_table_entry *fixup;
  631. fixup = search_exception_tables(regs->rip);
  632. if (fixup) {
  633. regs->rip = fixup->fixup;
  634. return;
  635. }
  636. tsk->thread.error_code = error_code;
  637. tsk->thread.trap_no = 13;
  638. if (notify_die(DIE_GPF, "general protection fault", regs,
  639. error_code, 13, SIGSEGV) == NOTIFY_STOP)
  640. return;
  641. die("general protection fault", regs, error_code);
  642. }
  643. }
  644. static __kprobes void
  645. mem_parity_error(unsigned char reason, struct pt_regs * regs)
  646. {
  647. printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
  648. reason);
  649. printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
  650. #if defined(CONFIG_EDAC)
  651. if(edac_handler_set()) {
  652. edac_atomic_assert_error();
  653. return;
  654. }
  655. #endif
  656. if (panic_on_unrecovered_nmi)
  657. panic("NMI: Not continuing");
  658. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  659. /* Clear and disable the memory parity error line. */
  660. reason = (reason & 0xf) | 4;
  661. outb(reason, 0x61);
  662. }
  663. static __kprobes void
  664. io_check_error(unsigned char reason, struct pt_regs * regs)
  665. {
  666. printk("NMI: IOCK error (debug interrupt?)\n");
  667. show_registers(regs);
  668. /* Re-enable the IOCK line, wait for a few seconds */
  669. reason = (reason & 0xf) | 8;
  670. outb(reason, 0x61);
  671. mdelay(2000);
  672. reason &= ~8;
  673. outb(reason, 0x61);
  674. }
  675. static __kprobes void
  676. unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
  677. {
  678. printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
  679. reason);
  680. printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
  681. if (panic_on_unrecovered_nmi)
  682. panic("NMI: Not continuing");
  683. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  684. }
  685. /* Runs on IST stack. This code must keep interrupts off all the time.
  686. Nested NMIs are prevented by the CPU. */
  687. asmlinkage __kprobes void default_do_nmi(struct pt_regs *regs)
  688. {
  689. unsigned char reason = 0;
  690. int cpu;
  691. cpu = smp_processor_id();
  692. /* Only the BSP gets external NMIs from the system. */
  693. if (!cpu)
  694. reason = get_nmi_reason();
  695. if (!(reason & 0xc0)) {
  696. if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
  697. == NOTIFY_STOP)
  698. return;
  699. /*
  700. * Ok, so this is none of the documented NMI sources,
  701. * so it must be the NMI watchdog.
  702. */
  703. if (nmi_watchdog_tick(regs,reason))
  704. return;
  705. if (!do_nmi_callback(regs,cpu))
  706. unknown_nmi_error(reason, regs);
  707. return;
  708. }
  709. if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
  710. return;
  711. /* AK: following checks seem to be broken on modern chipsets. FIXME */
  712. if (reason & 0x80)
  713. mem_parity_error(reason, regs);
  714. if (reason & 0x40)
  715. io_check_error(reason, regs);
  716. }
  717. /* runs on IST stack. */
  718. asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code)
  719. {
  720. trace_hardirqs_fixup();
  721. if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) {
  722. return;
  723. }
  724. preempt_conditional_sti(regs);
  725. do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
  726. preempt_conditional_cli(regs);
  727. }
  728. /* Help handler running on IST stack to switch back to user stack
  729. for scheduling or signal handling. The actual stack switch is done in
  730. entry.S */
  731. asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
  732. {
  733. struct pt_regs *regs = eregs;
  734. /* Did already sync */
  735. if (eregs == (struct pt_regs *)eregs->rsp)
  736. ;
  737. /* Exception from user space */
  738. else if (user_mode(eregs))
  739. regs = task_pt_regs(current);
  740. /* Exception from kernel and interrupts are enabled. Move to
  741. kernel process stack. */
  742. else if (eregs->eflags & X86_EFLAGS_IF)
  743. regs = (struct pt_regs *)(eregs->rsp -= sizeof(struct pt_regs));
  744. if (eregs != regs)
  745. *regs = *eregs;
  746. return regs;
  747. }
  748. /* runs on IST stack. */
  749. asmlinkage void __kprobes do_debug(struct pt_regs * regs,
  750. unsigned long error_code)
  751. {
  752. unsigned long condition;
  753. struct task_struct *tsk = current;
  754. siginfo_t info;
  755. trace_hardirqs_fixup();
  756. get_debugreg(condition, 6);
  757. if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
  758. SIGTRAP) == NOTIFY_STOP)
  759. return;
  760. preempt_conditional_sti(regs);
  761. /* Mask out spurious debug traps due to lazy DR7 setting */
  762. if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
  763. if (!tsk->thread.debugreg7) {
  764. goto clear_dr7;
  765. }
  766. }
  767. tsk->thread.debugreg6 = condition;
  768. /* Mask out spurious TF errors due to lazy TF clearing */
  769. if (condition & DR_STEP) {
  770. /*
  771. * The TF error should be masked out only if the current
  772. * process is not traced and if the TRAP flag has been set
  773. * previously by a tracing process (condition detected by
  774. * the PT_DTRACE flag); remember that the i386 TRAP flag
  775. * can be modified by the process itself in user mode,
  776. * allowing programs to debug themselves without the ptrace()
  777. * interface.
  778. */
  779. if (!user_mode(regs))
  780. goto clear_TF_reenable;
  781. /*
  782. * Was the TF flag set by a debugger? If so, clear it now,
  783. * so that register information is correct.
  784. */
  785. if (tsk->ptrace & PT_DTRACE) {
  786. regs->eflags &= ~TF_MASK;
  787. tsk->ptrace &= ~PT_DTRACE;
  788. }
  789. }
  790. /* Ok, finally something we can handle */
  791. tsk->thread.trap_no = 1;
  792. tsk->thread.error_code = error_code;
  793. info.si_signo = SIGTRAP;
  794. info.si_errno = 0;
  795. info.si_code = TRAP_BRKPT;
  796. info.si_addr = user_mode(regs) ? (void __user *)regs->rip : NULL;
  797. force_sig_info(SIGTRAP, &info, tsk);
  798. clear_dr7:
  799. set_debugreg(0UL, 7);
  800. preempt_conditional_cli(regs);
  801. return;
  802. clear_TF_reenable:
  803. set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
  804. regs->eflags &= ~TF_MASK;
  805. preempt_conditional_cli(regs);
  806. }
  807. static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
  808. {
  809. const struct exception_table_entry *fixup;
  810. fixup = search_exception_tables(regs->rip);
  811. if (fixup) {
  812. regs->rip = fixup->fixup;
  813. return 1;
  814. }
  815. notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
  816. /* Illegal floating point operation in the kernel */
  817. current->thread.trap_no = trapnr;
  818. die(str, regs, 0);
  819. return 0;
  820. }
  821. /*
  822. * Note that we play around with the 'TS' bit in an attempt to get
  823. * the correct behaviour even in the presence of the asynchronous
  824. * IRQ13 behaviour
  825. */
  826. asmlinkage void do_coprocessor_error(struct pt_regs *regs)
  827. {
  828. void __user *rip = (void __user *)(regs->rip);
  829. struct task_struct * task;
  830. siginfo_t info;
  831. unsigned short cwd, swd;
  832. conditional_sti(regs);
  833. if (!user_mode(regs) &&
  834. kernel_math_error(regs, "kernel x87 math error", 16))
  835. return;
  836. /*
  837. * Save the info for the exception handler and clear the error.
  838. */
  839. task = current;
  840. save_init_fpu(task);
  841. task->thread.trap_no = 16;
  842. task->thread.error_code = 0;
  843. info.si_signo = SIGFPE;
  844. info.si_errno = 0;
  845. info.si_code = __SI_FAULT;
  846. info.si_addr = rip;
  847. /*
  848. * (~cwd & swd) will mask out exceptions that are not set to unmasked
  849. * status. 0x3f is the exception bits in these regs, 0x200 is the
  850. * C1 reg you need in case of a stack fault, 0x040 is the stack
  851. * fault bit. We should only be taking one exception at a time,
  852. * so if this combination doesn't produce any single exception,
  853. * then we have a bad program that isn't synchronizing its FPU usage
  854. * and it will suffer the consequences since we won't be able to
  855. * fully reproduce the context of the exception
  856. */
  857. cwd = get_fpu_cwd(task);
  858. swd = get_fpu_swd(task);
  859. switch (swd & ~cwd & 0x3f) {
  860. case 0x000:
  861. default:
  862. break;
  863. case 0x001: /* Invalid Op */
  864. /*
  865. * swd & 0x240 == 0x040: Stack Underflow
  866. * swd & 0x240 == 0x240: Stack Overflow
  867. * User must clear the SF bit (0x40) if set
  868. */
  869. info.si_code = FPE_FLTINV;
  870. break;
  871. case 0x002: /* Denormalize */
  872. case 0x010: /* Underflow */
  873. info.si_code = FPE_FLTUND;
  874. break;
  875. case 0x004: /* Zero Divide */
  876. info.si_code = FPE_FLTDIV;
  877. break;
  878. case 0x008: /* Overflow */
  879. info.si_code = FPE_FLTOVF;
  880. break;
  881. case 0x020: /* Precision */
  882. info.si_code = FPE_FLTRES;
  883. break;
  884. }
  885. force_sig_info(SIGFPE, &info, task);
  886. }
  887. asmlinkage void bad_intr(void)
  888. {
  889. printk("bad interrupt");
  890. }
  891. asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
  892. {
  893. void __user *rip = (void __user *)(regs->rip);
  894. struct task_struct * task;
  895. siginfo_t info;
  896. unsigned short mxcsr;
  897. conditional_sti(regs);
  898. if (!user_mode(regs) &&
  899. kernel_math_error(regs, "kernel simd math error", 19))
  900. return;
  901. /*
  902. * Save the info for the exception handler and clear the error.
  903. */
  904. task = current;
  905. save_init_fpu(task);
  906. task->thread.trap_no = 19;
  907. task->thread.error_code = 0;
  908. info.si_signo = SIGFPE;
  909. info.si_errno = 0;
  910. info.si_code = __SI_FAULT;
  911. info.si_addr = rip;
  912. /*
  913. * The SIMD FPU exceptions are handled a little differently, as there
  914. * is only a single status/control register. Thus, to determine which
  915. * unmasked exception was caught we must mask the exception mask bits
  916. * at 0x1f80, and then use these to mask the exception bits at 0x3f.
  917. */
  918. mxcsr = get_fpu_mxcsr(task);
  919. switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
  920. case 0x000:
  921. default:
  922. break;
  923. case 0x001: /* Invalid Op */
  924. info.si_code = FPE_FLTINV;
  925. break;
  926. case 0x002: /* Denormalize */
  927. case 0x010: /* Underflow */
  928. info.si_code = FPE_FLTUND;
  929. break;
  930. case 0x004: /* Zero Divide */
  931. info.si_code = FPE_FLTDIV;
  932. break;
  933. case 0x008: /* Overflow */
  934. info.si_code = FPE_FLTOVF;
  935. break;
  936. case 0x020: /* Precision */
  937. info.si_code = FPE_FLTRES;
  938. break;
  939. }
  940. force_sig_info(SIGFPE, &info, task);
  941. }
  942. asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
  943. {
  944. }
  945. asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
  946. {
  947. }
  948. asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
  949. {
  950. }
  951. /*
  952. * 'math_state_restore()' saves the current math information in the
  953. * old math state array, and gets the new ones from the current task
  954. *
  955. * Careful.. There are problems with IBM-designed IRQ13 behaviour.
  956. * Don't touch unless you *really* know how it works.
  957. */
  958. asmlinkage void math_state_restore(void)
  959. {
  960. struct task_struct *me = current;
  961. clts(); /* Allow maths ops (or we recurse) */
  962. if (!used_math())
  963. init_fpu(me);
  964. restore_fpu_checking(&me->thread.i387.fxsave);
  965. task_thread_info(me)->status |= TS_USEDFPU;
  966. me->fpu_counter++;
  967. }
  968. void __init trap_init(void)
  969. {
  970. set_intr_gate(0,&divide_error);
  971. set_intr_gate_ist(1,&debug,DEBUG_STACK);
  972. set_intr_gate_ist(2,&nmi,NMI_STACK);
  973. set_system_gate_ist(3,&int3,DEBUG_STACK); /* int3 can be called from all */
  974. set_system_gate(4,&overflow); /* int4 can be called from all */
  975. set_intr_gate(5,&bounds);
  976. set_intr_gate(6,&invalid_op);
  977. set_intr_gate(7,&device_not_available);
  978. set_intr_gate_ist(8,&double_fault, DOUBLEFAULT_STACK);
  979. set_intr_gate(9,&coprocessor_segment_overrun);
  980. set_intr_gate(10,&invalid_TSS);
  981. set_intr_gate(11,&segment_not_present);
  982. set_intr_gate_ist(12,&stack_segment,STACKFAULT_STACK);
  983. set_intr_gate(13,&general_protection);
  984. set_intr_gate(14,&page_fault);
  985. set_intr_gate(15,&spurious_interrupt_bug);
  986. set_intr_gate(16,&coprocessor_error);
  987. set_intr_gate(17,&alignment_check);
  988. #ifdef CONFIG_X86_MCE
  989. set_intr_gate_ist(18,&machine_check, MCE_STACK);
  990. #endif
  991. set_intr_gate(19,&simd_coprocessor_error);
  992. #ifdef CONFIG_IA32_EMULATION
  993. set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
  994. #endif
  995. /*
  996. * Should be a barrier for any external CPU state.
  997. */
  998. cpu_init();
  999. }
  1000. static int __init oops_setup(char *s)
  1001. {
  1002. if (!s)
  1003. return -EINVAL;
  1004. if (!strcmp(s, "panic"))
  1005. panic_on_oops = 1;
  1006. return 0;
  1007. }
  1008. early_param("oops", oops_setup);
  1009. static int __init kstack_setup(char *s)
  1010. {
  1011. if (!s)
  1012. return -EINVAL;
  1013. kstack_depth_to_print = simple_strtoul(s,NULL,0);
  1014. return 0;
  1015. }
  1016. early_param("kstack", kstack_setup);