traps_64.c 28 KB

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