traps.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  1. /*
  2. * linux/arch/x86-64/traps.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
  6. *
  7. * Pentium III FXSR, SSE support
  8. * Gareth Hughes <gareth@valinux.com>, May 2000
  9. */
  10. /*
  11. * 'Traps.c' handles hardware traps and faults after we have saved some
  12. * state in 'entry.S'.
  13. */
  14. #include <linux/sched.h>
  15. #include <linux/kernel.h>
  16. #include <linux/string.h>
  17. #include <linux/errno.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/timer.h>
  20. #include <linux/mm.h>
  21. #include <linux/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/kallsyms.h>
  26. #include <linux/module.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/nmi.h>
  29. #include <linux/kprobes.h>
  30. #include <linux/kexec.h>
  31. #include <linux/unwind.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/bug.h>
  34. #include <linux/kdebug.h>
  35. #if defined(CONFIG_EDAC)
  36. #include <linux/edac.h>
  37. #endif
  38. #include <asm/system.h>
  39. #include <asm/io.h>
  40. #include <asm/atomic.h>
  41. #include <asm/debugreg.h>
  42. #include <asm/desc.h>
  43. #include <asm/i387.h>
  44. #include <asm/processor.h>
  45. #include <asm/unwind.h>
  46. #include <asm/smp.h>
  47. #include <asm/pgalloc.h>
  48. #include <asm/pda.h>
  49. #include <asm/proto.h>
  50. #include <asm/nmi.h>
  51. #include <asm/stacktrace.h>
  52. asmlinkage void divide_error(void);
  53. asmlinkage void debug(void);
  54. asmlinkage void nmi(void);
  55. asmlinkage void int3(void);
  56. asmlinkage void overflow(void);
  57. asmlinkage void bounds(void);
  58. asmlinkage void invalid_op(void);
  59. asmlinkage void device_not_available(void);
  60. asmlinkage void double_fault(void);
  61. asmlinkage void coprocessor_segment_overrun(void);
  62. asmlinkage void invalid_TSS(void);
  63. asmlinkage void segment_not_present(void);
  64. asmlinkage void stack_segment(void);
  65. asmlinkage void general_protection(void);
  66. asmlinkage void page_fault(void);
  67. asmlinkage void coprocessor_error(void);
  68. asmlinkage void simd_coprocessor_error(void);
  69. asmlinkage void reserved(void);
  70. asmlinkage void alignment_check(void);
  71. asmlinkage void machine_check(void);
  72. asmlinkage void spurious_interrupt_bug(void);
  73. static inline void conditional_sti(struct pt_regs *regs)
  74. {
  75. if (regs->eflags & X86_EFLAGS_IF)
  76. local_irq_enable();
  77. }
  78. static inline void preempt_conditional_sti(struct pt_regs *regs)
  79. {
  80. preempt_disable();
  81. if (regs->eflags & X86_EFLAGS_IF)
  82. local_irq_enable();
  83. }
  84. static inline void preempt_conditional_cli(struct pt_regs *regs)
  85. {
  86. if (regs->eflags & X86_EFLAGS_IF)
  87. local_irq_disable();
  88. /* Make sure to not schedule here because we could be running
  89. on an exception stack. */
  90. preempt_enable_no_resched();
  91. }
  92. int kstack_depth_to_print = 12;
  93. #ifdef CONFIG_KALLSYMS
  94. void printk_address(unsigned long address)
  95. {
  96. unsigned long offset = 0, symsize;
  97. const char *symname;
  98. char *modname;
  99. char *delim = ":";
  100. char namebuf[128];
  101. symname = kallsyms_lookup(address, &symsize, &offset,
  102. &modname, namebuf);
  103. if (!symname) {
  104. printk(" [<%016lx>]\n", address);
  105. return;
  106. }
  107. if (!modname)
  108. modname = delim = "";
  109. printk(" [<%016lx>] %s%s%s%s+0x%lx/0x%lx\n",
  110. address, delim, modname, delim, symname, offset, symsize);
  111. }
  112. #else
  113. void printk_address(unsigned long address)
  114. {
  115. printk(" [<%016lx>]\n", address);
  116. }
  117. #endif
  118. static unsigned long *in_exception_stack(unsigned cpu, unsigned long stack,
  119. unsigned *usedp, char **idp)
  120. {
  121. static char ids[][8] = {
  122. [DEBUG_STACK - 1] = "#DB",
  123. [NMI_STACK - 1] = "NMI",
  124. [DOUBLEFAULT_STACK - 1] = "#DF",
  125. [STACKFAULT_STACK - 1] = "#SS",
  126. [MCE_STACK - 1] = "#MC",
  127. #if DEBUG_STKSZ > EXCEPTION_STKSZ
  128. [N_EXCEPTION_STACKS ... N_EXCEPTION_STACKS + DEBUG_STKSZ / EXCEPTION_STKSZ - 2] = "#DB[?]"
  129. #endif
  130. };
  131. unsigned k;
  132. /*
  133. * Iterate over all exception stacks, and figure out whether
  134. * 'stack' is in one of them:
  135. */
  136. for (k = 0; k < N_EXCEPTION_STACKS; k++) {
  137. unsigned long end = per_cpu(orig_ist, cpu).ist[k];
  138. /*
  139. * Is 'stack' above this exception frame's end?
  140. * If yes then skip to the next frame.
  141. */
  142. if (stack >= end)
  143. continue;
  144. /*
  145. * Is 'stack' above this exception frame's start address?
  146. * If yes then we found the right frame.
  147. */
  148. if (stack >= end - EXCEPTION_STKSZ) {
  149. /*
  150. * Make sure we only iterate through an exception
  151. * stack once. If it comes up for the second time
  152. * then there's something wrong going on - just
  153. * break out and return NULL:
  154. */
  155. if (*usedp & (1U << k))
  156. break;
  157. *usedp |= 1U << k;
  158. *idp = ids[k];
  159. return (unsigned long *)end;
  160. }
  161. /*
  162. * If this is a debug stack, and if it has a larger size than
  163. * the usual exception stacks, then 'stack' might still
  164. * be within the lower portion of the debug stack:
  165. */
  166. #if DEBUG_STKSZ > EXCEPTION_STKSZ
  167. if (k == DEBUG_STACK - 1 && stack >= end - DEBUG_STKSZ) {
  168. unsigned j = N_EXCEPTION_STACKS - 1;
  169. /*
  170. * Black magic. A large debug stack is composed of
  171. * multiple exception stack entries, which we
  172. * iterate through now. Dont look:
  173. */
  174. do {
  175. ++j;
  176. end -= EXCEPTION_STKSZ;
  177. ids[j][4] = '1' + (j - N_EXCEPTION_STACKS);
  178. } while (stack < end - EXCEPTION_STKSZ);
  179. if (*usedp & (1U << j))
  180. break;
  181. *usedp |= 1U << j;
  182. *idp = ids[j];
  183. return (unsigned long *)end;
  184. }
  185. #endif
  186. }
  187. return NULL;
  188. }
  189. #define MSG(txt) ops->warning(data, txt)
  190. /*
  191. * x86-64 can have upto three kernel stacks:
  192. * process stack
  193. * interrupt stack
  194. * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
  195. */
  196. static inline int valid_stack_ptr(struct thread_info *tinfo, void *p)
  197. {
  198. void *t = (void *)tinfo;
  199. return p > t && p < t + THREAD_SIZE - 3;
  200. }
  201. void dump_trace(struct task_struct *tsk, struct pt_regs *regs,
  202. unsigned long *stack,
  203. struct stacktrace_ops *ops, void *data)
  204. {
  205. const unsigned cpu = get_cpu();
  206. unsigned long *irqstack_end = (unsigned long*)cpu_pda(cpu)->irqstackptr;
  207. unsigned used = 0;
  208. struct thread_info *tinfo;
  209. if (!tsk)
  210. tsk = current;
  211. if (!stack) {
  212. unsigned long dummy;
  213. stack = &dummy;
  214. if (tsk && tsk != current)
  215. stack = (unsigned long *)tsk->thread.rsp;
  216. }
  217. /*
  218. * Print function call entries within a stack. 'cond' is the
  219. * "end of stackframe" condition, that the 'stack++'
  220. * iteration will eventually trigger.
  221. */
  222. #define HANDLE_STACK(cond) \
  223. do while (cond) { \
  224. unsigned long addr = *stack++; \
  225. /* Use unlocked access here because except for NMIs \
  226. we should be already protected against module unloads */ \
  227. if (__kernel_text_address(addr)) { \
  228. /* \
  229. * If the address is either in the text segment of the \
  230. * kernel, or in the region which contains vmalloc'ed \
  231. * memory, it *may* be the address of a calling \
  232. * routine; if so, print it so that someone tracing \
  233. * down the cause of the crash will be able to figure \
  234. * out the call path that was taken. \
  235. */ \
  236. ops->address(data, addr); \
  237. } \
  238. } while (0)
  239. /*
  240. * Print function call entries in all stacks, starting at the
  241. * current stack address. If the stacks consist of nested
  242. * exceptions
  243. */
  244. for (;;) {
  245. char *id;
  246. unsigned long *estack_end;
  247. estack_end = in_exception_stack(cpu, (unsigned long)stack,
  248. &used, &id);
  249. if (estack_end) {
  250. if (ops->stack(data, id) < 0)
  251. break;
  252. HANDLE_STACK (stack < estack_end);
  253. ops->stack(data, "<EOE>");
  254. /*
  255. * We link to the next stack via the
  256. * second-to-last pointer (index -2 to end) in the
  257. * exception stack:
  258. */
  259. stack = (unsigned long *) estack_end[-2];
  260. continue;
  261. }
  262. if (irqstack_end) {
  263. unsigned long *irqstack;
  264. irqstack = irqstack_end -
  265. (IRQSTACKSIZE - 64) / sizeof(*irqstack);
  266. if (stack >= irqstack && stack < irqstack_end) {
  267. if (ops->stack(data, "IRQ") < 0)
  268. break;
  269. HANDLE_STACK (stack < irqstack_end);
  270. /*
  271. * We link to the next stack (which would be
  272. * the process stack normally) the last
  273. * pointer (index -1 to end) in the IRQ stack:
  274. */
  275. stack = (unsigned long *) (irqstack_end[-1]);
  276. irqstack_end = NULL;
  277. ops->stack(data, "EOI");
  278. continue;
  279. }
  280. }
  281. break;
  282. }
  283. /*
  284. * This handles the process stack:
  285. */
  286. tinfo = task_thread_info(tsk);
  287. HANDLE_STACK (valid_stack_ptr(tinfo, stack));
  288. #undef HANDLE_STACK
  289. put_cpu();
  290. }
  291. EXPORT_SYMBOL(dump_trace);
  292. static void
  293. print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
  294. {
  295. print_symbol(msg, symbol);
  296. printk("\n");
  297. }
  298. static void print_trace_warning(void *data, char *msg)
  299. {
  300. printk("%s\n", msg);
  301. }
  302. static int print_trace_stack(void *data, char *name)
  303. {
  304. printk(" <%s> ", name);
  305. return 0;
  306. }
  307. static void print_trace_address(void *data, unsigned long addr)
  308. {
  309. touch_nmi_watchdog();
  310. printk_address(addr);
  311. }
  312. static struct stacktrace_ops print_trace_ops = {
  313. .warning = print_trace_warning,
  314. .warning_symbol = print_trace_warning_symbol,
  315. .stack = print_trace_stack,
  316. .address = print_trace_address,
  317. };
  318. void
  319. show_trace(struct task_struct *tsk, struct pt_regs *regs, unsigned long *stack)
  320. {
  321. printk("\nCall Trace:\n");
  322. dump_trace(tsk, regs, stack, &print_trace_ops, NULL);
  323. printk("\n");
  324. }
  325. static void
  326. _show_stack(struct task_struct *tsk, struct pt_regs *regs, unsigned long *rsp)
  327. {
  328. unsigned long *stack;
  329. int i;
  330. const int cpu = smp_processor_id();
  331. unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
  332. unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
  333. // debugging aid: "show_stack(NULL, NULL);" prints the
  334. // back trace for this cpu.
  335. if (rsp == NULL) {
  336. if (tsk)
  337. rsp = (unsigned long *)tsk->thread.rsp;
  338. else
  339. rsp = (unsigned long *)&rsp;
  340. }
  341. stack = rsp;
  342. for(i=0; i < kstack_depth_to_print; i++) {
  343. if (stack >= irqstack && stack <= irqstack_end) {
  344. if (stack == irqstack_end) {
  345. stack = (unsigned long *) (irqstack_end[-1]);
  346. printk(" <EOI> ");
  347. }
  348. } else {
  349. if (((long) stack & (THREAD_SIZE-1)) == 0)
  350. break;
  351. }
  352. if (i && ((i % 4) == 0))
  353. printk("\n");
  354. printk(" %016lx", *stack++);
  355. touch_nmi_watchdog();
  356. }
  357. show_trace(tsk, regs, rsp);
  358. }
  359. void show_stack(struct task_struct *tsk, unsigned long * rsp)
  360. {
  361. _show_stack(tsk, NULL, rsp);
  362. }
  363. /*
  364. * The architecture-independent dump_stack generator
  365. */
  366. void dump_stack(void)
  367. {
  368. unsigned long dummy;
  369. show_trace(NULL, NULL, &dummy);
  370. }
  371. EXPORT_SYMBOL(dump_stack);
  372. void show_registers(struct pt_regs *regs)
  373. {
  374. int i;
  375. int in_kernel = !user_mode(regs);
  376. unsigned long rsp;
  377. const int cpu = smp_processor_id();
  378. struct task_struct *cur = cpu_pda(cpu)->pcurrent;
  379. rsp = regs->rsp;
  380. printk("CPU %d ", cpu);
  381. __show_regs(regs);
  382. printk("Process %s (pid: %d, threadinfo %p, task %p)\n",
  383. cur->comm, cur->pid, task_thread_info(cur), cur);
  384. /*
  385. * When in-kernel, we also print out the stack and code at the
  386. * time of the fault..
  387. */
  388. if (in_kernel) {
  389. printk("Stack: ");
  390. _show_stack(NULL, regs, (unsigned long*)rsp);
  391. printk("\nCode: ");
  392. if (regs->rip < PAGE_OFFSET)
  393. goto bad;
  394. for (i=0; i<20; i++) {
  395. unsigned char c;
  396. if (__get_user(c, &((unsigned char*)regs->rip)[i])) {
  397. bad:
  398. printk(" Bad RIP value.");
  399. break;
  400. }
  401. printk("%02x ", c);
  402. }
  403. }
  404. printk("\n");
  405. }
  406. int is_valid_bugaddr(unsigned long rip)
  407. {
  408. unsigned short ud2;
  409. if (__copy_from_user(&ud2, (const void __user *) rip, sizeof(ud2)))
  410. return 0;
  411. return ud2 == 0x0b0f;
  412. }
  413. #ifdef CONFIG_BUG
  414. void out_of_line_bug(void)
  415. {
  416. BUG();
  417. }
  418. EXPORT_SYMBOL(out_of_line_bug);
  419. #endif
  420. static DEFINE_SPINLOCK(die_lock);
  421. static int die_owner = -1;
  422. static unsigned int die_nest_count;
  423. unsigned __kprobes long oops_begin(void)
  424. {
  425. int cpu;
  426. unsigned long flags;
  427. oops_enter();
  428. /* racy, but better than risking deadlock. */
  429. local_irq_save(flags);
  430. cpu = smp_processor_id();
  431. if (!spin_trylock(&die_lock)) {
  432. if (cpu == die_owner)
  433. /* nested oops. should stop eventually */;
  434. else
  435. spin_lock(&die_lock);
  436. }
  437. die_nest_count++;
  438. die_owner = cpu;
  439. console_verbose();
  440. bust_spinlocks(1);
  441. return flags;
  442. }
  443. void __kprobes oops_end(unsigned long flags)
  444. {
  445. die_owner = -1;
  446. bust_spinlocks(0);
  447. die_nest_count--;
  448. if (die_nest_count)
  449. /* We still own the lock */
  450. local_irq_restore(flags);
  451. else
  452. /* Nest count reaches zero, release the lock. */
  453. spin_unlock_irqrestore(&die_lock, flags);
  454. if (panic_on_oops)
  455. panic("Fatal exception");
  456. oops_exit();
  457. }
  458. void __kprobes __die(const char * str, struct pt_regs * regs, long err)
  459. {
  460. static int die_counter;
  461. printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff,++die_counter);
  462. #ifdef CONFIG_PREEMPT
  463. printk("PREEMPT ");
  464. #endif
  465. #ifdef CONFIG_SMP
  466. printk("SMP ");
  467. #endif
  468. #ifdef CONFIG_DEBUG_PAGEALLOC
  469. printk("DEBUG_PAGEALLOC");
  470. #endif
  471. printk("\n");
  472. notify_die(DIE_OOPS, str, regs, err, current->thread.trap_no, SIGSEGV);
  473. show_registers(regs);
  474. add_taint(TAINT_DIE);
  475. /* Executive summary in case the oops scrolled away */
  476. printk(KERN_ALERT "RIP ");
  477. printk_address(regs->rip);
  478. printk(" RSP <%016lx>\n", regs->rsp);
  479. if (kexec_should_crash(current))
  480. crash_kexec(regs);
  481. }
  482. void die(const char * str, struct pt_regs * regs, long err)
  483. {
  484. unsigned long flags = oops_begin();
  485. if (!user_mode(regs))
  486. report_bug(regs->rip, regs);
  487. __die(str, regs, err);
  488. oops_end(flags);
  489. do_exit(SIGSEGV);
  490. }
  491. void __kprobes die_nmi(char *str, struct pt_regs *regs, int do_panic)
  492. {
  493. unsigned long flags = oops_begin();
  494. /*
  495. * We are in trouble anyway, lets at least try
  496. * to get a message out.
  497. */
  498. printk(str, smp_processor_id());
  499. show_registers(regs);
  500. if (kexec_should_crash(current))
  501. crash_kexec(regs);
  502. if (do_panic || panic_on_oops)
  503. panic("Non maskable interrupt");
  504. oops_end(flags);
  505. nmi_exit();
  506. local_irq_enable();
  507. do_exit(SIGSEGV);
  508. }
  509. static void __kprobes do_trap(int trapnr, int signr, char *str,
  510. struct pt_regs * regs, long error_code,
  511. siginfo_t *info)
  512. {
  513. struct task_struct *tsk = current;
  514. if (user_mode(regs)) {
  515. /*
  516. * We want error_code and trap_no set for userspace
  517. * faults and kernelspace faults which result in
  518. * die(), but not kernelspace faults which are fixed
  519. * up. die() gives the process no chance to handle
  520. * the signal and notice the kernel fault information,
  521. * so that won't result in polluting the information
  522. * about previously queued, but not yet delivered,
  523. * faults. See also do_general_protection below.
  524. */
  525. tsk->thread.error_code = error_code;
  526. tsk->thread.trap_no = trapnr;
  527. if (exception_trace && unhandled_signal(tsk, signr))
  528. printk(KERN_INFO
  529. "%s[%d] trap %s rip:%lx rsp:%lx error:%lx\n",
  530. tsk->comm, tsk->pid, str,
  531. regs->rip, regs->rsp, error_code);
  532. if (info)
  533. force_sig_info(signr, info, tsk);
  534. else
  535. force_sig(signr, tsk);
  536. return;
  537. }
  538. /* kernel trap */
  539. {
  540. const struct exception_table_entry *fixup;
  541. fixup = search_exception_tables(regs->rip);
  542. if (fixup)
  543. regs->rip = fixup->fixup;
  544. else {
  545. tsk->thread.error_code = error_code;
  546. tsk->thread.trap_no = trapnr;
  547. die(str, regs, error_code);
  548. }
  549. return;
  550. }
  551. }
  552. #define DO_ERROR(trapnr, signr, str, name) \
  553. asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
  554. { \
  555. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
  556. == NOTIFY_STOP) \
  557. return; \
  558. conditional_sti(regs); \
  559. do_trap(trapnr, signr, str, regs, error_code, NULL); \
  560. }
  561. #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
  562. asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
  563. { \
  564. siginfo_t info; \
  565. info.si_signo = signr; \
  566. info.si_errno = 0; \
  567. info.si_code = sicode; \
  568. info.si_addr = (void __user *)siaddr; \
  569. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
  570. == NOTIFY_STOP) \
  571. return; \
  572. conditional_sti(regs); \
  573. do_trap(trapnr, signr, str, regs, error_code, &info); \
  574. }
  575. DO_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->rip)
  576. DO_ERROR( 4, SIGSEGV, "overflow", overflow)
  577. DO_ERROR( 5, SIGSEGV, "bounds", bounds)
  578. DO_ERROR_INFO( 6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->rip)
  579. DO_ERROR( 7, SIGSEGV, "device not available", device_not_available)
  580. DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
  581. DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
  582. DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
  583. DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
  584. DO_ERROR(18, SIGSEGV, "reserved", reserved)
  585. /* Runs on IST stack */
  586. asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
  587. {
  588. if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
  589. 12, SIGBUS) == NOTIFY_STOP)
  590. return;
  591. preempt_conditional_sti(regs);
  592. do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
  593. preempt_conditional_cli(regs);
  594. }
  595. asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
  596. {
  597. static const char str[] = "double fault";
  598. struct task_struct *tsk = current;
  599. /* Return not checked because double check cannot be ignored */
  600. notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
  601. tsk->thread.error_code = error_code;
  602. tsk->thread.trap_no = 8;
  603. /* This is always a kernel trap and never fixable (and thus must
  604. never return). */
  605. for (;;)
  606. die(str, regs, error_code);
  607. }
  608. asmlinkage void __kprobes do_general_protection(struct pt_regs * regs,
  609. long error_code)
  610. {
  611. struct task_struct *tsk = current;
  612. conditional_sti(regs);
  613. if (user_mode(regs)) {
  614. tsk->thread.error_code = error_code;
  615. tsk->thread.trap_no = 13;
  616. if (exception_trace && unhandled_signal(tsk, SIGSEGV))
  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);