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