traps_64.c 29 KB

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