traps_64.c 30 KB

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