traps_64.c 30 KB

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