traps_64.c 29 KB

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