traps_64.c 30 KB

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