traps.c 27 KB

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