traps_64.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  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("Call Trace:\n");
  310. dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
  311. }
  312. void show_trace(struct task_struct *task, struct pt_regs *regs,
  313. unsigned long *stack, unsigned long bp)
  314. {
  315. show_trace_log_lvl(task, regs, stack, bp, "");
  316. }
  317. static void
  318. show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
  319. unsigned long *sp, unsigned long bp, char *log_lvl)
  320. {
  321. unsigned long *stack;
  322. int i;
  323. const int cpu = smp_processor_id();
  324. unsigned long *irqstack_end = (unsigned long *) (cpu_pda(cpu)->irqstackptr);
  325. unsigned long *irqstack = (unsigned long *) (cpu_pda(cpu)->irqstackptr - IRQSTACKSIZE);
  326. // debugging aid: "show_stack(NULL, NULL);" prints the
  327. // back trace for this cpu.
  328. if (sp == NULL) {
  329. if (task)
  330. sp = (unsigned long *)task->thread.sp;
  331. else
  332. sp = (unsigned long *)&sp;
  333. }
  334. stack = sp;
  335. for (i = 0; i < kstack_depth_to_print; i++) {
  336. if (stack >= irqstack && stack <= irqstack_end) {
  337. if (stack == irqstack_end) {
  338. stack = (unsigned long *) (irqstack_end[-1]);
  339. printk(" <EOI> ");
  340. }
  341. } else {
  342. if (((long) stack & (THREAD_SIZE-1)) == 0)
  343. break;
  344. }
  345. if (i && ((i % 4) == 0))
  346. printk("\n");
  347. printk(" %016lx", *stack++);
  348. touch_nmi_watchdog();
  349. }
  350. printk("\n");
  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(KERN_EMERG "Code: ");
  400. ip = (u8 *)regs->ip - code_prologue;
  401. if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
  402. /* try starting at RIP */
  403. ip = (u8 *)regs->ip;
  404. code_len = code_len - code_prologue + 1;
  405. }
  406. for (i = 0; i < code_len; i++, ip++) {
  407. if (ip < (u8 *)PAGE_OFFSET ||
  408. probe_kernel_address(ip, c)) {
  409. printk(" Bad RIP value.");
  410. break;
  411. }
  412. if (ip == (u8 *)regs->ip)
  413. printk("<%02x> ", c);
  414. else
  415. printk("%02x ", c);
  416. }
  417. }
  418. printk("\n");
  419. }
  420. int is_valid_bugaddr(unsigned long ip)
  421. {
  422. unsigned short ud2;
  423. if (__copy_from_user(&ud2, (const void __user *) ip, sizeof(ud2)))
  424. return 0;
  425. return ud2 == 0x0b0f;
  426. }
  427. static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
  428. static int die_owner = -1;
  429. static unsigned int die_nest_count;
  430. unsigned __kprobes long oops_begin(void)
  431. {
  432. int cpu;
  433. unsigned long flags;
  434. oops_enter();
  435. /* racy, but better than risking deadlock. */
  436. raw_local_irq_save(flags);
  437. cpu = smp_processor_id();
  438. if (!__raw_spin_trylock(&die_lock)) {
  439. if (cpu == die_owner)
  440. /* nested oops. should stop eventually */;
  441. else
  442. __raw_spin_lock(&die_lock);
  443. }
  444. die_nest_count++;
  445. die_owner = cpu;
  446. console_verbose();
  447. bust_spinlocks(1);
  448. return flags;
  449. }
  450. void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
  451. {
  452. die_owner = -1;
  453. bust_spinlocks(0);
  454. die_nest_count--;
  455. if (!die_nest_count)
  456. /* Nest count reaches zero, release the lock. */
  457. __raw_spin_unlock(&die_lock);
  458. raw_local_irq_restore(flags);
  459. if (!regs) {
  460. oops_exit();
  461. return;
  462. }
  463. if (panic_on_oops)
  464. panic("Fatal exception");
  465. oops_exit();
  466. do_exit(signr);
  467. }
  468. int __kprobes __die(const char *str, struct pt_regs *regs, long err)
  469. {
  470. printk(KERN_EMERG "%s: %04lx [%u] ", str, err & 0xffff, ++die_counter);
  471. #ifdef CONFIG_PREEMPT
  472. printk("PREEMPT ");
  473. #endif
  474. #ifdef CONFIG_SMP
  475. printk("SMP ");
  476. #endif
  477. #ifdef CONFIG_DEBUG_PAGEALLOC
  478. printk("DEBUG_PAGEALLOC");
  479. #endif
  480. printk("\n");
  481. if (notify_die(DIE_OOPS, str, regs, err,
  482. current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
  483. return 1;
  484. show_registers(regs);
  485. add_taint(TAINT_DIE);
  486. /* Executive summary in case the oops scrolled away */
  487. printk(KERN_ALERT "RIP ");
  488. printk_address(regs->ip, 1);
  489. printk(" RSP <%016lx>\n", regs->sp);
  490. if (kexec_should_crash(current))
  491. crash_kexec(regs);
  492. return 0;
  493. }
  494. void die(const char *str, struct pt_regs *regs, long err)
  495. {
  496. unsigned long flags = oops_begin();
  497. if (!user_mode(regs))
  498. report_bug(regs->ip, regs);
  499. if (__die(str, regs, err))
  500. regs = NULL;
  501. oops_end(flags, regs, SIGSEGV);
  502. }
  503. notrace __kprobes void
  504. die_nmi(char *str, struct pt_regs *regs, int do_panic)
  505. {
  506. unsigned long flags;
  507. if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
  508. return;
  509. flags = oops_begin();
  510. /*
  511. * We are in trouble anyway, lets at least try
  512. * to get a message out.
  513. */
  514. printk(KERN_EMERG "%s", str);
  515. printk(" on CPU%d, ip %08lx, registers:\n",
  516. smp_processor_id(), regs->ip);
  517. show_registers(regs);
  518. if (kexec_should_crash(current))
  519. crash_kexec(regs);
  520. if (do_panic || panic_on_oops)
  521. panic("Non maskable interrupt");
  522. oops_end(flags, NULL, SIGBUS);
  523. nmi_exit();
  524. local_irq_enable();
  525. do_exit(SIGBUS);
  526. }
  527. static void __kprobes
  528. do_trap(int trapnr, int signr, char *str, struct pt_regs *regs,
  529. long error_code, siginfo_t *info)
  530. {
  531. struct task_struct *tsk = current;
  532. if (!user_mode(regs))
  533. goto kernel_trap;
  534. /*
  535. * We want error_code and trap_no set for userspace faults and
  536. * kernelspace faults which result in die(), but not
  537. * kernelspace faults which are fixed up. die() gives the
  538. * process no chance to handle the signal and notice the
  539. * kernel fault information, so that won't result in polluting
  540. * the information about previously queued, but not yet
  541. * delivered, faults. See also do_general_protection below.
  542. */
  543. tsk->thread.error_code = error_code;
  544. tsk->thread.trap_no = trapnr;
  545. if (show_unhandled_signals && unhandled_signal(tsk, signr) &&
  546. printk_ratelimit()) {
  547. printk(KERN_INFO
  548. "%s[%d] trap %s ip:%lx sp:%lx error:%lx",
  549. tsk->comm, tsk->pid, str,
  550. regs->ip, regs->sp, error_code);
  551. print_vma_addr(" in ", regs->ip);
  552. printk("\n");
  553. }
  554. if (info)
  555. force_sig_info(signr, info, tsk);
  556. else
  557. force_sig(signr, tsk);
  558. return;
  559. kernel_trap:
  560. if (!fixup_exception(regs)) {
  561. tsk->thread.error_code = error_code;
  562. tsk->thread.trap_no = trapnr;
  563. die(str, regs, error_code);
  564. }
  565. return;
  566. }
  567. #define DO_ERROR(trapnr, signr, str, name) \
  568. asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
  569. { \
  570. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
  571. == NOTIFY_STOP) \
  572. return; \
  573. conditional_sti(regs); \
  574. do_trap(trapnr, signr, str, regs, error_code, NULL); \
  575. }
  576. #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
  577. asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
  578. { \
  579. siginfo_t info; \
  580. info.si_signo = signr; \
  581. info.si_errno = 0; \
  582. info.si_code = sicode; \
  583. info.si_addr = (void __user *)siaddr; \
  584. trace_hardirqs_fixup(); \
  585. if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) \
  586. == NOTIFY_STOP) \
  587. return; \
  588. conditional_sti(regs); \
  589. do_trap(trapnr, signr, str, regs, error_code, &info); \
  590. }
  591. DO_ERROR_INFO(0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->ip)
  592. DO_ERROR(4, SIGSEGV, "overflow", overflow)
  593. DO_ERROR(5, SIGSEGV, "bounds", bounds)
  594. DO_ERROR_INFO(6, SIGILL, "invalid opcode", invalid_op, ILL_ILLOPN, regs->ip)
  595. DO_ERROR(9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
  596. DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
  597. DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
  598. DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, 0)
  599. /* Runs on IST stack */
  600. asmlinkage void do_stack_segment(struct pt_regs *regs, long error_code)
  601. {
  602. if (notify_die(DIE_TRAP, "stack segment", regs, error_code,
  603. 12, SIGBUS) == NOTIFY_STOP)
  604. return;
  605. preempt_conditional_sti(regs);
  606. do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL);
  607. preempt_conditional_cli(regs);
  608. }
  609. asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
  610. {
  611. static const char str[] = "double fault";
  612. struct task_struct *tsk = current;
  613. /* Return not checked because double check cannot be ignored */
  614. notify_die(DIE_TRAP, str, regs, error_code, 8, SIGSEGV);
  615. tsk->thread.error_code = error_code;
  616. tsk->thread.trap_no = 8;
  617. /* This is always a kernel trap and never fixable (and thus must
  618. never return). */
  619. for (;;)
  620. die(str, regs, error_code);
  621. }
  622. asmlinkage void __kprobes
  623. do_general_protection(struct pt_regs *regs, long error_code)
  624. {
  625. struct task_struct *tsk;
  626. conditional_sti(regs);
  627. tsk = current;
  628. if (!user_mode(regs))
  629. goto gp_in_kernel;
  630. tsk->thread.error_code = error_code;
  631. tsk->thread.trap_no = 13;
  632. if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
  633. printk_ratelimit()) {
  634. printk(KERN_INFO
  635. "%s[%d] general protection ip:%lx sp:%lx error:%lx",
  636. tsk->comm, tsk->pid,
  637. regs->ip, regs->sp, error_code);
  638. print_vma_addr(" in ", regs->ip);
  639. printk("\n");
  640. }
  641. force_sig(SIGSEGV, tsk);
  642. return;
  643. gp_in_kernel:
  644. if (fixup_exception(regs))
  645. return;
  646. tsk->thread.error_code = error_code;
  647. tsk->thread.trap_no = 13;
  648. if (notify_die(DIE_GPF, "general protection fault", regs,
  649. error_code, 13, SIGSEGV) == NOTIFY_STOP)
  650. return;
  651. die("general protection fault", regs, error_code);
  652. }
  653. static notrace __kprobes void
  654. mem_parity_error(unsigned char reason, struct pt_regs *regs)
  655. {
  656. printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
  657. reason);
  658. printk(KERN_EMERG "You have some hardware problem, likely on the PCI bus.\n");
  659. #if defined(CONFIG_EDAC)
  660. if (edac_handler_set()) {
  661. edac_atomic_assert_error();
  662. return;
  663. }
  664. #endif
  665. if (panic_on_unrecovered_nmi)
  666. panic("NMI: Not continuing");
  667. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  668. /* Clear and disable the memory parity error line. */
  669. reason = (reason & 0xf) | 4;
  670. outb(reason, 0x61);
  671. }
  672. static notrace __kprobes void
  673. io_check_error(unsigned char reason, struct pt_regs *regs)
  674. {
  675. printk("NMI: IOCK error (debug interrupt?)\n");
  676. show_registers(regs);
  677. /* Re-enable the IOCK line, wait for a few seconds */
  678. reason = (reason & 0xf) | 8;
  679. outb(reason, 0x61);
  680. mdelay(2000);
  681. reason &= ~8;
  682. outb(reason, 0x61);
  683. }
  684. static notrace __kprobes void
  685. unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
  686. {
  687. if (notify_die(DIE_NMIUNKNOWN, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
  688. return;
  689. printk(KERN_EMERG "Uhhuh. NMI received for unknown reason %02x.\n",
  690. reason);
  691. printk(KERN_EMERG "Do you have a strange power saving mode enabled?\n");
  692. if (panic_on_unrecovered_nmi)
  693. panic("NMI: Not continuing");
  694. printk(KERN_EMERG "Dazed and confused, but trying to continue\n");
  695. }
  696. /* Runs on IST stack. This code must keep interrupts off all the time.
  697. Nested NMIs are prevented by the CPU. */
  698. asmlinkage notrace __kprobes void default_do_nmi(struct pt_regs *regs)
  699. {
  700. unsigned char reason = 0;
  701. int cpu;
  702. cpu = smp_processor_id();
  703. /* Only the BSP gets external NMIs from the system. */
  704. if (!cpu)
  705. reason = get_nmi_reason();
  706. if (!(reason & 0xc0)) {
  707. if (notify_die(DIE_NMI_IPI, "nmi_ipi", regs, reason, 2, SIGINT)
  708. == NOTIFY_STOP)
  709. return;
  710. /*
  711. * Ok, so this is none of the documented NMI sources,
  712. * so it must be the NMI watchdog.
  713. */
  714. if (nmi_watchdog_tick(regs, reason))
  715. return;
  716. if (!do_nmi_callback(regs, cpu))
  717. unknown_nmi_error(reason, regs);
  718. return;
  719. }
  720. if (notify_die(DIE_NMI, "nmi", regs, reason, 2, SIGINT) == NOTIFY_STOP)
  721. return;
  722. /* AK: following checks seem to be broken on modern chipsets. FIXME */
  723. if (reason & 0x80)
  724. mem_parity_error(reason, regs);
  725. if (reason & 0x40)
  726. io_check_error(reason, regs);
  727. }
  728. asmlinkage notrace __kprobes void
  729. do_nmi(struct pt_regs *regs, long error_code)
  730. {
  731. nmi_enter();
  732. add_pda(__nmi_count, 1);
  733. if (!ignore_nmis)
  734. default_do_nmi(regs);
  735. nmi_exit();
  736. }
  737. void stop_nmi(void)
  738. {
  739. acpi_nmi_disable();
  740. ignore_nmis++;
  741. }
  742. void restart_nmi(void)
  743. {
  744. ignore_nmis--;
  745. acpi_nmi_enable();
  746. }
  747. /* runs on IST stack. */
  748. asmlinkage void __kprobes do_int3(struct pt_regs *regs, long error_code)
  749. {
  750. trace_hardirqs_fixup();
  751. if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP)
  752. == NOTIFY_STOP)
  753. return;
  754. preempt_conditional_sti(regs);
  755. do_trap(3, SIGTRAP, "int3", regs, error_code, NULL);
  756. preempt_conditional_cli(regs);
  757. }
  758. /* Help handler running on IST stack to switch back to user stack
  759. for scheduling or signal handling. The actual stack switch is done in
  760. entry.S */
  761. asmlinkage __kprobes struct pt_regs *sync_regs(struct pt_regs *eregs)
  762. {
  763. struct pt_regs *regs = eregs;
  764. /* Did already sync */
  765. if (eregs == (struct pt_regs *)eregs->sp)
  766. ;
  767. /* Exception from user space */
  768. else if (user_mode(eregs))
  769. regs = task_pt_regs(current);
  770. /* Exception from kernel and interrupts are enabled. Move to
  771. kernel process stack. */
  772. else if (eregs->flags & X86_EFLAGS_IF)
  773. regs = (struct pt_regs *)(eregs->sp -= sizeof(struct pt_regs));
  774. if (eregs != regs)
  775. *regs = *eregs;
  776. return regs;
  777. }
  778. /* runs on IST stack. */
  779. asmlinkage void __kprobes do_debug(struct pt_regs * regs,
  780. unsigned long error_code)
  781. {
  782. struct task_struct *tsk = current;
  783. unsigned long condition;
  784. siginfo_t info;
  785. trace_hardirqs_fixup();
  786. get_debugreg(condition, 6);
  787. /*
  788. * The processor cleared BTF, so don't mark that we need it set.
  789. */
  790. clear_tsk_thread_flag(tsk, TIF_DEBUGCTLMSR);
  791. tsk->thread.debugctlmsr = 0;
  792. if (notify_die(DIE_DEBUG, "debug", regs, condition, error_code,
  793. SIGTRAP) == NOTIFY_STOP)
  794. return;
  795. preempt_conditional_sti(regs);
  796. /* Mask out spurious debug traps due to lazy DR7 setting */
  797. if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
  798. if (!tsk->thread.debugreg7)
  799. goto clear_dr7;
  800. }
  801. tsk->thread.debugreg6 = condition;
  802. /*
  803. * Single-stepping through TF: make sure we ignore any events in
  804. * kernel space (but re-enable TF when returning to user mode).
  805. */
  806. if (condition & DR_STEP) {
  807. if (!user_mode(regs))
  808. goto clear_TF_reenable;
  809. }
  810. /* Ok, finally something we can handle */
  811. tsk->thread.trap_no = 1;
  812. tsk->thread.error_code = error_code;
  813. info.si_signo = SIGTRAP;
  814. info.si_errno = 0;
  815. info.si_code = TRAP_BRKPT;
  816. info.si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
  817. force_sig_info(SIGTRAP, &info, tsk);
  818. clear_dr7:
  819. set_debugreg(0, 7);
  820. preempt_conditional_cli(regs);
  821. return;
  822. clear_TF_reenable:
  823. set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
  824. regs->flags &= ~X86_EFLAGS_TF;
  825. preempt_conditional_cli(regs);
  826. return;
  827. }
  828. static int kernel_math_error(struct pt_regs *regs, const char *str, int trapnr)
  829. {
  830. if (fixup_exception(regs))
  831. return 1;
  832. notify_die(DIE_GPF, str, regs, 0, trapnr, SIGFPE);
  833. /* Illegal floating point operation in the kernel */
  834. current->thread.trap_no = trapnr;
  835. die(str, regs, 0);
  836. return 0;
  837. }
  838. /*
  839. * Note that we play around with the 'TS' bit in an attempt to get
  840. * the correct behaviour even in the presence of the asynchronous
  841. * IRQ13 behaviour
  842. */
  843. asmlinkage void do_coprocessor_error(struct pt_regs *regs)
  844. {
  845. void __user *ip = (void __user *)(regs->ip);
  846. struct task_struct *task;
  847. siginfo_t info;
  848. unsigned short cwd, swd;
  849. conditional_sti(regs);
  850. if (!user_mode(regs) &&
  851. kernel_math_error(regs, "kernel x87 math error", 16))
  852. return;
  853. /*
  854. * Save the info for the exception handler and clear the error.
  855. */
  856. task = current;
  857. save_init_fpu(task);
  858. task->thread.trap_no = 16;
  859. task->thread.error_code = 0;
  860. info.si_signo = SIGFPE;
  861. info.si_errno = 0;
  862. info.si_code = __SI_FAULT;
  863. info.si_addr = ip;
  864. /*
  865. * (~cwd & swd) will mask out exceptions that are not set to unmasked
  866. * status. 0x3f is the exception bits in these regs, 0x200 is the
  867. * C1 reg you need in case of a stack fault, 0x040 is the stack
  868. * fault bit. We should only be taking one exception at a time,
  869. * so if this combination doesn't produce any single exception,
  870. * then we have a bad program that isn't synchronizing its FPU usage
  871. * and it will suffer the consequences since we won't be able to
  872. * fully reproduce the context of the exception
  873. */
  874. cwd = get_fpu_cwd(task);
  875. swd = get_fpu_swd(task);
  876. switch (swd & ~cwd & 0x3f) {
  877. case 0x000: /* No unmasked exception */
  878. default: /* Multiple exceptions */
  879. break;
  880. case 0x001: /* Invalid Op */
  881. /*
  882. * swd & 0x240 == 0x040: Stack Underflow
  883. * swd & 0x240 == 0x240: Stack Overflow
  884. * User must clear the SF bit (0x40) if set
  885. */
  886. info.si_code = FPE_FLTINV;
  887. break;
  888. case 0x002: /* Denormalize */
  889. case 0x010: /* Underflow */
  890. info.si_code = FPE_FLTUND;
  891. break;
  892. case 0x004: /* Zero Divide */
  893. info.si_code = FPE_FLTDIV;
  894. break;
  895. case 0x008: /* Overflow */
  896. info.si_code = FPE_FLTOVF;
  897. break;
  898. case 0x020: /* Precision */
  899. info.si_code = FPE_FLTRES;
  900. break;
  901. }
  902. force_sig_info(SIGFPE, &info, task);
  903. }
  904. asmlinkage void bad_intr(void)
  905. {
  906. printk("bad interrupt");
  907. }
  908. asmlinkage void do_simd_coprocessor_error(struct pt_regs *regs)
  909. {
  910. void __user *ip = (void __user *)(regs->ip);
  911. struct task_struct *task;
  912. siginfo_t info;
  913. unsigned short mxcsr;
  914. conditional_sti(regs);
  915. if (!user_mode(regs) &&
  916. kernel_math_error(regs, "kernel simd math error", 19))
  917. return;
  918. /*
  919. * Save the info for the exception handler and clear the error.
  920. */
  921. task = current;
  922. save_init_fpu(task);
  923. task->thread.trap_no = 19;
  924. task->thread.error_code = 0;
  925. info.si_signo = SIGFPE;
  926. info.si_errno = 0;
  927. info.si_code = __SI_FAULT;
  928. info.si_addr = ip;
  929. /*
  930. * The SIMD FPU exceptions are handled a little differently, as there
  931. * is only a single status/control register. Thus, to determine which
  932. * unmasked exception was caught we must mask the exception mask bits
  933. * at 0x1f80, and then use these to mask the exception bits at 0x3f.
  934. */
  935. mxcsr = get_fpu_mxcsr(task);
  936. switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
  937. case 0x000:
  938. default:
  939. break;
  940. case 0x001: /* Invalid Op */
  941. info.si_code = FPE_FLTINV;
  942. break;
  943. case 0x002: /* Denormalize */
  944. case 0x010: /* Underflow */
  945. info.si_code = FPE_FLTUND;
  946. break;
  947. case 0x004: /* Zero Divide */
  948. info.si_code = FPE_FLTDIV;
  949. break;
  950. case 0x008: /* Overflow */
  951. info.si_code = FPE_FLTOVF;
  952. break;
  953. case 0x020: /* Precision */
  954. info.si_code = FPE_FLTRES;
  955. break;
  956. }
  957. force_sig_info(SIGFPE, &info, task);
  958. }
  959. asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs)
  960. {
  961. }
  962. asmlinkage void __attribute__((weak)) smp_thermal_interrupt(void)
  963. {
  964. }
  965. asmlinkage void __attribute__((weak)) mce_threshold_interrupt(void)
  966. {
  967. }
  968. /*
  969. * 'math_state_restore()' saves the current math information in the
  970. * old math state array, and gets the new ones from the current task
  971. *
  972. * Careful.. There are problems with IBM-designed IRQ13 behaviour.
  973. * Don't touch unless you *really* know how it works.
  974. */
  975. asmlinkage void math_state_restore(void)
  976. {
  977. struct task_struct *me = current;
  978. if (!used_math()) {
  979. local_irq_enable();
  980. /*
  981. * does a slab alloc which can sleep
  982. */
  983. if (init_fpu(me)) {
  984. /*
  985. * ran out of memory!
  986. */
  987. do_group_exit(SIGKILL);
  988. return;
  989. }
  990. local_irq_disable();
  991. }
  992. clts(); /* Allow maths ops (or we recurse) */
  993. /*
  994. * Paranoid restore. send a SIGSEGV if we fail to restore the state.
  995. */
  996. if (unlikely(restore_fpu_checking(&me->thread.xstate->fxsave))) {
  997. stts();
  998. force_sig(SIGSEGV, me);
  999. return;
  1000. }
  1001. task_thread_info(me)->status |= TS_USEDFPU;
  1002. me->fpu_counter++;
  1003. }
  1004. EXPORT_SYMBOL_GPL(math_state_restore);
  1005. void __init trap_init(void)
  1006. {
  1007. set_intr_gate(0, &divide_error);
  1008. set_intr_gate_ist(1, &debug, DEBUG_STACK);
  1009. set_intr_gate_ist(2, &nmi, NMI_STACK);
  1010. set_system_gate_ist(3, &int3, DEBUG_STACK); /* int3 can be called from all */
  1011. set_system_gate(4, &overflow); /* int4 can be called from all */
  1012. set_intr_gate(5, &bounds);
  1013. set_intr_gate(6, &invalid_op);
  1014. set_intr_gate(7, &device_not_available);
  1015. set_intr_gate_ist(8, &double_fault, DOUBLEFAULT_STACK);
  1016. set_intr_gate(9, &coprocessor_segment_overrun);
  1017. set_intr_gate(10, &invalid_TSS);
  1018. set_intr_gate(11, &segment_not_present);
  1019. set_intr_gate_ist(12, &stack_segment, STACKFAULT_STACK);
  1020. set_intr_gate(13, &general_protection);
  1021. set_intr_gate(14, &page_fault);
  1022. set_intr_gate(15, &spurious_interrupt_bug);
  1023. set_intr_gate(16, &coprocessor_error);
  1024. set_intr_gate(17, &alignment_check);
  1025. #ifdef CONFIG_X86_MCE
  1026. set_intr_gate_ist(18, &machine_check, MCE_STACK);
  1027. #endif
  1028. set_intr_gate(19, &simd_coprocessor_error);
  1029. #ifdef CONFIG_IA32_EMULATION
  1030. set_system_gate(IA32_SYSCALL_VECTOR, ia32_syscall);
  1031. #endif
  1032. /*
  1033. * initialize the per thread extended state:
  1034. */
  1035. init_thread_xstate();
  1036. /*
  1037. * Should be a barrier for any external CPU state:
  1038. */
  1039. cpu_init();
  1040. }
  1041. static int __init oops_setup(char *s)
  1042. {
  1043. if (!s)
  1044. return -EINVAL;
  1045. if (!strcmp(s, "panic"))
  1046. panic_on_oops = 1;
  1047. return 0;
  1048. }
  1049. early_param("oops", oops_setup);
  1050. static int __init kstack_setup(char *s)
  1051. {
  1052. if (!s)
  1053. return -EINVAL;
  1054. kstack_depth_to_print = simple_strtoul(s, NULL, 0);
  1055. return 0;
  1056. }
  1057. early_param("kstack", kstack_setup);
  1058. static int __init code_bytes_setup(char *s)
  1059. {
  1060. code_bytes = simple_strtoul(s, NULL, 0);
  1061. if (code_bytes > 8192)
  1062. code_bytes = 8192;
  1063. return 1;
  1064. }
  1065. __setup("code_bytes=", code_bytes_setup);