traps_64.c 28 KB

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