traps.c 28 KB

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