traps_64.c 28 KB

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